summaryrefslogtreecommitdiff
path: root/usr/src/uts/intel/io/dnet.c
blob: e3e4d87ae3eedca7a4dbb1eb41daf2df240bcf4b (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
/*
 * 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 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * dnet -- DEC 21x4x
 *
 * Currently supports:
 *	21040, 21041, 21140, 21142, 21143
 *	SROM versions 1, 3, 3.03, 4
 *	TP, AUI, BNC, 100BASETX, 100BASET4
 *
 * XXX NEEDSWORK
 *	All media SHOULD work, FX is untested
 *
 * Depends on the Generic LAN Driver utility functions in /kernel/misc/gld
 */

#define	BUG_4010796	/* See 4007871, 4010796 */

#include <sys/types.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/stropts.h>
#include <sys/stream.h>
#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/ksynch.h>
#include <sys/stat.h>
#include <sys/modctl.h>
#include <sys/debug.h>
#include <sys/dlpi.h>
#include <sys/ethernet.h>
#include <sys/gld.h>
#include <sys/pci.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>

#include <sys/mii.h>
#include <sys/dnet.h>

#ifdef MII_IS_MODULE
#define	MII_DEPEND " misc/mii"
#else
#define	MII_DEPEND
#endif

char _depends_on[] = "misc/gld" MII_DEPEND;

/*
 *	Declarations and Module Linkage
 */

#define	IDENT	"DNET 21x4x"

/*
 * #define	DNET_NOISY
 * #define	SROMDEBUG
 * #define	SROMDUMPSTRUCTURES
 */

#ifdef DNETDEBUG
#ifdef DNET_NOISY
int	dnetdebug = -1;
#else
int	dnetdebug = 0;
#endif
#endif

/* used for message allocated using desballoc() */
struct free_ptr {
	struct free_rtn	free_rtn;
	caddr_t buf;
};

struct rbuf_list {
	struct rbuf_list	*rbuf_next;	/* next in the list */
	caddr_t			rbuf_vaddr;	/* virual addr of the buf */
	caddr_t			rbuf_paddr;	/* physical addr of the buf */
	caddr_t			rbuf_endpaddr;	/* physical addr at the end */
	ddi_dma_handle_t	rbuf_dmahdl;	/* dma handle */
	ddi_acc_handle_t	rbuf_acchdl;	/* handle for DDI functions */
};

/* Required system entry points */
static int dnetdevinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int dnetprobe(dev_info_t *);
static int dnetattach(dev_info_t *, ddi_attach_cmd_t);
static int dnetdetach(dev_info_t *, ddi_detach_cmd_t);

/* Required driver entry points for GLD */
static int dnet_reset(gld_mac_info_t *);
static int dnet_start_board(gld_mac_info_t *);
static int dnet_stop_board(gld_mac_info_t *);
static int dnet_set_mac_addr(gld_mac_info_t *, uchar_t *);
static int dnet_set_multicast(gld_mac_info_t *, uchar_t *, int);
static int dnet_set_promiscuous(gld_mac_info_t *, int);
static int dnet_get_stats(gld_mac_info_t *, struct gld_stats *);
static int dnet_send(gld_mac_info_t *, mblk_t *);
static uint_t dnetintr(gld_mac_info_t *);

/* Internal functions used by the above entry points */
static void write_gpr(struct dnetinstance *dnetp, uint32_t val);
static void dnet_reset_board(gld_mac_info_t *);
static void dnet_init_board(gld_mac_info_t *);
static void dnet_chip_init(gld_mac_info_t *);
static unsigned int hashindex(uchar_t *);
static int dnet_start(gld_mac_info_t *);
static int dnet_set_addr(gld_mac_info_t *, uchar_t *);

static void dnet_getp(gld_mac_info_t *);
static void update_rx_stats(gld_mac_info_t *, int);
static void update_tx_stats(gld_mac_info_t *, int);

/* Media Selection Setup Routines */
static void set_gpr(gld_mac_info_t *macinfo);
static void set_opr(gld_mac_info_t *macinfo);
static void set_sia(gld_mac_info_t *macinfo);

/* Buffer Management Routines */
static int dnet_alloc_bufs(gld_mac_info_t *);
static void dnet_free_bufs(gld_mac_info_t *);
static void dnet_init_txrx_bufs(gld_mac_info_t *);
static int alloc_descriptor(gld_mac_info_t *);
static void dnet_reclaim_Tx_desc(gld_mac_info_t *);
static int dnet_rbuf_init(dev_info_t *, int);
static int dnet_rbuf_destroy();
static struct rbuf_list *dnet_rbuf_alloc(dev_info_t *, int);
static void dnet_rbuf_free(caddr_t);
static void dnet_freemsg_buf(struct free_ptr *);

static void setup_block(gld_mac_info_t *macinfo);

/* SROM read functions */
static int dnet_read_srom(dev_info_t *, int, ddi_acc_handle_t, int, uchar_t *,
    int);
static void dnet_read21040addr(dev_info_t *, ddi_acc_handle_t, int, uchar_t *,
    int *);
static void dnet_read21140srom(ddi_acc_handle_t, int, uchar_t *, int);
static int get_alternative_srom_image(dev_info_t *, uchar_t *, int);
static void dnet_print_srom(SROM_FORMAT *sr);
static void dnet_dump_leaf(LEAF_FORMAT *leaf);
static void dnet_dump_block(media_block_t *block);
#ifdef BUG_4010796
static void set_alternative_srom_image(dev_info_t *, uchar_t *, int);
static int dnethack(dev_info_t *);
#endif

static int dnet_hack_interrupts(gld_mac_info_t *, int);
static int dnet_detach_hacked_interrupt(dev_info_t *devinfo);
static void enable_interrupts(struct dnetinstance *dnetp, int enable_xmit);

/* SROM parsing functions */
static void dnet_parse_srom(struct dnetinstance *dnetp, SROM_FORMAT *sr,
    uchar_t *vi);
static void parse_controller_leaf(struct dnetinstance *dnetp, LEAF_FORMAT *leaf,
    uchar_t *vi);
static uchar_t *parse_media_block(struct dnetinstance *dnetp,
    media_block_t *block, uchar_t *vi);
static int check_srom_valid(uchar_t *);
static void dnet_dumpbin(char *msg, uchar_t *, int size, int len);
static void setup_legacy_blocks();
/* Active Media Determination Routines */
static void find_active_media(gld_mac_info_t *);
static int send_test_packet(gld_mac_info_t *);
static int dnet_link_sense(gld_mac_info_t *macinfo);

/* PHY MII Routines */
static ushort_t dnet_mii_read(dev_info_t *dip, int phy_addr, int reg_num);
static void dnet_mii_write(dev_info_t *dip, int phy_addr, int reg_num,
			int reg_dat);
static void write_mii(struct dnetinstance *, uint32_t, int);
static void mii_tristate(struct dnetinstance *);
static void do_phy(gld_mac_info_t *);
static void dnet_mii_link_cb(dev_info_t *, int, enum mii_phy_state);
static void set_leaf(SROM_FORMAT *sr, LEAF_FORMAT *leaf);

#ifdef DNETDEBUG
uint32_t dnet_usecelapsed(struct dnetinstance *dnetp);
void dnet_timestamp(struct dnetinstance *, char *);
void dnet_usectimeout(struct dnetinstance *, uint32_t, int, timercb_t);
#endif
static char *media_str[] = {
	"10BaseT",
	"10Base2",
	"10Base5",
	"100BaseTX",
	"10BaseT FD",
	"100BaseTX FD",
	"100BaseT4",
	"100BaseFX",
	"100BaseFX FD",
	"MII"
};

/* default SROM info for cards with no SROMs */
static LEAF_FORMAT leaf_default_100;
static LEAF_FORMAT leaf_asante;
static LEAF_FORMAT leaf_phylegacy;
static LEAF_FORMAT leaf_cogent_100;
static LEAF_FORMAT leaf_21041;
static LEAF_FORMAT leaf_21040;

int rx_buf_size = (ETHERMAX + ETHERFCSL + 3) & ~3;	/* roundup to 4 */

int max_rx_desc_21040 = MAX_RX_DESC_21040;
int max_rx_desc_21140 = MAX_RX_DESC_21140;
int max_tx_desc = MAX_TX_DESC;
int dnet_xmit_threshold = MAX_TX_DESC >> 2;	/* XXX need tuning? */

static kmutex_t dnet_rbuf_lock;		/* mutex to protect rbuf_list data */

/* used for buffers allocated by ddi_dma_mem_alloc() */
static ddi_dma_attr_t dma_attr = {
	DMA_ATTR_V0,		/* dma_attr version */
	0,			/* dma_attr_addr_lo */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_addr_hi */
	0x7FFFFFFF,		/* dma_attr_count_max */
	4,			/* dma_attr_align */
	0x3F,			/* dma_attr_burstsizes */
	1,			/* dma_attr_minxfer */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_maxxfer */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_seg */
	1,			/* dma_attr_sgllen */
	1,			/* dma_attr_granular */
	0,			/* dma_attr_flags */
};

/* used for buffers allocated for rbuf, allow 2 cookies */
static ddi_dma_attr_t dma_attr_rb = {
	DMA_ATTR_V0,		/* dma_attr version */
	0,			/* dma_attr_addr_lo */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_addr_hi */
	0x7FFFFFFF,		/* dma_attr_count_max */
	4,			/* dma_attr_align */
	0x3F,			/* dma_attr_burstsizes */
	1,			/* dma_attr_minxfer */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_maxxfer */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_seg */
	2,			/* dma_attr_sgllen */
	1,			/* dma_attr_granular */
	0,			/* dma_attr_flags */
};
/* used for buffers which are NOT from ddi_dma_mem_alloc() - xmit side */
static ddi_dma_attr_t dma_attr_tx = {
	DMA_ATTR_V0,		/* dma_attr version */
	0,			/* dma_attr_addr_lo */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_addr_hi */
	0x7FFFFFFF,		/* dma_attr_count_max */
	1,			/* dma_attr_align */
	0x3F,			/* dma_attr_burstsizes */
	1,			/* dma_attr_minxfer */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_maxxfer */
	(uint64_t)0xFFFFFFFF,	/* dma_attr_seg */
	0x7FFF,			/* dma_attr_sgllen */
	1,			/* dma_attr_granular */
	0,			/* dma_attr_flags */
};

static ddi_device_acc_attr_t accattr = {
	DDI_DEVICE_ATTR_V0,
	DDI_NEVERSWAP_ACC,
	DDI_STRICTORDER_ACC,
};

uchar_t dnet_broadcastaddr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };

/* Standard Streams initialization */
static struct module_info minfo = {
	DNETIDNUM, "dnet", 0, INFPSZ, DNETHIWAT, DNETLOWAT
};

static struct qinit rinit = {	/* read queues */
	NULL, gld_rsrv, gld_open, gld_close, NULL, &minfo, NULL
};

static struct qinit winit = {	/* write queues */
	gld_wput, gld_wsrv, NULL, NULL, NULL, &minfo, NULL
};

static struct streamtab dnetinfo = {&rinit, &winit, NULL, NULL};

/* Standard Module linkage initialization for a Streams driver */
extern struct mod_ops mod_driverops;

static struct cb_ops cb_dnetops = {
	nulldev,		/* cb_open */
	nulldev,		/* cb_close */
	nodev,			/* cb_strategy */
	nodev,			/* cb_print */
	nodev,			/* cb_dump */
	nodev,			/* cb_read */
	nodev,			/* cb_write */
	nodev,			/* cb_ioctl */
	nodev,			/* cb_devmap */
	nodev,			/* cb_mmap */
	nodev,			/* cb_segmap */
	nochpoll,		/* cb_chpoll */
	ddi_prop_op,		/* cb_prop_op */
	&dnetinfo,		/* cb_stream */
	(int)(D_MP | D_HOTPLUG)	/* cb_flag */
};

static struct dev_ops dnetops = {
	DEVO_REV,		/* devo_rev */
	0,			/* devo_refcnt */
	gld_getinfo,		/* devo_getinfo */
	nulldev,		/* devo_identify */
	dnetprobe,		/* devo_probe */
	dnetattach,		/* devo_attach */
	dnetdetach,		/* devo_detach */
	nodev,			/* devo_reset */
	&cb_dnetops,		/* devo_cb_ops */
	(struct bus_ops *)NULL	/* devo_bus_ops */
};

static struct modldrv modldrv = {
	&mod_driverops,		/* Type of module.  This one is a driver */
	IDENT " %I%",		/* short description */
	&dnetops		/* driver specific ops */
};

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


/*
 * Passed to the hacked interrupt for multiport Cogent and ZNYX cards with
 * dodgy interrupt routing
 */
#define	MAX_INST 8 /* Maximum instances on a multiport adapter. */
struct hackintr_inf
{
	gld_mac_info_t *macinfos[MAX_INST]; /* macinfos for each port */
	dev_info_t *devinfo;		    /* Devinfo of the primary device */
	kmutex_t lock;
		/* Ensures the interrupt doesn't get called while detaching */
};
static char hackintr_propname[] = "InterruptData";
static char macoffset_propname[] = "MAC_offset";
static char speed_propname[] = "speed";
static char ofloprob_propname[] = "dmaworkaround";
static char duplex_propname[] = "full-duplex"; /* Must agree with MII */
static char printsrom_propname[] = "print-srom";

static uint_t dnet_hack_intr(struct hackintr_inf *);

/*
 *	========== Module Loading Entry Points ==========
 */

int
_init(void)
{
	int i;

	/* Configure fake sroms for legacy cards */
	mutex_init(&dnet_rbuf_lock, NULL, MUTEX_DRIVER, NULL);
	setup_legacy_blocks();
	if ((i = mod_install(&modlinkage)) != 0) {
		mutex_destroy(&dnet_rbuf_lock);
	}
	return (i);
}

int
_fini(void)
{
	int i;
	if ((i = mod_remove(&modlinkage)) == 0) { /* module can be unloaded */
		/* loop until all the receive buffers are freed */
		while (dnet_rbuf_destroy() != 0) {
			delay(drv_usectohz(100000));
#ifdef DNETDEBUG
			if (dnetdebug & DNETDDI)
				cmn_err(CE_WARN, "dnet _fini delay");
#endif
		}
		mutex_destroy(&dnet_rbuf_lock);
	}
	return (i);
}

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

/*
 *	========== DDI Entry Points ==========
 */

/*
 * probe(9E) -- Determine if a device is present
 */
static int
dnetprobe(dev_info_t *devinfo)
{
	ddi_acc_handle_t handle;
	uint16_t	vendorid;
	uint16_t	deviceid;

#ifdef DNETDEBUG
	if (dnetdebug & DNETDDI)
		cmn_err(CE_NOTE, "dnetprobe(0x%p)", (void *) devinfo);
#endif

	if (pci_config_setup(devinfo, &handle) != DDI_SUCCESS)
		return (DDI_PROBE_FAILURE);

	vendorid = pci_config_get16(handle, PCI_CONF_VENID);

	if (vendorid != DEC_VENDOR_ID) {
		pci_config_teardown(&handle);
		return (DDI_PROBE_FAILURE);
	}

	deviceid = pci_config_get16(handle, PCI_CONF_DEVID);
	switch (deviceid) {
	case DEVICE_ID_21040:
	case DEVICE_ID_21041:
	case DEVICE_ID_21140:
	case DEVICE_ID_21143: /* And 142 */
		break;
	default:
		pci_config_teardown(&handle);
		return (DDI_PROBE_FAILURE);
	}

	pci_config_teardown(&handle);
#ifndef BUG_4010796
	return (DDI_PROBE_SUCCESS);
#else
	return (dnethack(devinfo));
#endif
}

#ifdef BUG_4010796
/*
 * If we have a device, but we cannot presently access its SROM data,
 * then we return DDI_PROBE_PARTIAL and hope that sometime later we
 * will be able to get at the SROM data.  This can only happen if we
 * are a secondary port with no SROM, and the bootstrap failed to set
 * our DNET_SROM property, and our primary sibling has not yet probed.
 */
static int
dnethack(dev_info_t *devinfo)
{
	uchar_t 	vendor_info[SROM_SIZE];
	uint32_t	csr;
	uint16_t	deviceid;
	ddi_acc_handle_t handle;
	uint32_t	retval;
	int		secondary;
	ddi_acc_handle_t io_handle;
	uint32_t	io_reg;

#define	DNET_PCI_RNUMBER	1

#ifdef DNETDEBUG
	if (dnetdebug & DNETDDI)
		cmn_err(CE_NOTE, "dnethack(0x%p)", (void *) devinfo);
#endif

	if (pci_config_setup(devinfo, &handle) != DDI_SUCCESS)
		return (DDI_PROBE_FAILURE);

	deviceid = pci_config_get16(handle, PCI_CONF_DEVID);

	/*
	 * Turn on Master Enable and IO Enable bits.
	 */
	csr = pci_config_get32(handle, PCI_CONF_COMM);
	pci_config_put32(handle, PCI_CONF_COMM, (csr |PCI_COMM_ME|PCI_COMM_IO));

	pci_config_teardown(&handle);

	/* Now map I/O register */
	if (ddi_regs_map_setup(devinfo, DNET_PCI_RNUMBER,
	    (caddr_t *)&io_reg, (offset_t)0, (offset_t)0, &accattr,
	    &io_handle) != DDI_SUCCESS) {
		return (DDI_PROBE_FAILURE);
	}

	/*
	 * Reset the chip
	 */
	ddi_put32(io_handle, REG32(io_reg, BUS_MODE_REG), SW_RESET);
	drv_usecwait(3);
	ddi_put32(io_handle, REG32(io_reg, BUS_MODE_REG), 0);
	drv_usecwait(8);

	secondary = dnet_read_srom(devinfo, deviceid, io_handle,
	    io_reg, vendor_info, sizeof (vendor_info));

	switch (secondary) {
	case -1:
		/* We can't access our SROM data! */
		retval = DDI_PROBE_PARTIAL;
		break;
	case 0:
		retval = DDI_PROBE_SUCCESS;
		break;
	default:
		retval = DDI_PROBE_SUCCESS;
	}

	ddi_regs_map_free(&io_handle);
	return (retval);
}
#endif /* BUG_4010796 */

/*
 * attach(9E) -- Attach a device to the system
 *
 * Called once for each board successfully probed.
 */
