Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
/* sunhme.c: Sparc HME/BigMac 10/100baseT half/full duplex auto switching,
 *           auto carrier detecting ethernet driver.  Also known as the
 *           "Happy Meal Ethernet" found on SunSwift SBUS cards.
 *
 * Copyright (C) 1996, 1998 David S. Miller (davem@caipfs.rutgers.edu)
 */

static char *version =
        "sunhme.c:v1.10 27/Jan/99 David S. Miller (davem@caipfs.rutgers.edu)\n";

#include <linux/module.h>

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/malloc.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <linux/errno.h>
#include <asm/byteorder.h>

#include <asm/idprom.h>
#include <asm/sbus.h>
#include <asm/openprom.h>
#include <asm/oplib.h>
#include <asm/auxio.h>
#include <asm/pgtable.h>
#include <asm/irq.h>
#ifndef __sparc_v9__
#include <asm/io-unit.h>
#endif
#include <asm/ethtool.h>
#include <asm/uaccess.h>

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>

#ifdef CONFIG_PCI
#include <linux/pci.h>
#include <asm/pbm.h>
#endif

#include "sunhme.h"

#ifdef MODULE
static struct happy_meal *root_happy_dev = NULL;
#endif

static struct quattro *qfe_sbus_list = NULL;
#ifdef CONFIG_PCI
static struct quattro *qfe_pci_list = NULL;
#endif

#undef HMEDEBUG
#undef SXDEBUG
#undef RXDEBUG
#undef TXDEBUG
#undef TXLOGGING

#ifdef TXLOGGING
struct hme_tx_logent {
	unsigned int tstamp;
	int tx_new, tx_old;
	unsigned int action;
#define TXLOG_ACTION_IRQ	0x01
#define TXLOG_ACTION_TXMIT	0x02
#define TXLOG_ACTION_TBUSY	0x04
#define TXLOG_ACTION_NBUFS	0x08
	unsigned int status;
};
#define TX_LOG_LEN	128
static struct hme_tx_logent tx_log[TX_LOG_LEN];
static int txlog_cur_entry = 0;
static __inline__ void tx_add_log(struct happy_meal *hp, unsigned int a, unsigned int s)
{
	struct hme_tx_logent *tlp;
	unsigned long flags;

	save_and_cli(flags);
	tlp = &tx_log[txlog_cur_entry];
	tlp->tstamp = (unsigned int)jiffies;
	tlp->tx_new = hp->tx_new;
	tlp->tx_old = hp->tx_old;
	tlp->action = a;
	tlp->status = s;
	txlog_cur_entry = (txlog_cur_entry + 1) & (TX_LOG_LEN - 1);
	restore_flags(flags);
}
static __inline__ void tx_dump_log(void)
{
	int i, this;

	this = txlog_cur_entry;
	for(i = 0; i < TX_LOG_LEN; i++) {
		printk("TXLOG[%d]: j[%08x] tx[N(%d)O(%d)] action[%08x] stat[%08x]\n", i,
		       tx_log[this].tstamp,
		       tx_log[this].tx_new, tx_log[this].tx_old,
		       tx_log[this].action, tx_log[this].status);
		this = (this + 1) & (TX_LOG_LEN - 1);
	}
}
static __inline__ void tx_dump_ring(struct happy_meal *hp)
{
	struct hmeal_init_block *hb = hp->happy_block;
	struct happy_meal_txd *tp = &hb->happy_meal_txd[0];
	int i;

	for(i = 0; i < TX_RING_SIZE; i+=4) {
		printk("TXD[%d..%d]: [%08x:%08x] [%08x:%08x] [%08x:%08x] [%08x:%08x]\n",
		       i, i + 4,
		       le32_to_cpu(tp[i].tx_flags), le32_to_cpu(tp[i].tx_addr),
		       le32_to_cpu(tp[i + 1].tx_flags), le32_to_cpu(tp[i + 1].tx_addr),
		       le32_to_cpu(tp[i + 2].tx_flags), le32_to_cpu(tp[i + 2].tx_addr),
		       le32_to_cpu(tp[i + 3].tx_flags), le32_to_cpu(tp[i + 3].tx_addr));
	}
}
#else
#define tx_add_log(hp, a, s)		do { } while(0)
#define tx_dump_log()			do { } while(0)
#define tx_dump_ring(hp)		do { } while(0)
#endif

#ifdef HMEDEBUG
#define HMD(x)  printk x
#else
#define HMD(x)
#endif

/* #define AUTO_SWITCH_DEBUG */

#ifdef AUTO_SWITCH_DEBUG
#define ASD(x)  printk x
#else
#define ASD(x)
#endif

#define DEFAULT_IPG0      16 /* For lance-mode only */
#define DEFAULT_IPG1       8 /* For all modes */
#define DEFAULT_IPG2       4 /* For all modes */
#define DEFAULT_JAMSIZE    4 /* Toe jam */

/* Oh yes, the MIF BitBang is mighty fun to program.  BitBucket is more like it. */
#define BB_PUT_BIT(hp, tregs, bit)						\
do {	hme_write32(hp, &(tregs)->bb_data, (bit));				\
	hme_write32(hp, &(tregs)->bb_clock, 0);					\
	hme_write32(hp, &(tregs)->bb_clock, 1);					\
} while(0)

#define BB_GET_BIT(hp, tregs, internal)						\
({										\
	hme_write32(hp, &(tregs)->bb_clock, 0);					\
	hme_write32(hp, &(tregs)->bb_clock, 1);					\
	if(internal)								\
		hme_read32(hp, &(tregs)->cfg) & TCV_CFG_MDIO0;			\
	else									\
		hme_read32(hp, &(tregs)->cfg) & TCV_CFG_MDIO1;			\
})

#define BB_GET_BIT2(hp, tregs, internal)					\
({										\
	int retval;								\
	hme_write32(hp, &(tregs)->bb_clock, 0);					\
	udelay(1);								\
	if(internal)								\
		retval = hme_read32(hp, &(tregs)->cfg) & TCV_CFG_MDIO0;		\
	else									\
		retval = hme_read32(hp, &(tregs)->cfg) & TCV_CFG_MDIO1;		\
	hme_write32(hp, &(tregs)->bb_clock, 1);					\
	retval;									\
})

#define TCVR_FAILURE      0x80000000     /* Impossible MIF read value */

static inline int happy_meal_bb_read(struct happy_meal *hp,
				     struct hmeal_tcvregs *tregs, int reg)
{
	volatile int unused;
	unsigned long tmp;
	int retval = 0;
	int i;

	ASD(("happy_meal_bb_read: reg=%d ", reg));

	/* Enable the MIF BitBang outputs. */
	hme_write32(hp, &tregs->bb_oenab, 1);

	/* Force BitBang into the idle state. */
	for(i = 0; i < 32; i++)
		BB_PUT_BIT(hp, tregs, 1);

	/* Give it the read sequence. */
	BB_PUT_BIT(hp, tregs, 0);
	BB_PUT_BIT(hp, tregs, 1);
	BB_PUT_BIT(hp, tregs, 1);
	BB_PUT_BIT(hp, tregs, 0);

	/* Give it the PHY address. */
	tmp = hp->paddr & 0xff;
	for(i = 4; i >= 0; i--)
		BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));

	/* Tell it what register we want to read. */
	tmp = (reg & 0xff);
	for(i = 4; i >= 0; i--)
		BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));

	/* Close down the MIF BitBang outputs. */
	hme_write32(hp, &tregs->bb_oenab, 0);

	/* Now read in the value. */
	unused = BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
	for(i = 15; i >= 0; i--)
		retval |= BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
	unused = BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
	unused = BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
	unused = BB_GET_BIT2(hp, tregs, (hp->tcvr_type == internal));
	ASD(("value=%x\n", retval));
	return retval;
}

static inline void happy_meal_bb_write(struct happy_meal *hp,
				       struct hmeal_tcvregs *tregs, int reg,
				       unsigned short value)
{
	unsigned long tmp;
	int i;

	ASD(("happy_meal_bb_write: reg=%d value=%x\n", reg, value));

	/* Enable the MIF BitBang outputs. */
	hme_write32(hp, &tregs->bb_oenab, 1);

	/* Force BitBang into the idle state. */
	for(i = 0; i < 32; i++)
		BB_PUT_BIT(hp, tregs, 1);

	/* Give it write sequence. */
	BB_PUT_BIT(hp, tregs, 0);
	BB_PUT_BIT(hp, tregs, 1);
	BB_PUT_BIT(hp, tregs, 0);
	BB_PUT_BIT(hp, tregs, 1);

	/* Give it the PHY address. */
	tmp = (hp->paddr & 0xff);
	for(i = 4; i >= 0; i--)
		BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));

	/* Tell it what register we will be writing. */
	tmp = (reg & 0xff);
	for(i = 4; i >= 0; i--)
		BB_PUT_BIT(hp, tregs, ((tmp >> i) & 1));

	/* Tell it to become ready for the bits. */
	BB_PUT_BIT(hp, tregs, 1);
	BB_PUT_BIT(hp, tregs, 0);

	for(i = 15; i >= 0; i--)
		BB_PUT_BIT(hp, tregs, ((value >> i) & 1));

	/* Close down the MIF BitBang outputs. */
	hme_write32(hp, &tregs->bb_oenab, 0);
}

#define TCVR_READ_TRIES   16

static inline int happy_meal_tcvr_read(struct happy_meal *hp,
				       struct hmeal_tcvregs *tregs, int reg)
{
	int tries = TCVR_READ_TRIES;
	int retval;

	ASD(("happy_meal_tcvr_read: reg=0x%02x ", reg));
	if(hp->tcvr_type == none) {
		ASD(("no transceiver, value=TCVR_FAILURE\n"));
		return TCVR_FAILURE;
	}

	if(!(hp->happy_flags & HFLAG_FENABLE)) {
		ASD(("doing bit bang\n"));
		return happy_meal_bb_read(hp, tregs, reg);
	}

	hme_write32(hp, &tregs->frame,
		    (FRAME_READ | (hp->paddr << 23) | ((reg & 0xff) << 18)));
	while(!(hme_read32(hp, &tregs->frame) & 0x10000) && --tries)
		udelay(20);
	if(!tries) {
		printk(KERN_ERR "happy meal: Aieee, transceiver MIF read bolixed\n");
		return TCVR_FAILURE;
	}
	retval = hme_read32(hp, &tregs->frame) & 0xffff;
	ASD(("value=%04x\n", retval));
	return retval;
}

#define TCVR_WRITE_TRIES  16

static inline void happy_meal_tcvr_write(struct happy_meal *hp,
					 struct hmeal_tcvregs *tregs, int reg,
					 unsigned short value)
{
	int tries = TCVR_WRITE_TRIES;
	
	ASD(("happy_meal_tcvr_write: reg=0x%02x value=%04x\n", reg, value));

	/* Welcome to Sun Microsystems, can I take your order please? */
	if(!hp->happy_flags & HFLAG_FENABLE)
		return happy_meal_bb_write(hp, tregs, reg, value);

	/* Would you like fries with that? */
	hme_write32(hp, &tregs->frame,
		    (FRAME_WRITE | (hp->paddr << 23) |
		     ((reg & 0xff) << 18) | (value & 0xffff)));
	while(!(hme_read32(hp, &tregs->frame) & 0x10000) && --tries)
		udelay(20);

	/* Anything else? */
	if(!tries)
		printk(KERN_ERR "happy meal: Aieee, transceiver MIF write bolixed\n");

	/* Fifty-two cents is your change, have a nice day. */
}

/* Auto negotiation.  The scheme is very simple.  We have a timer routine
 * that keeps watching the auto negotiation process as it progresses.
 * The DP83840 is first told to start doing it's thing, we set up the time
 * and place the timer state machine in it's initial state.
 *
 * Here the timer peeks at the DP83840 status registers at each click to see
 * if the auto negotiation has completed, we assume here that the DP83840 PHY
 * will time out at some point and just tell us what (didn't) happen.  For
 * complete coverage we only allow so many of the ticks at this level to run,
 * when this has expired we print a warning message and try another strategy.
 * This "other" strategy is to force the interface into various speed/duplex
 * configurations and we stop when we see a link-up condition before the
 * maximum number of "peek" ticks have occurred.
 *
 * Once a valid link status has been detected we configure the BigMAC and
 * the rest of the Happy Meal to speak the most efficient protocol we could
 * get a clean link for.  The priority for link configurations, highest first
 * is:
 *                 100 Base-T Full Duplex
 *                 100 Base-T Half Duplex
 *                 10 Base-T Full Duplex
 *                 10 Base-T Half Duplex
 *
 * We start a new timer now, after a successful auto negotiation status has
 * been detected.  This timer just waits for the link-up bit to get set in
 * the BMCR of the DP83840.  When this occurs we print a kernel log message
 * describing the link type in use and the fact that it is up.
 *
 * If a fatal error of some sort is signalled and detected in the interrupt
 * service routine, and the chip is reset, or the link is ifconfig'd down
 * and then back up, this entire process repeats itself all over again.
 */
static int try_next_permutation(struct happy_meal *hp, struct hmeal_tcvregs *tregs)
{
	hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);

	/* Downgrade from full to half duplex.  Only possible
	 * via ethtool.
	 */
	if(hp->sw_bmcr & BMCR_FULLDPLX) {
		hp->sw_bmcr &= ~(BMCR_FULLDPLX);
		happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);
		return 0;
	}

	/* Downgrade from 100 to 10. */
	if(hp->sw_bmcr & BMCR_SPEED100) {
		hp->sw_bmcr &= ~(BMCR_SPEED100);
		happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);
		return 0;
	}

	/* We've tried everything. */
	return -1;
}

static void display_link_mode(struct happy_meal *hp, struct hmeal_tcvregs *tregs)
{
	printk(KERN_INFO "%s: Link is up using ", hp->dev->name);
	if(hp->tcvr_type == external)
		printk("external ");
	else
		printk("internal ");
	printk("transceiver at ");
	hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, DP83840_LPA);
	if(hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) {
		if(hp->sw_lpa & LPA_100FULL)
			printk("100Mb/s, Full Duplex.\n");
		else
			printk("100Mb/s, Half Duplex.\n");
	} else {
		if(hp->sw_lpa & LPA_10FULL)
			printk("10Mb/s, Full Duplex.\n");
		else
			printk("10Mb/s, Half Duplex.\n");
	}
}

static void display_forced_link_mode(struct happy_meal *hp, struct hmeal_tcvregs *tregs)
{
	printk(KERN_INFO "%s: Link has been forced up using ", hp->dev->name);
	if(hp->tcvr_type == external)
		printk("external ");
	else
		printk("internal ");
	printk("transceiver at ");
	hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
	if(hp->sw_bmcr & BMCR_SPEED100)
		printk("100Mb/s, ");
	else
		printk("10Mb/s, ");
	if(hp->sw_bmcr & BMCR_FULLDPLX)
		printk("Full Duplex.\n");
	else
		printk("Half Duplex.\n");
}

static int set_happy_link_modes(struct happy_meal *hp, struct hmeal_tcvregs *tregs)
{
	int full;

	/* All we care about is making sure the bigmac tx_cfg has a
	 * proper duplex setting.
	 */
	if(hp->timer_state == arbwait) {
		hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, DP83840_LPA);
		if(!(hp->sw_lpa & (LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL)))
			goto no_response;
		if(hp->sw_lpa & LPA_100FULL)
			full = 1;
		else if(hp->sw_lpa & LPA_100HALF)
			full = 0;
		else if(hp->sw_lpa & LPA_10FULL)
			full = 1;
		else
			full = 0;
	} else {
		/* Forcing a link mode. */
		hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
		if(hp->sw_bmcr & BMCR_FULLDPLX)
			full = 1;
		else
			full = 0;
	}

	/* Before changing other bits in the tx_cfg register, and in
	 * general any of other the TX config registers too, you
	 * must:
	 * 1) Clear Enable
	 * 2) Poll with reads until that bit reads back as zero
	 * 3) Make TX configuration changes
	 * 4) Set Enable once more
	 */
	hme_write32(hp, &hp->bigmacregs->tx_cfg,
		    hme_read32(hp, &hp->bigmacregs->tx_cfg) &
		    ~(BIGMAC_TXCFG_ENABLE));
	while(hme_read32(hp, &hp->bigmacregs->tx_cfg) & BIGMAC_TXCFG_ENABLE)
		barrier();
	if(full) {
		hp->happy_flags |= HFLAG_FULL;
		hme_write32(hp, &hp->bigmacregs->tx_cfg,
			    hme_read32(hp, &hp->bigmacregs->tx_cfg) |
			    BIGMAC_TXCFG_FULLDPLX);
	} else {
		hp->happy_flags &= ~(HFLAG_FULL);
		hme_write32(hp, &hp->bigmacregs->tx_cfg,
			    hme_read32(hp, &hp->bigmacregs->tx_cfg) &
			    ~(BIGMAC_TXCFG_FULLDPLX));
	}
	hme_write32(hp, &hp->bigmacregs->tx_cfg,
		    hme_read32(hp, &hp->bigmacregs->tx_cfg) |
		    BIGMAC_TXCFG_ENABLE);
	return 0;
