xref: /onnv-gate/usr/src/cmd/picl/plugins/sun4v/pri/init.c (revision 5029:ffbab30c82bd)
13941Svenki /*
23941Svenki  * CDDL HEADER START
33941Svenki  *
43941Svenki  * The contents of this file are subject to the terms of the
53941Svenki  * Common Development and Distribution License (the "License").
63941Svenki  * You may not use this file except in compliance with the License.
73941Svenki  *
83941Svenki  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93941Svenki  * or http://www.opensolaris.org/os/licensing.
103941Svenki  * See the License for the specific language governing permissions
113941Svenki  * and limitations under the License.
123941Svenki  *
133941Svenki  * When distributing Covered Code, include this CDDL HEADER in each
143941Svenki  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153941Svenki  * If applicable, add the following below this CDDL HEADER, with the
163941Svenki  * fields enclosed by brackets "[]" replaced with your own identifying
173941Svenki  * information: Portions Copyright [yyyy] [name of copyright owner]
183941Svenki  *
193941Svenki  * CDDL HEADER END
203941Svenki  */
213941Svenki 
223941Svenki /*
233941Svenki  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
243941Svenki  * Use is subject to license terms.
253941Svenki  */
263941Svenki 
273941Svenki #pragma ident	"%Z%%M%	%I%	%E% SMI"
283941Svenki 
293941Svenki #include <sys/types.h>
303941Svenki #include <errno.h>
313941Svenki #include <malloc.h>
323941Svenki #include <mdesc.h>
333941Svenki #include <pri.h>
343941Svenki #include "priplugin.h"
353941Svenki 
363941Svenki static void pri_free(void *bufp, size_t size);
374669Sfw157321 static uint64_t *md_bufp = NULL;
38*5029Sfw157321 static uint64_t *new_md_bufp;
393941Svenki 
40*5029Sfw157321 int
pri_devinit(uint64_t * tok)41*5029Sfw157321 pri_devinit(uint64_t *tok)
423941Svenki {
43*5029Sfw157321 	int status;
443941Svenki 
45*5029Sfw157321 	new_md_bufp = NULL;
46*5029Sfw157321 	status = 0;
474669Sfw157321 	if (pri_get(PRI_WAITGET, tok, &new_md_bufp, malloc, pri_free) ==
484669Sfw157321 	    (ssize_t)-1) {
494669Sfw157321 		pri_debug(LOG_NOTICE, "pri_devinit: can'r read from "
504669Sfw157321 		    "the PRI: %d\n", errno);
51*5029Sfw157321 		status = -1;
523941Svenki 	}
534669Sfw157321 	if (new_md_bufp == NULL) {
54*5029Sfw157321 		pri_debug(LOG_NOTICE, "pri_devinit: pri_get returned "
554669Sfw157321 		    "NULL buffer!\n");
56*5029Sfw157321 		status = -1;
574669Sfw157321 	}
58*5029Sfw157321 	return (status);
59*5029Sfw157321 }
60*5029Sfw157321 
61*5029Sfw157321 md_t *
pri_bufinit(md_t * mdp)62*5029Sfw157321 pri_bufinit(md_t *mdp)
63*5029Sfw157321 {
64*5029Sfw157321 
654669Sfw157321 	if (mdp)
664669Sfw157321 		md_fini(mdp);
674669Sfw157321 	if (md_bufp)
684669Sfw157321 		free(md_bufp);
694669Sfw157321 	md_bufp = new_md_bufp;
703941Svenki 
71*5029Sfw157321 	pri_debug(LOG_NOTICE, "pri_bufinit: done reading PRI\n");
723941Svenki 
733941Svenki 	/*
743941Svenki 	 * The PRI and the MD use the same data format so they can be
753941Svenki 	 * parsed by the same functions.
763941Svenki 	 */
773941Svenki 	if (md_bufp) {
783941Svenki 		mdp = md_init_intern(md_bufp, malloc, pri_free);
793941Svenki 		if (mdp == NULL) {
80*5029Sfw157321 			pri_debug(LOG_NOTICE, "pri_bufinit: md_init_intern "
813941Svenki 			"failed\n");
823941Svenki 			free(md_bufp);
833941Svenki 			md_bufp = NULL;
843941Svenki 		} else {
85*5029Sfw157321 			pri_debug(LOG_NOTICE, "pri_bufinit: mdi_init_intern "
863941Svenki 			    "completed successfully\n");
873941Svenki 		}
883941Svenki 	} else
893941Svenki 		mdp = NULL;
903941Svenki 
91*5029Sfw157321 	pri_debug(LOG_NOTICE, "pri_bufinit: returning\n");
923941Svenki 
933941Svenki 	return (mdp);
943941Svenki }
953941Svenki 
963941Svenki /*ARGSUSED*/
973941Svenki static void
pri_free(void * bufp,size_t size)983941Svenki pri_free(void *bufp, size_t size)
993941Svenki {
1003941Svenki 	if (bufp)
1013941Svenki 		free(bufp);
1023941Svenki }
1033941Svenki 
1043941Svenki void
pri_devfini(md_t * mdp)1053941Svenki pri_devfini(md_t *mdp)
1063941Svenki {
1073941Svenki 	if (mdp)
1083941Svenki 		(void) md_fini(mdp);
1093941Svenki 
1103941Svenki 	if (md_bufp)
1113941Svenki 		free(md_bufp);
1123941Svenki 	md_bufp = NULL;
1133941Svenki }
114