xref: /freebsd/sys/kern/subr_firmware.c (revision c2ede4b3)
16aec1278SMax Laier /*-
26c6eaea6SSam Leffler  * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com>
36aec1278SMax Laier  * All rights reserved.
46aec1278SMax Laier  *
56aec1278SMax Laier  * Redistribution and use in source and binary forms, with or without
66aec1278SMax Laier  * modification, are permitted provided that the following conditions
76aec1278SMax Laier  * are met:
86aec1278SMax Laier  * 1. Redistributions of source code must retain the above copyright
96aec1278SMax Laier  *    notice unmodified, this list of conditions, and the following
106aec1278SMax Laier  *    disclaimer.
116aec1278SMax Laier  * 2. Redistributions in binary form must reproduce the above copyright
126aec1278SMax Laier  *    notice, this list of conditions and the following disclaimer in the
136aec1278SMax Laier  *    documentation and/or other materials provided with the distribution.
146aec1278SMax Laier  *
156aec1278SMax Laier  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
166aec1278SMax Laier  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
176aec1278SMax Laier  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
186aec1278SMax Laier  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
196aec1278SMax Laier  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
206aec1278SMax Laier  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
216aec1278SMax Laier  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
226aec1278SMax Laier  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
236aec1278SMax Laier  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
246aec1278SMax Laier  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256aec1278SMax Laier  */
266aec1278SMax Laier 
276aec1278SMax Laier #include <sys/cdefs.h>
286aec1278SMax Laier __FBSDID("$FreeBSD$");
296aec1278SMax Laier 
306aec1278SMax Laier #include <sys/param.h>
316aec1278SMax Laier #include <sys/kernel.h>
326aec1278SMax Laier #include <sys/malloc.h>
336aec1278SMax Laier #include <sys/queue.h>
346aec1278SMax Laier #include <sys/taskqueue.h>
356aec1278SMax Laier #include <sys/systm.h>
366aec1278SMax Laier #include <sys/lock.h>
376aec1278SMax Laier #include <sys/mutex.h>
386aec1278SMax Laier #include <sys/errno.h>
396aec1278SMax Laier #include <sys/linker.h>
406aec1278SMax Laier #include <sys/firmware.h>
41acd3428bSRobert Watson #include <sys/priv.h>
426aec1278SMax Laier #include <sys/proc.h>
436aec1278SMax Laier #include <sys/module.h>
446c6eaea6SSam Leffler #include <sys/eventhandler.h>
456c6eaea6SSam Leffler 
466c6eaea6SSam Leffler #include <sys/filedesc.h>
476c6eaea6SSam Leffler #include <sys/vnode.h>
486aec1278SMax Laier 
4933d54970SLuigi Rizzo /*
5033d54970SLuigi Rizzo  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
5133d54970SLuigi Rizzo  * form more details on the subsystem.
5233d54970SLuigi Rizzo  *
5333d54970SLuigi Rizzo  * 'struct firmware' is the user-visible part of the firmware table.
5433d54970SLuigi Rizzo  * Additional internal information is stored in a 'struct priv_fw'
5533d54970SLuigi Rizzo  * (currently a static array). A slot is in use if FW_INUSE is true:
5633d54970SLuigi Rizzo  */
5733d54970SLuigi Rizzo 
5833d54970SLuigi Rizzo #define FW_INUSE(p)	((p)->file != NULL || (p)->fw.name != NULL)
5933d54970SLuigi Rizzo 
6033d54970SLuigi Rizzo /*
6133d54970SLuigi Rizzo  * fw.name != NULL when an image is registered; file != NULL for
6233d54970SLuigi Rizzo  * autoloaded images whose handling has not been completed.
6333d54970SLuigi Rizzo  *
6433d54970SLuigi Rizzo  * The state of a slot evolves as follows:
6533d54970SLuigi Rizzo  *	firmware_register	-->  fw.name = image_name
6633d54970SLuigi Rizzo  *	(autoloaded image)	-->  file = module reference
6733d54970SLuigi Rizzo  *	firmware_unregister	-->  fw.name = NULL
6833d54970SLuigi Rizzo  *	(unloadentry complete)	-->  file = NULL
6933d54970SLuigi Rizzo  *
7033d54970SLuigi Rizzo  * In order for the above to work, the 'file' field must remain
7133d54970SLuigi Rizzo  * unchanged in firmware_unregister().
7233d54970SLuigi Rizzo  *
7333d54970SLuigi Rizzo  * Images residing in the same module are linked to each other
7433d54970SLuigi Rizzo  * through the 'parent' argument of firmware_register().
7533d54970SLuigi Rizzo  * One image (typically, one with the same name as the module to let
7633d54970SLuigi Rizzo  * the autoloading mechanism work) is considered the parent image for
7733d54970SLuigi Rizzo  * all other images in the same module. Children affect the refcount
7833d54970SLuigi Rizzo  * on the parent image preventing improper unloading of the image itself.
7933d54970SLuigi Rizzo  */
8033d54970SLuigi Rizzo 
8133d54970SLuigi Rizzo struct priv_fw {
8233d54970SLuigi Rizzo 	int		refcnt;		/* reference count */
8333d54970SLuigi Rizzo 
8433d54970SLuigi Rizzo 	/*
8533d54970SLuigi Rizzo 	 * parent entry, see above. Set on firmware_register(),
8633d54970SLuigi Rizzo 	 * cleared on firmware_unregister().
8733d54970SLuigi Rizzo 	 */
8833d54970SLuigi Rizzo 	struct priv_fw	*parent;
8933d54970SLuigi Rizzo 
9033d54970SLuigi Rizzo 	int 		flags;	/* record FIRMWARE_UNLOAD requests */
9133d54970SLuigi Rizzo #define FW_UNLOAD	0x100
9233d54970SLuigi Rizzo 
9333d54970SLuigi Rizzo 	/*
9433d54970SLuigi Rizzo 	 * 'file' is private info managed by the autoload/unload code.
9533d54970SLuigi Rizzo 	 * Set at the end of firmware_get(), cleared only in the
966c6eaea6SSam Leffler 	 * firmware_unload_task, so the latter can depend on its value even
9733d54970SLuigi Rizzo 	 * while the lock is not held.
9833d54970SLuigi Rizzo 	 */
9933d54970SLuigi Rizzo 	linker_file_t   file;	/* module file, if autoloaded */
10033d54970SLuigi Rizzo 
10133d54970SLuigi Rizzo 	/*
10233d54970SLuigi Rizzo 	 * 'fw' is the externally visible image information.
10333d54970SLuigi Rizzo 	 * We do not make it the first field in priv_fw, to avoid the
10433d54970SLuigi Rizzo 	 * temptation of casting pointers to each other.
10533d54970SLuigi Rizzo 	 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
10633d54970SLuigi Rizzo 	 * Beware, PRIV_FW does not work for a NULL pointer.
10733d54970SLuigi Rizzo 	 */
10833d54970SLuigi Rizzo 	struct firmware	fw;	/* externally visible information */
10933d54970SLuigi Rizzo };
11033d54970SLuigi Rizzo 
11133d54970SLuigi Rizzo /*
11233d54970SLuigi Rizzo  * PRIV_FW returns the pointer to the container of struct firmware *x.
11333d54970SLuigi Rizzo  * Cast to intptr_t to override the 'const' attribute of x
11433d54970SLuigi Rizzo  */
11533d54970SLuigi Rizzo #define PRIV_FW(x)	((struct priv_fw *)		\
11633d54970SLuigi Rizzo 	((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
11733d54970SLuigi Rizzo 
11833d54970SLuigi Rizzo /*
11933d54970SLuigi Rizzo  * At the moment we use a static array as backing store for the registry.
12033d54970SLuigi Rizzo  * Should we move to a dynamic structure, keep in mind that we cannot
12133d54970SLuigi Rizzo  * reallocate the array because pointers are held externally.
12233d54970SLuigi Rizzo  * A list may work, though.
12333d54970SLuigi Rizzo  */
1246aec1278SMax Laier #define	FIRMWARE_MAX	30
12533d54970SLuigi Rizzo static struct priv_fw firmware_table[FIRMWARE_MAX];
12633d54970SLuigi Rizzo 
12733d54970SLuigi Rizzo /*
1286c6eaea6SSam Leffler  * Firmware module operations are handled in a separate task as they
1296c6eaea6SSam Leffler  * might sleep and they require directory context to do i/o.
13033d54970SLuigi Rizzo  */
1316c6eaea6SSam Leffler static struct taskqueue *firmware_tq;
1326c6eaea6SSam Leffler static struct task firmware_unload_task;
13333d54970SLuigi Rizzo 
13433d54970SLuigi Rizzo /*
13533d54970SLuigi Rizzo  * This mutex protects accesses to the firmware table.
13633d54970SLuigi Rizzo  */
1376c6eaea6SSam Leffler static struct mtx firmware_mtx;
1386aec1278SMax Laier MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
1396aec1278SMax Laier 
1406aec1278SMax Laier /*
14133d54970SLuigi Rizzo  * Helper function to lookup a name.
14233d54970SLuigi Rizzo  * As a side effect, it sets the pointer to a free slot, if any.
14333d54970SLuigi Rizzo  * This way we can concentrate most of the registry scanning in
14433d54970SLuigi Rizzo  * this function, which makes it easier to replace the registry
14533d54970SLuigi Rizzo  * with some other data structure.
14633d54970SLuigi Rizzo  */
14733d54970SLuigi Rizzo static struct priv_fw *
14833d54970SLuigi Rizzo lookup(const char *name, struct priv_fw **empty_slot)
14933d54970SLuigi Rizzo {
15033d54970SLuigi Rizzo 	struct priv_fw *fp = NULL;
15133d54970SLuigi Rizzo 	struct priv_fw *dummy;
15233d54970SLuigi Rizzo 	int i;
15333d54970SLuigi Rizzo 
15433d54970SLuigi Rizzo 	if (empty_slot == NULL)
15533d54970SLuigi Rizzo 		empty_slot = &dummy;
15633d54970SLuigi Rizzo 	*empty_slot = NULL;
15733d54970SLuigi Rizzo 	for (i = 0; i < FIRMWARE_MAX; i++) {
15833d54970SLuigi Rizzo 		fp = &firmware_table[i];
15933d54970SLuigi Rizzo 		if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
16033d54970SLuigi Rizzo 			break;
16133d54970SLuigi Rizzo 		else if (!FW_INUSE(fp))
16233d54970SLuigi Rizzo 			*empty_slot = fp;
16333d54970SLuigi Rizzo 	}
16433d54970SLuigi Rizzo 	return (i < FIRMWARE_MAX ) ? fp : NULL;
16533d54970SLuigi Rizzo }
16633d54970SLuigi Rizzo 
16733d54970SLuigi Rizzo /*
1686aec1278SMax Laier  * Register a firmware image with the specified name.  The
1696aec1278SMax Laier  * image name must not already be registered.  If this is a
1706aec1278SMax Laier  * subimage then parent refers to a previously registered
1716aec1278SMax Laier  * image that this should be associated with.
1726aec1278SMax Laier  */
17333d54970SLuigi Rizzo const struct firmware *
1746aec1278SMax Laier firmware_register(const char *imagename, const void *data, size_t datasize,
17533d54970SLuigi Rizzo     unsigned int version, const struct firmware *parent)
1766aec1278SMax Laier {
17733d54970SLuigi Rizzo 	struct priv_fw *match, *frp;
1786aec1278SMax Laier 
1796aec1278SMax Laier 	mtx_lock(&firmware_mtx);
18033d54970SLuigi Rizzo 	/*
18133d54970SLuigi Rizzo 	 * Do a lookup to make sure the name is unique or find a free slot.
18233d54970SLuigi Rizzo 	 */
18333d54970SLuigi Rizzo 	match = lookup(imagename, &frp);
18433d54970SLuigi Rizzo 	if (match != NULL) {
1856aec1278SMax Laier 		mtx_unlock(&firmware_mtx);
1866aec1278SMax Laier 		printf("%s: image %s already registered!\n",
1876aec1278SMax Laier 			__func__, imagename);
1886aec1278SMax Laier 		return NULL;
1896aec1278SMax Laier 	}
1906aec1278SMax Laier 	if (frp == NULL) {
1916aec1278SMax Laier 		mtx_unlock(&firmware_mtx);
1926aec1278SMax Laier 		printf("%s: cannot register image %s, firmware table full!\n",
1936aec1278SMax Laier 		    __func__, imagename);
1946aec1278SMax Laier 		return NULL;
1956aec1278SMax Laier 	}
19633d54970SLuigi Rizzo 	bzero(frp, sizeof(frp));	/* start from a clean record */
19733d54970SLuigi Rizzo 	frp->fw.name = imagename;
19833d54970SLuigi Rizzo 	frp->fw.data = data;
19933d54970SLuigi Rizzo 	frp->fw.datasize = datasize;
20033d54970SLuigi Rizzo 	frp->fw.version = version;
20133d54970SLuigi Rizzo 	if (parent != NULL) {
20233d54970SLuigi Rizzo 		frp->parent = PRIV_FW(parent);
20333d54970SLuigi Rizzo 		frp->parent->refcnt++;
20433d54970SLuigi Rizzo 	}
2056aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
20638d4db19SMax Laier 	if (bootverbose)
20738d4db19SMax Laier 		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
20838d4db19SMax Laier 		    imagename, version, datasize, data);
20933d54970SLuigi Rizzo 	return &frp->fw;
2106aec1278SMax Laier }
2116aec1278SMax Laier 
2126aec1278SMax Laier /*
2136aec1278SMax Laier  * Unregister/remove a firmware image.  If there are outstanding
2146aec1278SMax Laier  * references an error is returned and the image is not removed
2156aec1278SMax Laier  * from the registry.
2166aec1278SMax Laier  */
2176aec1278SMax Laier int
2186aec1278SMax Laier firmware_unregister(const char *imagename)
2196aec1278SMax Laier {
22033d54970SLuigi Rizzo 	struct priv_fw *fp;
22133d54970SLuigi Rizzo 	int err;
2226aec1278SMax Laier 
2236aec1278SMax Laier 	mtx_lock(&firmware_mtx);
22433d54970SLuigi Rizzo 	fp = lookup(imagename, NULL);
22533d54970SLuigi Rizzo 	if (fp == NULL) {
2266aec1278SMax Laier 		/*
22733d54970SLuigi Rizzo 		 * It is ok for the lookup to fail; this can happen
2286aec1278SMax Laier 		 * when a module is unloaded on last reference and the
2296aec1278SMax Laier 		 * module unload handler unregister's each of it's
2306aec1278SMax Laier 		 * firmware images.
2316aec1278SMax Laier 		 */
23233d54970SLuigi Rizzo 		err = 0;
23333d54970SLuigi Rizzo 	} else if (fp->refcnt != 0) {	/* cannot unregister */
23433d54970SLuigi Rizzo 		err = EBUSY;
23533d54970SLuigi Rizzo 	}  else {
23633d54970SLuigi Rizzo 		linker_file_t x = fp->file;	/* save value */
23733d54970SLuigi Rizzo 
23833d54970SLuigi Rizzo 		if (fp->parent != NULL)	/* release parent reference */
23933d54970SLuigi Rizzo 			fp->parent->refcnt--;
24033d54970SLuigi Rizzo 		/*
24133d54970SLuigi Rizzo 		 * Clear the whole entry with bzero to make sure we
24233d54970SLuigi Rizzo 		 * do not forget anything. Then restore 'file' which is
24333d54970SLuigi Rizzo 		 * non-null for autoloaded images.
24433d54970SLuigi Rizzo 		 */
24533d54970SLuigi Rizzo 		bzero(fp, sizeof(struct priv_fw));
24633d54970SLuigi Rizzo 		fp->file = x;
24733d54970SLuigi Rizzo 		err = 0;
2486aec1278SMax Laier 	}
2496aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
25033d54970SLuigi Rizzo 	return err;
2516aec1278SMax Laier }
2526aec1278SMax Laier 
2536c6eaea6SSam Leffler static void
2546c6eaea6SSam Leffler loadimage(void *arg, int npending)
2556c6eaea6SSam Leffler {
2566c6eaea6SSam Leffler 	struct thread *td = curthread;
2576c6eaea6SSam Leffler 	char *imagename = arg;
2586c6eaea6SSam Leffler 	struct priv_fw *fp;
2596c6eaea6SSam Leffler 	linker_file_t result;
2606c6eaea6SSam Leffler 	int error;
2616c6eaea6SSam Leffler 
2626c6eaea6SSam Leffler 	/* synchronize with the thread that dispatched us */
2636c6eaea6SSam Leffler 	mtx_lock(&firmware_mtx);
2646c6eaea6SSam Leffler 	mtx_unlock(&firmware_mtx);
2656c6eaea6SSam Leffler 
2666c6eaea6SSam Leffler 	if (td->td_proc->p_fd->fd_rdir == NULL) {
2676c6eaea6SSam Leffler 		printf("%s: root not mounted yet, no way to load image\n",
2686c6eaea6SSam Leffler 		    imagename);
2696c6eaea6SSam Leffler 		goto done;
2706c6eaea6SSam Leffler 	}
2716c6eaea6SSam Leffler 	error = linker_reference_module(imagename, NULL, &result);
2726c6eaea6SSam Leffler 	if (error != 0) {
2736c6eaea6SSam Leffler 		printf("%s: could not load firmware image, error %d\n",
2746c6eaea6SSam Leffler 		    imagename, error);
2756c6eaea6SSam Leffler 		goto done;
2766c6eaea6SSam Leffler 	}
2776c6eaea6SSam Leffler 
2786c6eaea6SSam Leffler 	mtx_lock(&firmware_mtx);
2796c6eaea6SSam Leffler 	fp = lookup(imagename, NULL);
2806c6eaea6SSam Leffler 	if (fp == NULL || fp->file != NULL) {
2816c6eaea6SSam Leffler 		mtx_unlock(&firmware_mtx);
2826c6eaea6SSam Leffler 		if (fp == NULL)
2836c6eaea6SSam Leffler 			printf("%s: firmware image loaded, "
2846c6eaea6SSam Leffler 			    "but did not register\n", imagename);
2856c6eaea6SSam Leffler 		(void) linker_release_module(imagename, NULL, NULL);
2866c6eaea6SSam Leffler 		goto done;
2876c6eaea6SSam Leffler 	}
2886c6eaea6SSam Leffler 	fp->file = result;	/* record the module identity */
2896c6eaea6SSam Leffler 	mtx_unlock(&firmware_mtx);
2906c6eaea6SSam Leffler done:
2916c6eaea6SSam Leffler 	wakeup_one(imagename);		/* we're done */
2926c6eaea6SSam Leffler }
2936c6eaea6SSam Leffler 
2946aec1278SMax Laier /*
2956aec1278SMax Laier  * Lookup and potentially load the specified firmware image.
29633d54970SLuigi Rizzo  * If the firmware is not found in the registry, try to load a kernel
29733d54970SLuigi Rizzo  * module named as the image name.
29833d54970SLuigi Rizzo  * If the firmware is located, a reference is returned. The caller must
29933d54970SLuigi Rizzo  * release this reference for the image to be eligible for removal/unload.
3006aec1278SMax Laier  */
30133d54970SLuigi Rizzo const struct firmware *
3026aec1278SMax Laier firmware_get(const char *imagename)
3036aec1278SMax Laier {
3046c6eaea6SSam Leffler 	struct task fwload_task;
3056aec1278SMax Laier 	struct thread *td;
30633d54970SLuigi Rizzo 	struct priv_fw *fp;
3076aec1278SMax Laier 
3086aec1278SMax Laier 	mtx_lock(&firmware_mtx);
30933d54970SLuigi Rizzo 	fp = lookup(imagename, NULL);
31033d54970SLuigi Rizzo 	if (fp != NULL)
31133d54970SLuigi Rizzo 		goto found;
3126aec1278SMax Laier 	/*
31333d54970SLuigi Rizzo 	 * Image not present, try to load the module holding it.
3146aec1278SMax Laier 	 */
3156aec1278SMax Laier 	td = curthread;
316acd3428bSRobert Watson 	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
317acd3428bSRobert Watson 	    securelevel_gt(td->td_ucred, 0) != 0) {
3186c6eaea6SSam Leffler 		mtx_unlock(&firmware_mtx);
3196aec1278SMax Laier 		printf("%s: insufficient privileges to "
3206aec1278SMax Laier 		    "load firmware image %s\n", __func__, imagename);
3216aec1278SMax Laier 		return NULL;
3226aec1278SMax Laier 	}
323450ec4edSIan Dowse 	/*
3246c6eaea6SSam Leffler 	 * Defer load to a thread with known context.  linker_reference_module
3256c6eaea6SSam Leffler 	 * may do filesystem i/o which requires root & current dirs, etc.
3266c6eaea6SSam Leffler 	 * Also we must not hold any mtx's over this call which is problematic.
327450ec4edSIan Dowse 	 */
328528fb798SAndrew Gallatin 	if (!cold) {
329528fb798SAndrew Gallatin 		TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *,
330528fb798SAndrew Gallatin 		    imagename));
3316c6eaea6SSam Leffler 		taskqueue_enqueue(firmware_tq, &fwload_task);
332528fb798SAndrew Gallatin 		msleep(__DECONST(void *, imagename), &firmware_mtx, 0,
333528fb798SAndrew Gallatin 		    "fwload", 0);
334528fb798SAndrew Gallatin 	}
3356c6eaea6SSam Leffler 	/*
3366c6eaea6SSam Leffler 	 * After attempting to load the module, see if the image is registered.
3376c6eaea6SSam Leffler 	 */
33833d54970SLuigi Rizzo 	fp = lookup(imagename, NULL);
33933d54970SLuigi Rizzo 	if (fp == NULL) {
3406aec1278SMax Laier 		mtx_unlock(&firmware_mtx);
34133d54970SLuigi Rizzo 		return NULL;
34233d54970SLuigi Rizzo 	}
34333d54970SLuigi Rizzo found:				/* common exit point on success */
34433d54970SLuigi Rizzo 	fp->refcnt++;
34533d54970SLuigi Rizzo 	mtx_unlock(&firmware_mtx);
34633d54970SLuigi Rizzo 	return &fp->fw;
3476aec1278SMax Laier }
3486aec1278SMax Laier 
3496aec1278SMax Laier /*
35033d54970SLuigi Rizzo  * Release a reference to a firmware image returned by firmware_get.
35133d54970SLuigi Rizzo  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
35233d54970SLuigi Rizzo  * to release the resource, but the flag is only advisory.
35333d54970SLuigi Rizzo  *
35433d54970SLuigi Rizzo  * If this is the last reference to the firmware image, and this is an
3556c6eaea6SSam Leffler  * autoloaded module, wake up the firmware_unload_task to figure out
3566c6eaea6SSam Leffler  * what to do with the associated module.
3576aec1278SMax Laier  */
3586aec1278SMax Laier void
35933d54970SLuigi Rizzo firmware_put(const struct firmware *p, int flags)
3606aec1278SMax Laier {
36133d54970SLuigi Rizzo 	struct priv_fw *fp = PRIV_FW(p);
36233d54970SLuigi Rizzo 
3636aec1278SMax Laier 	mtx_lock(&firmware_mtx);
3646aec1278SMax Laier 	fp->refcnt--;
365eb1030c4SIan Dowse 	if (fp->refcnt == 0) {
36633d54970SLuigi Rizzo 		if (flags & FIRMWARE_UNLOAD)
36733d54970SLuigi Rizzo 			fp->flags |= FW_UNLOAD;
3686aec1278SMax Laier 		if (fp->file)
3696c6eaea6SSam Leffler 			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
37033d54970SLuigi Rizzo 	}
37133d54970SLuigi Rizzo 	mtx_unlock(&firmware_mtx);
37233d54970SLuigi Rizzo }
37333d54970SLuigi Rizzo 
37433d54970SLuigi Rizzo /*
3756c6eaea6SSam Leffler  * Setup directory state for the firmware_tq thread so we can do i/o.
3766c6eaea6SSam Leffler  */
3776c6eaea6SSam Leffler static void
3786c6eaea6SSam Leffler set_rootvnode(void *arg, int npending)
3796c6eaea6SSam Leffler {
3806c6eaea6SSam Leffler 	struct thread *td = curthread;
3816c6eaea6SSam Leffler 	struct proc *p = td->td_proc;
3826c6eaea6SSam Leffler 
3836c6eaea6SSam Leffler 	FILEDESC_XLOCK(p->p_fd);
3846c6eaea6SSam Leffler 	if (p->p_fd->fd_cdir == NULL) {
3856c6eaea6SSam Leffler 		p->p_fd->fd_cdir = rootvnode;
3866c6eaea6SSam Leffler 		VREF(rootvnode);
3876c6eaea6SSam Leffler 	}
3886c6eaea6SSam Leffler 	if (p->p_fd->fd_rdir == NULL) {
3896c6eaea6SSam Leffler 		p->p_fd->fd_rdir = rootvnode;
3906c6eaea6SSam Leffler 		VREF(rootvnode);
3916c6eaea6SSam Leffler 	}
3926c6eaea6SSam Leffler 	FILEDESC_XUNLOCK(p->p_fd);
39373254c9eSSam Leffler 
39473254c9eSSam Leffler 	free(arg, M_TEMP);
3956c6eaea6SSam Leffler }
3966c6eaea6SSam Leffler 
3976c6eaea6SSam Leffler /*
3986c6eaea6SSam Leffler  * Event handler called on mounting of /; bounce a task
3996c6eaea6SSam Leffler  * into the task queue thread to setup it's directories.
4006c6eaea6SSam Leffler  */
4016c6eaea6SSam Leffler static void
4026c6eaea6SSam Leffler firmware_mountroot(void *arg)
4036c6eaea6SSam Leffler {
40473254c9eSSam Leffler 	struct task *setroot_task;
4056c6eaea6SSam Leffler 
40673254c9eSSam Leffler 	setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
40773254c9eSSam Leffler 	if (setroot_task != NULL) {
40873254c9eSSam Leffler 		TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
40973254c9eSSam Leffler 		taskqueue_enqueue(firmware_tq, setroot_task);
41073254c9eSSam Leffler 	} else
41173254c9eSSam Leffler 		printf("%s: no memory for task!\n", __func__);
4126c6eaea6SSam Leffler }
4136c6eaea6SSam Leffler EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
4146c6eaea6SSam Leffler 
4156c6eaea6SSam Leffler /*
41633d54970SLuigi Rizzo  * The body of the task in charge of unloading autoloaded modules
41733d54970SLuigi Rizzo  * that are not needed anymore.
41833d54970SLuigi Rizzo  * Images can be cross-linked so we may need to make multiple passes,
41933d54970SLuigi Rizzo  * but the time we spend in the loop is bounded because we clear entries
42033d54970SLuigi Rizzo  * as we touch them.
42133d54970SLuigi Rizzo  */
42233d54970SLuigi Rizzo static void
42333d54970SLuigi Rizzo unloadentry(void *unused1, int unused2)
42433d54970SLuigi Rizzo {
42533d54970SLuigi Rizzo 	int limit = FIRMWARE_MAX;
42633d54970SLuigi Rizzo 	int i;	/* current cycle */
42733d54970SLuigi Rizzo 
42833d54970SLuigi Rizzo 	mtx_lock(&firmware_mtx);
42933d54970SLuigi Rizzo 	/*
43033d54970SLuigi Rizzo 	 * Scan the table. limit is set to make sure we make another
43133d54970SLuigi Rizzo 	 * full sweep after matching an entry that requires unloading.
43233d54970SLuigi Rizzo 	 */
43333d54970SLuigi Rizzo 	for (i = 0; i < limit; i++) {
43433d54970SLuigi Rizzo 		struct priv_fw *fp;
43533d54970SLuigi Rizzo 		int err;
43633d54970SLuigi Rizzo 
43733d54970SLuigi Rizzo 		fp = &firmware_table[i % FIRMWARE_MAX];
43833d54970SLuigi Rizzo 		if (fp->fw.name == NULL || fp->file == NULL ||
43933d54970SLuigi Rizzo 		    fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0)
44033d54970SLuigi Rizzo 			continue;
44133d54970SLuigi Rizzo 
44233d54970SLuigi Rizzo 		/*
44333d54970SLuigi Rizzo 		 * Found an entry. Now:
44433d54970SLuigi Rizzo 		 * 1. bump up limit to make sure we make another full round;
44533d54970SLuigi Rizzo 		 * 2. clear FW_UNLOAD so we don't try this entry again.
44633d54970SLuigi Rizzo 		 * 3. release the lock while trying to unload the module.
44733d54970SLuigi Rizzo 		 * 'file' remains set so that the entry cannot be reused
44833d54970SLuigi Rizzo 		 * in the meantime (it also means that fp->file will
44933d54970SLuigi Rizzo 		 * not change while we release the lock).
45033d54970SLuigi Rizzo 		 */
45133d54970SLuigi Rizzo 		limit = i + FIRMWARE_MAX;	/* make another full round */
45233d54970SLuigi Rizzo 		fp->flags &= ~FW_UNLOAD;	/* do not try again */
45333d54970SLuigi Rizzo 
45433d54970SLuigi Rizzo 		mtx_unlock(&firmware_mtx);
45533d54970SLuigi Rizzo 		err = linker_release_module(NULL, NULL, fp->file);
45633d54970SLuigi Rizzo 		mtx_lock(&firmware_mtx);
45733d54970SLuigi Rizzo 
45833d54970SLuigi Rizzo 		/*
45933d54970SLuigi Rizzo 		 * We rely on the module to call firmware_unregister()
46033d54970SLuigi Rizzo 		 * on unload to actually release the entry.
46133d54970SLuigi Rizzo 		 * If err = 0 we can drop our reference as the system
46233d54970SLuigi Rizzo 		 * accepted it. Otherwise unloading failed (e.g. the
46333d54970SLuigi Rizzo 		 * module itself gave an error) so our reference is
46433d54970SLuigi Rizzo 		 * still valid.
46533d54970SLuigi Rizzo 		 */
46633d54970SLuigi Rizzo 		if (err == 0)
46733d54970SLuigi Rizzo 			fp->file = NULL;
46833d54970SLuigi Rizzo 	}
4696aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
4706aec1278SMax Laier }
4716aec1278SMax Laier 
4726aec1278SMax Laier /*
4736aec1278SMax Laier  * Module glue.
4746aec1278SMax Laier  */
4756aec1278SMax Laier static int
4766aec1278SMax Laier firmware_modevent(module_t mod, int type, void *unused)
4776aec1278SMax Laier {
47833d54970SLuigi Rizzo 	struct priv_fw *fp;
4796c6eaea6SSam Leffler 	int i, err;
480eb1030c4SIan Dowse 
4816aec1278SMax Laier 	switch (type) {
4826aec1278SMax Laier 	case MOD_LOAD:
4836c6eaea6SSam Leffler 		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
4846c6eaea6SSam Leffler 		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
4856c6eaea6SSam Leffler 		    taskqueue_thread_enqueue, &firmware_tq);
4866c6eaea6SSam Leffler 		/* NB: use our own loop routine that sets up context */
4876c6eaea6SSam Leffler 		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
4886c6eaea6SSam Leffler 		    "firmware taskq");
4896c6eaea6SSam Leffler 		if (rootvnode != NULL) {
4906c6eaea6SSam Leffler 			/*
4916c6eaea6SSam Leffler 			 * Root is already mounted so we won't get an event;
4926c6eaea6SSam Leffler 			 * simulate one here.
4936c6eaea6SSam Leffler 			 */
4946c6eaea6SSam Leffler 			firmware_mountroot(NULL);
4956c6eaea6SSam Leffler 		}
4966aec1278SMax Laier 		return 0;
49733d54970SLuigi Rizzo 
4986aec1278SMax Laier 	case MOD_UNLOAD:
49933d54970SLuigi Rizzo 		/* request all autoloaded modules to be released */
50033d54970SLuigi Rizzo 		mtx_lock(&firmware_mtx);
501eb1030c4SIan Dowse 		for (i = 0; i < FIRMWARE_MAX; i++) {
502b21c9288SJohn Baldwin 			fp = &firmware_table[i];
503c2ede4b3SMartin Blapp 			fp->flags |= FW_UNLOAD;
504eb1030c4SIan Dowse 		}
50533d54970SLuigi Rizzo 		mtx_unlock(&firmware_mtx);
5066c6eaea6SSam Leffler 		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
5076c6eaea6SSam Leffler 		taskqueue_drain(firmware_tq, &firmware_unload_task);
5086c6eaea6SSam Leffler 		err = 0;
50933d54970SLuigi Rizzo 		for (i = 0; i < FIRMWARE_MAX; i++) {
51033d54970SLuigi Rizzo 			fp = &firmware_table[i];
51133d54970SLuigi Rizzo 			if (fp->fw.name != NULL) {
51233d54970SLuigi Rizzo 				printf("%s: image %p ref %d still active slot %d\n",
51333d54970SLuigi Rizzo 					__func__, fp->fw.name,
51433d54970SLuigi Rizzo 					fp->refcnt,  i);
51533d54970SLuigi Rizzo 				err = EINVAL;
51633d54970SLuigi Rizzo 			}
51733d54970SLuigi Rizzo 		}
5186c6eaea6SSam Leffler 		if (err == 0)
5196c6eaea6SSam Leffler 			taskqueue_free(firmware_tq);
52033d54970SLuigi Rizzo 		return err;
5216aec1278SMax Laier 	}
5226aec1278SMax Laier 	return EINVAL;
5236aec1278SMax Laier }
5246aec1278SMax Laier 
5256aec1278SMax Laier static moduledata_t firmware_mod = {
5266aec1278SMax Laier 	"firmware",
5276aec1278SMax Laier 	firmware_modevent,
5284592c621SWarner Losh 	NULL
5296aec1278SMax Laier };
5306aec1278SMax Laier DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5316aec1278SMax Laier MODULE_VERSION(firmware, 1);
532