1 /*- 2 * Copyright (c) 2005-2008, Sam Leffler <sam@errno.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: src/sys/kern/subr_firmware.c,v 1.13.2.2 2010/02/11 18:34:06 mjacob Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/malloc.h> 32 #include <sys/queue.h> 33 #include <sys/taskqueue.h> 34 #include <sys/systm.h> 35 #include <sys/lock.h> 36 #include <sys/spinlock.h> 37 #include <sys/spinlock2.h> 38 #include <sys/errno.h> 39 #include <sys/linker.h> 40 #include <sys/firmware.h> 41 #include <sys/priv.h> 42 #include <sys/proc.h> 43 #include <sys/module.h> 44 #include <sys/eventhandler.h> 45 46 #include <sys/filedesc.h> 47 #include <sys/vnode.h> 48 49 /* 50 * Loadable firmware support. See sys/sys/firmware.h and firmware(9) 51 * form more details on the subsystem. 52 * 53 * 'struct firmware' is the user-visible part of the firmware table. 54 * Additional internal information is stored in a 'struct priv_fw' 55 * (currently a static array). A slot is in use if FW_INUSE is true: 56 */ 57 58 #define FW_INUSE(p) ((p)->file != NULL || (p)->fw.name != NULL) 59 60 /* 61 * fw.name != NULL when an image is registered; file != NULL for 62 * autoloaded images whose handling has not been completed. 63 * 64 * The state of a slot evolves as follows: 65 * firmware_register --> fw.name = image_name 66 * (autoloaded image) --> file = module reference 67 * firmware_unregister --> fw.name = NULL 68 * (unloadentry complete) --> file = NULL 69 * 70 * In order for the above to work, the 'file' field must remain 71 * unchanged in firmware_unregister(). 72 * 73 * Images residing in the same module are linked to each other 74 * through the 'parent' argument of firmware_register(). 75 * One image (typically, one with the same name as the module to let 76 * the autoloading mechanism work) is considered the parent image for 77 * all other images in the same module. Children affect the refcount 78 * on the parent image preventing improper unloading of the image itself. 79 */ 80 81 struct priv_fw { 82 int refcnt; /* reference count */ 83 84 /* 85 * parent entry, see above. Set on firmware_register(), 86 * cleared on firmware_unregister(). 87 */ 88 struct priv_fw *parent; 89 90 int flags; /* record FIRMWARE_UNLOAD requests */ 91 #define FW_UNLOAD 0x100 92 93 /* 94 * 'file' is private info managed by the autoload/unload code. 95 * Set at the end of firmware_get(), cleared only in the 96 * firmware_unload_task, so the latter can depend on its value even 97 * while the lock is not held. 98 */ 99 linker_file_t file; /* module file, if autoloaded */ 100 101 /* 102 * 'fw' is the externally visible image information. 103 * We do not make it the first field in priv_fw, to avoid the 104 * temptation of casting pointers to each other. 105 * Use PRIV_FW(fw) to get a pointer to the cointainer of fw. 106 * Beware, PRIV_FW does not work for a NULL pointer. 107 */ 108 struct firmware fw; /* externally visible information */ 109 }; 110 111 /* 112 * PRIV_FW returns the pointer to the container of struct firmware *x. 113 * Cast to intptr_t to override the 'const' attribute of x 114 */ 115 #define PRIV_FW(x) ((struct priv_fw *) \ 116 ((intptr_t)(x) - offsetof(struct priv_fw, fw)) ) 117 118 /* 119 * At the moment we use a static array as backing store for the registry. 120 * Should we move to a dynamic structure, keep in mind that we cannot 121 * reallocate the array because pointers are held externally. 122 * A list may work, though. 123 */ 124 #define FIRMWARE_MAX 30 125 static struct priv_fw firmware_table[FIRMWARE_MAX]; 126 127 /* 128 * Firmware module operations are handled in a separate task as they 129 * might sleep and they require directory context to do i/o. 130 */ 131 static struct taskqueue *firmware_tq; 132 static struct task firmware_unload_task; 133 134 /* 135 * This lock protects accesses to the firmware table. 136 */ 137 static struct lock firmware_lock; 138 139 /* 140 * Helper function to lookup a name. 141 * As a side effect, it sets the pointer to a free slot, if any. 142 * This way we can concentrate most of the registry scanning in 143 * this function, which makes it easier to replace the registry 144 * with some other data structure. 145 */ 146 static struct priv_fw * 147 lookup(const char *name, struct priv_fw **empty_slot) 148 { 149 struct priv_fw *fp = NULL; 150 struct priv_fw *dummy; 151 int i; 152 153 if (empty_slot == NULL) 154 empty_slot = &dummy; 155 *empty_slot = NULL; 156 for (i = 0; i < FIRMWARE_MAX; i++) { 157 fp = &firmware_table[i]; 158 if (fp->fw.name != NULL && strcasecmp(name, fp->fw.name) == 0) 159 break; 160 else if (!FW_INUSE(fp)) 161 *empty_slot = fp; 162 } 163 return (i < FIRMWARE_MAX ) ? fp : NULL; 164 } 165 166 /* 167 * Register a firmware image with the specified name. The 168 * image name must not already be registered. If this is a 169 * subimage then parent refers to a previously registered 170 * image that this should be associated with. 171 */ 172 const struct firmware * 173 firmware_register(const char *imagename, const void *data, size_t datasize, 174 unsigned int version, const struct firmware *parent) 175 { 176 struct priv_fw *match, *frp; 177 178 lockmgr(&firmware_lock, LK_EXCLUSIVE); 179 /* 180 * Do a lookup to make sure the name is unique or find a free slot. 181 */ 182 match = lookup(imagename, &frp); 183 if (match != NULL) { 184 lockmgr(&firmware_lock, LK_RELEASE); 185 kprintf("%s: image %s already registered!\n", 186 __func__, imagename); 187 return NULL; 188 } 189 if (frp == NULL) { 190 lockmgr(&firmware_lock, LK_RELEASE); 191 kprintf("%s: cannot register image %s, firmware table full!\n", 192 __func__, imagename); 193 return NULL; 194 } 195 bzero(frp, sizeof(*frp)); /* start from a clean record */ 196 frp->fw.name = imagename; 197 frp->fw.data = data; 198 frp->fw.datasize = datasize; 199 frp->fw.version = version; 200 if (parent != NULL) { 201 frp->parent = PRIV_FW(parent); 202 frp->parent->refcnt++; 203 } 204 lockmgr(&firmware_lock, LK_RELEASE); 205 if (bootverbose) 206 kprintf("firmware: '%s' version %u: %zu bytes loaded at %p\n", 207 imagename, version, datasize, data); 208 return &frp->fw; 209 } 210 211 /* 212 * Unregister/remove a firmware image. If there are outstanding 213 * references an error is returned and the image is not removed 214 * from the registry. 215 */ 216 int 217 firmware_unregister(const char *imagename) 218 { 219 struct priv_fw *fp; 220 int err; 221 222 lockmgr(&firmware_lock, LK_EXCLUSIVE); 223 fp = lookup(imagename, NULL); 224 if (fp == NULL) { 225 /* 226 * It is ok for the lookup to fail; this can happen 227 * when a module is unloaded on last reference and the 228 * module unload handler unregister's each of it's 229 * firmware images. 230 */ 231 err = 0; 232 } else if (fp->refcnt != 0) { /* cannot unregister */ 233 err = EBUSY; 234 } else { 235 linker_file_t x = fp->file; /* save value */ 236 237 if (fp->parent != NULL) /* release parent reference */ 238 fp->parent->refcnt--; 239 /* 240 * Clear the whole entry with bzero to make sure we 241 * do not forget anything. Then restore 'file' which is 242 * non-null for autoloaded images. 243 */ 244 bzero(fp, sizeof(struct priv_fw)); 245 fp->file = x; 246 err = 0; 247 } 248 lockmgr(&firmware_lock, LK_RELEASE); 249 return err; 250 } 251 252 static void 253 loadimage(void *arg, int npending) 254 { 255 #ifdef notyet 256 struct thread *td = curthread; 257 #endif 258 char *imagename = arg; 259 struct priv_fw *fp; 260 linker_file_t result; 261 int error; 262 263 /* synchronize with the thread that dispatched us */ 264 lockmgr(&firmware_lock, LK_EXCLUSIVE); 265 lockmgr(&firmware_lock, LK_RELEASE); 266 267 /* JAT 268 if (td->td_proc->p_fd->fd_rdir == NULL) { 269 kprintf("%s: root not mounted yet, no way to load image\n", 270 imagename); 271 goto done; 272 } 273 */ 274 error = linker_reference_module(imagename, NULL, &result); 275 if (error != 0) { 276 kprintf("%s: could not load firmware image, error %d\n", 277 imagename, error); 278 goto done; 279 } 280 281 lockmgr(&firmware_lock, LK_EXCLUSIVE); 282 fp = lookup(imagename, NULL); 283 if (fp == NULL || fp->file != NULL) { 284 lockmgr(&firmware_lock, LK_RELEASE); 285 if (fp == NULL) 286 kprintf("%s: firmware image loaded, " 287 "but did not register\n", imagename); 288 (void) linker_release_module(imagename, NULL, NULL); 289 goto done; 290 } 291 fp->file = result; /* record the module identity */ 292 lockmgr(&firmware_lock, LK_RELEASE); 293 done: 294 wakeup_one(imagename); /* we're done */ 295 } 296 297 /* 298 * Lookup and potentially load the specified firmware image. 299 * If the firmware is not found in the registry, try to load a kernel 300 * module named as the image name. 301 * If the firmware is located, a reference is returned. The caller must 302 * release this reference for the image to be eligible for removal/unload. 303 */ 304 const struct firmware * 305 firmware_get(const char *imagename) 306 { 307 struct task fwload_task; 308 struct thread *td; 309 struct priv_fw *fp; 310 311 lockmgr(&firmware_lock, LK_EXCLUSIVE); 312 fp = lookup(imagename, NULL); 313 if (fp != NULL) 314 goto found; 315 /* 316 * Image not present, try to load the module holding it. 317 */ 318 td = curthread; 319 if (priv_check(td, PRIV_FIRMWARE_LOAD) != 0 || securelevel > 0) { 320 lockmgr(&firmware_lock, LK_RELEASE); 321 kprintf("%s: insufficient privileges to " 322 "load firmware image %s\n", __func__, imagename); 323 return NULL; 324 } 325 /* 326 * Defer load to a thread with known context. linker_reference_module 327 * may do filesystem i/o which requires root & current dirs, etc. 328 * Also we must not hold any lock's over this call which is problematic. 329 */ 330 if (!cold) { 331 TASK_INIT(&fwload_task, 0, loadimage, __DECONST(void *, 332 imagename)); 333 taskqueue_enqueue(firmware_tq, &fwload_task); 334 lksleep(__DECONST(void *, imagename), &firmware_lock, 0, 335 "fwload", 0); 336 } 337 /* 338 * After attempting to load the module, see if the image is registered. 339 */ 340 fp = lookup(imagename, NULL); 341 if (fp == NULL) { 342 lockmgr(&firmware_lock, LK_RELEASE); 343 return NULL; 344 } 345 found: /* common exit point on success */ 346 fp->refcnt++; 347 lockmgr(&firmware_lock, LK_RELEASE); 348 return &fp->fw; 349 } 350 351 /* 352 * Release a reference to a firmware image returned by firmware_get. 353 * The caller may specify, with the FIRMWARE_UNLOAD flag, its desire 354 * to release the resource, but the flag is only advisory. 355 * 356 * If this is the last reference to the firmware image, and this is an 357 * autoloaded module, wake up the firmware_unload_task to figure out 358 * what to do with the associated module. 359 */ 360 void 361 firmware_put(const struct firmware *p, int flags) 362 { 363 struct priv_fw *fp = PRIV_FW(p); 364 365 lockmgr(&firmware_lock, LK_EXCLUSIVE); 366 fp->refcnt--; 367 if (fp->refcnt == 0) { 368 if (flags & FIRMWARE_UNLOAD) 369 fp->flags |= FW_UNLOAD; 370 if (fp->file) 371 taskqueue_enqueue(firmware_tq, &firmware_unload_task); 372 } 373 lockmgr(&firmware_lock, LK_RELEASE); 374 } 375 376 #ifdef notyet 377 /* 378 * Setup directory state for the firmware_tq thread so we can do i/o. 379 */ 380 static void 381 set_rootvnode(void *arg, int npending) 382 { 383 struct thread *td = curthread; 384 struct proc *p = td->td_proc; 385 386 387 #if 0 388 spin_lock_wr(&p->p_fd->fd_spin); 389 if (p->p_fd->fd_cdir == NULL) { 390 p->p_fd->fd_cdir = rootvnode; 391 vref(rootvnode); 392 } 393 if (p->p_fd->fd_rdir == NULL) { 394 p->p_fd->fd_rdir = rootvnode; 395 vref(rootvnode); 396 } 397 spin_unlock_wr(&p->p_fd->fd_spin); 398 399 kfree(arg, M_TEMP); 400 #endif 401 } 402 403 /* 404 * Event handler called on mounting of /; bounce a task 405 * into the task queue thread to setup it's directories. 406 */ 407 static void 408 firmware_mountroot(void *arg) 409 { 410 struct task *setroot_task; 411 412 setroot_task = kmalloc(sizeof(struct task), M_TEMP, M_NOWAIT); 413 if (setroot_task != NULL) { 414 TASK_INIT(setroot_task, 0, set_rootvnode, setroot_task); 415 taskqueue_enqueue(firmware_tq, setroot_task); 416 } else 417 kprintf("%s: no memory for task!\n", __func__); 418 } 419 EVENTHANDLER_DECLARE(mountroot, firmware_mountroot); 420 #endif 421 422 /* 423 * The body of the task in charge of unloading autoloaded modules 424 * that are not needed anymore. 425 * Images can be cross-linked so we may need to make multiple passes, 426 * but the time we spend in the loop is bounded because we clear entries 427 * as we touch them. 428 */ 429 static void 430 unloadentry(void *unused1, int unused2) 431 { 432 int limit = FIRMWARE_MAX; 433 int i; /* current cycle */ 434 435 lockmgr(&firmware_lock, LK_EXCLUSIVE); 436 /* 437 * Scan the table. limit is set to make sure we make another 438 * full sweep after matching an entry that requires unloading. 439 */ 440 for (i = 0; i < limit; i++) { 441 struct priv_fw *fp; 442 int err; 443 444 fp = &firmware_table[i % FIRMWARE_MAX]; 445 if (fp->fw.name == NULL || fp->file == NULL || 446 fp->refcnt != 0 || (fp->flags & FW_UNLOAD) == 0) 447 continue; 448 449 /* 450 * Found an entry. Now: 451 * 1. bump up limit to make sure we make another full round; 452 * 2. clear FW_UNLOAD so we don't try this entry again. 453 * 3. release the lock while trying to unload the module. 454 * 'file' remains set so that the entry cannot be reused 455 * in the meantime (it also means that fp->file will 456 * not change while we release the lock). 457 */ 458 limit = i + FIRMWARE_MAX; /* make another full round */ 459 fp->flags &= ~FW_UNLOAD; /* do not try again */ 460 461 lockmgr(&firmware_lock, LK_RELEASE); 462 err = linker_release_module(NULL, NULL, fp->file); 463 lockmgr(&firmware_lock, LK_EXCLUSIVE); 464 465 /* 466 * We rely on the module to call firmware_unregister() 467 * on unload to actually release the entry. 468 * If err = 0 we can drop our reference as the system 469 * accepted it. Otherwise unloading failed (e.g. the 470 * module itself gave an error) so our reference is 471 * still valid. 472 */ 473 if (err == 0) 474 fp->file = NULL; 475 } 476 lockmgr(&firmware_lock, LK_RELEASE); 477 } 478 479 /* 480 * Module glue. 481 */ 482 static int 483 firmware_modevent(module_t mod, int type, void *unused) 484 { 485 struct priv_fw *fp; 486 int i, err; 487 488 switch (type) { 489 case MOD_LOAD: 490 TASK_INIT(&firmware_unload_task, 0, unloadentry, NULL); 491 lockinit(&firmware_lock, "firmware table", 0, LK_CANRECURSE); 492 firmware_tq = taskqueue_create("taskqueue_firmware", M_WAITOK, 493 taskqueue_thread_enqueue, &firmware_tq); 494 /* NB: use our own loop routine that sets up context */ 495 (void) taskqueue_start_threads(&firmware_tq, 1, TDPRI_KERN_DAEMON, 496 -1, "firmware taskq"); 497 if (rootvnode != NULL) { 498 /* 499 * Root is already mounted so we won't get an event; 500 * simulate one here. 501 */ 502 #ifdef notyet 503 firmware_mountroot(NULL); 504 #endif 505 } 506 return 0; 507 508 case MOD_UNLOAD: 509 /* request all autoloaded modules to be released */ 510 lockmgr(&firmware_lock, LK_EXCLUSIVE); 511 for (i = 0; i < FIRMWARE_MAX; i++) { 512 fp = &firmware_table[i]; 513 fp->flags |= FW_UNLOAD; 514 } 515 lockmgr(&firmware_lock, LK_RELEASE); 516 taskqueue_enqueue(firmware_tq, &firmware_unload_task); 517 taskqueue_drain(firmware_tq, &firmware_unload_task); 518 err = 0; 519 for (i = 0; i < FIRMWARE_MAX; i++) { 520 fp = &firmware_table[i]; 521 if (fp->fw.name != NULL) { 522 kprintf("%s: image %p ref %d still active slot %d\n", 523 __func__, fp->fw.name, 524 fp->refcnt, i); 525 err = EINVAL; 526 } 527 } 528 if (err == 0) 529 taskqueue_free(firmware_tq); 530 return err; 531 } 532 return EINVAL; 533 } 534 535 static moduledata_t firmware_mod = { 536 "firmware", 537 firmware_modevent, 538 NULL 539 }; 540 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); 541 MODULE_VERSION(firmware, 1); 542