summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/os/sunddi.c
blob: 30cc5744c235c36e05f69f381f890393b83dd4f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
/*
 * 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) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2022 Garrett D'Amore
 * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
 */

#include <sys/note.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/cred.h>
#include <sys/poll.h>
#include <sys/mman.h>
#include <sys/kmem.h>
#include <sys/model.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/open.h>
#include <sys/user.h>
#include <sys/t_lock.h>
#include <sys/vm.h>
#include <sys/stat.h>
#include <vm/hat.h>
#include <vm/seg.h>
#include <vm/seg_vn.h>
#include <vm/seg_dev.h>
#include <vm/as.h>
#include <sys/cmn_err.h>
#include <sys/cpuvar.h>
#include <sys/debug.h>
#include <sys/autoconf.h>
#include <sys/sunddi.h>
#include <sys/esunddi.h>
#include <sys/sunndi.h>
#include <sys/kstat.h>
#include <sys/conf.h>
#include <sys/ddi_impldefs.h>	/* include implementation structure defs */
#include <sys/ndi_impldefs.h>	/* include prototypes */
#include <sys/ddi_periodic.h>
#include <sys/hwconf.h>
#include <sys/pathname.h>
#include <sys/modctl.h>
#include <sys/epm.h>
#include <sys/devctl.h>
#include <sys/callb.h>
#include <sys/cladm.h>
#include <sys/sysevent.h>
#include <sys/dacf_impl.h>
#include <sys/ddidevmap.h>
#include <sys/bootconf.h>
#include <sys/disp.h>
#include <sys/atomic.h>
#include <sys/promif.h>
#include <sys/instance.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/task.h>
#include <sys/project.h>
#include <sys/taskq.h>
#include <sys/devpolicy.h>
#include <sys/ctype.h>
#include <net/if.h>
#include <sys/rctl.h>
#include <sys/zone.h>
#include <sys/clock_impl.h>
#include <sys/ddi.h>
#include <sys/modhash.h>
#include <sys/sunldi_impl.h>
#include <sys/fs/dv_node.h>
#include <sys/fs/snode.h>

extern	pri_t	minclsyspri;

extern	rctl_hndl_t rc_project_locked_mem;
extern	rctl_hndl_t rc_zone_locked_mem;

#ifdef DEBUG
static int sunddi_debug = 0;
#endif /* DEBUG */

/* ddi_umem_unlock miscellaneous */

static	void	i_ddi_umem_unlock_thread_start(void);

static	kmutex_t	ddi_umem_unlock_mutex; /* unlock list mutex */
static	kcondvar_t	ddi_umem_unlock_cv; /* unlock list block/unblock */
static	kthread_t	*ddi_umem_unlock_thread;
/*
 * The ddi_umem_unlock FIFO list.  NULL head pointer indicates empty list.
 */
static	struct	ddi_umem_cookie *ddi_umem_unlock_head = NULL;
static	struct	ddi_umem_cookie *ddi_umem_unlock_tail = NULL;

/*
 * DDI(Sun) Function and flag definitions:
 */

#if defined(__x86)
/*
 * Used to indicate which entries were chosen from a range.
 */
char	*chosen_reg = "chosen-reg";
#endif

/*
 * Function used to ring system console bell
 */
void (*ddi_console_bell_func)(clock_t duration);

/*
 * Creating register mappings and handling interrupts:
 */

/*
 * Generic ddi_map: Call parent to fulfill request...
 */

int
ddi_map(dev_info_t *dp, ddi_map_req_t *mp, off_t offset,
    off_t len, caddr_t *addrp)
{
	dev_info_t *pdip;

	ASSERT(dp);
	pdip = (dev_info_t *)DEVI(dp)->devi_parent;
	return ((DEVI(pdip)->devi_ops->devo_bus_ops->bus_map)(pdip,
	    dp, mp, offset, len, addrp));
}

/*
 * ddi_apply_range: (Called by nexi only.)
 * Apply ranges in parent node dp, to child regspec rp...
 */

int
ddi_apply_range(dev_info_t *dp, dev_info_t *rdip, struct regspec *rp)
{
	return (i_ddi_apply_range(dp, rdip, rp));
}

int
ddi_map_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
    off_t len)
{
	ddi_map_req_t mr;
#if defined(__x86)
	struct {
		int	bus;
		int	addr;
		int	size;
	} reg, *reglist;
	uint_t	length;
	int	rc;

	/*
	 * get the 'registers' or the 'reg' property.
	 * We look up the reg property as an array of
	 * int's.
	 */
	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "registers", (int **)&reglist, &length);
	if (rc != DDI_PROP_SUCCESS)
		rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
		    DDI_PROP_DONTPASS, "reg", (int **)&reglist, &length);
	if (rc == DDI_PROP_SUCCESS) {
		/*
		 * point to the required entry.
		 */
		reg = reglist[rnumber];
		reg.addr += offset;
		if (len != 0)
			reg.size = len;
		/*
		 * make a new property containing ONLY the required tuple.
		 */
		if (ddi_prop_update_int_array(DDI_DEV_T_NONE, dip,
		    chosen_reg, (int *)&reg, (sizeof (reg)/sizeof (int)))
		    != DDI_PROP_SUCCESS) {
			cmn_err(CE_WARN, "%s%d: cannot create '%s' "
			    "property", DEVI(dip)->devi_name,
			    DEVI(dip)->devi_instance, chosen_reg);
		}
		/*
		 * free the memory allocated by
		 * ddi_prop_lookup_int_array ().
		 */
		ddi_prop_free((void *)reglist);
	}
#endif
	mr.map_op = DDI_MO_MAP_LOCKED;
	mr.map_type = DDI_MT_RNUMBER;
	mr.map_obj.rnumber = rnumber;
	mr.map_prot = PROT_READ | PROT_WRITE;
	mr.map_flags = DDI_MF_KERNEL_MAPPING;
	mr.map_handlep = NULL;
	mr.map_vers = DDI_MAP_VERSION;

	/*
	 * Call my parent to map in my regs.
	 */

	return (ddi_map(dip, &mr, offset, len, kaddrp));
}

void
ddi_unmap_regs(dev_info_t *dip, uint_t rnumber, caddr_t *kaddrp, off_t offset,
    off_t len)
{
	ddi_map_req_t mr;

	mr.map_op = DDI_MO_UNMAP;
	mr.map_type = DDI_MT_RNUMBER;
	mr.map_flags = DDI_MF_KERNEL_MAPPING;
	mr.map_prot = PROT_READ | PROT_WRITE;	/* who cares? */
	mr.map_obj.rnumber = rnumber;
	mr.map_handlep = NULL;
	mr.map_vers = DDI_MAP_VERSION;

	/*
	 * Call my parent to unmap my regs.
	 */

	(void) ddi_map(dip, &mr, offset, len, kaddrp);
	*kaddrp = (caddr_t)0;
#if defined(__x86)
	(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, chosen_reg);
#endif
}

int
ddi_bus_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
    off_t offset, off_t len, caddr_t *vaddrp)
{
	return (i_ddi_bus_map(dip, rdip, mp, offset, len, vaddrp));
}

/*
 * nullbusmap:	The/DDI default bus_map entry point for nexi
 *		not conforming to the reg/range paradigm (i.e. scsi, etc.)
 *		with no HAT/MMU layer to be programmed at this level.
 *
 *		If the call is to map by rnumber, return an error,
 *		otherwise pass anything else up the tree to my parent.
 */
int
nullbusmap(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
    off_t offset, off_t len, caddr_t *vaddrp)
{
	_NOTE(ARGUNUSED(rdip))
	if (mp->map_type == DDI_MT_RNUMBER)
		return (DDI_ME_UNSUPPORTED);

	return (ddi_map(dip, mp, offset, len, vaddrp));
}

/*
 * ddi_rnumber_to_regspec: Not for use by leaf drivers.
 *			   Only for use by nexi using the reg/range paradigm.
 */
struct regspec *
ddi_rnumber_to_regspec(dev_info_t *dip, int rnumber)
{
	return (i_ddi_rnumber_to_regspec(dip, rnumber));
}


/*
 * Note that we allow the dip to be nil because we may be called
 * prior even to the instantiation of the devinfo tree itself - all
 * regular leaf and nexus drivers should always use a non-nil dip!
 *
 * We treat peek in a somewhat cavalier fashion .. assuming that we'll
 * simply get a synchronous fault as soon as we touch a missing address.
 *
 * Poke is rather more carefully handled because we might poke to a write
 * buffer, "succeed", then only find some time later that we got an
 * asynchronous fault that indicated that the address we were writing to
 * was not really backed by hardware.
 */

static int
i_ddi_peekpoke(dev_info_t *devi, ddi_ctl_enum_t cmd, size_t size,
    void *addr, void *value_p)
{
	union {
		uint64_t	u64;
		uint32_t	u32;
		uint16_t	u16;
		uint8_t		u8;
	} peekpoke_value;

	peekpoke_ctlops_t peekpoke_args;
	uint64_t dummy_result;
	int rval;

	/* Note: size is assumed to be correct;  it is not checked. */
	peekpoke_args.size = size;
	peekpoke_args.dev_addr = (uintptr_t)addr;
	peekpoke_args.handle = NULL;
	peekpoke_args.repcount = 1;
	peekpoke_args.flags = 0;

	if (cmd == DDI_CTLOPS_POKE) {
		switch (size) {
		case sizeof (uint8_t):
			peekpoke_value.u8 = *(uint8_t *)value_p;
			break;
		case sizeof (uint16_t):
			peekpoke_value.u16 = *(uint16_t *)value_p;
			break;
		case sizeof (uint32_t):
			peekpoke_value.u32 = *(uint32_t *)value_p;
			break;
		case sizeof (uint64_t):
			peekpoke_value.u64 = *(uint64_t *)value_p;
			break;
		}
	}

	peekpoke_args.host_addr = (uintptr_t)&peekpoke_value.u64;

	if (devi != NULL)
		rval = ddi_ctlops(devi, devi, cmd, &peekpoke_args,
		    &dummy_result);
	else
		rval = peekpoke_mem(cmd, &peekpoke_args);

	/*
	 * A NULL value_p is permitted by ddi_peek(9F); discard the result.
	 */
	if ((cmd == DDI_CTLOPS_PEEK) & (value_p != NULL)) {
		switch (size) {
		case sizeof (uint8_t):
			*(uint8_t *)value_p = peekpoke_value.u8;
			break;
		case sizeof (uint16_t):
			*(uint16_t *)value_p = peekpoke_value.u16;
			break;
		case sizeof (uint32_t):
			*(uint32_t *)value_p = peekpoke_value.u32;
			break;
		case sizeof (uint64_t):
			*(uint64_t *)value_p = peekpoke_value.u64;
			break;
		}
	}

	return (rval);
}

/*
 * Keep ddi_peek() and ddi_poke() in case 3rd parties are calling this.
 * they shouldn't be, but the 9f manpage kind of pseudo exposes it.
 */
int
ddi_peek(dev_info_t *devi, size_t size, void *addr, void *value_p)
{
	switch (size) {
	case sizeof (uint8_t):
	case sizeof (uint16_t):
	case sizeof (uint32_t):
	case sizeof (uint64_t):
		break;
	default:
		return (DDI_FAILURE);
	}

	return (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, size, addr, value_p));
}

int
ddi_poke(dev_info_t *devi, size_t size, void *addr, void *value_p)
{
	switch (size) {
	case sizeof (uint8_t):
	case sizeof (uint16_t):
	case sizeof (uint32_t):
	case sizeof (uint64_t):
		break;
	default:
		return (DDI_FAILURE);
	}

	return (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, size, addr, value_p));
}

int
ddi_peek8(dev_info_t *dip, int8_t *addr, int8_t *val_p)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
	    val_p));
}

int
ddi_peek16(dev_info_t *dip, int16_t *addr, int16_t *val_p)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
	    val_p));
}

int
ddi_peek32(dev_info_t *dip, int32_t *addr, int32_t *val_p)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
	    val_p));
}

int
ddi_peek64(dev_info_t *dip, int64_t *addr, int64_t *val_p)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_PEEK, sizeof (*val_p), addr,
	    val_p));
}

int
ddi_poke8(dev_info_t *dip, int8_t *addr, int8_t val)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
}

int
ddi_poke16(dev_info_t *dip, int16_t *addr, int16_t val)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
}

int
ddi_poke32(dev_info_t *dip, int32_t *addr, int32_t val)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
}

int
ddi_poke64(dev_info_t *dip, int64_t *addr, int64_t val)
{
	return (i_ddi_peekpoke(dip, DDI_CTLOPS_POKE, sizeof (val), addr, &val));
}

/*
 * ddi_peekpokeio() is used primarily by the mem drivers for moving
 * data to and from uio structures via peek and poke.  Note that we
 * use "internal" routines ddi_peek and ddi_poke to make this go
 * slightly faster, avoiding the call overhead ..
 */
int
ddi_peekpokeio(dev_info_t *devi, struct uio *uio, enum uio_rw rw,
    caddr_t addr, size_t len, uint_t xfersize)
{
	int64_t	ibuffer;
	int8_t w8;
	size_t sz;
	int o;

	if (xfersize > sizeof (long))
		xfersize = sizeof (long);

	while (len != 0) {
		if ((len | (uintptr_t)addr) & 1) {
			sz = sizeof (int8_t);
			if (rw == UIO_WRITE) {
				if ((o = uwritec(uio)) == -1)
					return (DDI_FAILURE);
				if (ddi_poke8(devi, (int8_t *)addr,
				    (int8_t)o) != DDI_SUCCESS)
					return (DDI_FAILURE);
			} else {
				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
				    (int8_t *)addr, &w8) != DDI_SUCCESS)
					return (DDI_FAILURE);
				if (ureadc(w8, uio))
					return (DDI_FAILURE);
			}
		} else {
			switch (xfersize) {
			case sizeof (int64_t):
				if (((len | (uintptr_t)addr) &
				    (sizeof (int64_t) - 1)) == 0) {
					sz = xfersize;
					break;
				}
				/*FALLTHROUGH*/
			case sizeof (int32_t):
				if (((len | (uintptr_t)addr) &
				    (sizeof (int32_t) - 1)) == 0) {
					sz = xfersize;
					break;
				}
				/*FALLTHROUGH*/
			default:
				/*
				 * This still assumes that we might have an
				 * I/O bus out there that permits 16-bit
				 * transfers (and that it would be upset by
				 * 32-bit transfers from such locations).
				 */
				sz = sizeof (int16_t);
				break;
			}

			if (rw == UIO_READ) {
				if (i_ddi_peekpoke(devi, DDI_CTLOPS_PEEK, sz,
				    addr, &ibuffer) != DDI_SUCCESS)
					return (DDI_FAILURE);
			}

			if (uiomove(&ibuffer, sz, rw, uio))
				return (DDI_FAILURE);

			if (rw == UIO_WRITE) {
				if (i_ddi_peekpoke(devi, DDI_CTLOPS_POKE, sz,
				    addr, &ibuffer) != DDI_SUCCESS)
					return (DDI_FAILURE);
			}
		}
		addr += sz;
		len -= sz;
	}
	return (DDI_SUCCESS);
}

/*
 * These routines are used by drivers that do layered ioctls
 * On sparc, they're implemented in assembler to avoid spilling
 * register windows in the common (copyin) case ..
 */
#if !defined(__sparc)
int
ddi_copyin(const void *buf, void *kernbuf, size_t size, int flags)
{
	if (flags & FKIOCTL)
		return (kcopy(buf, kernbuf, size) ? -1 : 0);
	return (copyin(buf, kernbuf, size));
}

int
ddi_copyout(const void *buf, void *kernbuf, size_t size, int flags)
{
	if (flags & FKIOCTL)
		return (kcopy(buf, kernbuf, size) ? -1 : 0);
	return (copyout(buf, kernbuf, size));
}
#endif	/* !__sparc */

/*
 * Conversions in nexus pagesize units.  We don't duplicate the
 * 'nil dip' semantics of peek/poke because btopr/btop/ptob are DDI/DKI
 * routines anyway.
 */
unsigned long
ddi_btop(dev_info_t *dip, unsigned long bytes)
{
	unsigned long pages;

	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOP, &bytes, &pages);
	return (pages);
}

unsigned long
ddi_btopr(dev_info_t *dip, unsigned long bytes)
{
	unsigned long pages;

	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_BTOPR, &bytes, &pages);
	return (pages);
}

unsigned long
ddi_ptob(dev_info_t *dip, unsigned long pages)
{
	unsigned long bytes;

	(void) ddi_ctlops(dip, dip, DDI_CTLOPS_PTOB, &pages, &bytes);
	return (bytes);
}

unsigned int
ddi_enter_critical(void)
{
	return ((uint_t)spl7());
}

void
ddi_exit_critical(unsigned int spl)
{
	splx((int)spl);
}

/*
 * Nexus ctlops punter
 */

#if !defined(__sparc)
/*
 * Request bus_ctl parent to handle a bus_ctl request
 *
 * (The sparc version is in sparc_ddi.s)
 */
int
ddi_ctlops(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t op, void *a, void *v)
{
	int (*fp)();

	if (!d || !r)
		return (DDI_FAILURE);

	if ((d = (dev_info_t *)DEVI(d)->devi_bus_ctl) == NULL)
		return (DDI_FAILURE);

	fp = DEVI(d)->devi_ops->devo_bus_ops->bus_ctl;
	return ((*fp)(d, r, op, a, v));
}

#endif

/*
 * DMA/DVMA setup
 */

#if !defined(__sparc)
/*
 * Request bus_dma_ctl parent to fiddle with a dma request.
 *
 * (The sparc version is in sparc_subr.s)
 */
int
ddi_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
    off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
{
	int (*fp)();

	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_ctl;
	fp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_ctl;
	return ((*fp) (dip, rdip, handle, request, offp, lenp, objp, flags));
}
#endif

/*
 * For all DMA control functions, call the DMA control
 * routine and return status.
 *
 * Just plain assume that the parent is to be called.
 * If a nexus driver or a thread outside the framework
 * of a nexus driver or a leaf driver calls these functions,
 * it is up to them to deal with the fact that the parent's
 * bus_dma_ctl function will be the first one called.
 */

#define	HD	((ddi_dma_impl_t *)h)->dmai_rdip

/*
 * This routine is left in place to satisfy link dependencies
 * for any 3rd party nexus drivers that rely on it.  It is never
 * called, though.
 */
/*ARGSUSED*/
int
ddi_dma_map(dev_info_t *dip, dev_info_t *rdip,
    struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
{
	return (DDI_FAILURE);
}

#if !defined(__sparc)

/*
 * The SPARC versions of these routines are done in assembler to
 * save register windows, so they're in sparc_subr.s.
 */

int
ddi_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
    int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
{
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_attr_t *,
	    int (*)(caddr_t), caddr_t, ddi_dma_handle_t *);

	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;

	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_allochdl;
	return ((*funcp)(dip, rdip, attr, waitfp, arg, handlep));
}

int
ddi_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handlep)
{
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);

	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_allochdl;

	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_freehdl;
	return ((*funcp)(dip, rdip, handlep));
}

int
ddi_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
    ddi_dma_cookie_t *cp, uint_t *ccountp)
{
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
	    struct ddi_dma_req *, ddi_dma_cookie_t *, uint_t *);

	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;

	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_bindhdl;
	return ((*funcp)(dip, rdip, handle, dmareq, cp, ccountp));
}

int
ddi_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);

	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;

	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_unbindhdl;
	return ((*funcp)(dip, rdip, handle));
}


int
ddi_dma_flush(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, off_t off, size_t len,
    uint_t cache_flags)
{
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
	    off_t, size_t, uint_t);

	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;

	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
	return ((*funcp)(dip, rdip, handle, off, len, cache_flags));
}

int
ddi_dma_win(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, uint_t win, off_t *offp,
    size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t,
	    uint_t, off_t *, size_t *, ddi_dma_cookie_t *, uint_t *);

	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_win;

	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_win;
	return ((*funcp)(dip, rdip, handle, win, offp, lenp,
	    cookiep, ccountp));
}

int
ddi_dma_sync(ddi_dma_handle_t h, off_t o, size_t l, uint_t whom)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
	dev_info_t *dip, *rdip;
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t, off_t,
	    size_t, uint_t);

	/*
	 * the DMA nexus driver will set DMP_NOSYNC if the
	 * platform does not require any sync operation. For
	 * example if the memory is uncached or consistent
	 * and without any I/O write buffers involved.
	 */
	if ((hp->dmai_rflags & DMP_NOSYNC) == DMP_NOSYNC)
		return (DDI_SUCCESS);

	dip = rdip = hp->dmai_rdip;
	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_flush;
	funcp = DEVI(dip)->devi_ops->devo_bus_ops->bus_dma_flush;
	return ((*funcp)(dip, rdip, h, o, l, whom));
}

int
ddi_dma_unbind_handle(ddi_dma_handle_t h)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)h;
	dev_info_t *dip, *rdip;
	int (*funcp)(dev_info_t *, dev_info_t *, ddi_dma_handle_t);

	dip = rdip = hp->dmai_rdip;
	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_unbindhdl;
	funcp = DEVI(rdip)->devi_bus_dma_unbindfunc;
	return ((*funcp)(dip, rdip, h));
}

#endif	/* !__sparc */

/*
 * DMA burst sizes, and transfer minimums
 */

int
ddi_dma_burstsizes(ddi_dma_handle_t handle)
{
	ddi_dma_impl_t *dimp = (ddi_dma_impl_t *)handle;

	if (!dimp)
		return (0);
	else
		return (dimp->dmai_burstsizes);
}

/*
 * Given two DMA attribute structures, apply the attributes
 * of one to the other, following the rules of attributes
 * and the wishes of the caller.
 *
 * The rules of DMA attribute structures are that you cannot
 * make things *less* restrictive as you apply one set
 * of attributes to another.
 *
 */
void
ddi_dma_attr_merge(ddi_dma_attr_t *attr, ddi_dma_attr_t *mod)
{
	attr->dma_attr_addr_lo =
	    MAX(attr->dma_attr_addr_lo, mod->dma_attr_addr_lo);
	attr->dma_attr_addr_hi =
	    MIN(attr->dma_attr_addr_hi, mod->dma_attr_addr_hi);
	attr->dma_attr_count_max =
	    MIN(attr->dma_attr_count_max, mod->dma_attr_count_max);
	attr->dma_attr_align =
	    MAX(attr->dma_attr_align,  mod->dma_attr_align);
	attr->dma_attr_burstsizes =
	    (uint_t)(attr->dma_attr_burstsizes & mod->dma_attr_burstsizes);
	attr->dma_attr_minxfer =
	    maxbit(attr->dma_attr_minxfer, mod->dma_attr_minxfer);
	attr->dma_attr_maxxfer =
	    MIN(attr->dma_attr_maxxfer, mod->dma_attr_maxxfer);
	attr->dma_attr_seg = MIN(attr->dma_attr_seg, mod->dma_attr_seg);
	attr->dma_attr_sgllen = MIN((uint_t)attr->dma_attr_sgllen,
	    (uint_t)mod->dma_attr_sgllen);
	attr->dma_attr_granular =
	    MAX(attr->dma_attr_granular, mod->dma_attr_granular);
}

/*
 * mmap/segmap interface:
 */

/*
 * ddi_segmap:		setup the default segment driver. Calls the drivers
 *			XXmmap routine to validate the range to be mapped.
 *			Return ENXIO of the range is not valid.  Create
 *			a seg_dev segment that contains all of the
 *			necessary information and will reference the
 *			default segment driver routines. It returns zero
 *			on success or non-zero on failure.
 */
int
ddi_segmap(dev_t dev, off_t offset, struct as *asp, caddr_t *addrp, off_t len,
    uint_t prot, uint_t maxprot, uint_t flags, cred_t *credp)
{
	extern int spec_segmap(dev_t, off_t, struct as *, caddr_t *,
	    off_t, uint_t, uint_t, uint_t, struct cred *);

	return (spec_segmap(dev, offset, asp, addrp, len,
	    prot, maxprot, flags, credp));
}

/*
 * ddi_map_fault:	Resolve mappings at fault time.  Used by segment
 *			drivers. Allows each successive parent to resolve
 *			address translations and add its mappings to the
 *			mapping list supplied in the page structure. It
 *			returns zero on success	or non-zero on failure.
 */

int
ddi_map_fault(dev_info_t *dip, struct hat *hat, struct seg *seg,
    caddr_t addr, struct devpage *dp, pfn_t pfn, uint_t prot, uint_t lock)
{
	return (i_ddi_map_fault(dip, dip, hat, seg, addr, dp, pfn, prot, lock));
}

/*
 * ddi_device_mapping_check:	Called from ddi_segmap_setup.
 *	Invokes platform specific DDI to determine whether attributes specified
 *	in attr(9s) are	valid for the region of memory that will be made
 *	available for direct access to user process via the mmap(2) system call.
 */
int
ddi_device_mapping_check(dev_t dev, ddi_device_acc_attr_t *accattrp,
    uint_t rnumber, uint_t *hat_flags)
{
	ddi_acc_handle_t handle;
	ddi_map_req_t mr;
	ddi_acc_hdl_t *hp;
	int result;
	dev_info_t *dip;

	/*
	 * we use e_ddi_hold_devi_by_dev to search for the devi.  We
	 * release it immediately since it should already be held by
	 * a devfs vnode.
	 */
	if ((dip =
	    e_ddi_hold_devi_by_dev(dev, E_DDI_HOLD_DEVI_NOATTACH)) == NULL)
		return (-1);
	ddi_release_devi(dip);		/* for e_ddi_hold_devi_by_dev() */

	/*
	 * Allocate and initialize the common elements of data
	 * access handle.
	 */
	handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
	if (handle == NULL)
		return (-1);

	hp = impl_acc_hdl_get(handle);
	hp->ah_vers = VERS_ACCHDL;
	hp->ah_dip = dip;
	hp->ah_rnumber = rnumber;
	hp->ah_offset = 0;
	hp->ah_len = 0;
	hp->ah_acc = *accattrp;

	/*
	 * Set up the mapping request and call to parent.
	 */
	mr.map_op = DDI_MO_MAP_HANDLE;
	mr.map_type = DDI_MT_RNUMBER;
	mr.map_obj.rnumber = rnumber;
	mr.map_prot = PROT_READ | PROT_WRITE;
	mr.map_flags = DDI_MF_KERNEL_MAPPING;
	mr.map_handlep = hp;
	mr.map_vers = DDI_MAP_VERSION;
	result = ddi_map(dip, &mr, 0, 0, NULL);

	/*
	 * Region must be mappable, pick up flags from the framework.
	 */
	*hat_flags = hp->ah_hat_flags;

	impl_acc_hdl_free(handle);

	/*
	 * check for end result.
	 */
	if (result != DDI_SUCCESS)
		return (-1);
	return (0);
}


/*
 * Property functions:	 See also, ddipropdefs.h.
 *
 * These functions are the framework for the property functions,
 * i.e. they support software defined properties.  All implementation
 * specific property handling (i.e.: self-identifying devices and
 * PROM defined properties are handled in the implementation specific
 * functions (defined in ddi_implfuncs.h).
 */

/*
 * nopropop:	Shouldn't be called, right?
 */
int
nopropop(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
    char *name, caddr_t valuep, int *lengthp)
{
	_NOTE(ARGUNUSED(dev, dip, prop_op, mod_flags, name, valuep, lengthp))
	return (DDI_PROP_NOT_FOUND);
}

#ifdef	DDI_PROP_DEBUG
int ddi_prop_debug_flag = 0;

int
ddi_prop_debug(int enable)
{
	int prev = ddi_prop_debug_flag;

	if ((enable != 0) || (prev != 0))
		printf("ddi_prop_debug: debugging %s\n",
		    enable ? "enabled" : "disabled");
	ddi_prop_debug_flag = enable;
	return (prev);
}

#endif	/* DDI_PROP_DEBUG */

/*
 * Search a property list for a match, if found return pointer
 * to matching prop struct, else return NULL.
 */

ddi_prop_t *
i_ddi_prop_search(dev_t dev, char *name, uint_t flags, ddi_prop_t **list_head)
{
	ddi_prop_t	*propp;

	/*
	 * find the property in child's devinfo:
	 * Search order defined by this search function is first matching
	 * property with input dev == DDI_DEV_T_ANY matching any dev or
	 * dev == propp->prop_dev, name == propp->name, and the correct
	 * data type as specified in the flags.  If a DDI_DEV_T_NONE dev
	 * value made it this far then it implies a DDI_DEV_T_ANY search.
	 */
	if (dev == DDI_DEV_T_NONE)
		dev = DDI_DEV_T_ANY;

	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {

		if (!DDI_STRSAME(propp->prop_name, name))
			continue;

		if ((dev != DDI_DEV_T_ANY) && (propp->prop_dev != dev))
			continue;

		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
			continue;

		return (propp);
	}

	return ((ddi_prop_t *)0);
}

/*
 * Search for property within devnames structures
 */
ddi_prop_t *
i_ddi_search_global_prop(dev_t dev, char *name, uint_t flags)
{
	major_t		major;
	struct devnames	*dnp;
	ddi_prop_t	*propp;

	/*
	 * Valid dev_t value is needed to index into the
	 * correct devnames entry, therefore a dev_t
	 * value of DDI_DEV_T_ANY is not appropriate.
	 */
	ASSERT(dev != DDI_DEV_T_ANY);
	if (dev == DDI_DEV_T_ANY) {
		return ((ddi_prop_t *)0);
	}

	major = getmajor(dev);
	dnp = &(devnamesp[major]);

	if (dnp->dn_global_prop_ptr == NULL)
		return ((ddi_prop_t *)0);

	LOCK_DEV_OPS(&dnp->dn_lock);

	for (propp = dnp->dn_global_prop_ptr->prop_list;
	    propp != NULL;
	    propp = (ddi_prop_t *)propp->prop_next) {

		if (!DDI_STRSAME(propp->prop_name, name))
			continue;

		if ((!(flags & DDI_PROP_ROOTNEX_GLOBAL)) &&
		    (!(flags & LDI_DEV_T_ANY)) && (propp->prop_dev != dev))
			continue;

		if (((propp->prop_flags & flags) & DDI_PROP_TYPE_MASK) == 0)
			continue;

		/* Property found, return it */
		UNLOCK_DEV_OPS(&dnp->dn_lock);
		return (propp);
	}

	UNLOCK_DEV_OPS(&dnp->dn_lock);
	return ((ddi_prop_t *)0);
}

static char prop_no_mem_msg[] = "can't allocate memory for ddi property <%s>";

/*
 * ddi_prop_search_global:
 *	Search the global property list within devnames
 *	for the named property.  Return the encoded value.
 */
static int
i_ddi_prop_search_global(dev_t dev, uint_t flags, char *name,
    void *valuep, uint_t *lengthp)
{
	ddi_prop_t	*propp;
	caddr_t		buffer;

	propp =  i_ddi_search_global_prop(dev, name, flags);

	/* Property NOT found, bail */
	if (propp == (ddi_prop_t *)0)
		return (DDI_PROP_NOT_FOUND);

	if (propp->prop_flags & DDI_PROP_UNDEF_IT)
		return (DDI_PROP_UNDEFINED);

	if ((buffer = kmem_alloc(propp->prop_len,
	    (flags & DDI_PROP_CANSLEEP) ? KM_SLEEP : KM_NOSLEEP)) == NULL) {
		cmn_err(CE_CONT, prop_no_mem_msg, name);
		return (DDI_PROP_NO_MEMORY);
	}

	/*
	 * Return the encoded data
	 */
	*(caddr_t *)valuep = buffer;
	*lengthp = propp->prop_len;
	bcopy(propp->prop_val, buffer, propp->prop_len);

	return (DDI_PROP_SUCCESS);
}

/*
 * ddi_prop_search_common:	Lookup and return the encoded value
 */
int
ddi_prop_search_common(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    uint_t flags, char *name, void *valuep, uint_t *lengthp)
{
	ddi_prop_t	*propp;
	int		i;
	caddr_t		buffer = NULL;
	caddr_t		prealloc = NULL;
	int		plength = 0;
	dev_info_t	*pdip;
	int		(*bop)();

	/*CONSTANTCONDITION*/
	while (1)  {

		mutex_enter(&(DEVI(dip)->devi_lock));


		/*
		 * find the property in child's devinfo:
		 * Search order is:
		 *	1. driver defined properties
		 *	2. system defined properties
		 *	3. driver global properties
		 *	4. boot defined properties
		 */

		propp = i_ddi_prop_search(dev, name, flags,
		    &(DEVI(dip)->devi_drv_prop_ptr));
		if (propp == NULL)  {
			propp = i_ddi_prop_search(dev, name, flags,
			    &(DEVI(dip)->devi_sys_prop_ptr));
		}
		if ((propp == NULL) && DEVI(dip)->devi_global_prop_list) {
			propp = i_ddi_prop_search(dev, name, flags,
			    &DEVI(dip)->devi_global_prop_list->prop_list);
		}

		if (propp == NULL)  {
			propp = i_ddi_prop_search(dev, name, flags,
			    &(DEVI(dip)->devi_hw_prop_ptr));
		}

		/*
		 * Software property found?
		 */
		if (propp != (ddi_prop_t *)0)	{

			/*
			 * If explicit undefine, return now.
			 */
			if (propp->prop_flags & DDI_PROP_UNDEF_IT) {
				mutex_exit(&(DEVI(dip)->devi_lock));
				if (prealloc)
					kmem_free(prealloc, plength);
				return (DDI_PROP_UNDEFINED);
			}

			/*
			 * If we only want to know if it exists, return now
			 */
			if (prop_op == PROP_EXISTS) {
				mutex_exit(&(DEVI(dip)->devi_lock));
				ASSERT(prealloc == NULL);
				return (DDI_PROP_SUCCESS);
			}

			/*
			 * If length only request or prop length == 0,
			 * service request and return now.
			 */
			if ((prop_op == PROP_LEN) ||(propp->prop_len == 0)) {
				*lengthp = propp->prop_len;

				/*
				 * if prop_op is PROP_LEN_AND_VAL_ALLOC
				 * that means prop_len is 0, so set valuep
				 * also to NULL
				 */
				if (prop_op == PROP_LEN_AND_VAL_ALLOC)
					*(caddr_t *)valuep = NULL;

				mutex_exit(&(DEVI(dip)->devi_lock));
				if (prealloc)
					kmem_free(prealloc, plength);
				return (DDI_PROP_SUCCESS);
			}

			/*
			 * If LEN_AND_VAL_ALLOC and the request can sleep,
			 * drop the mutex, allocate the buffer, and go
			 * through the loop again.  If we already allocated
			 * the buffer, and the size of the property changed,
			 * keep trying...
			 */
			if ((prop_op == PROP_LEN_AND_VAL_ALLOC) &&
			    (flags & DDI_PROP_CANSLEEP))  {
				if (prealloc && (propp->prop_len != plength)) {
					kmem_free(prealloc, plength);
					prealloc = NULL;
				}
				if (prealloc == NULL)  {
					plength = propp->prop_len;
					mutex_exit(&(DEVI(dip)->devi_lock));
					prealloc = kmem_alloc(plength,
					    KM_SLEEP);
					continue;
				}
			}

			/*
			 * Allocate buffer, if required.  Either way,
			 * set `buffer' variable.
			 */
			i = *lengthp;			/* Get callers length */
			*lengthp = propp->prop_len;	/* Set callers length */

			switch (prop_op) {

			case PROP_LEN_AND_VAL_ALLOC:

				if (prealloc == NULL) {
					buffer = kmem_alloc(propp->prop_len,
					    KM_NOSLEEP);
				} else {
					buffer = prealloc;
				}

				if (buffer == NULL)  {
					mutex_exit(&(DEVI(dip)->devi_lock));
					cmn_err(CE_CONT, prop_no_mem_msg, name);
					return (DDI_PROP_NO_MEMORY);
				}
				/* Set callers buf ptr */
				*(caddr_t *)valuep = buffer;
				break;

			case PROP_LEN_AND_VAL_BUF:

				if (propp->prop_len > (i)) {
					mutex_exit(&(DEVI(dip)->devi_lock));
					return (DDI_PROP_BUF_TOO_SMALL);
				}

				buffer = valuep;  /* Get callers buf ptr */
				break;

			default:
				break;
			}

			/*
			 * Do the copy.
			 */
			if (buffer != NULL)
				bcopy(propp->prop_val, buffer, propp->prop_len);
			mutex_exit(&(DEVI(dip)->devi_lock));
			return (DDI_PROP_SUCCESS);
		}

		mutex_exit(&(DEVI(dip)->devi_lock));
		if (prealloc)
			kmem_free(prealloc, plength);
		prealloc = NULL;

		/*
		 * Prop not found, call parent bus_ops to deal with possible
		 * h/w layer (possible PROM defined props, etc.) and to
		 * possibly ascend the hierarchy, if allowed by flags.
		 */
		pdip = (dev_info_t *)DEVI(dip)->devi_parent;

		/*
		 * One last call for the root driver PROM props?
		 */
		if (dip == ddi_root_node())  {
			return (ddi_bus_prop_op(dev, dip, dip, prop_op,
			    flags, name, valuep, (int *)lengthp));
		}

		/*
		 * We may have been called to check for properties
		 * within a single devinfo node that has no parent -
		 * see make_prop()
		 */
		if (pdip == NULL) {
			ASSERT((flags &
			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM)) ==
			    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM));
			return (DDI_PROP_NOT_FOUND);
		}

		/*
		 * Instead of recursing, we do iterative calls up the tree.
		 * As a bit of optimization, skip the bus_op level if the
		 * node is a s/w node and if the parent's bus_prop_op function
		 * is `ddi_bus_prop_op', because we know that in this case,
		 * this function does nothing.
		 *
		 * 4225415: If the parent isn't attached, or the child
		 * hasn't been named by the parent yet, use the default
		 * ddi_bus_prop_op as a proxy for the parent.  This
		 * allows property lookups in any child/parent state to
		 * include 'prom' and inherited properties, even when
		 * there are no drivers attached to the child or parent.
		 */

		bop = ddi_bus_prop_op;
		if (i_ddi_devi_attached(pdip) &&
		    (i_ddi_node_state(dip) >= DS_INITIALIZED))
			bop = DEVI(pdip)->devi_ops->devo_bus_ops->bus_prop_op;

		i = DDI_PROP_NOT_FOUND;

		if ((bop != ddi_bus_prop_op) || ndi_dev_is_prom_node(dip)) {
			i = (*bop)(dev, pdip, dip, prop_op,
			    flags | DDI_PROP_DONTPASS,
			    name, valuep, lengthp);
		}

		if ((flags & DDI_PROP_DONTPASS) ||
		    (i != DDI_PROP_NOT_FOUND))
			return (i);

		dip = pdip;
	}
	/*NOTREACHED*/
}


