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