Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
/*	$NetBSD: if_dge.c,v 1.63 2021/12/31 14:25:23 riastradh Exp $ */

/*
 * Copyright (c) 2004, SUNET, Swedish University Computer Network.
 * All rights reserved.
 *
 * Written by Anders Magnusson for SUNET, Swedish University Computer Network.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	SUNET, Swedish University Computer Network.
 * 4. The name of SUNET may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY SUNET ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Device driver for the Intel 82597EX Ten Gigabit Ethernet controller.
 *
 * TODO (in no specific order):
 *	HW VLAN support.
 *	TSE offloading (needs kernel changes...)
 *	RAIDC (receive interrupt delay adaptation)
 *	Use memory > 4GB.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_dge.c,v 1.63 2021/12/31 14:25:23 riastradh Exp $");



#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/rndsource.h>

#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_ether.h>
#include <net/bpf.h>

#include <netinet/in.h>			/* XXX for struct ip */
#include <netinet/in_systm.h>		/* XXX for struct ip */
#include <netinet/ip.h>			/* XXX for struct ip */
#include <netinet/tcp.h>		/* XXX for struct tcphdr */

#include <sys/bus.h>
#include <sys/intr.h>
#include <machine/endian.h>

#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <dev/mii/mii_bitbang.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#include <dev/pci/if_dgereg.h>

/*
 * The receive engine may sometimes become off-by-one when writing back
 * chained descriptors.	 Avoid this by allocating a large chunk of
 * memory and use if instead (to avoid chained descriptors).
 * This only happens with chained descriptors under heavy load.
 */
#define DGE_OFFBYONE_RXBUG

#define DGE_EVENT_COUNTERS
#define DGE_DEBUG

#ifdef DGE_DEBUG
#define DGE_DEBUG_LINK		0x01
#define DGE_DEBUG_TX		0x02
#define DGE_DEBUG_RX		0x04
#define DGE_DEBUG_CKSUM		0x08
int	dge_debug = 0;

#define DPRINTF(x, y)	if (dge_debug & (x)) printf y
#else
#define DPRINTF(x, y)	/* nothing */
#endif /* DGE_DEBUG */

/*
 * Transmit descriptor list size. We allow up to 100 DMA segments per
 * packet (Intel reports of jumbo frame packets with as
 * many as 80 DMA segments when using 16k buffers).
 */
#define DGE_NTXSEGS		100
#define DGE_IFQUEUELEN		20000
#define DGE_TXQUEUELEN		2048
#define DGE_TXQUEUELEN_MASK	(DGE_TXQUEUELEN - 1)
#define DGE_TXQUEUE_GC		(DGE_TXQUEUELEN / 8)
#define DGE_NTXDESC		1024
#define DGE_NTXDESC_MASK		(DGE_NTXDESC - 1)
#define DGE_NEXTTX(x)		(((x) + 1) & DGE_NTXDESC_MASK)
#define DGE_NEXTTXS(x)		(((x) + 1) & DGE_TXQUEUELEN_MASK)

/*
 * Receive descriptor list size.
 * Packet is of size MCLBYTES, and for jumbo packets buffers may
 * be chained.	Due to the nature of the card (high-speed), keep this
 * ring large. With 2k buffers the ring can store 400 jumbo packets,
 * which at full speed will be received in just under 3ms.
 */
#define DGE_NRXDESC		2048
#define DGE_NRXDESC_MASK	(DGE_NRXDESC - 1)
#define DGE_NEXTRX(x)		(((x) + 1) & DGE_NRXDESC_MASK)
/*
 * # of descriptors between head and written descriptors.
 * This is to work-around two erratas.
 */
#define DGE_RXSPACE		10
#define DGE_PREVRX(x)		(((x) - DGE_RXSPACE) & DGE_NRXDESC_MASK)
/*
 * Receive descriptor fetch thresholds. These are values recommended
 * by Intel, do not touch them unless you know what you are doing.
 */
#define RXDCTL_PTHRESH_VAL	128
#define RXDCTL_HTHRESH_VAL	16
#define RXDCTL_WTHRESH_VAL	16


/*
 * Tweakable parameters; default values.
 */
#define FCRTH	0x30000 /* Send XOFF water mark */
#define FCRTL	0x28000 /* Send XON water mark */
#define RDTR	0x20	/* Interrupt delay after receive, .8192us units */
#define TIDV	0x20	/* Interrupt delay after send, .8192us units */

/*
 * Control structures are DMA'd to the i82597 chip.  We allocate them in
 * a single clump that maps to a single DMA segment to make serveral things
 * easier.
 */
struct dge_control_data {
	/*
	 * The transmit descriptors.
	 */
	struct dge_tdes wcd_txdescs[DGE_NTXDESC];

	/*
	 * The receive descriptors.
	 */
	struct dge_rdes wcd_rxdescs[DGE_NRXDESC];
};

#define DGE_CDOFF(x)	offsetof(struct dge_control_data, x)
#define DGE_CDTXOFF(x)	DGE_CDOFF(wcd_txdescs[(x)])
#define DGE_CDRXOFF(x)	DGE_CDOFF(wcd_rxdescs[(x)])

/*
 * The DGE interface have a higher max MTU size than normal jumbo frames.
 */
#define DGE_MAX_MTU	16288	/* Max MTU size for this interface */

/*
 * Software state for transmit jobs.
 */
struct dge_txsoft {
	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
	bus_dmamap_t txs_dmamap;	/* our DMA map */
	int txs_firstdesc;		/* first descriptor in packet */
	int txs_lastdesc;		/* last descriptor in packet */
	int txs_ndesc;			/* # of descriptors used */
};

/*
 * Software state for receive buffers.	Each descriptor gets a
 * 2k (MCLBYTES) buffer and a DMA map.	For packets which fill
 * more than one buffer, we chain them together.
 */
struct dge_rxsoft {
	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
	bus_dmamap_t rxs_dmamap;	/* our DMA map */
};

/*
 * Software state per device.
 */
struct dge_softc {
	device_t sc_dev;		/* generic device information */
	bus_space_tag_t sc_st;		/* bus space tag */
	bus_space_handle_t sc_sh;	/* bus space handle */
	bus_dma_tag_t sc_dmat;		/* bus DMA tag */
	struct ethercom sc_ethercom;	/* ethernet common data */

	int sc_flags;			/* flags; see below */
	int sc_bus_speed;		/* PCI/PCIX bus speed */
	int sc_pcix_offset;		/* PCIX capability register offset */

	const struct dge_product *sc_dgep; /* Pointer to the dge_product entry */
	pci_chipset_tag_t sc_pc;
	pcitag_t sc_pt;
	int sc_mmrbc;			/* Max PCIX memory read byte count */

	void *sc_ih;			/* interrupt cookie */

	struct ifmedia sc_media;

	bus_dmamap_t sc_cddmamap;	/* control data DMA map */
#define sc_cddma	sc_cddmamap->dm_segs[0].ds_addr

	int		sc_align_tweak;

	/*
	 * Software state for the transmit and receive descriptors.
	 */
	struct dge_txsoft sc_txsoft[DGE_TXQUEUELEN];
	struct dge_rxsoft sc_rxsoft[DGE_NRXDESC];

	/*
	 * Control data structures.
	 */
	struct dge_control_data *sc_control_data;
#define sc_txdescs	sc_control_data->wcd_txdescs
#define sc_rxdescs	sc_control_data->wcd_rxdescs

#ifdef DGE_EVENT_COUNTERS
	/* Event counters. */
	struct evcnt sc_ev_txsstall;	/* Tx stalled due to no txs */
	struct evcnt sc_ev_txdstall;	/* Tx stalled due to no txd */
	struct evcnt sc_ev_txforceintr; /* Tx interrupts forced */
	struct evcnt sc_ev_txdw;	/* Tx descriptor interrupts */
	struct evcnt sc_ev_txqe;	/* Tx queue empty interrupts */
	struct evcnt sc_ev_rxintr;	/* Rx interrupts */
	struct evcnt sc_ev_linkintr;	/* Link interrupts */

	struct evcnt sc_ev_rxipsum;	/* IP checksums checked in-bound */
	struct evcnt sc_ev_rxtusum;	/* TCP/UDP cksums checked in-bound */
	struct evcnt sc_ev_txipsum;	/* IP checksums comp. out-bound */
	struct evcnt sc_ev_txtusum;	/* TCP/UDP cksums comp. out-bound */

	struct evcnt sc_ev_txctx_init;	/* Tx cksum context cache initialized */
	struct evcnt sc_ev_txctx_hit;	/* Tx cksum context cache hit */
	struct evcnt sc_ev_txctx_miss;	/* Tx cksum context cache miss */

	struct evcnt sc_ev_txseg[DGE_NTXSEGS]; /* Tx packets w/ N segments */
	struct evcnt sc_ev_txdrop;	/* Tx packets dropped (too many segs) */
#endif /* DGE_EVENT_COUNTERS */

	int	sc_txfree;		/* number of free Tx descriptors */
	int	sc_txnext;		/* next ready Tx descriptor */

	int	sc_txsfree;		/* number of free Tx jobs */
	int	sc_txsnext;		/* next free Tx job */
	int	sc_txsdirty;		/* dirty Tx jobs */

	uint32_t sc_txctx_ipcs;		/* cached Tx IP cksum ctx */
	uint32_t sc_txctx_tucs;		/* cached Tx TCP/UDP cksum ctx */

	int	sc_rxptr;		/* next ready Rx descriptor/queue ent */
	int	sc_rxdiscard;
	int	sc_rxlen;
	struct mbuf *sc_rxhead;
	struct mbuf *sc_rxtail;
	struct mbuf **sc_rxtailp;

	uint32_t sc_ctrl0;		/* prototype CTRL0 register */
	uint32_t sc_icr;		/* prototype interrupt bits */
	uint32_t sc_tctl;		/* prototype TCTL register */
	uint32_t sc_rctl;		/* prototype RCTL register */

	int sc_mchash_type;		/* multicast filter offset */

	uint16_t sc_eeprom[EEPROM_SIZE];

	krndsource_t rnd_source; /* random source */
#ifdef DGE_OFFBYONE_RXBUG
	void *sc_bugbuf;
	SLIST_HEAD(, rxbugentry) sc_buglist;
	bus_dmamap_t sc_bugmap;
	struct rxbugentry *sc_entry;
#endif
};

#define DGE_RXCHAIN_RESET(sc)						\
do {									\
	(sc)->sc_rxtailp = &(sc)->sc_rxhead;				\
	*(sc)->sc_rxtailp = NULL;					\
	(sc)->sc_rxlen = 0;						\
} while (/*CONSTCOND*/0)

