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