16aec1278SMax Laier /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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/param.h> 306aec1278SMax Laier #include <sys/errno.h> 314b62b42aSWarner Losh #include <sys/eventhandler.h> 32c7b1e980SWarner Losh #include <sys/fcntl.h> 336aec1278SMax Laier #include <sys/firmware.h> 344b62b42aSWarner Losh #include <sys/kernel.h> 354b62b42aSWarner Losh #include <sys/linker.h> 364b62b42aSWarner Losh #include <sys/lock.h> 374b62b42aSWarner Losh #include <sys/malloc.h> 384b62b42aSWarner Losh #include <sys/module.h> 394b62b42aSWarner Losh #include <sys/mutex.h> 40c7b1e980SWarner Losh #include <sys/namei.h> 41acd3428bSRobert Watson #include <sys/priv.h> 426aec1278SMax Laier #include <sys/proc.h> 434b62b42aSWarner Losh #include <sys/queue.h> 44c7b1e980SWarner Losh #include <sys/sbuf.h> 45c7b1e980SWarner Losh #include <sys/sysctl.h> 464b62b42aSWarner Losh #include <sys/systm.h> 474b62b42aSWarner Losh #include <sys/taskqueue.h> 486c6eaea6SSam Leffler 496c6eaea6SSam Leffler #include <sys/filedesc.h> 506c6eaea6SSam Leffler #include <sys/vnode.h> 516aec1278SMax Laier 5233d54970SLuigi Rizzo /* 5333d54970SLuigi Rizzo * Loadable firmware support. See sys/sys/firmware.h and firmware(9) 5433d54970SLuigi Rizzo * form more details on the subsystem. 5533d54970SLuigi Rizzo * 5633d54970SLuigi Rizzo * 'struct firmware' is the user-visible part of the firmware table. 574f8ad92fSMark Johnston * Additional internal information is stored in a 'struct priv_fw', 584f8ad92fSMark Johnston * which embeds the public firmware structure. 5933d54970SLuigi Rizzo */ 6033d54970SLuigi Rizzo 6133d54970SLuigi Rizzo /* 6233d54970SLuigi Rizzo * fw.name != NULL when an image is registered; file != NULL for 6333d54970SLuigi Rizzo * autoloaded images whose handling has not been completed. 6433d54970SLuigi Rizzo * 6533d54970SLuigi Rizzo * The state of a slot evolves as follows: 6633d54970SLuigi Rizzo * firmware_register --> fw.name = image_name 6733d54970SLuigi Rizzo * (autoloaded image) --> file = module reference 6833d54970SLuigi Rizzo * firmware_unregister --> fw.name = NULL 6933d54970SLuigi Rizzo * (unloadentry complete) --> file = NULL 7033d54970SLuigi Rizzo * 7133d54970SLuigi Rizzo * In order for the above to work, the 'file' field must remain 7233d54970SLuigi Rizzo * unchanged in firmware_unregister(). 7333d54970SLuigi Rizzo * 7433d54970SLuigi Rizzo * Images residing in the same module are linked to each other 7533d54970SLuigi Rizzo * through the 'parent' argument of firmware_register(). 7633d54970SLuigi Rizzo * One image (typically, one with the same name as the module to let 7733d54970SLuigi Rizzo * the autoloading mechanism work) is considered the parent image for 7833d54970SLuigi Rizzo * all other images in the same module. Children affect the refcount 7933d54970SLuigi Rizzo * on the parent image preventing improper unloading of the image itself. 8033d54970SLuigi Rizzo */ 8133d54970SLuigi Rizzo 8233d54970SLuigi Rizzo struct priv_fw { 8333d54970SLuigi Rizzo int refcnt; /* reference count */ 844f8ad92fSMark Johnston LIST_ENTRY(priv_fw) link; /* table linkage */ 8533d54970SLuigi Rizzo 8633d54970SLuigi Rizzo /* 8733d54970SLuigi Rizzo * parent entry, see above. Set on firmware_register(), 8833d54970SLuigi Rizzo * cleared on firmware_unregister(). 8933d54970SLuigi Rizzo */ 9033d54970SLuigi Rizzo struct priv_fw *parent; 9133d54970SLuigi Rizzo 92c7b1e980SWarner Losh int flags; 93c7b1e980SWarner Losh #define FW_BINARY 0x080 /* Firmware directly loaded, file == NULL */ 94c7b1e980SWarner Losh #define FW_UNLOAD 0x100 /* record FIRMWARE_UNLOAD requests */ 9533d54970SLuigi Rizzo 9633d54970SLuigi Rizzo /* 9733d54970SLuigi Rizzo * 'file' is private info managed by the autoload/unload code. 9833d54970SLuigi Rizzo * Set at the end of firmware_get(), cleared only in the 996c6eaea6SSam Leffler * firmware_unload_task, so the latter can depend on its value even 10033d54970SLuigi Rizzo * while the lock is not held. 10133d54970SLuigi Rizzo */ 10233d54970SLuigi Rizzo linker_file_t file; /* module file, if autoloaded */ 10333d54970SLuigi Rizzo 10433d54970SLuigi Rizzo /* 10533d54970SLuigi Rizzo * 'fw' is the externally visible image information. 10633d54970SLuigi Rizzo * We do not make it the first field in priv_fw, to avoid the 10733d54970SLuigi Rizzo * temptation of casting pointers to each other. 10833d54970SLuigi Rizzo * Use PRIV_FW(fw) to get a pointer to the cointainer of fw. 10933d54970SLuigi Rizzo * Beware, PRIV_FW does not work for a NULL pointer. 11033d54970SLuigi Rizzo */ 11133d54970SLuigi Rizzo struct firmware fw; /* externally visible information */ 11233d54970SLuigi Rizzo }; 11333d54970SLuigi Rizzo 11433d54970SLuigi Rizzo /* 11533d54970SLuigi Rizzo * PRIV_FW returns the pointer to the container of struct firmware *x. 11633d54970SLuigi Rizzo * Cast to intptr_t to override the 'const' attribute of x 11733d54970SLuigi Rizzo */ 11833d54970SLuigi Rizzo #define PRIV_FW(x) ((struct priv_fw *) \ 11933d54970SLuigi Rizzo ((intptr_t)(x) - offsetof(struct priv_fw, fw)) ) 12033d54970SLuigi Rizzo 12133d54970SLuigi Rizzo /* 1224f8ad92fSMark Johnston * Global firmware image registry. 12333d54970SLuigi Rizzo */ 1244f8ad92fSMark Johnston static LIST_HEAD(, priv_fw) firmware_table; 12533d54970SLuigi Rizzo 12633d54970SLuigi Rizzo /* 1276c6eaea6SSam Leffler * Firmware module operations are handled in a separate task as they 128c7b1e980SWarner Losh * might sleep and they require directory context to do i/o. We also 129c7b1e980SWarner Losh * use this when loading binaries directly. 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 1404f8ad92fSMark Johnston static MALLOC_DEFINE(M_FIRMWARE, "firmware", "device firmware images"); 1414f8ad92fSMark Johnston 142c7b1e980SWarner Losh static uint64_t firmware_max_size = 8u << 20; /* Default to 8MB cap */ 143c7b1e980SWarner Losh SYSCTL_U64(_debug, OID_AUTO, firmware_max_size, 144c7b1e980SWarner Losh CTLFLAG_RWTUN, &firmware_max_size, 0, 145c7b1e980SWarner Losh "Max size permitted for a firmware file."); 146c7b1e980SWarner Losh 1476aec1278SMax Laier /* 14833d54970SLuigi Rizzo * Helper function to lookup a name. 14933d54970SLuigi Rizzo * As a side effect, it sets the pointer to a free slot, if any. 15033d54970SLuigi Rizzo * This way we can concentrate most of the registry scanning in 15133d54970SLuigi Rizzo * this function, which makes it easier to replace the registry 15233d54970SLuigi Rizzo * with some other data structure. 15333d54970SLuigi Rizzo */ 15433d54970SLuigi Rizzo static struct priv_fw * 1554f8ad92fSMark Johnston lookup(const char *name) 15633d54970SLuigi Rizzo { 1574f8ad92fSMark Johnston struct priv_fw *fp; 15833d54970SLuigi Rizzo 1594f8ad92fSMark Johnston mtx_assert(&firmware_mtx, MA_OWNED); 1604f8ad92fSMark Johnston 1614f8ad92fSMark Johnston LIST_FOREACH(fp, &firmware_table, link) { 16233d54970SLuigi Rizzo if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0) 16333d54970SLuigi Rizzo break; 16433d54970SLuigi Rizzo } 1654f8ad92fSMark Johnston return (fp); 16633d54970SLuigi Rizzo } 16733d54970SLuigi Rizzo 16833d54970SLuigi Rizzo /* 1696aec1278SMax Laier * Register a firmware image with the specified name. The 1706aec1278SMax Laier * image name must not already be registered. If this is a 1716aec1278SMax Laier * subimage then parent refers to a previously registered 1726aec1278SMax Laier * image that this should be associated with. 1736aec1278SMax Laier */ 17433d54970SLuigi Rizzo const struct firmware * 1756aec1278SMax Laier firmware_register(const char *imagename, const void *data, size_t datasize, 17633d54970SLuigi Rizzo unsigned int version, const struct firmware *parent) 1776aec1278SMax Laier { 1784f8ad92fSMark Johnston struct priv_fw *frp; 1794f8ad92fSMark Johnston char *name; 1806aec1278SMax Laier 1816aec1278SMax Laier mtx_lock(&firmware_mtx); 1824f8ad92fSMark Johnston frp = lookup(imagename); 1834f8ad92fSMark Johnston if (frp != NULL) { 1846aec1278SMax Laier mtx_unlock(&firmware_mtx); 1856aec1278SMax Laier printf("%s: image %s already registered!\n", 1866aec1278SMax Laier __func__, imagename); 1874f8ad92fSMark Johnston return (NULL); 1886aec1278SMax Laier } 1896aec1278SMax Laier mtx_unlock(&firmware_mtx); 1904f8ad92fSMark Johnston 1914f8ad92fSMark Johnston frp = malloc(sizeof(*frp), M_FIRMWARE, M_WAITOK | M_ZERO); 1924f8ad92fSMark Johnston name = strdup(imagename, M_FIRMWARE); 1934f8ad92fSMark Johnston 1944f8ad92fSMark Johnston mtx_lock(&firmware_mtx); 1954f8ad92fSMark Johnston if (lookup(imagename) != NULL) { 1964f8ad92fSMark Johnston /* We lost a race. */ 1974f8ad92fSMark Johnston mtx_unlock(&firmware_mtx); 1984f8ad92fSMark Johnston free(name, M_FIRMWARE); 1994f8ad92fSMark Johnston free(frp, M_FIRMWARE); 2004f8ad92fSMark Johnston return (NULL); 2016aec1278SMax Laier } 2024f8ad92fSMark Johnston frp->fw.name = name; 20333d54970SLuigi Rizzo frp->fw.data = data; 20433d54970SLuigi Rizzo frp->fw.datasize = datasize; 20533d54970SLuigi Rizzo frp->fw.version = version; 206059d10c7SNavdeep Parhar if (parent != NULL) 20733d54970SLuigi Rizzo frp->parent = PRIV_FW(parent); 2084f8ad92fSMark Johnston LIST_INSERT_HEAD(&firmware_table, frp, link); 2096aec1278SMax Laier mtx_unlock(&firmware_mtx); 21038d4db19SMax Laier if (bootverbose) 21138d4db19SMax Laier printf("firmware: '%s' version %u: %zu bytes loaded at %p\n", 21238d4db19SMax Laier imagename, version, datasize, data); 2134f8ad92fSMark Johnston return (&frp->fw); 2146aec1278SMax Laier } 2156aec1278SMax Laier 2166aec1278SMax Laier /* 2176aec1278SMax Laier * Unregister/remove a firmware image. If there are outstanding 2186aec1278SMax Laier * references an error is returned and the image is not removed 2196aec1278SMax Laier * from the registry. 2206aec1278SMax Laier */ 2216aec1278SMax Laier int 2226aec1278SMax Laier firmware_unregister(const char *imagename) 2236aec1278SMax Laier { 22433d54970SLuigi Rizzo struct priv_fw *fp; 22533d54970SLuigi Rizzo int err; 2266aec1278SMax Laier 2276aec1278SMax Laier mtx_lock(&firmware_mtx); 2284f8ad92fSMark Johnston fp = lookup(imagename); 22933d54970SLuigi Rizzo if (fp == NULL) { 2306aec1278SMax Laier /* 23133d54970SLuigi Rizzo * It is ok for the lookup to fail; this can happen 2326aec1278SMax Laier * when a module is unloaded on last reference and the 23328323addSBryan Drewery * module unload handler unregister's each of its 2346aec1278SMax Laier * firmware images. 2356aec1278SMax Laier */ 23633d54970SLuigi Rizzo err = 0; 23733d54970SLuigi Rizzo } else if (fp->refcnt != 0) { /* cannot unregister */ 23833d54970SLuigi Rizzo err = EBUSY; 23933d54970SLuigi Rizzo } else { 2404f8ad92fSMark Johnston LIST_REMOVE(fp, link); 2414f8ad92fSMark Johnston free(__DECONST(char *, fp->fw.name), M_FIRMWARE); 2424f8ad92fSMark Johnston free(fp, M_FIRMWARE); 24333d54970SLuigi Rizzo err = 0; 2446aec1278SMax Laier } 2456aec1278SMax Laier mtx_unlock(&firmware_mtx); 2464f8ad92fSMark Johnston return (err); 2476aec1278SMax Laier } 2486aec1278SMax Laier 2496f65b505SBjoern A. Zeeb struct fw_loadimage { 2506f65b505SBjoern A. Zeeb const char *imagename; 2516f65b505SBjoern A. Zeeb uint32_t flags; 2526f65b505SBjoern A. Zeeb }; 2536f65b505SBjoern A. Zeeb 254c7b1e980SWarner Losh static const char *fw_path = "/boot/firmware/"; 255c7b1e980SWarner Losh 256c7b1e980SWarner Losh static void 257c7b1e980SWarner Losh try_binary_file(const char *imagename, uint32_t flags) 258c7b1e980SWarner Losh { 259c7b1e980SWarner Losh struct nameidata nd; 260c7b1e980SWarner Losh struct thread *td = curthread; 261c7b1e980SWarner Losh struct ucred *cred = td ? td->td_ucred : NULL; 262c7b1e980SWarner Losh struct sbuf *sb; 263c7b1e980SWarner Losh struct priv_fw *fp; 264c7b1e980SWarner Losh const char *fn; 265c7b1e980SWarner Losh struct vattr vattr; 266c7b1e980SWarner Losh void *data = NULL; 267c7b1e980SWarner Losh const struct firmware *fw; 268c7b1e980SWarner Losh int flags; 269c7b1e980SWarner Losh size_t resid; 270c7b1e980SWarner Losh int error; 271c7b1e980SWarner Losh bool warn = flags & FIRMWARE_GET_NOWARN; 272c7b1e980SWarner Losh 273c7b1e980SWarner Losh /* 274c7b1e980SWarner Losh * XXX TODO: Loop over some path instead of a single element path. 275c7b1e980SWarner Losh * and fetch this path from the 'firmware_path' kenv the loader sets. 276c7b1e980SWarner Losh */ 277c7b1e980SWarner Losh sb = sbuf_new_auto(); 278c7b1e980SWarner Losh sbuf_printf(sb, "%s%s", fw_path, imagename); 279c7b1e980SWarner Losh sbuf_finish(sb); 280c7b1e980SWarner Losh fn = sbuf_data(sb); 281c7b1e980SWarner Losh if (bootverbose) 282c7b1e980SWarner Losh printf("Trying to load binary firmware from %s\n", fn); 283c7b1e980SWarner Losh 284c7b1e980SWarner Losh NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, fn); 285c7b1e980SWarner Losh flags = FREAD; 286c7b1e980SWarner Losh error = vn_open(&nd, &flags, 0, NULL); 287c7b1e980SWarner Losh if (error) 288c7b1e980SWarner Losh goto err; 289c7b1e980SWarner Losh NDFREE_PNBUF(&nd); 290c7b1e980SWarner Losh if (nd.ni_vp->v_type != VREG) 291c7b1e980SWarner Losh goto err2; 292c7b1e980SWarner Losh error = VOP_GETATTR(nd.ni_vp, &vattr, cred); 293c7b1e980SWarner Losh if (error) 294c7b1e980SWarner Losh goto err2; 295c7b1e980SWarner Losh 296c7b1e980SWarner Losh /* 297c7b1e980SWarner Losh * Limit this to something sane, 8MB by default. 298c7b1e980SWarner Losh */ 299c7b1e980SWarner Losh if (vattr.va_size > firmware_max_size) { 300c7b1e980SWarner Losh printf("Firmware %s is too big: %ld bytes, %ld bytes max.\n", 301c7b1e980SWarner Losh fn, vattr.va_size, firmware_max_size); 302c7b1e980SWarner Losh goto err2; 303c7b1e980SWarner Losh } 304c7b1e980SWarner Losh data = malloc(vattr.va_size, M_FIRMWARE, M_WAITOK); 305c7b1e980SWarner Losh error = vn_rdwr(UIO_READ, nd.ni_vp, (caddr_t)data, vattr.va_size, 0, 306c7b1e980SWarner Losh UIO_SYSSPACE, IO_NODELOCKED, cred, NOCRED, &resid, td); 307c7b1e980SWarner Losh /* XXX make data read only? */ 308c7b1e980SWarner Losh VOP_UNLOCK(nd.ni_vp); 309c7b1e980SWarner Losh vn_close(nd.ni_vp, FREAD, cred, td); 310c7b1e980SWarner Losh nd.ni_vp = NULL; 311c7b1e980SWarner Losh if (error != 0 || resid != 0) 312c7b1e980SWarner Losh goto err; 313c7b1e980SWarner Losh fw = firmware_register(fn, data, vattr.va_size, 0, NULL); 314c7b1e980SWarner Losh if (fw == NULL) 315c7b1e980SWarner Losh goto err; 316c7b1e980SWarner Losh fp = PRIV_FW(fw); 317c7b1e980SWarner Losh fp->flags |= FW_BINARY; 318c7b1e980SWarner Losh if (bootverbose) 319c7b1e980SWarner Losh printf("%s: Loaded binary firmware using %s\n", imagename, fn); 320c7b1e980SWarner Losh sbuf_delete(sb); 321c7b1e980SWarner Losh return; 322c7b1e980SWarner Losh 323c7b1e980SWarner Losh err2: /* cleanup in vn_open through vn_close */ 324c7b1e980SWarner Losh VOP_UNLOCK(nd.ni_vp); 325c7b1e980SWarner Losh vn_close(nd.ni_vp, FREAD, cred, td); 326c7b1e980SWarner Losh err: 327c7b1e980SWarner Losh free(data, M_FIRMWARE); 328c7b1e980SWarner Losh if (bootverbose || warn) 329c7b1e980SWarner Losh printf("%s: could not load binary firmware %s either\n", imagename, fn); 330c7b1e980SWarner Losh sbuf_delete(sb); 331c7b1e980SWarner Losh } 332c7b1e980SWarner Losh 3336c6eaea6SSam Leffler static void 3346f65b505SBjoern A. Zeeb loadimage(void *arg, int npending __unused) 3356c6eaea6SSam Leffler { 3366f65b505SBjoern A. Zeeb struct fw_loadimage *fwli = arg; 3376c6eaea6SSam Leffler struct priv_fw *fp; 3386c6eaea6SSam Leffler linker_file_t result; 3396c6eaea6SSam Leffler int error; 3406c6eaea6SSam Leffler 3416f65b505SBjoern A. Zeeb error = linker_reference_module(fwli->imagename, NULL, &result); 3426c6eaea6SSam Leffler if (error != 0) { 3436f65b505SBjoern A. Zeeb if (bootverbose || (fwli->flags & FIRMWARE_GET_NOWARN) == 0) 3446c6eaea6SSam Leffler printf("%s: could not load firmware image, error %d\n", 3456f65b505SBjoern A. Zeeb fwli->imagename, error); 346c7b1e980SWarner Losh try_binary_file(fwli->imagename, fwli->flags); 3474f8ad92fSMark Johnston mtx_lock(&firmware_mtx); 3486c6eaea6SSam Leffler goto done; 3496c6eaea6SSam Leffler } 3506c6eaea6SSam Leffler 3516c6eaea6SSam Leffler mtx_lock(&firmware_mtx); 3526f65b505SBjoern A. Zeeb fp = lookup(fwli->imagename); 3536c6eaea6SSam Leffler if (fp == NULL || fp->file != NULL) { 3546c6eaea6SSam Leffler mtx_unlock(&firmware_mtx); 3556c6eaea6SSam Leffler if (fp == NULL) 3566c6eaea6SSam Leffler printf("%s: firmware image loaded, " 3576f65b505SBjoern A. Zeeb "but did not register\n", fwli->imagename); 3586f65b505SBjoern A. Zeeb (void) linker_release_module(fwli->imagename, NULL, NULL); 3594f8ad92fSMark Johnston mtx_lock(&firmware_mtx); 3606c6eaea6SSam Leffler goto done; 3616c6eaea6SSam Leffler } 3626c6eaea6SSam Leffler fp->file = result; /* record the module identity */ 3636c6eaea6SSam Leffler done: 3646f65b505SBjoern A. Zeeb wakeup_one(arg); 3654f8ad92fSMark Johnston mtx_unlock(&firmware_mtx); 3666c6eaea6SSam Leffler } 3676c6eaea6SSam Leffler 3686aec1278SMax Laier /* 3696aec1278SMax Laier * Lookup and potentially load the specified firmware image. 37033d54970SLuigi Rizzo * If the firmware is not found in the registry, try to load a kernel 37133d54970SLuigi Rizzo * module named as the image name. 37233d54970SLuigi Rizzo * If the firmware is located, a reference is returned. The caller must 37333d54970SLuigi Rizzo * release this reference for the image to be eligible for removal/unload. 3746aec1278SMax Laier */ 37533d54970SLuigi Rizzo const struct firmware * 3766f65b505SBjoern A. Zeeb firmware_get_flags(const char *imagename, uint32_t flags) 3776aec1278SMax Laier { 3786c6eaea6SSam Leffler struct task fwload_task; 3796aec1278SMax Laier struct thread *td; 38033d54970SLuigi Rizzo struct priv_fw *fp; 3816aec1278SMax Laier 3826aec1278SMax Laier mtx_lock(&firmware_mtx); 3834f8ad92fSMark Johnston fp = lookup(imagename); 38433d54970SLuigi Rizzo if (fp != NULL) 38533d54970SLuigi Rizzo goto found; 3866aec1278SMax Laier /* 38733d54970SLuigi Rizzo * Image not present, try to load the module holding it. 3886aec1278SMax Laier */ 3896aec1278SMax Laier td = curthread; 390acd3428bSRobert Watson if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 || 391acd3428bSRobert Watson securelevel_gt(td->td_ucred, 0) != 0) { 3926c6eaea6SSam Leffler mtx_unlock(&firmware_mtx); 3936aec1278SMax Laier printf("%s: insufficient privileges to " 3946aec1278SMax Laier "load firmware image %s\n", __func__, imagename); 3956aec1278SMax Laier return NULL; 3966aec1278SMax Laier } 397450ec4edSIan Dowse /* 3986c6eaea6SSam Leffler * Defer load to a thread with known context. linker_reference_module 3996c6eaea6SSam Leffler * may do filesystem i/o which requires root & current dirs, etc. 4006c6eaea6SSam Leffler * Also we must not hold any mtx's over this call which is problematic. 401450ec4edSIan Dowse */ 402528fb798SAndrew Gallatin if (!cold) { 4036f65b505SBjoern A. Zeeb struct fw_loadimage fwli; 4046f65b505SBjoern A. Zeeb 4056f65b505SBjoern A. Zeeb fwli.imagename = imagename; 4066f65b505SBjoern A. Zeeb fwli.flags = flags; 4076f65b505SBjoern A. Zeeb TASK_INIT(&fwload_task, 0, loadimage, (void *)&fwli); 4086c6eaea6SSam Leffler taskqueue_enqueue(firmware_tq, &fwload_task); 4096f65b505SBjoern A. Zeeb PHOLD(curproc); 4106f65b505SBjoern A. Zeeb msleep((void *)&fwli, &firmware_mtx, 0, "fwload", 0); 4116f65b505SBjoern A. Zeeb PRELE(curproc); 412528fb798SAndrew Gallatin } 4136c6eaea6SSam Leffler /* 4146c6eaea6SSam Leffler * After attempting to load the module, see if the image is registered. 4156c6eaea6SSam Leffler */ 4164f8ad92fSMark Johnston fp = lookup(imagename); 41733d54970SLuigi Rizzo if (fp == NULL) { 4186aec1278SMax Laier mtx_unlock(&firmware_mtx); 41933d54970SLuigi Rizzo return NULL; 42033d54970SLuigi Rizzo } 42133d54970SLuigi Rizzo found: /* common exit point on success */ 422059d10c7SNavdeep Parhar if (fp->refcnt == 0 && fp->parent != NULL) 423059d10c7SNavdeep Parhar fp->parent->refcnt++; 42433d54970SLuigi Rizzo fp->refcnt++; 42533d54970SLuigi Rizzo mtx_unlock(&firmware_mtx); 42633d54970SLuigi Rizzo return &fp->fw; 4276aec1278SMax Laier } 4286aec1278SMax Laier 4296f65b505SBjoern A. Zeeb const struct firmware * 4306f65b505SBjoern A. Zeeb firmware_get(const char *imagename) 4316f65b505SBjoern A. Zeeb { 4326f65b505SBjoern A. Zeeb 4336f65b505SBjoern A. Zeeb return (firmware_get_flags(imagename, 0)); 4346f65b505SBjoern A. Zeeb } 4356f65b505SBjoern A. Zeeb 4366aec1278SMax Laier /* 43733d54970SLuigi Rizzo * Release a reference to a firmware image returned by firmware_get. 43833d54970SLuigi Rizzo * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire 43933d54970SLuigi Rizzo * to release the resource, but the flag is only advisory. 44033d54970SLuigi Rizzo * 44133d54970SLuigi Rizzo * If this is the last reference to the firmware image, and this is an 4426c6eaea6SSam Leffler * autoloaded module, wake up the firmware_unload_task to figure out 4436c6eaea6SSam Leffler * what to do with the associated module. 4446aec1278SMax Laier */ 4456aec1278SMax Laier void 44633d54970SLuigi Rizzo firmware_put(const struct firmware *p, int flags) 4476aec1278SMax Laier { 44833d54970SLuigi Rizzo struct priv_fw *fp = PRIV_FW(p); 44933d54970SLuigi Rizzo 4506aec1278SMax Laier mtx_lock(&firmware_mtx); 4516aec1278SMax Laier fp->refcnt--; 452eb1030c4SIan Dowse if (fp->refcnt == 0) { 453059d10c7SNavdeep Parhar if (fp->parent != NULL) 454059d10c7SNavdeep Parhar fp->parent->refcnt--; 45533d54970SLuigi Rizzo if (flags & FIRMWARE_UNLOAD) 45633d54970SLuigi Rizzo fp->flags |= FW_UNLOAD; 4576aec1278SMax Laier if (fp->file) 4586c6eaea6SSam Leffler taskqueue_enqueue(firmware_tq, &firmware_unload_task); 45933d54970SLuigi Rizzo } 46033d54970SLuigi Rizzo mtx_unlock(&firmware_mtx); 46133d54970SLuigi Rizzo } 46233d54970SLuigi Rizzo 46333d54970SLuigi Rizzo /* 4646c6eaea6SSam Leffler * Setup directory state for the firmware_tq thread so we can do i/o. 4656c6eaea6SSam Leffler */ 4666c6eaea6SSam Leffler static void 4676c6eaea6SSam Leffler set_rootvnode(void *arg, int npending) 4686c6eaea6SSam Leffler { 4696c6eaea6SSam Leffler 4708a08cec1SMateusz Guzik pwd_ensure_dirs(); 47173254c9eSSam Leffler free(arg, M_TEMP); 4726c6eaea6SSam Leffler } 4736c6eaea6SSam Leffler 4746c6eaea6SSam Leffler /* 4756c6eaea6SSam Leffler * Event handler called on mounting of /; bounce a task 4766c6eaea6SSam Leffler * into the task queue thread to setup it's directories. 4776c6eaea6SSam Leffler */ 4786c6eaea6SSam Leffler static void 4796c6eaea6SSam Leffler firmware_mountroot(void *arg) 4806c6eaea6SSam Leffler { 48173254c9eSSam Leffler struct task *setroot_task; 4826c6eaea6SSam Leffler 48373254c9eSSam Leffler setroot_task = malloc(sizeof(struct task), M_TEMP, M_NOWAIT); 48473254c9eSSam Leffler if (setroot_task != NULL) { 48573254c9eSSam Leffler TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task); 48673254c9eSSam Leffler taskqueue_enqueue(firmware_tq, setroot_task); 48773254c9eSSam Leffler } else 48873254c9eSSam Leffler printf("%s: no memory for task!\n", __func__); 4896c6eaea6SSam Leffler } 4906c6eaea6SSam Leffler EVENTHANDLER_DEFINE(mountroot, firmware_mountroot, NULL, 0); 4916c6eaea6SSam Leffler 4926c6eaea6SSam Leffler /* 49333d54970SLuigi Rizzo * The body of the task in charge of unloading autoloaded modules 49433d54970SLuigi Rizzo * that are not needed anymore. 49533d54970SLuigi Rizzo * Images can be cross-linked so we may need to make multiple passes, 49633d54970SLuigi Rizzo * but the time we spend in the loop is bounded because we clear entries 49733d54970SLuigi Rizzo * as we touch them. 49833d54970SLuigi Rizzo */ 49933d54970SLuigi Rizzo static void 50033d54970SLuigi Rizzo unloadentry(void *unused1, int unused2) 50133d54970SLuigi Rizzo { 502c7b1e980SWarner Losh struct priv_fw *fp, *tmp; 50333d54970SLuigi Rizzo 50433d54970SLuigi Rizzo mtx_lock(&firmware_mtx); 5054f8ad92fSMark Johnston restart: 506c7b1e980SWarner Losh LIST_FOREACH_SAFE(fp, &firmware_table, link, tmp) { 507c7b1e980SWarner Losh if (((fp->flags & FW_BINARY) == 0 && fp->file == NULL) || 508c7b1e980SWarner Losh fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0) 50933d54970SLuigi Rizzo continue; 51033d54970SLuigi Rizzo 51133d54970SLuigi Rizzo /* 512c7b1e980SWarner Losh * If we directly loaded the firmware, then we just need to 513c7b1e980SWarner Losh * remove the entry from the list and free the entry and go to 514c7b1e980SWarner Losh * the next one. There's no need for the indirection of the kld 515c7b1e980SWarner Losh * module case, we free memory and go to the next one. 516c7b1e980SWarner Losh */ 517c7b1e980SWarner Losh if ((fp->flags & FW_BINARY) != 0) { 518c7b1e980SWarner Losh LIST_REMOVE(fp, link); 519c7b1e980SWarner Losh free(__DECONST(char *, fp->fw.data), M_FIRMWARE); 520c7b1e980SWarner Losh free(__DECONST(char *, fp->fw.name), M_FIRMWARE); 521c7b1e980SWarner Losh free(fp, M_FIRMWARE); 522c7b1e980SWarner Losh continue; 523c7b1e980SWarner Losh } 524c7b1e980SWarner Losh 525c7b1e980SWarner Losh /* 526c7b1e980SWarner Losh * Found an entry. This is the kld case, so we have a more 527c7b1e980SWarner Losh * complex dance. Now: 5284f8ad92fSMark Johnston * 1. make sure we scan the table again 52933d54970SLuigi Rizzo * 2. clear FW_UNLOAD so we don't try this entry again. 53033d54970SLuigi Rizzo * 3. release the lock while trying to unload the module. 53133d54970SLuigi Rizzo */ 53233d54970SLuigi Rizzo fp->flags &= ~FW_UNLOAD; /* do not try again */ 53333d54970SLuigi Rizzo 53433d54970SLuigi Rizzo /* 53533d54970SLuigi Rizzo * We rely on the module to call firmware_unregister() 5364f8ad92fSMark Johnston * on unload to actually free the entry. 53733d54970SLuigi Rizzo */ 5384f8ad92fSMark Johnston mtx_unlock(&firmware_mtx); 5396776747aSKonstantin Belousov (void)linker_release_module(NULL, NULL, fp->file); 5404f8ad92fSMark Johnston mtx_lock(&firmware_mtx); 54146cac10bSAndrew Gallatin 54246cac10bSAndrew Gallatin /* 54346cac10bSAndrew Gallatin * When we dropped the lock, another thread could have 54446cac10bSAndrew Gallatin * removed an element, so we must restart the scan. 54546cac10bSAndrew Gallatin */ 5464f8ad92fSMark Johnston goto restart; 54733d54970SLuigi Rizzo } 5486aec1278SMax Laier mtx_unlock(&firmware_mtx); 5496aec1278SMax Laier } 5506aec1278SMax Laier 5516aec1278SMax Laier /* 5526aec1278SMax Laier * Module glue. 5536aec1278SMax Laier */ 5546aec1278SMax Laier static int 5556aec1278SMax Laier firmware_modevent(module_t mod, int type, void *unused) 5566aec1278SMax Laier { 55733d54970SLuigi Rizzo struct priv_fw *fp; 5584f8ad92fSMark Johnston int err; 559eb1030c4SIan Dowse 5604f8ad92fSMark Johnston err = 0; 5616aec1278SMax Laier switch (type) { 5626aec1278SMax Laier case MOD_LOAD: 5636c6eaea6SSam Leffler TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL); 5646c6eaea6SSam Leffler firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK, 5656c6eaea6SSam Leffler taskqueue_thread_enqueue, &firmware_tq); 5666c6eaea6SSam Leffler /* NB: use our own loop routine that sets up context */ 5676c6eaea6SSam Leffler (void) taskqueue_start_threads(&firmware_tq, 1, PWAIT, 5686c6eaea6SSam Leffler "firmware taskq"); 5696c6eaea6SSam Leffler if (rootvnode != NULL) { 5706c6eaea6SSam Leffler /* 5716c6eaea6SSam Leffler * Root is already mounted so we won't get an event; 5726c6eaea6SSam Leffler * simulate one here. 5736c6eaea6SSam Leffler */ 5746c6eaea6SSam Leffler firmware_mountroot(NULL); 5756c6eaea6SSam Leffler } 5764f8ad92fSMark Johnston break; 57733d54970SLuigi Rizzo 5786aec1278SMax Laier case MOD_UNLOAD: 57933d54970SLuigi Rizzo /* request all autoloaded modules to be released */ 58033d54970SLuigi Rizzo mtx_lock(&firmware_mtx); 5814f8ad92fSMark Johnston LIST_FOREACH(fp, &firmware_table, link) 582c2ede4b3SMartin Blapp fp->flags |= FW_UNLOAD; 58333d54970SLuigi Rizzo mtx_unlock(&firmware_mtx); 5846c6eaea6SSam Leffler taskqueue_enqueue(firmware_tq, &firmware_unload_task); 5856c6eaea6SSam Leffler taskqueue_drain(firmware_tq, &firmware_unload_task); 5864f8ad92fSMark Johnston 5874f8ad92fSMark Johnston LIST_FOREACH(fp, &firmware_table, link) { 58833d54970SLuigi Rizzo if (fp->fw.name != NULL) { 5894f8ad92fSMark Johnston printf("%s: image %s still active, %d refs\n", 5904f8ad92fSMark Johnston __func__, fp->fw.name, fp->refcnt); 59133d54970SLuigi Rizzo err = EINVAL; 59233d54970SLuigi Rizzo } 59333d54970SLuigi Rizzo } 5946c6eaea6SSam Leffler if (err == 0) 5956c6eaea6SSam Leffler taskqueue_free(firmware_tq); 5964f8ad92fSMark Johnston break; 5974f8ad92fSMark Johnston 5984f8ad92fSMark Johnston default: 5994f8ad92fSMark Johnston err = EOPNOTSUPP; 6004f8ad92fSMark Johnston break; 6016aec1278SMax Laier } 6024f8ad92fSMark Johnston return (err); 6036aec1278SMax Laier } 6046aec1278SMax Laier 6056aec1278SMax Laier static moduledata_t firmware_mod = { 6066aec1278SMax Laier "firmware", 6076aec1278SMax Laier firmware_modevent, 6084592c621SWarner Losh NULL 6096aec1278SMax Laier }; 6106aec1278SMax Laier DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 6116aec1278SMax Laier MODULE_VERSION(firmware, 1); 612