xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_module.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright (c) 2019, Joyent, Inc. All rights reserved.
28  */
29 
30 #include <signal.h>
31 #include <dirent.h>
32 #include <limits.h>
33 #include <alloca.h>
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <pthread.h>
37 #include <synch.h>
38 #include <errno.h>
39 #include <strings.h>
40 #include <assert.h>
41 #include <sys/nvpair.h>
42 
43 #include <topo_string.h>
44 #include <topo_alloc.h>
45 #include <topo_module.h>
46 #include <topo_error.h>
47 #include <topo_subr.h>
48 
49 extern nv_alloc_ops_t topo_nv_alloc_ops;
50 
51 void
52 topo_mod_release(topo_mod_t *mod, tnode_t *node)
53 {
54 	topo_mod_enter(mod);
55 
56 	if (mod->tm_info->tmi_ops->tmo_release != NULL)
57 		mod->tm_info->tmi_ops->tmo_release(mod, node);
58 
59 	topo_mod_exit(mod);
60 }
61 
62 void
63 topo_mod_hold(topo_mod_t *mod)
64 {
65 	(void) pthread_mutex_lock(&mod->tm_lock);
66 	mod->tm_refs++;
67 	assert(mod->tm_refs != 0);
68 	(void) pthread_mutex_unlock(&mod->tm_lock);
69 }
70 
71 void
72 topo_mod_rele(topo_mod_t *mod)
73 {
74 	assert(mod->tm_refs != 0);
75 
76 	(void) pthread_mutex_lock(&mod->tm_lock);
77 
78 	/*
79 	 * Lazy unload module
80 	 */
81 	if (--mod->tm_refs == 0)
82 		topo_modhash_unload(mod);
83 	else
84 		(void) pthread_mutex_unlock(&mod->tm_lock);
85 }
86 
87 void
88 topo_mod_enter(topo_mod_t *mod)
89 {
90 	(void) pthread_mutex_lock(&mod->tm_lock);
91 
92 	while (mod->tm_busy != 0)
93 		(void) pthread_cond_wait(&mod->tm_cv, &mod->tm_lock);
94 
95 	++mod->tm_busy;
96 
97 	(void) pthread_mutex_unlock(&mod->tm_lock);
98 }
99 
100 void
101 topo_mod_exit(topo_mod_t *mod)
102 {
103 	(void) pthread_mutex_lock(&mod->tm_lock);
104 	--mod->tm_busy;
105 
106 	assert(mod->tm_busy == 0);
107 
108 	(void) pthread_cond_broadcast(&mod->tm_cv);
109 	(void) pthread_mutex_unlock(&mod->tm_lock);
110 }
111 
112 static void
113 topo_modhash_lock(topo_modhash_t *mhp)
114 {
115 	(void) pthread_mutex_lock(&mhp->mh_lock);
116 }
117 
118 static void
119 topo_modhash_unlock(topo_modhash_t *mhp)
120 {
121 	(void) pthread_mutex_unlock(&mhp->mh_lock);
122 }
123 
124 static void
125 topo_mod_stop(topo_mod_t *mod)
126 {
127 	if (mod->tm_flags & TOPO_MOD_INIT) {
128 		(void) mod->tm_mops->mop_fini(mod);
129 		if (mod->tm_flags & TOPO_MOD_REG)
130 			topo_mod_unregister(mod);
131 	}
132 
133 	mod->tm_flags = TOPO_MOD_FINI;
134 
135 	topo_dprintf(mod->tm_hdl, TOPO_DBG_MODSVC,
136 	    "module %s stopped\n", mod->tm_name);
137 }
138 
139 static int
140 topo_mod_start(topo_mod_t *mod, topo_version_t version)
141 {
142 	topo_dprintf(mod->tm_hdl, TOPO_DBG_MODSVC,
143 	    "starting module %s\n", mod->tm_name);
144 
145 	if (mod->tm_mops->mop_init(mod, version) != 0) {
146 		if (mod->tm_errno == 0)
147 			mod->tm_errno = ETOPO_MOD_INIT;
148 		topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
149 		    "module %s failed to initialize: %s\n", mod->tm_name,
150 		    topo_strerror(mod->tm_errno));
151 		return (-1);
152 	}
153 
154 	mod->tm_flags |= TOPO_MOD_INIT;
155 
156 	if (!(mod->tm_flags & TOPO_MOD_REG)) {
157 		topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
158 		    "module %s failed to register\n", mod->tm_name);
159 		mod->tm_errno = ETOPO_MOD_NOREG;
160 		topo_mod_stop(mod);
161 		return (-1);
162 	}
163 
164 	return (0);
165 }
166 
167 topo_mod_t *
168 topo_mod_lookup(topo_hdl_t *thp, const char *name, int bump)
169 {
170 	topo_mod_t *mod;
171 	topo_modhash_t *mhp = thp->th_modhash;
172 
173 	topo_modhash_lock(mhp);
174 	mod = topo_modhash_lookup(mhp, name);
175 	if (mod != NULL && bump != 0)
176 		topo_mod_hold(mod);
177 	topo_modhash_unlock(mhp);
178 
179 	return (mod);
180 }
181 
182 static void
183 topo_mod_destroy(topo_mod_t *mod)
184 {
185 	topo_hdl_t *thp;
186 
187 	if (mod == NULL)
188 		return;
189 
190 	thp = mod->tm_hdl;
191 
192 	assert(mod->tm_refs == 0);
193 	assert(!MUTEX_HELD(&mod->tm_lock));
194 
195 	if (mod->tm_name != NULL)
196 		topo_hdl_strfree(thp, mod->tm_name);
197 	if (mod->tm_path != NULL)
198 		topo_hdl_strfree(thp, mod->tm_path);
199 	if (mod->tm_rootdir != NULL)
200 		topo_hdl_strfree(thp, mod->tm_rootdir);
201 
202 	topo_hdl_free(thp, mod, sizeof (topo_mod_t));
203 }
204 
205 static topo_mod_t *
206 set_create_error(topo_hdl_t *thp, topo_mod_t *mod, const char *path, int err)
207 {
208 	if (path != NULL)
209 		topo_dprintf(thp, TOPO_DBG_ERR, "unable to load module %s: "
210 		    "%s\n", path, topo_strerror(err));
211 	else
212 		topo_dprintf(thp, TOPO_DBG_ERR, "unable to load module: "
213 		    "%s\n", topo_strerror(err));
214 
215 	if (mod != NULL)
216 		topo_mod_destroy(mod);
217 
218 	(void) topo_hdl_seterrno(thp, err);
219 
220 	return (NULL);
221 }
222 
223 static topo_mod_t *
224 topo_mod_create(topo_hdl_t *thp, const char *name, const char *path,
225     const topo_imodops_t *ops, topo_version_t version)
226 {
227 	topo_mod_t *mod;
228 
229 	if (topo_modhash_lookup(thp->th_modhash, name) != NULL)
230 		return (set_create_error(thp, NULL, path, ETOPO_MOD_LOADED));
231 
232 	if ((mod = topo_hdl_zalloc(thp, sizeof (topo_mod_t))) == NULL)
233 		return (set_create_error(thp, mod, path, ETOPO_NOMEM));
234 
235 	mod->tm_hdl = thp;
236 
237 	(void) pthread_mutex_init(&mod->tm_lock, NULL);
238 
239 	mod->tm_name = topo_hdl_strdup(thp, name);
240 	if (path != NULL)
241 		mod->tm_path = topo_hdl_strdup(thp, path);
242 	mod->tm_rootdir = topo_hdl_strdup(thp, thp->th_rootdir);
243 	if (mod->tm_name == NULL || mod->tm_rootdir == NULL)
244 		return (set_create_error(thp, mod, path, ETOPO_NOMEM));
245 
246 	mod->tm_mops = (topo_imodops_t *)ops;
247 	mod->tm_alloc = thp->th_alloc;
248 
249 	/*
250 	 * Module will be held upon a successful return from topo_mod_start()
251 	 */
252 	if ((topo_mod_start(mod, version)) < 0)
253 		return (set_create_error(thp, mod, path, mod->tm_errno));
254 
255 	topo_dprintf(thp, TOPO_DBG_MODSVC, "loaded module %s\n", mod->tm_name);
256 
257 	return (mod);
258 }
259 
260 topo_modhash_t *
261 topo_modhash_create(topo_hdl_t *thp)
262 {
263 	topo_modhash_t *mhp;
264 
265 	if ((mhp = topo_hdl_zalloc(thp, sizeof (topo_modhash_t))) == NULL)
266 		return (NULL);
267 
268 	mhp->mh_hashlen = TOPO_HASH_BUCKETS;
269 	if ((mhp->mh_hash = topo_hdl_zalloc(thp,
270 	    sizeof (void *) * mhp->mh_hashlen)) == NULL) {
271 		topo_hdl_free(thp, mhp, sizeof (topo_modhash_t));
272 		return (NULL);
273 	}
274 	mhp->mh_nelems = 0;
275 	(void) pthread_mutex_init(&mhp->mh_lock, NULL);
276 
277 	thp->th_modhash = mhp;
278 
279 	return (mhp);
280 }
281 
282 void
283 topo_modhash_destroy(topo_hdl_t *thp)
284 {
285 	topo_modhash_t *mhp = thp->th_modhash;
286 
287 	if (mhp == NULL)
288 		return;
289 
290 	assert(mhp->mh_nelems == 0);
291 
292 	topo_hdl_free(thp, mhp->mh_hash, sizeof (void *) * mhp->mh_hashlen);
293 	topo_hdl_free(thp, mhp, sizeof (topo_modhash_t));
294 	thp->th_modhash = NULL;
295 }
296 
297 topo_mod_t *
298 topo_modhash_lookup(topo_modhash_t *mhp, const char *name)
299 {
300 	topo_mod_t *mod = NULL;
301 	uint_t h;
302 
303 	h = topo_strhash(name) % mhp->mh_hashlen;
304 
305 	for (mod = mhp->mh_hash[h]; mod != NULL; mod = mod->tm_next) {
306 		if (strcmp(name, mod->tm_name) == 0)
307 			break;
308 	}
309 
310 	return (mod);
311 }
312 
313 topo_mod_t *
314 topo_modhash_load(topo_hdl_t *thp, const char *name, const char *path,
315     const topo_imodops_t *ops, topo_version_t version)
316 {
317 	topo_modhash_t *mhp = thp->th_modhash;
318 	topo_mod_t *mod;
319 	uint_t h;
320 
321 	topo_modhash_lock(mhp);
322 
323 	if ((mod = topo_mod_create(thp, name, path, ops, version)) == NULL) {
324 		topo_modhash_unlock(mhp);
325 		return (NULL); /* th_errno set */
326 	}
327 
328 	topo_mod_hold(mod);
329 
330 	h = topo_strhash(name) % mhp->mh_hashlen;
331 	mod->tm_next = mhp->mh_hash[h];
332 	mhp->mh_hash[h] = mod;
333 	mhp->mh_nelems++;
334 	topo_modhash_unlock(mhp);
335 
336 	return (mod);
337 }
338 
339 void
340 topo_modhash_unload(topo_mod_t *mod)
341 {
342 	uint_t h;
343 	topo_mod_t **pp, *mp;
344 	topo_hdl_t *thp = mod->tm_hdl;
345 	topo_modhash_t *mhp;
346 
347 	assert(MUTEX_HELD(&mod->tm_lock));
348 	assert(mod->tm_busy == 0);
349 
350 	mhp = thp->th_modhash;
351 	topo_modhash_lock(mhp);
352 
353 	assert(mhp != NULL);
354 
355 	h = topo_strhash(mod->tm_name) % mhp->mh_hashlen;
356 	pp = &mhp->mh_hash[h];
357 
358 	for (mp = *pp; mp != NULL; mp = mp->tm_next) {
359 		if (mp == mod)
360 			break;
361 		else
362 			pp = &mp->tm_next;
363 	}
364 
365 	if (mp != NULL) {
366 		*pp = mod->tm_next;
367 
368 		assert(mhp->mh_nelems != 0);
369 
370 		mhp->mh_nelems--;
371 
372 	}
373 	topo_modhash_unlock(mhp);
374 
375 	(void) pthread_mutex_unlock(&mod->tm_lock);
376 
377 	topo_mod_stop(mod);
378 	topo_mod_destroy(mod);
379 
380 }
381 
382 void
383 topo_modhash_unload_all(topo_hdl_t *thp)
384 {
385 	int i;
386 	topo_modhash_t *mhp = thp->th_modhash;
387 	topo_mod_t *mp, **pp;
388 
389 	if (mhp == NULL)
390 		return;
391 
392 	topo_modhash_lock(mhp);
393 	for (i = 0; i < TOPO_HASH_BUCKETS; ++i) {
394 		pp = &mhp->mh_hash[i];
395 		mp = *pp;
396 		while (mp != NULL) {
397 			topo_mod_stop(mp);
398 
399 			/*
400 			 * At this point we are forcing all modules to
401 			 * stop, ignore any remaining module reference counts.
402 			 */
403 			mp->tm_refs = 0;
404 
405 			*pp = mp->tm_next;
406 			topo_mod_destroy(mp);
407 			mp = *pp;
408 
409 			--mhp->mh_nelems;
410 		}
411 	}
412 	topo_modhash_unlock(mhp);
413 }
414