no_response:
	return 1;
}

static int happy_meal_init(struct happy_meal *hp, int from_irq);

static int is_lucent_phy(struct happy_meal *hp)
{
	struct hmeal_tcvregs *tregs = hp->tcvregs;
	unsigned short mr2, mr3;
	int ret = 0;

	mr2 = happy_meal_tcvr_read(hp, tregs, 2);
	mr3 = happy_meal_tcvr_read(hp, tregs, 3);
	if ((mr2 & 0xffff) == 0x0180 &&
	    ((mr3 & 0xffff) >> 10) == 0x1d) {
#if 0
		printk("HMEDEBUG: Lucent PHY detected.\n");
#endif
		ret = 1;
	}

	return ret;
}

static void happy_meal_timer(unsigned long data)
{
	struct happy_meal *hp = (struct happy_meal *) data;
	struct hmeal_tcvregs *tregs = hp->tcvregs;
	int restart_timer = 0;

	hp->timer_ticks++;
	switch(hp->timer_state) {
	case arbwait:
		/* Only allow for 5 ticks, thats 10 seconds and much too
		 * long to wait for arbitration to complete.
		 */
		if(hp->timer_ticks >= 10) {
			/* Enter force mode. */
	do_force_mode:
			hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
			printk(KERN_NOTICE "%s: Auto-Negotiation unsuccessful, trying force link mode\n",
			       hp->dev->name);
			hp->sw_bmcr = BMCR_SPEED100;
			happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);

			if (!is_lucent_phy(hp)) {
				/* OK, seems we need do disable the transceiver for
				 * the first tick to make sure we get an accurate
				 * link state at the second tick.
				 */
				hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
								       DP83840_CSCONFIG);
				hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
				happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
						      hp->sw_csconfig);
			}
			hp->timer_state = ltrywait;
			hp->timer_ticks = 0;
			restart_timer = 1;
		} else {
			/* Anything interesting happen? */
			hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, DP83840_BMSR);
			if(hp->sw_bmsr & BMSR_ANEGCOMPLETE) {
				int ret;

				/* Just what we've been waiting for... */
				ret = set_happy_link_modes(hp, tregs);
				if(ret) {
					/* Ooops, something bad happened, go to force
					 * mode.
					 *
					 * XXX Broken hubs which don't support 802.3u
					 * XXX auto-negotiation make this happen as well.
					 */
					goto do_force_mode;
				}

				/* Success, at least so far, advance our state engine. */
				hp->timer_state = lupwait;
				restart_timer = 1;
			} else {
				restart_timer = 1;
			}
		}
		break;

	case lupwait:
		/* Auto negotiation was successful and we are awaiting a
		 * link up status.  I have decided to let this timer run
		 * forever until some sort of error is signalled, reporting
		 * a message to the user at 10 second intervals.
		 */
		hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, DP83840_BMSR);
		if(hp->sw_bmsr & BMSR_LSTATUS) {
			/* Wheee, it's up, display the link mode in use and put
			 * the timer to sleep.
			 */
			display_link_mode(hp, tregs);
			hp->timer_state = asleep;
			restart_timer = 0;
		} else {
			if(hp->timer_ticks >= 10) {
				printk(KERN_NOTICE "%s: Auto negotiation successful, link still "
				       "not completely up.\n", hp->dev->name);
				hp->timer_ticks = 0;
				restart_timer = 1;
			} else {
				restart_timer = 1;
			}
		}
		break;

	case ltrywait:
		/* Making the timeout here too long can make it take
		 * annoyingly long to attempt all of the link mode
		 * permutations, but then again this is essentially
		 * error recovery code for the most part.
		 */
		hp->sw_bmsr = happy_meal_tcvr_read(hp, tregs, DP83840_BMSR);
		hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
		if(hp->timer_ticks == 1) {
			/* Re-enable transceiver, we'll re-enable the transceiver next
			 * tick, then check link state on the following tick.
			 * XXX But dont do this on Lucent PHY -DaveM
			 */
			if (!is_lucent_phy(hp)) {
				hp->sw_csconfig |= CSCONFIG_TCVDISAB;
				happy_meal_tcvr_write(hp, tregs,
						      DP83840_CSCONFIG, hp->sw_csconfig);
			}
			restart_timer = 1;
			break;
		}
		if(hp->timer_ticks == 2) {
			/* XXX See above about Lucent PHY -DaveM */
			if (!is_lucent_phy(hp)) {
				hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
				happy_meal_tcvr_write(hp, tregs,
						      DP83840_CSCONFIG, hp->sw_csconfig);
			}
			restart_timer = 1;
			break;
		}
		if(hp->sw_bmsr & BMSR_LSTATUS) {
			/* Force mode selection success. */
			display_forced_link_mode(hp, tregs);
			set_happy_link_modes(hp, tregs); /* XXX error? then what? */
			hp->timer_state = asleep;
			restart_timer = 0;
		} else {
			if(hp->timer_ticks >= 4) { /* 6 seconds or so... */
				int ret;

				ret = try_next_permutation(hp, tregs);
				if(ret == -1) {
					/* Aieee, tried them all, reset the
					 * chip and try all over again.
					 */

					/* Let the user know... */
					printk(KERN_NOTICE "%s: Link down, cable problem?\n",
					       hp->dev->name);

					ret = happy_meal_init(hp, 0);
					if(ret) {
						/* ho hum... */
						printk(KERN_ERR "%s: Error, cannot re-init the "
						       "Happy Meal.\n", hp->dev->name);
					}
					return;
				}
				hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
								       DP83840_CSCONFIG);
				if (!is_lucent_phy(hp)) {
					hp->sw_csconfig |= CSCONFIG_TCVDISAB;
					happy_meal_tcvr_write(hp, tregs,
							      DP83840_CSCONFIG,
							      hp->sw_csconfig);
				}
				hp->timer_ticks = 0;
				restart_timer = 1;
			} else {
				restart_timer = 1;
			}
		}
		break;

	case asleep:
	default:
		/* Can't happens.... */
		printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
		       hp->dev->name);
		restart_timer = 0;
		hp->timer_ticks = 0;
		hp->timer_state = asleep; /* foo on you */
		break;
	};

	if(restart_timer) {
		hp->happy_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
		add_timer(&hp->happy_timer);
	}
}

#define TX_RESET_TRIES     32
#define RX_RESET_TRIES     32

static inline void happy_meal_tx_reset(struct happy_meal *hp,
				       struct hmeal_bigmacregs *bregs)
{
	int tries = TX_RESET_TRIES;

	HMD(("happy_meal_tx_reset: reset, "));

	/* Would you like to try our SMCC Delux? */
	hme_write32(hp, &bregs->tx_swreset, 0);
	while((hme_read32(hp, &bregs->tx_swreset) & 1) && --tries)
		udelay(20);

	/* Lettuce, tomato, buggy hardware (no extra charge)? */
	if(!tries)
		printk(KERN_ERR "happy meal: Transceiver BigMac ATTACK!");

	/* Take care. */
	HMD(("done\n"));
}

static inline void happy_meal_rx_reset(struct happy_meal *hp,
				       struct hmeal_bigmacregs *bregs)
{
	int tries = RX_RESET_TRIES;

	HMD(("happy_meal_rx_reset: reset, "));

	/* We have a special on GNU/Viking hardware bugs today. */
	hme_write32(hp, &bregs->rx_swreset, 0);
	while((hme_read32(hp, &bregs->rx_swreset) & 1) && --tries)
		udelay(20);

	/* Will that be all? */
	if(!tries)
		printk(KERN_ERR "happy meal: Receiver BigMac ATTACK!");

	/* Don't forget your vik_1137125_wa.  Have a nice day. */
	HMD(("done\n"));
}

#define STOP_TRIES         16

static inline void happy_meal_stop(struct happy_meal *hp,
				   struct hmeal_gregs *gregs)
{
	int tries = STOP_TRIES;

	HMD(("happy_meal_stop: reset, "));

	/* We're consolidating our STB products, it's your lucky day. */
	hme_write32(hp, &gregs->sw_reset, GREG_RESET_ALL);
	while(hme_read32(hp, &gregs->sw_reset) && --tries)
		udelay(20);

	/* Come back next week when we are "Sun Microelectronics". */
	if(!tries)
		printk(KERN_ERR "happy meal: Fry guys.");

	/* Remember: "Different name, same old buggy as shit hardware." */
	HMD(("done\n"));
}

static void happy_meal_get_counters(struct happy_meal *hp,
				    struct hmeal_bigmacregs *bregs)
{
	struct net_device_stats *stats = &hp->net_stats;

	stats->rx_crc_errors += hme_read32(hp, &bregs->rcrce_ctr);
	hme_write32(hp, &bregs->rcrce_ctr, 0);

	stats->rx_frame_errors += hme_read32(hp, &bregs->unale_ctr);
	hme_write32(hp, &bregs->unale_ctr, 0);

	stats->rx_length_errors += hme_read32(hp, &bregs->gle_ctr);
	hme_write32(hp, &bregs->gle_ctr, 0);

	stats->tx_aborted_errors += hme_read32(hp, &bregs->ex_ctr);

	stats->collisions +=
		(hme_read32(hp, &bregs->ex_ctr) +
		 hme_read32(hp, &bregs->lt_ctr));
	hme_write32(hp, &bregs->ex_ctr, 0);
	hme_write32(hp, &bregs->lt_ctr, 0);
}

static inline void happy_meal_poll_start(struct happy_meal *hp,
					 struct hmeal_tcvregs *tregs)
{
	unsigned long tmp;
	int speed;

	ASD(("happy_meal_poll_start: "));
	if(!(hp->happy_flags & HFLAG_POLLENABLE)) {
		HMD(("polling disabled, return\n"));
		return;
	}

	/* Start the MIF polling on the external transceiver. */
	ASD(("polling on, "));
	tmp = hme_read32(hp, &tregs->cfg);
	tmp &= ~(TCV_CFG_PDADDR | TCV_CFG_PREGADDR);
	tmp |= ((hp->paddr & 0x1f) << 10);
	tmp |= (TCV_PADDR_ETX << 3);
	tmp |= TCV_CFG_PENABLE;
	hme_write32(hp, &tregs->cfg, tmp);

	/* Let the bits set. */
	udelay(200);

	/* We are polling now. */
	ASD(("now polling, "));
	hp->happy_flags |= HFLAG_POLL;

	/* Clear the poll flags, get the basic status as of now. */
	hp->poll_flag = 0;
	hp->poll_data = tregs->status >> 16;

	if(hp->happy_flags & HFLAG_AUTO)
		speed = hp->auto_speed;
	else
		speed = hp->forced_speed;

	/* Listen only for the MIF interrupts we want to hear. */
	ASD(("mif ints on, "));
	if(speed == 100)
		hme_write32(hp, &tregs->int_mask, 0xfffb);
	else
		hme_write32(hp, &tregs->int_mask, 0xfff9);
	ASD(("done\n"));
}

static inline void happy_meal_poll_stop(struct happy_meal *hp,
					struct hmeal_tcvregs *tregs)
{
	ASD(("happy_meal_poll_stop: "));

	/* If polling disabled or not polling already, nothing to do. */
	if((hp->happy_flags & (HFLAG_POLLENABLE | HFLAG_POLL)) !=
	   (HFLAG_POLLENABLE | HFLAG_POLL)) {
		HMD(("not polling, return\n"));
		return;
	}

	/* Shut up the MIF. */
	ASD(("were polling, mif ints off, "));
	hme_write32(hp, &tregs->int_mask, 0xffff);

	/* Turn off polling. */
	ASD(("polling off, "));
	hme_write32(hp, &tregs->cfg,
		    hme_read32(hp, &tregs->cfg) & ~(TCV_CFG_PENABLE));

	/* We are no longer polling. */
	hp->happy_flags &= ~(HFLAG_POLL);

	/* Let the bits set. */
	udelay(200);
	ASD(("done\n"));
}

/* Only Sun can take such nice parts and fuck up the programming interface
 * like this.  Good job guys...
 */
#define TCVR_RESET_TRIES       16 /* It should reset quickly        */
#define TCVR_UNISOLATE_TRIES   32 /* Dis-isolation can take longer. */

static int happy_meal_tcvr_reset(struct happy_meal *hp,
				 struct hmeal_tcvregs *tregs)
{
	unsigned long tconfig;
	int result, tries = TCVR_RESET_TRIES;

	tconfig = hme_read32(hp, &tregs->cfg);
	ASD(("happy_meal_tcvr_reset: tcfg<%08lx> ", tconfig));
	if(hp->tcvr_type == external) {
		ASD(("external<"));
		hme_write32(hp, &tregs->cfg, tconfig & ~(TCV_CFG_PSELECT));
		hp->tcvr_type = internal;
		hp->paddr = TCV_PADDR_ITX;
		ASD(("ISOLATE,"));
		happy_meal_tcvr_write(hp, tregs, DP83840_BMCR,
				      (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
		result = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
		if(result == TCVR_FAILURE) {
			ASD(("phyread_fail>\n"));
			return -1;
		}
		ASD(("phyread_ok,PSELECT>"));
		hme_write32(hp, &tregs->cfg, tconfig | TCV_CFG_PSELECT);
		hp->tcvr_type = external;
		hp->paddr = TCV_PADDR_ETX;
	} else {
		if(tconfig & TCV_CFG_MDIO1) {
			ASD(("internal<PSELECT,"));
			hme_write32(hp, &tregs->cfg, (tconfig | TCV_CFG_PSELECT));
			ASD(("ISOLATE,"));
			happy_meal_tcvr_write(hp, tregs, DP83840_BMCR,
					      (BMCR_LOOPBACK|BMCR_PDOWN|BMCR_ISOLATE));
			result = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
			if(result == TCVR_FAILURE) {
				ASD(("phyread_fail>\n"));
				return -1;
			}
			ASD(("phyread_ok,~PSELECT>"));
			hme_write32(hp, &tregs->cfg, (tconfig & ~(TCV_CFG_PSELECT)));
			hp->tcvr_type = internal;
			hp->paddr = TCV_PADDR_ITX;
		}
	}

	ASD(("BMCR_RESET "));
	happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, BMCR_RESET);

	while(--tries) {
		result = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
		if(result == TCVR_FAILURE)
			return -1;
		hp->sw_bmcr = result;
		if(!(result & BMCR_RESET))
			break;
		udelay(20);
	}
	if(!tries) {
		ASD(("BMCR RESET FAILED!\n"));
		return -1;
	}
	ASD(("RESET_OK\n"));

	/* Get fresh copies of the PHY registers. */
	hp->sw_bmsr      = happy_meal_tcvr_read(hp, tregs, DP83840_BMSR);
	hp->sw_physid1   = happy_meal_tcvr_read(hp, tregs, DP83840_PHYSID1);
	hp->sw_physid2   = happy_meal_tcvr_read(hp, tregs, DP83840_PHYSID2);
	hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, DP83840_ADVERTISE);

	ASD(("UNISOLATE"));
	hp->sw_bmcr &= ~(BMCR_ISOLATE);
	happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);

	tries = TCVR_UNISOLATE_TRIES;
	while(--tries) {
		result = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
		if(result == TCVR_FAILURE)
			return -1;
		if(!(result & BMCR_ISOLATE))
			break;
		udelay(20);
	}
	if(!tries) {
		ASD((" FAILED!\n"));
		return -1;
	}
	ASD((" SUCCESS and CSCONFIG_DFBYPASS\n"));
	if (!is_lucent_phy(hp)) {
		result = happy_meal_tcvr_read(hp, tregs, DP83840_CSCONFIG);
		happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
				      (result | CSCONFIG_DFBYPASS));
	}
	return 0;
}

