summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun/io/fd.c
blob: 7a760aa8af9ebc588f03dc6a0cd6d0e0293404dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright (c) 2016 by Delphix. All rights reserved.
 * Copyright 2019 Peter Tribble.
 */


/*
 * Intel 82077 Floppy Disk Driver
 */

/*
 * Notes
 *
 *	0. The driver supports two flavors of hardware design:
 *		"SUNW,fdtwo"	- sun4m	- 82077 with sun4m style Auxio
 *		"fdthree"  - sun4u - 82077 with DMA
 *	   In addition it supports an apparent bug in some versions of
 *	   the 82077 controller.
 *
 *	1. The driver is mostly set up for multiple controllers, multiple
 *	drives. However- we *do* assume the use of the AUXIO register, and
 *	if we ever have > 1 fdc, we'll have to see what that means. This
 *	is all intrinsically machine specific, but there isn't much we
 *	can do about it.
 *
 *	2. The driver also is structured to deal with one drive active at
 *	a time. This is because the 82072 chip (no longer supported) was
 *	known to be buggy with respect to overlapped seeks.
 *
 *	3. The high level interrupt code is in assembler, and runs in a
 *	sparc trap window. It acts as a pseudo-dma engine as well as
 *	handles a couple of other interrupts. When it gets its job done,
 *	it schedules a second stage interrupt (soft interrupt) which
 *	is then fielded here in fd_lointr.  When DMA is used, the fdintr_dma
 *	interrupt handler is used.
 *
 *	4. Nearly all locking is done on a lower level MUTEX_DRIVER
 *	mutex. The locking is quite conservative, and is generally
 *	established very close to any of the entries into the driver.
 *	There is nearly no locking done of the high level MUTEX_DRIVER
 *	mutex (which generally is a SPIN mutex because the floppy usually
 *	interrupts above LOCK_LEVEL). The assembler high level interrupt
 *	handler grabs the high level mutex, but the code in the driver
 *	here is especially structured to not need to do this.
 *
 *	5. Fdrawioctl commands that pass data are not optimized for
 *	speed. If they need to be faster, the driver structure will
 *	have to be redone such that fdrawioctl calls physio after
 *	cons'ing up a uio structure and that fdstart will be able
 *	to detect that a particular buffer is a 'special' buffer.
 *
 *	6. Removable media support is not complete.
 *
 */

#include <sys/param.h>
#include <sys/buf.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <sys/open.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/cmn_err.h>
#include <sys/debug.h>
#include <sys/kmem.h>
#include <sys/stat.h>
#include <sys/autoconf.h>

#include <sys/dklabel.h>

#include <sys/vtoc.h>
#include <sys/dkio.h>
#include <sys/fdio.h>

#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/kstat.h>

/*
 * included to check for ELC or SLC which report floppy controller that
 */
#include <sys/cpu.h>

#include "sys/fdvar.h"
#include "sys/fdreg.h"
#include "sys/dma_i8237A.h"

/*
 * Defines
 */
#define	KIOSP	KSTAT_IO_PTR(un->un_iostat)
#define	KIOIP	KSTAT_INTR_PTR(fdc->c_intrstat)
#define	MEDIUM_DENSITY	0x40
#define	SEC_SIZE_CODE	(fdctlr.c_csb->csb_unit]->un_chars->medium ? 3 : 2)
#define	CMD_READ	(MT + SK + FDRAW_RDCMD + MFM)
#define	CMD_WRITE	(MT + FDRAW_WRCMD + MFM)
#define	C		CE_CONT
#define	FD_POLLABLE_PROP	"pollable"	/* prom property */
#define	FD_MANUAL_EJECT		"manual"	/* prom property */
#define	FD_UNIT			"unit"		/* prom property */

/*
 * Sony MP-F17W-50D Drive Parameters
 *				High Capacity
 *	Capacity unformatted	2Mb
 *	Capacity formatted	1.47Mb
 *	Encoding method	 MFM
 *	Recording density	17434 bpi
 *	Track density		135 tpi
 *	Cylinders		80
 *	Heads			2
 *	Tracks			160
 *	Rotational speed	300 rpm
 *	Transfer rate		250/500 kbps
 *	Latency (average)	100 ms
 *	Access time
 *		Average		95 ms
 *		Track to track	3 ms
 *	Head settling time	15 ms
 *	Motor start time	500 ms
 *	Head load time		? ms
 */

/*
 * The max_fd_dma_len is used only when southbridge is present.
 * It has been observed that when IFB tests are run the floppy dma could get
 * starved and result in underrun errors. After experimenting it was found that
 * doing dma in chunks of 2048 works OK.
 * The reason for making this a global variable is that there could be
 * situations under which the customer would like to get full performance
 * from floppy. They may not be having IFB boards that cause underrun errors.
 * Under those conditions we could set this value to a much higher value
 * by editing /etc/system file.
 */
int	max_fd_dma_len = 2048;

static void quiesce_fd_interrupt(struct fdctlr *);

/*
 * Character/block entry points function prototypes
 */
static int fd_open(dev_t *, int, int, cred_t *);
static int fd_close(dev_t, int, int, cred_t *);
static int fd_strategy(struct buf *);
static int fd_read(dev_t, struct uio *, cred_t *);
static int fd_write(dev_t, struct uio *, cred_t *);
static int fd_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
static int
fd_prop_op(dev_t, dev_info_t *, ddi_prop_op_t, int, char *, caddr_t, int *);

/*
 * Device operations (dev_ops) entries function prototypes
 */
static int fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
		void **result);
static int fd_attach(dev_info_t *, ddi_attach_cmd_t);
static int fd_detach(dev_info_t *, ddi_detach_cmd_t);
static int fd_power(dev_info_t *dip, int component, int level);

/*
 * Internal functions
 */
static int fd_attach_check_drive(struct fdctlr *fdc);
static int fd_attach_det_ctlr(dev_info_t *dip, struct fdctlr *fdc);
static int fd_attach_map_regs(dev_info_t *dip, struct fdctlr *fdc);
static int fd_attach_register_interrupts(dev_info_t *dip, struct fdctlr *fdc,
    int *hard);
static int fd_build_label_vtoc(struct fdunit *, struct vtoc *);
static void fd_build_user_vtoc(struct fdunit *, struct vtoc *);
static int fdcheckdisk(struct fdctlr *fdc, int unit);
static int fd_check_media(dev_t dev, enum dkio_state state);
static void fd_cleanup(dev_info_t *dip, struct fdctlr *fdc, int hard,
    int locks);
static void fdeject(struct fdctlr *, int unit);
static int fdexec(struct fdctlr *fdc, int flags);
static void fdexec_turn_on_motor(struct fdctlr *fdc, int flags, uint_t unit);
static int fdformat(struct fdctlr *fdc, int unit, int cyl, int hd);
static caddr_t fd_getauxiova();
static struct fdctlr *fd_getctlr(dev_t);
static void fdgetcsb(struct fdctlr *);
static int fdgetlabel(struct fdctlr *fdc, int unit);
enum dkio_state fd_get_media_state(struct fdctlr *, int);
static uint_t fdintr_dma();
static int fd_isauxiodip(dev_info_t *);
static uint_t  fd_lointr(caddr_t arg);
static void fd_media_watch(void *);
static void fdmotoff(void *);
static int fd_part_is_open(struct fdunit *un, int part);
static int fdrawioctl(struct fdctlr *, int, intptr_t, int);
static int fdrecalseek(struct fdctlr *fdc, int unit, int arg, int execflg);
static int fdrecover(struct fdctlr *);
static void fdretcsb(struct fdctlr *);
static int fdreset(struct fdctlr *);
static int fdrw(struct fdctlr *fdc, int, int, int, int, int, caddr_t, uint_t);
static void fdselect(struct fdctlr *fdc, int unit, int onoff);
static int fdsensedrv(struct fdctlr *fdc, int unit);
static int fdsense_chng(struct fdctlr *, int unit);
static void fdstart(struct fdctlr *);
static int fdstart_dma(register struct fdctlr *fdc, caddr_t addr, uint_t len);
static int fd_unit_is_open(struct fdunit *);
static void fdunpacklabel(struct packed_label *, struct dk_label *);
static int fd_unbind_handle(struct fdctlr *);
static void fdwatch(void *);
static void set_rotational_speed(struct fdctlr *, int);
static int fd_get_media_info(struct fdunit *un, caddr_t buf, int flag);
static int fd_pm_lower_power(struct fdctlr *fdc);
static int fd_pm_raise_power(struct fdctlr *fdc);
static void create_pm_components(dev_info_t *dip);
static void set_data_count_register(struct fdctlr *fdc, uint32_t count);
static uint32_t get_data_count_register(struct fdctlr *fdc);
static void reset_dma_controller(struct fdctlr *fdc);
static void set_data_address_register(struct fdctlr *fdc, uint32_t address);
static uint32_t get_dma_control_register(struct fdctlr *fdc);
static void set_dma_mode(struct fdctlr *fdc, int val);
static void set_dma_control_register(struct fdctlr *fdc, uint32_t val);
static void release_sb_dma(struct fdctlr *fdc);

/*
 * External functions
 */
extern uint_t fd_intr(caddr_t);	/* defined in fd_asm.s */
extern void set_auxioreg();
extern void call_debug();



/*
 * The following macro checks whether the device in a SUSPENDED state.
 * As per WDD guide lines the I/O requests to a suspended device should
 * be blocked until the device is resumed.
 * Here we cv_wait on c_suspend_cv, and there is a cv_broadcast() in
 * DDI_RESUME to wake up this thread.
 *
 * NOTE: This code is not tested because the kernel threads are suspended
 * before the device is suspended. So there can not be any I/O requests on
 * a suspended device until the cpr implementation changes..
 */

#define	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc)	\
		{\
			while (fdc->c_un->un_state == FD_STATE_SUSPENDED) {\
				cv_wait(&fdc->c_suspend_cv, \
							&fdc->c_lolock);\
			}\
		}

/*
 * bss (uninitialized data)
 */
struct	fdctlr	*fdctlrs;	/* linked list of controllers */

/*
 * initialized data
 */

static int fd_check_media_time = 5000000;	/* 5 second state check */
static int fd_pollable = 0;
static uchar_t rwretry = 10;
static uchar_t skretry = 5;
/* This variable allows the dynamic change of the burst size */
static int fd_burstsize = DCSR_BURST_0 | DCSR_BURST_1;

static struct driver_minor_data {
	char	*name;
	int	minor;
	int	type;
} fd_minor [] = {
	{ "a", 0, S_IFBLK},
	{ "b", 1, S_IFBLK},
	{ "c", 2, S_IFBLK},
	{ "a,raw", 0, S_IFCHR},
	{ "b,raw", 1, S_IFCHR},
	{ "c,raw", 2, S_IFCHR},
	{0}
};

/*
 * If the interrupt handler is invoked and no controllers expect an
 * interrupt, the kernel panics.  The following message is printed out.
 */
char *panic_msg = "fd_intr: unexpected interrupt\n";

/*
 * Specify/Configure cmd parameters
 */
static uchar_t fdspec[2] = { 0xc2, 0x33 };	/*  "specify" parameters */
static uchar_t fdconf[3] = { 0x64, 0x58, 0x00 }; /*  "configure" parameters */

/* When DMA is used, set the ND bit to 0 */
#define	SPEC_DMA_MODE	0x32

/*
 * default characteristics
 */
static struct fd_char fdtypes[] = {
	{	/* struct fd_char fdchar_1.7MB density */
		0,		/* medium */
		500,		/* transfer rate */
		80,		/* number of cylinders */
		2,		/* number of heads */
		512,		/* sector size */
		21,		/* sectors per track */
		-1,		/* (NA) # steps per data track */
	},
	{	/* struct fd_char fdchar_highdens */
		0,		/* medium */
		500,		/* transfer rate */
		80,		/* number of cylinders */
		2,		/* number of heads */
		512,		/* sector size */
		18,		/* sectors per track */
		-1,		/* (NA) # steps per data track */
	},
	{	/* struct fd_char fdchar_meddens */
		1,		/* medium */
		500,		/* transfer rate */
		77,		/* number of cylinders */
		2,		/* number of heads */
		1024,		/* sector size */
		8,		/* sectors per track */
		-1,		/* (NA) # steps per data track */
	},
	{	/* struct fd_char fdchar_lowdens  */
		0,		/* medium */
		250,		/* transfer rate */
		80,		/* number of cylinders */
		2,		/* number of heads */
		512,		/* sector size */
		9,		/* sectors per track */
		-1,		/* (NA) # steps per data track */
	}
};


static int nfdtypes = sizeof (fdtypes) / sizeof (fdtypes[0]);


/*
 * Default Label & partition maps
 */

static struct packed_label fdlbl_high_21 = {
	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 21" },
	300,				/* rotations per minute */
	80,				/* # physical cylinders */
	0,				/* alternates per cylinder */
	1,				/* interleave factor */
	80,				/* # of data cylinders */
	0,				/* # of alternate cylinders */
	2,				/* # of heads in this partition */
	21,				/* # of 512 byte sectors per track */
	{
		{ 0, 79 * 2 * 21 },	/* part 0 - all but last cyl */
		{ 79, 1 * 2 * 21 },	/* part 1 - just the last cyl */
		{ 0, 80 * 2 * 21 },	/* part 2 - "the whole thing" */
	},
	{	0,			/* version */
		"",			/* volume label */
		3,			/* no. of partitions */
		{ 0 },			/* partition hdrs, sec 2 */
		{ 0 },			/* mboot info.  unsupported */
		VTOC_SANE,		/* verify vtoc sanity */
		{ 0 },			/* reserved space */
		0,			/* timestamp */
	},
};

static struct packed_label fdlbl_high_80 = {
	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 18" },
	300,				/* rotations per minute */
	80,				/* # physical cylinders */
	0,				/* alternates per cylinder */
	1,				/* interleave factor */
	80,				/* # of data cylinders */
	0,				/* # of alternate cylinders */
	2,				/* # of heads in this partition */
	18,				/* # of 512 byte sectors per track */
	{
		{ 0, 79 * 2 * 18 },	/* part 0 - all but last cyl */
		{ 79, 1 * 2 * 18 },	/* part 1 - just the last cyl */
		{ 0, 80 * 2 * 18 },	/* part 2 - "the whole thing" */
	},
	{	0,			/* version */
		"",			/* volume label */
		3,			/* no. of partitions */
		{ 0 },			/* partition hdrs, sec 2 */
		{ 0 },			/* mboot info.  unsupported */
		VTOC_SANE,		/* verify vtoc sanity */
		{ 0 },			/* reserved space */
		0,			/* timestamp */
	},
};

/*
 * A medium density diskette has 1024 byte sectors.  The dk_label structure
 * assumes a sector is DEVBSIZE (512) bytes.
 */
static struct packed_label fdlbl_medium_80 = {
	{ "3.5\" floppy cyl 77 alt 0 hd 2 sec 8" },
	360,				/* rotations per minute */
	77,				/* # physical cylinders */
	0,				/* alternates per cylinder */
	1,				/* interleave factor */
	77,				/* # of data cylinders */
	0,				/* # of alternate cylinders */
	2,				/* # of heads in this partition */
	16,				/* # of 512 byte sectors per track */
	{
		{ 0, 76 * 2 * 8 * 2 },  /* part 0 - all but last cyl */
		{ 76, 1 * 2 * 8 * 2 },  /* part 1 - just the last cyl */
		{ 0, 77 * 2 * 8 * 2 },  /* part 2 - "the whole thing" */
	},
	{	0,			/* version */
		"",			/* volume label */
		3,			/* no. of partitions */
		{ 0 },			/* partition hdrs, sec 2 */
		{ 0 },			/* mboot info.  unsupported */
		VTOC_SANE,		/* verify vtoc sanity */
		{ 0 },			/* reserved space */
		0,			/* timestamp */
	},
};

static struct packed_label fdlbl_low_80 = {
	{ "3.5\" floppy cyl 80 alt 0 hd 2 sec 9" },
	300,				/* rotations per minute */
	80,				/* # physical cylinders */
	0,				/* alternates per cylinder */
	1,				/* interleave factor */
	80,				/* # of data cylinders */
	0,				/* # of alternate cylinders */
	2,				/* # of heads in this partition */
	9,				/* # of 512 byte sectors per track */
	{
		{ 0, 79 * 2 * 9 },	/* part 0 - all but last cyl */
		{ 79, 1 * 2 * 9 },	/* part 1 - just the last cyl */
		{ 0, 80 * 2 * 9 },	/* part 2 - "the whole thing" */
	},
	{	0,			/* version */
		"",			/* volume label */
		3,			/* no. of partitions */
		{ 0 },			/* partition hdrs, sec 2 */
		{ 0 },			/* mboot info.  unsupported */
		VTOC_SANE,		/* verify vtoc sanity */
		{ 0 },			/* reserved space */
		0,			/* timestamp */
	},
};

static struct fdcmdinfo {
	char *cmdname;		/* command name */
	uchar_t ncmdbytes;	/* number of bytes of command */
	uchar_t nrsltbytes;	/* number of bytes in result */
	uchar_t cmdtype;		/* characteristics */
} fdcmds[] = {
	"", 0, 0, 0,			/* - */
	"", 0, 0, 0,			/* - */
	"read_track", 9, 7, 1,		/* 2 */
	"specify", 3, 0, 3,		/* 3 */
	"sense_drv_status", 2, 1, 3,	/* 4 */
	"write", 9, 7, 1,		/* 5 */
	"read", 9, 7, 1,		/* 6 */
	"recalibrate", 2, 0, 2,		/* 7 */
	"sense_int_status", 1, 2, 3,	/* 8 */
	"write_del", 9, 7, 1,		/* 9 */
	"read_id", 2, 7, 2,		/* A */
	"motor_on/off", 1, 0, 4,	/* B */
	"read_del", 9, 7, 1,		/* C */
	"format_track", 10, 7, 1,	/* D */
	"dump_reg", 1, 10, 4,		/* E */
	"seek", 3, 0, 2,		/* F */
	"", 0, 0, 0,			/* - */
	"", 0, 0, 0,			/* - */
	"", 0, 0, 0,			/* - */
	"configure", 4, 0, 4,		/* 13 */
	/* relative seek */
};

static struct cb_ops fd_cb_ops = {
	fd_open,		/* open */
	fd_close,		/* close */
	fd_strategy,		/* strategy */
	nodev,			/* print */
	nodev,			/* dump */
	fd_read,		/* read */
	fd_write,		/* write */
	fd_ioctl,		/* ioctl */
	nodev,			/* devmap */
	nodev,			/* mmap */
	nodev,			/* segmap */
	nochpoll,		/* poll */
	fd_prop_op,		/* cb_prop_op */
	0,			/* streamtab  */
	D_NEW | D_MP		/* Driver compatibility flag */
};

static struct dev_ops	fd_ops = {
	DEVO_REV,		/* devo_rev, */
	0,			/* refcnt  */
	fd_info,		/* info */
	nulldev,		/* identify */
	nulldev,		/* probe */
	fd_attach,		/* attach */
	fd_detach,		/* detach */
	nodev,			/* reset */
	&fd_cb_ops,		/* driver operations */
	(struct bus_ops *)0,	/* bus operations */
	fd_power,		/* power */
	ddi_quiesce_not_supported,	/* devo_quiesce */
};


/*
 * error handling
 *
 * for debugging, set rwretry and skretry = 1
 *		set fderrlevel to 1
 *		set fderrmask  to 224  or 100644
 *
 * after debug set rwretry to 10, skretry to 5, and fderrlevel to 3
 * set fderrmask to FDEM_ALL
 * remove the define FD_DEBUG
 *
 */

static unsigned int fderrmask = (unsigned int)FDEM_ALL;
static int fderrlevel = 3;

static int tosec = 16;  /* long timeouts for sundiag for now */

/*
 * loadable module support
 */

#include <sys/modctl.h>

extern struct mod_ops mod_driverops;
static struct modldrv modldrv = {
	&mod_driverops,		/* Type of module. driver here */
	"Floppy Driver",	/* Name of the module. */
	&fd_ops,		/* Driver ops vector */
};

static struct modlinkage modlinkage = {
	MODREV_1,
	&modldrv,
	NULL
};

int
_init(void)
{
	return (mod_install(&modlinkage));
}

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

int
_fini(void)
{
	int e;

	if ((e = mod_remove(&modlinkage)) != 0)
		return (e);

	/* ddi_soft_state_fini() */
	return (0);
}

/* ARGSUSED */
static int
fd_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
	struct			fdctlr *fdc;
	struct			driver_minor_data *dmdp;
	int			instance = ddi_get_instance(dip);
	int			hard_intr_set = 0;

	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: start\n"));

	switch (cmd) {
		case DDI_ATTACH:
			break;
		case DDI_RESUME:

			if (!(fdc = fd_getctlr(instance << FDINSTSHIFT))) {
				return (DDI_FAILURE);
			}
			quiesce_fd_interrupt(fdc);
			if (fdc->c_fdtype & FDCTYPE_SB)
				if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
				    fdintr_dma, (caddr_t)0) != DDI_SUCCESS) {
				return (DDI_FAILURE);
			}

			(void) pm_raise_power(dip, 0, PM_LEVEL_ON);
			mutex_enter(&fdc->c_lolock);
			/*
			 * Wake up any thread blocked due to I/O requests
			 * while the device was suspended.
			 */
			cv_broadcast(&fdc->c_suspend_cv);
			mutex_exit(&fdc->c_lolock);
			return (DDI_SUCCESS);

		default:
			return (DDI_FAILURE);
	}


	/*
	 * Check for the pollable property
	 * A pollable floppy drive currently only exists on the
	 * Sparcstation Voyager.  This drive does not need to
	 * be turned on in order to sense whether or not a diskette
	 * is present.
	 */
	if (ddi_getprop(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, FD_POLLABLE_PROP, 0))
		fd_pollable = 1;

	fdc = kmem_zalloc(sizeof (*fdc), KM_SLEEP);
	fdc->c_dip = dip;


	fdc->c_next = fdctlrs;
	fdctlrs = fdc;

	/* Determine which type of controller is present and initialize it */
	if (fd_attach_det_ctlr(dip, fdc) == DDI_FAILURE) {
		fd_cleanup(dip, fdc, hard_intr_set, 0);
		return (DDI_FAILURE);
	}
	/* Finish mapping the device registers & setting up structures */
	if (fd_attach_map_regs(dip, fdc) == DDI_FAILURE) {
		fd_cleanup(dip, fdc, hard_intr_set, 0);
		return (DDI_FAILURE);
	}

	/*
	 * Initialize the DMA limit structures if it's being used.
	 */
	if (fdc->c_fdtype & FDCTYPE_DMA) {
		fdc->c_fd_dma_lim.dma_attr_version = DMA_ATTR_V0;
		fdc->c_fd_dma_lim.dma_attr_addr_lo = 0x00000000ull;
		fdc->c_fd_dma_lim.dma_attr_addr_hi = 0xfffffffeull;
		fdc->c_fd_dma_lim.dma_attr_count_max = 0xffffff;
		if (fdc->c_fdtype & FDCTYPE_SB) {
			fdc->c_fd_dma_lim.dma_attr_align = FD_SB_DMA_ALIGN;
		} else {
			fdc->c_fd_dma_lim.dma_attr_align = 1;
		}
		fdc->c_fd_dma_lim.dma_attr_burstsizes = 0x0;
		fdc->c_fd_dma_lim.dma_attr_minxfer = 1;
		fdc->c_fd_dma_lim.dma_attr_maxxfer = 0xffff;
		fdc->c_fd_dma_lim.dma_attr_seg = 0xffff;
		fdc->c_fd_dma_lim.dma_attr_sgllen = 1;
		fdc->c_fd_dma_lim.dma_attr_granular = 512;

		if (ddi_dma_alloc_handle(dip, &fdc->c_fd_dma_lim,
		    DDI_DMA_DONTWAIT, 0, &fdc->c_dmahandle) != DDI_SUCCESS) {
			fd_cleanup(dip, fdc, hard_intr_set, 0);
			return (DDI_FAILURE);
		}

		if (fdc->c_fdtype & FDCTYPE_SB) {
			ddi_device_acc_attr_t dev_attr;
			size_t	rlen;

			dev_attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
			dev_attr.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
			dev_attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

			if (ddi_dma_mem_alloc(fdc->c_dmahandle,
			    (size_t)(32*1024), &dev_attr, DDI_DMA_CONSISTENT,
			    DDI_DMA_SLEEP, NULL, (caddr_t *)&fdc->dma_buf,
			    &rlen, &fdc->c_dma_buf_handle) != DDI_SUCCESS) {
				fd_cleanup(dip, fdc, hard_intr_set, 0);
				return (DDI_FAILURE);
			}

		}
	}


	/* Register the interrupts */
	if (fd_attach_register_interrupts(dip, fdc,
	    &hard_intr_set) == DDI_FAILURE) {
		fd_cleanup(dip, fdc, hard_intr_set, 0);
		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fd_attach: registering interrupts failed\n"));
		return (DDI_FAILURE);
	}


	/*
	 * set initial controller/drive/disk "characteristics/geometry"
	 *
	 * NOTE:  The driver only supports one floppy drive.  The hardware
	 * only supports one drive because there is only one auxio register
	 * for one drive.
	 */
	fdc->c_un = kmem_zalloc(sizeof (struct fdunit), KM_SLEEP);
	fdc->c_un->un_chars = kmem_alloc(sizeof (struct fd_char), KM_SLEEP);
	fdc->c_un->un_iostat = kstat_create("fd", 0, "fd0", "disk",
	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
	if (fdc->c_un->un_iostat) {
		fdc->c_un->un_iostat->ks_lock = &fdc->c_lolock;
		kstat_install(fdc->c_un->un_iostat);
	}

	fdc->c_un->un_drive = kmem_zalloc(sizeof (struct fd_drive), KM_SLEEP);

	/* check for the manual eject property */
	if (ddi_getprop(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, FD_MANUAL_EJECT, 0)) {
		fdc->c_un->un_drive->fdd_ejectable = 0;
	} else {
		/* an absence of the property indicates auto eject */
		fdc->c_un->un_drive->fdd_ejectable = -1;
	}

	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: ejectable? %d\n",
	    fdc->c_un->un_drive->fdd_ejectable));

	/*
	 * Check for the drive id.  If the drive id property doesn't exist
	 * then the drive id is set to 0
	 */
	fdc->c_un->un_unit_no = ddi_getprop(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, FD_UNIT, 0);


	if (fdc->c_fdtype & FDCTYPE_SB) {
		fdc->sb_dma_channel = ddi_getprop(DDI_DEV_T_ANY, dip,
		    DDI_PROP_DONTPASS, "dma-channel", 0);
	}


	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: unit %d\n",
	    fdc->c_un->un_unit_no));

	/* Initially set the characteristics to high density */
	fdc->c_un->un_curfdtype = 1;
	*fdc->c_un->un_chars = fdtypes[fdc->c_un->un_curfdtype];
	fdunpacklabel(&fdlbl_high_80, &fdc->c_un->un_label);

	/* Make sure drive is present */
	if (fd_attach_check_drive(fdc) == DDI_FAILURE) {
		fd_cleanup(dip, fdc, hard_intr_set, 1);
		return (DDI_FAILURE);
	}

	for (dmdp = fd_minor; dmdp->name != NULL; dmdp++) {
		if (ddi_create_minor_node(dip, dmdp->name, dmdp->type,
		    (instance << FDINSTSHIFT) | dmdp->minor,
		    DDI_NT_FD, 0) == DDI_FAILURE) {
			fd_cleanup(dip, fdc, hard_intr_set, 1);
			return (DDI_FAILURE);
		}
	}

	create_pm_components(dip);

	/*
	 * Add a zero-length attribute to tell the world we support
	 * kernel ioctls (for layered drivers)
	 */
	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
	    DDI_KERNEL_IOCTL, NULL, 0);

	ddi_report_dev(dip);

	FDERRPRINT(FDEP_L1, FDEM_ATTA,
	    (C, "attached 0x%x\n", ddi_get_instance(dip)));

	return (DDI_SUCCESS);
}

