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>
43 #include <sys/module.h>
46 * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
47 * form more details on the subsystem.
49 * 'struct firmware' is the user-visible part of the firmware table.
50 * Additional internal information is stored in a 'struct priv_fw'
51 * (currently a static array). A slot is in use if FW_INUSE is true:
54 #define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL)
57 * fw.name != NULL when an image is registered; file != NULL for
58 * autoloaded images whose handling has not been completed.
60 * The state of a slot evolves as follows:
61 * firmware_register --> fw.name = image_name
62 * (autoloaded image) --> file = module reference
63 * firmware_unregister --> fw.name = NULL
64 * (unloadentry complete) --> file = NULL
66 * In order for the above to work, the 'file' field must remain
67 * unchanged in firmware_unregister().
69 * Images residing in the same module are linked to each other
70 * through the 'parent' argument of firmware_register().
71 * One image (typically, one with the same name as the module to let
72 * the autoloading mechanism work) is considered the parent image for
73 * all other images in the same module. Children affect the refcount
74 * on the parent image preventing improper unloading of the image itself.
78 int refcnt; /* reference count */
81 * parent entry, see above. Set on firmware_register(),
82 * cleared on firmware_unregister().
84 struct priv_fw *parent;
86 int flags; /* record FIRMWARE_UNLOAD requests */
87 #define FW_UNLOAD 0x100
90 * 'file' is private info managed by the autoload/unload code.
91 * Set at the end of firmware_get(), cleared only in the
92 * firmware_task, so the latter can depend on its value even
93 * while the lock is not held.
95 linker_file_t file; /* module file, if autoloaded */
98 * 'fw' is the externally visible image information.
99 * We do not make it the first field in priv_fw, to avoid the
100 * temptation of casting pointers to each other.
101 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
102 * Beware, PRIV_FW does not work for a NULL pointer.
104 struct firmware fw; /* externally visible information */
108 * PRIV_FW returns the pointer to the container of struct firmware *x.
109 * Cast to intptr_t to override the 'const' attribute of x
111 #define PRIV_FW(x) ((struct priv_fw *) \
112 ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
115 * At the moment we use a static array as backing store for the registry.
116 * Should we move to a dynamic structure, keep in mind that we cannot
117 * reallocate the array because pointers are held externally.
118 * A list may work, though.
120 #define FIRMWARE_MAX 30
121 static struct priv_fw firmware_table[FIRMWARE_MAX];
124 * module release are handled in a separate task as they might sleep.
126 struct task firmware_task;
129 * This mutex protects accesses to the firmware table.
131 struct mtx firmware_mtx;
132 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
135 * Helper function to lookup a name.
136 * As a side effect, it sets the pointer to a free slot, if any.
137 * This way we can concentrate most of the registry scanning in
138 * this function, which makes it easier to replace the registry
139 * with some other data structure.
141 static struct priv_fw *
142 lookup(const char *name, struct priv_fw **empty_slot)
144 struct priv_fw *fp = NULL;
145 struct priv_fw *dummy;
148 if (empty_slot == NULL)
151 for (i = 0; i < FIRMWARE_MAX; i++) {
152 fp = &firmware_table[i];
153 if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
155 else if (!FW_INUSE(fp))
158 return (i < FIRMWARE_MAX ) ? fp : NULL;
162 * Register a firmware image with the specified name. The
163 * image name must not already be registered. If this is a
164 * subimage then parent refers to a previously registered
165 * image that this should be associated with.
167 const struct firmware *
168 firmware_register(const char *imagename, const void *data, size_t datasize,
169 unsigned int version, const struct firmware *parent)
171 struct priv_fw *match, *frp;
173 mtx_lock(&firmware_mtx);
175 * Do a lookup to make sure the name is unique or find a free slot.
177 match = lookup(imagename, &frp);
179 mtx_unlock(&firmware_mtx);
180 printf("%s: image %s already registered!\n",
181 __func__, imagename);
185 mtx_unlock(&firmware_mtx);
186 printf("%s: cannot register image %s, firmware table full!\n",
187 __func__, imagename);
190 bzero(frp, sizeof(frp)); /* start from a clean record */
191 frp->fw.name = imagename;
193 frp->fw.datasize = datasize;
194 frp->fw.version = version;
195 if (parent != NULL) {
196 frp->parent = PRIV_FW(parent);
197 frp->parent->refcnt++;
199 mtx_unlock(&firmware_mtx);
201 printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
202 imagename, version, datasize, data);
207 * Unregister/remove a firmware image. If there are outstanding
208 * references an error is returned and the image is not removed
212 firmware_unregister(const char *imagename)
217 mtx_lock(&firmware_mtx);
218 fp = lookup(imagename, NULL);
221 * It is ok for the lookup to fail; this can happen
222 * when a module is unloaded on last reference and the
223 * module unload handler unregister's each of it's
227 } else if (fp->refcnt != 0) { /* cannot unregister */
230 linker_file_t x = fp->file; /* save value */
232 if (fp->parent != NULL) /* release parent reference */
233 fp->parent->refcnt--;
235 * Clear the whole entry with bzero to make sure we
236 * do not forget anything. Then restore 'file' which is
237 * non-null for autoloaded images.
239 bzero(fp, sizeof(struct priv_fw));
243 mtx_unlock(&firmware_mtx);
248 * Lookup and potentially load the specified firmware image.
249 * If the firmware is not found in the registry, try to load a kernel
250 * module named as the image name.
251 * If the firmware is located, a reference is returned. The caller must
252 * release this reference for the image to be eligible for removal/unload.
254 const struct firmware *
255 firmware_get(const char *imagename)
259 linker_file_t result;
261 mtx_lock(&firmware_mtx);
262 fp = lookup(imagename, NULL);
266 * Image not present, try to load the module holding it.
268 mtx_unlock(&firmware_mtx);
270 if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
271 securelevel_gt(td->td_ucred, 0) != 0) {
272 printf("%s: insufficient privileges to "
273 "load firmware image %s\n", __func__, imagename);
276 (void) linker_reference_module(imagename, NULL, &result);
278 * After loading the module, see if the image is registered now.
280 mtx_lock(&firmware_mtx);
281 fp = lookup(imagename, NULL);
283 mtx_unlock(&firmware_mtx);
284 printf("%s: failed to load firmware image %s\n",
285 __func__, imagename);
286 (void) linker_release_module(imagename, NULL, NULL);
289 fp->file = result; /* record the module identity */
291 found: /* common exit point on success */
293 mtx_unlock(&firmware_mtx);
298 * Release a reference to a firmware image returned by firmware_get.
299 * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
300 * to release the resource, but the flag is only advisory.
302 * If this is the last reference to the firmware image, and this is an
303 * autoloaded module, wake up the firmware_task to figure out what to do
304 * with the associated module.
307 firmware_put(const struct firmware *p, int flags)
309 struct priv_fw *fp = PRIV_FW(p);
311 mtx_lock(&firmware_mtx);
313 if (fp->refcnt == 0) {
314 if (flags & FIRMWARE_UNLOAD)
315 fp->flags |= FW_UNLOAD;
317 taskqueue_enqueue(taskqueue_thread, &firmware_task);
319 mtx_unlock(&firmware_mtx);
323 * The body of the task in charge of unloading autoloaded modules
324 * that are not needed anymore.
325 * Images can be cross-linked so we may need to make multiple passes,
326 * but the time we spend in the loop is bounded because we clear entries
330 unloadentry(void *unused1, int unused2)
332 int limit = FIRMWARE_MAX;
333 int i; /* current cycle */
335 mtx_lock(&firmware_mtx);
337 * Scan the table. limit is set to make sure we make another
338 * full sweep after matching an entry that requires unloading.
340 for (i = 0; i < limit; i++) {
344 fp = &firmware_table[i % FIRMWARE_MAX];
345 if (fp->fw.name == NULL || fp->file == NULL ||
346 fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
350 * Found an entry. Now:
351 * 1. bump up limit to make sure we make another full round;
352 * 2. clear FW_UNLOAD so we don't try this entry again.
353 * 3. release the lock while trying to unload the module.
354 * 'file' remains set so that the entry cannot be reused
355 * in the meantime (it also means that fp->file will
356 * not change while we release the lock).
358 limit = i + FIRMWARE_MAX; /* make another full round */
359 fp->flags &= ~FW_UNLOAD; /* do not try again */
361 mtx_unlock(&firmware_mtx);
362 err = linker_release_module(NULL, NULL, fp->file);
363 mtx_lock(&firmware_mtx);
366 * We rely on the module to call firmware_unregister()
367 * on unload to actually release the entry.
368 * If err = 0 we can drop our reference as the system
369 * accepted it. Otherwise unloading failed (e.g. the
370 * module itself gave an error) so our reference is
376 mtx_unlock(&firmware_mtx);
383 firmware_modevent(module_t mod, int type, void *unused)
390 TASK_INIT(&firmware_task, 0, unloadentry, NULL);
394 /* request all autoloaded modules to be released */
395 mtx_lock(&firmware_mtx);
396 for (i = 0; i < FIRMWARE_MAX; i++) {
397 fp = &firmware_table[i];
398 fp->flags |= FW_UNLOAD;;
400 mtx_unlock(&firmware_mtx);
401 taskqueue_enqueue(taskqueue_thread, &firmware_task);
402 taskqueue_drain(taskqueue_thread, &firmware_task);
403 for (i = 0; i < FIRMWARE_MAX; i++) {
404 fp = &firmware_table[i];
405 if (fp->fw.name != NULL) {
406 printf("%s: image %p ref %d still active slot %d\n",
407 __func__, fp->fw.name,
417 static moduledata_t firmware_mod = {
422 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
423 MODULE_VERSION(firmware, 1);