/*
 * ddi_prop_op: The basic property operator for drivers.
 *
 * In ddi_prop_op, the type of valuep is interpreted based on prop_op:
 *
 *	prop_op			valuep
 *	------			------
 *
 *	PROP_LEN		<unused>
 *
 *	PROP_LEN_AND_VAL_BUF	Pointer to callers buffer
 *
 *	PROP_LEN_AND_VAL_ALLOC	Address of callers pointer (will be set to
 *				address of allocated buffer, if successful)
 */
int
ddi_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
    char *name, caddr_t valuep, int *lengthp)
{
	int	i;

	ASSERT((mod_flags & DDI_PROP_TYPE_MASK) == 0);

	/*
	 * If this was originally an LDI prop lookup then we bail here.
	 * The reason is that the LDI property lookup interfaces first call
	 * a drivers prop_op() entry point to allow it to override
	 * properties.  But if we've made it here, then the driver hasn't
	 * overriden any properties.  We don't want to continue with the
	 * property search here because we don't have any type inforamtion.
	 * When we return failure, the LDI interfaces will then proceed to
	 * call the typed property interfaces to look up the property.
	 */
	if (mod_flags & DDI_PROP_DYNAMIC)
		return (DDI_PROP_NOT_FOUND);

	/*
	 * check for pre-typed property consumer asking for typed property:
	 * see e_ddi_getprop_int64.
	 */
	if (mod_flags & DDI_PROP_CONSUMER_TYPED)
		mod_flags |= DDI_PROP_TYPE_INT64;
	mod_flags |= DDI_PROP_TYPE_ANY;

	i = ddi_prop_search_common(dev, dip, prop_op,
	    mod_flags, name, valuep, (uint_t *)lengthp);
	if (i == DDI_PROP_FOUND_1275)
		return (DDI_PROP_SUCCESS);
	return (i);
}

/*
 * ddi_prop_op_nblocks_blksize: The basic property operator for drivers that
 * maintain size in number of blksize blocks.  Provides a dynamic property
 * implementation for size oriented properties based on nblocks64 and blksize
 * values passed in by the driver.  Fallback to ddi_prop_op if the nblocks64
 * is too large.  This interface should not be used with a nblocks64 that
 * represents the driver's idea of how to represent unknown, if nblocks is
 * unknown use ddi_prop_op.
 */
int
ddi_prop_op_nblocks_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    int mod_flags, char *name, caddr_t valuep, int *lengthp,
    uint64_t nblocks64, uint_t blksize)
{
	uint64_t size64;
	int	blkshift;

	/* convert block size to shift value */
	ASSERT(BIT_ONLYONESET(blksize));
	blkshift = highbit(blksize) - 1;

	/*
	 * There is no point in supporting nblocks64 values that don't have
	 * an accurate uint64_t byte count representation.
	 */
	if (nblocks64 >= (UINT64_MAX >> blkshift))
		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
		    name, valuep, lengthp));

	size64 = nblocks64 << blkshift;
	return (ddi_prop_op_size_blksize(dev, dip, prop_op, mod_flags,
	    name, valuep, lengthp, size64, blksize));
}

/*
 * ddi_prop_op_nblocks: ddi_prop_op_nblocks_blksize with DEV_BSIZE blksize.
 */
int
ddi_prop_op_nblocks(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t nblocks64)
{
	return (ddi_prop_op_nblocks_blksize(dev, dip, prop_op,
	    mod_flags, name, valuep, lengthp, nblocks64, DEV_BSIZE));
}

/*
 * ddi_prop_op_size_blksize: The basic property operator for block drivers that
 * maintain size in bytes. Provides a of dynamic property implementation for
 * size oriented properties based on size64 value and blksize passed in by the
 * driver.  Fallback to ddi_prop_op if the size64 is too large. This interface
 * should not be used with a size64 that represents the driver's idea of how
 * to represent unknown, if size is unknown use ddi_prop_op.
 *
 * NOTE: the legacy "nblocks"/"size" properties are treated as 32-bit unsigned
 * integers. While the most likely interface to request them ([bc]devi_size)
 * is declared int (signed) there is no enforcement of this, which means we
 * can't enforce limitations here without risking regression.
 */
int
ddi_prop_op_size_blksize(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64,
    uint_t blksize)
{
	uint64_t nblocks64;
	int	callers_length;
	caddr_t	buffer;
	int	blkshift;

	/*
	 * This is a kludge to support capture of size(9P) pure dynamic
	 * properties in snapshots for non-cmlb code (without exposing
	 * i_ddi_prop_dyn changes). When everyone uses cmlb, this code
	 * should be removed.
	 */
	if (i_ddi_prop_dyn_driver_get(dip) == NULL) {
		static i_ddi_prop_dyn_t prop_dyn_size[] = {
		    {"Size",		DDI_PROP_TYPE_INT64,	S_IFCHR},
		    {"Nblocks",		DDI_PROP_TYPE_INT64,	S_IFBLK},
		    {NULL}
		};
		i_ddi_prop_dyn_driver_set(dip, prop_dyn_size);
	}

	/* convert block size to shift value */
	ASSERT(BIT_ONLYONESET(blksize));
	blkshift = highbit(blksize) - 1;

	/* compute DEV_BSIZE nblocks value */
	nblocks64 = size64 >> blkshift;

	/* get callers length, establish length of our dynamic properties */
	callers_length = *lengthp;

	if (strcmp(name, "Nblocks") == 0)
		*lengthp = sizeof (uint64_t);
	else if (strcmp(name, "Size") == 0)
		*lengthp = sizeof (uint64_t);
	else if ((strcmp(name, "nblocks") == 0) && (nblocks64 < UINT_MAX))
		*lengthp = sizeof (uint32_t);
	else if ((strcmp(name, "size") == 0) && (size64 < UINT_MAX))
		*lengthp = sizeof (uint32_t);
	else if ((strcmp(name, "blksize") == 0) && (blksize < UINT_MAX))
		*lengthp = sizeof (uint32_t);
	else {
		/* fallback to ddi_prop_op */
		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
		    name, valuep, lengthp));
	}

	/* service request for the length of the property */
	if (prop_op == PROP_LEN)
		return (DDI_PROP_SUCCESS);

	switch (prop_op) {
	case PROP_LEN_AND_VAL_ALLOC:
		if ((buffer = kmem_alloc(*lengthp,
		    (mod_flags & DDI_PROP_CANSLEEP) ?
		    KM_SLEEP : KM_NOSLEEP)) == NULL)
			return (DDI_PROP_NO_MEMORY);

		*(caddr_t *)valuep = buffer;	/* set callers buf ptr */
		break;

	case PROP_LEN_AND_VAL_BUF:
		/* the length of the property and the request must match */
		if (callers_length != *lengthp)
			return (DDI_PROP_INVAL_ARG);

		buffer = valuep;		/* get callers buf ptr */
		break;

	default:
		return (DDI_PROP_INVAL_ARG);
	}

	/* transfer the value into the buffer */
	if (strcmp(name, "Nblocks") == 0)
		*((uint64_t *)buffer) = nblocks64;
	else if (strcmp(name, "Size") == 0)
		*((uint64_t *)buffer) = size64;
	else if (strcmp(name, "nblocks") == 0)
		*((uint32_t *)buffer) = (uint32_t)nblocks64;
	else if (strcmp(name, "size") == 0)
		*((uint32_t *)buffer) = (uint32_t)size64;
	else if (strcmp(name, "blksize") == 0)
		*((uint32_t *)buffer) = (uint32_t)blksize;
	return (DDI_PROP_SUCCESS);
}

/*
 * ddi_prop_op_size: ddi_prop_op_size_blksize with DEV_BSIZE block size.
 */
int
ddi_prop_op_size(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
    int mod_flags, char *name, caddr_t valuep, int *lengthp, uint64_t size64)
{
	return (ddi_prop_op_size_blksize(dev, dip, prop_op,
	    mod_flags, name, valuep, lengthp, size64, DEV_BSIZE));
}

/*
 * Variable length props...
 */

/*
 * ddi_getlongprop:	Get variable length property len+val into a buffer
 *		allocated by property provider via kmem_alloc. Requester
 *		is responsible for freeing returned property via kmem_free.
 *
 *	Arguments:
 *
 *	dev_t:	Input:	dev_t of property.
 *	dip:	Input:	dev_info_t pointer of child.
 *	flags:	Input:	Possible flag modifiers are:
 *		DDI_PROP_DONTPASS:	Don't pass to parent if prop not found.
 *		DDI_PROP_CANSLEEP:	Memory allocation may sleep.
 *	name:	Input:	name of property.
 *	valuep:	Output:	Addr of callers buffer pointer.
 *	lengthp:Output:	*lengthp will contain prop length on exit.
 *
 *	Possible Returns:
 *
 *		DDI_PROP_SUCCESS:	Prop found and returned.
 *		DDI_PROP_NOT_FOUND:	Prop not found
 *		DDI_PROP_UNDEFINED:	Prop explicitly undefined.
 *		DDI_PROP_NO_MEMORY:	Prop found, but unable to alloc mem.
 */

int
ddi_getlongprop(dev_t dev, dev_info_t *dip, int flags,
    char *name, caddr_t valuep, int *lengthp)
{
	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_ALLOC,
	    flags, name, valuep, lengthp));
}

/*
 *
 * ddi_getlongprop_buf:		Get long prop into pre-allocated callers
 *				buffer. (no memory allocation by provider).
 *
 *	dev_t:	Input:	dev_t of property.
 *	dip:	Input:	dev_info_t pointer of child.
 *	flags:	Input:	DDI_PROP_DONTPASS or NULL
 *	name:	Input:	name of property
 *	valuep:	Input:	ptr to callers buffer.
 *	lengthp:I/O:	ptr to length of callers buffer on entry,
 *			actual length of property on exit.
 *
 *	Possible returns:
 *
 *		DDI_PROP_SUCCESS	Prop found and returned
 *		DDI_PROP_NOT_FOUND	Prop not found
 *		DDI_PROP_UNDEFINED	Prop explicitly undefined.
 *		DDI_PROP_BUF_TOO_SMALL	Prop found, callers buf too small,
 *					no value returned, but actual prop
 *					length returned in *lengthp
 *
 */

int
ddi_getlongprop_buf(dev_t dev, dev_info_t *dip, int flags,
    char *name, caddr_t valuep, int *lengthp)
{
	return (ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
	    flags, name, valuep, lengthp));
}

/*
 * Integer/boolean sized props.
 *
 * Call is value only... returns found boolean or int sized prop value or
 * defvalue if prop not found or is wrong length or is explicitly undefined.
 * Only flag is DDI_PROP_DONTPASS...
 *
 * By convention, this interface returns boolean (0) sized properties
 * as value (int)1.
 *
 * This never returns an error, if property not found or specifically
 * undefined, the input `defvalue' is returned.
 */

int
ddi_getprop(dev_t dev, dev_info_t *dip, int flags, char *name, int defvalue)
{
	int	propvalue = defvalue;
	int	proplength = sizeof (int);
	int	error;

	error = ddi_prop_op(dev, dip, PROP_LEN_AND_VAL_BUF,
	    flags, name, (caddr_t)&propvalue, &proplength);

	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
		propvalue = 1;

	return (propvalue);
}

/*
 * Get prop length interface: flags are 0 or DDI_PROP_DONTPASS
 * if returns DDI_PROP_SUCCESS, length returned in *lengthp.
 */

int
ddi_getproplen(dev_t dev, dev_info_t *dip, int flags, char *name, int *lengthp)
{
	return (ddi_prop_op(dev, dip, PROP_LEN, flags, name, NULL, lengthp));
}

/*
 * Allocate a struct prop_driver_data, along with 'size' bytes
 * for decoded property data.  This structure is freed by
 * calling ddi_prop_free(9F).
 */
static void *
ddi_prop_decode_alloc(size_t size, void (*prop_free)(struct prop_driver_data *))
{
	struct prop_driver_data *pdd;

	/*
	 * Allocate a structure with enough memory to store the decoded data.
	 */
	pdd = kmem_zalloc(sizeof (struct prop_driver_data) + size, KM_SLEEP);
	pdd->pdd_size = (sizeof (struct prop_driver_data) + size);
	pdd->pdd_prop_free = prop_free;

	/*
	 * Return a pointer to the location to put the decoded data.
	 */
	return ((void *)((caddr_t)pdd + sizeof (struct prop_driver_data)));
}

/*
 * Allocated the memory needed to store the encoded data in the property
 * handle.
 */
static int
ddi_prop_encode_alloc(prop_handle_t *ph, size_t size)
{
	/*
	 * If size is zero, then set data to NULL and size to 0.  This
	 * is a boolean property.
	 */
	if (size == 0) {
		ph->ph_size = 0;
		ph->ph_data = NULL;
		ph->ph_cur_pos = NULL;
		ph->ph_save_pos = NULL;
	} else {
		if (ph->ph_flags == DDI_PROP_DONTSLEEP) {
			ph->ph_data = kmem_zalloc(size, KM_NOSLEEP);
			if (ph->ph_data == NULL)
				return (DDI_PROP_NO_MEMORY);
		} else
			ph->ph_data = kmem_zalloc(size, KM_SLEEP);
		ph->ph_size = size;
		ph->ph_cur_pos = ph->ph_data;
		ph->ph_save_pos = ph->ph_data;
	}
	return (DDI_PROP_SUCCESS);
}

/*
 * Free the space allocated by the lookup routines.  Each lookup routine
 * returns a pointer to the decoded data to the driver.  The driver then
 * passes this pointer back to us.  This data actually lives in a struct
 * prop_driver_data.  We use negative indexing to find the beginning of
 * the structure and then free the entire structure using the size and
 * the free routine stored in the structure.
 */
void
ddi_prop_free(void *datap)
{
	struct prop_driver_data *pdd;

	/*
	 * Get the structure
	 */
	pdd = (struct prop_driver_data *)
	    ((caddr_t)datap - sizeof (struct prop_driver_data));
	/*
	 * Call the free routine to free it
	 */
	(*pdd->pdd_prop_free)(pdd);
}

/*
 * Free the data associated with an array of ints,
 * allocated with ddi_prop_decode_alloc().
 */
static void
ddi_prop_free_ints(struct prop_driver_data *pdd)
{
	kmem_free(pdd, pdd->pdd_size);
}

/*
 * Free a single string property or a single string contained within
 * the argv style return value of an array of strings.
 */
static void
ddi_prop_free_string(struct prop_driver_data *pdd)
{
	kmem_free(pdd, pdd->pdd_size);

}

/*
 * Free an array of strings.
 */
static void
ddi_prop_free_strings(struct prop_driver_data *pdd)
{
	kmem_free(pdd, pdd->pdd_size);
}

/*
 * Free the data associated with an array of bytes.
 */
static void
ddi_prop_free_bytes(struct prop_driver_data *pdd)
{
	kmem_free(pdd, pdd->pdd_size);
}

/*
 * Reset the current location pointer in the property handle to the
 * beginning of the data.
 */
void
ddi_prop_reset_pos(prop_handle_t *ph)
{
	ph->ph_cur_pos = ph->ph_data;
	ph->ph_save_pos = ph->ph_data;
}

/*
 * Restore the current location pointer in the property handle to the
 * saved position.
 */
void
ddi_prop_save_pos(prop_handle_t *ph)
{
	ph->ph_save_pos = ph->ph_cur_pos;
}

/*
 * Save the location that the current location pointer is pointing to..
 */
void
ddi_prop_restore_pos(prop_handle_t *ph)
{
	ph->ph_cur_pos = ph->ph_save_pos;
}

/*
 * Property encode/decode functions
 */

/*
 * Decode a single integer property
 */
static int
ddi_prop_fm_decode_int(prop_handle_t *ph, void *data, uint_t *nelements)
{
	int	i;
	int	tmp;

	/*
	 * If there is nothing to decode return an error
	 */
	if (ph->ph_size == 0)
		return (DDI_PROP_END_OF_DATA);

	/*
	 * Decode the property as a single integer and return it
	 * in data if we were able to decode it.
	 */
	i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, &tmp);
	if (i < DDI_PROP_RESULT_OK) {
		switch (i) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_DECODE);
		}
	}

	*(int *)data = tmp;
	*nelements = 1;
	return (DDI_PROP_SUCCESS);
}

/*
 * Decode a single 64 bit integer property
 */
static int
ddi_prop_fm_decode_int64(prop_handle_t *ph, void *data, uint_t *nelements)
{
	int	i;
	int64_t	tmp;

	/*
	 * If there is nothing to decode return an error
	 */
	if (ph->ph_size == 0)
		return (DDI_PROP_END_OF_DATA);

	/*
	 * Decode the property as a single integer and return it
	 * in data if we were able to decode it.
	 */
	i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, &tmp);
	if (i < DDI_PROP_RESULT_OK) {
		switch (i) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_DECODE);
		}
	}

	*(int64_t *)data = tmp;
	*nelements = 1;
	return (DDI_PROP_SUCCESS);
}

/*
 * Decode an array of integers property
 */
static int
ddi_prop_fm_decode_ints(prop_handle_t *ph, void *data, uint_t *nelements)
{
	int	i;
	int	cnt = 0;
	int	*tmp;
	int	*intp;
	int	n;

	/*
	 * Figure out how many array elements there are by going through the
	 * data without decoding it first and counting.
	 */
	for (;;) {
		i = DDI_PROP_INT(ph, DDI_PROP_CMD_SKIP, NULL);
		if (i < 0)
			break;
		cnt++;
	}

	/*
	 * If there are no elements return an error
	 */
	if (cnt == 0)
		return (DDI_PROP_END_OF_DATA);

	/*
	 * If we cannot skip through the data, we cannot decode it
	 */
	if (i == DDI_PROP_RESULT_ERROR)
		return (DDI_PROP_CANNOT_DECODE);

	/*
	 * Reset the data pointer to the beginning of the encoded data
	 */
	ddi_prop_reset_pos(ph);

	/*
	 * Allocated memory to store the decoded value in.
	 */
	intp = ddi_prop_decode_alloc((cnt * sizeof (int)),
	    ddi_prop_free_ints);

	/*
	 * Decode each element and place it in the space we just allocated
	 */
	tmp = intp;
	for (n = 0; n < cnt; n++, tmp++) {
		i = DDI_PROP_INT(ph, DDI_PROP_CMD_DECODE, tmp);
		if (i < DDI_PROP_RESULT_OK) {
			/*
			 * Free the space we just allocated
			 * and return an error.
			 */
			ddi_prop_free(intp);
			switch (i) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_DECODE);
			}
		}
	}

	*nelements = cnt;
	*(int **)data = intp;

	return (DDI_PROP_SUCCESS);
}

/*
 * Decode a 64 bit integer array property
 */
static int
ddi_prop_fm_decode_int64_array(prop_handle_t *ph, void *data, uint_t *nelements)
{
	int	i;
	int	n;
	int	cnt = 0;
	int64_t	*tmp;
	int64_t	*intp;

	/*
	 * Count the number of array elements by going
	 * through the data without decoding it.
	 */
	for (;;) {
		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_SKIP, NULL);
		if (i < 0)
			break;
		cnt++;
	}

	/*
	 * If there are no elements return an error
	 */
	if (cnt == 0)
		return (DDI_PROP_END_OF_DATA);

	/*
	 * If we cannot skip through the data, we cannot decode it
	 */
	if (i == DDI_PROP_RESULT_ERROR)
		return (DDI_PROP_CANNOT_DECODE);

	/*
	 * Reset the data pointer to the beginning of the encoded data
	 */
	ddi_prop_reset_pos(ph);

	/*
	 * Allocate memory to store the decoded value.
	 */
	intp = ddi_prop_decode_alloc((cnt * sizeof (int64_t)),
	    ddi_prop_free_ints);

	/*
	 * Decode each element and place it in the space allocated
	 */
	tmp = intp;
	for (n = 0; n < cnt; n++, tmp++) {
		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_DECODE, tmp);
		if (i < DDI_PROP_RESULT_OK) {
			/*
			 * Free the space we just allocated
			 * and return an error.
			 */
			ddi_prop_free(intp);
			switch (i) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_DECODE);
			}
		}
	}

	*nelements = cnt;
	*(int64_t **)data = intp;

	return (DDI_PROP_SUCCESS);
}

/*
 * Encode an array of integers property (Can be one element)
 */
int
ddi_prop_fm_encode_ints(prop_handle_t *ph, void *data, uint_t nelements)
{
	int	i;
	int	*tmp;
	int	cnt;
	int	size;

	/*
	 * If there is no data, we cannot do anything
	 */
	if (nelements == 0)
		return (DDI_PROP_CANNOT_ENCODE);

	/*
	 * Get the size of an encoded int.
	 */
	size = DDI_PROP_INT(ph, DDI_PROP_CMD_GET_ESIZE, NULL);

	if (size < DDI_PROP_RESULT_OK) {
		switch (size) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_ENCODE);
		}
	}

	/*
	 * Allocate space in the handle to store the encoded int.
	 */
	if (ddi_prop_encode_alloc(ph, size * nelements) !=
	    DDI_PROP_SUCCESS)
		return (DDI_PROP_NO_MEMORY);

	/*
	 * Encode the array of ints.
	 */
	tmp = (int *)data;
	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
		i = DDI_PROP_INT(ph, DDI_PROP_CMD_ENCODE, tmp);
		if (i < DDI_PROP_RESULT_OK) {
			switch (i) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_ENCODE);
			}
		}
	}

	return (DDI_PROP_SUCCESS);
}


/*
 * Encode a 64 bit integer array property
 */
int
ddi_prop_fm_encode_int64(prop_handle_t *ph, void *data, uint_t nelements)
{
	int i;
	int cnt;
	int size;
	int64_t *tmp;

	/*
	 * If there is no data, we cannot do anything
	 */
	if (nelements == 0)
		return (DDI_PROP_CANNOT_ENCODE);

	/*
	 * Get the size of an encoded 64 bit int.
	 */
	size = DDI_PROP_INT64(ph, DDI_PROP_CMD_GET_ESIZE, NULL);

	if (size < DDI_PROP_RESULT_OK) {
		switch (size) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_ENCODE);
		}
	}

	/*
	 * Allocate space in the handle to store the encoded int.
	 */
	if (ddi_prop_encode_alloc(ph, size * nelements) !=
	    DDI_PROP_SUCCESS)
		return (DDI_PROP_NO_MEMORY);

	/*
	 * Encode the array of ints.
	 */
	tmp = (int64_t *)data;
	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
		i = DDI_PROP_INT64(ph, DDI_PROP_CMD_ENCODE, tmp);
		if (i < DDI_PROP_RESULT_OK) {
			switch (i) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_ENCODE);
			}
		}
	}

	return (DDI_PROP_SUCCESS);
}

/*
 * Decode a single string property
 */
static int
ddi_prop_fm_decode_string(prop_handle_t *ph, void *data, uint_t *nelements)
{
	char		*tmp;
	char		*str;
	int		i;
	int		size;

	/*
	 * If there is nothing to decode return an error
	 */
	if (ph->ph_size == 0)
		return (DDI_PROP_END_OF_DATA);

	/*
	 * Get the decoded size of the encoded string.
	 */
	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
	if (size < DDI_PROP_RESULT_OK) {
		switch (size) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_DECODE);
		}
	}

	/*
	 * Allocated memory to store the decoded value in.
	 */
	str = ddi_prop_decode_alloc((size_t)size, ddi_prop_free_string);

	ddi_prop_reset_pos(ph);

	/*
	 * Decode the str and place it in the space we just allocated
	 */
	tmp = str;
	i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, tmp);
	if (i < DDI_PROP_RESULT_OK) {
		/*
		 * Free the space we just allocated
		 * and return an error.
		 */
		ddi_prop_free(str);
		switch (i) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_DECODE);
		}
	}

	*(char **)data = str;
	*nelements = 1;

	return (DDI_PROP_SUCCESS);
}

/*
 * Decode an array of strings.
 */
int
ddi_prop_fm_decode_strings(prop_handle_t *ph, void *data, uint_t *nelements)
{
	int		cnt = 0;
	char		**strs;
	char		**tmp;
	char		*ptr;
	int		i;
	int		n;
	int		size;
	size_t		nbytes;

	/*
	 * Figure out how many array elements there are by going through the
	 * data without decoding it first and counting.
	 */
	for (;;) {
		i = DDI_PROP_STR(ph, DDI_PROP_CMD_SKIP, NULL);
		if (i < 0)
			break;
		cnt++;
	}

	/*
	 * If there are no elements return an error
	 */
	if (cnt == 0)
		return (DDI_PROP_END_OF_DATA);

	/*
	 * If we cannot skip through the data, we cannot decode it
	 */
	if (i == DDI_PROP_RESULT_ERROR)
		return (DDI_PROP_CANNOT_DECODE);

	/*
	 * Reset the data pointer to the beginning of the encoded data
	 */
	ddi_prop_reset_pos(ph);

	/*
	 * Figure out how much memory we need for the sum total
	 */
	nbytes = (cnt + 1) * sizeof (char *);

	for (n = 0; n < cnt; n++) {
		/*
		 * Get the decoded size of the current encoded string.
		 */
		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
		if (size < DDI_PROP_RESULT_OK) {
			switch (size) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_DECODE);
			}
		}

		nbytes += size;
	}

	/*
	 * Allocate memory in which to store the decoded strings.
	 */
	strs = ddi_prop_decode_alloc(nbytes, ddi_prop_free_strings);

	/*
	 * Set up pointers for each string by figuring out yet
	 * again how long each string is.
	 */
	ddi_prop_reset_pos(ph);
	ptr = (caddr_t)strs + ((cnt + 1) * sizeof (char *));
	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
		/*
		 * Get the decoded size of the current encoded string.
		 */
		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_DSIZE, NULL);
		if (size < DDI_PROP_RESULT_OK) {
			ddi_prop_free(strs);
			switch (size) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_DECODE);
			}
		}

		*tmp = ptr;
		ptr += size;
	}

	/*
	 * String array is terminated by a NULL
	 */
	*tmp = NULL;

	/*
	 * Finally, we can decode each string
	 */
	ddi_prop_reset_pos(ph);
	for (tmp = strs, n = 0; n < cnt; n++, tmp++) {
		i = DDI_PROP_STR(ph, DDI_PROP_CMD_DECODE, *tmp);
		if (i < DDI_PROP_RESULT_OK) {
			/*
			 * Free the space we just allocated
			 * and return an error
			 */
			ddi_prop_free(strs);
			switch (i) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_DECODE);
			}
		}
	}

	*(char ***)data = strs;
	*nelements = cnt;

	return (DDI_PROP_SUCCESS);
}

/*
 * Encode a string.
 */
int
ddi_prop_fm_encode_string(prop_handle_t *ph, void *data, uint_t nelements)
{
	char		**tmp;
	int		size;
	int		i;

	/*
	 * If there is no data, we cannot do anything
	 */
	if (nelements == 0)
		return (DDI_PROP_CANNOT_ENCODE);

	/*
	 * Get the size of the encoded string.
	 */
	tmp = (char **)data;
	size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
	if (size < DDI_PROP_RESULT_OK) {
		switch (size) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_ENCODE);
		}
	}

	/*
	 * Allocate space in the handle to store the encoded string.
	 */
	if (ddi_prop_encode_alloc(ph, size) != DDI_PROP_SUCCESS)
		return (DDI_PROP_NO_MEMORY);

	ddi_prop_reset_pos(ph);

	/*
	 * Encode the string.
	 */
	tmp = (char **)data;
	i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
	if (i < DDI_PROP_RESULT_OK) {
		switch (i) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_ENCODE);
		}
	}

	return (DDI_PROP_SUCCESS);
}


/*
 * Encode an array of strings.
 */
int
ddi_prop_fm_encode_strings(prop_handle_t *ph, void *data, uint_t nelements)
{
	int		cnt = 0;
	char		**tmp;
	int		size;
	uint_t		total_size;
	int		i;

	/*
	 * If there is no data, we cannot do anything
	 */
	if (nelements == 0)
		return (DDI_PROP_CANNOT_ENCODE);

	/*
	 * Get the total size required to encode all the strings.
	 */
	total_size = 0;
	tmp = (char **)data;
	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
		size = DDI_PROP_STR(ph, DDI_PROP_CMD_GET_ESIZE, *tmp);
		if (size < DDI_PROP_RESULT_OK) {
			switch (size) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_ENCODE);
			}
		}
		total_size += (uint_t)size;
	}

	/*
	 * Allocate space in the handle to store the encoded strings.
	 */
	if (ddi_prop_encode_alloc(ph, total_size) != DDI_PROP_SUCCESS)
		return (DDI_PROP_NO_MEMORY);

	ddi_prop_reset_pos(ph);

	/*
	 * Encode the array of strings.
	 */
	tmp = (char **)data;
	for (cnt = 0; cnt < nelements; cnt++, tmp++) {
		i = DDI_PROP_STR(ph, DDI_PROP_CMD_ENCODE, *tmp);
		if (i < DDI_PROP_RESULT_OK) {
			switch (i) {
			case DDI_PROP_RESULT_EOF:
				return (DDI_PROP_END_OF_DATA);

			case DDI_PROP_RESULT_ERROR:
				return (DDI_PROP_CANNOT_ENCODE);
			}
		}
	}

	return (DDI_PROP_SUCCESS);
}


/*
 * Decode an array of bytes.
 */
static int
ddi_prop_fm_decode_bytes(prop_handle_t *ph, void *data, uint_t *nelements)
{
	uchar_t		*tmp;
	int		nbytes;
	int		i;

	/*
	 * If there are no elements return an error
	 */
	if (ph->ph_size == 0)
		return (DDI_PROP_END_OF_DATA);

	/*
	 * Get the size of the encoded array of bytes.
	 */
	nbytes = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_DSIZE,
	    data, ph->ph_size);
	if (nbytes < DDI_PROP_RESULT_OK) {
		switch (nbytes) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_DECODE);
		}
	}

	/*
	 * Allocated memory to store the decoded value in.
	 */
	tmp = ddi_prop_decode_alloc(nbytes, ddi_prop_free_bytes);

	/*
	 * Decode each element and place it in the space we just allocated
	 */
	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_DECODE, tmp, nbytes);
	if (i < DDI_PROP_RESULT_OK) {
		/*
		 * Free the space we just allocated
		 * and return an error
		 */
		ddi_prop_free(tmp);
		switch (i) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_DECODE);
		}
	}

	*(uchar_t **)data = tmp;
	*nelements = nbytes;

	return (DDI_PROP_SUCCESS);
}

/*
 * Encode an array of bytes.
 */
int
ddi_prop_fm_encode_bytes(prop_handle_t *ph, void *data, uint_t nelements)
{
	int		size;
	int		i;

	/*
	 * If there are no elements, then this is a boolean property,
	 * so just create a property handle with no data and return.
	 */
	if (nelements == 0) {
		(void) ddi_prop_encode_alloc(ph, 0);
		return (DDI_PROP_SUCCESS);
	}

	/*
	 * Get the size of the encoded array of bytes.
	 */
	size = DDI_PROP_BYTES(ph, DDI_PROP_CMD_GET_ESIZE, (uchar_t *)data,
	    nelements);
	if (size < DDI_PROP_RESULT_OK) {
		switch (size) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_DECODE);
		}
	}

	/*
	 * Allocate space in the handle to store the encoded bytes.
	 */
	if (ddi_prop_encode_alloc(ph, (uint_t)size) != DDI_PROP_SUCCESS)
		return (DDI_PROP_NO_MEMORY);

	/*
	 * Encode the array of bytes.
	 */
	i = DDI_PROP_BYTES(ph, DDI_PROP_CMD_ENCODE, (uchar_t *)data,
	    nelements);
	if (i < DDI_PROP_RESULT_OK) {
		switch (i) {
		case DDI_PROP_RESULT_EOF:
			return (DDI_PROP_END_OF_DATA);

		case DDI_PROP_RESULT_ERROR:
			return (DDI_PROP_CANNOT_ENCODE);
		}
	}

	return (DDI_PROP_SUCCESS);
}

/*
 * OBP 1275 integer, string and byte operators.
 *
 * DDI_PROP_CMD_DECODE:
 *
 *	DDI_PROP_RESULT_ERROR:		cannot decode the data
 *	DDI_PROP_RESULT_EOF:		end of data
 *	DDI_PROP_OK:			data was decoded
 *
 * DDI_PROP_CMD_ENCODE:
 *
 *	DDI_PROP_RESULT_ERROR:		cannot encode the data
 *	DDI_PROP_RESULT_EOF:		end of data
 *	DDI_PROP_OK:			data was encoded
 *
 * DDI_PROP_CMD_SKIP:
 *
 *	DDI_PROP_RESULT_ERROR:		cannot skip the data
 *	DDI_PROP_RESULT_EOF:		end of data
 *	DDI_PROP_OK:			data was skipped
 *
 * DDI_PROP_CMD_GET_ESIZE:
 *
 *	DDI_PROP_RESULT_ERROR:		cannot get encoded size
 *	DDI_PROP_RESULT_EOF:		end of data
 *	> 0:				the encoded size
 *
 * DDI_PROP_CMD_GET_DSIZE:
 *
 *	DDI_PROP_RESULT_ERROR:		cannot get decoded size
 *	DDI_PROP_RESULT_EOF:		end of data
 *	> 0:				the decoded size
 */

/*
 * OBP 1275 integer operator
 *
 * OBP properties are a byte stream of data, so integers may not be
 * properly aligned.  Therefore we need to copy them one byte at a time.
 */
int
ddi_prop_1275_int(prop_handle_t *ph, uint_t cmd, int *data)
{
	int	i;

	switch (cmd) {
	case DDI_PROP_CMD_DECODE:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
			return (DDI_PROP_RESULT_ERROR);
		if (ph->ph_flags & PH_FROM_PROM) {
			i = MIN(ph->ph_size, PROP_1275_INT_SIZE);
			if ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
			    ph->ph_size - i))
				return (DDI_PROP_RESULT_ERROR);
		} else {
			if (ph->ph_size < sizeof (int) ||
			    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
			    ph->ph_size - sizeof (int))))
				return (DDI_PROP_RESULT_ERROR);
		}

		/*
		 * Copy the integer, using the implementation-specific
		 * copy function if the property is coming from the PROM.
		 */
		if (ph->ph_flags & PH_FROM_PROM) {
			*data = impl_ddi_prop_int_from_prom(
			    (uchar_t *)ph->ph_cur_pos,
			    (ph->ph_size < PROP_1275_INT_SIZE) ?
			    ph->ph_size : PROP_1275_INT_SIZE);
		} else {
			bcopy(ph->ph_cur_pos, data, sizeof (int));
		}

		/*
		 * Move the current location to the start of the next
		 * bit of undecoded data.
		 */
		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
		    PROP_1275_INT_SIZE;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_ENCODE:
		/*
		 * Check that there is room to encoded the data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
		    ph->ph_size < PROP_1275_INT_SIZE ||
		    ((int *)ph->ph_cur_pos > ((int *)ph->ph_data +
		    ph->ph_size - sizeof (int))))
			return (DDI_PROP_RESULT_ERROR);

		/*
		 * Encode the integer into the byte stream one byte at a
		 * time.
		 */
		bcopy(data, ph->ph_cur_pos, sizeof (int));

		/*
		 * Move the current location to the start of the next bit of
		 * space where we can store encoded data.
		 */
		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_SKIP:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
		    ph->ph_size < PROP_1275_INT_SIZE)
			return (DDI_PROP_RESULT_ERROR);


		if ((caddr_t)ph->ph_cur_pos ==
		    (caddr_t)ph->ph_data + ph->ph_size) {
			return (DDI_PROP_RESULT_EOF);
		} else if ((caddr_t)ph->ph_cur_pos >
		    (caddr_t)ph->ph_data + ph->ph_size) {
			return (DDI_PROP_RESULT_EOF);
		}

		/*
		 * Move the current location to the start of the next bit of
		 * undecoded data.
		 */
		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos + PROP_1275_INT_SIZE;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_GET_ESIZE:
		/*
		 * Return the size of an encoded integer on OBP
		 */
		return (PROP_1275_INT_SIZE);

	case DDI_PROP_CMD_GET_DSIZE:
		/*
		 * Return the size of a decoded integer on the system.
		 */
		return (sizeof (int));

	default:
#ifdef DEBUG
		panic("ddi_prop_1275_int: %x impossible", cmd);
		/*NOTREACHED*/
#else
		return (DDI_PROP_RESULT_ERROR);
#endif	/* DEBUG */
	}
}

/*
 * 64 bit integer operator.
 *
 * This is an extension, defined by Sun, to the 1275 integer
 * operator.  This routine handles the encoding/decoding of
 * 64 bit integer properties.
 */
int
ddi_prop_int64_op(prop_handle_t *ph, uint_t cmd, int64_t *data)
{

	switch (cmd) {
	case DDI_PROP_CMD_DECODE:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0)
			return (DDI_PROP_RESULT_ERROR);
		if (ph->ph_flags & PH_FROM_PROM) {
			return (DDI_PROP_RESULT_ERROR);
		} else {
			if (ph->ph_size < sizeof (int64_t) ||
			    ((int64_t *)ph->ph_cur_pos >
			    ((int64_t *)ph->ph_data +
			    ph->ph_size - sizeof (int64_t))))
				return (DDI_PROP_RESULT_ERROR);
		}
		/*
		 * Copy the integer, using the implementation-specific
		 * copy function if the property is coming from the PROM.
		 */
		if (ph->ph_flags & PH_FROM_PROM) {
			return (DDI_PROP_RESULT_ERROR);
		} else {
			bcopy(ph->ph_cur_pos, data, sizeof (int64_t));
		}

		/*
		 * Move the current location to the start of the next
		 * bit of undecoded data.
		 */
		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
		    sizeof (int64_t);
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_ENCODE:
		/*
		 * Check that there is room to encoded the data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
		    ph->ph_size < sizeof (int64_t) ||
		    ((int64_t *)ph->ph_cur_pos > ((int64_t *)ph->ph_data +
		    ph->ph_size - sizeof (int64_t))))
			return (DDI_PROP_RESULT_ERROR);

		/*
		 * Encode the integer into the byte stream one byte at a
		 * time.
		 */
		bcopy(data, ph->ph_cur_pos, sizeof (int64_t));

		/*
		 * Move the current location to the start of the next bit of
		 * space where we can store encoded data.
		 */
		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
		    sizeof (int64_t);
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_SKIP:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
		    ph->ph_size < sizeof (int64_t))
			return (DDI_PROP_RESULT_ERROR);

		if ((caddr_t)ph->ph_cur_pos ==
		    (caddr_t)ph->ph_data + ph->ph_size) {
			return (DDI_PROP_RESULT_EOF);
		} else if ((caddr_t)ph->ph_cur_pos >
		    (caddr_t)ph->ph_data + ph->ph_size) {
			return (DDI_PROP_RESULT_EOF);
		}

		/*
		 * Move the current location to the start of
		 * the next bit of undecoded data.
		 */
		ph->ph_cur_pos = (uchar_t *)ph->ph_cur_pos +
		    sizeof (int64_t);
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_GET_ESIZE:
		/*
		 * Return the size of an encoded integer on OBP
		 */
		return (sizeof (int64_t));

	case DDI_PROP_CMD_GET_DSIZE:
		/*
		 * Return the size of a decoded integer on the system.
		 */
		return (sizeof (int64_t));

	default:
#ifdef DEBUG
		panic("ddi_prop_int64_op: %x impossible", cmd);
		/*NOTREACHED*/
#else
		return (DDI_PROP_RESULT_ERROR);
#endif  /* DEBUG */
	}
}

/*
 * OBP 1275 string operator.
 *
 * OBP strings are NULL terminated.
 */
int
ddi_prop_1275_string(prop_handle_t *ph, uint_t cmd, char *data)
{
	int	n;
	char	*p;
	char	*end;

	switch (cmd) {
	case DDI_PROP_CMD_DECODE:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
			return (DDI_PROP_RESULT_ERROR);
		}

		/*
		 * Match DDI_PROP_CMD_GET_DSIZE logic for when to stop and
		 * how to NULL terminate result.
		 */
		p = (char *)ph->ph_cur_pos;
		end = (char *)ph->ph_data + ph->ph_size;
		if (p >= end)
			return (DDI_PROP_RESULT_EOF);

		while (p < end) {
			*data++ = *p;
			if (*p++ == 0) {	/* NULL from OBP */
				ph->ph_cur_pos = p;
				return (DDI_PROP_RESULT_OK);
			}
		}

		/*
		 * If OBP did not NULL terminate string, which happens
		 * (at least) for 'true'/'false' boolean values, account for
		 * the space and store null termination on decode.
		 */
		ph->ph_cur_pos = p;
		*data = 0;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_ENCODE:
		/*
		 * Check that there is room to encoded the data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
			return (DDI_PROP_RESULT_ERROR);
		}

		n = strlen(data) + 1;
		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
		    ph->ph_size - n)) {
			return (DDI_PROP_RESULT_ERROR);
		}

		/*
		 * Copy the NULL terminated string
		 */
		bcopy(data, ph->ph_cur_pos, n);

		/*
		 * Move the current location to the start of the next bit of
		 * space where we can store encoded data.
		 */
		ph->ph_cur_pos = (char *)ph->ph_cur_pos + n;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_SKIP:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0) {
			return (DDI_PROP_RESULT_ERROR);
		}

		/*
		 * Return the string length plus one for the NULL
		 * We know the size of the property, we need to
		 * ensure that the string is properly formatted,
		 * since we may be looking up random OBP data.
		 */
		p = (char *)ph->ph_cur_pos;
		end = (char *)ph->ph_data + ph->ph_size;
		if (p >= end)
			return (DDI_PROP_RESULT_EOF);

		while (p < end) {
			if (*p++ == 0) {	/* NULL from OBP */
				ph->ph_cur_pos = p;
				return (DDI_PROP_RESULT_OK);
			}
		}

		/*
		 * Accommodate the fact that OBP does not always NULL
		 * terminate strings.
		 */
		ph->ph_cur_pos = p;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_GET_ESIZE:
		/*
		 * Return the size of the encoded string on OBP.
		 */
		return (strlen(data) + 1);

	case DDI_PROP_CMD_GET_DSIZE:
		/*
		 * Return the string length plus one for the NULL.
		 * We know the size of the property, we need to
		 * ensure that the string is properly formatted,
		 * since we may be looking up random OBP data.
		 */
		p = (char *)ph->ph_cur_pos;
		end = (char *)ph->ph_data + ph->ph_size;
		if (p >= end)
			return (DDI_PROP_RESULT_EOF);

		for (n = 0; p < end; n++) {
			if (*p++ == 0) {	/* NULL from OBP */
				ph->ph_cur_pos = p;
				return (n + 1);
			}
		}

		/*
		 * If OBP did not NULL terminate string, which happens for
		 * 'true'/'false' boolean values, account for the space
		 * to store null termination here.
		 */
		ph->ph_cur_pos = p;
		return (n + 1);

	default:
#ifdef DEBUG
		panic("ddi_prop_1275_string: %x impossible", cmd);
		/*NOTREACHED*/
#else
		return (DDI_PROP_RESULT_ERROR);
#endif	/* DEBUG */
	}
}

/*
 * OBP 1275 byte operator
 *
 * Caller must specify the number of bytes to get.  OBP encodes bytes
 * as a byte so there is a 1-to-1 translation.
 */
int
ddi_prop_1275_bytes(prop_handle_t *ph, uint_t cmd, uchar_t *data,
    uint_t nelements)
{
	switch (cmd) {
	case DDI_PROP_CMD_DECODE:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
		    ph->ph_size < nelements ||
		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
		    ph->ph_size - nelements)))
			return (DDI_PROP_RESULT_ERROR);

		/*
		 * Copy out the bytes
		 */
		bcopy(ph->ph_cur_pos, data, nelements);

		/*
		 * Move the current location
		 */
		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_ENCODE:
		/*
		 * Check that there is room to encode the data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
		    ph->ph_size < nelements ||
		    ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
		    ph->ph_size - nelements)))
			return (DDI_PROP_RESULT_ERROR);

		/*
		 * Copy in the bytes
		 */
		bcopy(data, ph->ph_cur_pos, nelements);

		/*
		 * Move the current location to the start of the next bit of
		 * space where we can store encoded data.
		 */
		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_SKIP:
		/*
		 * Check that there is encoded data
		 */
		if (ph->ph_cur_pos == NULL || ph->ph_size == 0 ||
		    ph->ph_size < nelements)
			return (DDI_PROP_RESULT_ERROR);

		if ((char *)ph->ph_cur_pos > ((char *)ph->ph_data +
		    ph->ph_size - nelements))
			return (DDI_PROP_RESULT_EOF);

		/*
		 * Move the current location
		 */
		ph->ph_cur_pos = (char *)ph->ph_cur_pos + nelements;
		return (DDI_PROP_RESULT_OK);

	case DDI_PROP_CMD_GET_ESIZE:
		/*
		 * The size in bytes of the encoded size is the
		 * same as the decoded size provided by the caller.
		 */
		return (nelements);

	case DDI_PROP_CMD_GET_DSIZE:
		/*
		 * Just return the number of bytes specified by the caller.
		 */
		return (nelements);

	default:
#ifdef DEBUG
		panic("ddi_prop_1275_bytes: %x impossible", cmd);
		/*NOTREACHED*/
#else
		return (DDI_PROP_RESULT_ERROR);
#endif	/* DEBUG */
	}
}

/*
 * Used for properties that come from the OBP, hardware configuration files,
 * or that are created by calls to ddi_prop_update(9F).
 */
static struct prop_handle_ops prop_1275_ops = {
	ddi_prop_1275_int,
	ddi_prop_1275_string,
	ddi_prop_1275_bytes,
	ddi_prop_int64_op
};


/*
 * Interface to create/modify a managed property on child's behalf...
 * Flags interpreted are:
 *	DDI_PROP_CANSLEEP:	Allow memory allocation to sleep.
 *	DDI_PROP_SYSTEM_DEF:	Manipulate system list rather than driver list.
 *
 * Use same dev_t when modifying or undefining a property.
 * Search for properties with DDI_DEV_T_ANY to match first named
 * property on the list.
 *
 * Properties are stored LIFO and subsequently will match the first
 * `matching' instance.
 */

/*
 * ddi_prop_add:	Add a software defined property
 */

/*
 * define to get a new ddi_prop_t.
 * km_flags are KM_SLEEP or KM_NOSLEEP.
 */

#define	DDI_NEW_PROP_T(km_flags)	\
	(kmem_zalloc(sizeof (ddi_prop_t), km_flags))

static int
ddi_prop_add(dev_t dev, dev_info_t *dip, int flags,
    char *name, caddr_t value, int length)
{
	ddi_prop_t	*new_propp, *propp;
	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
	int		km_flags = KM_NOSLEEP;
	int		name_buf_len;

	/*
	 * If dev_t is DDI_DEV_T_ANY or name's length is zero return error.
	 */

	if (dev == DDI_DEV_T_ANY || name == (char *)0 || strlen(name) == 0)
		return (DDI_PROP_INVAL_ARG);

	if (flags & DDI_PROP_CANSLEEP)
		km_flags = KM_SLEEP;

	if (flags & DDI_PROP_SYSTEM_DEF)
		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
	else if (flags & DDI_PROP_HW_DEF)
		list_head = &(DEVI(dip)->devi_hw_prop_ptr);

	if ((new_propp = DDI_NEW_PROP_T(km_flags)) == NULL)  {
		cmn_err(CE_CONT, prop_no_mem_msg, name);
		return (DDI_PROP_NO_MEMORY);
	}

	/*
	 * If dev is major number 0, then we need to do a ddi_name_to_major
	 * to get the real major number for the device.  This needs to be
	 * done because some drivers need to call ddi_prop_create in their
	 * attach routines but they don't have a dev.  By creating the dev
	 * ourself if the major number is 0, drivers will not have to know what
	 * their major number.	They can just create a dev with major number
	 * 0 and pass it in.  For device 0, we will be doing a little extra
	 * work by recreating the same dev that we already have, but its the
	 * price you pay :-).
	 *
	 * This fixes bug #1098060.
	 */
	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN) {
		new_propp->prop_dev =
		    makedevice(ddi_name_to_major(DEVI(dip)->devi_binding_name),
		    getminor(dev));
	} else
		new_propp->prop_dev = dev;

	/*
	 * Allocate space for property name and copy it in...
	 */

	name_buf_len = strlen(name) + 1;
	new_propp->prop_name = kmem_alloc(name_buf_len, km_flags);
	if (new_propp->prop_name == 0)	{
		kmem_free(new_propp, sizeof (ddi_prop_t));
		cmn_err(CE_CONT, prop_no_mem_msg, name);
		return (DDI_PROP_NO_MEMORY);
	}
	bcopy(name, new_propp->prop_name, name_buf_len);

	/*
	 * Set the property type
	 */
	new_propp->prop_flags = flags & DDI_PROP_TYPE_MASK;

	/*
	 * Set length and value ONLY if not an explicit property undefine:
	 * NOTE: value and length are zero for explicit undefines.
	 */

	if (flags & DDI_PROP_UNDEF_IT) {
		new_propp->prop_flags |= DDI_PROP_UNDEF_IT;
	} else {
		if ((new_propp->prop_len = length) != 0) {
			new_propp->prop_val = kmem_alloc(length, km_flags);
			if (new_propp->prop_val == 0)  {
				kmem_free(new_propp->prop_name, name_buf_len);
				kmem_free(new_propp, sizeof (ddi_prop_t));
				cmn_err(CE_CONT, prop_no_mem_msg, name);
				return (DDI_PROP_NO_MEMORY);
			}
			bcopy(value, new_propp->prop_val, length);
		}
	}

	/*
	 * Link property into beginning of list. (Properties are LIFO order.)
	 */

	mutex_enter(&(DEVI(dip)->devi_lock));
	propp = *list_head;
	new_propp->prop_next = propp;
	*list_head = new_propp;
	mutex_exit(&(DEVI(dip)->devi_lock));
	return (DDI_PROP_SUCCESS);
}


/*
 * ddi_prop_change:	Modify a software managed property value
 *
 *			Set new length and value if found.
 *			returns DDI_PROP_INVAL_ARG if dev is DDI_DEV_T_ANY or
 *			input name is the NULL string.
 *			returns DDI_PROP_NO_MEMORY if unable to allocate memory
 *
 *			Note: an undef can be modified to be a define,
 *			(you can't go the other way.)
 */

static int
ddi_prop_change(dev_t dev, dev_info_t *dip, int flags,
    char *name, caddr_t value, int length)
{
	ddi_prop_t	*propp;
	ddi_prop_t	**ppropp;
	caddr_t		p = NULL;

	if ((dev == DDI_DEV_T_ANY) || (name == NULL) || (strlen(name) == 0))
		return (DDI_PROP_INVAL_ARG);

	/*
	 * Preallocate buffer, even if we don't need it...
	 */
	if (length != 0)  {
		p = kmem_alloc(length, (flags & DDI_PROP_CANSLEEP) ?
		    KM_SLEEP : KM_NOSLEEP);
		if (p == NULL)	{
			cmn_err(CE_CONT, prop_no_mem_msg, name);
			return (DDI_PROP_NO_MEMORY);
		}
	}

	/*
	 * If the dev_t value contains DDI_MAJOR_T_UNKNOWN for the major
	 * number, a real dev_t value should be created based upon the dip's
	 * binding driver.  See ddi_prop_add...
	 */
	if (getmajor(dev) == DDI_MAJOR_T_UNKNOWN)
		dev = makedevice(
		    ddi_name_to_major(DEVI(dip)->devi_binding_name),
		    getminor(dev));

	/*
	 * Check to see if the property exists.  If so we modify it.
	 * Else we create it by calling ddi_prop_add().
	 */
	mutex_enter(&(DEVI(dip)->devi_lock));
	ppropp = &DEVI(dip)->devi_drv_prop_ptr;
	if (flags & DDI_PROP_SYSTEM_DEF)
		ppropp = &DEVI(dip)->devi_sys_prop_ptr;
	else if (flags & DDI_PROP_HW_DEF)
		ppropp = &DEVI(dip)->devi_hw_prop_ptr;

	if ((propp = i_ddi_prop_search(dev, name, flags, ppropp)) != NULL) {
		/*
		 * Need to reallocate buffer?  If so, do it
		 * carefully (reuse same space if new prop
		 * is same size and non-NULL sized).
		 */
		if (length != 0)
			bcopy(value, p, length);

		if (propp->prop_len != 0)
			kmem_free(propp->prop_val, propp->prop_len);

		propp->prop_len = length;
		propp->prop_val = p;
		propp->prop_flags &= ~DDI_PROP_UNDEF_IT;
		mutex_exit(&(DEVI(dip)->devi_lock));
		return (DDI_PROP_SUCCESS);
	}

	mutex_exit(&(DEVI(dip)->devi_lock));
	if (length != 0)
		kmem_free(p, length);

	return (ddi_prop_add(dev, dip, flags, name, value, length));
}

/*
 * Common update routine used to update and encode a property.	Creates
 * a property handle, calls the property encode routine, figures out if
 * the property already exists and updates if it does.	Otherwise it
 * creates if it does not exist.
 */
int
ddi_prop_update_common(dev_t match_dev, dev_info_t *dip, int flags,
    char *name, void *data, uint_t nelements,
    int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
{
	prop_handle_t	ph;
	int		rval;
	uint_t		ourflags;

	/*
	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
	 * return error.
	 */
	if (match_dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
		return (DDI_PROP_INVAL_ARG);

	/*
	 * Create the handle
	 */
	ph.ph_data = NULL;
	ph.ph_cur_pos = NULL;
	ph.ph_save_pos = NULL;
	ph.ph_size = 0;
	ph.ph_ops = &prop_1275_ops;

	/*
	 * ourflags:
	 * For compatibility with the old interfaces.  The old interfaces
	 * didn't sleep by default and slept when the flag was set.  These
	 * interfaces to the opposite.	So the old interfaces now set the
	 * DDI_PROP_DONTSLEEP flag by default which tells us not to sleep.
	 *
	 * ph.ph_flags:
	 * Blocked data or unblocked data allocation
	 * for ph.ph_data in ddi_prop_encode_alloc()
	 */
	if (flags & DDI_PROP_DONTSLEEP) {
		ourflags = flags;
		ph.ph_flags = DDI_PROP_DONTSLEEP;
	} else {
		ourflags = flags | DDI_PROP_CANSLEEP;
		ph.ph_flags = DDI_PROP_CANSLEEP;
	}

	/*
	 * Encode the data and store it in the property handle by
	 * calling the prop_encode routine.
	 */
	if ((rval = (*prop_create)(&ph, data, nelements)) !=
	    DDI_PROP_SUCCESS) {
		if (rval == DDI_PROP_NO_MEMORY)
			cmn_err(CE_CONT, prop_no_mem_msg, name);
		if (ph.ph_size != 0)
			kmem_free(ph.ph_data, ph.ph_size);
		return (rval);
	}

	/*
	 * The old interfaces use a stacking approach to creating
	 * properties.	If we are being called from the old interfaces,
	 * the DDI_PROP_STACK_CREATE flag will be set, so we just do a
	 * create without checking.
	 */
	if (flags & DDI_PROP_STACK_CREATE) {
		rval = ddi_prop_add(match_dev, dip,
		    ourflags, name, ph.ph_data, ph.ph_size);
	} else {
		rval = ddi_prop_change(match_dev, dip,
		    ourflags, name, ph.ph_data, ph.ph_size);
	}

	/*
	 * Free the encoded data allocated in the prop_encode routine.
	 */
	if (ph.ph_size != 0)
		kmem_free(ph.ph_data, ph.ph_size);

	return (rval);
}


/*
 * ddi_prop_create:	Define a managed property:
 *			See above for details.
 */

int
ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
    char *name, caddr_t value, int length)
{
	if (!(flag & DDI_PROP_CANSLEEP)) {
		flag |= DDI_PROP_DONTSLEEP;
#ifdef DDI_PROP_DEBUG
		if (length != 0)
			cmn_err(CE_NOTE, "!ddi_prop_create: interface obsolete,"
			    "use ddi_prop_update (prop = %s, node = %s%d)",
			    name, ddi_driver_name(dip), ddi_get_instance(dip));
#endif /* DDI_PROP_DEBUG */
	}
	flag &= ~DDI_PROP_SYSTEM_DEF;
	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
	return (ddi_prop_update_common(dev, dip, flag, name,
	    value, length, ddi_prop_fm_encode_bytes));
}

int
e_ddi_prop_create(dev_t dev, dev_info_t *dip, int flag,
    char *name, caddr_t value, int length)
{
	if (!(flag & DDI_PROP_CANSLEEP))
		flag |= DDI_PROP_DONTSLEEP;
	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE | DDI_PROP_TYPE_ANY;
	return (ddi_prop_update_common(dev, dip, flag,
	    name, value, length, ddi_prop_fm_encode_bytes));
}

int
ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
    char *name, caddr_t value, int length)
{
	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);

	/*
	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
	 * return error.
	 */
	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
		return (DDI_PROP_INVAL_ARG);

	if (!(flag & DDI_PROP_CANSLEEP))
		flag |= DDI_PROP_DONTSLEEP;
	flag &= ~DDI_PROP_SYSTEM_DEF;
	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_NOTPROM), name) == 0)
		return (DDI_PROP_NOT_FOUND);

	return (ddi_prop_update_common(dev, dip,
	    (flag | DDI_PROP_TYPE_BYTE), name,
	    value, length, ddi_prop_fm_encode_bytes));
}

int
e_ddi_prop_modify(dev_t dev, dev_info_t *dip, int flag,
    char *name, caddr_t value, int length)
{
	ASSERT((flag & DDI_PROP_TYPE_MASK) == 0);

	/*
	 * If dev_t is DDI_DEV_T_ANY or name's length is zero,
	 * return error.
	 */
	if (dev == DDI_DEV_T_ANY || name == NULL || strlen(name) == 0)
		return (DDI_PROP_INVAL_ARG);

	if (ddi_prop_exists(dev, dip, (flag | DDI_PROP_SYSTEM_DEF), name) == 0)
		return (DDI_PROP_NOT_FOUND);

	if (!(flag & DDI_PROP_CANSLEEP))
		flag |= DDI_PROP_DONTSLEEP;
	return (ddi_prop_update_common(dev, dip,
	    (flag | DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE),
	    name, value, length, ddi_prop_fm_encode_bytes));
}


/*
 * Common lookup routine used to lookup and decode a property.
 * Creates a property handle, searches for the raw encoded data,
 * fills in the handle, and calls the property decode functions
 * passed in.
 *
 * This routine is not static because ddi_bus_prop_op() which lives in
 * ddi_impl.c calls it.  No driver should be calling this routine.
 */
int
ddi_prop_lookup_common(dev_t match_dev, dev_info_t *dip,
    uint_t flags, char *name, void *data, uint_t *nelements,
    int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
{
	int		rval;
	uint_t		ourflags;
	prop_handle_t	ph;

	if ((match_dev == DDI_DEV_T_NONE) ||
	    (name == NULL) || (strlen(name) == 0))
		return (DDI_PROP_INVAL_ARG);

	ourflags = (flags & DDI_PROP_DONTSLEEP) ? flags :
	    flags | DDI_PROP_CANSLEEP;

	/*
	 * Get the encoded data
	 */
	bzero(&ph, sizeof (prop_handle_t));

	if ((flags & DDI_UNBND_DLPI2) || (flags & DDI_PROP_ROOTNEX_GLOBAL)) {
		/*
		 * For rootnex and unbound dlpi style-2 devices, index into
		 * the devnames' array and search the global
		 * property list.
		 */
		ourflags &= ~DDI_UNBND_DLPI2;
		rval = i_ddi_prop_search_global(match_dev,
		    ourflags, name, &ph.ph_data, &ph.ph_size);
	} else {
		rval = ddi_prop_search_common(match_dev, dip,
		    PROP_LEN_AND_VAL_ALLOC, ourflags, name,
		    &ph.ph_data, &ph.ph_size);

	}

	if (rval != DDI_PROP_SUCCESS && rval != DDI_PROP_FOUND_1275) {
		ASSERT(ph.ph_data == NULL);
		ASSERT(ph.ph_size == 0);
		return (rval);
	}

	/*
	 * If the encoded data came from a OBP or software
	 * use the 1275 OBP decode/encode routines.
	 */
	ph.ph_cur_pos = ph.ph_data;
	ph.ph_save_pos = ph.ph_data;
	ph.ph_ops = &prop_1275_ops;
	ph.ph_flags = (rval == DDI_PROP_FOUND_1275) ? PH_FROM_PROM : 0;

	rval = (*prop_decoder)(&ph, data, nelements);

	/*
	 * Free the encoded data
	 */
	if (ph.ph_size != 0)
		kmem_free(ph.ph_data, ph.ph_size);

	return (rval);
}

/*
 * Lookup and return an array of composite properties.  The driver must
 * provide the decode routine.
 */
int
ddi_prop_lookup(dev_t match_dev, dev_info_t *dip,
    uint_t flags, char *name, void *data, uint_t *nelements,
    int (*prop_decoder)(prop_handle_t *, void *data, uint_t *nelements))
{
	return (ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_COMPOSITE), name,
	    data, nelements, prop_decoder));
}

/*
 * Return 1 if a property exists (no type checking done).
 * Return 0 if it does not exist.
 */
int
ddi_prop_exists(dev_t match_dev, dev_info_t *dip, uint_t flags, char *name)
{
	int	i;
	uint_t	x = 0;

	i = ddi_prop_search_common(match_dev, dip, PROP_EXISTS,
	    flags | DDI_PROP_TYPE_MASK, name, NULL, &x);
	return (i == DDI_PROP_SUCCESS || i == DDI_PROP_FOUND_1275);
}


/*
 * Update an array of composite properties.  The driver must
 * provide the encode routine.
 */
int
ddi_prop_update(dev_t match_dev, dev_info_t *dip,
    char *name, void *data, uint_t nelements,
    int (*prop_create)(prop_handle_t *, void *data, uint_t nelements))
{
	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_COMPOSITE,
	    name, data, nelements, prop_create));
}

/*
 * Get a single integer or boolean property and return it.
 * If the property does not exists, or cannot be decoded,
 * then return the defvalue passed in.
 *
 * This routine always succeeds.
 */
int
ddi_prop_get_int(dev_t match_dev, dev_info_t *dip, uint_t flags,
    char *name, int defvalue)
{
	int	data;
	uint_t	nelements;
	int	rval;

	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
#ifdef DEBUG
		if (dip != NULL) {
			cmn_err(CE_WARN, "ddi_prop_get_int: invalid flag"
			    " 0x%x (prop = %s, node = %s%d)", flags,
			    name, ddi_driver_name(dip), ddi_get_instance(dip));
		}
#endif /* DEBUG */
		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
	}

	if ((rval = ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_INT), name, &data, &nelements,
	    ddi_prop_fm_decode_int)) != DDI_PROP_SUCCESS) {
		if (rval == DDI_PROP_END_OF_DATA)
			data = 1;
		else
			data = defvalue;
	}
	return (data);
}

/*
 * Get a single 64 bit integer or boolean property and return it.
 * If the property does not exists, or cannot be decoded,
 * then return the defvalue passed in.
 *
 * This routine always succeeds.
 */
int64_t
ddi_prop_get_int64(dev_t match_dev, dev_info_t *dip, uint_t flags,
    char *name, int64_t defvalue)
{
	int64_t	data;
	uint_t	nelements;
	int	rval;

	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
#ifdef DEBUG
		if (dip != NULL) {
			cmn_err(CE_WARN, "ddi_prop_get_int64: invalid flag"
			    " 0x%x (prop = %s, node = %s%d)", flags,
			    name, ddi_driver_name(dip), ddi_get_instance(dip));
		}
#endif /* DEBUG */
		return (DDI_PROP_INVAL_ARG);
	}

	if ((rval = ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
	    name, &data, &nelements, ddi_prop_fm_decode_int64))
	    != DDI_PROP_SUCCESS) {
		if (rval == DDI_PROP_END_OF_DATA)
			data = 1;
		else
			data = defvalue;
	}
	return (data);
}

/*
 * Get an array of integer property
 */
int
ddi_prop_lookup_int_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
    char *name, int **data, uint_t *nelements)
{
	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
#ifdef DEBUG
		if (dip != NULL) {
			cmn_err(CE_WARN, "ddi_prop_lookup_int_array: "
			    "invalid flag 0x%x (prop = %s, node = %s%d)",
			    flags, name, ddi_driver_name(dip),
			    ddi_get_instance(dip));
		}
#endif /* DEBUG */
		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
	}

	return (ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_INT), name, data,
	    nelements, ddi_prop_fm_decode_ints));
}

/*
 * Get an array of 64 bit integer properties
 */
int
ddi_prop_lookup_int64_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
    char *name, int64_t **data, uint_t *nelements)
{
	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
#ifdef DEBUG
		if (dip != NULL) {
			cmn_err(CE_WARN, "ddi_prop_lookup_int64_array: "
			    "invalid flag 0x%x (prop = %s, node = %s%d)",
			    flags, name, ddi_driver_name(dip),
			    ddi_get_instance(dip));
		}
#endif /* DEBUG */
		return (DDI_PROP_INVAL_ARG);
	}

	return (ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_INT64 | DDI_PROP_NOTPROM),
	    name, data, nelements, ddi_prop_fm_decode_int64_array));
}

/*
 * Update a single integer property.  If the property exists on the drivers
 * property list it updates, else it creates it.
 */
int
ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
    char *name, int data)
{
	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
	    name, &data, 1, ddi_prop_fm_encode_ints));
}

/*
 * Update a single 64 bit integer property.
 * Update the driver property list if it exists, else create it.
 */
int
ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
    char *name, int64_t data)
{
	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
	    name, &data, 1, ddi_prop_fm_encode_int64));
}

int
e_ddi_prop_update_int(dev_t match_dev, dev_info_t *dip,
    char *name, int data)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
	    name, &data, 1, ddi_prop_fm_encode_ints));
}

int
e_ddi_prop_update_int64(dev_t match_dev, dev_info_t *dip,
    char *name, int64_t data)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
	    name, &data, 1, ddi_prop_fm_encode_int64));
}

/*
 * Update an array of integer property.  If the property exists on the drivers
 * property list it updates, else it creates it.
 */
int
ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
    char *name, int *data, uint_t nelements)
{
	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT,
	    name, data, nelements, ddi_prop_fm_encode_ints));
}

/*
 * Update an array of 64 bit integer properties.
 * Update the driver property list if it exists, else create it.
 */
int
ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
    char *name, int64_t *data, uint_t nelements)
{
	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_INT64,
	    name, data, nelements, ddi_prop_fm_encode_int64));
}

int
e_ddi_prop_update_int64_array(dev_t match_dev, dev_info_t *dip,
    char *name, int64_t *data, uint_t nelements)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT64,
	    name, data, nelements, ddi_prop_fm_encode_int64));
}

int
e_ddi_prop_update_int_array(dev_t match_dev, dev_info_t *dip,
    char *name, int *data, uint_t nelements)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_INT,
	    name, data, nelements, ddi_prop_fm_encode_ints));
}

/*
 * Get a single string property.
 */
int
ddi_prop_lookup_string(dev_t match_dev, dev_info_t *dip, uint_t flags,
    char *name, char **data)
{
	uint_t x;

	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
#ifdef DEBUG
		if (dip != NULL) {
			cmn_err(CE_WARN, "%s: invalid flag 0x%x "
			    "(prop = %s, node = %s%d); invalid bits ignored",
			    "ddi_prop_lookup_string", flags, name,
			    ddi_driver_name(dip), ddi_get_instance(dip));
		}
#endif /* DEBUG */
		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
	}

	return (ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_STRING), name, data,
	    &x, ddi_prop_fm_decode_string));
}

/*
 * Get an array of strings property.
 */
int
ddi_prop_lookup_string_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
    char *name, char ***data, uint_t *nelements)
{
	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
#ifdef DEBUG
		if (dip != NULL) {
			cmn_err(CE_WARN, "ddi_prop_lookup_string_array: "
			    "invalid flag 0x%x (prop = %s, node = %s%d)",
			    flags, name, ddi_driver_name(dip),
			    ddi_get_instance(dip));
		}
#endif /* DEBUG */
		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
	}

	return (ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_STRING), name, data,
	    nelements, ddi_prop_fm_decode_strings));
}

/*
 * Update a single string property.
 */
int
ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
    char *name, char *data)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_TYPE_STRING, name, &data, 1,
	    ddi_prop_fm_encode_string));
}

int
e_ddi_prop_update_string(dev_t match_dev, dev_info_t *dip,
    char *name, char *data)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
	    name, &data, 1, ddi_prop_fm_encode_string));
}


/*
 * Update an array of strings property.
 */
int
ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
    char *name, char **data, uint_t nelements)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_TYPE_STRING, name, data, nelements,
	    ddi_prop_fm_encode_strings));
}

int
e_ddi_prop_update_string_array(dev_t match_dev, dev_info_t *dip,
    char *name, char **data, uint_t nelements)
{
	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_STRING,
	    name, data, nelements,
	    ddi_prop_fm_encode_strings));
}


/*
 * Get an array of bytes property.
 */
int
ddi_prop_lookup_byte_array(dev_t match_dev, dev_info_t *dip, uint_t flags,
    char *name, uchar_t **data, uint_t *nelements)
{
	if (flags & ~(DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
	    LDI_DEV_T_ANY | DDI_UNBND_DLPI2 | DDI_PROP_ROOTNEX_GLOBAL)) {
#ifdef DEBUG
		if (dip != NULL) {
			cmn_err(CE_WARN, "ddi_prop_lookup_byte_array: "
			    " invalid flag 0x%x (prop = %s, node = %s%d)",
			    flags, name, ddi_driver_name(dip),
			    ddi_get_instance(dip));
		}
#endif /* DEBUG */
		flags &= DDI_PROP_DONTPASS | DDI_PROP_NOTPROM |
		    LDI_DEV_T_ANY | DDI_UNBND_DLPI2;
	}

	return (ddi_prop_lookup_common(match_dev, dip,
	    (flags | DDI_PROP_TYPE_BYTE), name, data,
	    nelements, ddi_prop_fm_decode_bytes));
}

/*
 * Update an array of bytes property.
 */
int
ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
    char *name, uchar_t *data, uint_t nelements)
{
	if (nelements == 0)
		return (DDI_PROP_INVAL_ARG);

	return (ddi_prop_update_common(match_dev, dip, DDI_PROP_TYPE_BYTE,
	    name, data, nelements, ddi_prop_fm_encode_bytes));
}


int
e_ddi_prop_update_byte_array(dev_t match_dev, dev_info_t *dip,
    char *name, uchar_t *data, uint_t nelements)
{
	if (nelements == 0)
		return (DDI_PROP_INVAL_ARG);

	return (ddi_prop_update_common(match_dev, dip,
	    DDI_PROP_SYSTEM_DEF | DDI_PROP_TYPE_BYTE,
	    name, data, nelements, ddi_prop_fm_encode_bytes));
}


/*
 * ddi_prop_remove_common:	Undefine a managed property:
 *			Input dev_t must match dev_t when defined.
 *			Returns DDI_PROP_NOT_FOUND, possibly.
 *			DDI_PROP_INVAL_ARG is also possible if dev is
 *			DDI_DEV_T_ANY or incoming name is the NULL string.
 */
int
ddi_prop_remove_common(dev_t dev, dev_info_t *dip, char *name, int flag)
{
	ddi_prop_t	**list_head = &(DEVI(dip)->devi_drv_prop_ptr);
	ddi_prop_t	*propp;
	ddi_prop_t	*lastpropp = NULL;

	if ((dev == DDI_DEV_T_ANY) || (name == (char *)0) ||
	    (strlen(name) == 0)) {
		return (DDI_PROP_INVAL_ARG);
	}

	if (flag & DDI_PROP_SYSTEM_DEF)
		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
	else if (flag & DDI_PROP_HW_DEF)
		list_head = &(DEVI(dip)->devi_hw_prop_ptr);

	mutex_enter(&(DEVI(dip)->devi_lock));

	for (propp = *list_head; propp != NULL; propp = propp->prop_next)  {
		if (DDI_STRSAME(propp->prop_name, name) &&
		    (dev == propp->prop_dev)) {
			/*
			 * Unlink this propp allowing for it to
			 * be first in the list:
			 */

			if (lastpropp == NULL)
				*list_head = propp->prop_next;
			else
				lastpropp->prop_next = propp->prop_next;

			mutex_exit(&(DEVI(dip)->devi_lock));

			/*
			 * Free memory and return...
			 */
			kmem_free(propp->prop_name,
			    strlen(propp->prop_name) + 1);
			if (propp->prop_len != 0)
				kmem_free(propp->prop_val, propp->prop_len);
			kmem_free(propp, sizeof (ddi_prop_t));
			return (DDI_PROP_SUCCESS);
		}
		lastpropp = propp;
	}
	mutex_exit(&(DEVI(dip)->devi_lock));
	return (DDI_PROP_NOT_FOUND);
}