/*
 * Finish mapping the registers and initializing structures
 */
static int
fd_attach_map_regs(dev_info_t *dip, struct fdctlr *fdc)
{
	ddi_device_acc_attr_t attr;

	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	/* Map the DMA registers of the platform supports DMA */
	if (fdc->c_fdtype & FDCTYPE_SB) {
		if (ddi_regs_map_setup(dip, 1, (caddr_t *)&fdc->c_dma_regs,
		    0, sizeof (struct sb_dma_reg), &attr,
		    &fdc->c_handlep_dma)) {
			return (DDI_FAILURE);
		}


	} else if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		if (ddi_regs_map_setup(dip, 1, (caddr_t *)&fdc->c_dma_regs,
		    0, sizeof (struct cheerio_dma_reg), &attr,
		    &fdc->c_handlep_dma)) {
			return (DDI_FAILURE);
		}
	}

	/* Reset the DMA engine and enable floppy interrupts */
	reset_dma_controller(fdc);
	set_dma_control_register(fdc, DCSR_INIT_BITS);

	/* Finish initializing structures associated with the device regs */
	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
	case FDCTYPE_82077:
		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "type is 82077\n"));
		/*
		 * Initialize addrs of key registers
		 */
		fdc->c_control =
		    (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_control;
		fdc->c_fifo = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_fifo;
		fdc->c_dor = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_dor;
		fdc->c_dir = (uchar_t *)&fdc->c_reg->fdc_82077_reg.fdc_dir;


		FDERRPRINT(FDEP_L1, FDEM_ATTA, ((int)C,
		    (char *)"fdattach: msr/dsr at %p\n",
		    (void *)fdc->c_control));

		/*
		 * The 82077 doesn't use the first configuration parameter
		 * so let's adjust that while we know we're an 82077.
		 */
		fdconf[0] = 0;

		quiesce_fd_interrupt(fdc);
		break;
	default:
		break;
	}

	return (0);
}

/*
 * Determine which type of floppy controller is present and
 * initialize the registers accordingly
 */
static int
fd_attach_det_ctlr(dev_info_t *dip, struct fdctlr *fdc)
{
	ddi_device_acc_attr_t attr;
	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	/* DDI_NEVERSWAP_ACC since the controller has a byte interface. */
	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_LE_ACC;
	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	FDERRPRINT(FDEP_L1, FDEM_ATTA,
	    (C, "fdattach_det_cltr: start \n"));

	/*
	 * First, map in the controller's registers
	 * The controller has an 8-bit interface, so byte
	 * swapping isn't needed
	 */

	if (ddi_regs_map_setup(dip, 0, (caddr_t *)&fdc->c_reg,
	    0, sizeof (union fdcreg),
	    &attr,
	    &fdc->c_handlep_cont)) {
		return (DDI_FAILURE);
	}

	FDERRPRINT(FDEP_L1, FDEM_ATTA,
	    (C, "fdattach_det_cltr: mapped floppy regs\n"));


	/*
	 * Set platform specific characteristics based on the device-tree
	 * node name.
	 */


	if (strcmp(ddi_get_name(dip), "SUNW,fdtwo") == 0) {
		fdc->c_fdtype |= FDCTYPE_SLAVIO;
		fdc->c_fdtype |= FDCTYPE_82077;
		fdc->c_auxiova = fd_getauxiova(dip);
		fdc->c_auxiodata = (uchar_t)(AUX_MBO4M|AUX_TC4M);
		fdc->c_auxiodata2 = (uchar_t)AUX_TC4M;
		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: slavio will be used!\n"));


/*
 * Check the binding name to identify whether it is a South bridge based
 * system or not.
 */
	} else if (strcmp(ddi_get_name(dip), "pnpALI,1533,0") == 0) {

		fdc->c_fdtype |= FDCTYPE_SB;
		fdc->c_fdtype |= FDCTYPE_82077;
		fdc->c_fdtype |= FDCTYPE_DMA;

		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: southbridge will be used!\n"));

		/*
		 * The driver assumes high density characteristics until
		 * the diskette is looked at.
		 */

		fdc->c_fdtype |= FDCTYPE_DMA8237;
		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: DMA used\n"));


	} else if (strcmp(ddi_get_name(dip), "fdthree") == 0) {

		fdc->c_fdtype |= FDCTYPE_CHEERIO;
		fdc->c_fdtype |= FDCTYPE_82077;

		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: cheerio will be used!\n"));
		/*
		 * The cheerio auxio register should be memory mapped.  The
		 * auxio register on other platforms is shared and mapped
		 * elsewhere in the kernel
		 */
		if (ddi_regs_map_setup(dip, 2, (caddr_t *)&fdc->c_auxio_reg,
		    0, sizeof (uint_t), &attr, &fdc->c_handlep_aux)) {
			return (DDI_FAILURE);
		}

		/*
		 * The driver assumes high density characteristics until
		 * the diskette is looked at.
		 */
		Set_auxio(fdc, AUX_HIGH_DENSITY);
		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: auxio register 0x%x\n",
		    *fdc->c_auxio_reg));

		fdc->c_fdtype |= FDCTYPE_DMA;
		FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_attach: DMA used\n"));

	}

	if (fdc->c_fdtype == 0) {
		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: no controller!\n"));
		return (DDI_FAILURE);
	} else {
		return (0);
	}
}


/*
 * Register the floppy interrupts
 */
static int
fd_attach_register_interrupts(dev_info_t *dip, struct fdctlr *fdc, int *hard)
{
	ddi_iblock_cookie_t  iblock_cookie_soft;
	int status;

	/*
	 * First call ddi_get_iblock_cookie() to retrieve the
	 * the interrupt block cookie so that the mutexes may
	 * be initialized before adding the interrupt.  If the
	 * mutexes are initialized after adding the interrupt, there
	 * could be a race condition.
	 */
	if (ddi_get_iblock_cookie(dip, 0, &fdc->c_block) != DDI_SUCCESS) {
		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: ddi_get_iblock_cookie failed\n"));
		return (DDI_FAILURE);

	}

	/* Initialize high level mutex */
	mutex_init(&fdc->c_hilock, NULL, MUTEX_DRIVER, fdc->c_block);

	/*
	 * Try to register fast trap handler, if unable try standard
	 * interrupt handler, else bad
	 */

	if (fdc->c_fdtype & FDCTYPE_DMA) {
		if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
		    fdintr_dma, (caddr_t)0) == DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_ATTA,
			    (C, "fdattach: standard intr\n"));

				/*
				 * When DMA is used, the low level lock
				 * is used in the hard interrupt handler.
				 */
				mutex_init(&fdc->c_lolock, NULL,
				    MUTEX_DRIVER, fdc->c_block);

				*hard = 1;
		} else {
			FDERRPRINT(FDEP_L1, FDEM_ATTA,
			    (C, "fdattach: can't add dma intr\n"));

			mutex_destroy(&fdc->c_hilock);

			return (DDI_FAILURE);
		}
	} else {
		/*
		 * Platforms that don't support DMA have both hard
		 * and soft interrupts.
		 */
		if (ddi_add_intr(dip, 0, &fdc->c_block, 0,
		    fd_intr, (caddr_t)0) == DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_ATTA,
			    (C, "fdattach: standard intr\n"));
			*hard = 1;

			/* fast traps are not enabled */
			fdc->c_fasttrap = 0;

		} else {
			FDERRPRINT(FDEP_L1, FDEM_ATTA,
			    (C, "fdattach: can't add intr\n"));

			mutex_destroy(&fdc->c_hilock);

			return (DDI_FAILURE);
		}


		/*
		 * Initialize the soft interrupt handler.  First call
		 * ddi_get_soft_iblock_cookie() so that the mutex may
		 * be initialized before the handler is added.
		 */
		status = ddi_get_soft_iblock_cookie(dip, DDI_SOFTINT_LOW,
		    &iblock_cookie_soft);


		if (status != DDI_SUCCESS) {
			mutex_destroy(&fdc->c_hilock);
			return (DDI_FAILURE);
		}

		/*
		 * Initialize low level mutex which is used in the soft
		 * interrupt handler
		 */
		mutex_init(&fdc->c_lolock, NULL, MUTEX_DRIVER,
		    iblock_cookie_soft);

		if (ddi_add_softintr(dip, DDI_SOFTINT_LOW, &fdc->c_softid,
		    NULL, NULL,
		    fd_lointr,
		    (caddr_t)fdc) != DDI_SUCCESS) {

			mutex_destroy(&fdc->c_hilock);
			mutex_destroy(&fdc->c_lolock);

			return (DDI_FAILURE);
		}
	}

	fdc->c_intrstat = kstat_create("fd", 0, "fdc0", "controller",
	    KSTAT_TYPE_INTR, 1, KSTAT_FLAG_PERSISTENT);
	if (fdc->c_intrstat) {
		fdc->c_hiintct = &KIOIP->intrs[KSTAT_INTR_HARD];
		kstat_install(fdc->c_intrstat);
	}

	/* condition variable to wait on while an io transaction occurs */
	cv_init(&fdc->c_iocv, NULL, CV_DRIVER, NULL);

	/* condition variable for the csb */
	cv_init(&fdc->c_csbcv, NULL, CV_DRIVER, NULL);

	/* condition variable for motor on waiting period */
	cv_init(&fdc->c_motoncv, NULL, CV_DRIVER, NULL);

	/* semaphore to serialize opens and closes */
	sema_init(&fdc->c_ocsem, 1, NULL, SEMA_DRIVER, NULL);

	/* condition variable to wait on suspended floppy controller. */
	cv_init(&fdc->c_suspend_cv, NULL, CV_DRIVER, NULL);

	return (0);
}

/*
 * Make sure the drive is present
 * - acquires the low level lock
 */
static int
fd_attach_check_drive(struct fdctlr *fdc)
{
	int tmp_fderrlevel;
	int unit = fdc->c_un->un_unit_no;

	FDERRPRINT(FDEP_L1, FDEM_ATTA,
	    (C, "fd_attach_check_drive\n"));


	mutex_enter(&fdc->c_lolock);
	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {

	/* insure that the eject line is reset */
	case FDCTYPE_82077:

		/*
		 * Everything but the motor enable, drive select,
		 * and reset bits are turned off.  These three
		 * bits remain as they are.
		 */
		/* LINTED */
		Set_dor(fdc, ~((MOTEN(unit))|DRVSEL|RESET), 0);

		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: Dor 0x%x\n", Dor(fdc)));

		drv_usecwait(5);
		if (unit == 0) {
			/* LINTED */
			Set_dor(fdc, RESET|DRVSEL, 1);
		} else {

			/* LINTED */
			Set_dor(fdc, DRVSEL, 0);
			/* LINTED */
			Set_dor(fdc, RESET, 1);
		}

		drv_usecwait(5);

		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: Dor 0x%x\n", Dor(fdc)));

		if (!((fdc->c_fdtype & FDCTYPE_CHEERIO) ||
		    (fdc->c_fdtype & FDCTYPE_SB))) {
			set_auxioreg(AUX_TC4M, 0);
		}
		break;
	default:
		break;
	}


	fdgetcsb(fdc);
	if (fdreset(fdc) != 0) {
		mutex_exit(&fdc->c_lolock);
		return (DDI_FAILURE);
	}


	/* check for drive present */

	tmp_fderrlevel = fderrlevel;


	fderrlevel = FDEP_LMAX;

	FDERRPRINT(FDEP_L1, FDEM_ATTA,
	    (C, "fdattach: call fdrecalseek\n"));

	/* Make sure the drive is present */
	if (fdrecalseek(fdc, unit, -1, 0) != 0) {
		timeout_id_t timeid = fdc->c_mtimeid;
		fderrlevel = tmp_fderrlevel;
		fdc->c_mtimeid = 0;
		mutex_exit(&fdc->c_lolock);


		/* Do not hold the mutex over the call to untimeout */
		if (timeid) {
			(void) untimeout(timeid);
		}

		FDERRPRINT(FDEP_L2, FDEM_ATTA,
		    (C, "fd_attach: no drive?\n"));

		return (DDI_FAILURE);
	}

	fderrlevel = tmp_fderrlevel;

	fdselect(fdc, unit, 0);    /* deselect drive zero (used in fdreset) */
	fdretcsb(fdc);
	mutex_exit(&fdc->c_lolock);

	return (0);
}

/*
 * Clean up routine used by fd_detach and fd_attach
 *
 * Note: if the soft id is non-zero, then ddi_add_softintr() completed
 * successfully.  I can not make the same assumption about the iblock_cookie
 * for the high level interrupt handler.  So, the hard parameter indicates
 * whether or not a high level interrupt handler has been added.
 *
 * If the locks parameter is nonzero, then all mutexes, semaphores and
 * condition variables will be destroyed.
 *
 * Does not assume the low level mutex is held.
 *
 */
static void
fd_cleanup(dev_info_t *dip, struct fdctlr *fdc, int hard, int locks)
{


	FDERRPRINT(FDEP_L1, FDEM_ATTA,
	    (C, "fd_cleanup instance: %d ctlr: 0x%p\n",
	    ddi_get_instance(dip), (void *)fdc));


	if (fdc == NULL) {
		return;
	}

	/*
	 * Remove interrupt handlers first before anything else
	 * is deallocated.
	 */

	/* Remove hard interrupt if one is registered */
	if (hard) {
		ddi_remove_intr(dip, (uint_t)0, fdc->c_block);
	}

	/* Remove soft interrupt if one is registered */
	if (fdc->c_softid != NULL)
		ddi_remove_softintr(fdc->c_softid);


	/* Remove timers */
	if (fdc->c_fdtype & FDCTYPE_82077) {
		if (fdc->c_mtimeid)
			(void) untimeout(fdc->c_mtimeid);
		/*
		 * Need to turn off motor (includes select/LED for South Bridge
		 * chipset) just in case it was on when timer was removed
		 */
		if (fdc->c_un != (struct fdunit *)NULL)
			fdmotoff(fdc);
	}
	if (fdc->c_timeid)
		(void) untimeout(fdc->c_timeid);


	/* Remove memory handles */
	if (fdc->c_handlep_cont)
		ddi_regs_map_free(&fdc->c_handlep_cont);

	if (fdc->c_handlep_aux)
		ddi_regs_map_free(&fdc->c_handlep_aux);

	if (fdc->c_handlep_dma)
		ddi_regs_map_free(&fdc->c_handlep_dma);

	if (fdc->c_dma_buf_handle != NULL)
		ddi_dma_mem_free(&fdc->c_dma_buf_handle);

	if (fdc->c_dmahandle != NULL)
		ddi_dma_free_handle(&fdc->c_dmahandle);


	/* Remove all minor nodes */
	ddi_remove_minor_node(dip, NULL);



	/* Remove unit structure if one exists */
	if (fdc->c_un != (struct fdunit *)NULL) {

		ASSERT(!mutex_owned(&fdc->c_lolock));

		if (fdc->c_un->un_iostat)
			kstat_delete(fdc->c_un->un_iostat);
		fdc->c_un->un_iostat = NULL;

		if (fdc->c_un->un_chars)
			kmem_free(fdc->c_un->un_chars, sizeof (struct fd_char));

		if (fdc->c_un->un_drive)
			kmem_free(fdc->c_un->un_drive,
			    sizeof (struct fd_drive));

		kmem_free((caddr_t)fdc->c_un, sizeof (struct fdunit));
	}

	if (fdc->c_intrstat) {
		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fd_cleanup: delete intrstat\n"));

		kstat_delete(fdc->c_intrstat);
	}

	fdc->c_intrstat = NULL;

	if (locks) {
		cv_destroy(&fdc->c_iocv);
		cv_destroy(&fdc->c_csbcv);
		cv_destroy(&fdc->c_motoncv);
		cv_destroy(&fdc->c_suspend_cv);
		sema_destroy(&fdc->c_ocsem);
		mutex_destroy(&fdc->c_hilock);
		mutex_destroy(&fdc->c_lolock);
	}


	fdctlrs = fdc->c_next;
	kmem_free(fdc, sizeof (*fdc));


}


static int
fd_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
	int instance = ddi_get_instance(dip);
	struct fdctlr *fdc = fd_getctlr(instance << FDINSTSHIFT);
	timeout_id_t c_mtimeid;

	FDERRPRINT(FDEP_L1, FDEM_ATTA, (C, "fd_detach\n"));

	switch (cmd) {

	case DDI_DETACH:
		/*
		 * The hard parameter is set to 1.  If detach is called, then
		 * attach must have passed meaning that the high level
		 * interrupt handler was successfully added.
		 * Similarly, the locks parameter is also set to 1.
		 */
		fd_cleanup(dip, fdc, 1, 1);

		ddi_prop_remove_all(dip);

		return (DDI_SUCCESS);

	case DDI_SUSPEND:
		if (!fdc)
			return (DDI_FAILURE);


		mutex_enter(&fdc->c_lolock);
		fdgetcsb(fdc);	/* Wait for I/O to finish */
		c_mtimeid = fdc->c_mtimeid;
		fdretcsb(fdc);
		mutex_exit(&fdc->c_lolock);

		(void) untimeout(c_mtimeid);
		/*
		 * After suspend, the system could be powered off.
		 * When it is later powered on the southbridge floppy
		 * controller will tristate the interrupt line causing
		 * continuous dma interrupts.
		 * To avoid getting continuous fd interrupts we will remove the
		 * dma interrupt handler installed. We will re-install the
		 * handler when we RESUME.
		 */
		if (fdc->c_fdtype & FDCTYPE_SB)
			ddi_remove_intr(dip, 0, fdc->c_block);

		fdc->c_un->un_state = FD_STATE_SUSPENDED;

		return (DDI_SUCCESS);

	default:
		return (DDI_FAILURE);
	}
}

/* ARGSUSED */
static int
fd_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
{
	register struct fdctlr *fdc;
	register int error;

	switch (infocmd) {

	case DDI_INFO_DEVT2DEVINFO:
		if ((fdc = fd_getctlr((dev_t)arg)) == NULL) {
			error = DDI_FAILURE;
		} else {
			*result = fdc->c_dip;
			error = DDI_SUCCESS;
		}
		break;

	case DDI_INFO_DEVT2INSTANCE:
		*result = 0;
		error = DDI_SUCCESS;
		break;

	default:
		error = DDI_FAILURE;
	}
	return (error);
}

/*
 * property operation routine.  return the number of blocks for the partition
 * in question or forward the request to the property facilities.
 */
static int
fd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
    char *name, caddr_t valuep, int *lengthp)
{
	struct fdunit	*un;
	struct fdctlr	*fdc;
	uint64_t	nblocks64;

	/*
	 * Our dynamic properties are all device specific and size oriented.
	 * Requests issued under conditions where size is valid are passed
	 * to ddi_prop_op_nblocks with the size information, otherwise the
	 * request is passed to ddi_prop_op.
	 */
	if (dev == DDI_DEV_T_ANY) {
pass:		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
		    name, valuep, lengthp));
	} else {
		fdc = fd_getctlr(dev);
		if (fdc == NULL)
			goto pass;

		/* we have size if diskette opened and label read */
		un = fdc->c_un;
		if ((un == NULL) || !fd_unit_is_open(fdc->c_un))
			goto pass;

		/* get nblocks value */
		nblocks64 = (ulong_t)
		    un->un_label.dkl_map[FDPARTITION(dev)].dkl_nblk;

		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
		    name, valuep, lengthp, nblocks64));
	}
}

/* ARGSUSED3 */
static int
fd_open(dev_t *devp, int flag, int otyp, cred_t *cred_p)
{
	dev_t dev;
	int  part;
	struct fdctlr *fdc;
	struct fdunit *un;
	struct dk_map32 *dkm;
	uchar_t	pbit;
	int	err, part_is_open;
	int	unit;

	dev = *devp;
	fdc = fd_getctlr(dev);
	if ((fdc == NULL) || ((un = fdc->c_un) == NULL)) {
		return (ENXIO);
	}

	unit = fdc->c_un->un_unit_no;

	/*
	 * Serialize opens/closes
	 */

	sema_p(&fdc->c_ocsem);

	/* check partition */
	part = FDPARTITION(dev);
	pbit = 1 << part;
	dkm = &un->un_label.dkl_map[part];
	if (dkm->dkl_nblk == 0) {
		sema_v(&fdc->c_ocsem);
		return (ENXIO);
	}

	FDERRPRINT(FDEP_L1, FDEM_OPEN,
	    (C, "fdopen: ctlr %d unit %d part %d\n",
	    ddi_get_instance(fdc->c_dip), unit, part));

	FDERRPRINT(FDEP_L1, FDEM_OPEN,
	    (C, "fdopen: flag 0x%x", flag));


	/*
	 * Insure that drive is present with a recalibrate on first open.
	 */
	(void) pm_busy_component(fdc->c_dip, 0);

	mutex_enter(&fdc->c_lolock);

	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);

	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
		mutex_exit(&fdc->c_lolock);
		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
		    != DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
			    failed. \n"));

				sema_v(&fdc->c_ocsem);
				(void) pm_idle_component(fdc->c_dip, 0);
				return (EIO);
		}
		mutex_enter(&fdc->c_lolock);
	}
	if (fd_unit_is_open(un) == 0) {
		fdgetcsb(fdc);
		/*
		 * no check changed!
		 */
		err = fdrecalseek(fdc, unit, -1, 0);
		fdretcsb(fdc);
		if (err) {
			FDERRPRINT(FDEP_L3, FDEM_OPEN,
			    (C, "fd%d: drive not ready\n", 0));
			/* deselect drv on last close */
			fdselect(fdc, unit, 0);
			mutex_exit(&fdc->c_lolock);
			sema_v(&fdc->c_ocsem);
			(void) pm_idle_component(fdc->c_dip, 0);
			return (EIO);
		}
	}

	/*
	 * Check for previous exclusive open, or trying to exclusive open
	 */
	if (otyp == OTYP_LYR) {
		part_is_open = (un->un_lyropen[part] != 0);
	} else {
		part_is_open = fd_part_is_open(un, part);
	}
	if ((un->un_exclmask & pbit) || ((flag & FEXCL) && part_is_open)) {
		mutex_exit(&fdc->c_lolock);
		sema_v(&fdc->c_ocsem);
		FDERRPRINT(FDEP_L2, FDEM_OPEN, (C, "fd:just return\n"));
		(void) pm_idle_component(fdc->c_dip, 0);
		return (EBUSY);
	}

	/* don't attempt access, just return successfully */
	if (flag & (FNDELAY | FNONBLOCK)) {
		FDERRPRINT(FDEP_L2, FDEM_OPEN,
		    (C, "fd: return busy..\n"));
		goto out;
	}

	fdc->c_csb.csb_unit = (uchar_t)unit;
	if (fdgetlabel(fdc, unit)) {
		/* didn't find label (couldn't read anything) */
		FDERRPRINT(FDEP_L3, FDEM_OPEN,
		    (C,
		    "fd%d: unformatted diskette or no diskette in the drive\n",
		    0));
		if (fd_unit_is_open(un) == 0) {
			/* deselect drv on last close */
			fdselect(fdc, unit, 0);
		}

		mutex_exit(&fdc->c_lolock);
		sema_v(&fdc->c_ocsem);
		(void) pm_idle_component(fdc->c_dip, 0);
		return (EIO);
	}

	/*
	 * if opening for writing, check write protect on diskette
	 */
	if (flag & FWRITE) {
		fdgetcsb(fdc);
		err = fdsensedrv(fdc, unit) & WP_SR3;
		fdretcsb(fdc);
		if (err) {
			if (fd_unit_is_open(un) == 0)
				fdselect(fdc, unit, 0);
			mutex_exit(&fdc->c_lolock);
			sema_v(&fdc->c_ocsem);
			(void) pm_idle_component(fdc->c_dip, 0);
			return (EROFS);
		}
	}

out:
	/*
	 * mark open as having succeeded
	 */
	if (flag & FEXCL) {
		un->un_exclmask |= pbit;
	}
	if (otyp == OTYP_LYR) {
		un->un_lyropen[part]++;
	} else {
		un->un_regopen[otyp] |= pbit;
	}
	mutex_exit(&fdc->c_lolock);
	sema_v(&fdc->c_ocsem);
	(void) pm_idle_component(fdc->c_dip, 0);
	return (0);
}
/*
 * fd_part_is_open
 *	return 1 if the partition is open
 *	return 0 otherwise
 */
static int
fd_part_is_open(struct fdunit *un, int part)
{
	int i;
	for (i = 0; i < OTYPCNT - 1; i++)
		if (un->un_regopen[i] & (1 << part))
			return (1);
	return (0);
}