#define DGE_RXCHAIN_LINK(sc, m)						\
do {									\
	*(sc)->sc_rxtailp = (sc)->sc_rxtail = (m);			\
	(sc)->sc_rxtailp = &(m)->m_next;				\
} while (/*CONSTCOND*/0)

/* sc_flags */
#define DGE_F_BUS64		0x20	/* bus is 64-bit */
#define DGE_F_PCIX		0x40	/* bus is PCI-X */

#ifdef DGE_EVENT_COUNTERS
#define DGE_EVCNT_INCR(ev)	(ev)->ev_count++
#else
#define DGE_EVCNT_INCR(ev)	/* nothing */
#endif

#define CSR_READ(sc, reg)						\
	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (reg))
#define CSR_WRITE(sc, reg, val)						\
	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (reg), (val))

#define DGE_CDTXADDR(sc, x)	((sc)->sc_cddma + DGE_CDTXOFF((x)))
#define DGE_CDRXADDR(sc, x)	((sc)->sc_cddma + DGE_CDRXOFF((x)))

#define DGE_CDTXSYNC(sc, x, n, ops)					\
do {									\
	int __x, __n;							\
									\
	__x = (x);							\
	__n = (n);							\
									\
	/* If it will wrap around, sync to the end of the ring. */	\
	if ((__x + __n) > DGE_NTXDESC) {				\
		bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,	\
		    DGE_CDTXOFF(__x), sizeof(struct dge_tdes) *		\
		    (DGE_NTXDESC - __x), (ops));			\
		__n -= (DGE_NTXDESC - __x);				\
		__x = 0;						\
	}								\
									\
	/* Now sync whatever is left. */				\
	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
	    DGE_CDTXOFF(__x), sizeof(struct dge_tdes) * __n, (ops));	\
} while (/*CONSTCOND*/0)

#define DGE_CDRXSYNC(sc, x, ops)						\
do {									\
	bus_dmamap_sync((sc)->sc_dmat, (sc)->sc_cddmamap,		\
	   DGE_CDRXOFF((x)), sizeof(struct dge_rdes), (ops));		\
} while (/*CONSTCOND*/0)

#ifdef DGE_OFFBYONE_RXBUG
#define DGE_INIT_RXDESC(sc, x)						\
do {									\
	struct dge_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
	struct dge_rdes *__rxd = &(sc)->sc_rxdescs[(x)];		\
	struct mbuf *__m = __rxs->rxs_mbuf;				\
	const bus_addr_t __rxaddr = sc->sc_bugmap->dm_segs[0].ds_addr +	\
	    (mtod((__m), char *) - (char *)sc->sc_bugbuf);		\
									\
	__rxd->dr_baddrl = htole32(__rxaddr);				\
	__rxd->dr_baddrh = htole32(((uint64_t)__rxaddr) >> 32);		\
	__rxd->dr_len = 0;						\
	__rxd->dr_cksum = 0;						\
	__rxd->dr_status = 0;						\
	__rxd->dr_errors = 0;						\
	__rxd->dr_special = 0;						\
	DGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); \
									\
	CSR_WRITE((sc), DGE_RDT, (x));					\
} while (/*CONSTCOND*/0)
#else
#define DGE_INIT_RXDESC(sc, x)						\
do {									\
	struct dge_rxsoft *__rxs = &(sc)->sc_rxsoft[(x)];		\
	struct dge_rdes *__rxd = &(sc)->sc_rxdescs[(x)];		\
	struct mbuf *__m = __rxs->rxs_mbuf;				\
									\
	/*								\
	 * Note: We scoot the packet forward 2 bytes in the buffer	\
	 * so that the payload after the Ethernet header is aligned	\
	 * to a 4-byte boundary.					\
	 *								\
	 * XXX BRAINDAMAGE ALERT!					\
	 * The stupid chip uses the same size for every buffer, which	\
	 * is set in the Receive Control register.  We are using the 2K \
	 * size option, but what we REALLY want is (2K - 2)!  For this	\
	 * reason, we can't "scoot" packets longer than the standard	\
	 * Ethernet MTU.  On strict-alignment platforms, if the total	\
	 * size exceeds (2K - 2) we set align_tweak to 0 and let	\
	 * the upper layer copy the headers.				\
	 */								\
	__m->m_data = __m->m_ext.ext_buf + (sc)->sc_align_tweak;	\
									\
	const bus_addr_t __rxaddr =					\
	    __rxs->rxs_dmamap->dm_segs[0].ds_addr +			\
	    (sc)->sc_align_tweak;					\
									\
	__rxd->dr_baddrl = htole32(__rxaddr);				\
	__rxd->dr_baddrh = htole32(((uint64_t)__rxaddr) >> 32);		\
	__rxd->dr_len = 0;						\
	__rxd->dr_cksum = 0;						\
	__rxd->dr_status = 0;						\
	__rxd->dr_errors = 0;						\
	__rxd->dr_special = 0;						\
	DGE_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); \
									\
	CSR_WRITE((sc), DGE_RDT, (x));					\
} while (/*CONSTCOND*/0)
#endif

#ifdef DGE_OFFBYONE_RXBUG
/*
 * Allocation constants.  Much memory may be used for this.
 */
#ifndef DGE_BUFFER_SIZE
#define DGE_BUFFER_SIZE DGE_MAX_MTU
#endif
#define DGE_NBUFFERS	(4*DGE_NRXDESC)
#define DGE_RXMEM	(DGE_NBUFFERS*DGE_BUFFER_SIZE)

struct rxbugentry {
	SLIST_ENTRY(rxbugentry) rb_entry;
	int rb_slot;
};

static int
dge_alloc_rcvmem(struct dge_softc *sc)
{
	char *kva;
	bus_dma_segment_t seg;
	int i, rseg, state, error;
	struct rxbugentry *entry;

	state = error = 0;

	if (bus_dmamem_alloc(sc->sc_dmat, DGE_RXMEM, PAGE_SIZE, 0,
	     &seg, 1, &rseg, BUS_DMA_NOWAIT)) {
		aprint_error_dev(sc->sc_dev, "can't alloc rx buffers\n");
		return ENOBUFS;
	}

	state = 1;
	if (bus_dmamem_map(sc->sc_dmat, &seg, rseg, DGE_RXMEM, (void **)&kva,
	    BUS_DMA_NOWAIT)) {
		aprint_error_dev(sc->sc_dev,
		    "can't map DMA buffers (%d bytes)\n", (int)DGE_RXMEM);
		error = ENOBUFS;
		goto out;
	}

	state = 2;
	if (bus_dmamap_create(sc->sc_dmat, DGE_RXMEM, 1, DGE_RXMEM, 0,
	    BUS_DMA_NOWAIT, &sc->sc_bugmap)) {
		aprint_error_dev(sc->sc_dev, "can't create DMA map\n");
		error = ENOBUFS;
		goto out;
	}

	state = 3;
	if (bus_dmamap_load(sc->sc_dmat, sc->sc_bugmap,
	    kva, DGE_RXMEM, NULL, BUS_DMA_NOWAIT)) {
		aprint_error_dev(sc->sc_dev, "can't load DMA map\n");
		error = ENOBUFS;
		goto out;
	}

	state = 4;
	sc->sc_bugbuf = (void *)kva;
	SLIST_INIT(&sc->sc_buglist);

	/*
	 * Now divide it up into DGE_BUFFER_SIZE pieces and save the addresses
	 * in an array.
	 */
	entry = malloc(sizeof(*entry) * DGE_NBUFFERS, M_DEVBUF, M_WAITOK);
	sc->sc_entry = entry;
	for (i = 0; i < DGE_NBUFFERS; i++) {
		entry[i].rb_slot = i;
		SLIST_INSERT_HEAD(&sc->sc_buglist, &entry[i], rb_entry);
	}
out:
	if (error != 0) {
		switch (state) {
		case 4:
			bus_dmamap_unload(sc->sc_dmat, sc->sc_bugmap);
			/* FALLTHROUGH */
		case 3:
			bus_dmamap_destroy(sc->sc_dmat, sc->sc_bugmap);
			/* FALLTHROUGH */
		case 2:
			bus_dmamem_unmap(sc->sc_dmat, kva, DGE_RXMEM);
			/* FALLTHROUGH */
		case 1:
			bus_dmamem_free(sc->sc_dmat, &seg, rseg);
			break;
		default:
			break;
		}
	}

	return error;
}

/*
 * Allocate a jumbo buffer.
 */
static void *
dge_getbuf(struct dge_softc *sc)
{
	struct rxbugentry *entry;

	entry = SLIST_FIRST(&sc->sc_buglist);

	if (entry == NULL) {
		printf("%s: no free RX buffers\n", device_xname(sc->sc_dev));
		return NULL;
	}

	SLIST_REMOVE_HEAD(&sc->sc_buglist, rb_entry);
	return (char *)sc->sc_bugbuf + entry->rb_slot * DGE_BUFFER_SIZE;
}

/*
 * Release a jumbo buffer.
 */
static void
dge_freebuf(struct mbuf *m, void *buf, size_t size, void *arg)
{
	struct rxbugentry *entry;
	struct dge_softc *sc;
	int i, s;

	/* Extract the softc struct pointer. */
	sc = (struct dge_softc *)arg;

	if (sc == NULL)
		panic("dge_freebuf: can't find softc pointer!");

	/* calculate the slot this buffer belongs to */

	i = ((char *)buf - (char *)sc->sc_bugbuf) / DGE_BUFFER_SIZE;

	if ((i < 0) || (i >= DGE_NBUFFERS))
		panic("dge_freebuf: asked to free buffer %d!", i);

	s = splvm();
	entry = sc->sc_entry + i;
	SLIST_INSERT_HEAD(&sc->sc_buglist, entry, rb_entry);

	if (__predict_true(m != NULL))
		pool_cache_put(mb_cache, m);
	splx(s);
}
#endif

static void	dge_start(struct ifnet *);
static void	dge_watchdog(struct ifnet *);
static int	dge_ioctl(struct ifnet *, u_long, void *);
static int	dge_init(struct ifnet *);
static void	dge_stop(struct ifnet *, int);

static bool	dge_shutdown(device_t, int);

static void	dge_reset(struct dge_softc *);
static void	dge_rxdrain(struct dge_softc *);
static int	dge_add_rxbuf(struct dge_softc *, int);

static void	dge_set_filter(struct dge_softc *);