int
ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
{
	return (ddi_prop_remove_common(dev, dip, name, 0));
}

int
e_ddi_prop_remove(dev_t dev, dev_info_t *dip, char *name)
{
	return (ddi_prop_remove_common(dev, dip, name, DDI_PROP_SYSTEM_DEF));
}

/*
 * e_ddi_prop_list_delete: remove a list of properties
 *	Note that the caller needs to provide the required protection
 *	(eg. devi_lock if these properties are still attached to a devi)
 */
void
e_ddi_prop_list_delete(ddi_prop_t *props)
{
	i_ddi_prop_list_delete(props);
}

/*
 * ddi_prop_remove_all_common:
 *	Used before unloading a driver to remove
 *	all properties. (undefines all dev_t's props.)
 *	Also removes `explicitly undefined' props.
 *	No errors possible.
 */
void
ddi_prop_remove_all_common(dev_info_t *dip, int flag)
{
	ddi_prop_t	**list_head;

	mutex_enter(&(DEVI(dip)->devi_lock));
	if (flag & DDI_PROP_SYSTEM_DEF) {
		list_head = &(DEVI(dip)->devi_sys_prop_ptr);
	} else if (flag & DDI_PROP_HW_DEF) {
		list_head = &(DEVI(dip)->devi_hw_prop_ptr);
	} else {
		list_head = &(DEVI(dip)->devi_drv_prop_ptr);
	}
	i_ddi_prop_list_delete(*list_head);
	*list_head = NULL;
	mutex_exit(&(DEVI(dip)->devi_lock));
}


/*
 * ddi_prop_remove_all:		Remove all driver prop definitions.
 */

void
ddi_prop_remove_all(dev_info_t *dip)
{
	i_ddi_prop_dyn_driver_set(dip, NULL);
	ddi_prop_remove_all_common(dip, 0);
}

/*
 * e_ddi_prop_remove_all:	Remove all system prop definitions.
 */

void
e_ddi_prop_remove_all(dev_info_t *dip)
{
	ddi_prop_remove_all_common(dip, (int)DDI_PROP_SYSTEM_DEF);
}


/*
 * ddi_prop_undefine:	Explicitly undefine a property.  Property
 *			searches which match this property return
 *			the error code DDI_PROP_UNDEFINED.
 *
 *			Use ddi_prop_remove to negate effect of
 *			ddi_prop_undefine
 *
 *			See above for error returns.
 */

int
ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
{
	if (!(flag & DDI_PROP_CANSLEEP))
		flag |= DDI_PROP_DONTSLEEP;
	flag |= DDI_PROP_STACK_CREATE | DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
	return (ddi_prop_update_common(dev, dip, flag,
	    name, NULL, 0, ddi_prop_fm_encode_bytes));
}

int
e_ddi_prop_undefine(dev_t dev, dev_info_t *dip, int flag, char *name)
{
	if (!(flag & DDI_PROP_CANSLEEP))
		flag |= DDI_PROP_DONTSLEEP;
	flag |= DDI_PROP_SYSTEM_DEF | DDI_PROP_STACK_CREATE |
	    DDI_PROP_UNDEF_IT | DDI_PROP_TYPE_ANY;
	return (ddi_prop_update_common(dev, dip, flag,
	    name, NULL, 0, ddi_prop_fm_encode_bytes));
}

/*
 * Support for gathering dynamic properties in devinfo snapshot.
 */
void
i_ddi_prop_dyn_driver_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
{
	DEVI(dip)->devi_prop_dyn_driver = dp;
}

i_ddi_prop_dyn_t *
i_ddi_prop_dyn_driver_get(dev_info_t *dip)
{
	return (DEVI(dip)->devi_prop_dyn_driver);
}

void
i_ddi_prop_dyn_parent_set(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
{
	DEVI(dip)->devi_prop_dyn_parent = dp;
}

i_ddi_prop_dyn_t *
i_ddi_prop_dyn_parent_get(dev_info_t *dip)
{
	return (DEVI(dip)->devi_prop_dyn_parent);
}

void
i_ddi_prop_dyn_cache_invalidate(dev_info_t *dip, i_ddi_prop_dyn_t *dp)
{
	/* for now we invalidate the entire cached snapshot */
	if (dip && dp)
		i_ddi_di_cache_invalidate();
}

/* ARGSUSED */
void
ddi_prop_cache_invalidate(dev_t dev, dev_info_t *dip, char *name, int flags)
{
	/* for now we invalidate the entire cached snapshot */
	i_ddi_di_cache_invalidate();
}


/*
 * Code to search hardware layer (PROM), if it exists, on behalf of child.
 *
 * if input dip != child_dip, then call is on behalf of child
 * to search PROM, do it via ddi_prop_search_common() and ascend only
 * if allowed.
 *
 * if input dip == ch_dip (child_dip), call is on behalf of root driver,
 * to search for PROM defined props only.
 *
 * Note that the PROM search is done only if the requested dev
 * is either DDI_DEV_T_ANY or DDI_DEV_T_NONE. PROM properties
 * have no associated dev, thus are automatically associated with
 * DDI_DEV_T_NONE.
 *
 * Modifying flag DDI_PROP_NOTPROM inhibits the search in the h/w layer.
 *
 * Returns DDI_PROP_FOUND_1275 if found to indicate to framework
 * that the property resides in the prom.
 */
int
impl_ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
    ddi_prop_op_t prop_op, int mod_flags,
    char *name, caddr_t valuep, int *lengthp)
{
	int	len;
	caddr_t buffer = NULL;

	/*
	 * If requested dev is DDI_DEV_T_NONE or DDI_DEV_T_ANY, then
	 * look in caller's PROM if it's a self identifying device...
	 *
	 * Note that this is very similar to ddi_prop_op, but we
	 * search the PROM instead of the s/w defined properties,
	 * and we are called on by the parent driver to do this for
	 * the child.
	 */

	if (((dev == DDI_DEV_T_NONE) || (dev == DDI_DEV_T_ANY)) &&
	    ndi_dev_is_prom_node(ch_dip) &&
	    ((mod_flags & DDI_PROP_NOTPROM) == 0)) {
		len = prom_getproplen((pnode_t)DEVI(ch_dip)->devi_nodeid, name);
		if (len == -1) {
			return (DDI_PROP_NOT_FOUND);
		}

		/*
		 * If exists only request, we're done
		 */
		if (prop_op == PROP_EXISTS) {
			return (DDI_PROP_FOUND_1275);
		}

		/*
		 * If length only request or prop length == 0, get out
		 */
		if ((prop_op == PROP_LEN) || (len == 0)) {
			*lengthp = len;
			return (DDI_PROP_FOUND_1275);
		}

		/*
		 * Allocate buffer if required... (either way `buffer'
		 * is receiving address).
		 */

		switch (prop_op) {

		case PROP_LEN_AND_VAL_ALLOC:

			buffer = kmem_alloc((size_t)len,
			    mod_flags & DDI_PROP_CANSLEEP ?
			    KM_SLEEP : KM_NOSLEEP);
			if (buffer == NULL) {
				return (DDI_PROP_NO_MEMORY);
			}
			*(caddr_t *)valuep = buffer;
			break;

		case PROP_LEN_AND_VAL_BUF:

			if (len > (*lengthp)) {
				*lengthp = len;
				return (DDI_PROP_BUF_TOO_SMALL);
			}

			buffer = valuep;
			break;

		default:
			break;
		}

		/*
		 * Call the PROM function to do the copy.
		 */
		(void) prom_getprop((pnode_t)DEVI(ch_dip)->devi_nodeid,
		    name, buffer);

		*lengthp = len; /* return the actual length to the caller */
		(void) impl_fix_props(dip, ch_dip, name, len, buffer);
		return (DDI_PROP_FOUND_1275);
	}

	return (DDI_PROP_NOT_FOUND);
}

/*
 * The ddi_bus_prop_op default bus nexus prop op function.
 *
 * Code to search hardware layer (PROM), if it exists,
 * on behalf of child, then, if appropriate, ascend and check
 * my own software defined properties...
 */
int
ddi_bus_prop_op(dev_t dev, dev_info_t *dip, dev_info_t *ch_dip,
    ddi_prop_op_t prop_op, int mod_flags,
    char *name, caddr_t valuep, int *lengthp)
{
	int	error;

	error = impl_ddi_bus_prop_op(dev, dip, ch_dip, prop_op, mod_flags,
	    name, valuep, lengthp);

	if (error == DDI_PROP_SUCCESS || error == DDI_PROP_FOUND_1275 ||
	    error == DDI_PROP_BUF_TOO_SMALL)
		return (error);

	if (error == DDI_PROP_NO_MEMORY) {
		cmn_err(CE_CONT, prop_no_mem_msg, name);
		return (DDI_PROP_NO_MEMORY);
	}

	/*
	 * Check the 'options' node as a last resort
	 */
	if ((mod_flags & DDI_PROP_DONTPASS) != 0)
		return (DDI_PROP_NOT_FOUND);

	if (ch_dip == ddi_root_node())	{
		/*
		 * As a last resort, when we've reached
		 * the top and still haven't found the
		 * property, see if the desired property
		 * is attached to the options node.
		 *
		 * The options dip is attached right after boot.
		 */
		ASSERT(options_dip != NULL);
		/*
		 * Force the "don't pass" flag to *just* see
		 * what the options node has to offer.
		 */
		return (ddi_prop_search_common(dev, options_dip, prop_op,
		    mod_flags|DDI_PROP_DONTPASS, name, valuep,
		    (uint_t *)lengthp));
	}

	/*
	 * Otherwise, continue search with parent's s/w defined properties...
	 * NOTE: Using `dip' in following call increments the level.
	 */

	return (ddi_prop_search_common(dev, dip, prop_op, mod_flags,
	    name, valuep, (uint_t *)lengthp));
}

/*
 * External property functions used by other parts of the kernel...
 */

/*
 * e_ddi_getlongprop: See comments for ddi_get_longprop.
 */

int
e_ddi_getlongprop(dev_t dev, vtype_t type, char *name, int flags,
    caddr_t valuep, int *lengthp)
{
	_NOTE(ARGUNUSED(type))
	dev_info_t *devi;
	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_ALLOC;
	int error;

	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
		return (DDI_PROP_NOT_FOUND);

	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
	ddi_release_devi(devi);
	return (error);
}

/*
 * e_ddi_getlongprop_buf:	See comments for ddi_getlongprop_buf.
 */

int
e_ddi_getlongprop_buf(dev_t dev, vtype_t type, char *name, int flags,
    caddr_t valuep, int *lengthp)
{
	_NOTE(ARGUNUSED(type))
	dev_info_t *devi;
	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
	int error;

	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
		return (DDI_PROP_NOT_FOUND);

	error = cdev_prop_op(dev, devi, prop_op, flags, name, valuep, lengthp);
	ddi_release_devi(devi);
	return (error);
}

/*
 * e_ddi_getprop:	See comments for ddi_getprop.
 */
int
e_ddi_getprop(dev_t dev, vtype_t type, char *name, int flags, int defvalue)
{
	_NOTE(ARGUNUSED(type))
	dev_info_t *devi;
	ddi_prop_op_t prop_op = PROP_LEN_AND_VAL_BUF;
	int	propvalue = defvalue;
	int	proplength = sizeof (int);
	int	error;

	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
		return (defvalue);

	error = cdev_prop_op(dev, devi, prop_op,
	    flags, name, (caddr_t)&propvalue, &proplength);
	ddi_release_devi(devi);

	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
		propvalue = 1;

	return (propvalue);
}

/*
 * e_ddi_getprop_int64:
 *
 * This is a typed interfaces, but predates typed properties. With the
 * introduction of typed properties the framework tries to ensure
 * consistent use of typed interfaces. This is why TYPE_INT64 is not
 * part of TYPE_ANY.  E_ddi_getprop_int64 is a special case where a
 * typed interface invokes legacy (non-typed) interfaces:
 * cdev_prop_op(), prop_op(9E), ddi_prop_op(9F)).  In this case the
 * fact that TYPE_INT64 is not part of TYPE_ANY matters.  To support
 * this type of lookup as a single operation we invoke the legacy
 * non-typed interfaces with the special CONSUMER_TYPED bit set. The
 * framework ddi_prop_op(9F) implementation is expected to check for
 * CONSUMER_TYPED and, if set, expand type bits beyond TYPE_ANY
 * (currently TYPE_INT64).
 */
int64_t
e_ddi_getprop_int64(dev_t dev, vtype_t type, char *name,
    int flags, int64_t defvalue)
{
	_NOTE(ARGUNUSED(type))
	dev_info_t	*devi;
	ddi_prop_op_t	prop_op = PROP_LEN_AND_VAL_BUF;
	int64_t		propvalue = defvalue;
	int		proplength = sizeof (propvalue);
	int		error;

	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
		return (defvalue);

	error = cdev_prop_op(dev, devi, prop_op, flags |
	    DDI_PROP_CONSUMER_TYPED, name, (caddr_t)&propvalue, &proplength);
	ddi_release_devi(devi);

	if ((error == DDI_PROP_SUCCESS) && (proplength == 0))
		propvalue = 1;

	return (propvalue);
}

/*
 * e_ddi_getproplen:	See comments for ddi_getproplen.
 */
int
e_ddi_getproplen(dev_t dev, vtype_t type, char *name, int flags, int *lengthp)
{
	_NOTE(ARGUNUSED(type))
	dev_info_t *devi;
	ddi_prop_op_t prop_op = PROP_LEN;
	int error;

	if ((devi = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
		return (DDI_PROP_NOT_FOUND);

	error = cdev_prop_op(dev, devi, prop_op, flags, name, NULL, lengthp);
	ddi_release_devi(devi);
	return (error);
}

/*
 * Routines to get at elements of the dev_info structure
 */

/*
 * ddi_binding_name: Return the driver binding name of the devinfo node
 *		This is the name the OS used to bind the node to a driver.
 */
char *
ddi_binding_name(dev_info_t *dip)
{
	return (DEVI(dip)->devi_binding_name);
}

/*
 * ddi_driver_major: Return the major number of the driver that
 *	the supplied devinfo is bound to.  If not yet bound,
 *	DDI_MAJOR_T_NONE.
 *
 * When used by the driver bound to 'devi', this
 * function will reliably return the driver major number.
 * Other ways of determining the driver major number, such as
 *	major = ddi_name_to_major(ddi_get_name(devi));
 *	major = ddi_name_to_major(ddi_binding_name(devi));
 * can return a different result as the driver/alias binding
 * can change dynamically, and thus should be avoided.
 */
major_t
ddi_driver_major(dev_info_t *devi)
{
	return (DEVI(devi)->devi_major);
}

/*
 * ddi_driver_name: Return the normalized driver name. this is the
 *		actual driver name
 */
const char *
ddi_driver_name(dev_info_t *devi)
{
	major_t major;

	if ((major = ddi_driver_major(devi)) != DDI_MAJOR_T_NONE)
		return (ddi_major_to_name(major));

	return (ddi_node_name(devi));
}

/*
 * i_ddi_set_binding_name:	Set binding name.
 *
 *	Set the binding name to the given name.
 *	This routine is for use by the ddi implementation, not by drivers.
 */
void
i_ddi_set_binding_name(dev_info_t *dip, char *name)
{
	DEVI(dip)->devi_binding_name = name;

}

/*
 * ddi_get_name: A synonym of ddi_binding_name() ... returns a name
 * the implementation has used to bind the node to a driver.
 */
char *
ddi_get_name(dev_info_t *dip)
{
	return (DEVI(dip)->devi_binding_name);
}

/*
 * ddi_node_name: Return the name property of the devinfo node
 *		This may differ from ddi_binding_name if the node name
 *		does not define a binding to a driver (i.e. generic names).
 */
char *
ddi_node_name(dev_info_t *dip)
{
	return (DEVI(dip)->devi_node_name);
}


/*
 * ddi_get_nodeid:	Get nodeid stored in dev_info structure.
 */
int
ddi_get_nodeid(dev_info_t *dip)
{
	return (DEVI(dip)->devi_nodeid);
}

int
ddi_get_instance(dev_info_t *dip)
{
	return (DEVI(dip)->devi_instance);
}

struct dev_ops *
ddi_get_driver(dev_info_t *dip)
{
	return (DEVI(dip)->devi_ops);
}

void
ddi_set_driver(dev_info_t *dip, struct dev_ops *devo)
{
	DEVI(dip)->devi_ops = devo;
}

/*
 * ddi_set_driver_private/ddi_get_driver_private:
 * Get/set device driver private data in devinfo.
 */
void
ddi_set_driver_private(dev_info_t *dip, void *data)
{
	DEVI(dip)->devi_driver_data = data;
}

void *
ddi_get_driver_private(dev_info_t *dip)
{
	return (DEVI(dip)->devi_driver_data);
}

/*
 * ddi_get_parent, ddi_get_child, ddi_get_next_sibling
 */

dev_info_t *
ddi_get_parent(dev_info_t *dip)
{
	return ((dev_info_t *)DEVI(dip)->devi_parent);
}

dev_info_t *
ddi_get_child(dev_info_t *dip)
{
	return ((dev_info_t *)DEVI(dip)->devi_child);
}

dev_info_t *
ddi_get_next_sibling(dev_info_t *dip)
{
	return ((dev_info_t *)DEVI(dip)->devi_sibling);
}

dev_info_t *
ddi_get_next(dev_info_t *dip)
{
	return ((dev_info_t *)DEVI(dip)->devi_next);
}

void
ddi_set_next(dev_info_t *dip, dev_info_t *nextdip)
{
	DEVI(dip)->devi_next = DEVI(nextdip);
}

/*
 * ddi_root_node:		Return root node of devinfo tree
 */

dev_info_t *
ddi_root_node(void)
{
	extern dev_info_t *top_devinfo;

	return (top_devinfo);
}

/*
 * Miscellaneous functions:
 */

/*
 * Implementation specific hooks
 */

void
ddi_report_dev(dev_info_t *d)
{
	char *b;

	(void) ddi_ctlops(d, d, DDI_CTLOPS_REPORTDEV, (void *)0, (void *)0);

	/*
	 * If this devinfo node has cb_ops, it's implicitly accessible from
	 * userland, so we print its full name together with the instance
	 * number 'abbreviation' that the driver may use internally.
	 */
	if (DEVI(d)->devi_ops->devo_cb_ops != (struct cb_ops *)0 &&
	    (b = kmem_zalloc(MAXPATHLEN, KM_NOSLEEP))) {
		cmn_err(CE_CONT, "?%s%d is %s\n",
		    ddi_driver_name(d), ddi_get_instance(d),
		    ddi_pathname(d, b));
		kmem_free(b, MAXPATHLEN);
	}
}

/*
 * ddi_ctlops() is described in the assembler not to buy a new register
 * window when it's called and can reduce cost in climbing the device tree
 * without using the tail call optimization.
 */
int
ddi_dev_regsize(dev_info_t *dev, uint_t rnumber, off_t *result)
{
	int ret;

	ret = ddi_ctlops(dev, dev, DDI_CTLOPS_REGSIZE,
	    (void *)&rnumber, (void *)result);

	return (ret == DDI_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
}

int
ddi_dev_nregs(dev_info_t *dev, int *result)
{
	return (ddi_ctlops(dev, dev, DDI_CTLOPS_NREGS, 0, (void *)result));
}

int
ddi_dev_is_sid(dev_info_t *d)
{
	return (ddi_ctlops(d, d, DDI_CTLOPS_SIDDEV, (void *)0, (void *)0));
}

int
ddi_slaveonly(dev_info_t *d)
{
	return (ddi_ctlops(d, d, DDI_CTLOPS_SLAVEONLY, (void *)0, (void *)0));
}

int
ddi_dev_affinity(dev_info_t *a, dev_info_t *b)
{
	return (ddi_ctlops(a, a, DDI_CTLOPS_AFFINITY, (void *)b, (void *)0));
}

int
ddi_streams_driver(dev_info_t *dip)
{
	if (i_ddi_devi_attached(dip) &&
	    (DEVI(dip)->devi_ops->devo_cb_ops != NULL) &&
	    (DEVI(dip)->devi_ops->devo_cb_ops->cb_str != NULL))
		return (DDI_SUCCESS);
	return (DDI_FAILURE);
}

/*
 * callback free list
 */

static int ncallbacks;
static int nc_low = 170;
static int nc_med = 512;
static int nc_high = 2048;
static struct ddi_callback *callbackq;
static struct ddi_callback *callbackqfree;

/*
 * set/run callback lists
 */
struct	cbstats	{
	kstat_named_t	cb_asked;
	kstat_named_t	cb_new;
	kstat_named_t	cb_run;
	kstat_named_t	cb_delete;
	kstat_named_t	cb_maxreq;
	kstat_named_t	cb_maxlist;
	kstat_named_t	cb_alloc;
	kstat_named_t	cb_runouts;
	kstat_named_t	cb_L2;
	kstat_named_t	cb_grow;
} cbstats = {
	{"asked",	KSTAT_DATA_UINT32},
	{"new",		KSTAT_DATA_UINT32},
	{"run",		KSTAT_DATA_UINT32},
	{"delete",	KSTAT_DATA_UINT32},
	{"maxreq",	KSTAT_DATA_UINT32},
	{"maxlist",	KSTAT_DATA_UINT32},
	{"alloc",	KSTAT_DATA_UINT32},
	{"runouts",	KSTAT_DATA_UINT32},
	{"L2",		KSTAT_DATA_UINT32},
	{"grow",	KSTAT_DATA_UINT32},
};

#define	nc_asked	cb_asked.value.ui32
#define	nc_new		cb_new.value.ui32
#define	nc_run		cb_run.value.ui32
#define	nc_delete	cb_delete.value.ui32
#define	nc_maxreq	cb_maxreq.value.ui32
#define	nc_maxlist	cb_maxlist.value.ui32
#define	nc_alloc	cb_alloc.value.ui32
#define	nc_runouts	cb_runouts.value.ui32
#define	nc_L2		cb_L2.value.ui32
#define	nc_grow		cb_grow.value.ui32

static kmutex_t ddi_callback_mutex;

/*
 * callbacks are handled using a L1/L2 cache. The L1 cache
 * comes out of kmem_cache_alloc and can expand/shrink dynamically. If
 * we can't get callbacks from the L1 cache [because pageout is doing
 * I/O at the time freemem is 0], we allocate callbacks out of the
 * L2 cache. The L2 cache is static and depends on the memory size.
 * [We might also count the number of devices at probe time and
 * allocate one structure per device and adjust for deferred attach]
 */
void
impl_ddi_callback_init(void)
{
	int	i;
	uint_t	physmegs;
	kstat_t	*ksp;

	physmegs = physmem >> (20 - PAGESHIFT);
	if (physmegs < 48) {
		ncallbacks = nc_low;
	} else if (physmegs < 128) {
		ncallbacks = nc_med;
	} else {
		ncallbacks = nc_high;
	}

	/*
	 * init free list
	 */
	callbackq = kmem_zalloc(
	    ncallbacks * sizeof (struct ddi_callback), KM_SLEEP);
	for (i = 0; i < ncallbacks-1; i++)
		callbackq[i].c_nfree = &callbackq[i+1];
	callbackqfree = callbackq;

	/* init kstats */
	if (ksp = kstat_create("unix", 0, "cbstats", "misc", KSTAT_TYPE_NAMED,
	    sizeof (cbstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL)) {
		ksp->ks_data = (void *) &cbstats;
		kstat_install(ksp);
	}

}

static void
callback_insert(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid,
    int count)
{
	struct ddi_callback *list, *marker, *new;
	size_t size = sizeof (struct ddi_callback);

	list = marker = (struct ddi_callback *)*listid;
	while (list != NULL) {
		if (list->c_call == funcp && list->c_arg == arg) {
			list->c_count += count;
			return;
		}
		marker = list;
		list = list->c_nlist;
	}
	new = kmem_alloc(size, KM_NOSLEEP);
	if (new == NULL) {
		new = callbackqfree;
		if (new == NULL) {
			new = kmem_alloc_tryhard(sizeof (struct ddi_callback),
			    &size, KM_NOSLEEP | KM_PANIC);
			cbstats.nc_grow++;
		} else {
			callbackqfree = new->c_nfree;
			cbstats.nc_L2++;
		}
	}
	if (marker != NULL) {
		marker->c_nlist = new;
	} else {
		*listid = (uintptr_t)new;
	}
	new->c_size = size;
	new->c_nlist = NULL;
	new->c_call = funcp;
	new->c_arg = arg;
	new->c_count = count;
	cbstats.nc_new++;
	cbstats.nc_alloc++;
	if (cbstats.nc_alloc > cbstats.nc_maxlist)
		cbstats.nc_maxlist = cbstats.nc_alloc;
}

void
ddi_set_callback(int (*funcp)(caddr_t), caddr_t arg, uintptr_t *listid)
{
	mutex_enter(&ddi_callback_mutex);
	cbstats.nc_asked++;
	if ((cbstats.nc_asked - cbstats.nc_run) > cbstats.nc_maxreq)
		cbstats.nc_maxreq = (cbstats.nc_asked - cbstats.nc_run);
	(void) callback_insert(funcp, arg, listid, 1);
	mutex_exit(&ddi_callback_mutex);
}

static void
real_callback_run(void *Queue)
{
	int (*funcp)(caddr_t);
	caddr_t arg;
	int count, rval;
	uintptr_t *listid;
	struct ddi_callback *list, *marker;
	int check_pending = 1;
	int pending = 0;

	do {
		mutex_enter(&ddi_callback_mutex);
		listid = Queue;
		list = (struct ddi_callback *)*listid;
		if (list == NULL) {
			mutex_exit(&ddi_callback_mutex);
			return;
		}
		if (check_pending) {
			marker = list;
			while (marker != NULL) {
				pending += marker->c_count;
				marker = marker->c_nlist;
			}
			check_pending = 0;
		}
		ASSERT(pending > 0);
		ASSERT(list->c_count > 0);
		funcp = list->c_call;
		arg = list->c_arg;
		count = list->c_count;
		*(uintptr_t *)Queue = (uintptr_t)list->c_nlist;
		if (list >= &callbackq[0] &&
		    list <= &callbackq[ncallbacks-1]) {
			list->c_nfree = callbackqfree;
			callbackqfree = list;
		} else
			kmem_free(list, list->c_size);

		cbstats.nc_delete++;
		cbstats.nc_alloc--;
		mutex_exit(&ddi_callback_mutex);

		do {
			if ((rval = (*funcp)(arg)) == 0) {
				pending -= count;
				mutex_enter(&ddi_callback_mutex);
				(void) callback_insert(funcp, arg, listid,
				    count);
				cbstats.nc_runouts++;
			} else {
				pending--;
				mutex_enter(&ddi_callback_mutex);
				cbstats.nc_run++;
			}
			mutex_exit(&ddi_callback_mutex);
		} while (rval != 0 && (--count > 0));
	} while (pending > 0);
}

void
ddi_run_callback(uintptr_t *listid)
{
	softcall(real_callback_run, listid);
}

/*
 * ddi_periodic_t
 * ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval,
 *     int level)
 *
 * INTERFACE LEVEL
 *      Solaris DDI specific (Solaris DDI)
 *
 * PARAMETERS
 *      func: the callback function
 *
 *            The callback function will be invoked. The function is invoked
 *            in kernel context if the argument level passed is the zero.
 *            Otherwise it's invoked in interrupt context at the specified
 *            level.
 *
 *       arg: the argument passed to the callback function
 *
 *  interval: interval time
 *
 *    level : callback interrupt level
 *
 *            If the value is the zero, the callback function is invoked
 *            in kernel context. If the value is more than the zero, but
 *            less than or equal to ten, the callback function is invoked in
 *            interrupt context at the specified interrupt level, which may
 *            be used for real time applications.
 *
 *            This value must be in range of 0-10, which can be a numeric
 *            number or a pre-defined macro (DDI_IPL_0, ... , DDI_IPL_10).
 *
 * DESCRIPTION
 *      ddi_periodic_add(9F) schedules the specified function to be
 *      periodically invoked in the interval time.
 *
 *      As well as timeout(9F), the exact time interval over which the function
 *      takes effect cannot be guaranteed, but the value given is a close
 *      approximation.
 *
 *      Drivers waiting on behalf of processes with real-time constraints must
 *      pass non-zero value with the level argument to ddi_periodic_add(9F).
 *
 * RETURN VALUES
 *      ddi_periodic_add(9F) returns a non-zero opaque value (ddi_periodic_t),
 *      which must be used for ddi_periodic_delete(9F) to specify the request.
 *
 * CONTEXT
 *      ddi_periodic_add(9F) can be called in user or kernel context, but
 *      it cannot be called in interrupt context, which is different from
 *      timeout(9F).
 */
ddi_periodic_t
ddi_periodic_add(void (*func)(void *), void *arg, hrtime_t interval, int level)
{
	/*
	 * Sanity check of the argument level.
	 */
	if (level < DDI_IPL_0 || level > DDI_IPL_10)
		cmn_err(CE_PANIC,
		    "ddi_periodic_add: invalid interrupt level (%d).", level);

	/*
	 * Sanity check of the context. ddi_periodic_add() cannot be
	 * called in either interrupt context or high interrupt context.
	 */
	if (servicing_interrupt())
		cmn_err(CE_PANIC,
		    "ddi_periodic_add: called in (high) interrupt context.");

	return ((ddi_periodic_t)i_timeout(func, arg, interval, level));
}

/*
 * void
 * ddi_periodic_delete(ddi_periodic_t req)
 *
 * INTERFACE LEVEL
 *     Solaris DDI specific (Solaris DDI)
 *
 * PARAMETERS
 *     req: ddi_periodic_t opaque value ddi_periodic_add(9F) returned
 *     previously.
 *
 * DESCRIPTION
 *     ddi_periodic_delete(9F) cancels the ddi_periodic_add(9F) request
 *     previously requested.
 *
 *     ddi_periodic_delete(9F) will not return until the pending request
 *     is canceled or executed.
 *
 *     As well as untimeout(9F), calling ddi_periodic_delete(9F) for a
 *     timeout which is either running on another CPU, or has already
 *     completed causes no problems. However, unlike untimeout(9F), there is
 *     no restrictions on the lock which might be held across the call to
 *     ddi_periodic_delete(9F).
 *
 *     Drivers should be structured with the understanding that the arrival of
 *     both an interrupt and a timeout for that interrupt can occasionally
 *     occur, in either order.
 *
 * CONTEXT
 *     ddi_periodic_delete(9F) can be called in user or kernel context, but
 *     it cannot be called in interrupt context, which is different from
 *     untimeout(9F).
 */
void
ddi_periodic_delete(ddi_periodic_t req)
{
	/*
	 * Sanity check of the context. ddi_periodic_delete() cannot be
	 * called in either interrupt context or high interrupt context.
	 */
	if (servicing_interrupt())
		cmn_err(CE_PANIC,
		    "ddi_periodic_delete: called in (high) interrupt context.");

	i_untimeout((timeout_t)req);
}

dev_info_t *
nodevinfo(dev_t dev, int otyp)
{
	_NOTE(ARGUNUSED(dev, otyp))
	return ((dev_info_t *)0);
}

/*
 * A driver should support its own getinfo(9E) entry point. This function
 * is provided as a convenience for ON drivers that don't expect their
 * getinfo(9E) entry point to be called. A driver that uses this must not
 * call ddi_create_minor_node.
 */
int
ddi_no_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
	_NOTE(ARGUNUSED(dip, infocmd, arg, result))
	return (DDI_FAILURE);
}

/*
 * A driver should support its own getinfo(9E) entry point. This function
 * is provided as a convenience for ON drivers that where the minor number
 * is the instance. Drivers that do not have 1:1 mapping must implement
 * their own getinfo(9E) function.
 */
int
ddi_getinfo_1to1(dev_info_t *dip, ddi_info_cmd_t infocmd,
    void *arg, void **result)
{
	_NOTE(ARGUNUSED(dip))
	int	instance;

	if (infocmd != DDI_INFO_DEVT2INSTANCE)
		return (DDI_FAILURE);

	instance = getminor((dev_t)(uintptr_t)arg);
	*result = (void *)(uintptr_t)instance;
	return (DDI_SUCCESS);
}

int
ddifail(dev_info_t *devi, ddi_attach_cmd_t cmd)
{
	_NOTE(ARGUNUSED(devi, cmd))
	return (DDI_FAILURE);
}

int
ddi_no_dma_map(dev_info_t *dip, dev_info_t *rdip,
    struct ddi_dma_req *dmareqp, ddi_dma_handle_t *handlep)
{
	_NOTE(ARGUNUSED(dip, rdip, dmareqp, handlep))
	return (DDI_DMA_NOMAPPING);
}

int
ddi_no_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attr,
    int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
{
	_NOTE(ARGUNUSED(dip, rdip, attr, waitfp, arg, handlep))
	return (DDI_DMA_BADATTR);
}

int
ddi_no_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	_NOTE(ARGUNUSED(dip, rdip, handle))
	return (DDI_FAILURE);
}

int
ddi_no_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
    ddi_dma_cookie_t *cp, uint_t *ccountp)
{
	_NOTE(ARGUNUSED(dip, rdip, handle, dmareq, cp, ccountp))
	return (DDI_DMA_NOMAPPING);
}

int
ddi_no_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	_NOTE(ARGUNUSED(dip, rdip, handle))
	return (DDI_FAILURE);
}

int
ddi_no_dma_flush(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, off_t off, size_t len,
    uint_t cache_flags)
{
	_NOTE(ARGUNUSED(dip, rdip, handle, off, len, cache_flags))
	return (DDI_FAILURE);
}

int
ddi_no_dma_win(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, uint_t win, off_t *offp,
    size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	_NOTE(ARGUNUSED(dip, rdip, handle, win, offp, lenp, cookiep, ccountp))
	return (DDI_FAILURE);
}

int
ddi_no_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
    off_t *offp, size_t *lenp, caddr_t *objp, uint_t flags)
{
	_NOTE(ARGUNUSED(dip, rdip, handle, request, offp, lenp, objp, flags))
	return (DDI_FAILURE);
}

void
ddivoid(void)
{}

int
nochpoll(dev_t dev, short events, int anyyet, short *reventsp,
    struct pollhead **pollhdrp)
{
	_NOTE(ARGUNUSED(dev, events, anyyet, reventsp, pollhdrp))
	return (ENXIO);
}

cred_t *
ddi_get_cred(void)
{
	return (CRED());
}

clock_t
ddi_get_lbolt(void)
{
	return ((clock_t)lbolt_hybrid());
}

int64_t
ddi_get_lbolt64(void)
{
	return (lbolt_hybrid());
}

time_t
ddi_get_time(void)
{
	time_t	now;

	if ((now = gethrestime_sec()) == 0) {
		timestruc_t ts;
		mutex_enter(&tod_lock);
		ts = tod_get();
		mutex_exit(&tod_lock);
		return (ts.tv_sec);
	} else {
		return (now);
	}
}

pid_t
ddi_get_pid(void)
{
	return (ttoproc(curthread)->p_pid);
}

kt_did_t
ddi_get_kt_did(void)
{
	return (curthread->t_did);
}

/*
 * This function returns B_TRUE if the caller can reasonably expect that a call
 * to cv_wait_sig(9F), cv_timedwait_sig(9F), or qwait_sig(9F) could be awakened
 * by user-level signal.  If it returns B_FALSE, then the caller should use
 * other means to make certain that the wait will not hang "forever."
 *
 * It does not check the signal mask, nor for reception of any particular
 * signal.
 *
 * Currently, a thread can receive a signal if it's not a kernel thread and it
 * is not in the middle of exit(2) tear-down.  Threads that are in that
 * tear-down effectively convert cv_wait_sig to cv_wait, cv_timedwait_sig to
 * cv_timedwait, and qwait_sig to qwait.
 */
boolean_t
ddi_can_receive_sig(void)
{
	proc_t *pp;

	if (curthread->t_proc_flag & TP_LWPEXIT)
		return (B_FALSE);
	if ((pp = ttoproc(curthread)) == NULL)
		return (B_FALSE);
	return (pp->p_as != &kas);
}

/*
 * Swap bytes in 16-bit [half-]words
 */
void
swab(void *src, void *dst, size_t nbytes)
{
	uchar_t *pf = (uchar_t *)src;
	uchar_t *pt = (uchar_t *)dst;
	uchar_t tmp;
	int nshorts;

	nshorts = nbytes >> 1;

	while (--nshorts >= 0) {
		tmp = *pf++;
		*pt++ = *pf++;
		*pt++ = tmp;
	}
}