/* ARGSUSED */
static int
fd_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
{
	int unit, part_is_closed, part;
	register struct fdctlr *fdc;
	register struct fdunit *un;

	fdc = fd_getctlr(dev);
	if (!fdc || !(un = fdc->c_un))
		return (ENXIO);


	unit = fdc->c_un->un_unit_no;
	FDERRPRINT(FDEP_L1, FDEM_CLOS, (C, "fd_close\n"));
	part = FDPARTITION(dev);

	sema_p(&fdc->c_ocsem);
	mutex_enter(&fdc->c_lolock);

	if (otyp == OTYP_LYR) {
		un->un_lyropen[part]--;
		part_is_closed = (un->un_lyropen[part] == 0);
	} else {
		un->un_regopen[otyp] &= ~(1<<part);
		part_is_closed = 1;
	}
	if (part_is_closed)
		un->un_exclmask &= ~(1<<part);

	if (fd_unit_is_open(un) == 0) {
		/* deselect drive on last close */
		fdselect(fdc, unit, 0);
		un->un_flags &= ~FDUNIT_CHANGED;
	}
	mutex_exit(&fdc->c_lolock);
	sema_v(&fdc->c_ocsem);

	return (0);
}

/*
 * fd_strategy
 *	checks operation, hangs buf struct off fdctlr, calls fdstart
 *	if not already busy.  Note that if we call start, then the operation
 *	will already be done on return (start sleeps).
 */
static int
fd_strategy(register struct buf *bp)
{
	struct fdctlr *fdc;
	struct fdunit *un;
	uint_t	phys_blkno;
	struct dk_map32 *dkm;

	FDERRPRINT(FDEP_L1, FDEM_STRA,
	    (C, "fd_strategy: bp = 0x%p, dev = 0x%lx\n",
	    (void *)bp, bp->b_edev));
	FDERRPRINT(FDEP_L1, FDEM_STRA,
	    (C, "b_blkno=%x b_flags=%x b_count=%x\n",
	    (int)bp->b_blkno, bp->b_flags, (int)bp->b_bcount));
	fdc = fd_getctlr(bp->b_edev);
	un = fdc->c_un;
	dkm = &un->un_label.dkl_map[FDPARTITION(bp->b_edev)];

	/*
	 * If it's medium density and the block no. isn't a multiple
	 * of 1K, then return an error.
	 */
	if (un->un_chars->fdc_medium) {
		phys_blkno = (uint_t)bp->b_blkno >> 1;
		if (bp->b_blkno & 1) {
			FDERRPRINT(FDEP_L3, FDEM_STRA,
			    (C, "b_blkno=0x%lx is not 1k aligned\n",
			    (long)bp->b_blkno));
			bp->b_error = EINVAL;
			bp->b_resid = bp->b_bcount;
			bp->b_flags |= B_ERROR;
			biodone(bp);
			return (0);
		}
	} else {
		phys_blkno = (uint_t)bp->b_blkno;
	}


	/* If the block number is past the end, return an error */
	if ((phys_blkno > dkm->dkl_nblk)) {
		FDERRPRINT(FDEP_L3, FDEM_STRA,
		    (C, "fd%d: block %ld is past the end! (nblk=%d)\n",
		    0, (long)bp->b_blkno, dkm->dkl_nblk));
		bp->b_error = ENOSPC;
		bp->b_resid = bp->b_bcount;
		bp->b_flags |= B_ERROR;
		biodone(bp);
		return (0);
	}

	/* if at end of file, skip out now */
	if (phys_blkno == dkm->dkl_nblk) {
		FDERRPRINT(FDEP_L1, FDEM_STRA,
		    (C, "b_blkno is at the end!\n"));

		if ((bp->b_flags & B_READ) == 0) {
			/* a write needs to get an error! */
			bp->b_error = ENOSPC;
			bp->b_flags |= B_ERROR;

			FDERRPRINT(FDEP_L1, FDEM_STRA,
			    (C, "block is at end and this is a write\n"));

		}

		bp->b_resid = bp->b_bcount;
		biodone(bp);
		return (0);
	}

	/* if operation not a multiple of sector size, is error! */
	if (bp->b_bcount % un->un_chars->fdc_sec_size)	{
		FDERRPRINT(FDEP_L3, FDEM_STRA,
		    (C, "fd%d: requested transfer size(0x%lx) is not"
		    " multiple of sector size(0x%x)\n", 0,
		    bp->b_bcount, un->un_chars->fdc_sec_size));
		FDERRPRINT(FDEP_L3, FDEM_STRA,
		    (C, "	b_blkno=0x%lx b_flags=0x%x\n",
		    (long)bp->b_blkno, bp->b_flags));
		bp->b_error = EINVAL;
		bp->b_resid = bp->b_bcount;
		bp->b_flags |= B_ERROR;
		biodone(bp);
		return (0);

	}

	/*
	 * Put the buf request in the controller's queue, FIFO.
	 */
	bp->av_forw = 0;
	sema_p(&fdc->c_ocsem);

	(void) pm_busy_component(fdc->c_dip, 0);

	mutex_enter(&fdc->c_lolock);

	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);

	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
		mutex_exit(&fdc->c_lolock);
		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
		    != DDI_SUCCESS) {
			sema_v(&fdc->c_ocsem);
			(void) pm_idle_component(fdc->c_dip, 0);
			bp->b_error = EIO;
			bp->b_resid = bp->b_bcount;
			bp->b_flags |= B_ERROR;
			biodone(bp);
			return (0);
		} else {
			mutex_enter(&fdc->c_lolock);
		}
	}
	if (un->un_iostat) {
		kstat_waitq_enter(KIOSP);
	}
	if (fdc->c_actf)
		fdc->c_actl->av_forw = bp;
	else
		fdc->c_actf = bp;
	fdc->c_actl = bp;


	/* call fdstart to start the transfer */
	fdstart(fdc);

	mutex_exit(&fdc->c_lolock);
	sema_v(&fdc->c_ocsem);
	(void) pm_idle_component(fdc->c_dip, 0);
	return (0);
}

/* ARGSUSED2 */
static int
fd_read(dev_t dev, struct uio *uio, cred_t *cred_p)
{
	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_read\n"));
	return (physio(fd_strategy, NULL, dev, B_READ, minphys, uio));
}

/* ARGSUSED2 */
static int
fd_write(dev_t dev, struct uio *uio, cred_t *cred_p)
{
	FDERRPRINT(FDEP_L1, FDEM_RDWR, (C, "fd_write\n"));
	return (physio(fd_strategy, NULL, dev, B_WRITE, minphys, uio));
}

static void
fdmotoff(void *arg)
{
	struct fdctlr *fdc = arg;
	int unit = fdc->c_un->un_unit_no;

	mutex_enter(&fdc->c_lolock);

	/* Just return if we're about to call untimeout */
	if (fdc->c_mtimeid == 0) {
		mutex_exit(&fdc->c_lolock);
		return;
	}

	FDERRPRINT(FDEP_L1, FDEM_MOFF, (C, "fdmotoff\n"));

	fdc->c_mtimeid = 0;

	if (!(Msr(fdc) & CB) && (Dor(fdc) & (MOTEN(unit)))) {
		/* LINTED */
		Set_dor(fdc, MOTEN(unit), 0);
	}

	mutex_exit(&fdc->c_lolock);
}

/* ARGSUSED */
static int
fd_ioctl(dev_t dev, int cmd, intptr_t arg, int flag,
    cred_t *cred_p, int *rval_p)
{
	union {
		struct dk_cinfo dki;
		struct dk_geom dkg;
		struct dk_allmap32 dka;
		struct fd_char fdchar;
		struct fd_drive drvchar;
		int	temp;
	} cpy;

	struct vtoc	vtoc;
	struct fdunit *un;
	struct fdctlr *fdc;
	int unit, dkunit;
	int err = 0;
	uint_t	sec_size;
	enum dkio_state state;
	int	transfer_rate;

	FDERRPRINT(FDEP_L1, FDEM_IOCT,
	    (C, "fd_ioctl: cmd 0x%x, arg 0x%lx\n", cmd, (long)arg));

	/* The minor number should always be 0 */
	if (FDUNIT(dev) != 0)
		return (ENXIO);

	fdc = fd_getctlr(dev);
	unit = fdc->c_un->un_unit_no;
	un = fdc->c_un;
	sec_size = un->un_chars->fdc_sec_size;
	bzero(&cpy, sizeof (cpy));

	switch (cmd) {
	case DKIOCINFO:
		cpy.dki.dki_addr = 0;

		/*
		 * The meaning of the dki_slave and dki_unit fields
		 * is unclear.  The sparc floppy driver follows the same
		 * convention as sd.c in that the instance number is
		 * returned in the dki_cnum field.  The dki_slave field is
		 * ignored.
		 *
		 * The dki_cnum contains the controller instance
		 * and its value can be any positive number. Even
		 * though currently Sparc platforms only support
		 * one controller, the controller instance number
		 * can be any number since it is assigned by the
		 * system depending on the device properties.
		 */

		cpy.dki.dki_cnum = FDCTLR(dev);

		/*
		 * Sparc platforms support only one floppy drive.
		 * The device node for the controller is the same as
		 * the device node for the drive.  The x86 driver is
		 * different in that it has a node for the controller
		 * and a child node for each drive. Since Sparc supports
		 * only one drive, the unit number will always be zero.
		 */

		cpy.dki.dki_unit = FDUNIT(dev);

		/*
		 * The meaning of the dki_slave field is unclear.
		 * So, I will leave it set to 0.
		 */

		cpy.dki.dki_slave = 0;

		cpy.dki.dki_ctype = (ushort_t)-1;
		if (fdc->c_fdtype & FDCTYPE_82077)
			cpy.dki.dki_ctype = DKC_INTEL82077;
		cpy.dki.dki_flags = DKI_FMTTRK;
		cpy.dki.dki_partition = FDPARTITION(dev);
		cpy.dki.dki_maxtransfer = maxphys / DEV_BSIZE;
		if (ddi_copyout((caddr_t)&cpy.dki, (caddr_t)arg,
		    sizeof (cpy.dki), flag))
			err = EFAULT;
		break;
	case DKIOCGGEOM:
		cpy.dkg.dkg_ncyl = un->un_chars->fdc_ncyl;
		cpy.dkg.dkg_nhead = un->un_chars->fdc_nhead;
		cpy.dkg.dkg_nsect = un->un_chars->fdc_secptrack;
		cpy.dkg.dkg_intrlv = un->un_label.dkl_intrlv;
		cpy.dkg.dkg_rpm = un->un_label.dkl_rpm;
		cpy.dkg.dkg_pcyl = un->un_chars->fdc_ncyl;
		cpy.dkg.dkg_read_reinstruct =
		    (int)(cpy.dkg.dkg_nsect * cpy.dkg.dkg_rpm * 4) / 60000;
		cpy.dkg.dkg_write_reinstruct = cpy.dkg.dkg_read_reinstruct;
		if (ddi_copyout((caddr_t)&cpy.dkg, (caddr_t)arg,
		    sizeof (cpy.dkg), flag))
			err = EFAULT;
		break;
	case DKIOCSGEOM:
		FDERRPRINT(FDEP_L3, FDEM_IOCT,
		    (C, "fd_ioctl: DKIOCSGEOM not supported\n"));
		err = ENOTTY;
		break;

	/*
	 * return the map of all logical partitions
	 */
	case DKIOCGAPART:
		/*
		 * We don't have anything to do if the application is ILP32
		 * because the label map has a 32-bit format. Otherwise
		 * convert.
		 */
		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
			if (ddi_copyout(&un->un_label.dkl_map,
			    (void *)arg, sizeof (struct dk_allmap32), flag))
				err = EFAULT;
		}
#ifdef _MULTI_DATAMODEL
		else {
			struct dk_allmap dk_allmap;

			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
			for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
				dk_allmap.dka_map[dkunit].dkl_cylno =
				    un->un_label.dkl_map[dkunit].dkl_cylno;
				dk_allmap.dka_map[dkunit].dkl_nblk =
				    un->un_label.dkl_map[dkunit].dkl_nblk;
			}
			if (ddi_copyout(&dk_allmap, (void *)arg,
			    sizeof (struct dk_allmap), flag))
				err = EFAULT;
		}
#endif /* _MULTI_DATAMODEL */
		break;

	/*
	 * Set the map of all logical partitions
	 */
	case DKIOCSAPART:
		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
			if (ddi_copyin((const void *)arg, &cpy.dka,
			    sizeof (cpy.dka), flag))
				return (EFAULT);
			else {
				mutex_enter(&fdc->c_lolock);
				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
					un->un_label.dkl_map[dkunit] =
					    cpy.dka.dka_map[dkunit];
				}
				mutex_exit(&fdc->c_lolock);
			}
		}
#ifdef _MULTI_DATAMODEL
		else {
			struct dk_allmap dk_allmap;

			ASSERT((flag & DATAMODEL_MASK) == DATAMODEL_LP64);
			if (ddi_copyin((const void *)arg, &dk_allmap,
			    sizeof (dk_allmap), flag))
				return (EFAULT);
			else {
				mutex_enter(&fdc->c_lolock);
				for (dkunit = 0; dkunit < NDKMAP; dkunit++) {
					un->un_label.dkl_map[dkunit].dkl_cylno =
					    dk_allmap.dka_map[dkunit].dkl_cylno;
					un->un_label.dkl_map[dkunit].dkl_nblk =
					    dk_allmap.dka_map[dkunit].dkl_nblk;
				}
				mutex_exit(&fdc->c_lolock);
			}
		}
#endif /* _MULTI_DATAMODEL */
		break;

	case DKIOCGVTOC:
		mutex_enter(&fdc->c_lolock);

		/*
		 * Exit if the diskette has no label.
		 * Also, get the label to make sure the
		 * correct one is being used since the diskette
		 * may have changed
		 */
		if (fdgetlabel(fdc, unit)) {
			mutex_exit(&fdc->c_lolock);
			err = EINVAL;
			break;
		}

		/* Build a vtoc from the diskette's label */
		fd_build_user_vtoc(un, &vtoc);
		mutex_exit(&fdc->c_lolock);

#ifdef _MULTI_DATAMODEL
		switch (ddi_model_convert_from(flag & FMODELS)) {
		case DDI_MODEL_ILP32: {
			struct vtoc32 vtoc32;

			vtoctovtoc32(vtoc, vtoc32);
			if (ddi_copyout(&vtoc32, (void *)arg,
			    sizeof (struct vtoc32), flag))
				return (EFAULT);
			break;
		}

		case DDI_MODEL_NONE:
			if (ddi_copyout(&vtoc, (void *)arg,
			    sizeof (vtoc), flag))
				return (EFAULT);
			break;
		}
#else /* ! _MULTI_DATAMODEL */
		if (ddi_copyout(&vtoc, (void *)arg, sizeof (vtoc), flag))
			return (EFAULT);
#endif /* _MULTI_DATAMODEL */
		break;

	case DKIOCSVTOC:

#ifdef _MULTI_DATAMODEL
		switch (ddi_model_convert_from(flag & FMODELS)) {
		case DDI_MODEL_ILP32: {
			struct vtoc32 vtoc32;

			if (ddi_copyin((const void *)arg, &vtoc32,
			    sizeof (struct vtoc32), flag)) {
				return (EFAULT);
			}
			vtoc32tovtoc(vtoc32, vtoc);
			break;
		}

		case DDI_MODEL_NONE:
			if (ddi_copyin((const void *)arg, &vtoc,
			    sizeof (vtoc), flag)) {
				return (EFAULT);
			}
			break;
		}
#else /* ! _MULTI_DATAMODEL */
		if (ddi_copyin((const void *)arg, &vtoc, sizeof (vtoc), flag))
			return (EFAULT);
#endif /* _MULTI_DATAMODEL */

		mutex_enter(&fdc->c_lolock);

		/*
		 * The characteristics structure must be filled in because
		 * it helps build the vtoc.
		 */
		if ((un->un_chars->fdc_ncyl == 0) ||
		    (un->un_chars->fdc_nhead == 0) ||
		    (un->un_chars->fdc_secptrack == 0)) {
			mutex_exit(&fdc->c_lolock);
			err = EINVAL;
			break;
		}

		if ((err = fd_build_label_vtoc(un, &vtoc)) != 0) {
			mutex_exit(&fdc->c_lolock);
			break;
		}

		(void) pm_busy_component(fdc->c_dip, 0);

		err = fdrw(fdc, unit, FDWRITE, 0, 0, 1,
		    (caddr_t)&un->un_label, sizeof (struct dk_label));
		mutex_exit(&fdc->c_lolock);
		(void) pm_idle_component(fdc->c_dip, 0);
		break;

	case DKIOCSTATE:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&state,
		    sizeof (int), flag)) {
			err = EFAULT;
			break;
		}
		(void) pm_busy_component(fdc->c_dip, 0);

		err = fd_check_media(dev, state);
		(void) pm_idle_component(fdc->c_dip, 0);

		if (ddi_copyout((caddr_t)&un->un_media_state,
		    (caddr_t)arg, sizeof (int), flag))
			err = EFAULT;
		break;

	case FDIOGCHAR:
		if (ddi_copyout((caddr_t)un->un_chars, (caddr_t)arg,
		    sizeof (struct fd_char), flag))
			err = EFAULT;
		break;

	case FDIOSCHAR:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.fdchar,
				sizeof (struct fd_char), flag)) {
			err = EFAULT;
			break;
		}

		/*
		 * Check the fields in the fdchar structure that are either
		 * driver or controller dependent.
		 */

		transfer_rate = cpy.fdchar.fdc_transfer_rate;
		if ((transfer_rate != 500) && (transfer_rate != 300) &&
		    (transfer_rate != 250) && (transfer_rate != 1000)) {
			FDERRPRINT(FDEP_L3, FDEM_IOCT,
			    (C, "fd_ioctl: FDIOSCHAR odd transfer rate %d\n",
			    cpy.fdchar.fdc_transfer_rate));
			err = EINVAL;
			break;
		}

		if ((cpy.fdchar.fdc_nhead < 1) ||
		    (cpy.fdchar.fdc_nhead > 2)) {
			FDERRPRINT(FDEP_L3, FDEM_IOCT,
			    (C, "fd_ioctl: FDIOSCHAR bad no. of heads %d\n",
			    cpy.fdchar.fdc_nhead));
			err = EINVAL;
			break;
		}

		/*
		 * The number of cylinders must be between 0 and 255
		 */
		if ((cpy.fdchar.fdc_ncyl < 0) || (cpy.fdchar.fdc_ncyl > 255)) {
			FDERRPRINT(FDEP_L3, FDEM_IOCT,
			    (C, "fd_ioctl: FDIOSCHAR bad cyl no %d\n",
			    cpy.fdchar.fdc_ncyl));
			err = EINVAL;
			break;
		}

		/* Copy the fdchar structure */

		mutex_enter(&fdc->c_lolock);
		*(un->un_chars) = cpy.fdchar;

		un->un_curfdtype = -1;

		mutex_exit(&fdc->c_lolock);

		break;
	case FDEJECT:  /* eject disk */
	case DKIOCEJECT:

		/*
		 * Fail the ioctl if auto-eject isn't supported
		 */
		if (fdc->c_un->un_drive->fdd_ejectable == 0) {

			err = ENOSYS;

		} else {
			(void) pm_busy_component(fdc->c_dip, 0);

			mutex_enter(&fdc->c_lolock);

			CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);

			if (fdc->c_un->un_state == FD_STATE_STOPPED) {
				mutex_exit(&fdc->c_lolock);
				if ((pm_raise_power(fdc->c_dip, 0,
				    PM_LEVEL_ON)) != DDI_SUCCESS) {
					(void) pm_idle_component(fdc->c_dip, 0);
					err = EIO;
				}
				mutex_enter(&fdc->c_lolock);
			}
		}
		if (err == 0) {
			fdselect(fdc, unit, 1);
			fdeject(fdc, unit);
			mutex_exit(&fdc->c_lolock);
		}

		(void) pm_idle_component(fdc->c_dip, 0);

		/*
		 * Make sure the drive is turned off
		 */
		if (fdc->c_fdtype & FDCTYPE_82077) {
			if (fdc->c_mtimeid == 0) {
				fdc->c_mtimeid = timeout(fdmotoff, fdc,
				    Motoff_delay);
			}
		}

		break;
	case FDGETCHANGE: /* disk changed */

		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.temp,
		    sizeof (int), flag)) {
			err = EFAULT;
			break;
		}

		/* zero out the user's parameter */
		cpy.temp = 0;

		(void) pm_busy_component(fdc->c_dip, 0);

		mutex_enter(&fdc->c_lolock);

		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);

		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
			mutex_exit(&fdc->c_lolock);
			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
			    != DDI_SUCCESS) {
				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
				    change failed. \n"));
				(void) pm_idle_component(fdc->c_dip, 0);
				return (EIO);
			}

			mutex_enter(&fdc->c_lolock);
		}
		if (un->un_flags & FDUNIT_CHANGED)
			cpy.temp |= FDGC_HISTORY;
		else
			cpy.temp &= ~FDGC_HISTORY;
		un->un_flags &= ~FDUNIT_CHANGED;

		if (fd_pollable) {
			/*
			 * If it's a "pollable" floppy, then we don't
			 * have to do all the fdcheckdisk nastyness to
			 * figure out if the thing is still there.
			 */
			if (fdsense_chng(fdc, unit)) {
				cpy.temp |= FDGC_CURRENT;
			} else {
				cpy.temp &= ~FDGC_CURRENT;
			}
		} else {

			if (fdsense_chng(fdc, unit)) {
				/*
				 * check disk change signal is asserted.
				 * Now find out if the floppy is
				 * inserted
				 */
				if (fdcheckdisk(fdc, unit)) {
					cpy.temp |= FDGC_CURRENT;
				} else {
					/*
					 * Yes, the floppy was
					 * reinserted. Implies
					 * floppy change.
					 */
					cpy.temp &= ~FDGC_CURRENT;
					cpy.temp |= FDGC_HISTORY;
				}
			} else {
				cpy.temp &= ~FDGC_CURRENT;
			}
		}

		/*
		 * For a pollable floppy, the floppy_change signal
		 * reflects whether the floppy is in there or not.
		 * We can not detect a floppy change if we don't poll
		 * this signal when the floppy is being changed.
		 * Because as soon as the floppy is put back, the
		 * signal is reset.
		 * BUT the pollable floppies are available only on
		 * Sparcstation Voyager Voyagers (Gypsy) only and
		 * those are motorized floppies. For motorized floppies,
		 * the floppy can only (assuming the user doesn't use a
		 * pin to take out the floppy) be taken out by
		 * issuing 'eject' command which sets the
		 * un->un_ejected flag. So, if the following
		 * condition is true, we can assume there
		 * was a floppy change.
		 */
		if (un->un_ejected && !(cpy.temp & FDGC_CURRENT)) {
			cpy.temp |= FDGC_HISTORY;
		}
		un->un_ejected = 0;


		/* return the write-protection status */
		fdgetcsb(fdc);
		if (fdsensedrv(fdc, unit) & WP_SR3) {
			cpy.temp |= FDGC_CURWPROT;
		}
		fdretcsb(fdc);
		mutex_exit(&fdc->c_lolock);

		if (ddi_copyout((caddr_t)&cpy.temp, (caddr_t)arg,
		    sizeof (int), flag))
			err = EFAULT;
		(void) pm_idle_component(fdc->c_dip, 0);
		break;

	case FDGETDRIVECHAR:

		if (ddi_copyin((caddr_t)arg, (caddr_t)&cpy.drvchar,
				sizeof (struct fd_drive), flag)) {
			err = EFAULT;
			break;
		}

		/*
		 * Return the ejectable value based on the FD_MANUAL_EJECT
		 * property
		 */
		cpy.drvchar.fdd_ejectable = fdc->c_un->un_drive->fdd_ejectable;
		cpy.drvchar.fdd_maxsearch = nfdtypes; /* 3 - hi m lo density */
		if (fd_pollable)	/* pollable device */
			cpy.drvchar.fdd_flags |= FDD_POLLABLE;

		/* the rest of the fd_drive struct is meaningless to us */

		if (ddi_copyout((caddr_t)&cpy.drvchar, (caddr_t)arg,
		    sizeof (struct fd_drive), flag))
			err = EFAULT;
		break;

	case FDSETDRIVECHAR:
		FDERRPRINT(FDEP_L3, FDEM_IOCT,
		    (C, "fd_ioctl: FDSETDRIVECHAR not supportedn\n"));
		err = ENOTTY;
		break;

	case DKIOCREMOVABLE: {
		int	i = 1;

		/* no brainer: floppies are always removable */
		if (ddi_copyout((caddr_t)&i, (caddr_t)arg, sizeof (int),
		    flag)) {
			err = EFAULT;
		}
		break;
	}
	case DKIOCGMEDIAINFO:
		err = fd_get_media_info(un, (caddr_t)arg, flag);
		break;


	case FDIOCMD:
	{
		struct fd_cmd fc;
		int cyl, hd, spc, spt;
		int nblks; /* total no. of blocks */

#ifdef _MULTI_DATAMODEL
		switch (ddi_model_convert_from(flag & FMODELS)) {
		case DDI_MODEL_ILP32: {
			struct fd_cmd32 fc32;

			if (ddi_copyin((const void *)arg, &fc32,
			    sizeof (fc32), flag)) {
				return (EFAULT);
			}
			fc.fdc_cmd	= fc32.fdc_cmd;
			fc.fdc_flags	= fc32.fdc_flags;
			fc.fdc_blkno	= (daddr_t)fc32.fdc_blkno;
			fc.fdc_secnt	= fc32.fdc_secnt;
			fc.fdc_bufaddr	= (caddr_t)(uintptr_t)fc32.fdc_bufaddr;
			fc.fdc_buflen	= fc32.fdc_buflen;
			fc.fdc_cmd	= fc32.fdc_cmd;

			break;
		}

		case DDI_MODEL_NONE:
			if (ddi_copyin((const void *)arg, &fc,
			    sizeof (fc), flag)) {
				return (EFAULT);
			}
			break;
		}
#else /* ! _MULTI_DATAMODEL */
		if (ddi_copyin((const void *)arg, &fc, sizeof (fc), flag)) {
			return (EFAULT);
		}
#endif /* _MULTI_DATAMODEL */

		if (fc.fdc_cmd == FDCMD_READ || fc.fdc_cmd == FDCMD_WRITE) {
			auto struct iovec aiov;
			auto struct uio auio;
			struct uio *uio = &auio;

			spc = (fc.fdc_cmd == FDCMD_READ)? B_READ: B_WRITE;

			bzero(&auio, sizeof (struct uio));
			bzero(&aiov, sizeof (struct iovec));
			aiov.iov_base = fc.fdc_bufaddr;
			aiov.iov_len = (uint_t)fc.fdc_secnt * sec_size;
			uio->uio_iov = &aiov;

			uio->uio_iovcnt = 1;
			uio->uio_resid = aiov.iov_len;
			uio->uio_segflg = UIO_USERSPACE;
			FDERRPRINT(FDEP_L2, FDEM_IOCT,
			    (C, "fd_ioctl: call physio\n"));
			err = physio(fd_strategy, NULL, dev,
			    spc, minphys, uio);
			break;
		} else if (fc.fdc_cmd != FDCMD_FORMAT_TRACK) {

			/*
			 * The manpage states that only the FDCMD_WRITE,
			 * FDCMD_READ, and the FDCMD_FORMAT_TR are available.
			 */
			FDERRPRINT(FDEP_L1, FDEM_IOCT,
			    (C, "fd_ioctl: FDIOCMD invalid command\n"));
			err = EINVAL;
			break;
		}

		/* The command is FDCMD_FORMAT_TRACK */

		spt = un->un_chars->fdc_secptrack;	/* sec/trk */
		spc = un->un_chars->fdc_nhead * spt;	/* sec/cyl */
		cyl = fc.fdc_blkno / spc;
		hd = (fc.fdc_blkno % spc) / spt;

		/*
		 * Make sure the specified block number is in the correct
		 * range. (block numbers start at 0)
		 */
		nblks = spc * un->un_chars->fdc_ncyl;

		if (fc.fdc_blkno < 0 || fc.fdc_blkno > (nblks - 1)) {
			err = EINVAL;
			break;
		}

		(void) pm_busy_component(fdc->c_dip, 0);

		mutex_enter(&fdc->c_lolock);
		CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);
		if (fdc->c_un->un_state == FD_STATE_STOPPED) {
			mutex_exit(&fdc->c_lolock);
			if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
			    != DDI_SUCCESS) {
				FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power \
				    change failed. \n"));
				(void) pm_idle_component(fdc->c_dip, 0);
				return (EIO);
			}

			mutex_enter(&fdc->c_lolock);
		}

		if (fdformat(fdc, unit, cyl, hd))
			err = EIO;

		mutex_exit(&fdc->c_lolock);
		(void) pm_idle_component(fdc->c_dip, 0);

		break;
	}

	case FDRAW:

		(void) pm_busy_component(fdc->c_dip, 0);
		err = fdrawioctl(fdc, unit, arg, flag);

		(void) pm_idle_component(fdc->c_dip, 0);

		break;