static int
dnetattach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
{
	uint16_t revid;
	struct dnetinstance 	*dnetp;		/* Our private device info */
	gld_mac_info_t		*macinfo;		/* GLD structure */
	uchar_t 		vendor_info[SROM_SIZE];
	uint32_t		csr;
	uint16_t		deviceid;
	ddi_acc_handle_t 	handle;
	int			secondary;

#define	DNET_PCI_RNUMBER	1

#ifdef DNETDEBUG
	if (dnetdebug & DNETDDI)
		cmn_err(CE_NOTE, "dnetattach(0x%p)", (void *) devinfo);
#endif

	if (cmd != DDI_ATTACH)
		return (DDI_FAILURE);
	if (pci_config_setup(devinfo, &handle) != DDI_SUCCESS)
		return (DDI_FAILURE);

	deviceid = pci_config_get16(handle, PCI_CONF_DEVID);
	switch (deviceid) {
	case DEVICE_ID_21040:
	case DEVICE_ID_21041:
	case DEVICE_ID_21140:
	case DEVICE_ID_21143: /* And 142 */
		break;
	default:
		pci_config_teardown(&handle);
		return (DDI_FAILURE);
	}

	/*
	 * Turn on Master Enable and IO Enable bits.
	 */
	csr = pci_config_get32(handle, PCI_CONF_COMM);
	pci_config_put32(handle, PCI_CONF_COMM, (csr |PCI_COMM_ME|PCI_COMM_IO));

	/* Make sure the device is not asleep */
	csr = pci_config_get32(handle, PCI_DNET_CONF_CFDD);
	pci_config_put32(handle, PCI_DNET_CONF_CFDD,
	    csr &  ~(CFDD_SLEEP|CFDD_SNOOZE));

	revid = pci_config_get8(handle, PCI_CONF_REVID);
	pci_config_teardown(&handle);

	/*
	 *	Allocate gld_mac_info_t and dnetinstance structures
	 */
	if ((macinfo = gld_mac_alloc(devinfo)) == NULL)
		return (DDI_FAILURE);

	if ((dnetp = (struct dnetinstance *)
	    kmem_zalloc(sizeof (struct dnetinstance), KM_SLEEP)) == NULL) {
		gld_mac_free(macinfo);
		return (DDI_FAILURE);
	}

	/* Now map I/O register */
	if (ddi_regs_map_setup(devinfo, DNET_PCI_RNUMBER,
	    (caddr_t *)&dnetp->io_reg, (offset_t)0, (offset_t)0, &accattr,
	    &dnetp->io_handle) != DDI_SUCCESS) {
		kmem_free(dnetp, sizeof (struct dnetinstance));
		gld_mac_free(macinfo);
		return (DDI_FAILURE);
	}

	dnetp->devinfo = devinfo;
	dnetp->board_type = deviceid;

	/*
	 * Initialize our private fields in macinfo and dnetinstance
	 */
	macinfo->gldm_private = (caddr_t)dnetp;

	/*
	 * Initialize pointers to device specific functions which will be
	 * used by the generic layer.
	 */
	macinfo->gldm_reset	= dnet_reset;
	macinfo->gldm_start	= dnet_start_board;
	macinfo->gldm_stop	= dnet_stop_board;
	macinfo->gldm_set_mac_addr	= dnet_set_mac_addr;
	macinfo->gldm_set_multicast	= dnet_set_multicast;
	macinfo->gldm_set_promiscuous	= dnet_set_promiscuous;
	macinfo->gldm_get_stats	= dnet_get_stats;
	macinfo->gldm_send	= dnet_send;
	macinfo->gldm_intr	= dnetintr;
	macinfo->gldm_ioctl	= NULL;

	/*
	 *	Initialize board characteristics needed by the generic layer.
	 */
	macinfo->gldm_ident = IDENT;
	macinfo->gldm_type = DL_ETHER;
	macinfo->gldm_minpkt = 0;		/* assumes we pad ourselves */
	macinfo->gldm_maxpkt = DNETMAXPKT;
	macinfo->gldm_addrlen = ETHERADDRL;
	macinfo->gldm_saplen = -2;

	/* Other required initialization */
	macinfo->gldm_ppa = ddi_get_instance(devinfo);
	macinfo->gldm_vendor_addr = dnetp->vendor_addr;
	macinfo->gldm_broadcast_addr = dnet_broadcastaddr;
	macinfo->gldm_devinfo = devinfo;


	/*
	 * Get the iblock cookie with which to initialize the mutexes.
	 */
	if (ddi_get_iblock_cookie(devinfo, 0, &macinfo->gldm_cookie)
	    != DDI_SUCCESS)
		goto fail;

	/*
	 * Initialize mutex's for this device.
	 * Do this before registering the interrupt handler to avoid
	 * condition where interrupt handler can try using uninitialized
	 * mutex.
	 * Lock ordering rules: always lock intrlock first before
	 * txlock if both are required.
	 */
	mutex_init(&dnetp->txlock,
	    NULL, MUTEX_DRIVER, macinfo->gldm_cookie);
	mutex_init(&dnetp->intrlock,
	    NULL, MUTEX_DRIVER, macinfo->gldm_cookie);

	/*
	 * Get the BNC/TP indicator from the conf file for 21040
	 */
	dnetp->bnc_indicator =
		ddi_getprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
				"bncaui", -1);

	/*
	 * For 21140 check the data rate set in the conf file. Default is
	 * 100Mb/s. Disallow connections at settings that would conflict
	 * with what's in the conf file
	 */
	dnetp->speed =
		ddi_getprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
				speed_propname, 0);
	dnetp->full_duplex =
		ddi_getprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
				duplex_propname, -1);

	if (dnetp->speed == 100) {
		dnetp->disallowed_media |= (1UL<<MEDIA_TP) | (1UL<<MEDIA_TP_FD);
	} else if (dnetp->speed == 10) {
		dnetp->disallowed_media |=
		    (1UL<<MEDIA_SYM_SCR) | (1UL<<MEDIA_SYM_SCR_FD);
	}

	if (dnetp->full_duplex == 1) {
		dnetp->disallowed_media |=
		    (1UL<<MEDIA_TP) | (1UL<<MEDIA_SYM_SCR);
	} else if (dnetp->full_duplex == 0) {
		dnetp->disallowed_media |=
		    (1UL<<MEDIA_TP_FD) | (1UL<<MEDIA_SYM_SCR_FD);
	}

	if (dnetp->bnc_indicator == 0) /* Disable BNC and AUI media */
		dnetp->disallowed_media |= (1UL<<MEDIA_BNC) | (1UL<<MEDIA_AUI);
	else if (dnetp->bnc_indicator == 1) /* Force BNC only */
		dnetp->disallowed_media =  (uint32_t)~(1U<<MEDIA_BNC);
	else if (dnetp->bnc_indicator == 2) /* Force AUI only */
		dnetp->disallowed_media = (uint32_t)~(1U<<MEDIA_AUI);

	dnet_reset_board(macinfo);

	secondary = dnet_read_srom(devinfo, dnetp->board_type, dnetp->io_handle,
	    dnetp->io_reg, vendor_info, sizeof (vendor_info));

	if (secondary == -1) /* ASSERT (vendor_info not big enough) */
		goto fail1;

	dnet_parse_srom(dnetp, &dnetp->sr, vendor_info);

	if (ddi_getprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
	    printsrom_propname, 0))
		dnet_print_srom(&dnetp->sr);

	dnetp->sr.netaddr[ETHERADDRL-1] += secondary;	/* unique ether addr */

	BCOPY((caddr_t)dnetp->sr.netaddr,
		(caddr_t)dnetp->vendor_addr, ETHERADDRL);

	BCOPY((caddr_t)dnetp->sr.netaddr,
		(caddr_t)dnetp->curr_macaddr, ETHERADDRL);


	/*
	 * determine whether to implement workaround from DEC
	 * for DMA overrun errata.
	 */
	dnetp->overrun_workaround =
	    ((dnetp->board_type == DEVICE_ID_21140 && revid >= 0x20) ||
	    (dnetp->board_type == DEVICE_ID_21143 && revid <= 0x30)) ? 1 : 0;

	dnetp->overrun_workaround =
		ddi_getprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
				ofloprob_propname, dnetp->overrun_workaround);

	/*
	 * Add the interrupt handler if dnet_hack_interrupts() returns 0.
	 * Otherwise dnet_hack_interrupts() itself adds the handler.
	 */
	if (!dnet_hack_interrupts(macinfo, secondary)) {
		(void) ddi_add_intr(devinfo, 0, NULL,
				    NULL, gld_intr, (caddr_t)macinfo);
	}

	dnetp->max_tx_desc = max_tx_desc;
	dnetp->max_rx_desc = max_rx_desc_21040;
	if (dnetp->board_type != DEVICE_ID_21040 &&
	    dnetp->board_type != DEVICE_ID_21041 &&
	    dnetp->speed != 10)
		dnetp->max_rx_desc = max_rx_desc_21140;

	/* Allocate the TX and RX descriptors/buffers. */
	if (dnet_alloc_bufs(macinfo) == FAILURE) {
		cmn_err(CE_WARN, "DNET: Not enough DMA memory for buffers.");
		goto fail2;
	}

	/*
	 *	Register ourselves with the GLD interface
	 *
	 *	gld_register will:
	 *	link us with the GLD system;
	 *	set our ddi_set_driver_private(9F) data to the macinfo pointer;
	 *	save the devinfo pointer in macinfo->gldm_devinfo;
	 *	map the registers, putting the kvaddr into macinfo->gldm_memp;
	 *	add the interrupt, putting the cookie in gldm_cookie;
	 *	init the gldm_intrlock mutex which will block that interrupt;
	 *	create the minor node.
	 */

	if (gld_register(devinfo, "dnet", macinfo) == DDI_SUCCESS) {
		mutex_enter(&dnetp->intrlock);
		dnetp->phyaddr = -1;
		if (dnetp->board_type == DEVICE_ID_21140 ||
		    dnetp->board_type == DEVICE_ID_21143)
			do_phy(macinfo);	/* Initialize the PHY, if any */
		find_active_media(macinfo);

		/* if the chosen media is non-MII, stop the port monitor */
		if (dnetp->selected_media_block->media_code != MEDIA_MII &&
		    dnetp->mii != NULL) {
			mii_destroy(dnetp->mii);
			dnetp->mii = NULL;
			dnetp->phyaddr = -1;
		}

#ifdef DNETDEBUG
		if (dnetdebug & DNETSENSE)
			cmn_err(CE_NOTE, "dnet: link configured : %s",
			    media_str[dnetp->selected_media_block->media_code]);
#endif
		bzero(dnetp->setup_buf_vaddr, SETUPBUF_SIZE);
		dnet_reset_board(macinfo);
		dnet_init_board(macinfo);
		/* XXX function return value ignored */
		mutex_exit(&dnetp->intrlock);
		/* dropped intrlock as dnet_stop_board() grabs intrlock */
		(void) dnet_stop_board(macinfo);
		return (DDI_SUCCESS);
	}
fail2:
	/* XXX function return value ignored */
	/*
	 * dnet_detach_hacked_interrupt() will remove
	 * interrupt for the non-hacked case also.
	 */
	(void) dnet_detach_hacked_interrupt(devinfo);
	dnet_free_bufs(macinfo);
fail1:
	mutex_destroy(&dnetp->txlock);
	mutex_destroy(&dnetp->intrlock);
fail:
	ddi_regs_map_free(&dnetp->io_handle);
	kmem_free(dnetp, sizeof (struct dnetinstance));
	gld_mac_free(macinfo);
	return (DDI_FAILURE);
}

/*
 * detach(9E) -- Detach a device from the system
 */
static int
dnetdetach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
{
	int32_t rc;
	gld_mac_info_t	*macinfo;		/* GLD structure */
	struct dnetinstance *dnetp;		/* Our private device info */
	int32_t		proplen;

#ifdef DNETDEBUG
	if (dnetdebug & DNETDDI)
		cmn_err(CE_NOTE, "dnetdetach(0x%p)", (void *) devinfo);
#endif

	if (cmd != DDI_DETACH) {
		return (DDI_FAILURE);
	}

	/* Get the driver private (gld_mac_info_t) structure */
	macinfo = ddi_get_driver_private(devinfo);
	dnetp = (struct dnetinstance *)(macinfo->gldm_private);

	/*
	 *	Unregister ourselves from the GLD interface
	 *
	 *	gld_unregister will:
	 *	remove the minor node;
	 *	unmap the registers;
	 *	remove the interrupt;
	 *	destroy the gldm_intrlock mutex;
	 *	unlink us from the GLD system.
	 */
	if (gld_unregister(macinfo) != DDI_SUCCESS)
		return (DDI_FAILURE);

	/* stop the board if it is running */
	dnet_reset_board(macinfo);

	if ((rc = dnet_detach_hacked_interrupt(devinfo)) != DDI_SUCCESS)
		return (rc);

	if (dnetp->mii != NULL)
		mii_destroy(dnetp->mii);

	/* Free leaf information */
	set_leaf(&dnetp->sr, NULL);

	ddi_regs_map_free(&dnetp->io_handle);
	dnet_free_bufs(macinfo);
	mutex_destroy(&dnetp->txlock);
	mutex_destroy(&dnetp->intrlock);
	kmem_free(dnetp, sizeof (struct dnetinstance));
	gld_mac_free(macinfo);

#ifdef BUG_4010796
	if (ddi_getproplen(DDI_DEV_T_ANY, devinfo, 0,
	    "DNET_HACK", &proplen) != DDI_PROP_SUCCESS)
		return (DDI_SUCCESS);

	/*
	 * We must remove the properties we added, because if we leave
	 * them in the devinfo nodes and the driver is unloaded, when
	 * the driver is reloaded the info will still be there, causing
	 * nodes which had returned PROBE_PARTIAL the first time to
	 * instead return PROBE_SUCCESS, in turn causing the nodes to be
	 * attached in a different order, causing their PPA numbers to
	 * be different the second time around, which is undesirable.
	 */
	(void) ddi_prop_remove(DDI_DEV_T_NONE, devinfo, "DNET_HACK");
	(void) ddi_prop_remove(DDI_DEV_T_NONE, ddi_get_parent(devinfo),
		"DNET_SROM");
	(void) ddi_prop_remove(DDI_DEV_T_NONE, ddi_get_parent(devinfo),
		"DNET_DEVNUM");
#endif

	return (DDI_SUCCESS);
}

/*
 *	========== GLD Entry Points ==========
 */

/*
 *	dnet_reset() -- reset the board to initial state;
 */
static int
dnet_reset(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp =		/* Our private device info */
				(struct dnetinstance *)macinfo->gldm_private;
#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_reset(0x%p)", (void *) macinfo);
#endif

	mutex_enter(&dnetp->intrlock);
	/*
	 * Initialize internal data structures
	 */
	bzero(dnetp->setup_buf_vaddr, SETUPBUF_SIZE);
	bzero(dnetp->multicast_cnt, MCASTBUF_SIZE);
	dnetp->promisc = 0;

	mutex_enter(&dnetp->txlock);
	dnetp->need_saddr = 0;
	mutex_exit(&dnetp->txlock);

	dnet_reset_board(macinfo);

	if (ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo, DDI_PROP_DONTPASS,
	    "reset_do_find_active_media", 0)) {
		find_active_media(macinfo);	/* Redetermine active media */
		/* Go back to a good state */
		bzero(dnetp->setup_buf_vaddr, SETUPBUF_SIZE);
		dnet_reset_board(macinfo);
	}
	dnet_init_board(macinfo);
	mutex_exit(&dnetp->intrlock);
	return (0);
}

static void
dnet_reset_board(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp =		/* Our private device info */
				(struct dnetinstance *)macinfo->gldm_private;
	uint32_t	val;

	/*
	 * before initializing the dnet should be in STOP state
	 */
	/* XXX function return value ignored */
	/* (void) dnet_stop_board(macinfo); */
	val = ddi_get32(dnetp->io_handle,
			    REG32(dnetp->io_reg, OPN_MODE_REG));
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, OPN_MODE_REG),
		    val & ~(START_TRANSMIT | START_RECEIVE));

	/*
	 * Reset the chip
	 */
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, INT_MASK_REG), 0);
	ddi_put32(dnetp->io_handle,
		REG32(dnetp->io_reg, BUS_MODE_REG), SW_RESET);
	drv_usecwait(5);
}

/*
 * dnet_init_board() -- initialize the specified network board short of
 * actually starting the board.  Call after dnet_reset_board().
 * called with intrlock held.
 */
static void
dnet_init_board(gld_mac_info_t *macinfo)
{
#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_init_board(0x%p)", (void *) macinfo);
#endif
	set_opr(macinfo);
	set_gpr(macinfo);
	set_sia(macinfo);
	dnet_chip_init(macinfo);
}

/* dnet_chip_init() - called with intrlock held */
static void
dnet_chip_init(gld_mac_info_t *macinfo)
{
	struct dnetinstance	*dnetp = (struct dnetinstance *)
					(macinfo->gldm_private);

	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, BUS_MODE_REG),
		    CACHE_ALIGN | BURST_SIZE);		/* CSR0 */

	/*
	 * Initialize the TX and RX descriptors/buffers
	 */
	dnet_init_txrx_bufs(macinfo);

	/*
	 * Set the base address of the Rx descriptor list in CSR3
	 */
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, RX_BASE_ADDR_REG),
	    (uint32_t)(dnetp->rx_desc_paddr));

	/*
	 * Set the base address of the Tx descrptor list in CSR4
	 */
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, TX_BASE_ADDR_REG),
		    (uint32_t)(dnetp->tx_desc_paddr));

	dnetp->tx_current_desc = dnetp->rx_current_desc = 0;
	dnetp->transmitted_desc = 0;
	dnetp->free_desc = dnetp->max_tx_desc;
	enable_interrupts(dnetp, 1);
}

/*
 *	dnet_start() -- start the board receiving and allow transmits.
 *  Called with intrlock held.
 */
static int
dnet_start(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp =		/* Our private device info */
				(struct dnetinstance *)macinfo->gldm_private;
	uint32_t 		val;

#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_start(0x%p)", (void *) macinfo);
#endif

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	/*
	 * start the board and enable receiving
	 */
	val = ddi_get32(dnetp->io_handle,
			    REG32(dnetp->io_reg, OPN_MODE_REG));
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, OPN_MODE_REG),
		    val | START_TRANSMIT);
	(void) dnet_set_addr(macinfo, dnetp->curr_macaddr);
	val = ddi_get32(dnetp->io_handle,
			    REG32(dnetp->io_reg, OPN_MODE_REG));
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, OPN_MODE_REG),
		    val | START_RECEIVE);
	enable_interrupts(dnetp, 1);
	return (0);
}

/*
 *	dnet_start_board() -- start the board receiving and allow transmits.
 */
static int
dnet_start_board(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp =		/* Our private device info */
				(struct dnetinstance *)macinfo->gldm_private;

#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_start_board(0x%p)", (void *) macinfo);
#endif

	mutex_enter(&dnetp->intrlock);
	/*
	 * start the board and enable receiving
	 */
	(void) dnet_start(macinfo);
	mutex_exit(&dnetp->intrlock);
	return (0);
}

/*
 *	dnet_stop_board() -- stop board receiving
 */
static int
dnet_stop_board(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp =		/* Our private device info */
				(struct dnetinstance *)macinfo->gldm_private;
	uint32_t 		val;

#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_stop_board(0x%p)", (void *) macinfo);
#endif
	/*
	 * stop the board and disable transmit/receive
	 */
	mutex_enter(&dnetp->intrlock);
	val = ddi_get32(dnetp->io_handle,
			    REG32(dnetp->io_reg, OPN_MODE_REG));
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, OPN_MODE_REG),
		    val & ~(START_TRANSMIT | START_RECEIVE));
	mutex_exit(&dnetp->intrlock);
	return (0);
}

/*
 *	dnet_set_addr() -- set the physical network address on the board
 *  Called with intrlock held.
 */
static int
dnet_set_addr(gld_mac_info_t *macinfo, uchar_t *macaddr)
{
	struct dnetinstance *dnetp =		/* Our private device info */
		(struct dnetinstance *)macinfo->gldm_private;
	struct tx_desc_type *desc;
	int 		current_desc;
	uint32_t	index;
	uint32_t	*hashp;
	uint32_t 	val;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_set_addr(0x%p)", (void *) macinfo);
#endif
	bcopy(macaddr, dnetp->curr_macaddr, ETHERADDRL);
	val = ddi_get32(dnetp->io_handle,
			    REG32(dnetp->io_reg, OPN_MODE_REG));
	if (!(val & START_TRANSMIT))
		return (0);

	current_desc = dnetp->tx_current_desc;
	desc = &dnetp->tx_desc[current_desc];

	mutex_enter(&dnetp->txlock);
	dnetp->need_saddr = 0;
	mutex_exit(&dnetp->txlock);

	if ((alloc_descriptor(macinfo)) == FAILURE) {
		mutex_enter(&dnetp->txlock);
		dnetp->need_saddr = 1;
		mutex_exit(&dnetp->txlock);
#ifdef DNETDEBUG
		if (dnetdebug & DNETTRACE)
			cmn_err(CE_WARN, "DNET saddr:alloc descriptor failure");
#endif
		return (0);
	}

	desc->buffer1 = (uint32_t)(dnetp->setup_buf_paddr);
	desc->buffer2			= (uint32_t)(0);
	desc->desc1.buffer_size1 	= SETUPBUF_SIZE;
	desc->desc1.buffer_size2 	= 0;
	desc->desc1.setup_packet	= 1;
	desc->desc1.first_desc		= 0;
	desc->desc1.last_desc 		= 0;
	desc->desc1.filter_type0 	= 1;
	desc->desc1.filter_type1 	= 1;
	desc->desc1.int_on_comp		= 1;

	/*
	 * As we are using Imperfect filtering, the broadcast address has to
	 * be set explicitly in the 512 bit hash table.  Hence the index into
	 * the hash table is calculated and the bit set to enable reception
	 * of broadcast packets.
	 *
	 * We also use HASH_ONLY mode, without using the perfect filter for
	 * our station address, because there appears to be a bug in the
	 * 21140 where it fails to receive the specified perfect filter
	 * address.
	 *
	 * Since dlsdmult comes through here, it doesn't matter that the count
	 * is wrong for the two bits that correspond to the cases below. The
	 * worst that could happen is that we'd leave on a bit for an old
	 * macaddr, in the case where the macaddr gets changed, which is rare.
	 * Since filtering is imperfect, it is OK if that happens.
	 */
	hashp = (uint32_t *)dnetp->setup_buf_vaddr;
	index = hashindex((uchar_t *)dnet_broadcastaddr);
	hashp[ index / 16 ] |= 1 << (index % 16);

	index = hashindex((uchar_t *)dnetp->curr_macaddr);
	hashp[ index / 16 ] |= 1 << (index % 16);

	desc->desc0.own = 1;
	ddi_put8(dnetp->io_handle, REG8(dnetp->io_reg, TX_POLL_REG),
		    TX_POLL_DEMAND);
	return (0);
}

/*
 *	dnet_set_mac_addr() -- set the physical network address on the board
 */
static int
dnet_set_mac_addr(gld_mac_info_t *macinfo, uchar_t *macaddr)
{
	struct dnetinstance *dnetp =		/* Our private device info */
		(struct dnetinstance *)macinfo->gldm_private;

#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_set_mac_addr(0x%p)", (void *) macinfo);
#endif
	mutex_enter(&dnetp->intrlock);
	(void) dnet_set_addr(macinfo, macaddr);
	mutex_exit(&dnetp->intrlock);
	return (0);
}

/*
 *	dnet_set_multicast() -- set (enable) or disable a multicast address
 *
 *	Program the hardware to enable/disable the multicast address
 *	in "mcast".  Enable if "op" is non-zero, disable if zero.
 */