static void
ddi_append_minor_node(dev_info_t *ddip, struct ddi_minor_data *dmdp)
{
	int			circ;
	struct ddi_minor_data	*dp;

	ndi_devi_enter(ddip, &circ);
	if ((dp = DEVI(ddip)->devi_minor) == (struct ddi_minor_data *)NULL) {
		DEVI(ddip)->devi_minor = dmdp;
	} else {
		while (dp->next != (struct ddi_minor_data *)NULL)
			dp = dp->next;
		dp->next = dmdp;
	}
	ndi_devi_exit(ddip, circ);
}

static int
i_log_devfs_minor_create(dev_info_t *dip, char *minor_name)
{
	int se_flag;
	int kmem_flag;
	int se_err;
	char *pathname, *class_name;
	sysevent_t *ev = NULL;
	sysevent_id_t eid;
	sysevent_value_t se_val;
	sysevent_attr_list_t *ev_attr_list = NULL;

	/* determine interrupt context */
	se_flag = (servicing_interrupt()) ? SE_NOSLEEP : SE_SLEEP;
	kmem_flag = (se_flag == SE_SLEEP) ? KM_SLEEP : KM_NOSLEEP;

	i_ddi_di_cache_invalidate();

#ifdef DEBUG
	if ((se_flag == SE_NOSLEEP) && sunddi_debug) {
		cmn_err(CE_CONT, "ddi_create_minor_node: called from "
		    "interrupt level by driver %s",
		    ddi_driver_name(dip));
	}
#endif /* DEBUG */

	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_CREATE, EP_DDI, se_flag);
	if (ev == NULL) {
		goto fail;
	}

	pathname = kmem_alloc(MAXPATHLEN, kmem_flag);
	if (pathname == NULL) {
		sysevent_free(ev);
		goto fail;
	}

	(void) ddi_pathname(dip, pathname);
	ASSERT(strlen(pathname));
	se_val.value_type = SE_DATA_TYPE_STRING;
	se_val.value.sv_string = pathname;
	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
	    &se_val, se_flag) != 0) {
		kmem_free(pathname, MAXPATHLEN);
		sysevent_free(ev);
		goto fail;
	}
	kmem_free(pathname, MAXPATHLEN);

	/* add the device class attribute */
	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
		se_val.value_type = SE_DATA_TYPE_STRING;
		se_val.value.sv_string = class_name;
		if (sysevent_add_attr(&ev_attr_list,
		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
			sysevent_free_attr(ev_attr_list);
			goto fail;
		}
	}

	/*
	 * allow for NULL minor names
	 */
	if (minor_name != NULL) {
		se_val.value.sv_string = minor_name;
		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
		    &se_val, se_flag) != 0) {
			sysevent_free_attr(ev_attr_list);
			sysevent_free(ev);
			goto fail;
		}
	}

	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
		sysevent_free_attr(ev_attr_list);
		sysevent_free(ev);
		goto fail;
	}

	if ((se_err = log_sysevent(ev, se_flag, &eid)) != 0) {
		if (se_err == SE_NO_TRANSPORT) {
			cmn_err(CE_WARN, "/devices or /dev may not be current "
			    "for driver %s (%s). Run devfsadm -i %s",
			    ddi_driver_name(dip), "syseventd not responding",
			    ddi_driver_name(dip));
		} else {
			sysevent_free(ev);
			goto fail;
		}
	}

	sysevent_free(ev);
	return (DDI_SUCCESS);
fail:
	cmn_err(CE_WARN, "/devices or /dev may not be current "
	    "for driver %s. Run devfsadm -i %s",
	    ddi_driver_name(dip), ddi_driver_name(dip));
	return (DDI_SUCCESS);
}

/*
 * failing to remove a minor node is not of interest
 * therefore we do not generate an error message
 */
static int
i_log_devfs_minor_remove(dev_info_t *dip, char *minor_name)
{
	char *pathname, *class_name;
	sysevent_t *ev;
	sysevent_id_t eid;
	sysevent_value_t se_val;
	sysevent_attr_list_t *ev_attr_list = NULL;

	/*
	 * only log ddi_remove_minor_node() calls outside the scope
	 * of attach/detach reconfigurations and when the dip is
	 * still initialized.
	 */
	if (DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip) ||
	    (i_ddi_node_state(dip) < DS_INITIALIZED)) {
		return (DDI_SUCCESS);
	}

	i_ddi_di_cache_invalidate();

	ev = sysevent_alloc(EC_DEVFS, ESC_DEVFS_MINOR_REMOVE, EP_DDI, SE_SLEEP);
	if (ev == NULL) {
		return (DDI_SUCCESS);
	}

	pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
	if (pathname == NULL) {
		sysevent_free(ev);
		return (DDI_SUCCESS);
	}

	(void) ddi_pathname(dip, pathname);
	ASSERT(strlen(pathname));
	se_val.value_type = SE_DATA_TYPE_STRING;
	se_val.value.sv_string = pathname;
	if (sysevent_add_attr(&ev_attr_list, DEVFS_PATHNAME,
	    &se_val, SE_SLEEP) != 0) {
		kmem_free(pathname, MAXPATHLEN);
		sysevent_free(ev);
		return (DDI_SUCCESS);
	}

	kmem_free(pathname, MAXPATHLEN);

	/*
	 * allow for NULL minor names
	 */
	if (minor_name != NULL) {
		se_val.value.sv_string = minor_name;
		if (sysevent_add_attr(&ev_attr_list, DEVFS_MINOR_NAME,
		    &se_val, SE_SLEEP) != 0) {
			sysevent_free_attr(ev_attr_list);
			goto fail;
		}
	}

	if ((class_name = i_ddi_devi_class(dip)) != NULL) {
		/* add the device class, driver name and instance attributes */

		se_val.value_type = SE_DATA_TYPE_STRING;
		se_val.value.sv_string = class_name;
		if (sysevent_add_attr(&ev_attr_list,
		    DEVFS_DEVI_CLASS, &se_val, SE_SLEEP) != 0) {
			sysevent_free_attr(ev_attr_list);
			goto fail;
		}

		se_val.value_type = SE_DATA_TYPE_STRING;
		se_val.value.sv_string = (char *)ddi_driver_name(dip);
		if (sysevent_add_attr(&ev_attr_list,
		    DEVFS_DRIVER_NAME, &se_val, SE_SLEEP) != 0) {
			sysevent_free_attr(ev_attr_list);
			goto fail;
		}

		se_val.value_type = SE_DATA_TYPE_INT32;
		se_val.value.sv_int32 = ddi_get_instance(dip);
		if (sysevent_add_attr(&ev_attr_list,
		    DEVFS_INSTANCE, &se_val, SE_SLEEP) != 0) {
			sysevent_free_attr(ev_attr_list);
			goto fail;
		}

	}

	if (sysevent_attach_attributes(ev, ev_attr_list) != 0) {
		sysevent_free_attr(ev_attr_list);
	} else {
		(void) log_sysevent(ev, SE_SLEEP, &eid);
	}
fail:
	sysevent_free(ev);
	return (DDI_SUCCESS);
}

/*
 * Derive the device class of the node.
 * Device class names aren't defined yet. Until this is done we use
 * devfs event subclass names as device class names.
 */
static int
derive_devi_class(dev_info_t *dip, const char *node_type, int flag)
{
	int rv = DDI_SUCCESS;

	if (i_ddi_devi_class(dip) == NULL) {
		if (strncmp(node_type, DDI_NT_BLOCK,
		    sizeof (DDI_NT_BLOCK) - 1) == 0 &&
		    (node_type[sizeof (DDI_NT_BLOCK) - 1] == '\0' ||
		    node_type[sizeof (DDI_NT_BLOCK) - 1] == ':') &&
		    strcmp(node_type, DDI_NT_FD) != 0) {

			rv = i_ddi_set_devi_class(dip, ESC_DISK, flag);

		} else if (strncmp(node_type, DDI_NT_NET,
		    sizeof (DDI_NT_NET) - 1) == 0 &&
		    (node_type[sizeof (DDI_NT_NET) - 1] == '\0' ||
		    node_type[sizeof (DDI_NT_NET) - 1] == ':')) {

			rv = i_ddi_set_devi_class(dip, ESC_NETWORK, flag);

		} else if (strncmp(node_type, DDI_NT_PRINTER,
		    sizeof (DDI_NT_PRINTER) - 1) == 0 &&
		    (node_type[sizeof (DDI_NT_PRINTER) - 1] == '\0' ||
		    node_type[sizeof (DDI_NT_PRINTER) - 1] == ':')) {

			rv = i_ddi_set_devi_class(dip, ESC_PRINTER, flag);

		} else if (strncmp(node_type, DDI_PSEUDO,
		    sizeof (DDI_PSEUDO) -1) == 0 &&
		    (strncmp(ESC_LOFI, ddi_node_name(dip),
		    sizeof (ESC_LOFI) -1) == 0)) {
			rv = i_ddi_set_devi_class(dip, ESC_LOFI, flag);
		}
	}

	return (rv);
}

/*
 * Check compliance with PSARC 2003/375:
 *
 * The name must contain only characters a-z, A-Z, 0-9 or _ and it must not
 * exceed IFNAMSIZ (16) characters in length.
 */
static boolean_t
verify_name(const char *name)
{
	size_t len = strlen(name);
	const char *cp;

	if (len == 0 || len > IFNAMSIZ)
		return (B_FALSE);

	for (cp = name; *cp != '\0'; cp++) {
		if (!isalnum(*cp) && *cp != '_')
			return (B_FALSE);
	}

	return (B_TRUE);
}

/*
 * ddi_create_minor_common:	Create a  ddi_minor_data structure and
 *				attach it to the given devinfo node.
 */

static int
ddi_create_minor_common(dev_info_t *dip, const char *name, int spec_type,
    minor_t minor_num, const char *node_type, int flag, ddi_minor_type mtype,
    const char *read_priv, const char *write_priv, mode_t priv_mode)
{
	struct ddi_minor_data *dmdp;
	major_t major;

	if (spec_type != S_IFCHR && spec_type != S_IFBLK)
		return (DDI_FAILURE);

	if (name == NULL)
		return (DDI_FAILURE);

	/*
	 * Log a message if the minor number the driver is creating
	 * is not expressible on the on-disk filesystem (currently
	 * this is limited to 18 bits both by UFS). The device can
	 * be opened via devfs, but not by device special files created
	 * via mknod().
	 */
	if (minor_num > L_MAXMIN32) {
		cmn_err(CE_WARN,
		    "%s%d:%s minor 0x%x too big for 32-bit applications",
		    ddi_driver_name(dip), ddi_get_instance(dip),
		    name, minor_num);
		return (DDI_FAILURE);
	}

	/* dip must be bound and attached */
	major = ddi_driver_major(dip);
	ASSERT(major != DDI_MAJOR_T_NONE);

	/*
	 * Default node_type to DDI_PSEUDO and issue notice in debug mode
	 */
	if (node_type == NULL) {
		node_type = DDI_PSEUDO;
		NDI_CONFIG_DEBUG((CE_NOTE, "!illegal node_type NULL for %s%d "
		    " minor node %s; default to DDI_PSEUDO",
		    ddi_driver_name(dip), ddi_get_instance(dip), name));
	}

	/*
	 * If the driver is a network driver, ensure that the name falls within
	 * the interface naming constraints specified by PSARC/2003/375.
	 */
	if (strcmp(node_type, DDI_NT_NET) == 0) {
		if (!verify_name(name))
			return (DDI_FAILURE);

		if (mtype == DDM_MINOR) {
			struct devnames *dnp = &devnamesp[major];

			/* Mark driver as a network driver */
			LOCK_DEV_OPS(&dnp->dn_lock);
			dnp->dn_flags |= DN_NETWORK_DRIVER;

			/*
			 * If this minor node is created during the device
			 * attachment, this is a physical network device.
			 * Mark the driver as a physical network driver.
			 */
			if (DEVI_IS_ATTACHING(dip))
				dnp->dn_flags |= DN_NETWORK_PHYSDRIVER;
			UNLOCK_DEV_OPS(&dnp->dn_lock);
		}
	}

	if (mtype == DDM_MINOR) {
		if (derive_devi_class(dip,  node_type, KM_NOSLEEP) !=
		    DDI_SUCCESS)
			return (DDI_FAILURE);
	}

	/*
	 * Take care of minor number information for the node.
	 */

	if ((dmdp = kmem_zalloc(sizeof (struct ddi_minor_data),
	    KM_NOSLEEP)) == NULL) {
		return (DDI_FAILURE);
	}
	if ((dmdp->ddm_name = i_ddi_strdup(name, KM_NOSLEEP)) == NULL) {
		kmem_free(dmdp, sizeof (struct ddi_minor_data));
		return (DDI_FAILURE);
	}
	dmdp->dip = dip;
	dmdp->ddm_dev = makedevice(major, minor_num);
	dmdp->ddm_spec_type = spec_type;
	dmdp->ddm_node_type = node_type;
	dmdp->type = mtype;
	if (flag & CLONE_DEV) {
		dmdp->type = DDM_ALIAS;
		dmdp->ddm_dev = makedevice(ddi_driver_major(clone_dip), major);
	}
	if (flag & PRIVONLY_DEV) {
		dmdp->ddm_flags |= DM_NO_FSPERM;
	}
	if (read_priv || write_priv) {
		dmdp->ddm_node_priv =
		    devpolicy_priv_by_name(read_priv, write_priv);
	}
	dmdp->ddm_priv_mode = priv_mode;

	ddi_append_minor_node(dip, dmdp);

	/*
	 * only log ddi_create_minor_node() calls which occur
	 * outside the scope of attach(9e)/detach(9e) reconfigurations
	 */
	if (!(DEVI_IS_ATTACHING(dip) || DEVI_IS_DETACHING(dip)) &&
	    mtype != DDM_INTERNAL_PATH) {
		(void) i_log_devfs_minor_create(dip, dmdp->ddm_name);
	}

	/*
	 * Check if any dacf rules match the creation of this minor node
	 */
	dacfc_match_create_minor(name, node_type, dip, dmdp, flag);
	return (DDI_SUCCESS);
}

int
ddi_create_minor_node(dev_info_t *dip, const char *name, int spec_type,
    minor_t minor_num, const char *node_type, int flag)
{
	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
	    node_type, flag, DDM_MINOR, NULL, NULL, 0));
}

int
ddi_create_priv_minor_node(dev_info_t *dip, const char *name, int spec_type,
    minor_t minor_num, const char *node_type, int flag,
    const char *rdpriv, const char *wrpriv, mode_t priv_mode)
{
	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
	    node_type, flag, DDM_MINOR, rdpriv, wrpriv, priv_mode));
}

int
ddi_create_default_minor_node(dev_info_t *dip, const char *name, int spec_type,
    minor_t minor_num, const char *node_type, int flag)
{
	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
	    node_type, flag, DDM_DEFAULT, NULL, NULL, 0));
}

/*
 * Internal (non-ddi) routine for drivers to export names known
 * to the kernel (especially ddi_pathname_to_dev_t and friends)
 * but not exported externally to /dev
 */
int
ddi_create_internal_pathname(dev_info_t *dip, char *name, int spec_type,
    minor_t minor_num)
{
	return (ddi_create_minor_common(dip, name, spec_type, minor_num,
	    "internal", 0, DDM_INTERNAL_PATH, NULL, NULL, 0));
}

void
ddi_remove_minor_node(dev_info_t *dip, const char *name)
{
	int			circ;
	struct ddi_minor_data	*dmdp, *dmdp1;
	struct ddi_minor_data	**dmdp_prev;

	ndi_devi_enter(dip, &circ);
	dmdp_prev = &DEVI(dip)->devi_minor;
	dmdp = DEVI(dip)->devi_minor;
	while (dmdp != NULL) {
		dmdp1 = dmdp->next;
		if ((name == NULL || (dmdp->ddm_name != NULL &&
		    strcmp(name, dmdp->ddm_name) == 0))) {
			if (dmdp->ddm_name != NULL) {
				if (dmdp->type != DDM_INTERNAL_PATH)
					(void) i_log_devfs_minor_remove(dip,
					    dmdp->ddm_name);
				kmem_free(dmdp->ddm_name,
				    strlen(dmdp->ddm_name) + 1);
			}
			/*
			 * Release device privilege, if any.
			 * Release dacf client data associated with this minor
			 * node by storing NULL.
			 */
			if (dmdp->ddm_node_priv)
				dpfree(dmdp->ddm_node_priv);
			dacf_store_info((dacf_infohdl_t)dmdp, NULL);
			kmem_free(dmdp, sizeof (struct ddi_minor_data));
			*dmdp_prev = dmdp1;
			/*
			 * OK, we found it, so get out now -- if we drive on,
			 * we will strcmp against garbage.  See 1139209.
			 */
			if (name != NULL)
				break;
		} else {
			dmdp_prev = &dmdp->next;
		}
		dmdp = dmdp1;
	}
	ndi_devi_exit(dip, circ);
}


int
ddi_in_panic()
{
	return (panicstr != NULL);
}


/*
 * Find first bit set in a mask (returned counting from 1 up)
 */

int
ddi_ffs(long mask)
{
	return (ffs(mask));
}

/*
 * Find last bit set. Take mask and clear
 * all but the most significant bit, and
 * then let ffs do the rest of the work.
 *
 * Algorithm courtesy of Steve Chessin.
 */

int
ddi_fls(long mask)
{
	while (mask) {
		long nx;

		if ((nx = (mask & (mask - 1))) == 0)
			break;
		mask = nx;
	}
	return (ffs(mask));
}

/*
 * The ddi_soft_state_* routines comprise generic storage management utilities
 * for driver soft state structures (in "the old days," this was done with
 * statically sized array - big systems and dynamic loading and unloading
 * make heap allocation more attractive).
 */

/*
 * Allocate a set of pointers to 'n_items' objects of size 'size'
 * bytes.  Each pointer is initialized to nil.
 *
 * The 'size' and 'n_items' values are stashed in the opaque
 * handle returned to the caller.
 *
 * This implementation interprets 'set of pointers' to mean 'array
 * of pointers' but note that nothing in the interface definition
 * precludes an implementation that uses, for example, a linked list.
 * However there should be a small efficiency gain from using an array
 * at lookup time.
 *
 * NOTE	As an optimization, we make our growable array allocations in
 *	powers of two (bytes), since that's how much kmem_alloc (currently)
 *	gives us anyway.  It should save us some free/realloc's ..
 *
 *	As a further optimization, we make the growable array start out
 *	with MIN_N_ITEMS in it.
 */

#define	MIN_N_ITEMS	8	/* 8 void *'s == 32 bytes */

int
ddi_soft_state_init(void **state_p, size_t size, size_t n_items)
{
	i_ddi_soft_state	*ss;

	if (state_p == NULL || size == 0)
		return (EINVAL);

	ss = kmem_zalloc(sizeof (*ss), KM_SLEEP);
	mutex_init(&ss->lock, NULL, MUTEX_DRIVER, NULL);
	ss->size = size;

	if (n_items < MIN_N_ITEMS)
		ss->n_items = MIN_N_ITEMS;
	else {
		int bitlog;

		if ((bitlog = ddi_fls(n_items)) == ddi_ffs(n_items))
			bitlog--;
		ss->n_items = 1 << bitlog;
	}

	ASSERT(ss->n_items >= n_items);

	ss->array = kmem_zalloc(ss->n_items * sizeof (void *), KM_SLEEP);

	*state_p = ss;
	return (0);
}

/*
 * Allocate a state structure of size 'size' to be associated
 * with item 'item'.
 *
 * In this implementation, the array is extended to
 * allow the requested offset, if needed.
 */
int
ddi_soft_state_zalloc(void *state, int item)
{
	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
	void			**array;
	void			*new_element;

	if ((state == NULL) || (item < 0))
		return (DDI_FAILURE);

	mutex_enter(&ss->lock);
	if (ss->size == 0) {
		mutex_exit(&ss->lock);
		cmn_err(CE_WARN, "ddi_soft_state_zalloc: bad handle: %s",
		    mod_containing_pc(caller()));
		return (DDI_FAILURE);
	}

	array = ss->array;	/* NULL if ss->n_items == 0 */
	ASSERT(ss->n_items != 0 && array != NULL);

	/*
	 * refuse to tread on an existing element
	 */
	if (item < ss->n_items && array[item] != NULL) {
		mutex_exit(&ss->lock);
		return (DDI_FAILURE);
	}

	/*
	 * Allocate a new element to plug in
	 */
	new_element = kmem_zalloc(ss->size, KM_SLEEP);

	/*
	 * Check if the array is big enough, if not, grow it.
	 */
	if (item >= ss->n_items) {
		void			**new_array;
		size_t			new_n_items;
		struct i_ddi_soft_state	*dirty;

		/*
		 * Allocate a new array of the right length, copy
		 * all the old pointers to the new array, then
		 * if it exists at all, put the old array on the
		 * dirty list.
		 *
		 * Note that we can't kmem_free() the old array.
		 *
		 * Why -- well the 'get' operation is 'mutex-free', so we
		 * can't easily catch a suspended thread that is just about
		 * to dereference the array we just grew out of.  So we
		 * cons up a header and put it on a list of 'dirty'
		 * pointer arrays.  (Dirty in the sense that there may
		 * be suspended threads somewhere that are in the middle
		 * of referencing them).  Fortunately, we -can- garbage
		 * collect it all at ddi_soft_state_fini time.
		 */
		new_n_items = ss->n_items;
		while (new_n_items < (1 + item))
			new_n_items <<= 1;	/* double array size .. */

		ASSERT(new_n_items >= (1 + item));	/* sanity check! */

		new_array = kmem_zalloc(new_n_items * sizeof (void *),
		    KM_SLEEP);
		/*
		 * Copy the pointers into the new array
		 */
		bcopy(array, new_array, ss->n_items * sizeof (void *));

		/*
		 * Save the old array on the dirty list
		 */
		dirty = kmem_zalloc(sizeof (*dirty), KM_SLEEP);
		dirty->array = ss->array;
		dirty->n_items = ss->n_items;
		dirty->next = ss->next;
		ss->next = dirty;

		ss->array = (array = new_array);
		ss->n_items = new_n_items;
	}

	ASSERT(array != NULL && item < ss->n_items && array[item] == NULL);

	array[item] = new_element;

	mutex_exit(&ss->lock);
	return (DDI_SUCCESS);
}

/*
 * Fetch a pointer to the allocated soft state structure.
 *
 * This is designed to be cheap.
 *
 * There's an argument that there should be more checking for
 * nil pointers and out of bounds on the array.. but we do a lot
 * of that in the alloc/free routines.
 *
 * An array has the convenience that we don't need to lock read-access
 * to it c.f. a linked list.  However our "expanding array" strategy
 * means that we should hold a readers lock on the i_ddi_soft_state
 * structure.
 *
 * However, from a performance viewpoint, we need to do it without
 * any locks at all -- this also makes it a leaf routine.  The algorithm
 * is 'lock-free' because we only discard the pointer arrays at
 * ddi_soft_state_fini() time.
 */
void *
ddi_get_soft_state(void *state, int item)
{
	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;

	ASSERT((ss != NULL) && (item >= 0));

	if (item < ss->n_items && ss->array != NULL)
		return (ss->array[item]);
	return (NULL);
}

/*
 * Free the state structure corresponding to 'item.'   Freeing an
 * element that has either gone or was never allocated is not
 * considered an error.  Note that we free the state structure, but
 * we don't shrink our pointer array, or discard 'dirty' arrays,
 * since even a few pointers don't really waste too much memory.
 *
 * Passing an item number that is out of bounds, or a null pointer will
 * provoke an error message.
 */
void
ddi_soft_state_free(void *state, int item)
{
	i_ddi_soft_state	*ss = (i_ddi_soft_state *)state;
	void			**array;
	void			*element;
	static char		msg[] = "ddi_soft_state_free:";

	if (ss == NULL) {
		cmn_err(CE_WARN, "%s null handle: %s",
		    msg, mod_containing_pc(caller()));
		return;
	}

	element = NULL;

	mutex_enter(&ss->lock);

	if ((array = ss->array) == NULL || ss->size == 0) {
		cmn_err(CE_WARN, "%s bad handle: %s",
		    msg, mod_containing_pc(caller()));
	} else if (item < 0 || item >= ss->n_items) {
		cmn_err(CE_WARN, "%s item %d not in range [0..%lu]: %s",
		    msg, item, ss->n_items - 1, mod_containing_pc(caller()));
	} else if (array[item] != NULL) {
		element = array[item];
		array[item] = NULL;
	}

	mutex_exit(&ss->lock);

	if (element)
		kmem_free(element, ss->size);
}

/*
 * Free the entire set of pointers, and any
 * soft state structures contained therein.
 *
 * Note that we don't grab the ss->lock mutex, even though
 * we're inspecting the various fields of the data structure.
 *
 * There is an implicit assumption that this routine will
 * never run concurrently with any of the above on this
 * particular state structure i.e. by the time the driver
 * calls this routine, there should be no other threads
 * running in the driver.
 */
void
ddi_soft_state_fini(void **state_p)
{
	i_ddi_soft_state	*ss, *dirty;
	int			item;
	static char		msg[] = "ddi_soft_state_fini:";

	if (state_p == NULL ||
	    (ss = (i_ddi_soft_state *)(*state_p)) == NULL) {
		cmn_err(CE_WARN, "%s null handle: %s",
		    msg, mod_containing_pc(caller()));
		return;
	}

	if (ss->size == 0) {
		cmn_err(CE_WARN, "%s bad handle: %s",
		    msg, mod_containing_pc(caller()));
		return;
	}

	if (ss->n_items > 0) {
		for (item = 0; item < ss->n_items; item++)
			ddi_soft_state_free(ss, item);
		kmem_free(ss->array, ss->n_items * sizeof (void *));
	}

	/*
	 * Now delete any dirty arrays from previous 'grow' operations
	 */
	for (dirty = ss->next; dirty; dirty = ss->next) {
		ss->next = dirty->next;
		kmem_free(dirty->array, dirty->n_items * sizeof (void *));
		kmem_free(dirty, sizeof (*dirty));
	}

	mutex_destroy(&ss->lock);
	kmem_free(ss, sizeof (*ss));

	*state_p = NULL;
}

#define	SS_N_ITEMS_PER_HASH	16
#define	SS_MIN_HASH_SZ		16
#define	SS_MAX_HASH_SZ		4096

int
ddi_soft_state_bystr_init(ddi_soft_state_bystr **state_p, size_t size,
    int n_items)
{
	i_ddi_soft_state_bystr	*sss;
	int			hash_sz;

	ASSERT(state_p && size && n_items);
	if ((state_p == NULL) || (size == 0) || (n_items == 0))
		return (EINVAL);

	/* current implementation is based on hash, convert n_items to hash */
	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
	if (hash_sz < SS_MIN_HASH_SZ)
		hash_sz = SS_MIN_HASH_SZ;
	else if (hash_sz > SS_MAX_HASH_SZ)
		hash_sz = SS_MAX_HASH_SZ;

	/* allocate soft_state pool */
	sss = kmem_zalloc(sizeof (*sss), KM_SLEEP);
	sss->ss_size = size;
	sss->ss_mod_hash = mod_hash_create_strhash("soft_state_bystr",
	    hash_sz, mod_hash_null_valdtor);
	*state_p = (ddi_soft_state_bystr *)sss;
	return (0);
}

int
ddi_soft_state_bystr_zalloc(ddi_soft_state_bystr *state, const char *str)
{
	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
	void			*sso;
	char			*dup_str;

	ASSERT(sss && str && sss->ss_mod_hash);
	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
		return (DDI_FAILURE);
	sso = kmem_zalloc(sss->ss_size, KM_SLEEP);
	dup_str = i_ddi_strdup((char *)str, KM_SLEEP);
	if (mod_hash_insert(sss->ss_mod_hash,
	    (mod_hash_key_t)dup_str, (mod_hash_val_t)sso) == 0)
		return (DDI_SUCCESS);

	/*
	 * The only error from an strhash insert is caused by a duplicate key.
	 * We refuse to tread on an existing elements, so free and fail.
	 */
	kmem_free(dup_str, strlen(dup_str) + 1);
	kmem_free(sso, sss->ss_size);
	return (DDI_FAILURE);
}

void *
ddi_soft_state_bystr_get(ddi_soft_state_bystr *state, const char *str)
{
	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
	void			*sso;

	ASSERT(sss && str && sss->ss_mod_hash);
	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
		return (NULL);

	if (mod_hash_find(sss->ss_mod_hash,
	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso) == 0)
		return (sso);
	return (NULL);
}

void
ddi_soft_state_bystr_free(ddi_soft_state_bystr *state, const char *str)
{
	i_ddi_soft_state_bystr	*sss = (i_ddi_soft_state_bystr *)state;
	void			*sso;

	ASSERT(sss && str && sss->ss_mod_hash);
	if ((sss == NULL) || (str == NULL) || (sss->ss_mod_hash == NULL))
		return;

	(void) mod_hash_remove(sss->ss_mod_hash,
	    (mod_hash_key_t)str, (mod_hash_val_t *)&sso);
	kmem_free(sso, sss->ss_size);
}

void
ddi_soft_state_bystr_fini(ddi_soft_state_bystr **state_p)
{
	i_ddi_soft_state_bystr	*sss;

	ASSERT(state_p);
	if (state_p == NULL)
		return;

	sss = (i_ddi_soft_state_bystr *)(*state_p);
	if (sss == NULL)
		return;

	ASSERT(sss->ss_mod_hash);
	if (sss->ss_mod_hash) {
		mod_hash_destroy_strhash(sss->ss_mod_hash);
		sss->ss_mod_hash = NULL;
	}

	kmem_free(sss, sizeof (*sss));
	*state_p = NULL;
}

/*
 * The ddi_strid_* routines provide string-to-index management utilities.
 */
/* allocate and initialize an strid set */
int
ddi_strid_init(ddi_strid **strid_p, int n_items)
{
	i_ddi_strid	*ss;
	int		hash_sz;

	if (strid_p == NULL)
		return (DDI_FAILURE);

	/* current implementation is based on hash, convert n_items to hash */
	hash_sz = n_items / SS_N_ITEMS_PER_HASH;
	if (hash_sz < SS_MIN_HASH_SZ)
		hash_sz = SS_MIN_HASH_SZ;
	else if (hash_sz > SS_MAX_HASH_SZ)
		hash_sz = SS_MAX_HASH_SZ;

	ss = kmem_alloc(sizeof (*ss), KM_SLEEP);
	ss->strid_chunksz = n_items;
	ss->strid_spacesz = n_items;
	ss->strid_space = id_space_create("strid", 1, n_items);
	ss->strid_bystr = mod_hash_create_strhash("strid_bystr", hash_sz,
	    mod_hash_null_valdtor);
	ss->strid_byid = mod_hash_create_idhash("strid_byid", hash_sz,
	    mod_hash_null_valdtor);
	*strid_p = (ddi_strid *)ss;
	return (DDI_SUCCESS);
}

/* allocate an id mapping within the specified set for str, return id */
static id_t
i_ddi_strid_alloc(ddi_strid *strid, char *str)
{
	i_ddi_strid	*ss = (i_ddi_strid *)strid;
	id_t		id;
	char		*s;

	ASSERT(ss && str);
	if ((ss == NULL) || (str == NULL))
		return (0);

	/*
	 * Allocate an id using VM_FIRSTFIT in order to keep allocated id
	 * range as compressed as possible.  This is important to minimize
	 * the amount of space used when the id is used as a ddi_soft_state
	 * index by the caller.
	 *
	 * If the id list is exhausted, increase the size of the list
	 * by the chuck size specified in ddi_strid_init and reattempt
	 * the allocation
	 */
	if ((id = id_allocff_nosleep(ss->strid_space)) == (id_t)-1) {
		id_space_extend(ss->strid_space, ss->strid_spacesz,
		    ss->strid_spacesz + ss->strid_chunksz);
		ss->strid_spacesz += ss->strid_chunksz;
		if ((id = id_allocff_nosleep(ss->strid_space)) == (id_t)-1)
			return (0);
	}

	/*
	 * NOTE: since we create and destroy in unison we can save space by
	 * using bystr key as the byid value.  This means destroy must occur
	 * in (byid, bystr) order.
	 */
	s = i_ddi_strdup(str, KM_SLEEP);
	if (mod_hash_insert(ss->strid_bystr, (mod_hash_key_t)s,
	    (mod_hash_val_t)(intptr_t)id) != 0) {
		ddi_strid_free(strid, id);
		return (0);
	}
	if (mod_hash_insert(ss->strid_byid, (mod_hash_key_t)(intptr_t)id,
	    (mod_hash_val_t)s) != 0) {
		ddi_strid_free(strid, id);
		return (0);
	}

	/* NOTE: s if freed on mod_hash_destroy by mod_hash_strval_dtor */
	return (id);
}

/* allocate an id mapping within the specified set for str, return id */
id_t
ddi_strid_alloc(ddi_strid *strid, char *str)
{
	return (i_ddi_strid_alloc(strid, str));
}

/* return the id within the specified strid given the str */
id_t
ddi_strid_str2id(ddi_strid *strid, char *str)
{
	i_ddi_strid	*ss = (i_ddi_strid *)strid;
	id_t		id = 0;
	mod_hash_val_t	hv;

	ASSERT(ss && str);
	if (ss && str && (mod_hash_find(ss->strid_bystr,
	    (mod_hash_key_t)str, &hv) == 0))
		id = (int)(intptr_t)hv;
	return (id);
}

/* return str within the specified strid given the id */
char *
ddi_strid_id2str(ddi_strid *strid, id_t id)
{
	i_ddi_strid	*ss = (i_ddi_strid *)strid;
	char		*str = NULL;
	mod_hash_val_t	hv;

	ASSERT(ss && id > 0);
	if (ss && (id > 0) && (mod_hash_find(ss->strid_byid,
	    (mod_hash_key_t)(uintptr_t)id, &hv) == 0))
		str = (char *)hv;
	return (str);
}

/* free the id mapping within the specified strid */
void
ddi_strid_free(ddi_strid *strid, id_t id)
{
	i_ddi_strid	*ss = (i_ddi_strid *)strid;
	char		*str;

	ASSERT(ss && id > 0);
	if ((ss == NULL) || (id <= 0))
		return;

	/* bystr key is byid value: destroy order must be (byid, bystr) */
	str = ddi_strid_id2str(strid, id);
	(void) mod_hash_destroy(ss->strid_byid, (mod_hash_key_t)(uintptr_t)id);
	id_free(ss->strid_space, id);

	if (str)
		(void) mod_hash_destroy(ss->strid_bystr, (mod_hash_key_t)str);
}

/* destroy the strid set */
void
ddi_strid_fini(ddi_strid **strid_p)
{
	i_ddi_strid	*ss;

	ASSERT(strid_p);
	if (strid_p == NULL)
		return;

	ss = (i_ddi_strid *)(*strid_p);
	if (ss == NULL)
		return;

	/* bystr key is byid value: destroy order must be (byid, bystr) */
	if (ss->strid_byid)
		mod_hash_destroy_hash(ss->strid_byid);
	if (ss->strid_byid)
		mod_hash_destroy_hash(ss->strid_bystr);
	if (ss->strid_space)
		id_space_destroy(ss->strid_space);
	kmem_free(ss, sizeof (*ss));
	*strid_p = NULL;
}

/*
 * This sets the devi_addr entry in the dev_info structure 'dip' to 'name'.
 * Storage is double buffered to prevent updates during devi_addr use -
 * double buffering is adaquate for reliable ddi_deviname() consumption.
 * The double buffer is not freed until dev_info structure destruction
 * (by i_ddi_free_node).
 */
void
ddi_set_name_addr(dev_info_t *dip, char *name)
{
	char	*buf = DEVI(dip)->devi_addr_buf;
	char	*newaddr;

	if (buf == NULL) {
		buf = kmem_zalloc(2 * MAXNAMELEN, KM_SLEEP);
		DEVI(dip)->devi_addr_buf = buf;
	}

	if (name) {
		ASSERT(strlen(name) < MAXNAMELEN);
		newaddr = (DEVI(dip)->devi_addr == buf) ?
		    (buf + MAXNAMELEN) : buf;
		(void) strlcpy(newaddr, name, MAXNAMELEN);
	} else
		newaddr = NULL;

	DEVI(dip)->devi_addr = newaddr;
}

char *
ddi_get_name_addr(dev_info_t *dip)
{
	return (DEVI(dip)->devi_addr);
}

void
ddi_set_parent_data(dev_info_t *dip, void *pd)
{
	DEVI(dip)->devi_parent_data = pd;
}

void *
ddi_get_parent_data(dev_info_t *dip)
{
	return (DEVI(dip)->devi_parent_data);
}

