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