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 #include    "sun_sas.h"
28*10652SHyon.Kim@Sun.COM 
29*10652SHyon.Kim@Sun.COM /*
30*10652SHyon.Kim@Sun.COM  * Discover an HBA node with  mtaching path.
31*10652SHyon.Kim@Sun.COM  * The di_node_t argument should be the root of the device tree.
32*10652SHyon.Kim@Sun.COM  * This routine assumes the locks have been taken
33*10652SHyon.Kim@Sun.COM  */
34*10652SHyon.Kim@Sun.COM static int
match_smhba_sas_hba(di_node_t node,void * arg)35*10652SHyon.Kim@Sun.COM match_smhba_sas_hba(di_node_t node, void *arg)
36*10652SHyon.Kim@Sun.COM {
37*10652SHyon.Kim@Sun.COM 	int *propData, rval;
38*10652SHyon.Kim@Sun.COM 	walkarg_t *wa = (walkarg_t *)arg;
39*10652SHyon.Kim@Sun.COM 	char	*devpath, fulldevpath[MAXPATHLEN];
40*10652SHyon.Kim@Sun.COM 
41*10652SHyon.Kim@Sun.COM 	/* Skip stub(instance -1) nodes */
42*10652SHyon.Kim@Sun.COM 	if (IS_STUB_NODE(node)) {
43*10652SHyon.Kim@Sun.COM 		return (DI_WALK_CONTINUE);
44*10652SHyon.Kim@Sun.COM 	}
45*10652SHyon.Kim@Sun.COM 
46*10652SHyon.Kim@Sun.COM 	rval = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
47*10652SHyon.Kim@Sun.COM 	    "sm-hba-supported", &propData);
48*10652SHyon.Kim@Sun.COM 	if (rval < 0) {
49*10652SHyon.Kim@Sun.COM 		return (DI_WALK_CONTINUE);
50*10652SHyon.Kim@Sun.COM 	} else {
51*10652SHyon.Kim@Sun.COM 		if ((devpath = di_devfs_path(node)) == NULL) {
52*10652SHyon.Kim@Sun.COM 			/* still continue to see if there is matching one. */
53*10652SHyon.Kim@Sun.COM 			return (DI_WALK_CONTINUE);
54*10652SHyon.Kim@Sun.COM 		}
55*10652SHyon.Kim@Sun.COM 		(void) snprintf(fulldevpath, MAXPATHLEN, "%s%s", DEVICES_DIR,
56*10652SHyon.Kim@Sun.COM 		    devpath);
57*10652SHyon.Kim@Sun.COM 
58*10652SHyon.Kim@Sun.COM 		if ((strstr(fulldevpath, wa->devpath)) != NULL) {
59*10652SHyon.Kim@Sun.COM 			/* add the hba to the hba list */
60*10652SHyon.Kim@Sun.COM 			if (devtree_get_one_hba(node) ==
61*10652SHyon.Kim@Sun.COM 			    HBA_STATUS_OK) {
62*10652SHyon.Kim@Sun.COM 				/* succeed to refresh the adapater. */
63*10652SHyon.Kim@Sun.COM 				*wa->flag = B_TRUE;
64*10652SHyon.Kim@Sun.COM 			}
65*10652SHyon.Kim@Sun.COM 			/* Found a node. No need to walk any more. */
66*10652SHyon.Kim@Sun.COM 			di_devfs_path_free(devpath);
67*10652SHyon.Kim@Sun.COM 			return (DI_WALK_TERMINATE);
68*10652SHyon.Kim@Sun.COM 		}
69*10652SHyon.Kim@Sun.COM 		di_devfs_path_free(devpath);
70*10652SHyon.Kim@Sun.COM 	}
71*10652SHyon.Kim@Sun.COM 
72*10652SHyon.Kim@Sun.COM 	return (DI_WALK_CONTINUE);
73*10652SHyon.Kim@Sun.COM }
74*10652SHyon.Kim@Sun.COM 
75*10652SHyon.Kim@Sun.COM /*
76*10652SHyon.Kim@Sun.COM  * Refreshes information about an HBA
77*10652SHyon.Kim@Sun.COM  *
78*10652SHyon.Kim@Sun.COM  * Note: This routine holds the locks in write mode
79*10652SHyon.Kim@Sun.COM  *       during most of the processing, and as such, will cause
80*10652SHyon.Kim@Sun.COM  *	 all other threads to block on entry into the library
81*10652SHyon.Kim@Sun.COM  *	 until the refresh is complete.  An optimization would be
82*10652SHyon.Kim@Sun.COM  *       to put fine-grain locking in for the open_handle structures.
83*10652SHyon.Kim@Sun.COM  */
84*10652SHyon.Kim@Sun.COM void
Sun_sasRefreshInformation(HBA_HANDLE handle)85*10652SHyon.Kim@Sun.COM Sun_sasRefreshInformation(HBA_HANDLE handle)
86*10652SHyon.Kim@Sun.COM {
87*10652SHyon.Kim@Sun.COM 	const char		    ROUTINE[] = "Sun_sasRefreshInformation";
88*10652SHyon.Kim@Sun.COM 	struct sun_sas_hba	    *hba_ptr;
89*10652SHyon.Kim@Sun.COM 	struct open_handle	    *oHandle;
90*10652SHyon.Kim@Sun.COM 	di_node_t		    root;
91*10652SHyon.Kim@Sun.COM 	hrtime_t		    start;
92*10652SHyon.Kim@Sun.COM 	hrtime_t		    end;
93*10652SHyon.Kim@Sun.COM 	double			    duration;
94*10652SHyon.Kim@Sun.COM 	walkarg_t		    wa;
95*10652SHyon.Kim@Sun.COM 
96*10652SHyon.Kim@Sun.COM 	/* take a lock for hbas and handles during rerfresh. */
97*10652SHyon.Kim@Sun.COM 	lock(&all_hbas_lock);
98*10652SHyon.Kim@Sun.COM 	lock(&open_handles_lock);
99*10652SHyon.Kim@Sun.COM 
100*10652SHyon.Kim@Sun.COM 	oHandle = RetrieveOpenHandle(handle);
101*10652SHyon.Kim@Sun.COM 	if (oHandle == NULL) {
102*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx", handle);
103*10652SHyon.Kim@Sun.COM 		unlock(&open_handles_lock);
104*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
105*10652SHyon.Kim@Sun.COM 		return;
106*10652SHyon.Kim@Sun.COM 	}
107*10652SHyon.Kim@Sun.COM 
108*10652SHyon.Kim@Sun.COM 	/* now we know the associated hba exists in the global list. */
109*10652SHyon.Kim@Sun.COM 	start = gethrtime();
110*10652SHyon.Kim@Sun.COM 	/* Grab device tree */
111*10652SHyon.Kim@Sun.COM 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
112*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
113*10652SHyon.Kim@Sun.COM 		    "Unable to load device tree for reason \"%s\"",
114*10652SHyon.Kim@Sun.COM 		    strerror(errno));
115*10652SHyon.Kim@Sun.COM 		unlock(&open_handles_lock);
116*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
117*10652SHyon.Kim@Sun.COM 		return;
118*10652SHyon.Kim@Sun.COM 	}
119*10652SHyon.Kim@Sun.COM 
120*10652SHyon.Kim@Sun.COM 	end = gethrtime();
121*10652SHyon.Kim@Sun.COM 	duration = end - start;
122*10652SHyon.Kim@Sun.COM 	duration /= HR_SECOND;
123*10652SHyon.Kim@Sun.COM 	log(LOG_DEBUG, ROUTINE, "Device tree init took "
124*10652SHyon.Kim@Sun.COM 	    "%.6f seconds", duration);
125*10652SHyon.Kim@Sun.COM 
126*10652SHyon.Kim@Sun.COM 	hba_ptr = RetrieveHandle(oHandle->adapterIndex);
127*10652SHyon.Kim@Sun.COM 	wa.devpath = hba_ptr->device_path;
128*10652SHyon.Kim@Sun.COM 	wa.flag = (boolean_t *)calloc(1, sizeof (boolean_t));
129*10652SHyon.Kim@Sun.COM 	*wa.flag = B_FALSE;
130*10652SHyon.Kim@Sun.COM 
131*10652SHyon.Kim@Sun.COM 	/* found the matching hba node and refresh hba ports and targets. */
132*10652SHyon.Kim@Sun.COM 	if (di_walk_node(root, DI_WALK_SIBFIRST, &wa,
133*10652SHyon.Kim@Sun.COM 	    match_smhba_sas_hba) != 0) {
134*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE, "di_walk_node failed.");
135*10652SHyon.Kim@Sun.COM 		unlock(&open_handles_lock);
136*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
137*10652SHyon.Kim@Sun.COM 		S_FREE(wa.flag);
138*10652SHyon.Kim@Sun.COM 		di_fini(root);
139*10652SHyon.Kim@Sun.COM 		return;
140*10652SHyon.Kim@Sun.COM 	}
141*10652SHyon.Kim@Sun.COM 
142*10652SHyon.Kim@Sun.COM 	if (*wa.flag != B_TRUE) {
143*10652SHyon.Kim@Sun.COM 		/* no matching HBA. */
144*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE, "No matching HBA found.");
145*10652SHyon.Kim@Sun.COM 		unlock(&open_handles_lock);
146*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
147*10652SHyon.Kim@Sun.COM 		S_FREE(wa.flag);
148*10652SHyon.Kim@Sun.COM 		di_fini(root);
149*10652SHyon.Kim@Sun.COM 		return;
150*10652SHyon.Kim@Sun.COM 	}
151*10652SHyon.Kim@Sun.COM 
152*10652SHyon.Kim@Sun.COM 	S_FREE(wa.flag);
153*10652SHyon.Kim@Sun.COM 
154*10652SHyon.Kim@Sun.COM 	di_fini(root);
155*10652SHyon.Kim@Sun.COM 
156*10652SHyon.Kim@Sun.COM 	/* All done, release the locks */
157*10652SHyon.Kim@Sun.COM 	unlock(&open_handles_lock);
158*10652SHyon.Kim@Sun.COM 	unlock(&all_hbas_lock);
159*10652SHyon.Kim@Sun.COM }
160