static int
dnet_set_multicast(gld_mac_info_t *macinfo, uchar_t *mcast, int op)
{
	struct dnetinstance *dnetp =		/* Our private device info */
		(struct dnetinstance *)macinfo->gldm_private;
	uint32_t	index;
	uint32_t	*hashp;
	uint32_t	retval;

#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_set_multicast(0x%p, %s)",
		    (void *) macinfo, op ? "ON" : "OFF");
#endif
	mutex_enter(&dnetp->intrlock);
	index = hashindex(mcast);
	hashp = (uint32_t *)dnetp->setup_buf_vaddr;
	if (op == GLD_MULTI_ENABLE) {
		if (dnetp->multicast_cnt[index]++) {
			mutex_exit(&dnetp->intrlock);
			return (0);
		}
		hashp[ index / 16 ] |= 1 << (index % 16);
	} else {
		if (--dnetp->multicast_cnt[index]) {
			mutex_exit(&dnetp->intrlock);
			return (0);
		}
		hashp[ index / 16 ] &= ~ (1 << (index % 16));
	}
	retval = dnet_set_addr(macinfo, dnetp->curr_macaddr);
	mutex_exit(&dnetp->intrlock);
	return (retval);
}

/*
 * A hashing function used for setting the
 * node address or a multicast address
 */
static uint32_t
hashindex(uchar_t *address)
{
	uint32_t	crc = (uint32_t)HASH_CRC;
	uint32_t const 	POLY = HASH_POLY;
	uint32_t	msb;
	int32_t 	byteslength;
	uint8_t 	currentbyte;
	uint32_t 	index;
	int32_t 	bit;
	int32_t		shift;

	for (byteslength = 0; byteslength < ETHERADDRL; byteslength++) {
		currentbyte = address[byteslength];
		for (bit = 0; bit < 8; bit++) {
			msb = crc >> 31;
			crc <<= 1;
			if (msb ^ (currentbyte & 1)) {
				crc ^= POLY;
				crc |= 0x00000001;
			}
			currentbyte >>= 1;
		}
	}

	for (index = 0, bit = 23, shift = 8;
		shift >= 0;
		bit++, shift--) {
			index |= (((crc >> bit) & 1) << shift);
	}
	return (index);
}

/*
 * dnet_set_promiscuous() -- set or reset promiscuous mode on the board
 *
 *	Program the hardware to enable/disable promiscuous mode.
 *	Enable if "on" is non-zero, disable if zero.
 */

static int
dnet_set_promiscuous(gld_mac_info_t *macinfo, int on)
{
	struct dnetinstance *dnetp;
	uint32_t val;

#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_set_promiscuous(0x%p, %s)",
		    (void *) macinfo, on ? "ON" : "OFF");
#endif

	dnetp = (struct dnetinstance *)macinfo->gldm_private;
	mutex_enter(&dnetp->intrlock);
	if (dnetp->promisc == on) {
		mutex_exit(&dnetp->intrlock);
		return (SUCCESS);
	}
	dnetp->promisc = on;

	val = ddi_get32(dnetp->io_handle,
			REG32(dnetp->io_reg, OPN_MODE_REG));
	if (on != GLD_MAC_PROMISC_NONE)
		ddi_put32(dnetp->io_handle,
			REG32(dnetp->io_reg, OPN_MODE_REG),
			val | PROM_MODE);
	else
		ddi_put32(dnetp->io_handle,
			REG32(dnetp->io_reg, OPN_MODE_REG),
			val & (~PROM_MODE));
	mutex_exit(&dnetp->intrlock);
	return (DDI_SUCCESS);
}

/*
 * dnet_get_stats() -- update statistics
 *
 *	GLD calls this routine just before it reads the driver's statistics
 *	structure.  If your board maintains statistics, this is the time to
 *	read them in and update the values in the structure.  If the driver
 *	maintains statistics continuously, this routine need do nothing.
 */

static int
dnet_get_stats(gld_mac_info_t *macinfo, struct gld_stats *sp)
{
	struct dnetinstance *dnetp =		/* Our private device info */
			(struct dnetinstance *)macinfo->gldm_private;
#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_get_stats(0x%p)", (void *) macinfo);
#endif
	sp->glds_errrcv = dnetp->stat_errrcv;
	sp->glds_overflow = dnetp->stat_overflow;
	sp->glds_intr = dnetp->stat_intr;
	sp->glds_defer = dnetp->stat_defer;
	sp->glds_missed	= dnetp->stat_missed;
	sp->glds_norcvbuf = dnetp->stat_norcvbuf;
	sp->glds_crc = dnetp->stat_crc;
	sp->glds_short = dnetp->stat_short;
	sp->glds_frame = dnetp->stat_frame;
	sp->glds_errxmt = dnetp->stat_errxmt;
	sp->glds_collisions	= dnetp->stat_collisions;
	sp->glds_xmtlatecoll = dnetp->stat_xmtlatecoll;
	sp->glds_excoll = dnetp->stat_excoll;
	sp->glds_underflow = dnetp->stat_underflow;
	sp->glds_nocarrier = dnetp->stat_nocarrier;

	/* stats from instance structure */
	sp->glds_speed = dnetp->speed * 1000000;
	sp->glds_duplex =
	    dnetp->full_duplex ? GLD_DUPLEX_FULL : GLD_DUPLEX_HALF;

	return (DDI_SUCCESS);
}

/*
 *	dnet_send() -- send a packet
 *
 *	Called when a packet is ready to be transmitted. A pointer to an
 *	M_DATA message that contains the packet is passed to this routine.
 *	The complete LLC header is contained in the message's first message
 *	block, and the remainder of the packet is contained within
 *	additional M_DATA message blocks linked to the first message block.
 */

#define	NextTXIndex(index) (((index)+1) % dnetp->max_tx_desc)
#define	PrevTXIndex(index) (((index)-1) < 0 ? dnetp->max_tx_desc - 1: (index)-1)

static int
dnet_send(gld_mac_info_t *macinfo, mblk_t *mp)
{
	struct dnetinstance *dnetp =		/* Our private device info */
			(struct dnetinstance *)macinfo->gldm_private;
	struct tx_desc_type	*ring = dnetp->tx_desc;
	int		mblen, totlen;
	int		index, end_index, start_index;
	int		avail;
	int		error;
	int		bufn;
	int		bp_count;
	int		retval;
	mblk_t		*bp;
	uint32_t	tx_interrupt_mask;

#ifdef DNETDEBUG
	if (dnetdebug & DNETSEND)
		cmn_err(CE_NOTE, "dnet_send(0x%p, 0x%p)",
			(void *) macinfo, (void *) mp);
#endif
	mutex_enter(&dnetp->txlock);
	if (dnetp->need_saddr) {
		/* XXX function return value ignored */
		mutex_exit(&dnetp->txlock);
		mutex_enter(&dnetp->intrlock);
		(void) dnet_set_addr(macinfo, dnetp->curr_macaddr);
		mutex_exit(&dnetp->intrlock);
		mutex_enter(&dnetp->txlock);
	}

	/* reclaim any xmit descriptors completed */
	dnet_reclaim_Tx_desc(macinfo);

	/*
	 * Use the data buffers from the message and construct the
	 * scatter/gather list by calling ddi_dma_addr_bind_handle().
	 */
	error = bp_count = 0;
	totlen = 0;
	bp = mp;
	bufn = 0;
	index = start_index = dnetp->tx_current_desc;
	avail = dnetp->free_desc;
	while (bp != NULL) {
		uint_t ncookies;
		ddi_dma_cookie_t dma_cookie;

		if (++bp_count > DNET_MAX_FRAG) {
#ifndef DNET_NOISY
			(void) pullupmsg(bp, -1);
#else
			if (pullupmsg(bp, -1))
			    cmn_err(CE_NOTE, "DNET: pulled up send msg");
			else
			    cmn_err(CE_NOTE, "DNET: couldn't pullup send msg");
#endif
		}

		mblen = (int)(bp->b_wptr - bp->b_rptr);

		if (!mblen) {	/* skip zero-length message blocks */
			bp = bp->b_cont;
			continue;
		}

		retval = ddi_dma_addr_bind_handle(dnetp->dma_handle_tx, NULL,
		    (caddr_t)bp->b_rptr, mblen,
		    DDI_DMA_WRITE | DDI_DMA_STREAMING, DDI_DMA_SLEEP, 0,
		    &dma_cookie, &ncookies);

		switch (retval) {
		case DDI_DMA_MAPPED:
			break;		/* everything's fine */

		case DDI_DMA_NORESOURCES:
			error = 1;	/* allow retry by gld */
			break;

		case DDI_DMA_NOMAPPING:
		case DDI_DMA_INUSE:
		case DDI_DMA_TOOBIG:
		default:
			error = 2;	/* error, no retry */
			break;
		}

		/*
		 * we can use two cookies per descriptor (i.e buffer1 and
		 * buffer2) so we need at least (ncookies+1)/2 descriptors.
		 */
		if (((ncookies + 1) >> 1) > dnetp->free_desc) {
			(void) ddi_dma_unbind_handle(dnetp->dma_handle_tx);
			error = 1;
			break;
		}

		/* setup the descriptors for this data buffer */
		while (ncookies) {
			end_index = index;
			if (bufn % 2) {
			    ring[index].buffer2 =
				(uint32_t)dma_cookie.dmac_address;
			    ring[index].desc1.buffer_size2 =
				dma_cookie.dmac_size;
			    index = NextTXIndex(index); /* goto next desc */
			} else {
			    /* initialize the descriptor */
			    ASSERT(ring[index].desc0.own == 0);
			    *(uint32_t *)&ring[index].desc0 = 0;
			    *(uint32_t *)&ring[index].desc1 &=
				DNET_END_OF_RING;
			    ring[index].buffer1 =
				(uint32_t)dma_cookie.dmac_address;
			    ring[index].desc1.buffer_size1 =
				dma_cookie.dmac_size;
			    ring[index].buffer2 = (uint32_t)(0);
			    dnetp->free_desc--;
			    ASSERT(dnetp->free_desc >= 0);
			}
			totlen += dma_cookie.dmac_size;
			bufn++;
			if (--ncookies)
			    ddi_dma_nextcookie(dnetp->dma_handle_tx,
				&dma_cookie);
		}
		(void) ddi_dma_unbind_handle(dnetp->dma_handle_tx);
		bp = bp->b_cont;
	}

	if (error == 1) {
		dnetp->stat_defer++;
		dnetp->free_desc = avail;
		dnetp->need_gld_sched = 1;
		mutex_exit(&dnetp->txlock);
		return (GLD_TX_RESEND);
	} else if (error) {
		dnetp->free_desc = avail;
		mutex_exit(&dnetp->txlock);
		freemsg(mp);
		return (0);	/* Drop packet, don't retry */
	}

	if (totlen > ETHERMAX) {
		cmn_err(CE_WARN, "DNET: tried to send large %d packet", totlen);
		dnetp->free_desc = avail;
		mutex_exit(&dnetp->txlock);
		freemsg(mp);
		return (0);	/* We don't want to repeat this attempt */
	}

	/*
	 * Remeber the message buffer pointer to do freemsg() at xmit
	 * interrupt time.
	 */
	dnetp->tx_msgbufp[end_index] = mp;

	/*
	 * Now set the first/last buffer and own bits
	 * Since the 21040 looks for these bits set in the
	 * first buffer, work backwards in multiple buffers.
	 */
	ring[end_index].desc1.last_desc = 1;
	ring[end_index].desc1.int_on_comp = 1;
	for (index = end_index; index != start_index;
	    index = PrevTXIndex(index))
		ring[index].desc0.own = 1;
	ring[start_index].desc1.first_desc = 1;
	ring[start_index].desc0.own = 1;

	dnetp->tx_current_desc = NextTXIndex(end_index);

	/*
	 * Safety check: make sure end-of-ring is set in last desc.
	 */
	ASSERT(ring[dnetp->max_tx_desc-1].desc1.end_of_ring != 0);

	/*
	 * Enable xmit interrupt if we are running out of xmit descriptors
	 * or there are more packets on the queue waiting to be transmitted.
	 */
#ifdef GLD_INTR_WAIT	/* XXX This relies on new GLD changes */
	if (dnetp->free_desc <= dnet_xmit_threshold)
		tx_interrupt_mask = TX_INTERRUPT_MASK;
	else
		tx_interrupt_mask = (macinfo->gldm_GLD_flags & GLD_INTR_WAIT) ?
					TX_INTERRUPT_MASK : 0;
#else
	tx_interrupt_mask = TX_INTERRUPT_MASK;
#endif

	mutex_exit(&dnetp->txlock);
	mutex_enter(&dnetp->intrlock);
	enable_interrupts(dnetp, tx_interrupt_mask);

	/*
	 * Kick the transmitter
	 */
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, TX_POLL_REG),
	    TX_POLL_DEMAND);
	mutex_exit(&dnetp->intrlock);
	return (GLD_TX_OK);		/* successful transmit attempt */
}

/*
 *	dnetintr() -- interrupt from board to inform us that a receive or
 *	transmit has completed.
 */


static uint_t
dnetintr(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp =	/* Our private device info */
				(struct dnetinstance *)macinfo->gldm_private;
	uint32_t		int_status;
	uint32_t		tx_interrupt_mask;

	mutex_enter(&dnetp->intrlock);
#ifdef DNETDEBUG
	if (dnetdebug & DNETINT)
		cmn_err(CE_NOTE, "dnetintr(0x%p)", (void *)macinfo);
#endif
	int_status = ddi_get32(dnetp->io_handle, REG32(dnetp->io_reg,
			STATUS_REG));

	/*
	 * If interrupt was not from this board
	 */
	if (!(int_status & (NORMAL_INTR_SUMM | ABNORMAL_INTR_SUMM))) {
		mutex_exit(&dnetp->intrlock);
		return (DDI_INTR_UNCLAIMED);
	}

	dnetp->stat_intr++;

	if (int_status & GPTIMER_INTR) {
		ddi_put32(dnetp->io_handle,
			    REG32(dnetp->io_reg, STATUS_REG), GPTIMER_INTR);
		if (dnetp->timer.cb)
			dnetp->timer.cb(dnetp);
		else
			cmn_err(CE_WARN, "dnet: unhandled timer interrupt");
	}

	if (int_status & TX_INTR) {
		ddi_put32(dnetp->io_handle,
			    REG32(dnetp->io_reg, STATUS_REG), TX_INTR);
		mutex_enter(&dnetp->txlock);
		if (dnetp->need_gld_sched) {
			mutex_exit(&dnetp->txlock);
			mutex_exit(&dnetp->intrlock);
			gld_sched(macinfo);
			mutex_enter(&dnetp->intrlock);
			mutex_enter(&dnetp->txlock);
			dnetp->need_gld_sched = 0;
		}
		/* reclaim any xmit descriptors that are completed */
		dnet_reclaim_Tx_desc(macinfo);
		mutex_exit(&dnetp->txlock);
	}

	/*
	 * Check if receive interrupt bit is set
	 */
	if (int_status & RX_INTR) {
		ddi_put32(dnetp->io_handle,
			    REG32(dnetp->io_reg, STATUS_REG), RX_INTR);
		dnet_getp(macinfo);
	}

	if (int_status & ABNORMAL_INTR_SUMM) {
		/*
		 * Check for system error
		 */
		if (int_status & SYS_ERR) {
			if ((int_status & SYS_ERR_BITS) == MASTER_ABORT)
				cmn_err(CE_WARN, "DNET: Bus Master Abort");
			if ((int_status & SYS_ERR_BITS) == TARGET_ABORT)
				cmn_err(CE_WARN, "DNET: Bus Target Abort");
			if ((int_status & SYS_ERR_BITS) == PARITY_ERROR)
				cmn_err(CE_WARN, "DNET: Parity error");
		}

		/*
		 * If the jabber has timed out then reset the chip
		 */
		if (int_status & TX_JABBER_TIMEOUT)
			cmn_err(CE_WARN, "DNET: Jabber timeout.");

		/*
		 * If an underflow has occurred, reset the chip
		 */
		if (int_status & TX_UNDERFLOW)
			cmn_err(CE_WARN, "DNET: Tx Underflow.");

#ifdef DNETDEBUG
		if (dnetdebug & DNETINT)
			cmn_err(CE_NOTE, "Trying to reset...");
#endif
		dnet_reset_board(macinfo);
		dnet_init_board(macinfo);
		/* XXX function return value ignored */
		(void) dnet_start(macinfo);
	}

	/*
	 * Enable the interrupts. Enable xmit interrupt only if we are
	 * running out of free descriptors or if there are packets
	 * in the queue waiting to be transmitted.
	 */
#ifdef GLD_INTR_WAIT	/* XXX This relies on new GLD changes */
	if (dnetp->free_desc <= dnet_xmit_threshold)
		tx_interrupt_mask = TX_INTERRUPT_MASK;
	else
		tx_interrupt_mask = (macinfo->gldm_GLD_flags & GLD_INTR_WAIT) ?
					TX_INTERRUPT_MASK : 0;
#else
	tx_interrupt_mask = TX_INTERRUPT_MASK;
#endif
	enable_interrupts(dnetp, tx_interrupt_mask);
	mutex_exit(&dnetp->intrlock);
	return (DDI_INTR_CLAIMED);	/* Indicate it was our interrupt */
}