/* Figure out whether we have an internal or external transceiver. */
static void happy_meal_transceiver_check(struct happy_meal *hp,
					 struct hmeal_tcvregs *tregs)
{
	unsigned long tconfig = hme_read32(hp, &tregs->cfg);

	ASD(("happy_meal_transceiver_check: tcfg=%08lx ", tconfig));
	if(hp->happy_flags & HFLAG_POLL) {
		/* If we are polling, we must stop to get the transceiver type. */
		ASD(("<polling> "));
		if(hp->tcvr_type == internal) {
			if(tconfig & TCV_CFG_MDIO1) {
				ASD(("<internal> <poll stop> "));
				happy_meal_poll_stop(hp, tregs);
				hp->paddr = TCV_PADDR_ETX;
				hp->tcvr_type = external;
				ASD(("<external>\n"));
				tconfig &= ~(TCV_CFG_PENABLE);
				tconfig |= TCV_CFG_PSELECT;
				hme_write32(hp, &tregs->cfg, tconfig);
			}
		} else {
			if(hp->tcvr_type == external) {
				ASD(("<external> "));
				if(!(hme_read32(hp, &tregs->status) >> 16)) {
					ASD(("<poll stop> "));
					happy_meal_poll_stop(hp, tregs);
					hp->paddr = TCV_PADDR_ITX;
					hp->tcvr_type = internal;
					ASD(("<internal>\n"));
					hme_write32(hp, &tregs->cfg,
						    hme_read32(hp, &tregs->cfg) &
						    ~(TCV_CFG_PSELECT));
				}
				ASD(("\n"));
			} else {
				ASD(("<none>\n"));
			}
		}
	} else {
		unsigned long reread = hme_read32(hp, &tregs->cfg);

		/* Else we can just work off of the MDIO bits. */
		ASD(("<not polling> "));
		if(reread & TCV_CFG_MDIO1) {
			hme_write32(hp, &tregs->cfg, tconfig | TCV_CFG_PSELECT);
			hp->paddr = TCV_PADDR_ETX;
			hp->tcvr_type = external;
			ASD(("<external>\n"));
		} else {
			if(reread & TCV_CFG_MDIO0) {
				hme_write32(hp, &tregs->cfg,
					    tconfig & ~(TCV_CFG_PSELECT));
				hp->paddr = TCV_PADDR_ITX;
				hp->tcvr_type = internal;
				ASD(("<internal>\n"));
			} else {
				printk(KERN_ERR "happy meal: Transceiver and a coke please.");
				hp->tcvr_type = none; /* Grrr... */
				ASD(("<none>\n"));
			}
		}
	}
}

/* The receive ring buffers are a bit tricky to get right.  Here goes...
 *
 * The buffers we dma into must be 64 byte aligned.  So we use a special
 * alloc_skb() routine for the happy meal to allocate 64 bytes more than
 * we really need.
 *
 * We use skb_reserve() to align the data block we get in the skb.  We
 * also program the etxregs->cfg register to use an offset of 2.  This
 * imperical constant plus the ethernet header size will always leave
 * us with a nicely aligned ip header once we pass things up to the
 * protocol layers.
 *
 * The numbers work out to:
 *
 *         Max ethernet frame size         1518
 *         Ethernet header size              14
 *         Happy Meal base offset             2
 *
 * Say a skb data area is at 0xf001b010, and its size alloced is
 * (ETH_FRAME_LEN + 64 + 2) = (1514 + 64 + 2) = 1580 bytes.
 *
 * First our alloc_skb() routine aligns the data base to a 64 byte
 * boundry.  We now have 0xf001b040 as our skb data address.  We
 * plug this into the receive descriptor address.
 *
 * Next, we skb_reserve() 2 bytes to account for the Happy Meal offset.
 * So now the data we will end up looking at starts at 0xf001b042.  When
 * the packet arrives, we will check out the size received and subtract
 * this from the skb->length.  Then we just pass the packet up to the
 * protocols as is, and allocate a new skb to replace this slot we have
 * just received from.
 *
 * The ethernet layer will strip the ether header from the front of the
 * skb we just sent to it, this leaves us with the ip header sitting
 * nicely aligned at 0xf001b050.  Also, for tcp and udp packets the
 * Happy Meal has even checksummed the tcp/udp data for us.  The 16
 * bit checksum is obtained from the low bits of the receive descriptor
 * flags, thus:
 *
 * 	skb->csum = rxd->rx_flags & 0xffff;
 * 	skb->ip_summed = CHECKSUM_HW;
 *
 * before sending off the skb to the protocols, and we are good as gold.
 */
static inline void happy_meal_clean_rings(struct happy_meal *hp)
{
	int i;

	for(i = 0; i < RX_RING_SIZE; i++) {
		if(hp->rx_skbs[i] != NULL) {
			dev_kfree_skb(hp->rx_skbs[i]);
			hp->rx_skbs[i] = NULL;
		}
	}

	for(i = 0; i < TX_RING_SIZE; i++) {
		if(hp->tx_skbs[i] != NULL) {
			dev_kfree_skb(hp->tx_skbs[i]);
			hp->tx_skbs[i] = NULL;
		}
	}
}

static void happy_meal_init_rings(struct happy_meal *hp, int from_irq)
{
	struct hmeal_init_block *hb = hp->happy_block;
	struct device *dev = hp->dev;
	int i, gfp_flags = GFP_KERNEL;

	if(from_irq || in_interrupt())
		gfp_flags = GFP_ATOMIC;

	HMD(("happy_meal_init_rings: counters to zero, "));
	hp->rx_new = hp->rx_old = hp->tx_new = hp->tx_old = 0;

	/* Free any skippy bufs left around in the rings. */
	HMD(("clean, "));
	happy_meal_clean_rings(hp);

	/* Now get new skippy bufs for the receive ring. */
	HMD(("init rxring, "));
	for(i = 0; i < RX_RING_SIZE; i++) {
		struct sk_buff *skb;

		skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE, gfp_flags | GFP_DMA);
		if(!skb)
			continue;
		hp->rx_skbs[i] = skb;
		skb->dev = dev;

		/* Because we reserve afterwards. */
		skb_put(skb, (ETH_FRAME_LEN + RX_OFFSET));

#ifdef CONFIG_PCI
		if(hp->happy_flags & HFLAG_PCI) {
			pcihme_write_rxd(&hb->happy_meal_rxd[i],
					 (RXFLAG_OWN |
					  ((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
					 (u32)virt_to_bus((volatile void *)skb->data));
		} else
#endif
#ifndef __sparc_v9__
		if (sparc_cpu_model == sun4d) {
			__u32 va = (__u32)hp->sun4d_buffers + i * PAGE_SIZE;

			hb->happy_meal_rxd[i].rx_addr =
				iounit_map_dma_page(va, skb->data, hp->happy_sbus_dev->my_bus);
			hb->happy_meal_rxd[i].rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));
		} else
#endif
		{
			hb->happy_meal_rxd[i].rx_addr = sbus_dvma_addr(skb->data);
			hb->happy_meal_rxd[i].rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));
		}
		skb_reserve(skb, RX_OFFSET);
	}

	HMD(("init txring, "));
	for(i = 0; i < TX_RING_SIZE; i++)
		hb->happy_meal_txd[i].tx_flags = 0;
	HMD(("done\n"));
}

#ifndef __sparc_v9__
static void sun4c_happy_meal_init_rings(struct happy_meal *hp)
{
	struct hmeal_init_block *hb = hp->happy_block;
	__u32 hbufs = hp->s4c_buf_dvma;
	int i;

	HMD(("happy_meal_init_rings: counters to zero, "));
	hp->rx_new = hp->rx_old = hp->tx_new = hp->tx_old = 0;

	HMD(("init rxring, "));
	for(i = 0; i < RX_RING_SIZE; i++) {
		hb->happy_meal_rxd[i].rx_addr = hbufs + hbuf_offset(rx_buf, i);
		hb->happy_meal_rxd[i].rx_flags =
			(RXFLAG_OWN | ((SUN4C_RX_BUFF_SIZE - RX_OFFSET) << 16));
	}

	HMD(("init txring, "));
	for(i = 0; i < TX_RING_SIZE; i++)
		hb->happy_meal_txd[i].tx_flags = 0;
	HMD(("done\n"));
}
#endif

static void happy_meal_begin_auto_negotiation(struct happy_meal *hp,
					      struct hmeal_tcvregs *tregs,
					      struct ethtool_cmd *ep)
{
	int timeout;

	/* Read all of the registers we are interested in now. */
	hp->sw_bmsr      = happy_meal_tcvr_read(hp, tregs, DP83840_BMSR);
	hp->sw_bmcr      = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
	hp->sw_physid1   = happy_meal_tcvr_read(hp, tregs, DP83840_PHYSID1);
	hp->sw_physid2   = happy_meal_tcvr_read(hp, tregs, DP83840_PHYSID2);

	/* XXX Check BMSR_ANEGCAPABLE, should not be necessary though. */

	hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, DP83840_ADVERTISE);
	if(ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
		/* Advertise everything we can support. */
		if(hp->sw_bmsr & BMSR_10HALF)
			hp->sw_advertise |= (ADVERTISE_10HALF);
		else
			hp->sw_advertise &= ~(ADVERTISE_10HALF);

		if(hp->sw_bmsr & BMSR_10FULL)
			hp->sw_advertise |= (ADVERTISE_10FULL);
		else
			hp->sw_advertise &= ~(ADVERTISE_10FULL);
		if(hp->sw_bmsr & BMSR_100HALF)
			hp->sw_advertise |= (ADVERTISE_100HALF);
		else
			hp->sw_advertise &= ~(ADVERTISE_100HALF);
		if(hp->sw_bmsr & BMSR_100FULL)
			hp->sw_advertise |= (ADVERTISE_100FULL);
		else
			hp->sw_advertise &= ~(ADVERTISE_100FULL);
		happy_meal_tcvr_write(hp, tregs, DP83840_ADVERTISE, hp->sw_advertise);

		/* XXX Currently no Happy Meal cards I know off support 100BaseT4,
		 * XXX and this is because the DP83840 does not support it, changes
		 * XXX would need to be made to the tx/rx logic in the driver as well
		 * XXX so I completely skip checking for it in the BMSR for now.
		 */

#ifdef AUTO_SWITCH_DEBUG
		ASD(("%s: Advertising [ ", hp->dev->name));
		if(hp->sw_advertise & ADVERTISE_10HALF)
			ASD(("10H "));
		if(hp->sw_advertise & ADVERTISE_10FULL)
			ASD(("10F "));
		if(hp->sw_advertise & ADVERTISE_100HALF)
			ASD(("100H "));
		if(hp->sw_advertise & ADVERTISE_100FULL)
			ASD(("100F "));
#endif

		/* Enable Auto-Negotiation, this is usually on already... */
		hp->sw_bmcr |= BMCR_ANENABLE;
		happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);

		/* Restart it to make sure it is going. */
		hp->sw_bmcr |= BMCR_ANRESTART;
		happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);

		/* BMCR_ANRESTART self clears when the process has begun. */

		timeout = 64;  /* More than enough. */
		while(--timeout) {
			hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
			if(!(hp->sw_bmcr & BMCR_ANRESTART))
				break; /* got it. */
			udelay(10);
		}
		if(!timeout) {
			printk(KERN_ERR "%s: Happy Meal would not start auto negotiation "
			       "BMCR=0x%04x\n", hp->dev->name, hp->sw_bmcr);
			printk(KERN_NOTICE "%s: Performing force link detection.\n",
			       hp->dev->name);
			goto force_link;
		} else {
			hp->timer_state = arbwait;
		}
	} else {
force_link:
		/* Force the link up, trying first a particular mode.
		 * Either we are here at the request of ethtool or
		 * because the Happy Meal would not start to autoneg.
		 */

		/* Disable auto-negotiation in BMCR, enable the duplex and
		 * speed setting, init the timer state machine, and fire it off.
		 */
		if(ep == NULL || ep->autoneg == AUTONEG_ENABLE) {
			hp->sw_bmcr = BMCR_SPEED100;
		} else {
			if(ep->speed == SPEED_100)
				hp->sw_bmcr = BMCR_SPEED100;
			else
				hp->sw_bmcr = 0;
			if(ep->duplex == DUPLEX_FULL)
				hp->sw_bmcr |= BMCR_FULLDPLX;
		}
		happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);
		if (!is_lucent_phy(hp)) {
			/* OK, seems we need do disable the transceiver for the first
			 * tick to make sure we get an accurate link state at the
			 * second tick.
			 */
			hp->sw_csconfig = happy_meal_tcvr_read(hp, tregs,
							       DP83840_CSCONFIG);
			hp->sw_csconfig &= ~(CSCONFIG_TCVDISAB);
			happy_meal_tcvr_write(hp, tregs, DP83840_CSCONFIG,
					      hp->sw_csconfig);
		}
		hp->timer_state = ltrywait;
	}

	hp->timer_ticks = 0;
	hp->happy_timer.expires = jiffies + (12 * HZ)/10;  /* 1.2 sec. */
	hp->happy_timer.data = (unsigned long) hp;
	hp->happy_timer.function = &happy_meal_timer;
	add_timer(&hp->happy_timer);
}

#define CRC_POLYNOMIAL_BE 0x04c11db7UL  /* Ethernet CRC, big endian */
#define CRC_POLYNOMIAL_LE 0xedb88320UL  /* Ethernet CRC, little endian */