#ifdef FD_DEBUG
	case IOCTL_DEBUG:
		fderrlevel--;
		if (fderrlevel < 0)
			fderrlevel = 3;
		cmn_err(C, "fdioctl: CHANGING debug to %d", fderrlevel);
		return (0);
#endif /* FD_DEBUG */
	default:
		FDERRPRINT(FDEP_L2, FDEM_IOCT,
		    (C, "fd_ioctl: invalid ioctl 0x%x\n", cmd));
		err = ENOTTY;
		break;
	}

	return (err);
}

/*
 * fdrawioctl
 *
 * - acquires the low level lock
 */

static int
fdrawioctl(struct fdctlr *fdc, int unit, intptr_t arg, int mode)
{
	struct fd_raw fdr;
#ifdef _MULTI_DATAMODEL
	struct fd_raw32 fdr32;
#endif
	struct fdcsb *csb;
	int i, err, flag;
	caddr_t fa;
	uint_t	fc;
	size_t	real_length;
	int	res;
	ddi_device_acc_attr_t attr;
	ddi_acc_handle_t	mem_handle;

	attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
	attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

	ASSERT(fdc->c_un->un_unit_no == unit);

	flag = B_READ;
	err = 0;
	fa = NULL;
	fc = (uint_t)0;

	/* Copy in the arguments */
	switch (ddi_model_convert_from(mode)) {
#ifdef _MULTI_DATAMODEL
	case DDI_MODEL_ILP32:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr32,
		    sizeof (fdr32), mode)) {
			FDERRPRINT(FDEP_L1, FDEM_RAWI,
			    (C, "fdrawioctl: copyin error, args32\n"));
			return (EFAULT);
		}
		bcopy(fdr32.fdr_cmd, fdr.fdr_cmd, sizeof (fdr.fdr_cmd));
		fdr.fdr_cnum = fdr32.fdr_cnum;
		bcopy(fdr32.fdr_result, fdr.fdr_result,
		    sizeof (fdr.fdr_result));
		fdr.fdr_nbytes = fdr32.fdr_nbytes;
		fdr.fdr_addr = (caddr_t)(uintptr_t)fdr32.fdr_addr;
		break;
#endif
	default:
	case DDI_MODEL_NONE:
		if (ddi_copyin((caddr_t)arg, (caddr_t)&fdr,
		    sizeof (fdr), mode)) {
			FDERRPRINT(FDEP_L1, FDEM_RAWI,
			    (C, "fdrawioctl: copyin error, args\n"));
			return (EFAULT);
		}
		break;
	}

	FDERRPRINT(FDEP_L1, FDEM_RAWI,
	    (C, "fdrawioctl: cmd[0]=0x%x\n", fdr.fdr_cmd[0]));

	mutex_enter(&fdc->c_lolock);

	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);

	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
		mutex_exit(&fdc->c_lolock);
		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
		    != DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
			    failed. \n"));

			(void) pm_idle_component(fdc->c_dip, 0);
			return (EIO);
		}
		mutex_enter(&fdc->c_lolock);
	}

	fdgetcsb(fdc);
	csb = &fdc->c_csb;
	csb->csb_unit = (uchar_t)unit;

	/* copy cmd bytes into csb */
	for (i = 0; i <= fdr.fdr_cnum; i++)
		csb->csb_cmds[i] = fdr.fdr_cmd[i];
	csb->csb_ncmds = (uchar_t)fdr.fdr_cnum;

	csb->csb_maxretry = 0;	/* let the application deal with errors */
	csb->csb_retrys = 0;

	switch (fdr.fdr_cmd[0] & 0x0f) {

	case FDRAW_SPECIFY:
		/*
		 * Ensure that the right DMA mode is selected.  There is
		 * currently no way for the user to tell if DMA is
		 * happening so set the value for the user.
		 */

		if (fdc->c_fdtype & FDCTYPE_DMA)
			csb->csb_cmds[2] = csb->csb_cmds[2] & 0xFE;
		else
			csb->csb_cmds[2] = csb->csb_cmds[2] | 0x1;

		csb->csb_opflags = CSB_OFNORESULTS;
		csb->csb_nrslts = 0;
		break;

	case FDRAW_SENSE_DRV:
		/* Insert the appropriate drive number */
		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
		csb->csb_opflags = CSB_OFIMMEDIATE;
		csb->csb_nrslts = 1;
		break;

	case FDRAW_REZERO:
	case FDRAW_SEEK:
		/* Insert the appropriate drive number */
		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
		csb->csb_opflags = CSB_OFSEEKOPS + CSB_OFTIMEIT;
		csb->csb_nrslts = 2;
		break;

	case FDRAW_FORMAT:
		FDERRPRINT(FDEP_L1, FDEM_RAWI,
		    (C, "fdrawioctl: cmd is fdfraw format\n"));

		/* Insert the appropriate drive number */
		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
		csb->csb_nrslts = NRBRW;
		flag = B_WRITE;

		/*
		 * Allocate memory for the command.
		 * If PIO is being used, then add an extra 16 bytes
		 */
		if (fdc->c_fdtype & FDCTYPE_DMA) {

			fc = (uint_t)(fdr.fdr_nbytes);
			mutex_enter(&fdc->c_hilock);

			res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
			    &attr, DDI_DMA_STREAMING,
			    DDI_DMA_DONTWAIT, 0, &fa, &real_length,
			    &mem_handle);

			if (res != DDI_SUCCESS) {
				fdretcsb(fdc);
				mutex_exit(&fdc->c_lolock);
				mutex_exit(&fdc->c_hilock);
				return (EIO);
			}

			fdc->c_csb.csb_read = CSB_WRITE;
			if (fdstart_dma(fdc, fa, fc) != 0) {
				ddi_dma_mem_free(&mem_handle);
				fdretcsb(fdc);
				mutex_exit(&fdc->c_lolock);
				mutex_exit(&fdc->c_hilock);
				return (EIO);
			}
			mutex_exit(&fdc->c_hilock);

		} else {
			fc = (uint_t)(fdr.fdr_nbytes + 16);
			fa = kmem_zalloc(fc, KM_SLEEP);
		}

		/* copy in the user's command bytes */
		if (ddi_copyin(fdr.fdr_addr, fa,
		    (uint_t)fdr.fdr_nbytes, mode)) {
			fdretcsb(fdc);
			mutex_exit(&fdc->c_lolock);

			if (fdc->c_fdtype & FDCTYPE_DMA) {
				ddi_dma_mem_free(&mem_handle);
				FDERRPRINT(FDEP_L1, FDEM_RAWI,
				    (C, "fdrawioctl: (err)free dma memory\n"));
			} else {
				kmem_free(fa, fc);
			}

			FDERRPRINT(FDEP_L1, FDEM_RAWI,
			    (C, "fdrawioctl: ddi_copyin error\n"));
			return (EFAULT);
		}

		break;
	case FDRAW_WRCMD:
	case FDRAW_WRITEDEL:
		flag = B_WRITE;
		/* FALLTHROUGH */
	case FDRAW_RDCMD:
	case FDRAW_READDEL:
	case FDRAW_READTRACK:
		/* Insert the appropriate drive number */
		csb->csb_cmds[1] = csb->csb_cmds[1] | (unit & DRV_MASK);
		if (fdc->c_fdtype & FDCTYPE_SB)
			csb->csb_cmds[1] |= IPS;
		csb->csb_opflags = CSB_OFXFEROPS + CSB_OFTIMEIT;
		csb->csb_nrslts = NRBRW;
		break;

	default:
		fdretcsb(fdc);
		mutex_exit(&fdc->c_lolock);
		return (EINVAL);
	}

	if ((csb->csb_opflags & CSB_OFXFEROPS) && (fdr.fdr_nbytes == 0)) {
		fdretcsb(fdc);
		mutex_exit(&fdc->c_lolock);
		return (EINVAL);
	}
	csb->csb_opflags |= CSB_OFRAWIOCTL;

	FDERRPRINT(FDEP_L1, FDEM_RAWI,
	    (C, "fdrawioctl: nbytes = %u\n", fdr.fdr_nbytes));

	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT) {
		if ((fc = (uint_t)fdr.fdr_nbytes) > 0) {
			/*
			 * In SunOS 4.X, we used to as_fault things in.
			 * We really cannot do this in 5.0/SVr4. Unless
			 * someone really believes that speed is of the
			 * essence here, it is just much simpler to do
			 * this in kernel space and use copyin/copyout.
			 */
			if (fdc->c_fdtype & FDCTYPE_DMA) {
				mutex_enter(&fdc->c_hilock);
				res = ddi_dma_mem_alloc(fdc->c_dmahandle, fc,
				    &attr, DDI_DMA_STREAMING,
				    DDI_DMA_DONTWAIT, 0, &fa, &real_length,
				    &mem_handle);

				if (res != DDI_SUCCESS) {
					fdretcsb(fdc);
					mutex_exit(&fdc->c_lolock);
					mutex_exit(&fdc->c_hilock);
					return (EIO);
				}

				if (flag == B_WRITE)
					fdc->c_csb.csb_read = CSB_WRITE;
				else
					fdc->c_csb.csb_read = CSB_READ;

				if (fdstart_dma(fdc, fa, fc) != 0) {
					ddi_dma_mem_free(&mem_handle);
					fdretcsb(fdc);
					mutex_exit(&fdc->c_lolock);
					mutex_exit(&fdc->c_hilock);
					return (EIO);
				}
				mutex_exit(&fdc->c_hilock);

			} else {
				fa = kmem_zalloc(fc, KM_SLEEP);
			}

			if (flag == B_WRITE) {
				if (ddi_copyin(fdr.fdr_addr, fa, fc, mode)) {
					if (fdc->c_fdtype & FDCTYPE_DMA)
						ddi_dma_mem_free(&mem_handle);
					else
						kmem_free(fa, fc);
					fdretcsb(fdc);
					mutex_exit(&fdc->c_lolock);
					FDERRPRINT(FDEP_L1, FDEM_RAWI, (C,
					    "fdrawioctl: can't copy data\n"));

					return (EFAULT);
				}
			}
			csb->csb_addr = fa;
			csb->csb_len = fc;
		} else {
			csb->csb_addr = 0;
			csb->csb_len = 0;
		}
	} else {
		csb->csb_addr = fa;
		csb->csb_len = fc;
	}

	FDERRPRINT(FDEP_L1, FDEM_RAWI,
	    (C, "cmd: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_cmds[0],
	    csb->csb_cmds[1], csb->csb_cmds[2], csb->csb_cmds[3],
	    csb->csb_cmds[4], csb->csb_cmds[5], csb->csb_cmds[6],
	    csb->csb_cmds[7], csb->csb_cmds[8], csb->csb_cmds[9]));
	FDERRPRINT(FDEP_L1, FDEM_RAWI,
	    (C, "nbytes: %x, opflags: %x, addr: %p, len: %x\n",
	    csb->csb_ncmds, csb->csb_opflags, (void *)csb->csb_addr,
	    csb->csb_len));


	/*
	 * Note that we ignore any error return s from fdexec.
	 * This is the way the driver has been, and it may be
	 * that the raw ioctl senders simply don't want to
	 * see any errors returned in this fashion.
	 */

	if ((csb->csb_opflags & CSB_OFNORESULTS) ||
	    (csb->csb_opflags & CSB_OFIMMEDIATE)) {
		(void) fdexec(fdc, 0); /* don't sleep, don't check change */
	} else {
		(void) fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG);
	}


	FDERRPRINT(FDEP_L1, FDEM_RAWI,
	    (C, "rslt: %x %x %x %x %x %x %x %x %x %x\n", csb->csb_rslt[0],
	    csb->csb_rslt[1], csb->csb_rslt[2], csb->csb_rslt[3],
	    csb->csb_rslt[4], csb->csb_rslt[5], csb->csb_rslt[6],
	    csb->csb_rslt[7], csb->csb_rslt[8], csb->csb_rslt[9]));

	if ((fdr.fdr_cmd[0] & 0x0f) != FDRAW_FORMAT && fc &&
	    flag == B_READ && err == 0) {
		if (ddi_copyout(fa, fdr.fdr_addr, fc, mode)) {
			FDERRPRINT(FDEP_L1, FDEM_RAWI,
			    (C, "fdrawioctl: can't copy read data\n"));

			err = EFAULT;
		}
	}


	if (fc) {
		if (fdc->c_fdtype & FDCTYPE_DMA) {
			ddi_dma_mem_free(&mem_handle);
			FDERRPRINT(FDEP_L1, FDEM_RAWI,
			    (C, "fdrawioctl: free dma memory\n"));
		} else {
			kmem_free(fa, fc);
		}
	}


	/* copy cmd results into fdr */
	for (i = 0; (int)i <= (int)csb->csb_nrslts; i++)
		fdr.fdr_result[i] = csb->csb_rslt[i];
	fdr.fdr_nbytes = fdc->c_csb.csb_rlen; /* return resid */

	switch (ddi_model_convert_from(mode)) {
#ifdef _MULTI_DATAMODEL
	case DDI_MODEL_ILP32:
		bcopy(fdr.fdr_cmd, fdr32.fdr_cmd, sizeof (fdr32.fdr_cmd));
		fdr32.fdr_cnum = fdr.fdr_cnum;
		bcopy(fdr.fdr_result, fdr32.fdr_result,
		    sizeof (fdr32.fdr_result));
		fdr32.fdr_nbytes = fdr.fdr_nbytes;
		fdr32.fdr_addr = (caddr32_t)(uintptr_t)fdr.fdr_addr;
		if (ddi_copyout(&fdr32, (caddr_t)arg, sizeof (fdr32), mode)) {
			FDERRPRINT(FDEP_L1, FDEM_RAWI,
			    (C, "fdrawioctl: can't copy results32\n"));
			err = EFAULT;
		}
		break;
#endif
	case DDI_MODEL_NONE:
	default:
		if (ddi_copyout(&fdr, (caddr_t)arg, sizeof (fdr), mode)) {
			FDERRPRINT(FDEP_L1, FDEM_RAWI,
			    (C, "fdrawioctl: can't copy results\n"));
			err = EFAULT;
		}
		break;
	}

	fdretcsb(fdc);
	mutex_exit(&fdc->c_lolock);
	return (0);
}

/*
 * fdformat
 *	format a track
 * For PIO, builds a table of sector data values with 16 bytes
 * (sizeof fdc's fifo) of dummy on end.	 This is so than when fdc->c_len
 * goes to 0 and fd_intr sends a TC that all the real formatting will
 * have already been done.
 *
 *	- called with the low level lock held
 */
static int
fdformat(struct fdctlr *fdc, int unit, int cyl, int hd)
{
	struct fdcsb *csb;
	struct fdunit *un;
	struct fd_char *ch;
	int	cmdresult;
	uchar_t	*fmthdrs;
	caddr_t fd;
	int	i;
	size_t	real_length;
	ddi_device_acc_attr_t attr;
	ddi_acc_handle_t mem_handle;

	FDERRPRINT(FDEP_L1, FDEM_FORM,
	    (C, "fdformat cyl %d, hd %d\n", cyl, hd));
	fdgetcsb(fdc);

	ASSERT(fdc->c_un->un_unit_no == unit);

	csb = &fdc->c_csb;
	un = fdc->c_un;
	ch = un->un_chars;

	/* setup common things in csb */
	csb->csb_unit = (uchar_t)unit;

	/*
	 * The controller needs to do a seek before
	 * each format to get to right cylinder.
	 */
	if (fdrecalseek(fdc, unit, cyl, FDXC_CHECKCHG)) {
		fdretcsb(fdc);
		return (EIO);
	}

	/*
	 * now do the format itself
	 */
	csb->csb_nrslts = NRBRW;
	csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;

	csb->csb_cmds[0] = FDRAW_FORMAT;
	/* always or in MFM bit */
	csb->csb_cmds[0] |= MFM;
	csb->csb_cmds[1] = (hd << 2) | (unit & 0x03);
	csb->csb_cmds[2] = ch->fdc_medium ? 3 : 2;
	csb->csb_cmds[3] = ch->fdc_secptrack;
	csb->csb_cmds[4] = GPLF;
	csb->csb_cmds[5] = FDATA;
	csb->csb_ncmds = 6;
	csb->csb_maxretry = rwretry;
	csb->csb_retrys = 0;

	/*
	 * NOTE: have to add size of fifo also - for dummy format action
	 * if PIO is being used.
	 */


	if (fdc->c_fdtype & FDCTYPE_DMA) {

		csb->csb_len = (uint_t)4 * ch->fdc_secptrack;

		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
		attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

		mutex_enter(&fdc->c_hilock);

		cmdresult = ddi_dma_mem_alloc(fdc->c_dmahandle, csb->csb_len,
		    &attr, DDI_DMA_STREAMING,
		    DDI_DMA_DONTWAIT, 0, &fd, &real_length,
		    &mem_handle);

		if (cmdresult != DDI_SUCCESS) {
			mutex_exit(&fdc->c_hilock);
			return (cmdresult);
		}

		fdc->c_csb.csb_read = CSB_WRITE;
		if (fdstart_dma(fdc, fd,  csb->csb_len) != 0) {
			ddi_dma_mem_free(&mem_handle);
			mutex_exit(&fdc->c_hilock);
			return (-1);
		}
		mutex_exit(&fdc->c_hilock);


	} else {
		csb->csb_len = (uint_t)4 * ch->fdc_secptrack + 16;
		fd = kmem_zalloc(csb->csb_len, KM_SLEEP);
		fmthdrs = (uchar_t *)fd;
	}

	csb->csb_addr = (caddr_t)fd;

	for (i = 1; i <= ch->fdc_secptrack; i++) {
		*fd++ = (uchar_t)cyl;		/* cylinder */
		*fd++ = (uchar_t)hd;		/* head */
		*fd++ = (uchar_t)i;	/* sector number */
		*fd++ = ch->fdc_medium ? 3 : 2; /* sec_size code */
	}

	if ((cmdresult = fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG)) == 0) {
		if (csb->csb_cmdstat)
			cmdresult = EIO;	/* XXX TBD NYD for now */
	}

	if (fdc->c_fdtype & FDCTYPE_DMA) {
		ddi_dma_mem_free(&mem_handle);
	} else {
		kmem_free((caddr_t)fmthdrs, csb->csb_len);
	}

	fdretcsb(fdc);

	return (cmdresult);
}

/*
 * fdstart
 *	called from fd_strategy() or from fdXXXX() to setup and
 *	start operations of read or write only (using buf structs).
 *	Because the chip doesn't handle crossing cylinder boundaries on
 *	the fly, this takes care of those boundary conditions.	Note that
 *	it sleeps until the operation is done *within fdstart* - so that
 *	when fdstart returns, the operation is already done.
 *
 *	- called with the low level lock held
 *
 */

static int slavio_index_pulse_work_around = 0;