static void
dnet_getp(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp =
			(struct dnetinstance *)macinfo->gldm_private;
	int packet_length, index;
	mblk_t	*mp;
	caddr_t 	virtual_address;
	struct	rx_desc_type *desc = dnetp->rx_desc;
	int marker = dnetp->rx_current_desc;
	int misses;

#ifdef DNETDEBUG
	if (dnetdebug & DNETRECV)
		cmn_err(CE_NOTE, "dnet_getp(0x%p)", (void *)macinfo);
#endif

	if (!dnetp->overrun_workaround) {
		/*
		 * If the workaround is not in place, we must still update
		 * the missed frame statistic from the on-chip counter.
		 */
		misses = ddi_get32(dnetp->io_handle,
		    REG32(dnetp->io_reg, MISSED_FRAME_REG));
		dnetp->stat_missed += (misses & MISSED_FRAME_MASK);
	}

	/* While host owns the current descriptor */
	while (!(desc[dnetp->rx_current_desc].desc0.own)) {
		struct free_ptr *frp;
		caddr_t newbuf;
		struct rbuf_list *rp;

		index = dnetp->rx_current_desc;
		ASSERT(desc[index].desc0.first_desc != 0);

		/*
		 * DMA overrun errata from DEC: avoid possible bus hangs
		 * and data corruption
		 */
		if (dnetp->overrun_workaround &&
		    marker == dnetp->rx_current_desc) {
			int opn;
			do {
				marker = (marker+1) % dnetp->max_rx_desc;
			} while (!(dnetp->rx_desc[marker].desc0.own) &&
			    marker != index);

			misses = ddi_get32(dnetp->io_handle,
			    REG32(dnetp->io_reg, MISSED_FRAME_REG));
			dnetp->stat_missed +=
			    (misses & MISSED_FRAME_MASK);
			if (misses & OVERFLOW_COUNTER_MASK) {
				/*
				 * Overflow(s) have occurred : stop receiver,
				 * and wait until in stopped state
				 */
				opn = ddi_get32(dnetp->io_handle,
				    REG32(dnetp->io_reg, OPN_MODE_REG));
				ddi_put32(dnetp->io_handle,
				    REG32(dnetp->io_reg, OPN_MODE_REG),
				    opn & ~(START_RECEIVE));

				do {
					drv_usecwait(10);
				} while ((ddi_get32(dnetp->io_handle,
				    REG32(dnetp->io_reg, STATUS_REG)) &
				    RECEIVE_PROCESS_STATE) != 0);
#ifdef DNETDEBUG
				if (dnetdebug & DNETRECV)
					cmn_err(CE_CONT, "^*");
#endif
				/* Discard probably corrupt frames */
				while (!(dnetp->rx_desc[index].desc0.own)) {
					dnetp->rx_desc[index].desc0.own = 1;
					index = (index+1) % dnetp->max_rx_desc;
					dnetp->stat_missed++;
				}

				/* restart the receiver */
				opn = ddi_get32(dnetp->io_handle,
				    REG32(dnetp->io_reg, OPN_MODE_REG));
				ddi_put32(dnetp->io_handle,
				    REG32(dnetp->io_reg, OPN_MODE_REG),
				    opn | START_RECEIVE);
				marker = dnetp->rx_current_desc = index;
				continue;
			}
			/*
			 * At this point, we know that all packets before
			 * "marker" were received before a dma overrun occurred
			 */
		}

		/*
		 * If we get an oversized packet it could span multiple
		 * descriptors.  If this happens an error bit should be set.
		 */
		while (desc[index].desc0.last_desc == 0) {
			index = (index + 1) % dnetp->max_rx_desc;
			if (desc[index].desc0.own)
				return;	/* not done receiving large packet */
		}
		while (dnetp->rx_current_desc != index) {
			desc[dnetp->rx_current_desc].desc0.own = 1;
			dnetp->rx_current_desc =
			    (dnetp->rx_current_desc + 1) % dnetp->max_rx_desc;
#ifdef DNETDEBUG
			if (dnetdebug & DNETRECV)
				cmn_err(CE_WARN, "dnet: received large packet");
#endif
		}

		packet_length = desc[index].desc0.frame_len;

		/*
		 * Remove CRC from received data. This is an artefact of the
		 * 21x4x chip and should not be passed higher up the network
		 * stack.
		 */
		packet_length -= ETHERFCSL;

#ifdef DNETDEBUG
		if (dnetdebug & DNETRECV) {
			if (packet_length > ETHERMAX)
				cmn_err(CE_WARN, "dnet: large packet size %d",
				    packet_length);
		}
#endif

		/* get the virtual address of the packet received */
		virtual_address =
		    dnetp->rx_buf_vaddr[index];

		/*
		 * If no packet errors then do:
		 * 	1. Allocate a new receive buffer so that we can
		 *	   use the current buffer as streams buffer to
		 *	   avoid bcopy.
		 *	2. If we got a new receive buffer then allocate
		 *	   an mblk using desballoc().
		 *	3. Otherwise use the mblk from allocb() and do
		 *	   the bcopy.
		 */
		frp = NULL;
		rp = NULL;
		newbuf = NULL;
		mp = NULL;
		if (!desc[index].desc0.err_summary) {
			ASSERT(packet_length < rx_buf_size);
			/*
			 * Allocate another receive buffer for this descriptor.
			 * If we fail to allocate then we do the normal bcopy.
			 */
			rp = dnet_rbuf_alloc(dnetp->devinfo, 0);
			if (rp != NULL) {
				newbuf = rp->rbuf_vaddr;
				frp = kmem_zalloc(sizeof (*frp), KM_NOSLEEP);
				if (frp != NULL) {
				    frp->free_rtn.free_func = dnet_freemsg_buf;
				    frp->free_rtn.free_arg = (char *)frp;
				    frp->buf = virtual_address;
				    mp = desballoc((uchar_t *)virtual_address,
					packet_length, 0, &frp->free_rtn);
				    if (mp == NULL) {
					kmem_free(frp, sizeof (*frp));
					dnet_rbuf_free((caddr_t)newbuf);
					frp = NULL;
					newbuf = NULL;
				    }
				}
			}
			if (mp == NULL) {
				if (newbuf != NULL)
					dnet_rbuf_free((caddr_t)newbuf);
				mp = allocb(packet_length, 0);
			}
		}

		if (desc[index].desc0.err_summary || (mp == NULL)) {

			/* Update gld statistics */
			if (desc[index].desc0.err_summary)
				update_rx_stats(macinfo, index);
			else
				dnetp->stat_norcvbuf++;

			/*
			 * Reset ownership of the descriptor.
			 */
			desc[index].desc0.own = 1;
			dnetp->rx_current_desc =
			    (dnetp->rx_current_desc+1) % dnetp->max_rx_desc;

			/* Demand receive polling by the chip */
			ddi_put32(dnetp->io_handle,
			    REG32(dnetp->io_reg, RX_POLL_REG), RX_POLL_DEMAND);

			continue;
		}

		if (newbuf != NULL) {
			uint32_t end_paddr;
			/* attach the new buffer to the rx descriptor */
			dnetp->rx_buf_vaddr[index] = newbuf;
			desc[index].buffer1 = (uint32_t)rp->rbuf_paddr;
			desc[index].desc1.buffer_size1 = rx_buf_size;
			desc[index].desc1.buffer_size2 = 0;
			end_paddr = (uint32_t)rp->rbuf_endpaddr;
			if ((desc[index].buffer1 & ~dnetp->pgmask) !=
			    (end_paddr & ~dnetp->pgmask)) {
				/* discontiguous */
				desc[index].buffer2 = end_paddr&~dnetp->pgmask;
				desc[index].desc1.buffer_size2 =
				    (end_paddr & dnetp->pgmask) + 1;
				desc[index].desc1.buffer_size1 =
				    rx_buf_size-desc[index].desc1.buffer_size2;
			}
		} else {
		    /* couldn't allocate another buffer; copy the data */
		    BCOPY((caddr_t)virtual_address, (caddr_t)mp->b_wptr,
			packet_length);
		}

		mp->b_wptr += packet_length;

		desc[dnetp->rx_current_desc].desc0.own = 1;

		/*
		 * Increment receive desc index. This is for the scan of
		 * next packet
		 */
		dnetp->rx_current_desc =
			(dnetp->rx_current_desc+1) % dnetp->max_rx_desc;

		/* Demand polling by chip */
		ddi_put32(dnetp->io_handle,
			    REG32(dnetp->io_reg, RX_POLL_REG), RX_POLL_DEMAND);

		/* send the packet upstream */
		mutex_exit(&dnetp->intrlock);
		gld_recv(macinfo, mp);
		mutex_enter(&dnetp->intrlock);
	}
}
/*
 * Function to update receive statistics
 */
static void
update_rx_stats(gld_mac_info_t *macinfo, int index)
{
	struct	dnetinstance	*dnetp =
			(struct dnetinstance *)macinfo->gldm_private;
	struct rx_desc_type *descp = &(dnetp->rx_desc[index]);

	/*
	 * Update gld statistics
	 */
	dnetp->stat_errrcv++;

	if (descp->desc0.overflow)	{
		/* FIFO Overrun */
		dnetp->stat_overflow++;
	}

	if (descp->desc0.collision) {
		/*EMPTY*/
		/* Late Colllision on receive */
		/* no appropriate counter */
	}

	if (descp->desc0.crc) {
		/* CRC Error */
		dnetp->stat_crc++;
	}

	if (descp->desc0.runt_frame) {
		/* Runt Error */
		dnetp->stat_short++;
	}

	if (descp->desc0.desc_err) {
		/*EMPTY*/
		/* Not enough receive descriptors */
		/* This condition is accounted in dnetintr() */
		/* macinfo->gldm_stats.glds_missed++; */
	}

	if (descp->desc0.frame2long) {
		dnetp->stat_frame++;
	}
}

/*
 * Function to update transmit statistics
 */
static void
update_tx_stats(gld_mac_info_t *macinfo, int index)
{
	struct	dnetinstance	*dnetp =
			(struct dnetinstance *)macinfo->gldm_private;
	struct tx_desc_type *descp = &(dnetp->tx_desc[index]);
	int	fd;
	media_block_t	*block = dnetp->selected_media_block;


	/* Update gld statistics */
	dnetp->stat_errxmt++;

	/* If we're in full-duplex don't count collisions or carrier loss. */
	if (dnetp->mii_up) {
		fd = dnetp->mii_duplex;
	} else {
		/* Rely on media code */
		fd =
			block->media_code == MEDIA_TP_FD ||
			block->media_code == MEDIA_SYM_SCR_FD;
	}

	if (descp->desc0.collision_count && !fd) {
		dnetp->stat_collisions +=
			descp->desc0.collision_count;
	}

	if (descp->desc0.late_collision && !fd) {
		dnetp->stat_xmtlatecoll++;
	}

	if (descp->desc0.excess_collision && !fd) {
		dnetp->stat_excoll++;
	}

	if (descp->desc0.underflow) {
		dnetp->stat_underflow++;
	}

#if 0
	if (descp->desc0.tx_jabber_to) {
		/* no appropriate counter */
	}
#endif

	if (descp->desc0.carrier_loss && !fd) {
		dnetp->stat_nocarrier++;
	}

	if (descp->desc0.no_carrier && !fd) {
		dnetp->stat_nocarrier++;
	}
}

/*
 *	========== Media Selection Setup Routines ==========
 */


static void
write_gpr(struct dnetinstance *dnetp, uint32_t val)
{
#ifdef DEBUG
	if (dnetdebug & DNETREGCFG)
		cmn_err(CE_NOTE, "GPR: %x", val);
#endif
	switch (dnetp->board_type) {
	case DEVICE_ID_21143:
		/* Set the correct bit for a control write */
		if (val & GPR_CONTROL_WRITE)
			val |= CWE_21143, val &= ~GPR_CONTROL_WRITE;
		/* Write to upper half of CSR15 */
		dnetp->gprsia = (dnetp->gprsia & 0xffff) | (val << 16);
		ddi_put32(dnetp->io_handle,
			REG32(dnetp->io_reg, SIA_GENERAL_REG),
			dnetp->gprsia);
		break;
	default:
		/* Set the correct bit for a control write */
		if (val & GPR_CONTROL_WRITE)
			val |= CWE_21140, val &= ~GPR_CONTROL_WRITE;
		ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, GP_REG),
			val);
		break;
	}
}

static uint32_t
read_gpr(struct dnetinstance *dnetp)
{
	switch (dnetp->board_type) {
	case DEVICE_ID_21143:
		/* Read upper half of CSR15 */
		return (ddi_get32(dnetp->io_handle,
			REG32(dnetp->io_reg, SIA_GENERAL_REG)) >> 16);
	default:
		return (ddi_get32(dnetp->io_handle,
				REG32(dnetp->io_reg, GP_REG)));
	}
}

static void
set_gpr(gld_mac_info_t *macinfo)
{
	uint32_t *sequence;
	int len;
	struct dnetinstance *dnetp = (struct dnetinstance *)
					macinfo->gldm_private;
	LEAF_FORMAT *leaf = &dnetp->sr.leaf[dnetp->leaf];
	media_block_t *block = dnetp->selected_media_block;
	int i;

	if (ddi_getlongprop(DDI_DEV_T_ANY, dnetp->devinfo,
	    DDI_PROP_DONTPASS, "gpr-sequence", (caddr_t)&sequence,
	    &len) == DDI_PROP_SUCCESS) {
		for (i = 0; i < len / sizeof (uint32_t); i++)
			write_gpr(dnetp, sequence[i]);
		kmem_free(sequence, len);
	} else {
		/*
		 * Write the reset sequence if this is the first time this
		 * block has been selected.
		 */
		if (block->rstseqlen) {
			for (i = 0; i < block->rstseqlen; i++)
				write_gpr(dnetp, block->rstseq[i]);
			/*
			 * XXX Legacy blocks do not have reset sequences, so the
			 * static blocks will never be modified by this
			 */
			block->rstseqlen = 0;
		}
		if (leaf->gpr)
			write_gpr(dnetp, leaf->gpr | GPR_CONTROL_WRITE);

		/* write GPR sequence each time */
		for (i = 0; i < block->gprseqlen; i++)
			write_gpr(dnetp, block->gprseq[i]);
	}

	/* This has possibly caused a PHY to reset.  Let MII know */
	if (dnetp->phyaddr != -1)
		/* XXX function return value ignored */
		(void) mii_sync(dnetp->mii, dnetp->phyaddr);
	drv_usecwait(5);
}

/* set_opr() - must be called with intrlock held */

static void
set_opr(gld_mac_info_t *macinfo)
{
	uint32_t fd, mb1, sf;

	struct dnetinstance *dnetp =
		(struct dnetinstance *)macinfo->gldm_private;
	int 		opnmode_len;
	uint32_t val;
	media_block_t *block = dnetp->selected_media_block;

	ASSERT(block);

	/* Check for custom "opnmode_reg" property */
	opnmode_len = sizeof (val);
	if (ddi_prop_op(DDI_DEV_T_ANY, dnetp->devinfo,
	    PROP_LEN_AND_VAL_BUF, DDI_PROP_DONTPASS, "opnmode_reg",
	    (caddr_t)&val, &opnmode_len) != DDI_PROP_SUCCESS)
		opnmode_len = 0;

	/* Some bits exist only on 21140 and greater */
	if (dnetp->board_type != DEVICE_ID_21040 &&
	    dnetp->board_type != DEVICE_ID_21041) {
		mb1 = OPN_REG_MB1;
		sf = STORE_AND_FORWARD;
	} else {
		mb1 = sf = 0;
		mb1 = OPN_REG_MB1; /* Needed for 21040? */
	}

	if (opnmode_len) {
		ddi_put32(dnetp->io_handle,
			REG32(dnetp->io_reg, OPN_MODE_REG), val);
		dnet_reset_board(macinfo);
		ddi_put32(dnetp->io_handle,
			REG32(dnetp->io_reg, OPN_MODE_REG), val);
		return;
	}

	/*
	 * Set each bit in CSR6 that we want
	 */

	/* Always want these bits set */
	val = HASH_FILTERING | HASH_ONLY | TX_THRESHOLD_160 | mb1 | sf;

	/* Promiscuous mode */
	val |= dnetp->promisc ? PROM_MODE : 0;

	/* Scrambler for SYM style media */
	val |= ((block->command & CMD_SCR) && !dnetp->disable_scrambler) ?
		SCRAMBLER_MODE : 0;

	/* Full duplex */
	if (dnetp->mii_up) {
		fd = dnetp->mii_duplex;
	} else {
		/* Rely on media code */
		fd =
		    block->media_code == MEDIA_TP_FD ||
		    block->media_code == MEDIA_SYM_SCR_FD;
	}

	/* Port select (and therefore, heartbeat disable) */
	val |= block->command & CMD_PS ? (PORT_SELECT | HEARTBEAT_DISABLE) : 0;

	/* PCS function */
	val |= (block->command) & CMD_PCS ? PCS_FUNCTION : 0;
	val |= fd ? FULL_DUPLEX : 0;

#ifdef DNETDEBUG
	if (dnetdebug & DNETREGCFG)
		cmn_err(CE_NOTE, "OPN: %x", val);
#endif
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, OPN_MODE_REG), val);
	dnet_reset_board(macinfo);
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, OPN_MODE_REG), val);
}

static void
set_sia(gld_mac_info_t *macinfo)
{
	struct dnetinstance *dnetp = (struct dnetinstance *)
	    (macinfo->gldm_private);
	media_block_t *block = dnetp->selected_media_block;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	if (block->type == 2) {
		int sia_delay;
#ifdef DNETDEBUG
		if (dnetdebug & DNETREGCFG)
			cmn_err(CE_NOTE,
			    "SIA: CSR13: %x, CSR14: %x, CSR15: %x",
			    block->un.sia.csr13,
			    block->un.sia.csr14,
			    block->un.sia.csr15);
#endif
		sia_delay = ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
		    DDI_PROP_DONTPASS, "sia-delay", 10000);

		ddi_put32(dnetp->io_handle,
			REG32(dnetp->io_reg, SIA_CONNECT_REG), 0);

		ddi_put32(dnetp->io_handle,
			REG32(dnetp->io_reg, SIA_TXRX_REG),
		    block->un.sia.csr14);

		/*
		 * For '143, we need to write through a copy of the register
		 * to keep the GP half intact
		 */
		dnetp->gprsia = (dnetp->gprsia&0xffff0000)|block->un.sia.csr15;
		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, SIA_GENERAL_REG),
		    dnetp->gprsia);

		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, SIA_CONNECT_REG),
		    block->un.sia.csr13);

		drv_usecwait(sia_delay);

	} else if (dnetp->board_type != DEVICE_ID_21140) {
		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, SIA_CONNECT_REG), 0);
		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, SIA_TXRX_REG), 0);
	}
}

/*
 *	========== Buffer Management Routines ==========
 */

/*
 * This function (re)allocates the receive and transmit buffers and
 * descriptors.  It can be called more than once per instance, though
 * currently it is only called from attach.  It should only be called
 * while the device is reset.
 */
static int
dnet_alloc_bufs(gld_mac_info_t *macinfo)
{
	struct dnetinstance	*dnetp = (struct dnetinstance *)
						(macinfo->gldm_private);
	int i;
	size_t len;
	int page_size;
	int realloc = 0;
	int nrecv_desc_old = 0;
	ddi_dma_cookie_t cookie;
	uint_t ncookies;

	/*
	 * check if we are trying to reallocate with different xmit/recv
	 * descriptor ring sizes.
	 */
	if ((dnetp->tx_desc != NULL) &&
	    (dnetp->nxmit_desc != dnetp->max_tx_desc))
		realloc = 1;

	if ((dnetp->rx_desc != NULL) &&
	    (dnetp->nrecv_desc != dnetp->max_rx_desc))
		realloc = 1;

	/* free up the old buffers if we are reallocating them */
	if (realloc) {
		nrecv_desc_old = dnetp->nrecv_desc;
		dnet_free_bufs(macinfo); /* free the old buffers */
	}

	if (dnetp->dma_handle == NULL)
		if (ddi_dma_alloc_handle(dnetp->devinfo, &dma_attr,
		    DDI_DMA_SLEEP, 0, &dnetp->dma_handle) != DDI_SUCCESS)
			return (FAILURE);

	if (dnetp->dma_handle_tx == NULL)
		if (ddi_dma_alloc_handle(dnetp->devinfo, &dma_attr_tx,
		    DDI_DMA_SLEEP, 0, &dnetp->dma_handle_tx) != DDI_SUCCESS)
			return (FAILURE);

	if (dnetp->dma_handle_txdesc == NULL)
		if (ddi_dma_alloc_handle(dnetp->devinfo, &dma_attr,
		    DDI_DMA_SLEEP, 0, &dnetp->dma_handle_txdesc) != DDI_SUCCESS)
			return (FAILURE);

	if (dnetp->dma_handle_setbuf == NULL)
		if (ddi_dma_alloc_handle(dnetp->devinfo, &dma_attr,
		    DDI_DMA_SLEEP, 0, &dnetp->dma_handle_setbuf) != DDI_SUCCESS)
			return (FAILURE);

	page_size = ddi_ptob(dnetp->devinfo, 1);
	for (i = page_size, len = 0; i > 1; len++)
		i >>= 1;
	dnetp->pgmask = page_size - 1;
	dnetp->pgshft = len;

	/* allocate setup buffer if necessary */
	if (dnetp->setup_buf_vaddr == NULL) {
		if (ddi_dma_mem_alloc(dnetp->dma_handle_setbuf,
		    SETUPBUF_SIZE, &accattr, DDI_DMA_STREAMING,
		    DDI_DMA_DONTWAIT, 0, (caddr_t *)&dnetp->setup_buf_vaddr,
		    &len, &dnetp->setup_buf_acchdl) != DDI_SUCCESS)
			return (FAILURE);

		if (ddi_dma_addr_bind_handle(dnetp->dma_handle_setbuf,
		    NULL, dnetp->setup_buf_vaddr, SETUPBUF_SIZE,
		    DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP,
		    NULL, &cookie, &ncookies) != DDI_DMA_MAPPED)
			return (FAILURE);

		dnetp->setup_buf_paddr = (caddr_t)(uintptr_t)
		    cookie.dmac_laddress;
		bzero(dnetp->setup_buf_vaddr, len);
	}

	/* allocate xmit descriptor array of size dnetp->max_tx_desc */
	if (dnetp->tx_desc == NULL) {
		if (ddi_dma_mem_alloc(dnetp->dma_handle_txdesc,
		    sizeof (struct tx_desc_type) * dnetp->max_tx_desc,
		    &accattr, DDI_DMA_STREAMING, DDI_DMA_DONTWAIT, 0,
		    (caddr_t *)&dnetp->tx_desc, &len,
		    &dnetp->tx_desc_acchdl) != DDI_SUCCESS)
			return (FAILURE);

		if (ddi_dma_addr_bind_handle(dnetp->dma_handle_txdesc,
		    NULL, (caddr_t)dnetp->tx_desc,
		    sizeof (struct tx_desc_type) * dnetp->max_tx_desc,
		    DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP,
		    NULL, &cookie, &ncookies) != DDI_DMA_MAPPED)
			return (FAILURE);
		dnetp->tx_desc_paddr = (caddr_t)(uintptr_t)
		    cookie.dmac_laddress;
		bzero(dnetp->tx_desc, len);
		dnetp->nxmit_desc = dnetp->max_tx_desc;

		dnetp->tx_msgbufp =
		    kmem_zalloc(dnetp->max_tx_desc * sizeof (mblk_t **),
			KM_SLEEP);
	}

	/* allocate receive descriptor array of size dnetp->max_rx_desc */
	if (dnetp->rx_desc == NULL) {
		int ndesc;

		if (ddi_dma_mem_alloc(dnetp->dma_handle,
		    sizeof (struct rx_desc_type) * dnetp->max_rx_desc,
		    &accattr, DDI_DMA_STREAMING, DDI_DMA_DONTWAIT, 0,
		    (caddr_t *)&dnetp->rx_desc, &len,
		    &dnetp->rx_desc_acchdl) != DDI_SUCCESS)
			return (FAILURE);

		if (ddi_dma_addr_bind_handle(dnetp->dma_handle,
		    NULL, (caddr_t)dnetp->rx_desc,
		    sizeof (struct rx_desc_type) * dnetp->max_rx_desc,
		    DDI_DMA_RDWR | DDI_DMA_STREAMING, DDI_DMA_SLEEP,
		    NULL, &cookie, &ncookies) != DDI_DMA_MAPPED)
			return (FAILURE);

		dnetp->rx_desc_paddr = (caddr_t)(uintptr_t)
		    cookie.dmac_laddress;
		bzero(dnetp->rx_desc, len);
		dnetp->nrecv_desc = dnetp->max_rx_desc;

		dnetp->rx_buf_vaddr =
		    kmem_zalloc(dnetp->max_rx_desc * sizeof (caddr_t),
			KM_SLEEP);
		dnetp->rx_buf_paddr =
		    kmem_zalloc(dnetp->max_rx_desc * sizeof (caddr_t),
			KM_SLEEP);
		/*
		 * Allocate or add to the pool of receive buffers.  The pool
		 * is shared among all instances of dnet.
		 *
		 * XXX NEEDSWORK
		 *
		 * We arbitrarily allocate twice as many receive buffers as
		 * receive descriptors because we use the buffers for streams
		 * messages to pass the packets up the stream.  We should
		 * instead have initialized constants reflecting
		 * MAX_RX_BUF_2104x and MAX_RX_BUF_2114x, and we should also
		 * probably have a total maximum for the free pool, so that we
		 * don't get out of hand when someone puts in an 8-port board.
		 * The maximum for the entire pool should be the total number
		 * of descriptors for all attached instances together, plus the
		 * total maximum for the free pool.  This maximum would only be
		 * reached after some number of instances allocate buffers:
		 * each instance would add (max_rx_buf-max_rx_desc) to the free
		 * pool.
		 */
		ndesc = dnetp->max_rx_desc - nrecv_desc_old;
		if ((ndesc > 0) &&
		    (dnet_rbuf_init(dnetp->devinfo, ndesc * 2) != 0))
			return (FAILURE);

		for (i = 0; i < dnetp->max_rx_desc; i++) {
			struct rbuf_list *rp;

			rp = dnet_rbuf_alloc(dnetp->devinfo, 1);
			if (rp == NULL)
				return (FAILURE);
			dnetp->rx_buf_vaddr[i] = rp->rbuf_vaddr;
		}
	}

	return (SUCCESS);
}
/*
 * free descriptors/buffers allocated for this device instance.  This routine
 * should only be called while the device is reset.
 */
