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;
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	}
196238013Smarius	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;
201240472Snp	if (parent != NULL)
202166756Sluigi		frp->parent = PRIV_FW(parent);
203154974Smlaier	mtx_unlock(&firmware_mtx);
204166465Smlaier	if (bootverbose)
205166465Smlaier		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
206166465Smlaier		    imagename, version, datasize, data);
207166756Sluigi	return &frp->fw;
208154974Smlaier}
209154974Smlaier
210154974Smlaier/*
211154974Smlaier * Unregister/remove a firmware image.  If there are outstanding
212154974Smlaier * references an error is returned and the image is not removed
213154974Smlaier * from the registry.
214154974Smlaier */
215154974Smlaierint
216154974Smlaierfirmware_unregister(const char *imagename)
217154974Smlaier{
218166756Sluigi	struct priv_fw *fp;
219166756Sluigi	int err;
220154974Smlaier
221154974Smlaier	mtx_lock(&firmware_mtx);
222166756Sluigi	fp = lookup(imagename, NULL);
223166756Sluigi	if (fp == NULL) {
224166756Sluigi		/*
225166756Sluigi		 * It is ok for the lookup to fail; this can happen
226166756Sluigi		 * when a module is unloaded on last reference and the
227166756Sluigi		 * module unload handler unregister's each of it's
228166756Sluigi		 * firmware images.
229166756Sluigi		 */
230166756Sluigi		err = 0;
231166756Sluigi	} else if (fp->refcnt != 0) {	/* cannot unregister */
232166756Sluigi		err = EBUSY;
233166756Sluigi	}  else {
234178042Ssam		linker_file_t x = fp->file;	/* save value */
235166756Sluigi
236166756Sluigi		/*
237166756Sluigi		 * Clear the whole entry with bzero to make sure we
238166756Sluigi		 * do not forget anything. Then restore 'file' which is
239166756Sluigi		 * non-null for autoloaded images.
240166756Sluigi		 */
241166756Sluigi		bzero(fp, sizeof(struct priv_fw));
242166756Sluigi		fp->file = x;
243166756Sluigi		err = 0;
244154974Smlaier	}
245154974Smlaier	mtx_unlock(&firmware_mtx);
246166756Sluigi	return err;
247154974Smlaier}
248154974Smlaier
249178042Ssamstatic void
250178042Ssamloadimage(void *arg, int npending)
251178042Ssam{
252178042Ssam	struct thread *td = curthread;
253178042Ssam	char *imagename = arg;
254178042Ssam	struct priv_fw *fp;
255178042Ssam	linker_file_t result;
256178042Ssam	int error;
257178042Ssam
258178042Ssam	/* synchronize with the thread that dispatched us */
259178042Ssam	mtx_lock(&firmware_mtx);
260178042Ssam	mtx_unlock(&firmware_mtx);
261178042Ssam
262178042Ssam	if (td->td_proc->p_fd->fd_rdir == NULL) {
263178042Ssam		printf("%s: root not mounted yet, no way to load image\n",
264178042Ssam		    imagename);
265178042Ssam		goto done;
266178042Ssam	}
267178042Ssam	error = linker_reference_module(imagename, NULL, &result);
268178042Ssam	if (error != 0) {
269178042Ssam		printf("%s: could not load firmware image, error %d\n",
270178042Ssam		    imagename, error);
271178042Ssam		goto done;
272178042Ssam	}
273178042Ssam
274178042Ssam	mtx_lock(&firmware_mtx);
275178042Ssam	fp = lookup(imagename, NULL);
276178042Ssam	if (fp == NULL || fp->file != NULL) {
277178042Ssam		mtx_unlock(&firmware_mtx);
278178042Ssam		if (fp == NULL)
279178042Ssam			printf("%s: firmware image loaded, "
280178042Ssam			    "but did not register\n", imagename);
281178042Ssam		(void) linker_release_module(imagename, NULL, NULL);
282178042Ssam		goto done;
283178042Ssam	}
284178042Ssam	fp->file = result;	/* record the module identity */
285178042Ssam	mtx_unlock(&firmware_mtx);
286178042Ssamdone:
287178042Ssam	wakeup_one(imagename);		/* we're done */
288178042Ssam}
289178042Ssam
290154974Smlaier/*
291154974Smlaier * Lookup and potentially load the specified firmware image.
292166756Sluigi * If the firmware is not found in the registry, try to load a kernel
293166756Sluigi * module named as the image name.
294166756Sluigi * If the firmware is located, a reference is returned. The caller must
295166756Sluigi * release this reference for the image to be eligible for removal/unload.
296154974Smlaier */
297166756Sluigiconst struct firmware *
298154974Smlaierfirmware_get(const char *imagename)
299154974Smlaier{
300178042Ssam	struct task fwload_task;
301154974Smlaier	struct thread *td;
302166756Sluigi	struct priv_fw *fp;
303154974Smlaier
304154974Smlaier	mtx_lock(&firmware_mtx);
305166756Sluigi	fp = lookup(imagename, NULL);
306166756Sluigi	if (fp != NULL)
307166756Sluigi		goto found;
308154974Smlaier	/*
309166756Sluigi	 * Image not present, try to load the module holding it.
310154974Smlaier	 */
311154974Smlaier	td = curthread;
312164033Srwatson	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
313164033Srwatson	    securelevel_gt(td->td_ucred, 0) != 0) {
314178042Ssam		mtx_unlock(&firmware_mtx);
315154974Smlaier		printf("%s: insufficient privileges to "
316154974Smlaier		    "load firmware image %s\n", __func__, imagename);
317154974Smlaier		return NULL;
318154974Smlaier	}
319178042Ssam	/*
320178042Ssam	 * Defer load to a thread with known context.  linker_reference_module
321178042Ssam	 * may do filesystem i/o which requires root & current dirs, etc.
322178042Ssam	 * Also we must not hold any mtx's over this call which is problematic.
323178042Ssam	 */
324184842Sgallatin	if (!cold) {
325184842Sgallatin		TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *,
326184842Sgallatin		    imagename));
327184842Sgallatin		taskqueue_enqueue(firmware_tq, &fwload_task);
328184842Sgallatin		msleep(__DECONST(void *, imagename), &firmware_mtx, 0,
329184842Sgallatin		    "fwload", 0);
330184842Sgallatin	}
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 */
340240472Snp	if (fp->refcnt == 0 && fp->parent != NULL)
341240472Snp		fp->parent->refcnt++;
342166756Sluigi	fp->refcnt++;
343154974Smlaier	mtx_unlock(&firmware_mtx);
344166756Sluigi	return &fp->fw;
345154974Smlaier}
346154974Smlaier
347154974Smlaier/*
348166756Sluigi * Release a reference to a firmware image returned by firmware_get.
349166756Sluigi * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
350166756Sluigi * to release the resource, but the flag is only advisory.
351166756Sluigi *
352166756Sluigi * If this is the last reference to the firmware image, and this is an
353178042Ssam * autoloaded module, wake up the firmware_unload_task to figure out
354178042Ssam * what to do with the associated module.
355154974Smlaier */
356154974Smlaiervoid
357166756Sluigifirmware_put(const struct firmware *p, int flags)
358154974Smlaier{
359166756Sluigi	struct priv_fw *fp = PRIV_FW(p);
360166756Sluigi
361154974Smlaier	mtx_lock(&firmware_mtx);
362154974Smlaier	fp->refcnt--;
363159486Siedowse	if (fp->refcnt == 0) {
364240472Snp		if (fp->parent != NULL)
365240472Snp			fp->parent->refcnt--;
366166756Sluigi		if (flags & FIRMWARE_UNLOAD)
367166756Sluigi			fp->flags |= FW_UNLOAD;
368166756Sluigi		if (fp->file)
369178042Ssam			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
370159486Siedowse	}
371154974Smlaier	mtx_unlock(&firmware_mtx);
372154974Smlaier}
373154974Smlaier
374154974Smlaier/*
375178042Ssam * Setup directory state for the firmware_tq thread so we can do i/o.
376178042Ssam */
377178042Ssamstatic void
378178042Ssamset_rootvnode(void *arg, int npending)
379178042Ssam{
380178042Ssam	struct thread *td = curthread;
381178042Ssam	struct proc *p = td->td_proc;
382178042Ssam
383178042Ssam	FILEDESC_XLOCK(p->p_fd);
384178042Ssam	if (p->p_fd->fd_cdir == NULL) {
385178042Ssam		p->p_fd->fd_cdir = rootvnode;
386178042Ssam		VREF(rootvnode);
387178042Ssam	}
388178042Ssam	if (p->p_fd->fd_rdir == NULL) {
389178042Ssam		p->p_fd->fd_rdir = rootvnode;
390178042Ssam		VREF(rootvnode);
391178042Ssam	}
392178042Ssam	FILEDESC_XUNLOCK(p->p_fd);
393183614Ssam
394183614Ssam	free(arg, M_TEMP);
395178042Ssam}
396178042Ssam
397178042Ssam/*
398178042Ssam * Event handler called on mounting of /; bounce a task
399178042Ssam * into the task queue thread to setup it's directories.
400178042Ssam */
401178042Ssamstatic void
402178042Ssamfirmware_mountroot(void *arg)
403178042Ssam{
404183614Ssam	struct task *setroot_task;
405178042Ssam
406183614Ssam	setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
407183614Ssam	if (setroot_task != NULL) {
408183614Ssam		TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
409183614Ssam		taskqueue_enqueue(firmware_tq, setroot_task);
410183614Ssam	} else
411183614Ssam		printf("%s: no memory for task!\n", __func__);
412178042Ssam}
413178042SsamEVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
414178042Ssam
415178042Ssam/*
416166756Sluigi * The body of the task in charge of unloading autoloaded modules
417166756Sluigi * that are not needed anymore.
418166756Sluigi * Images can be cross-linked so we may need to make multiple passes,
419166756Sluigi * but the time we spend in the loop is bounded because we clear entries
420166756Sluigi * as we touch them.
421166756Sluigi */
422166756Sluigistatic void
423166756Sluigiunloadentry(void *unused1, int unused2)
424166756Sluigi{
425166756Sluigi	int limit = FIRMWARE_MAX;
426166756Sluigi	int i;	/* current cycle */
427166756Sluigi
428166756Sluigi	mtx_lock(&firmware_mtx);
429166756Sluigi	/*
430166756Sluigi	 * Scan the table. limit is set to make sure we make another
431166756Sluigi	 * full sweep after matching an entry that requires unloading.
432166756Sluigi	 */
433166756Sluigi	for (i = 0; i < limit; i++) {
434166756Sluigi		struct priv_fw *fp;
435166756Sluigi		int err;
436166756Sluigi
437166756Sluigi		fp = &firmware_table[i % FIRMWARE_MAX];
438166756Sluigi		if (fp->fw.name == NULL || fp->file == NULL ||
439166756Sluigi		    fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
440166756Sluigi			continue;
441166756Sluigi
442166756Sluigi		/*
443166756Sluigi		 * Found an entry. Now:
444166756Sluigi		 * 1. bump up limit to make sure we make another full round;
445166756Sluigi		 * 2. clear FW_UNLOAD so we don't try this entry again.
446166756Sluigi		 * 3. release the lock while trying to unload the module.
447166756Sluigi		 * 'file' remains set so that the entry cannot be reused
448166756Sluigi		 * in the meantime (it also means that fp->file will
449166756Sluigi		 * not change while we release the lock).
450166756Sluigi		 */
451166756Sluigi		limit = i + FIRMWARE_MAX;	/* make another full round */
452166756Sluigi		fp->flags &= ~FW_UNLOAD;	/* do not try again */
453166756Sluigi
454166756Sluigi		mtx_unlock(&firmware_mtx);
455166756Sluigi		err = linker_release_module(NULL, NULL, fp->file);
456166756Sluigi		mtx_lock(&firmware_mtx);
457166756Sluigi
458166756Sluigi		/*
459166756Sluigi		 * We rely on the module to call firmware_unregister()
460166756Sluigi		 * on unload to actually release the entry.
461166756Sluigi		 * If err = 0 we can drop our reference as the system
462166756Sluigi		 * accepted it. Otherwise unloading failed (e.g. the
463166756Sluigi		 * module itself gave an error) so our reference is
464166756Sluigi		 * still valid.
465166756Sluigi		 */
466166756Sluigi		if (err == 0)
467166756Sluigi			fp->file = NULL;
468166756Sluigi	}
469166756Sluigi	mtx_unlock(&firmware_mtx);
470166756Sluigi}
471166756Sluigi
472166756Sluigi/*
473154974Smlaier * Module glue.
474154974Smlaier */
475154974Smlaierstatic int
476154974Smlaierfirmware_modevent(module_t mod, int type, void *unused)
477154974Smlaier{
478166756Sluigi	struct priv_fw *fp;
479178042Ssam	int i, err;
480159486Siedowse
481154974Smlaier	switch (type) {
482154974Smlaier	case MOD_LOAD:
483178042Ssam		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
484178042Ssam		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
485178042Ssam		    taskqueue_thread_enqueue, &firmware_tq);
486178042Ssam		/* NB: use our own loop routine that sets up context */
487178042Ssam		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
488178042Ssam		    "firmware taskq");
489178042Ssam		if (rootvnode != NULL) {
490178042Ssam			/*
491178042Ssam			 * Root is already mounted so we won't get an event;
492178042Ssam			 * simulate one here.
493178042Ssam			 */
494178042Ssam			firmware_mountroot(NULL);
495178042Ssam		}
496154974Smlaier		return 0;
497166756Sluigi
498154974Smlaier	case MOD_UNLOAD:
499166756Sluigi		/* request all autoloaded modules to be released */
500166756Sluigi		mtx_lock(&firmware_mtx);
501159486Siedowse		for (i = 0; i < FIRMWARE_MAX; i++) {
502159589Sjhb			fp = &firmware_table[i];
503201758Smbr			fp->flags |= FW_UNLOAD;
504159486Siedowse		}
505166756Sluigi		mtx_unlock(&firmware_mtx);
506178042Ssam		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
507178042Ssam		taskqueue_drain(firmware_tq, &firmware_unload_task);
508178042Ssam		err = 0;
509166756Sluigi		for (i = 0; i < FIRMWARE_MAX; i++) {
510166756Sluigi			fp = &firmware_table[i];
511166756Sluigi			if (fp->fw.name != NULL) {
512166756Sluigi				printf("%s: image %p ref %d still active slot %d\n",
513166756Sluigi					__func__, fp->fw.name,
514166756Sluigi					fp->refcnt,  i);
515166756Sluigi				err = EINVAL;
516166756Sluigi			}
517166756Sluigi		}
518178042Ssam		if (err == 0)
519178042Ssam			taskqueue_free(firmware_tq);
520166756Sluigi		return err;
521154974Smlaier	}
522154974Smlaier	return EINVAL;
523154974Smlaier}
524154974Smlaier
525154974Smlaierstatic moduledata_t firmware_mod = {
526154974Smlaier	"firmware",
527154974Smlaier	firmware_modevent,
528188057Simp	NULL
529154974Smlaier};
530154974SmlaierDECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
531154974SmlaierMODULE_VERSION(firmware, 1);
532