xref: /titanic_51/usr/src/uts/sun4u/io/pci/pci.c (revision 0c44d0008f52b6a42b9c01d3b344661217520a68)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * PCI nexus driver interface
30  */
31 
32 #include <sys/types.h>
33 #include <sys/conf.h>		/* nulldev */
34 #include <sys/stat.h>		/* devctl */
35 #include <sys/kmem.h>
36 #include <sys/async.h>		/* ecc_flt for pci_ecc.h */
37 #include <sys/sunddi.h>
38 #include <sys/sunndi.h>
39 #include <sys/ndifm.h>
40 #include <sys/ontrap.h>
41 #include <sys/ddi_impldefs.h>
42 #include <sys/ddi_subrdefs.h>
43 #include <sys/epm.h>
44 #include <sys/hotplug/pci/pcihp.h>
45 #include <sys/pci/pci_tools_ext.h>
46 #include <sys/spl.h>
47 #include <sys/pci/pci_obj.h>
48 
49 /*LINTLIBRARY*/
50 
51 /*
52  * function prototype for hotplug routine:
53  */
54 static void
55 pci_init_hotplug(struct pci *);
56 
57 /*
58  * function prototypes for dev ops routines:
59  */
60 static int pci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
61 static int pci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
62 static int pci_info(dev_info_t *dip, ddi_info_cmd_t infocmd,
63 	void *arg, void **result);
64 static int pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args);
65 static int pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args,
66     void *result);
67 static off_t get_reg_set_size(dev_info_t *child, int rnumber);
68 
69 /*
70  * bus ops and dev ops structures:
71  */
72 static struct bus_ops pci_bus_ops = {
73 	BUSO_REV,
74 	pci_map,
75 	0,
76 	0,
77 	0,
78 	i_ddi_map_fault,
79 	pci_dma_setup,
80 	pci_dma_allochdl,
81 	pci_dma_freehdl,
82 	pci_dma_bindhdl,
83 	pci_dma_unbindhdl,
84 	pci_dma_sync,
85 	pci_dma_win,
86 	pci_dma_ctlops,
87 	pci_ctlops,
88 	ddi_bus_prop_op,
89 	ndi_busop_get_eventcookie,	/* (*bus_get_eventcookie)(); */
90 	ndi_busop_add_eventcall,	/* (*bus_add_eventcall)(); */
91 	ndi_busop_remove_eventcall,	/* (*bus_remove_eventcall)(); */
92 	ndi_post_event,			/* (*bus_post_event)(); */
93 	NULL,				/* (*bus_intr_ctl)(); */
94 	NULL,				/* (*bus_config)(); */
95 	NULL,				/* (*bus_unconfig)(); */
96 	pci_fm_init_child,		/* (*bus_fm_init)(); */
97 	NULL,				/* (*bus_fm_fini)(); */
98 	pci_bus_enter,			/* (*bus_fm_access_enter)(); */
99 	pci_bus_exit,			/* (*bus_fm_access_fini)(); */
100 	NULL,				/* (*bus_power)(); */
101 	pci_intr_ops			/* (*bus_intr_op)(); */
102 };
103 
104 extern struct cb_ops pci_cb_ops;
105 
106 static struct dev_ops pci_ops = {
107 	DEVO_REV,
108 	0,
109 	pci_info,
110 	nulldev,
111 	0,
112 	pci_attach,
113 	pci_detach,
114 	nodev,
115 	&pci_cb_ops,
116 	&pci_bus_ops,
117 	0
118 };
119 
120 /*
121  * module definitions:
122  */
123 #include <sys/modctl.h>
124 extern struct mod_ops mod_driverops;
125 
126 static struct modldrv modldrv = {
127 	&mod_driverops, 	/* Type of module - driver */
128 	"PCI Bus nexus driver %I%",	/* Name of module. */
129 	&pci_ops,		/* driver ops */
130 };
131 
132 static struct modlinkage modlinkage = {
133 	MODREV_1, (void *)&modldrv, NULL
134 };
135 
136 /*
137  * driver global data:
138  */
139 void *per_pci_state;		/* per-pbm soft state pointer */
140 void *per_pci_common_state;	/* per-psycho soft state pointer */
141 kmutex_t pci_global_mutex;	/* attach/detach common struct lock */
142 errorq_t *pci_ecc_queue = NULL;	/* per-system ecc handling queue */
143 extern errorq_t *pci_target_queue;
144 struct cb_ops *pcihp_ops = NULL;	/* hotplug module cb ops */
145 
146 extern void pci_child_cfg_save(dev_info_t *dip);
147 extern void pci_child_cfg_restore(dev_info_t *dip);
148 
149 int
150 _init(void)
151 {
152 	int e;
153 
154 	/*
155 	 * Initialize per-pci bus soft state pointer.
156 	 */
157 	e = ddi_soft_state_init(&per_pci_state, sizeof (pci_t), 1);
158 	if (e != 0)
159 		return (e);
160 
161 	/*
162 	 * Initialize per-psycho soft state pointer.
163 	 */
164 	e = ddi_soft_state_init(&per_pci_common_state,
165 		sizeof (pci_common_t), 1);
166 	if (e != 0) {
167 		ddi_soft_state_fini(&per_pci_state);
168 		return (e);
169 	}
170 
171 	/*
172 	 * Initialize global mutexes.
173 	 */
174 	mutex_init(&pci_global_mutex, NULL, MUTEX_DRIVER, NULL);
175 	pci_reloc_init();
176 
177 	/*
178 	 * Create the performance kstats.
179 	 */
180 	pci_kstat_init();
181 
182 	/*
183 	 * Install the module.
184 	 */
185 	e = mod_install(&modlinkage);
186 	if (e != 0) {
187 		ddi_soft_state_fini(&per_pci_state);
188 		ddi_soft_state_fini(&per_pci_common_state);
189 		mutex_destroy(&pci_global_mutex);
190 	}
191 	return (e);
192 }
193 
194 int
195 _fini(void)
196 {
197 	int e;
198 
199 	/*
200 	 * Remove the module.
201 	 */
202 	e = mod_remove(&modlinkage);
203 	if (e != 0)
204 		return (e);
205 
206 	/*
207 	 * Destroy pci_ecc_queue, and set it to NULL.
208 	 */
209 	if (pci_ecc_queue)
210 		errorq_destroy(pci_ecc_queue);
211 
212 	pci_ecc_queue = NULL;
213 
214 	/*
215 	 * Destroy pci_target_queue, and set it to NULL.
216 	 */
217 	if (pci_target_queue)
218 		errorq_destroy(pci_target_queue);
219 
220 	pci_target_queue = NULL;
221 
222 	/*
223 	 * Destroy the performance kstats.
224 	 */
225 	pci_kstat_fini();
226 
227 	/*
228 	 * Free the per-pci and per-psycho soft state info and destroy
229 	 * mutex for per-psycho soft state.
230 	 */
231 	ddi_soft_state_fini(&per_pci_state);
232 	ddi_soft_state_fini(&per_pci_common_state);
233 	mutex_destroy(&pci_global_mutex);
234 	pci_reloc_fini();
235 	return (e);
236 }
237 
238 int
239 _info(struct modinfo *modinfop)
240 {
241 	return (mod_info(&modlinkage, modinfop));
242 }
243 
244 /*ARGSUSED*/
245 static int
246 pci_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
247 {
248 	int	instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(getminor((dev_t)arg));
249 	pci_t	*pci_p = get_pci_soft_state(instance);
250 
251 	/* allow hotplug to deal with ones it manages */
252 	if (pci_p && (pci_p->hotplug_capable == B_TRUE))
253 		return (pcihp_info(dip, infocmd, arg, result));
254 
255 	/* non-hotplug or not attached */
256 	switch (infocmd) {
257 	case DDI_INFO_DEVT2INSTANCE:
258 		*result = (void *)(uintptr_t)instance;
259 		return (DDI_SUCCESS);
260 
261 	case DDI_INFO_DEVT2DEVINFO:
262 		if (pci_p == NULL)
263 			return (DDI_FAILURE);
264 		*result = (void *)pci_p->pci_dip;
265 		return (DDI_SUCCESS);
266 
267 	default:
268 		return (DDI_FAILURE);
269 	}
270 }
271 
272 
273 /* device driver entry points */
274 /*
275  * attach entry point:
276  */
277 static int
278 pci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
279 {
280 	pci_t *pci_p;			/* per bus state pointer */
281 	int instance = ddi_get_instance(dip);
282 
283 	switch (cmd) {
284 	case DDI_ATTACH:
285 		DEBUG0(DBG_ATTACH, dip, "DDI_ATTACH\n");
286 
287 		/*
288 		 * Allocate and get the per-pci soft state structure.
289 		 */
290 		if (alloc_pci_soft_state(instance) != DDI_SUCCESS) {
291 			cmn_err(CE_WARN, "%s%d: can't allocate pci state",
292 				ddi_driver_name(dip), instance);
293 			goto err_bad_pci_softstate;
294 		}
295 		pci_p = get_pci_soft_state(instance);
296 		pci_p->pci_dip = dip;
297 		mutex_init(&pci_p->pci_mutex, NULL, MUTEX_DRIVER, NULL);
298 		pci_p->pci_soft_state = PCI_SOFT_STATE_CLOSED;
299 		pci_p->pci_open_count = 0;
300 
301 		/*
302 		 * Get key properties of the pci bridge node and
303 		 * determine it's type (psycho, schizo, etc ...).
304 		 */
305 		if (get_pci_properties(pci_p, dip) == DDI_FAILURE)
306 			goto err_bad_pci_prop;
307 
308 		/*
309 		 * Map in the registers.
310 		 */
311 		if (map_pci_registers(pci_p, dip) == DDI_FAILURE)
312 			goto err_bad_reg_prop;
313 
314 		if (pci_obj_setup(pci_p) != DDI_SUCCESS)
315 			goto err_bad_objs;
316 
317 		/*
318 		 * If this PCI leaf has hotplug and this platform
319 		 * loads hotplug modules then initialize the
320 		 * hotplug framework.
321 		 */
322 		pci_init_hotplug(pci_p);
323 
324 		/*
325 		 * Create the "devctl" node for hotplug support.
326 		 * For non-hotplug bus, we still need ":devctl" to
327 		 * support DEVCTL_DEVICE_* and DEVCTL_BUS_* ioctls.
328 		 */
329 		if (pci_p->hotplug_capable == B_FALSE) {
330 			if (ddi_create_minor_node(dip, "devctl", S_IFCHR,
331 			    PCIHP_AP_MINOR_NUM(instance, PCIHP_DEVCTL_MINOR),
332 			    DDI_NT_NEXUS, 0) != DDI_SUCCESS)
333 				goto err_bad_devctl_node;
334 		}
335 
336 		/*
337 		 * Create pcitool nodes for register access and interrupt
338 		 * routing.
339 		 */
340 		if (pcitool_init(dip) != DDI_SUCCESS) {
341 			goto err_bad_pcitool_nodes;
342 		}
343 
344 		/*
345 		 * Due to unresolved hardware issues, disable PCIPM until
346 		 * the problem is fully understood.
347 		 *
348 		 * pci_pwr_setup(pci_p, dip);
349 		 */
350 
351 		ddi_report_dev(dip);
352 
353 		pci_p->pci_state = PCI_ATTACHED;
354 		DEBUG0(DBG_ATTACH, dip, "attach success\n");
355 		break;
356 
357 err_bad_pcitool_nodes:
358 		if (pci_p->hotplug_capable == B_FALSE)
359 			ddi_remove_minor_node(dip, "devctl");
360 		else
361 			(void) pcihp_uninit(dip);
362 err_bad_devctl_node:
363 		pci_obj_destroy(pci_p);
364 err_bad_objs:
365 		unmap_pci_registers(pci_p);
366 err_bad_reg_prop:
367 		free_pci_properties(pci_p);
368 err_bad_pci_prop:
369 		mutex_destroy(&pci_p->pci_mutex);
370 		free_pci_soft_state(instance);
371 err_bad_pci_softstate:
372 		return (DDI_FAILURE);
373 
374 	case DDI_RESUME:
375 		DEBUG0(DBG_ATTACH, dip, "DDI_RESUME\n");
376 
377 		/*
378 		 * Make sure the Psycho control registers and IOMMU
379 		 * are configured properly.
380 		 */
381 		pci_p = get_pci_soft_state(instance);
382 		mutex_enter(&pci_p->pci_mutex);
383 
384 		/*
385 		 * Make sure this instance has been suspended.
386 		 */
387 		if (pci_p->pci_state != PCI_SUSPENDED) {
388 			DEBUG0(DBG_ATTACH, dip, "instance NOT suspended\n");
389 			mutex_exit(&pci_p->pci_mutex);
390 			return (DDI_FAILURE);
391 		}
392 		pci_obj_resume(pci_p);
393 		pci_p->pci_state = PCI_ATTACHED;
394 
395 		pci_child_cfg_restore(dip);
396 
397 		mutex_exit(&pci_p->pci_mutex);
398 		break;
399 
400 	default:
401 		DEBUG0(DBG_ATTACH, dip, "unsupported attach op\n");
402 		return (DDI_FAILURE);
403 	}
404 
405 	return (DDI_SUCCESS);
406 }
407 
408 /*
409  * detach entry point:
410  */
411 static int
412 pci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
413 {
414 	int instance = ddi_get_instance(dip);
415 	pci_t *pci_p = get_pci_soft_state(instance);
416 
417 	/*
418 	 * Make sure we are currently attached
419 	 */
420 	if (pci_p->pci_state != PCI_ATTACHED) {
421 		DEBUG0(DBG_ATTACH, dip, "failed - instance not attached\n");
422 		return (DDI_FAILURE);
423 	}
424 
425 	mutex_enter(&pci_p->pci_mutex);
426 
427 	switch (cmd) {
428 	case DDI_DETACH:
429 		DEBUG0(DBG_DETACH, dip, "DDI_DETACH\n");
430 
431 		if (pci_p->hotplug_capable == B_TRUE)
432 			if (pcihp_uninit(dip) == DDI_FAILURE) {
433 				mutex_exit(&pci_p->pci_mutex);
434 				return (DDI_FAILURE);
435 			}
436 
437 		pcitool_uninit(dip);
438 
439 		pci_obj_destroy(pci_p);
440 
441 		/*
442 		 * Free the pci soft state structure and the rest of the
443 		 * resources it's using.
444 		 */
445 		free_pci_properties(pci_p);
446 		unmap_pci_registers(pci_p);
447 		mutex_exit(&pci_p->pci_mutex);
448 		mutex_destroy(&pci_p->pci_mutex);
449 		free_pci_soft_state(instance);
450 
451 		/* Free the interrupt-priorities prop if we created it. */ {
452 			int len;
453 
454 			if (ddi_getproplen(DDI_DEV_T_ANY, dip,
455 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
456 			    "interrupt-priorities", &len) == DDI_PROP_SUCCESS)
457 				(void) ddi_prop_remove(DDI_DEV_T_NONE, dip,
458 				    "interrupt-priorities");
459 		}
460 		return (DDI_SUCCESS);
461 
462 	case DDI_SUSPEND:
463 		pci_child_cfg_save(dip);
464 		pci_obj_suspend(pci_p);
465 		pci_p->pci_state = PCI_SUSPENDED;
466 
467 		mutex_exit(&pci_p->pci_mutex);
468 		return (DDI_SUCCESS);
469 
470 	default:
471 		DEBUG0(DBG_DETACH, dip, "unsupported detach op\n");
472 		mutex_exit(&pci_p->pci_mutex);
473 		return (DDI_FAILURE);
474 	}
475 }
476 
477 
478 /* bus driver entry points */
479 
480 /*
481  * bus map entry point:
482  *
483  * 	if map request is for an rnumber
484  *		get the corresponding regspec from device node
485  * 	build a new regspec in our parent's format
486  *	build a new map_req with the new regspec
487  *	call up the tree to complete the mapping
488  */
489 int
490 pci_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
491 	off_t off, off_t len, caddr_t *addrp)
492 {
493 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
494 	struct regspec p_regspec;
495 	ddi_map_req_t p_mapreq;
496 	int reglen, rval, r_no;
497 	pci_regspec_t reloc_reg, *rp = &reloc_reg;
498 
499 	DEBUG2(DBG_MAP, dip, "rdip=%s%d:",
500 		ddi_driver_name(rdip), ddi_get_instance(rdip));
501 
502 	if (mp->map_flags & DDI_MF_USER_MAPPING)
503 		return (DDI_ME_UNIMPLEMENTED);
504 
505 	switch (mp->map_type) {
506 	case DDI_MT_REGSPEC:
507 		reloc_reg = *(pci_regspec_t *)mp->map_obj.rp;	/* dup whole */
508 		break;
509 
510 	case DDI_MT_RNUMBER:
511 		r_no = mp->map_obj.rnumber;
512 		DEBUG1(DBG_MAP | DBG_CONT, dip, " r#=%x", r_no);
513 
514 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
515 			"reg", (caddr_t)&rp, &reglen) != DDI_SUCCESS)
516 				return (DDI_ME_RNUMBER_RANGE);
517 
518 		if (r_no < 0 || r_no >= reglen / sizeof (pci_regspec_t)) {
519 			kmem_free(rp, reglen);
520 			return (DDI_ME_RNUMBER_RANGE);
521 		}
522 		rp += r_no;
523 		break;
524 
525 	default:
526 		return (DDI_ME_INVAL);
527 	}
528 	DEBUG0(DBG_MAP | DBG_CONT, dip, "\n");
529 
530 	/* use "assigned-addresses" to relocate regspec within pci space */
531 	if (rval = pci_reloc_reg(dip, rdip, pci_p, rp))
532 		goto done;
533 
534 	if (len)	/* adjust regspec according to mapping request */
535 		rp->pci_size_low = len;
536 	rp->pci_phys_low += off;
537 
538 	/* use "ranges" to translate relocated pci regspec into parent space */
539 	if (rval = pci_xlate_reg(pci_p, rp, &p_regspec))
540 		goto done;
541 
542 	p_mapreq = *mp;		/* dup the whole structure */
543 	p_mapreq.map_type = DDI_MT_REGSPEC;
544 	p_mapreq.map_obj.rp = &p_regspec;
545 	rval = ddi_map(dip, &p_mapreq, 0, 0, addrp);
546 
547 	if (rval == DDI_SUCCESS) {
548 		/*
549 		 * Set-up access functions for FM access error capable drivers.
550 		 * The axq workaround prevents fault management support
551 		 */
552 		if (DDI_FM_ACC_ERR_CAP(pci_p->pci_fm_cap) &&
553 		    DDI_FM_ACC_ERR_CAP(ddi_fm_capable(rdip)) &&
554 		    mp->map_handlep->ah_acc.devacc_attr_access !=
555 		    DDI_DEFAULT_ACC)
556 			pci_fm_acc_setup(mp, rdip);
557 		pci_axq_setup(mp, pci_p->pci_pbm_p);
558 	}
559 
560 done:
561 	if (mp->map_type == DDI_MT_RNUMBER)
562 		kmem_free(rp - r_no, reglen);
563 
564 	return (rval);
565 }
566 
567 /*
568  * bus dma map entry point
569  * return value:
570  *	DDI_DMA_PARTIAL_MAP	 1
571  *	DDI_DMA_MAPOK		 0
572  *	DDI_DMA_MAPPED		 0
573  *	DDI_DMA_NORESOURCES	-1
574  *	DDI_DMA_NOMAPPING	-2
575  *	DDI_DMA_TOOBIG		-3
576  */
577 int
578 pci_dma_setup(dev_info_t *dip, dev_info_t *rdip, ddi_dma_req_t *dmareq,
579 	ddi_dma_handle_t *handlep)
580 {
581 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
582 	iommu_t *iommu_p = pci_p->pci_iommu_p;
583 	ddi_dma_impl_t *mp;
584 	int ret;
585 
586 	DEBUG3(DBG_DMA_MAP, dip, "mapping - rdip=%s%d type=%s\n",
587 		ddi_driver_name(rdip), ddi_get_instance(rdip),
588 		handlep ? "alloc" : "advisory");
589 
590 	if (!(mp = pci_dma_lmts2hdl(dip, rdip, iommu_p, dmareq)))
591 		return (DDI_DMA_NORESOURCES);
592 	if (mp == (ddi_dma_impl_t *)DDI_DMA_NOMAPPING)
593 		return (DDI_DMA_NOMAPPING);
594 	if (ret = pci_dma_type(pci_p, dmareq, mp))
595 		goto freehandle;
596 	if (ret = pci_dma_pfn(pci_p, dmareq, mp))
597 		goto freehandle;
598 
599 	switch (PCI_DMA_TYPE(mp)) {
600 	case DMAI_FLAGS_DVMA:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
601 		if ((ret = pci_dvma_win(pci_p, dmareq, mp)) || !handlep)
602 			goto freehandle;
603 		if (!PCI_DMA_CANCACHE(mp)) {	/* try fast track */
604 			if (PCI_DMA_CANFAST(mp)) {
605 				if (!pci_dvma_map_fast(iommu_p, mp))
606 					break;
607 			/* LINTED E_NOP_ELSE_STMT */
608 			} else {
609 				PCI_DVMA_FASTTRAK_PROF(mp);
610 			}
611 		}
612 		if (ret = pci_dvma_map(mp, dmareq, iommu_p))
613 			goto freehandle;
614 		break;
615 	case DMAI_FLAGS_PEER_TO_PEER:	/* LINTED E_EQUALITY_NOT_ASSIGNMENT */
616 		if ((ret = pci_dma_physwin(pci_p, dmareq, mp)) || !handlep)
617 			goto freehandle;
618 		break;
619 	case DMAI_FLAGS_BYPASS:
620 	default:
621 		panic("%s%d: pci_dma_setup: bad dma type 0x%x",
622 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
623 		    PCI_DMA_TYPE(mp));
624 		/*NOTREACHED*/
625 	}
626 	*handlep = (ddi_dma_handle_t)mp;
627 	mp->dmai_flags |= (DMAI_FLAGS_INUSE | DMAI_FLAGS_MAPPED);
628 	dump_dma_handle(DBG_DMA_MAP, dip, mp);
629 
630 	return ((mp->dmai_nwin == 1) ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
631 freehandle:
632 	if (ret == DDI_DMA_NORESOURCES)
633 		pci_dma_freemp(mp); /* don't run_callback() */
634 	else
635 		(void) pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
636 	return (ret);
637 }
638 
639 
640 /*
641  * bus dma alloc handle entry point:
642  */
643 int
644 pci_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
645 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
646 {
647 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
648 	ddi_dma_impl_t *mp;
649 	int rval;
650 
651 	DEBUG2(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n",
652 		ddi_driver_name(rdip), ddi_get_instance(rdip));
653 
654 	if (attrp->dma_attr_version != DMA_ATTR_V0)
655 		return (DDI_DMA_BADATTR);
656 
657 	if (!(mp = pci_dma_allocmp(dip, rdip, waitfp, arg)))
658 		return (DDI_DMA_NORESOURCES);
659 
660 	/*
661 	 * Save requestor's information
662 	 */
663 	mp->dmai_attr	= *attrp; /* whole object - augmented later  */
664 	*DEV_ATTR(mp)	= *attrp; /* whole object - device orig attr */
665 	DEBUG1(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
666 
667 	/* check and convert dma attributes to handle parameters */
668 	if (rval = pci_dma_attr2hdl(pci_p, mp)) {
669 		pci_dma_freehdl(dip, rdip, (ddi_dma_handle_t)mp);
670 		*handlep = NULL;
671 		return (rval);
672 	}
673 	*handlep = (ddi_dma_handle_t)mp;
674 	return (DDI_SUCCESS);
675 }
676 
677 
678 /*
679  * bus dma free handle entry point:
680  */
681 /*ARGSUSED*/
682 int
683 pci_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
684 {
685 	DEBUG3(DBG_DMA_FREEH, dip, "rdip=%s%d mp=%p\n",
686 		ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
687 	pci_dma_freemp((ddi_dma_impl_t *)handle);
688 
689 	if (pci_kmem_clid) {
690 		DEBUG0(DBG_DMA_FREEH, dip, "run handle callback\n");
691 		ddi_run_callback(&pci_kmem_clid);
692 	}
693 	return (DDI_SUCCESS);
694 }
695 
696 
697 /*
698  * bus dma bind handle entry point:
699  */
700 int
701 pci_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
702 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
703 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
704 {
705 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
706 	iommu_t *iommu_p = pci_p->pci_iommu_p;
707 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
708 	int ret;
709 
710 	DEBUG4(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n",
711 		ddi_driver_name(rdip), ddi_get_instance(rdip), mp, dmareq);
712 
713 	if (mp->dmai_flags & DMAI_FLAGS_INUSE)
714 		return (DDI_DMA_INUSE);
715 
716 	ASSERT((mp->dmai_flags & ~DMAI_FLAGS_PRESERVE) == 0);
717 	mp->dmai_flags |= DMAI_FLAGS_INUSE;
718 
719 	if (ret = pci_dma_type(pci_p, dmareq, mp))
720 		goto err;
721 	if (ret = pci_dma_pfn(pci_p, dmareq, mp))
722 		goto err;
723 
724 	switch (PCI_DMA_TYPE(mp)) {
725 	case DMAI_FLAGS_DVMA:
726 		if (ret = pci_dvma_win(pci_p, dmareq, mp))
727 			goto map_err;
728 		if (!PCI_DMA_CANCACHE(mp)) {	/* try fast track */
729 			if (PCI_DMA_CANFAST(mp)) {
730 				if (!pci_dvma_map_fast(iommu_p, mp))
731 					goto mapped; /*LINTED E_NOP_ELSE_STMT*/
732 			} else {
733 				PCI_DVMA_FASTTRAK_PROF(mp);
734 			}
735 		}
736 		if (ret = pci_dvma_map(mp, dmareq, iommu_p))
737 			goto map_err;
738 mapped:
739 		*ccountp = 1;
740 		MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping, mp->dmai_size);
741 		break;
742 	case DMAI_FLAGS_BYPASS:
743 	case DMAI_FLAGS_PEER_TO_PEER:
744 		if (ret = pci_dma_physwin(pci_p, dmareq, mp))
745 			goto map_err;
746 		*ccountp = WINLST(mp)->win_ncookies;
747 		*cookiep = *(ddi_dma_cookie_t *)(WINLST(mp) + 1); /* wholeobj */
748 		break;
749 	default:
750 		panic("%s%d: pci_dma_bindhdl(%p): bad dma type",
751 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
752 		/*NOTREACHED*/
753 	}
754 	DEBUG2(DBG_DMA_BINDH, dip, "cookie %x+%x\n", cookiep->dmac_address,
755 	    cookiep->dmac_size);
756 	dump_dma_handle(DBG_DMA_MAP, dip, mp);
757 
758 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
759 		(void) ndi_fmc_insert(rdip, DMA_HANDLE, mp, NULL);
760 		mp->dmai_error.err_cf = impl_dma_check;
761 	}
762 
763 	mp->dmai_flags |= DMAI_FLAGS_MAPPED;
764 	return (mp->dmai_nwin == 1 ? DDI_DMA_MAPPED : DDI_DMA_PARTIAL_MAP);
765 map_err:
766 	pci_dvma_unregister_callbacks(pci_p, mp);
767 	pci_dma_freepfn(mp);
768 err:
769 	mp->dmai_flags &= DMAI_FLAGS_PRESERVE;
770 	return (ret);
771 }
772 
773 /*
774  * bus dma unbind handle entry point:
775  */
776 /*ARGSUSED*/
777 int
778 pci_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
779 {
780 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
781 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
782 	iommu_t *iommu_p = pci_p->pci_iommu_p;
783 
784 	DEBUG3(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
785 		ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
786 	if ((mp->dmai_flags & DMAI_FLAGS_INUSE) == 0) {
787 		DEBUG0(DBG_DMA_UNBINDH, dip, "handle not in use\n");
788 		return (DDI_FAILURE);
789 	}
790 
791 	mp->dmai_flags &= ~DMAI_FLAGS_MAPPED;
792 
793 	switch (PCI_DMA_TYPE(mp)) {
794 	case DMAI_FLAGS_DVMA:
795 		pci_dvma_unregister_callbacks(pci_p, mp);
796 		pci_dma_sync_unmap(dip, rdip, mp);
797 		pci_dvma_unmap(iommu_p, mp);
798 		pci_dma_freepfn(mp);
799 		break;
800 	case DMAI_FLAGS_BYPASS:
801 	case DMAI_FLAGS_PEER_TO_PEER:
802 		pci_dma_freewin(mp);
803 		break;
804 	default:
805 		panic("%s%d: pci_dma_unbindhdl:bad dma type %p",
806 		    ddi_driver_name(rdip), ddi_get_instance(rdip), mp);
807 		/*NOTREACHED*/
808 	}
809 	if (iommu_p->iommu_dvma_clid != 0) {
810 		DEBUG0(DBG_DMA_UNBINDH, dip, "run dvma callback\n");
811 		ddi_run_callback(&iommu_p->iommu_dvma_clid);
812 	}
813 	if (pci_kmem_clid) {
814 		DEBUG0(DBG_DMA_UNBINDH, dip, "run handle callback\n");
815 		ddi_run_callback(&pci_kmem_clid);
816 	}
817 	mp->dmai_flags &= DMAI_FLAGS_PRESERVE;
818 	SYNC_BUF_PA(mp) = 0;
819 
820 	if (mp->dmai_attr.dma_attr_flags & DDI_DMA_FLAGERR) {
821 		if (DEVI(rdip)->devi_fmhdl != NULL &&
822 		    DDI_FM_DMA_ERR_CAP(DEVI(rdip)->devi_fmhdl->fh_cap)) {
823 			(void) ndi_fmc_remove(rdip, DMA_HANDLE, mp);
824 		}
825 	}
826 
827 	return (DDI_SUCCESS);
828 }
829 
830 
831 /*
832  * bus dma win entry point:
833  */
834 int
835 pci_dma_win(dev_info_t *dip, dev_info_t *rdip,
836 	ddi_dma_handle_t handle, uint_t win, off_t *offp,
837 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
838 {
839 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
840 	DEBUG2(DBG_DMA_WIN, dip, "rdip=%s%d\n",
841 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
842 	dump_dma_handle(DBG_DMA_WIN, dip, mp);
843 	if (win >= mp->dmai_nwin) {
844 		DEBUG1(DBG_DMA_WIN, dip, "%x out of range\n", win);
845 		return (DDI_FAILURE);
846 	}
847 
848 	switch (PCI_DMA_TYPE(mp)) {
849 	case DMAI_FLAGS_DVMA:
850 		if (win != PCI_DMA_CURWIN(mp)) {
851 			pci_t *pci_p =
852 				get_pci_soft_state(ddi_get_instance(dip));
853 			pci_dma_sync_unmap(dip, rdip, mp);
854 			/* map_window sets dmai_mapping/size/offset */
855 			iommu_map_window(pci_p->pci_iommu_p, mp, win);
856 		}
857 		if (cookiep)
858 			MAKE_DMA_COOKIE(cookiep, mp->dmai_mapping,
859 				mp->dmai_size);
860 		if (ccountp)
861 			*ccountp = 1;
862 		break;
863 	case DMAI_FLAGS_PEER_TO_PEER:
864 	case DMAI_FLAGS_BYPASS: {
865 		int i;
866 		ddi_dma_cookie_t *ck_p;
867 		pci_dma_win_t *win_p = mp->dmai_winlst;
868 
869 		for (i = 0; i < win; win_p = win_p->win_next, i++);
870 		ck_p = (ddi_dma_cookie_t *)(win_p + 1);
871 		*cookiep = *ck_p;
872 		mp->dmai_offset = win_p->win_offset;
873 		mp->dmai_size   = win_p->win_size;
874 		mp->dmai_mapping = ck_p->dmac_laddress;
875 		mp->dmai_cookie = ck_p + 1;
876 		win_p->win_curseg = 0;
877 		if (ccountp)
878 			*ccountp = win_p->win_ncookies;
879 		}
880 		break;
881 	default:
882 		cmn_err(CE_WARN, "%s%d: pci_dma_win:bad dma type 0x%x",
883 		    ddi_driver_name(rdip), ddi_get_instance(rdip),
884 		    PCI_DMA_TYPE(mp));
885 		return (DDI_FAILURE);
886 	}
887 	if (cookiep)
888 		DEBUG2(DBG_DMA_WIN, dip,
889 			"cookie - dmac_address=%x dmac_size=%x\n",
890 			cookiep->dmac_address, cookiep->dmac_size);
891 	if (offp)
892 		*offp = (off_t)mp->dmai_offset;
893 	if (lenp)
894 		*lenp = mp->dmai_size;
895 	return (DDI_SUCCESS);
896 }
897 
898 #ifdef DEBUG
899 static char *pci_dmactl_str[] = {
900 	"DDI_DMA_FREE",
901 	"DDI_DMA_SYNC",
902 	"DDI_DMA_HTOC",
903 	"DDI_DMA_KVADDR",
904 	"DDI_DMA_MOVWIN",
905 	"DDI_DMA_REPWIN",
906 	"DDI_DMA_GETERR",
907 	"DDI_DMA_COFF",
908 	"DDI_DMA_NEXTWIN",
909 	"DDI_DMA_NEXTSEG",
910 	"DDI_DMA_SEGTOC",
911 	"DDI_DMA_RESERVE",
912 	"DDI_DMA_RELEASE",
913 	"DDI_DMA_RESETH",
914 	"DDI_DMA_CKSYNC",
915 	"DDI_DMA_IOPB_ALLOC",
916 	"DDI_DMA_IOPB_FREE",
917 	"DDI_DMA_SMEM_ALLOC",
918 	"DDI_DMA_SMEM_FREE",
919 	"DDI_DMA_SET_SBUS64",
920 	"DDI_DMA_REMAP"
921 };
922 #endif
923 
924 /*
925  * bus dma control entry point:
926  */
927 int
928 pci_dma_ctlops(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle,
929 	enum ddi_dma_ctlops cmd, off_t *offp, size_t *lenp, caddr_t *objp,
930 	uint_t cache_flags)
931 {
932 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
933 	DEBUG3(DBG_DMA_CTL, dip, "%s: rdip=%s%d\n", pci_dmactl_str[cmd],
934 	    ddi_driver_name(rdip), ddi_get_instance(rdip));
935 
936 	switch (cmd) {
937 	case DDI_DMA_FREE:
938 		(void) pci_dma_unbindhdl(dip, rdip, handle);
939 		(void) pci_dma_freehdl(dip, rdip, handle);
940 		return (DDI_SUCCESS);
941 	case DDI_DMA_RESERVE: {
942 		pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
943 		return (pci_fdvma_reserve(dip, rdip, pci_p,
944 			(ddi_dma_req_t *)offp, (ddi_dma_handle_t *)objp));
945 		}
946 	case DDI_DMA_RELEASE: {
947 		pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
948 		return (pci_fdvma_release(dip, pci_p, mp));
949 		}
950 	default:
951 		break;
952 	}
953 
954 	switch (PCI_DMA_TYPE(mp)) {
955 	case DMAI_FLAGS_DVMA:
956 		return (pci_dvma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
957 			cache_flags));
958 	case DMAI_FLAGS_PEER_TO_PEER:
959 	case DMAI_FLAGS_BYPASS:
960 		return (pci_dma_ctl(dip, rdip, mp, cmd, offp, lenp, objp,
961 			cache_flags));
962 	default:
963 		panic("%s%d: pci_dma_ctlops(%x):bad dma type %x",
964 		    ddi_driver_name(rdip), ddi_get_instance(rdip), cmd,
965 		    mp->dmai_flags);
966 		/*NOTREACHED*/
967 	}
968 }
969 
970 #ifdef  DEBUG
971 int	pci_peekfault_cnt = 0;
972 int	pci_pokefault_cnt = 0;
973 #endif  /* DEBUG */
974 
975 static int
976 pci_do_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args)
977 {
978 	pbm_t *pbm_p = pci_p->pci_pbm_p;
979 	int err = DDI_SUCCESS;
980 	on_trap_data_t otd;
981 
982 	mutex_enter(&pbm_p->pbm_pokefault_mutex);
983 	pbm_p->pbm_ontrap_data = &otd;
984 
985 	/* Set up protected environment. */
986 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
987 		uintptr_t tramp = otd.ot_trampoline;
988 
989 		otd.ot_trampoline = (uintptr_t)&poke_fault;
990 		err = do_poke(in_args->size, (void *)in_args->dev_addr,
991 		    (void *)in_args->host_addr);
992 		otd.ot_trampoline = tramp;
993 	} else
994 		err = DDI_FAILURE;
995 
996 	/*
997 	 * Read the async fault register for the PBM to see it sees
998 	 * a master-abort.
999 	 */
1000 	pbm_clear_error(pbm_p);
1001 
1002 	if (otd.ot_trap & OT_DATA_ACCESS)
1003 		err = DDI_FAILURE;
1004 
1005 	/* Take down protected environment. */
1006 	no_trap();
1007 
1008 	pbm_p->pbm_ontrap_data = NULL;
1009 	mutex_exit(&pbm_p->pbm_pokefault_mutex);
1010 
1011 #ifdef  DEBUG
1012 	if (err == DDI_FAILURE)
1013 		pci_pokefault_cnt++;
1014 #endif
1015 	return (err);
1016 }
1017 
1018 
1019 static int
1020 pci_do_caut_put(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg)
1021 {
1022 	size_t size = cautacc_ctlops_arg->size;
1023 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
1024 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
1025 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
1026 	size_t repcount = cautacc_ctlops_arg->repcount;
1027 	uint_t flags = cautacc_ctlops_arg->flags;
1028 
1029 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
1030 
1031 	/*
1032 	 * Note that i_ndi_busop_access_enter ends up grabbing the pokefault
1033 	 * mutex.
1034 	 */
1035 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1036 
1037 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
1038 		for (; repcount; repcount--) {
1039 			switch (size) {
1040 
1041 			case sizeof (uint8_t):
1042 				i_ddi_put8(hp, (uint8_t *)dev_addr,
1043 				    *(uint8_t *)host_addr);
1044 				break;
1045 
1046 			case sizeof (uint16_t):
1047 				i_ddi_put16(hp, (uint16_t *)dev_addr,
1048 				    *(uint16_t *)host_addr);
1049 				break;
1050 
1051 			case sizeof (uint32_t):
1052 				i_ddi_put32(hp, (uint32_t *)dev_addr,
1053 				    *(uint32_t *)host_addr);
1054 				break;
1055 
1056 			case sizeof (uint64_t):
1057 				i_ddi_put64(hp, (uint64_t *)dev_addr,
1058 				    *(uint64_t *)host_addr);
1059 				break;
1060 			}
1061 
1062 			host_addr += size;
1063 
1064 			if (flags == DDI_DEV_AUTOINCR)
1065 				dev_addr += size;
1066 
1067 		}
1068 	}
1069 
1070 	i_ddi_notrap((ddi_acc_handle_t)hp);
1071 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1072 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
1073 
1074 	if (hp->ahi_err->err_status != DDI_FM_OK) {
1075 		/* Clear the expected fault from the handle before returning */
1076 		hp->ahi_err->err_status = DDI_FM_OK;
1077 		return (DDI_FAILURE);
1078 	}
1079 
1080 	return (DDI_SUCCESS);
1081 }
1082 
1083 
1084 static int
1085 pci_ctlops_poke(pci_t *pci_p, peekpoke_ctlops_t *in_args)
1086 {
1087 	return (in_args->handle ? pci_do_caut_put(pci_p, in_args) :
1088 	    pci_do_poke(pci_p, in_args));
1089 }
1090 
1091 
1092 static int
1093 pci_do_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args)
1094 {
1095 	int err = DDI_SUCCESS;
1096 	on_trap_data_t otd;
1097 
1098 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
1099 		uintptr_t tramp = otd.ot_trampoline;
1100 
1101 		otd.ot_trampoline = (uintptr_t)&peek_fault;
1102 		err = do_peek(in_args->size, (void *)in_args->dev_addr,
1103 		    (void *)in_args->host_addr);
1104 		otd.ot_trampoline = tramp;
1105 	} else
1106 		err = DDI_FAILURE;
1107 
1108 	no_trap();
1109 
1110 #ifdef  DEBUG
1111 	if (err == DDI_FAILURE)
1112 		pci_peekfault_cnt++;
1113 #endif
1114 	return (err);
1115 }
1116 
1117 static int
1118 pci_do_caut_get(pci_t *pci_p, peekpoke_ctlops_t *cautacc_ctlops_arg)
1119 {
1120 	size_t size = cautacc_ctlops_arg->size;
1121 	uintptr_t dev_addr = cautacc_ctlops_arg->dev_addr;
1122 	uintptr_t host_addr = cautacc_ctlops_arg->host_addr;
1123 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)cautacc_ctlops_arg->handle;
1124 	size_t repcount = cautacc_ctlops_arg->repcount;
1125 	uint_t flags = cautacc_ctlops_arg->flags;
1126 
1127 	int err = DDI_SUCCESS;
1128 
1129 	hp->ahi_err->err_expected = DDI_FM_ERR_EXPECTED;
1130 	i_ndi_busop_access_enter(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1131 
1132 	if (!i_ddi_ontrap((ddi_acc_handle_t)hp)) {
1133 		for (; repcount; repcount--) {
1134 			i_ddi_caut_get(size, (void *)dev_addr,
1135 			    (void *)host_addr);
1136 
1137 			host_addr += size;
1138 
1139 			if (flags == DDI_DEV_AUTOINCR)
1140 				dev_addr += size;
1141 		}
1142 	} else {
1143 		int i;
1144 		uint8_t *ff_addr = (uint8_t *)host_addr;
1145 		for (i = 0; i < size; i++)
1146 			*ff_addr++ = 0xff;
1147 
1148 		err = DDI_FAILURE;
1149 	}
1150 
1151 	i_ddi_notrap((ddi_acc_handle_t)hp);
1152 	i_ndi_busop_access_exit(hp->ahi_common.ah_dip, (ddi_acc_handle_t)hp);
1153 	hp->ahi_err->err_expected = DDI_FM_ERR_UNEXPECTED;
1154 
1155 	return (err);
1156 }
1157 
1158 
1159 static int
1160 pci_ctlops_peek(pci_t *pci_p, peekpoke_ctlops_t *in_args, void *result)
1161 {
1162 	result = (void *)in_args->host_addr;
1163 	return (in_args->handle ? pci_do_caut_get(pci_p, in_args) :
1164 	    pci_do_peek(pci_p, in_args));
1165 }
1166 
1167 /*
1168  * get_reg_set_size
1169  *
1170  * Given a dev info pointer to a pci child and a register number, this
1171  * routine returns the size element of that reg set property.
1172  * return value: size of reg set on success, -1 on error
1173  */
1174 static off_t
1175 get_reg_set_size(dev_info_t *child, int rnumber)
1176 {
1177 	pci_regspec_t *pci_rp;
1178 	off_t size;
1179 	int i;
1180 
1181 	if (rnumber < 0)
1182 		return (-1);
1183 
1184 	/*
1185 	 * Get the reg property for the device.
1186 	 */
1187 	if (ddi_getlongprop(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS, "reg",
1188 	    (caddr_t)&pci_rp, &i) != DDI_SUCCESS)
1189 		return (-1);
1190 
1191 	if (rnumber >= (i / (int)sizeof (pci_regspec_t))) {
1192 		kmem_free(pci_rp, i);
1193 		return (-1);
1194 	}
1195 
1196 	size = pci_rp[rnumber].pci_size_low |
1197 		((uint64_t)pci_rp[rnumber].pci_size_hi << 32);
1198 	kmem_free(pci_rp, i);
1199 	return (size);
1200 }
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  *	DDI_CTLOPS_QUIESCE
1217  *	DDI_CTLOPS_UNQUIESCE
1218  *
1219  * All others passed to parent.
1220  */
1221 int
1222 pci_ctlops(dev_info_t *dip, dev_info_t *rdip,
1223 	ddi_ctl_enum_t op, void *arg, void *result)
1224 {
1225 	pci_t *pci_p = get_pci_soft_state(ddi_get_instance(dip));
1226 
1227 	switch (op) {
1228 	case DDI_CTLOPS_INITCHILD:
1229 		return (init_child(pci_p, (dev_info_t *)arg));
1230 
1231 	case DDI_CTLOPS_UNINITCHILD:
1232 		return (uninit_child(pci_p, (dev_info_t *)arg));
1233 
1234 	case DDI_CTLOPS_REPORTDEV:
1235 		return (report_dev(rdip));
1236 
1237 	case DDI_CTLOPS_IOMIN:
1238 
1239 		/*
1240 		 * If we are using the streaming cache, align at
1241 		 * least on a cache line boundary. Otherwise use
1242 		 * whatever alignment is passed in.
1243 		 */
1244 
1245 		if ((uintptr_t)arg) {
1246 			int val = *((int *)result);
1247 
1248 			val = maxbit(val, PCI_SBUF_LINE_SIZE);
1249 			*((int *)result) = val;
1250 		}
1251 		return (DDI_SUCCESS);
1252 
1253 	case DDI_CTLOPS_REGSIZE:
1254 		*((off_t *)result) = get_reg_set_size(rdip, *((int *)arg));
1255 		return (*((off_t *)result) == -1 ? DDI_FAILURE : DDI_SUCCESS);
1256 
1257 	case DDI_CTLOPS_NREGS:
1258 		*((uint_t *)result) = get_nreg_set(rdip);
1259 		return (DDI_SUCCESS);
1260 
1261 	case DDI_CTLOPS_DVMAPAGESIZE:
1262 		*((ulong_t *)result) = IOMMU_PAGE_SIZE;
1263 		return (DDI_SUCCESS);
1264 
1265 	case DDI_CTLOPS_POKE:
1266 		return (pci_ctlops_poke(pci_p, (peekpoke_ctlops_t *)arg));
1267 
1268 	case DDI_CTLOPS_PEEK:
1269 		return (pci_ctlops_peek(pci_p, (peekpoke_ctlops_t *)arg,
1270 		    result));
1271 
1272 	case DDI_CTLOPS_AFFINITY:
1273 		break;
1274 
1275 	case DDI_CTLOPS_QUIESCE:
1276 		return (pci_bus_quiesce(pci_p, rdip, result));
1277 
1278 	case DDI_CTLOPS_UNQUIESCE:
1279 		return (pci_bus_unquiesce(pci_p, rdip, result));
1280 
1281 	default:
1282 		break;
1283 	}
1284 
1285 	/*
1286 	 * Now pass the request up to our parent.
1287 	 */
1288 	DEBUG2(DBG_CTLOPS, dip, "passing request to parent: rdip=%s%d\n",
1289 		ddi_driver_name(rdip), ddi_get_instance(rdip));
1290 	return (ddi_ctlops(dip, rdip, op, arg, result));
1291 }
1292 
1293 
1294 /* ARGSUSED */
1295 int
1296 pci_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
1297     ddi_intr_handle_impl_t *hdlp, void *result)
1298 {
1299 	pci_t		*pci_p = get_pci_soft_state(
1300 			ddi_get_instance(dip));
1301 	int		ret = DDI_SUCCESS;
1302 
1303 	switch (intr_op) {
1304 	case DDI_INTROP_GETCAP:
1305 		/* GetCap will always fail for all non PCI devices */
1306 		(void) pci_intx_get_cap(rdip, (int *)result);
1307 		break;
1308 	case DDI_INTROP_SETCAP:
1309 		ret = DDI_ENOTSUP;
1310 		break;
1311 	case DDI_INTROP_ALLOC:
1312 		*(int *)result = hdlp->ih_scratch1;
1313 		break;
1314 	case DDI_INTROP_FREE:
1315 		break;
1316 	case DDI_INTROP_GETPRI:
1317 		*(int *)result = hdlp->ih_pri ?
1318 		    hdlp->ih_pri : pci_class_to_pil(rdip);
1319 		break;
1320 	case DDI_INTROP_SETPRI:
1321 		break;
1322 	case DDI_INTROP_ADDISR:
1323 		ret = pci_add_intr(dip, rdip, hdlp);
1324 		break;
1325 	case DDI_INTROP_REMISR:
1326 		ret = pci_remove_intr(dip, rdip, hdlp);
1327 		break;
1328 	case DDI_INTROP_ENABLE:
1329 		ret = ib_update_intr_state(pci_p, rdip, hdlp,
1330 		    PCI_INTR_STATE_ENABLE);
1331 		break;
1332 	case DDI_INTROP_DISABLE:
1333 		ret = ib_update_intr_state(pci_p, rdip, hdlp,
1334 		    PCI_INTR_STATE_DISABLE);
1335 		break;
1336 	case DDI_INTROP_SETMASK:
1337 		ret = pci_intx_set_mask(rdip);
1338 		break;
1339 	case DDI_INTROP_CLRMASK:
1340 		ret = pci_intx_clr_mask(rdip);
1341 		break;
1342 	case DDI_INTROP_GETPENDING:
1343 		ret = pci_intx_get_pending(rdip, (int *)result);
1344 		break;
1345 	case DDI_INTROP_NINTRS:
1346 	case DDI_INTROP_NAVAIL:
1347 		*(int *)result = i_ddi_get_nintrs(rdip);
1348 		break;
1349 	case DDI_INTROP_SUPPORTED_TYPES:
1350 		/* PCI nexus driver supports only fixed interrupts */
1351 		*(int *)result = i_ddi_get_nintrs(rdip) ?
1352 		    DDI_INTR_TYPE_FIXED : 0;
1353 		break;
1354 	default:
1355 		ret = DDI_ENOTSUP;
1356 		break;
1357 	}
1358 
1359 	return (ret);
1360 }
1361 
1362 static void
1363 pci_init_hotplug(struct pci *pci_p)
1364 {
1365 	pci_bus_range_t bus_range;
1366 	dev_info_t *dip;
1367 
1368 	/*
1369 	 * Before initializing hotplug - open up
1370 	 * bus range.  The busra module will
1371 	 * initialize its pool of bus numbers from
1372 	 * this. "busra" will be the agent that keeps
1373 	 * track of them during hotplug.  Also, note,
1374 	 * that busra will remove any bus numbers
1375 	 * already in use from boot time.
1376 	 */
1377 	bus_range.lo = 0x0;
1378 	bus_range.hi = 0xff;
1379 	dip = pci_p->pci_dip;
1380 	pci_p->hotplug_capable = B_FALSE;
1381 
1382 	/*
1383 	 * If this property exists, this nexus has hot-plug
1384 	 * slots.
1385 	 */
1386 	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1387 			"hotplug-capable")) {
1388 		if (ndi_prop_update_int_array(DDI_DEV_T_NONE,
1389 			dip, "bus-range",
1390 			(int *)&bus_range,
1391 			2) != DDI_PROP_SUCCESS) {
1392 			return;
1393 		}
1394 
1395 		if (pcihp_init(dip) != DDI_SUCCESS) {
1396 			return;
1397 		}
1398 
1399 		if ((pcihp_ops = pcihp_get_cb_ops()) != NULL) {
1400 			DEBUG2(DBG_ATTACH, dip, "%s%d hotplug enabled",
1401 			    ddi_driver_name(dip), ddi_get_instance(dip));
1402 			pci_p->hotplug_capable = B_TRUE;
1403 		}
1404 	}
1405 }
1406