static void
dnet_free_bufs(gld_mac_info_t *macinfo)
{
	int i;
	struct dnetinstance	*dnetp = (struct dnetinstance *)
						(macinfo->gldm_private);
	/* free up any xmit descriptors/buffers */
	if (dnetp->tx_desc != NULL) {
		ddi_dma_mem_free(&dnetp->tx_desc_acchdl);
		dnetp->tx_desc = NULL;
		/* we use streams buffers for DMA in xmit process */
		if (dnetp->tx_msgbufp != NULL) {
		    /* free up any streams message buffers unclaimed */
		    for (i = 0; i < dnetp->nxmit_desc; i++) {
			if (dnetp->tx_msgbufp[i] != NULL) {
				freemsg(dnetp->tx_msgbufp[i]);
			}
		    }
		    kmem_free(dnetp->tx_msgbufp,
			dnetp->nxmit_desc * sizeof (mblk_t **));
		    dnetp->tx_msgbufp = NULL;
		}
		dnetp->nxmit_desc = 0;
	}

	/* free up any receive descriptors/buffers */
	if (dnetp->rx_desc != NULL) {
		ddi_dma_mem_free(&dnetp->rx_desc_acchdl);
		dnetp->rx_desc = NULL;
		if (dnetp->rx_buf_vaddr != NULL) {
			/* free up the attached rbufs if any */
			for (i = 0; i < dnetp->nrecv_desc; i++) {
			    if (dnetp->rx_buf_vaddr[i])
				dnet_rbuf_free((caddr_t)dnetp->rx_buf_vaddr[i]);
			}
		    kmem_free(dnetp->rx_buf_vaddr,
			dnetp->nrecv_desc * sizeof (caddr_t));
		    kmem_free(dnetp->rx_buf_paddr,
			dnetp->nrecv_desc * sizeof (caddr_t));
		    dnetp->rx_buf_vaddr = NULL;
		    dnetp->rx_buf_paddr = NULL;
		}
		dnetp->nrecv_desc = 0;
	}

	if (dnetp->setup_buf_vaddr != NULL) {
		ddi_dma_mem_free(&dnetp->setup_buf_acchdl);
		dnetp->setup_buf_vaddr = NULL;
	}

	if (dnetp->dma_handle != NULL) {
		(void) ddi_dma_unbind_handle(dnetp->dma_handle);
		ddi_dma_free_handle(&dnetp->dma_handle);
		dnetp->dma_handle = NULL;
	}

	if (dnetp->dma_handle_tx != NULL) {
		(void) ddi_dma_unbind_handle(dnetp->dma_handle_tx);
		ddi_dma_free_handle(&dnetp->dma_handle_tx);
		dnetp->dma_handle_tx = NULL;
	}

	if (dnetp->dma_handle_txdesc != NULL) {
		(void) ddi_dma_unbind_handle(dnetp->dma_handle_txdesc);
		ddi_dma_free_handle(&dnetp->dma_handle_txdesc);
		dnetp->dma_handle_txdesc = NULL;
	}

	if (dnetp->dma_handle_setbuf != NULL) {
		(void) ddi_dma_unbind_handle(dnetp->dma_handle_setbuf);
		ddi_dma_free_handle(&dnetp->dma_handle_setbuf);
		dnetp->dma_handle_setbuf = NULL;
	}

}

/*
 * Initialize transmit and receive descriptors.
 */
static void
dnet_init_txrx_bufs(gld_mac_info_t *macinfo)
{
	int		i;
	struct dnetinstance	*dnetp = (struct dnetinstance *)
						(macinfo->gldm_private);

	/*
	 * Initilize all the Tx descriptors
	 */
	for (i = 0; i < dnetp->nxmit_desc; i++) {
		/*
		 * We may be resetting the device due to errors,
		 * so free up any streams message buffer unclaimed.
		 */
		if (dnetp->tx_msgbufp[i] != NULL) {
			freemsg(dnetp->tx_msgbufp[i]);
			dnetp->tx_msgbufp[i] = NULL;
		}
		*(uint32_t *)&dnetp->tx_desc[i].desc0 = 0;
		*(uint32_t *)&dnetp->tx_desc[i].desc1 = 0;
		dnetp->tx_desc[i].buffer1 = (uint32_t)(0);
		dnetp->tx_desc[i].buffer2 = (uint32_t)(0);
	}
	dnetp->tx_desc[i - 1].desc1.end_of_ring = 1;

	/*
	 * Initialize the Rx descriptors
	 */
	for (i = 0; i < dnetp->nrecv_desc; i++) {
		uint32_t end_paddr;
		*(uint32_t *)&dnetp->rx_desc[i].desc0 = 0;
		*(uint32_t *)&dnetp->rx_desc[i].desc1 = 0;
		dnetp->rx_desc[i].desc0.own = 1;
		dnetp->rx_desc[i].desc1.buffer_size1 = rx_buf_size;
		dnetp->rx_desc[i].buffer1 =
			(uint32_t)dnetp->rx_buf_paddr[i];
		dnetp->rx_desc[i].buffer2 = (uint32_t)(0);
		end_paddr =
			(uint32_t)dnetp->rx_buf_paddr[i]+rx_buf_size-1;

		if ((dnetp->rx_desc[i].buffer1 & ~dnetp->pgmask) !=
		    (end_paddr & ~dnetp->pgmask)) {
			/* discontiguous */
			dnetp->rx_desc[i].buffer2 = end_paddr&~dnetp->pgmask;
			dnetp->rx_desc[i].desc1.buffer_size2 =
			    (end_paddr & dnetp->pgmask) + 1;
			dnetp->rx_desc[i].desc1.buffer_size1 =
			    rx_buf_size-dnetp->rx_desc[i].desc1.buffer_size2;
		}
	}
	dnetp->rx_desc[i - 1].desc1.end_of_ring = 1;
}

static int
alloc_descriptor(gld_mac_info_t *macinfo)
{
	int index;
	struct dnetinstance *dnetp =	/* Our private device info */
		(struct dnetinstance *)macinfo->gldm_private;
	struct tx_desc_type    *ring = dnetp->tx_desc;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
alloctop:
	mutex_enter(&dnetp->txlock);
	index = dnetp->tx_current_desc;

	dnet_reclaim_Tx_desc(macinfo);

	/* we do have free descriptors, right? */
	if (dnetp->free_desc <= 0) {
#ifdef DNETDEBUG
		if (dnetdebug & DNETRECV)
			cmn_err(CE_NOTE, "dnet: Ring buffer is full");
#endif
		mutex_exit(&dnetp->txlock);
		return (FAILURE);
	}

	/* sanity, make sure the next descriptor is free for use (should be) */
	if (ring[index].desc0.own) {
#ifdef DNETDEBUG
		if (dnetdebug & DNETRECV)
			cmn_err(CE_WARN,
				"dnet: next descriptor is not free for use");
#endif
		mutex_exit(&dnetp->txlock);
		return (FAILURE);
	}
	if (dnetp->need_saddr) {
		mutex_exit(&dnetp->txlock);
		/* XXX function return value ignored */
		/* XXX function return value ignored */
		(void) dnet_set_addr(macinfo, dnetp->curr_macaddr);
		goto alloctop;
	}

	*(uint32_t *)&ring[index].desc0 = 0;  /* init descs */
	*(uint32_t *)&ring[index].desc1 &= DNET_END_OF_RING;

	/* hardware will own this descriptor when poll activated */
	dnetp->free_desc--;

	/* point to next free descriptor to be used */
	dnetp->tx_current_desc = NextTXIndex(index);

#ifdef DNET_NOISY
	cmn_err(CE_WARN, "sfree 0x%x, transmitted 0x%x, tx_current 0x%x",
	    dnetp->free_desc, dnetp->transmitted_desc, dnetp->tx_current_desc);
#endif
	mutex_exit(&dnetp->txlock);
	return (SUCCESS);
}

/*
 * dnet_reclaim_Tx_desc() - called with txlock held.
 */
static void
dnet_reclaim_Tx_desc(gld_mac_info_t *macinfo)
{
	struct dnetinstance	*dnetp = (struct dnetinstance *)
						(macinfo->gldm_private);
	struct tx_desc_type	*desc = dnetp->tx_desc;
	int index;

	ASSERT(MUTEX_HELD(&dnetp->txlock));
#ifdef DNETDEBUG
	if (dnetdebug & DNETTRACE)
		cmn_err(CE_NOTE, "dnet_reclaim_Tx_desc(0x%p)",
			(void *) macinfo);
#endif

	index = dnetp->transmitted_desc;
	while (((dnetp->free_desc == 0) || (index != dnetp->tx_current_desc)) &&
	    !(desc[index].desc0.own)) {
		/*
		 * Check for Tx Error that gets set
		 * in the last desc.
		 */
		if (desc[index].desc1.setup_packet == 0 &&
		    desc[index].desc1.last_desc &&
		    desc[index].desc0.err_summary)
			update_tx_stats(macinfo, index);

		/*
		 * If we have used the streams message buffer for this
		 * descriptor then free up the message now.
		 */
		if (dnetp->tx_msgbufp[index] != NULL) {
			freemsg(dnetp->tx_msgbufp[index]);
			dnetp->tx_msgbufp[index] = NULL;
		}
		dnetp->free_desc++;
		index = (index+1) % dnetp->max_tx_desc;
	}

	dnetp->transmitted_desc = index;
}

/*
 * Receive buffer allocation/freeing routines.
 *
 * There is a common pool of receive buffers shared by all dnet instances.
 *
 * XXX NEEDSWORK
 *
 * We arbitrarily allocate twice as many receive buffers as
 * receive descriptors because we use the buffers for streams
 * messages to pass the packets up the stream.  We should
 * instead have initialized constants reflecting
 * MAX_RX_BUF_2104x and MAX_RX_BUF_2114x, and we should also
 * probably have a total maximum for the free pool, so that we
 * don't get out of hand when someone puts in an 8-port board.
 * The maximum for the entire pool should be the total number
 * of descriptors for all attached instances together, plus the
 * total maximum for the free pool.  This maximum would only be
 * reached after some number of instances allocate buffers:
 * each instance would add (max_rx_buf-max_rx_desc) to the free
 * pool.
 */

static struct rbuf_list *rbuf_usedlist_head;
static struct rbuf_list *rbuf_freelist_head;
static struct rbuf_list *rbuf_usedlist_end;	/* last buffer allocated */

static int rbuf_freebufs;	/* no. of free buffers in the pool */
static int rbuf_pool_size;	/* total no. of buffers in the pool */

/* initialize/add 'nbufs' buffers to the rbuf pool */
/* ARGSUSED */
static int
dnet_rbuf_init(dev_info_t *dip, int nbufs)
{
	int i;
	struct rbuf_list *rp;
	ddi_dma_cookie_t cookie;
	uint_t ncookies;
	size_t len;

	mutex_enter(&dnet_rbuf_lock);

	/* allocate buffers and add them to the pool */
	for (i = 0; i < nbufs; i++) {
		/* allocate rbuf_list element */
		rp = kmem_zalloc(sizeof (struct rbuf_list), KM_SLEEP);
		if (ddi_dma_alloc_handle(dip, &dma_attr_rb, DDI_DMA_SLEEP,
				0, &rp->rbuf_dmahdl) != DDI_SUCCESS)
			goto fail_kfree;

		/* allocate dma memory for the buffer */
		if (ddi_dma_mem_alloc(rp->rbuf_dmahdl, rx_buf_size, &accattr,
		    DDI_DMA_STREAMING, DDI_DMA_DONTWAIT, 0,
		    &rp->rbuf_vaddr, &len,
		    &rp->rbuf_acchdl) != DDI_SUCCESS)
			goto fail_freehdl;

		if (ddi_dma_addr_bind_handle(rp->rbuf_dmahdl, NULL,
		    rp->rbuf_vaddr, len, DDI_DMA_RDWR | DDI_DMA_STREAMING,
		    DDI_DMA_SLEEP, NULL, &cookie,
		    &ncookies) != DDI_DMA_MAPPED)
			goto fail_free;

		if (ncookies > 2)
			goto fail_unbind;
		if (ncookies == 1) {
			rp->rbuf_endpaddr = (caddr_t)(uintptr_t)
			    (cookie.dmac_laddress + rx_buf_size - 1);
		} else {
			ddi_dma_nextcookie(rp->rbuf_dmahdl, &cookie);
			rp->rbuf_endpaddr = (caddr_t)(uintptr_t)
			    (cookie.dmac_laddress + cookie.dmac_size - 1);
		}
		rp->rbuf_paddr = (caddr_t)(uintptr_t)cookie.dmac_laddress;

		rp->rbuf_next = rbuf_freelist_head;
		rbuf_freelist_head = rp;
		rbuf_pool_size++;
		rbuf_freebufs++;
	}

	mutex_exit(&dnet_rbuf_lock);
	return (0);
fail_unbind:
	(void) ddi_dma_unbind_handle(rp->rbuf_dmahdl);
fail_free:
	ddi_dma_mem_free(&rp->rbuf_acchdl);
fail_freehdl:
	ddi_dma_free_handle(&rp->rbuf_dmahdl);
fail_kfree:
	kmem_free(rp, sizeof (struct rbuf_list));

	mutex_exit(&dnet_rbuf_lock);
	return (-1);
}

/*
 * Try to free up all the rbufs in the pool. Returns 0 if it frees up all
 * buffers. The buffers in the used list are considered busy so these
 * buffers are not freed.
 */
static int
dnet_rbuf_destroy()
{
	struct rbuf_list *rp, *next;

	mutex_enter(&dnet_rbuf_lock);

	for (rp = rbuf_freelist_head; rp; rp = next) {
		next = rp->rbuf_next;
		ddi_dma_mem_free(&rp->rbuf_acchdl);
		(void) ddi_dma_unbind_handle(rp->rbuf_dmahdl);
		kmem_free(rp, sizeof (struct rbuf_list));
		rbuf_pool_size--;
		rbuf_freebufs--;
	}
	rbuf_freelist_head = NULL;

	if (rbuf_pool_size) { /* pool is still not empty */
		mutex_exit(&dnet_rbuf_lock);
		return (-1);
	}
	mutex_exit(&dnet_rbuf_lock);
	return (0);
}
static struct rbuf_list *
dnet_rbuf_alloc(dev_info_t *dip, int cansleep)
{
	struct rbuf_list *rp;
	size_t len;
	ddi_dma_cookie_t cookie;
	uint_t ncookies;

	mutex_enter(&dnet_rbuf_lock);

	if (rbuf_freelist_head == NULL) {

		if (!cansleep) {
			mutex_exit(&dnet_rbuf_lock);
			return (NULL);
		}

		/* allocate rbuf_list element */
		rp = kmem_zalloc(sizeof (struct rbuf_list), KM_SLEEP);
		if (ddi_dma_alloc_handle(dip, &dma_attr_rb, DDI_DMA_SLEEP,
				0, &rp->rbuf_dmahdl) != DDI_SUCCESS)
			goto fail_kfree;

		/* allocate dma memory for the buffer */
		if (ddi_dma_mem_alloc(rp->rbuf_dmahdl, rx_buf_size, &accattr,
		    DDI_DMA_STREAMING, DDI_DMA_DONTWAIT, 0,
		    &rp->rbuf_vaddr, &len,
		    &rp->rbuf_acchdl) != DDI_SUCCESS)
			goto fail_freehdl;

		if (ddi_dma_addr_bind_handle(rp->rbuf_dmahdl, NULL,
		    rp->rbuf_vaddr, len, DDI_DMA_RDWR | DDI_DMA_STREAMING,
		    DDI_DMA_SLEEP, NULL, &cookie,
		    &ncookies) != DDI_DMA_MAPPED)
			goto fail_free;

		if (ncookies > 2)
			goto fail_unbind;
		if (ncookies == 1) {
			rp->rbuf_endpaddr = (caddr_t)(uintptr_t)
			    (cookie.dmac_laddress + rx_buf_size - 1);
		} else {
			ddi_dma_nextcookie(rp->rbuf_dmahdl, &cookie);
			rp->rbuf_endpaddr = (caddr_t)(uintptr_t)
			    (cookie.dmac_laddress + cookie.dmac_size - 1);
		}
		rp->rbuf_paddr = (caddr_t)(uintptr_t)cookie.dmac_laddress;

		rbuf_freelist_head = rp;
		rbuf_pool_size++;
		rbuf_freebufs++;
	}

	/* take the buffer from the head of the free list */
	rp = rbuf_freelist_head;
	rbuf_freelist_head = rbuf_freelist_head->rbuf_next;

	/* update the used list; put the entry at the end */
	if (rbuf_usedlist_head == NULL)
		rbuf_usedlist_head = rp;
	else
		rbuf_usedlist_end->rbuf_next = rp;
	rp->rbuf_next = NULL;
	rbuf_usedlist_end = rp;
	rbuf_freebufs--;

	mutex_exit(&dnet_rbuf_lock);

	return (rp);
fail_unbind:
	(void) ddi_dma_unbind_handle(rp->rbuf_dmahdl);
fail_free:
	ddi_dma_mem_free(&rp->rbuf_acchdl);
fail_freehdl:
	ddi_dma_free_handle(&rp->rbuf_dmahdl);
fail_kfree:
	kmem_free(rp, sizeof (struct rbuf_list));
	mutex_exit(&dnet_rbuf_lock);
	return (NULL);
}

static void
dnet_rbuf_free(caddr_t vaddr)
{
	struct rbuf_list *rp, *prev;

	ASSERT(vaddr != NULL);
	ASSERT(rbuf_usedlist_head != NULL);

	mutex_enter(&dnet_rbuf_lock);

	/* find the entry in the used list */
	for (prev = rp = rbuf_usedlist_head; rp; rp = rp->rbuf_next) {
		if (rp->rbuf_vaddr == vaddr)
			break;
		prev = rp;
	}

	if (rp == NULL) {
		cmn_err(CE_WARN, "DNET: rbuf_free: bad addr 0x%p",
			(void *)vaddr);
		mutex_exit(&dnet_rbuf_lock);
		return;
	}

	/* update the used list and put the buffer back in the free list */
	if (rbuf_usedlist_head != rp) {
		prev->rbuf_next = rp->rbuf_next;
		if (rbuf_usedlist_end == rp)
			rbuf_usedlist_end = prev;
	} else {
		rbuf_usedlist_head = rp->rbuf_next;
		if (rbuf_usedlist_end == rp)
			rbuf_usedlist_end = NULL;
	}
	rp->rbuf_next = rbuf_freelist_head;
	rbuf_freelist_head = rp;
	rbuf_freebufs++;

	mutex_exit(&dnet_rbuf_lock);
}

/*
 * Free the receive buffer used in a stream's message block allocated
 * thru desballoc().
 */
static void
dnet_freemsg_buf(struct free_ptr *frp)
{
	dnet_rbuf_free((caddr_t)frp->buf); /* buffer goes back to the pool */
	kmem_free(frp, sizeof (*frp)); /* free up the free_rtn structure */
}

/*
 *	========== SROM Read Routines ==========
 */

/*
 * The following code gets the SROM information, either by reading it
 * from the device or, failing that, by reading a property.
 */
