summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/io/dktp/controller/ata/ata_common.c
blob: dcd0cbf8d3ba3a00a881a9acd8a941a6d4af976a (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
/*
 * 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 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 2018 RackTop Systems.
 */

#include <sys/types.h>
#include <sys/modctl.h>
#include <sys/debug.h>
#include <sys/promif.h>
#include <sys/pci.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/uio.h>
#include <sys/cred.h>
#include <sys/cpu.h>
#include "ata_common.h"
#include "ata_disk.h"
#include "atapi.h"
#include "ata_blacklist.h"
#include "sil3xxx.h"

/*
 * Solaris Entry Points.
 */

static	int	ata_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
static	int	ata_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
static	int	ata_bus_ctl(dev_info_t *d, dev_info_t *r, ddi_ctl_enum_t o,
			void *a, void *v);
static	uint_t	ata_intr(caddr_t arg);

/*
 * GHD Entry points
 */

static	int	ata_get_status(void *hba_handle, void *intr_status);
static	void	ata_process_intr(void *hba_handle, void *intr_status);
static	int	ata_hba_start(void *handle, gcmd_t *gcmdp);
static	void	ata_hba_complete(void *handle, gcmd_t *gcmdp, int do_callback);
static	int	ata_timeout_func(void *hba_handle, gcmd_t  *gcmdp,
			gtgt_t *gtgtp, gact_t  action, int calltype);

/*
 * Local Function Prototypes
 */
static int ata_prop_lookup_int(dev_t match_dev, dev_info_t *dip,
		    uint_t flags, char *name, int defvalue);
static	int	ata_ctlr_fsm(uchar_t fsm_func, ata_ctl_t *ata_ctlp,
			ata_drv_t *ata_drvp, ata_pkt_t *ata_pktp,
				int *DoneFlgp);
static	void	ata_destroy_controller(dev_info_t *dip);
static	int	ata_drive_type(uchar_t drvhd,
			ddi_acc_handle_t io_hdl1, caddr_t ioaddr1,
			ddi_acc_handle_t io_hdl2, caddr_t ioaddr2,
			struct ata_id *ata_id_bufp);
static	ata_ctl_t *ata_init_controller(dev_info_t *dip);
static	ata_drv_t *ata_init_drive(ata_ctl_t *ata_ctlp,
			uchar_t targ, uchar_t lun);
static	int	ata_init_drive_pcidma(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp,
			dev_info_t *tdip);
static	int	ata_flush_cache(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp);
static	void	ata_init_pciide(dev_info_t *dip, ata_ctl_t *ata_ctlp);
static	int	ata_reset_bus(ata_ctl_t *ata_ctlp);
static	int	ata_setup_ioaddr(dev_info_t *dip,
			ddi_acc_handle_t *iohandle1, caddr_t *ioaddr1p,
			ddi_acc_handle_t *iohandle2, caddr_t *ioaddr2p,
			ddi_acc_handle_t *bm_hdlp, caddr_t *bm_addrp);
static	int	ata_software_reset(ata_ctl_t *ata_ctlp);
static	int	ata_start_arq(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp,
			ata_pkt_t *ata_pktp);
static	int	ata_strncmp(char *p1, char *p2, int cnt);
static	void	ata_uninit_drive(ata_drv_t *ata_drvp);

static	int	ata_check_pciide_blacklist(dev_info_t *dip, uint_t flags);
static	int	ata_check_revert_to_defaults(ata_drv_t *ata_drvp);
static  void	ata_show_transfer_mode(ata_ctl_t *, ata_drv_t *);
static	int	ata_spec_init_controller(dev_info_t *dip);

static void	ata_init_pm(dev_info_t *);
static int	ata_suspend(dev_info_t *);
static int	ata_resume(dev_info_t *);
static int	ata_power(dev_info_t *, int, int);
static int	ata_change_power(dev_info_t *, uint8_t);
static int	ata_is_pci(dev_info_t *);
static void	ata_disable_DMA(ata_drv_t *ata_drvp);
static int	ata_check_dma_mode(ata_drv_t *ata_drvp);

/*
 * Local static data
 */
static	void	*ata_state;

static	tmr_t	ata_timer_conf; /* single timeout list for all instances */
static	int	ata_watchdog_usec = 100000; /* check timeouts every 100 ms */

int	ata_hba_start_watchdog = 1000;
int	ata_process_intr_watchdog = 1000;
int	ata_reset_bus_watchdog = 1000;


/*
 * Use local or framework power management
 */

#ifdef	ATA_USE_AUTOPM
#define	ATA_BUSY_COMPONENT(d, c)	((void)pm_busy_component(d, c))
#define	ATA_IDLE_COMPONENT(d, c)	((void)pm_idle_component(d, c))
#define	ATA_RAISE_POWER(d, c, l)	pm_raise_power(d, c, l)
#define	ATA_LOWER_POWER(d, c, l)	pm_lower_power(d, c, l)
#else
#define	ATA_BUSY_COMPONENT(d, c)
#define	ATA_IDLE_COMPONENT(d, c)
#define	ATA_RAISE_POWER(d, c, l)	ata_power(d, c, l)
#define	ATA_LOWER_POWER(d, c, l)	ata_power(d, c, l)
#endif
/*
 * number of seconds to wait during various operations
 */
int	ata_flush_delay = 5 * 1000000;
uint_t	ata_set_feature_wait = 4 * 1000000;
uint_t	ata_flush_cache_wait = 60 * 1000000;	/* may take a long time */

/*
 * Change this for SFF-8070i support. Currently SFF-8070i is
 * using a field in the IDENTIFY PACKET DEVICE response which
 * already seems to be in use by some vendor's drives. I suspect
 * SFF will either move their laslun field or provide a reliable
 * way to validate it.
 */
int	ata_enable_atapi_luns = FALSE;

/*
 * set this to disable all DMA requests
 */
int	ata_dma_disabled = FALSE;

/*
 * set this to TRUE to enable storing the IDENTIFY DEVICE result in the
 * "ata" or "atapi" property.
 */
int	ata_id_debug = FALSE;

/*
 * set this to TRUE to enable logging device-capability data
 */
int	ata_capability_data = FALSE;

/*
 * DMA selection message pointers
 */
char *ata_cntrl_DMA_sel_msg;
char *ata_dev_DMA_sel_msg;

/*
 * bus nexus operations
 */
static	struct bus_ops	 ata_bus_ops;
static	struct bus_ops	*scsa_bus_ops_p;

/* ARGSUSED */
static int
ata_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
{
	if (ddi_get_soft_state(ata_state, getminor(*devp)) == NULL)
		return (ENXIO);

	return (0);
}

/*
 * The purpose of this function is to pass the ioaddress of the controller
 * to the caller, specifically used for upgrade from pre-pciide
 * to pciide nodes
 */
/* ARGSUSED */
static int
ata_read(dev_t dev, struct uio *uio_p, cred_t *cred_p)
{
	ata_ctl_t *ata_ctlp;
	char	buf[18];
	long len;

	ata_ctlp = ddi_get_soft_state(ata_state, getminor(dev));

	if (ata_ctlp == NULL)
		return (ENXIO);

	(void) sprintf(buf, "%p\n", (void *) ata_ctlp->ac_ioaddr1);

	len = strlen(buf) - uio_p->uio_offset;
	len = min(uio_p->uio_resid,  len);
	if (len <= 0)
		return (0);

	return (uiomove((caddr_t)(buf + uio_p->uio_offset), len,
	    UIO_READ, uio_p));
}

int
ata_devo_reset(
	dev_info_t *dip,
	ddi_reset_cmd_t cmd)
{
	ata_ctl_t *ata_ctlp;
	ata_drv_t *ata_drvp;
	int	   instance;
	int	   i;
	int	   rc;
	int	   flush_okay;

	if (cmd != DDI_RESET_FORCE)
		return (0);

	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);

	if (!ata_ctlp)
		return (0);

	/*
	 * reset ATA drives and flush the write cache of any drives
	 */
	flush_okay = TRUE;
	for (i = 0; i < ATA_MAXTARG; i++) {
		if ((ata_drvp = CTL2DRV(ata_ctlp, i, 0)) == 0)
			continue;
		/* Don't revert to defaults for certain IBM drives */
		if ((ata_drvp->ad_flags & AD_DISK) != 0 &&
		    ((ata_drvp->ad_flags & AD_NORVRT) == 0)) {
			/* Enable revert to defaults when reset */
			(void) ata_set_feature(ata_ctlp, ata_drvp,
			    ATSF_ENA_REVPOD, 0);
		}

		/*
		 * skip flush cache if device type is cdrom
		 *
		 * notes: the structure definitions for ata_drvp->ad_id are
		 * defined for the ATA IDENTIFY_DEVICE, but if AD_ATAPI is set
		 * the struct holds data for the ATAPI IDENTIFY_PACKET_DEVICE
		 */
		if (!IS_CDROM(ata_drvp)) {

			/*
			 * Try the ATA/ATAPI flush write cache command
			 */
			rc = ata_flush_cache(ata_ctlp, ata_drvp);
			ADBG_WARN(("ata_flush_cache %s\n",
			    rc ? "okay" : "failed"));

			if (!rc)
				flush_okay = FALSE;
		}


		/*
		 * do something else if flush cache not supported
		 */
	}

	/*
	 * just busy wait if any drive doesn't support FLUSH CACHE
	 */
	if (!flush_okay)
		drv_usecwait(ata_flush_delay);
	return (0);
}

/*
 * quiesce(9E) entry point.
 *
 * This function is called when the system is single-threaded at high
 * PIL with preemption disabled. Therefore, this function must not be
 * blocked.
 *
 * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
 * DDI_FAILURE indicates an error condition and should almost never happen.
 */
int
ata_quiesce(dev_info_t *dip)
{
#ifdef ATA_DEBUG
	/*
	 * Turn off debugging
	 */
	ata_debug = 0;
#endif

	return (ata_devo_reset(dip, DDI_RESET_FORCE));
}


static struct cb_ops ata_cb_ops = {
	ata_open,		/* open */
	nulldev,		/* close */
	nodev,			/* strategy */
	nodev,			/* print */
	nodev,			/* dump */
	ata_read,		/* read */
	nodev,			/* write */
	nodev,			/* ioctl */
	nodev,			/* devmap */
	nodev,			/* mmap */
	nodev,			/* segmap */
	nochpoll,		/* chpoll */
	ddi_prop_op,		/* prop_op */
	NULL,			/* stream info */
	D_MP,			/* driver compatibility flag */
	CB_REV,			/* cb_ops revision */
	nodev,			/* aread */
	nodev			/* awrite */
};

static struct dev_ops	ata_ops = {
	DEVO_REV,		/* devo_rev, */
	0,			/* refcnt  */
	ddi_getinfo_1to1,	/* info */
	nulldev,		/* identify */
	NULL,			/* probe */
	ata_attach,		/* attach */
	ata_detach,		/* detach */
	ata_devo_reset,		/* reset */
	&ata_cb_ops,		/* driver operations */
	NULL,			/* bus operations */
	ata_power,		/* power */
	ata_quiesce		/* quiesce */
};

/* driver loadable module wrapper */
static struct modldrv modldrv = {
	&mod_driverops,		/* Type of module. This one is a driver */
	"ATA AT-bus attachment disk controller Driver",	/* module name */
	&ata_ops,					/* driver ops */
};

static struct modlinkage modlinkage = {
	MODREV_1, (void *)&modldrv, NULL
};

#ifdef ATA_DEBUG
int	ata_debug_init = FALSE;
int	ata_debug_attach = FALSE;

int	ata_debug = ADBG_FLAG_ERROR
		/* | ADBG_FLAG_ARQ */
		/* | ADBG_FLAG_INIT */
		/* | ADBG_FLAG_TRACE */
		/* | ADBG_FLAG_TRANSPORT */
		/* | ADBG_FLAG_WARN */
		;
#endif

int
_init(void)
{
	int err;

#ifdef ATA_DEBUG
	if (ata_debug_init)
		debug_enter("\nATA _INIT\n");
#endif

	if ((err = ddi_soft_state_init(&ata_state, sizeof (ata_ctl_t), 0)) != 0)
		return (err);

	if ((err = scsi_hba_init(&modlinkage)) != 0) {
		ddi_soft_state_fini(&ata_state);
		return (err);
	}

	/* save pointer to SCSA provided bus_ops struct */
	scsa_bus_ops_p = ata_ops.devo_bus_ops;

	/* make a copy of SCSA bus_ops */
	ata_bus_ops = *(ata_ops.devo_bus_ops);

	/*
	 * Modify our bus_ops to call our routines.  Our implementation
	 * will determine if the device is ATA or ATAPI/SCSA and react
	 * accordingly.
	 */
	ata_bus_ops.bus_ctl = ata_bus_ctl;

	/* patch our bus_ops into the dev_ops struct */
	ata_ops.devo_bus_ops = &ata_bus_ops;

	if ((err = mod_install(&modlinkage)) != 0) {
		scsi_hba_fini(&modlinkage);
		ddi_soft_state_fini(&ata_state);
	}

	/*
	 * Initialize the per driver timer info.
	 */

	ghd_timer_init(&ata_timer_conf, drv_usectohz(ata_watchdog_usec));

	return (err);
}

int
_fini(void)
{
	int err;

	if ((err = mod_remove(&modlinkage)) == 0) {
		ghd_timer_fini(&ata_timer_conf);
		scsi_hba_fini(&modlinkage);
		ddi_soft_state_fini(&ata_state);
	}

	return (err);
}

int
_info(struct modinfo *modinfop)
{
	return (mod_info(&modlinkage, modinfop));
}


/*
 *
 * driver attach entry point
 *
 */

static int
ata_attach(
	dev_info_t *dip,
	ddi_attach_cmd_t cmd)
{
	ata_ctl_t	*ata_ctlp;
	ata_drv_t	*ata_drvp;
	ata_drv_t	*first_drvp = NULL;
	uchar_t		 targ;
	uchar_t		 lun;
	uchar_t		 lastlun;
	int		 atapi_count = 0;
	int		 disk_count = 0;

	ADBG_TRACE(("ata_attach entered\n"));
#ifdef ATA_DEBUG
	if (ata_debug_attach)
		debug_enter("\nATA_ATTACH\n\n");
#endif

	switch (cmd) {
	case DDI_ATTACH:
		break;
	case DDI_RESUME:
		return (ata_resume(dip));
	default:
		return (DDI_FAILURE);
	}

	/* initialize controller */
	ata_ctlp = ata_init_controller(dip);

	if (ata_ctlp == NULL)
		goto errout;

	mutex_enter(&ata_ctlp->ac_ccc.ccc_hba_mutex);

	/* initialize drives */

	for (targ = 0; targ < ATA_MAXTARG; targ++) {

		ata_drvp = ata_init_drive(ata_ctlp, targ, 0);
		if (ata_drvp == NULL)
			continue;

		if (first_drvp == NULL)
			first_drvp = ata_drvp;

		if (ATAPIDRV(ata_drvp)) {
			atapi_count++;
			lastlun = ata_drvp->ad_id.ai_lastlun;
		} else {
			disk_count++;
			lastlun = 0;
		}

		/*
		 * LUN support is currently disabled. Check with SFF-8070i
		 * before enabling.
		 */
		if (!ata_enable_atapi_luns)
			lastlun = 0;

		/* Initialize higher LUNs, if there are any */
		for (lun = 1; lun <= lastlun && lun < ATA_MAXLUN; lun++) {
			if ((ata_drvp =
			    ata_init_drive(ata_ctlp, targ, lun)) != NULL) {
				ata_show_transfer_mode(ata_ctlp, ata_drvp);
			}
		}
	}

	if ((atapi_count == 0) && (disk_count == 0)) {
		ADBG_WARN(("ata_attach: no drives detected\n"));
		goto errout1;
	}

	/*
	 * Always make certain that a valid drive is selected so
	 * that routines which poll the status register don't get
	 * confused by non-existent drives.
	 */
	ddi_put8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_drvhd,
	    first_drvp->ad_drive_bits);
	ata_nsecwait(400);

	/*
	 * make certain the drive selected
	 */
	if (!ata_wait(ata_ctlp->ac_iohandle2, ata_ctlp->ac_ioaddr2,
	    0, ATS_BSY, 5000000)) {
		ADBG_ERROR(("ata_attach: select failed\n"));
	}

	/*
	 * initialize atapi/ata_dsk modules if we have at least
	 * one drive of that type.
	 */

	if (atapi_count) {
		if (!atapi_attach(ata_ctlp))
			goto errout1;
		ata_ctlp->ac_flags |= AC_ATAPI_INIT;
	}

	if (disk_count) {
		if (!ata_disk_attach(ata_ctlp))
			goto errout1;
		ata_ctlp->ac_flags |= AC_DISK_INIT;
	}

	/*
	 * make certain the interrupt and error latches are clear
	 */
	if (ata_ctlp->ac_pciide) {

		int instance = ddi_get_instance(dip);
		if (ddi_create_minor_node(dip, "control", S_IFCHR, instance,
		    DDI_PSEUDO, 0) != DDI_SUCCESS) {
			goto errout1;
		}

		(void) ata_pciide_status_clear(ata_ctlp);

	}

	/*
	 * enable the interrupt handler and drop the mutex
	 */
	ata_ctlp->ac_flags |= AC_ATTACHED;
	mutex_exit(&ata_ctlp->ac_ccc.ccc_hba_mutex);

	ata_init_pm(dip);

	ddi_report_dev(dip);
	return (DDI_SUCCESS);

