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
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
/*	$NetBSD: tulip.c,v 1.208 2022/08/01 10:30:28 kre Exp $	*/

/*-
 * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
 * NASA Ames Research Center; and by Charles M. Hannum.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 THE FOUNDATION OR CONTRIBUTORS
 * 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 Digital Semiconductor ``Tulip'' (21x4x)
 * Ethernet controller family, and a variety of clone chips.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tulip.c,v 1.208 2022/08/01 10:30:28 kre 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 <machine/endian.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 <sys/bus.h>
#include <sys/intr.h>

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

#include <dev/ic/tulipreg.h>
#include <dev/ic/tulipvar.h>

static const char * const tlp_chip_names[] = TULIP_CHIP_NAMES;

static const struct tulip_txthresh_tab tlp_10_txthresh_tab[] =
    TLP_TXTHRESH_TAB_10;

static const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] =
    TLP_TXTHRESH_TAB_10_100;

static const struct tulip_txthresh_tab tlp_dm9102_txthresh_tab[] =
    TLP_TXTHRESH_TAB_DM9102;

static void	tlp_start(struct ifnet *);
static void	tlp_watchdog(struct ifnet *);
static int	tlp_ioctl(struct ifnet *, u_long, void *);
static int	tlp_init(struct ifnet *);
static void	tlp_stop(struct ifnet *, int);
static int	tlp_ifflags_cb(struct ethercom *);

static void	tlp_rxdrain(struct tulip_softc *);
static int	tlp_add_rxbuf(struct tulip_softc *, int);
static void	tlp_srom_idle(struct tulip_softc *);
static int	tlp_srom_size(struct tulip_softc *);

static int	tlp_enable(struct tulip_softc *);
static void	tlp_disable(struct tulip_softc *);

static void	tlp_filter_setup(struct tulip_softc *);
static void	tlp_winb_filter_setup(struct tulip_softc *);
static void	tlp_al981_filter_setup(struct tulip_softc *);
static void	tlp_asix_filter_setup(struct tulip_softc *);

static void	tlp_rxintr(struct tulip_softc *);
static void	tlp_txintr(struct tulip_softc *);

static void	tlp_mii_tick(void *);
static void	tlp_mii_statchg(struct ifnet *);
static void	tlp_winb_mii_statchg(struct ifnet *);
static void	tlp_dm9102_mii_statchg(struct ifnet *);

static void	tlp_mii_getmedia(struct tulip_softc *, struct ifmediareq *);
static int	tlp_mii_setmedia(struct tulip_softc *);

static int	tlp_bitbang_mii_readreg(device_t, int, int, uint16_t *);
static int	tlp_bitbang_mii_writereg(device_t, int, int, uint16_t);

static int	tlp_pnic_mii_readreg(device_t, int, int, uint16_t *);
static int	tlp_pnic_mii_writereg(device_t, int, int, uint16_t);

static int	tlp_al981_mii_readreg(device_t, int, int, uint16_t *);
static int	tlp_al981_mii_writereg(device_t, int, int, uint16_t);

static void	tlp_2114x_preinit(struct tulip_softc *);
static void	tlp_2114x_mii_preinit(struct tulip_softc *);
static void	tlp_pnic_preinit(struct tulip_softc *);
static void	tlp_dm9102_preinit(struct tulip_softc *);
static void	tlp_asix_preinit(struct tulip_softc *);

static void	tlp_21140_reset(struct tulip_softc *);
static void	tlp_21142_reset(struct tulip_softc *);
static void	tlp_pmac_reset(struct tulip_softc *);
#if 0
static void	tlp_dm9102_reset(struct tulip_softc *);
#endif

static void	tlp_2114x_nway_tick(void *);

#define	tlp_mchash(addr, sz)						\
	(ether_crc32_le((addr), ETHER_ADDR_LEN) & ((sz) - 1))

/*
 * MII bit-bang glue.
 */
static uint32_t tlp_sio_mii_bitbang_read(device_t);
static void	tlp_sio_mii_bitbang_write(device_t, uint32_t);

static const struct mii_bitbang_ops tlp_sio_mii_bitbang_ops = {
	tlp_sio_mii_bitbang_read,
	tlp_sio_mii_bitbang_write,
	{
		MIIROM_MDO,		/* MII_BIT_MDO */
		MIIROM_MDI,		/* MII_BIT_MDI */
		MIIROM_MDC,		/* MII_BIT_MDC */
		0,			/* MII_BIT_DIR_HOST_PHY */
		MIIROM_MIIDIR,		/* MII_BIT_DIR_PHY_HOST */
	}
};

#ifdef TLP_DEBUG
#define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
				printf x
#else
#define	DPRINTF(sc, x)	/* nothing */
#endif

#ifdef TLP_STATS
static void	tlp_print_stats(struct tulip_softc *);
#endif

/*
 * Can be used to debug the SROM-related things, including contents.
 * Initialized so that it's patchable.
 */
int	tlp_srom_debug = 0;

/*
 * tlp_attach:
 *
 *	Attach a Tulip interface to the system.
 */
int
tlp_attach(struct tulip_softc *sc, const uint8_t *enaddr)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	device_t self = sc->sc_dev;
	int i, error;

	callout_init(&sc->sc_nway_callout, 0);
	callout_init(&sc->sc_tick_callout, 0);

	/*
	 * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
	 */

	/*
	 * Setup the transmit threshold table.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_DE425:
	case TULIP_CHIP_21040:
	case TULIP_CHIP_21041:
		sc->sc_txth = tlp_10_txthresh_tab;
		break;

	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
		sc->sc_txth = tlp_dm9102_txthresh_tab;
		break;

	default:
		sc->sc_txth = tlp_10_100_txthresh_tab;
		break;
	}

	/*
	 * Setup the filter setup function.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_WB89C840F:
		sc->sc_filter_setup = tlp_winb_filter_setup;
		break;

	case TULIP_CHIP_AL981:
	case TULIP_CHIP_AN983:
	case TULIP_CHIP_AN985:
		sc->sc_filter_setup = tlp_al981_filter_setup;
		break;

	case TULIP_CHIP_AX88140:
	case TULIP_CHIP_AX88141:
		sc->sc_filter_setup = tlp_asix_filter_setup;
		break;

	default:
		sc->sc_filter_setup = tlp_filter_setup;
		break;
	}

	/*
	 * Set up the media status change function.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_WB89C840F:
		sc->sc_statchg = tlp_winb_mii_statchg;
		break;

	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
		sc->sc_statchg = tlp_dm9102_mii_statchg;
		break;

	default:
		/*
		 * We may override this if we have special media
		 * handling requirements (e.g. flipping GPIO pins).
		 *
		 * The pure-MII statchg function covers the basics.
		 */
		sc->sc_statchg = tlp_mii_statchg;
		break;
	}

	/*
	 * Default to no FS|LS in setup packet descriptors.  They're
	 * supposed to be zero according to the 21040 and 21143
	 * manuals, and some chips fall over badly if they're
	 * included.  Yet, other chips seem to require them.  Sigh.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_X3201_3:
		sc->sc_setup_fsls = TDCTL_Tx_FS | TDCTL_Tx_LS;
		break;

	default:
		sc->sc_setup_fsls = 0;
	}

	/*
	 * Set up various chip-specific quirks.
	 *
	 * Note that wherever we can, we use the "ring" option for
	 * transmit and receive descriptors.  This is because some
	 * clone chips apparently have problems when using chaining,
	 * although some *only* support chaining.
	 *
	 * What we do is always program the "next" pointer, and then
	 * conditionally set the TDCTL_CH and TDCTL_ER bits in the
	 * appropriate places.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_21140:
	case TULIP_CHIP_21140A:
	case TULIP_CHIP_21142:
	case TULIP_CHIP_21143:
	case TULIP_CHIP_82C115:		/* 21143-like */
	case TULIP_CHIP_MX98713:	/* 21140-like */
	case TULIP_CHIP_MX98713A:	/* 21143-like */
	case TULIP_CHIP_MX98715:	/* 21143-like */
	case TULIP_CHIP_MX98715A:	/* 21143-like */
	case TULIP_CHIP_MX98715AEC_X:	/* 21143-like */
	case TULIP_CHIP_MX98725:	/* 21143-like */
	case TULIP_CHIP_RS7112:		/* 21143-like */
		/*
		 * Run these chips in ring mode.
		 */
		sc->sc_tdctl_ch = 0;
		sc->sc_tdctl_er = TDCTL_ER;
		sc->sc_preinit = tlp_2114x_preinit;
		break;

	case TULIP_CHIP_82C168:
	case TULIP_CHIP_82C169:
		/*
		 * Run these chips in ring mode.
		 */
		sc->sc_tdctl_ch = 0;
		sc->sc_tdctl_er = TDCTL_ER;
		sc->sc_preinit = tlp_pnic_preinit;

		/*
		 * These chips seem to have busted DMA engines; just put them
		 * in Store-and-Forward mode from the get-go.
		 */
		sc->sc_txthresh = TXTH_SF;
		break;

	case TULIP_CHIP_WB89C840F:
		/*
		 * Run this chip in chained mode.
		 */
		sc->sc_tdctl_ch = TDCTL_CH;
		sc->sc_tdctl_er = 0;
		sc->sc_flags |= TULIPF_IC_FS;
		break;

	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
		/*
		 * Run these chips in chained mode.
		 */
		sc->sc_tdctl_ch = TDCTL_CH;
		sc->sc_tdctl_er = 0;
		sc->sc_preinit = tlp_dm9102_preinit;

		/*
		 * These chips have a broken bus interface, so we
		 * can't use any optimized bus commands.  For this
		 * reason, we tend to underrun pretty quickly, so
		 * just to Store-and-Forward mode from the get-go.
		 */
		sc->sc_txthresh = TXTH_DM9102_SF;
		break;

	case TULIP_CHIP_AX88140:
	case TULIP_CHIP_AX88141:
		/*
		 * Run these chips in ring mode.
		 */
		sc->sc_tdctl_ch = 0;
		sc->sc_tdctl_er = TDCTL_ER;
		sc->sc_preinit = tlp_asix_preinit;
		break;

	default:
		/*
		 * Default to running in ring mode.
		 */
		sc->sc_tdctl_ch = 0;
		sc->sc_tdctl_er = TDCTL_ER;
	}

	/*
	 * Set up the MII bit-bang operations.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_WB89C840F:	/* XXX direction bit different? */
		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
		break;

	default:
		sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
	}

	SIMPLEQ_INIT(&sc->sc_txfreeq);
	SIMPLEQ_INIT(&sc->sc_txdirtyq);

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

	if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
	    sizeof(struct tulip_control_data), (void **)&sc->sc_control_data,
	    BUS_DMA_COHERENT)) != 0) {
		aprint_error_dev(self,
		    "unable to map control data, error = %d\n", error);
		goto fail_1;
	}

	if ((error = bus_dmamap_create(sc->sc_dmat,
	    sizeof(struct tulip_control_data), 1,
	    sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
		sc->sc_cddmamap = NULL;
		aprint_error_dev(self,
		    "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 tulip_control_data), NULL,
	    0)) != 0) {
		aprint_error_dev(self,
		    "unable to load control data DMA map, error = %d\n",
		    error);
		goto fail_3;
	}

	/*
	 * Create the transmit buffer DMA maps.
	 *
	 * Note that on the Xircom clone, transmit buffers must be
	 * 4-byte aligned.  We're almost guaranteed to have to copy
	 * the packet in that case, so we just limit ourselves to
	 * one segment.
	 *
	 * On the DM9102, the transmit logic can only handle one
	 * DMA segment.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_X3201_3:
	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
	case TULIP_CHIP_AX88140:
	case TULIP_CHIP_AX88141:
		sc->sc_ntxsegs = 1;
		break;

	default:
		sc->sc_ntxsegs = TULIP_NTXSEGS;
	}
	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
		    sc->sc_ntxsegs, MCLBYTES, 0, 0,
		    &sc->sc_txsoft[i].txs_dmamap)) != 0) {
			sc->sc_txsoft[i].txs_dmamap = NULL;
			aprint_error_dev(self,
			    "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 < TULIP_NRXDESC; i++) {
		if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
			sc->sc_rxsoft[i].rxs_dmamap = NULL;
			aprint_error_dev(self,
			    "unable to create rx DMA map %d, error = %d\n", i,
			    error);
			goto fail_5;
		}
		sc->sc_rxsoft[i].rxs_mbuf = NULL;
	}

	/*
	 * From this point forward, the attachment cannot fail.  A failure
	 * before this point releases all resources that may have been
	 * allocated.
	 */
	sc->sc_flags |= TULIPF_ATTACHED;

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

	/* Announce ourselves. */
	aprint_normal_dev(self, "%s%sEthernet address %s\n",
	    sc->sc_name[0] != '\0' ? sc->sc_name : "",
	    sc->sc_name[0] != '\0' ? ", " : "",
	    ether_sprintf(enaddr));

	/*
	 * Check to see if we're the simulated Ethernet on Connectix
	 * Virtual PC.
	 */
	if (enaddr[0] == 0x00 && enaddr[1] == 0x03 && enaddr[2] == 0xff)
		sc->sc_flags |= TULIPF_VPC;

	/*
	 * Initialize our media structures.  This may probe the MII, if
	 * present.
	 */
	(*sc->sc_mediasw->tmsw_init)(sc);

	strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
	ifp->if_softc = sc;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
	sc->sc_if_flags = ifp->if_flags;
	ifp->if_ioctl = tlp_ioctl;
	ifp->if_start = tlp_start;
	ifp->if_watchdog = tlp_watchdog;
	ifp->if_init = tlp_init;
	ifp->if_stop = tlp_stop;
	IFQ_SET_READY(&ifp->if_snd);

	/*
	 * We can support 802.1Q VLAN-sized frames.
	 */
	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;

	/*
	 * Attach the interface.
	 */
	if_attach(ifp);
	if_deferred_start_init(ifp, NULL);
	ether_ifattach(ifp, enaddr);
	ether_set_ifflags_cb(&sc->sc_ethercom, tlp_ifflags_cb);

	rnd_attach_source(&sc->sc_rnd_source, device_xname(self),
	    RND_TYPE_NET, RND_FLAG_DEFAULT);

	if (pmf_device_register(self, NULL, NULL))
		pmf_class_network_register(self, ifp);
	else
		aprint_error_dev(self, "couldn't establish power handler\n");

	return 0;

	/*
	 * 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 < TULIP_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 < TULIP_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 tulip_control_data));
 fail_1:
	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
 fail_0:
	return error;
}

/*
 * tlp_activate:
 *
 *	Handle device activation/deactivation requests.
 */
int
tlp_activate(device_t self, enum devact act)
{
	struct tulip_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		if_deactivate(&sc->sc_ethercom.ec_if);
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

/*
 * tlp_detach:
 *
 *	Detach a Tulip interface.
 */
int
tlp_detach(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct tulip_rxsoft *rxs;
	struct tulip_txsoft *txs;
	device_t self = sc->sc_dev;
	int i, s;

	/*
	 * Succeed now if there isn't any work to do.
	 */
	if ((sc->sc_flags & TULIPF_ATTACHED) == 0)
		return 0;

	s = splnet();
	/* Stop the interface. Callouts are stopped in it. */
	tlp_stop(ifp, 1);
	splx(s);

	/* Destroy our callouts. */
	callout_destroy(&sc->sc_nway_callout);
	callout_destroy(&sc->sc_tick_callout);

	if (sc->sc_flags & TULIPF_HAS_MII) {
		/* Detach all PHYs */
		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
	}

	rnd_detach_source(&sc->sc_rnd_source);

	ether_ifdetach(ifp);
	if_detach(ifp);

	/* Delete all remaining media. */
	ifmedia_fini(&sc->sc_mii.mii_media);

	for (i = 0; i < TULIP_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;
		}
		bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
	}
	for (i = 0; i < TULIP_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;
		}
		bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
	}
	bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
	bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
	bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_control_data,
	    sizeof(struct tulip_control_data));
	bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);

	pmf_device_deregister(self);

	if (sc->sc_srom)
		free(sc->sc_srom, M_DEVBUF);

	return 0;
}

/*
 * tlp_start:		[ifnet interface function]
 *
 *	Start packet transmission on the interface.
 */
static void
tlp_start(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;
	struct mbuf *m0, *m;
	struct tulip_txsoft *txs, *last_txs = NULL;
	bus_dmamap_t dmamap;
	int error, firsttx, nexttx, lasttx = 1, ofree, seg;
	struct tulip_desc *txd;

	DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
	    device_xname(sc->sc_dev), sc->sc_flags, ifp->if_flags));

	/*
	 * If we want a filter setup, it means no more descriptors were
	 * available for the setup routine.  Let it get a chance to wedge
	 * itself into the ring.
	 */
	if (sc->sc_flags & TULIPF_WANT_SETUP)
		return;

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

	if (sc->sc_tick == tlp_2114x_nway_tick &&
	    (sc->sc_flags & TULIPF_LINK_UP) == 0 && ifp->if_snd.ifq_len < 10)
		return;

	/*
	 * Remember the previous number of free descriptors and
	 * the first descriptor we'll use.
	 */
	ofree = sc->sc_txfree;
	firsttx = sc->sc_txnext;

	DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
	    device_xname(sc->sc_dev), ofree, firsttx));

	/*
	 * Loop through the send queue, setting up transmit descriptors
	 * until we drain the queue, or use up all available transmit
	 * descriptors.
	 */
	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
	       sc->sc_txfree != 0) {
		/*
		 * Grab a packet off the queue.
		 */
		IFQ_POLL(&ifp->if_snd, m0);
		if (m0 == NULL)
			break;
		m = NULL;

		dmamap = txs->txs_dmamap;

		/*
		 * Load the DMA map.  If this fails, the packet either
		 * didn't fit in the alloted number of segments, or we were
		 * short on resources.  In this case, we'll copy and try
		 * again.
		 *
		 * Note that if we're only allowed 1 Tx segment, we
		 * have an alignment restriction.  Do this test before
		 * attempting to load the DMA map, because it's more
		 * likely we'll trip the alignment test than the
		 * more-than-one-segment test.
		 */
		if ((sc->sc_ntxsegs == 1 && (mtod(m0, uintptr_t) & 3) != 0) ||
		    bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
		      BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0) {
			MGETHDR(m, M_DONTWAIT, MT_DATA);
			if (m == NULL) {
				aprint_error_dev(sc->sc_dev, "unable to allocate Tx mbuf\n");
				break;
			}
			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
			if (m0->m_pkthdr.len > MHLEN) {
				MCLGET(m, M_DONTWAIT);
				if ((m->m_flags & M_EXT) == 0) {
					aprint_error_dev(sc->sc_dev,
					    "unable to allocate Tx cluster\n");
					m_freem(m);
					break;
				}
			}
			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
			error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
			    m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
			if (error) {
				aprint_error_dev(sc->sc_dev,
				    "unable to load Tx buffer, error = %d",
				    error);
				break;
			}
		}

		/*
		 * Ensure we have enough descriptors free to describe
		 * the packet.
		 */
		if (dmamap->dm_nsegs > sc->sc_txfree) {
			/*
			 * Not enough free descriptors to transmit this
			 * packet.
			 */
			bus_dmamap_unload(sc->sc_dmat, dmamap);
			if (m != NULL)
				m_freem(m);
			break;
		}

		IFQ_DEQUEUE(&ifp->if_snd, m0);
		if (m != NULL) {
			m_freem(m0);
			m0 = m;
		}

		/*
		 * 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);

		/*
		 * Initialize the transmit descriptors.
		 */
		for (nexttx = sc->sc_txnext, seg = 0;
		     seg < dmamap->dm_nsegs;
		     seg++, nexttx = TULIP_NEXTTX(nexttx)) {
			/*
			 * If this is the first descriptor we're
			 * enqueueing, don't set the OWN bit just
			 * yet.  That could cause a race condition.
			 * We'll do it below.
			 */
			txd = &sc->sc_txdescs[nexttx];
			txd->td_status =
			    (nexttx == firsttx) ? 0 : htole32(TDSTAT_OWN);
			txd->td_bufaddr1 =
			    htole32(dmamap->dm_segs[seg].ds_addr);
			txd->td_ctl =
			    htole32((dmamap->dm_segs[seg].ds_len <<
				TDCTL_SIZE1_SHIFT) | sc->sc_tdctl_ch |
				(nexttx == (TULIP_NTXDESC - 1) ?
				 sc->sc_tdctl_er : 0));
			lasttx = nexttx;
		}

		KASSERT(lasttx != -1);

		/* Set `first segment' and `last segment' appropriately. */
		sc->sc_txdescs[sc->sc_txnext].td_ctl |= htole32(TDCTL_Tx_FS);
		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_LS);

