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

/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
 * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
 */

/*
 * **********************************************************************
 *									*
 * Module Name:								*
 *   e1000g_main.c							*
 *									*
 * Abstract:								*
 *   This file contains the interface routines for the solaris OS.	*
 *   It has all DDI entry point routines and GLD entry point routines.	*
 *									*
 *   This file also contains routines that take care of initialization	*
 *   uninit routine and interrupt routine.				*
 *									*
 * **********************************************************************
 */

#include <sys/dlpi.h>
#include <sys/mac.h>
#include "e1000g_sw.h"
#include "e1000g_debug.h"

static char ident[] = "Intel PRO/1000 Ethernet";
/* LINTED E_STATIC_UNUSED */
static char e1000g_version[] = "Driver Ver. 5.3.24";

/*
 * Proto types for DDI entry points
 */
static int e1000g_attach(dev_info_t *, ddi_attach_cmd_t);
static int e1000g_detach(dev_info_t *, ddi_detach_cmd_t);
static int e1000g_quiesce(dev_info_t *);

/*
 * init and intr routines prototype
 */
static int e1000g_resume(dev_info_t *);
static int e1000g_suspend(dev_info_t *);
static uint_t e1000g_intr_pciexpress(caddr_t);
static uint_t e1000g_intr(caddr_t);
static void e1000g_intr_work(struct e1000g *, uint32_t);
#pragma inline(e1000g_intr_work)
static int e1000g_init(struct e1000g *);
static int e1000g_start(struct e1000g *, boolean_t);
static void e1000g_stop(struct e1000g *, boolean_t);
static int e1000g_m_start(void *);
static void e1000g_m_stop(void *);
static int e1000g_m_promisc(void *, boolean_t);
static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
static int e1000g_m_setprop(void *, const char *, mac_prop_id_t,
    uint_t, const void *);
static int e1000g_m_getprop(void *, const char *, mac_prop_id_t,
			    uint_t, void *);
static void e1000g_m_propinfo(void *, const char *, mac_prop_id_t,
    mac_prop_info_handle_t);
static int e1000g_set_priv_prop(struct e1000g *, const char *, uint_t,
    const void *);
static int e1000g_get_priv_prop(struct e1000g *, const char *, uint_t, void *);
static void e1000g_init_locks(struct e1000g *);
static void e1000g_destroy_locks(struct e1000g *);
static int e1000g_identify_hardware(struct e1000g *);
static int e1000g_regs_map(struct e1000g *);
static int e1000g_set_driver_params(struct e1000g *);
static void e1000g_set_bufsize(struct e1000g *);
static int e1000g_register_mac(struct e1000g *);
static boolean_t e1000g_rx_drain(struct e1000g *);
static boolean_t e1000g_tx_drain(struct e1000g *);
static void e1000g_init_unicst(struct e1000g *);
static int e1000g_unicst_set(struct e1000g *, const uint8_t *, int);
static int e1000g_alloc_rx_data(struct e1000g *);
static void e1000g_release_multicast(struct e1000g *);
static void e1000g_pch_limits(struct e1000g *);
static uint32_t e1000g_mtu2maxframe(uint32_t);

/*
 * Local routines
 */
static boolean_t e1000g_reset_adapter(struct e1000g *);
static void e1000g_tx_clean(struct e1000g *);
static void e1000g_rx_clean(struct e1000g *);
static void e1000g_link_timer(void *);
static void e1000g_local_timer(void *);
static boolean_t e1000g_link_check(struct e1000g *);
static boolean_t e1000g_stall_check(struct e1000g *);
static void e1000g_smartspeed(struct e1000g *);
static void e1000g_get_conf(struct e1000g *);
static boolean_t e1000g_get_prop(struct e1000g *, char *, int, int, int,
    int *);
static void enable_watchdog_timer(struct e1000g *);
static void disable_watchdog_timer(struct e1000g *);
static void start_watchdog_timer(struct e1000g *);
static void restart_watchdog_timer(struct e1000g *);
static void stop_watchdog_timer(struct e1000g *);
static void stop_link_timer(struct e1000g *);
static void stop_82547_timer(e1000g_tx_ring_t *);
static void e1000g_force_speed_duplex(struct e1000g *);
static void e1000g_setup_max_mtu(struct e1000g *);
static void e1000g_get_max_frame_size(struct e1000g *);
static boolean_t is_valid_mac_addr(uint8_t *);
static void e1000g_unattach(dev_info_t *, struct e1000g *);
static int e1000g_get_bar_info(dev_info_t *, int, bar_info_t *);
#ifdef E1000G_DEBUG
static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
    struct iocblk *, mblk_t *);
#endif
static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
    struct iocblk *, mblk_t *);
static boolean_t e1000g_check_loopback_support(struct e1000_hw *);
static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
static void e1000g_set_internal_loopback(struct e1000g *);
static void e1000g_set_external_loopback_1000(struct e1000g *);
static void e1000g_set_external_loopback_100(struct e1000g *);
static void e1000g_set_external_loopback_10(struct e1000g *);
static int e1000g_add_intrs(struct e1000g *);
static int e1000g_intr_add(struct e1000g *, int);
static int e1000g_rem_intrs(struct e1000g *);
static int e1000g_enable_intrs(struct e1000g *);
static int e1000g_disable_intrs(struct e1000g *);
static boolean_t e1000g_link_up(struct e1000g *);
#ifdef __sparc
static boolean_t e1000g_find_mac_address(struct e1000g *);
#endif
static void e1000g_get_phy_state(struct e1000g *);
static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
    const void *impl_data);
static void e1000g_fm_init(struct e1000g *Adapter);
static void e1000g_fm_fini(struct e1000g *Adapter);
static void e1000g_param_sync(struct e1000g *);
static void e1000g_get_driver_control(struct e1000_hw *);
static void e1000g_release_driver_control(struct e1000_hw *);
static void e1000g_restore_promisc(struct e1000g *Adapter);

char *e1000g_priv_props[] = {
	"_tx_bcopy_threshold",
	"_tx_interrupt_enable",
	"_tx_intr_delay",
	"_tx_intr_abs_delay",
	"_rx_bcopy_threshold",
	"_max_num_rcv_packets",
	"_rx_intr_delay",
	"_rx_intr_abs_delay",
	"_intr_throttling_rate",
	"_intr_adaptive",
	"_adv_pause_cap",
	"_adv_asym_pause_cap",
	NULL
};

static struct cb_ops cb_ws_ops = {
	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 */
	NULL,			/* cb_stream */
	D_MP | D_HOTPLUG,	/* cb_flag */
	CB_REV,			/* cb_rev */
	nodev,			/* cb_aread */
	nodev			/* cb_awrite */
};

static struct dev_ops ws_ops = {
	DEVO_REV,		/* devo_rev */
	0,			/* devo_refcnt */
	NULL,			/* devo_getinfo */
	nulldev,		/* devo_identify */
	nulldev,		/* devo_probe */
	e1000g_attach,		/* devo_attach */
	e1000g_detach,		/* devo_detach */
	nodev,			/* devo_reset */
	&cb_ws_ops,		/* devo_cb_ops */
	NULL,			/* devo_bus_ops */
	ddi_power,		/* devo_power */
	e1000g_quiesce		/* devo_quiesce */
};

static struct modldrv modldrv = {
	&mod_driverops,		/* Type of module.  This one is a driver */
	ident,			/* Discription string */
	&ws_ops,		/* driver ops */
};

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

/* Access attributes for register mapping */
static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
	DDI_DEVICE_ATTR_V1,
	DDI_STRUCTURE_LE_ACC,
	DDI_STRICTORDER_ACC,
	DDI_FLAGERR_ACC
};

#define	E1000G_M_CALLBACK_FLAGS \
	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP | MC_PROPINFO)

static mac_callbacks_t e1000g_m_callbacks = {
	E1000G_M_CALLBACK_FLAGS,
	e1000g_m_stat,
	e1000g_m_start,
	e1000g_m_stop,
	e1000g_m_promisc,
	e1000g_m_multicst,
	NULL,
	e1000g_m_tx,
	NULL,
	e1000g_m_ioctl,
	e1000g_m_getcapab,
	NULL,
	NULL,
	e1000g_m_setprop,
	e1000g_m_getprop,
	e1000g_m_propinfo
};

/*
 * Global variables
 */
uint32_t e1000g_jumbo_mtu = MAXIMUM_MTU_9K;
uint32_t e1000g_mblks_pending = 0;
/*
 * Workaround for Dynamic Reconfiguration support, for x86 platform only.
 * Here we maintain a private dev_info list if e1000g_force_detach is
 * enabled. If we force the driver to detach while there are still some
 * rx buffers retained in the upper layer, we have to keep a copy of the
 * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
 * structure will be freed after the driver is detached. However when we
 * finally free those rx buffers released by the upper layer, we need to
 * refer to the dev_info to free the dma buffers. So we save a copy of
 * the dev_info for this purpose. On x86 platform, we assume this copy
 * of dev_info is always valid, but on SPARC platform, it could be invalid
 * after the system board level DR operation. For this reason, the global
 * variable e1000g_force_detach must be B_FALSE on SPARC platform.
 */
#ifdef __sparc
boolean_t e1000g_force_detach = B_FALSE;
#else
boolean_t e1000g_force_detach = B_TRUE;
#endif
private_devi_list_t *e1000g_private_devi_list = NULL;

/*
 * The mutex e1000g_rx_detach_lock is defined to protect the processing of
 * the private dev_info list, and to serialize the processing of rx buffer
 * freeing and rx buffer recycling.
 */
kmutex_t e1000g_rx_detach_lock;
/*
 * The rwlock e1000g_dma_type_lock is defined to protect the global flag
 * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
 * If there are many e1000g instances, the system may run out of DVMA
 * resources during the initialization of the instances, then the flag will
 * be changed to "USE_DMA". Because different e1000g instances are initialized
 * in parallel, we need to use this lock to protect the flag.
 */
krwlock_t e1000g_dma_type_lock;

/*
 * The 82546 chipset is a dual-port device, both the ports share one eeprom.
 * Based on the information from Intel, the 82546 chipset has some hardware
 * problem. When one port is being reset and the other port is trying to
 * access the eeprom, it could cause system hang or panic. To workaround this
 * hardware problem, we use a global mutex to prevent such operations from
 * happening simultaneously on different instances. This workaround is applied
 * to all the devices supported by this driver.
 */
kmutex_t e1000g_nvm_lock;

/*
 * Loadable module configuration entry points for the driver
 */

/*
 * _init - module initialization
 */
int
_init(void)
{
	int status;

	mac_init_ops(&ws_ops, WSNAME);
	status = mod_install(&modlinkage);
	if (status != DDI_SUCCESS)
		mac_fini_ops(&ws_ops);
	else {
		mutex_init(&e1000g_rx_detach_lock, NULL, MUTEX_DRIVER, NULL);
		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
		mutex_init(&e1000g_nvm_lock, NULL, MUTEX_DRIVER, NULL);
	}

	return (status);
}

/*
 * _fini - module finalization
 */
int
_fini(void)
{
	int status;

	if (e1000g_mblks_pending != 0)
		return (EBUSY);

	status = mod_remove(&modlinkage);
	if (status == DDI_SUCCESS) {
		mac_fini_ops(&ws_ops);

		if (e1000g_force_detach) {
			private_devi_list_t *devi_node;

			mutex_enter(&e1000g_rx_detach_lock);
			while (e1000g_private_devi_list != NULL) {
				devi_node = e1000g_private_devi_list;
				e1000g_private_devi_list =
				    e1000g_private_devi_list->next;

				kmem_free(devi_node->priv_dip,
				    sizeof (struct dev_info));
				kmem_free(devi_node,
				    sizeof (private_devi_list_t));
			}
			mutex_exit(&e1000g_rx_detach_lock);
		}

		mutex_destroy(&e1000g_rx_detach_lock);
		rw_destroy(&e1000g_dma_type_lock);
		mutex_destroy(&e1000g_nvm_lock);
	}

	return (status);
}

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

/*
 * e1000g_attach - driver attach
 *
 * This function is the device-specific initialization entry
 * point. This entry point is required and must be written.
 * The DDI_ATTACH command must be provided in the attach entry
 * point. When attach() is called with cmd set to DDI_ATTACH,
 * all normal kernel services (such as kmem_alloc(9F)) are
 * available for use by the driver.
 *
 * The attach() function will be called once for each instance
 * of  the  device  on  the  system with cmd set to DDI_ATTACH.
 * Until attach() succeeds, the only driver entry points which
 * may be called are open(9E) and getinfo(9E).
 */
static int
e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
{
	struct e1000g *Adapter;
	struct e1000_hw *hw;
	struct e1000g_osdep *osdep;
	int instance;

	switch (cmd) {
	default:
		e1000g_log(NULL, CE_WARN,
		    "Unsupported command send to e1000g_attach... ");
		return (DDI_FAILURE);

	case DDI_RESUME:
		return (e1000g_resume(devinfo));

	case DDI_ATTACH:
		break;
	}

	/*
	 * get device instance number
	 */
	instance = ddi_get_instance(devinfo);

	/*
	 * Allocate soft data structure
	 */
	Adapter =
	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);

	Adapter->dip = devinfo;
	Adapter->instance = instance;
	Adapter->tx_ring->adapter = Adapter;
	Adapter->rx_ring->adapter = Adapter;

	hw = &Adapter->shared;
	osdep = &Adapter->osdep;
	hw->back = osdep;
	osdep->adapter = Adapter;

	ddi_set_driver_private(devinfo, (caddr_t)Adapter);

	/*
	 * Initialize for fma support
	 */
	(void) e1000g_get_prop(Adapter, "fm-capable",
	    0, 0x0f,
	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE,
	    &Adapter->fm_capabilities);
	e1000g_fm_init(Adapter);
	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;

	/*
	 * PCI Configure
	 */
	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
		goto attach_fail;
	}
	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;

	/*
	 * Setup hardware
	 */
	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
		goto attach_fail;
	}

	/*
	 * Map in the device registers.
	 */
	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
		goto attach_fail;
	}
	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;

	/*
	 * Initialize driver parameters
	 */
	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
		goto attach_fail;
	}
	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;

	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
		goto attach_fail;
	}

	/*
	 * Initialize interrupts
	 */
	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
		goto attach_fail;
	}
	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;

	/*
	 * Initialize mutex's for this device.
	 * Do this before enabling the interrupt handler and
	 * register the softint to avoid the condition where
	 * interrupt handler can try using uninitialized mutex
	 */
	e1000g_init_locks(Adapter);
	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;

	/*
	 * Initialize Driver Counters
	 */
	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Init stats failed");
		goto attach_fail;
	}
	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;

	/*
	 * Initialize chip hardware and software structures
	 */
	rw_enter(&Adapter->chip_lock, RW_WRITER);
	if (e1000g_init(Adapter) != DDI_SUCCESS) {
		rw_exit(&Adapter->chip_lock);
		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
		goto attach_fail;
	}
	rw_exit(&Adapter->chip_lock);
	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;

	/*
	 * Register the driver to the MAC
	 */
	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
		goto attach_fail;
	}
	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;

	/*
	 * Now that mutex locks are initialized, and the chip is also
	 * initialized, enable interrupts.
	 */
	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
		goto attach_fail;
	}
	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;

	/*
	 * If e1000g_force_detach is enabled, in global private dip list,
	 * we will create a new entry, which maintains the priv_dip for DR
	 * supports after driver detached.
	 */
	if (e1000g_force_detach) {
		private_devi_list_t *devi_node;

		Adapter->priv_dip =
		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
		    sizeof (struct dev_info));

		devi_node =
		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);

		mutex_enter(&e1000g_rx_detach_lock);
		devi_node->priv_dip = Adapter->priv_dip;
		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
		devi_node->pending_rx_count = 0;

		Adapter->priv_devi_node = devi_node;

		if (e1000g_private_devi_list == NULL) {
			devi_node->prev = NULL;
			devi_node->next = NULL;
			e1000g_private_devi_list = devi_node;
		} else {
			devi_node->prev = NULL;
			devi_node->next = e1000g_private_devi_list;
			e1000g_private_devi_list->prev = devi_node;
			e1000g_private_devi_list = devi_node;
		}
		mutex_exit(&e1000g_rx_detach_lock);
	}

	Adapter->e1000g_state = E1000G_INITIALIZED;
	return (DDI_SUCCESS);

attach_fail:
	e1000g_unattach(devinfo, Adapter);
	return (DDI_FAILURE);
}

static int
e1000g_register_mac(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;
	mac_register_t *mac;
	int err;

	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
		return (DDI_FAILURE);

	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
	mac->m_driver = Adapter;
	mac->m_dip = Adapter->dip;
	mac->m_src_addr = hw->mac.addr;
	mac->m_callbacks = &e1000g_m_callbacks;
	mac->m_min_sdu = 0;
	mac->m_max_sdu = Adapter->default_mtu;
	mac->m_margin = VLAN_TAGSZ;
	mac->m_priv_props = e1000g_priv_props;
	mac->m_v12n = MAC_VIRT_LEVEL1;

	err = mac_register(mac, &Adapter->mh);
	mac_free(mac);

	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
}

static int
e1000g_identify_hardware(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;
	struct e1000g_osdep *osdep = &Adapter->osdep;

	/* Get the device id */
	hw->vendor_id =
	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
	hw->device_id =
	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
	hw->revision_id =
	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
	hw->subsystem_device_id =
	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
	hw->subsystem_vendor_id =
	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);

	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
		    "MAC type could not be set properly.");
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

static int
e1000g_regs_map(struct e1000g *Adapter)
{
	dev_info_t *devinfo = Adapter->dip;
	struct e1000_hw *hw = &Adapter->shared;
	struct e1000g_osdep *osdep = &Adapter->osdep;
	off_t mem_size;
	bar_info_t bar_info;
	int offset, rnumber;

	rnumber = ADAPTER_REG_SET;
	/* Get size of adapter register memory */
	if (ddi_dev_regsize(devinfo, rnumber, &mem_size) !=
	    DDI_SUCCESS) {
		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
		    "ddi_dev_regsize for registers failed");
		return (DDI_FAILURE);
	}

	/* Map adapter register memory */
	if ((ddi_regs_map_setup(devinfo, rnumber,
	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
	    &osdep->reg_handle)) != DDI_SUCCESS) {
		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
		    "ddi_regs_map_setup for registers failed");
		goto regs_map_fail;
	}

	/* ICH needs to map flash memory */
	switch (hw->mac.type) {
	case e1000_ich8lan:
	case e1000_ich9lan:
	case e1000_ich10lan:
	case e1000_pchlan:
	case e1000_pch2lan:
	case e1000_pch_lpt:
		rnumber = ICH_FLASH_REG_SET;

		/* get flash size */
		if (ddi_dev_regsize(devinfo, rnumber,
		    &mem_size) != DDI_SUCCESS) {
			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
			    "ddi_dev_regsize for ICH flash failed");
			goto regs_map_fail;
		}

		/* map flash in */
		if (ddi_regs_map_setup(devinfo, rnumber,
		    (caddr_t *)&hw->flash_address, 0,
		    mem_size, &e1000g_regs_acc_attr,
		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
			    "ddi_regs_map_setup for ICH flash failed");
			goto regs_map_fail;
		}
		break;
	default:
		break;
	}

	/* map io space */
	switch (hw->mac.type) {
	case e1000_82544:
	case e1000_82540:
	case e1000_82545:
	case e1000_82546:
	case e1000_82541:
	case e1000_82541_rev_2:
		/* find the IO bar */
		rnumber = -1;
		for (offset = PCI_CONF_BASE1;
		    offset <= PCI_CONF_BASE5; offset += 4) {
			if (e1000g_get_bar_info(devinfo, offset, &bar_info)
			    != DDI_SUCCESS)
				continue;
			if (bar_info.type == E1000G_BAR_IO) {
				rnumber = bar_info.rnumber;
				break;
			}
		}

		if (rnumber < 0) {
			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
			    "No io space is found");
			goto regs_map_fail;
		}

		/* get io space size */
		if (ddi_dev_regsize(devinfo, rnumber,
		    &mem_size) != DDI_SUCCESS) {
			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
			    "ddi_dev_regsize for io space failed");
			goto regs_map_fail;
		}

		/* map io space */
		if ((ddi_regs_map_setup(devinfo, rnumber,
		    (caddr_t *)&hw->io_base, 0, mem_size,
		    &e1000g_regs_acc_attr,
		    &osdep->io_reg_handle)) != DDI_SUCCESS) {
			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
			    "ddi_regs_map_setup for io space failed");
			goto regs_map_fail;
		}
		break;
	default:
		hw->io_base = 0;
		break;
	}

	return (DDI_SUCCESS);

regs_map_fail:
	if (osdep->reg_handle != NULL)
		ddi_regs_map_free(&osdep->reg_handle);
	if (osdep->ich_flash_handle != NULL)
		ddi_regs_map_free(&osdep->ich_flash_handle);
	return (DDI_FAILURE);
}

static int
e1000g_set_driver_params(struct e1000g *Adapter)
{
	struct e1000_hw *hw;

	hw = &Adapter->shared;

	/* Set MAC type and initialize hardware functions */
	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
		    "Could not setup hardware functions");
		return (DDI_FAILURE);
	}

	/* Get bus information */
	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
		    "Could not get bus information");
		return (DDI_FAILURE);
	}

	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);

	hw->mac.autoneg_failed = B_TRUE;

	/* Set the autoneg_wait_to_complete flag to B_FALSE */
	hw->phy.autoneg_wait_to_complete = B_FALSE;

	/* Adaptive IFS related changes */
	hw->mac.adaptive_ifs = B_TRUE;

	/* Enable phy init script for IGP phy of 82541/82547 */
	if ((hw->mac.type == e1000_82547) ||
	    (hw->mac.type == e1000_82541) ||
	    (hw->mac.type == e1000_82547_rev_2) ||
	    (hw->mac.type == e1000_82541_rev_2))
		e1000_init_script_state_82541(hw, B_TRUE);

	/* Enable the TTL workaround for 82541/82547 */
	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);

#ifdef __sparc
	Adapter->strip_crc = B_TRUE;
#else
	Adapter->strip_crc = B_FALSE;