/*
 * ddi_name_to_major: returns the major number of a named module,
 * derived from the current driver alias binding.
 *
 * Caveat: drivers should avoid the use of this function, in particular
 * together with ddi_get_name/ddi_binding name, as per
 *	major = ddi_name_to_major(ddi_get_name(devi));
 * ddi_name_to_major() relies on the state of the device/alias binding,
 * which can and does change dynamically as aliases are administered
 * over time.  An attached device instance cannot rely on the major
 * number returned by ddi_name_to_major() to match its own major number.
 *
 * For driver use, ddi_driver_major() reliably returns the major number
 * for the module to which the device was bound at attach time over
 * the life of the instance.
 *	major = ddi_driver_major(dev_info_t *)
 */
major_t
ddi_name_to_major(char *name)
{
	return (mod_name_to_major(name));
}

/*
 * ddi_major_to_name: Returns the module name bound to a major number.
 */
char *
ddi_major_to_name(major_t major)
{
	return (mod_major_to_name(major));
}

/*
 * Return the name of the devinfo node pointed at by 'dip' in the buffer
 * pointed at by 'name.'  A devinfo node is named as a result of calling
 * ddi_initchild().
 *
 * Note: the driver must be held before calling this function!
 */
char *
ddi_deviname(dev_info_t *dip, char *name)
{
	char *addrname;
	char none = '\0';

	if (dip == ddi_root_node()) {
		*name = '\0';
		return (name);
	}

	if (i_ddi_node_state(dip) < DS_BOUND) {
		addrname = &none;
	} else {
		/*
		 * Use ddi_get_name_addr() without checking state so we get
		 * a unit-address if we are called after ddi_set_name_addr()
		 * by nexus DDI_CTL_INITCHILD code, but before completing
		 * node promotion to DS_INITIALIZED.  We currently have
		 * two situations where we are called in this state:
		 *   o  For framework processing of a path-oriented alias.
		 *   o  If a SCSA nexus driver calls ddi_devid_register()
		 *	from it's tran_tgt_init(9E) implementation.
		 */
		addrname = ddi_get_name_addr(dip);
		if (addrname == NULL)
			addrname = &none;
	}

	if (*addrname == '\0') {
		(void) sprintf(name, "/%s", ddi_node_name(dip));
	} else {
		(void) sprintf(name, "/%s@%s", ddi_node_name(dip), addrname);
	}

	return (name);
}

/*
 * Spits out the name of device node, typically name@addr, for a given node,
 * using the driver name, not the nodename.
 *
 * Used by match_parent. Not to be used elsewhere.
 */
char *
i_ddi_parname(dev_info_t *dip, char *name)
{
	char *addrname;

	if (dip == ddi_root_node()) {
		*name = '\0';
		return (name);
	}

	ASSERT(i_ddi_node_state(dip) >= DS_INITIALIZED);

	if (*(addrname = ddi_get_name_addr(dip)) == '\0')
		(void) sprintf(name, "%s", ddi_binding_name(dip));
	else
		(void) sprintf(name, "%s@%s", ddi_binding_name(dip), addrname);
	return (name);
}

static char *
pathname_work(dev_info_t *dip, char *path)
{
	char *bp;

	if (dip == ddi_root_node()) {
		*path = '\0';
		return (path);
	}
	(void) pathname_work(ddi_get_parent(dip), path);
	bp = path + strlen(path);
	(void) ddi_deviname(dip, bp);
	return (path);
}

char *
ddi_pathname(dev_info_t *dip, char *path)
{
	return (pathname_work(dip, path));
}

char *
ddi_pathname_minor(struct ddi_minor_data *dmdp, char *path)
{
	if (dmdp->dip == NULL)
		*path = '\0';
	else {
		(void) ddi_pathname(dmdp->dip, path);
		if (dmdp->ddm_name) {
			(void) strcat(path, ":");
			(void) strcat(path, dmdp->ddm_name);
		}
	}
	return (path);
}

static char *
pathname_work_obp(dev_info_t *dip, char *path)
{
	char *bp;
	char *obp_path;

	/*
	 * look up the "obp-path" property, return the path if it exists
	 */
	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
	    "obp-path", &obp_path) == DDI_PROP_SUCCESS) {
		(void) strcpy(path, obp_path);
		ddi_prop_free(obp_path);
		return (path);
	}

	/*
	 * stop at root, no obp path
	 */
	if (dip == ddi_root_node()) {
		return (NULL);
	}

	obp_path = pathname_work_obp(ddi_get_parent(dip), path);
	if (obp_path == NULL)
		return (NULL);

	/*
	 * append our component to parent's obp path
	 */
	bp = path + strlen(path);
	if (*(bp - 1) != '/')
		(void) strcat(bp++, "/");
	(void) ddi_deviname(dip, bp);
	return (path);
}

/*
 * return the 'obp-path' based path for the given node, or NULL if the node
 * does not have a different obp path. NOTE: Unlike ddi_pathname, this
 * function can't be called from interrupt context (since we need to
 * lookup a string property).
 */
char *
ddi_pathname_obp(dev_info_t *dip, char *path)
{
	ASSERT(!servicing_interrupt());
	if (dip == NULL || path == NULL)
		return (NULL);

	/* split work into a separate function to aid debugging */
	return (pathname_work_obp(dip, path));
}

int
ddi_pathname_obp_set(dev_info_t *dip, char *component)
{
	dev_info_t *pdip;
	char *obp_path = NULL;
	int rc = DDI_FAILURE;

	if (dip == NULL)
		return (DDI_FAILURE);

	obp_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);

	pdip = ddi_get_parent(dip);

	if (ddi_pathname_obp(pdip, obp_path) == NULL) {
		(void) ddi_pathname(pdip, obp_path);
	}

	if (component) {
		(void) strncat(obp_path, "/", MAXPATHLEN);
		(void) strncat(obp_path, component, MAXPATHLEN);
	}
	rc = ndi_prop_update_string(DDI_DEV_T_NONE, dip, "obp-path",
	    obp_path);

	if (obp_path)
		kmem_free(obp_path, MAXPATHLEN);

	return (rc);
}

/*
 * Given a dev_t, return the pathname of the corresponding device in the
 * buffer pointed at by "path."  The buffer is assumed to be large enough
 * to hold the pathname of the device (MAXPATHLEN).
 *
 * The pathname of a device is the pathname of the devinfo node to which
 * the device "belongs," concatenated with the character ':' and the name
 * of the minor node corresponding to the dev_t.  If spec_type is 0 then
 * just the pathname of the devinfo node is returned without driving attach
 * of that node.  For a non-zero spec_type, an attach is performed and a
 * search of the minor list occurs.
 *
 * It is possible that the path associated with the dev_t is not
 * currently available in the devinfo tree.  In order to have a
 * dev_t, a device must have been discovered before, which means
 * that the path is always in the instance tree.  The one exception
 * to this is if the dev_t is associated with a pseudo driver, in
 * which case the device must exist on the pseudo branch of the
 * devinfo tree as a result of parsing .conf files.
 */
int
ddi_dev_pathname(dev_t devt, int spec_type, char *path)
{
	int		circ;
	major_t		major = getmajor(devt);
	int		instance;
	dev_info_t	*dip;
	char		*minorname;
	char		*drvname;

	if (major >= devcnt)
		goto fail;
	if (major == clone_major) {
		/* clone has no minor nodes, manufacture the path here */
		if ((drvname = ddi_major_to_name(getminor(devt))) == NULL)
			goto fail;

		(void) snprintf(path, MAXPATHLEN, "%s:%s", CLONE_PATH, drvname);
		return (DDI_SUCCESS);
	}

	/* extract instance from devt (getinfo(9E) DDI_INFO_DEVT2INSTANCE). */
	if ((instance = dev_to_instance(devt)) == -1)
		goto fail;

	/* reconstruct the path given the major/instance */
	if (e_ddi_majorinstance_to_path(major, instance, path) != DDI_SUCCESS)
		goto fail;

	/* if spec_type given we must drive attach and search minor nodes */
	if ((spec_type == S_IFCHR) || (spec_type == S_IFBLK)) {
		/* attach the path so we can search minors */
		if ((dip = e_ddi_hold_devi_by_path(path, 0)) == NULL)
			goto fail;

		/* Add minorname to path. */
		ndi_devi_enter(dip, &circ);
		minorname = i_ddi_devtspectype_to_minorname(dip,
		    devt, spec_type);
		if (minorname) {
			(void) strcat(path, ":");
			(void) strcat(path, minorname);
		}
		ndi_devi_exit(dip, circ);
		ddi_release_devi(dip);
		if (minorname == NULL)
			goto fail;
	}
	ASSERT(strlen(path) < MAXPATHLEN);
	return (DDI_SUCCESS);

fail:	*path = 0;
	return (DDI_FAILURE);
}

/*
 * Given a major number and an instance, return the path.
 * This interface does NOT drive attach.
 */
int
e_ddi_majorinstance_to_path(major_t major, int instance, char *path)
{
	struct devnames *dnp;
	dev_info_t	*dip;

	if ((major >= devcnt) || (instance == -1)) {
		*path = 0;
		return (DDI_FAILURE);
	}

	/* look for the major/instance in the instance tree */
	if (e_ddi_instance_majorinstance_to_path(major, instance,
	    path) == DDI_SUCCESS) {
		ASSERT(strlen(path) < MAXPATHLEN);
		return (DDI_SUCCESS);
	}

	/*
	 * Not in instance tree, find the instance on the per driver list and
	 * construct path to instance via ddi_pathname(). This is how paths
	 * down the 'pseudo' branch are constructed.
	 */
	dnp = &(devnamesp[major]);
	LOCK_DEV_OPS(&(dnp->dn_lock));
	for (dip = dnp->dn_head; dip;
	    dip = (dev_info_t *)DEVI(dip)->devi_next) {
		/* Skip if instance does not match. */
		if (DEVI(dip)->devi_instance != instance)
			continue;

		/*
		 * An ndi_hold_devi() does not prevent DS_INITIALIZED->DS_BOUND
		 * node demotion, so it is not an effective way of ensuring
		 * that the ddi_pathname result has a unit-address.  Instead,
		 * we reverify the node state after calling ddi_pathname().
		 */
		if (i_ddi_node_state(dip) >= DS_INITIALIZED) {
			(void) ddi_pathname(dip, path);
			if (i_ddi_node_state(dip) < DS_INITIALIZED)
				continue;
			UNLOCK_DEV_OPS(&(dnp->dn_lock));
			ASSERT(strlen(path) < MAXPATHLEN);
			return (DDI_SUCCESS);
		}
	}
	UNLOCK_DEV_OPS(&(dnp->dn_lock));

	/* can't reconstruct the path */
	*path = 0;
	return (DDI_FAILURE);
}

#define	GLD_DRIVER_PPA "SUNW,gld_v0_ppa"

/*
 * Given the dip for a network interface return the ppa for that interface.
 *
 * In all cases except GLD v0 drivers, the ppa == instance.
 * In the case of GLD v0 drivers, the ppa is equal to the attach order.
 * So for these drivers when the attach routine calls gld_register(),
 * the GLD framework creates an integer property called "gld_driver_ppa"
 * that can be queried here.
 *
 * The only time this function is used is when a system is booting over nfs.
 * In this case the system has to resolve the pathname of the boot device
 * to it's ppa.
 */
int
i_ddi_devi_get_ppa(dev_info_t *dip)
{
	return (ddi_prop_get_int(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
	    GLD_DRIVER_PPA, ddi_get_instance(dip)));
}

/*
 * i_ddi_devi_set_ppa() should only be called from gld_register()
 * and only for GLD v0 drivers
 */
void
i_ddi_devi_set_ppa(dev_info_t *dip, int ppa)
{
	(void) e_ddi_prop_update_int(DDI_DEV_T_NONE, dip, GLD_DRIVER_PPA, ppa);
}


/*
 * Private DDI Console bell functions.
 */
void
ddi_ring_console_bell(clock_t duration)
{
	if (ddi_console_bell_func != NULL)
		(*ddi_console_bell_func)(duration);
}

void
ddi_set_console_bell(void (*bellfunc)(clock_t duration))
{
	ddi_console_bell_func = bellfunc;
}

int
ddi_dma_alloc_handle(dev_info_t *dip, ddi_dma_attr_t *attr,
    int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
{
	int (*funcp)() = ddi_dma_allochdl;
	ddi_dma_attr_t dma_attr;
	struct bus_ops *bop;

	if (attr == (ddi_dma_attr_t *)0)
		return (DDI_DMA_BADATTR);

	dma_attr = *attr;

	bop = DEVI(dip)->devi_ops->devo_bus_ops;
	if (bop && bop->bus_dma_allochdl)
		funcp = bop->bus_dma_allochdl;

	return ((*funcp)(dip, dip, &dma_attr, waitfp, arg, handlep));
}

void
ddi_dma_free_handle(ddi_dma_handle_t *handlep)
{
	ddi_dma_handle_t h = *handlep;
	(void) ddi_dma_freehdl(HD, HD, h);
}

static uintptr_t dma_mem_list_id = 0;


int
ddi_dma_mem_alloc(ddi_dma_handle_t handle, size_t length,
    ddi_device_acc_attr_t *accattrp, uint_t flags,
    int (*waitfp)(caddr_t), caddr_t arg, caddr_t *kaddrp,
    size_t *real_length, ddi_acc_handle_t *handlep)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	dev_info_t *dip = hp->dmai_rdip;
	ddi_acc_hdl_t *ap;
	ddi_dma_attr_t *attrp = &hp->dmai_attr;
	uint_t sleepflag, xfermodes;
	int (*fp)(caddr_t);
	int rval;

	if (waitfp == DDI_DMA_SLEEP)
		fp = (int (*)())KM_SLEEP;
	else if (waitfp == DDI_DMA_DONTWAIT)
		fp = (int (*)())KM_NOSLEEP;
	else
		fp = waitfp;
	*handlep = impl_acc_hdl_alloc(fp, arg);
	if (*handlep == NULL)
		return (DDI_FAILURE);

	/* check if the cache attributes are supported */
	if (i_ddi_check_cache_attr(flags) == B_FALSE)
		return (DDI_FAILURE);

	/*
	 * Transfer the meaningful bits to xfermodes.
	 * Double-check if the 3rd party driver correctly sets the bits.
	 * If not, set DDI_DMA_STREAMING to keep compatibility.
	 */
	xfermodes = flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING);
	if (xfermodes == 0) {
		xfermodes = DDI_DMA_STREAMING;
	}

	/*
	 * initialize the common elements of data access handle
	 */
	ap = impl_acc_hdl_get(*handlep);
	ap->ah_vers = VERS_ACCHDL;
	ap->ah_dip = dip;
	ap->ah_offset = 0;
	ap->ah_len = 0;
	ap->ah_xfermodes = flags;
	ap->ah_acc = *accattrp;

	sleepflag = ((waitfp == DDI_DMA_SLEEP) ? 1 : 0);
	if (xfermodes == DDI_DMA_CONSISTENT) {
		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
		    flags, accattrp, kaddrp, NULL, ap);
		*real_length = length;
	} else {
		rval = i_ddi_mem_alloc(dip, attrp, length, sleepflag,
		    flags, accattrp, kaddrp, real_length, ap);
	}
	if (rval == DDI_SUCCESS) {
		ap->ah_len = (off_t)(*real_length);
		ap->ah_addr = *kaddrp;
	} else {
		impl_acc_hdl_free(*handlep);
		*handlep = (ddi_acc_handle_t)NULL;
		if (waitfp != DDI_DMA_SLEEP && waitfp != DDI_DMA_DONTWAIT) {
			ddi_set_callback(waitfp, arg, &dma_mem_list_id);
		}
		rval = DDI_FAILURE;
	}
	return (rval);
}

void
ddi_dma_mem_free(ddi_acc_handle_t *handlep)
{
	ddi_acc_hdl_t *ap;

	ap = impl_acc_hdl_get(*handlep);
	ASSERT(ap);

	i_ddi_mem_free((caddr_t)ap->ah_addr, ap);

	/*
	 * free the handle
	 */
	impl_acc_hdl_free(*handlep);
	*handlep = (ddi_acc_handle_t)NULL;

	if (dma_mem_list_id != 0) {
		ddi_run_callback(&dma_mem_list_id);
	}
}

int
ddi_dma_buf_bind_handle(ddi_dma_handle_t handle, struct buf *bp,
    uint_t flags, int (*waitfp)(caddr_t), caddr_t arg,
    ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	dev_info_t *dip, *rdip;
	struct ddi_dma_req dmareq;
	int (*funcp)();
	ddi_dma_cookie_t cookie;
	uint_t count;

	if (cookiep == NULL)
		cookiep = &cookie;

	if (ccountp == NULL)
		ccountp = &count;

	dmareq.dmar_flags = flags;
	dmareq.dmar_fp = waitfp;
	dmareq.dmar_arg = arg;
	dmareq.dmar_object.dmao_size = (uint_t)bp->b_bcount;

	if (bp->b_flags & B_PAGEIO) {
		dmareq.dmar_object.dmao_type = DMA_OTYP_PAGES;
		dmareq.dmar_object.dmao_obj.pp_obj.pp_pp = bp->b_pages;
		dmareq.dmar_object.dmao_obj.pp_obj.pp_offset =
		    (uint_t)(((uintptr_t)bp->b_un.b_addr) & MMU_PAGEOFFSET);
	} else {
		dmareq.dmar_object.dmao_obj.virt_obj.v_addr = bp->b_un.b_addr;
		if (bp->b_flags & B_SHADOW) {
			dmareq.dmar_object.dmao_obj.virt_obj.v_priv =
			    bp->b_shadow;
			dmareq.dmar_object.dmao_type = DMA_OTYP_BUFVADDR;
		} else {
			dmareq.dmar_object.dmao_type =
			    (bp->b_flags & (B_PHYS | B_REMAPPED)) ?
			    DMA_OTYP_BUFVADDR : DMA_OTYP_VADDR;
			dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;
		}

		/*
		 * If the buffer has no proc pointer, or the proc
		 * struct has the kernel address space, or the buffer has
		 * been marked B_REMAPPED (meaning that it is now
		 * mapped into the kernel's address space), then
		 * the address space is kas (kernel address space).
		 */
		if ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas) ||
		    (bp->b_flags & B_REMAPPED)) {
			dmareq.dmar_object.dmao_obj.virt_obj.v_as = 0;
		} else {
			dmareq.dmar_object.dmao_obj.virt_obj.v_as =
			    bp->b_proc->p_as;
		}
	}

	dip = rdip = hp->dmai_rdip;
	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
}

int
ddi_dma_addr_bind_handle(ddi_dma_handle_t handle, struct as *as,
    caddr_t addr, size_t len, uint_t flags, int (*waitfp)(caddr_t),
    caddr_t arg, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	dev_info_t *dip, *rdip;
	struct ddi_dma_req dmareq;
	int (*funcp)();
	ddi_dma_cookie_t cookie;
	uint_t count;

	if (len == (uint_t)0) {
		return (DDI_DMA_NOMAPPING);
	}

	if (cookiep == NULL)
		cookiep = &cookie;

	if (ccountp == NULL)
		ccountp = &count;

	dmareq.dmar_flags = flags;
	dmareq.dmar_fp = waitfp;
	dmareq.dmar_arg = arg;
	dmareq.dmar_object.dmao_size = len;
	dmareq.dmar_object.dmao_type = DMA_OTYP_VADDR;
	dmareq.dmar_object.dmao_obj.virt_obj.v_as = as;
	dmareq.dmar_object.dmao_obj.virt_obj.v_addr = addr;
	dmareq.dmar_object.dmao_obj.virt_obj.v_priv = NULL;

	dip = rdip = hp->dmai_rdip;
	if (dip != ddi_root_node())
		dip = (dev_info_t *)DEVI(dip)->devi_bus_dma_bindhdl;
	funcp = DEVI(rdip)->devi_bus_dma_bindfunc;
	return ((*funcp)(dip, rdip, handle, &dmareq, cookiep, ccountp));
}

void
ddi_dma_nextcookie(ddi_dma_handle_t handle, ddi_dma_cookie_t *cookiep)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	ddi_dma_cookie_t *cp;

	if (hp->dmai_curcookie >= hp->dmai_ncookies) {
		panic("ddi_dma_nextcookie() called too many times on handle %p",
		    hp);
	}

	cp = hp->dmai_cookie;
	ASSERT(cp);

	cookiep->dmac_notused = cp->dmac_notused;
	cookiep->dmac_type = cp->dmac_type;
	cookiep->dmac_address = cp->dmac_address;
	cookiep->dmac_size = cp->dmac_size;
	hp->dmai_cookie++;
	hp->dmai_curcookie++;
}

int
ddi_dma_ncookies(ddi_dma_handle_t handle)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;

	return (hp->dmai_ncookies);
}

const ddi_dma_cookie_t *
ddi_dma_cookie_iter(ddi_dma_handle_t handle, const ddi_dma_cookie_t *iter)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	const ddi_dma_cookie_t *base, *end;

	if (hp->dmai_ncookies == 0) {
		return (NULL);
	}

	base = hp->dmai_cookie - hp->dmai_curcookie;
	end = base + hp->dmai_ncookies;
	if (iter == NULL) {
		return (base);
	}

	if ((uintptr_t)iter < (uintptr_t)base ||
	    (uintptr_t)iter >= (uintptr_t)end) {
		return (NULL);
	}

	iter++;
	if (iter == end) {
		return (NULL);
	}

	return (iter);
}

const ddi_dma_cookie_t *
ddi_dma_cookie_get(ddi_dma_handle_t handle, uint_t index)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	const ddi_dma_cookie_t *base;

	if (index >= hp->dmai_ncookies) {
		return (NULL);
	}

	base = hp->dmai_cookie - hp->dmai_curcookie;
	return (base + index);
}

const ddi_dma_cookie_t *
ddi_dma_cookie_one(ddi_dma_handle_t handle)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	const ddi_dma_cookie_t *base;

	if (hp->dmai_ncookies != 1) {
		panic("ddi_dma_cookie_one() called with improper handle %p",
		    hp);
	}
	ASSERT3P(hp->dmai_cookie, !=, NULL);

	base = hp->dmai_cookie - hp->dmai_curcookie;
	return (base);
}

int
ddi_dma_numwin(ddi_dma_handle_t handle, uint_t *nwinp)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	if ((hp->dmai_rflags & DDI_DMA_PARTIAL) == 0) {
		return (DDI_FAILURE);
	} else {
		*nwinp = hp->dmai_nwin;
		return (DDI_SUCCESS);
	}
}

int
ddi_dma_getwin(ddi_dma_handle_t h, uint_t win, off_t *offp,
    size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	int (*funcp)() = ddi_dma_win;
	struct bus_ops *bop;
	ddi_dma_cookie_t cookie;
	uint_t count;

	bop = DEVI(HD)->devi_ops->devo_bus_ops;
	if (bop && bop->bus_dma_win)
		funcp = bop->bus_dma_win;

	if (cookiep == NULL)
		cookiep = &cookie;

	if (ccountp == NULL)
		ccountp = &count;

	return ((*funcp)(HD, HD, h, win, offp, lenp, cookiep, ccountp));
}

int
ddi_dma_set_sbus64(ddi_dma_handle_t h, ulong_t burstsizes)
{
	return (ddi_dma_mctl(HD, HD, h, DDI_DMA_SET_SBUS64, 0,
	    &burstsizes, 0, 0));
}

int
i_ddi_dma_fault_check(ddi_dma_impl_t *hp)
{
	return (hp->dmai_fault);
}

int
ddi_check_dma_handle(ddi_dma_handle_t handle)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	int (*check)(ddi_dma_impl_t *);

	if ((check = hp->dmai_fault_check) == NULL)
		check = i_ddi_dma_fault_check;

	return (((*check)(hp) == DDI_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
}

void
i_ddi_dma_set_fault(ddi_dma_handle_t handle)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	void (*notify)(ddi_dma_impl_t *);

	if (!hp->dmai_fault) {
		hp->dmai_fault = 1;
		if ((notify = hp->dmai_fault_notify) != NULL)
			(*notify)(hp);
	}
}

void
i_ddi_dma_clr_fault(ddi_dma_handle_t handle)
{
	ddi_dma_impl_t *hp = (ddi_dma_impl_t *)handle;
	void (*notify)(ddi_dma_impl_t *);

	if (hp->dmai_fault) {
		hp->dmai_fault = 0;
		if ((notify = hp->dmai_fault_notify) != NULL)
			(*notify)(hp);
	}
}

/*
 * register mapping routines.
 */
int
ddi_regs_map_setup(dev_info_t *dip, uint_t rnumber, caddr_t *addrp,
    offset_t offset, offset_t len, ddi_device_acc_attr_t *accattrp,
    ddi_acc_handle_t *handle)
{
	ddi_map_req_t mr;
	ddi_acc_hdl_t *hp;
	int result;

	/*
	 * Allocate and initialize the common elements of data access handle.
	 */
	*handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
	hp = impl_acc_hdl_get(*handle);
	hp->ah_vers = VERS_ACCHDL;
	hp->ah_dip = dip;
	hp->ah_rnumber = rnumber;
	hp->ah_offset = offset;
	hp->ah_len = len;
	hp->ah_acc = *accattrp;

	/*
	 * Set up the mapping request and call to parent.
	 */
	mr.map_op = DDI_MO_MAP_LOCKED;
	mr.map_type = DDI_MT_RNUMBER;
	mr.map_obj.rnumber = rnumber;
	mr.map_prot = PROT_READ | PROT_WRITE;
	mr.map_flags = DDI_MF_KERNEL_MAPPING;
	mr.map_handlep = hp;
	mr.map_vers = DDI_MAP_VERSION;
	result = ddi_map(dip, &mr, offset, len, addrp);

	/*
	 * check for end result
	 */
	if (result != DDI_SUCCESS) {
		impl_acc_hdl_free(*handle);
		*handle = (ddi_acc_handle_t)NULL;
	} else {
		hp->ah_addr = *addrp;
	}

	return (result);
}

void
ddi_regs_map_free(ddi_acc_handle_t *handlep)
{
	ddi_map_req_t mr;
	ddi_acc_hdl_t *hp;

	hp = impl_acc_hdl_get(*handlep);
	ASSERT(hp);

	mr.map_op = DDI_MO_UNMAP;
	mr.map_type = DDI_MT_RNUMBER;
	mr.map_obj.rnumber = hp->ah_rnumber;
	mr.map_prot = PROT_READ | PROT_WRITE;
	mr.map_flags = DDI_MF_KERNEL_MAPPING;
	mr.map_handlep = hp;
	mr.map_vers = DDI_MAP_VERSION;

	/*
	 * Call my parent to unmap my regs.
	 */
	(void) ddi_map(hp->ah_dip, &mr, hp->ah_offset,
	    hp->ah_len, &hp->ah_addr);
	/*
	 * free the handle
	 */
	impl_acc_hdl_free(*handlep);
	*handlep = (ddi_acc_handle_t)NULL;
}

int
ddi_device_zero(ddi_acc_handle_t handle, caddr_t dev_addr, size_t bytecount,
    ssize_t dev_advcnt, uint_t dev_datasz)
{
	uint8_t *b;
	uint16_t *w;
	uint32_t *l;
	uint64_t *ll;

	/* check for total byte count is multiple of data transfer size */
	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
		return (DDI_FAILURE);

	switch (dev_datasz) {
	case DDI_DATA_SZ01_ACC:
		for (b = (uint8_t *)dev_addr;
		    bytecount != 0; bytecount -= 1, b += dev_advcnt)
			ddi_put8(handle, b, 0);
		break;
	case DDI_DATA_SZ02_ACC:
		for (w = (uint16_t *)dev_addr;
		    bytecount != 0; bytecount -= 2, w += dev_advcnt)
			ddi_put16(handle, w, 0);
		break;
	case DDI_DATA_SZ04_ACC:
		for (l = (uint32_t *)dev_addr;
		    bytecount != 0; bytecount -= 4, l += dev_advcnt)
			ddi_put32(handle, l, 0);
		break;
	case DDI_DATA_SZ08_ACC:
		for (ll = (uint64_t *)dev_addr;
		    bytecount != 0; bytecount -= 8, ll += dev_advcnt)
			ddi_put64(handle, ll, 0x0ll);
		break;
	default:
		return (DDI_FAILURE);
	}
	return (DDI_SUCCESS);
}

int
ddi_device_copy(
	ddi_acc_handle_t src_handle, caddr_t src_addr, ssize_t src_advcnt,
	ddi_acc_handle_t dest_handle, caddr_t dest_addr, ssize_t dest_advcnt,
	size_t bytecount, uint_t dev_datasz)
{
	uint8_t *b_src, *b_dst;
	uint16_t *w_src, *w_dst;
	uint32_t *l_src, *l_dst;
	uint64_t *ll_src, *ll_dst;

	/* check for total byte count is multiple of data transfer size */
	if (bytecount != ((bytecount / dev_datasz) * dev_datasz))
		return (DDI_FAILURE);

	switch (dev_datasz) {
	case DDI_DATA_SZ01_ACC:
		b_src = (uint8_t *)src_addr;
		b_dst = (uint8_t *)dest_addr;

		for (; bytecount != 0; bytecount -= 1) {
			ddi_put8(dest_handle, b_dst,
			    ddi_get8(src_handle, b_src));
			b_dst += dest_advcnt;
			b_src += src_advcnt;
		}
		break;
	case DDI_DATA_SZ02_ACC:
		w_src = (uint16_t *)src_addr;
		w_dst = (uint16_t *)dest_addr;

		for (; bytecount != 0; bytecount -= 2) {
			ddi_put16(dest_handle, w_dst,
			    ddi_get16(src_handle, w_src));
			w_dst += dest_advcnt;
			w_src += src_advcnt;
		}
		break;
	case DDI_DATA_SZ04_ACC:
		l_src = (uint32_t *)src_addr;
		l_dst = (uint32_t *)dest_addr;

		for (; bytecount != 0; bytecount -= 4) {
			ddi_put32(dest_handle, l_dst,
			    ddi_get32(src_handle, l_src));
			l_dst += dest_advcnt;
			l_src += src_advcnt;
		}
		break;
	case DDI_DATA_SZ08_ACC:
		ll_src = (uint64_t *)src_addr;
		ll_dst = (uint64_t *)dest_addr;

		for (; bytecount != 0; bytecount -= 8) {
			ddi_put64(dest_handle, ll_dst,
			    ddi_get64(src_handle, ll_src));
			ll_dst += dest_advcnt;
			ll_src += src_advcnt;
		}
		break;
	default:
		return (DDI_FAILURE);
	}
	return (DDI_SUCCESS);
}

#define	swap16(value)  \
	((((value) & 0xff) << 8) | ((value) >> 8))

#define	swap32(value)	\
	(((uint32_t)swap16((uint16_t)((value) & 0xffff)) << 16) | \
	(uint32_t)swap16((uint16_t)((value) >> 16)))

#define	swap64(value)	\
	(((uint64_t)swap32((uint32_t)((value) & 0xffffffff)) \
	    << 32) | \
	(uint64_t)swap32((uint32_t)((value) >> 32)))

uint16_t
ddi_swap16(uint16_t value)
{
	return (swap16(value));
}

uint32_t
ddi_swap32(uint32_t value)
{
	return (swap32(value));
}

uint64_t
ddi_swap64(uint64_t value)
{
	return (swap64(value));
}

/*
 * Convert a binding name to a driver name.
 * A binding name is the name used to determine the driver for a
 * device - it may be either an alias for the driver or the name
 * of the driver itself.
 */
char *
i_binding_to_drv_name(char *bname)
{
	major_t major_no;

	ASSERT(bname != NULL);

	if ((major_no = ddi_name_to_major(bname)) == -1)
		return (NULL);
	return (ddi_major_to_name(major_no));
}

/*
 * Search for minor name that has specified dev_t and spec_type.
 * If spec_type is zero then any dev_t match works.  Since we
 * are returning a pointer to the minor name string, we require the
 * caller to do the locking.
 */
char *
i_ddi_devtspectype_to_minorname(dev_info_t *dip, dev_t dev, int spec_type)
{
	struct ddi_minor_data	*dmdp;

	/*
	 * The did layered driver currently intentionally returns a
	 * devinfo ptr for an underlying sd instance based on a did
	 * dev_t. In this case it is not an error.
	 *
	 * The did layered driver is associated with Sun Cluster.
	 */
	ASSERT((ddi_driver_major(dip) == getmajor(dev)) ||
	    (strcmp(ddi_major_to_name(getmajor(dev)), "did") == 0));

	ASSERT(DEVI_BUSY_OWNED(dip));
	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
		if (((dmdp->type == DDM_MINOR) ||
		    (dmdp->type == DDM_INTERNAL_PATH) ||
		    (dmdp->type == DDM_DEFAULT)) &&
		    (dmdp->ddm_dev == dev) &&
		    ((((spec_type & (S_IFCHR|S_IFBLK))) == 0) ||
		    (dmdp->ddm_spec_type == spec_type)))
			return (dmdp->ddm_name);
	}

	return (NULL);
}

/*
 * Find the devt and spectype of the specified minor_name.
 * Return DDI_FAILURE if minor_name not found. Since we are
 * returning everything via arguments we can do the locking.
 */
int
i_ddi_minorname_to_devtspectype(dev_info_t *dip, char *minor_name,
    dev_t *devtp, int *spectypep)
{
	int			circ;
	struct ddi_minor_data	*dmdp;

	/* deal with clone minor nodes */
	if (dip == clone_dip) {
		major_t	major;
		/*
		 * Make sure minor_name is a STREAMS driver.
		 * We load the driver but don't attach to any instances.
		 */

		major = ddi_name_to_major(minor_name);
		if (major == DDI_MAJOR_T_NONE)
			return (DDI_FAILURE);

		if (ddi_hold_driver(major) == NULL)
			return (DDI_FAILURE);

		if (STREAMSTAB(major) == NULL) {
			ddi_rele_driver(major);
			return (DDI_FAILURE);
		}
		ddi_rele_driver(major);

		if (devtp)
			*devtp = makedevice(clone_major, (minor_t)major);

		if (spectypep)
			*spectypep = S_IFCHR;

		return (DDI_SUCCESS);
	}

	ndi_devi_enter(dip, &circ);
	for (dmdp = DEVI(dip)->devi_minor; dmdp; dmdp = dmdp->next) {
		if (((dmdp->type != DDM_MINOR) &&
		    (dmdp->type != DDM_INTERNAL_PATH) &&
		    (dmdp->type != DDM_DEFAULT)) ||
		    strcmp(minor_name, dmdp->ddm_name))
			continue;

		if (devtp)
			*devtp = dmdp->ddm_dev;

		if (spectypep)
			*spectypep = dmdp->ddm_spec_type;

		ndi_devi_exit(dip, circ);
		return (DDI_SUCCESS);
	}
	ndi_devi_exit(dip, circ);

	return (DDI_FAILURE);
}

static kmutex_t devid_gen_mutex;
static short	devid_gen_number;

#ifdef DEBUG

static int	devid_register_corrupt = 0;
static int	devid_register_corrupt_major = 0;
static int	devid_register_corrupt_hint = 0;
static int	devid_register_corrupt_hint_major = 0;

static int devid_lyr_debug = 0;

#define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)		\
	if (devid_lyr_debug)					\
		ddi_debug_devid_devts(msg, ndevs, devs)

#else

#define	DDI_DEBUG_DEVID_DEVTS(msg, ndevs, devs)

#endif /* DEBUG */


#ifdef	DEBUG

static void
ddi_debug_devid_devts(char *msg, int ndevs, dev_t *devs)
{
	int i;

	cmn_err(CE_CONT, "%s:\n", msg);
	for (i = 0; i < ndevs; i++) {
		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
	}
}

static void
ddi_debug_devid_paths(char *msg, int npaths, char **paths)
{
	int i;

	cmn_err(CE_CONT, "%s:\n", msg);
	for (i = 0; i < npaths; i++) {
		cmn_err(CE_CONT, "    %s\n", paths[i]);
	}
}

static void
ddi_debug_devid_devts_per_path(char *path, int ndevs, dev_t *devs)
{
	int i;

	cmn_err(CE_CONT, "dev_ts per path %s\n", path);
	for (i = 0; i < ndevs; i++) {
		cmn_err(CE_CONT, "    0x%lx\n", devs[i]);
	}
}

#endif	/* DEBUG */

/*
 * Register device id into DDI framework.
 * Must be called when the driver is bound.
 */