#ifdef TLP_DEBUG
		if (ifp->if_flags & IFF_DEBUG) {
			printf("     txsoft %p transmit chain:\n", txs);
			for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
				txd = &sc->sc_txdescs[seg];
				printf("     descriptor %d:\n", seg);
				printf("       td_status:   0x%08x\n",
				    le32toh(txd->td_status));
				printf("       td_ctl:      0x%08x\n",
				    le32toh(txd->td_ctl));
				printf("       td_bufaddr1: 0x%08x\n",
				    le32toh(txd->td_bufaddr1));
				printf("       td_bufaddr2: 0x%08x\n",
				    le32toh(txd->td_bufaddr2));
				if (seg == lasttx)
					break;
			}
		}
#endif

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

		/*
		 * Store a pointer to the packet so we can free it later,
		 * and remember what txdirty will be once the packet is
		 * done.
		 */
		txs->txs_mbuf = m0;
		txs->txs_firstdesc = sc->sc_txnext;
		txs->txs_lastdesc = lasttx;
		txs->txs_ndescs = dmamap->dm_nsegs;

		/* Advance the tx pointer. */
		sc->sc_txfree -= dmamap->dm_nsegs;
		sc->sc_txnext = nexttx;

		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);

		last_txs = txs;

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

	if (sc->sc_txfree != ofree) {
		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
		    device_xname(sc->sc_dev), lasttx, firsttx));
		/*
		 * Cause a transmit interrupt to happen on the
		 * last packet we enqueued.
		 */
		sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_IC);
		TULIP_CDTXSYNC(sc, lasttx, 1,
		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);

		/*
		 * Some clone chips want IC on the *first* segment in
		 * the packet.  Appease them.
		 */
		KASSERT(last_txs != NULL);
		if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
		    last_txs->txs_firstdesc != lasttx) {
			sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
			    htole32(TDCTL_Tx_IC);
			TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
			    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
		}

		/*
		 * The entire packet chain is set up.  Give the
		 * first descriptor to the chip now.
		 */
		sc->sc_txdescs[firsttx].td_status |= htole32(TDSTAT_OWN);
		TULIP_CDTXSYNC(sc, firsttx, 1,
		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);

		/* Wake up the transmitter. */
		/* XXX USE AUTOPOLLING? */
		TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);

		/* Set a watchdog timer in case the chip flakes out. */
		ifp->if_timer = 5;
	}
}

/*
 * tlp_watchdog:	[ifnet interface function]
 *
 *	Watchdog timer handler.
 */
static void
tlp_watchdog(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;
	int doing_setup, doing_transmit;

	doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
	doing_transmit = (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq));

	if (doing_setup && doing_transmit) {
		printf("%s: filter setup and transmit timeout\n",
		    device_xname(sc->sc_dev));
		if_statinc(ifp, if_oerrors);
	} else if (doing_transmit) {
		printf("%s: transmit timeout\n", device_xname(sc->sc_dev));
		if_statinc(ifp, if_oerrors);
	} else if (doing_setup)
		printf("%s: filter setup timeout\n", device_xname(sc->sc_dev));
	else
		printf("%s: spurious watchdog timeout\n",
		    device_xname(sc->sc_dev));

	(void) tlp_init(ifp);

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

/* If the interface is up and running, only modify the receive
 * filter when setting promiscuous or debug mode.  Otherwise fall
 * through to ether_ioctl, which will reset the chip.
 */
static int
tlp_ifflags_cb(struct ethercom *ec)
{
	struct ifnet *ifp = &ec->ec_if;
	struct tulip_softc *sc = ifp->if_softc;
	u_short change = ifp->if_flags ^ sc->sc_if_flags;

	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0)
		return ENETRESET;
	if ((change & IFF_PROMISC) != 0)
		(*sc->sc_filter_setup)(sc);
	return 0;
}

/*
 * tlp_ioctl:		[ifnet interface function]
 *
 *	Handle control requests from the operator.
 */
static int
tlp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
	struct tulip_softc *sc = ifp->if_softc;
	int s, error;

	s = splnet();

	switch (cmd) {
	default:
		error = ether_ioctl(ifp, cmd, data);
		if (error == ENETRESET) {
			if (ifp->if_flags & IFF_RUNNING) {
				/*
				 * Multicast list has changed.  Set the
				 * hardware filter accordingly.
				 */
				(*sc->sc_filter_setup)(sc);
			}
			error = 0;
		}
		break;
	}

	/* Try to get more packets going. */
	if (TULIP_IS_ENABLED(sc))
		tlp_start(ifp);

	sc->sc_if_flags = ifp->if_flags;
	splx(s);
	return error;
}

/*
 * tlp_intr:
 *
 *	Interrupt service routine.
 */
int
tlp_intr(void *arg)
{
	struct tulip_softc *sc = arg;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	uint32_t status, rxstatus, txstatus, rndstatus = 0;
	int handled = 0, txthresh;

	DPRINTF(sc, ("%s: tlp_intr\n", device_xname(sc->sc_dev)));

#ifdef DEBUG
	if (TULIP_IS_ENABLED(sc) == 0)
		panic("%s: tlp_intr: not enabled", device_xname(sc->sc_dev));
#endif

	/*
	 * If the interface isn't running, the interrupt couldn't
	 * possibly have come from us.
	 */
	if ((ifp->if_flags & IFF_RUNNING) == 0 ||
	    !device_is_active(sc->sc_dev))
		return 0;

	/* Disable interrupts on the DM9102 (interrupt edge bug). */
	switch (sc->sc_chip) {
	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
		TULIP_WRITE(sc, CSR_INTEN, 0);
		break;

	default:
		/* Nothing. */
		break;
	}

	for (;;) {
		status = TULIP_READ(sc, CSR_STATUS);
		if (status) {
			TULIP_WRITE(sc, CSR_STATUS, status);
			rndstatus = status;
		}

		if ((status & sc->sc_inten) == 0)
			break;

		handled = 1;

		rxstatus = status & sc->sc_rxint_mask;
		txstatus = status & sc->sc_txint_mask;

		if (rxstatus) {
			/* Grab new any new packets. */
			tlp_rxintr(sc);

			if (rxstatus & STATUS_RWT)
				printf("%s: receive watchdog timeout\n",
				    device_xname(sc->sc_dev));

			if (rxstatus & STATUS_RU) {
				printf("%s: receive ring overrun\n",
				    device_xname(sc->sc_dev));
				/* Get the receive process going again. */
				if (sc->sc_tdctl_er != TDCTL_ER) {
					tlp_idle(sc, OPMODE_SR);
					TULIP_WRITE(sc, CSR_RXLIST,
					    TULIP_CDRXADDR(sc, sc->sc_rxptr));
					TULIP_WRITE(sc, CSR_OPMODE,
					    sc->sc_opmode);
				}
				TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
				break;
			}
		}

		if (txstatus) {
			/* Sweep up transmit descriptors. */
			tlp_txintr(sc);

			if (txstatus & STATUS_TJT)
				printf("%s: transmit jabber timeout\n",
				    device_xname(sc->sc_dev));

			if (txstatus & STATUS_UNF) {
				/*
				 * Increase our transmit threshold if
				 * another is available.
				 */
				txthresh = sc->sc_txthresh + 1;
				if (sc->sc_txth[txthresh].txth_name != NULL) {
					/* Idle the transmit process. */
					tlp_idle(sc, OPMODE_ST);

					sc->sc_txthresh = txthresh;
					sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
					sc->sc_opmode |=
					    sc->sc_txth[txthresh].txth_opmode;
					printf("%s: transmit underrun; new "
					    "threshold: %s\n",
					    device_xname(sc->sc_dev),
					    sc->sc_txth[txthresh].txth_name);

					/*
					 * Set the new threshold and restart
					 * the transmit process.
					 */
					TULIP_WRITE(sc, CSR_OPMODE,
					    sc->sc_opmode);
				}
					/*
					 * XXX Log every Nth underrun from
					 * XXX now on?
					 */
			}
		}

		if (status & (STATUS_TPS | STATUS_RPS)) {
			if (status & STATUS_TPS)
				printf("%s: transmit process stopped\n",
				    device_xname(sc->sc_dev));
			if (status & STATUS_RPS)
				printf("%s: receive process stopped\n",
				    device_xname(sc->sc_dev));
			(void) tlp_init(ifp);
			break;
		}

		if (status & STATUS_SE) {
			const char *str;
			switch (status & STATUS_EB) {
			case STATUS_EB_PARITY:
				str = "parity error";
				break;

			case STATUS_EB_MABT:
				str = "master abort";
				break;

			case STATUS_EB_TABT:
				str = "target abort";
				break;

			default:
				str = "unknown error";
				break;
			}
			aprint_error_dev(sc->sc_dev,
			    "fatal system error: %s\n", str);
			(void) tlp_init(ifp);
			break;
		}

		/*
		 * Not handled:
		 *
		 *	Transmit buffer unavailable -- normal
		 *	condition, nothing to do, really.
		 *
		 *	General purpose timer experied -- we don't
		 *	use the general purpose timer.
		 *
		 *	Early receive interrupt -- not available on
		 *	all chips, we just use RI.  We also only
		 *	use single-segment receive DMA, so this
		 *	is mostly useless.
		 */
	}

	/* Bring interrupts back up on the DM9102. */
	switch (sc->sc_chip) {
	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
		TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
		break;

	default:
		/* Nothing. */
		break;
	}

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

	if (handled)
		rnd_add_uint32(&sc->sc_rnd_source, rndstatus);

	return handled;
}

/*
 * tlp_rxintr:
 *
 *	Helper; handle receive interrupts.
 */
static void
tlp_rxintr(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct ether_header *eh;
	struct tulip_rxsoft *rxs;
	struct mbuf *m;
	uint32_t rxstat, errors;
	int i, len;

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

		TULIP_CDRXSYNC(sc, i,
		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);

		rxstat = le32toh(sc->sc_rxdescs[i].td_status);

		if (rxstat & TDSTAT_OWN) {
			/*
			 * We have processed all of the receive buffers.
			 */
			break;
		}

		/*
		 * Make sure the packet fit in one buffer.  This should
		 * always be the case.  But the Lite-On PNIC, rev 33
		 * has an awful receive engine bug, which may require
		 * a very icky work-around.
		 */
		if ((rxstat & (TDSTAT_Rx_FS | TDSTAT_Rx_LS)) !=
		    (TDSTAT_Rx_FS | TDSTAT_Rx_LS)) {
			printf("%s: incoming packet spilled, resetting\n",
			    device_xname(sc->sc_dev));
			(void) tlp_init(ifp);
			return;
		}

		/*
		 * If any collisions were seen on the wire, count one.
		 */
		if (rxstat & TDSTAT_Rx_CS)
			if_statinc(ifp, if_collisions);

		/*
		 * If an error occurred, update stats, clear the status
		 * word, and leave the packet buffer in place.  It will
		 * simply be reused the next time the ring comes around.
		 */
		errors = TDSTAT_Rx_DE | TDSTAT_Rx_RF | TDSTAT_Rx_TL |
		    TDSTAT_Rx_CS | TDSTAT_Rx_RE | TDSTAT_Rx_DB | TDSTAT_Rx_CE;
		/*
	 	 * If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long
		 * error.
		 */
		if ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) != 0)
			errors &= ~TDSTAT_Rx_TL;
		/*
		 * If chip doesn't have MII, ignore the MII error bit.
		 */
		if ((sc->sc_flags & TULIPF_HAS_MII) == 0)
			errors &= ~TDSTAT_Rx_RE;

		if ((rxstat & TDSTAT_ES) != 0 &&
		    (rxstat & errors) != 0) {
			rxstat &= errors;
#define	PRINTERR(bit, str)						\
			if (rxstat & (bit))				\
				aprint_error_dev(sc->sc_dev,		\
				    "receive error: %s\n", str)
			if_statinc(ifp, if_ierrors);
			PRINTERR(TDSTAT_Rx_DE, "descriptor error");
			PRINTERR(TDSTAT_Rx_RF, "runt frame");
			PRINTERR(TDSTAT_Rx_TL, "frame too long");
			PRINTERR(TDSTAT_Rx_RE, "MII error");
			PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
			PRINTERR(TDSTAT_Rx_CE, "CRC error");
#undef PRINTERR
			TULIP_INIT_RXDESC(sc, i);
			continue;
		}

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

		/*
		 * No errors; receive the packet.  Note the Tulip
		 * includes the CRC with every packet.
		 */
		len = TDSTAT_Rx_LENGTH(rxstat) - ETHER_CRC_LEN;

#ifdef __NO_STRICT_ALIGNMENT
		/*
		 * Allocate a new mbuf cluster.  If that fails, we are
		 * out of memory, and must drop the packet and recycle
		 * the buffer that's already attached to this descriptor.
		 */
		m = rxs->rxs_mbuf;
		if (tlp_add_rxbuf(sc, i) != 0) {
			if_statinc(ifp, if_ierrors);
			TULIP_INIT_RXDESC(sc, i);
			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
			continue;
		}
#else
		/*
		 * The Tulip's receive buffers must be 4-byte aligned.
		 * But this means that the data after the Ethernet header
		 * is misaligned.  We must allocate a new buffer and
		 * copy the data, shifted forward 2 bytes.
		 */
		MGETHDR(m, M_DONTWAIT, MT_DATA);
		if (m == NULL) {
 dropit:
			if_statinc(ifp, if_ierrors);
			TULIP_INIT_RXDESC(sc, i);
			bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
			continue;
		}
		MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
		if (len > (MHLEN - 2)) {
			MCLGET(m, M_DONTWAIT);
			if ((m->m_flags & M_EXT) == 0) {
				m_freem(m);
				goto dropit;
			}
		}
		m->m_data += 2;

		/*
		 * Note that we use clusters for incoming frames, so the
		 * buffer is virtually contiguous.
		 */
		memcpy(mtod(m, void *), mtod(rxs->rxs_mbuf, void *), len);

		/* Allow the receive descriptor to continue using its mbuf. */
		TULIP_INIT_RXDESC(sc, i);
		bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
#endif /* __NO_STRICT_ALIGNMENT */

		eh = mtod(m, struct ether_header *);
		m_set_rcvif(m, ifp);
		m->m_pkthdr.len = m->m_len = len;

		/*
		 * XXX Work-around for a weird problem with the emulated
		 * 21041 on Connectix Virtual PC:
		 *
		 * When we receive a full-size TCP segment, we seem to get
		 * a packet there the Rx status says 1522 bytes, yet we do
		 * not get a frame-too-long error from the chip.  The extra
		 * bytes seem to always be zeros.  Perhaps Virtual PC is
		 * inserting 4 bytes of zeros after every packet.  In any
		 * case, let's try and detect this condition and truncate
		 * the length so that it will pass up the stack.
		 */
		if (__predict_false((sc->sc_flags & TULIPF_VPC) != 0)) {
			uint16_t etype = ntohs(eh->ether_type);

			if (len > ETHER_MAX_FRAME(ifp, etype, 0))
				m->m_pkthdr.len = m->m_len = len =
				    ETHER_MAX_FRAME(ifp, etype, 0);
		}

		/*
		 * We sometimes have to run the 21140 in Hash-Only
		 * mode.  If we're in that mode, and not in promiscuous
		 * mode, and we have a unicast packet that isn't for
		 * us, then drop it.
		 */
		if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY &&
		    (ifp->if_flags & IFF_PROMISC) == 0 &&
		    ETHER_IS_MULTICAST(eh->ether_dhost) == 0 &&
		    memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
			   ETHER_ADDR_LEN) != 0) {
			m_freem(m);
			continue;
		}

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

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

/*
 * tlp_txintr:
 *
 *	Helper; handle transmit interrupts.
 */
static void
tlp_txintr(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct tulip_txsoft *txs;
	uint32_t txstat;

	DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
	    device_xname(sc->sc_dev), sc->sc_flags));

	/*
	 * Go through our Tx list and free mbufs for those
	 * frames that have been transmitted.
	 */
	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
		TULIP_CDTXSYNC(sc, txs->txs_lastdesc, txs->txs_ndescs,
		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);

#ifdef TLP_DEBUG
		if (ifp->if_flags & IFF_DEBUG) {
			int i;
			struct tulip_desc *txd;
			printf("    txsoft %p transmit chain:\n", txs);
			for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
				txd = &sc->sc_txdescs[i];
				printf("     descriptor %d:\n", i);
				printf("       td_status:   0x%08x\n",
				    le32toh(txd->td_status));
				printf("       td_ctl:      0x%08x\n",
				    le32toh(txd->td_ctl));
				printf("       td_bufaddr1: 0x%08x\n",
				    le32toh(txd->td_bufaddr1));
				printf("       td_bufaddr2: 0x%08x\n",
				    le32toh(sc->sc_txdescs[i].td_bufaddr2));
				if (i == txs->txs_lastdesc)
					break;
			}
		}
#endif

		txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].td_status);
		if (txstat & TDSTAT_OWN)
			break;

		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);

		sc->sc_txfree += txs->txs_ndescs;

		if (txs->txs_mbuf == NULL) {
			/*
			 * If we didn't have an mbuf, it was the setup
			 * packet.
			 */
#ifdef DIAGNOSTIC
			if ((sc->sc_flags & TULIPF_DOING_SETUP) == 0)
				panic("tlp_txintr: null mbuf, not doing setup");
#endif
			TULIP_CDSPSYNC(sc, BUS_DMASYNC_POSTWRITE);
			sc->sc_flags &= ~TULIPF_DOING_SETUP;
			SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
			continue;
		}

		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;

		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);

		/*
		 * Check for errors and collisions.
		 */
#ifdef TLP_STATS
		if (txstat & TDSTAT_Tx_UF)
			sc->sc_stats.ts_tx_uf++;
		if (txstat & TDSTAT_Tx_TO)
			sc->sc_stats.ts_tx_to++;
		if (txstat & TDSTAT_Tx_EC)
			sc->sc_stats.ts_tx_ec++;
		if (txstat & TDSTAT_Tx_LC)
			sc->sc_stats.ts_tx_lc++;
#endif
		net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
		if (txstat & (TDSTAT_Tx_UF | TDSTAT_Tx_TO))
			if_statinc_ref(nsr, if_oerrors);

		if (txstat & TDSTAT_Tx_EC)
			if_statadd_ref(nsr, if_collisions, 16);
		else
			if_statadd_ref(nsr, if_collisions,
			    TDSTAT_Tx_COLLISIONS(txstat));
		if (txstat & TDSTAT_Tx_LC)
			if_statinc_ref(nsr, if_collisions);

		if_statinc_ref(nsr, if_opackets);
		IF_STAT_PUTREF(ifp);
	}

	/*
	 * If there are no more pending transmissions, cancel the watchdog
	 * timer.
	 */
	if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
		ifp->if_timer = 0;

	/*
	 * If we have a receive filter setup pending, do it now.
	 */
	if (sc->sc_flags & TULIPF_WANT_SETUP)
		(*sc->sc_filter_setup)(sc);
}

#ifdef TLP_STATS
void
tlp_print_stats(struct tulip_softc *sc)
{

	printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
	    device_xname(sc->sc_dev),
	    sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
	    sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
}
#endif

/*
 * tlp_reset:
 *
 *	Perform a soft reset on the Tulip.
 */
void
tlp_reset(struct tulip_softc *sc)
{
	int i;

	TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);

	/*
	 * Xircom, ASIX and Conexant clones don't bring themselves
	 * out of reset automatically.
	 * Instead, we have to wait at least 50 PCI cycles, and then
	 * clear SWR.
	 */
	switch (sc->sc_chip) {
		case TULIP_CHIP_X3201_3:
		case TULIP_CHIP_AX88140:
		case TULIP_CHIP_AX88141:
		case TULIP_CHIP_RS7112:
			delay(10);
			TULIP_WRITE(sc, CSR_BUSMODE, 0);
			break;
		default:
			break;
	}

	for (i = 0; i < 1000; i++) {
		/*
		 * Wait at least 50 PCI cycles for the reset to
		 * complete before peeking at the Tulip again.
		 * 10 uSec is a bit longer than 50 PCI cycles
		 * (at 33MHz), but it doesn't hurt have the extra
		 * wait.
		 */
		delay(10);
		if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
			break;
	}

	if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
		aprint_error_dev(sc->sc_dev, "reset failed to complete\n");

	delay(1000);

	/*
	 * If the board has any GPIO reset sequences to issue, do them now.
	 */
	if (sc->sc_reset != NULL)
		(*sc->sc_reset)(sc);
}