static int	dge_intr(void *);
static void	dge_txintr(struct dge_softc *);
static void	dge_rxintr(struct dge_softc *);
static void	dge_linkintr(struct dge_softc *, uint32_t);

static int	dge_match(device_t, cfdata_t, void *);
static void	dge_attach(device_t, device_t, void *);

static int	dge_read_eeprom(struct dge_softc *sc);
static int	dge_eeprom_clockin(struct dge_softc *sc);
static void	dge_eeprom_clockout(struct dge_softc *sc, int bit);
static uint16_t	dge_eeprom_word(struct dge_softc *sc, int addr);
static int	dge_xgmii_mediachange(struct ifnet *);
static void	dge_xgmii_mediastatus(struct ifnet *, struct ifmediareq *);
static void	dge_xgmii_reset(struct dge_softc *);
static void	dge_xgmii_writereg(struct dge_softc *, int, int, int);


CFATTACH_DECL_NEW(dge, sizeof(struct dge_softc),
    dge_match, dge_attach, NULL, NULL);

#ifdef DGE_EVENT_COUNTERS
#if DGE_NTXSEGS > 100
#error Update dge_txseg_evcnt_names
#endif
static char (*dge_txseg_evcnt_names)[DGE_NTXSEGS][8 /* "txseg00" + \0 */];
#endif /* DGE_EVENT_COUNTERS */

/*
 * Devices supported by this driver.
 */
struct dge_product {
	const char *name;
	int flags;
#define DGEP_F_10G_LR	  0x01
#define DGEP_F_10G_SR	  0x02
};

static const struct dge_product i82597EX_lr = {
	.name = "Intel i82597EX 10GbE-LR Ethernet",
	.flags = DGEP_F_10G_LR
};

static const struct dge_product i82597EX_sr = {
	.name = "Intel i82597EX 10GbE-SR Ethernet",
	.flags = DGEP_F_10G_SR
};

static const struct device_compatible_entry compat_data[] = {
	{ .id = PCI_ID_CODE(PCI_VENDOR_INTEL,
		PCI_PRODUCT_INTEL_82597EX),
	  .data = &i82597EX_lr },

	{ .id = PCI_ID_CODE(PCI_VENDOR_INTEL,
		PCI_PRODUCT_INTEL_82597EX_SR),
	  .data = &i82597EX_sr },

	PCI_COMPAT_EOL
};

static int
dge_match(device_t parent, cfdata_t cf, void *aux)
{
	struct pci_attach_args *pa = aux;

	return pci_compatible_match(pa, compat_data);
}

static void
dge_attach(device_t parent, device_t self, void *aux)
{
	struct dge_softc *sc = device_private(self);
	struct pci_attach_args *pa = aux;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	pci_chipset_tag_t pc = pa->pa_pc;
	pci_intr_handle_t ih;
	const char *intrstr = NULL;
	bus_dma_segment_t seg;
	int i, rseg, error;
	uint8_t enaddr[ETHER_ADDR_LEN];
	pcireg_t preg, memtype;
	uint32_t reg;
	char intrbuf[PCI_INTRSTR_LEN];
	const struct device_compatible_entry *dce;
	const struct dge_product *dgep;

	dce = pci_compatible_lookup(pa, compat_data);
	KASSERT(dce != NULL);
	sc->sc_dgep = dgep = dce->data;

	sc->sc_dev = self;
	sc->sc_pc = pa->pa_pc;
	sc->sc_pt = pa->pa_tag;

	if (pci_dma64_available(pa))
		sc->sc_dmat = pa->pa_dmat64;
	else
		sc->sc_dmat = pa->pa_dmat;

	pci_aprint_devinfo_fancy(pa, "Ethernet controller",
		dgep->name, 1);

	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, DGE_PCI_BAR);
	if (pci_mapreg_map(pa, DGE_PCI_BAR, memtype, 0,
	    &sc->sc_st, &sc->sc_sh, NULL, NULL)) {
		aprint_error_dev(sc->sc_dev,
		    "unable to map device registers\n");
		return;
	}

	/* Enable bus mastering */
	preg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
	preg |= PCI_COMMAND_MASTER_ENABLE;
	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, preg);

	/*
	 * Map and establish our interrupt.
	 */
	if (pci_intr_map(pa, &ih)) {
		aprint_error_dev(sc->sc_dev, "unable to map interrupt\n");
		return;
	}
	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
	sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_NET, dge_intr, sc,
	    device_xname(self));
	if (sc->sc_ih == NULL) {
		aprint_error_dev(sc->sc_dev, "unable to establish interrupt");
		if (intrstr != NULL)
			aprint_error(" at %s", intrstr);
		aprint_error("\n");
		return;
	}
	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);

	/*
	 * Determine a few things about the bus we're connected to.
	 */
	reg = CSR_READ(sc, DGE_STATUS);
	if (reg & STATUS_BUS64)
		sc->sc_flags |= DGE_F_BUS64;

	sc->sc_flags |= DGE_F_PCIX;
	if (pci_get_capability(pa->pa_pc, pa->pa_tag,
			       PCI_CAP_PCIX,
			       &sc->sc_pcix_offset, NULL) == 0)
		aprint_error_dev(sc->sc_dev, "unable to find PCIX "
		    "capability\n");

	if (sc->sc_flags & DGE_F_PCIX) {
		switch (reg & STATUS_PCIX_MSK) {
		case STATUS_PCIX_66:
			sc->sc_bus_speed = 66;
			break;
		case STATUS_PCIX_100:
			sc->sc_bus_speed = 100;
			break;
		case STATUS_PCIX_133:
			sc->sc_bus_speed = 133;
			break;
		default:
			aprint_error_dev(sc->sc_dev,
			    "unknown PCIXSPD %d; assuming 66MHz\n",
			    reg & STATUS_PCIX_MSK);
			sc->sc_bus_speed = 66;
		}
	} else
		sc->sc_bus_speed = (reg & STATUS_BUS64) ? 66 : 33;
	aprint_verbose_dev(sc->sc_dev, "%d-bit %dMHz %s bus\n",
	    (sc->sc_flags & DGE_F_BUS64) ? 64 : 32, sc->sc_bus_speed,
	    (sc->sc_flags & DGE_F_PCIX) ? "PCIX" : "PCI");

	/*
	 * Allocate the control data structures, and create and load the
	 * DMA map for it.
	 */
	if ((error = bus_dmamem_alloc(sc->sc_dmat,
	    sizeof(struct dge_control_data), PAGE_SIZE, 0, &seg, 1, &rseg,
	    0)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "unable to allocate control data, error = %d\n",
		    error);
		goto fail_0;
	}

	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
	    sizeof(struct dge_control_data), (void **)&sc->sc_control_data,
	    0)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "unable to map control data, error = %d\n", error);
		goto fail_1;
	}

	if ((error = bus_dmamap_create(sc->sc_dmat,
	    sizeof(struct dge_control_data), 1,
	    sizeof(struct dge_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
		aprint_error_dev(sc->sc_dev, "unable to create control data "
		    "DMA map, error = %d\n", error);
		goto fail_2;
	}

	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
	    sc->sc_control_data, sizeof(struct dge_control_data), NULL,
	    0)) != 0) {
		aprint_error_dev(sc->sc_dev,
		    "unable to load control data DMA map, error = %d\n",
		    error);
		goto fail_3;
	}

#ifdef DGE_OFFBYONE_RXBUG
	if (dge_alloc_rcvmem(sc) != 0)
		return; /* Already complained */
#endif
	/*
	 * Create the transmit buffer DMA maps.
	 */
	for (i = 0; i < DGE_TXQUEUELEN; i++) {
		if ((error = bus_dmamap_create(sc->sc_dmat, DGE_MAX_MTU,
		    DGE_NTXSEGS, MCLBYTES, 0, 0,
		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
			aprint_error_dev(sc->sc_dev, "unable to create Tx DMA map %d, "
			    "error = %d\n", i, error);
			goto fail_4;
		}
	}

	/*
	 * Create the receive buffer DMA maps.
	 */
	for (i = 0; i < DGE_NRXDESC; i++) {
#ifdef DGE_OFFBYONE_RXBUG
		if ((error = bus_dmamap_create(sc->sc_dmat, DGE_BUFFER_SIZE, 1,
		    DGE_BUFFER_SIZE, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
#else
		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
#endif
			aprint_error_dev(sc->sc_dev, "unable to create Rx DMA "
			    "map %d, error = %d\n", i, error);
			goto fail_5;
		}
		sc->sc_rxsoft[i].rxs_mbuf = NULL;
	}

	/*
	 * Set bits in ctrl0 register.
	 * Should get the software defined pins out of EEPROM?
	 */
	sc->sc_ctrl0 |= CTRL0_RPE | CTRL0_TPE; /* XON/XOFF */
	sc->sc_ctrl0 |= CTRL0_SDP3_DIR | CTRL0_SDP2_DIR | CTRL0_SDP1_DIR |
	    CTRL0_SDP0_DIR | CTRL0_SDP3 | CTRL0_SDP2 | CTRL0_SDP0;

	/*
	 * Reset the chip to a known state.
	 */
	dge_reset(sc);

	/*
	 * Reset the PHY.
	 */
	dge_xgmii_reset(sc);

	/*
	 * Read in EEPROM data.
	 */
	if (dge_read_eeprom(sc)) {
		aprint_error_dev(sc->sc_dev, "couldn't read EEPROM\n");
		return;
	}

	/*
	 * Get the ethernet address.
	 */
	enaddr[0] = sc->sc_eeprom[EE_ADDR01] & 0377;
	enaddr[1] = sc->sc_eeprom[EE_ADDR01] >> 8;
	enaddr[2] = sc->sc_eeprom[EE_ADDR23] & 0377;
	enaddr[3] = sc->sc_eeprom[EE_ADDR23] >> 8;
	enaddr[4] = sc->sc_eeprom[EE_ADDR45] & 0377;
	enaddr[5] = sc->sc_eeprom[EE_ADDR45] >> 8;

	aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
	    ether_sprintf(enaddr));

	/*
	 * Setup media stuff.
	 */
	sc->sc_ethercom.ec_ifmedia = &sc->sc_media;
	ifmedia_init(&sc->sc_media, IFM_IMASK, dge_xgmii_mediachange,
	    dge_xgmii_mediastatus);
	if (dgep->flags & DGEP_F_10G_SR) {
		ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_SR, 0, NULL);
		ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_10G_SR);
	} else { /* XXX default is LR */
		ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_10G_LR, 0, NULL);
		ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_10G_LR);
	}

	ifp = &sc->sc_ethercom.ec_if;
	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
	ifp->if_softc = sc;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	ifp->if_ioctl = dge_ioctl;
	ifp->if_start = dge_start;
	ifp->if_watchdog = dge_watchdog;
	ifp->if_init = dge_init;
	ifp->if_stop = dge_stop;
	IFQ_SET_MAXLEN(&ifp->if_snd, uimax(DGE_IFQUEUELEN, IFQ_MAXLEN));
	IFQ_SET_READY(&ifp->if_snd);

	sc->sc_ethercom.ec_capabilities |=
	    ETHERCAP_JUMBO_MTU | ETHERCAP_VLAN_MTU;

	/*
	 * We can perform TCPv4 and UDPv4 checksums in-bound.
	 */
	ifp->if_capabilities |=
	    IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx |
	    IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx |
	    IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx;

	/*
	 * Attach the interface.
	 */
	if_attach(ifp);
	if_deferred_start_init(ifp, NULL);
	ether_ifattach(ifp, enaddr);
	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
	    RND_TYPE_NET, RND_FLAG_DEFAULT);