#endif

	/* setup the maximum MTU size of the chip */
	e1000g_setup_max_mtu(Adapter);

	/* Get speed/duplex settings in conf file */
	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
	e1000g_force_speed_duplex(Adapter);

	/* Get Jumbo Frames settings in conf file */
	e1000g_get_max_frame_size(Adapter);

	/* Get conf file properties */
	e1000g_get_conf(Adapter);

	/* enforce PCH limits */
	e1000g_pch_limits(Adapter);

	/* Set Rx/Tx buffer size */
	e1000g_set_bufsize(Adapter);

	/* Master Latency Timer */
	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;

	/* copper options */
	if (hw->phy.media_type == e1000_media_type_copper) {
		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
		hw->phy.disable_polarity_correction = B_FALSE;
		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
	}

	/* The initial link state should be "unknown" */
	Adapter->link_state = LINK_STATE_UNKNOWN;

	/* Initialize rx parameters */
	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;

	/* Initialize tx parameters */
	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;

	/* Initialize rx parameters */
	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;

	return (DDI_SUCCESS);
}

static void
e1000g_setup_max_mtu(struct e1000g *Adapter)
{
	struct e1000_mac_info *mac = &Adapter->shared.mac;
	struct e1000_phy_info *phy = &Adapter->shared.phy;

	switch (mac->type) {
	/* types that do not support jumbo frames */
	case e1000_ich8lan:
	case e1000_82573:
	case e1000_82583:
		Adapter->max_mtu = ETHERMTU;
		break;
	/* ich9 supports jumbo frames except on one phy type */
	case e1000_ich9lan:
		if (phy->type == e1000_phy_ife)
			Adapter->max_mtu = ETHERMTU;
		else
			Adapter->max_mtu = MAXIMUM_MTU_9K;
		break;
	/* pch can do jumbo frames up to 4K */
	case e1000_pchlan:
		Adapter->max_mtu = MAXIMUM_MTU_4K;
		break;
	/* pch2 can do jumbo frames up to 9K */
	case e1000_pch2lan:
	case e1000_pch_lpt:
		Adapter->max_mtu = MAXIMUM_MTU_9K;
		break;
	/* types with a special limit */
	case e1000_82571:
	case e1000_82572:
	case e1000_82574:
	case e1000_80003es2lan:
	case e1000_ich10lan:
		if (e1000g_jumbo_mtu >= ETHERMTU &&
		    e1000g_jumbo_mtu <= MAXIMUM_MTU_9K) {
			Adapter->max_mtu = e1000g_jumbo_mtu;
		} else {
			Adapter->max_mtu = MAXIMUM_MTU_9K;
		}
		break;
	/* default limit is 16K */
	default:
		Adapter->max_mtu = FRAME_SIZE_UPTO_16K -
		    sizeof (struct ether_vlan_header) - ETHERFCSL;
		break;
	}
}

static void
e1000g_set_bufsize(struct e1000g *Adapter)
{
	struct e1000_mac_info *mac = &Adapter->shared.mac;
	uint64_t rx_size;
	uint64_t tx_size;

	dev_info_t *devinfo = Adapter->dip;
#ifdef __sparc
	ulong_t iommu_pagesize;
#endif
	/* Get the system page size */
	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);

#ifdef __sparc
	iommu_pagesize = dvma_pagesize(devinfo);
	if (iommu_pagesize != 0) {
		if (Adapter->sys_page_sz == iommu_pagesize) {
			if (iommu_pagesize > 0x4000)
				Adapter->sys_page_sz = 0x4000;
		} else {
			if (Adapter->sys_page_sz > iommu_pagesize)
				Adapter->sys_page_sz = iommu_pagesize;
		}
	}
	if (Adapter->lso_enable) {
		Adapter->dvma_page_num = E1000_LSO_MAXLEN /
		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
	} else {
		Adapter->dvma_page_num = Adapter->max_frame_size /
		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
	}
	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
#endif

	Adapter->min_frame_size = ETHERMIN + ETHERFCSL;

	if (Adapter->mem_workaround_82546 &&
	    ((mac->type == e1000_82545) ||
	    (mac->type == e1000_82546) ||
	    (mac->type == e1000_82546_rev_3))) {
		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
	} else {
		rx_size = Adapter->max_frame_size;
		if ((rx_size > FRAME_SIZE_UPTO_2K) &&
		    (rx_size <= FRAME_SIZE_UPTO_4K))
			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
		else if ((rx_size > FRAME_SIZE_UPTO_4K) &&
		    (rx_size <= FRAME_SIZE_UPTO_8K))
			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
		else if ((rx_size > FRAME_SIZE_UPTO_8K) &&
		    (rx_size <= FRAME_SIZE_UPTO_16K))
			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
		else
			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
	}
	Adapter->rx_buffer_size += E1000G_IPALIGNROOM;

	tx_size = Adapter->max_frame_size;
	if ((tx_size > FRAME_SIZE_UPTO_2K) && (tx_size <= FRAME_SIZE_UPTO_4K))
		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
	else if ((tx_size > FRAME_SIZE_UPTO_4K) &&
	    (tx_size <= FRAME_SIZE_UPTO_8K))
		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
	else if ((tx_size > FRAME_SIZE_UPTO_8K) &&
	    (tx_size <= FRAME_SIZE_UPTO_16K))
		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
	else
		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;

	/*
	 * For Wiseman adapters we have an requirement of having receive
	 * buffers aligned at 256 byte boundary. Since Livengood does not
	 * require this and forcing it for all hardwares will have
	 * performance implications, I am making it applicable only for
	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
	 * it is okay to have normal frames...but it does involve a
	 * potential risk where we may loose data if buffer is not
	 * aligned...so all wiseman boards to have 256 byte aligned
	 * buffers
	 */
	if (mac->type < e1000_82543)
		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
	else
		Adapter->rx_buf_align = 1;
}

/*
 * e1000g_detach - driver detach
 *
 * The detach() function is the complement of the attach routine.
 * If cmd is set to DDI_DETACH, detach() is used to remove  the
 * state  associated  with  a  given  instance of a device node
 * prior to the removal of that instance from the system.
 *
 * The detach() function will be called once for each  instance
 * of the device for which there has been a successful attach()
 * once there are no longer  any  opens  on  the  device.
 *
 * Interrupts routine are disabled, All memory allocated by this
 * driver are freed.
 */
static int
e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
{
	struct e1000g *Adapter;
	boolean_t rx_drain;

	switch (cmd) {
	default:
		return (DDI_FAILURE);

	case DDI_SUSPEND:
		return (e1000g_suspend(devinfo));

	case DDI_DETACH:
		break;
	}

	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
	if (Adapter == NULL)
		return (DDI_FAILURE);

	rx_drain = e1000g_rx_drain(Adapter);
	if (!rx_drain && !e1000g_force_detach)
		return (DDI_FAILURE);

	if (mac_unregister(Adapter->mh) != 0) {
		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
		return (DDI_FAILURE);
	}
	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;

	ASSERT(!(Adapter->e1000g_state & E1000G_STARTED));

	if (!e1000g_force_detach && !rx_drain)
		return (DDI_FAILURE);

	e1000g_unattach(devinfo, Adapter);

	return (DDI_SUCCESS);
}

/*
 * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
 */
void
e1000g_free_priv_devi_node(private_devi_list_t *devi_node)
{
	ASSERT(e1000g_private_devi_list != NULL);
	ASSERT(devi_node != NULL);

	if (devi_node->prev != NULL)
		devi_node->prev->next = devi_node->next;
	if (devi_node->next != NULL)
		devi_node->next->prev = devi_node->prev;
	if (devi_node == e1000g_private_devi_list)
		e1000g_private_devi_list = devi_node->next;

	kmem_free(devi_node->priv_dip,
	    sizeof (struct dev_info));
	kmem_free(devi_node,
	    sizeof (private_devi_list_t));
}

static void
e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
{
	private_devi_list_t *devi_node;
	int result;

	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
		(void) e1000g_disable_intrs(Adapter);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
		(void) mac_unregister(Adapter->mh);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
		(void) e1000g_rem_intrs(Adapter);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
		(void) ddi_prop_remove_all(devinfo);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
		stop_link_timer(Adapter);

		mutex_enter(&e1000g_nvm_lock);
		result = e1000_reset_hw(&Adapter->shared);
		mutex_exit(&e1000g_nvm_lock);

		if (result != E1000_SUCCESS) {
			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
		}
	}

	e1000g_release_multicast(Adapter);

	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
		if (Adapter->osdep.reg_handle != NULL)
			ddi_regs_map_free(&Adapter->osdep.reg_handle);
		if (Adapter->osdep.ich_flash_handle != NULL)
			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
		if (Adapter->osdep.io_reg_handle != NULL)
			ddi_regs_map_free(&Adapter->osdep.io_reg_handle);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
		if (Adapter->osdep.cfg_handle != NULL)
			pci_config_teardown(&Adapter->osdep.cfg_handle);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
		e1000g_destroy_locks(Adapter);
	}

	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
		e1000g_fm_fini(Adapter);
	}

	mutex_enter(&e1000g_rx_detach_lock);
	if (e1000g_force_detach && (Adapter->priv_devi_node != NULL)) {
		devi_node = Adapter->priv_devi_node;
		devi_node->flag |= E1000G_PRIV_DEVI_DETACH;

		if (devi_node->pending_rx_count == 0) {
			e1000g_free_priv_devi_node(devi_node);
		}
	}
	mutex_exit(&e1000g_rx_detach_lock);

	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));

	/*
	 * Another hotplug spec requirement,
	 * run ddi_set_driver_private(devinfo, null);
	 */
	ddi_set_driver_private(devinfo, NULL);
}

/*
 * Get the BAR type and rnumber for a given PCI BAR offset
 */
static int
e1000g_get_bar_info(dev_info_t *dip, int bar_offset, bar_info_t *bar_info)
{
	pci_regspec_t *regs;
	uint_t regs_length;
	int type, rnumber, rcount;

	ASSERT((bar_offset >= PCI_CONF_BASE0) &&
	    (bar_offset <= PCI_CONF_BASE5));

	/*
	 * Get the DDI "reg" property
	 */
	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
	    DDI_PROP_DONTPASS, "reg", (int **)&regs,
	    &regs_length) != DDI_PROP_SUCCESS) {
		return (DDI_FAILURE);
	}

	rcount = regs_length * sizeof (int) / sizeof (pci_regspec_t);
	/*
	 * Check the BAR offset
	 */
	for (rnumber = 0; rnumber < rcount; ++rnumber) {
		if (PCI_REG_REG_G(regs[rnumber].pci_phys_hi) == bar_offset) {
			type = regs[rnumber].pci_phys_hi & PCI_ADDR_MASK;
			break;
		}
	}

	ddi_prop_free(regs);

	if (rnumber >= rcount)
		return (DDI_FAILURE);

	switch (type) {
	case PCI_ADDR_CONFIG:
		bar_info->type = E1000G_BAR_CONFIG;
		break;
	case PCI_ADDR_IO:
		bar_info->type = E1000G_BAR_IO;
		break;
	case PCI_ADDR_MEM32:
		bar_info->type = E1000G_BAR_MEM32;
		break;
	case PCI_ADDR_MEM64:
		bar_info->type = E1000G_BAR_MEM64;
		break;
	default:
		return (DDI_FAILURE);
	}
	bar_info->rnumber = rnumber;
	return (DDI_SUCCESS);
}

static void
e1000g_init_locks(struct e1000g *Adapter)
{
	e1000g_tx_ring_t *tx_ring;
	e1000g_rx_ring_t *rx_ring;

	rw_init(&Adapter->chip_lock, NULL,
	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
	mutex_init(&Adapter->link_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
	mutex_init(&Adapter->watchdog_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));

	tx_ring = Adapter->tx_ring;

	mutex_init(&tx_ring->tx_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
	mutex_init(&tx_ring->usedlist_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
	mutex_init(&tx_ring->freelist_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));

	rx_ring = Adapter->rx_ring;

	mutex_init(&rx_ring->rx_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
}

static void
e1000g_destroy_locks(struct e1000g *Adapter)
{
	e1000g_tx_ring_t *tx_ring;
	e1000g_rx_ring_t *rx_ring;

	tx_ring = Adapter->tx_ring;
	mutex_destroy(&tx_ring->tx_lock);
	mutex_destroy(&tx_ring->usedlist_lock);
	mutex_destroy(&tx_ring->freelist_lock);

	rx_ring = Adapter->rx_ring;
	mutex_destroy(&rx_ring->rx_lock);

	mutex_destroy(&Adapter->link_lock);
	mutex_destroy(&Adapter->watchdog_lock);
	rw_destroy(&Adapter->chip_lock);

	/* destory mutex initialized in shared code */
	e1000_destroy_hw_mutex(&Adapter->shared);
}

static int
e1000g_resume(dev_info_t *devinfo)
{
	struct e1000g *Adapter;

	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
	if (Adapter == NULL)
		e1000g_log(Adapter, CE_PANIC,
		    "Instance pointer is null\n");

	if (Adapter->dip != devinfo)
		e1000g_log(Adapter, CE_PANIC,
		    "Devinfo is not the same as saved devinfo\n");

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_STARTED) {
		if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
			rw_exit(&Adapter->chip_lock);
			/*
			 * We note the failure, but return success, as the
			 * system is still usable without this controller.
			 */
			e1000g_log(Adapter, CE_WARN,
			    "e1000g_resume: failed to restart controller\n");
			return (DDI_SUCCESS);
		}
		/* Enable and start the watchdog timer */
		enable_watchdog_timer(Adapter);
	}

	Adapter->e1000g_state &= ~E1000G_SUSPENDED;

	rw_exit(&Adapter->chip_lock);

	return (DDI_SUCCESS);
}

static int
e1000g_suspend(dev_info_t *devinfo)
{
	struct e1000g *Adapter;

	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
	if (Adapter == NULL)
		return (DDI_FAILURE);

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	Adapter->e1000g_state |= E1000G_SUSPENDED;

	/* if the port isn't plumbed, we can simply return */
	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
		rw_exit(&Adapter->chip_lock);
		return (DDI_SUCCESS);
	}

	e1000g_stop(Adapter, B_FALSE);

	rw_exit(&Adapter->chip_lock);

	/* Disable and stop all the timers */
	disable_watchdog_timer(Adapter);
	stop_link_timer(Adapter);
	stop_82547_timer(Adapter->tx_ring);

	return (DDI_SUCCESS);
}

static int
e1000g_init(struct e1000g *Adapter)
{
	uint32_t pba;
	uint32_t high_water;
	struct e1000_hw *hw;
	clock_t link_timeout;
	int result;

	hw = &Adapter->shared;

	/*
	 * reset to put the hardware in a known state
	 * before we try to do anything with the eeprom
	 */
	mutex_enter(&e1000g_nvm_lock);
	result = e1000_reset_hw(hw);
	mutex_exit(&e1000g_nvm_lock);

	if (result != E1000_SUCCESS) {
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
		goto init_fail;
	}

	mutex_enter(&e1000g_nvm_lock);
	result = e1000_validate_nvm_checksum(hw);
	if (result < E1000_SUCCESS) {
		/*
		 * Some PCI-E parts fail the first check due to
		 * the link being in sleep state.  Call it again,
		 * if it fails a second time its a real issue.
		 */
		result = e1000_validate_nvm_checksum(hw);
	}
	mutex_exit(&e1000g_nvm_lock);

	if (result < E1000_SUCCESS) {
		e1000g_log(Adapter, CE_WARN,
		    "Invalid NVM checksum. Please contact "
		    "the vendor to update the NVM.");
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
		goto init_fail;
	}

	result = 0;
#ifdef __sparc
	/*
	 * First, we try to get the local ethernet address from OBP. If
	 * failed, then we get it from the EEPROM of NIC card.
	 */
	result = e1000g_find_mac_address(Adapter);
#endif
	/* Get the local ethernet address. */
	if (!result) {
		mutex_enter(&e1000g_nvm_lock);
		result = e1000_read_mac_addr(hw);
		mutex_exit(&e1000g_nvm_lock);
	}

	if (result < E1000_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
		goto init_fail;
	}

	/* check for valid mac address */
	if (!is_valid_mac_addr(hw->mac.addr)) {
		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
		goto init_fail;
	}

	/* Set LAA state for 82571 chipset */
	e1000_set_laa_state_82571(hw, B_TRUE);

	/* Master Latency Timer implementation */
	if (Adapter->master_latency_timer) {
		pci_config_put8(Adapter->osdep.cfg_handle,
		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
	}

	if (hw->mac.type < e1000_82547) {
		/*
		 * Total FIFO is 64K
		 */
		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
		else
			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
	} else if ((hw->mac.type == e1000_82571) ||
	    (hw->mac.type == e1000_82572) ||
	    (hw->mac.type == e1000_80003es2lan)) {
		/*
		 * Total FIFO is 48K
		 */
		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
		else
			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
	} else if (hw->mac.type == e1000_82573) {
		pba = E1000_PBA_20K;		/* 20K for Rx, 12K for Tx */
	} else if (hw->mac.type == e1000_82574) {
		/* Keep adapter default: 20K for Rx, 20K for Tx */
		pba = E1000_READ_REG(hw, E1000_PBA);
	} else if (hw->mac.type == e1000_ich8lan) {
		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
	} else if (hw->mac.type == e1000_ich9lan) {
		pba = E1000_PBA_10K;
	} else if (hw->mac.type == e1000_ich10lan) {
		pba = E1000_PBA_10K;
	} else if (hw->mac.type == e1000_pchlan) {
		pba = E1000_PBA_26K;
	} else if (hw->mac.type == e1000_pch2lan) {
		pba = E1000_PBA_26K;
	} else if (hw->mac.type == e1000_pch_lpt) {
		pba = E1000_PBA_26K;
	} else {
		/*
		 * Total FIFO is 40K
		 */
		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
		else
			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
	}
	E1000_WRITE_REG(hw, E1000_PBA, pba);

	/*
	 * These parameters set thresholds for the adapter's generation(Tx)
	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
	 * settings.  Flow control is enabled or disabled in the configuration
	 * file.
	 * High-water mark is set down from the top of the rx fifo (not
	 * sensitive to max_frame_size) and low-water is set just below
	 * high-water mark.
	 * The high water mark must be low enough to fit one full frame above
	 * it in the rx FIFO.  Should be the lower of:
	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
	 * Rx FIFO size minus one full frame.
	 */
	high_water = min(((pba << 10) * 9 / 10),
	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_82574 ||
	    hw->mac.type == e1000_ich9lan || hw->mac.type == e1000_ich10lan) ?
	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
	    ((pba << 10) - Adapter->max_frame_size)));

	hw->fc.high_water = high_water & 0xFFF8;
	hw->fc.low_water = hw->fc.high_water - 8;

	if (hw->mac.type == e1000_80003es2lan)
		hw->fc.pause_time = 0xFFFF;
	else
		hw->fc.pause_time = E1000_FC_PAUSE_TIME;
	hw->fc.send_xon = B_TRUE;

	/*
	 * Reset the adapter hardware the second time.
	 */
	mutex_enter(&e1000g_nvm_lock);
	result = e1000_reset_hw(hw);
	mutex_exit(&e1000g_nvm_lock);

	if (result != E1000_SUCCESS) {
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
		goto init_fail;
	}

	/* disable wakeup control by default */
	if (hw->mac.type >= e1000_82544)
		E1000_WRITE_REG(hw, E1000_WUC, 0);

	/*
	 * MWI should be disabled on 82546.
	 */
	if (hw->mac.type == e1000_82546)
		e1000_pci_clear_mwi(hw);
	else
		e1000_pci_set_mwi(hw);

	/*
	 * Configure/Initialize hardware
	 */
	mutex_enter(&e1000g_nvm_lock);
	result = e1000_init_hw(hw);
	mutex_exit(&e1000g_nvm_lock);

	if (result < E1000_SUCCESS) {
		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
		goto init_fail;
	}

	/*
	 * Restore LED settings to the default from EEPROM
	 * to meet the standard for Sun platforms.
	 */
	(void) e1000_cleanup_led(hw);

	/* Disable Smart Power Down */
	phy_spd_state(hw, B_FALSE);

	/* Make sure driver has control */
	e1000g_get_driver_control(hw);

	/*
	 * Initialize unicast addresses.
	 */
	e1000g_init_unicst(Adapter);

	/*
	 * Setup and initialize the mctable structures.  After this routine
	 * completes  Multicast table will be set
	 */
	e1000_update_mc_addr_list(hw,
	    (uint8_t *)Adapter->mcast_table, Adapter->mcast_count);
	msec_delay(5);

	/*
	 * Implement Adaptive IFS
	 */
	e1000_reset_adaptive(hw);

	/* Setup Interrupt Throttling Register */
	if (hw->mac.type >= e1000_82540) {
		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
	} else
		Adapter->intr_adaptive = B_FALSE;

	/* Start the timer for link setup */
	if (hw->mac.autoneg)
		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
	else
		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);

	mutex_enter(&Adapter->link_lock);
	if (hw->phy.autoneg_wait_to_complete) {
		Adapter->link_complete = B_TRUE;
	} else {
		Adapter->link_complete = B_FALSE;
		Adapter->link_tid = timeout(e1000g_link_timer,
		    (void *)Adapter, link_timeout);
	}
	mutex_exit(&Adapter->link_lock);

	/* Save the state of the phy */
	e1000g_get_phy_state(Adapter);

	e1000g_param_sync(Adapter);

	Adapter->init_count++;

	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
		goto init_fail;
	}
	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		goto init_fail;
	}

	Adapter->poll_mode = e1000g_poll_mode;

	return (DDI_SUCCESS);

init_fail:
	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
	return (DDI_FAILURE);
}

static int
e1000g_alloc_rx_data(struct e1000g *Adapter)
{
	e1000g_rx_ring_t *rx_ring;
	e1000g_rx_data_t *rx_data;

	rx_ring = Adapter->rx_ring;

	rx_data = kmem_zalloc(sizeof (e1000g_rx_data_t), KM_NOSLEEP);

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

	rx_data->priv_devi_node = Adapter->priv_devi_node;
	rx_data->rx_ring = rx_ring;

	mutex_init(&rx_data->freelist_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
	mutex_init(&rx_data->recycle_lock, NULL,
	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));

	rx_ring->rx_data = rx_data;

	return (DDI_SUCCESS);
}

