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