errout1:
	mutex_exit(&ata_ctlp->ac_ccc.ccc_hba_mutex);
errout:
	(void) ata_detach(dip, DDI_DETACH);
	return (DDI_FAILURE);
}

/* driver detach entry point */

static int
ata_detach(
	dev_info_t *dip,
	ddi_detach_cmd_t cmd)
{
	ata_ctl_t *ata_ctlp;
	ata_drv_t *ata_drvp;
	int	   instance;
	int	   i;
	int	   j;

	ADBG_TRACE(("ata_detach entered\n"));

	switch (cmd) {
	case DDI_DETACH:
		break;
	case DDI_SUSPEND:
		return (ata_suspend(dip));
	default:
		return (DDI_FAILURE);
	}

	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);

	if (!ata_ctlp)
		return (DDI_SUCCESS);

	if (ata_ctlp->ac_pm_support) {
		ATA_BUSY_COMPONENT(dip, 0);
		if (ata_ctlp->ac_pm_level != PM_LEVEL_D0) {
			if (ATA_RAISE_POWER(dip, 0, PM_LEVEL_D0) !=
			    DDI_SUCCESS) {
				ATA_IDLE_COMPONENT(dip, 0);
				return (DDI_FAILURE);
			}
		}
		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components");
	}
	ata_ctlp->ac_flags &= ~AC_ATTACHED;

	/* destroy ata module */
	if (ata_ctlp->ac_flags & AC_DISK_INIT)
		ata_disk_detach(ata_ctlp);

	/* destroy atapi module */
	if (ata_ctlp->ac_flags & AC_ATAPI_INIT)
		atapi_detach(ata_ctlp);

	ddi_remove_minor_node(dip, NULL);

	/* destroy drives */
	for (i = 0; i < ATA_MAXTARG; i++) {
		for (j = 0; j < ATA_MAXLUN; j++) {
			ata_drvp = CTL2DRV(ata_ctlp, i, j);
			if (ata_drvp != NULL)
				ata_uninit_drive(ata_drvp);
		}
	}

	if (ata_ctlp->ac_iohandle1)
		ddi_regs_map_free(&ata_ctlp->ac_iohandle1);
	if (ata_ctlp->ac_iohandle2)
		ddi_regs_map_free(&ata_ctlp->ac_iohandle2);
	if (ata_ctlp->ac_bmhandle)
		ddi_regs_map_free(&ata_ctlp->ac_bmhandle);

	/* destroy controller */
	ata_destroy_controller(dip);

	ddi_prop_remove_all(dip);

	return (DDI_SUCCESS);
}

/*
 * Nexus driver bus_ctl entry point
 */
/*ARGSUSED*/
static int
ata_bus_ctl(
	dev_info_t *d,
	dev_info_t *r,
	ddi_ctl_enum_t o,
	void *a,
	void *v)
{
	dev_info_t *tdip;
	int	target_type;
	int	rc;
	char	*bufp;

	ADBG_TRACE(("ata_bus_ctl entered\n"));

	switch (o) {

	case DDI_CTLOPS_SIDDEV:
		return (DDI_FAILURE);

	case DDI_CTLOPS_IOMIN:

		/*
		 * Since we use PIO, we return a minimum I/O size of
		 * one byte.  This will need to be updated when we
		 * implement DMA support
		 */

		*((int *)v) = 1;
		return (DDI_SUCCESS);

	case DDI_CTLOPS_DMAPMAPC:
	case DDI_CTLOPS_REPORTINT:
	case DDI_CTLOPS_REGSIZE:
	case DDI_CTLOPS_NREGS:
	case DDI_CTLOPS_SLAVEONLY:
	case DDI_CTLOPS_AFFINITY:
	case DDI_CTLOPS_POKE:
	case DDI_CTLOPS_PEEK:

		/* These ops shouldn't be called by a target driver */
		ADBG_ERROR(("ata_bus_ctl: %s%d: invalid op (%d) from %s%d\n",
		    ddi_driver_name(d), ddi_get_instance(d), o,
		    ddi_driver_name(r), ddi_get_instance(r)));

		return (DDI_FAILURE);

	case DDI_CTLOPS_REPORTDEV:
	case DDI_CTLOPS_INITCHILD:
	case DDI_CTLOPS_UNINITCHILD:

		/* these require special handling below */
		break;

	default:
		return (ddi_ctlops(d, r, o, a, v));
	}

	/* get targets dip */

	if (o == DDI_CTLOPS_INITCHILD || o == DDI_CTLOPS_UNINITCHILD)
		tdip = (dev_info_t *)a;
	else
		tdip = r;

	/*
	 * XXX - Get class of target
	 *   Before the "class" entry in a conf file becomes
	 *   a real property, we use an additional property
	 *   tentatively called "class_prop".  We will require that
	 *   new classes (ie. direct) export "class_prop".
	 *   SCSA target drivers will not have this property, so
	 *   no property implies SCSA.
	 */
	if ((ddi_prop_lookup_string(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
	    "class", &bufp) == DDI_PROP_SUCCESS) ||
	    (ddi_prop_lookup_string(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
	    "class_prop", &bufp) == DDI_PROP_SUCCESS)) {
		if (strcmp(bufp, "dada") == 0)
			target_type = ATA_DEV_DISK;
		else if (strcmp(bufp, "scsi") == 0)
			target_type = ATA_DEV_ATAPI;
		else {
			ADBG_WARN(("ata_bus_ctl: invalid target class %s\n",
			    bufp));
			ddi_prop_free(bufp);
			return (DDI_FAILURE);
		}
		ddi_prop_free(bufp);
	} else {
		target_type = ATA_DEV_ATAPI; /* no class prop, assume SCSI */
	}

	if (o == DDI_CTLOPS_INITCHILD) {
		int	instance = ddi_get_instance(d);
		ata_ctl_t *ata_ctlp = ddi_get_soft_state(ata_state, instance);
		ata_drv_t *ata_drvp;
		int	targ;
		int	lun;
		int	drive_type;
		char	*disk_prop;
		char	*class_prop;

		if (ata_ctlp == NULL) {
			ADBG_WARN(("ata_bus_ctl: failed to find ctl struct\n"));
			return (DDI_FAILURE);
		}

		/* get (target,lun) of child device */

		targ = ddi_prop_get_int(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
		    "target", -1);
		if (targ == -1) {
			ADBG_WARN(("ata_bus_ctl: failed to get targ num\n"));
			return (DDI_FAILURE);
		}

		lun = ddi_prop_get_int(DDI_DEV_T_ANY, tdip, DDI_PROP_DONTPASS,
		    "lun", 0);

		if ((targ < 0) || (targ >= ATA_MAXTARG) ||
		    (lun < 0) || (lun >= ATA_MAXLUN)) {
			return (DDI_FAILURE);
		}

		ata_drvp = CTL2DRV(ata_ctlp, targ, lun);

		if (ata_drvp == NULL)
			return (DDI_FAILURE);	/* no drive */

		/* get type of device */

		if (ATAPIDRV(ata_drvp))
			drive_type = ATA_DEV_ATAPI;
		else
			drive_type = ATA_DEV_DISK;

		/*
		 * Check for special handling when child driver is
		 * cmdk (which morphs to the correct interface)
		 */
		if (strcmp(ddi_get_name(tdip), "cmdk") == 0) {

			if ((target_type == ATA_DEV_DISK) &&
			    (target_type != drive_type))
				return (DDI_FAILURE);

			target_type = drive_type;

			if (drive_type == ATA_DEV_ATAPI) {
				class_prop = "scsi";
			} else {
				disk_prop = "dadk";
				class_prop = "dada";

				if (ndi_prop_update_string(DDI_DEV_T_NONE, tdip,
				    "disk", disk_prop) != DDI_PROP_SUCCESS) {
					ADBG_WARN(("ata_bus_ctl: failed to "
					    "create disk prop\n"));
					return (DDI_FAILURE);
				}
			}

			if (ndi_prop_update_string(DDI_DEV_T_NONE, tdip,
			    "class_prop", class_prop) != DDI_PROP_SUCCESS) {
				ADBG_WARN(("ata_bus_ctl: failed to "
				    "create class prop\n"));
				return (DDI_FAILURE);
			}
		}

		/* Check that target class matches the device */

		if (target_type != drive_type)
			return (DDI_FAILURE);

		/* save pointer to drive struct for ata_disk_bus_ctl */
		ddi_set_driver_private(tdip, ata_drvp);

		/*
		 * Determine whether to enable DMA support for this drive.  This
		 * check is deferred to this point so that the various dma
		 * properties could reside on the devinfo node should finer
		 * grained dma control be required.
		 */
		if (ata_drvp->ad_pciide_dma == ATA_DMA_UNINITIALIZED) {
			ata_drvp->ad_pciide_dma =
			    ata_init_drive_pcidma(ata_ctlp, ata_drvp, tdip);
			ata_show_transfer_mode(ata_ctlp, ata_drvp);
		}
	}

	if (target_type == ATA_DEV_ATAPI) {
		rc = scsa_bus_ops_p->bus_ctl(d, r, o, a, v);
	} else {
		rc = ata_disk_bus_ctl(d, r, o, a, v);
	}

	return (rc);
}

/*
 *
 * GHD ccc_hba_complete callback
 *
 */

/* ARGSUSED */
static void
ata_hba_complete(
	void *hba_handle,
	gcmd_t *gcmdp,
	int do_callback)
{
	ata_drv_t *ata_drvp;
	ata_pkt_t *ata_pktp;

	ADBG_TRACE(("ata_hba_complete entered\n"));

	ata_drvp = GCMD2DRV(gcmdp);
	ata_pktp = GCMD2APKT(gcmdp);
	if (ata_pktp->ap_complete)
		(*ata_pktp->ap_complete)(ata_drvp, ata_pktp,
		    do_callback);
}

/* GHD ccc_timeout_func callback */

/* ARGSUSED */
static int
ata_timeout_func(
	void	*hba_handle,
	gcmd_t	*gcmdp,
	gtgt_t	*gtgtp,
	gact_t	 action,
	int	 calltype)
{
	ata_ctl_t *ata_ctlp;
	ata_pkt_t *ata_pktp;
	ata_drv_t *ata_drvp;

	ADBG_TRACE(("ata_timeout_func entered\n"));

	ata_ctlp = (ata_ctl_t *)hba_handle;

	if (gcmdp != NULL)
		ata_pktp = GCMD2APKT(gcmdp);
	else
		ata_pktp = NULL;

	switch (action) {
	case GACTION_EARLY_ABORT:
		/* abort before request was started */
		if (ata_pktp != NULL) {
			ata_pktp->ap_flags |= AP_ABORT;
		}
		ghd_complete(&ata_ctlp->ac_ccc, gcmdp);
		return (TRUE);

	case GACTION_EARLY_TIMEOUT:
		/* timeout before request was started */
		if (ata_pktp != NULL) {
			ata_pktp->ap_flags |= AP_TIMEOUT;
		}
		ghd_complete(&ata_ctlp->ac_ccc, gcmdp);
		return (TRUE);

	case GACTION_RESET_TARGET:
		/*
		 * Reset a device is not supported. Resetting a specific
		 * device can't be done at all to an ATA device and if
		 * you send a RESET to an ATAPI device you have to
		 * reset the whole bus to make certain both devices
		 * on the bus stay in sync regarding which device is
		 * the currently selected one.
		 */
		return (FALSE);

	case GACTION_RESET_BUS:
		/*
		 * Issue bus reset and reinitialize both drives.
		 * But only if this is a timed-out request. Target
		 * driver reset requests are ignored because ATA
		 * and ATAPI devices shouldn't be gratuitously reset.
		 * Also disable DMA if it is a CF device.
		 */
		if (gcmdp == NULL)
			break;
		ata_drvp = GCMD2DRV(gcmdp);
		if (ata_drvp != NULL)
			if (ata_drvp->ad_id.ai_config == ATA_ID_CF_TO_ATA)
				ata_disable_DMA(ata_drvp);
		return (ata_reset_bus(ata_ctlp));
	default:
		break;
	}
	return (FALSE);
}

/*
 *
 * Initialize controller's soft-state structure
 *
 */

