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