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
|
/*
* 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 (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Interfaces for getting device configuration data from kernel
* through the devinfo driver.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <stropts.h>
#include <fcntl.h>
#include <poll.h>
#include <synch.h>
#include <unistd.h>
#include <sys/mkdev.h>
#include <sys/obpdefs.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/autoconf.h>
#include <stdarg.h>
#include <sys/ddi_hp.h>
#define NDEBUG 1
#include <assert.h>
#include "libdevinfo.h"
/*
* Debug message levels
*/
typedef enum {
DI_QUIET = 0, /* No debug messages - the default */
DI_ERR = 1,
DI_INFO,
DI_TRACE,
DI_TRACE1,
DI_TRACE2
} di_debug_t;
int di_debug = DI_QUIET;
#define DPRINTF(args) { if (di_debug != DI_QUIET) dprint args; }
void dprint(di_debug_t msglevel, const char *fmt, ...);
#pragma init(_libdevinfo_init)
void
_libdevinfo_init()
{
char *debug_str = getenv("_LIBDEVINFO_DEBUG");
if (debug_str) {
errno = 0;
di_debug = atoi(debug_str);
if (errno || di_debug < DI_QUIET)
di_debug = DI_QUIET;
}
}
di_node_t
di_init(const char *phys_path, uint_t flag)
{
return (di_init_impl(phys_path, flag, NULL));
}
/*
* We use blocking_open() to guarantee access to the devinfo device, if open()
* is failing with EAGAIN.
*/
static int
blocking_open(const char *path, int oflag)
{
int fd;
while ((fd = open(path, oflag)) == -1 && errno == EAGAIN)
(void) poll(NULL, 0, 1 * MILLISEC);
return (fd);
}
/* private interface */
di_node_t
di_init_driver(const char *drv_name, uint_t flag)
{
int fd;
char driver[MAXPATHLEN];
/*
* Don't allow drv_name to exceed MAXPATHLEN - 1, or 1023,
* which should be sufficient for any sensible programmer.
*/
if ((drv_name == NULL) || (strlen(drv_name) >= MAXPATHLEN)) {
errno = EINVAL;
return (DI_NODE_NIL);
}
(void) strcpy(driver, drv_name);
/*
* open the devinfo driver
*/
if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo",
O_RDONLY)) == -1) {
DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n", errno));
return (DI_NODE_NIL);
}
if (ioctl(fd, DINFOLODRV, driver) != 0) {
DPRINTF((DI_ERR, "failed to load driver %s\n", driver));
(void) close(fd);
errno = ENXIO;
return (DI_NODE_NIL);
}
(void) close(fd);
/*
* Driver load succeeded, return a snapshot
*/
return (di_init("/", flag));
}
di_node_t
di_init_impl(const char *phys_path, uint_t flag,
struct di_priv_data *priv)
{
caddr_t pa;
int fd, map_size;
struct di_all *dap;
struct dinfo_io dinfo_io;
uint_t pageoffset = sysconf(_SC_PAGESIZE) - 1;
uint_t pagemask = ~pageoffset;
DPRINTF((DI_INFO, "di_init: taking a snapshot\n"));
/*
* Make sure there is no minor name in the path
* and the path do not start with /devices....
*/
if (strchr(phys_path, ':') ||
(strncmp(phys_path, "/devices", 8) == 0) ||
(strlen(phys_path) > MAXPATHLEN)) {
errno = EINVAL;
return (DI_NODE_NIL);
}
if (strlen(phys_path) == 0)
(void) sprintf(dinfo_io.root_path, "/");
else if (*phys_path != '/')
(void) snprintf(dinfo_io.root_path, sizeof (dinfo_io.root_path),
"/%s", phys_path);
else
(void) snprintf(dinfo_io.root_path, sizeof (dinfo_io.root_path),
"%s", phys_path);
/*
* If private data is requested, copy the format specification
*/
if (flag & DINFOPRIVDATA & 0xff) {
if (priv)
bcopy(priv, &dinfo_io.priv,
sizeof (struct di_priv_data));
else {
errno = EINVAL;
return (DI_NODE_NIL);
}
}
/*
* Attempt to open the devinfo driver. Make a second attempt at the
* read-only minor node if we don't have privileges to open the full
* version _and_ if we're not requesting operations that the read-only
* node can't perform. (Setgid processes would fail an access() test,
* of course.)
*/
if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo",
O_RDONLY)) == -1) {
if ((flag & DINFOFORCE) == DINFOFORCE ||
(flag & DINFOPRIVDATA) == DINFOPRIVDATA) {
/*
* We wanted to perform a privileged operation, but the
* privileged node isn't available. Don't modify errno
* on our way out (but display it if we're running with
* di_debug set).
*/
DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n",
errno));
return (DI_NODE_NIL);
}
if ((fd = blocking_open("/devices/pseudo/devinfo@0:devinfo,ro",
O_RDONLY)) == -1) {
DPRINTF((DI_ERR, "devinfo open failed: errno = %d\n",
errno));
return (DI_NODE_NIL);
}
}
/*
* Verify that there is no major conflict, i.e., we are indeed opening
* the devinfo driver.
*/
if (ioctl(fd, DINFOIDENT, NULL) != DI_MAGIC) {
DPRINTF((DI_ERR,
"driver ID failed; check for major conflict\n"));
(void) close(fd);
return (DI_NODE_NIL);
}
/*
* create snapshot
*/
if ((map_size = ioctl(fd, flag, &dinfo_io)) < 0) {
DPRINTF((DI_ERR, "devinfo ioctl failed with "
"error: %d\n", errno));
(void) close(fd);
return (DI_NODE_NIL);
} else if (map_size == 0) {
DPRINTF((DI_ERR, "%s not found\n", phys_path));
errno = ENXIO;
(void) close(fd);
return (DI_NODE_NIL);
}
/*
* copy snapshot to userland
*/
map_size = (map_size + pageoffset) & pagemask;
if ((pa = valloc(map_size)) == NULL) {
DPRINTF((DI_ERR, "valloc failed for snapshot\n"));
(void) close(fd);
return (DI_NODE_NIL);
}
if (ioctl(fd, DINFOUSRLD, pa) != map_size) {
DPRINTF((DI_ERR, "failed to copy snapshot to usrld\n"));
(void) close(fd);
free(pa);
errno = EFAULT;
return (DI_NODE_NIL);
}
(void) close(fd);
dap = DI_ALL(pa);
if (dap->version != DI_SNAPSHOT_VERSION) {
DPRINTF((DI_ERR, "wrong snapshot version "
"(expected=%d, actual=%d)\n",
DI_SNAPSHOT_VERSION, dap->version));
free(pa);
errno = ESTALE;
return (DI_NODE_NIL);
}
if (dap->top_devinfo == 0) { /* phys_path not found */
DPRINTF((DI_ERR, "%s not found\n", phys_path));
free(pa);
errno = EINVAL;
return (DI_NODE_NIL);
}
return (DI_NODE(pa + dap->top_devinfo));
}
void
di_fini(di_node_t root)
{
caddr_t pa; /* starting address of map */
DPRINTF((DI_INFO, "di_fini: freeing a snapshot\n"));
/*
* paranoid checking
*/
if (root == DI_NODE_NIL) {
DPRINTF((DI_ERR, "di_fini called with NIL arg\n"));
return;
}
/*
* The root contains its own offset--self.
* Subtracting it from root address, we get the starting addr.
* The map_size is stored at the beginning of snapshot.
* Once we have starting address and size, we can free().
*/
pa = (caddr_t)root - DI_NODE(root)->self;
free(pa);
}
di_node_t
di_parent_node(di_node_t node)
{
caddr_t pa; /* starting address of map */
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
DPRINTF((DI_TRACE, "Get parent of node %s\n", di_node_name(node)));
pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->parent) {
return (DI_NODE(pa + DI_NODE(node)->parent));
}
/*
* Deal with error condition:
* If parent doesn't exist and node is not the root,
* set errno to ENOTSUP. Otherwise, set errno to ENXIO.
*/
if (strcmp(DI_ALL(pa)->root_path, "/") != 0)
errno = ENOTSUP;
else
errno = ENXIO;
return (DI_NODE_NIL);
}
di_node_t
di_sibling_node(di_node_t node)
{
caddr_t pa; /* starting address of map */
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
DPRINTF((DI_TRACE, "Get sibling of node %s\n", di_node_name(node)));
pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->sibling) {
return (DI_NODE(pa + DI_NODE(node)->sibling));
}
/*
* Deal with error condition:
* Sibling doesn't exist, figure out if ioctl command
* has DINFOSUBTREE set. If it doesn't, set errno to
* ENOTSUP.
*/
if (!(DI_ALL(pa)->command & DINFOSUBTREE))
errno = ENOTSUP;
else
errno = ENXIO;
return (DI_NODE_NIL);
}
di_node_t
di_child_node(di_node_t node)
{
caddr_t pa; /* starting address of map */
DPRINTF((DI_TRACE, "Get child of node %s\n", di_node_name(node)));
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->child) {
return (DI_NODE(pa + DI_NODE(node)->child));
}
/*
* Deal with error condition:
* Child doesn't exist, figure out if DINFOSUBTREE is set.
* If it isn't, set errno to ENOTSUP.
*/
if (!(DI_ALL(pa)->command & DINFOSUBTREE))
errno = ENOTSUP;
else
errno = ENXIO;
return (DI_NODE_NIL);
}
di_node_t
di_drv_first_node(const char *drv_name, di_node_t root)
{
caddr_t pa; /* starting address of map */
int major, devcnt;
struct di_devnm *devnm;
DPRINTF((DI_INFO, "Get first node of driver %s\n", drv_name));
if (root == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
/*
* get major number of driver
*/
pa = (caddr_t)root - DI_NODE(root)->self;
devcnt = DI_ALL(pa)->devcnt;
devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
for (major = 0; major < devcnt; major++)
if (devnm[major].name && (strcmp(drv_name,
(char *)(pa + devnm[major].name)) == 0))
break;
if (major >= devcnt) {
errno = EINVAL;
return (DI_NODE_NIL);
}
if (!(devnm[major].head)) {
errno = ENXIO;
return (DI_NODE_NIL);
}
return (DI_NODE(pa + devnm[major].head));
}
di_node_t
di_drv_next_node(di_node_t node)
{
caddr_t pa; /* starting address of map */
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
DPRINTF((DI_TRACE, "next node on per driver list:"
" current=%s, driver=%s\n",
di_node_name(node), di_driver_name(node)));
if (DI_NODE(node)->next == (di_off_t)-1) {
errno = ENOTSUP;
return (DI_NODE_NIL);
}
pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->next == 0) {
errno = ENXIO;
return (DI_NODE_NIL);
}
return (DI_NODE(pa + DI_NODE(node)->next));
}
/*
* Internal library interfaces:
* node_list etc. for node walking
*/
struct node_list {
struct node_list *next;
di_node_t node;
};
static void
free_node_list(struct node_list **headp)
{
struct node_list *tmp;
while (*headp) {
tmp = *headp;
*headp = (*headp)->next;
free(tmp);
}
}
static void
append_node_list(struct node_list **headp, struct node_list *list)
{
struct node_list *tmp;
if (*headp == NULL) {
*headp = list;
return;
}
if (list == NULL) /* a minor optimization */
return;
tmp = *headp;
while (tmp->next)
tmp = tmp->next;
tmp->next = list;
}
static void
prepend_node_list(struct node_list **headp, struct node_list *list)
{
struct node_list *tmp;
if (list == NULL)
return;
tmp = *headp;
*headp = list;
if (tmp == NULL) /* a minor optimization */
return;
while (list->next)
list = list->next;
list->next = tmp;
}
/*
* returns 1 if node is a descendant of parent, 0 otherwise
*/
static int
is_descendant(di_node_t node, di_node_t parent)
{
/*
* DI_NODE_NIL is parent of root, so it is
* the parent of all nodes.
*/
if (parent == DI_NODE_NIL) {
return (1);
}
do {
node = di_parent_node(node);
} while ((node != DI_NODE_NIL) && (node != parent));
return (node != DI_NODE_NIL);
}
/*
* Insert list before the first node which is NOT a descendent of parent.
* This is needed to reproduce the exact walking order of link generators.
*/
static void
insert_node_list(struct node_list **headp, struct node_list *list,
di_node_t parent)
{
struct node_list *tmp, *tmp1;
if (list == NULL)
return;
tmp = *headp;
if (tmp == NULL) { /* a minor optimization */
*headp = list;
return;
}
if (!is_descendant(tmp->node, parent)) {
prepend_node_list(headp, list);
return;
}
/*
* Find first node which is not a descendant
*/
while (tmp->next && is_descendant(tmp->next->node, parent)) {
tmp = tmp->next;
}
tmp1 = tmp->next;
tmp->next = list;
append_node_list(headp, tmp1);
}
/*
* Get a linked list of handles of all children
*/
static struct node_list *
get_children(di_node_t node)
{
di_node_t child;
struct node_list *result, *tmp;
DPRINTF((DI_TRACE1, "Get children of node %s\n", di_node_name(node)));
if ((child = di_child_node(node)) == DI_NODE_NIL) {
return (NULL);
}
if ((result = malloc(sizeof (struct node_list))) == NULL) {
DPRINTF((DI_ERR, "malloc of node_list failed\n"));
return (NULL);
}
result->node = child;
tmp = result;
while ((child = di_sibling_node(tmp->node)) != DI_NODE_NIL) {
if ((tmp->next = malloc(sizeof (struct node_list))) == NULL) {
DPRINTF((DI_ERR, "malloc of node_list failed\n"));
free_node_list(&result);
return (NULL);
}
tmp = tmp->next;
tmp->node = child;
}
tmp->next = NULL;
return (result);
}
/*
* Internal library interface:
* Delete all siblings of the first node from the node_list, along with
* the first node itself.
*/
static void
prune_sib(struct node_list **headp)
{
di_node_t parent, curr_par, curr_gpar;
struct node_list *curr, *prev;
/*
* get handle to parent of first node
*/
if ((parent = di_parent_node((*headp)->node)) == DI_NODE_NIL) {
/*
* This must be the root of the snapshot, so can't
* have any siblings.
*
* XXX Put a check here just in case.
*/
if ((*headp)->next)
DPRINTF((DI_ERR, "Unexpected err in di_walk_node.\n"));
free(*headp);
*headp = NULL;
return;
}
/*
* To be complete, we should also delete the children
* of siblings that have already been visited.
* This happens for DI_WALK_SIBFIRST when the first node
* is NOT the first in the linked list of siblings.
*
* Hence, we compare parent with BOTH the parent and grandparent
* of nodes, and delete node is a match is found.
*/
prev = *headp;
curr = prev->next;
while (curr) {
if (((curr_par = di_parent_node(curr->node)) != DI_NODE_NIL) &&
((curr_par == parent) || ((curr_gpar =
di_parent_node(curr_par)) != DI_NODE_NIL) &&
(curr_gpar == parent))) {
/*
* match parent/grandparent: delete curr
*/
prev->next = curr->next;
free(curr);
curr = prev->next;
} else
curr = curr->next;
}
/*
* delete the first node
*/
curr = *headp;
*headp = curr->next;
free(curr);
}
/*
* Internal library function:
* Update node list based on action (return code from callback)
* and flag specifying walking behavior.
*/
static void
update_node_list(int action, uint_t flag, struct node_list **headp)
{
struct node_list *children, *tmp;
di_node_t parent = di_parent_node((*headp)->node);
switch (action) {
case DI_WALK_TERMINATE:
/*
* free the node list and be done
*/
children = NULL;
free_node_list(headp);
break;
case DI_WALK_PRUNESIB:
/*
* Get list of children and prune siblings
*/
children = get_children((*headp)->node);
prune_sib(headp);
break;
case DI_WALK_PRUNECHILD:
/*
* Set children to NULL and pop first node
*/
children = NULL;
tmp = *headp;
*headp = tmp->next;
free(tmp);
break;
case DI_WALK_CONTINUE:
default:
/*
* Get list of children and pop first node
*/
children = get_children((*headp)->node);
tmp = *headp;
*headp = tmp->next;
free(tmp);
break;
}
/*
* insert the list of children
*/
switch (flag) {
case DI_WALK_CLDFIRST:
prepend_node_list(headp, children);
break;
case DI_WALK_SIBFIRST:
append_node_list(headp, children);
break;
case DI_WALK_LINKGEN:
default:
insert_node_list(headp, children, parent);
break;
}
}
/*
* Internal library function:
* Invoke callback on one node and update the list of nodes to be walked
* based on the flag and return code.
*/
static void
walk_one_node(struct node_list **headp, uint_t flag, void *arg,
int (*callback)(di_node_t, void *))
{
DPRINTF((DI_TRACE, "Walking node %s\n", di_node_name((*headp)->node)));
update_node_list(callback((*headp)->node, arg),
flag & DI_WALK_MASK, headp);
}
int
di_walk_node(di_node_t root, uint_t flag, void *arg,
int (*node_callback)(di_node_t, void *))
{
struct node_list *head; /* node_list for tree walk */
if (root == NULL) {
errno = EINVAL;
return (-1);
}
if ((head = malloc(sizeof (struct node_list))) == NULL) {
DPRINTF((DI_ERR, "malloc of node_list failed\n"));
return (-1);
}
head->next = NULL;
head->node = root;
DPRINTF((DI_INFO, "Start node walking from node %s\n",
di_node_name(root)));
while (head != NULL)
walk_one_node(&head, flag, arg, node_callback);
return (0);
}
/*
* Internal library function:
* Invoke callback for each minor on the minor list of first node
* on node_list headp, and place children of first node on the list.
*
* This is similar to walk_one_node, except we only walk in child
* first mode.
*/
static void
walk_one_minor_list(struct node_list **headp, const char *desired_type,
uint_t flag, void *arg, int (*callback)(di_node_t, di_minor_t, void *))
{
int ddm_type;
int action = DI_WALK_CONTINUE;
char *node_type;
di_minor_t minor = DI_MINOR_NIL;
di_node_t node = (*headp)->node;
while ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL) {
ddm_type = di_minor_type(minor);
if ((ddm_type == DDM_ALIAS) && !(flag & DI_CHECK_ALIAS))
continue;
if ((ddm_type == DDM_INTERNAL_PATH) &&
!(flag & DI_CHECK_INTERNAL_PATH))
continue;
node_type = di_minor_nodetype(minor);
if ((desired_type != NULL) && ((node_type == NULL) ||
strncmp(desired_type, node_type, strlen(desired_type))
!= 0))
continue;
if ((action = callback(node, minor, arg)) ==
DI_WALK_TERMINATE) {
break;
}
}
update_node_list(action, DI_WALK_LINKGEN, headp);
}
int
di_walk_minor(di_node_t root, const char *minor_type, uint_t flag, void *arg,
int (*minor_callback)(di_node_t, di_minor_t, void *))
{
struct node_list *head; /* node_list for tree walk */
#ifdef DEBUG
char *devfspath = di_devfs_path(root);
DPRINTF((DI_INFO, "walking minor nodes under %s\n", devfspath));
di_devfs_path_free(devfspath);
#endif
if (root == NULL) {
errno = EINVAL;
return (-1);
}
if ((head = malloc(sizeof (struct node_list))) == NULL) {
DPRINTF((DI_ERR, "malloc of node_list failed\n"));
return (-1);
}
head->next = NULL;
head->node = root;
DPRINTF((DI_INFO, "Start minor walking from node %s\n",
di_node_name(root)));
while (head != NULL)
walk_one_minor_list(&head, minor_type, flag, arg,
minor_callback);
return (0);
}
/*
* generic node parameters
* Calling these routines always succeeds.
*/
char *
di_node_name(di_node_t node)
{
return ((caddr_t)node + DI_NODE(node)->node_name - DI_NODE(node)->self);
}
/* returns NULL ptr or a valid ptr to non-NULL string */
char *
di_bus_addr(di_node_t node)
{
caddr_t pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->address == 0)
return (NULL);
return ((char *)(pa + DI_NODE(node)->address));
}
char *
di_binding_name(di_node_t node)
{
caddr_t pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->bind_name == 0)
return (NULL);
return ((char *)(pa + DI_NODE(node)->bind_name));
}
int
di_compatible_names(di_node_t node, char **names)
{
char *c;
int len, size, entries = 0;
if (DI_NODE(node)->compat_names == 0) {
*names = NULL;
return (0);
}
*names = (caddr_t)node +
DI_NODE(node)->compat_names - DI_NODE(node)->self;
c = *names;
len = DI_NODE(node)->compat_length;
while (len > 0) {
entries++;
size = strlen(c) + 1;
len -= size;
c += size;
}
return (entries);
}
int
di_instance(di_node_t node)
{
return (DI_NODE(node)->instance);
}
/*
* XXX: emulate the return value of the old implementation
* using info from devi_node_class and devi_node_attributes.
*/
int
di_nodeid(di_node_t node)
{
if (DI_NODE(node)->node_class == DDI_NC_PROM)
return (DI_PROM_NODEID);
if (DI_NODE(node)->attributes & DDI_PERSISTENT)
return (DI_SID_NODEID);
return (DI_PSEUDO_NODEID);
}
uint_t
di_state(di_node_t node)
{
uint_t result = 0;
if (di_node_state(node) < DS_ATTACHED)
result |= DI_DRIVER_DETACHED;
if (DI_NODE(node)->state & DEVI_DEVICE_OFFLINE)
result |= DI_DEVICE_OFFLINE;
if (DI_NODE(node)->state & DEVI_DEVICE_DOWN)
result |= DI_DEVICE_DOWN;
if (DI_NODE(node)->state & DEVI_DEVICE_DEGRADED)
result |= DI_DEVICE_DEGRADED;
if (DI_NODE(node)->state & DEVI_DEVICE_REMOVED)
result |= DI_DEVICE_REMOVED;
if (DI_NODE(node)->state & DEVI_BUS_QUIESCED)
result |= DI_BUS_QUIESCED;
if (DI_NODE(node)->state & DEVI_BUS_DOWN)
result |= DI_BUS_DOWN;
return (result);
}
ddi_node_state_t
di_node_state(di_node_t node)
{
return (DI_NODE(node)->node_state);
}
uint_t
di_flags(di_node_t node)
{
return (DI_NODE(node)->flags);
}
uint_t
di_retired(di_node_t node)
{
return (di_flags(node) & DEVI_RETIRED);
}
ddi_devid_t
di_devid(di_node_t node)
{
if (DI_NODE(node)->devid == 0)
return (NULL);
return ((ddi_devid_t)((caddr_t)node +
DI_NODE(node)->devid - DI_NODE(node)->self));
}
int
di_driver_major(di_node_t node)
{
int major;
major = DI_NODE(node)->drv_major;
if (major < 0)
return (-1);
return (major);
}
char *
di_driver_name(di_node_t node)
{
int major;
caddr_t pa;
struct di_devnm *devnm;
major = DI_NODE(node)->drv_major;
if (major < 0)
return (NULL);
pa = (caddr_t)node - DI_NODE(node)->self;
devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
if (devnm[major].name)
return (pa + devnm[major].name);
else
return (NULL);
}
uint_t
di_driver_ops(di_node_t node)
{
int major;
caddr_t pa;
struct di_devnm *devnm;
major = DI_NODE(node)->drv_major;
if (major < 0)
return (0);
pa = (caddr_t)node - DI_NODE(node)->self;
devnm = DI_DEVNM(pa + DI_ALL(pa)->devnames);
return (devnm[major].ops);
}
/*
* Returns pointer to the allocated string, which must be freed by the caller.
*/
char *
di_devfs_path(di_node_t node)
{
caddr_t pa;
di_node_t parent;
int depth = 0, len = 0;
char *buf, *name[MAX_TREE_DEPTH], *addr[MAX_TREE_DEPTH];
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (NULL);
}
/*
* trace back to root, note the node_name & address
*/
while ((parent = di_parent_node(node)) != DI_NODE_NIL) {
name[depth] = di_node_name(node);
len += strlen(name[depth]) + 1; /* 1 for '/' */
if ((addr[depth] = di_bus_addr(node)) != NULL)
len += strlen(addr[depth]) + 1; /* 1 for '@' */
node = parent;
depth++;
}
/*
* get the path to the root of snapshot
*/
pa = (caddr_t)node - DI_NODE(node)->self;
name[depth] = DI_ALL(pa)->root_path;
len += strlen(name[depth]) + 1;
/*
* allocate buffer and assemble path
*/
if ((buf = malloc(len)) == NULL) {
return (NULL);
}
(void) strcpy(buf, name[depth]);
len = strlen(buf);
if (buf[len - 1] == '/')
len--; /* delete trailing '/' */
while (depth) {
depth--;
buf[len] = '/';
(void) strcpy(buf + len + 1, name[depth]);
len += strlen(name[depth]) + 1;
if (addr[depth] && addr[depth][0] != '\0') {
buf[len] = '@';
(void) strcpy(buf + len + 1, addr[depth]);
len += strlen(addr[depth]) + 1;
}
}
return (buf);
}
char *
di_devfs_minor_path(di_minor_t minor)
{
di_node_t node;
char *full_path, *name, *devfspath;
int full_path_len;
if (minor == DI_MINOR_NIL) {
errno = EINVAL;
return (NULL);
}
name = di_minor_name(minor);
node = di_minor_devinfo(minor);
devfspath = di_devfs_path(node);
if (devfspath == NULL)
return (NULL);
/* make the full path to the device minor node */
full_path_len = strlen(devfspath) + strlen(name) + 2;
full_path = (char *)calloc(1, full_path_len);
if (full_path != NULL)
(void) snprintf(full_path, full_path_len, "%s:%s",
devfspath, name);
di_devfs_path_free(devfspath);
return (full_path);
}
/*
* Produce a string representation of path to di_path_t (pathinfo node). This
* string is identical to di_devfs_path had the device been enumerated under
* the pHCI: it has a base path to pHCI, then uses node_name of client, and
* device unit-address of pathinfo node.
*/
char *
di_path_devfs_path(di_path_t path)
{
di_node_t phci_node;
char *phci_path, *path_name, *path_addr;
char *full_path;
int full_path_len;
if (path == DI_PATH_NIL) {
errno = EINVAL;
return (NULL);
}
/* get name@addr for path */
path_name = di_path_node_name(path);
path_addr = di_path_bus_addr(path);
if ((path_name == NULL) || (path_addr == NULL))
return (NULL);
/* base path to pHCI devinfo node */
phci_node = di_path_phci_node(path);
if (phci_node == NULL)
return (NULL);
phci_path = di_devfs_path(phci_node);
if (phci_path == NULL)
return (NULL);
/* make the full string representation of path */
full_path_len = strlen(phci_path) + 1 + strlen(path_name) +
1 + strlen(path_addr) + 1;
full_path = (char *)calloc(1, full_path_len);
if (full_path != NULL)
(void) snprintf(full_path, full_path_len, "%s/%s@%s",
phci_path, path_name, path_addr);
di_devfs_path_free(phci_path);
return (full_path);
}
char *
di_path_client_devfs_path(di_path_t path)
{
return (di_devfs_path(di_path_client_node(path)));
}
void
di_devfs_path_free(char *buf)
{
if (buf == NULL) {
DPRINTF((DI_ERR, "di_devfs_path_free NULL arg!\n"));
return;
}
free(buf);
}
/*
* Return 1 if name is a IEEE-1275 generic name. If new generic
* names are defined, they should be added to this table
*/
static int
is_generic(const char *name, int len)
{
const char **gp;
/* from IEEE-1275 recommended practices section 3 */
static const char *generic_names[] = {
"atm",
"disk",
"display",
"dma-controller",
"ethernet",
"fcs",
"fdc",
"fddi",
"fibre-channel",
"ide",
"interrupt-controller",
"isa",
"keyboard",
"memory",
"mouse",
"nvram",
"pc-card",
"pci",
"printer",
"rtc",
"sbus",
"scanner",
"scsi",
"serial",
"sound",
"ssa",
"tape",
"timer",
"token-ring",
"vme",
0
};
for (gp = generic_names; *gp; gp++) {
if ((strncmp(*gp, name, len) == 0) &&
(strlen(*gp) == len))
return (1);
}
return (0);
}
/*
* Determine if two paths below /devices refer to the same device, ignoring
* any generic .vs. non-generic 'name' issues in "[[/]name[@addr[:minor]]]*".
* Return 1 if the paths match.
*/
int
di_devfs_path_match(const char *dp1, const char *dp2)
{
const char *p1, *p2;
const char *ec1, *ec2;
const char *at1, *at2;
char nc;
int g1, g2;
/* progress through both strings */
for (p1 = dp1, p2 = dp2; (*p1 == *p2) && *p1; p1++, p2++) {
/* require match until the start of a component */
if (*p1 != '/')
continue;
/* advance p1 and p2 to start of 'name' in component */
nc = *(p1 + 1);
if ((nc == '\0') || (nc == '/'))
continue; /* skip trash */
p1++;
p2++;
/*
* Both p1 and p2 point to beginning of 'name' in component.
* Determine where current component ends: next '/' or '\0'.
*/
ec1 = strchr(p1, '/');
if (ec1 == NULL)
ec1 = p1 + strlen(p1);
ec2 = strchr(p2, '/');
if (ec2 == NULL)
ec2 = p2 + strlen(p2);
/* Determine where name ends based on whether '@' exists */
at1 = strchr(p1, '@');
at2 = strchr(p2, '@');
if (at1 && (at1 < ec1))
ec1 = at1;
if (at2 && (at2 < ec2))
ec2 = at2;
/*
* At this point p[12] point to beginning of name and
* ec[12] point to character past the end of name. Determine
* if the names are generic.
*/
g1 = is_generic(p1, ec1 - p1);
g2 = is_generic(p2, ec2 - p2);
if (g1 != g2) {
/*
* one generic and one non-generic
* skip past the names in the match.
*/
p1 = ec1;
p2 = ec2;
} else {
if (*p1 != *p2)
break;
}
}
return ((*p1 == *p2) ? 1 : 0);
}
/* minor data access */
di_minor_t
di_minor_next(di_node_t node, di_minor_t minor)
{
caddr_t pa;
/*
* paranoid error checking
*/
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_MINOR_NIL);
}
/*
* minor is not NIL
*/
if (minor != DI_MINOR_NIL) {
if (DI_MINOR(minor)->next != 0)
return ((di_minor_t)((void *)((caddr_t)minor -
DI_MINOR(minor)->self + DI_MINOR(minor)->next)));
else {
errno = ENXIO;
return (DI_MINOR_NIL);
}
}
/*
* minor is NIL-->caller asks for first minor node
*/
if (DI_NODE(node)->minor_data != 0) {
return (DI_MINOR((caddr_t)node - DI_NODE(node)->self +
DI_NODE(node)->minor_data));
}
/*
* no minor data-->check if snapshot includes minor data
* in order to set the correct errno
*/
pa = (caddr_t)node - DI_NODE(node)->self;
if (DINFOMINOR & DI_ALL(pa)->command)
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_MINOR_NIL);
}
/* private interface for dealing with alias minor link generation */
di_node_t
di_minor_devinfo(di_minor_t minor)
{
if (minor == DI_MINOR_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
return (DI_NODE((caddr_t)minor - DI_MINOR(minor)->self +
DI_MINOR(minor)->node));
}
ddi_minor_type
di_minor_type(di_minor_t minor)
{
return (DI_MINOR(minor)->type);
}
char *
di_minor_name(di_minor_t minor)
{
if (DI_MINOR(minor)->name == 0)
return (NULL);
return ((caddr_t)minor - DI_MINOR(minor)->self + DI_MINOR(minor)->name);
}
dev_t
di_minor_devt(di_minor_t minor)
{
return (makedev(DI_MINOR(minor)->dev_major,
DI_MINOR(minor)->dev_minor));
}
int
di_minor_spectype(di_minor_t minor)
{
return (DI_MINOR(minor)->spec_type);
}
char *
di_minor_nodetype(di_minor_t minor)
{
if (DI_MINOR(minor)->node_type == 0)
return (NULL);
return ((caddr_t)minor -
DI_MINOR(minor)->self + DI_MINOR(minor)->node_type);
}
/*
* Single public interface for accessing software properties
*/
di_prop_t
di_prop_next(di_node_t node, di_prop_t prop)
{
int list = DI_PROP_DRV_LIST;
/*
* paranoid check
*/
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_PROP_NIL);
}
/*
* Find which prop list we are at
*/
if (prop != DI_PROP_NIL)
list = DI_PROP(prop)->prop_list;
do {
switch (list++) {
case DI_PROP_DRV_LIST:
prop = di_prop_drv_next(node, prop);
break;
case DI_PROP_SYS_LIST:
prop = di_prop_sys_next(node, prop);
break;
case DI_PROP_GLB_LIST:
prop = di_prop_global_next(node, prop);
break;
case DI_PROP_HW_LIST:
prop = di_prop_hw_next(node, prop);
break;
default: /* shouldn't happen */
errno = EFAULT;
return (DI_PROP_NIL);
}
} while ((prop == DI_PROP_NIL) && (list <= DI_PROP_HW_LIST));
return (prop);
}
dev_t
di_prop_devt(di_prop_t prop)
{
return (makedev(DI_PROP(prop)->dev_major, DI_PROP(prop)->dev_minor));
}
char *
di_prop_name(di_prop_t prop)
{
if (DI_PROP(prop)->prop_name == 0)
return (NULL);
return ((caddr_t)prop - DI_PROP(prop)->self + DI_PROP(prop)->prop_name);
}
int
di_prop_type(di_prop_t prop)
{
uint_t flags = DI_PROP(prop)->prop_flags;
if (flags & DDI_PROP_UNDEF_IT)
return (DI_PROP_TYPE_UNDEF_IT);
if (DI_PROP(prop)->prop_len == 0)
return (DI_PROP_TYPE_BOOLEAN);
if ((flags & DDI_PROP_TYPE_MASK) == DDI_PROP_TYPE_ANY)
return (DI_PROP_TYPE_UNKNOWN);
if (flags & DDI_PROP_TYPE_INT)
return (DI_PROP_TYPE_INT);
if (flags & DDI_PROP_TYPE_INT64)
return (DI_PROP_TYPE_INT64);
if (flags & DDI_PROP_TYPE_STRING)
return (DI_PROP_TYPE_STRING);
if (flags & DDI_PROP_TYPE_BYTE)
return (DI_PROP_TYPE_BYTE);
/*
* Shouldn't get here. In case we do, return unknown type.
*
* XXX--When DDI_PROP_TYPE_COMPOSITE is implemented, we need
* to add DI_PROP_TYPE_COMPOSITE.
*/
DPRINTF((DI_ERR, "Unimplemented property type: 0x%x\n", flags));
return (DI_PROP_TYPE_UNKNOWN);
}
/*
* Extract type-specific values of an property
*/
extern int di_prop_decode_common(void *prop_data, int len,
int ddi_type, int prom);
int
di_prop_ints(di_prop_t prop, int **prop_data)
{
if (DI_PROP(prop)->prop_len == 0)
return (0); /* boolean property */
if ((DI_PROP(prop)->prop_data == 0) ||
(DI_PROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (int *)((void *)((caddr_t)prop - DI_PROP(prop)->self
+ DI_PROP(prop)->prop_data));
return (di_prop_decode_common((void *)prop_data,
DI_PROP(prop)->prop_len, DI_PROP_TYPE_INT, 0));
}
int
di_prop_int64(di_prop_t prop, int64_t **prop_data)
{
if (DI_PROP(prop)->prop_len == 0)
return (0); /* boolean property */
if ((DI_PROP(prop)->prop_data == 0) ||
(DI_PROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (int64_t *)((void *)((caddr_t)prop - DI_PROP(prop)->self
+ DI_PROP(prop)->prop_data));
return (di_prop_decode_common((void *)prop_data,
DI_PROP(prop)->prop_len, DI_PROP_TYPE_INT64, 0));
}
int
di_prop_strings(di_prop_t prop, char **prop_data)
{
if (DI_PROP(prop)->prop_len == 0)
return (0); /* boolean property */
if ((DI_PROP(prop)->prop_data == 0) ||
(DI_PROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (char *)((caddr_t)prop - DI_PROP(prop)->self
+ DI_PROP(prop)->prop_data);
return (di_prop_decode_common((void *)prop_data,
DI_PROP(prop)->prop_len, DI_PROP_TYPE_STRING, 0));
}
int
di_prop_bytes(di_prop_t prop, uchar_t **prop_data)
{
if (DI_PROP(prop)->prop_len == 0)
return (0); /* boolean property */
if ((DI_PROP(prop)->prop_data == 0) ||
(DI_PROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (uchar_t *)((caddr_t)prop - DI_PROP(prop)->self
+ DI_PROP(prop)->prop_data);
return (di_prop_decode_common((void *)prop_data,
DI_PROP(prop)->prop_len, DI_PROP_TYPE_BYTE, 0));
}
/*
* returns 1 for match, 0 for no match
*/
static int
match_prop(di_prop_t prop, dev_t match_dev, const char *name, int type)
{
int prop_type;
#ifdef DEBUG
if (di_prop_name(prop) == NULL) {
DPRINTF((DI_ERR, "libdevinfo: property has no name!\n"));
return (0);
}
#endif /* DEBUG */
if (strcmp(name, di_prop_name(prop)) != 0)
return (0);
if ((match_dev != DDI_DEV_T_ANY) && (di_prop_devt(prop) != match_dev))
return (0);
/*
* XXX prop_type is different from DDI_*. See PSARC 1997/127.
*/
prop_type = di_prop_type(prop);
if ((prop_type != DI_PROP_TYPE_UNKNOWN) && (prop_type != type) &&
(prop_type != DI_PROP_TYPE_BOOLEAN))
return (0);
return (1);
}
static di_prop_t
di_prop_search(dev_t match_dev, di_node_t node, const char *name,
int type)
{
di_prop_t prop = DI_PROP_NIL;
/*
* The check on match_dev follows ddi_prop_lookup_common().
* Other checks are libdevinfo specific implementation.
*/
if ((node == DI_NODE_NIL) || (name == NULL) || (strlen(name) == 0) ||
(match_dev == DDI_DEV_T_NONE) || !DI_PROP_TYPE_VALID(type)) {
errno = EINVAL;
return (DI_PROP_NIL);
}
while ((prop = di_prop_next(node, prop)) != DI_PROP_NIL) {
DPRINTF((DI_TRACE1, "match prop name %s, devt 0x%lx, type %d\n",
di_prop_name(prop), di_prop_devt(prop),
di_prop_type(prop)));
if (match_prop(prop, match_dev, name, type))
return (prop);
}
return (DI_PROP_NIL);
}
di_prop_t
di_prop_find(dev_t match_dev, di_node_t node, const char *name)
{
di_prop_t prop = DI_PROP_NIL;
if ((node == DI_NODE_NIL) || (name == NULL) || (strlen(name) == 0) ||
(match_dev == DDI_DEV_T_NONE)) {
errno = EINVAL;
return (DI_PROP_NIL);
}
while ((prop = di_prop_next(node, prop)) != DI_PROP_NIL) {
DPRINTF((DI_TRACE1, "found prop name %s, devt 0x%lx, type %d\n",
di_prop_name(prop), di_prop_devt(prop),
di_prop_type(prop)));
if (strcmp(name, di_prop_name(prop)) == 0 &&
(match_dev == DDI_DEV_T_ANY ||
di_prop_devt(prop) == match_dev))
return (prop);
}
return (DI_PROP_NIL);
}
int
di_prop_lookup_ints(dev_t dev, di_node_t node, const char *prop_name,
int **prop_data)
{
di_prop_t prop;
if ((prop = di_prop_search(dev, node, prop_name,
DI_PROP_TYPE_INT)) == DI_PROP_NIL)
return (-1);
return (di_prop_ints(prop, (void *)prop_data));
}
int
di_prop_lookup_int64(dev_t dev, di_node_t node, const char *prop_name,
int64_t **prop_data)
{
di_prop_t prop;
if ((prop = di_prop_search(dev, node, prop_name,
DI_PROP_TYPE_INT64)) == DI_PROP_NIL)
return (-1);
return (di_prop_int64(prop, (void *)prop_data));
}
int
di_prop_lookup_strings(dev_t dev, di_node_t node, const char *prop_name,
char **prop_data)
{
di_prop_t prop;
if ((prop = di_prop_search(dev, node, prop_name,
DI_PROP_TYPE_STRING)) == DI_PROP_NIL)
return (-1);
return (di_prop_strings(prop, (void *)prop_data));
}
int
di_prop_lookup_bytes(dev_t dev, di_node_t node, const char *prop_name,
uchar_t **prop_data)
{
di_prop_t prop;
if ((prop = di_prop_search(dev, node, prop_name,
DI_PROP_TYPE_BYTE)) == DI_PROP_NIL)
return (-1);
return (di_prop_bytes(prop, (void *)prop_data));
}
/*
* Consolidation private property access functions
*/
enum prop_type {
PROP_TYPE_DRV,
PROP_TYPE_SYS,
PROP_TYPE_GLOB,
PROP_TYPE_HW
};
static di_prop_t
di_prop_next_common(di_node_t node, di_prop_t prop, int prop_type)
{
caddr_t pa;
di_off_t prop_off = 0;
if (prop != DI_PROP_NIL) {
if (DI_PROP(prop)->next) {
return (DI_PROP((caddr_t)prop -
DI_PROP(prop)->self + DI_PROP(prop)->next));
} else {
return (DI_PROP_NIL);
}
}
/*
* prop is NIL, caller asks for first property
*/
pa = (caddr_t)node - DI_NODE(node)->self;
switch (prop_type) {
case PROP_TYPE_DRV:
prop_off = DI_NODE(node)->drv_prop;
break;
case PROP_TYPE_SYS:
prop_off = DI_NODE(node)->sys_prop;
break;
case PROP_TYPE_HW:
prop_off = DI_NODE(node)->hw_prop;
break;
case PROP_TYPE_GLOB:
prop_off = DI_NODE(node)->glob_prop;
if (prop_off == -1) {
/* no global property */
prop_off = 0;
} else if ((prop_off == 0) && (DI_NODE(node)->drv_major >= 0)) {
/* refer to devnames array */
struct di_devnm *devnm = DI_DEVNM(pa +
DI_ALL(pa)->devnames + (DI_NODE(node)->drv_major *
sizeof (struct di_devnm)));
prop_off = devnm->global_prop;
}
break;
}
if (prop_off) {
return (DI_PROP(pa + prop_off));
}
/*
* no prop found. Check the reason for not found
*/
if (DINFOPROP & DI_ALL(pa)->command)
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_PROP_NIL);
}
di_prop_t
di_prop_drv_next(di_node_t node, di_prop_t prop)
{
return (di_prop_next_common(node, prop, PROP_TYPE_DRV));
}
di_prop_t
di_prop_sys_next(di_node_t node, di_prop_t prop)
{
return (di_prop_next_common(node, prop, PROP_TYPE_SYS));
}
di_prop_t
di_prop_global_next(di_node_t node, di_prop_t prop)
{
return (di_prop_next_common(node, prop, PROP_TYPE_GLOB));
}
di_prop_t
di_prop_hw_next(di_node_t node, di_prop_t prop)
{
return (di_prop_next_common(node, prop, PROP_TYPE_HW));
}
int
di_prop_rawdata(di_prop_t prop, uchar_t **prop_data)
{
#ifdef DEBUG
if (prop == DI_PROP_NIL) {
errno = EINVAL;
return (-1);
}
#endif /* DEBUG */
if (DI_PROP(prop)->prop_len == 0) {
*prop_data = NULL;
return (0);
}
if ((DI_PROP(prop)->prop_data == 0) ||
(DI_PROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
/*
* No memory allocation.
*/
*prop_data = (uchar_t *)((caddr_t)prop - DI_PROP(prop)->self +
DI_PROP(prop)->prop_data);
return (DI_PROP(prop)->prop_len);
}
/*
* Consolidation private interfaces for accessing I/O multipathing data
*/
di_path_t
di_path_phci_next_path(di_node_t node, di_path_t path)
{
caddr_t pa;
/*
* path is not NIL
*/
if (path != DI_PATH_NIL) {
if (DI_PATH(path)->path_p_link != 0)
return (DI_PATH((void *)((caddr_t)path -
DI_PATH(path)->self + DI_PATH(path)->path_p_link)));
else {
errno = ENXIO;
return (DI_PATH_NIL);
}
}
/*
* Path is NIL; the caller is asking for the first path info node
*/
if (DI_NODE(node)->multipath_phci != 0) {
DPRINTF((DI_INFO, "phci_next_path: returning %p\n",
((caddr_t)node -
DI_NODE(node)->self + DI_NODE(node)->multipath_phci)));
return (DI_PATH((caddr_t)node - DI_NODE(node)->self +
DI_NODE(node)->multipath_phci));
}
/*
* No pathing data; check if the snapshot includes path data in order
* to set errno properly.
*/
pa = (caddr_t)node - DI_NODE(node)->self;
if (DINFOPATH & (DI_ALL(pa)->command))
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_PATH_NIL);
}
di_path_t
di_path_client_next_path(di_node_t node, di_path_t path)
{
caddr_t pa;
/*
* path is not NIL
*/
if (path != DI_PATH_NIL) {
if (DI_PATH(path)->path_c_link != 0)
return (DI_PATH((caddr_t)path - DI_PATH(path)->self
+ DI_PATH(path)->path_c_link));
else {
errno = ENXIO;
return (DI_PATH_NIL);
}
}
/*
* Path is NIL; the caller is asking for the first path info node
*/
if (DI_NODE(node)->multipath_client != 0) {
DPRINTF((DI_INFO, "client_next_path: returning %p\n",
((caddr_t)node -
DI_NODE(node)->self + DI_NODE(node)->multipath_client)));
return (DI_PATH((caddr_t)node - DI_NODE(node)->self +
DI_NODE(node)->multipath_client));
}
/*
* No pathing data; check if the snapshot includes path data in order
* to set errno properly.
*/
pa = (caddr_t)node - DI_NODE(node)->self;
if (DINFOPATH & (DI_ALL(pa)->command))
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_PATH_NIL);
}
/*
* XXX Remove the private di_path_(addr,next,next_phci,next_client) interfaces
* below after NWS consolidation switches to using di_path_bus_addr,
* di_path_phci_next_path, and di_path_client_next_path per CR6638521.
*/
char *
di_path_addr(di_path_t path, char *buf)
{
caddr_t pa; /* starting address of map */
pa = (caddr_t)path - DI_PATH(path)->self;
(void) strncpy(buf, (char *)(pa + DI_PATH(path)->path_addr),
MAXPATHLEN);
return (buf);
}
di_path_t
di_path_next(di_node_t node, di_path_t path)
{
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_PATH_NIL);
}
if (DI_NODE(node)->multipath_client) {
return (di_path_client_next_path(node, path));
} else if (DI_NODE(node)->multipath_phci) {
return (di_path_phci_next_path(node, path));
} else {
/*
* The node had multipathing data but didn't appear to be a
* phci *or* a client; probably a programmer error.
*/
errno = EINVAL;
return (DI_PATH_NIL);
}
}
di_path_t
di_path_next_phci(di_node_t node, di_path_t path)
{
return (di_path_client_next_path(node, path));
}
di_path_t
di_path_next_client(di_node_t node, di_path_t path)
{
return (di_path_phci_next_path(node, path));
}
di_path_state_t
di_path_state(di_path_t path)
{
return ((di_path_state_t)DI_PATH(path)->path_state);
}
uint_t
di_path_flags(di_path_t path)
{
return (DI_PATH(path)->path_flags);
}
char *
di_path_node_name(di_path_t path)
{
di_node_t client_node;
/* pathinfo gets node_name from client */
if ((client_node = di_path_client_node(path)) == NULL)
return (NULL);
return (di_node_name(client_node));
}
char *
di_path_bus_addr(di_path_t path)
{
caddr_t pa = (caddr_t)path - DI_PATH(path)->self;
if (DI_PATH(path)->path_addr == 0)
return (NULL);
return ((char *)(pa + DI_PATH(path)->path_addr));
}
int
di_path_instance(di_path_t path)
{
return (DI_PATH(path)->path_instance);
}
di_node_t
di_path_client_node(di_path_t path)
{
caddr_t pa; /* starting address of map */
if (path == DI_PATH_NIL) {
errno = EINVAL;
return (DI_PATH_NIL);
}
DPRINTF((DI_TRACE, "Get client node for path %p\n", path));
pa = (caddr_t)path - DI_PATH(path)->self;
if (DI_PATH(path)->path_client) {
return (DI_NODE(pa + DI_PATH(path)->path_client));
}
/*
* Deal with error condition:
* If parent doesn't exist and node is not the root,
* set errno to ENOTSUP. Otherwise, set errno to ENXIO.
*/
if ((DI_PATH(path)->path_snap_state & DI_PATH_SNAP_NOCLIENT) == 0)
errno = ENOTSUP;
else
errno = ENXIO;
return (DI_NODE_NIL);
}
di_node_t
di_path_phci_node(di_path_t path)
{
caddr_t pa; /* starting address of map */
if (path == DI_PATH_NIL) {
errno = EINVAL;
return (DI_PATH_NIL);
}
DPRINTF((DI_TRACE, "Get phci node for path %p\n", path));
pa = (caddr_t)path - DI_PATH(path)->self;
if (DI_PATH(path)->path_phci) {
return (DI_NODE(pa + DI_PATH(path)->path_phci));
}
/*
* Deal with error condition:
* If parent doesn't exist and node is not the root,
* set errno to ENOTSUP. Otherwise, set errno to ENXIO.
*/
if ((DI_PATH(path)->path_snap_state & DI_PATH_SNAP_NOPHCI) == 0)
errno = ENOTSUP;
else
errno = ENXIO;
return (DI_NODE_NIL);
}
di_path_prop_t
di_path_prop_next(di_path_t path, di_path_prop_t prop)
{
caddr_t pa;
if (path == DI_PATH_NIL) {
errno = EINVAL;
return (DI_PROP_NIL);
}
/*
* prop is not NIL
*/
if (prop != DI_PROP_NIL) {
if (DI_PROP(prop)->next != 0)
return (DI_PATHPROP((caddr_t)prop -
DI_PROP(prop)->self + DI_PROP(prop)->next));
else {
errno = ENXIO;
return (DI_PROP_NIL);
}
}
/*
* prop is NIL-->caller asks for first property
*/
pa = (caddr_t)path - DI_PATH(path)->self;
if (DI_PATH(path)->path_prop != 0) {
return (DI_PATHPROP(pa + DI_PATH(path)->path_prop));
}
/*
* no property data-->check if snapshot includes props
* in order to set the correct errno
*/
if (DINFOPROP & (DI_ALL(pa)->command))
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_PROP_NIL);
}
char *
di_path_prop_name(di_path_prop_t prop)
{
caddr_t pa; /* starting address of map */
pa = (caddr_t)prop - DI_PATHPROP(prop)->self;
return ((char *)(pa + DI_PATHPROP(prop)->prop_name));
}
int
di_path_prop_len(di_path_prop_t prop)
{
return (DI_PATHPROP(prop)->prop_len);
}
int
di_path_prop_type(di_path_prop_t prop)
{
switch (DI_PATHPROP(prop)->prop_type) {
case DDI_PROP_TYPE_INT:
return (DI_PROP_TYPE_INT);
case DDI_PROP_TYPE_INT64:
return (DI_PROP_TYPE_INT64);
case DDI_PROP_TYPE_BYTE:
return (DI_PROP_TYPE_BYTE);
case DDI_PROP_TYPE_STRING:
return (DI_PROP_TYPE_STRING);
}
return (DI_PROP_TYPE_UNKNOWN);
}
int
di_path_prop_bytes(di_path_prop_t prop, uchar_t **prop_data)
{
if ((DI_PATHPROP(prop)->prop_data == 0) ||
(DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (uchar_t *)((caddr_t)prop - DI_PATHPROP(prop)->self
+ DI_PATHPROP(prop)->prop_data);
return (di_prop_decode_common((void *)prop_data,
DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_BYTE, 0));
}
int
di_path_prop_ints(di_path_prop_t prop, int **prop_data)
{
if (DI_PATHPROP(prop)->prop_len == 0)
return (0);
if ((DI_PATHPROP(prop)->prop_data == 0) ||
(DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (int *)((void *)((caddr_t)prop - DI_PATHPROP(prop)->self
+ DI_PATHPROP(prop)->prop_data));
return (di_prop_decode_common((void *)prop_data,
DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_INT, 0));
}
int
di_path_prop_int64s(di_path_prop_t prop, int64_t **prop_data)
{
if (DI_PATHPROP(prop)->prop_len == 0)
return (0);
if ((DI_PATHPROP(prop)->prop_data == 0) ||
(DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (int64_t *)((void *)((caddr_t)prop -
DI_PATHPROP(prop)->self + DI_PATHPROP(prop)->prop_data));
return (di_prop_decode_common((void *)prop_data,
DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_INT64, 0));
}
int
di_path_prop_strings(di_path_prop_t prop, char **prop_data)
{
if (DI_PATHPROP(prop)->prop_len == 0)
return (0);
if ((DI_PATHPROP(prop)->prop_data == 0) ||
(DI_PATHPROP(prop)->prop_data == (di_off_t)-1)) {
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
*prop_data = (char *)((caddr_t)prop - DI_PATHPROP(prop)->self
+ DI_PATHPROP(prop)->prop_data);
return (di_prop_decode_common((void *)prop_data,
DI_PATHPROP(prop)->prop_len, DI_PROP_TYPE_STRING, 0));
}
static di_path_prop_t
di_path_prop_search(di_path_t path, const char *name, int type)
{
di_path_prop_t prop = DI_PROP_NIL;
/*
* Sanity check arguments
*/
if ((path == DI_PATH_NIL) || (name == NULL) || (strlen(name) == 0) ||
!DI_PROP_TYPE_VALID(type)) {
errno = EINVAL;
return (DI_PROP_NIL);
}
while ((prop = di_path_prop_next(path, prop)) != DI_PROP_NIL) {
int prop_type = di_path_prop_type(prop);
DPRINTF((DI_TRACE1, "match path prop name %s, type %d\n",
di_path_prop_name(prop), prop_type));
if (strcmp(name, di_path_prop_name(prop)) != 0)
continue;
if ((prop_type != DI_PROP_TYPE_UNKNOWN) && (prop_type != type))
continue;
return (prop);
}
return (DI_PROP_NIL);
}
int
di_path_prop_lookup_bytes(di_path_t path, const char *prop_name,
uchar_t **prop_data)
{
di_path_prop_t prop;
if ((prop = di_path_prop_search(path, prop_name,
DI_PROP_TYPE_BYTE)) == DI_PROP_NIL)
return (-1);
return (di_path_prop_bytes(prop, prop_data));
}
int
di_path_prop_lookup_ints(di_path_t path, const char *prop_name,
int **prop_data)
{
di_path_prop_t prop;
if ((prop = di_path_prop_search(path, prop_name,
DI_PROP_TYPE_INT)) == DI_PROP_NIL)
return (-1);
return (di_path_prop_ints(prop, prop_data));
}
int
di_path_prop_lookup_int64s(di_path_t path, const char *prop_name,
int64_t **prop_data)
{
di_path_prop_t prop;
if ((prop = di_path_prop_search(path, prop_name,
DI_PROP_TYPE_INT64)) == DI_PROP_NIL)
return (-1);
return (di_path_prop_int64s(prop, prop_data));
}
int di_path_prop_lookup_strings(di_path_t path, const char *prop_name,
char **prop_data)
{
di_path_prop_t prop;
if ((prop = di_path_prop_search(path, prop_name,
DI_PROP_TYPE_STRING)) == DI_PROP_NIL)
return (-1);
return (di_path_prop_strings(prop, prop_data));
}
/*
* Consolidation private interfaces for traversing vhci nodes.
*/
di_node_t
di_vhci_first_node(di_node_t root)
{
struct di_all *dap;
caddr_t pa; /* starting address of map */
DPRINTF((DI_INFO, "Get first vhci node\n"));
if (root == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
pa = (caddr_t)root - DI_NODE(root)->self;
dap = DI_ALL(pa);
if (dap->top_vhci_devinfo == 0) {
errno = ENXIO;
return (DI_NODE_NIL);
}
return (DI_NODE(pa + dap->top_vhci_devinfo));
}
di_node_t
di_vhci_next_node(di_node_t node)
{
caddr_t pa; /* starting address of map */
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
DPRINTF((DI_TRACE, "next vhci node on the snap shot:"
" current=%s\n", di_node_name(node)));
if (DI_NODE(node)->next_vhci == 0) {
errno = ENXIO;
return (DI_NODE_NIL);
}
pa = (caddr_t)node - DI_NODE(node)->self;
return (DI_NODE(pa + DI_NODE(node)->next_vhci));
}
/*
* Consolidation private interfaces for traversing phci nodes.
*/
di_node_t
di_phci_first_node(di_node_t vhci_node)
{
caddr_t pa; /* starting address of map */
DPRINTF((DI_INFO, "Get first phci node:\n"
" current=%s", di_node_name(vhci_node)));
if (vhci_node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
pa = (caddr_t)vhci_node - DI_NODE(vhci_node)->self;
if (DI_NODE(vhci_node)->top_phci == 0) {
errno = ENXIO;
return (DI_NODE_NIL);
}
return (DI_NODE(pa + DI_NODE(vhci_node)->top_phci));
}
di_node_t
di_phci_next_node(di_node_t node)
{
caddr_t pa; /* starting address of map */
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
DPRINTF((DI_TRACE, "next phci node on the snap shot:"
" current=%s\n", di_node_name(node)));
if (DI_NODE(node)->next_phci == 0) {
errno = ENXIO;
return (DI_NODE_NIL);
}
pa = (caddr_t)node - DI_NODE(node)->self;
return (DI_NODE(pa + DI_NODE(node)->next_phci));
}
/*
* Consolidation private interfaces for private data
*/
void *
di_parent_private_data(di_node_t node)
{
caddr_t pa;
if (DI_NODE(node)->parent_data == 0) {
errno = ENXIO;
return (NULL);
}
if (DI_NODE(node)->parent_data == (di_off_t)-1) {
/*
* Private data requested, but not obtained due to a memory
* error (e.g. wrong format specified)
*/
errno = EFAULT;
return (NULL);
}
pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->parent_data)
return (pa + DI_NODE(node)->parent_data);
if (DI_ALL(pa)->command & DINFOPRIVDATA)
errno = ENXIO;
else
errno = ENOTSUP;
return (NULL);
}
void *
di_driver_private_data(di_node_t node)
{
caddr_t pa;
if (DI_NODE(node)->driver_data == 0) {
errno = ENXIO;
return (NULL);
}
if (DI_NODE(node)->driver_data == (di_off_t)-1) {
/*
* Private data requested, but not obtained due to a memory
* error (e.g. wrong format specified)
*/
errno = EFAULT;
return (NULL);
}
pa = (caddr_t)node - DI_NODE(node)->self;
if (DI_NODE(node)->driver_data)
return (pa + DI_NODE(node)->driver_data);
if (DI_ALL(pa)->command & DINFOPRIVDATA)
errno = ENXIO;
else
errno = ENOTSUP;
return (NULL);
}
/*
* Hotplug information access
*/
typedef struct {
void *arg;
const char *type;
uint_t flag;
int (*hp_callback)(di_node_t, di_hp_t, void *);
} di_walk_hp_arg_t;
static int
di_walk_hp_callback(di_node_t node, void *argp)
{
di_walk_hp_arg_t *arg = (di_walk_hp_arg_t *)argp;
di_hp_t hp;
char *type_str;
for (hp = DI_HP_NIL; (hp = di_hp_next(node, hp)) != DI_HP_NIL; ) {
/* Exclude non-matching types if a type filter is specified */
if (arg->type != NULL) {
type_str = di_hp_description(hp);
if (type_str && (strcmp(arg->type, type_str) != 0))
continue;
}
/* Exclude ports if DI_HP_PORT flag not specified */
if (!(arg->flag & DI_HP_PORT) &&
(di_hp_type(hp) == DDI_HP_CN_TYPE_VIRTUAL_PORT))
continue;
/* Exclude connectors if DI_HP_CONNECTOR flag not specified */
if (!(arg->flag & DI_HP_CONNECTOR) &&
!(di_hp_type(hp) == DDI_HP_CN_TYPE_VIRTUAL_PORT))
continue;
/* Perform callback */
if (arg->hp_callback(node, hp, arg->arg) != DI_WALK_CONTINUE)
return (DI_WALK_TERMINATE);
}
return (DI_WALK_CONTINUE);
}
int
di_walk_hp(di_node_t node, const char *type, uint_t flag, void *arg,
int (*hp_callback)(di_node_t node, di_hp_t hp, void *arg))
{
di_walk_hp_arg_t walk_arg;
caddr_t pa;
#ifdef DEBUG
char *devfspath = di_devfs_path(node);
DPRINTF((DI_INFO, "walking hotplug nodes under %s\n", devfspath));
di_devfs_path_free(devfspath);
#endif
/*
* paranoid error checking
*/
if ((node == DI_NODE_NIL) || (hp_callback == NULL)) {
errno = EINVAL;
return (-1);
}
/* check if hotplug data is included in snapshot */
pa = (caddr_t)node - DI_NODE(node)->self;
if (!(DI_ALL(pa)->command & DINFOHP)) {
errno = ENOTSUP;
return (-1);
}
walk_arg.arg = arg;
walk_arg.type = type;
walk_arg.flag = flag;
walk_arg.hp_callback = hp_callback;
return (di_walk_node(node, DI_WALK_CLDFIRST, &walk_arg,
di_walk_hp_callback));
}
di_hp_t
di_hp_next(di_node_t node, di_hp_t hp)
{
caddr_t pa;
/*
* paranoid error checking
*/
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_HP_NIL);
}
/*
* hotplug node is not NIL
*/
if (hp != DI_HP_NIL) {
if (DI_HP(hp)->next != 0)
return (DI_HP((caddr_t)hp - hp->self + hp->next));
else {
errno = ENXIO;
return (DI_HP_NIL);
}
}
/*
* hotplug node is NIL-->caller asks for first hotplug node
*/
if (DI_NODE(node)->hp_data != 0) {
return (DI_HP((caddr_t)node - DI_NODE(node)->self +
DI_NODE(node)->hp_data));
}
/*
* no hotplug data-->check if snapshot includes hotplug data
* in order to set the correct errno
*/
pa = (caddr_t)node - DI_NODE(node)->self;
if (DINFOHP & DI_ALL(pa)->command)
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_HP_NIL);
}
char *
di_hp_name(di_hp_t hp)
{
caddr_t pa;
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return (NULL);
}
pa = (caddr_t)hp - DI_HP(hp)->self;
if (DI_HP(hp)->hp_name == 0) {
errno = ENXIO;
return (NULL);
}
return ((char *)(pa + DI_HP(hp)->hp_name));
}
int
di_hp_connection(di_hp_t hp)
{
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return (-1);
}
if (DI_HP(hp)->hp_connection == -1)
errno = ENOENT;
return (DI_HP(hp)->hp_connection);
}
int
di_hp_depends_on(di_hp_t hp)
{
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return (-1);
}
if (DI_HP(hp)->hp_depends_on == -1)
errno = ENOENT;
return (DI_HP(hp)->hp_depends_on);
}
int
di_hp_state(di_hp_t hp)
{
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return (-1);
}
return (DI_HP(hp)->hp_state);
}
int
di_hp_type(di_hp_t hp)
{
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return (-1);
}
return (DI_HP(hp)->hp_type);
}
char *
di_hp_description(di_hp_t hp)
{
caddr_t pa;
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return (NULL);
}
pa = (caddr_t)hp - DI_HP(hp)->self;
if (DI_HP(hp)->hp_type_str == 0)
return (NULL);
return ((char *)(pa + DI_HP(hp)->hp_type_str));
}
di_node_t
di_hp_child(di_hp_t hp)
{
caddr_t pa; /* starting address of map */
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return (DI_NODE_NIL);
}
pa = (caddr_t)hp - DI_HP(hp)->self;
if (DI_HP(hp)->hp_child > 0) {
return (DI_NODE(pa + DI_HP(hp)->hp_child));
}
/*
* Deal with error condition:
* Child doesn't exist, figure out if DINFOSUBTREE is set.
* If it isn't, set errno to ENOTSUP.
*/
if (!(DINFOSUBTREE & DI_ALL(pa)->command))
errno = ENOTSUP;
else
errno = ENXIO;
return (DI_NODE_NIL);
}
time_t
di_hp_last_change(di_hp_t hp)
{
/*
* paranoid error checking
*/
if (hp == DI_HP_NIL) {
errno = EINVAL;
return ((time_t)0);
}
return ((time_t)DI_HP(hp)->hp_last_change);
}
/*
* PROM property access
*/
/*
* openprom driver stuff:
* The maximum property length depends on the buffer size. We use
* OPROMMAXPARAM defined in <sys/openpromio.h>
*
* MAXNAMESZ is max property name. obpdefs.h defines it as 32 based on 1275
* MAXVALSZ is maximum value size, which is whatever space left in buf
*/
#define OBP_MAXBUF OPROMMAXPARAM - sizeof (int)
#define OBP_MAXPROPLEN OBP_MAXBUF - OBP_MAXPROPNAME;
struct di_prom_prop {
char *name;
int len;
uchar_t *data;
struct di_prom_prop *next; /* form a linked list */
};
struct di_prom_handle { /* handle to prom */
mutex_t lock; /* synchronize access to openprom fd */
int fd; /* /dev/openprom file descriptor */
struct di_prom_prop *list; /* linked list of prop */
union {
char buf[OPROMMAXPARAM];
struct openpromio opp;
} oppbuf;
};
di_prom_handle_t
di_prom_init()
{
struct di_prom_handle *p;
if ((p = malloc(sizeof (struct di_prom_handle))) == NULL)
return (DI_PROM_HANDLE_NIL);
DPRINTF((DI_INFO, "di_prom_init: get prom handle 0x%p\n", p));
(void) mutex_init(&p->lock, USYNC_THREAD, NULL);
if ((p->fd = open("/dev/openprom", O_RDONLY)) < 0) {
free(p);
return (DI_PROM_HANDLE_NIL);
}
p->list = NULL;
return ((di_prom_handle_t)p);
}
static void
di_prom_prop_free(struct di_prom_prop *list)
{
struct di_prom_prop *tmp = list;
while (tmp != NULL) {
list = tmp->next;
if (tmp->name != NULL) {
free(tmp->name);
}
if (tmp->data != NULL) {
free(tmp->data);
}
free(tmp);
tmp = list;
}
}
void
di_prom_fini(di_prom_handle_t ph)
{
struct di_prom_handle *p = (struct di_prom_handle *)ph;
DPRINTF((DI_INFO, "di_prom_fini: free prom handle 0x%p\n", p));
(void) close(p->fd);
(void) mutex_destroy(&p->lock);
di_prom_prop_free(p->list);
free(p);
}
/*
* Internal library interface for locating the property
* XXX: ph->lock must be held for the duration of call.
*/
static di_prom_prop_t
di_prom_prop_found(di_prom_handle_t ph, int nodeid,
di_prom_prop_t prom_prop)
{
struct di_prom_handle *p = (struct di_prom_handle *)ph;
struct openpromio *opp = &p->oppbuf.opp;
int *ip = (int *)((void *)opp->oprom_array);
struct di_prom_prop *prop = (struct di_prom_prop *)prom_prop;
DPRINTF((DI_TRACE1, "Looking for nodeid 0x%x\n", nodeid));
/*
* Set "current" nodeid in the openprom driver
*/
opp->oprom_size = sizeof (int);
*ip = nodeid;
if (ioctl(p->fd, OPROMSETNODEID, opp) < 0) {
DPRINTF((DI_ERR, "*** Nodeid not found 0x%x\n", nodeid));
return (DI_PROM_PROP_NIL);
}
DPRINTF((DI_TRACE, "Found nodeid 0x%x\n", nodeid));
bzero(opp, OBP_MAXBUF);
opp->oprom_size = OBP_MAXPROPNAME;
if (prom_prop != DI_PROM_PROP_NIL)
(void) strcpy(opp->oprom_array, prop->name);
if ((ioctl(p->fd, OPROMNXTPROP, opp) < 0) || (opp->oprom_size == 0))
return (DI_PROM_PROP_NIL);
/*
* Prom property found. Allocate struct for storing prop
* (reuse variable prop)
*/
if ((prop = malloc(sizeof (struct di_prom_prop))) == NULL)
return (DI_PROM_PROP_NIL);
/*
* Get a copy of property name
*/
if ((prop->name = strdup(opp->oprom_array)) == NULL) {
free(prop);
return (DI_PROM_PROP_NIL);
}
/*
* get property value and length
*/
opp->oprom_size = OBP_MAXPROPLEN;
if ((ioctl(p->fd, OPROMGETPROP, opp) < 0) ||
(opp->oprom_size == (uint_t)-1)) {
free(prop->name);
free(prop);
return (DI_PROM_PROP_NIL);
}
/*
* make a copy of the property value
*/
prop->len = opp->oprom_size;
if (prop->len == 0)
prop->data = NULL;
else if ((prop->data = malloc(prop->len)) == NULL) {
free(prop->name);
free(prop);
return (DI_PROM_PROP_NIL);
}
bcopy(opp->oprom_array, prop->data, prop->len);
/*
* Prepend prop to list in prom handle
*/
prop->next = p->list;
p->list = prop;
return ((di_prom_prop_t)prop);
}
di_prom_prop_t
di_prom_prop_next(di_prom_handle_t ph, di_node_t node, di_prom_prop_t prom_prop)
{
struct di_prom_handle *p = (struct di_prom_handle *)ph;
DPRINTF((DI_TRACE1, "Search next prop for node 0x%p with ph 0x%p\n",
node, p));
/*
* paranoid check
*/
if ((ph == DI_PROM_HANDLE_NIL) || (node == DI_NODE_NIL)) {
errno = EINVAL;
return (DI_PROM_PROP_NIL);
}
if (di_nodeid(node) != DI_PROM_NODEID) {
errno = ENXIO;
return (DI_PROM_PROP_NIL);
}
/*
* synchronize access to prom file descriptor
*/
(void) mutex_lock(&p->lock);
/*
* look for next property
*/
prom_prop = di_prom_prop_found(ph, DI_NODE(node)->nodeid, prom_prop);
(void) mutex_unlock(&p->lock);
return (prom_prop);
}
char *
di_prom_prop_name(di_prom_prop_t prom_prop)
{
/*
* paranoid check
*/
if (prom_prop == DI_PROM_PROP_NIL) {
errno = EINVAL;
return (NULL);
}
return (((struct di_prom_prop *)prom_prop)->name);
}
int
di_prom_prop_data(di_prom_prop_t prom_prop, uchar_t **prom_prop_data)
{
/*
* paranoid check
*/
if (prom_prop == DI_PROM_PROP_NIL) {
errno = EINVAL;
return (0);
}
*prom_prop_data = ((struct di_prom_prop *)prom_prop)->data;
return (((struct di_prom_prop *)prom_prop)->len);
}
/*
* Internal library interface for locating the property
* Returns length if found, -1 if prop doesn't exist.
*/
static struct di_prom_prop *
di_prom_prop_lookup_common(di_prom_handle_t ph, di_node_t node,
const char *prom_prop_name)
{
struct openpromio *opp;
struct di_prom_prop *prop;
struct di_prom_handle *p = (struct di_prom_handle *)ph;
/*
* paranoid check
*/
if ((ph == DI_PROM_HANDLE_NIL) || (node == DI_NODE_NIL)) {
errno = EINVAL;
return (NULL);
}
if (di_nodeid(node) != DI_PROM_NODEID) {
errno = ENXIO;
return (NULL);
}
opp = &p->oppbuf.opp;
(void) mutex_lock(&p->lock);
opp->oprom_size = sizeof (int);
opp->oprom_node = DI_NODE(node)->nodeid;
if (ioctl(p->fd, OPROMSETNODEID, opp) < 0) {
errno = ENXIO;
DPRINTF((DI_ERR, "*** Nodeid not found 0x%x\n",
DI_NODE(node)->nodeid));
(void) mutex_unlock(&p->lock);
return (NULL);
}
/*
* get property length
*/
bzero(opp, OBP_MAXBUF);
opp->oprom_size = OBP_MAXPROPLEN;
(void) strcpy(opp->oprom_array, prom_prop_name);
if ((ioctl(p->fd, OPROMGETPROPLEN, opp) < 0) ||
(opp->oprom_len == -1)) {
/* no such property */
(void) mutex_unlock(&p->lock);
return (NULL);
}
/*
* Prom property found. Allocate struct for storing prop
*/
if ((prop = malloc(sizeof (struct di_prom_prop))) == NULL) {
(void) mutex_unlock(&p->lock);
return (NULL);
}
prop->name = NULL; /* we don't need the name */
prop->len = opp->oprom_len;
if (prop->len == 0) { /* boolean property */
prop->data = NULL;
prop->next = p->list;
p->list = prop;
(void) mutex_unlock(&p->lock);
return (prop);
}
/*
* retrieve the property value
*/
bzero(opp, OBP_MAXBUF);
opp->oprom_size = OBP_MAXPROPLEN;
(void) strcpy(opp->oprom_array, prom_prop_name);
if ((ioctl(p->fd, OPROMGETPROP, opp) < 0) ||
(opp->oprom_size == (uint_t)-1)) {
/* error retrieving property value */
(void) mutex_unlock(&p->lock);
free(prop);
return (NULL);
}
/*
* make a copy of the property value, stick in ph->list
*/
if ((prop->data = malloc(prop->len)) == NULL) {
(void) mutex_unlock(&p->lock);
free(prop);
return (NULL);
}
bcopy(opp->oprom_array, prop->data, prop->len);
prop->next = p->list;
p->list = prop;
(void) mutex_unlock(&p->lock);
return (prop);
}
int
di_prom_prop_lookup_ints(di_prom_handle_t ph, di_node_t node,
const char *prom_prop_name, int **prom_prop_data)
{
int len;
struct di_prom_prop *prop;
prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
if (prop == NULL) {
*prom_prop_data = NULL;
return (-1);
}
if (prop->len == 0) { /* boolean property */
*prom_prop_data = NULL;
return (0);
}
len = di_prop_decode_common((void *)&prop->data, prop->len,
DI_PROP_TYPE_INT, 1);
*prom_prop_data = (int *)((void *)prop->data);
return (len);
}
int
di_prom_prop_lookup_strings(di_prom_handle_t ph, di_node_t node,
const char *prom_prop_name, char **prom_prop_data)
{
int len;
struct di_prom_prop *prop;
prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
if (prop == NULL) {
*prom_prop_data = NULL;
return (-1);
}
if (prop->len == 0) { /* boolean property */
*prom_prop_data = NULL;
return (0);
}
/*
* Fix an openprom bug (OBP string not NULL terminated).
* XXX This should really be fixed in promif.
*/
if (((char *)prop->data)[prop->len - 1] != '\0') {
uchar_t *tmp;
prop->len++;
if ((tmp = realloc(prop->data, prop->len)) == NULL)
return (-1);
prop->data = tmp;
((char *)prop->data)[prop->len - 1] = '\0';
DPRINTF((DI_INFO, "OBP string not NULL terminated: "
"node=%s, prop=%s, val=%s\n",
di_node_name(node), prom_prop_name, prop->data));
}
len = di_prop_decode_common((void *)&prop->data, prop->len,
DI_PROP_TYPE_STRING, 1);
*prom_prop_data = (char *)prop->data;
return (len);
}
int
di_prom_prop_lookup_bytes(di_prom_handle_t ph, di_node_t node,
const char *prom_prop_name, uchar_t **prom_prop_data)
{
int len;
struct di_prom_prop *prop;
prop = di_prom_prop_lookup_common(ph, node, prom_prop_name);
if (prop == NULL) {
*prom_prop_data = NULL;
return (-1);
}
if (prop->len == 0) { /* boolean property */
*prom_prop_data = NULL;
return (0);
}
len = di_prop_decode_common((void *)&prop->data, prop->len,
DI_PROP_TYPE_BYTE, 1);
*prom_prop_data = prop->data;
return (len);
}
/*
* returns an allocated array through <prop_data> only when its count > 0
* and the number of entries (count) as the function return value;
* use di_slot_names_free() to free the array
*/
int
di_prop_slot_names(di_prop_t prop, di_slot_name_t **prop_data)
{
int rawlen, count;
uchar_t *rawdata;
char *nm = di_prop_name(prop);
if (nm == NULL || strcmp(DI_PROP_SLOT_NAMES, nm) != 0)
goto ERROUT;
rawlen = di_prop_rawdata(prop, &rawdata);
if (rawlen <= 0 || rawdata == NULL)
goto ERROUT;
count = di_slot_names_decode(rawdata, rawlen, prop_data);
if (count < 0 || *prop_data == NULL)
goto ERROUT;
return (count);
/*NOTREACHED*/
ERROUT:
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
int
di_prop_lookup_slot_names(dev_t dev, di_node_t node,
di_slot_name_t **prop_data)
{
di_prop_t prop;
/*
* change this if and when DI_PROP_TYPE_COMPOSITE is implemented
* and slot-names is properly flagged as such
*/
if ((prop = di_prop_find(dev, node, DI_PROP_SLOT_NAMES)) ==
DI_PROP_NIL) {
*prop_data = NULL;
return (-1);
}
return (di_prop_slot_names(prop, (void *)prop_data));
}
/*
* returns an allocated array through <prop_data> only when its count > 0
* and the number of entries (count) as the function return value;
* use di_slot_names_free() to free the array
*/
int
di_prom_prop_slot_names(di_prom_prop_t prom_prop, di_slot_name_t **prop_data)
{
int rawlen, count;
uchar_t *rawdata;
rawlen = di_prom_prop_data(prom_prop, &rawdata);
if (rawlen <= 0 || rawdata == NULL)
goto ERROUT;
count = di_slot_names_decode(rawdata, rawlen, prop_data);
if (count < 0 || *prop_data == NULL)
goto ERROUT;
return (count);
/*NOTREACHED*/
ERROUT:
errno = EFAULT;
*prop_data = NULL;
return (-1);
}
int
di_prom_prop_lookup_slot_names(di_prom_handle_t ph, di_node_t node,
di_slot_name_t **prop_data)
{
struct di_prom_prop *prom_prop;
prom_prop = di_prom_prop_lookup_common(ph, node, DI_PROP_SLOT_NAMES);
if (prom_prop == NULL) {
*prop_data = NULL;
return (-1);
}
return (di_prom_prop_slot_names(prom_prop, prop_data));
}
di_lnode_t
di_link_to_lnode(di_link_t link, uint_t endpoint)
{
struct di_all *di_all;
if ((link == DI_LINK_NIL) ||
((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
errno = EINVAL;
return (DI_LNODE_NIL);
}
di_all = DI_ALL((caddr_t)link - DI_LINK(link)->self);
if (endpoint == DI_LINK_SRC) {
return (DI_LNODE((caddr_t)di_all + DI_LINK(link)->src_lnode));
} else {
return (DI_LNODE((caddr_t)di_all + DI_LINK(link)->tgt_lnode));
}
/* NOTREACHED */
}
char *
di_lnode_name(di_lnode_t lnode)
{
return (di_driver_name(di_lnode_devinfo(lnode)));
}
di_node_t
di_lnode_devinfo(di_lnode_t lnode)
{
struct di_all *di_all;
di_all = DI_ALL((caddr_t)lnode - DI_LNODE(lnode)->self);
return (DI_NODE((caddr_t)di_all + DI_LNODE(lnode)->node));
}
int
di_lnode_devt(di_lnode_t lnode, dev_t *devt)
{
if ((lnode == DI_LNODE_NIL) || (devt == NULL)) {
errno = EINVAL;
return (-1);
}
if ((DI_LNODE(lnode)->dev_major == (major_t)-1) &&
(DI_LNODE(lnode)->dev_minor == (minor_t)-1))
return (-1);
*devt = makedev(DI_LNODE(lnode)->dev_major, DI_LNODE(lnode)->dev_minor);
return (0);
}
int
di_link_spectype(di_link_t link)
{
return (DI_LINK(link)->spec_type);
}
void
di_minor_private_set(di_minor_t minor, void *data)
{
DI_MINOR(minor)->user_private_data = (uintptr_t)data;
}
void *
di_minor_private_get(di_minor_t minor)
{
return ((void *)(uintptr_t)DI_MINOR(minor)->user_private_data);
}
void
di_node_private_set(di_node_t node, void *data)
{
DI_NODE(node)->user_private_data = (uintptr_t)data;
}
void *
di_node_private_get(di_node_t node)
{
return ((void *)(uintptr_t)DI_NODE(node)->user_private_data);
}
void
di_path_private_set(di_path_t path, void *data)
{
DI_PATH(path)->user_private_data = (uintptr_t)data;
}
void *
di_path_private_get(di_path_t path)
{
return ((void *)(uintptr_t)DI_PATH(path)->user_private_data);
}
void
di_lnode_private_set(di_lnode_t lnode, void *data)
{
DI_LNODE(lnode)->user_private_data = (uintptr_t)data;
}
void *
di_lnode_private_get(di_lnode_t lnode)
{
return ((void *)(uintptr_t)DI_LNODE(lnode)->user_private_data);
}
void
di_link_private_set(di_link_t link, void *data)
{
DI_LINK(link)->user_private_data = (uintptr_t)data;
}
void *
di_link_private_get(di_link_t link)
{
return ((void *)(uintptr_t)DI_LINK(link)->user_private_data);
}
di_lnode_t
di_lnode_next(di_node_t node, di_lnode_t lnode)
{
struct di_all *di_all;
/*
* paranoid error checking
*/
if (node == DI_NODE_NIL) {
errno = EINVAL;
return (DI_LNODE_NIL);
}
di_all = DI_ALL((caddr_t)node - DI_NODE(node)->self);
if (lnode == DI_NODE_NIL) {
if (DI_NODE(node)->lnodes != 0)
return (DI_LNODE((caddr_t)di_all +
DI_NODE(node)->lnodes));
} else {
if (DI_LNODE(lnode)->node_next != 0)
return (DI_LNODE((caddr_t)di_all +
DI_LNODE(lnode)->node_next));
}
if (DINFOLYR & DI_ALL(di_all)->command)
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_LNODE_NIL);
}
di_link_t
di_link_next_by_node(di_node_t node, di_link_t link, uint_t endpoint)
{
struct di_all *di_all;
/*
* paranoid error checking
*/
if ((node == DI_NODE_NIL) ||
((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
errno = EINVAL;
return (DI_LINK_NIL);
}
di_all = DI_ALL((caddr_t)node - DI_NODE(node)->self);
if (endpoint == DI_LINK_SRC) {
if (link == DI_LINK_NIL) {
if (DI_NODE(node)->src_links != 0)
return (DI_LINK((caddr_t)di_all +
DI_NODE(node)->src_links));
} else {
if (DI_LINK(link)->src_node_next != 0)
return (DI_LINK((caddr_t)di_all +
DI_LINK(link)->src_node_next));
}
} else {
if (link == DI_LINK_NIL) {
if (DI_NODE(node)->tgt_links != 0)
return (DI_LINK((caddr_t)di_all +
DI_NODE(node)->tgt_links));
} else {
if (DI_LINK(link)->tgt_node_next != 0)
return (DI_LINK((caddr_t)di_all +
DI_LINK(link)->tgt_node_next));
}
}
if (DINFOLYR & DI_ALL(di_all)->command)
errno = ENXIO;
else
errno = ENOTSUP;
return (DI_LINK_NIL);
}
di_link_t
di_link_next_by_lnode(di_lnode_t lnode, di_link_t link, uint_t endpoint)
{
struct di_all *di_all;
/*
* paranoid error checking
*/
if ((lnode == DI_LNODE_NIL) ||
((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
errno = EINVAL;
return (DI_LINK_NIL);
}
di_all = DI_ALL((caddr_t)lnode - DI_LNODE(lnode)->self);
if (endpoint == DI_LINK_SRC) {
if (link == DI_LINK_NIL) {
if (DI_LNODE(lnode)->link_out == 0)
return (DI_LINK_NIL);
return (DI_LINK((caddr_t)di_all +
DI_LNODE(lnode)->link_out));
} else {
if (DI_LINK(link)->src_link_next == 0)
return (DI_LINK_NIL);
return (DI_LINK((caddr_t)di_all +
DI_LINK(link)->src_link_next));
}
} else {
if (link == DI_LINK_NIL) {
if (DI_LNODE(lnode)->link_in == 0)
return (DI_LINK_NIL);
return (DI_LINK((caddr_t)di_all +
DI_LNODE(lnode)->link_in));
} else {
if (DI_LINK(link)->tgt_link_next == 0)
return (DI_LINK_NIL);
return (DI_LINK((caddr_t)di_all +
DI_LINK(link)->tgt_link_next));
}
}
/* NOTREACHED */
}
/*
* Internal library function:
* Invoke callback for each link data on the link list of first node
* on node_list headp, and place children of first node on the list.
*
* This is similar to walk_one_node, except we only walk in child
* first mode.
*/
static void
walk_one_link(struct node_list **headp, uint_t ep,
void *arg, int (*callback)(di_link_t link, void *arg))
{
int action = DI_WALK_CONTINUE;
di_link_t link = DI_LINK_NIL;
di_node_t node = (*headp)->node;
while ((link = di_link_next_by_node(node, link, ep)) != DI_LINK_NIL) {
action = callback(link, arg);
if (action == DI_WALK_TERMINATE) {
break;
}
}
update_node_list(action, DI_WALK_LINKGEN, headp);
}
int
di_walk_link(di_node_t root, uint_t flag, uint_t endpoint, void *arg,
int (*link_callback)(di_link_t link, void *arg))
{
struct node_list *head; /* node_list for tree walk */
#ifdef DEBUG
char *devfspath = di_devfs_path(root);
DPRINTF((DI_INFO, "walking %s link data under %s\n",
(endpoint == DI_LINK_SRC) ? "src" : "tgt", devfspath));
di_devfs_path_free(devfspath);
#endif
/*
* paranoid error checking
*/
if ((root == DI_NODE_NIL) || (link_callback == NULL) || (flag != 0) ||
((endpoint != DI_LINK_SRC) && (endpoint != DI_LINK_TGT))) {
errno = EINVAL;
return (-1);
}
if ((head = malloc(sizeof (struct node_list))) == NULL) {
DPRINTF((DI_ERR, "malloc of node_list failed\n"));
return (-1);
}
head->next = NULL;
head->node = root;
DPRINTF((DI_INFO, "Start link data walking from node %s\n",
di_node_name(root)));
while (head != NULL)
walk_one_link(&head, endpoint, arg, link_callback);
return (0);
}
/*
* Internal library function:
* Invoke callback for each link data on the link list of first node
* on node_list headp, and place children of first node on the list.
*
* This is similar to walk_one_node, except we only walk in child
* first mode.
*/
static void
walk_one_lnode(struct node_list **headp, void *arg,
int (*callback)(di_lnode_t lnode, void *arg))
{
int action = DI_WALK_CONTINUE;
di_lnode_t lnode = DI_LNODE_NIL;
di_node_t node = (*headp)->node;
while ((lnode = di_lnode_next(node, lnode)) != DI_LNODE_NIL) {
action = callback(lnode, arg);
if (action == DI_WALK_TERMINATE) {
break;
}
}
update_node_list(action, DI_WALK_LINKGEN, headp);
}
int
di_walk_lnode(di_node_t root, uint_t flag, void *arg,
int (*lnode_callback)(di_lnode_t lnode, void *arg))
{
struct node_list *head; /* node_list for tree walk */
#ifdef DEBUG
char *devfspath = di_devfs_path(root);
DPRINTF((DI_INFO, "walking lnode data under %s\n", devfspath));
di_devfs_path_free(devfspath);
#endif
/*
* paranoid error checking
*/
if ((root == DI_NODE_NIL) || (lnode_callback == NULL) || (flag != 0)) {
errno = EINVAL;
return (-1);
}
if ((head = malloc(sizeof (struct node_list))) == NULL) {
DPRINTF((DI_ERR, "malloc of node_list failed\n"));
return (-1);
}
head->next = NULL;
head->node = root;
DPRINTF((DI_INFO, "Start lnode data walking from node %s\n",
di_node_name(root)));
while (head != NULL)
walk_one_lnode(&head, arg, lnode_callback);
return (0);
}
static char *
alias_to_curr(di_node_t anynode, char *devfspath, di_node_t *nodep)
{
caddr_t pa;
struct di_all *all;
struct di_alias *di_alias;
di_node_t node;
char *curr;
char *cp;
char *alias;
di_off_t off;
char buf[MAXPATHLEN];
*nodep = NULL;
if (anynode == DI_NODE_NIL || devfspath == NULL)
return (NULL);
pa = (caddr_t)anynode - DI_NODE(anynode)->self;
all = DI_ALL(pa);
di_alias = NULL;
for (off = all->aliases; off > 0; off = di_alias->next) {
di_alias = DI_ALIAS(pa + off);
alias = di_alias->alias;
if (strncmp(devfspath, alias, strlen(alias)) == 0) {
cp = devfspath + strlen(alias);
node = DI_NODE(pa + di_alias->curroff);
assert(node != DI_NODE_NIL);
if (*cp == '\0') {
*nodep = node;
return (NULL);
} else if (*cp == '/') {
curr = di_devfs_path(node);
(void) snprintf(buf, sizeof (buf), "%s%s",
curr, cp);
di_devfs_path_free(curr);
curr = strdup(buf);
return (curr);
}
}
}
return (NULL);
}
static di_node_t
di_lookup_node_impl(di_node_t root, char *devfspath)
{
struct di_all *dap;
di_node_t node;
char *copy, *slash, *pname, *paddr;
/*
* Path must be absolute and musn't have duplicate slashes
*/
if (*devfspath != '/' || strstr(devfspath, "//")) {
DPRINTF((DI_ERR, "Invalid path: %s\n", devfspath));
return (DI_NODE_NIL);
}
if (root == DI_NODE_NIL) {
DPRINTF((DI_ERR, "root node is DI_NODE_NIL\n"));
return (DI_NODE_NIL);
}
dap = DI_ALL((caddr_t)root - DI_NODE(root)->self);
if (strcmp(dap->root_path, "/") != 0) {
DPRINTF((DI_ERR, "snapshot root not / : %s\n", dap->root_path));
return (DI_NODE_NIL);
}
if ((copy = strdup(devfspath)) == NULL) {
DPRINTF((DI_ERR, "strdup failed on: %s\n", devfspath));
return (DI_NODE_NIL);
}
for (slash = copy, node = root; slash; ) {
/*
* Handle devfspath = "/" case as well as trailing '/'
*/
if (*(slash + 1) == '\0')
break;
/*
* More path-components exist. Deal with the next one
*/
pname = slash + 1;
node = di_child_node(node);
if (slash = strchr(pname, '/'))
*slash = '\0';
if (paddr = strchr(pname, '@'))
*paddr++ = '\0';
for (; node != DI_NODE_NIL; node = di_sibling_node(node)) {
char *name, *baddr;
name = di_node_name(node);
baddr = di_bus_addr(node);
if (strcmp(pname, name) != 0)
continue;
/*
* Mappings between a "path-address" and bus-addr
*
* paddr baddr
* ---------------------
* NULL NULL
* NULL ""
* "" N/A (invalid paddr)
*/
if (paddr && baddr && strcmp(paddr, baddr) == 0)
break;
if (paddr == NULL && (baddr == NULL || *baddr == '\0'))
break;
}
/*
* No nodes in the sibling list or there was no match
*/
if (node == DI_NODE_NIL) {
DPRINTF((DI_ERR, "%s@%s: no node\n", pname, paddr));
free(copy);
return (DI_NODE_NIL);
}
}
assert(node != DI_NODE_NIL);
free(copy);
return (node);
}
di_node_t
di_lookup_node(di_node_t root, char *devfspath)
{
di_node_t node;
char *curr;
node = di_lookup_node_impl(root, devfspath);
if (node != DI_NODE_NIL) {
return (node);
}
/* node is already set to DI_NODE_NIL */
curr = alias_to_curr(root, devfspath, &node);
if (curr == NULL) {
/* node may or may node be DI_NODE_NIL */
return (node);
}
node = di_lookup_node_impl(root, curr);
free(curr);
return (node);
}
char *
di_alias2curr(di_node_t anynode, char *alias)
{
di_node_t currnode = DI_NODE_NIL;
char *curr;
if (anynode == DI_NODE_NIL || alias == NULL)
return (NULL);
curr = alias_to_curr(anynode, alias, &currnode);
if (curr == NULL && currnode != DI_NODE_NIL) {
return (di_devfs_path(currnode));
} else if (curr == NULL) {
return (strdup(alias));
}
return (curr);
}
di_path_t
di_lookup_path(di_node_t root, char *devfspath)
{
di_node_t phci_node;
di_path_t path = DI_PATH_NIL;
char *copy, *lastslash;
char *pname, *paddr;
char *path_name, *path_addr;
if ((copy = strdup(devfspath)) == NULL) {
DPRINTF((DI_ERR, "strdup failed on: %s\n", devfspath));
return (DI_NODE_NIL);
}
if ((lastslash = strrchr(copy, '/')) == NULL) {
DPRINTF((DI_ERR, "failed to find component: %s\n", devfspath));
goto out;
}
/* stop at pHCI and find the node for the phci */
*lastslash = '\0';
phci_node = di_lookup_node(root, copy);
if (phci_node == NULL) {
DPRINTF((DI_ERR, "failed to find component: %s\n", devfspath));
goto out;
}
/* set up pname and paddr for last component */
pname = lastslash + 1;
if ((paddr = strchr(pname, '@')) == NULL) {
DPRINTF((DI_ERR, "failed to find unit-addr: %s\n", devfspath));
goto out;
}
*paddr++ = '\0';
/* walk paths below phci looking for match */
for (path = di_path_phci_next_path(phci_node, DI_PATH_NIL);
path != DI_PATH_NIL;
path = di_path_phci_next_path(phci_node, path)) {
/* get name@addr of path */
path_name = di_path_node_name(path);
path_addr = di_path_bus_addr(path);
if ((path_name == NULL) || (path_addr == NULL))
continue;
/* break on match */
if ((strcmp(pname, path_name) == 0) &&
(strcmp(paddr, path_addr) == 0))
break;
}
out: free(copy);
return (path);
}
static char *
msglevel2str(di_debug_t msglevel)
{
switch (msglevel) {
case DI_ERR:
return ("ERROR");
case DI_INFO:
return ("Info");
case DI_TRACE:
return ("Trace");
case DI_TRACE1:
return ("Trace1");
case DI_TRACE2:
return ("Trace2");
default:
return ("UNKNOWN");
}
}
void
dprint(di_debug_t msglevel, const char *fmt, ...)
{
va_list ap;
char *estr;
if (di_debug <= DI_QUIET)
return;
if (di_debug < msglevel)
return;
estr = msglevel2str(msglevel);
assert(estr);
va_start(ap, fmt);
(void) fprintf(stderr, "libdevinfo[%lu]: %s: ",
(ulong_t)getpid(), estr);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
}
/* end of devinfo.c */
|