xref: /titanic_50/usr/src/uts/sun4/os/ddi_impl.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * sun4 specific DDI implementation
31  */
32 #include <sys/cpuvar.h>
33 #include <sys/ddi_subrdefs.h>
34 #include <sys/machsystm.h>
35 #include <sys/sunndi.h>
36 #include <sys/sysmacros.h>
37 #include <sys/ontrap.h>
38 #include <vm/seg_kmem.h>
39 #include <sys/membar.h>
40 #include <sys/dditypes.h>
41 #include <sys/ndifm.h>
42 #include <sys/fm/io/ddi.h>
43 #include <sys/ivintr.h>
44 #include <sys/bootconf.h>
45 #include <sys/conf.h>
46 #include <sys/ethernet.h>
47 #include <sys/idprom.h>
48 #include <sys/promif.h>
49 #include <sys/prom_plat.h>
50 #include <sys/systeminfo.h>
51 #include <sys/fpu/fpusystm.h>
52 #include <sys/vm.h>
53 #include <sys/fs/dv_node.h>
54 #include <sys/fs/snode.h>
55 #include <sys/ddi_isa.h>
56 #include <sys/modhash.h>
57 #include <sys/modctl.h>
58 #include <sys/sunldi_impl.h>
59 
60 dev_info_t *get_intr_parent(dev_info_t *, dev_info_t *,
61     ddi_intr_handle_impl_t *);
62 #pragma weak get_intr_parent
63 
64 int process_intr_ops(dev_info_t *, dev_info_t *, ddi_intr_op_t,
65     ddi_intr_handle_impl_t *, void *);
66 #pragma weak process_intr_ops
67 
68 void cells_1275_copy(prop_1275_cell_t *, prop_1275_cell_t *, int32_t);
69     prop_1275_cell_t *cells_1275_cmp(prop_1275_cell_t *, prop_1275_cell_t *,
70     int32_t len);
71 #pragma weak cells_1275_copy
72 
73 /*
74  * Wrapper for ddi_prop_lookup_int_array().
75  * This is handy because it returns the prop length in
76  * bytes which is what most of the callers require.
77  */
78 
79 static int
80 get_prop_int_array(dev_info_t *di, char *pname, int **pval, uint_t *plen)
81 {
82 	int ret;
83 
84 	if ((ret = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, di,
85 	    DDI_PROP_DONTPASS, pname, pval, plen)) == DDI_PROP_SUCCESS) {
86 		*plen = (*plen) * (uint_t)sizeof (int);
87 	}
88 	return (ret);
89 }
90 
91 /*
92  * SECTION: DDI Node Configuration
93  */
94 
95 /*
96  * init_regspec_64:
97  *
98  * If the parent #size-cells is 2, convert the upa-style or
99  * safari-style reg property from 2-size cells to 1 size cell
100  * format, ignoring the size_hi, which must be zero for devices.
101  * (It won't be zero in the memory list properties in the memory
102  * nodes, but that doesn't matter here.)
103  */
104 struct ddi_parent_private_data *
105 init_regspec_64(dev_info_t *dip)
106 {
107 	struct ddi_parent_private_data *pd;
108 	dev_info_t *parent;
109 	int size_cells;
110 
111 	/*
112 	 * If there are no "reg"s in the child node, return.
113 	 */
114 	pd = ddi_get_parent_data(dip);
115 	if ((pd == NULL) || (pd->par_nreg == 0)) {
116 		return (pd);
117 	}
118 	parent = ddi_get_parent(dip);
119 
120 	size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent,
121 	    DDI_PROP_DONTPASS, "#size-cells", 1);
122 
123 	if (size_cells != 1)  {
124 
125 		int n, j;
126 		struct regspec *irp;
127 		struct reg_64 {
128 			uint_t addr_hi, addr_lo, size_hi, size_lo;
129 		};
130 		struct reg_64 *r64_rp;
131 		struct regspec *rp;
132 		uint_t len = 0;
133 		int *reg_prop;
134 
135 		ASSERT(size_cells == 2);
136 
137 		/*
138 		 * We already looked the property up once before if
139 		 * pd is non-NULL.
140 		 */
141 		(void) ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
142 		    DDI_PROP_DONTPASS, OBP_REG, &reg_prop, &len);
143 		ASSERT(len != 0);
144 
145 		n = sizeof (struct reg_64) / sizeof (int);
146 		n = len / n;
147 
148 		/*
149 		 * We're allocating a buffer the size of the PROM's property,
150 		 * but we're only using a smaller portion when we assign it
151 		 * to a regspec.  We do this so that in the
152 		 * impl_ddi_sunbus_removechild function, we will
153 		 * always free the right amount of memory.
154 		 */
155 		irp = rp = (struct regspec *)reg_prop;
156 		r64_rp = (struct reg_64 *)pd->par_reg;
157 
158 		for (j = 0; j < n; ++j, ++rp, ++r64_rp) {
159 			ASSERT(r64_rp->size_hi == 0);
160 			rp->regspec_bustype = r64_rp->addr_hi;
161 			rp->regspec_addr = r64_rp->addr_lo;
162 			rp->regspec_size = r64_rp->size_lo;
163 		}
164 
165 		ddi_prop_free((void *)pd->par_reg);
166 		pd->par_nreg = n;
167 		pd->par_reg = irp;
168 	}
169 	return (pd);
170 }
171 
172 /*
173  * Create a ddi_parent_private_data structure from the ddi properties of
174  * the dev_info node.
175  *
176  * The "reg" is required if the driver wishes to create mappings on behalf
177  * of the device. The "reg" property is assumed to be a list of at least
178  * one triplet
179  *
180  *	<bustype, address, size>*1
181  *
182  * The "interrupt" property is no longer part of parent private data on
183  * sun4u. The interrupt parent is may not be the device tree parent.
184  *
185  * The "ranges" property describes the mapping of child addresses to parent
186  * addresses.
187  *
188  * N.B. struct rangespec is defined for the following default values:
189  *			parent  child
190  *	#address-cells	2	2
191  *	#size-cells	1	1
192  * This function doesn't deal with non-default cells and will not create
193  * ranges in such cases.
194  */
195 void
196 make_ddi_ppd(dev_info_t *child, struct ddi_parent_private_data **ppd)
197 {
198 	struct ddi_parent_private_data *pdptr;
199 	int *reg_prop, *rng_prop;
200 	uint_t reg_len = 0, rng_len = 0;
201 	dev_info_t *parent;
202 	int parent_addr_cells, parent_size_cells;
203 	int child_addr_cells, child_size_cells;
204 
205 	*ppd = pdptr = kmem_zalloc(sizeof (*pdptr), KM_SLEEP);
206 
207 	/*
208 	 * root node has no parent private data, so *ppd should
209 	 * be initialized for naming to work properly.
210 	 */
211 	if ((parent = ddi_get_parent(child)) == NULL)
212 		return;
213 
214 	/*
215 	 * Set reg field of parent data from "reg" property
216 	 */
217 	if ((get_prop_int_array(child, OBP_REG, &reg_prop, &reg_len)
218 	    == DDI_PROP_SUCCESS) && (reg_len != 0)) {
219 		pdptr->par_nreg = (int)(reg_len / sizeof (struct regspec));
220 		pdptr->par_reg = (struct regspec *)reg_prop;
221 	}
222 
223 	/*
224 	 * "ranges" property ...
225 	 *
226 	 * This function does not handle cases where #address-cells != 2
227 	 * and * min(parent, child) #size-cells != 1 (see bugid 4211124).
228 	 *
229 	 * Nexus drivers with such exceptions (e.g. pci ranges)
230 	 * should either create a separate function for handling
231 	 * ranges or not use parent private data to store ranges.
232 	 */
233 
234 	/* root node has no ranges */
235 	if ((parent = ddi_get_parent(child)) == NULL)
236 		return;
237 
238 	child_addr_cells = ddi_prop_get_int(DDI_DEV_T_ANY, child,
239 	    DDI_PROP_DONTPASS, "#address-cells", 2);
240 	child_size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, child,
241 	    DDI_PROP_DONTPASS, "#size-cells", 1);
242 	parent_addr_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent,
243 	    DDI_PROP_DONTPASS, "#address-cells", 2);
244 	parent_size_cells = ddi_prop_get_int(DDI_DEV_T_ANY, parent,
245 	    DDI_PROP_DONTPASS, "#size-cells", 1);
246 	if (child_addr_cells != 2 || parent_addr_cells != 2 ||
247 	    (child_size_cells != 1 && parent_size_cells != 1)) {
248 		NDI_CONFIG_DEBUG((CE_NOTE, "!ranges not made in parent data; "
249 		    "#address-cells or #size-cells have non-default value"));
250 		return;
251 	}
252 
253 	if (get_prop_int_array(child, OBP_RANGES, &rng_prop, &rng_len)
254 	    == DDI_PROP_SUCCESS) {
255 		pdptr->par_nrng = rng_len / (int)(sizeof (struct rangespec));
256 		pdptr->par_rng = (struct rangespec *)rng_prop;
257 	}
258 }
259 
260 /*
261  * Free ddi_parent_private_data structure
262  */
263 void
264 impl_free_ddi_ppd(dev_info_t *dip)
265 {
266 	struct ddi_parent_private_data *pdptr = ddi_get_parent_data(dip);
267 
268 	if (pdptr == NULL)
269 		return;
270 
271 	if (pdptr->par_nrng != 0)
272 		ddi_prop_free((void *)pdptr->par_rng);
273 
274 	if (pdptr->par_nreg != 0)
275 		ddi_prop_free((void *)pdptr->par_reg);
276 
277 	kmem_free(pdptr, sizeof (*pdptr));
278 	ddi_set_parent_data(dip, NULL);
279 }
280 
281 /*
282  * Name a child of sun busses based on the reg spec.
283  * Handles the following properties:
284  *
285  *	Property	value
286  *	Name		type
287  *
288  *	reg		register spec
289  *	interrupts	new (bus-oriented) interrupt spec
290  *	ranges		range spec
291  *
292  * This may be called multiple times, independent of
293  * initchild calls.
294  */
295 static int
296 impl_sunbus_name_child(dev_info_t *child, char *name, int namelen)
297 {
298 	struct ddi_parent_private_data *pdptr;
299 	struct regspec *rp;
300 
301 	/*
302 	 * Fill in parent-private data and this function returns to us
303 	 * an indication if it used "registers" to fill in the data.
304 	 */
305 	if (ddi_get_parent_data(child) == NULL) {
306 		make_ddi_ppd(child, &pdptr);
307 		ddi_set_parent_data(child, pdptr);
308 	}
309 
310 	/*
311 	 * No reg property, return null string as address
312 	 * (e.g. root node)
313 	 */
314 	name[0] = '\0';
315 	if (sparc_pd_getnreg(child) == 0) {
316 		return (DDI_SUCCESS);
317 	}
318 
319 	rp = sparc_pd_getreg(child, 0);
320 	(void) snprintf(name, namelen, "%x,%x",
321 	    rp->regspec_bustype, rp->regspec_addr);
322 	return (DDI_SUCCESS);
323 }
324 
325 
326 /*
327  * Called from the bus_ctl op of some drivers.
328  * to implement the DDI_CTLOPS_INITCHILD operation.
329  *
330  * NEW drivers should NOT use this function, but should declare
331  * there own initchild/uninitchild handlers. (This function assumes
332  * the layout of the parent private data and the format of "reg",
333  * "ranges", "interrupts" properties and that #address-cells and
334  * #size-cells of the parent bus are defined to be default values.)
335  */
336 int
337 impl_ddi_sunbus_initchild(dev_info_t *child)
338 {
339 	char name[MAXNAMELEN];
340 
341 	(void) impl_sunbus_name_child(child, name, MAXNAMELEN);
342 	ddi_set_name_addr(child, name);
343 
344 	/*
345 	 * Try to merge .conf node. If successful, return failure to
346 	 * remove this child.
347 	 */
348 	if ((ndi_dev_is_persistent_node(child) == 0) &&
349 	    (ndi_merge_node(child, impl_sunbus_name_child) == DDI_SUCCESS)) {
350 		impl_ddi_sunbus_removechild(child);
351 		return (DDI_FAILURE);
352 	}
353 	return (DDI_SUCCESS);
354 }
355 
356 /*
357  * A better name for this function would be impl_ddi_sunbus_uninitchild()
358  * It does not remove the child, it uninitializes it, reclaiming the
359  * resources taken by impl_ddi_sunbus_initchild.
360  */
361 void
362 impl_ddi_sunbus_removechild(dev_info_t *dip)
363 {
364 	impl_free_ddi_ppd(dip);
365 	ddi_set_name_addr(dip, NULL);
366 	/*
367 	 * Strip the node to properly convert it back to prototype form
368 	 */
369 	impl_rem_dev_props(dip);
370 }
371 
372 /*
373  * SECTION: DDI Interrupt
374  */
375 
376 void
377 cells_1275_copy(prop_1275_cell_t *from, prop_1275_cell_t *to, int32_t len)
378 {
379 	int i;
380 	for (i = 0; i < len; i++)
381 		*to = *from;
382 }
383 
384 prop_1275_cell_t *
385 cells_1275_cmp(prop_1275_cell_t *cell1, prop_1275_cell_t *cell2, int32_t len)
386 {
387 	prop_1275_cell_t *match_cell = 0;
388 	int32_t i;
389 
390 	for (i = 0; i < len; i++)
391 		if (cell1[i] != cell2[i]) {
392 			match_cell = &cell1[i];
393 			break;
394 		}
395 
396 	return (match_cell);
397 }
398 
399 /*
400  * get_intr_parent() is a generic routine that process a 1275 interrupt
401  * map (imap) property.  This function returns a dev_info_t structure
402  * which claims ownership of the interrupt domain.
403  * It also returns the new interrupt translation within this new domain.
404  * If an interrupt-parent or interrupt-map property are not found,
405  * then we fallback to using the device tree's parent.
406  *
407  * imap entry format:
408  * <reg>,<interrupt>,<phandle>,<translated interrupt>
409  * reg - The register specification in the interrupts domain
410  * interrupt - The interrupt specification
411  * phandle - PROM handle of the device that owns the xlated interrupt domain
412  * translated interrupt - interrupt specifier in the parents domain
413  * note: <reg>,<interrupt> - The reg and interrupt can be combined to create
414  *	a unique entry called a unit interrupt specifier.
415  *
416  * Here's the processing steps:
417  * step1 - If the interrupt-parent property exists, create the ispec and
418  *	return the dip of the interrupt parent.
419  * step2 - Extract the interrupt-map property and the interrupt-map-mask
420  *	If these don't exist, just return the device tree parent.
421  * step3 - build up the unit interrupt specifier to match against the
422  *	interrupt map property
423  * step4 - Scan the interrupt-map property until a match is found
424  * step4a - Extract the interrupt parent
425  * step4b - Compare the unit interrupt specifier
426  */
427 dev_info_t *
428 get_intr_parent(dev_info_t *pdip, dev_info_t *dip, ddi_intr_handle_impl_t *hdlp)
429 {
430 	prop_1275_cell_t *imap, *imap_mask, *scan, *reg_p, *match_req;
431 	int32_t imap_sz, imap_cells, imap_scan_cells, imap_mask_sz,
432 	    addr_cells, intr_cells, reg_len, i, j;
433 	int32_t match_found = 0;
434 	dev_info_t *intr_parent_dip = NULL;
435 	uint32_t *intr = &hdlp->ih_vector;
436 	uint32_t nodeid;
437 #ifdef DEBUG
438 	static int debug = 0;
439 #endif
440 
441 	/*
442 	 * step1
443 	 * If we have an interrupt-parent property, this property represents
444 	 * the nodeid of our interrupt parent.
445 	 */
446 	if ((nodeid = ddi_getprop(DDI_DEV_T_ANY, dip, 0,
447 	    "interrupt-parent", -1)) != -1) {
448 		intr_parent_dip = e_ddi_nodeid_to_dip(nodeid);
449 		ASSERT(intr_parent_dip);
450 
451 		/*
452 		 * Attach the interrupt parent.
453 		 *
454 		 * N.B. e_ddi_nodeid_to_dip() isn't safe under DR.
455 		 *	Also, interrupt parent isn't held. This needs
456 		 *	to be revisited if DR-capable platforms implement
457 		 *	interrupt redirection.
458 		 */
459 		if (i_ddi_attach_node_hierarchy(intr_parent_dip)
460 		    != DDI_SUCCESS) {
461 			ndi_rele_devi(intr_parent_dip);
462 			return (NULL);
463 		}
464 
465 		return (intr_parent_dip);
466 	}
467 
468 	/*
469 	 * step2
470 	 * Get interrupt map structure from PROM property
471 	 */
472 	if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
473 	    "interrupt-map", (caddr_t)&imap, &imap_sz)
474 	    != DDI_PROP_SUCCESS) {
475 		/*
476 		 * If we don't have an imap property, default to using the
477 		 * device tree.
478 		 */
479 
480 		ndi_hold_devi(pdip);
481 		return (pdip);
482 	}
483 
484 	/* Get the interrupt mask property */
485 	if (ddi_getlongprop(DDI_DEV_T_ANY, pdip, DDI_PROP_DONTPASS,
486 	    "interrupt-map-mask", (caddr_t)&imap_mask, &imap_mask_sz)
487 	    != DDI_PROP_SUCCESS) {
488 		/*
489 		 * If we don't find this property, we have to fail the request
490 		 * because the 1275 imap property wasn't defined correctly.
491 		 */
492 		ASSERT(intr_parent_dip == NULL);
493 		goto exit2;
494 	}
495 
496 	/* Get the address cell size */
497 	addr_cells = ddi_getprop(DDI_DEV_T_ANY, pdip, 0,
498 	    "#address-cells", 2);
499 
500 	/* Get the interrupts cell size */
501 	intr_cells = ddi_getprop(DDI_DEV_T_ANY, pdip, 0,
502 	    "#interrupt-cells", 1);
503 
504 	/*
505 	 * step3
506 	 * Now lets build up the unit interrupt specifier e.g. reg,intr
507 	 * and apply the imap mask.  match_req will hold this when we're
508 	 * through.
509 	 */
510 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "reg",
511 	    (caddr_t)&reg_p, &reg_len) != DDI_SUCCESS) {
512 		ASSERT(intr_parent_dip == NULL);
513 		goto exit3;
514 	}
515 
516 	match_req = kmem_alloc(CELLS_1275_TO_BYTES(addr_cells) +
517 	    CELLS_1275_TO_BYTES(intr_cells), KM_SLEEP);
518 
519 	for (i = 0; i < addr_cells; i++)
520 		match_req[i] = (reg_p[i] & imap_mask[i]);
521 
522 	for (j = 0; j < intr_cells; i++, j++)
523 		match_req[i] = (intr[j] & imap_mask[i]);
524 
525 	/* Calculate the imap size in cells */
526 	imap_cells = BYTES_TO_1275_CELLS(imap_sz);
527 
528 #ifdef DEBUG
529 	if (debug)
530 		prom_printf("reg cell size 0x%x, intr cell size 0x%x, "
531 		    "match_request 0x%p, imap 0x%p\n", addr_cells, intr_cells,
532 		    match_req, imap);
533 #endif
534 
535 	/*
536 	 * Scan the imap property looking for a match of the interrupt unit
537 	 * specifier.  This loop is rather complex since the data within the
538 	 * imap property may vary in size.
539 	 */
540 	for (scan = imap, imap_scan_cells = i = 0;
541 	    imap_scan_cells < imap_cells; scan += i, imap_scan_cells += i) {
542 		int new_intr_cells;
543 
544 		/* Set the index to the nodeid field */
545 		i = addr_cells + intr_cells;
546 
547 		/*
548 		 * step4a
549 		 * Translate the nodeid field to a dip
550 		 */
551 		ASSERT(intr_parent_dip == NULL);
552 		intr_parent_dip = e_ddi_nodeid_to_dip((uint_t)scan[i++]);
553 
554 		ASSERT(intr_parent_dip != 0);
555 #ifdef DEBUG
556 		if (debug)
557 			prom_printf("scan 0x%p\n", scan);
558 #endif
559 		/*
560 		 * The tmp_dip describes the new domain, get it's interrupt
561 		 * cell size
562 		 */
563 		new_intr_cells = ddi_getprop(DDI_DEV_T_ANY, intr_parent_dip, 0,
564 		    "#interrupts-cells", 1);
565 
566 		/*
567 		 * step4b
568 		 * See if we have a match on the interrupt unit specifier
569 		 */
570 		if (cells_1275_cmp(match_req, scan, addr_cells + intr_cells)
571 		    == 0) {
572 			uint32_t *intr;
573 
574 			match_found = 1;
575 
576 			/*
577 			 * If we have an imap parent whose not in our device
578 			 * tree path, we need to hold and install that driver.
579 			 */
580 			if (i_ddi_attach_node_hierarchy(intr_parent_dip)
581 			    != DDI_SUCCESS) {
582 				ndi_rele_devi(intr_parent_dip);
583 				intr_parent_dip = (dev_info_t *)NULL;
584 				goto exit4;
585 			}
586 
587 			/*
588 			 * We need to handcraft an ispec along with a bus
589 			 * interrupt value, so we can dup it into our
590 			 * standard ispec structure.
591 			 */
592 			/* Extract the translated interrupt information */
593 			intr = kmem_alloc(
594 			    CELLS_1275_TO_BYTES(new_intr_cells), KM_SLEEP);
595 
596 			for (j = 0; j < new_intr_cells; j++, i++)
597 				intr[j] = scan[i];
598 
599 			cells_1275_copy(intr, &hdlp->ih_vector, new_intr_cells);
600 
601 			kmem_free(intr, CELLS_1275_TO_BYTES(new_intr_cells));
602 
603 #ifdef DEBUG
604 			if (debug)
605 				prom_printf("dip 0x%p\n", intr_parent_dip);
606 #endif
607 			break;
608 		} else {
609 #ifdef DEBUG
610 			if (debug)
611 				prom_printf("dip 0x%p\n", intr_parent_dip);
612 #endif
613 			ndi_rele_devi(intr_parent_dip);
614 			intr_parent_dip = NULL;
615 			i += new_intr_cells;
616 		}
617 	}
618 
619 	/*
620 	 * If we haven't found our interrupt parent at this point, fallback
621 	 * to using the device tree.
622 	 */
623 	if (!match_found) {
624 		ndi_hold_devi(pdip);
625 		ASSERT(intr_parent_dip == NULL);
626 		intr_parent_dip = pdip;
627 	}
628 
629 	ASSERT(intr_parent_dip != NULL);
630 
631 exit4:
632 	kmem_free(reg_p, reg_len);
633 	kmem_free(match_req, CELLS_1275_TO_BYTES(addr_cells) +
634 	    CELLS_1275_TO_BYTES(intr_cells));
635 
636 exit3:
637 	kmem_free(imap_mask, imap_mask_sz);
638 
639 exit2:
640 	kmem_free(imap, imap_sz);
641 
642 	return (intr_parent_dip);
643 }
644 
645 /*
646  * process_intr_ops:
647  *
648  * Process the interrupt op via the interrupt parent.
649  */
650 int
651 process_intr_ops(dev_info_t *pdip, dev_info_t *rdip, ddi_intr_op_t op,
652     ddi_intr_handle_impl_t *hdlp, void *result)
653 {
654 	int		ret = DDI_FAILURE;
655 
656 	if (NEXUS_HAS_INTR_OP(pdip)) {
657 		ret = (*(DEVI(pdip)->devi_ops->devo_bus_ops->
658 		    bus_intr_op)) (pdip, rdip, op, hdlp, result);
659 	} else {
660 		cmn_err(CE_WARN, "Failed to process interrupt "
661 		    "for %s%d due to down-rev nexus driver %s%d",
662 		    ddi_get_name(rdip), ddi_get_instance(rdip),
663 		    ddi_get_name(pdip), ddi_get_instance(pdip));
664 	}
665 
666 	return (ret);
667 }
668 
669 /*ARGSUSED*/
670 uint_t
671 softlevel1(caddr_t arg)
672 {
673 	softint();
674 	return (1);
675 }
676 
677 /*
678  * indirection table, to save us some large switch statements
679  * NOTE: This must agree with "INTLEVEL_foo" constants in
680  *	<sys/avintr.h>
681  */
682 struct autovec *const vectorlist[] = { 0 };
683 
684 /*
685  * This value is exported here for the functions in avintr.c
686  */
687 const uint_t maxautovec = (sizeof (vectorlist) / sizeof (vectorlist[0]));
688 
689 /*
690  * Check for machine specific interrupt levels which cannot be reassigned by
691  * settrap(), sun4u version.
692  *
693  * sun4u does not support V8 SPARC "fast trap" handlers.
694  */
695 /*ARGSUSED*/
696 int
697 exclude_settrap(int lvl)
698 {
699 	return (1);
700 }
701 
702 /*
703  * Check for machine specific interrupt levels which cannot have interrupt
704  * handlers added. We allow levels 1 through 15; level 0 is nonsense.
705  */
706 /*ARGSUSED*/
707 int
708 exclude_level(int lvl)
709 {
710 	return ((lvl < 1) || (lvl > 15));
711 }
712 
713 /*
714  * Wrapper functions used by New DDI interrupt framework.
715  */
716 
717 /*
718  * i_ddi_intr_ops:
719  */
720 int
721 i_ddi_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t op,
722     ddi_intr_handle_impl_t *hdlp, void *result)
723 {
724 	dev_info_t	*pdip = ddi_get_parent(dip);
725 	int		ret = DDI_FAILURE;
726 
727 	/*
728 	 * The following check is required to address
729 	 * one of the test case of ADDI test suite.
730 	 */
731 	if (pdip == NULL)
732 		return (DDI_FAILURE);
733 
734 	if (hdlp->ih_type != DDI_INTR_TYPE_FIXED)
735 		return (process_intr_ops(pdip, rdip, op, hdlp, result));
736 
737 	if (hdlp->ih_vector == 0)
738 		hdlp->ih_vector = i_ddi_get_inum(rdip, hdlp->ih_inum);
739 
740 	if (hdlp->ih_pri == 0)
741 		hdlp->ih_pri = i_ddi_get_intr_pri(rdip, hdlp->ih_inum);
742 
743 	switch (op) {
744 	case DDI_INTROP_ADDISR:
745 	case DDI_INTROP_REMISR:
746 	case DDI_INTROP_ENABLE:
747 	case DDI_INTROP_DISABLE:
748 	case DDI_INTROP_BLOCKENABLE:
749 	case DDI_INTROP_BLOCKDISABLE:
750 		/*
751 		 * Try and determine our parent and possibly an interrupt
752 		 * translation. intr parent dip returned held
753 		 */
754 		if ((pdip = get_intr_parent(pdip, dip, hdlp)) == NULL)
755 			goto done;
756 	}
757 
758 	ret = process_intr_ops(pdip, rdip, op, hdlp, result);
759 
760 done:
761 	switch (op) {
762 	case DDI_INTROP_ADDISR:
763 	case DDI_INTROP_REMISR:
764 	case DDI_INTROP_ENABLE:
765 	case DDI_INTROP_DISABLE:
766 	case DDI_INTROP_BLOCKENABLE:
767 	case DDI_INTROP_BLOCKDISABLE:
768 		/* Release hold acquired in get_intr_parent() */
769 		if (pdip)
770 			ndi_rele_devi(pdip);
771 	}
772 
773 	hdlp->ih_vector = 0;
774 
775 	return (ret);
776 }
777 
778 /*
779  * i_ddi_add_ivintr:
780  */
781 /*ARGSUSED*/
782 int
783 i_ddi_add_ivintr(ddi_intr_handle_impl_t *hdlp)
784 {
785 	/*
786 	 * If the PIL was set and is valid use it, otherwise
787 	 * default it to 1
788 	 */
789 	if ((hdlp->ih_pri < 1) || (hdlp->ih_pri > PIL_MAX))
790 		hdlp->ih_pri = 1;
791 
792 	VERIFY(add_ivintr(hdlp->ih_vector, hdlp->ih_pri,
793 	    (intrfunc)hdlp->ih_cb_func, hdlp->ih_cb_arg1,
794 	    hdlp->ih_cb_arg2, NULL) == 0);
795 
796 	return (DDI_SUCCESS);
797 }
798 
799 /*
800  * i_ddi_rem_ivintr:
801  */
802 /*ARGSUSED*/
803 void
804 i_ddi_rem_ivintr(ddi_intr_handle_impl_t *hdlp)
805 {
806 	VERIFY(rem_ivintr(hdlp->ih_vector, hdlp->ih_pri) == 0);
807 }
808 
809 /*
810  * i_ddi_get_inum - Get the interrupt number property from the
811  * specified device. Note that this function is called only for
812  * the FIXED interrupt type.
813  */
814 uint32_t
815 i_ddi_get_inum(dev_info_t *dip, uint_t inumber)
816 {
817 	int32_t			intrlen, intr_cells, max_intrs;
818 	prop_1275_cell_t	*ip, intr_sz;
819 	uint32_t		intr = 0;
820 
821 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
822 	    DDI_PROP_CANSLEEP,
823 	    "interrupts", (caddr_t)&ip, &intrlen) == DDI_SUCCESS) {
824 
825 		intr_cells = ddi_getprop(DDI_DEV_T_ANY, dip, 0,
826 		    "#interrupt-cells", 1);
827 
828 		/* adjust for number of bytes */
829 		intr_sz = CELLS_1275_TO_BYTES(intr_cells);
830 
831 		/* Calculate the number of interrupts */
832 		max_intrs = intrlen / intr_sz;
833 
834 		if (inumber < max_intrs) {
835 			prop_1275_cell_t *intrp = ip;
836 
837 			/* Index into interrupt property */
838 			intrp += (inumber * intr_cells);
839 
840 			cells_1275_copy(intrp, &intr, intr_cells);
841 		}
842 
843 		kmem_free(ip, intrlen);
844 	}
845 
846 	return (intr);
847 }
848 
849 /*
850  * i_ddi_get_intr_pri - Get the interrupt-priorities property from
851  * the specified device. Note that this function is called only for
852  * the FIXED interrupt type.
853  */
854 uint32_t
855 i_ddi_get_intr_pri(dev_info_t *dip, uint_t inumber)
856 {
857 	uint32_t	*intr_prio_p;
858 	uint32_t	pri = 0;
859 	int32_t		i;
860 
861 	/*
862 	 * Use the "interrupt-priorities" property to determine the
863 	 * the pil/ipl for the interrupt handler.
864 	 */
865 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
866 	    "interrupt-priorities", (caddr_t)&intr_prio_p,
867 	    &i) == DDI_SUCCESS) {
868 		if (inumber < (i / sizeof (int32_t)))
869 			pri = intr_prio_p[inumber];
870 		kmem_free(intr_prio_p, i);
871 	}
872 
873 	return (pri);
874 }
875 
876 int
877 i_ddi_get_intx_nintrs(dev_info_t *dip)
878 {
879 	int32_t intrlen;
880 	prop_1275_cell_t intr_sz;
881 	prop_1275_cell_t *ip;
882 	int32_t ret = 0;
883 
884 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS |
885 	    DDI_PROP_CANSLEEP,
886 	    "interrupts", (caddr_t)&ip, &intrlen) == DDI_SUCCESS) {
887 
888 		intr_sz = ddi_getprop(DDI_DEV_T_ANY, dip, 0,
889 		    "#interrupt-cells", 1);
890 		/* adjust for number of bytes */
891 		intr_sz = CELLS_1275_TO_BYTES(intr_sz);
892 
893 		ret = intrlen / intr_sz;
894 
895 		kmem_free(ip, intrlen);
896 	}
897 
898 	return (ret);
899 }
900 
901 /*
902  * i_ddi_add_softint - allocate and add a software interrupt.
903  *
904  * NOTE: All software interrupts that are registered through DDI
905  *	 should be triggered only on a single target or CPU.
906  */
907 int
908 i_ddi_add_softint(ddi_softint_hdl_impl_t *hdlp)
909 {
910 	if ((hdlp->ih_private = (void *)add_softintr(hdlp->ih_pri,
911 	    hdlp->ih_cb_func, hdlp->ih_cb_arg1, SOFTINT_ST)) == NULL)
912 		return (DDI_FAILURE);
913 
914 	return (DDI_SUCCESS);
915 }
916 
917 /*
918  * i_ddi_remove_softint - remove and free a software interrupt.
919  */
920 void
921 i_ddi_remove_softint(ddi_softint_hdl_impl_t *hdlp)
922 {
923 	ASSERT(hdlp->ih_private != NULL);
924 
925 	if (rem_softintr((uint64_t)hdlp->ih_private) == 0)
926 		hdlp->ih_private = NULL;
927 }
928 
929 /*
930  * i_ddi_trigger_softint - trigger a software interrupt.
931  */
932 int
933 i_ddi_trigger_softint(ddi_softint_hdl_impl_t *hdlp, void *arg2)
934 {
935 	int	ret;
936 
937 	ASSERT(hdlp->ih_private != NULL);
938 
939 	/* Update the second argument for the software interrupt */
940 	if ((ret = update_softint_arg2((uint64_t)hdlp->ih_private, arg2)) == 0)
941 		setsoftint((uint64_t)hdlp->ih_private);
942 
943 	return (ret ? DDI_EPENDING : DDI_SUCCESS);
944 }
945 
946 /*
947  * i_ddi_set_softint_pri - change software interrupt priority.
948  */
949 /* ARGSUSED */
950 int
951 i_ddi_set_softint_pri(ddi_softint_hdl_impl_t *hdlp, uint_t old_pri)
952 {
953 	int	ret;
954 
955 	ASSERT(hdlp->ih_private != NULL);
956 
957 	/* Update the interrupt priority for the software interrupt */
958 	ret = update_softint_pri((uint64_t)hdlp->ih_private, hdlp->ih_pri);
959 
960 	return (ret ? DDI_FAILURE : DDI_SUCCESS);
961 }
962 
963 /*ARGSUSED*/
964 void
965 i_ddi_alloc_intr_phdl(ddi_intr_handle_impl_t *hdlp)
966 {
967 }
968 
969 /*ARGSUSED*/
970 void
971 i_ddi_free_intr_phdl(ddi_intr_handle_impl_t *hdlp)
972 {
973 }
974 
975 /*
976  * SECTION: DDI Memory/DMA
977  */
978 
979 /* set HAT endianess attributes from ddi_device_acc_attr */
980 void
981 i_ddi_devacc_to_hatacc(ddi_device_acc_attr_t *devaccp, uint_t *hataccp)
982 {
983 	if (devaccp != NULL) {
984 		if (devaccp->devacc_attr_endian_flags == DDI_STRUCTURE_LE_ACC) {
985 			*hataccp &= ~HAT_ENDIAN_MASK;
986 			*hataccp |= HAT_STRUCTURE_LE;
987 		}
988 	}
989 }
990 
991 /*
992  * Check if the specified cache attribute is supported on the platform.
993  * This function must be called before i_ddi_cacheattr_to_hatacc().
994  */
995 boolean_t
996 i_ddi_check_cache_attr(uint_t flags)
997 {
998 	/*
999 	 * The cache attributes are mutually exclusive. Any combination of
1000 	 * the attributes leads to a failure.
1001 	 */
1002 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
1003 	if ((cache_attr != 0) && ((cache_attr & (cache_attr - 1)) != 0))
1004 		return (B_FALSE);
1005 
1006 	/*
1007 	 * On the sparc architecture, only IOMEM_DATA_CACHED is meaningful,
1008 	 * but others lead to a failure.
1009 	 */
1010 	if (cache_attr & IOMEM_DATA_CACHED)
1011 		return (B_TRUE);
1012 	else
1013 		return (B_FALSE);
1014 }
1015 
1016 /* set HAT cache attributes from the cache attributes */
1017 void
1018 i_ddi_cacheattr_to_hatacc(uint_t flags, uint_t *hataccp)
1019 {
1020 	uint_t cache_attr = IOMEM_CACHE_ATTR(flags);
1021 	static char *fname = "i_ddi_cacheattr_to_hatacc";
1022 #if defined(lint)
1023 	*hataccp = *hataccp;
1024 #endif
1025 	/*
1026 	 * set HAT attrs according to the cache attrs.
1027 	 */
1028 	switch (cache_attr) {
1029 	/*
1030 	 * The cache coherency is always maintained on SPARC, and
1031 	 * nothing is required.
1032 	 */
1033 	case IOMEM_DATA_CACHED:
1034 		break;
1035 	/*
1036 	 * Both IOMEM_DATA_UC_WRITE_COMBINED and IOMEM_DATA_UNCACHED are
1037 	 * not supported on SPARC -- this case must not occur because the
1038 	 * cache attribute is scrutinized before this function is called.
1039 	 */
1040 	case IOMEM_DATA_UNCACHED:
1041 	case IOMEM_DATA_UC_WR_COMBINE:
1042 	default:
1043 		cmn_err(CE_WARN, "%s: cache_attr=0x%x is ignored.",
1044 		    fname, cache_attr);
1045 	}
1046 }
1047 
1048 static vmem_t *little_endian_arena;
1049 static vmem_t *big_endian_arena;
1050 
1051 static void *
1052 segkmem_alloc_le(vmem_t *vmp, size_t size, int flag)
1053 {
1054 	return (segkmem_xalloc(vmp, NULL, size, flag, HAT_STRUCTURE_LE,
1055 	    segkmem_page_create, NULL));
1056 }
1057 
1058 static void *
1059 segkmem_alloc_be(vmem_t *vmp, size_t size, int flag)
1060 {
1061 	return (segkmem_xalloc(vmp, NULL, size, flag, HAT_STRUCTURE_BE,
1062 	    segkmem_page_create, NULL));
1063 }
1064 
1065 void
1066 ka_init(void)
1067 {
1068 	little_endian_arena = vmem_create("little_endian", NULL, 0, 1,
1069 	    segkmem_alloc_le, segkmem_free, heap_arena, 0, VM_SLEEP);
1070 	big_endian_arena = vmem_create("big_endian", NULL, 0, 1,
1071 	    segkmem_alloc_be, segkmem_free, heap_arena, 0, VM_SLEEP);
1072 }
1073 
1074 /*
1075  * Allocate from the system, aligned on a specific boundary.
1076  * The alignment, if non-zero, must be a power of 2.
1077  */
1078 static void *
1079 kalloca(size_t size, size_t align, int cansleep, uint_t endian_flags)
1080 {
1081 	size_t *addr, *raddr, rsize;
1082 	size_t hdrsize = 4 * sizeof (size_t);	/* must be power of 2 */
1083 
1084 	align = MAX(align, hdrsize);
1085 	ASSERT((align & (align - 1)) == 0);
1086 
1087 	/*
1088 	 * We need to allocate
1089 	 *    rsize = size + hdrsize + align - MIN(hdrsize, buffer_alignment)
1090 	 * bytes to be sure we have enough freedom to satisfy the request.
1091 	 * Since the buffer alignment depends on the request size, this is
1092 	 * not straightforward to use directly.
1093 	 *
1094 	 * kmem guarantees that any allocation of a 64-byte multiple will be
1095 	 * 64-byte aligned.  Since rounding up the request could add more
1096 	 * than we save, we compute the size with and without alignment, and
1097 	 * use the smaller of the two.
1098 	 */
1099 	rsize = size + hdrsize + align;
1100 
1101 	if (endian_flags == DDI_STRUCTURE_LE_ACC) {
1102 		raddr = vmem_alloc(little_endian_arena, rsize,
1103 		    cansleep ? VM_SLEEP : VM_NOSLEEP);
1104 	} else {
1105 		raddr = vmem_alloc(big_endian_arena, rsize,
1106 		    cansleep ? VM_SLEEP : VM_NOSLEEP);
1107 	}
1108 
1109 	if (raddr == NULL)
1110 		return (NULL);
1111 
1112 	addr = (size_t *)P2ROUNDUP((uintptr_t)raddr + hdrsize, align);
1113 	ASSERT((uintptr_t)addr + size - (uintptr_t)raddr <= rsize);
1114 
1115 	addr[-3] = (size_t)endian_flags;
1116 	addr[-2] = (size_t)raddr;
1117 	addr[-1] = rsize;
1118 
1119 	return (addr);
1120 }
1121 
1122 static void
1123 kfreea(void *addr)
1124 {
1125 	size_t *saddr = addr;
1126 
1127 	if (saddr[-3] == DDI_STRUCTURE_LE_ACC)
1128 		vmem_free(little_endian_arena, (void *)saddr[-2], saddr[-1]);
1129 	else
1130 		vmem_free(big_endian_arena, (void *)saddr[-2], saddr[-1]);
1131 }
1132 
1133 int
1134 i_ddi_mem_alloc(dev_info_t *dip, ddi_dma_attr_t *attr,
1135     size_t length, int cansleep, int flags,
1136     ddi_device_acc_attr_t *accattrp,
1137     caddr_t *kaddrp, size_t *real_length, ddi_acc_hdl_t *handlep)
1138 {
1139 	caddr_t a;
1140 	int iomin, align, streaming;
1141 	uint_t endian_flags = DDI_NEVERSWAP_ACC;
1142 
1143 #if defined(lint)
1144 	*handlep = *handlep;
1145 #endif
1146 
1147 	/*
1148 	 * Check legality of arguments
1149 	 */
1150 	if (length == 0 || kaddrp == NULL || attr == NULL) {
1151 		return (DDI_FAILURE);
1152 	}
1153 
1154 	if (attr->dma_attr_minxfer == 0 || attr->dma_attr_align == 0 ||
1155 	    (attr->dma_attr_align & (attr->dma_attr_align - 1)) ||
1156 	    (attr->dma_attr_minxfer & (attr->dma_attr_minxfer - 1))) {
1157 		return (DDI_FAILURE);
1158 	}
1159 
1160 	/*
1161 	 * check if a streaming sequential xfer is requested.
1162 	 */
1163 	streaming = (flags & DDI_DMA_STREAMING) ? 1 : 0;
1164 
1165 	/*
1166 	 * Drivers for 64-bit capable SBus devices will encode
1167 	 * the burtsizes for 64-bit xfers in the upper 16-bits.
1168 	 * For DMA alignment, we use the most restrictive
1169 	 * alignment of 32-bit and 64-bit xfers.
1170 	 */
1171 	iomin = (attr->dma_attr_burstsizes & 0xffff) |
1172 	    ((attr->dma_attr_burstsizes >> 16) & 0xffff);
1173 	/*
1174 	 * If a driver set burtsizes to 0, we give him byte alignment.
1175 	 * Otherwise align at the burtsizes boundary.
1176 	 */
1177 	if (iomin == 0)
1178 		iomin = 1;
1179 	else
1180 		iomin = 1 << (ddi_fls(iomin) - 1);
1181 	iomin = maxbit(iomin, attr->dma_attr_minxfer);
1182 	iomin = maxbit(iomin, attr->dma_attr_align);
1183 	iomin = ddi_iomin(dip, iomin, streaming);
1184 	if (iomin == 0)
1185 		return (DDI_FAILURE);
1186 
1187 	ASSERT((iomin & (iomin - 1)) == 0);
1188 	ASSERT(iomin >= attr->dma_attr_minxfer);
1189 	ASSERT(iomin >= attr->dma_attr_align);
1190 
1191 	length = P2ROUNDUP(length, iomin);
1192 	align = iomin;
1193 
1194 	if (accattrp != NULL)
1195 		endian_flags = accattrp->devacc_attr_endian_flags;
1196 
1197 	a = kalloca(length, align, cansleep, endian_flags);
1198 	if ((*kaddrp = a) == 0) {
1199 		return (DDI_FAILURE);
1200 	} else {
1201 		if (real_length) {
1202 			*real_length = length;
1203 		}
1204 		if (handlep) {
1205 			/*
1206 			 * assign handle information
1207 			 */
1208 			impl_acc_hdl_init(handlep);
1209 		}
1210 		return (DDI_SUCCESS);
1211 	}
1212 }
1213 
1214 /*
1215  * covert old DMA limits structure to DMA attribute structure
1216  * and continue
1217  */
1218 int
1219 i_ddi_mem_alloc_lim(dev_info_t *dip, ddi_dma_lim_t *limits,
1220     size_t length, int cansleep, int streaming,
1221     ddi_device_acc_attr_t *accattrp, caddr_t *kaddrp,
1222     uint_t *real_length, ddi_acc_hdl_t *ap)
1223 {
1224 	ddi_dma_attr_t dma_attr, *attrp;
1225 	size_t rlen;
1226 	int ret;
1227 
1228 	ASSERT(limits);
1229 	attrp = &dma_attr;
1230 	attrp->dma_attr_version = DMA_ATTR_V0;
1231 	attrp->dma_attr_addr_lo = (uint64_t)limits->dlim_addr_lo;
1232 	attrp->dma_attr_addr_hi = (uint64_t)limits->dlim_addr_hi;
1233 	attrp->dma_attr_count_max = (uint64_t)-1;
1234 	attrp->dma_attr_align = 1;
1235 	attrp->dma_attr_burstsizes = (uint_t)limits->dlim_burstsizes;
1236 	attrp->dma_attr_minxfer = (uint32_t)limits->dlim_minxfer;
1237 	attrp->dma_attr_maxxfer = (uint64_t)-1;
1238 	attrp->dma_attr_seg = (uint64_t)limits->dlim_cntr_max;
1239 	attrp->dma_attr_sgllen = 1;
1240 	attrp->dma_attr_granular = 1;
1241 	attrp->dma_attr_flags = 0;
1242 
1243 	ret = i_ddi_mem_alloc(dip, attrp, length, cansleep, streaming,
1244 	    accattrp, kaddrp, &rlen, ap);
1245 	if (ret == DDI_SUCCESS) {
1246 		if (real_length)
1247 			*real_length = (uint_t)rlen;
1248 	}
1249 	return (ret);
1250 }
1251 
1252 /* ARGSUSED */
1253 void
1254 i_ddi_mem_free(caddr_t kaddr, ddi_acc_hdl_t *ap)
1255 {
1256 	kfreea(kaddr);
1257 }
1258 
1259 /*
1260  * SECTION: DDI Data Access
1261  */
1262 
1263 static uintptr_t impl_acc_hdl_id = 0;
1264 
1265 /*
1266  * access handle allocator
1267  */
1268 ddi_acc_hdl_t *
1269 impl_acc_hdl_get(ddi_acc_handle_t hdl)
1270 {
1271 	/*
1272 	 * Extract the access handle address from the DDI implemented
1273 	 * access handle
1274 	 */
1275 	return (&((ddi_acc_impl_t *)hdl)->ahi_common);
1276 }
1277 
1278 ddi_acc_handle_t
1279 impl_acc_hdl_alloc(int (*waitfp)(caddr_t), caddr_t arg)
1280 {
1281 	ddi_acc_impl_t *hp;
1282 	on_trap_data_t *otp;
1283 	int sleepflag;
1284 
1285 	sleepflag = ((waitfp == (int (*)())KM_SLEEP) ? KM_SLEEP : KM_NOSLEEP);
1286 
1287 	/*
1288 	 * Allocate and initialize the data access handle and error status.
1289 	 */
1290 	if ((hp = kmem_zalloc(sizeof (ddi_acc_impl_t), sleepflag)) == NULL)
1291 		goto fail;
1292 	if ((hp->ahi_err = (ndi_err_t *)kmem_zalloc(
1293 	    sizeof (ndi_err_t), sleepflag)) == NULL) {
1294 		kmem_free(hp, sizeof (ddi_acc_impl_t));
1295 		goto fail;
1296 	}
1297 	if ((otp = (on_trap_data_t *)kmem_zalloc(
1298 	    sizeof (on_trap_data_t), sleepflag)) == NULL) {
1299 		kmem_free(hp->ahi_err, sizeof (ndi_err_t));
1300 		kmem_free(hp, sizeof (ddi_acc_impl_t));
1301 		goto fail;
1302 	}
1303 	hp->ahi_err->err_ontrap = otp;
1304 	hp->ahi_common.ah_platform_private = (void *)hp;
1305 
1306 	return ((ddi_acc_handle_t)hp);
1307 fail:
1308 	if ((waitfp != (int (*)())KM_SLEEP) &&
1309 	    (waitfp != (int (*)())KM_NOSLEEP))
1310 		ddi_set_callback(waitfp, arg, &impl_acc_hdl_id);
1311 	return (NULL);
1312 }
1313 
1314 void
1315 impl_acc_hdl_free(ddi_acc_handle_t handle)
1316 {
1317 	ddi_acc_impl_t *hp;
1318 
1319 	/*
1320 	 * The supplied (ddi_acc_handle_t) is actually a (ddi_acc_impl_t *),
1321 	 * because that's what we allocated in impl_acc_hdl_alloc() above.
1322 	 */
1323 	hp = (ddi_acc_impl_t *)handle;
1324 	if (hp) {
1325 		kmem_free(hp->ahi_err->err_ontrap, sizeof (on_trap_data_t));
1326 		kmem_free(hp->ahi_err, sizeof (ndi_err_t));
1327 		kmem_free(hp, sizeof (ddi_acc_impl_t));
1328 		if (impl_acc_hdl_id)
1329 			ddi_run_callback(&impl_acc_hdl_id);
1330 	}
1331 }
1332 
1333 #define	PCI_GET_MP_PFN(mp, page_no)	((mp)->dmai_ndvmapages == 1 ? \
1334 	(pfn_t)(mp)->dmai_iopte:(((pfn_t *)(mp)->dmai_iopte)[page_no]))
1335 
1336 /*
1337  * Function called after a dma fault occurred to find out whether the
1338  * fault address is associated with a driver that is able to handle faults
1339  * and recover from faults.
1340  */
1341 /* ARGSUSED */
1342 int
1343 impl_dma_check(dev_info_t *dip, const void *handle, const void *addr,
1344     const void *not_used)
1345 {
1346 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
1347 	pfn_t fault_pfn = mmu_btop(*(uint64_t *)addr);
1348 	pfn_t comp_pfn;
1349 
1350 	/*
1351 	 * The driver has to set DDI_DMA_FLAGERR to recover from dma faults.
1352 	 */
1353 	int page;
1354 
1355 	ASSERT(mp);
1356 	for (page = 0; page < mp->dmai_ndvmapages; page++) {
1357 		comp_pfn = PCI_GET_MP_PFN(mp, page);
1358 		if (fault_pfn == comp_pfn)
1359 			return (DDI_FM_NONFATAL);
1360 	}
1361 	return (DDI_FM_UNKNOWN);
1362 }
1363 
1364 /*
1365  * Function used to check if a given access handle owns the failing address.
1366  * Called by ndi_fmc_error, when we detect a PIO error.
1367  */
1368 /* ARGSUSED */
1369 static int
1370 impl_acc_check(dev_info_t *dip, const void *handle, const void *addr,
1371     const void *not_used)
1372 {
1373 	pfn_t pfn, fault_pfn;
1374 	ddi_acc_hdl_t *hp;
1375 
1376 	hp = impl_acc_hdl_get((ddi_acc_handle_t)handle);
1377 
1378 	ASSERT(hp);
1379 
1380 	if (addr != NULL) {
1381 		pfn = hp->ah_pfn;
1382 		fault_pfn = mmu_btop(*(uint64_t *)addr);
1383 		if (fault_pfn >= pfn && fault_pfn < (pfn + hp->ah_pnum))
1384 			return (DDI_FM_NONFATAL);
1385 	}
1386 	return (DDI_FM_UNKNOWN);
1387 }
1388 
1389 void
1390 impl_acc_err_init(ddi_acc_hdl_t *handlep)
1391 {
1392 	int fmcap;
1393 	ndi_err_t *errp;
1394 	on_trap_data_t *otp;
1395 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handlep;
1396 
1397 	fmcap = ddi_fm_capable(handlep->ah_dip);
1398 
1399 	if (handlep->ah_acc.devacc_attr_version < DDI_DEVICE_ATTR_V1 ||
1400 	    !DDI_FM_ACC_ERR_CAP(fmcap)) {
1401 		handlep->ah_acc.devacc_attr_access = DDI_DEFAULT_ACC;
1402 	} else if (DDI_FM_ACC_ERR_CAP(fmcap)) {
1403 		if (handlep->ah_acc.devacc_attr_access == DDI_DEFAULT_ACC) {
1404 			i_ddi_drv_ereport_post(handlep->ah_dip, DVR_EFMCAP,
1405 			    NULL, DDI_NOSLEEP);
1406 		} else {
1407 			errp = hp->ahi_err;
1408 			otp = (on_trap_data_t *)errp->err_ontrap;
1409 			otp->ot_handle = (void *)(hp);
1410 			otp->ot_prot = OT_DATA_ACCESS;
1411 			if (handlep->ah_acc.devacc_attr_access ==
1412 			    DDI_CAUTIOUS_ACC)
1413 				otp->ot_trampoline =
1414 				    (uintptr_t)&i_ddi_caut_trampoline;
1415 			else
1416 				otp->ot_trampoline =
1417 				    (uintptr_t)&i_ddi_prot_trampoline;
1418 			errp->err_status = DDI_FM_OK;
1419 			errp->err_expected = DDI_FM_ERR_UNEXPECTED;
1420 			errp->err_cf = impl_acc_check;
1421 		}
1422 	}
1423 }
1424 
1425 void
1426 impl_acc_hdl_init(ddi_acc_hdl_t *handlep)
1427 {
1428 	ddi_acc_impl_t *hp;
1429 
1430 	ASSERT(handlep);
1431 
1432 	hp = (ddi_acc_impl_t *)handlep;
1433 
1434 	/*
1435 	 * check for SW byte-swapping
1436 	 */
1437 	hp->ahi_get8 = i_ddi_get8;
1438 	hp->ahi_put8 = i_ddi_put8;
1439 	hp->ahi_rep_get8 = i_ddi_rep_get8;
1440 	hp->ahi_rep_put8 = i_ddi_rep_put8;
1441 	if (handlep->ah_acc.devacc_attr_endian_flags & DDI_STRUCTURE_LE_ACC) {
1442 		hp->ahi_get16 = i_ddi_swap_get16;
1443 		hp->ahi_get32 = i_ddi_swap_get32;
1444 		hp->ahi_get64 = i_ddi_swap_get64;
1445 		hp->ahi_put16 = i_ddi_swap_put16;
1446 		hp->ahi_put32 = i_ddi_swap_put32;
1447 		hp->ahi_put64 = i_ddi_swap_put64;
1448 		hp->ahi_rep_get16 = i_ddi_swap_rep_get16;
1449 		hp->ahi_rep_get32 = i_ddi_swap_rep_get32;
1450 		hp->ahi_rep_get64 = i_ddi_swap_rep_get64;
1451 		hp->ahi_rep_put16 = i_ddi_swap_rep_put16;
1452 		hp->ahi_rep_put32 = i_ddi_swap_rep_put32;
1453 		hp->ahi_rep_put64 = i_ddi_swap_rep_put64;
1454 	} else {
1455 		hp->ahi_get16 = i_ddi_get16;
1456 		hp->ahi_get32 = i_ddi_get32;
1457 		hp->ahi_get64 = i_ddi_get64;
1458 		hp->ahi_put16 = i_ddi_put16;
1459 		hp->ahi_put32 = i_ddi_put32;
1460 		hp->ahi_put64 = i_ddi_put64;
1461 		hp->ahi_rep_get16 = i_ddi_rep_get16;
1462 		hp->ahi_rep_get32 = i_ddi_rep_get32;
1463 		hp->ahi_rep_get64 = i_ddi_rep_get64;
1464 		hp->ahi_rep_put16 = i_ddi_rep_put16;
1465 		hp->ahi_rep_put32 = i_ddi_rep_put32;
1466 		hp->ahi_rep_put64 = i_ddi_rep_put64;
1467 	}
1468 
1469 	/* Legacy fault flags and support */
1470 	hp->ahi_fault_check = i_ddi_acc_fault_check;
1471 	hp->ahi_fault_notify = i_ddi_acc_fault_notify;
1472 	hp->ahi_fault = 0;
1473 	impl_acc_err_init(handlep);
1474 }
1475 
1476 void
1477 i_ddi_acc_set_fault(ddi_acc_handle_t handle)
1478 {
1479 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle;
1480 
1481 	if (!hp->ahi_fault) {
1482 		hp->ahi_fault = 1;
1483 			(*hp->ahi_fault_notify)(hp);
1484 	}
1485 }
1486 
1487 void
1488 i_ddi_acc_clr_fault(ddi_acc_handle_t handle)
1489 {
1490 	ddi_acc_impl_t *hp = (ddi_acc_impl_t *)handle;
1491 
1492 	if (hp->ahi_fault) {
1493 		hp->ahi_fault = 0;
1494 			(*hp->ahi_fault_notify)(hp);
1495 	}
1496 }
1497 
1498 /* ARGSUSED */
1499 void
1500 i_ddi_acc_fault_notify(ddi_acc_impl_t *hp)
1501 {
1502 	/* Default version, does nothing */
1503 }
1504 
1505 /*
1506  * SECTION: Misc functions
1507  */
1508 
1509 /*
1510  * instance wrappers
1511  */
1512 /*ARGSUSED*/
1513 uint_t
1514 impl_assign_instance(dev_info_t *dip)
1515 {
1516 	return ((uint_t)-1);
1517 }
1518 
1519 /*ARGSUSED*/
1520 int
1521 impl_keep_instance(dev_info_t *dip)
1522 {
1523 	return (DDI_FAILURE);
1524 }
1525 
1526 /*ARGSUSED*/
1527 int
1528 impl_free_instance(dev_info_t *dip)
1529 {
1530 	return (DDI_FAILURE);
1531 }
1532 
1533 /*ARGSUSED*/
1534 int
1535 impl_check_cpu(dev_info_t *devi)
1536 {
1537 	return (DDI_SUCCESS);
1538 }
1539 
1540 
1541 static const char *nocopydevs[] = {
1542 	"SUNW,ffb",
1543 	"SUNW,afb",
1544 	NULL
1545 };
1546 
1547 /*
1548  * Perform a copy from a memory mapped device (whose devinfo pointer is devi)
1549  * separately mapped at devaddr in the kernel to a kernel buffer at kaddr.
1550  */
1551 /*ARGSUSED*/
1552 int
1553 e_ddi_copyfromdev(dev_info_t *devi,
1554     off_t off, const void *devaddr, void *kaddr, size_t len)
1555 {
1556 	const char **argv;
1557 
1558 	for (argv = nocopydevs; *argv; argv++)
1559 		if (strcmp(ddi_binding_name(devi), *argv) == 0) {
1560 			bzero(kaddr, len);
1561 			return (0);
1562 		}
1563 
1564 	bcopy(devaddr, kaddr, len);
1565 	return (0);
1566 }
1567 
1568 /*
1569  * Perform a copy to a memory mapped device (whose devinfo pointer is devi)
1570  * separately mapped at devaddr in the kernel from a kernel buffer at kaddr.
1571  */
1572 /*ARGSUSED*/
1573 int
1574 e_ddi_copytodev(dev_info_t *devi,
1575     off_t off, const void *kaddr, void *devaddr, size_t len)
1576 {
1577 	const char **argv;
1578 
1579 	for (argv = nocopydevs; *argv; argv++)
1580 		if (strcmp(ddi_binding_name(devi), *argv) == 0)
1581 			return (1);
1582 
1583 	bcopy(kaddr, devaddr, len);
1584 	return (0);
1585 }
1586 
1587 /*
1588  * Boot Configuration
1589  */
1590 idprom_t idprom;
1591 
1592 /*
1593  * Configure the hardware on the system.
1594  * Called before the rootfs is mounted
1595  */
1596 void
1597 configure(void)
1598 {
1599 	extern void i_ddi_init_root();
1600 
1601 	/* We better have released boot by this time! */
1602 	ASSERT(!bootops);
1603 
1604 	/*
1605 	 * Determine whether or not to use the fpu, V9 SPARC cpus
1606 	 * always have one. Could check for existence of a fp queue,
1607 	 * Ultra I, II and IIa do not have a fp queue.
1608 	 */
1609 	if (fpu_exists)
1610 		fpu_probe();
1611 	else
1612 		cmn_err(CE_CONT, "FPU not in use\n");
1613 
1614 #if 0 /* XXXQ - not necessary for sun4u */
1615 	/*
1616 	 * This following line fixes bugid 1041296; we need to do a
1617 	 * prom_nextnode(0) because this call ALSO patches the DMA+
1618 	 * bug in Campus-B and Phoenix. The prom uncaches the traptable
1619 	 * page as a side-effect of devr_next(0) (which prom_nextnode calls),
1620 	 * so this *must* be executed early on. (XXX This is untrue for sun4u)
1621 	 */
1622 	(void) prom_nextnode((pnode_t)0);
1623 #endif
1624 
1625 	/*
1626 	 * Initialize devices on the machine.
1627 	 * Uses configuration tree built by the PROMs to determine what
1628 	 * is present, and builds a tree of prototype dev_info nodes
1629 	 * corresponding to the hardware which identified itself.
1630 	 */
1631 	i_ddi_init_root();
1632 
1633 #ifdef	DDI_PROP_DEBUG
1634 	(void) ddi_prop_debug(1);	/* Enable property debugging */
1635 #endif	/* DDI_PROP_DEBUG */
1636 }
1637 
1638 /*
1639  * The "status" property indicates the operational status of a device.
1640  * If this property is present, the value is a string indicating the
1641  * status of the device as follows:
1642  *
1643  *	"okay"		operational.
1644  *	"disabled"	not operational, but might become operational.
1645  *	"fail"		not operational because a fault has been detected,
1646  *			and it is unlikely that the device will become
1647  *			operational without repair. no additional details
1648  *			are available.
1649  *	"fail-xxx"	not operational because a fault has been detected,
1650  *			and it is unlikely that the device will become
1651  *			operational without repair. "xxx" is additional
1652  *			human-readable information about the particular
1653  *			fault condition that was detected.
1654  *
1655  * The absence of this property means that the operational status is
1656  * unknown or okay.
1657  *
1658  * This routine checks the status property of the specified device node
1659  * and returns 0 if the operational status indicates failure, and 1 otherwise.
1660  *
1661  * The property may exist on plug-in cards the existed before IEEE 1275-1994.
1662  * And, in that case, the property may not even be a string. So we carefully
1663  * check for the value "fail", in the beginning of the string, noting
1664  * the property length.
1665  */
1666 int
1667 status_okay(int id, char *buf, int buflen)
1668 {
1669 	char status_buf[OBP_MAXPROPNAME];
1670 	char *bufp = buf;
1671 	int len = buflen;
1672 	int proplen;
1673 	static const char *status = "status";
1674 	static const char *fail = "fail";
1675 	size_t fail_len = strlen(fail);
1676 
1677 	/*
1678 	 * Get the proplen ... if it's smaller than "fail",
1679 	 * or doesn't exist ... then we don't care, since
1680 	 * the value can't begin with the char string "fail".
1681 	 *
1682 	 * NB: proplen, if it's a string, includes the NULL in the
1683 	 * the size of the property, and fail_len does not.
1684 	 */
1685 	proplen = prom_getproplen((pnode_t)id, (caddr_t)status);
1686 	if (proplen <= fail_len)	/* nonexistent or uninteresting len */
1687 		return (1);
1688 
1689 	/*
1690 	 * if a buffer was provided, use it
1691 	 */
1692 	if ((buf == (char *)NULL) || (buflen <= 0)) {
1693 		bufp = status_buf;
1694 		len = sizeof (status_buf);
1695 	}
1696 	*bufp = (char)0;
1697 
1698 	/*
1699 	 * Get the property into the buffer, to the extent of the buffer,
1700 	 * and in case the buffer is smaller than the property size,
1701 	 * NULL terminate the buffer. (This handles the case where
1702 	 * a buffer was passed in and the caller wants to print the
1703 	 * value, but the buffer was too small).
1704 	 */
1705 	(void) prom_bounded_getprop((pnode_t)id, (caddr_t)status,
1706 	    (caddr_t)bufp, len);
1707 	*(bufp + len - 1) = (char)0;
1708 
1709 	/*
1710 	 * If the value begins with the char string "fail",
1711 	 * then it means the node is failed. We don't care
1712 	 * about any other values. We assume the node is ok
1713 	 * although it might be 'disabled'.
1714 	 */
1715 	if (strncmp(bufp, fail, fail_len) == 0)
1716 		return (0);
1717 
1718 	return (1);
1719 }
1720 
1721 
1722 /*
1723  * We set the cpu type from the idprom, if we can.
1724  * Note that we just read out the contents of it, for the most part.
1725  */
1726 void
1727 setcputype(void)
1728 {
1729 	/*
1730 	 * We cache the idprom info early on so that we don't
1731 	 * rummage through the NVRAM unnecessarily later.
1732 	 */
1733 	(void) prom_getidprom((caddr_t)&idprom, sizeof (idprom));
1734 }
1735 
1736 /*
1737  *  Here is where we actually infer meanings to the members of idprom_t
1738  */
1739 void
1740 parse_idprom(void)
1741 {
1742 	if (idprom.id_format == IDFORM_1) {
1743 		uint_t i;
1744 
1745 		(void) localetheraddr((struct ether_addr *)idprom.id_ether,
1746 		    (struct ether_addr *)NULL);
1747 
1748 		i = idprom.id_machine << 24;
1749 		i = i + idprom.id_serial;
1750 		numtos((ulong_t)i, hw_serial);
1751 	} else
1752 		prom_printf("Invalid format code in IDprom.\n");
1753 }
1754 
1755 /*
1756  * Allow for implementation specific correction of PROM property values.
1757  */
1758 /*ARGSUSED*/
1759 void
1760 impl_fix_props(dev_info_t *dip, dev_info_t *ch_dip, char *name, int len,
1761     caddr_t buffer)
1762 {
1763 	/*
1764 	 * There are no adjustments needed in this implementation.
1765 	 */
1766 }
1767 
1768 /*
1769  * The following functions ready a cautious request to go up to the nexus
1770  * driver.  It is up to the nexus driver to decide how to process the request.
1771  * It may choose to call i_ddi_do_caut_get/put in this file, or do it
1772  * differently.
1773  */
1774 
1775 static void
1776 i_ddi_caut_getput_ctlops(
1777     ddi_acc_impl_t *hp, uint64_t host_addr, uint64_t dev_addr, size_t size,
1778     size_t repcount, uint_t flags, ddi_ctl_enum_t cmd)
1779 {
1780 	peekpoke_ctlops_t	cautacc_ctlops_arg;
1781 
1782 	cautacc_ctlops_arg.size = size;
1783 	cautacc_ctlops_arg.dev_addr = dev_addr;
1784 	cautacc_ctlops_arg.host_addr = host_addr;
1785 	cautacc_ctlops_arg.handle = (ddi_acc_handle_t)hp;
1786 	cautacc_ctlops_arg.repcount = repcount;
1787 	cautacc_ctlops_arg.flags = flags;
1788 
1789 	(void) ddi_ctlops(hp->ahi_common.ah_dip, hp->ahi_common.ah_dip, cmd,
1790 	    &cautacc_ctlops_arg, NULL);
1791 }
1792 
1793 uint8_t
1794 i_ddi_caut_get8(ddi_acc_impl_t *hp, uint8_t *addr)
1795 {
1796 	uint8_t value;
1797 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1798 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_PEEK);
1799 
1800 	return (value);
1801 }
1802 
1803 uint16_t
1804 i_ddi_caut_get16(ddi_acc_impl_t *hp, uint16_t *addr)
1805 {
1806 	uint16_t value;
1807 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1808 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_PEEK);
1809 
1810 	return (value);
1811 }
1812 
1813 uint32_t
1814 i_ddi_caut_get32(ddi_acc_impl_t *hp, uint32_t *addr)
1815 {
1816 	uint32_t value;
1817 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1818 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_PEEK);
1819 
1820 	return (value);
1821 }
1822 
1823 uint64_t
1824 i_ddi_caut_get64(ddi_acc_impl_t *hp, uint64_t *addr)
1825 {
1826 	uint64_t value;
1827 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1828 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_PEEK);
1829 
1830 	return (value);
1831 }
1832 
1833 void
1834 i_ddi_caut_put8(ddi_acc_impl_t *hp, uint8_t *addr, uint8_t value)
1835 {
1836 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1837 	    sizeof (uint8_t), 1, 0, DDI_CTLOPS_POKE);
1838 }
1839 
1840 void
1841 i_ddi_caut_put16(ddi_acc_impl_t *hp, uint16_t *addr, uint16_t value)
1842 {
1843 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1844 	    sizeof (uint16_t), 1, 0, DDI_CTLOPS_POKE);
1845 }
1846 
1847 void
1848 i_ddi_caut_put32(ddi_acc_impl_t *hp, uint32_t *addr, uint32_t value)
1849 {
1850 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1851 	    sizeof (uint32_t), 1, 0, DDI_CTLOPS_POKE);
1852 }
1853 
1854 void
1855 i_ddi_caut_put64(ddi_acc_impl_t *hp, uint64_t *addr, uint64_t value)
1856 {
1857 	i_ddi_caut_getput_ctlops(hp, (uint64_t)&value, (uint64_t)addr,
1858 	    sizeof (uint64_t), 1, 0, DDI_CTLOPS_POKE);
1859 }
1860 
1861 void
1862 i_ddi_caut_rep_get8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
1863 	size_t repcount, uint_t flags)
1864 {
1865 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1866 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_PEEK);
1867 }
1868 
1869 void
1870 i_ddi_caut_rep_get16(ddi_acc_impl_t *hp, uint16_t *host_addr,
1871     uint16_t *dev_addr, size_t repcount, uint_t flags)
1872 {
1873 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1874 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_PEEK);
1875 }
1876 
1877 void
1878 i_ddi_caut_rep_get32(ddi_acc_impl_t *hp, uint32_t *host_addr,
1879     uint32_t *dev_addr, size_t repcount, uint_t flags)
1880 {
1881 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1882 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_PEEK);
1883 }
1884 
1885 void
1886 i_ddi_caut_rep_get64(ddi_acc_impl_t *hp, uint64_t *host_addr,
1887     uint64_t *dev_addr, size_t repcount, uint_t flags)
1888 {
1889 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1890 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_PEEK);
1891 }
1892 
1893 void
1894 i_ddi_caut_rep_put8(ddi_acc_impl_t *hp, uint8_t *host_addr, uint8_t *dev_addr,
1895 	size_t repcount, uint_t flags)
1896 {
1897 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1898 	    sizeof (uint8_t), repcount, flags, DDI_CTLOPS_POKE);
1899 }
1900 
1901 void
1902 i_ddi_caut_rep_put16(ddi_acc_impl_t *hp, uint16_t *host_addr,
1903     uint16_t *dev_addr, size_t repcount, uint_t flags)
1904 {
1905 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1906 	    sizeof (uint16_t), repcount, flags, DDI_CTLOPS_POKE);
1907 }
1908 
1909 void
1910 i_ddi_caut_rep_put32(ddi_acc_impl_t *hp, uint32_t *host_addr,
1911     uint32_t *dev_addr, size_t repcount, uint_t flags)
1912 {
1913 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1914 	    sizeof (uint32_t), repcount, flags, DDI_CTLOPS_POKE);
1915 }
1916 
1917 void
1918 i_ddi_caut_rep_put64(ddi_acc_impl_t *hp, uint64_t *host_addr,
1919     uint64_t *dev_addr, size_t repcount, uint_t flags)
1920 {
1921 	i_ddi_caut_getput_ctlops(hp, (uint64_t)host_addr, (uint64_t)dev_addr,
1922 	    sizeof (uint64_t), repcount, flags, DDI_CTLOPS_POKE);
1923 }
1924 
1925 /*
1926  * This is called only to process peek/poke when the DIP is NULL.
1927  * Assume that this is for memory, as nexi take care of device safe accesses.
1928  */
1929 int
1930 peekpoke_mem(ddi_ctl_enum_t cmd, peekpoke_ctlops_t *in_args)
1931 {
1932 	int err = DDI_SUCCESS;
1933 	on_trap_data_t otd;
1934 
1935 	/* Set up protected environment. */
1936 	if (!on_trap(&otd, OT_DATA_ACCESS)) {
1937 		uintptr_t tramp = otd.ot_trampoline;
1938 
1939 		if (cmd == DDI_CTLOPS_POKE) {
1940 			otd.ot_trampoline = (uintptr_t)&poke_fault;
1941 			err = do_poke(in_args->size, (void *)in_args->dev_addr,
1942 			    (void *)in_args->host_addr);
1943 		} else {
1944 			otd.ot_trampoline = (uintptr_t)&peek_fault;
1945 			err = do_peek(in_args->size, (void *)in_args->dev_addr,
1946 			    (void *)in_args->host_addr);
1947 		}
1948 		otd.ot_trampoline = tramp;
1949 	} else
1950 		err = DDI_FAILURE;
1951 
1952 	/* Take down protected environment. */
1953 	no_trap();
1954 
1955 	return (err);
1956 }
1957 
1958 /*
1959  * Platform independent DR routines
1960  */
1961 
1962 static int
1963 ndi2errno(int n)
1964 {
1965 	int err = 0;
1966 
1967 	switch (n) {
1968 		case NDI_NOMEM:
1969 			err = ENOMEM;
1970 			break;
1971 		case NDI_BUSY:
1972 			err = EBUSY;
1973 			break;
1974 		case NDI_FAULT:
1975 			err = EFAULT;
1976 			break;
1977 		case NDI_FAILURE:
1978 			err = EIO;
1979 			break;
1980 		case NDI_SUCCESS:
1981 			break;
1982 		case NDI_BADHANDLE:
1983 		default:
1984 			err = EINVAL;
1985 			break;
1986 	}
1987 	return (err);
1988 }
1989 
1990 /*
1991  * Prom tree node list
1992  */
1993 struct ptnode {
1994 	pnode_t		nodeid;
1995 	struct ptnode	*next;
1996 };
1997 
1998 /*
1999  * Prom tree walk arg
2000  */
2001 struct pta {
2002 	dev_info_t	*pdip;
2003 	devi_branch_t	*bp;
2004 	uint_t		flags;
2005 	dev_info_t	*fdip;
2006 	struct ptnode	*head;
2007 };
2008 
2009 static void
2010 visit_node(pnode_t nodeid, struct pta *ap)
2011 {
2012 	struct ptnode	**nextp;
2013 	int		(*select)(pnode_t, void *, uint_t);
2014 
2015 	ASSERT(nodeid != OBP_NONODE && nodeid != OBP_BADNODE);
2016 
2017 	select = ap->bp->create.prom_branch_select;
2018 
2019 	ASSERT(select);
2020 
2021 	if (select(nodeid, ap->bp->arg, 0) == DDI_SUCCESS) {
2022 
2023 		for (nextp = &ap->head; *nextp; nextp = &(*nextp)->next)
2024 			;
2025 
2026 		*nextp = kmem_zalloc(sizeof (struct ptnode), KM_SLEEP);
2027 
2028 		(*nextp)->nodeid = nodeid;
2029 	}
2030 
2031 	if ((ap->flags & DEVI_BRANCH_CHILD) == DEVI_BRANCH_CHILD)
2032 		return;
2033 
2034 	nodeid = prom_childnode(nodeid);
2035 	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
2036 		visit_node(nodeid, ap);
2037 		nodeid = prom_nextnode(nodeid);
2038 	}
2039 }
2040 
2041 /*
2042  * NOTE: The caller of this function must check for device contracts
2043  * or LDI callbacks against this dip before setting the dip offline.
2044  */
2045 static int
2046 set_infant_dip_offline(dev_info_t *dip, void *arg)
2047 {
2048 	char	*path = (char *)arg;
2049 
2050 	ASSERT(dip);
2051 	ASSERT(arg);
2052 
2053 	if (i_ddi_node_state(dip) >= DS_ATTACHED) {
2054 		(void) ddi_pathname(dip, path);
2055 		cmn_err(CE_WARN, "Attempt to set offline flag on attached "
2056 		    "node: %s", path);
2057 		return (DDI_FAILURE);
2058 	}
2059 
2060 	mutex_enter(&(DEVI(dip)->devi_lock));
2061 	if (!DEVI_IS_DEVICE_OFFLINE(dip))
2062 		DEVI_SET_DEVICE_OFFLINE(dip);
2063 	mutex_exit(&(DEVI(dip)->devi_lock));
2064 
2065 	return (DDI_SUCCESS);
2066 }
2067 
2068 typedef struct result {
2069 	char	*path;
2070 	int	result;
2071 } result_t;
2072 
2073 static int
2074 dip_set_offline(dev_info_t *dip, void *arg)
2075 {
2076 	int end;
2077 	result_t *resp = (result_t *)arg;
2078 
2079 	ASSERT(dip);
2080 	ASSERT(resp);
2081 
2082 	/*
2083 	 * We stop the walk if e_ddi_offline_notify() returns
2084 	 * failure, because this implies that one or more consumers
2085 	 * (either LDI or contract based) has blocked the offline.
2086 	 * So there is no point in conitnuing the walk
2087 	 */
2088 	if (e_ddi_offline_notify(dip) == DDI_FAILURE) {
2089 		resp->result = DDI_FAILURE;
2090 		return (DDI_WALK_TERMINATE);
2091 	}
2092 
2093 	/*
2094 	 * If set_infant_dip_offline() returns failure, it implies
2095 	 * that we failed to set a particular dip offline. This
2096 	 * does not imply that the offline as a whole should fail.
2097 	 * We want to do the best we can, so we continue the walk.
2098 	 */
2099 	if (set_infant_dip_offline(dip, resp->path) == DDI_SUCCESS)
2100 		end = DDI_SUCCESS;
2101 	else
2102 		end = DDI_FAILURE;
2103 
2104 	e_ddi_offline_finalize(dip, end);
2105 
2106 	return (DDI_WALK_CONTINUE);
2107 }
2108 
2109 /*
2110  * The call to e_ddi_offline_notify() exists for the
2111  * unlikely error case that a branch we are trying to
2112  * create already exists and has device contracts or LDI
2113  * event callbacks against it.
2114  *
2115  * We allow create to succeed for such branches only if
2116  * no constraints block the offline.
2117  */
2118 static int
2119 branch_set_offline(dev_info_t *dip, char *path)
2120 {
2121 	int		circ;
2122 	int		end;
2123 	result_t	res;
2124 
2125 
2126 	if (e_ddi_offline_notify(dip) == DDI_FAILURE) {
2127 		return (DDI_FAILURE);
2128 	}
2129 
2130 	if (set_infant_dip_offline(dip, path) == DDI_SUCCESS)
2131 		end = DDI_SUCCESS;
2132 	else
2133 		end = DDI_FAILURE;
2134 
2135 	e_ddi_offline_finalize(dip, end);
2136 
2137 	if (end == DDI_FAILURE)
2138 		return (DDI_FAILURE);
2139 
2140 	res.result = DDI_SUCCESS;
2141 	res.path = path;
2142 
2143 	ndi_devi_enter(dip, &circ);
2144 	ddi_walk_devs(ddi_get_child(dip), dip_set_offline, &res);
2145 	ndi_devi_exit(dip, circ);
2146 
2147 	return (res.result);
2148 }
2149 
2150 /*ARGSUSED*/
2151 static int
2152 create_prom_branch(void *arg, int has_changed)
2153 {
2154 	int		circ;
2155 	int		exists, rv;
2156 	pnode_t		nodeid;
2157 	struct ptnode	*tnp;
2158 	dev_info_t	*dip;
2159 	struct pta	*ap = arg;
2160 	devi_branch_t	*bp;
2161 	char		*path;
2162 
2163 	ASSERT(ap);
2164 	ASSERT(ap->fdip == NULL);
2165 	ASSERT(ap->pdip && ndi_dev_is_prom_node(ap->pdip));
2166 
2167 	bp = ap->bp;
2168 
2169 	nodeid = ddi_get_nodeid(ap->pdip);
2170 	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE) {
2171 		cmn_err(CE_WARN, "create_prom_branch: invalid "
2172 		    "nodeid: 0x%x", nodeid);
2173 		return (EINVAL);
2174 	}
2175 
2176 	ap->head = NULL;
2177 
2178 	nodeid = prom_childnode(nodeid);
2179 	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
2180 		visit_node(nodeid, ap);
2181 		nodeid = prom_nextnode(nodeid);
2182 	}
2183 
2184 	if (ap->head == NULL)
2185 		return (ENODEV);
2186 
2187 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2188 	rv = 0;
2189 	while ((tnp = ap->head) != NULL) {
2190 		ap->head = tnp->next;
2191 
2192 		ndi_devi_enter(ap->pdip, &circ);
2193 
2194 		/*
2195 		 * Check if the branch already exists.
2196 		 */
2197 		exists = 0;
2198 		dip = e_ddi_nodeid_to_dip(tnp->nodeid);
2199 		if (dip != NULL) {
2200 			exists = 1;
2201 
2202 			/* Parent is held busy, so release hold */
2203 			ndi_rele_devi(dip);
2204 #ifdef	DEBUG
2205 			cmn_err(CE_WARN, "create_prom_branch: dip(%p) exists"
2206 			    " for nodeid 0x%x", (void *)dip, tnp->nodeid);
2207 #endif
2208 		} else {
2209 			dip = i_ddi_create_branch(ap->pdip, tnp->nodeid);
2210 		}
2211 
2212 		kmem_free(tnp, sizeof (struct ptnode));
2213 
2214 		/*
2215 		 * Hold the branch if it is not already held
2216 		 */
2217 		if (dip && !exists) {
2218 			e_ddi_branch_hold(dip);
2219 		}
2220 
2221 		ASSERT(dip == NULL || e_ddi_branch_held(dip));
2222 
2223 		/*
2224 		 * Set all dips in the newly created branch offline so that
2225 		 * only a "configure" operation can attach
2226 		 * the branch
2227 		 */
2228 		if (dip == NULL || branch_set_offline(dip, path)
2229 		    == DDI_FAILURE) {
2230 			ndi_devi_exit(ap->pdip, circ);
2231 			rv = EIO;
2232 			continue;
2233 		}
2234 
2235 		ASSERT(ddi_get_parent(dip) == ap->pdip);
2236 
2237 		ndi_devi_exit(ap->pdip, circ);
2238 
2239 		if (ap->flags & DEVI_BRANCH_CONFIGURE) {
2240 			int error = e_ddi_branch_configure(dip, &ap->fdip, 0);
2241 			if (error && rv == 0)
2242 				rv = error;
2243 		}
2244 
2245 		/*
2246 		 * Invoke devi_branch_callback() (if it exists) only for
2247 		 * newly created branches
2248 		 */
2249 		if (bp->devi_branch_callback && !exists)
2250 			bp->devi_branch_callback(dip, bp->arg, 0);
2251 	}
2252 
2253 	kmem_free(path, MAXPATHLEN);
2254 
2255 	return (rv);
2256 }
2257 
2258 static int
2259 sid_node_create(dev_info_t *pdip, devi_branch_t *bp, dev_info_t **rdipp)
2260 {
2261 	int			rv, circ, len;
2262 	int			i, flags, ret;
2263 	dev_info_t		*dip;
2264 	char			*nbuf;
2265 	char			*path;
2266 	static const char	*noname = "<none>";
2267 
2268 	ASSERT(pdip);
2269 	ASSERT(DEVI_BUSY_OWNED(pdip));
2270 
2271 	flags = 0;
2272 
2273 	/*
2274 	 * Creating the root of a branch ?
2275 	 */
2276 	if (rdipp) {
2277 		*rdipp = NULL;
2278 		flags = DEVI_BRANCH_ROOT;
2279 	}
2280 
2281 	ndi_devi_alloc_sleep(pdip, (char *)noname, DEVI_SID_NODEID, &dip);
2282 	rv = bp->create.sid_branch_create(dip, bp->arg, flags);
2283 
2284 	nbuf = kmem_alloc(OBP_MAXDRVNAME, KM_SLEEP);
2285 
2286 	if (rv == DDI_WALK_ERROR) {
2287 		cmn_err(CE_WARN, "e_ddi_branch_create: Error setting"
2288 		    " properties on devinfo node %p",  (void *)dip);
2289 		goto fail;
2290 	}
2291 
2292 	len = OBP_MAXDRVNAME;
2293 	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
2294 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "name", nbuf, &len)
2295 	    != DDI_PROP_SUCCESS) {
2296 		cmn_err(CE_WARN, "e_ddi_branch_create: devinfo node %p has"
2297 		    "no name property", (void *)dip);
2298 		goto fail;
2299 	}
2300 
2301 	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
2302 	if (ndi_devi_set_nodename(dip, nbuf, 0) != NDI_SUCCESS) {
2303 		cmn_err(CE_WARN, "e_ddi_branch_create: cannot set name (%s)"
2304 		    " for devinfo node %p", nbuf, (void *)dip);
2305 		goto fail;
2306 	}
2307 
2308 	kmem_free(nbuf, OBP_MAXDRVNAME);
2309 
2310 	/*
2311 	 * Ignore bind failures just like boot does
2312 	 */
2313 	(void) ndi_devi_bind_driver(dip, 0);
2314 
2315 	switch (rv) {
2316 	case DDI_WALK_CONTINUE:
2317 	case DDI_WALK_PRUNESIB:
2318 		ndi_devi_enter(dip, &circ);
2319 
2320 		i = DDI_WALK_CONTINUE;
2321 		for (; i == DDI_WALK_CONTINUE; ) {
2322 			i = sid_node_create(dip, bp, NULL);
2323 		}
2324 
2325 		ASSERT(i == DDI_WALK_ERROR || i == DDI_WALK_PRUNESIB);
2326 		if (i == DDI_WALK_ERROR)
2327 			rv = i;
2328 		/*
2329 		 * If PRUNESIB stop creating siblings
2330 		 * of dip's child. Subsequent walk behavior
2331 		 * is determined by rv returned by dip.
2332 		 */
2333 
2334 		ndi_devi_exit(dip, circ);
2335 		break;
2336 	case DDI_WALK_TERMINATE:
2337 		/*
2338 		 * Don't create children and ask our parent
2339 		 * to not create siblings either.
2340 		 */
2341 		rv = DDI_WALK_PRUNESIB;
2342 		break;
2343 	case DDI_WALK_PRUNECHILD:
2344 		/*
2345 		 * Don't create children, but ask parent to continue
2346 		 * with siblings.
2347 		 */
2348 		rv = DDI_WALK_CONTINUE;
2349 		break;
2350 	default:
2351 		ASSERT(0);
2352 		break;
2353 	}
2354 
2355 	if (rdipp)
2356 		*rdipp = dip;
2357 
2358 	/*
2359 	 * Set device offline - only the "configure" op should cause an attach.
2360 	 * Note that it is safe to set the dip offline without checking
2361 	 * for either device contract or layered driver (LDI) based constraints
2362 	 * since there cannot be any contracts or LDI opens of this device.
2363 	 * This is because this node is a newly created dip with the parent busy
2364 	 * held, so no other thread can come in and attach this dip. A dip that
2365 	 * has never been attached cannot have contracts since by definition
2366 	 * a device contract (an agreement between a process and a device minor
2367 	 * node) can only be created against a device that has minor nodes
2368 	 * i.e is attached. Similarly an LDI open will only succeed if the
2369 	 * dip is attached. We assert below that the dip is not attached.
2370 	 */
2371 	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
2372 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2373 	ret = set_infant_dip_offline(dip, path);
2374 	ASSERT(ret == DDI_SUCCESS);
2375 	kmem_free(path, MAXPATHLEN);
2376 
2377 	return (rv);
2378 fail:
2379 	(void) ndi_devi_free(dip);
2380 	kmem_free(nbuf, OBP_MAXDRVNAME);
2381 	return (DDI_WALK_ERROR);
2382 }
2383 
2384 static int
2385 create_sid_branch(
2386 	dev_info_t	*pdip,
2387 	devi_branch_t	*bp,
2388 	dev_info_t	**dipp,
2389 	uint_t		flags)
2390 {
2391 	int		rv = 0, state = DDI_WALK_CONTINUE;
2392 	dev_info_t	*rdip;
2393 
2394 	while (state == DDI_WALK_CONTINUE) {
2395 		int	circ;
2396 
2397 		ndi_devi_enter(pdip, &circ);
2398 
2399 		state = sid_node_create(pdip, bp, &rdip);
2400 		if (rdip == NULL) {
2401 			ndi_devi_exit(pdip, circ);
2402 			ASSERT(state == DDI_WALK_ERROR);
2403 			break;
2404 		}
2405 
2406 		e_ddi_branch_hold(rdip);
2407 
2408 		ndi_devi_exit(pdip, circ);
2409 
2410 		if (flags & DEVI_BRANCH_CONFIGURE) {
2411 			int error = e_ddi_branch_configure(rdip, dipp, 0);
2412 			if (error && rv == 0)
2413 				rv = error;
2414 		}
2415 
2416 		/*
2417 		 * devi_branch_callback() is optional
2418 		 */
2419 		if (bp->devi_branch_callback)
2420 			bp->devi_branch_callback(rdip, bp->arg, 0);
2421 	}
2422 
2423 	ASSERT(state == DDI_WALK_ERROR || state == DDI_WALK_PRUNESIB);
2424 
2425 	return (state == DDI_WALK_ERROR ? EIO : rv);
2426 }
2427 
2428 int
2429 e_ddi_branch_create(
2430 	dev_info_t	*pdip,
2431 	devi_branch_t	*bp,
2432 	dev_info_t	**dipp,
2433 	uint_t		flags)
2434 {
2435 	int prom_devi, sid_devi, error;
2436 
2437 	if (pdip == NULL || bp == NULL || bp->type == 0)
2438 		return (EINVAL);
2439 
2440 	prom_devi = (bp->type == DEVI_BRANCH_PROM) ? 1 : 0;
2441 	sid_devi = (bp->type == DEVI_BRANCH_SID) ? 1 : 0;
2442 
2443 	if (prom_devi && bp->create.prom_branch_select == NULL)
2444 		return (EINVAL);
2445 	else if (sid_devi && bp->create.sid_branch_create == NULL)
2446 		return (EINVAL);
2447 	else if (!prom_devi && !sid_devi)
2448 		return (EINVAL);
2449 
2450 	if (flags & DEVI_BRANCH_EVENT)
2451 		return (EINVAL);
2452 
2453 	if (prom_devi) {
2454 		struct pta pta = {0};
2455 
2456 		pta.pdip = pdip;
2457 		pta.bp = bp;
2458 		pta.flags = flags;
2459 
2460 		error = prom_tree_access(create_prom_branch, &pta, NULL);
2461 
2462 		if (dipp)
2463 			*dipp = pta.fdip;
2464 		else if (pta.fdip)
2465 			ndi_rele_devi(pta.fdip);
2466 	} else {
2467 		error = create_sid_branch(pdip, bp, dipp, flags);
2468 	}
2469 
2470 	return (error);
2471 }
2472 
2473 int
2474 e_ddi_branch_configure(dev_info_t *rdip, dev_info_t **dipp, uint_t flags)
2475 {
2476 	int		circ, rv;
2477 	char		*devnm;
2478 	dev_info_t	*pdip;
2479 
2480 	if (dipp)
2481 		*dipp = NULL;
2482 
2483 	if (rdip == NULL || flags != 0 || (flags & DEVI_BRANCH_EVENT))
2484 		return (EINVAL);
2485 
2486 	pdip = ddi_get_parent(rdip);
2487 
2488 	ndi_devi_enter(pdip, &circ);
2489 
2490 	if (!e_ddi_branch_held(rdip)) {
2491 		ndi_devi_exit(pdip, circ);
2492 		cmn_err(CE_WARN, "e_ddi_branch_configure: "
2493 		    "dip(%p) not held", (void *)rdip);
2494 		return (EINVAL);
2495 	}
2496 
2497 	if (i_ddi_node_state(rdip) < DS_INITIALIZED) {
2498 		/*
2499 		 * First attempt to bind a driver. If we fail, return
2500 		 * success (On some platforms, dips for some device
2501 		 * types (CPUs) may not have a driver)
2502 		 */
2503 		if (ndi_devi_bind_driver(rdip, 0) != NDI_SUCCESS) {
2504 			ndi_devi_exit(pdip, circ);
2505 			return (0);
2506 		}
2507 
2508 		if (ddi_initchild(pdip, rdip) != DDI_SUCCESS) {
2509 			rv = NDI_FAILURE;
2510 			goto out;
2511 		}
2512 	}
2513 
2514 	ASSERT(i_ddi_node_state(rdip) >= DS_INITIALIZED);
2515 
2516 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2517 
2518 	(void) ddi_deviname(rdip, devnm);
2519 
2520 	if ((rv = ndi_devi_config_one(pdip, devnm+1, &rdip,
2521 	    NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
2522 		/* release hold from ndi_devi_config_one() */
2523 		ndi_rele_devi(rdip);
2524 	}
2525 
2526 	kmem_free(devnm, MAXNAMELEN + 1);
2527 out:
2528 	if (rv != NDI_SUCCESS && dipp) {
2529 		ndi_hold_devi(rdip);
2530 		*dipp = rdip;
2531 	}
2532 	ndi_devi_exit(pdip, circ);
2533 	return (ndi2errno(rv));
2534 }
2535 
2536 void
2537 e_ddi_branch_hold(dev_info_t *rdip)
2538 {
2539 	if (e_ddi_branch_held(rdip)) {
2540 		cmn_err(CE_WARN, "e_ddi_branch_hold: branch already held");
2541 		return;
2542 	}
2543 
2544 	mutex_enter(&DEVI(rdip)->devi_lock);
2545 	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) == 0) {
2546 		DEVI(rdip)->devi_flags |= DEVI_BRANCH_HELD;
2547 		DEVI(rdip)->devi_ref++;
2548 	}
2549 	ASSERT(DEVI(rdip)->devi_ref > 0);
2550 	mutex_exit(&DEVI(rdip)->devi_lock);
2551 }
2552 
2553 int
2554 e_ddi_branch_held(dev_info_t *rdip)
2555 {
2556 	int rv = 0;
2557 
2558 	mutex_enter(&DEVI(rdip)->devi_lock);
2559 	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) &&
2560 	    DEVI(rdip)->devi_ref > 0) {
2561 		rv = 1;
2562 	}
2563 	mutex_exit(&DEVI(rdip)->devi_lock);
2564 
2565 	return (rv);
2566 }
2567 void
2568 e_ddi_branch_rele(dev_info_t *rdip)
2569 {
2570 	mutex_enter(&DEVI(rdip)->devi_lock);
2571 	DEVI(rdip)->devi_flags &= ~DEVI_BRANCH_HELD;
2572 	DEVI(rdip)->devi_ref--;
2573 	mutex_exit(&DEVI(rdip)->devi_lock);
2574 }
2575 
2576 int
2577 e_ddi_branch_unconfigure(
2578 	dev_info_t *rdip,
2579 	dev_info_t **dipp,
2580 	uint_t flags)
2581 {
2582 	int	circ, rv;
2583 	int	destroy;
2584 	char	*devnm;
2585 	uint_t	nflags;
2586 	dev_info_t *pdip;
2587 
2588 	if (dipp)
2589 		*dipp = NULL;
2590 
2591 	if (rdip == NULL)
2592 		return (EINVAL);
2593 
2594 	pdip = ddi_get_parent(rdip);
2595 
2596 	ASSERT(pdip);
2597 
2598 	/*
2599 	 * Check if caller holds pdip busy - can cause deadlocks during
2600 	 * devfs_clean()
2601 	 */
2602 	if (DEVI_BUSY_OWNED(pdip)) {
2603 		cmn_err(CE_WARN, "e_ddi_branch_unconfigure: failed: parent"
2604 		    " devinfo node(%p) is busy held", (void *)pdip);
2605 		return (EINVAL);
2606 	}
2607 
2608 	destroy = (flags & DEVI_BRANCH_DESTROY) ? 1 : 0;
2609 
2610 	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
2611 
2612 	ndi_devi_enter(pdip, &circ);
2613 	(void) ddi_deviname(rdip, devnm);
2614 	ndi_devi_exit(pdip, circ);
2615 
2616 	/*
2617 	 * ddi_deviname() returns a component name with / prepended.
2618 	 */
2619 	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
2620 
2621 	ndi_devi_enter(pdip, &circ);
2622 
2623 	/*
2624 	 * Recreate device name as it may have changed state (init/uninit)
2625 	 * when parent busy lock was dropped for devfs_clean()
2626 	 */
2627 	(void) ddi_deviname(rdip, devnm);
2628 
2629 	if (!e_ddi_branch_held(rdip)) {
2630 		kmem_free(devnm, MAXNAMELEN + 1);
2631 		ndi_devi_exit(pdip, circ);
2632 		cmn_err(CE_WARN, "e_ddi_%s_branch: dip(%p) not held",
2633 		    destroy ? "destroy" : "unconfigure", (void *)rdip);
2634 		return (EINVAL);
2635 	}
2636 
2637 	/*
2638 	 * Release hold on the branch. This is ok since we are holding the
2639 	 * parent busy. If rdip is not removed, we must do a hold on the
2640 	 * branch before returning.
2641 	 */
2642 	e_ddi_branch_rele(rdip);
2643 
2644 	nflags = NDI_DEVI_OFFLINE;
2645 	if (destroy || (flags & DEVI_BRANCH_DESTROY)) {
2646 		nflags |= NDI_DEVI_REMOVE;
2647 		destroy = 1;
2648 	} else {
2649 		nflags |= NDI_UNCONFIG;		/* uninit but don't remove */
2650 	}
2651 
2652 	if (flags & DEVI_BRANCH_EVENT)
2653 		nflags |= NDI_POST_EVENT;
2654 
2655 	if (i_ddi_devi_attached(pdip) &&
2656 	    (i_ddi_node_state(rdip) >= DS_INITIALIZED)) {
2657 		rv = ndi_devi_unconfig_one(pdip, devnm+1, dipp, nflags);
2658 	} else {
2659 		rv = e_ddi_devi_unconfig(rdip, dipp, nflags);
2660 		if (rv == NDI_SUCCESS) {
2661 			ASSERT(!destroy || ddi_get_child(rdip) == NULL);
2662 			rv = ndi_devi_offline(rdip, nflags);
2663 		}
2664 	}
2665 
2666 	if (!destroy || rv != NDI_SUCCESS) {
2667 		/* The dip still exists, so do a hold */
2668 		e_ddi_branch_hold(rdip);
2669 	}
2670 out:
2671 	kmem_free(devnm, MAXNAMELEN + 1);
2672 	ndi_devi_exit(pdip, circ);
2673 	return (ndi2errno(rv));
2674 }
2675 
2676 int
2677 e_ddi_branch_destroy(dev_info_t *rdip, dev_info_t **dipp, uint_t flag)
2678 {
2679 	return (e_ddi_branch_unconfigure(rdip, dipp,
2680 	    flag|DEVI_BRANCH_DESTROY));
2681 }
2682 
2683 /*
2684  * Number of chains for hash table
2685  */
2686 #define	NUMCHAINS	17
2687 
2688 /*
2689  * Devinfo busy arg
2690  */
2691 struct devi_busy {
2692 	int dv_total;
2693 	int s_total;
2694 	mod_hash_t *dv_hash;
2695 	mod_hash_t *s_hash;
2696 	int (*callback)(dev_info_t *, void *, uint_t);
2697 	void *arg;
2698 };
2699 
2700 static int
2701 visit_dip(dev_info_t *dip, void *arg)
2702 {
2703 	uintptr_t sbusy, dvbusy, ref;
2704 	struct devi_busy *bsp = arg;
2705 
2706 	ASSERT(bsp->callback);
2707 
2708 	/*
2709 	 * A dip cannot be busy if its reference count is 0
2710 	 */
2711 	if ((ref = e_ddi_devi_holdcnt(dip)) == 0) {
2712 		return (bsp->callback(dip, bsp->arg, 0));
2713 	}
2714 
2715 	if (mod_hash_find(bsp->dv_hash, dip, (mod_hash_val_t *)&dvbusy))
2716 		dvbusy = 0;
2717 
2718 	/*
2719 	 * To catch device opens currently maintained on specfs common snodes.
2720 	 */
2721 	if (mod_hash_find(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
2722 		sbusy = 0;
2723 
2724 #ifdef	DEBUG
2725 	if (ref < sbusy || ref < dvbusy) {
2726 		cmn_err(CE_WARN, "dip(%p): sopen = %lu, dvopen = %lu "
2727 		    "dip ref = %lu\n", (void *)dip, sbusy, dvbusy, ref);
2728 	}
2729 #endif
2730 
2731 	dvbusy = (sbusy > dvbusy) ? sbusy : dvbusy;
2732 
2733 	return (bsp->callback(dip, bsp->arg, dvbusy));
2734 }
2735 
2736 static int
2737 visit_snode(struct snode *sp, void *arg)
2738 {
2739 	uintptr_t sbusy;
2740 	dev_info_t *dip;
2741 	int count;
2742 	struct devi_busy *bsp = arg;
2743 
2744 	ASSERT(sp);
2745 
2746 	/*
2747 	 * The stable lock is held. This prevents
2748 	 * the snode and its associated dip from
2749 	 * going away.
2750 	 */
2751 	dip = NULL;
2752 	count = spec_devi_open_count(sp, &dip);
2753 
2754 	if (count <= 0)
2755 		return (DDI_WALK_CONTINUE);
2756 
2757 	ASSERT(dip);
2758 
2759 	if (mod_hash_remove(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
2760 		sbusy = count;
2761 	else
2762 		sbusy += count;
2763 
2764 	if (mod_hash_insert(bsp->s_hash, dip, (mod_hash_val_t)sbusy)) {
2765 		cmn_err(CE_WARN, "%s: s_hash insert failed: dip=0x%p, "
2766 		    "sbusy = %lu", "e_ddi_branch_referenced",
2767 		    (void *)dip, sbusy);
2768 	}
2769 
2770 	bsp->s_total += count;
2771 
2772 	return (DDI_WALK_CONTINUE);
2773 }
2774 
2775 static void
2776 visit_dvnode(struct dv_node *dv, void *arg)
2777 {
2778 	uintptr_t dvbusy;
2779 	uint_t count;
2780 	struct vnode *vp;
2781 	struct devi_busy *bsp = arg;
2782 
2783 	ASSERT(dv && dv->dv_devi);
2784 
2785 	vp = DVTOV(dv);
2786 
2787 	mutex_enter(&vp->v_lock);
2788 	count = vp->v_count;
2789 	mutex_exit(&vp->v_lock);
2790 
2791 	if (!count)
2792 		return;
2793 
2794 	if (mod_hash_remove(bsp->dv_hash, dv->dv_devi,
2795 	    (mod_hash_val_t *)&dvbusy))
2796 		dvbusy = count;
2797 	else
2798 		dvbusy += count;
2799 
2800 	if (mod_hash_insert(bsp->dv_hash, dv->dv_devi,
2801 	    (mod_hash_val_t)dvbusy)) {
2802 		cmn_err(CE_WARN, "%s: dv_hash insert failed: dip=0x%p, "
2803 		    "dvbusy=%lu", "e_ddi_branch_referenced",
2804 		    (void *)dv->dv_devi, dvbusy);
2805 	}
2806 
2807 	bsp->dv_total += count;
2808 }
2809 
2810 /*
2811  * Returns reference count on success or -1 on failure.
2812  */
2813 int
2814 e_ddi_branch_referenced(
2815 	dev_info_t *rdip,
2816 	int (*callback)(dev_info_t *dip, void *arg, uint_t ref),
2817 	void *arg)
2818 {
2819 	int circ;
2820 	char *path;
2821 	dev_info_t *pdip;
2822 	struct devi_busy bsa = {0};
2823 
2824 	ASSERT(rdip);
2825 
2826 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
2827 
2828 	ndi_hold_devi(rdip);
2829 
2830 	pdip = ddi_get_parent(rdip);
2831 
2832 	ASSERT(pdip);
2833 
2834 	/*
2835 	 * Check if caller holds pdip busy - can cause deadlocks during
2836 	 * devfs_walk()
2837 	 */
2838 	if (!e_ddi_branch_held(rdip) || DEVI_BUSY_OWNED(pdip)) {
2839 		cmn_err(CE_WARN, "e_ddi_branch_referenced: failed: "
2840 		    "devinfo branch(%p) not held or parent busy held",
2841 		    (void *)rdip);
2842 		ndi_rele_devi(rdip);
2843 		kmem_free(path, MAXPATHLEN);
2844 		return (-1);
2845 	}
2846 
2847 	ndi_devi_enter(pdip, &circ);
2848 	(void) ddi_pathname(rdip, path);
2849 	ndi_devi_exit(pdip, circ);
2850 
2851 	bsa.dv_hash = mod_hash_create_ptrhash("dv_node busy hash", NUMCHAINS,
2852 	    mod_hash_null_valdtor, sizeof (struct dev_info));
2853 
2854 	bsa.s_hash = mod_hash_create_ptrhash("snode busy hash", NUMCHAINS,
2855 	    mod_hash_null_valdtor, sizeof (struct snode));
2856 
2857 	if (devfs_walk(path, visit_dvnode, &bsa)) {
2858 		cmn_err(CE_WARN, "e_ddi_branch_referenced: "
2859 		    "devfs walk failed for: %s", path);
2860 		kmem_free(path, MAXPATHLEN);
2861 		bsa.s_total = bsa.dv_total = -1;
2862 		goto out;
2863 	}
2864 
2865 	kmem_free(path, MAXPATHLEN);
2866 
2867 	/*
2868 	 * Walk the snode table to detect device opens, which are currently
2869 	 * maintained on specfs common snodes.
2870 	 */
2871 	spec_snode_walk(visit_snode, &bsa);
2872 
2873 	if (callback == NULL)
2874 		goto out;
2875 
2876 	bsa.callback = callback;
2877 	bsa.arg = arg;
2878 
2879 	if (visit_dip(rdip, &bsa) == DDI_WALK_CONTINUE) {
2880 		ndi_devi_enter(rdip, &circ);
2881 		ddi_walk_devs(ddi_get_child(rdip), visit_dip, &bsa);
2882 		ndi_devi_exit(rdip, circ);
2883 	}
2884 
2885 out:
2886 	ndi_rele_devi(rdip);
2887 	mod_hash_destroy_ptrhash(bsa.s_hash);
2888 	mod_hash_destroy_ptrhash(bsa.dv_hash);
2889 	return (bsa.s_total > bsa.dv_total ? bsa.s_total : bsa.dv_total);
2890 }
2891