void
e1000g_free_rx_pending_buffers(e1000g_rx_data_t *rx_data)
{
	rx_sw_packet_t *packet, *next_packet;

	if (rx_data == NULL)
		return;

	packet = rx_data->packet_area;
	while (packet != NULL) {
		next_packet = packet->next;
		e1000g_free_rx_sw_packet(packet, B_TRUE);
		packet = next_packet;
	}
	rx_data->packet_area = NULL;
}

void
e1000g_free_rx_data(e1000g_rx_data_t *rx_data)
{
	if (rx_data == NULL)
		return;

	mutex_destroy(&rx_data->freelist_lock);
	mutex_destroy(&rx_data->recycle_lock);

	kmem_free(rx_data, sizeof (e1000g_rx_data_t));
}

/*
 * Check if the link is up
 */
static boolean_t
e1000g_link_up(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;
	boolean_t link_up = B_FALSE;

	/*
	 * get_link_status is set in the interrupt handler on link-status-change
	 * or rx sequence error interrupt.  get_link_status will stay
	 * false until the e1000_check_for_link establishes link only
	 * for copper adapters.
	 */
	switch (hw->phy.media_type) {
	case e1000_media_type_copper:
		if (hw->mac.get_link_status) {
			(void) e1000_check_for_link(hw);
			if ((E1000_READ_REG(hw, E1000_STATUS) &
			    E1000_STATUS_LU)) {
				link_up = B_TRUE;
			} else {
				link_up = !hw->mac.get_link_status;
			}
		} else {
			link_up = B_TRUE;
		}
		break;
	case e1000_media_type_fiber:
		(void) e1000_check_for_link(hw);
		link_up = (E1000_READ_REG(hw, E1000_STATUS) &
		    E1000_STATUS_LU);
		break;
	case e1000_media_type_internal_serdes:
		(void) e1000_check_for_link(hw);
		link_up = hw->mac.serdes_has_link;
		break;
	}

	return (link_up);
}

static void
e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
{
	struct iocblk *iocp;
	struct e1000g *e1000gp;
	enum ioc_reply status;

	iocp = (struct iocblk *)(uintptr_t)mp->b_rptr;
	iocp->ioc_error = 0;
	e1000gp = (struct e1000g *)arg;

	ASSERT(e1000gp);
	if (e1000gp == NULL) {
		miocnak(q, mp, 0, EINVAL);
		return;
	}

	rw_enter(&e1000gp->chip_lock, RW_READER);
	if (e1000gp->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&e1000gp->chip_lock);
		miocnak(q, mp, 0, EINVAL);
		return;
	}
	rw_exit(&e1000gp->chip_lock);

	switch (iocp->ioc_cmd) {

	case LB_GET_INFO_SIZE:
	case LB_GET_INFO:
	case LB_GET_MODE:
	case LB_SET_MODE:
		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
		break;


#ifdef E1000G_DEBUG
	case E1000G_IOC_REG_PEEK:
	case E1000G_IOC_REG_POKE:
		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
		break;
	case E1000G_IOC_CHIP_RESET:
		e1000gp->reset_count++;
		if (e1000g_reset_adapter(e1000gp))
			status = IOC_ACK;
		else
			status = IOC_INVAL;
		break;
#endif
	default:
		status = IOC_INVAL;
		break;
	}

	/*
	 * Decide how to reply
	 */
	switch (status) {
	default:
	case IOC_INVAL:
		/*
		 * Error, reply with a NAK and EINVAL or the specified error
		 */
		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
		    EINVAL : iocp->ioc_error);
		break;

	case IOC_DONE:
		/*
		 * OK, reply already sent
		 */
		break;

	case IOC_ACK:
		/*
		 * OK, reply with an ACK
		 */
		miocack(q, mp, 0, 0);
		break;

	case IOC_REPLY:
		/*
		 * OK, send prepared reply as ACK or NAK
		 */
		mp->b_datap->db_type = iocp->ioc_error == 0 ?
		    M_IOCACK : M_IOCNAK;
		qreply(q, mp);
		break;
	}
}

/*
 * The default value of e1000g_poll_mode == 0 assumes that the NIC is
 * capable of supporting only one interrupt and we shouldn't disable
 * the physical interrupt. In this case we let the interrupt come and
 * we queue the packets in the rx ring itself in case we are in polling
 * mode (better latency but slightly lower performance and a very
 * high intrrupt count in mpstat which is harmless).
 *
 * e1000g_poll_mode == 1 assumes that we have per Rx ring interrupt
 * which can be disabled in poll mode. This gives better overall
 * throughput (compared to the mode above), shows very low interrupt
 * count but has slightly higher latency since we pick the packets when
 * the poll thread does polling.
 *
 * Currently, this flag should be enabled only while doing performance
 * measurement or when it can be guaranteed that entire NIC going
 * in poll mode will not harm any traffic like cluster heartbeat etc.
 */
int e1000g_poll_mode = 0;

/*
 * Called from the upper layers when driver is in polling mode to
 * pick up any queued packets. Care should be taken to not block
 * this thread.
 */
static mblk_t *e1000g_poll_ring(void *arg, int bytes_to_pickup)
{
	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)arg;
	mblk_t			*mp = NULL;
	mblk_t			*tail;
	struct e1000g 		*adapter;

	adapter = rx_ring->adapter;

	rw_enter(&adapter->chip_lock, RW_READER);

	if (adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&adapter->chip_lock);
		return (NULL);
	}

	mutex_enter(&rx_ring->rx_lock);
	mp = e1000g_receive(rx_ring, &tail, bytes_to_pickup);
	mutex_exit(&rx_ring->rx_lock);
	rw_exit(&adapter->chip_lock);
	return (mp);
}

static int
e1000g_m_start(void *arg)
{
	struct e1000g *Adapter = (struct e1000g *)arg;

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&Adapter->chip_lock);
		return (ECANCELED);
	}

	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
		rw_exit(&Adapter->chip_lock);
		return (ENOTACTIVE);
	}

	Adapter->e1000g_state |= E1000G_STARTED;

	rw_exit(&Adapter->chip_lock);

	/* Enable and start the watchdog timer */
	enable_watchdog_timer(Adapter);

	return (0);
}

static int
e1000g_start(struct e1000g *Adapter, boolean_t global)
{
	e1000g_rx_data_t *rx_data;

	if (global) {
		if (e1000g_alloc_rx_data(Adapter) != DDI_SUCCESS) {
			e1000g_log(Adapter, CE_WARN, "Allocate rx data failed");
			goto start_fail;
		}

		/* Allocate dma resources for descriptors and buffers */
		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
			e1000g_log(Adapter, CE_WARN,
			    "Alloc DMA resources failed");
			goto start_fail;
		}
		Adapter->rx_buffer_setup = B_FALSE;
	}

	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
		if (e1000g_init(Adapter) != DDI_SUCCESS) {
			e1000g_log(Adapter, CE_WARN,
			    "Adapter initialization failed");
			goto start_fail;
		}
	}

	/* Setup and initialize the transmit structures */
	e1000g_tx_setup(Adapter);
	msec_delay(5);

	/* Setup and initialize the receive structures */
	e1000g_rx_setup(Adapter);
	msec_delay(5);

	/* Restore the e1000g promiscuous mode */
	e1000g_restore_promisc(Adapter);

	e1000g_mask_interrupt(Adapter);

	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
		goto start_fail;
	}

	return (DDI_SUCCESS);

start_fail:
	rx_data = Adapter->rx_ring->rx_data;

	if (global) {
		e1000g_release_dma_resources(Adapter);
		e1000g_free_rx_pending_buffers(rx_data);
		e1000g_free_rx_data(rx_data);
	}

	mutex_enter(&e1000g_nvm_lock);
	(void) e1000_reset_hw(&Adapter->shared);
	mutex_exit(&e1000g_nvm_lock);

	return (DDI_FAILURE);
}

static void
e1000g_m_stop(void *arg)
{
	struct e1000g *Adapter = (struct e1000g *)arg;

	/* Drain tx sessions */
	(void) e1000g_tx_drain(Adapter);

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&Adapter->chip_lock);
		return;
	}
	Adapter->e1000g_state &= ~E1000G_STARTED;
	e1000g_stop(Adapter, B_TRUE);

	rw_exit(&Adapter->chip_lock);

	/* Disable and stop all the timers */
	disable_watchdog_timer(Adapter);
	stop_link_timer(Adapter);
	stop_82547_timer(Adapter->tx_ring);
}

static void
e1000g_stop(struct e1000g *Adapter, boolean_t global)
{
	private_devi_list_t *devi_node;
	e1000g_rx_data_t *rx_data;
	int result;

	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;

	/* Stop the chip and release pending resources */

	/* Tell firmware driver is no longer in control */
	e1000g_release_driver_control(&Adapter->shared);

	e1000g_clear_all_interrupts(Adapter);

	mutex_enter(&e1000g_nvm_lock);
	result = e1000_reset_hw(&Adapter->shared);
	mutex_exit(&e1000g_nvm_lock);

	if (result != E1000_SUCCESS) {
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
	}

	mutex_enter(&Adapter->link_lock);
	Adapter->link_complete = B_FALSE;
	mutex_exit(&Adapter->link_lock);

	/* Release resources still held by the TX descriptors */
	e1000g_tx_clean(Adapter);

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);

	/* Clean the pending rx jumbo packet fragment */
	e1000g_rx_clean(Adapter);

	if (global) {
		e1000g_release_dma_resources(Adapter);

		mutex_enter(&e1000g_rx_detach_lock);
		rx_data = Adapter->rx_ring->rx_data;
		rx_data->flag |= E1000G_RX_STOPPED;

		if (rx_data->pending_count == 0) {
			e1000g_free_rx_pending_buffers(rx_data);
			e1000g_free_rx_data(rx_data);
		} else {
			devi_node = rx_data->priv_devi_node;
			if (devi_node != NULL)
				atomic_inc_32(&devi_node->pending_rx_count);
			else
				atomic_inc_32(&Adapter->pending_rx_count);
		}
		mutex_exit(&e1000g_rx_detach_lock);
	}

	if (Adapter->link_state != LINK_STATE_UNKNOWN) {
		Adapter->link_state = LINK_STATE_UNKNOWN;
		if (!Adapter->reset_flag)
			mac_link_update(Adapter->mh, Adapter->link_state);
	}
}

static void
e1000g_rx_clean(struct e1000g *Adapter)
{
	e1000g_rx_data_t *rx_data = Adapter->rx_ring->rx_data;

	if (rx_data == NULL)
		return;

	if (rx_data->rx_mblk != NULL) {
		freemsg(rx_data->rx_mblk);
		rx_data->rx_mblk = NULL;
		rx_data->rx_mblk_tail = NULL;
		rx_data->rx_mblk_len = 0;
	}
}

static void
e1000g_tx_clean(struct e1000g *Adapter)
{
	e1000g_tx_ring_t *tx_ring;
	p_tx_sw_packet_t packet;
	mblk_t *mp;
	mblk_t *nmp;
	uint32_t packet_count;

	tx_ring = Adapter->tx_ring;

	/*
	 * Here we don't need to protect the lists using
	 * the usedlist_lock and freelist_lock, for they
	 * have been protected by the chip_lock.
	 */
	mp = NULL;
	nmp = NULL;
	packet_count = 0;
	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
	while (packet != NULL) {
		if (packet->mp != NULL) {
			/* Assemble the message chain */
			if (mp == NULL) {
				mp = packet->mp;
				nmp = packet->mp;
			} else {
				nmp->b_next = packet->mp;
				nmp = packet->mp;
			}
			/* Disconnect the message from the sw packet */
			packet->mp = NULL;
		}

		e1000g_free_tx_swpkt(packet);
		packet_count++;

		packet = (p_tx_sw_packet_t)
		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
	}

	if (mp != NULL)
		freemsgchain(mp);

	if (packet_count > 0) {
		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
		QUEUE_INIT_LIST(&tx_ring->used_list);

		/* Setup TX descriptor pointers */
		tx_ring->tbd_next = tx_ring->tbd_first;
		tx_ring->tbd_oldest = tx_ring->tbd_first;

		/* Setup our HW Tx Head & Tail descriptor pointers */
		E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
		E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
	}
}

static boolean_t
e1000g_tx_drain(struct e1000g *Adapter)
{
	int i;
	boolean_t done;
	e1000g_tx_ring_t *tx_ring;

	tx_ring = Adapter->tx_ring;

	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
	for (i = 0; i < TX_DRAIN_TIME; i++) {
		mutex_enter(&tx_ring->usedlist_lock);
		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
		mutex_exit(&tx_ring->usedlist_lock);

		if (done)
			break;

		msec_delay(1);
	}

	return (done);
}

static boolean_t
e1000g_rx_drain(struct e1000g *Adapter)
{
	int i;
	boolean_t done;

	/*
	 * Allow up to RX_DRAIN_TIME for pending received packets to complete.
	 */
	for (i = 0; i < RX_DRAIN_TIME; i++) {
		done = (Adapter->pending_rx_count == 0);

		if (done)
			break;

		msec_delay(1);
	}

	return (done);
}

static boolean_t
e1000g_reset_adapter(struct e1000g *Adapter)
{
	/* Disable and stop all the timers */
	disable_watchdog_timer(Adapter);
	stop_link_timer(Adapter);
	stop_82547_timer(Adapter->tx_ring);

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->stall_flag) {
		Adapter->stall_flag = B_FALSE;
		Adapter->reset_flag = B_TRUE;
	}

	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
		rw_exit(&Adapter->chip_lock);
		return (B_TRUE);
	}

	e1000g_stop(Adapter, B_FALSE);

	if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
		rw_exit(&Adapter->chip_lock);
		e1000g_log(Adapter, CE_WARN, "Reset failed");
			return (B_FALSE);
	}

	rw_exit(&Adapter->chip_lock);

	/* Enable and start the watchdog timer */
	enable_watchdog_timer(Adapter);

	return (B_TRUE);
}

boolean_t
e1000g_global_reset(struct e1000g *Adapter)
{
	/* Disable and stop all the timers */
	disable_watchdog_timer(Adapter);
	stop_link_timer(Adapter);
	stop_82547_timer(Adapter->tx_ring);

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	e1000g_stop(Adapter, B_TRUE);

	Adapter->init_count = 0;

	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
		rw_exit(&Adapter->chip_lock);
		e1000g_log(Adapter, CE_WARN, "Reset failed");
		return (B_FALSE);
	}

	rw_exit(&Adapter->chip_lock);

	/* Enable and start the watchdog timer */
	enable_watchdog_timer(Adapter);

	return (B_TRUE);
}

/*
 * e1000g_intr_pciexpress - ISR for PCI Express chipsets
 *
 * This interrupt service routine is for PCI-Express adapters.
 * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
 * bit is set.
 */
static uint_t
e1000g_intr_pciexpress(caddr_t arg)
{
	struct e1000g *Adapter;
	uint32_t icr;

	Adapter = (struct e1000g *)(uintptr_t)arg;
	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
		return (DDI_INTR_CLAIMED);
	}

	if (icr & E1000_ICR_INT_ASSERTED) {
		/*
		 * E1000_ICR_INT_ASSERTED bit was set:
		 * Read(Clear) the ICR, claim this interrupt,
		 * look for work to do.
		 */
		e1000g_intr_work(Adapter, icr);
		return (DDI_INTR_CLAIMED);
	} else {
		/*
		 * E1000_ICR_INT_ASSERTED bit was not set:
		 * Don't claim this interrupt, return immediately.
		 */
		return (DDI_INTR_UNCLAIMED);
	}
}

/*
 * e1000g_intr - ISR for PCI/PCI-X chipsets
 *
 * This interrupt service routine is for PCI/PCI-X adapters.
 * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
 * bit is set or not.
 */
static uint_t
e1000g_intr(caddr_t arg)
{
	struct e1000g *Adapter;
	uint32_t icr;

	Adapter = (struct e1000g *)(uintptr_t)arg;
	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
		return (DDI_INTR_CLAIMED);
	}

	if (icr) {
		/*
		 * Any bit was set in ICR:
		 * Read(Clear) the ICR, claim this interrupt,
		 * look for work to do.
		 */
		e1000g_intr_work(Adapter, icr);
		return (DDI_INTR_CLAIMED);
	} else {
		/*
		 * No bit was set in ICR:
		 * Don't claim this interrupt, return immediately.
		 */
		return (DDI_INTR_UNCLAIMED);
	}
}

/*
 * e1000g_intr_work - actual processing of ISR
 *
 * Read(clear) the ICR contents and call appropriate interrupt
 * processing routines.
 */
static void
e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
{
	struct e1000_hw *hw;
	hw = &Adapter->shared;
	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;

	Adapter->rx_pkt_cnt = 0;
	Adapter->tx_pkt_cnt = 0;

	rw_enter(&Adapter->chip_lock, RW_READER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&Adapter->chip_lock);
		return;
	}
	/*
	 * Here we need to check the "e1000g_state" flag within the chip_lock to
	 * ensure the receive routine will not execute when the adapter is
	 * being reset.
	 */
	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
		rw_exit(&Adapter->chip_lock);
		return;
	}

	if (icr & E1000_ICR_RXT0) {
		mblk_t			*mp = NULL;
		mblk_t			*tail = NULL;
		e1000g_rx_ring_t	*rx_ring;

		rx_ring = Adapter->rx_ring;
		mutex_enter(&rx_ring->rx_lock);
		/*
		 * Sometimes with legacy interrupts, it possible that
		 * there is a single interrupt for Rx/Tx. In which
		 * case, if poll flag is set, we shouldn't really
		 * be doing Rx processing.
		 */
		if (!rx_ring->poll_flag)
			mp = e1000g_receive(rx_ring, &tail,
			    E1000G_CHAIN_NO_LIMIT);
		mutex_exit(&rx_ring->rx_lock);
		rw_exit(&Adapter->chip_lock);
		if (mp != NULL)
			mac_rx_ring(Adapter->mh, rx_ring->mrh,
			    mp, rx_ring->ring_gen_num);
	} else
		rw_exit(&Adapter->chip_lock);

	if (icr & E1000_ICR_TXDW) {
		if (!Adapter->tx_intr_enable)
			e1000g_clear_tx_interrupt(Adapter);

		/* Recycle the tx descriptors */
		rw_enter(&Adapter->chip_lock, RW_READER);
		(void) e1000g_recycle(tx_ring);
		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
		rw_exit(&Adapter->chip_lock);

		if (tx_ring->resched_needed &&
		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
			tx_ring->resched_needed = B_FALSE;
			mac_tx_update(Adapter->mh);
			E1000G_STAT(tx_ring->stat_reschedule);
		}
	}

	/*
	 * The Receive Sequence errors RXSEQ and the link status change LSC
	 * are checked to detect that the cable has been pulled out. For
	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
	 * are an indication that cable is not connected.
	 */
	if ((icr & E1000_ICR_RXSEQ) ||
	    (icr & E1000_ICR_LSC) ||
	    (icr & E1000_ICR_GPI_EN1)) {
		boolean_t link_changed;
		timeout_id_t tid = 0;

		stop_watchdog_timer(Adapter);

		rw_enter(&Adapter->chip_lock, RW_WRITER);

		/*
		 * Because we got a link-status-change interrupt, force
		 * e1000_check_for_link() to look at phy
		 */
		Adapter->shared.mac.get_link_status = B_TRUE;

		/* e1000g_link_check takes care of link status change */
		link_changed = e1000g_link_check(Adapter);

		/* Get new phy state */
		e1000g_get_phy_state(Adapter);

		/*
		 * If the link timer has not timed out, we'll not notify
		 * the upper layer with any link state until the link is up.
		 */
		if (link_changed && !Adapter->link_complete) {
			if (Adapter->link_state == LINK_STATE_UP) {
				mutex_enter(&Adapter->link_lock);
				Adapter->link_complete = B_TRUE;
				tid = Adapter->link_tid;
				Adapter->link_tid = 0;
				mutex_exit(&Adapter->link_lock);
			} else {
				link_changed = B_FALSE;
			}
		}
		rw_exit(&Adapter->chip_lock);

		if (link_changed) {
			if (tid != 0)
				(void) untimeout(tid);

			/*
			 * Workaround for esb2. Data stuck in fifo on a link
			 * down event. Stop receiver here and reset in watchdog.
			 */
			if ((Adapter->link_state == LINK_STATE_DOWN) &&
			    (Adapter->shared.mac.type == e1000_80003es2lan)) {
				uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
				E1000_WRITE_REG(hw, E1000_RCTL,
				    rctl & ~E1000_RCTL_EN);
				e1000g_log(Adapter, CE_WARN,
				    "ESB2 receiver disabled");
				Adapter->esb2_workaround = B_TRUE;
			}
			if (!Adapter->reset_flag)
				mac_link_update(Adapter->mh,
				    Adapter->link_state);
			if (Adapter->link_state == LINK_STATE_UP)
				Adapter->reset_flag = B_FALSE;
		}

		start_watchdog_timer(Adapter);
	}
}