static ata_ctl_t *
ata_init_controller(
	dev_info_t *dip)
{
	ata_ctl_t *ata_ctlp;
	int	   instance;
	caddr_t	   ioaddr1;
	caddr_t	   ioaddr2;

	ADBG_TRACE(("ata_init_controller entered\n"));

	instance = ddi_get_instance(dip);

	/* allocate controller structure */
	if (ddi_soft_state_zalloc(ata_state, instance) != DDI_SUCCESS) {
		ADBG_WARN(("ata_init_controller: soft_state_zalloc failed\n"));
		return (NULL);
	}

	ata_ctlp = ddi_get_soft_state(ata_state, instance);

	if (ata_ctlp == NULL) {
		ADBG_WARN(("ata_init_controller: failed to find "
		    "controller struct\n"));
		return (NULL);
	}

	/*
	 * initialize per-controller data
	 */
	ata_ctlp->ac_dip = dip;
	ata_ctlp->ac_arq_pktp = kmem_zalloc(sizeof (ata_pkt_t), KM_SLEEP);

	/*
	 * map the device registers
	 */
	if (!ata_setup_ioaddr(dip, &ata_ctlp->ac_iohandle1, &ioaddr1,
	    &ata_ctlp->ac_iohandle2, &ioaddr2,
	    &ata_ctlp->ac_bmhandle, &ata_ctlp->ac_bmaddr)) {
		(void) ata_detach(dip, DDI_DETACH);
		return (NULL);
	}

	ADBG_INIT(("ata_init_controller: ioaddr1 = 0x%p, ioaddr2 = 0x%p\n",
	    ioaddr1, ioaddr2));

	/*
	 * Do ARQ setup
	 */
	atapi_init_arq(ata_ctlp);

	/*
	 * Do PCI-IDE setup
	 */
	ata_init_pciide(dip, ata_ctlp);

	/*
	 * port addresses associated with ioaddr1
	 */
	ata_ctlp->ac_ioaddr1	= ioaddr1;
	ata_ctlp->ac_data	= (ushort_t *)ioaddr1 + AT_DATA;
	ata_ctlp->ac_error	= (uchar_t *)ioaddr1 + AT_ERROR;
	ata_ctlp->ac_feature	= (uchar_t *)ioaddr1 + AT_FEATURE;
	ata_ctlp->ac_count	= (uchar_t *)ioaddr1 + AT_COUNT;
	ata_ctlp->ac_sect	= (uchar_t *)ioaddr1 + AT_SECT;
	ata_ctlp->ac_lcyl	= (uchar_t *)ioaddr1 + AT_LCYL;
	ata_ctlp->ac_hcyl	= (uchar_t *)ioaddr1 + AT_HCYL;
	ata_ctlp->ac_drvhd	= (uchar_t *)ioaddr1 + AT_DRVHD;
	ata_ctlp->ac_status	= (uchar_t *)ioaddr1 + AT_STATUS;
	ata_ctlp->ac_cmd	= (uchar_t *)ioaddr1 + AT_CMD;

	/*
	 * port addresses associated with ioaddr2
	 */
	ata_ctlp->ac_ioaddr2	= ioaddr2;
	ata_ctlp->ac_altstatus	= (uchar_t *)ioaddr2 + AT_ALTSTATUS;
	ata_ctlp->ac_devctl	= (uchar_t *)ioaddr2 + AT_DEVCTL;

	/*
	 * If AC_BSY_WAIT needs to be set  for laptops that do
	 * suspend/resume but do not correctly wait for the busy bit to
	 * drop after a resume.
	 */
	ata_ctlp->ac_timing_flags = ddi_prop_get_int(DDI_DEV_T_ANY,
	    dip, DDI_PROP_DONTPASS, "timing_flags", 0);
	/*
	 * get max transfer size, default to 256 sectors
	 */
	ata_ctlp->ac_max_transfer = ddi_prop_get_int(DDI_DEV_T_ANY,
	    dip, DDI_PROP_DONTPASS, "max_transfer", 0x100);
	if (ata_ctlp->ac_max_transfer < 1)
		ata_ctlp->ac_max_transfer = 1;
	if (ata_ctlp->ac_max_transfer > 0x100)
		ata_ctlp->ac_max_transfer = 0x100;

	/*
	 * Get the standby timer value
	 */
	ata_ctlp->ac_standby_time = ddi_prop_get_int(DDI_DEV_T_ANY,
	    dip, DDI_PROP_DONTPASS, "standby", -1);

	/*
	 * If this is a /pci/pci-ide instance check to see if
	 * it's supposed to be attached as an /isa/ata
	 */
	if (ata_ctlp->ac_pciide) {
		static char prop_buf[] = "SUNW-ata-ffff-isa";
		int addr1 = (intptr_t)ioaddr1;


		if (addr1 < 0 || addr1 > 0xffff) {
			(void) ata_detach(dip, DDI_DETACH);
			return (NULL);
		}
		(void) sprintf(prop_buf, "SUNW-ata-%04x-isa",
		    addr1);
		if (ddi_prop_exists(DDI_DEV_T_ANY, ddi_root_node(),
		    DDI_PROP_DONTPASS, prop_buf)) {
			(void) ata_detach(dip, DDI_DETACH);
			return (NULL);
		}
	}

	/* Init controller specific stuff */
	(void) ata_spec_init_controller(dip);

	/*
	 * initialize GHD
	 */

	GHD_WAITQ_INIT(&ata_ctlp->ac_ccc.ccc_waitq, NULL, 1);

	if (!ghd_register("ata", &ata_ctlp->ac_ccc, dip, 0, ata_ctlp,
	    atapi_ccballoc, atapi_ccbfree,
	    ata_pciide_dma_sg_func, ata_hba_start,
	    ata_hba_complete, ata_intr,
	    ata_get_status, ata_process_intr, ata_timeout_func,
	    &ata_timer_conf, NULL)) {
		(void) ata_detach(dip, DDI_DETACH);
		return (NULL);
	}

	ata_ctlp->ac_flags |= AC_GHD_INIT;
	return (ata_ctlp);
}

/* destroy a controller */

static void
ata_destroy_controller(
	dev_info_t *dip)
{
	ata_ctl_t *ata_ctlp;
	int	instance;

	ADBG_TRACE(("ata_destroy_controller entered\n"));

	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);

	if (ata_ctlp == NULL)
		return;

	/* destroy ghd */
	if (ata_ctlp->ac_flags & AC_GHD_INIT)
		ghd_unregister(&ata_ctlp->ac_ccc);

	/* free the pciide buffer (if any) */
	ata_pciide_free(ata_ctlp);

	/* destroy controller struct */
	kmem_free(ata_ctlp->ac_arq_pktp, sizeof (ata_pkt_t));
	ddi_soft_state_free(ata_state, instance);

}


/*
 *
 * initialize a drive
 *
 */

static ata_drv_t *
ata_init_drive(
	ata_ctl_t	*ata_ctlp,
	uchar_t		targ,
	uchar_t		lun)
{
	static	char	 nec_260[]	= "NEC CD-ROM DRIVE";
	ata_drv_t *ata_drvp;
	struct ata_id	*aidp;
	char	buf[80];
	int	drive_type;
	int	i;
	int	valid_version = 0;

	ADBG_TRACE(("ata_init_drive entered, targ = %d, lun = %d\n",
	    targ, lun));

	/* check if device already exists */

	ata_drvp = CTL2DRV(ata_ctlp, targ, lun);

	if (ata_drvp != NULL)
		return (ata_drvp);

	/* allocate new device structure */

	ata_drvp = kmem_zalloc(sizeof (ata_drv_t), KM_SLEEP);
	aidp = &ata_drvp->ad_id;

	/*
	 * set up drive struct
	 */
	ata_drvp->ad_ctlp = ata_ctlp;
	ata_drvp->ad_pciide_dma = ATA_DMA_UNINITIALIZED;
	ata_drvp->ad_targ = targ;
	ata_drvp->ad_drive_bits =
	    (ata_drvp->ad_targ == 0 ? ATDH_DRIVE0 : ATDH_DRIVE1);
	/*
	 * Add the LUN for SFF-8070i support
	 */
	ata_drvp->ad_lun = lun;
	ata_drvp->ad_drive_bits |= ata_drvp->ad_lun;

	/*
	 * get drive type, side effect is to collect
	 * IDENTIFY DRIVE data
	 */

	drive_type = ata_drive_type(ata_drvp->ad_drive_bits,
	    ata_ctlp->ac_iohandle1,
	    ata_ctlp->ac_ioaddr1,
	    ata_ctlp->ac_iohandle2,
	    ata_ctlp->ac_ioaddr2,
	    aidp);

	switch (drive_type) {
	case ATA_DEV_NONE:
		/* no drive found */
		goto errout;
	case ATA_DEV_ATAPI:
		ata_drvp->ad_flags |= AD_ATAPI;
		break;
	case ATA_DEV_DISK:
		ata_drvp->ad_flags |= AD_DISK;
		break;
	}

	/*
	 * swap bytes of all text fields
	 */
	if (!ata_strncmp(nec_260, aidp->ai_model, sizeof (aidp->ai_model))) {
		swab(aidp->ai_drvser, aidp->ai_drvser,
		    sizeof (aidp->ai_drvser));
		swab(aidp->ai_fw, aidp->ai_fw,
		    sizeof (aidp->ai_fw));
		swab(aidp->ai_model, aidp->ai_model,
		    sizeof (aidp->ai_model));
	}

	/*
	 * Check if this drive has the Single Sector bug
	 */

	if (ata_check_drive_blacklist(&ata_drvp->ad_id, ATA_BL_1SECTOR))
		ata_drvp->ad_flags |= AD_1SECTOR;
	else
		ata_drvp->ad_flags &= ~AD_1SECTOR;

	if (ata_check_drive_blacklist(&ata_drvp->ad_id, ATA_BL_LBA48))
		ata_drvp->ad_flags |= AD_BLLBA48;
	else
		ata_drvp->ad_flags &= ~AD_BLLBA48;

	/* Check if this drive has the "revert to defaults" bug */
	if (!ata_check_revert_to_defaults(ata_drvp))
		ata_drvp->ad_flags |= AD_NORVRT;

	/* Dump the drive info */
	(void) strncpy(buf, aidp->ai_model, sizeof (aidp->ai_model));
	buf[sizeof (aidp->ai_model)-1] = '\0';
	for (i = sizeof (aidp->ai_model) - 2; buf[i] == ' '; i--)
		buf[i] = '\0';

	ATAPRT(("?\t%s device at targ %d, lun %d lastlun 0x%x\n",
	    (ATAPIDRV(ata_drvp) ? "ATAPI":"IDE"),
	    ata_drvp->ad_targ, ata_drvp->ad_lun, aidp->ai_lastlun));

	ATAPRT(("?\tmodel %s\n", buf));

	if (aidp->ai_majorversion != 0 && aidp->ai_majorversion != 0xffff) {
		for (i = 14; i >= 2; i--) {
			if (aidp->ai_majorversion & (1 << i)) {
				valid_version = i;
				break;
			}
		}
		ATAPRT((
		    "?\tATA/ATAPI-%d supported, majver 0x%x minver 0x%x\n",
		    valid_version,
		    aidp->ai_majorversion,
		    aidp->ai_minorversion));
	}

	if (ata_capability_data) {

		ATAPRT(("?\t\tstat %x, err %x\n",
		    ddi_get8(ata_ctlp->ac_iohandle2,
		    ata_ctlp->ac_altstatus),
		    ddi_get8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_error)));

		ATAPRT(("?\t\tcfg 0x%x, cap 0x%x\n",
		    aidp->ai_config,
		    aidp->ai_cap));

		/*
		 * Be aware that ATA-6 and later drives may not provide valid
		 * geometry information and other obsoleted info.
		 * Select what is printed based on supported ATA model (skip
		 * anything below ATA/ATAPI-3)
		 */

		if (valid_version == 0 || aidp->ai_majorversion <
		    ATAC_MAJVER_6) {
			/*
			 * Supported version less then ATA-6
			 */
			ATAPRT(("?\t\tcyl %d, hd %d, sec/trk %d\n",
			    aidp->ai_fixcyls,
			    aidp->ai_heads,
			    aidp->ai_sectors));
		}
		ATAPRT(("?\t\tmult1 0x%x, mult2 0x%x\n",
		    aidp->ai_mult1,
		    aidp->ai_mult2));
		if (valid_version && aidp->ai_majorversion < ATAC_MAJVER_4) {
			ATAPRT((
			"?\t\tpiomode 0x%x, dmamode 0x%x, advpiomode 0x%x\n",
			    aidp->ai_piomode,
			    aidp->ai_dmamode,
			    aidp->ai_advpiomode));
		} else {
			ATAPRT(("?\t\tadvpiomode 0x%x\n",
			    aidp->ai_advpiomode));
		}
		ATAPRT(("?\t\tminpio %d, minpioflow %d\n",
		    aidp->ai_minpio,
		    aidp->ai_minpioflow));
		if (valid_version && aidp->ai_majorversion >= ATAC_MAJVER_4 &&
		    (aidp->ai_validinfo & ATAC_VALIDINFO_83)) {
			ATAPRT(("?\t\tdwdma 0x%x, ultradma 0x%x\n",
			    aidp->ai_dworddma,
			    aidp->ai_ultradma));
		} else {
			ATAPRT(("?\t\tdwdma 0x%x\n",
			    aidp->ai_dworddma));
		}
	}

	if (ATAPIDRV(ata_drvp)) {
		if (!atapi_init_drive(ata_drvp))
			goto errout;
	} else {
		if (!ata_disk_init_drive(ata_drvp))
			goto errout;
	}

	/*
	 * store pointer in controller struct
	 */
	CTL2DRV(ata_ctlp, targ, lun) = ata_drvp;

	/*
	 * lock the drive's current settings in case I have to
	 * reset the drive due to some sort of error
	 */
	(void) ata_set_feature(ata_ctlp, ata_drvp, ATSF_DIS_REVPOD, 0);

	return (ata_drvp);

errout:
	ata_uninit_drive(ata_drvp);
	return (NULL);
}

/* destroy a drive */

static void
ata_uninit_drive(
	ata_drv_t *ata_drvp)
{
#if 0
	ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp;
#endif

	ADBG_TRACE(("ata_uninit_drive entered\n"));

#if 0
	/*
	 * DON'T DO THIS. disabling interrupts floats the IRQ line
	 * which generates spurious interrupts
	 */

	/*
	 * Select the correct drive
	 */
	ddi_put8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_drvhd,
	    ata_drvp->ad_drive_bits);
	ata_nsecwait(400);

	/*
	 * Disable interrupts from the drive
	 */
	ddi_put8(ata_ctlp->ac_iohandle2, ata_ctlp->ac_devctl,
	    (ATDC_D3 | ATDC_NIEN));
#endif

	/* interface specific clean-ups */

	if (ata_drvp->ad_flags & AD_ATAPI)
		atapi_uninit_drive(ata_drvp);
	else if (ata_drvp->ad_flags & AD_DISK)
		ata_disk_uninit_drive(ata_drvp);

	/* free drive struct */

	kmem_free(ata_drvp, sizeof (ata_drv_t));
}


/*
 * ata_drive_type()
 *
 * The timeout values and exact sequence of checking is critical
 * especially for atapi device detection, and should not be changed lightly.
 *
 */
static int
ata_drive_type(
	uchar_t		 drvhd,
	ddi_acc_handle_t io_hdl1,
	caddr_t		 ioaddr1,
	ddi_acc_handle_t io_hdl2,
	caddr_t		 ioaddr2,
	struct ata_id	*ata_id_bufp)
{
	uchar_t	status;

	ADBG_TRACE(("ata_drive_type entered\n"));

	/*
	 * select the appropriate drive and LUN
	 */
	ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_DRVHD, drvhd);
	ata_nsecwait(400);

	/*
	 * make certain the drive is selected, and wait for not busy
	 */
	(void) ata_wait3(io_hdl2, ioaddr2, 0, ATS_BSY, 0x7f, 0, 0x7f, 0,
	    5 * 1000000);

	status = ddi_get8(io_hdl2, (uchar_t *)ioaddr2 + AT_ALTSTATUS);

	if (status & ATS_BSY) {
		ADBG_TRACE(("ata_drive_type 0x%p 0x%x\n", ioaddr1, status));
		return (ATA_DEV_NONE);
	}

	if (ata_disk_id(io_hdl1, ioaddr1, io_hdl2, ioaddr2, ata_id_bufp))
		return (ATA_DEV_DISK);

	/*
	 * No disk, check for atapi unit.
	 */
	if (!atapi_signature(io_hdl1, ioaddr1)) {
#ifndef ATA_DISABLE_ATAPI_1_7
		/*
		 * Check for old (but prevalent) atapi 1.7B
		 * spec device, the only known example is the
		 * NEC CDR-260 (not 260R which is (mostly) ATAPI 1.2
		 * compliant). This device has no signature
		 * and requires conversion from hex to BCD
		 * for some scsi audio commands.
		 */
		if (atapi_id(io_hdl1, ioaddr1, io_hdl2, ioaddr2, ata_id_bufp)) {
			return (ATA_DEV_ATAPI);
		}
#endif
		return (ATA_DEV_NONE);
	}

	if (atapi_id(io_hdl1, ioaddr1, io_hdl2, ioaddr2, ata_id_bufp)) {
		return (ATA_DEV_ATAPI);
	}

	return (ATA_DEV_NONE);

}

/*
 * nsec-granularity time delay function
 */
void
ata_nsecwait(clock_t count)
{
	extern int tsc_gethrtime_initted;

	if (tsc_gethrtime_initted) {
		hrtime_t end = gethrtime() + count;

		while (gethrtime() < end) {
			SMT_PAUSE();
		}
	} else {
		drv_usecwait(1 + (count / 1000));
	}
}


/*
 * Wait for a register of a controller to achieve a specific state.
 * To return normally, all the bits in the first sub-mask must be ON,
 * all the bits in the second sub-mask must be OFF.
 * If timeout_usec microseconds pass without the controller achieving
 * the desired bit configuration, we return TRUE, else FALSE.
 */

int ata_usec_delay = 10;

