xref: /titanic_41/usr/src/lib/sun_sas/common/Sun_sasLoadLibrary.c (revision 9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bb)
1*9e86db79SHyon Kim /*
2*9e86db79SHyon Kim  * CDDL HEADER START
3*9e86db79SHyon Kim  *
4*9e86db79SHyon Kim  * The contents of this file are subject to the terms of the
5*9e86db79SHyon Kim  * Common Development and Distribution License (the "License").
6*9e86db79SHyon Kim  * You may not use this file except in compliance with the License.
7*9e86db79SHyon Kim  *
8*9e86db79SHyon Kim  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9e86db79SHyon Kim  * or http://www.opensolaris.org/os/licensing.
10*9e86db79SHyon Kim  * See the License for the specific language governing permissions
11*9e86db79SHyon Kim  * and limitations under the License.
12*9e86db79SHyon Kim  *
13*9e86db79SHyon Kim  * When distributing Covered Code, include this CDDL HEADER in each
14*9e86db79SHyon Kim  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9e86db79SHyon Kim  * If applicable, add the following below this CDDL HEADER, with the
16*9e86db79SHyon Kim  * fields enclosed by brackets "[]" replaced with your own identifying
17*9e86db79SHyon Kim  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9e86db79SHyon Kim  *
19*9e86db79SHyon Kim  * CDDL HEADER END
20*9e86db79SHyon Kim  */
21*9e86db79SHyon Kim 
22*9e86db79SHyon Kim /*
23*9e86db79SHyon Kim  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*9e86db79SHyon Kim  * Use is subject to license terms.
25*9e86db79SHyon Kim  */
26*9e86db79SHyon Kim 
27*9e86db79SHyon Kim 
28*9e86db79SHyon Kim #include <sun_sas.h>
29*9e86db79SHyon Kim 
30*9e86db79SHyon Kim /*
31*9e86db79SHyon Kim  * Loads the HBA Library.  Must be called before calling any HBA library
32*9e86db79SHyon Kim  * functions
33*9e86db79SHyon Kim  *
34*9e86db79SHyon Kim  * Return values:
35*9e86db79SHyon Kim  *	HBA_STATUS_OK	    library properly loaded
36*9e86db79SHyon Kim  *	HBA_STATUS_ERROR    library loaded incorrectly
37*9e86db79SHyon Kim  */
38*9e86db79SHyon Kim int	loadCount = 0;
Sun_sasLoadLibrary()39*9e86db79SHyon Kim HBA_STATUS Sun_sasLoadLibrary() {
40*9e86db79SHyon Kim 	const char	ROUTINE[] = "Sun_sasLoadLibrary";
41*9e86db79SHyon Kim 	di_node_t	root;
42*9e86db79SHyon Kim 	boolean_t	atLeastOneHBA = B_FALSE;
43*9e86db79SHyon Kim 	boolean_t	atLeastOneFailure = B_FALSE;
44*9e86db79SHyon Kim 	hrtime_t	    start = 0;
45*9e86db79SHyon Kim 	hrtime_t	    end = 0;
46*9e86db79SHyon Kim 	double		    duration = 0;
47*9e86db79SHyon Kim 
48*9e86db79SHyon Kim 	/* Make sure that library has not been already loaded */
49*9e86db79SHyon Kim 	if (loadCount++ > 0) {
50*9e86db79SHyon Kim 		log(LOG_DEBUG, ROUTINE, "Library already loaded %d time."
51*9e86db79SHyon Kim 		    " Ignoring.", loadCount);
52*9e86db79SHyon Kim 		return (HBA_STATUS_ERROR);
53*9e86db79SHyon Kim 	}
54*9e86db79SHyon Kim 	hba_count = 0;
55*9e86db79SHyon Kim 	open_handle_index = 1;
56*9e86db79SHyon Kim 	/* Initialize the read-write lock */
57*9e86db79SHyon Kim 	if (mutex_init(&all_hbas_lock, USYNC_THREAD, NULL)) {
58*9e86db79SHyon Kim 	    log(LOG_DEBUG, ROUTINE,
59*9e86db79SHyon Kim 		"Unable to initialize lock in LoadLibrary for reason \"%s\"",
60*9e86db79SHyon Kim 		strerror(errno));
61*9e86db79SHyon Kim 	    return (HBA_STATUS_ERROR);
62*9e86db79SHyon Kim 	}
63*9e86db79SHyon Kim 	/* grab write lock */
64*9e86db79SHyon Kim 	lock(&all_hbas_lock);
65*9e86db79SHyon Kim 
66*9e86db79SHyon Kim 	start = gethrtime();
67*9e86db79SHyon Kim 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
68*9e86db79SHyon Kim 	    log(LOG_DEBUG, ROUTINE,
69*9e86db79SHyon Kim 		"Unable to load device tree for reason \"%s\"",
70*9e86db79SHyon Kim 		strerror(errno));
71*9e86db79SHyon Kim 	    unlock(&all_hbas_lock);
72*9e86db79SHyon Kim 	    return (HBA_STATUS_ERROR);
73*9e86db79SHyon Kim 	}
74*9e86db79SHyon Kim 	end = gethrtime();
75*9e86db79SHyon Kim 	duration = end - start;
76*9e86db79SHyon Kim 	duration /= HR_SECOND;
77*9e86db79SHyon Kim 	log(LOG_DEBUG, ROUTINE, "Loading device tree init took "
78*9e86db79SHyon Kim 	    "%.6f seconds", duration);
79*9e86db79SHyon Kim 
80*9e86db79SHyon Kim 	/* At load time, we only gather libdevinfo information */
81*9e86db79SHyon Kim 	if (devtree_get_all_hbas(root) == HBA_STATUS_OK) {
82*9e86db79SHyon Kim 	    atLeastOneHBA = B_TRUE;
83*9e86db79SHyon Kim 	} else {
84*9e86db79SHyon Kim 	    atLeastOneFailure = B_TRUE;
85*9e86db79SHyon Kim 	}
86*9e86db79SHyon Kim 
87*9e86db79SHyon Kim 	di_fini(root);
88*9e86db79SHyon Kim 
89*9e86db79SHyon Kim 	unlock(&all_hbas_lock);
90*9e86db79SHyon Kim 
91*9e86db79SHyon Kim 	/* Now determine what status code to return */
92*9e86db79SHyon Kim 	if (atLeastOneHBA) {
93*9e86db79SHyon Kim 	    /* We've got at least one HBA and possibly some failures */
94*9e86db79SHyon Kim 	    return (HBA_STATUS_OK);
95*9e86db79SHyon Kim 	} else if (atLeastOneFailure) {
96*9e86db79SHyon Kim 	    /* We have no HBAs but have failures */
97*9e86db79SHyon Kim 	    return (HBA_STATUS_ERROR);
98*9e86db79SHyon Kim 	} else {
99*9e86db79SHyon Kim 	    /* We have no HBAs and no failures */
100*9e86db79SHyon Kim 	    return (HBA_STATUS_OK);
101*9e86db79SHyon Kim 	}
102*9e86db79SHyon Kim }
103