static void
e1000g_init_unicst(struct e1000g *Adapter)
{
	struct e1000_hw *hw;
	int slot;

	hw = &Adapter->shared;

	if (Adapter->init_count == 0) {
		/* Initialize the multiple unicast addresses */
		Adapter->unicst_total = min(hw->mac.rar_entry_count,
		    MAX_NUM_UNICAST_ADDRESSES);

		/*
		 * The common code does not correctly calculate the number of
		 * rar's that could be reserved by firmware for the pch_lpt
		 * macs. The interface has one primary rar, and 11 additional
		 * ones. Those 11 additional ones are not always available.
		 * According to the datasheet, we need to check a few of the
		 * bits set in the FWSM register. If the value is zero,
		 * everything is available. If the value is 1, none of the
		 * additional registers are availabl.e If the value is 2-7, only
		 * that number are available.
		 */
		if (hw->mac.type == e1000_pch_lpt) {
			uint32_t locked, rar;

			locked = E1000_READ_REG(hw, E1000_FWSM) &
			    E1000_FWSM_WLOCK_MAC_MASK;
			locked >>= E1000_FWSM_WLOCK_MAC_SHIFT;
			rar = 1;
			if (locked == 0)
				rar += 11;
			else if (locked == 1)
				rar += 0;
			else
				rar += locked;
			Adapter->unicst_total = min(rar,
			    MAX_NUM_UNICAST_ADDRESSES);
		}

		/* Workaround for an erratum of 82571 chipst */
		if ((hw->mac.type == e1000_82571) &&
		    (e1000_get_laa_state_82571(hw) == B_TRUE))
			Adapter->unicst_total--;

		/* VMware doesn't support multiple mac addresses properly */
		if (hw->subsystem_vendor_id == 0x15ad)
			Adapter->unicst_total = 1;

		Adapter->unicst_avail = Adapter->unicst_total;

		for (slot = 0; slot < Adapter->unicst_total; slot++) {
			/* Clear both the flag and MAC address */
			Adapter->unicst_addr[slot].reg.high = 0;
			Adapter->unicst_addr[slot].reg.low = 0;
		}
	} else {
		/* Workaround for an erratum of 82571 chipst */
		if ((hw->mac.type == e1000_82571) &&
		    (e1000_get_laa_state_82571(hw) == B_TRUE))
			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);

		/* Re-configure the RAR registers */
		for (slot = 0; slot < Adapter->unicst_total; slot++)
			if (Adapter->unicst_addr[slot].mac.set == 1)
				e1000_rar_set(hw,
				    Adapter->unicst_addr[slot].mac.addr, slot);
	}

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
}

static int
e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
    int slot)
{
	struct e1000_hw *hw;

	hw = &Adapter->shared;

	/*
	 * The first revision of Wiseman silicon (rev 2.0) has an errata
	 * that requires the receiver to be in reset when any of the
	 * receive address registers (RAR regs) are accessed.  The first
	 * rev of Wiseman silicon also requires MWI to be disabled when
	 * a global reset or a receive reset is issued.  So before we
	 * initialize the RARs, we check the rev of the Wiseman controller
	 * and work around any necessary HW errata.
	 */
	if ((hw->mac.type == e1000_82542) &&
	    (hw->revision_id == E1000_REVISION_2)) {
		e1000_pci_clear_mwi(hw);
		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
		msec_delay(5);
	}
	if (mac_addr == NULL) {
		E1000_WRITE_REG_ARRAY(hw, E1000_RA, slot << 1, 0);
		E1000_WRITE_FLUSH(hw);
		E1000_WRITE_REG_ARRAY(hw, E1000_RA, (slot << 1) + 1, 0);
		E1000_WRITE_FLUSH(hw);
		/* Clear both the flag and MAC address */
		Adapter->unicst_addr[slot].reg.high = 0;
		Adapter->unicst_addr[slot].reg.low = 0;
	} else {
		bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr,
		    ETHERADDRL);
		e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
		Adapter->unicst_addr[slot].mac.set = 1;
	}

	/* Workaround for an erratum of 82571 chipst */
	if (slot == 0) {
		if ((hw->mac.type == e1000_82571) &&
		    (e1000_get_laa_state_82571(hw) == B_TRUE))
			if (mac_addr == NULL) {
				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
				    slot << 1, 0);
				E1000_WRITE_FLUSH(hw);
				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
				    (slot << 1) + 1, 0);
				E1000_WRITE_FLUSH(hw);
			} else {
				e1000_rar_set(hw, (uint8_t *)mac_addr,
				    LAST_RAR_ENTRY);
			}
	}

	/*
	 * If we are using Wiseman rev 2.0 silicon, we will have previously
	 * put the receive in reset, and disabled MWI, to work around some
	 * HW errata.  Now we should take the receiver out of reset, and
	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
	 */
	if ((hw->mac.type == e1000_82542) &&
	    (hw->revision_id == E1000_REVISION_2)) {
		E1000_WRITE_REG(hw, E1000_RCTL, 0);
		msec_delay(1);
		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
			e1000_pci_set_mwi(hw);
		e1000g_rx_setup(Adapter);
	}

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
		return (EIO);
	}

	return (0);
}

static int
multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
{
	struct e1000_hw *hw = &Adapter->shared;
	struct ether_addr *newtable;
	size_t new_len;
	size_t old_len;
	int res = 0;

	if ((multiaddr[0] & 01) == 0) {
		res = EINVAL;
		e1000g_log(Adapter, CE_WARN, "Illegal multicast address");
		goto done;
	}

	if (Adapter->mcast_count >= Adapter->mcast_max_num) {
		res = ENOENT;
		e1000g_log(Adapter, CE_WARN,
		    "Adapter requested more than %d mcast addresses",
		    Adapter->mcast_max_num);
		goto done;
	}


	if (Adapter->mcast_count == Adapter->mcast_alloc_count) {
		old_len = Adapter->mcast_alloc_count *
		    sizeof (struct ether_addr);
		new_len = (Adapter->mcast_alloc_count + MCAST_ALLOC_SIZE) *
		    sizeof (struct ether_addr);

		newtable = kmem_alloc(new_len, KM_NOSLEEP);
		if (newtable == NULL) {
			res = ENOMEM;
			e1000g_log(Adapter, CE_WARN,
			    "Not enough memory to alloc mcast table");
			goto done;
		}

		if (Adapter->mcast_table != NULL) {
			bcopy(Adapter->mcast_table, newtable, old_len);
			kmem_free(Adapter->mcast_table, old_len);
		}
		Adapter->mcast_alloc_count += MCAST_ALLOC_SIZE;
		Adapter->mcast_table = newtable;
	}

	bcopy(multiaddr,
	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
	Adapter->mcast_count++;

	/*
	 * Update the MC table in the hardware
	 */
	e1000g_clear_interrupt(Adapter);

	e1000_update_mc_addr_list(hw,
	    (uint8_t *)Adapter->mcast_table, Adapter->mcast_count);

	e1000g_mask_interrupt(Adapter);

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
		res = EIO;
	}

done:
	return (res);
}

static int
multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
{
	struct e1000_hw *hw = &Adapter->shared;
	struct ether_addr *newtable;
	size_t new_len;
	size_t old_len;
	unsigned i;

	for (i = 0; i < Adapter->mcast_count; i++) {
		if (bcmp(multiaddr, &Adapter->mcast_table[i],
		    ETHERADDRL) == 0) {
			for (i++; i < Adapter->mcast_count; i++) {
				Adapter->mcast_table[i - 1] =
				    Adapter->mcast_table[i];
			}
			Adapter->mcast_count--;
			break;
		}
	}

	if ((Adapter->mcast_alloc_count - Adapter->mcast_count) >
	    MCAST_ALLOC_SIZE) {
		old_len = Adapter->mcast_alloc_count *
		    sizeof (struct ether_addr);
		new_len = (Adapter->mcast_alloc_count - MCAST_ALLOC_SIZE) *
		    sizeof (struct ether_addr);

		newtable = kmem_alloc(new_len, KM_NOSLEEP);
		if (newtable != NULL) {
			bcopy(Adapter->mcast_table, newtable, new_len);
			kmem_free(Adapter->mcast_table, old_len);

			Adapter->mcast_alloc_count -= MCAST_ALLOC_SIZE;
			Adapter->mcast_table = newtable;
		}
	}

	/*
	 * Update the MC table in the hardware
	 */
	e1000g_clear_interrupt(Adapter);

	e1000_update_mc_addr_list(hw,
	    (uint8_t *)Adapter->mcast_table, Adapter->mcast_count);

	e1000g_mask_interrupt(Adapter);

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
		return (EIO);
	}

	return (0);
}

static void
e1000g_release_multicast(struct e1000g *Adapter)
{
	if (Adapter->mcast_table != NULL) {
		kmem_free(Adapter->mcast_table,
		    Adapter->mcast_alloc_count * sizeof (struct ether_addr));
		Adapter->mcast_table = NULL;
	}
}

int
e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
{
	struct e1000g *Adapter = (struct e1000g *)arg;
	int result;

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		result = ECANCELED;
		goto done;
	}

	result = (add) ? multicst_add(Adapter, addr)
	    : multicst_remove(Adapter, addr);

done:
	rw_exit(&Adapter->chip_lock);
	return (result);

}

int
e1000g_m_promisc(void *arg, boolean_t on)
{
	struct e1000g *Adapter = (struct e1000g *)arg;
	uint32_t rctl;

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&Adapter->chip_lock);
		return (ECANCELED);
	}

	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);

	if (on)
		rctl |=
		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
	else
		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));

	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);

	Adapter->e1000g_promisc = on;

	rw_exit(&Adapter->chip_lock);

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
		return (EIO);
	}

	return (0);
}

/*
 * Entry points to enable and disable interrupts at the granularity of
 * a group.
 * Turns the poll_mode for the whole adapter on and off to enable or
 * override the ring level polling control over the hardware interrupts.
 */
static int
e1000g_rx_group_intr_enable(mac_intr_handle_t arg)
{
	struct e1000g		*adapter = (struct e1000g *)arg;
	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;

	/*
	 * Later interrupts at the granularity of the this ring will
	 * invoke mac_rx() with NULL, indicating the need for another
	 * software classification.
	 * We have a single ring usable per adapter now, so we only need to
	 * reset the rx handle for that one.
	 * When more RX rings can be used, we should update each one of them.
	 */
	mutex_enter(&rx_ring->rx_lock);
	rx_ring->mrh = NULL;
	adapter->poll_mode = B_FALSE;
	mutex_exit(&rx_ring->rx_lock);
	return (0);
}

static int
e1000g_rx_group_intr_disable(mac_intr_handle_t arg)
{
	struct e1000g *adapter = (struct e1000g *)arg;
	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;

	mutex_enter(&rx_ring->rx_lock);

	/*
	 * Later interrupts at the granularity of the this ring will
	 * invoke mac_rx() with the handle for this ring;
	 */
	adapter->poll_mode = B_TRUE;
	rx_ring->mrh = rx_ring->mrh_init;
	mutex_exit(&rx_ring->rx_lock);
	return (0);
}

/*
 * Entry points to enable and disable interrupts at the granularity of
 * a ring.
 * adapter poll_mode controls whether we actually proceed with hardware
 * interrupt toggling.
 */
static int
e1000g_rx_ring_intr_enable(mac_intr_handle_t intrh)
{
	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
	struct e1000g 		*adapter = rx_ring->adapter;
	struct e1000_hw 	*hw = &adapter->shared;
	uint32_t		intr_mask;

	rw_enter(&adapter->chip_lock, RW_READER);

	if (adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&adapter->chip_lock);
		return (0);
	}

	mutex_enter(&rx_ring->rx_lock);
	rx_ring->poll_flag = 0;
	mutex_exit(&rx_ring->rx_lock);

	/* Rx interrupt enabling for MSI and legacy */
	intr_mask = E1000_READ_REG(hw, E1000_IMS);
	intr_mask |= E1000_IMS_RXT0;
	E1000_WRITE_REG(hw, E1000_IMS, intr_mask);
	E1000_WRITE_FLUSH(hw);

	/* Trigger a Rx interrupt to check Rx ring */
	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
	E1000_WRITE_FLUSH(hw);

	rw_exit(&adapter->chip_lock);
	return (0);
}

static int
e1000g_rx_ring_intr_disable(mac_intr_handle_t intrh)
{
	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
	struct e1000g 		*adapter = rx_ring->adapter;
	struct e1000_hw 	*hw = &adapter->shared;

	rw_enter(&adapter->chip_lock, RW_READER);

	if (adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&adapter->chip_lock);
		return (0);
	}
	mutex_enter(&rx_ring->rx_lock);
	rx_ring->poll_flag = 1;
	mutex_exit(&rx_ring->rx_lock);

	/* Rx interrupt disabling for MSI and legacy */
	E1000_WRITE_REG(hw, E1000_IMC, E1000_IMS_RXT0);
	E1000_WRITE_FLUSH(hw);

	rw_exit(&adapter->chip_lock);
	return (0);
}

/*
 * e1000g_unicst_find - Find the slot for the specified unicast address
 */
static int
e1000g_unicst_find(struct e1000g *Adapter, const uint8_t *mac_addr)
{
	int slot;

	for (slot = 0; slot < Adapter->unicst_total; slot++) {
		if ((Adapter->unicst_addr[slot].mac.set == 1) &&
		    (bcmp(Adapter->unicst_addr[slot].mac.addr,
		    mac_addr, ETHERADDRL) == 0))
				return (slot);
	}

	return (-1);
}

/*
 * Entry points to add and remove a MAC address to a ring group.
 * The caller takes care of adding and removing the MAC addresses
 * to the filter via these two routines.
 */

static int
e1000g_addmac(void *arg, const uint8_t *mac_addr)
{
	struct e1000g *Adapter = (struct e1000g *)arg;
	int slot, err;

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&Adapter->chip_lock);
		return (ECANCELED);
	}

	if (e1000g_unicst_find(Adapter, mac_addr) != -1) {
		/* The same address is already in slot */
		rw_exit(&Adapter->chip_lock);
		return (0);
	}

	if (Adapter->unicst_avail == 0) {
		/* no slots available */
		rw_exit(&Adapter->chip_lock);
		return (ENOSPC);
	}

	/* Search for a free slot */
	for (slot = 0; slot < Adapter->unicst_total; slot++) {
		if (Adapter->unicst_addr[slot].mac.set == 0)
			break;
	}
	ASSERT(slot < Adapter->unicst_total);

	err = e1000g_unicst_set(Adapter, mac_addr, slot);
	if (err == 0)
		Adapter->unicst_avail--;

	rw_exit(&Adapter->chip_lock);

	return (err);
}

static int
e1000g_remmac(void *arg, const uint8_t *mac_addr)
{
	struct e1000g *Adapter = (struct e1000g *)arg;
	int slot, err;

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&Adapter->chip_lock);
		return (ECANCELED);
	}

	slot = e1000g_unicst_find(Adapter, mac_addr);
	if (slot == -1) {
		rw_exit(&Adapter->chip_lock);
		return (EINVAL);
	}

	ASSERT(Adapter->unicst_addr[slot].mac.set);

	/* Clear this slot */
	err = e1000g_unicst_set(Adapter, NULL, slot);
	if (err == 0)
		Adapter->unicst_avail++;

	rw_exit(&Adapter->chip_lock);

	return (err);
}

static int
e1000g_ring_start(mac_ring_driver_t rh, uint64_t mr_gen_num)
{
	e1000g_rx_ring_t *rx_ring = (e1000g_rx_ring_t *)rh;

	mutex_enter(&rx_ring->rx_lock);
	rx_ring->ring_gen_num = mr_gen_num;
	mutex_exit(&rx_ring->rx_lock);
	return (0);
}

/*
 * Callback funtion for MAC layer to register all rings.
 *
 * The hardware supports a single group with currently only one ring
 * available.
 * Though not offering virtualization ability per se, exposing the
 * group/ring still enables the polling and interrupt toggling.
 */
/* ARGSUSED */
void
e1000g_fill_ring(void *arg, mac_ring_type_t rtype, const int grp_index,
    const int ring_index, mac_ring_info_t *infop, mac_ring_handle_t rh)
{
	struct e1000g *Adapter = (struct e1000g *)arg;
	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
	mac_intr_t *mintr;

	/*
	 * We advertised only RX group/rings, so the MAC framework shouldn't
	 * ask for any thing else.
	 */
	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0 && ring_index == 0);

	rx_ring->mrh = rx_ring->mrh_init = rh;
	infop->mri_driver = (mac_ring_driver_t)rx_ring;
	infop->mri_start = e1000g_ring_start;
	infop->mri_stop = NULL;
	infop->mri_poll = e1000g_poll_ring;
	infop->mri_stat = e1000g_rx_ring_stat;

	/* Ring level interrupts */
	mintr = &infop->mri_intr;
	mintr->mi_handle = (mac_intr_handle_t)rx_ring;
	mintr->mi_enable = e1000g_rx_ring_intr_enable;
	mintr->mi_disable = e1000g_rx_ring_intr_disable;
	if (Adapter->msi_enable)
		mintr->mi_ddi_handle = Adapter->htable[0];
}

/* ARGSUSED */
static void
e1000g_fill_group(void *arg, mac_ring_type_t rtype, const int grp_index,
    mac_group_info_t *infop, mac_group_handle_t gh)
{
	struct e1000g *Adapter = (struct e1000g *)arg;
	mac_intr_t *mintr;

	/*
	 * We advertised a single RX ring. Getting a request for anything else
	 * signifies a bug in the MAC framework.
	 */
	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0);

	Adapter->rx_group = gh;

	infop->mgi_driver = (mac_group_driver_t)Adapter;
	infop->mgi_start = NULL;
	infop->mgi_stop = NULL;
	infop->mgi_addmac = e1000g_addmac;
	infop->mgi_remmac = e1000g_remmac;
	infop->mgi_count = 1;

	/* Group level interrupts */
	mintr = &infop->mgi_intr;
	mintr->mi_handle = (mac_intr_handle_t)Adapter;
	mintr->mi_enable = e1000g_rx_group_intr_enable;
	mintr->mi_disable = e1000g_rx_group_intr_disable;
}

static boolean_t
e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
{
	struct e1000g *Adapter = (struct e1000g *)arg;

	switch (cap) {
	case MAC_CAPAB_HCKSUM: {
		uint32_t *txflags = cap_data;

		if (Adapter->tx_hcksum_enable)
			*txflags = HCKSUM_IPHDRCKSUM |
			    HCKSUM_INET_PARTIAL;
		else
			return (B_FALSE);
		break;
	}

	case MAC_CAPAB_LSO: {
		mac_capab_lso_t *cap_lso = cap_data;

		if (Adapter->lso_enable) {
			cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
			cap_lso->lso_basic_tcp_ipv4.lso_max =
			    E1000_LSO_MAXLEN;
		} else
			return (B_FALSE);
		break;
	}
	case MAC_CAPAB_RINGS: {
		mac_capab_rings_t *cap_rings = cap_data;

		/* No TX rings exposed yet */
		if (cap_rings->mr_type != MAC_RING_TYPE_RX)
			return (B_FALSE);

		cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
		cap_rings->mr_rnum = 1;
		cap_rings->mr_gnum = 1;
		cap_rings->mr_rget = e1000g_fill_ring;
		cap_rings->mr_gget = e1000g_fill_group;
		break;
	}
	default:
		return (B_FALSE);
	}
	return (B_TRUE);
}

static boolean_t
e1000g_param_locked(mac_prop_id_t pr_num)
{
	/*
	 * All en_* parameters are locked (read-only) while
	 * the device is in any sort of loopback mode ...
	 */
	switch (pr_num) {
		case MAC_PROP_EN_1000FDX_CAP:
		case MAC_PROP_EN_1000HDX_CAP:
		case MAC_PROP_EN_100FDX_CAP:
		case MAC_PROP_EN_100HDX_CAP:
		case MAC_PROP_EN_10FDX_CAP:
		case MAC_PROP_EN_10HDX_CAP:
		case MAC_PROP_AUTONEG:
		case MAC_PROP_FLOWCTRL:
			return (B_TRUE);
	}
	return (B_FALSE);
}

/*
 * callback function for set/get of properties
 */