static void
fdstart(struct fdctlr *fdc)
{
	struct buf *bp;
	struct fdcsb *csb;
	struct fdunit *un;
	struct fd_char *ch;
	struct dk_map32 *dkm;
	uint_t	part;		/* partition number for the transfer */
	uint_t	start_part;	/* starting block of the partition */
	uint_t	last_part;	/* last block of the partition */
	uint_t	blk;		/* starting block of transfer on diskette */
	uint_t	sect;		/* starting block's offset into track */
	uint_t	cyl;		/* starting cylinder of the transfer */
	uint_t	bincyl;		/* starting blocks's offset into cylinder */
	uint_t	secpcyl;	/* number of sectors per cylinder */
	uint_t	phys_blkno;	/* no. of blocks on the diskette */
	uint_t	head;		/* one of two diskette heads */
	uint_t	unit;
	uint_t	len, tlen;
	caddr_t addr;
	caddr_t temp_addr;
	uint_t	partial_read = 0;
	int sb_temp_buf_used = 0;

	bp = fdc->c_actf;

	while (bp != NULL) {

		fdc->c_actf = bp->av_forw;
		fdc->c_current = bp;

		/*
		 * Initialize the buf structure.  The residual count is
		 * initially the number of bytes to be read or written
		 */
		bp->b_flags &= ~B_ERROR;
		bp->b_error = 0;
		bp->b_resid = bp->b_bcount;
		bp_mapin(bp);			/* map in buffers */

		addr = bp->b_un.b_addr;		/* assign buffer address */

		/*
		 * Find the unit and partition numbers.
		 */
		unit = fdc->c_un->un_unit_no;
		un = fdc->c_un;
		ch = un->un_chars;
		part = FDPARTITION(bp->b_edev);
		dkm = &un->un_label.dkl_map[part];

		if (un->un_chars->fdc_medium) {
			phys_blkno = bp->b_blkno >> 1;
		} else {
			phys_blkno = bp->b_blkno;
		}

		if (un->un_iostat) {
			kstat_waitq_to_runq(KIOSP);
		}

		FDERRPRINT(FDEP_L1, FDEM_STRT,
		    (C, "fdstart: bp=0x%p blkno=0x%x bcount=0x%x\n",
		    (void *)bp, (int)bp->b_blkno, (int)bp->b_bcount));

		/*
		 * Get the csb and initialize the values that are the same
		 * for DMA and PIO.
		 */
		fdgetcsb(fdc);		/* get csb (maybe wait for it) */
		csb = &fdc->c_csb;
		csb->csb_unit = unit;		/* floppy unit number */


		/*
		 * bugID:4133425 : If the controller is SLAVIO, and
		 * the read does not reach end of track, then modify
		 * the tlen to read until the end of track to a temp
		 * buffer and disable MT. After the read is over,
		 * copy the useful portion of the data to 'addr'.
		 * Enable this feature only when
		 * slavio_index_pulse_work_aound variable is
		 * set in /etc/system.
		 */


		if (bp->b_flags & B_READ) {
			if (((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
			    slavio_index_pulse_work_around) ||
			    (fdc->c_fdtype & FDCTYPE_TCBUG))
				csb->csb_cmds[0] = SK | FDRAW_RDCMD | MFM;
			else
				csb->csb_cmds[0] = MT | SK | FDRAW_RDCMD | MFM;
		} else {
			if (fdc->c_fdtype & FDCTYPE_TCBUG)
				csb->csb_cmds[0] = FDRAW_WRCMD | MFM;
			else
				csb->csb_cmds[0] = MT | FDRAW_WRCMD | MFM;
		}


		if (bp->b_flags & B_READ)
			fdc->c_csb.csb_read = CSB_READ;
		else
			fdc->c_csb.csb_read = CSB_WRITE;


		csb->csb_cmds[5] = ch->fdc_medium ? 3 : 2; /* sector size  */
		csb->csb_cmds[6] = ch->fdc_secptrack; /* EOT-# of sectors/trk */
		csb->csb_cmds[7] = GPLN;	/* GPL - gap 3 size code */
		csb->csb_cmds[8] = SSSDTL;	/* DTL - be 0xFF if N != 0 */

		csb->csb_ncmds = NCBRW;		/* number of command bytes */
		csb->csb_nrslts = NRBRW;	/* number of result bytes */


		/*
		 * opflags for interrupt handler, et.al.
		 */
		csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;


		/*
		 * Make sure the transfer does not go off the end
		 * of the partition.  Limit the actual amount transferred
		 * to fit the partition.
		 */

		blk = phys_blkno;
		start_part = (dkm->dkl_cylno * ch->fdc_secptrack
		    * ch->fdc_nhead);
		blk = blk + start_part;
		last_part = start_part + dkm->dkl_nblk;

		if ((blk + (bp->b_bcount / ch->fdc_sec_size)) > last_part)
			len = (last_part - blk) * ch->fdc_sec_size;
		else
			len = (uint_t)bp->b_bcount;

		/*
		 * now we have the real start blk,
		 * addr and len for xfer op
		 * sectors per cylinder
		 */
		secpcyl = ch->fdc_nhead * ch->fdc_secptrack;

		/*
		 * The controller can transfer up to a cylinder at a time.
		 * Early revs of the 82077 have a bug that causes the chip to
		 * fail to respond to the Terminal Count signal.  Due to this
		 * bug, controllers with type FDCTYPE_TCBUG, only transfer up
		 * to a track at a time.
		 * See earlier comment for bugID:4133425 for index pulse
		 * work around.
		 */

		while (len != 0) {

			cyl = blk / secpcyl;	/* cylinder of transfer */
			bincyl = blk % secpcyl;	/* blk within cylinder */
			head = bincyl / ch->fdc_secptrack;
			sect = (bincyl % ch->fdc_secptrack) + 1;
						/* sect w/in track */

			/*
			 * If the desired block and length will go beyond the
			 * cylinder end, limit it to the cylinder end.
			 */

			if ((fdc->c_fdtype & FDCTYPE_SLAVIO) &&
			    slavio_index_pulse_work_around &&
			    (fdc->c_csb.csb_read == CSB_READ)) {

				tlen = (ch->fdc_secptrack - sect + 1) *
				    ch->fdc_sec_size;
				if (len < tlen) {
					partial_read = 1;
					temp_addr = (caddr_t)kmem_alloc(tlen,
					    KM_SLEEP);
				}

			} else if (fdc->c_fdtype & FDCTYPE_TCBUG) {
				tlen = len;
				if (len > ((ch->fdc_secptrack - sect + 1) *
				    ch->fdc_sec_size))
					tlen = (ch->fdc_secptrack - sect + 1)
					    * ch->fdc_sec_size;
			} else {
				if (len > ((secpcyl - bincyl)
				    * ch->fdc_sec_size))
					tlen = (secpcyl - bincyl)
					    * ch->fdc_sec_size;

				else
					tlen = len;
			}
			if (fdc->c_fdtype & FDCTYPE_SB) {
				/*
				 * To avoid underrun errors during IFB activity.
				 */
				if (tlen > max_fd_dma_len)
					tlen = max_fd_dma_len;
			}

			FDERRPRINT(FDEP_L1, FDEM_STRT,
			    (C, "	blk 0x%x, addr 0x%p, len 0x%x\n",
			    blk, (void *)addr, len));
			FDERRPRINT(FDEP_L1, FDEM_STRT,
			    (C, "cyl:%x, head:%x, sec:%x\n",
			    cyl, head, sect));

			FDERRPRINT(FDEP_L1, FDEM_STRT,
			    (C, "	resid 0x%lx, tlen %d\n",
			    bp->b_resid, tlen));

			/*
			 * Finish programming the command
			 */
			csb->csb_cmds[1] = (head << 2) | unit;
			if (fdc->c_fdtype & FDCTYPE_SB)
				csb->csb_cmds[1] |= IPS;

			csb->csb_cmds[2] = cyl;	/* C - cylinder address */
			csb->csb_cmds[3] = head;	/* H - head number */
			csb->csb_cmds[4] = sect;	/* R - sector number */
			if (fdc->c_fdtype & FDCTYPE_TCBUG)
				csb->csb_cmds[6] = sect +
				    (tlen / ch->fdc_sec_size) - 1;

			csb->csb_len = tlen;
			if (partial_read)
				csb->csb_addr = temp_addr;
			else
				csb->csb_addr = addr;

			/* retry this many times max */
			csb->csb_maxretry = rwretry;
			csb->csb_retrys = 0;

			/* If platform supports DMA, set up DMA resources */
			if (fdc->c_fdtype & FDCTYPE_DMA) {
				if ((fdc->c_fdtype & FDCTYPE_SB) &&
				    (((uint32_t)(uintptr_t)addr & 0xFFFF0000) !=
				    (((uint32_t)(uintptr_t)addr + tlen) &
				    0xFFFF0000))) {
					csb->csb_addr = fdc->dma_buf;
					sb_temp_buf_used = 1;
					if (csb->csb_read != CSB_READ) {
						bcopy(addr, fdc->dma_buf, tlen);
				}
			}
				mutex_enter(&fdc->c_hilock);

				if (fdstart_dma(fdc, csb->csb_addr,
				    tlen) != 0) {

					bp->b_flags |= B_ERROR;
					bp->b_error = EAGAIN;

					mutex_exit(&fdc->c_hilock);
					FDERRPRINT(FDEP_L1, FDEM_STRT,
					    (C, "fdstart: no dma resources\n"));

					break;
				}
				mutex_exit(&fdc->c_hilock);

			}

			bp->b_error = fdexec(fdc, FDXC_SLEEP|FDXC_CHECKCHG);
			if (bp->b_error != 0) {
				/*
				 * error in fdexec
				 */
				FDERRPRINT(FDEP_L1, FDEM_STRT, (C,
				    "fdstart: bad exec of bp: 0x%p, err %d\n",
				    (void *)bp, bp->b_error));

				bp->b_flags |= B_ERROR;
				if (partial_read) {
					partial_read = 0;
					kmem_free(temp_addr, tlen);
				}
				break;
			}

			/*
			 * If it was a partial read, copy the useful
			 * portion of data to 'addr'.
			 */
			if (partial_read) {
				partial_read = 0;
				bcopy(temp_addr, addr, len);
				kmem_free(temp_addr, tlen);
				tlen = len;
			}
			if ((fdc->c_fdtype & FDCTYPE_SB) &&
			    (csb->csb_read == CSB_READ)) {
				if (sb_temp_buf_used) {
					bcopy(fdc->dma_buf, addr, tlen);
					sb_temp_buf_used = 0;
				}
			}

			blk += tlen / ch->fdc_sec_size;
			len -= tlen;
			addr += tlen;
			bp->b_resid -= tlen;

		}

		FDERRPRINT(FDEP_L1, FDEM_STRT,
		    (C, "fdstart done: b_resid %lu, b_count %lu, csb_rlen %d\n",
		    bp->b_resid, bp->b_bcount, fdc->c_csb.csb_rlen));

		fdc->c_current = 0;
		fdretcsb(fdc);
		if (un->un_iostat) {
			if (bp->b_flags & B_READ) {
				KIOSP->reads++;
				KIOSP->nread +=
				    (bp->b_bcount - bp->b_resid);
			} else {
				KIOSP->writes++;
				KIOSP->nwritten += (bp->b_bcount - bp->b_resid);
			}
			kstat_runq_exit(KIOSP);
		}
		biodone(bp);

		/*
		 * Look at the next buffer
		 */
		bp = fdc->c_actf;

	}
}

/*
 * Set up DMA resources
 * The DMA handle was initialized in fd_attach()
 * Assumes the handle has already been allocated by fd_attach()
 */
static int
fdstart_dma(struct fdctlr *fdc, caddr_t addr, uint_t len)
{
	int		flags;		/* flags for setting up resources */
	int		res;

	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: start\n"));

	if (fdc->c_csb.csb_read == CSB_READ) {
		flags = DDI_DMA_READ;
	} else {
		flags = DDI_DMA_WRITE;
	}


	/* allow partial mapping to maximize the portability of the driver */
	flags = flags | DDI_DMA_PARTIAL;

	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: amt. asked for %d\n",
	    len));

	/*
	 * Zero out the current cookie.  This is done to ensure that
	 * the previous transfers cookie information can in no way be
	 * used.
	 */
	bzero((char *)&fdc->c_csb.csb_dmacookie,
	    sizeof (fdc->c_csb.csb_dmacookie));
	fdc->c_csb.csb_nwin = 0;
	fdc->c_csb.csb_windex = 0;
	fdc->c_csb.csb_ccount = 0;

	res = ddi_dma_addr_bind_handle(fdc->c_dmahandle, NULL, addr, len,
	    flags, DDI_DMA_DONTWAIT, 0,  &fdc->c_csb.csb_dmacookie,
	    &fdc->c_csb.csb_ccount);

	switch (res) {
		case DDI_DMA_MAPPED:
			/*
			 * There is one window. csb_windex is the index
			 * into the array of windows. If there are n
			 * windows then, (0 <= windex <= n-1).  csb_windex
			 * represents the index of the next window
			 * to be processed.
			 */
			fdc->c_csb.csb_nwin = 1;
			fdc->c_csb.csb_windex = 1;


			FDERRPRINT(FDEP_L1, FDEM_SDMA,
			    (C, "fdstart_dma: DDI_DMA_MAPPED\n"));

			break;
		case DDI_DMA_PARTIAL_MAP:

			/*
			 * obtain the number of DMA windows
			 */
			if (ddi_dma_numwin(fdc->c_dmahandle,
			    &fdc->c_csb.csb_nwin) != DDI_SUCCESS) {
				return (-1);
			}


			FDERRPRINT(FDEP_L1, FDEM_SDMA,
			    (C, "fdstart_dma: partially mapped %d windows\n",
			    fdc->c_csb.csb_nwin));

			/*
			 * The DMA window currently in use is window number
			 * one.
			 */
			fdc->c_csb.csb_windex = 1;

			break;
		case DDI_DMA_NORESOURCES:
			FDERRPRINT(FDEP_L1, FDEM_SDMA,
			    (C, "fdstart_dma: no resources\n"));
			return (-1);
		case DDI_DMA_NOMAPPING:
			FDERRPRINT(FDEP_L1, FDEM_SDMA,
			    (C, "fdstart_dma: no mapping\n"));
			return (-1);
		case DDI_DMA_TOOBIG:
			FDERRPRINT(FDEP_L1, FDEM_SDMA,
			    (C, "fdstart_dma: too big\n"));
			return (-1);

		case DDI_DMA_INUSE:
			FDERRPRINT(FDEP_L1, FDEM_SDMA,
			    (C, "fdstart_dma: dma inuse\n"));
			return (-1);
		default:
			FDERRPRINT(FDEP_L1, FDEM_SDMA,
			    (C, "fdstart_dma: result is 0x%x\n", res));
			return (-1);

	};

	FDERRPRINT(FDEP_L1, FDEM_SDMA,
	    (C, "fdstart_dma: bound the handle\n"));

	ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);

	FDERRPRINT(FDEP_L1, FDEM_SDMA, (C, "fdstart_dma: done\n"));
	return (0);
}


/*
 * fd_unbind_handle: unbind a dma handle if one exists
 *		return EIO if unbind failes
 */
static int
fd_unbind_handle(struct fdctlr *fdc)
{
	if ((fdc->c_fdtype & FDCTYPE_DMA) &&
	    ((fdc->c_csb.csb_read == CSB_READ) ||
	    (fdc->c_csb.csb_read == CSB_WRITE))) {
		mutex_enter(&fdc->c_hilock);

		if (fdc->c_fdtype & FDCTYPE_SB) {
			if (fdc->sb_dma_lock) {
				release_sb_dma(fdc);
			}
		}

		/*
		 * If the byte count isn't zero, then the DMA engine is
		 * still doing a transfer.  If the byte count is nonzero,
		 * reset the DMA engine to cause it to drain.
		 */

		if (get_data_count_register(fdc) != 0) {
			FDERRPRINT(FDEP_L1, FDEM_EXEC,
			    (C, "unbind & byte count isn't zero\n"));

			reset_dma_controller(fdc);
			set_dma_control_register(fdc, DCSR_INIT_BITS);
		}

		if (ddi_dma_unbind_handle(fdc->c_dmahandle) != DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_EXEC,
			    (C, "problem unbinding the handle\n"));
			mutex_exit(&fdc->c_hilock);
			return (EIO);
		}
		mutex_exit(&fdc->c_hilock);
	}
	return (0);
}

/*
 * fdexec
 *	all commands go through here.  Assumes the command block
 *	fdctlr.c_csb is filled in.  The bytes are sent to the
 *	controller and then we do whatever else the csb says -
 *	like wait for immediate results, etc.
 *
 *	All waiting for operations done is in here - to allow retrys
 *	and checking for disk changed - so we don't have to worry
 *	about sleeping at interrupt level.
 *
 * RETURNS: 0 if all ok,
 *	ENXIO - diskette not in drive
 *	EBUSY - if chip is locked or busy
 *	EIO - for timeout during sending cmds to chip
 *
 * to sleep: set FDXC_SLEEP, to check for disk
 * changed: set FDXC_CHECKCHG
 *
 *	- called with the lock held
 */
static int
fdexec(struct fdctlr *fdc, int flags)
{
	struct fdcsb *csb;
	int	i;
	int	to, unit;
	uchar_t	tmp;
	caddr_t a = (caddr_t)fdc;

	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: flags:%x\n", flags));

	ASSERT(mutex_owned(&fdc->c_lolock));

	csb = &fdc->c_csb;
	unit = csb->csb_unit;


	ASSERT(unit == fdc->c_un->un_unit_no);

retry:
	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: cmd is %s\n",
	    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: transfer rate = %d\n",
	    fdc->c_un->un_chars->fdc_transfer_rate));
	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: sec size = %d\n",
	    fdc->c_un->un_chars->fdc_sec_size));
	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: nblocks (512) = %d\n",
	    fdc->c_un->un_label.dkl_map[2].dkl_nblk));

	if ((fdc->c_fdtype & FDCTYPE_CTRLMASK) == FDCTYPE_82077) {
		fdexec_turn_on_motor(fdc, flags, unit);
	}


	fdselect(fdc, unit, 1);	/* select drive */

	/*
	 * select data rate for this unit/command
	 */
	switch (fdc->c_un->un_chars->fdc_transfer_rate) {
	case 500:
		Dsr(fdc, 0);
		break;
	case 300:
		Dsr(fdc, 1);
		break;
	case 250:
		Dsr(fdc, 2);
		break;
	}
	drv_usecwait(2);


	/*
	 * If checking for changed is enabled (i.e., not seeking in checkdisk),
	 * we sample the DSKCHG line to see if the diskette has wandered away.
	 */
	if ((flags & FDXC_CHECKCHG) && fdsense_chng(fdc, unit)) {
		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "diskette changed\n"));
		fdc->c_un->un_flags |= FDUNIT_CHANGED;

		if (fdcheckdisk(fdc, unit)) {

			(void) fd_unbind_handle(fdc);
			return (ENXIO);

		}
	}

	/*
	 * gather some statistics
	 */
	switch (csb->csb_cmds[0] & 0x1f) {
	case FDRAW_RDCMD:
		fdc->fdstats.rd++;
		break;
	case FDRAW_WRCMD:
		fdc->fdstats.wr++;
		break;
	case FDRAW_REZERO:
		fdc->fdstats.recal++;
		break;
	case FDRAW_FORMAT:
		fdc->fdstats.form++;
		break;
	default:
		fdc->fdstats.other++;
		break;
	}

	/*
	 * Always set the opmode *prior* to poking the chip.
	 * This way we don't have to do any locking at high level.
	 */
	csb->csb_raddr = 0;
	csb->csb_rlen = 0;
	if (csb->csb_opflags & CSB_OFSEEKOPS) {
		csb->csb_opmode = 2;
	} else if (csb->csb_opflags & CSB_OFIMMEDIATE) {
		csb->csb_opmode = 0;
	} else {
		csb->csb_opmode = 1;	/* normal data xfer commands */
		csb->csb_raddr = csb->csb_addr;
		csb->csb_rlen = csb->csb_len;
	}

	bzero((caddr_t)csb->csb_rslt, 10);
	csb->csb_status = 0;
	csb->csb_cmdstat = 0;


	/*
	 * Program the DMA engine with the length and address of the transfer
	 * (DMA is only used on a read or a write)
	 */
	if ((fdc->c_fdtype & FDCTYPE_DMA) &&
	    ((fdc->c_csb.csb_read == CSB_READ) ||
	    (fdc->c_csb.csb_read == CSB_WRITE)))  {
		mutex_enter(&fdc->c_hilock);

		/* Reset the dcsr to clear it of all errors */

		reset_dma_controller(fdc);

		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "cookie addr 0x%p\n",
		    (void *)fdc->c_csb.csb_dmacookie.dmac_laddress));

		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "cookie length %ld\n",
		    fdc->c_csb.csb_dmacookie.dmac_size));
		ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);

		set_data_count_register(fdc,
		    fdc->c_csb.csb_dmacookie.dmac_size);
		set_data_address_register(fdc,
		    fdc->c_csb.csb_dmacookie.dmac_laddress);

		/* Program the DCSR */

		if (fdc->c_csb.csb_read == CSB_READ)
			set_dma_mode(fdc, CSB_READ);
		else
			set_dma_mode(fdc, CSB_WRITE);
		mutex_exit(&fdc->c_hilock);
	}

	/*
	 * I saw this (chip unexpectedly busy) happen when i shoved the
	 * floppy into the drive while
	 * running a dd if= /dev/rfd0c.	so it *is* possible for this to happen.
	 * we need to do a ctlr reset ...
	 */

	if (Msr(fdc) & CB) {
		/* tried to give command to chip when it is busy! */
		FDERRPRINT(FDEP_L3, FDEM_EXEC,
		    (C, "fdc: unexpectedly busy-stat 0x%x\n", Msr(fdc)));
		csb->csb_cmdstat = 1;	/* XXX TBD ERRS NYD for now */

		(void) fd_unbind_handle(fdc);
		return (EBUSY);
	}

	/* Give command to the controller */
	for (i = 0; i < (int)csb->csb_ncmds; i++) {

		/* Test the readiness of the controller to receive the cmd */
		for (to = FD_CRETRY; to; to--) {
			if ((Msr(fdc) & (DIO|RQM)) == RQM)
				break;
		}
		if (to == 0) {
			FDERRPRINT(FDEP_L2, FDEM_EXEC,
			    (C, "fdc: no RQM - stat 0x%x\n", Msr(fdc)));
			csb->csb_cmdstat = 1;

			(void) fd_unbind_handle(fdc);
			return (EIO);
		}

		Set_Fifo(fdc, csb->csb_cmds[i]);

		FDERRPRINT(FDEP_L1, FDEM_EXEC,
		    (C, "fdexec: sent 0x%x, Msr 0x%x\n", csb->csb_cmds[i],
		    Msr(fdc)));

	}


	/*
	 * Start watchdog timer on data transfer type commands - required
	 * in case a diskette is not present or is unformatted
	 */
	if (csb->csb_opflags & CSB_OFTIMEIT) {
		fdc->c_timeid = timeout(fdwatch, a,
		    tosec * drv_usectohz(1000000));
	}

	FDERRPRINT(FDEP_L1, FDEM_EXEC,
	    (C, "fdexec: cmd sent, Msr 0x%x\n", Msr(fdc)));

	/* If the operation has no results - then just return */
	if (csb->csb_opflags & CSB_OFNORESULTS) {
		if (fdc->c_fdtype & FDCTYPE_82077) {
			if (fdc->c_mtimeid == 0) {
				fdc->c_mtimeid = timeout(fdmotoff, a,
				    Motoff_delay);
			}
		}
		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: O K ..\n"));

		/*
		 * Make sure the last byte is received well by the
		 * controller. On faster CPU, it may still be busy
		 * by the time another command comes here.
		 */
		for (to = FD_CRETRY; to; to--) {
			if ((Msr(fdc) & (DIO|RQM)) == RQM)
				break;
			}
		if (to == 0) {
			csb->csb_cmdstat = 1;
			return (EIO);
		}

		/*
		 * An operation that has no results isn't doing DMA so,
		 * there is no reason to try to unbind a handle
		 */
		return (0);
	}

	/*
	 * If this operation has no interrupt AND an immediate result
	 * then we just busy wait for the results and stuff them into
	 * the csb
	 */
	if (csb->csb_opflags & CSB_OFIMMEDIATE) {
		to = FD_RRETRY;
		csb->csb_nrslts = 0;
		/*
		 * Wait while this command is still going on.
		 */
		while ((tmp = Msr(fdc)) & CB) {
			/*
			 * If RQM + DIO, then a result byte is at hand.
			 */
			if ((tmp & (RQM|DIO|CB)) == (RQM|DIO|CB)) {
				csb->csb_rslt[csb->csb_nrslts++] =
				    Fifo(fdc);
				/*
				 * FDERRPRINT(FDEP_L4, FDEM_EXEC,
				 *    (C, "fdexec: got result 0x%x\n",
				 *    csb->csb_nrslts));
				 */
			} else if (--to == 0) {
				FDERRPRINT(FDEP_L4, FDEM_EXEC,
				    (C, "fdexec: timeout, Msr%x, nr%x\n",
				    Msr(fdc), csb->csb_nrslts));

				csb->csb_status = 2;
				if (fdc->c_fdtype & FDCTYPE_82077) {
					if (fdc->c_mtimeid == 0) {
						fdc->c_mtimeid = timeout(
						    fdmotoff, a, Motoff_delay);
					}
				}
				/*
				 * There is no DMA happening.  No need to
				 * try freeing a handle.
				 */

				return (EIO);
			}
		}
	}

	/*
	 * If told to sleep here, well then sleep!
	 */

	if (flags & FDXC_SLEEP) {
		fdc->c_flags |= FDCFLG_WAITING;
		while (fdc->c_flags & FDCFLG_WAITING) {
			cv_wait(&fdc->c_iocv, &fdc->c_lolock);
		}
	}

	/*
	 * kludge for end-of-cylinder error which must be ignored!!!
	 */

	if ((fdc->c_fdtype & FDCTYPE_TCBUG) &&
	    ((csb->csb_rslt[0] & IC_SR0) == 0x40) &&
	    (csb->csb_rslt[1] & EN_SR1))
		csb->csb_rslt[0] &= ~IC_SR0;

	/*
	 * See if there was an error detected, if so, fdrecover()
	 * will check it out and say what to do.
	 *
	 * Don't do this, though, if this was the Sense Drive Status
	 * or the Dump Registers command.
	 */
	if (((csb->csb_rslt[0] & IC_SR0) || (fdc->c_csb.csb_dcsr_rslt) ||
	    (csb->csb_status)) &&
	    ((csb->csb_cmds[0] != FDRAW_SENSE_DRV) &&
	    (csb->csb_cmds[0] != DUMPREG))) {
		/* if it can restarted OK, then do so, else return error */
		if (fdrecover(fdc) != 0) {
			if (fdc->c_fdtype & FDCTYPE_82077) {
				if (fdc->c_mtimeid == 0) {
					fdc->c_mtimeid = timeout(fdmotoff,
					    a, Motoff_delay);
				}
			}

			/*
			 * If this was a dma transfer, unbind the handle so
			 * that other transfers may use it.
			 */

			(void) fd_unbind_handle(fdc);
			return (EIO);
		} else {
			/* ASSUMES that cmd is still intact in csb */
			goto retry;
		}
	}

	/* things went ok */
	if (fdc->c_fdtype & FDCTYPE_82077) {
		if (fdc->c_mtimeid == 0) {
			fdc->c_mtimeid = timeout(fdmotoff, a, Motoff_delay);
		}
	}
	FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "fdexec: O K ..........\n"));

	if (fd_unbind_handle(fdc))
		return (EIO);

	return (0);
}

/*
 * Turn on the drive's motor
 *
 *	- called with the low level lock held
 */
static void
fdexec_turn_on_motor(struct fdctlr *fdc, int flags,  uint_t unit)
{
	clock_t local_lbolt;
	timeout_id_t timeid;

	/*
	 * The low level mutex may not be held over the call to
	 * untimeout().  See the manpage for details.
	 */
	timeid = fdc->c_mtimeid;
	fdc->c_mtimeid = 0;
	if (timeid) {
		mutex_exit(&fdc->c_lolock);
		(void) untimeout(timeid);
		mutex_enter(&fdc->c_lolock);
	}

	ASSERT(fdc->c_un->un_unit_no == unit);


	set_rotational_speed(fdc, unit);

	if (!(Dor(fdc) & (MOTEN(unit)))) {
		/*
		 * Turn on the motor
		 */
		FDERRPRINT(FDEP_L1, FDEM_EXEC,
		    (C, "fdexec: turning on motor\n"));

		/* LINTED */
		Set_dor(fdc, (MOTEN(unit)), 1);

		if (flags & FDXC_SLEEP) {
			local_lbolt = ddi_get_lbolt();
			(void) cv_timedwait(&fdc->c_motoncv,
			    &fdc->c_lolock, local_lbolt + Moton_delay);
		} else {
			drv_usecwait(1000000);
		}
	}

}

/*
 * fdrecover
 *	see if possible to retry an operation.
 *	All we can do is restart the operation.	 If we are out of allowed
 *	retries - return non-zero so that the higher levels will be notified.
 *
 * RETURNS: 0 if ok to restart, !0 if can't or out of retries
 *	- called with the low level lock held
 */
