xref: /titanic_52/usr/src/uts/common/os/sunddi.c (revision f841f6ad96ea6675d6c6b35c749eaac601799fdf)
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 2006 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) {
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) {
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)) {
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, NULL);
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, NULL);
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_devi_attached(pdip) &&
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 		/*
3263 		 * Match DDI_PROP_CMD_GET_DSIZE logic for when to stop and
3264 		 * how to NULL terminate result.
3265 		 */
3266 		p = (char *)ph->ph_cur_pos;
3267 		end = (char *)ph->ph_data + ph->ph_size;
3268 		if (p >= end)
3269 			return (DDI_PROP_RESULT_EOF);
3270 
3271 		while (p < end) {
3272 			*data++ = *p;
3273 			if (*p++ == 0) {	/* NULL from OBP */
3274 				ph->ph_cur_pos = p;
3275 				return (DDI_PROP_RESULT_OK);
3276 			}
3277 		}
3278 
3279 		/*
3280 		 * If OBP did not NULL terminate string, which happens
3281 		 * (at least) for 'true'/'false' boolean values, account for
3282 		 * the space and store null termination on decode.
3283 		 */
3284 		ph->ph_cur_pos = p;
3285 		*data = 0;
3286 		return (DDI_PROP_RESULT_OK);
3287 
3288 	case DDI_PROP_CMD_ENCODE:
3289 		/*
3290 		 * Check that there is room to encoded the data
3291 		 */
3292 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
3293 			return (DDI_PROP_RESULT_ERROR);
3294 		}
3295 
3296 		n = strlen(data) + 1;
3297 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3298 				ph->ph_size - n)) {
3299 			return (DDI_PROP_RESULT_ERROR);
3300 		}
3301 
3302 		/*
3303 		 * Copy the NULL terminated string
3304 		 */
3305 		bcopy(data, ph->ph_cur_pos, n);
3306 
3307 		/*
3308 		 * Move the current location to the start of the next bit of
3309 		 * space where we can store encoded data.
3310 		 */
3311 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + n;
3312 		return (DDI_PROP_RESULT_OK);
3313 
3314 	case DDI_PROP_CMD_SKIP:
3315 		/*
3316 		 * Check that there is encoded data
3317 		 */
3318 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
3319 			return (DDI_PROP_RESULT_ERROR);
3320 		}
3321 
3322 		/*
3323 		 * Return the string length plus one for the NULL
3324 		 * We know the size of the property, we need to
3325 		 * ensure that the string is properly formatted,
3326 		 * since we may be looking up random OBP data.
3327 		 */
3328 		p = (char *)ph->ph_cur_pos;
3329 		end = (char *)ph->ph_data + ph->ph_size;
3330 		if (p >= end)
3331 			return (DDI_PROP_RESULT_EOF);
3332 
3333 		while (p < end) {
3334 			if (*p++ == 0) {	/* NULL from OBP */
3335 				ph->ph_cur_pos = p;
3336 				return (DDI_PROP_RESULT_OK);
3337 			}
3338 		}
3339 
3340 		/*
3341 		 * Accommodate the fact that OBP does not always NULL
3342 		 * terminate strings.
3343 		 */
3344 		ph->ph_cur_pos = p;
3345 		return (DDI_PROP_RESULT_OK);
3346 
3347 	case DDI_PROP_CMD_GET_ESIZE:
3348 		/*
3349 		 * Return the size of the encoded string on OBP.
3350 		 */
3351 		return (strlen(data) + 1);
3352 
3353 	case DDI_PROP_CMD_GET_DSIZE:
3354 		/*
3355 		 * Return the string length plus one for the NULL.
3356 		 * We know the size of the property, we need to
3357 		 * ensure that the string is properly formatted,
3358 		 * since we may be looking up random OBP data.
3359 		 */
3360 		p = (char *)ph->ph_cur_pos;
3361 		end = (char *)ph->ph_data + ph->ph_size;
3362 		if (p >= end)
3363 			return (DDI_PROP_RESULT_EOF);
3364 
3365 		for (n = 0; p < end; n++) {
3366 			if (*p++ == 0) {	/* NULL from OBP */
3367 				ph->ph_cur_pos = p;
3368 				return (n + 1);
3369 			}
3370 		}
3371 
3372 		/*
3373 		 * If OBP did not NULL terminate string, which happens for
3374 		 * 'true'/'false' boolean values, account for the space
3375 		 * to store null termination here.
3376 		 */
3377 		ph->ph_cur_pos = p;
3378 		return (n + 1);
3379 
3380 	default:
3381 #ifdef DEBUG
3382 		panic("ddi_prop_1275_string: %x impossible", cmd);
3383 		/*NOTREACHED*/
3384 #else
3385 		return (DDI_PROP_RESULT_ERROR);
3386 #endif	/* DEBUG */
3387 	}
3388 }
3389 
3390 /*
3391  * OBP 1275 byte operator
3392  *
3393  * Caller must specify the number of bytes to get.  OBP encodes bytes
3394  * as a byte so there is a 1-to-1 translation.
3395  */
3396 int
3397 ddi_prop_1275_bytes(prop_handle_t *ph, uint_t cmd, uchar_t *data,
3398 	uint_t nelements)
3399 {
3400 	switch (cmd) {
3401 	case DDI_PROP_CMD_DECODE:
3402 		/*
3403 		 * Check that there is encoded 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 out the bytes
3413 		 */
3414 		bcopy(ph->ph_cur_pos, data, nelements);
3415 
3416 		/*
3417 		 * Move the current location
3418 		 */
3419 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
3420 		return (DDI_PROP_RESULT_OK);
3421 
3422 	case DDI_PROP_CMD_ENCODE:
3423 		/*
3424 		 * Check that there is room to encode the data
3425 		 */
3426 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3427 			ph->ph_size < nelements ||
3428 			((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3429 				ph->ph_size - nelements)))
3430 			return (DDI_PROP_RESULT_ERROR);
3431 
3432 		/*
3433 		 * Copy in the bytes
3434 		 */
3435 		bcopy(data, ph->ph_cur_pos, nelements);
3436 
3437 		/*
3438 		 * Move the current location to the start of the next bit of
3439 		 * space where we can store encoded data.
3440 		 */
3441 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
3442 		return (DDI_PROP_RESULT_OK);
3443 
3444 	case DDI_PROP_CMD_SKIP:
3445 		/*
3446 		 * Check that there is encoded data
3447 		 */
3448 		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
3449 				ph->ph_size < nelements)
3450 			return (DDI_PROP_RESULT_ERROR);
3451 
3452 		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
3453 				ph->ph_size - nelements))
3454 			return (DDI_PROP_RESULT_EOF);
3455 
3456 		/*
3457 		 * Move the current location
3458 		 */
3459 		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
3460 		return (DDI_PROP_RESULT_OK);
3461 
3462 	case DDI_PROP_CMD_GET_ESIZE:
3463 		/*
3464 		 * The size in bytes of the encoded size is the
3465 		 * same as the decoded size provided by the caller.
3466 		 */
3467 		return (nelements);
3468 
3469 	case DDI_PROP_CMD_GET_DSIZE:
3470 		/*
3471 		 * Just return the number of bytes specified by the caller.
3472 		 */
3473 		return (nelements);
3474 
3475 	default:
3476 #ifdef DEBUG
3477 		panic("ddi_prop_1275_bytes: %x impossible", cmd);
3478 		/*NOTREACHED*/
3479 #else
3480 		return (DDI_PROP_RESULT_ERROR);
3481 #endif	/* DEBUG */
3482 	}
3483 }
3484 
3485 /*
3486  * Used for properties that come from the OBP, hardware configuration files,
3487  * or that are created by calls to ddi_prop_update(9F).
3488  */
3489 static struct prop_handle_ops prop_1275_ops = {
3490 	ddi_prop_1275_int,
3491 	ddi_prop_1275_string,
3492 	ddi_prop_1275_bytes,
3493 	ddi_prop_int64_op
3494 };
3495 
3496 
3497 /*
3498  * Interface to create/modify a managed property on child's behalf...
3499  * Flags interpreted are:
3500  *	DDI_PROP_CANSLEEP:	Allow memory allocation to sleep.
3501  *	DDI_PROP_SYSTEM_DEF:	Manipulate system list rather than driver list.
3502  *
3503  * Use same dev_t when modifying or undefining a property.
3504  * Search for properties with DDI_DEV_T_ANY to match first named
3505  * property on the list.
3506  *
3507  * Properties are stored LIFO and subsequently will match the first
3508  * `matching' instance.
3509  */
3510 
3511 /*
3512  * ddi_prop_add:	Add a software defined property
3513  */
3514 
3515 /*
3516  * define to get a new ddi_prop_t.
3517  * km_flags are KM_SLEEP or KM_NOSLEEP.
3518  */
3519 
3520 #define	DDI_NEW_PROP_T(km_flags)	\
3521 	(kmem_zalloc(sizeof (ddi_prop_t), km_flags))
3522 
3523 static int
3524 ddi_prop_add(dev_t dev, dev_info_t *dip, int flags,
3525     char *name, caddr_t value, int length)
3526 {
3527 	ddi_prop_t	*new_propp, *propp;
3528 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
3529 	int		km_flags = KM_NOSLEEP;
3530 	int		name_buf_len;
3531 
3532 	/*
3533 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero return error.
3534 	 */
3535 
3536 	if (dev == DDI_DEV_T_ANY || name == (char *)0 || strlen(name) == 0)
3537 		return (DDI_PROP_INVAL_ARG);
3538 
3539 	if (flags & DDI_PROP_CANSLEEP)
3540 		km_flags = KM_SLEEP;
3541 
3542 	if (flags & DDI_PROP_SYSTEM_DEF)
3543 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
3544 	else if (flags & DDI_PROP_HW_DEF)
3545 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
3546 
3547 	if ((new_propp = DDI_NEW_PROP_T(km_flags)) == NULL)  {
3548 		cmn_err(CE_CONT, prop_no_mem_msg, name);
3549 		return (DDI_PROP_NO_MEMORY);
3550 	}
3551 
3552 	/*
3553 	 * If dev is major number 0, then we need to do a ddi_name_to_major
3554 	 * to get the real major number for the device.  This needs to be
3555 	 * done because some drivers need to call ddi_prop_create in their
3556 	 * attach routines but they don't have a dev.  By creating the dev
3557 	 * ourself if the major number is 0, drivers will not have to know what
3558 	 * their major number.	They can just create a dev with major number
3559 	 * 0 and pass it in.  For device 0, we will be doing a little extra
3560 	 * work by recreating the same dev that we already have, but its the
3561 	 * price you pay :-).
3562 	 *
3563 	 * This fixes bug #1098060.
3564 	 */
3565 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN) {
3566 		new_propp->prop_dev =
3567 		    makedevice(ddi_name_to_major(DEVI(dip)->devi_binding_name),
3568 		    getminor(dev));
3569 	} else
3570 		new_propp->prop_dev = dev;
3571 
3572 	/*
3573 	 * Allocate space for property name and copy it in...
3574 	 */
3575 
3576 	name_buf_len = strlen(name) + 1;
3577 	new_propp->prop_name = kmem_alloc(name_buf_len, km_flags);
3578 	if (new_propp->prop_name == 0)	{
3579 		kmem_free(new_propp, sizeof (ddi_prop_t));
3580 		cmn_err(CE_CONT, prop_no_mem_msg, name);
3581 		return (DDI_PROP_NO_MEMORY);
3582 	}
3583 	bcopy(name, new_propp->prop_name, name_buf_len);
3584 
3585 	/*
3586 	 * Set the property type
3587 	 */
3588 	new_propp->prop_flags = flags & DDI_PROP_TYPE_MASK;
3589 
3590 	/*
3591 	 * Set length and value ONLY if not an explicit property undefine:
3592 	 * NOTE: value and length are zero for explicit undefines.
3593 	 */
3594 
3595 	if (flags & DDI_PROP_UNDEF_IT) {
3596 		new_propp->prop_flags |= DDI_PROP_UNDEF_IT;
3597 	} else {
3598 		if ((new_propp->prop_len = length) != 0) {
3599 			new_propp->prop_val = kmem_alloc(length, km_flags);
3600 			if (new_propp->prop_val == 0)  {
3601 				kmem_free(new_propp->prop_name, name_buf_len);
3602 				kmem_free(new_propp, sizeof (ddi_prop_t));
3603 				cmn_err(CE_CONT, prop_no_mem_msg, name);
3604 				return (DDI_PROP_NO_MEMORY);
3605 			}
3606 			bcopy(value, new_propp->prop_val, length);
3607 		}
3608 	}
3609 
3610 	/*
3611 	 * Link property into beginning of list. (Properties are LIFO order.)
3612 	 */
3613 
3614 	mutex_enter(&(DEVI(dip)->devi_lock));
3615 	propp = *list_head;
3616 	new_propp->prop_next = propp;
3617 	*list_head = new_propp;
3618 	mutex_exit(&(DEVI(dip)->devi_lock));
3619 	return (DDI_PROP_SUCCESS);
3620 }
3621 
3622 
3623 /*
3624  * ddi_prop_change:	Modify a software managed property value
3625  *
3626  *			Set new length and value if found.
3627  *			returns DDI_PROP_INVAL_ARG if dev is DDI_DEV_T_ANY or
3628  *			input name is the NULL string.
3629  *			returns DDI_PROP_NO_MEMORY if unable to allocate memory
3630  *
3631  *			Note: an undef can be modified to be a define,
3632  *			(you can't go the other way.)
3633  */
3634 
3635 static int
3636 ddi_prop_change(dev_t dev, dev_info_t *dip, int flags,
3637     char *name, caddr_t value, int length)
3638 {
3639 	ddi_prop_t	*propp;
3640 	ddi_prop_t	**ppropp;
3641 	caddr_t		p = NULL;
3642 
3643 	if ((dev == DDI_DEV_T_ANY) || (name == NULL) || (strlen(name) == 0))
3644 		return (DDI_PROP_INVAL_ARG);
3645 
3646 	/*
3647 	 * Preallocate buffer, even if we don't need it...
3648 	 */
3649 	if (length != 0)  {
3650 		p = kmem_alloc(length, (flags & DDI_PROP_CANSLEEP) ?
3651 		    KM_SLEEP : KM_NOSLEEP);
3652 		if (p == NULL)	{
3653 			cmn_err(CE_CONT, prop_no_mem_msg, name);
3654 			return (DDI_PROP_NO_MEMORY);
3655 		}
3656 	}
3657 
3658 	/*
3659 	 * If the dev_t value contains DDI_MAJOR_T_UNKNOWN for the major
3660 	 * number, a real dev_t value should be created based upon the dip's
3661 	 * binding driver.  See ddi_prop_add...
3662 	 */
3663 	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN)
3664 		dev = makedevice(
3665 		    ddi_name_to_major(DEVI(dip)->devi_binding_name),
3666 		    getminor(dev));
3667 
3668 	/*
3669 	 * Check to see if the property exists.  If so we modify it.
3670 	 * Else we create it by calling ddi_prop_add().
3671 	 */
3672 	mutex_enter(&(DEVI(dip)->devi_lock));
3673 	ppropp = &DEVI(dip)->devi_drv_prop_ptr;
3674 	if (flags & DDI_PROP_SYSTEM_DEF)
3675 		ppropp = &DEVI(dip)->devi_sys_prop_ptr;
3676 	else if (flags & DDI_PROP_HW_DEF)
3677 		ppropp = &DEVI(dip)->devi_hw_prop_ptr;
3678 
3679 	if ((propp = i_ddi_prop_search(dev, name, flags, ppropp)) != NULL) {
3680 		/*
3681 		 * Need to reallocate buffer?  If so, do it
3682 		 * carefully (reuse same space if new prop
3683 		 * is same size and non-NULL sized).
3684 		 */
3685 		if (length != 0)
3686 			bcopy(value, p, length);
3687 
3688 		if (propp->prop_len != 0)
3689 			kmem_free(propp->prop_val, propp->prop_len);
3690 
3691 		propp->prop_len = length;
3692 		propp->prop_val = p;
3693 		propp->prop_flags &= ~DDI_PROP_UNDEF_IT;
3694 		mutex_exit(&(DEVI(dip)->devi_lock));
3695 		return (DDI_PROP_SUCCESS);
3696 	}
3697 
3698 	mutex_exit(&(DEVI(dip)->devi_lock));
3699 	if (length != 0)
3700 		kmem_free(p, length);
3701 
3702 	return (ddi_prop_add(dev, dip, flags, name, value, length));
3703 }
3704 
3705 /*
3706  * Common update routine used to update and encode a property.	Creates
3707  * a property handle, calls the property encode routine, figures out if
3708  * the property already exists and updates if it does.	Otherwise it
3709  * creates if it does not exist.
3710  */
3711 int
3712 ddi_prop_update_common(dev_t match_dev, dev_info_t *dip, int flags,
3713     char *name, void *data, uint_t nelements,
3714     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
3715 {
3716 	prop_handle_t	ph;
3717 	int		rval;
3718 	uint_t		ourflags;
3719 
3720 	/*
3721 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
3722 	 * return error.
3723 	 */
3724 	if (match_dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
3725 		return (DDI_PROP_INVAL_ARG);
3726 
3727 	/*
3728 	 * Create the handle
3729 	 */
3730 	ph.ph_data = NULL;
3731 	ph.ph_cur_pos = NULL;
3732 	ph.ph_save_pos = NULL;
3733 	ph.ph_size = 0;
3734 	ph.ph_ops = &prop_1275_ops;
3735 
3736 	/*
3737 	 * ourflags:
3738 	 * For compatibility with the old interfaces.  The old interfaces
3739 	 * didn't sleep by default and slept when the flag was set.  These
3740 	 * interfaces to the opposite.	So the old interfaces now set the
3741 	 * DDI_PROP_DONTSLEEP flag by default which tells us not to sleep.
3742 	 *
3743 	 * ph.ph_flags:
3744 	 * Blocked data or unblocked data allocation
3745 	 * for ph.ph_data in ddi_prop_encode_alloc()
3746 	 */
3747 	if (flags & DDI_PROP_DONTSLEEP) {
3748 		ourflags = flags;
3749 		ph.ph_flags = DDI_PROP_DONTSLEEP;
3750 	} else {
3751 		ourflags = flags | DDI_PROP_CANSLEEP;
3752 		ph.ph_flags = DDI_PROP_CANSLEEP;
3753 	}
3754 
3755 	/*
3756 	 * Encode the data and store it in the property handle by
3757 	 * calling the prop_encode routine.
3758 	 */
3759 	if ((rval = (*prop_create)(&ph, data, nelements)) !=
3760 	    DDI_PROP_SUCCESS) {
3761 		if (rval == DDI_PROP_NO_MEMORY)
3762 			cmn_err(CE_CONT, prop_no_mem_msg, name);
3763 		if (ph.ph_size != 0)
3764 			kmem_free(ph.ph_data, ph.ph_size);
3765 		return (rval);
3766 	}
3767 
3768 	/*
3769 	 * The old interfaces use a stacking approach to creating
3770 	 * properties.	If we are being called from the old interfaces,
3771 	 * the DDI_PROP_STACK_CREATE flag will be set, so we just do a
3772 	 * create without checking.
3773 	 */
3774 	if (flags & DDI_PROP_STACK_CREATE) {
3775 		rval = ddi_prop_add(match_dev, dip,
3776 		    ourflags, name, ph.ph_data, ph.ph_size);
3777 	} else {
3778 		rval = ddi_prop_change(match_dev, dip,
3779 		    ourflags, name, ph.ph_data, ph.ph_size);
3780 	}
3781 
3782 	/*
3783 	 * Free the encoded data allocated in the prop_encode routine.
3784 	 */
3785 	if (ph.ph_size != 0)
3786 		kmem_free(ph.ph_data, ph.ph_size);
3787 
3788 	return (rval);
3789 }
3790 
3791 
3792 /*
3793  * ddi_prop_create:	Define a managed property:
3794  *			See above for details.
3795  */
3796 
3797 int
3798 ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
3799     char *name, caddr_t value, int length)
3800 {
3801 	if (!(flag & DDI_PROP_CANSLEEP)) {
3802 		flag |= DDI_PROP_DONTSLEEP;
3803 #ifdef DDI_PROP_DEBUG
3804 		if (length != 0)
3805 			cmn_err(CE_NOTE, "!ddi_prop_create: interface obsolete,"
3806 			    "use ddi_prop_update (prop = %s, node = %s%d)",
3807 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
3808 #endif /* DDI_PROP_DEBUG */
3809 	}
3810 	flag &= ~DDI_PROP_SYSTEM_DEF;
3811 	return (ddi_prop_update_common(dev, dip,
3812 	    (flag | DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY), name,
3813 	    value, length, ddi_prop_fm_encode_bytes));
3814 }
3815 
3816 int
3817 e_ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
3818     char *name, caddr_t value, int length)
3819 {
3820 	if (!(flag & DDI_PROP_CANSLEEP))
3821 		flag |= DDI_PROP_DONTSLEEP;
3822 	return (ddi_prop_update_common(dev, dip,
3823 	    (flag | DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE |
3824 	    DDI_PROP_TYPE_ANY),
3825 	    name, value, length, ddi_prop_fm_encode_bytes));
3826 }
3827 
3828 int
3829 ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
3830     char *name, caddr_t value, int length)
3831 {
3832 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
3833 
3834 	/*
3835 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
3836 	 * return error.
3837 	 */
3838 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
3839 		return (DDI_PROP_INVAL_ARG);
3840 
3841 	if (!(flag & DDI_PROP_CANSLEEP))
3842 		flag |= DDI_PROP_DONTSLEEP;
3843 	flag &= ~DDI_PROP_SYSTEM_DEF;
3844 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_NOTPROM), name) == 0)
3845 		return (DDI_PROP_NOT_FOUND);
3846 
3847 	return (ddi_prop_update_common(dev, dip,
3848 	    (flag | DDI_PROP_TYPE_BYTE), name,
3849 	    value, length, ddi_prop_fm_encode_bytes));
3850 }
3851 
3852 int
3853 e_ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
3854     char *name, caddr_t value, int length)
3855 {
3856 	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);
3857 
3858 	/*
3859 	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
3860 	 * return error.
3861 	 */
3862 	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
3863 		return (DDI_PROP_INVAL_ARG);
3864 
3865 	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_SYSTEM_DEF), name) == 0)
3866 		return (DDI_PROP_NOT_FOUND);
3867 
3868 	if (!(flag & DDI_PROP_CANSLEEP))
3869 		flag |= DDI_PROP_DONTSLEEP;
3870 	return (ddi_prop_update_common(dev, dip,
3871 		(flag | DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE),
3872 		name, value, length, ddi_prop_fm_encode_bytes));
3873 }
3874 
3875 
3876 /*
3877  * Common lookup routine used to lookup and decode a property.
3878  * Creates a property handle, searches for the raw encoded data,
3879  * fills in the handle, and calls the property decode functions
3880  * passed in.
3881  *
3882  * This routine is not static because ddi_bus_prop_op() which lives in
3883  * ddi_impl.c calls it.  No driver should be calling this routine.
3884  */
3885 int
3886 ddi_prop_lookup_common(dev_t match_dev, dev_info_t *dip,
3887     uint_t flags, char *name, void *data, uint_t *nelements,
3888     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
3889 {
3890 	int		rval;
3891 	uint_t		ourflags;
3892 	prop_handle_t	ph;
3893 
3894 	if ((match_dev == DDI_DEV_T_NONE) ||
3895 	    (name == NULL) || (strlen(name) == 0))
3896 		return (DDI_PROP_INVAL_ARG);
3897 
3898 	ourflags = (flags & DDI_PROP_DONTSLEEP) ? flags :
3899 		flags | DDI_PROP_CANSLEEP;
3900 
3901 	/*
3902 	 * Get the encoded data
3903 	 */
3904 	bzero(&ph, sizeof (prop_handle_t));
3905 
3906 	if (flags & DDI_UNBND_DLPI2) {
3907 		/*
3908 		 * For unbound dlpi style-2 devices, index into
3909 		 * the devnames' array and search the global
3910 		 * property list.
3911 		 */
3912 		ourflags &= ~DDI_UNBND_DLPI2;
3913 		rval = i_ddi_prop_search_global(match_dev,
3914 		    ourflags, name, &ph.ph_data, &ph.ph_size);
3915 	} else {
3916 		rval = ddi_prop_search_common(match_dev, dip,
3917 		    PROP_LEN_AND_VAL_ALLOC, ourflags, name,
3918 		    &ph.ph_data, &ph.ph_size);
3919 
3920 	}
3921 
3922 	if (rval != DDI_PROP_SUCCESS && rval != DDI_PROP_FOUND_1275) {
3923 		ASSERT(ph.ph_data == NULL);
3924 		ASSERT(ph.ph_size == 0);
3925 		return (rval);
3926 	}
3927 
3928 	/*
3929 	 * If the encoded data came from a OBP or software
3930 	 * use the 1275 OBP decode/encode routines.
3931 	 */
3932 	ph.ph_cur_pos = ph.ph_data;
3933 	ph.ph_save_pos = ph.ph_data;
3934 	ph.ph_ops = &prop_1275_ops;
3935 	ph.ph_flags = (rval == DDI_PROP_FOUND_1275) ? PH_FROM_PROM : 0;
3936 
3937 	rval = (*prop_decoder)(&ph, data, nelements);
3938 
3939 	/*
3940 	 * Free the encoded data
3941 	 */
3942 	if (ph.ph_size != 0)
3943 		kmem_free(ph.ph_data, ph.ph_size);
3944 
3945 	return (rval);
3946 }
3947 
3948 /*
3949  * Lookup and return an array of composite properties.  The driver must
3950  * provide the decode routine.
3951  */
3952 int
3953 ddi_prop_lookup(dev_t match_dev, dev_info_t *dip,
3954     uint_t flags, char *name, void *data, uint_t *nelements,
3955     int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
3956 {
3957 	return (ddi_prop_lookup_common(match_dev, dip,
3958 	    (flags | DDI_PROP_TYPE_COMPOSITE), name,
3959 	    data, nelements, prop_decoder));
3960 }
3961 
3962 /*
3963  * Return 1 if a property exists (no type checking done).
3964  * Return 0 if it does not exist.
3965  */
3966 int
3967 ddi_prop_exists(dev_t match_dev, dev_info_t *dip, uint_t flags, char *name)
3968 {
3969 	int	i;
3970 	uint_t	x = 0;
3971 
3972 	i = ddi_prop_search_common(match_dev, dip, PROP_EXISTS,
3973 		flags | DDI_PROP_TYPE_MASK, name, NULL, &x);
3974 	return (i == DDI_PROP_SUCCESS || i == DDI_PROP_FOUND_1275);
3975 }
3976 
3977 
3978 /*
3979  * Update an array of composite properties.  The driver must
3980  * provide the encode routine.
3981  */
3982 int
3983 ddi_prop_update(dev_t match_dev, dev_info_t *dip,
3984     char *name, void *data, uint_t nelements,
3985     int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
3986 {
3987 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_COMPOSITE,
3988 	    name, data, nelements, prop_create));
3989 }
3990 
3991 /*
3992  * Get a single integer or boolean property and return it.
3993  * If the property does not exists, or cannot be decoded,
3994  * then return the defvalue passed in.
3995  *
3996  * This routine always succeeds.
3997  */
3998 int
3999 ddi_prop_get_int(dev_t match_dev, dev_info_t *dip, uint_t flags,
4000     char *name, int defvalue)
4001 {
4002 	int	data;
4003 	uint_t	nelements;
4004 	int	rval;
4005 
4006 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4007 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2)) {
4008 #ifdef DEBUG
4009 		if (dip != NULL) {
4010 			cmn_err(CE_WARN, "ddi_prop_get_int: invalid flag"
4011 			    " 0x%x (prop = %s, node = %s%d)", flags,
4012 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
4013 		}
4014 #endif /* DEBUG */
4015 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4016 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4017 	}
4018 
4019 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
4020 	    (flags | DDI_PROP_TYPE_INT), name, &data, &nelements,
4021 	    ddi_prop_fm_decode_int)) != DDI_PROP_SUCCESS) {
4022 		if (rval == DDI_PROP_END_OF_DATA)
4023 			data = 1;
4024 		else
4025 			data = defvalue;
4026 	}
4027 	return (data);
4028 }
4029 
4030 /*
4031  * Get a single 64 bit integer or boolean property and return it.
4032  * If the property does not exists, or cannot be decoded,
4033  * then return the defvalue passed in.
4034  *
4035  * This routine always succeeds.
4036  */
4037 int64_t
4038 ddi_prop_get_int64(dev_t match_dev, dev_info_t *dip, uint_t flags,
4039     char *name, int64_t defvalue)
4040 {
4041 	int64_t	data;
4042 	uint_t	nelements;
4043 	int	rval;
4044 
4045 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4046 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2)) {
4047 #ifdef DEBUG
4048 		if (dip != NULL) {
4049 			cmn_err(CE_WARN, "ddi_prop_get_int64: invalid flag"
4050 			    " 0x%x (prop = %s, node = %s%d)", flags,
4051 			    name, ddi_driver_name(dip), ddi_get_instance(dip));
4052 		}
4053 #endif /* DEBUG */
4054 		return (DDI_PROP_INVAL_ARG);
4055 	}
4056 
4057 	if ((rval = ddi_prop_lookup_common(match_dev, dip,
4058 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
4059 	    name, &data, &nelements, ddi_prop_fm_decode_int64))
4060 	    != DDI_PROP_SUCCESS) {
4061 		if (rval == DDI_PROP_END_OF_DATA)
4062 			data = 1;
4063 		else
4064 			data = defvalue;
4065 	}
4066 	return (data);
4067 }
4068 
4069 /*
4070  * Get an array of integer property
4071  */
4072 int
4073 ddi_prop_lookup_int_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4074     char *name, int **data, uint_t *nelements)
4075 {
4076 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4077 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2)) {
4078 #ifdef DEBUG
4079 		if (dip != NULL) {
4080 			cmn_err(CE_WARN, "ddi_prop_lookup_int_array: "
4081 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
4082 			    flags, name, ddi_driver_name(dip),
4083 			    ddi_get_instance(dip));
4084 		}
4085 #endif /* DEBUG */
4086 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4087 		LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4088 	}
4089 
4090 	return (ddi_prop_lookup_common(match_dev, dip,
4091 	    (flags | DDI_PROP_TYPE_INT), name, data,
4092 	    nelements, ddi_prop_fm_decode_ints));
4093 }
4094 
4095 /*
4096  * Get an array of 64 bit integer properties
4097  */
4098 int
4099 ddi_prop_lookup_int64_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4100     char *name, int64_t **data, uint_t *nelements)
4101 {
4102 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4103 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2)) {
4104 #ifdef DEBUG
4105 		if (dip != NULL) {
4106 			cmn_err(CE_WARN, "ddi_prop_lookup_int64_array: "
4107 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
4108 			    flags, name, ddi_driver_name(dip),
4109 			    ddi_get_instance(dip));
4110 		}
4111 #endif /* DEBUG */
4112 		return (DDI_PROP_INVAL_ARG);
4113 	}
4114 
4115 	return (ddi_prop_lookup_common(match_dev, dip,
4116 	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
4117 	    name, data, nelements, ddi_prop_fm_decode_int64_array));
4118 }
4119 
4120 /*
4121  * Update a single integer property.  If the property exists on the drivers
4122  * property list it updates, else it creates it.
4123  */
4124 int
4125 ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
4126     char *name, int data)
4127 {
4128 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
4129 	    name, &data, 1, ddi_prop_fm_encode_ints));
4130 }
4131 
4132 /*
4133  * Update a single 64 bit integer property.
4134  * Update the driver property list if it exists, else create it.
4135  */
4136 int
4137 ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
4138     char *name, int64_t data)
4139 {
4140 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
4141 	    name, &data, 1, ddi_prop_fm_encode_int64));
4142 }
4143 
4144 int
4145 e_ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
4146     char *name, int data)
4147 {
4148 	return (ddi_prop_update_common(match_dev, dip,
4149 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
4150 	    name, &data, 1, ddi_prop_fm_encode_ints));
4151 }
4152 
4153 int
4154 e_ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
4155     char *name, int64_t data)
4156 {
4157 	return (ddi_prop_update_common(match_dev, dip,
4158 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
4159 	    name, &data, 1, ddi_prop_fm_encode_int64));
4160 }
4161 
4162 /*
4163  * Update an array of integer property.  If the property exists on the drivers
4164  * property list it updates, else it creates it.
4165  */
4166 int
4167 ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
4168     char *name, int *data, uint_t nelements)
4169 {
4170 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
4171 	    name, data, nelements, ddi_prop_fm_encode_ints));
4172 }
4173 
4174 /*
4175  * Update an array of 64 bit integer properties.
4176  * Update the driver property list if it exists, else create it.
4177  */
4178 int
4179 ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
4180     char *name, int64_t *data, uint_t nelements)
4181 {
4182 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
4183 	    name, data, nelements, ddi_prop_fm_encode_int64));
4184 }
4185 
4186 int
4187 e_ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
4188     char *name, int64_t *data, uint_t nelements)
4189 {
4190 	return (ddi_prop_update_common(match_dev, dip,
4191 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
4192 	    name, data, nelements, ddi_prop_fm_encode_int64));
4193 }
4194 
4195 int
4196 e_ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
4197     char *name, int *data, uint_t nelements)
4198 {
4199 	return (ddi_prop_update_common(match_dev, dip,
4200 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
4201 	    name, data, nelements, ddi_prop_fm_encode_ints));
4202 }
4203 
4204 /*
4205  * Get a single string property.
4206  */
4207 int
4208 ddi_prop_lookup_string(dev_t match_dev, dev_info_t *dip, uint_t flags,
4209     char *name, char **data)
4210 {
4211 	uint_t x;
4212 
4213 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4214 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2)) {
4215 #ifdef DEBUG
4216 		if (dip != NULL) {
4217 			cmn_err(CE_WARN, "%s: invalid flag 0x%x "
4218 			    "(prop = %s, node = %s%d); invalid bits ignored",
4219 			    "ddi_prop_lookup_string", flags, name,
4220 			    ddi_driver_name(dip), ddi_get_instance(dip));
4221 		}
4222 #endif /* DEBUG */
4223 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4224 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4225 	}
4226 
4227 	return (ddi_prop_lookup_common(match_dev, dip,
4228 	    (flags | DDI_PROP_TYPE_STRING), name, data,
4229 	    &x, ddi_prop_fm_decode_string));
4230 }
4231 
4232 /*
4233  * Get an array of strings property.
4234  */
4235 int
4236 ddi_prop_lookup_string_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4237     char *name, char ***data, uint_t *nelements)
4238 {
4239 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4240 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2)) {
4241 #ifdef DEBUG
4242 		if (dip != NULL) {
4243 			cmn_err(CE_WARN, "ddi_prop_lookup_string_array: "
4244 			    "invalid flag 0x%x (prop = %s, node = %s%d)",
4245 			    flags, name, ddi_driver_name(dip),
4246 			    ddi_get_instance(dip));
4247 		}
4248 #endif /* DEBUG */
4249 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4250 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4251 	}
4252 
4253 	return (ddi_prop_lookup_common(match_dev, dip,
4254 	    (flags | DDI_PROP_TYPE_STRING), name, data,
4255 	    nelements, ddi_prop_fm_decode_strings));
4256 }
4257 
4258 /*
4259  * Update a single string property.
4260  */
4261 int
4262 ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
4263     char *name, char *data)
4264 {
4265 	return (ddi_prop_update_common(match_dev, dip,
4266 	    DDI_PROP_TYPE_STRING, name, &data, 1,
4267 	    ddi_prop_fm_encode_string));
4268 }
4269 
4270 int
4271 e_ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
4272     char *name, char *data)
4273 {
4274 	return (ddi_prop_update_common(match_dev, dip,
4275 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
4276 	    name, &data, 1, ddi_prop_fm_encode_string));
4277 }
4278 
4279 
4280 /*
4281  * Update an array of strings property.
4282  */
4283 int
4284 ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
4285     char *name, char **data, uint_t nelements)
4286 {
4287 	return (ddi_prop_update_common(match_dev, dip,
4288 	    DDI_PROP_TYPE_STRING, name, data, nelements,
4289 	    ddi_prop_fm_encode_strings));
4290 }
4291 
4292 int
4293 e_ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
4294     char *name, char **data, uint_t nelements)
4295 {
4296 	return (ddi_prop_update_common(match_dev, dip,
4297 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
4298 	    name, data, nelements,
4299 	    ddi_prop_fm_encode_strings));
4300 }
4301 
4302 
4303 /*
4304  * Get an array of bytes property.
4305  */
4306 int
4307 ddi_prop_lookup_byte_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
4308     char *name, uchar_t **data, uint_t *nelements)
4309 {
4310 	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4311 	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2)) {
4312 #ifdef DEBUG
4313 		if (dip != NULL) {
4314 			cmn_err(CE_WARN, "ddi_prop_lookup_byte_array: "
4315 			    " invalid flag 0x%x (prop = %s, node = %s%d)",
4316 			    flags, name, ddi_driver_name(dip),
4317 			    ddi_get_instance(dip));
4318 		}
4319 #endif /* DEBUG */
4320 		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
4321 		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
4322 	}
4323 
4324 	return (ddi_prop_lookup_common(match_dev, dip,
4325 	    (flags | DDI_PROP_TYPE_BYTE), name, data,
4326 	    nelements, ddi_prop_fm_decode_bytes));
4327 }
4328 
4329 /*
4330  * Update an array of bytes property.
4331  */
4332 int
4333 ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
4334     char *name, uchar_t *data, uint_t nelements)
4335 {
4336 	if (nelements == 0)
4337 		return (DDI_PROP_INVAL_ARG);
4338 
4339 	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_BYTE,
4340 	    name, data, nelements, ddi_prop_fm_encode_bytes));
4341 }
4342 
4343 
4344 int
4345 e_ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
4346     char *name, uchar_t *data, uint_t nelements)
4347 {
4348 	if (nelements == 0)
4349 		return (DDI_PROP_INVAL_ARG);
4350 
4351 	return (ddi_prop_update_common(match_dev, dip,
4352 	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE,
4353 	    name, data, nelements, ddi_prop_fm_encode_bytes));
4354 }
4355 
4356 
4357 /*
4358  * ddi_prop_remove_common:	Undefine a managed property:
4359  *			Input dev_t must match dev_t when defined.
4360  *			Returns DDI_PROP_NOT_FOUND, possibly.
4361  *			DDI_PROP_INVAL_ARG is also possible if dev is
4362  *			DDI_DEV_T_ANY or incoming name is the NULL string.
4363  */
4364 int
4365 ddi_prop_remove_common(dev_t dev, dev_info_t *dip, char *name, int flag)
4366 {
4367 	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
4368 	ddi_prop_t	*propp;
4369 	ddi_prop_t	*lastpropp = NULL;
4370 
4371 	if ((dev == DDI_DEV_T_ANY) || (name == (char *)0) ||
4372 	    (strlen(name) == 0)) {
4373 		return (DDI_PROP_INVAL_ARG);
4374 	}
4375 
4376 	if (flag & DDI_PROP_SYSTEM_DEF)
4377 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
4378 	else if (flag & DDI_PROP_HW_DEF)
4379 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
4380 
4381 	mutex_enter(&(DEVI(dip)->devi_lock));
4382 
4383 	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
4384 		if (DDI_STRSAME(propp->prop_name, name) &&
4385 		    (dev == propp->prop_dev)) {
4386 			/*
4387 			 * Unlink this propp allowing for it to
4388 			 * be first in the list:
4389 			 */
4390 
4391 			if (lastpropp == NULL)
4392 				*list_head = propp->prop_next;
4393 			else
4394 				lastpropp->prop_next = propp->prop_next;
4395 
4396 			mutex_exit(&(DEVI(dip)->devi_lock));
4397 
4398 			/*
4399 			 * Free memory and return...
4400 			 */
4401 			kmem_free(propp->prop_name,
4402 			    strlen(propp->prop_name) + 1);
4403 			if (propp->prop_len != 0)
4404 				kmem_free(propp->prop_val, propp->prop_len);
4405 			kmem_free(propp, sizeof (ddi_prop_t));
4406 			return (DDI_PROP_SUCCESS);
4407 		}
4408 		lastpropp = propp;
4409 	}
4410 	mutex_exit(&(DEVI(dip)->devi_lock));
4411 	return (DDI_PROP_NOT_FOUND);
4412 }
4413 
4414 int
4415 ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
4416 {
4417 	return (ddi_prop_remove_common(dev, dip, name, 0));
4418 }
4419 
4420 int
4421 e_ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
4422 {
4423 	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_SYSTEM_DEF));
4424 }
4425 
4426 /*
4427  * e_ddi_prop_list_delete: remove a list of properties
4428  *	Note that the caller needs to provide the required protection
4429  *	(eg. devi_lock if these properties are still attached to a devi)
4430  */
4431 void
4432 e_ddi_prop_list_delete(ddi_prop_t *props)
4433 {
4434 	i_ddi_prop_list_delete(props);
4435 }
4436 
4437 /*
4438  * ddi_prop_remove_all_common:
4439  *	Used before unloading a driver to remove
4440  *	all properties. (undefines all dev_t's props.)
4441  *	Also removes `explicitly undefined' props.
4442  *	No errors possible.
4443  */
4444 void
4445 ddi_prop_remove_all_common(dev_info_t *dip, int flag)
4446 {
4447 	ddi_prop_t	**list_head;
4448 
4449 	mutex_enter(&(DEVI(dip)->devi_lock));
4450 	if (flag & DDI_PROP_SYSTEM_DEF) {
4451 		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
4452 	} else if (flag & DDI_PROP_HW_DEF) {
4453 		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
4454 	} else {
4455 		list_head = &(DEVI(dip)->devi_drv_prop_ptr);
4456 	}
4457 	i_ddi_prop_list_delete(*list_head);
4458 	*list_head = NULL;
4459 	mutex_exit(&(DEVI(dip)->devi_lock));
4460 }
4461 
4462 
4463 /*
4464  * ddi_prop_remove_all:		Remove all driver prop definitions.
4465  */
4466 
4467 void
4468 ddi_prop_remove_all(dev_info_t *dip)
4469 {
4470 	ddi_prop_remove_all_common(dip, 0);
4471 }
4472 
4473 /*
4474  * e_ddi_prop_remove_all:	Remove all system prop definitions.
4475  */
4476 
4477 void
4478 e_ddi_prop_remove_all(dev_info_t *dip)
4479 {
4480 	ddi_prop_remove_all_common(dip, (int)DDI_PROP_SYSTEM_DEF);
4481 }
4482 
4483 
4484 /*
4485  * ddi_prop_undefine:	Explicitly undefine a property.  Property
4486  *			searches which match this property return
4487  *			the error code DDI_PROP_UNDEFINED.
4488  *
4489  *			Use ddi_prop_remove to negate effect of
4490  *			ddi_prop_undefine
4491  *
4492  *			See above for error returns.
4493  */
4494 
4495 int
4496 ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
4497 {
4498 	if (!(flag & DDI_PROP_CANSLEEP))
4499 		flag |= DDI_PROP_DONTSLEEP;
4500 	return (ddi_prop_update_common(dev, dip,
4501 	    (flag | DDI_PROP_STACK_CREATE | DDI_PROP_UNDEF_IT |
4502 	    DDI_PROP_TYPE_ANY), name, NULL, 0, ddi_prop_fm_encode_bytes));
4503 }
4504 
4505 int
4506 e_ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
4507 {
4508 	if (!(flag & DDI_PROP_CANSLEEP))
4509 		flag |= DDI_PROP_DONTSLEEP;
4510 	return (ddi_prop_update_common(dev, dip,
4511 	    (flag | DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE |
4512 	    DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY),
4513 	    name, NULL, 0, ddi_prop_fm_encode_bytes));
4514 }
4515 
4516 /*
4517  * Code to search hardware layer (PROM), if it exists, on behalf of child.
4518  *
4519  * if input dip != child_dip, then call is on behalf of child
4520  * to search PROM, do it via ddi_prop_search_common() and ascend only
4521  * if allowed.
4522  *
4523  * if input dip == ch_dip (child_dip), call is on behalf of root driver,
4524  * to search for PROM defined props only.
4525  *
4526  * Note that the PROM search is done only if the requested dev
4527  * is either DDI_DEV_T_ANY or DDI_DEV_T_NONE. PROM properties
4528  * have no associated dev, thus are automatically associated with
4529  * DDI_DEV_T_NONE.
4530  *
4531  * Modifying flag DDI_PROP_NOTPROM inhibits the search in the h/w layer.
4532  *
4533  * Returns DDI_PROP_FOUND_1275 if found to indicate to framework
4534  * that the property resides in the prom.
4535  */
4536 int
4537 impl_ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
4538     ddi_prop_op_t prop_op, int mod_flags,
4539     char *name, caddr_t valuep, int *lengthp)
4540 {
4541 	int	len;
4542 	caddr_t buffer;
4543 
4544 	/*
4545 	 * If requested dev is DDI_DEV_T_NONE or DDI_DEV_T_ANY, then
4546 	 * look in caller's PROM if it's a self identifying device...
4547 	 *
4548 	 * Note that this is very similar to ddi_prop_op, but we
4549 	 * search the PROM instead of the s/w defined properties,
4550 	 * and we are called on by the parent driver to do this for
4551 	 * the child.
4552 	 */
4553 
4554 	if (((dev == DDI_DEV_T_NONE) || (dev == DDI_DEV_T_ANY)) &&
4555 	    ndi_dev_is_prom_node(ch_dip) &&
4556 	    ((mod_flags & DDI_PROP_NOTPROM) == 0)) {
4557 		len = prom_getproplen((pnode_t)DEVI(ch_dip)->devi_nodeid, name);
4558 		if (len == -1) {
4559 			return (DDI_PROP_NOT_FOUND);
4560 		}
4561 
4562 		/*
4563 		 * If exists only request, we're done
4564 		 */
4565 		if (prop_op == PROP_EXISTS) {
4566 			return (DDI_PROP_FOUND_1275);
4567 		}
4568 
4569 		/*
4570 		 * If length only request or prop length == 0, get out
4571 		 */
4572 		if ((prop_op == PROP_LEN) || (len == 0)) {
4573 			*lengthp = len;
4574 			return (DDI_PROP_FOUND_1275);
4575 		}
4576 
4577 		/*
4578 		 * Allocate buffer if required... (either way `buffer'
4579 		 * is receiving address).
4580 		 */
4581 
4582 		switch (prop_op) {
4583 
4584 		case PROP_LEN_AND_VAL_ALLOC:
4585 
4586 			buffer = kmem_alloc((size_t)len,
4587 			    mod_flags & DDI_PROP_CANSLEEP ?
4588 			    KM_SLEEP : KM_NOSLEEP);
4589 			if (buffer == NULL) {
4590 				return (DDI_PROP_NO_MEMORY);
4591 			}
4592 			*(caddr_t *)valuep = buffer;
4593 			break;
4594 
4595 		case PROP_LEN_AND_VAL_BUF:
4596 
4597 			if (len > (*lengthp)) {
4598 				*lengthp = len;
4599 				return (DDI_PROP_BUF_TOO_SMALL);
4600 			}
4601 
4602 			buffer = valuep;
4603 			break;
4604 
4605 		default:
4606 			break;
4607 		}
4608 
4609 		/*
4610 		 * Call the PROM function to do the copy.
4611 		 */
4612 		(void) prom_getprop((pnode_t)DEVI(ch_dip)->devi_nodeid,
4613 			name, buffer);
4614 
4615 		*lengthp = len; /* return the actual length to the caller */
4616 		(void) impl_fix_props(dip, ch_dip, name, len, buffer);
4617 		return (DDI_PROP_FOUND_1275);
4618 	}
4619 
4620 	return (DDI_PROP_NOT_FOUND);
4621 }
4622 
4623 /*
4624  * The ddi_bus_prop_op default bus nexus prop op function.
4625  *
4626  * Code to search hardware layer (PROM), if it exists,
4627  * on behalf of child, then, if appropriate, ascend and check
4628  * my own software defined properties...
4629  */
4630 int
4631 ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
4632     ddi_prop_op_t prop_op, int mod_flags,
4633     char *name, caddr_t valuep, int *lengthp)
4634 {
4635 	int	error;
4636 
4637 	error = impl_ddi_bus_prop_op(dev, dip, ch_dip, prop_op, mod_flags,
4638 				    name, valuep, lengthp);
4639 
4640 	if (error == DDI_PROP_SUCCESS || error == DDI_PROP_FOUND_1275 ||
4641 	    error == DDI_PROP_BUF_TOO_SMALL)
4642 		return (error);
4643 
4644 	if (error == DDI_PROP_NO_MEMORY) {
4645 		cmn_err(CE_CONT, prop_no_mem_msg, name);
4646 		return (DDI_PROP_NO_MEMORY);
4647 	}
4648 
4649 	/*
4650 	 * Check the 'options' node as a last resort
4651 	 */
4652 	if ((mod_flags & DDI_PROP_DONTPASS) != 0)
4653 		return (DDI_PROP_NOT_FOUND);
4654 
4655 	if (ch_dip == ddi_root_node())	{
4656 		/*
4657 		 * As a last resort, when we've reached
4658 		 * the top and still haven't found the
4659 		 * property, see if the desired property
4660 		 * is attached to the options node.
4661 		 *
4662 		 * The options dip is attached right after boot.
4663 		 */
4664 		ASSERT(options_dip != NULL);
4665 		/*
4666 		 * Force the "don't pass" flag to *just* see
4667 		 * what the options node has to offer.
4668 		 */
4669 		return (ddi_prop_search_common(dev, options_dip, prop_op,
4670 		    mod_flags|DDI_PROP_DONTPASS, name, valuep,
4671 		    (uint_t *)lengthp));
4672 	}
4673 
4674 	/*
4675 	 * Otherwise, continue search with parent's s/w defined properties...
4676 	 * NOTE: Using `dip' in following call increments the level.
4677 	 */
4678 
4679 	return (ddi_prop_search_common(dev, dip, prop_op, mod_flags,
4680 	    name, valuep, (uint_t *)lengthp));
4681 }
4682 
4683 /*
4684  * External property functions used by other parts of the kernel...
4685  */
4686 
4687 /*
4688  * e_ddi_getlongprop: See comments for ddi_get_longprop.
4689  */
4690 
4691 int
4692 e_ddi_getlongprop(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_ALLOC;
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_getlongprop_buf:	See comments for ddi_getlongprop_buf.
4710  */
4711 
4712 int
4713 e_ddi_getlongprop_buf(dev_t dev, vtype_t type, char *name, int flags,
4714     caddr_t valuep, int *lengthp)
4715 {
4716 	_NOTE(ARGUNUSED(type))
4717 	dev_info_t *devi;
4718 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
4719 	int error;
4720 
4721 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4722 		return (DDI_PROP_NOT_FOUND);
4723 
4724 	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
4725 	ddi_release_devi(devi);
4726 	return (error);
4727 }
4728 
4729 /*
4730  * e_ddi_getprop:	See comments for ddi_getprop.
4731  */
4732 int
4733 e_ddi_getprop(dev_t dev, vtype_t type, char *name, int flags, int defvalue)
4734 {
4735 	_NOTE(ARGUNUSED(type))
4736 	dev_info_t *devi;
4737 	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
4738 	int	propvalue = defvalue;
4739 	int	proplength = sizeof (int);
4740 	int	error;
4741 
4742 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4743 		return (defvalue);
4744 
4745 	error = cdev_prop_op(dev, devi, prop_op,
4746 	    flags, name, (caddr_t)&propvalue, &proplength);
4747 	ddi_release_devi(devi);
4748 
4749 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
4750 		propvalue = 1;
4751 
4752 	return (propvalue);
4753 }
4754 
4755 /*
4756  * e_ddi_getprop_int64:
4757  *
4758  * This is a typed interfaces, but predates typed properties. With the
4759  * introduction of typed properties the framework tries to ensure
4760  * consistent use of typed interfaces. This is why TYPE_INT64 is not
4761  * part of TYPE_ANY.  E_ddi_getprop_int64 is a special case where a
4762  * typed interface invokes legacy (non-typed) interfaces:
4763  * cdev_prop_op(), prop_op(9E), ddi_prop_op(9F)).  In this case the
4764  * fact that TYPE_INT64 is not part of TYPE_ANY matters.  To support
4765  * this type of lookup as a single operation we invoke the legacy
4766  * non-typed interfaces with the special CONSUMER_TYPED bit set. The
4767  * framework ddi_prop_op(9F) implementation is expected to check for
4768  * CONSUMER_TYPED and, if set, expand type bits beyond TYPE_ANY
4769  * (currently TYPE_INT64).
4770  */
4771 int64_t
4772 e_ddi_getprop_int64(dev_t dev, vtype_t type, char *name,
4773     int flags, int64_t defvalue)
4774 {
4775 	_NOTE(ARGUNUSED(type))
4776 	dev_info_t	*devi;
4777 	ddi_prop_op_t	prop_op = PROP_LEN_AND_VAL_BUF;
4778 	int64_t		propvalue = defvalue;
4779 	int		proplength = sizeof (propvalue);
4780 	int		error;
4781 
4782 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4783 		return (defvalue);
4784 
4785 	error = cdev_prop_op(dev, devi, prop_op, flags |
4786 	    DDI_PROP_CONSUMER_TYPED, name, (caddr_t)&propvalue, &proplength);
4787 	ddi_release_devi(devi);
4788 
4789 	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
4790 		propvalue = 1;
4791 
4792 	return (propvalue);
4793 }
4794 
4795 /*
4796  * e_ddi_getproplen:	See comments for ddi_getproplen.
4797  */
4798 int
4799 e_ddi_getproplen(dev_t dev, vtype_t type, char *name, int flags, int *lengthp)
4800 {
4801 	_NOTE(ARGUNUSED(type))
4802 	dev_info_t *devi;
4803 	ddi_prop_op_t prop_op = PROP_LEN;
4804 	int error;
4805 
4806 	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
4807 		return (DDI_PROP_NOT_FOUND);
4808 
4809 	error = cdev_prop_op(dev, devi, prop_op, flags, name, NULL, lengthp);
4810 	ddi_release_devi(devi);
4811 	return (error);
4812 }
4813 
4814 /*
4815  * Routines to get at elements of the dev_info structure
4816  */
4817 
4818 /*
4819  * ddi_binding_name: Return the driver binding name of the devinfo node
4820  *		This is the name the OS used to bind the node to a driver.
4821  */
4822 char *
4823 ddi_binding_name(dev_info_t *dip)
4824 {
4825 	return (DEVI(dip)->devi_binding_name);
4826 }
4827 
4828 /*
4829  * ddi_driver_major: Return the major number of the driver that
4830  *		the supplied devinfo is bound to (-1 if none)
4831  */
4832 major_t
4833 ddi_driver_major(dev_info_t *devi)
4834 {
4835 	return (DEVI(devi)->devi_major);
4836 }
4837 
4838 /*
4839  * ddi_driver_name: Return the normalized driver name. this is the
4840  *		actual driver name
4841  */
4842 const char *
4843 ddi_driver_name(dev_info_t *devi)
4844 {
4845 	major_t major;
4846 
4847 	if ((major = ddi_driver_major(devi)) != (major_t)-1)
4848 		return (ddi_major_to_name(major));
4849 
4850 	return (ddi_node_name(devi));
4851 }
4852 
4853 /*
4854  * i_ddi_set_binding_name:	Set binding name.
4855  *
4856  *	Set the binding name to the given name.
4857  *	This routine is for use by the ddi implementation, not by drivers.
4858  */
4859 void
4860 i_ddi_set_binding_name(dev_info_t *dip, char *name)
4861 {
4862 	DEVI(dip)->devi_binding_name = name;
4863 
4864 }
4865 
4866 /*
4867  * ddi_get_name: A synonym of ddi_binding_name() ... returns a name
4868  * the implementation has used to bind the node to a driver.
4869  */
4870 char *
4871 ddi_get_name(dev_info_t *dip)
4872 {
4873 	return (DEVI(dip)->devi_binding_name);
4874 }
4875 
4876 /*
4877  * ddi_node_name: Return the name property of the devinfo node
4878  *		This may differ from ddi_binding_name if the node name
4879  *		does not define a binding to a driver (i.e. generic names).
4880  */
4881 char *
4882 ddi_node_name(dev_info_t *dip)
4883 {
4884 	return (DEVI(dip)->devi_node_name);
4885 }
4886 
4887 
4888 /*
4889  * ddi_get_nodeid:	Get nodeid stored in dev_info structure.
4890  */
4891 int
4892 ddi_get_nodeid(dev_info_t *dip)
4893 {
4894 	return (DEVI(dip)->devi_nodeid);
4895 }
4896 
4897 int
4898 ddi_get_instance(dev_info_t *dip)
4899 {
4900 	return (DEVI(dip)->devi_instance);
4901 }
4902 
4903 struct dev_ops *
4904 ddi_get_driver(dev_info_t *dip)
4905 {
4906 	return (DEVI(dip)->devi_ops);
4907 }
4908 
4909 void
4910 ddi_set_driver(dev_info_t *dip, struct dev_ops *devo)
4911 {
4912 	DEVI(dip)->devi_ops = devo;
4913 }
4914 
4915 /*
4916  * ddi_set_driver_private/ddi_get_driver_private:
4917  * Get/set device driver private data in devinfo.
4918  */
4919 void
4920 ddi_set_driver_private(dev_info_t *dip, void *data)
4921 {
4922 	DEVI(dip)->devi_driver_data = data;
4923 }
4924 
4925 void *
4926 ddi_get_driver_private(dev_info_t *dip)
4927 {
4928 	return (DEVI(dip)->devi_driver_data);
4929 }
4930 
4931 /*
4932  * ddi_get_parent, ddi_get_child, ddi_get_next_sibling
4933  */
4934 
4935 dev_info_t *
4936 ddi_get_parent(dev_info_t *dip)
4937 {
4938 	return ((dev_info_t *)DEVI(dip)->devi_parent);
4939 }
4940 
4941 dev_info_t *
4942 ddi_get_child(dev_info_t *dip)
4943 {
4944 	return ((dev_info_t *)DEVI(dip)->devi_child);
4945 }
4946 
4947 dev_info_t *
4948 ddi_get_next_sibling(dev_info_t *dip)
4949 {
4950 	return ((dev_info_t *)DEVI(dip)->devi_sibling);
4951 }
4952 
4953 dev_info_t *
4954 ddi_get_next(dev_info_t *dip)
4955 {
4956 	return ((dev_info_t *)DEVI(dip)->devi_next);
4957 }
4958 
4959 void
4960 ddi_set_next(dev_info_t *dip, dev_info_t *nextdip)
4961 {
4962 	DEVI(dip)->devi_next = DEVI(nextdip);
4963 }
4964 
4965 /*
4966  * ddi_root_node:		Return root node of devinfo tree
4967  */
4968 
4969 dev_info_t *
4970 ddi_root_node(void)
4971 {
4972 	extern dev_info_t *top_devinfo;
4973 
4974 	return (top_devinfo);
4975 }
4976 
4977 /*
4978  * Miscellaneous functions:
4979  */
4980 
4981 /*
4982  * Implementation specific hooks
4983  */
4984 
4985 void
4986 ddi_report_dev(dev_info_t *d)
4987 {
4988 	char *b;
4989 
4990 	(void) ddi_ctlops(d, d, DDI_CTLOPS_REPORTDEV, (void *)0, (void *)0);
4991 
4992 	/*
4993 	 * If this devinfo node has cb_ops, it's implicitly accessible from
4994 	 * userland, so we print its full name together with the instance
4995 	 * number 'abbreviation' that the driver may use internally.
4996 	 */
4997 	if (DEVI(d)->devi_ops->devo_cb_ops != (struct cb_ops *)0 &&
4998 	    (b = kmem_zalloc(MAXPATHLEN, KM_NOSLEEP))) {
4999 		cmn_err(CE_CONT, "?%s%d is %s\n",
5000 		    ddi_driver_name(d), ddi_get_instance(d),
5001 		    ddi_pathname(d, b));
5002 		kmem_free(b, MAXPATHLEN);
5003 	}
5004 }
5005 
5006 /*
5007  * ddi_ctlops() is described in the assembler not to buy a new register
5008  * window when it's called and can reduce cost in climbing the device tree
5009  * without using the tail call optimization.
5010  */
5011 int
5012 ddi_dev_regsize(dev_info_t *dev, uint_t rnumber, off_t *result)
5013 {
5014 	int ret;
5015 
5016 	ret = ddi_ctlops(dev, dev, DDI_CTLOPS_REGSIZE,
5017 	    (void *)&rnumber, (void *)result);
5018 
5019 	return (ret == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
5020 }
5021 
5022 int
5023 ddi_dev_nregs(dev_info_t *dev, int *result)
5024 {
5025 	return (ddi_ctlops(dev, dev, DDI_CTLOPS_NREGS, 0, (void *)result));
5026 }
5027 
5028 int
5029 ddi_dev_is_sid(dev_info_t *d)
5030 {
5031 	return (ddi_ctlops(d, d, DDI_CTLOPS_SIDDEV, (void *)0, (void *)0));
5032 }
5033 
5034 int
5035 ddi_slaveonly(dev_info_t *d)
5036 {
5037 	return (ddi_ctlops(d, d, DDI_CTLOPS_SLAVEONLY, (void *)0, (void *)0));
5038 }
5039 
5040 int
5041 ddi_dev_affinity(dev_info_t *a, dev_info_t *b)
5042 {
5043 	return (ddi_ctlops(a, a, DDI_CTLOPS_AFFINITY, (void *)b, (void *)0));
5044 }
5045 
5046 int
5047 ddi_streams_driver(dev_info_t *dip)
5048 {
5049 	if (i_ddi_devi_attached(dip) &&
5050 	    (DEVI(dip)->devi_ops->devo_cb_ops != NULL) &&
5051 	    (DEVI(dip)->devi_ops->devo_cb_ops->cb_str != NULL))
5052 		return (DDI_SUCCESS);
5053 	return (DDI_FAILURE);
5054 }
5055 
5056 /*
5057  * callback free list
5058  */
5059 
5060 static int ncallbacks;
5061 static int nc_low = 170;
5062 static int nc_med = 512;
5063 static int nc_high = 2048;
5064 static struct ddi_callback *callbackq;
5065 static struct ddi_callback *callbackqfree;
5066 
5067 /*
5068  * set/run callback lists
5069  */
5070 struct	cbstats	{
5071 	kstat_named_t	cb_asked;
5072 	kstat_named_t	cb_new;
5073 	kstat_named_t	cb_run;
5074 	kstat_named_t	cb_delete;
5075 	kstat_named_t	cb_maxreq;
5076 	kstat_named_t	cb_maxlist;
5077 	kstat_named_t	cb_alloc;
5078 	kstat_named_t	cb_runouts;
5079 	kstat_named_t	cb_L2;
5080 	kstat_named_t	cb_grow;
5081 } cbstats = {
5082 	{"asked",	KSTAT_DATA_UINT32},
5083 	{"new",		KSTAT_DATA_UINT32},
5084 	{"run",		KSTAT_DATA_UINT32},
5085 	{"delete",	KSTAT_DATA_UINT32},
5086 	{"maxreq",	KSTAT_DATA_UINT32},
5087 	{"maxlist",	KSTAT_DATA_UINT32},
5088 	{"alloc",	KSTAT_DATA_UINT32},
5089 	{"runouts",	KSTAT_DATA_UINT32},
5090 	{"L2",		KSTAT_DATA_UINT32},
5091 	{"grow",	KSTAT_DATA_UINT32},
5092 };
5093 
5094 #define	nc_asked	cb_asked.value.ui32
5095 #define	nc_new		cb_new.value.ui32
5096 #define	nc_run		cb_run.value.ui32
5097 #define	nc_delete	cb_delete.value.ui32
5098 #define	nc_maxreq	cb_maxreq.value.ui32
5099 #define	nc_maxlist	cb_maxlist.value.ui32
5100 #define	nc_alloc	cb_alloc.value.ui32
5101 #define	nc_runouts	cb_runouts.value.ui32
5102 #define	nc_L2		cb_L2.value.ui32
5103 #define	nc_grow		cb_grow.value.ui32
5104 
5105 static kmutex_t ddi_callback_mutex;
5106 
5107 /*
5108  * callbacks are handled using a L1/L2 cache. The L1 cache
5109  * comes out of kmem_cache_alloc and can expand/shrink dynamically. If
5110  * we can't get callbacks from the L1 cache [because pageout is doing
5111  * I/O at the time freemem is 0], we allocate callbacks out of the
5112  * L2 cache. The L2 cache is static and depends on the memory size.
5113  * [We might also count the number of devices at probe time and
5114  * allocate one structure per device and adjust for deferred attach]
5115  */
5116 void
5117 impl_ddi_callback_init(void)
5118 {
5119 	int	i;
5120 	uint_t	physmegs;
5121 	kstat_t	*ksp;
5122 
5123 	physmegs = physmem >> (20 - PAGESHIFT);
5124 	if (physmegs < 48) {
5125 		ncallbacks = nc_low;
5126 	} else if (physmegs < 128) {
5127 		ncallbacks = nc_med;
5128 	} else {
5129 		ncallbacks = nc_high;
5130 	}
5131 
5132 	/*
5133 	 * init free list
5134 	 */
5135 	callbackq = kmem_zalloc(
5136 	    ncallbacks * sizeof (struct ddi_callback), KM_SLEEP);
5137 	for (i = 0; i < ncallbacks-1; i++)
5138 		callbackq[i].c_nfree = &callbackq[i+1];
5139 	callbackqfree = callbackq;
5140 
5141 	/* init kstats */
5142 	if (ksp = kstat_create("unix", 0, "cbstats", "misc", KSTAT_TYPE_NAMED,
5143 	    sizeof (cbstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) {
5144 		ksp->ks_data = (void *) &cbstats;
5145 		kstat_install(ksp);
5146 	}
5147 
5148 }
5149 
5150 static void
5151 callback_insert(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid,
5152 	int count)
5153 {
5154 	struct ddi_callback *list, *marker, *new;
5155 	size_t size = sizeof (struct ddi_callback);
5156 
5157 	list = marker = (struct ddi_callback *)*listid;
5158 	while (list != NULL) {
5159 		if (list->c_call == funcp && list->c_arg == arg) {
5160 			list->c_count += count;
5161 			return;
5162 		}
5163 		marker = list;
5164 		list = list->c_nlist;
5165 	}
5166 	new = kmem_alloc(size, KM_NOSLEEP);
5167 	if (new == NULL) {
5168 		new = callbackqfree;
5169 		if (new == NULL) {
5170 			new = kmem_alloc_tryhard(sizeof (struct ddi_callback),
5171 			    &size, KM_NOSLEEP | KM_PANIC);
5172 			cbstats.nc_grow++;
5173 		} else {
5174 			callbackqfree = new->c_nfree;
5175 			cbstats.nc_L2++;
5176 		}
5177 	}
5178 	if (marker != NULL) {
5179 		marker->c_nlist = new;
5180 	} else {
5181 		*listid = (uintptr_t)new;
5182 	}
5183 	new->c_size = size;
5184 	new->c_nlist = NULL;
5185 	new->c_call = funcp;
5186 	new->c_arg = arg;
5187 	new->c_count = count;
5188 	cbstats.nc_new++;
5189 	cbstats.nc_alloc++;
5190 	if (cbstats.nc_alloc > cbstats.nc_maxlist)
5191 		cbstats.nc_maxlist = cbstats.nc_alloc;
5192 }
5193 
5194 void
5195 ddi_set_callback(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid)
5196 {
5197 	mutex_enter(&ddi_callback_mutex);
5198 	cbstats.nc_asked++;
5199 	if ((cbstats.nc_asked - cbstats.nc_run) > cbstats.nc_maxreq)
5200 		cbstats.nc_maxreq = (cbstats.nc_asked - cbstats.nc_run);
5201 	(void) callback_insert(funcp, arg, listid, 1);
5202 	mutex_exit(&ddi_callback_mutex);
5203 }
5204 
5205 static void
5206 real_callback_run(void *Queue)
5207 {
5208 	int (*funcp)(caddr_t);
5209 	caddr_t arg;
5210 	int count, rval;
5211 	uintptr_t *listid;
5212 	struct ddi_callback *list, *marker;
5213 	int check_pending = 1;
5214 	int pending = 0;
5215 
5216 	do {
5217 		mutex_enter(&ddi_callback_mutex);
5218 		listid = Queue;
5219 		list = (struct ddi_callback *)*listid;
5220 		if (list == NULL) {
5221 			mutex_exit(&ddi_callback_mutex);
5222 			return;
5223 		}
5224 		if (check_pending) {
5225 			marker = list;
5226 			while (marker != NULL) {
5227 				pending += marker->c_count;
5228 				marker = marker->c_nlist;
5229 			}
5230 			check_pending = 0;
5231 		}
5232 		ASSERT(pending > 0);
5233 		ASSERT(list->c_count > 0);
5234 		funcp = list->c_call;
5235 		arg = list->c_arg;
5236 		count = list->c_count;
5237 		*(uintptr_t *)Queue = (uintptr_t)list->c_nlist;
5238 		if (list >= &callbackq[0] &&
5239 		    list <= &callbackq[ncallbacks-1]) {
5240 			list->c_nfree = callbackqfree;
5241 			callbackqfree = list;
5242 		} else
5243 			kmem_free(list, list->c_size);
5244 
5245 		cbstats.nc_delete++;
5246 		cbstats.nc_alloc--;
5247 		mutex_exit(&ddi_callback_mutex);
5248 
5249 		do {
5250 			if ((rval = (*funcp)(arg)) == 0) {
5251 				pending -= count;
5252 				mutex_enter(&ddi_callback_mutex);
5253 				(void) callback_insert(funcp, arg, listid,
5254 					count);
5255 				cbstats.nc_runouts++;
5256 			} else {
5257 				pending--;
5258 				mutex_enter(&ddi_callback_mutex);
5259 				cbstats.nc_run++;
5260 			}
5261 			mutex_exit(&ddi_callback_mutex);
5262 		} while (rval != 0 && (--count > 0));
5263 	} while (pending > 0);
5264 }
5265 
5266 void
5267 ddi_run_callback(uintptr_t *listid)
5268 {
5269 	softcall(real_callback_run, listid);
5270 }
5271 
5272 dev_info_t *
5273 nodevinfo(dev_t dev, int otyp)
5274 {
5275 	_NOTE(ARGUNUSED(dev, otyp))
5276 	return ((dev_info_t *)0);
5277 }
5278 
5279 /*
5280  * A driver should support its own getinfo(9E) entry point. This function
5281  * is provided as a convenience for ON drivers that don't expect their
5282  * getinfo(9E) entry point to be called. A driver that uses this must not
5283  * call ddi_create_minor_node.
5284  */
5285 int
5286 ddi_no_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
5287 {
5288 	_NOTE(ARGUNUSED(dip, infocmd, arg, result))
5289 	return (DDI_FAILURE);
5290 }
5291 
5292 /*
5293  * A driver should support its own getinfo(9E) entry point. This function
5294  * is provided as a convenience for ON drivers that where the minor number
5295  * is the instance. Drivers that do not have 1:1 mapping must implement
5296  * their own getinfo(9E) function.
5297  */
5298 int
5299 ddi_getinfo_1to1(dev_info_t *dip, ddi_info_cmd_t infocmd,
5300     void *arg, void **result)
5301 {
5302 	_NOTE(ARGUNUSED(dip))
5303 	int	instance;
5304 
5305 	if (infocmd != DDI_INFO_DEVT2INSTANCE)
5306 		return (DDI_FAILURE);
5307 
5308 	instance = getminor((dev_t)(uintptr_t)arg);
5309 	*result = (void *)(uintptr_t)instance;
5310 	return (DDI_SUCCESS);
5311 }
5312 
5313 int
5314 ddifail(dev_info_t *devi, ddi_attach_cmd_t cmd)
5315 {
5316 	_NOTE(ARGUNUSED(devi, cmd))
5317 	return (DDI_FAILURE);
5318 }
5319 
5320 int
5321 ddi_no_dma_map(dev_info_t *dip, dev_info_t *rdip,
5322     struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
5323 {
5324 	_NOTE(ARGUNUSED(dip, rdip, dmareqp, handlep))
5325 	return (DDI_DMA_NOMAPPING);
5326 }
5327 
5328 int
5329 ddi_no_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
5330     int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
5331 {
5332 	_NOTE(ARGUNUSED(dip, rdip, attr, waitfp, arg, handlep))
5333 	return (DDI_DMA_BADATTR);
5334 }
5335 
5336 int
5337 ddi_no_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
5338     ddi_dma_handle_t handle)
5339 {
5340 	_NOTE(ARGUNUSED(dip, rdip, handle))
5341 	return (DDI_FAILURE);
5342 }
5343 
5344 int
5345 ddi_no_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
5346     ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
5347     ddi_dma_cookie_t *cp, uint_t *ccountp)
5348 {
5349 	_NOTE(ARGUNUSED(dip, rdip, handle, dmareq, cp, ccountp))
5350 	return (DDI_DMA_NOMAPPING);
5351 }
5352 
5353 int
5354 ddi_no_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
5355     ddi_dma_handle_t handle)
5356 {
5357 	_NOTE(ARGUNUSED(dip, rdip, handle))
5358 	return (DDI_FAILURE);
5359 }
5360 
5361 int
5362 ddi_no_dma_flush(dev_info_t *dip, dev_info_t *rdip,
5363     ddi_dma_handle_t handle, off_t off, size_t len,
5364     uint_t cache_flags)
5365 {
5366 	_NOTE(ARGUNUSED(dip, rdip, handle, off, len, cache_flags))
5367 	return (DDI_FAILURE);
5368 }
5369 
5370 int
5371 ddi_no_dma_win(dev_info_t *dip, dev_info_t *rdip,
5372     ddi_dma_handle_t handle, uint_t win, off_t *offp,
5373     size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
5374 {
5375 	_NOTE(ARGUNUSED(dip, rdip, handle, win, offp, lenp, cookiep, ccountp))
5376 	return (DDI_FAILURE);
5377 }
5378 
5379 int
5380 ddi_no_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
5381     ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
5382     off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
5383 {
5384 	_NOTE(ARGUNUSED(dip, rdip, handle, request, offp, lenp, objp, flags))
5385 	return (DDI_FAILURE);
5386 }
5387 
5388 void
5389 ddivoid(void)
5390 {}
5391 
5392 int
5393 nochpoll(dev_t dev, short events, int anyyet, short *reventsp,
5394     struct pollhead **pollhdrp)
5395 {
5396 	_NOTE(ARGUNUSED(dev, events, anyyet, reventsp, pollhdrp))
5397 	return (ENXIO);
5398 }
5399 
5400 cred_t *
5401 ddi_get_cred(void)
5402 {
5403 	return (CRED());
5404 }
5405 
5406 clock_t
5407 ddi_get_lbolt(void)
5408 {
5409 	return (lbolt);
5410 }
5411 
5412 time_t
5413 ddi_get_time(void)
5414 {
5415 	time_t	now;
5416 
5417 	if ((now = gethrestime_sec()) == 0) {
5418 		timestruc_t ts;
5419 		mutex_enter(&tod_lock);
5420 		ts = tod_get();
5421 		mutex_exit(&tod_lock);
5422 		return (ts.tv_sec);
5423 	} else {
5424 		return (now);
5425 	}
5426 }
5427 
5428 pid_t
5429 ddi_get_pid(void)
5430 {
5431 	return (ttoproc(curthread)->p_pid);
5432 }
5433 
5434 kt_did_t
5435 ddi_get_kt_did(void)
5436 {
5437 	return (curthread->t_did);
5438 }
5439 
5440 /*
5441  * This function returns B_TRUE if the caller can reasonably expect that a call
5442  * to cv_wait_sig(9F), cv_timedwait_sig(9F), or qwait_sig(9F) could be awakened
5443  * by user-level signal.  If it returns B_FALSE, then the caller should use
5444  * other means to make certain that the wait will not hang "forever."
5445  *
5446  * It does not check the signal mask, nor for reception of any particular
5447  * signal.
5448  *
5449  * Currently, a thread can receive a signal if it's not a kernel thread and it
5450  * is not in the middle of exit(2) tear-down.  Threads that are in that
5451  * tear-down effectively convert cv_wait_sig to cv_wait, cv_timedwait_sig to
5452  * cv_timedwait, and qwait_sig to qwait.
5453  */
5454 boolean_t
5455 ddi_can_receive_sig(void)
5456 {
5457 	proc_t *pp;
5458 
5459 	if (curthread->t_proc_flag & TP_LWPEXIT)
5460 		return (B_FALSE);
5461 	if ((pp = ttoproc(curthread)) == NULL)
5462 		return (B_FALSE);
5463 	return (pp->p_as != &kas);
5464 }
5465 
5466 /*
5467  * Swap bytes in 16-bit [half-]words
5468  */
5469 void
5470 swab(void *src, void *dst, size_t nbytes)
5471 {
5472 	uchar_t *pf = (uchar_t *)src;
5473 	uchar_t *pt = (uchar_t *)dst;
5474 	uchar_t tmp;
5475 	int nshorts;
5476 
5477 	nshorts = nbytes >> 1;
5478 
5479 	while (--nshorts >= 0) {
5480 		tmp = *pf++;
5481 		*pt++ = *pf++;
5482 		*pt++ = tmp;
5483 	}
5484 }
5485 
5486 static void
5487 ddi_append_minor_node(dev_info_t *ddip, struct ddi_minor_data *dmdp)
5488 {
5489 	struct ddi_minor_data *dp;
5490 
5491 	mutex_enter(&(DEVI(ddip)->devi_lock));
5492 	i_devi_enter(ddip, DEVI_S_MD_UPDATE, DEVI_S_MD_UPDATE, 1);
5493 
5494 	if ((dp = DEVI(ddip)->devi_minor) == (struct ddi_minor_data *)NULL) {
5495 		DEVI(ddip)->devi_minor = dmdp;
5496 	} else {
5497 		while (dp->next != (struct ddi_minor_data *)NULL)
5498 			dp = dp->next;
5499 		dp->next = dmdp;
5500 	}
5501 
5502 	i_devi_exit(ddip, DEVI_S_MD_UPDATE, 1);
5503 	mutex_exit(&(DEVI(ddip)->devi_lock));
5504 }
5505 
5506 /*
5507  * Part of the obsolete SunCluster DDI Hooks.
5508  * Keep for binary compatibility
5509  */
5510 minor_t
5511 ddi_getiminor(dev_t dev)
5512 {
5513 	return (getminor(dev));
5514 }
5515 
5516 static int
5517 i_log_devfs_minor_create(dev_info_t *dip, char *minor_name)
5518 {
5519 	int se_flag;
5520 	int kmem_flag;
5521 	int se_err;
5522 	char *pathname;
5523 	sysevent_t *ev = NULL;
5524 	sysevent_id_t eid;
5525 	sysevent_value_t se_val;
5526 	sysevent_attr_list_t *ev_attr_list = NULL;
5527 
5528 	/* determine interrupt context */
5529 	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
5530 	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
5531 
5532 	i_ddi_di_cache_invalidate(kmem_flag);
5533 
5534 #ifdef DEBUG
5535 	if ((se_flag == SE_NOSLEEP) && sunddi_debug) {
5536 		cmn_err(CE_CONT, "ddi_create_minor_node: called from "
5537 		    "interrupt level by driver %s",
5538 		    ddi_driver_name(dip));
5539 	}
5540 #endif /* DEBUG */
5541 
5542 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_CREATE, EP_DDI, se_flag);
5543 	if (ev == NULL) {
5544 		goto fail;
5545 	}
5546 
5547 	pathname = kmem_alloc(MAXPATHLEN, kmem_flag);
5548 	if (pathname == NULL) {
5549 		sysevent_free(ev);
5550 		goto fail;
5551 	}
5552 
5553 	(void) ddi_pathname(dip, pathname);
5554 	ASSERT(strlen(pathname));
5555 	se_val.value_type = SE_DATA_TYPE_STRING;
5556 	se_val.value.sv_string = pathname;
5557 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
5558 	    &se_val, se_flag) != 0) {
5559 		kmem_free(pathname, MAXPATHLEN);
5560 		sysevent_free(ev);
5561 		goto fail;
5562 	}
5563 	kmem_free(pathname, MAXPATHLEN);
5564 
5565 	/*
5566 	 * allow for NULL minor names
5567 	 */
5568 	if (minor_name != NULL) {
5569 		se_val.value.sv_string = minor_name;
5570 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
5571 		    &se_val, se_flag) != 0) {
5572 			sysevent_free_attr(ev_attr_list);
5573 			sysevent_free(ev);
5574 			goto fail;
5575 		}
5576 	}
5577 
5578 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
5579 		sysevent_free_attr(ev_attr_list);
5580 		sysevent_free(ev);
5581 		goto fail;
5582 	}
5583 
5584 	if ((se_err = log_sysevent(ev, se_flag, &eid)) != 0) {
5585 		if (se_err == SE_NO_TRANSPORT) {
5586 			cmn_err(CE_WARN, "/devices or /dev may not be current "
5587 			    "for driver %s (%s). Run devfsadm -i %s",
5588 			    ddi_driver_name(dip), "syseventd not responding",
5589 			    ddi_driver_name(dip));
5590 		} else {
5591 			sysevent_free(ev);
5592 			goto fail;
5593 		}
5594 	}
5595 
5596 	sysevent_free(ev);
5597 	return (DDI_SUCCESS);
5598 fail:
5599 	cmn_err(CE_WARN, "/devices or /dev may not be current "
5600 	    "for driver %s. Run devfsadm -i %s",
5601 	    ddi_driver_name(dip), ddi_driver_name(dip));
5602 	return (DDI_SUCCESS);
5603 }
5604 
5605 /*
5606  * failing to remove a minor node is not of interest
5607  * therefore we do not generate an error message
5608  */
5609 static int
5610 i_log_devfs_minor_remove(dev_info_t *dip, char *minor_name)
5611 {
5612 	char *pathname;
5613 	sysevent_t *ev;
5614 	sysevent_id_t eid;
5615 	sysevent_value_t se_val;
5616 	sysevent_attr_list_t *ev_attr_list = NULL;
5617 
5618 	/*
5619 	 * only log ddi_remove_minor_node() calls outside the scope
5620 	 * of attach/detach reconfigurations and when the dip is
5621 	 * still initialized.
5622 	 */
5623 	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip) ||
5624 	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
5625 		return (DDI_SUCCESS);
5626 	}
5627 
5628 	i_ddi_di_cache_invalidate(KM_SLEEP);
5629 
5630 	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_REMOVE, EP_DDI, SE_SLEEP);
5631 	if (ev == NULL) {
5632 		return (DDI_SUCCESS);
5633 	}
5634 
5635 	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
5636 	if (pathname == NULL) {
5637 		sysevent_free(ev);
5638 		return (DDI_SUCCESS);
5639 	}
5640 
5641 	(void) ddi_pathname(dip, pathname);
5642 	ASSERT(strlen(pathname));
5643 	se_val.value_type = SE_DATA_TYPE_STRING;
5644 	se_val.value.sv_string = pathname;
5645 	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
5646 	    &se_val, SE_SLEEP) != 0) {
5647 		kmem_free(pathname, MAXPATHLEN);
5648 		sysevent_free(ev);
5649 		return (DDI_SUCCESS);
5650 	}
5651 
5652 	kmem_free(pathname, MAXPATHLEN);
5653 
5654 	/*
5655 	 * allow for NULL minor names
5656 	 */
5657 	if (minor_name != NULL) {
5658 		se_val.value.sv_string = minor_name;
5659 		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
5660 		    &se_val, SE_SLEEP) != 0) {
5661 			sysevent_free_attr(ev_attr_list);
5662 			goto fail;
5663 		}
5664 	}
5665 
5666 	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
5667 		sysevent_free_attr(ev_attr_list);
5668 	} else {
5669 		(void) log_sysevent(ev, SE_SLEEP, &eid);
5670 	}
5671 fail:
5672 	sysevent_free(ev);
5673 	return (DDI_SUCCESS);
5674 }
5675 
5676 /*
5677  * Derive the device class of the node.
5678  * Device class names aren't defined yet. Until this is done we use
5679  * devfs event subclass names as device class names.
5680  */
5681 static int
5682 derive_devi_class(dev_info_t *dip, char *node_type, int flag)
5683 {
5684 	int rv = DDI_SUCCESS;
5685 
5686 	if (i_ddi_devi_class(dip) == NULL) {
5687 		if (strncmp(node_type, DDI_NT_BLOCK,
5688 		    sizeof (DDI_NT_BLOCK) - 1) == 0 &&
5689 		    (node_type[sizeof (DDI_NT_BLOCK) - 1] == '\0' ||
5690 		    node_type[sizeof (DDI_NT_BLOCK) - 1] == ':') &&
5691 		    strcmp(node_type, DDI_NT_FD) != 0) {
5692 
5693 			rv = i_ddi_set_devi_class(dip, ESC_DISK, flag);
5694 
5695 		} else if (strncmp(node_type, DDI_NT_NET,
5696 		    sizeof (DDI_NT_NET) - 1) == 0 &&
5697 		    (node_type[sizeof (DDI_NT_NET) - 1] == '\0' ||
5698 		    node_type[sizeof (DDI_NT_NET) - 1] == ':')) {
5699 
5700 			rv = i_ddi_set_devi_class(dip, ESC_NETWORK, flag);
5701 		}
5702 	}
5703 
5704 	return (rv);
5705 }
5706 
5707 /*
5708  * Check compliance with PSARC 2003/375:
5709  *
5710  * The name must contain only characters a-z, A-Z, 0-9 or _ and it must not
5711  * exceed IFNAMSIZ (16) characters in length.
5712  */
5713 static boolean_t
5714 verify_name(char *name)
5715 {
5716 	size_t	len = strlen(name);
5717 	char	*cp;
5718 
5719 	if (len == 0 || len > IFNAMSIZ)
5720 		return (B_FALSE);
5721 
5722 	for (cp = name; *cp != '\0'; cp++) {
5723 		if (!isalnum(*cp) && *cp != '_')
5724 			return (B_FALSE);
5725 	}
5726 
5727 	return (B_TRUE);
5728 }
5729 
5730 /*
5731  * ddi_create_minor_common:	Create a  ddi_minor_data structure and
5732  *				attach it to the given devinfo node.
5733  */
5734 
5735 int
5736 ddi_create_minor_common(dev_info_t *dip, char *name, int spec_type,
5737     minor_t minor_num, char *node_type, int flag, ddi_minor_type mtype,
5738     const char *read_priv, const char *write_priv, mode_t priv_mode)
5739 {
5740 	struct ddi_minor_data *dmdp;
5741 	major_t major;
5742 
5743 	if (spec_type != S_IFCHR && spec_type != S_IFBLK)
5744 		return (DDI_FAILURE);
5745 
5746 	if (name == NULL)
5747 		return (DDI_FAILURE);
5748 
5749 	/*
5750 	 * Log a message if the minor number the driver is creating
5751 	 * is not expressible on the on-disk filesystem (currently
5752 	 * this is limited to 18 bits both by UFS). The device can
5753 	 * be opened via devfs, but not by device special files created
5754 	 * via mknod().
5755 	 */
5756 	if (minor_num > L_MAXMIN32) {
5757 		cmn_err(CE_WARN,
5758 		    "%s%d:%s minor 0x%x too big for 32-bit applications",
5759 		    ddi_driver_name(dip), ddi_get_instance(dip),
5760 		    name, minor_num);
5761 		return (DDI_FAILURE);
5762 	}
5763 
5764 	/* dip must be bound and attached */
5765 	major = ddi_driver_major(dip);
5766 	ASSERT(major != (major_t)-1);
5767 
5768 	/*
5769 	 * Default node_type to DDI_PSEUDO and issue notice in debug mode
5770 	 */
5771 	if (node_type == NULL) {
5772 		node_type = DDI_PSEUDO;
5773 		NDI_CONFIG_DEBUG((CE_NOTE, "!illegal node_type NULL for %s%d "
5774 		    " minor node %s; default to DDI_PSEUDO",
5775 		    ddi_driver_name(dip), ddi_get_instance(dip), name));
5776 	}
5777 
5778 	/*
5779 	 * If the driver is a network driver, ensure that the name falls within
5780 	 * the interface naming constraints specified by PSARC/2003/375.
5781 	 */
5782 	if (strcmp(node_type, DDI_NT_NET) == 0) {
5783 		if (!verify_name(name))
5784 			return (DDI_FAILURE);
5785 
5786 		if (mtype == DDM_MINOR) {
5787 			struct devnames *dnp = &devnamesp[major];
5788 
5789 			/* Mark driver as a network driver */
5790 			LOCK_DEV_OPS(&dnp->dn_lock);
5791 			dnp->dn_flags |= DN_NETWORK_DRIVER;
5792 			UNLOCK_DEV_OPS(&dnp->dn_lock);
5793 		}
5794 	}
5795 
5796 	if (mtype == DDM_MINOR) {
5797 		if (derive_devi_class(dip,  node_type, KM_NOSLEEP) !=
5798 		    DDI_SUCCESS)
5799 			return (DDI_FAILURE);
5800 	}
5801 
5802 	/*
5803 	 * Take care of minor number information for the node.
5804 	 */
5805 
5806 	if ((dmdp = kmem_zalloc(sizeof (struct ddi_minor_data),
5807 	    KM_NOSLEEP)) == NULL) {
5808 		return (DDI_FAILURE);
5809 	}
5810 	if ((dmdp->ddm_name = i_ddi_strdup(name, KM_NOSLEEP)) == NULL) {
5811 		kmem_free(dmdp, sizeof (struct ddi_minor_data));
5812 		return (DDI_FAILURE);
5813 	}
5814 	dmdp->dip = dip;
5815 	dmdp->ddm_dev = makedevice(major, minor_num);
5816 	dmdp->ddm_spec_type = spec_type;
5817 	dmdp->ddm_node_type = node_type;
5818 	dmdp->type = mtype;
5819 	if (flag & CLONE_DEV) {
5820 		dmdp->type = DDM_ALIAS;
5821 		dmdp->ddm_dev = makedevice(ddi_driver_major(clone_dip), major);
5822 	}
5823 	if (flag & PRIVONLY_DEV) {
5824 		dmdp->ddm_flags |= DM_NO_FSPERM;
5825 	}
5826 	if (read_priv || write_priv) {
5827 		dmdp->ddm_node_priv =
5828 		    devpolicy_priv_by_name(read_priv, write_priv);
5829 	}
5830 	dmdp->ddm_priv_mode = priv_mode;
5831 
5832 	ddi_append_minor_node(dip, dmdp);
5833 
5834 	/*
5835 	 * only log ddi_create_minor_node() calls which occur
5836 	 * outside the scope of attach(9e)/detach(9e) reconfigurations
5837 	 */
5838 	if (!(DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) &&
5839 	    mtype != DDM_INTERNAL_PATH) {
5840 		(void) i_log_devfs_minor_create(dip, name);
5841 	}
5842 
5843 	/*
5844 	 * Check if any dacf rules match the creation of this minor node
5845 	 */
5846 	dacfc_match_create_minor(name, node_type, dip, dmdp, flag);
5847 	return (DDI_SUCCESS);
5848 }
5849 
5850 int
5851 ddi_create_minor_node(dev_info_t *dip, char *name, int spec_type,
5852     minor_t minor_num, char *node_type, int flag)
5853 {
5854 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
5855 	    node_type, flag, DDM_MINOR, NULL, NULL, 0));
5856 }
5857 
5858 int
5859 ddi_create_priv_minor_node(dev_info_t *dip, char *name, int spec_type,
5860     minor_t minor_num, char *node_type, int flag,
5861     const char *rdpriv, const char *wrpriv, mode_t priv_mode)
5862 {
5863 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
5864 	    node_type, flag, DDM_MINOR, rdpriv, wrpriv, priv_mode));
5865 }
5866 
5867 int
5868 ddi_create_default_minor_node(dev_info_t *dip, char *name, int spec_type,
5869     minor_t minor_num, char *node_type, int flag)
5870 {
5871 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
5872 	    node_type, flag, DDM_DEFAULT, NULL, NULL, 0));
5873 }
5874 
5875 /*
5876  * Internal (non-ddi) routine for drivers to export names known
5877  * to the kernel (especially ddi_pathname_to_dev_t and friends)
5878  * but not exported externally to /dev
5879  */
5880 int
5881 ddi_create_internal_pathname(dev_info_t *dip, char *name, int spec_type,
5882     minor_t minor_num)
5883 {
5884 	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
5885 	    "internal", 0, DDM_INTERNAL_PATH, NULL, NULL, 0));
5886 }
5887 
5888 void
5889 ddi_remove_minor_node(dev_info_t *dip, char *name)
5890 {
5891 	struct ddi_minor_data *dmdp, *dmdp1;
5892 	struct ddi_minor_data **dmdp_prev;
5893 
5894 	mutex_enter(&(DEVI(dip)->devi_lock));
5895 	i_devi_enter(dip, DEVI_S_MD_UPDATE, DEVI_S_MD_UPDATE, 1);
5896 
5897 	dmdp_prev = &DEVI(dip)->devi_minor;
5898 	dmdp = DEVI(dip)->devi_minor;
5899 	while (dmdp != NULL) {
5900 		dmdp1 = dmdp->next;
5901 		if ((name == NULL || (dmdp->ddm_name != NULL &&
5902 		    strcmp(name, dmdp->ddm_name) == 0))) {
5903 			if (dmdp->ddm_name != NULL) {
5904 				if (dmdp->type != DDM_INTERNAL_PATH)
5905 					(void) i_log_devfs_minor_remove(dip,
5906 					    dmdp->ddm_name);
5907 				kmem_free(dmdp->ddm_name,
5908 				    strlen(dmdp->ddm_name) + 1);
5909 			}
5910 			/*
5911 			 * Release device privilege, if any.
5912 			 * Release dacf client data associated with this minor
5913 			 * node by storing NULL.
5914 			 */
5915 			if (dmdp->ddm_node_priv)
5916 				dpfree(dmdp->ddm_node_priv);
5917 			dacf_store_info((dacf_infohdl_t)dmdp, NULL);
5918 			kmem_free(dmdp, sizeof (struct ddi_minor_data));
5919 			*dmdp_prev = dmdp1;
5920 			/*
5921 			 * OK, we found it, so get out now -- if we drive on,
5922 			 * we will strcmp against garbage.  See 1139209.
5923 			 */
5924 			if (name != NULL)
5925 				break;
5926 		} else {
5927 			dmdp_prev = &dmdp->next;
5928 		}
5929 		dmdp = dmdp1;
5930 	}
5931 
5932 	i_devi_exit(dip, DEVI_S_MD_UPDATE, 1);
5933 	mutex_exit(&(DEVI(dip)->devi_lock));
5934 }
5935 
5936 
5937 int
5938 ddi_in_panic()
5939 {
5940 	return (panicstr != NULL);
5941 }
5942 
5943 
5944 /*
5945  * Find first bit set in a mask (returned counting from 1 up)
5946  */
5947 
5948 int
5949 ddi_ffs(long mask)
5950 {
5951 	extern int ffs(long mask);
5952 	return (ffs(mask));
5953 }
5954 
5955 /*
5956  * Find last bit set. Take mask and clear
5957  * all but the most significant bit, and
5958  * then let ffs do the rest of the work.
5959  *
5960  * Algorithm courtesy of Steve Chessin.
5961  */
5962 
5963 int
5964 ddi_fls(long mask)
5965 {
5966 	extern int ffs(long);
5967 
5968 	while (mask) {
5969 		long nx;
5970 
5971 		if ((nx = (mask & (mask - 1))) == 0)
5972 			break;
5973 		mask = nx;
5974 	}
5975 	return (ffs(mask));
5976 }
5977 
5978 /*
5979  * The next five routines comprise generic storage management utilities
5980  * for driver soft state structures (in "the old days," this was done
5981  * with a statically sized array - big systems and dynamic loading
5982  * and unloading make heap allocation more attractive)
5983  */
5984 
5985 /*
5986  * Allocate a set of pointers to 'n_items' objects of size 'size'
5987  * bytes.  Each pointer is initialized to nil.
5988  *
5989  * The 'size' and 'n_items' values are stashed in the opaque
5990  * handle returned to the caller.
5991  *
5992  * This implementation interprets 'set of pointers' to mean 'array
5993  * of pointers' but note that nothing in the interface definition
5994  * precludes an implementation that uses, for example, a linked list.
5995  * However there should be a small efficiency gain from using an array
5996  * at lookup time.
5997  *
5998  * NOTE	As an optimization, we make our growable array allocations in
5999  *	powers of two (bytes), since that's how much kmem_alloc (currently)
6000  *	gives us anyway.  It should save us some free/realloc's ..
6001  *
6002  *	As a further optimization, we make the growable array start out
6003  *	with MIN_N_ITEMS in it.
6004  */
6005 
6006 #define	MIN_N_ITEMS	8	/* 8 void *'s == 32 bytes */
6007 
6008 int
6009 ddi_soft_state_init(void **state_p, size_t size, size_t n_items)
6010 {
6011 	struct i_ddi_soft_state *ss;
6012 
6013 	if (state_p == NULL || *state_p != NULL || size == 0)
6014 		return (EINVAL);
6015 
6016 	ss = kmem_zalloc(sizeof (*ss), KM_SLEEP);
6017 	mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL);
6018 	ss->size = size;
6019 
6020 	if (n_items < MIN_N_ITEMS)
6021 		ss->n_items = MIN_N_ITEMS;
6022 	else {
6023 		int bitlog;
6024 
6025 		if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items))
6026 			bitlog--;
6027 		ss->n_items = 1 << bitlog;
6028 	}
6029 
6030 	ASSERT(ss->n_items >= n_items);
6031 
6032 	ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP);
6033 
6034 	*state_p = ss;
6035 
6036 	return (0);
6037 }
6038 
6039 
6040 /*
6041  * Allocate a state structure of size 'size' to be associated
6042  * with item 'item'.
6043  *
6044  * In this implementation, the array is extended to
6045  * allow the requested offset, if needed.
6046  */
6047 int
6048 ddi_soft_state_zalloc(void *state, int item)
6049 {
6050 	struct i_ddi_soft_state *ss;
6051 	void **array;
6052 	void *new_element;
6053 
6054 	if ((ss = state) == NULL || item < 0)
6055 		return (DDI_FAILURE);
6056 
6057 	mutex_enter(&ss->lock);
6058 	if (ss->size == 0) {
6059 		mutex_exit(&ss->lock);
6060 		cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle: %s",
6061 		    mod_containing_pc(caller()));
6062 		return (DDI_FAILURE);
6063 	}
6064 
6065 	array = ss->array;	/* NULL if ss->n_items == 0 */
6066 	ASSERT(ss->n_items != 0 && array != NULL);
6067 
6068 	/*
6069 	 * refuse to tread on an existing element
6070 	 */
6071 	if (item < ss->n_items && array[item] != NULL) {
6072 		mutex_exit(&ss->lock);
6073 		return (DDI_FAILURE);
6074 	}
6075 
6076 	/*
6077 	 * Allocate a new element to plug in
6078 	 */
6079 	new_element = kmem_zalloc(ss->size, KM_SLEEP);
6080 
6081 	/*
6082 	 * Check if the array is big enough, if not, grow it.
6083 	 */
6084 	if (item >= ss->n_items) {
6085 		void	**new_array;
6086 		size_t	new_n_items;
6087 		struct i_ddi_soft_state *dirty;
6088 
6089 		/*
6090 		 * Allocate a new array of the right length, copy
6091 		 * all the old pointers to the new array, then
6092 		 * if it exists at all, put the old array on the
6093 		 * dirty list.
6094 		 *
6095 		 * Note that we can't kmem_free() the old array.
6096 		 *
6097 		 * Why -- well the 'get' operation is 'mutex-free', so we
6098 		 * can't easily catch a suspended thread that is just about
6099 		 * to dereference the array we just grew out of.  So we
6100 		 * cons up a header and put it on a list of 'dirty'
6101 		 * pointer arrays.  (Dirty in the sense that there may
6102 		 * be suspended threads somewhere that are in the middle
6103 		 * of referencing them).  Fortunately, we -can- garbage
6104 		 * collect it all at ddi_soft_state_fini time.
6105 		 */
6106 		new_n_items = ss->n_items;
6107 		while (new_n_items < (1 + item))
6108 			new_n_items <<= 1;	/* double array size .. */
6109 
6110 		ASSERT(new_n_items >= (1 + item));	/* sanity check! */
6111 
6112 		new_array = kmem_zalloc(new_n_items * sizeof (void *),
6113 		    KM_SLEEP);
6114 		/*
6115 		 * Copy the pointers into the new array
6116 		 */
6117 		bcopy(array, new_array, ss->n_items * sizeof (void *));
6118 
6119 		/*
6120 		 * Save the old array on the dirty list
6121 		 */
6122 		dirty = kmem_zalloc(sizeof (*dirty), KM_SLEEP);
6123 		dirty->array = ss->array;
6124 		dirty->n_items = ss->n_items;
6125 		dirty->next = ss->next;
6126 		ss->next = dirty;
6127 
6128 		ss->array = (array = new_array);
6129 		ss->n_items = new_n_items;
6130 	}
6131 
6132 	ASSERT(array != NULL && item < ss->n_items && array[item] == NULL);
6133 
6134 	array[item] = new_element;
6135 
6136 	mutex_exit(&ss->lock);
6137 	return (DDI_SUCCESS);
6138 }
6139 
6140 
6141 /*
6142  * Fetch a pointer to the allocated soft state structure.
6143  *
6144  * This is designed to be cheap.
6145  *
6146  * There's an argument that there should be more checking for
6147  * nil pointers and out of bounds on the array.. but we do a lot
6148  * of that in the alloc/free routines.
6149  *
6150  * An array has the convenience that we don't need to lock read-access
6151  * to it c.f. a linked list.  However our "expanding array" strategy
6152  * means that we should hold a readers lock on the i_ddi_soft_state
6153  * structure.
6154  *
6155  * However, from a performance viewpoint, we need to do it without
6156  * any locks at all -- this also makes it a leaf routine.  The algorithm
6157  * is 'lock-free' because we only discard the pointer arrays at
6158  * ddi_soft_state_fini() time.
6159  */
6160 void *
6161 ddi_get_soft_state(void *state, int item)
6162 {
6163 	struct i_ddi_soft_state *ss = state;
6164 
6165 	ASSERT(ss != NULL && item >= 0);
6166 
6167 	if (item < ss->n_items && ss->array != NULL)
6168 		return (ss->array[item]);
6169 	return (NULL);
6170 }
6171 
6172 /*
6173  * Free the state structure corresponding to 'item.'   Freeing an
6174  * element that has either gone or was never allocated is not
6175  * considered an error.  Note that we free the state structure, but
6176  * we don't shrink our pointer array, or discard 'dirty' arrays,
6177  * since even a few pointers don't really waste too much memory.
6178  *
6179  * Passing an item number that is out of bounds, or a null pointer will
6180  * provoke an error message.
6181  */
6182 void
6183 ddi_soft_state_free(void *state, int item)
6184 {
6185 	struct i_ddi_soft_state *ss;
6186 	void **array;
6187 	void *element;
6188 	static char msg[] = "ddi_soft_state_free:";
6189 
6190 	if ((ss = state) == NULL) {
6191 		cmn_err(CE_WARN, "%s null handle: %s",
6192 		    msg, mod_containing_pc(caller()));
6193 		return;
6194 	}
6195 
6196 	element = NULL;
6197 
6198 	mutex_enter(&ss->lock);
6199 
6200 	if ((array = ss->array) == NULL || ss->size == 0) {
6201 		cmn_err(CE_WARN, "%s bad handle: %s",
6202 		    msg, mod_containing_pc(caller()));
6203 	} else if (item < 0 || item >= ss->n_items) {
6204 		cmn_err(CE_WARN, "%s item %d not in range [0..%lu]: %s",
6205 		    msg, item, ss->n_items - 1, mod_containing_pc(caller()));
6206 	} else if (array[item] != NULL) {
6207 		element = array[item];
6208 		array[item] = NULL;
6209 	}
6210 
6211 	mutex_exit(&ss->lock);
6212 
6213 	if (element)
6214 		kmem_free(element, ss->size);
6215 }
6216 
6217 
6218 /*
6219  * Free the entire set of pointers, and any
6220  * soft state structures contained therein.
6221  *
6222  * Note that we don't grab the ss->lock mutex, even though
6223  * we're inspecting the various fields of the data structure.
6224  *
6225  * There is an implicit assumption that this routine will
6226  * never run concurrently with any of the above on this
6227  * particular state structure i.e. by the time the driver
6228  * calls this routine, there should be no other threads
6229  * running in the driver.
6230  */
6231 void
6232 ddi_soft_state_fini(void **state_p)
6233 {
6234 	struct i_ddi_soft_state *ss, *dirty;
6235 	int item;
6236 	static char msg[] = "ddi_soft_state_fini:";
6237 
6238 	if (state_p == NULL || (ss = *state_p) == NULL) {
6239 		cmn_err(CE_WARN, "%s null handle: %s",
6240 		    msg, mod_containing_pc(caller()));
6241 		return;
6242 	}
6243 
6244 	if (ss->size == 0) {
6245 		cmn_err(CE_WARN, "%s bad handle: %s",
6246 		    msg, mod_containing_pc(caller()));
6247 		return;
6248 	}
6249 
6250 	if (ss->n_items > 0) {
6251 		for (item = 0; item < ss->n_items; item++)
6252 			ddi_soft_state_free(ss, item);
6253 		kmem_free(ss->array, ss->n_items * sizeof (void *));
6254 	}
6255 
6256 	/*
6257 	 * Now delete any dirty arrays from previous 'grow' operations
6258 	 */
6259 	for (dirty = ss->next; dirty; dirty = ss->next) {
6260 		ss->next = dirty->next;
6261 		kmem_free(dirty->array, dirty->n_items * sizeof (void *));
6262 		kmem_free(dirty, sizeof (*dirty));
6263 	}
6264 
6265 	mutex_destroy(&ss->lock);
6266 	kmem_free(ss, sizeof (*ss));
6267 
6268 	*state_p = NULL;
6269 }
6270 
6271 /*
6272  * This sets the devi_addr entry in the dev_info structure 'dip' to 'name'.
6273  * Storage is double buffered to prevent updates during devi_addr use -
6274  * double buffering is adaquate for reliable ddi_deviname() consumption.
6275  * The double buffer is not freed until dev_info structure destruction
6276  * (by i_ddi_free_node).
6277  */
6278 void
6279 ddi_set_name_addr(dev_info_t *dip, char *name)
6280 {
6281 	char	*buf = DEVI(dip)->devi_addr_buf;
6282 	char	*newaddr;
6283 
6284 	if (buf == NULL) {
6285 		buf = kmem_zalloc(2 * MAXNAMELEN, KM_SLEEP);
6286 		DEVI(dip)->devi_addr_buf = buf;
6287 	}
6288 
6289 	if (name) {
6290 		ASSERT(strlen(name) < MAXNAMELEN);
6291 		newaddr = (DEVI(dip)->devi_addr == buf) ?
6292 		    (buf + MAXNAMELEN) : buf;
6293 		(void) strlcpy(newaddr, name, MAXNAMELEN);
6294 	} else
6295 		newaddr = NULL;
6296 
6297 	DEVI(dip)->devi_addr = newaddr;
6298 }
6299 
6300 char *
6301 ddi_get_name_addr(dev_info_t *dip)
6302 {
6303 	return (DEVI(dip)->devi_addr);
6304 }
6305 
6306 void
6307 ddi_set_parent_data(dev_info_t *dip, void *pd)
6308 {
6309 	DEVI(dip)->devi_parent_data = pd;
6310 }
6311 
6312 void *
6313 ddi_get_parent_data(dev_info_t *dip)
6314 {
6315 	return (DEVI(dip)->devi_parent_data);
6316 }
6317 
6318 /*
6319  * ddi_name_to_major: Returns the major number of a module given its name.
6320  */
6321 major_t
6322 ddi_name_to_major(char *name)
6323 {
6324 	return (mod_name_to_major(name));
6325 }
6326 
6327 /*
6328  * ddi_major_to_name: Returns the module name bound to a major number.
6329  */
6330 char *
6331 ddi_major_to_name(major_t major)
6332 {
6333 	return (mod_major_to_name(major));
6334 }
6335 
6336 /*
6337  * Return the name of the devinfo node pointed at by 'dip' in the buffer
6338  * pointed at by 'name.'  A devinfo node is named as a result of calling
6339  * ddi_initchild().
6340  *
6341  * Note: the driver must be held before calling this function!
6342  */
6343 char *
6344 ddi_deviname(dev_info_t *dip, char *name)
6345 {
6346 	char *addrname;
6347 	char none = '\0';
6348 
6349 	if (dip == ddi_root_node()) {
6350 		*name = '\0';
6351 		return (name);
6352 	}
6353 
6354 	if (i_ddi_node_state(dip) < DS_INITIALIZED) {
6355 		addrname = &none;
6356 	} else {
6357 		addrname = ddi_get_name_addr(dip);
6358 	}
6359 
6360 	if (*addrname == '\0') {
6361 		(void) sprintf(name, "/%s", ddi_node_name(dip));
6362 	} else {
6363 		(void) sprintf(name, "/%s@%s", ddi_node_name(dip), addrname);
6364 	}
6365 
6366 	return (name);
6367 }
6368 
6369 /*
6370  * Spits out the name of device node, typically name@addr, for a given node,
6371  * using the driver name, not the nodename.
6372  *
6373  * Used by match_parent. Not to be used elsewhere.
6374  */
6375 char *
6376 i_ddi_parname(dev_info_t *dip, char *name)
6377 {
6378 	char *addrname;
6379 
6380 	if (dip == ddi_root_node()) {
6381 		*name = '\0';
6382 		return (name);
6383 	}
6384 
6385 	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
6386 
6387 	if (*(addrname = ddi_get_name_addr(dip)) == '\0')
6388 		(void) sprintf(name, "%s", ddi_binding_name(dip));
6389 	else
6390 		(void) sprintf(name, "%s@%s", ddi_binding_name(dip), addrname);
6391 	return (name);
6392 }
6393 
6394 static char *
6395 pathname_work(dev_info_t *dip, char *path)
6396 {
6397 	char *bp;
6398 
6399 	if (dip == ddi_root_node()) {
6400 		*path = '\0';
6401 		return (path);
6402 	}
6403 	(void) pathname_work(ddi_get_parent(dip), path);
6404 	bp = path + strlen(path);
6405 	(void) ddi_deviname(dip, bp);
6406 	return (path);
6407 }
6408 
6409 char *
6410 ddi_pathname(dev_info_t *dip, char *path)
6411 {
6412 	return (pathname_work(dip, path));
6413 }
6414 
6415 /*
6416  * Given a dev_t, return the pathname of the corresponding device in the
6417  * buffer pointed at by "path."  The buffer is assumed to be large enough
6418  * to hold the pathname of the device (MAXPATHLEN).
6419  *
6420  * The pathname of a device is the pathname of the devinfo node to which
6421  * the device "belongs," concatenated with the character ':' and the name
6422  * of the minor node corresponding to the dev_t.  If spec_type is 0 then
6423  * just the pathname of the devinfo node is returned without driving attach
6424  * of that node.  For a non-zero spec_type, an attach is performed and a
6425  * search of the minor list occurs.
6426  *
6427  * It is possible that the path associated with the dev_t is not
6428  * currently available in the devinfo tree.  In order to have a
6429  * dev_t, a device must have been discovered before, which means
6430  * that the path is always in the instance tree.  The one exception
6431  * to this is if the dev_t is associated with a pseudo driver, in
6432  * which case the device must exist on the pseudo branch of the
6433  * devinfo tree as a result of parsing .conf files.
6434  */
6435 int
6436 ddi_dev_pathname(dev_t devt, int spec_type, char *path)
6437 {
6438 	major_t		major = getmajor(devt);
6439 	int		instance;
6440 	dev_info_t	*dip;
6441 	char		*minorname;
6442 	char		*drvname;
6443 
6444 	if (major >= devcnt)
6445 		goto fail;
6446 	if (major == clone_major) {
6447 		/* clone has no minor nodes, manufacture the path here */
6448 		if ((drvname = ddi_major_to_name(getminor(devt))) == NULL)
6449 			goto fail;
6450 
6451 		(void) snprintf(path, MAXPATHLEN, "%s:%s", CLONE_PATH, drvname);
6452 		return (DDI_SUCCESS);
6453 	}
6454 
6455 	/* extract instance from devt (getinfo(9E) DDI_INFO_DEVT2INSTANCE). */
6456 	if ((instance = dev_to_instance(devt)) == -1)
6457 		goto fail;
6458 
6459 	/* reconstruct the path given the major/instance */
6460 	if (e_ddi_majorinstance_to_path(major, instance, path) != DDI_SUCCESS)
6461 		goto fail;
6462 
6463 	/* if spec_type given we must drive attach and search minor nodes */
6464 	if ((spec_type == S_IFCHR) || (spec_type == S_IFBLK)) {
6465 		/* attach the path so we can search minors */
6466 		if ((dip = e_ddi_hold_devi_by_path(path, 0)) == NULL)
6467 			goto fail;
6468 
6469 		/* Add minorname to path. */
6470 		mutex_enter(&(DEVI(dip)->devi_lock));
6471 		minorname = i_ddi_devtspectype_to_minorname(dip,
6472 		    devt, spec_type);
6473 		if (minorname) {
6474 			(void) strcat(path, ":");
6475 			(void) strcat(path, minorname);
6476 		}
6477 		mutex_exit(&(DEVI(dip)->devi_lock));
6478 		ddi_release_devi(dip);
6479 		if (minorname == NULL)
6480 			goto fail;
6481 	}
6482 	ASSERT(strlen(path) < MAXPATHLEN);
6483 	return (DDI_SUCCESS);
6484 
6485 fail:	*path = 0;
6486 	return (DDI_FAILURE);
6487 }
6488 
6489 /*
6490  * Given a major number and an instance, return the path.
6491  * This interface does NOT drive attach.
6492  */
6493 int
6494 e_ddi_majorinstance_to_path(major_t major, int instance, char *path)
6495 {
6496 	struct devnames *dnp;
6497 	dev_info_t	*dip;
6498 
6499 	if ((major >= devcnt) || (instance == -1)) {
6500 		*path = 0;
6501 		return (DDI_FAILURE);
6502 	}
6503 
6504 	/* look for the major/instance in the instance tree */
6505 	if (e_ddi_instance_majorinstance_to_path(major, instance,
6506 	    path) == DDI_SUCCESS) {
6507 		ASSERT(strlen(path) < MAXPATHLEN);
6508 		return (DDI_SUCCESS);
6509 	}
6510 
6511 	/*
6512 	 * Not in instance tree, find the instance on the per driver list and
6513 	 * construct path to instance via ddi_pathname(). This is how paths
6514 	 * down the 'pseudo' branch are constructed.
6515 	 */
6516 	dnp = &(devnamesp[major]);
6517 	LOCK_DEV_OPS(&(dnp->dn_lock));
6518 	for (dip = dnp->dn_head; dip;
6519 	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
6520 		/* Skip if instance does not match. */
6521 		if (DEVI(dip)->devi_instance != instance)
6522 			continue;
6523 
6524 		/*
6525 		 * An ndi_hold_devi() does not prevent DS_INITIALIZED->DS_BOUND
6526 		 * node demotion, so it is not an effective way of ensuring
6527 		 * that the ddi_pathname result has a unit-address.  Instead,
6528 		 * we reverify the node state after calling ddi_pathname().
6529 		 */
6530 		if (i_ddi_node_state(dip) >= DS_INITIALIZED) {
6531 			(void) ddi_pathname(dip, path);
6532 			if (i_ddi_node_state(dip) < DS_INITIALIZED)
6533 				continue;
6534 			UNLOCK_DEV_OPS(&(dnp->dn_lock));
6535 			ASSERT(strlen(path) < MAXPATHLEN);
6536 			return (DDI_SUCCESS);
6537 		}
6538 	}
6539 	UNLOCK_DEV_OPS(&(dnp->dn_lock));
6540 
6541 	/* can't reconstruct the path */
6542 	*path = 0;
6543 	return (DDI_FAILURE);
6544 }
6545 
6546 #define	GLD_DRIVER_PPA "SUNW,gld_v0_ppa"
6547 
6548 /*
6549  * Given the dip for a network interface return the ppa for that interface.
6550  *
6551  * In all cases except GLD v0 drivers, the ppa == instance.
6552  * In the case of GLD v0 drivers, the ppa is equal to the attach order.
6553  * So for these drivers when the attach routine calls gld_register(),
6554  * the GLD framework creates an integer property called "gld_driver_ppa"
6555  * that can be queried here.
6556  *
6557  * The only time this function is used is when a system is booting over nfs.
6558  * In this case the system has to resolve the pathname of the boot device
6559  * to it's ppa.
6560  */
6561 int
6562 i_ddi_devi_get_ppa(dev_info_t *dip)
6563 {
6564 	return (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
6565 			DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
6566 			GLD_DRIVER_PPA, ddi_get_instance(dip)));
6567 }
6568 
6569 /*
6570  * i_ddi_devi_set_ppa() should only be called from gld_register()
6571  * and only for GLD v0 drivers
6572  */
6573 void
6574 i_ddi_devi_set_ppa(dev_info_t *dip, int ppa)
6575 {
6576 	(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip, GLD_DRIVER_PPA, ppa);
6577 }
6578 
6579 
6580 /*
6581  * Private DDI Console bell functions.
6582  */
6583 void
6584 ddi_ring_console_bell(clock_t duration)
6585 {
6586 	if (ddi_console_bell_func != NULL)
6587 		(*ddi_console_bell_func)(duration);
6588 }
6589 
6590 void
6591 ddi_set_console_bell(void (*bellfunc)(clock_t duration))
6592 {
6593 	ddi_console_bell_func = bellfunc;
6594 }
6595 
6596 int
6597 ddi_dma_alloc_handle(dev_info_t *dip, ddi_dma_attr_t *attr,
6598 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
6599 {
6600 	int (*funcp)() = ddi_dma_allochdl;
6601 	ddi_dma_attr_t dma_attr;
6602 	struct bus_ops *bop;
6603 
6604 	if (attr == (ddi_dma_attr_t *)0)
6605 		return (DDI_DMA_BADATTR);
6606 
6607 	dma_attr = *attr;
6608 
6609 	bop = DEVI(dip)->devi_ops->devo_bus_ops;
6610 	if (bop && bop->bus_dma_allochdl)
6611 		funcp = bop->bus_dma_allochdl;
6612 
6613 	return ((*funcp)(dip, dip, &dma_attr, waitfp, arg, handlep));
6614 }
6615 
6616 void
6617 ddi_dma_free_handle(ddi_dma_handle_t *handlep)
6618 {
6619 	ddi_dma_handle_t h = *handlep;
6620 	(void) ddi_dma_freehdl(HD, HD, h);
6621 }
6622 
6623 static uintptr_t dma_mem_list_id = 0;
6624 
6625 
6626 int
6627 ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
6628 	ddi_device_acc_attr_t *accattrp, uint_t flags,
6629 	int (*waitfp)(caddr_t), caddr_t arg, caddr_t *kaddrp,
6630 	size_t *real_length, ddi_acc_handle_t *handlep)
6631 {
6632 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6633 	dev_info_t *dip = hp->dmai_rdip;
6634 	ddi_acc_hdl_t *ap;
6635 	ddi_dma_attr_t *attrp = &hp->dmai_attr;
6636 	uint_t sleepflag, xfermodes;
6637 	int (*fp)(caddr_t);
6638 	int rval;
6639 
6640 	if (waitfp == DDI_DMA_SLEEP)
6641 		fp = (int (*)())KM_SLEEP;
6642 	else if (waitfp == DDI_DMA_DONTWAIT)
6643 		fp = (int (*)())KM_NOSLEEP;
6644 	else
6645 		fp = waitfp;
6646 	*handlep = impl_acc_hdl_alloc(fp, arg);
6647 	if (*handlep == NULL)
6648 		return (DDI_FAILURE);
6649 
6650 	/* check if the cache attributes are supported */
6651 	if (i_ddi_check_cache_attr(flags) == B_FALSE)
6652 		return (DDI_FAILURE);
6653 
6654 	/*
6655 	 * Transfer the meaningful bits to xfermodes.
6656 	 * Double-check if the 3rd party driver correctly sets the bits.
6657 	 * If not, set DDI_DMA_STREAMING to keep compatibility.
6658 	 */
6659 	xfermodes = flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING);
6660 	if (xfermodes == 0) {
6661 		xfermodes = DDI_DMA_STREAMING;
6662 	}
6663 
6664 	/*
6665 	 * initialize the common elements of data access handle
6666 	 */
6667 	ap = impl_acc_hdl_get(*handlep);
6668 	ap->ah_vers = VERS_ACCHDL;
6669 	ap->ah_dip = dip;
6670 	ap->ah_offset = 0;
6671 	ap->ah_len = 0;
6672 	ap->ah_xfermodes = flags;
6673 	ap->ah_acc = *accattrp;
6674 
6675 	sleepflag = ((waitfp == DDI_DMA_SLEEP) ? 1 : 0);
6676 	if (xfermodes == DDI_DMA_CONSISTENT) {
6677 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
6678 		    flags, accattrp, kaddrp, NULL, ap);
6679 		*real_length = length;
6680 	} else {
6681 		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
6682 		    flags, accattrp, kaddrp, real_length, ap);
6683 	}
6684 	if (rval == DDI_SUCCESS) {
6685 		ap->ah_len = (off_t)(*real_length);
6686 		ap->ah_addr = *kaddrp;
6687 	} else {
6688 		impl_acc_hdl_free(*handlep);
6689 		*handlep = (ddi_acc_handle_t)NULL;
6690 		if (waitfp != DDI_DMA_SLEEP && waitfp != DDI_DMA_DONTWAIT) {
6691 			ddi_set_callback(waitfp, arg, &dma_mem_list_id);
6692 		}
6693 		rval = DDI_FAILURE;
6694 	}
6695 	return (rval);
6696 }
6697 
6698 void
6699 ddi_dma_mem_free(ddi_acc_handle_t *handlep)
6700 {
6701 	ddi_acc_hdl_t *ap;
6702 
6703 	ap = impl_acc_hdl_get(*handlep);
6704 	ASSERT(ap);
6705 
6706 	i_ddi_mem_free((caddr_t)ap->ah_addr, ap);
6707 
6708 	/*
6709 	 * free the handle
6710 	 */
6711 	impl_acc_hdl_free(*handlep);
6712 	*handlep = (ddi_acc_handle_t)NULL;
6713 
6714 	if (dma_mem_list_id != 0) {
6715 		ddi_run_callback(&dma_mem_list_id);
6716 	}
6717 }
6718 
6719 int
6720 ddi_dma_buf_bind_handle(ddi_dma_handle_t handle, struct buf *bp,
6721 	uint_t flags, int (*waitfp)(caddr_t), caddr_t arg,
6722 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
6723 {
6724 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6725 	dev_info_t *hdip, *dip;
6726 	struct ddi_dma_req dmareq;
6727 	int (*funcp)();
6728 
6729 	dmareq.dmar_flags = flags;
6730 	dmareq.dmar_fp = waitfp;
6731 	dmareq.dmar_arg = arg;
6732 	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;
6733 
6734 	if (bp->b_flags & B_PAGEIO) {
6735 		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
6736 		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
6737 		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
6738 		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
6739 	} else {
6740 		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
6741 		if (bp->b_flags & B_SHADOW) {
6742 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
6743 							bp->b_shadow;
6744 			dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
6745 		} else {
6746 			dmareq.dmar_object.dmao_type =
6747 				(bp->b_flags & (B_PHYS | B_REMAPPED)) ?
6748 				DMA_OTYP_BUFVADDR : DMA_OTYP_VADDR;
6749 			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
6750 		}
6751 
6752 		/*
6753 		 * If the buffer has no proc pointer, or the proc
6754 		 * struct has the kernel address space, or the buffer has
6755 		 * been marked B_REMAPPED (meaning that it is now
6756 		 * mapped into the kernel's address space), then
6757 		 * the address space is kas (kernel address space).
6758 		 */
6759 		if ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas) ||
6760 		    (bp->b_flags & B_REMAPPED)) {
6761 			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
6762 		} else {
6763 			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
6764 			    bp->b_proc->p_as;
6765 		}
6766 	}
6767 
6768 	dip = hp->dmai_rdip;
6769 	hdip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
6770 	funcp = DEVI(dip)->devi_bus_dma_bindfunc;
6771 	return ((*funcp)(hdip, dip, handle, &dmareq, cookiep, ccountp));
6772 }
6773 
6774 int
6775 ddi_dma_addr_bind_handle(ddi_dma_handle_t handle, struct as *as,
6776 	caddr_t addr, size_t len, uint_t flags, int (*waitfp)(caddr_t),
6777 	caddr_t arg, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
6778 {
6779 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6780 	dev_info_t *hdip, *dip;
6781 	struct ddi_dma_req dmareq;
6782 	int (*funcp)();
6783 
6784 	if (len == (uint_t)0) {
6785 		return (DDI_DMA_NOMAPPING);
6786 	}
6787 	dmareq.dmar_flags = flags;
6788 	dmareq.dmar_fp = waitfp;
6789 	dmareq.dmar_arg = arg;
6790 	dmareq.dmar_object.dmao_size = len;
6791 	dmareq.dmar_object.dmao_type = DMA_OTYP_VADDR;
6792 	dmareq.dmar_object.dmao_obj.virt_obj.v_as = as;
6793 	dmareq.dmar_object.dmao_obj.virt_obj.v_addr = addr;
6794 	dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
6795 
6796 	dip = hp->dmai_rdip;
6797 	hdip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
6798 	funcp = DEVI(dip)->devi_bus_dma_bindfunc;
6799 	return ((*funcp)(hdip, dip, handle, &dmareq, cookiep, ccountp));
6800 }
6801 
6802 void
6803 ddi_dma_nextcookie(ddi_dma_handle_t handle, ddi_dma_cookie_t *cookiep)
6804 {
6805 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6806 	ddi_dma_cookie_t *cp;
6807 
6808 	cp = hp->dmai_cookie;
6809 	ASSERT(cp);
6810 
6811 	cookiep->dmac_notused = cp->dmac_notused;
6812 	cookiep->dmac_type = cp->dmac_type;
6813 	cookiep->dmac_address = cp->dmac_address;
6814 	cookiep->dmac_size = cp->dmac_size;
6815 	hp->dmai_cookie++;
6816 }
6817 
6818 int
6819 ddi_dma_numwin(ddi_dma_handle_t handle, uint_t *nwinp)
6820 {
6821 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6822 	if ((hp->dmai_rflags & DDI_DMA_PARTIAL) == 0) {
6823 		return (DDI_FAILURE);
6824 	} else {
6825 		*nwinp = hp->dmai_nwin;
6826 		return (DDI_SUCCESS);
6827 	}
6828 }
6829 
6830 int
6831 ddi_dma_getwin(ddi_dma_handle_t h, uint_t win, off_t *offp,
6832 	size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
6833 {
6834 	int (*funcp)() = ddi_dma_win;
6835 	struct bus_ops *bop;
6836 
6837 	bop = DEVI(HD)->devi_ops->devo_bus_ops;
6838 	if (bop && bop->bus_dma_win)
6839 		funcp = bop->bus_dma_win;
6840 
6841 	return ((*funcp)(HD, HD, h, win, offp, lenp, cookiep, ccountp));
6842 }
6843 
6844 int
6845 ddi_dma_set_sbus64(ddi_dma_handle_t h, ulong_t burstsizes)
6846 {
6847 	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_SET_SBUS64, 0,
6848 		&burstsizes, 0, 0));
6849 }
6850 
6851 int
6852 i_ddi_dma_fault_check(ddi_dma_impl_t *hp)
6853 {
6854 	return (hp->dmai_fault);
6855 }
6856 
6857 int
6858 ddi_check_dma_handle(ddi_dma_handle_t handle)
6859 {
6860 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6861 	int (*check)(ddi_dma_impl_t *);
6862 
6863 	if ((check = hp->dmai_fault_check) == NULL)
6864 		check = i_ddi_dma_fault_check;
6865 
6866 	return (((*check)(hp) == DDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
6867 }
6868 
6869 void
6870 i_ddi_dma_set_fault(ddi_dma_handle_t handle)
6871 {
6872 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6873 	void (*notify)(ddi_dma_impl_t *);
6874 
6875 	if (!hp->dmai_fault) {
6876 		hp->dmai_fault = 1;
6877 		if ((notify = hp->dmai_fault_notify) != NULL)
6878 			(*notify)(hp);
6879 	}
6880 }
6881 
6882 void
6883 i_ddi_dma_clr_fault(ddi_dma_handle_t handle)
6884 {
6885 	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
6886 	void (*notify)(ddi_dma_impl_t *);
6887 
6888 	if (hp->dmai_fault) {
6889 		hp->dmai_fault = 0;
6890 		if ((notify = hp->dmai_fault_notify) != NULL)
6891 			(*notify)(hp);
6892 	}
6893 }
6894 
6895 /*
6896  * register mapping routines.
6897  */
6898 int
6899 ddi_regs_map_setup(dev_info_t *dip, uint_t rnumber, caddr_t *addrp,
6900 	offset_t offset, offset_t len, ddi_device_acc_attr_t *accattrp,
6901 	ddi_acc_handle_t *handle)
6902 {
6903 	ddi_map_req_t mr;
6904 	ddi_acc_hdl_t *hp;
6905 	int result;
6906 
6907 	/*
6908 	 * Allocate and initialize the common elements of data access handle.
6909 	 */
6910 	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
6911 	hp = impl_acc_hdl_get(*handle);
6912 	hp->ah_vers = VERS_ACCHDL;
6913 	hp->ah_dip = dip;
6914 	hp->ah_rnumber = rnumber;
6915 	hp->ah_offset = offset;
6916 	hp->ah_len = len;
6917 	hp->ah_acc = *accattrp;
6918 
6919 	/*
6920 	 * Set up the mapping request and call to parent.
6921 	 */
6922 	mr.map_op = DDI_MO_MAP_LOCKED;
6923 	mr.map_type = DDI_MT_RNUMBER;
6924 	mr.map_obj.rnumber = rnumber;
6925 	mr.map_prot = PROT_READ | PROT_WRITE;
6926 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
6927 	mr.map_handlep = hp;
6928 	mr.map_vers = DDI_MAP_VERSION;
6929 	result = ddi_map(dip, &mr, offset, len, addrp);
6930 
6931 	/*
6932 	 * check for end result
6933 	 */
6934 	if (result != DDI_SUCCESS) {
6935 		impl_acc_hdl_free(*handle);
6936 		*handle = (ddi_acc_handle_t)NULL;
6937 	} else {
6938 		hp->ah_addr = *addrp;
6939 	}
6940 
6941 	return (result);
6942 }
6943 
6944 void
6945 ddi_regs_map_free(ddi_acc_handle_t *handlep)
6946 {
6947 	ddi_map_req_t mr;
6948 	ddi_acc_hdl_t *hp;
6949 
6950 	hp = impl_acc_hdl_get(*handlep);
6951 	ASSERT(hp);
6952 
6953 	mr.map_op = DDI_MO_UNMAP;
6954 	mr.map_type = DDI_MT_RNUMBER;
6955 	mr.map_obj.rnumber = hp->ah_rnumber;
6956 	mr.map_prot = PROT_READ | PROT_WRITE;
6957 	mr.map_flags = DDI_MF_KERNEL_MAPPING;
6958 	mr.map_handlep = hp;
6959 	mr.map_vers = DDI_MAP_VERSION;
6960 
6961 	/*
6962 	 * Call my parent to unmap my regs.
6963 	 */
6964 	(void) ddi_map(hp->ah_dip, &mr, hp->ah_offset,
6965 		hp->ah_len, &hp->ah_addr);
6966 	/*
6967 	 * free the handle
6968 	 */
6969 	impl_acc_hdl_free(*handlep);
6970 	*handlep = (ddi_acc_handle_t)NULL;
6971 }
6972 
6973 int
6974 ddi_device_zero(ddi_acc_handle_t handle, caddr_t dev_addr, size_t bytecount,
6975 	ssize_t dev_advcnt, uint_t dev_datasz)
6976 {
6977 	uint8_t *b;
6978 	uint16_t *w;
6979 	uint32_t *l;
6980 	uint64_t *ll;
6981 
6982 	/* check for total byte count is multiple of data transfer size */
6983 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
6984 		return (DDI_FAILURE);
6985 
6986 	switch (dev_datasz) {
6987 	case DDI_DATA_SZ01_ACC:
6988 		for (b = (uint8_t *)dev_addr;
6989 			bytecount != 0; bytecount -= 1, b += dev_advcnt)
6990 			ddi_put8(handle, b, 0);
6991 		break;
6992 	case DDI_DATA_SZ02_ACC:
6993 		for (w = (uint16_t *)dev_addr;
6994 			bytecount != 0; bytecount -= 2, w += dev_advcnt)
6995 			ddi_put16(handle, w, 0);
6996 		break;
6997 	case DDI_DATA_SZ04_ACC:
6998 		for (l = (uint32_t *)dev_addr;
6999 			bytecount != 0; bytecount -= 4, l += dev_advcnt)
7000 			ddi_put32(handle, l, 0);
7001 		break;
7002 	case DDI_DATA_SZ08_ACC:
7003 		for (ll = (uint64_t *)dev_addr;
7004 			bytecount != 0; bytecount -= 8, ll += dev_advcnt)
7005 			ddi_put64(handle, ll, 0x0ll);
7006 		break;
7007 	default:
7008 		return (DDI_FAILURE);
7009 	}
7010 	return (DDI_SUCCESS);
7011 }
7012 
7013 int
7014 ddi_device_copy(
7015 	ddi_acc_handle_t src_handle, caddr_t src_addr, ssize_t src_advcnt,
7016 	ddi_acc_handle_t dest_handle, caddr_t dest_addr, ssize_t dest_advcnt,
7017 	size_t bytecount, uint_t dev_datasz)
7018 {
7019 	uint8_t *b_src, *b_dst;
7020 	uint16_t *w_src, *w_dst;
7021 	uint32_t *l_src, *l_dst;
7022 	uint64_t *ll_src, *ll_dst;
7023 
7024 	/* check for total byte count is multiple of data transfer size */
7025 	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
7026 		return (DDI_FAILURE);
7027 
7028 	switch (dev_datasz) {
7029 	case DDI_DATA_SZ01_ACC:
7030 		b_src = (uint8_t *)src_addr;
7031 		b_dst = (uint8_t *)dest_addr;
7032 
7033 		for (; bytecount != 0; bytecount -= 1) {
7034 			ddi_put8(dest_handle, b_dst,
7035 				ddi_get8(src_handle, b_src));
7036 			b_dst += dest_advcnt;
7037 			b_src += src_advcnt;
7038 		}
7039 		break;
7040 	case DDI_DATA_SZ02_ACC:
7041 		w_src = (uint16_t *)src_addr;
7042 		w_dst = (uint16_t *)dest_addr;
7043 
7044 		for (; bytecount != 0; bytecount -= 2) {
7045 			ddi_put16(dest_handle, w_dst,
7046 				ddi_get16(src_handle, w_src));
7047 			w_dst += dest_advcnt;
7048 			w_src += src_advcnt;
7049 		}
7050 		break;
7051 	case DDI_DATA_SZ04_ACC:
7052 		l_src = (uint32_t *)src_addr;
7053 		l_dst = (uint32_t *)dest_addr;
7054 
7055 		for (; bytecount != 0; bytecount -= 4) {
7056 			ddi_put32(dest_handle, l_dst,
7057 				ddi_get32(src_handle, l_src));
7058 			l_dst += dest_advcnt;
7059 			l_src += src_advcnt;
7060 		}
7061 		break;
7062 	case DDI_DATA_SZ08_ACC:
7063 		ll_src = (uint64_t *)src_addr;
7064 		ll_dst = (uint64_t *)dest_addr;
7065 
7066 		for (; bytecount != 0; bytecount -= 8) {
7067 			ddi_put64(dest_handle, ll_dst,
7068 				ddi_get64(src_handle, ll_src));
7069 			ll_dst += dest_advcnt;
7070 			ll_src += src_advcnt;
7071 		}
7072 		break;
7073 	default:
7074 		return (DDI_FAILURE);
7075 	}
7076 	return (DDI_SUCCESS);
7077 }
7078 
7079 #define	swap16(value)  \
7080 	((((value) & 0xff) << 8) | ((value) >> 8))
7081 
7082 #define	swap32(value)	\
7083 	(((uint32_t)swap16((uint16_t)((value) & 0xffff)) << 16) | \
7084 	(uint32_t)swap16((uint16_t)((value) >> 16)))
7085 
7086 #define	swap64(value)	\
7087 	(((uint64_t)swap32((uint32_t)((value) & 0xffffffff)) \
7088 	    << 32) | \
7089 	(uint64_t)swap32((uint32_t)((value) >> 32)))
7090 
7091 uint16_t
7092 ddi_swap16(uint16_t value)
7093 {
7094 	return (swap16(value));
7095 }
7096 
7097 uint32_t
7098 ddi_swap32(uint32_t value)
7099 {
7100 	return (swap32(value));
7101 }
7102 
7103 uint64_t
7104 ddi_swap64(uint64_t value)
7105 {
7106 	return (swap64(value));
7107 }
7108 
7109 /*
7110  * Convert a binding name to a driver name.
7111  * A binding name is the name used to determine the driver for a
7112  * device - it may be either an alias for the driver or the name
7113  * of the driver itself.
7114  */
7115 char *
7116 i_binding_to_drv_name(char *bname)
7117 {
7118 	major_t major_no;
7119 
7120 	ASSERT(bname != NULL);
7121 
7122 	if ((major_no = ddi_name_to_major(bname)) == -1)
7123 		return (NULL);
7124 	return (ddi_major_to_name(major_no));
7125 }
7126 
7127 /*
7128  * Search for minor name that has specified dev_t and spec_type.
7129  * If spec_type is zero then any dev_t match works.  Since we
7130  * are returning a pointer to the minor name string, we require the
7131  * caller to do the locking.
7132  */
7133 char *
7134 i_ddi_devtspectype_to_minorname(dev_info_t *dip, dev_t dev, int spec_type)
7135 {
7136 	struct ddi_minor_data	*dmdp;
7137 
7138 	/*
7139 	 * The did layered driver currently intentionally returns a
7140 	 * devinfo ptr for an underlying sd instance based on a did
7141 	 * dev_t. In this case it is not an error.
7142 	 *
7143 	 * The did layered driver is associated with Sun Cluster.
7144 	 */
7145 	ASSERT((ddi_driver_major(dip) == getmajor(dev)) ||
7146 		(strcmp(ddi_major_to_name(getmajor(dev)), "did") == 0));
7147 	ASSERT(MUTEX_HELD(&(DEVI(dip)->devi_lock)));
7148 
7149 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
7150 		if (((dmdp->type == DDM_MINOR) ||
7151 		    (dmdp->type == DDM_INTERNAL_PATH) ||
7152 		    (dmdp->type == DDM_DEFAULT)) &&
7153 		    (dmdp->ddm_dev == dev) &&
7154 		    ((((spec_type & (S_IFCHR|S_IFBLK))) == 0) ||
7155 		    (dmdp->ddm_spec_type == spec_type)))
7156 			return (dmdp->ddm_name);
7157 	}
7158 
7159 	return (NULL);
7160 }
7161 
7162 /*
7163  * Find the devt and spectype of the specified minor_name.
7164  * Return DDI_FAILURE if minor_name not found. Since we are
7165  * returning everything via arguments we can do the locking.
7166  */
7167 int
7168 i_ddi_minorname_to_devtspectype(dev_info_t *dip, char *minor_name,
7169 	dev_t *devtp, int *spectypep)
7170 {
7171 	struct ddi_minor_data	*dmdp;
7172 
7173 	/* deal with clone minor nodes */
7174 	if (dip == clone_dip) {
7175 		major_t	major;
7176 		/*
7177 		 * Make sure minor_name is a STREAMS driver.
7178 		 * We load the driver but don't attach to any instances.
7179 		 */
7180 
7181 		major = ddi_name_to_major(minor_name);
7182 		if (major == (major_t)-1)
7183 			return (DDI_FAILURE);
7184 
7185 		if (ddi_hold_driver(major) == NULL)
7186 			return (DDI_FAILURE);
7187 
7188 		if (STREAMSTAB(major) == NULL) {
7189 			ddi_rele_driver(major);
7190 			return (DDI_FAILURE);
7191 		}
7192 		ddi_rele_driver(major);
7193 
7194 		if (devtp)
7195 			*devtp = makedevice(clone_major, (minor_t)major);
7196 
7197 		if (spectypep)
7198 			*spectypep = S_IFCHR;
7199 
7200 		return (DDI_SUCCESS);
7201 	}
7202 
7203 	ASSERT(!MUTEX_HELD(&(DEVI(dip)->devi_lock)));
7204 	mutex_enter(&(DEVI(dip)->devi_lock));
7205 
7206 	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
7207 		if (((dmdp->type != DDM_MINOR) &&
7208 		    (dmdp->type != DDM_INTERNAL_PATH) &&
7209 		    (dmdp->type != DDM_DEFAULT)) ||
7210 		    strcmp(minor_name, dmdp->ddm_name))
7211 			continue;
7212 
7213 		if (devtp)
7214 			*devtp = dmdp->ddm_dev;
7215 
7216 		if (spectypep)
7217 			*spectypep = dmdp->ddm_spec_type;
7218 
7219 		mutex_exit(&(DEVI(dip)->devi_lock));
7220 		return (DDI_SUCCESS);
7221 	}
7222 
7223 	mutex_exit(&(DEVI(dip)->devi_lock));
7224 	return (DDI_FAILURE);
7225 }
7226 
7227 extern char	hw_serial[];
7228 static kmutex_t devid_gen_mutex;
7229 static short	devid_gen_number;
7230 
7231 #ifdef DEBUG
7232 
7233 static int	devid_register_corrupt = 0;
7234 static int	devid_register_corrupt_major = 0;
7235 static int	devid_register_corrupt_hint = 0;
7236 static int	devid_register_corrupt_hint_major = 0;
7237 
7238 static int devid_lyr_debug = 0;
7239 
7240 #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)		\
7241 	if (devid_lyr_debug)					\
7242 		ddi_debug_devid_devts(msg, ndevs, devs)
7243 
7244 #else
7245 
7246 #define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)
7247 
7248 #endif /* DEBUG */
7249 
7250 
7251 #ifdef	DEBUG
7252 
7253 static void
7254 ddi_debug_devid_devts(char *msg, int ndevs, dev_t *devs)
7255 {
7256 	int i;
7257 
7258 	cmn_err(CE_CONT, "%s:\n", msg);
7259 	for (i = 0; i < ndevs; i++) {
7260 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
7261 	}
7262 }
7263 
7264 static void
7265 ddi_debug_devid_paths(char *msg, int npaths, char **paths)
7266 {
7267 	int i;
7268 
7269 	cmn_err(CE_CONT, "%s:\n", msg);
7270 	for (i = 0; i < npaths; i++) {
7271 		cmn_err(CE_CONT, "    %s\n", paths[i]);
7272 	}
7273 }
7274 
7275 static void
7276 ddi_debug_devid_devts_per_path(char *path, int ndevs, dev_t *devs)
7277 {
7278 	int i;
7279 
7280 	cmn_err(CE_CONT, "dev_ts per path %s\n", path);
7281 	for (i = 0; i < ndevs; i++) {
7282 		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
7283 	}
7284 }
7285 
7286 #endif	/* DEBUG */
7287 
7288 /*
7289  * Register device id into DDI framework.
7290  * Must be called when device is attached.
7291  */
7292 static int
7293 i_ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
7294 {
7295 	impl_devid_t	*i_devid = (impl_devid_t *)devid;
7296 	size_t		driver_len;
7297 	const char	*driver_name;
7298 	char		*devid_str;
7299 	major_t		major;
7300 
7301 	if ((dip == NULL) ||
7302 	    ((major = ddi_driver_major(dip)) == (major_t)-1))
7303 		return (DDI_FAILURE);
7304 
7305 	/* verify that the devid is valid */
7306 	if (ddi_devid_valid(devid) != DDI_SUCCESS)
7307 		return (DDI_FAILURE);
7308 
7309 	/* Updating driver name hint in devid */
7310 	driver_name = ddi_driver_name(dip);
7311 	driver_len = strlen(driver_name);
7312 	if (driver_len > DEVID_HINT_SIZE) {
7313 		/* Pick up last four characters of driver name */
7314 		driver_name += driver_len - DEVID_HINT_SIZE;
7315 		driver_len = DEVID_HINT_SIZE;
7316 	}
7317 	bzero(i_devid->did_driver, DEVID_HINT_SIZE);
7318 	bcopy(driver_name, i_devid->did_driver, driver_len);
7319 
7320 #ifdef DEBUG
7321 	/* Corrupt the devid for testing. */
7322 	if (devid_register_corrupt)
7323 		i_devid->did_id[0] += devid_register_corrupt;
7324 	if (devid_register_corrupt_major &&
7325 	    (major == devid_register_corrupt_major))
7326 		i_devid->did_id[0] += 1;
7327 	if (devid_register_corrupt_hint)
7328 		i_devid->did_driver[0] += devid_register_corrupt_hint;
7329 	if (devid_register_corrupt_hint_major &&
7330 	    (major == devid_register_corrupt_hint_major))
7331 		i_devid->did_driver[0] += 1;
7332 #endif /* DEBUG */
7333 
7334 	/* encode the devid as a string */
7335 	if ((devid_str = ddi_devid_str_encode(devid, NULL)) == NULL)
7336 		return (DDI_FAILURE);
7337 
7338 	/* add string as a string property */
7339 	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
7340 	    DEVID_PROP_NAME, devid_str) != DDI_SUCCESS) {
7341 		cmn_err(CE_WARN, "%s%d: devid property update failed",
7342 			ddi_driver_name(dip), ddi_get_instance(dip));
7343 		ddi_devid_str_free(devid_str);
7344 		return (DDI_FAILURE);
7345 	}
7346 
7347 	ddi_devid_str_free(devid_str);
7348 
7349 #ifdef	DEVID_COMPATIBILITY
7350 	/*
7351 	 * marker for devinfo snapshot compatibility.
7352 	 * This code gets deleted when di_devid is gone from libdevid
7353 	 */
7354 	DEVI(dip)->devi_devid = DEVID_COMPATIBILITY;
7355 #endif	/* DEVID_COMPATIBILITY */
7356 	return (DDI_SUCCESS);
7357 }
7358 
7359 int
7360 ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
7361 {
7362 	int rval;
7363 
7364 	rval = i_ddi_devid_register(dip, devid);
7365 	if (rval == DDI_SUCCESS) {
7366 		/*
7367 		 * Register devid in devid-to-path cache
7368 		 */
7369 		if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
7370 			mutex_enter(&DEVI(dip)->devi_lock);
7371 			DEVI(dip)->devi_flags |= DEVI_REGISTERED_DEVID;
7372 			mutex_exit(&DEVI(dip)->devi_lock);
7373 		} else {
7374 			cmn_err(CE_WARN, "%s%d: failed to cache devid",
7375 				ddi_driver_name(dip), ddi_get_instance(dip));
7376 		}
7377 	} else {
7378 		cmn_err(CE_WARN, "%s%d: failed to register devid",
7379 			ddi_driver_name(dip), ddi_get_instance(dip));
7380 	}
7381 	return (rval);
7382 }
7383 
7384 /*
7385  * Remove (unregister) device id from DDI framework.
7386  * Must be called when device is detached.
7387  */
7388 static void
7389 i_ddi_devid_unregister(dev_info_t *dip)
7390 {
7391 #ifdef	DEVID_COMPATIBILITY
7392 	/*
7393 	 * marker for micro release devinfo snapshot compatibility.
7394 	 * This code gets deleted for the minor release.
7395 	 */
7396 	DEVI(dip)->devi_devid = NULL;		/* unset DEVID_PROP */
7397 #endif	/* DEVID_COMPATIBILITY */
7398 
7399 	/* remove the devid property */
7400 	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, DEVID_PROP_NAME);
7401 }
7402 
7403 void
7404 ddi_devid_unregister(dev_info_t *dip)
7405 {
7406 	mutex_enter(&DEVI(dip)->devi_lock);
7407 	DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
7408 	mutex_exit(&DEVI(dip)->devi_lock);
7409 	e_devid_cache_unregister(dip);
7410 	i_ddi_devid_unregister(dip);
7411 }
7412 
7413 /*
7414  * Allocate and initialize a device id.
7415  */
7416 int
7417 ddi_devid_init(
7418 	dev_info_t	*dip,
7419 	ushort_t	devid_type,
7420 	ushort_t	nbytes,
7421 	void		*id,
7422 	ddi_devid_t	*ret_devid)
7423 {
7424 	impl_devid_t	*i_devid;
7425 	int		sz = sizeof (*i_devid) + nbytes - sizeof (char);
7426 	int		driver_len;
7427 	const char	*driver_name;
7428 
7429 	switch (devid_type) {
7430 	case DEVID_SCSI3_WWN:
7431 		/*FALLTHRU*/
7432 	case DEVID_SCSI_SERIAL:
7433 		/*FALLTHRU*/
7434 	case DEVID_ATA_SERIAL:
7435 		/*FALLTHRU*/
7436 	case DEVID_ENCAP:
7437 		if (nbytes == 0)
7438 			return (DDI_FAILURE);
7439 		if (id == NULL)
7440 			return (DDI_FAILURE);
7441 		break;
7442 	case DEVID_FAB:
7443 		if (nbytes != 0)
7444 			return (DDI_FAILURE);
7445 		if (id != NULL)
7446 			return (DDI_FAILURE);
7447 		nbytes = sizeof (int) +
7448 		    sizeof (struct timeval32) + sizeof (short);
7449 		sz += nbytes;
7450 		break;
7451 	default:
7452 		return (DDI_FAILURE);
7453 	}
7454 
7455 	if ((i_devid = kmem_zalloc(sz, KM_SLEEP)) == NULL)
7456 		return (DDI_FAILURE);
7457 
7458 	i_devid->did_magic_hi = DEVID_MAGIC_MSB;
7459 	i_devid->did_magic_lo = DEVID_MAGIC_LSB;
7460 	i_devid->did_rev_hi = DEVID_REV_MSB;
7461 	i_devid->did_rev_lo = DEVID_REV_LSB;
7462 	DEVID_FORMTYPE(i_devid, devid_type);
7463 	DEVID_FORMLEN(i_devid, nbytes);
7464 
7465 	/* Fill in driver name hint */
7466 	driver_name = ddi_driver_name(dip);
7467 	driver_len = strlen(driver_name);
7468 	if (driver_len > DEVID_HINT_SIZE) {
7469 		/* Pick up last four characters of driver name */
7470 		driver_name += driver_len - DEVID_HINT_SIZE;
7471 		driver_len = DEVID_HINT_SIZE;
7472 	}
7473 
7474 	bcopy(driver_name, i_devid->did_driver, driver_len);
7475 
7476 	/* Fill in id field */
7477 	if (devid_type == DEVID_FAB) {
7478 		char		*cp;
7479 		int		hostid;
7480 		char		*hostid_cp = &hw_serial[0];
7481 		struct timeval32 timestamp32;
7482 		int		i;
7483 		int		*ip;
7484 		short		gen;
7485 
7486 		/* increase the generation number */
7487 		mutex_enter(&devid_gen_mutex);
7488 		gen = devid_gen_number++;
7489 		mutex_exit(&devid_gen_mutex);
7490 
7491 		cp = i_devid->did_id;
7492 
7493 		/* Fill in host id (big-endian byte ordering) */
7494 		hostid = stoi(&hostid_cp);
7495 		*cp++ = hibyte(hiword(hostid));
7496 		*cp++ = lobyte(hiword(hostid));
7497 		*cp++ = hibyte(loword(hostid));
7498 		*cp++ = lobyte(loword(hostid));
7499 
7500 		/*
7501 		 * Fill in timestamp (big-endian byte ordering)
7502 		 *
7503 		 * (Note that the format may have to be changed
7504 		 * before 2038 comes around, though it's arguably
7505 		 * unique enough as it is..)
7506 		 */
7507 		uniqtime32(&timestamp32);
7508 		ip = (int *)&timestamp32;
7509 		for (i = 0;
7510 		    i < sizeof (timestamp32) / sizeof (int); i++, ip++) {
7511 			int	val;
7512 			val = *ip;
7513 			*cp++ = hibyte(hiword(val));
7514 			*cp++ = lobyte(hiword(val));
7515 			*cp++ = hibyte(loword(val));
7516 			*cp++ = lobyte(loword(val));
7517 		}
7518 
7519 		/* fill in the generation number */
7520 		*cp++ = hibyte(gen);
7521 		*cp++ = lobyte(gen);
7522 	} else
7523 		bcopy(id, i_devid->did_id, nbytes);
7524 
7525 	/* return device id */
7526 	*ret_devid = (ddi_devid_t)i_devid;
7527 	return (DDI_SUCCESS);
7528 }
7529 
7530 int
7531 i_ddi_devi_get_devid(dev_t dev, dev_info_t *dip, ddi_devid_t *ret_devid)
7532 {
7533 	char		*devidstr;
7534 
7535 	ASSERT(dev != DDI_DEV_T_NONE);
7536 
7537 	/* look up the property, devt specific first */
7538 	if (ddi_prop_lookup_string(dev, dip, DDI_PROP_DONTPASS,
7539 	    DEVID_PROP_NAME, &devidstr) != DDI_PROP_SUCCESS) {
7540 		if ((dev == DDI_DEV_T_ANY) ||
7541 		    (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
7542 			DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) !=
7543 			DDI_PROP_SUCCESS)) {
7544 				return (DDI_FAILURE);
7545 		}
7546 	}
7547 
7548 	/* convert to binary form */
7549 	if (ddi_devid_str_decode(devidstr, ret_devid, NULL) == -1) {
7550 		ddi_prop_free(devidstr);
7551 		return (DDI_FAILURE);
7552 	}
7553 	ddi_prop_free(devidstr);
7554 	return (DDI_SUCCESS);
7555 }
7556 
7557 /*
7558  * Return a copy of the device id for dev_t
7559  */
7560 int
7561 ddi_lyr_get_devid(dev_t dev, ddi_devid_t *ret_devid)
7562 {
7563 	dev_info_t	*dip;
7564 	int		rval;
7565 
7566 	/* get the dip */
7567 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
7568 		return (DDI_FAILURE);
7569 
7570 	rval = i_ddi_devi_get_devid(dev, dip, ret_devid);
7571 
7572 	ddi_release_devi(dip);		/* e_ddi_hold_devi_by_dev() */
7573 	return (rval);
7574 }
7575 
7576 /*
7577  * Return a copy of the minor name for dev_t and spec_type
7578  */
7579 int
7580 ddi_lyr_get_minor_name(dev_t dev, int spec_type, char **minor_name)
7581 {
7582 	dev_info_t	*dip;
7583 	char		*nm;
7584 	size_t		alloc_sz, sz;
7585 
7586 	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
7587 		return (DDI_FAILURE);
7588 
7589 	mutex_enter(&(DEVI(dip)->devi_lock));
7590 
7591 	if ((nm = i_ddi_devtspectype_to_minorname(dip,
7592 	    dev, spec_type)) == NULL) {
7593 		mutex_exit(&(DEVI(dip)->devi_lock));
7594 		ddi_release_devi(dip);	/* e_ddi_hold_devi_by_dev() */
7595 		return (DDI_FAILURE);
7596 	}
7597 
7598 	/* make a copy */
7599 	alloc_sz = strlen(nm) + 1;
7600 retry:
7601 	/* drop lock to allocate memory */
7602 	mutex_exit(&(DEVI(dip)->devi_lock));
7603 	*minor_name = kmem_alloc(alloc_sz, KM_SLEEP);
7604 	mutex_enter(&(DEVI(dip)->devi_lock));
7605 
7606 	/* re-check things, since we dropped the lock */
7607 	if ((nm = i_ddi_devtspectype_to_minorname(dip,
7608 	    dev, spec_type)) == NULL) {
7609 		mutex_exit(&(DEVI(dip)->devi_lock));
7610 		kmem_free(*minor_name, alloc_sz);
7611 		*minor_name = NULL;
7612 		ddi_release_devi(dip);	/* e_ddi_hold_devi_by_dev() */
7613 		return (DDI_FAILURE);
7614 	}
7615 
7616 	/* verify size is the same */
7617 	sz = strlen(nm) + 1;
7618 	if (alloc_sz != sz) {
7619 		kmem_free(*minor_name, alloc_sz);
7620 		alloc_sz = sz;
7621 		goto retry;
7622 	}
7623 
7624 	/* sz == alloc_sz - make a copy */
7625 	(void) strcpy(*minor_name, nm);
7626 
7627 	mutex_exit(&(DEVI(dip)->devi_lock));
7628 	ddi_release_devi(dip);	/* e_ddi_hold_devi_by_dev() */
7629 	return (DDI_SUCCESS);
7630 }
7631 
7632 int
7633 ddi_lyr_devid_to_devlist(
7634 	ddi_devid_t	devid,
7635 	char		*minor_name,
7636 	int		*retndevs,
7637 	dev_t		**retdevs)
7638 {
7639 	ASSERT(ddi_devid_valid(devid) == DDI_SUCCESS);
7640 
7641 	if (e_devid_cache_to_devt_list(devid, minor_name,
7642 	    retndevs, retdevs) == DDI_SUCCESS) {
7643 		ASSERT(*retndevs > 0);
7644 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
7645 			*retndevs, *retdevs);
7646 		return (DDI_SUCCESS);
7647 	}
7648 
7649 	if (e_ddi_devid_discovery(devid) == DDI_FAILURE) {
7650 		return (DDI_FAILURE);
7651 	}
7652 
7653 	if (e_devid_cache_to_devt_list(devid, minor_name,
7654 	    retndevs, retdevs) == DDI_SUCCESS) {
7655 		ASSERT(*retndevs > 0);
7656 		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
7657 			*retndevs, *retdevs);
7658 		return (DDI_SUCCESS);
7659 	}
7660 
7661 	return (DDI_FAILURE);
7662 }
7663 
7664 void
7665 ddi_lyr_free_devlist(dev_t *devlist, int ndevs)
7666 {
7667 	kmem_free(devlist, sizeof (dev_t) * ndevs);
7668 }
7669 
7670 /*
7671  * Note: This will need to be fixed if we ever allow processes to
7672  * have more than one data model per exec.
7673  */
7674 model_t
7675 ddi_mmap_get_model(void)
7676 {
7677 	return (get_udatamodel());
7678 }
7679 
7680 model_t
7681 ddi_model_convert_from(model_t model)
7682 {
7683 	return ((model & DDI_MODEL_MASK) & ~DDI_MODEL_NATIVE);
7684 }
7685 
7686 /*
7687  * ddi interfaces managing storage and retrieval of eventcookies.
7688  */
7689 
7690 /*
7691  * Invoke bus nexus driver's implementation of the
7692  * (*bus_remove_eventcall)() interface to remove a registered
7693  * callback handler for "event".
7694  */
7695 int
7696 ddi_remove_event_handler(ddi_callback_id_t id)
7697 {
7698 	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)id;
7699 	dev_info_t *ddip;
7700 
7701 	ASSERT(cb);
7702 	if (!cb) {
7703 		return (DDI_FAILURE);
7704 	}
7705 
7706 	ddip = NDI_EVENT_DDIP(cb->ndi_evtcb_cookie);
7707 	return (ndi_busop_remove_eventcall(ddip, id));
7708 }
7709 
7710 /*
7711  * Invoke bus nexus driver's implementation of the
7712  * (*bus_add_eventcall)() interface to register a callback handler
7713  * for "event".
7714  */
7715 int
7716 ddi_add_event_handler(dev_info_t *dip, ddi_eventcookie_t event,
7717     void (*handler)(dev_info_t *, ddi_eventcookie_t, void *, void *),
7718     void *arg, ddi_callback_id_t *id)
7719 {
7720 	return (ndi_busop_add_eventcall(dip, dip, event, handler, arg, id));
7721 }
7722 
7723 
7724 /*
7725  * Return a handle for event "name" by calling up the device tree
7726  * hierarchy via  (*bus_get_eventcookie)() interface until claimed
7727  * by a bus nexus or top of dev_info tree is reached.
7728  */
7729 int
7730 ddi_get_eventcookie(dev_info_t *dip, char *name,
7731     ddi_eventcookie_t *event_cookiep)
7732 {
7733 	return (ndi_busop_get_eventcookie(dip, dip,
7734 	    name, event_cookiep));
7735 }
7736 
7737 /*
7738  * single thread access to dev_info node and set state
7739  */
7740 void
7741 i_devi_enter(dev_info_t *dip, uint_t s_mask, uint_t w_mask, int has_lock)
7742 {
7743 	if (!has_lock)
7744 		mutex_enter(&(DEVI(dip)->devi_lock));
7745 
7746 	ASSERT(mutex_owned(&(DEVI(dip)->devi_lock)));
7747 
7748 	/*
7749 	 * wait until state(s) have been changed
7750 	 */
7751 	while ((DEVI(dip)->devi_state & w_mask) != 0) {
7752 		cv_wait(&(DEVI(dip)->devi_cv), &(DEVI(dip)->devi_lock));
7753 	}
7754 	DEVI(dip)->devi_state |= s_mask;
7755 
7756 	if (!has_lock)
7757 		mutex_exit(&(DEVI(dip)->devi_lock));
7758 }
7759 
7760 void
7761 i_devi_exit(dev_info_t *dip, uint_t c_mask, int has_lock)
7762 {
7763 	if (!has_lock)
7764 		mutex_enter(&(DEVI(dip)->devi_lock));
7765 
7766 	ASSERT(mutex_owned(&(DEVI(dip)->devi_lock)));
7767 
7768 	/*
7769 	 * clear the state(s) and wakeup any threads waiting
7770 	 * for state change
7771 	 */
7772 	DEVI(dip)->devi_state &= ~c_mask;
7773 	cv_broadcast(&(DEVI(dip)->devi_cv));
7774 
7775 	if (!has_lock)
7776 		mutex_exit(&(DEVI(dip)->devi_lock));
7777 }
7778 
7779 /*
7780  * This procedure is provided as the general callback function when
7781  * umem_lockmemory calls as_add_callback for long term memory locking.
7782  * When as_unmap, as_setprot, or as_free encounter segments which have
7783  * locked memory, this callback will be invoked.
7784  */
7785 void
7786 umem_lock_undo(struct as *as, void *arg, uint_t event)
7787 {
7788 	_NOTE(ARGUNUSED(as, event))
7789 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)arg;
7790 
7791 	/*
7792 	 * Call the cleanup function.  Decrement the cookie reference
7793 	 * count, if it goes to zero, return the memory for the cookie.
7794 	 * The i_ddi_umem_unlock for this cookie may or may not have been
7795 	 * called already.  It is the responsibility of the caller of
7796 	 * umem_lockmemory to handle the case of the cleanup routine
7797 	 * being called after a ddi_umem_unlock for the cookie
7798 	 * was called.
7799 	 */
7800 
7801 	(*cp->callbacks.cbo_umem_lock_cleanup)((ddi_umem_cookie_t)cp);
7802 
7803 	/* remove the cookie if reference goes to zero */
7804 	if (atomic_add_long_nv((ulong_t *)(&(cp->cook_refcnt)), -1) == 0) {
7805 		kmem_free(cp, sizeof (struct ddi_umem_cookie));
7806 	}
7807 }
7808 
7809 /*
7810  * The following two Consolidation Private routines provide generic
7811  * interfaces to increase/decrease the amount of device-locked memory.
7812  *
7813  * To keep project_rele and project_hold consistent, i_ddi_decr_locked_memory()
7814  * must be called every time i_ddi_incr_locked_memory() is called.
7815  */
7816 int
7817 /* ARGSUSED */
7818 i_ddi_incr_locked_memory(proc_t *procp, task_t *taskp,
7819     kproject_t *projectp, zone_t *zonep, rctl_qty_t inc)
7820 {
7821 	kproject_t *projp;
7822 
7823 	ASSERT(procp);
7824 	ASSERT(mutex_owned(&procp->p_lock));
7825 
7826 	projp = procp->p_task->tk_proj;
7827 	mutex_enter(&umem_devlockmem_rctl_lock);
7828 	/*
7829 	 * Test if the requested memory can be locked without exceeding the
7830 	 * limits.
7831 	 */
7832 	if (rctl_test(rc_project_devlockmem, projp->kpj_rctls,
7833 	    procp, inc, RCA_SAFE) & RCT_DENY) {
7834 		mutex_exit(&umem_devlockmem_rctl_lock);
7835 		return (ENOMEM);
7836 	}
7837 	projp->kpj_data.kpd_devlockmem += inc;
7838 	mutex_exit(&umem_devlockmem_rctl_lock);
7839 	/*
7840 	 * Grab a hold on the project.
7841 	 */
7842 	(void) project_hold(projp);
7843 
7844 	return (0);
7845 }
7846 
7847 /*
7848  * To keep project_rele and project_hold consistent, i_ddi_incr_locked_memory()
7849  * must be called every time i_ddi_decr_locked_memory() is called.
7850  */
7851 /* ARGSUSED */
7852 void
7853 i_ddi_decr_locked_memory(proc_t *procp, task_t *taskp,
7854     kproject_t *projectp, zone_t *zonep, rctl_qty_t dec)
7855 {
7856 	ASSERT(projectp);
7857 
7858 	mutex_enter(&umem_devlockmem_rctl_lock);
7859 	projectp->kpj_data.kpd_devlockmem -= dec;
7860 	mutex_exit(&umem_devlockmem_rctl_lock);
7861 
7862 	/*
7863 	 * Release the project pointer reference accquired in
7864 	 * i_ddi_incr_locked_memory().
7865 	 */
7866 	(void) project_rele(projectp);
7867 }
7868 
7869 /*
7870  * This routine checks if the max-device-locked-memory resource ctl is
7871  * exceeded, if not increments it, grabs a hold on the project.
7872  * Returns 0 if successful otherwise returns error code
7873  */
7874 static int
7875 umem_incr_devlockmem(struct ddi_umem_cookie *cookie)
7876 {
7877 	proc_t		*procp;
7878 	int		ret;
7879 
7880 	ASSERT(cookie);
7881 	procp = cookie->procp;
7882 	ASSERT(procp);
7883 
7884 	mutex_enter(&procp->p_lock);
7885 
7886 	if ((ret = i_ddi_incr_locked_memory(procp, NULL,
7887 		NULL, NULL, cookie->size)) != 0) {
7888 		mutex_exit(&procp->p_lock);
7889 		return (ret);
7890 	}
7891 
7892 	/*
7893 	 * save the project pointer in the
7894 	 * umem cookie, project pointer already
7895 	 * hold in i_ddi_incr_locked_memory
7896 	 */
7897 	cookie->lockmem_proj = (void *)procp->p_task->tk_proj;
7898 	mutex_exit(&procp->p_lock);
7899 
7900 	return (0);
7901 }
7902 
7903 /*
7904  * Decrements the max-device-locked-memory resource ctl and releases
7905  * the hold on the project that was acquired during umem_incr_devlockmem
7906  */
7907 static void
7908 umem_decr_devlockmem(struct ddi_umem_cookie *cookie)
7909 {
7910 	kproject_t	*projp;
7911 
7912 	if (!cookie->lockmem_proj)
7913 		return;
7914 
7915 	projp = (kproject_t *)cookie->lockmem_proj;
7916 	i_ddi_decr_locked_memory(NULL, NULL, projp, NULL, cookie->size);
7917 
7918 	cookie->lockmem_proj = NULL;
7919 }
7920 
7921 /*
7922  * A consolidation private function which is essentially equivalent to
7923  * ddi_umem_lock but with the addition of arguments ops_vector and procp.
7924  * A call to as_add_callback is done if DDI_UMEMLOCK_LONGTERM is set, and
7925  * the ops_vector is valid.
7926  *
7927  * Lock the virtual address range in the current process and create a
7928  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
7929  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
7930  * to user space.
7931  *
7932  * Note: The resource control accounting currently uses a full charge model
7933  * in other words attempts to lock the same/overlapping areas of memory
7934  * will deduct the full size of the buffer from the projects running
7935  * counter for the device locked memory.
7936  *
7937  * addr, size should be PAGESIZE aligned
7938  *
7939  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
7940  *	identifies whether the locked memory will be read or written or both
7941  *      DDI_UMEMLOCK_LONGTERM  must be set when the locking will
7942  * be maintained for an indefinitely long period (essentially permanent),
7943  * rather than for what would be required for a typical I/O completion.
7944  * When DDI_UMEMLOCK_LONGTERM is set, umem_lockmemory will return EFAULT
7945  * if the memory pertains to a regular file which is mapped MAP_SHARED.
7946  * This is to prevent a deadlock if a file truncation is attempted after
7947  * after the locking is done.
7948  *
7949  * Returns 0 on success
7950  *	EINVAL - for invalid parameters
7951  *	EPERM, ENOMEM and other error codes returned by as_pagelock
7952  *	ENOMEM - is returned if the current request to lock memory exceeds
7953  *		project.max-device-locked-memory resource control value.
7954  *      EFAULT - memory pertains to a regular file mapped shared and
7955  *		and DDI_UMEMLOCK_LONGTERM flag is set
7956  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
7957  */
7958 int
7959 umem_lockmemory(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie,
7960 		struct umem_callback_ops *ops_vector,
7961 		proc_t *procp)
7962 {
7963 	int	error;
7964 	struct ddi_umem_cookie *p;
7965 	void	(*driver_callback)() = NULL;
7966 	struct as *as = procp->p_as;
7967 	struct seg		*seg;
7968 	vnode_t			*vp;
7969 
7970 	*cookie = NULL;		/* in case of any error return */
7971 
7972 	/* These are the only three valid flags */
7973 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE |
7974 	    DDI_UMEMLOCK_LONGTERM)) != 0)
7975 		return (EINVAL);
7976 
7977 	/* At least one (can be both) of the two access flags must be set */
7978 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0)
7979 		return (EINVAL);
7980 
7981 	/* addr and len must be page-aligned */
7982 	if (((uintptr_t)addr & PAGEOFFSET) != 0)
7983 		return (EINVAL);
7984 
7985 	if ((len & PAGEOFFSET) != 0)
7986 		return (EINVAL);
7987 
7988 	/*
7989 	 * For longterm locking a driver callback must be specified; if
7990 	 * not longterm then a callback is optional.
7991 	 */
7992 	if (ops_vector != NULL) {
7993 		if (ops_vector->cbo_umem_callback_version !=
7994 		    UMEM_CALLBACK_VERSION)
7995 			return (EINVAL);
7996 		else
7997 			driver_callback = ops_vector->cbo_umem_lock_cleanup;
7998 	}
7999 	if ((driver_callback == NULL) && (flags & DDI_UMEMLOCK_LONGTERM))
8000 		return (EINVAL);
8001 
8002 	/*
8003 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
8004 	 * be called on first ddi_umem_lock or umem_lockmemory call.
8005 	 */
8006 	if (ddi_umem_unlock_thread == NULL)
8007 		i_ddi_umem_unlock_thread_start();
8008 
8009 	/* Allocate memory for the cookie */
8010 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
8011 
8012 	/* Convert the flags to seg_rw type */
8013 	if (flags & DDI_UMEMLOCK_WRITE) {
8014 		p->s_flags = S_WRITE;
8015 	} else {
8016 		p->s_flags = S_READ;
8017 	}
8018 
8019 	/* Store procp in cookie for later iosetup/unlock */
8020 	p->procp = (void *)procp;
8021 
8022 	/*
8023 	 * Store the struct as pointer in cookie for later use by
8024 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
8025 	 * is called after relvm is called.
8026 	 */
8027 	p->asp = as;
8028 
8029 	/*
8030 	 * The size field is needed for lockmem accounting.
8031 	 */
8032 	p->size = len;
8033 
8034 	if (umem_incr_devlockmem(p) != 0) {
8035 		/*
8036 		 * The requested memory cannot be locked
8037 		 */
8038 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8039 		*cookie = (ddi_umem_cookie_t)NULL;
8040 		return (ENOMEM);
8041 	}
8042 	/*
8043 	 * umem_incr_devlockmem stashes the project ptr into the
8044 	 * cookie. This is needed during unlock since that can
8045 	 * happen in a non-USER context
8046 	 */
8047 	ASSERT(p->lockmem_proj);
8048 
8049 	/* Lock the pages corresponding to addr, len in memory */
8050 	error = as_pagelock(as, &(p->pparray), addr, len, p->s_flags);
8051 	if (error != 0) {
8052 		umem_decr_devlockmem(p);
8053 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8054 		*cookie = (ddi_umem_cookie_t)NULL;
8055 		return (error);
8056 	}
8057 
8058 	/*
8059 	 * For longterm locking the addr must pertain to a seg_vn segment or
8060 	 * or a seg_spt segment.
8061 	 * If the segment pertains to a regular file, it cannot be
8062 	 * mapped MAP_SHARED.
8063 	 * This is to prevent a deadlock if a file truncation is attempted
8064 	 * after the locking is done.
8065 	 * Doing this after as_pagelock guarantees persistence of the as; if
8066 	 * an unacceptable segment is found, the cleanup includes calling
8067 	 * as_pageunlock before returning EFAULT.
8068 	 */
8069 	if (flags & DDI_UMEMLOCK_LONGTERM) {
8070 		extern  struct seg_ops segspt_shmops;
8071 		AS_LOCK_ENTER(as, &as->a_lock, RW_READER);
8072 		for (seg = as_segat(as, addr); ; seg = AS_SEGNEXT(as, seg)) {
8073 			if (seg == NULL || seg->s_base > addr + len)
8074 				break;
8075 			if (((seg->s_ops != &segvn_ops) &&
8076 			    (seg->s_ops != &segspt_shmops)) ||
8077 			    ((SEGOP_GETVP(seg, addr, &vp) == 0 &&
8078 			    vp != NULL && vp->v_type == VREG) &&
8079 			    (SEGOP_GETTYPE(seg, addr) & MAP_SHARED))) {
8080 				as_pageunlock(as, p->pparray,
8081 						addr, len, p->s_flags);
8082 				AS_LOCK_EXIT(as, &as->a_lock);
8083 				umem_decr_devlockmem(p);
8084 				kmem_free(p, sizeof (struct ddi_umem_cookie));
8085 				*cookie = (ddi_umem_cookie_t)NULL;
8086 				return (EFAULT);
8087 			}
8088 		}
8089 		AS_LOCK_EXIT(as, &as->a_lock);
8090 	}
8091 
8092 
8093 	/* Initialize the fields in the ddi_umem_cookie */
8094 	p->cvaddr = addr;
8095 	p->type = UMEM_LOCKED;
8096 	if (driver_callback != NULL) {
8097 		/* i_ddi_umem_unlock and umem_lock_undo may need the cookie */
8098 		p->cook_refcnt = 2;
8099 		p->callbacks = *ops_vector;
8100 	} else {
8101 		/* only i_ddi_umme_unlock needs the cookie */
8102 		p->cook_refcnt = 1;
8103 	}
8104 
8105 	*cookie = (ddi_umem_cookie_t)p;
8106 
8107 	/*
8108 	 * If a driver callback was specified, add an entry to the
8109 	 * as struct callback list. The as_pagelock above guarantees
8110 	 * the persistence of as.
8111 	 */
8112 	if (driver_callback) {
8113 		error = as_add_callback(as, umem_lock_undo, p, AS_ALL_EVENT,
8114 						addr, len, KM_SLEEP);
8115 		if (error != 0) {
8116 			as_pageunlock(as, p->pparray,
8117 					addr, len, p->s_flags);
8118 			umem_decr_devlockmem(p);
8119 			kmem_free(p, sizeof (struct ddi_umem_cookie));
8120 			*cookie = (ddi_umem_cookie_t)NULL;
8121 		}
8122 	}
8123 	return (error);
8124 }
8125 
8126 /*
8127  * Unlock the pages locked by ddi_umem_lock or umem_lockmemory and free
8128  * the cookie.  Called from i_ddi_umem_unlock_thread.
8129  */
8130 
8131 static void
8132 i_ddi_umem_unlock(struct ddi_umem_cookie *p)
8133 {
8134 	uint_t	rc;
8135 
8136 	/*
8137 	 * There is no way to determine whether a callback to
8138 	 * umem_lock_undo was registered via as_add_callback.
8139 	 * (i.e. umem_lockmemory was called with DDI_MEMLOCK_LONGTERM and
8140 	 * a valid callback function structure.)  as_delete_callback
8141 	 * is called to delete a possible registered callback.  If the
8142 	 * return from as_delete_callbacks is AS_CALLBACK_DELETED, it
8143 	 * indicates that there was a callback registered, and that is was
8144 	 * successfully deleted.  Thus, the cookie reference count
8145 	 * will never be decremented by umem_lock_undo.  Just return the
8146 	 * memory for the cookie, since both users of the cookie are done.
8147 	 * A return of AS_CALLBACK_NOTFOUND indicates a callback was
8148 	 * never registered.  A return of AS_CALLBACK_DELETE_DEFERRED
8149 	 * indicates that callback processing is taking place and, and
8150 	 * umem_lock_undo is, or will be, executing, and thus decrementing
8151 	 * the cookie reference count when it is complete.
8152 	 *
8153 	 * This needs to be done before as_pageunlock so that the
8154 	 * persistence of as is guaranteed because of the locked pages.
8155 	 *
8156 	 */
8157 	rc = as_delete_callback(p->asp, p);
8158 
8159 
8160 	/*
8161 	 * The proc->p_as will be stale if i_ddi_umem_unlock is called
8162 	 * after relvm is called so use p->asp.
8163 	 */
8164 	as_pageunlock(p->asp, p->pparray, p->cvaddr, p->size, p->s_flags);
8165 
8166 	/*
8167 	 * Now that we have unlocked the memory decrement the
8168 	 * max-device-locked-memory rctl
8169 	 */
8170 	umem_decr_devlockmem(p);
8171 
8172 	if (rc == AS_CALLBACK_DELETED) {
8173 		/* umem_lock_undo will not happen, return the cookie memory */
8174 		ASSERT(p->cook_refcnt == 2);
8175 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8176 	} else {
8177 		/*
8178 		 * umem_undo_lock may happen if as_delete_callback returned
8179 		 * AS_CALLBACK_DELETE_DEFERRED.  In that case, decrement the
8180 		 * reference count, atomically, and return the cookie
8181 		 * memory if the reference count goes to zero.  The only
8182 		 * other value for rc is AS_CALLBACK_NOTFOUND.  In that
8183 		 * case, just return the cookie memory.
8184 		 */
8185 		if ((rc != AS_CALLBACK_DELETE_DEFERRED) ||
8186 		    (atomic_add_long_nv((ulong_t *)(&(p->cook_refcnt)), -1)
8187 		    == 0)) {
8188 			kmem_free(p, sizeof (struct ddi_umem_cookie));
8189 		}
8190 	}
8191 }
8192 
8193 /*
8194  * i_ddi_umem_unlock_thread - deferred ddi_umem_unlock list handler.
8195  *
8196  * Call i_ddi_umem_unlock for entries in the ddi_umem_unlock list
8197  * until it is empty.  Then, wait for more to be added.  This thread is awoken
8198  * via calls to ddi_umem_unlock.
8199  */
8200 
8201 static void
8202 i_ddi_umem_unlock_thread(void)
8203 {
8204 	struct ddi_umem_cookie	*ret_cookie;
8205 	callb_cpr_t	cprinfo;
8206 
8207 	/* process the ddi_umem_unlock list */
8208 	CALLB_CPR_INIT(&cprinfo, &ddi_umem_unlock_mutex,
8209 	    callb_generic_cpr, "unlock_thread");
8210 	for (;;) {
8211 		mutex_enter(&ddi_umem_unlock_mutex);
8212 		if (ddi_umem_unlock_head != NULL) {	/* list not empty */
8213 			ret_cookie = ddi_umem_unlock_head;
8214 			/* take if off the list */
8215 			if ((ddi_umem_unlock_head =
8216 			    ddi_umem_unlock_head->unl_forw) == NULL) {
8217 				ddi_umem_unlock_tail = NULL;
8218 			}
8219 			mutex_exit(&ddi_umem_unlock_mutex);
8220 			/* unlock the pages in this cookie */
8221 			(void) i_ddi_umem_unlock(ret_cookie);
8222 		} else {   /* list is empty, wait for next ddi_umem_unlock */
8223 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
8224 			cv_wait(&ddi_umem_unlock_cv, &ddi_umem_unlock_mutex);
8225 			CALLB_CPR_SAFE_END(&cprinfo, &ddi_umem_unlock_mutex);
8226 			mutex_exit(&ddi_umem_unlock_mutex);
8227 		}
8228 	}
8229 	/* ddi_umem_unlock_thread does not exit */
8230 	/* NOTREACHED */
8231 }
8232 
8233 /*
8234  * Start the thread that will process the ddi_umem_unlock list if it is
8235  * not already started (i_ddi_umem_unlock_thread).
8236  */
8237 static void
8238 i_ddi_umem_unlock_thread_start(void)
8239 {
8240 	mutex_enter(&ddi_umem_unlock_mutex);
8241 	if (ddi_umem_unlock_thread == NULL) {
8242 		ddi_umem_unlock_thread = thread_create(NULL, 0,
8243 		    i_ddi_umem_unlock_thread, NULL, 0, &p0,
8244 		    TS_RUN, minclsyspri);
8245 	}
8246 	mutex_exit(&ddi_umem_unlock_mutex);
8247 }
8248 
8249 /*
8250  * Lock the virtual address range in the current process and create a
8251  * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
8252  * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
8253  * to user space.
8254  *
8255  * Note: The resource control accounting currently uses a full charge model
8256  * in other words attempts to lock the same/overlapping areas of memory
8257  * will deduct the full size of the buffer from the projects running
8258  * counter for the device locked memory. This applies to umem_lockmemory too.
8259  *
8260  * addr, size should be PAGESIZE aligned
8261  * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
8262  *	identifies whether the locked memory will be read or written or both
8263  *
8264  * Returns 0 on success
8265  *	EINVAL - for invalid parameters
8266  *	EPERM, ENOMEM and other error codes returned by as_pagelock
8267  *	ENOMEM - is returned if the current request to lock memory exceeds
8268  *		project.max-device-locked-memory resource control value.
8269  *	EAGAIN - could not start the ddi_umem_unlock list processing thread
8270  */
8271 int
8272 ddi_umem_lock(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie)
8273 {
8274 	int	error;
8275 	struct ddi_umem_cookie *p;
8276 
8277 	*cookie = NULL;		/* in case of any error return */
8278 
8279 	/* These are the only two valid flags */
8280 	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) != 0) {
8281 		return (EINVAL);
8282 	}
8283 
8284 	/* At least one of the two flags (or both) must be set */
8285 	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0) {
8286 		return (EINVAL);
8287 	}
8288 
8289 	/* addr and len must be page-aligned */
8290 	if (((uintptr_t)addr & PAGEOFFSET) != 0) {
8291 		return (EINVAL);
8292 	}
8293 
8294 	if ((len & PAGEOFFSET) != 0) {
8295 		return (EINVAL);
8296 	}
8297 
8298 	/*
8299 	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
8300 	 * be called on first ddi_umem_lock or umem_lockmemory call.
8301 	 */
8302 	if (ddi_umem_unlock_thread == NULL)
8303 		i_ddi_umem_unlock_thread_start();
8304 
8305 	/* Allocate memory for the cookie */
8306 	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);
8307 
8308 	/* Convert the flags to seg_rw type */
8309 	if (flags & DDI_UMEMLOCK_WRITE) {
8310 		p->s_flags = S_WRITE;
8311 	} else {
8312 		p->s_flags = S_READ;
8313 	}
8314 
8315 	/* Store curproc in cookie for later iosetup/unlock */
8316 	p->procp = (void *)curproc;
8317 
8318 	/*
8319 	 * Store the struct as pointer in cookie for later use by
8320 	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
8321 	 * is called after relvm is called.
8322 	 */
8323 	p->asp = curproc->p_as;
8324 	/*
8325 	 * The size field is needed for lockmem accounting.
8326 	 */
8327 	p->size = len;
8328 
8329 	if (umem_incr_devlockmem(p) != 0) {
8330 		/*
8331 		 * The requested memory cannot be locked
8332 		 */
8333 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8334 		*cookie = (ddi_umem_cookie_t)NULL;
8335 		return (ENOMEM);
8336 	}
8337 	/*
8338 	 * umem_incr_devlockmem stashes the project ptr into the
8339 	 * cookie. This is needed during unlock since that can
8340 	 * happen in a non-USER context
8341 	 */
8342 	ASSERT(p->lockmem_proj);
8343 
8344 	/* Lock the pages corresponding to addr, len in memory */
8345 	error = as_pagelock(((proc_t *)p->procp)->p_as, &(p->pparray),
8346 	    addr, len, p->s_flags);
8347 	if (error != 0) {
8348 		umem_decr_devlockmem(p);
8349 		kmem_free(p, sizeof (struct ddi_umem_cookie));
8350 		*cookie = (ddi_umem_cookie_t)NULL;
8351 		return (error);
8352 	}
8353 
8354 	/* Initialize the fields in the ddi_umem_cookie */
8355 	p->cvaddr = addr;
8356 	p->type = UMEM_LOCKED;
8357 	p->cook_refcnt = 1;
8358 
8359 	*cookie = (ddi_umem_cookie_t)p;
8360 	return (error);
8361 }
8362 
8363 /*
8364  * Add the cookie to the ddi_umem_unlock list.  Pages will be
8365  * unlocked by i_ddi_umem_unlock_thread.
8366  */
8367 
8368 void
8369 ddi_umem_unlock(ddi_umem_cookie_t cookie)
8370 {
8371 	struct ddi_umem_cookie	*p = (struct ddi_umem_cookie *)cookie;
8372 
8373 	ASSERT(p->type == UMEM_LOCKED);
8374 	ASSERT(CPU_ON_INTR(CPU) == 0); /* cannot be high level */
8375 	ASSERT(ddi_umem_unlock_thread != NULL);
8376 
8377 	p->unl_forw = (struct ddi_umem_cookie *)NULL;	/* end of list */
8378 	/*
8379 	 * Queue the unlock request and notify i_ddi_umem_unlock thread
8380 	 * if it's called in the interrupt context. Otherwise, unlock pages
8381 	 * immediately.
8382 	 */
8383 	if (servicing_interrupt()) {
8384 		/* queue the unlock request and notify the thread */
8385 		mutex_enter(&ddi_umem_unlock_mutex);
8386 		if (ddi_umem_unlock_head == NULL) {
8387 			ddi_umem_unlock_head = ddi_umem_unlock_tail = p;
8388 			cv_broadcast(&ddi_umem_unlock_cv);
8389 		} else {
8390 			ddi_umem_unlock_tail->unl_forw = p;
8391 			ddi_umem_unlock_tail = p;
8392 		}
8393 		mutex_exit(&ddi_umem_unlock_mutex);
8394 	} else {
8395 		/* unlock the pages right away */
8396 		(void) i_ddi_umem_unlock(p);
8397 	}
8398 }
8399 
8400 /*
8401  * Create a buf structure from a ddi_umem_cookie
8402  * cookie - is a ddi_umem_cookie for from ddi_umem_lock and ddi_umem_alloc
8403  *		(only UMEM_LOCKED & KMEM_NON_PAGEABLE types supported)
8404  * off, len - identifies the portion of the memory represented by the cookie
8405  *		that the buf points to.
8406  *	NOTE: off, len need to follow the alignment/size restrictions of the
8407  *		device (dev) that this buf will be passed to. Some devices
8408  *		will accept unrestricted alignment/size, whereas others (such as
8409  *		st) require some block-size alignment/size. It is the caller's
8410  *		responsibility to ensure that the alignment/size restrictions
8411  *		are met (we cannot assert as we do not know the restrictions)
8412  *
8413  * direction - is one of B_READ or B_WRITE and needs to be compatible with
8414  *		the flags used in ddi_umem_lock
8415  *
8416  * The following three arguments are used to initialize fields in the
8417  * buf structure and are uninterpreted by this routine.
8418  *
8419  * dev
8420  * blkno
8421  * iodone
8422  *
8423  * sleepflag - is one of DDI_UMEM_SLEEP or DDI_UMEM_NOSLEEP
8424  *
8425  * Returns a buf structure pointer on success (to be freed by freerbuf)
8426  *	NULL on any parameter error or memory alloc failure
8427  *
8428  */
8429 struct buf *
8430 ddi_umem_iosetup(ddi_umem_cookie_t cookie, off_t off, size_t len,
8431 	int direction, dev_t dev, daddr_t blkno,
8432 	int (*iodone)(struct buf *), int sleepflag)
8433 {
8434 	struct ddi_umem_cookie *p = (struct ddi_umem_cookie *)cookie;
8435 	struct buf *bp;
8436 
8437 	/*
8438 	 * check for valid cookie offset, len
8439 	 */
8440 	if ((off + len) > p->size) {
8441 		return (NULL);
8442 	}
8443 
8444 	if (len > p->size) {
8445 		return (NULL);
8446 	}
8447 
8448 	/* direction has to be one of B_READ or B_WRITE */
8449 	if ((direction != B_READ) && (direction != B_WRITE)) {
8450 		return (NULL);
8451 	}
8452 
8453 	/* These are the only two valid sleepflags */
8454 	if ((sleepflag != DDI_UMEM_SLEEP) && (sleepflag != DDI_UMEM_NOSLEEP)) {
8455 		return (NULL);
8456 	}
8457 
8458 	/*
8459 	 * Only cookies of type UMEM_LOCKED and KMEM_NON_PAGEABLE are supported
8460 	 */
8461 	if ((p->type != UMEM_LOCKED) && (p->type != KMEM_NON_PAGEABLE)) {
8462 		return (NULL);
8463 	}
8464 
8465 	/* If type is KMEM_NON_PAGEABLE procp is NULL */
8466 	ASSERT((p->type == KMEM_NON_PAGEABLE) ?
8467 		(p->procp == NULL) : (p->procp != NULL));
8468 
8469 	bp = kmem_alloc(sizeof (struct buf), sleepflag);
8470 	if (bp == NULL) {
8471 		return (NULL);
8472 	}
8473 	bioinit(bp);
8474 
8475 	bp->b_flags = B_BUSY | B_PHYS | direction;
8476 	bp->b_edev = dev;
8477 	bp->b_lblkno = blkno;
8478 	bp->b_iodone = iodone;
8479 	bp->b_bcount = len;
8480 	bp->b_proc = (proc_t *)p->procp;
8481 	ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
8482 	bp->b_un.b_addr = (caddr_t)((uintptr_t)(p->cvaddr) + off);
8483 	if (p->pparray != NULL) {
8484 		bp->b_flags |= B_SHADOW;
8485 		ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
8486 		bp->b_shadow = p->pparray + btop(off);
8487 	}
8488 	return (bp);
8489 }
8490 
8491 /*
8492  * Fault-handling and related routines
8493  */
8494 
8495 ddi_devstate_t
8496 ddi_get_devstate(dev_info_t *dip)
8497 {
8498 	if (DEVI_IS_DEVICE_OFFLINE(dip))
8499 		return (DDI_DEVSTATE_OFFLINE);
8500 	else if (DEVI_IS_DEVICE_DOWN(dip) || DEVI_IS_BUS_DOWN(dip))
8501 		return (DDI_DEVSTATE_DOWN);
8502 	else if (DEVI_IS_BUS_QUIESCED(dip))
8503 		return (DDI_DEVSTATE_QUIESCED);
8504 	else if (DEVI_IS_DEVICE_DEGRADED(dip))
8505 		return (DDI_DEVSTATE_DEGRADED);
8506 	else
8507 		return (DDI_DEVSTATE_UP);
8508 }
8509 
8510 void
8511 ddi_dev_report_fault(dev_info_t *dip, ddi_fault_impact_t impact,
8512 	ddi_fault_location_t location, const char *message)
8513 {
8514 	struct ddi_fault_event_data fd;
8515 	ddi_eventcookie_t ec;
8516 
8517 	/*
8518 	 * Assemble all the information into a fault-event-data structure
8519 	 */
8520 	fd.f_dip = dip;
8521 	fd.f_impact = impact;
8522 	fd.f_location = location;
8523 	fd.f_message = message;
8524 	fd.f_oldstate = ddi_get_devstate(dip);
8525 
8526 	/*
8527 	 * Get eventcookie from defining parent.
8528 	 */
8529 	if (ddi_get_eventcookie(dip, DDI_DEVI_FAULT_EVENT, &ec) !=
8530 	    DDI_SUCCESS)
8531 		return;
8532 
8533 	(void) ndi_post_event(dip, dip, ec, &fd);
8534 }
8535 
8536 char *
8537 i_ddi_devi_class(dev_info_t *dip)
8538 {
8539 	return (DEVI(dip)->devi_device_class);
8540 }
8541 
8542 int
8543 i_ddi_set_devi_class(dev_info_t *dip, char *devi_class, int flag)
8544 {
8545 	struct dev_info *devi = DEVI(dip);
8546 
8547 	mutex_enter(&devi->devi_lock);
8548 
8549 	if (devi->devi_device_class)
8550 		kmem_free(devi->devi_device_class,
8551 		    strlen(devi->devi_device_class) + 1);
8552 
8553 	if ((devi->devi_device_class = i_ddi_strdup(devi_class, flag))
8554 	    != NULL) {
8555 		mutex_exit(&devi->devi_lock);
8556 		return (DDI_SUCCESS);
8557 	}
8558 
8559 	mutex_exit(&devi->devi_lock);
8560 
8561 	return (DDI_FAILURE);
8562 }
8563 
8564 
8565 /*
8566  * Task Queues DDI interfaces.
8567  */
8568 
8569 /* ARGSUSED */
8570 ddi_taskq_t *
8571 ddi_taskq_create(dev_info_t *dip, const char *name, int nthreads,
8572     pri_t pri, uint_t cflags)
8573 {
8574 	char full_name[TASKQ_NAMELEN];
8575 	const char *tq_name;
8576 	int nodeid = 0;
8577 
8578 	if (dip == NULL)
8579 		tq_name = name;
8580 	else {
8581 		nodeid = ddi_get_instance(dip);
8582 
8583 		if (name == NULL)
8584 			name = "tq";
8585 
8586 		(void) snprintf(full_name, sizeof (full_name), "%s_%s",
8587 		    ddi_driver_name(dip), name);
8588 
8589 		tq_name = full_name;
8590 	}
8591 
8592 	return ((ddi_taskq_t *)taskq_create_instance(tq_name, nodeid, nthreads,
8593 		    pri == TASKQ_DEFAULTPRI ? minclsyspri : pri,
8594 		    nthreads, INT_MAX, TASKQ_PREPOPULATE));
8595 }
8596 
8597 void
8598 ddi_taskq_destroy(ddi_taskq_t *tq)
8599 {
8600 	taskq_destroy((taskq_t *)tq);
8601 }
8602 
8603 int
8604 ddi_taskq_dispatch(ddi_taskq_t *tq, void (* func)(void *),
8605     void *arg, uint_t dflags)
8606 {
8607 	taskqid_t id = taskq_dispatch((taskq_t *)tq, func, arg,
8608 	    dflags == DDI_SLEEP ? TQ_SLEEP : TQ_NOSLEEP);
8609 
8610 	return (id != 0 ? DDI_SUCCESS : DDI_FAILURE);
8611 }
8612 
8613 void
8614 ddi_taskq_wait(ddi_taskq_t *tq)
8615 {
8616 	taskq_wait((taskq_t *)tq);
8617 }
8618 
8619 void
8620 ddi_taskq_suspend(ddi_taskq_t *tq)
8621 {
8622 	taskq_suspend((taskq_t *)tq);
8623 }
8624 
8625 boolean_t
8626 ddi_taskq_suspended(ddi_taskq_t *tq)
8627 {
8628 	return (taskq_suspended((taskq_t *)tq));
8629 }
8630 
8631 void
8632 ddi_taskq_resume(ddi_taskq_t *tq)
8633 {
8634 	taskq_resume((taskq_t *)tq);
8635 }
8636 
8637 int
8638 ddi_parse(
8639 	const char	*ifname,
8640 	char		*alnum,
8641 	uint_t		*nump)
8642 {
8643 	const char	*p;
8644 	int		l;
8645 	ulong_t		num;
8646 	boolean_t	nonum = B_TRUE;
8647 	char		c;
8648 
8649 	l = strlen(ifname);
8650 	for (p = ifname + l; p != ifname; l--) {
8651 		c = *--p;
8652 		if (!isdigit(c)) {
8653 			(void) strlcpy(alnum, ifname, l + 1);
8654 			if (ddi_strtoul(p + 1, NULL, 10, &num) != 0)
8655 				return (DDI_FAILURE);
8656 			break;
8657 		}
8658 		nonum = B_FALSE;
8659 	}
8660 	if (l == 0 || nonum)
8661 		return (DDI_FAILURE);
8662 
8663 	*nump = num;
8664 	return (DDI_SUCCESS);
8665 }
8666