static int
e1000g_m_setprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
    uint_t pr_valsize, const void *pr_val)
{
	struct e1000g *Adapter = arg;
	struct e1000_hw *hw = &Adapter->shared;
	struct e1000_fc_info *fc = &Adapter->shared.fc;
	int err = 0;
	link_flowctrl_t flowctrl;
	uint32_t cur_mtu, new_mtu;

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
		rw_exit(&Adapter->chip_lock);
		return (ECANCELED);
	}

	if (Adapter->loopback_mode != E1000G_LB_NONE &&
	    e1000g_param_locked(pr_num)) {
		/*
		 * All en_* parameters are locked (read-only)
		 * while the device is in any sort of loopback mode.
		 */
		rw_exit(&Adapter->chip_lock);
		return (EBUSY);
	}

	switch (pr_num) {
		case MAC_PROP_EN_1000FDX_CAP:
			if (hw->phy.media_type != e1000_media_type_copper) {
				err = ENOTSUP;
				break;
			}
			Adapter->param_en_1000fdx = *(uint8_t *)pr_val;
			Adapter->param_adv_1000fdx = *(uint8_t *)pr_val;
			goto reset;
		case MAC_PROP_EN_100FDX_CAP:
			if (hw->phy.media_type != e1000_media_type_copper) {
				err = ENOTSUP;
				break;
			}
			Adapter->param_en_100fdx = *(uint8_t *)pr_val;
			Adapter->param_adv_100fdx = *(uint8_t *)pr_val;
			goto reset;
		case MAC_PROP_EN_100HDX_CAP:
			if (hw->phy.media_type != e1000_media_type_copper) {
				err = ENOTSUP;
				break;
			}
			Adapter->param_en_100hdx = *(uint8_t *)pr_val;
			Adapter->param_adv_100hdx = *(uint8_t *)pr_val;
			goto reset;
		case MAC_PROP_EN_10FDX_CAP:
			if (hw->phy.media_type != e1000_media_type_copper) {
				err = ENOTSUP;
				break;
			}
			Adapter->param_en_10fdx = *(uint8_t *)pr_val;
			Adapter->param_adv_10fdx = *(uint8_t *)pr_val;
			goto reset;
		case MAC_PROP_EN_10HDX_CAP:
			if (hw->phy.media_type != e1000_media_type_copper) {
				err = ENOTSUP;
				break;
			}
			Adapter->param_en_10hdx = *(uint8_t *)pr_val;
			Adapter->param_adv_10hdx = *(uint8_t *)pr_val;
			goto reset;
		case MAC_PROP_AUTONEG:
			if (hw->phy.media_type != e1000_media_type_copper) {
				err = ENOTSUP;
				break;
			}
			Adapter->param_adv_autoneg = *(uint8_t *)pr_val;
			goto reset;
		case MAC_PROP_FLOWCTRL:
			fc->send_xon = B_TRUE;
			bcopy(pr_val, &flowctrl, sizeof (flowctrl));

			switch (flowctrl) {
			default:
				err = EINVAL;
				break;
			case LINK_FLOWCTRL_NONE:
				fc->requested_mode = e1000_fc_none;
				break;
			case LINK_FLOWCTRL_RX:
				fc->requested_mode = e1000_fc_rx_pause;
				break;
			case LINK_FLOWCTRL_TX:
				fc->requested_mode = e1000_fc_tx_pause;
				break;
			case LINK_FLOWCTRL_BI:
				fc->requested_mode = e1000_fc_full;
				break;
			}
reset:
			if (err == 0) {
				/* check PCH limits & reset the link */
				e1000g_pch_limits(Adapter);
				if (e1000g_reset_link(Adapter) != DDI_SUCCESS)
					err = EINVAL;
			}
			break;
		case MAC_PROP_ADV_1000FDX_CAP:
		case MAC_PROP_ADV_1000HDX_CAP:
		case MAC_PROP_ADV_100FDX_CAP:
		case MAC_PROP_ADV_100HDX_CAP:
		case MAC_PROP_ADV_10FDX_CAP:
		case MAC_PROP_ADV_10HDX_CAP:
		case MAC_PROP_EN_1000HDX_CAP:
		case MAC_PROP_STATUS:
		case MAC_PROP_SPEED:
		case MAC_PROP_DUPLEX:
			err = ENOTSUP; /* read-only prop. Can't set this. */
			break;
		case MAC_PROP_MTU:
			/* adapter must be stopped for an MTU change */
			if (Adapter->e1000g_state & E1000G_STARTED) {
				err = EBUSY;
				break;
			}

			cur_mtu = Adapter->default_mtu;

			/* get new requested MTU */
			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
			if (new_mtu == cur_mtu) {
				err = 0;
				break;
			}

			if ((new_mtu < DEFAULT_MTU) ||
			    (new_mtu > Adapter->max_mtu)) {
				err = EINVAL;
				break;
			}

			/* inform MAC framework of new MTU */
			err = mac_maxsdu_update(Adapter->mh, new_mtu);

			if (err == 0) {
				Adapter->default_mtu = new_mtu;
				Adapter->max_frame_size =
				    e1000g_mtu2maxframe(new_mtu);

				/*
				 * check PCH limits & set buffer sizes to
				 * match new MTU
				 */
				e1000g_pch_limits(Adapter);
				e1000g_set_bufsize(Adapter);

				/*
				 * decrease the number of descriptors and free
				 * packets for jumbo frames to reduce tx/rx
				 * resource consumption
				 */
				if (Adapter->max_frame_size >=
				    (FRAME_SIZE_UPTO_4K)) {
					if (Adapter->tx_desc_num_flag == 0)
						Adapter->tx_desc_num =
						    DEFAULT_JUMBO_NUM_TX_DESC;

					if (Adapter->rx_desc_num_flag == 0)
						Adapter->rx_desc_num =
						    DEFAULT_JUMBO_NUM_RX_DESC;

					if (Adapter->tx_buf_num_flag == 0)
						Adapter->tx_freelist_num =
						    DEFAULT_JUMBO_NUM_TX_BUF;

					if (Adapter->rx_buf_num_flag == 0)
						Adapter->rx_freelist_limit =
						    DEFAULT_JUMBO_NUM_RX_BUF;
				} else {
					if (Adapter->tx_desc_num_flag == 0)
						Adapter->tx_desc_num =
						    DEFAULT_NUM_TX_DESCRIPTOR;

					if (Adapter->rx_desc_num_flag == 0)
						Adapter->rx_desc_num =
						    DEFAULT_NUM_RX_DESCRIPTOR;

					if (Adapter->tx_buf_num_flag == 0)
						Adapter->tx_freelist_num =
						    DEFAULT_NUM_TX_FREELIST;

					if (Adapter->rx_buf_num_flag == 0)
						Adapter->rx_freelist_limit =
						    DEFAULT_NUM_RX_FREELIST;
				}
			}
			break;
		case MAC_PROP_PRIVATE:
			err = e1000g_set_priv_prop(Adapter, pr_name,
			    pr_valsize, pr_val);
			break;
		default:
			err = ENOTSUP;
			break;
	}
	rw_exit(&Adapter->chip_lock);
	return (err);
}

static int
e1000g_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
    uint_t pr_valsize, void *pr_val)
{
	struct e1000g *Adapter = arg;
	struct e1000_fc_info *fc = &Adapter->shared.fc;
	int err = 0;
	link_flowctrl_t flowctrl;
	uint64_t tmp = 0;

	switch (pr_num) {
		case MAC_PROP_DUPLEX:
			ASSERT(pr_valsize >= sizeof (link_duplex_t));
			bcopy(&Adapter->link_duplex, pr_val,
			    sizeof (link_duplex_t));
			break;
		case MAC_PROP_SPEED:
			ASSERT(pr_valsize >= sizeof (uint64_t));
			tmp = Adapter->link_speed * 1000000ull;
			bcopy(&tmp, pr_val, sizeof (tmp));
			break;
		case MAC_PROP_AUTONEG:
			*(uint8_t *)pr_val = Adapter->param_adv_autoneg;
			break;
		case MAC_PROP_FLOWCTRL:
			ASSERT(pr_valsize >= sizeof (link_flowctrl_t));
			switch (fc->current_mode) {
				case e1000_fc_none:
					flowctrl = LINK_FLOWCTRL_NONE;
					break;
				case e1000_fc_rx_pause:
					flowctrl = LINK_FLOWCTRL_RX;
					break;
				case e1000_fc_tx_pause:
					flowctrl = LINK_FLOWCTRL_TX;
					break;
				case e1000_fc_full:
					flowctrl = LINK_FLOWCTRL_BI;
					break;
			}
			bcopy(&flowctrl, pr_val, sizeof (flowctrl));
			break;
		case MAC_PROP_ADV_1000FDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_adv_1000fdx;
			break;
		case MAC_PROP_EN_1000FDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_en_1000fdx;
			break;
		case MAC_PROP_ADV_1000HDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_adv_1000hdx;
			break;
		case MAC_PROP_EN_1000HDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_en_1000hdx;
			break;
		case MAC_PROP_ADV_100FDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_adv_100fdx;
			break;
		case MAC_PROP_EN_100FDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_en_100fdx;
			break;
		case MAC_PROP_ADV_100HDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_adv_100hdx;
			break;
		case MAC_PROP_EN_100HDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_en_100hdx;
			break;
		case MAC_PROP_ADV_10FDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_adv_10fdx;
			break;
		case MAC_PROP_EN_10FDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_en_10fdx;
			break;
		case MAC_PROP_ADV_10HDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_adv_10hdx;
			break;
		case MAC_PROP_EN_10HDX_CAP:
			*(uint8_t *)pr_val = Adapter->param_en_10hdx;
			break;
		case MAC_PROP_ADV_100T4_CAP:
		case MAC_PROP_EN_100T4_CAP:
			*(uint8_t *)pr_val = Adapter->param_adv_100t4;
			break;
		case MAC_PROP_PRIVATE:
			err = e1000g_get_priv_prop(Adapter, pr_name,
			    pr_valsize, pr_val);
			break;
		default:
			err = ENOTSUP;
			break;
	}

	return (err);
}

static void
e1000g_m_propinfo(void *arg, const char *pr_name, mac_prop_id_t pr_num,
    mac_prop_info_handle_t prh)
{
	struct e1000g *Adapter = arg;
	struct e1000_hw *hw = &Adapter->shared;

	switch (pr_num) {
	case MAC_PROP_DUPLEX:
	case MAC_PROP_SPEED:
	case MAC_PROP_ADV_1000FDX_CAP:
	case MAC_PROP_ADV_1000HDX_CAP:
	case MAC_PROP_ADV_100FDX_CAP:
	case MAC_PROP_ADV_100HDX_CAP:
	case MAC_PROP_ADV_10FDX_CAP:
	case MAC_PROP_ADV_10HDX_CAP:
	case MAC_PROP_ADV_100T4_CAP:
	case MAC_PROP_EN_100T4_CAP:
		mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		break;

	case MAC_PROP_EN_1000FDX_CAP:
		if (hw->phy.media_type != e1000_media_type_copper) {
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		} else {
			mac_prop_info_set_default_uint8(prh,
			    ((Adapter->phy_ext_status &
			    IEEE_ESR_1000T_FD_CAPS) ||
			    (Adapter->phy_ext_status &
			    IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0);
		}
		break;

	case MAC_PROP_EN_100FDX_CAP:
		if (hw->phy.media_type != e1000_media_type_copper) {
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		} else {
			mac_prop_info_set_default_uint8(prh,
			    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
			    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
			    ? 1 : 0);
		}
		break;

	case MAC_PROP_EN_100HDX_CAP:
		if (hw->phy.media_type != e1000_media_type_copper) {
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		} else {
			mac_prop_info_set_default_uint8(prh,
			    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
			    (Adapter->phy_status & MII_SR_100T2_HD_CAPS))
			    ? 1 : 0);
		}
		break;

	case MAC_PROP_EN_10FDX_CAP:
		if (hw->phy.media_type != e1000_media_type_copper) {
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		} else {
			mac_prop_info_set_default_uint8(prh,
			    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0);
		}
		break;

	case MAC_PROP_EN_10HDX_CAP:
		if (hw->phy.media_type != e1000_media_type_copper) {
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		} else {
			mac_prop_info_set_default_uint8(prh,
			    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0);
		}
		break;

	case MAC_PROP_EN_1000HDX_CAP:
		if (hw->phy.media_type != e1000_media_type_copper)
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		break;

	case MAC_PROP_AUTONEG:
		if (hw->phy.media_type != e1000_media_type_copper) {
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
		} else {
			mac_prop_info_set_default_uint8(prh,
			    (Adapter->phy_status & MII_SR_AUTONEG_CAPS)
			    ? 1 : 0);
		}
		break;

	case MAC_PROP_FLOWCTRL:
		mac_prop_info_set_default_link_flowctrl(prh, LINK_FLOWCTRL_BI);
		break;

	case MAC_PROP_MTU: {
		struct e1000_mac_info *mac = &Adapter->shared.mac;
		struct e1000_phy_info *phy = &Adapter->shared.phy;
		uint32_t max;

		/* some MAC types do not support jumbo frames */
		if ((mac->type == e1000_ich8lan) ||
		    ((mac->type == e1000_ich9lan) && (phy->type ==
		    e1000_phy_ife))) {
			max = DEFAULT_MTU;
		} else {
			max = Adapter->max_mtu;
		}

		mac_prop_info_set_range_uint32(prh, DEFAULT_MTU, max);
		break;
	}
	case MAC_PROP_PRIVATE: {
		char valstr[64];
		int value;

		if (strcmp(pr_name, "_adv_pause_cap") == 0 ||
		    strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
			mac_prop_info_set_perm(prh, MAC_PROP_PERM_READ);
			return;
		} else if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
			value = DEFAULT_TX_BCOPY_THRESHOLD;
		} else if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
			value = DEFAULT_TX_INTR_ENABLE;
		} else if (strcmp(pr_name, "_tx_intr_delay") == 0) {
			value = DEFAULT_TX_INTR_DELAY;
		} else if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
			value = DEFAULT_TX_INTR_ABS_DELAY;
		} else if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
			value = DEFAULT_RX_BCOPY_THRESHOLD;
		} else if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
			value = DEFAULT_RX_LIMIT_ON_INTR;
		} else if (strcmp(pr_name, "_rx_intr_delay") == 0) {
			value = DEFAULT_RX_INTR_DELAY;
		} else if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
			value = DEFAULT_RX_INTR_ABS_DELAY;
		} else if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
			value = DEFAULT_INTR_THROTTLING;
		} else if (strcmp(pr_name, "_intr_adaptive") == 0) {
			value = 1;
		} else {
			return;
		}

		(void) snprintf(valstr, sizeof (valstr), "%d", value);
		mac_prop_info_set_default_str(prh, valstr);
		break;
	}
	}
}

/* ARGSUSED2 */
static int
e1000g_set_priv_prop(struct e1000g *Adapter, const char *pr_name,
    uint_t pr_valsize, const void *pr_val)
{
	int err = 0;
	long result;
	struct e1000_hw *hw = &Adapter->shared;

	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_TX_BCOPY_THRESHOLD ||
		    result > MAX_TX_BCOPY_THRESHOLD)
			err = EINVAL;
		else {
			Adapter->tx_bcopy_thresh = (uint32_t)result;
		}
		return (err);
	}
	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < 0 || result > 1)
			err = EINVAL;
		else {
			Adapter->tx_intr_enable = (result == 1) ?
			    B_TRUE: B_FALSE;
			if (Adapter->tx_intr_enable)
				e1000g_mask_tx_interrupt(Adapter);
			else
				e1000g_clear_tx_interrupt(Adapter);
			if (e1000g_check_acc_handle(
			    Adapter->osdep.reg_handle) != DDI_FM_OK) {
				ddi_fm_service_impact(Adapter->dip,
				    DDI_SERVICE_DEGRADED);
				err = EIO;
			}
		}
		return (err);
	}
	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_TX_INTR_DELAY ||
		    result > MAX_TX_INTR_DELAY)
			err = EINVAL;
		else {
			Adapter->tx_intr_delay = (uint32_t)result;
			E1000_WRITE_REG(hw, E1000_TIDV, Adapter->tx_intr_delay);
			if (e1000g_check_acc_handle(
			    Adapter->osdep.reg_handle) != DDI_FM_OK) {
				ddi_fm_service_impact(Adapter->dip,
				    DDI_SERVICE_DEGRADED);
				err = EIO;
			}
		}
		return (err);
	}
	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_TX_INTR_ABS_DELAY ||
		    result > MAX_TX_INTR_ABS_DELAY)
			err = EINVAL;
		else {
			Adapter->tx_intr_abs_delay = (uint32_t)result;
			E1000_WRITE_REG(hw, E1000_TADV,
			    Adapter->tx_intr_abs_delay);
			if (e1000g_check_acc_handle(
			    Adapter->osdep.reg_handle) != DDI_FM_OK) {
				ddi_fm_service_impact(Adapter->dip,
				    DDI_SERVICE_DEGRADED);
				err = EIO;
			}
		}
		return (err);
	}
	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_RX_BCOPY_THRESHOLD ||
		    result > MAX_RX_BCOPY_THRESHOLD)
			err = EINVAL;
		else
			Adapter->rx_bcopy_thresh = (uint32_t)result;
		return (err);
	}
	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_RX_LIMIT_ON_INTR ||
		    result > MAX_RX_LIMIT_ON_INTR)
			err = EINVAL;
		else
			Adapter->rx_limit_onintr = (uint32_t)result;
		return (err);
	}
	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_RX_INTR_DELAY ||
		    result > MAX_RX_INTR_DELAY)
			err = EINVAL;
		else {
			Adapter->rx_intr_delay = (uint32_t)result;
			E1000_WRITE_REG(hw, E1000_RDTR, Adapter->rx_intr_delay);
			if (e1000g_check_acc_handle(
			    Adapter->osdep.reg_handle) != DDI_FM_OK) {
				ddi_fm_service_impact(Adapter->dip,
				    DDI_SERVICE_DEGRADED);
				err = EIO;
			}
		}
		return (err);
	}
	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_RX_INTR_ABS_DELAY ||
		    result > MAX_RX_INTR_ABS_DELAY)
			err = EINVAL;
		else {
			Adapter->rx_intr_abs_delay = (uint32_t)result;
			E1000_WRITE_REG(hw, E1000_RADV,
			    Adapter->rx_intr_abs_delay);
			if (e1000g_check_acc_handle(
			    Adapter->osdep.reg_handle) != DDI_FM_OK) {
				ddi_fm_service_impact(Adapter->dip,
				    DDI_SERVICE_DEGRADED);
				err = EIO;
			}
		}
		return (err);
	}
	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < MIN_INTR_THROTTLING ||
		    result > MAX_INTR_THROTTLING)
			err = EINVAL;
		else {
			if (hw->mac.type >= e1000_82540) {
				Adapter->intr_throttling_rate =
				    (uint32_t)result;
				E1000_WRITE_REG(hw, E1000_ITR,
				    Adapter->intr_throttling_rate);
				if (e1000g_check_acc_handle(
				    Adapter->osdep.reg_handle) != DDI_FM_OK) {
					ddi_fm_service_impact(Adapter->dip,
					    DDI_SERVICE_DEGRADED);
					err = EIO;
				}
			} else
				err = EINVAL;
		}
		return (err);
	}
	if (strcmp(pr_name, "_intr_adaptive") == 0) {
		if (pr_val == NULL) {
			err = EINVAL;
			return (err);
		}
		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
		if (result < 0 || result > 1)
			err = EINVAL;
		else {
			if (hw->mac.type >= e1000_82540) {
				Adapter->intr_adaptive = (result == 1) ?
				    B_TRUE : B_FALSE;
			} else {
				err = EINVAL;
			}
		}
		return (err);
	}
	return (ENOTSUP);
}

static int
e1000g_get_priv_prop(struct e1000g *Adapter, const char *pr_name,
    uint_t pr_valsize, void *pr_val)
{
	int err = ENOTSUP;
	int value;

	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
		value = Adapter->param_adv_pause;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
		value = Adapter->param_adv_asym_pause;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
		value = Adapter->tx_bcopy_thresh;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
		value = Adapter->tx_intr_enable;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
		value = Adapter->tx_intr_delay;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
		value = Adapter->tx_intr_abs_delay;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
		value = Adapter->rx_bcopy_thresh;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
		value = Adapter->rx_limit_onintr;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
		value = Adapter->rx_intr_delay;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
		value = Adapter->rx_intr_abs_delay;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
		value = Adapter->intr_throttling_rate;
		err = 0;
		goto done;
	}
	if (strcmp(pr_name, "_intr_adaptive") == 0) {
		value = Adapter->intr_adaptive;
		err = 0;
		goto done;
	}
done:
	if (err == 0) {
		(void) snprintf(pr_val, pr_valsize, "%d", value);
	}
	return (err);
}

/*
 * e1000g_get_conf - get configurations set in e1000g.conf
 * This routine gets user-configured values out of the configuration
 * file e1000g.conf.
 *
 * For each configurable value, there is a minimum, a maximum, and a
 * default.
 * If user does not configure a value, use the default.
 * If user configures below the minimum, use the minumum.
 * If user configures above the maximum, use the maxumum.
 */
static void
e1000g_get_conf(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;
	boolean_t tbi_compatibility = B_FALSE;
	boolean_t is_jumbo = B_FALSE;
	int propval;
	/*
	 * decrease the number of descriptors and free packets
	 * for jumbo frames to reduce tx/rx resource consumption
	 */
	if (Adapter->max_frame_size >= FRAME_SIZE_UPTO_4K) {
		is_jumbo = B_TRUE;
	}

	/*
	 * get each configurable property from e1000g.conf
	 */

	/*
	 * NumTxDescriptors
	 */
	Adapter->tx_desc_num_flag =
	    e1000g_get_prop(Adapter, "NumTxDescriptors",
	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
	    is_jumbo ? DEFAULT_JUMBO_NUM_TX_DESC
	    : DEFAULT_NUM_TX_DESCRIPTOR, &propval);
	Adapter->tx_desc_num = propval;

	/*
	 * NumRxDescriptors
	 */
	Adapter->rx_desc_num_flag =
	    e1000g_get_prop(Adapter, "NumRxDescriptors",
	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
	    is_jumbo ? DEFAULT_JUMBO_NUM_RX_DESC
	    : DEFAULT_NUM_RX_DESCRIPTOR, &propval);
	Adapter->rx_desc_num = propval;

	/*
	 * NumRxFreeList
	 */
	Adapter->rx_buf_num_flag =
	    e1000g_get_prop(Adapter, "NumRxFreeList",
	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
	    is_jumbo ? DEFAULT_JUMBO_NUM_RX_BUF
	    : DEFAULT_NUM_RX_FREELIST, &propval);
	Adapter->rx_freelist_limit = propval;

	/*
	 * NumTxPacketList
	 */
	Adapter->tx_buf_num_flag =
	    e1000g_get_prop(Adapter, "NumTxPacketList",
	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
	    is_jumbo ? DEFAULT_JUMBO_NUM_TX_BUF
	    : DEFAULT_NUM_TX_FREELIST, &propval);
	Adapter->tx_freelist_num = propval;

	/*
	 * FlowControl
	 */
	hw->fc.send_xon = B_TRUE;
	(void) e1000g_get_prop(Adapter, "FlowControl",
	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL, &propval);
	hw->fc.requested_mode = propval;
	/* 4 is the setting that says "let the eeprom decide" */
	if (hw->fc.requested_mode == 4)
		hw->fc.requested_mode = e1000_fc_default;

	/*
	 * Max Num Receive Packets on Interrupt
	 */
	(void) e1000g_get_prop(Adapter, "MaxNumReceivePackets",
	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
	    DEFAULT_RX_LIMIT_ON_INTR, &propval);
	Adapter->rx_limit_onintr = propval;

	/*
	 * PHY master slave setting
	 */
	(void) e1000g_get_prop(Adapter, "SetMasterSlave",
	    e1000_ms_hw_default, e1000_ms_auto,
	    e1000_ms_hw_default, &propval);
	hw->phy.ms_type = propval;

	/*
	 * Parameter which controls TBI mode workaround, which is only
	 * needed on certain switches such as Cisco 6500/Foundry
	 */
	(void) e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
	    0, 1, DEFAULT_TBI_COMPAT_ENABLE, &propval);
	tbi_compatibility = (propval == 1);
	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);

	/*
	 * MSI Enable
	 */
	(void) e1000g_get_prop(Adapter, "MSIEnable",
	    0, 1, DEFAULT_MSI_ENABLE, &propval);
	Adapter->msi_enable = (propval == 1);

	/*
	 * Interrupt Throttling Rate
	 */
	(void) e1000g_get_prop(Adapter, "intr_throttling_rate",
	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
	    DEFAULT_INTR_THROTTLING, &propval);
	Adapter->intr_throttling_rate = propval;

	/*
	 * Adaptive Interrupt Blanking Enable/Disable
	 * It is enabled by default
	 */
	(void) e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1,
	    &propval);
	Adapter->intr_adaptive = (propval == 1);

	/*
	 * Hardware checksum enable/disable parameter
	 */
	(void) e1000g_get_prop(Adapter, "tx_hcksum_enable",
	    0, 1, DEFAULT_TX_HCKSUM_ENABLE, &propval);
	Adapter->tx_hcksum_enable = (propval == 1);
	/*
	 * Checksum on/off selection via global parameters.
	 *
	 * If the chip is flagged as not capable of (correctly)
	 * handling checksumming, we don't enable it on either
	 * Rx or Tx side.  Otherwise, we take this chip's settings
	 * from the patchable global defaults.
	 *
	 * We advertise our capabilities only if TX offload is
	 * enabled.  On receive, the stack will accept checksummed
	 * packets anyway, even if we haven't said we can deliver
	 * them.
	 */
	switch (hw->mac.type) {
		case e1000_82540:
		case e1000_82544:
		case e1000_82545:
		case e1000_82545_rev_3:
		case e1000_82546:
		case e1000_82546_rev_3:
		case e1000_82571:
		case e1000_82572:
		case e1000_82573:
		case e1000_80003es2lan:
			break;
		/*
		 * For the following Intel PRO/1000 chipsets, we have not
		 * tested the hardware checksum offload capability, so we
		 * disable the capability for them.
		 *	e1000_82542,
		 *	e1000_82543,
		 *	e1000_82541,
		 *	e1000_82541_rev_2,
		 *	e1000_82547,
		 *	e1000_82547_rev_2,
		 */
		default:
			Adapter->tx_hcksum_enable = B_FALSE;
	}

	/*
	 * Large Send Offloading(LSO) Enable/Disable
	 * If the tx hardware checksum is not enabled, LSO should be
	 * disabled.
	 */
	(void) e1000g_get_prop(Adapter, "lso_enable",
	    0, 1, DEFAULT_LSO_ENABLE, &propval);
	Adapter->lso_enable = (propval == 1);

	switch (hw->mac.type) {
		case e1000_82546:
		case e1000_82546_rev_3:
			if (Adapter->lso_enable)
				Adapter->lso_premature_issue = B_TRUE;
			/* FALLTHRU */
		case e1000_82571:
		case e1000_82572:
		case e1000_82573:
		case e1000_80003es2lan:
			break;
		default:
			Adapter->lso_enable = B_FALSE;
	}

	if (!Adapter->tx_hcksum_enable) {
		Adapter->lso_premature_issue = B_FALSE;
		Adapter->lso_enable = B_FALSE;
	}

	/*
	 * If mem_workaround_82546 is enabled, the rx buffer allocated by
	 * e1000_82545, e1000_82546 and e1000_82546_rev_3
	 * will not cross 64k boundary.
	 */
	(void) e1000g_get_prop(Adapter, "mem_workaround_82546",
	    0, 1, DEFAULT_MEM_WORKAROUND_82546, &propval);
	Adapter->mem_workaround_82546 = (propval == 1);

	/*
	 * Max number of multicast addresses
	 */
	(void) e1000g_get_prop(Adapter, "mcast_max_num",
	    MIN_MCAST_NUM, MAX_MCAST_NUM, hw->mac.mta_reg_count * 32,
	    &propval);
	Adapter->mcast_max_num = propval;
}

