xref: /titanic_52/usr/src/uts/sun4/io/px/px.c (revision 2f172c55ef76964744bc62b4500ece87f3089b4d)
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  * 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 		if (pxtool_init(dip) != DDI_SUCCESS)
327 			goto err_bad_pcitool_node;
328 
329 		/*
330 		 * power management setup. Even if it fails, attach will
331 		 * succeed as this is a optional feature. Since we are
332 		 * always at full power, this is not critical.
333 		 */
334 		if (pwr_common_setup(dip) != DDI_SUCCESS) {
335 			DBG(DBG_PWR, dip, "pwr_common_setup failed\n");
336 		} else if (px_pwr_setup(dip) != DDI_SUCCESS) {
337 			DBG(DBG_PWR, dip, "px_pwr_setup failed \n");
338 			pwr_common_teardown(dip);
339 		}
340 
341 		/*
342 		 * add cpr callback
343 		 */
344 		px_cpr_add_callb(px_p);
345 
346 		ddi_report_dev(dip);
347 
348 		px_p->px_state = PX_ATTACHED;
349 
350 		/*
351 		 * save base addr in bus_t for pci_cfgacc_xxx(), this
352 		 * depends of px structure being properly initialized.
353 		 */
354 		bus_p = PCIE_DIP2BUS(dip);
355 		bus_p->bus_cfgacc_base = px_lib_get_cfgacc_base(dip);
356 
357 		/*
358 		 * Partially populate bus_t for all devices in this fabric
359 		 * for device type macros to work.
360 		 */
361 		/*
362 		 * Populate bus_t for all devices in this fabric, after FMA
363 		 * is initializated, so that config access errors could
364 		 * trigger panic.
365 		 */
366 		pcie_fab_init_bus(dip, PCIE_BUS_ALL);
367 
368 		DBG(DBG_ATTACH, dip, "attach success\n");
369 		break;
370 
371 err_bad_pcitool_node:
372 		(void) pcie_uninit(dip);
373 err_bad_hotplug:
374 		(void) px_lib_hotplug_uninit(dip);
375 		px_err_rem_intr(&px_p->px_fault);
376 err_bad_intr:
377 		px_fm_detach(px_p);
378 err_bad_dma:
379 		px_pec_detach(px_p);
380 err_bad_pec:
381 		px_msi_detach(px_p);
382 err_bad_msi:
383 		px_msiq_detach(px_p);
384 err_bad_msiq:
385 		px_mmu_detach(px_p);
386 err_bad_mmu:
387 		px_cb_detach(px_p);
388 err_bad_cb:
389 		px_ib_detach(px_p);
390 err_bad_ib:
391 		if (px_lib_dev_fini(dip) != DDI_SUCCESS) {
392 			DBG(DBG_ATTACH, dip, "px_lib_dev_fini failed\n");
393 		}
394 err_bad_dev_init:
395 		px_free_props(px_p);
396 err_bad_px_prop:
397 		pcie_rc_fini_bus(dip);
398 		px_dbg_detach(dip, &px_p->px_dbg_hdl);
399 		mutex_destroy(&px_p->px_mutex);
400 		ddi_soft_state_free(px_state_p, instance);
401 err_bad_px_softstate:
402 		ret = DDI_FAILURE;
403 		break;
404 
405 	case DDI_RESUME:
406 		DBG(DBG_ATTACH, dip, "DDI_RESUME\n");
407 
408 		px_p = INST_TO_STATE(instance);
409 
410 		mutex_enter(&px_p->px_mutex);
411 
412 		/* suspend might have not succeeded */
413 		if (px_p->px_state != PX_SUSPENDED) {
414 			DBG(DBG_ATTACH, px_p->px_dip,
415 			    "instance NOT suspended\n");
416 			ret = DDI_FAILURE;
417 			break;
418 		}
419 
420 		px_msiq_resume(px_p);
421 		px_lib_resume(dip);
422 		(void) pcie_pwr_resume(dip);
423 		px_p->px_state = PX_ATTACHED;
424 
425 		mutex_exit(&px_p->px_mutex);
426 
427 		break;
428 	default:
429 		DBG(DBG_ATTACH, dip, "unsupported attach op\n");
430 		ret = DDI_FAILURE;
431 		break;
432 	}
433 
434 	return (ret);
435 }
436 
437 /*
438  * detach entry point:
439  */
440 /*ARGSUSED*/
441 static int
442 px_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
443 {
444 	int		instance = ddi_get_instance(dip);
445 	px_t		*px_p = INST_TO_STATE(instance);
446 	pcie_bus_t	*bus_p = PCIE_DIP2BUS(dip);
447 	int		ret;
448 
449 	/*
450 	 * Make sure we are currently attached
451 	 */
452 	if (px_p->px_state != PX_ATTACHED) {
453 		DBG(DBG_DETACH, dip, "Instance not attached\n");
454 		return (DDI_FAILURE);
455 	}
456 
457 	mutex_enter(&px_p->px_mutex);
458 
459 	switch (cmd) {
460 	case DDI_DETACH:
461 		DBG(DBG_DETACH, dip, "DDI_DETACH\n");
462 
463 		/*
464 		 * remove cpr callback
465 		 */
466 		px_cpr_rem_callb(px_p);
467 
468 		if (PCIE_IS_PCIE_HOTPLUG_ENABLED(bus_p))
469 			(void) px_lib_hotplug_uninit(dip);
470 
471 		if (pcie_uninit(dip) != DDI_SUCCESS) {
472 			mutex_exit(&px_p->px_mutex);
473 			return (DDI_FAILURE);
474 		}
475 
476 		/* Destroy bus_t for the whole fabric */
477 		pcie_fab_fini_bus(dip, PCIE_BUS_ALL);
478 
479 		/*
480 		 * things which used to be done in obj_destroy
481 		 * are now in-lined here.
482 		 */
483 
484 		px_p->px_state = PX_DETACHED;
485 
486 		pxtool_uninit(dip);
487 
488 		px_err_rem_intr(&px_p->px_fault);
489 		px_fm_detach(px_p);
490 		px_pec_detach(px_p);
491 		px_pwr_teardown(dip);
492 		pwr_common_teardown(dip);
493 		px_msi_detach(px_p);
494 		px_msiq_detach(px_p);
495 		px_mmu_detach(px_p);
496 		px_cb_detach(px_p);
497 		px_ib_detach(px_p);
498 		if (px_lib_dev_fini(dip) != DDI_SUCCESS) {
499 			DBG(DBG_DETACH, dip, "px_lib_dev_fini failed\n");
500 		}
501 
502 		/*
503 		 * Free the px soft state structure and the rest of the
504 		 * resources it's using.
505 		 */
506 		px_free_props(px_p);
507 		pcie_rc_fini_bus(dip);
508 		px_dbg_detach(dip, &px_p->px_dbg_hdl);
509 		mutex_exit(&px_p->px_mutex);
510 		mutex_destroy(&px_p->px_mutex);
511 
512 		px_p->px_dev_hdl = NULL;
513 		ddi_soft_state_free(px_state_p, instance);
514 
515 		return (DDI_SUCCESS);
516 
517 	case DDI_SUSPEND:
518 		if (pcie_pwr_suspend(dip) != DDI_SUCCESS) {
519 			mutex_exit(&px_p->px_mutex);
520 			return (DDI_FAILURE);
521 		}
522 		if ((ret = px_lib_suspend(dip)) == DDI_SUCCESS)
523 			px_p->px_state = PX_SUSPENDED;
524 		mutex_exit(&px_p->px_mutex);
525 
526 		return (ret);
527 
528 	default:
529 		DBG(DBG_DETACH, dip, "unsupported detach op\n");
530 		mutex_exit(&px_p->px_mutex);
531 		return (DDI_FAILURE);
532 	}
533 }
534 
535 int
536 px_cb_attach(px_t *px_p)
537 {
538 	px_fault_t	*fault_p = &px_p->px_cb_fault;
539 	dev_info_t	*dip = px_p->px_dip;
540 	sysino_t	sysino;
541 
542 	if (px_lib_intr_devino_to_sysino(dip,
543 	    px_p->px_inos[PX_INTR_XBC], &sysino) != DDI_SUCCESS)
544 		return (DDI_FAILURE);
545 
546 	fault_p->px_fh_dip = dip;
547 	fault_p->px_fh_sysino = sysino;
548 	fault_p->px_err_func = px_err_cb_intr;
549 	fault_p->px_intr_ino = px_p->px_inos[PX_INTR_XBC];
550 
551 	return (px_cb_add_intr(fault_p));
552 }
553 
554 void
555 px_cb_detach(px_t *px_p)
556 {
557 	px_cb_rem_intr(&px_p->px_cb_fault);
558 }
559 
560 /*
561  * power management related initialization specific to px
562  * called by px_attach()
563  */
564 static int
565 px_pwr_setup(dev_info_t *dip)
566 {
567 	pcie_pwr_t *pwr_p;
568 	int instance = ddi_get_instance(dip);
569 	px_t *px_p = INST_TO_STATE(instance);
570 	ddi_intr_handle_impl_t hdl;
571 
572 	ASSERT(PCIE_PMINFO(dip));
573 	pwr_p = PCIE_NEXUS_PMINFO(dip);
574 	ASSERT(pwr_p);
575 
576 	/*
577 	 * indicate support LDI (Layered Driver Interface)
578 	 * Create the property, if it is not already there
579 	 */
580 	if (!ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
581 	    DDI_KERNEL_IOCTL)) {
582 		if (ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
583 		    DDI_KERNEL_IOCTL, NULL, 0) != DDI_PROP_SUCCESS) {
584 			DBG(DBG_PWR, dip, "can't create kernel ioctl prop\n");
585 			return (DDI_FAILURE);
586 		}
587 	}
588 	/* No support for device PM. We are always at full power */
589 	pwr_p->pwr_func_lvl = PM_LEVEL_D0;
590 
591 	mutex_init(&px_p->px_l23ready_lock, NULL, MUTEX_DRIVER,
592 	    DDI_INTR_PRI(px_pwr_pil));
593 	cv_init(&px_p->px_l23ready_cv, NULL, CV_DRIVER, NULL);
594 
595 	/* Initialize handle */
596 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
597 	hdl.ih_cb_arg1 = px_p;
598 	hdl.ih_ver = DDI_INTR_VERSION;
599 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
600 	hdl.ih_dip = dip;
601 	hdl.ih_pri = px_pwr_pil;
602 
603 	/* Add PME_TO_ACK message handler */
604 	hdl.ih_cb_func = (ddi_intr_handler_t *)px_pmeq_intr;
605 	if (px_add_msiq_intr(dip, dip, &hdl, MSG_REC,
606 	    (msgcode_t)PCIE_PME_ACK_MSG, -1,
607 	    &px_p->px_pm_msiq_id) != DDI_SUCCESS) {
608 		DBG(DBG_PWR, dip, "px_pwr_setup: couldn't add "
609 		    " PME_TO_ACK intr\n");
610 		goto pwr_setup_err1;
611 	}
612 	px_lib_msg_setmsiq(dip, PCIE_PME_ACK_MSG, px_p->px_pm_msiq_id);
613 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_VALID);
614 
615 	if (px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
616 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
617 	    PX_INTR_STATE_ENABLE, MSG_REC, PCIE_PME_ACK_MSG) != DDI_SUCCESS) {
618 		DBG(DBG_PWR, dip, "px_pwr_setup: PME_TO_ACK update interrupt"
619 		    " state failed\n");
620 		goto px_pwrsetup_err_state;
621 	}
622 
623 	return (DDI_SUCCESS);
624 
625 px_pwrsetup_err_state:
626 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
627 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
628 	    px_p->px_pm_msiq_id);
629 pwr_setup_err1:
630 	mutex_destroy(&px_p->px_l23ready_lock);
631 	cv_destroy(&px_p->px_l23ready_cv);
632 
633 	return (DDI_FAILURE);
634 }
635 
636 /*
637  * undo whatever is done in px_pwr_setup. called by px_detach()
638  */
639 static void
640 px_pwr_teardown(dev_info_t *dip)
641 {
642 	int instance = ddi_get_instance(dip);
643 	px_t *px_p = INST_TO_STATE(instance);
644 	ddi_intr_handle_impl_t	hdl;
645 
646 	if (!PCIE_PMINFO(dip) || !PCIE_NEXUS_PMINFO(dip))
647 		return;
648 
649 	/* Initialize handle */
650 	bzero(&hdl, sizeof (ddi_intr_handle_impl_t));
651 	hdl.ih_ver = DDI_INTR_VERSION;
652 	hdl.ih_state = DDI_IHDL_STATE_ALLOC;
653 	hdl.ih_dip = dip;
654 	hdl.ih_pri = px_pwr_pil;
655 
656 	px_lib_msg_setvalid(dip, PCIE_PME_ACK_MSG, PCIE_MSG_INVALID);
657 	(void) px_rem_msiq_intr(dip, dip, &hdl, MSG_REC, PCIE_PME_ACK_MSG,
658 	    px_p->px_pm_msiq_id);
659 
660 	(void) px_ib_update_intr_state(px_p, px_p->px_dip, hdl.ih_inum,
661 	    px_msiqid_to_devino(px_p, px_p->px_pm_msiq_id), px_pwr_pil,
662 	    PX_INTR_STATE_DISABLE, MSG_REC, PCIE_PME_ACK_MSG);
663 
664 	px_p->px_pm_msiq_id = (msiqid_t)-1;
665 
666 	cv_destroy(&px_p->px_l23ready_cv);
667 	mutex_destroy(&px_p->px_l23ready_lock);
668 }
669 
670 /* bus driver entry points */
671 
672 /*
673  * bus map entry point:
674  *
675  * 	if map request is for an rnumber
676  *		get the corresponding regspec from device node
677  * 	build a new regspec in our parent's format
678  *	build a new map_req with the new regspec
679  *	call up the tree to complete the mapping
680  */
681 int
682 px_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
683 	off_t off, off_t len, caddr_t *addrp)
684 {
685 	px_t *px_p = DIP_TO_STATE(dip);
686 	struct regspec p_regspec;
687 	ddi_map_req_t p_mapreq;
688 	int reglen, rval, r_no;
689 	pci_regspec_t reloc_reg, *rp = &reloc_reg;
690 
691 	DBG(DBG_MAP, dip, "rdip=%s%d:",
692 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
693 
694 	if (mp->map_flags & DDI_MF_USER_MAPPING)
695 		return (DDI_ME_UNIMPLEMENTED);
696 
697 	switch (mp->map_type) {
698 	case DDI_MT_REGSPEC:
699 		reloc_reg = *(pci_regspec_t *)mp->map_obj.rp;	/* dup whole */
700 		break;
701 
702 	case DDI_MT_RNUMBER:
703 		r_no = mp->map_obj.rnumber;
704 		DBG(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no);
705 
706 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
707 		    "reg", (caddr_t)&rp, &reglen) != DDI_SUCCESS)
708 			return (DDI_ME_RNUMBER_RANGE);
709 
710 		if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) {
711 			kmem_free(rp, reglen);
712 			return (DDI_ME_RNUMBER_RANGE);
713 		}
714 		rp += r_no;
715 		break;
716 
717 	default:
718 		return (DDI_ME_INVAL);
719 	}
720 	DBG(DBG_MAP | DBG_CONT, dip, "\n");
721 
722 	if ((rp->pci_phys_hi & PCI_REG_ADDR_M) == PCI_ADDR_CONFIG) {
723 		/*
724 		 * There may be a need to differentiate between PCI
725 		 * and PCI-Ex devices so the following range check is
726 		 * done correctly, depending on the implementation of
727 		 * pcieb bridge nexus driver.
728 		 */
729 		if ((off >= PCIE_CONF_HDR_SIZE) ||
730 		    (len > PCIE_CONF_HDR_SIZE) ||
731 		    (off + len > PCIE_CONF_HDR_SIZE))
732 			return (DDI_ME_INVAL);
733 		/*
734 		 * the following function returning a DDI_FAILURE assumes
735 		 * that there are no virtual config space access services
736 		 * defined in this layer. Otherwise it is availed right
737 		 * here and we return.
738 		 */
739 		rval = px_lib_map_vconfig(dip, mp, off, rp, addrp);
740 		if (rval == DDI_SUCCESS)
741 			goto done;
742 	}
743 
744 	/*
745 	 * No virtual config space services or we are mapping
746 	 * a region of memory mapped config/IO/memory space, so proceed
747 	 * to the parent.
748 	 */
749 
750 	/* relocate within 64-bit pci space through "assigned-addresses" */
751 	if (rval = px_reloc_reg(dip, rdip, px_p, rp))
752 		goto done;
753 
754 	if (len)	/* adjust regspec according to mapping request */
755 		rp->pci_size_low = len;	/* MIN ? */
756 	rp->pci_phys_low += off;
757 
758 	/* translate relocated pci regspec into parent space through "ranges" */
759 	if (rval = px_xlate_reg(px_p, rp, &p_regspec))
760 		goto done;
761 
762 	p_mapreq = *mp;		/* dup the whole structure */
763 	p_mapreq.map_type = DDI_MT_REGSPEC;
764 	p_mapreq.map_obj.rp = &p_regspec;
765 	px_lib_map_attr_check(&p_mapreq);
766 	rval = ddi_map(dip, &p_mapreq, 0, 0, addrp);
767 
768 	if (rval == DDI_SUCCESS) {
769 		/*
770 		 * Set-up access functions for FM access error capable drivers.
771 		 */
772 		if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)))
773 			px_fm_acc_setup(mp, rdip, rp);
774 	}
775 
776 done:
777 	if (mp->map_type == DDI_MT_RNUMBER)
778 		kmem_free(rp - r_no, reglen);
779 
780 	return (rval);
781 }
782 
783 /*
784  * bus dma map entry point
785  * return value:
786  *	DDI_DMA_PARTIAL_MAP	 1
787  *	DDI_DMA_MAPOK		 0
788  *	DDI_DMA_MAPPED		 0
789  *	DDI_DMA_NORESOURCES	-1
790  *	DDI_DMA_NOMAPPING	-2
791  *	DDI_DMA_TOOBIG		-3
792  */
793 int
794 px_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq,
795 	ddi_dma_handle_t *handlep)
796 {
797 	px_t *px_p = DIP_TO_STATE(dip);
798 	px_mmu_t *mmu_p = px_p->px_mmu_p;
799 	ddi_dma_impl_t *mp;
800 	int ret;
801 
802 	DBG(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n",
803 	    ddi_driver_name(rdip), ddi_get_instance(rdip),
804 	    handlep ? "alloc" : "advisory");
805 
806 	if (!(mp = px_dma_lmts2hdl(dip, rdip, mmu_p, dmareq)))
807 		return (DDI_DMA_NORESOURCES);
808 	if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING)
809 		return (DDI_DMA_NOMAPPING);
810 	if (ret = px_dma_type(px_p, dmareq, mp))
811 		goto freehandle;
812 	if (ret = px_dma_pfn(px_p, dmareq, mp))
813 		goto freehandle;
814 
815 	switch (PX_DMA_TYPE(mp)) {
816 	case PX_DMAI_FLAGS_DVMA:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
817 		if ((ret = px_dvma_win(px_p, dmareq, mp)) || !handlep)
818 			goto freehandle;
819 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
820 			if (PX_DMA_CANFAST(mp)) {
821 				if (!px_dvma_map_fast(mmu_p, mp))
822 					break;
823 			/* LINTED E_NOP_ELSE_STMT */
824 			} else {
825 				PX_DVMA_FASTTRAK_PROF(mp);
826 			}
827 		}
828 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
829 			goto freehandle;
830 		break;
831 	case PX_DMAI_FLAGS_PTP:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
832 		if ((ret = px_dma_physwin(px_p, dmareq, mp)) || !handlep)
833 			goto freehandle;
834 		break;
835 	case PX_DMAI_FLAGS_BYPASS:
836 	default:
837 		cmn_err(CE_PANIC, "%s%d: px_dma_setup: bad dma type 0x%x",
838 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
839 		    PX_DMA_TYPE(mp));
840 		/*NOTREACHED*/
841 	}
842 	*handlep = (ddi_dma_handle_t)mp;
843 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
844 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
845 
846 	return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
847 freehandle:
848 	if (ret == DDI_DMA_NORESOURCES)
849 		px_dma_freemp(mp); /* don't run_callback() */
850 	else
851 		(void) px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
852 	return (ret);
853 }
854 
855 
856 /*
857  * bus dma alloc handle entry point:
858  */
859 int
860 px_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
861 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
862 {
863 	px_t *px_p = DIP_TO_STATE(dip);
864 	ddi_dma_impl_t *mp;
865 	int rval;
866 
867 	DBG(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n",
868 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
869 
870 	if (attrp->dma_attr_version != DMA_ATTR_V0)
871 		return (DDI_DMA_BADATTR);
872 
873 	if (!(mp = px_dma_allocmp(dip, rdip, waitfp, arg)))
874 		return (DDI_DMA_NORESOURCES);
875 
876 	/*
877 	 * Save requestor's information
878 	 */
879 	mp->dmai_attr	= *attrp; /* whole object - augmented later  */
880 	*PX_DEV_ATTR(mp)	= *attrp; /* whole object - device orig attr */
881 	DBG(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
882 
883 	/* check and convert dma attributes to handle parameters */
884 	if (rval = px_dma_attr2hdl(px_p, mp)) {
885 		px_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
886 		*handlep = NULL;
887 		return (rval);
888 	}
889 	*handlep = (ddi_dma_handle_t)mp;
890 	return (DDI_SUCCESS);
891 }
892 
893 
894 /*
895  * bus dma free handle entry point:
896  */
897 /*ARGSUSED*/
898 int
899 px_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
900 {
901 	DBG(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n",
902 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
903 	px_dma_freemp((ddi_dma_impl_t *)handle);
904 
905 	if (px_kmem_clid) {
906 		DBG(DBG_DMA_FREEH, dip, "run handle callback\n");
907 		ddi_run_callback(&px_kmem_clid);
908 	}
909 	return (DDI_SUCCESS);
910 }
911 
912 
913 /*
914  * bus dma bind handle entry point:
915  */
916 int
917 px_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
918 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
919 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
920 {
921 	px_t *px_p = DIP_TO_STATE(dip);
922 	px_mmu_t *mmu_p = px_p->px_mmu_p;
923 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
924 	int ret;
925 
926 	DBG(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n",
927 	    ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq);
928 
929 	if (mp->dmai_flags & PX_DMAI_FLAGS_INUSE)
930 		return (DDI_DMA_INUSE);
931 
932 	ASSERT((mp->dmai_flags & ~PX_DMAI_FLAGS_PRESERVE) == 0);
933 	mp->dmai_flags |= PX_DMAI_FLAGS_INUSE;
934 
935 	if (ret = px_dma_type(px_p, dmareq, mp))
936 		goto err;
937 	if (ret = px_dma_pfn(px_p, dmareq, mp))
938 		goto err;
939 
940 	switch (PX_DMA_TYPE(mp)) {
941 	case PX_DMAI_FLAGS_DVMA:
942 		if (ret = px_dvma_win(px_p, dmareq, mp))
943 			goto map_err;
944 		if (!PX_DMA_CANCACHE(mp)) {	/* try fast track */
945 			if (PX_DMA_CANFAST(mp)) {
946 				if (!px_dvma_map_fast(mmu_p, mp))
947 					goto mapped; /*LINTED E_NOP_ELSE_STMT*/
948 			} else {
949 				PX_DVMA_FASTTRAK_PROF(mp);
950 			}
951 		}
952 		if (ret = px_dvma_map(mp, dmareq, mmu_p))
953 			goto map_err;
954 mapped:
955 		*ccountp = 1;
956 		MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size);
957 		break;
958 	case PX_DMAI_FLAGS_BYPASS:
959 	case PX_DMAI_FLAGS_PTP:
960 		if (ret = px_dma_physwin(px_p, dmareq, mp))
961 			goto map_err;
962 		*ccountp = PX_WINLST(mp)->win_ncookies;
963 		*cookiep =
964 		    *(ddi_dma_cookie_t *)(PX_WINLST(mp) + 1); /* wholeobj */
965 		break;
966 	default:
967 		cmn_err(CE_PANIC, "%s%d: px_dma_bindhdl(%p): bad dma type",
968 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
969 		/*NOTREACHED*/
970 	}
971 	DBG(DBG_DMA_BINDH, dip, "cookie %" PRIx64 "+%x\n",
972 	    cookiep->dmac_address, cookiep->dmac_size);
973 	px_dump_dma_handle(DBG_DMA_MAP, dip, mp);
974 
975 	/* insert dma handle into FMA cache */
976 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
977 		(void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
978 		mp->dmai_error.err_cf = px_err_dma_hdl_check;
979 	}
980 
981 	return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
982 map_err:
983 	px_dma_freepfn(mp);
984 err:
985 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
986 	return (ret);
987 }
988 
989 
990 /*
991  * bus dma unbind handle entry point:
992  */
993 /*ARGSUSED*/
994 int
995 px_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
996 {
997 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
998 	px_t *px_p = DIP_TO_STATE(dip);
999 	px_mmu_t *mmu_p = px_p->px_mmu_p;
1000 
1001 	DBG(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
1002 	    ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
1003 	if ((mp->dmai_flags & PX_DMAI_FLAGS_INUSE) == 0) {
1004 		DBG(DBG_DMA_UNBINDH, dip, "handle not inuse\n");
1005 		return (DDI_FAILURE);
1006 	}
1007 
1008 	/* remove dma handle from FMA cache */
1009 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
1010 		if (DEVI(rdip)->devi_fmhdl != NULL &&
1011 		    DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) {
1012 			(void) ndi_fmc_remove(rdip, DMA_HANDLE, mp);
1013 		}
1014 	}
1015 
1016 	/*
1017 	 * Here if the handle is using the iommu.  Unload all the iommu
1018 	 * translations.
1019 	 */
1020 	switch (PX_DMA_TYPE(mp)) {
1021 	case PX_DMAI_FLAGS_DVMA:
1022 		px_mmu_unmap_window(mmu_p, mp);
1023 		px_dvma_unmap(mmu_p, mp);
1024 		px_dma_freepfn(mp);
1025 		break;
1026 	case PX_DMAI_FLAGS_BYPASS:
1027 	case PX_DMAI_FLAGS_PTP:
1028 		px_dma_freewin(mp);
1029 		break;
1030 	default:
1031 		cmn_err(CE_PANIC, "%s%d: px_dma_unbindhdl:bad dma type %p",
1032 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
1033 		/*NOTREACHED*/
1034 	}
1035 	if (mmu_p->mmu_dvma_clid != 0) {
1036 		DBG(DBG_DMA_UNBINDH, dip, "run dvma callback\n");
1037 		ddi_run_callback(&mmu_p->mmu_dvma_clid);
1038 	}
1039 	if (px_kmem_clid) {
1040 		DBG(DBG_DMA_UNBINDH, dip, "run handle callback\n");
1041 		ddi_run_callback(&px_kmem_clid);
1042 	}
1043 	mp->dmai_flags &= PX_DMAI_FLAGS_PRESERVE;
1044 
1045 	return (DDI_SUCCESS);
1046 }
1047 
1048 /*
1049  * bus dma win entry point:
1050  */
1051 int
1052 px_dma_win(dev_info_t *dip, dev_info_t *rdip,
1053 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
1054 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
1055 {
1056 	ddi_dma_impl_t	*mp = (ddi_dma_impl_t *)handle;
1057 	int		ret;
1058 
1059 	DBG(DBG_DMA_WIN, dip, "rdip=%s%d\n",
1060 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1061 
1062 	px_dump_dma_handle(DBG_DMA_WIN, dip, mp);
1063 	if (win >= mp->dmai_nwin) {
1064 		DBG(DBG_DMA_WIN, dip, "%x out of range\n", win);
1065 		return (DDI_FAILURE);
1066 	}
1067 
1068 	switch (PX_DMA_TYPE(mp)) {
1069 	case PX_DMAI_FLAGS_DVMA:
1070 		if (win != PX_DMA_CURWIN(mp)) {
1071 			px_t *px_p = DIP_TO_STATE(dip);
1072 			px_mmu_t *mmu_p = px_p->px_mmu_p;
1073 			px_mmu_unmap_window(mmu_p, mp);
1074 
1075 			/* map_window sets dmai_mapping/size/offset */
1076 			px_mmu_map_window(mmu_p, mp, win);
1077 			if ((ret = px_mmu_map_window(mmu_p,
1078 			    mp, win)) != DDI_SUCCESS)
1079 				return (ret);
1080 		}
1081 		if (cookiep)
1082 			MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping,
1083 			    mp->dmai_size);
1084 		if (ccountp)
1085 			*ccountp = 1;
1086 		break;
1087 	case PX_DMAI_FLAGS_PTP:
1088 	case PX_DMAI_FLAGS_BYPASS: {
1089 		int i;
1090 		ddi_dma_cookie_t *ck_p;
1091 		px_dma_win_t *win_p = mp->dmai_winlst;
1092 
1093 		for (i = 0; i < win; win_p = win_p->win_next, i++) {};
1094 		ck_p = (ddi_dma_cookie_t *)(win_p + 1);
1095 		*cookiep = *ck_p;
1096 		mp->dmai_offset = win_p->win_offset;
1097 		mp->dmai_size   = win_p->win_size;
1098 		mp->dmai_mapping = ck_p->dmac_laddress;
1099 		mp->dmai_cookie = ck_p + 1;
1100 		win_p->win_curseg = 0;
1101 		if (ccountp)
1102 			*ccountp = win_p->win_ncookies;
1103 		}
1104 		break;
1105 	default:
1106 		cmn_err(CE_WARN, "%s%d: px_dma_win:bad dma type 0x%x",
1107 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
1108 		    PX_DMA_TYPE(mp));
1109 		return (DDI_FAILURE);
1110 	}
1111 	if (cookiep)
1112 		DBG(DBG_DMA_WIN, dip,
1113 		    "cookie - dmac_address=%x dmac_size=%x\n",
1114 		    cookiep->dmac_address, cookiep->dmac_size);
1115 	if (offp)
1116 		*offp = (off_t)mp->dmai_offset;
1117 	if (lenp)
1118 		*lenp = mp->dmai_size;
1119 	return (DDI_SUCCESS);
1120 }
1121 
1122 #ifdef	DEBUG
1123 static char *px_dmactl_str[] = {
1124 	"DDI_DMA_FREE",
1125 	"DDI_DMA_SYNC",
1126 	"DDI_DMA_HTOC",
1127 	"DDI_DMA_KVADDR",
1128 	"DDI_DMA_MOVWIN",
1129 	"DDI_DMA_REPWIN",
1130 	"DDI_DMA_GETERR",
1131 	"DDI_DMA_COFF",
1132 	"DDI_DMA_NEXTWIN",
1133 	"DDI_DMA_NEXTSEG",
1134 	"DDI_DMA_SEGTOC",
1135 	"DDI_DMA_RESERVE",
1136 	"DDI_DMA_RELEASE",
1137 	"DDI_DMA_RESETH",
1138 	"DDI_DMA_CKSYNC",
1139 	"DDI_DMA_IOPB_ALLOC",
1140 	"DDI_DMA_IOPB_FREE",
1141 	"DDI_DMA_SMEM_ALLOC",
1142 	"DDI_DMA_SMEM_FREE",
1143 	"DDI_DMA_SET_SBUS64"
1144 };
1145 #endif	/* DEBUG */
1146 
1147 /*
1148  * bus dma control entry point:
1149  */
1150 /*ARGSUSED*/
1151 int
1152 px_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
1153 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
1154 	uint_t cache_flags)
1155 {
1156 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
1157 
1158 #ifdef	DEBUG
1159 	DBG(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", px_dmactl_str[cmd],
1160 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1161 #endif	/* DEBUG */
1162 
1163 	switch (cmd) {
1164 	case DDI_DMA_FREE:
1165 		(void) px_dma_unbindhdl(dip, rdip, handle);
1166 		(void) px_dma_freehdl(dip, rdip, handle);
1167 		return (DDI_SUCCESS);
1168 	case DDI_DMA_RESERVE: {
1169 		px_t *px_p = DIP_TO_STATE(dip);
1170 		return (px_fdvma_reserve(dip, rdip, px_p,
1171 		    (ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp));
1172 		}
1173 	case DDI_DMA_RELEASE: {
1174 		px_t *px_p = DIP_TO_STATE(dip);
1175 		return (px_fdvma_release(dip, px_p, mp));
1176 		}
1177 	default:
1178 		break;
1179 	}
1180 
1181 	switch (PX_DMA_TYPE(mp)) {
1182 	case PX_DMAI_FLAGS_DVMA:
1183 		return (px_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
1184 		    cache_flags));
1185 	case PX_DMAI_FLAGS_PTP:
1186 	case PX_DMAI_FLAGS_BYPASS:
1187 		return (px_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
1188 		    cache_flags));
1189 	default:
1190 		cmn_err(CE_PANIC, "%s%d: px_dma_ctlops(%x):bad dma type %x",
1191 		    ddi_driver_name(rdip), ddi_get_instance(rdip), cmd,
1192 		    mp->dmai_flags);
1193 		/*NOTREACHED*/
1194 	}
1195 	return (0);
1196 }
1197 
1198 /*
1199  * control ops entry point:
1200  *
1201  * Requests handled completely:
1202  *	DDI_CTLOPS_INITCHILD	see init_child() for details
1203  *	DDI_CTLOPS_UNINITCHILD
1204  *	DDI_CTLOPS_REPORTDEV	see report_dev() for details
1205  *	DDI_CTLOPS_IOMIN	cache line size if streaming otherwise 1
1206  *	DDI_CTLOPS_REGSIZE
1207  *	DDI_CTLOPS_NREGS
1208  *	DDI_CTLOPS_DVMAPAGESIZE
1209  *	DDI_CTLOPS_POKE
1210  *	DDI_CTLOPS_PEEK
1211  *
1212  * All others passed to parent.
1213  */
1214 int
1215 px_ctlops(dev_info_t *dip, dev_info_t *rdip,
1216 	ddi_ctl_enum_t op, void *arg, void *result)
1217 {
1218 	px_t *px_p = DIP_TO_STATE(dip);
1219 	struct detachspec *ds;
1220 	struct attachspec *as;
1221 
1222 	switch (op) {
1223 	case DDI_CTLOPS_INITCHILD:
1224 		return (px_init_child(px_p, (dev_info_t *)arg));
1225 
1226 	case DDI_CTLOPS_UNINITCHILD:
1227 		return (px_uninit_child(px_p, (dev_info_t *)arg));
1228 
1229 	case DDI_CTLOPS_ATTACH:
1230 		if (!pcie_is_child(dip, rdip))
1231 			return (DDI_SUCCESS);
1232 
1233 		as = (struct attachspec *)arg;
1234 		switch (as->when) {
1235 		case DDI_PRE:
1236 			if (as->cmd == DDI_ATTACH) {
1237 				DBG(DBG_PWR, dip, "PRE_ATTACH for %s@%d\n",
1238 				    ddi_driver_name(rdip),
1239 				    ddi_get_instance(rdip));
1240 				return (pcie_pm_hold(dip));
1241 			}
1242 			if (as->cmd == DDI_RESUME) {
1243 				DBG(DBG_PWR, dip, "PRE_RESUME for %s@%d\n",
1244 				    ddi_driver_name(rdip),
1245 				    ddi_get_instance(rdip));
1246 
1247 				pcie_clear_errors(rdip);
1248 			}
1249 			return (DDI_SUCCESS);
1250 
1251 		case DDI_POST:
1252 			DBG(DBG_PWR, dip, "POST_ATTACH for %s@%d\n",
1253 			    ddi_driver_name(rdip), ddi_get_instance(rdip));
1254 			if (as->cmd == DDI_ATTACH &&
1255 			    as->result != DDI_SUCCESS) {
1256 				/*
1257 				 * Attach failed for the child device. The child
1258 				 * driver may have made PM calls before the
1259 				 * attach failed. pcie_pm_remove_child() should
1260 				 * cleanup PM state and holds (if any)
1261 				 * associated with the child device.
1262 				 */
1263 				return (pcie_pm_remove_child(dip, rdip));
1264 			}
1265 
1266 			if (as->result == DDI_SUCCESS)
1267 				pf_init(rdip, (void *)px_p->px_fm_ibc, as->cmd);
1268 
1269 			(void) pcie_postattach_child(rdip);
1270 
1271 			return (DDI_SUCCESS);
1272 		default:
1273 			break;
1274 		}
1275 		break;
1276 
1277 	case DDI_CTLOPS_DETACH:
1278 		if (!pcie_is_child(dip, rdip))
1279 			return (DDI_SUCCESS);
1280 
1281 		ds = (struct detachspec *)arg;
1282 		switch (ds->when) {
1283 		case DDI_POST:
1284 			if (ds->cmd == DDI_DETACH &&
1285 			    ds->result == DDI_SUCCESS) {
1286 				DBG(DBG_PWR, dip, "POST_DETACH for %s@%d\n",
1287 				    ddi_driver_name(rdip),
1288 				    ddi_get_instance(rdip));
1289 				return (pcie_pm_remove_child(dip, rdip));
1290 			}
1291 			return (DDI_SUCCESS);
1292 		case DDI_PRE:
1293 			pf_fini(rdip, ds->cmd);
1294 			return (DDI_SUCCESS);
1295 		default:
1296 			break;
1297 		}
1298 		break;
1299 
1300 	case DDI_CTLOPS_REPORTDEV:
1301 		return (px_report_dev(rdip));
1302 
1303 	case DDI_CTLOPS_IOMIN:
1304 		return (DDI_SUCCESS);
1305 
1306 	case DDI_CTLOPS_REGSIZE:
1307 		*((off_t *)result) = px_get_reg_set_size(rdip, *((int *)arg));
1308 		return (*((off_t *)result) == 0 ? DDI_FAILURE : DDI_SUCCESS);
1309 
1310 	case DDI_CTLOPS_NREGS:
1311 		*((uint_t *)result) = px_get_nreg_set(rdip);
1312 		return (DDI_SUCCESS);
1313 
1314 	case DDI_CTLOPS_DVMAPAGESIZE:
1315 		*((ulong_t *)result) = MMU_PAGE_SIZE;
1316 		return (DDI_SUCCESS);
1317 
1318 	case DDI_CTLOPS_POKE:	/* platform dependent implementation. */
1319 		return (px_lib_ctlops_poke(dip, rdip,
1320 		    (peekpoke_ctlops_t *)arg));
1321 
1322 	case DDI_CTLOPS_PEEK:	/* platform dependent implementation. */
1323 		return (px_lib_ctlops_peek(dip, rdip,
1324 		    (peekpoke_ctlops_t *)arg, result));
1325 
1326 	case DDI_CTLOPS_POWER:
1327 	default:
1328 		break;
1329 	}
1330 
1331 	/*
1332 	 * Now pass the request up to our parent.
1333 	 */
1334 	DBG(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n",
1335 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1336 	return (ddi_ctlops(dip, rdip, op, arg, result));
1337 }
1338 
1339 /* ARGSUSED */
1340 int
1341 px_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
1342     ddi_intr_handle_impl_t *hdlp, void *result)
1343 {
1344 	int	intr_types, ret = DDI_SUCCESS;
1345 
1346 	DBG(DBG_INTROPS, dip, "px_intr_ops: rdip=%s%d\n",
1347 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
1348 
1349 	/* Process DDI_INTROP_SUPPORTED_TYPES request here */
1350 	if (intr_op == DDI_INTROP_SUPPORTED_TYPES) {
1351 		*(int *)result = i_ddi_get_intx_nintrs(rdip) ?
1352 		    DDI_INTR_TYPE_FIXED : 0;
1353 
1354 		if ((pci_msi_get_supported_type(rdip,
1355 		    &intr_types)) == DDI_SUCCESS) {
1356 			/*
1357 			 * Double check supported interrupt types vs.
1358 			 * what the host bridge supports.
1359 			 */
1360 			*(int *)result |= intr_types;
1361 		}
1362 
1363 		return (ret);
1364 	}
1365 
1366 	/*
1367 	 * PCI-E nexus driver supports fixed, MSI and MSI-X interrupts.
1368 	 * Return failure if interrupt type is not supported.
1369 	 */
1370 	switch (hdlp->ih_type) {
1371 	case DDI_INTR_TYPE_FIXED:
1372 		ret = px_intx_ops(dip, rdip, intr_op, hdlp, result);
1373 		break;
1374 	case DDI_INTR_TYPE_MSI:
1375 	case DDI_INTR_TYPE_MSIX:
1376 		ret = px_msix_ops(dip, rdip, intr_op, hdlp, result);
1377 		break;
1378 	default:
1379 		ret = DDI_ENOTSUP;
1380 		break;
1381 	}
1382 
1383 	return (ret);
1384 }
1385 
1386 static void
1387 px_set_mps(px_t *px_p)
1388 {
1389 	dev_info_t	*dip;
1390 	pcie_bus_t	*bus_p;
1391 	int		max_supported;
1392 
1393 	dip = px_p->px_dip;
1394 	bus_p = PCIE_DIP2BUS(dip);
1395 
1396 	bus_p->bus_mps = -1;
1397 
1398 	if (pcie_root_port(dip) == DDI_FAILURE) {
1399 		if (px_lib_get_root_complex_mps(px_p, dip,
1400 		    &max_supported) < 0) {
1401 
1402 			DBG(DBG_MPS, dip, "MPS:  Can not get RC MPS\n");
1403 			return;
1404 		}
1405 
1406 		DBG(DBG_MPS, dip, "MPS: Root Complex MPS Cap of = %x\n",
1407 		    max_supported);
1408 
1409 		if (pcie_max_mps < max_supported)
1410 			max_supported = pcie_max_mps;
1411 
1412 		(void) pcie_get_fabric_mps(dip, ddi_get_child(dip),
1413 		    &max_supported);
1414 
1415 		bus_p->bus_mps = max_supported;
1416 
1417 		(void) px_lib_set_root_complex_mps(px_p, dip, bus_p->bus_mps);
1418 
1419 		DBG(DBG_MPS, dip, "MPS: Root Complex MPS Set to = %x\n",
1420 		    bus_p->bus_mps);
1421 	}
1422 }
1423