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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
|
/* $NetBSD: subr_autoconf.c,v 1.313 2023/05/23 08:16:43 riastradh 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.313 2023/05/23 08:16:43 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "opt_ddb.h"
#include "drvctl.h"
#endif
#include <sys/param.h>
#include <sys/device.h>
#include <sys/device_impl.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/localcount.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;
}
static int __diagused
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;
}
/*
* 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;
}
/*
* Detach a supplemental config data table. Detach all devices found
* through that table (and thus keeping references to it) before.
*/
int
config_cfdata_detach(cfdata_t cf)
{
device_t d;
int error = 0;
struct cftable *ct;
deviter_t di;
KERNEL_LOCK(1, NULL);
for (d = deviter_first(&di, DEVITER_F_RW); d != NULL;
d = deviter_next(&di)) {
if (!dev_in_cfdata(d, cf))
continue;
if ((error = config_detach(d, 0)) != 0)
break;
}
deviter_release(&di);
if (error) {
aprint_error_dev(d, "unable to detach instance\n");
goto out;
}
TAILQ_FOREACH(ct, &allcftables, ct_list) {
if (ct->ct_cfdata == cf) {
TAILQ_REMOVE(&allcftables, ct, ct_list);
kmem_free(ct, sizeof(*ct));
error = 0;
goto out;
}
}
/* not found -- shouldn't happen */
error = EINVAL;
out: KERNEL_UNLOCK_ONE(NULL);
return error;
}
/*
* Invoke the "match" routine for a cfdata entry on behalf of
* an external caller, usually a direct config "submatch" routine.
*/
int
config_match(device_t parent, cfdata_t cf, void *aux)
{
struct cfattach *ca;
KASSERT(KERNEL_LOCKED_P());
ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
if (ca == NULL) {
/* No attachment for this entry, oh well. */
return 0;
}
return (*ca->ca_match)(parent, cf, aux);
}
/*
* Invoke the "probe" routine for a cfdata entry on behalf of
* an external caller, usually an indirect config "search" routine.
*/
int
config_probe(device_t parent, cfdata_t cf, void *aux)
{
/*
* This is currently a synonym for config_match(), but this
* is an implementation detail; "match" and "probe" routines
* have different behaviors.
*
* XXX config_probe() should return a bool, because there is
* XXX no match score for probe -- it's either there or it's
* XXX not, but some ports abuse the return value as a way
* XXX to attach "critical" devices before "non-critical"
* XXX devices.
*/
return config_match(parent, cf, aux);
}
static struct cfargs_internal *
cfargs_canonicalize(const struct cfargs * const cfargs,
struct cfargs_internal * const store)
{
struct cfargs_internal *args = store;
memset(args, 0, sizeof(*args));
/* If none specified, are all-NULL pointers are good. */
if (cfargs == NULL) {
return args;
}
/*
* Only one arguments version is recognized at this time.
*/
if (cfargs->cfargs_version != CFARGS_VERSION) {
panic("cfargs_canonicalize: unknown version %lu\n",
(unsigned long)cfargs->cfargs_version);
}
/*
* submatch and search are mutually-exclusive.
*/
if (cfargs->submatch != NULL && cfargs->search != NULL) {
panic("cfargs_canonicalize: submatch and search are "
"mutually-exclusive");
}
if (cfargs->submatch != NULL) {
args->submatch = cfargs->submatch;
} else if (cfargs->search != NULL) {
args->search = cfargs->search;
}
args->iattr = cfargs->iattr;
args->locators = cfargs->locators;
args->devhandle = cfargs->devhandle;
return args;
}
/*
* Iterate over all potential children of some device, calling the given
* function (default being the child's match function) for each one.
* Nonzero returns are matches; the highest value returned is considered
* the best match. Return the `found child' if we got a match, or NULL
* otherwise. The `aux' pointer is simply passed on through.
*
* Note that this function is designed so that it can be used to apply
* an arbitrary function to all potential children (its return value
* can be ignored).
*/
static cfdata_t
config_search_internal(device_t parent, void *aux,
const struct cfargs_internal * const args)
{
struct cftable *ct;
cfdata_t cf;
struct matchinfo m;
KASSERT(config_initialized);
KASSERTMSG((!args->iattr ||
cfdriver_get_iattr(parent->dv_cfdriver, args->iattr)),
"%s searched for child at interface attribute %s,"
" but device %s(4) has no such interface attribute in config(5)",
device_xname(parent), args->iattr,
parent->dv_cfdriver->cd_name);
KASSERTMSG((args->iattr ||
cfdriver_iattr_count(parent->dv_cfdriver) < 2),
"%s searched for child without interface attribute,"
" needed to disambiguate among the %d declared for in %s(4)"
" in config(5)",
device_xname(parent),
cfdriver_iattr_count(parent->dv_cfdriver),
parent->dv_cfdriver->cd_name);
m.fn = args->submatch; /* N.B. union */
m.parent = parent;
m.locs = args->locators;
m.aux = aux;
m.match = NULL;
m.pri = 0;
TAILQ_FOREACH(ct, &allcftables, ct_list) {
for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
/* We don't match root nodes here. */
if (!cf->cf_pspec)
continue;
/*
* Skip cf if no longer eligible, otherwise scan
* through parents for one matching `parent', and
* try match function.
*/
if (cf->cf_fstate == FSTATE_FOUND)
continue;
if (cf->cf_fstate == FSTATE_DNOTFOUND ||
cf->cf_fstate == FSTATE_DSTAR)
continue;
/*
* If an interface attribute was specified,
* consider only children which attach to
* that attribute.
*/
if (args->iattr != NULL &&
!STREQ(args->iattr, cfdata_ifattr(cf)))
continue;
if (cfparent_match(parent, cf->cf_pspec))
mapply(&m, cf);
}
}
rnd_add_uint32(&rnd_autoconf_source, 0);
return m.match;
}
cfdata_t
config_search(device_t parent, void *aux, const struct cfargs *cfargs)
{
cfdata_t cf;
struct cfargs_internal store;
cf = config_search_internal(parent, aux,
cfargs_canonicalize(cfargs, &store));
return cf;
}
/*
* Find the given root device.
* This is much like config_search, but there is no parent.
* Don't bother with multiple cfdata tables; the root node
* must always be in the initial table.
*/
cfdata_t
config_rootsearch(cfsubmatch_t fn, const char *rootname, void *aux)
{
cfdata_t cf;
const short *p;
struct matchinfo m;
m.fn = fn;
m.parent = ROOT;
m.aux = aux;
m.match = NULL;
m.pri = 0;
m.locs = 0;
/*
* Look at root entries for matching name. We do not bother
* with found-state here since only one root should ever be
* searched (and it must be done first).
*/
for (p = cfroots; *p >= 0; p++) {
cf = &cfdata[*p];
if (strcmp(cf->cf_name, rootname) == 0)
mapply(&m, cf);
}
return m.match;
}
static const char * const msgs[] = {
[QUIET] = "",
[UNCONF] = " not configured\n",
[UNSUPP] = " unsupported\n",
};
/*
* The given `aux' argument describes a device that has been found
* on the given parent, but not necessarily configured. Locate the
* configuration data for that device (using the submatch function
* provided, or using candidates' cd_match configuration driver
* functions) and attach it, and return its device_t. If the device was
* not configured, call the given `print' function and return NULL.
*/
device_t
config_found_acquire(device_t parent, void *aux, cfprint_t print,
const struct cfargs * const cfargs)
{
cfdata_t cf;
struct cfargs_internal store;
const struct cfargs_internal * const args =
cfargs_canonicalize(cfargs, &store);
device_t dev;
KERNEL_LOCK(1, NULL);
cf = config_search_internal(parent, aux, args);
if (cf != NULL) {
dev = config_attach_internal(parent, cf, aux, print, args);
goto out;
}
if (print) {
if (config_do_twiddle && cold)
twiddle();
const int pret = (*print)(aux, device_xname(parent));
KASSERT(pret >= 0);
KASSERT(pret < __arraycount(msgs));
KASSERT(msgs[pret] != NULL);
aprint_normal("%s", msgs[pret]);
}
dev = NULL;
out: KERNEL_UNLOCK_ONE(NULL);
return dev;
}
/*
* config_found(parent, aux, print, cfargs)
*
* Legacy entry point for callers whose use of the returned
* device_t is not delimited by device_release.
*
* The caller is required to hold the kernel lock as a fragile
* defence against races.
*
* Callers should ignore the return value or be converted to
* config_found_acquire with a matching device_release once they
* have finished with the returned device_t.
*/
device_t
config_found(device_t parent, void *aux, cfprint_t print,
const struct cfargs * const cfargs)
{
device_t dev;
KASSERT(KERNEL_LOCKED_P());
dev = config_found_acquire(parent, aux, print, cfargs);
if (dev == NULL)
return NULL;
device_release(dev);
return dev;
}
/*
* As above, but for root devices.
*/
device_t
config_rootfound(const char *rootname, void *aux)
{
cfdata_t cf;
device_t dev = NULL;
KERNEL_LOCK(1, NULL);
if ((cf = config_rootsearch(NULL, rootname, aux)) != NULL)
dev = config_attach(ROOT, cf, aux, NULL, CFARGS_NONE);
else
aprint_error("root device %s not configured\n", rootname);
KERNEL_UNLOCK_ONE(NULL);
return dev;
}
/* just like sprintf(buf, "%d") except that it works from the end */
static char *
number(char *ep, int n)
{
*--ep = 0;
while (n >= 10) {
*--ep = (n % 10) + '0';
n /= 10;
}
*--ep = n + '0';
return ep;
}
/*
* Expand the size of the cd_devs array if necessary.
*
* The caller must hold alldevs_lock. config_makeroom() may release and
* re-acquire alldevs_lock, so callers should re-check conditions such
* as alldevs_nwrite == 0 and alldevs_nread == 0 when config_makeroom()
* returns.
*/
static void
config_makeroom(int n, struct cfdriver *cd)
{
int ondevs, nndevs;
device_t *osp, *nsp;
KASSERT(mutex_owned(&alldevs_lock));
alldevs_nwrite++;
/* XXX arithmetic overflow */
for (nndevs = MAX(4, cd->cd_ndevs); nndevs <= n; nndevs += nndevs)
;
while (n >= cd->cd_ndevs) {
/*
* Need to expand the array.
*/
ondevs = cd->cd_ndevs;
osp = cd->cd_devs;
/*
* Release alldevs_lock around allocation, which may
* sleep.
*/
mutex_exit(&alldevs_lock);
nsp = kmem_alloc(sizeof(device_t) * nndevs, KM_SLEEP);
mutex_enter(&alldevs_lock);
/*
* If another thread moved the array while we did
* not hold alldevs_lock, try again.
*/
if (cd->cd_devs != osp || cd->cd_ndevs != ondevs) {
mutex_exit(&alldevs_lock);
kmem_free(nsp, sizeof(device_t) * nndevs);
mutex_enter(&alldevs_lock);
continue;
}
memset(nsp + ondevs, 0, sizeof(device_t) * (nndevs - ondevs));
if (ondevs != 0)
memcpy(nsp, cd->cd_devs, sizeof(device_t) * ondevs);
cd->cd_ndevs = nndevs;
cd->cd_devs = nsp;
if (ondevs != 0) {
mutex_exit(&alldevs_lock);
kmem_free(osp, sizeof(device_t) * ondevs);
mutex_enter(&alldevs_lock);
}
}
KASSERT(mutex_owned(&alldevs_lock));
alldevs_nwrite--;
}
/*
* Put dev into the devices list.
*/
static void
config_devlink(device_t dev)
{
mutex_enter(&alldevs_lock);
KASSERT(device_cfdriver(dev)->cd_devs[dev->dv_unit] == dev);
dev->dv_add_gen = alldevs_gen;
/* It is safe to add a device to the tail of the list while
* readers and writers are in the list.
*/
TAILQ_INSERT_TAIL(&alldevs, dev, dv_list);
mutex_exit(&alldevs_lock);
}
static void
config_devfree(device_t dev)
{
KASSERT(dev->dv_flags & DVF_PRIV_ALLOC);
KASSERTMSG(dev->dv_pending == 0, "%d", dev->dv_pending);
if (dev->dv_cfattach->ca_devsize > 0)
kmem_free(dev->dv_private, dev->dv_cfattach->ca_devsize);
kmem_free(dev, sizeof(*dev));
}
/*
* Caller must hold alldevs_lock.
*/
static void
config_devunlink(device_t dev, struct devicelist *garbage)
{
struct device_garbage *dg = &dev->dv_garbage;
cfdriver_t cd = device_cfdriver(dev);
int i;
KASSERT(mutex_owned(&alldevs_lock));
KASSERTMSG(dev->dv_pending == 0, "%d", dev->dv_pending);
/* Unlink from device list. Link to garbage list. */
TAILQ_REMOVE(&alldevs, dev, dv_list);
TAILQ_INSERT_TAIL(garbage, dev, dv_list);
/* Remove from cfdriver's array. */
cd->cd_devs[dev->dv_unit] = NULL;
/*
* If the device now has no units in use, unlink its softc array.
*/
for (i = 0; i < cd->cd_ndevs; i++) {
if (cd->cd_devs[i] != NULL)
break;
}
/* Nothing found. Unlink, now. Deallocate, later. */
if (i == cd->cd_ndevs) {
dg->dg_ndevs = cd->cd_ndevs;
dg->dg_devs = cd->cd_devs;
cd->cd_devs = NULL;
cd->cd_ndevs = 0;
}
}
static void
config_devdelete(device_t dev)
{
struct device_garbage *dg = &dev->dv_garbage;
device_lock_t dvl = device_getlock(dev);
KASSERTMSG(dev->dv_pending == 0, "%d", dev->dv_pending);
if (dg->dg_devs != NULL)
kmem_free(dg->dg_devs, sizeof(device_t) * dg->dg_ndevs);
localcount_fini(dev->dv_localcount);
kmem_free(dev->dv_localcount, sizeof(*dev->dv_localcount));
cv_destroy(&dvl->dvl_cv);
mutex_destroy(&dvl->dvl_mtx);
KASSERT(dev->dv_properties != NULL);
prop_object_release(dev->dv_properties);
if (dev->dv_activity_handlers)
panic("%s with registered handlers", __func__);
if (dev->dv_locators) {
size_t amount = *--dev->dv_locators;
kmem_free(dev->dv_locators, amount);
}
config_devfree(dev);
}
static int
config_unit_nextfree(cfdriver_t cd, cfdata_t cf)
{
int unit = cf->cf_unit;
KASSERT(mutex_owned(&alldevs_lock));
if (unit < 0)
return -1;
if (cf->cf_fstate == FSTATE_STAR) {
for (; unit < cd->cd_ndevs; unit++)
if (cd->cd_devs[unit] == NULL)
break;
/*
* unit is now the unit of the first NULL device pointer,
* or max(cd->cd_ndevs,cf->cf_unit).
*/
} else {
if (unit < cd->cd_ndevs && cd->cd_devs[unit] != NULL)
unit = -1;
}
return unit;
}
static int
config_unit_alloc(device_t dev, cfdriver_t cd, cfdata_t cf)
{
struct alldevs_foray af;
int unit;
config_alldevs_enter(&af);
for (;;) {
unit = config_unit_nextfree(cd, cf);
if (unit == -1)
break;
if (unit < cd->cd_ndevs) {
cd->cd_devs[unit] = dev;
dev->dv_unit = unit;
break;
}
config_makeroom(unit, cd);
}
config_alldevs_exit(&af);
return unit;
}
static device_t
config_devalloc(const device_t parent, const cfdata_t cf,
const struct cfargs_internal * const args)
{
cfdriver_t cd;
cfattach_t ca;
size_t lname, lunit;
const char *xunit;
int myunit;
char num[10];
device_t dev;
void *dev_private;
const struct cfiattrdata *ia;
device_lock_t dvl;
cd = config_cfdriver_lookup(cf->cf_name);
if (cd == NULL)
return NULL;
ca = config_cfattach_lookup_cd(cd, cf->cf_atname);
if (ca == NULL)
return NULL;
/* get memory for all device vars */
KASSERT(ca->ca_flags & DVF_PRIV_ALLOC);
if (ca->ca_devsize > 0) {
dev_private = kmem_zalloc(ca->ca_devsize, KM_SLEEP);
} else {
dev_private = NULL;
}
dev = kmem_zalloc(sizeof(*dev), KM_SLEEP);
dev->dv_handle = args->devhandle;
dev->dv_class = cd->cd_class;
dev->dv_cfdata = cf;
dev->dv_cfdriver = cd;
dev->dv_cfattach = ca;
dev->dv_activity_count = 0;
dev->dv_activity_handlers = NULL;
dev->dv_private = dev_private;
dev->dv_flags = ca->ca_flags; /* inherit flags from class */
dev->dv_attaching = curlwp;
myunit = config_unit_alloc(dev, cd, cf);
if (myunit == -1) {
config_devfree(dev);
return NULL;
}
/* compute length of name and decimal expansion of unit number */
lname = strlen(cd->cd_name);
xunit = number(&num[sizeof(num)], myunit);
lunit = &num[sizeof(num)] - xunit;
if (lname + lunit > sizeof(dev->dv_xname))
panic("config_devalloc: device name too long");
dvl = device_getlock(dev);
mutex_init(&dvl->dvl_mtx, MUTEX_DEFAULT, IPL_NONE);
cv_init(&dvl->dvl_cv, "pmfsusp");
memcpy(dev->dv_xname, cd->cd_name, lname);
memcpy(dev->dv_xname + lname, xunit, lunit);
dev->dv_parent = parent;
if (parent != NULL)
dev->dv_depth = parent->dv_depth + 1;
else
dev->dv_depth = 0;
dev->dv_flags |= DVF_ACTIVE; /* always initially active */
if (args->locators) {
KASSERT(parent); /* no locators at root */
ia = cfiattr_lookup(cfdata_ifattr(cf), parent->dv_cfdriver);
dev->dv_locators =
kmem_alloc(sizeof(int) * (ia->ci_loclen + 1), KM_SLEEP);
*dev->dv_locators++ = sizeof(int) * (ia->ci_loclen + 1);
memcpy(dev->dv_locators, args->locators,
sizeof(int) * ia->ci_loclen);
}
dev->dv_properties = prop_dictionary_create();
KASSERT(dev->dv_properties != NULL);
prop_dictionary_set_string_nocopy(dev->dv_properties,
"device-driver", dev->dv_cfdriver->cd_name);
prop_dictionary_set_uint16(dev->dv_properties,
"device-unit", dev->dv_unit);
if (parent != NULL) {
prop_dictionary_set_string(dev->dv_properties,
"device-parent", device_xname(parent));
}
dev->dv_localcount = kmem_zalloc(sizeof(*dev->dv_localcount),
KM_SLEEP);
localcount_init(dev->dv_localcount);
if (dev->dv_cfdriver->cd_attrs != NULL)
config_add_attrib_dict(dev);
return dev;
}
/*
* Create an array of device attach attributes and add it
* to the device's dv_properties dictionary.
*
* <key>interface-attributes</key>
* <array>
* <dict>
* <key>attribute-name</key>
* <string>foo</string>
* <key>locators</key>
* <array>
* <dict>
* <key>loc-name</key>
* <string>foo-loc1</string>
* </dict>
* <dict>
* <key>loc-name</key>
* <string>foo-loc2</string>
* <key>default</key>
* <string>foo-loc2-default</string>
* </dict>
* ...
* </array>
* </dict>
* ...
* </array>
*/
static void
config_add_attrib_dict(device_t dev)
{
int i, j;
const struct cfiattrdata *ci;
prop_dictionary_t attr_dict, loc_dict;
prop_array_t attr_array, loc_array;
if ((attr_array = prop_array_create()) == NULL)
return;
for (i = 0; ; i++) {
if ((ci = dev->dv_cfdriver->cd_attrs[i]) == NULL)
break;
if ((attr_dict = prop_dictionary_create()) == NULL)
break;
prop_dictionary_set_string_nocopy(attr_dict, "attribute-name",
ci->ci_name);
/* Create an array of the locator names and defaults */
if (ci->ci_loclen != 0 &&
(loc_array = prop_array_create()) != NULL) {
for (j = 0; j < ci->ci_loclen; j++) {
loc_dict = prop_dictionary_create();
if (loc_dict == NULL)
continue;
prop_dictionary_set_string_nocopy(loc_dict,
"loc-name", ci->ci_locdesc[j].cld_name);
if (ci->ci_locdesc[j].cld_defaultstr != NULL)
prop_dictionary_set_string_nocopy(
loc_dict, "default",
ci->ci_locdesc[j].cld_defaultstr);
prop_array_set(loc_array, j, loc_dict);
prop_object_release(loc_dict);
}
prop_dictionary_set_and_rel(attr_dict, "locators",
loc_array);
}
prop_array_add(attr_array, attr_dict);
prop_object_release(attr_dict);
}
if (i == 0)
prop_object_release(attr_array);
else
prop_dictionary_set_and_rel(dev->dv_properties,
"interface-attributes", attr_array);
return;
}
/*
* Attach a found device.
*
* Returns the device referenced, to be released with device_release.
*/
static device_t
config_attach_internal(device_t parent, cfdata_t cf, void *aux, cfprint_t print,
const struct cfargs_internal * const args)
{
device_t dev;
struct cftable *ct;
const char *drvname;
bool deferred;
KASSERT(KERNEL_LOCKED_P());
dev = config_devalloc(parent, cf, args);
if (!dev)
panic("config_attach: allocation of device softc failed");
/* XXX redundant - see below? */
if (cf->cf_fstate != FSTATE_STAR) {
KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
cf->cf_fstate = FSTATE_FOUND;
}
config_devlink(dev);
if (config_do_twiddle && cold)
twiddle();
else
aprint_naive("Found ");
/*
* We want the next two printfs for normal, verbose, and quiet,
* but not silent (in which case, we're twiddling, instead).
*/
if (parent == ROOT) {
aprint_naive("%s (root)", device_xname(dev));
aprint_normal("%s (root)", device_xname(dev));
} else {
aprint_naive("%s at %s", device_xname(dev),
device_xname(parent));
aprint_normal("%s at %s", device_xname(dev),
device_xname(parent));
if (print)
(void) (*print)(aux, NULL);
}
/*
* Before attaching, clobber any unfound devices that are
* otherwise identical.
* XXX code above is redundant?
*/
drvname = dev->dv_cfdriver->cd_name;
TAILQ_FOREACH(ct, &allcftables, ct_list) {
for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
if (STREQ(cf->cf_name, drvname) &&
cf->cf_unit == dev->dv_unit) {
if (cf->cf_fstate == FSTATE_NOTFOUND)
cf->cf_fstate = FSTATE_FOUND;
}
}
}
device_register(dev, aux);
/* Let userland know */
devmon_report_device(dev, true);
/*
* Prevent detach until the driver's attach function, and all
* deferred actions, have finished.
*/
config_pending_incr(dev);
/*
* Prevent concurrent detach from destroying the device_t until
* the caller has released the device.
*/
device_acquire(dev);
/* Call the driver's attach function. */
(*dev->dv_cfattach->ca_attach)(parent, dev, aux);
/*
* Allow other threads to acquire references to the device now
* that the driver's attach function is done.
*/
mutex_enter(&config_misc_lock);
KASSERT(dev->dv_attaching == curlwp);
dev->dv_attaching = NULL;
cv_broadcast(&config_misc_cv);
mutex_exit(&config_misc_lock);
/*
* Synchronous parts of attach are done. Allow detach, unless
* the driver's attach function scheduled deferred actions.
*/
config_pending_decr(dev);
mutex_enter(&config_misc_lock);
deferred = (dev->dv_pending != 0);
mutex_exit(&config_misc_lock);
if (!deferred && !device_pmf_is_registered(dev))
aprint_debug_dev(dev,
"WARNING: power management not supported\n");
config_process_deferred(&deferred_config_queue, dev);
device_register_post_config(dev, aux);
rnd_add_uint32(&rnd_autoconf_source, 0);
return dev;
}
device_t
config_attach_acquire(device_t parent, cfdata_t cf, void *aux, cfprint_t print,
const struct cfargs *cfargs)
{
struct cfargs_internal store;
device_t dev;
KERNEL_LOCK(1, NULL);
dev = config_attach_internal(parent, cf, aux, print,
cfargs_canonicalize(cfargs, &store));
KERNEL_UNLOCK_ONE(NULL);
return dev;
}
/*
* config_attach(parent, cf, aux, print, cfargs)
*
* Legacy entry point for callers whose use of the returned
* device_t is not delimited by device_release.
*
* The caller is required to hold the kernel lock as a fragile
* defence against races.
*
* Callers should ignore the return value or be converted to
* config_attach_acquire with a matching device_release once they
* have finished with the returned device_t.
*/
device_t
config_attach(device_t parent, cfdata_t cf, void *aux, cfprint_t print,
const struct cfargs *cfargs)
{
device_t dev;
KASSERT(KERNEL_LOCKED_P());
dev = config_attach_acquire(parent, cf, aux, print, cfargs);
if (dev == NULL)
return NULL;
device_release(dev);
return dev;
}
/*
* As above, but for pseudo-devices. Pseudo-devices attached in this
* way are silently inserted into the device tree, and their children
* attached.
*
* Note that because pseudo-devices are attached silently, any information
* the attach routine wishes to print should be prefixed with the device
* name by the attach routine.
*/
device_t
config_attach_pseudo_acquire(cfdata_t cf, void *aux)
{
device_t dev;
KERNEL_LOCK(1, NULL);
struct cfargs_internal args = { };
dev = config_devalloc(ROOT, cf, &args);
if (!dev)
goto out;
/* XXX mark busy in cfdata */
if (cf->cf_fstate != FSTATE_STAR) {
KASSERT(cf->cf_fstate == FSTATE_NOTFOUND);
cf->cf_fstate = FSTATE_FOUND;
}
config_devlink(dev);
#if 0 /* XXXJRT not yet */
device_register(dev, NULL); /* like a root node */
#endif
/* Let userland know */
devmon_report_device(dev, true);
/*
* Prevent detach until the driver's attach function, and all
* deferred actions, have finished.
*/
config_pending_incr(dev);
/*
* Prevent concurrent detach from destroying the device_t until
* the caller has released the device.
*/
device_acquire(dev);
/* Call the driver's attach function. */
(*dev->dv_cfattach->ca_attach)(ROOT, dev, aux);
/*
* Allow other threads to acquire references to the device now
* that the driver's attach function is done.
*/
mutex_enter(&config_misc_lock);
KASSERT(dev->dv_attaching == curlwp);
dev->dv_attaching = NULL;
cv_broadcast(&config_misc_cv);
mutex_exit(&config_misc_lock);
/*
* Synchronous parts of attach are done. Allow detach, unless
* the driver's attach function scheduled deferred actions.
*/
config_pending_decr(dev);
config_process_deferred(&deferred_config_queue, dev);
out: KERNEL_UNLOCK_ONE(NULL);
return dev;
}
/*
* config_attach_pseudo(cf)
*
* Legacy entry point for callers whose use of the returned
* device_t is not delimited by device_release.
*
* The caller is required to hold the kernel lock as a fragile
* defence against races.
*
* Callers should ignore the return value or be converted to
* config_attach_pseudo_acquire with a matching device_release
* once they have finished with the returned device_t. As a
* bonus, config_attach_pseudo_acquire can pass a non-null aux
* argument into the driver's attach routine.
*/
device_t
config_attach_pseudo(cfdata_t cf)
{
device_t dev;
dev = config_attach_pseudo_acquire(cf, NULL);
if (dev == NULL)
return dev;
device_release(dev);
return dev;
}
/*
* Caller must hold alldevs_lock.
*/
static void
config_collect_garbage(struct devicelist *garbage)
{
device_t dv;
KASSERT(!cpu_intr_p());
KASSERT(!cpu_softintr_p());
KASSERT(mutex_owned(&alldevs_lock));
while (alldevs_nwrite == 0 && alldevs_nread == 0 && alldevs_garbage) {
TAILQ_FOREACH(dv, &alldevs, dv_list) {
if (dv->dv_del_gen != 0)
break;
}
if (dv == NULL) {
alldevs_garbage = false;
break;
}
config_devunlink(dv, garbage);
}
KASSERT(mutex_owned(&alldevs_lock));
}
static void
config_dump_garbage(struct devicelist *garbage)
{
device_t dv;
while ((dv = TAILQ_FIRST(garbage)) != NULL) {
TAILQ_REMOVE(garbage, dv, dv_list);
config_devdelete(dv);
}
}
static int
config_detach_enter(device_t dev)
{
struct lwp *l __diagused;
int error = 0;
mutex_enter(&config_misc_lock);
/*
* Wait until attach has fully completed, and until any
* concurrent detach (e.g., drvctl racing with USB event
* thread) has completed.
*
* Caller must hold alldevs_nread or alldevs_nwrite (e.g., via
* deviter) to ensure the winner of the race doesn't free the
* device leading the loser of the race into use-after-free.
*
* XXX Not all callers do this!
*/
while (dev->dv_pending || dev->dv_detaching) {
KASSERTMSG(dev->dv_detaching != curlwp,
"recursively detaching %s", device_xname(dev));
error = cv_wait_sig(&config_misc_cv, &config_misc_lock);
if (error)
goto out;
}
/*
* Attach has completed, and no other concurrent detach is
* running. Claim the device for detaching. This will cause
* all new attempts to acquire references to block.
*/
KASSERTMSG((l = dev->dv_attaching) == NULL,
"lwp %ld [%s] @ %p attaching %s",
(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
device_xname(dev));
KASSERTMSG((l = dev->dv_detaching) == NULL,
"lwp %ld [%s] @ %p detaching %s",
(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
device_xname(dev));
dev->dv_detaching = curlwp;
out: mutex_exit(&config_misc_lock);
return error;
}
static void
config_detach_exit(device_t dev)
{
struct lwp *l __diagused;
mutex_enter(&config_misc_lock);
KASSERTMSG(dev->dv_detaching != NULL, "not detaching %s",
device_xname(dev));
KASSERTMSG((l = dev->dv_detaching) == curlwp,
"lwp %ld [%s] @ %p detaching %s",
(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
device_xname(dev));
dev->dv_detaching = NULL;
cv_broadcast(&config_misc_cv);
mutex_exit(&config_misc_lock);
}
/*
* Detach a device. Optionally forced (e.g. because of hardware
* removal) and quiet. Returns zero if successful, non-zero
* (an error code) otherwise.
*
* Note that this code wants to be run from a process context, so
* that the detach can sleep to allow processes which have a device
* open to run and unwind their stacks.
*
* Caller must hold a reference with device_acquire or
* device_lookup_acquire.
*/
int
config_detach_release(device_t dev, int flags)
{
struct alldevs_foray af;
struct cftable *ct;
cfdata_t cf;
const struct cfattach *ca;
struct cfdriver *cd;
device_t d __diagused;
int rv = 0;
KERNEL_LOCK(1, NULL);
cf = dev->dv_cfdata;
KASSERTMSG((cf == NULL || cf->cf_fstate == FSTATE_FOUND ||
cf->cf_fstate == FSTATE_STAR),
"config_detach: %s: bad device fstate: %d",
device_xname(dev), cf ? cf->cf_fstate : -1);
cd = dev->dv_cfdriver;
KASSERT(cd != NULL);
ca = dev->dv_cfattach;
KASSERT(ca != NULL);
/*
* Only one detach at a time, please -- and not until fully
* attached.
*/
rv = config_detach_enter(dev);
device_release(dev);
if (rv) {
KERNEL_UNLOCK_ONE(NULL);
return rv;
}
mutex_enter(&alldevs_lock);
if (dev->dv_del_gen != 0) {
mutex_exit(&alldevs_lock);
#ifdef DIAGNOSTIC
printf("%s: %s is already detached\n", __func__,
device_xname(dev));
#endif /* DIAGNOSTIC */
config_detach_exit(dev);
KERNEL_UNLOCK_ONE(NULL);
return ENOENT;
}
alldevs_nwrite++;
mutex_exit(&alldevs_lock);
/*
* Call the driver's .ca_detach function, unless it has none or
* we are skipping it because it's unforced shutdown time and
* the driver didn't ask to detach on shutdown.
*/
if (!detachall &&
(flags & (DETACH_SHUTDOWN|DETACH_FORCE)) == DETACH_SHUTDOWN &&
(dev->dv_flags & DVF_DETACH_SHUTDOWN) == 0) {
rv = EOPNOTSUPP;
} else if (ca->ca_detach != NULL) {
rv = (*ca->ca_detach)(dev, flags);
} else
rv = EOPNOTSUPP;
KASSERTMSG(!dev->dv_detach_done, "%s detached twice, error=%d",
device_xname(dev), rv);
/*
* If it was not possible to detach the device, then we either
* panic() (for the forced but failed case), or return an error.
*/
if (rv) {
/*
* Detach failed -- likely EOPNOTSUPP or EBUSY. Driver
* must not have called config_detach_commit.
*/
KASSERTMSG(!dev->dv_detach_committed,
"%s committed to detaching and then backed out, error=%d",
device_xname(dev), rv);
if (flags & DETACH_FORCE) {
panic("config_detach: forced detach of %s failed (%d)",
device_xname(dev), rv);
}
goto out;
}
/*
* The device has now been successfully detached.
*/
dev->dv_detach_done = true;
/*
* If .ca_detach didn't commit to detach, then do that for it.
* This wakes any pending device_lookup_acquire calls so they
* will fail.
*/
config_detach_commit(dev);
/*
* If it was possible to detach the device, ensure that the
* device is deactivated.
*/
dev->dv_flags &= ~DVF_ACTIVE; /* XXXSMP */
/*
* Wait for all device_lookup_acquire references -- mostly, for
* all attempts to open the device -- to drain. It is the
* responsibility of .ca_detach to ensure anything with open
* references will be interrupted and release them promptly,
* not block indefinitely. All new attempts to acquire
* references will fail, as config_detach_commit has arranged
* by now.
*/
mutex_enter(&config_misc_lock);
localcount_drain(dev->dv_localcount,
&config_misc_cv, &config_misc_lock);
mutex_exit(&config_misc_lock);
/* Let userland know */
devmon_report_device(dev, false);
#ifdef DIAGNOSTIC
/*
* Sanity: If you're successfully detached, you should have no
* children. (Note that because children must be attached
* after parents, we only need to search the latter part of
* the list.)
*/
mutex_enter(&alldevs_lock);
for (d = TAILQ_NEXT(dev, dv_list); d != NULL;
d = TAILQ_NEXT(d, dv_list)) {
if (d->dv_parent == dev && d->dv_del_gen == 0) {
printf("config_detach: detached device %s"
" has children %s\n", device_xname(dev),
device_xname(d));
panic("config_detach");
}
}
mutex_exit(&alldevs_lock);
#endif
/* notify the parent that the child is gone */
if (dev->dv_parent) {
device_t p = dev->dv_parent;
if (p->dv_cfattach->ca_childdetached)
(*p->dv_cfattach->ca_childdetached)(p, dev);
}
/*
* Mark cfdata to show that the unit can be reused, if possible.
*/
TAILQ_FOREACH(ct, &allcftables, ct_list) {
for (cf = ct->ct_cfdata; cf->cf_name; cf++) {
if (STREQ(cf->cf_name, cd->cd_name)) {
if (cf->cf_fstate == FSTATE_FOUND &&
cf->cf_unit == dev->dv_unit)
cf->cf_fstate = FSTATE_NOTFOUND;
}
}
}
if (dev->dv_cfdata != NULL && (flags & DETACH_QUIET) == 0)
aprint_normal_dev(dev, "detached\n");
out:
config_detach_exit(dev);
config_alldevs_enter(&af);
KASSERT(alldevs_nwrite != 0);
--alldevs_nwrite;
if (rv == 0 && dev->dv_del_gen == 0) {
if (alldevs_nwrite == 0 && alldevs_nread == 0)
config_devunlink(dev, &af.af_garbage);
else {
dev->dv_del_gen = alldevs_gen;
alldevs_garbage = true;
}
}
config_alldevs_exit(&af);
KERNEL_UNLOCK_ONE(NULL);
return rv;
}
/*
* config_detach(dev, flags)
*
* Legacy entry point for callers that have not acquired a
* reference to dev.
*
* The caller is required to hold the kernel lock as a fragile
* defence against races.
*
* Callers should be converted to use device_acquire under a lock
* taken also by .ca_childdetached to synchronize access to the
* device_t, and then config_detach_release ouside the lock.
* Alternatively, most drivers detach children only in their own
* detach routines, which can be done with config_detach_children
* instead.
*/
int
config_detach(device_t dev, int flags)
{
device_acquire(dev);
return config_detach_release(dev, flags);
}
/*
* config_detach_commit(dev)
*
* Issued by a driver's .ca_detach routine to notify anyone
* waiting in device_lookup_acquire that the driver is committed
* to detaching the device, which allows device_lookup_acquire to
* wake up and fail immediately.
*
* Safe to call multiple times -- idempotent. Must be called
* during config_detach_enter/exit. Safe to use with
* device_lookup because the device is not actually removed from
* the table until after config_detach_exit.
*/
void
config_detach_commit(device_t dev)
{
struct lwp *l __diagused;
mutex_enter(&config_misc_lock);
KASSERTMSG(dev->dv_detaching != NULL, "not detaching %s",
device_xname(dev));
KASSERTMSG((l = dev->dv_detaching) == curlwp,
"lwp %ld [%s] @ %p detaching %s",
(long)l->l_lid, (l->l_name ? l->l_name : l->l_proc->p_comm), l,
device_xname(dev));
dev->dv_detach_committed = true;
cv_broadcast(&config_misc_cv);
mutex_exit(&config_misc_lock);
}
int
config_detach_children(device_t parent, int flags)
{
device_t dv;
deviter_t di;
int error = 0;
KASSERT(KERNEL_LOCKED_P());
for (dv = deviter_first(&di, DEVITER_F_RW); dv != NULL;
dv = deviter_next(&di)) {
if (device_parent(dv) != parent)
continue;
if ((error = config_detach(dv, flags)) != 0)
break;
}
deviter_release(&di);
return error;
}
device_t
shutdown_first(struct shutdown_state *s)
{
if (!s->initialized) {
deviter_init(&s->di, DEVITER_F_SHUTDOWN|DEVITER_F_LEAVES_FIRST);
s->initialized = true;
}
return shutdown_next(s);
}
device_t
shutdown_next(struct shutdown_state *s)
{
device_t dv;
while ((dv = deviter_next(&s->di)) != NULL && !device_is_active(dv))
;
if (dv == NULL)
s->initialized = false;
return dv;
}
bool
config_detach_all(int how)
{
static struct shutdown_state s;
device_t curdev;
bool progress = false;
int flags;
KERNEL_LOCK(1, NULL);
if ((how & (RB_NOSYNC|RB_DUMP)) != 0)
goto out;
if ((how & RB_POWERDOWN) == RB_POWERDOWN)
flags = DETACH_SHUTDOWN | DETACH_POWEROFF;
else
flags = DETACH_SHUTDOWN;
for (curdev = shutdown_first(&s); curdev != NULL;
curdev = shutdown_next(&s)) {
aprint_debug(" detaching %s, ", device_xname(curdev));
if (config_detach(curdev, flags) == 0) {
progress = true;
aprint_debug("success.");
} else
aprint_debug("failed.");
}
out: KERNEL_UNLOCK_ONE(NULL);
return progress;
}
static bool
device_is_ancestor_of(device_t ancestor, device_t descendant)
{
device_t dv;
for (dv = descendant; dv != NULL; dv = device_parent(dv)) {
if (device_parent(dv) == ancestor)
return true;
}
return false;
}
int
config_deactivate(device_t dev)
{
deviter_t di;
const struct cfattach *ca;
device_t descendant;
int s, rv = 0, oflags;
for (descendant = deviter_first(&di, DEVITER_F_ROOT_FIRST);
descendant != NULL;
descendant = deviter_next(&di)) {
if (dev != descendant &&
!device_is_ancestor_of(dev, descendant))
continue;
if ((descendant->dv_flags & DVF_ACTIVE) == 0)
continue;
ca = descendant->dv_cfattach;
oflags = descendant->dv_flags;
descendant->dv_flags &= ~DVF_ACTIVE;
if (ca->ca_activate == NULL)
continue;
s = splhigh();
rv = (*ca->ca_activate)(descendant, DVACT_DEACTIVATE);
splx(s);
if (rv != 0)
descendant->dv_flags = oflags;
}
deviter_release(&di);
return rv;
}
/*
* Defer the configuration of the specified device until all
* of its parent's devices have been attached.
*/
void
config_defer(device_t dev, void (*func)(device_t))
{
struct deferred_config *dc;
if (dev->dv_parent == NULL)
panic("config_defer: can't defer config of a root device");
dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
config_pending_incr(dev);
mutex_enter(&config_misc_lock);
#ifdef DIAGNOSTIC
struct deferred_config *odc;
TAILQ_FOREACH(odc, &deferred_config_queue, dc_queue) {
if (odc->dc_dev == dev)
panic("config_defer: deferred twice");
}
#endif
dc->dc_dev = dev;
dc->dc_func = func;
TAILQ_INSERT_TAIL(&deferred_config_queue, dc, dc_queue);
mutex_exit(&config_misc_lock);
}
/*
* Defer some autoconfiguration for a device until after interrupts
* are enabled.
*/
void
config_interrupts(device_t dev, void (*func)(device_t))
{
struct deferred_config *dc;
/*
* If interrupts are enabled, callback now.
*/
if (cold == 0) {
(*func)(dev);
return;
}
dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
config_pending_incr(dev);
mutex_enter(&config_misc_lock);
#ifdef DIAGNOSTIC
struct deferred_config *odc;
TAILQ_FOREACH(odc, &interrupt_config_queue, dc_queue) {
if (odc->dc_dev == dev)
panic("config_interrupts: deferred twice");
}
#endif
dc->dc_dev = dev;
dc->dc_func = func;
TAILQ_INSERT_TAIL(&interrupt_config_queue, dc, dc_queue);
mutex_exit(&config_misc_lock);
}
/*
* Defer some autoconfiguration for a device until after root file system
* is mounted (to load firmware etc).
*/
void
config_mountroot(device_t dev, void (*func)(device_t))
{
struct deferred_config *dc;
/*
* If root file system is mounted, callback now.
*/
if (root_is_mounted) {
(*func)(dev);
return;
}
dc = kmem_alloc(sizeof(*dc), KM_SLEEP);
mutex_enter(&config_misc_lock);
#ifdef DIAGNOSTIC
struct deferred_config *odc;
TAILQ_FOREACH(odc, &mountroot_config_queue, dc_queue) {
if (odc->dc_dev == dev)
panic("%s: deferred twice", __func__);
}
#endif
dc->dc_dev = dev;
dc->dc_func = func;
TAILQ_INSERT_TAIL(&mountroot_config_queue, dc, dc_queue);
mutex_exit(&config_misc_lock);
}
/*
* Process a deferred configuration queue.
*/
static void
config_process_deferred(struct deferred_config_head *queue, device_t parent)
{
struct deferred_config *dc;
KASSERT(KERNEL_LOCKED_P());
mutex_enter(&config_misc_lock);
dc = TAILQ_FIRST(queue);
while (dc) {
if (parent == NULL || dc->dc_dev->dv_parent == parent) {
TAILQ_REMOVE(queue, dc, dc_queue);
mutex_exit(&config_misc_lock);
(*dc->dc_func)(dc->dc_dev);
config_pending_decr(dc->dc_dev);
kmem_free(dc, sizeof(*dc));
mutex_enter(&config_misc_lock);
/* Restart, queue might have changed */
dc = TAILQ_FIRST(queue);
} else {
dc = TAILQ_NEXT(dc, dc_queue);
}
}
mutex_exit(&config_misc_lock);
}
/*
* Manipulate the config_pending semaphore.
*/
void
config_pending_incr(device_t dev)
{
mutex_enter(&config_misc_lock);
KASSERTMSG(dev->dv_pending < INT_MAX,
"%s: excess config_pending_incr", device_xname(dev));
if (dev->dv_pending++ == 0)
TAILQ_INSERT_TAIL(&config_pending, dev, dv_pending_list);
#ifdef DEBUG_AUTOCONF
printf("%s: %s %d\n", __func__, device_xname(dev), dev->dv_pending);
#endif
mutex_exit(&config_misc_lock);
}
void
config_pending_decr(device_t dev)
{
mutex_enter(&config_misc_lock);
KASSERTMSG(dev->dv_pending > 0,
"%s: excess config_pending_decr", device_xname(dev));
if (--dev->dv_pending == 0) {
TAILQ_REMOVE(&config_pending, dev, dv_pending_list);
cv_broadcast(&config_misc_cv);
}
#ifdef DEBUG_AUTOCONF
printf("%s: %s %d\n", __func__, device_xname(dev), dev->dv_pending);
#endif
mutex_exit(&config_misc_lock);
}
/*
* Register a "finalization" routine. Finalization routines are
* called iteratively once all real devices have been found during
* autoconfiguration, for as long as any one finalizer has done
* any work.
*/
int
config_finalize_register(device_t dev, int (*fn)(device_t))
{
struct finalize_hook *f;
int error = 0;
KERNEL_LOCK(1, NULL);
/*
* If finalization has already been done, invoke the
* callback function now.
*/
if (config_finalize_done) {
while ((*fn)(dev) != 0)
/* loop */ ;
goto out;
}
/* Ensure this isn't already on the list. */
TAILQ_FOREACH(f, &config_finalize_list, f_list) {
if (f->f_func == fn && f->f_dev == dev) {
error = EEXIST;
goto out;
}
}
f = kmem_alloc(sizeof(*f), KM_SLEEP);
f->f_func = fn;
f->f_dev = dev;
TAILQ_INSERT_TAIL(&config_finalize_list, f, f_list);
/* Success! */
error = 0;
out: KERNEL_UNLOCK_ONE(NULL);
return error;
}
void
config_finalize(void)
{
struct finalize_hook *f;
struct pdevinit *pdev;
extern struct pdevinit pdevinit[];
int errcnt, rv;
/*
* Now that device driver threads have been created, wait for
* them to finish any deferred autoconfiguration.
*/
mutex_enter(&config_misc_lock);
while (!TAILQ_EMPTY(&config_pending)) {
device_t dev;
int error;
error = cv_timedwait(&config_misc_cv, &config_misc_lock,
mstohz(1000));
if (error == EWOULDBLOCK) {
aprint_debug("waiting for devices:");
TAILQ_FOREACH(dev, &config_pending, dv_pending_list)
aprint_debug(" %s", device_xname(dev));
aprint_debug("\n");
}
}
mutex_exit(&config_misc_lock);
KERNEL_LOCK(1, NULL);
/* Attach pseudo-devices. */
for (pdev = pdevinit; pdev->pdev_attach != NULL; pdev++)
(*pdev->pdev_attach)(pdev->pdev_count);
/* Run the hooks until none of them does any work. */
do {
rv = 0;
TAILQ_FOREACH(f, &config_finalize_list, f_list)
rv |= (*f->f_func)(f->f_dev);
} while (rv != 0);
config_finalize_done = 1;
/* Now free all the hooks. */
while ((f = TAILQ_FIRST(&config_finalize_list)) != NULL) {
TAILQ_REMOVE(&config_finalize_list, f, f_list);
kmem_free(f, sizeof(*f));
}
KERNEL_UNLOCK_ONE(NULL);
errcnt = aprint_get_error_count();
if ((boothowto & (AB_QUIET|AB_SILENT)) != 0 &&
(boothowto & AB_VERBOSE) == 0) {
mutex_enter(&config_misc_lock);
if (config_do_twiddle) {
config_do_twiddle = 0;
printf_nolog(" done.\n");
}
mutex_exit(&config_misc_lock);
}
if (errcnt != 0) {
printf("WARNING: %d error%s while detecting hardware; "
"check system log.\n", errcnt,
errcnt == 1 ? "" : "s");
}
}
void
config_twiddle_init(void)
{
if ((boothowto & (AB_SILENT|AB_VERBOSE)) == AB_SILENT) {
config_do_twiddle = 1;
}
callout_setfunc(&config_twiddle_ch, config_twiddle_fn, NULL);
}
void
config_twiddle_fn(void *cookie)
{
mutex_enter(&config_misc_lock);
if (config_do_twiddle) {
twiddle();
callout_schedule(&config_twiddle_ch, mstohz(100));
}
mutex_exit(&config_misc_lock);
}
static void
config_alldevs_enter(struct alldevs_foray *af)
{
TAILQ_INIT(&af->af_garbage);
mutex_enter(&alldevs_lock);
config_collect_garbage(&af->af_garbage);
}
static void
config_alldevs_exit(struct alldevs_foray *af)
{
mutex_exit(&alldevs_lock);
config_dump_garbage(&af->af_garbage);
}
/*
* device_lookup:
*
* Look up a device instance for a given driver.
*
* Caller is responsible for ensuring the device's state is
* stable, either by holding a reference already obtained with
* device_lookup_acquire or by otherwise ensuring the device is
* attached and can't be detached (e.g., holding an open device
* node and ensuring *_detach calls vdevgone).
*
* XXX Find a way to assert this.
*
* Safe for use up to and including interrupt context at IPL_VM.
* Never sleeps.
*/
device_t
device_lookup(cfdriver_t cd, int unit)
{
device_t dv;
mutex_enter(&alldevs_lock);
if (unit < 0 || unit >= cd->cd_ndevs)
dv = NULL;
else if ((dv = cd->cd_devs[unit]) != NULL && dv->dv_del_gen != 0)
dv = NULL;
mutex_exit(&alldevs_lock);
return dv;
}
/*
* device_lookup_private:
*
* Look up a softc instance for a given driver.
*/
void *
device_lookup_private(cfdriver_t cd, int unit)
{
return device_private(device_lookup(cd, unit));
}
/*
* device_lookup_acquire:
*
* Look up a device instance for a given driver, and return a
* reference to it that must be released by device_release.
*
* => If the device is still attaching, blocks until *_attach has
* returned.
*
* => If the device is detaching, blocks until *_detach has
* returned. May succeed or fail in that case, depending on
* whether *_detach has backed out (EBUSY) or committed to
* detaching.
*
* May sleep.
*/
device_t
device_lookup_acquire(cfdriver_t cd, int unit)
{
device_t dv;
ASSERT_SLEEPABLE();
/* XXX This should have a pserialized fast path -- TBD. */
mutex_enter(&config_misc_lock);
mutex_enter(&alldevs_lock);
retry: if (unit < 0 || unit >= cd->cd_ndevs ||
(dv = cd->cd_devs[unit]) == NULL ||
dv->dv_del_gen != 0 ||
dv->dv_detach_committed) {
dv = NULL;
} else {
/*
* Wait for the device to stabilize, if attaching or
* detaching. Either way we must wait for *_attach or
* *_detach to complete, and either way we must retry:
* even if detaching, *_detach might fail (EBUSY) so
* the device may still be there.
*/
if ((dv->dv_attaching != NULL && dv->dv_attaching != curlwp) ||
dv->dv_detaching != NULL) {
mutex_exit(&alldevs_lock);
cv_wait(&config_misc_cv, &config_misc_lock);
mutex_enter(&alldevs_lock);
goto retry;
}
device_acquire(dv);
}
mutex_exit(&alldevs_lock);
mutex_exit(&config_misc_lock);
return dv;
}
/*
* device_acquire:
*
* Acquire a reference to a device. It is the caller's
* responsibility to ensure that the device's .ca_detach routine
* cannot return before calling this. Caller must release the
* reference with device_release or config_detach_release.
*/
void
device_acquire(device_t dv)
{
/*
* No lock because the caller has promised that this can't
* change concurrently with device_acquire.
*/
KASSERTMSG(!dv->dv_detach_done, "%s",
dv == NULL ? "(null)" : device_xname(dv));
localcount_acquire(dv->dv_localcount);
}
/*
* device_release:
*
* Release a reference to a device acquired with device_acquire or
* device_lookup_acquire.
*/
void
device_release(device_t dv)
{
localcount_release(dv->dv_localcount,
&config_misc_cv, &config_misc_lock);
}
/*
* device_find_by_xname:
*
* Returns the device of the given name or NULL if it doesn't exist.
*/
device_t
device_find_by_xname(const char *name)
{
device_t dv;
deviter_t di;
for (dv = deviter_first(&di, 0); dv != NULL; dv = deviter_next(&di)) {
if (strcmp(device_xname(dv), name) == 0)
break;
}
deviter_release(&di);
return dv;
}
/*
* device_find_by_driver_unit:
*
* Returns the device of the given driver name and unit or
* NULL if it doesn't exist.
*/
device_t
device_find_by_driver_unit(const char *name, int unit)
{
struct cfdriver *cd;
if ((cd = config_cfdriver_lookup(name)) == NULL)
return NULL;
return device_lookup(cd, unit);
}
static bool
match_strcmp(const char * const s1, const char * const s2)
{
return strcmp(s1, s2) == 0;
}
static bool
match_pmatch(const char * const s1, const char * const s2)
{
return pmatch(s1, s2, NULL) == 2;
}
static bool
strarray_match_internal(const char ** const strings,
unsigned int const nstrings, const char * const str,
unsigned int * const indexp,
bool (*match_fn)(const char *, const char *))
{
unsigned int i;
if (strings == NULL || nstrings == 0) {
return false;
}
for (i = 0; i < nstrings; i++) {
if ((*match_fn)(strings[i], str)) {
*indexp = i;
return true;
}
}
return false;
}
static int
strarray_match(const char ** const strings, unsigned int const nstrings,
const char * const str)
{
unsigned int idx;
if (strarray_match_internal(strings, nstrings, str, &idx,
match_strcmp)) {
return (int)(nstrings - idx);
}
return 0;
}
static int
strarray_pmatch(const char ** const strings, unsigned int const nstrings,
const char * const pattern)
{
unsigned int idx;
if (strarray_match_internal(strings, nstrings, pattern, &idx,
match_pmatch)) {
return (int)(nstrings - idx);
}
return 0;
}
static int
device_compatible_match_strarray_internal(
const char **device_compats, int ndevice_compats,
const struct device_compatible_entry *driver_compats,
const struct device_compatible_entry **matching_entryp,
int (*match_fn)(const char **, unsigned int, const char *))
{
const struct device_compatible_entry *dce = NULL;
int rv;
if (ndevice_compats == 0 || device_compats == NULL ||
driver_compats == NULL)
return 0;
for (dce = driver_compats; dce->compat != NULL; dce++) {
rv = (*match_fn)(device_compats, ndevice_compats, dce->compat);
if (rv != 0) {
if (matching_entryp != NULL) {
*matching_entryp = dce;
}
return rv;
}
}
return 0;
}
/*
* device_compatible_match:
*
* Match a driver's "compatible" data against a device's
* "compatible" strings. Returns resulted weighted by
* which device "compatible" string was matched.
*/
int
device_compatible_match(const char **device_compats, int ndevice_compats,
const struct device_compatible_entry *driver_compats)
{
return device_compatible_match_strarray_internal(device_compats,
ndevice_compats, driver_compats, NULL, strarray_match);
}
/*
* device_compatible_pmatch:
*
* Like device_compatible_match(), but uses pmatch(9) to compare
* the device "compatible" strings against patterns in the
* driver's "compatible" data.
*/
int
device_compatible_pmatch(const char **device_compats, int ndevice_compats,
const struct device_compatible_entry *driver_compats)
{
return device_compatible_match_strarray_internal(device_compats,
ndevice_compats, driver_compats, NULL, strarray_pmatch);
}
static int
device_compatible_match_strlist_internal(
const char * const device_compats, size_t const device_compatsize,
const struct device_compatible_entry *driver_compats,
const struct device_compatible_entry **matching_entryp,
int (*match_fn)(const char *, size_t, const char *))
{
const struct device_compatible_entry *dce = NULL;
int rv;
if (device_compats == NULL || device_compatsize == 0 ||
driver_compats == NULL)
return 0;
for (dce = driver_compats; dce->compat != NULL; dce++) {
rv = (*match_fn)(device_compats, device_compatsize,
dce->compat);
if (rv != 0) {
if (matching_entryp != NULL) {
*matching_entryp = dce;
}
return rv;
}
}
return 0;
}
/*
* device_compatible_match_strlist:
*
* Like device_compatible_match(), but take the device
* "compatible" strings as an OpenFirmware-style string
* list.
*/
int
device_compatible_match_strlist(
const char * const device_compats, size_t const device_compatsize,
const struct device_compatible_entry *driver_compats)
{
return device_compatible_match_strlist_internal(device_compats,
device_compatsize, driver_compats, NULL, strlist_match);
}
/*
* device_compatible_pmatch_strlist:
*
* Like device_compatible_pmatch(), but take the device
* "compatible" strings as an OpenFirmware-style string
* list.
*/
int
device_compatible_pmatch_strlist(
const char * const device_compats, size_t const device_compatsize,
const struct device_compatible_entry *driver_compats)
{
return device_compatible_match_strlist_internal(device_compats,
device_compatsize, driver_compats, NULL, strlist_pmatch);
}
static int
device_compatible_match_id_internal(
uintptr_t const id, uintptr_t const mask, uintptr_t const sentinel_id,
const struct device_compatible_entry *driver_compats,
const struct device_compatible_entry **matching_entryp)
{
const struct device_compatible_entry *dce = NULL;
if (mask == 0)
return 0;
for (dce = driver_compats; dce->id != sentinel_id; dce++) {
if ((id & mask) == dce->id) {
if (matching_entryp != NULL) {
*matching_entryp = dce;
}
return 1;
}
}
return 0;
}
/*
* device_compatible_match_id:
*
* Like device_compatible_match(), but takes a single
* unsigned integer device ID.
*/
int
device_compatible_match_id(
uintptr_t const id, uintptr_t const sentinel_id,
const struct device_compatible_entry *driver_compats)
{
return device_compatible_match_id_internal(id, (uintptr_t)-1,
sentinel_id, driver_compats, NULL);
}
/*
* device_compatible_lookup:
*
* Look up and return the device_compatible_entry, using the
* same matching criteria used by device_compatible_match().
*/
const struct device_compatible_entry *
device_compatible_lookup(const char **device_compats, int ndevice_compats,
const struct device_compatible_entry *driver_compats)
{
const struct device_compatible_entry *dce;
if (device_compatible_match_strarray_internal(device_compats,
ndevice_compats, driver_compats, &dce, strarray_match)) {
return dce;
}
return NULL;
}
/*
* device_compatible_plookup:
*
* Look up and return the device_compatible_entry, using the
* same matching criteria used by device_compatible_pmatch().
*/
const struct device_compatible_entry *
device_compatible_plookup(const char **device_compats, int ndevice_compats,
const struct device_compatible_entry *driver_compats)
{
const struct device_compatible_entry *dce;
if (device_compatible_match_strarray_internal(device_compats,
ndevice_compats, driver_compats, &dce, strarray_pmatch)) {
return dce;
}
return NULL;
}
/*
* device_compatible_lookup_strlist:
*
* Like device_compatible_lookup(), but take the device
* "compatible" strings as an OpenFirmware-style string
* list.
*/
const struct device_compatible_entry *
device_compatible_lookup_strlist(
const char * const device_compats, size_t const device_compatsize,
const struct device_compatible_entry *driver_compats)
{
const struct device_compatible_entry *dce;
if (device_compatible_match_strlist_internal(device_compats,
device_compatsize, driver_compats, &dce, strlist_match)) {
return dce;
}
return NULL;
}
/*
* device_compatible_plookup_strlist:
*
* Like device_compatible_plookup(), but take the device
* "compatible" strings as an OpenFirmware-style string
* list.
*/
const struct device_compatible_entry *
device_compatible_plookup_strlist(
const char * const device_compats, size_t const device_compatsize,
const struct device_compatible_entry *driver_compats)
{
const struct device_compatible_entry *dce;
if (device_compatible_match_strlist_internal(device_compats,
device_compatsize, driver_compats, &dce, strlist_pmatch)) {
return dce;
}
return NULL;
}
/*
* device_compatible_lookup_id:
*
* Like device_compatible_lookup(), but takes a single
* unsigned integer device ID.
*/
const struct device_compatible_entry *
device_compatible_lookup_id(
uintptr_t const id, uintptr_t const sentinel_id,
const struct device_compatible_entry *driver_compats)
{
const struct device_compatible_entry *dce;
if (device_compatible_match_id_internal(id, (uintptr_t)-1,
sentinel_id, driver_compats, &dce)) {
return dce;
}
return NULL;
}
/*
* Power management related functions.
*/
bool
device_pmf_is_registered(device_t dev)
{
return (dev->dv_flags & DVF_POWER_HANDLERS) != 0;
}
bool
device_pmf_driver_suspend(device_t dev, const pmf_qual_t *qual)
{
if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
return true;
if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
return false;
if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
dev->dv_driver_suspend != NULL &&
!(*dev->dv_driver_suspend)(dev, qual))
return false;
dev->dv_flags |= DVF_DRIVER_SUSPENDED;
return true;
}
bool
device_pmf_driver_resume(device_t dev, const pmf_qual_t *qual)
{
if ((dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
return true;
if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
return false;
if (pmf_qual_depth(qual) <= DEVACT_LEVEL_DRIVER &&
dev->dv_driver_resume != NULL &&
!(*dev->dv_driver_resume)(dev, qual))
return false;
dev->dv_flags &= ~DVF_DRIVER_SUSPENDED;
return true;
}
bool
device_pmf_driver_shutdown(device_t dev, int how)
{
if (*dev->dv_driver_shutdown != NULL &&
!(*dev->dv_driver_shutdown)(dev, how))
return false;
return true;
}
void
device_pmf_driver_register(device_t dev,
bool (*suspend)(device_t, const pmf_qual_t *),
bool (*resume)(device_t, const pmf_qual_t *),
bool (*shutdown)(device_t, int))
{
dev->dv_driver_suspend = suspend;
dev->dv_driver_resume = resume;
dev->dv_driver_shutdown = shutdown;
dev->dv_flags |= DVF_POWER_HANDLERS;
}
void
device_pmf_driver_deregister(device_t dev)
{
device_lock_t dvl = device_getlock(dev);
dev->dv_driver_suspend = NULL;
dev->dv_driver_resume = NULL;
mutex_enter(&dvl->dvl_mtx);
dev->dv_flags &= ~DVF_POWER_HANDLERS;
while (dvl->dvl_nlock > 0 || dvl->dvl_nwait > 0) {
/* Wake a thread that waits for the lock. That
* thread will fail to acquire the lock, and then
* it will wake the next thread that waits for the
* lock, or else it will wake us.
*/
cv_signal(&dvl->dvl_cv);
pmflock_debug(dev, __func__, __LINE__);
cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
pmflock_debug(dev, __func__, __LINE__);
}
mutex_exit(&dvl->dvl_mtx);
}
void
device_pmf_driver_child_register(device_t dev)
{
device_t parent = device_parent(dev);
if (parent == NULL || parent->dv_driver_child_register == NULL)
return;
(*parent->dv_driver_child_register)(dev);
}
void
device_pmf_driver_set_child_register(device_t dev,
void (*child_register)(device_t))
{
dev->dv_driver_child_register = child_register;
}
static void
pmflock_debug(device_t dev, const char *func, int line)
{
#ifdef PMFLOCK_DEBUG
device_lock_t dvl = device_getlock(dev);
const char *curlwp_name;
if (curlwp->l_name != NULL)
curlwp_name = curlwp->l_name;
else
curlwp_name = curlwp->l_proc->p_comm;
aprint_debug_dev(dev,
"%s.%d, %s dvl_nlock %d dvl_nwait %d dv_flags %x\n", func, line,
curlwp_name, dvl->dvl_nlock, dvl->dvl_nwait, dev->dv_flags);
#endif /* PMFLOCK_DEBUG */
}
static bool
device_pmf_lock1(device_t dev)
{
device_lock_t dvl = device_getlock(dev);
while (device_pmf_is_registered(dev) &&
dvl->dvl_nlock > 0 && dvl->dvl_holder != curlwp) {
dvl->dvl_nwait++;
pmflock_debug(dev, __func__, __LINE__);
cv_wait(&dvl->dvl_cv, &dvl->dvl_mtx);
pmflock_debug(dev, __func__, __LINE__);
dvl->dvl_nwait--;
}
if (!device_pmf_is_registered(dev)) {
pmflock_debug(dev, __func__, __LINE__);
/* We could not acquire the lock, but some other thread may
* wait for it, also. Wake that thread.
*/
cv_signal(&dvl->dvl_cv);
return false;
}
dvl->dvl_nlock++;
dvl->dvl_holder = curlwp;
pmflock_debug(dev, __func__, __LINE__);
return true;
}
bool
device_pmf_lock(device_t dev)
{
bool rc;
device_lock_t dvl = device_getlock(dev);
mutex_enter(&dvl->dvl_mtx);
rc = device_pmf_lock1(dev);
mutex_exit(&dvl->dvl_mtx);
return rc;
}
void
device_pmf_unlock(device_t dev)
{
device_lock_t dvl = device_getlock(dev);
KASSERT(dvl->dvl_nlock > 0);
mutex_enter(&dvl->dvl_mtx);
if (--dvl->dvl_nlock == 0)
dvl->dvl_holder = NULL;
cv_signal(&dvl->dvl_cv);
pmflock_debug(dev, __func__, __LINE__);
mutex_exit(&dvl->dvl_mtx);
}
device_lock_t
device_getlock(device_t dev)
{
return &dev->dv_lock;
}
void *
device_pmf_bus_private(device_t dev)
{
return dev->dv_bus_private;
}
bool
device_pmf_bus_suspend(device_t dev, const pmf_qual_t *qual)
{
if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0)
return true;
if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0 ||
(dev->dv_flags & DVF_DRIVER_SUSPENDED) == 0)
return false;
if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
dev->dv_bus_suspend != NULL &&
!(*dev->dv_bus_suspend)(dev, qual))
return false;
dev->dv_flags |= DVF_BUS_SUSPENDED;
return true;
}
bool
device_pmf_bus_resume(device_t dev, const pmf_qual_t *qual)
{
if ((dev->dv_flags & DVF_BUS_SUSPENDED) == 0)
return true;
if (pmf_qual_depth(qual) <= DEVACT_LEVEL_BUS &&
dev->dv_bus_resume != NULL &&
!(*dev->dv_bus_resume)(dev, qual))
return false;
dev->dv_flags &= ~DVF_BUS_SUSPENDED;
return true;
}
bool
device_pmf_bus_shutdown(device_t dev, int how)
{
if (*dev->dv_bus_shutdown != NULL &&
!(*dev->dv_bus_shutdown)(dev, how))
return false;
return true;
}
void
device_pmf_bus_register(device_t dev, void *priv,
bool (*suspend)(device_t, const pmf_qual_t *),
bool (*resume)(device_t, const pmf_qual_t *),
bool (*shutdown)(device_t, int), void (*deregister)(device_t))
{
dev->dv_bus_private = priv;
dev->dv_bus_resume = resume;
dev->dv_bus_suspend = suspend;
dev->dv_bus_shutdown = shutdown;
dev->dv_bus_deregister = deregister;
}
void
device_pmf_bus_deregister(device_t dev)
{
if (dev->dv_bus_deregister == NULL)
return;
(*dev->dv_bus_deregister)(dev);
dev->dv_bus_private = NULL;
dev->dv_bus_suspend = NULL;
dev->dv_bus_resume = NULL;
dev->dv_bus_deregister = NULL;
}
void *
device_pmf_class_private(device_t dev)
{
return dev->dv_class_private;
}
bool
device_pmf_class_suspend(device_t dev, const pmf_qual_t *qual)
{
if ((dev->dv_flags & DVF_CLASS_SUSPENDED) != 0)
return true;
if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
dev->dv_class_suspend != NULL &&
!(*dev->dv_class_suspend)(dev, qual))
return false;
dev->dv_flags |= DVF_CLASS_SUSPENDED;
return true;
}
bool
device_pmf_class_resume(device_t dev, const pmf_qual_t *qual)
{
if ((dev->dv_flags & DVF_CLASS_SUSPENDED) == 0)
return true;
if ((dev->dv_flags & DVF_BUS_SUSPENDED) != 0 ||
(dev->dv_flags & DVF_DRIVER_SUSPENDED) != 0)
return false;
if (pmf_qual_depth(qual) <= DEVACT_LEVEL_CLASS &&
dev->dv_class_resume != NULL &&
!(*dev->dv_class_resume)(dev, qual))
return false;
dev->dv_flags &= ~DVF_CLASS_SUSPENDED;
return true;
}
void
device_pmf_class_register(device_t dev, void *priv,
bool (*suspend)(device_t, const pmf_qual_t *),
bool (*resume)(device_t, const pmf_qual_t *),
void (*deregister)(device_t))
{
dev->dv_class_private = priv;
dev->dv_class_suspend = suspend;
dev->dv_class_resume = resume;
dev->dv_class_deregister = deregister;
}
void
device_pmf_class_deregister(device_t dev)
{
if (dev->dv_class_deregister == NULL)
return;
(*dev->dv_class_deregister)(dev);
dev->dv_class_private = NULL;
dev->dv_class_suspend = NULL;
dev->dv_class_resume = NULL;
dev->dv_class_deregister = NULL;
}
bool
device_active(device_t dev, devactive_t type)
{
size_t i;
if (dev->dv_activity_count == 0)
return false;
for (i = 0; i < dev->dv_activity_count; ++i) {
if (dev->dv_activity_handlers[i] == NULL)
break;
(*dev->dv_activity_handlers[i])(dev, type);
}
return true;
}
bool
device_active_register(device_t dev, void (*handler)(device_t, devactive_t))
{
void (**new_handlers)(device_t, devactive_t);
void (**old_handlers)(device_t, devactive_t);
size_t i, old_size, new_size;
int s;
old_handlers = dev->dv_activity_handlers;
old_size = dev->dv_activity_count;
KASSERT(old_size == 0 || old_handlers != NULL);
for (i = 0; i < old_size; ++i) {
KASSERT(old_handlers[i] != handler);
if (old_handlers[i] == NULL) {
old_handlers[i] = handler;
return true;
}
}
new_size = old_size + 4;
new_handlers = kmem_alloc(sizeof(void *) * new_size, KM_SLEEP);
for (i = 0; i < old_size; ++i)
new_handlers[i] = old_handlers[i];
new_handlers[old_size] = handler;
for (i = old_size+1; i < new_size; ++i)
new_handlers[i] = NULL;
s = splhigh();
dev->dv_activity_count = new_size;
dev->dv_activity_handlers = new_handlers;
splx(s);
if (old_size > 0)
kmem_free(old_handlers, sizeof(void *) * old_size);
return true;
}
void
device_active_deregister(device_t dev, void (*handler)(device_t, devactive_t))
{
void (**old_handlers)(device_t, devactive_t);
size_t i, old_size;
int s;
old_handlers = dev->dv_activity_handlers;
old_size = dev->dv_activity_count;
for (i = 0; i < old_size; ++i) {
if (old_handlers[i] == handler)
break;
if (old_handlers[i] == NULL)
return; /* XXX panic? */
}
if (i == old_size)
return; /* XXX panic? */
for (; i < old_size - 1; ++i) {
if ((old_handlers[i] = old_handlers[i + 1]) != NULL)
continue;
if (i == 0) {
s = splhigh();
dev->dv_activity_count = 0;
dev->dv_activity_handlers = NULL;
splx(s);
kmem_free(old_handlers, sizeof(void *) * old_size);
}
return;
}
old_handlers[i] = NULL;
}
/* Return true iff the device_t `dev' exists at generation `gen'. */
static bool
device_exists_at(device_t dv, devgen_t gen)
{
return (dv->dv_del_gen == 0 || dv->dv_del_gen > gen) &&
dv->dv_add_gen <= gen;
}
static bool
deviter_visits(const deviter_t *di, device_t dv)
{
return device_exists_at(dv, di->di_gen);
}
/*
* Device Iteration
*
* deviter_t: a device iterator. Holds state for a "walk" visiting
* each device_t's in the device tree.
*
* deviter_init(di, flags): initialize the device iterator `di'
* to "walk" the device tree. deviter_next(di) will return
* the first device_t in the device tree, or NULL if there are
* no devices.
*
* `flags' is one or more of DEVITER_F_RW, indicating that the
* caller intends to modify the device tree by calling
* config_detach(9) on devices in the order that the iterator
* returns them; DEVITER_F_ROOT_FIRST, asking for the devices
* nearest the "root" of the device tree to be returned, first;
* DEVITER_F_LEAVES_FIRST, asking for the devices furthest from
* the root of the device tree, first; and DEVITER_F_SHUTDOWN,
* indicating both that deviter_init() should not respect any
* locks on the device tree, and that deviter_next(di) may run
* in more than one LWP before the walk has finished.
*
* Only one DEVITER_F_RW iterator may be in the device tree at
* once.
*
* DEVITER_F_SHUTDOWN implies DEVITER_F_RW.
*
* Results are undefined if the flags DEVITER_F_ROOT_FIRST and
* DEVITER_F_LEAVES_FIRST are used in combination.
*
* deviter_first(di, flags): initialize the device iterator `di'
* and return the first device_t in the device tree, or NULL
* if there are no devices. The statement
*
* dv = deviter_first(di);
*
* is shorthand for
*
* deviter_init(di);
* dv = deviter_next(di);
*
* deviter_next(di): return the next device_t in the device tree,
* or NULL if there are no more devices. deviter_next(di)
* is undefined if `di' was not initialized with deviter_init() or
* deviter_first().
*
* deviter_release(di): stops iteration (subsequent calls to
* deviter_next() will return NULL), releases any locks and
* resources held by the device iterator.
*
* Device iteration does not return device_t's in any particular
* order. An iterator will never return the same device_t twice.
* Device iteration is guaranteed to complete---i.e., if deviter_next(di)
* is called repeatedly on the same `di', it will eventually return
* NULL. It is ok to attach/detach devices during device iteration.
*/
void
deviter_init(deviter_t *di, deviter_flags_t flags)
{
device_t dv;
memset(di, 0, sizeof(*di));
if ((flags & DEVITER_F_SHUTDOWN) != 0)
flags |= DEVITER_F_RW;
mutex_enter(&alldevs_lock);
if ((flags & DEVITER_F_RW) != 0)
alldevs_nwrite++;
else
alldevs_nread++;
di->di_gen = alldevs_gen++;
di->di_flags = flags;
switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
case DEVITER_F_LEAVES_FIRST:
TAILQ_FOREACH(dv, &alldevs, dv_list) {
if (!deviter_visits(di, dv))
continue;
di->di_curdepth = MAX(di->di_curdepth, dv->dv_depth);
}
break;
case DEVITER_F_ROOT_FIRST:
TAILQ_FOREACH(dv, &alldevs, dv_list) {
if (!deviter_visits(di, dv))
continue;
di->di_maxdepth = MAX(di->di_maxdepth, dv->dv_depth);
}
break;
default:
break;
}
deviter_reinit(di);
mutex_exit(&alldevs_lock);
}
static void
deviter_reinit(deviter_t *di)
{
KASSERT(mutex_owned(&alldevs_lock));
if ((di->di_flags & DEVITER_F_RW) != 0)
di->di_prev = TAILQ_LAST(&alldevs, devicelist);
else
di->di_prev = TAILQ_FIRST(&alldevs);
}
device_t
deviter_first(deviter_t *di, deviter_flags_t flags)
{
deviter_init(di, flags);
return deviter_next(di);
}
static device_t
deviter_next2(deviter_t *di)
{
device_t dv;
KASSERT(mutex_owned(&alldevs_lock));
dv = di->di_prev;
if (dv == NULL)
return NULL;
if ((di->di_flags & DEVITER_F_RW) != 0)
di->di_prev = TAILQ_PREV(dv, devicelist, dv_list);
else
di->di_prev = TAILQ_NEXT(dv, dv_list);
return dv;
}
static device_t
deviter_next1(deviter_t *di)
{
device_t dv;
KASSERT(mutex_owned(&alldevs_lock));
do {
dv = deviter_next2(di);
} while (dv != NULL && !deviter_visits(di, dv));
return dv;
}
device_t
deviter_next(deviter_t *di)
{
device_t dv = NULL;
mutex_enter(&alldevs_lock);
switch (di->di_flags & (DEVITER_F_LEAVES_FIRST|DEVITER_F_ROOT_FIRST)) {
case 0:
dv = deviter_next1(di);
break;
case DEVITER_F_LEAVES_FIRST:
while (di->di_curdepth >= 0) {
if ((dv = deviter_next1(di)) == NULL) {
di->di_curdepth--;
deviter_reinit(di);
} else if (dv->dv_depth == di->di_curdepth)
break;
}
break;
case DEVITER_F_ROOT_FIRST:
while (di->di_curdepth <= di->di_maxdepth) {
if ((dv = deviter_next1(di)) == NULL) {
di->di_curdepth++;
deviter_reinit(di);
} else if (dv->dv_depth == di->di_curdepth)
break;
}
break;
default:
break;
}
mutex_exit(&alldevs_lock);
return dv;
}
void
deviter_release(deviter_t *di)
{
bool rw = (di->di_flags & DEVITER_F_RW) != 0;
mutex_enter(&alldevs_lock);
if (rw)
--alldevs_nwrite;
else
--alldevs_nread;
/* XXX wake a garbage-collection thread */
mutex_exit(&alldevs_lock);
}
const char *
cfdata_ifattr(const struct cfdata *cf)
{
return cf->cf_pspec->cfp_iattr;
}
bool
ifattr_match(const char *snull, const char *t)
{
return (snull == NULL) || strcmp(snull, t) == 0;
}
void
null_childdetached(device_t self, device_t child)
{
/* do nothing */
}
static void
sysctl_detach_setup(struct sysctllog **clog)
{
sysctl_createv(clog, 0, NULL, NULL,
CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
CTLTYPE_BOOL, "detachall",
SYSCTL_DESCR("Detach all devices at shutdown"),
NULL, 0, &detachall, 0,
CTL_KERN, CTL_CREATE, CTL_EOL);
}
|