xref: /titanic_44/usr/src/uts/common/os/sunddi.c (revision 4165f46543b20d435f7ef0aced6239f7e97b12ff)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/note.h>
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/buf.h>
32 #include <sys/uio.h>
33 #include <sys/cred.h>
34 #include <sys/poll.h>
35 #include <sys/mman.h>
36 #include <sys/kmem.h>
37 #include <sys/model.h>
38 #include <sys/file.h>
39 #include <sys/proc.h>
40 #include <sys/open.h>
41 #include <sys/user.h>
42 #include <sys/t_lock.h>
43 #include <sys/vm.h>
44 #include <sys/stat.h>
45 #include <vm/hat.h>
46 #include <vm/seg.h>
47 #include <vm/seg_vn.h>
48 #include <vm/seg_dev.h>
49 #include <vm/as.h>
50 #include <sys/cmn_err.h>
51 #include <sys/cpuvar.h>
52 #include <sys/debug.h>
53 #include <sys/autoconf.h>
54 #include <sys/sunddi.h>
55 #include <sys/esunddi.h>
56 #include <sys/sunndi.h>
57 #include <sys/kstat.h>
58 #include <sys/conf.h>
59 #include <sys/ddi_impldefs.h>	/* include implementation structure defs */
60 #include <sys/ndi_impldefs.h>	/* include prototypes */
61 #include <sys/ddi_timer.h>
62 #include <sys/hwconf.h>
63 #include <sys/pathname.h>
64 #include <sys/modctl.h>
65 #include <sys/epm.h>
66 #include <sys/devctl.h>
67 #include <sys/callb.h>
68 #include <sys/cladm.h>
69 #include <sys/sysevent.h>
70 #include <sys/dacf_impl.h>
71 #include <sys/ddidevmap.h>
72 #include <sys/bootconf.h>
73 #include <sys/disp.h>
74 #include <sys/atomic.h>
75 #include <sys/promif.h>
76 #include <sys/instance.h>
77 #include <sys/sysevent/eventdefs.h>
78 #include <sys/task.h>
79 #include <sys/project.h>
80 #include <sys/taskq.h>
81 #include <sys/devpolicy.h>
82 #include <sys/ctype.h>
83 #include <net/if.h>
84 #include <sys/rctl.h>
85 #include <sys/zone.h>
86 #include <sys/ddi.h>
87 
88 extern	pri_t	minclsyspri;
89 
90 extern	rctl_hndl_t rc_project_locked_mem;
91 extern	rctl_hndl_t rc_zone_locked_mem;
92 
93 #ifdef DEBUG
94 static int sunddi_debug = 0;
95 #endif /* DEBUG */
96 
97 /* ddi_umem_unlock miscellaneous */
98 
99 static	void	i_ddi_umem_unlock_thread_start(void);
100 
101 static	kmutex_t	ddi_umem_unlock_mutex; /* unlock list mutex */
102 static	kcondvar_t	ddi_umem_unlock_cv; /* unlock list block/unblock */
103 static	kthread_t	*ddi_umem_unlock_thread;
104 /*
105  * The ddi_umem_unlock FIFO list.  NULL head pointer indicates empty list.
106  */
107 static	struct	ddi_umem_cookie *ddi_umem_unlock_head = NULL;
108 static	struct	ddi_umem_cookie *ddi_umem_unlock_tail = NULL;
109 
110 /*
111  * DDI(Sun) Function and flag definitions:
112  */
113 
114 #if defined(__x86)
115 /*
116  * Used to indicate which entries were chosen from a range.
117  */
118 char	*chosen_reg = "chosen-reg";
119 #endif
120 
121 /*
122  * Function used to ring system console bell
123  */
124 void (*ddi_console_bell_func)(clock_t duration);
125 
126 /*
127  * Creating register mappings and handling interrupts:
128  */
129 
130 /*
131  * Generic ddi_map: Call parent to fulfill request...
132  */
133 
134 int
135 ddi_map(dev_info_t *dp, ddi_map_req_t *mp, off_t offset,
136     off_t len, caddr_t *addrp)
137 {
138 	dev_info_t *pdip;
139 
140 	ASSERT(dp);
141 	pdip = (dev_info_t *)DEVI(dp)->devi_parent;
142 	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip,
143 	    dp, mp, offset, len, addrp));
144 }
145 
146 /*
147  * ddi_apply_range: (Called by nexi only.)
148  * Apply ranges in parent node dp, to child regspec rp...
149  */
150 
151 int
152 ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
153 {
154 	return (i_ddi_apply_range(dp, rdip, rp));
155 }
156 
157 int
158 ddi_map_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
159     off_t len)
160 {
161 	ddi_map_req_t mr;
162 #if defined(__x86)
163 	struct {
164 		int	bus;
165 		int	addr;
166 		int	size;
167 	} reg, *reglist;
168 	uint_t	length;
169 	int	rc;
170 
171 	/*
172 	 * get the 'registers' or the 'reg' property.
173 	 * We look up the reg property as an array of
174 	 * int's.
175 	 */
176 	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
177 	    DDI_PROP_DONTPASS, "registers", (int **)&reglist, &length);
178 	if (rc != DDI_PROP_SUCCESS)
179 		rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
180 		    DDI_PROP_DONTPASS, "reg", (int **)&reglist, &length);
181 	if (rc == DDI_PROP_SUCCESS) {
182 		/*
183 		 * point to the required entry.
184 		 */
185 		reg = reglist[rnumber];
186 		reg.addr += offset;
187 		if (len != 0)
188 			reg.size = len;
189 		/*
190 		 * make a new property containing ONLY the required tuple.
191 		 */
192 		if (ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
193 		    chosen_reg, (int *)&reg, (sizeof (reg)/sizeof (int)))
194 		    != DDI_PROP_SUCCESS) {
195 			cmn_err(CE_WARN, "%s%d: cannot create '%s' "
196 			    "property", DEVI(dip)->devi_name,
197 			    DEVI(dip)->devi_instance, chosen_reg);
198 		}
199 		/*
200 		 * free the memory allocated by
201 		 * ddi_prop_lookup_int_array ().
202 		 */
203 		ddi_prop_free((void *)reglist);
204 	}
205 #endif
206 	mr.map_op = DDI_MO_MAP_LOCKED;
207 	mr.map_type = DDI_MT_RNUMBER;
208 	mr.map_obj.rnumber = rnumber;
209 	mr.map_prot = PROT_READ | PROT_WRITE;
210 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
211 	mr.map_handlep = NULL;
212 	mr.map_vers = DDI_MAP_VERSION;
213 
214 	/*
215 	 * Call my parent to map in my regs.
216 	 */
217 
218 	return (ddi_map(dip, &mr, offset, len, kaddrp));
219 }
220 
221 void
222 ddi_unmap_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
223     off_t len)
224 {
225 	ddi_map_req_t mr;
226 
227 	mr.map_op = DDI_MO_UNMAP;
228 	mr.map_type = DDI_MT_RNUMBER;
229 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
230 	mr.map_prot = PROT_READ | PROT_WRITE;	/* who cares? */
231 	mr.map_obj.rnumber = rnumber;
232 	mr.map_handlep = NULL;
233 	mr.map_vers = DDI_MAP_VERSION;
234 
235 	/*
236 	 * Call my parent to unmap my regs.
237 	 */
238 
239 	(void) ddi_map(dip, &mr, offset, len, kaddrp);
240 	*kaddrp = (caddr_t)0;
241 #if defined(__x86)
242 	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, chosen_reg);
243 #endif
244 }
245 
246 int
247 ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
248 	off_t offset, off_t len, caddr_t *vaddrp)
249 {
250 	return (i_ddi_bus_map(dip, rdip, mp, offset, len, vaddrp));
251 }
252 
253 /*
254  * nullbusmap:	The/DDI default bus_map entry point for nexi
255  *		not conforming to the reg/range paradigm (i.e. scsi, etc.)
256  *		with no HAT/MMU layer to be programmed at this level.
257  *
258  *		If the call is to map by rnumber, return an error,
259  *		otherwise pass anything else up the tree to my parent.
260  */
261 int
262 nullbusmap(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
263 	off_t offset, off_t len, caddr_t *vaddrp)
264 {
265 	_NOTE(ARGUNUSED(rdip))
266 	if (mp->map_type == DDI_MT_RNUMBER)
267 		return (DDI_ME_UNSUPPORTED);
268 
269 	return (ddi_map(dip, mp, offset, len, vaddrp));
270 }
271 
272 /*
273  * ddi_rnumber_to_regspec: Not for use by leaf drivers.
274  *			   Only for use by nexi using the reg/range paradigm.
275  */
276 struct regspec *
277 ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
278 {
279 	return (i_ddi_rnumber_to_regspec(dip, rnumber));
280 }
281 
282 
283 /*
284  * Note that we allow the dip to be nil because we may be called
285  * prior even to the instantiation of the devinfo tree itself - all
286  * regular leaf and nexus drivers should always use a non-nil dip!
287  *
288  * We treat peek in a somewhat cavalier fashion .. assuming that we'll
289  * simply get a synchronous fault as soon as we touch a missing address.
290  *
291  * Poke is rather more carefully handled because we might poke to a write
292  * buffer, "succeed", then only find some time later that we got an
293  * asynchronous fault that indicated that the address we were writing to
294  * was not really backed by hardware.
295  */
296 
297 static int
298 i_ddi_peekpoke(dev_info_t *devi, ddi_ctl_enum_t cmd, size_t size,
299     void *addr, void *value_p)
300 {
301 	union {
302 		uint64_t	u64;
303 		uint32_t	u32;
304 		uint16_t	u16;
305 		uint8_t		u8;
306 	} peekpoke_value;
307 
308 	peekpoke_ctlops_t peekpoke_args;
309 	uint64_t dummy_result;
310 	int rval;
311 
312 	/* Note: size is assumed to be correct;  it is not checked. */
313 	peekpoke_args.size = size;
314 	peekpoke_args.dev_addr = (uintptr_t)addr;
315 	peekpoke_args.handle = NULL;
316 	peekpoke_args.repcount = 1;
317 	peekpoke_args.flags = 0;
318 
319 	if (cmd == DDI_CTLOPS_POKE) {
320 		switch (size) {
321 		case sizeof (uint8_t):
322 			peekpoke_value.u8 = *(uint8_t *)value_p;
323 			break;
324 		case sizeof (uint16_t):
325 			peekpoke_value.u16 = *(uint16_t *)value_p;
326 			break;
327 		case sizeof (uint32_t):
328 			peekpoke_value.u32 = *(uint32_t *)value_p;
329 			break;
330 		case sizeof (uint64_t):
331 			peekpoke_value.u64 = *(uint64_t *)value_p;
332 			break;
333 		}
334 	}
335 
336 	peekpoke_args.host_addr = (uintptr_t)&peekpoke_value.u64;
337 
338 	if (devi != NULL)
339 		rval = ddi_ctlops(devi, devi, cmd, &peekpoke_args,
340 		    &dummy_result);
341 	else
342 		rval = peekpoke_mem(cmd, &peekpoke_args);
343 
344 	/*
345 	 * A NULL value_p is permitted by ddi_peek(9F); discard the result.
346 	 */
347 	if ((cmd == DDI_CTLOPS_PEEK) & (value_p != NULL)) {
348 		switch (size) {
349 		case sizeof (uint8_t):
350 			*(uint8_t *)value_p = peekpoke_value.u8;
351 			break;
352 		case sizeof (uint16_t):
353 			*(uint16_t *)value_p = peekpoke_value.u16;
354 			break;
355 		case sizeof (uint32_t):
356 			*(uint32_t *)value_p = peekpoke_value.u32;
357 			break;
358 		case sizeof (uint64_t):
359 			*(uint64_t *)value_p = peekpoke_value.u64;
360 			break;
361 		}
362 	}
363 
364 	return (rval);
365 }
366 
367 /*
368  * Keep ddi_peek() and ddi_poke() in case 3rd parties are calling this.
369  * they shouldn't be, but the 9f manpage kind of pseudo exposes it.
370  */
371 int
372 ddi_peek(dev_info_t *devi, size_t size, void *addr, void *value_p)
373 {
374 	switch (size) {
375 	case sizeof (uint8_t):
376 	case sizeof (uint16_t):
377 	case sizeof (uint32_t):
378 	case sizeof (uint64_t):
379 		break;
380 	default:
381 		return (DDI_FAILURE);
382 	}
383 
384 	return (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, size, addr, value_p));
385 }
386 
387 int
388 ddi_poke(dev_info_t *devi, size_t size, void *addr, void *value_p)
389 {
390 	switch (size) {
391 	case sizeof (uint8_t):
392 	case sizeof (uint16_t):
393 	case sizeof (uint32_t):
394 	case sizeof (uint64_t):
395 		break;
396 	default:
397 		return (DDI_FAILURE);
398 	}
399 
400 	return (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, size, addr, value_p));
401 }
402 
403 int
404 ddi_peek8(dev_info_t *dip, int8_t *addr, int8_t *val_p)
405 {
406 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
407 	    val_p));
408 }
409 
410 int
411 ddi_peek16(dev_info_t *dip, int16_t *addr, int16_t *val_p)
412 {
413 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
414 	    val_p));
415 }
416 
417 int
418 ddi_peek32(dev_info_t *dip, int32_t *addr, int32_t *val_p)
419 {
420 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
421 	    val_p));
422 }
423 
424 int
425 ddi_peek64(dev_info_t *dip, int64_t *addr, int64_t *val_p)
426 {
427 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
428 	    val_p));
429 }
430 
431 
432 /*
433  * We need to separate the old interfaces from the new ones and leave them
434  * in here for a while. Previous versions of the OS defined the new interfaces
435  * to the old interfaces. This way we can fix things up so that we can
436  * eventually remove these interfaces.
437  * e.g. A 3rd party module/driver using ddi_peek8 and built against S10
438  * or earlier will actually have a reference to ddi_peekc in the binary.
439  */
440 #ifdef _ILP32
441 int
442 ddi_peekc(dev_info_t *dip, int8_t *addr, int8_t *val_p)
443 {
444 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
445 	    val_p));
446 }
447 
448 int
449 ddi_peeks(dev_info_t *dip, int16_t *addr, int16_t *val_p)
450 {
451 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
452 	    val_p));
453 }
454 
455 int
456 ddi_peekl(dev_info_t *dip, int32_t *addr, int32_t *val_p)
457 {
458 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
459 	    val_p));
460 }
461 
462 int
463 ddi_peekd(dev_info_t *dip, int64_t *addr, int64_t *val_p)
464 {
465 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
466 	    val_p));
467 }
468 #endif /* _ILP32 */
469 
470 int
471 ddi_poke8(dev_info_t *dip, int8_t *addr, int8_t val)
472 {
473 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
474 }
475 
476 int
477 ddi_poke16(dev_info_t *dip, int16_t *addr, int16_t val)
478 {
479 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
480 }
481 
482 int
483 ddi_poke32(dev_info_t *dip, int32_t *addr, int32_t val)
484 {
485 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
486 }
487 
488 int
489 ddi_poke64(dev_info_t *dip, int64_t *addr, int64_t val)
490 {
491 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
492 }
493 
494 /*
495  * We need to separate the old interfaces from the new ones and leave them
496  * in here for a while. Previous versions of the OS defined the new interfaces
497  * to the old interfaces. This way we can fix things up so that we can
498  * eventually remove these interfaces.
499  * e.g. A 3rd party module/driver using ddi_poke8 and built against S10
500  * or earlier will actually have a reference to ddi_pokec in the binary.
501  */
502 #ifdef _ILP32
503 int
504 ddi_pokec(dev_info_t *dip, int8_t *addr, int8_t val)
505 {
506 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
507 }
508 
509 int
510 ddi_pokes(dev_info_t *dip, int16_t *addr, int16_t val)
511 {
512 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
513 }
514 
515 int
516 ddi_pokel(dev_info_t *dip, int32_t *addr, int32_t val)
517 {
518 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
519 }
520 
521 int
522 ddi_poked(dev_info_t *dip, int64_t *addr, int64_t val)
523 {
524 	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
525 }
526 #endif /* _ILP32 */
527 
528 /*
529  * ddi_peekpokeio() is used primarily by the mem drivers for moving
530  * data to and from uio structures via peek and poke.  Note that we
531  * use "internal" routines ddi_peek and ddi_poke to make this go
532  * slightly faster, avoiding the call overhead ..
533  */
534 int
535 ddi_peekpokeio(dev_info_t *devi, struct uio *uio, enum uio_rw rw,
536     caddr_t addr, size_t len, uint_t xfersize)
537 {
538 	int64_t	ibuffer;
539 	int8_t w8;
540 	size_t sz;
541 	int o;
542 
543 	if (xfersize > sizeof (long))
544 		xfersize = sizeof (long);
545 
546 	while (len != 0) {
547 		if ((len | (uintptr_t)addr) & 1) {
548 			sz = sizeof (int8_t);
549 			if (rw == UIO_WRITE) {
550 				if ((o = uwritec(uio)) == -1)
551 					return (DDI_FAILURE);
552 				if (ddi_poke8(devi, (int8_t *)addr,
553 				    (int8_t)o) != DDI_SUCCESS)
554 					return (DDI_FAILURE);
555 			} else {
556 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
557 				    (int8_t *)addr, &w8) != DDI_SUCCESS)
558 					return (DDI_FAILURE);
559 				if (ureadc(w8, uio))
560 					return (DDI_FAILURE);
561 			}
562 		} else {
563 			switch (xfersize) {
564 			case sizeof (int64_t):
565 				if (((len | (uintptr_t)addr) &
566 				    (sizeof (int64_t) - 1)) == 0) {
567 					sz = xfersize;
568 					break;
569 				}
570 				/*FALLTHROUGH*/
571 			case sizeof (int32_t):
572 				if (((len | (uintptr_t)addr) &
573 				    (sizeof (int32_t) - 1)) == 0) {
574 					sz = xfersize;
575 					break;
576 				}
577 				/*FALLTHROUGH*/
578 			default:
579 				/*
580 				 * This still assumes that we might have an
581 				 * I/O bus out there that permits 16-bit
582 				 * transfers (and that it would be upset by
583 				 * 32-bit transfers from such locations).
584 				 */
585 				sz = sizeof (int16_t);
586 				break;
587 			}
588 
589 			if (rw == UIO_READ) {
590 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
591 				    addr, &ibuffer) != DDI_SUCCESS)
592 					return (DDI_FAILURE);
593 			}
594 
595 			if (uiomove(&ibuffer, sz, rw, uio))
596 				return (DDI_FAILURE);
597 
598 			if (rw == UIO_WRITE) {
599 				if (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, sz,
600 				    addr, &ibuffer) != DDI_SUCCESS)
601 					return (DDI_FAILURE);
602 			}
603 		}
604 		addr += sz;
605 		len -= sz;
606 	}
607 	return (DDI_SUCCESS);
608 }
609 
610 /*
611  * These routines are used by drivers that do layered ioctls
612  * On sparc, they're implemented in assembler to avoid spilling
613  * register windows in the common (copyin) case ..
614  */
615 #if !defined(__sparc)
616 int
617 ddi_copyin(const void *buf, void *kernbuf, size_t size, int flags)
618 {
619 	if (flags & FKIOCTL)
620 		return (kcopy(buf, kernbuf, size) ? -1 : 0);
621 	return (copyin(buf, kernbuf, size));
622 }
623 
624 int
625 ddi_copyout(const void *buf, void *kernbuf, size_t size, int flags)
626 {
627 	if (flags & FKIOCTL)
628 		return (kcopy(buf, kernbuf, size) ? -1 : 0);
629 	return (copyout(buf, kernbuf, size));
630 }
631 #endif	/* !__sparc */
632 
633 /*
634  * Conversions in nexus pagesize units.  We don't duplicate the
635  * 'nil dip' semantics of peek/poke because btopr/btop/ptob are DDI/DKI
636  * routines anyway.
637  */
638 unsigned long
639 ddi_btop(dev_info_t *dip, unsigned long bytes)
640 {
641 	unsigned long pages;
642 
643 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOP, &bytes, &pages);
644 	return (pages);
645 }
646 
647 unsigned long
648 ddi_btopr(dev_info_t *dip, unsigned long bytes)
649 {
650 	unsigned long pages;
651 
652 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOPR, &bytes, &pages);
653 	return (pages);
654 }
655 
656 unsigned long
657 ddi_ptob(dev_info_t *dip, unsigned long pages)
658 {
659 	unsigned long bytes;
660 
661 	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_PTOB, &pages, &bytes);
662 	return (bytes);
663 }
664 
665 unsigned int
666 ddi_enter_critical(void)
667 {
668 	return ((uint_t)spl7());
669 }
670 
671 void
672 ddi_exit_critical(unsigned int spl)
673 {
674 	splx((int)spl);
675 }
676 
677 /*
678  * Nexus ctlops punter
679  */
680 
681 #if !defined(__sparc)
682 /*
683  * Request bus_ctl parent to handle a bus_ctl request
684  *
685  * (The sparc version is in sparc_ddi.s)
686  */
687 int
688 ddi_ctlops(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t op, void *a, void *v)
689 {
690 	int (*fp)();
691 
692 	if (!d || !r)
693 		return (DDI_FAILURE);
694 
695 	if ((d = (dev_info_t *)DEVI(d)->devi_bus_ctl) == NULL)
696 		return (DDI_FAILURE);
697 
698 	fp = DEVI(d)->devi_ops->devo_bus_ops->bus_ctl;
699 	return ((*fp)(d, r, op, a, v));
700 }
701 
702 #endif
703 
704 /*
705  * DMA/DVMA setup
706  */
707 
708 #if defined(__sparc)
709 static ddi_dma_lim_t standard_limits = {
710 	(uint_t)0,	/* addr_t dlim_addr_lo */
711 	(uint_t)-1,	/* addr_t dlim_addr_hi */
712 	(uint_t)-1,	/* uint_t dlim_cntr_max */
713 	(uint_t)1,	/* uint_t dlim_burstsizes */
714 	(uint_t)1,	/* uint_t dlim_minxfer */
715 	0		/* uint_t dlim_dmaspeed */
716 };
717 #elif defined(__x86)
718 static ddi_dma_lim_t standard_limits = {
719 	(uint_t)0,		/* addr_t dlim_addr_lo */
720 	(uint_t)0xffffff,	/* addr_t dlim_addr_hi */
721 	(uint_t)0,		/* uint_t dlim_cntr_max */
722 	(uint_t)0x00000001,	/* uint_t dlim_burstsizes */
723 	(uint_t)DMA_UNIT_8,	/* uint_t dlim_minxfer */
724 	(uint_t)0,		/* uint_t dlim_dmaspeed */
725 	(uint_t)0x86<<24+0,	/* uint_t dlim_version */
726 	(uint_t)0xffff,		/* uint_t dlim_adreg_max */
727 	(uint_t)0xffff,		/* uint_t dlim_ctreg_max */
728 	(uint_t)512,		/* uint_t dlim_granular */
729 	(int)1,			/* int dlim_sgllen */
730 	(uint_t)0xffffffff	/* uint_t dlim_reqsizes */
731 };
732 
733 #endif
734 
735 int
736 ddi_dma_setup(dev_info_t *dip, struct ddi_dma_req *dmareqp,
737     ddi_dma_handle_t *handlep)
738 {
739 	int (*funcp)() = ddi_dma_map;
740 	struct bus_ops *bop;
741 #if defined(__sparc)
742 	auto ddi_dma_lim_t dma_lim;
743 
744 	if (dmareqp->dmar_limits == (ddi_dma_lim_t *)0) {
745 		dma_lim = standard_limits;
746 	} else {
747 		dma_lim = *dmareqp->dmar_limits;
748 	}
749 	dmareqp->dmar_limits = &dma_lim;
750 #endif
751 #if defined(__x86)
752 	if (dmareqp->dmar_limits == (ddi_dma_lim_t *)0)
753 		return (DDI_FAILURE);
754 #endif
755 
756 	/*
757 	 * Handle the case that the requester is both a leaf
758 	 * and a nexus driver simultaneously by calling the
759 	 * requester's bus_dma_map function directly instead
760 	 * of ddi_dma_map.
761 	 */
762 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
763 	if (bop && bop->bus_dma_map)
764 		funcp = bop->bus_dma_map;
765 	return ((*funcp)(dip, dip, dmareqp, handlep));
766 }
767 
768 int
769 ddi_dma_addr_setup(dev_info_t *dip, struct as *as, caddr_t addr, size_t len,
770     uint_t flags, int (*waitfp)(), caddr_t arg,
771     ddi_dma_lim_t *limits, ddi_dma_handle_t *handlep)
772 {
773 	int (*funcp)() = ddi_dma_map;
774 	ddi_dma_lim_t dma_lim;
775 	struct ddi_dma_req dmareq;
776 	struct bus_ops *bop;
777 
778 	if (len == 0) {
779 		return (DDI_DMA_NOMAPPING);
780 	}
781 	if (limits == (ddi_dma_lim_t *)0) {
782 		dma_lim = standard_limits;
783 	} else {
784 		dma_lim = *limits;
785 	}
786 	dmareq.dmar_limits = &dma_lim;
787 	dmareq.dmar_flags = flags;
788 	dmareq.dmar_fp = waitfp;
789 	dmareq.dmar_arg = arg;
790 	dmareq.dmar_object.dmao_size = len;
791 	dmareq.dmar_object.dmao_type = DMA_OTYP_VADDR;
792 	dmareq.dmar_object.dmao_obj.virt_obj.v_as = as;
793 	dmareq.dmar_object.dmao_obj.virt_obj.v_addr = addr;
794 	dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
795 
796 	/*
797 	 * Handle the case that the requester is both a leaf
798 	 * and a nexus driver simultaneously by calling the
799 	 * requester's bus_dma_map function directly instead
800 	 * of ddi_dma_map.
801 	 */
802 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
803 	if (bop && bop->bus_dma_map)
804 		funcp = bop->bus_dma_map;
805 
806 	return ((*funcp)(dip, dip, &dmareq, handlep));
807 }
808 
809 int
810 ddi_dma_buf_setup(dev_info_t *dip, struct buf *bp, uint_t flags,
811     int (*waitfp)(), caddr_t arg, ddi_dma_lim_t *limits,
812     ddi_dma_handle_t *handlep)
813 {
814 	int (*funcp)() = ddi_dma_map;
815 	ddi_dma_lim_t dma_lim;
816 	struct ddi_dma_req dmareq;
817 	struct bus_ops *bop;
818 
819 	if (limits == (ddi_dma_lim_t *)0) {
820 		dma_lim = standard_limits;
821 	} else {
822 		dma_lim = *limits;
823 	}
824 	dmareq.dmar_limits = &dma_lim;
825 	dmareq.dmar_flags = flags;
826 	dmareq.dmar_fp = waitfp;
827 	dmareq.dmar_arg = arg;
828 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
829 
830 	if (bp->b_flags & B_PAGEIO) {
831 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
832 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
833 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
834 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
835 	} else {
836 		dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
837 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
838 		if (bp->b_flags & B_SHADOW) {
839 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
840 			    bp->b_shadow;
841 		} else {
842 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
843 		}
844 
845 		/*
846 		 * If the buffer has no proc pointer, or the proc
847 		 * struct has the kernel address space, or the buffer has
848 		 * been marked B_REMAPPED (meaning that it is now
849 		 * mapped into the kernel's address space), then
850 		 * the address space is kas (kernel address space).
851 		 */
852 		if ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas) ||
853 		    (bp->b_flags & B_REMAPPED)) {
854 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
855 		} else {
856 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
857 			    bp->b_proc->p_as;
858 		}
859 	}
860 
861 	/*
862 	 * Handle the case that the requester is both a leaf
863 	 * and a nexus driver simultaneously by calling the
864 	 * requester's bus_dma_map function directly instead
865 	 * of ddi_dma_map.
866 	 */
867 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
868 	if (bop && bop->bus_dma_map)
869 		funcp = bop->bus_dma_map;
870 
871 	return ((*funcp)(dip, dip, &dmareq, handlep));
872 }
873 
874 #if !defined(__sparc)
875 /*
876  * Request bus_dma_ctl parent to fiddle with a dma request.
877  *
878  * (The sparc version is in sparc_subr.s)
879  */
880 int
881 ddi_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
882     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
883     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
884 {
885 	int (*fp)();
886 
887 	if (dip != ddi_root_node())
888 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_ctl;
889 	fp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_ctl;
890 	return ((*fp) (dip, rdip, handle, request, offp, lenp, objp, flags));
891 }
892 #endif
893 
894 /*
895  * For all DMA control functions, call the DMA control
896  * routine and return status.
897  *
898  * Just plain assume that the parent is to be called.
899  * If a nexus driver or a thread outside the framework
900  * of a nexus driver or a leaf driver calls these functions,
901  * it is up to them to deal with the fact that the parent's
902  * bus_dma_ctl function will be the first one called.
903  */
904 
905 #define	HD	((ddi_dma_impl_t *)h)->dmai_rdip
906 
907 int
908 ddi_dma_kvaddrp(ddi_dma_handle_t h, off_t off, size_t len, caddr_t *kp)
909 {
910 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_KVADDR, &off, &len, kp, 0));
911 }
912 
913 int
914 ddi_dma_htoc(ddi_dma_handle_t h, off_t o, ddi_dma_cookie_t *c)
915 {
916 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_HTOC, &o, 0, (caddr_t *)c, 0));
917 }
918 
919 int
920 ddi_dma_coff(ddi_dma_handle_t h, ddi_dma_cookie_t *c, off_t *o)
921 {
922 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_COFF,
923 	    (off_t *)c, 0, (caddr_t *)o, 0));
924 }
925 
926 int
927 ddi_dma_movwin(ddi_dma_handle_t h, off_t *o, size_t *l, ddi_dma_cookie_t *c)
928 {
929 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_MOVWIN, o,
930 	    l, (caddr_t *)c, 0));
931 }
932 
933 int
934 ddi_dma_curwin(ddi_dma_handle_t h, off_t *o, size_t *l)
935 {
936 	if ((((ddi_dma_impl_t *)h)->dmai_rflags & DDI_DMA_PARTIAL) == 0)
937 		return (DDI_FAILURE);
938 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_REPWIN, o, l, 0, 0));
939 }
940 
941 int
942 ddi_dma_nextwin(ddi_dma_handle_t h, ddi_dma_win_t win,
943     ddi_dma_win_t *nwin)
944 {
945 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_NEXTWIN, (off_t *)&win, 0,
946 	    (caddr_t *)nwin, 0));
947 }
948 
949 int
950 ddi_dma_nextseg(ddi_dma_win_t win, ddi_dma_seg_t seg, ddi_dma_seg_t *nseg)
951 {
952 	ddi_dma_handle_t h = (ddi_dma_handle_t)win;
953 
954 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_NEXTSEG, (off_t *)&win,
955 	    (size_t *)&seg, (caddr_t *)nseg, 0));
956 }
957 
958 #if (defined(__i386) && !defined(__amd64)) || defined(__sparc)
959 /*
960  * This routine is Obsolete and should be removed from ALL architectures
961  * in a future release of Solaris.
962  *
963  * It is deliberately NOT ported to amd64; please fix the code that
964  * depends on this routine to use ddi_dma_nextcookie(9F).
965  *
966  * NOTE: even though we fixed the pointer through a 32-bit param issue (the fix
967  * is a side effect to some other cleanup), we're still not going to support
968  * this interface on x64.
969  */
970 int
971 ddi_dma_segtocookie(ddi_dma_seg_t seg, off_t *o, off_t *l,
972     ddi_dma_cookie_t *cookiep)
973 {
974 	ddi_dma_handle_t h = (ddi_dma_handle_t)seg;
975 
976 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_SEGTOC, o, (size_t *)l,
977 	    (caddr_t *)cookiep, 0));
978 }
979 #endif	/* (__i386 && !__amd64) || __sparc */
980 
981 #if !defined(__sparc)
982 
983 /*
984  * The SPARC versions of these routines are done in assembler to
985  * save register windows, so they're in sparc_subr.s.
986  */
987 
988 int
989 ddi_dma_map(dev_info_t *dip, dev_info_t *rdip,
990 	struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
991 {
992 	int (*funcp)(dev_info_t *, dev_info_t *, struct ddi_dma_req *,
993 	    ddi_dma_handle_t *);
994 
995 	if (dip != ddi_root_node())
996 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_map;
997 
998 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_map;
999 	return ((*funcp)(dip, rdip, dmareqp, handlep));
1000 }
1001 
1002 int
1003 ddi_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
1004     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
1005 {
1006 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
1007 	    int (*)(caddr_t), caddr_t, ddi_dma_handle_t *);
1008 
1009 	if (dip != ddi_root_node())
1010 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;
1011 
1012 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_allochdl;
1013 	return ((*funcp)(dip, rdip, attr, waitfp, arg, handlep));
1014 }
1015 
1016 int
1017 ddi_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handlep)
1018 {
1019 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
1020 
1021 	if (dip != ddi_root_node())
1022 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;
1023 
1024 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_freehdl;
1025 	return ((*funcp)(dip, rdip, handlep));
1026 }
1027 
1028 int
1029 ddi_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
1030     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
1031     ddi_dma_cookie_t *cp, uint_t *ccountp)
1032 {
1033 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
1034 	    struct ddi_dma_req *, ddi_dma_cookie_t *, uint_t *);
1035 
1036 	if (dip != ddi_root_node())
1037 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
1038 
1039 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_bindhdl;
1040 	return ((*funcp)(dip, rdip, handle, dmareq, cp, ccountp));
1041 }
1042 
1043 int
1044 ddi_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
1045     ddi_dma_handle_t handle)
1046 {
1047 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
1048 
1049 	if (dip != ddi_root_node())
1050 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;
1051 
1052 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
1053 	return ((*funcp)(dip, rdip, handle));
1054 }
1055 
1056 
1057 int
1058 ddi_dma_flush(dev_info_t *dip, dev_info_t *rdip,
1059     ddi_dma_handle_t handle, off_t off, size_t len,
1060     uint_t cache_flags)
1061 {
1062 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
1063 	    off_t, size_t, uint_t);
1064 
1065 	if (dip != ddi_root_node())
1066 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;
1067 
1068 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
1069 	return ((*funcp)(dip, rdip, handle, off, len, cache_flags));
1070 }
1071 
1072 int
1073 ddi_dma_win(dev_info_t *dip, dev_info_t *rdip,
1074     ddi_dma_handle_t handle, uint_t win, off_t *offp,
1075     size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
1076 {
1077 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
1078 	    uint_t, off_t *, size_t *, ddi_dma_cookie_t *, uint_t *);
1079 
1080 	if (dip != ddi_root_node())
1081 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_win;
1082 
1083 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_win;
1084 	return ((*funcp)(dip, rdip, handle, win, offp, lenp,
1085 	    cookiep, ccountp));
1086 }
1087 
1088 int
1089 ddi_dma_sync(ddi_dma_handle_t h, off_t o, size_t l, uint_t whom)
1090 {
1091 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
1092 	dev_info_t *dip, *rdip;
1093 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t, off_t,
1094 	    size_t, uint_t);
1095 
1096 	/*
1097 	 * the DMA nexus driver will set DMP_NOSYNC if the
1098 	 * platform does not require any sync operation. For
1099 	 * example if the memory is uncached or consistent
1100 	 * and without any I/O write buffers involved.
1101 	 */
1102 	if ((hp->dmai_rflags & DMP_NOSYNC) == DMP_NOSYNC)
1103 		return (DDI_SUCCESS);
1104 
1105 	dip = rdip = hp->dmai_rdip;
1106 	if (dip != ddi_root_node())
1107 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;
1108 	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
1109 	return ((*funcp)(dip, rdip, h, o, l, whom));
1110 }
1111 
1112 int
1113 ddi_dma_unbind_handle(ddi_dma_handle_t h)
1114 {
1115 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
1116 	dev_info_t *dip, *rdip;
1117 	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);
1118 
1119 	dip = rdip = hp->dmai_rdip;
1120 	if (dip != ddi_root_node())
1121 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;
1122 	funcp = DEVI(rdip)->devi_bus_dma_unbindfunc;
1123 	return ((*funcp)(dip, rdip, h));
1124 }
1125 
1126 #endif	/* !__sparc */
1127 
1128 int
1129 ddi_dma_free(ddi_dma_handle_t h)
1130 {
1131 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_FREE, 0, 0, 0, 0));
1132 }
1133 
1134 int
1135 ddi_iopb_alloc(dev_info_t *dip, ddi_dma_lim_t *limp, uint_t len, caddr_t *iopbp)
1136 {
1137 	ddi_dma_lim_t defalt;
1138 	size_t size = len;
1139 
1140 	if (!limp) {
1141 		defalt = standard_limits;
1142 		limp = &defalt;
1143 	}
1144 	return (i_ddi_mem_alloc_lim(dip, limp, size, 0, 0, 0,
1145 	    iopbp, NULL, NULL));
1146 }
1147 
1148 void
1149 ddi_iopb_free(caddr_t iopb)
1150 {
1151 	i_ddi_mem_free(iopb, NULL);
1152 }
1153 
1154 int
1155 ddi_mem_alloc(dev_info_t *dip, ddi_dma_lim_t *limits, uint_t length,
1156 	uint_t flags, caddr_t *kaddrp, uint_t *real_length)
1157 {
1158 	ddi_dma_lim_t defalt;
1159 	size_t size = length;
1160 
1161 	if (!limits) {
1162 		defalt = standard_limits;
1163 		limits = &defalt;
1164 	}
1165 	return (i_ddi_mem_alloc_lim(dip, limits, size, flags & 0x1,
1166 	    1, 0, kaddrp, real_length, NULL));
1167 }
1168 
1169 void
1170 ddi_mem_free(caddr_t kaddr)
1171 {
1172 	i_ddi_mem_free(kaddr, NULL);
1173 }
1174 
1175 /*
1176  * DMA attributes, alignment, burst sizes, and transfer minimums
1177  */
1178 int
1179 ddi_dma_get_attr(ddi_dma_handle_t handle, ddi_dma_attr_t *attrp)
1180 {
1181 	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;
1182 
1183 	if (attrp == NULL)
1184 		return (DDI_FAILURE);
1185 	*attrp = dimp->dmai_attr;
1186 	return (DDI_SUCCESS);
1187 }
1188 
1189 int
1190 ddi_dma_burstsizes(ddi_dma_handle_t handle)
1191 {
1192 	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;
1193 
1194 	if (!dimp)
1195 		return (0);
1196 	else
1197 		return (dimp->dmai_burstsizes);
1198 }
1199 
1200 int
1201 ddi_dma_devalign(ddi_dma_handle_t handle, uint_t *alignment, uint_t *mineffect)
1202 {
1203 	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;
1204 
1205 	if (!dimp || !alignment || !mineffect)
1206 		return (DDI_FAILURE);
1207 	if (!(dimp->dmai_rflags & DDI_DMA_SBUS_64BIT)) {
1208 		*alignment = 1 << ddi_ffs(dimp->dmai_burstsizes);
1209 	} else {
1210 		if (dimp->dmai_burstsizes & 0xff0000) {
1211 			*alignment = 1 << ddi_ffs(dimp->dmai_burstsizes >> 16);
1212 		} else {
1213 			*alignment = 1 << ddi_ffs(dimp->dmai_burstsizes);
1214 		}
1215 	}
1216 	*mineffect = dimp->dmai_minxfer;
1217 	return (DDI_SUCCESS);
1218 }
1219 
1220 int
1221 ddi_iomin(dev_info_t *a, int i, int stream)
1222 {
1223 	int r;
1224 
1225 	/*
1226 	 * Make sure that the initial value is sane
1227 	 */
1228 	if (i & (i - 1))
1229 		return (0);
1230 	if (i == 0)
1231 		i = (stream) ? 4 : 1;
1232 
1233 	r = ddi_ctlops(a, a,
1234 	    DDI_CTLOPS_IOMIN, (void *)(uintptr_t)stream, (void *)&i);
1235 	if (r != DDI_SUCCESS || (i & (i - 1)))
1236 		return (0);
1237 	return (i);
1238 }
1239 
1240 /*
1241  * Given two DMA attribute structures, apply the attributes
1242  * of one to the other, following the rules of attributes
1243  * and the wishes of the caller.
1244  *
1245  * The rules of DMA attribute structures are that you cannot
1246  * make things *less* restrictive as you apply one set
1247  * of attributes to another.
1248  *
1249  */
1250 void
1251 ddi_dma_attr_merge(ddi_dma_attr_t *attr, ddi_dma_attr_t *mod)
1252 {
1253 	attr->dma_attr_addr_lo =
1254 	    MAX(attr->dma_attr_addr_lo, mod->dma_attr_addr_lo);
1255 	attr->dma_attr_addr_hi =
1256 	    MIN(attr->dma_attr_addr_hi, mod->dma_attr_addr_hi);
1257 	attr->dma_attr_count_max =
1258 	    MIN(attr->dma_attr_count_max, mod->dma_attr_count_max);
1259 	attr->dma_attr_align =
1260 	    MAX(attr->dma_attr_align,  mod->dma_attr_align);
1261 	attr->dma_attr_burstsizes =
1262 	    (uint_t)(attr->dma_attr_burstsizes & mod->dma_attr_burstsizes);
1263 	attr->dma_attr_minxfer =
1264 	    maxbit(attr->dma_attr_minxfer, mod->dma_attr_minxfer);
1265 	attr->dma_attr_maxxfer =
1266 	    MIN(attr->dma_attr_maxxfer, mod->dma_attr_maxxfer);
1267 	attr->dma_attr_seg = MIN(attr->dma_attr_seg, mod->dma_attr_seg);
1268 	attr->dma_attr_sgllen = MIN((uint_t)attr->dma_attr_sgllen,
1269 	    (uint_t)mod->dma_attr_sgllen);
1270 	attr->dma_attr_granular =
1271 	    MAX(attr->dma_attr_granular, mod->dma_attr_granular);
1272 }
1273 
1274 /*
1275  * mmap/segmap interface:
1276  */
1277 
1278 /*
1279  * ddi_segmap:		setup the default segment driver. Calls the drivers
1280  *			XXmmap routine to validate the range to be mapped.
1281  *			Return ENXIO of the range is not valid.  Create
1282  *			a seg_dev segment that contains all of the
1283  *			necessary information and will reference the
1284  *			default segment driver routines. It returns zero
1285  *			on success or non-zero on failure.
1286  */
1287 int
1288 ddi_segmap(dev_t dev, off_t offset, struct as *asp, caddr_t *addrp, off_t len,
1289     uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp)
1290 {
1291 	extern int spec_segmap(dev_t, off_t, struct as *, caddr_t *,
1292 	    off_t, uint_t, uint_t, uint_t, struct cred *);
1293 
1294 	return (spec_segmap(dev, offset, asp, addrp, len,
1295 	    prot, maxprot, flags, credp));
1296 }
1297 
1298 /*
1299  * ddi_map_fault:	Resolve mappings at fault time.  Used by segment
1300  *			drivers. Allows each successive parent to resolve
1301  *			address translations and add its mappings to the
1302  *			mapping list supplied in the page structure. It
1303  *			returns zero on success	or non-zero on failure.
1304  */
1305 
1306 int
1307 ddi_map_fault(dev_info_t *dip, struct hat *hat, struct seg *seg,
1308     caddr_t addr, struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
1309 {
1310 	return (i_ddi_map_fault(dip, dip, hat, seg, addr, dp, pfn, prot, lock));
1311 }
1312 
1313 /*
1314  * ddi_device_mapping_check:	Called from ddi_segmap_setup.
1315  *	Invokes platform specific DDI to determine whether attributes specified
1316  *	in attr(9s) are	valid for the region of memory that will be made
1317  *	available for direct access to user process via the mmap(2) system call.
1318  */
1319 int
1320 ddi_device_mapping_check(dev_t dev, ddi_device_acc_attr_t *accattrp,
1321     uint_t rnumber, uint_t *hat_flags)
1322 {
1323 	ddi_acc_handle_t handle;
1324 	ddi_map_req_t mr;
1325 	ddi_acc_hdl_t *hp;
1326 	int result;
1327 	dev_info_t *dip;
1328 
1329 	/*
1330 	 * we use e_ddi_hold_devi_by_dev to search for the devi.  We
1331 	 * release it immediately since it should already be held by
1332 	 * a devfs vnode.
1333 	 */
1334 	if ((dip =
1335 	    e_ddi_hold_devi_by_dev(dev, E_DDI_HOLD_DEVI_NOATTACH)) == NULL)
1336 		return (-1);
1337 	ddi_release_devi(dip);		/* for e_ddi_hold_devi_by_dev() */
1338 
1339 	/*
1340 	 * Allocate and initialize the common elements of data
1341 	 * access handle.
1342 	 */
1343 	handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
1344 	if (handle == NULL)
1345 		return (-1);
1346 
1347 	hp = impl_acc_hdl_get(handle);
1348 	hp->ah_vers = VERS_ACCHDL;
1349 	hp->ah_dip = dip;
1350 	hp->ah_rnumber = rnumber;
1351 	hp->ah_offset = 0;
1352 	hp->ah_len = 0;
1353 	hp->ah_acc = *accattrp;
1354 
1355 	/*
1356 	 * Set up the mapping request and call to parent.
1357 	 */
1358 	mr.map_op = DDI_MO_MAP_HANDLE;
1359 	mr.map_type = DDI_MT_RNUMBER;
1360 	mr.map_obj.rnumber = rnumber;
1361 	mr.map_prot = PROT_READ | PROT_WRITE;
1362 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
1363 	mr.map_handlep = hp;
1364 	mr.map_vers = DDI_MAP_VERSION;
1365 	result = ddi_map(dip, &mr, 0, 0, NULL);
1366 
1367 	/*
1368 	 * Region must be mappable, pick up flags from the framework.
1369 	 */
1370 	*hat_flags = hp->ah_hat_flags;
1371 
1372 	impl_acc_hdl_free(handle);
1373 
1374 	/*
1375 	 * check for end result.
1376 	 */
1377 	if (result != DDI_SUCCESS)
1378 		return (-1);
1379 	return (0);
1380 }
1381 
1382 
1383 /*
1384  * Property functions:	 See also, ddipropdefs.h.
1385  *
1386  * These functions are the framework for the property functions,
1387  * i.e. they support software defined properties.  All implementation
1388  * specific property handling (i.e.: self-identifying devices and
1389  * PROM defined properties are handled in the implementation specific
1390  * functions (defined in ddi_implfuncs.h).
1391  */
1392 
1393 /*
1394  * nopropop:	Shouldn't be called, right?
1395  */
1396 int
1397 nopropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
1398     char *name, caddr_t valuep, int *lengthp)
1399 {
1400 	_NOTE(ARGUNUSED(dev, dip, prop_op, mod_flags, name, valuep, lengthp))
1401 	return (DDI_PROP_NOT_FOUND);
1402 }
1403 
1404 #ifdef	DDI_PROP_DEBUG
1405 int ddi_prop_debug_flag = 0;
1406 
1407 int
1408 ddi_prop_debug(int enable)
1409 {
1410 	int prev = ddi_prop_debug_flag;
1411 
1412 	if ((enable != 0) || (prev != 0))
1413 		printf("ddi_prop_debug: debugging %s\n",
1414 		    enable ? "enabled" : "disabled");
1415 	ddi_prop_debug_flag = enable;
1416 	return (prev);
1417 }
1418 
1419 #endif	/* DDI_PROP_DEBUG */
1420 
1421 /*
1422  * Search a property list for a match, if found return pointer
1423  * to matching prop struct, else return NULL.
1424  */
1425 
1426 ddi_prop_t *
1427 i_ddi_prop_search(dev_t dev, char *name, uint_t flags, ddi_prop_t **list_head)
1428 {
1429 	ddi_prop_t	*propp;
1430 
1431 	/*
1432 	 * find the property in child's devinfo:
1433 	 * Search order defined by this search function is first matching
1434 	 * property with input dev == DDI_DEV_T_ANY matching any dev or
1435 	 * dev == propp->prop_dev, name == propp->name, and the correct
1436 	 * data type as specified in the flags.  If a DDI_DEV_T_NONE dev
1437 	 * value made it this far then it implies a DDI_DEV_T_ANY search.
1438 	 */
1439 	if (dev == DDI_DEV_T_NONE)
1440 		dev = DDI_DEV_T_ANY;
1441 
1442 	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
1443 
1444 		if (!DDI_STRSAME(propp->prop_name, name))
1445 			continue;
1446 
1447 		if ((dev != DDI_DEV_T_ANY) && (propp->prop_dev != dev))
1448 			continue;
1449 
1450 		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
1451 			continue;
1452 
1453 		return (propp);
1454 	}
1455 
1456 	return ((ddi_prop_t *)0);
1457 }
1458 
1459 /*
1460  * Search for property within devnames structures
1461  */
1462 ddi_prop_t *
1463 i_ddi_search_global_prop(dev_t dev, char *name, uint_t flags)
1464 {
1465 	major_t		major;
1466 	struct devnames	*dnp;
1467 	ddi_prop_t	*propp;
1468 
1469 	/*
1470 	 * Valid dev_t value is needed to index into the
1471 	 * correct devnames entry, therefore a dev_t
1472 	 * value of DDI_DEV_T_ANY is not appropriate.
1473 	 */
1474 	ASSERT(dev != DDI_DEV_T_ANY);
1475 	if (dev == DDI_DEV_T_ANY) {
1476 		return ((ddi_prop_t *)0);
1477 	}
1478 
1479 	major = getmajor(dev);
1480 	dnp = &(devnamesp[major]);
1481 
1482 	if (dnp->dn_global_prop_ptr == NULL)
1483 		return ((ddi_prop_t *)0);
1484 
1485 	LOCK_DEV_OPS(&dnp->dn_lock);
1486 
1487 	for (propp = dnp->dn_global_prop_ptr->prop_list;
1488 	    propp != NULL;
1489 	    propp = (ddi_prop_t *)propp->prop_next) {
1490 
1491 		if (!DDI_STRSAME(propp->prop_name, name))
1492 			continue;
1493 
1494 		if ((!(flags & DDI_PROP_ROOTNEX_GLOBAL)) &&
1495 		    (!(flags & LDI_DEV_T_ANY)) && (propp->prop_dev != dev))
1496 			continue;
1497 
1498 		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
1499 			continue;
1500 
1501 		/* Property found, return it */
1502 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1503 		return (propp);
1504 	}
1505 
1506 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1507 	return ((ddi_prop_t *)0);
1508 }
1509 
1510 static char prop_no_mem_msg[] = "can't allocate memory for ddi property <%s>";
1511 
1512 /*
1513  * ddi_prop_search_global:
1514  *	Search the global property list within devnames
1515  *	for the named property.  Return the encoded value.
1516  */
1517 static int
1518 i_ddi_prop_search_global(dev_t dev, uint_t flags, char *name,
1519     void *valuep, uint_t *lengthp)
1520 {
1521 	ddi_prop_t	*propp;
1522 	caddr_t		buffer;
1523 
1524 	propp =  i_ddi_search_global_prop(dev, name, flags);
1525 
1526 	/* Property NOT found, bail */
1527 	if (propp == (ddi_prop_t *)0)
1528 		return (DDI_PROP_NOT_FOUND);
1529 
1530 	if (propp->prop_flags & DDI_PROP_UNDEF_IT)
1531 		return (DDI_PROP_UNDEFINED);
1532 
1533 	if ((buffer = kmem_alloc(propp->prop_len,
1534 	    (flags & DDI_PROP_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP)) == NULL) {
1535 		cmn_err(CE_CONT, prop_no_mem_msg, name);
1536 		return (DDI_PROP_NO_MEMORY);
1537 	}
1538 
1539 	/*
1540 	 * Return the encoded data
1541 	 */
1542 	*(caddr_t *)valuep = buffer;
1543 	*lengthp = propp->prop_len;
1544 	bcopy(propp->prop_val, buffer, propp->prop_len);
1545 
1546 	return (DDI_PROP_SUCCESS);
1547 }
1548 
1549 /*
1550  * ddi_prop_search_common:	Lookup and return the encoded value
1551  */
1552 int
1553 ddi_prop_search_common(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1554     uint_t flags, char *name, void *valuep, uint_t *lengthp)
1555 {
1556 	ddi_prop_t	*propp;
1557 	int		i;
1558 	caddr_t		buffer;
1559 	caddr_t		prealloc = NULL;
1560 	int		plength = 0;
1561 	dev_info_t	*pdip;
1562 	int		(*bop)();
1563 
1564 	/*CONSTANTCONDITION*/
1565 	while (1)  {
1566 
1567 		mutex_enter(&(DEVI(dip)->devi_lock));
1568 
1569 
1570 		/*
1571 		 * find the property in child's devinfo:
1572 		 * Search order is:
1573 		 *	1. driver defined properties
1574 		 *	2. system defined properties
1575 		 *	3. driver global properties
1576 		 *	4. boot defined properties
1577 		 */
1578 
1579 		propp = i_ddi_prop_search(dev, name, flags,
1580 		    &(DEVI(dip)->devi_drv_prop_ptr));
1581 		if (propp == NULL)  {
1582 			propp = i_ddi_prop_search(dev, name, flags,
1583 			    &(DEVI(dip)->devi_sys_prop_ptr));
1584 		}
1585 		if ((propp == NULL) && DEVI(dip)->devi_global_prop_list) {
1586 			propp = i_ddi_prop_search(dev, name, flags,
1587 			    &DEVI(dip)->devi_global_prop_list->prop_list);
1588 		}
1589 
1590 		if (propp == NULL)  {
1591 			propp = i_ddi_prop_search(dev, name, flags,
1592 			    &(DEVI(dip)->devi_hw_prop_ptr));
1593 		}
1594 
1595 		/*
1596 		 * Software property found?
1597 		 */
1598 		if (propp != (ddi_prop_t *)0)	{
1599 
1600 			/*
1601 			 * If explicit undefine, return now.
1602 			 */
1603 			if (propp->prop_flags & DDI_PROP_UNDEF_IT) {
1604 				mutex_exit(&(DEVI(dip)->devi_lock));
1605 				if (prealloc)
1606 					kmem_free(prealloc, plength);
1607 				return (DDI_PROP_UNDEFINED);
1608 			}
1609 
1610 			/*
1611 			 * If we only want to know if it exists, return now
1612 			 */
1613 			if (prop_op == PROP_EXISTS) {
1614 				mutex_exit(&(DEVI(dip)->devi_lock));
1615 				ASSERT(prealloc == NULL);
1616 				return (DDI_PROP_SUCCESS);
1617 			}
1618 
1619 			/*
1620 			 * If length only request or prop length == 0,
1621 			 * service request and return now.
1622 			 */
1623 			if ((prop_op == PROP_LEN) ||(propp->prop_len == 0)) {
1624 				*lengthp = propp->prop_len;
1625 
1626 				/*
1627 				 * if prop_op is PROP_LEN_AND_VAL_ALLOC
1628 				 * that means prop_len is 0, so set valuep
1629 				 * also to NULL
1630 				 */
1631 				if (prop_op == PROP_LEN_AND_VAL_ALLOC)
1632 					*(caddr_t *)valuep = NULL;
1633 
1634 				mutex_exit(&(DEVI(dip)->devi_lock));
1635 				if (prealloc)
1636 					kmem_free(prealloc, plength);
1637 				return (DDI_PROP_SUCCESS);
1638 			}
1639 
1640 			/*
1641 			 * If LEN_AND_VAL_ALLOC and the request can sleep,
1642 			 * drop the mutex, allocate the buffer, and go
1643 			 * through the loop again.  If we already allocated
1644 			 * the buffer, and the size of the property changed,
1645 			 * keep trying...
1646 			 */
1647 			if ((prop_op == PROP_LEN_AND_VAL_ALLOC) &&
1648 			    (flags & DDI_PROP_CANSLEEP))  {
1649 				if (prealloc && (propp->prop_len != plength)) {
1650 					kmem_free(prealloc, plength);
1651 					prealloc = NULL;
1652 				}
1653 				if (prealloc == NULL)  {
1654 					plength = propp->prop_len;
1655 					mutex_exit(&(DEVI(dip)->devi_lock));
1656 					prealloc = kmem_alloc(plength,
1657 					    KM_SLEEP);
1658 					continue;
1659 				}
1660 			}
1661 
1662 			/*
1663 			 * Allocate buffer, if required.  Either way,
1664 			 * set `buffer' variable.
1665 			 */
1666 			i = *lengthp;			/* Get callers length */
1667 			*lengthp = propp->prop_len;	/* Set callers length */
1668 
1669 			switch (prop_op) {
1670 
1671 			case PROP_LEN_AND_VAL_ALLOC:
1672 
1673 				if (prealloc == NULL) {
1674 					buffer = kmem_alloc(propp->prop_len,
1675 					    KM_NOSLEEP);
1676 				} else {
1677 					buffer = prealloc;
1678 				}
1679 
1680 				if (buffer == NULL)  {
1681 					mutex_exit(&(DEVI(dip)->devi_lock));
1682 					cmn_err(CE_CONT, prop_no_mem_msg, name);
1683 					return (DDI_PROP_NO_MEMORY);
1684 				}
1685 				/* Set callers buf ptr */
1686 				*(caddr_t *)valuep = buffer;
1687 				break;
1688 
1689 			case PROP_LEN_AND_VAL_BUF:
1690 
1691 				if (propp->prop_len > (i)) {
1692 					mutex_exit(&(DEVI(dip)->devi_lock));
1693 					return (DDI_PROP_BUF_TOO_SMALL);
1694 				}
1695 
1696 				buffer = valuep;  /* Get callers buf ptr */
1697 				break;
1698 
1699 			default:
1700 				break;
1701 			}
1702 
1703 			/*
1704 			 * Do the copy.
1705 			 */
1706 			bcopy(propp->prop_val, buffer, propp->prop_len);
1707 			mutex_exit(&(DEVI(dip)->devi_lock));
1708 			return (DDI_PROP_SUCCESS);
1709 		}
1710 
1711 		mutex_exit(&(DEVI(dip)->devi_lock));
1712 		if (prealloc)
1713 			kmem_free(prealloc, plength);
1714 		prealloc = NULL;
1715 
1716 		/*
1717 		 * Prop not found, call parent bus_ops to deal with possible
1718 		 * h/w layer (possible PROM defined props, etc.) and to
1719 		 * possibly ascend the hierarchy, if allowed by flags.
1720 		 */
1721 		pdip = (dev_info_t *)DEVI(dip)->devi_parent;
1722 
1723 		/*
1724 		 * One last call for the root driver PROM props?
1725 		 */
1726 		if (dip == ddi_root_node())  {
1727 			return (ddi_bus_prop_op(dev, dip, dip, prop_op,
1728 			    flags, name, valuep, (int *)lengthp));
1729 		}
1730 
1731 		/*
1732 		 * We may have been called to check for properties
1733 		 * within a single devinfo node that has no parent -
1734 		 * see make_prop()
1735 		 */
1736 		if (pdip == NULL) {
1737 			ASSERT((flags &
1738 			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM)) ==
1739 			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM));
1740 			return (DDI_PROP_NOT_FOUND);
1741 		}
1742 
1743 		/*
1744 		 * Instead of recursing, we do iterative calls up the tree.
1745 		 * As a bit of optimization, skip the bus_op level if the
1746 		 * node is a s/w node and if the parent's bus_prop_op function
1747 		 * is `ddi_bus_prop_op', because we know that in this case,
1748 		 * this function does nothing.
1749 		 *
1750 		 * 4225415: If the parent isn't attached, or the child
1751 		 * hasn't been named by the parent yet, use the default
1752 		 * ddi_bus_prop_op as a proxy for the parent.  This
1753 		 * allows property lookups in any child/parent state to
1754 		 * include 'prom' and inherited properties, even when
1755 		 * there are no drivers attached to the child or parent.
1756 		 */
1757 
1758 		bop = ddi_bus_prop_op;
1759 		if (i_ddi_devi_attached(pdip) &&
1760 		    (i_ddi_node_state(dip) >= DS_INITIALIZED))
1761 			bop = DEVI(pdip)->devi_ops->devo_bus_ops->bus_prop_op;
1762 
1763 		i = DDI_PROP_NOT_FOUND;
1764 
1765 		if ((bop != ddi_bus_prop_op) || ndi_dev_is_prom_node(dip)) {
1766 			i = (*bop)(dev, pdip, dip, prop_op,
1767 			    flags | DDI_PROP_DONTPASS,
1768 			    name, valuep, lengthp);
1769 		}
1770 
1771 		if ((flags & DDI_PROP_DONTPASS) ||
1772 		    (i != DDI_PROP_NOT_FOUND))
1773 			return (i);
1774 
1775 		dip = pdip;
1776 	}
1777 	/*NOTREACHED*/
1778 }
1779 
1780 
1781 /*
1782  * ddi_prop_op: The basic property operator for drivers.
1783  *
1784  * In ddi_prop_op, the type of valuep is interpreted based on prop_op:
1785  *
1786  *	prop_op			valuep
1787  *	------			------
1788  *
1789  *	PROP_LEN		<unused>
1790  *
1791  *	PROP_LEN_AND_VAL_BUF	Pointer to callers buffer
1792  *
1793  *	PROP_LEN_AND_VAL_ALLOC	Address of callers pointer (will be set to
1794  *				address of allocated buffer, if successful)
1795  */
1796 int
1797 ddi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
1798     char *name, caddr_t valuep, int *lengthp)
1799 {
1800 	int	i;
1801 
1802 	ASSERT((mod_flags & DDI_PROP_TYPE_MASK) == 0);
1803 
1804 	/*
1805 	 * If this was originally an LDI prop lookup then we bail here.
1806 	 * The reason is that the LDI property lookup interfaces first call
1807 	 * a drivers prop_op() entry point to allow it to override
1808 	 * properties.  But if we've made it here, then the driver hasn't
1809 	 * overriden any properties.  We don't want to continue with the
1810 	 * property search here because we don't have any type inforamtion.
1811 	 * When we return failure, the LDI interfaces will then proceed to
1812 	 * call the typed property interfaces to look up the property.
1813 	 */
1814 	if (mod_flags & DDI_PROP_DYNAMIC)
1815 		return (DDI_PROP_NOT_FOUND);
1816 
1817 	/*
1818 	 * check for pre-typed property consumer asking for typed property:
1819 	 * see e_ddi_getprop_int64.
1820 	 */
1821 	if (mod_flags & DDI_PROP_CONSUMER_TYPED)
1822 		mod_flags |= DDI_PROP_TYPE_INT64;
1823 	mod_flags |= DDI_PROP_TYPE_ANY;
1824 
1825 	i = ddi_prop_search_common(dev, dip, prop_op,
1826 	    mod_flags, name, valuep, (uint_t *)lengthp);
1827 	if (i == DDI_PROP_FOUND_1275)
1828 		return (DDI_PROP_SUCCESS);
1829 	return (i);
1830 }
1831 
1832 /*
1833  * ddi_prop_op_nblocks_blksize: The basic property operator for drivers that
1834  * maintain size in number of blksize blocks.  Provides a dynamic property
1835  * implementation for size oriented properties based on nblocks64 and blksize
1836  * values passed in by the driver.  Fallback to ddi_prop_op if the nblocks64
1837  * is too large.  This interface should not be used with a nblocks64 that
1838  * represents the driver's idea of how to represent unknown, if nblocks is
1839  * unknown use ddi_prop_op.
1840  */
1841 int
1842 ddi_prop_op_nblocks_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1843     int mod_flags, char *name, caddr_t valuep, int *lengthp,
1844     uint64_t nblocks64, uint_t blksize)
1845 {
1846 	uint64_t size64;
1847 	int	blkshift;
1848 
1849 	/* convert block size to shift value */
1850 	ASSERT(BIT_ONLYONESET(blksize));
1851 	blkshift = highbit(blksize) - 1;
1852 
1853 	/*
1854 	 * There is no point in supporting nblocks64 values that don't have
1855 	 * an accurate uint64_t byte count representation.
1856 	 */
1857 	if (nblocks64 >= (UINT64_MAX >> blkshift))
1858 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
1859 		    name, valuep, lengthp));
1860 
1861 	size64 = nblocks64 << blkshift;
1862 	return (ddi_prop_op_size_blksize(dev, dip, prop_op, mod_flags,
1863 	    name, valuep, lengthp, size64, blksize));
1864 }
1865 
1866 /*
1867  * ddi_prop_op_nblocks: ddi_prop_op_nblocks_blksize with DEV_BSIZE blksize.
1868  */
1869 int
1870 ddi_prop_op_nblocks(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1871     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t nblocks64)
1872 {
1873 	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op,
1874 	    mod_flags, name, valuep, lengthp, nblocks64, DEV_BSIZE));
1875 }
1876 
1877 /*
1878  * ddi_prop_op_size_blksize: The basic property operator for block drivers that
1879  * maintain size in bytes. Provides a of dynamic property implementation for
1880  * size oriented properties based on size64 value and blksize passed in by the
1881  * driver.  Fallback to ddi_prop_op if the size64 is too large. This interface
1882  * should not be used with a size64 that represents the driver's idea of how
1883  * to represent unknown, if size is unknown use ddi_prop_op.
1884  *
1885  * NOTE: the legacy "nblocks"/"size" properties are treated as 32-bit unsigned
1886  * integers. While the most likely interface to request them ([bc]devi_size)
1887  * is declared int (signed) there is no enforcement of this, which means we
1888  * can't enforce limitations here without risking regression.
1889  */
1890 int
1891 ddi_prop_op_size_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1892     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64,
1893     uint_t blksize)
1894 {
1895 	uint64_t nblocks64;
1896 	int	callers_length;
1897 	caddr_t	buffer;
1898 	int	blkshift;
1899 
1900 	/*
1901 	 * This is a kludge to support capture of size(9P) pure dynamic
1902 	 * properties in snapshots for non-cmlb code (without exposing
1903 	 * i_ddi_prop_dyn changes). When everyone uses cmlb, this code
1904 	 * should be removed.
1905 	 */
1906 	if (i_ddi_prop_dyn_driver_get(dip) == NULL) {
1907 		static i_ddi_prop_dyn_t prop_dyn_size[] = {
1908 		    {"Size",		DDI_PROP_TYPE_INT64,	S_IFCHR},
1909 		    {"Nblocks",		DDI_PROP_TYPE_INT64,	S_IFBLK},
1910 		    {NULL}
1911 		};
1912 		i_ddi_prop_dyn_driver_set(dip, prop_dyn_size);
1913 	}
1914 
1915 	/* convert block size to shift value */
1916 	ASSERT(BIT_ONLYONESET(blksize));
1917 	blkshift = highbit(blksize) - 1;
1918 
1919 	/* compute DEV_BSIZE nblocks value */
1920 	nblocks64 = size64 >> blkshift;
1921 
1922 	/* get callers length, establish length of our dynamic properties */
1923 	callers_length = *lengthp;
1924 
1925 	if (strcmp(name, "Nblocks") == 0)
1926 		*lengthp = sizeof (uint64_t);
1927 	else if (strcmp(name, "Size") == 0)
1928 		*lengthp = sizeof (uint64_t);
1929 	else if ((strcmp(name, "nblocks") == 0) && (nblocks64 < UINT_MAX))
1930 		*lengthp = sizeof (uint32_t);
1931 	else if ((strcmp(name, "size") == 0) && (size64 < UINT_MAX))
1932 		*lengthp = sizeof (uint32_t);
1933 	else if ((strcmp(name, "blksize") == 0) && (blksize < UINT_MAX))
1934 		*lengthp = sizeof (uint32_t);
1935 	else {
1936 		/* fallback to ddi_prop_op */
1937 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
1938 		    name, valuep, lengthp));
1939 	}
1940 
1941 	/* service request for the length of the property */
1942 	if (prop_op == PROP_LEN)
1943 		return (DDI_PROP_SUCCESS);
1944 
1945 	switch (prop_op) {
1946 	case PROP_LEN_AND_VAL_ALLOC:
1947 		if ((buffer = kmem_alloc(*lengthp,
1948 		    (mod_flags & DDI_PROP_CANSLEEP) ?
1949 		    KM_SLEEP : KM_NOSLEEP)) == NULL)
1950 			return (DDI_PROP_NO_MEMORY);
1951 
1952 		*(caddr_t *)valuep = buffer;	/* set callers buf ptr */
1953 		break;
1954 
1955 	case PROP_LEN_AND_VAL_BUF:
1956 		/* the length of the property and the request must match */
1957 		if (callers_length != *lengthp)
1958 			return (DDI_PROP_INVAL_ARG);
1959 
1960 		buffer = valuep;		/* get callers buf ptr */
1961 		break;
1962 
1963 	default:
1964 		return (DDI_PROP_INVAL_ARG);
1965 	}
1966 
1967 	/* transfer the value into the buffer */
1968 	if (strcmp(name, "Nblocks") == 0)
1969 		*((uint64_t *)buffer) = nblocks64;
1970 	else if (strcmp(name, "Size") == 0)
1971 		*((uint64_t *)buffer) = size64;
1972 	else if (strcmp(name, "nblocks") == 0)
1973 		*((uint32_t *)buffer) = (uint32_t)nblocks64;
1974 	else if (strcmp(name, "size") == 0)
1975 		*((uint32_t *)buffer) = (uint32_t)size64;
1976 	else if (strcmp(name, "blksize") == 0)
1977 		*((uint32_t *)buffer) = (uint32_t)blksize;
1978 	return (DDI_PROP_SUCCESS);
1979 }
1980 
1981 /*
1982  * ddi_prop_op_size: ddi_prop_op_size_blksize with DEV_BSIZE block size.
1983  */
1984 int
1985 ddi_prop_op_size(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1986     int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64)
1987 {
1988 	return (ddi_prop_op_size_blksize(dev, dip, prop_op,
1989 	    mod_flags, name, valuep, lengthp, size64, DEV_BSIZE));
1990 }
1991 
1992 /*
1993  * Variable length props...
1994  */
1995 
1996 /*
1997  * ddi_getlongprop:	Get variable length property len+val into a buffer
1998  *		allocated by property provider via kmem_alloc. Requester
1999  *		is responsible for freeing returned property via kmem_free.
2000  *
2001  *	Arguments:
2002  *
2003  *	dev_t:	Input:	dev_t of property.
2004  *	dip:	Input:	dev_info_t pointer of child.
2005  *	flags:	Input:	Possible flag modifiers are:
2006  *		DDI_PROP_DONTPASS:	Don't pass to parent if prop not found.
2007  *		DDI_PROP_CANSLEEP:	Memory allocation may sleep.
2008  *	name:	Input:	name of property.
2009  *	valuep:	Output:	Addr of callers buffer pointer.
2010  *	lengthp:Output:	*lengthp will contain prop length on exit.
2011  *
2012  *	Possible Returns:
2013  *
2014  *		DDI_PROP_SUCCESS:	Prop found and returned.
2015  *		DDI_PROP_NOT_FOUND:	Prop not found
2016  *		DDI_PROP_UNDEFINED:	Prop explicitly undefined.
2017  *		DDI_PROP_NO_MEMORY:	Prop found, but unable to alloc mem.
2018  */
2019 
2020 int
2021 ddi_getlongprop(dev_t dev, dev_info_t *dip, int flags,
2022     char *name, caddr_t valuep, int *lengthp)
2023 {
2024 	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_ALLOC,
2025 	    flags, name, valuep, lengthp));
2026 }
2027 
2028 /*
2029  *
2030  * ddi_getlongprop_buf:		Get long prop into pre-allocated callers
2031  *				buffer. (no memory allocation by provider).
2032  *
2033  *	dev_t:	Input:	dev_t of property.
2034  *	dip:	Input:	dev_info_t pointer of child.
2035  *	flags:	Input:	DDI_PROP_DONTPASS or NULL
2036  *	name:	Input:	name of property
2037  *	valuep:	Input:	ptr to callers buffer.
2038  *	lengthp:I/O:	ptr to length of callers buffer on entry,
2039  *			actual length of property on exit.
2040  *
2041  *	Possible returns:
2042  *
2043  *		DDI_PROP_SUCCESS	Prop found and returned
2044  *		DDI_PROP_NOT_FOUND	Prop not found
2045  *		DDI_PROP_UNDEFINED	Prop explicitly undefined.
2046  *		DDI_PROP_BUF_TOO_SMALL	Prop found, callers buf too small,
2047  *					no value returned, but actual prop
2048  *					length returned in *lengthp
2049  *
2050  */
2051 
2052 int
2053 ddi_getlongprop_buf(dev_t dev, dev_info_t *dip, int flags,
2054     char *name, caddr_t valuep, int *lengthp)
2055 {
2056 	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
2057 	    flags, name, valuep, lengthp));
2058 }
2059 
2060 /*
2061  * Integer/boolean sized props.
2062  *
2063  * Call is value only... returns found boolean or int sized prop value or
2064  * defvalue if prop not found or is wrong length or is explicitly undefined.
2065  * Only flag is DDI_PROP_DONTPASS...
2066  *
2067  * By convention, this interface returns boolean (0) sized properties
2068  * as value (int)1.
2069  *
2070  * This never returns an error, if property not found or specifically
2071  * undefined, the input `defvalue' is returned.
2072  */
2073 
2074 int
2075 ddi_getprop(dev_t dev, dev_info_t *dip, int flags, char *name, int defvalue)
2076 {
2077 	int	propvalue = defvalue;
2078 	int	proplength = sizeof (int);
2079 	int	error;
2080 
2081 	error = ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
2082 	    flags, name, (caddr_t)&propvalue, &proplength);
2083 
2084 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
2085 		propvalue = 1;
2086 
2087 	return (propvalue);
2088 }
2089 
2090 /*
2091  * Get prop length interface: flags are 0 or DDI_PROP_DONTPASS
2092  * if returns DDI_PROP_SUCCESS, length returned in *lengthp.
2093  */
2094 
2095 int
2096 ddi_getproplen(dev_t dev, dev_info_t *dip, int flags, char *name, int *lengthp)
2097 {
2098 	return (ddi_prop_op(dev, dip, PROP_LEN, flags, name, NULL, lengthp));
2099 }
2100 
2101 /*
2102  * Allocate a struct prop_driver_data, along with 'size' bytes
2103  * for decoded property data.  This structure is freed by
2104  * calling ddi_prop_free(9F).
2105  */
2106 static void *
2107 ddi_prop_decode_alloc(size_t size, void (*prop_free)(struct prop_driver_data *))
2108 {
2109 	struct prop_driver_data *pdd;
2110 
2111 	/*
2112 	 * Allocate a structure with enough memory to store the decoded data.
2113 	 */
2114 	pdd = kmem_zalloc(sizeof (struct prop_driver_data) + size, KM_SLEEP);
2115 	pdd->pdd_size = (sizeof (struct prop_driver_data) + size);
2116 	pdd->pdd_prop_free = prop_free;
2117 
2118 	/*
2119 	 * Return a pointer to the location to put the decoded data.
2120 	 */
2121 	return ((void *)((caddr_t)pdd + sizeof (struct prop_driver_data)));
2122 }
2123 
2124 /*
2125  * Allocated the memory needed to store the encoded data in the property
2126  * handle.
2127  */
2128 static int
2129 ddi_prop_encode_alloc(prop_handle_t *ph, size_t size)
2130 {
2131 	/*
2132 	 * If size is zero, then set data to NULL and size to 0.  This
2133 	 * is a boolean property.
2134 	 */
2135 	if (size == 0) {
2136 		ph->ph_size = 0;
2137 		ph->ph_data = NULL;
2138 		ph->ph_cur_pos = NULL;
2139 		ph->ph_save_pos = NULL;
2140 	} else {
2141 		if (ph->ph_flags == DDI_PROP_DONTSLEEP) {
2142 			ph->ph_data = kmem_zalloc(size, KM_NOSLEEP);
2143 			if (ph->ph_data == NULL)
2144 				return (DDI_PROP_NO_MEMORY);
2145 		} else
2146 			ph->ph_data = kmem_zalloc(size, KM_SLEEP);
2147 		ph->ph_size = size;
2148 		ph->ph_cur_pos = ph->ph_data;
2149 		ph->ph_save_pos = ph->ph_data;
2150 	}
2151 	return (DDI_PROP_SUCCESS);
2152 }
2153 
2154 /*
2155  * Free the space allocated by the lookup routines.  Each lookup routine
2156  * returns a pointer to the decoded data to the driver.  The driver then
2157  * passes this pointer back to us.  This data actually lives in a struct
2158  * prop_driver_data.  We use negative indexing to find the beginning of
2159  * the structure and then free the entire structure using the size and
2160  * the free routine stored in the structure.
2161  */
2162 void
2163 ddi_prop_free(void *datap)
2164 {
2165 	struct prop_driver_data *pdd;
2166 
2167 	/*
2168 	 * Get the structure
2169 	 */
2170 	pdd = (struct prop_driver_data *)
2171 	    ((caddr_t)datap - sizeof (struct prop_driver_data));
2172 	/*
2173 	 * Call the free routine to free it
2174 	 */
2175 	(*pdd->pdd_prop_free)(pdd);
2176 }
2177 
2178 /*
2179  * Free the data associated with an array of ints,
2180  * allocated with ddi_prop_decode_alloc().
2181  */
2182 static void
2183 ddi_prop_free_ints(struct prop_driver_data *pdd)
2184 {
2185 	kmem_free(pdd, pdd->pdd_size);
2186 }
2187 
2188 /*
2189  * Free a single string property or a single string contained within
2190  * the argv style return value of an array of strings.
2191  */
2192 static void
2193 ddi_prop_free_string(struct prop_driver_data *pdd)
2194 {
2195 	kmem_free(pdd, pdd->pdd_size);
2196 
2197 }
2198 
2199 /*
2200  * Free an array of strings.
2201  */
2202 static void
2203 ddi_prop_free_strings(struct prop_driver_data *pdd)
2204 {
2205 	kmem_free(pdd, pdd->pdd_size);
2206 }
2207 
2208 /*
2209  * Free the data associated with an array of bytes.
2210  */
2211 static void
2212 ddi_prop_free_bytes(struct prop_driver_data *pdd)
2213 {
2214 	kmem_free(pdd, pdd->pdd_size);
2215 }
2216 
2217 /*
2218  * Reset the current location pointer in the property handle to the
2219  * beginning of the data.
2220  */
2221 void
2222 ddi_prop_reset_pos(prop_handle_t *ph)
2223 {
2224 	ph->ph_cur_pos = ph->ph_data;
2225 	ph->ph_save_pos = ph->ph_data;
2226 }
2227 
2228 /*
2229  * Restore the current location pointer in the property handle to the
2230  * saved position.
2231  */
2232 void
2233 ddi_prop_save_pos(prop_handle_t *ph)
2234 {
2235 	ph->ph_save_pos = ph->ph_cur_pos;
2236 }
2237 
2238 /*
2239  * Save the location that the current location pointer is pointing to..
2240  */
2241 void
2242 ddi_prop_restore_pos(prop_handle_t *ph)
2243 {
2244 	ph->ph_cur_pos = ph->ph_save_pos;
2245 }
2246 
2247 /*
2248  * Property encode/decode functions
2249  */
2250 
2251 /*
2252  * Decode a single integer property
2253  */
2254 static int
2255 ddi_prop_fm_decode_int(prop_handle_t *ph, void *data, uint_t *nelements)
2256 {
2257 	int	i;
2258 	int	tmp;
2259 
2260 	/*
2261 	 * If there is nothing to decode return an error
2262 	 */
2263 	if (ph->ph_size == 0)
2264 		return (DDI_PROP_END_OF_DATA);
2265 
2266 	/*
2267 	 * Decode the property as a single integer and return it
2268 	 * in data if we were able to decode it.
2269 	 */
2270 	i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, &tmp);
2271 	if (i < DDI_PROP_RESULT_OK) {
2272 		switch (i) {
2273 		case DDI_PROP_RESULT_EOF:
2274 			return (DDI_PROP_END_OF_DATA);
2275 
2276 		case DDI_PROP_RESULT_ERROR:
2277 			return (DDI_PROP_CANNOT_DECODE);
2278 		}
2279 	}
2280 
2281 	*(int *)data = tmp;
2282 	*nelements = 1;
2283 	return (DDI_PROP_SUCCESS);
2284 }
2285 
2286 /*
2287  * Decode a single 64 bit integer property
2288  */
2289 static int
2290 ddi_prop_fm_decode_int64(prop_handle_t *ph, void *data, uint_t *nelements)
2291 {
2292 	int	i;
2293 	int64_t	tmp;
2294 
2295 	/*
2296 	 * If there is nothing to decode return an error
2297 	 */
2298 	if (ph->ph_size == 0)
2299 		return (DDI_PROP_END_OF_DATA);
2300 
2301 	/*
2302 	 * Decode the property as a single integer and return it
2303 	 * in data if we were able to decode it.
2304 	 */
2305 	i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, &tmp);
2306 	if (i < DDI_PROP_RESULT_OK) {
2307 		switch (i) {
2308 		case DDI_PROP_RESULT_EOF:
2309 			return (DDI_PROP_END_OF_DATA);
2310 
2311 		case DDI_PROP_RESULT_ERROR:
2312 			return (DDI_PROP_CANNOT_DECODE);
2313 		}
2314 	}
2315 
2316 	*(int64_t *)data = tmp;
2317 	*nelements = 1;
2318 	return (DDI_PROP_SUCCESS);
2319 }
2320 
2321 /*
2322  * Decode an array of integers property
2323  */
2324 static int
2325 ddi_prop_fm_decode_ints(prop_handle_t *ph, void *data, uint_t *nelements)
2326 {
2327 	int	i;
2328 	int	cnt = 0;
2329 	int	*tmp;
2330 	int	*intp;
2331 	int	n;
2332 
2333 	/*
2334 	 * Figure out how many array elements there are by going through the
2335 	 * data without decoding it first and counting.
2336 	 */
2337 	for (;;) {
2338 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_SKIP, NULL);
2339 		if (i < 0)
2340 			break;
2341 		cnt++;
2342 	}
2343 
2344 	/*
2345 	 * If there are no elements return an error
2346 	 */
2347 	if (cnt == 0)
2348 		return (DDI_PROP_END_OF_DATA);
2349 
2350 	/*
2351 	 * If we cannot skip through the data, we cannot decode it
2352 	 */
2353 	if (i == DDI_PROP_RESULT_ERROR)
2354 		return (DDI_PROP_CANNOT_DECODE);
2355 
2356 	/*
2357 	 * Reset the data pointer to the beginning of the encoded data
2358 	 */
2359 	ddi_prop_reset_pos(ph);
2360 
2361 	/*
2362 	 * Allocated memory to store the decoded value in.
2363 	 */
2364 	intp = ddi_prop_decode_alloc((cnt * sizeof (int)),
2365 	    ddi_prop_free_ints);
2366 
2367 	/*
2368 	 * Decode each element and place it in the space we just allocated
2369 	 */
2370 	tmp = intp;
2371 	for (n = 0; n < cnt; n++, tmp++) {
2372 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, tmp);
2373 		if (i < DDI_PROP_RESULT_OK) {
2374 			/*
2375 			 * Free the space we just allocated
2376 			 * and return an error.
2377 			 */
2378 			ddi_prop_free(intp);
2379 			switch (i) {
2380 			case DDI_PROP_RESULT_EOF:
2381 				return (DDI_PROP_END_OF_DATA);
2382 
2383 			case DDI_PROP_RESULT_ERROR:
2384 				return (DDI_PROP_CANNOT_DECODE);
2385 			}
2386 		}
2387 	}
2388 
2389 	*nelements = cnt;
2390 	*(int **)data = intp;
2391 
2392 	return (DDI_PROP_SUCCESS);
2393 }
2394 
2395 /*
2396  * Decode a 64 bit integer array property
2397  */
2398 static int
2399 ddi_prop_fm_decode_int64_array(prop_handle_t *ph, void *data, uint_t *nelements)
2400 {
2401 	int	i;
2402 	int	n;
2403 	int	cnt = 0;
2404 	int64_t	*tmp;
2405 	int64_t	*intp;
2406 
2407 	/*
2408 	 * Count the number of array elements by going
2409 	 * through the data without decoding it.
2410 	 */
2411 	for (;;) {
2412 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_SKIP, NULL);
2413 		if (i < 0)
2414 			break;
2415 		cnt++;
2416 	}
2417 
2418 	/*
2419 	 * If there are no elements return an error
2420 	 */
2421 	if (cnt == 0)
2422 		return (DDI_PROP_END_OF_DATA);
2423 
2424 	/*
2425 	 * If we cannot skip through the data, we cannot decode it
2426 	 */
2427 	if (i == DDI_PROP_RESULT_ERROR)
2428 		return (DDI_PROP_CANNOT_DECODE);
2429 
2430 	/*
2431 	 * Reset the data pointer to the beginning of the encoded data
2432 	 */
2433 	ddi_prop_reset_pos(ph);
2434 
2435 	/*
2436 	 * Allocate memory to store the decoded value.
2437 	 */
2438 	intp = ddi_prop_decode_alloc((cnt * sizeof (int64_t)),
2439 	    ddi_prop_free_ints);
2440 
2441 	/*
2442 	 * Decode each element and place it in the space allocated
2443 	 */
2444 	tmp = intp;
2445 	for (n = 0; n < cnt; n++, tmp++) {
2446 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, tmp);
2447 		if (i < DDI_PROP_RESULT_OK) {
2448 			/*
2449 			 * Free the space we just allocated
2450 			 * and return an error.
2451 			 */
2452 			ddi_prop_free(intp);
2453 			switch (i) {
2454 			case DDI_PROP_RESULT_EOF:
2455 				return (DDI_PROP_END_OF_DATA);
2456 
2457 			case DDI_PROP_RESULT_ERROR:
2458 				return (DDI_PROP_CANNOT_DECODE);
2459 			}
2460 		}
2461 	}
2462 
2463 	*nelements = cnt;
2464 	*(int64_t **)data = intp;
2465 
2466 	return (DDI_PROP_SUCCESS);
2467 }
2468 
2469 /*
2470  * Encode an array of integers property (Can be one element)
2471  */
2472 int
2473 ddi_prop_fm_encode_ints(prop_handle_t *ph, void *data, uint_t nelements)
2474 {
2475 	int	i;
2476 	int	*tmp;
2477 	int	cnt;
2478 	int	size;
2479 
2480 	/*
2481 	 * If there is no data, we cannot do anything
2482 	 */
2483 	if (nelements == 0)
2484 		return (DDI_PROP_CANNOT_ENCODE);
2485 
2486 	/*
2487 	 * Get the size of an encoded int.
2488 	 */
2489 	size = DDI_PROP_INT(ph, DDI_PROP_CMD_GET_ESIZE, NULL);
2490 
2491 	if (size < DDI_PROP_RESULT_OK) {
2492 		switch (size) {
2493 		case DDI_PROP_RESULT_EOF:
2494 			return (DDI_PROP_END_OF_DATA);
2495 
2496 		case DDI_PROP_RESULT_ERROR:
2497 			return (DDI_PROP_CANNOT_ENCODE);
2498 		}
2499 	}
2500 
2501 	/*
2502 	 * Allocate space in the handle to store the encoded int.
2503 	 */
2504 	if (ddi_prop_encode_alloc(ph, size * nelements) !=
2505 	    DDI_PROP_SUCCESS)
2506 		return (DDI_PROP_NO_MEMORY);
2507 
2508 	/*
2509 	 * Encode the array of ints.
2510 	 */
2511 	tmp = (int *)data;
2512 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
2513 		i = DDI_PROP_INT(ph, DDI_PROP_CMD_ENCODE, tmp);
2514 		if (i < DDI_PROP_RESULT_OK) {
2515 			switch (i) {
2516 			case DDI_PROP_RESULT_EOF:
2517 				return (DDI_PROP_END_OF_DATA);
2518 
2519 			case DDI_PROP_RESULT_ERROR:
2520 				return (DDI_PROP_CANNOT_ENCODE);
2521 			}
2522 		}
2523 	}
2524 
2525 	return (DDI_PROP_SUCCESS);
2526 }
2527 
2528 
2529 /*
2530  * Encode a 64 bit integer array property
2531  */
2532 int
2533 ddi_prop_fm_encode_int64(prop_handle_t *ph, void *data, uint_t nelements)
2534 {
2535 	int i;
2536 	int cnt;
2537 	int size;
2538 	int64_t *tmp;
2539 
2540 	/*
2541 	 * If there is no data, we cannot do anything
2542 	 */
2543 	if (nelements == 0)
2544 		return (DDI_PROP_CANNOT_ENCODE);
2545 
2546 	/*
2547 	 * Get the size of an encoded 64 bit int.
2548 	 */
2549 	size = DDI_PROP_INT64(ph, DDI_PROP_CMD_GET_ESIZE, NULL);
2550 
2551 	if (size < DDI_PROP_RESULT_OK) {
2552 		switch (size) {
2553 		case DDI_PROP_RESULT_EOF:
2554 			return (DDI_PROP_END_OF_DATA);
2555 
2556 		case DDI_PROP_RESULT_ERROR:
2557 			return (DDI_PROP_CANNOT_ENCODE);
2558 		}
2559 	}
2560 
2561 	/*
2562 	 * Allocate space in the handle to store the encoded int.
2563 	 */
2564 	if (ddi_prop_encode_alloc(ph, size * nelements) !=
2565 	    DDI_PROP_SUCCESS)
2566 		return (DDI_PROP_NO_MEMORY);
2567 
2568 	/*
2569 	 * Encode the array of ints.
2570 	 */
2571 	tmp = (int64_t *)data;
2572 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
2573 		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_ENCODE, tmp);
2574 		if (i < DDI_PROP_RESULT_OK) {
2575 			switch (i) {
2576 			case DDI_PROP_RESULT_EOF:
2577 				return (DDI_PROP_END_OF_DATA);
2578 
2579 			case DDI_PROP_RESULT_ERROR:
2580 				return (DDI_PROP_CANNOT_ENCODE);
2581 			}
2582 		}
2583 	}
2584 
2585 	return (DDI_PROP_SUCCESS);
2586 }
2587 
2588 /*
2589  * Decode a single string property
2590  */
2591 static int
2592 ddi_prop_fm_decode_string(prop_handle_t *ph, void *data, uint_t *nelements)
2593 {
2594 	char		*tmp;
2595 	char		*str;
2596 	int		i;
2597 	int		size;
2598 
2599 	/*
2600 	 * If there is nothing to decode return an error
2601 	 */
2602 	if (ph->ph_size == 0)
2603 		return (DDI_PROP_END_OF_DATA);
2604 
2605 	/*
2606 	 * Get the decoded size of the encoded string.
2607 	 */
2608 	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
2609 	if (size < DDI_PROP_RESULT_OK) {
2610 		switch (size) {
2611 		case DDI_PROP_RESULT_EOF:
2612 			return (DDI_PROP_END_OF_DATA);
2613 
2614 		case DDI_PROP_RESULT_ERROR:
2615 			return (DDI_PROP_CANNOT_DECODE);
2616 		}
2617 	}
2618 
2619 	/*
2620 	 * Allocated memory to store the decoded value in.
2621 	 */
2622 	str = ddi_prop_decode_alloc((size_t)size, ddi_prop_free_string);
2623 
2624 	ddi_prop_reset_pos(ph);
2625 
2626 	/*
2627 	 * Decode the str and place it in the space we just allocated
2628 	 */
2629 	tmp = str;
2630 	i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, tmp);
2631 	if (i < DDI_PROP_RESULT_OK) {
2632 		/*
2633 		 * Free the space we just allocated
2634 		 * and return an error.
2635 		 */
2636 		ddi_prop_free(str);
2637 		switch (i) {
2638 		case DDI_PROP_RESULT_EOF:
2639 			return (DDI_PROP_END_OF_DATA);
2640 
2641 		case DDI_PROP_RESULT_ERROR:
2642 			return (DDI_PROP_CANNOT_DECODE);
2643 		}
2644 	}
2645 
2646 	*(char **)data = str;
2647 	*nelements = 1;
2648 
2649 	return (DDI_PROP_SUCCESS);
2650 }
2651 
2652 /*
2653  * Decode an array of strings.
2654  */
2655 int
2656 ddi_prop_fm_decode_strings(prop_handle_t *ph, void *data, uint_t *nelements)
2657 {
2658 	int		cnt = 0;
2659 	char		**strs;
2660 	char		**tmp;
2661 	char		*ptr;
2662 	int		i;
2663 	int		n;
2664 	int		size;
2665 	size_t		nbytes;
2666 
2667 	/*
2668 	 * Figure out how many array elements there are by going through the
2669 	 * data without decoding it first and counting.
2670 	 */
2671 	for (;;) {
2672 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_SKIP, NULL);
2673 		if (i < 0)
2674 			break;
2675 		cnt++;
2676 	}
2677 
2678 	/*
2679 	 * If there are no elements return an error
2680 	 */
2681 	if (cnt == 0)
2682 		return (DDI_PROP_END_OF_DATA);
2683 
2684 	/*
2685 	 * If we cannot skip through the data, we cannot decode it
2686 	 */
2687 	if (i == DDI_PROP_RESULT_ERROR)
2688 		return (DDI_PROP_CANNOT_DECODE);
2689 
2690 	/*
2691 	 * Reset the data pointer to the beginning of the encoded data
2692 	 */
2693 	ddi_prop_reset_pos(ph);
2694 
2695 	/*
2696 	 * Figure out how much memory we need for the sum total
2697 	 */
2698 	nbytes = (cnt + 1) * sizeof (char *);
2699 
2700 	for (n = 0; n < cnt; n++) {
2701 		/*
2702 		 * Get the decoded size of the current encoded string.
2703 		 */
2704 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
2705 		if (size < DDI_PROP_RESULT_OK) {
2706 			switch (size) {
2707 			case DDI_PROP_RESULT_EOF:
2708 				return (DDI_PROP_END_OF_DATA);
2709 
2710 			case DDI_PROP_RESULT_ERROR:
2711 				return (DDI_PROP_CANNOT_DECODE);
2712 			}
2713 		}
2714 
2715 		nbytes += size;
2716 	}
2717 
2718 	/*
2719 	 * Allocate memory in which to store the decoded strings.
2720 	 */
2721 	strs = ddi_prop_decode_alloc(nbytes, ddi_prop_free_strings);
2722 
2723 	/*
2724 	 * Set up pointers for each string by figuring out yet
2725 	 * again how long each string is.
2726 	 */
2727 	ddi_prop_reset_pos(ph);
2728 	ptr = (caddr_t)strs + ((cnt + 1) * sizeof (char *));
2729 	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
2730 		/*
2731 		 * Get the decoded size of the current encoded string.
2732 		 */
2733 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
2734 		if (size < DDI_PROP_RESULT_OK) {
2735 			ddi_prop_free(strs);
2736 			switch (size) {
2737 			case DDI_PROP_RESULT_EOF:
2738 				return (DDI_PROP_END_OF_DATA);
2739 
2740 			case DDI_PROP_RESULT_ERROR:
2741 				return (DDI_PROP_CANNOT_DECODE);
2742 			}
2743 		}
2744 
2745 		*tmp = ptr;
2746 		ptr += size;
2747 	}
2748 
2749 	/*
2750 	 * String array is terminated by a NULL
2751 	 */
2752 	*tmp = NULL;
2753 
2754 	/*
2755 	 * Finally, we can decode each string
2756 	 */
2757 	ddi_prop_reset_pos(ph);
2758 	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
2759 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, *tmp);
2760 		if (i < DDI_PROP_RESULT_OK) {
2761 			/*
2762 			 * Free the space we just allocated
2763 			 * and return an error
2764 			 */
2765 			ddi_prop_free(strs);
2766 			switch (i) {
2767 			case DDI_PROP_RESULT_EOF:
2768 				return (DDI_PROP_END_OF_DATA);
2769 
2770 			case DDI_PROP_RESULT_ERROR:
2771 				return (DDI_PROP_CANNOT_DECODE);
2772 			}
2773 		}
2774 	}
2775 
2776 	*(char ***)data = strs;
2777 	*nelements = cnt;
2778 
2779 	return (DDI_PROP_SUCCESS);
2780 }
2781 
2782 /*
2783  * Encode a string.
2784  */
2785 int
2786 ddi_prop_fm_encode_string(prop_handle_t *ph, void *data, uint_t nelements)
2787 {
2788 	char		**tmp;
2789 	int		size;
2790 	int		i;
2791 
2792 	/*
2793 	 * If there is no data, we cannot do anything
2794 	 */
2795 	if (nelements == 0)
2796 		return (DDI_PROP_CANNOT_ENCODE);
2797 
2798 	/*
2799 	 * Get the size of the encoded string.
2800 	 */
2801 	tmp = (char **)data;
2802 	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
2803 	if (size < DDI_PROP_RESULT_OK) {
2804 		switch (size) {
2805 		case DDI_PROP_RESULT_EOF:
2806 			return (DDI_PROP_END_OF_DATA);
2807 
2808 		case DDI_PROP_RESULT_ERROR:
2809 			return (DDI_PROP_CANNOT_ENCODE);
2810 		}
2811 	}
2812 
2813 	/*
2814 	 * Allocate space in the handle to store the encoded string.
2815 	 */
2816 	if (ddi_prop_encode_alloc(ph, size) != DDI_PROP_SUCCESS)
2817 		return (DDI_PROP_NO_MEMORY);
2818 
2819 	ddi_prop_reset_pos(ph);
2820 
2821 	/*
2822 	 * Encode the string.
2823 	 */
2824 	tmp = (char **)data;
2825 	i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
2826 	if (i < DDI_PROP_RESULT_OK) {
2827 		switch (i) {
2828 		case DDI_PROP_RESULT_EOF:
2829 			return (DDI_PROP_END_OF_DATA);
2830 
2831 		case DDI_PROP_RESULT_ERROR:
2832 			return (DDI_PROP_CANNOT_ENCODE);
2833 		}
2834 	}
2835 
2836 	return (DDI_PROP_SUCCESS);
2837 }
2838 
2839 
2840 /*
2841  * Encode an array of strings.
2842  */
2843 int
2844 ddi_prop_fm_encode_strings(prop_handle_t *ph, void *data, uint_t nelements)
2845 {
2846 	int		cnt = 0;
2847 	char		**tmp;
2848 	int		size;
2849 	uint_t		total_size;
2850 	int		i;
2851 
2852 	/*
2853 	 * If there is no data, we cannot do anything
2854 	 */
2855 	if (nelements == 0)
2856 		return (DDI_PROP_CANNOT_ENCODE);
2857 
2858 	/*
2859 	 * Get the total size required to encode all the strings.
2860 	 */
2861 	total_size = 0;
2862 	tmp = (char **)data;
2863 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
2864 		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
2865 		if (size < DDI_PROP_RESULT_OK) {
2866 			switch (size) {
2867 			case DDI_PROP_RESULT_EOF:
2868 				return (DDI_PROP_END_OF_DATA);
2869 
2870 			case DDI_PROP_RESULT_ERROR:
2871 				return (DDI_PROP_CANNOT_ENCODE);
2872 			}
2873 		}
2874 		total_size += (uint_t)size;
2875 	}
2876 
2877 	/*
2878 	 * Allocate space in the handle to store the encoded strings.
2879 	 */
2880 	if (ddi_prop_encode_alloc(ph, total_size) != DDI_PROP_SUCCESS)
2881 		return (DDI_PROP_NO_MEMORY);
2882 
2883 	ddi_prop_reset_pos(ph);
2884 
2885 	/*
2886 	 * Encode the array of strings.
2887 	 */
2888 	tmp = (char **)data;
2889 	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
2890 		i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
2891 		if (i < DDI_PROP_RESULT_OK) {
2892 			switch (i) {
2893 			case DDI_PROP_RESULT_EOF:
2894 				return (DDI_PROP_END_OF_DATA);
2895 
2896 			case DDI_PROP_RESULT_ERROR:
2897 				return (DDI_PROP_CANNOT_ENCODE);
2898 			}
2899 		}
2900 	}
2901 
2902 	return (DDI_PROP_SUCCESS);
2903 }
2904 
2905 
2906 /*
2907  * Decode an array of bytes.
2908  */
2909 static int
2910 ddi_prop_fm_decode_bytes(prop_handle_t *ph, void *data, uint_t *nelements)
2911 {
2912 	uchar_t		*tmp;
2913 	int		nbytes;
2914 	int		i;
2915 
2916 	/*
2917 	 * If there are no elements return an error
2918 	 */
2919 	if (ph->ph_size == 0)
2920 		return (DDI_PROP_END_OF_DATA);
2921 
2922 	/*
2923 	 * Get the size of the encoded array of bytes.
2924 	 */
2925 	nbytes = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_DSIZE,
2926 	    data, ph->ph_size);
2927 	if (nbytes < DDI_PROP_RESULT_OK) {
2928 		switch (nbytes) {
2929 		case DDI_PROP_RESULT_EOF:
2930 			return (DDI_PROP_END_OF_DATA);
2931 
2932 		case DDI_PROP_RESULT_ERROR:
2933 			return (DDI_PROP_CANNOT_DECODE);
2934 		}
2935 	}
2936 
2937 	/*
2938 	 * Allocated memory to store the decoded value in.
2939 	 */
2940 	tmp = ddi_prop_decode_alloc(nbytes, ddi_prop_free_bytes);
2941 
2942 	/*
2943 	 * Decode each element and place it in the space we just allocated
2944 	 */
2945 	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_DECODE, tmp, nbytes);
2946 	if (i < DDI_PROP_RESULT_OK) {
2947 		/*
2948 		 * Free the space we just allocated
2949 		 * and return an error
2950 		 */
2951 		ddi_prop_free(tmp);
2952 		switch (i) {
2953 		case DDI_PROP_RESULT_EOF:
2954 			return (DDI_PROP_END_OF_DATA);
2955 
2956 		case DDI_PROP_RESULT_ERROR:
2957 			return (DDI_PROP_CANNOT_DECODE);
2958 		}
2959 	}
2960 
2961 	*(uchar_t **)data = tmp;
2962 	*nelements = nbytes;
2963 
2964 	return (DDI_PROP_SUCCESS);
2965 }
2966 
2967 /*
2968  * Encode an array of bytes.
2969  */
2970 int
2971 ddi_prop_fm_encode_bytes(prop_handle_t *ph, void *data, uint_t nelements)
2972 {
2973 	int		size;
2974 	int		i;
2975 
2976 	/*
2977 	 * If there are no elements, then this is a boolean property,
2978 	 * so just create a property handle with no data and return.
2979 	 */
2980 	if (nelements == 0) {
2981 		(void) ddi_prop_encode_alloc(ph, 0);
2982 		return (DDI_PROP_SUCCESS);
2983 	}
2984 
2985 	/*
2986 	 * Get the size of the encoded array of bytes.
2987 	 */
2988 	size = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_ESIZE, (uchar_t *)data,
2989 	    nelements);
2990 	if (size < DDI_PROP_RESULT_OK) {
2991 		switch (size) {
2992 		case DDI_PROP_RESULT_EOF:
2993 			return (DDI_PROP_END_OF_DATA);
2994 
2995 		case DDI_PROP_RESULT_ERROR:
2996 			return (DDI_PROP_CANNOT_DECODE);
2997 		}
2998 	}
2999 
3000 	/*
3001 	 * Allocate space in the handle to store the encoded bytes.
3002 	 */
3003 	if (ddi_prop_encode_alloc(ph, (uint_t)size) != DDI_PROP_SUCCESS)
3004 		return (DDI_PROP_NO_MEMORY);
3005 
3006 	/*
3007 	 * Encode the array of bytes.
3008 	 */
3009 	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_ENCODE, (uchar_t *)data,
3010 	    nelements);
3011 	if (i < DDI_PROP_RESULT_OK) {
3012 		switch (i) {
3013 		case DDI_PROP_RESULT_EOF:
3014 			return (DDI_PROP_END_OF_DATA);
3015 
3016 		case DDI_PROP_RESULT_ERROR:
3017 			return (DDI_PROP_CANNOT_ENCODE);
3018 		}
3019 	}
3020 
3021 	return (DDI_PROP_SUCCESS);
3022 }
3023 
3024 /*
3025  * OBP 1275 integer, string and byte operators.
3026  *
3027  * DDI_PROP_CMD_DECODE:
3028  *
3029  *	DDI_PROP_RESULT_ERROR:		cannot decode the data
3030  *	DDI_PROP_RESULT_EOF:		end of data
3031  *	DDI_PROP_OK:			data was decoded
3032  *
3033  * DDI_PROP_CMD_ENCODE:
3034  *
3035  *	DDI_PROP_RESULT_ERROR:		cannot encode the data
3036  *	DDI_PROP_RESULT_EOF:		end of data
3037  *	DDI_PROP_OK:			data was encoded
3038  *
3039  * DDI_PROP_CMD_SKIP:
3040  *
3041  *	DDI_PROP_RESULT_ERROR:		cannot skip the data
3042  *	DDI_PROP_RESULT_EOF:		end of data
3043  *	DDI_PROP_OK:			data was skipped
3044  *
3045  * DDI_PROP_CMD_GET_ESIZE:
3046  *
3047  *	DDI_PROP_RESULT_ERROR:		cannot get encoded size
3048  *	DDI_PROP_RESULT_EOF:		end of data
3049  *	> 0:				the encoded size
3050  *
3051  * DDI_PROP_CMD_GET_DSIZE:
3052  *
3053  *	DDI_PROP_RESULT_ERROR:		cannot get decoded size
3054  *	DDI_PROP_RESULT_EOF:		end of data
3055  *	> 0:				the decoded size
3056  */
3057 
3058 /*
3059  * OBP 1275 integer operator
3060  *
3061  * OBP properties are a byte stream of data, so integers may not be
3062  * properly aligned.  Therefore we need to copy them one byte at a time.
3063  */
3064 int
3065 ddi_prop_1275_int(prop_handle_t *ph, uint_t cmd, int *data)
3066 {
3067 	int	i;
3068 
3069 	switch (cmd) {
3070 	case DDI_PROP_CMD_DECODE:
3071 		/*
3072 		 * Check that there is encoded data
3073 		 */
3074 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
3075 			return (DDI_PROP_RESULT_ERROR);
3076 		if (ph->ph_flags & PH_FROM_PROM) {
3077 			i = MIN(ph->ph_size, PROP_1275_INT_SIZE);
3078 			if ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3079 			    ph->ph_size - i))
3080 				return (DDI_PROP_RESULT_ERROR);
3081 		} else {
3082 			if (ph->ph_size < sizeof (int) ||
3083 			    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3084 			    ph->ph_size - sizeof (int))))
3085 				return (DDI_PROP_RESULT_ERROR);
3086 		}
3087 
3088 		/*
3089 		 * Copy the integer, using the implementation-specific
3090 		 * copy function if the property is coming from the PROM.
3091 		 */
3092 		if (ph->ph_flags & PH_FROM_PROM) {
3093 			*data = impl_ddi_prop_int_from_prom(
3094 			    (uchar_t *)ph->ph_cur_pos,
3095 			    (ph->ph_size < PROP_1275_INT_SIZE) ?
3096 			    ph->ph_size : PROP_1275_INT_SIZE);
3097 		} else {
3098 			bcopy(ph->ph_cur_pos, data, sizeof (int));
3099 		}
3100 
3101 		/*
3102 		 * Move the current location to the start of the next
3103 		 * bit of undecoded data.
3104 		 */
3105 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
3106 		    PROP_1275_INT_SIZE;
3107 		return (DDI_PROP_RESULT_OK);
3108 
3109 	case DDI_PROP_CMD_ENCODE:
3110 		/*
3111 		 * Check that there is room to encoded the data
3112 		 */
3113 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3114 		    ph->ph_size < PROP_1275_INT_SIZE ||
3115 		    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
3116 		    ph->ph_size - sizeof (int))))
3117 			return (DDI_PROP_RESULT_ERROR);
3118 
3119 		/*
3120 		 * Encode the integer into the byte stream one byte at a
3121 		 * time.
3122 		 */
3123 		bcopy(data, ph->ph_cur_pos, sizeof (int));
3124 
3125 		/*
3126 		 * Move the current location to the start of the next bit of
3127 		 * space where we can store encoded data.
3128 		 */
3129 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
3130 		return (DDI_PROP_RESULT_OK);
3131 
3132 	case DDI_PROP_CMD_SKIP:
3133 		/*
3134 		 * Check that there is encoded data
3135 		 */
3136 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3137 		    ph->ph_size < PROP_1275_INT_SIZE)
3138 			return (DDI_PROP_RESULT_ERROR);
3139 
3140 
3141 		if ((caddr_t)ph->ph_cur_pos ==
3142 		    (caddr_t)ph->ph_data + ph->ph_size) {
3143 			return (DDI_PROP_RESULT_EOF);
3144 		} else if ((caddr_t)ph->ph_cur_pos >
3145 		    (caddr_t)ph->ph_data + ph->ph_size) {
3146 			return (DDI_PROP_RESULT_EOF);
3147 		}
3148 
3149 		/*
3150 		 * Move the current location to the start of the next bit of
3151 		 * undecoded data.
3152 		 */
3153 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
3154 		return (DDI_PROP_RESULT_OK);
3155 
3156 	case DDI_PROP_CMD_GET_ESIZE:
3157 		/*
3158 		 * Return the size of an encoded integer on OBP
3159 		 */
3160 		return (PROP_1275_INT_SIZE);
3161 
3162 	case DDI_PROP_CMD_GET_DSIZE:
3163 		/*
3164 		 * Return the size of a decoded integer on the system.
3165 		 */
3166 		return (sizeof (int));
3167 
3168 	default:
3169 #ifdef DEBUG
3170 		panic("ddi_prop_1275_int: %x impossible", cmd);
3171 		/*NOTREACHED*/
3172 #else
3173 		return (DDI_PROP_RESULT_ERROR);
3174 #endif	/* DEBUG */
3175 	}
3176 }
3177 
3178 /*
3179  * 64 bit integer operator.
3180  *
3181  * This is an extension, defined by Sun, to the 1275 integer
3182  * operator.  This routine handles the encoding/decoding of
3183  * 64 bit integer properties.
3184  */
3185 int
3186 ddi_prop_int64_op(prop_handle_t *ph, uint_t cmd, int64_t *data)
3187 {
3188 
3189 	switch (cmd) {
3190 	case DDI_PROP_CMD_DECODE:
3191 		/*
3192 		 * Check that there is encoded data
3193 		 */
3194 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
3195 			return (DDI_PROP_RESULT_ERROR);
3196 		if (ph->ph_flags & PH_FROM_PROM) {
3197 			return (DDI_PROP_RESULT_ERROR);
3198 		} else {
3199 			if (ph->ph_size < sizeof (int64_t) ||
3200 			    ((int64_t *)ph->ph_cur_pos >
3201 			    ((int64_t *)ph->ph_data +
3202 			    ph->ph_size - sizeof (int64_t))))
3203 				return (DDI_PROP_RESULT_ERROR);
3204 		}
3205 		/*
3206 		 * Copy the integer, using the implementation-specific
3207 		 * copy function if the property is coming from the PROM.
3208 		 */
3209 		if (ph->ph_flags & PH_FROM_PROM) {
3210 			return (DDI_PROP_RESULT_ERROR);
3211 		} else {
3212 			bcopy(ph->ph_cur_pos, data, sizeof (int64_t));
3213 		}
3214 
3215 		/*
3216 		 * Move the current location to the start of the next
3217 		 * bit of undecoded data.
3218 		 */
3219 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
3220 		    sizeof (int64_t);
3221 			return (DDI_PROP_RESULT_OK);
3222 
3223 	case DDI_PROP_CMD_ENCODE:
3224 		/*
3225 		 * Check that there is room to encoded the data
3226 		 */
3227 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3228 		    ph->ph_size < sizeof (int64_t) ||
3229 		    ((int64_t *)ph->ph_cur_pos > ((int64_t *)ph->ph_data +
3230 		    ph->ph_size - sizeof (int64_t))))
3231 			return (DDI_PROP_RESULT_ERROR);
3232 
3233 		/*
3234 		 * Encode the integer into the byte stream one byte at a
3235 		 * time.
3236 		 */
3237 		bcopy(data, ph->ph_cur_pos, sizeof (int64_t));
3238 
3239 		/*
3240 		 * Move the current location to the start of the next bit of
3241 		 * space where we can store encoded data.
3242 		 */
3243 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
3244 		    sizeof (int64_t);
3245 		return (DDI_PROP_RESULT_OK);
3246 
3247 	case DDI_PROP_CMD_SKIP:
3248 		/*
3249 		 * Check that there is encoded data
3250 		 */
3251 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3252 		    ph->ph_size < sizeof (int64_t))
3253 			return (DDI_PROP_RESULT_ERROR);
3254 
3255 		if ((caddr_t)ph->ph_cur_pos ==
3256 		    (caddr_t)ph->ph_data + ph->ph_size) {
3257 			return (DDI_PROP_RESULT_EOF);
3258 		} else if ((caddr_t)ph->ph_cur_pos >
3259 		    (caddr_t)ph->ph_data + ph->ph_size) {
3260 			return (DDI_PROP_RESULT_EOF);
3261 		}
3262 
3263 		/*
3264 		 * Move the current location to the start of
3265 		 * the next bit of undecoded data.
3266 		 */
3267 		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
3268 		    sizeof (int64_t);
3269 			return (DDI_PROP_RESULT_OK);
3270 
3271 	case DDI_PROP_CMD_GET_ESIZE:
3272 		/*
3273 		 * Return the size of an encoded integer on OBP
3274 		 */
3275 		return (sizeof (int64_t));
3276 
3277 	case DDI_PROP_CMD_GET_DSIZE:
3278 		/*
3279 		 * Return the size of a decoded integer on the system.
3280 		 */
3281 		return (sizeof (int64_t));
3282 
3283 	default:
3284 #ifdef DEBUG
3285 		panic("ddi_prop_int64_op: %x impossible", cmd);
3286 		/*NOTREACHED*/
3287 #else
3288 		return (DDI_PROP_RESULT_ERROR);
3289 #endif  /* DEBUG */
3290 	}
3291 }
3292 
3293 /*
3294  * OBP 1275 string operator.
3295  *
3296  * OBP strings are NULL terminated.
3297  */
3298 int
3299 ddi_prop_1275_string(prop_handle_t *ph, uint_t cmd, char *data)
3300 {
3301 	int	n;
3302 	char	*p;
3303 	char	*end;
3304 
3305 	switch (cmd) {
3306 	case DDI_PROP_CMD_DECODE:
3307 		/*
3308 		 * Check that there is encoded data
3309 		 */
3310 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
3311 			return (DDI_PROP_RESULT_ERROR);
3312 		}
3313 
3314 		/*
3315 		 * Match DDI_PROP_CMD_GET_DSIZE logic for when to stop and
3316 		 * how to NULL terminate result.
3317 		 */
3318 		p = (char *)ph->ph_cur_pos;
3319 		end = (char *)ph->ph_data + ph->ph_size;
3320 		if (p >= end)
3321 			return (DDI_PROP_RESULT_EOF);
3322 
3323 		while (p < end) {
3324 			*data++ = *p;
3325 			if (*p++ == 0) {	/* NULL from OBP */
3326 				ph->ph_cur_pos = p;
3327 				return (DDI_PROP_RESULT_OK);
3328 			}
3329 		}
3330 
3331 		/*
3332 		 * If OBP did not NULL terminate string, which happens
3333 		 * (at least) for 'true'/'false' boolean values, account for
3334 		 * the space and store null termination on decode.
3335 		 */
3336 		ph->ph_cur_pos = p;
3337 		*data = 0;
3338 		return (DDI_PROP_RESULT_OK);
3339 
3340 	case DDI_PROP_CMD_ENCODE:
3341 		/*
3342 		 * Check that there is room to encoded the data
3343 		 */
3344 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
3345 			return (DDI_PROP_RESULT_ERROR);
3346 		}
3347 
3348 		n = strlen(data) + 1;
3349 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3350 		    ph->ph_size - n)) {
3351 			return (DDI_PROP_RESULT_ERROR);
3352 		}
3353 
3354 		/*
3355 		 * Copy the NULL terminated string
3356 		 */
3357 		bcopy(data, ph->ph_cur_pos, n);
3358 
3359 		/*
3360 		 * Move the current location to the start of the next bit of
3361 		 * space where we can store encoded data.
3362 		 */
3363 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + n;
3364 		return (DDI_PROP_RESULT_OK);
3365 
3366 	case DDI_PROP_CMD_SKIP:
3367 		/*
3368 		 * Check that there is encoded data
3369 		 */
3370 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
3371 			return (DDI_PROP_RESULT_ERROR);
3372 		}
3373 
3374 		/*
3375 		 * Return the string length plus one for the NULL
3376 		 * We know the size of the property, we need to
3377 		 * ensure that the string is properly formatted,
3378 		 * since we may be looking up random OBP data.
3379 		 */
3380 		p = (char *)ph->ph_cur_pos;
3381 		end = (char *)ph->ph_data + ph->ph_size;
3382 		if (p >= end)
3383 			return (DDI_PROP_RESULT_EOF);
3384 
3385 		while (p < end) {
3386 			if (*p++ == 0) {	/* NULL from OBP */
3387 				ph->ph_cur_pos = p;
3388 				return (DDI_PROP_RESULT_OK);
3389 			}
3390 		}
3391 
3392 		/*
3393 		 * Accommodate the fact that OBP does not always NULL
3394 		 * terminate strings.
3395 		 */
3396 		ph->ph_cur_pos = p;
3397 		return (DDI_PROP_RESULT_OK);
3398 
3399 	case DDI_PROP_CMD_GET_ESIZE:
3400 		/*
3401 		 * Return the size of the encoded string on OBP.
3402 		 */
3403 		return (strlen(data) + 1);
3404 
3405 	case DDI_PROP_CMD_GET_DSIZE:
3406 		/*
3407 		 * Return the string length plus one for the NULL.
3408 		 * We know the size of the property, we need to
3409 		 * ensure that the string is properly formatted,
3410 		 * since we may be looking up random OBP data.
3411 		 */
3412 		p = (char *)ph->ph_cur_pos;
3413 		end = (char *)ph->ph_data + ph->ph_size;
3414 		if (p >= end)
3415 			return (DDI_PROP_RESULT_EOF);
3416 
3417 		for (n = 0; p < end; n++) {
3418 			if (*p++ == 0) {	/* NULL from OBP */
3419 				ph->ph_cur_pos = p;
3420 				return (n + 1);
3421 			}
3422 		}
3423 
3424 		/*
3425 		 * If OBP did not NULL terminate string, which happens for
3426 		 * 'true'/'false' boolean values, account for the space
3427 		 * to store null termination here.
3428 		 */
3429 		ph->ph_cur_pos = p;
3430 		return (n + 1);
3431 
3432 	default:
3433 #ifdef DEBUG
3434 		panic("ddi_prop_1275_string: %x impossible", cmd);
3435 		/*NOTREACHED*/
3436 #else
3437 		return (DDI_PROP_RESULT_ERROR);
3438 #endif	/* DEBUG */
3439 	}
3440 }
3441 
3442 /*
3443  * OBP 1275 byte operator
3444  *
3445  * Caller must specify the number of bytes to get.  OBP encodes bytes
3446  * as a byte so there is a 1-to-1 translation.
3447  */
3448 int
3449 ddi_prop_1275_bytes(prop_handle_t *ph, uint_t cmd, uchar_t *data,
3450 	uint_t nelements)
3451 {
3452 	switch (cmd) {
3453 	case DDI_PROP_CMD_DECODE:
3454 		/*
3455 		 * Check that there is encoded data
3456 		 */
3457 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3458 		    ph->ph_size < nelements ||
3459 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3460 		    ph->ph_size - nelements)))
3461 			return (DDI_PROP_RESULT_ERROR);
3462 
3463 		/*
3464 		 * Copy out the bytes
3465 		 */
3466 		bcopy(ph->ph_cur_pos, data, nelements);
3467 
3468 		/*
3469 		 * Move the current location
3470 		 */
3471 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
3472 		return (DDI_PROP_RESULT_OK);
3473 
3474 	case DDI_PROP_CMD_ENCODE:
3475 		/*
3476 		 * Check that there is room to encode the data
3477 		 */
3478 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3479 		    ph->ph_size < nelements ||
3480 		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3481 		    ph->ph_size - nelements)))
3482 			return (DDI_PROP_RESULT_ERROR);
3483 
3484 		/*
3485 		 * Copy in the bytes
3486 		 */
3487 		bcopy(data, ph->ph_cur_pos, nelements);
3488 
3489 		/*
3490 		 * Move the current location to the start of the next bit of
3491 		 * space where we can store encoded data.
3492 		 */
3493 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
3494 		return (DDI_PROP_RESULT_OK);
3495 
3496 	case DDI_PROP_CMD_SKIP:
3497 		/*
3498 		 * Check that there is encoded data
3499 		 */
3500 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3501 		    ph->ph_size < nelements)
3502 			return (DDI_PROP_RESULT_ERROR);
3503 
3504 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3505 		    ph->ph_size - nelements))
3506 			return (DDI_PROP_RESULT_EOF);
3507 
3508 		/*
3509 		 * Move the current location
3510 		 */
3511 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
3512 		return (DDI_PROP_RESULT_OK);
3513 
3514 	case DDI_PROP_CMD_GET_ESIZE:
3515 		/*
3516 		 * The size in bytes of the encoded size is the
3517 		 * same as the decoded size provided by the caller.
3518 		 */
3519 		return (nelements);
3520 
3521 	case DDI_PROP_CMD_GET_DSIZE:
3522 		/*
3523 		 * Just return the number of bytes specified by the caller.
3524 		 */
3525 		return (nelements);
3526 
3527 	default:
3528 #ifdef DEBUG
3529 		panic("ddi_prop_1275_bytes: %x impossible", cmd);
3530 		/*NOTREACHED*/
3531 #else
3532 		return (DDI_PROP_RESULT_ERROR);
3533 #endif	/* DEBUG */
3534 	}
3535 }
3536 
3537 /*
3538  * Used for properties that come from the OBP, hardware configuration files,
3539  * or that are created by calls to ddi_prop_update(9F).
3540  */
3541 static struct prop_handle_ops prop_1275_ops = {
3542 	ddi_prop_1275_int,
3543 	ddi_prop_1275_string,
3544 	ddi_prop_1275_bytes,
3545 	ddi_prop_int64_op
3546 };
3547 
3548 
3549 /*
3550  * Interface to create/modify a managed property on child's behalf...
3551  * Flags interpreted are:
3552  *	DDI_PROP_CANSLEEP:	Allow memory allocation to sleep.
3553  *	DDI_PROP_SYSTEM_DEF:	Manipulate system list rather than driver list.
3554  *
3555  * Use same dev_t when modifying or undefining a property.
3556  * Search for properties with DDI_DEV_T_ANY to match first named
3557  * property on the list.
3558  *
3559  * Properties are stored LIFO and subsequently will match the first
3560  * `matching' instance.
3561  */
3562 
3563 /*
3564  * ddi_prop_add:	Add a software defined property
3565  */
3566 
3567 /*
3568  * define to get a new ddi_prop_t.
3569  * km_flags are KM_SLEEP or KM_NOSLEEP.
3570  */
3571 
3572 #define	DDI_NEW_PROP_T(km_flags)	\
3573 	(kmem_zalloc(sizeof (ddi_prop_t), km_flags))
3574 
3575 static int
3576 ddi_prop_add(dev_t dev, dev_info_t *dip, int flags,
3577     char *name, caddr_t value, int length)
3578 {
3579 	ddi_prop_t	*new_propp, *propp;
3580 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
3581 	int		km_flags = KM_NOSLEEP;
3582 	int		name_buf_len;
3583 
3584 	/*
3585 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero return error.
3586 	 */
3587 
3588 	if (dev == DDI_DEV_T_ANY || name == (char *)0 || strlen(name) == 0)
3589 		return (DDI_PROP_INVAL_ARG);
3590 
3591 	if (flags & DDI_PROP_CANSLEEP)
3592 		km_flags = KM_SLEEP;
3593 
3594 	if (flags & DDI_PROP_SYSTEM_DEF)
3595 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
3596 	else if (flags & DDI_PROP_HW_DEF)
3597 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
3598 
3599 	if ((new_propp = DDI_NEW_PROP_T(km_flags)) == NULL)  {
3600 		cmn_err(CE_CONT, prop_no_mem_msg, name);
3601 		return (DDI_PROP_NO_MEMORY);
3602 	}
3603 
3604 	/*
3605 	 * If dev is major number 0, then we need to do a ddi_name_to_major
3606 	 * to get the real major number for the device.  This needs to be
3607 	 * done because some drivers need to call ddi_prop_create in their
3608 	 * attach routines but they don't have a dev.  By creating the dev
3609 	 * ourself if the major number is 0, drivers will not have to know what
3610 	 * their major number.	They can just create a dev with major number
3611 	 * 0 and pass it in.  For device 0, we will be doing a little extra
3612 	 * work by recreating the same dev that we already have, but its the
3613 	 * price you pay :-).
3614 	 *
3615 	 * This fixes bug #1098060.
3616 	 */
3617 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN) {
3618 		new_propp->prop_dev =
3619 		    makedevice(ddi_name_to_major(DEVI(dip)->devi_binding_name),
3620 		    getminor(dev));
3621 	} else
3622 		new_propp->prop_dev = dev;
3623 
3624 	/*
3625 	 * Allocate space for property name and copy it in...
3626 	 */
3627 
3628 	name_buf_len = strlen(name) + 1;
3629 	new_propp->prop_name = kmem_alloc(name_buf_len, km_flags);
3630 	if (new_propp->prop_name == 0)	{
3631 		kmem_free(new_propp, sizeof (ddi_prop_t));
3632 		cmn_err(CE_CONT, prop_no_mem_msg, name);
3633 		return (DDI_PROP_NO_MEMORY);
3634 	}
3635 	bcopy(name, new_propp->prop_name, name_buf_len);
3636 
3637 	/*
3638 	 * Set the property type
3639 	 */
3640 	new_propp->prop_flags = flags & DDI_PROP_TYPE_MASK;
3641 
3642 	/*
3643 	 * Set length and value ONLY if not an explicit property undefine:
3644 	 * NOTE: value and length are zero for explicit undefines.
3645 	 */
3646 
3647 	if (flags & DDI_PROP_UNDEF_IT) {
3648 		new_propp->prop_flags |= DDI_PROP_UNDEF_IT;
3649 	} else {
3650 		if ((new_propp->prop_len = length) != 0) {
3651 			new_propp->prop_val = kmem_alloc(length, km_flags);
3652 			if (new_propp->prop_val == 0)  {
3653 				kmem_free(new_propp->prop_name, name_buf_len);
3654 				kmem_free(new_propp, sizeof (ddi_prop_t));
3655 				cmn_err(CE_CONT, prop_no_mem_msg, name);
3656 				return (DDI_PROP_NO_MEMORY);
3657 			}
3658 			bcopy(value, new_propp->prop_val, length);
3659 		}
3660 	}
3661 
3662 	/*
3663 	 * Link property into beginning of list. (Properties are LIFO order.)
3664 	 */
3665 
3666 	mutex_enter(&(DEVI(dip)->devi_lock));
3667 	propp = *list_head;
3668 	new_propp->prop_next = propp;
3669 	*list_head = new_propp;
3670 	mutex_exit(&(DEVI(dip)->devi_lock));
3671 	return (DDI_PROP_SUCCESS);
3672 }
3673 
3674 
3675 /*
3676  * ddi_prop_change:	Modify a software managed property value
3677  *
3678  *			Set new length and value if found.
3679  *			returns DDI_PROP_INVAL_ARG if dev is DDI_DEV_T_ANY or
3680  *			input name is the NULL string.
3681  *			returns DDI_PROP_NO_MEMORY if unable to allocate memory
3682  *
3683  *			Note: an undef can be modified to be a define,
3684  *			(you can't go the other way.)
3685  */
3686 
3687 static int
3688 ddi_prop_change(dev_t dev, dev_info_t *dip, int flags,
3689     char *name, caddr_t value, int length)
3690 {
3691 	ddi_prop_t	*propp;
3692 	ddi_prop_t	**ppropp;
3693 	caddr_t		p = NULL;
3694 
3695 	if ((dev == DDI_DEV_T_ANY) || (name == NULL) || (strlen(name) == 0))
3696 		return (DDI_PROP_INVAL_ARG);
3697 
3698 	/*
3699 	 * Preallocate buffer, even if we don't need it...
3700 	 */
3701 	if (length != 0)  {
3702 		p = kmem_alloc(length, (flags & DDI_PROP_CANSLEEP) ?
3703 		    KM_SLEEP : KM_NOSLEEP);
3704 		if (p == NULL)	{
3705 			cmn_err(CE_CONT, prop_no_mem_msg, name);
3706 			return (DDI_PROP_NO_MEMORY);
3707 		}
3708 	}
3709 
3710 	/*
3711 	 * If the dev_t value contains DDI_MAJOR_T_UNKNOWN for the major
3712 	 * number, a real dev_t value should be created based upon the dip's
3713 	 * binding driver.  See ddi_prop_add...
3714 	 */
3715 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN)
3716 		dev = makedevice(
3717 		    ddi_name_to_major(DEVI(dip)->devi_binding_name),
3718 		    getminor(dev));
3719 
3720 	/*
3721 	 * Check to see if the property exists.  If so we modify it.
3722 	 * Else we create it by calling ddi_prop_add().
3723 	 */
3724 	mutex_enter(&(DEVI(dip)->devi_lock));
3725 	ppropp = &DEVI(dip)->devi_drv_prop_ptr;
3726 	if (flags & DDI_PROP_SYSTEM_DEF)
3727 		ppropp = &DEVI(dip)->devi_sys_prop_ptr;
3728 	else if (flags & DDI_PROP_HW_DEF)
3729 		ppropp = &DEVI(dip)->devi_hw_prop_ptr;
3730 
3731 	if ((propp = i_ddi_prop_search(dev, name, flags, ppropp)) != NULL) {
3732 		/*
3733 		 * Need to reallocate buffer?  If so, do it
3734 		 * carefully (reuse same space if new prop
3735 		 * is same size and non-NULL sized).
3736 		 */
3737 		if (length != 0)
3738 			bcopy(value, p, length);
3739 
3740 		if (propp->prop_len != 0)
3741 			kmem_free(propp->prop_val, propp->prop_len);
3742 
3743 		propp->prop_len = length;
3744 		propp->prop_val = p;
3745 		propp->prop_flags &= ~DDI_PROP_UNDEF_IT;
3746 		mutex_exit(&(DEVI(dip)->devi_lock));
3747 		return (DDI_PROP_SUCCESS);
3748 	}
3749 
3750 	mutex_exit(&(DEVI(dip)->devi_lock));
3751 	if (length != 0)
3752 		kmem_free(p, length);
3753 
3754 	return (ddi_prop_add(dev, dip, flags, name, value, length));
3755 }
3756 
3757 /*
3758  * Common update routine used to update and encode a property.	Creates
3759  * a property handle, calls the property encode routine, figures out if
3760  * the property already exists and updates if it does.	Otherwise it
3761  * creates if it does not exist.
3762  */
3763 int
3764 ddi_prop_update_common(dev_t match_dev, dev_info_t *dip, int flags,
3765     char *name, void *data, uint_t nelements,
3766     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
3767 {
3768 	prop_handle_t	ph;
3769 	int		rval;
3770 	uint_t		ourflags;
3771 
3772 	/*
3773 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
3774 	 * return error.
3775 	 */
3776 	if (match_dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
3777 		return (DDI_PROP_INVAL_ARG);
3778 
3779 	/*
3780 	 * Create the handle
3781 	 */
3782 	ph.ph_data = NULL;
3783 	ph.ph_cur_pos = NULL;
3784 	ph.ph_save_pos = NULL;
3785 	ph.ph_size = 0;
3786 	ph.ph_ops = &prop_1275_ops;
3787 
3788 	/*
3789 	 * ourflags:
3790 	 * For compatibility with the old interfaces.  The old interfaces
3791 	 * didn't sleep by default and slept when the flag was set.  These
3792 	 * interfaces to the opposite.	So the old interfaces now set the
3793 	 * DDI_PROP_DONTSLEEP flag by default which tells us not to sleep.
3794 	 *
3795 	 * ph.ph_flags:
3796 	 * Blocked data or unblocked data allocation
3797 	 * for ph.ph_data in ddi_prop_encode_alloc()
3798 	 */
3799 	if (flags & DDI_PROP_DONTSLEEP) {
3800 		ourflags = flags;
3801 		ph.ph_flags = DDI_PROP_DONTSLEEP;
3802 	} else {
3803 		ourflags = flags | DDI_PROP_CANSLEEP;
3804 		ph.ph_flags = DDI_PROP_CANSLEEP;
3805 	}
3806 
3807 	/*
3808 	 * Encode the data and store it in the property handle by
3809 	 * calling the prop_encode routine.
3810 	 */
3811 	if ((rval = (*prop_create)(&ph, data, nelements)) !=
3812 	    DDI_PROP_SUCCESS) {
3813 		if (rval == DDI_PROP_NO_MEMORY)
3814 			cmn_err(CE_CONT, prop_no_mem_msg, name);
3815 		if (ph.ph_size != 0)
3816 			kmem_free(ph.ph_data, ph.ph_size);
3817 		return (rval);
3818 	}
3819 
3820 	/*
3821 	 * The old interfaces use a stacking approach to creating
3822 	 * properties.	If we are being called from the old interfaces,
3823 	 * the DDI_PROP_STACK_CREATE flag will be set, so we just do a
3824 	 * create without checking.
3825 	 */
3826 	if (flags & DDI_PROP_STACK_CREATE) {
3827 		rval = ddi_prop_add(match_dev, dip,
3828 		    ourflags, name, ph.ph_data, ph.ph_size);
3829 	} else {
3830 		rval = ddi_prop_change(match_dev, dip,
3831 		    ourflags, name, ph.ph_data, ph.ph_size);
3832 	}
3833 
3834 	/*
3835 	 * Free the encoded data allocated in the prop_encode routine.
3836 	 */
3837 	if (ph.ph_size != 0)
3838 		kmem_free(ph.ph_data, ph.ph_size);
3839 
3840 	return (rval);
3841 }
3842 
3843 
3844 /*
3845  * ddi_prop_create:	Define a managed property:
3846  *			See above for details.
3847  */
3848 
3849 int
3850 ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
3851     char *name, caddr_t value, int length)
3852 {
3853 	if (!(flag & DDI_PROP_CANSLEEP)) {
3854 		flag |= DDI_PROP_DONTSLEEP;
3855 #ifdef DDI_PROP_DEBUG
3856 		if (length != 0)
3857 			cmn_err(CE_NOTE, "!ddi_prop_create: interface obsolete,"
3858 			    "use ddi_prop_update (prop = %s, node = %s%d)",
3859 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
3860 #endif /* DDI_PROP_DEBUG */
3861 	}
3862 	flag &= ~DDI_PROP_SYSTEM_DEF;
3863 	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
3864 	return (ddi_prop_update_common(dev, dip, flag, name,
3865 	    value, length, ddi_prop_fm_encode_bytes));
3866 }
3867 
3868 int
3869 e_ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
3870     char *name, caddr_t value, int length)
3871 {
3872 	if (!(flag & DDI_PROP_CANSLEEP))
3873 		flag |= DDI_PROP_DONTSLEEP;
3874 	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
3875 	return (ddi_prop_update_common(dev, dip, flag,
3876 	    name, value, length, ddi_prop_fm_encode_bytes));
3877 }
3878 
3879 int
3880 ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
3881     char *name, caddr_t value, int length)
3882 {
3883 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
3884 
3885 	/*
3886 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
3887 	 * return error.
3888 	 */
3889 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
3890 		return (DDI_PROP_INVAL_ARG);
3891 
3892 	if (!(flag & DDI_PROP_CANSLEEP))
3893 		flag |= DDI_PROP_DONTSLEEP;
3894 	flag &= ~DDI_PROP_SYSTEM_DEF;
3895 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_NOTPROM), name) == 0)
3896 		return (DDI_PROP_NOT_FOUND);
3897 
3898 	return (ddi_prop_update_common(dev, dip,
3899 	    (flag | DDI_PROP_TYPE_BYTE), name,
3900 	    value, length, ddi_prop_fm_encode_bytes));
3901 }
3902 
3903 int
3904 e_ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
3905     char *name, caddr_t value, int length)
3906 {
3907 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
3908 
3909 	/*
3910 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
3911 	 * return error.
3912 	 */
3913 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
3914 		return (DDI_PROP_INVAL_ARG);
3915 
3916 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_SYSTEM_DEF), name) == 0)
3917 		return (DDI_PROP_NOT_FOUND);
3918 
3919 	if (!(flag & DDI_PROP_CANSLEEP))
3920 		flag |= DDI_PROP_DONTSLEEP;
3921 	return (ddi_prop_update_common(dev, dip,
3922 	    (flag | DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE),
3923 	    name, value, length, ddi_prop_fm_encode_bytes));
3924 }
3925 
3926 
3927 /*
3928  * Common lookup routine used to lookup and decode a property.
3929  * Creates a property handle, searches for the raw encoded data,
3930  * fills in the handle, and calls the property decode functions
3931  * passed in.
3932  *
3933  * This routine is not static because ddi_bus_prop_op() which lives in
3934  * ddi_impl.c calls it.  No driver should be calling this routine.
3935  */
3936 int
3937 ddi_prop_lookup_common(dev_t match_dev, dev_info_t *dip,
3938     uint_t flags, char *name, void *data, uint_t *nelements,
3939     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
3940 {
3941 	int		rval;
3942 	uint_t		ourflags;
3943 	prop_handle_t	ph;
3944 
3945 	if ((match_dev == DDI_DEV_T_NONE) ||
3946 	    (name == NULL) || (strlen(name) == 0))
3947 		return (DDI_PROP_INVAL_ARG);
3948 
3949 	ourflags = (flags & DDI_PROP_DONTSLEEP) ? flags :
3950 	    flags | DDI_PROP_CANSLEEP;
3951 
3952 	/*
3953 	 * Get the encoded data
3954 	 */
3955 	bzero(&ph, sizeof (prop_handle_t));
3956 
3957 	if ((flags & DDI_UNBND_DLPI2) || (flags & DDI_PROP_ROOTNEX_GLOBAL)) {
3958 		/*
3959 		 * For rootnex and unbound dlpi style-2 devices, index into
3960 		 * the devnames' array and search the global
3961 		 * property list.
3962 		 */
3963 		ourflags &= ~DDI_UNBND_DLPI2;
3964 		rval = i_ddi_prop_search_global(match_dev,
3965 		    ourflags, name, &ph.ph_data, &ph.ph_size);
3966 	} else {
3967 		rval = ddi_prop_search_common(match_dev, dip,
3968 		    PROP_LEN_AND_VAL_ALLOC, ourflags, name,
3969 		    &ph.ph_data, &ph.ph_size);
3970 
3971 	}
3972 
3973 	if (rval != DDI_PROP_SUCCESS && rval != DDI_PROP_FOUND_1275) {
3974 		ASSERT(ph.ph_data == NULL);
3975 		ASSERT(ph.ph_size == 0);
3976 		return (rval);
3977 	}
3978 
3979 	/*
3980 	 * If the encoded data came from a OBP or software
3981 	 * use the 1275 OBP decode/encode routines.
3982 	 */
3983 	ph.ph_cur_pos = ph.ph_data;
3984 	ph.ph_save_pos = ph.ph_data;
3985 	ph.ph_ops = &prop_1275_ops;
3986 	ph.ph_flags = (rval == DDI_PROP_FOUND_1275) ? PH_FROM_PROM : 0;
3987 
3988 	rval = (*prop_decoder)(&ph, data, nelements);
3989 
3990 	/*
3991 	 * Free the encoded data
3992 	 */
3993 	if (ph.ph_size != 0)
3994 		kmem_free(ph.ph_data, ph.ph_size);
3995 
3996 	return (rval);
3997 }
3998 
3999 /*
4000  * Lookup and return an array of composite properties.  The driver must
4001  * provide the decode routine.
4002  */
4003 int
4004 ddi_prop_lookup(dev_t match_dev, dev_info_t *dip,
4005     uint_t flags, char *name, void *data, uint_t *nelements,
4006     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
4007 {
4008 	return (ddi_prop_lookup_common(match_dev, dip,
4009 	    (flags | DDI_PROP_TYPE_COMPOSITE), name,
4010 	    data, nelements, prop_decoder));
4011 }
4012 
4013 /*
4014  * Return 1 if a property exists (no type checking done).
4015  * Return 0 if it does not exist.
4016  */
4017 int
4018 ddi_prop_exists(dev_t match_dev, dev_info_t *dip, uint_t flags, char *name)
4019 {
4020 	int	i;
4021 	uint_t	x = 0;
4022 
4023 	i = ddi_prop_search_common(match_dev, dip, PROP_EXISTS,
4024 	    flags | DDI_PROP_TYPE_MASK, name, NULL, &x);
4025 	return (i == DDI_PROP_SUCCESS || i == DDI_PROP_FOUND_1275);
4026 }
4027 
4028 
4029 /*
4030  * Update an array of composite properties.  The driver must
4031  * provide the encode routine.
4032  */
4033 int
4034 ddi_prop_update(dev_t match_dev, dev_info_t *dip,
4035     char *name, void *data, uint_t nelements,
4036     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
4037 {
4038 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_COMPOSITE,
4039 	    name, data, nelements, prop_create));
4040 }
4041 
4042 /*
4043  * Get a single integer or boolean property and return it.
4044  * If the property does not exists, or cannot be decoded,
4045  * then return the defvalue passed in.
4046  *
4047  * This routine always succeeds.
4048  */
4049 int
4050 ddi_prop_get_int(dev_t match_dev, dev_info_t *dip, uint_t flags,
4051     char *name, int defvalue)
4052 {
4053 	int	data;
4054 	uint_t	nelements;
4055 	int	rval;
4056 
4057 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4058 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
4059 #ifdef DEBUG
4060 		if (dip != NULL) {
4061 			cmn_err(CE_WARN, "ddi_prop_get_int: invalid flag"
4062 			    " 0x%x (prop = %s, node = %s%d)", flags,
4063 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
4064 		}
4065 #endif /* DEBUG */
4066 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4067 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4068 	}
4069 
4070 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
4071 	    (flags | DDI_PROP_TYPE_INT), name, &data, &nelements,
4072 	    ddi_prop_fm_decode_int)) != DDI_PROP_SUCCESS) {
4073 		if (rval == DDI_PROP_END_OF_DATA)
4074 			data = 1;
4075 		else
4076 			data = defvalue;
4077 	}
4078 	return (data);
4079 }
4080 
4081 /*
4082  * Get a single 64 bit integer or boolean property and return it.
4083  * If the property does not exists, or cannot be decoded,
4084  * then return the defvalue passed in.
4085  *
4086  * This routine always succeeds.
4087  */
4088 int64_t
4089 ddi_prop_get_int64(dev_t match_dev, dev_info_t *dip, uint_t flags,
4090     char *name, int64_t defvalue)
4091 {
4092 	int64_t	data;
4093 	uint_t	nelements;
4094 	int	rval;
4095 
4096 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4097 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
4098 #ifdef DEBUG
4099 		if (dip != NULL) {
4100 			cmn_err(CE_WARN, "ddi_prop_get_int64: invalid flag"
4101 			    " 0x%x (prop = %s, node = %s%d)", flags,
4102 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
4103 		}
4104 #endif /* DEBUG */
4105 		return (DDI_PROP_INVAL_ARG);
4106 	}
4107 
4108 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
4109 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
4110 	    name, &data, &nelements, ddi_prop_fm_decode_int64))
4111 	    != DDI_PROP_SUCCESS) {
4112 		if (rval == DDI_PROP_END_OF_DATA)
4113 			data = 1;
4114 		else
4115 			data = defvalue;
4116 	}
4117 	return (data);
4118 }
4119 
4120 /*
4121  * Get an array of integer property
4122  */
4123 int
4124 ddi_prop_lookup_int_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4125     char *name, int **data, uint_t *nelements)
4126 {
4127 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4128 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
4129 #ifdef DEBUG
4130 		if (dip != NULL) {
4131 			cmn_err(CE_WARN, "ddi_prop_lookup_int_array: "
4132 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
4133 			    flags, name, ddi_driver_name(dip),
4134 			    ddi_get_instance(dip));
4135 		}
4136 #endif /* DEBUG */
4137 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4138 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4139 	}
4140 
4141 	return (ddi_prop_lookup_common(match_dev, dip,
4142 	    (flags | DDI_PROP_TYPE_INT), name, data,
4143 	    nelements, ddi_prop_fm_decode_ints));
4144 }
4145 
4146 /*
4147  * Get an array of 64 bit integer properties
4148  */
4149 int
4150 ddi_prop_lookup_int64_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4151     char *name, int64_t **data, uint_t *nelements)
4152 {
4153 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4154 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
4155 #ifdef DEBUG
4156 		if (dip != NULL) {
4157 			cmn_err(CE_WARN, "ddi_prop_lookup_int64_array: "
4158 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
4159 			    flags, name, ddi_driver_name(dip),
4160 			    ddi_get_instance(dip));
4161 		}
4162 #endif /* DEBUG */
4163 		return (DDI_PROP_INVAL_ARG);
4164 	}
4165 
4166 	return (ddi_prop_lookup_common(match_dev, dip,
4167 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
4168 	    name, data, nelements, ddi_prop_fm_decode_int64_array));
4169 }
4170 
4171 /*
4172  * Update a single integer property.  If the property exists on the drivers
4173  * property list it updates, else it creates it.
4174  */
4175 int
4176 ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
4177     char *name, int data)
4178 {
4179 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
4180 	    name, &data, 1, ddi_prop_fm_encode_ints));
4181 }
4182 
4183 /*
4184  * Update a single 64 bit integer property.
4185  * Update the driver property list if it exists, else create it.
4186  */
4187 int
4188 ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
4189     char *name, int64_t data)
4190 {
4191 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
4192 	    name, &data, 1, ddi_prop_fm_encode_int64));
4193 }
4194 
4195 int
4196 e_ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
4197     char *name, int data)
4198 {
4199 	return (ddi_prop_update_common(match_dev, dip,
4200 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
4201 	    name, &data, 1, ddi_prop_fm_encode_ints));
4202 }
4203 
4204 int
4205 e_ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
4206     char *name, int64_t data)
4207 {
4208 	return (ddi_prop_update_common(match_dev, dip,
4209 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
4210 	    name, &data, 1, ddi_prop_fm_encode_int64));
4211 }
4212 
4213 /*
4214  * Update an array of integer property.  If the property exists on the drivers
4215  * property list it updates, else it creates it.
4216  */
4217 int
4218 ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
4219     char *name, int *data, uint_t nelements)
4220 {
4221 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
4222 	    name, data, nelements, ddi_prop_fm_encode_ints));
4223 }
4224 
4225 /*
4226  * Update an array of 64 bit integer properties.
4227  * Update the driver property list if it exists, else create it.
4228  */
4229 int
4230 ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
4231     char *name, int64_t *data, uint_t nelements)
4232 {
4233 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
4234 	    name, data, nelements, ddi_prop_fm_encode_int64));
4235 }
4236 
4237 int
4238 e_ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
4239     char *name, int64_t *data, uint_t nelements)
4240 {
4241 	return (ddi_prop_update_common(match_dev, dip,
4242 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
4243 	    name, data, nelements, ddi_prop_fm_encode_int64));
4244 }
4245 
4246 int
4247 e_ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
4248     char *name, int *data, uint_t nelements)
4249 {
4250 	return (ddi_prop_update_common(match_dev, dip,
4251 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
4252 	    name, data, nelements, ddi_prop_fm_encode_ints));
4253 }
4254 
4255 /*
4256  * Get a single string property.
4257  */
4258 int
4259 ddi_prop_lookup_string(dev_t match_dev, dev_info_t *dip, uint_t flags,
4260     char *name, char **data)
4261 {
4262 	uint_t x;
4263 
4264 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4265 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
4266 #ifdef DEBUG
4267 		if (dip != NULL) {
4268 			cmn_err(CE_WARN, "%s: invalid flag 0x%x "
4269 			    "(prop = %s, node = %s%d); invalid bits ignored",
4270 			    "ddi_prop_lookup_string", flags, name,
4271 			    ddi_driver_name(dip), ddi_get_instance(dip));
4272 		}
4273 #endif /* DEBUG */
4274 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4275 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4276 	}
4277 
4278 	return (ddi_prop_lookup_common(match_dev, dip,
4279 	    (flags | DDI_PROP_TYPE_STRING), name, data,
4280 	    &x, ddi_prop_fm_decode_string));
4281 }
4282 
4283 /*
4284  * Get an array of strings property.
4285  */
4286 int
4287 ddi_prop_lookup_string_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4288     char *name, char ***data, uint_t *nelements)
4289 {
4290 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4291 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
4292 #ifdef DEBUG
4293 		if (dip != NULL) {
4294 			cmn_err(CE_WARN, "ddi_prop_lookup_string_array: "
4295 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
4296 			    flags, name, ddi_driver_name(dip),
4297 			    ddi_get_instance(dip));
4298 		}
4299 #endif /* DEBUG */
4300 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4301 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4302 	}
4303 
4304 	return (ddi_prop_lookup_common(match_dev, dip,
4305 	    (flags | DDI_PROP_TYPE_STRING), name, data,
4306 	    nelements, ddi_prop_fm_decode_strings));
4307 }
4308 
4309 /*
4310  * Update a single string property.
4311  */
4312 int
4313 ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
4314     char *name, char *data)
4315 {
4316 	return (ddi_prop_update_common(match_dev, dip,
4317 	    DDI_PROP_TYPE_STRING, name, &data, 1,
4318 	    ddi_prop_fm_encode_string));
4319 }
4320 
4321 int
4322 e_ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
4323     char *name, char *data)
4324 {
4325 	return (ddi_prop_update_common(match_dev, dip,
4326 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
4327 	    name, &data, 1, ddi_prop_fm_encode_string));
4328 }
4329 
4330 
4331 /*
4332  * Update an array of strings property.
4333  */
4334 int
4335 ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
4336     char *name, char **data, uint_t nelements)
4337 {
4338 	return (ddi_prop_update_common(match_dev, dip,
4339 	    DDI_PROP_TYPE_STRING, name, data, nelements,
4340 	    ddi_prop_fm_encode_strings));
4341 }
4342 
4343 int
4344 e_ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
4345     char *name, char **data, uint_t nelements)
4346 {
4347 	return (ddi_prop_update_common(match_dev, dip,
4348 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
4349 	    name, data, nelements,
4350 	    ddi_prop_fm_encode_strings));
4351 }
4352 
4353 
4354 /*
4355  * Get an array of bytes property.
4356  */
4357 int
4358 ddi_prop_lookup_byte_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4359     char *name, uchar_t **data, uint_t *nelements)
4360 {
4361 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4362 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
4363 #ifdef DEBUG
4364 		if (dip != NULL) {
4365 			cmn_err(CE_WARN, "ddi_prop_lookup_byte_array: "
4366 			    " invalid flag 0x%x (prop = %s, node = %s%d)",
4367 			    flags, name, ddi_driver_name(dip),
4368 			    ddi_get_instance(dip));
4369 		}
4370 #endif /* DEBUG */
4371 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4372 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4373 	}
4374 
4375 	return (ddi_prop_lookup_common(match_dev, dip,
4376 	    (flags | DDI_PROP_TYPE_BYTE), name, data,
4377 	    nelements, ddi_prop_fm_decode_bytes));
4378 }
4379 
4380 /*
4381  * Update an array of bytes property.
4382  */
4383 int
4384 ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
4385     char *name, uchar_t *data, uint_t nelements)
4386 {
4387 	if (nelements == 0)
4388 		return (DDI_PROP_INVAL_ARG);
4389 
4390 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_BYTE,
4391 	    name, data, nelements, ddi_prop_fm_encode_bytes));
4392 }
4393 
4394 
4395 int
4396 e_ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
4397     char *name, uchar_t *data, uint_t nelements)
4398 {
4399 	if (nelements == 0)
4400 		return (DDI_PROP_INVAL_ARG);
4401 
4402 	return (ddi_prop_update_common(match_dev, dip,
4403 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE,
4404 	    name, data, nelements, ddi_prop_fm_encode_bytes));
4405 }
4406 
4407 
4408 /*
4409  * ddi_prop_remove_common:	Undefine a managed property:
4410  *			Input dev_t must match dev_t when defined.
4411  *			Returns DDI_PROP_NOT_FOUND, possibly.
4412  *			DDI_PROP_INVAL_ARG is also possible if dev is
4413  *			DDI_DEV_T_ANY or incoming name is the NULL string.
4414  */
4415 int
4416 ddi_prop_remove_common(dev_t dev, dev_info_t *dip, char *name, int flag)
4417 {
4418 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
4419 	ddi_prop_t	*propp;
4420 	ddi_prop_t	*lastpropp = NULL;
4421 
4422 	if ((dev == DDI_DEV_T_ANY) || (name == (char *)0) ||
4423 	    (strlen(name) == 0)) {
4424 		return (DDI_PROP_INVAL_ARG);
4425 	}
4426 
4427 	if (flag & DDI_PROP_SYSTEM_DEF)
4428 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
4429 	else if (flag & DDI_PROP_HW_DEF)
4430 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
4431 
4432 	mutex_enter(&(DEVI(dip)->devi_lock));
4433 
4434 	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
4435 		if (DDI_STRSAME(propp->prop_name, name) &&
4436 		    (dev == propp->prop_dev)) {
4437 			/*
4438 			 * Unlink this propp allowing for it to
4439 			 * be first in the list:
4440 			 */
4441 
4442 			if (lastpropp == NULL)
4443 				*list_head = propp->prop_next;
4444 			else
4445 				lastpropp->prop_next = propp->prop_next;
4446 
4447 			mutex_exit(&(DEVI(dip)->devi_lock));
4448 
4449 			/*
4450 			 * Free memory and return...
4451 			 */
4452 			kmem_free(propp->prop_name,
4453 			    strlen(propp->prop_name) + 1);
4454 			if (propp->prop_len != 0)
4455 				kmem_free(propp->prop_val, propp->prop_len);
4456 			kmem_free(propp, sizeof (ddi_prop_t));
4457 			return (DDI_PROP_SUCCESS);
4458 		}
4459 		lastpropp = propp;
4460 	}
4461 	mutex_exit(&(DEVI(dip)->devi_lock));
4462 	return (DDI_PROP_NOT_FOUND);
4463 }
4464 
4465 int
4466 ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
4467 {
4468 	return (ddi_prop_remove_common(dev, dip, name, 0));
4469 }
4470 
4471 int
4472 e_ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
4473 {
4474 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_SYSTEM_DEF));
4475 }
4476 
4477 /*
4478  * e_ddi_prop_list_delete: remove a list of properties
4479  *	Note that the caller needs to provide the required protection
4480  *	(eg. devi_lock if these properties are still attached to a devi)
4481  */
4482 void
4483 e_ddi_prop_list_delete(ddi_prop_t *props)
4484 {
4485 	i_ddi_prop_list_delete(props);
4486 }
4487 
4488 /*
4489  * ddi_prop_remove_all_common:
4490  *	Used before unloading a driver to remove
4491  *	all properties. (undefines all dev_t's props.)
4492  *	Also removes `explicitly undefined' props.
4493  *	No errors possible.
4494  */
4495 void
4496 ddi_prop_remove_all_common(dev_info_t *dip, int flag)
4497 {
4498 	ddi_prop_t	**list_head;
4499 
4500 	mutex_enter(&(DEVI(dip)->devi_lock));
4501 	if (flag & DDI_PROP_SYSTEM_DEF) {
4502 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
4503 	} else if (flag & DDI_PROP_HW_DEF) {
4504 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
4505 	} else {
4506 		list_head = &(DEVI(dip)->devi_drv_prop_ptr);
4507 	}
4508 	i_ddi_prop_list_delete(*list_head);
4509 	*list_head = NULL;
4510 	mutex_exit(&(DEVI(dip)->devi_lock));
4511 }
4512 
4513 
4514 /*
4515  * ddi_prop_remove_all:		Remove all driver prop definitions.
4516  */
4517 
4518 void
4519 ddi_prop_remove_all(dev_info_t *dip)
4520 {
4521 	i_ddi_prop_dyn_driver_set(dip, NULL);
4522 	ddi_prop_remove_all_common(dip, 0);
4523 }
4524 
4525 /*
4526  * e_ddi_prop_remove_all:	Remove all system prop definitions.
4527  */
4528 
4529 void
4530 e_ddi_prop_remove_all(dev_info_t *dip)
4531 {
4532 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_SYSTEM_DEF);
4533 }
4534 
4535 
4536 /*
4537  * ddi_prop_undefine:	Explicitly undefine a property.  Property
4538  *			searches which match this property return
4539  *			the error code DDI_PROP_UNDEFINED.
4540  *
4541  *			Use ddi_prop_remove to negate effect of
4542  *			ddi_prop_undefine
4543  *
4544  *			See above for error returns.
4545  */
4546 
4547 int
4548 ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
4549 {
4550 	if (!(flag & DDI_PROP_CANSLEEP))
4551 		flag |= DDI_PROP_DONTSLEEP;
4552 	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
4553 	return (ddi_prop_update_common(dev, dip, flag,
4554 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
4555 }
4556 
4557 int
4558 e_ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
4559 {
4560 	if (!(flag & DDI_PROP_CANSLEEP))
4561 		flag |= DDI_PROP_DONTSLEEP;
4562 	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE |
4563 	    DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
4564 	return (ddi_prop_update_common(dev, dip, flag,
4565 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
4566 }
4567 
4568 /*
4569  * Support for gathering dynamic properties in devinfo snapshot.
4570  */
4571 void
4572 i_ddi_prop_dyn_driver_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4573 {
4574 	DEVI(dip)->devi_prop_dyn_driver = dp;
4575 }
4576 
4577 i_ddi_prop_dyn_t *
4578 i_ddi_prop_dyn_driver_get(dev_info_t *dip)
4579 {
4580 	return (DEVI(dip)->devi_prop_dyn_driver);
4581 }
4582 
4583 void
4584 i_ddi_prop_dyn_parent_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4585 {
4586 	DEVI(dip)->devi_prop_dyn_parent = dp;
4587 }
4588 
4589 i_ddi_prop_dyn_t *
4590 i_ddi_prop_dyn_parent_get(dev_info_t *dip)
4591 {
4592 	return (DEVI(dip)->devi_prop_dyn_parent);
4593 }
4594 
4595 void
4596 i_ddi_prop_dyn_cache_invalidate(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
4597 {
4598 	/* for now we invalidate the entire cached snapshot */
4599 	if (dip && dp)
4600 		i_ddi_di_cache_invalidate();
4601 }
4602 
4603 /* ARGSUSED */
4604 void
4605 ddi_prop_cache_invalidate(dev_t dev, dev_info_t *dip, char *name, int flags)
4606 {
4607 	/* for now we invalidate the entire cached snapshot */
4608 	i_ddi_di_cache_invalidate();
4609 }
4610 
4611 
4612 /*
4613  * Code to search hardware layer (PROM), if it exists, on behalf of child.
4614  *
4615  * if input dip != child_dip, then call is on behalf of child
4616  * to search PROM, do it via ddi_prop_search_common() and ascend only
4617  * if allowed.
4618  *
4619  * if input dip == ch_dip (child_dip), call is on behalf of root driver,
4620  * to search for PROM defined props only.
4621  *
4622  * Note that the PROM search is done only if the requested dev
4623  * is either DDI_DEV_T_ANY or DDI_DEV_T_NONE. PROM properties
4624  * have no associated dev, thus are automatically associated with
4625  * DDI_DEV_T_NONE.
4626  *
4627  * Modifying flag DDI_PROP_NOTPROM inhibits the search in the h/w layer.
4628  *
4629  * Returns DDI_PROP_FOUND_1275 if found to indicate to framework
4630  * that the property resides in the prom.
4631  */
4632 int
4633 impl_ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
4634     ddi_prop_op_t prop_op, int mod_flags,
4635     char *name, caddr_t valuep, int *lengthp)
4636 {
4637 	int	len;
4638 	caddr_t buffer;
4639 
4640 	/*
4641 	 * If requested dev is DDI_DEV_T_NONE or DDI_DEV_T_ANY, then
4642 	 * look in caller's PROM if it's a self identifying device...
4643 	 *
4644 	 * Note that this is very similar to ddi_prop_op, but we
4645 	 * search the PROM instead of the s/w defined properties,
4646 	 * and we are called on by the parent driver to do this for
4647 	 * the child.
4648 	 */
4649 
4650 	if (((dev == DDI_DEV_T_NONE) || (dev == DDI_DEV_T_ANY)) &&
4651 	    ndi_dev_is_prom_node(ch_dip) &&
4652 	    ((mod_flags & DDI_PROP_NOTPROM) == 0)) {
4653 		len = prom_getproplen((pnode_t)DEVI(ch_dip)->devi_nodeid, name);
4654 		if (len == -1) {
4655 			return (DDI_PROP_NOT_FOUND);
4656 		}
4657 
4658 		/*
4659 		 * If exists only request, we're done
4660 		 */
4661 		if (prop_op == PROP_EXISTS) {
4662 			return (DDI_PROP_FOUND_1275);
4663 		}
4664 
4665 		/*
4666 		 * If length only request or prop length == 0, get out
4667 		 */
4668 		if ((prop_op == PROP_LEN) || (len == 0)) {
4669 			*lengthp = len;
4670 			return (DDI_PROP_FOUND_1275);
4671 		}
4672 
4673 		/*
4674 		 * Allocate buffer if required... (either way `buffer'
4675 		 * is receiving address).
4676 		 */
4677 
4678 		switch (prop_op) {
4679 
4680 		case PROP_LEN_AND_VAL_ALLOC:
4681 
4682 			buffer = kmem_alloc((size_t)len,
4683 			    mod_flags & DDI_PROP_CANSLEEP ?
4684 			    KM_SLEEP : KM_NOSLEEP);
4685 			if (buffer == NULL) {
4686 				return (DDI_PROP_NO_MEMORY);
4687 			}
4688 			*(caddr_t *)valuep = buffer;
4689 			break;
4690 
4691 		case PROP_LEN_AND_VAL_BUF:
4692 
4693 			if (len > (*lengthp)) {
4694 				*lengthp = len;
4695 				return (DDI_PROP_BUF_TOO_SMALL);
4696 			}
4697 
4698 			buffer = valuep;
4699 			break;
4700 
4701 		default:
4702 			break;
4703 		}
4704 
4705 		/*
4706 		 * Call the PROM function to do the copy.
4707 		 */
4708 		(void) prom_getprop((pnode_t)DEVI(ch_dip)->devi_nodeid,
4709 		    name, buffer);
4710 
4711 		*lengthp = len; /* return the actual length to the caller */
4712 		(void) impl_fix_props(dip, ch_dip, name, len, buffer);
4713 		return (DDI_PROP_FOUND_1275);
4714 	}
4715 
4716 	return (DDI_PROP_NOT_FOUND);
4717 }
4718 
4719 /*
4720  * The ddi_bus_prop_op default bus nexus prop op function.
4721  *
4722  * Code to search hardware layer (PROM), if it exists,
4723  * on behalf of child, then, if appropriate, ascend and check
4724  * my own software defined properties...
4725  */
4726 int
4727 ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
4728     ddi_prop_op_t prop_op, int mod_flags,
4729     char *name, caddr_t valuep, int *lengthp)
4730 {
4731 	int	error;
4732 
4733 	error = impl_ddi_bus_prop_op(dev, dip, ch_dip, prop_op, mod_flags,
4734 	    name, valuep, lengthp);
4735 
4736 	if (error == DDI_PROP_SUCCESS || error == DDI_PROP_FOUND_1275 ||
4737 	    error == DDI_PROP_BUF_TOO_SMALL)
4738 		return (error);
4739 
4740 	if (error == DDI_PROP_NO_MEMORY) {
4741 		cmn_err(CE_CONT, prop_no_mem_msg, name);
4742 		return (DDI_PROP_NO_MEMORY);
4743 	}
4744 
4745 	/*
4746 	 * Check the 'options' node as a last resort
4747 	 */
4748 	if ((mod_flags & DDI_PROP_DONTPASS) != 0)
4749 		return (DDI_PROP_NOT_FOUND);
4750 
4751 	if (ch_dip == ddi_root_node())	{
4752 		/*
4753 		 * As a last resort, when we've reached
4754 		 * the top and still haven't found the
4755 		 * property, see if the desired property
4756 		 * is attached to the options node.
4757 		 *
4758 		 * The options dip is attached right after boot.
4759 		 */
4760 		ASSERT(options_dip != NULL);
4761 		/*
4762 		 * Force the "don't pass" flag to *just* see
4763 		 * what the options node has to offer.
4764 		 */
4765 		return (ddi_prop_search_common(dev, options_dip, prop_op,
4766 		    mod_flags|DDI_PROP_DONTPASS, name, valuep,
4767 		    (uint_t *)lengthp));
4768 	}
4769 
4770 	/*
4771 	 * Otherwise, continue search with parent's s/w defined properties...
4772 	 * NOTE: Using `dip' in following call increments the level.
4773 	 */
4774 
4775 	return (ddi_prop_search_common(dev, dip, prop_op, mod_flags,
4776 	    name, valuep, (uint_t *)lengthp));
4777 }
4778 
4779 /*
4780  * External property functions used by other parts of the kernel...
4781  */
4782 
4783 /*
4784  * e_ddi_getlongprop: See comments for ddi_get_longprop.
4785  */
4786 
4787 int
4788 e_ddi_getlongprop(dev_t dev, vtype_t type, char *name, int flags,
4789     caddr_t valuep, int *lengthp)
4790 {
4791 	_NOTE(ARGUNUSED(type))
4792 	dev_info_t *devi;
4793 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_ALLOC;
4794 	int error;
4795 
4796 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4797 		return (DDI_PROP_NOT_FOUND);
4798 
4799 	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
4800 	ddi_release_devi(devi);
4801 	return (error);
4802 }
4803 
4804 /*
4805  * e_ddi_getlongprop_buf:	See comments for ddi_getlongprop_buf.
4806  */
4807 
4808 int
4809 e_ddi_getlongprop_buf(dev_t dev, vtype_t type, char *name, int flags,
4810     caddr_t valuep, int *lengthp)
4811 {
4812 	_NOTE(ARGUNUSED(type))
4813 	dev_info_t *devi;
4814 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
4815 	int error;
4816 
4817 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4818 		return (DDI_PROP_NOT_FOUND);
4819 
4820 	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
4821 	ddi_release_devi(devi);
4822 	return (error);
4823 }
4824 
4825 /*
4826  * e_ddi_getprop:	See comments for ddi_getprop.
4827  */
4828 int
4829 e_ddi_getprop(dev_t dev, vtype_t type, char *name, int flags, int defvalue)
4830 {
4831 	_NOTE(ARGUNUSED(type))
4832 	dev_info_t *devi;
4833 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
4834 	int	propvalue = defvalue;
4835 	int	proplength = sizeof (int);
4836 	int	error;
4837 
4838 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4839 		return (defvalue);
4840 
4841 	error = cdev_prop_op(dev, devi, prop_op,
4842 	    flags, name, (caddr_t)&propvalue, &proplength);
4843 	ddi_release_devi(devi);
4844 
4845 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
4846 		propvalue = 1;
4847 
4848 	return (propvalue);
4849 }
4850 
4851 /*
4852  * e_ddi_getprop_int64:
4853  *
4854  * This is a typed interfaces, but predates typed properties. With the
4855  * introduction of typed properties the framework tries to ensure
4856  * consistent use of typed interfaces. This is why TYPE_INT64 is not
4857  * part of TYPE_ANY.  E_ddi_getprop_int64 is a special case where a
4858  * typed interface invokes legacy (non-typed) interfaces:
4859  * cdev_prop_op(), prop_op(9E), ddi_prop_op(9F)).  In this case the
4860  * fact that TYPE_INT64 is not part of TYPE_ANY matters.  To support
4861  * this type of lookup as a single operation we invoke the legacy
4862  * non-typed interfaces with the special CONSUMER_TYPED bit set. The
4863  * framework ddi_prop_op(9F) implementation is expected to check for
4864  * CONSUMER_TYPED and, if set, expand type bits beyond TYPE_ANY
4865  * (currently TYPE_INT64).
4866  */
4867 int64_t
4868 e_ddi_getprop_int64(dev_t dev, vtype_t type, char *name,
4869     int flags, int64_t defvalue)
4870 {
4871 	_NOTE(ARGUNUSED(type))
4872 	dev_info_t	*devi;
4873 	ddi_prop_op_t	prop_op = PROP_LEN_AND_VAL_BUF;
4874 	int64_t		propvalue = defvalue;
4875 	int		proplength = sizeof (propvalue);
4876 	int		error;
4877 
4878 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4879 		return (defvalue);
4880 
4881 	error = cdev_prop_op(dev, devi, prop_op, flags |
4882 	    DDI_PROP_CONSUMER_TYPED, name, (caddr_t)&propvalue, &proplength);
4883 	ddi_release_devi(devi);
4884 
4885 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
4886 		propvalue = 1;
4887 
4888 	return (propvalue);
4889 }
4890 
4891 /*
4892  * e_ddi_getproplen:	See comments for ddi_getproplen.
4893  */
4894 int
4895 e_ddi_getproplen(dev_t dev, vtype_t type, char *name, int flags, int *lengthp)
4896 {
4897 	_NOTE(ARGUNUSED(type))
4898 	dev_info_t *devi;
4899 	ddi_prop_op_t prop_op = PROP_LEN;
4900 	int error;
4901 
4902 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4903 		return (DDI_PROP_NOT_FOUND);
4904 
4905 	error = cdev_prop_op(dev, devi, prop_op, flags, name, NULL, lengthp);
4906 	ddi_release_devi(devi);
4907 	return (error);
4908 }
4909 
4910 /*
4911  * Routines to get at elements of the dev_info structure
4912  */
4913 
4914 /*
4915  * ddi_binding_name: Return the driver binding name of the devinfo node
4916  *		This is the name the OS used to bind the node to a driver.
4917  */
4918 char *
4919 ddi_binding_name(dev_info_t *dip)
4920 {
4921 	return (DEVI(dip)->devi_binding_name);
4922 }
4923 
4924 /*
4925  * ddi_driver_major: Return the major number of the driver that
4926  *	the supplied devinfo is bound to.  If not yet bound,
4927  *	DDI_MAJOR_T_NONE.
4928  *
4929  * When used by the driver bound to 'devi', this
4930  * function will reliably return the driver major number.
4931  * Other ways of determining the driver major number, such as
4932  *	major = ddi_name_to_major(ddi_get_name(devi));
4933  *	major = ddi_name_to_major(ddi_binding_name(devi));
4934  * can return a different result as the driver/alias binding
4935  * can change dynamically, and thus should be avoided.
4936  */
4937 major_t
4938 ddi_driver_major(dev_info_t *devi)
4939 {
4940 	return (DEVI(devi)->devi_major);
4941 }
4942 
4943 /*
4944  * ddi_driver_name: Return the normalized driver name. this is the
4945  *		actual driver name
4946  */
4947 const char *
4948 ddi_driver_name(dev_info_t *devi)
4949 {
4950 	major_t major;
4951 
4952 	if ((major = ddi_driver_major(devi)) != DDI_MAJOR_T_NONE)
4953 		return (ddi_major_to_name(major));
4954 
4955 	return (ddi_node_name(devi));
4956 }
4957 
4958 /*
4959  * i_ddi_set_binding_name:	Set binding name.
4960  *
4961  *	Set the binding name to the given name.
4962  *	This routine is for use by the ddi implementation, not by drivers.
4963  */
4964 void
4965 i_ddi_set_binding_name(dev_info_t *dip, char *name)
4966 {
4967 	DEVI(dip)->devi_binding_name = name;
4968 
4969 }
4970 
4971 /*
4972  * ddi_get_name: A synonym of ddi_binding_name() ... returns a name
4973  * the implementation has used to bind the node to a driver.
4974  */
4975 char *
4976 ddi_get_name(dev_info_t *dip)
4977 {
4978 	return (DEVI(dip)->devi_binding_name);
4979 }
4980 
4981 /*
4982  * ddi_node_name: Return the name property of the devinfo node
4983  *		This may differ from ddi_binding_name if the node name
4984  *		does not define a binding to a driver (i.e. generic names).
4985  */
4986 char *
4987 ddi_node_name(dev_info_t *dip)
4988 {
4989 	return (DEVI(dip)->devi_node_name);
4990 }
4991 
4992 
4993 /*
4994  * ddi_get_nodeid:	Get nodeid stored in dev_info structure.
4995  */
4996 int
4997 ddi_get_nodeid(dev_info_t *dip)
4998 {
4999 	return (DEVI(dip)->devi_nodeid);
5000 }
5001 
5002 int
5003 ddi_get_instance(dev_info_t *dip)
5004 {
5005 	return (DEVI(dip)->devi_instance);
5006 }
5007 
5008 struct dev_ops *
5009 ddi_get_driver(dev_info_t *dip)
5010 {
5011 	return (DEVI(dip)->devi_ops);
5012 }
5013 
5014 void
5015 ddi_set_driver(dev_info_t *dip, struct dev_ops *devo)
5016 {
5017 	DEVI(dip)->devi_ops = devo;
5018 }
5019 
5020 /*
5021  * ddi_set_driver_private/ddi_get_driver_private:
5022  * Get/set device driver private data in devinfo.
5023  */
5024 void
5025 ddi_set_driver_private(dev_info_t *dip, void *data)
5026 {
5027 	DEVI(dip)->devi_driver_data = data;
5028 }
5029 
5030 void *
5031 ddi_get_driver_private(dev_info_t *dip)
5032 {
5033 	return (DEVI(dip)->devi_driver_data);
5034 }
5035 
5036 /*
5037  * ddi_get_parent, ddi_get_child, ddi_get_next_sibling
5038  */
5039 
5040 dev_info_t *
5041 ddi_get_parent(dev_info_t *dip)
5042 {
5043 	return ((dev_info_t *)DEVI(dip)->devi_parent);
5044 }
5045 
5046 dev_info_t *
5047 ddi_get_child(dev_info_t *dip)
5048 {
5049 	return ((dev_info_t *)DEVI(dip)->devi_child);
5050 }
5051 
5052 dev_info_t *
5053 ddi_get_next_sibling(dev_info_t *dip)
5054 {
5055 	return ((dev_info_t *)DEVI(dip)->devi_sibling);
5056 }
5057 
5058 dev_info_t *
5059 ddi_get_next(dev_info_t *dip)
5060 {
5061 	return ((dev_info_t *)DEVI(dip)->devi_next);
5062 }
5063 
5064 void
5065 ddi_set_next(dev_info_t *dip, dev_info_t *nextdip)
5066 {
5067 	DEVI(dip)->devi_next = DEVI(nextdip);
5068 }
5069 
5070 /*
5071  * ddi_root_node:		Return root node of devinfo tree
5072  */
5073 
5074 dev_info_t *
5075 ddi_root_node(void)
5076 {
5077 	extern dev_info_t *top_devinfo;
5078 
5079 	return (top_devinfo);
5080 }
5081 
5082 /*
5083  * Miscellaneous functions:
5084  */
5085 
5086 /*
5087  * Implementation specific hooks
5088  */
5089 
5090 void
5091 ddi_report_dev(dev_info_t *d)
5092 {
5093 	char *b;
5094 
5095 	(void) ddi_ctlops(d, d, DDI_CTLOPS_REPORTDEV, (void *)0, (void *)0);
5096 
5097 	/*
5098 	 * If this devinfo node has cb_ops, it's implicitly accessible from
5099 	 * userland, so we print its full name together with the instance
5100 	 * number 'abbreviation' that the driver may use internally.
5101 	 */
5102 	if (DEVI(d)->devi_ops->devo_cb_ops != (struct cb_ops *)0 &&
5103 	    (b = kmem_zalloc(MAXPATHLEN, KM_NOSLEEP))) {
5104 		cmn_err(CE_CONT, "?%s%d is %s\n",
5105 		    ddi_driver_name(d), ddi_get_instance(d),
5106 		    ddi_pathname(d, b));
5107 		kmem_free(b, MAXPATHLEN);
5108 	}
5109 }
5110 
5111 /*
5112  * ddi_ctlops() is described in the assembler not to buy a new register
5113  * window when it's called and can reduce cost in climbing the device tree
5114  * without using the tail call optimization.
5115  */
5116 int
5117 ddi_dev_regsize(dev_info_t *dev, uint_t rnumber, off_t *result)
5118 {
5119 	int ret;
5120 
5121 	ret = ddi_ctlops(dev, dev, DDI_CTLOPS_REGSIZE,
5122 	    (void *)&rnumber, (void *)result);
5123 
5124 	return (ret == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
5125 }
5126 
5127 int
5128 ddi_dev_nregs(dev_info_t *dev, int *result)
5129 {
5130 	return (ddi_ctlops(dev, dev, DDI_CTLOPS_NREGS, 0, (void *)result));
5131 }
5132 
5133 int
5134 ddi_dev_is_sid(dev_info_t *d)
5135 {
5136 	return (ddi_ctlops(d, d, DDI_CTLOPS_SIDDEV, (void *)0, (void *)0));
5137 }
5138 
5139 int
5140 ddi_slaveonly(dev_info_t *d)
5141 {
5142 	return (ddi_ctlops(d, d, DDI_CTLOPS_SLAVEONLY, (void *)0, (void *)0));
5143 }
5144 
5145 int
5146 ddi_dev_affinity(dev_info_t *a, dev_info_t *b)
5147 {
5148 	return (ddi_ctlops(a, a, DDI_CTLOPS_AFFINITY, (void *)b, (void *)0));
5149 }
5150 
5151 int
5152 ddi_streams_driver(dev_info_t *dip)
5153 {
5154 	if (i_ddi_devi_attached(dip) &&
5155 	    (DEVI(dip)->devi_ops->devo_cb_ops != NULL) &&
5156 	    (DEVI(dip)->devi_ops->devo_cb_ops->cb_str != NULL))
5157 		return (DDI_SUCCESS);
5158 	return (DDI_FAILURE);
5159 }
5160 
5161 /*
5162  * callback free list
5163  */
5164 
5165 static int ncallbacks;
5166 static int nc_low = 170;
5167 static int nc_med = 512;
5168 static int nc_high = 2048;
5169 static struct ddi_callback *callbackq;
5170 static struct ddi_callback *callbackqfree;
5171 
5172 /*
5173  * set/run callback lists
5174  */
5175 struct	cbstats	{
5176 	kstat_named_t	cb_asked;
5177 	kstat_named_t	cb_new;
5178 	kstat_named_t	cb_run;
5179 	kstat_named_t	cb_delete;
5180 	kstat_named_t	cb_maxreq;
5181 	kstat_named_t	cb_maxlist;
5182 	kstat_named_t	cb_alloc;
5183 	kstat_named_t	cb_runouts;
5184 	kstat_named_t	cb_L2;
5185 	kstat_named_t	cb_grow;
5186 } cbstats = {
5187 	{"asked",	KSTAT_DATA_UINT32},
5188 	{"new",		KSTAT_DATA_UINT32},
5189 	{"run",		KSTAT_DATA_UINT32},
5190 	{"delete",	KSTAT_DATA_UINT32},
5191 	{"maxreq",	KSTAT_DATA_UINT32},
5192 	{"maxlist",	KSTAT_DATA_UINT32},
5193 	{"alloc",	KSTAT_DATA_UINT32},
5194 	{"runouts",	KSTAT_DATA_UINT32},
5195 	{"L2",		KSTAT_DATA_UINT32},
5196 	{"grow",	KSTAT_DATA_UINT32},
5197 };
5198 
5199 #define	nc_asked	cb_asked.value.ui32
5200 #define	nc_new		cb_new.value.ui32
5201 #define	nc_run		cb_run.value.ui32
5202 #define	nc_delete	cb_delete.value.ui32
5203 #define	nc_maxreq	cb_maxreq.value.ui32
5204 #define	nc_maxlist	cb_maxlist.value.ui32
5205 #define	nc_alloc	cb_alloc.value.ui32
5206 #define	nc_runouts	cb_runouts.value.ui32
5207 #define	nc_L2		cb_L2.value.ui32
5208 #define	nc_grow		cb_grow.value.ui32
5209 
5210 static kmutex_t ddi_callback_mutex;
5211 
5212 /*
5213  * callbacks are handled using a L1/L2 cache. The L1 cache
5214  * comes out of kmem_cache_alloc and can expand/shrink dynamically. If
5215  * we can't get callbacks from the L1 cache [because pageout is doing
5216  * I/O at the time freemem is 0], we allocate callbacks out of the
5217  * L2 cache. The L2 cache is static and depends on the memory size.
5218  * [We might also count the number of devices at probe time and
5219  * allocate one structure per device and adjust for deferred attach]
5220  */
5221 void
5222 impl_ddi_callback_init(void)
5223 {
5224 	int	i;
5225 	uint_t	physmegs;
5226 	kstat_t	*ksp;
5227 
5228 	physmegs = physmem >> (20 - PAGESHIFT);
5229 	if (physmegs < 48) {
5230 		ncallbacks = nc_low;
5231 	} else if (physmegs < 128) {
5232 		ncallbacks = nc_med;
5233 	} else {
5234 		ncallbacks = nc_high;
5235 	}
5236 
5237 	/*
5238 	 * init free list
5239 	 */
5240 	callbackq = kmem_zalloc(
5241 	    ncallbacks * sizeof (struct ddi_callback), KM_SLEEP);
5242 	for (i = 0; i < ncallbacks-1; i++)
5243 		callbackq[i].c_nfree = &callbackq[i+1];
5244 	callbackqfree = callbackq;
5245 
5246 	/* init kstats */
5247 	if (ksp = kstat_create("unix", 0, "cbstats", "misc", KSTAT_TYPE_NAMED,
5248 	    sizeof (cbstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) {
5249 		ksp->ks_data = (void *) &cbstats;
5250 		kstat_install(ksp);
5251 	}
5252 
5253 }
5254 
5255 static void
5256 callback_insert(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid,
5257 	int count)
5258 {
5259 	struct ddi_callback *list, *marker, *new;
5260 	size_t size = sizeof (struct ddi_callback);
5261 
5262 	list = marker = (struct ddi_callback *)*listid;
5263 	while (list != NULL) {
5264 		if (list->c_call == funcp && list->c_arg == arg) {
5265 			list->c_count += count;
5266 			return;
5267 		}
5268 		marker = list;
5269 		list = list->c_nlist;
5270 	}
5271 	new = kmem_alloc(size, KM_NOSLEEP);
5272 	if (new == NULL) {
5273 		new = callbackqfree;
5274 		if (new == NULL) {
5275 			new = kmem_alloc_tryhard(sizeof (struct ddi_callback),
5276 			    &size, KM_NOSLEEP | KM_PANIC);
5277 			cbstats.nc_grow++;
5278 		} else {
5279 			callbackqfree = new->c_nfree;
5280 			cbstats.nc_L2++;
5281 		}
5282 	}
5283 	if (marker != NULL) {
5284 		marker->c_nlist = new;
5285 	} else {
5286 		*listid = (uintptr_t)new;
5287 	}
5288 	new->c_size = size;
5289 	new->c_nlist = NULL;
5290 	new->c_call = funcp;
5291 	new->c_arg = arg;
5292 	new->c_count = count;
5293 	cbstats.nc_new++;
5294 	cbstats.nc_alloc++;
5295 	if (cbstats.nc_alloc > cbstats.nc_maxlist)
5296 		cbstats.nc_maxlist = cbstats.nc_alloc;
5297 }
5298 
5299 void
5300 ddi_set_callback(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid)
5301 {
5302 	mutex_enter(&ddi_callback_mutex);
5303 	cbstats.nc_asked++;
5304 	if ((cbstats.nc_asked - cbstats.nc_run) > cbstats.nc_maxreq)
5305 		cbstats.nc_maxreq = (cbstats.nc_asked - cbstats.nc_run);
5306 	(void) callback_insert(funcp, arg, listid, 1);
5307 	mutex_exit(&ddi_callback_mutex);
5308 }
5309 
5310 static void
5311 real_callback_run(void *Queue)
5312 {
5313 	int (*funcp)(caddr_t);
5314 	caddr_t arg;
5315 	int count, rval;
5316 	uintptr_t *listid;
5317 	struct ddi_callback *list, *marker;
5318 	int check_pending = 1;
5319 	int pending = 0;
5320 
5321 	do {
5322 		mutex_enter(&ddi_callback_mutex);
5323 		listid = Queue;
5324 		list = (struct ddi_callback *)*listid;
5325 		if (list == NULL) {
5326 			mutex_exit(&ddi_callback_mutex);
5327 			return;
5328 		}
5329 		if (check_pending) {
5330 			marker = list;
5331 			while (marker != NULL) {
5332 				pending += marker->c_count;
5333 				marker = marker->c_nlist;
5334 			}
5335 			check_pending = 0;
5336 		}
5337 		ASSERT(pending > 0);
5338 		ASSERT(list->c_count > 0);
5339 		funcp = list->c_call;
5340 		arg = list->c_arg;
5341 		count = list->c_count;
5342 		*(uintptr_t *)Queue = (uintptr_t)list->c_nlist;
5343 		if (list >= &callbackq[0] &&
5344 		    list <= &callbackq[ncallbacks-1]) {
5345 			list->c_nfree = callbackqfree;
5346 			callbackqfree = list;
5347 		} else
5348 			kmem_free(list, list->c_size);
5349 
5350 		cbstats.nc_delete++;
5351 		cbstats.nc_alloc--;
5352 		mutex_exit(&ddi_callback_mutex);
5353 
5354 		do {
5355 			if ((rval = (*funcp)(arg)) == 0) {
5356 				pending -= count;
5357 				mutex_enter(&ddi_callback_mutex);
5358 				(void) callback_insert(funcp, arg, listid,
5359 				    count);
5360 				cbstats.nc_runouts++;
5361 			} else {
5362 				pending--;
5363 				mutex_enter(&ddi_callback_mutex);
5364 				cbstats.nc_run++;
5365 			}
5366 			mutex_exit(&ddi_callback_mutex);
5367 		} while (rval != 0 && (--count > 0));
5368 	} while (pending > 0);
5369 }
5370 
5371 void
5372 ddi_run_callback(uintptr_t *listid)
5373 {
5374 	softcall(real_callback_run, listid);
5375 }
5376 
5377 /*
5378  * ddi_periodic_t
5379  * ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval,
5380  *     int level)
5381  *
5382  * INTERFACE LEVEL
5383  *      Solaris DDI specific (Solaris DDI)
5384  *
5385  * PARAMETERS
5386  *      func: the callback function
5387  *
5388  *            The callback function will be invoked. The function is invoked
5389  *            in kernel context if the argument level passed is the zero.
5390  *            Otherwise it's invoked in interrupt context at the specified
5391  *            level.
5392  *
5393  *       arg: the argument passed to the callback function
5394  *
5395  *  interval: interval time
5396  *
5397  *    level : callback interrupt level
5398  *
5399  *            If the value is the zero, the callback function is invoked
5400  *            in kernel context. If the value is more than the zero, but
5401  *            less than or equal to ten, the callback function is invoked in
5402  *            interrupt context at the specified interrupt level, which may
5403  *            be used for real time applications.
5404  *
5405  *            This value must be in range of 0-10, which can be a numeric
5406  *            number or a pre-defined macro (DDI_IPL_0, ... , DDI_IPL_10).
5407  *
5408  * DESCRIPTION
5409  *      ddi_periodic_add(9F) schedules the specified function to be
5410  *      periodically invoked in the interval time.
5411  *
5412  *      As well as timeout(9F), the exact time interval over which the function
5413  *      takes effect cannot be guaranteed, but the value given is a close
5414  *      approximation.
5415  *
5416  *      Drivers waiting on behalf of processes with real-time constraints must
5417  *      pass non-zero value with the level argument to ddi_periodic_add(9F).
5418  *
5419  * RETURN VALUES
5420  *      ddi_periodic_add(9F) returns a non-zero opaque value (ddi_periodic_t),
5421  *      which must be used for ddi_periodic_delete(9F) to specify the request.
5422  *
5423  * CONTEXT
5424  *      ddi_periodic_add(9F) can be called in user or kernel context, but
5425  *      it cannot be called in interrupt context, which is different from
5426  *      timeout(9F).
5427  */
5428 ddi_periodic_t
5429 ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval, int level)
5430 {
5431 	/*
5432 	 * Sanity check of the argument level.
5433 	 */
5434 	if (level < DDI_IPL_0 || level > DDI_IPL_10)
5435 		cmn_err(CE_PANIC,
5436 		    "ddi_periodic_add: invalid interrupt level (%d).", level);
5437 
5438 	/*
5439 	 * Sanity check of the context. ddi_periodic_add() cannot be
5440 	 * called in either interrupt context or high interrupt context.
5441 	 */
5442 	if (servicing_interrupt())
5443 		cmn_err(CE_PANIC,
5444 		    "ddi_periodic_add: called in (high) interrupt context.");
5445 
5446 	return ((ddi_periodic_t)i_timeout(func, arg, interval, level));
5447 }
5448 
5449 /*
5450  * void
5451  * ddi_periodic_delete(ddi_periodic_t req)
5452  *
5453  * INTERFACE LEVEL
5454  *     Solaris DDI specific (Solaris DDI)
5455  *
5456  * PARAMETERS
5457  *     req: ddi_periodic_t opaque value ddi_periodic_add(9F) returned
5458  *     previously.
5459  *
5460  * DESCRIPTION
5461  *     ddi_periodic_delete(9F) cancels the ddi_periodic_add(9F) request
5462  *     previously requested.
5463  *
5464  *     ddi_periodic_delete(9F) will not return until the pending request
5465  *     is canceled or executed.
5466  *
5467  *     As well as untimeout(9F), calling ddi_periodic_delete(9F) for a
5468  *     timeout which is either running on another CPU, or has already
5469  *     completed causes no problems. However, unlike untimeout(9F), there is
5470  *     no restrictions on the lock which might be held across the call to
5471  *     ddi_periodic_delete(9F).
5472  *
5473  *     Drivers should be structured with the understanding that the arrival of
5474  *     both an interrupt and a timeout for that interrupt can occasionally
5475  *     occur, in either order.
5476  *
5477  * CONTEXT
5478  *     ddi_periodic_delete(9F) can be called in user or kernel context, but
5479  *     it cannot be called in interrupt context, which is different from
5480  *     untimeout(9F).
5481  */
5482 void
5483 ddi_periodic_delete(ddi_periodic_t req)
5484 {
5485 	/*
5486 	 * Sanity check of the context. ddi_periodic_delete() cannot be
5487 	 * called in either interrupt context or high interrupt context.
5488 	 */
5489 	if (servicing_interrupt())
5490 		cmn_err(CE_PANIC,
5491 		    "ddi_periodic_delete: called in (high) interrupt context.");
5492 
5493 	i_untimeout((timeout_t)req);
5494 }
5495 
5496 dev_info_t *
5497 nodevinfo(dev_t dev, int otyp)
5498 {
5499 	_NOTE(ARGUNUSED(dev, otyp))
5500 	return ((dev_info_t *)0);
5501 }
5502 
5503 /*
5504  * A driver should support its own getinfo(9E) entry point. This function
5505  * is provided as a convenience for ON drivers that don't expect their
5506  * getinfo(9E) entry point to be called. A driver that uses this must not
5507  * call ddi_create_minor_node.
5508  */
5509 int
5510 ddi_no_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5511 {
5512 	_NOTE(ARGUNUSED(dip, infocmd, arg, result))
5513 	return (DDI_FAILURE);
5514 }
5515 
5516 /*
5517  * A driver should support its own getinfo(9E) entry point. This function
5518  * is provided as a convenience for ON drivers that where the minor number
5519  * is the instance. Drivers that do not have 1:1 mapping must implement
5520  * their own getinfo(9E) function.
5521  */
5522 int
5523 ddi_getinfo_1to1(dev_info_t *dip, ddi_info_cmd_t infocmd,
5524     void *arg, void **result)
5525 {
5526 	_NOTE(ARGUNUSED(dip))
5527 	int	instance;
5528 
5529 	if (infocmd != DDI_INFO_DEVT2INSTANCE)
5530 		return (DDI_FAILURE);
5531 
5532 	instance = getminor((dev_t)(uintptr_t)arg);
5533 	*result = (void *)(uintptr_t)instance;
5534 	return (DDI_SUCCESS);
5535 }
5536 
5537 int
5538 ddifail(dev_info_t *devi, ddi_attach_cmd_t cmd)
5539 {
5540 	_NOTE(ARGUNUSED(devi, cmd))
5541 	return (DDI_FAILURE);
5542 }
5543 
5544 int
5545 ddi_no_dma_map(dev_info_t *dip, dev_info_t *rdip,
5546     struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
5547 {
5548 	_NOTE(ARGUNUSED(dip, rdip, dmareqp, handlep))
5549 	return (DDI_DMA_NOMAPPING);
5550 }
5551 
5552 int
5553 ddi_no_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
5554     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
5555 {
5556 	_NOTE(ARGUNUSED(dip, rdip, attr, waitfp, arg, handlep))
5557 	return (DDI_DMA_BADATTR);
5558 }
5559 
5560 int
5561 ddi_no_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
5562     ddi_dma_handle_t handle)
5563 {
5564 	_NOTE(ARGUNUSED(dip, rdip, handle))
5565 	return (DDI_FAILURE);
5566 }
5567 
5568 int
5569 ddi_no_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
5570     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
5571     ddi_dma_cookie_t *cp, uint_t *ccountp)
5572 {
5573 	_NOTE(ARGUNUSED(dip, rdip, handle, dmareq, cp, ccountp))
5574 	return (DDI_DMA_NOMAPPING);
5575 }
5576 
5577 int
5578 ddi_no_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
5579     ddi_dma_handle_t handle)
5580 {
5581 	_NOTE(ARGUNUSED(dip, rdip, handle))
5582 	return (DDI_FAILURE);
5583 }
5584 
5585 int
5586 ddi_no_dma_flush(dev_info_t *dip, dev_info_t *rdip,
5587     ddi_dma_handle_t handle, off_t off, size_t len,
5588     uint_t cache_flags)
5589 {
5590 	_NOTE(ARGUNUSED(dip, rdip, handle, off, len, cache_flags))
5591 	return (DDI_FAILURE);
5592 }
5593 
5594 int
5595 ddi_no_dma_win(dev_info_t *dip, dev_info_t *rdip,
5596     ddi_dma_handle_t handle, uint_t win, off_t *offp,
5597     size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
5598 {
5599 	_NOTE(ARGUNUSED(dip, rdip, handle, win, offp, lenp, cookiep, ccountp))
5600 	return (DDI_FAILURE);
5601 }
5602 
5603 int
5604 ddi_no_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
5605     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
5606     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
5607 {
5608 	_NOTE(ARGUNUSED(dip, rdip, handle, request, offp, lenp, objp, flags))
5609 	return (DDI_FAILURE);
5610 }
5611 
5612 void
5613 ddivoid(void)
5614 {}
5615 
5616 int
5617 nochpoll(dev_t dev, short events, int anyyet, short *reventsp,
5618     struct pollhead **pollhdrp)
5619 {
5620 	_NOTE(ARGUNUSED(dev, events, anyyet, reventsp, pollhdrp))
5621 	return (ENXIO);
5622 }
5623 
5624 cred_t *
5625 ddi_get_cred(void)
5626 {
5627 	return (CRED());
5628 }
5629 
5630 clock_t
5631 ddi_get_lbolt(void)
5632 {
5633 	return (lbolt);
5634 }
5635 
5636 time_t
5637 ddi_get_time(void)
5638 {
5639 	time_t	now;
5640 
5641 	if ((now = gethrestime_sec()) == 0) {
5642 		timestruc_t ts;
5643 		mutex_enter(&tod_lock);
5644 		ts = tod_get();
5645 		mutex_exit(&tod_lock);
5646 		return (ts.tv_sec);
5647 	} else {
5648 		return (now);
5649 	}
5650 }
5651 
5652 pid_t
5653 ddi_get_pid(void)
5654 {
5655 	return (ttoproc(curthread)->p_pid);
5656 }
5657 
5658 kt_did_t
5659 ddi_get_kt_did(void)
5660 {
5661 	return (curthread->t_did);
5662 }
5663 
5664 /*
5665  * This function returns B_TRUE if the caller can reasonably expect that a call
5666  * to cv_wait_sig(9F), cv_timedwait_sig(9F), or qwait_sig(9F) could be awakened
5667  * by user-level signal.  If it returns B_FALSE, then the caller should use
5668  * other means to make certain that the wait will not hang "forever."
5669  *
5670  * It does not check the signal mask, nor for reception of any particular
5671  * signal.
5672  *
5673  * Currently, a thread can receive a signal if it's not a kernel thread and it
5674  * is not in the middle of exit(2) tear-down.  Threads that are in that
5675  * tear-down effectively convert cv_wait_sig to cv_wait, cv_timedwait_sig to
5676  * cv_timedwait, and qwait_sig to qwait.
5677  */
5678 boolean_t
5679 ddi_can_receive_sig(void)
5680 {
5681 	proc_t *pp;
5682 
5683 	if (curthread->t_proc_flag & TP_LWPEXIT)
5684 		return (B_FALSE);
5685 	if ((pp = ttoproc(curthread)) == NULL)
5686 		return (B_FALSE);
5687 	return (pp->p_as != &kas);
5688 }
5689 
5690 /*
5691  * Swap bytes in 16-bit [half-]words
5692  */
5693 void
5694 swab(void *src, void *dst, size_t nbytes)
5695 {
5696 	uchar_t *pf = (uchar_t *)src;
5697 	uchar_t *pt = (uchar_t *)dst;
5698 	uchar_t tmp;
5699 	int nshorts;
5700 
5701 	nshorts = nbytes >> 1;
5702 
5703 	while (--nshorts >= 0) {
5704 		tmp = *pf++;
5705 		*pt++ = *pf++;
5706 		*pt++ = tmp;
5707 	}
5708 }
5709 
5710 static void
5711 ddi_append_minor_node(dev_info_t *ddip, struct ddi_minor_data *dmdp)
5712 {
5713 	int			circ;
5714 	struct ddi_minor_data	*dp;
5715 
5716 	ndi_devi_enter(ddip, &circ);
5717 	if ((dp = DEVI(ddip)->devi_minor) == (struct ddi_minor_data *)NULL) {
5718 		DEVI(ddip)->devi_minor = dmdp;
5719 	} else {
5720 		while (dp->next != (struct ddi_minor_data *)NULL)
5721 			dp = dp->next;
5722 		dp->next = dmdp;
5723 	}
5724 	ndi_devi_exit(ddip, circ);
5725 }
5726 
5727 /*
5728  * Part of the obsolete SunCluster DDI Hooks.
5729  * Keep for binary compatibility
5730  */
5731 minor_t
5732 ddi_getiminor(dev_t dev)
5733 {
5734 	return (getminor(dev));
5735 }
5736 
5737 static int
5738 i_log_devfs_minor_create(dev_info_t *dip, char *minor_name)
5739 {
5740 	int se_flag;
5741 	int kmem_flag;
5742 	int se_err;
5743 	char *pathname, *class_name;
5744 	sysevent_t *ev = NULL;
5745 	sysevent_id_t eid;
5746 	sysevent_value_t se_val;
5747 	sysevent_attr_list_t *ev_attr_list = NULL;
5748 
5749 	/* determine interrupt context */
5750 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
5751 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
5752 
5753 	i_ddi_di_cache_invalidate();
5754 
5755 #ifdef DEBUG
5756 	if ((se_flag == SE_NOSLEEP) && sunddi_debug) {
5757 		cmn_err(CE_CONT, "ddi_create_minor_node: called from "
5758 		    "interrupt level by driver %s",
5759 		    ddi_driver_name(dip));
5760 	}
5761 #endif /* DEBUG */
5762 
5763 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_CREATE, EP_DDI, se_flag);
5764 	if (ev == NULL) {
5765 		goto fail;
5766 	}
5767 
5768 	pathname = kmem_alloc(MAXPATHLEN, kmem_flag);
5769 	if (pathname == NULL) {
5770 		sysevent_free(ev);
5771 		goto fail;
5772 	}
5773 
5774 	(void) ddi_pathname(dip, pathname);
5775 	ASSERT(strlen(pathname));
5776 	se_val.value_type = SE_DATA_TYPE_STRING;
5777 	se_val.value.sv_string = pathname;
5778 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
5779 	    &se_val, se_flag) != 0) {
5780 		kmem_free(pathname, MAXPATHLEN);
5781 		sysevent_free(ev);
5782 		goto fail;
5783 	}
5784 	kmem_free(pathname, MAXPATHLEN);
5785 
5786 	/* add the device class attribute */
5787 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
5788 		se_val.value_type = SE_DATA_TYPE_STRING;
5789 		se_val.value.sv_string = class_name;
5790 		if (sysevent_add_attr(&ev_attr_list,
5791 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
5792 			sysevent_free_attr(ev_attr_list);
5793 			goto fail;
5794 		}
5795 	}
5796 
5797 	/*
5798 	 * allow for NULL minor names
5799 	 */
5800 	if (minor_name != NULL) {
5801 		se_val.value.sv_string = minor_name;
5802 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
5803 		    &se_val, se_flag) != 0) {
5804 			sysevent_free_attr(ev_attr_list);
5805 			sysevent_free(ev);
5806 			goto fail;
5807 		}
5808 	}
5809 
5810 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
5811 		sysevent_free_attr(ev_attr_list);
5812 		sysevent_free(ev);
5813 		goto fail;
5814 	}
5815 
5816 	if ((se_err = log_sysevent(ev, se_flag, &eid)) != 0) {
5817 		if (se_err == SE_NO_TRANSPORT) {
5818 			cmn_err(CE_WARN, "/devices or /dev may not be current "
5819 			    "for driver %s (%s). Run devfsadm -i %s",
5820 			    ddi_driver_name(dip), "syseventd not responding",
5821 			    ddi_driver_name(dip));
5822 		} else {
5823 			sysevent_free(ev);
5824 			goto fail;
5825 		}
5826 	}
5827 
5828 	sysevent_free(ev);
5829 	return (DDI_SUCCESS);
5830 fail:
5831 	cmn_err(CE_WARN, "/devices or /dev may not be current "
5832 	    "for driver %s. Run devfsadm -i %s",
5833 	    ddi_driver_name(dip), ddi_driver_name(dip));
5834 	return (DDI_SUCCESS);
5835 }
5836 
5837 /*
5838  * failing to remove a minor node is not of interest
5839  * therefore we do not generate an error message
5840  */
5841 static int
5842 i_log_devfs_minor_remove(dev_info_t *dip, char *minor_name)
5843 {
5844 	char *pathname, *class_name;
5845 	sysevent_t *ev;
5846 	sysevent_id_t eid;
5847 	sysevent_value_t se_val;
5848 	sysevent_attr_list_t *ev_attr_list = NULL;
5849 
5850 	/*
5851 	 * only log ddi_remove_minor_node() calls outside the scope
5852 	 * of attach/detach reconfigurations and when the dip is
5853 	 * still initialized.
5854 	 */
5855 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip) ||
5856 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
5857 		return (DDI_SUCCESS);
5858 	}
5859 
5860 	i_ddi_di_cache_invalidate();
5861 
5862 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_REMOVE, EP_DDI, SE_SLEEP);
5863 	if (ev == NULL) {
5864 		return (DDI_SUCCESS);
5865 	}
5866 
5867 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5868 	if (pathname == NULL) {
5869 		sysevent_free(ev);
5870 		return (DDI_SUCCESS);
5871 	}
5872 
5873 	(void) ddi_pathname(dip, pathname);
5874 	ASSERT(strlen(pathname));
5875 	se_val.value_type = SE_DATA_TYPE_STRING;
5876 	se_val.value.sv_string = pathname;
5877 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
5878 	    &se_val, SE_SLEEP) != 0) {
5879 		kmem_free(pathname, MAXPATHLEN);
5880 		sysevent_free(ev);
5881 		return (DDI_SUCCESS);
5882 	}
5883 
5884 	kmem_free(pathname, MAXPATHLEN);
5885 
5886 	/*
5887 	 * allow for NULL minor names
5888 	 */
5889 	if (minor_name != NULL) {
5890 		se_val.value.sv_string = minor_name;
5891 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
5892 		    &se_val, SE_SLEEP) != 0) {
5893 			sysevent_free_attr(ev_attr_list);
5894 			goto fail;
5895 		}
5896 	}
5897 
5898 	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
5899 		/* add the device class, driver name and instance attributes */
5900 
5901 		se_val.value_type = SE_DATA_TYPE_STRING;
5902 		se_val.value.sv_string = class_name;
5903 		if (sysevent_add_attr(&ev_attr_list,
5904 		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
5905 			sysevent_free_attr(ev_attr_list);
5906 			goto fail;
5907 		}
5908 
5909 		se_val.value_type = SE_DATA_TYPE_STRING;
5910 		se_val.value.sv_string = (char *)ddi_driver_name(dip);
5911 		if (sysevent_add_attr(&ev_attr_list,
5912 		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
5913 			sysevent_free_attr(ev_attr_list);
5914 			goto fail;
5915 		}
5916 
5917 		se_val.value_type = SE_DATA_TYPE_INT32;
5918 		se_val.value.sv_int32 = ddi_get_instance(dip);
5919 		if (sysevent_add_attr(&ev_attr_list,
5920 		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
5921 			sysevent_free_attr(ev_attr_list);
5922 			goto fail;
5923 		}
5924 
5925 	}
5926 
5927 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
5928 		sysevent_free_attr(ev_attr_list);
5929 	} else {
5930 		(void) log_sysevent(ev, SE_SLEEP, &eid);
5931 	}
5932 fail:
5933 	sysevent_free(ev);
5934 	return (DDI_SUCCESS);
5935 }
5936 
5937 /*
5938  * Derive the device class of the node.
5939  * Device class names aren't defined yet. Until this is done we use
5940  * devfs event subclass names as device class names.
5941  */
5942 static int
5943 derive_devi_class(dev_info_t *dip, char *node_type, int flag)
5944 {
5945 	int rv = DDI_SUCCESS;
5946 
5947 	if (i_ddi_devi_class(dip) == NULL) {
5948 		if (strncmp(node_type, DDI_NT_BLOCK,
5949 		    sizeof (DDI_NT_BLOCK) - 1) == 0 &&
5950 		    (node_type[sizeof (DDI_NT_BLOCK) - 1] == '\0' ||
5951 		    node_type[sizeof (DDI_NT_BLOCK) - 1] == ':') &&
5952 		    strcmp(node_type, DDI_NT_FD) != 0) {
5953 
5954 			rv = i_ddi_set_devi_class(dip, ESC_DISK, flag);
5955 
5956 		} else if (strncmp(node_type, DDI_NT_NET,
5957 		    sizeof (DDI_NT_NET) - 1) == 0 &&
5958 		    (node_type[sizeof (DDI_NT_NET) - 1] == '\0' ||
5959 		    node_type[sizeof (DDI_NT_NET) - 1] == ':')) {
5960 
5961 			rv = i_ddi_set_devi_class(dip, ESC_NETWORK, flag);
5962 
5963 		} else if (strncmp(node_type, DDI_NT_PRINTER,
5964 		    sizeof (DDI_NT_PRINTER) - 1) == 0 &&
5965 		    (node_type[sizeof (DDI_NT_PRINTER) - 1] == '\0' ||
5966 		    node_type[sizeof (DDI_NT_PRINTER) - 1] == ':')) {
5967 
5968 			rv = i_ddi_set_devi_class(dip, ESC_PRINTER, flag);
5969 
5970 		} else if (strncmp(node_type, DDI_PSEUDO,
5971 		    sizeof (DDI_PSEUDO) -1) == 0 &&
5972 		    (strncmp(ESC_LOFI, ddi_node_name(dip),
5973 		    sizeof (ESC_LOFI) -1) == 0)) {
5974 			rv = i_ddi_set_devi_class(dip, ESC_LOFI, flag);
5975 		}
5976 	}
5977 
5978 	return (rv);
5979 }
5980 
5981 /*
5982  * Check compliance with PSARC 2003/375:
5983  *
5984  * The name must contain only characters a-z, A-Z, 0-9 or _ and it must not
5985  * exceed IFNAMSIZ (16) characters in length.
5986  */
5987 static boolean_t
5988 verify_name(char *name)
5989 {
5990 	size_t	len = strlen(name);
5991 	char	*cp;
5992 
5993 	if (len == 0 || len > IFNAMSIZ)
5994 		return (B_FALSE);
5995 
5996 	for (cp = name; *cp != '\0'; cp++) {
5997 		if (!isalnum(*cp) && *cp != '_')
5998 			return (B_FALSE);
5999 	}
6000 
6001 	return (B_TRUE);
6002 }
6003 
6004 /*
6005  * ddi_create_minor_common:	Create a  ddi_minor_data structure and
6006  *				attach it to the given devinfo node.
6007  */
6008 
6009 int
6010 ddi_create_minor_common(dev_info_t *dip, char *name, int spec_type,
6011     minor_t minor_num, char *node_type, int flag, ddi_minor_type mtype,
6012     const char *read_priv, const char *write_priv, mode_t priv_mode)
6013 {
6014 	struct ddi_minor_data *dmdp;
6015 	major_t major;
6016 
6017 	if (spec_type != S_IFCHR && spec_type != S_IFBLK)
6018 		return (DDI_FAILURE);
6019 
6020 	if (name == NULL)
6021 		return (DDI_FAILURE);
6022 
6023 	/*
6024 	 * Log a message if the minor number the driver is creating
6025 	 * is not expressible on the on-disk filesystem (currently
6026 	 * this is limited to 18 bits both by UFS). The device can
6027 	 * be opened via devfs, but not by device special files created
6028 	 * via mknod().
6029 	 */
6030 	if (minor_num > L_MAXMIN32) {
6031 		cmn_err(CE_WARN,
6032 		    "%s%d:%s minor 0x%x too big for 32-bit applications",
6033 		    ddi_driver_name(dip), ddi_get_instance(dip),
6034 		    name, minor_num);
6035 		return (DDI_FAILURE);
6036 	}
6037 
6038 	/* dip must be bound and attached */
6039 	major = ddi_driver_major(dip);
6040 	ASSERT(major != DDI_MAJOR_T_NONE);
6041 
6042 	/*
6043 	 * Default node_type to DDI_PSEUDO and issue notice in debug mode
6044 	 */
6045 	if (node_type == NULL) {
6046 		node_type = DDI_PSEUDO;
6047 		NDI_CONFIG_DEBUG((CE_NOTE, "!illegal node_type NULL for %s%d "
6048 		    " minor node %s; default to DDI_PSEUDO",
6049 		    ddi_driver_name(dip), ddi_get_instance(dip), name));
6050 	}
6051 
6052 	/*
6053 	 * If the driver is a network driver, ensure that the name falls within
6054 	 * the interface naming constraints specified by PSARC/2003/375.
6055 	 */
6056 	if (strcmp(node_type, DDI_NT_NET) == 0) {
6057 		if (!verify_name(name))
6058 			return (DDI_FAILURE);
6059 
6060 		if (mtype == DDM_MINOR) {
6061 			struct devnames *dnp = &devnamesp[major];
6062 
6063 			/* Mark driver as a network driver */
6064 			LOCK_DEV_OPS(&dnp->dn_lock);
6065 			dnp->dn_flags |= DN_NETWORK_DRIVER;
6066 			UNLOCK_DEV_OPS(&dnp->dn_lock);
6067 		}
6068 	}
6069 
6070 	if (mtype == DDM_MINOR) {
6071 		if (derive_devi_class(dip,  node_type, KM_NOSLEEP) !=
6072 		    DDI_SUCCESS)
6073 			return (DDI_FAILURE);
6074 	}
6075 
6076 	/*
6077 	 * Take care of minor number information for the node.
6078 	 */
6079 
6080 	if ((dmdp = kmem_zalloc(sizeof (struct ddi_minor_data),
6081 	    KM_NOSLEEP)) == NULL) {
6082 		return (DDI_FAILURE);
6083 	}
6084 	if ((dmdp->ddm_name = i_ddi_strdup(name, KM_NOSLEEP)) == NULL) {
6085 		kmem_free(dmdp, sizeof (struct ddi_minor_data));
6086 		return (DDI_FAILURE);
6087 	}
6088 	dmdp->dip = dip;
6089 	dmdp->ddm_dev = makedevice(major, minor_num);
6090 	dmdp->ddm_spec_type = spec_type;
6091 	dmdp->ddm_node_type = node_type;
6092 	dmdp->type = mtype;
6093 	if (flag & CLONE_DEV) {
6094 		dmdp->type = DDM_ALIAS;
6095 		dmdp->ddm_dev = makedevice(ddi_driver_major(clone_dip), major);
6096 	}
6097 	if (flag & PRIVONLY_DEV) {
6098 		dmdp->ddm_flags |= DM_NO_FSPERM;
6099 	}
6100 	if (read_priv || write_priv) {
6101 		dmdp->ddm_node_priv =
6102 		    devpolicy_priv_by_name(read_priv, write_priv);
6103 	}
6104 	dmdp->ddm_priv_mode = priv_mode;
6105 
6106 	ddi_append_minor_node(dip, dmdp);
6107 
6108 	/*
6109 	 * only log ddi_create_minor_node() calls which occur
6110 	 * outside the scope of attach(9e)/detach(9e) reconfigurations
6111 	 */
6112 	if (!(DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) &&
6113 	    mtype != DDM_INTERNAL_PATH) {
6114 		(void) i_log_devfs_minor_create(dip, name);
6115 	}
6116 
6117 	/*
6118 	 * Check if any dacf rules match the creation of this minor node
6119 	 */
6120 	dacfc_match_create_minor(name, node_type, dip, dmdp, flag);
6121 	return (DDI_SUCCESS);
6122 }
6123 
6124 int
6125 ddi_create_minor_node(dev_info_t *dip, char *name, int spec_type,
6126     minor_t minor_num, char *node_type, int flag)
6127 {
6128 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
6129 	    node_type, flag, DDM_MINOR, NULL, NULL, 0));
6130 }
6131 
6132 int
6133 ddi_create_priv_minor_node(dev_info_t *dip, char *name, int spec_type,
6134     minor_t minor_num, char *node_type, int flag,
6135     const char *rdpriv, const char *wrpriv, mode_t priv_mode)
6136 {
6137 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
6138 	    node_type, flag, DDM_MINOR, rdpriv, wrpriv, priv_mode));
6139 }
6140 
6141 int
6142 ddi_create_default_minor_node(dev_info_t *dip, char *name, int spec_type,
6143     minor_t minor_num, char *node_type, int flag)
6144 {
6145 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
6146 	    node_type, flag, DDM_DEFAULT, NULL, NULL, 0));
6147 }
6148 
6149 /*
6150  * Internal (non-ddi) routine for drivers to export names known
6151  * to the kernel (especially ddi_pathname_to_dev_t and friends)
6152  * but not exported externally to /dev
6153  */
6154 int
6155 ddi_create_internal_pathname(dev_info_t *dip, char *name, int spec_type,
6156     minor_t minor_num)
6157 {
6158 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
6159 	    "internal", 0, DDM_INTERNAL_PATH, NULL, NULL, 0));
6160 }
6161 
6162 void
6163 ddi_remove_minor_node(dev_info_t *dip, char *name)
6164 {
6165 	int			circ;
6166 	struct ddi_minor_data	*dmdp, *dmdp1;
6167 	struct ddi_minor_data	**dmdp_prev;
6168 
6169 	ndi_devi_enter(dip, &circ);
6170 	dmdp_prev = &DEVI(dip)->devi_minor;
6171 	dmdp = DEVI(dip)->devi_minor;
6172 	while (dmdp != NULL) {
6173 		dmdp1 = dmdp->next;
6174 		if ((name == NULL || (dmdp->ddm_name != NULL &&
6175 		    strcmp(name, dmdp->ddm_name) == 0))) {
6176 			if (dmdp->ddm_name != NULL) {
6177 				if (dmdp->type != DDM_INTERNAL_PATH)
6178 					(void) i_log_devfs_minor_remove(dip,
6179 					    dmdp->ddm_name);
6180 				kmem_free(dmdp->ddm_name,
6181 				    strlen(dmdp->ddm_name) + 1);
6182 			}
6183 			/*
6184 			 * Release device privilege, if any.
6185 			 * Release dacf client data associated with this minor
6186 			 * node by storing NULL.
6187 			 */
6188 			if (dmdp->ddm_node_priv)
6189 				dpfree(dmdp->ddm_node_priv);
6190 			dacf_store_info((dacf_infohdl_t)dmdp, NULL);
6191 			kmem_free(dmdp, sizeof (struct ddi_minor_data));
6192 			*dmdp_prev = dmdp1;
6193 			/*
6194 			 * OK, we found it, so get out now -- if we drive on,
6195 			 * we will strcmp against garbage.  See 1139209.
6196 			 */
6197 			if (name != NULL)
6198 				break;
6199 		} else {
6200 			dmdp_prev = &dmdp->next;
6201 		}
6202 		dmdp = dmdp1;
6203 	}
6204 	ndi_devi_exit(dip, circ);
6205 }
6206 
6207 
6208 int
6209 ddi_in_panic()
6210 {
6211 	return (panicstr != NULL);
6212 }
6213 
6214 
6215 /*
6216  * Find first bit set in a mask (returned counting from 1 up)
6217  */
6218 
6219 int
6220 ddi_ffs(long mask)
6221 {
6222 	return (ffs(mask));
6223 }
6224 
6225 /*
6226  * Find last bit set. Take mask and clear
6227  * all but the most significant bit, and
6228  * then let ffs do the rest of the work.
6229  *
6230  * Algorithm courtesy of Steve Chessin.
6231  */
6232 
6233 int
6234 ddi_fls(long mask)
6235 {
6236 	while (mask) {
6237 		long nx;
6238 
6239 		if ((nx = (mask & (mask - 1))) == 0)
6240 			break;
6241 		mask = nx;
6242 	}
6243 	return (ffs(mask));
6244 }
6245 
6246 /*
6247  * The ddi_soft_state_* routines comprise generic storage management utilities
6248  * for driver soft state structures (in "the old days," this was done with
6249  * statically sized array - big systems and dynamic loading and unloading
6250  * make heap allocation more attractive).
6251  */
6252 
6253 /*
6254  * Allocate a set of pointers to 'n_items' objects of size 'size'
6255  * bytes.  Each pointer is initialized to nil.
6256  *
6257  * The 'size' and 'n_items' values are stashed in the opaque
6258  * handle returned to the caller.
6259  *
6260  * This implementation interprets 'set of pointers' to mean 'array
6261  * of pointers' but note that nothing in the interface definition
6262  * precludes an implementation that uses, for example, a linked list.
6263  * However there should be a small efficiency gain from using an array
6264  * at lookup time.
6265  *
6266  * NOTE	As an optimization, we make our growable array allocations in
6267  *	powers of two (bytes), since that's how much kmem_alloc (currently)
6268  *	gives us anyway.  It should save us some free/realloc's ..
6269  *
6270  *	As a further optimization, we make the growable array start out
6271  *	with MIN_N_ITEMS in it.
6272  */
6273 
6274 #define	MIN_N_ITEMS	8	/* 8 void *'s == 32 bytes */
6275 
6276 int
6277 ddi_soft_state_init(void **state_p, size_t size, size_t n_items)
6278 {
6279 	i_ddi_soft_state	*ss;
6280 
6281 	if (state_p == NULL || size == 0)
6282 		return (EINVAL);
6283 
6284 	ss = kmem_zalloc(sizeof (*ss), KM_SLEEP);
6285 	mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL);
6286 	ss->size = size;
6287 
6288 	if (n_items < MIN_N_ITEMS)
6289 		ss->n_items = MIN_N_ITEMS;
6290 	else {
6291 		int bitlog;
6292 
6293 		if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items))
6294 			bitlog--;
6295 		ss->n_items = 1 << bitlog;
6296 	}
6297 
6298 	ASSERT(ss->n_items >= n_items);
6299 
6300 	ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP);
6301 
6302 	*state_p = ss;
6303 	return (0);
6304 }
6305 
6306 /*
6307  * Allocate a state structure of size 'size' to be associated
6308  * with item 'item'.
6309  *
6310  * In this implementation, the array is extended to
6311  * allow the requested offset, if needed.
6312  */
6313 int
6314 ddi_soft_state_zalloc(void *state, int item)
6315 {
6316 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
6317 	void			**array;
6318 	void			*new_element;
6319 
6320 	if ((state == NULL) || (item < 0))
6321 		return (DDI_FAILURE);
6322 
6323 	mutex_enter(&ss->lock);
6324 	if (ss->size == 0) {
6325 		mutex_exit(&ss->lock);
6326 		cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle: %s",
6327 		    mod_containing_pc(caller()));
6328 		return (DDI_FAILURE);
6329 	}
6330 
6331 	array = ss->array;	/* NULL if ss->n_items == 0 */
6332 	ASSERT(ss->n_items != 0 && array != NULL);
6333 
6334 	/*
6335 	 * refuse to tread on an existing element
6336 	 */
6337 	if (item < ss->n_items && array[item] != NULL) {
6338 		mutex_exit(&ss->lock);
6339 		return (DDI_FAILURE);
6340 	}
6341 
6342 	/*
6343 	 * Allocate a new element to plug in
6344 	 */
6345 	new_element = kmem_zalloc(ss->size, KM_SLEEP);
6346 
6347 	/*
6348 	 * Check if the array is big enough, if not, grow it.
6349 	 */
6350 	if (item >= ss->n_items) {
6351 		void			**new_array;
6352 		size_t			new_n_items;
6353 		struct i_ddi_soft_state	*dirty;
6354 
6355 		/*
6356 		 * Allocate a new array of the right length, copy
6357 		 * all the old pointers to the new array, then
6358 		 * if it exists at all, put the old array on the
6359 		 * dirty list.
6360 		 *
6361 		 * Note that we can't kmem_free() the old array.
6362 		 *
6363 		 * Why -- well the 'get' operation is 'mutex-free', so we
6364 		 * can't easily catch a suspended thread that is just about
6365 		 * to dereference the array we just grew out of.  So we
6366 		 * cons up a header and put it on a list of 'dirty'
6367 		 * pointer arrays.  (Dirty in the sense that there may
6368 		 * be suspended threads somewhere that are in the middle
6369 		 * of referencing them).  Fortunately, we -can- garbage
6370 		 * collect it all at ddi_soft_state_fini time.
6371 		 */
6372 		new_n_items = ss->n_items;
6373 		while (new_n_items < (1 + item))
6374 			new_n_items <<= 1;	/* double array size .. */
6375 
6376 		ASSERT(new_n_items >= (1 + item));	/* sanity check! */
6377 
6378 		new_array = kmem_zalloc(new_n_items * sizeof (void *),
6379 		    KM_SLEEP);
6380 		/*
6381 		 * Copy the pointers into the new array
6382 		 */
6383 		bcopy(array, new_array, ss->n_items * sizeof (void *));
6384 
6385 		/*
6386 		 * Save the old array on the dirty list
6387 		 */
6388 		dirty = kmem_zalloc(sizeof (*dirty), KM_SLEEP);
6389 		dirty->array = ss->array;
6390 		dirty->n_items = ss->n_items;
6391 		dirty->next = ss->next;
6392 		ss->next = dirty;
6393 
6394 		ss->array = (array = new_array);
6395 		ss->n_items = new_n_items;
6396 	}
6397 
6398 	ASSERT(array != NULL && item < ss->n_items && array[item] == NULL);
6399 
6400 	array[item] = new_element;
6401 
6402 	mutex_exit(&ss->lock);
6403 	return (DDI_SUCCESS);
6404 }
6405 
6406 /*
6407  * Fetch a pointer to the allocated soft state structure.
6408  *
6409  * This is designed to be cheap.
6410  *
6411  * There's an argument that there should be more checking for
6412  * nil pointers and out of bounds on the array.. but we do a lot
6413  * of that in the alloc/free routines.
6414  *
6415  * An array has the convenience that we don't need to lock read-access
6416  * to it c.f. a linked list.  However our "expanding array" strategy
6417  * means that we should hold a readers lock on the i_ddi_soft_state
6418  * structure.
6419  *
6420  * However, from a performance viewpoint, we need to do it without
6421  * any locks at all -- this also makes it a leaf routine.  The algorithm
6422  * is 'lock-free' because we only discard the pointer arrays at
6423  * ddi_soft_state_fini() time.
6424  */
6425 void *
6426 ddi_get_soft_state(void *state, int item)
6427 {
6428 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
6429 
6430 	ASSERT((ss != NULL) && (item >= 0));
6431 
6432 	if (item < ss->n_items && ss->array != NULL)
6433 		return (ss->array[item]);
6434 	return (NULL);
6435 }
6436 
6437 /*
6438  * Free the state structure corresponding to 'item.'   Freeing an
6439  * element that has either gone or was never allocated is not
6440  * considered an error.  Note that we free the state structure, but
6441  * we don't shrink our pointer array, or discard 'dirty' arrays,
6442  * since even a few pointers don't really waste too much memory.
6443  *
6444  * Passing an item number that is out of bounds, or a null pointer will
6445  * provoke an error message.
6446  */
6447 void
6448 ddi_soft_state_free(void *state, int item)
6449 {
6450 	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
6451 	void			**array;
6452 	void			*element;
6453 	static char		msg[] = "ddi_soft_state_free:";
6454 
6455 	if (ss == NULL) {
6456 		cmn_err(CE_WARN, "%s null handle: %s",
6457 		    msg, mod_containing_pc(caller()));
6458 		return;
6459 	}
6460 
6461 	element = NULL;
6462 
6463 	mutex_enter(&ss->lock);
6464 
6465 	if ((array = ss->array) == NULL || ss->size == 0) {
6466 		cmn_err(CE_WARN, "%s bad handle: %s",
6467 		    msg, mod_containing_pc(caller()));
6468 	} else if (item < 0 || item >= ss->n_items) {
6469 		cmn_err(CE_WARN, "%s item %d not in range [0..%lu]: %s",
6470 		    msg, item, ss->n_items - 1, mod_containing_pc(caller()));
6471 	} else if (array[item] != NULL) {
6472 		element = array[item];
6473 		array[item] = NULL;
6474 	}
6475 
6476 	mutex_exit(&ss->lock);
6477 
6478 	if (element)
6479 		kmem_free(element, ss->size);
6480 }
6481 
6482 /*
6483  * Free the entire set of pointers, and any
6484  * soft state structures contained therein.
6485  *
6486  * Note that we don't grab the ss->lock mutex, even though
6487  * we're inspecting the various fields of the data structure.
6488  *
6489  * There is an implicit assumption that this routine will
6490  * never run concurrently with any of the above on this
6491  * particular state structure i.e. by the time the driver
6492  * calls this routine, there should be no other threads
6493  * running in the driver.
6494  */
6495 void
6496 ddi_soft_state_fini(void **state_p)
6497 {
6498 	i_ddi_soft_state	*ss, *dirty;
6499 	int			item;
6500 	static char		msg[] = "ddi_soft_state_fini:";
6501 
6502 	if (state_p == NULL ||
6503 	    (ss = (i_ddi_soft_state *)(*state_p)) == NULL) {
6504 		cmn_err(CE_WARN, "%s null handle: %s",
6505 		    msg, mod_containing_pc(caller()));
6506 		return;
6507 	}
6508 
6509 	if (ss->size == 0) {
6510 		cmn_err(CE_WARN, "%s bad handle: %s",
6511 		    msg, mod_containing_pc(caller()));
6512 		return;
6513 	}
6514 
6515 	if (ss->n_items > 0) {
6516 		for (item = 0; item < ss->n_items; item++)
6517 			ddi_soft_state_free(ss, item);
6518 		kmem_free(ss->array, ss->n_items * sizeof (void *));
6519 	}
6520 
6521 	/*
6522 	 * Now delete any dirty arrays from previous 'grow' operations
6523 	 */
6524 	for (dirty = ss->next; dirty; dirty = ss->next) {
6525 		ss->next = dirty->next;
6526 		kmem_free(dirty->array, dirty->n_items * sizeof (void *));
6527 		kmem_free(dirty, sizeof (*dirty));
6528 	}
6529 
6530 	mutex_destroy(&ss->lock);
6531 	kmem_free(ss, sizeof (*ss));
6532 
6533 	*state_p = NULL;
6534 }
6535 
6536 #define	SS_N_ITEMS_PER_HASH	16
6537 #define	SS_MIN_HASH_SZ		16
6538 #define	SS_MAX_HASH_SZ		4096
6539 
6540 int
6541 ddi_soft_state_bystr_init(ddi_soft_state_bystr **state_p, size_t size,
6542     int n_items)
6543 {
6544 	i_ddi_soft_state_bystr	*sss;
6545 	int			hash_sz;
6546 
6547 	ASSERT(state_p && size && n_items);
6548 	if ((state_p == NULL) || (size == 0) || (n_items == 0))
6549 		return (EINVAL);
6550 
6551 	/* current implementation is based on hash, convert n_items to hash */
6552 	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
6553 	if (hash_sz < SS_MIN_HASH_SZ)
6554 		hash_sz = SS_MIN_HASH_SZ;
6555 	else if (hash_sz > SS_MAX_HASH_SZ)
6556 		hash_sz = SS_MAX_HASH_SZ;
6557 
6558 	/* allocate soft_state pool */
6559 	sss = kmem_zalloc(sizeof (*sss), KM_SLEEP);
6560 	sss->ss_size = size;
6561 	sss->ss_mod_hash = mod_hash_create_strhash("soft_state_bystr",
6562 	    hash_sz, mod_hash_null_valdtor);
6563 	*state_p = (ddi_soft_state_bystr *)sss;
6564 	return (0);
6565 }
6566 
6567 int
6568 ddi_soft_state_bystr_zalloc(ddi_soft_state_bystr *state, const char *str)
6569 {
6570 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
6571 	void			*sso;
6572 	char			*dup_str;
6573 
6574 	ASSERT(sss && str && sss->ss_mod_hash);
6575 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
6576 		return (DDI_FAILURE);
6577 	sso = kmem_zalloc(sss->ss_size, KM_SLEEP);
6578 	dup_str = i_ddi_strdup((char *)str, KM_SLEEP);
6579 	if (mod_hash_insert(sss->ss_mod_hash,
6580 	    (mod_hash_key_t)dup_str, (mod_hash_val_t)sso) == 0)
6581 		return (DDI_SUCCESS);
6582 
6583 	/*
6584 	 * The only error from an strhash insert is caused by a duplicate key.
6585 	 * We refuse to tread on an existing elements, so free and fail.
6586 	 */
6587 	kmem_free(dup_str, strlen(dup_str) + 1);
6588 	kmem_free(sso, sss->ss_size);
6589 	return (DDI_FAILURE);
6590 }
6591 
6592 void *
6593 ddi_soft_state_bystr_get(ddi_soft_state_bystr *state, const char *str)
6594 {
6595 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
6596 	void			*sso;
6597 
6598 	ASSERT(sss && str && sss->ss_mod_hash);
6599 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
6600 		return (NULL);
6601 
6602 	if (mod_hash_find(sss->ss_mod_hash,
6603 	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso) == 0)
6604 		return (sso);
6605 	return (NULL);
6606 }
6607 
6608 void
6609 ddi_soft_state_bystr_free(ddi_soft_state_bystr *state, const char *str)
6610 {
6611 	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
6612 	void			*sso;
6613 
6614 	ASSERT(sss && str && sss->ss_mod_hash);
6615 	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
6616 		return;
6617 
6618 	(void) mod_hash_remove(sss->ss_mod_hash,
6619 	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso);
6620 	kmem_free(sso, sss->ss_size);
6621 }
6622 
6623 void
6624 ddi_soft_state_bystr_fini(ddi_soft_state_bystr **state_p)
6625 {
6626 	i_ddi_soft_state_bystr	*sss;
6627 
6628 	ASSERT(state_p);
6629 	if (state_p == NULL)
6630 		return;
6631 
6632 	sss = (i_ddi_soft_state_bystr *)(*state_p);
6633 	if (sss == NULL)
6634 		return;
6635 
6636 	ASSERT(sss->ss_mod_hash);
6637 	if (sss->ss_mod_hash) {
6638 		mod_hash_destroy_strhash(sss->ss_mod_hash);
6639 		sss->ss_mod_hash = NULL;
6640 	}
6641 
6642 	kmem_free(sss, sizeof (*sss));
6643 	*state_p = NULL;
6644 }
6645 
6646 /*
6647  * The ddi_strid_* routines provide string-to-index management utilities.
6648  */
6649 /* allocate and initialize an strid set */
6650 int
6651 ddi_strid_init(ddi_strid **strid_p, int n_items)
6652 {
6653 	i_ddi_strid	*ss;
6654 	int		hash_sz;
6655 
6656 	if (strid_p == NULL)
6657 		return (DDI_FAILURE);
6658 
6659 	/* current implementation is based on hash, convert n_items to hash */
6660 	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
6661 	if (hash_sz < SS_MIN_HASH_SZ)
6662 		hash_sz = SS_MIN_HASH_SZ;
6663 	else if (hash_sz > SS_MAX_HASH_SZ)
6664 		hash_sz = SS_MAX_HASH_SZ;
6665 
6666 	ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
6667 	ss->strid_space = id_space_create("strid", 1, n_items);
6668 	ss->strid_bystr = mod_hash_create_strhash("strid_bystr", hash_sz,
6669 	    mod_hash_null_valdtor);
6670 	ss->strid_byid = mod_hash_create_idhash("strid_byid", hash_sz,
6671 	    mod_hash_null_valdtor);
6672 	*strid_p = (ddi_strid *)ss;
6673 	return (DDI_SUCCESS);
6674 }
6675 
6676 #define	ID_FIXED_SIZE	0x1
6677 
6678 /* allocate an id mapping within the specified set for str, return id */
6679 static id_t
6680 i_ddi_strid_alloc(ddi_strid *strid, char *str, int flags)
6681 {
6682 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6683 	id_t		id;
6684 	char		*s;
6685 
6686 	ASSERT(ss && str);
6687 	if ((ss == NULL) || (str == NULL))
6688 		return (0);
6689 
6690 	/*
6691 	 * Allocate an id using VM_FIRSTFIT in order to keep allocated id
6692 	 * range as compressed as possible.  This is important to minimize
6693 	 * the amount of space used when the id is used as a ddi_soft_state
6694 	 * index by the caller.
6695 	 *
6696 	 * If ID_FIXED_SIZE, use the _nosleep variant to fail rather
6697 	 * than sleep in id_allocff()
6698 	 */
6699 	if (flags & ID_FIXED_SIZE) {
6700 		id = id_allocff_nosleep(ss->strid_space);
6701 		if (id == (id_t)-1)
6702 			return (0);
6703 	} else {
6704 		id = id_allocff(ss->strid_space);
6705 	}
6706 
6707 	/*
6708 	 * NOTE: since we create and destroy in unison we can save space by
6709 	 * using bystr key as the byid value.  This means destroy must occur
6710 	 * in (byid, bystr) order.
6711 	 */
6712 	s = i_ddi_strdup(str, KM_SLEEP);
6713 	if (mod_hash_insert(ss->strid_bystr, (mod_hash_key_t)s,
6714 	    (mod_hash_val_t)(intptr_t)id) != 0) {
6715 		ddi_strid_free(strid, id);
6716 		return (0);
6717 	}
6718 	if (mod_hash_insert(ss->strid_byid, (mod_hash_key_t)(intptr_t)id,
6719 	    (mod_hash_val_t)s) != 0) {
6720 		ddi_strid_free(strid, id);
6721 		return (0);
6722 	}
6723 
6724 	/* NOTE: s if freed on mod_hash_destroy by mod_hash_strval_dtor */
6725 	return (id);
6726 }
6727 
6728 /* allocate an id mapping within the specified set for str, return id */
6729 id_t
6730 ddi_strid_alloc(ddi_strid *strid, char *str)
6731 {
6732 	return (i_ddi_strid_alloc(strid, str, 0));
6733 }
6734 
6735 /* allocate an id mapping within the specified set for str, return id */
6736 id_t
6737 ddi_strid_fixed_alloc(ddi_strid *strid, char *str)
6738 {
6739 	return (i_ddi_strid_alloc(strid, str, ID_FIXED_SIZE));
6740 }
6741 
6742 /* return the id within the specified strid given the str */
6743 id_t
6744 ddi_strid_str2id(ddi_strid *strid, char *str)
6745 {
6746 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6747 	id_t		id = 0;
6748 	mod_hash_val_t	hv;
6749 
6750 	ASSERT(ss && str);
6751 	if (ss && str && (mod_hash_find(ss->strid_bystr,
6752 	    (mod_hash_key_t)str, &hv) == 0))
6753 		id = (int)(intptr_t)hv;
6754 	return (id);
6755 }
6756 
6757 /* return str within the specified strid given the id */
6758 char *
6759 ddi_strid_id2str(ddi_strid *strid, id_t id)
6760 {
6761 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6762 	char		*str = NULL;
6763 	mod_hash_val_t	hv;
6764 
6765 	ASSERT(ss && id > 0);
6766 	if (ss && (id > 0) && (mod_hash_find(ss->strid_byid,
6767 	    (mod_hash_key_t)(uintptr_t)id, &hv) == 0))
6768 		str = (char *)hv;
6769 	return (str);
6770 }
6771 
6772 /* free the id mapping within the specified strid */
6773 void
6774 ddi_strid_free(ddi_strid *strid, id_t id)
6775 {
6776 	i_ddi_strid	*ss = (i_ddi_strid *)strid;
6777 	char		*str;
6778 
6779 	ASSERT(ss && id > 0);
6780 	if ((ss == NULL) || (id <= 0))
6781 		return;
6782 
6783 	/* bystr key is byid value: destroy order must be (byid, bystr) */
6784 	str = ddi_strid_id2str(strid, id);
6785 	(void) mod_hash_destroy(ss->strid_byid, (mod_hash_key_t)(uintptr_t)id);
6786 	id_free(ss->strid_space, id);
6787 
6788 	if (str)
6789 		(void) mod_hash_destroy(ss->strid_bystr, (mod_hash_key_t)str);
6790 }
6791 
6792 /* destroy the strid set */
6793 void
6794 ddi_strid_fini(ddi_strid **strid_p)
6795 {
6796 	i_ddi_strid	*ss;
6797 
6798 	ASSERT(strid_p);
6799 	if (strid_p == NULL)
6800 		return;
6801 
6802 	ss = (i_ddi_strid *)(*strid_p);
6803 	if (ss == NULL)
6804 		return;
6805 
6806 	/* bystr key is byid value: destroy order must be (byid, bystr) */
6807 	if (ss->strid_byid)
6808 		mod_hash_destroy_hash(ss->strid_byid);
6809 	if (ss->strid_byid)
6810 		mod_hash_destroy_hash(ss->strid_bystr);
6811 	if (ss->strid_space)
6812 		id_space_destroy(ss->strid_space);
6813 	kmem_free(ss, sizeof (*ss));
6814 	*strid_p = NULL;
6815 }
6816 
6817 /*
6818  * This sets the devi_addr entry in the dev_info structure 'dip' to 'name'.
6819  * Storage is double buffered to prevent updates during devi_addr use -
6820  * double buffering is adaquate for reliable ddi_deviname() consumption.
6821  * The double buffer is not freed until dev_info structure destruction
6822  * (by i_ddi_free_node).
6823  */
6824 void
6825 ddi_set_name_addr(dev_info_t *dip, char *name)
6826 {
6827 	char	*buf = DEVI(dip)->devi_addr_buf;
6828 	char	*newaddr;
6829 
6830 	if (buf == NULL) {
6831 		buf = kmem_zalloc(2 * MAXNAMELEN, KM_SLEEP);
6832 		DEVI(dip)->devi_addr_buf = buf;
6833 	}
6834 
6835 	if (name) {
6836 		ASSERT(strlen(name) < MAXNAMELEN);
6837 		newaddr = (DEVI(dip)->devi_addr == buf) ?
6838 		    (buf + MAXNAMELEN) : buf;
6839 		(void) strlcpy(newaddr, name, MAXNAMELEN);
6840 	} else
6841 		newaddr = NULL;
6842 
6843 	DEVI(dip)->devi_addr = newaddr;
6844 }
6845 
6846 char *
6847 ddi_get_name_addr(dev_info_t *dip)
6848 {
6849 	return (DEVI(dip)->devi_addr);
6850 }
6851 
6852 void
6853 ddi_set_parent_data(dev_info_t *dip, void *pd)
6854 {
6855 	DEVI(dip)->devi_parent_data = pd;
6856 }
6857 
6858 void *
6859 ddi_get_parent_data(dev_info_t *dip)
6860 {
6861 	return (DEVI(dip)->devi_parent_data);
6862 }
6863 
6864 /*
6865  * ddi_name_to_major: returns the major number of a named module,
6866  * derived from the current driver alias binding.
6867  *
6868  * Caveat: drivers should avoid the use of this function, in particular
6869  * together with ddi_get_name/ddi_binding name, as per
6870  *	major = ddi_name_to_major(ddi_get_name(devi));
6871  * ddi_name_to_major() relies on the state of the device/alias binding,
6872  * which can and does change dynamically as aliases are administered
6873  * over time.  An attached device instance cannot rely on the major
6874  * number returned by ddi_name_to_major() to match its own major number.
6875  *
6876  * For driver use, ddi_driver_major() reliably returns the major number
6877  * for the module to which the device was bound at attach time over
6878  * the life of the instance.
6879  *	major = ddi_driver_major(dev_info_t *)
6880  */
6881 major_t
6882 ddi_name_to_major(char *name)
6883 {
6884 	return (mod_name_to_major(name));
6885 }
6886 
6887 /*
6888  * ddi_major_to_name: Returns the module name bound to a major number.
6889  */
6890 char *
6891 ddi_major_to_name(major_t major)
6892 {
6893 	return (mod_major_to_name(major));
6894 }
6895 
6896 /*
6897  * Return the name of the devinfo node pointed at by 'dip' in the buffer
6898  * pointed at by 'name.'  A devinfo node is named as a result of calling
6899  * ddi_initchild().
6900  *
6901  * Note: the driver must be held before calling this function!
6902  */
6903 char *
6904 ddi_deviname(dev_info_t *dip, char *name)
6905 {
6906 	char *addrname;
6907 	char none = '\0';
6908 
6909 	if (dip == ddi_root_node()) {
6910 		*name = '\0';
6911 		return (name);
6912 	}
6913 
6914 	if (i_ddi_node_state(dip) < DS_BOUND) {
6915 		addrname = &none;
6916 	} else {
6917 		/*
6918 		 * Use ddi_get_name_addr() without checking state so we get
6919 		 * a unit-address if we are called after ddi_set_name_addr()
6920 		 * by nexus DDI_CTL_INITCHILD code, but before completing
6921 		 * node promotion to DS_INITIALIZED.  We currently have
6922 		 * two situations where we are called in this state:
6923 		 *   o  For framework processing of a path-oriented alias.
6924 		 *   o  If a SCSA nexus driver calls ddi_devid_register()
6925 		 *	from it's tran_tgt_init(9E) implementation.
6926 		 */
6927 		addrname = ddi_get_name_addr(dip);
6928 		if (addrname == NULL)
6929 			addrname = &none;
6930 	}
6931 
6932 	if (*addrname == '\0') {
6933 		(void) sprintf(name, "/%s", ddi_node_name(dip));
6934 	} else {
6935 		(void) sprintf(name, "/%s@%s", ddi_node_name(dip), addrname);
6936 	}
6937 
6938 	return (name);
6939 }
6940 
6941 /*
6942  * Spits out the name of device node, typically name@addr, for a given node,
6943  * using the driver name, not the nodename.
6944  *
6945  * Used by match_parent. Not to be used elsewhere.
6946  */
6947 char *
6948 i_ddi_parname(dev_info_t *dip, char *name)
6949 {
6950 	char *addrname;
6951 
6952 	if (dip == ddi_root_node()) {
6953 		*name = '\0';
6954 		return (name);
6955 	}
6956 
6957 	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
6958 
6959 	if (*(addrname = ddi_get_name_addr(dip)) == '\0')
6960 		(void) sprintf(name, "%s", ddi_binding_name(dip));
6961 	else
6962 		(void) sprintf(name, "%s@%s", ddi_binding_name(dip), addrname);
6963 	return (name);
6964 }
6965 
6966 static char *
6967 pathname_work(dev_info_t *dip, char *path)
6968 {
6969 	char *bp;
6970 
6971 	if (dip == ddi_root_node()) {
6972 		*path = '\0';
6973 		return (path);
6974 	}
6975 	(void) pathname_work(ddi_get_parent(dip), path);
6976 	bp = path + strlen(path);
6977 	(void) ddi_deviname(dip, bp);
6978 	return (path);
6979 }
6980 
6981 char *
6982 ddi_pathname(dev_info_t *dip, char *path)
6983 {
6984 	return (pathname_work(dip, path));
6985 }
6986 
6987 char *
6988 ddi_pathname_minor(struct ddi_minor_data *dmdp, char *path)
6989 {
6990 	if (dmdp->dip == NULL)
6991 		*path = '\0';
6992 	else {
6993 		(void) ddi_pathname(dmdp->dip, path);
6994 		if (dmdp->ddm_name) {
6995 			(void) strcat(path, ":");
6996 			(void) strcat(path, dmdp->ddm_name);
6997 		}
6998 	}
6999 	return (path);
7000 }
7001 
7002 static char *
7003 pathname_work_obp(dev_info_t *dip, char *path)
7004 {
7005 	char *bp;
7006 	char *obp_path;
7007 
7008 	/*
7009 	 * look up the "obp-path" property, return the path if it exists
7010 	 */
7011 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
7012 	    "obp-path", &obp_path) == DDI_PROP_SUCCESS) {
7013 		(void) strcpy(path, obp_path);
7014 		ddi_prop_free(obp_path);
7015 		return (path);
7016 	}
7017 
7018 	/*
7019 	 * stop at root, no obp path
7020 	 */
7021 	if (dip == ddi_root_node()) {
7022 		return (NULL);
7023 	}
7024 
7025 	obp_path = pathname_work_obp(ddi_get_parent(dip), path);
7026 	if (obp_path == NULL)
7027 		return (NULL);
7028 
7029 	/*
7030 	 * append our component to parent's obp path
7031 	 */
7032 	bp = path + strlen(path);
7033 	if (*(bp - 1) != '/')
7034 		(void) strcat(bp++, "/");
7035 	(void) ddi_deviname(dip, bp);
7036 	return (path);
7037 }
7038 
7039 /*
7040  * return the 'obp-path' based path for the given node, or NULL if the node
7041  * does not have a different obp path. NOTE: Unlike ddi_pathname, this
7042  * function can't be called from interrupt context (since we need to
7043  * lookup a string property).
7044  */
7045 char *
7046 ddi_pathname_obp(dev_info_t *dip, char *path)
7047 {
7048 	ASSERT(!servicing_interrupt());
7049 	if (dip == NULL || path == NULL)
7050 		return (NULL);
7051 
7052 	/* split work into a separate function to aid debugging */
7053 	return (pathname_work_obp(dip, path));
7054 }
7055 
7056 int
7057 ddi_pathname_obp_set(dev_info_t *dip, char *component)
7058 {
7059 	dev_info_t *pdip;
7060 	char *obp_path = NULL;
7061 	int rc = DDI_FAILURE;
7062 
7063 	if (dip == NULL)
7064 		return (DDI_FAILURE);
7065 
7066 	obp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
7067 
7068 	pdip = ddi_get_parent(dip);
7069 
7070 	if (ddi_pathname_obp(pdip, obp_path) == NULL) {
7071 		(void) ddi_pathname(pdip, obp_path);
7072 	}
7073 
7074 	if (component) {
7075 		(void) strncat(obp_path, "/", MAXPATHLEN);
7076 		(void) strncat(obp_path, component, MAXPATHLEN);
7077 	}
7078 	rc = ndi_prop_update_string(DDI_DEV_T_NONE, dip, "obp-path",
7079 	    obp_path);
7080 
7081 	if (obp_path)
7082 		kmem_free(obp_path, MAXPATHLEN);
7083 
7084 	return (rc);
7085 }
7086 
7087 /*
7088  * Given a dev_t, return the pathname of the corresponding device in the
7089  * buffer pointed at by "path."  The buffer is assumed to be large enough
7090  * to hold the pathname of the device (MAXPATHLEN).
7091  *
7092  * The pathname of a device is the pathname of the devinfo node to which
7093  * the device "belongs," concatenated with the character ':' and the name
7094  * of the minor node corresponding to the dev_t.  If spec_type is 0 then
7095  * just the pathname of the devinfo node is returned without driving attach
7096  * of that node.  For a non-zero spec_type, an attach is performed and a
7097  * search of the minor list occurs.
7098  *
7099  * It is possible that the path associated with the dev_t is not
7100  * currently available in the devinfo tree.  In order to have a
7101  * dev_t, a device must have been discovered before, which means
7102  * that the path is always in the instance tree.  The one exception
7103  * to this is if the dev_t is associated with a pseudo driver, in
7104  * which case the device must exist on the pseudo branch of the
7105  * devinfo tree as a result of parsing .conf files.
7106  */
7107 int
7108 ddi_dev_pathname(dev_t devt, int spec_type, char *path)
7109 {
7110 	int		circ;
7111 	major_t		major = getmajor(devt);
7112 	int		instance;
7113 	dev_info_t	*dip;
7114 	char		*minorname;
7115 	char		*drvname;
7116 
7117 	if (major >= devcnt)
7118 		goto fail;
7119 	if (major == clone_major) {
7120 		/* clone has no minor nodes, manufacture the path here */
7121 		if ((drvname = ddi_major_to_name(getminor(devt))) == NULL)
7122 			goto fail;
7123 
7124 		(void) snprintf(path, MAXPATHLEN, "%s:%s", CLONE_PATH, drvname);
7125 		return (DDI_SUCCESS);
7126 	}
7127 
7128 	/* extract instance from devt (getinfo(9E) DDI_INFO_DEVT2INSTANCE). */
7129 	if ((instance = dev_to_instance(devt)) == -1)
7130 		goto fail;
7131 
7132 	/* reconstruct the path given the major/instance */
7133 	if (e_ddi_majorinstance_to_path(major, instance, path) != DDI_SUCCESS)
7134 		goto fail;
7135 
7136 	/* if spec_type given we must drive attach and search minor nodes */
7137 	if ((spec_type == S_IFCHR) || (spec_type == S_IFBLK)) {
7138 		/* attach the path so we can search minors */
7139 		if ((dip = e_ddi_hold_devi_by_path(path, 0)) == NULL)
7140 			goto fail;
7141 
7142 		/* Add minorname to path. */
7143 		ndi_devi_enter(dip, &circ);
7144 		minorname = i_ddi_devtspectype_to_minorname(dip,
7145 		    devt, spec_type);
7146 		if (minorname) {
7147 			(void) strcat(path, ":");
7148 			(void) strcat(path, minorname);
7149 		}
7150 		ndi_devi_exit(dip, circ);
7151 		ddi_release_devi(dip);
7152 		if (minorname == NULL)
7153 			goto fail;
7154 	}
7155 	ASSERT(strlen(path) < MAXPATHLEN);
7156 	return (DDI_SUCCESS);
7157 
7158 fail:	*path = 0;
7159 	return (DDI_FAILURE);
7160 }
7161 
7162 /*
7163  * Given a major number and an instance, return the path.
7164  * This interface does NOT drive attach.
7165  */
7166 int
7167 e_ddi_majorinstance_to_path(major_t major, int instance, char *path)
7168 {
7169 	struct devnames *dnp;
7170 	dev_info_t	*dip;
7171 
7172 	if ((major >= devcnt) || (instance == -1)) {
7173 		*path = 0;
7174 		return (DDI_FAILURE);
7175 	}
7176 
7177 	/* look for the major/instance in the instance tree */
7178 	if (e_ddi_instance_majorinstance_to_path(major, instance,
7179 	    path) == DDI_SUCCESS) {
7180 		ASSERT(strlen(path) < MAXPATHLEN);
7181 		return (DDI_SUCCESS);
7182 	}
7183 
7184 	/*
7185 	 * Not in instance tree, find the instance on the per driver list and
7186 	 * construct path to instance via ddi_pathname(). This is how paths
7187 	 * down the 'pseudo' branch are constructed.
7188 	 */
7189 	dnp = &(devnamesp[major]);
7190 	LOCK_DEV_OPS(&(dnp->dn_lock));
7191 	for (dip = dnp->dn_head; dip;
7192 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
7193 		/* Skip if instance does not match. */
7194 		if (DEVI(dip)->devi_instance != instance)
7195 			continue;
7196 
7197 		/*
7198 		 * An ndi_hold_devi() does not prevent DS_INITIALIZED->DS_BOUND
7199 		 * node demotion, so it is not an effective way of ensuring
7200 		 * that the ddi_pathname result has a unit-address.  Instead,
7201 		 * we reverify the node state after calling ddi_pathname().
7202 		 */
7203 		if (i_ddi_node_state(dip) >= DS_INITIALIZED) {
7204 			(void) ddi_pathname(dip, path);
7205 			if (i_ddi_node_state(dip) < DS_INITIALIZED)
7206 				continue;
7207 			UNLOCK_DEV_OPS(&(dnp->dn_lock));
7208 			ASSERT(strlen(path) < MAXPATHLEN);
7209 			return (DDI_SUCCESS);
7210 		}
7211 	}
7212 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
7213 
7214 	/* can't reconstruct the path */
7215 	*path = 0;
7216 	return (DDI_FAILURE);
7217 }
7218 
7219 #define	GLD_DRIVER_PPA "SUNW,gld_v0_ppa"
7220 
7221 /*
7222  * Given the dip for a network interface return the ppa for that interface.
7223  *
7224  * In all cases except GLD v0 drivers, the ppa == instance.
7225  * In the case of GLD v0 drivers, the ppa is equal to the attach order.
7226  * So for these drivers when the attach routine calls gld_register(),
7227  * the GLD framework creates an integer property called "gld_driver_ppa"
7228  * that can be queried here.
7229  *
7230  * The only time this function is used is when a system is booting over nfs.
7231  * In this case the system has to resolve the pathname of the boot device
7232  * to it's ppa.
7233  */
7234 int
7235 i_ddi_devi_get_ppa(dev_info_t *dip)
7236 {
7237 	return (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
7238 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
7239 	    GLD_DRIVER_PPA, ddi_get_instance(dip)));
7240 }
7241 
7242 /*
7243  * i_ddi_devi_set_ppa() should only be called from gld_register()
7244  * and only for GLD v0 drivers
7245  */
7246 void
7247 i_ddi_devi_set_ppa(dev_info_t *dip, int ppa)
7248 {
7249 	(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip, GLD_DRIVER_PPA, ppa);
7250 }
7251 
7252 
7253 /*
7254  * Private DDI Console bell functions.
7255  */
7256 void
7257 ddi_ring_console_bell(clock_t duration)
7258 {
7259 	if (ddi_console_bell_func != NULL)
7260 		(*ddi_console_bell_func)(duration);
7261 }
7262 
7263 void
7264 ddi_set_console_bell(void (*bellfunc)(clock_t duration))
7265 {
7266 	ddi_console_bell_func = bellfunc;
7267 }
7268 
7269 int
7270 ddi_dma_alloc_handle(dev_info_t *dip, ddi_dma_attr_t *attr,
7271 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
7272 {
7273 	int (*funcp)() = ddi_dma_allochdl;
7274 	ddi_dma_attr_t dma_attr;
7275 	struct bus_ops *bop;
7276 
7277 	if (attr == (ddi_dma_attr_t *)0)
7278 		return (DDI_DMA_BADATTR);
7279 
7280 	dma_attr = *attr;
7281 
7282 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
7283 	if (bop && bop->bus_dma_allochdl)
7284 		funcp = bop->bus_dma_allochdl;
7285 
7286 	return ((*funcp)(dip, dip, &dma_attr, waitfp, arg, handlep));
7287 }
7288 
7289 void
7290 ddi_dma_free_handle(ddi_dma_handle_t *handlep)
7291 {
7292 	ddi_dma_handle_t h = *handlep;
7293 	(void) ddi_dma_freehdl(HD, HD, h);
7294 }
7295 
7296 static uintptr_t dma_mem_list_id = 0;
7297 
7298 
7299 int
7300 ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
7301 	ddi_device_acc_attr_t *accattrp, uint_t flags,
7302 	int (*waitfp)(caddr_t), caddr_t arg, caddr_t *kaddrp,
7303 	size_t *real_length, ddi_acc_handle_t *handlep)
7304 {
7305 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7306 	dev_info_t *dip = hp->dmai_rdip;
7307 	ddi_acc_hdl_t *ap;
7308 	ddi_dma_attr_t *attrp = &hp->dmai_attr;
7309 	uint_t sleepflag, xfermodes;
7310 	int (*fp)(caddr_t);
7311 	int rval;
7312 
7313 	if (waitfp == DDI_DMA_SLEEP)
7314 		fp = (int (*)())KM_SLEEP;
7315 	else if (waitfp == DDI_DMA_DONTWAIT)
7316 		fp = (int (*)())KM_NOSLEEP;
7317 	else
7318 		fp = waitfp;
7319 	*handlep = impl_acc_hdl_alloc(fp, arg);
7320 	if (*handlep == NULL)
7321 		return (DDI_FAILURE);
7322 
7323 	/* check if the cache attributes are supported */
7324 	if (i_ddi_check_cache_attr(flags) == B_FALSE)
7325 		return (DDI_FAILURE);
7326 
7327 	/*
7328 	 * Transfer the meaningful bits to xfermodes.
7329 	 * Double-check if the 3rd party driver correctly sets the bits.
7330 	 * If not, set DDI_DMA_STREAMING to keep compatibility.
7331 	 */
7332 	xfermodes = flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING);
7333 	if (xfermodes == 0) {
7334 		xfermodes = DDI_DMA_STREAMING;
7335 	}
7336 
7337 	/*
7338 	 * initialize the common elements of data access handle
7339 	 */
7340 	ap = impl_acc_hdl_get(*handlep);
7341 	ap->ah_vers = VERS_ACCHDL;
7342 	ap->ah_dip = dip;
7343 	ap->ah_offset = 0;
7344 	ap->ah_len = 0;
7345 	ap->ah_xfermodes = flags;
7346 	ap->ah_acc = *accattrp;
7347 
7348 	sleepflag = ((waitfp == DDI_DMA_SLEEP) ? 1 : 0);
7349 	if (xfermodes == DDI_DMA_CONSISTENT) {
7350 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
7351 		    flags, accattrp, kaddrp, NULL, ap);
7352 		*real_length = length;
7353 	} else {
7354 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
7355 		    flags, accattrp, kaddrp, real_length, ap);
7356 	}
7357 	if (rval == DDI_SUCCESS) {
7358 		ap->ah_len = (off_t)(*real_length);
7359 		ap->ah_addr = *kaddrp;
7360 	} else {
7361 		impl_acc_hdl_free(*handlep);
7362 		*handlep = (ddi_acc_handle_t)NULL;
7363 		if (waitfp != DDI_DMA_SLEEP && waitfp != DDI_DMA_DONTWAIT) {
7364 			ddi_set_callback(waitfp, arg, &dma_mem_list_id);
7365 		}
7366 		rval = DDI_FAILURE;
7367 	}
7368 	return (rval);
7369 }
7370 
7371 void
7372 ddi_dma_mem_free(ddi_acc_handle_t *handlep)
7373 {
7374 	ddi_acc_hdl_t *ap;
7375 
7376 	ap = impl_acc_hdl_get(*handlep);
7377 	ASSERT(ap);
7378 
7379 	i_ddi_mem_free((caddr_t)ap->ah_addr, ap);
7380 
7381 	/*
7382 	 * free the handle
7383 	 */
7384 	impl_acc_hdl_free(*handlep);
7385 	*handlep = (ddi_acc_handle_t)NULL;
7386 
7387 	if (dma_mem_list_id != 0) {
7388 		ddi_run_callback(&dma_mem_list_id);
7389 	}
7390 }
7391 
7392 int
7393 ddi_dma_buf_bind_handle(ddi_dma_handle_t handle, struct buf *bp,
7394 	uint_t flags, int (*waitfp)(caddr_t), caddr_t arg,
7395 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
7396 {
7397 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7398 	dev_info_t *dip, *rdip;
7399 	struct ddi_dma_req dmareq;
7400 	int (*funcp)();
7401 
7402 	dmareq.dmar_flags = flags;
7403 	dmareq.dmar_fp = waitfp;
7404 	dmareq.dmar_arg = arg;
7405 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
7406 
7407 	if (bp->b_flags & B_PAGEIO) {
7408 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
7409 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
7410 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
7411 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
7412 	} else {
7413 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
7414 		if (bp->b_flags & B_SHADOW) {
7415 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
7416 			    bp->b_shadow;
7417 			dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
7418 		} else {
7419 			dmareq.dmar_object.dmao_type =
7420 			    (bp->b_flags & (B_PHYS | B_REMAPPED)) ?
7421 			    DMA_OTYP_BUFVADDR : DMA_OTYP_VADDR;
7422 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
7423 		}
7424 
7425 		/*
7426 		 * If the buffer has no proc pointer, or the proc
7427 		 * struct has the kernel address space, or the buffer has
7428 		 * been marked B_REMAPPED (meaning that it is now
7429 		 * mapped into the kernel's address space), then
7430 		 * the address space is kas (kernel address space).
7431 		 */
7432 		if ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas) ||
7433 		    (bp->b_flags & B_REMAPPED)) {
7434 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
7435 		} else {
7436 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
7437 			    bp->b_proc->p_as;
7438 		}
7439 	}
7440 
7441 	dip = rdip = hp->dmai_rdip;
7442 	if (dip != ddi_root_node())
7443 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
7444 	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
7445 	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
7446 }
7447 
7448 int
7449 ddi_dma_addr_bind_handle(ddi_dma_handle_t handle, struct as *as,
7450 	caddr_t addr, size_t len, uint_t flags, int (*waitfp)(caddr_t),
7451 	caddr_t arg, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
7452 {
7453 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7454 	dev_info_t *dip, *rdip;
7455 	struct ddi_dma_req dmareq;
7456 	int (*funcp)();
7457 
7458 	if (len == (uint_t)0) {
7459 		return (DDI_DMA_NOMAPPING);
7460 	}
7461 	dmareq.dmar_flags = flags;
7462 	dmareq.dmar_fp = waitfp;
7463 	dmareq.dmar_arg = arg;
7464 	dmareq.dmar_object.dmao_size = len;
7465 	dmareq.dmar_object.dmao_type = DMA_OTYP_VADDR;
7466 	dmareq.dmar_object.dmao_obj.virt_obj.v_as = as;
7467 	dmareq.dmar_object.dmao_obj.virt_obj.v_addr = addr;
7468 	dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
7469 
7470 	dip = rdip = hp->dmai_rdip;
7471 	if (dip != ddi_root_node())
7472 		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
7473 	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
7474 	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
7475 }
7476 
7477 void
7478 ddi_dma_nextcookie(ddi_dma_handle_t handle, ddi_dma_cookie_t *cookiep)
7479 {
7480 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7481 	ddi_dma_cookie_t *cp;
7482 
7483 	cp = hp->dmai_cookie;
7484 	ASSERT(cp);
7485 
7486 	cookiep->dmac_notused = cp->dmac_notused;
7487 	cookiep->dmac_type = cp->dmac_type;
7488 	cookiep->dmac_address = cp->dmac_address;
7489 	cookiep->dmac_size = cp->dmac_size;
7490 	hp->dmai_cookie++;
7491 }
7492 
7493 int
7494 ddi_dma_numwin(ddi_dma_handle_t handle, uint_t *nwinp)
7495 {
7496 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7497 	if ((hp->dmai_rflags & DDI_DMA_PARTIAL) == 0) {
7498 		return (DDI_FAILURE);
7499 	} else {
7500 		*nwinp = hp->dmai_nwin;
7501 		return (DDI_SUCCESS);
7502 	}
7503 }
7504 
7505 int
7506 ddi_dma_getwin(ddi_dma_handle_t h, uint_t win, off_t *offp,
7507 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
7508 {
7509 	int (*funcp)() = ddi_dma_win;
7510 	struct bus_ops *bop;
7511 
7512 	bop = DEVI(HD)->devi_ops->devo_bus_ops;
7513 	if (bop && bop->bus_dma_win)
7514 		funcp = bop->bus_dma_win;
7515 
7516 	return ((*funcp)(HD, HD, h, win, offp, lenp, cookiep, ccountp));
7517 }
7518 
7519 int
7520 ddi_dma_set_sbus64(ddi_dma_handle_t h, ulong_t burstsizes)
7521 {
7522 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_SET_SBUS64, 0,
7523 	    &burstsizes, 0, 0));
7524 }
7525 
7526 int
7527 i_ddi_dma_fault_check(ddi_dma_impl_t *hp)
7528 {
7529 	return (hp->dmai_fault);
7530 }
7531 
7532 int
7533 ddi_check_dma_handle(ddi_dma_handle_t handle)
7534 {
7535 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7536 	int (*check)(ddi_dma_impl_t *);
7537 
7538 	if ((check = hp->dmai_fault_check) == NULL)
7539 		check = i_ddi_dma_fault_check;
7540 
7541 	return (((*check)(hp) == DDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
7542 }
7543 
7544 void
7545 i_ddi_dma_set_fault(ddi_dma_handle_t handle)
7546 {
7547 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7548 	void (*notify)(ddi_dma_impl_t *);
7549 
7550 	if (!hp->dmai_fault) {
7551 		hp->dmai_fault = 1;
7552 		if ((notify = hp->dmai_fault_notify) != NULL)
7553 			(*notify)(hp);
7554 	}
7555 }
7556 
7557 void
7558 i_ddi_dma_clr_fault(ddi_dma_handle_t handle)
7559 {
7560 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
7561 	void (*notify)(ddi_dma_impl_t *);
7562 
7563 	if (hp->dmai_fault) {
7564 		hp->dmai_fault = 0;
7565 		if ((notify = hp->dmai_fault_notify) != NULL)
7566 			(*notify)(hp);
7567 	}
7568 }
7569 
7570 /*
7571  * register mapping routines.
7572  */
7573 int
7574 ddi_regs_map_setup(dev_info_t *dip, uint_t rnumber, caddr_t *addrp,
7575 	offset_t offset, offset_t len, ddi_device_acc_attr_t *accattrp,
7576 	ddi_acc_handle_t *handle)
7577 {
7578 	ddi_map_req_t mr;
7579 	ddi_acc_hdl_t *hp;
7580 	int result;
7581 
7582 	/*
7583 	 * Allocate and initialize the common elements of data access handle.
7584 	 */
7585 	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
7586 	hp = impl_acc_hdl_get(*handle);
7587 	hp->ah_vers = VERS_ACCHDL;
7588 	hp->ah_dip = dip;
7589 	hp->ah_rnumber = rnumber;
7590 	hp->ah_offset = offset;
7591 	hp->ah_len = len;
7592 	hp->ah_acc = *accattrp;
7593 
7594 	/*
7595 	 * Set up the mapping request and call to parent.
7596 	 */
7597 	mr.map_op = DDI_MO_MAP_LOCKED;
7598 	mr.map_type = DDI_MT_RNUMBER;
7599 	mr.map_obj.rnumber = rnumber;
7600 	mr.map_prot = PROT_READ | PROT_WRITE;
7601 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
7602 	mr.map_handlep = hp;
7603 	mr.map_vers = DDI_MAP_VERSION;
7604 	result = ddi_map(dip, &mr, offset, len, addrp);
7605 
7606 	/*
7607 	 * check for end result
7608 	 */
7609 	if (result != DDI_SUCCESS) {
7610 		impl_acc_hdl_free(*handle);
7611 		*handle = (ddi_acc_handle_t)NULL;
7612 	} else {
7613 		hp->ah_addr = *addrp;
7614 	}
7615 
7616 	return (result);
7617 }
7618 
7619 void
7620 ddi_regs_map_free(ddi_acc_handle_t *handlep)
7621 {
7622 	ddi_map_req_t mr;
7623 	ddi_acc_hdl_t *hp;
7624 
7625 	hp = impl_acc_hdl_get(*handlep);
7626 	ASSERT(hp);
7627 
7628 	mr.map_op = DDI_MO_UNMAP;
7629 	mr.map_type = DDI_MT_RNUMBER;
7630 	mr.map_obj.rnumber = hp->ah_rnumber;
7631 	mr.map_prot = PROT_READ | PROT_WRITE;
7632 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
7633 	mr.map_handlep = hp;
7634 	mr.map_vers = DDI_MAP_VERSION;
7635 
7636 	/*
7637 	 * Call my parent to unmap my regs.
7638 	 */
7639 	(void) ddi_map(hp->ah_dip, &mr, hp->ah_offset,
7640 	    hp->ah_len, &hp->ah_addr);
7641 	/*
7642 	 * free the handle
7643 	 */
7644 	impl_acc_hdl_free(*handlep);
7645 	*handlep = (ddi_acc_handle_t)NULL;
7646 }
7647 
7648 int
7649 ddi_device_zero(ddi_acc_handle_t handle, caddr_t dev_addr, size_t bytecount,
7650 	ssize_t dev_advcnt, uint_t dev_datasz)
7651 {
7652 	uint8_t *b;
7653 	uint16_t *w;
7654 	uint32_t *l;
7655 	uint64_t *ll;
7656 
7657 	/* check for total byte count is multiple of data transfer size */
7658 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
7659 		return (DDI_FAILURE);
7660 
7661 	switch (dev_datasz) {
7662 	case DDI_DATA_SZ01_ACC:
7663 		for (b = (uint8_t *)dev_addr;
7664 		    bytecount != 0; bytecount -= 1, b += dev_advcnt)
7665 			ddi_put8(handle, b, 0);
7666 		break;
7667 	case DDI_DATA_SZ02_ACC:
7668 		for (w = (uint16_t *)dev_addr;
7669 		    bytecount != 0; bytecount -= 2, w += dev_advcnt)
7670 			ddi_put16(handle, w, 0);
7671 		break;
7672 	case DDI_DATA_SZ04_ACC:
7673 		for (l = (uint32_t *)dev_addr;
7674 		    bytecount != 0; bytecount -= 4, l += dev_advcnt)
7675 			ddi_put32(handle, l, 0);
7676 		break;
7677 	case DDI_DATA_SZ08_ACC:
7678 		for (ll = (uint64_t *)dev_addr;
7679 		    bytecount != 0; bytecount -= 8, ll += dev_advcnt)
7680 			ddi_put64(handle, ll, 0x0ll);
7681 		break;
7682 	default:
7683 		return (DDI_FAILURE);
7684 	}
7685 	return (DDI_SUCCESS);
7686 }
7687 
7688 int
7689 ddi_device_copy(
7690 	ddi_acc_handle_t src_handle, caddr_t src_addr, ssize_t src_advcnt,
7691 	ddi_acc_handle_t dest_handle, caddr_t dest_addr, ssize_t dest_advcnt,
7692 	size_t bytecount, uint_t dev_datasz)
7693 {
7694 	uint8_t *b_src, *b_dst;
7695 	uint16_t *w_src, *w_dst;
7696 	uint32_t *l_src, *l_dst;
7697 	uint64_t *ll_src, *ll_dst;
7698 
7699 	/* check for total byte count is multiple of data transfer size */
7700 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
7701 		return (DDI_FAILURE);
7702 
7703 	switch (dev_datasz) {
7704 	case DDI_DATA_SZ01_ACC:
7705 		b_src = (uint8_t *)src_addr;
7706 		b_dst = (uint8_t *)dest_addr;
7707 
7708 		for (; bytecount != 0; bytecount -= 1) {
7709 			ddi_put8(dest_handle, b_dst,
7710 			    ddi_get8(src_handle, b_src));
7711 			b_dst += dest_advcnt;
7712 			b_src += src_advcnt;
7713 		}
7714 		break;
7715 	case DDI_DATA_SZ02_ACC:
7716 		w_src = (uint16_t *)src_addr;
7717 		w_dst = (uint16_t *)dest_addr;
7718 
7719 		for (; bytecount != 0; bytecount -= 2) {
7720 			ddi_put16(dest_handle, w_dst,
7721 			    ddi_get16(src_handle, w_src));
7722 			w_dst += dest_advcnt;
7723 			w_src += src_advcnt;
7724 		}
7725 		break;
7726 	case DDI_DATA_SZ04_ACC:
7727 		l_src = (uint32_t *)src_addr;
7728 		l_dst = (uint32_t *)dest_addr;
7729 
7730 		for (; bytecount != 0; bytecount -= 4) {
7731 			ddi_put32(dest_handle, l_dst,
7732 			    ddi_get32(src_handle, l_src));
7733 			l_dst += dest_advcnt;
7734 			l_src += src_advcnt;
7735 		}
7736 		break;
7737 	case DDI_DATA_SZ08_ACC:
7738 		ll_src = (uint64_t *)src_addr;
7739 		ll_dst = (uint64_t *)dest_addr;
7740 
7741 		for (; bytecount != 0; bytecount -= 8) {
7742 			ddi_put64(dest_handle, ll_dst,
7743 			    ddi_get64(src_handle, ll_src));
7744 			ll_dst += dest_advcnt;
7745 			ll_src += src_advcnt;
7746 		}
7747 		break;
7748 	default:
7749 		return (DDI_FAILURE);
7750 	}
7751 	return (DDI_SUCCESS);
7752 }
7753 
7754 #define	swap16(value)  \
7755 	((((value) & 0xff) << 8) | ((value) >> 8))
7756 
7757 #define	swap32(value)	\
7758 	(((uint32_t)swap16((uint16_t)((value) & 0xffff)) << 16) | \
7759 	(uint32_t)swap16((uint16_t)((value) >> 16)))
7760 
7761 #define	swap64(value)	\
7762 	(((uint64_t)swap32((uint32_t)((value) & 0xffffffff)) \
7763 	    << 32) | \
7764 	(uint64_t)swap32((uint32_t)((value) >> 32)))
7765 
7766 uint16_t
7767 ddi_swap16(uint16_t value)
7768 {
7769 	return (swap16(value));
7770 }
7771 
7772 uint32_t
7773 ddi_swap32(uint32_t value)
7774 {
7775 	return (swap32(value));
7776 }
7777 
7778 uint64_t
7779 ddi_swap64(uint64_t value)
7780 {
7781 	return (swap64(value));
7782 }
7783 
7784 /*
7785  * Convert a binding name to a driver name.
7786  * A binding name is the name used to determine the driver for a
7787  * device - it may be either an alias for the driver or the name
7788  * of the driver itself.
7789  */
7790 char *
7791 i_binding_to_drv_name(char *bname)
7792 {
7793 	major_t major_no;
7794 
7795 	ASSERT(bname != NULL);
7796 
7797 	if ((major_no = ddi_name_to_major(bname)) == -1)
7798 		return (NULL);
7799 	return (ddi_major_to_name(major_no));
7800 }
7801 
7802 /*
7803  * Search for minor name that has specified dev_t and spec_type.
7804  * If spec_type is zero then any dev_t match works.  Since we
7805  * are returning a pointer to the minor name string, we require the
7806  * caller to do the locking.
7807  */
7808 char *
7809 i_ddi_devtspectype_to_minorname(dev_info_t *dip, dev_t dev, int spec_type)
7810 {
7811 	struct ddi_minor_data	*dmdp;
7812 
7813 	/*
7814 	 * The did layered driver currently intentionally returns a
7815 	 * devinfo ptr for an underlying sd instance based on a did
7816 	 * dev_t. In this case it is not an error.
7817 	 *
7818 	 * The did layered driver is associated with Sun Cluster.
7819 	 */
7820 	ASSERT((ddi_driver_major(dip) == getmajor(dev)) ||
7821 	    (strcmp(ddi_major_to_name(getmajor(dev)), "did") == 0));
7822 
7823 	ASSERT(DEVI_BUSY_OWNED(dip));
7824 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
7825 		if (((dmdp->type == DDM_MINOR) ||
7826 		    (dmdp->type == DDM_INTERNAL_PATH) ||
7827 		    (dmdp->type == DDM_DEFAULT)) &&
7828 		    (dmdp->ddm_dev == dev) &&
7829 		    ((((spec_type & (S_IFCHR|S_IFBLK))) == 0) ||
7830 		    (dmdp->ddm_spec_type == spec_type)))
7831 			return (dmdp->ddm_name);
7832 	}
7833 
7834 	return (NULL);
7835 }
7836 
7837 /*
7838  * Find the devt and spectype of the specified minor_name.
7839  * Return DDI_FAILURE if minor_name not found. Since we are
7840  * returning everything via arguments we can do the locking.
7841  */
7842 int
7843 i_ddi_minorname_to_devtspectype(dev_info_t *dip, char *minor_name,
7844 	dev_t *devtp, int *spectypep)
7845 {
7846 	int			circ;
7847 	struct ddi_minor_data	*dmdp;
7848 
7849 	/* deal with clone minor nodes */
7850 	if (dip == clone_dip) {
7851 		major_t	major;
7852 		/*
7853 		 * Make sure minor_name is a STREAMS driver.
7854 		 * We load the driver but don't attach to any instances.
7855 		 */
7856 
7857 		major = ddi_name_to_major(minor_name);
7858 		if (major == DDI_MAJOR_T_NONE)
7859 			return (DDI_FAILURE);
7860 
7861 		if (ddi_hold_driver(major) == NULL)
7862 			return (DDI_FAILURE);
7863 
7864 		if (STREAMSTAB(major) == NULL) {
7865 			ddi_rele_driver(major);
7866 			return (DDI_FAILURE);
7867 		}
7868 		ddi_rele_driver(major);
7869 
7870 		if (devtp)
7871 			*devtp = makedevice(clone_major, (minor_t)major);
7872 
7873 		if (spectypep)
7874 			*spectypep = S_IFCHR;
7875 
7876 		return (DDI_SUCCESS);
7877 	}
7878 
7879 	ndi_devi_enter(dip, &circ);
7880 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
7881 		if (((dmdp->type != DDM_MINOR) &&
7882 		    (dmdp->type != DDM_INTERNAL_PATH) &&
7883 		    (dmdp->type != DDM_DEFAULT)) ||
7884 		    strcmp(minor_name, dmdp->ddm_name))
7885 			continue;
7886 
7887 		if (devtp)
7888 			*devtp = dmdp->ddm_dev;
7889 
7890 		if (spectypep)
7891 			*spectypep = dmdp->ddm_spec_type;
7892 
7893 		ndi_devi_exit(dip, circ);
7894 		return (DDI_SUCCESS);
7895 	}
7896 	ndi_devi_exit(dip, circ);
7897 
7898 	return (DDI_FAILURE);
7899 }
7900 
7901 static kmutex_t devid_gen_mutex;
7902 static short	devid_gen_number;
7903 
7904 #ifdef DEBUG
7905 
7906 static int	devid_register_corrupt = 0;
7907 static int	devid_register_corrupt_major = 0;
7908 static int	devid_register_corrupt_hint = 0;
7909 static int	devid_register_corrupt_hint_major = 0;
7910 
7911 static int devid_lyr_debug = 0;
7912 
7913 #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)		\
7914 	if (devid_lyr_debug)					\
7915 		ddi_debug_devid_devts(msg, ndevs, devs)
7916 
7917 #else
7918 
7919 #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)
7920 
7921 #endif /* DEBUG */
7922 
7923 
7924 #ifdef	DEBUG
7925 
7926 static void
7927 ddi_debug_devid_devts(char *msg, int ndevs, dev_t *devs)
7928 {
7929 	int i;
7930 
7931 	cmn_err(CE_CONT, "%s:\n", msg);
7932 	for (i = 0; i < ndevs; i++) {
7933 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
7934 	}
7935 }
7936 
7937 static void
7938 ddi_debug_devid_paths(char *msg, int npaths, char **paths)
7939 {
7940 	int i;
7941 
7942 	cmn_err(CE_CONT, "%s:\n", msg);
7943 	for (i = 0; i < npaths; i++) {
7944 		cmn_err(CE_CONT, "    %s\n", paths[i]);
7945 	}
7946 }
7947 
7948 static void
7949 ddi_debug_devid_devts_per_path(char *path, int ndevs, dev_t *devs)
7950 {
7951 	int i;
7952 
7953 	cmn_err(CE_CONT, "dev_ts per path %s\n", path);
7954 	for (i = 0; i < ndevs; i++) {
7955 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
7956 	}
7957 }
7958 
7959 #endif	/* DEBUG */
7960 
7961 /*
7962  * Register device id into DDI framework.
7963  * Must be called when device is attached.
7964  */
7965 static int
7966 i_ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
7967 {
7968 	impl_devid_t	*i_devid = (impl_devid_t *)devid;
7969 	size_t		driver_len;
7970 	const char	*driver_name;
7971 	char		*devid_str;
7972 	major_t		major;
7973 
7974 	if ((dip == NULL) ||
7975 	    ((major = ddi_driver_major(dip)) == DDI_MAJOR_T_NONE))
7976 		return (DDI_FAILURE);
7977 
7978 	/* verify that the devid is valid */
7979 	if (ddi_devid_valid(devid) != DDI_SUCCESS)
7980 		return (DDI_FAILURE);
7981 
7982 	/* Updating driver name hint in devid */
7983 	driver_name = ddi_driver_name(dip);
7984 	driver_len = strlen(driver_name);
7985 	if (driver_len > DEVID_HINT_SIZE) {
7986 		/* Pick up last four characters of driver name */
7987 		driver_name += driver_len - DEVID_HINT_SIZE;
7988 		driver_len = DEVID_HINT_SIZE;
7989 	}
7990 	bzero(i_devid->did_driver, DEVID_HINT_SIZE);
7991 	bcopy(driver_name, i_devid->did_driver, driver_len);
7992 
7993 #ifdef DEBUG
7994 	/* Corrupt the devid for testing. */
7995 	if (devid_register_corrupt)
7996 		i_devid->did_id[0] += devid_register_corrupt;
7997 	if (devid_register_corrupt_major &&
7998 	    (major == devid_register_corrupt_major))
7999 		i_devid->did_id[0] += 1;
8000 	if (devid_register_corrupt_hint)
8001 		i_devid->did_driver[0] += devid_register_corrupt_hint;
8002 	if (devid_register_corrupt_hint_major &&
8003 	    (major == devid_register_corrupt_hint_major))
8004 		i_devid->did_driver[0] += 1;
8005 #endif /* DEBUG */
8006 
8007 	/* encode the devid as a string */
8008 	if ((devid_str = ddi_devid_str_encode(devid, NULL)) == NULL)
8009 		return (DDI_FAILURE);
8010 
8011 	/* add string as a string property */
8012 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
8013 	    DEVID_PROP_NAME, devid_str) != DDI_SUCCESS) {
8014 		cmn_err(CE_WARN, "%s%d: devid property update failed",
8015 		    ddi_driver_name(dip), ddi_get_instance(dip));
8016 		ddi_devid_str_free(devid_str);
8017 		return (DDI_FAILURE);
8018 	}
8019 
8020 	/* keep pointer to devid string for interrupt context fma code */
8021 	if (DEVI(dip)->devi_devid_str)
8022 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
8023 	DEVI(dip)->devi_devid_str = devid_str;
8024 	return (DDI_SUCCESS);
8025 }
8026 
8027 int
8028 ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
8029 {
8030 	int rval;
8031 
8032 	rval = i_ddi_devid_register(dip, devid);
8033 	if (rval == DDI_SUCCESS) {
8034 		/*
8035 		 * Register devid in devid-to-path cache
8036 		 */
8037 		if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
8038 			mutex_enter(&DEVI(dip)->devi_lock);
8039 			DEVI(dip)->devi_flags |= DEVI_REGISTERED_DEVID;
8040 			mutex_exit(&DEVI(dip)->devi_lock);
8041 		} else {
8042 			cmn_err(CE_WARN, "%s%d: failed to cache devid",
8043 			    ddi_driver_name(dip), ddi_get_instance(dip));
8044 		}
8045 	} else {
8046 		cmn_err(CE_WARN, "%s%d: failed to register devid",
8047 		    ddi_driver_name(dip), ddi_get_instance(dip));
8048 	}
8049 	return (rval);
8050 }
8051 
8052 /*
8053  * Remove (unregister) device id from DDI framework.
8054  * Must be called when device is detached.
8055  */
8056 static void
8057 i_ddi_devid_unregister(dev_info_t *dip)
8058 {
8059 	if (DEVI(dip)->devi_devid_str) {
8060 		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
8061 		DEVI(dip)->devi_devid_str = NULL;
8062 	}
8063 
8064 	/* remove the devid property */
8065 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, DEVID_PROP_NAME);
8066 }
8067 
8068 void
8069 ddi_devid_unregister(dev_info_t *dip)
8070 {
8071 	mutex_enter(&DEVI(dip)->devi_lock);
8072 	DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
8073 	mutex_exit(&DEVI(dip)->devi_lock);
8074 	e_devid_cache_unregister(dip);
8075 	i_ddi_devid_unregister(dip);
8076 }
8077 
8078 /*
8079  * Allocate and initialize a device id.
8080  */
8081 int
8082 ddi_devid_init(
8083 	dev_info_t	*dip,
8084 	ushort_t	devid_type,
8085 	ushort_t	nbytes,
8086 	void		*id,
8087 	ddi_devid_t	*ret_devid)
8088 {
8089 	impl_devid_t	*i_devid;
8090 	int		sz = sizeof (*i_devid) + nbytes - sizeof (char);
8091 	int		driver_len;
8092 	const char	*driver_name;
8093 
8094 	switch (devid_type) {
8095 	case DEVID_SCSI3_WWN:
8096 		/*FALLTHRU*/
8097 	case DEVID_SCSI_SERIAL:
8098 		/*FALLTHRU*/
8099 	case DEVID_ATA_SERIAL:
8100 		/*FALLTHRU*/
8101 	case DEVID_ENCAP:
8102 		if (nbytes == 0)
8103 			return (DDI_FAILURE);
8104 		if (id == NULL)
8105 			return (DDI_FAILURE);
8106 		break;
8107 	case DEVID_FAB:
8108 		if (nbytes != 0)
8109 			return (DDI_FAILURE);
8110 		if (id != NULL)
8111 			return (DDI_FAILURE);
8112 		nbytes = sizeof (int) +
8113 		    sizeof (struct timeval32) + sizeof (short);
8114 		sz += nbytes;
8115 		break;
8116 	default:
8117 		return (DDI_FAILURE);
8118 	}
8119 
8120 	if ((i_devid = kmem_zalloc(sz, KM_SLEEP)) == NULL)
8121 		return (DDI_FAILURE);
8122 
8123 	i_devid->did_magic_hi = DEVID_MAGIC_MSB;
8124 	i_devid->did_magic_lo = DEVID_MAGIC_LSB;
8125 	i_devid->did_rev_hi = DEVID_REV_MSB;
8126 	i_devid->did_rev_lo = DEVID_REV_LSB;
8127 	DEVID_FORMTYPE(i_devid, devid_type);
8128 	DEVID_FORMLEN(i_devid, nbytes);
8129 
8130 	/* Fill in driver name hint */
8131 	driver_name = ddi_driver_name(dip);
8132 	driver_len = strlen(driver_name);
8133 	if (driver_len > DEVID_HINT_SIZE) {
8134 		/* Pick up last four characters of driver name */
8135 		driver_name += driver_len - DEVID_HINT_SIZE;
8136 		driver_len = DEVID_HINT_SIZE;
8137 	}
8138 
8139 	bcopy(driver_name, i_devid->did_driver, driver_len);
8140 
8141 	/* Fill in id field */
8142 	if (devid_type == DEVID_FAB) {
8143 		char		*cp;
8144 		uint32_t	hostid;
8145 		struct timeval32 timestamp32;
8146 		int		i;
8147 		int		*ip;
8148 		short		gen;
8149 
8150 		/* increase the generation number */
8151 		mutex_enter(&devid_gen_mutex);
8152 		gen = devid_gen_number++;
8153 		mutex_exit(&devid_gen_mutex);
8154 
8155 		cp = i_devid->did_id;
8156 
8157 		/* Fill in host id (big-endian byte ordering) */
8158 		hostid = zone_get_hostid(NULL);
8159 		*cp++ = hibyte(hiword(hostid));
8160 		*cp++ = lobyte(hiword(hostid));
8161 		*cp++ = hibyte(loword(hostid));
8162 		*cp++ = lobyte(loword(hostid));
8163 
8164 		/*
8165 		 * Fill in timestamp (big-endian byte ordering)
8166 		 *
8167 		 * (Note that the format may have to be changed
8168 		 * before 2038 comes around, though it's arguably
8169 		 * unique enough as it is..)
8170 		 */
8171 		uniqtime32(&timestamp32);
8172 		ip = (int *)&timestamp32;
8173 		for (i = 0;
8174 		    i < sizeof (timestamp32) / sizeof (int); i++, ip++) {
8175 			int	val;
8176 			val = *ip;
8177 			*cp++ = hibyte(hiword(val));
8178 			*cp++ = lobyte(hiword(val));
8179 			*cp++ = hibyte(loword(val));
8180 			*cp++ = lobyte(loword(val));
8181 		}
8182 
8183 		/* fill in the generation number */
8184 		*cp++ = hibyte(gen);
8185 		*cp++ = lobyte(gen);
8186 	} else
8187 		bcopy(id, i_devid->did_id, nbytes);
8188 
8189 	/* return device id */
8190 	*ret_devid = (ddi_devid_t)i_devid;
8191 	return (DDI_SUCCESS);
8192 }
8193 
8194 int
8195 ddi_devid_get(dev_info_t *dip, ddi_devid_t *ret_devid)
8196 {
8197 	return (i_ddi_devi_get_devid(DDI_DEV_T_ANY, dip, ret_devid));
8198 }
8199 
8200 int
8201 i_ddi_devi_get_devid(dev_t dev, dev_info_t *dip, ddi_devid_t *ret_devid)
8202 {
8203 	char		*devidstr;
8204 
8205 	ASSERT(dev != DDI_DEV_T_NONE);
8206 
8207 	/* look up the property, devt specific first */
8208 	if (ddi_prop_lookup_string(dev, dip, DDI_PROP_DONTPASS,
8209 	    DEVID_PROP_NAME, &devidstr) != DDI_PROP_SUCCESS) {
8210 		if ((dev == DDI_DEV_T_ANY) ||
8211 		    (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
8212 		    DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) !=
8213 		    DDI_PROP_SUCCESS)) {
8214 			return (DDI_FAILURE);
8215 		}
8216 	}
8217 
8218 	/* convert to binary form */
8219 	if (ddi_devid_str_decode(devidstr, ret_devid, NULL) == -1) {
8220 		ddi_prop_free(devidstr);
8221 		return (DDI_FAILURE);
8222 	}
8223 	ddi_prop_free(devidstr);
8224 	return (DDI_SUCCESS);
8225 }
8226 
8227 /*
8228  * Return a copy of the device id for dev_t
8229  */
8230 int
8231 ddi_lyr_get_devid(dev_t dev, ddi_devid_t *ret_devid)
8232 {
8233 	dev_info_t	*dip;
8234 	int		rval;
8235 
8236 	/* get the dip */
8237 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
8238 		return (DDI_FAILURE);
8239 
8240 	rval = i_ddi_devi_get_devid(dev, dip, ret_devid);
8241 
8242 	ddi_release_devi(dip);		/* e_ddi_hold_devi_by_dev() */
8243 	return (rval);
8244 }
8245 
8246 /*
8247  * Return a copy of the minor name for dev_t and spec_type
8248  */
8249 int
8250 ddi_lyr_get_minor_name(dev_t dev, int spec_type, char **minor_name)
8251 {
8252 	char		*buf;
8253 	int		circ;
8254 	dev_info_t	*dip;
8255 	char		*nm;
8256 	int		rval;
8257 
8258 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
8259 		*minor_name = NULL;
8260 		return (DDI_FAILURE);
8261 	}
8262 
8263 	/* Find the minor name and copy into max size buf */
8264 	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
8265 	ndi_devi_enter(dip, &circ);
8266 	nm = i_ddi_devtspectype_to_minorname(dip, dev, spec_type);
8267 	if (nm)
8268 		(void) strcpy(buf, nm);
8269 	ndi_devi_exit(dip, circ);
8270 	ddi_release_devi(dip);	/* e_ddi_hold_devi_by_dev() */
8271 
8272 	if (nm) {
8273 		/* duplicate into min size buf for return result */
8274 		*minor_name = i_ddi_strdup(buf, KM_SLEEP);
8275 		rval = DDI_SUCCESS;
8276 	} else {
8277 		*minor_name = NULL;
8278 		rval = DDI_FAILURE;
8279 	}
8280 
8281 	/* free max size buf and return */
8282 	kmem_free(buf, MAXNAMELEN);
8283 	return (rval);
8284 }
8285 
8286 int
8287 ddi_lyr_devid_to_devlist(
8288 	ddi_devid_t	devid,
8289 	char		*minor_name,
8290 	int		*retndevs,
8291 	dev_t		**retdevs)
8292 {
8293 	ASSERT(ddi_devid_valid(devid) == DDI_SUCCESS);
8294 
8295 	if (e_devid_cache_to_devt_list(devid, minor_name,
8296 	    retndevs, retdevs) == DDI_SUCCESS) {
8297 		ASSERT(*retndevs > 0);
8298 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
8299 		    *retndevs, *retdevs);
8300 		return (DDI_SUCCESS);
8301 	}
8302 
8303 	if (e_ddi_devid_discovery(devid) == DDI_FAILURE) {
8304 		return (DDI_FAILURE);
8305 	}
8306 
8307 	if (e_devid_cache_to_devt_list(devid, minor_name,
8308 	    retndevs, retdevs) == DDI_SUCCESS) {
8309 		ASSERT(*retndevs > 0);
8310 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
8311 		    *retndevs, *retdevs);
8312 		return (DDI_SUCCESS);
8313 	}
8314 
8315 	return (DDI_FAILURE);
8316 }
8317 
8318 void
8319 ddi_lyr_free_devlist(dev_t *devlist, int ndevs)
8320 {
8321 	kmem_free(devlist, sizeof (dev_t) * ndevs);
8322 }
8323 
8324 /*
8325  * Note: This will need to be fixed if we ever allow processes to
8326  * have more than one data model per exec.
8327  */
8328 model_t
8329 ddi_mmap_get_model(void)
8330 {
8331 	return (get_udatamodel());
8332 }
8333 
8334 model_t
8335 ddi_model_convert_from(model_t model)
8336 {
8337 	return ((model & DDI_MODEL_MASK) & ~DDI_MODEL_NATIVE);
8338 }
8339 
8340 /*
8341  * ddi interfaces managing storage and retrieval of eventcookies.
8342  */
8343 
8344 /*
8345  * Invoke bus nexus driver's implementation of the
8346  * (*bus_remove_eventcall)() interface to remove a registered
8347  * callback handler for "event".
8348  */
8349 int
8350 ddi_remove_event_handler(ddi_callback_id_t id)
8351 {
8352 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)id;
8353 	dev_info_t *ddip;
8354 
8355 	ASSERT(cb);
8356 	if (!cb) {
8357 		return (DDI_FAILURE);
8358 	}
8359 
8360 	ddip = NDI_EVENT_DDIP(cb->ndi_evtcb_cookie);
8361 	return (ndi_busop_remove_eventcall(ddip, id));
8362 }
8363 
8364 /*
8365  * Invoke bus nexus driver's implementation of the
8366  * (*bus_add_eventcall)() interface to register a callback handler
8367  * for "event".
8368  */
8369 int
8370 ddi_add_event_handler(dev_info_t *dip, ddi_eventcookie_t event,
8371     void (*handler)(dev_info_t *, ddi_eventcookie_t, void *, void *),
8372     void *arg, ddi_callback_id_t *id)
8373 {
8374 	return (ndi_busop_add_eventcall(dip, dip, event, handler, arg, id));
8375 }
8376 
8377 
8378 /*
8379  * Return a handle for event "name" by calling up the device tree
8380  * hierarchy via  (*bus_get_eventcookie)() interface until claimed
8381  * by a bus nexus or top of dev_info tree is reached.
8382  */
8383 int
8384 ddi_get_eventcookie(dev_info_t *dip, char *name,
8385     ddi_eventcookie_t *event_cookiep)
8386 {
8387 	return (ndi_busop_get_eventcookie(dip, dip,
8388 	    name, event_cookiep));
8389 }
8390 
8391 /*
8392  * This procedure is provided as the general callback function when
8393  * umem_lockmemory calls as_add_callback for long term memory locking.
8394  * When as_unmap, as_setprot, or as_free encounter segments which have
8395  * locked memory, this callback will be invoked.
8396  */
8397 void
8398 umem_lock_undo(struct as *as, void *arg, uint_t event)
8399 {
8400 	_NOTE(ARGUNUSED(as, event))
8401 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)arg;
8402 
8403 	/*
8404 	 * Call the cleanup function.  Decrement the cookie reference
8405 	 * count, if it goes to zero, return the memory for the cookie.
8406 	 * The i_ddi_umem_unlock for this cookie may or may not have been
8407 	 * called already.  It is the responsibility of the caller of
8408 	 * umem_lockmemory to handle the case of the cleanup routine
8409 	 * being called after a ddi_umem_unlock for the cookie
8410 	 * was called.
8411 	 */
8412 
8413 	(*cp->callbacks.cbo_umem_lock_cleanup)((ddi_umem_cookie_t)cp);
8414 
8415 	/* remove the cookie if reference goes to zero */
8416 	if (atomic_add_long_nv((ulong_t *)(&(cp->cook_refcnt)), -1) == 0) {
8417 		kmem_free(cp, sizeof (struct ddi_umem_cookie));
8418 	}
8419 }
8420 
8421 /*
8422  * The following two Consolidation Private routines provide generic
8423  * interfaces to increase/decrease the amount of device-locked memory.
8424  *
8425  * To keep project_rele and project_hold consistent, i_ddi_decr_locked_memory()
8426  * must be called every time i_ddi_incr_locked_memory() is called.
8427  */
8428 int
8429 /* ARGSUSED */
8430 i_ddi_incr_locked_memory(proc_t *procp, rctl_qty_t inc)
8431 {
8432 	ASSERT(procp != NULL);
8433 	mutex_enter(&procp->p_lock);
8434 	if (rctl_incr_locked_mem(procp, NULL, inc, 1)) {
8435 		mutex_exit(&procp->p_lock);
8436 		return (ENOMEM);
8437 	}
8438 	mutex_exit(&procp->p_lock);
8439 	return (0);
8440 }
8441 
8442 /*
8443  * To keep project_rele and project_hold consistent, i_ddi_incr_locked_memory()
8444  * must be called every time i_ddi_decr_locked_memory() is called.
8445  */
8446 /* ARGSUSED */
8447 void
8448 i_ddi_decr_locked_memory(proc_t *procp, rctl_qty_t dec)
8449 {
8450 	ASSERT(procp != NULL);
8451 	mutex_enter(&procp->p_lock);
8452 	rctl_decr_locked_mem(procp, NULL, dec, 1);
8453 	mutex_exit(&procp->p_lock);
8454 }
8455 
8456 /*
8457  * This routine checks if the max-locked-memory resource ctl is
8458  * exceeded, if not increments it, grabs a hold on the project.
8459  * Returns 0 if successful otherwise returns error code
8460  */
8461 static int
8462 umem_incr_devlockmem(struct ddi_umem_cookie *cookie)
8463 {
8464 	proc_t		*procp;
8465 	int		ret;
8466 
8467 	ASSERT(cookie);
8468 	procp = cookie->procp;
8469 	ASSERT(procp);
8470 
8471 	if ((ret = i_ddi_incr_locked_memory(procp,
8472 	    cookie->size)) != 0) {
8473 		return (ret);
8474 	}
8475 	return (0);
8476 }
8477 
8478 /*
8479  * Decrements the max-locked-memory resource ctl and releases
8480  * the hold on the project that was acquired during umem_incr_devlockmem
8481  */
8482 static void
8483 umem_decr_devlockmem(struct ddi_umem_cookie *cookie)
8484 {
8485 	proc_t		*proc;
8486 
8487 	proc = (proc_t *)cookie->procp;
8488 	if (!proc)
8489 		return;
8490 
8491 	i_ddi_decr_locked_memory(proc, cookie->size);
8492 }
8493 
8494 /*
8495  * A consolidation private function which is essentially equivalent to
8496  * ddi_umem_lock but with the addition of arguments ops_vector and procp.
8497  * A call to as_add_callback is done if DDI_UMEMLOCK_LONGTERM is set, and
8498  * the ops_vector is valid.
8499  *
8500  * Lock the virtual address range in the current process and create a
8501  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
8502  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
8503  * to user space.
8504  *
8505  * Note: The resource control accounting currently uses a full charge model
8506  * in other words attempts to lock the same/overlapping areas of memory
8507  * will deduct the full size of the buffer from the projects running
8508  * counter for the device locked memory.
8509  *
8510  * addr, size should be PAGESIZE aligned
8511  *
8512  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
8513  *	identifies whether the locked memory will be read or written or both
8514  *      DDI_UMEMLOCK_LONGTERM  must be set when the locking will
8515  * be maintained for an indefinitely long period (essentially permanent),
8516  * rather than for what would be required for a typical I/O completion.
8517  * When DDI_UMEMLOCK_LONGTERM is set, umem_lockmemory will return EFAULT
8518  * if the memory pertains to a regular file which is mapped MAP_SHARED.
8519  * This is to prevent a deadlock if a file truncation is attempted after
8520  * after the locking is done.
8521  *
8522  * Returns 0 on success
8523  *	EINVAL - for invalid parameters
8524  *	EPERM, ENOMEM and other error codes returned by as_pagelock
8525  *	ENOMEM - is returned if the current request to lock memory exceeds
8526  *		*.max-locked-memory resource control value.
8527  *      EFAULT - memory pertains to a regular file mapped shared and
8528  *		and DDI_UMEMLOCK_LONGTERM flag is set
8529  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
8530  */
8531 int
8532 umem_lockmemory(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie,
8533 		struct umem_callback_ops *ops_vector,
8534 		proc_t *procp)
8535 {
8536 	int	error;
8537 	struct ddi_umem_cookie *p;
8538 	void	(*driver_callback)() = NULL;
8539 	struct as *as;
8540 	struct seg		*seg;
8541 	vnode_t			*vp;
8542 
8543 	/* Allow device drivers to not have to reference "curproc" */
8544 	if (procp == NULL)
8545 		procp = curproc;
8546 	as = procp->p_as;
8547 	*cookie = NULL;		/* in case of any error return */
8548 
8549 	/* These are the only three valid flags */
8550 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE |
8551 	    DDI_UMEMLOCK_LONGTERM)) != 0)
8552 		return (EINVAL);
8553 
8554 	/* At least one (can be both) of the two access flags must be set */
8555 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0)
8556 		return (EINVAL);
8557 
8558 	/* addr and len must be page-aligned */
8559 	if (((uintptr_t)addr & PAGEOFFSET) != 0)
8560 		return (EINVAL);
8561 
8562 	if ((len & PAGEOFFSET) != 0)
8563 		return (EINVAL);
8564 
8565 	/*
8566 	 * For longterm locking a driver callback must be specified; if
8567 	 * not longterm then a callback is optional.
8568 	 */
8569 	if (ops_vector != NULL) {
8570 		if (ops_vector->cbo_umem_callback_version !=
8571 		    UMEM_CALLBACK_VERSION)
8572 			return (EINVAL);
8573 		else
8574 			driver_callback = ops_vector->cbo_umem_lock_cleanup;
8575 	}
8576 	if ((driver_callback == NULL) && (flags & DDI_UMEMLOCK_LONGTERM))
8577 		return (EINVAL);
8578 
8579 	/*
8580 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
8581 	 * be called on first ddi_umem_lock or umem_lockmemory call.
8582 	 */
8583 	if (ddi_umem_unlock_thread == NULL)
8584 		i_ddi_umem_unlock_thread_start();
8585 
8586 	/* Allocate memory for the cookie */
8587 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
8588 
8589 	/* Convert the flags to seg_rw type */
8590 	if (flags & DDI_UMEMLOCK_WRITE) {
8591 		p->s_flags = S_WRITE;
8592 	} else {
8593 		p->s_flags = S_READ;
8594 	}
8595 
8596 	/* Store procp in cookie for later iosetup/unlock */
8597 	p->procp = (void *)procp;
8598 
8599 	/*
8600 	 * Store the struct as pointer in cookie for later use by
8601 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
8602 	 * is called after relvm is called.
8603 	 */
8604 	p->asp = as;
8605 
8606 	/*
8607 	 * The size field is needed for lockmem accounting.
8608 	 */
8609 	p->size = len;
8610 
8611 	if (umem_incr_devlockmem(p) != 0) {
8612 		/*
8613 		 * The requested memory cannot be locked
8614 		 */
8615 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8616 		*cookie = (ddi_umem_cookie_t)NULL;
8617 		return (ENOMEM);
8618 	}
8619 
8620 	/* Lock the pages corresponding to addr, len in memory */
8621 	error = as_pagelock(as, &(p->pparray), addr, len, p->s_flags);
8622 	if (error != 0) {
8623 		umem_decr_devlockmem(p);
8624 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8625 		*cookie = (ddi_umem_cookie_t)NULL;
8626 		return (error);
8627 	}
8628 
8629 	/*
8630 	 * For longterm locking the addr must pertain to a seg_vn segment or
8631 	 * or a seg_spt segment.
8632 	 * If the segment pertains to a regular file, it cannot be
8633 	 * mapped MAP_SHARED.
8634 	 * This is to prevent a deadlock if a file truncation is attempted
8635 	 * after the locking is done.
8636 	 * Doing this after as_pagelock guarantees persistence of the as; if
8637 	 * an unacceptable segment is found, the cleanup includes calling
8638 	 * as_pageunlock before returning EFAULT.
8639 	 *
8640 	 * segdev is allowed here as it is already locked.  This allows
8641 	 * for memory exported by drivers through mmap() (which is already
8642 	 * locked) to be allowed for LONGTERM.
8643 	 */
8644 	if (flags & DDI_UMEMLOCK_LONGTERM) {
8645 		extern  struct seg_ops segspt_shmops;
8646 		extern	struct seg_ops segdev_ops;
8647 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
8648 		for (seg = as_segat(as, addr); ; seg = AS_SEGNEXT(as, seg)) {
8649 			if (seg == NULL || seg->s_base > addr + len)
8650 				break;
8651 			if (seg->s_ops == &segdev_ops)
8652 				continue;
8653 			if (((seg->s_ops != &segvn_ops) &&
8654 			    (seg->s_ops != &segspt_shmops)) ||
8655 			    ((SEGOP_GETVP(seg, addr, &vp) == 0 &&
8656 			    vp != NULL && vp->v_type == VREG) &&
8657 			    (SEGOP_GETTYPE(seg, addr) & MAP_SHARED))) {
8658 				as_pageunlock(as, p->pparray,
8659 				    addr, len, p->s_flags);
8660 				AS_LOCK_EXIT(as, &as->a_lock);
8661 				umem_decr_devlockmem(p);
8662 				kmem_free(p, sizeof (struct ddi_umem_cookie));
8663 				*cookie = (ddi_umem_cookie_t)NULL;
8664 				return (EFAULT);
8665 			}
8666 		}
8667 		AS_LOCK_EXIT(as, &as->a_lock);
8668 	}
8669 
8670 
8671 	/* Initialize the fields in the ddi_umem_cookie */
8672 	p->cvaddr = addr;
8673 	p->type = UMEM_LOCKED;
8674 	if (driver_callback != NULL) {
8675 		/* i_ddi_umem_unlock and umem_lock_undo may need the cookie */
8676 		p->cook_refcnt = 2;
8677 		p->callbacks = *ops_vector;
8678 	} else {
8679 		/* only i_ddi_umme_unlock needs the cookie */
8680 		p->cook_refcnt = 1;
8681 	}
8682 
8683 	*cookie = (ddi_umem_cookie_t)p;
8684 
8685 	/*
8686 	 * If a driver callback was specified, add an entry to the
8687 	 * as struct callback list. The as_pagelock above guarantees
8688 	 * the persistence of as.
8689 	 */
8690 	if (driver_callback) {
8691 		error = as_add_callback(as, umem_lock_undo, p, AS_ALL_EVENT,
8692 		    addr, len, KM_SLEEP);
8693 		if (error != 0) {
8694 			as_pageunlock(as, p->pparray,
8695 			    addr, len, p->s_flags);
8696 			umem_decr_devlockmem(p);
8697 			kmem_free(p, sizeof (struct ddi_umem_cookie));
8698 			*cookie = (ddi_umem_cookie_t)NULL;
8699 		}
8700 	}
8701 	return (error);
8702 }
8703 
8704 /*
8705  * Unlock the pages locked by ddi_umem_lock or umem_lockmemory and free
8706  * the cookie.  Called from i_ddi_umem_unlock_thread.
8707  */
8708 
8709 static void
8710 i_ddi_umem_unlock(struct ddi_umem_cookie *p)
8711 {
8712 	uint_t	rc;
8713 
8714 	/*
8715 	 * There is no way to determine whether a callback to
8716 	 * umem_lock_undo was registered via as_add_callback.
8717 	 * (i.e. umem_lockmemory was called with DDI_MEMLOCK_LONGTERM and
8718 	 * a valid callback function structure.)  as_delete_callback
8719 	 * is called to delete a possible registered callback.  If the
8720 	 * return from as_delete_callbacks is AS_CALLBACK_DELETED, it
8721 	 * indicates that there was a callback registered, and that is was
8722 	 * successfully deleted.  Thus, the cookie reference count
8723 	 * will never be decremented by umem_lock_undo.  Just return the
8724 	 * memory for the cookie, since both users of the cookie are done.
8725 	 * A return of AS_CALLBACK_NOTFOUND indicates a callback was
8726 	 * never registered.  A return of AS_CALLBACK_DELETE_DEFERRED
8727 	 * indicates that callback processing is taking place and, and
8728 	 * umem_lock_undo is, or will be, executing, and thus decrementing
8729 	 * the cookie reference count when it is complete.
8730 	 *
8731 	 * This needs to be done before as_pageunlock so that the
8732 	 * persistence of as is guaranteed because of the locked pages.
8733 	 *
8734 	 */
8735 	rc = as_delete_callback(p->asp, p);
8736 
8737 
8738 	/*
8739 	 * The proc->p_as will be stale if i_ddi_umem_unlock is called
8740 	 * after relvm is called so use p->asp.
8741 	 */
8742 	as_pageunlock(p->asp, p->pparray, p->cvaddr, p->size, p->s_flags);
8743 
8744 	/*
8745 	 * Now that we have unlocked the memory decrement the
8746 	 * *.max-locked-memory rctl
8747 	 */
8748 	umem_decr_devlockmem(p);
8749 
8750 	if (rc == AS_CALLBACK_DELETED) {
8751 		/* umem_lock_undo will not happen, return the cookie memory */
8752 		ASSERT(p->cook_refcnt == 2);
8753 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8754 	} else {
8755 		/*
8756 		 * umem_undo_lock may happen if as_delete_callback returned
8757 		 * AS_CALLBACK_DELETE_DEFERRED.  In that case, decrement the
8758 		 * reference count, atomically, and return the cookie
8759 		 * memory if the reference count goes to zero.  The only
8760 		 * other value for rc is AS_CALLBACK_NOTFOUND.  In that
8761 		 * case, just return the cookie memory.
8762 		 */
8763 		if ((rc != AS_CALLBACK_DELETE_DEFERRED) ||
8764 		    (atomic_add_long_nv((ulong_t *)(&(p->cook_refcnt)), -1)
8765 		    == 0)) {
8766 			kmem_free(p, sizeof (struct ddi_umem_cookie));
8767 		}
8768 	}
8769 }
8770 
8771 /*
8772  * i_ddi_umem_unlock_thread - deferred ddi_umem_unlock list handler.
8773  *
8774  * Call i_ddi_umem_unlock for entries in the ddi_umem_unlock list
8775  * until it is empty.  Then, wait for more to be added.  This thread is awoken
8776  * via calls to ddi_umem_unlock.
8777  */
8778 
8779 static void
8780 i_ddi_umem_unlock_thread(void)
8781 {
8782 	struct ddi_umem_cookie	*ret_cookie;
8783 	callb_cpr_t	cprinfo;
8784 
8785 	/* process the ddi_umem_unlock list */
8786 	CALLB_CPR_INIT(&cprinfo, &ddi_umem_unlock_mutex,
8787 	    callb_generic_cpr, "unlock_thread");
8788 	for (;;) {
8789 		mutex_enter(&ddi_umem_unlock_mutex);
8790 		if (ddi_umem_unlock_head != NULL) {	/* list not empty */
8791 			ret_cookie = ddi_umem_unlock_head;
8792 			/* take if off the list */
8793 			if ((ddi_umem_unlock_head =
8794 			    ddi_umem_unlock_head->unl_forw) == NULL) {
8795 				ddi_umem_unlock_tail = NULL;
8796 			}
8797 			mutex_exit(&ddi_umem_unlock_mutex);
8798 			/* unlock the pages in this cookie */
8799 			(void) i_ddi_umem_unlock(ret_cookie);
8800 		} else {   /* list is empty, wait for next ddi_umem_unlock */
8801 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
8802 			cv_wait(&ddi_umem_unlock_cv, &ddi_umem_unlock_mutex);
8803 			CALLB_CPR_SAFE_END(&cprinfo, &ddi_umem_unlock_mutex);
8804 			mutex_exit(&ddi_umem_unlock_mutex);
8805 		}
8806 	}
8807 	/* ddi_umem_unlock_thread does not exit */
8808 	/* NOTREACHED */
8809 }
8810 
8811 /*
8812  * Start the thread that will process the ddi_umem_unlock list if it is
8813  * not already started (i_ddi_umem_unlock_thread).
8814  */
8815 static void
8816 i_ddi_umem_unlock_thread_start(void)
8817 {
8818 	mutex_enter(&ddi_umem_unlock_mutex);
8819 	if (ddi_umem_unlock_thread == NULL) {
8820 		ddi_umem_unlock_thread = thread_create(NULL, 0,
8821 		    i_ddi_umem_unlock_thread, NULL, 0, &p0,
8822 		    TS_RUN, minclsyspri);
8823 	}
8824 	mutex_exit(&ddi_umem_unlock_mutex);
8825 }
8826 
8827 /*
8828  * Lock the virtual address range in the current process and create a
8829  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
8830  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
8831  * to user space.
8832  *
8833  * Note: The resource control accounting currently uses a full charge model
8834  * in other words attempts to lock the same/overlapping areas of memory
8835  * will deduct the full size of the buffer from the projects running
8836  * counter for the device locked memory. This applies to umem_lockmemory too.
8837  *
8838  * addr, size should be PAGESIZE aligned
8839  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
8840  *	identifies whether the locked memory will be read or written or both
8841  *
8842  * Returns 0 on success
8843  *	EINVAL - for invalid parameters
8844  *	EPERM, ENOMEM and other error codes returned by as_pagelock
8845  *	ENOMEM - is returned if the current request to lock memory exceeds
8846  *		*.max-locked-memory resource control value.
8847  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
8848  */
8849 int
8850 ddi_umem_lock(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie)
8851 {
8852 	int	error;
8853 	struct ddi_umem_cookie *p;
8854 
8855 	*cookie = NULL;		/* in case of any error return */
8856 
8857 	/* These are the only two valid flags */
8858 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) != 0) {
8859 		return (EINVAL);
8860 	}
8861 
8862 	/* At least one of the two flags (or both) must be set */
8863 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0) {
8864 		return (EINVAL);
8865 	}
8866 
8867 	/* addr and len must be page-aligned */
8868 	if (((uintptr_t)addr & PAGEOFFSET) != 0) {
8869 		return (EINVAL);
8870 	}
8871 
8872 	if ((len & PAGEOFFSET) != 0) {
8873 		return (EINVAL);
8874 	}
8875 
8876 	/*
8877 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
8878 	 * be called on first ddi_umem_lock or umem_lockmemory call.
8879 	 */
8880 	if (ddi_umem_unlock_thread == NULL)
8881 		i_ddi_umem_unlock_thread_start();
8882 
8883 	/* Allocate memory for the cookie */
8884 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
8885 
8886 	/* Convert the flags to seg_rw type */
8887 	if (flags & DDI_UMEMLOCK_WRITE) {
8888 		p->s_flags = S_WRITE;
8889 	} else {
8890 		p->s_flags = S_READ;
8891 	}
8892 
8893 	/* Store curproc in cookie for later iosetup/unlock */
8894 	p->procp = (void *)curproc;
8895 
8896 	/*
8897 	 * Store the struct as pointer in cookie for later use by
8898 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
8899 	 * is called after relvm is called.
8900 	 */
8901 	p->asp = curproc->p_as;
8902 	/*
8903 	 * The size field is needed for lockmem accounting.
8904 	 */
8905 	p->size = len;
8906 
8907 	if (umem_incr_devlockmem(p) != 0) {
8908 		/*
8909 		 * The requested memory cannot be locked
8910 		 */
8911 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8912 		*cookie = (ddi_umem_cookie_t)NULL;
8913 		return (ENOMEM);
8914 	}
8915 
8916 	/* Lock the pages corresponding to addr, len in memory */
8917 	error = as_pagelock(((proc_t *)p->procp)->p_as, &(p->pparray),
8918 	    addr, len, p->s_flags);
8919 	if (error != 0) {
8920 		umem_decr_devlockmem(p);
8921 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8922 		*cookie = (ddi_umem_cookie_t)NULL;
8923 		return (error);
8924 	}
8925 
8926 	/* Initialize the fields in the ddi_umem_cookie */
8927 	p->cvaddr = addr;
8928 	p->type = UMEM_LOCKED;
8929 	p->cook_refcnt = 1;
8930 
8931 	*cookie = (ddi_umem_cookie_t)p;
8932 	return (error);
8933 }
8934 
8935 /*
8936  * Add the cookie to the ddi_umem_unlock list.  Pages will be
8937  * unlocked by i_ddi_umem_unlock_thread.
8938  */
8939 
8940 void
8941 ddi_umem_unlock(ddi_umem_cookie_t cookie)
8942 {
8943 	struct ddi_umem_cookie	*p = (struct ddi_umem_cookie *)cookie;
8944 
8945 	ASSERT(p->type == UMEM_LOCKED);
8946 	ASSERT(CPU_ON_INTR(CPU) == 0); /* cannot be high level */
8947 	ASSERT(ddi_umem_unlock_thread != NULL);
8948 
8949 	p->unl_forw = (struct ddi_umem_cookie *)NULL;	/* end of list */
8950 	/*
8951 	 * Queue the unlock request and notify i_ddi_umem_unlock thread
8952 	 * if it's called in the interrupt context. Otherwise, unlock pages
8953 	 * immediately.
8954 	 */
8955 	if (servicing_interrupt()) {
8956 		/* queue the unlock request and notify the thread */
8957 		mutex_enter(&ddi_umem_unlock_mutex);
8958 		if (ddi_umem_unlock_head == NULL) {
8959 			ddi_umem_unlock_head = ddi_umem_unlock_tail = p;
8960 			cv_broadcast(&ddi_umem_unlock_cv);
8961 		} else {
8962 			ddi_umem_unlock_tail->unl_forw = p;
8963 			ddi_umem_unlock_tail = p;
8964 		}
8965 		mutex_exit(&ddi_umem_unlock_mutex);
8966 	} else {
8967 		/* unlock the pages right away */
8968 		(void) i_ddi_umem_unlock(p);
8969 	}
8970 }
8971 
8972 /*
8973  * Create a buf structure from a ddi_umem_cookie
8974  * cookie - is a ddi_umem_cookie for from ddi_umem_lock and ddi_umem_alloc
8975  *		(only UMEM_LOCKED & KMEM_NON_PAGEABLE types supported)
8976  * off, len - identifies the portion of the memory represented by the cookie
8977  *		that the buf points to.
8978  *	NOTE: off, len need to follow the alignment/size restrictions of the
8979  *		device (dev) that this buf will be passed to. Some devices
8980  *		will accept unrestricted alignment/size, whereas others (such as
8981  *		st) require some block-size alignment/size. It is the caller's
8982  *		responsibility to ensure that the alignment/size restrictions
8983  *		are met (we cannot assert as we do not know the restrictions)
8984  *
8985  * direction - is one of B_READ or B_WRITE and needs to be compatible with
8986  *		the flags used in ddi_umem_lock
8987  *
8988  * The following three arguments are used to initialize fields in the
8989  * buf structure and are uninterpreted by this routine.
8990  *
8991  * dev
8992  * blkno
8993  * iodone
8994  *
8995  * sleepflag - is one of DDI_UMEM_SLEEP or DDI_UMEM_NOSLEEP
8996  *
8997  * Returns a buf structure pointer on success (to be freed by freerbuf)
8998  *	NULL on any parameter error or memory alloc failure
8999  *
9000  */
9001 struct buf *
9002 ddi_umem_iosetup(ddi_umem_cookie_t cookie, off_t off, size_t len,
9003 	int direction, dev_t dev, daddr_t blkno,
9004 	int (*iodone)(struct buf *), int sleepflag)
9005 {
9006 	struct ddi_umem_cookie *p = (struct ddi_umem_cookie *)cookie;
9007 	struct buf *bp;
9008 
9009 	/*
9010 	 * check for valid cookie offset, len
9011 	 */
9012 	if ((off + len) > p->size) {
9013 		return (NULL);
9014 	}
9015 
9016 	if (len > p->size) {
9017 		return (NULL);
9018 	}
9019 
9020 	/* direction has to be one of B_READ or B_WRITE */
9021 	if ((direction != B_READ) && (direction != B_WRITE)) {
9022 		return (NULL);
9023 	}
9024 
9025 	/* These are the only two valid sleepflags */
9026 	if ((sleepflag != DDI_UMEM_SLEEP) && (sleepflag != DDI_UMEM_NOSLEEP)) {
9027 		return (NULL);
9028 	}
9029 
9030 	/*
9031 	 * Only cookies of type UMEM_LOCKED and KMEM_NON_PAGEABLE are supported
9032 	 */
9033 	if ((p->type != UMEM_LOCKED) && (p->type != KMEM_NON_PAGEABLE)) {
9034 		return (NULL);
9035 	}
9036 
9037 	/* If type is KMEM_NON_PAGEABLE procp is NULL */
9038 	ASSERT((p->type == KMEM_NON_PAGEABLE) ?
9039 	    (p->procp == NULL) : (p->procp != NULL));
9040 
9041 	bp = kmem_alloc(sizeof (struct buf), sleepflag);
9042 	if (bp == NULL) {
9043 		return (NULL);
9044 	}
9045 	bioinit(bp);
9046 
9047 	bp->b_flags = B_BUSY | B_PHYS | direction;
9048 	bp->b_edev = dev;
9049 	bp->b_lblkno = blkno;
9050 	bp->b_iodone = iodone;
9051 	bp->b_bcount = len;
9052 	bp->b_proc = (proc_t *)p->procp;
9053 	ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
9054 	bp->b_un.b_addr = (caddr_t)((uintptr_t)(p->cvaddr) + off);
9055 	if (p->pparray != NULL) {
9056 		bp->b_flags |= B_SHADOW;
9057 		ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
9058 		bp->b_shadow = p->pparray + btop(off);
9059 	}
9060 	return (bp);
9061 }
9062 
9063 /*
9064  * Fault-handling and related routines
9065  */
9066 
9067 ddi_devstate_t
9068 ddi_get_devstate(dev_info_t *dip)
9069 {
9070 	if (DEVI_IS_DEVICE_OFFLINE(dip))
9071 		return (DDI_DEVSTATE_OFFLINE);
9072 	else if (DEVI_IS_DEVICE_DOWN(dip) || DEVI_IS_BUS_DOWN(dip))
9073 		return (DDI_DEVSTATE_DOWN);
9074 	else if (DEVI_IS_BUS_QUIESCED(dip))
9075 		return (DDI_DEVSTATE_QUIESCED);
9076 	else if (DEVI_IS_DEVICE_DEGRADED(dip))
9077 		return (DDI_DEVSTATE_DEGRADED);
9078 	else
9079 		return (DDI_DEVSTATE_UP);
9080 }
9081 
9082 void
9083 ddi_dev_report_fault(dev_info_t *dip, ddi_fault_impact_t impact,
9084 	ddi_fault_location_t location, const char *message)
9085 {
9086 	struct ddi_fault_event_data fd;
9087 	ddi_eventcookie_t ec;
9088 
9089 	/*
9090 	 * Assemble all the information into a fault-event-data structure
9091 	 */
9092 	fd.f_dip = dip;
9093 	fd.f_impact = impact;
9094 	fd.f_location = location;
9095 	fd.f_message = message;
9096 	fd.f_oldstate = ddi_get_devstate(dip);
9097 
9098 	/*
9099 	 * Get eventcookie from defining parent.
9100 	 */
9101 	if (ddi_get_eventcookie(dip, DDI_DEVI_FAULT_EVENT, &ec) !=
9102 	    DDI_SUCCESS)
9103 		return;
9104 
9105 	(void) ndi_post_event(dip, dip, ec, &fd);
9106 }
9107 
9108 char *
9109 i_ddi_devi_class(dev_info_t *dip)
9110 {
9111 	return (DEVI(dip)->devi_device_class);
9112 }
9113 
9114 int
9115 i_ddi_set_devi_class(dev_info_t *dip, char *devi_class, int flag)
9116 {
9117 	struct dev_info *devi = DEVI(dip);
9118 
9119 	mutex_enter(&devi->devi_lock);
9120 
9121 	if (devi->devi_device_class)
9122 		kmem_free(devi->devi_device_class,
9123 		    strlen(devi->devi_device_class) + 1);
9124 
9125 	if ((devi->devi_device_class = i_ddi_strdup(devi_class, flag))
9126 	    != NULL) {
9127 		mutex_exit(&devi->devi_lock);
9128 		return (DDI_SUCCESS);
9129 	}
9130 
9131 	mutex_exit(&devi->devi_lock);
9132 
9133 	return (DDI_FAILURE);
9134 }
9135 
9136 
9137 /*
9138  * Task Queues DDI interfaces.
9139  */
9140 
9141 /* ARGSUSED */
9142 ddi_taskq_t *
9143 ddi_taskq_create(dev_info_t *dip, const char *name, int nthreads,
9144     pri_t pri, uint_t cflags)
9145 {
9146 	char full_name[TASKQ_NAMELEN];
9147 	const char *tq_name;
9148 	int nodeid = 0;
9149 
9150 	if (dip == NULL)
9151 		tq_name = name;
9152 	else {
9153 		nodeid = ddi_get_instance(dip);
9154 
9155 		if (name == NULL)
9156 			name = "tq";
9157 
9158 		(void) snprintf(full_name, sizeof (full_name), "%s_%s",
9159 		    ddi_driver_name(dip), name);
9160 
9161 		tq_name = full_name;
9162 	}
9163 
9164 	return ((ddi_taskq_t *)taskq_create_instance(tq_name, nodeid, nthreads,
9165 	    pri == TASKQ_DEFAULTPRI ? minclsyspri : pri,
9166 	    nthreads, INT_MAX, TASKQ_PREPOPULATE));
9167 }
9168 
9169 void
9170 ddi_taskq_destroy(ddi_taskq_t *tq)
9171 {
9172 	taskq_destroy((taskq_t *)tq);
9173 }
9174 
9175 int
9176 ddi_taskq_dispatch(ddi_taskq_t *tq, void (* func)(void *),
9177     void *arg, uint_t dflags)
9178 {
9179 	taskqid_t id = taskq_dispatch((taskq_t *)tq, func, arg,
9180 	    dflags == DDI_SLEEP ? TQ_SLEEP : TQ_NOSLEEP);
9181 
9182 	return (id != 0 ? DDI_SUCCESS : DDI_FAILURE);
9183 }
9184 
9185 void
9186 ddi_taskq_wait(ddi_taskq_t *tq)
9187 {
9188 	taskq_wait((taskq_t *)tq);
9189 }
9190 
9191 void
9192 ddi_taskq_suspend(ddi_taskq_t *tq)
9193 {
9194 	taskq_suspend((taskq_t *)tq);
9195 }
9196 
9197 boolean_t
9198 ddi_taskq_suspended(ddi_taskq_t *tq)
9199 {
9200 	return (taskq_suspended((taskq_t *)tq));
9201 }
9202 
9203 void
9204 ddi_taskq_resume(ddi_taskq_t *tq)
9205 {
9206 	taskq_resume((taskq_t *)tq);
9207 }
9208 
9209 int
9210 ddi_parse(
9211 	const char	*ifname,
9212 	char		*alnum,
9213 	uint_t		*nump)
9214 {
9215 	const char	*p;
9216 	int		l;
9217 	ulong_t		num;
9218 	boolean_t	nonum = B_TRUE;
9219 	char		c;
9220 
9221 	l = strlen(ifname);
9222 	for (p = ifname + l; p != ifname; l--) {
9223 		c = *--p;
9224 		if (!isdigit(c)) {
9225 			(void) strlcpy(alnum, ifname, l + 1);
9226 			if (ddi_strtoul(p + 1, NULL, 10, &num) != 0)
9227 				return (DDI_FAILURE);
9228 			break;
9229 		}
9230 		nonum = B_FALSE;
9231 	}
9232 	if (l == 0 || nonum)
9233 		return (DDI_FAILURE);
9234 
9235 	*nump = num;
9236 	return (DDI_SUCCESS);
9237 }
9238 
9239 /*
9240  * Default initialization function for drivers that don't need to quiesce.
9241  */
9242 /* ARGSUSED */
9243 int
9244 ddi_quiesce_not_needed(dev_info_t *dip)
9245 {
9246 	return (DDI_SUCCESS);
9247 }
9248 
9249 /*
9250  * Initialization function for drivers that should implement quiesce()
9251  * but haven't yet.
9252  */
9253 /* ARGSUSED */
9254 int
9255 ddi_quiesce_not_supported(dev_info_t *dip)
9256 {
9257 	return (DDI_FAILURE);
9258 }
9259 
9260 char *
9261 ddi_strdup(const char *str, int flag)
9262 {
9263 	int	n;
9264 	char	*ptr;
9265 
9266 	ASSERT(str != NULL);
9267 	ASSERT((flag == KM_SLEEP) || (flag == KM_NOSLEEP));
9268 
9269 	n = strlen(str);
9270 	if ((ptr = kmem_alloc(n + 1, flag)) == NULL)
9271 		return (NULL);
9272 	bcopy(str, ptr, n + 1);
9273 	return (ptr);
9274 }
9275 
9276 char *
9277 strdup(const char *str)
9278 {
9279 	return (ddi_strdup(str, KM_SLEEP));
9280 }
9281 
9282 void
9283 strfree(char *str)
9284 {
9285 	ASSERT(str != NULL);
9286 	kmem_free(str, strlen(str) + 1);
9287 }
9288 
9289 /*
9290  * Generic DDI callback interfaces.
9291  */
9292 
9293 int
9294 ddi_cb_register(dev_info_t *dip, ddi_cb_flags_t flags, ddi_cb_func_t cbfunc,
9295     void *arg1, void *arg2, ddi_cb_handle_t *ret_hdlp)
9296 {
9297 	ddi_cb_t	*cbp;
9298 
9299 	ASSERT(dip != NULL);
9300 	ASSERT(DDI_CB_FLAG_VALID(flags));
9301 	ASSERT(cbfunc != NULL);
9302 	ASSERT(ret_hdlp != NULL);
9303 
9304 	/* Sanity check the context */
9305 	ASSERT(!servicing_interrupt());
9306 	if (servicing_interrupt())
9307 		return (DDI_FAILURE);
9308 
9309 	/* Validate parameters */
9310 	if ((dip == NULL) || !DDI_CB_FLAG_VALID(flags) ||
9311 	    (cbfunc == NULL) || (ret_hdlp == NULL))
9312 		return (DDI_EINVAL);
9313 
9314 	/* Check for previous registration */
9315 	if (DEVI(dip)->devi_cb_p != NULL)
9316 		return (DDI_EALREADY);
9317 
9318 	/* Allocate and initialize callback */
9319 	cbp = kmem_zalloc(sizeof (ddi_cb_t), KM_SLEEP);
9320 	cbp->cb_dip = dip;
9321 	cbp->cb_func = cbfunc;
9322 	cbp->cb_arg1 = arg1;
9323 	cbp->cb_arg2 = arg2;
9324 	cbp->cb_flags = flags;
9325 	DEVI(dip)->devi_cb_p = cbp;
9326 
9327 	/* If adding an IRM callback, notify IRM */
9328 	if (flags & DDI_CB_FLAG_INTR)
9329 		i_ddi_irm_set_cb(dip, B_TRUE);
9330 
9331 	*ret_hdlp = (ddi_cb_handle_t)&(DEVI(dip)->devi_cb_p);
9332 	return (DDI_SUCCESS);
9333 }
9334 
9335 int
9336 ddi_cb_unregister(ddi_cb_handle_t hdl)
9337 {
9338 	ddi_cb_t	*cbp;
9339 	dev_info_t	*dip;
9340 
9341 	ASSERT(hdl != NULL);
9342 
9343 	/* Sanity check the context */
9344 	ASSERT(!servicing_interrupt());
9345 	if (servicing_interrupt())
9346 		return (DDI_FAILURE);
9347 
9348 	/* Validate parameters */
9349 	if ((hdl == NULL) || ((cbp = *(ddi_cb_t **)hdl) == NULL) ||
9350 	    ((dip = cbp->cb_dip) == NULL))
9351 		return (DDI_EINVAL);
9352 
9353 	/* If removing an IRM callback, notify IRM */
9354 	if (cbp->cb_flags & DDI_CB_FLAG_INTR)
9355 		i_ddi_irm_set_cb(dip, B_FALSE);
9356 
9357 	/* Destroy the callback */
9358 	kmem_free(cbp, sizeof (ddi_cb_t));
9359 	DEVI(dip)->devi_cb_p = NULL;
9360 
9361 	return (DDI_SUCCESS);
9362 }
9363