#ifdef DGE_EVENT_COUNTERS
	/* Fix segment event naming */
	if (dge_txseg_evcnt_names == NULL) {
		dge_txseg_evcnt_names =
		    malloc(sizeof(*dge_txseg_evcnt_names), M_DEVBUF, M_WAITOK);
		for (i = 0; i < DGE_NTXSEGS; i++)
			snprintf((*dge_txseg_evcnt_names)[i],
			    sizeof((*dge_txseg_evcnt_names)[i]), "txseg%d", i);
	}

	/* Attach event counters. */
	evcnt_attach_dynamic(&sc->sc_ev_txsstall, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txsstall");
	evcnt_attach_dynamic(&sc->sc_ev_txdstall, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txdstall");
	evcnt_attach_dynamic(&sc->sc_ev_txforceintr, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txforceintr");
	evcnt_attach_dynamic(&sc->sc_ev_txdw, EVCNT_TYPE_INTR,
	    NULL, device_xname(sc->sc_dev), "txdw");
	evcnt_attach_dynamic(&sc->sc_ev_txqe, EVCNT_TYPE_INTR,
	    NULL, device_xname(sc->sc_dev), "txqe");
	evcnt_attach_dynamic(&sc->sc_ev_rxintr, EVCNT_TYPE_INTR,
	    NULL, device_xname(sc->sc_dev), "rxintr");
	evcnt_attach_dynamic(&sc->sc_ev_linkintr, EVCNT_TYPE_INTR,
	    NULL, device_xname(sc->sc_dev), "linkintr");

	evcnt_attach_dynamic(&sc->sc_ev_rxipsum, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "rxipsum");
	evcnt_attach_dynamic(&sc->sc_ev_rxtusum, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "rxtusum");
	evcnt_attach_dynamic(&sc->sc_ev_txipsum, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txipsum");
	evcnt_attach_dynamic(&sc->sc_ev_txtusum, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txtusum");

	evcnt_attach_dynamic(&sc->sc_ev_txctx_init, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txctx init");
	evcnt_attach_dynamic(&sc->sc_ev_txctx_hit, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txctx hit");
	evcnt_attach_dynamic(&sc->sc_ev_txctx_miss, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txctx miss");

	for (i = 0; i < DGE_NTXSEGS; i++)
		evcnt_attach_dynamic(&sc->sc_ev_txseg[i], EVCNT_TYPE_MISC,
		    NULL, device_xname(sc->sc_dev), (*dge_txseg_evcnt_names)[i]);

	evcnt_attach_dynamic(&sc->sc_ev_txdrop, EVCNT_TYPE_MISC,
	    NULL, device_xname(sc->sc_dev), "txdrop");

#endif /* DGE_EVENT_COUNTERS */

	/*
	 * Make sure the interface is shutdown during reboot.
	 */
	if (pmf_device_register1(self, NULL, NULL, dge_shutdown))
		pmf_class_network_register(self, ifp);
	else
		aprint_error_dev(self, "couldn't establish power handler\n");

	return;

	/*
	 * Free any resources we've allocated during the failed attach
	 * attempt.  Do this in reverse order and fall through.
	 */
 fail_5:
	for (i = 0; i < DGE_NRXDESC; i++) {
		if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
			bus_dmamap_destroy(sc->sc_dmat,
			    sc->sc_rxsoft[i].rxs_dmamap);
	}
 fail_4:
	for (i = 0; i < DGE_TXQUEUELEN; i++) {
		if (sc->sc_txsoft[i].txs_dmamap != NULL)
			bus_dmamap_destroy(sc->sc_dmat,
			    sc->sc_txsoft[i].txs_dmamap);
	}
	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
 fail_3:
	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
 fail_2:
	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
	    sizeof(struct dge_control_data));
 fail_1:
	bus_dmamem_free(sc->sc_dmat, &seg, rseg);
 fail_0:
	return;
}

/*
 * dge_shutdown:
 *
 *	Make sure the interface is stopped at reboot time.
 */
static bool
dge_shutdown(device_t self, int howto)
{
	struct dge_softc *sc;

	sc = device_private(self);
	dge_stop(&sc->sc_ethercom.ec_if, 1);

	return true;
}

/*
 * dge_tx_cksum:
 *
 *	Set up TCP/IP checksumming parameters for the
 *	specified packet.
 */
static int
dge_tx_cksum(struct dge_softc *sc, struct dge_txsoft *txs, uint8_t *fieldsp)
{
	struct mbuf *m0 = txs->txs_mbuf;
	struct dge_ctdes *t;
	uint32_t ipcs, tucs;
	struct ether_header *eh;
	int offset, iphl;
	uint8_t fields = 0;

	/*
	 * XXX It would be nice if the mbuf pkthdr had offset
	 * fields for the protocol headers.
	 */

	eh = mtod(m0, struct ether_header *);
	switch (htons(eh->ether_type)) {
	case ETHERTYPE_IP:
		offset = ETHER_HDR_LEN;
		break;

	case ETHERTYPE_VLAN:
		offset = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
		break;

	default:
		/*
		 * Don't support this protocol or encapsulation.
		 */
		*fieldsp = 0;
		return 0;
	}

	iphl = M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);

	/*
	 * NOTE: Even if we're not using the IP or TCP/UDP checksum
	 * offload feature, if we load the context descriptor, we
	 * MUST provide valid values for IPCSS and TUCSS fields.
	 */

	if (m0->m_pkthdr.csum_flags & M_CSUM_IPv4) {
		DGE_EVCNT_INCR(&sc->sc_ev_txipsum);
		fields |= TDESC_POPTS_IXSM;
		ipcs = DGE_TCPIP_IPCSS(offset) |
		    DGE_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
		    DGE_TCPIP_IPCSE(offset + iphl - 1);
	} else if (__predict_true(sc->sc_txctx_ipcs != 0xffffffff)) {
		/* Use the cached value. */
		ipcs = sc->sc_txctx_ipcs;
	} else {
		/* Just initialize it to the likely value anyway. */
		ipcs = DGE_TCPIP_IPCSS(offset) |
		    DGE_TCPIP_IPCSO(offset + offsetof(struct ip, ip_sum)) |
		    DGE_TCPIP_IPCSE(offset + iphl - 1);
	}
	DPRINTF(DGE_DEBUG_CKSUM,
	    ("%s: CKSUM: offset %d ipcs 0x%x\n",
	    device_xname(sc->sc_dev), offset, ipcs));

	offset += iphl;

	if (m0->m_pkthdr.csum_flags & (M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
		DGE_EVCNT_INCR(&sc->sc_ev_txtusum);
		fields |= TDESC_POPTS_TXSM;
		tucs = DGE_TCPIP_TUCSS(offset) |
		   DGE_TCPIP_TUCSO(offset + M_CSUM_DATA_IPv4_OFFSET(m0->m_pkthdr.csum_data)) |
		   DGE_TCPIP_TUCSE(0) /* rest of packet */;
	} else if (__predict_true(sc->sc_txctx_tucs != 0xffffffff)) {
		/* Use the cached value. */
		tucs = sc->sc_txctx_tucs;
	} else {
		/* Just initialize it to a valid TCP context. */
		tucs = DGE_TCPIP_TUCSS(offset) |
		    DGE_TCPIP_TUCSO(offset + offsetof(struct tcphdr, th_sum)) |
		    DGE_TCPIP_TUCSE(0) /* rest of packet */;
	}

	DPRINTF(DGE_DEBUG_CKSUM,
	    ("%s: CKSUM: offset %d tucs 0x%x\n",
	    device_xname(sc->sc_dev), offset, tucs));

	if (sc->sc_txctx_ipcs == ipcs &&
	    sc->sc_txctx_tucs == tucs) {
		/* Cached context is fine. */
		DGE_EVCNT_INCR(&sc->sc_ev_txctx_hit);
	} else {
		/* Fill in the context descriptor. */
#ifdef DGE_EVENT_COUNTERS
		if (sc->sc_txctx_ipcs == 0xffffffff &&
		    sc->sc_txctx_tucs == 0xffffffff)
			DGE_EVCNT_INCR(&sc->sc_ev_txctx_init);
		else
			DGE_EVCNT_INCR(&sc->sc_ev_txctx_miss);
#endif
		t = (struct dge_ctdes *)&sc->sc_txdescs[sc->sc_txnext];
		t->dc_tcpip_ipcs = htole32(ipcs);
		t->dc_tcpip_tucs = htole32(tucs);
		t->dc_tcpip_cmdlen = htole32(TDESC_DTYP_CTD);
		t->dc_tcpip_seg = 0;
		DGE_CDTXSYNC(sc, sc->sc_txnext, 1, BUS_DMASYNC_PREWRITE);

		sc->sc_txctx_ipcs = ipcs;
		sc->sc_txctx_tucs = tucs;

		sc->sc_txnext = DGE_NEXTTX(sc->sc_txnext);
		txs->txs_ndesc++;
	}

	*fieldsp = fields;

	return 0;
}

/*
 * dge_start:		[ifnet interface function]
 *
 *	Start packet transmission on the interface.
 */
static void
dge_start(struct ifnet *ifp)
{
	struct dge_softc *sc = ifp->if_softc;
	struct mbuf *m0;
	struct dge_txsoft *txs;
	bus_dmamap_t dmamap;
	int error, nexttx, lasttx = -1, ofree, seg;
	uint32_t cksumcmd;
	uint8_t cksumfields;

	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
		return;

	/*
	 * Remember the previous number of free descriptors.
	 */
	ofree = sc->sc_txfree;

	/*
	 * Loop through the send queue, setting up transmit descriptors
	 * until we drain the queue, or use up all available transmit
	 * descriptors.
	 */
	for (;;) {
		/* Grab a packet off the queue. */
		IFQ_POLL(&ifp->if_snd, m0);
		if (m0 == NULL)
			break;

		DPRINTF(DGE_DEBUG_TX,
		    ("%s: TX: have packet to transmit: %p\n",
		    device_xname(sc->sc_dev), m0));

		/* Get a work queue entry. */
		if (sc->sc_txsfree < DGE_TXQUEUE_GC) {
			dge_txintr(sc);
			if (sc->sc_txsfree == 0) {
				DPRINTF(DGE_DEBUG_TX,
				    ("%s: TX: no free job descriptors\n",
					device_xname(sc->sc_dev)));
				DGE_EVCNT_INCR(&sc->sc_ev_txsstall);
				break;
			}
		}

		txs = &sc->sc_txsoft[sc->sc_txsnext];
		dmamap = txs->txs_dmamap;

		/*
		 * Load the DMA map.  If this fails, the packet either
		 * didn't fit in the allotted number of segments, or we
		 * were short on resources.  For the too-many-segments
		 * case, we simply report an error and drop the packet,
		 * since we can't sanely copy a jumbo packet to a single
		 * buffer.
		 */
		error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
		    BUS_DMA_WRITE | BUS_DMA_NOWAIT);
		if (error) {
			if (error == EFBIG) {
				DGE_EVCNT_INCR(&sc->sc_ev_txdrop);
				printf("%s: Tx packet consumes too many "
				    "DMA segments, dropping...\n",
				    device_xname(sc->sc_dev));
				IFQ_DEQUEUE(&ifp->if_snd, m0);
				m_freem(m0);
				continue;
			}
			/*
			 * Short on resources, just stop for now.
			 */
			DPRINTF(DGE_DEBUG_TX,
			    ("%s: TX: dmamap load failed: %d\n",
			    device_xname(sc->sc_dev), error));
			break;
		}

		/*
		 * Ensure we have enough descriptors free to describe
		 * the packet.  Note, we always reserve one descriptor
		 * at the end of the ring due to the semantics of the
		 * TDT register, plus one more in the event we need
		 * to re-load checksum offload context.
		 */
		if (dmamap->dm_nsegs > (sc->sc_txfree - 2)) {
			/*
			 * Not enough free descriptors to transmit this
			 * packet.  We haven't committed anything yet,
			 * so just unload the DMA map, put the packet
			 * pack on the queue, and punt.  Notify the upper
			 * layer that there are no more slots left.
			 */
			DPRINTF(DGE_DEBUG_TX,
			    ("%s: TX: need %d descriptors, have %d\n",
			    device_xname(sc->sc_dev), dmamap->dm_nsegs,
			    sc->sc_txfree - 1));
			ifp->if_flags |= IFF_OACTIVE;
			bus_dmamap_unload(sc->sc_dmat, dmamap);
			DGE_EVCNT_INCR(&sc->sc_ev_txdstall);
			break;
		}

		IFQ_DEQUEUE(&ifp->if_snd, m0);

		/*
		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
		 */

		/* Sync the DMA map. */
		bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
		    BUS_DMASYNC_PREWRITE);

		DPRINTF(DGE_DEBUG_TX,
		    ("%s: TX: packet has %d DMA segments\n",
		    device_xname(sc->sc_dev), dmamap->dm_nsegs));

		DGE_EVCNT_INCR(&sc->sc_ev_txseg[dmamap->dm_nsegs - 1]);

		/*
		 * Store a pointer to the packet so that we can free it
		 * later.
		 *
		 * Initially, we consider the number of descriptors the
		 * packet uses the number of DMA segments.  This may be
		 * incremented by 1 if we do checksum offload (a descriptor
		 * is used to set the checksum context).
		 */
		txs->txs_mbuf = m0;
		txs->txs_firstdesc = sc->sc_txnext;
		txs->txs_ndesc = dmamap->dm_nsegs;

		/*
		 * Set up checksum offload parameters for
		 * this packet.
		 */
		if (m0->m_pkthdr.csum_flags &
		    (M_CSUM_IPv4 | M_CSUM_TCPv4 | M_CSUM_UDPv4)) {
			if (dge_tx_cksum(sc, txs, &cksumfields) != 0) {
				/* Error message already displayed. */
				bus_dmamap_unload(sc->sc_dmat, dmamap);
				continue;
			}
		} else {
			cksumfields = 0;
		}

		cksumcmd = TDESC_DCMD_IDE | TDESC_DTYP_DATA;

		/*
		 * Initialize the transmit descriptor.
		 */
		for (nexttx = sc->sc_txnext, seg = 0;
		     seg < dmamap->dm_nsegs;
		     seg++, nexttx = DGE_NEXTTX(nexttx)) {
			sc->sc_txdescs[nexttx].dt_baddrh =
			    htole32(((uint64_t)dmamap->dm_segs[seg].ds_addr) >> 32);
			sc->sc_txdescs[nexttx].dt_baddrl =
			    htole32(dmamap->dm_segs[seg].ds_addr);
			sc->sc_txdescs[nexttx].dt_ctl =
			    htole32(cksumcmd | dmamap->dm_segs[seg].ds_len);
			sc->sc_txdescs[nexttx].dt_status = 0;
			sc->sc_txdescs[nexttx].dt_popts = cksumfields;
			sc->sc_txdescs[nexttx].dt_vlan = 0;
			lasttx = nexttx;

			DPRINTF(DGE_DEBUG_TX,
			    ("%s: TX: desc %d: high 0x%08lx, low 0x%08lx, len 0x%04lx\n",
			    device_xname(sc->sc_dev), nexttx,
			    (unsigned long)(((uint64_t)dmamap->dm_segs[seg].ds_addr) >> 32),
			    (unsigned long)((uint32_t)dmamap->dm_segs[seg].ds_addr),
			    (unsigned long)dmamap->dm_segs[seg].ds_len));
		}

		KASSERT(lasttx != -1);

		/*
		 * Set up the command byte on the last descriptor of
		 * the packet.  If we're in the interrupt delay window,
		 * delay the interrupt.
		 */
		sc->sc_txdescs[lasttx].dt_ctl |=
		    htole32(TDESC_DCMD_EOP | TDESC_DCMD_RS);

		txs->txs_lastdesc = lasttx;

		DPRINTF(DGE_DEBUG_TX,
		    ("%s: TX: desc %d: cmdlen 0x%08x\n", device_xname(sc->sc_dev),
		    lasttx, le32toh(sc->sc_txdescs[lasttx].dt_ctl)));

		/* Sync the descriptors we're using. */
		DGE_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);

		/* Give the packet to the chip. */
		CSR_WRITE(sc, DGE_TDT, nexttx);

		DPRINTF(DGE_DEBUG_TX,
		    ("%s: TX: TDT -> %d\n", device_xname(sc->sc_dev), nexttx));

		DPRINTF(DGE_DEBUG_TX,
		    ("%s: TX: finished transmitting packet, job %d\n",
		    device_xname(sc->sc_dev), sc->sc_txsnext));

		/* Advance the tx pointer. */
		sc->sc_txfree -= txs->txs_ndesc;
		sc->sc_txnext = nexttx;

		sc->sc_txsfree--;
		sc->sc_txsnext = DGE_NEXTTXS(sc->sc_txsnext);

		/* Pass the packet to any BPF listeners. */
		bpf_mtap(ifp, m0, BPF_D_OUT);
	}

	if (sc->sc_txsfree == 0 || sc->sc_txfree <= 2) {
		/* No more slots; notify upper layer. */
		ifp->if_flags |= IFF_OACTIVE;
	}

	if (sc->sc_txfree != ofree) {
		/* Set a watchdog timer in case the chip flakes out. */
		ifp->if_timer = 5;
	}
}

/*
 * dge_watchdog:		[ifnet interface function]
 *
 *	Watchdog timer handler.
 */
static void
dge_watchdog(struct ifnet *ifp)
{
	struct dge_softc *sc = ifp->if_softc;

	/*
	 * Since we're using delayed interrupts, sweep up
	 * before we report an error.
	 */
	dge_txintr(sc);

	if (sc->sc_txfree != DGE_NTXDESC) {
		printf("%s: device timeout (txfree %d txsfree %d txnext %d)\n",
		    device_xname(sc->sc_dev), sc->sc_txfree, sc->sc_txsfree,
		    sc->sc_txnext);
		if_statinc(ifp, if_oerrors);

		/* Reset the interface. */
		(void) dge_init(ifp);
	}

	/* Try to get more packets going. */
	dge_start(ifp);
}

/*
 * dge_ioctl:		[ifnet interface function]
 *
 *	Handle control requests from the operator.
 */
static int
dge_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct dge_softc *sc = ifp->if_softc;
	struct ifreq *ifr = (struct ifreq *) data;
	pcireg_t preg;
	int s, error, mmrbc;

	s = splnet();

	switch (cmd) {
	case SIOCSIFMTU:
		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > DGE_MAX_MTU)
			error = EINVAL;
		else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
			break;
		else if (ifp->if_flags & IFF_UP)
			error = if_init(ifp);
		else
			error = 0;
		break;

	case SIOCSIFFLAGS:
		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
			break;
		/* extract link flags */
		if ((ifp->if_flags & IFF_LINK0) == 0 &&
		    (ifp->if_flags & IFF_LINK1) == 0)
			mmrbc = PCIX_MMRBC_512;
		else if ((ifp->if_flags & IFF_LINK0) == 0 &&
		    (ifp->if_flags & IFF_LINK1) != 0)
			mmrbc = PCIX_MMRBC_1024;
		else if ((ifp->if_flags & IFF_LINK0) != 0 &&
		    (ifp->if_flags & IFF_LINK1) == 0)
			mmrbc = PCIX_MMRBC_2048;
		else
			mmrbc = PCIX_MMRBC_4096;
		if (mmrbc != sc->sc_mmrbc) {
			preg = pci_conf_read(sc->sc_pc, sc->sc_pt,DGE_PCIX_CMD);
			preg &= ~PCIX_MMRBC_MSK;
			preg |= mmrbc;
			pci_conf_write(sc->sc_pc, sc->sc_pt,DGE_PCIX_CMD, preg);
			sc->sc_mmrbc = mmrbc;
		}
		/* FALLTHROUGH */
	default:
		if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
			break;

		error = 0;

		if (cmd == SIOCSIFCAP)
			error = if_init(ifp);
		else if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
			;
		else if (ifp->if_flags & IFF_RUNNING) {
			/*
			 * Multicast list has changed; set the hardware filter
			 * accordingly.
			 */
			dge_set_filter(sc);
		}
		break;
	}

	/* Try to get more packets going. */
	dge_start(ifp);

	splx(s);
	return error;
}

/*
 * dge_intr:
 *
 *	Interrupt service routine.
 */
static int
dge_intr(void *arg)
{
	struct dge_softc *sc = arg;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	uint32_t icr;
	int wantinit, handled = 0;

	for (wantinit = 0; wantinit == 0;) {
		icr = CSR_READ(sc, DGE_ICR);
		if ((icr & sc->sc_icr) == 0)
			break;

		rnd_add_uint32(&sc->rnd_source, icr);

		handled = 1;

#if defined(DGE_DEBUG) || defined(DGE_EVENT_COUNTERS)
		if (icr & (ICR_RXDMT0 | ICR_RXT0)) {
			DPRINTF(DGE_DEBUG_RX,
			    ("%s: RX: got Rx intr 0x%08x\n",
			    device_xname(sc->sc_dev),
			    icr & (ICR_RXDMT0 | ICR_RXT0)));
			DGE_EVCNT_INCR(&sc->sc_ev_rxintr);
		}
#endif
		dge_rxintr(sc);

#if defined(DGE_DEBUG) || defined(DGE_EVENT_COUNTERS)
		if (icr & ICR_TXDW) {
			DPRINTF(DGE_DEBUG_TX,
			    ("%s: TX: got TXDW interrupt\n",
			    device_xname(sc->sc_dev)));
			DGE_EVCNT_INCR(&sc->sc_ev_txdw);
		}
		if (icr & ICR_TXQE)
			DGE_EVCNT_INCR(&sc->sc_ev_txqe);
#endif
		dge_txintr(sc);

		if (icr & (ICR_LSC | ICR_RXSEQ)) {
			DGE_EVCNT_INCR(&sc->sc_ev_linkintr);
			dge_linkintr(sc, icr);
		}

		if (icr & ICR_RXO) {
			printf("%s: Receive overrun\n",
			    device_xname(sc->sc_dev));
			wantinit = 1;
		}
	}

	if (handled) {
		if (wantinit)
			dge_init(ifp);

		/* Try to get more packets going. */
		if_schedule_deferred_start(ifp);
	}

	return handled;
}

/*
 * dge_txintr:
 *
 *	Helper; handle transmit interrupts.
 */
static void
dge_txintr(struct dge_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct dge_txsoft *txs;
	uint8_t status;
	int i;

	ifp->if_flags &= ~IFF_OACTIVE;

	/*
	 * Go through the Tx list and free mbufs for those
	 * frames which have been transmitted.
	 */
	for (i = sc->sc_txsdirty; sc->sc_txsfree != DGE_TXQUEUELEN;
	     i = DGE_NEXTTXS(i), sc->sc_txsfree++) {
		txs = &sc->sc_txsoft[i];

		DPRINTF(DGE_DEBUG_TX,
		    ("%s: TX: checking job %d\n", device_xname(sc->sc_dev), i));

		DGE_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_dmamap->dm_nsegs,
		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);

		status =
		    sc->sc_txdescs[txs->txs_lastdesc].dt_status;
		if ((status & TDESC_STA_DD) == 0) {
			DGE_CDTXSYNC(sc, txs->txs_lastdesc, 1,
			    BUS_DMASYNC_PREREAD);
			break;
		}

		DPRINTF(DGE_DEBUG_TX,
		    ("%s: TX: job %d done: descs %d..%d\n",
		    device_xname(sc->sc_dev), i, txs->txs_firstdesc,
		    txs->txs_lastdesc));

		if_statinc(ifp, if_opackets);
		sc->sc_txfree += txs->txs_ndesc;
		bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
		    0, txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
		bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
		m_freem(txs->txs_mbuf);
		txs->txs_mbuf = NULL;
	}

	/* Update the dirty transmit buffer pointer. */
	sc->sc_txsdirty = i;
	DPRINTF(DGE_DEBUG_TX,
	    ("%s: TX: txsdirty -> %d\n", device_xname(sc->sc_dev), i));

	/*
	 * If there are no more pending transmissions, cancel the watchdog
	 * timer.
	 */
	if (sc->sc_txsfree == DGE_TXQUEUELEN)
		ifp->if_timer = 0;
}