static int
fdrecover(struct fdctlr *fdc)
{
	struct fdcsb *csb;

	FDERRPRINT(FDEP_L1, FDEM_RECO, (C, "fdrecover\n"));
	csb = &fdc->c_csb;

	if (fdc->c_flags & FDCFLG_TIMEDOUT) {
		struct fdcsb savecsb;

		fdc->c_flags ^= FDCFLG_TIMEDOUT;
		csb->csb_rslt[1] |= TO_SR1;
		FDERRPRINT(FDEP_L1, FDEM_RECO,
		    (C, "fd%d: %s timed out\n", csb->csb_unit,
		    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));

		/* use private csb */
		savecsb = fdc->c_csb;
		bzero(&fdc->c_csb, sizeof (struct fdcsb));
		FDERRPRINT(FDEP_L1, FDEM_RECO, (C, "fdc: resetting\n"));

		(void) fdreset(fdc);

		if (fdc->c_fdtype & FDCTYPE_DMA) {
			mutex_enter(&fdc->c_hilock);
			/* Reset the DMA engine as well */
			reset_dma_controller(fdc);
			set_dma_control_register(fdc, DCSR_INIT_BITS);
			mutex_exit(&fdc->c_hilock);
		}


		/* check change first?? */
		/* don't ckchg in fdexec, too convoluted */
		(void) fdrecalseek(fdc, savecsb.csb_unit, -1, 0);
		fdc->c_csb = savecsb; /* restore original csb */
	}

	/*
	 * gather statistics on errors
	 */
	if (csb->csb_rslt[1] & DE_SR1) {
		fdc->fdstats.de++;
	}
	if (csb->csb_rslt[1] & OR_SR1) {
		fdc->fdstats.run++;
	}
	if (csb->csb_rslt[1] & (ND_SR1+MA_SR1)) {
		fdc->fdstats.bfmt++;
	}
	if (csb->csb_rslt[1] & TO_SR1) {
		fdc->fdstats.to++;
	}

	/*
	 * If raw ioctl don't examine results just pass status
	 * back via fdraw. Raw commands are timed too, so put this
	 * after the above check.
	 */
	if (csb->csb_opflags & CSB_OFRAWIOCTL) {
		return (1);
	}


	/*
	 * if there was a pci bus error, do not retry
	 */

		if (csb->csb_dcsr_rslt == 1) {
			FDERRPRINT(FDEP_L3, FDEM_RECO,
			    (C, "fd%d: host bus error\n", 0));
		return (1);
		}

	/*
	 * If there was an error with the DMA functions, do not retry
	 */
	if (csb->csb_dma_rslt == 1) {
			FDERRPRINT(FDEP_L1, FDEM_RECO,
			    (C, "fd%d: DMA interface error\n", csb->csb_unit));
		return (1);
	}


	/*
	 * if we have run out of retries, return an error
	 * XXX need better status interp
	 */

	csb->csb_retrys++;
	if (csb->csb_retrys > csb->csb_maxretry) {
		FDERRPRINT(FDEP_L3, FDEM_RECO,
		    (C, "fd%d: %s failed (%x %x %x)\n",
		    0, fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
		if (csb->csb_rslt[1] & NW_SR1) {
			FDERRPRINT(FDEP_L3, FDEM_RECO,
			    (C, "fd%d: not writable\n", 0));
		}
		if (csb->csb_rslt[1] & DE_SR1) {
			FDERRPRINT(FDEP_L3, FDEM_RECO,
			    (C, "fd%d: crc error blk %d\n", 0,
			    (int)fdc->c_current->b_blkno));
		}
		if (csb->csb_rslt[1] & OR_SR1) {
			if (fdc->c_fdtype & FDCTYPE_SB) {
				/*
				 * When using southbridge chip we need to
				 * retry atleast 10 times to shake off the
				 * underrun err.
				 */
				if (csb->csb_retrys <= rwretry)
					return (0);
			}
			FDERRPRINT(FDEP_L3, FDEM_RECO,
			    (C, "fd%d: over/underrun\n", 0));
		}

		if (csb->csb_rslt[1] & (ND_SR1+MA_SR1)) {
			FDERRPRINT(FDEP_L3, FDEM_RECO,
			    (C, "fd%d: bad format\n", 0));
		}

		if (csb->csb_rslt[1] & TO_SR1) {
			FDERRPRINT(FDEP_L3, FDEM_RECO,
			    (C, "fd%d: timeout\n", 0));
		}

		csb->csb_cmdstat = 1; /* failed - give up */
		return (1);
	}

	if (csb->csb_opflags & CSB_OFSEEKOPS) {
		/* seek, recal type commands - just look at st0 */
		FDERRPRINT(FDEP_L2, FDEM_RECO,
		    (C, "fd%d: %s error : st0 0x%x\n", csb->csb_unit,
		    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
		    csb->csb_rslt[0]));
	}
	if (csb->csb_opflags & CSB_OFXFEROPS) {
		/* rd, wr, fmt type commands - look at st0, st1, st2 */
		FDERRPRINT(FDEP_L2, FDEM_RECO,
		    (C, "fd%d: %s error : st0=0x%x st1=0x%x st2=0x%x\n",
		    csb->csb_unit, fdcmds[csb->csb_cmds[0] & 0x1f].cmdname,
		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
	}

	return (0);	/* tell fdexec to retry */
}

/*
 * Interrupt handle for DMA
 */

static uint_t
fdintr_dma()
{
	struct fdctlr   *fdc;
	off_t		off;
	size_t		len;
	uint_t		ccount;
	uint_t		windex;
	uint_t		done = 0;
	int		tmp_dcsr;
	int		to;
	uchar_t		tmp;
	int		i = 0;
	int		res = DDI_INTR_UNCLAIMED;
	int		not_cheerio = 1;

	/* search for a controller that's expecting an interrupt */
	fdc = fdctlrs;

	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		tmp_dcsr = get_dma_control_register(fdc);
		if (!(tmp_dcsr & DCSR_INT_PEND) && !(DCSR_ERR_PEND & tmp_dcsr))
			return (res);
		not_cheerio = 0;
	}

	mutex_enter(&fdc->c_hilock);

	if (fdc->c_csb.csb_opmode == 0x0) {
		fdc->c_csb.csb_opmode = 2;
	}
	if (fdc->sb_dma_lock) {
		release_sb_dma(fdc);
	}

	/*
	 * An interrupt can come from either the floppy controller or
	 * or the DMA engine.  The DMA engine will only issue an
	 * interrupt if there was an error.
	 */

	switch (fdc->c_csb.csb_opmode) {
		case 0x1:
			/* read/write/format data-xfer case */

			FDERRPRINT(FDEP_L1, FDEM_INTR,
			    (C, "fdintr_dma: opmode 1\n"));

			/*
			 * See if the interrupt is from the floppy
			 * controller.  If there is, take out the status bytes.
			 */

			if (not_cheerio || (tmp_dcsr & DCSR_INT_PEND)) {

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "fdintr_dma: INT_PEND \n"));

				res = DDI_INTR_CLAIMED;

				to = FD_RRETRY;
				fdc->c_csb.csb_nrslts = 0;

				/* check status */
				i = 0;

				/*
				 * CB turns off once all the result bytes are
				 *  read.
				 *
				 * NOTE: the counters are there so that the
				 * handler will never get stuck in a loop.
				 * If the counters do reach their maximum
				 * values, then a catastrophic error has
				 * occurred.  This should never be the case.
				 * The counters only came into play during
				 * development.
				 */
				while (((tmp = Msr(fdc)) & CB) &&
				    (i < 1000001)) {

					/*
					 * If RQM + DIO, then a result byte
					 * is at hand.
					 */
					if ((tmp & (RQM|DIO|CB)) ==
					    (RQM|DIO|CB)) {
						fdc->c_csb.csb_rslt
						    [fdc->c_csb.csb_nrslts++]
						    = Fifo(fdc);

						FDERRPRINT(FDEP_L1, FDEM_INTR,
						    (C,
						    "fdintr_dma: res 0x%x\n",
						    fdc->c_csb.csb_rslt
						    [fdc->c_csb.csb_nrslts
						    - 1]));

					} else if (--to == 0) {
						/*
						 * controller was never
						 * ready to give results
						 */
						fdc->c_csb.csb_status = 2;
						break;
					}
					i++;
				}
				if (i == 10000) {
					FDERRPRINT(FDEP_L1, FDEM_INTR,
					    (C, "First loop overran\n"));
				}
			}

			/*
			 * See if the interrupt is from the DMA engine,
			 * which will only interrupt on an error
			 */
			if ((!not_cheerio) && (tmp_dcsr & DCSR_ERR_PEND)) {

				res = DDI_INTR_CLAIMED;

				done = 1;
				fdc->c_csb.csb_dcsr_rslt = 1;
				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "fdintr_dma: Error pending\n"));
				reset_dma_controller(fdc);
				set_dma_control_register(fdc, DCSR_INIT_BITS);
				break;
			}

			/* TCBUG kludge */
			if ((fdc->c_fdtype & FDCTYPE_TCBUG) &&
			    ((fdc->c_csb.csb_rslt[0] & IC_SR0) == 0x40) &&
			    (fdc->c_csb.csb_rslt[1] & EN_SR1)) {

				fdc->c_csb.csb_rslt[0] &= ~IC_SR0;

				fdc->c_csb.csb_rslt[1] &= ~EN_SR1;


			}


			/* Exit if there were errors in the DMA */
			if (((fdc->c_csb.csb_rslt[0] & IC_SR0) != 0) ||
			    (fdc->c_csb.csb_rslt[1] != 0) ||
			    (fdc->c_csb.csb_rslt[2] != 0)) {
				done = 1;
				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "fdintr_dma: errors in command\n"));


				break;
			}


			FDERRPRINT(FDEP_L1, FDEM_INTR,
			    (C, "fdintr_dma: dbcr 0x%x\n",
			    get_data_count_register(fdc)));
			/*
			 * The csb_ccount is the number of cookies that still
			 * need to be processed.  A cookie was just processed
			 * so decrement the cookie counter.
			 */
			if (fdc->c_csb.csb_ccount == 0) {
				done = 1;
				break;
			}
			fdc->c_csb.csb_ccount--;
			ccount = fdc->c_csb.csb_ccount;

			windex = fdc->c_csb.csb_windex;

			/*
			 * If there are no more cookies and all the windows
			 * have been DMA'd, then DMA is done.
			 *
			 */
			if ((ccount == 0) && (windex == fdc->c_csb.csb_nwin)) {

				done = 1;

				/*
				 * The handle is unbound in fdexec
				 */

				break;
			}

			if (ccount != 0) {
				/* process the next cookie */
				ddi_dma_nextcookie(fdc->c_dmahandle,
				    &fdc->c_csb.csb_dmacookie);

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "cookie addr 0x%" PRIx64 "\n",
				    fdc->c_csb.csb_dmacookie.dmac_laddress));

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "cookie length %lu\n",
				    fdc->c_csb.csb_dmacookie.dmac_size));

			} else {

				(void) ddi_dma_getwin(fdc->c_dmahandle,
				    fdc->c_csb.csb_windex,
				    &off, &len,
				    &fdc->c_csb.csb_dmacookie,
				    &fdc->c_csb.csb_ccount);
				fdc->c_csb.csb_windex++;

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "fdintr_dma: process %d window\n",
				    fdc->c_csb.csb_windex));

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "fdintr_dma: process no. cookies %d\n",
				    fdc->c_csb.csb_ccount));

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "cookie addr 0x%" PRIx64 "\n",
				    fdc->c_csb.csb_dmacookie.dmac_laddress));

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "cookie length %lu\n",
				    fdc->c_csb.csb_dmacookie.dmac_size));
			}

			/*
			 * Program the DMA engine with the length and
			 * the address of the transfer
			 */

			ASSERT(fdc->c_csb.csb_dmacookie.dmac_size);

			set_data_count_register(fdc,
			    fdc->c_csb.csb_dmacookie.dmac_size);
			set_data_address_register(fdc,
			    fdc->c_csb.csb_dmacookie.dmac_laddress);

			FDERRPRINT(FDEP_L1, FDEM_INTR, (C,
			    "fdintr_dma: size 0x%lx\n",
			    fdc->c_csb.csb_dmacookie.dmac_size));


			/* reprogram the controller */
			fdc->c_csb.csb_cmds[2] = fdc->c_csb.csb_rslt[3];
			fdc->c_csb.csb_cmds[3] = fdc->c_csb.csb_rslt[4];
			fdc->c_csb.csb_cmds[4] = fdc->c_csb.csb_rslt[5];
			fdc->c_csb.csb_cmds[1] = (fdc->c_csb.csb_cmds[1]
			    & ~0x04) | (fdc->c_csb.csb_rslt[4] << 2);

			for (i = 0; i < (int)fdc->c_csb.csb_ncmds; i++) {

				/*
				 * Test the readiness of the controller
				 * to receive the cmd
				 */
				for (to = FD_CRETRY; to; to--) {
					if ((Msr(fdc) & (DIO|RQM)) == RQM)
						break;
				}
				if (to == 0) {
					FDERRPRINT(FDEP_L2, FDEM_EXEC,
					    (C,
					    "fdc: no RQM - stat 0x%x\n",
					    Msr(fdc)));
					/* stop the DMA from happening */
					fdc->c_csb.csb_status = 2;
					done = 1;
					break;
				}

				Set_Fifo(fdc, fdc->c_csb.csb_cmds[i]);

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C,
				    "fdintr_dma: sent 0x%x, Msr 0x%x\n",
				    fdc->c_csb.csb_cmds[i], Msr(fdc)));
			}

			/* reenable DMA */
			if ((!not_cheerio) && (!done))
				set_dma_control_register(fdc, tmp_dcsr |
				    DCSR_EN_DMA);
			break;

		case 0x2:
		/* seek/recal type cmd */
			FDERRPRINT(FDEP_L1, FDEM_INTR,
			    (C, "fintr_dma: opmode 2\n"));

			/*
			 *  See if the interrupt is from the DMA engine,
			 *  which will only interrupt if there was an error.
			 */
			if ((!not_cheerio) && (tmp_dcsr & DCSR_ERR_PEND)) {
				res = DDI_INTR_CLAIMED;
				done = 1;
				fdc->c_csb.csb_dcsr_rslt = 1;
				reset_dma_controller(fdc);
				set_dma_control_register(fdc, DCSR_INIT_BITS);

				break;
			}


			/* See if the interrupt is from the floppy controller */
			if (not_cheerio || (tmp_dcsr & DCSR_INT_PEND)) {

				res = DDI_INTR_CLAIMED;


				/*
				 * Wait until there's no longer a command
				 * in progress
				 */

				FDERRPRINT(FDEP_L1, FDEM_INTR,
				    (C, "fdintr_dma: interrupt pending\n"));
				i = 0;
				while (((Msr(fdc) & CB)) && (i < 10000)) {
					i++;
				}

				if (i == 10000)
					FDERRPRINT(FDEP_L1, FDEM_INTR,
					    (C, "2nd loop overran !!!\n"));

				/*
				 * Check the RQM bit to see if the controller is
				 * ready to transfer status of the command.
				 */
				i = 0;
				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
					i++;
				}

				if (i == 10000)
					FDERRPRINT(FDEP_L1, FDEM_INTR,
					    (C, "3rd loop overran !!!\n"));

				/*
				 * Issue the Sense Interrupt Status Command
				 */
				Set_Fifo(fdc, SNSISTAT);

				i = 0;
				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
					i++;
				}
				if (i == 10000)
					FDERRPRINT(FDEP_L1, FDEM_INTR,
					    (C, "4th loop overran !!!\n"));

				/* Store the first result byte */
				fdc->c_csb.csb_rslt[0] = Fifo(fdc);

				i = 0;
				while ((!(Msr(fdc) & RQM)) && (i < 10000)) {
					i++;
				}
				if (i == 10000)
					FDERRPRINT(FDEP_L1, FDEM_INTR,
					    (C, "5th loop overran !!!\n"));

				/* Store the second  result byte */
				fdc->c_csb.csb_rslt[1] = Fifo(fdc);

				done = 1;
			}

		}

	/*
	 * We are done with the actual interrupt handling here.
	 * The portion below should be actually be done by fd_lointr().
	 * We should be triggering the fd_lointr here and exiting.
	 * However for want of time this will be done in the next FIX.
	 *
	 * Hence for now we will release hilock only and keep the remaining
	 * code as it is.
	 * Releasing of hilock ensures that we don't hold on to the
	 * lolock and hilock at the same time.
	 * hilock is acquired each time dma related  registers are accessed.
	 */
	mutex_exit(&fdc->c_hilock);
	/* Make signal and get out of interrupt handler */
	if (done) {
		mutex_enter(&fdc->c_lolock);

		fdc->c_csb.csb_opmode = 0;

		/*  reset watchdog timer if armed and not already triggered */


		if (fdc->c_timeid) {
			timeout_id_t timeid = fdc->c_timeid;
			fdc->c_timeid = 0;
			mutex_exit(&fdc->c_lolock);
			(void) untimeout(timeid);
			mutex_enter(&fdc->c_lolock);
		}


		if (fdc->c_flags & FDCFLG_WAITING) {
			/*
			 * somebody's waiting on finish of fdctlr/csb,
			 * wake them
			 */

			FDERRPRINT(FDEP_L1, FDEM_INTR,
			    (C, "fdintr_dma: signal the waiter\n"));

			fdc->c_flags ^= FDCFLG_WAITING;
			cv_signal(&fdc->c_iocv);

			/*
			 * FDCFLG_BUSY is NOT cleared, NOR is the csb given
			 * back; the operation just finished can look at the csb
			 */
		} else {
			FDERRPRINT(FDEP_L1, FDEM_INTR,
			    (C, "fdintr_dma: nobody sleeping (%x %x %x)\n",
			    fdc->c_csb.csb_rslt[0], fdc->c_csb.csb_rslt[1],
			    fdc->c_csb.csb_rslt[2]));
		}
		mutex_exit(&fdc->c_lolock);
	}
	/* update high level interrupt counter */
	if (fdc->c_intrstat)
		KIOIP->intrs[KSTAT_INTR_HARD]++;


	FDERRPRINT(FDEP_L1, FDEM_INTR, (C, "fdintr_dma: done\n"));
	return (res);
}

/*
 * fd_lointr
 *	This is the low level SW interrupt handler triggered by the high
 *	level interrupt handler (or by fdwatch).
 */
static uint_t
fd_lointr(caddr_t arg)
{
	struct fdctlr *fdc = (struct fdctlr *)arg;
	struct fdcsb *csb;

	csb = &fdc->c_csb;
	FDERRPRINT(FDEP_L1, FDEM_INTR, (C, "fdintr: opmode %d\n",
	    csb->csb_opmode));
	/*
	 * Check that lowlevel interrupt really meant to trigger us.
	 */
	if (csb->csb_opmode != 4) {
		/*
		 * This should probably be protected, but, what the
		 * heck...the cost isn't worth the accuracy for this
		 * statistic.
		 */
		if (fdc->c_intrstat)
			KIOIP->intrs[KSTAT_INTR_SPURIOUS]++;
		return (DDI_INTR_UNCLAIMED);
	}

	mutex_enter(&fdc->c_lolock);
	csb->csb_opmode = 0;

	/*  reset watchdog timer if armed and not already triggered */
	if (fdc->c_timeid) {
		timeout_id_t timeid = fdc->c_timeid;
		fdc->c_timeid = 0;
		mutex_exit(&fdc->c_lolock);
		(void) untimeout(timeid);
		mutex_enter(&fdc->c_lolock);

	}

	if (fdc->c_flags & FDCFLG_WAITING) {
		/*
		 * somebody's waiting on finish of fdctlr/csb, wake them
		 */
		fdc->c_flags ^= FDCFLG_WAITING;
		cv_signal(&fdc->c_iocv);

		/*
		 * FDCFLG_BUSY is NOT cleared, NOR is the csb given back; so
		 * the operation just finished can look at the csb
		 */
	} else {
		FDERRPRINT(FDEP_L3, FDEM_INTR,
		    (C, "fdintr: nobody sleeping (%x %x %x)\n",
		    csb->csb_rslt[0], csb->csb_rslt[1], csb->csb_rslt[2]));
	}
	if (fdc->c_intrstat)
		KIOIP->intrs[KSTAT_INTR_SOFT]++;
	mutex_exit(&fdc->c_lolock);
	return (DDI_INTR_CLAIMED);
}

/*
 * fdwatch
 *	is called from timein() when a floppy operation has expired.
 */
static void
fdwatch(void *arg)
{
	struct fdctlr *fdc = arg;
	int old_opmode;
	struct fdcsb *csb;

	FDERRPRINT(FDEP_L1, FDEM_WATC, (C, "fdwatch\n"));

	mutex_enter(&fdc->c_lolock);
	if (fdc->c_timeid == 0) {
		/*
		 * fdintr got here first, ergo, no timeout condition..
		 */

		FDERRPRINT(FDEP_L1, FDEM_WATC,
		    (C, "fdwatch: no timeout\n"));

		mutex_exit(&fdc->c_lolock);
		return;
	}
	fdc->c_timeid = 0;
	csb = &fdc->c_csb;

	mutex_enter(&fdc->c_hilock);
	/*
	 * XXXX: We should probably reset the bloody chip
	 */
	old_opmode = csb->csb_opmode;

	FDERRPRINT(FDEP_L1, FDEM_WATC,
	    (C, "fd%d: timeout, opmode:%d\n", csb->csb_unit, old_opmode));

	csb->csb_opmode = 4;
	mutex_exit(&fdc->c_hilock);

	FDERRPRINT(FDEP_L1, FDEM_WATC, (C, "fdwatch: cmd %s timed out\n",
	    fdcmds[csb->csb_cmds[0] & 0x1f].cmdname));
	fdc->c_flags |= FDCFLG_TIMEDOUT;
	csb->csb_status = CSB_CMDTO;

	if ((fdc->c_fdtype & FDCTYPE_DMA) == 0) {
		ddi_trigger_softintr(fdc->c_softid);
		KIOIP->intrs[KSTAT_INTR_WATCHDOG]++;
		mutex_exit(&fdc->c_lolock);
	} else {
		mutex_exit(&fdc->c_lolock);
		(void) fd_lointr((caddr_t)fdctlrs);
	}
}

/*
 * fdgetcsb
 *	wait until the csb is free
 */
static void
fdgetcsb(struct fdctlr *fdc)
{
	FDERRPRINT(FDEP_L1, FDEM_GETC, (C, "fdgetcsb\n"));
	ASSERT(mutex_owned(&fdc->c_lolock));
	while (fdc->c_flags & FDCFLG_BUSY) {
		fdc->c_flags |= FDCFLG_WANT;
		cv_wait(&fdc->c_csbcv, &fdc->c_lolock);
	}
	fdc->c_flags |= FDCFLG_BUSY; /* got it! */
}

/*
 * fdretcsb
 *	return csb
 */
static void
fdretcsb(struct fdctlr *fdc)
{

	ASSERT(mutex_owned(&fdc->c_lolock));
	FDERRPRINT(FDEP_L1, FDEM_RETC, (C, "fdretcsb\n"));
	fdc->c_flags &= ~FDCFLG_BUSY; /* let go */

	fdc->c_csb.csb_read = 0;

	if (fdc->c_flags & FDCFLG_WANT) {
		fdc->c_flags ^= FDCFLG_WANT;
		/*
		 * broadcast the signal.  One thread will wake up and
		 * set the flags to FDCFLG_BUSY.  If more than one thread is
		 * waiting then each thread will wake up in turn.  The first
		 * thread to wake-up will set the FDCFLG_BUSY flag and the
		 * subsequent threads will will wake-up, but reset the
		 * flag to FDCFLG_WANT because the FDCFLG_BUSY bit is set.
		 */
		cv_broadcast(&fdc->c_csbcv);
	}
}


/*
 * fdreset
 *	reset THE controller, and configure it to be
 *	the way it ought to be
 * ASSUMES: that it already owns the csb/fdctlr!
 *
 *	- called with the low level lock held
 */
static int
fdreset(struct fdctlr *fdc)
{
	struct fdcsb *csb;
	clock_t local_lbolt = 0;
	timeout_id_t timeid;

	FDERRPRINT(FDEP_L1, FDEM_RESE, (C, "fdreset\n"));

	ASSERT(mutex_owned(&fdc->c_lolock));

	/* count resets */
	fdc->fdstats.reset++;

	/*
	 * On the 82077, the DSR will clear itself after a reset.  Upon exiting
	 * the reset, a polling interrupt will be generated.  If the floppy
	 * interrupt is enabled, it's possible for cv_signal() to be called
	 * before cv_wait().  This will cause the system to hang.  Turn off
	 * the floppy interrupt to avoid this race condition
	 */
	if ((fdc->c_fdtype & FDCTYPE_CTRLMASK) == FDCTYPE_82077) {
		/*
		 * We need to perform any timeouts before we Reset the
		 * controller. We cannot afford to drop the c_lolock mutex after
		 * Resetting the controller. The reason is that we get a spate
		 * of interrupts until we take the controller out of reset.
		 * The way we avoid this spate of continuous interrupts is by
		 * holding on to the c_lolock and forcing the fdintr_dma routine
		 * to go to sleep waiting for this mutex.
		 */
		/* Do not hold the mutex across the untimeout call */
		timeid = fdc->c_mtimeid;
		fdc->c_mtimeid = 0;
		if (timeid) {
			mutex_exit(&fdc->c_lolock);
			(void) untimeout(timeid);
			mutex_enter(&fdc->c_lolock);
		}
		/* LINTED */
		Set_dor(fdc, DMAGATE, 0);
		FDERRPRINT(FDEP_L1, FDEM_RESE, (C, "fdreset: set dor\n"));
	}

	/* toggle software reset */
	Dsr(fdc, SWR);

	drv_usecwait(5);

	FDERRPRINT(FDEP_L1, FDEM_RESE,
	    (C, "fdreset: toggled software reset\n"));

	/*
	 * This sets the data rate to 500Kbps (for high density)
	 * XXX should use current characteristics instead XXX
	 */
	Dsr(fdc, 0);
	drv_usecwait(5);
	switch (fdc->c_fdtype & FDCTYPE_CTRLMASK) {
	case FDCTYPE_82077:
		/*
		 * when we bring the controller out of reset it will generate
		 * a polling interrupt. fdintr() will field it and schedule
		 * fd_lointr(). There will be no one sleeping but we are
		 * expecting an interrupt so....
		 */
		fdc->c_flags |= FDCFLG_WAITING;

		/*
		 * The reset bit must be cleared to take the 077 out of
		 * reset state and the DMAGATE bit must be high to enable
		 * interrupts.
		 */
		/* LINTED */
		Set_dor(fdc, DMAGATE|RESET, 1);

		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdattach: Dor 0x%x\n", Dor(fdc)));

		local_lbolt = ddi_get_lbolt();
		if (cv_timedwait(&fdc->c_iocv, &fdc->c_lolock,
		    local_lbolt + drv_usectohz(1000000)) == -1) {
			return (-1);
		}
		break;

	default:
		fdc->c_flags |= FDCFLG_WAITING;

		/*
		 * A timed wait is not used because it's possible for the timer
		 * to go off before the controller has a chance to interrupt.
		 */
		cv_wait(&fdc->c_iocv, &fdc->c_lolock);
		break;
	}
	csb = &fdc->c_csb;

	/* setup common things in csb */
	csb->csb_unit = fdc->c_un->un_unit_no;
	csb->csb_nrslts = 0;
	csb->csb_opflags = CSB_OFNORESULTS;
	csb->csb_maxretry = 0;
	csb->csb_retrys = 0;

	csb->csb_read = CSB_NULL;

	/* send SPECIFY command to fdc */
	/* csb->unit is don't care */
	csb->csb_cmds[0] = FDRAW_SPECIFY;
	csb->csb_cmds[1] = fdspec[0]; /* step rate, head unload time */
	if (fdc->c_fdtype & FDCTYPE_DMA)
		csb->csb_cmds[2] =  SPEC_DMA_MODE;
	else
		csb->csb_cmds[2] = fdspec[1];  /* head load time, DMA mode */

	csb->csb_ncmds = 3;

	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
	(void) fdexec(fdc, 0);	/* no FDXC_CHECKCHG, ... */
	/* no results */

	/* send CONFIGURE command to fdc */
	/* csb->unit is don't care */
	csb->csb_cmds[0] = CONFIGURE;
	csb->csb_cmds[1] = fdconf[0]; /* motor info, motor delays */
	csb->csb_cmds[2] = fdconf[1]; /* enaimplsk, disapoll, fifothru */
	csb->csb_cmds[3] = fdconf[2]; /* track precomp */
	csb->csb_ncmds = 4;

	csb->csb_read = CSB_NULL;

	csb->csb_retrys = 0;

	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
	(void) fdexec(fdc, 0); /* no FDXC_CHECKCHG, ... */
	return (0);
}

