xref: /illumos-gate/usr/src/cmd/picl/plugins/sun4v/pri/init.c (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <errno.h>
29 #include <malloc.h>
30 #include <mdesc.h>
31 #include <pri.h>
32 #include "priplugin.h"
33 
34 static void pri_free(void *bufp, size_t size);
35 static uint64_t *md_bufp = NULL;
36 static uint64_t *new_md_bufp;
37 
38 int
39 pri_devinit(uint64_t *tok)
40 {
41 	int status;
42 
43 	new_md_bufp = NULL;
44 	status = 0;
45 	if (pri_get(PRI_WAITGET, tok, &new_md_bufp, malloc, pri_free) ==
46 	    (ssize_t)-1) {
47 		pri_debug(LOG_NOTICE, "pri_devinit: can'r read from "
48 		    "the PRI: %d\n", errno);
49 		status = -1;
50 	}
51 	if (new_md_bufp == NULL) {
52 		pri_debug(LOG_NOTICE, "pri_devinit: pri_get returned "
53 		    "NULL buffer!\n");
54 		status = -1;
55 	}
56 	return (status);
57 }
58 
59 md_t *
60 pri_bufinit(md_t *mdp)
61 {
62 
63 	if (mdp)
64 		md_fini(mdp);
65 	if (md_bufp)
66 		free(md_bufp);
67 	md_bufp = new_md_bufp;
68 
69 	pri_debug(LOG_NOTICE, "pri_bufinit: done reading PRI\n");
70 
71 	/*
72 	 * The PRI and the MD use the same data format so they can be
73 	 * parsed by the same functions.
74 	 */
75 	if (md_bufp) {
76 		mdp = md_init_intern(md_bufp, malloc, pri_free);
77 		if (mdp == NULL) {
78 			pri_debug(LOG_NOTICE, "pri_bufinit: md_init_intern "
79 			"failed\n");
80 			free(md_bufp);
81 			md_bufp = NULL;
82 		} else {
83 			pri_debug(LOG_NOTICE, "pri_bufinit: mdi_init_intern "
84 			    "completed successfully\n");
85 		}
86 	} else
87 		mdp = NULL;
88 
89 	pri_debug(LOG_NOTICE, "pri_bufinit: returning\n");
90 
91 	return (mdp);
92 }
93 
94 /*ARGSUSED*/
95 static void
96 pri_free(void *bufp, size_t size)
97 {
98 	if (bufp)
99 		free(bufp);
100 }
101 
102 void
103 pri_devfini(md_t *mdp)
104 {
105 	if (mdp)
106 		(void) md_fini(mdp);
107 
108 	if (md_bufp)
109 		free(md_bufp);
110 	md_bufp = NULL;
111 }
112