static int happy_meal_init(struct happy_meal *hp, int from_irq)
{
	struct hmeal_gregs   *gregs        = hp->gregs;
	struct hmeal_etxregs *etxregs      = hp->etxregs;
	struct hmeal_erxregs *erxregs      = hp->erxregs;
	struct hmeal_bigmacregs *bregs     = hp->bigmacregs;
	struct hmeal_tcvregs *tregs        = hp->tcvregs;
	unsigned long regtmp, rxcfg;
	unsigned char *e = &hp->dev->dev_addr[0];

	/* If auto-negotiation timer is running, kill it. */
	del_timer(&hp->happy_timer);

	HMD(("happy_meal_init: happy_flags[%08x] ",
	     hp->happy_flags));
	if(!(hp->happy_flags & HFLAG_INIT)) {
		HMD(("set HFLAG_INIT, "));
		hp->happy_flags |= HFLAG_INIT;
		happy_meal_get_counters(hp, bregs);
	}

	/* Stop polling. */
	HMD(("to happy_meal_poll_stop\n"));
	happy_meal_poll_stop(hp, tregs);

	/* Stop transmitter and receiver. */
	HMD(("happy_meal_init: to happy_meal_stop\n"));
	happy_meal_stop(hp, gregs);

	/* Alloc and reset the tx/rx descriptor chains. */
	HMD(("happy_meal_init: to happy_meal_init_rings\n"));
#ifndef __sparc_v9__	
	if(sparc_cpu_model == sun4c)
		sun4c_happy_meal_init_rings(hp);
	else
#endif	
		happy_meal_init_rings(hp, from_irq);

	/* Shut up the MIF. */
	HMD(("happy_meal_init: Disable all MIF irqs (old[%08x]), ",
	     hme_read32(hp, &tregs->int_mask)));
	hme_write32(hp, &tregs->int_mask, 0xffff);

	/* See if we can enable the MIF frame on this card to speak to the DP83840. */
	if(hp->happy_flags & HFLAG_FENABLE) {
		HMD(("use frame old[%08x], ",
		     hme_read32(hp, &tregs->cfg)));
		hme_write32(hp, &tregs->cfg,
			    hme_read32(hp, &tregs->cfg) & ~(TCV_CFG_BENABLE));
	} else {
		HMD(("use bitbang old[%08x], ",
		     hme_read32(hp, &tregs->cfg)));
		hme_write32(hp, &tregs->cfg,
			    hme_read32(hp, &tregs->cfg) | TCV_CFG_BENABLE);
	}

	/* Check the state of the transceiver. */
	HMD(("to happy_meal_transceiver_check\n"));
	happy_meal_transceiver_check(hp, tregs);

	/* Put the Big Mac into a sane state. */
	HMD(("happy_meal_init: "));
	switch(hp->tcvr_type) {
	case none:
		/* Cannot operate if we don't know the transceiver type! */
		HMD(("AAIEEE no transceiver type, EAGAIN"));
		return -EAGAIN;

	case internal:
		/* Using the MII buffers. */
		HMD(("internal, using MII, "));
		hme_write32(hp, &bregs->xif_cfg, 0);
		break;

	case external:
		/* Not using the MII, disable it. */
		HMD(("external, disable MII, "));
		hme_write32(hp, &bregs->xif_cfg, BIGMAC_XCFG_MIIDISAB);
		break;
	};

	if(happy_meal_tcvr_reset(hp, tregs))
		return -EAGAIN;

	/* Reset the Happy Meal Big Mac transceiver and the receiver. */
	HMD(("tx/rx reset, "));
	happy_meal_tx_reset(hp, bregs);
	happy_meal_rx_reset(hp, bregs);

	/* Set jam size and inter-packet gaps to reasonable defaults. */
	HMD(("jsize/ipg1/ipg2, "));
	hme_write32(hp, &bregs->jsize, DEFAULT_JAMSIZE);
	hme_write32(hp, &bregs->ipkt_gap1, DEFAULT_IPG1);
	hme_write32(hp, &bregs->ipkt_gap2, DEFAULT_IPG2);

	/* Load up the MAC address and random seed. */
	HMD(("rseed/macaddr, "));

	/* The docs recommend to use the 10LSB of our MAC here. */
	hme_write32(hp, &bregs->rand_seed, ((e[5] | e[4]<<8)&0x3ff));

	hme_write32(hp, &bregs->mac_addr2, ((e[4] << 8) | e[5]));
	hme_write32(hp, &bregs->mac_addr1, ((e[2] << 8) | e[3]));
	hme_write32(hp, &bregs->mac_addr0, ((e[0] << 8) | e[1]));

	HMD(("htable, "));
	if((hp->dev->flags & IFF_ALLMULTI) ||
	   (hp->dev->mc_count > 64)) {
		hme_write32(hp, &bregs->htable0, 0xffff);
		hme_write32(hp, &bregs->htable1, 0xffff);
		hme_write32(hp, &bregs->htable2, 0xffff);
		hme_write32(hp, &bregs->htable3, 0xffff);
	} else if((hp->dev->flags & IFF_PROMISC) == 0) {
		u16 hash_table[4];
		struct dev_mc_list *dmi = hp->dev->mc_list;
		char *addrs;
		int i, j, bit, byte;
		u32 crc, poly = CRC_POLYNOMIAL_LE;

		for(i = 0; i < 4; i++)
			hash_table[i] = 0;

		for(i = 0; i < hp->dev->mc_count; i++) {
			addrs = dmi->dmi_addr;
			dmi = dmi->next;

			if(!(*addrs & 1))
				continue;

			crc = 0xffffffffU;
			for(byte = 0; byte < 6; byte++) {
				for(bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
					int test;

					test = ((bit ^ crc) & 0x01);
					crc >>= 1;
					if(test)
						crc = crc ^ poly;
				}
			}
			crc >>= 26;
			hash_table[crc >> 4] |= 1 << (crc & 0xf);
		}
		hme_write32(hp, &bregs->htable0, hash_table[0]);
		hme_write32(hp, &bregs->htable1, hash_table[1]);
		hme_write32(hp, &bregs->htable2, hash_table[2]);
		hme_write32(hp, &bregs->htable3, hash_table[3]);
	} else {
		hme_write32(hp, &bregs->htable3, 0);
		hme_write32(hp, &bregs->htable2, 0);
		hme_write32(hp, &bregs->htable1, 0);
		hme_write32(hp, &bregs->htable0, 0);
	}

	/* Set the RX and TX ring ptrs. */
	HMD(("ring ptrs rxr[%08x] txr[%08x]\n",
	     (hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)),
	     (hp->hblock_dvma + hblock_offset(happy_meal_txd, 0))));
	hme_write32(hp, &erxregs->rx_ring,
		    (hp->hblock_dvma + hblock_offset(happy_meal_rxd, 0)));
	hme_write32(hp, &etxregs->tx_ring,
		    (hp->hblock_dvma + hblock_offset(happy_meal_txd, 0)));

	/* Set the supported burst sizes. */
	HMD(("happy_meal_init: old[%08x] bursts<",
	     hme_read32(hp, &gregs->cfg)));

#ifdef __sparc_v9__
	/* XXX Can sun4d do these too? */
	if(hp->happy_bursts & DMA_BURST64) {
		u32 gcfg = GREG_CFG_BURST64;

		/* I have no idea if I should set the extended
		 * transfer mode bit for Cheerio, so for now I
		 * do not.  -DaveM
		 */
		if((hp->happy_flags & HFLAG_PCI) == 0) {
			mmu_set_sbus64(hp->happy_sbus_dev,
				       hp->happy_bursts);
			gcfg |= GREG_CFG_64BIT;
		}

		HMD(("64>"));
		hme_write32(hp, &gregs->cfg, gcfg);
	} else
#endif
	if(hp->happy_bursts & DMA_BURST32) {
		HMD(("32>"));
		hme_write32(hp, &gregs->cfg, GREG_CFG_BURST32);
	} else if(hp->happy_bursts & DMA_BURST16) {
		HMD(("16>"));
		hme_write32(hp, &gregs->cfg, GREG_CFG_BURST16);
	} else {
		HMD(("XXX>"));
		hme_write32(hp, &gregs->cfg, 0);
	}

	/* Turn off interrupts we do not want to hear. */
	HMD((", enable global interrupts, "));
	hme_write32(hp, &gregs->imask,
		    (GREG_IMASK_GOTFRAME | GREG_IMASK_RCNTEXP |
		     GREG_IMASK_SENTFRAME | GREG_IMASK_TXPERR));

	/* Set the transmit ring buffer size. */
	HMD(("tx rsize=%d oreg[%08x], ", (int)TX_RING_SIZE,
	     hme_read32(hp, &etxregs->tx_rsize)));
	hme_write32(hp, &etxregs->tx_rsize, (TX_RING_SIZE >> ETX_RSIZE_SHIFT) - 1);

	/* Enable transmitter DVMA. */
	HMD(("tx dma enable old[%08x], ",
	     hme_read32(hp, &etxregs->cfg)));
	hme_write32(hp, &etxregs->cfg,
		    hme_read32(hp, &etxregs->cfg) | ETX_CFG_DMAENABLE);

	/* This chip really rots, for the receiver sometimes when you
	 * write to it's control registers not all the bits get there
	 * properly.  I cannot think of a sane way to provide complete
	 * coverage for this hardware bug yet.
	 */
	HMD(("erx regs bug old[%08x]\n",
	     hme_read32(hp, &erxregs->cfg)));
	hme_write32(hp, &erxregs->cfg, ERX_CFG_DEFAULT(RX_OFFSET));
	regtmp = hme_read32(hp, &erxregs->cfg);
	hme_write32(hp, &erxregs->cfg, ERX_CFG_DEFAULT(RX_OFFSET));
	if(hme_read32(hp, &erxregs->cfg) != ERX_CFG_DEFAULT(RX_OFFSET)) {
		printk(KERN_ERR "happy meal: Eieee, rx config register gets greasy fries.\n");
		printk(KERN_ERR "happy meal: Trying to set %08x, reread gives %08lx\n",
		       ERX_CFG_DEFAULT(RX_OFFSET), regtmp);
		/* XXX Should return failure here... */
	}

	/* Enable Big Mac hash table filter. */
	HMD(("happy_meal_init: enable hash rx_cfg_old[%08x], ",
	     hme_read32(hp, &bregs->rx_cfg)));
	rxcfg = BIGMAC_RXCFG_HENABLE;
	if(hp->dev->flags & IFF_PROMISC)
		rxcfg |= BIGMAC_RXCFG_PMISC;
	hme_write32(hp, &bregs->rx_cfg, rxcfg);

	/* Let the bits settle in the chip. */
	udelay(10);

	/* Ok, configure the Big Mac transmitter. */
	HMD(("BIGMAC init, "));
	regtmp = 0;
	if(hp->happy_flags & HFLAG_FULL)
		regtmp |= BIGMAC_TXCFG_FULLDPLX;
	hme_write32(hp, &bregs->tx_cfg, regtmp | BIGMAC_TXCFG_DGIVEUP);

	/* Enable the output drivers no matter what. */
	regtmp = BIGMAC_XCFG_ODENABLE;

	/* If card can do lance mode, enable it. */
	if(hp->happy_flags & HFLAG_LANCE)
		regtmp |= (DEFAULT_IPG0 << 5) | BIGMAC_XCFG_LANCE;

	/* Disable the MII buffers if using external transceiver. */
	if(hp->tcvr_type == external)
		regtmp |= BIGMAC_XCFG_MIIDISAB;

	HMD(("XIF config old[%08x], ",
	     hme_read32(hp, &bregs->xif_cfg)));
	hme_write32(hp, &bregs->xif_cfg, regtmp);

	/* Start things up. */
	HMD(("tx old[%08x] and rx [%08x] ON!\n",
	     hme_read32(hp, &bregs->tx_cfg),
	     hme_read32(hp, &bregs->rx_cfg)));
	hme_write32(hp, &bregs->tx_cfg,
		    hme_read32(hp, &bregs->tx_cfg) | BIGMAC_TXCFG_ENABLE);
	hme_write32(hp, &bregs->rx_cfg,
		    hme_read32(hp, &bregs->rx_cfg) | BIGMAC_RXCFG_ENABLE);

	/* Get the autonegotiation started, and the watch timer ticking. */
	happy_meal_begin_auto_negotiation(hp, tregs, NULL);

	/* Success. */
	return 0;
}

static void happy_meal_set_initial_advertisement(struct happy_meal *hp)
{
	struct hmeal_tcvregs *tregs	= hp->tcvregs;
	struct hmeal_bigmacregs *bregs	= hp->bigmacregs;
	struct hmeal_gregs *gregs	= hp->gregs;

	happy_meal_stop(hp, gregs);
	hme_write32(hp, &tregs->int_mask, 0xffff);
	if(hp->happy_flags & HFLAG_FENABLE)
		hme_write32(hp, &tregs->cfg,
			    hme_read32(hp, &tregs->cfg) & ~(TCV_CFG_BENABLE));
	else
		hme_write32(hp, &tregs->cfg,
			    hme_read32(hp, &tregs->cfg) | TCV_CFG_BENABLE);
	happy_meal_transceiver_check(hp, tregs);
	switch(hp->tcvr_type) {
	case none:
		return;
	case internal:
		hme_write32(hp, &bregs->xif_cfg, 0);
		break;
	case external:
		hme_write32(hp, &bregs->xif_cfg, BIGMAC_XCFG_MIIDISAB);
		break;
	};
	if(happy_meal_tcvr_reset(hp, tregs))
		return;

	/* Latch PHY registers as of now. */
	hp->sw_bmsr      = happy_meal_tcvr_read(hp, tregs, DP83840_BMSR);
	hp->sw_advertise = happy_meal_tcvr_read(hp, tregs, DP83840_ADVERTISE);

	/* Advertise everything we can support. */
	if(hp->sw_bmsr & BMSR_10HALF)
		hp->sw_advertise |= (ADVERTISE_10HALF);
	else
		hp->sw_advertise &= ~(ADVERTISE_10HALF);

	if(hp->sw_bmsr & BMSR_10FULL)
		hp->sw_advertise |= (ADVERTISE_10FULL);
	else
		hp->sw_advertise &= ~(ADVERTISE_10FULL);
	if(hp->sw_bmsr & BMSR_100HALF)
		hp->sw_advertise |= (ADVERTISE_100HALF);
	else
		hp->sw_advertise &= ~(ADVERTISE_100HALF);
	if(hp->sw_bmsr & BMSR_100FULL)
		hp->sw_advertise |= (ADVERTISE_100FULL);
	else
		hp->sw_advertise &= ~(ADVERTISE_100FULL);

	/* Update the PHY advertisement register. */
	happy_meal_tcvr_write(hp, tregs, DP83840_ADVERTISE, hp->sw_advertise);
}

/* Once status is latched (by happy_meal_interrupt) it is cleared by
 * the hardware, so we cannot re-read it and get a correct value.
 */
static int happy_meal_is_not_so_happy(struct happy_meal *hp,
				      struct hmeal_gregs *gregs,
				      unsigned long status)
{
	int reset = 0;

	/* Only print messages for non-counter related interrupts. */
	if(status & (GREG_STAT_RFIFOVF | GREG_STAT_STSTERR | GREG_STAT_TFIFO_UND |
		     GREG_STAT_MAXPKTERR | GREG_STAT_RXERR |
		     GREG_STAT_RXPERR | GREG_STAT_RXTERR | GREG_STAT_EOPERR |
		     GREG_STAT_MIFIRQ | GREG_STAT_TXEACK | GREG_STAT_TXLERR |
		     GREG_STAT_TXPERR | GREG_STAT_TXTERR | GREG_STAT_SLVERR |
		     GREG_STAT_SLVPERR))
		printk(KERN_ERR "%s: Error interrupt for happy meal, status = %08lx\n",
		       hp->dev->name, status);

	if(status & GREG_STAT_RFIFOVF) {
		/* The receive FIFO overflowwed, usually a DMA error. */
		printk(KERN_ERR "%s: Happy Meal receive FIFO overflow.\n", hp->dev->name);
		reset = 1;
	}

	if(status & GREG_STAT_STSTERR) {
		/* BigMAC SQE link test failed. */
		printk(KERN_ERR "%s: Happy Meal BigMAC SQE test failed.\n", hp->dev->name);
		reset = 1;
	}

	if(status & GREG_STAT_TFIFO_UND) {
		/* Transmit FIFO underrun, again DMA error likely. */
		printk(KERN_ERR "%s: Happy Meal transmitter FIFO underrun, DMA error.\n",
		       hp->dev->name);
		reset = 1;
	}

	if(status & GREG_STAT_MAXPKTERR) {
		/* Driver error, tried to transmit something larger
		 * than ethernet max mtu.
		 */
		printk(KERN_ERR "%s: Happy Meal MAX Packet size error.\n", hp->dev->name);
		reset = 1;
	}

	if(status & GREG_STAT_NORXD) {
		/* This is harmless, it just means the system is
		 * quite loaded and the incomming packet rate was
		 * faster than the interrupt handler could keep up
		 * with.
		 */
		printk(KERN_INFO "%s: Happy Meal out of receive "
		       "descriptors, packet dropped.\n",
		       hp->dev->name);
	}

	if(status & (GREG_STAT_RXERR|GREG_STAT_RXPERR|GREG_STAT_RXTERR)) {
		/* All sorts of DMA receive errors. */
		printk(KERN_ERR "%s: Happy Meal rx DMA errors [ ", hp->dev->name);
		if(status & GREG_STAT_RXERR)
			printk("GenericError ");
		if(status & GREG_STAT_RXPERR)
			printk("ParityError ");
		if(status & GREG_STAT_RXTERR)
			printk("RxTagBotch ");
		printk("]\n");
		reset = 1;
	}

	if(status & GREG_STAT_EOPERR) {
		/* Driver bug, didn't set EOP bit in tx descriptor given
		 * to the happy meal.
		 */
		printk(KERN_ERR "%s: EOP not set in happy meal transmit descriptor!\n",
		       hp->dev->name);
		reset = 1;
	}

	if(status & GREG_STAT_MIFIRQ) {
		/* MIF signalled an interrupt, were we polling it? */
		printk(KERN_ERR "%s: Happy Meal MIF interrupt.\n", hp->dev->name);
	}

	if(status &
	   (GREG_STAT_TXEACK|GREG_STAT_TXLERR|GREG_STAT_TXPERR|GREG_STAT_TXTERR)) {
		/* All sorts of transmit DMA errors. */
		printk(KERN_ERR "%s: Happy Meal tx DMA errors [ ", hp->dev->name);
		if(status & GREG_STAT_TXEACK)
			printk("GenericError ");
		if(status & GREG_STAT_TXLERR)
			printk("LateError ");
		if(status & GREG_STAT_TXPERR)
			printk("ParityErro ");
		if(status & GREG_STAT_TXTERR)
			printk("TagBotch ");
		printk("]\n");
		reset = 1;
	}

	if(status & (GREG_STAT_SLVERR|GREG_STAT_SLVPERR)) {
		/* Bus or parity error when cpu accessed happy meal registers
		 * or it's internal FIFO's.  Should never see this.
		 */
		printk(KERN_ERR "%s: Happy Meal register access SBUS slave (%s) error.\n",
		       hp->dev->name,
		       (status & GREG_STAT_SLVPERR) ? "parity" : "generic");
		reset = 1;
	}

	if(reset) {
		printk(KERN_NOTICE "%s: Resetting...\n", hp->dev->name);
		happy_meal_init(hp, 1);
		return 1;
	}
	return 0;
}