int
ata_wait(
	ddi_acc_handle_t io_hdl,
	caddr_t		ioaddr,
	uchar_t		onbits,
	uchar_t		offbits,
	uint_t		timeout_usec)
{
	ushort_t val;
	hrtime_t deadline = gethrtime() +
	    (hrtime_t)timeout_usec * (NANOSEC / MICROSEC);


	do  {
		val = ddi_get8(io_hdl, (uchar_t *)ioaddr + AT_ALTSTATUS);
		if ((val & onbits) == onbits && (val & offbits) == 0)
			return (TRUE);
		drv_usecwait(ata_usec_delay);
	} while (gethrtime() < deadline);

	return (FALSE);
}


/*
 *
 * This is a slightly more complicated version that checks
 * for error conditions and bails-out rather than looping
 * until the timeout expires
 */
int
ata_wait3(
	ddi_acc_handle_t io_hdl,
	caddr_t		ioaddr,
	uchar_t		onbits1,
	uchar_t		offbits1,
	uchar_t		failure_onbits2,
	uchar_t		failure_offbits2,
	uchar_t		failure_onbits3,
	uchar_t		failure_offbits3,
	uint_t		timeout_usec)
{
	ushort_t val;
	hrtime_t deadline = gethrtime() +
	    (hrtime_t)timeout_usec * (NANOSEC / MICROSEC);

	do  {
		val = ddi_get8(io_hdl, (uchar_t *)ioaddr + AT_ALTSTATUS);

		/*
		 * check for expected condition
		 */
		if ((val & onbits1) == onbits1 && (val & offbits1) == 0)
			return (TRUE);

		/*
		 * check for error conditions
		 */
		if ((val & failure_onbits2) == failure_onbits2 &&
		    (val & failure_offbits2) == 0) {
			return (FALSE);
		}

		if ((val & failure_onbits3) == failure_onbits3 &&
		    (val & failure_offbits3) == 0) {
			return (FALSE);
		}

		drv_usecwait(ata_usec_delay);
	} while (gethrtime() < deadline);

	return (FALSE);
}


/*
 *
 * low level routine for ata_disk_id() and atapi_id()
 *
 */

int
ata_id_common(
	uchar_t		 id_cmd,
	int		 expect_drdy,
	ddi_acc_handle_t io_hdl1,
	caddr_t		 ioaddr1,
	ddi_acc_handle_t io_hdl2,
	caddr_t		 ioaddr2,
	struct ata_id	*aidp)
{
	uchar_t	status;

	ADBG_TRACE(("ata_id_common entered\n"));

	bzero(aidp, sizeof (struct ata_id));

	/*
	 * clear the features register
	 */
	ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_FEATURE, 0);

	/*
	 * Disable interrupts from the device.  When the ata
	 * hardware is sharing its interrupt with another
	 * device, the shared interrupt might have already been
	 * unmasked in the interrupt controller and
	 * triggering ata device interrupts will result in an
	 * interrupt storm and a hung system.
	 */
	ddi_put8(io_hdl2, (uchar_t *)ioaddr2 + AT_DEVCTL, ATDC_D3 | ATDC_NIEN);

	/*
	 * issue IDENTIFY DEVICE or IDENTIFY PACKET DEVICE command
	 */
	ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_CMD, id_cmd);

	/* wait for the busy bit to settle */
	ata_nsecwait(400);

	/*
	 * read alternate status and check for conditions which
	 * may indicate the drive is not present, to prevent getting
	 * stuck in ata_wait3() below.
	 */
	status = ddi_get8(io_hdl2, (uchar_t *)ioaddr2 + AT_ALTSTATUS);

	/*
	 * 0x0, 0x7f, or ATS_DF can happen when no drive is present
	 */
	if ((status == 0x0) || (status == 0x7f) ||
	    ((status & (ATS_BSY|ATS_DF)) == ATS_DF)) {
		/* invalid status, can't be an ATA or ATAPI device */
		return (FALSE);
	}

	/*
	 * According to the ATA specification, some drives may have
	 * to read the media to complete this command.  We need to
	 * make sure we give them enough time to respond.
	 */
	(void) ata_wait3(io_hdl2, ioaddr2, 0, ATS_BSY,
	    ATS_ERR, ATS_BSY, 0x7f, 0, 5 * 1000000);

	/*
	 * read the status byte and clear the pending interrupt
	 */
	status = ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_STATUS);

	/*
	 * this happens if there's no drive present
	 */
	if (status == 0xff || status == 0x7f) {
		/* invalid status, can't be an ATA or ATAPI device */
		return (FALSE);
	}

	if (status & ATS_BSY) {
		ADBG_ERROR(("ata_id_common: BUSY status 0x%x error 0x%x\n",
		    ddi_get8(io_hdl2, (uchar_t *)ioaddr2 +AT_ALTSTATUS),
		    ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
		return (FALSE);
	}

	if (!(status & ATS_DRQ)) {
		if (status & (ATS_ERR | ATS_DF)) {
			return (FALSE);
		}
		/*
		 * Give the drive another second to assert DRQ. Some older
		 * drives de-assert BSY before asserting DRQ. Bail out
		 * immediately if the status becomes 0x7f, which is invalid
		 * value. It can happen when no drive is present.
		 */
		if (!ata_wait3(io_hdl2, ioaddr2, ATS_DRQ, ATS_BSY, 0x7f,
		    ATS_BSY, 0x7f, ATS_BSY, 1000000)) {
			ADBG_WARN(("ata_id_common: "
			    "!DRQ status 0x%x error 0x%x\n",
			    ddi_get8(io_hdl2, (uchar_t *)ioaddr2 +AT_ALTSTATUS),
			    ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
			return (FALSE);
		}
	}

	/*
	 * transfer the data
	 */
	ddi_rep_get16(io_hdl1, (ushort_t *)aidp, (ushort_t *)ioaddr1 + AT_DATA,
	    NBPSCTR >> 1, DDI_DEV_NO_AUTOINCR);

	/* wait for the busy bit to settle */
	ata_nsecwait(400);


	/*
	 * Wait for the drive to recognize I've read all the data.
	 * Some drives have been observed to take as much as 3msec to
	 * deassert DRQ after reading the data; allow 1 sec just in case.
	 *
	 * Note: some non-compliant ATAPI drives (e.g., NEC Multispin 6V,
	 * CDR-1350A) don't assert DRDY. If we've made it this far we can
	 * safely ignore the DRDY bit since the ATAPI Packet command
	 * actually doesn't require it to ever be asserted.
	 *
	 * Bail out immediately if the status becomes 0x7f, which is invalid
	 * value. It can happen when no drive is present.
	 *
	 */
	if (!ata_wait3(io_hdl2, ioaddr2, (uchar_t)(expect_drdy ? ATS_DRDY : 0),
	    (ATS_BSY | ATS_DRQ), 0x7f, ATS_BSY, 0x7f, ATS_BSY, 1000000)) {
		ADBG_WARN(("ata_id_common: bad status 0x%x error 0x%x\n",
		    ddi_get8(io_hdl2, (uchar_t *)ioaddr2 + AT_ALTSTATUS),
		    ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
		return (FALSE);
	}

	/*
	 * Check to see if the command aborted. This happens if
	 * an IDENTIFY DEVICE command is issued to an ATAPI PACKET device,
	 * or if an IDENTIFY PACKET DEVICE command is issued to an ATA
	 * (non-PACKET) device.
	 */
	if (status & (ATS_DF | ATS_ERR)) {
		ADBG_WARN(("ata_id_common: status 0x%x error 0x%x \n",
		    ddi_get8(io_hdl2, (uchar_t *)ioaddr2 + AT_ALTSTATUS),
		    ddi_get8(io_hdl1, (uchar_t *)ioaddr1 + AT_ERROR)));
		return (FALSE);
	}
	return (TRUE);
}


/*
 * Low level routine to issue a non-data command and busy wait for
 * the completion status.
 */

int
ata_command(
	ata_ctl_t *ata_ctlp,
	ata_drv_t *ata_drvp,
	int		 expect_drdy,
	int		 silent,
	uint_t		 busy_wait,
	uchar_t		 cmd,
	uchar_t		 feature,
	uchar_t		 count,
	uchar_t		 sector,
	uchar_t		 head,
	uchar_t		 cyl_low,
	uchar_t		 cyl_hi)
{
	ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1;
	ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2;
	uchar_t		 status;

	/* select the drive */
	ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, ata_drvp->ad_drive_bits);
	ata_nsecwait(400);

	/* make certain the drive selected */
	if (!ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2,
	    (uchar_t)(expect_drdy ? ATS_DRDY : 0),
	    ATS_BSY, busy_wait)) {
		ADBG_ERROR(("ata_command: select failed "
		    "DRDY 0x%x CMD 0x%x F 0x%x N 0x%x  "
		    "S 0x%x H 0x%x CL 0x%x CH 0x%x\n",
		    expect_drdy, cmd, feature, count,
		    sector, head, cyl_low, cyl_hi));
		return (FALSE);
	}

	/*
	 * set all the regs
	 */
	ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, (head | ata_drvp->ad_drive_bits));
	ddi_put8(io_hdl1, ata_ctlp->ac_sect, sector);
	ddi_put8(io_hdl1, ata_ctlp->ac_count, count);
	ddi_put8(io_hdl1, ata_ctlp->ac_lcyl, cyl_low);
	ddi_put8(io_hdl1, ata_ctlp->ac_hcyl, cyl_hi);
	ddi_put8(io_hdl1, ata_ctlp->ac_feature, feature);

	/* send the command */
	ddi_put8(io_hdl1, ata_ctlp->ac_cmd, cmd);

	/* wait for the busy bit to settle */
	ata_nsecwait(400);

	/* wait for not busy */
	if (!ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2, 0, ATS_BSY, busy_wait)) {
		ADBG_ERROR(("ata_command: BSY too long!"
		    "DRDY 0x%x CMD 0x%x F 0x%x N 0x%x  "
		    "S 0x%x H 0x%x CL 0x%x CH 0x%x\n",
		    expect_drdy, cmd, feature, count,
		    sector, head, cyl_low, cyl_hi));
		return (FALSE);
	}

	/*
	 * wait for DRDY before continuing
	 */
	(void) ata_wait3(io_hdl2, ata_ctlp->ac_ioaddr2,
	    ATS_DRDY, ATS_BSY, /* okay */
	    ATS_ERR, ATS_BSY, /* cmd failed */
	    ATS_DF, ATS_BSY, /* drive failed */
	    busy_wait);

	/* read status to clear IRQ, and check for error */
	status =  ddi_get8(io_hdl1, ata_ctlp->ac_status);

	if ((status & (ATS_BSY | ATS_DF | ATS_ERR)) == 0)
		return (TRUE);

	if (!silent) {
		ADBG_ERROR(("ata_command status 0x%x error 0x%x "
		    "DRDY 0x%x CMD 0x%x F 0x%x N 0x%x  "
		    "S 0x%x H 0x%x CL 0x%x CH 0x%x\n",
		    ddi_get8(io_hdl1, ata_ctlp->ac_status),
		    ddi_get8(io_hdl1, ata_ctlp->ac_error),
		    expect_drdy, cmd, feature, count,
		    sector, head, cyl_low, cyl_hi));
	}
	return (FALSE);
}



/*
 *
 * Issue a SET FEATURES command
 *
 */

int
ata_set_feature(
	ata_ctl_t *ata_ctlp,
	ata_drv_t *ata_drvp,
	uchar_t    feature,
	uchar_t    value)
{
	int		 rc;

	rc = ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, ata_set_feature_wait,
	    ATC_SET_FEAT, feature, value, 0, 0, 0, 0);
	/* feature, count, sector, head, cyl_low, cyl_hi */

	if (rc) {
		return (TRUE);
	}

	ADBG_ERROR(("?ata_set_feature: (0x%x,0x%x) failed\n", feature, value));
	return (FALSE);
}



/*
 *
 * Issue a FLUSH CACHE command
 *
 */

static int
ata_flush_cache(
	ata_ctl_t *ata_ctlp,
	ata_drv_t *ata_drvp)
{
	/* this command is optional so fail silently */
	return (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE,
	    ata_flush_cache_wait,
	    ATC_FLUSH_CACHE, 0, 0, 0, 0, 0, 0));
}

/*
 * ata_setup_ioaddr()
 *
 * Map the device registers and return the handles.
 *
 * If this is a ISA-ATA controller then only two handles are
 * initialized and returned.
 *
 * If this is a PCI-IDE controller than a third handle (for the
 * PCI-IDE Bus Mastering registers) is initialized and returned.
 *
 */

static int
ata_setup_ioaddr(
	dev_info_t	 *dip,
	ddi_acc_handle_t *handle1p,
	caddr_t		 *addr1p,
	ddi_acc_handle_t *handle2p,
	caddr_t		 *addr2p,
	ddi_acc_handle_t *bm_hdlp,
	caddr_t		 *bm_addrp)
{
	ddi_device_acc_attr_t dev_attr;
	int	 rnumber;
	int	 rc;
	off_t	 regsize;

	/*
	 * Make certain the controller is enabled and its regs are map-able
	 *
	 */
	rc = ddi_dev_regsize(dip, 0, &regsize);
	if (rc != DDI_SUCCESS || regsize <= AT_CMD) {
		ADBG_INIT(("ata_setup_ioaddr(1): rc %d regsize %lld\n",
		    rc, (long long)regsize));
		return (FALSE);
	}

	rc = ddi_dev_regsize(dip, 1, &regsize);
	if (rc != DDI_SUCCESS || regsize <= AT_ALTSTATUS) {
		ADBG_INIT(("ata_setup_ioaddr(2): rc %d regsize %lld\n",
		    rc, (long long)regsize));
		return (FALSE);
	}

	/*
	 * setup the device attribute structure for little-endian,
	 * strict ordering access.
	 */
	dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	dev_attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
	dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	*handle1p = NULL;
	*handle2p = NULL;
	*bm_hdlp = NULL;

	/*
	 * Determine whether this is a ISA, PNP-ISA, or PCI-IDE device
	 */
	if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "pnp-csn")) {
		/* it's PNP-ISA, skip over the extra reg tuple */
		rnumber = 1;
		goto not_pciide;
	}

	/* else, it's ISA or PCI-IDE, check further */
	rnumber = 0;

	if (!ata_is_pci(dip)) {
		/*
		 * If it's not a PCI-IDE, there are only two reg tuples
		 * and the first one contains the I/O base (170 or 1f0)
		 * rather than the controller instance number.
		 */
		ADBG_TRACE(("ata_setup_ioaddr !pci-ide\n"));
		goto not_pciide;
	}


	/*
	 * Map the correct half of the PCI-IDE Bus Master registers.
	 * There's a single BAR that maps these registers for both
	 * controller's in a dual-controller chip and it's upto my
	 * parent nexus, pciide, to adjust which (based on my instance
	 * number) half this call maps.
	 */
	rc = ddi_dev_regsize(dip, 2, &regsize);
	if (rc != DDI_SUCCESS || regsize < 8) {
		ADBG_INIT(("ata_setup_ioaddr(3): rc %d regsize %lld\n",
		    rc, (long long)regsize));
		goto not_pciide;
	}

	rc = ddi_regs_map_setup(dip, 2, bm_addrp, 0, 0, &dev_attr, bm_hdlp);

	if (rc != DDI_SUCCESS) {
		/* map failed, try to use in non-pci-ide mode */
		ADBG_WARN(("ata_setup_ioaddr bus master map failed, rc=0x%x\n",
		    rc));
		*bm_hdlp = NULL;
	}

