xref: /freebsd/sys/kern/subr_firmware.c (revision 6f65b505)
16aec1278SMax Laier /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
46c6eaea6SSam Leffler  * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com>
56aec1278SMax Laier  * All rights reserved.
66aec1278SMax Laier  *
76aec1278SMax Laier  * Redistribution and use in source and binary forms, with or without
86aec1278SMax Laier  * modification, are permitted provided that the following conditions
96aec1278SMax Laier  * are met:
106aec1278SMax Laier  * 1. Redistributions of source code must retain the above copyright
116aec1278SMax Laier  *    notice unmodified, this list of conditions, and the following
126aec1278SMax Laier  *    disclaimer.
136aec1278SMax Laier  * 2. Redistributions in binary form must reproduce the above copyright
146aec1278SMax Laier  *    notice, this list of conditions and the following disclaimer in the
156aec1278SMax Laier  *    documentation and/or other materials provided with the distribution.
166aec1278SMax Laier  *
176aec1278SMax Laier  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186aec1278SMax Laier  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196aec1278SMax Laier  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206aec1278SMax Laier  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216aec1278SMax Laier  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226aec1278SMax Laier  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236aec1278SMax Laier  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246aec1278SMax Laier  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256aec1278SMax Laier  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266aec1278SMax Laier  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276aec1278SMax Laier  */
286aec1278SMax Laier 
296aec1278SMax Laier #include <sys/cdefs.h>
306aec1278SMax Laier __FBSDID("$FreeBSD$");
316aec1278SMax Laier 
326aec1278SMax Laier #include <sys/param.h>
336aec1278SMax Laier #include <sys/kernel.h>
346aec1278SMax Laier #include <sys/malloc.h>
356aec1278SMax Laier #include <sys/queue.h>
366aec1278SMax Laier #include <sys/taskqueue.h>
376aec1278SMax Laier #include <sys/systm.h>
386aec1278SMax Laier #include <sys/lock.h>
396aec1278SMax Laier #include <sys/mutex.h>
406aec1278SMax Laier #include <sys/errno.h>
416aec1278SMax Laier #include <sys/linker.h>
426aec1278SMax Laier #include <sys/firmware.h>
43acd3428bSRobert Watson #include <sys/priv.h>
446aec1278SMax Laier #include <sys/proc.h>
456aec1278SMax Laier #include <sys/module.h>
466c6eaea6SSam Leffler #include <sys/eventhandler.h>
476c6eaea6SSam Leffler 
486c6eaea6SSam Leffler #include <sys/filedesc.h>
496c6eaea6SSam Leffler #include <sys/vnode.h>
506aec1278SMax Laier 
5133d54970SLuigi Rizzo /*
5233d54970SLuigi Rizzo  * Loadable firmware support. See sys/sys/firmware.h and firmware(9)
5333d54970SLuigi Rizzo  * form more details on the subsystem.
5433d54970SLuigi Rizzo  *
5533d54970SLuigi Rizzo  * 'struct firmware' is the user-visible part of the firmware table.
564f8ad92fSMark Johnston  * Additional internal information is stored in a 'struct priv_fw',
574f8ad92fSMark Johnston  * which embeds the public firmware structure.
5833d54970SLuigi Rizzo  */
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 */
834f8ad92fSMark Johnston 	LIST_ENTRY(priv_fw) link;	/* table linkage */
8433d54970SLuigi Rizzo 
8533d54970SLuigi Rizzo 	/*
8633d54970SLuigi Rizzo 	 * parent entry, see above. Set on firmware_register(),
8733d54970SLuigi Rizzo 	 * cleared on firmware_unregister().
8833d54970SLuigi Rizzo 	 */
8933d54970SLuigi Rizzo 	struct priv_fw	*parent;
9033d54970SLuigi Rizzo 
9133d54970SLuigi Rizzo 	int 		flags;	/* record FIRMWARE_UNLOAD requests */
9233d54970SLuigi Rizzo #define FW_UNLOAD	0x100
9333d54970SLuigi Rizzo 
9433d54970SLuigi Rizzo 	/*
9533d54970SLuigi Rizzo 	 * 'file' is private info managed by the autoload/unload code.
9633d54970SLuigi Rizzo 	 * Set at the end of firmware_get(), cleared only in the
976c6eaea6SSam Leffler 	 * firmware_unload_task, so the latter can depend on its value even
9833d54970SLuigi Rizzo 	 * while the lock is not held.
9933d54970SLuigi Rizzo 	 */
10033d54970SLuigi Rizzo 	linker_file_t   file;	/* module file, if autoloaded */
10133d54970SLuigi Rizzo 
10233d54970SLuigi Rizzo 	/*
10333d54970SLuigi Rizzo 	 * 'fw' is the externally visible image information.
10433d54970SLuigi Rizzo 	 * We do not make it the first field in priv_fw, to avoid the
10533d54970SLuigi Rizzo 	 * temptation of casting pointers to each other.
10633d54970SLuigi Rizzo 	 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw.
10733d54970SLuigi Rizzo 	 * Beware, PRIV_FW does not work for a NULL pointer.
10833d54970SLuigi Rizzo 	 */
10933d54970SLuigi Rizzo 	struct firmware	fw;	/* externally visible information */
11033d54970SLuigi Rizzo };
11133d54970SLuigi Rizzo 
11233d54970SLuigi Rizzo /*
11333d54970SLuigi Rizzo  * PRIV_FW returns the pointer to the container of struct firmware *x.
11433d54970SLuigi Rizzo  * Cast to intptr_t to override the 'const' attribute of x
11533d54970SLuigi Rizzo  */
11633d54970SLuigi Rizzo #define PRIV_FW(x)	((struct priv_fw *)		\
11733d54970SLuigi Rizzo 	((intptr_t)(x) - offsetof(struct priv_fw, fw)) )
11833d54970SLuigi Rizzo 
11933d54970SLuigi Rizzo /*
1204f8ad92fSMark Johnston  * Global firmware image registry.
12133d54970SLuigi Rizzo  */
1224f8ad92fSMark Johnston static LIST_HEAD(, priv_fw) firmware_table;
12333d54970SLuigi Rizzo 
12433d54970SLuigi Rizzo /*
1256c6eaea6SSam Leffler  * Firmware module operations are handled in a separate task as they
1266c6eaea6SSam Leffler  * might sleep and they require directory context to do i/o.
12733d54970SLuigi Rizzo  */
1286c6eaea6SSam Leffler static struct taskqueue *firmware_tq;
1296c6eaea6SSam Leffler static struct task firmware_unload_task;
13033d54970SLuigi Rizzo 
13133d54970SLuigi Rizzo /*
13233d54970SLuigi Rizzo  * This mutex protects accesses to the firmware table.
13333d54970SLuigi Rizzo  */
1346c6eaea6SSam Leffler static struct mtx firmware_mtx;
1356aec1278SMax Laier MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
1366aec1278SMax Laier 
1374f8ad92fSMark Johnston static MALLOC_DEFINE(M_FIRMWARE, "firmware", "device firmware images");
1384f8ad92fSMark Johnston 
1396aec1278SMax Laier /*
14033d54970SLuigi Rizzo  * Helper function to lookup a name.
14133d54970SLuigi Rizzo  * As a side effect, it sets the pointer to a free slot, if any.
14233d54970SLuigi Rizzo  * This way we can concentrate most of the registry scanning in
14333d54970SLuigi Rizzo  * this function, which makes it easier to replace the registry
14433d54970SLuigi Rizzo  * with some other data structure.
14533d54970SLuigi Rizzo  */
14633d54970SLuigi Rizzo static struct priv_fw *
1474f8ad92fSMark Johnston lookup(const char *name)
14833d54970SLuigi Rizzo {
1494f8ad92fSMark Johnston 	struct priv_fw *fp;
15033d54970SLuigi Rizzo 
1514f8ad92fSMark Johnston 	mtx_assert(&firmware_mtx, MA_OWNED);
1524f8ad92fSMark Johnston 
1534f8ad92fSMark Johnston 	LIST_FOREACH(fp, &firmware_table, link) {
15433d54970SLuigi Rizzo 		if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0)
15533d54970SLuigi Rizzo 			break;
15633d54970SLuigi Rizzo 	}
1574f8ad92fSMark Johnston 	return (fp);
15833d54970SLuigi Rizzo }
15933d54970SLuigi Rizzo 
16033d54970SLuigi Rizzo /*
1616aec1278SMax Laier  * Register a firmware image with the specified name.  The
1626aec1278SMax Laier  * image name must not already be registered.  If this is a
1636aec1278SMax Laier  * subimage then parent refers to a previously registered
1646aec1278SMax Laier  * image that this should be associated with.
1656aec1278SMax Laier  */
16633d54970SLuigi Rizzo const struct firmware *
1676aec1278SMax Laier firmware_register(const char *imagename, const void *data, size_t datasize,
16833d54970SLuigi Rizzo     unsigned int version, const struct firmware *parent)
1696aec1278SMax Laier {
1704f8ad92fSMark Johnston 	struct priv_fw *frp;
1714f8ad92fSMark Johnston 	char *name;
1726aec1278SMax Laier 
1736aec1278SMax Laier 	mtx_lock(&firmware_mtx);
1744f8ad92fSMark Johnston 	frp = lookup(imagename);
1754f8ad92fSMark Johnston 	if (frp != NULL) {
1766aec1278SMax Laier 		mtx_unlock(&firmware_mtx);
1776aec1278SMax Laier 		printf("%s: image %s already registered!\n",
1786aec1278SMax Laier 		    __func__, imagename);
1794f8ad92fSMark Johnston 		return (NULL);
1806aec1278SMax Laier 	}
1816aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
1824f8ad92fSMark Johnston 
1834f8ad92fSMark Johnston 	frp = malloc(sizeof(*frp), M_FIRMWARE, M_WAITOK | M_ZERO);
1844f8ad92fSMark Johnston 	name = strdup(imagename, M_FIRMWARE);
1854f8ad92fSMark Johnston 
1864f8ad92fSMark Johnston 	mtx_lock(&firmware_mtx);
1874f8ad92fSMark Johnston 	if (lookup(imagename) != NULL) {
1884f8ad92fSMark Johnston 		/* We lost a race. */
1894f8ad92fSMark Johnston 		mtx_unlock(&firmware_mtx);
1904f8ad92fSMark Johnston 		free(name, M_FIRMWARE);
1914f8ad92fSMark Johnston 		free(frp, M_FIRMWARE);
1924f8ad92fSMark Johnston 		return (NULL);
1936aec1278SMax Laier 	}
1944f8ad92fSMark Johnston 	frp->fw.name = name;
19533d54970SLuigi Rizzo 	frp->fw.data = data;
19633d54970SLuigi Rizzo 	frp->fw.datasize = datasize;
19733d54970SLuigi Rizzo 	frp->fw.version = version;
198059d10c7SNavdeep Parhar 	if (parent != NULL)
19933d54970SLuigi Rizzo 		frp->parent = PRIV_FW(parent);
2004f8ad92fSMark Johnston 	LIST_INSERT_HEAD(&firmware_table, frp, link);
2016aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
20238d4db19SMax Laier 	if (bootverbose)
20338d4db19SMax Laier 		printf("firmware: '%s' version %u: %zu bytes loaded at %p\n",
20438d4db19SMax Laier 		    imagename, version, datasize, data);
2054f8ad92fSMark Johnston 	return (&frp->fw);
2066aec1278SMax Laier }
2076aec1278SMax Laier 
2086aec1278SMax Laier /*
2096aec1278SMax Laier  * Unregister/remove a firmware image.  If there are outstanding
2106aec1278SMax Laier  * references an error is returned and the image is not removed
2116aec1278SMax Laier  * from the registry.
2126aec1278SMax Laier  */
2136aec1278SMax Laier int
2146aec1278SMax Laier firmware_unregister(const char *imagename)
2156aec1278SMax Laier {
21633d54970SLuigi Rizzo 	struct priv_fw *fp;
21733d54970SLuigi Rizzo 	int err;
2186aec1278SMax Laier 
2196aec1278SMax Laier 	mtx_lock(&firmware_mtx);
2204f8ad92fSMark Johnston 	fp = lookup(imagename);
22133d54970SLuigi Rizzo 	if (fp == NULL) {
2226aec1278SMax Laier 		/*
22333d54970SLuigi Rizzo 		 * It is ok for the lookup to fail; this can happen
2246aec1278SMax Laier 		 * when a module is unloaded on last reference and the
22528323addSBryan Drewery 		 * module unload handler unregister's each of its
2266aec1278SMax Laier 		 * firmware images.
2276aec1278SMax Laier 		 */
22833d54970SLuigi Rizzo 		err = 0;
22933d54970SLuigi Rizzo 	} else if (fp->refcnt != 0) {	/* cannot unregister */
23033d54970SLuigi Rizzo 		err = EBUSY;
23133d54970SLuigi Rizzo 	} else {
2324f8ad92fSMark Johnston 		LIST_REMOVE(fp, link);
2334f8ad92fSMark Johnston 		free(__DECONST(char *, fp->fw.name), M_FIRMWARE);
2344f8ad92fSMark Johnston 		free(fp, M_FIRMWARE);
23533d54970SLuigi Rizzo 		err = 0;
2366aec1278SMax Laier 	}
2376aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
2384f8ad92fSMark Johnston 	return (err);
2396aec1278SMax Laier }
2406aec1278SMax Laier 
2416f65b505SBjoern A. Zeeb struct fw_loadimage {
2426f65b505SBjoern A. Zeeb 	const char	*imagename;
2436f65b505SBjoern A. Zeeb 	uint32_t	flags;
2446f65b505SBjoern A. Zeeb };
2456f65b505SBjoern A. Zeeb 
2466c6eaea6SSam Leffler static void
2476f65b505SBjoern A. Zeeb loadimage(void *arg, int npending __unused)
2486c6eaea6SSam Leffler {
2496f65b505SBjoern A. Zeeb 	struct fw_loadimage *fwli = arg;
2506c6eaea6SSam Leffler 	struct priv_fw *fp;
2516c6eaea6SSam Leffler 	linker_file_t result;
2526c6eaea6SSam Leffler 	int error;
2536c6eaea6SSam Leffler 
2546f65b505SBjoern A. Zeeb 	error = linker_reference_module(fwli->imagename, NULL, &result);
2556c6eaea6SSam Leffler 	if (error != 0) {
2566f65b505SBjoern A. Zeeb 		if (bootverbose || (fwli->flags & FIRMWARE_GET_NOWARN) == 0)
2576c6eaea6SSam Leffler 			printf("%s: could not load firmware image, error %d\n",
2586f65b505SBjoern A. Zeeb 			    fwli->imagename, error);
2594f8ad92fSMark Johnston 		mtx_lock(&firmware_mtx);
2606c6eaea6SSam Leffler 		goto done;
2616c6eaea6SSam Leffler 	}
2626c6eaea6SSam Leffler 
2636c6eaea6SSam Leffler 	mtx_lock(&firmware_mtx);
2646f65b505SBjoern A. Zeeb 	fp = lookup(fwli->imagename);
2656c6eaea6SSam Leffler 	if (fp == NULL || fp->file != NULL) {
2666c6eaea6SSam Leffler 		mtx_unlock(&firmware_mtx);
2676c6eaea6SSam Leffler 		if (fp == NULL)
2686c6eaea6SSam Leffler 			printf("%s: firmware image loaded, "
2696f65b505SBjoern A. Zeeb 			    "but did not register\n", fwli->imagename);
2706f65b505SBjoern A. Zeeb 		(void) linker_release_module(fwli->imagename, NULL, NULL);
2714f8ad92fSMark Johnston 		mtx_lock(&firmware_mtx);
2726c6eaea6SSam Leffler 		goto done;
2736c6eaea6SSam Leffler 	}
2746c6eaea6SSam Leffler 	fp->file = result;	/* record the module identity */
2756c6eaea6SSam Leffler done:
2766f65b505SBjoern A. Zeeb 	wakeup_one(arg);
2774f8ad92fSMark Johnston 	mtx_unlock(&firmware_mtx);
2786c6eaea6SSam Leffler }
2796c6eaea6SSam Leffler 
2806aec1278SMax Laier /*
2816aec1278SMax Laier  * Lookup and potentially load the specified firmware image.
28233d54970SLuigi Rizzo  * If the firmware is not found in the registry, try to load a kernel
28333d54970SLuigi Rizzo  * module named as the image name.
28433d54970SLuigi Rizzo  * If the firmware is located, a reference is returned. The caller must
28533d54970SLuigi Rizzo  * release this reference for the image to be eligible for removal/unload.
2866aec1278SMax Laier  */
28733d54970SLuigi Rizzo const struct firmware *
2886f65b505SBjoern A. Zeeb firmware_get_flags(const char *imagename, uint32_t flags)
2896aec1278SMax Laier {
2906c6eaea6SSam Leffler 	struct task fwload_task;
2916aec1278SMax Laier 	struct thread *td;
29233d54970SLuigi Rizzo 	struct priv_fw *fp;
2936aec1278SMax Laier 
2946aec1278SMax Laier 	mtx_lock(&firmware_mtx);
2954f8ad92fSMark Johnston 	fp = lookup(imagename);
29633d54970SLuigi Rizzo 	if (fp != NULL)
29733d54970SLuigi Rizzo 		goto found;
2986aec1278SMax Laier 	/*
29933d54970SLuigi Rizzo 	 * Image not present, try to load the module holding it.
3006aec1278SMax Laier 	 */
3016aec1278SMax Laier 	td = curthread;
302acd3428bSRobert Watson 	if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 ||
303acd3428bSRobert Watson 	    securelevel_gt(td->td_ucred, 0) != 0) {
3046c6eaea6SSam Leffler 		mtx_unlock(&firmware_mtx);
3056aec1278SMax Laier 		printf("%s: insufficient privileges to "
3066aec1278SMax Laier 		    "load firmware image %s\n", __func__, imagename);
3076aec1278SMax Laier 		return NULL;
3086aec1278SMax Laier 	}
309450ec4edSIan Dowse 	/*
3106c6eaea6SSam Leffler 	 * Defer load to a thread with known context.  linker_reference_module
3116c6eaea6SSam Leffler 	 * may do filesystem i/o which requires root & current dirs, etc.
3126c6eaea6SSam Leffler 	 * Also we must not hold any mtx's over this call which is problematic.
313450ec4edSIan Dowse 	 */
314528fb798SAndrew Gallatin 	if (!cold) {
3156f65b505SBjoern A. Zeeb 		struct fw_loadimage fwli;
3166f65b505SBjoern A. Zeeb 
3176f65b505SBjoern A. Zeeb 		fwli.imagename = imagename;
3186f65b505SBjoern A. Zeeb 		fwli.flags = flags;
3196f65b505SBjoern A. Zeeb 		TASK_INIT(&fwload_task, 0, loadimage, (void *)&fwli);
3206c6eaea6SSam Leffler 		taskqueue_enqueue(firmware_tq, &fwload_task);
3216f65b505SBjoern A. Zeeb 		PHOLD(curproc);
3226f65b505SBjoern A. Zeeb 		msleep((void *)&fwli, &firmware_mtx, 0, "fwload", 0);
3236f65b505SBjoern A. Zeeb 		PRELE(curproc);
324528fb798SAndrew Gallatin 	}
3256c6eaea6SSam Leffler 	/*
3266c6eaea6SSam Leffler 	 * After attempting to load the module, see if the image is registered.
3276c6eaea6SSam Leffler 	 */
3284f8ad92fSMark Johnston 	fp = lookup(imagename);
32933d54970SLuigi Rizzo 	if (fp == NULL) {
3306aec1278SMax Laier 		mtx_unlock(&firmware_mtx);
33133d54970SLuigi Rizzo 		return NULL;
33233d54970SLuigi Rizzo 	}
33333d54970SLuigi Rizzo found:				/* common exit point on success */
334059d10c7SNavdeep Parhar 	if (fp->refcnt == 0 && fp->parent != NULL)
335059d10c7SNavdeep Parhar 		fp->parent->refcnt++;
33633d54970SLuigi Rizzo 	fp->refcnt++;
33733d54970SLuigi Rizzo 	mtx_unlock(&firmware_mtx);
33833d54970SLuigi Rizzo 	return &fp->fw;
3396aec1278SMax Laier }
3406aec1278SMax Laier 
3416f65b505SBjoern A. Zeeb const struct firmware *
3426f65b505SBjoern A. Zeeb firmware_get(const char *imagename)
3436f65b505SBjoern A. Zeeb {
3446f65b505SBjoern A. Zeeb 
3456f65b505SBjoern A. Zeeb 	return (firmware_get_flags(imagename, 0));
3466f65b505SBjoern A. Zeeb }
3476f65b505SBjoern A. Zeeb 
3486aec1278SMax Laier /*
34933d54970SLuigi Rizzo  * Release a reference to a firmware image returned by firmware_get.
35033d54970SLuigi Rizzo  * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire
35133d54970SLuigi Rizzo  * to release the resource, but the flag is only advisory.
35233d54970SLuigi Rizzo  *
35333d54970SLuigi Rizzo  * If this is the last reference to the firmware image, and this is an
3546c6eaea6SSam Leffler  * autoloaded module, wake up the firmware_unload_task to figure out
3556c6eaea6SSam Leffler  * what to do with the associated module.
3566aec1278SMax Laier  */
3576aec1278SMax Laier void
35833d54970SLuigi Rizzo firmware_put(const struct firmware *p, int flags)
3596aec1278SMax Laier {
36033d54970SLuigi Rizzo 	struct priv_fw *fp = PRIV_FW(p);
36133d54970SLuigi Rizzo 
3626aec1278SMax Laier 	mtx_lock(&firmware_mtx);
3636aec1278SMax Laier 	fp->refcnt--;
364eb1030c4SIan Dowse 	if (fp->refcnt == 0) {
365059d10c7SNavdeep Parhar 		if (fp->parent != NULL)
366059d10c7SNavdeep Parhar 			fp->parent->refcnt--;
36733d54970SLuigi Rizzo 		if (flags & FIRMWARE_UNLOAD)
36833d54970SLuigi Rizzo 			fp->flags |= FW_UNLOAD;
3696aec1278SMax Laier 		if (fp->file)
3706c6eaea6SSam Leffler 			taskqueue_enqueue(firmware_tq, &firmware_unload_task);
37133d54970SLuigi Rizzo 	}
37233d54970SLuigi Rizzo 	mtx_unlock(&firmware_mtx);
37333d54970SLuigi Rizzo }
37433d54970SLuigi Rizzo 
37533d54970SLuigi Rizzo /*
3766c6eaea6SSam Leffler  * Setup directory state for the firmware_tq thread so we can do i/o.
3776c6eaea6SSam Leffler  */
3786c6eaea6SSam Leffler static void
3796c6eaea6SSam Leffler set_rootvnode(void *arg, int npending)
3806c6eaea6SSam Leffler {
3816c6eaea6SSam Leffler 
3828a08cec1SMateusz Guzik 	pwd_ensure_dirs();
38373254c9eSSam Leffler 	free(arg, M_TEMP);
3846c6eaea6SSam Leffler }
3856c6eaea6SSam Leffler 
3866c6eaea6SSam Leffler /*
3876c6eaea6SSam Leffler  * Event handler called on mounting of /; bounce a task
3886c6eaea6SSam Leffler  * into the task queue thread to setup it's directories.
3896c6eaea6SSam Leffler  */
3906c6eaea6SSam Leffler static void
3916c6eaea6SSam Leffler firmware_mountroot(void *arg)
3926c6eaea6SSam Leffler {
39373254c9eSSam Leffler 	struct task *setroot_task;
3946c6eaea6SSam Leffler 
39573254c9eSSam Leffler 	setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT);
39673254c9eSSam Leffler 	if (setroot_task != NULL) {
39773254c9eSSam Leffler 		TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task);
39873254c9eSSam Leffler 		taskqueue_enqueue(firmware_tq, setroot_task);
39973254c9eSSam Leffler 	} else
40073254c9eSSam Leffler 		printf("%s: no memory for task!\n", __func__);
4016c6eaea6SSam Leffler }
4026c6eaea6SSam Leffler EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0);
4036c6eaea6SSam Leffler 
4046c6eaea6SSam Leffler /*
40533d54970SLuigi Rizzo  * The body of the task in charge of unloading autoloaded modules
40633d54970SLuigi Rizzo  * that are not needed anymore.
40733d54970SLuigi Rizzo  * Images can be cross-linked so we may need to make multiple passes,
40833d54970SLuigi Rizzo  * but the time we spend in the loop is bounded because we clear entries
40933d54970SLuigi Rizzo  * as we touch them.
41033d54970SLuigi Rizzo  */
41133d54970SLuigi Rizzo static void
41233d54970SLuigi Rizzo unloadentry(void *unused1, int unused2)
41333d54970SLuigi Rizzo {
41446cac10bSAndrew Gallatin 	struct priv_fw *fp;
4154f8ad92fSMark Johnston 	int err;
41633d54970SLuigi Rizzo 
41733d54970SLuigi Rizzo 	mtx_lock(&firmware_mtx);
4184f8ad92fSMark Johnston restart:
41946cac10bSAndrew Gallatin 	LIST_FOREACH(fp, &firmware_table, link) {
4204f8ad92fSMark Johnston 		if (fp->file == NULL || fp->refcnt != 0 ||
4214f8ad92fSMark Johnston 		    (fp->flags & FW_UNLOAD) == 0)
42233d54970SLuigi Rizzo 			continue;
42333d54970SLuigi Rizzo 
42433d54970SLuigi Rizzo 		/*
42533d54970SLuigi Rizzo 		 * Found an entry. Now:
4264f8ad92fSMark Johnston 		 * 1. make sure we scan the table again
42733d54970SLuigi Rizzo 		 * 2. clear FW_UNLOAD so we don't try this entry again.
42833d54970SLuigi Rizzo 		 * 3. release the lock while trying to unload the module.
42933d54970SLuigi Rizzo 		 */
43033d54970SLuigi Rizzo 		fp->flags &= ~FW_UNLOAD;	/* do not try again */
43133d54970SLuigi Rizzo 
43233d54970SLuigi Rizzo 		/*
43333d54970SLuigi Rizzo 		 * We rely on the module to call firmware_unregister()
4344f8ad92fSMark Johnston 		 * on unload to actually free the entry.
43533d54970SLuigi Rizzo 		 */
4364f8ad92fSMark Johnston 		mtx_unlock(&firmware_mtx);
4374f8ad92fSMark Johnston 		err = linker_release_module(NULL, NULL, fp->file);
4384f8ad92fSMark Johnston 		mtx_lock(&firmware_mtx);
43946cac10bSAndrew Gallatin 
44046cac10bSAndrew Gallatin 		/*
44146cac10bSAndrew Gallatin 		 * When we dropped the lock, another thread could have
44246cac10bSAndrew Gallatin 		 * removed an element, so we must restart the scan.
44346cac10bSAndrew Gallatin 		 */
4444f8ad92fSMark Johnston 		goto restart;
44533d54970SLuigi Rizzo 	}
4466aec1278SMax Laier 	mtx_unlock(&firmware_mtx);
4476aec1278SMax Laier }
4486aec1278SMax Laier 
4496aec1278SMax Laier /*
4506aec1278SMax Laier  * Module glue.
4516aec1278SMax Laier  */
4526aec1278SMax Laier static int
4536aec1278SMax Laier firmware_modevent(module_t mod, int type, void *unused)
4546aec1278SMax Laier {
45533d54970SLuigi Rizzo 	struct priv_fw *fp;
4564f8ad92fSMark Johnston 	int err;
457eb1030c4SIan Dowse 
4584f8ad92fSMark Johnston 	err = 0;
4596aec1278SMax Laier 	switch (type) {
4606aec1278SMax Laier 	case MOD_LOAD:
4616c6eaea6SSam Leffler 		TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL);
4626c6eaea6SSam Leffler 		firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK,
4636c6eaea6SSam Leffler 		    taskqueue_thread_enqueue, &firmware_tq);
4646c6eaea6SSam Leffler 		/* NB: use our own loop routine that sets up context */
4656c6eaea6SSam Leffler 		(void) taskqueue_start_threads(&firmware_tq, 1, PWAIT,
4666c6eaea6SSam Leffler 		    "firmware taskq");
4676c6eaea6SSam Leffler 		if (rootvnode != NULL) {
4686c6eaea6SSam Leffler 			/*
4696c6eaea6SSam Leffler 			 * Root is already mounted so we won't get an event;
4706c6eaea6SSam Leffler 			 * simulate one here.
4716c6eaea6SSam Leffler 			 */
4726c6eaea6SSam Leffler 			firmware_mountroot(NULL);
4736c6eaea6SSam Leffler 		}
4744f8ad92fSMark Johnston 		break;
47533d54970SLuigi Rizzo 
4766aec1278SMax Laier 	case MOD_UNLOAD:
47733d54970SLuigi Rizzo 		/* request all autoloaded modules to be released */
47833d54970SLuigi Rizzo 		mtx_lock(&firmware_mtx);
4794f8ad92fSMark Johnston 		LIST_FOREACH(fp, &firmware_table, link)
480c2ede4b3SMartin Blapp 			fp->flags |= FW_UNLOAD;
48133d54970SLuigi Rizzo 		mtx_unlock(&firmware_mtx);
4826c6eaea6SSam Leffler 		taskqueue_enqueue(firmware_tq, &firmware_unload_task);
4836c6eaea6SSam Leffler 		taskqueue_drain(firmware_tq, &firmware_unload_task);
4844f8ad92fSMark Johnston 
4854f8ad92fSMark Johnston 		LIST_FOREACH(fp, &firmware_table, link) {
48633d54970SLuigi Rizzo 			if (fp->fw.name != NULL) {
4874f8ad92fSMark Johnston 				printf("%s: image %s still active, %d refs\n",
4884f8ad92fSMark Johnston 				    __func__, fp->fw.name, fp->refcnt);
48933d54970SLuigi Rizzo 				err = EINVAL;
49033d54970SLuigi Rizzo 			}
49133d54970SLuigi Rizzo 		}
4926c6eaea6SSam Leffler 		if (err == 0)
4936c6eaea6SSam Leffler 			taskqueue_free(firmware_tq);
4944f8ad92fSMark Johnston 		break;
4954f8ad92fSMark Johnston 
4964f8ad92fSMark Johnston 	default:
4974f8ad92fSMark Johnston 		err = EOPNOTSUPP;
4984f8ad92fSMark Johnston 		break;
4996aec1278SMax Laier 	}
5004f8ad92fSMark Johnston 	return (err);
5016aec1278SMax Laier }
5026aec1278SMax Laier 
5036aec1278SMax Laier static moduledata_t firmware_mod = {
5046aec1278SMax Laier 	"firmware",
5056aec1278SMax Laier 	firmware_modevent,
5064592c621SWarner Losh 	NULL
5076aec1278SMax Laier };
5086aec1278SMax Laier DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
5096aec1278SMax Laier MODULE_VERSION(firmware, 1);
510