subr_firmware.c revision 178042
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: head/sys/kern/subr_firmware.c 178042 2008-04-09 19:07:48Z sam $");
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 */
124154974Smlaier#define	FIRMWARE_MAX	30
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;
178154974Smlaier
179154974Smlaier	mtx_lock(&firmware_mtx);
180166756Sluigi	/*
181166756Sluigi	 * Do a lookup to make sure the name is unique or find a free slot.
182166756Sluigi	 */
183166756Sluigi	match = lookup(imagename, &frp);
184166756Sluigi	if (match != NULL) {
185166756Sluigi		mtx_unlock(&firmware_mtx);
186166756Sluigi		printf("%s: image %s already registered!\n",
187166756Sluigi			__func__, imagename);
188166756Sluigi		return NULL;
189154974Smlaier	}
190154974Smlaier	if (frp == NULL) {
191154974Smlaier		mtx_unlock(&firmware_mtx);
192154974Smlaier		printf("%s: cannot register image %s, firmware table full!\n",
193154974Smlaier		    __func__, imagename);
194154974Smlaier		return NULL;
195154974Smlaier	}
196166756Sluigi	bzero(frp, sizeof(frp));	/* start from a clean record */
197166756Sluigi	frp->fw.name = imagename;
198166756Sluigi	frp->fw.data = data;
199166756Sluigi	frp->fw.datasize = datasize;
200166756Sluigi	frp->fw.version = version;
201166756Sluigi	if (parent != NULL) {
202166756Sluigi		frp->parent = PRIV_FW(parent);
203166756Sluigi		frp->parent->refcnt++;
204166756Sluigi	}
205154974Smlaier	mtx_unlock(&firmware_mtx);
206166465Smlaier	if (bootverbose)
207166465Smlaier		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
208166465Smlaier		    imagename, version, datasize, data);
209166756Sluigi	return &frp->fw;
210154974Smlaier}
211154974Smlaier
212154974Smlaier/*
213154974Smlaier * Unregister/remove a firmware image.  If there are outstanding
214154974Smlaier * references an error is returned and the image is not removed
215154974Smlaier * from the registry.
216154974Smlaier */
217154974Smlaierint
218154974Smlaierfirmware_unregister(const char *imagename)
219154974Smlaier{
220166756Sluigi	struct priv_fw *fp;
221166756Sluigi	int err;
222154974Smlaier
223154974Smlaier	mtx_lock(&firmware_mtx);
224166756Sluigi	fp = lookup(imagename, NULL);
225166756Sluigi	if (fp == NULL) {
226166756Sluigi		/*
227166756Sluigi		 * It is ok for the lookup to fail; this can happen
228166756Sluigi		 * when a module is unloaded on last reference and the
229166756Sluigi		 * module unload handler unregister's each of it's
230166756Sluigi		 * firmware images.
231166756Sluigi		 */
232166756Sluigi		err = 0;
233166756Sluigi	} else if (fp->refcnt != 0) {	/* cannot unregister */
234166756Sluigi		err = EBUSY;
235166756Sluigi	}  else {
236178042Ssam		linker_file_t x = fp->file;	/* save value */
237166756Sluigi
238166756Sluigi		if (fp->parent != NULL)	/* release parent reference */
239166756Sluigi			fp->parent->refcnt--;
240166756Sluigi		/*
241166756Sluigi		 * Clear the whole entry with bzero to make sure we
242166756Sluigi		 * do not forget anything. Then restore 'file' which is
243166756Sluigi		 * non-null for autoloaded images.
244166756Sluigi		 */
245166756Sluigi		bzero(fp, sizeof(struct priv_fw));
246166756Sluigi		fp->file = x;
247166756Sluigi		err = 0;
248154974Smlaier	}
249154974Smlaier	mtx_unlock(&firmware_mtx);
250166756Sluigi	return err;
251154974Smlaier}
252154974Smlaier
253178042Ssamstatic void
254178042Ssamloadimage(void *arg, int npending)
255178042Ssam{
256178042Ssam	struct thread *td = curthread;
257178042Ssam	char *imagename = arg;
258178042Ssam	struct priv_fw *fp;
259178042Ssam	linker_file_t result;
260178042Ssam	int error;
261178042Ssam
262178042Ssam	/* synchronize with the thread that dispatched us */
263178042Ssam	mtx_lock(&firmware_mtx);
264178042Ssam	mtx_unlock(&firmware_mtx);
265178042Ssam
266178042Ssam	if (td->td_proc->p_fd->fd_rdir == NULL) {
267178042Ssam		printf("%s: root not mounted yet, no way to load image\n",
268178042Ssam		    imagename);
269178042Ssam		goto done;
270178042Ssam	}
271178042Ssam	error = linker_reference_module(imagename, NULL, &result);
272178042Ssam	if (error != 0) {
273178042Ssam		printf("%s: could not load firmware image, error %d\n",
274178042Ssam		    imagename, error);
275178042Ssam		goto done;
276178042Ssam	}
277178042Ssam
278178042Ssam	mtx_lock(&firmware_mtx);
279178042Ssam	fp = lookup(imagename, NULL);
280178042Ssam	if (fp == NULL || fp->file != NULL) {
281178042Ssam		mtx_unlock(&firmware_mtx);
282178042Ssam		if (fp == NULL)
283178042Ssam			printf("%s: firmware image loaded, "
284178042Ssam			    "but did not register\n", imagename);
285178042Ssam		(void) linker_release_module(imagename, NULL, NULL);
286178042Ssam		goto done;
287178042Ssam	}
288178042Ssam	fp->file = result;	/* record the module identity */
289178042Ssam	mtx_unlock(&firmware_mtx);
290178042Ssamdone:
291178042Ssam	wakeup_one(imagename);		/* we're done */
292178042Ssam}
293178042Ssam
294154974Smlaier/*
295154974Smlaier * Lookup and potentially load the specified firmware image.
296166756Sluigi * If the firmware is not found in the registry, try to load a kernel
297166756Sluigi * module named as the image name.
298166756Sluigi * If the firmware is located, a reference is returned. The caller must
299166756Sluigi * release this reference for the image to be eligible for removal/unload.
300154974Smlaier */
301166756Sluigiconst struct firmware *
302154974Smlaierfirmware_get(const char *imagename)
303154974Smlaier{
304178042Ssam	struct task fwload_task;
305154974Smlaier	struct thread *td;
306166756Sluigi	struct priv_fw *fp;
307154974Smlaier
308154974Smlaier	mtx_lock(&firmware_mtx);
309166756Sluigi	fp = lookup(imagename, NULL);
310166756Sluigi	if (fp != NULL)
311166756Sluigi		goto found;
312154974Smlaier	/*
313166756Sluigi	 * Image not present, try to load the module holding it.
314154974Smlaier	 */
315154974Smlaier	td = curthread;
316164033Srwatson	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
317164033Srwatson	    securelevel_gt(td->td_ucred, 0) != 0) {
318178042Ssam		mtx_unlock(&firmware_mtx);
319154974Smlaier		printf("%s: insufficient privileges to "
320154974Smlaier		    "load firmware image %s\n", __func__, imagename);
321154974Smlaier		return NULL;
322154974Smlaier	}
323178042Ssam	/*
324178042Ssam	 * Defer load to a thread with known context.  linker_reference_module
325178042Ssam	 * may do filesystem i/o which requires root & current dirs, etc.
326178042Ssam	 * Also we must not hold any mtx's over this call which is problematic.
327178042Ssam	 */
328178042Ssam	TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *, imagename));
329178042Ssam	taskqueue_enqueue(firmware_tq, &fwload_task);
330178042Ssam	msleep(__DECONST(void *, imagename), &firmware_mtx, 0, "fwload", 0);
331166756Sluigi	/*
332178042Ssam	 * After attempting to load the module, see if the image is registered.
333166756Sluigi	 */
334166756Sluigi	fp = lookup(imagename, NULL);
335166756Sluigi	if (fp == NULL) {
336154974Smlaier		mtx_unlock(&firmware_mtx);
337166756Sluigi		return NULL;
338166756Sluigi	}
339166756Sluigifound:				/* common exit point on success */
340166756Sluigi	fp->refcnt++;
341154974Smlaier	mtx_unlock(&firmware_mtx);
342166756Sluigi	return &fp->fw;
343154974Smlaier}
344154974Smlaier
345154974Smlaier/*
346166756Sluigi * Release a reference to a firmware image returned by firmware_get.
347166756Sluigi * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
348166756Sluigi * to release the resource, but the flag is only advisory.
349166756Sluigi *
350166756Sluigi * If this is the last reference to the firmware image, and this is an
351178042Ssam * autoloaded module, wake up the firmware_unload_task to figure out
352178042Ssam * what to do with the associated module.
353154974Smlaier */
354154974Smlaiervoid
355166756Sluigifirmware_put(const struct firmware *p, int flags)
356154974Smlaier{
357166756Sluigi	struct priv_fw *fp = PRIV_FW(p);
358166756Sluigi
359154974Smlaier	mtx_lock(&firmware_mtx);
360154974Smlaier	fp->refcnt--;
361159486Siedowse	if (fp->refcnt == 0) {
362166756Sluigi		if (flags & FIRMWARE_UNLOAD)
363166756Sluigi			fp->flags |= FW_UNLOAD;
364166756Sluigi		if (fp->file)
365178042Ssam			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
366159486Siedowse	}
367154974Smlaier	mtx_unlock(&firmware_mtx);
368154974Smlaier}
369154974Smlaier
370154974Smlaier/*
371178042Ssam * Setup directory state for the firmware_tq thread so we can do i/o.
372178042Ssam */
373178042Ssamstatic void
374178042Ssamset_rootvnode(void *arg, int npending)
375178042Ssam{
376178042Ssam	struct thread *td = curthread;
377178042Ssam	struct proc *p = td->td_proc;
378178042Ssam
379178042Ssam	FILEDESC_XLOCK(p->p_fd);
380178042Ssam	if (p->p_fd->fd_cdir == NULL) {
381178042Ssam		p->p_fd->fd_cdir = rootvnode;
382178042Ssam		VREF(rootvnode);
383178042Ssam	}
384178042Ssam	if (p->p_fd->fd_rdir == NULL) {
385178042Ssam		p->p_fd->fd_rdir = rootvnode;
386178042Ssam		VREF(rootvnode);
387178042Ssam	}
388178042Ssam	FILEDESC_XUNLOCK(p->p_fd);
389178042Ssam}
390178042Ssam
391178042Ssam/*
392178042Ssam * Event handler called on mounting of /; bounce a task
393178042Ssam * into the task queue thread to setup it's directories.
394178042Ssam */
395178042Ssamstatic void
396178042Ssamfirmware_mountroot(void *arg)
397178042Ssam{
398178042Ssam	static struct task setroot_task;
399178042Ssam
400178042Ssam	TASK_INIT(&setroot_task, 0, set_rootvnode, NULL);
401178042Ssam	taskqueue_enqueue(firmware_tq, &setroot_task);
402178042Ssam}
403178042SsamEVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
404178042Ssam
405178042Ssam/*
406166756Sluigi * The body of the task in charge of unloading autoloaded modules
407166756Sluigi * that are not needed anymore.
408166756Sluigi * Images can be cross-linked so we may need to make multiple passes,
409166756Sluigi * but the time we spend in the loop is bounded because we clear entries
410166756Sluigi * as we touch them.
411166756Sluigi */
412166756Sluigistatic void
413166756Sluigiunloadentry(void *unused1, int unused2)
414166756Sluigi{
415166756Sluigi	int limit = FIRMWARE_MAX;
416166756Sluigi	int i;	/* current cycle */
417166756Sluigi
418166756Sluigi	mtx_lock(&firmware_mtx);
419166756Sluigi	/*
420166756Sluigi	 * Scan the table. limit is set to make sure we make another
421166756Sluigi	 * full sweep after matching an entry that requires unloading.
422166756Sluigi	 */
423166756Sluigi	for (i = 0; i < limit; i++) {
424166756Sluigi		struct priv_fw *fp;
425166756Sluigi		int err;
426166756Sluigi
427166756Sluigi		fp = &firmware_table[i % FIRMWARE_MAX];
428166756Sluigi		if (fp->fw.name == NULL || fp->file == NULL ||
429166756Sluigi		    fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
430166756Sluigi			continue;
431166756Sluigi
432166756Sluigi		/*
433166756Sluigi		 * Found an entry. Now:
434166756Sluigi		 * 1. bump up limit to make sure we make another full round;
435166756Sluigi		 * 2. clear FW_UNLOAD so we don't try this entry again.
436166756Sluigi		 * 3. release the lock while trying to unload the module.
437166756Sluigi		 * 'file' remains set so that the entry cannot be reused
438166756Sluigi		 * in the meantime (it also means that fp->file will
439166756Sluigi		 * not change while we release the lock).
440166756Sluigi		 */
441166756Sluigi		limit = i + FIRMWARE_MAX;	/* make another full round */
442166756Sluigi		fp->flags &= ~FW_UNLOAD;	/* do not try again */
443166756Sluigi
444166756Sluigi		mtx_unlock(&firmware_mtx);
445166756Sluigi		err = linker_release_module(NULL, NULL, fp->file);
446166756Sluigi		mtx_lock(&firmware_mtx);
447166756Sluigi
448166756Sluigi		/*
449166756Sluigi		 * We rely on the module to call firmware_unregister()
450166756Sluigi		 * on unload to actually release the entry.
451166756Sluigi		 * If err = 0 we can drop our reference as the system
452166756Sluigi		 * accepted it. Otherwise unloading failed (e.g. the
453166756Sluigi		 * module itself gave an error) so our reference is
454166756Sluigi		 * still valid.
455166756Sluigi		 */
456166756Sluigi		if (err == 0)
457166756Sluigi			fp->file = NULL;
458166756Sluigi	}
459166756Sluigi	mtx_unlock(&firmware_mtx);
460166756Sluigi}
461166756Sluigi
462166756Sluigi/*
463154974Smlaier * Module glue.
464154974Smlaier */
465154974Smlaierstatic int
466154974Smlaierfirmware_modevent(module_t mod, int type, void *unused)
467154974Smlaier{
468166756Sluigi	struct priv_fw *fp;
469178042Ssam	int i, err;
470159486Siedowse
471154974Smlaier	switch (type) {
472154974Smlaier	case MOD_LOAD:
473178042Ssam		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
474178042Ssam		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
475178042Ssam		    taskqueue_thread_enqueue, &firmware_tq);
476178042Ssam		/* NB: use our own loop routine that sets up context */
477178042Ssam		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
478178042Ssam		    "firmware taskq");
479178042Ssam		if (rootvnode != NULL) {
480178042Ssam			/*
481178042Ssam			 * Root is already mounted so we won't get an event;
482178042Ssam			 * simulate one here.
483178042Ssam			 */
484178042Ssam			firmware_mountroot(NULL);
485178042Ssam		}
486154974Smlaier		return 0;
487166756Sluigi
488154974Smlaier	case MOD_UNLOAD:
489166756Sluigi		/* request all autoloaded modules to be released */
490166756Sluigi		mtx_lock(&firmware_mtx);
491159486Siedowse		for (i = 0; i < FIRMWARE_MAX; i++) {
492159589Sjhb			fp = &firmware_table[i];
493166756Sluigi			fp->flags |= FW_UNLOAD;;
494159486Siedowse		}
495166756Sluigi		mtx_unlock(&firmware_mtx);
496178042Ssam		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
497178042Ssam		taskqueue_drain(firmware_tq, &firmware_unload_task);
498178042Ssam		err = 0;
499166756Sluigi		for (i = 0; i < FIRMWARE_MAX; i++) {
500166756Sluigi			fp = &firmware_table[i];
501166756Sluigi			if (fp->fw.name != NULL) {
502166756Sluigi				printf("%s: image %p ref %d still active slot %d\n",
503166756Sluigi					__func__, fp->fw.name,
504166756Sluigi					fp->refcnt,  i);
505166756Sluigi				err = EINVAL;
506166756Sluigi			}
507166756Sluigi		}
508178042Ssam		if (err == 0)
509178042Ssam			taskqueue_free(firmware_tq);
510166756Sluigi		return err;
511154974Smlaier	}
512154974Smlaier	return EINVAL;
513154974Smlaier}
514154974Smlaier
515154974Smlaierstatic moduledata_t firmware_mod = {
516154974Smlaier	"firmware",
517154974Smlaier	firmware_modevent,
518154974Smlaier	0
519154974Smlaier};
520154974SmlaierDECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
521154974SmlaierMODULE_VERSION(firmware, 1);
522