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  matching 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
find_matching_hba(di_node_t node,void * arg)35*10652SHyon.Kim@Sun.COM find_matching_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 				*wa->flag = B_TRUE;
60*10652SHyon.Kim@Sun.COM 			/* Found a node. No need to walk any more. */
61*10652SHyon.Kim@Sun.COM 			di_devfs_path_free(devpath);
62*10652SHyon.Kim@Sun.COM 			return (DI_WALK_TERMINATE);
63*10652SHyon.Kim@Sun.COM 		}
64*10652SHyon.Kim@Sun.COM 		di_devfs_path_free(devpath);
65*10652SHyon.Kim@Sun.COM 	}
66*10652SHyon.Kim@Sun.COM 
67*10652SHyon.Kim@Sun.COM 	return (DI_WALK_CONTINUE);
68*10652SHyon.Kim@Sun.COM }
69*10652SHyon.Kim@Sun.COM 
70*10652SHyon.Kim@Sun.COM /*
71*10652SHyon.Kim@Sun.COM  * Refreshes information about an HBA
72*10652SHyon.Kim@Sun.COM  *
73*10652SHyon.Kim@Sun.COM  * Note: This routine holds the locks in write mode
74*10652SHyon.Kim@Sun.COM  *       during most of the processing, and as such, will cause
75*10652SHyon.Kim@Sun.COM  *	 all other threads to block on entry into the library
76*10652SHyon.Kim@Sun.COM  *	 until the refresh is complete.  An optimization would be
77*10652SHyon.Kim@Sun.COM  *       to put fine-grain locking in for the open_handle structures.
78*10652SHyon.Kim@Sun.COM  */
79*10652SHyon.Kim@Sun.COM void
Sun_sasRefreshAdapterConfiguration()80*10652SHyon.Kim@Sun.COM Sun_sasRefreshAdapterConfiguration()
81*10652SHyon.Kim@Sun.COM {
82*10652SHyon.Kim@Sun.COM 	const char		    ROUTINE[] =
83*10652SHyon.Kim@Sun.COM 	    "Sun_sasRefreshAdapterConfiguration";
84*10652SHyon.Kim@Sun.COM 	struct sun_sas_hba	    *hba_ptr;
85*10652SHyon.Kim@Sun.COM 	di_node_t		    root;
86*10652SHyon.Kim@Sun.COM 	hrtime_t		    start;
87*10652SHyon.Kim@Sun.COM 	hrtime_t		    end;
88*10652SHyon.Kim@Sun.COM 	double			    duration;
89*10652SHyon.Kim@Sun.COM 	walkarg_t		    wa;
90*10652SHyon.Kim@Sun.COM 
91*10652SHyon.Kim@Sun.COM 	/*
92*10652SHyon.Kim@Sun.COM 	 * We're going to be tweaking a lot of global lists, so write
93*10652SHyon.Kim@Sun.COM 	 * lock them.
94*10652SHyon.Kim@Sun.COM 	 */
95*10652SHyon.Kim@Sun.COM 	lock(&all_hbas_lock);
96*10652SHyon.Kim@Sun.COM 	lock(&open_handles_lock);
97*10652SHyon.Kim@Sun.COM 
98*10652SHyon.Kim@Sun.COM 	start = gethrtime();
99*10652SHyon.Kim@Sun.COM 	/* Grab device tree */
100*10652SHyon.Kim@Sun.COM 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
101*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE,
102*10652SHyon.Kim@Sun.COM 		    "Unable to load device tree for reason \"%s\"",
103*10652SHyon.Kim@Sun.COM 		    strerror(errno));
104*10652SHyon.Kim@Sun.COM 		unlock(&open_handles_lock);
105*10652SHyon.Kim@Sun.COM 		unlock(&all_hbas_lock);
106*10652SHyon.Kim@Sun.COM 		return;
107*10652SHyon.Kim@Sun.COM 	}
108*10652SHyon.Kim@Sun.COM 
109*10652SHyon.Kim@Sun.COM 	end = gethrtime();
110*10652SHyon.Kim@Sun.COM 	duration = end - start;
111*10652SHyon.Kim@Sun.COM 	duration /= HR_SECOND;
112*10652SHyon.Kim@Sun.COM 	log(LOG_DEBUG, ROUTINE, "Device tree init took "
113*10652SHyon.Kim@Sun.COM 	    "%.6f seconds", duration);
114*10652SHyon.Kim@Sun.COM 
115*10652SHyon.Kim@Sun.COM 	for (hba_ptr = global_hba_head; hba_ptr != NULL;
116*10652SHyon.Kim@Sun.COM 	    hba_ptr = hba_ptr->next) {
117*10652SHyon.Kim@Sun.COM 		wa.devpath = hba_ptr->device_path;
118*10652SHyon.Kim@Sun.COM 		wa.flag = (boolean_t *)calloc(1, sizeof (boolean_t));
119*10652SHyon.Kim@Sun.COM 		*wa.flag = B_FALSE;
120*10652SHyon.Kim@Sun.COM 
121*10652SHyon.Kim@Sun.COM 		if (di_walk_node(root, DI_WALK_SIBFIRST, &wa,
122*10652SHyon.Kim@Sun.COM 		    find_matching_hba) != 0) {
123*10652SHyon.Kim@Sun.COM 			log(LOG_DEBUG, ROUTINE, "di_walk_node failed.");
124*10652SHyon.Kim@Sun.COM 			unlock(&open_handles_lock);
125*10652SHyon.Kim@Sun.COM 			unlock(&all_hbas_lock);
126*10652SHyon.Kim@Sun.COM 			S_FREE(wa.flag);
127*10652SHyon.Kim@Sun.COM 			di_fini(root);
128*10652SHyon.Kim@Sun.COM 			return;
129*10652SHyon.Kim@Sun.COM 		}
130*10652SHyon.Kim@Sun.COM 
131*10652SHyon.Kim@Sun.COM 		if (*wa.flag == B_FALSE) {
132*10652SHyon.Kim@Sun.COM 			/*
133*10652SHyon.Kim@Sun.COM 			 * Keep the HBA as it is including open handles
134*10652SHyon.Kim@Sun.COM 			 * per the SM-HBA standards. Just mark it as invalid.
135*10652SHyon.Kim@Sun.COM 			 */
136*10652SHyon.Kim@Sun.COM 			log(LOG_DEBUG, ROUTINE, "No matching HBA found. %s",
137*10652SHyon.Kim@Sun.COM 			    hba_ptr->device_path);
138*10652SHyon.Kim@Sun.COM 			hba_ptr->invalid = B_TRUE;
139*10652SHyon.Kim@Sun.COM 		}
140*10652SHyon.Kim@Sun.COM 		S_FREE(wa.flag);
141*10652SHyon.Kim@Sun.COM 	}
142*10652SHyon.Kim@Sun.COM 
143*10652SHyon.Kim@Sun.COM 	/*
144*10652SHyon.Kim@Sun.COM 	 * Now we marked missing HBA(s). Redisoover hbas to refresh
145*10652SHyon.Kim@Sun.COM 	 * or add any new adapter to the hba list.
146*10652SHyon.Kim@Sun.COM 	 * Simply call devtree_get_all_hbas().
147*10652SHyon.Kim@Sun.COM 	 */
148*10652SHyon.Kim@Sun.COM 	if (devtree_get_all_hbas(root) != HBA_STATUS_OK) {
149*10652SHyon.Kim@Sun.COM 		log(LOG_DEBUG, ROUTINE, "devtree_get_all_hbas failed.");
150*10652SHyon.Kim@Sun.COM 	}
151*10652SHyon.Kim@Sun.COM 	di_fini(root);
152*10652SHyon.Kim@Sun.COM 
153*10652SHyon.Kim@Sun.COM 	unlock(&open_handles_lock);
154*10652SHyon.Kim@Sun.COM 	unlock(&all_hbas_lock);
155*10652SHyon.Kim@Sun.COM }
156