/*
 * dge_rxintr:
 *
 *	Helper; handle receive interrupts.
 */
static void
dge_rxintr(struct dge_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct dge_rxsoft *rxs;
	struct mbuf *m;
	int i, len;
	uint8_t status, errors;

	for (i = sc->sc_rxptr;; i = DGE_NEXTRX(i)) {
		rxs = &sc->sc_rxsoft[i];

		DPRINTF(DGE_DEBUG_RX,
		    ("%s: RX: checking descriptor %d\n",
		    device_xname(sc->sc_dev), i));

		DGE_CDRXSYNC(sc, i,
		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);

		status = sc->sc_rxdescs[i].dr_status;
		errors = sc->sc_rxdescs[i].dr_errors;
		len = le16toh(sc->sc_rxdescs[i].dr_len);

		if ((status & RDESC_STS_DD) == 0) {
			/* We have processed all of the receive descriptors. */
			DGE_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD);
			break;
		}

		if (__predict_false(sc->sc_rxdiscard)) {
			DPRINTF(DGE_DEBUG_RX,
			    ("%s: RX: discarding contents of descriptor %d\n",
			    device_xname(sc->sc_dev), i));
			DGE_INIT_RXDESC(sc, i);
			if (status & RDESC_STS_EOP) {
				/* Reset our state. */
				DPRINTF(DGE_DEBUG_RX,
				    ("%s: RX: resetting rxdiscard -> 0\n",
				    device_xname(sc->sc_dev)));
				sc->sc_rxdiscard = 0;
			}
			continue;
		}

		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);

		m = rxs->rxs_mbuf;

		/*
		 * Add a new receive buffer to the ring.
		 */
		if (dge_add_rxbuf(sc, i) != 0) {
			/*
			 * Failed, throw away what we've done so
			 * far, and discard the rest of the packet.
			 */
			if_statinc(ifp, if_ierrors);
			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
			DGE_INIT_RXDESC(sc, i);
			if ((status & RDESC_STS_EOP) == 0)
				sc->sc_rxdiscard = 1;
			if (sc->sc_rxhead != NULL)
				m_freem(sc->sc_rxhead);
			DGE_RXCHAIN_RESET(sc);
			DPRINTF(DGE_DEBUG_RX,
			    ("%s: RX: Rx buffer allocation failed, "
			    "dropping packet%s\n", device_xname(sc->sc_dev),
			    sc->sc_rxdiscard ? " (discard)" : ""));
			continue;
		}
		DGE_INIT_RXDESC(sc, DGE_PREVRX(i)); /* Write the descriptor */

		DGE_RXCHAIN_LINK(sc, m);

		m->m_len = len;

		DPRINTF(DGE_DEBUG_RX,
		    ("%s: RX: buffer at %p len %d\n",
		    device_xname(sc->sc_dev), m->m_data, len));

		/*
		 * If this is not the end of the packet, keep
		 * looking.
		 */
		if ((status & RDESC_STS_EOP) == 0) {
			sc->sc_rxlen += len;
			DPRINTF(DGE_DEBUG_RX,
			    ("%s: RX: not yet EOP, rxlen -> %d\n",
			    device_xname(sc->sc_dev), sc->sc_rxlen));
			continue;
		}

		/*
		 * Okay, we have the entire packet now...
		 */
		*sc->sc_rxtailp = NULL;
		m = sc->sc_rxhead;
		len += sc->sc_rxlen;

		DGE_RXCHAIN_RESET(sc);

		DPRINTF(DGE_DEBUG_RX,
		    ("%s: RX: have entire packet, len -> %d\n",
		    device_xname(sc->sc_dev), len));

		/*
		 * If an error occurred, update stats and drop the packet.
		 */
		if (errors & (RDESC_ERR_CE | RDESC_ERR_SE | RDESC_ERR_P |
		    RDESC_ERR_RXE)) {
			if_statinc(ifp, if_ierrors);
			if (errors & RDESC_ERR_SE)
				printf("%s: symbol error\n",
				    device_xname(sc->sc_dev));
			else if (errors & RDESC_ERR_P)
				printf("%s: parity error\n",
				    device_xname(sc->sc_dev));
			else if (errors & RDESC_ERR_CE)
				printf("%s: CRC error\n",
				    device_xname(sc->sc_dev));
			m_freem(m);
			continue;
		}

		/*
		 * No errors.  Receive the packet.
		 */
		m_set_rcvif(m, ifp);
		m->m_pkthdr.len = len;

		/*
		 * Set up checksum info for this packet.
		 */
		if (status & RDESC_STS_IPCS) {
			DGE_EVCNT_INCR(&sc->sc_ev_rxipsum);
			m->m_pkthdr.csum_flags |= M_CSUM_IPv4;
			if (errors & RDESC_ERR_IPE)
				m->m_pkthdr.csum_flags |= M_CSUM_IPv4_BAD;
		}
		if (status & RDESC_STS_TCPCS) {
			/*
			 * Note: we don't know if this was TCP or UDP,
			 * so we just set both bits, and expect the
			 * upper layers to deal.
			 */
			DGE_EVCNT_INCR(&sc->sc_ev_rxtusum);
			m->m_pkthdr.csum_flags |= M_CSUM_TCPv4 | M_CSUM_UDPv4;
			if (errors & RDESC_ERR_TCPE)
				m->m_pkthdr.csum_flags |= M_CSUM_TCP_UDP_BAD;
		}

		/* Pass it on. */
		if_percpuq_enqueue(ifp->if_percpuq, m);
	}

	/* Update the receive pointer. */
	sc->sc_rxptr = i;

	DPRINTF(DGE_DEBUG_RX,
	    ("%s: RX: rxptr -> %d\n", device_xname(sc->sc_dev), i));
}