static inline void happy_meal_mif_interrupt(struct happy_meal *hp,
					    struct hmeal_gregs *gregs,
					    struct hmeal_tcvregs *tregs)
{
	printk(KERN_INFO "%s: Link status change.\n", hp->dev->name);
	hp->sw_bmcr = happy_meal_tcvr_read(hp, tregs, DP83840_BMCR);
	hp->sw_lpa = happy_meal_tcvr_read(hp, tregs, DP83840_LPA);

	/* Use the fastest transmission protocol possible. */
	if(hp->sw_lpa & LPA_100FULL) {
		printk(KERN_INFO "%s: Switching to 100Mbps at full duplex.", hp->dev->name);
		hp->sw_bmcr |= (BMCR_FULLDPLX | BMCR_SPEED100);
	} else if(hp->sw_lpa & LPA_100HALF) {
		printk(KERN_INFO "%s: Switching to 100MBps at half duplex.", hp->dev->name);
		hp->sw_bmcr |= BMCR_SPEED100;
	} else if(hp->sw_lpa & LPA_10FULL) {
		printk(KERN_INFO "%s: Switching to 10MBps at full duplex.", hp->dev->name);
		hp->sw_bmcr |= BMCR_FULLDPLX;
	} else {
		printk(KERN_INFO "%s: Using 10Mbps at half duplex.", hp->dev->name);
	}
	happy_meal_tcvr_write(hp, tregs, DP83840_BMCR, hp->sw_bmcr);

	/* Finally stop polling and shut up the MIF. */
	happy_meal_poll_stop(hp, tregs);
}

#ifdef TXDEBUG
#define TXD(x) printk x
#else
#define TXD(x)
#endif

static inline void happy_meal_tx(struct happy_meal *hp)
{
	struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
	struct happy_meal_txd *this;
	int elem = hp->tx_old;

	TXD(("TX<"));
	while(elem != hp->tx_new) {
		struct sk_buff *skb;

		TXD(("[%d]", elem));
		this = &txbase[elem];
		if(this->tx_flags & TXFLAG_OWN)
			break;
		skb = hp->tx_skbs[elem];
		hp->tx_skbs[elem] = NULL;
		hp->net_stats.tx_bytes+=skb->len;
		
		dev_kfree_skb(skb);

		hp->net_stats.tx_packets++;
		elem = NEXT_TX(elem);
	}
	hp->tx_old = elem;
	TXD((">"));
}

#ifdef CONFIG_PCI
static inline void pci_happy_meal_tx(struct happy_meal *hp)
{
	struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
	struct happy_meal_txd *this;
	int elem = hp->tx_old;

	TXD(("TX<"));
	while(elem != hp->tx_new) {
		struct sk_buff *skb;
		unsigned int flags;

		TXD(("[%d]", elem));
		this = &txbase[elem];
#ifdef  __sparc_v9__
		__asm__ __volatile__("lduwa [%1] %2, %0"
				     : "=r" (flags)
				     : "r" (&this->tx_flags), "i" (ASI_PL));
#else
		flags = flip_dword(this->tx_flags);
#endif
		if(flags & TXFLAG_OWN)
			break;
		skb = hp->tx_skbs[elem];
		hp->tx_skbs[elem] = NULL;
		hp->net_stats.tx_bytes+=skb->len;
		
		dev_kfree_skb(skb);

		hp->net_stats.tx_packets++;
		elem = NEXT_TX(elem);
	}
	hp->tx_old = elem;
	TXD((">"));
}
#endif

#ifndef __sparc_v9__
static inline void sun4c_happy_meal_tx(struct happy_meal *hp)
{
	struct happy_meal_txd *txbase = &hp->happy_block->happy_meal_txd[0];
	struct happy_meal_txd *this;
	int elem = hp->tx_old;

	TXD(("TX<"));
	while(elem != hp->tx_new) {
		TXD(("[%d]", elem));

		this = &txbase[elem];

		if(this->tx_flags & TXFLAG_OWN)
			break;

		hp->net_stats.tx_packets++;
		elem = NEXT_TX(elem);
	}
	hp->tx_old = elem;
	TXD((">"));
}
#endif

#ifdef RXDEBUG
#define RXD(x) printk x
#else
#define RXD(x)
#endif

/* Originally I used to handle the allocation failure by just giving back just
 * that one ring buffer to the happy meal.  Problem is that usually when that
 * condition is triggered, the happy meal expects you to do something reasonable
 * with all of the packets it has DMA'd in.  So now I just drop the entire
 * ring when we cannot get a new skb and give them all back to the happy meal,
 * maybe things will be "happier" now.
 */
static inline void happy_meal_rx(struct happy_meal *hp, struct device *dev,
				 struct hmeal_gregs *gregs)
{
	struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
	struct happy_meal_rxd *this;
	int elem = hp->rx_new, drops = 0;

	RXD(("RX<"));
	this = &rxbase[elem];
	while(!(this->rx_flags & RXFLAG_OWN)) {
		struct sk_buff *skb;
		unsigned int flags = this->rx_flags;
		int len = flags >> 16;
		u16 csum = flags & RXFLAG_CSUM;

		RXD(("[%d ", elem));

		/* Check for errors. */
		if((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
			RXD(("ERR(%08x)]", flags));
			hp->net_stats.rx_errors++;
			if(len < ETH_ZLEN)
				hp->net_stats.rx_length_errors++;
			if(len & (RXFLAG_OVERFLOW >> 16)) {
				hp->net_stats.rx_over_errors++;
				hp->net_stats.rx_fifo_errors++;
			}

			/* Return it to the Happy meal. */
	drop_it:
			hp->net_stats.rx_dropped++;
			this->rx_addr = kva_to_hva(hp, hp->rx_skbs[elem]->data);
			this->rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));
			goto next;
		}
		skb = hp->rx_skbs[elem];
#ifdef NEED_DMA_SYNCHRONIZATION
		mmu_sync_dma(kva_to_hva(hp, skb->data),
			     skb->len, hp->happy_sbus_dev->my_bus);
#endif
		if(len > RX_COPY_THRESHOLD) {
			struct sk_buff *new_skb;

			/* Now refill the entry, if we can. */
			new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE,
						       (GFP_DMA|GFP_ATOMIC));
			if(!new_skb) {
				drops++;
				goto drop_it;
			}

			hp->rx_skbs[elem] = new_skb;
			new_skb->dev = dev;
			skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET));
			rxbase[elem].rx_addr = kva_to_hva(hp, new_skb->data);
			skb_reserve(new_skb, RX_OFFSET);
			rxbase[elem].rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));

			/* Trim the original skb for the netif. */
			skb_trim(skb, len);
		} else {
			struct sk_buff *copy_skb = dev_alloc_skb(len + 2);

			if(!copy_skb) {
				drops++;
				goto drop_it;
			}

			copy_skb->dev = dev;
			skb_reserve(copy_skb, 2);
			skb_put(copy_skb, len);
			memcpy(copy_skb->data, skb->data, len);

			/* Reuse original ring buffer. */
			rxbase[elem].rx_addr = kva_to_hva(hp, skb->data);
			rxbase[elem].rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));

			skb = copy_skb;
		}

		/* This card is _fucking_ hot... */
		if(!(csum ^ 0xffff))
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
			skb->ip_summed = CHECKSUM_NONE;

		RXD(("len=%d csum=%4x]", len, csum));
		skb->protocol = eth_type_trans(skb, dev);
		netif_rx(skb);

		hp->net_stats.rx_packets++;
		hp->net_stats.rx_bytes+=len;
	next:
		elem = NEXT_RX(elem);
		this = &rxbase[elem];
	}
	hp->rx_new = elem;
	if(drops)
		printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
	RXD((">"));
}

#ifdef CONFIG_PCI
static inline void pci_happy_meal_rx(struct happy_meal *hp, struct device *dev,
				     struct hmeal_gregs *gregs)
{
	struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
	struct happy_meal_rxd *this;
	unsigned int flags;
	int elem = hp->rx_new, drops = 0;

	RXD(("RX<"));
	this = &rxbase[elem];
#ifdef  __sparc_v9__
	__asm__ __volatile__("lduwa [%1] %2, %0"
			     : "=r" (flags)
			     : "r" (&this->rx_flags), "i" (ASI_PL));
#else
	flags = flip_dword(this->rx_flags); /* FIXME */
#endif
	while(!(flags & RXFLAG_OWN)) {
		struct sk_buff *skb;
		int len;
		u16 csum;

		RXD(("[%d ", elem));

		len = flags >> 16;
		csum = flags & RXFLAG_CSUM;

		/* Check for errors. */
		if((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
			RXD(("ERR(%08x)]", flags));
			hp->net_stats.rx_errors++;
			if(len < ETH_ZLEN)
				hp->net_stats.rx_length_errors++;
			if(len & (RXFLAG_OVERFLOW >> 16)) {
				hp->net_stats.rx_over_errors++;
				hp->net_stats.rx_fifo_errors++;
			}

			/* Return it to the Happy meal. */
	drop_it:
			hp->net_stats.rx_dropped++;
			pcihme_write_rxd(this,
				 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
				 (u32) virt_to_bus((volatile void *)hp->rx_skbs[elem]->data));
			goto next;
		}
		skb = hp->rx_skbs[elem];
		if(len > RX_COPY_THRESHOLD) {
			struct sk_buff *new_skb;

			/* Now refill the entry, if we can. */
			new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE,
						       (GFP_DMA|GFP_ATOMIC));
			if(!new_skb) {
				drops++;
				goto drop_it;
			}

			hp->rx_skbs[elem] = new_skb;
			new_skb->dev = dev;
			skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET));
			pcihme_write_rxd(&rxbase[elem],
				 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
				  (u32)virt_to_bus((volatile void *)new_skb->data));
			skb_reserve(new_skb, RX_OFFSET);

			/* Trim the original skb for the netif. */
			skb_trim(skb, len);
		} else {
			struct sk_buff *copy_skb = dev_alloc_skb(len + 2);

			if(!copy_skb) {
				drops++;
				goto drop_it;
			}

			copy_skb->dev = dev;
			skb_reserve(copy_skb, 2);
			skb_put(copy_skb, len);
			memcpy(copy_skb->data, skb->data, len);

			/* Reuse original ring buffer. */
			pcihme_write_rxd(&rxbase[elem],
				 (RXFLAG_OWN|((RX_BUF_ALLOC_SIZE-RX_OFFSET)<<16)),
				 (u32)virt_to_bus((volatile void *)skb->data));

			skb = copy_skb;
		}

		/* This card is _fucking_ hot... */
		if(!~(csum))
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
			skb->ip_summed = CHECKSUM_NONE;

		RXD(("len=%d csum=%4x]", len, csum));
		skb->protocol = eth_type_trans(skb, dev);
		netif_rx(skb);

		hp->net_stats.rx_packets++;
		hp->net_stats.rx_bytes+=len;
	next:
		elem = NEXT_RX(elem);
		this = &rxbase[elem];
#ifdef __sparc_v9__ 
		__asm__ __volatile__("lduwa [%1] %2, %0"
				     : "=r" (flags)
				     : "r" (&this->rx_flags), "i" (ASI_PL));
#else
		flags = flip_dword(this->rx_flags); /* FIXME */
#endif
	}
	hp->rx_new = elem;
	if(drops)
		printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
	RXD((">"));
}
#endif

#ifndef __sparc_v9__
static inline void sun4c_happy_meal_rx(struct happy_meal *hp, struct device *dev,
				       struct hmeal_gregs *gregs)
{
	struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
	struct happy_meal_rxd *this;
	struct hmeal_buffers *hbufs = hp->sun4c_buffers;
	__u32 hbufs_dvma = hp->s4c_buf_dvma;
	int elem = hp->rx_new, drops = 0;

	RXD(("RX<"));
	this = &rxbase[elem];
	while(!(this->rx_flags & RXFLAG_OWN)) {
		struct sk_buff *skb;
		unsigned int flags = this->rx_flags;
		unsigned char *thisbuf = &hbufs->rx_buf[elem][0];
		__u32 thisbuf_dvma = hbufs_dvma + hbuf_offset(rx_buf, elem);
		int len = flags >> 16;

		RXD(("[%d ", elem));

		/* Check for errors. */
		if((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
			RXD(("ERR(%08x)]", flags));
			hp->net_stats.rx_errors++;
			if(len < ETH_ZLEN)
				hp->net_stats.rx_length_errors++;
			if(len & (RXFLAG_OVERFLOW >> 16)) {
				hp->net_stats.rx_over_errors++;
				hp->net_stats.rx_fifo_errors++;
			}

			hp->net_stats.rx_dropped++;
		} else {
			skb = dev_alloc_skb(len + 2);
			if(skb == 0) {
				drops++;
				hp->net_stats.rx_dropped++;
			} else {
				RXD(("len=%d]", len));
				skb->dev = hp->dev;
				skb_reserve(skb, 2);
				skb_put(skb, len);
				eth_copy_and_sum(skb, (thisbuf+2), len, 0);
				skb->protocol = eth_type_trans(skb, dev);
				netif_rx(skb);
				hp->net_stats.rx_packets++;
				hp->net_stats.rx_bytes+=len;
			}
		}
		/* Return the buffer to the Happy Meal. */
		this->rx_addr = thisbuf_dvma;
		this->rx_flags =
			(RXFLAG_OWN | ((SUN4C_RX_BUFF_SIZE - RX_OFFSET) << 16));

		elem = NEXT_RX(elem);
		this = &rxbase[elem];
	}
	hp->rx_new = elem;
	if(drops)
		printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
	RXD((">"));
}

static inline void sun4d_happy_meal_rx(struct happy_meal *hp, struct device *dev,
				       struct hmeal_gregs *gregs)
{
	struct happy_meal_rxd *rxbase = &hp->happy_block->happy_meal_rxd[0];
	struct happy_meal_rxd *this;
	int elem = hp->rx_new, drops = 0;
	__u32 va;

