17aec1d6eScindi /*
27aec1d6eScindi  * CDDL HEADER START
37aec1d6eScindi  *
47aec1d6eScindi  * The contents of this file are subject to the terms of the
57aec1d6eScindi  * Common Development and Distribution License (the "License").
67aec1d6eScindi  * You may not use this file except in compliance with the License.
77aec1d6eScindi  *
87aec1d6eScindi  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97aec1d6eScindi  * or http://www.opensolaris.org/os/licensing.
107aec1d6eScindi  * See the License for the specific language governing permissions
117aec1d6eScindi  * and limitations under the License.
127aec1d6eScindi  *
137aec1d6eScindi  * When distributing Covered Code, include this CDDL HEADER in each
147aec1d6eScindi  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157aec1d6eScindi  * If applicable, add the following below this CDDL HEADER, with the
167aec1d6eScindi  * fields enclosed by brackets "[]" replaced with your own identifying
177aec1d6eScindi  * information: Portions Copyright [yyyy] [name of copyright owner]
187aec1d6eScindi  *
197aec1d6eScindi  * CDDL HEADER END
207aec1d6eScindi  */
217aec1d6eScindi 
227aec1d6eScindi /*
237aec1d6eScindi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247aec1d6eScindi  * Use is subject to license terms.
257aec1d6eScindi  */
267aec1d6eScindi 
277aec1d6eScindi #include <libxml/parser.h>
287aec1d6eScindi #include <fm/libtopo.h>
297aec1d6eScindi #include <topo_alloc.h>
307aec1d6eScindi #include <topo_error.h>
317aec1d6eScindi #include <topo_parse.h>
32*0eb822a1Scindi #include <topo_subr.h>
337aec1d6eScindi 
347aec1d6eScindi tf_info_t *
tf_info_new(topo_mod_t * mp,xmlDocPtr doc,xmlChar * scheme)35*0eb822a1Scindi tf_info_new(topo_mod_t *mp, xmlDocPtr doc, xmlChar *scheme)
367aec1d6eScindi {
377aec1d6eScindi 	tf_info_t *r;
387aec1d6eScindi 
397aec1d6eScindi 	if ((r = topo_mod_zalloc(mp, sizeof (tf_info_t))) == NULL)
407aec1d6eScindi 		return (NULL);
417aec1d6eScindi 	r->tf_flags = TF_LIVE;
427aec1d6eScindi 	if ((r->tf_scheme = topo_mod_strdup(mp, (char *)scheme)) == NULL) {
437aec1d6eScindi 		tf_info_free(mp, r);
447aec1d6eScindi 		return (NULL);
457aec1d6eScindi 	}
467aec1d6eScindi 	r->tf_xdoc = doc;
477aec1d6eScindi 	return (r);
487aec1d6eScindi }
497aec1d6eScindi 
507aec1d6eScindi void
tf_info_free(topo_mod_t * mp,tf_info_t * p)517aec1d6eScindi tf_info_free(topo_mod_t *mp, tf_info_t *p)
527aec1d6eScindi {
537aec1d6eScindi 	if (p->tf_xdoc != NULL)
547aec1d6eScindi 		xmlFreeDoc(p->tf_xdoc);
557aec1d6eScindi 	if (p->tf_scheme != NULL)
567aec1d6eScindi 		topo_mod_strfree(mp, p->tf_scheme);
577aec1d6eScindi 	tf_rdata_free(mp, p->tf_rd);
587aec1d6eScindi 	topo_mod_free(mp, p, sizeof (tf_info_t));
597aec1d6eScindi }
607aec1d6eScindi 
617aec1d6eScindi tf_rdata_t *
tf_rdata_new(topo_mod_t * mp,tf_info_t * xinfo,xmlNodePtr n,tnode_t * troot)627aec1d6eScindi tf_rdata_new(topo_mod_t *mp, tf_info_t *xinfo, xmlNodePtr n, tnode_t *troot)
637aec1d6eScindi {
647aec1d6eScindi 	tf_rdata_t *r;
657aec1d6eScindi 	uint64_t ui;
667aec1d6eScindi 	xmlChar *name = NULL;
677aec1d6eScindi 
68*0eb822a1Scindi 	topo_dprintf(mp->tm_hdl, TOPO_DBG_XML, "new rdata\n");
697aec1d6eScindi 	if ((r = topo_mod_zalloc(mp, sizeof (tf_rdata_t))) == NULL) {
707aec1d6eScindi 		(void) topo_mod_seterrno(mp, ETOPO_NOMEM);
717aec1d6eScindi 		return (NULL);
727aec1d6eScindi 	}
737aec1d6eScindi 	r->rd_pn = troot;
747aec1d6eScindi 	if ((name = xmlGetProp(n, (xmlChar *)Name)) == NULL) {
757aec1d6eScindi 		(void) topo_mod_seterrno(mp, ETOPO_PRSR_NOATTR);
767aec1d6eScindi 		goto rdata_nogood;
777aec1d6eScindi 	}
787aec1d6eScindi 	if ((r->rd_name = topo_mod_strdup(mp, (char *)name)) == NULL) {
797aec1d6eScindi 		(void) topo_mod_seterrno(mp, ETOPO_NOMEM);
807aec1d6eScindi 		goto rdata_nogood;
817aec1d6eScindi 	}
827aec1d6eScindi 	if (xmlattr_to_int(mp, n, Min, &ui) < 0)
837aec1d6eScindi 		goto rdata_nogood;
847aec1d6eScindi 	r->rd_min = (int)ui;
857aec1d6eScindi 	if (xmlattr_to_int(mp, n, Max, &ui) < 0)
867aec1d6eScindi 		goto rdata_nogood;
877aec1d6eScindi 	r->rd_max = (int)ui;
887aec1d6eScindi 	if (r->rd_min < 0 || r->rd_max < 0 || r->rd_max < r->rd_min) {
897aec1d6eScindi 		(void) topo_mod_seterrno(mp, ETOPO_PRSR_BADRNG);
907aec1d6eScindi 		goto rdata_nogood;
917aec1d6eScindi 	}
927aec1d6eScindi 	r->rd_finfo = xinfo;
937aec1d6eScindi 	r->rd_mod = mp;
947aec1d6eScindi 
957aec1d6eScindi 	if (topo_xml_range_process(mp, n, r) < 0)
967aec1d6eScindi 		goto rdata_nogood;
977aec1d6eScindi 
987aec1d6eScindi 	xmlFree(name);
997aec1d6eScindi 	return (r);
1007aec1d6eScindi 
1017aec1d6eScindi rdata_nogood:
1027aec1d6eScindi 	if (name != NULL)
1037aec1d6eScindi 		xmlFree(name);
1047aec1d6eScindi 	tf_rdata_free(mp, r);
1057aec1d6eScindi 	return (NULL);
1067aec1d6eScindi }
1077aec1d6eScindi 
1087aec1d6eScindi void
tf_rdata_free(topo_mod_t * mp,tf_rdata_t * p)1097aec1d6eScindi tf_rdata_free(topo_mod_t *mp, tf_rdata_t *p)
1107aec1d6eScindi {
1117aec1d6eScindi 	if (p == NULL)
1127aec1d6eScindi 		return;
1137aec1d6eScindi 	tf_rdata_free(mp, p->rd_next);
1147aec1d6eScindi 	if (p->rd_name != NULL)
1157aec1d6eScindi 		topo_mod_strfree(mp, p->rd_name);
1167aec1d6eScindi 	tf_edata_free(mp, p->rd_einfo);
1177aec1d6eScindi 	tf_idata_free(mp, p->rd_instances);
1187aec1d6eScindi 	tf_pad_free(mp, p->rd_pad);
1197aec1d6eScindi 	topo_mod_free(mp, p, sizeof (tf_rdata_t));
1207aec1d6eScindi }
1217aec1d6eScindi 
1227aec1d6eScindi tf_idata_t *
tf_idata_new(topo_mod_t * mp,topo_instance_t i,tnode_t * tn)1237aec1d6eScindi tf_idata_new(topo_mod_t *mp, topo_instance_t i, tnode_t *tn)
1247aec1d6eScindi {
1257aec1d6eScindi 	tf_idata_t *r;
1267aec1d6eScindi 
127*0eb822a1Scindi 	topo_dprintf(mp->tm_hdl, TOPO_DBG_XML, "new idata %d\n", i);
1287aec1d6eScindi 	if ((r = topo_mod_zalloc(mp, sizeof (tf_idata_t))) == NULL)
1297aec1d6eScindi 		return (NULL);
1307aec1d6eScindi 	r->ti_tn = tn;
1317aec1d6eScindi 	r->ti_i = i;
1327aec1d6eScindi 	return (r);
1337aec1d6eScindi }
1347aec1d6eScindi 
1357aec1d6eScindi void
tf_idata_free(topo_mod_t * mp,tf_idata_t * p)1367aec1d6eScindi tf_idata_free(topo_mod_t *mp, tf_idata_t *p)
1377aec1d6eScindi {
1387aec1d6eScindi 	if (p == NULL)
1397aec1d6eScindi 		return;
1407aec1d6eScindi 	tf_idata_free(mp, p->ti_next);
1417aec1d6eScindi 	tf_pad_free(mp, p->ti_pad);
1427aec1d6eScindi 	topo_mod_free(mp, p, sizeof (tf_idata_t));
1437aec1d6eScindi }
1447aec1d6eScindi 
1457aec1d6eScindi int
tf_idata_insert(tf_idata_t ** head,tf_idata_t * ni)146*0eb822a1Scindi tf_idata_insert(tf_idata_t **head, tf_idata_t *ni)
1477aec1d6eScindi {
1487aec1d6eScindi 	tf_idata_t *l, *p;
1497aec1d6eScindi 
1507aec1d6eScindi 	p = NULL;
1517aec1d6eScindi 	for (l = *head; l != NULL; l = l->ti_next) {
1527aec1d6eScindi 		if (ni->ti_i < l->ti_i)
1537aec1d6eScindi 			break;
1547aec1d6eScindi 		p = l;
1557aec1d6eScindi 	}
1567aec1d6eScindi 	ni->ti_next = l;
1577aec1d6eScindi 	if (p == NULL)
1587aec1d6eScindi 		*head = ni;
1597aec1d6eScindi 	else
1607aec1d6eScindi 		p->ti_next = ni;
1617aec1d6eScindi 	return (0);
1627aec1d6eScindi }
1637aec1d6eScindi 
1647aec1d6eScindi tf_idata_t *
tf_idata_lookup(tf_idata_t * head,topo_instance_t i)165*0eb822a1Scindi tf_idata_lookup(tf_idata_t *head, topo_instance_t i)
1667aec1d6eScindi {
1677aec1d6eScindi 	tf_idata_t *f;
1687aec1d6eScindi 	for (f = head; f != NULL; f = f->ti_next)
1697aec1d6eScindi 		if (i == f->ti_i)
1707aec1d6eScindi 			break;
1717aec1d6eScindi 	return (f);
1727aec1d6eScindi }
1737aec1d6eScindi 
1747aec1d6eScindi tf_pad_t *
tf_pad_new(topo_mod_t * mp,int pcnt,int dcnt)1757aec1d6eScindi tf_pad_new(topo_mod_t *mp, int pcnt, int dcnt)
1767aec1d6eScindi {
1777aec1d6eScindi 	tf_pad_t *r;
1787aec1d6eScindi 
179*0eb822a1Scindi 	topo_dprintf(mp->tm_hdl, TOPO_DBG_XML, "new pad p=%d, d=%d\n",
180*0eb822a1Scindi 	    pcnt, dcnt);
1817aec1d6eScindi 	if ((r = topo_mod_zalloc(mp, sizeof (tf_pad_t))) == NULL)
1827aec1d6eScindi 		return (NULL);
1837aec1d6eScindi 	r->tpad_pgcnt = pcnt;
1847aec1d6eScindi 	r->tpad_dcnt = dcnt;
1857aec1d6eScindi 	return (r);
1867aec1d6eScindi }
1877aec1d6eScindi 
1887aec1d6eScindi void
tf_pad_free(topo_mod_t * mp,tf_pad_t * p)1897aec1d6eScindi tf_pad_free(topo_mod_t *mp, tf_pad_t *p)
1907aec1d6eScindi {
1917aec1d6eScindi 	int n;
1927aec1d6eScindi 	if (p == NULL)
1937aec1d6eScindi 		return;
1947aec1d6eScindi 	if (p->tpad_pgs != NULL) {
1957aec1d6eScindi 		for (n = 0; n < p->tpad_pgcnt; n++)
1967aec1d6eScindi 			nvlist_free(p->tpad_pgs[n]);
1977aec1d6eScindi 		topo_mod_free(mp,
1987aec1d6eScindi 		    p->tpad_pgs, p->tpad_pgcnt * sizeof (nvlist_t *));
1997aec1d6eScindi 	}
2007aec1d6eScindi 	tf_rdata_free(mp, p->tpad_child);
2017aec1d6eScindi 	tf_rdata_free(mp, p->tpad_sibs);
2027aec1d6eScindi 	topo_mod_free(mp, p, sizeof (tf_pad_t));
2037aec1d6eScindi }
2047aec1d6eScindi 
2057aec1d6eScindi void
tf_edata_free(topo_mod_t * mp,tf_edata_t * p)2067aec1d6eScindi tf_edata_free(topo_mod_t *mp, tf_edata_t *p)
2077aec1d6eScindi {
2087aec1d6eScindi 	if (p == NULL)
2097aec1d6eScindi 		return;
2107aec1d6eScindi 	if (p->te_name != NULL)
2117aec1d6eScindi 		xmlFree(p->te_name);
2127aec1d6eScindi 	topo_mod_free(mp, p, sizeof (tf_edata_t));
2137aec1d6eScindi }
214