1154974Smlaier/*- 2178042Ssam * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com> 3154974Smlaier * All rights reserved. 4154974Smlaier * 5154974Smlaier * Redistribution and use in source and binary forms, with or without 6154974Smlaier * modification, are permitted provided that the following conditions 7154974Smlaier * are met: 8154974Smlaier * 1. Redistributions of source code must retain the above copyright 9154974Smlaier * notice unmodified, this list of conditions, and the following 10154974Smlaier * disclaimer. 11154974Smlaier * 2. Redistributions in binary form must reproduce the above copyright 12154974Smlaier * notice, this list of conditions and the following disclaimer in the 13154974Smlaier * documentation and/or other materials provided with the distribution. 14154974Smlaier * 15154974Smlaier * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16154974Smlaier * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17154974Smlaier * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18154974Smlaier * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19154974Smlaier * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20154974Smlaier * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21154974Smlaier * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22154974Smlaier * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23154974Smlaier * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24154974Smlaier * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25154974Smlaier */ 26154974Smlaier 27154974Smlaier#include <sys/cdefs.h> 28154974Smlaier__FBSDID("$FreeBSD$"); 29154974Smlaier 30154974Smlaier#include <sys/param.h> 31154974Smlaier#include <sys/kernel.h> 32154974Smlaier#include <sys/malloc.h> 33154974Smlaier#include <sys/queue.h> 34154974Smlaier#include <sys/taskqueue.h> 35154974Smlaier#include <sys/systm.h> 36154974Smlaier#include <sys/lock.h> 37154974Smlaier#include <sys/mutex.h> 38154974Smlaier#include <sys/errno.h> 39154974Smlaier#include <sys/linker.h> 40154974Smlaier#include <sys/firmware.h> 41164033Srwatson#include <sys/priv.h> 42154974Smlaier#include <sys/proc.h> 43154974Smlaier#include <sys/module.h> 44178042Ssam#include <sys/eventhandler.h> 45154974Smlaier 46178042Ssam#include <sys/filedesc.h> 47178042Ssam#include <sys/vnode.h> 48178042Ssam 49166756Sluigi/* 50166756Sluigi * Loadable firmware support. See sys/sys/firmware.h and firmware(9) 51166756Sluigi * form more details on the subsystem. 52166756Sluigi * 53166756Sluigi * 'struct firmware' is the user-visible part of the firmware table. 54166756Sluigi * Additional internal information is stored in a 'struct priv_fw' 55166756Sluigi * (currently a static array). A slot is in use if FW_INUSE is true: 56166756Sluigi */ 57166756Sluigi 58166756Sluigi#define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL) 59166756Sluigi 60166756Sluigi/* 61166756Sluigi * fw.name != NULL when an image is registered; file != NULL for 62166756Sluigi * autoloaded images whose handling has not been completed. 63166756Sluigi * 64166756Sluigi * The state of a slot evolves as follows: 65166756Sluigi * firmware_register --> fw.name = image_name 66166756Sluigi * (autoloaded image) --> file = module reference 67166756Sluigi * firmware_unregister --> fw.name = NULL 68166756Sluigi * (unloadentry complete) --> file = NULL 69166756Sluigi * 70166756Sluigi * In order for the above to work, the 'file' field must remain 71166756Sluigi * unchanged in firmware_unregister(). 72166756Sluigi * 73166756Sluigi * Images residing in the same module are linked to each other 74166756Sluigi * through the 'parent' argument of firmware_register(). 75166756Sluigi * One image (typically, one with the same name as the module to let 76166756Sluigi * the autoloading mechanism work) is considered the parent image for 77166756Sluigi * all other images in the same module. Children affect the refcount 78166756Sluigi * on the parent image preventing improper unloading of the image itself. 79166756Sluigi */ 80166756Sluigi 81166756Sluigistruct priv_fw { 82166756Sluigi int refcnt; /* reference count */ 83166756Sluigi 84166756Sluigi /* 85166756Sluigi * parent entry, see above. Set on firmware_register(), 86166756Sluigi * cleared on firmware_unregister(). 87166756Sluigi */ 88166756Sluigi struct priv_fw *parent; 89166756Sluigi 90166756Sluigi int flags; /* record FIRMWARE_UNLOAD requests */ 91166756Sluigi#define FW_UNLOAD 0x100 92166756Sluigi 93166756Sluigi /* 94166756Sluigi * 'file' is private info managed by the autoload/unload code. 95166756Sluigi * Set at the end of firmware_get(), cleared only in the 96178042Ssam * firmware_unload_task, so the latter can depend on its value even 97166756Sluigi * while the lock is not held. 98166756Sluigi */ 99166756Sluigi linker_file_t file; /* module file, if autoloaded */ 100166756Sluigi 101166756Sluigi /* 102166756Sluigi * 'fw' is the externally visible image information. 103166756Sluigi * We do not make it the first field in priv_fw, to avoid the 104166756Sluigi * temptation of casting pointers to each other. 105166756Sluigi * Use PRIV_FW(fw) to get a pointer to the cointainer of fw. 106166756Sluigi * Beware, PRIV_FW does not work for a NULL pointer. 107166756Sluigi */ 108166756Sluigi struct firmware fw; /* externally visible information */ 109166756Sluigi}; 110166756Sluigi 111166756Sluigi/* 112166756Sluigi * PRIV_FW returns the pointer to the container of struct firmware *x. 113166756Sluigi * Cast to intptr_t to override the 'const' attribute of x 114166756Sluigi */ 115166756Sluigi#define PRIV_FW(x) ((struct priv_fw *) \ 116166756Sluigi ((intptr_t)(x) - offsetof(struct priv_fw, fw)) ) 117166756Sluigi 118166756Sluigi/* 119166756Sluigi * At the moment we use a static array as backing store for the registry. 120166756Sluigi * Should we move to a dynamic structure, keep in mind that we cannot 121166756Sluigi * reallocate the array because pointers are held externally. 122166756Sluigi * A list may work, though. 123166756Sluigi */ 124204850Simp#define FIRMWARE_MAX 50 125166756Sluigistatic struct priv_fw firmware_table[FIRMWARE_MAX]; 126166756Sluigi 127166756Sluigi/* 128178042Ssam * Firmware module operations are handled in a separate task as they 129178042Ssam * might sleep and they require directory context to do i/o. 130166756Sluigi */ 131178042Ssamstatic struct taskqueue *firmware_tq; 132178042Ssamstatic struct task firmware_unload_task; 133166756Sluigi 134166756Sluigi/* 135166756Sluigi * This mutex protects accesses to the firmware table. 136166756Sluigi */ 137178042Ssamstatic struct mtx firmware_mtx; 138154974SmlaierMTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF); 139154974Smlaier 140154974Smlaier/* 141166756Sluigi * Helper function to lookup a name. 142166756Sluigi * As a side effect, it sets the pointer to a free slot, if any. 143166756Sluigi * This way we can concentrate most of the registry scanning in 144166756Sluigi * this function, which makes it easier to replace the registry 145166756Sluigi * with some other data structure. 146166756Sluigi */ 147166756Sluigistatic struct priv_fw * 148166756Sluigilookup(const char *name, struct priv_fw **empty_slot) 149166756Sluigi{ 150166756Sluigi struct priv_fw *fp = NULL; 151166756Sluigi struct priv_fw *dummy; 152166756Sluigi int i; 153166756Sluigi 154166756Sluigi if (empty_slot == NULL) 155166756Sluigi empty_slot = &dummy; 156166756Sluigi *empty_slot = NULL; 157166756Sluigi for (i = 0; i < FIRMWARE_MAX; i++) { 158166756Sluigi fp = &firmware_table[i]; 159166756Sluigi if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0) 160166756Sluigi break; 161166756Sluigi else if (!FW_INUSE(fp)) 162166756Sluigi *empty_slot = fp; 163166756Sluigi } 164166756Sluigi return (i < FIRMWARE_MAX ) ? fp : NULL; 165166756Sluigi} 166166756Sluigi 167166756Sluigi/* 168154974Smlaier * Register a firmware image with the specified name. The 169154974Smlaier * image name must not already be registered. If this is a 170154974Smlaier * subimage then parent refers to a previously registered 171154974Smlaier * image that this should be associated with. 172154974Smlaier */ 173166756Sluigiconst struct firmware * 174154974Smlaierfirmware_register(const char *imagename, const void *data, size_t datasize, 175166756Sluigi unsigned int version, const struct firmware *parent) 176154974Smlaier{ 177166756Sluigi struct priv_fw *match, *frp; 178234201Sadrian char *str; 179154974Smlaier 180234201Sadrian str = strdup(imagename, M_TEMP); 181234201Sadrian 182154974Smlaier mtx_lock(&firmware_mtx); 183166756Sluigi /* 184166756Sluigi * Do a lookup to make sure the name is unique or find a free slot. 185166756Sluigi */ 186166756Sluigi match = lookup(imagename, &frp); 187166756Sluigi if (match != NULL) { 188166756Sluigi mtx_unlock(&firmware_mtx); 189166756Sluigi printf("%s: image %s already registered!\n", 190166756Sluigi __func__, imagename); 191234201Sadrian free(str, M_TEMP); 192166756Sluigi return NULL; 193154974Smlaier } 194154974Smlaier if (frp == NULL) { 195154974Smlaier mtx_unlock(&firmware_mtx); 196154974Smlaier printf("%s: cannot register image %s, firmware table full!\n", 197154974Smlaier __func__, imagename); 198234201Sadrian free(str, M_TEMP); 199154974Smlaier return NULL; 200154974Smlaier } 201237546Skevlo bzero(frp, sizeof(*frp)); /* start from a clean record */ 202234201Sadrian frp->fw.name = str; 203166756Sluigi frp->fw.data = data; 204166756Sluigi frp->fw.datasize = datasize; 205166756Sluigi frp->fw.version = version; 206227689Snp if (parent != NULL) 207166756Sluigi frp->parent = PRIV_FW(parent); 208154974Smlaier mtx_unlock(&firmware_mtx); 209166465Smlaier if (bootverbose) 210166465Smlaier printf("firmware: '%s' version %u: %zu bytes loaded at %p\n", 211166465Smlaier imagename, version, datasize, data); 212166756Sluigi return &frp->fw; 213154974Smlaier} 214154974Smlaier 215154974Smlaier/* 216154974Smlaier * Unregister/remove a firmware image. If there are outstanding 217154974Smlaier * references an error is returned and the image is not removed 218154974Smlaier * from the registry. 219154974Smlaier */ 220154974Smlaierint 221154974Smlaierfirmware_unregister(const char *imagename) 222154974Smlaier{ 223166756Sluigi struct priv_fw *fp; 224166756Sluigi int err; 225154974Smlaier 226154974Smlaier mtx_lock(&firmware_mtx); 227166756Sluigi fp = lookup(imagename, NULL); 228166756Sluigi if (fp == NULL) { 229166756Sluigi /* 230166756Sluigi * It is ok for the lookup to fail; this can happen 231166756Sluigi * when a module is unloaded on last reference and the 232166756Sluigi * module unload handler unregister's each of it's 233166756Sluigi * firmware images. 234166756Sluigi */ 235166756Sluigi err = 0; 236166756Sluigi } else if (fp->refcnt != 0) { /* cannot unregister */ 237166756Sluigi err = EBUSY; 238234201Sadrian } else { 239178042Ssam linker_file_t x = fp->file; /* save value */ 240166756Sluigi 241166756Sluigi /* 242166756Sluigi * Clear the whole entry with bzero to make sure we 243166756Sluigi * do not forget anything. Then restore 'file' which is 244166756Sluigi * non-null for autoloaded images. 245166756Sluigi */ 246234201Sadrian free((void *) (uintptr_t) fp->fw.name, M_TEMP); 247166756Sluigi bzero(fp, sizeof(struct priv_fw)); 248166756Sluigi fp->file = x; 249166756Sluigi err = 0; 250154974Smlaier } 251154974Smlaier mtx_unlock(&firmware_mtx); 252166756Sluigi return err; 253154974Smlaier} 254154974Smlaier 255178042Ssamstatic void 256178042Ssamloadimage(void *arg, int npending) 257178042Ssam{ 258178042Ssam struct thread *td = curthread; 259178042Ssam char *imagename = arg; 260178042Ssam struct priv_fw *fp; 261178042Ssam linker_file_t result; 262178042Ssam int error; 263178042Ssam 264178042Ssam /* synchronize with the thread that dispatched us */ 265178042Ssam mtx_lock(&firmware_mtx); 266178042Ssam mtx_unlock(&firmware_mtx); 267178042Ssam 268178042Ssam if (td->td_proc->p_fd->fd_rdir == NULL) { 269178042Ssam printf("%s: root not mounted yet, no way to load image\n", 270178042Ssam imagename); 271178042Ssam goto done; 272178042Ssam } 273178042Ssam error = linker_reference_module(imagename, NULL, &result); 274178042Ssam if (error != 0) { 275178042Ssam printf("%s: could not load firmware image, error %d\n", 276178042Ssam imagename, error); 277178042Ssam goto done; 278178042Ssam } 279178042Ssam 280178042Ssam mtx_lock(&firmware_mtx); 281178042Ssam fp = lookup(imagename, NULL); 282178042Ssam if (fp == NULL || fp->file != NULL) { 283178042Ssam mtx_unlock(&firmware_mtx); 284178042Ssam if (fp == NULL) 285178042Ssam printf("%s: firmware image loaded, " 286178042Ssam "but did not register\n", imagename); 287178042Ssam (void) linker_release_module(imagename, NULL, NULL); 288178042Ssam goto done; 289178042Ssam } 290178042Ssam fp->file = result; /* record the module identity */ 291178042Ssam mtx_unlock(&firmware_mtx); 292178042Ssamdone: 293178042Ssam wakeup_one(imagename); /* we're done */ 294178042Ssam} 295178042Ssam 296154974Smlaier/* 297154974Smlaier * Lookup and potentially load the specified firmware image. 298166756Sluigi * If the firmware is not found in the registry, try to load a kernel 299166756Sluigi * module named as the image name. 300166756Sluigi * If the firmware is located, a reference is returned. The caller must 301166756Sluigi * release this reference for the image to be eligible for removal/unload. 302154974Smlaier */ 303166756Sluigiconst struct firmware * 304154974Smlaierfirmware_get(const char *imagename) 305154974Smlaier{ 306178042Ssam struct task fwload_task; 307154974Smlaier struct thread *td; 308166756Sluigi struct priv_fw *fp; 309154974Smlaier 310154974Smlaier mtx_lock(&firmware_mtx); 311166756Sluigi fp = lookup(imagename, NULL); 312166756Sluigi if (fp != NULL) 313166756Sluigi goto found; 314154974Smlaier /* 315166756Sluigi * Image not present, try to load the module holding it. 316154974Smlaier */ 317154974Smlaier td = curthread; 318164033Srwatson if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 || 319164033Srwatson securelevel_gt(td->td_ucred, 0) != 0) { 320178042Ssam mtx_unlock(&firmware_mtx); 321154974Smlaier printf("%s: insufficient privileges to " 322154974Smlaier "load firmware image %s\n", __func__, imagename); 323154974Smlaier return NULL; 324154974Smlaier } 325178042Ssam /* 326178042Ssam * Defer load to a thread with known context. linker_reference_module 327178042Ssam * may do filesystem i/o which requires root & current dirs, etc. 328178042Ssam * Also we must not hold any mtx's over this call which is problematic. 329178042Ssam */ 330184842Sgallatin if (!cold) { 331184842Sgallatin TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *, 332184842Sgallatin imagename)); 333184842Sgallatin taskqueue_enqueue(firmware_tq, &fwload_task); 334184842Sgallatin msleep(__DECONST(void *, imagename), &firmware_mtx, 0, 335184842Sgallatin "fwload", 0); 336184842Sgallatin } 337166756Sluigi /* 338178042Ssam * After attempting to load the module, see if the image is registered. 339166756Sluigi */ 340166756Sluigi fp = lookup(imagename, NULL); 341166756Sluigi if (fp == NULL) { 342154974Smlaier mtx_unlock(&firmware_mtx); 343166756Sluigi return NULL; 344166756Sluigi } 345166756Sluigifound: /* common exit point on success */ 346227689Snp if (fp->refcnt == 0 && fp->parent != NULL) 347227689Snp fp->parent->refcnt++; 348166756Sluigi fp->refcnt++; 349154974Smlaier mtx_unlock(&firmware_mtx); 350166756Sluigi return &fp->fw; 351154974Smlaier} 352154974Smlaier 353154974Smlaier/* 354166756Sluigi * Release a reference to a firmware image returned by firmware_get. 355166756Sluigi * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire 356166756Sluigi * to release the resource, but the flag is only advisory. 357166756Sluigi * 358166756Sluigi * If this is the last reference to the firmware image, and this is an 359178042Ssam * autoloaded module, wake up the firmware_unload_task to figure out 360178042Ssam * what to do with the associated module. 361154974Smlaier */ 362154974Smlaiervoid 363166756Sluigifirmware_put(const struct firmware *p, int flags) 364154974Smlaier{ 365166756Sluigi struct priv_fw *fp = PRIV_FW(p); 366166756Sluigi 367154974Smlaier mtx_lock(&firmware_mtx); 368154974Smlaier fp->refcnt--; 369159486Siedowse if (fp->refcnt == 0) { 370227689Snp if (fp->parent != NULL) 371227689Snp fp->parent->refcnt--; 372166756Sluigi if (flags & FIRMWARE_UNLOAD) 373166756Sluigi fp->flags |= FW_UNLOAD; 374166756Sluigi if (fp->file) 375178042Ssam taskqueue_enqueue(firmware_tq, &firmware_unload_task); 376159486Siedowse } 377154974Smlaier mtx_unlock(&firmware_mtx); 378154974Smlaier} 379154974Smlaier 380154974Smlaier/* 381178042Ssam * Setup directory state for the firmware_tq thread so we can do i/o. 382178042Ssam */ 383178042Ssamstatic void 384178042Ssamset_rootvnode(void *arg, int npending) 385178042Ssam{ 386178042Ssam struct thread *td = curthread; 387178042Ssam struct proc *p = td->td_proc; 388178042Ssam 389178042Ssam FILEDESC_XLOCK(p->p_fd); 390178042Ssam if (p->p_fd->fd_cdir == NULL) { 391178042Ssam p->p_fd->fd_cdir = rootvnode; 392178042Ssam VREF(rootvnode); 393178042Ssam } 394178042Ssam if (p->p_fd->fd_rdir == NULL) { 395178042Ssam p->p_fd->fd_rdir = rootvnode; 396178042Ssam VREF(rootvnode); 397178042Ssam } 398178042Ssam FILEDESC_XUNLOCK(p->p_fd); 399183614Ssam 400183614Ssam free(arg, M_TEMP); 401178042Ssam} 402178042Ssam 403178042Ssam/* 404178042Ssam * Event handler called on mounting of /; bounce a task 405178042Ssam * into the task queue thread to setup it's directories. 406178042Ssam */ 407178042Ssamstatic void 408178042Ssamfirmware_mountroot(void *arg) 409178042Ssam{ 410183614Ssam struct task *setroot_task; 411178042Ssam 412183614Ssam setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT); 413183614Ssam if (setroot_task != NULL) { 414183614Ssam TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task); 415183614Ssam taskqueue_enqueue(firmware_tq, setroot_task); 416183614Ssam } else 417183614Ssam printf("%s: no memory for task!\n", __func__); 418178042Ssam} 419178042SsamEVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0); 420178042Ssam 421178042Ssam/* 422166756Sluigi * The body of the task in charge of unloading autoloaded modules 423166756Sluigi * that are not needed anymore. 424166756Sluigi * Images can be cross-linked so we may need to make multiple passes, 425166756Sluigi * but the time we spend in the loop is bounded because we clear entries 426166756Sluigi * as we touch them. 427166756Sluigi */ 428166756Sluigistatic void 429166756Sluigiunloadentry(void *unused1, int unused2) 430166756Sluigi{ 431166756Sluigi int limit = FIRMWARE_MAX; 432166756Sluigi int i; /* current cycle */ 433166756Sluigi 434166756Sluigi mtx_lock(&firmware_mtx); 435166756Sluigi /* 436166756Sluigi * Scan the table. limit is set to make sure we make another 437166756Sluigi * full sweep after matching an entry that requires unloading. 438166756Sluigi */ 439166756Sluigi for (i = 0; i < limit; i++) { 440166756Sluigi struct priv_fw *fp; 441166756Sluigi int err; 442166756Sluigi 443166756Sluigi fp = &firmware_table[i % FIRMWARE_MAX]; 444166756Sluigi if (fp->fw.name == NULL || fp->file == NULL || 445166756Sluigi fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0) 446166756Sluigi continue; 447166756Sluigi 448166756Sluigi /* 449166756Sluigi * Found an entry. Now: 450166756Sluigi * 1. bump up limit to make sure we make another full round; 451166756Sluigi * 2. clear FW_UNLOAD so we don't try this entry again. 452166756Sluigi * 3. release the lock while trying to unload the module. 453166756Sluigi * 'file' remains set so that the entry cannot be reused 454166756Sluigi * in the meantime (it also means that fp->file will 455166756Sluigi * not change while we release the lock). 456166756Sluigi */ 457166756Sluigi limit = i + FIRMWARE_MAX; /* make another full round */ 458166756Sluigi fp->flags &= ~FW_UNLOAD; /* do not try again */ 459166756Sluigi 460166756Sluigi mtx_unlock(&firmware_mtx); 461166756Sluigi err = linker_release_module(NULL, NULL, fp->file); 462166756Sluigi mtx_lock(&firmware_mtx); 463166756Sluigi 464166756Sluigi /* 465166756Sluigi * We rely on the module to call firmware_unregister() 466166756Sluigi * on unload to actually release the entry. 467166756Sluigi * If err = 0 we can drop our reference as the system 468166756Sluigi * accepted it. Otherwise unloading failed (e.g. the 469166756Sluigi * module itself gave an error) so our reference is 470166756Sluigi * still valid. 471166756Sluigi */ 472166756Sluigi if (err == 0) 473166756Sluigi fp->file = NULL; 474166756Sluigi } 475166756Sluigi mtx_unlock(&firmware_mtx); 476166756Sluigi} 477166756Sluigi 478166756Sluigi/* 479154974Smlaier * Module glue. 480154974Smlaier */ 481154974Smlaierstatic int 482154974Smlaierfirmware_modevent(module_t mod, int type, void *unused) 483154974Smlaier{ 484166756Sluigi struct priv_fw *fp; 485178042Ssam int i, err; 486159486Siedowse 487154974Smlaier switch (type) { 488154974Smlaier case MOD_LOAD: 489178042Ssam TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL); 490178042Ssam firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK, 491178042Ssam taskqueue_thread_enqueue, &firmware_tq); 492178042Ssam /* NB: use our own loop routine that sets up context */ 493178042Ssam (void) taskqueue_start_threads(&firmware_tq, 1, PWAIT, 494178042Ssam "firmware taskq"); 495178042Ssam if (rootvnode != NULL) { 496178042Ssam /* 497178042Ssam * Root is already mounted so we won't get an event; 498178042Ssam * simulate one here. 499178042Ssam */ 500178042Ssam firmware_mountroot(NULL); 501178042Ssam } 502154974Smlaier return 0; 503166756Sluigi 504154974Smlaier case MOD_UNLOAD: 505166756Sluigi /* request all autoloaded modules to be released */ 506166756Sluigi mtx_lock(&firmware_mtx); 507159486Siedowse for (i = 0; i < FIRMWARE_MAX; i++) { 508159589Sjhb fp = &firmware_table[i]; 509201758Smbr fp->flags |= FW_UNLOAD; 510159486Siedowse } 511166756Sluigi mtx_unlock(&firmware_mtx); 512178042Ssam taskqueue_enqueue(firmware_tq, &firmware_unload_task); 513178042Ssam taskqueue_drain(firmware_tq, &firmware_unload_task); 514178042Ssam err = 0; 515166756Sluigi for (i = 0; i < FIRMWARE_MAX; i++) { 516166756Sluigi fp = &firmware_table[i]; 517166756Sluigi if (fp->fw.name != NULL) { 518166756Sluigi printf("%s: image %p ref %d still active slot %d\n", 519166756Sluigi __func__, fp->fw.name, 520166756Sluigi fp->refcnt, i); 521166756Sluigi err = EINVAL; 522166756Sluigi } 523166756Sluigi } 524178042Ssam if (err == 0) 525178042Ssam taskqueue_free(firmware_tq); 526166756Sluigi return err; 527154974Smlaier } 528154974Smlaier return EINVAL; 529154974Smlaier} 530154974Smlaier 531154974Smlaierstatic moduledata_t firmware_mod = { 532154974Smlaier "firmware", 533154974Smlaier firmware_modevent, 534188057Simp NULL 535154974Smlaier}; 536154974SmlaierDECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 537154974SmlaierMODULE_VERSION(firmware, 1); 538