/*
 * tlp_init:		[ ifnet interface function ]
 *
 *	Initialize the interface.  Must be called at splnet().
 */
static int
tlp_init(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;
	struct tulip_txsoft *txs;
	struct tulip_rxsoft *rxs;
	int i, error = 0;

	if ((error = tlp_enable(sc)) != 0)
		goto out;

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

	/*
	 * Initialize `opmode' to 0, and call the pre-init routine, if
	 * any.  This is required because the 2114x and some of the
	 * clones require that the media-related bits in `opmode' be
	 * set before performing a soft-reset in order to get internal
	 * chip pathways are correct.  Yay!
	 */
	sc->sc_opmode = 0;
	if (sc->sc_preinit != NULL)
		(*sc->sc_preinit)(sc);

	/*
	 * Reset the Tulip to a known state.
	 */
	tlp_reset(sc);

	/*
	 * Initialize the BUSMODE register.
	 */
	sc->sc_busmode = BUSMODE_BAR;
	switch (sc->sc_chip) {
	case TULIP_CHIP_21140:
	case TULIP_CHIP_21140A:
	case TULIP_CHIP_21142:
	case TULIP_CHIP_21143:
	case TULIP_CHIP_82C115:
	case TULIP_CHIP_MX98725:
		/*
		 * If we're allowed to do so, use Memory Read Line
		 * and Memory Read Multiple.
		 *
		 * XXX Should we use Memory Write and Invalidate?
		 */
		if (sc->sc_flags & TULIPF_MRL)
			sc->sc_busmode |= BUSMODE_RLE;
		if (sc->sc_flags & TULIPF_MRM)
			sc->sc_busmode |= BUSMODE_RME;
#if 0
		if (sc->sc_flags & TULIPF_MWI)
			sc->sc_busmode |= BUSMODE_WLE;
#endif
		break;

	case TULIP_CHIP_82C168:
	case TULIP_CHIP_82C169:
		sc->sc_busmode |= BUSMODE_PNIC_MBO;
		if (sc->sc_maxburst == 0)
			sc->sc_maxburst = 16;
		break;

	case TULIP_CHIP_AX88140:
	case TULIP_CHIP_AX88141:
		if (sc->sc_maxburst == 0)
			sc->sc_maxburst = 16;
		break;

	default:
		/* Nothing. */
		break;
	}
	switch (sc->sc_cacheline) {
	default:
		/*
		 * Note: We must *always* set these bits; a cache
		 * alignment of 0 is RESERVED.
		 */
	case 8:
		sc->sc_busmode |= BUSMODE_CAL_8LW;
		break;
	case 16:
		sc->sc_busmode |= BUSMODE_CAL_16LW;
		break;
	case 32:
		sc->sc_busmode |= BUSMODE_CAL_32LW;
		break;
	}
	switch (sc->sc_maxburst) {
	case 1:
		sc->sc_busmode |= BUSMODE_PBL_1LW;
		break;
	case 2:
		sc->sc_busmode |= BUSMODE_PBL_2LW;
		break;
	case 4:
		sc->sc_busmode |= BUSMODE_PBL_4LW;
		break;
	case 8:
		sc->sc_busmode |= BUSMODE_PBL_8LW;
		break;
	case 16:
		sc->sc_busmode |= BUSMODE_PBL_16LW;
		break;
	case 32:
		sc->sc_busmode |= BUSMODE_PBL_32LW;
		break;
	default:
		sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
		break;
	}
#if BYTE_ORDER == BIG_ENDIAN
	/*
	 * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips
	 * support them, and even on ones that do, it doesn't
	 * always work.  So we always access descriptors with
	 * little endian via htole32/le32toh.
	 */
#endif
	/*
	 * Big-endian bus requires BUSMODE_BLE anyway.
	 * Also, BUSMODE_DBO is needed because we assume
	 * descriptors are little endian.
	 */
	if (sc->sc_flags & TULIPF_BLE)
		sc->sc_busmode |= BUSMODE_BLE;
	if (sc->sc_flags & TULIPF_DBO)
		sc->sc_busmode |= BUSMODE_DBO;

	/*
	 * Some chips have a broken bus interface.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
		sc->sc_busmode = 0;
		break;

	default:
		/* Nothing. */
		break;
	}

	TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);

	/*
	 * Initialize the OPMODE register.  We don't write it until
	 * we're ready to begin the transmit and receive processes.
	 *
	 * Media-related OPMODE bits are set in the media callbacks
	 * for each specific chip/board.
	 */
	sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
	    sc->sc_txth[sc->sc_txthresh].txth_opmode;

	/*
	 * Magical mystery initialization on the Macronix chips.
	 * The MX98713 uses its own magic value, the rest share
	 * a common one.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_MX98713:
		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
		break;

	case TULIP_CHIP_MX98713A:
	case TULIP_CHIP_MX98715:
	case TULIP_CHIP_MX98715A:
	case TULIP_CHIP_MX98715AEC_X:
	case TULIP_CHIP_MX98725:
		TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
		break;

	default:
		/* Nothing. */
		break;
	}

	/*
	 * Initialize the transmit descriptor ring.
	 */
	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
	for (i = 0; i < TULIP_NTXDESC; i++) {
		struct tulip_desc *txd = &sc->sc_txdescs[i];
		txd->td_ctl = htole32(sc->sc_tdctl_ch);
		txd->td_bufaddr2 = htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i)));
	}
	sc->sc_txdescs[TULIP_NTXDESC - 1].td_ctl |= htole32(sc->sc_tdctl_er);
	TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
	sc->sc_txfree = TULIP_NTXDESC;
	sc->sc_txnext = 0;

	/*
	 * Initialize the transmit job descriptors.
	 */
	SIMPLEQ_INIT(&sc->sc_txfreeq);
	SIMPLEQ_INIT(&sc->sc_txdirtyq);
	for (i = 0; i < TULIP_TXQUEUELEN; i++) {
		txs = &sc->sc_txsoft[i];
		txs->txs_mbuf = NULL;
		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
	}

	/*
	 * Initialize the receive descriptor and receive job
	 * descriptor rings.
	 */
	for (i = 0; i < TULIP_NRXDESC; i++) {
		rxs = &sc->sc_rxsoft[i];
		if (rxs->rxs_mbuf == NULL) {
			if ((error = tlp_add_rxbuf(sc, i)) != 0) {
				aprint_error_dev(sc->sc_dev,
				    "unable to allocate or map rx "
				    "buffer %d, error = %d\n", i, error);
				/*
				 * XXX Should attempt to run with fewer receive
				 * XXX buffers instead of just failing.
				 */
				tlp_rxdrain(sc);
				goto out;
			}
		} else
			TULIP_INIT_RXDESC(sc, i);
	}
	sc->sc_rxptr = 0;

	/*
	 * Initialize the interrupt mask and enable interrupts.
	 */
	/* normal interrupts */
	sc->sc_inten = STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;

	/* abnormal interrupts */
	sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
	    STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;

	sc->sc_rxint_mask = STATUS_RI | STATUS_RU | STATUS_RWT;
	sc->sc_txint_mask = STATUS_TI | STATUS_UNF | STATUS_TJT;

	switch (sc->sc_chip) {
	case TULIP_CHIP_WB89C840F:
		/*
		 * Clear bits that we don't want that happen to
		 * overlap or don't exist.
		 */
		sc->sc_inten &= ~(STATUS_WINB_REI | STATUS_RWT);
		break;

	default:
		/* Nothing. */
		break;
	}

	sc->sc_rxint_mask &= sc->sc_inten;
	sc->sc_txint_mask &= sc->sc_inten;

	TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
	TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);

	/*
	 * Give the transmit and receive rings to the Tulip.
	 */
	TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
	TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));

	/*
	 * On chips that do this differently, set the station address.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_WB89C840F:
	    {
		/* XXX Do this with stream writes? */
		bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);

		for (i = 0; i < ETHER_ADDR_LEN; i++) {
			bus_space_write_1(sc->sc_st, sc->sc_sh,
			    cpa + i, CLLADDR(ifp->if_sadl)[i]);
		}
		break;
	    }

	case TULIP_CHIP_AL981:
	case TULIP_CHIP_AN983:
	case TULIP_CHIP_AN985:
	    {
		uint32_t reg;
		const uint8_t *enaddr = CLLADDR(ifp->if_sadl);

		reg = enaddr[0] |
		    (enaddr[1] << 8) |
		    (enaddr[2] << 16) |
		    ((uint32_t)enaddr[3] << 24);
		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);

		reg = enaddr[4] |
		      (enaddr[5] << 8);
		bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
		break;
	    }

	case TULIP_CHIP_AX88140:
	case TULIP_CHIP_AX88141:
	    {
		uint32_t reg;
		const uint8_t *enaddr = CLLADDR(ifp->if_sadl);

		reg = enaddr[0] |
		    (enaddr[1] << 8) |
		    (enaddr[2] << 16) |
		    ((uint32_t)enaddr[3] << 24);
		TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_PAR0);
		TULIP_WRITE(sc, CSR_AX_FILTDATA, reg);

		reg = enaddr[4] | (enaddr[5] << 8);
		TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_PAR1);
		TULIP_WRITE(sc, CSR_AX_FILTDATA, reg);
		break;
	    }

	default:
		/* Nothing. */
		break;
	}

	/*
	 * Set the receive filter.  This will start the transmit and
	 * receive processes.
	 */
	(*sc->sc_filter_setup)(sc);

	/*
	 * Set the current media.
	 */
	(void)(*sc->sc_mediasw->tmsw_set)(sc);

	/*
	 * Start the receive process.
	 */
	TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);

	if (sc->sc_tick != NULL) {
		/* Start the one second clock. */
		callout_reset(&sc->sc_tick_callout, hz >> 3, sc->sc_tick, sc);
	}

	/*
	 * Note that the interface is now running.
	 */
	ifp->if_flags |= IFF_RUNNING;
	sc->sc_if_flags = ifp->if_flags;

 out:
	if (error) {
		ifp->if_flags &= ~IFF_RUNNING;
		ifp->if_timer = 0;
		printf("%s: interface not running\n", device_xname(sc->sc_dev));
	}
	return error;
}

/*
 * tlp_enable:
 *
 *	Enable the Tulip chip.
 */
static int
tlp_enable(struct tulip_softc *sc)
{

	if (TULIP_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
		if ((*sc->sc_enable)(sc) != 0) {
			aprint_error_dev(sc->sc_dev, "device enable failed\n");
			return EIO;
		}
		sc->sc_flags |= TULIPF_ENABLED;
	}
	return 0;
}

/*
 * tlp_disable:
 *
 *	Disable the Tulip chip.
 */
static void
tlp_disable(struct tulip_softc *sc)
{

	if (TULIP_IS_ENABLED(sc) && sc->sc_disable != NULL) {
		(*sc->sc_disable)(sc);
		sc->sc_flags &= ~TULIPF_ENABLED;
	}
}

/*
 * tlp_rxdrain:
 *
 *	Drain the receive queue.
 */
static void
tlp_rxdrain(struct tulip_softc *sc)
{
	struct tulip_rxsoft *rxs;
	int i;

	for (i = 0; i < TULIP_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;
		}
	}
}

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

	if (sc->sc_tick != NULL) {
		/* Stop the one second clock. */
		callout_stop(&sc->sc_tick_callout);
	}

	if (sc->sc_flags & TULIPF_HAS_MII) {
		/* Down the MII. */
		mii_down(&sc->sc_mii);
	}

	/* Disable interrupts. */
	TULIP_WRITE(sc, CSR_INTEN, 0);

	/* Stop the transmit and receive processes. */
	sc->sc_opmode = 0;
	TULIP_WRITE(sc, CSR_OPMODE, 0);
	TULIP_WRITE(sc, CSR_RXLIST, 0);
	TULIP_WRITE(sc, CSR_TXLIST, 0);

	/*
	 * Release any queued transmit buffers.
	 */
	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
		if (txs->txs_mbuf != NULL) {
			bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
			m_freem(txs->txs_mbuf);
			txs->txs_mbuf = NULL;
		}
		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
	}

	sc->sc_flags &= ~(TULIPF_WANT_SETUP | TULIPF_DOING_SETUP);

	/*
	 * Mark the interface down and cancel the watchdog timer.
	 */
	ifp->if_flags &= ~IFF_RUNNING;
	sc->sc_if_flags = ifp->if_flags;
	ifp->if_timer = 0;

	/*
	 * Reset the chip (needed on some flavors to actually disable it).
	 */
	tlp_reset(sc);

	if (disable) {
		tlp_rxdrain(sc);
		tlp_disable(sc);
	}
}

#define	SROM_EMIT(sc, x)						\
do {									\
	TULIP_WRITE((sc), CSR_MIIROM, (x));				\
	delay(2);							\
} while (0)

/*
 * tlp_srom_idle:
 *
 *	Put the SROM in idle state.
 */
static void
tlp_srom_idle(struct tulip_softc *sc)
{
	uint32_t miirom;
	int i;

	miirom = MIIROM_SR;
	SROM_EMIT(sc, miirom);

	miirom |= MIIROM_RD;
	SROM_EMIT(sc, miirom);

	miirom |= MIIROM_SROMCS;
	SROM_EMIT(sc, miirom);

	SROM_EMIT(sc, miirom | MIIROM_SROMSK);

	/* Strobe the clock 32 times. */
	for (i = 0; i < 32; i++) {
		SROM_EMIT(sc, miirom);
		SROM_EMIT(sc, miirom | MIIROM_SROMSK);
	}

	SROM_EMIT(sc, miirom);

	miirom &= ~MIIROM_SROMCS;
	SROM_EMIT(sc, miirom);

	SROM_EMIT(sc, 0);
}

/*
 * tlp_srom_size:
 *
 *	Determine the number of address bits in the SROM.
 */
static int
tlp_srom_size(struct tulip_softc *sc)
{
	uint32_t miirom;
	int x;

	/* Select the SROM. */
	miirom = MIIROM_SR;
	SROM_EMIT(sc, miirom);

	miirom |= MIIROM_RD;
	SROM_EMIT(sc, miirom);

	/* Send CHIP SELECT for one clock tick. */
	miirom |= MIIROM_SROMCS;
	SROM_EMIT(sc, miirom);

	/* Shift in the READ opcode. */
	for (x = 3; x > 0; x--) {
		if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
			miirom |= MIIROM_SROMDI;
		else
			miirom &= ~MIIROM_SROMDI;
		SROM_EMIT(sc, miirom);
		SROM_EMIT(sc, miirom | MIIROM_SROMSK);
		SROM_EMIT(sc, miirom);
	}

	/* Shift in address and look for dummy 0 bit. */
	for (x = 1; x <= 12; x++) {
		miirom &= ~MIIROM_SROMDI;
		SROM_EMIT(sc, miirom);
		SROM_EMIT(sc, miirom | MIIROM_SROMSK);
		if (!TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
			break;
		SROM_EMIT(sc, miirom);
	}

	/* Clear CHIP SELECT. */
	miirom &= ~MIIROM_SROMCS;
	SROM_EMIT(sc, miirom);

	/* Deselect the SROM. */
	SROM_EMIT(sc, 0);

	if (x < 4 || x > 12) {
		aprint_debug_dev(sc->sc_dev, "broken MicroWire interface "
		    "detected; setting SROM size to 1Kb\n");
		return 6;
	} else {
		if (tlp_srom_debug)
			printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
			    device_xname(sc->sc_dev), x, (1 << (x + 4)) >> 3);
		return x;
	}
}

/*
 * tlp_read_srom:
 *
 *	Read the Tulip SROM.
 */
int
tlp_read_srom(struct tulip_softc *sc)
{
	int size;
	uint32_t miirom;
	uint16_t datain;
	int i, x;

	tlp_srom_idle(sc);

	sc->sc_srom_addrbits = tlp_srom_size(sc);
	if (sc->sc_srom_addrbits == 0)
		return 0;
	size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
	sc->sc_srom = malloc(size, M_DEVBUF, M_WAITOK);

	/* Select the SROM. */
	miirom = MIIROM_SR;
	SROM_EMIT(sc, miirom);

	miirom |= MIIROM_RD;
	SROM_EMIT(sc, miirom);

	for (i = 0; i < size; i += 2) {
		/* Send CHIP SELECT for one clock tick. */
		miirom |= MIIROM_SROMCS;
		SROM_EMIT(sc, miirom);

		/* Shift in the READ opcode. */
		for (x = 3; x > 0; x--) {
			if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
				miirom |= MIIROM_SROMDI;
			else
				miirom &= ~MIIROM_SROMDI;
			SROM_EMIT(sc, miirom);
			SROM_EMIT(sc, miirom | MIIROM_SROMSK);
			SROM_EMIT(sc, miirom);
		}

		/* Shift in address. */
		for (x = sc->sc_srom_addrbits; x > 0; x--) {
			if (i & (1 << x))
				miirom |= MIIROM_SROMDI;
			else
				miirom &= ~MIIROM_SROMDI;
			SROM_EMIT(sc, miirom);
			SROM_EMIT(sc, miirom | MIIROM_SROMSK);
			SROM_EMIT(sc, miirom);
		}

		/* Shift out data. */
		miirom &= ~MIIROM_SROMDI;
		datain = 0;
		for (x = 16; x > 0; x--) {
			SROM_EMIT(sc, miirom | MIIROM_SROMSK);
			if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
				datain |= (1 << (x - 1));
			SROM_EMIT(sc, miirom);
		}
		sc->sc_srom[i] = datain & 0xff;
		sc->sc_srom[i + 1] = datain >> 8;

		/* Clear CHIP SELECT. */
		miirom &= ~MIIROM_SROMCS;
		SROM_EMIT(sc, miirom);
	}

	/* Deselect the SROM. */
	SROM_EMIT(sc, 0);

	/* ...and idle it. */
	tlp_srom_idle(sc);

	if (tlp_srom_debug) {
		printf("SROM CONTENTS:");
		for (i = 0; i < size; i++) {
			if ((i % 8) == 0)
				printf("\n\t");
			printf("0x%02x ", sc->sc_srom[i]);
		}
		printf("\n");
	}

	return 1;
}

#undef SROM_EMIT

/*
 * tlp_add_rxbuf:
 *
 *	Add a receive buffer to the indicated descriptor.
 */
static int
tlp_add_rxbuf(struct tulip_softc *sc, int idx)
{
	struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
	struct mbuf *m;
	int error;

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

	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
	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;

	error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
	    BUS_DMA_READ | BUS_DMA_NOWAIT);
	if (error) {
		aprint_error_dev(sc->sc_dev,
		    "can't load rx DMA map %d, error = %d\n", idx, error);
		panic("tlp_add_rxbuf");	/* XXX */
	}

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

	TULIP_INIT_RXDESC(sc, idx);

	return 0;
}

/*
 * tlp_srom_crcok:
 *
 *	Check the CRC of the Tulip SROM.
 */
int
tlp_srom_crcok(const uint8_t *romdata)
{
	uint32_t crc;

	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM);
	crc = (crc & 0xffff) ^ 0xffff;
	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
		return 1;

	/*
	 * Try an alternate checksum.
	 */
	crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM1);
	crc = (crc & 0xffff) ^ 0xffff;
	if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
		return 1;

	return 0;
}

/*
 * tlp_isv_srom:
 *
 *	Check to see if the SROM is in the new standardized format.
 */
int
tlp_isv_srom(const uint8_t *romdata)
{
	int i;
	uint16_t cksum;

	if (tlp_srom_crcok(romdata)) {
		/*
		 * SROM CRC checks out; must be in the new format.
		 */
		return 1;
	}

	cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
	if (cksum == 0xffff || cksum == 0) {
		/*
		 * No checksum present.  Check the SROM ID; 18 bytes of 0
		 * followed by 1 (version) followed by the number of
		 * adapters which use this SROM (should be non-zero).
		 */
		for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
			if (romdata[i] != 0)
				return 0;
		}
		if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
			return 0;
		if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
			return 0;
		return 1;
	}

	return 0;
}

/*
 * tlp_isv_srom_enaddr:
 *
 *	Get the Ethernet address from an ISV SROM.
 */