static int
dnet_read_srom(dev_info_t *devinfo, int board_type, ddi_acc_handle_t io_handle,
    int io_reg, uchar_t *vi, int maxlen)
{
	int all_ones, zerocheck, i;

	/*
	 * Load SROM into vendor_info
	 */
	if (board_type == DEVICE_ID_21040)
		dnet_read21040addr(devinfo, io_handle, io_reg, vi, &maxlen);
	else
		/* 21041/21140 serial rom */
		dnet_read21140srom(io_handle, io_reg, vi, maxlen);
	/*
	 * If the dumpsrom property is present in the conf file, print
	 * the contents of the SROM to the console
	 */
	if (ddi_getprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
				"dumpsrom", 0))
		dnet_dumpbin("SROM", vi, 1, maxlen);

	for (zerocheck = i = 0, all_ones = 0xff; i < maxlen; i++) {
		zerocheck |= vi[i];
		all_ones &= vi[i];
	}
	if (zerocheck == 0 || all_ones == 0xff) {
		return (get_alternative_srom_image(devinfo, vi, maxlen));
	} else {
#ifdef BUG_4010796
		set_alternative_srom_image(devinfo, vi, maxlen);
#endif
		return (0);	/* Primary */
	}
}

/*
 * The function reads the ethernet address of the 21040 adapter
 */
static void
dnet_read21040addr(dev_info_t *dip, ddi_acc_handle_t io_handle, int io_reg,
    uchar_t *addr, int *len)
{
	uint32_t	val;
	int		i;

	/* No point reading more than the ethernet address */
	*len = ddi_getprop(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, macoffset_propname, 0) + ETHERADDRL;

	/* Reset ROM pointer */
	ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG), 0);
	for (i = 0; i < *len; i++) {
		do {
			val = ddi_get32(io_handle,
				REG32(io_reg, ETHER_ROM_REG));
		} while (val & 0x80000000);
		addr[i] = val & 0xFF;
	}
}

#define	drv_nsecwait(x)	drv_usecwait(((x)+999)/1000) /* XXX */

/*
 * The function reads the SROM	of the 21140 adapter
 */
static void
dnet_read21140srom(ddi_acc_handle_t io_handle, int io_reg, uchar_t *addr,
    int maxlen)
{
	uint32_t 	i, j;
	uint32_t	dout;
	uint16_t	word;
	uint8_t		rom_addr;
	uint8_t		bit;


	rom_addr = 0;
	for (i = 0; i <	maxlen; i += 2) {
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM);
		drv_nsecwait(30);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP);
		drv_nsecwait(50);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | SEL_CLK);
		drv_nsecwait(250);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP);
		drv_nsecwait(100);

		/* command */
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | DATA_IN);
		drv_nsecwait(150);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | DATA_IN | SEL_CLK);
		drv_nsecwait(250);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | DATA_IN);
		drv_nsecwait(250);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | DATA_IN | SEL_CLK);
		drv_nsecwait(250);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | DATA_IN);
		drv_nsecwait(100);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP);
		drv_nsecwait(150);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | SEL_CLK);
		drv_nsecwait(250);
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP);
		drv_nsecwait(100);

		/* Address */
		for (j = HIGH_ADDRESS_BIT; j >= 1; j >>= 1) {
			bit = (rom_addr & j) ? DATA_IN : 0;
			ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | bit);
			drv_nsecwait(150);
			ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | bit | SEL_CLK);
			drv_nsecwait(250);
			ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM | SEL_CHIP | bit);
			drv_nsecwait(100);
		}
		drv_nsecwait(150);

		/* Data */
		word = 0;
		for (j = 0x8000; j >= 1; j >>= 1) {
			ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
				    READ_OP | SEL_ROM | SEL_CHIP | SEL_CLK);
			drv_nsecwait(100);
			dout = ddi_get32(io_handle,
					    REG32(io_reg, ETHER_ROM_REG));
			drv_nsecwait(150);
			if (dout & DATA_OUT)
				word |= j;
			ddi_put32(io_handle,
				    REG32(io_reg, ETHER_ROM_REG),
				    READ_OP | SEL_ROM | SEL_CHIP);
			drv_nsecwait(250);
		}
		addr[i] = (word & 0x0000FF);
		addr[i + 1] = (word >> 8);
		rom_addr++;
		ddi_put32(io_handle, REG32(io_reg, ETHER_ROM_REG),
			    READ_OP | SEL_ROM);
		drv_nsecwait(100);
	}
}


/*
 * XXX NEEDSWORK
 *
 * Some lame multiport cards have only one SROM, which can be accessed
 * only from the "first" 21x4x chip, whichever that one is.  If we can't
 * get at our SROM, we look for its contents in a property instead, which
 * we rely on the bootstrap to have properly set.
 * #ifdef BUG_4010796
 * We also have a hack to try to set it ourselves, when the "first" port
 * attaches, if it has not already been properly set.  However, this method
 * is not reliable, since it makes the unwarrented assumption that the
 * "first" port will attach first.
 * #endif
 */

static int
get_alternative_srom_image(dev_info_t *devinfo, uchar_t *vi, int len)
{
	int	l = len;

	if (ddi_getlongprop_buf(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
	    "DNET_SROM", (caddr_t)vi, &len) != DDI_PROP_SUCCESS &&
	    (len = l) && ddi_getlongprop_buf(DDI_DEV_T_ANY,
	    ddi_get_parent(devinfo), DDI_PROP_DONTPASS, "DNET_SROM",
	    (caddr_t)vi, &len) != DDI_PROP_SUCCESS)
		return (-1);	/* Can't find it! */

	/*
	 * The return value from this routine specifies which port number
	 * we are.  The primary port is denoted port 0.  On a QUAD card we
	 * should return 1, 2, and 3 from this routine.  The return value
	 * is used to modify the ethernet address from the SROM data.
	 */

#ifdef BUG_4010796
	{
	/*
	 * For the present, we remember the device number of our primary
	 * sibling and hope we and our other siblings are consecutively
	 * numbered up from there.  In the future perhaps the bootstrap
	 * will pass us the necessary information telling us which physical
	 * port we really are.
	 */
	pci_regspec_t	*assignp;
	int		assign_len;
	int 		devnum;
	int		primary_devnum;

	primary_devnum = ddi_getprop(DDI_DEV_T_ANY, devinfo, 0,
	    "DNET_DEVNUM", -1);
	if (primary_devnum == -1)
		return (1);	/* XXX NEEDSWORK -- We have no better idea */

	if ((ddi_getlongprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
	    "assigned-addresses", (caddr_t)&assignp,
	    &assign_len)) != DDI_PROP_SUCCESS)
		return (1);	/* XXX NEEDSWORK -- We have no better idea */

	devnum = PCI_REG_DEV_G(assignp->pci_phys_hi);
	kmem_free(assignp, assign_len);
	return (devnum - primary_devnum);
	}
#else
	return (1);	/* XXX NEEDSWORK -- We have no better idea */
#endif
}


#ifdef BUG_4010796
static void
set_alternative_srom_image(dev_info_t *devinfo, uchar_t *vi, int len)
{
	int 		proplen;
	pci_regspec_t	*assignp;
	int		assign_len;
	int 		devnum;

	if (ddi_getproplen(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
	    "DNET_SROM", &proplen) == DDI_PROP_SUCCESS ||
	    ddi_getproplen(DDI_DEV_T_ANY, ddi_get_parent(devinfo),
	    DDI_PROP_DONTPASS, "DNET_SROM", &proplen) == DDI_PROP_SUCCESS)
		return;		/* Already done! */

	/* function return value ignored */
	(void) ddi_prop_update_byte_array(DDI_DEV_T_NONE,
	    ddi_get_parent(devinfo), "DNET_SROM", (uchar_t *)vi, len);
	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devinfo,
	    "DNET_HACK", "hack");

	if ((ddi_getlongprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
	    "assigned-addresses", (caddr_t)&assignp,
	    &assign_len)) == DDI_PROP_SUCCESS) {
		devnum = PCI_REG_DEV_G(assignp->pci_phys_hi);
		kmem_free(assignp, assign_len);
		/* function return value ignored */
		(void) ddi_prop_update_int(DDI_DEV_T_NONE,
		    ddi_get_parent(devinfo), "DNET_DEVNUM", devnum);
	}
}
#endif

/*
 *	========== SROM Parsing Routines ==========
 */

static int
check_srom_valid(uchar_t *vi)
{
	int		word, bit;
	uint8_t		crc;
	uint16_t	*wvi;		/* word16 pointer to vendor info */
	uint16_t	bitval;

	/* verify that the number of controllers on the card is within range */
	if (vi[SROM_ADAPTER_CNT] < 1 || vi[SROM_ADAPTER_CNT] > MAX_ADAPTERS)
		return (0);

	/*
	 * version 1 and 3 of this card did not check the id block CRC value
	 * and this can't be changed without retesting every supported card
	 *
	 * however version 4 of the SROM can have this test applied
	 * without fear of breaking something that used to work.
	 * the CRC algorithm is taken from the Intel document
	 *	"21x4 Serial ROM Format"
	 *	version 4.09
	 *	3-Mar-1999
	 */

	switch (vi[SROM_VERSION]) {
	case 1:
	    /* fallthru */
	case 3:
	    return (
		vi[SROM_MBZ] == 0 &&	/* must be zero */
		vi[SROM_MBZ2] == 0 &&	/* must be zero */
		vi[SROM_MBZ3] == 0);	/* must be zero */

	case 4:
	    wvi = (uint16_t *)vi;
	    crc = 0xff;
	    for (word = 0; word < 9; word++)
		for (bit = 15; bit >= 0; bit--) {
		    if (word == 8 && bit == 7)
			return (crc == vi[16]);
		    bitval = ((wvi[word] >> bit) & 1) ^ ((crc >> 7) & 1);
		    crc <<= 1;
		    if (bitval == 1) {
			crc ^= 7;
		    }
		}

	default:
		return (0);
	}
}

/*
 *	========== Active Media Determination Routines ==========
 */

/* This routine is also called for V3 Compact and extended type 0 SROMs */
static int
is_fdmedia(int media)
{
	if (media == MEDIA_TP_FD || media == MEDIA_SYM_SCR_FD)
		return (1);
	else
		return (0);
}

/*
 * "Linkset" is used to merge media that use the same link test check. So,
 * if the TP link is added to the linkset, so is the TP Full duplex link.
 * Used to avoid checking the same link status twice.
 */
static void
linkset_add(uint32_t *set, int media)
{
	if (media == MEDIA_TP_FD || media == MEDIA_TP)
		*set |= (1UL<<MEDIA_TP_FD) | (1UL<<MEDIA_TP);
	else if (media == MEDIA_SYM_SCR_FD || media == MEDIA_SYM_SCR)
		*set |= (1UL<<MEDIA_SYM_SCR_FD) | (1UL<<MEDIA_SYM_SCR);
	else *set |= 1UL<<media;
}
static int
linkset_isset(uint32_t linkset, int media)
{
	return (((1UL<<media)  & linkset) ? 1:0);
}

/*
 * The following code detects which Media is connected for 21041/21140
 * Expect to change this code to support new 21140 variants.
 * find_active_media() - called with intrlock held.
 */
static void
find_active_media(gld_mac_info_t *macinfo)
{
	int i;
	media_block_t *block;
	media_block_t *best_allowed = NULL;
	media_block_t *hd_found = NULL;
	media_block_t *fd_found = NULL;
	struct dnetinstance	*dnetp = (struct dnetinstance *)
					(macinfo->gldm_private);
	LEAF_FORMAT *leaf = &dnetp->sr.leaf[dnetp->leaf];
	uint32_t checked = 0, links_up = 0;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
#ifdef SROMDEBUG
	cmn_err(CE_NOTE, "find_active_media 0x%x,0x%x", sr->version, macinfo);
#endif
	dnetp->selected_media_block = leaf->default_block;

	if (dnetp->phyaddr != -1) {
		dnetp->selected_media_block = leaf->mii_block;
		setup_block(macinfo);

		if (ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
		    DDI_PROP_DONTPASS, "portmon", 1)) {
			/* XXX return value ignored */
			(void) mii_start_portmon(dnetp->mii, dnet_mii_link_cb,
			    &dnetp->intrlock);
			/*
			 * If the port monitor detects the link is already
			 * up, there is no point going through the rest of the
			 * link sense
			 */
			if (dnetp->mii_up) {
				return;
			}
		}
	}

	/*
	 * Media is searched for in order of Precedence. This DEC SROM spec
	 * tells us that the first media entry in the SROM is the lowest
	 * precedence and should be checked last. This is why we go to the last
	 * Media block and work back to the beginning.
	 *
	 * However, some older SROMs (Cogent EM110's etc.) have this the wrong
	 * way around. As a result, following the SROM spec would result in a
	 * 10 link being chosen over a 100 link if both media are available.
	 * So we continue trying the media until we have at least tried the
	 * DEFAULT media.
	 */

	/* Search for an active medium, and select it */
	for (block = leaf->block + leaf->block_count  - 1;
	    block >= leaf->block; block--) {
		int media = block->media_code;

		/* User settings disallow selection of this block */
		if (dnetp->disallowed_media & (1UL<<media))
			continue;

		/* We may not be able to pick the default */
		if (best_allowed == NULL || block == leaf->default_block)
			best_allowed = block;
#ifdef DEBUG
		if (dnetdebug & DNETSENSE)
		    cmn_err(CE_NOTE, "Testing %s medium (block type %d)",
			media_str[media], block->type);
#endif

		dnetp->selected_media_block = block;
		switch (block->type) {

		case 2: /* SIA Media block: Best we can do is send a packet */
			setup_block(macinfo);
			if (send_test_packet(macinfo)) {
				if (!is_fdmedia(media))
					return;
				if (!fd_found)
					fd_found = block;
			}
			break;

		/* SYM/SCR or TP block: Use the link-sense bits */
		case 0:
			if (!linkset_isset(checked, media)) {
				linkset_add(&checked, media);
				if (((media == MEDIA_BNC ||
				    media == MEDIA_AUI) &&
				    send_test_packet(macinfo)) ||
				    dnet_link_sense(macinfo))
					linkset_add(&links_up, media);
			}

			if (linkset_isset(links_up, media)) {
				/*
				 * Half Duplex is *always* the favoured media.
				 * Full Duplex can be set and forced via the
				 * conf file.
				 */
				if (!is_fdmedia(media) &&
				    dnetp->selected_media_block ==
				    leaf->default_block) {
					/*
					 * Cogent cards have the media in
					 * opposite order to the spec.,
					 * this code forces the media test to
					 * keep going until the default media
					 * is tested.
					 *
					 * In Cogent case, 10, 10FD, 100FD, 100
					 * 100 is the default but 10 could have
					 * been detected and would have been
					 * chosen but now we force it through to
					 * 100.
					 */
					setup_block(macinfo);
					return;
				} else if (!is_fdmedia(media)) {
					/*
					 * This allows all the others to work
					 * properly by remembering the media
					 * that works and not defaulting to
					 * a FD link.
					 */
						if (hd_found == NULL)
							hd_found = block;
				} else if (fd_found == NULL) {
					/*
					 * No media have already been found
					 * so far, this is FD, it works so
					 * remember it and if no others are
					 * detected, use it.
					 */
					fd_found = block;
				}
			}
			break;

		/*
		 * MII block: May take up to a second or so to settle if
		 * setup causes a PHY reset
		 */
		case 1: case 3:
			setup_block(macinfo);
			for (i = 0; ; i++) {
				if (mii_linkup(dnetp->mii, dnetp->phyaddr)) {
					/* XXX function return value ignored */
					(void) mii_getspeed(dnetp->mii,
						dnetp->phyaddr,
						&dnetp->mii_speed,
						&dnetp->mii_duplex);
					dnetp->mii_up = 1;
					leaf->mii_block = block;
					return;
				}
				if (i == 10)
					break;
				delay(drv_usectohz(150000));
			}
			dnetp->mii_up = 0;
			break;
		}
	} /* for loop */
	if (hd_found) {
		dnetp->selected_media_block = hd_found;
	} else if (fd_found) {
		dnetp->selected_media_block = fd_found;
	} else {
		if (best_allowed == NULL)
			best_allowed = leaf->default_block;
		dnetp->selected_media_block = best_allowed;
		cmn_err(CE_WARN, "!dnet: Default media selected\n");
	}
	setup_block(macinfo);
}

/*
 * Do anything neccessary to select the selected_media_block.
 * setup_block() - called with intrlock held.
 */
static void
setup_block(gld_mac_info_t *macinfo)
{
	dnet_reset_board(macinfo);
	dnet_init_board(macinfo);
	/* XXX function return value ignored */
	(void) dnet_start(macinfo);
}

/* dnet_link_sense() - called with intrlock held */
static int
dnet_link_sense(gld_mac_info_t *macinfo)
{
	/*
	 * This routine makes use of the command word from the srom config.
	 * Details of the auto-sensing information contained in this can
	 * be found in the "Digital Semiconductor 21X4 Serial ROM Format v3.03"
	 * spec. Section 4.3.2.1, and 4.5.2.1.3
	 */
	struct dnetinstance *dnetp = (struct dnetinstance *)
					(macinfo->gldm_private);
	media_block_t *block = dnetp->selected_media_block;
	uint32_t link, status, mask, polarity;
	int settletime, stabletime, waittime, upsamples;
	int delay_100, delay_10;


	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	/* Don't autosense if the medium does not support it */
	if (block->command & (1 << 15)) {
		/* This should be the default block */
		if (block->command & (1UL<<14))
			dnetp->sr.leaf[dnetp->leaf].default_block = block;
		return (0);
	}

	delay_100 = ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
	    DDI_PROP_DONTPASS, "autosense-delay-100", 2000);

	delay_10 = ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
	    DDI_PROP_DONTPASS, "autosense-delay-10", 400);

	/*
	 * Scrambler may need to be disabled for link sensing
	 * to work
	 */
	dnetp->disable_scrambler = 1;
	setup_block(macinfo);
	dnetp->disable_scrambler = 0;

	if (block->media_code == MEDIA_TP || block->media_code == MEDIA_TP_FD)
		settletime = delay_10;
	else
		settletime = delay_100;
	stabletime = settletime / 4;

	mask = 1 << ((block->command & CMD_MEDIABIT_MASK) >> 1);
	polarity = block->command & CMD_POL ? 0xffffffff : 0;

	for (waittime = 0, upsamples = 0;
	    waittime <= settletime + stabletime && upsamples < 8;
	    waittime += stabletime/8) {
		delay(drv_usectohz(stabletime*1000 / 8));
		status = read_gpr(dnetp);
		link = (status^polarity) & mask;
		if (link)
			upsamples++;
		else
			upsamples = 0;
	}
#ifdef DNETDEBUG
	if (dnetdebug & DNETSENSE)
		cmn_err(CE_NOTE, "%s upsamples:%d stat:%x polarity:%x "
		    "mask:%x link:%x",
		    upsamples == 8 ? "UP":"DOWN",
		    upsamples, status, polarity, mask, link);
#endif
	if (upsamples == 8)
		return (1);
	return (0);
}

static int
send_test_packet(gld_mac_info_t *macinfo)
{
	int packet_delay;
	struct dnetinstance	*dnetp = (struct dnetinstance *)
					(macinfo->gldm_private);
	struct tx_desc_type *desc;
	int bufindex;
	int media_code = dnetp->selected_media_block->media_code;
	uint32_t del;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	/*
	 * For a successful test packet, the card must have settled into
	 * its current setting.  Almost all cards we've tested manage to
	 * do this with all media within 50ms.  However, the SMC 8432
	 * requires 300ms to settle into BNC mode.  We now only do this
	 * from attach, and we do sleeping delay() instead of drv_usecwait()
	 * so we hope this .2 second delay won't cause too much suffering.
	 * ALSO: with an autonegotiating hub, an aditional 1 second delay is
	 * required. This is done if the media type is TP
	 */
	if (media_code == MEDIA_TP || media_code == MEDIA_TP_FD) {
		packet_delay = ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
		    DDI_PROP_DONTPASS, "test_packet_delay_tp", 1300000);
	} else {
		packet_delay = ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
		    DDI_PROP_DONTPASS, "test_packet_delay", 300000);
	}
	delay(drv_usectohz(packet_delay));

	desc = dnetp->tx_desc;

	bufindex = dnetp->tx_current_desc;
	if (alloc_descriptor(macinfo) == FAILURE) {
		cmn_err(CE_WARN, "DNET: send_test_packet: alloc_descriptor"
		    "failed");
		return (0);
	}

	/*
	 * use setup buffer as the buffer for the test packet
	 * instead of allocating one.
	 */

	ASSERT(dnetp->setup_buf_vaddr != NULL);
	/* Put something decent in dest address so we don't annoy other cards */
	BCOPY((caddr_t)dnetp->curr_macaddr,
		(caddr_t)dnetp->setup_buf_vaddr, ETHERADDRL);
	BCOPY((caddr_t)dnetp->curr_macaddr,
		(caddr_t)dnetp->setup_buf_vaddr+ETHERADDRL, ETHERADDRL);

	desc[bufindex].buffer1 = (uint32_t)(dnetp->setup_buf_paddr);
	desc[bufindex].desc1.buffer_size1 = SETUPBUF_SIZE;
	desc[bufindex].buffer2 = (uint32_t)(0);
	desc[bufindex].desc1.first_desc = 1;
	desc[bufindex].desc1.last_desc = 1;
	desc[bufindex].desc1.int_on_comp = 1;
	desc[bufindex].desc0.own = 1;

	ddi_put8(dnetp->io_handle, REG8(dnetp->io_reg, TX_POLL_REG),
		    TX_POLL_DEMAND);

	/*
	 * Give enough time for the chip to transmit the packet
	 */