not_pciide:
	/*
	 * map the lower command block registers
	 */

	rc = ddi_regs_map_setup(dip, rnumber, addr1p, 0, 0, &dev_attr,
	    handle1p);

	if (rc != DDI_SUCCESS) {
		cmn_err(CE_WARN, "ata: reg tuple 0 map failed, rc=0x%x\n", rc);
		goto out1;
	}

	/*
	 * If the controller is being used in compatibility mode
	 * via /devices/isa/ata@1,{1f0,1f0}/..., the reg property
	 * will specify zeros for the I/O ports for the PCI
	 * instance.
	 */
	if (*addr1p == 0) {
		ADBG_TRACE(("ata_setup_ioaddr ioaddr1 0\n"));
		goto out2;
	}

	/*
	 * map the upper control block registers
	 */
	rc = ddi_regs_map_setup(dip, rnumber + 1, addr2p, 0, 0, &dev_attr,
	    handle2p);
	if (rc == DDI_SUCCESS)
		return (TRUE);

	cmn_err(CE_WARN, "ata: reg tuple 1 map failed, rc=0x%x", rc);

out2:
	if (*handle1p != NULL) {
		ddi_regs_map_free(handle1p);
		*handle1p = NULL;
	}

out1:
	if (*bm_hdlp != NULL) {
		ddi_regs_map_free(bm_hdlp);
		*bm_hdlp = NULL;
	}
	return (FALSE);

}

/*
 *
 * Currently, the only supported controllers are ones which
 * support the SFF-8038 Bus Mastering spec.
 *
 * Check the parent node's IEEE 1275 class-code property to
 * determine if it's an PCI-IDE instance which supports SFF-8038
 * Bus Mastering. It's perfectly valid to have a PCI-IDE controller
 * that doesn't do Bus Mastering. In that case, my interrupt handler
 * only uses the interrupt latch bit in PCI-IDE status register.
 * The assumption is that the programming interface byte of the
 * class-code property reflects the bus master DMA capability of
 * the controller.
 *
 * Whether the drive support supports the DMA option still needs
 * to be checked later. Each individual request also has to be
 * checked for alignment and size to decide whether to use the
 * DMA transfer mode.
 */

static void
ata_init_pciide(
	dev_info_t	 *dip,
	ata_ctl_t *ata_ctlp)
{
	uint_t	 class_code;
	uchar_t	 status;

	ata_cntrl_DMA_sel_msg = NULL;

	if (ata_ctlp->ac_bmhandle == NULL) {
		ata_ctlp->ac_pciide = FALSE;
		ata_ctlp->ac_pciide_bm = FALSE;
		ata_cntrl_DMA_sel_msg = "cntrl not Bus Master DMA capable";
		return;
	}

	/*
	 * check if it's a known bogus PCI-IDE chip
	 */
	if (ata_check_pciide_blacklist(dip, ATA_BL_BOGUS)) {
		ADBG_WARN(("ata_setup_ioaddr pci-ide blacklist\n"));
		ata_ctlp->ac_pciide = FALSE;
		ata_ctlp->ac_pciide_bm = FALSE;
		ata_cntrl_DMA_sel_msg = "cntrl blacklisted";
		return;
	}
	ata_ctlp->ac_pciide = TRUE;

	if (ata_check_pciide_blacklist(dip, ATA_BL_BMSTATREG_PIO_BROKEN)) {
		ata_ctlp->ac_flags |= AC_BMSTATREG_PIO_BROKEN;
	}

	/*
	 * check for a PCI-IDE chip with a broken DMA engine
	 */
	if (ata_check_pciide_blacklist(dip, ATA_BL_NODMA)) {
		ata_ctlp->ac_pciide_bm = FALSE;
		ata_cntrl_DMA_sel_msg =
		    "cntrl blacklisted/DMA engine broken";
		return;
	}

	/*
	 * Check the Programming Interface register to determine
	 * if this device supports PCI-IDE Bus Mastering. Some PCI-IDE
	 * devices don't support Bus Mastering or DMA.
	 * Since we are dealing with pre-qualified pci-ide controller,
	 * check programming interface byte only.
	 */

	class_code = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
	    DDI_PROP_DONTPASS, "class-code", 0);
	if ((class_code & PCIIDE_BM_CAP_MASK) != PCIIDE_BM_CAP_MASK) {
		ata_ctlp->ac_pciide_bm = FALSE;
		ata_cntrl_DMA_sel_msg =
		    "cntrl not Bus Master DMA capable";
		return;
	}

	/*
	 * Avoid doing DMA on "simplex" chips which share hardware
	 * between channels
	 */
	status = ddi_get8(ata_ctlp->ac_bmhandle,
	    (uchar_t *)ata_ctlp->ac_bmaddr + PCIIDE_BMISX_REG);
	/*
	 * Some motherboards have CSB5's that are wired "to emulate CSB4 mode".
	 * In such a mode, the simplex bit is asserted,  but in fact testing
	 * on such a motherboard has shown that the devices are not simplex
	 * -- DMA can be used on both channels concurrently with no special
	 * considerations.  For chips like this, we have the ATA_BL_NO_SIMPLEX
	 * flag set to indicate that the value of the simplex bit can be
	 * ignored.
	 */

	if (status & PCIIDE_BMISX_SIMPLEX) {
		if (ata_check_pciide_blacklist(dip, ATA_BL_NO_SIMPLEX)) {
			cmn_err(CE_WARN, "Ignoring false simplex bit \n");

		} else {

			int simplex_dma_channel, *rp, proplen, channel;
			int dma_on = FALSE;

			/*
			 * By default,use DMA on channel 0 and PIO on channel
			 * 1.  This can be switched by setting
			 * ata-simplex-dma-channel to:
			 *	0  DMA on channel 0 (default without this
			 *			    property)
			 *	1  DMA on channel 1
			 *	any other value: DMA off on both channels.
			 */
			simplex_dma_channel = ata_prop_lookup_int(DDI_DEV_T_ANY,
			    ata_ctlp->ac_dip, 0, "ata-simplex-dma-channel", 0);

			if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
			    ata_ctlp->ac_dip, DDI_PROP_DONTPASS, "reg", &rp,
			    (uint_t *)&proplen) == DDI_PROP_SUCCESS) {

				channel = *rp;
				ddi_prop_free(rp);

				if (simplex_dma_channel == channel) {
					cmn_err(CE_CONT, "?ata: simplex "
					    "controller.  DMA on channel"
					    "  %d PIO on channel %d",
					    channel, channel ? 0:1);
					dma_on = TRUE;
				} else {
					ata_cntrl_DMA_sel_msg =
					    "simplex controller";
				}
			}

			if (dma_on == FALSE) {
				ata_ctlp->ac_pciide_bm = FALSE;

				return;
			}
		}
	}

	/*
	 * It's a compatible PCI-IDE Bus Mastering controller,
	 * allocate and map the DMA Scatter/Gather list (PRDE table).
	 */
	if (ata_pciide_alloc(dip, ata_ctlp))
		ata_ctlp->ac_pciide_bm = TRUE;
	else {
		ata_ctlp->ac_pciide_bm = FALSE;
		ata_cntrl_DMA_sel_msg = "unable to init DMA S/G list";
	}
}

/*
 *
 * Determine whether to enable DMA support for this drive.
 * The controller and the drive both have to support DMA.
 * The controller's capabilities were already checked in
 * ata_init_pciide(), now just check the drive's capabilities.
 *
 */

static int
ata_init_drive_pcidma(
	ata_ctl_t *ata_ctlp,
	ata_drv_t *ata_drvp,
	dev_info_t *tdip)
{
	boolean_t dma;
	boolean_t cd_dma;
	boolean_t disk_dma;
	boolean_t atapi_dma;
	int ata_options;

	ata_dev_DMA_sel_msg = NULL;

	if (ata_ctlp->ac_pciide_bm != TRUE) {
		ata_dev_DMA_sel_msg =
		    "controller is not Bus Master capable";

		return (ATA_DMA_OFF);
	}

	ata_options = ddi_prop_get_int(DDI_DEV_T_ANY, ata_ctlp->ac_dip,
	    0, "ata-options", 0);

	if (!(ata_options & ATA_OPTIONS_DMA)) {
		/*
		 * Either the ata-options property was not found or
		 * DMA is not enabled by this property
		 */
		ata_dev_DMA_sel_msg =
		    "disabled by \"ata-options\" property";

		return (ATA_DMA_OFF);
	}

	if (ata_check_drive_blacklist(&ata_drvp->ad_id, ATA_BL_NODMA)) {
		ata_dev_DMA_sel_msg = "device not DMA capable; blacklisted";

		return (ATA_DMA_OFF);
	}

	/*
	 * DMA mode is mandatory on ATA-3 (or newer) drives but is
	 * optional on ATA-2 (or older) drives.
	 *
	 * On ATA-2 drives the ai_majorversion word will probably
	 * be 0xffff or 0x0000, check the (now obsolete) DMA bit in
	 * the capabilities word instead. The order of these tests
	 * is important since an ATA-3 drive doesn't have to set
	 * the DMA bit in the capabilities word.
	 *
	 */

	if (!((ata_drvp->ad_id.ai_majorversion & 0x8000) == 0 &&
	    ata_drvp->ad_id.ai_majorversion >= (1 << 2)) &&
	    !(ata_drvp->ad_id.ai_cap & ATAC_DMA_SUPPORT)) {
		ata_dev_DMA_sel_msg = "device not DMA capable";

		return (ATA_DMA_OFF);
	}

	/*
	 * Disable DMA for ATAPI devices on controllers known to
	 * have trouble with ATAPI DMA
	 */

	if (ATAPIDRV(ata_drvp)) {
		if (ata_check_pciide_blacklist(ata_ctlp->ac_dip,
		    ATA_BL_ATAPI_NODMA)) {
			ata_dev_DMA_sel_msg =
			    "controller incapable of DMA for ATAPI device";

			return (ATA_DMA_OFF);
		}
	}
	dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
	    0, "ata-dma-enabled", TRUE);
	disk_dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
	    0, "ata-disk-dma-enabled", TRUE);
	cd_dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
	    0, "atapi-cd-dma-enabled", FALSE);
	atapi_dma = ata_prop_lookup_int(DDI_DEV_T_ANY, tdip,
	    0, "atapi-other-dma-enabled", TRUE);

	if (dma == FALSE) {
		cmn_err(CE_CONT, "?ata_init_drive_pcidma: "
		    "DMA disabled by \"ata-dma-enabled\" property");
		ata_dev_DMA_sel_msg = "disabled by prop ata-dma-enabled";

		return (ATA_DMA_OFF);
	}

	if (IS_CDROM(ata_drvp) == TRUE) {
		if (cd_dma == FALSE) {
			ata_dev_DMA_sel_msg =
			    "disabled.  Control with \"atapi-cd-dma-enabled\""
			    " property";

			return (ATA_DMA_OFF);
		}

	} else if (ATAPIDRV(ata_drvp) == FALSE) {
		if (disk_dma == FALSE) {
			ata_dev_DMA_sel_msg =
			    "disabled by \"ata-disk-dma-enabled\" property";

			return (ATA_DMA_OFF);
		}

	} else if (atapi_dma == FALSE) {
			ata_dev_DMA_sel_msg =
			    "disabled by \"atapi-other-dma-enabled\" property";

			return (ATA_DMA_OFF);
	}

	return (ATA_DMA_ON);
}



/*
 * this compare routine squeezes out extra blanks and
 * returns TRUE if p1 matches the leftmost substring of p2
 */

static int
ata_strncmp(
	char *p1,
	char *p2,
	int cnt)
{

	for (;;) {
		/*
		 * skip over any extra blanks in both strings
		 */
		while (*p1 != '\0' && *p1 == ' ')
			p1++;

		while (cnt != 0 && *p2 == ' ') {
			p2++;
			cnt--;
		}

		/*
		 * compare the two strings
		 */

		if (cnt == 0 || *p1 != *p2)
			break;

		while (cnt > 0 && *p1 == *p2) {
			p1++;
			p2++;
			cnt--;
		}

	}

	/* return TRUE if both strings ended at same point */
	return ((*p1 == '\0') ? TRUE : FALSE);
}

/*
 * Per PSARC/1997/281 create variant="atapi" property (if necessary)
 * on the target's dev_info node. Currently, the sd target driver
 * is the only driver which refers to this property.
 *
 * If the flag ata_id_debug is set also create the
 * the "ata" or "atapi" property on the target's dev_info node
 *
 */

int
ata_prop_create(
	dev_info_t *tgt_dip,
	ata_drv_t  *ata_drvp,
	char	   *name)
{
	int	rc;

	ADBG_TRACE(("ata_prop_create 0x%p 0x%p %s\n", tgt_dip, ata_drvp, name));

	if (strcmp("atapi", name) == 0) {
		rc =  ndi_prop_update_string(DDI_DEV_T_NONE, tgt_dip,
		    "variant", name);
		if (rc != DDI_PROP_SUCCESS)
			return (FALSE);
	}

	if (!ata_id_debug)
		return (TRUE);

	rc =  ndi_prop_update_byte_array(DDI_DEV_T_NONE, tgt_dip, name,
	    (uchar_t *)&ata_drvp->ad_id, sizeof (ata_drvp->ad_id));
	if (rc != DDI_PROP_SUCCESS) {
		ADBG_ERROR(("ata_prop_create failed, rc=%d\n", rc));
	}
	return (TRUE);
}


/* *********************************************************************** */
/* *********************************************************************** */
/* *********************************************************************** */

/*
 * This state machine doesn't implement the ATAPI Optional Overlap
 * feature. You need that feature to efficiently support ATAPI
 * tape drives. See the 1394-ATA Tailgate spec (D97107), Figure 24,
 * for an example of how to add the necessary additional NextActions
 * and NextStates to this FSM and the atapi_fsm, in order to support
 * the Overlap Feature.
 */


uchar_t ata_ctlr_fsm_NextAction[ATA_CTLR_NSTATES][ATA_CTLR_NFUNCS] = {
/* --------------------- next action --------------------- | - current - */
/* start0 --- start1 ---- intr ------ fini --- reset --- */
{ AC_START,   AC_START,	  AC_NADA,    AC_NADA, AC_RESET_I }, /* idle	 */
{ AC_BUSY,    AC_BUSY,	  AC_INTR,    AC_FINI, AC_RESET_A }, /* active0  */
{ AC_BUSY,    AC_BUSY,	  AC_INTR,    AC_FINI, AC_RESET_A }, /* active1  */
};

uchar_t ata_ctlr_fsm_NextState[ATA_CTLR_NSTATES][ATA_CTLR_NFUNCS] = {

/* --------------------- next state --------------------- | - current - */
/* start0 --- start1 ---- intr ------ fini --- reset --- */
{ AS_ACTIVE0, AS_ACTIVE1, AS_IDLE,    AS_IDLE, AS_IDLE	  }, /* idle    */
{ AS_ACTIVE0, AS_ACTIVE0, AS_ACTIVE0, AS_IDLE, AS_ACTIVE0 }, /* active0 */
{ AS_ACTIVE1, AS_ACTIVE1, AS_ACTIVE1, AS_IDLE, AS_ACTIVE1 }, /* active1 */
};