/*
 * dge_linkintr:
 *
 *	Helper; handle link interrupts.
 */
static void
dge_linkintr(struct dge_softc *sc, uint32_t icr)
{
	uint32_t status;

	if (icr & ICR_LSC) {
		status = CSR_READ(sc, DGE_STATUS);
		if (status & STATUS_LINKUP) {
			DPRINTF(DGE_DEBUG_LINK, ("%s: LINK: LSC -> up\n",
			    device_xname(sc->sc_dev)));
		} else {
			DPRINTF(DGE_DEBUG_LINK, ("%s: LINK: LSC -> down\n",
			    device_xname(sc->sc_dev)));
		}
	} else if (icr & ICR_RXSEQ) {
		DPRINTF(DGE_DEBUG_LINK,
		    ("%s: LINK: Receive sequence error\n",
		    device_xname(sc->sc_dev)));
	}
	/* XXX - fix errata */
}

/*
 * dge_reset:
 *
 *	Reset the i82597 chip.
 */
static void
dge_reset(struct dge_softc *sc)
{
	int i;

	/*
	 * Do a chip reset.
	 */
	CSR_WRITE(sc, DGE_CTRL0, CTRL0_RST | sc->sc_ctrl0);

	delay(10000);

	for (i = 0; i < 1000; i++) {
		if ((CSR_READ(sc, DGE_CTRL0) & CTRL0_RST) == 0)
			break;
		delay(20);
	}

	if (CSR_READ(sc, DGE_CTRL0) & CTRL0_RST)
		printf("%s: WARNING: reset failed to complete\n",
		    device_xname(sc->sc_dev));
	/*
	 * Reset the EEPROM logic.
	 * This will cause the chip to reread its default values,
	 * which doesn't happen otherwise (errata).
	 */
	CSR_WRITE(sc, DGE_CTRL1, CTRL1_EE_RST);
	delay(10000);
}