static int
i_ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
{
	impl_devid_t	*i_devid = (impl_devid_t *)devid;
	size_t		driver_len;
	const char	*driver_name;
	char		*devid_str;
	major_t		major;

	if ((dip == NULL) ||
	    ((major = ddi_driver_major(dip)) == DDI_MAJOR_T_NONE))
		return (DDI_FAILURE);

	/* verify that the devid is valid */
	if (ddi_devid_valid(devid) != DDI_SUCCESS)
		return (DDI_FAILURE);

	/* Updating driver name hint in devid */
	driver_name = ddi_driver_name(dip);
	driver_len = strlen(driver_name);
	if (driver_len > DEVID_HINT_SIZE) {
		/* Pick up last four characters of driver name */
		driver_name += driver_len - DEVID_HINT_SIZE;
		driver_len = DEVID_HINT_SIZE;
	}
	bzero(i_devid->did_driver, DEVID_HINT_SIZE);
	bcopy(driver_name, i_devid->did_driver, driver_len);

#ifdef DEBUG
	/* Corrupt the devid for testing. */
	if (devid_register_corrupt)
		i_devid->did_id[0] += devid_register_corrupt;
	if (devid_register_corrupt_major &&
	    (major == devid_register_corrupt_major))
		i_devid->did_id[0] += 1;
	if (devid_register_corrupt_hint)
		i_devid->did_driver[0] += devid_register_corrupt_hint;
	if (devid_register_corrupt_hint_major &&
	    (major == devid_register_corrupt_hint_major))
		i_devid->did_driver[0] += 1;
#endif /* DEBUG */

	/* encode the devid as a string */
	if ((devid_str = ddi_devid_str_encode(devid, NULL)) == NULL)
		return (DDI_FAILURE);

	/* add string as a string property */
	if (ndi_prop_update_string(DDI_DEV_T_NONE, dip,
	    DEVID_PROP_NAME, devid_str) != DDI_SUCCESS) {
		cmn_err(CE_WARN, "%s%d: devid property update failed",
		    ddi_driver_name(dip), ddi_get_instance(dip));
		ddi_devid_str_free(devid_str);
		return (DDI_FAILURE);
	}

	/* keep pointer to devid string for interrupt context fma code */
	if (DEVI(dip)->devi_devid_str)
		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
	DEVI(dip)->devi_devid_str = devid_str;
	return (DDI_SUCCESS);
}

int
ddi_devid_register(dev_info_t *dip, ddi_devid_t devid)
{
	int rval;

	rval = i_ddi_devid_register(dip, devid);
	if (rval == DDI_SUCCESS) {
		/*
		 * Register devid in devid-to-path cache
		 */
		if (e_devid_cache_register(dip, devid) == DDI_SUCCESS) {
			mutex_enter(&DEVI(dip)->devi_lock);
			DEVI(dip)->devi_flags |= DEVI_CACHED_DEVID;
			mutex_exit(&DEVI(dip)->devi_lock);
		} else if (ddi_get_name_addr(dip)) {
			/*
			 * We only expect cache_register DDI_FAILURE when we
			 * can't form the full path because of NULL devi_addr.
			 */
			cmn_err(CE_WARN, "%s%d: failed to cache devid",
			    ddi_driver_name(dip), ddi_get_instance(dip));
		}
	} else {
		cmn_err(CE_WARN, "%s%d: failed to register devid",
		    ddi_driver_name(dip), ddi_get_instance(dip));
	}
	return (rval);
}

/*
 * Remove (unregister) device id from DDI framework.
 * Must be called when device is detached.
 */
static void
i_ddi_devid_unregister(dev_info_t *dip)
{
	if (DEVI(dip)->devi_devid_str) {
		ddi_devid_str_free(DEVI(dip)->devi_devid_str);
		DEVI(dip)->devi_devid_str = NULL;
	}

	/* remove the devid property */
	(void) ndi_prop_remove(DDI_DEV_T_NONE, dip, DEVID_PROP_NAME);
}

void
ddi_devid_unregister(dev_info_t *dip)
{
	mutex_enter(&DEVI(dip)->devi_lock);
	DEVI(dip)->devi_flags &= ~DEVI_CACHED_DEVID;
	mutex_exit(&DEVI(dip)->devi_lock);
	e_devid_cache_unregister(dip);
	i_ddi_devid_unregister(dip);
}

/*
 * Allocate and initialize a device id.
 */
int
ddi_devid_init(
	dev_info_t	*dip,
	ushort_t	devid_type,
	ushort_t	nbytes,
	void		*id,
	ddi_devid_t	*ret_devid)
{
	impl_devid_t	*i_devid;
	int		sz = sizeof (*i_devid) + nbytes - sizeof (char);
	int		driver_len;
	const char	*driver_name;

	switch (devid_type) {
	case DEVID_SCSI3_WWN:
		/*FALLTHRU*/
	case DEVID_SCSI_SERIAL:
		/*FALLTHRU*/
	case DEVID_ATA_SERIAL:
		/*FALLTHRU*/
	case DEVID_NVME_NSID:
		/*FALLTHRU*/
	case DEVID_NVME_EUI64:
		/*FALLTHRU*/
	case DEVID_NVME_NGUID:
		/*FALLTHRU*/
	case DEVID_ENCAP:
		if (nbytes == 0)
			return (DDI_FAILURE);
		if (id == NULL)
			return (DDI_FAILURE);
		break;
	case DEVID_FAB:
		if (nbytes != 0)
			return (DDI_FAILURE);
		if (id != NULL)
			return (DDI_FAILURE);
		nbytes = sizeof (int) +
		    sizeof (struct timeval32) + sizeof (short);
		sz += nbytes;
		break;
	default:
		return (DDI_FAILURE);
	}

	if ((i_devid = kmem_zalloc(sz, KM_SLEEP)) == NULL)
		return (DDI_FAILURE);

	i_devid->did_magic_hi = DEVID_MAGIC_MSB;
	i_devid->did_magic_lo = DEVID_MAGIC_LSB;
	i_devid->did_rev_hi = DEVID_REV_MSB;
	i_devid->did_rev_lo = DEVID_REV_LSB;
	DEVID_FORMTYPE(i_devid, devid_type);
	DEVID_FORMLEN(i_devid, nbytes);

	/* Fill in driver name hint */
	driver_name = ddi_driver_name(dip);
	driver_len = strlen(driver_name);
	if (driver_len > DEVID_HINT_SIZE) {
		/* Pick up last four characters of driver name */
		driver_name += driver_len - DEVID_HINT_SIZE;
		driver_len = DEVID_HINT_SIZE;
	}

	bcopy(driver_name, i_devid->did_driver, driver_len);

	/* Fill in id field */
	if (devid_type == DEVID_FAB) {
		char		*cp;
		uint32_t	hostid;
		struct timeval32 timestamp32;
		int		i;
		int		*ip;
		short		gen;

		/* increase the generation number */
		mutex_enter(&devid_gen_mutex);
		gen = devid_gen_number++;
		mutex_exit(&devid_gen_mutex);

		cp = i_devid->did_id;

		/* Fill in host id (big-endian byte ordering) */
		hostid = zone_get_hostid(NULL);
		*cp++ = hibyte(hiword(hostid));
		*cp++ = lobyte(hiword(hostid));
		*cp++ = hibyte(loword(hostid));
		*cp++ = lobyte(loword(hostid));

		/*
		 * Fill in timestamp (big-endian byte ordering)
		 *
		 * (Note that the format may have to be changed
		 * before 2038 comes around, though it's arguably
		 * unique enough as it is..)
		 */
		uniqtime32(&timestamp32);
		ip = (int *)&timestamp32;
		for (i = 0;
		    i < sizeof (timestamp32) / sizeof (int); i++, ip++) {
			int	val;
			val = *ip;
			*cp++ = hibyte(hiword(val));
			*cp++ = lobyte(hiword(val));
			*cp++ = hibyte(loword(val));
			*cp++ = lobyte(loword(val));
		}

		/* fill in the generation number */
		*cp++ = hibyte(gen);
		*cp++ = lobyte(gen);
	} else
		bcopy(id, i_devid->did_id, nbytes);

	/* return device id */
	*ret_devid = (ddi_devid_t)i_devid;
	return (DDI_SUCCESS);
}

int
ddi_devid_get(dev_info_t *dip, ddi_devid_t *ret_devid)
{
	return (i_ddi_devi_get_devid(DDI_DEV_T_ANY, dip, ret_devid));
}

int
i_ddi_devi_get_devid(dev_t dev, dev_info_t *dip, ddi_devid_t *ret_devid)
{
	char		*devidstr;

	ASSERT(dev != DDI_DEV_T_NONE);

	/* look up the property, devt specific first */
	if (ddi_prop_lookup_string(dev, dip, DDI_PROP_DONTPASS,
	    DEVID_PROP_NAME, &devidstr) != DDI_PROP_SUCCESS) {
		if ((dev == DDI_DEV_T_ANY) ||
		    (ddi_prop_lookup_string(DDI_DEV_T_ANY, dip,
		    DDI_PROP_DONTPASS, DEVID_PROP_NAME, &devidstr) !=
		    DDI_PROP_SUCCESS)) {
			return (DDI_FAILURE);
		}
	}

	/* convert to binary form */
	if (ddi_devid_str_decode(devidstr, ret_devid, NULL) == -1) {
		ddi_prop_free(devidstr);
		return (DDI_FAILURE);
	}
	ddi_prop_free(devidstr);
	return (DDI_SUCCESS);
}

/*
 * Return a copy of the device id for dev_t
 */
int
ddi_lyr_get_devid(dev_t dev, ddi_devid_t *ret_devid)
{
	dev_info_t	*dip;
	int		rval;

	/* get the dip */
	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
		return (DDI_FAILURE);

	rval = i_ddi_devi_get_devid(dev, dip, ret_devid);

	ddi_release_devi(dip);		/* e_ddi_hold_devi_by_dev() */
	return (rval);
}

/*
 * Return a copy of the minor name for dev_t and spec_type
 */
int
ddi_lyr_get_minor_name(dev_t dev, int spec_type, char **minor_name)
{
	char		*buf;
	int		circ;
	dev_info_t	*dip;
	char		*nm;
	int		rval;

	if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) {
		*minor_name = NULL;
		return (DDI_FAILURE);
	}

	/* Find the minor name and copy into max size buf */
	buf = kmem_alloc(MAXNAMELEN, KM_SLEEP);
	ndi_devi_enter(dip, &circ);
	nm = i_ddi_devtspectype_to_minorname(dip, dev, spec_type);
	if (nm)
		(void) strcpy(buf, nm);
	ndi_devi_exit(dip, circ);
	ddi_release_devi(dip);	/* e_ddi_hold_devi_by_dev() */

	if (nm) {
		/* duplicate into min size buf for return result */
		*minor_name = i_ddi_strdup(buf, KM_SLEEP);
		rval = DDI_SUCCESS;
	} else {
		*minor_name = NULL;
		rval = DDI_FAILURE;
	}

	/* free max size buf and return */
	kmem_free(buf, MAXNAMELEN);
	return (rval);
}

int
ddi_lyr_devid_to_devlist(
	ddi_devid_t	devid,
	char		*minor_name,
	int		*retndevs,
	dev_t		**retdevs)
{
	ASSERT(ddi_devid_valid(devid) == DDI_SUCCESS);

	if (e_devid_cache_to_devt_list(devid, minor_name,
	    retndevs, retdevs) == DDI_SUCCESS) {
		ASSERT(*retndevs > 0);
		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
		    *retndevs, *retdevs);
		return (DDI_SUCCESS);
	}

	if (e_ddi_devid_discovery(devid) == DDI_FAILURE) {
		return (DDI_FAILURE);
	}

	if (e_devid_cache_to_devt_list(devid, minor_name,
	    retndevs, retdevs) == DDI_SUCCESS) {
		ASSERT(*retndevs > 0);
		DDI_DEBUG_DEVID_DEVTS("ddi_lyr_devid_to_devlist",
		    *retndevs, *retdevs);
		return (DDI_SUCCESS);
	}

	return (DDI_FAILURE);
}

void
ddi_lyr_free_devlist(dev_t *devlist, int ndevs)
{
	kmem_free(devlist, sizeof (dev_t) * ndevs);
}

/*
 * Note: This will need to be fixed if we ever allow processes to
 * have more than one data model per exec.
 */
model_t
ddi_mmap_get_model(void)
{
	return (get_udatamodel());
}

model_t
ddi_model_convert_from(model_t model)
{
	return ((model & DDI_MODEL_MASK) & ~DDI_MODEL_NATIVE);
}

/*
 * ddi interfaces managing storage and retrieval of eventcookies.
 */

/*
 * Invoke bus nexus driver's implementation of the
 * (*bus_remove_eventcall)() interface to remove a registered
 * callback handler for "event".
 */
int
ddi_remove_event_handler(ddi_callback_id_t id)
{
	ndi_event_callbacks_t *cb = (ndi_event_callbacks_t *)id;
	dev_info_t *ddip;

	ASSERT(cb);
	if (!cb) {
		return (DDI_FAILURE);
	}

	ddip = NDI_EVENT_DDIP(cb->ndi_evtcb_cookie);
	return (ndi_busop_remove_eventcall(ddip, id));
}

/*
 * Invoke bus nexus driver's implementation of the
 * (*bus_add_eventcall)() interface to register a callback handler
 * for "event".
 */
int
ddi_add_event_handler(dev_info_t *dip, ddi_eventcookie_t event,
    void (*handler)(dev_info_t *, ddi_eventcookie_t, void *, void *),
    void *arg, ddi_callback_id_t *id)
{
	return (ndi_busop_add_eventcall(dip, dip, event, handler, arg, id));
}


/*
 * Return a handle for event "name" by calling up the device tree
 * hierarchy via  (*bus_get_eventcookie)() interface until claimed
 * by a bus nexus or top of dev_info tree is reached.
 */
int
ddi_get_eventcookie(dev_info_t *dip, char *name,
    ddi_eventcookie_t *event_cookiep)
{
	return (ndi_busop_get_eventcookie(dip, dip,
	    name, event_cookiep));
}

/*
 * This procedure is provided as the general callback function when
 * umem_lockmemory calls as_add_callback for long term memory locking.
 * When as_unmap, as_setprot, or as_free encounter segments which have
 * locked memory, this callback will be invoked.
 */
void
umem_lock_undo(struct as *as, void *arg, uint_t event)
{
	_NOTE(ARGUNUSED(as, event))
	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)arg;

	/*
	 * Call the cleanup function.  Decrement the cookie reference
	 * count, if it goes to zero, return the memory for the cookie.
	 * The i_ddi_umem_unlock for this cookie may or may not have been
	 * called already.  It is the responsibility of the caller of
	 * umem_lockmemory to handle the case of the cleanup routine
	 * being called after a ddi_umem_unlock for the cookie
	 * was called.
	 */

	(*cp->callbacks.cbo_umem_lock_cleanup)((ddi_umem_cookie_t)cp);

	/* remove the cookie if reference goes to zero */
	if (atomic_dec_ulong_nv((ulong_t *)(&(cp->cook_refcnt))) == 0) {
		kmem_free(cp, sizeof (struct ddi_umem_cookie));
	}
}

/*
 * The following two Consolidation Private routines provide generic
 * interfaces to increase/decrease the amount of device-locked memory.
 *
 * To keep project_rele and project_hold consistent, i_ddi_decr_locked_memory()
 * must be called every time i_ddi_incr_locked_memory() is called.
 */
int
/* ARGSUSED */
i_ddi_incr_locked_memory(proc_t *procp, rctl_qty_t inc)
{
	ASSERT(procp != NULL);
	mutex_enter(&procp->p_lock);
	if (rctl_incr_locked_mem(procp, NULL, inc, 1)) {
		mutex_exit(&procp->p_lock);
		return (ENOMEM);
	}
	mutex_exit(&procp->p_lock);
	return (0);
}

/*
 * To keep project_rele and project_hold consistent, i_ddi_incr_locked_memory()
 * must be called every time i_ddi_decr_locked_memory() is called.
 */
/* ARGSUSED */
void
i_ddi_decr_locked_memory(proc_t *procp, rctl_qty_t dec)
{
	ASSERT(procp != NULL);
	mutex_enter(&procp->p_lock);
	rctl_decr_locked_mem(procp, NULL, dec, 1);
	mutex_exit(&procp->p_lock);
}

/*
 * The cookie->upd_max_lock_rctl flag is used to determine if we should
 * charge device locked memory to the max-locked-memory rctl.  Tracking
 * device locked memory causes the rctl locks to get hot under high-speed
 * I/O such as RDSv3 over IB.  If there is no max-locked-memory rctl limit,
 * we bypass charging the locked memory to the rctl altogether.  The cookie's
 * flag tells us if the rctl value should be updated when unlocking the memory,
 * in case the rctl gets changed after the memory was locked.  Any device
 * locked memory in that rare case will not be counted toward the rctl limit.
 *
 * When tracking the locked memory, the kproject_t parameter is always NULL
 * in the code paths:
 *	i_ddi_incr_locked_memory -> rctl_incr_locked_mem
 *	i_ddi_decr_locked_memory -> rctl_decr_locked_mem
 * Thus, we always use the tk_proj member to check the projp setting.
 */
static void
init_lockedmem_rctl_flag(struct ddi_umem_cookie *cookie)
{
	proc_t		*p;
	kproject_t	*projp;
	zone_t		*zonep;

	ASSERT(cookie);
	p = cookie->procp;
	ASSERT(p);

	zonep = p->p_zone;
	projp = p->p_task->tk_proj;

	ASSERT(zonep);
	ASSERT(projp);

	if (zonep->zone_locked_mem_ctl == UINT64_MAX &&
	    projp->kpj_data.kpd_locked_mem_ctl == UINT64_MAX)
		cookie->upd_max_lock_rctl = 0;
	else
		cookie->upd_max_lock_rctl = 1;
}

/*
 * This routine checks if the max-locked-memory resource ctl is
 * exceeded, if not increments it, grabs a hold on the project.
 * Returns 0 if successful otherwise returns error code
 */
static int
umem_incr_devlockmem(struct ddi_umem_cookie *cookie)
{
	proc_t		*procp;
	int		ret;

	ASSERT(cookie);
	if (cookie->upd_max_lock_rctl == 0)
		return (0);

	procp = cookie->procp;
	ASSERT(procp);

	if ((ret = i_ddi_incr_locked_memory(procp,
	    cookie->size)) != 0) {
		return (ret);
	}
	return (0);
}

/*
 * Decrements the max-locked-memory resource ctl and releases
 * the hold on the project that was acquired during umem_incr_devlockmem
 */
static void
umem_decr_devlockmem(struct ddi_umem_cookie *cookie)
{
	proc_t		*proc;

	if (cookie->upd_max_lock_rctl == 0)
		return;

	proc = (proc_t *)cookie->procp;
	if (!proc)
		return;

	i_ddi_decr_locked_memory(proc, cookie->size);
}

/*
 * A consolidation private function which is essentially equivalent to
 * ddi_umem_lock but with the addition of arguments ops_vector and procp.
 * A call to as_add_callback is done if DDI_UMEMLOCK_LONGTERM is set, and
 * the ops_vector is valid.
 *
 * Lock the virtual address range in the current process and create a
 * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
 * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
 * to user space.
 *
 * Note: The resource control accounting currently uses a full charge model
 * in other words attempts to lock the same/overlapping areas of memory
 * will deduct the full size of the buffer from the projects running
 * counter for the device locked memory.
 *
 * addr, size should be PAGESIZE aligned
 *
 * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
 *	identifies whether the locked memory will be read or written or both
 *      DDI_UMEMLOCK_LONGTERM  must be set when the locking will
 * be maintained for an indefinitely long period (essentially permanent),
 * rather than for what would be required for a typical I/O completion.
 * When DDI_UMEMLOCK_LONGTERM is set, umem_lockmemory will return EFAULT
 * if the memory pertains to a regular file which is mapped MAP_SHARED.
 * This is to prevent a deadlock if a file truncation is attempted after
 * after the locking is done.
 *
 * Returns 0 on success
 *	EINVAL - for invalid parameters
 *	EPERM, ENOMEM and other error codes returned by as_pagelock
 *	ENOMEM - is returned if the current request to lock memory exceeds
 *		*.max-locked-memory resource control value.
 *      EFAULT - memory pertains to a regular file mapped shared and
 *		and DDI_UMEMLOCK_LONGTERM flag is set
 *	EAGAIN - could not start the ddi_umem_unlock list processing thread
 */
int
umem_lockmemory(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie,
    struct umem_callback_ops *ops_vector,
    proc_t *procp)
{
	int	error;
	struct ddi_umem_cookie *p;
	void	(*driver_callback)() = NULL;
	struct as *as;
	struct seg		*seg;
	vnode_t			*vp;

	/* Allow device drivers to not have to reference "curproc" */
	if (procp == NULL)
		procp = curproc;
	as = procp->p_as;
	*cookie = NULL;		/* in case of any error return */

	/* These are the only three valid flags */
	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE |
	    DDI_UMEMLOCK_LONGTERM)) != 0)
		return (EINVAL);

	/* At least one (can be both) of the two access flags must be set */
	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0)
		return (EINVAL);

	/* addr and len must be page-aligned */
	if (((uintptr_t)addr & PAGEOFFSET) != 0)
		return (EINVAL);

	if ((len & PAGEOFFSET) != 0)
		return (EINVAL);

	/*
	 * For longterm locking a driver callback must be specified; if
	 * not longterm then a callback is optional.
	 */
	if (ops_vector != NULL) {
		if (ops_vector->cbo_umem_callback_version !=
		    UMEM_CALLBACK_VERSION)
			return (EINVAL);
		else
			driver_callback = ops_vector->cbo_umem_lock_cleanup;
	}
	if ((driver_callback == NULL) && (flags & DDI_UMEMLOCK_LONGTERM))
		return (EINVAL);

	/*
	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
	 * be called on first ddi_umem_lock or umem_lockmemory call.
	 */
	if (ddi_umem_unlock_thread == NULL)
		i_ddi_umem_unlock_thread_start();

	/* Allocate memory for the cookie */
	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);

	/* Convert the flags to seg_rw type */
	if (flags & DDI_UMEMLOCK_WRITE) {
		p->s_flags = S_WRITE;
	} else {
		p->s_flags = S_READ;
	}

	/* Store procp in cookie for later iosetup/unlock */
	p->procp = (void *)procp;

	/*
	 * Store the struct as pointer in cookie for later use by
	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
	 * is called after relvm is called.
	 */
	p->asp = as;

	/*
	 * The size field is needed for lockmem accounting.
	 */
	p->size = len;
	init_lockedmem_rctl_flag(p);

	if (umem_incr_devlockmem(p) != 0) {
		/*
		 * The requested memory cannot be locked
		 */
		kmem_free(p, sizeof (struct ddi_umem_cookie));
		*cookie = (ddi_umem_cookie_t)NULL;
		return (ENOMEM);
	}

	/* Lock the pages corresponding to addr, len in memory */
	error = as_pagelock(as, &(p->pparray), addr, len, p->s_flags);
	if (error != 0) {
		umem_decr_devlockmem(p);
		kmem_free(p, sizeof (struct ddi_umem_cookie));
		*cookie = (ddi_umem_cookie_t)NULL;
		return (error);
	}

	/*
	 * For longterm locking the addr must pertain to a seg_vn segment or
	 * or a seg_spt segment.
	 * If the segment pertains to a regular file, it cannot be
	 * mapped MAP_SHARED.
	 * This is to prevent a deadlock if a file truncation is attempted
	 * after the locking is done.
	 * Doing this after as_pagelock guarantees persistence of the as; if
	 * an unacceptable segment is found, the cleanup includes calling
	 * as_pageunlock before returning EFAULT.
	 *
	 * segdev is allowed here as it is already locked.  This allows
	 * for memory exported by drivers through mmap() (which is already
	 * locked) to be allowed for LONGTERM.
	 */
	if (flags & DDI_UMEMLOCK_LONGTERM) {
		extern  struct seg_ops segspt_shmops;
		extern	struct seg_ops segdev_ops;
		AS_LOCK_ENTER(as, RW_READER);
		for (seg = as_segat(as, addr); ; seg = AS_SEGNEXT(as, seg)) {
			if (seg == NULL || seg->s_base > addr + len)
				break;
			if (seg->s_ops == &segdev_ops)
				continue;
			if (((seg->s_ops != &segvn_ops) &&
			    (seg->s_ops != &segspt_shmops)) ||
			    ((SEGOP_GETVP(seg, addr, &vp) == 0 &&
			    vp != NULL && vp->v_type == VREG) &&
			    (SEGOP_GETTYPE(seg, addr) & MAP_SHARED))) {
				as_pageunlock(as, p->pparray,
				    addr, len, p->s_flags);
				AS_LOCK_EXIT(as);
				umem_decr_devlockmem(p);
				kmem_free(p, sizeof (struct ddi_umem_cookie));
				*cookie = (ddi_umem_cookie_t)NULL;
				return (EFAULT);
			}
		}
		AS_LOCK_EXIT(as);
	}


	/* Initialize the fields in the ddi_umem_cookie */
	p->cvaddr = addr;
	p->type = UMEM_LOCKED;
	if (driver_callback != NULL) {
		/* i_ddi_umem_unlock and umem_lock_undo may need the cookie */
		p->cook_refcnt = 2;
		p->callbacks = *ops_vector;
	} else {
		/* only i_ddi_umme_unlock needs the cookie */
		p->cook_refcnt = 1;
	}

	*cookie = (ddi_umem_cookie_t)p;

	/*
	 * If a driver callback was specified, add an entry to the
	 * as struct callback list. The as_pagelock above guarantees
	 * the persistence of as.
	 */
	if (driver_callback) {
		error = as_add_callback(as, umem_lock_undo, p, AS_ALL_EVENT,
		    addr, len, KM_SLEEP);
		if (error != 0) {
			as_pageunlock(as, p->pparray,
			    addr, len, p->s_flags);
			umem_decr_devlockmem(p);
			kmem_free(p, sizeof (struct ddi_umem_cookie));
			*cookie = (ddi_umem_cookie_t)NULL;
		}
	}
	return (error);
}

/*
 * Unlock the pages locked by ddi_umem_lock or umem_lockmemory and free
 * the cookie.  Called from i_ddi_umem_unlock_thread.
 */

static void
i_ddi_umem_unlock(struct ddi_umem_cookie *p)
{
	uint_t	rc;

	/*
	 * There is no way to determine whether a callback to
	 * umem_lock_undo was registered via as_add_callback.
	 * (i.e. umem_lockmemory was called with DDI_MEMLOCK_LONGTERM and
	 * a valid callback function structure.)  as_delete_callback
	 * is called to delete a possible registered callback.  If the
	 * return from as_delete_callbacks is AS_CALLBACK_DELETED, it
	 * indicates that there was a callback registered, and that is was
	 * successfully deleted.  Thus, the cookie reference count
	 * will never be decremented by umem_lock_undo.  Just return the
	 * memory for the cookie, since both users of the cookie are done.
	 * A return of AS_CALLBACK_NOTFOUND indicates a callback was
	 * never registered.  A return of AS_CALLBACK_DELETE_DEFERRED
	 * indicates that callback processing is taking place and, and
	 * umem_lock_undo is, or will be, executing, and thus decrementing
	 * the cookie reference count when it is complete.
	 *
	 * This needs to be done before as_pageunlock so that the
	 * persistence of as is guaranteed because of the locked pages.
	 *
	 */
	rc = as_delete_callback(p->asp, p);


	/*
	 * The proc->p_as will be stale if i_ddi_umem_unlock is called
	 * after relvm is called so use p->asp.
	 */
	as_pageunlock(p->asp, p->pparray, p->cvaddr, p->size, p->s_flags);

	/*
	 * Now that we have unlocked the memory decrement the
	 * *.max-locked-memory rctl
	 */
	umem_decr_devlockmem(p);

	if (rc == AS_CALLBACK_DELETED) {
		/* umem_lock_undo will not happen, return the cookie memory */
		ASSERT(p->cook_refcnt == 2);
		kmem_free(p, sizeof (struct ddi_umem_cookie));
	} else {
		/*
		 * umem_undo_lock may happen if as_delete_callback returned
		 * AS_CALLBACK_DELETE_DEFERRED.  In that case, decrement the
		 * reference count, atomically, and return the cookie
		 * memory if the reference count goes to zero.  The only
		 * other value for rc is AS_CALLBACK_NOTFOUND.  In that
		 * case, just return the cookie memory.
		 */
		if ((rc != AS_CALLBACK_DELETE_DEFERRED) ||
		    (atomic_dec_ulong_nv((ulong_t *)(&(p->cook_refcnt)))
		    == 0)) {
			kmem_free(p, sizeof (struct ddi_umem_cookie));
		}
	}
}

/*
 * i_ddi_umem_unlock_thread - deferred ddi_umem_unlock list handler.
 *
 * Call i_ddi_umem_unlock for entries in the ddi_umem_unlock list
 * until it is empty.  Then, wait for more to be added.  This thread is awoken
 * via calls to ddi_umem_unlock.
 */

static void
i_ddi_umem_unlock_thread(void)
{
	struct ddi_umem_cookie	*ret_cookie;
	callb_cpr_t	cprinfo;

	/* process the ddi_umem_unlock list */
	CALLB_CPR_INIT(&cprinfo, &ddi_umem_unlock_mutex,
	    callb_generic_cpr, "unlock_thread");
	for (;;) {
		mutex_enter(&ddi_umem_unlock_mutex);
		if (ddi_umem_unlock_head != NULL) {	/* list not empty */
			ret_cookie = ddi_umem_unlock_head;
			/* take if off the list */
			if ((ddi_umem_unlock_head =
			    ddi_umem_unlock_head->unl_forw) == NULL) {
				ddi_umem_unlock_tail = NULL;
			}
			mutex_exit(&ddi_umem_unlock_mutex);
			/* unlock the pages in this cookie */
			(void) i_ddi_umem_unlock(ret_cookie);
		} else {   /* list is empty, wait for next ddi_umem_unlock */
			CALLB_CPR_SAFE_BEGIN(&cprinfo);
			cv_wait(&ddi_umem_unlock_cv, &ddi_umem_unlock_mutex);
			CALLB_CPR_SAFE_END(&cprinfo, &ddi_umem_unlock_mutex);
			mutex_exit(&ddi_umem_unlock_mutex);
		}
	}
	/* ddi_umem_unlock_thread does not exit */
	/* NOTREACHED */
}

/*
 * Start the thread that will process the ddi_umem_unlock list if it is
 * not already started (i_ddi_umem_unlock_thread).
 */
static void
i_ddi_umem_unlock_thread_start(void)
{
	mutex_enter(&ddi_umem_unlock_mutex);
	if (ddi_umem_unlock_thread == NULL) {
		ddi_umem_unlock_thread = thread_create(NULL, 0,
		    i_ddi_umem_unlock_thread, NULL, 0, &p0,
		    TS_RUN, minclsyspri);
	}
	mutex_exit(&ddi_umem_unlock_mutex);
}

/*
 * Lock the virtual address range in the current process and create a
 * ddi_umem_cookie (of type UMEM_LOCKED). This can be used to pass to
 * ddi_umem_iosetup to create a buf or do devmap_umem_setup/remap to export
 * to user space.
 *
 * Note: The resource control accounting currently uses a full charge model
 * in other words attempts to lock the same/overlapping areas of memory
 * will deduct the full size of the buffer from the projects running
 * counter for the device locked memory. This applies to umem_lockmemory too.
 *
 * addr, size should be PAGESIZE aligned
 * flags - DDI_UMEMLOCK_READ, DDI_UMEMLOCK_WRITE or both
 *	identifies whether the locked memory will be read or written or both
 *
 * Returns 0 on success
 *	EINVAL - for invalid parameters
 *	EPERM, ENOMEM and other error codes returned by as_pagelock
 *	ENOMEM - is returned if the current request to lock memory exceeds
 *		*.max-locked-memory resource control value.
 *	EAGAIN - could not start the ddi_umem_unlock list processing thread
 */
