xref: /titanic_50/usr/src/cmd/picl/plugins/sun4v/mdesc/init.c (revision 0d63ce2b32a9e1cc8ed71d4d92536c44d66a530a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51ae08745Sheppo  * Common Development and Distribution License (the "License").
61ae08745Sheppo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
211ae08745Sheppo 
227c478bd9Sstevel@tonic-gate /*
23*0d63ce2bSvenki  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <alloca.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <malloc.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <syslog.h>
387c478bd9Sstevel@tonic-gate #include <mdesc.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	MDESC_PATH	"/devices/pseudo/mdesc@0:mdesc"
437c478bd9Sstevel@tonic-gate 
4426cf27f0Sla135387 static void mdesc_free(void *bufp, size_t size);
45*0d63ce2bSvenki uint64_t *md_bufp;
4626cf27f0Sla135387 
477c478bd9Sstevel@tonic-gate md_t *
mdesc_devinit(void)487c478bd9Sstevel@tonic-gate mdesc_devinit(void)
497c478bd9Sstevel@tonic-gate {
50*0d63ce2bSvenki 	int fd;
517c478bd9Sstevel@tonic-gate 	md_t *mdp;
52*0d63ce2bSvenki 	size_t size;
537c478bd9Sstevel@tonic-gate 
54*0d63ce2bSvenki 	/*
55*0d63ce2bSvenki 	 * We haven't finished using the previous MD/PRI info.
56*0d63ce2bSvenki 	 */
571ae08745Sheppo 	if (md_bufp != NULL)
581ae08745Sheppo 		return (NULL);
591ae08745Sheppo 
607c478bd9Sstevel@tonic-gate 	do {
61*0d63ce2bSvenki 		if ((fd = open(MDESC_PATH, O_RDONLY, 0)) < 0)
62*0d63ce2bSvenki 			break;
637c478bd9Sstevel@tonic-gate 
64*0d63ce2bSvenki 		if (ioctl(fd, MDESCIOCGSZ, &size) < 0)
65*0d63ce2bSvenki 			break;
66*0d63ce2bSvenki 		if ((md_bufp = (uint64_t *)malloc(size)) == NULL) {
67*0d63ce2bSvenki 			(void) close(fd);
68*0d63ce2bSvenki 			break;
697c478bd9Sstevel@tonic-gate 		}
707c478bd9Sstevel@tonic-gate 
71*0d63ce2bSvenki 		/*
72*0d63ce2bSvenki 		 * A partial read is as bad as a failed read.
73*0d63ce2bSvenki 		 */
74*0d63ce2bSvenki 		if (read(fd, md_bufp, size) != size) {
751ae08745Sheppo 			free(md_bufp);
76*0d63ce2bSvenki 			md_bufp = NULL;
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 
79*0d63ce2bSvenki 		(void) close(fd);
80*0d63ce2bSvenki 	/*LINTED: E_CONSTANT_CONDITION */
81*0d63ce2bSvenki 	} while (0);
827c478bd9Sstevel@tonic-gate 
83*0d63ce2bSvenki 	if (md_bufp) {
84*0d63ce2bSvenki 		mdp = md_init_intern(md_bufp, malloc, mdesc_free);
85*0d63ce2bSvenki 		if (mdp == NULL) {
861ae08745Sheppo 			free(md_bufp);
87*0d63ce2bSvenki 			md_bufp = NULL;
887c478bd9Sstevel@tonic-gate 		}
89*0d63ce2bSvenki 	} else
90*0d63ce2bSvenki 		mdp = NULL;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	return (mdp);
937c478bd9Sstevel@tonic-gate }
9426cf27f0Sla135387 
9526cf27f0Sla135387 /*ARGSUSED*/
9626cf27f0Sla135387 void
mdesc_free(void * bufp,size_t size)9726cf27f0Sla135387 mdesc_free(void *bufp, size_t size)
9826cf27f0Sla135387 {
991ae08745Sheppo 	if (bufp)
10026cf27f0Sla135387 		free(bufp);
10126cf27f0Sla135387 }
1021ae08745Sheppo 
1031ae08745Sheppo void
mdesc_devfini(md_t * mdp)1041ae08745Sheppo mdesc_devfini(md_t *mdp)
1051ae08745Sheppo {
1061ae08745Sheppo 	if (mdp)
1071ae08745Sheppo 		(void) md_fini(mdp);
1081ae08745Sheppo 
1091ae08745Sheppo 	if (md_bufp)
1101ae08745Sheppo 		free(md_bufp);
1111ae08745Sheppo 	md_bufp = NULL;
1121ae08745Sheppo }
113