int
tlp_isv_srom_enaddr(struct tulip_softc *sc, uint8_t *enaddr)
{
	int i, devcnt;

	if (tlp_isv_srom(sc->sc_srom) == 0)
		return 0;

	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
	for (i = 0; i < devcnt; i++) {
		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
			break;
		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
		    sc->sc_devno)
			break;
	}

	if (i == devcnt)
		return 0;

	memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
	    ETHER_ADDR_LEN);
	enaddr[5] += i;

	return 1;
}

/*
 * tlp_parse_old_srom:
 *
 *	Parse old-format SROMs.
 *
 *	This routine is largely lifted from Matt Thomas's `de' driver.
 */
int
tlp_parse_old_srom(struct tulip_softc *sc, uint8_t *enaddr)
{
	static const uint8_t testpat[] =
	    { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
	int i;
	uint32_t cksum;

	if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
		/*
		 * Phobos G100 interfaces have the address at
		 * offsets 0 and 20, but each pair of bytes is
		 * swapped.
		 */
		if (sc->sc_srom_addrbits == 6 &&
		    sc->sc_srom[1] == 0x00 &&
		    sc->sc_srom[0] == 0x60 &&
		    sc->sc_srom[3] == 0xf5 &&
		    memcmp(&sc->sc_srom[0], &sc->sc_srom[20], 6) == 0) {
			for (i = 0; i < 6; i += 2) {
				enaddr[i] = sc->sc_srom[i + 1];
				enaddr[i + 1] = sc->sc_srom[i];
			}
			return 1;
		}

		/*
		 * Phobos G130/G160 interfaces have the address at
		 * offsets 20 and 84, but each pair of bytes is
		 * swapped.
		 */
		if (sc->sc_srom_addrbits == 6 &&
		    sc->sc_srom[21] == 0x00 &&
		    sc->sc_srom[20] == 0x60 &&
		    sc->sc_srom[23] == 0xf5 &&
		    memcmp(&sc->sc_srom[20], &sc->sc_srom[84], 6) == 0) {
			for (i = 0; i < 6; i += 2) {
				enaddr[i] = sc->sc_srom[20 + i + 1];
				enaddr[i + 1] = sc->sc_srom[20 + i];
			}
			return 1;
		}

		/*
		 * Cobalt Networks interfaces simply have the address
		 * in the first six bytes. The rest is zeroed out
		 * on some models, but others contain unknown data.
		 */
		if (sc->sc_srom[0] == 0x00 &&
		    sc->sc_srom[1] == 0x10 &&
		    sc->sc_srom[2] == 0xe0) {
			memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
			return 1;
		}

		/*
		 * Some vendors (e.g. ZNYX) don't use the standard
		 * DEC Address ROM format, but rather just have an
		 * Ethernet address in the first 6 bytes, maybe a
		 * 2 byte checksum, and then all 0xff's.
		 */
		for (i = 8; i < 32; i++) {
			if (sc->sc_srom[i] != 0xff &&
			    sc->sc_srom[i] != 0)
				return 0;
		}

		/*
		 * Sanity check the Ethernet address:
		 *
		 *	- Make sure it's not multicast or locally
		 *	  assigned
		 *	- Make sure it has a non-0 OUI
		 */
		if (sc->sc_srom[0] & 3)
			return 0;
		if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
		    sc->sc_srom[2] == 0)
			return 0;

		memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
		return 1;
	}

	/*
	 * Standard DEC Address ROM test.
	 */

	if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
		return 0;

	for (i = 0; i < 8; i++) {
		if (sc->sc_srom[i] != sc->sc_srom[15 - i])
			return 0;
	}

	memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);

	cksum = *(uint16_t *) &enaddr[0];

	cksum <<= 1;
	if (cksum > 0xffff)
		cksum -= 0xffff;

	cksum += *(uint16_t *) &enaddr[2];
	if (cksum > 0xffff)
		cksum -= 0xffff;

	cksum <<= 1;
	if (cksum > 0xffff)
		cksum -= 0xffff;

	cksum += *(uint16_t *) &enaddr[4];
	if (cksum >= 0xffff)
		cksum -= 0xffff;

	if (cksum != *(uint16_t *) &sc->sc_srom[6])
		return 0;

	return 1;
}

/*
 * tlp_filter_setup:
 *
 *	Set the Tulip's receive filter.
 */
static void
tlp_filter_setup(struct tulip_softc *sc)
{
	struct ethercom *ec = &sc->sc_ethercom;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct ether_multi *enm;
	struct ether_multistep step;
	volatile uint32_t *sp;
	struct tulip_txsoft *txs;
	struct tulip_desc *txd;
	uint8_t enaddr[ETHER_ADDR_LEN];
	uint32_t hash, hashsize;
	int cnt, nexttx;

	DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
	    device_xname(sc->sc_dev), sc->sc_flags));

	memcpy(enaddr, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);

	/*
	 * If there are transmissions pending, wait until they have
	 * completed.
	 */
	if (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq) ||
	    (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
		sc->sc_flags |= TULIPF_WANT_SETUP;
		DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
		    device_xname(sc->sc_dev)));
		return;
	}
	sc->sc_flags &= ~TULIPF_WANT_SETUP;

	switch (sc->sc_chip) {
	case TULIP_CHIP_82C115:
		hashsize = TULIP_PNICII_HASHSIZE;
		break;

	default:
		hashsize = TULIP_MCHASHSIZE;
	}

	/*
	 * If we're running, idle the transmit and receive engines.  If
	 * we're NOT running, we're being called from tlp_init(), and our
	 * writing OPMODE will start the transmit and receive processes
	 * in motion.
	 */
	if (ifp->if_flags & IFF_RUNNING)
		tlp_idle(sc, OPMODE_ST | OPMODE_SR);

	sc->sc_opmode &= ~(OPMODE_PR | OPMODE_PM);

	if (ifp->if_flags & IFF_PROMISC) {
		sc->sc_opmode |= OPMODE_PR;
		goto allmulti;
	}

	/*
	 * Try Perfect filtering first.
	 */

	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
	sp = TULIP_CDSP(sc);
	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
	cnt = 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;
		}
		if (cnt == (TULIP_MAXADDRS - 2)) {
			/*
			 * We already have our multicast limit (still need
			 * our station address and broadcast).  Go to
			 * Hash-Perfect mode.
			 */
			ETHER_UNLOCK(ec);
			goto hashperfect;
		}
		cnt++;
		*sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 0));
		*sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 1));
		*sp++ = htole32(TULIP_SP_FIELD(enm->enm_addrlo, 2));
		ETHER_NEXT_MULTI(step, enm);
	}
	ETHER_UNLOCK(ec);

	if (ifp->if_flags & IFF_BROADCAST) {
		/* ...and the broadcast address. */
		cnt++;
		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
	}

	/* Pad the rest with our station address. */
	for (; cnt < TULIP_MAXADDRS; cnt++) {
		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 0));
		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 1));
		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 2));
	}
	ifp->if_flags &= ~IFF_ALLMULTI;
	goto setit;

 hashperfect:
	/*
	 * Try Hash-Perfect mode.
	 */

	/*
	 * Some 21140 chips have broken Hash-Perfect modes.  On these
	 * chips, we simply use Hash-Only mode, and put our station
	 * address into the filter.
	 */
	if (sc->sc_chip == TULIP_CHIP_21140)
		sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
	else
		sc->sc_filtmode = TDCTL_Tx_FT_HASH;
	sp = TULIP_CDSP(sc);
	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
	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 = tlp_mchash(enm->enm_addrlo, hashsize);
		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
		ETHER_NEXT_MULTI(step, enm);
	}
	ETHER_UNLOCK(ec);

	if (ifp->if_flags & IFF_BROADCAST) {
		/* ...and the broadcast address. */
		hash = tlp_mchash(etherbroadcastaddr, hashsize);
		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
	}

	if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
		/* ...and our station address. */
		hash = tlp_mchash(enaddr, hashsize);
		sp[hash >> 4] |= htole32(1 << (hash & 0xf));
	} else {
		/*
		 * Hash-Perfect mode; put our station address after
		 * the hash table.
		 */
		sp[39] = htole32(TULIP_SP_FIELD(enaddr, 0));
		sp[40] = htole32(TULIP_SP_FIELD(enaddr, 1));
		sp[41] = htole32(TULIP_SP_FIELD(enaddr, 2));
	}
	ifp->if_flags &= ~IFF_ALLMULTI;
	goto setit;

 allmulti:
	/*
	 * Use Perfect filter mode.  First address is the broadcast address,
	 * and pad the rest with our station address.  We'll set Pass-all-
	 * multicast in OPMODE below.
	 */
	sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
	sp = TULIP_CDSP(sc);
	memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
	cnt = 0;
	if (ifp->if_flags & IFF_BROADCAST) {
		cnt++;
		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
		*sp++ = htole32(TULIP_SP_FIELD_C(0xff, 0xff));
	}
	for (; cnt < TULIP_MAXADDRS; cnt++) {
		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 0));
		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 1));
		*sp++ = htole32(TULIP_SP_FIELD(enaddr, 2));
	}
	ifp->if_flags |= IFF_ALLMULTI;

 setit:
	if (ifp->if_flags & IFF_ALLMULTI)
		sc->sc_opmode |= OPMODE_PM;

	/* Sync the setup packet buffer. */
	TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);

	/*
	 * Fill in the setup packet descriptor.
	 */
	txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);

	txs->txs_firstdesc = sc->sc_txnext;
	txs->txs_lastdesc = sc->sc_txnext;
	txs->txs_ndescs = 1;
	txs->txs_mbuf = NULL;

	nexttx = sc->sc_txnext;
	txd = &sc->sc_txdescs[nexttx];
	txd->td_status = 0;
	txd->td_bufaddr1 = htole32(TULIP_CDSPADDR(sc));
	txd->td_ctl = htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
	    sc->sc_filtmode | TDCTL_Tx_SET | sc->sc_setup_fsls |
	    TDCTL_Tx_IC | sc->sc_tdctl_ch |
	    (nexttx == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
	TULIP_CDTXSYNC(sc, nexttx, 1,
	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);

#ifdef TLP_DEBUG
	if (ifp->if_flags & IFF_DEBUG) {
		printf("     filter_setup %p transmit chain:\n", txs);
		printf("     descriptor %d:\n", nexttx);
		printf("       td_status:   0x%08x\n", le32toh(txd->td_status));
		printf("       td_ctl:      0x%08x\n", le32toh(txd->td_ctl));
		printf("       td_bufaddr1: 0x%08x\n",
		    le32toh(txd->td_bufaddr1));
		printf("       td_bufaddr2: 0x%08x\n",
		    le32toh(txd->td_bufaddr2));
	}
#endif

	txd->td_status = htole32(TDSTAT_OWN);
	TULIP_CDTXSYNC(sc, nexttx, 1,
	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);

	/* Advance the tx pointer. */
	sc->sc_txfree -= 1;
	sc->sc_txnext = TULIP_NEXTTX(nexttx);

	SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
	SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);

	/*
	 * Set the OPMODE register.  This will also resume the
	 * transmit process we idled above.
	 */
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);

	sc->sc_flags |= TULIPF_DOING_SETUP;

	/*
	 * Kick the transmitter; this will cause the Tulip to
	 * read the setup descriptor.
	 */
	/* XXX USE AUTOPOLLING? */
	TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);

	/* Set up a watchdog timer in case the chip flakes out. */
	ifp->if_timer = 5;

	DPRINTF(sc, ("%s: tlp_filter_setup: returning\n",
		device_xname(sc->sc_dev)));
}

/*
 * tlp_winb_filter_setup:
 *
 *	Set the Winbond 89C840F's receive filter.
 */
static void
tlp_winb_filter_setup(struct tulip_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, mchash[2];

	DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
	    device_xname(sc->sc_dev), sc->sc_flags));

	sc->sc_opmode &= ~(OPMODE_WINB_APP | OPMODE_WINB_AMP |OPMODE_WINB_ABP);

	if (ifp->if_flags & IFF_MULTICAST)
		sc->sc_opmode |= OPMODE_WINB_AMP;

	if (ifp->if_flags & IFF_BROADCAST)
		sc->sc_opmode |= OPMODE_WINB_ABP;

	if (ifp->if_flags & IFF_PROMISC) {
		sc->sc_opmode |= OPMODE_WINB_APP;
		goto allmulti;
	}

	mchash[0] = mchash[1] = 0;

	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.)
			 */
			goto allmulti;
		}

		/*
		 * According to the FreeBSD `wb' driver, yes, you
		 * really do invert the hash.
		 */
		hash =
		    (~(ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
		    & 0x3f;
		mchash[hash >> 5] |= 1 << (hash & 0x1f);
		ETHER_NEXT_MULTI(step, enm);
	}
	ifp->if_flags &= ~IFF_ALLMULTI;
	goto setit;

 allmulti:
	ifp->if_flags |= IFF_ALLMULTI;
	mchash[0] = mchash[1] = 0xffffffff;

 setit:
	TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
	TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
	DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
	    device_xname(sc->sc_dev)));
}

/*
 * tlp_al981_filter_setup:
 *
 *	Set the ADMtek AL981's receive filter.
 */
static void
tlp_al981_filter_setup(struct tulip_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, mchash[2];

	/*
	 * If the chip is running, we need to reset the interface,
	 * and will revisit here (with IFF_RUNNING) clear.  The
	 * chip seems to really not like to have its multicast
	 * filter programmed without a reset.
	 */
	if (ifp->if_flags & IFF_RUNNING) {
		(void) tlp_init(ifp);
		return;
	}

	DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
	    device_xname(sc->sc_dev), sc->sc_flags));

	sc->sc_opmode &= ~(OPMODE_PR | OPMODE_PM);

	if (ifp->if_flags & IFF_PROMISC) {
		sc->sc_opmode |= OPMODE_PR;
		goto allmulti;
	}

	mchash[0] = mchash[1] = 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 = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f;
		mchash[hash >> 5] |= __BIT(hash & 0x1f);
		ETHER_NEXT_MULTI(step, enm);
	}
	ETHER_UNLOCK(ec);
	ifp->if_flags &= ~IFF_ALLMULTI;
	goto setit;

 allmulti:
	ifp->if_flags |= IFF_ALLMULTI;
	mchash[0] = mchash[1] = 0xffffffff;

 setit:
	bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR0, mchash[0]);
	bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR1, mchash[1]);
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
	DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
	    device_xname(sc->sc_dev)));
}

/*
 * tlp_asix_filter_setup:
 *
 * 	Set the ASIX AX8814x receive filter.
 */
static void
tlp_asix_filter_setup(struct tulip_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, mchash[2];

	DPRINTF(sc, ("%s: tlp_asix_filter_setup: sc_flags 0x%08x\n",
		device_xname(sc->sc_dev), sc->sc_flags));

	sc->sc_opmode &= ~(OPMODE_PM | OPMODE_AX_RB | OPMODE_PR);

	if (ifp->if_flags & IFF_MULTICAST)
		sc->sc_opmode |= OPMODE_PM;

	if (ifp->if_flags & IFF_BROADCAST)
		sc->sc_opmode |= OPMODE_AX_RB;

	if (ifp->if_flags & IFF_PROMISC) {
		sc->sc_opmode |= OPMODE_PR;
		goto allmulti;
	}

	mchash[0] = mchash[1] = 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 = (ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26)
		       & 0x3f;
		if (hash < 32)
			mchash[0] |= (1 << hash);
		else
			mchash[1] |= (1 << (hash - 32));
		ETHER_NEXT_MULTI(step, enm);
	}
	ETHER_UNLOCK(ec);
	ifp->if_flags &= ~IFF_ALLMULTI;
	goto setit;

allmulti:
	ifp->if_flags |= IFF_ALLMULTI;
	mchash[0] = mchash[1] = 0xffffffff;

setit:
	TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR0);
	TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[0]);
	TULIP_WRITE(sc, CSR_AX_FILTIDX, AX_FILTIDX_MAR1);
	TULIP_WRITE(sc, CSR_AX_FILTDATA, mchash[1]);
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
	DPRINTF(sc, ("%s: tlp_asix_filter_setup: returning\n",
		device_xname(sc->sc_dev)));
}


/*
 * tlp_idle:
 *
 *	Cause the transmit and/or receive processes to go idle.
 */
void
tlp_idle(struct tulip_softc *sc, uint32_t bits)
{
	static const char * const tlp_tx_state_names[] = {
		"STOPPED",
		"RUNNING - FETCH",
		"RUNNING - WAIT",
		"RUNNING - READING",
		"-- RESERVED --",
		"RUNNING - SETUP",
		"SUSPENDED",
		"RUNNING - CLOSE",
	};
	static const char * const tlp_rx_state_names[] = {
		"STOPPED",
		"RUNNING - FETCH",
		"RUNNING - CHECK",
		"RUNNING - WAIT",
		"SUSPENDED",
		"RUNNING - CLOSE",
		"RUNNING - FLUSH",
		"RUNNING - QUEUE",
	};
	static const char * const dm9102_tx_state_names[] = {
		"STOPPED",
		"RUNNING - FETCH",
		"RUNNING - SETUP",
		"RUNNING - READING",
		"RUNNING - CLOSE - CLEAR OWNER",
		"RUNNING - WAIT",
		"RUNNING - CLOSE - WRITE STATUS",
		"SUSPENDED",
	};
	static const char * const dm9102_rx_state_names[] = {
		"STOPPED",
		"RUNNING - FETCH",
		"RUNNING - WAIT",
		"RUNNING - QUEUE",
		"RUNNING - CLOSE - CLEAR OWNER",
		"RUNNING - CLOSE - WRITE STATUS",
		"SUSPENDED",
		"RUNNING - FLUSH",
	};

	const char * const *tx_state_names, * const *rx_state_names;
	uint32_t csr, ackmask = 0;
	int i;

	switch (sc->sc_chip) {
	case TULIP_CHIP_DM9102:
	case TULIP_CHIP_DM9102A:
		tx_state_names = dm9102_tx_state_names;
		rx_state_names = dm9102_rx_state_names;
		break;

	default:
		tx_state_names = tlp_tx_state_names;
		rx_state_names = tlp_rx_state_names;
		break;
	}

	if (bits & OPMODE_ST)
		ackmask |= STATUS_TPS;

	if (bits & OPMODE_SR)
		ackmask |= STATUS_RPS;

	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);

	for (i = 0; i < 1000; i++) {
		if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
			break;
		delay(10);
	}

	csr = TULIP_READ(sc, CSR_STATUS);
	if ((csr & ackmask) != ackmask) {
		if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
		    (csr & STATUS_TS) != STATUS_TS_STOPPED) {
			switch (sc->sc_chip) {
			case TULIP_CHIP_AX88140:
			case TULIP_CHIP_AX88141:
				/*
				 * Filter the message out on noisy chips.
				 */
				break;
			default:
				printf("%s: transmit process failed to idle: "
				    "state %s\n", device_xname(sc->sc_dev),
				    tx_state_names[(csr & STATUS_TS) >> 20]);
			}
		}
		if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
		    (csr & STATUS_RS) != STATUS_RS_STOPPED) {
			switch (sc->sc_chip) {
			case TULIP_CHIP_AN983:
			case TULIP_CHIP_AN985:
			case TULIP_CHIP_DM9102A:
			case TULIP_CHIP_RS7112:
				/*
				 * Filter the message out on noisy chips.
				 */
				break;
			default:
				printf("%s: receive process failed to idle: "
				    "state %s\n", device_xname(sc->sc_dev),
				    rx_state_names[(csr & STATUS_RS) >> 17]);
			}
		}
	}
	TULIP_WRITE(sc, CSR_STATUS, ackmask);
}

/*****************************************************************************
 * Generic media support functions.
 *****************************************************************************/

/*
 * tlp_mediastatus:	[ifmedia interface function]
 *
 *	Query the current media.
 */
void
tlp_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
{
	struct tulip_softc *sc = ifp->if_softc;

	if (TULIP_IS_ENABLED(sc) == 0) {
		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
		ifmr->ifm_status = 0;
		return;
	}

	(*sc->sc_mediasw->tmsw_get)(sc, ifmr);
}

/*
 * tlp_mediachange:	[ifmedia interface function]
 *
 *	Update the current media.
 */
int
tlp_mediachange(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;

	if ((ifp->if_flags & IFF_UP) == 0)
		return 0;
	return (*sc->sc_mediasw->tmsw_set)(sc);
}

/*****************************************************************************
 * Support functions for MII-attached media.
 *****************************************************************************/