int
ddi_umem_lock(caddr_t addr, size_t len, int flags, ddi_umem_cookie_t *cookie)
{
	int	error;
	struct ddi_umem_cookie *p;

	*cookie = NULL;		/* in case of any error return */

	/* These are the only two valid flags */
	if ((flags & ~(DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) != 0) {
		return (EINVAL);
	}

	/* At least one of the two flags (or both) must be set */
	if ((flags & (DDI_UMEMLOCK_READ | DDI_UMEMLOCK_WRITE)) == 0) {
		return (EINVAL);
	}

	/* addr and len must be page-aligned */
	if (((uintptr_t)addr & PAGEOFFSET) != 0) {
		return (EINVAL);
	}

	if ((len & PAGEOFFSET) != 0) {
		return (EINVAL);
	}

	/*
	 * Call i_ddi_umem_unlock_thread_start if necessary.  It will
	 * be called on first ddi_umem_lock or umem_lockmemory call.
	 */
	if (ddi_umem_unlock_thread == NULL)
		i_ddi_umem_unlock_thread_start();

	/* Allocate memory for the cookie */
	p = kmem_zalloc(sizeof (struct ddi_umem_cookie), KM_SLEEP);

	/* Convert the flags to seg_rw type */
	if (flags & DDI_UMEMLOCK_WRITE) {
		p->s_flags = S_WRITE;
	} else {
		p->s_flags = S_READ;
	}

	/* Store curproc in cookie for later iosetup/unlock */
	p->procp = (void *)curproc;

	/*
	 * Store the struct as pointer in cookie for later use by
	 * ddi_umem_unlock.  The proc->p_as will be stale if ddi_umem_unlock
	 * is called after relvm is called.
	 */
	p->asp = curproc->p_as;
	/*
	 * The size field is needed for lockmem accounting.
	 */
	p->size = len;
	init_lockedmem_rctl_flag(p);

	if (umem_incr_devlockmem(p) != 0) {
		/*
		 * The requested memory cannot be locked
		 */
		kmem_free(p, sizeof (struct ddi_umem_cookie));
		*cookie = (ddi_umem_cookie_t)NULL;
		return (ENOMEM);
	}

	/* Lock the pages corresponding to addr, len in memory */
	error = as_pagelock(((proc_t *)p->procp)->p_as, &(p->pparray),
	    addr, len, p->s_flags);
	if (error != 0) {
		umem_decr_devlockmem(p);
		kmem_free(p, sizeof (struct ddi_umem_cookie));
		*cookie = (ddi_umem_cookie_t)NULL;
		return (error);
	}

	/* Initialize the fields in the ddi_umem_cookie */
	p->cvaddr = addr;
	p->type = UMEM_LOCKED;
	p->cook_refcnt = 1;

	*cookie = (ddi_umem_cookie_t)p;
	return (error);
}

/*
 * Add the cookie to the ddi_umem_unlock list.  Pages will be
 * unlocked by i_ddi_umem_unlock_thread.
 */

void
ddi_umem_unlock(ddi_umem_cookie_t cookie)
{
	struct ddi_umem_cookie	*p = (struct ddi_umem_cookie *)cookie;

	ASSERT(p->type == UMEM_LOCKED);
	ASSERT(CPU_ON_INTR(CPU) == 0); /* cannot be high level */
	ASSERT(ddi_umem_unlock_thread != NULL);

	p->unl_forw = (struct ddi_umem_cookie *)NULL;	/* end of list */
	/*
	 * Queue the unlock request and notify i_ddi_umem_unlock thread
	 * if it's called in the interrupt context. Otherwise, unlock pages
	 * immediately.
	 */
	if (servicing_interrupt()) {
		/* queue the unlock request and notify the thread */
		mutex_enter(&ddi_umem_unlock_mutex);
		if (ddi_umem_unlock_head == NULL) {
			ddi_umem_unlock_head = ddi_umem_unlock_tail = p;
			cv_broadcast(&ddi_umem_unlock_cv);
		} else {
			ddi_umem_unlock_tail->unl_forw = p;
			ddi_umem_unlock_tail = p;
		}
		mutex_exit(&ddi_umem_unlock_mutex);
	} else {
		/* unlock the pages right away */
		(void) i_ddi_umem_unlock(p);
	}
}

/*
 * Create a buf structure from a ddi_umem_cookie
 * cookie - is a ddi_umem_cookie for from ddi_umem_lock and ddi_umem_alloc
 *		(only UMEM_LOCKED & KMEM_NON_PAGEABLE types supported)
 * off, len - identifies the portion of the memory represented by the cookie
 *		that the buf points to.
 *	NOTE: off, len need to follow the alignment/size restrictions of the
 *		device (dev) that this buf will be passed to. Some devices
 *		will accept unrestricted alignment/size, whereas others (such as
 *		st) require some block-size alignment/size. It is the caller's
 *		responsibility to ensure that the alignment/size restrictions
 *		are met (we cannot assert as we do not know the restrictions)
 *
 * direction - is one of B_READ or B_WRITE and needs to be compatible with
 *		the flags used in ddi_umem_lock
 *
 * The following three arguments are used to initialize fields in the
 * buf structure and are uninterpreted by this routine.
 *
 * dev
 * blkno
 * iodone
 *
 * sleepflag - is one of DDI_UMEM_SLEEP or DDI_UMEM_NOSLEEP
 *
 * Returns a buf structure pointer on success (to be freed by freerbuf)
 *	NULL on any parameter error or memory alloc failure
 *
 */
struct buf *
ddi_umem_iosetup(ddi_umem_cookie_t cookie, off_t off, size_t len,
    int direction, dev_t dev, daddr_t blkno,
    int (*iodone)(struct buf *), int sleepflag)
{
	struct ddi_umem_cookie *p = (struct ddi_umem_cookie *)cookie;
	struct buf *bp;

	/*
	 * check for valid cookie offset, len
	 */
	if ((off + len) > p->size) {
		return (NULL);
	}

	if (len > p->size) {
		return (NULL);
	}

	/* direction has to be one of B_READ or B_WRITE */
	if ((direction != B_READ) && (direction != B_WRITE)) {
		return (NULL);
	}

	/* These are the only two valid sleepflags */
	if ((sleepflag != DDI_UMEM_SLEEP) && (sleepflag != DDI_UMEM_NOSLEEP)) {
		return (NULL);
	}

	/*
	 * Only cookies of type UMEM_LOCKED and KMEM_NON_PAGEABLE are supported
	 */
	if ((p->type != UMEM_LOCKED) && (p->type != KMEM_NON_PAGEABLE)) {
		return (NULL);
	}

	/* If type is KMEM_NON_PAGEABLE procp is NULL */
	ASSERT((p->type == KMEM_NON_PAGEABLE) ?
	    (p->procp == NULL) : (p->procp != NULL));

	bp = kmem_alloc(sizeof (struct buf), sleepflag);
	if (bp == NULL) {
		return (NULL);
	}
	bioinit(bp);

	bp->b_flags = B_BUSY | B_PHYS | direction;
	bp->b_edev = dev;
	bp->b_lblkno = blkno;
	bp->b_iodone = iodone;
	bp->b_bcount = len;
	bp->b_proc = (proc_t *)p->procp;
	ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
	bp->b_un.b_addr = (caddr_t)((uintptr_t)(p->cvaddr) + off);
	if (p->pparray != NULL) {
		bp->b_flags |= B_SHADOW;
		ASSERT(((uintptr_t)(p->cvaddr) & PAGEOFFSET) == 0);
		bp->b_shadow = p->pparray + btop(off);
	}
	return (bp);
}

/*
 * Fault-handling and related routines
 */

ddi_devstate_t
ddi_get_devstate(dev_info_t *dip)
{
	if (DEVI_IS_DEVICE_OFFLINE(dip))
		return (DDI_DEVSTATE_OFFLINE);
	else if (DEVI_IS_DEVICE_DOWN(dip) || DEVI_IS_BUS_DOWN(dip))
		return (DDI_DEVSTATE_DOWN);
	else if (DEVI_IS_BUS_QUIESCED(dip))
		return (DDI_DEVSTATE_QUIESCED);
	else if (DEVI_IS_DEVICE_DEGRADED(dip))
		return (DDI_DEVSTATE_DEGRADED);
	else
		return (DDI_DEVSTATE_UP);
}

void
ddi_dev_report_fault(dev_info_t *dip, ddi_fault_impact_t impact,
    ddi_fault_location_t location, const char *message)
{
	struct ddi_fault_event_data fd;
	ddi_eventcookie_t ec;

	/*
	 * Assemble all the information into a fault-event-data structure
	 */
	fd.f_dip = dip;
	fd.f_impact = impact;
	fd.f_location = location;
	fd.f_message = message;
	fd.f_oldstate = ddi_get_devstate(dip);

	/*
	 * Get eventcookie from defining parent.
	 */
	if (ddi_get_eventcookie(dip, DDI_DEVI_FAULT_EVENT, &ec) !=
	    DDI_SUCCESS)
		return;

	(void) ndi_post_event(dip, dip, ec, &fd);
}

char *
i_ddi_devi_class(dev_info_t *dip)
{
	return (DEVI(dip)->devi_device_class);
}

int
i_ddi_set_devi_class(dev_info_t *dip, const char *devi_class, int flag)
{
	struct dev_info *devi = DEVI(dip);

	mutex_enter(&devi->devi_lock);

	if (devi->devi_device_class)
		kmem_free(devi->devi_device_class,
		    strlen(devi->devi_device_class) + 1);

	if ((devi->devi_device_class = i_ddi_strdup(devi_class, flag))
	    != NULL) {
		mutex_exit(&devi->devi_lock);
		return (DDI_SUCCESS);
	}

	mutex_exit(&devi->devi_lock);

	return (DDI_FAILURE);
}


/*
 * Task Queues DDI interfaces.
 */

/* ARGSUSED */
ddi_taskq_t *
ddi_taskq_create(dev_info_t *dip, const char *name, int nthreads,
    pri_t pri, uint_t cflags)
{
	char full_name[TASKQ_NAMELEN];
	const char *tq_name;
	int nodeid = 0;

	if (dip == NULL)
		tq_name = name;
	else {
		nodeid = ddi_get_instance(dip);

		if (name == NULL)
			name = "tq";

		(void) snprintf(full_name, sizeof (full_name), "%s_%s",
		    ddi_driver_name(dip), name);

		tq_name = full_name;
	}

	return ((ddi_taskq_t *)taskq_create_instance(tq_name, nodeid, nthreads,
	    pri == TASKQ_DEFAULTPRI ? minclsyspri : pri,
	    nthreads, INT_MAX, TASKQ_PREPOPULATE));
}

void
ddi_taskq_destroy(ddi_taskq_t *tq)
{
	taskq_destroy((taskq_t *)tq);
}

int
ddi_taskq_dispatch(ddi_taskq_t *tq, void (* func)(void *),
    void *arg, uint_t dflags)
{
	taskqid_t id = taskq_dispatch((taskq_t *)tq, func, arg,
	    dflags == DDI_SLEEP ? TQ_SLEEP : TQ_NOSLEEP);

	return (id != TASKQID_INVALID ? DDI_SUCCESS : DDI_FAILURE);
}

void
ddi_taskq_wait(ddi_taskq_t *tq)
{
	taskq_wait((taskq_t *)tq);
}

void
ddi_taskq_suspend(ddi_taskq_t *tq)
{
	taskq_suspend((taskq_t *)tq);
}

boolean_t
ddi_taskq_suspended(ddi_taskq_t *tq)
{
	return (taskq_suspended((taskq_t *)tq));
}

void
ddi_taskq_resume(ddi_taskq_t *tq)
{
	taskq_resume((taskq_t *)tq);
}

int
ddi_parse(
	const char	*ifname,
	char		*alnum,
	uint_t		*nump)
{
	const char	*p;
	int		l;
	ulong_t		num;
	boolean_t	nonum = B_TRUE;
	char		c;

	l = strlen(ifname);
	for (p = ifname + l; p != ifname; l--) {
		c = *--p;
		if (!isdigit(c)) {
			(void) strlcpy(alnum, ifname, l + 1);
			if (ddi_strtoul(p + 1, NULL, 10, &num) != 0)
				return (DDI_FAILURE);
			break;
		}
		nonum = B_FALSE;
	}
	if (l == 0 || nonum)
		return (DDI_FAILURE);

	*nump = num;
	return (DDI_SUCCESS);
}

/*
 * Default initialization function for drivers that don't need to quiesce.
 */
/* ARGSUSED */
int
ddi_quiesce_not_needed(dev_info_t *dip)
{
	return (DDI_SUCCESS);
}

/*
 * Initialization function for drivers that should implement quiesce()
 * but haven't yet.
 */
/* ARGSUSED */
int
ddi_quiesce_not_supported(dev_info_t *dip)
{
	return (DDI_FAILURE);
}

char *
ddi_strdup(const char *str, int flag)
{
	int	n;
	char	*ptr;

	ASSERT(str != NULL);
	ASSERT((flag == KM_SLEEP) || (flag == KM_NOSLEEP));

	n = strlen(str);
	if ((ptr = kmem_alloc(n + 1, flag)) == NULL)
		return (NULL);
	bcopy(str, ptr, n + 1);
	return (ptr);
}

char *
strdup(const char *str)
{
	return (ddi_strdup(str, KM_SLEEP));
}

void
strfree(char *str)
{
	ASSERT(str != NULL);
	kmem_free(str, strlen(str) + 1);
}

/*
 * Generic DDI callback interfaces.
 */

int
ddi_cb_register(dev_info_t *dip, ddi_cb_flags_t flags, ddi_cb_func_t cbfunc,
    void *arg1, void *arg2, ddi_cb_handle_t *ret_hdlp)
{
	ddi_cb_t	*cbp;

	ASSERT(dip != NULL);
	ASSERT(DDI_CB_FLAG_VALID(flags));
	ASSERT(cbfunc != NULL);
	ASSERT(ret_hdlp != NULL);

	/* Sanity check the context */
	ASSERT(!servicing_interrupt());
	if (servicing_interrupt())
		return (DDI_FAILURE);

	/* Validate parameters */
	if ((dip == NULL) || !DDI_CB_FLAG_VALID(flags) ||
	    (cbfunc == NULL) || (ret_hdlp == NULL))
		return (DDI_EINVAL);

	/* Check for previous registration */
	if (DEVI(dip)->devi_cb_p != NULL)
		return (DDI_EALREADY);

	/* Allocate and initialize callback */
	cbp = kmem_zalloc(sizeof (ddi_cb_t), KM_SLEEP);
	cbp->cb_dip = dip;
	cbp->cb_func = cbfunc;
	cbp->cb_arg1 = arg1;
	cbp->cb_arg2 = arg2;
	cbp->cb_flags = flags;
	DEVI(dip)->devi_cb_p = cbp;

	/* If adding an IRM callback, notify IRM */
	if (flags & DDI_CB_FLAG_INTR)
		i_ddi_irm_set_cb(dip, B_TRUE);

	*ret_hdlp = (ddi_cb_handle_t)&(DEVI(dip)->devi_cb_p);
	return (DDI_SUCCESS);
}

int
ddi_cb_unregister(ddi_cb_handle_t hdl)
{
	ddi_cb_t	*cbp;
	dev_info_t	*dip;

	ASSERT(hdl != NULL);

	/* Sanity check the context */
	ASSERT(!servicing_interrupt());
	if (servicing_interrupt())
		return (DDI_FAILURE);

	/* Validate parameters */
	if ((hdl == NULL) || ((cbp = *(ddi_cb_t **)hdl) == NULL) ||
	    ((dip = cbp->cb_dip) == NULL))
		return (DDI_EINVAL);

	/* If removing an IRM callback, notify IRM */
	if (cbp->cb_flags & DDI_CB_FLAG_INTR)
		i_ddi_irm_set_cb(dip, B_FALSE);

	/* Destroy the callback */
	kmem_free(cbp, sizeof (ddi_cb_t));
	DEVI(dip)->devi_cb_p = NULL;

	return (DDI_SUCCESS);
}

/*
 * Platform independent DR routines
 */

static int
ndi2errno(int n)
{
	int err = 0;

	switch (n) {
		case NDI_NOMEM:
			err = ENOMEM;
			break;
		case NDI_BUSY:
			err = EBUSY;
			break;
		case NDI_FAULT:
			err = EFAULT;
			break;
		case NDI_FAILURE:
			err = EIO;
			break;
		case NDI_SUCCESS:
			break;
		case NDI_BADHANDLE:
		default:
			err = EINVAL;
			break;
	}
	return (err);
}

/*
 * Prom tree node list
 */
struct ptnode {
	pnode_t		nodeid;
	struct ptnode	*next;
};

/*
 * Prom tree walk arg
 */
struct pta {
	dev_info_t	*pdip;
	devi_branch_t	*bp;
	uint_t		flags;
	dev_info_t	*fdip;
	struct ptnode	*head;
};

static void
visit_node(pnode_t nodeid, struct pta *ap)
{
	struct ptnode	**nextp;
	int		(*select)(pnode_t, void *, uint_t);

	ASSERT(nodeid != OBP_NONODE && nodeid != OBP_BADNODE);

	select = ap->bp->create.prom_branch_select;

	ASSERT(select);

	if (select(nodeid, ap->bp->arg, 0) == DDI_SUCCESS) {

		for (nextp = &ap->head; *nextp; nextp = &(*nextp)->next)
			;

		*nextp = kmem_zalloc(sizeof (struct ptnode), KM_SLEEP);

		(*nextp)->nodeid = nodeid;
	}

	if ((ap->flags & DEVI_BRANCH_CHILD) == DEVI_BRANCH_CHILD)
		return;

	nodeid = prom_childnode(nodeid);
	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
		visit_node(nodeid, ap);
		nodeid = prom_nextnode(nodeid);
	}
}

/*
 * NOTE: The caller of this function must check for device contracts
 * or LDI callbacks against this dip before setting the dip offline.
 */
static int
set_infant_dip_offline(dev_info_t *dip, void *arg)
{
	char	*path = (char *)arg;

	ASSERT(dip);
	ASSERT(arg);

	if (i_ddi_node_state(dip) >= DS_ATTACHED) {
		(void) ddi_pathname(dip, path);
		cmn_err(CE_WARN, "Attempt to set offline flag on attached "
		    "node: %s", path);
		return (DDI_FAILURE);
	}

	mutex_enter(&(DEVI(dip)->devi_lock));
	if (!DEVI_IS_DEVICE_OFFLINE(dip))
		DEVI_SET_DEVICE_OFFLINE(dip);
	mutex_exit(&(DEVI(dip)->devi_lock));

	return (DDI_SUCCESS);
}

typedef struct result {
	char	*path;
	int	result;
} result_t;

static int
dip_set_offline(dev_info_t *dip, void *arg)
{
	int end;
	result_t *resp = (result_t *)arg;

	ASSERT(dip);
	ASSERT(resp);

	/*
	 * We stop the walk if e_ddi_offline_notify() returns
	 * failure, because this implies that one or more consumers
	 * (either LDI or contract based) has blocked the offline.
	 * So there is no point in conitnuing the walk
	 */
	if (e_ddi_offline_notify(dip) == DDI_FAILURE) {
		resp->result = DDI_FAILURE;
		return (DDI_WALK_TERMINATE);
	}

	/*
	 * If set_infant_dip_offline() returns failure, it implies
	 * that we failed to set a particular dip offline. This
	 * does not imply that the offline as a whole should fail.
	 * We want to do the best we can, so we continue the walk.
	 */
	if (set_infant_dip_offline(dip, resp->path) == DDI_SUCCESS)
		end = DDI_SUCCESS;
	else
		end = DDI_FAILURE;

	e_ddi_offline_finalize(dip, end);

	return (DDI_WALK_CONTINUE);
}

/*
 * The call to e_ddi_offline_notify() exists for the
 * unlikely error case that a branch we are trying to
 * create already exists and has device contracts or LDI
 * event callbacks against it.
 *
 * We allow create to succeed for such branches only if
 * no constraints block the offline.
 */
static int
branch_set_offline(dev_info_t *dip, char *path)
{
	int		circ;
	int		end;
	result_t	res;


	if (e_ddi_offline_notify(dip) == DDI_FAILURE) {
		return (DDI_FAILURE);
	}

	if (set_infant_dip_offline(dip, path) == DDI_SUCCESS)
		end = DDI_SUCCESS;
	else
		end = DDI_FAILURE;

	e_ddi_offline_finalize(dip, end);

	if (end == DDI_FAILURE)
		return (DDI_FAILURE);

	res.result = DDI_SUCCESS;
	res.path = path;

	ndi_devi_enter(dip, &circ);
	ddi_walk_devs(ddi_get_child(dip), dip_set_offline, &res);
	ndi_devi_exit(dip, circ);

	return (res.result);
}

/*ARGSUSED*/
static int
create_prom_branch(void *arg, int has_changed)
{
	int		circ;
	int		exists, rv;
	pnode_t		nodeid;
	struct ptnode	*tnp;
	dev_info_t	*dip;
	struct pta	*ap = arg;
	devi_branch_t	*bp;
	char		*path;

	ASSERT(ap);
	ASSERT(ap->fdip == NULL);
	ASSERT(ap->pdip && ndi_dev_is_prom_node(ap->pdip));

	bp = ap->bp;

	nodeid = ddi_get_nodeid(ap->pdip);
	if (nodeid == OBP_NONODE || nodeid == OBP_BADNODE) {
		cmn_err(CE_WARN, "create_prom_branch: invalid "
		    "nodeid: 0x%x", nodeid);
		return (EINVAL);
	}

	ap->head = NULL;

	nodeid = prom_childnode(nodeid);
	while (nodeid != OBP_NONODE && nodeid != OBP_BADNODE) {
		visit_node(nodeid, ap);
		nodeid = prom_nextnode(nodeid);
	}

	if (ap->head == NULL)
		return (ENODEV);

	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
	rv = 0;
	while ((tnp = ap->head) != NULL) {
		ap->head = tnp->next;

		ndi_devi_enter(ap->pdip, &circ);

		/*
		 * Check if the branch already exists.
		 */
		exists = 0;
		dip = e_ddi_nodeid_to_dip(tnp->nodeid);
		if (dip != NULL) {
			exists = 1;

			/* Parent is held busy, so release hold */
			ndi_rele_devi(dip);
#ifdef	DEBUG
			cmn_err(CE_WARN, "create_prom_branch: dip(%p) exists"
			    " for nodeid 0x%x", (void *)dip, tnp->nodeid);
#endif
		} else {
			dip = i_ddi_create_branch(ap->pdip, tnp->nodeid);
		}

		kmem_free(tnp, sizeof (struct ptnode));

		/*
		 * Hold the branch if it is not already held
		 */
		if (dip && !exists) {
			e_ddi_branch_hold(dip);
		}

		ASSERT(dip == NULL || e_ddi_branch_held(dip));

		/*
		 * Set all dips in the newly created branch offline so that
		 * only a "configure" operation can attach
		 * the branch
		 */
		if (dip == NULL || branch_set_offline(dip, path)
		    == DDI_FAILURE) {
			ndi_devi_exit(ap->pdip, circ);
			rv = EIO;
			continue;
		}

		ASSERT(ddi_get_parent(dip) == ap->pdip);

		ndi_devi_exit(ap->pdip, circ);

		if (ap->flags & DEVI_BRANCH_CONFIGURE) {
			int error = e_ddi_branch_configure(dip, &ap->fdip, 0);
			if (error && rv == 0)
				rv = error;
		}

		/*
		 * Invoke devi_branch_callback() (if it exists) only for
		 * newly created branches
		 */
		if (bp->devi_branch_callback && !exists)
			bp->devi_branch_callback(dip, bp->arg, 0);
	}

	kmem_free(path, MAXPATHLEN);

	return (rv);
}

static int
sid_node_create(dev_info_t *pdip, devi_branch_t *bp, dev_info_t **rdipp)
{
	int			rv, circ, len;
	int			i, flags, ret;
	dev_info_t		*dip;
	char			*nbuf;
	char			*path;
	static const char	*noname = "<none>";

	ASSERT(pdip);
	ASSERT(DEVI_BUSY_OWNED(pdip));

	flags = 0;

	/*
	 * Creating the root of a branch ?
	 */
	if (rdipp) {
		*rdipp = NULL;
		flags = DEVI_BRANCH_ROOT;
	}

	ndi_devi_alloc_sleep(pdip, (char *)noname, DEVI_SID_NODEID, &dip);
	rv = bp->create.sid_branch_create(dip, bp->arg, flags);

	nbuf = kmem_alloc(OBP_MAXDRVNAME, KM_SLEEP);

	if (rv == DDI_WALK_ERROR) {
		cmn_err(CE_WARN, "e_ddi_branch_create: Error setting"
		    " properties on devinfo node %p",  (void *)dip);
		goto fail;
	}

	len = OBP_MAXDRVNAME;
	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "name", nbuf, &len)
	    != DDI_PROP_SUCCESS) {
		cmn_err(CE_WARN, "e_ddi_branch_create: devinfo node %p has"
		    "no name property", (void *)dip);
		goto fail;
	}

	ASSERT(i_ddi_node_state(dip) == DS_PROTO);
	if (ndi_devi_set_nodename(dip, nbuf, 0) != NDI_SUCCESS) {
		cmn_err(CE_WARN, "e_ddi_branch_create: cannot set name (%s)"
		    " for devinfo node %p", nbuf, (void *)dip);
		goto fail;
	}

	kmem_free(nbuf, OBP_MAXDRVNAME);

	/*
	 * Ignore bind failures just like boot does
	 */
	(void) ndi_devi_bind_driver(dip, 0);

	switch (rv) {
	case DDI_WALK_CONTINUE:
	case DDI_WALK_PRUNESIB:
		ndi_devi_enter(dip, &circ);

		i = DDI_WALK_CONTINUE;
		for (; i == DDI_WALK_CONTINUE; ) {
			i = sid_node_create(dip, bp, NULL);
		}

		ASSERT(i == DDI_WALK_ERROR || i == DDI_WALK_PRUNESIB);
		if (i == DDI_WALK_ERROR)
			rv = i;
		/*
		 * If PRUNESIB stop creating siblings
		 * of dip's child. Subsequent walk behavior
		 * is determined by rv returned by dip.
		 */

		ndi_devi_exit(dip, circ);
		break;
	case DDI_WALK_TERMINATE:
		/*
		 * Don't create children and ask our parent
		 * to not create siblings either.
		 */
		rv = DDI_WALK_PRUNESIB;
		break;
	case DDI_WALK_PRUNECHILD:
		/*
		 * Don't create children, but ask parent to continue
		 * with siblings.
		 */
		rv = DDI_WALK_CONTINUE;
		break;
	default:
		ASSERT(0);
		break;
	}

	if (rdipp)
		*rdipp = dip;

	/*
	 * Set device offline - only the "configure" op should cause an attach.
	 * Note that it is safe to set the dip offline without checking
	 * for either device contract or layered driver (LDI) based constraints
	 * since there cannot be any contracts or LDI opens of this device.
	 * This is because this node is a newly created dip with the parent busy
	 * held, so no other thread can come in and attach this dip. A dip that
	 * has never been attached cannot have contracts since by definition
	 * a device contract (an agreement between a process and a device minor
	 * node) can only be created against a device that has minor nodes
	 * i.e is attached. Similarly an LDI open will only succeed if the
	 * dip is attached. We assert below that the dip is not attached.
	 */
	ASSERT(i_ddi_node_state(dip) < DS_ATTACHED);
	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
	ret = set_infant_dip_offline(dip, path);
	ASSERT(ret == DDI_SUCCESS);
	kmem_free(path, MAXPATHLEN);

	return (rv);
fail:
	(void) ndi_devi_free(dip);
	kmem_free(nbuf, OBP_MAXDRVNAME);
	return (DDI_WALK_ERROR);
}

static int
create_sid_branch(
	dev_info_t	*pdip,
	devi_branch_t	*bp,
	dev_info_t	**dipp,
	uint_t		flags)
{
	int		rv = 0, state = DDI_WALK_CONTINUE;
	dev_info_t	*rdip;

	while (state == DDI_WALK_CONTINUE) {
		int	circ;

		ndi_devi_enter(pdip, &circ);

		state = sid_node_create(pdip, bp, &rdip);
		if (rdip == NULL) {
			ndi_devi_exit(pdip, circ);
			ASSERT(state == DDI_WALK_ERROR);
			break;
		}

		e_ddi_branch_hold(rdip);

		ndi_devi_exit(pdip, circ);

		if (flags & DEVI_BRANCH_CONFIGURE) {
			int error = e_ddi_branch_configure(rdip, dipp, 0);
			if (error && rv == 0)
				rv = error;
		}

		/*
		 * devi_branch_callback() is optional
		 */
		if (bp->devi_branch_callback)
			bp->devi_branch_callback(rdip, bp->arg, 0);
	}

	ASSERT(state == DDI_WALK_ERROR || state == DDI_WALK_PRUNESIB);

	return (state == DDI_WALK_ERROR ? EIO : rv);
}

int
e_ddi_branch_create(
	dev_info_t	*pdip,
	devi_branch_t	*bp,
	dev_info_t	**dipp,
	uint_t		flags)
{
	int prom_devi, sid_devi, error;

	if (pdip == NULL || bp == NULL || bp->type == 0)
		return (EINVAL);

	prom_devi = (bp->type == DEVI_BRANCH_PROM) ? 1 : 0;
	sid_devi = (bp->type == DEVI_BRANCH_SID) ? 1 : 0;

	if (prom_devi && bp->create.prom_branch_select == NULL)
		return (EINVAL);
	else if (sid_devi && bp->create.sid_branch_create == NULL)
		return (EINVAL);
	else if (!prom_devi && !sid_devi)
		return (EINVAL);

	if (flags & DEVI_BRANCH_EVENT)
		return (EINVAL);

	if (prom_devi) {
		struct pta pta = {0};

		pta.pdip = pdip;
		pta.bp = bp;
		pta.flags = flags;

		error = prom_tree_access(create_prom_branch, &pta, NULL);

		if (dipp)
			*dipp = pta.fdip;
		else if (pta.fdip)
			ndi_rele_devi(pta.fdip);
	} else {
		error = create_sid_branch(pdip, bp, dipp, flags);
	}

	return (error);
}

int
e_ddi_branch_configure(dev_info_t *rdip, dev_info_t **dipp, uint_t flags)
{
	int		rv;
	char		*devnm;
	dev_info_t	*pdip;

	if (dipp)
		*dipp = NULL;

	if (rdip == NULL || flags != 0 || (flags & DEVI_BRANCH_EVENT))
		return (EINVAL);

	pdip = ddi_get_parent(rdip);

	ndi_hold_devi(pdip);

	if (!e_ddi_branch_held(rdip)) {
		ndi_rele_devi(pdip);
		cmn_err(CE_WARN, "e_ddi_branch_configure: "
		    "dip(%p) not held", (void *)rdip);
		return (EINVAL);
	}

	if (i_ddi_node_state(rdip) < DS_INITIALIZED) {
		/*
		 * First attempt to bind a driver. If we fail, return
		 * success (On some platforms, dips for some device
		 * types (CPUs) may not have a driver)
		 */
		if (ndi_devi_bind_driver(rdip, 0) != NDI_SUCCESS) {
			ndi_rele_devi(pdip);
			return (0);
		}

		if (ddi_initchild(pdip, rdip) != DDI_SUCCESS) {
			rv = NDI_FAILURE;
			goto out;
		}
	}

	ASSERT(i_ddi_node_state(rdip) >= DS_INITIALIZED);

	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);

	(void) ddi_deviname(rdip, devnm);

	if ((rv = ndi_devi_config_one(pdip, devnm+1, &rdip,
	    NDI_DEVI_ONLINE | NDI_CONFIG)) == NDI_SUCCESS) {
		/* release hold from ndi_devi_config_one() */
		ndi_rele_devi(rdip);
	}

	kmem_free(devnm, MAXNAMELEN + 1);
out:
	if (rv != NDI_SUCCESS && dipp && rdip) {
		ndi_hold_devi(rdip);
		*dipp = rdip;
	}
	ndi_rele_devi(pdip);
	return (ndi2errno(rv));
}

void
e_ddi_branch_hold(dev_info_t *rdip)
{
	if (e_ddi_branch_held(rdip)) {
		cmn_err(CE_WARN, "e_ddi_branch_hold: branch already held");
		return;
	}

	mutex_enter(&DEVI(rdip)->devi_lock);
	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) == 0) {
		DEVI(rdip)->devi_flags |= DEVI_BRANCH_HELD;
		DEVI(rdip)->devi_ref++;
	}
	ASSERT(DEVI(rdip)->devi_ref > 0);
	mutex_exit(&DEVI(rdip)->devi_lock);
}

int
e_ddi_branch_held(dev_info_t *rdip)
{
	int rv = 0;

	mutex_enter(&DEVI(rdip)->devi_lock);
	if ((DEVI(rdip)->devi_flags & DEVI_BRANCH_HELD) &&
	    DEVI(rdip)->devi_ref > 0) {
		rv = 1;
	}
	mutex_exit(&DEVI(rdip)->devi_lock);

	return (rv);
}

void
e_ddi_branch_rele(dev_info_t *rdip)
{
	mutex_enter(&DEVI(rdip)->devi_lock);
	DEVI(rdip)->devi_flags &= ~DEVI_BRANCH_HELD;
	DEVI(rdip)->devi_ref--;
	mutex_exit(&DEVI(rdip)->devi_lock);
}

int
e_ddi_branch_unconfigure(
	dev_info_t *rdip,
	dev_info_t **dipp,
	uint_t flags)
{
	int	circ, rv;
	int	destroy;
	char	*devnm;
	uint_t	nflags;
	dev_info_t *pdip;

	if (dipp)
		*dipp = NULL;

	if (rdip == NULL)
		return (EINVAL);

	pdip = ddi_get_parent(rdip);

	ASSERT(pdip);

	/*
	 * Check if caller holds pdip busy - can cause deadlocks during
	 * devfs_clean()
	 */
	if (DEVI_BUSY_OWNED(pdip)) {
		cmn_err(CE_WARN, "e_ddi_branch_unconfigure: failed: parent"
		    " devinfo node(%p) is busy held", (void *)pdip);
		return (EINVAL);
	}

	destroy = (flags & DEVI_BRANCH_DESTROY) ? 1 : 0;

	devnm = kmem_alloc(MAXNAMELEN + 1, KM_SLEEP);

	ndi_devi_enter(pdip, &circ);
	(void) ddi_deviname(rdip, devnm);
	ndi_devi_exit(pdip, circ);

	/*
	 * ddi_deviname() returns a component name with / prepended.
	 */
	(void) devfs_clean(pdip, devnm + 1, DV_CLEAN_FORCE);

	ndi_devi_enter(pdip, &circ);

	/*
	 * Recreate device name as it may have changed state (init/uninit)
	 * when parent busy lock was dropped for devfs_clean()
	 */
	(void) ddi_deviname(rdip, devnm);

	if (!e_ddi_branch_held(rdip)) {
		kmem_free(devnm, MAXNAMELEN + 1);
		ndi_devi_exit(pdip, circ);
		cmn_err(CE_WARN, "e_ddi_%s_branch: dip(%p) not held",
		    destroy ? "destroy" : "unconfigure", (void *)rdip);
		return (EINVAL);
	}

	/*
	 * Release hold on the branch. This is ok since we are holding the
	 * parent busy. If rdip is not removed, we must do a hold on the
	 * branch before returning.
	 */
	e_ddi_branch_rele(rdip);

	nflags = NDI_DEVI_OFFLINE;
	if (destroy || (flags & DEVI_BRANCH_DESTROY)) {
		nflags |= NDI_DEVI_REMOVE;
		destroy = 1;
	} else {
		nflags |= NDI_UNCONFIG;		/* uninit but don't remove */
	}

	if (flags & DEVI_BRANCH_EVENT)
		nflags |= NDI_POST_EVENT;

	if (i_ddi_devi_attached(pdip) &&
	    (i_ddi_node_state(rdip) >= DS_INITIALIZED)) {
		rv = ndi_devi_unconfig_one(pdip, devnm+1, dipp, nflags);
	} else {
		rv = e_ddi_devi_unconfig(rdip, dipp, nflags);
		if (rv == NDI_SUCCESS) {
			ASSERT(!destroy || ddi_get_child(rdip) == NULL);
			rv = ndi_devi_offline(rdip, nflags);
		}
	}

	if (!destroy || rv != NDI_SUCCESS) {
		/* The dip still exists, so do a hold */
		e_ddi_branch_hold(rdip);
	}

	kmem_free(devnm, MAXNAMELEN + 1);
	ndi_devi_exit(pdip, circ);
	return (ndi2errno(rv));
}

int
e_ddi_branch_destroy(dev_info_t *rdip, dev_info_t **dipp, uint_t flag)
{
	return (e_ddi_branch_unconfigure(rdip, dipp,
	    flag|DEVI_BRANCH_DESTROY));
}

/*
 * Number of chains for hash table
 */
#define	NUMCHAINS	17

/*
 * Devinfo busy arg
 */
struct devi_busy {
	int dv_total;
	int s_total;
	mod_hash_t *dv_hash;
	mod_hash_t *s_hash;
	int (*callback)(dev_info_t *, void *, uint_t);
	void *arg;
};

static int
visit_dip(dev_info_t *dip, void *arg)
{
	uintptr_t sbusy, dvbusy, ref;
	struct devi_busy *bsp = arg;

	ASSERT(bsp->callback);

	/*
	 * A dip cannot be busy if its reference count is 0
	 */
	if ((ref = e_ddi_devi_holdcnt(dip)) == 0) {
		return (bsp->callback(dip, bsp->arg, 0));
	}

	if (mod_hash_find(bsp->dv_hash, dip, (mod_hash_val_t *)&dvbusy))
		dvbusy = 0;

	/*
	 * To catch device opens currently maintained on specfs common snodes.
	 */
	if (mod_hash_find(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
		sbusy = 0;

#ifdef	DEBUG
	if (ref < sbusy || ref < dvbusy) {
		cmn_err(CE_WARN, "dip(%p): sopen = %lu, dvopen = %lu "
		    "dip ref = %lu\n", (void *)dip, sbusy, dvbusy, ref);
	}
#endif

	dvbusy = (sbusy > dvbusy) ? sbusy : dvbusy;

	return (bsp->callback(dip, bsp->arg, dvbusy));
}

static int
visit_snode(struct snode *sp, void *arg)
{
	uintptr_t sbusy;
	dev_info_t *dip;
	int count;
	struct devi_busy *bsp = arg;

	ASSERT(sp);

	/*
	 * The stable lock is held. This prevents
	 * the snode and its associated dip from
	 * going away.
	 */
	dip = NULL;
	count = spec_devi_open_count(sp, &dip);

	if (count <= 0)
		return (DDI_WALK_CONTINUE);

	ASSERT(dip);

	if (mod_hash_remove(bsp->s_hash, dip, (mod_hash_val_t *)&sbusy))
		sbusy = count;
	else
		sbusy += count;

	if (mod_hash_insert(bsp->s_hash, dip, (mod_hash_val_t)sbusy)) {
		cmn_err(CE_WARN, "%s: s_hash insert failed: dip=0x%p, "
		    "sbusy = %lu", "e_ddi_branch_referenced",
		    (void *)dip, sbusy);
	}

	bsp->s_total += count;

	return (DDI_WALK_CONTINUE);
}

static void
visit_dvnode(struct dv_node *dv, void *arg)
{
	uintptr_t dvbusy;
	uint_t count;
	struct vnode *vp;
	struct devi_busy *bsp = arg;

	ASSERT(dv && dv->dv_devi);

	vp = DVTOV(dv);

	mutex_enter(&vp->v_lock);
	count = vp->v_count;
	mutex_exit(&vp->v_lock);

	if (!count)
		return;

	if (mod_hash_remove(bsp->dv_hash, dv->dv_devi,
	    (mod_hash_val_t *)&dvbusy))
		dvbusy = count;
	else
		dvbusy += count;

	if (mod_hash_insert(bsp->dv_hash, dv->dv_devi,
	    (mod_hash_val_t)dvbusy)) {
		cmn_err(CE_WARN, "%s: dv_hash insert failed: dip=0x%p, "
		    "dvbusy=%lu", "e_ddi_branch_referenced",
		    (void *)dv->dv_devi, dvbusy);
	}

	bsp->dv_total += count;
}

/*
 * Returns reference count on success or -1 on failure.
 */
int
e_ddi_branch_referenced(
	dev_info_t *rdip,
	int (*callback)(dev_info_t *dip, void *arg, uint_t ref),
	void *arg)
{
	int circ;
	char *path;
	dev_info_t *pdip;
	struct devi_busy bsa = {0};

	ASSERT(rdip);

	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);

	ndi_hold_devi(rdip);

	pdip = ddi_get_parent(rdip);

	ASSERT(pdip);

	/*
	 * Check if caller holds pdip busy - can cause deadlocks during
	 * devfs_walk()
	 */
	if (!e_ddi_branch_held(rdip) || DEVI_BUSY_OWNED(pdip)) {
		cmn_err(CE_WARN, "e_ddi_branch_referenced: failed: "
		    "devinfo branch(%p) not held or parent busy held",
		    (void *)rdip);
		ndi_rele_devi(rdip);
		kmem_free(path, MAXPATHLEN);
		return (-1);
	}

	ndi_devi_enter(pdip, &circ);
	(void) ddi_pathname(rdip, path);
	ndi_devi_exit(pdip, circ);

	bsa.dv_hash = mod_hash_create_ptrhash("dv_node busy hash", NUMCHAINS,
	    mod_hash_null_valdtor, sizeof (struct dev_info));

	bsa.s_hash = mod_hash_create_ptrhash("snode busy hash", NUMCHAINS,
	    mod_hash_null_valdtor, sizeof (struct snode));

	if (devfs_walk(path, visit_dvnode, &bsa)) {
		cmn_err(CE_WARN, "e_ddi_branch_referenced: "
		    "devfs walk failed for: %s", path);
		kmem_free(path, MAXPATHLEN);
		bsa.s_total = bsa.dv_total = -1;
		goto out;
	}

	kmem_free(path, MAXPATHLEN);

	/*
	 * Walk the snode table to detect device opens, which are currently
	 * maintained on specfs common snodes.
	 */
	spec_snode_walk(visit_snode, &bsa);

	if (callback == NULL)
		goto out;

	bsa.callback = callback;
	bsa.arg = arg;

	if (visit_dip(rdip, &bsa) == DDI_WALK_CONTINUE) {
		ndi_devi_enter(rdip, &circ);
		ddi_walk_devs(ddi_get_child(rdip), visit_dip, &bsa);
		ndi_devi_exit(rdip, circ);
	}

out:
	ndi_rele_devi(rdip);
	mod_hash_destroy_ptrhash(bsa.s_hash);
	mod_hash_destroy_ptrhash(bsa.dv_hash);
	return (bsa.s_total > bsa.dv_total ? bsa.s_total : bsa.dv_total);
}