static int
ata_ctlr_fsm(
	uchar_t		 fsm_func,
	ata_ctl_t	*ata_ctlp,
	ata_drv_t	*ata_drvp,
	ata_pkt_t	*ata_pktp,
	int		*DoneFlgp)
{
	uchar_t	   action;
	uchar_t	   current_state;
	uchar_t	   next_state;
	int	   rc;

	current_state = ata_ctlp->ac_state;
	action = ata_ctlr_fsm_NextAction[current_state][fsm_func];
	next_state = ata_ctlr_fsm_NextState[current_state][fsm_func];

	/*
	 * Set the controller's new state
	 */
	ata_ctlp->ac_state = next_state;
	switch (action) {

	case AC_BUSY:
		return (ATA_FSM_RC_BUSY);

	case AC_NADA:
		return (ATA_FSM_RC_OKAY);

	case AC_START:
		ASSERT(ata_ctlp->ac_active_pktp == NULL);
		ASSERT(ata_ctlp->ac_active_drvp == NULL);

		ata_ctlp->ac_active_pktp = ata_pktp;
		ata_ctlp->ac_active_drvp = ata_drvp;

		rc = (*ata_pktp->ap_start)(ata_ctlp, ata_drvp, ata_pktp);

		if (rc == ATA_FSM_RC_BUSY) {
			/* the request didn't start, GHD will requeue it */
			ata_ctlp->ac_state = AS_IDLE;
			ata_ctlp->ac_active_pktp = NULL;
			ata_ctlp->ac_active_drvp = NULL;
		}
		return (rc);

	case AC_INTR:
		ASSERT(ata_ctlp->ac_active_pktp != NULL);
		ASSERT(ata_ctlp->ac_active_drvp != NULL);

		ata_drvp = ata_ctlp->ac_active_drvp;
		ata_pktp = ata_ctlp->ac_active_pktp;
		return ((*ata_pktp->ap_intr)(ata_ctlp, ata_drvp, ata_pktp));

	case AC_RESET_A: /* Reset, controller active */
		ASSERT(ata_ctlp->ac_active_pktp != NULL);
		ASSERT(ata_ctlp->ac_active_drvp != NULL);

		/* clean up the active request */
		ata_pktp = ata_ctlp->ac_active_pktp;
		ata_pktp->ap_flags |= AP_DEV_RESET | AP_BUS_RESET;

		/* halt the DMA engine */
		if (ata_pktp->ap_pciide_dma) {
			ata_pciide_dma_stop(ata_ctlp);
			(void) ata_pciide_status_clear(ata_ctlp);
		}

		/* Do a Software Reset to unwedge the bus */
		if (!ata_software_reset(ata_ctlp)) {
			return (ATA_FSM_RC_BUSY);
		}

		/* Then send a DEVICE RESET cmd to each ATAPI device */
		atapi_fsm_reset(ata_ctlp);
		return (ATA_FSM_RC_FINI);

	case AC_RESET_I: /* Reset, controller idle */
		/* Do a Software Reset to unwedge the bus */
		if (!ata_software_reset(ata_ctlp)) {
			return (ATA_FSM_RC_BUSY);
		}

		/* Then send a DEVICE RESET cmd to each ATAPI device */
		atapi_fsm_reset(ata_ctlp);
		return (ATA_FSM_RC_OKAY);

	case AC_FINI:
		break;
	}

	/*
	 * AC_FINI, check ARQ needs to be started or finished
	 */

	ASSERT(action == AC_FINI);
	ASSERT(ata_ctlp->ac_active_pktp != NULL);
	ASSERT(ata_ctlp->ac_active_drvp != NULL);

	/*
	 * The active request is done now.
	 * Disconnect the request from the controller and
	 * add it to the done queue.
	 */
	ata_drvp = ata_ctlp->ac_active_drvp;
	ata_pktp = ata_ctlp->ac_active_pktp;

	/*
	 * If ARQ pkt is done, get ptr to original pkt and wrap it up.
	 */
	if (ata_pktp == ata_ctlp->ac_arq_pktp) {
		ata_pkt_t *arq_pktp;

		ADBG_ARQ(("ata_ctlr_fsm 0x%p ARQ done\n", ata_ctlp));

		arq_pktp = ata_pktp;
		ata_pktp = ata_ctlp->ac_fault_pktp;
		ata_ctlp->ac_fault_pktp = NULL;
		if (arq_pktp->ap_flags & (AP_ERROR | AP_BUS_RESET))
			ata_pktp->ap_flags |= AP_ARQ_ERROR;
		else
			ata_pktp->ap_flags |= AP_ARQ_OKAY;
		goto all_done;
	}


#define	AP_ARQ_NEEDED	(AP_ARQ_ON_ERROR | AP_GOT_STATUS | AP_ERROR)

	/*
	 * Start ARQ pkt if necessary
	 */
	if ((ata_pktp->ap_flags & AP_ARQ_NEEDED) == AP_ARQ_NEEDED &&
	    (ata_pktp->ap_status & ATS_ERR)) {

		/* set controller state back to active */
		ata_ctlp->ac_state = current_state;

		/* try to start the ARQ pkt */
		rc = ata_start_arq(ata_ctlp, ata_drvp, ata_pktp);

		if (rc == ATA_FSM_RC_BUSY) {
			ADBG_ARQ(("ata_ctlr_fsm 0x%p ARQ BUSY\n", ata_ctlp));
			/* let the target driver handle the problem */
			ata_ctlp->ac_state = AS_IDLE;
			ata_ctlp->ac_active_pktp = NULL;
			ata_ctlp->ac_active_drvp = NULL;
			ata_ctlp->ac_fault_pktp = NULL;
			goto all_done;
		}

		ADBG_ARQ(("ata_ctlr_fsm 0x%p ARQ started\n", ata_ctlp));
		return (rc);
	}

	/*
	 * Normal completion, no error status, and not an ARQ pkt,
	 * just fall through.
	 */

all_done:

	/*
	 * wrap everything up and tie a ribbon around it
	 */
	ata_ctlp->ac_active_pktp = NULL;
	ata_ctlp->ac_active_drvp = NULL;
	if (APKT2GCMD(ata_pktp) != (gcmd_t *)0) {
		ghd_complete(&ata_ctlp->ac_ccc, APKT2GCMD(ata_pktp));
		if (DoneFlgp)
			*DoneFlgp = TRUE;
	}

	return (ATA_FSM_RC_OKAY);
}


static int
ata_start_arq(
	ata_ctl_t *ata_ctlp,
	ata_drv_t *ata_drvp,
	ata_pkt_t *ata_pktp)
{
	ata_pkt_t		*arq_pktp;
	int			 bytes;
	uint_t			 senselen;

	ADBG_ARQ(("ata_start_arq 0x%p ARQ needed\n", ata_ctlp));

	/*
	 * Determine just the size of the Request Sense Data buffer within
	 * the scsi_arq_status structure.
	 */
#define	SIZEOF_ARQ_HEADER	(sizeof (struct scsi_arq_status)	\
				- sizeof (struct scsi_extended_sense))
	senselen = ata_pktp->ap_statuslen - SIZEOF_ARQ_HEADER;
	ASSERT(senselen > 0);


	/* save ptr to original pkt */
	ata_ctlp->ac_fault_pktp = ata_pktp;

	/* switch the controller's active pkt to the ARQ pkt */
	arq_pktp = ata_ctlp->ac_arq_pktp;
	ata_ctlp->ac_active_pktp = arq_pktp;

	/* finish initializing the ARQ CDB */
	ata_ctlp->ac_arq_cdb[1] = ata_drvp->ad_lun << 4;
	ata_ctlp->ac_arq_cdb[4] = (uchar_t)senselen;

	/* finish initializing the ARQ pkt */
	arq_pktp->ap_v_addr = (caddr_t)&ata_pktp->ap_scbp->sts_sensedata;

	arq_pktp->ap_resid = senselen;
	arq_pktp->ap_flags = AP_ATAPI | AP_READ;
	arq_pktp->ap_cdb_pad =
	    ((unsigned)(ata_drvp->ad_cdb_len - arq_pktp->ap_cdb_len)) >> 1;

	bytes = min(senselen, ATAPI_MAX_BYTES_PER_DRQ);
	arq_pktp->ap_hicyl = (uchar_t)(bytes >> 8);
	arq_pktp->ap_lwcyl = (uchar_t)bytes;

	/*
	 * This packet is shared by all drives on this controller
	 * therefore we need to init the drive number on every ARQ.
	 */
	arq_pktp->ap_hd = ata_drvp->ad_drive_bits;

	/* start it up */
	return ((*arq_pktp->ap_start)(ata_ctlp, ata_drvp, arq_pktp));
}

/*
 *
 * reset the bus
 *
 */

static int
ata_reset_bus(
	ata_ctl_t *ata_ctlp)
{
	int	watchdog;
	uchar_t	drive;
	int	rc = FALSE;
	uchar_t	fsm_func;
	int	DoneFlg = FALSE;

	/*
	 * Do a Software Reset to unwedge the bus, and send
	 * ATAPI DEVICE RESET to each ATAPI drive.
	 */
	fsm_func = ATA_FSM_RESET;
	for (watchdog = ata_reset_bus_watchdog; watchdog > 0; watchdog--) {
		switch (ata_ctlr_fsm(fsm_func, ata_ctlp, NULL, NULL,
		    &DoneFlg)) {
		case ATA_FSM_RC_OKAY:
			rc = TRUE;
			goto fsm_done;

		case ATA_FSM_RC_BUSY:
			return (FALSE);

		case ATA_FSM_RC_INTR:
			fsm_func = ATA_FSM_INTR;
			rc = TRUE;
			continue;

		case ATA_FSM_RC_FINI:
			fsm_func = ATA_FSM_FINI;
			rc = TRUE;
			continue;
		}
	}
	ADBG_WARN(("ata_reset_bus: watchdog\n"));

fsm_done:

	/*
	 * Reinitialize the ATA drives
	 */
	for (drive = 0; drive < ATA_MAXTARG; drive++) {
		ata_drv_t *ata_drvp;

		if ((ata_drvp = CTL2DRV(ata_ctlp, drive, 0)) == NULL)
			continue;

		if (ATAPIDRV(ata_drvp))
			continue;

		/*
		 * Reprogram the Read/Write Multiple block factor
		 * and current geometry into the drive.
		 */
		if (!ata_disk_setup_parms(ata_ctlp, ata_drvp))
			rc = FALSE;
	}

	/* If DoneFlg is TRUE, it means that ghd_complete() function */
	/* has been already called. In this case ignore any errors and */
	/* return TRUE to the caller, otherwise return the value of rc */
	/* to the caller */
	if (DoneFlg)
		return (TRUE);
	else
		return (rc);
}


/*
 *
 * Low level routine to toggle the Software Reset bit
 *
 */

static int
ata_software_reset(
	ata_ctl_t *ata_ctlp)
{
	ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1;
	ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2;
	hrtime_t deadline;
	uint_t usecs_left;

	ADBG_TRACE(("ata_reset_bus entered\n"));

	/* disable interrupts and turn the software reset bit on */
	ddi_put8(io_hdl2, ata_ctlp->ac_devctl, (ATDC_D3 | ATDC_SRST));

	/* why 30 milliseconds, the ATA/ATAPI-4 spec says 5 usec. */
	drv_usecwait(30000);

	/* turn the software reset bit back off */
	ddi_put8(io_hdl2, ata_ctlp->ac_devctl, ATDC_D3);

	/*
	 * Wait for the controller to assert BUSY status.
	 * I don't think 300 msecs is correct. The ATA/ATAPI-4
	 * spec says 400 nsecs, (and 2 msecs if device
	 * was in sleep mode; but we don't put drives to sleep
	 * so it probably doesn't matter).
	 */
	drv_usecwait(300000);

	/*
	 * If drive 0 exists the test for completion is simple
	 */
	deadline = gethrtime() + ((hrtime_t)31 * NANOSEC);

	if (CTL2DRV(ata_ctlp, 0, 0)) {
		goto wait_for_not_busy;
	}

	ASSERT(CTL2DRV(ata_ctlp, 1, 0) != NULL);

	/*
	 * This must be a single device configuration, with drive 1
	 * only. This complicates the test for completion because
	 * issuing the software reset just caused drive 1 to
	 * deselect. With drive 1 deselected, if I just read the
	 * status register to test the BSY bit I get garbage, but
	 * I can't re-select drive 1 until I'm certain the BSY bit
	 * is de-asserted. Catch-22.
	 *
	 * In ATA/ATAPI-4, rev 15, section 9.16.2, it says to handle
	 * this situation like this:
	 */

	/* give up if the drive doesn't settle within 31 seconds */
	while (gethrtime() < deadline) {
		/*
		 * delay 10msec each time around the loop
		 */
		drv_usecwait(10000);

		/*
		 * try to select drive 1
		 */
		ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, ATDH_DRIVE1);

		ddi_put8(io_hdl1, ata_ctlp->ac_sect, 0x55);
		ddi_put8(io_hdl1, ata_ctlp->ac_sect, 0xaa);
		if (ddi_get8(io_hdl1, ata_ctlp->ac_sect) != 0xaa)
			continue;

		ddi_put8(io_hdl1, ata_ctlp->ac_count, 0x55);
		ddi_put8(io_hdl1, ata_ctlp->ac_count, 0xaa);
		if (ddi_get8(io_hdl1, ata_ctlp->ac_count) != 0xaa)
			continue;

		goto wait_for_not_busy;
	}
	return (FALSE);

wait_for_not_busy:

	/*
	 * Now wait up to 31 seconds for BUSY to clear.
	 */
	usecs_left = (deadline - gethrtime()) / 1000;
	(void) ata_wait3(io_hdl2, ata_ctlp->ac_ioaddr2, 0, ATS_BSY,
	    ATS_ERR, ATS_BSY, ATS_DF, ATS_BSY, usecs_left);

	return (TRUE);
}

/*
 *
 * DDI interrupt handler
 *
 */

static uint_t
ata_intr(
	caddr_t arg)
{
	ata_ctl_t *ata_ctlp;
	int	   one_shot = 1;

	ata_ctlp = (ata_ctl_t *)arg;

	return (ghd_intr(&ata_ctlp->ac_ccc, (void *)&one_shot));
}


/*
 *
 * GHD ccc_get_status callback
 *
 */

static int
ata_get_status(
	void *hba_handle,
	void *intr_status)
{
	ata_ctl_t *ata_ctlp = (ata_ctl_t *)hba_handle;
	uchar_t	   status;

	ADBG_TRACE(("ata_get_status entered\n"));

	/*
	 * ignore interrupts before ata_attach completes
	 */
	if (!(ata_ctlp->ac_flags & AC_ATTACHED))
		return (FALSE);

	/*
	 * can't be interrupt pending if nothing active
	 */
	switch (ata_ctlp->ac_state) {
	case AS_IDLE:
		return (FALSE);
	case AS_ACTIVE0:
	case AS_ACTIVE1:
		ASSERT(ata_ctlp->ac_active_drvp != NULL);
		ASSERT(ata_ctlp->ac_active_pktp != NULL);
		break;
	}

	/*
	 * If this is a PCI-IDE controller, check the PCI-IDE controller's
	 * interrupt status latch. But don't clear it yet.
	 *
	 * AC_BMSTATREG_PIO_BROKEN flag is used currently for
	 * CMD chips with device id 0x646. Since the interrupt bit on
	 * Bus master IDE register is not usable when in PIO mode,
	 * this chip is treated as a legacy device for interrupt
	 * indication.  The following code for CMD
	 * chips may need to be revisited when we enable support for dma.
	 *
	 * CHANGE: DMA is not disabled for these devices. BM intr bit is
	 * checked only if there was DMA used or BM intr is useable on PIO,
	 * else treat it as before - as legacy device.
	 */

	if ((ata_ctlp->ac_pciide) &&
	    ((ata_ctlp->ac_pciide_bm != FALSE) &&
	    ((ata_ctlp->ac_active_pktp->ap_pciide_dma == TRUE) ||
	    !(ata_ctlp->ac_flags & AC_BMSTATREG_PIO_BROKEN)))) {

		if (!ata_pciide_status_pending(ata_ctlp))
			return (FALSE);
	} else {
		/*
		 * Interrupts from legacy ATA/IDE controllers are
		 * edge-triggered but the dumb legacy ATA/IDE controllers
		 * and drives don't have an interrupt status bit.
		 *
		 * Use a one_shot variable to make sure we only return
		 * one status per interrupt.
		 */
		if (intr_status != NULL) {
			int *one_shot = (int *)intr_status;

			if (*one_shot == 1)
				*one_shot = 0;
			else
				return (FALSE);
		}
	}

	/* check if device is still busy */

	status = ddi_get8(ata_ctlp->ac_iohandle2, ata_ctlp->ac_altstatus);
	if (status & ATS_BSY)
		return (FALSE);
	return (TRUE);
}


/*
 *
 * get the current status and clear the IRQ
 *
 */

