xref: /spdk/lib/init/subsystem.c (revision 927f1fd57bd004df581518466ec4c1b8083e5d23)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/init.h"
37 #include "spdk/log.h"
38 #include "spdk/queue.h"
39 #include "spdk/thread.h"
40 
41 #include "spdk_internal/init.h"
42 #include "spdk/env.h"
43 
44 #include "spdk/json.h"
45 
46 #include "subsystem.h"
47 
48 TAILQ_HEAD(spdk_subsystem_list, spdk_subsystem);
49 struct spdk_subsystem_list g_subsystems = TAILQ_HEAD_INITIALIZER(g_subsystems);
50 
51 TAILQ_HEAD(spdk_subsystem_depend_list, spdk_subsystem_depend);
52 struct spdk_subsystem_depend_list g_subsystems_deps = TAILQ_HEAD_INITIALIZER(g_subsystems_deps);
53 static struct spdk_subsystem *g_next_subsystem;
54 static bool g_subsystems_initialized = false;
55 static bool g_subsystems_init_interrupted = false;
56 static spdk_subsystem_init_fn g_subsystem_start_fn = NULL;
57 static void *g_subsystem_start_arg = NULL;
58 static spdk_msg_fn g_subsystem_stop_fn = NULL;
59 static void *g_subsystem_stop_arg = NULL;
60 static struct spdk_thread *g_fini_thread = NULL;
61 
62 void
63 spdk_add_subsystem(struct spdk_subsystem *subsystem)
64 {
65 	TAILQ_INSERT_TAIL(&g_subsystems, subsystem, tailq);
66 }
67 
68 void
69 spdk_add_subsystem_depend(struct spdk_subsystem_depend *depend)
70 {
71 	TAILQ_INSERT_TAIL(&g_subsystems_deps, depend, tailq);
72 }
73 
74 static struct spdk_subsystem *
75 _subsystem_find(struct spdk_subsystem_list *list, const char *name)
76 {
77 	struct spdk_subsystem *iter;
78 
79 	TAILQ_FOREACH(iter, list, tailq) {
80 		if (strcmp(name, iter->name) == 0) {
81 			return iter;
82 		}
83 	}
84 
85 	return NULL;
86 }
87 
88 struct spdk_subsystem *
89 subsystem_find(const char *name)
90 {
91 	return _subsystem_find(&g_subsystems, name);
92 }
93 
94 struct spdk_subsystem *
95 subsystem_get_first(void)
96 {
97 	return TAILQ_FIRST(&g_subsystems);
98 }
99 
100 struct spdk_subsystem *
101 subsystem_get_next(struct spdk_subsystem *cur_subsystem)
102 {
103 	return TAILQ_NEXT(cur_subsystem, tailq);
104 }
105 
106 
107 struct spdk_subsystem_depend *
108 subsystem_get_first_depend(void)
109 {
110 	return TAILQ_FIRST(&g_subsystems_deps);
111 }
112 
113 struct spdk_subsystem_depend *
114 subsystem_get_next_depend(struct spdk_subsystem_depend *cur_depend)
115 {
116 	return TAILQ_NEXT(cur_depend, tailq);
117 }
118 
119 static void
120 subsystem_sort(void)
121 {
122 	bool depends_on, depends_on_sorted;
123 	struct spdk_subsystem *subsystem, *subsystem_tmp;
124 	struct spdk_subsystem_depend *subsystem_dep;
125 
126 	struct spdk_subsystem_list subsystems_list = TAILQ_HEAD_INITIALIZER(subsystems_list);
127 
128 	while (!TAILQ_EMPTY(&g_subsystems)) {
129 		TAILQ_FOREACH_SAFE(subsystem, &g_subsystems, tailq, subsystem_tmp) {
130 			depends_on = false;
131 			TAILQ_FOREACH(subsystem_dep, &g_subsystems_deps, tailq) {
132 				if (strcmp(subsystem->name, subsystem_dep->name) == 0) {
133 					depends_on = true;
134 					depends_on_sorted = !!_subsystem_find(&subsystems_list, subsystem_dep->depends_on);
135 					if (depends_on_sorted) {
136 						continue;
137 					}
138 					break;
139 				}
140 			}
141 
142 			if (depends_on == false) {
143 				TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
144 				TAILQ_INSERT_TAIL(&subsystems_list, subsystem, tailq);
145 			} else {
146 				if (depends_on_sorted == true) {
147 					TAILQ_REMOVE(&g_subsystems, subsystem, tailq);
148 					TAILQ_INSERT_TAIL(&subsystems_list, subsystem, tailq);
149 				}
150 			}
151 		}
152 	}
153 
154 	TAILQ_FOREACH_SAFE(subsystem, &subsystems_list, tailq, subsystem_tmp) {
155 		TAILQ_REMOVE(&subsystems_list, subsystem, tailq);
156 		TAILQ_INSERT_TAIL(&g_subsystems, subsystem, tailq);
157 	}
158 }
159 
160 void
161 spdk_subsystem_init_next(int rc)
162 {
163 	/* The initialization is interrupted by the spdk_subsystem_fini, so just return */
164 	if (g_subsystems_init_interrupted) {
165 		return;
166 	}
167 
168 	if (rc) {
169 		SPDK_ERRLOG("Init subsystem %s failed\n", g_next_subsystem->name);
170 		g_subsystem_start_fn(rc, g_subsystem_start_arg);
171 		return;
172 	}
173 
174 	if (!g_next_subsystem) {
175 		g_next_subsystem = TAILQ_FIRST(&g_subsystems);
176 	} else {
177 		g_next_subsystem = TAILQ_NEXT(g_next_subsystem, tailq);
178 	}
179 
180 	if (!g_next_subsystem) {
181 		g_subsystems_initialized = true;
182 		g_subsystem_start_fn(0, g_subsystem_start_arg);
183 		return;
184 	}
185 
186 	if (g_next_subsystem->init) {
187 		g_next_subsystem->init();
188 	} else {
189 		spdk_subsystem_init_next(0);
190 	}
191 }
192 
193 void
194 spdk_subsystem_init(spdk_subsystem_init_fn cb_fn, void *cb_arg)
195 {
196 	struct spdk_subsystem_depend *dep;
197 
198 	g_subsystem_start_fn = cb_fn;
199 	g_subsystem_start_arg = cb_arg;
200 
201 	/* Verify that all dependency name and depends_on subsystems are registered */
202 	TAILQ_FOREACH(dep, &g_subsystems_deps, tailq) {
203 		if (!subsystem_find(dep->name)) {
204 			SPDK_ERRLOG("subsystem %s is missing\n", dep->name);
205 			g_subsystem_start_fn(-1, g_subsystem_start_arg);
206 			return;
207 		}
208 		if (!subsystem_find(dep->depends_on)) {
209 			SPDK_ERRLOG("subsystem %s dependency %s is missing\n",
210 				    dep->name, dep->depends_on);
211 			g_subsystem_start_fn(-1, g_subsystem_start_arg);
212 			return;
213 		}
214 	}
215 
216 	subsystem_sort();
217 
218 	spdk_subsystem_init_next(0);
219 }
220 
221 static void
222 subsystem_fini_next(void *arg1)
223 {
224 	assert(g_fini_thread == spdk_get_thread());
225 
226 	if (!g_next_subsystem) {
227 		/* If the initialized flag is false, then we've failed to initialize
228 		 * the very first subsystem and no de-init is needed
229 		 */
230 		if (g_subsystems_initialized) {
231 			g_next_subsystem = TAILQ_LAST(&g_subsystems, spdk_subsystem_list);
232 		}
233 	} else {
234 		if (g_subsystems_initialized || g_subsystems_init_interrupted) {
235 			g_next_subsystem = TAILQ_PREV(g_next_subsystem, spdk_subsystem_list, tailq);
236 		} else {
237 			g_subsystems_init_interrupted = true;
238 		}
239 	}
240 
241 	while (g_next_subsystem) {
242 		if (g_next_subsystem->fini) {
243 			g_next_subsystem->fini();
244 			return;
245 		}
246 		g_next_subsystem = TAILQ_PREV(g_next_subsystem, spdk_subsystem_list, tailq);
247 	}
248 
249 	g_subsystem_stop_fn(g_subsystem_stop_arg);
250 	return;
251 }
252 
253 void
254 spdk_subsystem_fini_next(void)
255 {
256 	if (g_fini_thread != spdk_get_thread()) {
257 		spdk_thread_send_msg(g_fini_thread, subsystem_fini_next, NULL);
258 	} else {
259 		subsystem_fini_next(NULL);
260 	}
261 }
262 
263 void
264 spdk_subsystem_fini(spdk_msg_fn cb_fn, void *cb_arg)
265 {
266 	g_subsystem_stop_fn = cb_fn;
267 	g_subsystem_stop_arg = cb_arg;
268 
269 	g_fini_thread = spdk_get_thread();
270 
271 	spdk_subsystem_fini_next();
272 }
273 
274 void
275 subsystem_config_json(struct spdk_json_write_ctx *w, struct spdk_subsystem *subsystem)
276 {
277 	if (subsystem && subsystem->write_config_json) {
278 		subsystem->write_config_json(w);
279 	} else {
280 		spdk_json_write_null(w);
281 	}
282 }
283