bridge.4: Add missing .Bl/.El
[dragonfly.git] / sys / kern / subr_firmware.c
blob10d314911323fb1ec683ddb53fe3635e96110748
1 /*-
2 * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
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.
26 * $FreeBSD: src/sys/kern/subr_firmware.c,v 1.13.2.2 2010/02/11 18:34:06 mjacob Exp $
27 * $DragonFly$
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>
36 #include <sys/lock.h>
37 #include <sys/spinlock.h>
38 #include <sys/spinlock2.h>
39 #include <sys/errno.h>
40 #include <sys/linker.h>
41 #include <sys/firmware.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/module.h>
45 #include <sys/eventhandler.h>
47 #include <sys/filedesc.h>
48 #include <sys/vnode.h>
51 * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
52 * form more details on the subsystem.
54 * 'struct firmware' is the user-visible part of the firmware table.
55 * Additional internal information is stored in a 'struct priv_fw'
56 * (currently a static array). A slot is in use if FW_INUSE is true:
59 #define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL)
62 * fw.name != NULL when an image is registered; file != NULL for
63 * autoloaded images whose handling has not been completed.
65 * The state of a slot evolves as follows:
66 * firmware_register --> fw.name = image_name
67 * (autoloaded image) --> file = module reference
68 * firmware_unregister --> fw.name = NULL
69 * (unloadentry complete) --> file = NULL
71 * In order for the above to work, the 'file' field must remain
72 * unchanged in firmware_unregister().
74 * Images residing in the same module are linked to each other
75 * through the 'parent' argument of firmware_register().
76 * One image (typically, one with the same name as the module to let
77 * the autoloading mechanism work) is considered the parent image for
78 * all other images in the same module. Children affect the refcount
79 * on the parent image preventing improper unloading of the image itself.
82 struct priv_fw {
83 int refcnt; /* reference count */
86 * parent entry, see above. Set on firmware_register(),
87 * cleared on firmware_unregister().
89 struct priv_fw *parent;
91 int flags; /* record FIRMWARE_UNLOAD requests */
92 #define FW_UNLOAD 0x100
95 * 'file' is private info managed by the autoload/unload code.
96 * Set at the end of firmware_get(), cleared only in the
97 * firmware_unload_task, so the latter can depend on its value even
98 * while the lock is not held.
100 linker_file_t file; /* module file, if autoloaded */
103 * 'fw' is the externally visible image information.
104 * We do not make it the first field in priv_fw, to avoid the
105 * temptation of casting pointers to each other.
106 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
107 * Beware, PRIV_FW does not work for a NULL pointer.
109 struct firmware fw; /* externally visible information */
113 * PRIV_FW returns the pointer to the container of struct firmware *x.
114 * Cast to intptr_t to override the 'const' attribute of x
116 #define PRIV_FW(x) ((struct priv_fw *) \
117 ((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
120 * At the moment we use a static array as backing store for the registry.
121 * Should we move to a dynamic structure, keep in mind that we cannot
122 * reallocate the array because pointers are held externally.
123 * A list may work, though.
125 #define FIRMWARE_MAX 30
126 static struct priv_fw firmware_table[FIRMWARE_MAX];
129 * Firmware module operations are handled in a separate task as they
130 * might sleep and they require directory context to do i/o.
132 static struct taskqueue *firmware_tq;
133 static struct task firmware_unload_task;
136 * This mutex protects accesses to the firmware table.
138 static struct lock firmware_lock;
139 #if 0
140 MTX_SYSINIT(firmware, &firmware_lock, "firmware table", MTX_DEF);
141 #endif
144 * Helper function to lookup a name.
145 * As a side effect, it sets the pointer to a free slot, if any.
146 * This way we can concentrate most of the registry scanning in
147 * this function, which makes it easier to replace the registry
148 * with some other data structure.
150 static struct priv_fw *
151 lookup(const char *name, struct priv_fw **empty_slot)
153 struct priv_fw *fp = NULL;
154 struct priv_fw *dummy;
155 int i;
157 if (empty_slot == NULL)
158 empty_slot = &dummy;
159 *empty_slot = NULL;
160 for (i = 0; i < FIRMWARE_MAX; i++) {
161 fp = &firmware_table[i];
162 if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
163 break;
164 else if (!FW_INUSE(fp))
165 *empty_slot = fp;
167 return (i < FIRMWARE_MAX ) ? fp : NULL;
171 * Register a firmware image with the specified name. The
172 * image name must not already be registered. If this is a
173 * subimage then parent refers to a previously registered
174 * image that this should be associated with.
176 const struct firmware *
177 firmware_register(const char *imagename, const void *data, size_t datasize,
178 unsigned int version, const struct firmware *parent)
180 struct priv_fw *match, *frp;
182 lockmgr(&firmware_lock, LK_EXCLUSIVE);
184 * Do a lookup to make sure the name is unique or find a free slot.
186 match = lookup(imagename, &frp);
187 if (match != NULL) {
188 lockmgr(&firmware_lock, LK_RELEASE);
189 kprintf("%s: image %s already registered!\n",
190 __func__, imagename);
191 return NULL;
193 if (frp == NULL) {
194 lockmgr(&firmware_lock, LK_RELEASE);
195 kprintf("%s: cannot register image %s, firmware table full!\n",
196 __func__, imagename);
197 return NULL;
199 bzero(frp, sizeof(frp)); /* start from a clean record */
200 frp->fw.name = imagename;
201 frp->fw.data = data;
202 frp->fw.datasize = datasize;
203 frp->fw.version = version;
204 if (parent != NULL) {
205 frp->parent = PRIV_FW(parent);
206 frp->parent->refcnt++;
208 lockmgr(&firmware_lock, LK_RELEASE);
209 if (bootverbose)
210 kprintf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
211 imagename, version, datasize, data);
212 return &frp->fw;
216 * Unregister/remove a firmware image. If there are outstanding
217 * references an error is returned and the image is not removed
218 * from the registry.
221 firmware_unregister(const char *imagename)
223 struct priv_fw *fp;
224 int err;
226 lockmgr(&firmware_lock, LK_EXCLUSIVE);
227 fp = lookup(imagename, NULL);
228 if (fp == NULL) {
230 * It is ok for the lookup to fail; this can happen
231 * when a module is unloaded on last reference and the
232 * module unload handler unregister's each of it's
233 * firmware images.
235 err = 0;
236 } else if (fp->refcnt != 0) { /* cannot unregister */
237 err = EBUSY;
238 } else {
239 linker_file_t x = fp->file; /* save value */
241 if (fp->parent != NULL) /* release parent reference */
242 fp->parent->refcnt--;
244 * Clear the whole entry with bzero to make sure we
245 * do not forget anything. Then restore 'file' which is
246 * non-null for autoloaded images.
248 bzero(fp, sizeof(struct priv_fw));
249 fp->file = x;
250 err = 0;
252 lockmgr(&firmware_lock, LK_RELEASE);
253 return err;
256 static void
257 loadimage(void *arg, int npending)
259 #ifdef notyet
260 struct thread *td = curthread;
261 #endif
262 char *imagename = arg;
263 struct priv_fw *fp;
264 linker_file_t result;
265 int error;
267 /* synchronize with the thread that dispatched us */
268 lockmgr(&firmware_lock, LK_EXCLUSIVE);
269 lockmgr(&firmware_lock, LK_RELEASE);
271 /* JAT
272 if (td->td_proc->p_fd->fd_rdir == NULL) {
273 kprintf("%s: root not mounted yet, no way to load image\n",
274 imagename);
275 goto done;
278 error = linker_reference_module(imagename, NULL, &result);
279 if (error != 0) {
280 kprintf("%s: could not load firmware image, error %d\n",
281 imagename, error);
282 goto done;
285 lockmgr(&firmware_lock, LK_EXCLUSIVE);
286 fp = lookup(imagename, NULL);
287 if (fp == NULL || fp->file != NULL) {
288 lockmgr(&firmware_lock, LK_RELEASE);
289 if (fp == NULL)
290 kprintf("%s: firmware image loaded, "
291 "but did not register\n", imagename);
292 (void) linker_release_module(imagename, NULL, NULL);
293 goto done;
295 fp->file = result; /* record the module identity */
296 lockmgr(&firmware_lock, LK_RELEASE);
297 done:
298 wakeup_one(imagename); /* we're done */
302 * Lookup and potentially load the specified firmware image.
303 * If the firmware is not found in the registry, try to load a kernel
304 * module named as the image name.
305 * If the firmware is located, a reference is returned. The caller must
306 * release this reference for the image to be eligible for removal/unload.
308 const struct firmware *
309 firmware_get(const char *imagename)
311 struct task fwload_task;
312 struct thread *td;
313 struct priv_fw *fp;
315 lockmgr(&firmware_lock, LK_EXCLUSIVE);
316 fp = lookup(imagename, NULL);
317 if (fp != NULL)
318 goto found;
320 * Image not present, try to load the module holding it.
322 td = curthread;
323 if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 || securelevel > 0) {
324 lockmgr(&firmware_lock, LK_RELEASE);
325 kprintf("%s: insufficient privileges to "
326 "load firmware image %s\n", __func__, imagename);
327 return NULL;
330 * Defer load to a thread with known context. linker_reference_module
331 * may do filesystem i/o which requires root & current dirs, etc.
332 * Also we must not hold any lock's over this call which is problematic.
334 if (!cold) {
335 TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *,
336 imagename));
337 taskqueue_enqueue(firmware_tq, &fwload_task);
338 lksleep(__DECONST(void *, imagename), &firmware_lock, 0,
339 "fwload", 0);
342 * After attempting to load the module, see if the image is registered.
344 fp = lookup(imagename, NULL);
345 if (fp == NULL) {
346 lockmgr(&firmware_lock, LK_RELEASE);
347 return NULL;
349 found: /* common exit point on success */
350 fp->refcnt++;
351 lockmgr(&firmware_lock, LK_RELEASE);
352 return &fp->fw;
356 * Release a reference to a firmware image returned by firmware_get.
357 * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
358 * to release the resource, but the flag is only advisory.
360 * If this is the last reference to the firmware image, and this is an
361 * autoloaded module, wake up the firmware_unload_task to figure out
362 * what to do with the associated module.
364 void
365 firmware_put(const struct firmware *p, int flags)
367 struct priv_fw *fp = PRIV_FW(p);
369 lockmgr(&firmware_lock, LK_EXCLUSIVE);
370 fp->refcnt--;
371 if (fp->refcnt == 0) {
372 if (flags & FIRMWARE_UNLOAD)
373 fp->flags |= FW_UNLOAD;
374 if (fp->file)
375 taskqueue_enqueue(firmware_tq, &firmware_unload_task);
377 lockmgr(&firmware_lock, LK_RELEASE);
380 #ifdef notyet
382 * Setup directory state for the firmware_tq thread so we can do i/o.
384 static void
385 set_rootvnode(void *arg, int npending)
387 struct thread *td = curthread;
388 struct proc *p = td->td_proc;
391 #if 0
392 spin_lock_wr(&p->p_fd->fd_spin);
393 if (p->p_fd->fd_cdir == NULL) {
394 p->p_fd->fd_cdir = rootvnode;
395 vref(rootvnode);
397 if (p->p_fd->fd_rdir == NULL) {
398 p->p_fd->fd_rdir = rootvnode;
399 vref(rootvnode);
401 spin_unlock_wr(&p->p_fd->fd_spin);
403 kfree(arg, M_TEMP);
404 #endif
408 * Event handler called on mounting of /; bounce a task
409 * into the task queue thread to setup it's directories.
411 static void
412 firmware_mountroot(void *arg)
414 struct task *setroot_task;
416 setroot_task = kmalloc(sizeof(struct task), M_TEMP, M_NOWAIT);
417 if (setroot_task != NULL) {
418 TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
419 taskqueue_enqueue(firmware_tq, setroot_task);
420 } else
421 kprintf("%s: no memory for task!\n", __func__);
423 EVENTHANDLER_DECLARE(mountroot, firmware_mountroot);
424 #endif
427 * The body of the task in charge of unloading autoloaded modules
428 * that are not needed anymore.
429 * Images can be cross-linked so we may need to make multiple passes,
430 * but the time we spend in the loop is bounded because we clear entries
431 * as we touch them.
433 static void
434 unloadentry(void *unused1, int unused2)
436 int limit = FIRMWARE_MAX;
437 int i; /* current cycle */
439 lockmgr(&firmware_lock, LK_EXCLUSIVE);
441 * Scan the table. limit is set to make sure we make another
442 * full sweep after matching an entry that requires unloading.
444 for (i = 0; i < limit; i++) {
445 struct priv_fw *fp;
446 int err;
448 fp = &firmware_table[i % FIRMWARE_MAX];
449 if (fp->fw.name == NULL || fp->file == NULL ||
450 fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
451 continue;
454 * Found an entry. Now:
455 * 1. bump up limit to make sure we make another full round;
456 * 2. clear FW_UNLOAD so we don't try this entry again.
457 * 3. release the lock while trying to unload the module.
458 * 'file' remains set so that the entry cannot be reused
459 * in the meantime (it also means that fp->file will
460 * not change while we release the lock).
462 limit = i + FIRMWARE_MAX; /* make another full round */
463 fp->flags &= ~FW_UNLOAD; /* do not try again */
465 lockmgr(&firmware_lock, LK_RELEASE);
466 err = linker_release_module(NULL, NULL, fp->file);
467 lockmgr(&firmware_lock, LK_EXCLUSIVE);
470 * We rely on the module to call firmware_unregister()
471 * on unload to actually release the entry.
472 * If err = 0 we can drop our reference as the system
473 * accepted it. Otherwise unloading failed (e.g. the
474 * module itself gave an error) so our reference is
475 * still valid.
477 if (err == 0)
478 fp->file = NULL;
480 lockmgr(&firmware_lock, LK_RELEASE);
484 * Module glue.
486 static int
487 firmware_modevent(module_t mod, int type, void *unused)
489 struct priv_fw *fp;
490 int i, err;
492 switch (type) {
493 case MOD_LOAD:
494 TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
495 lockinit(&firmware_lock, "firmware table", 0, LK_CANRECURSE);
496 firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
497 taskqueue_thread_enqueue, &firmware_tq);
498 /* NB: use our own loop routine that sets up context */
499 (void) taskqueue_start_threads(&firmware_tq, 1, TDPRI_KERN_DAEMON,
500 -1, "firmware taskq");
501 if (rootvnode != NULL) {
503 * Root is already mounted so we won't get an event;
504 * simulate one here.
506 #ifdef notyet
507 firmware_mountroot(NULL);
508 #endif
510 return 0;
512 case MOD_UNLOAD:
513 /* request all autoloaded modules to be released */
514 lockmgr(&firmware_lock, LK_EXCLUSIVE);
515 for (i = 0; i < FIRMWARE_MAX; i++) {
516 fp = &firmware_table[i];
517 fp->flags |= FW_UNLOAD;
519 lockmgr(&firmware_lock, LK_RELEASE);
520 taskqueue_enqueue(firmware_tq, &firmware_unload_task);
521 taskqueue_drain(firmware_tq, &firmware_unload_task);
522 err = 0;
523 for (i = 0; i < FIRMWARE_MAX; i++) {
524 fp = &firmware_table[i];
525 if (fp->fw.name != NULL) {
526 kprintf("%s: image %p ref %d still active slot %d\n",
527 __func__, fp->fw.name,
528 fp->refcnt, i);
529 err = EINVAL;
532 if (err == 0)
533 taskqueue_free(firmware_tq);
534 return err;
536 return EINVAL;
539 static moduledata_t firmware_mod = {
540 "firmware",
541 firmware_modevent,
542 NULL
544 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
545 MODULE_VERSION(firmware, 1);