int
ata_get_status_clear_intr(
	ata_ctl_t *ata_ctlp,
	ata_pkt_t *ata_pktp)
{
	uchar_t	status;

	/*
	 * Here's where we clear the PCI-IDE interrupt latch. If this
	 * request used DMA mode then we also have to check and clear
	 * the DMA error latch at the same time.
	 */

	if (ata_pktp->ap_pciide_dma) {
		if (ata_pciide_status_dmacheck_clear(ata_ctlp))
			ata_pktp->ap_flags |= AP_ERROR | AP_TRAN_ERROR;
	} else if ((ata_ctlp->ac_pciide) &&
	    !(ata_ctlp->ac_flags & AC_BMSTATREG_PIO_BROKEN)) {
		/*
		 * Some requests don't use DMA mode and therefore won't
		 * set the DMA error latch, but we still have to clear
		 * the interrupt latch.
		 * Controllers with broken BM intr in PIO mode do not go
		 * through this path.
		 */
		(void) ata_pciide_status_clear(ata_ctlp);
	}

	/*
	 * this clears the drive's interrupt
	 */
	status = ddi_get8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_status);
	ADBG_TRACE(("ata_get_status_clear_intr: 0x%x\n", status));
	return (status);
}



/*
 *
 * GHD interrupt handler
 *
 */

/* ARGSUSED */
static void
ata_process_intr(
	void *hba_handle,
	void *intr_status)
{
	ata_ctl_t *ata_ctlp = (ata_ctl_t *)hba_handle;
	int	   watchdog;
	uchar_t	   fsm_func;
	int	   rc;

	ADBG_TRACE(("ata_process_intr entered\n"));

	/*
	 * process the ATA or ATAPI interrupt
	 */

	fsm_func = ATA_FSM_INTR;
	for (watchdog = ata_process_intr_watchdog; watchdog > 0; watchdog--) {
		rc =  ata_ctlr_fsm(fsm_func, ata_ctlp, NULL, NULL, NULL);

		switch (rc) {
		case ATA_FSM_RC_OKAY:
			return;

		case ATA_FSM_RC_BUSY:	/* wait for the next interrupt */
			return;

		case ATA_FSM_RC_INTR:	/* re-invoke the FSM */
			fsm_func = ATA_FSM_INTR;
			break;

		case ATA_FSM_RC_FINI:	/* move a request to done Q */
			fsm_func = ATA_FSM_FINI;
			break;
		}
	}
	ADBG_WARN(("ata_process_intr: watchdog\n"));
}



/*
 *
 * GHD ccc_hba_start callback
 *
 */

static int
ata_hba_start(
	void *hba_handle,
	gcmd_t *gcmdp)
{
	ata_ctl_t *ata_ctlp;
	ata_drv_t *ata_drvp;
	ata_pkt_t *ata_pktp;
	uchar_t	   fsm_func;
	int	   request_started;
	int	   watchdog;

	ADBG_TRACE(("ata_hba_start entered\n"));

	ata_ctlp = (ata_ctl_t *)hba_handle;

	if (ata_ctlp->ac_active_drvp != NULL) {
		ADBG_WARN(("ata_hba_start drvp not null\n"));
		return (FALSE);
	}
	if (ata_ctlp->ac_active_pktp != NULL) {
		ADBG_WARN(("ata_hba_start pktp not null\n"));
		return (FALSE);
	}

	ata_pktp = GCMD2APKT(gcmdp);
	ata_drvp = GCMD2DRV(gcmdp);

	/*
	 * which drive?
	 */
	if (ata_drvp->ad_targ == 0)
		fsm_func = ATA_FSM_START0;
	else
		fsm_func = ATA_FSM_START1;

	/*
	 * start the request
	 */
	request_started = FALSE;
	for (watchdog = ata_hba_start_watchdog; watchdog > 0; watchdog--) {
		switch (ata_ctlr_fsm(fsm_func, ata_ctlp, ata_drvp, ata_pktp,
		    NULL)) {
		case ATA_FSM_RC_OKAY:
			request_started = TRUE;
			goto fsm_done;

		case ATA_FSM_RC_BUSY:
			/* if first time, tell GHD to requeue the request */
			goto fsm_done;

		case ATA_FSM_RC_INTR:
			/*
			 * The start function polled for the next
			 * bus phase, now fake an interrupt to process
			 * the next action.
			 */
			request_started = TRUE;
			fsm_func = ATA_FSM_INTR;
			ata_drvp = NULL;
			ata_pktp = NULL;
			break;

		case ATA_FSM_RC_FINI: /* move request to the done queue */
			request_started = TRUE;
			fsm_func = ATA_FSM_FINI;
			ata_drvp = NULL;
			ata_pktp = NULL;
			break;
		}
	}
	ADBG_WARN(("ata_hba_start: watchdog\n"));

fsm_done:
	return (request_started);

}

static int
ata_check_pciide_blacklist(
	dev_info_t *dip,
	uint_t flags)
{
	ushort_t vendorid;
	ushort_t deviceid;
	pcibl_t	*blp;
	int	*propp;
	uint_t	 count;
	int	 rc;


	vendorid = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
	    DDI_PROP_DONTPASS, "vendor-id", 0);
	deviceid = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
	    DDI_PROP_DONTPASS, "device-id", 0);

	/*
	 * first check for a match in the "pci-ide-blacklist" property
	 */
	rc = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, 0,
	    "pci-ide-blacklist", &propp, &count);

	if (rc == DDI_PROP_SUCCESS) {
		count = (count * sizeof (uint_t)) / sizeof (pcibl_t);
		blp = (pcibl_t *)propp;
		while (count--) {
			/* check for matching ID */
			if ((vendorid & blp->b_vmask)
			    != (blp->b_vendorid & blp->b_vmask)) {
				blp++;
				continue;
			}
			if ((deviceid & blp->b_dmask)
			    != (blp->b_deviceid & blp->b_dmask)) {
				blp++;
				continue;
			}

			/* got a match */
			if (blp->b_flags & flags) {
				ddi_prop_free(propp);
				return (TRUE);
			} else {
				ddi_prop_free(propp);
				return (FALSE);
			}
		}
		ddi_prop_free(propp);
	}

	/*
	 * then check the built-in blacklist
	 */
	for (blp = ata_pciide_blacklist; blp->b_vendorid; blp++) {
		if ((vendorid & blp->b_vmask) != blp->b_vendorid)
			continue;
		if ((deviceid & blp->b_dmask) != blp->b_deviceid)
			continue;
		if (!(blp->b_flags & flags))
			continue;
		return (TRUE);
	}
	return (FALSE);
}

int
ata_check_drive_blacklist(
	struct ata_id *aidp,
	uint_t flags)
{
	atabl_t	*blp;

	for (blp = ata_drive_blacklist; blp->b_model != NULL; blp++) {
		if (!ata_strncmp(blp->b_model, aidp->ai_model,
		    sizeof (aidp->ai_model)))
			continue;
		if (blp->b_fw != NULL) {
			if (!ata_strncmp(blp->b_fw, aidp->ai_fw,
			    sizeof (aidp->ai_fw)))
				continue;
		}
		if (blp->b_flags & flags)
			return (TRUE);
		return (FALSE);
	}
	return (FALSE);
}

/*
 * Queue a request to perform some sort of internally
 * generated command. When this request packet reaches
 * the front of the queue (*func)() is invoked.
 *
 */

int
ata_queue_cmd(
	int	  (*func)(ata_ctl_t *, ata_drv_t *, ata_pkt_t *),
	void	  *arg,
	ata_ctl_t *ata_ctlp,
	ata_drv_t *ata_drvp,
	gtgt_t	  *gtgtp)
{
	ata_pkt_t	*ata_pktp;
	gcmd_t		*gcmdp;
	int		 rc;

	if (!(gcmdp = ghd_gcmd_alloc(gtgtp, sizeof (*ata_pktp), TRUE))) {
		ADBG_ERROR(("atapi_id_update alloc failed\n"));
		return (FALSE);
	}


	/* set the back ptr from the ata_pkt to the gcmd_t */
	ata_pktp = GCMD2APKT(gcmdp);
	ata_pktp->ap_gcmdp = gcmdp;
	ata_pktp->ap_hd = ata_drvp->ad_drive_bits;
	ata_pktp->ap_bytes_per_block = ata_drvp->ad_bytes_per_block;

	/*
	 * over-ride the default start function
	 */
	ata_pktp = GCMD2APKT(gcmdp);
	ata_pktp->ap_start = func;
	ata_pktp->ap_complete = NULL;
	ata_pktp->ap_v_addr = (caddr_t)arg;

	/*
	 * add it to the queue, when it gets to the front the
	 * ap_start function is called.
	 */
	rc = ghd_transport(&ata_ctlp->ac_ccc, gcmdp, gcmdp->cmd_gtgtp,
	    0, TRUE, NULL);

	if (rc != TRAN_ACCEPT) {
		/* this should never, ever happen */
		return (FALSE);
	}

	if (ata_pktp->ap_flags & AP_ERROR)
		return (FALSE);
	return (TRUE);
}

/*
 * Check if this drive has the "revert to defaults" bug
 * PSARC 2001/500 and 2001/xxx - check for the properties
 * ata-revert-to-defaults and atarvrt-<diskmodel> before
 * examining the blacklist.
 * <diskmodel> is made from the model number reported by Identify Drive
 * with uppercase letters converted to lowercase and all characters
 * except letters, digits, ".", "_", and "-" deleted.
 * Return value:
 *	TRUE:	enable revert to defaults
 *	FALSE:	disable revert to defaults
 *
 * NOTE: revert to power on defaults that includes reverting to MDMA
 * mode is allowed by ATA-6 & ATA-7 specs.
 * Therefore drives exhibiting this behaviour are not violating the spec.
 * Furthermore, the spec explicitly says that after the soft reset
 * host should check the current setting of the device features.
 * Correctly working BIOS would therefore reprogram either the drive
 * and/or the host controller to match transfer modes.
 * Devices with ATA_BL_NORVRT flag will be removed from
 * the ata_blacklist.
 * The default behaviour will be - no revert to power-on defaults
 * for all devices. The property is retained in case the user
 * explicitly requests revert-to-defaults before reboot.
 */

#define	ATA_REVERT_PROP_PREFIX "revert-"
#define	ATA_REVERT_PROP_GLOBAL	"ata-revert-to-defaults"
/* room for prefix + model number + terminating NUL character */
#define	PROP_BUF_SIZE	(sizeof (ATA_REVERT_PROP_PREFIX) + \
				sizeof (aidp->ai_model) + 1)
#define	PROP_LEN_MAX	(31)

static int
ata_check_revert_to_defaults(
	ata_drv_t *ata_drvp)
{
	struct ata_id	*aidp = &ata_drvp->ad_id;
	ata_ctl_t	*ata_ctlp = ata_drvp->ad_ctlp;
	char	 prop_buf[PROP_BUF_SIZE];
	int	 i, j;
	int	 propval;

	/* put prefix into the buffer */
	(void) strcpy(prop_buf, ATA_REVERT_PROP_PREFIX);
	j = strlen(prop_buf);

	/* append the model number, leaving out invalid characters */
	for (i = 0;  i < sizeof (aidp->ai_model);  ++i) {
		char c = aidp->ai_model[i];
		if (c >= 'A' && c <= 'Z')	/* uppercase -> lower */
			c = c - 'A' + 'a';
		if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9' ||
		    c == '.' || c == '_' || c == '-')
			prop_buf[j++] = c;
		if (c == '\0')
			break;
	}

	/* make sure there's a terminating NUL character */
	if (j >= PROP_LEN_MAX)
		j =  PROP_LEN_MAX;
	prop_buf[j] = '\0';

	/* look for a disk-specific "revert" property" */
	propval = ddi_getprop(DDI_DEV_T_ANY, ata_ctlp->ac_dip,
	    DDI_PROP_DONTPASS, prop_buf, -1);
	if (propval == 0)
		return (FALSE);
	else if (propval != -1)
		return (TRUE);

	/* look for a global "revert" property" */
	propval = ddi_getprop(DDI_DEV_T_ANY, ata_ctlp->ac_dip,
	    0, ATA_REVERT_PROP_GLOBAL, -1);
	if (propval == 0)
		return (FALSE);
	else if (propval != -1)
		return (TRUE);

	return (FALSE);
}

void
ata_show_transfer_mode(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp)
{
	int i;

	if (ata_ctlp->ac_pciide_bm == FALSE ||
	    ata_drvp->ad_pciide_dma != ATA_DMA_ON) {
		if (ata_cntrl_DMA_sel_msg) {
			ATAPRT((
			    "?\tATA DMA off: %s\n", ata_cntrl_DMA_sel_msg));
		} else if (ata_dev_DMA_sel_msg) {
			ATAPRT(("?\tATA DMA off: %s\n", ata_dev_DMA_sel_msg));
		}
		ATAPRT(("?\tPIO mode %d selected\n",
		    (ata_drvp->ad_id.ai_advpiomode & ATAC_ADVPIO_4_SUP) ==
		    ATAC_ADVPIO_4_SUP ? 4 : 3));
	} else {
		/* Using DMA */
		if (ata_drvp->ad_id.ai_dworddma & ATAC_MDMA_SEL_MASK) {
			/*
			 * Rely on the fact that either dwdma or udma is
			 * selected, not both.
			 */
			ATAPRT(("?\tMultiwordDMA mode %d selected\n",
			    (ata_drvp->ad_id.ai_dworddma & ATAC_MDMA_2_SEL) ==
			    ATAC_MDMA_2_SEL ? 2 :
			    (ata_drvp->ad_id.ai_dworddma & ATAC_MDMA_1_SEL) ==
			    ATAC_MDMA_1_SEL ? 1 : 0));
		} else {
			for (i = 0; i <= 6; i++) {
				if (ata_drvp->ad_id.ai_ultradma &
				    (1 << (i + 8))) {
					ATAPRT((
					    "?\tUltraDMA mode %d selected\n",
					    i));
					break;
				}
			}
		}
	}
}

/*
 * Controller-specific operation pointers.
 * Should be extended as needed - init only for now
 */
struct ata_ctl_spec_ops {
	uint_t	(*cs_init)(dev_info_t *, ushort_t, ushort_t); /* ctlr init */
};


struct ata_ctl_spec {
	ushort_t		cs_vendor_id;
	ushort_t		cs_device_id;
	struct ata_ctl_spec_ops	*cs_ops;
};

/* Sil3XXX-specific functions (init only for now) */
struct ata_ctl_spec_ops sil3xxx_ops = {
	&sil3xxx_init_controller	/* Sil3XXX cntrl initialization */
};


struct ata_ctl_spec ata_cntrls_spec[] = {
	{0x1095, 0x3114, &sil3xxx_ops},
	{0x1095, 0x3512, &sil3xxx_ops},
	{0x1095, 0x3112, &sil3xxx_ops},
	{0, 0, NULL}		/* List must end with cs_ops set to NULL */
};

/*
 * Do controller specific initialization if necessary.
 * Pick-up controller specific functions.
 */

int
ata_spec_init_controller(dev_info_t *dip)
{
	ushort_t		vendor_id;
	ushort_t		device_id;
	struct ata_ctl_spec	*ctlsp;

	vendor_id = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
	    DDI_PROP_DONTPASS, "vendor-id", 0);
	device_id = ddi_prop_get_int(DDI_DEV_T_ANY, ddi_get_parent(dip),
	    DDI_PROP_DONTPASS, "device-id", 0);

	/* Locate controller specific ops, if they exist */
	ctlsp = ata_cntrls_spec;
	while (ctlsp->cs_ops != NULL) {
		if (ctlsp->cs_vendor_id == vendor_id &&
		    ctlsp->cs_device_id == device_id)
			break;
		ctlsp++;
	}

	if (ctlsp->cs_ops != NULL) {
		if (ctlsp->cs_ops->cs_init != NULL) {
			/* Initialize controller */
			if ((*(ctlsp->cs_ops->cs_init))
			    (dip, vendor_id, device_id) != TRUE) {
				cmn_err(CE_WARN,
				    "pci%4x,%4x cntrl specific "
				    "initialization failed",
				    vendor_id, device_id);
				return (FALSE);
			}
		}
	}
	return (TRUE);
}