/*
 * fdrecalseek
 *	performs recalibrates or seeks if the "arg" is -1 does a
 *	recalibrate on a drive, else it seeks to the cylinder of
 *	the drive.  The recalibrate is also used to find a drive,
 *	ie if the drive is not there, the controller says "error"
 *	on the operation
 * NOTE: that there is special handling of this operation in the hardware
 * interrupt routine - it causes the operation to appear to have results;
 * ie the results of the SENSE INTERRUPT STATUS that the hardware interrupt
 * function did for us.
 * NOTE: because it uses sleep/wakeup it must be protected in a critical
 * section so create one before calling it!
 *
 * RETURNS: 0 for ok,
 *	else	errno from fdexec,
 *	or	ENODEV if error (infers hardware type error)
 *
 *	- called with the low level lock held
 */
static int
fdrecalseek(struct fdctlr *fdc, int unit, int arg, int execflg)
{
	struct fdcsb *csb;
	int result;

	ASSERT(fdc->c_un->un_unit_no == unit);

	FDERRPRINT(FDEP_L1, FDEM_RECA, (C, "fdrecalseek to %d\n", arg));

	/* XXX TODO: check see argument for <= num cyls OR < 256 */

	csb = &fdc->c_csb;
	csb->csb_unit = (uchar_t)unit;
	csb->csb_cmds[1] = unit & 0x03;

	if (arg == -1) {			/* is recal... */
		csb->csb_cmds[0] = FDRAW_REZERO;
		csb->csb_ncmds = 2;
	} else {
		csb->csb_cmds[0] = FDRAW_SEEK;
		csb->csb_cmds[2] = (uchar_t)arg;
		csb->csb_ncmds = 3;
	}
	csb->csb_nrslts = 2;	/* 2 for SENSE INTERRUPTS */
	csb->csb_opflags = CSB_OFSEEKOPS | CSB_OFTIMEIT;
	/*
	 * MAYBE NYD need to set retries to different values? - depending on
	 * drive characteristics - if we get to high capacity drives
	 */
	csb->csb_maxretry = skretry;
	csb->csb_retrys = 0;

	/* send cmd off to fdexec */
	if (result = fdexec(fdc, FDXC_SLEEP | execflg)) {
		goto out;
	}

	/*
	 * if recal, test for equipment check error
	 * ASSUMES result = 0 from above call
	 */
	if (arg == -1) {
		result = 0;
	} else {
		/* for seeks, any old error will do */
		if ((csb->csb_rslt[0] & IC_SR0) || csb->csb_cmdstat)
			result = ENODEV;
	}

out:
	return (result);
}

/*
 * fdsensedrv
 *	do a sense_drive command.  used by fdopen and fdcheckdisk.
 *
 *	- called with the lock held
 */
static int
fdsensedrv(struct fdctlr *fdc, int unit)
{
	struct fdcsb *csb;

	ASSERT(fdc->c_un->un_unit_no == unit);

	csb = &fdc->c_csb;

	/* setup common things in csb */
	csb->csb_unit = (uchar_t)unit;
	csb->csb_opflags = CSB_OFIMMEDIATE;
	csb->csb_cmds[0] = FDRAW_SENSE_DRV;
	/* MOT bit set means don't delay */
	csb->csb_cmds[1] = MOT | (unit & 0x03);
	csb->csb_ncmds = 2;
	csb->csb_nrslts = 1;
	csb->csb_maxretry = skretry;
	csb->csb_retrys = 0;

	/* XXX for now ignore errors, they "CAN'T HAPPEN" */
	(void) fdexec(fdc, 0);	/* DON't check changed!, no sleep */

	FDERRPRINT(FDEP_L1, FDEM_CHEK,
	    (C, "fdsensedrv: result 0x%x", csb->csb_rslt[0]));

	return (csb->csb_rslt[0]); /* return status byte 3 */
}

/*
 * fdcheckdisk
 *	check to see if the disk is still there - do a recalibrate,
 *	then see if DSKCHG line went away, if so, diskette is in; else
 *	it's (still) out.
 */

static int
fdcheckdisk(struct fdctlr *fdc, int unit)
{
	auto struct fdcsb savecsb;
	struct fdcsb *csb;
	int	err, st3;
	int	seekto;			/* where to seek for reset of DSKCHG */

	FDERRPRINT(FDEP_L1, FDEM_CHEK,
	    (C, "fdcheckdisk, unit %d\n", unit));

	ASSERT(fdc->c_un->un_unit_no == unit);

	/*
	 * save old csb
	 */

	csb = &fdc->c_csb;
	savecsb = fdc->c_csb;
	bzero((caddr_t)csb, sizeof (*csb));

	/*
	 * Read drive status to see if at TRK0, if so, seek to cyl 1,
	 * else seek to cyl 0.	We do this because the controller is
	 * "smart" enough to not send any step pulses (which are how
	 * the DSKCHG line gets reset) if it sees TRK0 'cause it
	 * knows the drive is already recalibrated.
	 */
	st3 = fdsensedrv(fdc, unit);

	/* check TRK0 bit in status */
	if (st3 & T0_SR3)
		seekto = 1;	/* at TRK0, seek out */
	else
		seekto = 0;

	/*
	 * DON'T recurse check changed
	 */
	err = fdrecalseek(fdc, unit, seekto, 0);

	/* "restore" old csb, check change state */
	fdc->c_csb = savecsb;

	/* any recal/seek errors are too serious to attend to */
	if (err) {
		FDERRPRINT(FDEP_L2, FDEM_CHEK,
		    (C, "fdcheckdisk err %d\n", err));
		return (err);
	}

	/*
	 * if disk change still asserted, no diskette in drive!
	 */
	if (fdsense_chng(fdc, csb->csb_unit)) {
		FDERRPRINT(FDEP_L2, FDEM_CHEK,
		    (C, "fdcheckdisk no disk\n"));
		return (1);
	}
	return (0);
}

/*
 *	fdselect() - select drive, needed for external to chip select logic
 *	fdeject() - ejects drive, must be previously selected
 *	fdsense_chng() - sense disk changed line from previously selected drive
 *		return s 1 is signal asserted, else 0
 */
/* ARGSUSED */
static void
fdselect(struct fdctlr *fdc, int unit, int on)
{

	ASSERT(fdc->c_un->un_unit_no == unit);

	FDERRPRINT(FDEP_L1, FDEM_DSEL,
	    (C, "fdselect, unit %d, on = %d\n", unit, on));

	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
	case FDCTYPE_MACHIO:
		set_auxioreg(AUX_DRVSELECT, on);
		break;

	case FDCTYPE_SLAVIO:
	case FDCTYPE_CHEERIO:
		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdselect: (before) Dor 0x%x\n", Dor(fdc)));

		if (unit == 0) {
			Set_dor(fdc, DRVSEL, !on);
		} else {
			Set_dor(fdc, DRVSEL, on);
		}

		FDERRPRINT(FDEP_L1, FDEM_ATTA,
		    (C, "fdselect: Dor 0x%x\n", Dor(fdc)));

		break;

	default:
		break;
	}
}

/* ARGSUSED */
static void
fdeject(struct fdctlr *fdc, int unit)
{
	struct fdunit *un;

	ASSERT(fdc->c_un->un_unit_no == unit);

	un = fdc->c_un;

	FDERRPRINT(FDEP_L1, FDEM_EJEC, (C, "fdeject\n"));
	/*
	 * assume delay of function calling sufficient settling time
	 * eject line is NOT driven by inverter so it is true low
	 */
	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
	case FDCTYPE_MACHIO:
		set_auxioreg(AUX_EJECT, 0);
		drv_usecwait(2);
		set_auxioreg(AUX_EJECT, 1);
		break;

	case FDCTYPE_SLAVIO:
		if (!(Dor(fdc) & MOTEN(unit))) {
			/* LINTED */
			Set_dor(fdc, MOTEN(unit), 1);
		}
		drv_usecwait(2);	/* just to settle */
		/* LINTED */
		Set_dor(fdc, EJECT, 1);
		drv_usecwait(2);
		/* LINTED */
		Set_dor(fdc, EJECT, 0);
		break;
	case FDCTYPE_CHEERIO:
		if (!(Dor(fdc) & MOTEN(unit))) {
			/* LINTED */
			Set_dor(fdc, MOTEN(unit), 1);
		}
		drv_usecwait(2);	/* just to settle */
		/* LINTED */
		Set_dor(fdc, EJECT_DMA, 1);
		drv_usecwait(2);
		/* LINTED */
		Set_dor(fdc, EJECT_DMA, 0);
		break;
	}
	/*
	 * XXX set ejected state?
	 */
	un->un_ejected = 1;
}

/* ARGSUSED */
static int
fdsense_chng(struct fdctlr *fdc, int unit)
{
	int changed = 0;

	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "fdsense_chng:start\n"));

	ASSERT(fdc->c_un->un_unit_no == unit);

	/*
	 * Do not turn on the motor of a pollable drive
	 */
	if (fd_pollable) {
	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "pollable: don't turn on motor\n"));
		/*
		 * Invert the sense of the DSKCHG for pollable drives
		 */
		if (Dir(fdc) & DSKCHG)
			changed = 0;
		else
			changed = 1;

		return (changed);
	}

	switch (fdc->c_fdtype & FDCTYPE_AUXIOMASK) {
	case FDCTYPE_MACHIO:
		if (*fdc->c_auxiova & AUX_DISKCHG)
			changed = 1;
		break;

	case FDCTYPE_SB:
	case FDCTYPE_SLAVIO:
	case FDCTYPE_CHEERIO:
		if (!(Dor(fdc) & MOTEN(unit))) {
			/* LINTED */
			Set_dor(fdc, MOTEN(unit), 1);
		}
		drv_usecwait(2);	/* just to settle */
		if (Dir(fdc) & DSKCHG)
			changed = 1;
		break;
	}

	FDERRPRINT(FDEP_L1, FDEM_SCHG, (C, "fdsense_chng:end\n"));

	return (changed);
}

/*
 *	if it can read a valid label it does so, else it will use a
 *	default.  If it can`t read the diskette - that is an error.
 *
 * RETURNS: 0 for ok - meaning that it could at least read the device,
 *	!0 for error XXX TBD NYD error codes
 *
 *	- called with the low level lock held
 */
static int
fdgetlabel(struct fdctlr *fdc, int unit)
{
	struct dk_label *label = NULL;
	struct fdunit *un;
	short *sp;
	short count;
	short xsum;			/* checksum */
	int	i, tries;
	int	err = 0;
	short	oldlvl;

	FDERRPRINT(FDEP_L1, FDEM_GETL,
	    (C, "fdgetlabel: unit %d\n", unit));

	un = fdc->c_un;
	un->un_flags &= ~(FDUNIT_UNLABELED);

	ASSERT(fdc->c_un->un_unit_no == unit);

	/* Do not print errors since this is a private cmd */

	oldlvl = fderrlevel;


	fderrlevel = FDEP_L4;

	label = (struct dk_label *)
	    kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);

	/*
	 * try different characteristics (ie densities) by attempting to read
	 * from the diskette.  The diskette may not be present or
	 * is unformatted.
	 *
	 * First, the last sector of the first track is read.  If this
	 * passes, attempt to read the last sector + 1 of the first track.
	 * For example, for a high density diskette, sector 18 is read.  If
	 * the diskette is high density, this will pass.  Next, try to
	 * read sector 19 of the first track.  This should fail.  If it
	 * passes, this is not a high density diskette.  Finally, read
	 * the first sector which should contain a label.
	 *
	 * if un->un_curfdtype is -1 then the current characteristics
	 * were set by FDIOSCHAR and need to try it as well as everything
	 * in the table
	 */
	if (un->un_curfdtype == -1) {
		tries = nfdtypes+1;
		FDERRPRINT(FDEP_L1, FDEM_GETL,
		    (C, "fdgetl: un_curfdtype is -1\n"));

	} else {
		tries = nfdtypes;

		/* Always start with the highest density (1.7MB) */
		un->un_curfdtype = 0;
		*(un->un_chars) = fdtypes[un->un_curfdtype];
	}

	FDERRPRINT(FDEP_L1, FDEM_GETL,
	    (C, "fdgetl: no. of tries %d\n", tries));
	FDERRPRINT(FDEP_L1, FDEM_GETL,
	    (C, "fdgetl: no. of curfdtype %d\n", un->un_curfdtype));

	for (i = 0; i < tries; i++) {
		FDERRPRINT(FDEP_L1, FDEM_GETL,
		    (C, "fdgetl: trying %d\n", i));

		if (!(err = fdrw(fdc, unit, FDREAD, 0, 0,
		    un->un_chars->fdc_secptrack, (caddr_t)label,
		    sizeof (struct dk_label))) &&

		    fdrw(fdc, unit, FDREAD, 0, 0,
		    un->un_chars->fdc_secptrack + 1,
		    (caddr_t)label, sizeof (struct dk_label)) &&

		    !(err = fdrw(fdc, unit, FDREAD, 0, 0, 1, (caddr_t)label,
		    sizeof (struct dk_label)))) {

			FDERRPRINT(FDEP_L1, FDEM_GETL,
				(C, "fdgetl: succeeded\n"));

			break;
		}

		/*
		 * try the next entry in the characteristics tbl
		 * If curfdtype is -1, the nxt entry in tbl is 0 (the first).
		 */

		un->un_curfdtype = (un->un_curfdtype + 1) % nfdtypes;
		*(un->un_chars) = fdtypes[un->un_curfdtype];


	}

	/* print errors again */
	fderrlevel = oldlvl;

	/* Couldn't read anything */
	if (err) {

		/* The default characteristics are high density (1.4MB) */
		un->un_curfdtype = 1;
		*(un->un_chars) = fdtypes[un->un_curfdtype];

		fdunpacklabel(&fdlbl_high_80, &un->un_label);

		FDERRPRINT(FDEP_L1, FDEM_GETL,
		    (C, "fdgetl: Can't autosense diskette\n"));

		goto out;
	}

	FDERRPRINT(FDEP_L1, FDEM_GETL,
	    (C, "fdgetl: fdtype=%d !!!\n", un->un_curfdtype));
	FDERRPRINT(FDEP_L1, FDEM_GETL,
	    (C, "fdgetl: rate=%d ssize=%d !!!\n",
	    un->un_chars->fdc_transfer_rate, un->un_chars->fdc_sec_size));

	/*
	 * _something_ was read	 -  look for unixtype label
	 */
	if (label->dkl_magic != DKL_MAGIC) {

		/*
		 * The label isn't a unix label.  However, the diskette
		 * is formatted because we were able to read the first
		 * cylinder.
		 */

		FDERRPRINT(FDEP_L1, FDEM_GETL,
		    (C, "fdgetl: not unix label\n"));

		goto nolabel;
	}

	/*
	 * Checksum the label
	 */
	count = sizeof (struct dk_label)/sizeof (short);
	sp = (short *)label;
	xsum = 0;
	while (count--)
		xsum ^= *sp++;	/* should add up to 0 */
	if (xsum) {

		/*
		 * The checksum fails.  However, the diskette is formatted
		 * because we were able to read the first cylinder
		 */

		FDERRPRINT(FDEP_L1, FDEM_GETL,
		    (C, "fdgetl: bad cksum\n"));

		goto nolabel;
	}

	/*
	 * The diskette has a unix label with a correct checksum.
	 * Copy the label into the unit structure
	 */
	un->un_label = *label;

	goto out;

nolabel:
	/*
	 * The diskette doesn't have a correct unix label, but it is formatted.
	 * Use a default label according to the diskette's density
	 * (mark default used)
	 */
	FDERRPRINT(FDEP_L1, FDEM_GETL,
	    (C, "fdgetlabel: unit %d\n", unit));
	un->un_flags |= FDUNIT_UNLABELED;
	switch (un->un_chars->fdc_secptrack) {
	case 9:
		fdunpacklabel(&fdlbl_low_80, &un->un_label);
		break;
	case 8:
		fdunpacklabel(&fdlbl_medium_80, &un->un_label);
		break;
	case 18:
		fdunpacklabel(&fdlbl_high_80, &un->un_label);
		break;
	case 21:
		fdunpacklabel(&fdlbl_high_21, &un->un_label);
		break;
	default:
		fdunpacklabel(&fdlbl_high_80, &un->un_label);
		break;
	}

out:
	if (label != NULL)
		kmem_free((caddr_t)label, sizeof (struct dk_label));
	return (err);
}

/*
 * fdrw- used only for reading labels  and for DKIOCSVTOC ioctl
 *	 which reads the 1 sector.
 */
static int
fdrw(struct fdctlr *fdc, int unit, int rw, int cyl, int head,
    int sector, caddr_t bufp, uint_t len)
{
	struct fdcsb *csb;
	struct	fd_char *ch;
	int	cmdresult = 0;
	caddr_t dma_addr;
	size_t	real_length;
	int	res;
	ddi_device_acc_attr_t attr;
	ddi_acc_handle_t	mem_handle = NULL;

	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw\n"));

	ASSERT(fdc->c_un->un_unit_no == unit);

	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);

	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
		mutex_exit(&fdc->c_lolock);
		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
		    != DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
			    failed. \n"));
			mutex_enter(&fdc->c_lolock);
			return (EIO);
		}

		mutex_enter(&fdc->c_lolock);
	}

	fdgetcsb(fdc);
	csb = &fdc->c_csb;
	ch = fdc->c_un->un_chars;
	if (rw == FDREAD) {
		if (fdc->c_fdtype & FDCTYPE_TCBUG) {
			/*
			 * kludge for lack of Multitrack functionality
			 */
			csb->csb_cmds[0] = SK + FDRAW_RDCMD;
		} else
			csb->csb_cmds[0] = MT + SK + FDRAW_RDCMD;
	} else { /* write */
		if (fdc->c_fdtype & FDCTYPE_TCBUG) {
			/*
			 * kludge for lack of Multitrack functionality
			 */
			csb->csb_cmds[0] = FDRAW_WRCMD;
		} else
			csb->csb_cmds[0] = MT + FDRAW_WRCMD;
	}

	if (rw == FDREAD)
		fdc->c_csb.csb_read = CSB_READ;
	else
		fdc->c_csb.csb_read = CSB_WRITE;

	/* always or in MFM bit */
	csb->csb_cmds[0] |= MFM;
	csb->csb_cmds[1] = (uchar_t)(unit | ((head & 0x1) << 2));
	if (fdc->c_fdtype & FDCTYPE_SB)
		csb->csb_cmds[1] |= IPS;
	csb->csb_cmds[2] = (uchar_t)cyl;
	csb->csb_cmds[3] = (uchar_t)head;
	csb->csb_cmds[4] = (uchar_t)sector;
	csb->csb_cmds[5] = ch->fdc_medium ? 3 : 2; /* sector size code */
	/*
	 * kludge for end-of-cylinder error.
	 */
	if (fdc->c_fdtype & FDCTYPE_TCBUG)
		csb->csb_cmds[6] = sector + (len / ch->fdc_sec_size) - 1;
	else
		csb->csb_cmds[6] =
		    (uchar_t)max(fdc->c_un->un_chars->fdc_secptrack, sector);
	csb->csb_len = len;
	csb->csb_cmds[7] = GPLN;
	csb->csb_cmds[8] = SSSDTL;
	csb->csb_ncmds = NCBRW;
	csb->csb_len = len;
	csb->csb_maxretry = 2;
	csb->csb_retrys = 0;
	bzero(csb->csb_rslt, NRBRW);
	csb->csb_nrslts = NRBRW;
	csb->csb_opflags = CSB_OFXFEROPS | CSB_OFTIMEIT;

	/* If platform supports DMA, set up DMA resources */
	if (fdc->c_fdtype & FDCTYPE_DMA) {

		mutex_enter(&fdc->c_hilock);

		attr.devacc_attr_version = DDI_DEVICE_ATTR_V0;
		attr.devacc_attr_endian_flags  = DDI_STRUCTURE_BE_ACC;
		attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;

		res = ddi_dma_mem_alloc(fdc->c_dmahandle, len,
		    &attr, DDI_DMA_STREAMING,
		    DDI_DMA_DONTWAIT, 0, &dma_addr, &real_length,
		    &mem_handle);

		if (res != DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_RW,
			    (C, "fdrw: dma mem alloc failed\n"));

			fdretcsb(fdc);
			mutex_exit(&fdc->c_hilock);
			return (EIO);
		}

		FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: allocated memory"));

		if (fdstart_dma(fdc, dma_addr, len) != 0) {
			fdretcsb(fdc);
			ddi_dma_mem_free(&mem_handle);
			mutex_exit(&fdc->c_hilock);
			return (-1);

		}

		/*
		 * If the command is a write, copy the data to be written to
		 * dma_addr.
		 */

		if (fdc->c_csb.csb_read == CSB_WRITE) {
			bcopy((char *)bufp, (char *)dma_addr, len);
		}

		csb->csb_addr = dma_addr;
		mutex_exit(&fdc->c_hilock);
	} else {
		csb->csb_addr = bufp;
	}


	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: call fdexec\n"));

	if (fdexec(fdc, FDXC_SLEEP | FDXC_CHECKCHG) != 0) {
		fdretcsb(fdc);

		if (mem_handle)
			ddi_dma_mem_free(&mem_handle);

		return (EIO);

	}

	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fdrw: fdexec returned\n"));

	/*
	 * if DMA was used and the command was a read
	 * copy the results into bufp
	 */
	if (fdc->c_fdtype & FDCTYPE_DMA) {
		if (fdc->c_csb.csb_read == CSB_READ) {
			bcopy((char *)dma_addr, (char *)bufp, len);
		}
		ddi_dma_mem_free(&mem_handle);
	}

	if (csb->csb_cmdstat)
		cmdresult = EIO;	/* XXX TBD NYD for now */

	fdretcsb(fdc);
	return (cmdresult);
}

/*
 * fdunpacklabel
 *	this unpacks a (packed) struct dk_label into a standard dk_label.
 */
static void
fdunpacklabel(struct packed_label *from, struct dk_label *to)
{
	FDERRPRINT(FDEP_L1, FDEM_PACK, (C, "fdpacklabel\n"));
	bzero((caddr_t)to, sizeof (*to));
	bcopy((caddr_t)&from->dkl_vname, (caddr_t)to->dkl_asciilabel,
	    sizeof (to->dkl_asciilabel));
	to->dkl_rpm = from->dkl_rpm;	/* rotations per minute */
	to->dkl_pcyl = from->dkl_pcyl;	/* # physical cylinders */
	to->dkl_apc = from->dkl_apc;	/* alternates per cylinder */
	to->dkl_intrlv = from->dkl_intrlv;	/* interleave factor */
	to->dkl_ncyl = from->dkl_ncyl;	/* # of data cylinders */
	to->dkl_acyl = from->dkl_acyl;	/* # of alternate cylinders */
	to->dkl_nhead = from->dkl_nhead; /* # of heads in this partition */
	to->dkl_nsect = from->dkl_nsect; /* # of 512 byte sectors per track */
	/* logical partitions */
	bcopy((caddr_t)from->dkl_map, (caddr_t)to->dkl_map,
	    sizeof (struct dk_map32) * NDKMAP);
	to->dkl_vtoc = from->dkl_vtoc;
}

static struct fdctlr *
fd_getctlr(dev_t dev)
{

	struct fdctlr *fdc = fdctlrs;
	int ctlr = FDCTLR(dev);

	while (fdc) {
		if (ddi_get_instance(fdc->c_dip) == ctlr)
			return (fdc);
		fdc = fdc->c_next;
	}
	return (fdc);
}

static int
fd_unit_is_open(struct fdunit *un)
{
	int i;
	for (i = 0; i < NDKMAP; i++)
		if (un->un_lyropen[i])
			return (1);
	for (i = 0; i < OTYPCNT - 1; i++)
		if (un->un_regopen[i])
			return (1);
	return (0);
}

/*
 * Return the a vtoc structure in *vtoc.
 * The vtoc is built from information in
 * the diskette's label.
 */
static void
fd_build_user_vtoc(struct fdunit *un, struct vtoc *vtoc)
{
	int i;
	int nblks;			/* DEV_BSIZE sectors per cylinder */
	struct dk_map2 *lpart;
	struct dk_map32	*lmap;
	struct partition *vpart;

	bzero(vtoc, sizeof (struct vtoc));

	/* Initialize info. needed by mboot.  (unsupported) */
	vtoc->v_bootinfo[0] = un->un_label.dkl_vtoc.v_bootinfo[0];
	vtoc->v_bootinfo[1] = un->un_label.dkl_vtoc.v_bootinfo[1];
	vtoc->v_bootinfo[2] = un->un_label.dkl_vtoc.v_bootinfo[2];

	/* Fill in vtoc sanity and version information */
	vtoc->v_sanity		= un->un_label.dkl_vtoc.v_sanity;
	vtoc->v_version		= un->un_label.dkl_vtoc.v_version;

	/* Copy the volume name */
	bcopy(un->un_label.dkl_vtoc.v_volume,
	    vtoc->v_volume, LEN_DKL_VVOL);

	/*
	 * The dk_map32 structure is based on DEV_BSIZE byte blocks.
	 * However, medium density diskettes have 1024 byte blocks.
	 * The number of sectors per partition listed in the dk_map32 structure
	 * accounts for this by multiplying the number of 1024 byte
	 * blocks by 2.  (See the packed_label initializations.)  The
	 * 1024 byte block size can not be listed for medium density
	 * diskettes because the kernel is hard coded for DEV_BSIZE
	 * blocks.
	 */
	vtoc->v_sectorsz = DEV_BSIZE;
	vtoc->v_nparts = un->un_label.dkl_vtoc.v_nparts;

	/* Copy the reserved space */
	bcopy(un->un_label.dkl_vtoc.v_reserved,
	    vtoc->v_reserved, sizeof (un->un_label.dkl_vtoc.v_reserved));
	/*
	 * Convert partitioning information.
	 *
	 * Note the conversion from starting cylinder number
	 * to starting sector number.
	 */
	lmap = un->un_label.dkl_map;
	lpart = un->un_label.dkl_vtoc.v_part;
	vpart = vtoc->v_part;

	nblks = (un->un_chars->fdc_nhead * un->un_chars->fdc_secptrack *
	    un->un_chars->fdc_sec_size) / DEV_BSIZE;

	for (i = 0; i < V_NUMPAR; i++) {
		vpart->p_tag	= lpart->p_tag;
		vpart->p_flag	= lpart->p_flag;
		vpart->p_start	= lmap->dkl_cylno * nblks;
		vpart->p_size	= lmap->dkl_nblk;

		lmap++;
		lpart++;
		vpart++;
	}

	/* Initialize timestamp and label */
	bcopy(un->un_label.dkl_vtoc.v_timestamp,
	    vtoc->timestamp, sizeof (vtoc->timestamp));

	bcopy(un->un_label.dkl_asciilabel,
	    vtoc->v_asciilabel, LEN_DKL_ASCII);
}

