-
Notifications
You must be signed in to change notification settings - Fork 272
/
subr_autoconf.c
3533 lines (3010 loc) · 82.3 KB
/
subr_autoconf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: subr_autoconf.c,v 1.290 2021/10/11 10:59:09 jmcneill Exp $ */
/*
* Copyright (c) 1996, 2000 Christopher G. Demetriou
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the
* NetBSD Project. See http://www.NetBSD.org/ for
* information about NetBSD.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
*/
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratories.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* from: Header: subr_autoconf.c,v 1.12 93/02/01 19:31:48 torek Exp (LBL)
*
* @(#)subr_autoconf.c 8.3 (Berkeley) 5/17/94
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.290 2021/10/11 10:59:09 jmcneill Exp $");
#ifdef _KERNEL_OPT
#include "opt_ddb.h"
#include "drvctl.h"
#endif
#include <sys/param.h>
#include <sys/device.h>
#include <sys/disklabel.h>
#include <sys/conf.h>
#include <sys/kauth.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/proc.h>
#include <sys/reboot.h>
#include <sys/kthread.h>
#include <sys/buf.h>
#include <sys/dirent.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/unistd.h>
#include <sys/fcntl.h>
#include <sys/lockf.h>
#include <sys/callout.h>
#include <sys/devmon.h>
#include <sys/cpu.h>
#include <sys/sysctl.h>
#include <sys/stdarg.h>
#include <sys/disk.h>
#include <sys/rndsource.h>
#include <machine/limits.h>
/*
* Autoconfiguration subroutines.
*/
/*
* Device autoconfiguration timings are mixed into the entropy pool.
*/
static krndsource_t rnd_autoconf_source;
/*
* ioconf.c exports exactly two names: cfdata and cfroots. All system
* devices and drivers are found via these tables.
*/
extern struct cfdata cfdata[];
extern const short cfroots[];
/*
* List of all cfdriver structures. We use this to detect duplicates
* when other cfdrivers are loaded.
*/
struct cfdriverlist allcfdrivers = LIST_HEAD_INITIALIZER(&allcfdrivers);
extern struct cfdriver * const cfdriver_list_initial[];
/*
* Initial list of cfattach's.
*/
extern const struct cfattachinit cfattachinit[];
/*
* List of cfdata tables. We always have one such list -- the one
* built statically when the kernel was configured.
*/
struct cftablelist allcftables = TAILQ_HEAD_INITIALIZER(allcftables);
static struct cftable initcftable;
#define ROOT ((device_t)NULL)
struct matchinfo {
cfsubmatch_t fn;
device_t parent;
const int *locs;
void *aux;
struct cfdata *match;
int pri;
};
struct alldevs_foray {
int af_s;
struct devicelist af_garbage;
};
/*
* Internal version of the cfargs structure; all versions are
* canonicalized to this.
*/
struct cfargs_internal {
union {
cfsubmatch_t submatch;/* submatch function (direct config) */
cfsearch_t search; /* search function (indirect config) */
};
const char * iattr; /* interface attribute */
const int * locators; /* locators array */
devhandle_t devhandle; /* devhandle_t (by value) */
};
static char *number(char *, int);
static void mapply(struct matchinfo *, cfdata_t);
static void config_devdelete(device_t);
static void config_devunlink(device_t, struct devicelist *);
static void config_makeroom(int, struct cfdriver *);
static void config_devlink(device_t);
static void config_alldevs_enter(struct alldevs_foray *);
static void config_alldevs_exit(struct alldevs_foray *);
static void config_add_attrib_dict(device_t);
static device_t config_attach_internal(device_t, cfdata_t, void *,
cfprint_t, const struct cfargs_internal *);
static void config_collect_garbage(struct devicelist *);
static void config_dump_garbage(struct devicelist *);
static void pmflock_debug(device_t, const char *, int);
static device_t deviter_next1(deviter_t *);
static void deviter_reinit(deviter_t *);
struct deferred_config {
TAILQ_ENTRY(deferred_config) dc_queue;
device_t dc_dev;
void (*dc_func)(device_t);
};
TAILQ_HEAD(deferred_config_head, deferred_config);
static struct deferred_config_head deferred_config_queue =
TAILQ_HEAD_INITIALIZER(deferred_config_queue);
static struct deferred_config_head interrupt_config_queue =
TAILQ_HEAD_INITIALIZER(interrupt_config_queue);
static int interrupt_config_threads = 8;
static struct deferred_config_head mountroot_config_queue =
TAILQ_HEAD_INITIALIZER(mountroot_config_queue);
static int mountroot_config_threads = 2;
static lwp_t **mountroot_config_lwpids;
static size_t mountroot_config_lwpids_size;
bool root_is_mounted = false;
static void config_process_deferred(struct deferred_config_head *, device_t);
/* Hooks to finalize configuration once all real devices have been found. */
struct finalize_hook {
TAILQ_ENTRY(finalize_hook) f_list;
int (*f_func)(device_t);
device_t f_dev;
};
static TAILQ_HEAD(, finalize_hook) config_finalize_list =
TAILQ_HEAD_INITIALIZER(config_finalize_list);
static int config_finalize_done;
/* list of all devices */
static struct devicelist alldevs = TAILQ_HEAD_INITIALIZER(alldevs);
static kmutex_t alldevs_lock __cacheline_aligned;
static devgen_t alldevs_gen = 1;
static int alldevs_nread = 0;
static int alldevs_nwrite = 0;
static bool alldevs_garbage = false;
static struct devicelist config_pending =
TAILQ_HEAD_INITIALIZER(config_pending);
static kmutex_t config_misc_lock;
static kcondvar_t config_misc_cv;
static bool detachall = false;
#define STREQ(s1, s2) \
(*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
static bool config_initialized = false; /* config_init() has been called. */
static int config_do_twiddle;
static callout_t config_twiddle_ch;
static void sysctl_detach_setup(struct sysctllog **);
int no_devmon_insert(const char *, prop_dictionary_t);
int (*devmon_insert_vec)(const char *, prop_dictionary_t) = no_devmon_insert;
typedef int (*cfdriver_fn)(struct cfdriver *);
static int
frob_cfdrivervec(struct cfdriver * const *cfdriverv,
cfdriver_fn drv_do, cfdriver_fn drv_undo,
const char *style, bool dopanic)
{
void (*pr)(const char *, ...) __printflike(1, 2) =
dopanic ? panic : printf;
int i, error = 0, e2 __diagused;
for (i = 0; cfdriverv[i] != NULL; i++) {
if ((error = drv_do(cfdriverv[i])) != 0) {
pr("configure: `%s' driver %s failed: %d",
cfdriverv[i]->cd_name, style, error);
goto bad;
}
}
KASSERT(error == 0);
return 0;
bad:
printf("\n");
for (i--; i >= 0; i--) {
e2 = drv_undo(cfdriverv[i]);
KASSERT(e2 == 0);
}
return error;
}
typedef int (*cfattach_fn)(const char *, struct cfattach *);
static int
frob_cfattachvec(const struct cfattachinit *cfattachv,
cfattach_fn att_do, cfattach_fn att_undo,
const char *style, bool dopanic)
{
const struct cfattachinit *cfai = NULL;
void (*pr)(const char *, ...) __printflike(1, 2) =
dopanic ? panic : printf;
int j = 0, error = 0, e2 __diagused;
for (cfai = &cfattachv[0]; cfai->cfai_name != NULL; cfai++) {
for (j = 0; cfai->cfai_list[j] != NULL; j++) {
if ((error = att_do(cfai->cfai_name,
cfai->cfai_list[j])) != 0) {
pr("configure: attachment `%s' "
"of `%s' driver %s failed: %d",
cfai->cfai_list[j]->ca_name,
cfai->cfai_name, style, error);
goto bad;
}
}
}
KASSERT(error == 0);
return 0;
bad:
/*
* Rollback in reverse order. dunno if super-important, but
* do that anyway. Although the code looks a little like
* someone did a little integration (in the math sense).
*/
printf("\n");
if (cfai) {
bool last;
for (last = false; last == false; ) {
if (cfai == &cfattachv[0])
last = true;
for (j--; j >= 0; j--) {
e2 = att_undo(cfai->cfai_name,
cfai->cfai_list[j]);
KASSERT(e2 == 0);
}
if (!last) {
cfai--;
for (j = 0; cfai->cfai_list[j] != NULL; j++)
;
}
}
}
return error;
}
/*
* Initialize the autoconfiguration data structures. Normally this
* is done by configure(), but some platforms need to do this very
* early (to e.g. initialize the console).
*/
void
config_init(void)
{
KASSERT(config_initialized == false);
mutex_init(&alldevs_lock, MUTEX_DEFAULT, IPL_VM);
mutex_init(&config_misc_lock, MUTEX_DEFAULT, IPL_NONE);
cv_init(&config_misc_cv, "cfgmisc");
callout_init(&config_twiddle_ch, CALLOUT_MPSAFE);
frob_cfdrivervec(cfdriver_list_initial,
config_cfdriver_attach, NULL, "bootstrap", true);
frob_cfattachvec(cfattachinit,
config_cfattach_attach, NULL, "bootstrap", true);
initcftable.ct_cfdata = cfdata;
TAILQ_INSERT_TAIL(&allcftables, &initcftable, ct_list);
rnd_attach_source(&rnd_autoconf_source, "autoconf", RND_TYPE_UNKNOWN,
RND_FLAG_COLLECT_TIME);
config_initialized = true;
}
/*
* Init or fini drivers and attachments. Either all or none
* are processed (via rollback). It would be nice if this were
* atomic to outside consumers, but with the current state of
* locking ...
*/
int
config_init_component(struct cfdriver * const *cfdriverv,
const struct cfattachinit *cfattachv, struct cfdata *cfdatav)
{
int error;
KERNEL_LOCK(1, NULL);
if ((error = frob_cfdrivervec(cfdriverv,
config_cfdriver_attach, config_cfdriver_detach, "init", false))!= 0)
goto out;
if ((error = frob_cfattachvec(cfattachv,
config_cfattach_attach, config_cfattach_detach,
"init", false)) != 0) {
frob_cfdrivervec(cfdriverv,
config_cfdriver_detach, NULL, "init rollback", true);
goto out;
}
if ((error = config_cfdata_attach(cfdatav, 1)) != 0) {
frob_cfattachvec(cfattachv,
config_cfattach_detach, NULL, "init rollback", true);
frob_cfdrivervec(cfdriverv,
config_cfdriver_detach, NULL, "init rollback", true);
goto out;
}
/* Success! */
error = 0;
out: KERNEL_UNLOCK_ONE(NULL);
return error;
}
int
config_fini_component(struct cfdriver * const *cfdriverv,
const struct cfattachinit *cfattachv, struct cfdata *cfdatav)
{
int error;
KERNEL_LOCK(1, NULL);
if ((error = config_cfdata_detach(cfdatav)) != 0)
goto out;
if ((error = frob_cfattachvec(cfattachv,
config_cfattach_detach, config_cfattach_attach,
"fini", false)) != 0) {
if (config_cfdata_attach(cfdatav, 0) != 0)
panic("config_cfdata fini rollback failed");
goto out;
}
if ((error = frob_cfdrivervec(cfdriverv,
config_cfdriver_detach, config_cfdriver_attach,
"fini", false)) != 0) {
frob_cfattachvec(cfattachv,
config_cfattach_attach, NULL, "fini rollback", true);
if (config_cfdata_attach(cfdatav, 0) != 0)
panic("config_cfdata fini rollback failed");
goto out;
}
/* Success! */
error = 0;
out: KERNEL_UNLOCK_ONE(NULL);
return error;
}
void
config_init_mi(void)
{
if (!config_initialized)
config_init();
sysctl_detach_setup(NULL);
}
void
config_deferred(device_t dev)
{
KASSERT(KERNEL_LOCKED_P());
config_process_deferred(&deferred_config_queue, dev);
config_process_deferred(&interrupt_config_queue, dev);
config_process_deferred(&mountroot_config_queue, dev);
}
static void
config_interrupts_thread(void *cookie)
{
struct deferred_config *dc;
device_t dev;
mutex_enter(&config_misc_lock);
while ((dc = TAILQ_FIRST(&interrupt_config_queue)) != NULL) {
TAILQ_REMOVE(&interrupt_config_queue, dc, dc_queue);
mutex_exit(&config_misc_lock);
dev = dc->dc_dev;
(*dc->dc_func)(dev);
if (!device_pmf_is_registered(dev))
aprint_debug_dev(dev,
"WARNING: power management not supported\n");
config_pending_decr(dev);
kmem_free(dc, sizeof(*dc));
mutex_enter(&config_misc_lock);
}
mutex_exit(&config_misc_lock);
kthread_exit(0);
}
void
config_create_interruptthreads(void)
{
int i;
for (i = 0; i < interrupt_config_threads; i++) {
(void)kthread_create(PRI_NONE, 0/*XXXSMP */, NULL,
config_interrupts_thread, NULL, NULL, "configintr");
}
}
static void
config_mountroot_thread(void *cookie)
{
struct deferred_config *dc;
mutex_enter(&config_misc_lock);
while ((dc = TAILQ_FIRST(&mountroot_config_queue)) != NULL) {
TAILQ_REMOVE(&mountroot_config_queue, dc, dc_queue);
mutex_exit(&config_misc_lock);
(*dc->dc_func)(dc->dc_dev);
kmem_free(dc, sizeof(*dc));
mutex_enter(&config_misc_lock);
}
mutex_exit(&config_misc_lock);
kthread_exit(0);
}
void
config_create_mountrootthreads(void)
{
int i;
if (!root_is_mounted)
root_is_mounted = true;
mountroot_config_lwpids_size = sizeof(mountroot_config_lwpids) *
mountroot_config_threads;
mountroot_config_lwpids = kmem_alloc(mountroot_config_lwpids_size,
KM_NOSLEEP);
KASSERT(mountroot_config_lwpids);
for (i = 0; i < mountroot_config_threads; i++) {
mountroot_config_lwpids[i] = 0;
(void)kthread_create(PRI_NONE, KTHREAD_MUSTJOIN/* XXXSMP */,
NULL, config_mountroot_thread, NULL,
&mountroot_config_lwpids[i],
"configroot");
}
}
void
config_finalize_mountroot(void)
{
int i, error;
for (i = 0; i < mountroot_config_threads; i++) {
if (mountroot_config_lwpids[i] == 0)
continue;
error = kthread_join(mountroot_config_lwpids[i]);
if (error)
printf("%s: thread %x joined with error %d\n",
__func__, i, error);
}
kmem_free(mountroot_config_lwpids, mountroot_config_lwpids_size);
}
/*
* Announce device attach/detach to userland listeners.
*/
int
no_devmon_insert(const char *name, prop_dictionary_t p)
{
return ENODEV;
}
static void
devmon_report_device(device_t dev, bool isattach)
{
prop_dictionary_t ev, dict = device_properties(dev);
const char *parent;
const char *what;
const char *where;
device_t pdev = device_parent(dev);
/* If currently no drvctl device, just return */
if (devmon_insert_vec == no_devmon_insert)
return;
ev = prop_dictionary_create();
if (ev == NULL)
return;
what = (isattach ? "device-attach" : "device-detach");
parent = (pdev == NULL ? "root" : device_xname(pdev));
if (prop_dictionary_get_string(dict, "location", &where)) {
prop_dictionary_set_string(ev, "location", where);
aprint_debug("ev: %s %s at %s in [%s]\n",
what, device_xname(dev), parent, where);
}
if (!prop_dictionary_set_string(ev, "device", device_xname(dev)) ||
!prop_dictionary_set_string(ev, "parent", parent)) {
prop_object_release(ev);
return;
}
if ((*devmon_insert_vec)(what, ev) != 0)
prop_object_release(ev);
}
/*
* Add a cfdriver to the system.
*/
int
config_cfdriver_attach(struct cfdriver *cd)
{
struct cfdriver *lcd;
/* Make sure this driver isn't already in the system. */
LIST_FOREACH(lcd, &allcfdrivers, cd_list) {
if (STREQ(lcd->cd_name, cd->cd_name))
return EEXIST;
}
LIST_INIT(&cd->cd_attach);
LIST_INSERT_HEAD(&allcfdrivers, cd, cd_list);
return 0;
}
/*
* Remove a cfdriver from the system.
*/
int
config_cfdriver_detach(struct cfdriver *cd)
{
struct alldevs_foray af;
int i, rc = 0;
config_alldevs_enter(&af);
/* Make sure there are no active instances. */
for (i = 0; i < cd->cd_ndevs; i++) {
if (cd->cd_devs[i] != NULL) {
rc = EBUSY;
break;
}
}
config_alldevs_exit(&af);
if (rc != 0)
return rc;
/* ...and no attachments loaded. */
if (LIST_EMPTY(&cd->cd_attach) == 0)
return EBUSY;
LIST_REMOVE(cd, cd_list);
KASSERT(cd->cd_devs == NULL);
return 0;
}
/*
* Look up a cfdriver by name.
*/
struct cfdriver *
config_cfdriver_lookup(const char *name)
{
struct cfdriver *cd;
LIST_FOREACH(cd, &allcfdrivers, cd_list) {
if (STREQ(cd->cd_name, name))
return cd;
}
return NULL;
}
/*
* Add a cfattach to the specified driver.
*/
int
config_cfattach_attach(const char *driver, struct cfattach *ca)
{
struct cfattach *lca;
struct cfdriver *cd;
cd = config_cfdriver_lookup(driver);
if (cd == NULL)
return ESRCH;
/* Make sure this attachment isn't already on this driver. */
LIST_FOREACH(lca, &cd->cd_attach, ca_list) {
if (STREQ(lca->ca_name, ca->ca_name))
return EEXIST;
}
LIST_INSERT_HEAD(&cd->cd_attach, ca, ca_list);
return 0;
}
/*
* Remove a cfattach from the specified driver.
*/
int
config_cfattach_detach(const char *driver, struct cfattach *ca)
{
struct alldevs_foray af;
struct cfdriver *cd;
device_t dev;
int i, rc = 0;
cd = config_cfdriver_lookup(driver);
if (cd == NULL)
return ESRCH;
config_alldevs_enter(&af);
/* Make sure there are no active instances. */
for (i = 0; i < cd->cd_ndevs; i++) {
if ((dev = cd->cd_devs[i]) == NULL)
continue;
if (dev->dv_cfattach == ca) {
rc = EBUSY;
break;
}
}
config_alldevs_exit(&af);
if (rc != 0)
return rc;
LIST_REMOVE(ca, ca_list);
return 0;
}
/*
* Look up a cfattach by name.
*/
static struct cfattach *
config_cfattach_lookup_cd(struct cfdriver *cd, const char *atname)
{
struct cfattach *ca;
LIST_FOREACH(ca, &cd->cd_attach, ca_list) {
if (STREQ(ca->ca_name, atname))
return ca;
}
return NULL;
}
/*
* Look up a cfattach by driver/attachment name.
*/
struct cfattach *
config_cfattach_lookup(const char *name, const char *atname)
{
struct cfdriver *cd;
cd = config_cfdriver_lookup(name);
if (cd == NULL)
return NULL;
return config_cfattach_lookup_cd(cd, atname);
}
/*
* Apply the matching function and choose the best. This is used
* a few times and we want to keep the code small.
*/
static void
mapply(struct matchinfo *m, cfdata_t cf)
{
int pri;
if (m->fn != NULL) {
pri = (*m->fn)(m->parent, cf, m->locs, m->aux);
} else {
pri = config_match(m->parent, cf, m->aux);
}
if (pri > m->pri) {
m->match = cf;
m->pri = pri;
}
}
int
config_stdsubmatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
{
const struct cfiattrdata *ci;
const struct cflocdesc *cl;
int nlocs, i;
ci = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver);
KASSERT(ci);
nlocs = ci->ci_loclen;
KASSERT(!nlocs || locs);
for (i = 0; i < nlocs; i++) {
cl = &ci->ci_locdesc[i];
if (cl->cld_defaultstr != NULL &&
cf->cf_loc[i] == cl->cld_default)
continue;
if (cf->cf_loc[i] == locs[i])
continue;
return 0;
}
return config_match(parent, cf, aux);
}
/*
* Helper function: check whether the driver supports the interface attribute
* and return its descriptor structure.
*/
static const struct cfiattrdata *
cfdriver_get_iattr(const struct cfdriver *cd, const char *ia)
{
const struct cfiattrdata * const *cpp;
if (cd->cd_attrs == NULL)
return 0;
for (cpp = cd->cd_attrs; *cpp; cpp++) {
if (STREQ((*cpp)->ci_name, ia)) {
/* Match. */
return *cpp;
}
}
return 0;
}
#if defined(DIAGNOSTIC)
static int
cfdriver_iattr_count(const struct cfdriver *cd)
{
const struct cfiattrdata * const *cpp;
int i;
if (cd->cd_attrs == NULL)
return 0;
for (i = 0, cpp = cd->cd_attrs; *cpp; cpp++) {
i++;
}
return i;
}
#endif /* DIAGNOSTIC */
/*
* Lookup an interface attribute description by name.
* If the driver is given, consider only its supported attributes.
*/
const struct cfiattrdata *
cfiattr_lookup(const char *name, const struct cfdriver *cd)
{
const struct cfdriver *d;
const struct cfiattrdata *ia;
if (cd)
return cfdriver_get_iattr(cd, name);
LIST_FOREACH(d, &allcfdrivers, cd_list) {
ia = cfdriver_get_iattr(d, name);
if (ia)
return ia;
}
return 0;
}
/*
* Determine if `parent' is a potential parent for a device spec based
* on `cfp'.
*/
static int
cfparent_match(const device_t parent, const struct cfparent *cfp)
{
struct cfdriver *pcd;
/* We don't match root nodes here. */
if (cfp == NULL)
return 0;
pcd = parent->dv_cfdriver;
KASSERT(pcd != NULL);
/*
* First, ensure this parent has the correct interface
* attribute.
*/
if (!cfdriver_get_iattr(pcd, cfp->cfp_iattr))
return 0;
/*
* If no specific parent device instance was specified (i.e.
* we're attaching to the attribute only), we're done!
*/
if (cfp->cfp_parent == NULL)
return 1;
/*
* Check the parent device's name.
*/
if (STREQ(pcd->cd_name, cfp->cfp_parent) == 0)
return 0; /* not the same parent */
/*
* Make sure the unit number matches.
*/
if (cfp->cfp_unit == DVUNIT_ANY || /* wildcard */
cfp->cfp_unit == parent->dv_unit)
return 1;
/* Unit numbers don't match. */
return 0;
}
/*
* Helper for config_cfdata_attach(): check all devices whether it could be
* parent any attachment in the config data table passed, and rescan.
*/
static void
rescan_with_cfdata(const struct cfdata *cf)
{
device_t d;
const struct cfdata *cf1;
deviter_t di;
KASSERT(KERNEL_LOCKED_P());
/*
* "alldevs" is likely longer than a modules's cfdata, so make it
* the outer loop.
*/
for (d = deviter_first(&di, 0); d != NULL; d = deviter_next(&di)) {
if (!(d->dv_cfattach->ca_rescan))
continue;
for (cf1 = cf; cf1->cf_name; cf1++) {
if (!cfparent_match(d, cf1->cf_pspec))
continue;
(*d->dv_cfattach->ca_rescan)(d,
cfdata_ifattr(cf1), cf1->cf_loc);
config_deferred(d);
}
}
deviter_release(&di);
}
/*
* Attach a supplemental config data table and rescan potential
* parent devices if required.
*/
int
config_cfdata_attach(cfdata_t cf, int scannow)
{
struct cftable *ct;
KERNEL_LOCK(1, NULL);
ct = kmem_alloc(sizeof(*ct), KM_SLEEP);
ct->ct_cfdata = cf;
TAILQ_INSERT_TAIL(&allcftables, ct, ct_list);
if (scannow)
rescan_with_cfdata(cf);
KERNEL_UNLOCK_ONE(NULL);
return 0;
}
/*
* Helper for config_cfdata_detach: check whether a device is
* found through any attachment in the config data table.
*/
static int
dev_in_cfdata(device_t d, cfdata_t cf)
{
const struct cfdata *cf1;
for (cf1 = cf; cf1->cf_name; cf1++)
if (d->dv_cfdata == cf1)
return 1;
return 0;
}