xref: /freebsd-14.2/sys/kern/subr_firmware.c (revision d36ba398)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2008, Sam Leffler <[email protected]>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/errno.h>
32 #include <sys/eventhandler.h>
33 #include <sys/fcntl.h>
34 #include <sys/firmware.h>
35 #include <sys/kernel.h>
36 #include <sys/linker.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/namei.h>
42 #include <sys/priv.h>
43 #include <sys/proc.h>
44 #include <sys/queue.h>
45 #include <sys/sbuf.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/taskqueue.h>
49 
50 #include <sys/filedesc.h>
51 #include <sys/vnode.h>
52 
53 /*
54  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
55  * form more details on the subsystem.
56  *
57  * 'struct firmware' is the user-visible part of the firmware table.
58  * Additional internal information is stored in a 'struct priv_fw',
59  * which embeds the public firmware structure.
60  */
61 
62 /*
63  * fw.name != NULL when an image is registered; file != NULL for
64  * autoloaded images whose handling has not been completed.
65  *
66  * The state of a slot evolves as follows:
67  *	firmware_register	-->  fw.name = image_name
68  *	(autoloaded image)	-->  file = module reference
69  *	firmware_unregister	-->  fw.name = NULL
70  *	(unloadentry complete)	-->  file = NULL
71  *
72  * In order for the above to work, the 'file' field must remain
73  * unchanged in firmware_unregister().
74  *
75  * Images residing in the same module are linked to each other
76  * through the 'parent' argument of firmware_register().
77  * One image (typically, one with the same name as the module to let
78  * the autoloading mechanism work) is considered the parent image for
79  * all other images in the same module. Children affect the refcount
80  * on the parent image preventing improper unloading of the image itself.
81  */
82 
83 struct priv_fw {
84 	int		refcnt;		/* reference count */
85 	LIST_ENTRY(priv_fw) link;	/* table linkage */
86 
87 	/*
88 	 * parent entry, see above. Set on firmware_register(),
89 	 * cleared on firmware_unregister().
90 	 */
91 	struct priv_fw	*parent;
92 
93 	int 		flags;
94 #define FW_BINARY	0x080	/* Firmware directly loaded, file == NULL */
95 #define FW_UNLOAD	0x100	/* record FIRMWARE_UNLOAD requests */
96 
97 	/*
98 	 * 'file' is private info managed by the autoload/unload code.
99 	 * Set at the end of firmware_get(), cleared only in the
100 	 * firmware_unload_task, so the latter can depend on its value even
101 	 * while the lock is not held.
102 	 */
103 	linker_file_t   file;	/* module file, if autoloaded */
104 
105 	/*
106 	 * 'fw' is the externally visible image information.
107 	 * We do not make it the first field in priv_fw, to avoid the
108 	 * temptation of casting pointers to each other.
109 	 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
110 	 * Beware, PRIV_FW does not work for a NULL pointer.
111 	 */
112 	struct firmware	fw;	/* externally visible information */
113 };
114 
115 /*
116  * PRIV_FW returns the pointer to the container of struct firmware *x.
117  * Cast to intptr_t to override the 'const' attribute of x
118  */
119 #define PRIV_FW(x)	((struct priv_fw *)		\
120 	((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
121 
122 /*
123  * Global firmware image registry.
124  */
125 static LIST_HEAD(, priv_fw) firmware_table;
126 
127 /*
128  * Firmware module operations are handled in a separate task as they
129  * might sleep and they require directory context to do i/o. We also
130  * use this when loading binaries directly.
131  */
132 static struct taskqueue *firmware_tq;
133 static struct task firmware_unload_task;
134 
135 /*
136  * This mutex protects accesses to the firmware table.
137  */
138 static struct mtx firmware_mtx;
139 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
140 
141 static MALLOC_DEFINE(M_FIRMWARE, "firmware", "device firmware images");
142 
143 static uint64_t firmware_max_size = 8u << 20; /* Default to 8MB cap */
144 SYSCTL_U64(_debug, OID_AUTO, firmware_max_size,
145     CTLFLAG_RWTUN, &firmware_max_size, 0,
146     "Max size permitted for a firmware file.");
147 
148 /*
149  * Helper function to lookup a name.
150  * As a side effect, it sets the pointer to a free slot, if any.
151  * This way we can concentrate most of the registry scanning in
152  * this function, which makes it easier to replace the registry
153  * with some other data structure.
154  */
155 static struct priv_fw *
lookup(const char * name)156 lookup(const char *name)
157 {
158 	struct priv_fw *fp;
159 
160 	mtx_assert(&firmware_mtx, MA_OWNED);
161 
162 	LIST_FOREACH(fp, &firmware_table, link) {
163 		if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
164 			break;
165 
166 		/*
167 		 * If the name looks like an absolute path, also try to match
168 		 * the last part of the string to the requested firmware if it
169 		 * matches the trailing components.  This allows us to load
170 		 * /boot/firmware/abc/bca2233_fw.bin and match it against
171 		 * requests for bca2233_fw.bin or abc/bca2233_fw.bin.
172 		 */
173 		if (*fp->fw.name == '/' && strlen(fp->fw.name) > strlen(name)) {
174 			const char *p = fp->fw.name + strlen(fp->fw.name) - strlen(name);
175 			if (p[-1] == '/' && strcasecmp(name, p) == 0)
176 				break;
177 		}
178 	}
179 	return (fp);
180 }
181 
182 /*
183  * Register a firmware image with the specified name.  The
184  * image name must not already be registered.  If this is a
185  * subimage then parent refers to a previously registered
186  * image that this should be associated with.
187  */
188 const struct firmware *
firmware_register(const char * imagename,const void * data,size_t datasize,unsigned int version,const struct firmware * parent)189 firmware_register(const char *imagename, const void *data, size_t datasize,
190     unsigned int version, const struct firmware *parent)
191 {
192 	struct priv_fw *frp;
193 	char *name;
194 
195 	mtx_lock(&firmware_mtx);
196 	frp = lookup(imagename);
197 	if (frp != NULL) {
198 		mtx_unlock(&firmware_mtx);
199 		printf("%s: image %s already registered!\n",
200 		    __func__, imagename);
201 		return (NULL);
202 	}
203 	mtx_unlock(&firmware_mtx);
204 
205 	frp = malloc(sizeof(*frp), M_FIRMWARE, M_WAITOK | M_ZERO);
206 	name = strdup(imagename, M_FIRMWARE);
207 
208 	mtx_lock(&firmware_mtx);
209 	if (lookup(imagename) != NULL) {
210 		/* We lost a race. */
211 		mtx_unlock(&firmware_mtx);
212 		free(name, M_FIRMWARE);
213 		free(frp, M_FIRMWARE);
214 		return (NULL);
215 	}
216 	frp->fw.name = name;
217 	frp->fw.data = data;
218 	frp->fw.datasize = datasize;
219 	frp->fw.version = version;
220 	if (parent != NULL)
221 		frp->parent = PRIV_FW(parent);
222 	LIST_INSERT_HEAD(&firmware_table, frp, link);
223 	mtx_unlock(&firmware_mtx);
224 	if (bootverbose)
225 		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
226 		    imagename, version, datasize, data);
227 	return (&frp->fw);
228 }
229 
230 /*
231  * Unregister/remove a firmware image.  If there are outstanding
232  * references an error is returned and the image is not removed
233  * from the registry.
234  */
235 int
firmware_unregister(const char * imagename)236 firmware_unregister(const char *imagename)
237 {
238 	struct priv_fw *fp;
239 	int err;
240 
241 	mtx_lock(&firmware_mtx);
242 	fp = lookup(imagename);
243 	if (fp == NULL) {
244 		/*
245 		 * It is ok for the lookup to fail; this can happen
246 		 * when a module is unloaded on last reference and the
247 		 * module unload handler unregister's each of its
248 		 * firmware images.
249 		 */
250 		err = 0;
251 	} else if (fp->refcnt != 0) {	/* cannot unregister */
252 		err = EBUSY;
253 	} else {
254 		LIST_REMOVE(fp, link);
255 		free(__DECONST(char *, fp->fw.name), M_FIRMWARE);
256 		free(fp, M_FIRMWARE);
257 		err = 0;
258 	}
259 	mtx_unlock(&firmware_mtx);
260 	return (err);
261 }
262 
263 struct fw_loadimage {
264 	const char	*imagename;
265 	uint32_t	flags;
266 };
267 
268 static const char *fw_path = "/boot/firmware/";
269 
270 static void
try_binary_file(const char * imagename,uint32_t flags)271 try_binary_file(const char *imagename, uint32_t flags)
272 {
273 	struct nameidata nd;
274 	struct thread *td = curthread;
275 	struct ucred *cred = td ? td->td_ucred : NULL;
276 	struct sbuf *sb;
277 	struct priv_fw *fp;
278 	const char *fn;
279 	struct vattr vattr;
280 	void *data = NULL;
281 	const struct firmware *fw;
282 	int oflags;
283 	size_t resid;
284 	int error;
285 	bool warn = flags & FIRMWARE_GET_NOWARN;
286 
287 	/*
288 	 * XXX TODO: Loop over some path instead of a single element path.
289 	 * and fetch this path from the 'firmware_path' kenv the loader sets.
290 	 */
291 	sb = sbuf_new_auto();
292 	sbuf_printf(sb, "%s%s", fw_path, imagename);
293 	sbuf_finish(sb);
294 	fn = sbuf_data(sb);
295 	if (bootverbose)
296 		printf("Trying to load binary firmware from %s\n", fn);
297 
298 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, fn);
299 	oflags = FREAD;
300 	error = vn_open(&nd, &oflags, 0, NULL);
301 	if (error)
302 		goto err;
303 	NDFREE_PNBUF(&nd);
304 	if (nd.ni_vp->v_type != VREG)
305 		goto err2;
306 	error = VOP_GETATTR(nd.ni_vp, &vattr, cred);
307 	if (error)
308 		goto err2;
309 
310 	/*
311 	 * Limit this to something sane, 8MB by default.
312 	 */
313 	if (vattr.va_size > firmware_max_size) {
314 		printf("Firmware %s is too big: %lld bytes, %ld bytes max.\n",
315 		    fn, (long long)vattr.va_size, (long)firmware_max_size);
316 		goto err2;
317 	}
318 	data = malloc(vattr.va_size, M_FIRMWARE, M_WAITOK);
319 	error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)data, vattr.va_size, 0,
320 	    UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &resid, td);
321 	/* XXX make data read only? */
322 	VOP_UNLOCK(nd.ni_vp);
323 	vn_close(nd.ni_vp, FREAD, cred, td);
324 	nd.ni_vp = NULL;
325 	if (error != 0 || resid != 0)
326 		goto err;
327 	fw = firmware_register(fn, data, vattr.va_size, 0, NULL);
328 	if (fw == NULL)
329 		goto err;
330 	fp = PRIV_FW(fw);
331 	fp->flags |= FW_BINARY;
332 	if (bootverbose)
333 		printf("%s: Loaded binary firmware using %s\n", imagename, fn);
334 	sbuf_delete(sb);
335 	return;
336 
337 err2: /* cleanup in vn_open through vn_close */
338 	VOP_UNLOCK(nd.ni_vp);
339 	vn_close(nd.ni_vp, FREAD, cred, td);
340 err:
341 	free(data, M_FIRMWARE);
342 	if (bootverbose || warn)
343 		printf("%s: could not load binary firmware %s either\n", imagename, fn);
344 	sbuf_delete(sb);
345 }
346 
347 static void
loadimage(void * arg,int npending __unused)348 loadimage(void *arg, int npending __unused)
349 {
350 	struct fw_loadimage *fwli = arg;
351 	struct priv_fw *fp;
352 	linker_file_t result;
353 	int error;
354 
355 	error = linker_reference_module(fwli->imagename, NULL, &result);
356 	if (error != 0) {
357 		if (bootverbose || (fwli->flags & FIRMWARE_GET_NOWARN) == 0)
358 			printf("%s: could not load firmware image, error %d\n",
359 			    fwli->imagename, error);
360 		try_binary_file(fwli->imagename, fwli->flags);
361 		mtx_lock(&firmware_mtx);
362 		goto done;
363 	}
364 
365 	mtx_lock(&firmware_mtx);
366 	fp = lookup(fwli->imagename);
367 	if (fp == NULL || fp->file != NULL) {
368 		mtx_unlock(&firmware_mtx);
369 		if (fp == NULL)
370 			printf("%s: firmware image loaded, "
371 			    "but did not register\n", fwli->imagename);
372 		(void) linker_release_module(fwli->imagename, NULL, NULL);
373 		mtx_lock(&firmware_mtx);
374 		goto done;
375 	}
376 	fp->file = result;	/* record the module identity */
377 done:
378 	wakeup_one(arg);
379 	mtx_unlock(&firmware_mtx);
380 }
381 
382 /*
383  * Lookup and potentially load the specified firmware image.
384  * If the firmware is not found in the registry, try to load a kernel
385  * module named as the image name.
386  * If the firmware is located, a reference is returned. The caller must
387  * release this reference for the image to be eligible for removal/unload.
388  */
389 const struct firmware *
firmware_get_flags(const char * imagename,uint32_t flags)390 firmware_get_flags(const char *imagename, uint32_t flags)
391 {
392 	struct task fwload_task;
393 	struct thread *td;
394 	struct priv_fw *fp;
395 
396 	mtx_lock(&firmware_mtx);
397 	fp = lookup(imagename);
398 	if (fp != NULL)
399 		goto found;
400 	/*
401 	 * Image not present, try to load the module holding it.
402 	 */
403 	td = curthread;
404 	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
405 	    securelevel_gt(td->td_ucred, 0) != 0) {
406 		mtx_unlock(&firmware_mtx);
407 		printf("%s: insufficient privileges to "
408 		    "load firmware image %s\n", __func__, imagename);
409 		return NULL;
410 	}
411 	/*
412 	 * Defer load to a thread with known context.  linker_reference_module
413 	 * may do filesystem i/o which requires root & current dirs, etc.
414 	 * Also we must not hold any mtx's over this call which is problematic.
415 	 */
416 	if (!cold) {
417 		struct fw_loadimage fwli;
418 
419 		fwli.imagename = imagename;
420 		fwli.flags = flags;
421 		TASK_INIT(&fwload_task, 0, loadimage, (void *)&fwli);
422 		taskqueue_enqueue(firmware_tq, &fwload_task);
423 		PHOLD(curproc);
424 		msleep((void *)&fwli, &firmware_mtx, 0, "fwload", 0);
425 		PRELE(curproc);
426 	}
427 	/*
428 	 * After attempting to load the module, see if the image is registered.
429 	 */
430 	fp = lookup(imagename);
431 	if (fp == NULL) {
432 		mtx_unlock(&firmware_mtx);
433 		return NULL;
434 	}
435 found:				/* common exit point on success */
436 	if (fp->refcnt == 0 && fp->parent != NULL)
437 		fp->parent->refcnt++;
438 	fp->refcnt++;
439 	mtx_unlock(&firmware_mtx);
440 	return &fp->fw;
441 }
442 
443 const struct firmware *
firmware_get(const char * imagename)444 firmware_get(const char *imagename)
445 {
446 
447 	return (firmware_get_flags(imagename, 0));
448 }
449 
450 /*
451  * Release a reference to a firmware image returned by firmware_get.
452  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
453  * to release the resource, but the flag is only advisory.
454  *
455  * If this is the last reference to the firmware image, and this is an
456  * autoloaded module, wake up the firmware_unload_task to figure out
457  * what to do with the associated module.
458  */
459 void
firmware_put(const struct firmware * p,int flags)460 firmware_put(const struct firmware *p, int flags)
461 {
462 	struct priv_fw *fp = PRIV_FW(p);
463 
464 	mtx_lock(&firmware_mtx);
465 	fp->refcnt--;
466 	if (fp->refcnt == 0) {
467 		if (fp->parent != NULL)
468 			fp->parent->refcnt--;
469 		if (flags & FIRMWARE_UNLOAD)
470 			fp->flags |= FW_UNLOAD;
471 		if (fp->file)
472 			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
473 	}
474 	mtx_unlock(&firmware_mtx);
475 }
476 
477 /*
478  * Setup directory state for the firmware_tq thread so we can do i/o.
479  */
480 static void
set_rootvnode(void * arg,int npending)481 set_rootvnode(void *arg, int npending)
482 {
483 
484 	pwd_ensure_dirs();
485 	free(arg, M_TEMP);
486 }
487 
488 /*
489  * Event handler called on mounting of /; bounce a task
490  * into the task queue thread to setup it's directories.
491  */
492 static void
firmware_mountroot(void * arg)493 firmware_mountroot(void *arg)
494 {
495 	struct task *setroot_task;
496 
497 	setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
498 	if (setroot_task != NULL) {
499 		TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
500 		taskqueue_enqueue(firmware_tq, setroot_task);
501 	} else
502 		printf("%s: no memory for task!\n", __func__);
503 }
504 EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
505 
506 /*
507  * The body of the task in charge of unloading autoloaded modules
508  * that are not needed anymore.
509  * Images can be cross-linked so we may need to make multiple passes,
510  * but the time we spend in the loop is bounded because we clear entries
511  * as we touch them.
512  */
513 static void
unloadentry(void * unused1,int unused2)514 unloadentry(void *unused1, int unused2)
515 {
516 	struct priv_fw *fp, *tmp;
517 
518 	mtx_lock(&firmware_mtx);
519 restart:
520 	LIST_FOREACH_SAFE(fp, &firmware_table, link, tmp) {
521 		if (((fp->flags & FW_BINARY) == 0 && fp->file == NULL) ||
522 		    fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
523 			continue;
524 
525 		/*
526 		 * If we directly loaded the firmware, then we just need to
527 		 * remove the entry from the list and free the entry and go to
528 		 * the next one.  There's no need for the indirection of the kld
529 		 * module case, we free memory and go to the next one.
530 		 */
531 		if ((fp->flags & FW_BINARY) != 0) {
532 			LIST_REMOVE(fp, link);
533 			free(__DECONST(char *, fp->fw.data), M_FIRMWARE);
534 			free(__DECONST(char *, fp->fw.name), M_FIRMWARE);
535 			free(fp, M_FIRMWARE);
536 			continue;
537 		}
538 
539 		/*
540 		 * Found an entry.  This is the kld case, so we have a more
541 		 * complex dance.  Now:
542 		 * 1. make sure we scan the table again
543 		 * 2. clear FW_UNLOAD so we don't try this entry again.
544 		 * 3. release the lock while trying to unload the module.
545 		 */
546 		fp->flags &= ~FW_UNLOAD;	/* do not try again */
547 
548 		/*
549 		 * We rely on the module to call firmware_unregister()
550 		 * on unload to actually free the entry.
551 		 */
552 		mtx_unlock(&firmware_mtx);
553 		(void)linker_release_module(NULL, NULL, fp->file);
554 		mtx_lock(&firmware_mtx);
555 
556 		/*
557 		 * When we dropped the lock, another thread could have
558 		 * removed an element, so we must restart the scan.
559 		 */
560 		goto restart;
561 	}
562 	mtx_unlock(&firmware_mtx);
563 }
564 
565 /*
566  * Find all the binary firmware that was loaded in the boot loader via load -t
567  * firmware foo.  There is only one firmware per file, it's the whole file, and
568  * there's no meaningful version passed in, so pass 0 for that.  If version is
569  * needed by the consumer (and not just arbitrarily defined), the .ko version
570  * must be used instead.
571  */
572 static void
firmware_binary_files(void)573 firmware_binary_files(void)
574 {
575 	caddr_t file;
576 	char *name;
577 	const char *type;
578 	const void *addr;
579 	size_t size;
580 	unsigned int version = 0;
581 	const struct firmware *fw;
582 	struct priv_fw *fp;
583 
584 	file = 0;
585 	for (;;) {
586 		file = preload_search_next_name(file);
587 		if (file == 0)
588 			break;
589 		type = (const char *)preload_search_info(file, MODINFO_TYPE);
590 		if (type == NULL || strcmp(type, "firmware") != 0)
591 			continue;
592 		name = preload_search_info(file, MODINFO_NAME);
593 		addr = preload_fetch_addr(file);
594 		size = preload_fetch_size(file);
595 		fw = firmware_register(name, addr, size, version, NULL);
596 		fp = PRIV_FW(fw);
597 		fp->refcnt++;	/* Hold an extra reference so we never unload */
598 	}
599 }
600 
601 /*
602  * Module glue.
603  */
604 static int
firmware_modevent(module_t mod,int type,void * unused)605 firmware_modevent(module_t mod, int type, void *unused)
606 {
607 	struct priv_fw *fp;
608 	int err;
609 
610 	err = 0;
611 	switch (type) {
612 	case MOD_LOAD:
613 		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
614 		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
615 		    taskqueue_thread_enqueue, &firmware_tq);
616 		/* NB: use our own loop routine that sets up context */
617 		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
618 		    "firmware taskq");
619 		firmware_binary_files();
620 		if (rootvnode != NULL) {
621 			/*
622 			 * Root is already mounted so we won't get an event;
623 			 * simulate one here.
624 			 */
625 			firmware_mountroot(NULL);
626 		}
627 		break;
628 
629 	case MOD_UNLOAD:
630 		/* request all autoloaded modules to be released */
631 		mtx_lock(&firmware_mtx);
632 		LIST_FOREACH(fp, &firmware_table, link)
633 			fp->flags |= FW_UNLOAD;
634 		mtx_unlock(&firmware_mtx);
635 		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
636 		taskqueue_drain(firmware_tq, &firmware_unload_task);
637 
638 		LIST_FOREACH(fp, &firmware_table, link) {
639 			if (fp->fw.name != NULL) {
640 				printf("%s: image %s still active, %d refs\n",
641 				    __func__, fp->fw.name, fp->refcnt);
642 				err = EINVAL;
643 			}
644 		}
645 		if (err == 0)
646 			taskqueue_free(firmware_tq);
647 		break;
648 
649 	default:
650 		err = EOPNOTSUPP;
651 		break;
652 	}
653 	return (err);
654 }
655 
656 static moduledata_t firmware_mod = {
657 	"firmware",
658 	firmware_modevent,
659 	NULL
660 };
661 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
662 MODULE_VERSION(firmware, 1);
663