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