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) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * Open connections to the LDOM and Machine Description libraries used during
2N/A * enumeration.
2N/A */
2N/A
2N/A#include <sys/types.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <sys/mdesc.h>
2N/A#include <sys/fm/ldom.h>
2N/A#include <sys/systeminfo.h>
2N/A
2N/A#include "pi_impl.h"
2N/A
2N/Astatic topo_mod_t *Pi_mod;
2N/A
2N/Astatic void pi_free(void *, size_t);
2N/Astatic void * pi_alloc(size_t);
2N/A
2N/A/*
2N/A * Initialize a connection to the LDOM machine description interface.
2N/A */
2N/Aint
2N/Api_ldompri_open(topo_mod_t *mod, pi_enum_t *pip, uint32_t version)
2N/A{
2N/A uint32_t domain_type = 0;
2N/A
2N/A if (mod == NULL || pip == NULL) {
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Store the module pointer for this session. This file-global
2N/A * is used by the allocators called by libldom and libmdesc.
2N/A */
2N/A Pi_mod = mod;
2N/A
2N/A /* Initialize the LDOM connection */
2N/A pip->ldomp = ldom_init(pi_alloc, pi_free);
2N/A if (pip->ldomp == NULL) {
2N/A topo_mod_dprintf(mod,
2N/A "sun4vpi failed to initialize LDOM layer.\n");
2N/A Pi_mod = NULL;
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Initialize the machine description layer for this ldom instance.
2N/A * If this is a guest domain and this module is being called only for
2N/A * authority (pre-T3 systems); then PRI domain services are not
2N/A * available and we must use the local md copy.
2N/A */
2N/A (void) ldom_get_type(pip->ldomp, &domain_type);
2N/A if (((domain_type & LDOM_TYPE_CONTROL) != 0) ||
2N/A (version != TOPO_VERSION_AUTH_ONLY)) {
2N/A pip->ldom_bufsize = ldom_get_core_md(pip->ldomp,
2N/A &(pip->ldom_bufp));
2N/A } else {
2N/A pip->ldom_bufsize = ldom_get_local_md(pip->ldomp,
2N/A &(pip->ldom_bufp));
2N/A }
2N/A if (pip->ldom_bufsize < 1) {
2N/A topo_mod_dprintf(mod, "ldom_get_core_md error: bufsize = %d\n",
2N/A pip->ldom_bufsize);
2N/A ldom_fini(pip->ldomp);
2N/A Pi_mod = NULL;
2N/A return (-1);
2N/A }
2N/A
2N/A /* Initialize the machine description internal layer */
2N/A pip->mdp = md_init_intern(pip->ldom_bufp, pi_alloc, pi_free);
2N/A if (pip->mdp == NULL ||
2N/A (pip->md_nodes = md_node_count(pip->mdp)) < 1) {
2N/A topo_mod_dprintf(mod, "md_init_intern error\n");
2N/A pi_free(pip->ldom_bufp, pip->ldom_bufsize);
2N/A ldom_fini(pip->ldomp);
2N/A Pi_mod = NULL;
2N/A return (-1);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A
2N/A/* ARGSUSED */
2N/Avoid
2N/Api_ldompri_close(topo_mod_t *mod, pi_enum_t *pip)
2N/A{
2N/A if (pip == NULL) {
2N/A return;
2N/A }
2N/A
2N/A /* Close the machine description connection */
2N/A (void) md_fini(pip->mdp);
2N/A
2N/A /* Close the connection to the LDOM layer */
2N/A ldom_fini(pip->ldomp);
2N/A
2N/A /* Free the ldom connection data */
2N/A pi_free(pip->ldom_bufp, pip->ldom_bufsize);
2N/A
2N/A /* Reset the file-global module pointer */
2N/A Pi_mod = NULL;
2N/A}
2N/A
2N/A
2N/Astatic void *
2N/Api_alloc(size_t size)
2N/A{
2N/A if (Pi_mod == NULL) {
2N/A /* Cannot allocate memory without a module pointer */
2N/A return (NULL);
2N/A }
2N/A return (topo_mod_alloc(Pi_mod, size));
2N/A}
2N/A
2N/A
2N/Astatic void
2N/Api_free(void *buf, size_t size)
2N/A{
2N/A if (Pi_mod == NULL) {
2N/A /* Cannot free memory without a module pointer */
2N/A return;
2N/A }
2N/A topo_mod_free(Pi_mod, buf, size);
2N/A}