/*
 * tlp_mii_tick:
 *
 *	One second timer, used to tick the MII.
 */
static void
tlp_mii_tick(void *arg)
{
	struct tulip_softc *sc = arg;
	int s;

	if (!device_is_active(sc->sc_dev))
		return;

	s = splnet();
	mii_tick(&sc->sc_mii);
	splx(s);

	callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
}

/*
 * tlp_mii_statchg:	[mii interface function]
 *
 *	Callback from PHY when media changes.
 */
static void
tlp_mii_statchg(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;

	/* Idle the transmit and receive processes. */
	tlp_idle(sc, OPMODE_ST | OPMODE_SR);

	sc->sc_opmode &= ~(OPMODE_TTM | OPMODE_FD | OPMODE_HBD);

	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
		sc->sc_opmode |= OPMODE_TTM;
	else
		sc->sc_opmode |= OPMODE_HBD;

	if (sc->sc_mii.mii_media_active & IFM_FDX)
		sc->sc_opmode |= OPMODE_FD | OPMODE_HBD;

	/*
	 * Write new OPMODE bits.  This also restarts the transmit
	 * and receive processes.
	 */
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

/*
 * tlp_winb_mii_statchg: [mii interface function]
 *
 *	Callback from PHY when media changes.  This version is
 *	for the Winbond 89C840F, which has different OPMODE bits.
 */
static void
tlp_winb_mii_statchg(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;

	/* Idle the transmit and receive processes. */
	tlp_idle(sc, OPMODE_ST | OPMODE_SR);

	sc->sc_opmode &= ~(OPMODE_WINB_FES | OPMODE_FD);

	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
		sc->sc_opmode |= OPMODE_WINB_FES;

	if (sc->sc_mii.mii_media_active & IFM_FDX)
		sc->sc_opmode |= OPMODE_FD;

	/*
	 * Write new OPMODE bits.  This also restarts the transmit
	 * and receive processes.
	 */
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

/*
 * tlp_dm9102_mii_statchg: [mii interface function]
 *
 *	Callback from PHY when media changes.  This version is
 *	for the DM9102.
 */
static void
tlp_dm9102_mii_statchg(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;

	/*
	 * Don't idle the transmit and receive processes, here.  It
	 * seems to fail, and just causes excess noise.
	 */
	sc->sc_opmode &= ~(OPMODE_TTM | OPMODE_FD);

	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_100_TX)
		sc->sc_opmode |= OPMODE_TTM;

	if (sc->sc_mii.mii_media_active & IFM_FDX)
		sc->sc_opmode |= OPMODE_FD;

	/*
	 * Write new OPMODE bits.
	 */
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

/*
 * tlp_mii_getmedia:
 *
 *	Callback from ifmedia to request current media status.
 */
static void
tlp_mii_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
{
	struct mii_data * const mii = &sc->sc_mii;

	mii_pollstat(mii);
	ifmr->ifm_status = mii->mii_media_status;
	ifmr->ifm_active = mii->mii_media_active;
}

/*
 * tlp_mii_setmedia:
 *
 *	Callback from ifmedia to request new media setting.
 */
static int
tlp_mii_setmedia(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	int rc;

	if ((ifp->if_flags & IFF_UP) == 0)
		return 0;
	switch (sc->sc_chip) {
	case TULIP_CHIP_21142:
	case TULIP_CHIP_21143:
		/* Disable the internal Nway engine. */
		TULIP_WRITE(sc, CSR_SIATXRX, 0);
		break;

	default:
		/* Nothing. */
		break;
	}
	if ((rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
		return 0;
	return rc;
}

/*
 * tlp_bitbang_mii_readreg:
 *
 *	Read a PHY register via bit-bang'ing the MII.
 */
static int
tlp_bitbang_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
	struct tulip_softc *sc = device_private(self);

	return mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg, val);
}

/*
 * tlp_bitbang_mii_writereg:
 *
 *	Write a PHY register via bit-bang'ing the MII.
 */
static int
tlp_bitbang_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
	struct tulip_softc *sc = device_private(self);

	return mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
}

/*
 * tlp_sio_mii_bitbang_read:
 *
 *	Read the MII serial port for the MII bit-bang module.
 */
static uint32_t
tlp_sio_mii_bitbang_read(device_t self)
{
	struct tulip_softc *sc = device_private(self);

	return TULIP_READ(sc, CSR_MIIROM);
}

/*
 * tlp_sio_mii_bitbang_write:
 *
 *	Write the MII serial port for the MII bit-bang module.
 */
static void
tlp_sio_mii_bitbang_write(device_t self, uint32_t val)
{
	struct tulip_softc *sc = device_private(self);

	TULIP_WRITE(sc, CSR_MIIROM, val);
}

/*
 * tlp_pnic_mii_readreg:
 *
 *	Read a PHY register on the Lite-On PNIC.
 */
static int
tlp_pnic_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
	struct tulip_softc *sc = device_private(self);
	uint32_t data;
	int i;

	TULIP_WRITE(sc, CSR_PNIC_MII,
	    PNIC_MII_MBO | PNIC_MII_RESERVED |
	    PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
	    (reg << PNIC_MII_REGSHIFT));

	for (i = 0; i < 1000; i++) {
		delay(10);
		data = TULIP_READ(sc, CSR_PNIC_MII);
		if ((data & PNIC_MII_BUSY) == 0) {
			if ((data & PNIC_MII_DATA) == PNIC_MII_DATA)
				return -1;
			else {
				*val = data & PNIC_MII_DATA;
				return 0;
			}
		}
	}
	printf("%s: MII read timed out\n", device_xname(sc->sc_dev));
	return ETIMEDOUT;
}

/*
 * tlp_pnic_mii_writereg:
 *
 *	Write a PHY register on the Lite-On PNIC.
 */
static int
tlp_pnic_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
	struct tulip_softc *sc = device_private(self);
	int i;

	TULIP_WRITE(sc, CSR_PNIC_MII,
	    PNIC_MII_MBO | PNIC_MII_RESERVED |
	    PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
	    (reg << PNIC_MII_REGSHIFT) | val);

	for (i = 0; i < 1000; i++) {
		delay(10);
		if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
			return 0;
	}
	printf("%s: MII write timed out\n", device_xname(sc->sc_dev));
	return ETIMEDOUT;
}

static const bus_addr_t tlp_al981_phy_regmap[] = {
	CSR_ADM_BMCR,
	CSR_ADM_BMSR,
	CSR_ADM_PHYIDR1,
	CSR_ADM_PHYIDR2,
	CSR_ADM_ANAR,
	CSR_ADM_ANLPAR,
	CSR_ADM_ANER,

	CSR_ADM_XMC,
	CSR_ADM_XCIIS,
	CSR_ADM_XIE,
	CSR_ADM_100CTR,
};
static const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
    sizeof(tlp_al981_phy_regmap[0]);

/*
 * tlp_al981_mii_readreg:
 *
 *	Read a PHY register on the ADMtek AL981.
 */
static int
tlp_al981_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
	struct tulip_softc *sc = device_private(self);

	/* AL981 only has an internal PHY. */
	if (phy != 0)
		return -1;

	if (reg >= tlp_al981_phy_regmap_size)
		return -1;

	*val = bus_space_read_4(sc->sc_st, sc->sc_sh,
	    tlp_al981_phy_regmap[reg]) & 0xffff;
	return 0;
}

/*
 * tlp_al981_mii_writereg:
 *
 *	Write a PHY register on the ADMtek AL981.
 */
static int
tlp_al981_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
	struct tulip_softc *sc = device_private(self);

	/* AL981 only has an internal PHY. */
	if (phy != 0)
		return -1;

	if (reg >= tlp_al981_phy_regmap_size)
		return -1;

	bus_space_write_4(sc->sc_st, sc->sc_sh,
	    tlp_al981_phy_regmap[reg], val);

	return 0;
}

/*****************************************************************************
 * Chip-specific pre-init and reset functions.
 *****************************************************************************/

/*
 * tlp_2114x_preinit:
 *
 *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
 */
static void
tlp_2114x_preinit(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
	struct tulip_21x4x_media *tm = ife->ifm_aux;

	/*
	 * Whether or not we're in MII or SIA/SYM mode, the media info
	 * contains the appropriate OPMODE bits.
	 *
	 * Also, we always set the Must-Be-One bit.
	 */
	sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;

	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

/*
 * tlp_2114x_mii_preinit:
 *
 *	Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
 *	This version is used by boards which only have MII and don't have
 *	an ISV SROM.
 */
static void
tlp_2114x_mii_preinit(struct tulip_softc *sc)
{

	/*
	 * Always set the Must-Be-One bit, and Port Select (to select MII).
	 * We'll never be called during a media change.
	 */
	sc->sc_opmode |= OPMODE_MBO | OPMODE_PS;
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

/*
 * tlp_pnic_preinit:
 *
 *	Pre-init function for the Lite-On 82c168 and 82c169.
 */
static void
tlp_pnic_preinit(struct tulip_softc *sc)
{

	if (sc->sc_flags & TULIPF_HAS_MII) {
		/*
		 * MII case: just set the port-select bit; we will never
		 * be called during a media change.
		 */
		sc->sc_opmode |= OPMODE_PS;
	} else {
		/*
		 * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
		 */
		sc->sc_opmode |= OPMODE_PNIC_TBEN;
	}
}

/*
 * tlp_asix_preinit:
 *
 * 	Pre-init function for the ASIX chipsets.
 */
static void
tlp_asix_preinit(struct tulip_softc *sc)
{

	switch (sc->sc_chip) {
		case TULIP_CHIP_AX88140:
		case TULIP_CHIP_AX88141:
			/* XXX Handle PHY. */
			sc->sc_opmode |= OPMODE_HBD | OPMODE_PS;
			break;
		default:
			/* Nothing */
			break;
	}

	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

/*
 * tlp_dm9102_preinit:
 *
 *	Pre-init function for the Davicom DM9102.
 */
static void
tlp_dm9102_preinit(struct tulip_softc *sc)
{

	switch (sc->sc_chip) {
	case TULIP_CHIP_DM9102:
		sc->sc_opmode |= OPMODE_MBO | OPMODE_HBD | OPMODE_PS;
		break;

	case TULIP_CHIP_DM9102A:
		/*
		 * XXX Figure out how to actually deal with the HomePNA
		 * XXX portion of the DM9102A.
		 */
		sc->sc_opmode |= OPMODE_MBO | OPMODE_HBD;
		break;

	default:
		/* Nothing. */
		break;
	}

	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

/*
 * tlp_21140_reset:
 *
 *	Issue a reset sequence on the 21140 via the GPIO facility.
 */
static void
tlp_21140_reset(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
	struct tulip_21x4x_media *tm = ife->ifm_aux;
	int i;

	/* First, set the direction on the GPIO pins. */
	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);

	/* Now, issue the reset sequence. */
	for (i = 0; i < tm->tm_reset_length; i++) {
		delay(10);
		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
	}

	/* Now, issue the selection sequence. */
	for (i = 0; i < tm->tm_gp_length; i++) {
		delay(10);
		TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
	}

	/* If there were no sequences, just lower the pins. */
	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
		delay(10);
		TULIP_WRITE(sc, CSR_GPP, 0);
	}
}

/*
 * tlp_21142_reset:
 *
 *	Issue a reset sequence on the 21142 via the GPIO facility.
 */
static void
tlp_21142_reset(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
	struct tulip_21x4x_media *tm = ife->ifm_aux;
	const uint8_t *cp;
	int i;

	cp = &sc->sc_srom[tm->tm_reset_offset];
	for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
		delay(10);
		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
	}

	cp = &sc->sc_srom[tm->tm_gp_offset];
	for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
		delay(10);
		TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
	}

	/* If there were no sequences, just lower the pins. */
	if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
		delay(10);
		TULIP_WRITE(sc, CSR_SIAGEN, 0);
	}
}

/*
 * tlp_pmac_reset:
 *
 *	Reset routine for Macronix chips.
 */
static void
tlp_pmac_reset(struct tulip_softc *sc)
{

	switch (sc->sc_chip) {
	case TULIP_CHIP_82C115:
	case TULIP_CHIP_MX98715:
	case TULIP_CHIP_MX98715A:
	case TULIP_CHIP_MX98725:
		/*
		 * Set the LED operating mode.  This information is located
		 * in the EEPROM at byte offset 0x77, per the MX98715A and
		 * MX98725 application notes.
		 */
		TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
		break;
	case TULIP_CHIP_MX98715AEC_X:
		/*
		 * Set the LED operating mode.  This information is located
		 * in the EEPROM at byte offset 0x76, per the MX98715AEC
		 * application note.
		 */
		TULIP_WRITE(sc, CSR_MIIROM, ((0xf & sc->sc_srom[0x76]) << 28)
		    | ((0xf0 & sc->sc_srom[0x76]) << 20));
		break;

	default:
		/* Nothing. */
		break;
	}
}

#if 0
/*
 * tlp_dm9102_reset:
 *
 *	Reset routine for the Davicom DM9102.
 */
static void
tlp_dm9102_reset(struct tulip_softc *sc)
{

	TULIP_WRITE(sc, CSR_DM_PHYSTAT, DM_PHYSTAT_GEPC | DM_PHYSTAT_GPED);
	delay(100);
	TULIP_WRITE(sc, CSR_DM_PHYSTAT, 0);
}
#endif

/*****************************************************************************
 * Chip/board-specific media switches.  The ones here are ones that
 * are potentially common to multiple front-ends.
 *****************************************************************************/

/*
 * This table is a common place for all sorts of media information,
 * keyed off of the SROM media code for that media.
 *
 * Note that we explicitly configure the 21142/21143 to always advertise
 * NWay capabilities when using the UTP port.
 * XXX Actually, we don't yet.
 */
static const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
	{ TULIP_ROM_MB_MEDIA_TP,	IFM_10_T,	0,
	  "10baseT",
	  OPMODE_TTM,
	  BMSR_10THDX,
	  { SIACONN_21040_10BASET,
	    SIATXRX_21040_10BASET,
	    SIAGEN_21040_10BASET },

	  { SIACONN_21041_10BASET,
	    SIATXRX_21041_10BASET,
	    SIAGEN_21041_10BASET },

	  { SIACONN_21142_10BASET,
	    SIATXRX_21142_10BASET,
	    SIAGEN_21142_10BASET } },

	{ TULIP_ROM_MB_MEDIA_BNC,	IFM_10_2,	0,
	  "10base2",
	  0,
	  0,
	  { 0,
	    0,
	    0 },

	  { SIACONN_21041_BNC,
	    SIATXRX_21041_BNC,
	    SIAGEN_21041_BNC },

	  { SIACONN_21142_BNC,
	    SIATXRX_21142_BNC,
	    SIAGEN_21142_BNC } },

	{ TULIP_ROM_MB_MEDIA_AUI,	IFM_10_5,	0,
	  "10base5",
	  0,
	  0,
	  { SIACONN_21040_AUI,
	    SIATXRX_21040_AUI,
	    SIAGEN_21040_AUI },

	  { SIACONN_21041_AUI,
	    SIATXRX_21041_AUI,
	    SIAGEN_21041_AUI },

	  { SIACONN_21142_AUI,
	    SIATXRX_21142_AUI,
	    SIAGEN_21142_AUI } },

	{ TULIP_ROM_MB_MEDIA_100TX,	IFM_100_TX,	0,
	  "100baseTX",
	  OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_HBD,
	  BMSR_100TXHDX,
	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    SIAGEN_ABM } },

	{ TULIP_ROM_MB_MEDIA_TP_FDX,	IFM_10_T,	IFM_FDX,
	  "10baseT-FDX",
	  OPMODE_TTM | OPMODE_FD | OPMODE_HBD,
	  BMSR_10TFDX,
	  { SIACONN_21040_10BASET_FDX,
	    SIATXRX_21040_10BASET_FDX,
	    SIAGEN_21040_10BASET_FDX },

	  { SIACONN_21041_10BASET_FDX,
	    SIATXRX_21041_10BASET_FDX,
	    SIAGEN_21041_10BASET_FDX },

	  { SIACONN_21142_10BASET_FDX,
	    SIATXRX_21142_10BASET_FDX,
	    SIAGEN_21142_10BASET_FDX } },

	{ TULIP_ROM_MB_MEDIA_100TX_FDX,	IFM_100_TX,	IFM_FDX,
	  "100baseTX-FDX",
	  OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_FD | OPMODE_HBD,
	  BMSR_100TXFDX,
	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    SIAGEN_ABM } },

	{ TULIP_ROM_MB_MEDIA_100T4,	IFM_100_T4,	0,
	  "100baseT4",
	  OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_HBD,
	  BMSR_100T4,
	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    SIAGEN_ABM } },

	{ TULIP_ROM_MB_MEDIA_100FX,	IFM_100_FX,	0,
	  "100baseFX",
	  OPMODE_PS | OPMODE_PCS | OPMODE_HBD,
	  0,
	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    SIAGEN_ABM } },

	{ TULIP_ROM_MB_MEDIA_100FX_FDX,	IFM_100_FX,	IFM_FDX,
	  "100baseFX-FDX",
	  OPMODE_PS | OPMODE_PCS | OPMODE_FD | OPMODE_HBD,
	  0,
	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    SIAGEN_ABM } },

	{ 0,				0,		0,
	  NULL,
	  0,
	  0,
	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    0 },

	  { 0,
	    0,
	    0 } },
};

static const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia(uint8_t);
static void	tlp_srom_media_info(struct tulip_softc *,
		    const struct tulip_srom_to_ifmedia *,
		    struct tulip_21x4x_media *);
static void	tlp_add_srom_media(struct tulip_softc *, int,
		    void (*)(struct tulip_softc *, struct ifmediareq *),
		    int (*)(struct tulip_softc *), const uint8_t *, int);
static void	tlp_print_media(struct tulip_softc *);
static void	tlp_nway_activate(struct tulip_softc *, int);
static void	tlp_get_minst(struct tulip_softc *);

static const struct tulip_srom_to_ifmedia *
tlp_srom_to_ifmedia(uint8_t sm)
{
	const struct tulip_srom_to_ifmedia *tsti;

	for (tsti = tulip_srom_to_ifmedia_table;
	     tsti->tsti_name != NULL; tsti++) {
		if (tsti->tsti_srom == sm)
			return tsti;
	}

	return NULL;
}

static void
tlp_srom_media_info(struct tulip_softc *sc,
    const struct tulip_srom_to_ifmedia *tsti, struct tulip_21x4x_media *tm)
{

	tm->tm_name = tsti->tsti_name;
	tm->tm_opmode = tsti->tsti_opmode;

	sc->sc_sia_cap |= tsti->tsti_sia_cap;

	switch (sc->sc_chip) {
	case TULIP_CHIP_DE425:
	case TULIP_CHIP_21040:
		tm->tm_sia = tsti->tsti_21040;	/* struct assignment */
		break;

	case TULIP_CHIP_21041:
		tm->tm_sia = tsti->tsti_21041;	/* struct assignment */
		break;

	case TULIP_CHIP_21142:
	case TULIP_CHIP_21143:
	case TULIP_CHIP_82C115:
	case TULIP_CHIP_MX98715:
	case TULIP_CHIP_MX98715A:
	case TULIP_CHIP_MX98715AEC_X:
	case TULIP_CHIP_MX98725:
		tm->tm_sia = tsti->tsti_21142;	/* struct assignment */
		break;

	default:
		/* Nothing. */
		break;
	}
}

static void
tlp_add_srom_media(struct tulip_softc *sc, int type,
    void (*get)(struct tulip_softc *, struct ifmediareq *),
    int (*set)(struct tulip_softc *), const uint8_t *list,
    int cnt)
{
	struct tulip_21x4x_media *tm;
	const struct tulip_srom_to_ifmedia *tsti;
	int i;

	for (i = 0; i < cnt; i++) {
		tsti = tlp_srom_to_ifmedia(list[i]);
		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
		tlp_srom_media_info(sc, tsti, tm);
		tm->tm_type = type;
		tm->tm_get = get;
		tm->tm_set = set;

		ifmedia_add(&sc->sc_mii.mii_media,
		    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
		    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
	}
}

static void
tlp_print_media(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife;
	struct tulip_21x4x_media *tm;
	const char *sep = "";

#define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "

	aprint_normal_dev(sc->sc_dev, "");
	TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, ifm_list) {
		tm = ife->ifm_aux;
		if (tm == NULL) {
#ifdef DIAGNOSTIC
			if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
				panic("tlp_print_media");
#endif
			PRINT("auto");
		} else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
			   tm->tm_type != TULIP_ROM_MB_21142_MII) {
			PRINT(tm->tm_name);
		}
	}
	aprint_normal("\n");