/*
 * dge_init:		[ifnet interface function]
 *
 *	Initialize the interface.  Must be called at splnet().
 */
static int
dge_init(struct ifnet *ifp)
{
	struct dge_softc *sc = ifp->if_softc;
	struct dge_rxsoft *rxs;
	int i, error = 0;
	uint32_t reg;

	/*
	 * *_HDR_ALIGNED_P is constant 1 if __NO_STRICT_ALIGNMENT is set.
	 * There is a small but measurable benefit to avoiding the adjusment
	 * of the descriptor so that the headers are aligned, for normal mtu,
	 * on such platforms.  One possibility is that the DMA itself is
	 * slightly more efficient if the front of the entire packet (instead
	 * of the front of the headers) is aligned.
	 *
	 * Note we must always set align_tweak to 0 if we are using
	 * jumbo frames.
	 */
#ifdef __NO_STRICT_ALIGNMENT
	sc->sc_align_tweak = 0;
#else
	if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN) > (MCLBYTES - 2))
		sc->sc_align_tweak = 0;
	else
		sc->sc_align_tweak = 2;
#endif /* __NO_STRICT_ALIGNMENT */

	/* Cancel any pending I/O. */
	dge_stop(ifp, 0);

	/* Reset the chip to a known state. */
	dge_reset(sc);

	/* Initialize the transmit descriptor ring. */
	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
	DGE_CDTXSYNC(sc, 0, DGE_NTXDESC,
	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
	sc->sc_txfree = DGE_NTXDESC;
	sc->sc_txnext = 0;

	sc->sc_txctx_ipcs = 0xffffffff;
	sc->sc_txctx_tucs = 0xffffffff;

	CSR_WRITE(sc, DGE_TDBAH, ((uint64_t)DGE_CDTXADDR(sc, 0)) >> 32);
	CSR_WRITE(sc, DGE_TDBAL, DGE_CDTXADDR(sc, 0));
	CSR_WRITE(sc, DGE_TDLEN, sizeof(sc->sc_txdescs));
	CSR_WRITE(sc, DGE_TDH, 0);
	CSR_WRITE(sc, DGE_TDT, 0);
	CSR_WRITE(sc, DGE_TIDV, TIDV);

#if 0
	CSR_WRITE(sc, DGE_TXDCTL, TXDCTL_PTHRESH(0) |
	    TXDCTL_HTHRESH(0) | TXDCTL_WTHRESH(0));
#endif
	CSR_WRITE(sc, DGE_RXDCTL,
	    RXDCTL_PTHRESH(RXDCTL_PTHRESH_VAL) |
	    RXDCTL_HTHRESH(RXDCTL_HTHRESH_VAL) |
	    RXDCTL_WTHRESH(RXDCTL_WTHRESH_VAL));

	/* Initialize the transmit job descriptors. */
	for (i = 0; i < DGE_TXQUEUELEN; i++)
		sc->sc_txsoft[i].txs_mbuf = NULL;
	sc->sc_txsfree = DGE_TXQUEUELEN;
	sc->sc_txsnext = 0;
	sc->sc_txsdirty = 0;

	/*
	 * Initialize the receive descriptor and receive job
	 * descriptor rings.
	 */
	CSR_WRITE(sc, DGE_RDBAH, ((uint64_t)DGE_CDRXADDR(sc, 0)) >> 32);
	CSR_WRITE(sc, DGE_RDBAL, DGE_CDRXADDR(sc, 0));
	CSR_WRITE(sc, DGE_RDLEN, sizeof(sc->sc_rxdescs));
	CSR_WRITE(sc, DGE_RDH, DGE_RXSPACE);
	CSR_WRITE(sc, DGE_RDT, 0);
	CSR_WRITE(sc, DGE_RDTR, RDTR | 0x80000000);
	CSR_WRITE(sc, DGE_FCRTL, FCRTL | FCRTL_XONE);
	CSR_WRITE(sc, DGE_FCRTH, FCRTH);

	for (i = 0; i < DGE_NRXDESC; i++) {
		rxs = &sc->sc_rxsoft[i];
		if (rxs->rxs_mbuf == NULL) {
			if ((error = dge_add_rxbuf(sc, i)) != 0) {
				printf("%s: unable to allocate or map rx "
				    "buffer %d, error = %d\n",
				    device_xname(sc->sc_dev), i, error);
				/*
				 * XXX Should attempt to run with fewer receive
				 * XXX buffers instead of just failing.
				 */
				dge_rxdrain(sc);
				goto out;
			}
		}
		DGE_INIT_RXDESC(sc, i);
	}
	sc->sc_rxptr = DGE_RXSPACE;
	sc->sc_rxdiscard = 0;
	DGE_RXCHAIN_RESET(sc);

	if (sc->sc_ethercom.ec_capabilities & ETHERCAP_JUMBO_MTU) {
		sc->sc_ctrl0 |= CTRL0_JFE;
		CSR_WRITE(sc, DGE_MFS, ETHER_MAX_LEN_JUMBO << 16);
	}

	/* Write the control registers. */
	CSR_WRITE(sc, DGE_CTRL0, sc->sc_ctrl0);

	/*
	 * Set up checksum offload parameters.
	 */
	reg = CSR_READ(sc, DGE_RXCSUM);
	if (ifp->if_capenable & IFCAP_CSUM_IPv4_Rx)
		reg |= RXCSUM_IPOFL;
	else
		reg &= ~RXCSUM_IPOFL;
	if (ifp->if_capenable & (IFCAP_CSUM_TCPv4_Rx | IFCAP_CSUM_UDPv4_Rx))
		reg |= RXCSUM_IPOFL | RXCSUM_TUOFL;
	else {
		reg &= ~RXCSUM_TUOFL;
		if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) == 0)
			reg &= ~RXCSUM_IPOFL;
	}
	CSR_WRITE(sc, DGE_RXCSUM, reg);

	/*
	 * Set up the interrupt registers.
	 */
	CSR_WRITE(sc, DGE_IMC, 0xffffffffU);
	sc->sc_icr = ICR_TXDW | ICR_LSC | ICR_RXSEQ | ICR_RXDMT0 |
	    ICR_RXO | ICR_RXT0;

	CSR_WRITE(sc, DGE_IMS, sc->sc_icr);

	/*
	 * Set up the transmit control register.
	 */
	sc->sc_tctl = TCTL_TCE | TCTL_TPDE | TCTL_TXEN;
	CSR_WRITE(sc, DGE_TCTL, sc->sc_tctl);

	/*
	 * Set up the receive control register; we actually program
	 * the register when we set the receive filter.  Use multicast
	 * address offset type 0.
	 */
	sc->sc_mchash_type = 0;

	sc->sc_rctl = RCTL_RXEN | RCTL_RDMTS_12 | RCTL_RPDA_MC |
	    RCTL_CFF | RCTL_SECRC | RCTL_MO(sc->sc_mchash_type);

#ifdef DGE_OFFBYONE_RXBUG
	sc->sc_rctl |= RCTL_BSIZE_16k;
#else
	switch (MCLBYTES) {
	case 2048:
		sc->sc_rctl |= RCTL_BSIZE_2k;
		break;
	case 4096:
		sc->sc_rctl |= RCTL_BSIZE_4k;
		break;
	case 8192:
		sc->sc_rctl |= RCTL_BSIZE_8k;
		break;
	case 16384:
		sc->sc_rctl |= RCTL_BSIZE_16k;
		break;
	default:
		panic("dge_init: MCLBYTES %d unsupported", MCLBYTES);
	}
#endif

	/* Set the receive filter. */
	/* Also sets RCTL */
	dge_set_filter(sc);

	/* ...all done! */
	ifp->if_flags |= IFF_RUNNING;
	ifp->if_flags &= ~IFF_OACTIVE;

 out:
	if (error)
		printf("%s: interface not running\n", device_xname(sc->sc_dev));
	return error;
}

/*
 * dge_rxdrain:
 *
 *	Drain the receive queue.
 */
static void
dge_rxdrain(struct dge_softc *sc)
{
	struct dge_rxsoft *rxs;
	int i;

	for (i = 0; i < DGE_NRXDESC; i++) {
		rxs = &sc->sc_rxsoft[i];
		if (rxs->rxs_mbuf != NULL) {
			bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
			m_freem(rxs->rxs_mbuf);
			rxs->rxs_mbuf = NULL;
		}
	}
}

/*
 * dge_stop:		[ifnet interface function]
 *
 *	Stop transmission on the interface.
 */
static void
dge_stop(struct ifnet *ifp, int disable)
{
	struct dge_softc *sc = ifp->if_softc;
	struct dge_txsoft *txs;
	int i;

	/* Stop the transmit and receive processes. */
	CSR_WRITE(sc, DGE_TCTL, 0);
	CSR_WRITE(sc, DGE_RCTL, 0);

	/* Release any queued transmit buffers. */
	for (i = 0; i < DGE_TXQUEUELEN; i++) {
		txs = &sc->sc_txsoft[i];
		if (txs->txs_mbuf != NULL) {
			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
			m_freem(txs->txs_mbuf);
			txs->txs_mbuf = NULL;
		}
	}

	/* Mark the interface as down and cancel the watchdog timer. */
	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
	ifp->if_timer = 0;

	if (disable)
		dge_rxdrain(sc);
}