/*
 * e1000g_get_prop - routine to read properties
 *
 * Get a user-configure property value out of the configuration
 * file e1000g.conf.
 *
 * Caller provides name of the property, a default value, a minimum
 * value, a maximum value and a pointer to the returned property
 * value.
 *
 * Return B_TRUE if the configured value of the property is not a default
 * value, otherwise return B_FALSE.
 */
static boolean_t
e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
    char *propname,		/* name of the property */
    int minval,			/* minimum acceptable value */
    int maxval,			/* maximim acceptable value */
    int defval,			/* default value */
    int *propvalue)		/* property value return to caller */
{
	int propval;		/* value returned for requested property */
	int *props;		/* point to array of properties returned */
	uint_t nprops;		/* number of property value returned */
	boolean_t ret = B_TRUE;

	/*
	 * get the array of properties from the config file
	 */
	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
		/* got some properties, test if we got enough */
		if (Adapter->instance < nprops) {
			propval = props[Adapter->instance];
		} else {
			/* not enough properties configured */
			propval = defval;
			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
			    "Not Enough %s values found in e1000g.conf"
			    " - set to %d\n",
			    propname, propval);
			ret = B_FALSE;
		}

		/* free memory allocated for properties */
		ddi_prop_free(props);

	} else {
		propval = defval;
		ret = B_FALSE;
	}

	/*
	 * enforce limits
	 */
	if (propval > maxval) {
		propval = maxval;
		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
		    "Too High %s value in e1000g.conf - set to %d\n",
		    propname, propval);
	}

	if (propval < minval) {
		propval = minval;
		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
		    "Too Low %s value in e1000g.conf - set to %d\n",
		    propname, propval);
	}

	*propvalue = propval;
	return (ret);
}

static boolean_t
e1000g_link_check(struct e1000g *Adapter)
{
	uint16_t speed, duplex, phydata;
	boolean_t link_changed = B_FALSE;
	struct e1000_hw *hw;
	uint32_t reg_tarc;

	hw = &Adapter->shared;

	if (e1000g_link_up(Adapter)) {
		/*
		 * The Link is up, check whether it was marked as down earlier
		 */
		if (Adapter->link_state != LINK_STATE_UP) {
			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
			Adapter->link_speed = speed;
			Adapter->link_duplex = duplex;
			Adapter->link_state = LINK_STATE_UP;
			link_changed = B_TRUE;

			if (Adapter->link_speed == SPEED_1000)
				Adapter->stall_threshold = TX_STALL_TIME_2S;
			else
				Adapter->stall_threshold = TX_STALL_TIME_8S;

			Adapter->tx_link_down_timeout = 0;

			if ((hw->mac.type == e1000_82571) ||
			    (hw->mac.type == e1000_82572)) {
				reg_tarc = E1000_READ_REG(hw, E1000_TARC(0));
				if (speed == SPEED_1000)
					reg_tarc |= (1 << 21);
				else
					reg_tarc &= ~(1 << 21);
				E1000_WRITE_REG(hw, E1000_TARC(0), reg_tarc);
			}
		}
		Adapter->smartspeed = 0;
	} else {
		if (Adapter->link_state != LINK_STATE_DOWN) {
			Adapter->link_speed = 0;
			Adapter->link_duplex = 0;
			Adapter->link_state = LINK_STATE_DOWN;
			link_changed = B_TRUE;

			/*
			 * SmartSpeed workaround for Tabor/TanaX, When the
			 * driver loses link disable auto master/slave
			 * resolution.
			 */
			if (hw->phy.type == e1000_phy_igp) {
				(void) e1000_read_phy_reg(hw,
				    PHY_1000T_CTRL, &phydata);
				phydata |= CR_1000T_MS_ENABLE;
				(void) e1000_write_phy_reg(hw,
				    PHY_1000T_CTRL, phydata);
			}
		} else {
			e1000g_smartspeed(Adapter);
		}

		if (Adapter->e1000g_state & E1000G_STARTED) {
			if (Adapter->tx_link_down_timeout <
			    MAX_TX_LINK_DOWN_TIMEOUT) {
				Adapter->tx_link_down_timeout++;
			} else if (Adapter->tx_link_down_timeout ==
			    MAX_TX_LINK_DOWN_TIMEOUT) {
				e1000g_tx_clean(Adapter);
				Adapter->tx_link_down_timeout++;
			}
		}
	}

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);

	return (link_changed);
}

/*
 * e1000g_reset_link - Using the link properties to setup the link
 */
int
e1000g_reset_link(struct e1000g *Adapter)
{
	struct e1000_mac_info *mac;
	struct e1000_phy_info *phy;
	struct e1000_hw *hw;
	boolean_t invalid;

	mac = &Adapter->shared.mac;
	phy = &Adapter->shared.phy;
	hw = &Adapter->shared;
	invalid = B_FALSE;

	if (hw->phy.media_type != e1000_media_type_copper)
		goto out;

	if (Adapter->param_adv_autoneg == 1) {
		mac->autoneg = B_TRUE;
		phy->autoneg_advertised = 0;

		/*
		 * 1000hdx is not supported for autonegotiation
		 */
		if (Adapter->param_adv_1000fdx == 1)
			phy->autoneg_advertised |= ADVERTISE_1000_FULL;

		if (Adapter->param_adv_100fdx == 1)
			phy->autoneg_advertised |= ADVERTISE_100_FULL;

		if (Adapter->param_adv_100hdx == 1)
			phy->autoneg_advertised |= ADVERTISE_100_HALF;

		if (Adapter->param_adv_10fdx == 1)
			phy->autoneg_advertised |= ADVERTISE_10_FULL;

		if (Adapter->param_adv_10hdx == 1)
			phy->autoneg_advertised |= ADVERTISE_10_HALF;

		if (phy->autoneg_advertised == 0)
			invalid = B_TRUE;
	} else {
		mac->autoneg = B_FALSE;

		/*
		 * For Intel copper cards, 1000fdx and 1000hdx are not
		 * supported for forced link
		 */
		if (Adapter->param_adv_100fdx == 1)
			mac->forced_speed_duplex = ADVERTISE_100_FULL;
		else if (Adapter->param_adv_100hdx == 1)
			mac->forced_speed_duplex = ADVERTISE_100_HALF;
		else if (Adapter->param_adv_10fdx == 1)
			mac->forced_speed_duplex = ADVERTISE_10_FULL;
		else if (Adapter->param_adv_10hdx == 1)
			mac->forced_speed_duplex = ADVERTISE_10_HALF;
		else
			invalid = B_TRUE;

	}

	if (invalid) {
		e1000g_log(Adapter, CE_WARN,
		    "Invalid link settings. Setup link to "
		    "support autonegotiation with all link capabilities.");
		mac->autoneg = B_TRUE;
		phy->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
	}

out:
	return (e1000_setup_link(&Adapter->shared));
}

static void
e1000g_timer_tx_resched(struct e1000g *Adapter)
{
	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;

	rw_enter(&Adapter->chip_lock, RW_READER);

	if (tx_ring->resched_needed &&
	    ((ddi_get_lbolt() - tx_ring->resched_timestamp) >
	    drv_usectohz(1000000)) &&
	    (Adapter->e1000g_state & E1000G_STARTED) &&
	    (tx_ring->tbd_avail >= DEFAULT_TX_NO_RESOURCE)) {
		tx_ring->resched_needed = B_FALSE;
		mac_tx_update(Adapter->mh);
		E1000G_STAT(tx_ring->stat_reschedule);
		E1000G_STAT(tx_ring->stat_timer_reschedule);
	}

	rw_exit(&Adapter->chip_lock);
}

static void
e1000g_local_timer(void *ws)
{
	struct e1000g *Adapter = (struct e1000g *)ws;
	struct e1000_hw *hw;
	e1000g_ether_addr_t ether_addr;
	boolean_t link_changed;

	hw = &Adapter->shared;

	if (Adapter->e1000g_state & E1000G_ERROR) {
		rw_enter(&Adapter->chip_lock, RW_WRITER);
		Adapter->e1000g_state &= ~E1000G_ERROR;
		rw_exit(&Adapter->chip_lock);

		Adapter->reset_count++;
		if (e1000g_global_reset(Adapter)) {
			ddi_fm_service_impact(Adapter->dip,
			    DDI_SERVICE_RESTORED);
			e1000g_timer_tx_resched(Adapter);
		} else
			ddi_fm_service_impact(Adapter->dip,
			    DDI_SERVICE_LOST);
		return;
	}

	if (e1000g_stall_check(Adapter)) {
		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
		    "Tx stall detected. Activate automatic recovery.\n");
		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
		Adapter->reset_count++;
		if (e1000g_reset_adapter(Adapter)) {
			ddi_fm_service_impact(Adapter->dip,
			    DDI_SERVICE_RESTORED);
			e1000g_timer_tx_resched(Adapter);
		}
		return;
	}

	link_changed = B_FALSE;
	rw_enter(&Adapter->chip_lock, RW_READER);
	if (Adapter->link_complete)
		link_changed = e1000g_link_check(Adapter);
	rw_exit(&Adapter->chip_lock);

	if (link_changed) {
		if (!Adapter->reset_flag &&
		    (Adapter->e1000g_state & E1000G_STARTED) &&
		    !(Adapter->e1000g_state & E1000G_SUSPENDED))
			mac_link_update(Adapter->mh, Adapter->link_state);
		if (Adapter->link_state == LINK_STATE_UP)
			Adapter->reset_flag = B_FALSE;
	}
	/*
	 * Workaround for esb2. Data stuck in fifo on a link
	 * down event. Reset the adapter to recover it.
	 */
	if (Adapter->esb2_workaround) {
		Adapter->esb2_workaround = B_FALSE;
		(void) e1000g_reset_adapter(Adapter);
		return;
	}

	/*
	 * With 82571 controllers, any locally administered address will
	 * be overwritten when there is a reset on the other port.
	 * Detect this circumstance and correct it.
	 */
	if ((hw->mac.type == e1000_82571) &&
	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);

		ether_addr.reg.low = ntohl(ether_addr.reg.low);
		ether_addr.reg.high = ntohl(ether_addr.reg.high);

		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
			e1000_rar_set(hw, hw->mac.addr, 0);
		}
	}

	/*
	 * Long TTL workaround for 82541/82547
	 */
	(void) e1000_igp_ttl_workaround_82547(hw);

	/*
	 * Check for Adaptive IFS settings If there are lots of collisions
	 * change the value in steps...
	 * These properties should only be set for 10/100
	 */
	if ((hw->phy.media_type == e1000_media_type_copper) &&
	    ((Adapter->link_speed == SPEED_100) ||
	    (Adapter->link_speed == SPEED_10))) {
		e1000_update_adaptive(hw);
	}
	/*
	 * Set Timer Interrupts
	 */
	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
	else
		e1000g_timer_tx_resched(Adapter);

	restart_watchdog_timer(Adapter);
}

/*
 * The function e1000g_link_timer() is called when the timer for link setup
 * is expired, which indicates the completion of the link setup. The link
 * state will not be updated until the link setup is completed. And the
 * link state will not be sent to the upper layer through mac_link_update()
 * in this function. It will be updated in the local timer routine or the
 * interrupt service routine after the interface is started (plumbed).
 */
static void
e1000g_link_timer(void *arg)
{
	struct e1000g *Adapter = (struct e1000g *)arg;

	mutex_enter(&Adapter->link_lock);
	Adapter->link_complete = B_TRUE;
	Adapter->link_tid = 0;
	mutex_exit(&Adapter->link_lock);
}

/*
 * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
 *
 * This function read the forced speed and duplex for 10/100 Mbps speeds
 * and also for 1000 Mbps speeds from the e1000g.conf file
 */
static void
e1000g_force_speed_duplex(struct e1000g *Adapter)
{
	int forced;
	int propval;
	struct e1000_mac_info *mac = &Adapter->shared.mac;
	struct e1000_phy_info *phy = &Adapter->shared.phy;

	/*
	 * get value out of config file
	 */
	(void) e1000g_get_prop(Adapter, "ForceSpeedDuplex",
	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY, &forced);

	switch (forced) {
	case GDIAG_10_HALF:
		/*
		 * Disable Auto Negotiation
		 */
		mac->autoneg = B_FALSE;
		mac->forced_speed_duplex = ADVERTISE_10_HALF;
		break;
	case GDIAG_10_FULL:
		/*
		 * Disable Auto Negotiation
		 */
		mac->autoneg = B_FALSE;
		mac->forced_speed_duplex = ADVERTISE_10_FULL;
		break;
	case GDIAG_100_HALF:
		/*
		 * Disable Auto Negotiation
		 */
		mac->autoneg = B_FALSE;
		mac->forced_speed_duplex = ADVERTISE_100_HALF;
		break;
	case GDIAG_100_FULL:
		/*
		 * Disable Auto Negotiation
		 */
		mac->autoneg = B_FALSE;
		mac->forced_speed_duplex = ADVERTISE_100_FULL;
		break;
	case GDIAG_1000_FULL:
		/*
		 * The gigabit spec requires autonegotiation.  Therefore,
		 * when the user wants to force the speed to 1000Mbps, we
		 * enable AutoNeg, but only allow the harware to advertise
		 * 1000Mbps.  This is different from 10/100 operation, where
		 * we are allowed to link without any negotiation.
		 */
		mac->autoneg = B_TRUE;
		phy->autoneg_advertised = ADVERTISE_1000_FULL;
		break;
	default:	/* obey the setting of AutoNegAdvertised */
		mac->autoneg = B_TRUE;
		(void) e1000g_get_prop(Adapter, "AutoNegAdvertised",
		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
		    AUTONEG_ADVERTISE_SPEED_DEFAULT, &propval);
		phy->autoneg_advertised = (uint16_t)propval;
		break;
	}	/* switch */
}

/*
 * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
 *
 * This function reads MaxFrameSize from e1000g.conf
 */
static void
e1000g_get_max_frame_size(struct e1000g *Adapter)
{
	int max_frame;

	/*
	 * get value out of config file
	 */
	(void) e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0,
	    &max_frame);

	switch (max_frame) {
	case 0:
		Adapter->default_mtu = ETHERMTU;
		break;
	case 1:
		Adapter->default_mtu = FRAME_SIZE_UPTO_4K -
		    sizeof (struct ether_vlan_header) - ETHERFCSL;
		break;
	case 2:
		Adapter->default_mtu = FRAME_SIZE_UPTO_8K -
		    sizeof (struct ether_vlan_header) - ETHERFCSL;
		break;
	case 3:
		Adapter->default_mtu = FRAME_SIZE_UPTO_16K -
		    sizeof (struct ether_vlan_header) - ETHERFCSL;
		break;
	default:
		Adapter->default_mtu = ETHERMTU;
		break;
	}	/* switch */

	/*
	 * If the user configed MTU is larger than the deivce's maximum MTU,
	 * the MTU is set to the deivce's maximum value.
	 */
	if (Adapter->default_mtu > Adapter->max_mtu)
		Adapter->default_mtu = Adapter->max_mtu;

	Adapter->max_frame_size = e1000g_mtu2maxframe(Adapter->default_mtu);
}

/*
 * e1000g_pch_limits - Apply limits of the PCH silicon type
 *
 * At any frame size larger than the ethernet default,
 * prevent linking at 10/100 speeds.
 */
static void
e1000g_pch_limits(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;

	/* only applies to PCH silicon type */
	if (hw->mac.type != e1000_pchlan && hw->mac.type != e1000_pch2lan)
		return;

	/* only applies to frames larger than ethernet default */
	if (Adapter->max_frame_size > DEFAULT_FRAME_SIZE) {
		hw->mac.autoneg = B_TRUE;
		hw->phy.autoneg_advertised = ADVERTISE_1000_FULL;

		Adapter->param_adv_autoneg = 1;
		Adapter->param_adv_1000fdx = 1;

		Adapter->param_adv_100fdx = 0;
		Adapter->param_adv_100hdx = 0;
		Adapter->param_adv_10fdx = 0;
		Adapter->param_adv_10hdx = 0;

		e1000g_param_sync(Adapter);
	}
}

/*
 * e1000g_mtu2maxframe - convert given MTU to maximum frame size
 */
static uint32_t
e1000g_mtu2maxframe(uint32_t mtu)
{
	uint32_t maxframe;

	maxframe = mtu + sizeof (struct ether_vlan_header) + ETHERFCSL;

	return (maxframe);
}

static void
arm_watchdog_timer(struct e1000g *Adapter)
{
	Adapter->watchdog_tid =
	    timeout(e1000g_local_timer,
	    (void *)Adapter, 1 * drv_usectohz(1000000));
}
#pragma inline(arm_watchdog_timer)

static void
enable_watchdog_timer(struct e1000g *Adapter)
{
	mutex_enter(&Adapter->watchdog_lock);

	if (!Adapter->watchdog_timer_enabled) {
		Adapter->watchdog_timer_enabled = B_TRUE;
		Adapter->watchdog_timer_started = B_TRUE;
		arm_watchdog_timer(Adapter);
	}

	mutex_exit(&Adapter->watchdog_lock);
}

static void
disable_watchdog_timer(struct e1000g *Adapter)
{
	timeout_id_t tid;

	mutex_enter(&Adapter->watchdog_lock);

	Adapter->watchdog_timer_enabled = B_FALSE;
	Adapter->watchdog_timer_started = B_FALSE;
	tid = Adapter->watchdog_tid;
	Adapter->watchdog_tid = 0;

	mutex_exit(&Adapter->watchdog_lock);

	if (tid != 0)
		(void) untimeout(tid);
}

static void
start_watchdog_timer(struct e1000g *Adapter)
{
	mutex_enter(&Adapter->watchdog_lock);

	if (Adapter->watchdog_timer_enabled) {
		if (!Adapter->watchdog_timer_started) {
			Adapter->watchdog_timer_started = B_TRUE;
			arm_watchdog_timer(Adapter);
		}
	}

	mutex_exit(&Adapter->watchdog_lock);
}

static void
restart_watchdog_timer(struct e1000g *Adapter)
{
	mutex_enter(&Adapter->watchdog_lock);

	if (Adapter->watchdog_timer_started)
		arm_watchdog_timer(Adapter);

	mutex_exit(&Adapter->watchdog_lock);
}

static void
stop_watchdog_timer(struct e1000g *Adapter)
{
	timeout_id_t tid;

	mutex_enter(&Adapter->watchdog_lock);

	Adapter->watchdog_timer_started = B_FALSE;
	tid = Adapter->watchdog_tid;
	Adapter->watchdog_tid = 0;

	mutex_exit(&Adapter->watchdog_lock);

	if (tid != 0)
		(void) untimeout(tid);
}

static void
stop_link_timer(struct e1000g *Adapter)
{
	timeout_id_t tid;

	/* Disable the link timer */
	mutex_enter(&Adapter->link_lock);

	tid = Adapter->link_tid;
	Adapter->link_tid = 0;

	mutex_exit(&Adapter->link_lock);

	if (tid != 0)
		(void) untimeout(tid);
}

static void
stop_82547_timer(e1000g_tx_ring_t *tx_ring)
{
	timeout_id_t tid;

	/* Disable the tx timer for 82547 chipset */
	mutex_enter(&tx_ring->tx_lock);

	tx_ring->timer_enable_82547 = B_FALSE;
	tid = tx_ring->timer_id_82547;
	tx_ring->timer_id_82547 = 0;

	mutex_exit(&tx_ring->tx_lock);

	if (tid != 0)
		(void) untimeout(tid);
}

