xref: /titanic_52/usr/src/cmd/picl/plugins/sun4v/mdesc/init.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <alloca.h>
34 #include <sys/stat.h>
35 #include <malloc.h>
36 #include <fcntl.h>
37 #include <syslog.h>
38 #include <mdesc.h>
39 #include <string.h>
40 #include <errno.h>
41 
42 #define	MDESC_PATH	"/devices/pseudo/mdesc@0:mdesc"
43 
44 static void mdesc_free(void *bufp, size_t size);
45 uint64_t *md_bufp;
46 
47 md_t *
48 mdesc_devinit(void)
49 {
50 	int fd;
51 	md_t *mdp;
52 	size_t size;
53 
54 	/*
55 	 * We haven't finished using the previous MD/PRI info.
56 	 */
57 	if (md_bufp != NULL)
58 		return (NULL);
59 
60 	do {
61 		if ((fd = open(MDESC_PATH, O_RDONLY, 0)) < 0)
62 			break;
63 
64 		if (ioctl(fd, MDESCIOCGSZ, &size) < 0)
65 			break;
66 		if ((md_bufp = (uint64_t *)malloc(size)) == NULL) {
67 			(void) close(fd);
68 			break;
69 		}
70 
71 		/*
72 		 * A partial read is as bad as a failed read.
73 		 */
74 		if (read(fd, md_bufp, size) != size) {
75 			free(md_bufp);
76 			md_bufp = NULL;
77 		}
78 
79 		(void) close(fd);
80 	/*LINTED: E_CONSTANT_CONDITION */
81 	} while (0);
82 
83 	if (md_bufp) {
84 		mdp = md_init_intern(md_bufp, malloc, mdesc_free);
85 		if (mdp == NULL) {
86 			free(md_bufp);
87 			md_bufp = NULL;
88 		}
89 	} else
90 		mdp = NULL;
91 
92 	return (mdp);
93 }
94 
95 /*ARGSUSED*/
96 void
97 mdesc_free(void *bufp, size_t size)
98 {
99 	if (bufp)
100 		free(bufp);
101 }
102 
103 void
104 mdesc_devfini(md_t *mdp)
105 {
106 	if (mdp)
107 		(void) md_fini(mdp);
108 
109 	if (md_bufp)
110 		free(md_bufp);
111 	md_bufp = NULL;
112 }
113