#undef PRINT
}

static void
tlp_nway_activate(struct tulip_softc *sc, int media)
{
	struct ifmedia_entry *ife;

	ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
#ifdef DIAGNOSTIC
	if (ife == NULL)
		panic("tlp_nway_activate");
#endif
	sc->sc_nway_active = ife;
}

static void
tlp_get_minst(struct tulip_softc *sc)
{

	if ((sc->sc_media_seen &
	    ~((1 << TULIP_ROM_MB_21140_MII) |
	      (1 << TULIP_ROM_MB_21142_MII))) == 0) {
		/*
		 * We have not yet seen any SIA/SYM media (but are
		 * about to; that's why we're called!), so assign
		 * the current media instance to be the `internal media'
		 * instance, and advance it so any MII media gets a
		 * fresh one (used to selecting/isolating a PHY).
		 */
		sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
	}
}

/*
 * SIA Utility functions.
 */
static void	tlp_sia_update_link(struct tulip_softc *);
static void	tlp_sia_get(struct tulip_softc *, struct ifmediareq *);
static int	tlp_sia_set(struct tulip_softc *);
static int	tlp_sia_media(struct tulip_softc *, struct ifmedia_entry *);
static void	tlp_sia_fixup(struct tulip_softc *);

static void
tlp_sia_update_link(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife;
	struct tulip_21x4x_media *tm;
	uint32_t siastat;

	ife = TULIP_CURRENT_MEDIA(sc);
	tm = ife->ifm_aux;

	sc->sc_flags &= ~(TULIPF_LINK_UP | TULIPF_LINK_VALID);

	siastat = TULIP_READ(sc, CSR_SIASTAT);

	/*
	 * Note that when we do SIA link tests, we are assuming that
	 * the chip is really in the mode that the current media setting
	 * reflects.  If we're not, then the link tests will not be
	 * accurate!
	 */
	switch (IFM_SUBTYPE(ife->ifm_media)) {
	case IFM_10_T:
		sc->sc_flags |= TULIPF_LINK_VALID;
		if ((siastat & SIASTAT_LS10) == 0)
			sc->sc_flags |= TULIPF_LINK_UP;
		break;

	case IFM_100_TX:
	case IFM_100_T4:
		sc->sc_flags |= TULIPF_LINK_VALID;
		if ((siastat & SIASTAT_LS100) == 0)
			sc->sc_flags |= TULIPF_LINK_UP;
		break;
	}

	switch (sc->sc_chip) {
	case TULIP_CHIP_21142:
	case TULIP_CHIP_21143:
		/*
		 * On these chips, we can tell more information about
		 * AUI/BNC.  Note that the AUI/BNC selection is made
		 * in a different register; for our purpose, it's all
		 * AUI.
		 */
		switch (IFM_SUBTYPE(ife->ifm_media)) {
		case IFM_10_2:
		case IFM_10_5:
			sc->sc_flags |= TULIPF_LINK_VALID;
			if (siastat & SIASTAT_ARA) {
				TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
				sc->sc_flags |= TULIPF_LINK_UP;
			}
			break;

		default:
			/*
			 * If we're SYM media and can detect the link
			 * via the GPIO facility, prefer that status
			 * over LS100.
			 */
			if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
			    tm->tm_actmask != 0) {
				sc->sc_flags = (sc->sc_flags &
				    ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
				if (TULIP_ISSET(sc, CSR_SIAGEN,
				    tm->tm_actmask) == tm->tm_actdata)
					sc->sc_flags |= TULIPF_LINK_UP;
			}
		}
		break;

	default:
		/* Nothing. */
		break;
	}
}

static void
tlp_sia_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
{
	struct ifmedia_entry *ife;

	ifmr->ifm_status = 0;

	tlp_sia_update_link(sc);

	ife = TULIP_CURRENT_MEDIA(sc);

	if (sc->sc_flags & TULIPF_LINK_VALID)
		ifmr->ifm_status |= IFM_AVALID;
	if (sc->sc_flags & TULIPF_LINK_UP)
		ifmr->ifm_status |= IFM_ACTIVE;
	ifmr->ifm_active = ife->ifm_media;
}

static void
tlp_sia_fixup(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife;
	struct tulip_21x4x_media *tm;
	uint32_t siaconn, siatxrx, siagen;

	switch (sc->sc_chip) {
	case TULIP_CHIP_82C115:
	case TULIP_CHIP_MX98713A:
	case TULIP_CHIP_MX98715:
	case TULIP_CHIP_MX98715A:
	case TULIP_CHIP_MX98715AEC_X:
	case TULIP_CHIP_MX98725:
		siaconn = PMAC_SIACONN_MASK;
		siatxrx = PMAC_SIATXRX_MASK;
		siagen  = PMAC_SIAGEN_MASK;
		break;

	default:
		/* No fixups required on any other chips. */
		return;
	}

	TAILQ_FOREACH(ife, &sc->sc_mii.mii_media.ifm_list, ifm_list) {
		tm = ife->ifm_aux;
		if (tm == NULL)
			continue;

		tm->tm_siaconn &= siaconn;
		tm->tm_siatxrx &= siatxrx;
		tm->tm_siagen  &= siagen;
	}
}

static int
tlp_sia_set(struct tulip_softc *sc)
{

	return tlp_sia_media(sc, TULIP_CURRENT_MEDIA(sc));
}

static int
tlp_sia_media(struct tulip_softc *sc, struct ifmedia_entry *ife)
{
	struct tulip_21x4x_media *tm;

	tm = ife->ifm_aux;

	/*
	 * XXX This appears to be necessary on a bunch of the clone chips.
	 */
	delay(20000);

	/*
	 * Idle the chip.
	 */
	tlp_idle(sc, OPMODE_ST | OPMODE_SR);

	/*
	 * Program the SIA.  It's important to write in this order,
	 * resetting the SIA first.
	 */
	TULIP_WRITE(sc, CSR_SIACONN, 0);		/* SRL bit clear */
	delay(1000);

	TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);

	switch (sc->sc_chip) {
	case TULIP_CHIP_21142:
	case TULIP_CHIP_21143:
		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
		break;
	default:
		TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen);
	}

	TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);

	/*
	 * Set the OPMODE bits for this media and write OPMODE.
	 * This will resume the transmit and receive processes.
	 */
	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);

	return 0;
}

/*
 * 21140 GPIO utility functions.
 */
static void	tlp_21140_gpio_update_link(struct tulip_softc *);

static void
tlp_21140_gpio_update_link(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife;
	struct tulip_21x4x_media *tm;

	ife = TULIP_CURRENT_MEDIA(sc);
	tm = ife->ifm_aux;

	sc->sc_flags &= ~(TULIPF_LINK_UP | TULIPF_LINK_VALID);

	if (tm->tm_actmask != 0) {
		sc->sc_flags |= TULIPF_LINK_VALID;
		if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
		    tm->tm_actdata)
			sc->sc_flags |= TULIPF_LINK_UP;
	}
}

void
tlp_21140_gpio_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
{
	struct ifmedia_entry *ife;

	ifmr->ifm_status = 0;

	tlp_21140_gpio_update_link(sc);

	ife = TULIP_CURRENT_MEDIA(sc);

	if (sc->sc_flags & TULIPF_LINK_VALID)
		ifmr->ifm_status |= IFM_AVALID;
	if (sc->sc_flags & TULIPF_LINK_UP)
		ifmr->ifm_status |= IFM_ACTIVE;
	ifmr->ifm_active = ife->ifm_media;
}

int
tlp_21140_gpio_set(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife;
	struct tulip_21x4x_media *tm;

	ife = TULIP_CURRENT_MEDIA(sc);
	tm = ife->ifm_aux;

	/*
	 * Idle the chip.
	 */
	tlp_idle(sc, OPMODE_ST | OPMODE_SR);

	/*
	 * Set the GPIO pins for this media, to flip any
	 * relays, etc.
	 */
	TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);
	delay(10);
	TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);

	/*
	 * Set the OPMODE bits for this media and write OPMODE.
	 * This will resume the transmit and receive processes.
	 */
	sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);

	return 0;
}

/*
 * 21040 and 21041 media switches.
 */
static void	tlp_21040_tmsw_init(struct tulip_softc *);
static void	tlp_21040_tp_tmsw_init(struct tulip_softc *);
static void	tlp_21040_auibnc_tmsw_init(struct tulip_softc *);
static void	tlp_21041_tmsw_init(struct tulip_softc *);

const struct tulip_mediasw tlp_21040_mediasw = {
	tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
};

const struct tulip_mediasw tlp_21040_tp_mediasw = {
	tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
};

const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
	tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
};

const struct tulip_mediasw tlp_21041_mediasw = {
	tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
};

static void
tlp_21040_tmsw_init(struct tulip_softc *sc)
{
	struct mii_data * const mii = &sc->sc_mii;
	static const uint8_t media[] = {
		TULIP_ROM_MB_MEDIA_TP,
		TULIP_ROM_MB_MEDIA_TP_FDX,
		TULIP_ROM_MB_MEDIA_AUI,
	};
	struct tulip_21x4x_media *tm;

	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);

	tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);

	/*
	 * No SROM type for External SIA.
	 */
	tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
	tm->tm_name = "manual";
	tm->tm_opmode = 0;
	tm->tm_siaconn = SIACONN_21040_EXTSIA;
	tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
	tm->tm_siagen  = SIAGEN_21040_EXTSIA;
	ifmedia_add(&mii->mii_media,
	    IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);

	/*
	 * XXX Autosense not yet supported.
	 */

	/* XXX This should be auto-sense. */
	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);

	tlp_print_media(sc);
}

static void
tlp_21040_tp_tmsw_init(struct tulip_softc *sc)
{
	struct mii_data * const mii = &sc->sc_mii;
	static const uint8_t media[] = {
		TULIP_ROM_MB_MEDIA_TP,
		TULIP_ROM_MB_MEDIA_TP_FDX,
	};

	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);

	tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);

	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);

	tlp_print_media(sc);
}

static void
tlp_21040_auibnc_tmsw_init(struct tulip_softc *sc)
{
	struct mii_data * const mii = &sc->sc_mii;
	static const uint8_t media[] = {
		TULIP_ROM_MB_MEDIA_AUI,
	};

	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);

	tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);

	ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_5);

	tlp_print_media(sc);
}

static void
tlp_21041_tmsw_init(struct tulip_softc *sc)
{
	struct mii_data * const mii = &sc->sc_mii;
	static const uint8_t media[] = {
		TULIP_ROM_MB_MEDIA_TP,
		TULIP_ROM_MB_MEDIA_TP_FDX,
		TULIP_ROM_MB_MEDIA_BNC,
		TULIP_ROM_MB_MEDIA_AUI,
	};
	int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
	const struct tulip_srom_to_ifmedia *tsti;
	struct tulip_21x4x_media *tm;
	uint16_t romdef;
	uint8_t mb;

	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);

	if (tlp_isv_srom(sc->sc_srom) == 0) {
 not_isv_srom:
		/*
		 * If we have a board without the standard 21041 SROM format,
		 * we just assume all media are present and try and pick a
		 * reasonable default.
		 */
		tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);

		/*
		 * XXX Autosense not yet supported.
		 */

		/* XXX This should be auto-sense. */
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);

		tlp_print_media(sc);
		return;
	}

	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
	for (i = 0; i < devcnt; i++) {
		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
			break;
		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
		    sc->sc_devno)
			break;
	}

	if (i == devcnt)
		goto not_isv_srom;

	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
	mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
	m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];

	for (; m_cnt != 0;
	     m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
		mb = sc->sc_srom[mb_offset];
		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
		switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
		case TULIP_ROM_MB_MEDIA_TP_FDX:
		case TULIP_ROM_MB_MEDIA_TP:
		case TULIP_ROM_MB_MEDIA_BNC:
		case TULIP_ROM_MB_MEDIA_AUI:
			tsti = tlp_srom_to_ifmedia(mb &
			    TULIP_ROM_MB_MEDIA_CODE);

			tlp_srom_media_info(sc, tsti, tm);

			/*
			 * Override our default SIA settings if the
			 * SROM contains its own.
			 */
			if (mb & TULIP_ROM_MB_EXT) {
				tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
				    mb_offset + TULIP_ROM_MB_CSR13);
				tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
				    mb_offset + TULIP_ROM_MB_CSR14);
				tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
				    mb_offset + TULIP_ROM_MB_CSR15);
			}

			ifmedia_add(&mii->mii_media,
			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
			break;

		default:
			aprint_error_dev(sc->sc_dev,
			    "unknown media code 0x%02x\n",
			    mb & TULIP_ROM_MB_MEDIA_CODE);
			free(tm, M_DEVBUF);
		}
	}

	/*
	 * XXX Autosense not yet supported.
	 */

	romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
	    TULIP_ROM_IL_SELECT_CONN_TYPE);
	switch (romdef) {
	case SELECT_CONN_TYPE_TP:
	case SELECT_CONN_TYPE_TP_AUTONEG:
	case SELECT_CONN_TYPE_TP_NOLINKPASS:
		defmedia = IFM_ETHER | IFM_10_T;
		break;

	case SELECT_CONN_TYPE_TP_FDX:
		defmedia = IFM_ETHER | IFM_10_T | IFM_FDX;
		break;

	case SELECT_CONN_TYPE_BNC:
		defmedia = IFM_ETHER | IFM_10_2;
		break;

	case SELECT_CONN_TYPE_AUI:
		defmedia = IFM_ETHER | IFM_10_5;
		break;
#if 0 /* XXX */
	case SELECT_CONN_TYPE_ASENSE:
	case SELECT_CONN_TYPE_ASENSE_AUTONEG:
		defmedia = IFM_ETHER | IFM_AUTO;
		break;
#endif
	default:
		defmedia = 0;
	}

	if (defmedia == 0) {
		/*
		 * XXX We should default to auto-sense.
		 */
		defmedia = IFM_ETHER | IFM_10_T;
	}

	ifmedia_set(&mii->mii_media, defmedia);

	tlp_print_media(sc);
}

/*
 * DECchip 2114x ISV media switch.
 */
static void	tlp_2114x_isv_tmsw_init(struct tulip_softc *);
static void	tlp_2114x_isv_tmsw_get(struct tulip_softc *,
		    struct ifmediareq *);
static int	tlp_2114x_isv_tmsw_set(struct tulip_softc *);

const struct tulip_mediasw tlp_2114x_isv_mediasw = {
	tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
};

static void	tlp_2114x_nway_get(struct tulip_softc *, struct ifmediareq *);
static int	tlp_2114x_nway_set(struct tulip_softc *);

static void	tlp_2114x_nway_statchg(struct ifnet *);
static int	tlp_2114x_nway_service(struct tulip_softc *, int);
static void	tlp_2114x_nway_auto(struct tulip_softc *);
static void	tlp_2114x_nway_status(struct tulip_softc *);