void
e1000g_clear_interrupt(struct e1000g *Adapter)
{
	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
	    0xffffffff & ~E1000_IMS_RXSEQ);
}

void
e1000g_mask_interrupt(struct e1000g *Adapter)
{
	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);

	if (Adapter->tx_intr_enable)
		e1000g_mask_tx_interrupt(Adapter);
}

/*
 * This routine is called by e1000g_quiesce(), therefore must not block.
 */
void
e1000g_clear_all_interrupts(struct e1000g *Adapter)
{
	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
}

void
e1000g_mask_tx_interrupt(struct e1000g *Adapter)
{
	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
}

void
e1000g_clear_tx_interrupt(struct e1000g *Adapter)
{
	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
}

static void
e1000g_smartspeed(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;
	uint16_t phy_status;
	uint16_t phy_ctrl;

	/*
	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
	 * advertising 1000Full, we don't even use the workaround
	 */
	if ((hw->phy.type != e1000_phy_igp) ||
	    !hw->mac.autoneg ||
	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
		return;

	/*
	 * True if this is the first call of this function or after every
	 * 30 seconds of not having link
	 */
	if (Adapter->smartspeed == 0) {
		/*
		 * If Master/Slave config fault is asserted twice, we
		 * assume back-to-back
		 */
		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
			return;

		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
			return;
		/*
		 * We're assuming back-2-back because our status register
		 * insists! there's a fault in the master/slave
		 * relationship that was "negotiated"
		 */
		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
		/*
		 * Is the phy configured for manual configuration of
		 * master/slave?
		 */
		if (phy_ctrl & CR_1000T_MS_ENABLE) {
			/*
			 * Yes.  Then disable manual configuration (enable
			 * auto configuration) of master/slave
			 */
			phy_ctrl &= ~CR_1000T_MS_ENABLE;
			(void) e1000_write_phy_reg(hw,
			    PHY_1000T_CTRL, phy_ctrl);
			/*
			 * Effectively starting the clock
			 */
			Adapter->smartspeed++;
			/*
			 * Restart autonegotiation
			 */
			if (!e1000_phy_setup_autoneg(hw) &&
			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
				    MII_CR_RESTART_AUTO_NEG);
				(void) e1000_write_phy_reg(hw,
				    PHY_CONTROL, phy_ctrl);
			}
		}
		return;
		/*
		 * Has 6 seconds transpired still without link? Remember,
		 * you should reset the smartspeed counter once you obtain
		 * link
		 */
	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
		/*
		 * Yes.  Remember, we did at the start determine that
		 * there's a master/slave configuration fault, so we're
		 * still assuming there's someone on the other end, but we
		 * just haven't yet been able to talk to it. We then
		 * re-enable auto configuration of master/slave to see if
		 * we're running 2/3 pair cables.
		 */
		/*
		 * If still no link, perhaps using 2/3 pair cable
		 */
		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
		phy_ctrl |= CR_1000T_MS_ENABLE;
		(void) e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
		/*
		 * Restart autoneg with phy enabled for manual
		 * configuration of master/slave
		 */
		if (!e1000_phy_setup_autoneg(hw) &&
		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
			phy_ctrl |=
			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
			(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
		}
		/*
		 * Hopefully, there are no more faults and we've obtained
		 * link as a result.
		 */
	}
	/*
	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
	 * seconds)
	 */
	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
		Adapter->smartspeed = 0;
}

static boolean_t
is_valid_mac_addr(uint8_t *mac_addr)
{
	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
	const uint8_t addr_test2[6] =
	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
		return (B_FALSE);

	return (B_TRUE);
}

/*
 * e1000g_stall_check - check for tx stall
 *
 * This function checks if the adapter is stalled (in transmit).
 *
 * It is called each time the watchdog timeout is invoked.
 * If the transmit descriptor reclaim continuously fails,
 * the watchdog value will increment by 1. If the watchdog
 * value exceeds the threshold, the adapter is assumed to
 * have stalled and need to be reset.
 */
static boolean_t
e1000g_stall_check(struct e1000g *Adapter)
{
	e1000g_tx_ring_t *tx_ring;

	tx_ring = Adapter->tx_ring;

	if (Adapter->link_state != LINK_STATE_UP)
		return (B_FALSE);

	(void) e1000g_recycle(tx_ring);

	if (Adapter->stall_flag)
		return (B_TRUE);

	return (B_FALSE);
}

#ifdef E1000G_DEBUG
static enum ioc_reply
e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
{
	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
	e1000g_peekpoke_t *ppd;
	uint64_t mem_va;
	uint64_t maxoff;
	boolean_t peek;

	switch (iocp->ioc_cmd) {

	case E1000G_IOC_REG_PEEK:
		peek = B_TRUE;
		break;

	case E1000G_IOC_REG_POKE:
		peek = B_FALSE;
		break;

	deault:
		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
		    iocp->ioc_cmd);
		return (IOC_INVAL);
	}

	/*
	 * Validate format of ioctl
	 */
	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
		return (IOC_INVAL);
	if (mp->b_cont == NULL)
		return (IOC_INVAL);

	ppd = (e1000g_peekpoke_t *)(uintptr_t)mp->b_cont->b_rptr;

	/*
	 * Validate request parameters
	 */
	switch (ppd->pp_acc_space) {

	default:
		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
		    ppd->pp_acc_space);
		return (IOC_INVAL);

	case E1000G_PP_SPACE_REG:
		/*
		 * Memory-mapped I/O space
		 */
		ASSERT(ppd->pp_acc_size == 4);
		if (ppd->pp_acc_size != 4)
			return (IOC_INVAL);

		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
			return (IOC_INVAL);

		mem_va = 0;
		maxoff = 0x10000;
		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
		break;

	case E1000G_PP_SPACE_E1000G:
		/*
		 * E1000g data structure!
		 */
		mem_va = (uintptr_t)e1000gp;
		maxoff = sizeof (struct e1000g);
		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
		break;

	}

	if (ppd->pp_acc_offset >= maxoff)
		return (IOC_INVAL);

	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
		return (IOC_INVAL);

	/*
	 * All OK - go!
	 */
	ppd->pp_acc_offset += mem_va;
	(*ppfn)(e1000gp, ppd);
	return (peek ? IOC_REPLY : IOC_ACK);
}

static void
e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
{
	ddi_acc_handle_t handle;
	uint32_t *regaddr;

	handle = e1000gp->osdep.reg_handle;
	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
	    (uintptr_t)ppd->pp_acc_offset);

	ppd->pp_acc_data = ddi_get32(handle, regaddr);
}

static void
e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
{
	ddi_acc_handle_t handle;
	uint32_t *regaddr;
	uint32_t value;

	handle = e1000gp->osdep.reg_handle;
	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
	    (uintptr_t)ppd->pp_acc_offset);
	value = (uint32_t)ppd->pp_acc_data;

	ddi_put32(handle, regaddr, value);
}

static void
e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
{
	uint64_t value;
	void *vaddr;

	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;

	switch (ppd->pp_acc_size) {
	case 1:
		value = *(uint8_t *)vaddr;
		break;

	case 2:
		value = *(uint16_t *)vaddr;
		break;

	case 4:
		value = *(uint32_t *)vaddr;
		break;

	case 8:
		value = *(uint64_t *)vaddr;
		break;
	}

	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
	    (void *)e1000gp, (void *)ppd, value, vaddr);

	ppd->pp_acc_data = value;
}

static void
e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
{
	uint64_t value;
	void *vaddr;

	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
	value = ppd->pp_acc_data;

	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
	    (void *)e1000gp, (void *)ppd, value, vaddr);

	switch (ppd->pp_acc_size) {
	case 1:
		*(uint8_t *)vaddr = (uint8_t)value;
		break;

	case 2:
		*(uint16_t *)vaddr = (uint16_t)value;
		break;

	case 4:
		*(uint32_t *)vaddr = (uint32_t)value;
		break;

	case 8:
		*(uint64_t *)vaddr = (uint64_t)value;
		break;
	}
}
#endif

/*
 * Loopback Support
 */
static lb_property_t lb_normal =
	{ normal,	"normal",	E1000G_LB_NONE		};
static lb_property_t lb_external1000 =
	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
static lb_property_t lb_external100 =
	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
static lb_property_t lb_external10 =
	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
static lb_property_t lb_phy =
	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};

static enum ioc_reply
e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
{
	lb_info_sz_t *lbsp;
	lb_property_t *lbpp;
	struct e1000_hw *hw;
	uint32_t *lbmp;
	uint32_t size;
	uint32_t value;

	hw = &Adapter->shared;

	if (mp->b_cont == NULL)
		return (IOC_INVAL);

	if (!e1000g_check_loopback_support(hw)) {
		e1000g_log(NULL, CE_WARN,
		    "Loopback is not supported on e1000g%d", Adapter->instance);
		return (IOC_INVAL);
	}

	switch (iocp->ioc_cmd) {
	default:
		return (IOC_INVAL);

	case LB_GET_INFO_SIZE:
		size = sizeof (lb_info_sz_t);
		if (iocp->ioc_count != size)
			return (IOC_INVAL);

		rw_enter(&Adapter->chip_lock, RW_WRITER);
		e1000g_get_phy_state(Adapter);

		/*
		 * Workaround for hardware faults. In order to get a stable
		 * state of phy, we will wait for a specific interval and
		 * try again. The time delay is an experiential value based
		 * on our testing.
		 */
		msec_delay(100);
		e1000g_get_phy_state(Adapter);
		rw_exit(&Adapter->chip_lock);

		value = sizeof (lb_normal);
		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
		    (hw->phy.media_type == e1000_media_type_fiber) ||
		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
			value += sizeof (lb_phy);
			switch (hw->mac.type) {
			case e1000_82571:
			case e1000_82572:
			case e1000_80003es2lan:
				value += sizeof (lb_external1000);
				break;
			}
		}
		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
			value += sizeof (lb_external100);
		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
			value += sizeof (lb_external10);

		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
		*lbsp = value;
		break;

	case LB_GET_INFO:
		value = sizeof (lb_normal);
		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
		    (hw->phy.media_type == e1000_media_type_fiber) ||
		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
			value += sizeof (lb_phy);
			switch (hw->mac.type) {
			case e1000_82571:
			case e1000_82572:
			case e1000_80003es2lan:
				value += sizeof (lb_external1000);
				break;
			}
		}
		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
			value += sizeof (lb_external100);
		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
			value += sizeof (lb_external10);

		size = value;
		if (iocp->ioc_count != size)
			return (IOC_INVAL);

		value = 0;
		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
		lbpp[value++] = lb_normal;
		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
		    (hw->phy.media_type == e1000_media_type_fiber) ||
		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
			lbpp[value++] = lb_phy;
			switch (hw->mac.type) {
			case e1000_82571:
			case e1000_82572:
			case e1000_80003es2lan:
				lbpp[value++] = lb_external1000;
				break;
			}
		}
		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
			lbpp[value++] = lb_external100;
		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
			lbpp[value++] = lb_external10;
		break;

	case LB_GET_MODE:
		size = sizeof (uint32_t);
		if (iocp->ioc_count != size)
			return (IOC_INVAL);

		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
		*lbmp = Adapter->loopback_mode;
		break;

	case LB_SET_MODE:
		size = 0;
		if (iocp->ioc_count != sizeof (uint32_t))
			return (IOC_INVAL);

		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
			return (IOC_INVAL);
		break;
	}

	iocp->ioc_count = size;
	iocp->ioc_error = 0;

	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
		return (IOC_INVAL);
	}

	return (IOC_REPLY);
}

static boolean_t
e1000g_check_loopback_support(struct e1000_hw *hw)
{
	switch (hw->mac.type) {
	case e1000_82540:
	case e1000_82545:
	case e1000_82545_rev_3:
	case e1000_82546:
	case e1000_82546_rev_3:
	case e1000_82541:
	case e1000_82541_rev_2:
	case e1000_82547:
	case e1000_82547_rev_2:
	case e1000_82571:
	case e1000_82572:
	case e1000_82573:
	case e1000_82574:
	case e1000_80003es2lan:
	case e1000_ich9lan:
	case e1000_ich10lan:
		return (B_TRUE);
	}
	return (B_FALSE);
}

static boolean_t
e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
{
	struct e1000_hw *hw;
	int i, times;
	boolean_t link_up;

	if (mode == Adapter->loopback_mode)
		return (B_TRUE);

	hw = &Adapter->shared;
	times = 0;

	Adapter->loopback_mode = mode;

	if (mode == E1000G_LB_NONE) {
		/* Reset the chip */
		hw->phy.autoneg_wait_to_complete = B_TRUE;
		(void) e1000g_reset_adapter(Adapter);
		hw->phy.autoneg_wait_to_complete = B_FALSE;
		return (B_TRUE);
	}

again:

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	switch (mode) {
	default:
		rw_exit(&Adapter->chip_lock);
		return (B_FALSE);

	case E1000G_LB_EXTERNAL_1000:
		e1000g_set_external_loopback_1000(Adapter);
		break;

	case E1000G_LB_EXTERNAL_100:
		e1000g_set_external_loopback_100(Adapter);
		break;

	case E1000G_LB_EXTERNAL_10:
		e1000g_set_external_loopback_10(Adapter);
		break;

	case E1000G_LB_INTERNAL_PHY:
		e1000g_set_internal_loopback(Adapter);
		break;
	}

	times++;

	rw_exit(&Adapter->chip_lock);

	/* Wait for link up */
	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
		msec_delay(100);

	rw_enter(&Adapter->chip_lock, RW_WRITER);

	link_up = e1000g_link_up(Adapter);

	rw_exit(&Adapter->chip_lock);

	if (!link_up) {
		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
		    "Failed to get the link up");
		if (times < 2) {
			/* Reset the link */
			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
			    "Reset the link ...");
			(void) e1000g_reset_adapter(Adapter);
			goto again;
		}

		/*
		 * Reset driver to loopback none when set loopback failed
		 * for the second time.
		 */
		Adapter->loopback_mode = E1000G_LB_NONE;

		/* Reset the chip */
		hw->phy.autoneg_wait_to_complete = B_TRUE;
		(void) e1000g_reset_adapter(Adapter);
		hw->phy.autoneg_wait_to_complete = B_FALSE;

		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
		    "Set loopback mode failed, reset to loopback none");

		return (B_FALSE);
	}

	return (B_TRUE);
}

/*
 * The following loopback settings are from Intel's technical
 * document - "How To Loopback". All the register settings and
 * time delay values are directly inherited from the document
 * without more explanations available.
 */
static void
e1000g_set_internal_loopback(struct e1000g *Adapter)
{
	struct e1000_hw *hw;
	uint32_t ctrl;
	uint32_t status;
	uint16_t phy_ctrl;
	uint16_t phy_reg;
	uint32_t txcw;

	hw = &Adapter->shared;

	/* Disable Smart Power Down */
	phy_spd_state(hw, B_FALSE);

	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;

	switch (hw->mac.type) {
	case e1000_82540:
	case e1000_82545:
	case e1000_82545_rev_3:
	case e1000_82546:
	case e1000_82546_rev_3:
	case e1000_82573:
		/* Auto-MDI/MDIX off */
		(void) e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
		/* Reset PHY to update Auto-MDI/MDIX */
		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
		/* Reset PHY to auto-neg off and force 1000 */
		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
		    phy_ctrl | MII_CR_RESET);
		/*
		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
		 * See comments above e1000g_set_internal_loopback() for the
		 * background.
		 */
		(void) e1000_write_phy_reg(hw, 29, 0x001F);
		(void) e1000_write_phy_reg(hw, 30, 0x8FFC);
		(void) e1000_write_phy_reg(hw, 29, 0x001A);
		(void) e1000_write_phy_reg(hw, 30, 0x8FF0);
		break;
	case e1000_80003es2lan:
		/* Force Link Up */
		(void) e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
		    0x1CC);
		/* Sets PCS loopback at 1Gbs */
		(void) e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
		    0x1046);
		break;
	}

	/*
	 * The following registers should be set for e1000_phy_bm phy type.
	 * e1000_82574, e1000_ich10lan and some e1000_ich9lan use this phy.
	 * For others, we do not need to set these registers.
	 */
	if (hw->phy.type == e1000_phy_bm) {
		/* Set Default MAC Interface speed to 1GB */
		(void) e1000_read_phy_reg(hw, PHY_REG(2, 21), &phy_reg);
		phy_reg &= ~0x0007;
		phy_reg |= 0x006;
		(void) e1000_write_phy_reg(hw, PHY_REG(2, 21), phy_reg);
		/* Assert SW reset for above settings to take effect */
		(void) e1000_phy_commit(hw);
		msec_delay(1);
		/* Force Full Duplex */
		(void) e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
		(void) e1000_write_phy_reg(hw, PHY_REG(769, 16),
		    phy_reg | 0x000C);
		/* Set Link Up (in force link) */
		(void) e1000_read_phy_reg(hw, PHY_REG(776, 16), &phy_reg);
		(void) e1000_write_phy_reg(hw, PHY_REG(776, 16),
		    phy_reg | 0x0040);
		/* Force Link */
		(void) e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
		(void) e1000_write_phy_reg(hw, PHY_REG(769, 16),
		    phy_reg | 0x0040);
		/* Set Early Link Enable */
		(void) e1000_read_phy_reg(hw, PHY_REG(769, 20), &phy_reg);
		(void) e1000_write_phy_reg(hw, PHY_REG(769, 20),
		    phy_reg | 0x0400);
	}

	/* Set loopback */
	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);

	msec_delay(250);

	/* Now set up the MAC to the same speed/duplex as the PHY. */
	ctrl = E1000_READ_REG(hw, E1000_CTRL);
	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
	    E1000_CTRL_FD);		/* Force Duplex to FULL */

	switch (hw->mac.type) {
	case e1000_82540:
	case e1000_82545:
	case e1000_82545_rev_3:
	case e1000_82546:
	case e1000_82546_rev_3:
		/*
		 * For some serdes we'll need to commit the writes now
		 * so that the status is updated on link
		 */
		if (hw->phy.media_type == e1000_media_type_internal_serdes) {
			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
			msec_delay(100);
			ctrl = E1000_READ_REG(hw, E1000_CTRL);
		}

		if (hw->phy.media_type == e1000_media_type_copper) {
			/* Invert Loss of Signal */
			ctrl |= E1000_CTRL_ILOS;
		} else {
			/* Set ILOS on fiber nic if half duplex is detected */
			status = E1000_READ_REG(hw, E1000_STATUS);
			if ((status & E1000_STATUS_FD) == 0)
				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
		}
		break;

	case e1000_82571:
	case e1000_82572:
		/*
		 * The fiber/SerDes versions of this adapter do not contain an
		 * accessible PHY. Therefore, loopback beyond MAC must be done
		 * using SerDes analog loopback.
		 */
		if (hw->phy.media_type != e1000_media_type_copper) {
			/* Disable autoneg by setting bit 31 of TXCW to zero */
			txcw = E1000_READ_REG(hw, E1000_TXCW);
			txcw &= ~((uint32_t)1 << 31);
			E1000_WRITE_REG(hw, E1000_TXCW, txcw);

			/*
			 * Write 0x410 to Serdes Control register
			 * to enable Serdes analog loopback
			 */
			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
			msec_delay(10);
		}

		status = E1000_READ_REG(hw, E1000_STATUS);
		/* Set ILOS on fiber nic if half duplex is detected */
		if ((hw->phy.media_type == e1000_media_type_fiber) &&
		    ((status & E1000_STATUS_FD) == 0 ||
		    (status & E1000_STATUS_LU) == 0))
			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
		else if (hw->phy.media_type == e1000_media_type_internal_serdes)
			ctrl |= E1000_CTRL_SLU;
		break;

	case e1000_82573:
		ctrl |= E1000_CTRL_ILOS;
		break;
	case e1000_ich9lan:
	case e1000_ich10lan:
		ctrl |= E1000_CTRL_SLU;
		break;
	}
	if (hw->phy.type == e1000_phy_bm)
		ctrl |= E1000_CTRL_SLU | E1000_CTRL_ILOS;

	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
}

static void
e1000g_set_external_loopback_1000(struct e1000g *Adapter)
{
	struct e1000_hw *hw;
	uint32_t rctl;
	uint32_t ctrl_ext;
	uint32_t ctrl;
	uint32_t status;
	uint32_t txcw;
	uint16_t phydata;

	hw = &Adapter->shared;

	/* Disable Smart Power Down */
	phy_spd_state(hw, B_FALSE);

	switch (hw->mac.type) {
	case e1000_82571:
	case e1000_82572:
		switch (hw->phy.media_type) {
		case e1000_media_type_copper:
			/* Force link up (Must be done before the PHY writes) */
			ctrl = E1000_READ_REG(hw, E1000_CTRL);
			ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);

			rctl = E1000_READ_REG(hw, E1000_RCTL);
			rctl |= (E1000_RCTL_EN |
			    E1000_RCTL_SBP |
			    E1000_RCTL_UPE |
			    E1000_RCTL_MPE |
			    E1000_RCTL_LPE |
			    E1000_RCTL_BAM);		/* 0x803E */
			E1000_WRITE_REG(hw, E1000_RCTL, rctl);

			ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
			ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
			    E1000_CTRL_EXT_SDP6_DATA |
			    E1000_CTRL_EXT_SDP3_DATA |
			    E1000_CTRL_EXT_SDP4_DIR |
			    E1000_CTRL_EXT_SDP6_DIR |
			    E1000_CTRL_EXT_SDP3_DIR);	/* 0x0DD0 */
			E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);

			/*
			 * This sequence tunes the PHY's SDP and no customer
			 * settable values. For background, see comments above
			 * e1000g_set_internal_loopback().
			 */
			(void) e1000_write_phy_reg(hw, 0x0, 0x140);
			msec_delay(10);
			(void) e1000_write_phy_reg(hw, 0x9, 0x1A00);
			(void) e1000_write_phy_reg(hw, 0x12, 0xC10);
			(void) e1000_write_phy_reg(hw, 0x12, 0x1C10);
			(void) e1000_write_phy_reg(hw, 0x1F37, 0x76);
			(void) e1000_write_phy_reg(hw, 0x1F33, 0x1);
			(void) e1000_write_phy_reg(hw, 0x1F33, 0x0);

			(void) e1000_write_phy_reg(hw, 0x1F35, 0x65);
			(void) e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
			(void) e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
			(void) e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
			(void) e1000_write_phy_reg(hw, 0x1137, 0x3FDC);

			msec_delay(50);
			break;
		case e1000_media_type_fiber:
		case e1000_media_type_internal_serdes:
			status = E1000_READ_REG(hw, E1000_STATUS);
			if (((status & E1000_STATUS_LU) == 0) ||
			    (hw->phy.media_type ==
			    e1000_media_type_internal_serdes)) {
				ctrl = E1000_READ_REG(hw, E1000_CTRL);
				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
				E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
			}

			/* Disable autoneg by setting bit 31 of TXCW to zero */
			txcw = E1000_READ_REG(hw, E1000_TXCW);
			txcw &= ~((uint32_t)1 << 31);
			E1000_WRITE_REG(hw, E1000_TXCW, txcw);

			/*
			 * Write 0x410 to Serdes Control register
			 * to enable Serdes analog loopback
			 */
			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
			msec_delay(10);
			break;
		default:
			break;
		}
		break;
	case e1000_82574:
	case e1000_80003es2lan:
	case e1000_ich9lan:
	case e1000_ich10lan:
		(void) e1000_read_phy_reg(hw, GG82563_REG(6, 16), &phydata);
		(void) e1000_write_phy_reg(hw, GG82563_REG(6, 16),
		    phydata | (1 << 5));
		Adapter->param_adv_autoneg = 1;
		Adapter->param_adv_1000fdx = 1;
		(void) e1000g_reset_link(Adapter);
		break;
	}
}

