2 * Copyright (c) 2005-2008, 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>
44 #include <sys/eventhandler.h>
46 #include <sys/filedesc.h>
47 #include <sys/vnode.h>
50 * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
51 * form more details on the subsystem.
53 * 'struct firmware' is the user-visible part of the firmware table.
54 * Additional internal information is stored in a 'struct priv_fw'
55 * (currently a static array). A slot is in use if FW_INUSE is true:
58 #define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL)
61 * fw.name != NULL when an image is registered; file != NULL for
62 * autoloaded images whose handling has not been completed.
64 * The state of a slot evolves as follows:
65 * firmware_register --> fw.name = image_name
66 * (autoloaded image) --> file = module reference
67 * firmware_unregister --> fw.name = NULL
68 * (unloadentry complete) --> file = NULL
70 * In order for the above to work, the 'file' field must remain
71 * unchanged in firmware_unregister().
73 * Images residing in the same module are linked to each other
74 * through the 'parent' argument of firmware_register().
75 * One image (typically, one with the same name as the module to let
76 * the autoloading mechanism work) is considered the parent image for
77 * all other images in the same module. Children affect the refcount
78 * on the parent image preventing improper unloading of the image itself.
82 int refcnt
; /* reference count */
85 * parent entry, see above. Set on firmware_register(),
86 * cleared on firmware_unregister().
88 struct priv_fw
*parent
;
90 int flags
; /* record FIRMWARE_UNLOAD requests */
91 #define FW_UNLOAD 0x100
94 * 'file' is private info managed by the autoload/unload code.
95 * Set at the end of firmware_get(), cleared only in the
96 * firmware_unload_task, so the latter can depend on its value even
97 * while the lock is not held.
99 linker_file_t file
; /* module file, if autoloaded */
102 * 'fw' is the externally visible image information.
103 * We do not make it the first field in priv_fw, to avoid the
104 * temptation of casting pointers to each other.
105 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
106 * Beware, PRIV_FW does not work for a NULL pointer.
108 struct firmware fw
; /* externally visible information */
112 * PRIV_FW returns the pointer to the container of struct firmware *x.
113 * Cast to intptr_t to override the 'const' attribute of x
115 #define PRIV_FW(x) ((struct priv_fw *) \
116 ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
119 * At the moment we use a static array as backing store for the registry.
120 * Should we move to a dynamic structure, keep in mind that we cannot
121 * reallocate the array because pointers are held externally.
122 * A list may work, though.
124 #define FIRMWARE_MAX 30
125 static struct priv_fw firmware_table
[FIRMWARE_MAX
];
128 * Firmware module operations are handled in a separate task as they
129 * might sleep and they require directory context to do i/o.
131 static struct taskqueue
*firmware_tq
;
132 static struct task firmware_unload_task
;
135 * This mutex protects accesses to the firmware table.
137 static struct mtx firmware_mtx
;
138 MTX_SYSINIT(firmware
, &firmware_mtx
, "firmware table", MTX_DEF
);
141 * Helper function to lookup a name.
142 * As a side effect, it sets the pointer to a free slot, if any.
143 * This way we can concentrate most of the registry scanning in
144 * this function, which makes it easier to replace the registry
145 * with some other data structure.
147 static struct priv_fw
*
148 lookup(const char *name
, struct priv_fw
**empty_slot
)
150 struct priv_fw
*fp
= NULL
;
151 struct priv_fw
*dummy
;
154 if (empty_slot
== NULL
)
157 for (i
= 0; i
< FIRMWARE_MAX
; i
++) {
158 fp
= &firmware_table
[i
];
159 if (fp
->fw
.name
!= NULL
&& strcasecmp(name
, fp
->fw
.name
) == 0)
161 else if (!FW_INUSE(fp
))
164 return (i
< FIRMWARE_MAX
) ? fp
: NULL
;
168 * Register a firmware image with the specified name. The
169 * image name must not already be registered. If this is a
170 * subimage then parent refers to a previously registered
171 * image that this should be associated with.
173 const struct firmware
*
174 firmware_register(const char *imagename
, const void *data
, size_t datasize
,
175 unsigned int version
, const struct firmware
*parent
)
177 struct priv_fw
*match
, *frp
;
179 mtx_lock(&firmware_mtx
);
181 * Do a lookup to make sure the name is unique or find a free slot.
183 match
= lookup(imagename
, &frp
);
185 mtx_unlock(&firmware_mtx
);
186 printf("%s: image %s already registered!\n",
187 __func__
, imagename
);
191 mtx_unlock(&firmware_mtx
);
192 printf("%s: cannot register image %s, firmware table full!\n",
193 __func__
, imagename
);
196 bzero(frp
, sizeof(frp
)); /* start from a clean record */
197 frp
->fw
.name
= imagename
;
199 frp
->fw
.datasize
= datasize
;
200 frp
->fw
.version
= version
;
201 if (parent
!= NULL
) {
202 frp
->parent
= PRIV_FW(parent
);
203 frp
->parent
->refcnt
++;
205 mtx_unlock(&firmware_mtx
);
207 printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
208 imagename
, version
, datasize
, data
);
213 * Unregister/remove a firmware image. If there are outstanding
214 * references an error is returned and the image is not removed
218 firmware_unregister(const char *imagename
)
223 mtx_lock(&firmware_mtx
);
224 fp
= lookup(imagename
, NULL
);
227 * It is ok for the lookup to fail; this can happen
228 * when a module is unloaded on last reference and the
229 * module unload handler unregister's each of it's
233 } else if (fp
->refcnt
!= 0) { /* cannot unregister */
236 linker_file_t x
= fp
->file
; /* save value */
238 if (fp
->parent
!= NULL
) /* release parent reference */
239 fp
->parent
->refcnt
--;
241 * Clear the whole entry with bzero to make sure we
242 * do not forget anything. Then restore 'file' which is
243 * non-null for autoloaded images.
245 bzero(fp
, sizeof(struct priv_fw
));
249 mtx_unlock(&firmware_mtx
);
254 loadimage(void *arg
, int npending
)
256 struct thread
*td
= curthread
;
257 char *imagename
= arg
;
259 linker_file_t result
;
262 /* synchronize with the thread that dispatched us */
263 mtx_lock(&firmware_mtx
);
264 mtx_unlock(&firmware_mtx
);
266 if (td
->td_proc
->p_fd
->fd_rdir
== NULL
) {
267 printf("%s: root not mounted yet, no way to load image\n",
271 error
= linker_reference_module(imagename
, NULL
, &result
);
273 printf("%s: could not load firmware image, error %d\n",
278 mtx_lock(&firmware_mtx
);
279 fp
= lookup(imagename
, NULL
);
280 if (fp
== NULL
|| fp
->file
!= NULL
) {
281 mtx_unlock(&firmware_mtx
);
283 printf("%s: firmware image loaded, "
284 "but did not register\n", imagename
);
285 (void) linker_release_module(imagename
, NULL
, NULL
);
288 fp
->file
= result
; /* record the module identity */
289 mtx_unlock(&firmware_mtx
);
291 wakeup_one(imagename
); /* we're done */
295 * Lookup and potentially load the specified firmware image.
296 * If the firmware is not found in the registry, try to load a kernel
297 * module named as the image name.
298 * If the firmware is located, a reference is returned. The caller must
299 * release this reference for the image to be eligible for removal/unload.
301 const struct firmware
*
302 firmware_get(const char *imagename
)
304 struct task fwload_task
;
308 mtx_lock(&firmware_mtx
);
309 fp
= lookup(imagename
, NULL
);
313 * Image not present, try to load the module holding it.
316 if (priv_check(td
, PRIV_FIRMWARE_LOAD
) != 0 ||
317 securelevel_gt(td
->td_ucred
, 0) != 0) {
318 mtx_unlock(&firmware_mtx
);
319 printf("%s: insufficient privileges to "
320 "load firmware image %s\n", __func__
, imagename
);
324 * Defer load to a thread with known context. linker_reference_module
325 * may do filesystem i/o which requires root & current dirs, etc.
326 * Also we must not hold any mtx's over this call which is problematic.
328 TASK_INIT(&fwload_task
, 0, loadimage
, __DECONST(void *, imagename
));
329 taskqueue_enqueue(firmware_tq
, &fwload_task
);
330 msleep(__DECONST(void *, imagename
), &firmware_mtx
, 0, "fwload", 0);
332 * After attempting to load the module, see if the image is registered.
334 fp
= lookup(imagename
, NULL
);
336 mtx_unlock(&firmware_mtx
);
339 found
: /* common exit point on success */
341 mtx_unlock(&firmware_mtx
);
346 * Release a reference to a firmware image returned by firmware_get.
347 * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
348 * to release the resource, but the flag is only advisory.
350 * If this is the last reference to the firmware image, and this is an
351 * autoloaded module, wake up the firmware_unload_task to figure out
352 * what to do with the associated module.
355 firmware_put(const struct firmware
*p
, int flags
)
357 struct priv_fw
*fp
= PRIV_FW(p
);
359 mtx_lock(&firmware_mtx
);
361 if (fp
->refcnt
== 0) {
362 if (flags
& FIRMWARE_UNLOAD
)
363 fp
->flags
|= FW_UNLOAD
;
365 taskqueue_enqueue(firmware_tq
, &firmware_unload_task
);
367 mtx_unlock(&firmware_mtx
);
371 * Setup directory state for the firmware_tq thread so we can do i/o.
374 set_rootvnode(void *arg
, int npending
)
376 struct thread
*td
= curthread
;
377 struct proc
*p
= td
->td_proc
;
379 FILEDESC_XLOCK(p
->p_fd
);
380 if (p
->p_fd
->fd_cdir
== NULL
) {
381 p
->p_fd
->fd_cdir
= rootvnode
;
384 if (p
->p_fd
->fd_rdir
== NULL
) {
385 p
->p_fd
->fd_rdir
= rootvnode
;
388 FILEDESC_XUNLOCK(p
->p_fd
);
392 * Event handler called on mounting of /; bounce a task
393 * into the task queue thread to setup it's directories.
396 firmware_mountroot(void *arg
)
398 static struct task setroot_task
;
400 TASK_INIT(&setroot_task
, 0, set_rootvnode
, NULL
);
401 taskqueue_enqueue(firmware_tq
, &setroot_task
);
403 EVENTHANDLER_DEFINE(mountroot
, firmware_mountroot
, NULL
, 0);
406 * The body of the task in charge of unloading autoloaded modules
407 * that are not needed anymore.
408 * Images can be cross-linked so we may need to make multiple passes,
409 * but the time we spend in the loop is bounded because we clear entries
413 unloadentry(void *unused1
, int unused2
)
415 int limit
= FIRMWARE_MAX
;
416 int i
; /* current cycle */
418 mtx_lock(&firmware_mtx
);
420 * Scan the table. limit is set to make sure we make another
421 * full sweep after matching an entry that requires unloading.
423 for (i
= 0; i
< limit
; i
++) {
427 fp
= &firmware_table
[i
% FIRMWARE_MAX
];
428 if (fp
->fw
.name
== NULL
|| fp
->file
== NULL
||
429 fp
->refcnt
!= 0 || (fp
->flags
& FW_UNLOAD
) == 0)
433 * Found an entry. Now:
434 * 1. bump up limit to make sure we make another full round;
435 * 2. clear FW_UNLOAD so we don't try this entry again.
436 * 3. release the lock while trying to unload the module.
437 * 'file' remains set so that the entry cannot be reused
438 * in the meantime (it also means that fp->file will
439 * not change while we release the lock).
441 limit
= i
+ FIRMWARE_MAX
; /* make another full round */
442 fp
->flags
&= ~FW_UNLOAD
; /* do not try again */
444 mtx_unlock(&firmware_mtx
);
445 err
= linker_release_module(NULL
, NULL
, fp
->file
);
446 mtx_lock(&firmware_mtx
);
449 * We rely on the module to call firmware_unregister()
450 * on unload to actually release the entry.
451 * If err = 0 we can drop our reference as the system
452 * accepted it. Otherwise unloading failed (e.g. the
453 * module itself gave an error) so our reference is
459 mtx_unlock(&firmware_mtx
);
466 firmware_modevent(module_t mod
, int type
, void *unused
)
473 TASK_INIT(&firmware_unload_task
, 0, unloadentry
, NULL
);
474 firmware_tq
= taskqueue_create("taskqueue_firmware", M_WAITOK
,
475 taskqueue_thread_enqueue
, &firmware_tq
);
476 /* NB: use our own loop routine that sets up context */
477 (void) taskqueue_start_threads(&firmware_tq
, 1, PWAIT
,
479 if (rootvnode
!= NULL
) {
481 * Root is already mounted so we won't get an event;
484 firmware_mountroot(NULL
);
489 /* request all autoloaded modules to be released */
490 mtx_lock(&firmware_mtx
);
491 for (i
= 0; i
< FIRMWARE_MAX
; i
++) {
492 fp
= &firmware_table
[i
];
493 fp
->flags
|= FW_UNLOAD
;;
495 mtx_unlock(&firmware_mtx
);
496 taskqueue_enqueue(firmware_tq
, &firmware_unload_task
);
497 taskqueue_drain(firmware_tq
, &firmware_unload_task
);
499 for (i
= 0; i
< FIRMWARE_MAX
; i
++) {
500 fp
= &firmware_table
[i
];
501 if (fp
->fw
.name
!= NULL
) {
502 printf("%s: image %p ref %d still active slot %d\n",
503 __func__
, fp
->fw
.name
,
509 taskqueue_free(firmware_tq
);
515 static moduledata_t firmware_mod
= {
520 DECLARE_MODULE(firmware
, firmware_mod
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
);
521 MODULE_VERSION(firmware
, 1);