xref: /illumos-gate/usr/src/lib/sun_sas/common/Sun_sasRefreshAdapterConfiguration.c (revision 3665ce8aeee26b1a84fb98951ef011e0779e1ae2)
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  * Copyright 2019 Joyent, Inc.
28  */
29 
30 #include    <sun_sas.h>
31 
32 /*
33  * Discover an HBA node with  matching path.
34  * The di_node_t argument should be the root of the device tree.
35  * This routine assumes the locks have been taken
36  */
37 static int
38 find_matching_hba(di_node_t node, void *arg)
39 {
40 	int *propData, rval;
41 	walkarg_t *wa = (walkarg_t *)arg;
42 	char	*devpath, fulldevpath[MAXPATHLEN];
43 
44 	/* Skip stub(instance -1) nodes */
45 	if (IS_STUB_NODE(node)) {
46 		return (DI_WALK_CONTINUE);
47 	}
48 
49 	rval = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
50 	    "sm-hba-supported", &propData);
51 	if (rval < 0) {
52 		return (DI_WALK_CONTINUE);
53 	} else {
54 		if ((devpath = di_devfs_path(node)) == NULL) {
55 			/* still continue to see if there is matching one. */
56 			return (DI_WALK_CONTINUE);
57 		}
58 		(void) snprintf(fulldevpath, MAXPATHLEN, "%s%s", DEVICES_DIR,
59 		    devpath);
60 
61 		if ((strstr(fulldevpath, wa->devpath)) != NULL) {
62 			*wa->flag = B_TRUE;
63 			/* Found a node. No need to walk any more. */
64 			di_devfs_path_free(devpath);
65 			return (DI_WALK_TERMINATE);
66 		}
67 		di_devfs_path_free(devpath);
68 	}
69 
70 	return (DI_WALK_CONTINUE);
71 }
72 
73 /*
74  * Refreshes information about an HBA
75  *
76  * Note: This routine holds the locks in write mode
77  *       during most of the processing, and as such, will cause
78  *	 all other threads to block on entry into the library
79  *	 until the refresh is complete.  An optimization would be
80  *       to put fine-grain locking in for the open_handle structures.
81  */
82 void
83 Sun_sasRefreshAdapterConfiguration()
84 {
85 	const char		    ROUTINE[] =
86 	    "Sun_sasRefreshAdapterConfiguration";
87 	struct sun_sas_hba	    *hba_ptr;
88 	di_node_t		    root;
89 	hrtime_t		    start;
90 	hrtime_t		    end;
91 	double			    duration;
92 	walkarg_t		    wa;
93 
94 	/*
95 	 * We're going to be tweaking a lot of global lists, so write
96 	 * lock them.
97 	 */
98 	lock(&all_hbas_lock);
99 	lock(&open_handles_lock);
100 
101 	start = gethrtime();
102 	/* Grab device tree */
103 	if ((root = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
104 		log(LOG_DEBUG, ROUTINE,
105 		    "Unable to load device tree for reason \"%s\"",
106 		    strerror(errno));
107 		unlock(&open_handles_lock);
108 		unlock(&all_hbas_lock);
109 		return;
110 	}
111 
112 	end = gethrtime();
113 	duration = end - start;
114 	duration /= HR_SECOND;
115 	log(LOG_DEBUG, ROUTINE, "Device tree init took "
116 	    "%.6f seconds", duration);
117 
118 	for (hba_ptr = global_hba_head; hba_ptr != NULL;
119 	    hba_ptr = hba_ptr->next) {
120 		wa.devpath = hba_ptr->device_path;
121 		wa.flag = (boolean_t *)calloc(1, sizeof (boolean_t));
122 		*wa.flag = B_FALSE;
123 
124 		if (di_walk_node(root, DI_WALK_SIBFIRST, &wa,
125 		    find_matching_hba) != 0) {
126 			log(LOG_DEBUG, ROUTINE, "di_walk_node failed.");
127 			unlock(&open_handles_lock);
128 			unlock(&all_hbas_lock);
129 			S_FREE(wa.flag);
130 			di_fini(root);
131 			return;
132 		}
133 
134 		if (*wa.flag == B_FALSE) {
135 			/*
136 			 * Keep the HBA as it is including open handles
137 			 * per the SM-HBA standards. Just mark it as invalid.
138 			 */
139 			log(LOG_DEBUG, ROUTINE, "No matching HBA found. %s",
140 			    hba_ptr->device_path);
141 			hba_ptr->invalid = B_TRUE;
142 		}
143 		S_FREE(wa.flag);
144 	}
145 
146 	/*
147 	 * Now we marked missing HBA(s). Redisoover hbas to refresh
148 	 * or add any new adapter to the hba list.
149 	 * Simply call devtree_get_all_hbas().
150 	 */
151 	if (devtree_get_all_hbas(root) != HBA_STATUS_OK) {
152 		log(LOG_DEBUG, ROUTINE, "devtree_get_all_hbas failed.");
153 	}
154 	di_fini(root);
155 
156 	unlock(&open_handles_lock);
157 	unlock(&all_hbas_lock);
158 }
159