/*
 * Build a label out of a vtoc structure.
 */
static int
fd_build_label_vtoc(struct fdunit *un, struct vtoc *vtoc)
{
	struct dk_map32		*lmap;
	struct dk_map2		*lpart;
	struct partition	*vpart;
	int			nblks;	/* no. blocks per cylinder */
	int			ncyl;
	int			i;
	short	 sum, *sp;

	/* Sanity-check the vtoc */
	if ((vtoc->v_sanity != VTOC_SANE) ||
	    (vtoc->v_nparts > NDKMAP) || (vtoc->v_nparts <= 0)) {
		FDERRPRINT(FDEP_L1, FDEM_IOCT,
		    (C, "fd_build_label:  sanity check on vtoc failed\n"));
		return (EINVAL);
	}

	nblks = (un->un_chars->fdc_nhead * un->un_chars->fdc_secptrack *
	    un->un_chars->fdc_sec_size) / DEV_BSIZE;

	vpart = vtoc->v_part;

	/*
	 * Check the partition information in the vtoc.  The starting sectors
	 * must lie along partition boundaries. (NDKMAP entries are checked
	 * to ensure that the unused entries are set to 0 if vtoc->v_nparts
	 * is less than NDKMAP)
	 */

	for (i = 0; i < NDKMAP; i++) {
		if ((vpart->p_start % nblks) != 0) {
			return (EINVAL);
		}
		ncyl = vpart->p_start % nblks;
		ncyl += vpart->p_size % nblks;
		if ((vpart->p_size % nblks) != 0)
			ncyl++;
		if (ncyl > un->un_chars->fdc_ncyl) {
			return (EINVAL);
		}
		vpart++;
	}

	/*
	 * reinitialize the existing label
	 */
	bzero(&un->un_label, sizeof (un->un_label));

	/* Put appropriate vtoc structure fields into the disk label */
	un->un_label.dkl_vtoc.v_bootinfo[0] = (uint32_t)vtoc->v_bootinfo[0];
	un->un_label.dkl_vtoc.v_bootinfo[1] = (uint32_t)vtoc->v_bootinfo[1];
	un->un_label.dkl_vtoc.v_bootinfo[2] = (uint32_t)vtoc->v_bootinfo[2];

	un->un_label.dkl_vtoc.v_sanity = vtoc->v_sanity;
	un->un_label.dkl_vtoc.v_version = vtoc->v_version;

	bcopy(vtoc->v_volume, un->un_label.dkl_vtoc.v_volume, LEN_DKL_VVOL);

	un->un_label.dkl_vtoc.v_nparts = vtoc->v_nparts;

	bcopy(vtoc->v_reserved, un->un_label.dkl_vtoc.v_reserved,
	    sizeof (un->un_label.dkl_vtoc.v_reserved));

	/*
	 * Initialize cylinder information in the label.
	 * Note the conversion from starting sector number
	 * to starting cylinder number.
	 * Return error if division results in a remainder.
	 */
	lmap = un->un_label.dkl_map;
	lpart = un->un_label.dkl_vtoc.v_part;
	vpart = vtoc->v_part;

	for (i = 0; i < (int)vtoc->v_nparts; i++) {
		lpart->p_tag  = vtoc->v_part[i].p_tag;
		lpart->p_flag = vtoc->v_part[i].p_flag;
		lmap->dkl_cylno = vpart->p_start / nblks;
		lmap->dkl_nblk = vpart->p_size;

		lmap++;
		lpart++;
		vpart++;
	}

	/* Copy the timestamp and ascii label */
	for (i = 0; i < NDKMAP; i++) {
		un->un_label.dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
	}


	bcopy(vtoc->v_asciilabel, un->un_label.dkl_asciilabel, LEN_DKL_ASCII);

	FDERRPRINT(FDEP_L1, FDEM_IOCT,
	    (C, "fd_build_label: asciilabel %s\n",
	    un->un_label.dkl_asciilabel));

	/* Initialize the magic number */
	un->un_label.dkl_magic = DKL_MAGIC;

	un->un_label.dkl_pcyl = un->un_chars->fdc_ncyl;

	/*
	 * The fdc_secptrack filed of the fd_char structure is the number
	 * of sectors per track where the sectors are fdc_sec_size.  The
	 * dkl_nsect field of the dk_label structure is the number of
	 * 512 (DEVBSIZE) byte sectors per track.
	 */
	un->un_label.dkl_nsect = (un->un_chars->fdc_secptrack *
	    un->un_chars->fdc_sec_size) / DEV_BSIZE;


	un->un_label.dkl_ncyl = un->un_label.dkl_pcyl;
	un->un_label.dkl_nhead = un->un_chars->fdc_nhead;
	un->un_label.dkl_rpm = un->un_chars->fdc_medium ? 360 : 300;
	un->un_label.dkl_intrlv = 1;

	/* Create the checksum */
	sum = 0;
	un->un_label.dkl_cksum = 0;
	sp = (short *)&un->un_label;
	i = sizeof (struct dk_label)/sizeof (short);
	while (i--) {
		sum ^= *sp++;
	}
	un->un_label.dkl_cksum = sum;

	return (0);
}

/*
 * Check for auxio register node
 */

int
fd_isauxiodip(dev_info_t *dip)
{
	if (strcmp(ddi_get_name(dip), "auxio") == 0 ||
	    strcmp(ddi_get_name(dip), "auxiliary-io") == 0) {
		return (1);
	}
	return (0);
}

/*
 * Search for auxio register node, then for address property
 */

caddr_t
fd_getauxiova(dev_info_t *dip)
{
	dev_info_t *auxdip;
	caddr_t addr;

	/*
	 * Search sibling list, which happens to be safe inside attach
	 */
	auxdip = ddi_get_child(ddi_get_parent(dip));
	while (auxdip) {
		if (fd_isauxiodip(auxdip))
			break;
		auxdip = ddi_get_next_sibling(auxdip);
	}

	if (auxdip == NULL)
		return (NULL);

	addr = (caddr_t)(uintptr_t)(caddr32_t)ddi_getprop(DDI_DEV_T_ANY,
	    auxdip, DDI_PROP_DONTPASS, "address", 0);

	return (addr);
}


/*
 * set_rotational speed
 * 300 rpm for high and low density.
 * 360 rpm for medium density.
 * for now, we assume that 3rd density is supported only for Sun4M,
 * not for Clones. (else we would have to check for 82077, and do
 * specific things for the MEDIUM_DENSITY BIT for clones.
 * this code should not break CLONES.
 *
 * REMARK: there is a SOny requirement, to deselect the drive then
 * select it again after the medium density change, since the
 * leading edge of the select line latches the rotational Speed.
 * then after that, we have to wait 500 ms for the rotation to
 * stabilize.
 *
 */
static void
set_rotational_speed(struct fdctlr *fdc, int unit)
{
	int check;
	int is_medium;

	ASSERT(fdc->c_un->un_unit_no == unit);

	/*
	 * if we do not have a Sun4m, medium density is not supported.
	 */
	if (fdc->c_fdtype & FDCTYPE_MACHIO)
		return;

	/*
	 * if FDUNIT_SET_SPEED is set, set the speed.
	 * else,
	 *	if there is a change, do it, if not leave it alone.
	 *	there is a change if un->un_chars->fdc_medium does not match
	 *	un->un_flags & FDUNIT_MEDIUM
	 *	un->un_flags & FDUNIT_MEDIUM specifies the last setting.
	 *	un->un_chars->fdc_medium specifies next setting.
	 *	if there is a change, wait 500ms according to Sony spec.
	 */

	is_medium = fdc->c_un->un_chars->fdc_medium;

	if (fdc->c_un->un_flags & FDUNIT_SET_SPEED) {
		check = 1;
	} else {
		check = is_medium ^
		    ((fdc->c_un->un_flags & FDUNIT_MEDIUM) ? 1 : 0);

		/* Set the un_flags if necessary */

		if (check)
			fdc->c_un->un_flags ^= FDUNIT_MEDIUM;
	}

	fdc->c_un->un_flags &= ~FDUNIT_SET_SPEED;


	if (check) {

		fdselect(fdc, unit, 0);
		drv_usecwait(5);

		if ((fdc->c_fdtype & FDCTYPE_AUXIOMASK) == FDCTYPE_SLAVIO) {
			Set_dor(fdc, MEDIUM_DENSITY, is_medium);
		}

		if ((fdc->c_fdtype & FDCTYPE_AUXIOMASK) == FDCTYPE_CHEERIO) {
			if (is_medium) {
				Set_auxio(fdc, AUX_MEDIUM_DENSITY);
			} else {
				Set_auxio(fdc, AUX_HIGH_DENSITY);
			}

		}

		if (is_medium) {
			drv_usecwait(5);
		}

		fdselect(fdc, unit, 1);	/* Sony requirement */
		FDERRPRINT(FDEP_L1, FDEM_EXEC, (C, "rotation:medium\n"));
		drv_usecwait(500000);
	}
}

static void
fd_media_watch(void *arg)
{
	dev_t		dev;
	struct fdunit *un;
	struct fdctlr *fdc;
	int		unit;

	dev = (dev_t)arg;
	fdc = fd_getctlr(dev);
	unit = fdc->c_un->un_unit_no;
	un = fdc->c_un;

	mutex_enter(&fdc->c_lolock);

	if (un->un_media_timeout_id == 0) {
		/*
		 * Untimeout is about to be called.
		 * Don't call fd_get_media_state again
		 */
		mutex_exit(&fdc->c_lolock);
		return;
	}


	un->un_media_state = fd_get_media_state(fdc, unit);
	cv_broadcast(&fdc->c_statecv);

	mutex_exit(&fdc->c_lolock);

	if (un->un_media_timeout) {
		un->un_media_timeout_id = timeout(fd_media_watch,
		    (void *)(ulong_t)dev, un->un_media_timeout);
	}
}

enum dkio_state
fd_get_media_state(struct fdctlr *fdc, int unit)
{
	enum dkio_state state;

	ASSERT(fdc->c_un->un_unit_no == unit);

	if (fdsense_chng(fdc, unit)) {
		/* check disk only if DSKCHG "high" */
		if (fdcheckdisk(fdc, unit)) {
			state = DKIO_EJECTED;
		} else {
			state = DKIO_INSERTED;
		}
	} else {
		state = DKIO_INSERTED;
	}
	return (state);
}

static int
fd_check_media(dev_t dev, enum dkio_state state)
{
	struct fdunit *un;
	struct fdctlr *fdc;
	int		unit;

	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fd_check_media: start\n"));

	fdc = fd_getctlr(dev);
	unit = fdc->c_un->un_unit_no;
	un = fdc->c_un;

	mutex_enter(&fdc->c_lolock);

	CHECK_AND_WAIT_FD_STATE_SUSPENDED(fdc);

	if (fdc->c_un->un_state == FD_STATE_STOPPED) {
		mutex_exit(&fdc->c_lolock);
		if ((pm_raise_power(fdc->c_dip, 0, PM_LEVEL_ON))
		    != DDI_SUCCESS) {
			FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "Power change \
			    failed. \n"));

			(void) pm_idle_component(fdc->c_dip, 0);
			return (EIO);
		}

		mutex_enter(&fdc->c_lolock);
	}

	un->un_media_state = fd_get_media_state(fdc, unit);

	/* turn on timeout */
	un->un_media_timeout = drv_usectohz(fd_check_media_time);
	un->un_media_timeout_id = timeout(fd_media_watch,
	    (void *)(ulong_t)dev, un->un_media_timeout);

	while (un->un_media_state == state) {
		if (cv_wait_sig(&fdc->c_statecv, &fdc->c_lolock) == 0) {
			un->un_media_timeout = 0;
			mutex_exit(&fdc->c_lolock);
			return (EINTR);
		}
	}

	if (un->un_media_timeout_id) {
		timeout_id_t timeid = un->un_media_timeout_id;
		un->un_media_timeout_id = 0;

		mutex_exit(&fdc->c_lolock);
		(void) untimeout(timeid);
		mutex_enter(&fdc->c_lolock);
	}

	if (un->un_media_state == DKIO_INSERTED) {
		if (fdgetlabel(fdc, unit)) {
			mutex_exit(&fdc->c_lolock);
			return (EIO);
		}
	}
	mutex_exit(&fdc->c_lolock);

	FDERRPRINT(FDEP_L1, FDEM_RW, (C, "fd_check_media: end\n"));
	return (0);
}

/*
 * fd_get_media_info :
 *	Collects medium information for
 *	DKIOCGMEDIAINFO ioctl.
 */

static int
fd_get_media_info(struct fdunit *un, caddr_t buf, int flag)
{
	struct dk_minfo media_info;
	int err = 0;

	media_info.dki_media_type = DK_FLOPPY;
	media_info.dki_lbsize = un->un_chars->fdc_sec_size;
	media_info.dki_capacity = un->un_chars->fdc_ncyl *
	    un->un_chars->fdc_secptrack * un->un_chars->fdc_nhead;

	if (ddi_copyout((caddr_t)&media_info, buf,
	    sizeof (struct dk_minfo), flag))
		err = EFAULT;
	return (err);
}

/*
 * fd_power :
 *	Power entry point of fd driver.
 */

static int
fd_power(dev_info_t *dip, int component, int level)
{

	struct fdctlr *fdc;
	int instance;
	int rval;

	if ((level < PM_LEVEL_OFF) || (level > PM_LEVEL_ON) ||
	    (component != 0)) {
		return (DDI_FAILURE);
	}

	instance = ddi_get_instance(dip);
	fdc = fd_getctlr(instance << FDINSTSHIFT);
	if (fdc->c_un == NULL)
		return (DDI_FAILURE);

	if (level == PM_LEVEL_OFF) {
		rval = fd_pm_lower_power(fdc);
	}
	if (level == PM_LEVEL_ON) {
		rval = fd_pm_raise_power(fdc);
	}
	return (rval);
}

/*
 * fd_pm_lower_power :
 *	This function is called only during pm suspend. At this point,
 *	the power management framework thinks the device is idle for
 *	long enough to go to a low power mode. If the device is busy,
 *	then this function returns DDI_FAILURE.
 */

static int
fd_pm_lower_power(struct fdctlr *fdc)
{

	mutex_enter(&fdc->c_lolock);

	if ((fdc->c_un->un_state == FD_STATE_SUSPENDED) ||
	    (fdc->c_un->un_state == FD_STATE_STOPPED)) {
		mutex_exit(&fdc->c_lolock);
		return (DDI_SUCCESS);
	}


	FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "fd_pm_lower_power called\n"));

	/* if the device is busy then we fail the lower power request */
	if (fdc->c_flags & FDCFLG_BUSY) {
		FDERRPRINT(FDEP_L2, FDEM_PWR, (C, "fd_pm_lower_power : \
controller is busy.\n"));
		mutex_exit(&fdc->c_lolock);
		return (DDI_FAILURE);
	}

	fdc->c_un->un_state = FD_STATE_STOPPED;

	mutex_exit(&fdc->c_lolock);
	return (DDI_SUCCESS);
}

/*
 * fd_pm_raise_power :
 *	This function performs the necessary steps for resuming a
 *	device, either from pm suspend or CPR. Here the controller
 *	is reset, initialized and the state is set to FD_STATE_NORMAL.
 */

static int
fd_pm_raise_power(struct fdctlr *fdc)
{

	struct fdunit *un = fdc->c_un;
	int unit;

	FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "fd_pm_raise_power called\n"));
	mutex_enter(&fdc->c_lolock);
	fdgetcsb(fdc);

	/* Reset the dma engine */
	if (fdc->c_fdtype & FDCTYPE_DMA) {
		mutex_enter(&fdc->c_hilock);
		reset_dma_controller(fdc);
		set_dma_control_register(fdc, DCSR_INIT_BITS);
		mutex_exit(&fdc->c_hilock);
	}

	/*
	 * Force a rotational speed set in the next
	 * call to set_rotational_speed().
	 */

	fdc->c_un->un_flags |= FDUNIT_SET_SPEED;

	/* Reset and configure the controller */
	(void) fdreset(fdc);

	unit = fdc->c_un->un_unit_no;

	/* Recalibrate the drive */
	if (fdrecalseek(fdc, unit, -1, 0) != 0) {
		FDERRPRINT(FDEP_L1, FDEM_PWR, (C, "raise_power : recalibrate \
failed\n"));
		fdretcsb(fdc);
		mutex_exit(&fdc->c_lolock);
		return (DDI_FAILURE);
	}

	/* Select the drive through the AUXIO registers */
	fdselect(fdc, unit, 0);
	un->un_state = FD_STATE_NORMAL;
	fdretcsb(fdc);
	mutex_exit(&fdc->c_lolock);
	return (DDI_SUCCESS);
}

/*
 * create_pm_components :
 *	creates the power management components for auto pm framework.
 */

static void
create_pm_components(dev_info_t *dip)
{
	char	*un_pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on"};

	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, dip,
	    "pm-components", un_pm_comp, 3) == DDI_PROP_SUCCESS) {

		(void) pm_raise_power(dip, 0, PM_LEVEL_ON);
	}
}

/*
 * set_data_count_register(struct fdctlr *fdc, uint32_t count)
 *	Set the data count in appropriate dma register.
 */

static void
set_data_count_register(struct fdctlr *fdc, uint32_t count)
{
	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		struct cheerio_dma_reg *dma_reg;
		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dbcr, count);
	} else if (fdc->c_fdtype & FDCTYPE_SB) {
		struct sb_dma_reg *dma_reg;
		count = count - 1; /* 8237 needs it */
		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
		switch (fdc->sb_dma_channel) {
		case 0 :
			ddi_put16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_0WCNT],
			    count & 0xFFFF);
			break;
		case 1 :
			ddi_put16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_1WCNT],
			    count & 0xFFFF);
			break;
		case 2 :
			ddi_put16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_2WCNT],
			    count & 0xFFFF);
			break;
		case 3 :
			ddi_put16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_3WCNT],
			    count & 0xFFFF);
			break;
		default :
			FDERRPRINT(FDEP_L3, FDEM_SDMA,
			    (C, "set_data_count: wrong channel %x\n",
			    fdc->sb_dma_channel));
			break;
		}
	}
}

/*
 * get_data_count_register(struct fdctlr *fdc)
 *	Read the data count from appropriate dma register.
 */

static uint32_t
get_data_count_register(struct fdctlr *fdc)
{
	uint32_t retval = 0;
	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		struct cheerio_dma_reg *dma_reg;
		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
		retval = ddi_get32(fdc->c_handlep_dma, &dma_reg->fdc_dbcr);
	} else if (fdc->c_fdtype & FDCTYPE_SB) {
		struct sb_dma_reg *dma_reg;
		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
		switch (fdc->sb_dma_channel) {
		case 0 :
			retval = ddi_get16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_0WCNT]);
			break;
		case 1 :
			retval = ddi_get16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_1WCNT]);
			break;
		case 2 :
			retval = ddi_get16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_2WCNT]);
			break;
		case 3 :
			retval = ddi_get16(fdc->c_handlep_dma,
			    (ushort_t *)&dma_reg->sb_dma_regs[DMA_3WCNT]);
			break;
		default :
			FDERRPRINT(FDEP_L3, FDEM_SDMA,
			    (C, "get_data_count: wrong channel %x\n",
			    fdc->sb_dma_channel));
			break;
		}
		retval = (uint32_t)((uint16_t)(retval +1));
	}

	return (retval);

}

/*
 * reset_dma_controller(struct fdctlr *fdc)
 *	Reset and initialize the dma controller.
 */

static void
reset_dma_controller(struct fdctlr *fdc)
{
	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		struct cheerio_dma_reg *dma_reg;
		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, DCSR_RESET);
		while (get_dma_control_register(fdc) & DCSR_CYC_PEND)
			;
		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, 0);
	} else if (fdc->c_fdtype & FDCTYPE_SB) {
		struct sb_dma_reg *dma_reg;
		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
		ddi_put8(fdc->c_handlep_dma, &dma_reg->sb_dma_regs[DMAC1_MASK],
		    (fdc->sb_dma_channel & 0x3));

	}
}

/*
 * Get the DMA control register for CHEERIO.
 * For SouthBridge 8237 DMA controller, this register is not valid.
 * So, just return 0.
 */
static uint32_t
get_dma_control_register(struct fdctlr *fdc)
{
	uint32_t retval = 0;
	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		struct cheerio_dma_reg *dma_reg;
		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
		retval = ddi_get32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr);
	}

	return (retval);
}


/*
 * set_data_address_register(struct fdctlr *fdc)
 *	Set the data address in appropriate dma register.
 */
static void
set_data_address_register(struct fdctlr *fdc, uint32_t address)
{
	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		struct cheerio_dma_reg *dma_reg;
		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dacr, address);
	} else if (fdc->c_fdtype & FDCTYPE_SB) {
		struct sb_dma_reg *dma_reg;
		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
		switch (fdc->sb_dma_channel) {
			case 0 :
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_0PAGE],
				    (address & 0xFF0000) >>16);
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_0HPG],
				    (address & 0xFF000000) >>24);
				ddi_put16(fdc->c_handlep_dma,
				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_0ADR],
				    address & 0xFFFF);
				break;
			case 1 :
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_1PAGE],
				    (address & 0xFF0000) >>16);
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_1HPG],
				    (address & 0xFF000000) >>24);
				ddi_put16(fdc->c_handlep_dma,
				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_1ADR],
				    address & 0xFFFF);
				break;
			case 2 :
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_2PAGE],
				    (address & 0xFF0000) >>16);
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_2HPG],
				    (address & 0xFF000000) >>24);
				ddi_put16(fdc->c_handlep_dma,
				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_2ADR],
				    address & 0xFFFF);
				break;
			case 3 :
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_3PAGE],
				    (address & 0xFF0000) >>16);
				ddi_put8(fdc->c_handlep_dma,
				    &dma_reg->sb_dma_regs[DMA_3HPG],
				    (address & 0xFF000000) >>24);
				ddi_put16(fdc->c_handlep_dma,
				    (ushort_t *)&dma_reg->sb_dma_regs[DMA_3ADR],
				    address & 0xFFFF);
				break;
			default :
				FDERRPRINT(FDEP_L3, FDEM_SDMA,
				    (C, "set_data_address: wrong channel %x\n",
				    fdc->sb_dma_channel));
			break;
		}
	}

}


/*
 * set_dma_mode(struct fdctlr *fdc, int val)
 *	Set the appropriate dma direction and registers.
 */
static void
set_dma_mode(struct fdctlr *fdc, int val)
{
	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		struct cheerio_dma_reg *dma_reg;
		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
		if (val == CSB_READ)
			ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr,
			    DCSR_INIT_BITS|DCSR_WRITE);
		else
			ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr,
			    DCSR_INIT_BITS);

	} else if (fdc->c_fdtype & FDCTYPE_SB) {
		uint8_t mode_reg_val, chn_mask;
		struct sb_dma_reg *dma_reg;
		dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;

		if (val == CSB_READ) {
			mode_reg_val = fdc->sb_dma_channel | DMAMODE_READ
			    | DMAMODE_SINGLE;
		} else { /* Read operation */
			mode_reg_val = fdc->sb_dma_channel | DMAMODE_WRITE
			    | DMAMODE_SINGLE;
		}
		ddi_put8(fdc->c_handlep_dma, &dma_reg->sb_dma_regs[DMAC1_MODE],
		    mode_reg_val);
		chn_mask = 1 << (fdc->sb_dma_channel & 0x3);
		ddi_put8(fdc->c_handlep_dma,
		    &dma_reg->sb_dma_regs[DMAC1_ALLMASK], ~chn_mask);
		fdc->sb_dma_lock = 1;
	}
}

/*
 * This function is valid only for CHEERIO/RIO based
 * controllers. The control register for the dma channel
 * is initialized by this function.
 */

static void
set_dma_control_register(struct fdctlr *fdc, uint32_t val)
{
	if (fdc->c_fdtype & FDCTYPE_CHEERIO) {
		struct cheerio_dma_reg *dma_reg;
		dma_reg = (struct cheerio_dma_reg *)fdc->c_dma_regs;
		ddi_put32(fdc->c_handlep_dma, &dma_reg->fdc_dcsr, val);
	}
}

static void
release_sb_dma(struct fdctlr *fdc)
{
	struct sb_dma_reg *dma_reg;
	dma_reg = (struct sb_dma_reg *)fdc->c_dma_regs;
	/* Unmask all the channels to release the DMA controller */
	ddi_put8(fdc->c_handlep_dma,
	    &dma_reg->sb_dma_regs[DMAC1_ALLMASK], NULL);
	fdc->sb_dma_lock = 0;
}

static void
quiesce_fd_interrupt(struct fdctlr *fdc)
{
	/*
	 * The following code is put here to take care of HW problem.
	 * The HW problem is as follows:
	 *
	 *	After poweron the Southbridge floppy controller asserts the
	 * interrupt in tristate. This causes continuous interrupts to
	 * be generated.
	 * Until the Hardware is FIXED we will have to use the following code
	 * to set the interrupt line to proper state after poweron.
	 */
	if (fdc->c_fdtype & FDCTYPE_SB) {
		ddi_put8(fdc->c_handlep_cont, ((uint8_t *)fdc->c_dor),
		    0x0);
		drv_usecwait(200);
		ddi_put8(fdc->c_handlep_cont, ((uint8_t *)fdc->c_dor),
		    0xC);
		drv_usecwait(200);
		Set_Fifo(fdc, 0xE6);
		drv_usecwait(200);
	}
}