static void
e1000g_set_external_loopback_100(struct e1000g *Adapter)
{
	struct e1000_hw *hw;
	uint32_t ctrl;
	uint16_t phy_ctrl;

	hw = &Adapter->shared;

	/* Disable Smart Power Down */
	phy_spd_state(hw, B_FALSE);

	phy_ctrl = (MII_CR_FULL_DUPLEX |
	    MII_CR_SPEED_100);

	/* Force 100/FD, reset PHY */
	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
	msec_delay(10);

	/* Force 100/FD */
	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
	    phy_ctrl);			/* 0x2100 */
	msec_delay(10);

	/* Now setup the MAC to the same speed/duplex as the PHY. */
	ctrl = E1000_READ_REG(hw, E1000_CTRL);
	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
	    E1000_CTRL_FD);		/* Force Duplex to FULL */

	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
}

static void
e1000g_set_external_loopback_10(struct e1000g *Adapter)
{
	struct e1000_hw *hw;
	uint32_t ctrl;
	uint16_t phy_ctrl;

	hw = &Adapter->shared;

	/* Disable Smart Power Down */
	phy_spd_state(hw, B_FALSE);

	phy_ctrl = (MII_CR_FULL_DUPLEX |
	    MII_CR_SPEED_10);

	/* Force 10/FD, reset PHY */
	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
	msec_delay(10);

	/* Force 10/FD */
	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
	    phy_ctrl);			/* 0x0100 */
	msec_delay(10);

	/* Now setup the MAC to the same speed/duplex as the PHY. */
	ctrl = E1000_READ_REG(hw, E1000_CTRL);
	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
	    E1000_CTRL_FD);		/* Force Duplex to FULL */

	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
}

#ifdef __sparc
static boolean_t
e1000g_find_mac_address(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;
	uchar_t *bytes;
	struct ether_addr sysaddr;
	uint_t nelts;
	int err;
	boolean_t found = B_FALSE;

	/*
	 * The "vendor's factory-set address" may already have
	 * been extracted from the chip, but if the property
	 * "local-mac-address" is set we use that instead.
	 *
	 * We check whether it looks like an array of 6
	 * bytes (which it should, if OBP set it).  If we can't
	 * make sense of it this way, we'll ignore it.
	 */
	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
	if (err == DDI_PROP_SUCCESS) {
		if (nelts == ETHERADDRL) {
			while (nelts--)
				hw->mac.addr[nelts] = bytes[nelts];
			found = B_TRUE;
		}
		ddi_prop_free(bytes);
	}

	/*
	 * Look up the OBP property "local-mac-address?". If the user has set
	 * 'local-mac-address? = false', use "the system address" instead.
	 */
	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
			if (localetheraddr(NULL, &sysaddr) != 0) {
				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
				found = B_TRUE;
			}
		}
		ddi_prop_free(bytes);
	}

	/*
	 * Finally(!), if there's a valid "mac-address" property (created
	 * if we netbooted from this interface), we must use this instead
	 * of any of the above to ensure that the NFS/install server doesn't
	 * get confused by the address changing as Solaris takes over!
	 */
	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
	if (err == DDI_PROP_SUCCESS) {
		if (nelts == ETHERADDRL) {
			while (nelts--)
				hw->mac.addr[nelts] = bytes[nelts];
			found = B_TRUE;
		}
		ddi_prop_free(bytes);
	}

	if (found) {
		bcopy(hw->mac.addr, hw->mac.perm_addr,
		    ETHERADDRL);
	}

	return (found);
}
#endif

static int
e1000g_add_intrs(struct e1000g *Adapter)
{
	dev_info_t *devinfo;
	int intr_types;
	int rc;

	devinfo = Adapter->dip;

	/* Get supported interrupt types */
	rc = ddi_intr_get_supported_types(devinfo, &intr_types);

	if (rc != DDI_SUCCESS) {
		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
		    "Get supported interrupt types failed: %d\n", rc);
		return (DDI_FAILURE);
	}

	/*
	 * Based on Intel Technical Advisory document (TA-160), there are some
	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
	 * that it supports MSI, but in fact has problems.
	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
	 * PCI/PCI-X NICs.
	 */
	if (Adapter->shared.mac.type < e1000_82571)
		Adapter->msi_enable = B_FALSE;

	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enable) {
		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);

		if (rc != DDI_SUCCESS) {
			/* EMPTY */
			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
			    "Add MSI failed, trying Legacy interrupts\n");
		} else {
			Adapter->intr_type = DDI_INTR_TYPE_MSI;
		}
	}

	if ((Adapter->intr_type == 0) &&
	    (intr_types & DDI_INTR_TYPE_FIXED)) {
		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);

		if (rc != DDI_SUCCESS) {
			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
			    "Add Legacy interrupts failed\n");
			return (DDI_FAILURE);
		}

		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
	}

	if (Adapter->intr_type == 0) {
		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
		    "No interrupts registered\n");
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

/*
 * e1000g_intr_add() handles MSI/Legacy interrupts
 */
static int
e1000g_intr_add(struct e1000g *Adapter, int intr_type)
{
	dev_info_t *devinfo;
	int count, avail, actual;
	int x, y, rc, inum = 0;
	int flag;
	ddi_intr_handler_t *intr_handler;

	devinfo = Adapter->dip;

	/* get number of interrupts */
	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
	if ((rc != DDI_SUCCESS) || (count == 0)) {
		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
		    "Get interrupt number failed. Return: %d, count: %d\n",
		    rc, count);
		return (DDI_FAILURE);
	}

	/* get number of available interrupts */
	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
	if ((rc != DDI_SUCCESS) || (avail == 0)) {
		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
		    "Get interrupt available number failed. "
		    "Return: %d, available: %d\n", rc, avail);
		return (DDI_FAILURE);
	}

	if (avail < count) {
		/* EMPTY */
		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
		    "Interrupts count: %d, available: %d\n",
		    count, avail);
	}

	/* Allocate an array of interrupt handles */
	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);

	/* Set NORMAL behavior for both MSI and FIXED interrupt */
	flag = DDI_INTR_ALLOC_NORMAL;

	/* call ddi_intr_alloc() */
	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
	    count, &actual, flag);

	if ((rc != DDI_SUCCESS) || (actual == 0)) {
		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
		    "Allocate interrupts failed: %d\n", rc);

		kmem_free(Adapter->htable, Adapter->intr_size);
		return (DDI_FAILURE);
	}

	if (actual < count) {
		/* EMPTY */
		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
		    "Interrupts requested: %d, received: %d\n",
		    count, actual);
	}

	Adapter->intr_cnt = actual;

	/* Get priority for first msi, assume remaining are all the same */
	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);

	if (rc != DDI_SUCCESS) {
		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
		    "Get interrupt priority failed: %d\n", rc);

		/* Free already allocated intr */
		for (y = 0; y < actual; y++)
			(void) ddi_intr_free(Adapter->htable[y]);

		kmem_free(Adapter->htable, Adapter->intr_size);
		return (DDI_FAILURE);
	}

	/*
	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
	 * use the interrupt service routine e1000g_intr_pciexpress()
	 * to avoid interrupt stealing when sharing interrupt with other
	 * devices.
	 */
	if (Adapter->shared.mac.type < e1000_82571)
		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
	else
		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;

	/* Call ddi_intr_add_handler() */
	for (x = 0; x < actual; x++) {
		rc = ddi_intr_add_handler(Adapter->htable[x],
		    intr_handler, (caddr_t)Adapter, NULL);

		if (rc != DDI_SUCCESS) {
			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
			    "Add interrupt handler failed: %d\n", rc);

			/* Remove already added handler */
			for (y = 0; y < x; y++)
				(void) ddi_intr_remove_handler(
				    Adapter->htable[y]);

			/* Free already allocated intr */
			for (y = 0; y < actual; y++)
				(void) ddi_intr_free(Adapter->htable[y]);

			kmem_free(Adapter->htable, Adapter->intr_size);
			return (DDI_FAILURE);
		}
	}

	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);

	if (rc != DDI_SUCCESS) {
		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
		    "Get interrupt cap failed: %d\n", rc);

		/* Free already allocated intr */
		for (y = 0; y < actual; y++) {
			(void) ddi_intr_remove_handler(Adapter->htable[y]);
			(void) ddi_intr_free(Adapter->htable[y]);
		}

		kmem_free(Adapter->htable, Adapter->intr_size);
		return (DDI_FAILURE);
	}

	return (DDI_SUCCESS);
}

static int
e1000g_rem_intrs(struct e1000g *Adapter)
{
	int x;
	int rc;

	for (x = 0; x < Adapter->intr_cnt; x++) {
		rc = ddi_intr_remove_handler(Adapter->htable[x]);
		if (rc != DDI_SUCCESS) {
			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
			    "Remove intr handler failed: %d\n", rc);
			return (DDI_FAILURE);
		}

		rc = ddi_intr_free(Adapter->htable[x]);
		if (rc != DDI_SUCCESS) {
			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
			    "Free intr failed: %d\n", rc);
			return (DDI_FAILURE);
		}
	}

	kmem_free(Adapter->htable, Adapter->intr_size);

	return (DDI_SUCCESS);
}

static int
e1000g_enable_intrs(struct e1000g *Adapter)
{
	int x;
	int rc;

	/* Enable interrupts */
	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
		/* Call ddi_intr_block_enable() for MSI */
		rc = ddi_intr_block_enable(Adapter->htable,
		    Adapter->intr_cnt);
		if (rc != DDI_SUCCESS) {
			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
			    "Enable block intr failed: %d\n", rc);
			return (DDI_FAILURE);
		}
	} else {
		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
		for (x = 0; x < Adapter->intr_cnt; x++) {
			rc = ddi_intr_enable(Adapter->htable[x]);
			if (rc != DDI_SUCCESS) {
				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
				    "Enable intr failed: %d\n", rc);
				return (DDI_FAILURE);
			}
		}
	}

	return (DDI_SUCCESS);
}

static int
e1000g_disable_intrs(struct e1000g *Adapter)
{
	int x;
	int rc;

	/* Disable all interrupts */
	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
		rc = ddi_intr_block_disable(Adapter->htable,
		    Adapter->intr_cnt);
		if (rc != DDI_SUCCESS) {
			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
			    "Disable block intr failed: %d\n", rc);
			return (DDI_FAILURE);
		}
	} else {
		for (x = 0; x < Adapter->intr_cnt; x++) {
			rc = ddi_intr_disable(Adapter->htable[x]);
			if (rc != DDI_SUCCESS) {
				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
				    "Disable intr failed: %d\n", rc);
				return (DDI_FAILURE);
			}
		}
	}

	return (DDI_SUCCESS);
}

/*
 * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
 */
static void
e1000g_get_phy_state(struct e1000g *Adapter)
{
	struct e1000_hw *hw = &Adapter->shared;

	if (hw->phy.media_type == e1000_media_type_copper) {
		(void) e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
		(void) e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
		    &Adapter->phy_an_adv);
		(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP,
		    &Adapter->phy_an_exp);
		(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS,
		    &Adapter->phy_ext_status);
		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL,
		    &Adapter->phy_1000t_ctrl);
		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS,
		    &Adapter->phy_1000t_status);
		(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY,
		    &Adapter->phy_lp_able);

		Adapter->param_autoneg_cap =
		    (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
		Adapter->param_pause_cap =
		    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
		Adapter->param_asym_pause_cap =
		    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
		Adapter->param_1000fdx_cap =
		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
		Adapter->param_1000hdx_cap =
		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
		Adapter->param_100t4_cap =
		    (Adapter->phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
		Adapter->param_100fdx_cap =
		    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
		Adapter->param_100hdx_cap =
		    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
		Adapter->param_10fdx_cap =
		    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
		Adapter->param_10hdx_cap =
		    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;

		Adapter->param_adv_autoneg = hw->mac.autoneg;
		Adapter->param_adv_pause =
		    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
		Adapter->param_adv_asym_pause =
		    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
		Adapter->param_adv_1000hdx =
		    (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
		Adapter->param_adv_100t4 =
		    (Adapter->phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
		if (Adapter->param_adv_autoneg == 1) {
			Adapter->param_adv_1000fdx =
			    (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS)
			    ? 1 : 0;
			Adapter->param_adv_100fdx =
			    (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS)
			    ? 1 : 0;
			Adapter->param_adv_100hdx =
			    (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS)
			    ? 1 : 0;
			Adapter->param_adv_10fdx =
			    (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
			Adapter->param_adv_10hdx =
			    (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
		}

		Adapter->param_lp_autoneg =
		    (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
		Adapter->param_lp_pause =
		    (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
		Adapter->param_lp_asym_pause =
		    (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
		Adapter->param_lp_1000fdx =
		    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
		Adapter->param_lp_1000hdx =
		    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
		Adapter->param_lp_100t4 =
		    (Adapter->phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
		Adapter->param_lp_100fdx =
		    (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
		Adapter->param_lp_100hdx =
		    (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
		Adapter->param_lp_10fdx =
		    (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
		Adapter->param_lp_10hdx =
		    (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
	} else {
		/*
		 * 1Gig Fiber adapter only offers 1Gig Full Duplex. Meaning,
		 * it can only work with 1Gig Full Duplex Link Partner.
		 */
		Adapter->param_autoneg_cap = 0;
		Adapter->param_pause_cap = 1;
		Adapter->param_asym_pause_cap = 1;
		Adapter->param_1000fdx_cap = 1;
		Adapter->param_1000hdx_cap = 0;
		Adapter->param_100t4_cap = 0;
		Adapter->param_100fdx_cap = 0;
		Adapter->param_100hdx_cap = 0;
		Adapter->param_10fdx_cap = 0;
		Adapter->param_10hdx_cap = 0;

		Adapter->param_adv_autoneg = 0;
		Adapter->param_adv_pause = 1;
		Adapter->param_adv_asym_pause = 1;
		Adapter->param_adv_1000fdx = 1;
		Adapter->param_adv_1000hdx = 0;
		Adapter->param_adv_100t4 = 0;
		Adapter->param_adv_100fdx = 0;
		Adapter->param_adv_100hdx = 0;
		Adapter->param_adv_10fdx = 0;
		Adapter->param_adv_10hdx = 0;

		Adapter->param_lp_autoneg = 0;
		Adapter->param_lp_pause = 0;
		Adapter->param_lp_asym_pause = 0;
		Adapter->param_lp_1000fdx = 0;
		Adapter->param_lp_1000hdx = 0;
		Adapter->param_lp_100t4 = 0;
		Adapter->param_lp_100fdx = 0;
		Adapter->param_lp_100hdx = 0;
		Adapter->param_lp_10fdx = 0;
		Adapter->param_lp_10hdx = 0;
	}
}

/*
 * FMA support
 */

int
e1000g_check_acc_handle(ddi_acc_handle_t handle)
{
	ddi_fm_error_t de;

	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
	return (de.fme_status);
}

int
e1000g_check_dma_handle(ddi_dma_handle_t handle)
{
	ddi_fm_error_t de;

	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
	return (de.fme_status);
}

/*
 * The IO fault service error handling callback function
 */
/* ARGSUSED2 */
static int
e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
{
	/*
	 * as the driver can always deal with an error in any dma or
	 * access handle, we can just return the fme_status value.
	 */
	pci_ereport_post(dip, err, NULL);
	return (err->fme_status);
}

static void
e1000g_fm_init(struct e1000g *Adapter)
{
	ddi_iblock_cookie_t iblk;
	int fma_dma_flag;

	/* Only register with IO Fault Services if we have some capability */
	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
	} else {
		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
	}

	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
		fma_dma_flag = 1;
	} else {
		fma_dma_flag = 0;
	}

	(void) e1000g_set_fma_flags(fma_dma_flag);

	if (Adapter->fm_capabilities) {

		/* Register capabilities with IO Fault Services */
		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);

		/*
		 * Initialize pci ereport capabilities if ereport capable
		 */
		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
			pci_ereport_setup(Adapter->dip);

		/*
		 * Register error callback if error callback capable
		 */
		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
			ddi_fm_handler_register(Adapter->dip,
			    e1000g_fm_error_cb, (void*) Adapter);
	}
}

static void
e1000g_fm_fini(struct e1000g *Adapter)
{
	/* Only unregister FMA capabilities if we registered some */
	if (Adapter->fm_capabilities) {

		/*
		 * Release any resources allocated by pci_ereport_setup()
		 */
		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
			pci_ereport_teardown(Adapter->dip);

		/*
		 * Un-register error callback if error callback capable
		 */
		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
			ddi_fm_handler_unregister(Adapter->dip);

		/* Unregister from IO Fault Services */
		mutex_enter(&e1000g_rx_detach_lock);
		ddi_fm_fini(Adapter->dip);
		if (Adapter->priv_dip != NULL) {
			DEVI(Adapter->priv_dip)->devi_fmhdl = NULL;
		}
		mutex_exit(&e1000g_rx_detach_lock);
	}
}

void
e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
{
	uint64_t ena;
	char buf[FM_MAX_CLASS];

	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
	ena = fm_ena_generate(0, FM_ENA_FMT1);
	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
	}
}

/*
 * quiesce(9E) entry point.
 *
 * This function is called when the system is single-threaded at high
 * PIL with preemption disabled. Therefore, this function must not be
 * blocked.
 *
 * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
 * DDI_FAILURE indicates an error condition and should almost never happen.
 */
static int
e1000g_quiesce(dev_info_t *devinfo)
{
	struct e1000g *Adapter;

	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);

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

	e1000g_clear_all_interrupts(Adapter);

	(void) e1000_reset_hw(&Adapter->shared);

	/* Setup our HW Tx Head & Tail descriptor pointers */
	E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
	E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);

	/* Setup our HW Rx Head & Tail descriptor pointers */
	E1000_WRITE_REG(&Adapter->shared, E1000_RDH(0), 0);
	E1000_WRITE_REG(&Adapter->shared, E1000_RDT(0), 0);

	return (DDI_SUCCESS);
}

/*
 * synchronize the adv* and en* parameters.
 *
 * See comments in <sys/dld.h> for details of the *_en_*
 * parameters. The usage of ndd for setting adv parameters will
 * synchronize all the en parameters with the e1000g parameters,
 * implicitly disabling any settings made via dladm.
 */
static void
e1000g_param_sync(struct e1000g *Adapter)
{
	Adapter->param_en_1000fdx = Adapter->param_adv_1000fdx;
	Adapter->param_en_1000hdx = Adapter->param_adv_1000hdx;
	Adapter->param_en_100fdx = Adapter->param_adv_100fdx;
	Adapter->param_en_100hdx = Adapter->param_adv_100hdx;
	Adapter->param_en_10fdx = Adapter->param_adv_10fdx;
	Adapter->param_en_10hdx = Adapter->param_adv_10hdx;
}

/*
 * e1000g_get_driver_control - tell manageability firmware that the driver
 * has control.
 */
static void
e1000g_get_driver_control(struct e1000_hw *hw)
{
	uint32_t ctrl_ext;
	uint32_t swsm;

	/* tell manageability firmware the driver has taken over */
	switch (hw->mac.type) {
	case e1000_82573:
		swsm = E1000_READ_REG(hw, E1000_SWSM);
		E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_DRV_LOAD);
		break;
	case e1000_82571:
	case e1000_82572:
	case e1000_82574:
	case e1000_80003es2lan:
	case e1000_ich8lan:
	case e1000_ich9lan:
	case e1000_ich10lan:
	case e1000_pchlan:
	case e1000_pch2lan:
		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
		    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
		break;
	default:
		/* no manageability firmware: do nothing */
		break;
	}
}

/*
 * e1000g_release_driver_control - tell manageability firmware that the driver
 * has released control.
 */
static void
e1000g_release_driver_control(struct e1000_hw *hw)
{
	uint32_t ctrl_ext;
	uint32_t swsm;

	/* tell manageability firmware the driver has released control */
	switch (hw->mac.type) {
	case e1000_82573:
		swsm = E1000_READ_REG(hw, E1000_SWSM);
		E1000_WRITE_REG(hw, E1000_SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
		break;
	case e1000_82571:
	case e1000_82572:
	case e1000_82574:
	case e1000_80003es2lan:
	case e1000_ich8lan:
	case e1000_ich9lan:
	case e1000_ich10lan:
	case e1000_pchlan:
	case e1000_pch2lan:
		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
		    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
		break;
	default:
		/* no manageability firmware: do nothing */
		break;
	}
}

/*
 * Restore e1000g promiscuous mode.
 */
static void
e1000g_restore_promisc(struct e1000g *Adapter)
{
	if (Adapter->e1000g_promisc) {
		uint32_t rctl;

		rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
		E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
	}
}