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
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <sys/note.h>
#include <sys/t_lock.h>
#include <sys/cmn_err.h>
#include <sys/instance.h>
#include <sys/conf.h>
#include <sys/stat.h>
#include <sys/ddi.h>
#include <sys/hwconf.h>
#include <sys/sunddi.h>
#include <sys/sunndi.h>
#include <sys/ddi_impldefs.h>
#include <sys/ndi_impldefs.h>
#include <sys/modctl.h>
#include <sys/contract/device_impl.h>
#include <sys/dacf.h>
#include <sys/promif.h>
#include <sys/pci.h>
#include <sys/cpuvar.h>
#include <sys/pathname.h>
#include <sys/taskq.h>
#include <sys/sysevent.h>
#include <sys/sunmdi.h>
#include <sys/stream.h>
#include <sys/strsubr.h>
#include <sys/fs/snode.h>
#include <sys/fs/dv_node.h>
#include <sys/reboot.h>
#include <sys/sysmacros.h>
#include <sys/systm.h>
#include <sys/fs/sdev_impl.h>
#include <sys/sunldi.h>
#include <sys/sunldi_impl.h>
#include <sys/bootprops.h>
#if defined(__amd64) && !defined(__xpv)
#include <sys/iommulib.h>
#endif
/* XXX remove before putback */
boolean_t ddi_err_panic = B_TRUE;
#ifdef DEBUG
int ddidebug = DDI_AUDIT;
#else
int ddidebug = 0;
#endif
#define MT_CONFIG_OP 0
#define MT_UNCONFIG_OP 1
/* Multi-threaded configuration */
struct mt_config_handle {
kmutex_t mtc_lock;
kcondvar_t mtc_cv;
int mtc_thr_count;
dev_info_t *mtc_pdip; /* parent dip for mt_config_children */
dev_info_t **mtc_fdip; /* "a" dip where unconfigure failed */
major_t mtc_parmajor; /* parent major for mt_config_driver */
major_t mtc_major;
int mtc_flags;
int mtc_op; /* config or unconfig */
int mtc_error; /* operation error */
struct brevq_node **mtc_brevqp; /* outstanding branch events queue */
#ifdef DEBUG
int total_time;
timestruc_t start_time;
#endif /* DEBUG */
};
struct devi_nodeid {
pnode_t nodeid;
dev_info_t *dip;
struct devi_nodeid *next;
};
struct devi_nodeid_list {
kmutex_t dno_lock; /* Protects other fields */
struct devi_nodeid *dno_head; /* list of devi nodeid elements */
struct devi_nodeid *dno_free; /* Free list */
uint_t dno_list_length; /* number of dips in list */
};
/* used to keep track of branch remove events to be generated */
struct brevq_node {
char *brn_deviname;
struct brevq_node *brn_sibling;
struct brevq_node *brn_child;
};
static struct devi_nodeid_list devi_nodeid_list;
static struct devi_nodeid_list *devimap = &devi_nodeid_list;
/*
* Well known nodes which are attached first at boot time.
*/
dev_info_t *top_devinfo; /* root of device tree */
dev_info_t *options_dip;
dev_info_t *pseudo_dip;
dev_info_t *clone_dip;
dev_info_t *scsi_vhci_dip; /* MPXIO dip */
major_t clone_major;
/*
* A non-global zone's /dev is derived from the device tree.
* This generation number serves to indicate when a zone's
* /dev may need to be updated.
*/
volatile ulong_t devtree_gen; /* generation number */
/* block all future dev_info state changes */
hrtime_t volatile devinfo_freeze = 0;
/* number of dev_info attaches/detaches currently in progress */
static ulong_t devinfo_attach_detach = 0;
extern int sys_shutdown;
extern kmutex_t global_vhci_lock;
/* bitset of DS_SYSAVAIL & DS_RECONFIG - no races, no lock */
static int devname_state = 0;
/*
* The devinfo snapshot cache and related variables.
* The only field in the di_cache structure that needs initialization
* is the mutex (cache_lock). However, since this is an adaptive mutex
* (MUTEX_DEFAULT) - it is automatically initialized by being allocated
* in zeroed memory (static storage class). Therefore no explicit
* initialization of the di_cache structure is needed.
*/
struct di_cache di_cache = {1};
int di_cache_debug = 0;
/* For ddvis, which needs pseudo children under PCI */
int pci_allow_pseudo_children = 0;
/* Allow path-oriented alias driver binding on driver.conf enumerated nodes */
int driver_conf_allow_path_alias = 1;
/*
* The following switch is for service people, in case a
* 3rd party driver depends on identify(9e) being called.
*/
int identify_9e = 0;
int mtc_off; /* turn off mt config */
int quiesce_debug = 0;
static kmem_cache_t *ddi_node_cache; /* devinfo node cache */
static devinfo_log_header_t *devinfo_audit_log; /* devinfo log */
static int devinfo_log_size; /* size in pages */
static int lookup_compatible(dev_info_t *, uint_t);
static char *encode_composite_string(char **, uint_t, size_t *, uint_t);
static void link_to_driver_list(dev_info_t *);
static void unlink_from_driver_list(dev_info_t *);
static void add_to_dn_list(struct devnames *, dev_info_t *);
static void remove_from_dn_list(struct devnames *, dev_info_t *);
static dev_info_t *find_duplicate_child();
static void add_global_props(dev_info_t *);
static void remove_global_props(dev_info_t *);
static int uninit_node(dev_info_t *);
static void da_log_init(void);
static void da_log_enter(dev_info_t *);
static int walk_devs(dev_info_t *, int (*f)(dev_info_t *, void *), void *, int);
static int reset_nexus_flags(dev_info_t *, void *);
static void ddi_optimize_dtree(dev_info_t *);
static int is_leaf_node(dev_info_t *);
static struct mt_config_handle *mt_config_init(dev_info_t *, dev_info_t **,
int, major_t, int, struct brevq_node **);
static void mt_config_children(struct mt_config_handle *);
static void mt_config_driver(struct mt_config_handle *);
static int mt_config_fini(struct mt_config_handle *);
static int devi_unconfig_common(dev_info_t *, dev_info_t **, int, major_t,
struct brevq_node **);
static int
ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
dev_info_t **childp, int flags);
static void i_link_vhci_node(dev_info_t *);
static void ndi_devi_exit_and_wait(dev_info_t *dip,
int circular, clock_t end_time);
static int ndi_devi_unbind_driver(dev_info_t *dip);
static void i_ddi_check_retire(dev_info_t *dip);
static void quiesce_one_device(dev_info_t *, void *);
/*
* dev_info cache and node management
*/
/* initialize dev_info node cache */
void
i_ddi_node_cache_init()
{
ASSERT(ddi_node_cache == NULL);
ddi_node_cache = kmem_cache_create("dev_info_node_cache",
sizeof (struct dev_info), 0, NULL, NULL, NULL, NULL, NULL, 0);
if (ddidebug & DDI_AUDIT)
da_log_init();
}
/*
* Allocating a dev_info node, callable from interrupt context with KM_NOSLEEP
* The allocated node has a reference count of 0.
*/
dev_info_t *
i_ddi_alloc_node(dev_info_t *pdip, char *node_name, pnode_t nodeid,
int instance, ddi_prop_t *sys_prop, int flag)
{
struct dev_info *devi;
struct devi_nodeid *elem;
static char failed[] = "i_ddi_alloc_node: out of memory";
ASSERT(node_name != NULL);
if ((devi = kmem_cache_alloc(ddi_node_cache, flag)) == NULL) {
cmn_err(CE_NOTE, failed);
return (NULL);
}
bzero(devi, sizeof (struct dev_info));
if (devinfo_audit_log) {
devi->devi_audit = kmem_zalloc(sizeof (devinfo_audit_t), flag);
if (devi->devi_audit == NULL)
goto fail;
}
if ((devi->devi_node_name = i_ddi_strdup(node_name, flag)) == NULL)
goto fail;
/* default binding name is node name */
devi->devi_binding_name = devi->devi_node_name;
devi->devi_major = DDI_MAJOR_T_NONE; /* unbound by default */
/*
* Make a copy of system property
*/
if (sys_prop &&
(devi->devi_sys_prop_ptr = i_ddi_prop_list_dup(sys_prop, flag))
== NULL)
goto fail;
/*
* Assign devi_nodeid, devi_node_class, devi_node_attributes
* according to the following algorithm:
*
* nodeid arg node class node attributes
*
* DEVI_PSEUDO_NODEID DDI_NC_PSEUDO A
* DEVI_SID_NODEID DDI_NC_PSEUDO A,P
* DEVI_SID_HIDDEN_NODEID DDI_NC_PSEUDO A,P,H
* DEVI_SID_HP_NODEID DDI_NC_PSEUDO A,P,h
* DEVI_SID_HP_HIDDEN_NODEID DDI_NC_PSEUDO A,P,H,h
* other DDI_NC_PROM P
*
* Where A = DDI_AUTO_ASSIGNED_NODEID (auto-assign a nodeid)
* and P = DDI_PERSISTENT
* and H = DDI_HIDDEN_NODE
* and h = DDI_HOTPLUG_NODE
*
* auto-assigned nodeids are also auto-freed.
*/
devi->devi_node_attributes = 0;
switch (nodeid) {
case DEVI_SID_HIDDEN_NODEID:
devi->devi_node_attributes |= DDI_HIDDEN_NODE;
goto sid;
case DEVI_SID_HP_NODEID:
devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
goto sid;
case DEVI_SID_HP_HIDDEN_NODEID:
devi->devi_node_attributes |= DDI_HIDDEN_NODE;
devi->devi_node_attributes |= DDI_HOTPLUG_NODE;
goto sid;
case DEVI_SID_NODEID:
sid: devi->devi_node_attributes |= DDI_PERSISTENT;
if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
goto fail;
/*FALLTHROUGH*/
case DEVI_PSEUDO_NODEID:
devi->devi_node_attributes |= DDI_AUTO_ASSIGNED_NODEID;
devi->devi_node_class = DDI_NC_PSEUDO;
if (impl_ddi_alloc_nodeid(&devi->devi_nodeid)) {
panic("i_ddi_alloc_node: out of nodeids");
/*NOTREACHED*/
}
break;
default:
if ((elem = kmem_zalloc(sizeof (*elem), flag)) == NULL)
goto fail;
/*
* the nodetype is 'prom', try to 'take' the nodeid now.
* This requires memory allocation, so check for failure.
*/
if (impl_ddi_take_nodeid(nodeid, flag) != 0) {
kmem_free(elem, sizeof (*elem));
goto fail;
}
devi->devi_nodeid = nodeid;
devi->devi_node_class = DDI_NC_PROM;
devi->devi_node_attributes = DDI_PERSISTENT;
break;
}
if (ndi_dev_is_persistent_node((dev_info_t *)devi)) {
mutex_enter(&devimap->dno_lock);
elem->next = devimap->dno_free;
devimap->dno_free = elem;
mutex_exit(&devimap->dno_lock);
}
/*
* Instance is normally initialized to -1. In a few special
* cases, the caller may specify an instance (e.g. CPU nodes).
*/
devi->devi_instance = instance;
/*
* set parent and bus_ctl parent
*/
devi->devi_parent = DEVI(pdip);
devi->devi_bus_ctl = DEVI(pdip);
NDI_CONFIG_DEBUG((CE_CONT,
"i_ddi_alloc_node: name=%s id=%d\n", node_name, devi->devi_nodeid));
cv_init(&(devi->devi_cv), NULL, CV_DEFAULT, NULL);
mutex_init(&(devi->devi_lock), NULL, MUTEX_DEFAULT, NULL);
mutex_init(&(devi->devi_pm_lock), NULL, MUTEX_DEFAULT, NULL);
mutex_init(&(devi->devi_pm_busy_lock), NULL, MUTEX_DEFAULT, NULL);
RIO_TRACE((CE_NOTE, "i_ddi_alloc_node: Initing contract fields: "
"dip=%p, name=%s", (void *)devi, node_name));
mutex_init(&(devi->devi_ct_lock), NULL, MUTEX_DEFAULT, NULL);
cv_init(&(devi->devi_ct_cv), NULL, CV_DEFAULT, NULL);
devi->devi_ct_count = -1; /* counter not in use if -1 */
list_create(&(devi->devi_ct), sizeof (cont_device_t),
offsetof(cont_device_t, cond_next));
i_ddi_set_node_state((dev_info_t *)devi, DS_PROTO);
da_log_enter((dev_info_t *)devi);
return ((dev_info_t *)devi);
fail:
if (devi->devi_sys_prop_ptr)
i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
if (devi->devi_node_name)
kmem_free(devi->devi_node_name, strlen(node_name) + 1);
if (devi->devi_audit)
kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
kmem_cache_free(ddi_node_cache, devi);
cmn_err(CE_NOTE, failed);
return (NULL);
}
/*
* free a dev_info structure.
* NB. Not callable from interrupt since impl_ddi_free_nodeid may block.
*/
void
i_ddi_free_node(dev_info_t *dip)
{
struct dev_info *devi = DEVI(dip);
struct devi_nodeid *elem;
ASSERT(devi->devi_ref == 0);
ASSERT(devi->devi_addr == NULL);
ASSERT(devi->devi_node_state == DS_PROTO);
ASSERT(devi->devi_child == NULL);
ASSERT(devi->devi_hp_hdlp == NULL);
/* free devi_addr_buf allocated by ddi_set_name_addr() */
if (devi->devi_addr_buf)
kmem_free(devi->devi_addr_buf, 2 * MAXNAMELEN);
if (i_ndi_dev_is_auto_assigned_node(dip))
impl_ddi_free_nodeid(DEVI(dip)->devi_nodeid);
if (ndi_dev_is_persistent_node(dip)) {
mutex_enter(&devimap->dno_lock);
ASSERT(devimap->dno_free);
elem = devimap->dno_free;
devimap->dno_free = elem->next;
mutex_exit(&devimap->dno_lock);
kmem_free(elem, sizeof (*elem));
}
if (DEVI(dip)->devi_compat_names)
kmem_free(DEVI(dip)->devi_compat_names,
DEVI(dip)->devi_compat_length);
if (DEVI(dip)->devi_rebinding_name)
kmem_free(DEVI(dip)->devi_rebinding_name,
strlen(DEVI(dip)->devi_rebinding_name) + 1);
ddi_prop_remove_all(dip); /* remove driver properties */
if (devi->devi_sys_prop_ptr)
i_ddi_prop_list_delete(devi->devi_sys_prop_ptr);
if (devi->devi_hw_prop_ptr)
i_ddi_prop_list_delete(devi->devi_hw_prop_ptr);
if (DEVI(dip)->devi_devid_str)
ddi_devid_str_free(DEVI(dip)->devi_devid_str);
i_ddi_set_node_state(dip, DS_INVAL);
da_log_enter(dip);
if (devi->devi_audit) {
kmem_free(devi->devi_audit, sizeof (devinfo_audit_t));
}
if (devi->devi_device_class)
kmem_free(devi->devi_device_class,
strlen(devi->devi_device_class) + 1);
cv_destroy(&(devi->devi_cv));
mutex_destroy(&(devi->devi_lock));
mutex_destroy(&(devi->devi_pm_lock));
mutex_destroy(&(devi->devi_pm_busy_lock));
RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroying contract fields: "
"dip=%p", (void *)dip));
contract_device_remove_dip(dip);
ASSERT(devi->devi_ct_count == -1);
ASSERT(list_is_empty(&(devi->devi_ct)));
cv_destroy(&(devi->devi_ct_cv));
list_destroy(&(devi->devi_ct));
/* free this last since contract_device_remove_dip() uses it */
mutex_destroy(&(devi->devi_ct_lock));
RIO_TRACE((CE_NOTE, "i_ddi_free_node: destroyed all contract fields: "
"dip=%p, name=%s", (void *)dip, devi->devi_node_name));
kmem_free(devi->devi_node_name, strlen(devi->devi_node_name) + 1);
kmem_cache_free(ddi_node_cache, devi);
}
/*
* Node state transitions
*/
/*
* Change the node name
*/
int
ndi_devi_set_nodename(dev_info_t *dip, char *name, int flags)
{
_NOTE(ARGUNUSED(flags))
char *nname, *oname;
ASSERT(dip && name);
oname = DEVI(dip)->devi_node_name;
if (strcmp(oname, name) == 0)
return (DDI_SUCCESS);
/*
* pcicfg_fix_ethernet requires a name change after node
* is linked into the tree. When pcicfg is fixed, we
* should only allow name change in DS_PROTO state.
*/
if (i_ddi_node_state(dip) >= DS_BOUND) {
/*
* Don't allow name change once node is bound
*/
cmn_err(CE_NOTE,
"ndi_devi_set_nodename: node already bound dip = %p,"
" %s -> %s", (void *)dip, ddi_node_name(dip), name);
return (NDI_FAILURE);
}
nname = i_ddi_strdup(name, KM_SLEEP);
DEVI(dip)->devi_node_name = nname;
i_ddi_set_binding_name(dip, nname);
kmem_free(oname, strlen(oname) + 1);
da_log_enter(dip);
return (NDI_SUCCESS);
}
void
i_ddi_add_devimap(dev_info_t *dip)
{
struct devi_nodeid *elem;
ASSERT(dip);
if (!ndi_dev_is_persistent_node(dip))
return;
ASSERT(ddi_get_parent(dip) == NULL || (DEVI_VHCI_NODE(dip)) ||
DEVI_BUSY_OWNED(ddi_get_parent(dip)));
mutex_enter(&devimap->dno_lock);
ASSERT(devimap->dno_free);
elem = devimap->dno_free;
devimap->dno_free = elem->next;
elem->nodeid = ddi_get_nodeid(dip);
elem->dip = dip;
elem->next = devimap->dno_head;
devimap->dno_head = elem;
devimap->dno_list_length++;
mutex_exit(&devimap->dno_lock);
}
static int
i_ddi_remove_devimap(dev_info_t *dip)
{
struct devi_nodeid *prev, *elem;
static const char *fcn = "i_ddi_remove_devimap";
ASSERT(dip);
if (!ndi_dev_is_persistent_node(dip))
return (DDI_SUCCESS);
mutex_enter(&devimap->dno_lock);
/*
* The following check is done with dno_lock held
* to prevent race between dip removal and
* e_ddi_prom_node_to_dip()
*/
if (e_ddi_devi_holdcnt(dip)) {
mutex_exit(&devimap->dno_lock);
return (DDI_FAILURE);
}
ASSERT(devimap->dno_head);
ASSERT(devimap->dno_list_length > 0);
prev = NULL;
for (elem = devimap->dno_head; elem; elem = elem->next) {
if (elem->dip == dip) {
ASSERT(elem->nodeid == ddi_get_nodeid(dip));
break;
}
prev = elem;
}
if (elem && prev)
prev->next = elem->next;
else if (elem)
devimap->dno_head = elem->next;
else
panic("%s: devinfo node(%p) not found",
fcn, (void *)dip);
devimap->dno_list_length--;
elem->nodeid = 0;
elem->dip = NULL;
elem->next = devimap->dno_free;
devimap->dno_free = elem;
mutex_exit(&devimap->dno_lock);
return (DDI_SUCCESS);
}
/*
* Link this node into the devinfo tree and add to orphan list
* Not callable from interrupt context
*/
static void
link_node(dev_info_t *dip)
{
struct dev_info *devi = DEVI(dip);
struct dev_info *parent = devi->devi_parent;
dev_info_t **dipp;
ASSERT(parent); /* never called for root node */
NDI_CONFIG_DEBUG((CE_CONT, "link_node: parent = %s child = %s\n",
parent->devi_node_name, devi->devi_node_name));
/*
* Hold the global_vhci_lock before linking any direct
* children of rootnex driver. This special lock protects
* linking and unlinking for rootnext direct children.
*/
if ((dev_info_t *)parent == ddi_root_node())
mutex_enter(&global_vhci_lock);
/*
* attach the node to end of the list unless the node is already there
*/
dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
while (*dipp && (*dipp != dip)) {
dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
}
ASSERT(*dipp == NULL); /* node is not linked */
/*
* Now that we are in the tree, update the devi-nodeid map.
*/
i_ddi_add_devimap(dip);
/*
* This is a temporary workaround for Bug 4618861.
* We keep the scsi_vhci nexus node on the left side of the devinfo
* tree (under the root nexus driver), so that virtual nodes under
* scsi_vhci will be SUSPENDed first and RESUMEd last. This ensures
* that the pHCI nodes are active during times when their clients
* may be depending on them. This workaround embodies the knowledge
* that system PM and CPR both traverse the tree left-to-right during
* SUSPEND and right-to-left during RESUME.
* Extending the workaround to IB Nexus/VHCI
* driver also.
*/
if (strcmp(devi->devi_binding_name, "scsi_vhci") == 0) {
/* Add scsi_vhci to beginning of list */
ASSERT((dev_info_t *)parent == top_devinfo);
/* scsi_vhci under rootnex */
devi->devi_sibling = parent->devi_child;
parent->devi_child = devi;
} else if (strcmp(devi->devi_binding_name, "ib") == 0) {
i_link_vhci_node(dip);
} else {
/* Add to end of list */
*dipp = dip;
DEVI(dip)->devi_sibling = NULL;
}
/*
* Release the global_vhci_lock before linking any direct
* children of rootnex driver.
*/
if ((dev_info_t *)parent == ddi_root_node())
mutex_exit(&global_vhci_lock);
/* persistent nodes go on orphan list */
if (ndi_dev_is_persistent_node(dip))
add_to_dn_list(&orphanlist, dip);
}
/*
* Unlink this node from the devinfo tree
*/
static int
unlink_node(dev_info_t *dip)
{
struct dev_info *devi = DEVI(dip);
struct dev_info *parent = devi->devi_parent;
dev_info_t **dipp;
ddi_hp_cn_handle_t *hdlp;
ASSERT(parent != NULL);
ASSERT(devi->devi_node_state == DS_LINKED);
NDI_CONFIG_DEBUG((CE_CONT, "unlink_node: name = %s\n",
ddi_node_name(dip)));
/* check references */
if (devi->devi_ref || i_ddi_remove_devimap(dip) != DDI_SUCCESS)
return (DDI_FAILURE);
/*
* Hold the global_vhci_lock before linking any direct
* children of rootnex driver.
*/
if ((dev_info_t *)parent == ddi_root_node())
mutex_enter(&global_vhci_lock);
dipp = (dev_info_t **)(&DEVI(parent)->devi_child);
while (*dipp && (*dipp != dip)) {
dipp = (dev_info_t **)(&DEVI(*dipp)->devi_sibling);
}
if (*dipp) {
*dipp = (dev_info_t *)(devi->devi_sibling);
devi->devi_sibling = NULL;
} else {
NDI_CONFIG_DEBUG((CE_NOTE, "unlink_node: %s not linked",
devi->devi_node_name));
}
/*
* Release the global_vhci_lock before linking any direct
* children of rootnex driver.
*/
if ((dev_info_t *)parent == ddi_root_node())
mutex_exit(&global_vhci_lock);
/* Remove node from orphan list */
if (ndi_dev_is_persistent_node(dip)) {
remove_from_dn_list(&orphanlist, dip);
}
/* Update parent's hotplug handle list */
for (hdlp = DEVI(parent)->devi_hp_hdlp; hdlp; hdlp = hdlp->next) {
if (hdlp->cn_info.cn_child == dip)
hdlp->cn_info.cn_child = NULL;
}
return (DDI_SUCCESS);
}
/*
* Bind this devinfo node to a driver. If compat is NON-NULL, try that first.
* Else, use the node-name.
*
* NOTE: IEEE1275 specifies that nodename should be tried before compatible.
* Solaris implementation binds nodename after compatible.
*
* If we find a binding,
* - set the binding name to the string,
* - set major number to driver major
*
* If we don't find a binding,
* - return failure
*/
static int
bind_node(dev_info_t *dip)
{
char *p = NULL;
major_t major = DDI_MAJOR_T_NONE;
struct dev_info *devi = DEVI(dip);
dev_info_t *parent = ddi_get_parent(dip);
ASSERT(devi->devi_node_state == DS_LINKED);
NDI_CONFIG_DEBUG((CE_CONT, "bind_node: 0x%p(name = %s)\n",
(void *)dip, ddi_node_name(dip)));
mutex_enter(&DEVI(dip)->devi_lock);
if (DEVI(dip)->devi_flags & DEVI_NO_BIND) {
mutex_exit(&DEVI(dip)->devi_lock);
return (DDI_FAILURE);
}
mutex_exit(&DEVI(dip)->devi_lock);
/* find the driver with most specific binding using compatible */
major = ddi_compatible_driver_major(dip, &p);
if (major == DDI_MAJOR_T_NONE)
return (DDI_FAILURE);
devi->devi_major = major;
if (p != NULL) {
i_ddi_set_binding_name(dip, p);
NDI_CONFIG_DEBUG((CE_CONT, "bind_node: %s bound to %s\n",
devi->devi_node_name, p));
}
/* Link node to per-driver list */
link_to_driver_list(dip);
/*
* reset parent flag so that nexus will merge .conf props
*/
if (ndi_dev_is_persistent_node(dip)) {
mutex_enter(&DEVI(parent)->devi_lock);
DEVI(parent)->devi_flags &=
~(DEVI_ATTACHED_CHILDREN|DEVI_MADE_CHILDREN);
mutex_exit(&DEVI(parent)->devi_lock);
}
return (DDI_SUCCESS);
}
/*
* Unbind this devinfo node
* Called before the node is destroyed or driver is removed from system
*/
static int
unbind_node(dev_info_t *dip)
{
ASSERT(DEVI(dip)->devi_node_state == DS_BOUND);
ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
/* check references */
if (DEVI(dip)->devi_ref)
return (DDI_FAILURE);
NDI_CONFIG_DEBUG((CE_CONT, "unbind_node: 0x%p(name = %s)\n",
(void *)dip, ddi_node_name(dip)));
unlink_from_driver_list(dip);
DEVI(dip)->devi_major = DDI_MAJOR_T_NONE;
DEVI(dip)->devi_binding_name = DEVI(dip)->devi_node_name;
return (DDI_SUCCESS);
}
/*
* Initialize a node: calls the parent nexus' bus_ctl ops to do the operation.
* Must hold parent and per-driver list while calling this function.
* A successful init_node() returns with an active ndi_hold_devi() hold on
* the parent.
*/
static int
init_node(dev_info_t *dip)
{
int error;
dev_info_t *pdip = ddi_get_parent(dip);
int (*f)(dev_info_t *, dev_info_t *, ddi_ctl_enum_t, void *, void *);
char *path;
major_t major;
ASSERT(i_ddi_node_state(dip) == DS_BOUND);
/* should be DS_READY except for pcmcia ... */
ASSERT(i_ddi_node_state(pdip) >= DS_PROBED);
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
NDI_CONFIG_DEBUG((CE_CONT, "init_node: entry: path %s 0x%p\n",
path, (void *)dip));
/*
* The parent must have a bus_ctl operation.
*/
if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
(f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_ctl) == NULL) {
error = DDI_FAILURE;
goto out;
}
add_global_props(dip);
/*
* Invoke the parent's bus_ctl operation with the DDI_CTLOPS_INITCHILD
* command to transform the child to canonical form 1. If there
* is an error, ddi_remove_child should be called, to clean up.
*/
error = (*f)(pdip, pdip, DDI_CTLOPS_INITCHILD, dip, NULL);
if (error != DDI_SUCCESS) {
NDI_CONFIG_DEBUG((CE_CONT, "init_node: %s 0x%p failed\n",
path, (void *)dip));
remove_global_props(dip);
/* in case nexus driver didn't clear this field */
ddi_set_name_addr(dip, NULL);
error = DDI_FAILURE;
goto out;
}
ndi_hold_devi(pdip); /* initial hold of parent */
/* recompute path after initchild for @addr information */
(void) ddi_pathname(dip, path);
/* Check for duplicate nodes */
if (find_duplicate_child(pdip, dip) != NULL) {
/*
* uninit_node() the duplicate - a successful uninit_node()
* will release inital hold of parent using ndi_rele_devi().
*/
if ((error = uninit_node(dip)) != DDI_SUCCESS) {
ndi_rele_devi(pdip); /* release initial hold */
cmn_err(CE_WARN, "init_node: uninit of duplicate "
"node %s failed", path);
}
NDI_CONFIG_DEBUG((CE_CONT, "init_node: duplicate uninit "
"%s 0x%p%s\n", path, (void *)dip,
(error == DDI_SUCCESS) ? "" : " failed"));
error = DDI_FAILURE;
goto out;
}
/*
* Check to see if we have a path-oriented driver alias that overrides
* the current driver binding. If so, we need to rebind. This check
* needs to be delayed until after a successful DDI_CTLOPS_INITCHILD,
* so the unit-address is established on the last component of the path.
*
* NOTE: Allowing a path-oriented alias to change the driver binding
* of a driver.conf node results in non-intuitive property behavior.
* We provide a tunable (driver_conf_allow_path_alias) to control
* this behavior. See uninit_node() for more details.
*
* NOTE: If you are adding a path-oriented alias for the boot device,
* and there is mismatch between OBP and the kernel in regard to
* generic name use, like "disk" .vs. "ssd", then you will need
* to add a path-oriented alias for both paths.
*/
major = ddi_name_to_major(path);
if (driver_installed(major) && (major != DEVI(dip)->devi_major) &&
(ndi_dev_is_persistent_node(dip) || driver_conf_allow_path_alias)) {
/* Mark node for rebind processing. */
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(dip)->devi_flags |= DEVI_REBIND;
mutex_exit(&DEVI(dip)->devi_lock);
/*
* Add an extra hold on the parent to prevent it from ever
* having a zero devi_ref during the child rebind process.
* This is necessary to ensure that the parent will never
* detach(9E) during the rebind.
*/
ndi_hold_devi(pdip); /* extra hold of parent */
/*
* uninit_node() current binding - a successful uninit_node()
* will release extra hold of parent using ndi_rele_devi().
*/
if ((error = uninit_node(dip)) != DDI_SUCCESS) {
ndi_rele_devi(pdip); /* release extra hold */
ndi_rele_devi(pdip); /* release initial hold */
cmn_err(CE_WARN, "init_node: uninit for rebind "
"of node %s failed", path);
goto out;
}
/* Unbind: demote the node back to DS_LINKED. */
if ((error = ndi_devi_unbind_driver(dip)) != DDI_SUCCESS) {
ndi_rele_devi(pdip); /* release initial hold */
cmn_err(CE_WARN, "init_node: unbind for rebind "
"of node %s failed", path);
goto out;
}
/* establish rebinding name */
if (DEVI(dip)->devi_rebinding_name == NULL)
DEVI(dip)->devi_rebinding_name =
i_ddi_strdup(path, KM_SLEEP);
/*
* Now that we are demoted and marked for rebind, repromote.
* We need to do this in steps, instead of just calling
* ddi_initchild, so that we can redo the merge operation
* after we are rebound to the path-bound driver.
*
* Start by rebinding node to the path-bound driver.
*/
if ((error = ndi_devi_bind_driver(dip, 0)) != DDI_SUCCESS) {
ndi_rele_devi(pdip); /* release initial hold */
cmn_err(CE_WARN, "init_node: rebind "
"of node %s failed", path);
goto out;
}
/*
* If the node is not a driver.conf node then merge
* driver.conf properties from new path-bound driver.conf.
*/
if (ndi_dev_is_persistent_node(dip))
(void) i_ndi_make_spec_children(pdip, 0);
/*
* Now that we have taken care of merge, repromote back
* to DS_INITIALIZED.
*/
error = ddi_initchild(pdip, dip);
NDI_CONFIG_DEBUG((CE_CONT, "init_node: rebind "
"%s 0x%p\n", path, (void *)dip));
/*
* Release our initial hold. If ddi_initchild() was
* successful then it will return with the active hold.
*/
ndi_rele_devi(pdip);
goto out;
}
/*
* Apply multi-parent/deep-nexus optimization to the new node
*/
DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
ddi_optimize_dtree(dip);
error = DDI_SUCCESS; /* return with active hold */
out: if (error != DDI_SUCCESS) {
/* On failure ensure that DEVI_REBIND is cleared */
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(dip)->devi_flags &= ~DEVI_REBIND;
mutex_exit(&DEVI(dip)->devi_lock);
}
kmem_free(path, MAXPATHLEN);
return (error);
}
/*
* Uninitialize node
* The per-driver list must be held busy during the call.
* A successful uninit_node() releases the init_node() hold on
* the parent by calling ndi_rele_devi().
*/
static int
uninit_node(dev_info_t *dip)
{
int node_state_entry;
dev_info_t *pdip;
struct dev_ops *ops;
int (*f)();
int error;
char *addr;
/*
* Don't check for references here or else a ref-counted
* dip cannot be downgraded by the framework.
*/
node_state_entry = i_ddi_node_state(dip);
ASSERT((node_state_entry == DS_BOUND) ||
(node_state_entry == DS_INITIALIZED));
pdip = ddi_get_parent(dip);
ASSERT(pdip);
NDI_CONFIG_DEBUG((CE_CONT, "uninit_node: 0x%p(%s%d)\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
if (((ops = ddi_get_driver(pdip)) == NULL) ||
(ops->devo_bus_ops == NULL) ||
((f = ops->devo_bus_ops->bus_ctl) == NULL)) {
return (DDI_FAILURE);
}
/*
* save the @addr prior to DDI_CTLOPS_UNINITCHILD for use in
* freeing the instance if it succeeds.
*/
if (node_state_entry == DS_INITIALIZED) {
addr = ddi_get_name_addr(dip);
if (addr)
addr = i_ddi_strdup(addr, KM_SLEEP);
} else {
addr = NULL;
}
error = (*f)(pdip, pdip, DDI_CTLOPS_UNINITCHILD, dip, (void *)NULL);
if (error == DDI_SUCCESS) {
/* ensure that devids are unregistered */
if (DEVI(dip)->devi_flags & DEVI_REGISTERED_DEVID) {
DEVI(dip)->devi_flags &= ~DEVI_REGISTERED_DEVID;
ddi_devid_unregister(dip);
}
/* if uninitchild forgot to set devi_addr to NULL do it now */
ddi_set_name_addr(dip, NULL);
/*
* Free instance number. This is a no-op if instance has
* been kept by probe_node(). Avoid free when we are called
* from init_node (DS_BOUND) because the instance has not yet
* been assigned.
*/
if (node_state_entry == DS_INITIALIZED) {
e_ddi_free_instance(dip, addr);
DEVI(dip)->devi_instance = -1;
}
/* release the init_node hold */
ndi_rele_devi(pdip);
remove_global_props(dip);
/*
* NOTE: The decision on whether to allow a path-oriented
* rebind of a driver.conf enumerated node is made by
* init_node() based on driver_conf_allow_path_alias. The
* rebind code below prevents deletion of system properties
* on driver.conf nodes.
*
* When driver_conf_allow_path_alias is set, property behavior
* on rebound driver.conf file is non-intuitive. For a
* driver.conf node, the unit-address properties come from
* the driver.conf file as system properties. Removing system
* properties from a driver.conf node makes the node
* useless (we get node without unit-address properties) - so
* we leave system properties in place. The result is a node
* where system properties come from the node being rebound,
* and global properties come from the driver.conf file
* of the driver we are rebinding to. If we could determine
* that the path-oriented alias driver.conf file defined a
* node at the same unit address, it would be best to use
* that node and avoid the non-intuitive property behavior.
* Unfortunately, the current "merge" code does not support
* this, so we live with the non-intuitive property behavior.
*/
if (!((ndi_dev_is_persistent_node(dip) == 0) &&
(DEVI(dip)->devi_flags & DEVI_REBIND)))
e_ddi_prop_remove_all(dip);
} else {
NDI_CONFIG_DEBUG((CE_CONT, "uninit_node failed: 0x%p(%s%d)\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
}
if (addr)
kmem_free(addr, strlen(addr) + 1);
return (error);
}
/*
* Invoke driver's probe entry point to probe for existence of hardware.
* Keep instance permanent for successful probe and leaf nodes.
*
* Per-driver list must be held busy while calling this function.
*/
static int
probe_node(dev_info_t *dip)
{
int rv;
ASSERT(i_ddi_node_state(dip) == DS_INITIALIZED);
NDI_CONFIG_DEBUG((CE_CONT, "probe_node: 0x%p(%s%d)\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
/* temporarily hold the driver while we probe */
DEVI(dip)->devi_ops = ndi_hold_driver(dip);
if (DEVI(dip)->devi_ops == NULL) {
NDI_CONFIG_DEBUG((CE_CONT,
"probe_node: 0x%p(%s%d) cannot load driver\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
return (DDI_FAILURE);
}
if (identify_9e != 0)
(void) devi_identify(dip);
rv = devi_probe(dip);
/* release the driver now that probe is complete */
ndi_rele_driver(dip);
DEVI(dip)->devi_ops = NULL;
switch (rv) {
case DDI_PROBE_SUCCESS: /* found */
case DDI_PROBE_DONTCARE: /* ddi_dev_is_sid */
e_ddi_keep_instance(dip); /* persist instance */
rv = DDI_SUCCESS;
break;
case DDI_PROBE_PARTIAL: /* maybe later */
case DDI_PROBE_FAILURE: /* not found */
NDI_CONFIG_DEBUG((CE_CONT,
"probe_node: 0x%p(%s%d) no hardware found%s\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip),
(rv == DDI_PROBE_PARTIAL) ? " yet" : ""));
rv = DDI_FAILURE;
break;
default:
#ifdef DEBUG
cmn_err(CE_WARN, "probe_node: %s%d: illegal probe(9E) value",
ddi_driver_name(dip), ddi_get_instance(dip));
#endif /* DEBUG */
rv = DDI_FAILURE;
break;
}
return (rv);
}
/*
* Unprobe a node. Simply reset the node state.
* Per-driver list must be held busy while calling this function.
*/
static int
unprobe_node(dev_info_t *dip)
{
ASSERT(i_ddi_node_state(dip) == DS_PROBED);
/*
* Don't check for references here or else a ref-counted
* dip cannot be downgraded by the framework.
*/
NDI_CONFIG_DEBUG((CE_CONT, "unprobe_node: 0x%p(name = %s)\n",
(void *)dip, ddi_node_name(dip)));
return (DDI_SUCCESS);
}
/*
* Attach devinfo node.
* Per-driver list must be held busy.
*/
static int
attach_node(dev_info_t *dip)
{
int rv;
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
ASSERT(i_ddi_node_state(dip) == DS_PROBED);
NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d)\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
/*
* Tell mpxio framework that a node is about to online.
*/
if ((rv = mdi_devi_online(dip, 0)) != NDI_SUCCESS) {
return (DDI_FAILURE);
}
/* no recursive attachment */
ASSERT(DEVI(dip)->devi_ops == NULL);
/*
* Hold driver the node is bound to.
*/
DEVI(dip)->devi_ops = ndi_hold_driver(dip);
if (DEVI(dip)->devi_ops == NULL) {
/*
* We were able to load driver for probing, so we should
* not get here unless something really bad happened.
*/
cmn_err(CE_WARN, "attach_node: no driver for major %d",
DEVI(dip)->devi_major);
return (DDI_FAILURE);
}
if (NEXUS_DRV(DEVI(dip)->devi_ops))
DEVI(dip)->devi_taskq = ddi_taskq_create(dip,
"nexus_enum_tq", 1,
TASKQ_DEFAULTPRI, 0);
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_ATTACHING(dip);
DEVI_SET_NEED_RESET(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
rv = devi_attach(dip, DDI_ATTACH);
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_CLR_ATTACHING(dip);
if (rv != DDI_SUCCESS) {
DEVI_CLR_NEED_RESET(dip);
mutex_exit(&DEVI(dip)->devi_lock);
/*
* Cleanup dacf reservations
*/
mutex_enter(&dacf_lock);
dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
mutex_exit(&dacf_lock);
if (DEVI(dip)->devi_taskq)
ddi_taskq_destroy(DEVI(dip)->devi_taskq);
ddi_remove_minor_node(dip, NULL);
/* release the driver if attach failed */
ndi_rele_driver(dip);
DEVI(dip)->devi_ops = NULL;
NDI_CONFIG_DEBUG((CE_CONT, "attach_node: 0x%p(%s%d) failed\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
return (DDI_FAILURE);
} else
mutex_exit(&DEVI(dip)->devi_lock);
/* successful attach, return with driver held */
return (DDI_SUCCESS);
}
/*
* Detach devinfo node.
* Per-driver list must be held busy.
*/
static int
detach_node(dev_info_t *dip, uint_t flag)
{
struct devnames *dnp;
int rv;
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
ASSERT(i_ddi_node_state(dip) == DS_ATTACHED);
/* check references */
if (DEVI(dip)->devi_ref)
return (DDI_FAILURE);
NDI_CONFIG_DEBUG((CE_CONT, "detach_node: 0x%p(%s%d)\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
/*
* NOTE: If we are processing a pHCI node then the calling code
* must detect this and ndi_devi_enter() in (vHCI, parent(pHCI))
* order unless pHCI and vHCI are siblings. Code paths leading
* here that must ensure this ordering include:
* unconfig_immediate_children(), devi_unconfig_one(),
* ndi_devi_unconfig_one(), ndi_devi_offline().
*/
ASSERT(!MDI_PHCI(dip) ||
(ddi_get_parent(mdi_devi_get_vdip(dip)) == ddi_get_parent(dip)) ||
DEVI_BUSY_OWNED(mdi_devi_get_vdip(dip)));
/* Offline the device node with the mpxio framework. */
if (mdi_devi_offline(dip, flag) != NDI_SUCCESS) {
return (DDI_FAILURE);
}
/* drain the taskq */
if (DEVI(dip)->devi_taskq)
ddi_taskq_wait(DEVI(dip)->devi_taskq);
rv = devi_detach(dip, DDI_DETACH);
if (rv != DDI_SUCCESS) {
NDI_CONFIG_DEBUG((CE_CONT,
"detach_node: 0x%p(%s%d) failed\n",
(void *)dip, ddi_driver_name(dip), ddi_get_instance(dip)));
return (DDI_FAILURE);
}
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_CLR_NEED_RESET(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
#if defined(__amd64) && !defined(__xpv)
/*
* Close any iommulib mediated linkage to an IOMMU
*/
iommulib_nex_close(dip);
#endif
/* destroy the taskq */
if (DEVI(dip)->devi_taskq) {
ddi_taskq_destroy(DEVI(dip)->devi_taskq);
DEVI(dip)->devi_taskq = NULL;
}
/* Cleanup dacf reservations */
mutex_enter(&dacf_lock);
dacf_clr_rsrvs(dip, DACF_OPID_POSTATTACH);
dacf_clr_rsrvs(dip, DACF_OPID_PREDETACH);
mutex_exit(&dacf_lock);
/* remove any additional flavors that were added */
if (DEVI(dip)->devi_flavorv_n > 1 && DEVI(dip)->devi_flavorv != NULL) {
kmem_free(DEVI(dip)->devi_flavorv,
(DEVI(dip)->devi_flavorv_n - 1) * sizeof (void *));
DEVI(dip)->devi_flavorv = NULL;
}
/* Remove properties and minor nodes in case driver forgots */
ddi_remove_minor_node(dip, NULL);
ddi_prop_remove_all(dip);
/* a detached node can't have attached or .conf children */
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN|DEVI_ATTACHED_CHILDREN);
mutex_exit(&DEVI(dip)->devi_lock);
/*
* If the instance has successfully detached in detach_driver() context,
* clear DN_DRIVER_HELD for correct ddi_hold_installed_driver()
* behavior. Consumers like qassociate() depend on this (via clnopen()).
*/
if (flag & NDI_DETACH_DRIVER) {
dnp = &(devnamesp[DEVI(dip)->devi_major]);
LOCK_DEV_OPS(&dnp->dn_lock);
dnp->dn_flags &= ~DN_DRIVER_HELD;
UNLOCK_DEV_OPS(&dnp->dn_lock);
}
/* successful detach, release the driver */
ndi_rele_driver(dip);
DEVI(dip)->devi_ops = NULL;
return (DDI_SUCCESS);
}
/*
* Run dacf post_attach routines
*/
static int
postattach_node(dev_info_t *dip)
{
int rval;
/*
* For hotplug busses like USB, it's possible that devices
* are removed but dip is still around. We don't want to
* run dacf routines as part of detach failure recovery.
*
* Pretend success until we figure out how to prevent
* access to such devinfo nodes.
*/
if (DEVI_IS_DEVICE_REMOVED(dip))
return (DDI_SUCCESS);
/*
* if dacf_postattach failed, report it to the framework
* so that it can be retried later at the open time.
*/
mutex_enter(&dacf_lock);
rval = dacfc_postattach(dip);
mutex_exit(&dacf_lock);
/*
* Plumbing during postattach may fail because of the
* underlying device is not ready. This will fail ndi_devi_config()
* in dv_filldir() and a warning message is issued. The message
* from here will explain what happened
*/
if (rval != DACF_SUCCESS) {
cmn_err(CE_WARN, "Postattach failed for %s%d\n",
ddi_driver_name(dip), ddi_get_instance(dip));
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
/*
* Run dacf pre-detach routines
*/
static int
predetach_node(dev_info_t *dip, uint_t flag)
{
int ret;
/*
* Don't auto-detach if DDI_FORCEATTACH or DDI_NO_AUTODETACH
* properties are set.
*/
if (flag & NDI_AUTODETACH) {
struct devnames *dnp;
int pflag = DDI_PROP_NOTPROM | DDI_PROP_DONTPASS;
if ((ddi_prop_get_int(DDI_DEV_T_ANY, dip,
pflag, DDI_FORCEATTACH, 0) == 1) ||
(ddi_prop_get_int(DDI_DEV_T_ANY, dip,
pflag, DDI_NO_AUTODETACH, 0) == 1))
return (DDI_FAILURE);
/* check for driver global version of DDI_NO_AUTODETACH */
dnp = &devnamesp[DEVI(dip)->devi_major];
LOCK_DEV_OPS(&dnp->dn_lock);
if (dnp->dn_flags & DN_NO_AUTODETACH) {
UNLOCK_DEV_OPS(&dnp->dn_lock);
return (DDI_FAILURE);
}
UNLOCK_DEV_OPS(&dnp->dn_lock);
}
mutex_enter(&dacf_lock);
ret = dacfc_predetach(dip);
mutex_exit(&dacf_lock);
return (ret);
}
/*
* Wrapper for making multiple state transitions
*/
/*
* i_ndi_config_node: upgrade dev_info node into a specified state.
* It is a bit tricky because the locking protocol changes before and
* after a node is bound to a driver. All locks are held external to
* this function.
*/
int
i_ndi_config_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
{
_NOTE(ARGUNUSED(flag))
int rv = DDI_SUCCESS;
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
while ((i_ddi_node_state(dip) < state) && (rv == DDI_SUCCESS)) {
/* don't allow any more changes to the device tree */
if (devinfo_freeze) {
rv = DDI_FAILURE;
break;
}
switch (i_ddi_node_state(dip)) {
case DS_PROTO:
/*
* only caller can reference this node, no external
* locking needed.
*/
link_node(dip);
translate_devid((dev_info_t *)dip);
i_ddi_set_node_state(dip, DS_LINKED);
break;
case DS_LINKED:
/*
* Three code path may attempt to bind a node:
* - boot code
* - add_drv
* - hotplug thread
* Boot code is single threaded, add_drv synchronize
* on a userland lock, and hotplug synchronize on
* hotplug_lk. There could be a race between add_drv
* and hotplug thread. We'll live with this until the
* conversion to top-down loading.
*/
if ((rv = bind_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_BOUND);
break;
case DS_BOUND:
/*
* The following transitions synchronizes on the
* per-driver busy changing flag, since we already
* have a driver.
*/
if ((rv = init_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_INITIALIZED);
break;
case DS_INITIALIZED:
if ((rv = probe_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_PROBED);
break;
case DS_PROBED:
i_ddi_check_retire(dip);
atomic_add_long(&devinfo_attach_detach, 1);
if ((rv = attach_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_ATTACHED);
atomic_add_long(&devinfo_attach_detach, -1);
break;
case DS_ATTACHED:
if ((rv = postattach_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_READY);
break;
case DS_READY:
break;
default:
/* should never reach here */
ASSERT("unknown devinfo state");
}
}
if (ddidebug & DDI_AUDIT)
da_log_enter(dip);
return (rv);
}
/*
* i_ndi_unconfig_node: downgrade dev_info node into a specified state.
*/
int
i_ndi_unconfig_node(dev_info_t *dip, ddi_node_state_t state, uint_t flag)
{
int rv = DDI_SUCCESS;
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
while ((i_ddi_node_state(dip) > state) && (rv == DDI_SUCCESS)) {
/* don't allow any more changes to the device tree */
if (devinfo_freeze) {
rv = DDI_FAILURE;
break;
}
switch (i_ddi_node_state(dip)) {
case DS_PROTO:
break;
case DS_LINKED:
/*
* Persistent nodes are only removed by hotplug code
* .conf nodes synchronizes on per-driver list.
*/
if ((rv = unlink_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_PROTO);
break;
case DS_BOUND:
/*
* The following transitions synchronizes on the
* per-driver busy changing flag, since we already
* have a driver.
*/
if ((rv = unbind_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_LINKED);
break;
case DS_INITIALIZED:
if ((rv = uninit_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_BOUND);
break;
case DS_PROBED:
if ((rv = unprobe_node(dip)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_INITIALIZED);
break;
case DS_ATTACHED:
atomic_add_long(&devinfo_attach_detach, 1);
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_DETACHING(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
membar_enter(); /* ensure visibility for hold_devi */
if ((rv = detach_node(dip, flag)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_PROBED);
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_CLR_DETACHING(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
atomic_add_long(&devinfo_attach_detach, -1);
break;
case DS_READY:
if ((rv = predetach_node(dip, flag)) == DDI_SUCCESS)
i_ddi_set_node_state(dip, DS_ATTACHED);
break;
default:
ASSERT("unknown devinfo state");
}
}
da_log_enter(dip);
return (rv);
}
/*
* ddi_initchild: transform node to DS_INITIALIZED state
*/
int
ddi_initchild(dev_info_t *parent, dev_info_t *proto)
{
int ret, circ;
ndi_devi_enter(parent, &circ);
ret = i_ndi_config_node(proto, DS_INITIALIZED, 0);
ndi_devi_exit(parent, circ);
return (ret);
}
/*
* ddi_uninitchild: transform node down to DS_BOUND state
*/
int
ddi_uninitchild(dev_info_t *dip)
{
int ret, circ;
dev_info_t *parent = ddi_get_parent(dip);
ASSERT(parent);
ndi_devi_enter(parent, &circ);
ret = i_ndi_unconfig_node(dip, DS_BOUND, 0);
ndi_devi_exit(parent, circ);
return (ret);
}
/*
* i_ddi_attachchild: transform node to DS_READY/i_ddi_devi_attached() state
*/
static int
i_ddi_attachchild(dev_info_t *dip)
{
dev_info_t *parent = ddi_get_parent(dip);
int ret;
ASSERT(parent && DEVI_BUSY_OWNED(parent));
if ((i_ddi_node_state(dip) < DS_BOUND) || DEVI_IS_DEVICE_OFFLINE(dip))
return (DDI_FAILURE);
ret = i_ndi_config_node(dip, DS_READY, 0);
if (ret == NDI_SUCCESS) {
ret = DDI_SUCCESS;
} else {
/*
* Take it down to DS_INITIALIZED so pm_pre_probe is run
* on the next attach
*/
(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
ret = DDI_FAILURE;
}
return (ret);
}
/*
* i_ddi_detachchild: transform node down to DS_PROBED state
* If it fails, put it back to DS_READY state.
* NOTE: A node that fails detach may be at DS_ATTACHED instead
* of DS_READY for a small amount of time - this is the source of
* transient DS_READY->DS_ATTACHED->DS_READY state changes.
*/
static int
i_ddi_detachchild(dev_info_t *dip, uint_t flags)
{
dev_info_t *parent = ddi_get_parent(dip);
int ret;
ASSERT(parent && DEVI_BUSY_OWNED(parent));
ret = i_ndi_unconfig_node(dip, DS_PROBED, flags);
if (ret != DDI_SUCCESS)
(void) i_ndi_config_node(dip, DS_READY, 0);
else
/* allow pm_pre_probe to reestablish pm state */
(void) i_ndi_unconfig_node(dip, DS_INITIALIZED, 0);
return (ret);
}
/*
* Add a child and bind to driver
*/
dev_info_t *
ddi_add_child(dev_info_t *pdip, char *name, uint_t nodeid, uint_t unit)
{
int circ;
dev_info_t *dip;
/* allocate a new node */
dip = i_ddi_alloc_node(pdip, name, nodeid, (int)unit, NULL, KM_SLEEP);
ndi_devi_enter(pdip, &circ);
(void) i_ndi_config_node(dip, DS_BOUND, 0);
ndi_devi_exit(pdip, circ);
return (dip);
}
/*
* ddi_remove_child: remove the dip. The parent must be attached and held
*/
int
ddi_remove_child(dev_info_t *dip, int dummy)
{
_NOTE(ARGUNUSED(dummy))
int circ, ret;
dev_info_t *parent = ddi_get_parent(dip);
ASSERT(parent);
ndi_devi_enter(parent, &circ);
/*
* If we still have children, for example SID nodes marked
* as persistent but not attached, attempt to remove them.
*/
if (DEVI(dip)->devi_child) {
ret = ndi_devi_unconfig(dip, NDI_DEVI_REMOVE);
if (ret != NDI_SUCCESS) {
ndi_devi_exit(parent, circ);
return (DDI_FAILURE);
}
ASSERT(DEVI(dip)->devi_child == NULL);
}
ret = i_ndi_unconfig_node(dip, DS_PROTO, 0);
ndi_devi_exit(parent, circ);
if (ret != DDI_SUCCESS)
return (ret);
ASSERT(i_ddi_node_state(dip) == DS_PROTO);
i_ddi_free_node(dip);
return (DDI_SUCCESS);
}
/*
* NDI wrappers for ref counting, node allocation, and transitions
*/
/*
* Hold/release the devinfo node itself.
* Caller is assumed to prevent the devi from detaching during this call
*/
void
ndi_hold_devi(dev_info_t *dip)
{
mutex_enter(&DEVI(dip)->devi_lock);
ASSERT(DEVI(dip)->devi_ref >= 0);
DEVI(dip)->devi_ref++;
membar_enter(); /* make sure stores are flushed */
mutex_exit(&DEVI(dip)->devi_lock);
}
void
ndi_rele_devi(dev_info_t *dip)
{
ASSERT(DEVI(dip)->devi_ref > 0);
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(dip)->devi_ref--;
membar_enter(); /* make sure stores are flushed */
mutex_exit(&DEVI(dip)->devi_lock);
}
int
e_ddi_devi_holdcnt(dev_info_t *dip)
{
return (DEVI(dip)->devi_ref);
}
/*
* Hold/release the driver the devinfo node is bound to.
*/
struct dev_ops *
ndi_hold_driver(dev_info_t *dip)
{
if (i_ddi_node_state(dip) < DS_BOUND)
return (NULL);
ASSERT(DEVI(dip)->devi_major != -1);
return (mod_hold_dev_by_major(DEVI(dip)->devi_major));
}
void
ndi_rele_driver(dev_info_t *dip)
{
ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
mod_rele_dev_by_major(DEVI(dip)->devi_major);
}
/*
* Single thread entry into devinfo node for modifying its children (devinfo,
* pathinfo, and minor). To verify in ASSERTS use DEVI_BUSY_OWNED macro.
*/
void
ndi_devi_enter(dev_info_t *dip, int *circular)
{
struct dev_info *devi = DEVI(dip);
ASSERT(dip != NULL);
/* for vHCI, enforce (vHCI, pHCI) ndi_deve_enter() order */
ASSERT(!MDI_VHCI(dip) || (mdi_devi_pdip_entered(dip) == 0) ||
DEVI_BUSY_OWNED(dip));
mutex_enter(&devi->devi_lock);
if (devi->devi_busy_thread == curthread) {
devi->devi_circular++;
} else {
while (DEVI_BUSY_CHANGING(devi) && !panicstr)
cv_wait(&(devi->devi_cv), &(devi->devi_lock));
if (panicstr) {
mutex_exit(&devi->devi_lock);
return;
}
devi->devi_flags |= DEVI_BUSY;
devi->devi_busy_thread = curthread;
}
*circular = devi->devi_circular;
mutex_exit(&devi->devi_lock);
}
/*
* Release ndi_devi_enter or successful ndi_devi_tryenter.
*/
void
ndi_devi_exit(dev_info_t *dip, int circular)
{
struct dev_info *devi = DEVI(dip);
struct dev_info *vdevi;
ASSERT(dip != NULL);
if (panicstr)
return;
mutex_enter(&(devi->devi_lock));
if (circular != 0) {
devi->devi_circular--;
} else {
devi->devi_flags &= ~DEVI_BUSY;
ASSERT(devi->devi_busy_thread == curthread);
devi->devi_busy_thread = NULL;
cv_broadcast(&(devi->devi_cv));
}
mutex_exit(&(devi->devi_lock));
/*
* For pHCI exit we issue a broadcast to vHCI for ndi_devi_config_one()
* doing cv_wait on vHCI.
*/
if (MDI_PHCI(dip)) {
vdevi = DEVI(mdi_devi_get_vdip(dip));
if (vdevi) {
mutex_enter(&(vdevi->devi_lock));
if (vdevi->devi_flags & DEVI_PHCI_SIGNALS_VHCI) {
vdevi->devi_flags &= ~DEVI_PHCI_SIGNALS_VHCI;
cv_broadcast(&(vdevi->devi_cv));
}
mutex_exit(&(vdevi->devi_lock));
}
}
}
/*
* Release ndi_devi_enter and wait for possibility of new children, avoiding
* possibility of missing broadcast before getting to cv_timedwait().
*/
static void
ndi_devi_exit_and_wait(dev_info_t *dip, int circular, clock_t end_time)
{
struct dev_info *devi = DEVI(dip);
ASSERT(dip != NULL);
if (panicstr)
return;
/*
* We are called to wait for of a new child, and new child can
* only be added if circular is zero.
*/
ASSERT(circular == 0);
/* like ndi_devi_exit with circular of zero */
mutex_enter(&(devi->devi_lock));
devi->devi_flags &= ~DEVI_BUSY;
ASSERT(devi->devi_busy_thread == curthread);
devi->devi_busy_thread = NULL;
cv_broadcast(&(devi->devi_cv));
/* now wait for new children while still holding devi_lock */
(void) cv_timedwait(&devi->devi_cv, &(devi->devi_lock), end_time);
mutex_exit(&(devi->devi_lock));
}
/*
* Attempt to single thread entry into devinfo node for modifying its children.
*/
int
ndi_devi_tryenter(dev_info_t *dip, int *circular)
{
int rval = 1; /* assume we enter */
struct dev_info *devi = DEVI(dip);
ASSERT(dip != NULL);
mutex_enter(&devi->devi_lock);
if (devi->devi_busy_thread == (void *)curthread) {
devi->devi_circular++;
} else {
if (!DEVI_BUSY_CHANGING(devi)) {
devi->devi_flags |= DEVI_BUSY;
devi->devi_busy_thread = (void *)curthread;
} else {
rval = 0; /* devi is busy */
}
}
*circular = devi->devi_circular;
mutex_exit(&devi->devi_lock);
return (rval);
}
/*
* Allocate and initialize a new dev_info structure.
*
* This routine may be called at interrupt time by a nexus in
* response to a hotplug event, therefore memory allocations are
* not allowed to sleep.
*/
int
ndi_devi_alloc(dev_info_t *parent, char *node_name, pnode_t nodeid,
dev_info_t **ret_dip)
{
ASSERT(node_name != NULL);
ASSERT(ret_dip != NULL);
*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
KM_NOSLEEP);
if (*ret_dip == NULL) {
return (NDI_NOMEM);
}
return (NDI_SUCCESS);
}
/*
* Allocate and initialize a new dev_info structure
* This routine may sleep and should not be called at interrupt time
*/
void
ndi_devi_alloc_sleep(dev_info_t *parent, char *node_name, pnode_t nodeid,
dev_info_t **ret_dip)
{
ASSERT(node_name != NULL);
ASSERT(ret_dip != NULL);
*ret_dip = i_ddi_alloc_node(parent, node_name, nodeid, -1, NULL,
KM_SLEEP);
ASSERT(*ret_dip);
}
/*
* Remove an initialized (but not yet attached) dev_info
* node from it's parent.
*/
int
ndi_devi_free(dev_info_t *dip)
{
ASSERT(dip != NULL);
if (i_ddi_node_state(dip) >= DS_INITIALIZED)
return (DDI_FAILURE);
NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_free: %s%d (%p)\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
(void) ddi_remove_child(dip, 0);
return (NDI_SUCCESS);
}
/*
* ndi_devi_bind_driver() binds a driver to a given device. If it fails
* to bind the driver, it returns an appropriate error back. Some drivers
* may want to know if the actually failed to bind.
*/
int
ndi_devi_bind_driver(dev_info_t *dip, uint_t flags)
{
int ret = NDI_FAILURE;
int circ;
dev_info_t *pdip = ddi_get_parent(dip);
ASSERT(pdip);
NDI_CONFIG_DEBUG((CE_CONT,
"ndi_devi_bind_driver: %s%d (%p) flags: %x\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
ndi_devi_enter(pdip, &circ);
if (i_ndi_config_node(dip, DS_BOUND, flags) == DDI_SUCCESS)
ret = NDI_SUCCESS;
ndi_devi_exit(pdip, circ);
return (ret);
}
/*
* ndi_devi_unbind_driver: unbind the dip
*/
static int
ndi_devi_unbind_driver(dev_info_t *dip)
{
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
return (i_ndi_unconfig_node(dip, DS_LINKED, 0));
}
/*
* Misc. help routines called by framework only
*/
/*
* Get the state of node
*/
ddi_node_state_t
i_ddi_node_state(dev_info_t *dip)
{
return (DEVI(dip)->devi_node_state);
}
/*
* Set the state of node
*/
void
i_ddi_set_node_state(dev_info_t *dip, ddi_node_state_t state)
{
DEVI(dip)->devi_node_state = state;
membar_enter(); /* make sure stores are flushed */
}
/*
* Determine if node is attached. The implementation accommodates transient
* DS_READY->DS_ATTACHED->DS_READY state changes. Outside this file, this
* function should be instead of i_ddi_node_state() DS_ATTACHED/DS_READY
* state checks.
*/
int
i_ddi_devi_attached(dev_info_t *dip)
{
return (DEVI(dip)->devi_node_state >= DS_ATTACHED);
}
/*
* Common function for finding a node in a sibling list given name and addr.
*
* By default, name is matched with devi_node_name. The following
* alternative match strategies are supported:
*
* FIND_NODE_BY_NODENAME: Match on node name - typical use.
*
* FIND_NODE_BY_DRIVER: A match on driver name bound to node is conducted.
* This support is used for support of OBP generic names and
* for the conversion from driver names to generic names. When
* more consistency in the generic name environment is achieved
* (and not needed for upgrade) this support can be removed.
*
* FIND_NODE_BY_ADDR: Match on just the addr.
* This support is only used/needed during boot to match
* a node bound via a path-based driver alias.
*
* If a child is not named (dev_addr == NULL), there are three
* possible actions:
*
* (1) skip it
* (2) FIND_ADDR_BY_INIT: bring child to DS_INITIALIZED state
* (3) FIND_ADDR_BY_CALLBACK: use a caller-supplied callback function
*/
#define FIND_NODE_BY_NODENAME 0x01
#define FIND_NODE_BY_DRIVER 0x02
#define FIND_NODE_BY_ADDR 0x04
#define FIND_ADDR_BY_INIT 0x10
#define FIND_ADDR_BY_CALLBACK 0x20
static dev_info_t *
find_sibling(dev_info_t *head, char *cname, char *caddr, uint_t flag,
int (*callback)(dev_info_t *, char *, int))
{
dev_info_t *dip;
char *addr, *buf;
major_t major;
uint_t by;
/* only one way to find a node */
by = flag &
(FIND_NODE_BY_DRIVER | FIND_NODE_BY_NODENAME | FIND_NODE_BY_ADDR);
ASSERT(by && BIT_ONLYONESET(by));
/* only one way to name a node */
ASSERT(((flag & FIND_ADDR_BY_INIT) == 0) ||
((flag & FIND_ADDR_BY_CALLBACK) == 0));
if (by == FIND_NODE_BY_DRIVER) {
major = ddi_name_to_major(cname);
if (major == DDI_MAJOR_T_NONE)
return (NULL);
}
/* preallocate buffer of naming node by callback */
if (flag & FIND_ADDR_BY_CALLBACK)
buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
/*
* Walk the child list to find a match
*/
if (head == NULL)
return (NULL);
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(head)));
for (dip = head; dip; dip = ddi_get_next_sibling(dip)) {
if (by == FIND_NODE_BY_NODENAME) {
/* match node name */
if (strcmp(cname, DEVI(dip)->devi_node_name) != 0)
continue;
} else if (by == FIND_NODE_BY_DRIVER) {
/* match driver major */
if (DEVI(dip)->devi_major != major)
continue;
}
if ((addr = DEVI(dip)->devi_addr) == NULL) {
/* name the child based on the flag */
if (flag & FIND_ADDR_BY_INIT) {
if (ddi_initchild(ddi_get_parent(dip), dip)
!= DDI_SUCCESS)
continue;
addr = DEVI(dip)->devi_addr;
} else if (flag & FIND_ADDR_BY_CALLBACK) {
if ((callback == NULL) || (callback(
dip, buf, MAXNAMELEN) != DDI_SUCCESS))
continue;
addr = buf;
} else {
continue; /* skip */
}
}
/* match addr */
ASSERT(addr != NULL);
if (strcmp(caddr, addr) == 0)
break; /* node found */
}
if (flag & FIND_ADDR_BY_CALLBACK)
kmem_free(buf, MAXNAMELEN);
return (dip);
}
/*
* Find child of pdip with name: cname@caddr
* Called by init_node() to look for duplicate nodes
*/
static dev_info_t *
find_duplicate_child(dev_info_t *pdip, dev_info_t *dip)
{
dev_info_t *dup;
char *cname = DEVI(dip)->devi_node_name;
char *caddr = DEVI(dip)->devi_addr;
/* search nodes before dip */
dup = find_sibling(ddi_get_child(pdip), cname, caddr,
FIND_NODE_BY_NODENAME, NULL);
if (dup != dip)
return (dup);
/*
* search nodes after dip; normally this is not needed,
*/
return (find_sibling(ddi_get_next_sibling(dip), cname, caddr,
FIND_NODE_BY_NODENAME, NULL));
}
/*
* Find a child of a given name and address, using a callback to name
* unnamed children. cname is the binding name.
*/
dev_info_t *
ndi_devi_findchild_by_callback(dev_info_t *pdip, char *dname, char *ua,
int (*make_ua)(dev_info_t *, char *, int))
{
int by = FIND_ADDR_BY_CALLBACK;
ASSERT(DEVI_BUSY_OWNED(pdip));
by |= dname ? FIND_NODE_BY_DRIVER : FIND_NODE_BY_ADDR;
return (find_sibling(ddi_get_child(pdip), dname, ua, by, make_ua));
}
/*
* Find a child of a given name and address, invoking initchild to name
* unnamed children. cname is the node name.
*/
static dev_info_t *
find_child_by_name(dev_info_t *pdip, char *cname, char *caddr)
{
dev_info_t *dip;
/* attempt search without changing state of preceding siblings */
dip = find_sibling(ddi_get_child(pdip), cname, caddr,
FIND_NODE_BY_NODENAME, NULL);
if (dip)
return (dip);
return (find_sibling(ddi_get_child(pdip), cname, caddr,
FIND_NODE_BY_NODENAME|FIND_ADDR_BY_INIT, NULL));
}
/*
* Find a child of a given name and address, invoking initchild to name
* unnamed children. cname is the node name.
*/
static dev_info_t *
find_child_by_driver(dev_info_t *pdip, char *cname, char *caddr)
{
dev_info_t *dip;
/* attempt search without changing state of preceding siblings */
dip = find_sibling(ddi_get_child(pdip), cname, caddr,
FIND_NODE_BY_DRIVER, NULL);
if (dip)
return (dip);
return (find_sibling(ddi_get_child(pdip), cname, caddr,
FIND_NODE_BY_DRIVER|FIND_ADDR_BY_INIT, NULL));
}
/*
* Find a child of a given address, invoking initchild to name
* unnamed children. cname is the node name.
*
* NOTE: This function is only used during boot. One would hope that
* unique sibling unit-addresses on hardware branches of the tree would
* be a requirement to avoid two drivers trying to control the same
* piece of hardware. Unfortunately there are some cases where this
* situation exists (/ssm@0,0/pci@1c,700000 /ssm@0,0/sghsc@1c,700000).
* Until unit-address uniqueness of siblings is guaranteed, use of this
* interface for purposes other than boot should be avoided.
*/
static dev_info_t *
find_child_by_addr(dev_info_t *pdip, char *caddr)
{
dev_info_t *dip;
/* return NULL if called without a unit-address */
if ((caddr == NULL) || (*caddr == '\0'))
return (NULL);
/* attempt search without changing state of preceding siblings */
dip = find_sibling(ddi_get_child(pdip), NULL, caddr,
FIND_NODE_BY_ADDR, NULL);
if (dip)
return (dip);
return (find_sibling(ddi_get_child(pdip), NULL, caddr,
FIND_NODE_BY_ADDR|FIND_ADDR_BY_INIT, NULL));
}
/*
* Deleting a property list. Take care, since some property structures
* may not be fully built.
*/
void
i_ddi_prop_list_delete(ddi_prop_t *prop)
{
while (prop) {
ddi_prop_t *next = prop->prop_next;
if (prop->prop_name)
kmem_free(prop->prop_name, strlen(prop->prop_name) + 1);
if ((prop->prop_len != 0) && prop->prop_val)
kmem_free(prop->prop_val, prop->prop_len);
kmem_free(prop, sizeof (struct ddi_prop));
prop = next;
}
}
/*
* Duplicate property list
*/
ddi_prop_t *
i_ddi_prop_list_dup(ddi_prop_t *prop, uint_t flag)
{
ddi_prop_t *result, *prev, *copy;
if (prop == NULL)
return (NULL);
result = prev = NULL;
for (; prop != NULL; prop = prop->prop_next) {
ASSERT(prop->prop_name != NULL);
copy = kmem_zalloc(sizeof (struct ddi_prop), flag);
if (copy == NULL)
goto fail;
copy->prop_dev = prop->prop_dev;
copy->prop_flags = prop->prop_flags;
copy->prop_name = i_ddi_strdup(prop->prop_name, flag);
if (copy->prop_name == NULL)
goto fail;
if ((copy->prop_len = prop->prop_len) != 0) {
copy->prop_val = kmem_zalloc(prop->prop_len, flag);
if (copy->prop_val == NULL)
goto fail;
bcopy(prop->prop_val, copy->prop_val, prop->prop_len);
}
if (prev == NULL)
result = prev = copy;
else
prev->prop_next = copy;
prev = copy;
}
return (result);
fail:
i_ddi_prop_list_delete(result);
return (NULL);
}
/*
* Create a reference property list, currently used only for
* driver global properties. Created with ref count of 1.
*/
ddi_prop_list_t *
i_ddi_prop_list_create(ddi_prop_t *props)
{
ddi_prop_list_t *list = kmem_alloc(sizeof (*list), KM_SLEEP);
list->prop_list = props;
list->prop_ref = 1;
return (list);
}
/*
* Increment/decrement reference count. The reference is
* protected by dn_lock. The only interfaces modifying
* dn_global_prop_ptr is in impl_make[free]_parlist().
*/
void
i_ddi_prop_list_hold(ddi_prop_list_t *prop_list, struct devnames *dnp)
{
ASSERT(prop_list->prop_ref >= 0);
ASSERT(mutex_owned(&dnp->dn_lock));
prop_list->prop_ref++;
}
void
i_ddi_prop_list_rele(ddi_prop_list_t *prop_list, struct devnames *dnp)
{
ASSERT(prop_list->prop_ref > 0);
ASSERT(mutex_owned(&dnp->dn_lock));
prop_list->prop_ref--;
if (prop_list->prop_ref == 0) {
i_ddi_prop_list_delete(prop_list->prop_list);
kmem_free(prop_list, sizeof (*prop_list));
}
}
/*
* Free table of classes by drivers
*/
void
i_ddi_free_exported_classes(char **classes, int n)
{
if ((n == 0) || (classes == NULL))
return;
kmem_free(classes, n * sizeof (char *));
}
/*
* Get all classes exported by dip
*/
int
i_ddi_get_exported_classes(dev_info_t *dip, char ***classes)
{
extern void lock_hw_class_list();
extern void unlock_hw_class_list();
extern int get_class(const char *, char **);
static char *rootclass = "root";
int n = 0, nclass = 0;
char **buf;
ASSERT(i_ddi_node_state(dip) >= DS_BOUND);
if (dip == ddi_root_node()) /* rootnode exports class "root" */
nclass = 1;
lock_hw_class_list();
nclass += get_class(ddi_driver_name(dip), NULL);
if (nclass == 0) {
unlock_hw_class_list();
return (0); /* no class exported */
}
*classes = buf = kmem_alloc(nclass * sizeof (char *), KM_SLEEP);
if (dip == ddi_root_node()) {
*buf++ = rootclass;
n = 1;
}
n += get_class(ddi_driver_name(dip), buf);
unlock_hw_class_list();
ASSERT(n == nclass); /* make sure buf wasn't overrun */
return (nclass);
}
/*
* Helper functions, returns NULL if no memory.
*/
char *
i_ddi_strdup(char *str, uint_t flag)
{
char *copy;
if (str == NULL)
return (NULL);
copy = kmem_alloc(strlen(str) + 1, flag);
if (copy == NULL)
return (NULL);
(void) strcpy(copy, str);
return (copy);
}
/*
* Load driver.conf file for major. Load all if major == -1.
*
* This is called
* - early in boot after devnames array is initialized
* - from vfs code when certain file systems are mounted
* - from add_drv when a new driver is added
*/
int
i_ddi_load_drvconf(major_t major)
{
extern int modrootloaded;
major_t low, high, m;
if (major == DDI_MAJOR_T_NONE) {
low = 0;
high = devcnt - 1;
} else {
if (major >= devcnt)
return (EINVAL);
low = high = major;
}
for (m = low; m <= high; m++) {
struct devnames *dnp = &devnamesp[m];
LOCK_DEV_OPS(&dnp->dn_lock);
dnp->dn_flags &= ~(DN_DRIVER_HELD|DN_DRIVER_INACTIVE);
(void) impl_make_parlist(m);
UNLOCK_DEV_OPS(&dnp->dn_lock);
}
if (modrootloaded) {
ddi_walk_devs(ddi_root_node(), reset_nexus_flags,
(void *)(uintptr_t)major);
}
/* build dn_list from old entries in path_to_inst */
e_ddi_unorphan_instance_nos();
return (0);
}
/*
* Unload a specific driver.conf.
* Don't support unload all because it doesn't make any sense
*/
int
i_ddi_unload_drvconf(major_t major)
{
int error;
struct devnames *dnp;
if (major >= devcnt)
return (EINVAL);
/*
* Take the per-driver lock while unloading driver.conf
*/
dnp = &devnamesp[major];
LOCK_DEV_OPS(&dnp->dn_lock);
error = impl_free_parlist(major);
UNLOCK_DEV_OPS(&dnp->dn_lock);
return (error);
}
/*
* Merge a .conf node. This is called by nexus drivers to augment
* hw node with properties specified in driver.conf file. This function
* takes a callback routine to name nexus children.
* The parent node must be held busy.
*
* It returns DDI_SUCCESS if the node is merged and DDI_FAILURE otherwise.
*/
int
ndi_merge_node(dev_info_t *dip, int (*make_ua)(dev_info_t *, char *, int))
{
dev_info_t *hwdip;
ASSERT(ndi_dev_is_persistent_node(dip) == 0);
ASSERT(ddi_get_name_addr(dip) != NULL);
hwdip = ndi_devi_findchild_by_callback(ddi_get_parent(dip),
ddi_binding_name(dip), ddi_get_name_addr(dip), make_ua);
/*
* Look for the hardware node that is the target of the merge;
* return failure if not found.
*/
if ((hwdip == NULL) || (hwdip == dip)) {
char *buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
NDI_CONFIG_DEBUG((CE_WARN, "No HW node to merge conf node %s",
ddi_deviname(dip, buf)));
kmem_free(buf, MAXNAMELEN);
return (DDI_FAILURE);
}
/*
* Make sure the hardware node is uninitialized and has no property.
* This may not be the case if new .conf files are load after some
* hardware nodes have already been initialized and attached.
*
* N.B. We return success here because the node was *intended*
* to be a merge node because there is a hw node with the name.
*/
mutex_enter(&DEVI(hwdip)->devi_lock);
if (ndi_dev_is_persistent_node(hwdip) == 0) {
char *buf;
mutex_exit(&DEVI(hwdip)->devi_lock);
buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
NDI_CONFIG_DEBUG((CE_NOTE, "Duplicate .conf node %s",
ddi_deviname(dip, buf)));
kmem_free(buf, MAXNAMELEN);
return (DDI_SUCCESS);
}
/*
* If it is possible that the hardware has already been touched
* then don't merge.
*/
if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
(DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
(DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
char *buf;
mutex_exit(&DEVI(hwdip)->devi_lock);
buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
NDI_CONFIG_DEBUG((CE_NOTE,
"!Cannot merge .conf node %s with hw node %p "
"-- not in proper state",
ddi_deviname(dip, buf), (void *)hwdip));
kmem_free(buf, MAXNAMELEN);
return (DDI_SUCCESS);
}
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(hwdip)->devi_sys_prop_ptr = DEVI(dip)->devi_sys_prop_ptr;
DEVI(hwdip)->devi_drv_prop_ptr = DEVI(dip)->devi_drv_prop_ptr;
DEVI(dip)->devi_sys_prop_ptr = NULL;
DEVI(dip)->devi_drv_prop_ptr = NULL;
mutex_exit(&DEVI(dip)->devi_lock);
mutex_exit(&DEVI(hwdip)->devi_lock);
return (DDI_SUCCESS);
}
/*
* Merge a "wildcard" .conf node. This is called by nexus drivers to
* augment a set of hw node with properties specified in driver.conf file.
* The parent node must be held busy.
*
* There is no failure mode, since the nexus may or may not have child
* node bound the driver specified by the wildcard node.
*/
void
ndi_merge_wildcard_node(dev_info_t *dip)
{
dev_info_t *hwdip;
dev_info_t *pdip = ddi_get_parent(dip);
major_t major = ddi_driver_major(dip);
/* never attempt to merge a hw node */
ASSERT(ndi_dev_is_persistent_node(dip) == 0);
/* must be bound to a driver major number */
ASSERT(major != DDI_MAJOR_T_NONE);
/*
* Walk the child list to find all nodes bound to major
* and copy properties.
*/
mutex_enter(&DEVI(dip)->devi_lock);
ASSERT(DEVI_BUSY_OWNED(pdip));
for (hwdip = ddi_get_child(pdip); hwdip;
hwdip = ddi_get_next_sibling(hwdip)) {
/*
* Skip nodes not bound to same driver
*/
if (ddi_driver_major(hwdip) != major)
continue;
/*
* Skip .conf nodes
*/
if (ndi_dev_is_persistent_node(hwdip) == 0)
continue;
/*
* Make sure the node is uninitialized and has no property.
*/
mutex_enter(&DEVI(hwdip)->devi_lock);
if (i_ddi_node_state(hwdip) >= DS_INITIALIZED ||
(DEVI(hwdip)->devi_sys_prop_ptr != NULL) ||
(DEVI(hwdip)->devi_drv_prop_ptr != NULL)) {
mutex_exit(&DEVI(hwdip)->devi_lock);
NDI_CONFIG_DEBUG((CE_NOTE, "HW node %p state not "
"suitable for merging wildcard conf node %s",
(void *)hwdip, ddi_node_name(dip)));
continue;
}
DEVI(hwdip)->devi_sys_prop_ptr =
i_ddi_prop_list_dup(DEVI(dip)->devi_sys_prop_ptr, KM_SLEEP);
DEVI(hwdip)->devi_drv_prop_ptr =
i_ddi_prop_list_dup(DEVI(dip)->devi_drv_prop_ptr, KM_SLEEP);
mutex_exit(&DEVI(hwdip)->devi_lock);
}
mutex_exit(&DEVI(dip)->devi_lock);
}
/*
* Return the major number based on the compatible property. This interface
* may be used in situations where we are trying to detect if a better driver
* now exists for a device, so it must use the 'compatible' property. If
* a non-NULL formp is specified and the binding was based on compatible then
* return the pointer to the form used in *formp.
*/
major_t
ddi_compatible_driver_major(dev_info_t *dip, char **formp)
{
struct dev_info *devi = DEVI(dip);
void *compat;
size_t len;
char *p = NULL;
major_t major = DDI_MAJOR_T_NONE;
if (formp)
*formp = NULL;
if (ddi_prop_exists(DDI_DEV_T_NONE, dip, DDI_PROP_DONTPASS,
"ddi-assigned")) {
major = ddi_name_to_major("nulldriver");
return (major);
}
/*
* Highest precedence binding is a path-oriented alias. Since this
* requires a 'path', this type of binding occurs via more obtuse
* 'rebind'. The need for a path-oriented alias 'rebind' is detected
* after a successful DDI_CTLOPS_INITCHILD to another driver: this is
* is the first point at which the unit-address (or instance) of the
* last component of the path is available (even though the path is
* bound to the wrong driver at this point).
*/
if (devi->devi_flags & DEVI_REBIND) {
p = devi->devi_rebinding_name;
major = ddi_name_to_major(p);
if (driver_installed(major)) {
if (formp)
*formp = p;
return (major);
}
/*
* If for some reason devi_rebinding_name no longer resolves
* to a proper driver then clear DEVI_REBIND.
*/
mutex_enter(&devi->devi_lock);
devi->devi_flags &= ~DEVI_REBIND;
mutex_exit(&devi->devi_lock);
}
/* look up compatible property */
(void) lookup_compatible(dip, KM_SLEEP);
compat = (void *)(devi->devi_compat_names);
len = devi->devi_compat_length;
/* find the highest precedence compatible form with a driver binding */
while ((p = prom_decode_composite_string(compat, len, p)) != NULL) {
major = ddi_name_to_major(p);
if (driver_installed(major)) {
if (formp)
*formp = p;
return (major);
}
}
/*
* none of the compatible forms have a driver binding, see if
* the node name has a driver binding.
*/
major = ddi_name_to_major(ddi_node_name(dip));
if (driver_installed(major))
return (major);
/* no driver */
return (DDI_MAJOR_T_NONE);
}
/*
* Static help functions
*/
/*
* lookup the "compatible" property and cache it's contents in the
* device node.
*/
static int
lookup_compatible(dev_info_t *dip, uint_t flag)
{
int rv;
int prop_flags;
uint_t ncompatstrs;
char **compatstrpp;
char *di_compat_strp;
size_t di_compat_strlen;
if (DEVI(dip)->devi_compat_names) {
return (DDI_SUCCESS);
}
prop_flags = DDI_PROP_TYPE_STRING | DDI_PROP_DONTPASS;
if (flag & KM_NOSLEEP) {
prop_flags |= DDI_PROP_DONTSLEEP;
}
if (ndi_dev_is_prom_node(dip) == 0) {
prop_flags |= DDI_PROP_NOTPROM;
}
rv = ddi_prop_lookup_common(DDI_DEV_T_ANY, dip, prop_flags,
"compatible", &compatstrpp, &ncompatstrs,
ddi_prop_fm_decode_strings);
if (rv == DDI_PROP_NOT_FOUND) {
return (DDI_SUCCESS);
}
if (rv != DDI_PROP_SUCCESS) {
return (DDI_FAILURE);
}
/*
* encode the compatible property data in the dev_info node
*/
rv = DDI_SUCCESS;
if (ncompatstrs != 0) {
di_compat_strp = encode_composite_string(compatstrpp,
ncompatstrs, &di_compat_strlen, flag);
if (di_compat_strp != NULL) {
DEVI(dip)->devi_compat_names = di_compat_strp;
DEVI(dip)->devi_compat_length = di_compat_strlen;
} else {
rv = DDI_FAILURE;
}
}
ddi_prop_free(compatstrpp);
return (rv);
}
/*
* Create a composite string from a list of strings.
*
* A composite string consists of a single buffer containing one
* or more NULL terminated strings.
*/
static char *
encode_composite_string(char **strings, uint_t nstrings, size_t *retsz,
uint_t flag)
{
uint_t index;
char **strpp;
uint_t slen;
size_t cbuf_sz = 0;
char *cbuf_p;
char *cbuf_ip;
if (strings == NULL || nstrings == 0 || retsz == NULL) {
return (NULL);
}
for (index = 0, strpp = strings; index < nstrings; index++)
cbuf_sz += strlen(*(strpp++)) + 1;
if ((cbuf_p = kmem_alloc(cbuf_sz, flag)) == NULL) {
cmn_err(CE_NOTE,
"?failed to allocate device node compatstr");
return (NULL);
}
cbuf_ip = cbuf_p;
for (index = 0, strpp = strings; index < nstrings; index++) {
slen = strlen(*strpp);
bcopy(*(strpp++), cbuf_ip, slen);
cbuf_ip += slen;
*(cbuf_ip++) = '\0';
}
*retsz = cbuf_sz;
return (cbuf_p);
}
static void
link_to_driver_list(dev_info_t *dip)
{
major_t major = DEVI(dip)->devi_major;
struct devnames *dnp;
ASSERT(major != DDI_MAJOR_T_NONE);
/*
* Remove from orphan list
*/
if (ndi_dev_is_persistent_node(dip)) {
dnp = &orphanlist;
remove_from_dn_list(dnp, dip);
}
/*
* Add to per driver list
*/
dnp = &devnamesp[major];
add_to_dn_list(dnp, dip);
}
static void
unlink_from_driver_list(dev_info_t *dip)
{
major_t major = DEVI(dip)->devi_major;
struct devnames *dnp;
ASSERT(major != DDI_MAJOR_T_NONE);
/*
* Remove from per-driver list
*/
dnp = &devnamesp[major];
remove_from_dn_list(dnp, dip);
/*
* Add to orphan list
*/
if (ndi_dev_is_persistent_node(dip)) {
dnp = &orphanlist;
add_to_dn_list(dnp, dip);
}
}
/*
* scan the per-driver list looking for dev_info "dip"
*/
static dev_info_t *
in_dn_list(struct devnames *dnp, dev_info_t *dip)
{
struct dev_info *idevi;
if ((idevi = DEVI(dnp->dn_head)) == NULL)
return (NULL);
while (idevi) {
if (idevi == DEVI(dip))
return (dip);
idevi = idevi->devi_next;
}
return (NULL);
}
/*
* insert devinfo node 'dip' into the per-driver instance list
* headed by 'dnp'
*
* Nodes on the per-driver list are ordered: HW - SID - PSEUDO. The order is
* required for merging of .conf file data to work properly.
*/
static void
add_to_ordered_dn_list(struct devnames *dnp, dev_info_t *dip)
{
dev_info_t **dipp;
ASSERT(mutex_owned(&(dnp->dn_lock)));
dipp = &dnp->dn_head;
if (ndi_dev_is_prom_node(dip)) {
/*
* Find the first non-prom node or end of list
*/
while (*dipp && (ndi_dev_is_prom_node(*dipp) != 0)) {
dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
}
} else if (ndi_dev_is_persistent_node(dip)) {
/*
* Find the first non-persistent node
*/
while (*dipp && (ndi_dev_is_persistent_node(*dipp) != 0)) {
dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
}
} else {
/*
* Find the end of the list
*/
while (*dipp) {
dipp = (dev_info_t **)&DEVI(*dipp)->devi_next;
}
}
DEVI(dip)->devi_next = DEVI(*dipp);
*dipp = dip;
}
/*
* add a list of device nodes to the device node list in the
* devnames structure
*/
static void
add_to_dn_list(struct devnames *dnp, dev_info_t *dip)
{
/*
* Look to see if node already exists
*/
LOCK_DEV_OPS(&(dnp->dn_lock));
if (in_dn_list(dnp, dip)) {
cmn_err(CE_NOTE, "add_to_dn_list: node %s already in list",
DEVI(dip)->devi_node_name);
} else {
add_to_ordered_dn_list(dnp, dip);
}
UNLOCK_DEV_OPS(&(dnp->dn_lock));
}
static void
remove_from_dn_list(struct devnames *dnp, dev_info_t *dip)
{
dev_info_t **plist;
LOCK_DEV_OPS(&(dnp->dn_lock));
plist = (dev_info_t **)&dnp->dn_head;
while (*plist && (*plist != dip)) {
plist = (dev_info_t **)&DEVI(*plist)->devi_next;
}
if (*plist != NULL) {
ASSERT(*plist == dip);
*plist = (dev_info_t *)(DEVI(dip)->devi_next);
DEVI(dip)->devi_next = NULL;
} else {
NDI_CONFIG_DEBUG((CE_NOTE,
"remove_from_dn_list: node %s not found in list",
DEVI(dip)->devi_node_name));
}
UNLOCK_DEV_OPS(&(dnp->dn_lock));
}
/*
* Add and remove reference driver global property list
*/
static void
add_global_props(dev_info_t *dip)
{
struct devnames *dnp;
ddi_prop_list_t *plist;
ASSERT(DEVI(dip)->devi_global_prop_list == NULL);
ASSERT(DEVI(dip)->devi_major != DDI_MAJOR_T_NONE);
dnp = &devnamesp[DEVI(dip)->devi_major];
LOCK_DEV_OPS(&dnp->dn_lock);
plist = dnp->dn_global_prop_ptr;
if (plist == NULL) {
UNLOCK_DEV_OPS(&dnp->dn_lock);
return;
}
i_ddi_prop_list_hold(plist, dnp);
UNLOCK_DEV_OPS(&dnp->dn_lock);
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(dip)->devi_global_prop_list = plist;
mutex_exit(&DEVI(dip)->devi_lock);
}
static void
remove_global_props(dev_info_t *dip)
{
ddi_prop_list_t *proplist;
mutex_enter(&DEVI(dip)->devi_lock);
proplist = DEVI(dip)->devi_global_prop_list;
DEVI(dip)->devi_global_prop_list = NULL;
mutex_exit(&DEVI(dip)->devi_lock);
if (proplist) {
major_t major;
struct devnames *dnp;
major = ddi_driver_major(dip);
ASSERT(major != DDI_MAJOR_T_NONE);
dnp = &devnamesp[major];
LOCK_DEV_OPS(&dnp->dn_lock);
i_ddi_prop_list_rele(proplist, dnp);
UNLOCK_DEV_OPS(&dnp->dn_lock);
}
}
#ifdef DEBUG
/*
* Set this variable to '0' to disable the optimization,
* and to 2 to print debug message.
*/
static int optimize_dtree = 1;
static void
debug_dtree(dev_info_t *devi, struct dev_info *adevi, char *service)
{
char *adeviname, *buf;
/*
* Don't print unless optimize dtree is set to 2+
*/
if (optimize_dtree <= 1)
return;
buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
adeviname = ddi_deviname((dev_info_t *)adevi, buf);
if (*adeviname == '\0')
adeviname = "root";
cmn_err(CE_CONT, "%s %s -> %s\n",
ddi_deviname(devi, buf), service, adeviname);
kmem_free(buf, MAXNAMELEN);
}
#else /* DEBUG */
#define debug_dtree(a1, a2, a3) /* nothing */
#endif /* DEBUG */
static void
ddi_optimize_dtree(dev_info_t *devi)
{
struct dev_info *pdevi;
struct bus_ops *b;
pdevi = DEVI(devi)->devi_parent;
ASSERT(pdevi);
/*
* Set the unoptimized values
*/
DEVI(devi)->devi_bus_map_fault = pdevi;
DEVI(devi)->devi_bus_dma_map = pdevi;
DEVI(devi)->devi_bus_dma_allochdl = pdevi;
DEVI(devi)->devi_bus_dma_freehdl = pdevi;
DEVI(devi)->devi_bus_dma_bindhdl = pdevi;
DEVI(devi)->devi_bus_dma_bindfunc =
pdevi->devi_ops->devo_bus_ops->bus_dma_bindhdl;
DEVI(devi)->devi_bus_dma_unbindhdl = pdevi;
DEVI(devi)->devi_bus_dma_unbindfunc =
pdevi->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
DEVI(devi)->devi_bus_dma_flush = pdevi;
DEVI(devi)->devi_bus_dma_win = pdevi;
DEVI(devi)->devi_bus_dma_ctl = pdevi;
DEVI(devi)->devi_bus_ctl = pdevi;
#ifdef DEBUG
if (optimize_dtree == 0)
return;
#endif /* DEBUG */
b = pdevi->devi_ops->devo_bus_ops;
if (i_ddi_map_fault == b->bus_map_fault) {
DEVI(devi)->devi_bus_map_fault = pdevi->devi_bus_map_fault;
debug_dtree(devi, DEVI(devi)->devi_bus_map_fault,
"bus_map_fault");
}
if (ddi_dma_map == b->bus_dma_map) {
DEVI(devi)->devi_bus_dma_map = pdevi->devi_bus_dma_map;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_map, "bus_dma_map");
}
if (ddi_dma_allochdl == b->bus_dma_allochdl) {
DEVI(devi)->devi_bus_dma_allochdl =
pdevi->devi_bus_dma_allochdl;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_allochdl,
"bus_dma_allochdl");
}
if (ddi_dma_freehdl == b->bus_dma_freehdl) {
DEVI(devi)->devi_bus_dma_freehdl = pdevi->devi_bus_dma_freehdl;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_freehdl,
"bus_dma_freehdl");
}
if (ddi_dma_bindhdl == b->bus_dma_bindhdl) {
DEVI(devi)->devi_bus_dma_bindhdl = pdevi->devi_bus_dma_bindhdl;
DEVI(devi)->devi_bus_dma_bindfunc =
pdevi->devi_bus_dma_bindhdl->devi_ops->
devo_bus_ops->bus_dma_bindhdl;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_bindhdl,
"bus_dma_bindhdl");
}
if (ddi_dma_unbindhdl == b->bus_dma_unbindhdl) {
DEVI(devi)->devi_bus_dma_unbindhdl =
pdevi->devi_bus_dma_unbindhdl;
DEVI(devi)->devi_bus_dma_unbindfunc =
pdevi->devi_bus_dma_unbindhdl->devi_ops->
devo_bus_ops->bus_dma_unbindhdl;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_unbindhdl,
"bus_dma_unbindhdl");
}
if (ddi_dma_flush == b->bus_dma_flush) {
DEVI(devi)->devi_bus_dma_flush = pdevi->devi_bus_dma_flush;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_flush,
"bus_dma_flush");
}
if (ddi_dma_win == b->bus_dma_win) {
DEVI(devi)->devi_bus_dma_win = pdevi->devi_bus_dma_win;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_win,
"bus_dma_win");
}
if (ddi_dma_mctl == b->bus_dma_ctl) {
DEVI(devi)->devi_bus_dma_ctl = pdevi->devi_bus_dma_ctl;
debug_dtree(devi, DEVI(devi)->devi_bus_dma_ctl, "bus_dma_ctl");
}
if (ddi_ctlops == b->bus_ctl) {
DEVI(devi)->devi_bus_ctl = pdevi->devi_bus_ctl;
debug_dtree(devi, DEVI(devi)->devi_bus_ctl, "bus_ctl");
}
}
#define MIN_DEVINFO_LOG_SIZE max_ncpus
#define MAX_DEVINFO_LOG_SIZE max_ncpus * 10
static void
da_log_init()
{
devinfo_log_header_t *dh;
int logsize = devinfo_log_size;
if (logsize == 0)
logsize = MIN_DEVINFO_LOG_SIZE;
else if (logsize > MAX_DEVINFO_LOG_SIZE)
logsize = MAX_DEVINFO_LOG_SIZE;
dh = kmem_alloc(logsize * PAGESIZE, KM_SLEEP);
mutex_init(&dh->dh_lock, NULL, MUTEX_DEFAULT, NULL);
dh->dh_max = ((logsize * PAGESIZE) - sizeof (*dh)) /
sizeof (devinfo_audit_t) + 1;
dh->dh_curr = -1;
dh->dh_hits = 0;
devinfo_audit_log = dh;
}
/*
* Log the stack trace in per-devinfo audit structure and also enter
* it into a system wide log for recording the time history.
*/
static void
da_log_enter(dev_info_t *dip)
{
devinfo_audit_t *da_log, *da = DEVI(dip)->devi_audit;
devinfo_log_header_t *dh = devinfo_audit_log;
if (devinfo_audit_log == NULL)
return;
ASSERT(da != NULL);
da->da_devinfo = dip;
da->da_timestamp = gethrtime();
da->da_thread = curthread;
da->da_node_state = DEVI(dip)->devi_node_state;
da->da_device_state = DEVI(dip)->devi_state;
da->da_depth = getpcstack(da->da_stack, DDI_STACK_DEPTH);
/*
* Copy into common log and note the location for tracing history
*/
mutex_enter(&dh->dh_lock);
dh->dh_hits++;
dh->dh_curr++;
if (dh->dh_curr >= dh->dh_max)
dh->dh_curr -= dh->dh_max;
da_log = &dh->dh_entry[dh->dh_curr];
mutex_exit(&dh->dh_lock);
bcopy(da, da_log, sizeof (devinfo_audit_t));
da->da_lastlog = da_log;
}
static void
attach_drivers()
{
int i;
for (i = 0; i < devcnt; i++) {
struct devnames *dnp = &devnamesp[i];
if ((dnp->dn_flags & DN_FORCE_ATTACH) &&
(ddi_hold_installed_driver((major_t)i) != NULL))
ddi_rele_driver((major_t)i);
}
}
/*
* Launch a thread to force attach drivers. This avoids penalty on boot time.
*/
void
i_ddi_forceattach_drivers()
{
/*
* Attach IB VHCI driver before the force-attach thread attaches the
* IB HCA driver. IB HCA driver will fail if IB Nexus has not yet
* been attached.
*/
(void) ddi_hold_installed_driver(ddi_name_to_major("ib"));
(void) thread_create(NULL, 0, (void (*)())attach_drivers, NULL, 0, &p0,
TS_RUN, minclsyspri);
}
/*
* This is a private DDI interface for optimizing boot performance.
* I/O subsystem initialization is considered complete when devfsadm
* is executed.
*
* NOTE: The start of syseventd happens to be a convenient indicator
* of the completion of I/O initialization during boot.
* The implementation should be replaced by something more robust.
*/
int
i_ddi_io_initialized()
{
extern int sysevent_daemon_init;
return (sysevent_daemon_init);
}
/*
* May be used to determine system boot state
* "Available" means the system is for the most part up
* and initialized, with all system services either up or
* capable of being started. This state is set by devfsadm
* during the boot process. The /dev filesystem infers
* from this when implicit reconfig can be performed,
* ie, devfsadm can be invoked. Please avoid making
* further use of this unless it's really necessary.
*/
int
i_ddi_sysavail()
{
return (devname_state & DS_SYSAVAIL);
}
/*
* May be used to determine if boot is a reconfigure boot.
*/
int
i_ddi_reconfig()
{
return (devname_state & DS_RECONFIG);
}
/*
* Note system services are up, inform /dev.
*/
void
i_ddi_set_sysavail()
{
if ((devname_state & DS_SYSAVAIL) == 0) {
devname_state |= DS_SYSAVAIL;
sdev_devstate_change();
}
}
/*
* Note reconfiguration boot, inform /dev.
*/
void
i_ddi_set_reconfig()
{
if ((devname_state & DS_RECONFIG) == 0) {
devname_state |= DS_RECONFIG;
sdev_devstate_change();
}
}
/*
* device tree walking
*/
struct walk_elem {
struct walk_elem *next;
dev_info_t *dip;
};
static void
free_list(struct walk_elem *list)
{
while (list) {
struct walk_elem *next = list->next;
kmem_free(list, sizeof (*list));
list = next;
}
}
static void
append_node(struct walk_elem **list, dev_info_t *dip)
{
struct walk_elem *tail;
struct walk_elem *elem = kmem_alloc(sizeof (*elem), KM_SLEEP);
elem->next = NULL;
elem->dip = dip;
if (*list == NULL) {
*list = elem;
return;
}
tail = *list;
while (tail->next)
tail = tail->next;
tail->next = elem;
}
/*
* The implementation of ddi_walk_devs().
*/
static int
walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg,
int do_locking)
{
struct walk_elem *head = NULL;
/*
* Do it in two passes. First pass invoke callback on each
* dip on the sibling list. Second pass invoke callback on
* children of each dip.
*/
while (dip) {
switch ((*f)(dip, arg)) {
case DDI_WALK_TERMINATE:
free_list(head);
return (DDI_WALK_TERMINATE);
case DDI_WALK_PRUNESIB:
/* ignore sibling by setting dip to NULL */
append_node(&head, dip);
dip = NULL;
break;
case DDI_WALK_PRUNECHILD:
/* don't worry about children */
dip = ddi_get_next_sibling(dip);
break;
case DDI_WALK_CONTINUE:
default:
append_node(&head, dip);
dip = ddi_get_next_sibling(dip);
break;
}
}
/* second pass */
while (head) {
int circ;
struct walk_elem *next = head->next;
if (do_locking)
ndi_devi_enter(head->dip, &circ);
if (walk_devs(ddi_get_child(head->dip), f, arg, do_locking) ==
DDI_WALK_TERMINATE) {
if (do_locking)
ndi_devi_exit(head->dip, circ);
free_list(head);
return (DDI_WALK_TERMINATE);
}
if (do_locking)
ndi_devi_exit(head->dip, circ);
kmem_free(head, sizeof (*head));
head = next;
}
return (DDI_WALK_CONTINUE);
}
/*
* This general-purpose routine traverses the tree of dev_info nodes,
* starting from the given node, and calls the given function for each
* node that it finds with the current node and the pointer arg (which
* can point to a structure of information that the function
* needs) as arguments.
*
* It does the walk a layer at a time, not depth-first. The given function
* must return one of the following values:
* DDI_WALK_CONTINUE
* DDI_WALK_PRUNESIB
* DDI_WALK_PRUNECHILD
* DDI_WALK_TERMINATE
*
* N.B. Since we walk the sibling list, the caller must ensure that
* the parent of dip is held against changes, unless the parent
* is rootnode. ndi_devi_enter() on the parent is sufficient.
*
* To avoid deadlock situations, caller must not attempt to
* configure/unconfigure/remove device node in (*f)(), nor should
* it attempt to recurse on other nodes in the system. Any
* ndi_devi_enter() done by (*f)() must occur 'at-or-below' the
* node entered prior to ddi_walk_devs(). Furthermore, if (*f)()
* does any multi-threading (in framework *or* in driver) then the
* ndi_devi_enter() calls done by dependent threads must be
* 'strictly-below'.
*
* This is not callable from device autoconfiguration routines.
* They include, but not limited to, _init(9e), _fini(9e), probe(9e),
* attach(9e), and detach(9e).
*/
void
ddi_walk_devs(dev_info_t *dip, int (*f)(dev_info_t *, void *), void *arg)
{
ASSERT(dip == NULL || ddi_get_parent(dip) == NULL ||
DEVI_BUSY_OWNED(ddi_get_parent(dip)));
(void) walk_devs(dip, f, arg, 1);
}
/*
* This is a general-purpose routine traverses the per-driver list
* and calls the given function for each node. must return one of
* the following values:
* DDI_WALK_CONTINUE
* DDI_WALK_TERMINATE
*
* N.B. The same restrictions from ddi_walk_devs() apply.
*/
void
e_ddi_walk_driver(char *drv, int (*f)(dev_info_t *, void *), void *arg)
{
major_t major;
struct devnames *dnp;
dev_info_t *dip;
major = ddi_name_to_major(drv);
if (major == DDI_MAJOR_T_NONE)
return;
dnp = &devnamesp[major];
LOCK_DEV_OPS(&dnp->dn_lock);
dip = dnp->dn_head;
while (dip) {
ndi_hold_devi(dip);
UNLOCK_DEV_OPS(&dnp->dn_lock);
if ((*f)(dip, arg) == DDI_WALK_TERMINATE) {
ndi_rele_devi(dip);
return;
}
LOCK_DEV_OPS(&dnp->dn_lock);
ndi_rele_devi(dip);
dip = ddi_get_next(dip);
}
UNLOCK_DEV_OPS(&dnp->dn_lock);
}
/*
* argument to i_find_devi, a devinfo node search callback function.
*/
struct match_info {
dev_info_t *dip; /* result */
char *nodename; /* if non-null, nodename must match */
int instance; /* if != -1, instance must match */
int attached; /* if != 0, i_ddi_devi_attached() */
};
static int
i_find_devi(dev_info_t *dip, void *arg)
{
struct match_info *info = (struct match_info *)arg;
if (((info->nodename == NULL) ||
(strcmp(ddi_node_name(dip), info->nodename) == 0)) &&
((info->instance == -1) ||
(ddi_get_instance(dip) == info->instance)) &&
((info->attached == 0) || i_ddi_devi_attached(dip))) {
info->dip = dip;
ndi_hold_devi(dip);
return (DDI_WALK_TERMINATE);
}
return (DDI_WALK_CONTINUE);
}
/*
* Find dip with a known node name and instance and return with it held
*/
dev_info_t *
ddi_find_devinfo(char *nodename, int instance, int attached)
{
struct match_info info;
info.nodename = nodename;
info.instance = instance;
info.attached = attached;
info.dip = NULL;
ddi_walk_devs(ddi_root_node(), i_find_devi, &info);
return (info.dip);
}
extern ib_boot_prop_t *iscsiboot_prop;
static void
i_ddi_parse_iscsi_name(char *name, char **nodename, char **addrname,
char **minorname)
{
char *cp, *colon;
static char nulladdrname[] = "";
/* default values */
if (nodename)
*nodename = name;
if (addrname)
*addrname = nulladdrname;
if (minorname)
*minorname = NULL;
cp = colon = name;
while (*cp != '\0') {
if (addrname && *cp == '@') {
*addrname = cp + 1;
*cp = '\0';
} else if (minorname && *cp == ':') {
*minorname = cp + 1;
colon = cp;
}
++cp;
}
if (colon != name) {
*colon = '\0';
}
}
/*
* Parse for name, addr, and minor names. Some args may be NULL.
*/
void
i_ddi_parse_name(char *name, char **nodename, char **addrname, char **minorname)
{
char *cp;
static char nulladdrname[] = "";
/* default values */
if (nodename)
*nodename = name;
if (addrname)
*addrname = nulladdrname;
if (minorname)
*minorname = NULL;
cp = name;
while (*cp != '\0') {
if (addrname && *cp == '@') {
*addrname = cp + 1;
*cp = '\0';
} else if (minorname && *cp == ':') {
*minorname = cp + 1;
*cp = '\0';
}
++cp;
}
}
static char *
child_path_to_driver(dev_info_t *parent, char *child_name, char *unit_address)
{
char *p, *drvname = NULL;
major_t maj;
/*
* Construct the pathname and ask the implementation
* if it can do a driver = f(pathname) for us, if not
* we'll just default to using the node-name that
* was given to us. We want to do this first to
* allow the platform to use 'generic' names for
* legacy device drivers.
*/
p = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(parent, p);
(void) strcat(p, "/");
(void) strcat(p, child_name);
if (unit_address && *unit_address) {
(void) strcat(p, "@");
(void) strcat(p, unit_address);
}
/*
* Get the binding. If there is none, return the child_name
* and let the caller deal with it.
*/
maj = path_to_major(p);
kmem_free(p, MAXPATHLEN);
if (maj != DDI_MAJOR_T_NONE)
drvname = ddi_major_to_name(maj);
if (drvname == NULL)
drvname = child_name;
return (drvname);
}
#define PCI_EX_CLASS "pciexclass"
#define PCI_EX "pciex"
#define PCI_CLASS "pciclass"
#define PCI "pci"
int
ddi_is_pci_dip(dev_info_t *dip)
{
char *prop = NULL;
if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
"compatible", &prop) == DDI_PROP_SUCCESS) {
ASSERT(prop);
if (strncmp(prop, PCI_EX_CLASS, sizeof (PCI_EX_CLASS) - 1)
== 0 ||
strncmp(prop, PCI_EX, sizeof (PCI_EX)- 1)
== 0 ||
strncmp(prop, PCI_CLASS, sizeof (PCI_CLASS) - 1)
== 0 ||
strncmp(prop, PCI, sizeof (PCI) - 1)
== 0) {
ddi_prop_free(prop);
return (1);
}
}
if (prop != NULL) {
ddi_prop_free(prop);
}
return (0);
}
/*
* Given the pathname of a device, fill in the dev_info_t value and/or the
* dev_t value and/or the spectype, depending on which parameters are non-NULL.
* If there is an error, this function returns -1.
*
* NOTE: If this function returns the dev_info_t structure, then it
* does so with a hold on the devi. Caller should ensure that they get
* decremented via ddi_release_devi() or ndi_rele_devi();
*
* This function can be invoked in the boot case for a pathname without
* device argument (:xxxx), traditionally treated as a minor name.
* In this case, we do the following
* (1) search the minor node of type DDM_DEFAULT.
* (2) if no DDM_DEFAULT minor exists, then the first non-alias minor is chosen.
* (3) if neither exists, a dev_t is faked with minor number = instance.
* As of S9 FCS, no instance of #1 exists. #2 is used by several platforms
* to default the boot partition to :a possibly by other OBP definitions.
* #3 is used for booting off network interfaces, most SPARC network
* drivers support Style-2 only, so only DDM_ALIAS minor exists.
*
* It is possible for OBP to present device args at the end of the path as
* well as in the middle. For example, with IB the following strings are
* valid boot paths.
* a /pci@8,700000/ib@1,2:port=1,pkey=ff,dhcp,...
* b /pci@8,700000/ib@1,1:port=1/ioc@xxxxxx,yyyyyyy:dhcp
* Case (a), we first look for minor node "port=1,pkey...".
* Failing that, we will pass "port=1,pkey..." to the bus_config
* entry point of ib (HCA) driver.
* Case (b), configure ib@1,1 as usual. Then invoke ib's bus_config
* with argument "ioc@xxxxxxx,yyyyyyy:port=1". After configuring
* the ioc, look for minor node dhcp. If not found, pass ":dhcp"
* to ioc's bus_config entry point.
*/
int
resolve_pathname(char *pathname,
dev_info_t **dipp, dev_t *devtp, int *spectypep)
{
int error;
dev_info_t *parent, *child;
struct pathname pn;
char *component, *config_name;
char *minorname = NULL;
char *prev_minor = NULL;
dev_t devt = NODEV;
int spectype;
struct ddi_minor_data *dmn;
int circ;
if (*pathname != '/')
return (EINVAL);
parent = ddi_root_node(); /* Begin at the top of the tree */
if (error = pn_get(pathname, UIO_SYSSPACE, &pn))
return (error);
pn_skipslash(&pn);
ASSERT(i_ddi_devi_attached(parent));
ndi_hold_devi(parent);
component = kmem_alloc(MAXNAMELEN, KM_SLEEP);
config_name = kmem_alloc(MAXNAMELEN, KM_SLEEP);
while (pn_pathleft(&pn)) {
/* remember prev minor (:xxx) in the middle of path */
if (minorname)
prev_minor = i_ddi_strdup(minorname, KM_SLEEP);
/* Get component and chop off minorname */
(void) pn_getcomponent(&pn, component);
if ((iscsiboot_prop != NULL) &&
(strcmp((DEVI(parent)->devi_node_name), "iscsi") == 0)) {
i_ddi_parse_iscsi_name(component, NULL, NULL,
&minorname);
} else {
i_ddi_parse_name(component, NULL, NULL, &minorname);
}
if (prev_minor == NULL) {
(void) snprintf(config_name, MAXNAMELEN, "%s",
component);
} else {
(void) snprintf(config_name, MAXNAMELEN, "%s:%s",
component, prev_minor);
kmem_free(prev_minor, strlen(prev_minor) + 1);
prev_minor = NULL;
}
/*
* Find and configure the child
*/
if (ndi_devi_config_one(parent, config_name, &child,
NDI_PROMNAME | NDI_NO_EVENT) != NDI_SUCCESS) {
ndi_rele_devi(parent);
pn_free(&pn);
kmem_free(component, MAXNAMELEN);
kmem_free(config_name, MAXNAMELEN);
return (-1);
}
ASSERT(i_ddi_devi_attached(child));
ndi_rele_devi(parent);
parent = child;
pn_skipslash(&pn);
}
/*
* First look for a minor node matching minorname.
* Failing that, try to pass minorname to bus_config().
*/
if (minorname && i_ddi_minorname_to_devtspectype(parent,
minorname, &devt, &spectype) == DDI_FAILURE) {
(void) snprintf(config_name, MAXNAMELEN, "%s", minorname);
if (ndi_devi_config_obp_args(parent,
config_name, &child, 0) != NDI_SUCCESS) {
ndi_rele_devi(parent);
pn_free(&pn);
kmem_free(component, MAXNAMELEN);
kmem_free(config_name, MAXNAMELEN);
NDI_CONFIG_DEBUG((CE_NOTE,
"%s: minor node not found\n", pathname));
return (-1);
}
minorname = NULL; /* look for default minor */
ASSERT(i_ddi_devi_attached(child));
ndi_rele_devi(parent);
parent = child;
}
if (devtp || spectypep) {
if (minorname == NULL) {
/*
* Search for a default entry with an active
* ndi_devi_enter to protect the devi_minor list.
*/
ndi_devi_enter(parent, &circ);
for (dmn = DEVI(parent)->devi_minor; dmn;
dmn = dmn->next) {
if (dmn->type == DDM_DEFAULT) {
devt = dmn->ddm_dev;
spectype = dmn->ddm_spec_type;
break;
}
}
if (devt == NODEV) {
/*
* No default minor node, try the first one;
* else, assume 1-1 instance-minor mapping
*/
dmn = DEVI(parent)->devi_minor;
if (dmn && ((dmn->type == DDM_MINOR) ||
(dmn->type == DDM_INTERNAL_PATH))) {
devt = dmn->ddm_dev;
spectype = dmn->ddm_spec_type;
} else {
devt = makedevice(
DEVI(parent)->devi_major,
ddi_get_instance(parent));
spectype = S_IFCHR;
}
}
ndi_devi_exit(parent, circ);
}
if (devtp)
*devtp = devt;
if (spectypep)
*spectypep = spectype;
}
pn_free(&pn);
kmem_free(component, MAXNAMELEN);
kmem_free(config_name, MAXNAMELEN);
/*
* If there is no error, return the appropriate parameters
*/
if (dipp != NULL)
*dipp = parent;
else {
/*
* We should really keep the ref count to keep the node from
* detaching but ddi_pathname_to_dev_t() specifies a NULL dipp,
* so we have no way of passing back the held dip. Not holding
* the dip allows detaches to occur - which can cause problems
* for subsystems which call ddi_pathname_to_dev_t (console).
*
* Instead of holding the dip, we place a ddi-no-autodetach
* property on the node to prevent auto detaching.
*
* The right fix is to remove ddi_pathname_to_dev_t and replace
* it, and all references, with a call that specifies a dipp.
* In addition, the callers of this new interfaces would then
* need to call ndi_rele_devi when the reference is complete.
*
*/
(void) ddi_prop_update_int(DDI_DEV_T_NONE, parent,
DDI_NO_AUTODETACH, 1);
ndi_rele_devi(parent);
}
return (0);
}
/*
* Given the pathname of a device, return the dev_t of the corresponding
* device. Returns NODEV on failure.
*
* Note that this call sets the DDI_NO_AUTODETACH property on the devinfo node.
*/
dev_t
ddi_pathname_to_dev_t(char *pathname)
{
dev_t devt;
int error;
error = resolve_pathname(pathname, NULL, &devt, NULL);
return (error ? NODEV : devt);
}
/*
* Translate a prom pathname to kernel devfs pathname.
* Caller is assumed to allocate devfspath memory of
* size at least MAXPATHLEN
*
* The prom pathname may not include minor name, but
* devfs pathname has a minor name portion.
*/
int
i_ddi_prompath_to_devfspath(char *prompath, char *devfspath)
{
dev_t devt = (dev_t)NODEV;
dev_info_t *dip = NULL;
char *minor_name = NULL;
int spectype;
int error;
int circ;
error = resolve_pathname(prompath, &dip, &devt, &spectype);
if (error)
return (DDI_FAILURE);
ASSERT(dip && devt != NODEV);
/*
* Get in-kernel devfs pathname
*/
(void) ddi_pathname(dip, devfspath);
ndi_devi_enter(dip, &circ);
minor_name = i_ddi_devtspectype_to_minorname(dip, devt, spectype);
if (minor_name) {
(void) strcat(devfspath, ":");
(void) strcat(devfspath, minor_name);
} else {
/*
* If minor_name is NULL, we have an alias minor node.
* So manufacture a path to the corresponding clone minor.
*/
(void) snprintf(devfspath, MAXPATHLEN, "%s:%s",
CLONE_PATH, ddi_driver_name(dip));
}
ndi_devi_exit(dip, circ);
/* release hold from resolve_pathname() */
ndi_rele_devi(dip);
return (0);
}
/*
* This function is intended to identify drivers that must quiesce for fast
* reboot to succeed. It does not claim to have more knowledge about the device
* than its driver. If a driver has implemented quiesce(), it will be invoked;
* if a so identified driver does not manage any device that needs to be
* quiesced, it must explicitly set its devo_quiesce dev_op to
* ddi_quiesce_not_needed.
*/
static int skip_pseudo = 1; /* Skip pseudo devices */
static int skip_non_hw = 1; /* Skip devices with no hardware property */
static int
should_implement_quiesce(dev_info_t *dip)
{
struct dev_info *devi = DEVI(dip);
dev_info_t *pdip;
/*
* If dip is pseudo and skip_pseudo is set, driver doesn't have to
* implement quiesce().
*/
if (skip_pseudo &&
strncmp(ddi_binding_name(dip), "pseudo", sizeof ("pseudo")) == 0)
return (0);
/*
* If parent dip is pseudo and skip_pseudo is set, driver doesn't have
* to implement quiesce().
*/
if (skip_pseudo && (pdip = ddi_get_parent(dip)) != NULL &&
strncmp(ddi_binding_name(pdip), "pseudo", sizeof ("pseudo")) == 0)
return (0);
/*
* If not attached, driver doesn't have to implement quiesce().
*/
if (!i_ddi_devi_attached(dip))
return (0);
/*
* If dip has no hardware property and skip_non_hw is set,
* driver doesn't have to implement quiesce().
*/
if (skip_non_hw && devi->devi_hw_prop_ptr == NULL)
return (0);
return (1);
}
static int
driver_has_quiesce(struct dev_ops *ops)
{
if ((ops->devo_rev >= 4) && (ops->devo_quiesce != nodev) &&
(ops->devo_quiesce != NULL) && (ops->devo_quiesce != nulldev) &&
(ops->devo_quiesce != ddi_quiesce_not_supported))
return (1);
else
return (0);
}
/*
* Check to see if a driver has implemented the quiesce() DDI function.
*/
int
check_driver_quiesce(dev_info_t *dip, void *arg)
{
struct dev_ops *ops;
if (!should_implement_quiesce(dip))
return (DDI_WALK_CONTINUE);
if ((ops = ddi_get_driver(dip)) == NULL)
return (DDI_WALK_CONTINUE);
if (driver_has_quiesce(ops)) {
if ((quiesce_debug & 0x2) == 0x2) {
if (ops->devo_quiesce == ddi_quiesce_not_needed)
cmn_err(CE_CONT, "%s does not need to be "
"quiesced", ddi_driver_name(dip));
else
cmn_err(CE_CONT, "%s has quiesce routine",
ddi_driver_name(dip));
}
} else {
if (arg != NULL)
*((int *)arg) = -1;
cmn_err(CE_WARN, "%s has no quiesce()", ddi_driver_name(dip));
}
return (DDI_WALK_CONTINUE);
}
/*
* Quiesce device.
*/
static void
quiesce_one_device(dev_info_t *dip, void *arg)
{
struct dev_ops *ops;
int should_quiesce = 0;
/*
* If the device is not attached it doesn't need to be quiesced.
*/
if (!i_ddi_devi_attached(dip))
return;
if ((ops = ddi_get_driver(dip)) == NULL)
return;
should_quiesce = should_implement_quiesce(dip);
/*
* If there's an implementation of quiesce(), always call it even if
* some of the drivers don't have quiesce() or quiesce() have failed
* so we can do force fast reboot. The implementation of quiesce()
* should not negatively affect a regular reboot.
*/
if (driver_has_quiesce(ops)) {
int rc = DDI_SUCCESS;
if (ops->devo_quiesce == ddi_quiesce_not_needed)
return;
rc = devi_quiesce(dip);
/* quiesce() should never fail */
ASSERT(rc == DDI_SUCCESS);
if (rc != DDI_SUCCESS && should_quiesce) {
if (arg != NULL)
*((int *)arg) = -1;
}
} else if (should_quiesce && arg != NULL) {
*((int *)arg) = -1;
}
}
/*
* Traverse the dev info tree in a breadth-first manner so that we quiesce
* children first. All subtrees under the parent of dip will be quiesced.
*/
void
quiesce_devices(dev_info_t *dip, void *arg)
{
/*
* if we're reached here, the device tree better not be changing.
* so either devinfo_freeze better be set or we better be panicing.
*/
ASSERT(devinfo_freeze || panicstr);
for (; dip != NULL; dip = ddi_get_next_sibling(dip)) {
quiesce_devices(ddi_get_child(dip), arg);
quiesce_one_device(dip, arg);
}
}
/*
* Reset all the pure leaf drivers on the system at halt time
*/
static int
reset_leaf_device(dev_info_t *dip, void *arg)
{
_NOTE(ARGUNUSED(arg))
struct dev_ops *ops;
/* if the device doesn't need to be reset then there's nothing to do */
if (!DEVI_NEED_RESET(dip))
return (DDI_WALK_CONTINUE);
/*
* if the device isn't a char/block device or doesn't have a
* reset entry point then there's nothing to do.
*/
ops = ddi_get_driver(dip);
if ((ops == NULL) || (ops->devo_cb_ops == NULL) ||
(ops->devo_reset == nodev) || (ops->devo_reset == nulldev) ||
(ops->devo_reset == NULL))
return (DDI_WALK_CONTINUE);
if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) {
static char path[MAXPATHLEN];
/*
* bad news, this device has blocked in it's attach or
* detach routine, which means it not safe to call it's
* devo_reset() entry point.
*/
cmn_err(CE_WARN, "unable to reset device: %s",
ddi_pathname(dip, path));
return (DDI_WALK_CONTINUE);
}
NDI_CONFIG_DEBUG((CE_NOTE, "resetting %s%d\n",
ddi_driver_name(dip), ddi_get_instance(dip)));
(void) devi_reset(dip, DDI_RESET_FORCE);
return (DDI_WALK_CONTINUE);
}
void
reset_leaves(void)
{
/*
* if we're reached here, the device tree better not be changing.
* so either devinfo_freeze better be set or we better be panicing.
*/
ASSERT(devinfo_freeze || panicstr);
(void) walk_devs(top_devinfo, reset_leaf_device, NULL, 0);
}
/*
* devtree_freeze() must be called before quiesce_devices() and reset_leaves()
* during a normal system shutdown. It attempts to ensure that there are no
* outstanding attach or detach operations in progress when quiesce_devices() or
* reset_leaves()is invoked. It must be called before the system becomes
* single-threaded because device attach and detach are multi-threaded
* operations. (note that during system shutdown the system doesn't actually
* become single-thread since other threads still exist, but the shutdown thread
* will disable preemption for itself, raise it's pil, and stop all the other
* cpus in the system there by effectively making the system single-threaded.)
*/
void
devtree_freeze(void)
{
int delayed = 0;
/* if we're panicing then the device tree isn't going to be changing */
if (panicstr)
return;
/* stop all dev_info state changes in the device tree */
devinfo_freeze = gethrtime();
/*
* if we're not panicing and there are on-going attach or detach
* operations, wait for up to 3 seconds for them to finish. This
* is a randomly chosen interval but this should be ok because:
* - 3 seconds is very small relative to the deadman timer.
* - normal attach and detach operations should be very quick.
* - attach and detach operations are fairly rare.
*/
while (!panicstr && atomic_add_long_nv(&devinfo_attach_detach, 0) &&
(delayed < 3)) {
delayed += 1;
/* do a sleeping wait for one second */
ASSERT(!servicing_interrupt());
delay(drv_usectohz(MICROSEC));
}
}
static int
bind_dip(dev_info_t *dip, void *arg)
{
_NOTE(ARGUNUSED(arg))
char *path;
major_t major, pmajor;
/*
* If the node is currently bound to the wrong driver, try to unbind
* so that we can rebind to the correct driver.
*/
if (i_ddi_node_state(dip) >= DS_BOUND) {
major = ddi_compatible_driver_major(dip, NULL);
if ((DEVI(dip)->devi_major == major) &&
(i_ddi_node_state(dip) >= DS_INITIALIZED)) {
/*
* Check for a path-oriented driver alias that
* takes precedence over current driver binding.
*/
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
pmajor = ddi_name_to_major(path);
if (driver_installed(pmajor))
major = pmajor;
kmem_free(path, MAXPATHLEN);
}
/* attempt unbind if current driver is incorrect */
if (driver_installed(major) &&
(major != DEVI(dip)->devi_major))
(void) ndi_devi_unbind_driver(dip);
}
/* If unbound, try to bind to a driver */
if (i_ddi_node_state(dip) < DS_BOUND)
(void) ndi_devi_bind_driver(dip, 0);
return (DDI_WALK_CONTINUE);
}
void
i_ddi_bind_devs(void)
{
/* flush devfs so that ndi_devi_unbind_driver will work when possible */
(void) devfs_clean(top_devinfo, NULL, 0);
ddi_walk_devs(top_devinfo, bind_dip, (void *)NULL);
}
/* callback data for unbind_children_by_alias() */
typedef struct unbind_data {
major_t drv_major;
char *drv_alias;
int ndevs_bound;
int unbind_errors;
} unbind_data_t;
/*
* A utility function provided for testing and support convenience
* Called for each device during an upgrade_drv -d bound to the alias
* that cannot be unbound due to device in use.
*/
static void
unbind_alias_dev_in_use(dev_info_t *dip, char *alias)
{
if (moddebug & MODDEBUG_BINDING) {
cmn_err(CE_CONT, "%s%d: state %d: bound to %s\n",
ddi_driver_name(dip), ddi_get_instance(dip),
i_ddi_node_state(dip), alias);
}
}
/*
* walkdevs callback for unbind devices bound to specific driver
* and alias. Invoked within the context of update_drv -d <alias>.
*/
static int
unbind_children_by_alias(dev_info_t *dip, void *arg)
{
int circ;
dev_info_t *cdip;
dev_info_t *next;
unbind_data_t *ub = (unbind_data_t *)(uintptr_t)arg;
int rv;
/*
* We are called from update_drv to try to unbind a specific
* set of aliases for a driver. Unbind what persistent nodes
* we can, and return the number of nodes which cannot be unbound.
* If not all nodes can be unbound, update_drv leaves the
* state of the driver binding files unchanged, except in
* the case of -f.
*/
ndi_devi_enter(dip, &circ);
for (cdip = ddi_get_child(dip); cdip; cdip = next) {
next = ddi_get_next_sibling(cdip);
if ((ddi_driver_major(cdip) != ub->drv_major) ||
(strcmp(DEVI(cdip)->devi_node_name, ub->drv_alias) != 0))
continue;
if (i_ddi_node_state(cdip) >= DS_BOUND) {
rv = ndi_devi_unbind_driver(cdip);
if (rv != DDI_SUCCESS ||
(i_ddi_node_state(cdip) >= DS_BOUND)) {
unbind_alias_dev_in_use(cdip, ub->drv_alias);
ub->ndevs_bound++;
continue;
}
if (ndi_dev_is_persistent_node(cdip) == 0)
(void) ddi_remove_child(cdip, 0);
}
}
ndi_devi_exit(dip, circ);
return (DDI_WALK_CONTINUE);
}
/*
* Unbind devices by driver & alias
* Context: update_drv [-f] -d -i <alias> <driver>
*/
int
i_ddi_unbind_devs_by_alias(major_t major, char *alias)
{
unbind_data_t *ub;
int rv;
ub = kmem_zalloc(sizeof (*ub), KM_SLEEP);
ub->drv_major = major;
ub->drv_alias = alias;
ub->ndevs_bound = 0;
ub->unbind_errors = 0;
/* flush devfs so that ndi_devi_unbind_driver will work when possible */
(void) devfs_clean(top_devinfo, NULL, 0);
ddi_walk_devs(top_devinfo, unbind_children_by_alias,
(void *)(uintptr_t)ub);
/* return the number of devices remaining bound to the alias */
rv = ub->ndevs_bound + ub->unbind_errors;
kmem_free(ub, sizeof (*ub));
return (rv);
}
/*
* walkdevs callback for unbind devices by driver
*/
static int
unbind_children_by_driver(dev_info_t *dip, void *arg)
{
int circ;
dev_info_t *cdip;
dev_info_t *next;
major_t major = (major_t)(uintptr_t)arg;
int rv;
/*
* We are called either from rem_drv or update_drv when reloading
* a driver.conf file. In either case, we unbind persistent nodes
* and destroy .conf nodes. In the case of rem_drv, this will be
* the final state. In the case of update_drv, i_ddi_bind_devs()
* may be invoked later to re-enumerate (new) driver.conf rebind
* persistent nodes.
*/
ndi_devi_enter(dip, &circ);
for (cdip = ddi_get_child(dip); cdip; cdip = next) {
next = ddi_get_next_sibling(cdip);
if (ddi_driver_major(cdip) != major)
continue;
if (i_ddi_node_state(cdip) >= DS_BOUND) {
rv = ndi_devi_unbind_driver(cdip);
if (rv == DDI_FAILURE ||
(i_ddi_node_state(cdip) >= DS_BOUND))
continue;
if (ndi_dev_is_persistent_node(cdip) == 0)
(void) ddi_remove_child(cdip, 0);
}
}
ndi_devi_exit(dip, circ);
return (DDI_WALK_CONTINUE);
}
/*
* Unbind devices by driver
* Context: rem_drv or unload driver.conf
*/
void
i_ddi_unbind_devs(major_t major)
{
/* flush devfs so that ndi_devi_unbind_driver will work when possible */
(void) devfs_clean(top_devinfo, NULL, 0);
ddi_walk_devs(top_devinfo, unbind_children_by_driver,
(void *)(uintptr_t)major);
}
/*
* I/O Hotplug control
*/
/*
* create and attach a dev_info node from a .conf file spec
*/
static void
init_spec_child(dev_info_t *pdip, struct hwc_spec *specp, uint_t flags)
{
_NOTE(ARGUNUSED(flags))
dev_info_t *dip;
char *node_name;
if (((node_name = specp->hwc_devi_name) == NULL) ||
(ddi_name_to_major(node_name) == DDI_MAJOR_T_NONE)) {
char *tmp = node_name;
if (tmp == NULL)
tmp = "<none>";
cmn_err(CE_CONT,
"init_spec_child: parent=%s, bad spec (%s)\n",
ddi_node_name(pdip), tmp);
return;
}
dip = i_ddi_alloc_node(pdip, node_name, (pnode_t)DEVI_PSEUDO_NODEID,
-1, specp->hwc_devi_sys_prop_ptr, KM_SLEEP);
if (dip == NULL)
return;
if (ddi_initchild(pdip, dip) != DDI_SUCCESS)
(void) ddi_remove_child(dip, 0);
}
/*
* Lookup hwc specs from hash tables and make children from the spec
* Because some .conf children are "merge" nodes, we also initialize
* .conf children to merge properties onto hardware nodes.
*
* The pdip must be held busy.
*/
int
i_ndi_make_spec_children(dev_info_t *pdip, uint_t flags)
{
extern struct hwc_spec *hwc_get_child_spec(dev_info_t *, major_t);
int circ;
struct hwc_spec *list, *spec;
ndi_devi_enter(pdip, &circ);
if (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN) {
ndi_devi_exit(pdip, circ);
return (DDI_SUCCESS);
}
list = hwc_get_child_spec(pdip, DDI_MAJOR_T_NONE);
for (spec = list; spec != NULL; spec = spec->hwc_next) {
init_spec_child(pdip, spec, flags);
}
hwc_free_spec_list(list);
mutex_enter(&DEVI(pdip)->devi_lock);
DEVI(pdip)->devi_flags |= DEVI_MADE_CHILDREN;
mutex_exit(&DEVI(pdip)->devi_lock);
ndi_devi_exit(pdip, circ);
return (DDI_SUCCESS);
}
/*
* Run initchild on all child nodes such that instance assignment
* for multiport network cards are contiguous.
*
* The pdip must be held busy.
*/
static void
i_ndi_init_hw_children(dev_info_t *pdip, uint_t flags)
{
dev_info_t *dip;
ASSERT(DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
/* contiguous instance assignment */
e_ddi_enter_instance();
dip = ddi_get_child(pdip);
while (dip) {
if (ndi_dev_is_persistent_node(dip))
(void) i_ndi_config_node(dip, DS_INITIALIZED, flags);
dip = ddi_get_next_sibling(dip);
}
e_ddi_exit_instance();
}
/*
* report device status
*/
static void
i_ndi_devi_report_status_change(dev_info_t *dip, char *path)
{
char *status;
if (!DEVI_NEED_REPORT(dip) ||
(i_ddi_node_state(dip) < DS_INITIALIZED) ||
ndi_dev_is_hidden_node(dip)) {
return;
}
/* Invalidate the devinfo snapshot cache */
i_ddi_di_cache_invalidate();
if (DEVI_IS_DEVICE_REMOVED(dip)) {
status = "removed";
} else if (DEVI_IS_DEVICE_OFFLINE(dip)) {
status = "offline";
} else if (DEVI_IS_DEVICE_DOWN(dip)) {
status = "down";
} else if (DEVI_IS_BUS_QUIESCED(dip)) {
status = "quiesced";
} else if (DEVI_IS_BUS_DOWN(dip)) {
status = "down";
} else if (i_ddi_devi_attached(dip)) {
status = "online";
} else {
status = "unknown";
}
if (path == NULL) {
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
cmn_err(CE_CONT, "?%s (%s%d) %s\n",
ddi_pathname(dip, path), ddi_driver_name(dip),
ddi_get_instance(dip), status);
kmem_free(path, MAXPATHLEN);
} else {
cmn_err(CE_CONT, "?%s (%s%d) %s\n",
path, ddi_driver_name(dip),
ddi_get_instance(dip), status);
}
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_REPORT_DONE(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
}
/*
* log a notification that a dev_info node has been configured.
*/
static int
i_log_devfs_add_devinfo(dev_info_t *dip, uint_t flags)
{
int se_err;
char *pathname;
sysevent_t *ev;
sysevent_id_t eid;
sysevent_value_t se_val;
sysevent_attr_list_t *ev_attr_list = NULL;
char *class_name;
int no_transport = 0;
ASSERT(dip && ddi_get_parent(dip) &&
DEVI_BUSY_OWNED(ddi_get_parent(dip)));
/* do not generate ESC_DEVFS_DEVI_ADD event during boot */
if (!i_ddi_io_initialized())
return (DDI_SUCCESS);
/* Invalidate the devinfo snapshot cache */
i_ddi_di_cache_invalidate();
ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_ADD, EP_DDI, SE_SLEEP);
pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, pathname);
ASSERT(strlen(pathname));
se_val.value_type = SE_DATA_TYPE_STRING;
se_val.value.sv_string = pathname;
if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
&se_val, SE_SLEEP) != 0) {
goto fail;
}
/* add the device class attribute */
if ((class_name = i_ddi_devi_class(dip)) != NULL) {
se_val.value_type = SE_DATA_TYPE_STRING;
se_val.value.sv_string = class_name;
if (sysevent_add_attr(&ev_attr_list,
DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
}
/*
* must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
* in which case the branch event will be logged by the caller
* after the entire branch has been configured.
*/
if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
/*
* Instead of logging a separate branch event just add
* DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
* generate a EC_DEV_BRANCH event.
*/
se_val.value_type = SE_DATA_TYPE_INT32;
se_val.value.sv_int32 = 1;
if (sysevent_add_attr(&ev_attr_list,
DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
}
if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
if (se_err == SE_NO_TRANSPORT)
no_transport = 1;
goto fail;
}
sysevent_free(ev);
kmem_free(pathname, MAXPATHLEN);
return (DDI_SUCCESS);
fail:
cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_ADD event for %s%s",
pathname, (no_transport) ? " (syseventd not responding)" : "");
cmn_err(CE_WARN, "/dev may not be current for driver %s. "
"Run devfsadm -i %s",
ddi_driver_name(dip), ddi_driver_name(dip));
sysevent_free(ev);
kmem_free(pathname, MAXPATHLEN);
return (DDI_SUCCESS);
}
/*
* log a notification that a dev_info node has been unconfigured.
*/
static int
i_log_devfs_remove_devinfo(char *pathname, char *class_name, char *driver_name,
int instance, uint_t flags)
{
sysevent_t *ev;
sysevent_id_t eid;
sysevent_value_t se_val;
sysevent_attr_list_t *ev_attr_list = NULL;
int se_err;
int no_transport = 0;
if (!i_ddi_io_initialized())
return (DDI_SUCCESS);
/* Invalidate the devinfo snapshot cache */
i_ddi_di_cache_invalidate();
ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_DEVI_REMOVE, EP_DDI, SE_SLEEP);
se_val.value_type = SE_DATA_TYPE_STRING;
se_val.value.sv_string = pathname;
if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
&se_val, SE_SLEEP) != 0) {
goto fail;
}
if (class_name) {
/* add the device class, driver name and instance attributes */
se_val.value_type = SE_DATA_TYPE_STRING;
se_val.value.sv_string = class_name;
if (sysevent_add_attr(&ev_attr_list,
DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
se_val.value_type = SE_DATA_TYPE_STRING;
se_val.value.sv_string = driver_name;
if (sysevent_add_attr(&ev_attr_list,
DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
se_val.value_type = SE_DATA_TYPE_INT32;
se_val.value.sv_int32 = instance;
if (sysevent_add_attr(&ev_attr_list,
DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
}
/*
* must log a branch event too unless NDI_BRANCH_EVENT_OP is set,
* in which case the branch event will be logged by the caller
* after the entire branch has been unconfigured.
*/
if ((flags & NDI_BRANCH_EVENT_OP) == 0) {
/*
* Instead of logging a separate branch event just add
* DEVFS_BRANCH_EVENT attribute. It indicates devfsadmd to
* generate a EC_DEV_BRANCH event.
*/
se_val.value_type = SE_DATA_TYPE_INT32;
se_val.value.sv_int32 = 1;
if (sysevent_add_attr(&ev_attr_list,
DEVFS_BRANCH_EVENT, &se_val, SE_SLEEP) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
}
if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
if (se_err == SE_NO_TRANSPORT)
no_transport = 1;
goto fail;
}
sysevent_free(ev);
return (DDI_SUCCESS);
fail:
sysevent_free(ev);
cmn_err(CE_WARN, "failed to log ESC_DEVFS_DEVI_REMOVE event for %s%s",
pathname, (no_transport) ? " (syseventd not responding)" : "");
return (DDI_SUCCESS);
}
static void
i_ddi_log_devfs_device_remove(dev_info_t *dip)
{
char *path;
ASSERT(dip && ddi_get_parent(dip) &&
DEVI_BUSY_OWNED(ddi_get_parent(dip)));
ASSERT(DEVI_IS_DEVICE_REMOVED(dip));
ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);
if (i_ddi_node_state(dip) < DS_INITIALIZED)
return;
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) i_log_devfs_remove_devinfo(ddi_pathname(dip, path),
i_ddi_devi_class(dip), (char *)ddi_driver_name(dip),
ddi_get_instance(dip), 0);
kmem_free(path, MAXPATHLEN);
}
static void
i_ddi_log_devfs_device_insert(dev_info_t *dip)
{
ASSERT(dip && ddi_get_parent(dip) &&
DEVI_BUSY_OWNED(ddi_get_parent(dip)));
ASSERT(!DEVI_IS_DEVICE_REMOVED(dip));
(void) i_log_devfs_add_devinfo(dip, 0);
}
/*
* log an event that a dev_info branch has been configured or unconfigured.
*/
static int
i_log_devfs_branch(char *node_path, char *subclass)
{
int se_err;
sysevent_t *ev;
sysevent_id_t eid;
sysevent_value_t se_val;
sysevent_attr_list_t *ev_attr_list = NULL;
int no_transport = 0;
/* do not generate the event during boot */
if (!i_ddi_io_initialized())
return (DDI_SUCCESS);
/* Invalidate the devinfo snapshot cache */
i_ddi_di_cache_invalidate();
ev = sysevent_alloc(EC_DEVFS, subclass, EP_DDI, SE_SLEEP);
se_val.value_type = SE_DATA_TYPE_STRING;
se_val.value.sv_string = node_path;
if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
&se_val, SE_SLEEP) != 0) {
goto fail;
}
if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
sysevent_free_attr(ev_attr_list);
goto fail;
}
if ((se_err = log_sysevent(ev, SE_SLEEP, &eid)) != 0) {
if (se_err == SE_NO_TRANSPORT)
no_transport = 1;
goto fail;
}
sysevent_free(ev);
return (DDI_SUCCESS);
fail:
cmn_err(CE_WARN, "failed to log %s branch event for %s%s",
subclass, node_path,
(no_transport) ? " (syseventd not responding)" : "");
sysevent_free(ev);
return (DDI_FAILURE);
}
/*
* log an event that a dev_info tree branch has been configured.
*/
static int
i_log_devfs_branch_add(dev_info_t *dip)
{
char *node_path;
int rv;
node_path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, node_path);
rv = i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_ADD);
kmem_free(node_path, MAXPATHLEN);
return (rv);
}
/*
* log an event that a dev_info tree branch has been unconfigured.
*/
static int
i_log_devfs_branch_remove(char *node_path)
{
return (i_log_devfs_branch(node_path, ESC_DEVFS_BRANCH_REMOVE));
}
/*
* enqueue the dip's deviname on the branch event queue.
*/
static struct brevq_node *
brevq_enqueue(struct brevq_node **brevqp, dev_info_t *dip,
struct brevq_node *child)
{
struct brevq_node *brn;
char *deviname;
deviname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
(void) ddi_deviname(dip, deviname);
brn = kmem_zalloc(sizeof (*brn), KM_SLEEP);
brn->brn_deviname = i_ddi_strdup(deviname, KM_SLEEP);
kmem_free(deviname, MAXNAMELEN);
brn->brn_child = child;
brn->brn_sibling = *brevqp;
*brevqp = brn;
return (brn);
}
/*
* free the memory allocated for the elements on the branch event queue.
*/
static void
free_brevq(struct brevq_node *brevq)
{
struct brevq_node *brn, *next_brn;
for (brn = brevq; brn != NULL; brn = next_brn) {
next_brn = brn->brn_sibling;
ASSERT(brn->brn_child == NULL);
kmem_free(brn->brn_deviname, strlen(brn->brn_deviname) + 1);
kmem_free(brn, sizeof (*brn));
}
}
/*
* log the events queued up on the branch event queue and free the
* associated memory.
*
* node_path must have been allocated with at least MAXPATHLEN bytes.
*/
static void
log_and_free_brevq(char *node_path, struct brevq_node *brevq)
{
struct brevq_node *brn;
char *p;
p = node_path + strlen(node_path);
for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
(void) strcpy(p, brn->brn_deviname);
(void) i_log_devfs_branch_remove(node_path);
}
*p = '\0';
free_brevq(brevq);
}
/*
* log the events queued up on the branch event queue and free the
* associated memory. Same as the previous function but operates on dip.
*/
static void
log_and_free_brevq_dip(dev_info_t *dip, struct brevq_node *brevq)
{
char *path;
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
log_and_free_brevq(path, brevq);
kmem_free(path, MAXPATHLEN);
}
/*
* log the outstanding branch remove events for the grand children of the dip
* and free the associated memory.
*/
static void
log_and_free_br_events_on_grand_children(dev_info_t *dip,
struct brevq_node *brevq)
{
struct brevq_node *brn;
char *path;
char *p;
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
p = path + strlen(path);
for (brn = brevq; brn != NULL; brn = brn->brn_sibling) {
if (brn->brn_child) {
(void) strcpy(p, brn->brn_deviname);
/* now path contains the node path to the dip's child */
log_and_free_brevq(path, brn->brn_child);
brn->brn_child = NULL;
}
}
kmem_free(path, MAXPATHLEN);
}
/*
* log and cleanup branch remove events for the grand children of the dip.
*/
static void
cleanup_br_events_on_grand_children(dev_info_t *dip, struct brevq_node **brevqp)
{
dev_info_t *child;
struct brevq_node *brevq, *brn, *prev_brn, *next_brn;
char *path;
int circ;
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
prev_brn = NULL;
brevq = *brevqp;
ndi_devi_enter(dip, &circ);
for (brn = brevq; brn != NULL; brn = next_brn) {
next_brn = brn->brn_sibling;
for (child = ddi_get_child(dip); child != NULL;
child = ddi_get_next_sibling(child)) {
if (i_ddi_node_state(child) >= DS_INITIALIZED) {
(void) ddi_deviname(child, path);
if (strcmp(path, brn->brn_deviname) == 0)
break;
}
}
if (child != NULL && !(DEVI_EVREMOVE(child))) {
/*
* Event state is not REMOVE. So branch remove event
* is not going be generated on brn->brn_child.
* If any branch remove events were queued up on
* brn->brn_child log them and remove the brn
* from the queue.
*/
if (brn->brn_child) {
(void) ddi_pathname(dip, path);
(void) strcat(path, brn->brn_deviname);
log_and_free_brevq(path, brn->brn_child);
}
if (prev_brn)
prev_brn->brn_sibling = next_brn;
else
*brevqp = next_brn;
kmem_free(brn->brn_deviname,
strlen(brn->brn_deviname) + 1);
kmem_free(brn, sizeof (*brn));
} else {
/*
* Free up the outstanding branch remove events
* queued on brn->brn_child since brn->brn_child
* itself is eligible for branch remove event.
*/
if (brn->brn_child) {
free_brevq(brn->brn_child);
brn->brn_child = NULL;
}
prev_brn = brn;
}
}
ndi_devi_exit(dip, circ);
kmem_free(path, MAXPATHLEN);
}
static int
need_remove_event(dev_info_t *dip, int flags)
{
if ((flags & (NDI_NO_EVENT | NDI_AUTODETACH)) == 0 &&
(flags & (NDI_DEVI_OFFLINE | NDI_UNCONFIG | NDI_DEVI_REMOVE)) &&
!(DEVI_EVREMOVE(dip)))
return (1);
else
return (0);
}
/*
* Unconfigure children/descendants of the dip.
*
* If the operation involves a branch event NDI_BRANCH_EVENT_OP is set
* through out the unconfiguration. On successful return *brevqp is set to
* a queue of dip's child devinames for which branch remove events need
* to be generated.
*/
static int
devi_unconfig_branch(dev_info_t *dip, dev_info_t **dipp, int flags,
struct brevq_node **brevqp)
{
int rval;
*brevqp = NULL;
if ((!(flags & NDI_BRANCH_EVENT_OP)) && need_remove_event(dip, flags))
flags |= NDI_BRANCH_EVENT_OP;
if (flags & NDI_BRANCH_EVENT_OP) {
rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
brevqp);
if (rval != NDI_SUCCESS && (*brevqp)) {
log_and_free_brevq_dip(dip, *brevqp);
*brevqp = NULL;
}
} else
rval = devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE,
NULL);
return (rval);
}
/*
* If the dip is already bound to a driver transition to DS_INITIALIZED
* in order to generate an event in the case where the node was left in
* DS_BOUND state since boot (never got attached) and the node is now
* being offlined.
*/
static void
init_bound_node_ev(dev_info_t *pdip, dev_info_t *dip, int flags)
{
if (need_remove_event(dip, flags) &&
i_ddi_node_state(dip) == DS_BOUND &&
i_ddi_devi_attached(pdip) && !DEVI_IS_DEVICE_OFFLINE(dip))
(void) ddi_initchild(pdip, dip);
}
/*
* attach a node/branch with parent already held busy
*/
static int
devi_attach_node(dev_info_t *dip, uint_t flags)
{
dev_info_t *pdip = ddi_get_parent(dip);
ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
mutex_enter(&(DEVI(dip)->devi_lock));
if (flags & NDI_DEVI_ONLINE) {
if (!i_ddi_devi_attached(dip))
DEVI_SET_REPORT(dip);
DEVI_SET_DEVICE_ONLINE(dip);
}
if (DEVI_IS_DEVICE_OFFLINE(dip)) {
mutex_exit(&(DEVI(dip)->devi_lock));
return (NDI_FAILURE);
}
mutex_exit(&(DEVI(dip)->devi_lock));
if (i_ddi_attachchild(dip) != DDI_SUCCESS) {
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_EVUNINIT(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
if (ndi_dev_is_persistent_node(dip))
(void) ddi_uninitchild(dip);
else {
/*
* Delete .conf nodes and nodes that are not
* well formed.
*/
(void) ddi_remove_child(dip, 0);
}
return (NDI_FAILURE);
}
i_ndi_devi_report_status_change(dip, NULL);
/*
* log an event, but not during devfs lookups in which case
* NDI_NO_EVENT is set.
*/
if ((flags & NDI_NO_EVENT) == 0 && !(DEVI_EVADD(dip))) {
(void) i_log_devfs_add_devinfo(dip, flags);
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_EVADD(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
} else if (!(flags & NDI_NO_EVENT_STATE_CHNG)) {
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_EVADD(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
}
return (NDI_SUCCESS);
}
/* internal function to config immediate children */
static int
config_immediate_children(dev_info_t *pdip, uint_t flags, major_t major)
{
dev_info_t *child, *next;
int circ;
ASSERT(i_ddi_devi_attached(pdip));
if (!NEXUS_DRV(ddi_get_driver(pdip)))
return (NDI_SUCCESS);
NDI_CONFIG_DEBUG((CE_CONT,
"config_immediate_children: %s%d (%p), flags=%x\n",
ddi_driver_name(pdip), ddi_get_instance(pdip),
(void *)pdip, flags));
ndi_devi_enter(pdip, &circ);
if (flags & NDI_CONFIG_REPROBE) {
mutex_enter(&DEVI(pdip)->devi_lock);
DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
mutex_exit(&DEVI(pdip)->devi_lock);
}
(void) i_ndi_make_spec_children(pdip, flags);
i_ndi_init_hw_children(pdip, flags);
child = ddi_get_child(pdip);
while (child) {
/* NOTE: devi_attach_node() may remove the dip */
next = ddi_get_next_sibling(child);
/*
* Configure all nexus nodes or leaf nodes with
* matching driver major
*/
if ((major == DDI_MAJOR_T_NONE) ||
(major == ddi_driver_major(child)) ||
((flags & NDI_CONFIG) && (is_leaf_node(child) == 0)))
(void) devi_attach_node(child, flags);
child = next;
}
ndi_devi_exit(pdip, circ);
return (NDI_SUCCESS);
}
/* internal function to config grand children */
static int
config_grand_children(dev_info_t *pdip, uint_t flags, major_t major)
{
struct mt_config_handle *hdl;
/* multi-threaded configuration of child nexus */
hdl = mt_config_init(pdip, NULL, flags, major, MT_CONFIG_OP, NULL);
mt_config_children(hdl);
return (mt_config_fini(hdl)); /* wait for threads to exit */
}
/*
* Common function for device tree configuration,
* either BUS_CONFIG_ALL or BUS_CONFIG_DRIVER.
* The NDI_CONFIG flag causes recursive configuration of
* grandchildren, devfs usage should not recurse.
*/
static int
devi_config_common(dev_info_t *dip, int flags, major_t major)
{
int error;
int (*f)();
if (!i_ddi_devi_attached(dip))
return (NDI_FAILURE);
if (pm_pre_config(dip, NULL) != DDI_SUCCESS)
return (NDI_FAILURE);
if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
(DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
(f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
error = config_immediate_children(dip, flags, major);
} else {
/* call bus_config entry point */
ddi_bus_config_op_t bus_op = (major == DDI_MAJOR_T_NONE) ?
BUS_CONFIG_ALL : BUS_CONFIG_DRIVER;
error = (*f)(dip,
flags, bus_op, (void *)(uintptr_t)major, NULL, 0);
}
if (error) {
pm_post_config(dip, NULL);
return (error);
}
/*
* Some callers, notably SCSI, need to mark the devfs cache
* to be rebuilt together with the config operation.
*/
if (flags & NDI_DEVFS_CLEAN)
(void) devfs_clean(dip, NULL, 0);
if (flags & NDI_CONFIG)
(void) config_grand_children(dip, flags, major);
pm_post_config(dip, NULL);
return (NDI_SUCCESS);
}
/*
* Framework entry point for BUS_CONFIG_ALL
*/
int
ndi_devi_config(dev_info_t *dip, int flags)
{
NDI_CONFIG_DEBUG((CE_CONT,
"ndi_devi_config: par = %s%d (%p), flags = 0x%x\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
return (devi_config_common(dip, flags, DDI_MAJOR_T_NONE));
}
/*
* Framework entry point for BUS_CONFIG_DRIVER, bound to major
*/
int
ndi_devi_config_driver(dev_info_t *dip, int flags, major_t major)
{
/* don't abuse this function */
ASSERT(major != DDI_MAJOR_T_NONE);
NDI_CONFIG_DEBUG((CE_CONT,
"ndi_devi_config_driver: par = %s%d (%p), flags = 0x%x\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
return (devi_config_common(dip, flags, major));
}
/*
* Called by nexus drivers to configure its children.
*/
static int
devi_config_one(dev_info_t *pdip, char *devnm, dev_info_t **cdipp,
uint_t flags, clock_t timeout)
{
dev_info_t *vdip = NULL;
char *drivername = NULL;
int find_by_addr = 0;
char *name, *addr;
int v_circ, p_circ;
clock_t end_time; /* 60 sec */
int probed;
dev_info_t *cdip;
mdi_pathinfo_t *cpip;
*cdipp = NULL;
if (!NEXUS_DRV(ddi_get_driver(pdip)))
return (NDI_FAILURE);
/* split name into "name@addr" parts */
i_ddi_parse_name(devnm, &name, &addr, NULL);
/*
* If the nexus is a pHCI and we are not processing a pHCI from
* mdi bus_config code then we need to know the vHCI.
*/
if (MDI_PHCI(pdip))
vdip = mdi_devi_get_vdip(pdip);
/*
* We may have a genericname on a system that creates drivername
* nodes (from .conf files). Find the drivername by nodeid. If we
* can't find a node with devnm as the node name then we search by
* drivername. This allows an implementation to supply a genericly
* named boot path (disk) and locate drivename nodes (sd). The
* NDI_PROMNAME flag does not apply to /devices/pseudo paths.
*/
if ((flags & NDI_PROMNAME) && (pdip != pseudo_dip)) {
drivername = child_path_to_driver(pdip, name, addr);
find_by_addr = 1;
}
/*
* Determine end_time: This routine should *not* be called with a
* constant non-zero timeout argument, the caller should be adjusting
* the timeout argument relative to when it *started* its asynchronous
* enumeration.
*/
if (timeout > 0)
end_time = ddi_get_lbolt() + timeout;
for (;;) {
/*
* For pHCI, enter (vHCI, pHCI) and search for pathinfo/client
* child - break out of for(;;) loop if child found.
* NOTE: Lock order for ndi_devi_enter is (vHCI, pHCI).
*/
if (vdip) {
/* use mdi_devi_enter ordering */
ndi_devi_enter(vdip, &v_circ);
ndi_devi_enter(pdip, &p_circ);
cpip = mdi_pi_find(pdip, NULL, addr);
cdip = mdi_pi_get_client(cpip);
if (cdip)
break;
} else
ndi_devi_enter(pdip, &p_circ);
/*
* When not a vHCI or not all pHCI devices are required to
* enumerated under the vHCI (NDI_MDI_FALLBACK) search for
* devinfo child.
*/
if ((vdip == NULL) || (flags & NDI_MDI_FALLBACK)) {
/* determine if .conf nodes already built */
probed = (DEVI(pdip)->devi_flags & DEVI_MADE_CHILDREN);
/*
* Search for child by name, if not found then search
* for a node bound to the drivername driver with the
* specified "@addr". Break out of for(;;) loop if
* child found. To support path-oriented aliases
* binding on boot-device, we do a search_by_addr too.
*/
again: (void) i_ndi_make_spec_children(pdip, flags);
cdip = find_child_by_name(pdip, name, addr);
if ((cdip == NULL) && drivername)
cdip = find_child_by_driver(pdip,
drivername, addr);
if ((cdip == NULL) && find_by_addr)
cdip = find_child_by_addr(pdip, addr);
if (cdip)
break;
/*
* determine if we should reenumerate .conf nodes
* and look for child again.
*/
if (probed &&
i_ddi_io_initialized() &&
(flags & NDI_CONFIG_REPROBE) &&
((timeout <= 0) || (ddi_get_lbolt() >= end_time))) {
probed = 0;
mutex_enter(&DEVI(pdip)->devi_lock);
DEVI(pdip)->devi_flags &= ~DEVI_MADE_CHILDREN;
mutex_exit(&DEVI(pdip)->devi_lock);
goto again;
}
}
/* break out of for(;;) if time expired */
if ((timeout <= 0) || (ddi_get_lbolt() >= end_time))
break;
/*
* Child not found, exit and wait for asynchronous enumeration
* to add child (or timeout). The addition of a new child (vhci
* or phci) requires the asynchronous enumeration thread to
* ndi_devi_enter/ndi_devi_exit. This exit will signal devi_cv
* and cause us to return from ndi_devi_exit_and_wait, after
* which we loop and search for the requested child again.
*/
NDI_DEBUG(flags, (CE_CONT,
"%s%d: waiting for child %s@%s, timeout %ld",
ddi_driver_name(pdip), ddi_get_instance(pdip),
name, addr, timeout));
if (vdip) {
/*
* Mark vHCI for pHCI ndi_devi_exit broadcast.
*/
mutex_enter(&DEVI(vdip)->devi_lock);
DEVI(vdip)->devi_flags |=
DEVI_PHCI_SIGNALS_VHCI;
mutex_exit(&DEVI(vdip)->devi_lock);
ndi_devi_exit(pdip, p_circ);
/*
* NB: There is a small race window from above
* ndi_devi_exit() of pdip to cv_wait() in
* ndi_devi_exit_and_wait() which can result in
* not immediately finding a new pHCI child
* of a pHCI that uses NDI_MDI_FAILBACK.
*/
ndi_devi_exit_and_wait(vdip, v_circ, end_time);
} else {
ndi_devi_exit_and_wait(pdip, p_circ, end_time);
}
}
/* done with paddr, fixup i_ddi_parse_name '@'->'\0' change */
if (addr && *addr != '\0')
*(addr - 1) = '@';
/* attach and hold the child, returning pointer to child */
if (cdip && (devi_attach_node(cdip, flags) == NDI_SUCCESS)) {
ndi_hold_devi(cdip);
*cdipp = cdip;
}
ndi_devi_exit(pdip, p_circ);
if (vdip)
ndi_devi_exit(vdip, v_circ);
return (*cdipp ? NDI_SUCCESS : NDI_FAILURE);
}
/*
* Enumerate and attach a child specified by name 'devnm'.
* Called by devfs lookup and DR to perform a BUS_CONFIG_ONE.
* Note: devfs does not make use of NDI_CONFIG to configure
* an entire branch.
*/
int
ndi_devi_config_one(dev_info_t *dip, char *devnm, dev_info_t **dipp, int flags)
{
int error;
int (*f)();
int branch_event = 0;
ASSERT(dipp);
ASSERT(i_ddi_devi_attached(dip));
NDI_CONFIG_DEBUG((CE_CONT,
"ndi_devi_config_one: par = %s%d (%p), child = %s\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, devnm));
if (pm_pre_config(dip, devnm) != DDI_SUCCESS)
return (NDI_FAILURE);
if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
(flags & NDI_CONFIG)) {
flags |= NDI_BRANCH_EVENT_OP;
branch_event = 1;
}
if ((DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
(DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
(f = DEVI(dip)->devi_ops->devo_bus_ops->bus_config) == NULL) {
error = devi_config_one(dip, devnm, dipp, flags, 0);
} else {
/* call bus_config entry point */
error = (*f)(dip, flags, BUS_CONFIG_ONE, (void *)devnm, dipp);
}
if (error || (flags & NDI_CONFIG) == 0) {
pm_post_config(dip, devnm);
return (error);
}
/*
* DR usage (i.e. call with NDI_CONFIG) recursively configures
* grandchildren, performing a BUS_CONFIG_ALL from the node attached
* by the BUS_CONFIG_ONE.
*/
ASSERT(*dipp);
error = devi_config_common(*dipp, flags, DDI_MAJOR_T_NONE);
pm_post_config(dip, devnm);
if (branch_event)
(void) i_log_devfs_branch_add(*dipp);
return (error);
}
/*
* Enumerate and attach a child specified by name 'devnm'.
* Called during configure the OBP options. This configures
* only one node.
*/
static int
ndi_devi_config_obp_args(dev_info_t *parent, char *devnm,
dev_info_t **childp, int flags)
{
int error;
int (*f)();
ASSERT(childp);
ASSERT(i_ddi_devi_attached(parent));
NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_config_obp_args: "
"par = %s%d (%p), child = %s\n", ddi_driver_name(parent),
ddi_get_instance(parent), (void *)parent, devnm));
if ((DEVI(parent)->devi_ops->devo_bus_ops == NULL) ||
(DEVI(parent)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
(f = DEVI(parent)->devi_ops->devo_bus_ops->bus_config) == NULL) {
error = NDI_FAILURE;
} else {
/* call bus_config entry point */
error = (*f)(parent, flags,
BUS_CONFIG_OBP_ARGS, (void *)devnm, childp);
}
return (error);
}
/*
* Pay attention, the following is a bit tricky:
* There are three possible cases when constraints are applied
*
* - A constraint is applied and the offline is disallowed.
* Simply return failure and block the offline
*
* - A constraint is applied and the offline is allowed.
* Mark the dip as having passed the constraint and allow
* offline to proceed.
*
* - A constraint is not applied. Allow the offline to proceed for now.
*
* In the latter two cases we allow the offline to proceed. If the
* offline succeeds (no users) everything is fine. It is ok for an unused
* device to be offlined even if no constraints were imposed on the offline.
* If the offline fails because there are users, we look at the constraint
* flag on the dip. If the constraint flag is set (implying that it passed
* a constraint) we allow the dip to be retired. If not, we don't allow
* the retire. This ensures that we don't allow unconstrained retire.
*/
int
e_ddi_offline_notify(dev_info_t *dip)
{
int retval;
int constraint;
int failure;
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): entered: dip=%p",
(void *) dip));
constraint = 0;
failure = 0;
/*
* Start with userland constraints first - applied via device contracts
*/
retval = contract_device_offline(dip, DDI_DEV_T_ANY, 0);
switch (retval) {
case CT_NACK:
RIO_DEBUG((CE_NOTE, "Received NACK for dip=%p", (void *)dip));
failure = 1;
goto out;
case CT_ACK:
constraint = 1;
RIO_DEBUG((CE_NOTE, "Received ACK for dip=%p", (void *)dip));
break;
case CT_NONE:
/* no contracts */
RIO_DEBUG((CE_NOTE, "No contracts on dip=%p", (void *)dip));
break;
default:
ASSERT(retval == CT_NONE);
}
/*
* Next, use LDI to impose kernel constraints
*/
retval = ldi_invoke_notify(dip, DDI_DEV_T_ANY, 0, LDI_EV_OFFLINE, NULL);
switch (retval) {
case LDI_EV_FAILURE:
contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_FAILURE);
RIO_DEBUG((CE_NOTE, "LDI callback failed on dip=%p",
(void *)dip));
failure = 1;
goto out;
case LDI_EV_SUCCESS:
constraint = 1;
RIO_DEBUG((CE_NOTE, "LDI callback success on dip=%p",
(void *)dip));
break;
case LDI_EV_NONE:
/* no matching LDI callbacks */
RIO_DEBUG((CE_NOTE, "No LDI callbacks for dip=%p",
(void *)dip));
break;
default:
ASSERT(retval == LDI_EV_NONE);
}
out:
mutex_enter(&(DEVI(dip)->devi_lock));
if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && failure) {
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
"BLOCKED flag. dip=%p", (void *)dip));
DEVI(dip)->devi_flags |= DEVI_R_BLOCKED;
if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): "
"blocked. clearing RCM CONSTRAINT flag. dip=%p",
(void *)dip));
DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
}
} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) && constraint) {
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): setting "
"CONSTRAINT flag. dip=%p", (void *)dip));
DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
} else if ((DEVI(dip)->devi_flags & DEVI_RETIRING) &&
DEVI(dip)->devi_ref == 0) {
/* also allow retire if device is not in use */
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): device not in "
"use. Setting CONSTRAINT flag. dip=%p", (void *)dip));
DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
} else {
/*
* Note: We cannot ASSERT here that DEVI_R_CONSTRAINT is
* not set, since other sources (such as RCM) may have
* set the flag.
*/
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): not setting "
"constraint flag. dip=%p", (void *)dip));
}
mutex_exit(&(DEVI(dip)->devi_lock));
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_notify(): exit: dip=%p",
(void *) dip));
return (failure ? DDI_FAILURE : DDI_SUCCESS);
}
void
e_ddi_offline_finalize(dev_info_t *dip, int result)
{
RIO_DEBUG((CE_NOTE, "e_ddi_offline_finalize(): entry: result=%s, "
"dip=%p", result == DDI_SUCCESS ? "SUCCESS" : "FAILURE",
(void *)dip));
contract_device_negend(dip, DDI_DEV_T_ANY, 0, result == DDI_SUCCESS ?
CT_EV_SUCCESS : CT_EV_FAILURE);
ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0,
LDI_EV_OFFLINE, result == DDI_SUCCESS ?
LDI_EV_SUCCESS : LDI_EV_FAILURE, NULL);
RIO_VERBOSE((CE_NOTE, "e_ddi_offline_finalize(): exit: dip=%p",
(void *)dip));
}
void
e_ddi_degrade_finalize(dev_info_t *dip)
{
RIO_DEBUG((CE_NOTE, "e_ddi_degrade_finalize(): entry: "
"result always = DDI_SUCCESS, dip=%p", (void *)dip));
contract_device_degrade(dip, DDI_DEV_T_ANY, 0);
contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
ldi_invoke_finalize(dip, DDI_DEV_T_ANY, 0, LDI_EV_DEGRADE,
LDI_EV_SUCCESS, NULL);
RIO_VERBOSE((CE_NOTE, "e_ddi_degrade_finalize(): exit: dip=%p",
(void *)dip));
}
void
e_ddi_undegrade_finalize(dev_info_t *dip)
{
RIO_DEBUG((CE_NOTE, "e_ddi_undegrade_finalize(): entry: "
"result always = DDI_SUCCESS, dip=%p", (void *)dip));
contract_device_undegrade(dip, DDI_DEV_T_ANY, 0);
contract_device_negend(dip, DDI_DEV_T_ANY, 0, CT_EV_SUCCESS);
RIO_VERBOSE((CE_NOTE, "e_ddi_undegrade_finalize(): exit: dip=%p",
(void *)dip));
}
/*
* detach a node with parent already held busy
*/
static int
devi_detach_node(dev_info_t *dip, uint_t flags)
{
dev_info_t *pdip = ddi_get_parent(dip);
int ret = NDI_SUCCESS;
ddi_eventcookie_t cookie;
char *path = NULL;
char *class = NULL;
char *driver = NULL;
int instance = -1;
int post_event = 0;
ASSERT(pdip && DEVI_BUSY_OWNED(pdip));
/*
* Invoke notify if offlining
*/
if (flags & NDI_DEVI_OFFLINE) {
RIO_DEBUG((CE_NOTE, "devi_detach_node: offlining dip=%p",
(void *)dip));
if (e_ddi_offline_notify(dip) != DDI_SUCCESS) {
RIO_DEBUG((CE_NOTE, "devi_detach_node: offline NACKed"
"dip=%p", (void *)dip));
return (NDI_FAILURE);
}
}
if (flags & NDI_POST_EVENT) {
if (i_ddi_devi_attached(pdip)) {
if (ddi_get_eventcookie(dip, DDI_DEVI_REMOVE_EVENT,
&cookie) == NDI_SUCCESS)
(void) ndi_post_event(dip, dip, cookie, NULL);
}
}
if (i_ddi_detachchild(dip, flags) != DDI_SUCCESS) {
if (flags & NDI_DEVI_OFFLINE) {
RIO_DEBUG((CE_NOTE, "devi_detach_node: offline failed."
" Calling e_ddi_offline_finalize with result=%d. "
"dip=%p", DDI_FAILURE, (void *)dip));
e_ddi_offline_finalize(dip, DDI_FAILURE);
}
return (NDI_FAILURE);
}
if (flags & NDI_DEVI_OFFLINE) {
RIO_DEBUG((CE_NOTE, "devi_detach_node: offline succeeded."
" Calling e_ddi_offline_finalize with result=%d, "
"dip=%p", DDI_SUCCESS, (void *)dip));
e_ddi_offline_finalize(dip, DDI_SUCCESS);
}
if (flags & NDI_AUTODETACH)
return (NDI_SUCCESS);
/*
* For DR, even bound nodes may need to have offline
* flag set.
*/
if (flags & NDI_DEVI_OFFLINE) {
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_DEVICE_OFFLINE(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
}
if (i_ddi_node_state(dip) == DS_INITIALIZED) {
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
if (flags & NDI_DEVI_OFFLINE)
i_ndi_devi_report_status_change(dip, path);
if (need_remove_event(dip, flags)) {
post_event = 1;
class = i_ddi_strdup(i_ddi_devi_class(dip), KM_SLEEP);
driver = i_ddi_strdup((char *)ddi_driver_name(dip),
KM_SLEEP);
instance = ddi_get_instance(dip);
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_EVREMOVE(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
}
}
if (flags & (NDI_UNCONFIG | NDI_DEVI_REMOVE)) {
ret = ddi_uninitchild(dip);
if (ret == NDI_SUCCESS) {
/*
* Remove uninitialized pseudo nodes because
* system props are lost and the node cannot be
* reattached.
*/
if (!ndi_dev_is_persistent_node(dip))
flags |= NDI_DEVI_REMOVE;
if (flags & NDI_DEVI_REMOVE) {
ret = ddi_remove_child(dip, 0);
if (post_event && ret == NDI_SUCCESS) {
(void) i_log_devfs_remove_devinfo(path,
class, driver, instance, flags);
}
}
}
}
if (path)
kmem_free(path, MAXPATHLEN);
if (class)
kmem_free(class, strlen(class) + 1);
if (driver)
kmem_free(driver, strlen(driver) + 1);
return (ret);
}
/*
* unconfigure immediate children of bus nexus device
*/
static int
unconfig_immediate_children(
dev_info_t *dip,
dev_info_t **dipp,
int flags,
major_t major)
{
int rv = NDI_SUCCESS;
int circ, vcirc;
dev_info_t *child;
dev_info_t *vdip = NULL;
dev_info_t *next;
ASSERT(dipp == NULL || *dipp == NULL);
/*
* Scan forward to see if we will be processing a pHCI child. If we
* have a child that is a pHCI and vHCI and pHCI are not siblings then
* enter vHCI before parent(pHCI) to prevent deadlock with mpxio
* Client power management operations.
*/
ndi_devi_enter(dip, &circ);
for (child = ddi_get_child(dip); child;
child = ddi_get_next_sibling(child)) {
/* skip same nodes we skip below */
if (((major != DDI_MAJOR_T_NONE) &&
(major != ddi_driver_major(child))) ||
((flags & NDI_AUTODETACH) && !is_leaf_node(child)))
continue;
if (MDI_PHCI(child)) {
vdip = mdi_devi_get_vdip(child);
/*
* If vHCI and vHCI is not a sibling of pHCI
* then enter in (vHCI, parent(pHCI)) order.
*/
if (vdip && (ddi_get_parent(vdip) != dip)) {
ndi_devi_exit(dip, circ);
/* use mdi_devi_enter ordering */
ndi_devi_enter(vdip, &vcirc);
ndi_devi_enter(dip, &circ);
break;
} else
vdip = NULL;
}
}
child = ddi_get_child(dip);
while (child) {
next = ddi_get_next_sibling(child);
if ((major != DDI_MAJOR_T_NONE) &&
(major != ddi_driver_major(child))) {
child = next;
continue;
}
/* skip nexus nodes during autodetach */
if ((flags & NDI_AUTODETACH) && !is_leaf_node(child)) {
child = next;
continue;
}
if (devi_detach_node(child, flags) != NDI_SUCCESS) {
if (dipp && *dipp == NULL) {
ndi_hold_devi(child);
*dipp = child;
}
rv = NDI_FAILURE;
}
/*
* Continue upon failure--best effort algorithm
*/
child = next;
}
ndi_devi_exit(dip, circ);
if (vdip)
ndi_devi_exit(vdip, vcirc);
return (rv);
}
/*
* unconfigure grand children of bus nexus device
*/
static int
unconfig_grand_children(
dev_info_t *dip,
dev_info_t **dipp,
int flags,
major_t major,
struct brevq_node **brevqp)
{
struct mt_config_handle *hdl;
if (brevqp)
*brevqp = NULL;
/* multi-threaded configuration of child nexus */
hdl = mt_config_init(dip, dipp, flags, major, MT_UNCONFIG_OP, brevqp);
mt_config_children(hdl);
return (mt_config_fini(hdl)); /* wait for threads to exit */
}
/*
* Unconfigure children/descendants of the dip.
*
* If brevqp is not NULL, on return *brevqp is set to a queue of dip's
* child devinames for which branch remove events need to be generated.
*/
static int
devi_unconfig_common(
dev_info_t *dip,
dev_info_t **dipp,
int flags,
major_t major,
struct brevq_node **brevqp)
{
int rv;
int pm_cookie;
int (*f)();
ddi_bus_config_op_t bus_op;
if (dipp)
*dipp = NULL;
if (brevqp)
*brevqp = NULL;
/*
* Power up the dip if it is powered off. If the flag bit
* NDI_AUTODETACH is set and the dip is not at its full power,
* skip the rest of the branch.
*/
if (pm_pre_unconfig(dip, flags, &pm_cookie, NULL) != DDI_SUCCESS)
return ((flags & NDI_AUTODETACH) ? NDI_SUCCESS :
NDI_FAILURE);
/*
* Some callers, notably SCSI, need to clear out the devfs
* cache together with the unconfig to prevent stale entries.
*/
if (flags & NDI_DEVFS_CLEAN)
(void) devfs_clean(dip, NULL, 0);
rv = unconfig_grand_children(dip, dipp, flags, major, brevqp);
if ((rv != NDI_SUCCESS) && ((flags & NDI_AUTODETACH) == 0)) {
if (brevqp && *brevqp) {
log_and_free_br_events_on_grand_children(dip, *brevqp);
free_brevq(*brevqp);
*brevqp = NULL;
}
pm_post_unconfig(dip, pm_cookie, NULL);
return (rv);
}
if (dipp && *dipp) {
ndi_rele_devi(*dipp);
*dipp = NULL;
}
/*
* It is possible to have a detached nexus with children
* and grandchildren (for example: a branch consisting
* entirely of bound nodes.) Since the nexus is detached
* the bus_unconfig entry point cannot be used to remove
* or unconfigure the descendants.
*/
if (!i_ddi_devi_attached(dip) ||
(DEVI(dip)->devi_ops->devo_bus_ops == NULL) ||
(DEVI(dip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
(f = DEVI(dip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
rv = unconfig_immediate_children(dip, dipp, flags, major);
} else {
/*
* call bus_unconfig entry point
* It should reset nexus flags if unconfigure succeeds.
*/
bus_op = (major == DDI_MAJOR_T_NONE) ?
BUS_UNCONFIG_ALL : BUS_UNCONFIG_DRIVER;
rv = (*f)(dip, flags, bus_op, (void *)(uintptr_t)major);
}
pm_post_unconfig(dip, pm_cookie, NULL);
if (brevqp && *brevqp)
cleanup_br_events_on_grand_children(dip, brevqp);
return (rv);
}
/*
* called by devfs/framework to unconfigure children bound to major
* If NDI_AUTODETACH is specified, this is invoked by either the
* moduninstall daemon or the modunload -i 0 command.
*/
int
ndi_devi_unconfig_driver(dev_info_t *dip, int flags, major_t major)
{
NDI_CONFIG_DEBUG((CE_CONT,
"ndi_devi_unconfig_driver: par = %s%d (%p), flags = 0x%x\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
return (devi_unconfig_common(dip, NULL, flags, major, NULL));
}
int
ndi_devi_unconfig(dev_info_t *dip, int flags)
{
NDI_CONFIG_DEBUG((CE_CONT,
"ndi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
return (devi_unconfig_common(dip, NULL, flags, DDI_MAJOR_T_NONE, NULL));
}
int
e_ddi_devi_unconfig(dev_info_t *dip, dev_info_t **dipp, int flags)
{
NDI_CONFIG_DEBUG((CE_CONT,
"e_ddi_devi_unconfig: par = %s%d (%p), flags = 0x%x\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip, flags));
return (devi_unconfig_common(dip, dipp, flags, DDI_MAJOR_T_NONE, NULL));
}
/*
* Unconfigure child by name
*/
static int
devi_unconfig_one(dev_info_t *pdip, char *devnm, int flags)
{
int rv, circ;
dev_info_t *child;
dev_info_t *vdip = NULL;
int v_circ;
ndi_devi_enter(pdip, &circ);
child = ndi_devi_findchild(pdip, devnm);
/*
* If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
* before parent(pHCI) to avoid deadlock with mpxio Client power
* management operations.
*/
if (child && MDI_PHCI(child)) {
vdip = mdi_devi_get_vdip(child);
if (vdip && (ddi_get_parent(vdip) != pdip)) {
ndi_devi_exit(pdip, circ);
/* use mdi_devi_enter ordering */
ndi_devi_enter(vdip, &v_circ);
ndi_devi_enter(pdip, &circ);
child = ndi_devi_findchild(pdip, devnm);
} else
vdip = NULL;
}
if (child) {
rv = devi_detach_node(child, flags);
} else {
NDI_CONFIG_DEBUG((CE_CONT,
"devi_unconfig_one: %s not found\n", devnm));
rv = NDI_SUCCESS;
}
ndi_devi_exit(pdip, circ);
if (vdip)
ndi_devi_exit(vdip, v_circ);
return (rv);
}
int
ndi_devi_unconfig_one(
dev_info_t *pdip,
char *devnm,
dev_info_t **dipp,
int flags)
{
int (*f)();
int circ, rv;
int pm_cookie;
dev_info_t *child;
dev_info_t *vdip = NULL;
int v_circ;
struct brevq_node *brevq = NULL;
ASSERT(i_ddi_devi_attached(pdip));
NDI_CONFIG_DEBUG((CE_CONT,
"ndi_devi_unconfig_one: par = %s%d (%p), child = %s\n",
ddi_driver_name(pdip), ddi_get_instance(pdip),
(void *)pdip, devnm));
if (pm_pre_unconfig(pdip, flags, &pm_cookie, devnm) != DDI_SUCCESS)
return (NDI_FAILURE);
if (dipp)
*dipp = NULL;
ndi_devi_enter(pdip, &circ);
child = ndi_devi_findchild(pdip, devnm);
/*
* If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
* before parent(pHCI) to avoid deadlock with mpxio Client power
* management operations.
*/
if (child && MDI_PHCI(child)) {
vdip = mdi_devi_get_vdip(child);
if (vdip && (ddi_get_parent(vdip) != pdip)) {
ndi_devi_exit(pdip, circ);
/* use mdi_devi_enter ordering */
ndi_devi_enter(vdip, &v_circ);
ndi_devi_enter(pdip, &circ);
child = ndi_devi_findchild(pdip, devnm);
} else
vdip = NULL;
}
if (child == NULL) {
NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_unconfig_one: %s"
" not found\n", devnm));
rv = NDI_SUCCESS;
goto out;
}
/*
* Unconfigure children/descendants of named child
*/
rv = devi_unconfig_branch(child, dipp, flags | NDI_UNCONFIG, &brevq);
if (rv != NDI_SUCCESS)
goto out;
init_bound_node_ev(pdip, child, flags);
if ((DEVI(pdip)->devi_ops->devo_bus_ops == NULL) ||
(DEVI(pdip)->devi_ops->devo_bus_ops->busops_rev < BUSO_REV_5) ||
(f = DEVI(pdip)->devi_ops->devo_bus_ops->bus_unconfig) == NULL) {
rv = devi_detach_node(child, flags);
} else {
/* call bus_config entry point */
rv = (*f)(pdip, flags, BUS_UNCONFIG_ONE, (void *)devnm);
}
if (brevq) {
if (rv != NDI_SUCCESS)
log_and_free_brevq_dip(child, brevq);
else
free_brevq(brevq);
}
if (dipp && rv != NDI_SUCCESS) {
ndi_hold_devi(child);
ASSERT(*dipp == NULL);
*dipp = child;
}
out:
ndi_devi_exit(pdip, circ);
if (vdip)
ndi_devi_exit(vdip, v_circ);
pm_post_unconfig(pdip, pm_cookie, devnm);
return (rv);
}
struct async_arg {
dev_info_t *dip;
uint_t flags;
};
/*
* Common async handler for:
* ndi_devi_bind_driver_async
* ndi_devi_online_async
*/
static int
i_ndi_devi_async_common(dev_info_t *dip, uint_t flags, void (*func)())
{
int tqflag;
int kmflag;
struct async_arg *arg;
dev_info_t *pdip = ddi_get_parent(dip);
ASSERT(pdip);
ASSERT(DEVI(pdip)->devi_taskq);
ASSERT(ndi_dev_is_persistent_node(dip));
if (flags & NDI_NOSLEEP) {
kmflag = KM_NOSLEEP;
tqflag = TQ_NOSLEEP;
} else {
kmflag = KM_SLEEP;
tqflag = TQ_SLEEP;
}
arg = kmem_alloc(sizeof (*arg), kmflag);
if (arg == NULL)
goto fail;
arg->flags = flags;
arg->dip = dip;
if (ddi_taskq_dispatch(DEVI(pdip)->devi_taskq, func, arg, tqflag) ==
DDI_SUCCESS) {
return (NDI_SUCCESS);
}
fail:
NDI_CONFIG_DEBUG((CE_CONT, "%s%d: ddi_taskq_dispatch failed",
ddi_driver_name(pdip), ddi_get_instance(pdip)));
if (arg)
kmem_free(arg, sizeof (*arg));
return (NDI_FAILURE);
}
static void
i_ndi_devi_bind_driver_cb(struct async_arg *arg)
{
(void) ndi_devi_bind_driver(arg->dip, arg->flags);
kmem_free(arg, sizeof (*arg));
}
int
ndi_devi_bind_driver_async(dev_info_t *dip, uint_t flags)
{
return (i_ndi_devi_async_common(dip, flags,
(void (*)())i_ndi_devi_bind_driver_cb));
}
/*
* place the devinfo in the ONLINE state.
*/
int
ndi_devi_online(dev_info_t *dip, uint_t flags)
{
int circ, rv;
dev_info_t *pdip = ddi_get_parent(dip);
int branch_event = 0;
ASSERT(pdip);
NDI_CONFIG_DEBUG((CE_CONT, "ndi_devi_online: %s%d (%p)\n",
ddi_driver_name(dip), ddi_get_instance(dip), (void *)dip));
ndi_devi_enter(pdip, &circ);
/* bind child before merging .conf nodes */
rv = i_ndi_config_node(dip, DS_BOUND, flags);
if (rv != NDI_SUCCESS) {
ndi_devi_exit(pdip, circ);
return (rv);
}
/* merge .conf properties */
(void) i_ndi_make_spec_children(pdip, flags);
flags |= (NDI_DEVI_ONLINE | NDI_CONFIG);
if (flags & NDI_NO_EVENT) {
/*
* Caller is specifically asking for not to generate an event.
* Set the following flag so that devi_attach_node() don't
* change the event state.
*/
flags |= NDI_NO_EVENT_STATE_CHNG;
}
if ((flags & (NDI_NO_EVENT | NDI_BRANCH_EVENT_OP)) == 0 &&
((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip))) {
flags |= NDI_BRANCH_EVENT_OP;
branch_event = 1;
}
/*
* devi_attach_node() may remove dip on failure
*/
if ((rv = devi_attach_node(dip, flags)) == NDI_SUCCESS) {
if ((flags & NDI_CONFIG) || DEVI_NEED_NDI_CONFIG(dip)) {
(void) ndi_devi_config(dip, flags);
}
if (branch_event)
(void) i_log_devfs_branch_add(dip);
}
ndi_devi_exit(pdip, circ);
/*
* Notify devfs that we have a new node. Devfs needs to invalidate
* cached directory contents.
*
* For PCMCIA devices, it is possible the pdip is not fully
* attached. In this case, calling back into devfs will
* result in a loop or assertion error. Hence, the check
* on node state.
*
* If we own parent lock, this is part of a branch operation.
* We skip the devfs_clean() step because the cache invalidation
* is done higher up in the device tree.
*/
if (rv == NDI_SUCCESS && i_ddi_devi_attached(pdip) &&
!DEVI_BUSY_OWNED(pdip))
(void) devfs_clean(pdip, NULL, 0);
return (rv);
}
static void
i_ndi_devi_online_cb(struct async_arg *arg)
{
(void) ndi_devi_online(arg->dip, arg->flags);
kmem_free(arg, sizeof (*arg));
}
int
ndi_devi_online_async(dev_info_t *dip, uint_t flags)
{
/* mark child as need config if requested. */
if (flags & NDI_CONFIG) {
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_NDI_CONFIG(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
}
return (i_ndi_devi_async_common(dip, flags,
(void (*)())i_ndi_devi_online_cb));
}
/*
* Take a device node Offline
* To take a device Offline means to detach the device instance from
* the driver and prevent devfs requests from re-attaching the device
* instance.
*
* The flag NDI_DEVI_REMOVE causes removes the device node from
* the driver list and the device tree. In this case, the device
* is assumed to be removed from the system.
*/
int
ndi_devi_offline(dev_info_t *dip, uint_t flags)
{
int circ, rval = 0;
dev_info_t *pdip = ddi_get_parent(dip);
dev_info_t *vdip = NULL;
int v_circ;
struct brevq_node *brevq = NULL;
ASSERT(pdip);
flags |= NDI_DEVI_OFFLINE;
/*
* If child is pHCI and vHCI and pHCI are not siblings then enter vHCI
* before parent(pHCI) to avoid deadlock with mpxio Client power
* management operations.
*/
if (MDI_PHCI(dip)) {
vdip = mdi_devi_get_vdip(dip);
if (vdip && (ddi_get_parent(vdip) != pdip))
ndi_devi_enter(vdip, &v_circ);
else
vdip = NULL;
}
ndi_devi_enter(pdip, &circ);
if (i_ddi_devi_attached(dip)) {
/*
* If dip is in DS_READY state, there may be cached dv_nodes
* referencing this dip, so we invoke devfs code path.
* Note that we must release busy changing on pdip to
* avoid deadlock against devfs.
*/
char *devname = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
(void) ddi_deviname(dip, devname);
ndi_devi_exit(pdip, circ);
if (vdip)
ndi_devi_exit(vdip, v_circ);
/*
* If we are explictly told to clean, then clean. If we own the
* parent lock then this is part of a branch operation, and we
* skip the devfs_clean() step.
*
* NOTE: A thread performing a devfs file system lookup/
* bus_config can't call devfs_clean to unconfig without
* causing rwlock problems in devfs. For ndi_devi_offline, this
* means that the NDI_DEVFS_CLEAN flag is safe from ioctl code
* or from an async hotplug thread, but is not safe from a
* nexus driver's bus_config implementation.
*/
if ((flags & NDI_DEVFS_CLEAN) ||
(!DEVI_BUSY_OWNED(pdip)))
(void) devfs_clean(pdip, devname + 1, DV_CLEAN_FORCE);
kmem_free(devname, MAXNAMELEN + 1);
rval = devi_unconfig_branch(dip, NULL, flags|NDI_UNCONFIG,
&brevq);
if (rval)
return (NDI_FAILURE);
if (vdip)
ndi_devi_enter(vdip, &v_circ);
ndi_devi_enter(pdip, &circ);
}
init_bound_node_ev(pdip, dip, flags);
rval = devi_detach_node(dip, flags);
if (brevq) {
if (rval != NDI_SUCCESS)
log_and_free_brevq_dip(dip, brevq);
else
free_brevq(brevq);
}
ndi_devi_exit(pdip, circ);
if (vdip)
ndi_devi_exit(vdip, v_circ);
return (rval);
}
/*
* Find the child dev_info node of parent nexus 'p' whose unit address
* matches "cname@caddr". Recommend use of ndi_devi_findchild() instead.
*/
dev_info_t *
ndi_devi_find(dev_info_t *pdip, char *cname, char *caddr)
{
dev_info_t *child;
int circ;
if (pdip == NULL || cname == NULL || caddr == NULL)
return ((dev_info_t *)NULL);
ndi_devi_enter(pdip, &circ);
child = find_sibling(ddi_get_child(pdip), cname, caddr,
FIND_NODE_BY_NODENAME, NULL);
ndi_devi_exit(pdip, circ);
return (child);
}
/*
* Find the child dev_info node of parent nexus 'p' whose unit address
* matches devname "name@addr". Permits caller to hold the parent.
*/
dev_info_t *
ndi_devi_findchild(dev_info_t *pdip, char *devname)
{
dev_info_t *child;
char *cname, *caddr;
char *devstr;
ASSERT(DEVI_BUSY_OWNED(pdip));
devstr = i_ddi_strdup(devname, KM_SLEEP);
i_ddi_parse_name(devstr, &cname, &caddr, NULL);
if (cname == NULL || caddr == NULL) {
kmem_free(devstr, strlen(devname)+1);
return ((dev_info_t *)NULL);
}
child = find_sibling(ddi_get_child(pdip), cname, caddr,
FIND_NODE_BY_NODENAME, NULL);
kmem_free(devstr, strlen(devname)+1);
return (child);
}
/*
* Misc. routines called by framework only
*/
/*
* Clear the DEVI_MADE_CHILDREN/DEVI_ATTACHED_CHILDREN flags
* if new child spec has been added.
*/
static int
reset_nexus_flags(dev_info_t *dip, void *arg)
{
struct hwc_spec *list;
int circ;
if (((DEVI(dip)->devi_flags & DEVI_MADE_CHILDREN) == 0) ||
((list = hwc_get_child_spec(dip, (major_t)(uintptr_t)arg)) == NULL))
return (DDI_WALK_CONTINUE);
hwc_free_spec_list(list);
/* coordinate child state update */
ndi_devi_enter(dip, &circ);
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(dip)->devi_flags &= ~(DEVI_MADE_CHILDREN | DEVI_ATTACHED_CHILDREN);
mutex_exit(&DEVI(dip)->devi_lock);
ndi_devi_exit(dip, circ);
return (DDI_WALK_CONTINUE);
}
/*
* Helper functions, returns NULL if no memory.
*/
/*
* path_to_major:
*
* Return an alternate driver name binding for the leaf device
* of the given pathname, if there is one. The purpose of this
* function is to deal with generic pathnames. The default action
* for platforms that can't do this (ie: x86 or any platform that
* does not have prom_finddevice functionality, which matches
* nodenames and unit-addresses without the drivers participation)
* is to return DDI_MAJOR_T_NONE.
*
* Used in loadrootmodules() in the swapgeneric module to
* associate a given pathname with a given leaf driver.
*
*/
major_t
path_to_major(char *path)
{
dev_info_t *dip;
char *p, *q;
pnode_t nodeid;
major_t major;
/* check for path-oriented alias */
major = ddi_name_to_major(path);
if (driver_installed(major)) {
NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s path bound %s\n",
path, ddi_major_to_name(major)));
return (major);
}
/*
* Get the nodeid of the given pathname, if such a mapping exists.
*/
dip = NULL;
nodeid = prom_finddevice(path);
if (nodeid != OBP_BADNODE) {
/*
* Find the nodeid in our copy of the device tree and return
* whatever name we used to bind this node to a driver.
*/
dip = e_ddi_nodeid_to_dip(nodeid);
}
if (dip == NULL) {
NDI_CONFIG_DEBUG((CE_WARN,
"path_to_major: can't bind <%s>\n", path));
return (DDI_MAJOR_T_NONE);
}
/*
* If we're bound to something other than the nodename,
* note that in the message buffer and system log.
*/
p = ddi_binding_name(dip);
q = ddi_node_name(dip);
if (p && q && (strcmp(p, q) != 0))
NDI_CONFIG_DEBUG((CE_NOTE, "path_to_major: %s bound to %s\n",
path, p));
major = ddi_name_to_major(p);
ndi_rele_devi(dip); /* release e_ddi_nodeid_to_dip hold */
return (major);
}
/*
* Return the held dip for the specified major and instance, attempting to do
* an attach if specified. Return NULL if the devi can't be found or put in
* the proper state. The caller must release the hold via ddi_release_devi if
* a non-NULL value is returned.
*
* Some callers expect to be able to perform a hold_devi() while in a context
* where using ndi_devi_enter() to ensure the hold might cause deadlock (see
* open-from-attach code in consconfig_dacf.c). Such special-case callers
* must ensure that an ndi_devi_enter(parent)/ndi_hold_devi() from a safe
* context is already active. The hold_devi() implementation must accommodate
* these callers.
*/
static dev_info_t *
hold_devi(major_t major, int instance, int flags)
{
struct devnames *dnp;
dev_info_t *dip;
char *path;
char *vpath;
if ((major >= devcnt) || (instance == -1))
return (NULL);
/* try to find the instance in the per driver list */
dnp = &(devnamesp[major]);
LOCK_DEV_OPS(&(dnp->dn_lock));
for (dip = dnp->dn_head; dip;
dip = (dev_info_t *)DEVI(dip)->devi_next) {
/* skip node if instance field is not valid */
if (i_ddi_node_state(dip) < DS_INITIALIZED)
continue;
/* look for instance match */
if (DEVI(dip)->devi_instance == instance) {
/*
* To accommodate callers that can't block in
* ndi_devi_enter() we do an ndi_hold_devi(), and
* afterwards check that the node is in a state where
* the hold prevents detach(). If we did not manage to
* prevent detach then we ndi_rele_devi() and perform
* the slow path below (which can result in a blocking
* ndi_devi_enter() while driving attach top-down).
* This code depends on the ordering of
* DEVI_SET_DETACHING and the devi_ref check in the
* detach_node() code path.
*/
ndi_hold_devi(dip);
if (i_ddi_devi_attached(dip) &&
!DEVI_IS_DETACHING(dip)) {
UNLOCK_DEV_OPS(&(dnp->dn_lock));
return (dip); /* fast-path with devi held */
}
ndi_rele_devi(dip);
/* try slow-path */
dip = NULL;
break;
}
}
ASSERT(dip == NULL);
UNLOCK_DEV_OPS(&(dnp->dn_lock));
if (flags & E_DDI_HOLD_DEVI_NOATTACH)
return (NULL); /* told not to drive attach */
/* slow-path may block, so it should not occur from interrupt */
ASSERT(!servicing_interrupt());
if (servicing_interrupt())
return (NULL);
/* reconstruct the path and drive attach by path through devfs. */
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if (e_ddi_majorinstance_to_path(major, instance, path) == 0) {
dip = e_ddi_hold_devi_by_path(path, flags);
/*
* Verify that we got the correct device - a path_to_inst file
* with a bogus/corrupt path (or a nexus that changes its
* unit-address format) could result in an incorrect answer
*
* Verify major, instance, and path.
*/
vpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
if (dip &&
((DEVI(dip)->devi_major != major) ||
((DEVI(dip)->devi_instance != instance)) ||
(strcmp(path, ddi_pathname(dip, vpath)) != 0))) {
ndi_rele_devi(dip);
dip = NULL; /* no answer better than wrong answer */
}
kmem_free(vpath, MAXPATHLEN);
}
kmem_free(path, MAXPATHLEN);
return (dip); /* with devi held */
}
/*
* The {e_}ddi_hold_devi{_by_{instance|dev|path}} hold the devinfo node
* associated with the specified arguments. This hold should be released
* by calling ddi_release_devi.
*
* The E_DDI_HOLD_DEVI_NOATTACH flag argument allows the caller to to specify
* a failure return if the node is not already attached.
*
* NOTE: by the time we make e_ddi_hold_devi public, we should be able to reuse
* ddi_hold_devi again.
*/
dev_info_t *
ddi_hold_devi_by_instance(major_t major, int instance, int flags)
{
return (hold_devi(major, instance, flags));
}
dev_info_t *
e_ddi_hold_devi_by_dev(dev_t dev, int flags)
{
major_t major = getmajor(dev);
dev_info_t *dip;
struct dev_ops *ops;
dev_info_t *ddip = NULL;
dip = hold_devi(major, dev_to_instance(dev), flags);
/*
* The rest of this routine is legacy support for drivers that
* have broken DDI_INFO_DEVT2INSTANCE implementations but may have
* functional DDI_INFO_DEVT2DEVINFO implementations. This code will
* diagnose inconsistency and, for maximum compatibility with legacy
* drivers, give preference to the drivers DDI_INFO_DEVT2DEVINFO
* implementation over the above derived dip based the driver's
* DDI_INFO_DEVT2INSTANCE implementation. This legacy support should
* be removed when DDI_INFO_DEVT2DEVINFO is deprecated.
*
* NOTE: The following code has a race condition. DEVT2DEVINFO
* returns a dip which is not held. By the time we ref ddip,
* it could have been freed. The saving grace is that for
* most drivers, the dip returned from hold_devi() is the
* same one as the one returned by DEVT2DEVINFO, so we are
* safe for drivers with the correct getinfo(9e) impl.
*/
if (((ops = ddi_hold_driver(major)) != NULL) &&
CB_DRV_INSTALLED(ops) && ops->devo_getinfo) {
if ((*ops->devo_getinfo)(NULL, DDI_INFO_DEVT2DEVINFO,
(void *)dev, (void **)&ddip) != DDI_SUCCESS)
ddip = NULL;
}
/* give preference to the driver returned DEVT2DEVINFO dip */
if (ddip && (dip != ddip)) {
#ifdef DEBUG
cmn_err(CE_WARN, "%s: inconsistent getinfo(9E) implementation",
ddi_driver_name(ddip));
#endif /* DEBUG */
ndi_hold_devi(ddip);
if (dip)
ndi_rele_devi(dip);
dip = ddip;
}
if (ops)
ddi_rele_driver(major);
return (dip);
}
/*
* For compatibility only. Do not call this function!
*/
dev_info_t *
e_ddi_get_dev_info(dev_t dev, vtype_t type)
{
dev_info_t *dip = NULL;
if (getmajor(dev) >= devcnt)
return (NULL);
switch (type) {
case VCHR:
case VBLK:
dip = e_ddi_hold_devi_by_dev(dev, 0);
default:
break;
}
/*
* For compatibility reasons, we can only return the dip with
* the driver ref count held. This is not a safe thing to do.
* For certain broken third-party software, we are willing
* to venture into unknown territory.
*/
if (dip) {
(void) ndi_hold_driver(dip);
ndi_rele_devi(dip);
}
return (dip);
}
dev_info_t *
e_ddi_hold_devi_by_path(char *path, int flags)
{
dev_info_t *dip;
/* can't specify NOATTACH by path */
ASSERT(!(flags & E_DDI_HOLD_DEVI_NOATTACH));
return (resolve_pathname(path, &dip, NULL, NULL) ? NULL : dip);
}
void
e_ddi_hold_devi(dev_info_t *dip)
{
ndi_hold_devi(dip);
}
void
ddi_release_devi(dev_info_t *dip)
{
ndi_rele_devi(dip);
}
/*
* Associate a streams queue with a devinfo node
* NOTE: This function is called by STREAM driver's put procedure.
* It cannot block.
*/
void
ddi_assoc_queue_with_devi(queue_t *q, dev_info_t *dip)
{
queue_t *rq = _RD(q);
struct stdata *stp;
vnode_t *vp;
/* set flag indicating that ddi_assoc_queue_with_devi was called */
mutex_enter(QLOCK(rq));
rq->q_flag |= _QASSOCIATED;
mutex_exit(QLOCK(rq));
/* get the vnode associated with the queue */
stp = STREAM(rq);
vp = stp->sd_vnode;
ASSERT(vp);
/* change the hardware association of the vnode */
spec_assoc_vp_with_devi(vp, dip);
}
/*
* ddi_install_driver(name)
*
* Driver installation is currently a byproduct of driver loading. This
* may change.
*/
int
ddi_install_driver(char *name)
{
major_t major = ddi_name_to_major(name);
if ((major == DDI_MAJOR_T_NONE) ||
(ddi_hold_installed_driver(major) == NULL)) {
return (DDI_FAILURE);
}
ddi_rele_driver(major);
return (DDI_SUCCESS);
}
struct dev_ops *
ddi_hold_driver(major_t major)
{
return (mod_hold_dev_by_major(major));
}
void
ddi_rele_driver(major_t major)
{
mod_rele_dev_by_major(major);
}
/*
* This is called during boot to force attachment order of special dips
* dip must be referenced via ndi_hold_devi()
*/
int
i_ddi_attach_node_hierarchy(dev_info_t *dip)
{
dev_info_t *parent;
int ret, circ;
/*
* Recurse up until attached parent is found.
*/
if (i_ddi_devi_attached(dip))
return (DDI_SUCCESS);
parent = ddi_get_parent(dip);
if (i_ddi_attach_node_hierarchy(parent) != DDI_SUCCESS)
return (DDI_FAILURE);
/*
* Come top-down, expanding .conf nodes under this parent
* and driving attach.
*/
ndi_devi_enter(parent, &circ);
(void) i_ndi_make_spec_children(parent, 0);
ret = i_ddi_attachchild(dip);
ndi_devi_exit(parent, circ);
return (ret);
}
/* keep this function static */
static int
attach_driver_nodes(major_t major)
{
struct devnames *dnp;
dev_info_t *dip;
int error = DDI_FAILURE;
int circ;
dnp = &devnamesp[major];
LOCK_DEV_OPS(&dnp->dn_lock);
dip = dnp->dn_head;
while (dip) {
ndi_hold_devi(dip);
UNLOCK_DEV_OPS(&dnp->dn_lock);
if (i_ddi_attach_node_hierarchy(dip) == DDI_SUCCESS)
error = DDI_SUCCESS;
/*
* Set the 'ddi-config-driver-node' property on a nexus
* node to cause attach_driver_nodes() to configure all
* immediate children of the nexus. This property should
* be set on nodes with immediate children that bind to
* the same driver as parent.
*/
if ((error == DDI_SUCCESS) && (ddi_prop_exists(DDI_DEV_T_ANY,
dip, DDI_PROP_DONTPASS, "ddi-config-driver-node"))) {
ndi_devi_enter(dip, &circ);
(void) ndi_devi_config(dip, NDI_NO_EVENT);
ndi_devi_exit(dip, circ);
}
LOCK_DEV_OPS(&dnp->dn_lock);
ndi_rele_devi(dip);
dip = ddi_get_next(dip);
}
if (error == DDI_SUCCESS)
dnp->dn_flags |= DN_NO_AUTODETACH;
UNLOCK_DEV_OPS(&dnp->dn_lock);
return (error);
}
/*
* i_ddi_attach_hw_nodes configures and attaches all hw nodes
* bound to a specific driver. This function replaces calls to
* ddi_hold_installed_driver() for drivers with no .conf
* enumerated nodes.
*
* This facility is typically called at boot time to attach
* platform-specific hardware nodes, such as ppm nodes on xcal
* and grover and keyswitch nodes on cherrystone. It does not
* deal with .conf enumerated node. Calling it beyond the boot
* process is strongly discouraged.
*/
int
i_ddi_attach_hw_nodes(char *driver)
{
major_t major;
major = ddi_name_to_major(driver);
if (major == DDI_MAJOR_T_NONE)
return (DDI_FAILURE);
return (attach_driver_nodes(major));
}
/*
* i_ddi_attach_pseudo_node configures pseudo drivers which
* has a single node. The .conf nodes must be enumerated
* before calling this interface. The dip is held attached
* upon returning.
*
* This facility should only be called only at boot time
* by the I/O framework.
*/
dev_info_t *
i_ddi_attach_pseudo_node(char *driver)
{
major_t major;
dev_info_t *dip;
major = ddi_name_to_major(driver);
if (major == DDI_MAJOR_T_NONE)
return (NULL);
if (attach_driver_nodes(major) != DDI_SUCCESS)
return (NULL);
dip = devnamesp[major].dn_head;
ASSERT(dip && ddi_get_next(dip) == NULL);
ndi_hold_devi(dip);
return (dip);
}
static void
diplist_to_parent_major(dev_info_t *head, char parents[])
{
major_t major;
dev_info_t *dip, *pdip;
for (dip = head; dip != NULL; dip = ddi_get_next(dip)) {
pdip = ddi_get_parent(dip);
ASSERT(pdip); /* disallow rootnex.conf nodes */
major = ddi_driver_major(pdip);
if ((major != DDI_MAJOR_T_NONE) && parents[major] == 0)
parents[major] = 1;
}
}
/*
* Call ddi_hold_installed_driver() on each parent major
* and invoke mt_config_driver() to attach child major.
* This is part of the implementation of ddi_hold_installed_driver.
*/
static int
attach_driver_by_parent(major_t child_major, char parents[])
{
major_t par_major;
struct mt_config_handle *hdl;
int flags = NDI_DEVI_PERSIST | NDI_NO_EVENT;
hdl = mt_config_init(NULL, NULL, flags, child_major, MT_CONFIG_OP,
NULL);
for (par_major = 0; par_major < devcnt; par_major++) {
/* disallow recursion on the same driver */
if (parents[par_major] == 0 || par_major == child_major)
continue;
if (ddi_hold_installed_driver(par_major) == NULL)
continue;
hdl->mtc_parmajor = par_major;
mt_config_driver(hdl);
ddi_rele_driver(par_major);
}
(void) mt_config_fini(hdl);
return (i_ddi_devs_attached(child_major));
}
int
i_ddi_devs_attached(major_t major)
{
dev_info_t *dip;
struct devnames *dnp;
int error = DDI_FAILURE;
/* check for attached instances */
dnp = &devnamesp[major];
LOCK_DEV_OPS(&dnp->dn_lock);
for (dip = dnp->dn_head; dip != NULL; dip = ddi_get_next(dip)) {
if (i_ddi_devi_attached(dip)) {
error = DDI_SUCCESS;
break;
}
}
UNLOCK_DEV_OPS(&dnp->dn_lock);
return (error);
}
int
i_ddi_minor_node_count(dev_info_t *ddip, const char *node_type)
{
int circ;
struct ddi_minor_data *dp;
int count = 0;
ndi_devi_enter(ddip, &circ);
for (dp = DEVI(ddip)->devi_minor; dp != NULL; dp = dp->next) {
if (strcmp(dp->ddm_node_type, node_type) == 0)
count++;
}
ndi_devi_exit(ddip, circ);
return (count);
}
/*
* ddi_hold_installed_driver configures and attaches all
* instances of the specified driver. To accomplish this
* it configures and attaches all possible parents of
* the driver, enumerated both in h/w nodes and in the
* driver's .conf file.
*
* NOTE: This facility is for compatibility purposes only and will
* eventually go away. Its usage is strongly discouraged.
*/
static void
enter_driver(struct devnames *dnp)
{
mutex_enter(&dnp->dn_lock);
ASSERT(dnp->dn_busy_thread != curthread);
while (dnp->dn_flags & DN_DRIVER_BUSY)
cv_wait(&dnp->dn_wait, &dnp->dn_lock);
dnp->dn_flags |= DN_DRIVER_BUSY;
dnp->dn_busy_thread = curthread;
mutex_exit(&dnp->dn_lock);
}
static void
exit_driver(struct devnames *dnp)
{
mutex_enter(&dnp->dn_lock);
ASSERT(dnp->dn_busy_thread == curthread);
dnp->dn_flags &= ~DN_DRIVER_BUSY;
dnp->dn_busy_thread = NULL;
cv_broadcast(&dnp->dn_wait);
mutex_exit(&dnp->dn_lock);
}
struct dev_ops *
ddi_hold_installed_driver(major_t major)
{
struct dev_ops *ops;
struct devnames *dnp;
char *parents;
int error;
ops = ddi_hold_driver(major);
if (ops == NULL)
return (NULL);
/*
* Return immediately if all the attach operations associated
* with a ddi_hold_installed_driver() call have already been done.
*/
dnp = &devnamesp[major];
enter_driver(dnp);
ASSERT(driver_installed(major));
if (dnp->dn_flags & DN_DRIVER_HELD) {
exit_driver(dnp);
if (i_ddi_devs_attached(major) == DDI_SUCCESS)
return (ops);
ddi_rele_driver(major);
return (NULL);
}
LOCK_DEV_OPS(&dnp->dn_lock);
dnp->dn_flags |= (DN_DRIVER_HELD | DN_NO_AUTODETACH);
UNLOCK_DEV_OPS(&dnp->dn_lock);
DCOMPATPRINTF((CE_CONT,
"ddi_hold_installed_driver: %s\n", dnp->dn_name));
/*
* When the driver has no .conf children, it is sufficient
* to attach existing nodes in the device tree. Nodes not
* enumerated by the OBP are not attached.
*/
if (dnp->dn_pl == NULL) {
if (attach_driver_nodes(major) == DDI_SUCCESS) {
exit_driver(dnp);
return (ops);
}
exit_driver(dnp);
ddi_rele_driver(major);
return (NULL);
}
/*
* Driver has .conf nodes. We find all possible parents
* and recursively all ddi_hold_installed_driver on the
* parent driver; then we invoke ndi_config_driver()
* on all possible parent node in parallel to speed up
* performance.
*/
parents = kmem_zalloc(devcnt * sizeof (char), KM_SLEEP);
LOCK_DEV_OPS(&dnp->dn_lock);
/* find .conf parents */
(void) impl_parlist_to_major(dnp->dn_pl, parents);
/* find hw node parents */
diplist_to_parent_major(dnp->dn_head, parents);
UNLOCK_DEV_OPS(&dnp->dn_lock);
error = attach_driver_by_parent(major, parents);
kmem_free(parents, devcnt * sizeof (char));
if (error == DDI_SUCCESS) {
exit_driver(dnp);
return (ops);
}
exit_driver(dnp);
ddi_rele_driver(major);
return (NULL);
}
/*
* Default bus_config entry point for nexus drivers
*/
int
ndi_busop_bus_config(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
void *arg, dev_info_t **child, clock_t timeout)
{
major_t major;
/*
* A timeout of 30 minutes or more is probably a mistake
* This is intended to catch uses where timeout is in
* the wrong units. timeout must be in units of ticks.
*/
ASSERT(timeout < SEC_TO_TICK(1800));
major = DDI_MAJOR_T_NONE;
switch (op) {
case BUS_CONFIG_ONE:
NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config %s timeout=%ld\n",
ddi_driver_name(pdip), ddi_get_instance(pdip),
(char *)arg, timeout));
return (devi_config_one(pdip, (char *)arg, child, flags,
timeout));
case BUS_CONFIG_DRIVER:
major = (major_t)(uintptr_t)arg;
/*FALLTHROUGH*/
case BUS_CONFIG_ALL:
NDI_DEBUG(flags, (CE_CONT, "%s%d: bus config timeout=%ld\n",
ddi_driver_name(pdip), ddi_get_instance(pdip),
timeout));
if (timeout > 0) {
NDI_DEBUG(flags, (CE_CONT,
"%s%d: bus config all timeout=%ld\n",
ddi_driver_name(pdip), ddi_get_instance(pdip),
timeout));
delay(timeout);
}
return (config_immediate_children(pdip, flags, major));
default:
return (NDI_FAILURE);
}
/*NOTREACHED*/
}
/*
* Default busop bus_unconfig handler for nexus drivers
*/
int
ndi_busop_bus_unconfig(dev_info_t *pdip, uint_t flags, ddi_bus_config_op_t op,
void *arg)
{
major_t major;
major = DDI_MAJOR_T_NONE;
switch (op) {
case BUS_UNCONFIG_ONE:
NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig %s\n",
ddi_driver_name(pdip), ddi_get_instance(pdip),
(char *)arg));
return (devi_unconfig_one(pdip, (char *)arg, flags));
case BUS_UNCONFIG_DRIVER:
major = (major_t)(uintptr_t)arg;
/*FALLTHROUGH*/
case BUS_UNCONFIG_ALL:
NDI_DEBUG(flags, (CE_CONT, "%s%d: bus unconfig all\n",
ddi_driver_name(pdip), ddi_get_instance(pdip)));
return (unconfig_immediate_children(pdip, NULL, flags, major));
default:
return (NDI_FAILURE);
}
/*NOTREACHED*/
}
/*
* dummy functions to be removed
*/
void
impl_rem_dev_props(dev_info_t *dip)
{
_NOTE(ARGUNUSED(dip))
/* do nothing */
}
/*
* Determine if a node is a leaf node. If not sure, return false (0).
*/
static int
is_leaf_node(dev_info_t *dip)
{
major_t major = ddi_driver_major(dip);
if (major == DDI_MAJOR_T_NONE)
return (0);
return (devnamesp[major].dn_flags & DN_LEAF_DRIVER);
}
/*
* Multithreaded [un]configuration
*/
static struct mt_config_handle *
mt_config_init(dev_info_t *pdip, dev_info_t **dipp, int flags,
major_t major, int op, struct brevq_node **brevqp)
{
struct mt_config_handle *hdl = kmem_alloc(sizeof (*hdl), KM_SLEEP);
mutex_init(&hdl->mtc_lock, NULL, MUTEX_DEFAULT, NULL);
cv_init(&hdl->mtc_cv, NULL, CV_DEFAULT, NULL);
hdl->mtc_pdip = pdip;
hdl->mtc_fdip = dipp;
hdl->mtc_parmajor = DDI_MAJOR_T_NONE;
hdl->mtc_flags = flags;
hdl->mtc_major = major;
hdl->mtc_thr_count = 0;
hdl->mtc_op = op;
hdl->mtc_error = 0;
hdl->mtc_brevqp = brevqp;
#ifdef DEBUG
gethrestime(&hdl->start_time);
hdl->total_time = 0;
#endif /* DEBUG */
return (hdl);
}
#ifdef DEBUG
static int
time_diff_in_msec(timestruc_t start, timestruc_t end)
{
int nsec, sec;
sec = end.tv_sec - start.tv_sec;
nsec = end.tv_nsec - start.tv_nsec;
if (nsec < 0) {
nsec += NANOSEC;
sec -= 1;
}
return (sec * (NANOSEC >> 20) + (nsec >> 20));
}
#endif /* DEBUG */
static int
mt_config_fini(struct mt_config_handle *hdl)
{
int rv;
#ifdef DEBUG
int real_time;
timestruc_t end_time;
#endif /* DEBUG */
mutex_enter(&hdl->mtc_lock);
while (hdl->mtc_thr_count > 0)
cv_wait(&hdl->mtc_cv, &hdl->mtc_lock);
rv = hdl->mtc_error;
mutex_exit(&hdl->mtc_lock);
#ifdef DEBUG
gethrestime(&end_time);
real_time = time_diff_in_msec(hdl->start_time, end_time);
if ((ddidebug & DDI_MTCONFIG) && hdl->mtc_pdip)
cmn_err(CE_NOTE,
"config %s%d: total time %d msec, real time %d msec",
ddi_driver_name(hdl->mtc_pdip),
ddi_get_instance(hdl->mtc_pdip),
hdl->total_time, real_time);
#endif /* DEBUG */
cv_destroy(&hdl->mtc_cv);
mutex_destroy(&hdl->mtc_lock);
kmem_free(hdl, sizeof (*hdl));
return (rv);
}
struct mt_config_data {
struct mt_config_handle *mtc_hdl;
dev_info_t *mtc_dip;
major_t mtc_major;
int mtc_flags;
struct brevq_node *mtc_brn;
struct mt_config_data *mtc_next;
};
static void
mt_config_thread(void *arg)
{
struct mt_config_data *mcd = (struct mt_config_data *)arg;
struct mt_config_handle *hdl = mcd->mtc_hdl;
dev_info_t *dip = mcd->mtc_dip;
dev_info_t *rdip, **dipp;
major_t major = mcd->mtc_major;
int flags = mcd->mtc_flags;
int rv = 0;
#ifdef DEBUG
timestruc_t start_time, end_time;
gethrestime(&start_time);
#endif /* DEBUG */
rdip = NULL;
dipp = hdl->mtc_fdip ? &rdip : NULL;
switch (hdl->mtc_op) {
case MT_CONFIG_OP:
rv = devi_config_common(dip, flags, major);
break;
case MT_UNCONFIG_OP:
if (mcd->mtc_brn) {
struct brevq_node *brevq = NULL;
rv = devi_unconfig_common(dip, dipp, flags, major,
&brevq);
mcd->mtc_brn->brn_child = brevq;
} else
rv = devi_unconfig_common(dip, dipp, flags, major,
NULL);
break;
}
mutex_enter(&hdl->mtc_lock);
#ifdef DEBUG
gethrestime(&end_time);
hdl->total_time += time_diff_in_msec(start_time, end_time);
#endif /* DEBUG */
if ((rv != NDI_SUCCESS) && (hdl->mtc_error == 0)) {
hdl->mtc_error = rv;
#ifdef DEBUG
if ((ddidebug & DDI_DEBUG) && (major != DDI_MAJOR_T_NONE)) {
char *path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
cmn_err(CE_NOTE, "mt_config_thread: "
"op %d.%d.%x at %s failed %d",
hdl->mtc_op, major, flags, path, rv);
kmem_free(path, MAXPATHLEN);
}
#endif /* DEBUG */
}
if (hdl->mtc_fdip && *hdl->mtc_fdip == NULL) {
*hdl->mtc_fdip = rdip;
rdip = NULL;
}
if (rdip) {
ASSERT(rv != NDI_SUCCESS);
ndi_rele_devi(rdip);
}
ndi_rele_devi(dip);
if (--hdl->mtc_thr_count == 0)
cv_broadcast(&hdl->mtc_cv);
mutex_exit(&hdl->mtc_lock);
kmem_free(mcd, sizeof (*mcd));
}
/*
* Multi-threaded config/unconfig of child nexus
*/
static void
mt_config_children(struct mt_config_handle *hdl)
{
dev_info_t *pdip = hdl->mtc_pdip;
major_t major = hdl->mtc_major;
dev_info_t *dip;
int circ;
struct brevq_node *brn;
struct mt_config_data *mcd_head = NULL;
struct mt_config_data *mcd_tail = NULL;
struct mt_config_data *mcd;
#ifdef DEBUG
timestruc_t end_time;
/* Update total_time in handle */
gethrestime(&end_time);
hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
#endif
ndi_devi_enter(pdip, &circ);
dip = ddi_get_child(pdip);
while (dip) {
if (hdl->mtc_op == MT_UNCONFIG_OP && hdl->mtc_brevqp &&
!(DEVI_EVREMOVE(dip)) &&
i_ddi_node_state(dip) >= DS_INITIALIZED) {
/*
* Enqueue this dip's deviname.
* No need to hold a lock while enqueuing since this
* is the only thread doing the enqueue and no one
* walks the queue while we are in multithreaded
* unconfiguration.
*/
brn = brevq_enqueue(hdl->mtc_brevqp, dip, NULL);
} else
brn = NULL;
/*
* Hold the child that we are processing so he does not get
* removed. The corrisponding ndi_rele_devi() for children
* that are not being skipped is done at the end of
* mt_config_thread().
*/
ndi_hold_devi(dip);
/*
* skip leaf nodes and (for configure) nodes not
* fully attached.
*/
if (is_leaf_node(dip) ||
(hdl->mtc_op == MT_CONFIG_OP &&
i_ddi_node_state(dip) < DS_READY)) {
ndi_rele_devi(dip);
dip = ddi_get_next_sibling(dip);
continue;
}
mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
mcd->mtc_dip = dip;
mcd->mtc_hdl = hdl;
mcd->mtc_brn = brn;
/*
* Switch a 'driver' operation to an 'all' operation below a
* node bound to the driver.
*/
if ((major == DDI_MAJOR_T_NONE) ||
(major == ddi_driver_major(dip)))
mcd->mtc_major = DDI_MAJOR_T_NONE;
else
mcd->mtc_major = major;
/*
* The unconfig-driver to unconfig-all conversion above
* constitutes an autodetach for NDI_DETACH_DRIVER calls,
* set NDI_AUTODETACH.
*/
mcd->mtc_flags = hdl->mtc_flags;
if ((mcd->mtc_flags & NDI_DETACH_DRIVER) &&
(hdl->mtc_op == MT_UNCONFIG_OP) &&
(major == ddi_driver_major(pdip)))
mcd->mtc_flags |= NDI_AUTODETACH;
mutex_enter(&hdl->mtc_lock);
hdl->mtc_thr_count++;
mutex_exit(&hdl->mtc_lock);
/*
* Add to end of list to process after ndi_devi_exit to avoid
* locking differences depending on value of mtc_off.
*/
mcd->mtc_next = NULL;
if (mcd_head == NULL)
mcd_head = mcd;
else
mcd_tail->mtc_next = mcd;
mcd_tail = mcd;
dip = ddi_get_next_sibling(dip);
}
ndi_devi_exit(pdip, circ);
/* go through the list of held children */
for (mcd = mcd_head; mcd; mcd = mcd_head) {
mcd_head = mcd->mtc_next;
if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
mt_config_thread(mcd);
else
(void) thread_create(NULL, 0, mt_config_thread, mcd,
0, &p0, TS_RUN, minclsyspri);
}
}
static void
mt_config_driver(struct mt_config_handle *hdl)
{
major_t par_major = hdl->mtc_parmajor;
major_t major = hdl->mtc_major;
struct devnames *dnp = &devnamesp[par_major];
dev_info_t *dip;
struct mt_config_data *mcd_head = NULL;
struct mt_config_data *mcd_tail = NULL;
struct mt_config_data *mcd;
#ifdef DEBUG
timestruc_t end_time;
/* Update total_time in handle */
gethrestime(&end_time);
hdl->total_time += time_diff_in_msec(hdl->start_time, end_time);
#endif
ASSERT(par_major != DDI_MAJOR_T_NONE);
ASSERT(major != DDI_MAJOR_T_NONE);
LOCK_DEV_OPS(&dnp->dn_lock);
dip = devnamesp[par_major].dn_head;
while (dip) {
/*
* Hold the child that we are processing so he does not get
* removed. The corrisponding ndi_rele_devi() for children
* that are not being skipped is done at the end of
* mt_config_thread().
*/
ndi_hold_devi(dip);
/* skip leaf nodes and nodes not fully attached */
if (!i_ddi_devi_attached(dip) || is_leaf_node(dip)) {
ndi_rele_devi(dip);
dip = ddi_get_next(dip);
continue;
}
mcd = kmem_alloc(sizeof (*mcd), KM_SLEEP);
mcd->mtc_dip = dip;
mcd->mtc_hdl = hdl;
mcd->mtc_major = major;
mcd->mtc_flags = hdl->mtc_flags;
mutex_enter(&hdl->mtc_lock);
hdl->mtc_thr_count++;
mutex_exit(&hdl->mtc_lock);
/*
* Add to end of list to process after UNLOCK_DEV_OPS to avoid
* locking differences depending on value of mtc_off.
*/
mcd->mtc_next = NULL;
if (mcd_head == NULL)
mcd_head = mcd;
else
mcd_tail->mtc_next = mcd;
mcd_tail = mcd;
dip = ddi_get_next(dip);
}
UNLOCK_DEV_OPS(&dnp->dn_lock);
/* go through the list of held children */
for (mcd = mcd_head; mcd; mcd = mcd_head) {
mcd_head = mcd->mtc_next;
if (mtc_off || (mcd->mtc_flags & NDI_MTC_OFF))
mt_config_thread(mcd);
else
(void) thread_create(NULL, 0, mt_config_thread, mcd,
0, &p0, TS_RUN, minclsyspri);
}
}
/*
* Given the nodeid for a persistent (PROM or SID) node, return
* the corresponding devinfo node
* NOTE: This function will return NULL for .conf nodeids.
*/
dev_info_t *
e_ddi_nodeid_to_dip(pnode_t nodeid)
{
dev_info_t *dip = NULL;
struct devi_nodeid *prev, *elem;
mutex_enter(&devimap->dno_lock);
prev = NULL;
for (elem = devimap->dno_head; elem; elem = elem->next) {
if (elem->nodeid == nodeid) {
ndi_hold_devi(elem->dip);
dip = elem->dip;
break;
}
prev = elem;
}
/*
* Move to head for faster lookup next time
*/
if (elem && prev) {
prev->next = elem->next;
elem->next = devimap->dno_head;
devimap->dno_head = elem;
}
mutex_exit(&devimap->dno_lock);
return (dip);
}
static void
free_cache_task(void *arg)
{
ASSERT(arg == NULL);
mutex_enter(&di_cache.cache_lock);
/*
* The cache can be invalidated without holding the lock
* but it can be made valid again only while the lock is held.
* So if the cache is invalid when the lock is held, it will
* stay invalid until lock is released.
*/
if (!di_cache.cache_valid)
i_ddi_di_cache_free(&di_cache);
mutex_exit(&di_cache.cache_lock);
if (di_cache_debug)
cmn_err(CE_NOTE, "system_taskq: di_cache freed");
}
extern int modrootloaded;
void
i_ddi_di_cache_free(struct di_cache *cache)
{
int error;
extern int sys_shutdown;
ASSERT(mutex_owned(&cache->cache_lock));
if (cache->cache_size) {
ASSERT(cache->cache_size > 0);
ASSERT(cache->cache_data);
kmem_free(cache->cache_data, cache->cache_size);
cache->cache_data = NULL;
cache->cache_size = 0;
if (di_cache_debug)
cmn_err(CE_NOTE, "i_ddi_di_cache_free: freed cachemem");
} else {
ASSERT(cache->cache_data == NULL);
if (di_cache_debug)
cmn_err(CE_NOTE, "i_ddi_di_cache_free: NULL cache");
}
if (!modrootloaded || rootvp == NULL ||
vn_is_readonly(rootvp) || sys_shutdown) {
if (di_cache_debug) {
cmn_err(CE_WARN, "/ not mounted/RDONLY. Skip unlink");
}
return;
}
error = vn_remove(DI_CACHE_FILE, UIO_SYSSPACE, RMFILE);
if (di_cache_debug && error && error != ENOENT) {
cmn_err(CE_WARN, "%s: unlink failed: %d", DI_CACHE_FILE, error);
} else if (di_cache_debug && !error) {
cmn_err(CE_NOTE, "i_ddi_di_cache_free: unlinked cache file");
}
}
void
i_ddi_di_cache_invalidate()
{
int cache_valid;
if (!modrootloaded || !i_ddi_io_initialized()) {
if (di_cache_debug)
cmn_err(CE_NOTE, "I/O not inited. Skipping invalidate");
return;
}
/* Increment devtree generation number. */
atomic_inc_ulong(&devtree_gen);
/* Invalidate the in-core cache and dispatch free on valid->invalid */
cache_valid = atomic_swap_uint(&di_cache.cache_valid, 0);
if (cache_valid) {
/*
* This is an optimization to start cleaning up a cached
* snapshot early. For this reason, it is OK for
* taskq_dispatach to fail (and it is OK to not track calling
* context relative to sleep, and assume NOSLEEP).
*/
(void) taskq_dispatch(system_taskq, free_cache_task, NULL,
TQ_NOSLEEP);
}
if (di_cache_debug) {
cmn_err(CE_NOTE, "invalidation");
}
}
static void
i_bind_vhci_node(dev_info_t *dip)
{
DEVI(dip)->devi_major = ddi_name_to_major(ddi_node_name(dip));
i_ddi_set_node_state(dip, DS_BOUND);
}
static char vhci_node_addr[2];
static int
i_init_vhci_node(dev_info_t *dip)
{
add_global_props(dip);
DEVI(dip)->devi_ops = ndi_hold_driver(dip);
if (DEVI(dip)->devi_ops == NULL)
return (-1);
DEVI(dip)->devi_instance = e_ddi_assign_instance(dip);
e_ddi_keep_instance(dip);
vhci_node_addr[0] = '\0';
ddi_set_name_addr(dip, vhci_node_addr);
i_ddi_set_node_state(dip, DS_INITIALIZED);
return (0);
}
static void
i_link_vhci_node(dev_info_t *dip)
{
ASSERT(MUTEX_HELD(&global_vhci_lock));
/*
* scsi_vhci should be kept left most of the device tree.
*/
if (scsi_vhci_dip) {
DEVI(dip)->devi_sibling = DEVI(scsi_vhci_dip)->devi_sibling;
DEVI(scsi_vhci_dip)->devi_sibling = DEVI(dip);
} else {
DEVI(dip)->devi_sibling = DEVI(top_devinfo)->devi_child;
DEVI(top_devinfo)->devi_child = DEVI(dip);
}
}
/*
* This a special routine to enumerate vhci node (child of rootnex
* node) without holding the ndi_devi_enter() lock. The device node
* is allocated, initialized and brought into DS_READY state before
* inserting into the device tree. The VHCI node is handcrafted
* here to bring the node to DS_READY, similar to rootnex node.
*
* The global_vhci_lock protects linking the node into the device
* as same lock is held before linking/unlinking any direct child
* of rootnex children.
*
* This routine is a workaround to handle a possible deadlock
* that occurs while trying to enumerate node in a different sub-tree
* during _init/_attach entry points.
*/
/*ARGSUSED*/
dev_info_t *
ndi_devi_config_vhci(char *drvname, int flags)
{
struct devnames *dnp;
dev_info_t *dip;
major_t major = ddi_name_to_major(drvname);
if (major == -1)
return (NULL);
/* Make sure we create the VHCI node only once */
dnp = &devnamesp[major];
LOCK_DEV_OPS(&dnp->dn_lock);
if (dnp->dn_head) {
dip = dnp->dn_head;
UNLOCK_DEV_OPS(&dnp->dn_lock);
return (dip);
}
UNLOCK_DEV_OPS(&dnp->dn_lock);
/* Allocate the VHCI node */
ndi_devi_alloc_sleep(top_devinfo, drvname, DEVI_SID_NODEID, &dip);
ndi_hold_devi(dip);
/* Mark the node as VHCI */
DEVI(dip)->devi_node_attributes |= DDI_VHCI_NODE;
i_ddi_add_devimap(dip);
i_bind_vhci_node(dip);
if (i_init_vhci_node(dip) == -1) {
ndi_rele_devi(dip);
(void) ndi_devi_free(dip);
return (NULL);
}
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_SET_ATTACHING(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
if (devi_attach(dip, DDI_ATTACH) != DDI_SUCCESS) {
cmn_err(CE_CONT, "Could not attach %s driver", drvname);
e_ddi_free_instance(dip, vhci_node_addr);
ndi_rele_devi(dip);
(void) ndi_devi_free(dip);
return (NULL);
}
mutex_enter(&(DEVI(dip)->devi_lock));
DEVI_CLR_ATTACHING(dip);
mutex_exit(&(DEVI(dip)->devi_lock));
mutex_enter(&global_vhci_lock);
i_link_vhci_node(dip);
mutex_exit(&global_vhci_lock);
i_ddi_set_node_state(dip, DS_READY);
LOCK_DEV_OPS(&dnp->dn_lock);
dnp->dn_flags |= DN_DRIVER_HELD;
dnp->dn_head = dip;
UNLOCK_DEV_OPS(&dnp->dn_lock);
i_ndi_devi_report_status_change(dip, NULL);
return (dip);
}
/*
* Maintain DEVI_DEVICE_REMOVED hotplug devi_state for remove/reinsert hotplug
* of open devices. Currently, because of tight coupling between the devfs file
* system and the Solaris device tree, a driver can't always make the device
* tree state (esp devi_node_state) match device hardware hotplug state. Until
* resolved, to overcome this deficiency we use the following interfaces that
* maintain the DEVI_DEVICE_REMOVED devi_state status bit. These interface
* report current state, and drive operation (like events and cache
* invalidation) when a driver changes remove/insert state of an open device.
*
* The ndi_devi_device_isremoved() returns 1 if the device is currently removed.
*
* The ndi_devi_device_remove() interface declares the device as removed, and
* returns 1 if there was a state change associated with this declaration.
*
* The ndi_devi_device_insert() declares the device as inserted, and returns 1
* if there was a state change associated with this declaration.
*/
int
ndi_devi_device_isremoved(dev_info_t *dip)
{
return (DEVI_IS_DEVICE_REMOVED(dip));
}
int
ndi_devi_device_remove(dev_info_t *dip)
{
ASSERT(dip && ddi_get_parent(dip) &&
DEVI_BUSY_OWNED(ddi_get_parent(dip)));
/* Return if already marked removed. */
if (ndi_devi_device_isremoved(dip))
return (0);
/* Mark the device as having been physically removed. */
mutex_enter(&(DEVI(dip)->devi_lock));
ndi_devi_set_hidden(dip); /* invisible: lookup/snapshot */
DEVI_SET_DEVICE_REMOVED(dip);
DEVI_SET_EVREMOVE(dip); /* this clears EVADD too */
mutex_exit(&(DEVI(dip)->devi_lock));
/* report remove (as 'removed') */
i_ndi_devi_report_status_change(dip, NULL);
/*
* Invalidate the cache to ensure accurate
* (di_state() & DI_DEVICE_REMOVED).
*/
i_ddi_di_cache_invalidate();
/*
* Generate sysevent for those interested in removal (either directly
* via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
*/
i_ddi_log_devfs_device_remove(dip);
return (1); /* DEVICE_REMOVED state changed */
}
int
ndi_devi_device_insert(dev_info_t *dip)
{
ASSERT(dip && ddi_get_parent(dip) &&
DEVI_BUSY_OWNED(ddi_get_parent(dip)));
/* Return if not marked removed. */
if (!ndi_devi_device_isremoved(dip))
return (0);
/* Mark the device as having been physically reinserted. */
mutex_enter(&(DEVI(dip)->devi_lock));
ndi_devi_clr_hidden(dip); /* visible: lookup/snapshot */
DEVI_SET_DEVICE_REINSERTED(dip);
DEVI_SET_EVADD(dip); /* this clears EVREMOVE too */
mutex_exit(&(DEVI(dip)->devi_lock));
/* report insert (as 'online') */
i_ndi_devi_report_status_change(dip, NULL);
/*
* Invalidate the cache to ensure accurate
* (di_state() & DI_DEVICE_REMOVED).
*/
i_ddi_di_cache_invalidate();
/*
* Generate sysevent for those interested in removal (either directly
* via EC_DEVFS or indirectly via devfsadmd generated EC_DEV).
*/
i_ddi_log_devfs_device_insert(dip);
return (1); /* DEVICE_REMOVED state changed */
}
/*
* ibt_hw_is_present() returns 0 when there is no IB hardware actively
* running. This is primarily useful for modules like rpcmod which
* needs a quick check to decide whether or not it should try to use
* InfiniBand
*/
int ib_hw_status = 0;
int
ibt_hw_is_present()
{
return (ib_hw_status);
}
/*
* ASSERT that constraint flag is not set and then set the "retire attempt"
* flag.
*/
int
e_ddi_mark_retiring(dev_info_t *dip, void *arg)
{
char **cons_array = (char **)arg;
char *path;
int constraint;
int i;
constraint = 0;
if (cons_array) {
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
for (i = 0; cons_array[i] != NULL; i++) {
if (strcmp(path, cons_array[i]) == 0) {
constraint = 1;
break;
}
}
kmem_free(path, MAXPATHLEN);
}
mutex_enter(&DEVI(dip)->devi_lock);
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
DEVI(dip)->devi_flags |= DEVI_RETIRING;
if (constraint)
DEVI(dip)->devi_flags |= DEVI_R_CONSTRAINT;
mutex_exit(&DEVI(dip)->devi_lock);
RIO_VERBOSE((CE_NOTE, "marked dip as undergoing retire process dip=%p",
(void *)dip));
if (constraint)
RIO_DEBUG((CE_NOTE, "marked dip as constrained, dip=%p",
(void *)dip));
if (MDI_PHCI(dip))
mdi_phci_mark_retiring(dip, cons_array);
return (DDI_WALK_CONTINUE);
}
static void
free_array(char **cons_array)
{
int i;
if (cons_array == NULL)
return;
for (i = 0; cons_array[i] != NULL; i++) {
kmem_free(cons_array[i], strlen(cons_array[i]) + 1);
}
kmem_free(cons_array, (i+1) * sizeof (char *));
}
/*
* Walk *every* node in subtree and check if it blocks, allows or has no
* comment on a proposed retire.
*/
int
e_ddi_retire_notify(dev_info_t *dip, void *arg)
{
int *constraint = (int *)arg;
RIO_DEBUG((CE_NOTE, "retire notify: dip = %p", (void *)dip));
(void) e_ddi_offline_notify(dip);
mutex_enter(&(DEVI(dip)->devi_lock));
if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
RIO_DEBUG((CE_WARN, "retire notify: dip in retire "
"subtree is not marked: dip = %p", (void *)dip));
*constraint = 0;
} else if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
RIO_DEBUG((CE_NOTE, "retire notify: BLOCKED: dip = %p",
(void *)dip));
*constraint = 0;
} else if (!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT)) {
RIO_DEBUG((CE_NOTE, "retire notify: NO CONSTRAINT: "
"dip = %p", (void *)dip));
*constraint = 0;
} else {
RIO_DEBUG((CE_NOTE, "retire notify: CONSTRAINT set: "
"dip = %p", (void *)dip));
}
mutex_exit(&DEVI(dip)->devi_lock);
if (MDI_PHCI(dip))
mdi_phci_retire_notify(dip, constraint);
return (DDI_WALK_CONTINUE);
}
int
e_ddi_retire_finalize(dev_info_t *dip, void *arg)
{
int constraint = *(int *)arg;
int finalize;
int phci_only;
ASSERT(DEVI_BUSY_OWNED(ddi_get_parent(dip)));
mutex_enter(&DEVI(dip)->devi_lock);
if (!(DEVI(dip)->devi_flags & DEVI_RETIRING)) {
RIO_DEBUG((CE_WARN,
"retire: unmarked dip(%p) in retire subtree",
(void *)dip));
ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRED));
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
mutex_exit(&DEVI(dip)->devi_lock);
return (DDI_WALK_CONTINUE);
}
/*
* retire the device if constraints have been applied
* or if the device is not in use
*/
finalize = 0;
if (constraint) {
ASSERT(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT);
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
DEVI(dip)->devi_flags |= DEVI_RETIRED;
mutex_exit(&DEVI(dip)->devi_lock);
(void) spec_fence_snode(dip, NULL);
RIO_DEBUG((CE_NOTE, "Fenced off: dip = %p", (void *)dip));
e_ddi_offline_finalize(dip, DDI_SUCCESS);
} else {
if (DEVI(dip)->devi_flags & DEVI_R_BLOCKED) {
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
DEVI(dip)->devi_flags &= ~DEVI_R_BLOCKED;
DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
/* we have already finalized during notify */
} else if (DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT) {
DEVI(dip)->devi_flags &= ~DEVI_R_CONSTRAINT;
DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
finalize = 1;
} else {
DEVI(dip)->devi_flags &= ~DEVI_RETIRING;
/*
* even if no contracts, need to call finalize
* to clear the contract barrier on the dip
*/
finalize = 1;
}
mutex_exit(&DEVI(dip)->devi_lock);
RIO_DEBUG((CE_NOTE, "finalize: NOT retired: dip = %p",
(void *)dip));
if (finalize)
e_ddi_offline_finalize(dip, DDI_FAILURE);
}
/*
* phci_only variable indicates no client checking, just
* offline the PHCI. We set that to 0 to enable client
* checking
*/
phci_only = 0;
if (MDI_PHCI(dip))
mdi_phci_retire_finalize(dip, phci_only);
return (DDI_WALK_CONTINUE);
}
/*
* Returns
* DDI_SUCCESS if constraints allow retire
* DDI_FAILURE if constraints don't allow retire.
* cons_array is a NULL terminated array of node paths for
* which constraints have already been applied.
*/
int
e_ddi_retire_device(char *path, char **cons_array)
{
dev_info_t *dip;
dev_info_t *pdip;
int circ;
int circ2;
int constraint;
char *devnm;
/*
* First, lookup the device
*/
dip = e_ddi_hold_devi_by_path(path, 0);
if (dip == NULL) {
/*
* device does not exist. This device cannot be
* a critical device since it is not in use. Thus
* this device is always retireable. Return DDI_SUCCESS
* to indicate this. If this device is ever
* instantiated, I/O framework will consult the
* the persistent retire store, mark it as
* retired and fence it off.
*/
RIO_DEBUG((CE_NOTE, "Retire device: device doesn't exist."
" NOP. Just returning SUCCESS. path=%s", path));
free_array(cons_array);
return (DDI_SUCCESS);
}
RIO_DEBUG((CE_NOTE, "Retire device: found dip = %p.", (void *)dip));
pdip = ddi_get_parent(dip);
ndi_hold_devi(pdip);
/*
* Run devfs_clean() in case dip has no constraints and is
* not in use, so is retireable but there are dv_nodes holding
* ref-count on the dip. Note that devfs_clean() always returns
* success.
*/
devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);
(void) ddi_deviname(dip, devnm);
(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);
kmem_free(devnm, MAXNAMELEN + 1);
ndi_devi_enter(pdip, &circ);
/* release hold from e_ddi_hold_devi_by_path */
ndi_rele_devi(dip);
/*
* If it cannot make a determination, is_leaf_node() assumes
* dip is a nexus.
*/
(void) e_ddi_mark_retiring(dip, cons_array);
if (!is_leaf_node(dip)) {
ndi_devi_enter(dip, &circ2);
ddi_walk_devs(ddi_get_child(dip), e_ddi_mark_retiring,
cons_array);
ndi_devi_exit(dip, circ2);
}
free_array(cons_array);
/*
* apply constraints
*/
RIO_DEBUG((CE_NOTE, "retire: subtree retire notify: path = %s", path));
constraint = 1; /* assume constraints allow retire */
(void) e_ddi_retire_notify(dip, &constraint);
if (!is_leaf_node(dip)) {
ndi_devi_enter(dip, &circ2);
ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_notify,
&constraint);
ndi_devi_exit(dip, circ2);
}
/*
* Now finalize the retire
*/
(void) e_ddi_retire_finalize(dip, &constraint);
if (!is_leaf_node(dip)) {
ndi_devi_enter(dip, &circ2);
ddi_walk_devs(ddi_get_child(dip), e_ddi_retire_finalize,
&constraint);
ndi_devi_exit(dip, circ2);
}
if (!constraint) {
RIO_DEBUG((CE_WARN, "retire failed: path = %s", path));
} else {
RIO_DEBUG((CE_NOTE, "retire succeeded: path = %s", path));
}
ndi_devi_exit(pdip, circ);
ndi_rele_devi(pdip);
return (constraint ? DDI_SUCCESS : DDI_FAILURE);
}
static int
unmark_and_unfence(dev_info_t *dip, void *arg)
{
char *path = (char *)arg;
ASSERT(path);
(void) ddi_pathname(dip, path);
mutex_enter(&DEVI(dip)->devi_lock);
DEVI(dip)->devi_flags &= ~DEVI_RETIRED;
DEVI_SET_DEVICE_ONLINE(dip);
mutex_exit(&DEVI(dip)->devi_lock);
RIO_VERBOSE((CE_NOTE, "Cleared RETIRED flag: dip=%p, path=%s",
(void *)dip, path));
(void) spec_unfence_snode(dip);
RIO_DEBUG((CE_NOTE, "Unfenced device: %s", path));
if (MDI_PHCI(dip))
mdi_phci_unretire(dip);
return (DDI_WALK_CONTINUE);
}
struct find_dip {
char *fd_buf;
char *fd_path;
dev_info_t *fd_dip;
};
static int
find_dip_fcn(dev_info_t *dip, void *arg)
{
struct find_dip *findp = (struct find_dip *)arg;
(void) ddi_pathname(dip, findp->fd_buf);
if (strcmp(findp->fd_path, findp->fd_buf) != 0)
return (DDI_WALK_CONTINUE);
ndi_hold_devi(dip);
findp->fd_dip = dip;
return (DDI_WALK_TERMINATE);
}
int
e_ddi_unretire_device(char *path)
{
int circ;
int circ2;
char *path2;
dev_info_t *pdip;
dev_info_t *dip;
struct find_dip find_dip;
ASSERT(path);
ASSERT(*path == '/');
if (strcmp(path, "/") == 0) {
cmn_err(CE_WARN, "Root node cannot be retired. Skipping "
"device unretire: %s", path);
return (0);
}
/*
* We can't lookup the dip (corresponding to path) via
* e_ddi_hold_devi_by_path() because the dip may be offline
* and may not attach. Use ddi_walk_devs() instead;
*/
find_dip.fd_buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
find_dip.fd_path = path;
find_dip.fd_dip = NULL;
pdip = ddi_root_node();
ndi_devi_enter(pdip, &circ);
ddi_walk_devs(ddi_get_child(pdip), find_dip_fcn, &find_dip);
ndi_devi_exit(pdip, circ);
kmem_free(find_dip.fd_buf, MAXPATHLEN);
if (find_dip.fd_dip == NULL) {
cmn_err(CE_WARN, "Device not found in device tree. Skipping "
"device unretire: %s", path);
return (0);
}
dip = find_dip.fd_dip;
pdip = ddi_get_parent(dip);
ndi_hold_devi(pdip);
ndi_devi_enter(pdip, &circ);
path2 = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) unmark_and_unfence(dip, path2);
if (!is_leaf_node(dip)) {
ndi_devi_enter(dip, &circ2);
ddi_walk_devs(ddi_get_child(dip), unmark_and_unfence, path2);
ndi_devi_exit(dip, circ2);
}
kmem_free(path2, MAXPATHLEN);
/* release hold from find_dip_fcn() */
ndi_rele_devi(dip);
ndi_devi_exit(pdip, circ);
ndi_rele_devi(pdip);
return (0);
}
/*
* Called before attach on a dip that has been retired.
*/
static int
mark_and_fence(dev_info_t *dip, void *arg)
{
char *fencepath = (char *)arg;
/*
* We have already decided to retire this device. The various
* constraint checking should not be set.
* NOTE that the retire flag may already be set due to
* fenced -> detach -> fenced transitions.
*/
mutex_enter(&DEVI(dip)->devi_lock);
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_CONSTRAINT));
ASSERT(!(DEVI(dip)->devi_flags & DEVI_R_BLOCKED));
ASSERT(!(DEVI(dip)->devi_flags & DEVI_RETIRING));
DEVI(dip)->devi_flags |= DEVI_RETIRED;
mutex_exit(&DEVI(dip)->devi_lock);
RIO_VERBOSE((CE_NOTE, "marked as RETIRED dip=%p", (void *)dip));
if (fencepath) {
(void) spec_fence_snode(dip, NULL);
RIO_DEBUG((CE_NOTE, "Fenced: %s",
ddi_pathname(dip, fencepath)));
}
return (DDI_WALK_CONTINUE);
}
/*
* Checks the retire database and:
*
* - if device is present in the retire database, marks the device retired
* and fences it off.
* - if device is not in retire database, allows the device to attach normally
*
* To be called only by framework attach code on first attach attempt.
*
*/
static void
i_ddi_check_retire(dev_info_t *dip)
{
char *path;
dev_info_t *pdip;
int circ;
int phci_only;
pdip = ddi_get_parent(dip);
/*
* Root dip is treated special and doesn't take this code path.
* Also root can never be retired.
*/
ASSERT(pdip);
ASSERT(DEVI_BUSY_OWNED(pdip));
ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
(void) ddi_pathname(dip, path);
RIO_VERBOSE((CE_NOTE, "Checking if dip should attach: dip=%p, path=%s",
(void *)dip, path));
/*
* Check if this device is in the "retired" store i.e. should
* be retired. If not, we have nothing to do.
*/
if (e_ddi_device_retired(path) == 0) {
RIO_VERBOSE((CE_NOTE, "device is NOT retired: path=%s", path));
kmem_free(path, MAXPATHLEN);
return;
}
RIO_DEBUG((CE_NOTE, "attach: device is retired: path=%s", path));
/*
* Mark dips and fence off snodes (if any)
*/
RIO_DEBUG((CE_NOTE, "attach: Mark and fence subtree: path=%s", path));
(void) mark_and_fence(dip, path);
if (!is_leaf_node(dip)) {
ndi_devi_enter(dip, &circ);
ddi_walk_devs(ddi_get_child(dip), mark_and_fence, path);
ndi_devi_exit(dip, circ);
}
kmem_free(path, MAXPATHLEN);
/*
* We don't want to check the client. We just want to
* offline the PHCI
*/
phci_only = 1;
if (MDI_PHCI(dip))
mdi_phci_retire_finalize(dip, phci_only);
}
void
ddi_err(ddi_err_t ade, dev_info_t *rdip, const char *fmt, ...)
{
va_list ap;
char strbuf[256];
char *buf;
size_t buflen, tlen;
int ce;
int de;
const char *fmtbad = "Invalid arguments to ddi_err()";
de = DER_CONT;
strbuf[1] = '\0';
switch (ade) {
case DER_CONS:
strbuf[0] = '^';
break;
case DER_LOG:
strbuf[0] = '!';
break;
case DER_VERB:
strbuf[0] = '?';
break;
default:
strbuf[0] = '\0';
de = ade;
break;
}
tlen = strlen(strbuf);
buf = strbuf + tlen;
buflen = sizeof (strbuf) - tlen;
if (rdip && ddi_get_instance(rdip) == -1) {
(void) snprintf(buf, buflen, "%s: ",
ddi_driver_name(rdip));
} else if (rdip) {
(void) snprintf(buf, buflen, "%s%d: ",
ddi_driver_name(rdip), ddi_get_instance(rdip));
}
tlen = strlen(strbuf);
buf = strbuf + tlen;
buflen = sizeof (strbuf) - tlen;
va_start(ap, fmt);
switch (de) {
case DER_CONT:
(void) vsnprintf(buf, buflen, fmt, ap);
if (ade != DER_CONT) {
(void) strlcat(strbuf, "\n", sizeof (strbuf));
}
ce = CE_CONT;
break;
case DER_NOTE:
(void) vsnprintf(buf, buflen, fmt, ap);
ce = CE_NOTE;
break;
case DER_WARN:
(void) vsnprintf(buf, buflen, fmt, ap);
ce = CE_WARN;
break;
case DER_MODE:
(void) vsnprintf(buf, buflen, fmt, ap);
if (ddi_err_panic == B_TRUE) {
ce = CE_PANIC;
} else {
ce = CE_WARN;
}
break;
case DER_DEBUG:
(void) snprintf(buf, buflen, "DEBUG: ");
tlen = strlen("DEBUG: ");
(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
ce = CE_CONT;
break;
case DER_PANIC:
(void) vsnprintf(buf, buflen, fmt, ap);
ce = CE_PANIC;
break;
case DER_INVALID:
default:
(void) snprintf(buf, buflen, fmtbad);
tlen = strlen(fmtbad);
(void) vsnprintf(buf + tlen, buflen - tlen, fmt, ap);
ce = CE_PANIC;
break;
}
va_end(ap);
cmn_err(ce, strbuf);
}
/*ARGSUSED*/
void
ddi_mem_update(uint64_t addr, uint64_t size)
{
#if defined(__x86) && !defined(__xpv)
extern void immu_physmem_update(uint64_t addr, uint64_t size);
immu_physmem_update(addr, size);
#else
/*LINTED*/
;
#endif
}
|