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