static void
tlp_2114x_isv_tmsw_init(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;
	struct ifmedia_entry *ife;
	struct mii_softc *phy;
	struct tulip_21x4x_media *tm;
	const struct tulip_srom_to_ifmedia *tsti;
	int i, devcnt, leaf_offset, m_cnt, type, length;
	int defmedia, miidef;
	uint16_t word;
	uint8_t *cp, *ncp;

	defmedia = miidef = 0;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_bitbang_mii_readreg;
	mii->mii_writereg = tlp_bitbang_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;

	/*
	 * Ignore `instance'; we may get a mixture of SIA and MII
	 * media, and `instance' is used to isolate or select the
	 * PHY on the MII as appropriate.  Note that duplicate media
	 * are disallowed, so ignoring `instance' is safe.
	 */
	ifmedia_init(&mii->mii_media, IFM_IMASK, tlp_mediachange,
	    tlp_mediastatus);

	devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
	for (i = 0; i < devcnt; i++) {
		if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
			break;
		if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
		    sc->sc_devno)
			break;
	}

	if (i == devcnt) {
		aprint_error_dev(sc->sc_dev,
		    "unable to locate info leaf in SROM\n");
		return;
	}

	leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
	    TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));

	/* XXX SELECT CONN TYPE */

	cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];

	/*
	 * On some chips, the first thing in the Info Leaf is the
	 * GPIO pin direction data.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_21140:
	case TULIP_CHIP_21140A:
	case TULIP_CHIP_MX98713:
	case TULIP_CHIP_AX88140:
	case TULIP_CHIP_AX88141:
		sc->sc_gp_dir = *cp++;
		break;

	default:
		/* Nothing. */
		break;
	}

	/* Get the media count. */
	m_cnt = *cp++;

	if (m_cnt == 0) {
		sc->sc_mediasw = &tlp_sio_mii_mediasw;
		(*sc->sc_mediasw->tmsw_init)(sc);
		return;
	}

	for (; m_cnt != 0; cp = ncp, m_cnt--) {
		/*
		 * Determine the type and length of this media block.
		 * The 21143 is spec'd to always use extended format blocks,
		 * but some cards don't set the bit to indicate this.
		 * Hopefully there are no cards which really don't use
		 * extended format blocks.
		 */
		if ((*cp & 0x80) == 0 && sc->sc_chip != TULIP_CHIP_21143) {
			length = 4;
			type = TULIP_ROM_MB_21140_GPR;
		} else {
			length = (*cp++ & 0x7f) - 1;
			type = *cp++ & 0x3f;
		}

		/* Compute the start of the next block. */
		ncp = cp + length;

		/* Now, parse the block. */
		switch (type) {
		case TULIP_ROM_MB_21140_GPR:
			tlp_get_minst(sc);
			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;

			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);

			tm->tm_type = TULIP_ROM_MB_21140_GPR;
			tm->tm_get = tlp_21140_gpio_get;
			tm->tm_set = tlp_21140_gpio_set;

			/* First is the media type code. */
			tsti = tlp_srom_to_ifmedia(cp[0] &
			    TULIP_ROM_MB_MEDIA_CODE);
			if (tsti == NULL) {
				/* Invalid media code. */
				free(tm, M_DEVBUF);
				break;
			}

			/* Get defaults. */
			tlp_srom_media_info(sc, tsti, tm);

			/* Next is any GPIO info for this media. */
			tm->tm_gpdata = cp[1];

			/*
			 * Next is a word containing OPMODE information
			 * and info on how to detect if this media is
			 * active.
			 */
			word = TULIP_ROM_GETW(cp, 2);
			tm->tm_opmode &= OPMODE_FD;
			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
				tm->tm_actmask =
				    TULIP_ROM_MB_BITPOS(word);
				tm->tm_actdata =
				    (word & TULIP_ROM_MB_POLARITY) ?
				    0 : tm->tm_actmask;
			}

			ifmedia_add(&mii->mii_media,
			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
			break;

		case TULIP_ROM_MB_21140_MII:
			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;

			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);

			tm->tm_type = TULIP_ROM_MB_21140_MII;
			tm->tm_get = tlp_mii_getmedia;
			tm->tm_set = tlp_mii_setmedia;
			tm->tm_opmode = OPMODE_PS;

			if (sc->sc_reset == NULL)
				sc->sc_reset = tlp_21140_reset;

			/* First is the PHY number. */
			tm->tm_phyno = *cp++;

			/* Next is the MII select sequence length and offset. */
			tm->tm_gp_length = *cp++;
			tm->tm_gp_offset = cp - &sc->sc_srom[0];
			cp += tm->tm_gp_length;

			/* Next is the MII reset sequence length and offset. */
			tm->tm_reset_length = *cp++;
			tm->tm_reset_offset = cp - &sc->sc_srom[0];
			cp += tm->tm_reset_length;

			/*
			 * The following items are left in the media block
			 * that we don't particularly care about:
			 *
			 *	capabilities		W
			 *	advertisement		W
			 *	full duplex		W
			 *	tx threshold		W
			 *
			 * These appear to be bits in the PHY registers,
			 * which our MII code handles on its own.
			 */

			/*
			 * Before we probe the MII bus, we need to reset
			 * it and issue the selection sequence.
			 */

			/* Set the direction of the pins... */
			TULIP_WRITE(sc, CSR_GPP, GPP_GPC | sc->sc_gp_dir);

			for (i = 0; i < tm->tm_reset_length; i++) {
				delay(10);
				TULIP_WRITE(sc, CSR_GPP,
				    sc->sc_srom[tm->tm_reset_offset + i]);
			}

			for (i = 0; i < tm->tm_gp_length; i++) {
				delay(10);
				TULIP_WRITE(sc, CSR_GPP,
				    sc->sc_srom[tm->tm_gp_offset + i]);
			}

			/* If there were no sequences, just lower the pins. */
			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
				delay(10);
				TULIP_WRITE(sc, CSR_GPP, 0);
			}

			/*
			 * Now, probe the MII for the PHY.  Note, we know
			 * the location of the PHY on the bus, but we don't
			 * particularly care; the MII code just likes to
			 * search the whole thing anyhow.
			 */
			mii_attach(sc->sc_dev, mii, 0xffffffff,
			    MII_PHY_ANY, tm->tm_phyno, 0);

			/*
			 * Now, search for the PHY we hopefully just
			 * configured.  If it's not configured into the
			 * kernel, we lose.  The PHY's default media always
			 * takes priority.
			 */
			LIST_FOREACH(phy, &mii->mii_phys, mii_list) {
				if (phy->mii_offset == tm->tm_phyno)
					break;
			}
			if (phy == NULL) {
				aprint_error_dev(sc->sc_dev,
				    "unable to configure MII\n");
				break;
			}

			sc->sc_flags |= TULIPF_HAS_MII;
			sc->sc_tick = tlp_mii_tick;
			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
			    phy->mii_inst);

			/*
			 * Okay, now that we've found the PHY and the MII
			 * layer has added all of the media associated
			 * with that PHY, we need to traverse the media
			 * list, and add our `tm' to each entry's `aux'
			 * pointer.
			 *
			 * We do this by looking for media with our
			 * PHY's `instance'.
			 */
			TAILQ_FOREACH(ife, &mii->mii_media.ifm_list,
			      ifm_list) {
				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
					continue;
				ife->ifm_aux = tm;
			}
			break;

		case TULIP_ROM_MB_21142_SIA:
			tlp_get_minst(sc);
			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;

			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);

			tm->tm_type = TULIP_ROM_MB_21142_SIA;
			tm->tm_get = tlp_sia_get;
			tm->tm_set = tlp_sia_set;

			/* First is the media type code. */
			tsti = tlp_srom_to_ifmedia(cp[0] &
			    TULIP_ROM_MB_MEDIA_CODE);
			if (tsti == NULL) {
				/* Invalid media code. */
				free(tm, M_DEVBUF);
				break;
			}

			/* Get defaults. */
			tlp_srom_media_info(sc, tsti, tm);

			/*
			 * Override our default SIA settings if the
			 * SROM contains its own.
			 */
			if (cp[0] & 0x40) {
				tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
				tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
				tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
				cp += 7;
			} else
				cp++;

			/* Next is GPIO control/data. */
			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0) << 16;
			tm->tm_gpdata = TULIP_ROM_GETW(cp, 2) << 16;

			ifmedia_add(&mii->mii_media,
			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
			break;

		case TULIP_ROM_MB_21142_MII:
			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;

			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);

			tm->tm_type = TULIP_ROM_MB_21142_MII;
			tm->tm_get = tlp_mii_getmedia;
			tm->tm_set = tlp_mii_setmedia;
			tm->tm_opmode = OPMODE_PS;

			if (sc->sc_reset == NULL)
				sc->sc_reset = tlp_21142_reset;

			/* First is the PHY number. */
			tm->tm_phyno = *cp++;

			/* Next is the MII select sequence length and offset. */
			tm->tm_gp_length = *cp++;
			tm->tm_gp_offset = cp - &sc->sc_srom[0];
			cp += tm->tm_gp_length * 2;

			/* Next is the MII reset sequence length and offset. */
			tm->tm_reset_length = *cp++;
			tm->tm_reset_offset = cp - &sc->sc_srom[0];
			cp += tm->tm_reset_length * 2;

			/*
			 * The following items are left in the media block
			 * that we don't particularly care about:
			 *
			 *	capabilities		W
			 *	advertisement		W
			 *	full duplex		W
			 *	tx threshold		W
			 *	MII interrupt		W
			 *
			 * These appear to be bits in the PHY registers,
			 * which our MII code handles on its own.
			 */

			/*
			 * Before we probe the MII bus, we need to reset
			 * it and issue the selection sequence.
			 */

			cp = &sc->sc_srom[tm->tm_reset_offset];
			for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
				delay(10);
				TULIP_WRITE(sc, CSR_SIAGEN,
				    TULIP_ROM_GETW(cp, 0) << 16);
			}

			cp = &sc->sc_srom[tm->tm_gp_offset];
			for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
				delay(10);
				TULIP_WRITE(sc, CSR_SIAGEN,
				    TULIP_ROM_GETW(cp, 0) << 16);
			}

			/* If there were no sequences, just lower the pins. */
			if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
				delay(10);
				TULIP_WRITE(sc, CSR_SIAGEN, 0);
			}

			/*
			 * Now, probe the MII for the PHY.  Note, we know
			 * the location of the PHY on the bus, but we don't
			 * particularly care; the MII code just likes to
			 * search the whole thing anyhow.
			 */
			mii_attach(sc->sc_dev, mii, 0xffffffff,
			    MII_PHY_ANY, tm->tm_phyno, 0);

			/*
			 * Now, search for the PHY we hopefully just
			 * configured.  If it's not configured into the
			 * kernel, we lose.  The PHY's default media always
			 * takes priority.
			 */
			LIST_FOREACH(phy, &mii->mii_phys, mii_list) {
				if (phy->mii_offset == tm->tm_phyno)
					break;
			}
			if (phy == NULL) {
				aprint_error_dev(sc->sc_dev,
				    "unable to configure MII\n");
				break;
			}

			sc->sc_flags |= TULIPF_HAS_MII;
			sc->sc_tick = tlp_mii_tick;
			miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
			    phy->mii_inst);

			/*
			 * Okay, now that we've found the PHY and the MII
			 * layer has added all of the media associated
			 * with that PHY, we need to traverse the media
			 * list, and add our `tm' to each entry's `aux'
			 * pointer.
			 *
			 * We do this by looking for media with our
			 * PHY's `instance'.
			 */
			TAILQ_FOREACH(ife, &mii->mii_media.ifm_list,
			      ifm_list) {
				if (IFM_INST(ife->ifm_media) != phy->mii_inst)
					continue;
				ife->ifm_aux = tm;
			}
			break;

		case TULIP_ROM_MB_21143_SYM:
			tlp_get_minst(sc);
			sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;

			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);

			tm->tm_type = TULIP_ROM_MB_21143_SYM;
			tm->tm_get = tlp_sia_get;
			tm->tm_set = tlp_sia_set;

			/* First is the media type code. */
			tsti = tlp_srom_to_ifmedia(cp[0] &
			    TULIP_ROM_MB_MEDIA_CODE);
			if (tsti == NULL) {
				/* Invalid media code. */
				free(tm, M_DEVBUF);
				break;
			}

			/* Get defaults. */
			tlp_srom_media_info(sc, tsti, tm);

			/* Next is GPIO control/data. */
			tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1) << 16;
			tm->tm_gpdata = TULIP_ROM_GETW(cp, 3) << 16;

			/*
			 * Next is a word containing OPMODE information
			 * and info on how to detect if this media is
			 * active.
			 */
			word = TULIP_ROM_GETW(cp, 5);
			tm->tm_opmode &= OPMODE_FD;
			tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
			if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
				tm->tm_actmask =
				    TULIP_ROM_MB_BITPOS(word);
				tm->tm_actdata =
				    (word & TULIP_ROM_MB_POLARITY) ?
				    0 : tm->tm_actmask;
			}

			ifmedia_add(&mii->mii_media,
			    IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
			    tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
			break;

		case TULIP_ROM_MB_21143_RESET:
			aprint_normal_dev(sc->sc_dev, "21143 reset block\n");
			break;

		default:
			aprint_error_dev(sc->sc_dev,
			    "unknown ISV media block type 0x%02x\n", type);
		}
	}

	/*
	 * Deal with the case where no media is configured.
	 */
	if (TAILQ_FIRST(&mii->mii_media.ifm_list) == NULL) {
		aprint_error_dev(sc->sc_dev, "no media found!\n");
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
		return;
	}

	/*
	 * Pick the default media.
	 */
	if (miidef != 0)
		defmedia = miidef;
	else {
		switch (sc->sc_chip) {
		case TULIP_CHIP_21140:
		case TULIP_CHIP_21140A:
			/* XXX should come from SROM */
			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
			if (ifmedia_match(&mii->mii_media, defmedia,
				mii->mii_media.ifm_mask) == NULL) {
				/*
				 * There is not a 10baseT media.
				 * Fall back to the first found one.
				 */
				ife = TAILQ_FIRST(&mii->mii_media.ifm_list);
				defmedia = ife->ifm_media;
			}
			break;

		case TULIP_CHIP_21142:
		case TULIP_CHIP_21143:
		case TULIP_CHIP_MX98713A:
		case TULIP_CHIP_MX98715:
		case TULIP_CHIP_MX98715A:
		case TULIP_CHIP_MX98715AEC_X:
		case TULIP_CHIP_MX98725:
			tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
			tm->tm_name = "auto";
			tm->tm_get = tlp_2114x_nway_get;
			tm->tm_set = tlp_2114x_nway_set;

			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0);
			ifmedia_add(&mii->mii_media, defmedia, 0, tm);

			sc->sc_statchg = tlp_2114x_nway_statchg;
			sc->sc_tick = tlp_2114x_nway_tick;
			break;

		default:
			defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
			break;
		}
	}

	ifmedia_set(&mii->mii_media, defmedia);

	/*
	 * Display any non-MII media we've located.
	 */
	if (sc->sc_media_seen &
	    ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
		tlp_print_media(sc);

	tlp_sia_fixup(sc);
}

static void
tlp_2114x_nway_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
{

	(void) tlp_2114x_nway_service(sc, MII_POLLSTAT);
	ifmr->ifm_status = sc->sc_mii.mii_media_status;
	ifmr->ifm_active = sc->sc_mii.mii_media_active;
}

static int
tlp_2114x_nway_set(struct tulip_softc *sc)
{

	return tlp_2114x_nway_service(sc, MII_MEDIACHG);
}

static void
tlp_2114x_nway_statchg(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;
	struct mii_data *mii = &sc->sc_mii;
	struct ifmedia_entry *ife;

	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)
		return;

	if ((ife = ifmedia_match(&mii->mii_media, mii->mii_media_active,
	    mii->mii_media.ifm_mask)) == NULL) {
		printf("tlp_2114x_nway_statchg: no match for media 0x%x/0x%x\n",
		    mii->mii_media_active, ~mii->mii_media.ifm_mask);
		panic("tlp_2114x_nway_statchg");
	}

	tlp_sia_media(sc, ife);
}

static void
tlp_2114x_nway_tick(void *arg)
{
	struct tulip_softc *sc = arg;
	struct mii_data *mii = &sc->sc_mii;
	int s, ticks;

	if (!device_is_active(sc->sc_dev))
		return;

	s = splnet();
	tlp_2114x_nway_service(sc, MII_TICK);
	if ((sc->sc_flags & TULIPF_LINK_UP) == 0 &&
	    (mii->mii_media_status & IFM_ACTIVE) != 0 &&
	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
		sc->sc_flags |= TULIPF_LINK_UP;
		tlp_start(&sc->sc_ethercom.ec_if);
	} else if ((sc->sc_flags & TULIPF_LINK_UP) != 0 &&
	    (mii->mii_media_status & IFM_ACTIVE) == 0) {
		sc->sc_flags &= ~TULIPF_LINK_UP;
	}
	splx(s);

	if ((sc->sc_flags & TULIPF_LINK_UP) == 0)
		ticks = hz >> 3;
	else
		ticks = hz;
	callout_reset(&sc->sc_tick_callout, ticks, tlp_2114x_nway_tick, sc);
}

/*
 * Support for the 2114X internal NWay block.  This is constructed
 * somewhat like a PHY driver for simplicity.
 */

static int
tlp_2114x_nway_service(struct tulip_softc *sc, int cmd)
{
	struct mii_data *mii = &sc->sc_mii;
	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;

	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
		return 0;

	switch (cmd) {
	case MII_POLLSTAT:
		/* Nothing special to do here. */
		break;

	case MII_MEDIACHG:
		switch (IFM_SUBTYPE(ife->ifm_media)) {
		case IFM_AUTO:
			goto restart;
		default:
			/* Manual setting doesn't go through here. */
			printf("tlp_2114x_nway_service: oops!\n");
			return EINVAL;
		}
		break;

	case MII_TICK:
		/*
		 * Only used for autonegotiation.
		 */
		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
			break;

		/*
		 * Check to see if we have link.  If we do, we don't
		 * need to restart the autonegotiation process.
		 */
#if 0
		if (mii->mii_media_status & IFM_ACTIVE)
#else
		if (sc->sc_flags & TULIPF_LINK_UP)
#endif
			break;

		/*
		 * Only retry autonegotiation every 5 seconds.
		 */
		if (++sc->sc_nway_ticks != (5 << 3))
			break;

	restart:
		sc->sc_nway_ticks = 0;
		ife->ifm_data = IFM_NONE;
		tlp_2114x_nway_auto(sc);
		break;
	}

	/* Update the media status. */
	tlp_2114x_nway_status(sc);

	/*
	 * Callback if something changed.  Manually configuration goes through
	 * tlp_sia_set() anyway, so ignore that here.
	 */
	if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO &&
	    ife->ifm_data != mii->mii_media_active) {
		(*sc->sc_statchg)(mii->mii_ifp);
		ife->ifm_data = mii->mii_media_active;
	}
	return 0;
}

static void
tlp_2114x_nway_auto(struct tulip_softc *sc)
{
	uint32_t siastat, siatxrx;

	tlp_idle(sc, OPMODE_ST | OPMODE_SR);

	sc->sc_opmode &= ~(OPMODE_PS | OPMODE_PCS | OPMODE_SCR | OPMODE_FD);
	sc->sc_opmode |= OPMODE_TTM | OPMODE_HBD;
	siatxrx = 0xffbf;		/* XXX magic number */

	/* Compute the link code word to advertise. */
	if (sc->sc_sia_cap & BMSR_100T4)
		siatxrx |= SIATXRX_T4;
	if (sc->sc_sia_cap & BMSR_100TXFDX)
		siatxrx |= SIATXRX_TXF;
	if (sc->sc_sia_cap & BMSR_100TXHDX)
		siatxrx |= SIATXRX_THX;
	if (sc->sc_sia_cap & BMSR_10TFDX)
		sc->sc_opmode |= OPMODE_FD;
	if (sc->sc_sia_cap & BMSR_10THDX)
		siatxrx |= SIATXRX_TH;

	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);

	TULIP_WRITE(sc, CSR_SIACONN, 0);
	delay(1000);
	TULIP_WRITE(sc, CSR_SIATXRX, siatxrx);
	TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);

	siastat = TULIP_READ(sc, CSR_SIASTAT);
	siastat &= ~(SIASTAT_ANS | SIASTAT_LPC | SIASTAT_TRA | SIASTAT_ARA |
		     SIASTAT_LS100 | SIASTAT_LS10 | SIASTAT_MRA);
	siastat |= SIASTAT_ANS_TXDIS;
	TULIP_WRITE(sc, CSR_SIASTAT, siastat);
}

static void
tlp_2114x_nway_status(struct tulip_softc *sc)
{
	struct mii_data *mii = &sc->sc_mii;
	uint32_t siatxrx, siastat, anlpar;

	mii->mii_media_status = IFM_AVALID;
	mii->mii_media_active = IFM_ETHER;

	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
		return;

	siastat = TULIP_READ(sc, CSR_SIASTAT);
	siatxrx = TULIP_READ(sc, CSR_SIATXRX);

	if (siatxrx & SIATXRX_ANE) {
		if ((siastat & SIASTAT_ANS) != SIASTAT_ANS_FLPGOOD) {
			/* Erg, still trying, I guess... */
			mii->mii_media_active |= IFM_NONE;
			return;
		}

		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
			mii->mii_media_status |= IFM_ACTIVE;

		if (siastat & SIASTAT_LPN) {
			anlpar = SIASTAT_GETLPC(siastat);
			if (anlpar & ANLPAR_T4 &&
			    sc->sc_sia_cap & BMSR_100T4)
				mii->mii_media_active |= IFM_100_T4;
			else if (anlpar & ANLPAR_TX_FD &&
				 sc->sc_sia_cap & BMSR_100TXFDX)
				mii->mii_media_active |= IFM_100_TX | IFM_FDX;
			else if (anlpar & ANLPAR_TX &&
				 sc->sc_sia_cap & BMSR_100TXHDX)
				mii->mii_media_active |= IFM_100_TX;
			else if (anlpar & ANLPAR_10_FD &&
				 sc->sc_sia_cap & BMSR_10TFDX)
				mii->mii_media_active |= IFM_10_T | IFM_FDX;
			else if (anlpar & ANLPAR_10 &&
				 sc->sc_sia_cap & BMSR_10THDX)
				mii->mii_media_active |= IFM_10_T;
			else
				mii->mii_media_active |= IFM_NONE;
		} else {
			/*
			 * If the other side doesn't support NWAY, then the
			 * best we can do is determine if we have a 10Mbps or
			 * 100Mbps link. There's no way to know if the link
			 * is full or half duplex, so we default to half duplex
			 * and hope that the user is clever enough to manually
			 * change the media settings if we're wrong.
			 */
			if ((siastat & SIASTAT_LS100) == 0)
				mii->mii_media_active |= IFM_100_TX;
			else if ((siastat & SIASTAT_LS10) == 0)
				mii->mii_media_active |= IFM_10_T;
			else
				mii->mii_media_active |= IFM_NONE;
		}
	} else {
		if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
			mii->mii_media_status |= IFM_ACTIVE;

		if (sc->sc_opmode & OPMODE_TTM)
			mii->mii_media_active |= IFM_10_T;
		else
			mii->mii_media_active |= IFM_100_TX;
		if (sc->sc_opmode & OPMODE_FD)
			mii->mii_media_active |= IFM_FDX;
	}
}

static void
tlp_2114x_isv_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
{
	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
	struct tulip_21x4x_media *tm = ife->ifm_aux;

	(*tm->tm_get)(sc, ifmr);
}

static int
tlp_2114x_isv_tmsw_set(struct tulip_softc *sc)
{
	struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
	struct tulip_21x4x_media *tm = ife->ifm_aux;

	/*
	 * Check to see if we need to reset the chip, and do it.  The
	 * reset path will get the OPMODE register right the next
	 * time through.
	 */
	if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
		return tlp_init(&sc->sc_ethercom.ec_if);

	return (*tm->tm_set)(sc);
}

/*
 * MII-on-SIO media switch.  Handles only MII attached to the SIO.
 */
static void	tlp_sio_mii_tmsw_init(struct tulip_softc *);

const struct tulip_mediasw tlp_sio_mii_mediasw = {
	tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
};

static void
tlp_sio_mii_tmsw_init(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;

	/*
	 * We don't attach any media info structures to the ifmedia
	 * entries, so if we're using a pre-init function that needs
	 * that info, override it to one that doesn't.
	 */
	if (sc->sc_preinit == tlp_2114x_preinit)
		sc->sc_preinit = tlp_2114x_mii_preinit;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_bitbang_mii_readreg;
	mii->mii_writereg = tlp_bitbang_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
	    MII_OFFSET_ANY, 0);
	if (LIST_FIRST(&mii->mii_phys) == NULL) {
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
	} else {
		sc->sc_flags |= TULIPF_HAS_MII;
		sc->sc_tick = tlp_mii_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	}
}

/*
 * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
 */
static void	tlp_pnic_tmsw_init(struct tulip_softc *);
static void	tlp_pnic_tmsw_get(struct tulip_softc *, struct ifmediareq *);
static int	tlp_pnic_tmsw_set(struct tulip_softc *);

const struct tulip_mediasw tlp_pnic_mediasw = {
	tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
};

static void	tlp_pnic_nway_statchg(struct ifnet *);
static void	tlp_pnic_nway_tick(void *);
static int	tlp_pnic_nway_service(struct tulip_softc *, int);
static void	tlp_pnic_nway_reset(struct tulip_softc *);
static int	tlp_pnic_nway_auto(struct tulip_softc *, int);
static void	tlp_pnic_nway_auto_timeout(void *);
static void	tlp_pnic_nway_status(struct tulip_softc *);
static void	tlp_pnic_nway_acomp(struct tulip_softc *);

