xref: /titanic_44/usr/src/uts/sun4v/os/mach_descrip.c (revision 34f9b3eef6fdadbda0a846aa4d68691ac40eace5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Kernel Machine Description (MD)
31  *
32  * The Kernel maintains a global copy of the machine description for
33  * the system. This is for use by all kernel subsystems and is exported
34  * to user applications through the the 'mdesc' device driver. It is
35  * initially copied in from the Hypervisor at boot time, but can be
36  * updated dynamically on demand. The Kernel provides an interface
37  * for consumers to obtain a handle to the global MD. Consumers of the
38  * MD must use the specified interfaces. An update interface is provided
39  * for platform services to intiate an MD update on notification by a
40  * service entity.
41  *
42  * Locks
43  * The current global MD is protected by the curr_mach_descrip_lock.
44  * Each Machine description has a lock to synchornize its ref count.
45  * The Obsolete MD list is protected by the obs_list_lock.
46  */
47 
48 #include <sys/machsystm.h>
49 #include <sys/vm.h>
50 #include <sys/cpu.h>
51 #include <sys/intreg.h>
52 #include <sys/machcpuvar.h>
53 #include <sys/machparam.h>
54 #include <vm/hat_sfmmu.h>
55 #include <vm/seg_kmem.h>
56 #include <sys/error.h>
57 #include <sys/hypervisor_api.h>
58 #include <sys/types.h>
59 #include <sys/sysmacros.h>
60 #include <sys/mdesc.h>
61 #include <sys/mdesc_impl.h>
62 #include <sys/mach_descrip.h>
63 #include <sys/prom_plat.h>
64 #include <sys/promif.h>
65 #include <sys/ldoms.h>
66 
67 static void *mach_descrip_strt_meta_alloc(size_t size);
68 static void mach_descrip_strt_meta_free(void *buf, size_t size);
69 static void *mach_descrip_strt_buf_alloc(size_t size, size_t align);
70 static void mach_descrip_strt_buf_free(void *buf, size_t size);
71 static void *mach_descrip_buf_alloc(size_t size, size_t align);
72 static void *mach_descrip_meta_alloc(size_t size);
73 static uint64_t mach_descrip_find_md_gen(caddr_t ptr);
74 static void init_md_params(void);
75 static void init_domaining_capabilities(md_t *mdp, mde_cookie_t *listp);
76 
77 /*
78  * Global ptr of the current generation Machine Description
79  */
80 static machine_descrip_t *curr_mach_descrip;
81 
82 /*
83  * Initialized by machine_descrip_startup_init in startup.
84  * machine_descript_init will reintialize the structure with
85  * the vmem allocators once the vmem is available in the boot up
86  * process.
87  */
88 static machine_descrip_memops_t *curr_mach_descrip_memops = NULL;
89 
90 static machine_descrip_memops_t startup_memops = {
91 	mach_descrip_strt_buf_alloc,
92 	mach_descrip_strt_buf_free,
93 	mach_descrip_strt_meta_alloc,
94 	mach_descrip_strt_meta_free,
95 };
96 
97 static machine_descrip_memops_t mach_descrip_memops = {
98 	mach_descrip_buf_alloc,
99 	contig_mem_free,
100 	mach_descrip_meta_alloc,
101 	kmem_free,
102 };
103 
104 static kmutex_t curr_mach_descrip_lock;
105 /*
106  * List of obsolete Machine Descriptions
107  * Machine descriptions that have users are put on this list
108  * and freed after the last user has called md_fini_handle.
109  */
110 static machine_descrip_t *obs_machine_descrip_list;
111 
112 static kmutex_t obs_list_lock;
113 
114 static const char alloc_fail_msg[] =
115 	"MD: cannot allocate MD buffer of size %ld bytes\n";
116 
117 /*
118  * Global flags that indicate what domaining features are
119  * available, if any. The value is set at boot time based on
120  * the value of the 'domaining-enabled' property in the MD
121  * and the global override flag below. Updates to this
122  * variable after boot are not supported.
123  */
124 uint_t domaining_capabilities;
125 
126 /*
127  * Global override for the 'domaining_capailities' flags. If this
128  * flag is set in /etc/system, domaining features are disabled,
129  * ignoring the value of the 'domaining-enabled' property in
130  * the MD.
131  */
132 uint_t force_domaining_disabled;
133 
134 #define	META_ALLOC_ALIGN	8
135 #define	HAS_GEN(x)	(x != MDESC_INVAL_GEN)
136 
137 #ifdef DEBUG
138 static int mach_descrip_debug = 0;
139 
140 #define	MDP(ARGS)	if (mach_descrip_debug) prom_printf ARGS
141 #define	PRINT_LIST() 	if (mach_descrip_debug) print_obs_list()
142 
143 #ifdef	MACH_DESC_DEBUG
144 static void
145 dump_buf(uint8_t *bufp, int size)
146 {
147 	int i;
148 	for (i = 0; i < size; i += 16) {
149 		int j;
150 		prom_printf("0x%04x :", i);
151 		for (j = 0; j < 16 && (i+j) < size; j++)
152 			prom_printf(" %02x", bufp[i+j]);
153 		prom_printf("\n");
154 	}
155 }
156 #endif /* MACH_DESC_DEBUG */
157 
158 static void
159 print_obs_list(void)
160 {
161 	machine_descrip_t *lmdescp;
162 	mutex_enter(&obs_list_lock);
163 
164 	lmdescp	= obs_machine_descrip_list;
165 	prom_printf("MD_obs_list->");
166 	while (lmdescp != NULL) {
167 		prom_printf("g:%ld,r:%d", lmdescp->gen, lmdescp->refcnt);
168 
169 		lmdescp = lmdescp->next;
170 		prom_printf("->");
171 	}
172 	prom_printf("NULL\n");
173 	mutex_exit(&obs_list_lock);
174 }
175 
176 #else
177 #define	MDP(ARGS)
178 #define	PRINT_LIST()
179 #endif /* DEBUG */
180 
181 /*
182  * MD obsolete list managment functions
183  */
184 static machine_descrip_t *
185 md_obs_list_look_up_by_gen(uint64_t gen)
186 {
187 	machine_descrip_t *mdescp;
188 
189 	mutex_enter(&obs_list_lock);
190 	mdescp = obs_machine_descrip_list;
191 
192 	while (mdescp != NULL) {
193 		if (mdescp->gen == gen) {
194 			mutex_exit(&obs_list_lock);
195 			return (mdescp);
196 		}
197 		mdescp = mdescp->next;
198 	}
199 
200 	mutex_exit(&obs_list_lock);
201 	return (mdescp);
202 }
203 
204 static void
205 md_obs_list_remove(machine_descrip_t *mdescp)
206 {
207 	machine_descrip_t *lmdescp;
208 
209 	mutex_enter(&obs_list_lock);
210 
211 	lmdescp	= obs_machine_descrip_list;
212 
213 	if (obs_machine_descrip_list == mdescp) {
214 		obs_machine_descrip_list = mdescp->next;
215 	} else {
216 		while (lmdescp != NULL) {
217 			if (lmdescp->next == mdescp) {
218 				lmdescp->next = mdescp->next;
219 				mdescp->next = NULL;
220 				break;
221 			}
222 			lmdescp = lmdescp->next;
223 		}
224 	}
225 	mutex_exit(&obs_list_lock);
226 	PRINT_LIST();
227 }
228 
229 static void
230 md_obs_list_add(machine_descrip_t *mdescp)
231 {
232 	mutex_enter(&obs_list_lock);
233 
234 	mdescp->next = obs_machine_descrip_list;
235 	obs_machine_descrip_list = mdescp;
236 
237 	mutex_exit(&obs_list_lock);
238 	PRINT_LIST();
239 }
240 
241 /*
242  * Allocate a machine_descrip meta structure and intitialize it.
243  */
244 static machine_descrip_t *
245 new_mach_descrip(void)
246 {
247 	machine_descrip_t *mdescp;
248 
249 	mdescp = (machine_descrip_t *)(*curr_mach_descrip_memops->meta_allocp)
250 	    (sizeof (machine_descrip_t));
251 	if (mdescp != NULL) {
252 		bzero(mdescp, sizeof (*mdescp));
253 		mdescp->memops = curr_mach_descrip_memops;
254 		mutex_init(&mdescp->lock, NULL, MUTEX_DRIVER, NULL);
255 	}
256 
257 	return (mdescp);
258 }
259 
260 /*
261  * Free a machine_descrip meta structure and intitialize it.
262  * Also free the MD buffer.
263  */
264 static void
265 destroy_machine_descrip(machine_descrip_t *mdescp)
266 {
267 	machine_descrip_memops_t  *mdesc_memopsp;
268 
269 	ASSERT((mdescp != NULL));
270 
271 	mdesc_memopsp = mdescp->memops;
272 	if (mdescp->memops == NULL)
273 		panic("destroy_machine_descrip: memops NULL\n");
274 
275 	(*mdesc_memopsp->buf_freep)(mdescp->va, mdescp->space);
276 	mutex_destroy(&mdescp->lock);
277 	(*mdesc_memopsp->meta_freep)(mdescp, sizeof (*mdescp));
278 }
279 
280 /*
281  * Call into the Hypervisor to retrieve the most recent copy of the
282  * machine description. If references to the current MD are active
283  * stow it in the obsolete MD list and update the current MD reference
284  * with the new one.
285  * The obsolete list contains one MD per generation. If the firmware
286  * doesn't support MD generation fail the call.
287  */
288 int
289 mach_descrip_update(void)
290 {
291 	uint64_t	md_size0, md_size;
292 	uint64_t	md_space = 0;
293 	uint64_t	hvret;
294 	caddr_t		tbuf = NULL;
295 	uint64_t	tbuf_pa;
296 	uint64_t	tgen;
297 	int		ret = 0;
298 
299 	MDP(("MD: Requesting buffer size\n"));
300 
301 	ASSERT((curr_mach_descrip != NULL));
302 
303 	mutex_enter(&curr_mach_descrip_lock);
304 
305 	/*
306 	 * If the required MD size changes between our first call
307 	 * to hv_mach_desc (to find the required buf size) and the
308 	 * second call (to get the actual MD) and our allocated
309 	 * memory is insufficient, loop until we have allocated
310 	 * sufficient space.
311 	 */
312 	do {
313 		if (tbuf != NULL)
314 			(*curr_mach_descrip_memops->buf_freep)(tbuf, md_space);
315 
316 		md_size0 = 0LL;
317 		(void) hv_mach_desc((uint64_t)0, &md_size0);
318 		MDP(("MD: buffer size is %ld\n", md_size0));
319 
320 		/*
321 		 * Align allocated space to nearest page.
322 		 * contig_mem_alloc_align() requires a power of 2 alignment.
323 		 */
324 		md_space = P2ROUNDUP(md_size0, PAGESIZE);
325 		MDP(("MD: allocated space is %ld\n", md_space));
326 
327 		tbuf = (caddr_t)(*curr_mach_descrip_memops->buf_allocp)
328 		    (md_space, PAGESIZE);
329 		if (tbuf == NULL) {
330 			ret = -1;
331 			goto done;
332 		}
333 
334 		tbuf_pa =  va_to_pa(tbuf);
335 		md_size = md_space;
336 		hvret = hv_mach_desc(tbuf_pa, &md_size);
337 		MDP(("MD: HV return code = %ld\n", hvret));
338 
339 		/*
340 		 * We get H_EINVAL if our buffer size is too small. In
341 		 * that case stay in the loop, reallocate the buffer
342 		 * and try again.
343 		 */
344 		if (hvret != H_EOK && hvret != H_EINVAL) {
345 			MDP(("MD: Failed with code %ld from HV\n", hvret));
346 			ret = -1;
347 			goto done;
348 		}
349 
350 	} while (md_space < md_size);
351 
352 	tgen = mach_descrip_find_md_gen(tbuf);
353 
354 #ifdef DEBUG
355 	if (!HAS_GEN(tgen)) {
356 		MDP(("MD: generation number not found\n"));
357 	} else
358 		MDP(("MD: generation number %ld\n", tgen));
359 #endif /* DEBUG */
360 
361 	if (curr_mach_descrip->va != NULL) {
362 
363 		/* check for the same generation number */
364 		if (HAS_GEN(tgen) && ((curr_mach_descrip->gen == tgen) &&
365 		    (curr_mach_descrip->size == md_size))) {
366 #ifdef DEBUG
367 			/*
368 			 * Pedantic Check for generation number. If the
369 			 * generation number is the same, make sure the
370 			 * MDs are really identical.
371 			 */
372 			if (bcmp(curr_mach_descrip->va, tbuf, md_size) != 0) {
373 				cmn_err(CE_WARN, "machine_descrip_update: MDs "
374 				    "with the same generation (%ld) are not "
375 				    "identical", tgen);
376 				ret = -1;
377 				goto done;
378 			}
379 #endif
380 			ret = 0;
381 			goto done;
382 		}
383 
384 		/* check for generations moving backwards */
385 		if (HAS_GEN(tgen) && HAS_GEN(curr_mach_descrip->gen) &&
386 		    (curr_mach_descrip->gen > tgen)) {
387 			cmn_err(CE_WARN, "machine_descrip_update: new MD"
388 			    " older generation (%ld) than current MD (%ld)",
389 			    tgen, curr_mach_descrip->gen);
390 			ret = -1;
391 			goto done;
392 		}
393 
394 		if (curr_mach_descrip->refcnt == 0) {
395 
396 			MDP(("MD: freeing old md buffer gen %ld\n",
397 			    curr_mach_descrip->gen));
398 
399 			/* Free old space */
400 			ASSERT(curr_mach_descrip->space > 0);
401 
402 			(*curr_mach_descrip_memops->buf_freep)
403 			    (curr_mach_descrip->va, curr_mach_descrip->space);
404 		} else {
405 			if (!HAS_GEN(tgen)) {
406 				/*
407 				 * No update support if FW
408 				 * doesn't have MD generation id
409 				 * feature.
410 				 */
411 				prom_printf("WARNING: F/W does not support MD "
412 				    "generation count, MD update failed\n");
413 				ret = -1;
414 				goto done;
415 			}
416 
417 			MDP(("MD: adding to obs list %ld\n",
418 			    curr_mach_descrip->gen));
419 
420 			md_obs_list_add(curr_mach_descrip);
421 
422 			curr_mach_descrip = new_mach_descrip();
423 
424 			if (curr_mach_descrip == NULL) {
425 				panic("Allocation for machine description"
426 				    " failed\n");
427 			}
428 		}
429 	}
430 
431 	curr_mach_descrip->va = tbuf;
432 	curr_mach_descrip->gen = tgen;
433 	curr_mach_descrip->size = md_size;
434 	curr_mach_descrip->space = md_space;
435 
436 #ifdef MACH_DESC_DEBUG
437 	dump_buf((uint8_t *)curr_mach_descrip->va, md_size);
438 #endif /* MACH_DESC_DEBUG */
439 
440 	mutex_exit(&curr_mach_descrip_lock);
441 	return (ret);
442 
443 done:
444 	if (tbuf != NULL)
445 		(*curr_mach_descrip_memops->buf_freep)(tbuf, md_space);
446 	mutex_exit(&curr_mach_descrip_lock);
447 	return (ret);
448 }
449 
450 static void *
451 mach_descrip_buf_alloc(size_t size, size_t align)
452 {
453 	void *p;
454 
455 	if ((p = contig_mem_alloc_align(size, align)) == NULL)
456 		cmn_err(CE_WARN, alloc_fail_msg, size);
457 
458 	return (p);
459 }
460 
461 static void *
462 mach_descrip_strt_meta_alloc(size_t size)
463 {
464 	return (mach_descrip_strt_buf_alloc(size, META_ALLOC_ALIGN));
465 }
466 
467 static void
468 mach_descrip_strt_meta_free(void *buf, size_t size)
469 {
470 	mach_descrip_strt_buf_free(buf, size);
471 }
472 
473 static void *
474 mach_descrip_strt_buf_alloc(size_t size, size_t align)
475 {
476 	void *p = prom_alloc((caddr_t)0, size, align);
477 
478 	if (p == NULL)
479 		prom_printf(alloc_fail_msg, size);
480 
481 	return (p);
482 }
483 
484 static void
485 mach_descrip_strt_buf_free(void *buf, size_t size)
486 {
487 	prom_free((caddr_t)buf, size);
488 }
489 
490 static void *
491 mach_descrip_meta_alloc(size_t size)
492 {
493 	return (kmem_alloc(size, KM_SLEEP));
494 }
495 
496 /*
497  * Initialize the kernel's Machine Description(MD) framework
498  * early on in startup during mlsetup() so consumers
499  * can get to the MD before the VM system has been initialized.
500  *
501  * Also get the most recent version of the MD.
502  */
503 void
504 mach_descrip_startup_init(void)
505 {
506 
507 	mutex_init(&curr_mach_descrip_lock, NULL, MUTEX_DRIVER, NULL);
508 	mutex_init(&obs_list_lock, NULL, MUTEX_DRIVER, NULL);
509 
510 	obs_machine_descrip_list = NULL;
511 
512 	curr_mach_descrip_memops = &startup_memops;
513 
514 	curr_mach_descrip = new_mach_descrip();
515 	if (curr_mach_descrip == NULL)
516 		panic("Allocation for machine description failed\n");
517 
518 	if (mach_descrip_update())
519 		panic("Machine description initialization failed\n");
520 
521 }
522 
523 /*
524  * Counterpart to the above init function.  Free up resources
525  * allocated at startup by mach_descrip_startup_setup().
526  * And reset machine description framework state.
527  *
528  * All consumers must have fini'ed their handles at this point.
529  */
530 void
531 mach_descrip_startup_fini(void)
532 {
533 
534 	ASSERT((curr_mach_descrip != NULL));
535 	ASSERT((curr_mach_descrip->refcnt == 0));
536 	ASSERT((obs_machine_descrip_list == NULL));
537 
538 	destroy_machine_descrip(curr_mach_descrip);
539 	curr_mach_descrip = NULL;
540 	curr_mach_descrip_memops = NULL;
541 }
542 
543 /*
544  * Initialize the kernel's Machine Description(MD) framework
545  * after the the VM system has been initialized.
546  *
547  * Also get the most recent version of the MD.
548  * Assumes that the machine description frame work is in a clean
549  * state and the machine description intialized during startup
550  * has been cleaned up and resources deallocated.
551  */
552 void
553 mach_descrip_init(void)
554 {
555 	ASSERT((curr_mach_descrip == NULL &&
556 	    curr_mach_descrip_memops == NULL));
557 
558 	curr_mach_descrip_memops = &mach_descrip_memops;
559 
560 	curr_mach_descrip = new_mach_descrip();
561 	if (curr_mach_descrip == NULL)
562 		panic("Allocation for machine description failed\n");
563 
564 	if (mach_descrip_update())
565 		panic("Machine description intialization failed\n");
566 
567 	/* read in global params */
568 	init_md_params();
569 }
570 
571 /*
572  * Client interface to get a handle to the current MD.
573  * The md_fini_handle() interface should be used to
574  * clean up the refernce to the MD returned by this function.
575  */
576 md_t *
577 md_get_handle(void)
578 {
579 	md_t *mdp;
580 
581 	mdp = NULL;
582 
583 	mutex_enter(&curr_mach_descrip_lock);
584 
585 	if (curr_mach_descrip != NULL) {
586 
587 		mdp = md_init_intern(curr_mach_descrip->va,
588 		    curr_mach_descrip->memops->meta_allocp,
589 		    curr_mach_descrip->memops->meta_freep);
590 
591 		if (mdp != NULL)
592 			curr_mach_descrip->refcnt++;
593 	}
594 
595 	mutex_exit(&curr_mach_descrip_lock);
596 
597 	return (mdp);
598 }
599 
600 /*
601  * Client interface to clean up the refernce to the MD returned
602  * by md_get_handle().
603  */
604 int
605 md_fini_handle(md_t *ptr)
606 {
607 	machine_descrip_t *mdescp;
608 	md_impl_t *mdp;
609 
610 
611 	mdp = (md_impl_t *)ptr;
612 
613 	if (mdp == NULL)
614 		return (-1);
615 	/*
616 	 * Check if mdp is current MD gen
617 	 */
618 	mutex_enter(&curr_mach_descrip_lock);
619 
620 	if (curr_mach_descrip->gen == mdp->gen) {
621 		curr_mach_descrip->refcnt--;
622 		mutex_exit(&curr_mach_descrip_lock);
623 		goto fini;
624 	}
625 	mutex_exit(&curr_mach_descrip_lock);
626 
627 	/*
628 	 * MD is in the obsolete list
629 	 */
630 	mdescp = md_obs_list_look_up_by_gen(mdp->gen);
631 	if (mdescp == NULL)
632 		return (-1);
633 
634 	mutex_enter(&mdescp->lock);
635 	mdescp->refcnt--;
636 	if (mdescp->refcnt == 0) {
637 		md_obs_list_remove(mdescp);
638 		mutex_exit(&mdescp->lock);
639 		destroy_machine_descrip(mdescp);
640 		goto fini;
641 	}
642 	mutex_exit(&mdescp->lock);
643 
644 fini:
645 	return (md_fini(ptr));
646 }
647 
648 /*
649  * General purpose initialization function used to extract parameters
650  * from the MD during the boot process. This is called immediately after
651  * the in kernel copy of the MD has been initialized so that global
652  * flags are available to various subsystems as they get initialized.
653  */
654 static void
655 init_md_params(void)
656 {
657 	md_t		*mdp;
658 	int		num_nodes;
659 	mde_cookie_t	*listp;
660 	int		listsz;
661 
662 	mdp = md_get_handle();
663 	ASSERT(mdp);
664 	num_nodes = md_node_count(mdp);
665 	ASSERT(num_nodes >= 0);
666 
667 	listsz = num_nodes * sizeof (mde_cookie_t);
668 	listp = (mde_cookie_t *)
669 	    (*curr_mach_descrip_memops->meta_allocp)(listsz);
670 
671 	/*
672 	 * Import various parameters from the MD. For now,
673 	 * the only parameter of interest is whether or not
674 	 * domaining features are supported.
675 	 */
676 	init_domaining_capabilities(mdp, listp);
677 
678 	(*curr_mach_descrip_memops->meta_freep)(listp, listsz);
679 	(void) md_fini_handle(mdp);
680 }
681 
682 static void
683 init_domaining_capabilities(md_t *mdp, mde_cookie_t *listp)
684 {
685 	mde_cookie_t	rootnode;
686 	int		num_nodes;
687 	uint64_t	val = 0;
688 
689 	rootnode = md_root_node(mdp);
690 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
691 
692 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "platform"),
693 	    md_find_name(mdp, "fwd"), listp);
694 
695 	/* should only be one platform node */
696 	ASSERT(num_nodes == 1);
697 
698 	if (md_get_prop_val(mdp, *listp, "domaining-enabled", &val) != 0) {
699 		/*
700 		 * The property is not present. This implies
701 		 * that the firmware does not support domaining
702 		 * features.
703 		 */
704 		MDP(("'domaining-enabled' property not present\n"));
705 
706 		domaining_capabilities = 0;
707 		return;
708 	}
709 
710 	domaining_capabilities = DOMAINING_SUPPORTED;
711 
712 	if (val == 1) {
713 		if (force_domaining_disabled) {
714 			MDP(("domaining manually disabled\n"));
715 		} else {
716 			domaining_capabilities |= DOMAINING_ENABLED;
717 		}
718 	}
719 
720 	MDP(("domaining_capabilities= 0x%x\n", domaining_capabilities));
721 }
722 
723 /*
724  * Client interface to get a pointer to the raw MD buffer
725  * Private to kernel and mdesc driver.
726  */
727 caddr_t
728 md_get_md_raw(md_t *ptr)
729 {
730 	md_impl_t *mdp;
731 
732 	mdp = (md_impl_t *)ptr;
733 	if (mdp ==  NULL)
734 		return (NULL);
735 	return (mdp->caddr);
736 }
737 
738 /*
739  * This is called before an MD structure is intialized, so
740  * it walks the raw MD looking for the generation property.
741  */
742 static uint64_t
743 mach_descrip_find_md_gen(caddr_t ptr)
744 {
745 	md_header_t	*hdrp;
746 	md_element_t	*mdep;
747 	md_element_t	*rootnode = NULL;
748 	md_element_t	*elem = NULL;
749 	char		*namep;
750 	boolean_t	done;
751 	int		idx;
752 
753 	hdrp = (md_header_t *)ptr;
754 	mdep = (md_element_t *)(ptr + MD_HEADER_SIZE);
755 	namep = (char *)(ptr + MD_HEADER_SIZE + hdrp->node_blk_sz);
756 
757 	/*
758 	 * Very basic check for alignment to avoid
759 	 * bus error issues.
760 	 */
761 	if ((((uint64_t)ptr) & 7) != 0)
762 		return (MDESC_INVAL_GEN);
763 
764 	if (mdtoh32(hdrp->transport_version) != MD_TRANSPORT_VERSION) {
765 		return (MDESC_INVAL_GEN);
766 	}
767 
768 	/*
769 	 * Search for the root node. Perform the walk manually
770 	 * since the MD structure is not set up yet.
771 	 */
772 	for (idx = 0, done = B_FALSE; done == B_FALSE; ) {
773 
774 		md_element_t *np = &(mdep[idx]);
775 
776 		switch (MDE_TAG(np)) {
777 		case MDET_LIST_END:
778 			done = B_TRUE;
779 			break;
780 
781 		case MDET_NODE:
782 			if (strcmp(namep + MDE_NAME(np), "root") == 0) {
783 				/* found root node */
784 				rootnode = np;
785 				done = B_TRUE;
786 				break;
787 			}
788 			idx = MDE_PROP_INDEX(np);
789 			break;
790 
791 		default:
792 			/* ignore */
793 			idx++;
794 		}
795 	}
796 
797 	if (rootnode == NULL) {
798 		/* root not found */
799 		return (MDESC_INVAL_GEN);
800 	}
801 
802 	/* search the rootnode for the generation property */
803 	for (elem = (rootnode + 1); MDE_TAG(elem) != MDET_NODE_END; elem++) {
804 
805 		char *prop_name;
806 
807 		/* generation field is a prop_val */
808 		if (MDE_TAG(elem) != MDET_PROP_VAL)
809 			continue;
810 
811 		prop_name = namep + MDE_NAME(elem);
812 
813 		if (strcmp(prop_name, "md-generation#") == 0) {
814 			return (MDE_PROP_VALUE(elem));
815 		}
816 	}
817 
818 	return (MDESC_INVAL_GEN);
819 }
820 
821 /*
822  * Failed to allocate the list : Return value -1
823  * md_scan_dag API failed      : Return the result from md_scan_dag API
824  */
825 int
826 md_alloc_scan_dag(md_t *ptr,
827 	mde_cookie_t startnode,
828 	char *node_name,
829 	char *dag,
830 	mde_cookie_t **list)
831 {
832 	int res;
833 	md_impl_t *mdp = (md_impl_t *)ptr;
834 
835 	*list = (mde_cookie_t *)mdp->allocp(sizeof (mde_cookie_t) *
836 	    mdp->node_count);
837 	if (*list == NULL)
838 		return (-1);
839 
840 	res = md_scan_dag(ptr, startnode,
841 	    md_find_name(ptr, node_name),
842 	    md_find_name(ptr, dag), *list);
843 
844 	/*
845 	 * If md_scan_dag API returned 0 or -1 then free the buffer
846 	 * and return -1 to indicate the error from this API.
847 	 */
848 	if (res < 1) {
849 		md_free_scan_dag(ptr, list);
850 		*list = NULL;
851 	}
852 
853 	return (res);
854 }
855 
856 void
857 md_free_scan_dag(md_t *ptr,
858 	mde_cookie_t **list)
859 {
860 	md_impl_t *mdp = (md_impl_t *)ptr;
861 
862 	mdp->freep(*list, sizeof (mde_cookie_t) * mdp->node_count);
863 }
864