	RXD(("RX<"));
	this = &rxbase[elem];
	while(!(this->rx_flags & RXFLAG_OWN)) {
		struct sk_buff *skb;
		unsigned int flags = this->rx_flags;
		int len = flags >> 16;
		u16 csum = flags & RXFLAG_CSUM;

		RXD(("[%d ", elem));

		/* Check for errors. */
		if((len < ETH_ZLEN) || (flags & RXFLAG_OVERFLOW)) {
			RXD(("ERR(%08x)]", flags));
			hp->net_stats.rx_errors++;
			if(len < ETH_ZLEN)
				hp->net_stats.rx_length_errors++;
			if(len & (RXFLAG_OVERFLOW >> 16)) {
				hp->net_stats.rx_over_errors++;
				hp->net_stats.rx_fifo_errors++;
			}

			/* Return it to the Happy meal. */
	drop_it:
			hp->net_stats.rx_dropped++;
			va = (__u32)hp->sun4d_buffers + elem * PAGE_SIZE;
			this->rx_addr = iounit_map_dma_page(va, hp->rx_skbs[elem]->data,
							    hp->happy_sbus_dev->my_bus);
			this->rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));
			goto next;
		}
		skb = hp->rx_skbs[elem];
		if(len > RX_COPY_THRESHOLD) {
			struct sk_buff *new_skb;

			/* Now refill the entry, if we can. */
			new_skb = happy_meal_alloc_skb(RX_BUF_ALLOC_SIZE,
						       (GFP_DMA | GFP_ATOMIC));
			if(!new_skb) {
				drops++;
				goto drop_it;
			}

			hp->rx_skbs[elem] = new_skb;
			new_skb->dev = dev;
			skb_put(new_skb, (ETH_FRAME_LEN + RX_OFFSET));
			va = (__u32)hp->sun4d_buffers + elem * PAGE_SIZE;
			rxbase[elem].rx_addr = iounit_map_dma_page(va, new_skb->data,
								hp->happy_sbus_dev->my_bus);

			skb_reserve(new_skb, RX_OFFSET);
			rxbase[elem].rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));

			/* Trim the original skb for the netif. */
			skb_trim(skb, len);
		} else {
			struct sk_buff *copy_skb = dev_alloc_skb(len + 2);

			if(!copy_skb) {
				drops++;
				goto drop_it;
			}

			copy_skb->dev = dev;
			skb_reserve(copy_skb, 2);
			skb_put(copy_skb, len);
			memcpy(copy_skb->data, skb->data, len);

			/* Reuse original ring buffer. */
			va = (__u32)hp->sun4d_buffers + elem * PAGE_SIZE;
			rxbase[elem].rx_addr = iounit_map_dma_page(va, skb->data,
								hp->happy_sbus_dev->my_bus);
			rxbase[elem].rx_flags =
				(RXFLAG_OWN | ((RX_BUF_ALLOC_SIZE - RX_OFFSET) << 16));

			skb = copy_skb;
		}

		/* This card is _fucking_ hot... */
		if(!(csum ^ 0xffff))
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
			skb->ip_summed = CHECKSUM_NONE;

		RXD(("len=%d csum=%4x]", len, csum));
		skb->protocol = eth_type_trans(skb, dev);
		netif_rx(skb);

		hp->net_stats.rx_packets++;
		hp->net_stats.rx_bytes+=len;
	next:
		elem = NEXT_RX(elem);
		this = &rxbase[elem];
	}
	hp->rx_new = elem;
	if(drops)
		printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n", hp->dev->name);
	RXD((">"));
}
#endif

static void happy_meal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct device *dev            = (struct device *) dev_id;
	struct happy_meal *hp         = (struct happy_meal *) dev->priv;
	struct hmeal_gregs *gregs     = hp->gregs;
	struct hmeal_tcvregs *tregs   = hp->tcvregs;
	unsigned int happy_status    = hme_read32(hp, &gregs->stat);

	HMD(("happy_meal_interrupt: status=%08x ", happy_status));

	dev->interrupt = 1;

	if(happy_status & GREG_STAT_ERRORS) {
		HMD(("ERRORS "));
		if(happy_meal_is_not_so_happy(hp, gregs, /* un- */ happy_status)) {
			dev->interrupt = 0;
			return;
		}
	}

	if(happy_status & GREG_STAT_MIFIRQ) {
		HMD(("MIFIRQ "));
		happy_meal_mif_interrupt(hp, gregs, tregs);
	}

	if(happy_status & GREG_STAT_TXALL) {
		HMD(("TXALL "));
		happy_meal_tx(hp);
	}

	if(happy_status & GREG_STAT_RXTOHOST) {
		HMD(("RXTOHOST "));
		happy_meal_rx(hp, dev, gregs);
	}

	if(dev->tbusy && (TX_BUFFS_AVAIL(hp) >= 0)) {
		hp->dev->tbusy = 0;
		mark_bh(NET_BH);
	}

	dev->interrupt = 0;
	HMD(("done\n"));
}

#ifdef CONFIG_PCI
static void pci_happy_meal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct device *dev            = (struct device *) dev_id;
	struct happy_meal *hp         = (struct happy_meal *) dev->priv;
	struct hmeal_gregs *gregs     = hp->gregs;
	struct hmeal_tcvregs *tregs   = hp->tcvregs;
	unsigned int happy_status     = readl((unsigned long)&gregs->stat);

	HMD(("happy_meal_interrupt: status=%08x ", happy_status));

	dev->interrupt = 1;

	if(happy_status & GREG_STAT_ERRORS) {
		HMD(("ERRORS "));
		if(happy_meal_is_not_so_happy(hp, gregs, /* un- */ happy_status)) {
			dev->interrupt = 0;
			return;
		}
	}

	if(happy_status & GREG_STAT_MIFIRQ) {
		HMD(("MIFIRQ "));
		happy_meal_mif_interrupt(hp, gregs, tregs);
	}

	if(happy_status & GREG_STAT_TXALL) {
		HMD(("TXALL "));
		pci_happy_meal_tx(hp);
	}

	if(happy_status & GREG_STAT_RXTOHOST) {
		HMD(("RXTOHOST "));
		pci_happy_meal_rx(hp, dev, gregs);
	}

	if(dev->tbusy && (TX_BUFFS_AVAIL(hp) >= 0)) {
		hp->dev->tbusy = 0;
		mark_bh(NET_BH);
	}
	tx_add_log(hp, TXLOG_ACTION_IRQ, happy_status);
	dev->interrupt = 0;
	HMD(("done\n"));
}
#endif

#ifndef __sparc_v9__
static void sun4c_happy_meal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct device *dev            = (struct device *) dev_id;
	struct happy_meal *hp         = (struct happy_meal *) dev->priv;
	struct hmeal_gregs *gregs     = hp->gregs;
	struct hmeal_tcvregs *tregs   = hp->tcvregs;
	unsigned int happy_status    = hme_read32(hp, &gregs->stat);

	HMD(("happy_meal_interrupt: status=%08x ", happy_status));

	dev->interrupt = 1;

	if(happy_status & GREG_STAT_ERRORS) {
		HMD(("ERRORS "));
		if(happy_meal_is_not_so_happy(hp, gregs, /* un- */ happy_status)) {
			dev->interrupt = 0;
			return;
		}
	}

	if(happy_status & GREG_STAT_MIFIRQ) {
		HMD(("MIFIRQ "));
		happy_meal_mif_interrupt(hp, gregs, tregs);
	}

	if(happy_status & GREG_STAT_TXALL) {
		HMD(("TXALL "));
		sun4c_happy_meal_tx(hp);
	}

	if(happy_status & GREG_STAT_RXTOHOST) {
		HMD(("RXTOHOST "));
		sun4c_happy_meal_rx(hp, dev, gregs);
	}

	if(dev->tbusy && (TX_BUFFS_AVAIL(hp) >= 0)) {
		hp->dev->tbusy = 0;
		mark_bh(NET_BH);
	}

	dev->interrupt = 0;
	HMD(("done\n"));
}

static void sun4d_happy_meal_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
	struct device *dev            = (struct device *) dev_id;
	struct happy_meal *hp         = (struct happy_meal *) dev->priv;
	struct hmeal_gregs *gregs     = hp->gregs;
	struct hmeal_tcvregs *tregs   = hp->tcvregs;
	unsigned int happy_status    = hme_read32(hp, &gregs->stat);

	HMD(("happy_meal_interrupt: status=%08x ", happy_status));

	dev->interrupt = 1;

	if(happy_status & GREG_STAT_ERRORS) {
		HMD(("ERRORS "));
		if(happy_meal_is_not_so_happy(hp, gregs, /* un- */ happy_status)) {
			dev->interrupt = 0;
			return;
		}
	}

	if(happy_status & GREG_STAT_MIFIRQ) {
		HMD(("MIFIRQ "));
		happy_meal_mif_interrupt(hp, gregs, tregs);
	}

	if(happy_status & GREG_STAT_TXALL) {
		HMD(("TXALL "));
		happy_meal_tx(hp);
	}

	if(happy_status & GREG_STAT_RXTOHOST) {
		HMD(("RXTOHOST "));
		sun4d_happy_meal_rx(hp, dev, gregs);
	}

	if(dev->tbusy && (TX_BUFFS_AVAIL(hp) >= 0)) {
		hp->dev->tbusy = 0;
		mark_bh(NET_BH);
	}

	dev->interrupt = 0;
	HMD(("done\n"));
}
#endif

static void quattro_sbus_interrupt(int irq, void *cookie, struct pt_regs *ptregs)
{
	struct quattro *qp = (struct quattro *)cookie;
	int i;

	for(i = 0; i < 4; i++) {
		struct device *dev = qp->happy_meals[i];
		struct happy_meal *hp = (struct happy_meal *) dev->priv;
		struct hmeal_gregs *gregs = hp->gregs;
		struct hmeal_tcvregs *tregs = hp->tcvregs;
		unsigned int happy_status = hme_read32(hp, &gregs->stat);

		HMD(("quattro_interrupt: status=%08x ",happy_status));

		dev->interrupt=1;

		if(happy_status & GREG_STAT_ERRORS) {
			HMD(("ERRORS "));
			if(happy_meal_is_not_so_happy(hp, gregs, happy_status)) {
				dev->interrupt=0;
				break;
			}
		}

		if(happy_status & GREG_STAT_MIFIRQ) {
			HMD(("MIFIRQ "));
			happy_meal_mif_interrupt(hp, gregs, tregs);
		}

		if(happy_status & GREG_STAT_TXALL) {
			HMD(("TXALL "));
			happy_meal_tx(hp);
		}

		if(happy_status & GREG_STAT_RXTOHOST) {
			HMD(("RXTOHOST "));
			happy_meal_rx(hp, dev, gregs);
		}

		if(dev->tbusy && (TX_BUFFS_AVAIL(hp) >= 0)) {
			hp->dev->tbusy = 0;
			mark_bh(NET_BH);
		}
		dev->interrupt=0;
	}
	HMD(("done\n"));
}

static int happy_meal_open(struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;
	int res;

	HMD(("happy_meal_open: "));

	/* On SBUS Quattro QFE cards, all hme interrupts are concentrated
	 * into a single source which we register handling at probe time.
	 */
	if((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) == HFLAG_QUATTRO) {
		hp->qfe_parent->irq_status[hp->qfe_ent] = &hp->gregs->stat;
		goto after_request_irq;
	}
#ifndef __sparc_v9__
	if(sparc_cpu_model == sun4c) {
		if(request_irq(dev->irq, &sun4c_happy_meal_interrupt,
			       SA_SHIRQ, "HAPPY MEAL", (void *) dev)) {
			HMD(("EAGAIN\n"));
			printk(KERN_ERR "happy meal: Can't order irq %d to go.\n", dev->irq);
			return -EAGAIN;
		}
	} else if (sparc_cpu_model == sun4d) {
		if(request_irq(dev->irq, &sun4d_happy_meal_interrupt,
			       SA_SHIRQ, "HAPPY MEAL", (void *) dev)) {
			HMD(("EAGAIN\n"));
			printk(KERN_ERR "happy_meal(SBUS): Can't order irq %s to go.\n",
			       __irq_itoa(dev->irq));
			return -EAGAIN;
		}
	} else
#endif
#ifdef CONFIG_PCI
	if(hp->happy_flags & HFLAG_PCI) {
		if(request_irq(dev->irq, &pci_happy_meal_interrupt,
			       SA_SHIRQ, "HAPPY MEAL (PCI)", dev)) {
		HMD(("EAGAIN\n"));
		printk(KERN_ERR "happy_meal(PCI: Can't order irq %s to go.\n",
		       __irq_itoa(dev->irq));
			return -EAGAIN;
		}
	} else
#endif
	if(request_irq(dev->irq, &happy_meal_interrupt,
		       SA_SHIRQ, "HAPPY MEAL", (void *)dev)) {
		HMD(("EAGAIN\n"));
		printk(KERN_ERR "happy_meal(SBUS): Can't order irq %s to go.\n",
		       __irq_itoa(dev->irq));
		return -EAGAIN;
	}

after_request_irq:
	HMD(("to happy_meal_init\n"));
	res = happy_meal_init(hp, 0);
	if(!res) {
		MOD_INC_USE_COUNT;
	}
	return res;
}

static int happy_meal_close(struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;

	happy_meal_stop(hp, hp->gregs);
	happy_meal_clean_rings(hp);

	/* If auto-negotiation timer is running, kill it. */
	del_timer(&hp->happy_timer);

	/* On Quattro QFE cards, all hme interrupts are concentrated
	 * into a single source which we register handling at probe
	 * time and never unregister.
	 */
	if((hp->happy_flags & (HFLAG_QUATTRO|HFLAG_PCI)) != HFLAG_QUATTRO) {
		free_irq(dev->irq, (void *)dev);
	} else {
		/* Zap the status register pointer. */
		hp->qfe_parent->irq_status[hp->qfe_ent] = NULL;
	}

	MOD_DEC_USE_COUNT;
	return 0;
}

#ifdef SXDEBUG
#define SXD(x) printk x
#else
#define SXD(x)
#endif

static int happy_meal_start_xmit(struct sk_buff *skb, struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;
	int len, entry;

	if(test_and_set_bit(0, (void *) &dev->tbusy) != 0) {
		int tickssofar = jiffies - dev->trans_start;
	    
		if (tickssofar >= 40) {
			printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
			hp->net_stats.tx_errors++;
			tx_dump_log();
			printk (KERN_ERR "%s: Happy Status %08x TX[%08x:%08x]\n", dev->name,
				hme_read32(hp, &hp->gregs->stat),
				hme_read32(hp, &hp->etxregs->cfg),
				hme_read32(hp, &hp->bigmacregs->tx_cfg));
			happy_meal_init(hp, 0);
			dev->tbusy = 0;
			dev->trans_start = jiffies;
		} else
			tx_add_log(hp, TXLOG_ACTION_TXMIT|TXLOG_ACTION_TBUSY, 0);
		return 1;
	}

	if(!TX_BUFFS_AVAIL(hp)) {
		tx_add_log(hp, TXLOG_ACTION_TXMIT|TXLOG_ACTION_NBUFS, 0);
		return 1;
	}
#ifdef __sparc_v9__
	if ((unsigned long)(skb->data + skb->len) >= MAX_DMA_ADDRESS) {
		struct sk_buff *new_skb = skb_copy(skb, GFP_DMA | GFP_ATOMIC);
		if (!new_skb)
			return 1;
		dev_kfree_skb(skb);
		skb = new_skb;
	}
#endif
	len = skb->len;
	entry = hp->tx_new;

	SXD(("SX<l[%d]e[%d]>", len, entry));
#ifdef NEED_DMA_SYNCHRONIZATION
	mmu_sync_dma(kva_to_hva(hp, skb->data),
		     skb->len, hp->happy_sbus_dev->my_bus);
#endif
	hp->tx_skbs[entry] = skb;
	hp->happy_block->happy_meal_txd[entry].tx_addr = kva_to_hva(hp, skb->data);
	hp->happy_block->happy_meal_txd[entry].tx_flags =
		(TXFLAG_OWN | TXFLAG_SOP | TXFLAG_EOP | (len & TXFLAG_SIZE));
	hp->tx_new = NEXT_TX(entry);

	/* Get it going. */
	dev->trans_start = jiffies;
	hme_write32(hp, &hp->etxregs->tx_pnding, ETX_TP_DMAWAKEUP);

	if(TX_BUFFS_AVAIL(hp))
		dev->tbusy = 0;

	tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
	return 0;
}

#ifdef CONFIG_PCI
static int pci_happy_meal_start_xmit(struct sk_buff *skb, struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;
	int len, entry;

	if(test_and_set_bit(0, (void *) &dev->tbusy) != 0) {
		int tickssofar = jiffies - dev->trans_start;
	    
		if (tickssofar >= 40) {
			unsigned long flags;

			printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);

			save_and_cli(flags);
			tx_dump_log();
			tx_dump_ring(hp);
			restore_flags(flags);

			hp->net_stats.tx_errors++;
			happy_meal_init(hp, 0);
			dev->tbusy = 0;
			dev->trans_start = jiffies;
		} else
			tx_add_log(hp, TXLOG_ACTION_TXMIT|TXLOG_ACTION_TBUSY, 0);
		return 1;
	}

	if(!TX_BUFFS_AVAIL(hp)) {
		tx_add_log(hp, TXLOG_ACTION_TXMIT|TXLOG_ACTION_NBUFS, 0);
		return 1;
	}
	len = skb->len;
	entry = hp->tx_new;

	SXD(("SX<l[%d]e[%d]>", len, entry));
	hp->tx_skbs[entry] = skb;
	pcihme_write_txd(&hp->happy_block->happy_meal_txd[entry],
			 (TXFLAG_OWN|TXFLAG_SOP|TXFLAG_EOP|(len & TXFLAG_SIZE)),
			 (u32) virt_to_bus((volatile void *)skb->data));
	hp->tx_new = NEXT_TX(entry);

	/* Get it going. */
	dev->trans_start = jiffies;
	writel(ETX_TP_DMAWAKEUP, (unsigned long)&hp->etxregs->tx_pnding);

	if(TX_BUFFS_AVAIL(hp))
		dev->tbusy = 0;

	tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
	return 0;
}
#endif

