xref: /titanic_51/usr/src/uts/sun4v/os/mach_descrip.c (revision dd965b2f9a9a3b72551dc5bce176bb256273798b)
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 			cmn_err(CE_WARN, "machine_descrip_update: new MD has "
381 			    "the same generation (%ld) as the old MD", tgen);
382 			ret = 0;
383 			goto done;
384 		}
385 
386 		/* check for generations moving backwards */
387 		if (HAS_GEN(tgen) && HAS_GEN(curr_mach_descrip->gen) &&
388 		    (curr_mach_descrip->gen > tgen)) {
389 			cmn_err(CE_WARN, "machine_descrip_update: new MD"
390 			    " older generation (%ld) than current MD (%ld)",
391 			    tgen, curr_mach_descrip->gen);
392 			ret = -1;
393 			goto done;
394 		}
395 
396 		if (curr_mach_descrip->refcnt == 0) {
397 
398 			MDP(("MD: freeing old md buffer gen %ld\n",
399 			    curr_mach_descrip->gen));
400 
401 			/* Free old space */
402 			ASSERT(curr_mach_descrip->space > 0);
403 
404 			(*curr_mach_descrip_memops->buf_freep)
405 			    (curr_mach_descrip->va, curr_mach_descrip->space);
406 		} else {
407 			if (!HAS_GEN(tgen)) {
408 				/*
409 				 * No update support if FW
410 				 * doesn't have MD generation id
411 				 * feature.
412 				 */
413 				prom_printf("WARNING: F/W does not support MD "
414 				    "generation count, MD update failed\n");
415 				ret = -1;
416 				goto done;
417 			}
418 
419 			MDP(("MD: adding to obs list %ld\n",
420 			    curr_mach_descrip->gen));
421 
422 			md_obs_list_add(curr_mach_descrip);
423 
424 			curr_mach_descrip = new_mach_descrip();
425 
426 			if (curr_mach_descrip == NULL) {
427 				panic("Allocation for machine description"
428 				    " failed\n");
429 			}
430 		}
431 	}
432 
433 	curr_mach_descrip->va = tbuf;
434 	curr_mach_descrip->gen = tgen;
435 	curr_mach_descrip->size = md_size;
436 	curr_mach_descrip->space = md_space;
437 
438 #ifdef MACH_DESC_DEBUG
439 	dump_buf((uint8_t *)curr_mach_descrip->va, md_size);
440 #endif /* MACH_DESC_DEBUG */
441 
442 	mutex_exit(&curr_mach_descrip_lock);
443 	return (ret);
444 
445 done:
446 	if (tbuf != NULL)
447 		(*curr_mach_descrip_memops->buf_freep)(tbuf, md_space);
448 	mutex_exit(&curr_mach_descrip_lock);
449 	return (ret);
450 }
451 
452 static void *
453 mach_descrip_buf_alloc(size_t size, size_t align)
454 {
455 	void *p;
456 
457 	if ((p = contig_mem_alloc_align(size, align)) == NULL)
458 		cmn_err(CE_WARN, alloc_fail_msg, size);
459 
460 	return (p);
461 }
462 
463 static void *
464 mach_descrip_strt_meta_alloc(size_t size)
465 {
466 	return (mach_descrip_strt_buf_alloc(size, META_ALLOC_ALIGN));
467 }
468 
469 static void
470 mach_descrip_strt_meta_free(void *buf, size_t size)
471 {
472 	mach_descrip_strt_buf_free(buf, size);
473 }
474 
475 static void *
476 mach_descrip_strt_buf_alloc(size_t size, size_t align)
477 {
478 	void *p = prom_alloc((caddr_t)0, size, align);
479 
480 	if (p == NULL)
481 		prom_printf(alloc_fail_msg, size);
482 
483 	return (p);
484 }
485 
486 static void
487 mach_descrip_strt_buf_free(void *buf, size_t size)
488 {
489 	prom_free((caddr_t)buf, size);
490 }
491 
492 static void *
493 mach_descrip_meta_alloc(size_t size)
494 {
495 	return (kmem_alloc(size, KM_SLEEP));
496 }
497 
498 /*
499  * Initialize the kernel's Machine Description(MD) framework
500  * early on in startup during mlsetup() so consumers
501  * can get to the MD before the VM system has been initialized.
502  *
503  * Also get the most recent version of the MD.
504  */
505 void
506 mach_descrip_startup_init(void)
507 {
508 
509 	mutex_init(&curr_mach_descrip_lock, NULL, MUTEX_DRIVER, NULL);
510 	mutex_init(&obs_list_lock, NULL, MUTEX_DRIVER, NULL);
511 
512 	obs_machine_descrip_list = NULL;
513 
514 	curr_mach_descrip_memops = &startup_memops;
515 
516 	curr_mach_descrip = new_mach_descrip();
517 	if (curr_mach_descrip == NULL)
518 		panic("Allocation for machine description failed\n");
519 
520 	if (mach_descrip_update())
521 		panic("Machine description initialization failed\n");
522 
523 }
524 
525 /*
526  * Counterpart to the above init function.  Free up resources
527  * allocated at startup by mach_descrip_startup_setup().
528  * And reset machine description framework state.
529  *
530  * All consumers must have fini'ed their handles at this point.
531  */
532 void
533 mach_descrip_startup_fini(void)
534 {
535 
536 	ASSERT((curr_mach_descrip != NULL));
537 	ASSERT((curr_mach_descrip->refcnt == 0));
538 	ASSERT((obs_machine_descrip_list == NULL));
539 
540 	destroy_machine_descrip(curr_mach_descrip);
541 	curr_mach_descrip = NULL;
542 	curr_mach_descrip_memops = NULL;
543 }
544 
545 /*
546  * Initialize the kernel's Machine Description(MD) framework
547  * after the the VM system has been initialized.
548  *
549  * Also get the most recent version of the MD.
550  * Assumes that the machine description frame work is in a clean
551  * state and the machine description intialized during startup
552  * has been cleaned up and resources deallocated.
553  */
554 void
555 mach_descrip_init(void)
556 {
557 	ASSERT((curr_mach_descrip == NULL &&
558 	    curr_mach_descrip_memops == NULL));
559 
560 	curr_mach_descrip_memops = &mach_descrip_memops;
561 
562 	curr_mach_descrip = new_mach_descrip();
563 	if (curr_mach_descrip == NULL)
564 		panic("Allocation for machine description failed\n");
565 
566 	if (mach_descrip_update())
567 		panic("Machine description intialization failed\n");
568 
569 	/* read in global params */
570 	init_md_params();
571 }
572 
573 /*
574  * Client interface to get a handle to the current MD.
575  * The md_fini_handle() interface should be used to
576  * clean up the refernce to the MD returned by this function.
577  */
578 md_t *
579 md_get_handle(void)
580 {
581 	md_t *mdp;
582 
583 	mdp = NULL;
584 
585 	mutex_enter(&curr_mach_descrip_lock);
586 
587 	if (curr_mach_descrip != NULL) {
588 
589 		mdp = md_init_intern(curr_mach_descrip->va,
590 		    curr_mach_descrip->memops->meta_allocp,
591 		    curr_mach_descrip->memops->meta_freep);
592 
593 		if (mdp != NULL)
594 			curr_mach_descrip->refcnt++;
595 	}
596 
597 	mutex_exit(&curr_mach_descrip_lock);
598 
599 	return (mdp);
600 }
601 
602 /*
603  * Client interface to clean up the refernce to the MD returned
604  * by md_get_handle().
605  */
606 int
607 md_fini_handle(md_t *ptr)
608 {
609 	machine_descrip_t *mdescp;
610 	md_impl_t *mdp;
611 
612 
613 	mdp = (md_impl_t *)ptr;
614 
615 	if (mdp == NULL)
616 		return (-1);
617 	/*
618 	 * Check if mdp is current MD gen
619 	 */
620 	mutex_enter(&curr_mach_descrip_lock);
621 
622 	if (curr_mach_descrip->gen == mdp->gen) {
623 		curr_mach_descrip->refcnt--;
624 		mutex_exit(&curr_mach_descrip_lock);
625 		goto fini;
626 	}
627 	mutex_exit(&curr_mach_descrip_lock);
628 
629 	/*
630 	 * MD is in the obsolete list
631 	 */
632 	mdescp = md_obs_list_look_up_by_gen(mdp->gen);
633 	if (mdescp == NULL)
634 		return (-1);
635 
636 	mutex_enter(&mdescp->lock);
637 	mdescp->refcnt--;
638 	if (mdescp->refcnt == 0) {
639 		md_obs_list_remove(mdescp);
640 		mutex_exit(&mdescp->lock);
641 		destroy_machine_descrip(mdescp);
642 		goto fini;
643 	}
644 	mutex_exit(&mdescp->lock);
645 
646 fini:
647 	return (md_fini(ptr));
648 }
649 
650 /*
651  * General purpose initialization function used to extract parameters
652  * from the MD during the boot process. This is called immediately after
653  * the in kernel copy of the MD has been initialized so that global
654  * flags are available to various subsystems as they get initialized.
655  */
656 static void
657 init_md_params(void)
658 {
659 	md_t		*mdp;
660 	int		num_nodes;
661 	mde_cookie_t	*listp;
662 	int		listsz;
663 
664 	mdp = md_get_handle();
665 	ASSERT(mdp);
666 	num_nodes = md_node_count(mdp);
667 	ASSERT(num_nodes >= 0);
668 
669 	listsz = num_nodes * sizeof (mde_cookie_t);
670 	listp = (mde_cookie_t *)
671 	    (*curr_mach_descrip_memops->meta_allocp)(listsz);
672 
673 	/*
674 	 * Import various parameters from the MD. For now,
675 	 * the only parameter of interest is whether or not
676 	 * domaining features are supported.
677 	 */
678 	init_domaining_capabilities(mdp, listp);
679 
680 	(*curr_mach_descrip_memops->meta_freep)(listp, listsz);
681 	(void) md_fini_handle(mdp);
682 }
683 
684 static void
685 init_domaining_capabilities(md_t *mdp, mde_cookie_t *listp)
686 {
687 	mde_cookie_t	rootnode;
688 	int		num_nodes;
689 	uint64_t	val = 0;
690 
691 	rootnode = md_root_node(mdp);
692 	ASSERT(rootnode != MDE_INVAL_ELEM_COOKIE);
693 
694 	num_nodes = md_scan_dag(mdp, rootnode, md_find_name(mdp, "platform"),
695 	    md_find_name(mdp, "fwd"), listp);
696 
697 	/* should only be one platform node */
698 	ASSERT(num_nodes == 1);
699 
700 	if (md_get_prop_val(mdp, *listp, "domaining-enabled", &val) != 0) {
701 		/*
702 		 * The property is not present. This implies
703 		 * that the firmware does not support domaining
704 		 * features.
705 		 */
706 		MDP(("'domaining-enabled' property not present\n"));
707 
708 		domaining_capabilities = 0;
709 		return;
710 	}
711 
712 	domaining_capabilities = DOMAINING_SUPPORTED;
713 
714 	if (val == 1) {
715 		if (force_domaining_disabled) {
716 			MDP(("domaining manually disabled\n"));
717 		} else {
718 			domaining_capabilities |= DOMAINING_ENABLED;
719 		}
720 	}
721 
722 	MDP(("domaining_capabilities= 0x%x\n", domaining_capabilities));
723 }
724 
725 /*
726  * Client interface to get a pointer to the raw MD buffer
727  * Private to kernel and mdesc driver.
728  */
729 caddr_t
730 md_get_md_raw(md_t *ptr)
731 {
732 	md_impl_t *mdp;
733 
734 	mdp = (md_impl_t *)ptr;
735 	if (mdp ==  NULL)
736 		return (NULL);
737 	return (mdp->caddr);
738 }
739 
740 /*
741  * This is called before an MD structure is intialized, so
742  * it walks the raw MD looking for the generation property.
743  */
744 static uint64_t
745 mach_descrip_find_md_gen(caddr_t ptr)
746 {
747 	md_header_t	*hdrp;
748 	md_element_t	*mdep;
749 	md_element_t	*rootnode = NULL;
750 	md_element_t	*elem = NULL;
751 	char		*namep;
752 	boolean_t	done;
753 	int		idx;
754 
755 	hdrp = (md_header_t *)ptr;
756 	mdep = (md_element_t *)(ptr + MD_HEADER_SIZE);
757 	namep = (char *)(ptr + MD_HEADER_SIZE + hdrp->node_blk_sz);
758 
759 	/*
760 	 * Very basic check for alignment to avoid
761 	 * bus error issues.
762 	 */
763 	if ((((uint64_t)ptr) & 7) != 0)
764 		return (MDESC_INVAL_GEN);
765 
766 	if (mdtoh32(hdrp->transport_version) != MD_TRANSPORT_VERSION) {
767 		return (MDESC_INVAL_GEN);
768 	}
769 
770 	/*
771 	 * Search for the root node. Perform the walk manually
772 	 * since the MD structure is not set up yet.
773 	 */
774 	for (idx = 0, done = B_FALSE; done == B_FALSE; ) {
775 
776 		md_element_t *np = &(mdep[idx]);
777 
778 		switch (MDE_TAG(np)) {
779 		case MDET_LIST_END:
780 			done = B_TRUE;
781 			break;
782 
783 		case MDET_NODE:
784 			if (strcmp(namep + MDE_NAME(np), "root") == 0) {
785 				/* found root node */
786 				rootnode = np;
787 				done = B_TRUE;
788 				break;
789 			}
790 			idx = MDE_PROP_INDEX(np);
791 			break;
792 
793 		default:
794 			/* ignore */
795 			idx++;
796 		}
797 	}
798 
799 	if (rootnode == NULL) {
800 		/* root not found */
801 		return (MDESC_INVAL_GEN);
802 	}
803 
804 	/* search the rootnode for the generation property */
805 	for (elem = (rootnode + 1); MDE_TAG(elem) != MDET_NODE_END; elem++) {
806 
807 		char *prop_name;
808 
809 		/* generation field is a prop_val */
810 		if (MDE_TAG(elem) != MDET_PROP_VAL)
811 			continue;
812 
813 		prop_name = namep + MDE_NAME(elem);
814 
815 		if (strcmp(prop_name, "md-generation#") == 0) {
816 			return (MDE_PROP_VALUE(elem));
817 		}
818 	}
819 
820 	return (MDESC_INVAL_GEN);
821 }
822 
823 /*
824  * Failed to allocate the list : Return value -1
825  * md_scan_dag API failed      : Return the result from md_scan_dag API
826  */
827 int
828 md_alloc_scan_dag(md_t *ptr,
829 	mde_cookie_t startnode,
830 	char *node_name,
831 	char *dag,
832 	mde_cookie_t **list)
833 {
834 	int res;
835 	md_impl_t *mdp = (md_impl_t *)ptr;
836 
837 	*list = (mde_cookie_t *)mdp->allocp(sizeof (mde_cookie_t) *
838 	    mdp->node_count);
839 	if (*list == NULL)
840 		return (-1);
841 
842 	res = md_scan_dag(ptr, startnode,
843 	    md_find_name(ptr, node_name),
844 	    md_find_name(ptr, dag), *list);
845 
846 	/*
847 	 * If md_scan_dag API returned 0 or -1 then free the buffer
848 	 * and return -1 to indicate the error from this API.
849 	 */
850 	if (res < 1) {
851 		md_free_scan_dag(ptr, list);
852 		*list = NULL;
853 	}
854 
855 	return (res);
856 }
857 
858 void
859 md_free_scan_dag(md_t *ptr,
860 	mde_cookie_t **list)
861 {
862 	md_impl_t *mdp = (md_impl_t *)ptr;
863 
864 	mdp->freep(*list, sizeof (mde_cookie_t) * mdp->node_count);
865 }
866