/*
 * this routine works like ddi_prop_get_int, except that it works on
 * a string property that contains ascii representations
 * of an integer.
 * If the property is not found, the default value is returned.
 */
static int
ata_prop_lookup_int(dev_t match_dev, dev_info_t *dip,
    uint_t flags, char *name, int defvalue)
{

	char *bufp, *cp;
	int rc = defvalue;
	int proprc;

	proprc = ddi_prop_lookup_string(match_dev, dip,
	    flags, name, &bufp);

	if (proprc == DDI_PROP_SUCCESS) {
		cp = bufp;
		rc = stoi(&cp);
		ddi_prop_free(bufp);
	} else {
		/*
		 * see if property is encoded as an int instead of string.
		 */
		rc = ddi_prop_get_int(match_dev, dip, flags, name, defvalue);
	}

	return (rc);
}

/*
 * Initialize the power management components
 */
static void
ata_init_pm(dev_info_t *dip)
{
	int		instance;
	ata_ctl_t 	*ata_ctlp;
#ifdef	ATA_USE_AUTOPM
	char		pmc_name[16];
	char		*pmc[] = {
				NULL,
				"0=Sleep (PCI D3 State)",
				"3=PowerOn (PCI D0 State)",
				NULL
			};
#endif


	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);
	ata_ctlp->ac_pm_support = 0;

	/* check PCI capabilities */
	if (!ata_is_pci(dip))
		return;

#ifdef	ATA_USE_AUTOPM
	(void) sprintf(pmc_name, "NAME=ata%d", instance);
	pmc[0] = pmc_name;

	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
	    "pm-components", pmc, 3) != DDI_PROP_SUCCESS) {
		return;
	}
#endif

	ata_ctlp->ac_pm_support = 1;
	ata_ctlp->ac_pm_level = PM_LEVEL_D0;

	ATA_BUSY_COMPONENT(dip, 0);
	if (ATA_RAISE_POWER(dip, 0, PM_LEVEL_D0) != DDI_SUCCESS) {
		(void) ddi_prop_remove(DDI_DEV_T_NONE, dip, "pm-components");
	}
	ATA_IDLE_COMPONENT(dip, 0);
}

/*
 * resume the hard drive
 */
static void
ata_resume_drive(ata_drv_t *ata_drvp)
{
	ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp;
	int drive_type;
	struct ata_id id;

	ADBG_TRACE(("ata_resume_drive entered\n"));

	drive_type = ata_drive_type(ata_drvp->ad_drive_bits,
	    ata_ctlp->ac_iohandle1, ata_ctlp->ac_ioaddr1,
	    ata_ctlp->ac_iohandle2, ata_ctlp->ac_ioaddr2,
	    &id);
	if (drive_type == ATA_DEV_NONE)
		return;

	if (!ATAPIDRV(ata_drvp)) {
		/* Reset Ultra DMA mode */
		ata_reset_dma_mode(ata_drvp);
		if (!ata_disk_setup_parms(ata_ctlp, ata_drvp))
			return;
	} else {
		(void) atapi_init_drive(ata_drvp);
		if (ata_drvp->ad_dma_mode != 0) {
			(void) atapi_reset_dma_mode(ata_drvp, FALSE);
			if (!ata_check_dma_mode(ata_drvp))
				atapi_reset_dma_mode(ata_drvp, TRUE);
			if (ata_drvp->ad_id.ai_ultradma !=
			    ata_drvp->ad_dma_mode) {
				ata_drvp->ad_pciide_dma = ATA_DMA_OFF;
			} else {
				ata_drvp->ad_pciide_dma = ATA_DMA_ON;
			}
		}
	}
	(void) ata_set_feature(ata_ctlp, ata_drvp, ATSF_DIS_REVPOD, 0);

}

/*
 * resume routine, it will be run when get the command
 * DDI_RESUME at attach(9E) from system power management
 */
static int
ata_resume(dev_info_t *dip)
{
	int		instance;
	ata_ctl_t 	*ata_ctlp;
	ddi_acc_handle_t io_hdl2;
	caddr_t		ioaddr2;

	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);

	if (!ata_ctlp->ac_pm_support)
		return (DDI_FAILURE);
	if (ata_ctlp->ac_pm_level == PM_LEVEL_D0)
		return (DDI_SUCCESS);

	ATA_BUSY_COMPONENT(dip, 0);
	if (ATA_RAISE_POWER(dip, 0, PM_LEVEL_D0) == DDI_FAILURE)
		return (DDI_FAILURE);
	ATA_IDLE_COMPONENT(dip, 0);

	/* enable interrupts from the device */
	io_hdl2 = ata_ctlp->ac_iohandle2;
	ioaddr2 = ata_ctlp->ac_ioaddr2;
	ddi_put8(io_hdl2, (uchar_t *)ioaddr2 + AT_DEVCTL, ATDC_D3);
	ata_ctlp->ac_pm_level = PM_LEVEL_D0;

	return (DDI_SUCCESS);
}

/*
 * suspend routine, it will be run when get the command
 * DDI_SUSPEND at detach(9E) from system power management
 */
static int
ata_suspend(dev_info_t *dip)
{
	int		instance;
	ata_ctl_t 	*ata_ctlp;
	ddi_acc_handle_t io_hdl2;

	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);

	if (!ata_ctlp->ac_pm_support)
		return (DDI_FAILURE);
	if (ata_ctlp->ac_pm_level == PM_LEVEL_D3)
		return (DDI_SUCCESS);

	/* disable interrupts and turn the software reset bit on */
	io_hdl2 = ata_ctlp->ac_iohandle2;
	ddi_put8(io_hdl2, ata_ctlp->ac_devctl, (ATDC_D3 | ATDC_SRST));

	(void) ata_reset_bus(ata_ctlp);
	(void) ata_change_power(dip, ATC_SLEEP);
	ata_ctlp->ac_pm_level = PM_LEVEL_D3;
	return (DDI_SUCCESS);
}

int ata_save_pci_config = 0;
/*
 * ata specific power management entry point, it was
 * used to change the power management component
 */
static int
ata_power(dev_info_t *dip, int component, int level)
{
	int		instance;
	ata_ctl_t 	*ata_ctlp;
	uint8_t		cmd;

	ADBG_TRACE(("ata_power entered, component = %d, level = %d\n",
	    component, level));

	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);
	if (ata_ctlp == NULL || component != 0)
		return (DDI_FAILURE);

	if (!ata_ctlp->ac_pm_support)
		return (DDI_FAILURE);

	if (ata_ctlp->ac_pm_level == level)
		return (DDI_SUCCESS);

	switch (level) {
	case PM_LEVEL_D0:
		if (ata_save_pci_config)
			(void) pci_restore_config_regs(dip);
		ata_ctlp->ac_pm_level = PM_LEVEL_D0;
		cmd = ATC_IDLE_IMMED;
		break;
	case PM_LEVEL_D3:
		if (ata_save_pci_config)
			(void) pci_save_config_regs(dip);
		ata_ctlp->ac_pm_level = PM_LEVEL_D3;
		cmd = ATC_SLEEP;
		break;
	default:
		return (DDI_FAILURE);
	}
	return (ata_change_power(dip, cmd));
}

/*
 * sent commands to ata controller to change the power level
 */
static int
ata_change_power(dev_info_t *dip, uint8_t cmd)
{
	int		instance;
	ata_ctl_t	*ata_ctlp;
	ata_drv_t	*ata_drvp;
	uchar_t		targ;
	struct ata_id	id;
	uchar_t		lun;
	uchar_t		lastlun;
	struct ata_id	*aidp;

	ADBG_TRACE(("ata_change_power entered, cmd = %d\n", cmd));

	instance = ddi_get_instance(dip);
	ata_ctlp = ddi_get_soft_state(ata_state, instance);

	/*
	 * Issue command on each disk device on the bus.
	 */
	if (cmd == ATC_SLEEP) {
		for (targ = 0; targ < ATA_MAXTARG; targ++) {
			ata_drvp = CTL2DRV(ata_ctlp, targ, 0);
			if (ata_drvp == NULL)
				continue;
			if (ata_drvp->ad_dma_cap == 0 &&
			    ata_drvp->ad_pciide_dma == ATA_DMA_ON) {
				aidp = &ata_drvp->ad_id;
				if ((aidp->ai_validinfo & ATAC_VALIDINFO_83) &&
				    (aidp->ai_ultradma & ATAC_UDMA_SEL_MASK)) {
					ata_drvp->ad_dma_cap =
					    ATA_DMA_ULTRAMODE;
					ata_drvp->ad_dma_mode =
					    aidp->ai_ultradma;
				} else if (aidp->ai_dworddma &
				    ATAC_MDMA_SEL_MASK) {
					ata_drvp->ad_dma_cap =
					    ATA_DMA_MWORDMODE;
					ata_drvp->ad_dma_mode =
					    aidp->ai_dworddma;
				}
			}
			if (ata_drive_type(ata_drvp->ad_drive_bits,
			    ata_ctlp->ac_iohandle1, ata_ctlp->ac_ioaddr1,
			    ata_ctlp->ac_iohandle2, ata_ctlp->ac_ioaddr2,
			    &id) != ATA_DEV_DISK)
				continue;
			(void) ata_flush_cache(ata_ctlp, ata_drvp);
			if (!ata_command(ata_ctlp, ata_drvp, TRUE, TRUE,
			    5 * 1000000, cmd, 0, 0, 0, 0, 0, 0)) {
				cmn_err(CE_WARN, "!ata_controller - Can not "
				    "put drive %d in to power mode %u",
				    targ, cmd);
				(void) ata_devo_reset(dip, DDI_RESET_FORCE);
				return (DDI_FAILURE);
			}
		}
		return (DDI_SUCCESS);
	}

	(void) ata_software_reset(ata_ctlp);
	for (targ = 0; targ < ATA_MAXTARG; targ++) {
		ata_drvp = CTL2DRV(ata_ctlp, targ, 0);
		if (ata_drvp == NULL)
			continue;
		ata_resume_drive(ata_drvp);

		if (ATAPIDRV(ata_drvp))
			lastlun = ata_drvp->ad_id.ai_lastlun;
		else
			lastlun = 0;
		if (!ata_enable_atapi_luns)
			lastlun = 0;
		for (lun = 1; lun <= lastlun && lun < ATA_MAXLUN; lun++) {
			ata_drvp = CTL2DRV(ata_ctlp, targ, lun);
			if (ata_drvp != NULL)
				ata_resume_drive(ata_drvp);
		}
	}

	return (DDI_SUCCESS);
}

/*
 * return 1 when ata controller is a pci device,
 * otherwise return 0
 */
static int
ata_is_pci(dev_info_t *dip)
{
	int rc;
	char *bufp;
	int ispci;

	rc = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(dip),
	    DDI_PROP_DONTPASS, "device_type", &bufp);

	if (rc != DDI_PROP_SUCCESS) {
		ADBG_ERROR(("ata_is_pci !device_type\n"));
		return (0);
	}

	ispci = (strcmp(bufp, "pci-ide") == 0);

	ddi_prop_free(bufp);

	return (ispci);
}

/*
 * Disable DMA for this drive
 */
static void
ata_disable_DMA(ata_drv_t *ata_drvp)
{
	struct ata_id *aidp;
	char buf[sizeof (aidp->ai_model) +2];
	int i;

	if (ata_drvp == NULL)
		return;

	if (ata_drvp->ad_pciide_dma == ATA_DMA_OFF)
		return;

	ata_drvp->ad_pciide_dma = ATA_DMA_OFF;

	/* Print the message */
	buf[0] = '\0';
	aidp = &ata_drvp->ad_id;
	if (aidp != NULL) {
		(void) strncpy(buf, aidp->ai_model, sizeof (aidp->ai_model));
		buf[sizeof (aidp->ai_model) -1] = '\0';
		for (i = sizeof (aidp->ai_model) - 2; buf[i] == ' '; i--)
			buf[i] = '\0';
	}
	cmn_err(CE_CONT,
	    "?DMA disabled on %s target=%d, lun=%d due to DMA errors,",
	    buf, ata_drvp->ad_targ, ata_drvp->ad_lun);
	cmn_err(CE_CONT, "?most likely due to the CF-to-IDE adapter.");
}

/*
 * Check and select DMA mode
 *
 * TRUE is returned when set feature is called successfully,
 * otherwise return FALSE
 */
int
ata_set_dma_mode(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp)
{
	struct ata_id *aidp;
	int mode, rval = FALSE;
	uint8_t subcmd;

	aidp = &ata_drvp->ad_id;

	/* Return directly if DMA is not supported */
	if (!(aidp->ai_cap & ATAC_DMA_SUPPORT))
		return (rval);

	/* Return if DMA mode is already selected */
	if (((aidp->ai_validinfo & ATAC_VALIDINFO_83) &&
	    (aidp->ai_ultradma & ATAC_UDMA_SEL_MASK)) ||
	    (aidp->ai_dworddma & ATAC_MDMA_SEL_MASK))
		return (rval);

	/* First check Ultra DMA mode if no DMA is selected */
	if ((aidp->ai_validinfo & ATAC_VALIDINFO_83) &&
	    (aidp->ai_ultradma & ATAC_UDMA_SUP_MASK)) {
		for (mode = 6; mode >= 0; --mode) {
			if (aidp->ai_ultradma & (1 << mode))
				break;
		}
		subcmd = ATF_XFRMOD_UDMA;

	} else if (aidp->ai_dworddma & ATAC_MDMA_SUP_MASK) {
		/* Then check multi-word DMA mode */
		for (mode = 2; mode >= 0; --mode) {
			if (aidp->ai_dworddma & (1 << mode))
				break;
		}
		subcmd = ATF_XFRMOD_MDMA;

	} else {
		return (rval);
	}

	rval = ata_set_feature(ata_ctlp, ata_drvp, ATSF_SET_XFRMOD,
	    subcmd|mode);

	return (rval);
}

/*
 * Reset Ultra DMA mode / MWDMA mode
 */
void
ata_reset_dma_mode(ata_drv_t *ata_drvp)
{
	uint8_t	subcmd;
	int	mode;
	ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp;

	switch (ata_drvp->ad_dma_cap) {
	case ATA_DMA_ULTRAMODE:
		subcmd = ATF_XFRMOD_UDMA;
		for (mode = 0; mode <= 6; mode++) {
			if (ata_drvp->ad_dma_mode & (1 << (mode + 8)))
				break;
		}
		break;
	case ATA_DMA_MWORDMODE:
		subcmd = ATF_XFRMOD_MDMA;
		mode = ((ata_drvp->ad_dma_mode & ATAC_MDMA_2_SEL) ==
		    ATAC_MDMA_2_SEL ? 2 :
		    (ata_drvp->ad_dma_mode & ATAC_MDMA_1_SEL) ==
		    ATAC_MDMA_1_SEL ? 1 : 0);
		break;
	default:
		return;
	}

	(void) ata_set_feature(ata_ctlp, ata_drvp, ATSF_SET_XFRMOD,
	    (subcmd | mode));
}

/*
 * Check DMA mode is the same with saved info
 * return value: 0 - not same
 *		 1 - same
 */
static int
ata_check_dma_mode(ata_drv_t *ata_drvp)
{
	struct ata_id	*aidp;

	aidp = &ata_drvp->ad_id;
	switch (ata_drvp->ad_dma_cap) {
	case ATA_DMA_ULTRAMODE:
		if ((aidp->ai_validinfo & ATAC_VALIDINFO_83) &&
		    (aidp->ai_ultradma & ATAC_UDMA_SEL_MASK) &&
		    (aidp->ai_ultradma == ata_drvp->ad_dma_mode))
			break;
		else
			return (0);
	case ATA_DMA_MWORDMODE:
		if ((aidp->ai_dworddma & ATAC_MDMA_SEL_MASK) &&
		    (aidp->ai_dworddma == ata_drvp->ad_dma_mode))
			break;
		else
			return (0);
	default:
		return (0);
	}
	return (1);
}