#ifndef __sparc_v9__
static int sun4c_happy_meal_start_xmit(struct sk_buff *skb, struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;
	struct hmeal_buffers *hbufs = hp->sun4c_buffers;
	__u32 txbuf_dvma, hbufs_dvma = hp->s4c_buf_dvma;
	unsigned char *txbuf;
	int len, entry;

	if(dev->tbusy) {
		int tickssofar = jiffies - dev->trans_start;
	    
		if (tickssofar < 40) {
			return 1;
		} else {
			printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
			hp->net_stats.tx_errors++;
			happy_meal_init(hp, 0);
			dev->tbusy = 0;
			dev->trans_start = jiffies;
			return 0;
		}
	}

	if(test_and_set_bit(0, (void *) &dev->tbusy) != 0) {
		printk(KERN_ERR "happy meal: Transmitter access conflict.\n");
		return 1;
	}

	if(!TX_BUFFS_AVAIL(hp))
		return 1;

	len = skb->len;
	entry = hp->tx_new;

	txbuf = &hbufs->tx_buf[entry][0];
	memcpy(txbuf, skb->data, len);

	SXD(("SX<l[%d]e[%d]>", len, entry));
	txbuf_dvma = hbufs_dvma + hbuf_offset(tx_buf, entry);
	hp->happy_block->happy_meal_txd[entry].tx_addr = txbuf_dvma;
	hp->happy_block->happy_meal_txd[entry].tx_flags =
		(TXFLAG_OWN | TXFLAG_SOP | TXFLAG_EOP | (len & TXFLAG_SIZE));
	hp->tx_new = NEXT_TX(entry);

	/* Get it going. */
	dev->trans_start = jiffies;
	hp->etxregs->tx_pnding = ETX_TP_DMAWAKEUP;

	dev_kfree_skb(skb);

	if(TX_BUFFS_AVAIL(hp))
		dev->tbusy = 0;

	return 0;
}

static int sun4d_happy_meal_start_xmit(struct sk_buff *skb, struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;
	int len, entry;
	__u32 va;

	if(test_and_set_bit(0, (void *) &dev->tbusy) != 0) {
		int tickssofar = jiffies - dev->trans_start;
	    
		if (tickssofar >= 40) {
			printk (KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
			hp->net_stats.tx_errors++;
			tx_dump_log();
			printk (KERN_ERR "%s: Happy Status %08x TX[%08x:%08x]\n", dev->name,
				hme_read32(hp, &hp->gregs->stat),
				hme_read32(hp, &hp->etxregs->cfg),
				hme_read32(hp, &hp->bigmacregs->tx_cfg));
			happy_meal_init(hp, 0);
			dev->tbusy = 0;
			dev->trans_start = jiffies;
		} else
			tx_add_log(hp, TXLOG_ACTION_TXMIT|TXLOG_ACTION_TBUSY, 0);
		return 1;
	}

	if(!TX_BUFFS_AVAIL(hp)) {
		tx_add_log(hp, TXLOG_ACTION_TXMIT|TXLOG_ACTION_NBUFS, 0);
		return 1;
	}
	len = skb->len;
	entry = hp->tx_new;

	SXD(("SX<l[%d]e[%d]>", len, entry));
	hp->tx_skbs[entry] = skb;
	va = (__u32)hp->sun4d_buffers + (RX_RING_SIZE + entry) * PAGE_SIZE;
	hp->happy_block->happy_meal_txd[entry].tx_addr = 
		iounit_map_dma_page(va, skb->data, hp->happy_sbus_dev->my_bus);
	hp->happy_block->happy_meal_txd[entry].tx_flags =
		(TXFLAG_OWN | TXFLAG_SOP | TXFLAG_EOP | (len & TXFLAG_SIZE));
	hp->tx_new = NEXT_TX(entry);

	/* Get it going. */
	dev->trans_start = jiffies;
	hme_write32(hp, &hp->etxregs->tx_pnding, ETX_TP_DMAWAKEUP);

	if(TX_BUFFS_AVAIL(hp))
		dev->tbusy = 0;

	tx_add_log(hp, TXLOG_ACTION_TXMIT, 0);
	return 0;
}
#endif

static struct net_device_stats *happy_meal_get_stats(struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;

	happy_meal_get_counters(hp, hp->bigmacregs);
	return &hp->net_stats;
}

static void happy_meal_set_multicast(struct device *dev)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;
	struct hmeal_bigmacregs *bregs = hp->bigmacregs;
	struct dev_mc_list *dmi = dev->mc_list;
	char *addrs;
	int i, j, bit, byte;
	u32 crc, poly = CRC_POLYNOMIAL_LE;

	/* Lock out others. */
	set_bit(0, (void *) &dev->tbusy);

	if((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 64)) {
		hme_write32(hp, &bregs->htable0, 0xffff);
		hme_write32(hp, &bregs->htable1, 0xffff);
		hme_write32(hp, &bregs->htable2, 0xffff);
		hme_write32(hp, &bregs->htable3, 0xffff);
	} else if(dev->flags & IFF_PROMISC) {
		hme_write32(hp, &bregs->rx_cfg,
			    hme_read32(hp, &bregs->rx_cfg) | BIGMAC_RXCFG_PMISC);
	} else {
		u16 hash_table[4];

		for(i = 0; i < 4; i++)
			hash_table[i] = 0;

		for(i = 0; i < dev->mc_count; i++) {
			addrs = dmi->dmi_addr;
			dmi = dmi->next;

			if(!(*addrs & 1))
				continue;

			crc = 0xffffffffU;
			for(byte = 0; byte < 6; byte++) {
				for(bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
					int test;

					test = ((bit ^ crc) & 0x01);
					crc >>= 1;
					if(test)
						crc = crc ^ poly;
				}
			}
			crc >>= 26;
			hash_table[crc >> 4] |= 1 << (crc & 0xf);
		}
		hme_write32(hp, &bregs->htable0, hash_table[0]);
		hme_write32(hp, &bregs->htable1, hash_table[1]);
		hme_write32(hp, &bregs->htable2, hash_table[2]);
		hme_write32(hp, &bregs->htable3, hash_table[3]);
	}

	/* Let us get going again. */
	dev->tbusy = 0;
}

/* Ethtool support... */
static int happy_meal_ioctl(struct device *dev,
			    struct ifreq *rq, int cmd)
{
	struct happy_meal *hp = (struct happy_meal *) dev->priv;
	struct ethtool_cmd *ep_user = (struct ethtool_cmd *) rq->ifr_data;
	struct ethtool_cmd ecmd;

	if(cmd != SIOCETHTOOL)
		return -EOPNOTSUPP;
	if(copy_from_user(&ecmd, ep_user, sizeof(ecmd)))
		return -EFAULT;

	if(ecmd.cmd == SPARC_ETH_GSET) {
		ecmd.supported =
			(SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
			 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
			 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);

		/* XXX hardcoded stuff for now */
		ecmd.port = PORT_TP; /* XXX no MII support */
		ecmd.transceiver = XCVR_INTERNAL; /* XXX no external xcvr support */
		ecmd.phy_address = 0; /* XXX fixed PHYAD */

		/* Record PHY settings. */
		hp->sw_bmcr = happy_meal_tcvr_read(hp, hp->tcvregs, DP83840_BMCR);
		hp->sw_lpa = happy_meal_tcvr_read(hp, hp->tcvregs, DP83840_LPA);
		if(hp->sw_bmcr & BMCR_ANENABLE) {
			ecmd.autoneg = AUTONEG_ENABLE;
			ecmd.speed =
				(hp->sw_lpa & (LPA_100HALF | LPA_100FULL)) ?
				SPEED_100 : SPEED_10;
			if(ecmd.speed == SPEED_100)
				ecmd.duplex =
					(hp->sw_lpa & (LPA_100FULL)) ?
					DUPLEX_FULL : DUPLEX_HALF;
			else
				ecmd.duplex =
					(hp->sw_lpa & (LPA_10FULL)) ?
					DUPLEX_FULL : DUPLEX_HALF;
		} else {
			ecmd.autoneg = AUTONEG_DISABLE;
			ecmd.speed =
				(hp->sw_bmcr & BMCR_SPEED100) ?
				SPEED_100 : SPEED_10;
			ecmd.duplex =
				(hp->sw_bmcr & BMCR_FULLDPLX) ?
				DUPLEX_FULL : DUPLEX_HALF;
		}
		if(copy_to_user(ep_user, &ecmd, sizeof(ecmd)))
			return -EFAULT;
		return 0;
	} else if(ecmd.cmd == SPARC_ETH_SSET) {
		if(!capable(CAP_NET_ADMIN))
			return -EPERM;

		/* Verify the settings we care about. */
		if(ecmd.autoneg != AUTONEG_ENABLE &&
		   ecmd.autoneg != AUTONEG_DISABLE)
			return -EINVAL;
		if(ecmd.autoneg == AUTONEG_DISABLE &&
		   ((ecmd.speed != SPEED_100 &&
		     ecmd.speed != SPEED_10) ||
		    (ecmd.duplex != DUPLEX_HALF &&
		     ecmd.duplex != DUPLEX_FULL)))
			return -EINVAL;

		/* Ok, do it to it. */
		del_timer(&hp->happy_timer);
		happy_meal_begin_auto_negotiation(hp,
						  hp->tcvregs,
						  &ecmd);

		return 0;
	} else
		return -EOPNOTSUPP;
}

void __init quattro_get_ranges(struct quattro *qp)
{
	int err;

	err = prom_getproperty(qp->quattro_sbus_dev->prom_node,
			       "ranges",
			       (char *)&qp->ranges[0],
			       sizeof(qp->ranges));
	if(err == 0 || err == -1) {
		qp->nranges = 0;
		return;
	}
	qp->nranges = (err / sizeof(struct linux_prom_ranges));
}

static void __init quattro_apply_ranges(struct quattro *qp, struct happy_meal *hp)
{
	struct linux_sbus_device *sdev = hp->happy_sbus_dev;
	int rng;

	for(rng = 0; rng < qp->nranges; rng++) {
		struct linux_prom_ranges *rngp = &qp->ranges[rng];
		int reg;

		for(reg = 0; reg < 5; reg++) {
			if(sdev->reg_addrs[reg].which_io ==
			   rngp->ot_child_space)
				break;
		}
		if(reg == 5)
			continue;

		sdev->reg_addrs[reg].which_io = rngp->ot_parent_space;
		sdev->reg_addrs[reg].phys_addr += rngp->ot_parent_base;
	}
}

/* Given a happy meal sbus device, find it's quattro parent.
 * If none exist, allocate and return a new one.
 *
 * Return NULL on failure.
 */
static struct quattro * __init quattro_sbus_find(struct linux_sbus_device *goal_sdev)
{
	struct linux_sbus *sbus;
	struct linux_sbus_device *sdev;
	struct quattro *qp;

	if(qfe_sbus_list == NULL)
		goto found;

	for(qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
		for(sdev = qp->quattro_sbus_dev;
		    sdev != NULL;
		    sdev = sdev->next) {
			if(sdev == goal_sdev)
				return qp;
		}
	}
	for_each_sbus(sbus) {
		for_each_sbusdev(sdev, sbus) {
			if(sdev->child != NULL) {
				struct linux_sbus_device *p;

				for(p = sdev->child; p != NULL; p = p->next)
					if(p == goal_sdev)
						goto found;
			}
		}
	}

	/* Cannot find quattro parent, fail. */
	return NULL;

found:
	qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
	if(qp != NULL) {
		int i;

		for(i = 0; i < 4; i++) {
			qp->irq_status[i] = NULL;
			qp->happy_meals[i] = NULL;
		}
		qp->handler = NULL;
		qp->quattro_sbus_dev = goal_sdev;
#ifdef CONFIG_PCI
		qp->quattro_pci_dev = NULL;
#endif
		qp->next = qfe_sbus_list;
		qfe_sbus_list = qp;
		quattro_get_ranges(qp);
	}
	return qp;
}

#ifdef CONFIG_PCI
static struct quattro * __init quattro_pci_find(struct pci_dev *pdev)
{
	struct pci_dev *bdev = pdev->bus->self;
	struct quattro *qp;

	if (!bdev) return NULL;
	for(qp = qfe_pci_list; qp != NULL; qp = qp->next) {
		if(qp->quattro_pci_dev == bdev)
			return qp;
	}
	qp = kmalloc(sizeof(struct quattro), GFP_KERNEL);
	if(qp != NULL) {
		int i;

		for(i = 0; i < 4; i++) {
			qp->irq_status[i] = NULL;
			qp->happy_meals[i] = NULL;
		}
		qp->handler = NULL;
		qp->quattro_sbus_dev = NULL;
		qp->quattro_pci_dev = bdev;
		qp->next = qfe_pci_list;
		qfe_pci_list = qp;

		/* No range tricks necessary on PCI. */
		qp->nranges = 0;
	}
	return qp;
}
#endif

/* After all quattro cards have been probed, we call these functions
 * to register the IRQ handlers.
 */
static void __init quattro_sbus_register_irqs(void)
{
	struct quattro *qp;

	for(qp = qfe_sbus_list; qp != NULL; qp = qp->next) {
		int err;

		/* Set the handler. */
#ifndef __sparc_v9__
		if(sparc_cpu_model == sun4c)
			qp->handler = sun4c_happy_meal_interrupt;
		else if(sparc_cpu_model == sun4d)
			qp->handler = sun4d_happy_meal_interrupt;
		else
#endif
#ifdef CONFIG_PCI
		if(qp->quattro_pci_dev != NULL)
			panic("QFE: PCI qfe in sbus_register_irqs!");
		else
#endif
			qp->handler = happy_meal_interrupt;

		err = request_irq(qp->quattro_sbus_dev->irqs[0],
				  quattro_sbus_interrupt,
				  SA_SHIRQ, "Quattro",
				  qp);
		if(err != 0) {
			printk(KERN_ERR "Quattro: Fatal IRQ registery error %d.\n", err);
			panic("QFE request irq");
		}
	}
}

static unsigned hme_version_printed = 0;

