9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim/*
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * CDDL HEADER START
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * The contents of this file are subject to the terms of the
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Common Development and Distribution License (the "License").
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * You may not use this file except in compliance with the License.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * or http://www.opensolaris.org/os/licensing.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * See the License for the specific language governing permissions
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * and limitations under the License.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * When distributing Covered Code, include this CDDL HEADER in each
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * If applicable, add the following below this CDDL HEADER, with the
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * fields enclosed by brackets "[]" replaced with your own identifying
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * information: Portions Copyright [yyyy] [name of copyright owner]
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * CDDL HEADER END
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim/*
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Use is subject to license terms.
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim#include <sun_sas.h>
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim/*
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Loads the HBA Library. Must be called before calling any HBA library
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * functions
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim *
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * Return values:
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * HBA_STATUS_OK library properly loaded
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim * HBA_STATUS_ERROR library loaded incorrectly
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kimint loadCount = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon KimHBA_STATUS Sun_sasLoadLibrary() {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim const char ROUTINE[] = "Sun_sasLoadLibrary";
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim di_node_t root;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim boolean_t atLeastOneHBA = B_FALSE;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim boolean_t atLeastOneFailure = B_FALSE;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hrtime_t start = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hrtime_t end = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim double duration = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Make sure that library has not been already loaded */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (loadCount++ > 0) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE, "Library already loaded %d time."
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim " Ignoring.", loadCount);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim hba_count = 0;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim open_handle_index = 1;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Initialize the read-write lock */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (mutex_init(&all_hbas_lock, USYNC_THREAD, NULL)) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Unable to initialize lock in LoadLibrary for reason \"%s\"",
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim strerror(errno));
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* grab write lock */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim lock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim start = gethrtime();
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE,
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "Unable to load device tree for reason \"%s\"",
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim strerror(errno));
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim end = gethrtime();
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim duration = end - start;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim duration /= HR_SECOND;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim log(LOG_DEBUG, ROUTINE, "Loading device tree init took "
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim "%.6f seconds", duration);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* At load time, we only gather libdevinfo information */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (devtree_get_all_hbas(root) == HBA_STATUS_OK) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim atLeastOneHBA = B_TRUE;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim } else {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim atLeastOneFailure = B_TRUE;
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim di_fini(root);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim unlock(&all_hbas_lock);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* Now determine what status code to return */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim if (atLeastOneHBA) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* We've got at least one HBA and possibly some failures */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_OK);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim } else if (atLeastOneFailure) {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* We have no HBAs but have failures */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_ERROR);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim } else {
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim /* We have no HBAs and no failures */
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim return (HBA_STATUS_OK);
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim }
9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bbHyon Kim}