2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <libxml/parser.h>
2N/A#include <fm/libtopo.h>
2N/A#include <topo_alloc.h>
2N/A#include <topo_error.h>
2N/A#include <topo_parse.h>
2N/A#include <topo_subr.h>
2N/A
2N/Atf_info_t *
2N/Atf_info_new(topo_mod_t *mp, xmlDocPtr doc, xmlChar *scheme)
2N/A{
2N/A tf_info_t *r;
2N/A
2N/A if ((r = topo_mod_zalloc(mp, sizeof (tf_info_t))) == NULL) {
2N/A (void) topo_mod_seterrno(mp, ETOPO_NOMEM);
2N/A return (NULL);
2N/A }
2N/A r->tf_flags = TF_LIVE;
2N/A if ((r->tf_scheme = topo_mod_strdup(mp, (char *)scheme)) == NULL) {
2N/A tf_info_free(mp, r);
2N/A (void) topo_mod_seterrno(mp, ETOPO_NOMEM);
2N/A return (NULL);
2N/A }
2N/A r->tf_xdoc = doc;
2N/A return (r);
2N/A}
2N/A
2N/Avoid
2N/Atf_info_free(topo_mod_t *mp, tf_info_t *p)
2N/A{
2N/A if (p->tf_xdoc != NULL)
2N/A xmlFreeDoc(p->tf_xdoc);
2N/A if (p->tf_scheme != NULL)
2N/A topo_mod_strfree(mp, p->tf_scheme);
2N/A tf_rdata_free(mp, p->tf_rd);
2N/A topo_mod_free(mp, p, sizeof (tf_info_t));
2N/A}
2N/A
2N/Atf_rdata_t *
2N/Atf_rdata_new(topo_mod_t *mp, tf_info_t *xinfo, xmlNodePtr n, tnode_t *troot)
2N/A{
2N/A tf_rdata_t *r;
2N/A uint64_t ui;
2N/A xmlChar *name = NULL;
2N/A
2N/A topo_dprintf(mp->tm_hdl, TOPO_DBG_XML, "new rdata\n");
2N/A if ((r = topo_mod_zalloc(mp, sizeof (tf_rdata_t))) == NULL) {
2N/A (void) topo_mod_seterrno(mp, ETOPO_NOMEM);
2N/A return (NULL);
2N/A }
2N/A r->rd_pn = troot;
2N/A if ((name = xmlGetProp(n, (xmlChar *)Name)) == NULL) {
2N/A (void) topo_mod_seterrno(mp, ETOPO_PRSR_NOATTR);
2N/A goto rdata_nogood;
2N/A }
2N/A if ((r->rd_name = topo_mod_strdup(mp, (char *)name)) == NULL) {
2N/A (void) topo_mod_seterrno(mp, ETOPO_NOMEM);
2N/A goto rdata_nogood;
2N/A }
2N/A if (xmlattr_to_int(mp, n, Min, &ui) < 0)
2N/A goto rdata_nogood;
2N/A r->rd_min = (int)ui;
2N/A if (xmlattr_to_int(mp, n, Max, &ui) < 0)
2N/A goto rdata_nogood;
2N/A r->rd_max = (int)ui;
2N/A if (r->rd_min < 0 || r->rd_max < 0 || r->rd_max < r->rd_min) {
2N/A (void) topo_mod_seterrno(mp, ETOPO_PRSR_BADRNG);
2N/A goto rdata_nogood;
2N/A }
2N/A r->rd_finfo = xinfo;
2N/A r->rd_mod = mp;
2N/A
2N/A if (topo_xml_range_process(mp, n, r) < 0)
2N/A goto rdata_nogood;
2N/A
2N/A xmlFree(name);
2N/A return (r);
2N/A
2N/Ardata_nogood:
2N/A if (name != NULL)
2N/A xmlFree(name);
2N/A tf_rdata_free(mp, r);
2N/A return (NULL);
2N/A}
2N/A
2N/Avoid
2N/Atf_rdata_free(topo_mod_t *mp, tf_rdata_t *p)
2N/A{
2N/A if (p == NULL)
2N/A return;
2N/A tf_rdata_free(mp, p->rd_next);
2N/A if (p->rd_name != NULL)
2N/A topo_mod_strfree(mp, p->rd_name);
2N/A tf_edata_free(mp, p->rd_einfo);
2N/A tf_idata_free(mp, p->rd_instances);
2N/A tf_pad_free(mp, p->rd_pad);
2N/A topo_mod_free(mp, p, sizeof (tf_rdata_t));
2N/A}
2N/A
2N/Atf_idata_t *
2N/Atf_idata_new(topo_mod_t *mp, topo_instance_t i, tnode_t *tn)
2N/A{
2N/A tf_idata_t *r;
2N/A
2N/A topo_dprintf(mp->tm_hdl, TOPO_DBG_XML, "new idata %d\n", i);
2N/A if ((r = topo_mod_zalloc(mp, sizeof (tf_idata_t))) == NULL)
2N/A return (NULL);
2N/A r->ti_tn = tn;
2N/A r->ti_i = i;
2N/A return (r);
2N/A}
2N/A
2N/Avoid
2N/Atf_idata_free(topo_mod_t *mp, tf_idata_t *p)
2N/A{
2N/A if (p == NULL)
2N/A return;
2N/A tf_idata_free(mp, p->ti_next);
2N/A tf_pad_free(mp, p->ti_pad);
2N/A topo_mod_free(mp, p, sizeof (tf_idata_t));
2N/A}
2N/A
2N/Aint
2N/Atf_idata_insert(tf_idata_t **head, tf_idata_t *ni)
2N/A{
2N/A tf_idata_t *l, *p;
2N/A
2N/A p = NULL;
2N/A for (l = *head; l != NULL; l = l->ti_next) {
2N/A if (ni->ti_i < l->ti_i)
2N/A break;
2N/A p = l;
2N/A }
2N/A ni->ti_next = l;
2N/A if (p == NULL)
2N/A *head = ni;
2N/A else
2N/A p->ti_next = ni;
2N/A return (0);
2N/A}
2N/A
2N/Atf_idata_t *
2N/Atf_idata_lookup(tf_idata_t *head, topo_instance_t i)
2N/A{
2N/A tf_idata_t *f;
2N/A for (f = head; f != NULL; f = f->ti_next)
2N/A if (i == f->ti_i)
2N/A break;
2N/A return (f);
2N/A}
2N/A
2N/Atf_pad_t *
2N/Atf_pad_new(topo_mod_t *mp, int pcnt, int dcnt)
2N/A{
2N/A tf_pad_t *r;
2N/A
2N/A topo_dprintf(mp->tm_hdl, TOPO_DBG_XML, "new pad p=%d, d=%d\n",
2N/A pcnt, dcnt);
2N/A if ((r = topo_mod_zalloc(mp, sizeof (tf_pad_t))) == NULL)
2N/A return (NULL);
2N/A r->tpad_pgcnt = pcnt;
2N/A r->tpad_dcnt = dcnt;
2N/A return (r);
2N/A}
2N/A
2N/Avoid
2N/Atf_pad_free(topo_mod_t *mp, tf_pad_t *p)
2N/A{
2N/A int n;
2N/A if (p == NULL)
2N/A return;
2N/A if (p->tpad_pgs != NULL) {
2N/A for (n = 0; n < p->tpad_pgcnt; n++)
2N/A if (p->tpad_pgs[n] != NULL)
2N/A nvlist_free(p->tpad_pgs[n]);
2N/A topo_mod_free(mp,
2N/A p->tpad_pgs, p->tpad_pgcnt * sizeof (nvlist_t *));
2N/A }
2N/A tf_rdata_free(mp, p->tpad_child);
2N/A tf_rdata_free(mp, p->tpad_sibs);
2N/A topo_mod_free(mp, p, sizeof (tf_pad_t));
2N/A}
2N/A
2N/Avoid
2N/Atf_edata_free(topo_mod_t *mp, tf_edata_t *p)
2N/A{
2N/A if (p == NULL)
2N/A return;
2N/A if (p->te_name != NULL)
2N/A xmlFree(p->te_name);
2N/A topo_mod_free(mp, p, sizeof (tf_edata_t));
2N/A}