/*
 * dge_add_rxbuf:
 *
 *	Add a receive buffer to the indiciated descriptor.
 */
static int
dge_add_rxbuf(struct dge_softc *sc, int idx)
{
	struct dge_rxsoft *rxs = &sc->sc_rxsoft[idx];
	struct mbuf *m;
	int error;
#ifdef DGE_OFFBYONE_RXBUG
	void *buf;
#endif

	MGETHDR(m, M_DONTWAIT, MT_DATA);
	if (m == NULL)
		return ENOBUFS;

#ifdef DGE_OFFBYONE_RXBUG
	if ((buf = dge_getbuf(sc)) == NULL)
		return ENOBUFS;

	m->m_len = m->m_pkthdr.len = DGE_BUFFER_SIZE;
	MEXTADD(m, buf, DGE_BUFFER_SIZE, M_DEVBUF, dge_freebuf, sc);
	m->m_flags |= M_EXT_RW;

	if (rxs->rxs_mbuf != NULL)
		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
	rxs->rxs_mbuf = m;

	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap, buf,
	    DGE_BUFFER_SIZE, NULL, BUS_DMA_READ | BUS_DMA_NOWAIT);
#else
	MCLGET(m, M_DONTWAIT);
	if ((m->m_flags & M_EXT) == 0) {
		m_freem(m);
		return ENOBUFS;
	}

	if (rxs->rxs_mbuf != NULL)
		bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);

	rxs->rxs_mbuf = m;

	m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
	error = bus_dmamap_load_mbuf(sc->sc_dmat, rxs->rxs_dmamap, m,
	    BUS_DMA_READ | BUS_DMA_NOWAIT);
#endif
	if (error) {
		printf("%s: unable to load rx DMA map %d, error = %d\n",
		    device_xname(sc->sc_dev), idx, error);
		panic("dge_add_rxbuf");	/* XXX XXX XXX */
	}
	bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);

	return 0;
}

/*
 * dge_set_ral:
 *
 *	Set an entry in the receive address list.
 */
static void
dge_set_ral(struct dge_softc *sc, const uint8_t *enaddr, int idx)
{
	uint32_t ral_lo, ral_hi;

	if (enaddr != NULL) {
		ral_lo = enaddr[0] | (enaddr[1] << 8) | (enaddr[2] << 16) |
		    (enaddr[3] << 24);
		ral_hi = enaddr[4] | (enaddr[5] << 8);
		ral_hi |= RAH_AV;
	} else {
		ral_lo = 0;
		ral_hi = 0;
	}
	CSR_WRITE(sc, RA_ADDR(DGE_RAL, idx), ral_lo);
	CSR_WRITE(sc, RA_ADDR(DGE_RAH, idx), ral_hi);
}

/*
 * dge_mchash:
 *
 *	Compute the hash of the multicast address for the 4096-bit
 *	multicast filter.
 */
static uint32_t
dge_mchash(struct dge_softc *sc, const uint8_t *enaddr)
{
	static const int lo_shift[4] = { 4, 3, 2, 0 };
	static const int hi_shift[4] = { 4, 5, 6, 8 };
	uint32_t hash;

	hash = (enaddr[4] >> lo_shift[sc->sc_mchash_type]) |
	    (((uint16_t) enaddr[5]) << hi_shift[sc->sc_mchash_type]);

	return (hash & 0xfff);
}

/*
 * dge_set_filter:
 *
 *	Set up the receive filter.
 */
static void
dge_set_filter(struct dge_softc *sc)
{
	struct ethercom *ec = &sc->sc_ethercom;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct ether_multi *enm;
	struct ether_multistep step;
	uint32_t hash, reg, bit;
	int i;

	sc->sc_rctl &= ~(RCTL_BAM | RCTL_UPE | RCTL_MPE);

	if (ifp->if_flags & IFF_BROADCAST)
		sc->sc_rctl |= RCTL_BAM;
	if (ifp->if_flags & IFF_PROMISC) {
		sc->sc_rctl |= RCTL_UPE;
		goto allmulti;
	}

	/*
	 * Set the station address in the first RAL slot, and
	 * clear the remaining slots.
	 */
	dge_set_ral(sc, CLLADDR(ifp->if_sadl), 0);
	for (i = 1; i < RA_TABSIZE; i++)
		dge_set_ral(sc, NULL, i);

	/* Clear out the multicast table. */
	for (i = 0; i < MC_TABSIZE; i++)
		CSR_WRITE(sc, DGE_MTA + (i << 2), 0);

	ETHER_LOCK(ec);
	ETHER_FIRST_MULTI(step, ec, enm);
	while (enm != NULL) {
		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
			/*
			 * We must listen to a range of multicast addresses.
			 * For now, just accept all multicasts, rather than
			 * trying to set only those filter bits needed to match
			 * the range.  (At this time, the only use of address
			 * ranges is for IP multicast routing, for which the
			 * range is big enough to require all bits set.)
			 */
			ETHER_UNLOCK(ec);
			goto allmulti;
		}

		hash = dge_mchash(sc, enm->enm_addrlo);

		reg = (hash >> 5) & 0x7f;
		bit = hash & 0x1f;

		hash = CSR_READ(sc, DGE_MTA + (reg << 2));
		hash |= 1U << bit;

		CSR_WRITE(sc, DGE_MTA + (reg << 2), hash);

		ETHER_NEXT_MULTI(step, enm);
	}
	ETHER_UNLOCK(ec);

	ifp->if_flags &= ~IFF_ALLMULTI;
	goto setit;

 allmulti:
	ifp->if_flags |= IFF_ALLMULTI;
	sc->sc_rctl |= RCTL_MPE;

 setit:
	CSR_WRITE(sc, DGE_RCTL, sc->sc_rctl);
}

/*
 * Read in the EEPROM info and verify checksum.
 */
int
dge_read_eeprom(struct dge_softc *sc)
{
	uint16_t cksum;
	int i;

	cksum = 0;
	for (i = 0; i < EEPROM_SIZE; i++) {
		sc->sc_eeprom[i] = dge_eeprom_word(sc, i);
		cksum += sc->sc_eeprom[i];
	}
	return cksum != EEPROM_CKSUM;
}


/*
 * Read a 16-bit word from address addr in the serial EEPROM.
 */
uint16_t
dge_eeprom_word(struct dge_softc *sc, int addr)
{
	uint32_t reg;
	uint16_t rval = 0;
	int i;

	reg = CSR_READ(sc, DGE_EECD) & ~(EECD_SK | EECD_DI | EECD_CS);

	/* Lower clock pulse (and data in to chip) */
	CSR_WRITE(sc, DGE_EECD, reg);
	/* Select chip */
	CSR_WRITE(sc, DGE_EECD, reg | EECD_CS);

	/* Send read command */
	dge_eeprom_clockout(sc, 1);
	dge_eeprom_clockout(sc, 1);
	dge_eeprom_clockout(sc, 0);

	/* Send address */
	for (i = 5; i >= 0; i--)
		dge_eeprom_clockout(sc, (addr >> i) & 1);

	/* Read data */
	for (i = 0; i < 16; i++) {
		rval <<= 1;
		rval |= dge_eeprom_clockin(sc);
	}

	/* Deselect chip */
	CSR_WRITE(sc, DGE_EECD, reg);

	return rval;
}

/*
 * Clock out a single bit to the EEPROM.
 */
void
dge_eeprom_clockout(struct dge_softc *sc, int bit)
{
	int reg;

	reg = CSR_READ(sc, DGE_EECD) & ~(EECD_DI | EECD_SK);
	if (bit)
		reg |= EECD_DI;

	CSR_WRITE(sc, DGE_EECD, reg);
	delay(2);
	CSR_WRITE(sc, DGE_EECD, reg | EECD_SK);
	delay(2);
	CSR_WRITE(sc, DGE_EECD, reg);
	delay(2);
}

/*
 * Clock in a single bit from EEPROM.
 */
int
dge_eeprom_clockin(struct dge_softc *sc)
{
	int reg, rv;

	reg = CSR_READ(sc, DGE_EECD) & ~(EECD_DI | EECD_DO | EECD_SK);

	CSR_WRITE(sc, DGE_EECD, reg | EECD_SK); /* Raise clock */
	delay(2);
	rv = (CSR_READ(sc, DGE_EECD) & EECD_DO) != 0; /* Get bit */
	CSR_WRITE(sc, DGE_EECD, reg); /* Lower clock */
	delay(2);

	return rv;
}

static void
dge_xgmii_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
{
	struct dge_softc *sc = ifp->if_softc;

	ifmr->ifm_status = IFM_AVALID;
	if (sc->sc_dgep->flags & DGEP_F_10G_SR ) {
		ifmr->ifm_active = IFM_ETHER | IFM_10G_SR;
	} else {
		ifmr->ifm_active = IFM_ETHER | IFM_10G_LR;
	}

	if (CSR_READ(sc, DGE_STATUS) & STATUS_LINKUP)
		ifmr->ifm_status |= IFM_ACTIVE;
}

static inline int
phwait(struct dge_softc *sc, int p, int r, int d, int type)
{
	int i, mdic;

	CSR_WRITE(sc, DGE_MDIO,
	    MDIO_PHY(p) | MDIO_REG(r) | MDIO_DEV(d) | type | MDIO_CMD);
	for (i = 0; i < 10; i++) {
		delay(10);
		if (((mdic = CSR_READ(sc, DGE_MDIO)) & MDIO_CMD) == 0)
			break;
	}
	return mdic;
}

static void
dge_xgmii_writereg(struct dge_softc *sc, int phy, int reg, int val)
{
	int mdic;

	CSR_WRITE(sc, DGE_MDIRW, val);
	if (((mdic = phwait(sc, phy, reg, 1, MDIO_ADDR)) & MDIO_CMD)) {
		printf("%s: address cycle timeout; phy %d reg %d\n",
		    device_xname(sc->sc_dev), phy, reg);
		return;
	}
	if (((mdic = phwait(sc, phy, reg, 1, MDIO_WRITE)) & MDIO_CMD)) {
		printf("%s: write cycle timeout; phy %d reg %d\n",
		    device_xname(sc->sc_dev), phy, reg);
		return;
	}
}

static void
dge_xgmii_reset(struct dge_softc *sc)
{
	dge_xgmii_writereg(sc, 0, 0, BMCR_RESET);
}

static int
dge_xgmii_mediachange(struct ifnet *ifp)
{
	return 0;
}