static void
tlp_pnic_tmsw_init(struct tulip_softc *sc)
{
	struct mii_data * const mii = &sc->sc_mii;
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	const char *sep = "";

#define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
#define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_pnic_mii_readreg;
	mii->mii_writereg = tlp_pnic_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
	    MII_OFFSET_ANY, 0);
	if (LIST_FIRST(&mii->mii_phys) == NULL) {
		/* XXX What about AUI/BNC support? */
		aprint_normal_dev(sc->sc_dev, "");

		tlp_pnic_nway_reset(sc);

		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
		    PNIC_NWAY_TW | PNIC_NWAY_CAP10T);
		PRINT("10baseT");

		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
		    PNIC_NWAY_TW | PNIC_NWAY_FD | PNIC_NWAY_CAP10TFDX);
		PRINT("10baseT-FDX");

		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
		    PNIC_NWAY_TW | PNIC_NWAY_100 | PNIC_NWAY_CAP100TX);
		PRINT("100baseTX");

		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
		    PNIC_NWAY_TW | PNIC_NWAY_100 | PNIC_NWAY_FD |
		    PNIC_NWAY_CAP100TXFDX);
		PRINT("100baseTX-FDX");

		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
		    PNIC_NWAY_TW | PNIC_NWAY_RN | PNIC_NWAY_NW |
		    PNIC_NWAY_CAP10T | PNIC_NWAY_CAP10TFDX |
		    PNIC_NWAY_CAP100TXFDX | PNIC_NWAY_CAP100TX);
		PRINT("auto");

		aprint_normal("\n");

		sc->sc_statchg = tlp_pnic_nway_statchg;
		sc->sc_tick = tlp_pnic_nway_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	} else {
		sc->sc_flags |= TULIPF_HAS_MII;
		sc->sc_tick = tlp_mii_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	}

#undef ADD
#undef PRINT
}

static void
tlp_pnic_tmsw_get(struct tulip_softc *sc, struct ifmediareq *ifmr)
{
	struct mii_data *mii = &sc->sc_mii;

	if (sc->sc_flags & TULIPF_HAS_MII)
		tlp_mii_getmedia(sc, ifmr);
	else {
		mii->mii_media_status = 0;
		mii->mii_media_active = IFM_NONE;
		tlp_pnic_nway_service(sc, MII_POLLSTAT);
		ifmr->ifm_status = mii->mii_media_status;
		ifmr->ifm_active = mii->mii_media_active;
	}
}

static int
tlp_pnic_tmsw_set(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data *mii = &sc->sc_mii;

	if (sc->sc_flags & TULIPF_HAS_MII) {
		/*
		 * Make sure the built-in Tx jabber timer is disabled.
		 */
		TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);

		return tlp_mii_setmedia(sc);
	}

	if (ifp->if_flags & IFF_UP) {
		mii->mii_media_status = 0;
		mii->mii_media_active = IFM_NONE;
		return tlp_pnic_nway_service(sc, MII_MEDIACHG);
	}

	return 0;
}

static void
tlp_pnic_nway_statchg(struct ifnet *ifp)
{
	struct tulip_softc *sc = ifp->if_softc;

	/* Idle the transmit and receive processes. */
	tlp_idle(sc, OPMODE_ST | OPMODE_SR);

	sc->sc_opmode &= ~(OPMODE_TTM | OPMODE_FD | OPMODE_PS | OPMODE_PCS |
	    OPMODE_SCR | OPMODE_HBD);

	if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
		sc->sc_opmode |= OPMODE_TTM;
		TULIP_WRITE(sc, CSR_GPP,
		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
	} else {
		sc->sc_opmode |= OPMODE_PS |OPMODE_PCS |OPMODE_SCR |OPMODE_HBD;
		TULIP_WRITE(sc, CSR_GPP,
		    GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
		    GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
	}

	if (sc->sc_mii.mii_media_active & IFM_FDX)
		sc->sc_opmode |= OPMODE_FD | OPMODE_HBD;

	/*
	 * Write new OPMODE bits.  This also restarts the transmit
	 * and receive processes.
	 */
	TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
}

static void
tlp_pnic_nway_tick(void *arg)
{
	struct tulip_softc *sc = arg;
	int s;

	if (!device_is_active(sc->sc_dev))
		return;

	s = splnet();
	tlp_pnic_nway_service(sc, MII_TICK);
	splx(s);

	callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
}

/*
 * Support for the Lite-On PNIC internal NWay block.  This is constructed
 * somewhat like a PHY driver for simplicity.
 */

static int
tlp_pnic_nway_service(struct tulip_softc *sc, int cmd)
{
	struct mii_data *mii = &sc->sc_mii;
	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;

	if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
		return 0;

	switch (cmd) {
	case MII_POLLSTAT:
		/* Nothing special to do here. */
		break;

	case MII_MEDIACHG:
		switch (IFM_SUBTYPE(ife->ifm_media)) {
		case IFM_AUTO:
			(void) tlp_pnic_nway_auto(sc, 1);
			break;
		case IFM_100_T4:
			/*
			 * XXX Not supported as a manual setting right now.
			 */
			return EINVAL;
		default:
			/*
			 * NWAY register data is stored in the ifmedia entry.
			 */
			TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
		}
		break;

	case MII_TICK:
		/*
		 * Only used for autonegotiation.
		 */
		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
			return 0;

		/*
		 * Check to see if we have link.  If we do, we don't
		 * need to restart the autonegotiation process.
		 */
		if (sc->sc_flags & TULIPF_LINK_UP)
			return 0;

		/*
		 * Only retry autonegotiation every 5 seconds.
		 */
		if (++sc->sc_nway_ticks != 5)
			return 0;

		sc->sc_nway_ticks = 0;
		tlp_pnic_nway_reset(sc);
		if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
			return 0;
		break;
	}

	/* Update the media status. */
	tlp_pnic_nway_status(sc);

	/* Callback if something changed. */
	if ((sc->sc_nway_active == NULL ||
	     sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
	    cmd == MII_MEDIACHG) {
		(*sc->sc_statchg)(mii->mii_ifp);
		tlp_nway_activate(sc, mii->mii_media_active);
	}
	return 0;
}

static void
tlp_pnic_nway_reset(struct tulip_softc *sc)
{

	TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
	delay(100);
	TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
}

static int
tlp_pnic_nway_auto(struct tulip_softc *sc, int waitfor)
{
	struct mii_data *mii = &sc->sc_mii;
	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
	uint32_t reg;
	int i;

	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
		TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);

	if (waitfor) {
		/* Wait 500ms for it to complete. */
		for (i = 0; i < 500; i++) {
			reg = TULIP_READ(sc, CSR_PNIC_NWAY);
			if (reg & PNIC_NWAY_LPAR_MASK) {
				tlp_pnic_nway_acomp(sc);
				return 0;
			}
			delay(1000);
		}
#if 0
		if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
			aprint_error_dev(sc->sc_dev,
			    "autonegotiation failed to complete\n");
#endif

		/*
		 * Don't need to worry about clearing DOINGAUTO.
		 * If that's set, a timeout is pending, and it will
		 * clear the flag.
		 */
		return EIO;
	}

	/*
	 * Just let it finish asynchronously.  This is for the benefit of
	 * the tick handler driving autonegotiation.  Don't want 500ms
	 * delays all the time while the system is running!
	 */
	if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
		sc->sc_flags |= TULIPF_DOINGAUTO;
		callout_reset(&sc->sc_nway_callout, hz >> 1,
		    tlp_pnic_nway_auto_timeout, sc);
	}
	return EJUSTRETURN;
}

static void
tlp_pnic_nway_auto_timeout(void *arg)
{
	struct tulip_softc *sc = arg;
	/* uint32_t reg; */
	int s;

	s = splnet();
	sc->sc_flags &= ~TULIPF_DOINGAUTO;
	/* reg = */
	TULIP_READ(sc, CSR_PNIC_NWAY);
#if 0
	if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
		aprint_error_dev(sc->sc_dev,
		    "autonegotiation failed to complete\n");
#endif

	tlp_pnic_nway_acomp(sc);

	/* Update the media status. */
	(void)tlp_pnic_nway_service(sc, MII_POLLSTAT);
	splx(s);
}

static void
tlp_pnic_nway_status(struct tulip_softc *sc)
{
	struct mii_data *mii = &sc->sc_mii;
	uint32_t reg;

	mii->mii_media_status = IFM_AVALID;
	mii->mii_media_active = IFM_ETHER;

	reg = TULIP_READ(sc, CSR_PNIC_NWAY);

	if (sc->sc_flags & TULIPF_LINK_UP)
		mii->mii_media_status |= IFM_ACTIVE;

	if (reg & PNIC_NWAY_NW) {
		if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
			/* Erg, still trying, I guess... */
			mii->mii_media_active |= IFM_NONE;
			return;
		}

#if 0
		if (reg & PNIC_NWAY_LPAR100T4)
			mii->mii_media_active |= IFM_100_T4;
		else
#endif
		if (reg & PNIC_NWAY_LPAR100TXFDX)
			mii->mii_media_active |= IFM_100_TX | IFM_FDX;
		else if (reg & PNIC_NWAY_LPAR100TX)
			mii->mii_media_active |= IFM_100_TX;
		else if (reg & PNIC_NWAY_LPAR10TFDX)
			mii->mii_media_active |= IFM_10_T | IFM_FDX;
		else if (reg & PNIC_NWAY_LPAR10T)
			mii->mii_media_active |= IFM_10_T;
		else
			mii->mii_media_active |= IFM_NONE;
	} else {
		if (reg & PNIC_NWAY_100)
			mii->mii_media_active |= IFM_100_TX;
		else
			mii->mii_media_active |= IFM_10_T;
		if (reg & PNIC_NWAY_FD)
			mii->mii_media_active |= IFM_FDX;
	}
}

static void
tlp_pnic_nway_acomp(struct tulip_softc *sc)
{
	uint32_t reg;

	reg = TULIP_READ(sc, CSR_PNIC_NWAY);
	reg &= ~(PNIC_NWAY_FD | PNIC_NWAY_100 | PNIC_NWAY_RN);

	if (reg & (PNIC_NWAY_LPAR100TXFDX | PNIC_NWAY_LPAR100TX))
		reg |= PNIC_NWAY_100;
	if (reg & (PNIC_NWAY_LPAR10TFDX | PNIC_NWAY_LPAR100TXFDX))
		reg |= PNIC_NWAY_FD;

	TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
}

/*
 * Macronix PMAC and Lite-On PNIC-II media switch:
 *
 *	MX98713 and MX98713A		21140-like MII or GPIO media.
 *
 *	MX98713A			21143-like MII or SIA/SYM media.
 *
 *	MX98715, MX98715A, MX98725,	21143-like SIA/SYM media.
 *	82C115, MX98715AEC-C, -E
 *
 * So, what we do here is fake MII-on-SIO or ISV media info, and
 * use the ISV media switch get/set functions to handle the rest.
 */

static void	tlp_pmac_tmsw_init(struct tulip_softc *);

const struct tulip_mediasw tlp_pmac_mediasw = {
	tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
};

const struct tulip_mediasw tlp_pmac_mii_mediasw = {
	tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
};

static void
tlp_pmac_tmsw_init(struct tulip_softc *sc)
{
	struct mii_data * const mii = &sc->sc_mii;
	static const uint8_t media[] = {
		TULIP_ROM_MB_MEDIA_TP,
		TULIP_ROM_MB_MEDIA_TP_FDX,
		TULIP_ROM_MB_MEDIA_100TX,
		TULIP_ROM_MB_MEDIA_100TX_FDX,
	};
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct tulip_21x4x_media *tm;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_bitbang_mii_readreg;
	mii->mii_writereg = tlp_bitbang_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
	if (sc->sc_chip == TULIP_CHIP_MX98713 ||
	    sc->sc_chip == TULIP_CHIP_MX98713A) {
		mii_attach(sc->sc_dev, mii, 0xffffffff,
		    MII_PHY_ANY, MII_OFFSET_ANY, 0);
		if (LIST_FIRST(&mii->mii_phys) != NULL) {
			sc->sc_flags |= TULIPF_HAS_MII;
			sc->sc_tick = tlp_mii_tick;
			sc->sc_preinit = tlp_2114x_mii_preinit;
			sc->sc_mediasw = &tlp_pmac_mii_mediasw;
			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
			return;
		}
	}

	switch (sc->sc_chip) {
	case TULIP_CHIP_MX98713:
		tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
		    tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);

		/*
		 * XXX Should implement auto-sense for this someday,
		 * XXX when we do the same for the 21140.
		 */
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_10_T);
		break;

	default:
		tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
		    tlp_sia_get, tlp_sia_set, media, 2);
		tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
		    tlp_sia_get, tlp_sia_set, media + 2, 2);

		tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK | M_ZERO);
		tm->tm_name = "auto";
		tm->tm_get = tlp_2114x_nway_get;
		tm->tm_set = tlp_2114x_nway_set;
		ifmedia_add(&mii->mii_media,
		    IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0, tm);

		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
		sc->sc_statchg = tlp_2114x_nway_statchg;
		sc->sc_tick = tlp_2114x_nway_tick;
		break;
	}

	tlp_print_media(sc);
	tlp_sia_fixup(sc);

	/* Set the LED modes. */
	tlp_pmac_reset(sc);

	sc->sc_reset = tlp_pmac_reset;
}

/*
 * ADMtek AL981 media switch.  Only has internal PHY.
 */
static void	tlp_al981_tmsw_init(struct tulip_softc *);

const struct tulip_mediasw tlp_al981_mediasw = {
	tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
};

static void
tlp_al981_tmsw_init(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_al981_mii_readreg;
	mii->mii_writereg = tlp_al981_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
	    MII_OFFSET_ANY, 0);
	if (LIST_FIRST(&mii->mii_phys) == NULL) {
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
	} else {
		sc->sc_flags |= TULIPF_HAS_MII;
		sc->sc_tick = tlp_mii_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	}
}

/*
 * ADMtek AN983/985 media switch.  Only has internal PHY, but
 * on an SIO-like interface.  Unfortunately, we can't use the
 * standard SIO media switch, because the AN985 "ghosts" the
 * singly PHY at every address.
 */
static void	tlp_an985_tmsw_init(struct tulip_softc *);

const struct tulip_mediasw tlp_an985_mediasw = {
	tlp_an985_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
};

static void
tlp_an985_tmsw_init(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_bitbang_mii_readreg;
	mii->mii_writereg = tlp_bitbang_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);
	mii_attach(sc->sc_dev, mii, 0xffffffff, 1, MII_OFFSET_ANY, 0);
	if (LIST_FIRST(&mii->mii_phys) == NULL) {
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
	} else {
		sc->sc_flags |= TULIPF_HAS_MII;
		sc->sc_tick = tlp_mii_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	}
}

/*
 * Davicom DM9102 media switch.  Internal PHY and possibly HomePNA.
 */
static void	tlp_dm9102_tmsw_init(struct tulip_softc *);
static void	tlp_dm9102_tmsw_getmedia(struct tulip_softc *,
		    struct ifmediareq *);
static int	tlp_dm9102_tmsw_setmedia(struct tulip_softc *);

const struct tulip_mediasw tlp_dm9102_mediasw = {
	tlp_dm9102_tmsw_init, tlp_dm9102_tmsw_getmedia,
	    tlp_dm9102_tmsw_setmedia
};

static void
tlp_dm9102_tmsw_init(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;
	uint32_t opmode;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_bitbang_mii_readreg;
	mii->mii_writereg = tlp_bitbang_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);

	/* PHY block already reset via tlp_reset(). */

	/*
	 * Configure OPMODE properly for the internal MII interface.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_DM9102:
		opmode = OPMODE_MBO | OPMODE_HBD | OPMODE_PS;
		break;

	case TULIP_CHIP_DM9102A:
		opmode = OPMODE_MBO | OPMODE_HBD;
		break;

	default:
		opmode = 0;
		break;
	}

	TULIP_WRITE(sc, CSR_OPMODE, opmode);

	/* Now, probe the internal MII for the internal PHY. */
	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
	    MII_OFFSET_ANY, 0);

	/*
	 * XXX Figure out what to do about the HomePNA portion
	 * XXX of the DM9102A.
	 */

	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
	} else {
		sc->sc_flags |= TULIPF_HAS_MII;
		sc->sc_tick = tlp_mii_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	}
}

static void
tlp_dm9102_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
{

	/* XXX HomePNA on DM9102A. */
	tlp_mii_getmedia(sc, ifmr);
}

static int
tlp_dm9102_tmsw_setmedia(struct tulip_softc *sc)
{

	/* XXX HomePNA on DM9102A. */
	return tlp_mii_setmedia(sc);
}

/*
 * ASIX AX88140A/AX88141 media switch. Internal PHY or MII.
 */

static void	tlp_asix_tmsw_init(struct tulip_softc *);
static void	tlp_asix_tmsw_getmedia(struct tulip_softc *,
		    struct ifmediareq *);
static int	tlp_asix_tmsw_setmedia(struct tulip_softc *);

const struct tulip_mediasw tlp_asix_mediasw = {
	tlp_asix_tmsw_init, tlp_asix_tmsw_getmedia,
	tlp_asix_tmsw_setmedia
};

static void
tlp_asix_tmsw_init(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;
	uint32_t opmode;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_bitbang_mii_readreg;
	mii->mii_writereg = tlp_bitbang_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);

	/*
	 * Configure OPMODE properly for the internal MII interface.
	 */
	switch (sc->sc_chip) {
	case TULIP_CHIP_AX88140:
	case TULIP_CHIP_AX88141:
		opmode = OPMODE_HBD | OPMODE_PS;
		break;
	default:
		opmode = 0;
		break;
	}

	TULIP_WRITE(sc, CSR_OPMODE, opmode);

	/* Now, probe the internal MII for the internal PHY. */
	mii_attach(sc->sc_dev, mii, 0xffffffff, MII_PHY_ANY,
	    MII_OFFSET_ANY, 0);

	/* XXX Figure how to handle the PHY. */

	if (LIST_FIRST(&mii->mii_phys) == NULL) {
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
	} else {
		sc->sc_flags |= TULIPF_HAS_MII;
		sc->sc_tick = tlp_mii_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	}


}

static void
tlp_asix_tmsw_getmedia(struct tulip_softc *sc, struct ifmediareq *ifmr)
{

	/* XXX PHY handling. */
	tlp_mii_getmedia(sc, ifmr);
}

static int
tlp_asix_tmsw_setmedia(struct tulip_softc *sc)
{

	/* XXX PHY handling. */
	return tlp_mii_setmedia(sc);
}

/*
 * RS7112 media switch.  Handles only MII attached to the SIO.
 * We only have a PHY at 1.
 */
void   tlp_rs7112_tmsw_init(struct tulip_softc *);

const struct tulip_mediasw tlp_rs7112_mediasw = {
	tlp_rs7112_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
};

void
tlp_rs7112_tmsw_init(struct tulip_softc *sc)
{
	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
	struct mii_data * const mii = &sc->sc_mii;

	/*
	 * We don't attach any media info structures to the ifmedia
	 * entries, so if we're using a pre-init function that needs
	 * that info, override it to one that doesn't.
	 */
	if (sc->sc_preinit == tlp_2114x_preinit)
		sc->sc_preinit = tlp_2114x_mii_preinit;

	mii->mii_ifp = ifp;
	mii->mii_readreg = tlp_bitbang_mii_readreg;
	mii->mii_writereg = tlp_bitbang_mii_writereg;
	mii->mii_statchg = sc->sc_statchg;
	sc->sc_ethercom.ec_mii = mii;
	ifmedia_init(&mii->mii_media, 0, tlp_mediachange, tlp_mediastatus);

	/*
	 * The RS7112 reports a PHY at 0 (possibly HomePNA?)
	 * and 1 (ethernet). We attach ethernet only.
	 */
	mii_attach(sc->sc_dev, mii, 0xffffffff, 1, MII_OFFSET_ANY, 0);

	if (LIST_FIRST(&mii->mii_phys) == NULL) {
		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_NONE);
	} else {
		sc->sc_flags |= TULIPF_HAS_MII;
		sc->sc_tick = tlp_mii_tick;
		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
	}
}

const char *
tlp_chip_name(tulip_chip_t t) {
	if ((int)t < 0 || (int)t >= __arraycount(tlp_chip_names)) {
		static char buf[256];
		(void)snprintf(buf, sizeof(buf), "[unknown 0x%x]", t);
		return buf;
	}
	return tlp_chip_names[t];
}