2 * Copyright (c) 2005, Sam Leffler <sam@errno.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/queue.h>
34 #include <sys/taskqueue.h>
35 #include <sys/systm.h>
37 #include <sys/mutex.h>
38 #include <sys/errno.h>
39 #include <sys/linker.h>
40 #include <sys/firmware.h>
42 #include <sys/module.h>
45 * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
46 * form more details on the subsystem.
48 * 'struct firmware' is the user-visible part of the firmware table.
49 * Additional internal information is stored in a 'struct priv_fw'
50 * (currently a static array). A slot is in use if FW_INUSE is true:
53 #define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL)
56 * fw.name != NULL when an image is registered; file != NULL for
57 * autoloaded images whose handling has not been completed.
59 * The state of a slot evolves as follows:
60 * firmware_register --> fw.name = image_name
61 * (autoloaded image) --> file = module reference
62 * firmware_unregister --> fw.name = NULL
63 * (unloadentry complete) --> file = NULL
65 * In order for the above to work, the 'file' field must remain
66 * unchanged in firmware_unregister().
68 * Images residing in the same module are linked to each other
69 * through the 'parent' argument of firmware_register().
70 * One image (typically, one with the same name as the module to let
71 * the autoloading mechanism work) is considered the parent image for
72 * all other images in the same module. Children affect the refcount
73 * on the parent image preventing improper unloading of the image itself.
77 int refcnt; /* reference count */
80 * parent entry, see above. Set on firmware_register(),
81 * cleared on firmware_unregister().
83 struct priv_fw *parent;
85 int flags; /* record FIRMWARE_UNLOAD requests */
86 #define FW_UNLOAD 0x100
89 * 'file' is private info managed by the autoload/unload code.
90 * Set at the end of firmware_get(), cleared only in the
91 * firmware_task, so the latter can depend on its value even
92 * while the lock is not held.
94 linker_file_t file; /* module file, if autoloaded */
97 * 'fw' is the externally visible image information.
98 * We do not make it the first field in priv_fw, to avoid the
99 * temptation of casting pointers to each other.
100 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
101 * Beware, PRIV_FW does not work for a NULL pointer.
103 struct firmware fw; /* externally visible information */
107 * PRIV_FW returns the pointer to the container of struct firmware *x.
108 * Cast to intptr_t to override the 'const' attribute of x
110 #define PRIV_FW(x) ((struct priv_fw *) \
111 ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
114 * At the moment we use a static array as backing store for the registry.
115 * Should we move to a dynamic structure, keep in mind that we cannot
116 * reallocate the array because pointers are held externally.
117 * A list may work, though.
119 #define FIRMWARE_MAX 30
120 static struct priv_fw firmware_table[FIRMWARE_MAX];
123 * module release are handled in a separate task as they might sleep.
125 struct task firmware_task;
128 * This mutex protects accesses to the firmware table.
130 struct mtx firmware_mtx;
131 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
134 * Helper function to lookup a name.
135 * As a side effect, it sets the pointer to a free slot, if any.
136 * This way we can concentrate most of the registry scanning in
137 * this function, which makes it easier to replace the registry
138 * with some other data structure.
140 static struct priv_fw *
141 lookup(const char *name, struct priv_fw **empty_slot)
143 struct priv_fw *fp = NULL;
144 struct priv_fw *dummy;
147 if (empty_slot == NULL)
150 for (i = 0; i < FIRMWARE_MAX; i++) {
151 fp = &firmware_table[i];
152 if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
154 else if (!FW_INUSE(fp))
157 return (i < FIRMWARE_MAX ) ? fp : NULL;
161 * Register a firmware image with the specified name. The
162 * image name must not already be registered. If this is a
163 * subimage then parent refers to a previously registered
164 * image that this should be associated with.
166 const struct firmware *
167 firmware_register(const char *imagename, const void *data, size_t datasize,
168 unsigned int version, const struct firmware *parent)
170 struct priv_fw *match, *frp;
172 mtx_lock(&firmware_mtx);
174 * Do a lookup to make sure the name is unique or find a free slot.
176 match = lookup(imagename, &frp);
178 mtx_unlock(&firmware_mtx);
179 printf("%s: image %s already registered!\n",
180 __func__, imagename);
184 mtx_unlock(&firmware_mtx);
185 printf("%s: cannot register image %s, firmware table full!\n",
186 __func__, imagename);
189 bzero(frp, sizeof(frp)); /* start from a clean record */
190 frp->fw.name = imagename;
192 frp->fw.datasize = datasize;
193 frp->fw.version = version;
194 if (parent != NULL) {
195 frp->parent = PRIV_FW(parent);
196 frp->parent->refcnt++;
198 mtx_unlock(&firmware_mtx);
200 printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
201 imagename, version, datasize, data);
206 * Unregister/remove a firmware image. If there are outstanding
207 * references an error is returned and the image is not removed
211 firmware_unregister(const char *imagename)
216 mtx_lock(&firmware_mtx);
217 fp = lookup(imagename, NULL);
220 * It is ok for the lookup to fail; this can happen
221 * when a module is unloaded on last reference and the
222 * module unload handler unregister's each of it's
226 } else if (fp->refcnt != 0) { /* cannot unregister */
229 linker_file_t x = fp->file; /* save value */
231 if (fp->parent != NULL) /* release parent reference */
232 fp->parent->refcnt--;
234 * Clear the whole entry with bzero to make sure we
235 * do not forget anything. Then restore 'file' which is
236 * non-null for autoloaded images.
238 bzero(fp, sizeof(struct priv_fw));
242 mtx_unlock(&firmware_mtx);
247 * Lookup and potentially load the specified firmware image.
248 * If the firmware is not found in the registry, try to load a kernel
249 * module named as the image name.
250 * If the firmware is located, a reference is returned. The caller must
251 * release this reference for the image to be eligible for removal/unload.
253 const struct firmware *
254 firmware_get(const char *imagename)
258 linker_file_t result;
260 mtx_lock(&firmware_mtx);
261 fp = lookup(imagename, NULL);
265 * Image not present, try to load the module holding it.
267 mtx_unlock(&firmware_mtx);
269 if (suser(td) != 0 ||
270 securelevel_gt(td->td_ucred, 0) != 0) {
271 printf("%s: insufficient privileges to "
272 "load firmware image %s\n", __func__, imagename);
275 (void) linker_reference_module(imagename, NULL, &result);
277 * After loading the module, see if the image is registered now.
279 mtx_lock(&firmware_mtx);
280 fp = lookup(imagename, NULL);
282 mtx_unlock(&firmware_mtx);
283 printf("%s: failed to load firmware image %s\n",
284 __func__, imagename);
285 (void) linker_release_module(imagename, NULL, NULL);
288 fp->file = result; /* record the module identity */
290 found: /* common exit point on success */
292 mtx_unlock(&firmware_mtx);
297 * Release a reference to a firmware image returned by firmware_get.
298 * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
299 * to release the resource, but the flag is only advisory.
301 * If this is the last reference to the firmware image, and this is an
302 * autoloaded module, wake up the firmware_task to figure out what to do
303 * with the associated module.
306 firmware_put(const struct firmware *p, int flags)
308 struct priv_fw *fp = PRIV_FW(p);
310 mtx_lock(&firmware_mtx);
312 if (fp->refcnt == 0) {
313 if (flags & FIRMWARE_UNLOAD)
314 fp->flags |= FW_UNLOAD;
316 taskqueue_enqueue(taskqueue_thread, &firmware_task);
318 mtx_unlock(&firmware_mtx);
322 * The body of the task in charge of unloading autoloaded modules
323 * that are not needed anymore.
324 * Images can be cross-linked so we may need to make multiple passes,
325 * but the time we spend in the loop is bounded because we clear entries
329 unloadentry(void *unused1, int unused2)
331 int limit = FIRMWARE_MAX;
332 int i; /* current cycle */
334 mtx_lock(&firmware_mtx);
336 * Scan the table. limit is set to make sure we make another
337 * full sweep after matching an entry that requires unloading.
339 for (i = 0; i < limit; i++) {
343 fp = &firmware_table[i % FIRMWARE_MAX];
344 if (fp->fw.name == NULL || fp->file == NULL ||
345 fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
349 * Found an entry. Now:
350 * 1. bump up limit to make sure we make another full round;
351 * 2. clear FW_UNLOAD so we don't try this entry again.
352 * 3. release the lock while trying to unload the module.
353 * 'file' remains set so that the entry cannot be reused
354 * in the meantime (it also means that fp->file will
355 * not change while we release the lock).
357 limit = i + FIRMWARE_MAX; /* make another full round */
358 fp->flags &= ~FW_UNLOAD; /* do not try again */
360 mtx_unlock(&firmware_mtx);
361 err = linker_release_module(NULL, NULL, fp->file);
362 mtx_lock(&firmware_mtx);
365 * We rely on the module to call firmware_unregister()
366 * on unload to actually release the entry.
367 * If err = 0 we can drop our reference as the system
368 * accepted it. Otherwise unloading failed (e.g. the
369 * module itself gave an error) so our reference is
375 mtx_unlock(&firmware_mtx);
382 firmware_modevent(module_t mod, int type, void *unused)
389 TASK_INIT(&firmware_task, 0, unloadentry, NULL);
393 /* request all autoloaded modules to be released */
394 mtx_lock(&firmware_mtx);
395 for (i = 0; i < FIRMWARE_MAX; i++) {
396 fp = &firmware_table[i];
397 fp->flags |= FW_UNLOAD;;
399 mtx_unlock(&firmware_mtx);
400 taskqueue_enqueue(taskqueue_thread, &firmware_task);
401 taskqueue_drain(taskqueue_thread, &firmware_task);
402 for (i = 0; i < FIRMWARE_MAX; i++) {
403 fp = &firmware_table[i];
404 if (fp->fw.name != NULL) {
405 printf("%s: image %p ref %d still active slot %d\n",
406 __func__, fp->fw.name,
416 static moduledata_t firmware_mod = {
421 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
422 MODULE_VERSION(firmware, 1);