static int __init happy_meal_ether_init(struct device *dev, struct linux_sbus_device *sdev, int is_qfe)
{
	struct quattro *qp = NULL;
	struct happy_meal *hp;
	int i, qfe_slot = -1;

	if(is_qfe) {
		qp = quattro_sbus_find(sdev);
		if(qp == NULL)
			return ENODEV;
		for(qfe_slot = 0; qfe_slot < 4; qfe_slot++)
			if(qp->happy_meals[qfe_slot] == NULL)
				break;
		if(qfe_slot == 4)
			return ENODEV;
	}
	if(dev == NULL) {
		dev = init_etherdev(0, sizeof(struct happy_meal));
	} else {
		dev->priv = kmalloc(sizeof(struct happy_meal), GFP_KERNEL);
		if(dev->priv == NULL)
			return -ENOMEM;
	}
	if(hme_version_printed++ == 0)
		printk(KERN_INFO "%s", version);

	if(qfe_slot != -1)
		printk(KERN_INFO "%s: Quattro HME slot %d (SBUS) 10/100baseT Ethernet ",
		       dev->name, qfe_slot);
	else
		printk(KERN_INFO "%s: HAPPY MEAL (SBUS) 10/100baseT Ethernet ",
		       dev->name);

	dev->base_addr = (long) sdev;

	/* XXX Check for local-mac-address property on Quattro... -DaveM */
	/* Quattro local-mac-address... */
	if(qfe_slot != -1 && prom_getproplen(sdev->prom_node,"local-mac-address")==6)
		prom_getproperty(sdev->prom_node,"local-mac-address",dev->dev_addr,6);
	else
		memcpy(dev->dev_addr,idprom->id_ethaddr,6);
	for(i = 0; i < 6; i++)
		printk("%2.2x%c",
		       dev->dev_addr[i], i == 5 ? ' ' : ':');
	printk("\n");

	hp = (struct happy_meal *) dev->priv;
	memset(hp, 0, sizeof(*hp));

	hp->happy_sbus_dev = sdev;
#ifdef CONFIG_PCI
	hp->happy_pci_dev = NULL;
#endif

	if(sdev->num_registers != 5) {
		printk(KERN_ERR "happymeal: Device does not have 5 regs, it has %d.\n",
		       sdev->num_registers);
		printk(KERN_ERR "happymeal: Would you like that for here or to go?\n");
		return ENODEV;
	}

	if(qp != NULL) {
		hp->qfe_parent = qp;
		hp->qfe_ent = qfe_slot;
		qp->happy_meals[qfe_slot] = dev;
		quattro_apply_ranges(qp, hp);
	}

	prom_apply_sbus_ranges(sdev->my_bus, &sdev->reg_addrs[0],
			       sdev->num_registers, sdev);
	hp->gregs = sparc_alloc_io(sdev->reg_addrs[0].phys_addr, 0,
				   sizeof(struct hmeal_gregs),
				   "Happy Meal Global Regs",
				   sdev->reg_addrs[0].which_io, 0);
	if(!hp->gregs) {
		printk(KERN_ERR "happymeal: Cannot map Happy Meal global registers.\n");
		return ENODEV;
	}

	hp->etxregs = sparc_alloc_io(sdev->reg_addrs[1].phys_addr, 0,
				     sizeof(struct hmeal_etxregs),
				     "Happy Meal MAC TX Regs",
				     sdev->reg_addrs[1].which_io, 0);
	if(!hp->etxregs) {
		printk(KERN_ERR "happymeal: Cannot map Happy Meal MAC Transmit registers.\n");
		return ENODEV;
	}

	hp->erxregs = sparc_alloc_io(sdev->reg_addrs[2].phys_addr, 0,
				     sizeof(struct hmeal_erxregs),
				     "Happy Meal MAC RX Regs",
				     sdev->reg_addrs[2].which_io, 0);
	if(!hp->erxregs) {
		printk(KERN_ERR "happymeal: Cannot map Happy Meal MAC Receive registers.\n");
		return ENODEV;
	}

	hp->bigmacregs = sparc_alloc_io(sdev->reg_addrs[3].phys_addr, 0,
					sizeof(struct hmeal_bigmacregs),
					"Happy Meal BIGMAC Regs",
					sdev->reg_addrs[3].which_io, 0);
	if(!hp->bigmacregs) {
		printk(KERN_ERR "happymeal: Cannot map Happy Meal BIGMAC registers.\n");
		return ENODEV;
	}

	hp->tcvregs = sparc_alloc_io(sdev->reg_addrs[4].phys_addr, 0,
				     sizeof(struct hmeal_tcvregs),
				     "Happy Meal Tranceiver Regs",
				     sdev->reg_addrs[4].which_io, 0);
	if(!hp->tcvregs) {
		printk(KERN_ERR "happymeal: Cannot map Happy Meal Tranceiver registers.\n");
		return ENODEV;
	}

	hp->hm_revision = prom_getintdefault(sdev->prom_node, "hm-rev", 0xff);
	if(hp->hm_revision == 0xff)
		hp->hm_revision = 0xa0;

	/* Now enable the feature flags we can. */
	if(hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
		hp->happy_flags = HFLAG_20_21;
	else if(hp->hm_revision != 0xa0)
		hp->happy_flags = HFLAG_NOT_A0;

	if(qp != NULL)
		hp->happy_flags |= HFLAG_QUATTRO;

	/* Get the supported DVMA burst sizes from our Happy SBUS. */
	hp->happy_bursts = prom_getintdefault(hp->happy_sbus_dev->my_bus->prom_node,
					      "burst-sizes", 0x00);

	hp->happy_block = (struct hmeal_init_block *)
		sparc_dvma_malloc(PAGE_SIZE, "Happy Meal Init Block",
				  &hp->hblock_dvma);

#ifndef __sparc_v9__
	if(sparc_cpu_model == sun4c)
		hp->sun4c_buffers = (struct hmeal_buffers *)
		    sparc_dvma_malloc(sizeof(struct hmeal_buffers), "Happy Meal Bufs",
				      &hp->s4c_buf_dvma);
	else if (sparc_cpu_model == sun4d)
		hp->sun4d_buffers = (struct hmeal_buffers *)
		    iounit_map_dma_init(hp->happy_sbus_dev->my_bus,
		    			(RX_RING_SIZE + TX_RING_SIZE) * PAGE_SIZE);
	else
#endif
		hp->sun4c_buffers = 0;

	/* Force check of the link first time we are brought up. */
	hp->linkcheck = 0;

	/* Force timer state to 'asleep' with count of zero. */
	hp->timer_state = asleep;
	hp->timer_ticks = 0;

	/* Grrr, Happy Meal comes up by default not advertising
	 * full duplex 100baseT capabilities, fix this.
	 */
	happy_meal_set_initial_advertisement(hp);

	init_timer(&hp->happy_timer);

	hp->dev = dev;
	dev->open = &happy_meal_open;
	dev->stop = &happy_meal_close;
#ifndef __sparc_v9__	
	if(sparc_cpu_model == sun4c)
		dev->hard_start_xmit = &sun4c_happy_meal_start_xmit;
	else if (sparc_cpu_model == sun4d)
		dev->hard_start_xmit = &sun4d_happy_meal_start_xmit;
	else
#endif
		dev->hard_start_xmit = &happy_meal_start_xmit;
	dev->get_stats = &happy_meal_get_stats;
	dev->set_multicast_list = &happy_meal_set_multicast;
	dev->do_ioctl = &happy_meal_ioctl;

	dev->irq = sdev->irqs[0];
	dev->dma = 0;
	ether_setup(dev);
#ifdef MODULE
	/* We are home free at this point, link us in to the happy
	 * module device list.
	 */
	dev->ifindex = dev_new_index();
	hp->next_module = root_happy_dev;
	root_happy_dev = hp;
#endif
	return 0;
}

#ifdef CONFIG_PCI
static int __init happy_meal_pci_init(struct device *dev, struct pci_dev *pdev)
{
	struct quattro *qp = NULL;
	struct pcidev_cookie *pcp;
	struct happy_meal *hp;
	unsigned long hpreg_base;
	unsigned short pci_command;
	int i, node, qfe_slot = -1;
	char prom_name[64];

	/* Now make sure pci_dev cookie is there. */
	pcp = pdev->sysdata;
	if(pcp == NULL || pcp->prom_node == -1) {
		printk(KERN_ERR "happymeal(PCI): Some PCI device info missing\n");
		return ENODEV;
	}
	node = pcp->prom_node;
	
	prom_getstring(node, "name", prom_name, sizeof(prom_name));
	if (!strcmp(prom_name, "SUNW,qfe") || !strcmp(prom_name, "qfe")) {
		qp = quattro_pci_find(pdev);
		if(qp == NULL)
			return ENODEV;
		for(qfe_slot = 0; qfe_slot < 4; qfe_slot++)
			if(qp->happy_meals[qfe_slot] == NULL)
				break;
		if(qfe_slot == 4)
			return ENODEV;
	}
	if(dev == NULL) {
		dev = init_etherdev(0, sizeof(struct happy_meal));
	} else {
		dev->priv = kmalloc(sizeof(struct happy_meal), GFP_KERNEL);
		if(dev->priv == NULL)
			return -ENOMEM;
	}
	if(hme_version_printed++ == 0)
		printk(KERN_INFO "%s", version);

	if (!qfe_slot) {
		prom_name[0] = 0;
		if (!strncmp(dev->name, "eth", 3)) {
			int i = simple_strtoul(dev->name + 3, NULL, 10);
			sprintf(prom_name, "-%d", i + 3);
		}
		printk(KERN_INFO "%s%s: Quattro HME (PCI/CheerIO) 10/100baseT Ethernet ", dev->name, prom_name);
		if (qp->quattro_pci_dev->vendor == PCI_VENDOR_ID_DEC &&
		    qp->quattro_pci_dev->device == PCI_DEVICE_ID_DEC_21153)
			printk("DEC 21153 PCI Bridge\n");
		else
			printk("unknown bridge %04x.%04x\n", 
				qp->quattro_pci_dev->vendor, qp->quattro_pci_dev->device);
	}
	if(qfe_slot != -1)
		printk(KERN_INFO "%s: Quattro HME slot %d (PCI/CheerIO) 10/100baseT Ethernet ",
		       dev->name, qfe_slot);
	else
		printk(KERN_INFO "%s: HAPPY MEAL (PCI/CheerIO) 10/100BaseT Ethernet ",
		       dev->name);

	dev->base_addr = (long) pdev;

	hp = (struct happy_meal *)dev->priv;
	memset(hp, 0, sizeof(*hp));

	hp->happy_sbus_dev = NULL;
	hp->happy_pci_dev = pdev;

	if(qp != NULL) {
		hp->qfe_parent = qp;
		hp->qfe_ent = qfe_slot;
		qp->happy_meals[qfe_slot] = dev;
	}		

	hpreg_base = pdev->base_address[0];
	if((hpreg_base & PCI_BASE_ADDRESS_SPACE) != PCI_BASE_ADDRESS_SPACE_MEMORY) {
		printk(KERN_ERR "happymeal(PCI): Cannot find proper PCI device base address.\n");
		return ENODEV;
	}
	hpreg_base &= PCI_BASE_ADDRESS_MEM_MASK;

	if (qfe_slot != -1 && prom_getproplen(node, "local-mac-address") == 6)
		prom_getproperty(node, "local-mac-address", dev->dev_addr, 6);
	else
		memcpy(dev->dev_addr, idprom->id_ethaddr, 6);
	for(i = 0; i < 6; i++)
		printk("%2.2x%c", dev->dev_addr[i], i == 5 ? ' ' : ':');

	printk("\n");

	/* Layout registers. */
	hp->gregs      = (struct hmeal_gregs *)		(hpreg_base + 0x0000);
	hp->etxregs    = (struct hmeal_etxregs *)	(hpreg_base + 0x2000);
	hp->erxregs    = (struct hmeal_erxregs *)	(hpreg_base + 0x4000);
	hp->bigmacregs = (struct hmeal_bigmacregs *)	(hpreg_base + 0x6000);
	hp->tcvregs    = (struct hmeal_tcvregs *)	(hpreg_base + 0x7000);

	hp->hm_revision = prom_getintdefault(node, "hm-rev", 0xff);
	if(hp->hm_revision == 0xff)
		hp->hm_revision = 0xa0;

	/* Now enable the feature flags we can. */
	if(hp->hm_revision == 0x20 || hp->hm_revision == 0x21)
		hp->happy_flags = HFLAG_20_21;
	else if(hp->hm_revision != 0xa0)
		hp->happy_flags = HFLAG_NOT_A0;

	if(qp != NULL)
		hp->happy_flags |= HFLAG_QUATTRO;

	/* And of course, indicate this is PCI. */
	hp->happy_flags |= HFLAG_PCI;

	/* Assume PCI happy meals can handle all burst sizes. */
	hp->happy_bursts = DMA_BURSTBITS;

	hp->happy_block = (struct hmeal_init_block *) get_free_page(GFP_DMA);
	if(!hp->happy_block) {
		printk(KERN_ERR "happymeal(PCI): Cannot get hme init block.\n");
		return ENODEV;
	}

	hp->hblock_dvma = (u32) virt_to_bus(hp->happy_block);
#ifndef __sparc_v9__
	/* This case we currently need to use 'sparc_alloc_io' */
	hp->happy_block = sparc_alloc_io (hp->hblock_dvma, NULL, 
					  PAGE_SIZE, "sunhme", 0, 0);
#endif
	hp->sun4c_buffers = 0;

	hp->linkcheck = 0;
	hp->timer_state = asleep;
	hp->timer_ticks = 0;
	happy_meal_set_initial_advertisement(hp);

	init_timer(&hp->happy_timer);

	hp->dev = dev;
	dev->open = &happy_meal_open;
	dev->stop = &happy_meal_close;
	dev->hard_start_xmit = &pci_happy_meal_start_xmit;
	dev->get_stats = &happy_meal_get_stats;
	dev->set_multicast_list = &happy_meal_set_multicast;
	dev->do_ioctl = &happy_meal_ioctl;
	dev->irq = pdev->irq;
	dev->dma = 0;
	ether_setup(dev);

	/* If we don't do this, nothing works. */
	pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
	pci_command |= PCI_COMMAND_MASTER;
	pci_write_config_word(pdev, PCI_COMMAND, pci_command);

	/* Set the latency timer and cache line size as well,
	 * PROM leaves it at zero.
	 */
	{
#if 1
		unsigned char latency_timer = 64;
#else
		unsigned char min_gnt, latency_timer;

		pci_read_config_byte(pdev, PCI_MIN_GNT, &min_gnt);
		if (min_gnt == 0)
			latency_timer = 64;
		else
			latency_timer = ((min_gnt << 3) & 0xff);
#endif
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, latency_timer);
	}
#ifdef __sparc_v9__
	/* NOTE: Cache line size is in 32-bit word units. */
	pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x10);
#endif

#ifdef MODULE
	/* We are home free at this point, link us in to the happy
	 * module device list.
	 */
	dev->ifindex = dev_new_index();
	hp->next_module = root_happy_dev;
	root_happy_dev = hp;
#endif
	return 0;
}
#endif

int __init happy_meal_probe(struct device *dev)
{
	struct linux_sbus *bus;
	struct linux_sbus_device *sdev = 0;
	static int called = 0;
	int cards = 0, v;

	if(called)
		return ENODEV;
	called++;

	for_each_sbus(bus) {
		for_each_sbusdev(sdev, bus) {
			if(cards)
				dev = NULL;
			if(!strcmp(sdev->prom_name, "SUNW,hme")) {
				cards++;
				if((v = happy_meal_ether_init(dev, sdev, 0)))
					return v;
			} else if(!strcmp(sdev->prom_name, "qfe") ||
				  !strcmp(sdev->prom_name, "SUNW,qfe")) {
				cards++;
				if((v = happy_meal_ether_init(dev, sdev, 1)))
					return v;
			}
		}
	}
	if(cards != 0)
		quattro_sbus_register_irqs();
#ifdef CONFIG_PCI
	if(pci_present()) {
		struct pci_dev *pdev;

		pdev = pci_find_device(PCI_VENDOR_ID_SUN,
				       PCI_DEVICE_ID_SUN_HAPPYMEAL, 0);
		while (pdev) {
			if(cards)
				dev = NULL;
			cards++;
			if((v = happy_meal_pci_init(dev, pdev)))
				return v;
			pdev = pci_find_device(PCI_VENDOR_ID_SUN,
					       PCI_DEVICE_ID_SUN_HAPPYMEAL,
					       pdev);
		}
	}
#endif
	if(!cards)
		return ENODEV;
	return 0;
}

#ifdef MODULE

int
init_module(void)
{
	root_happy_dev = NULL;
	return happy_meal_probe(NULL);
}

void
cleanup_module(void)
{
	struct happy_meal *sunshine;

	/* No need to check MOD_IN_USE, as sys_delete_module() checks. */
	while (root_happy_dev) {
		struct happy_meal *hp = root_happy_dev;
		sunshine = root_happy_dev->next_module;

		sparc_free_io(hp->gregs, sizeof(struct hmeal_gregs));
		sparc_free_io(hp->etxregs, sizeof(struct hmeal_etxregs));
		sparc_free_io(hp->erxregs, sizeof(struct hmeal_erxregs));
		sparc_free_io(hp->bigmacregs, sizeof(struct hmeal_bigmacregs));
		sparc_free_io(hp->tcvregs, sizeof(struct hmeal_tcvregs));
#ifndef __sparc_v9__
		if (sparc_cpu_model == sun4d)
			iounit_map_dma_finish(hp->happy_sbus_dev->my_bus,
					      (__u32)hp->sun4d_buffers, (RX_RING_SIZE + TX_RING_SIZE) * PAGE_SIZE);
#endif		
		unregister_netdev(hp->dev);
		kfree(hp->dev);
		root_happy_dev = sunshine;
	}
}

#endif /* MODULE */