1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include    "sun_sas.h"
28 
29 /*
30  * Discover an HBA node with  mtaching path.
31  * The di_node_t argument should be the root of the device tree.
32  * This routine assumes the locks have been taken
33  */
34 static int
match_smhba_sas_hba(di_node_t node,void * arg)35 match_smhba_sas_hba(di_node_t node, void *arg)
36 {
37 	int *propData, rval;
38 	walkarg_t *wa = (walkarg_t *)arg;
39 	char	*devpath, fulldevpath[MAXPATHLEN];
40 
41 	/* Skip stub(instance -1) nodes */
42 	if (IS_STUB_NODE(node)) {
43 		return (DI_WALK_CONTINUE);
44 	}
45 
46 	rval = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
47 	    "sm-hba-supported", &propData);
48 	if (rval < 0) {
49 		return (DI_WALK_CONTINUE);
50 	} else {
51 		if ((devpath = di_devfs_path(node)) == NULL) {
52 			/* still continue to see if there is matching one. */
53 			return (DI_WALK_CONTINUE);
54 		}
55 		(void) snprintf(fulldevpath, MAXPATHLEN, "%s%s", DEVICES_DIR,
56 		    devpath);
57 
58 		if ((strstr(fulldevpath, wa->devpath)) != NULL) {
59 			/* add the hba to the hba list */
60 			if (devtree_get_one_hba(node) ==
61 			    HBA_STATUS_OK) {
62 				/* succeed to refresh the adapater. */
63 				*wa->flag = B_TRUE;
64 			}
65 			/* Found a node. No need to walk any more. */
66 			di_devfs_path_free(devpath);
67 			return (DI_WALK_TERMINATE);
68 		}
69 		di_devfs_path_free(devpath);
70 	}
71 
72 	return (DI_WALK_CONTINUE);
73 }
74 
75 /*
76  * Refreshes information about an HBA
77  *
78  * Note: This routine holds the locks in write mode
79  *       during most of the processing, and as such, will cause
80  *	 all other threads to block on entry into the library
81  *	 until the refresh is complete.  An optimization would be
82  *       to put fine-grain locking in for the open_handle structures.
83  */
84 void
Sun_sasRefreshInformation(HBA_HANDLE handle)85 Sun_sasRefreshInformation(HBA_HANDLE handle)
86 {
87 	const char		    ROUTINE[] = "Sun_sasRefreshInformation";
88 	struct sun_sas_hba	    *hba_ptr;
89 	struct open_handle	    *oHandle;
90 	di_node_t		    root;
91 	hrtime_t		    start;
92 	hrtime_t		    end;
93 	double			    duration;
94 	walkarg_t		    wa;
95 
96 	/* take a lock for hbas and handles during rerfresh. */
97 	lock(&all_hbas_lock);
98 	lock(&open_handles_lock);
99 
100 	oHandle = RetrieveOpenHandle(handle);
101 	if (oHandle == NULL) {
102 		log(LOG_DEBUG, ROUTINE, "Invalid handle %08lx", handle);
103 		unlock(&open_handles_lock);
104 		unlock(&all_hbas_lock);
105 		return;
106 	}
107 
108 	/* now we know the associated hba exists in the global list. */
109 	start = gethrtime();
110 	/* Grab device tree */
111 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
112 		log(LOG_DEBUG, ROUTINE,
113 		    "Unable to load device tree for reason \"%s\"",
114 		    strerror(errno));
115 		unlock(&open_handles_lock);
116 		unlock(&all_hbas_lock);
117 		return;
118 	}
119 
120 	end = gethrtime();
121 	duration = end - start;
122 	duration /= HR_SECOND;
123 	log(LOG_DEBUG, ROUTINE, "Device tree init took "
124 	    "%.6f seconds", duration);
125 
126 	hba_ptr = RetrieveHandle(oHandle->adapterIndex);
127 	wa.devpath = hba_ptr->device_path;
128 	wa.flag = (boolean_t *)calloc(1, sizeof (boolean_t));
129 	*wa.flag = B_FALSE;
130 
131 	/* found the matching hba node and refresh hba ports and targets. */
132 	if (di_walk_node(root, DI_WALK_SIBFIRST, &wa,
133 	    match_smhba_sas_hba) != 0) {
134 		log(LOG_DEBUG, ROUTINE, "di_walk_node failed.");
135 		unlock(&open_handles_lock);
136 		unlock(&all_hbas_lock);
137 		S_FREE(wa.flag);
138 		di_fini(root);
139 		return;
140 	}
141 
142 	if (*wa.flag != B_TRUE) {
143 		/* no matching HBA. */
144 		log(LOG_DEBUG, ROUTINE, "No matching HBA found.");
145 		unlock(&open_handles_lock);
146 		unlock(&all_hbas_lock);
147 		S_FREE(wa.flag);
148 		di_fini(root);
149 		return;
150 	}
151 
152 	S_FREE(wa.flag);
153 
154 	di_fini(root);
155 
156 	/* All done, release the locks */
157 	unlock(&open_handles_lock);
158 	unlock(&all_hbas_lock);
159 }
160