#if 1
	del = 1000;
	while (desc[bufindex].desc0.own && --del)
		drv_usecwait(10);	/* quickly wait up to 10ms */
	if (desc[bufindex].desc0.own)
		delay(drv_usectohz(200000));	/* nicely wait a longer time */
#else
	del = 0x10000;
	while (desc[bufindex].desc0.own && --del)
		drv_usecwait(10);
#endif

#ifdef DNETDEBUG
	if (dnetdebug & DNETSENSE)
		cmn_err(CE_NOTE, "desc0 bits = %u, %u, %u, %u, %u, %u",
			desc[bufindex].desc0.own,
			desc[bufindex].desc0.err_summary,
			desc[bufindex].desc0.carrier_loss,
			desc[bufindex].desc0.no_carrier,
			desc[bufindex].desc0.late_collision,
			desc[bufindex].desc0.link_fail);
#endif
	if (desc[bufindex].desc0.own) /* it shouldn't take this long, error */
	    return (0);

	return (!desc[bufindex].desc0.err_summary);
}

/* enable_interrupts - called with intrlock held */
static void
enable_interrupts(struct dnetinstance *dnetp, int enable_xmit)
{
	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	/* Don't enable interrupts if they have been forced off */
	if (dnetp->interrupts_disabled)
		return;
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, INT_MASK_REG),
	    NORMAL_INTR_MASK | ABNORMAL_INTR_MASK | TX_UNDERFLOW_MASK |
	    (enable_xmit ? TX_INTERRUPT_MASK : 0) |
	    (dnetp->timer.cb ? GPTIMER_INTR : 0) |
	    RX_INTERRUPT_MASK | SYSTEM_ERROR_MASK | TX_JABBER_MASK);

}

/*
 * Some older multiport cards are non-PCI compliant in their interrupt routing.
 * Second and subsequent devices are incorrectly configured by the BIOS
 * (either in their ILINE configuration or the MP Configuration Table for PC+MP
 * systems).
 * The hack stops gldregister() registering the interrupt routine for the
 * FIRST device on the adapter, and registers its own. It builds up a table
 * of macinfo structures for each device, and the new interrupt routine
 * calls gldintr for each of them.
 * Known cards that suffer from this problem are:
 *	All Cogent multiport cards;
 * 	Znyx 314;
 *	Znyx 315.
 *
 * XXX NEEDSWORK -- see comments above get_alternative_srom_image(). This
 * hack relies on the fact that the offending cards will have only one SROM.
 * It uses this fact to identify devices that are on the same multiport
 * adapter, as opposed to multiple devices from the same vendor (as
 * indicated by "secondary")
 */
static int
dnet_hack_interrupts(gld_mac_info_t *macinfo, int secondary)
{
	int i;
	struct hackintr_inf *hackintr_inf;
	struct dnetinstance *dnetp =
		(struct dnetinstance *)macinfo->gldm_private;
	dev_info_t *devinfo = dnetp->devinfo;
	uint32_t oui = 0;	/* Organizationally Unique ID */

	if (ddi_getprop(DDI_DEV_T_ANY, devinfo, DDI_PROP_DONTPASS,
	    "no_INTA_workaround", 0) != 0)
		return (0);

	for (i = 0; i < 3; i++)
		oui = (oui << 8) | dnetp->vendor_addr[i];

	/* Check wheather or not we need to implement the hack */

	switch (oui) {
	case ZNYX_ETHER:
		/* Znyx multiport 21040 cards <<==>> ZX314 or ZX315 */
		if (dnetp->board_type != DEVICE_ID_21040)
			return (0);
		break;

	case COGENT_ETHER:
		/* All known Cogent multiport cards */
		break;

	case ADAPTEC_ETHER:
		/* Adaptec multiport cards */
		break;

	default:
		/* Other cards work correctly */
		return (0);
	}

	/* card is (probably) non-PCI compliant in its interrupt routing */


	if (!secondary) {

		/*
		 * If we have already registered a hacked interrupt, and
		 * this is also a 'primary' adapter, then this is NOT part of
		 * a multiport card, but a second card on the same PCI bus.
		 * BUGID: 4057747
		 */
		if (ddi_getprop(DDI_DEV_T_ANY, ddi_get_parent(devinfo),
		    DDI_PROP_DONTPASS, hackintr_propname, 0) != 0)
			return (0);
				/* ... Primary not part of a multiport device */

#ifdef DNETDEBUG
		if (dnetdebug & DNETTRACE)
			cmn_err(CE_NOTE, "dnet: Implementing hardware "
				"interrupt flaw workaround");
#endif
		dnetp->hackintr_inf = hackintr_inf =
		    kmem_zalloc(sizeof (struct hackintr_inf), KM_SLEEP);
		if (hackintr_inf == NULL)
			goto fail;

		hackintr_inf->macinfos[0] = macinfo;
		hackintr_inf->devinfo = devinfo;

		/*
		 * Add a property to allow successive attaches to find the
		 * table
		 */

		if (ddi_prop_update_byte_array(DDI_DEV_T_NONE,
		    ddi_get_parent(devinfo), hackintr_propname,
		    (uchar_t *)&dnetp->hackintr_inf,
		    sizeof (void *)) != DDI_PROP_SUCCESS)
			goto fail;


		/* Register our hacked interrupt routine */
		if (ddi_add_intr(devinfo, 0, &macinfo->gldm_cookie, NULL,
		    (uint_t (*)(char *))dnet_hack_intr,
		    (caddr_t)hackintr_inf) != DDI_SUCCESS) {
			/* XXX function return value ignored */
			(void) ddi_prop_remove(DDI_DEV_T_NONE,
				ddi_get_parent(devinfo),
				hackintr_propname);
			goto fail;
		}

		/*
		 * Mutex required to ensure interrupt routine has completed
		 * when detaching devices
		 */
		mutex_init(&hackintr_inf->lock, NULL, MUTEX_DRIVER,
		    macinfo->gldm_cookie);

		/* Stop GLD registering an interrupt */
		return (-1);
	} else {

		/* Add the macinfo for this secondary device to the table */

		hackintr_inf = (struct hackintr_inf *)(uintptr_t)
		    ddi_getprop(DDI_DEV_T_ANY, ddi_get_parent(devinfo),
			DDI_PROP_DONTPASS, hackintr_propname, 0);

		if (hackintr_inf == NULL)
			goto fail;

		/* Find an empty slot */
		for (i = 0; i < MAX_INST; i++)
			if (hackintr_inf->macinfos[i] == NULL)
				break;

		/* More than 8 ports on adapter ?! */
		if (i == MAX_INST)
			goto fail;

		hackintr_inf->macinfos[i] = macinfo;

		/*
		 * Allow GLD to register a handler for this
		 * device. If the card is actually broken, as we suspect, this
		 * handler will never get called. However, by registering the
		 * interrupt handler, we can copy gracefully with new multiport
		 * Cogent cards that decide to fix the hardware problem
		 */
		return (0);
	}

fail:
	cmn_err(CE_WARN, "dnet: Could not work around hardware interrupt"
	    " routing problem");
	return (0);
}

/*
 * Call gld_intr for all adapters on a multiport card
 */

static uint_t
dnet_hack_intr(struct hackintr_inf *hackintr_inf)
{
	int i;
	int claimed = DDI_INTR_UNCLAIMED;

	/* Stop detaches while processing interrupts */
	mutex_enter(&hackintr_inf->lock);

	for (i = 0; i < MAX_INST; i++) {
		if (hackintr_inf->macinfos[i] &&
		    gld_intr(hackintr_inf->macinfos[i]) == DDI_INTR_CLAIMED)
			claimed = DDI_INTR_CLAIMED;
	}
	mutex_exit(&hackintr_inf->lock);
	return (claimed);
}

/*
 * This removes the detaching device from the table procesed by the hacked
 * interrupt routine. Because the interrupts from all devices come in to the
 * same interrupt handler, ALL devices must stop interrupting once the
 * primary device detaches. This isn't a problem at present, because all
 * instances of a device are detached when the driver is unloaded.
 */
static int
dnet_detach_hacked_interrupt(dev_info_t *devinfo)
{
	int i;
	struct hackintr_inf *hackintr_inf;
	gld_mac_info_t *mac, *macinfo = ddi_get_driver_private(devinfo);

	hackintr_inf = (struct hackintr_inf *)(uintptr_t)
	    ddi_getprop(DDI_DEV_T_ANY, ddi_get_parent(devinfo),
	    DDI_PROP_DONTPASS, hackintr_propname, 0);

	/*
	 * No hackintr_inf implies hack was not required or the primary has
	 * detached, and our interrupts are already disabled
	 */
	if (!hackintr_inf) {
		/* remove the interrupt for the non-hacked case */
		ddi_remove_intr(devinfo, 0, macinfo->gldm_cookie);
		return (DDI_SUCCESS);
	}

	/* Remove this device from the handled table */
	mutex_enter(&hackintr_inf->lock);
	for (i = 0; i < MAX_INST; i++) {
		if (hackintr_inf->macinfos[i] == macinfo) {
			hackintr_inf->macinfos[i] = NULL;
			break;
		}
	}

	mutex_exit(&hackintr_inf->lock);

	/* Not the primary card, we are done */
	if (devinfo != hackintr_inf->devinfo)
		return (DDI_SUCCESS);

	/*
	 * This is the primary card. All remaining adapters on this device
	 * must have their interrupts disabled before we remove the handler
	 */
	for (i = 0; i < MAX_INST; i++) {
		if ((mac = hackintr_inf->macinfos[i]) != NULL) {
			struct dnetinstance *altdnetp =
				(struct dnetinstance *)mac->gldm_private;
			altdnetp->interrupts_disabled = 1;
			ddi_put32(altdnetp->io_handle,
				REG32(altdnetp->io_reg, INT_MASK_REG), 0);
		}
	}

	/* It should now be safe to remove the interrupt handler */

	ddi_remove_intr(devinfo, 0, macinfo->gldm_cookie);
	mutex_destroy(&hackintr_inf->lock);
	/* XXX function return value ignored */
	(void) ddi_prop_remove(DDI_DEV_T_NONE, ddi_get_parent(devinfo),
	    hackintr_propname);
	kmem_free(hackintr_inf, sizeof (struct hackintr_inf));
	return (DDI_SUCCESS);
}

/*
 *	========== PHY MII Routines ==========
 */

/* do_phy() - called with intrlock held */
static void
do_phy(gld_mac_info_t *macinfo)
{
	dev_info_t *dip;
	struct dnetinstance
	    *dnetp = (struct dnetinstance *)macinfo->gldm_private;
	LEAF_FORMAT *leaf = dnetp->sr.leaf + dnetp->leaf;
	media_block_t *block;
	int phy;

	dip = dnetp->devinfo;

	/*
	 * Find and configure the PHY media block. If NO PHY blocks are
	 * found on the SROM, but a PHY device is present, we assume the card
	 * is a legacy device, and that there is ONLY a PHY interface on the
	 * card (ie, no BNC or AUI, and 10BaseT is implemented by the PHY
	 */

	for (block = leaf->block + leaf->block_count -1;
	    block >= leaf->block; block --) {
		if (block->type == 3 || block->type == 1) {
			leaf->mii_block = block;
			break;
		}
	}

	/*
	 * If no MII block, select default, and hope this configuration will
	 * allow the phy to be read/written if it is present
	 */
	dnetp->selected_media_block = leaf->mii_block ?
		leaf->mii_block : leaf->default_block;

	setup_block(macinfo);
	/* XXX function return value ignored */
	(void) mii_create(dip, dnet_mii_write, dnet_mii_read, &dnetp->mii);

	/*
	 * We try PHY 0 LAST because it is less likely to be connected
	 */
	for (phy = 1; phy < 33; phy++)
		if (mii_probe_phy(dnetp->mii, phy % 32) == MII_SUCCESS &&
			mii_init_phy(dnetp->mii, phy % 32) == MII_SUCCESS) {
#ifdef DNETDEBUG
			if (dnetdebug & DNETSENSE)
				cmn_err(CE_NOTE, "dnet: "
				"PHY at address %d", phy % 32);
#endif
			dnetp->phyaddr = phy % 32;
			if (!leaf->mii_block) {
				/* Legacy card, change the leaf node */
				set_leaf(&dnetp->sr, &leaf_phylegacy);
			}
			return;
		}
#ifdef DNETDEBUG
	if (dnetdebug & DNETSENSE)
		cmn_err(CE_NOTE, "dnet: No PHY found");
#endif
}

static ushort_t
dnet_mii_read(dev_info_t *dip, int phy_addr, int reg_num)
{
	gld_mac_info_t *macinfo;
	struct dnetinstance *dnetp;

	uint32_t command_word;
	uint32_t tmp;
	uint32_t data = 0;
	int i;
	int bits_in_ushort = ((sizeof (ushort_t))*8);
	int turned_around = 0;

	macinfo = ddi_get_driver_private(dip);
	dnetp = (struct dnetinstance *)macinfo->gldm_private;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	/* Write Preamble */
	write_mii(dnetp, MII_PRE, 2*bits_in_ushort);

	/* Prepare command word */
	command_word = (uint32_t)phy_addr << MII_PHY_ADDR_ALIGN;
	command_word |= (uint32_t)reg_num << MII_REG_ADDR_ALIGN;
	command_word |= MII_READ_FRAME;

	write_mii(dnetp, command_word, bits_in_ushort-2);

	mii_tristate(dnetp);

	/* Check that the PHY generated a zero bit the 2nd clock */
	tmp = ddi_get32(dnetp->io_handle,
		REG32(dnetp->io_reg, ETHER_ROM_REG));

	turned_around = (tmp & MII_DATA_IN) ? 0 : 1;

	/* read data WORD */
	for (i = 0; i < bits_in_ushort; i++) {
		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, ETHER_ROM_REG), MII_READ);
		drv_usecwait(MII_DELAY);
		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, ETHER_ROM_REG), MII_READ | MII_CLOCK);
		drv_usecwait(MII_DELAY);
		tmp = ddi_get32(dnetp->io_handle,
		    REG32(dnetp->io_reg, ETHER_ROM_REG));
		drv_usecwait(MII_DELAY);
		data = (data << 1) | (tmp >> MII_DATA_IN_POSITION) & 0x0001;
	}

	mii_tristate(dnetp);
	return (turned_around ? data: -1);
}

static void
dnet_mii_write(dev_info_t *dip, int phy_addr, int reg_num, int reg_dat)
{
	gld_mac_info_t *macinfo;
	struct dnetinstance *dnetp;
	uint32_t command_word;
	int bits_in_ushort = ((sizeof (ushort_t))*8);

	macinfo = ddi_get_driver_private(dip);
	dnetp = (struct dnetinstance *)macinfo->gldm_private;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	write_mii(dnetp, MII_PRE, 2*bits_in_ushort);

	/* Prepare command word */
	command_word = ((uint32_t)phy_addr << MII_PHY_ADDR_ALIGN);
	command_word |= ((uint32_t)reg_num << MII_REG_ADDR_ALIGN);
	command_word |= (MII_WRITE_FRAME | (uint32_t)reg_dat);

	write_mii(dnetp, command_word, 2*bits_in_ushort);
	mii_tristate(dnetp);
}

/*
 * Write data size bits from mii_data to the MII control lines.
 */
static void
write_mii(struct dnetinstance *dnetp, uint32_t mii_data, int data_size)
{
	int i;
	uint32_t dbit;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	for (i = data_size; i > 0; i--) {
		dbit = ((mii_data >>
		    (31 - MII_WRITE_DATA_POSITION)) & MII_WRITE_DATA);
		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, ETHER_ROM_REG),
		    MII_WRITE | dbit);
		drv_usecwait(MII_DELAY);
		ddi_put32(dnetp->io_handle,
		    REG32(dnetp->io_reg, ETHER_ROM_REG),
		    MII_WRITE | MII_CLOCK | dbit);
		drv_usecwait(MII_DELAY);
		mii_data <<= 1;
	}
}

/*
 * Put the MDIO port in tri-state for the turn around bits
 * in MII read and at end of MII management sequence.
 */
static void
mii_tristate(struct dnetinstance *dnetp)
{
	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, ETHER_ROM_REG),
	    MII_WRITE_TS);
	drv_usecwait(MII_DELAY);
	ddi_put32(dnetp->io_handle, REG32(dnetp->io_reg, ETHER_ROM_REG),
	    MII_WRITE_TS | MII_CLOCK);
	drv_usecwait(MII_DELAY);
}


static void
set_leaf(SROM_FORMAT *sr, LEAF_FORMAT *leaf)
{
	if (sr->leaf && !sr->leaf->is_static)
		kmem_free(sr->leaf, sr->adapters * sizeof (LEAF_FORMAT));
	sr->leaf = leaf;
}

/*
 * Callback from MII module. Makes sure that the CSR registers are
 * configured properly if the PHY changes mode.
 */
/* ARGSUSED */
/* dnet_mii_link_cb - called with intrlock held */
static void
dnet_mii_link_cb(dev_info_t *dip, int phy, enum mii_phy_state state)
{
	gld_mac_info_t *macinfo = ddi_get_driver_private(dip);
	struct dnetinstance *dnetp =
	    (struct dnetinstance *)macinfo->gldm_private;
	LEAF_FORMAT *leaf;

	ASSERT(MUTEX_HELD(&dnetp->intrlock));
	leaf = dnetp->sr.leaf + dnetp->leaf;
	if (state == phy_state_linkup) {
		dnetp->mii_up = 1;
		/* XXX function return value ignored */
		(void) mii_getspeed(dnetp->mii,
			dnetp->phyaddr, &dnetp->mii_speed,
			&dnetp->mii_duplex);
		dnetp->selected_media_block = leaf->mii_block;
		setup_block(macinfo);
	} else {
		/* NEEDSWORK: Probably can call find_active_media here */
		dnetp->mii_up = 0;
		if (leaf->default_block->media_code == MEDIA_MII)
			dnetp->selected_media_block = leaf->default_block;
		setup_block(macinfo);
	}
}

/*
 * SROM parsing routines.
 * Refer to the Digital 3.03 SROM spec while reading this! (references refer
 * to this document)
 * Where possible ALL vendor specific changes should be localised here. The
 * SROM data should be capable of describing any programmatic irregularities
 * of DNET cards (via SIA or GP registers, in particular), so vendor specific
 * code elsewhere should not be required
 */
static void
dnet_parse_srom(struct dnetinstance *dnetp, SROM_FORMAT *sr, uchar_t *vi)
{
	uint32_t ether_mfg = 0;
	int i;
	uchar_t *p;

	if (!ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
	    DDI_PROP_DONTPASS, "no_sromconfig", 0))
		dnetp->sr.init_from_srom = check_srom_valid(vi);

	if (dnetp->sr.init_from_srom && dnetp->board_type != DEVICE_ID_21040) {
		/* Section 2/3: General SROM Format/ ID Block */
		p = vi+18;
		sr->version = *p++;
		sr->adapters = *p++;

		sr->leaf =
		    kmem_zalloc(sr->adapters * sizeof (LEAF_FORMAT), KM_SLEEP);
		for (i = 0; i < 6; i++)
			sr->netaddr[i] = *p++;

		for (i = 0; i < sr->adapters; i++) {
			uchar_t devno = *p++;
			uint16_t offset = *p++;
			offset |= *p++ << 8;
			sr->leaf[i].device_number = devno;
			parse_controller_leaf(dnetp, sr->leaf+i, vi+offset);
		}
		/*
		 * 'Orrible hack for cogent cards. The 6911A board seems to
		 * have an incorrect SROM. (From the OEMDEMO program
		 * supplied by cogent, it seems that the ROM matches a setup
		 * or a board with a QSI or ICS PHY.
		 */
		for (i = 0; i < 3; i++)
			ether_mfg = (ether_mfg << 8) | sr->netaddr[i];

		if (ether_mfg == ADAPTEC_ETHER) {
			static uint16_t cogent_gprseq[] = {0x821, 0};
			switch (vi[COGENT_SROM_ID]) {
			case COGENT_ANA6911A_C:
			case COGENT_ANA6911AC_C:
#ifdef DNETDEBUG
				if (dnetdebug & DNETTRACE)
					cmn_err(CE_WARN,
					    "Suspected bad GPR sequence."
					    " Making a guess (821,0)");
#endif

				/* XXX function return value ignored */
				(void) ddi_prop_update_byte_array(
				    DDI_DEV_T_NONE, dnetp->devinfo,
				    "gpr-sequence", (uchar_t *)cogent_gprseq,
				    sizeof (cogent_gprseq));
				break;
			}
		}
	} else {
		/*
		 * Adhoc SROM, check for some cards which need special handling
		 * Assume vendor info contains ether address in first six bytes
		 */

		uchar_t *mac = vi + ddi_getprop(DDI_DEV_T_ANY, dnetp->devinfo,
		    DDI_PROP_DONTPASS, macoffset_propname, 0);

		for (i = 0; i < 6; i++)
			sr->netaddr[i] = mac[i];

		if (dnetp->board_type == DEVICE_ID_21140) {
			for (i = 0; i < 3; i++)
				ether_mfg = (ether_mfg << 8) | mac[i];

			switch (ether_mfg) {
			case ASANTE_ETHER:
				dnetp->vendor_21140 = ASANTE_TYPE;
				dnetp->vendor_revision = 0;
				set_leaf(sr, &leaf_asante);
				sr->adapters = 1;
				break;

			case COGENT_ETHER:
			case ADAPTEC_ETHER:
				dnetp->vendor_21140 = COGENT_EM_TYPE;
				dnetp->vendor_revision =
				    vi[VENDOR_REVISION_OFFSET];
				set_leaf(sr, &leaf_cogent_100);
				sr->adapters = 1;
				break;

			default:
				dnetp->vendor_21140 = DEFAULT_TYPE;
				dnetp->vendor_revision = 0;
				set_leaf(sr, &leaf_default_100);
				sr->adapters = 1;
				break;
			}
		} else if (dnetp->board_type == DEVICE_ID_21041) {
			set_leaf(sr, &leaf_21041);
		} else if (dnetp->board_type == DEVICE_ID_21040) {
			set_leaf(sr, &leaf_21040);
		}
	}
}

/* Section 4.2, 4.3, 4.4, 4.5 */
static void
parse_controller_leaf(struct dnetinstance *dnetp, LEAF_FORMAT *leaf,
	uchar_t *vi)
{
	int i;

	leaf->selected_contype = *vi++;
	leaf->selected_contype |= *vi++ << 8;

	if (dnetp->board_type == DEVICE_ID_21140) /* Sect. 4.3 */
		leaf->gpr = *vi++;

	leaf->block_count = *vi++;

	if (leaf->block_count > MAX_MEDIA) {
		cmn_err(CE_WARN, "dnet: Too many media in SROM!");
		leaf->block_count = 1;
	}
	for (i = 0; i <= leaf->block_count; i++) {
		vi = parse_media_block(dnetp, leaf->block + i, vi);
		if (leaf->block[i].command & CMD_DEFAULT_MEDIUM)
			leaf->default_block = leaf->block+i;
	}
	/* No explicit default block: use last in the ROM */
	if (leaf->default_block == NULL)
		leaf->default_block = leaf->block + leaf->block_count -1;

}

static uchar_t *
parse_media_block(struct dnetinstance *dnetp, media_block_t *block, uchar_t *vi)
{
	int i;

	/*
	 * There are three kinds of media block we need to worry about:
	 * The 21041 blocks.
	 * 21140 blocks from a version 1 SROM
	 * 2114[023] block from a version 3 SROM
	 */

	if (dnetp->board_type == DEVICE_ID_21041) {
		/* Section 4.2 */
		block->media_code = *vi & 0x3f;
		block->type = 2;
		if (*vi++ & 0x40) {
			block->un.sia.csr13 = *vi++;
			block->un.sia.csr13 |= *vi++ << 8;
			block->un.sia.csr14 = *vi++;
			block->un.sia.csr14 |= *vi++ << 8;
			block->un.sia.csr15 = *vi++;
			block->un.sia.csr15 |= *vi++ << 8;
		} else {
			/* No media data (csrs 13,14,15). Insert defaults */
			switch (block->media_code) {
			case MEDIA_TP:
				block->un.sia.csr13 = 0xef01;
				block->un.sia.csr14 = 0x7f3f;
				block->un.sia.csr15 = 0x0008;
				break;
			case MEDIA_TP_FD:
				block->un.sia.csr13 = 0xef01;
				block->un.sia.csr14 = 0x7f3d;
				block->un.sia.csr15 = 0x0008;
				break;
			case MEDIA_BNC:
				block->un.sia.csr13 = 0xef09;
				block->un.sia.csr14 = 0x0705;
				block->un.sia.csr15 = 0x0006;
				break;
			case MEDIA_AUI:
				block->un.sia.csr13 = 0xef09;
				block->un.sia.csr14 = 0x0705;
				block->un.sia.csr15 = 0x000e;
				break;
			}
		}
	} else  if (*vi & 0x80) {  /* Extended format: Section 4.3.2.2 */
		int blocklen = *vi++ & 0x7f;
		block->type = *vi++;
		switch (block->type) {
		case 0: /* "non-MII": Section 4.3.2.2.1 */
			block->media_code = (*vi++) & 0x3f;
			block->gprseqlen = 1;
			block->gprseq[0] = *vi++;
			block->command = *vi++;
			block->command |= *vi++ << 8;
			break;

		case 1: /* MII/PHY: Section 4.3.2.2.2 */
			block->command = CMD_PS;
			block->media_code = MEDIA_MII;
				/* This is whats needed in CSR6 */

			block->un.mii.phy_num = *vi++;
			block->gprseqlen = *vi++;

			for (i = 0; i < block->gprseqlen; i++)
				block->gprseq[i] = *vi++;
			block->rstseqlen = *vi++;
			for (i = 0; i < block->rstseqlen; i++)
				block->rstseq[i] = *vi++;

			block->un.mii.mediacaps = *vi++;
			block->un.mii.mediacaps |= *vi++ << 8;
			block->un.mii.nwayadvert = *vi++;
			block->un.mii.nwayadvert |= *vi++ << 8;
			block->un.mii.fdxmask = *vi++;
			block->un.mii.fdxmask |= *vi++ << 8;
			block->un.mii.ttmmask = *vi++;
			block->un.mii.ttmmask |= *vi++ << 8;
			break;

		case 2: /* SIA Media: Section 4.4.2.1.1 */
			block->media_code = *vi & 0x3f;
			if (*vi++ & 0x40) {
				block->un.sia.csr13 = *vi++;
				block->un.sia.csr13 |= *vi++ << 8;
				block->un.sia.csr14 = *vi++;
				block->un.sia.csr14 |= *vi++ << 8;
				block->un.sia.csr15 = *vi++;
				block->un.sia.csr15 |= *vi++ << 8;
			} else {
				/*
				 * SIA values not provided by SROM; provide
				 * defaults. See appendix D of 2114[23] manuals.
				 */
				switch (block->media_code) {
				case MEDIA_BNC:
					block->un.sia.csr13 = 0x0009;
					block->un.sia.csr14 = 0x0705;
					block->un.sia.csr15 = 0x0000;
					break;
				case MEDIA_AUI:
					block->un.sia.csr13 = 0x0009;
					block->un.sia.csr14 = 0x0705;
					block->un.sia.csr15 = 0x0008;
					break;
				case MEDIA_TP:
					block->un.sia.csr13 = 0x0001;
					block->un.sia.csr14 = 0x7f3f;
					block->un.sia.csr15 = 0x0000;
					break;
				case MEDIA_TP_FD:
					block->un.sia.csr13 = 0x0001;
					block->un.sia.csr14 = 0x7f3d;
					block->un.sia.csr15 = 0x0000;
					break;
				default:
					block->un.sia.csr13 = 0x0000;
					block->un.sia.csr14 = 0x0000;
					block->un.sia.csr15 = 0x0000;
				}
			}

			/* Treat GP control/data as a GPR sequence */
			block->gprseqlen = 2;
			block->gprseq[0] = *vi++;
			block->gprseq[0] |= *vi++ << 8;
			block->gprseq[0] |= GPR_CONTROL_WRITE;
			block->gprseq[1] = *vi++;
			block->gprseq[1] |= *vi++ << 8;
			break;

		case 3: /* MII/PHY : Section 4.4.2.1.2 */
			block->command = CMD_PS;
			block->media_code = MEDIA_MII;
			block->un.mii.phy_num = *vi++;

			block->gprseqlen = *vi++;
			for (i = 0; i < block->gprseqlen; i++) {
				block->gprseq[i] = *vi++;
				block->gprseq[i] |= *vi++ << 8;
			}

			block->rstseqlen = *vi++;
			for (i = 0; i < block->rstseqlen; i++) {
				block->rstseq[i] = *vi++;
				block->rstseq[i] |= *vi++ << 8;
			}
			block->un.mii.mediacaps = *vi++;
			block->un.mii.mediacaps |= *vi++ << 8;
			block->un.mii.nwayadvert = *vi++;
			block->un.mii.nwayadvert |= *vi++ << 8;
			block->un.mii.fdxmask = *vi++;
			block->un.mii.fdxmask |= *vi++ << 8;
			block->un.mii.ttmmask = *vi++;
			block->un.mii.ttmmask |= *vi++ << 8;
			block->un.mii.miiintr |= *vi++;
			break;

		case 4: /* SYM Media: 4.5.2.1.3 */
			block->media_code = *vi++ & 0x3f;
			/* Treat GP control and data as a GPR sequence */
			block->gprseqlen = 2;
			block->gprseq[0] = *vi++;
			block->gprseq[0] |= *vi++ << 8;
			block->gprseq[0] |= GPR_CONTROL_WRITE;
			block->gprseq[1]  = *vi++;
			block->gprseq[1] |= *vi++ << 8;
			block->command = *vi++;
			block->command = *vi++ << 8;
			break;

		case 5: /* GPR reset sequence:  Section 4.5.2.1.4 */
			block->rstseqlen = *vi++;
			for (i = 0; i < block->rstseqlen; i++)
				block->rstseq[i] = *vi++;
			break;

		default: /* Unknown media block. Skip it. */
			cmn_err(CE_WARN, "dnet: Unsupported SROM block.");
			vi += blocklen;
			break;
		}
	} else { /* Compact format (or V1 SROM): Section 4.3.2.1 */
		block->type = 0;
		block->media_code = *vi++ & 0x3f;
		block->gprseqlen = 1;
		block->gprseq[0] = *vi++;
		block->command = *vi++;
		block->command |= (*vi++) << 8;
	}
	return (vi);
}


/*
 * An alternative to doing this would be to store the legacy ROMs in binary
 * format in the conf file, and in read_srom, pick out the data. This would
 * then allow the parser to continue on as normal. This makes it a little
 * easier to read.
 */
static void
setup_legacy_blocks()
{
	LEAF_FORMAT *leaf;
	media_block_t *block;

	/* Default FAKE SROM */
	leaf = &leaf_default_100;
	leaf->is_static = 1;
	leaf->default_block = &leaf->block[3];
	leaf->block_count = 4; /* 100 cards are highly unlikely to have BNC */
	block = leaf->block;
	block->media_code = MEDIA_TP_FD;
	block->type = 0;
	block->command = 0x8e;  /* PCS, PS off, media sense: bit7, pol=1 */
	block++;
	block->media_code = MEDIA_TP;
	block->type = 0;
	block->command = 0x8e;  /* PCS, PS off, media sense: bit7, pol=1 */
	block++;
	block->media_code = MEDIA_SYM_SCR_FD;
	block->type = 0;
	block->command = 0x6d;  /* PCS, PS, SCR on, media sense: bit6, pol=0 */
	block++;
	block->media_code = MEDIA_SYM_SCR;
	block->type = 0;
	block->command = 0x406d; /* PCS, PS, SCR on, media sense: bit6, pol=0 */

	/* COGENT FAKE SROM */
	leaf = &leaf_cogent_100;
	leaf->is_static = 1;
	leaf->default_block = &leaf->block[4];
	leaf->block_count = 5; /* 100TX, 100TX-FD, 10T 10T-FD, BNC */
	block = leaf->block; /* BNC */
	block->media_code = MEDIA_BNC;
	block->type = 0;
	block->command =  0x8000; /* No media sense, PCS, SCR, PS all off */
	block->gprseqlen = 2;
	block->rstseqlen = 0;
	block->gprseq[0] = 0x13f;
	block->gprseq[1] = 1;

	block++;
	block->media_code = MEDIA_TP_FD;
	block->type = 0;
	block->command = 0x8e;  /* PCS, PS off, media sense: bit7, pol=1 */
	block->gprseqlen = 2;
	block->rstseqlen = 0;
	block->gprseq[0] = 0x13f;
	block->gprseq[1] = 0x26;

	block++; /* 10BaseT */
	block->media_code = MEDIA_TP;
	block->type = 0;
	block->command = 0x8e;  /* PCS, PS off, media sense: bit7, pol=1 */
	block->gprseqlen = 2;
	block->rstseqlen = 0;
	block->gprseq[0] = 0x13f;
	block->gprseq[1] = 0x3e;

	block++; /* 100BaseTX-FD */
	block->media_code = MEDIA_SYM_SCR_FD;
	block->type = 0;
	block->command = 0x6d;  /* PCS, PS, SCR on, media sense: bit6, pol=0 */
	block->gprseqlen = 2;
	block->rstseqlen = 0;
	block->gprseq[0] = 0x13f;
	block->gprseq[1] = 1;

	block++; /* 100BaseTX */
	block->media_code = MEDIA_SYM_SCR;
	block->type = 0;
	block->command = 0x406d; /* PCS, PS, SCR on, media sense: bit6, pol=0 */
	block->gprseqlen = 2;
	block->rstseqlen = 0;
	block->gprseq[0] = 0x13f;
	block->gprseq[1] = 1;

	/* Generic legacy card with a PHY. */
	leaf = &leaf_phylegacy;
	leaf->block_count = 1;
	leaf->mii_block = leaf->block;
	leaf->default_block = &leaf->block[0];
	leaf->is_static = 1;
	block = leaf->block;
	block->media_code = MEDIA_MII;
	block->type = 1; /* MII Block type 1 */
	block->command = 1; /* Port select */
	block->gprseqlen = 0;
	block->rstseqlen = 0;

	/* ASANTE FAKE SROM */
	leaf = &leaf_asante;
	leaf->is_static = 1;
	leaf->default_block = &leaf->block[0];
	leaf->block_count = 1;
	block = leaf->block;
	block->media_code = MEDIA_MII;
	block->type = 1; /* MII Block type 1 */
	block->command = 1; /* Port select */
	block->gprseqlen = 3;
	block->rstseqlen = 0;
	block->gprseq[0] = 0x180;
	block->gprseq[1] = 0x80;
	block->gprseq[2] = 0x0;

	/* LEGACY 21041 card FAKE SROM */
	leaf = &leaf_21041;
	leaf->is_static = 1;
	leaf->block_count = 4;  /* SIA Blocks for TP, TPfd, BNC, AUI */
	leaf->default_block = &leaf->block[3];

	block = leaf->block;
	block->media_code = MEDIA_AUI;
	block->type = 2;
	block->un.sia.csr13 = 0xef09;
	block->un.sia.csr14 = 0x0705;
	block->un.sia.csr15 = 0x000e;

	block++;
	block->media_code = MEDIA_TP_FD;
	block->type = 2;
	block->un.sia.csr13 = 0xef01;
	block->un.sia.csr14 = 0x7f3d;
	block->un.sia.csr15 = 0x0008;

	block++;
	block->media_code = MEDIA_BNC;
	block->type = 2;
	block->un.sia.csr13 = 0xef09;
	block->un.sia.csr14 = 0x0705;
	block->un.sia.csr15 = 0x0006;

	block++;
	block->media_code = MEDIA_TP;
	block->type = 2;
	block->un.sia.csr13 = 0xef01;
	block->un.sia.csr14 = 0x7f3f;
	block->un.sia.csr15 = 0x0008;

	/* LEGACY 21040 card FAKE SROM */
	leaf = &leaf_21040;
	leaf->is_static = 1;
	leaf->block_count = 4;  /* SIA Blocks for TP, TPfd, BNC, AUI */
	block = leaf->block;
	block->media_code = MEDIA_AUI;
	block->type = 2;
	block->un.sia.csr13 = 0x8f09;
	block->un.sia.csr14 = 0x0705;
	block->un.sia.csr15 = 0x000e;
	block++;
	block->media_code = MEDIA_TP_FD;
	block->type = 2;
	block->un.sia.csr13 = 0x0f01;
	block->un.sia.csr14 = 0x7f3d;
	block->un.sia.csr15 = 0x0008;
	block++;
	block->media_code = MEDIA_BNC;
	block->type = 2;
	block->un.sia.csr13 = 0xef09;
	block->un.sia.csr14 = 0x0705;
	block->un.sia.csr15 = 0x0006;
	block++;
	block->media_code = MEDIA_TP;
	block->type = 2;
	block->un.sia.csr13 = 0x8f01;
	block->un.sia.csr14 = 0x7f3f;
	block->un.sia.csr15 = 0x0008;
}

static void
dnet_print_srom(SROM_FORMAT *sr)
{
	int i;
	uchar_t *a = sr->netaddr;
	cmn_err(CE_NOTE, "SROM Dump: %d. ver %d, Num adapters %d,"
		"Addr:%x:%x:%x:%x:%x:%x",
		sr->init_from_srom, sr->version, sr->adapters,
		a[0], a[1], a[2], a[3], a[4], a[5]);

	for (i = 0; i < sr->adapters; i++)
		dnet_dump_leaf(sr->leaf+i);
}

static void
dnet_dump_leaf(LEAF_FORMAT *leaf)
{
	int i;
	cmn_err(CE_NOTE, "Leaf: Device %d, block_count %d, gpr: %x",
		leaf->device_number, leaf->block_count, leaf->gpr);
	for (i = 0; i < leaf->block_count; i++)
		dnet_dump_block(leaf->block+i);
}

static void
dnet_dump_block(media_block_t *block)
{
	cmn_err(CE_NOTE, "Block(%p): type %x, media %s, command: %x ",
	    (void *)block,
	    block->type, media_str[block->media_code], block->command);
	dnet_dumpbin("\tGPR Seq", (uchar_t *)block->gprseq, 2,
	    block->gprseqlen *2);
	dnet_dumpbin("\tGPR Reset", (uchar_t *)block->rstseq, 2,
	    block->rstseqlen *2);
	switch (block->type) {
	case 1: case 3:
		cmn_err(CE_NOTE, "\tMII Info: phy %d, nway %x, fdx"
			"%x, ttm %x, mediacap %x",
			block->un.mii.phy_num, block->un.mii.nwayadvert,
			block->un.mii.fdxmask, block->un.mii.ttmmask,
			block->un.mii.mediacaps);
		break;
	case 2:
		cmn_err(CE_NOTE, "\tSIA Regs: CSR13:%x, CSR14:%x, CSR15:%x",
			block->un.sia.csr13, block->un.sia.csr14,
			block->un.sia.csr15);
		break;
	}
}


/* Utility to print out binary info dumps. Handy for SROMs, etc */

static int
hexcode(unsigned val)
{
	if (val <= 9)
		return (val +'0');
	if (val <= 15)
		return (val + 'a' - 10);
	return (-1);
}

static void
dnet_dumpbin(char *msg, unsigned char *data, int size, int len)
{
	char hex[128], *p = hex;
	char ascii[128], *q = ascii;
	int i, j;

	if (!len)
		return;

	for (i = 0; i < len; i += size) {
		for (j = size - 1; j >= 0; j--) { /* PORTABILITY: byte order */
			*p++ = hexcode(data[i+j] >> 4);
			*p++ = hexcode(data[i+j] & 0xf);
			*q++ = (data[i+j] < 32 || data[i+j] > 127) ?
			    '.' : data[i];
		}
		*p++ = ' ';
		if (q-ascii >= 8) {
			*p = *q = 0;
			cmn_err(CE_NOTE, "%s: %s\t%s", msg, hex, ascii);
			p = hex;
			q = ascii;
		}
	}
	if (p != hex) {
		while ((p - hex) < 8*3)
		    *p++ = ' ';
		*p = *q = 0;
		cmn_err(CE_NOTE, "%s: %s\t%s", msg, hex, ascii);
	}
}

#ifdef DNETDEBUG
void
dnet_usectimeout(struct dnetinstance *dnetp, uint32_t usecs, int contin,
    timercb_t cback)
{
	mutex_enter(&dnetp->intrlock);
	dnetp->timer.start_ticks = (usecs * 100) / 8192;
	dnetp->timer.cb = cback;
	outl(dnetp->io_reg + GP_TIMER_REG, dnetp->timer.start_ticks |
		(contin ? GPTIMER_CONT : 0));
	if (dnetp->timer.cb)
		enable_interrupts(dnetp, 1);
	mutex_exit(&dnetp->intrlock);
}

uint32_t
dnet_usecelapsed(struct dnetinstance *dnetp)
{
	uint32_t ticks = dnetp->timer.start_ticks -
	    (inl(dnetp->io_reg + GP_TIMER_REG) & 0xffff);
	return ((ticks * 8192) / 100);
}

/* ARGSUSED */
void
dnet_timestamp(struct dnetinstance *dnetp,  char *buf)
{
	uint32_t elapsed = dnet_usecelapsed(dnetp);
	char loc[32], *p = loc;
	int firstdigit = 1;
	uint32_t divisor;

	while (*p++ = *buf++)
		;
	p--;

	for (divisor = 1000000000; divisor /= 10; ) {
		int digit = (elapsed / divisor);
		elapsed -= digit * divisor;
		if (!firstdigit || digit) {
			*p++ = digit + '0';
			firstdigit = 0;
		}

	}

	/* Actual zero, output it */
	if (firstdigit)
		*p++ = '0';

	*p++ = '-';
	*p++ = '>';
	*p++ = 0;

	printf(loc);
	dnet_usectimeout(dnetp, 1000000, 0, 0);
}

#endif