summaryrefslogtreecommitdiff
path: root/MdeModulePkg/Bus/Pci/UndiRuntimeDxe/E100b.c
blob: c0600c5e5ae203502ecdc04a8d507dcfcf600855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
/** @file
  Provides basic function upon network adapter card.

Copyright (c) 2006, Intel Corporation
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "Undi32.h"

UINT8 basic_config_cmd[22] = {
                    22,        0x08,
                    0,           0,
                    0, (UINT8)0x80,
                    0x32,        0x03,
                    1,            0,
                    0x2E,           0,
                    0x60,           0,
                    (UINT8)0xf2,        0x48,
                    0,        0x40,
                    (UINT8)0xf2, (UINT8)0x80, // 0x40=Force full-duplex
                    0x3f,       0x05,
};

//
// How to wait for the command unit to accept a command.
// Typically this takes 0 ticks.
//
#define wait_for_cmd_done(cmd_ioaddr) \
{                      \
  INT16 wait_count = 2000;              \
  while ((InByte (AdapterInfo, cmd_ioaddr) != 0) && --wait_count >= 0)  \
    DelayIt (AdapterInfo, 10);  \
  if (wait_count == 0) \
    DelayIt (AdapterInfo, 50);    \
}


/**
  This function calls the MemIo callback to read a byte from the device's
  address space
  Since UNDI3.0 uses the TmpMemIo function (instead of the callback routine)
  which also takes the UniqueId parameter (as in UNDI3.1 spec) we don't have
  to make undi3.0 a special case

  @param  Port                            Which port to read from.

  @retval Results                         The data read from the port.

**/
// TODO:    AdapterInfo - add argument and description to function comment
UINT8
InByte (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT32            Port
  )
{
  UINT8 Results;

  (*AdapterInfo->Mem_Io) (
    AdapterInfo->Unique_ID,
    PXE_MEM_READ,
    1,
    (UINT64)Port,
    (UINT64) (UINTN) &Results
    );
  return Results;
}


/**
  This function calls the MemIo callback to read a word from the device's
  address space
  Since UNDI3.0 uses the TmpMemIo function (instead of the callback routine)
  which also takes the UniqueId parameter (as in UNDI3.1 spec) we don't have
  to make undi3.0 a special case

  @param  Port                            Which port to read from.

  @retval Results                         The data read from the port.

**/
// TODO:    AdapterInfo - add argument and description to function comment
UINT16
InWord (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT32            Port
  )
{
  UINT16  Results;

  (*AdapterInfo->Mem_Io) (
    AdapterInfo->Unique_ID,
    PXE_MEM_READ,
    2,
    (UINT64)Port,
    (UINT64)(UINTN)&Results
    );
  return Results;
}


/**
  This function calls the MemIo callback to read a dword from the device's
  address space
  Since UNDI3.0 uses the TmpMemIo function (instead of the callback routine)
  which also takes the UniqueId parameter (as in UNDI3.1 spec) we don't have
  to make undi3.0 a special case

  @param  Port                            Which port to read from.

  @retval Results                         The data read from the port.

**/
// TODO:    AdapterInfo - add argument and description to function comment
UINT32
InLong (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT32            Port
  )
{
  UINT32  Results;

  (*AdapterInfo->Mem_Io) (
    AdapterInfo->Unique_ID,
    PXE_MEM_READ,
    4,
    (UINT64)Port,
    (UINT64)(UINTN)&Results
    );
  return Results;
}


/**
  This function calls the MemIo callback to write a byte from the device's
  address space
  Since UNDI3.0 uses the TmpMemIo function (instead of the callback routine)
  which also takes the UniqueId parameter (as in UNDI3.1 spec) we don't have
  to make undi3.0 a special case

  @param  Data                            Data to write to Port.
  @param  Port                            Which port to write to.

  @return none

**/
// TODO:    AdapterInfo - add argument and description to function comment
VOID
OutByte (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT8             Data,
  IN UINT32            Port
  )
{
  UINT8 Val;

  Val = Data;
  (*AdapterInfo->Mem_Io) (
     AdapterInfo->Unique_ID,
     PXE_MEM_WRITE,
     1,
     (UINT64)Port,
     (UINT64)(UINTN)(UINTN)&Val
     );
  return ;
}


/**
  This function calls the MemIo callback to write a word from the device's
  address space
  Since UNDI3.0 uses the TmpMemIo function (instead of the callback routine)
  which also takes the UniqueId parameter (as in UNDI3.1 spec) we don't have
  to make undi3.0 a special case

  @param  Data                            Data to write to Port.
  @param  Port                            Which port to write to.

  @return none

**/
// TODO:    AdapterInfo - add argument and description to function comment
VOID
OutWord (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT16            Data,
  IN UINT32            Port
  )
{
  UINT16  Val;

  Val = Data;
  (*AdapterInfo->Mem_Io) (
     AdapterInfo->Unique_ID,
     PXE_MEM_WRITE,
     2,
     (UINT64)Port,
     (UINT64)(UINTN)&Val
     );
  return ;
}


/**
  This function calls the MemIo callback to write a dword from the device's
  address space
  Since UNDI3.0 uses the TmpMemIo function (instead of the callback routine)
  which also takes the UniqueId parameter (as in UNDI3.1 spec) we don't have
  to make undi3.0 a special case

  @param  Data                            Data to write to Port.
  @param  Port                            Which port to write to.

  @return none

**/
// TODO:    AdapterInfo - add argument and description to function comment
VOID
OutLong (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT32            Data,
  IN UINT32            Port
  )
{
  UINT32  Val;

  Val = Data;
  (*AdapterInfo->Mem_Io) (
     AdapterInfo->Unique_ID,
     PXE_MEM_WRITE,
     4,
     (UINT64)Port,
     (UINT64)(UINTN)&Val
     );
  return ;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  MemAddr                         TODO: add argument description
  @param  Size                            TODO: add argument description
  @param  Direction                       TODO: add argument description
  @param  MappedAddr                      TODO: add argument description

  @return TODO: add return values

**/
UINTN
MapIt (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT64            MemAddr,
  IN UINT32            Size,
  IN UINT32            Direction,
  OUT UINT64           MappedAddr
  )
{
  UINT64  *PhyAddr;

  PhyAddr = (UINT64 *) (UINTN) MappedAddr;
  //
  // mapping is different for theold and new NII protocols
  //
  if (AdapterInfo->VersionFlag == 0x30) {
    if (AdapterInfo->Virt2Phys_30 == (VOID *) NULL) {
      *PhyAddr = (UINT64) AdapterInfo->MemoryPtr;
    } else {
      (*AdapterInfo->Virt2Phys_30) (MemAddr, (UINT64) (UINTN) PhyAddr);
    }

    if (*PhyAddr > FOUR_GIGABYTE) {
      return PXE_STATCODE_INVALID_PARAMETER;
    }
  } else {
    if (AdapterInfo->Map_Mem == (VOID *) NULL) {
      //
      // this UNDI cannot handle addresses beyond 4 GB without a map routine
      //
      if (MemAddr > FOUR_GIGABYTE) {
        return PXE_STATCODE_INVALID_PARAMETER;
      } else {
        *PhyAddr = MemAddr;
      }
    } else {
      (*AdapterInfo->Map_Mem) (
        AdapterInfo->Unique_ID,
        MemAddr,
        Size,
        Direction,
        MappedAddr
        );
    }
  }

  return PXE_STATCODE_SUCCESS;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  MemAddr                         TODO: add argument description
  @param  Size                            TODO: add argument description
  @param  Direction                       TODO: add argument description
  @param  MappedAddr                      TODO: add argument description

  @return TODO: add return values

**/
VOID
UnMapIt (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT64            MemAddr,
  IN UINT32            Size,
  IN UINT32            Direction,
  IN UINT64            MappedAddr
  )
{
  if (AdapterInfo->VersionFlag > 0x30) {
    //
    // no mapping service
    //
    if (AdapterInfo->UnMap_Mem != (VOID *) NULL) {
      (*AdapterInfo->UnMap_Mem) (
        AdapterInfo->Unique_ID,
        MemAddr,
        Size,
        Direction,
        MappedAddr
        );

    }
  }

  return ;
}


/**

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..


**/
// TODO:    MicroSeconds - add argument and description to function comment
VOID
DelayIt (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  UINT16               MicroSeconds
  )
{
  if (AdapterInfo->VersionFlag == 0x30) {
    (*AdapterInfo->Delay_30) (MicroSeconds);
  } else {
    (*AdapterInfo->Delay) (AdapterInfo->Unique_ID, MicroSeconds);
  }
}


/**

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..


**/
// TODO:    flag - add argument and description to function comment
VOID
BlockIt (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  UINT32               flag
  )
{
  if (AdapterInfo->VersionFlag == 0x30) {
    (*AdapterInfo->Block_30) (flag);
  } else {
    (*AdapterInfo->Block) (AdapterInfo->Unique_ID, flag);
  }
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT8
Load_Base_Regs (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  //
  // we will use the linear (flat) memory model and fill our base registers
  // with 0's so that the entire physical address is our offset
  //
  //
  // we reset the statistics totals here because this is where we are loading stats addr
  //
  AdapterInfo->RxTotals = 0;
  AdapterInfo->TxTotals = 0;

  //
  // Load the statistics block address.
  //
  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);
  OutLong (AdapterInfo, (UINT32) AdapterInfo->stat_phy_addr, AdapterInfo->ioaddr + SCBPointer);
  OutByte (AdapterInfo, CU_STATSADDR, AdapterInfo->ioaddr + SCBCmd);
  AdapterInfo->statistics->done_marker = 0;

  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);
  OutLong (AdapterInfo, 0, AdapterInfo->ioaddr + SCBPointer);
  OutByte (AdapterInfo, RX_ADDR_LOAD, AdapterInfo->ioaddr + SCBCmd);

  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);
  OutLong (AdapterInfo, 0, AdapterInfo->ioaddr + SCBPointer);
  OutByte (AdapterInfo, CU_CMD_BASE, AdapterInfo->ioaddr + SCBCmd);

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  cmd_ptr                         TODO: add argument description

  @return TODO: add return values

**/
UINT8
IssueCB (
  NIC_DATA_INSTANCE *AdapterInfo,
  TxCB              *cmd_ptr
  )
{
  UINT16  status;

  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);

  //
  // read the CU status, if it is idle, write the address of cb_ptr
  // in the scbpointer and issue a cu_start,
  // if it is suspended, remove the suspend bit in the previous command
  // block and issue a resume
  //
  // Ensure that the CU Active Status bit is not on from previous CBs.
  //
  status = InWord (AdapterInfo, AdapterInfo->ioaddr + SCBStatus);

  //
  // Skip acknowledging the interrupt if it is not already set
  //

  //
  // ack only the cna the integer
  //
  if ((status & SCB_STATUS_CNA) != 0) {
    OutWord (AdapterInfo, SCB_STATUS_CNA, AdapterInfo->ioaddr + SCBStatus);

  }

  if ((status & SCB_STATUS_CU_MASK) == SCB_STATUS_CU_IDLE) {
    //
    // give a cu_start
    //
    OutLong (AdapterInfo, cmd_ptr->PhysTCBAddress, AdapterInfo->ioaddr + SCBPointer);
    OutByte (AdapterInfo, CU_START, AdapterInfo->ioaddr + SCBCmd);
  } else {
    //
    // either active or suspended, give a resume
    //

    cmd_ptr->PrevTCBVirtualLinkPtr->cb_header.command &= ~(CmdSuspend | CmdIntr);
    OutByte (AdapterInfo, CU_RESUME, AdapterInfo->ioaddr + SCBCmd);
  }

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT8
Configure (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  //
  // all command blocks are of TxCB format
  //
  TxCB  *cmd_ptr;
  UINT8 *data_ptr;
  volatile INT16 Index;
  UINT8 my_filter;

  cmd_ptr   = GetFreeCB (AdapterInfo);
  data_ptr  = (UINT8 *) (&cmd_ptr->PhysTBDArrayAddres);

  //
  // start the config data right after the command header
  //
  for (Index = 0; Index < sizeof (basic_config_cmd); Index++) {
    data_ptr[Index] = basic_config_cmd[Index];
  }

  my_filter = (UINT8) ((AdapterInfo->Rx_Filter & PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS) ? 1 : 0);
  my_filter = (UINT8) (my_filter | ((AdapterInfo->Rx_Filter & PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST) ? 0 : 2));

  data_ptr[15]  = (UINT8) (data_ptr[15] | my_filter);
  data_ptr[19]  = (UINT8) (AdapterInfo->Duplex ? 0xC0 : 0x80);
  data_ptr[21]  = (UINT8) ((AdapterInfo->Rx_Filter & PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST) ? 0x0D : 0x05);

  //
  // check if we have to use the AUI port instead
  //
  if ((AdapterInfo->PhyRecord[0] & 0x8000) != 0) {
    data_ptr[15] |= 0x80;
    data_ptr[8] = 0;
  }

  BlockIt (AdapterInfo, TRUE);
  cmd_ptr->cb_header.command = CmdSuspend | CmdConfigure;

  IssueCB (AdapterInfo, cmd_ptr);
  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);

  BlockIt (AdapterInfo, FALSE);

  CommandWaitForCompletion (cmd_ptr, AdapterInfo);

  //
  // restore the cb values for tx
  //
  cmd_ptr->PhysTBDArrayAddres = cmd_ptr->PhysArrayAddr;
  cmd_ptr->ByteCount = cmd_ptr->Threshold = cmd_ptr->TBDCount = 0;
  //
  // fields beyond the immediatedata are assumed to be safe
  // add the CB to the free list again
  //
  SetFreeCB (AdapterInfo, cmd_ptr);
  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT8
E100bSetupIAAddr (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  //
  // all command blocks are of TxCB format
  //
  TxCB    *cmd_ptr;
  UINT16  *data_ptr;
  UINT16  *eaddrs;

  eaddrs    = (UINT16 *) AdapterInfo->CurrentNodeAddress;

  cmd_ptr   = GetFreeCB (AdapterInfo);
  data_ptr  = (UINT16 *) (&cmd_ptr->PhysTBDArrayAddres);

  //
  // AVOID a bug (?!) here by marking the command already completed.
  //
  cmd_ptr->cb_header.command  = (CmdSuspend | CmdIASetup);
  cmd_ptr->cb_header.status   = 0;
  data_ptr[0]                 = eaddrs[0];
  data_ptr[1]                 = eaddrs[1];
  data_ptr[2]                 = eaddrs[2];

  BlockIt (AdapterInfo, TRUE);
  IssueCB (AdapterInfo, cmd_ptr);
  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);
  BlockIt (AdapterInfo, FALSE);

  CommandWaitForCompletion (cmd_ptr, AdapterInfo);

  //
  // restore the cb values for tx
  //
  cmd_ptr->PhysTBDArrayAddres = cmd_ptr->PhysArrayAddr;
  cmd_ptr->ByteCount = cmd_ptr->Threshold = cmd_ptr->TBDCount = 0;
  //
  // fields beyond the immediatedata are assumed to be safe
  // add the CB to the free list again
  //
  SetFreeCB (AdapterInfo, cmd_ptr);
  return 0;
}


/**
  Instructs the NIC to stop receiving packets.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..


**/
VOID
StopRU (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  if (AdapterInfo->Receive_Started) {

    //
    // Todo: verify that we must wait for previous command completion.
    //
    wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);

    //
    // Disable interrupts, and stop the chip's Rx process.
    //
    OutWord (AdapterInfo, INT_MASK, AdapterInfo->ioaddr + SCBCmd);
    OutWord (AdapterInfo, INT_MASK | RX_ABORT, AdapterInfo->ioaddr + SCBCmd);

    AdapterInfo->Receive_Started = FALSE;
  }

  return ;
}


/**
  Instructs the NIC to start receiving packets.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..

  @retval 0                               Successful
  @retval -1                              Already Started

**/
INT8
StartRU (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{

  if (AdapterInfo->Receive_Started) {
    //
    // already started
    //
    return -1;
  }

  AdapterInfo->cur_rx_ind = 0;
  AdapterInfo->Int_Status = 0;

  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);

  OutLong (AdapterInfo, (UINT32) AdapterInfo->rx_phy_addr, AdapterInfo->ioaddr + SCBPointer);
  OutByte (AdapterInfo, RX_START, AdapterInfo->ioaddr + SCBCmd);

  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);

  AdapterInfo->Receive_Started = TRUE;
  return 0;
}


/**
  Configures the chip.  This routine expects the NIC_DATA_INSTANCE structure to be filled in.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..

  @retval 0                               Successful
  @retval PXE_STATCODE_NOT_ENOUGH_MEMORY  Insufficient length of locked memory
  @retval other                           Failure initializing chip

**/
UINTN
E100bInit (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  PCI_CONFIG_HEADER *CfgHdr;
  UINTN             stat;
  UINTN             rx_size;
  UINTN             tx_size;

  if (AdapterInfo->MemoryLength < MEMORY_NEEDED) {
    return PXE_STATCODE_NOT_ENOUGH_MEMORY;
  }

  stat = MapIt (
          AdapterInfo,
          AdapterInfo->MemoryPtr,
          AdapterInfo->MemoryLength,
          TO_AND_FROM_DEVICE,
          (UINT64)(UINTN) &AdapterInfo->Mapped_MemoryPtr
          );

  if (stat != 0) {
    return stat;
  }

  CfgHdr = (PCI_CONFIG_HEADER *) &(AdapterInfo->Config[0]);

  //
  // fill in the ioaddr, int... from the config space
  //
  AdapterInfo->int_num = CfgHdr->int_line;

  //
  // we don't need to validate integer number, what if they don't want to assign one?
  // if (AdapterInfo->int_num == 0 || AdapterInfo->int_num == 0xff)
  // return PXE_STATCODE_DEVICE_FAILURE;
  //
  AdapterInfo->ioaddr       = 0;
  AdapterInfo->VendorID     = CfgHdr->VendorID;
  AdapterInfo->DeviceID     = CfgHdr->DeviceID;
  AdapterInfo->RevID        = CfgHdr->RevID;
  AdapterInfo->SubVendorID  = CfgHdr->SubVendorID;
  AdapterInfo->SubSystemID  = CfgHdr->SubSystemID;
  AdapterInfo->flash_addr   = 0;

  //
  // Read the station address EEPROM before doing the reset.
  // Perhaps this should even be done before accepting the device,
  // then we wouldn't have a device name with which to report the error.
  //
  if (E100bReadEepromAndStationAddress (AdapterInfo) != 0) {
    return PXE_STATCODE_DEVICE_FAILURE;

  }
  //
  // ## calculate the buffer #s depending on memory given
  // ## calculate the rx and tx ring pointers
  //

  AdapterInfo->TxBufCnt       = TX_BUFFER_COUNT;
  AdapterInfo->RxBufCnt       = RX_BUFFER_COUNT;
  rx_size                     = (AdapterInfo->RxBufCnt * sizeof (RxFD));
  tx_size                     = (AdapterInfo->TxBufCnt * sizeof (TxCB));
  AdapterInfo->rx_ring        = (RxFD *) (UINTN) (AdapterInfo->MemoryPtr);
  AdapterInfo->tx_ring        = (TxCB *) (UINTN) (AdapterInfo->MemoryPtr + rx_size);
  AdapterInfo->statistics     = (struct speedo_stats *) (UINTN) (AdapterInfo->MemoryPtr + rx_size + tx_size);

  AdapterInfo->rx_phy_addr    = AdapterInfo->Mapped_MemoryPtr;
  AdapterInfo->tx_phy_addr    = AdapterInfo->Mapped_MemoryPtr + rx_size;
  AdapterInfo->stat_phy_addr  = AdapterInfo->tx_phy_addr + tx_size;

  //
  // auto detect.
  //
  AdapterInfo->PhyAddress     = 0xFF;
  AdapterInfo->Rx_Filter            = PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
  AdapterInfo->Receive_Started      = FALSE;
  AdapterInfo->mcast_list.list_len  = 0;
  return InitializeChip (AdapterInfo);
}


/**
  Sets the interrupt state for the NIC.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..

  @retval 0                               Successful

**/
UINT8
E100bSetInterruptState (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  //
  // don't set receive interrupt if receiver is disabled...
  //
  UINT16  cmd_word;

  if ((AdapterInfo->int_mask & PXE_OPFLAGS_INTERRUPT_RECEIVE) != 0) {
    cmd_word = InWord (AdapterInfo, AdapterInfo->ioaddr + SCBCmd);
    cmd_word &= ~INT_MASK;
    OutWord (AdapterInfo, cmd_word, AdapterInfo->ioaddr + SCBCmd);
  } else {
    //
    // disable ints, should not be given for SW Int.
    //
    OutWord (AdapterInfo, INT_MASK, AdapterInfo->ioaddr + SCBCmd);
  }

  if ((AdapterInfo->int_mask & PXE_OPFLAGS_INTERRUPT_SOFTWARE) != 0) {
    //
    // reset the bit in our mask, it is only one time!!
    //
    AdapterInfo->int_mask &= ~(PXE_OPFLAGS_INTERRUPT_SOFTWARE);
    cmd_word = InWord (AdapterInfo, AdapterInfo->ioaddr + SCBCmd);
    cmd_word |= DRVR_INT;
    OutWord (AdapterInfo, cmd_word, AdapterInfo->ioaddr + SCBCmd);
  }

  return 0;
}
//
// we are not going to disable broadcast for the WOL's sake!
//

/**
  Instructs the NIC to start receiving packets.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on.. new_filter
                                              - cpb                             -
                                          cpbsize                         -

  @retval 0                               Successful
  @retval -1                              Already Started

**/
UINTN
E100bSetfilter (
  NIC_DATA_INSTANCE *AdapterInfo,
  UINT16            new_filter,
  UINT64            cpb,
  UINT32            cpbsize
  )
{
  PXE_CPB_RECEIVE_FILTERS *mc_list = (PXE_CPB_RECEIVE_FILTERS *) (UINTN)cpb;
  UINT16                  cfg_flt;
  UINT16                  old_filter;
  UINT16                  Index;
  UINT16                  Index2;
  UINT16                  mc_count;
  TxCB                    *cmd_ptr;
  struct MC_CB_STRUCT     *data_ptr;
  UINT16                  mc_byte_cnt;

  old_filter  = AdapterInfo->Rx_Filter;

  //
  // only these bits need a change in the configuration
  // actually change in bcast requires configure but we ignore that change
  //
  cfg_flt = PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS |
            PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;

  if ((old_filter & cfg_flt) != (new_filter & cfg_flt)) {
    XmitWaitForCompletion (AdapterInfo);

    if (AdapterInfo->Receive_Started) {
      StopRU (AdapterInfo);
    }

    AdapterInfo->Rx_Filter = (UINT8) (new_filter | PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST);
    Configure (AdapterInfo);
  }

  //
  // check if mcast setting changed
  //
  if ( ((new_filter & PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST) !=
       (old_filter & PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST) ) ||
       (mc_list != NULL) ) {


    if (mc_list != NULL) {
      mc_count = AdapterInfo->mcast_list.list_len = (UINT16) (cpbsize / PXE_MAC_LENGTH);

      for (Index = 0; (Index < mc_count && Index < MAX_MCAST_ADDRESS_CNT); Index++) {
        for (Index2 = 0; Index2 < PXE_MAC_LENGTH; Index2++) {
          AdapterInfo->mcast_list.mc_list[Index][Index2] = mc_list->MCastList[Index][Index2];
        }
      }
    }

    //
    // are we setting the list or resetting??
    //
    if ((new_filter & PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST) != 0) {
      //
      // we are setting a new list!
      //
      mc_count = AdapterInfo->mcast_list.list_len;
      //
      // count should be the actual # of bytes in the list
      // so multiply this with 6
      //
      mc_byte_cnt = (UINT16) ((mc_count << 2) + (mc_count << 1));
      AdapterInfo->Rx_Filter |= PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST;
    } else {
      //
      // disabling the list in the NIC.
      //
      mc_byte_cnt = mc_count = 0;
      AdapterInfo->Rx_Filter &= (~PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST);
    }

    //
    // before issuing any new command!
    //
    XmitWaitForCompletion (AdapterInfo);

    if (AdapterInfo->Receive_Started) {
      StopRU (AdapterInfo);

    }

    cmd_ptr = GetFreeCB (AdapterInfo);
    if (cmd_ptr == NULL) {
      return PXE_STATCODE_QUEUE_FULL;
    }
    //
    // fill the command structure and issue
    //
    data_ptr = (struct MC_CB_STRUCT *) (&cmd_ptr->PhysTBDArrayAddres);
    //
    // first 2 bytes are the count;
    //
    data_ptr->count = mc_byte_cnt;
    for (Index = 0; Index < mc_count; Index++) {
      for (Index2 = 0; Index2 < PXE_HWADDR_LEN_ETHER; Index2++) {
        data_ptr->m_list[Index][Index2] = AdapterInfo->mcast_list.mc_list[Index][Index2];
      }
    }

    cmd_ptr->cb_header.command  = CmdSuspend | CmdMulticastList;
    cmd_ptr->cb_header.status   = 0;

    BlockIt (AdapterInfo, TRUE);
    IssueCB (AdapterInfo, cmd_ptr);
    wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);

    BlockIt (AdapterInfo, FALSE);

    CommandWaitForCompletion (cmd_ptr, AdapterInfo);

    cmd_ptr->PhysTBDArrayAddres = cmd_ptr->PhysArrayAddr;
    cmd_ptr->ByteCount = cmd_ptr->Threshold = cmd_ptr->TBDCount = 0;
    //
    // fields beyond the immediatedata are assumed to be safe
    // add the CB to the free list again
    //
    SetFreeCB (AdapterInfo, cmd_ptr);
  }

  if (new_filter != 0) {
    //
    // enable unicast and start the RU
    //
    AdapterInfo->Rx_Filter = (UINT8) (AdapterInfo->Rx_Filter | (new_filter | PXE_OPFLAGS_RECEIVE_FILTER_UNICAST));
    StartRU (AdapterInfo);
  } else {
    //
    // may be disabling everything!
    //
    if (AdapterInfo->Receive_Started) {
      StopRU (AdapterInfo);
    }

    AdapterInfo->Rx_Filter |= (~PXE_OPFLAGS_RECEIVE_FILTER_UNICAST);
  }

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  cpb                             TODO: add argument description
  @param  opflags                         TODO: add argument description

  @return TODO: add return values

**/
UINTN
E100bTransmit (
  NIC_DATA_INSTANCE *AdapterInfo,
  UINT64            cpb,
  UINT16            opflags
  )
{
  PXE_CPB_TRANSMIT_FRAGMENTS  *tx_ptr_f;
  PXE_CPB_TRANSMIT            *tx_ptr_1;
  TxCB                        *tcb_ptr;
  UINT64                      Tmp_ptr;
  UINTN                       stat;
  INT32                       Index;
  UINT16                      wait_sec;

  tx_ptr_1  = (PXE_CPB_TRANSMIT *) (UINTN) cpb;
  tx_ptr_f  = (PXE_CPB_TRANSMIT_FRAGMENTS *) (UINTN) cpb;

  //
  // stop reentrancy here
  //
  if (AdapterInfo->in_transmit) {
    return PXE_STATCODE_BUSY;

  }

  AdapterInfo->in_transmit = TRUE;

  //
  // Prevent interrupts from changing the Tx ring from underneath us.
  //
  // Calculate the Tx descriptor entry.
  //
  if ((tcb_ptr = GetFreeCB (AdapterInfo)) == NULL) {
    AdapterInfo->in_transmit = FALSE;
    return PXE_STATCODE_QUEUE_FULL;
  }

  AdapterInfo->TxTotals++;

  tcb_ptr->cb_header.command  = (CmdSuspend | CmdTx | CmdTxFlex);
  tcb_ptr->cb_header.status   = 0;

  //
  // no immediate data, set EOF in the ByteCount
  //
  tcb_ptr->ByteCount = 0x8000;

  //
  // The data region is always in one buffer descriptor, Tx FIFO
  // threshold of 256.
  // 82557 multiplies the threashold value by 8, so give 256/8
  //
  tcb_ptr->Threshold = 32;
  if ((opflags & PXE_OPFLAGS_TRANSMIT_FRAGMENTED) != 0) {

    if (tx_ptr_f->FragCnt > MAX_XMIT_FRAGMENTS) {
      SetFreeCB (AdapterInfo, tcb_ptr);
      AdapterInfo->in_transmit = FALSE;
      return PXE_STATCODE_INVALID_PARAMETER;
    }

    tcb_ptr->TBDCount = (UINT8) tx_ptr_f->FragCnt;

    for (Index = 0; Index < tx_ptr_f->FragCnt; Index++) {
      stat = MapIt (
              AdapterInfo,
              tx_ptr_f->FragDesc[Index].FragAddr,
              tx_ptr_f->FragDesc[Index].FragLen,
              TO_DEVICE,
              (UINT64)(UINTN) &Tmp_ptr
              );
      if (stat != 0) {
        SetFreeCB (AdapterInfo, tcb_ptr);
        AdapterInfo->in_transmit = FALSE;
        return PXE_STATCODE_INVALID_PARAMETER;
      }

      tcb_ptr->TBDArray[Index].phys_buf_addr  = (UINT32) Tmp_ptr;
      tcb_ptr->TBDArray[Index].buf_len        = tx_ptr_f->FragDesc[Index].FragLen;
    }

    tcb_ptr->free_data_ptr = tx_ptr_f->FragDesc[0].FragAddr;

  } else {
    //
    // non fragmented case
    //
    tcb_ptr->TBDCount = 1;
    stat = MapIt (
            AdapterInfo,
            tx_ptr_1->FrameAddr,
            tx_ptr_1->DataLen + tx_ptr_1->MediaheaderLen,
            TO_DEVICE,
            (UINT64)(UINTN) &Tmp_ptr
            );
    if (stat != 0) {
      SetFreeCB (AdapterInfo, tcb_ptr);
      AdapterInfo->in_transmit = FALSE;
      return PXE_STATCODE_INVALID_PARAMETER;
    }

    tcb_ptr->TBDArray[0].phys_buf_addr  = (UINT32) (Tmp_ptr);
    tcb_ptr->TBDArray[0].buf_len        = tx_ptr_1->DataLen + tx_ptr_1->MediaheaderLen;
    tcb_ptr->free_data_ptr              = tx_ptr_1->FrameAddr;
  }

  //
  // must wait for previous command completion only if it was a non-transmit
  //
  BlockIt (AdapterInfo, TRUE);
  IssueCB (AdapterInfo, tcb_ptr);
  BlockIt (AdapterInfo, FALSE);

  //
  // see if we need to wait for completion here
  //
  if ((opflags & PXE_OPFLAGS_TRANSMIT_BLOCK) != 0) {
    //
    // don't wait for more than 1 second!!!
    //
    wait_sec = 1000;
    while (tcb_ptr->cb_header.status == 0) {
      DelayIt (AdapterInfo, 10);
      wait_sec--;
      if (wait_sec == 0) {
        break;
      }
    }
    //
    // we need to un-map any mapped buffers here
    //
    if ((opflags & PXE_OPFLAGS_TRANSMIT_FRAGMENTED) != 0) {

      for (Index = 0; Index < tx_ptr_f->FragCnt; Index++) {
        Tmp_ptr = tcb_ptr->TBDArray[Index].phys_buf_addr;
        UnMapIt (
          AdapterInfo,
          tx_ptr_f->FragDesc[Index].FragAddr,
          tx_ptr_f->FragDesc[Index].FragLen,
          TO_DEVICE,
          (UINT64) Tmp_ptr
          );
      }
    } else {
      Tmp_ptr = tcb_ptr->TBDArray[0].phys_buf_addr;
      UnMapIt (
        AdapterInfo,
        tx_ptr_1->FrameAddr,
        tx_ptr_1->DataLen + tx_ptr_1->MediaheaderLen,
        TO_DEVICE,
        (UINT64) Tmp_ptr
        );
    }

    if (tcb_ptr->cb_header.status == 0) {
      SetFreeCB (AdapterInfo, tcb_ptr);
      AdapterInfo->in_transmit = FALSE;
      return PXE_STATCODE_DEVICE_FAILURE;
    }

    SetFreeCB (AdapterInfo, tcb_ptr);
  }
  //
  // CB will be set free later in get_status (or when we run out of xmit buffers
  //
  AdapterInfo->in_transmit = FALSE;

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  cpb                             TODO: add argument description
  @param  db                              TODO: add argument description

  @return TODO: add return values

**/
UINTN
E100bReceive (
  NIC_DATA_INSTANCE *AdapterInfo,
  UINT64            cpb,
  UINT64            db
  )
{
  PXE_CPB_RECEIVE *rx_cpbptr;
  PXE_DB_RECEIVE  *rx_dbptr;
  RxFD            *rx_ptr;
  INT32           status;
  INT32           Index;
  UINT16          pkt_len;
  UINT16          ret_code;
  PXE_FRAME_TYPE  pkt_type;
  UINT16          Tmp_len;
  EtherHeader     *hdr_ptr;
  ret_code  = PXE_STATCODE_NO_DATA;
  pkt_type  = PXE_FRAME_TYPE_NONE;
  status    = InWord (AdapterInfo, AdapterInfo->ioaddr + SCBStatus);
  AdapterInfo->Int_Status = (UINT16) (AdapterInfo->Int_Status | status);
  //
  // acknoledge the interrupts
  //
  OutWord (AdapterInfo, (UINT16) (status & 0xfc00), (UINT32) (AdapterInfo->ioaddr + SCBStatus));

  //
  // include the prev ints as well
  //
  status = AdapterInfo->Int_Status;
  rx_cpbptr = (PXE_CPB_RECEIVE *) (UINTN) cpb;
  rx_dbptr  = (PXE_DB_RECEIVE *) (UINTN) db;

  rx_ptr    = &AdapterInfo->rx_ring[AdapterInfo->cur_rx_ind];

  //
  // be in a loop just in case (we may drop a pkt)
  //
  while ((status = rx_ptr->cb_header.status) & RX_COMPLETE) {

    AdapterInfo->RxTotals++;
    //
    // If we own the next entry, it's a new packet. Send it up.
    //
    if (rx_ptr->forwarded) {
      goto FreeRFD;

    }

    //
    // discard bad frames
    //

    //
    // crc, align, dma overrun, too short, receive error (v22 no coll)
    //
    if ((status & 0x0D90) != 0) {
      goto FreeRFD;

    }

    //
    // make sure the status is OK
    //
    if ((status & 0x02000) == 0) {
      goto FreeRFD;
    }

    pkt_len = (UINT16) (rx_ptr->ActualCount & 0x3fff);

    if (pkt_len != 0) {

      Tmp_len = pkt_len;
      if (pkt_len > rx_cpbptr->BufferLen) {
        Tmp_len = (UINT16) rx_cpbptr->BufferLen;
      }

      CopyMem ((INT8 *) (UINTN) rx_cpbptr->BufferAddr, (INT8 *) &rx_ptr->RFDBuffer, Tmp_len);

      hdr_ptr = (EtherHeader *) &rx_ptr->RFDBuffer;
      //
      // fill the CDB and break the loop
      //

      //
      // includes header
      //
      rx_dbptr->FrameLen = pkt_len;
      rx_dbptr->MediaHeaderLen = PXE_MAC_HEADER_LEN_ETHER;

      for (Index = 0; Index < PXE_HWADDR_LEN_ETHER; Index++) {
        if (hdr_ptr->dest_addr[Index] != AdapterInfo->CurrentNodeAddress[Index]) {
          break;
        }
      }

      if (Index >= PXE_HWADDR_LEN_ETHER) {
        pkt_type = PXE_FRAME_TYPE_UNICAST;
      } else {
        for (Index = 0; Index < PXE_HWADDR_LEN_ETHER; Index++) {
          if (hdr_ptr->dest_addr[Index] != AdapterInfo->BroadcastNodeAddress[Index]) {
            break;
          }
        }

        if (Index >= PXE_HWADDR_LEN_ETHER) {
          pkt_type = PXE_FRAME_TYPE_BROADCAST;
        } else {
          if ((hdr_ptr->dest_addr[0] & 1) == 1) {
            //
            // mcast
            //

            pkt_type = PXE_FRAME_TYPE_FILTERED_MULTICAST;
          } else {
            pkt_type = PXE_FRAME_TYPE_PROMISCUOUS;
          }
        }
      }

      rx_dbptr->Type      = pkt_type;
      rx_dbptr->Protocol  = hdr_ptr->type;

      for (Index = 0; Index < PXE_HWADDR_LEN_ETHER; Index++) {
        rx_dbptr->SrcAddr[Index]  = hdr_ptr->src_addr[Index];
        rx_dbptr->DestAddr[Index] = hdr_ptr->dest_addr[Index];
      }

      rx_ptr->forwarded = TRUE;
      //
      // success
      //
      ret_code          = 0;
      Recycle_RFD (AdapterInfo, AdapterInfo->cur_rx_ind);
      AdapterInfo->cur_rx_ind++;
      if (AdapterInfo->cur_rx_ind == AdapterInfo->RxBufCnt) {
        AdapterInfo->cur_rx_ind = 0;
      }
      break;
    }

FreeRFD:
    Recycle_RFD (AdapterInfo, AdapterInfo->cur_rx_ind);
    AdapterInfo->cur_rx_ind++;
    if (AdapterInfo->cur_rx_ind == AdapterInfo->RxBufCnt) {
      AdapterInfo->cur_rx_ind = 0;
    }

    rx_ptr = &AdapterInfo->rx_ring[AdapterInfo->cur_rx_ind];
  }

  if (pkt_type == PXE_FRAME_TYPE_NONE) {
    AdapterInfo->Int_Status &= (~SCB_STATUS_FR);
  }

  status = InWord (AdapterInfo, AdapterInfo->ioaddr + SCBStatus);
  if ((status & SCB_RUS_NO_RESOURCES) != 0) {
    //
    // start the receive unit here!
    // leave all the filled frames,
    //
    SetupReceiveQueues (AdapterInfo);
    OutLong (AdapterInfo, (UINT32) AdapterInfo->rx_phy_addr, AdapterInfo->ioaddr + SCBPointer);
    OutWord (AdapterInfo, RX_START, AdapterInfo->ioaddr + SCBCmd);
    AdapterInfo->cur_rx_ind = 0;
  }

  return ret_code;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
INT16
E100bReadEepromAndStationAddress (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  INT32   Index;
  INT32   Index2;
  UINT16  sum;
  UINT16  eeprom_len;
  UINT8   addr_len;
  UINT16  *eedata;

  eedata    = (UINT16 *) (&AdapterInfo->NVData[0]);

  sum       = 0;
  addr_len  = E100bGetEepromAddrLen (AdapterInfo);

  //
  // in words
  //
  AdapterInfo->NVData_Len = eeprom_len = (UINT16) (1 << addr_len);
  for (Index2 = 0, Index = 0; Index < eeprom_len; Index++) {
    UINT16  value;
    value         = E100bReadEeprom (AdapterInfo, Index, addr_len);
    eedata[Index] = value;
    sum           = (UINT16) (sum + value);
    if (Index < 3) {
      AdapterInfo->PermNodeAddress[Index2++]  = (UINT8) value;
      AdapterInfo->PermNodeAddress[Index2++]  = (UINT8) (value >> 8);
    }
  }

  if (sum != 0xBABA) {
    return -1;
  }

  for (Index = 0; Index < PXE_HWADDR_LEN_ETHER; Index++) {
    AdapterInfo->CurrentNodeAddress[Index] = AdapterInfo->PermNodeAddress[Index];
  }

  for (Index = 0; Index < PXE_HWADDR_LEN_ETHER; Index++) {
    AdapterInfo->BroadcastNodeAddress[Index] = 0xff;
  }

  for (Index = PXE_HWADDR_LEN_ETHER; Index < PXE_MAC_LENGTH; Index++) {
    AdapterInfo->CurrentNodeAddress[Index]    = 0;
    AdapterInfo->PermNodeAddress[Index]       = 0;
    AdapterInfo->BroadcastNodeAddress[Index]  = 0;
  }

  return 0;
}

//
//  CBList is a circular linked list
//  1) When all are free, Tail->next == Head and FreeCount == # allocated
//  2) When none are free, Tail == Head and FreeCount == 0
//  3) when one is free, Tail == Head and Freecount == 1
//  4) First non-Free frame is always at Tail->next
//

/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT8
SetupCBlink (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  TxCB  *head_ptr;
  TxCB  *tail_ptr;
  TxCB  *cur_ptr;
  INT32 Index;
  UINTN array_off;

  cur_ptr   = &(AdapterInfo->tx_ring[0]);
  array_off = (UINTN) (&cur_ptr->TBDArray) - (UINTN) cur_ptr;
  for (Index = 0; Index < AdapterInfo->TxBufCnt; Index++) {
    cur_ptr[Index].cb_header.status   = 0;
    cur_ptr[Index].cb_header.command  = 0;

    cur_ptr[Index].PhysTCBAddress     =
    (UINT32) AdapterInfo->tx_phy_addr + (Index * sizeof (TxCB));

    cur_ptr[Index].PhysArrayAddr      = (UINT32)(cur_ptr[Index].PhysTCBAddress + array_off);
    cur_ptr[Index].PhysTBDArrayAddres = (UINT32)(cur_ptr[Index].PhysTCBAddress + array_off);

    cur_ptr->free_data_ptr = (UINT64) 0;

    if (Index < AdapterInfo->TxBufCnt - 1) {
      cur_ptr[Index].cb_header.link             = cur_ptr[Index].PhysTCBAddress + sizeof (TxCB);
      cur_ptr[Index].NextTCBVirtualLinkPtr      = &cur_ptr[Index + 1];
      cur_ptr[Index + 1].PrevTCBVirtualLinkPtr  = &cur_ptr[Index];
    }
  }

  head_ptr                        = &cur_ptr[0];
  tail_ptr                        = &cur_ptr[AdapterInfo->TxBufCnt - 1];
  tail_ptr->cb_header.link        = head_ptr->PhysTCBAddress;
  tail_ptr->NextTCBVirtualLinkPtr = head_ptr;
  head_ptr->PrevTCBVirtualLinkPtr = tail_ptr;

  AdapterInfo->FreeCBCount        = AdapterInfo->TxBufCnt;
  AdapterInfo->FreeTxHeadPtr      = head_ptr;
  //
  // set tail of the free list, next to this would be either in use
  // or the head itself
  //
  AdapterInfo->FreeTxTailPtr  = tail_ptr;

  AdapterInfo->xmit_done_head = AdapterInfo->xmit_done_tail = 0;

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
TxCB *
GetFreeCB (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  TxCB  *free_cb_ptr;

  //
  // claim any hanging free CBs
  //
  if (AdapterInfo->FreeCBCount <= 1) {
    CheckCBList (AdapterInfo);
  }

  //
  // don't use up the last CB problem if the previous CB that the CU used
  // becomes the last CB we submit because of the SUSPEND bit we set.
  // the CU thinks it was never cleared.
  //

  if (AdapterInfo->FreeCBCount <= 1) {
    return NULL;
  }

  BlockIt (AdapterInfo, TRUE);
  free_cb_ptr                 = AdapterInfo->FreeTxHeadPtr;
  AdapterInfo->FreeTxHeadPtr  = free_cb_ptr->NextTCBVirtualLinkPtr;
  --AdapterInfo->FreeCBCount;
  BlockIt (AdapterInfo, FALSE);
  return free_cb_ptr;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  cb_ptr                          TODO: add argument description

  @return TODO: add return values

**/
VOID
SetFreeCB (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN TxCB              *cb_ptr
  )
{
  //
  // here we assume cb are returned in the order they are taken out
  // and we link the newly freed cb at the tail of free cb list
  //
  cb_ptr->cb_header.status    = 0;
  cb_ptr->free_data_ptr       = (UINT64) 0;

  AdapterInfo->FreeTxTailPtr  = cb_ptr;
  ++AdapterInfo->FreeCBCount;
  return ;
}


/**
  TODO: Add function description

  @param  ind                             TODO: add argument description

  @return TODO: add return values

**/
UINT16
next (
  IN UINT16 ind
  )
{
  UINT16  Tmp;

  Tmp = (UINT16) (ind + 1);
  if (Tmp >= (TX_BUFFER_COUNT << 1)) {
    Tmp = 0;
  }

  return Tmp;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT16
CheckCBList (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  TxCB    *Tmp_ptr;
  UINT16  cnt;

  cnt = 0;
  while (1) {
    Tmp_ptr = AdapterInfo->FreeTxTailPtr->NextTCBVirtualLinkPtr;
    if ((Tmp_ptr->cb_header.status & CMD_STATUS_MASK) != 0) {
      //
      // check if Q is full
      //
      if (next (AdapterInfo->xmit_done_tail) != AdapterInfo->xmit_done_head) {
        AdapterInfo->xmit_done[AdapterInfo->xmit_done_tail] = Tmp_ptr->free_data_ptr;

        UnMapIt (
          AdapterInfo,
          Tmp_ptr->free_data_ptr,
          Tmp_ptr->TBDArray[0].buf_len,
          TO_DEVICE,
          (UINT64) Tmp_ptr->TBDArray[0].phys_buf_addr
          );

        AdapterInfo->xmit_done_tail = next (AdapterInfo->xmit_done_tail);
      }

      SetFreeCB (AdapterInfo, Tmp_ptr);
    } else {
      break;
    }
  }

  return cnt;
}
//
// Description : Initialize the RFD list list by linking each element together
//               in a circular list.  The simplified memory model is used.
//               All data is in the RFD.  The RFDs are linked together and the
//               last one points back to the first one.  When the current RFD
//               is processed (frame received), its EL bit is set and the EL
//               bit in the previous RXFD is cleared.
//               Allocation done during INIT, this is making linked list.
//

/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT8
SetupReceiveQueues (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  RxFD    *rx_ptr;
  RxFD    *tail_ptr;
  UINT16  Index;

  AdapterInfo->cur_rx_ind = 0;
  rx_ptr                  = (&AdapterInfo->rx_ring[0]);

  for (Index = 0; Index < AdapterInfo->RxBufCnt; Index++) {
    rx_ptr[Index].cb_header.status  = 0;
    rx_ptr[Index].cb_header.command = 0;
    rx_ptr[Index].RFDSize           = RX_BUFFER_SIZE;
    rx_ptr[Index].ActualCount       = 0;
    //
    // RBDs not used, simple memory model
    //
    rx_ptr[Index].rx_buf_addr       = (UINT32) (-1);

    //
    // RBDs not used, simple memory model
    //
    rx_ptr[Index].forwarded = FALSE;

    //
    // don't use Tmp_ptr if it is beyond the last one
    //
    if (Index < AdapterInfo->RxBufCnt - 1) {
      rx_ptr[Index].cb_header.link = (UINT32) AdapterInfo->rx_phy_addr + ((Index + 1) * sizeof (RxFD));
    }
  }

  tail_ptr                    = (&AdapterInfo->rx_ring[AdapterInfo->RxBufCnt - 1]);
  tail_ptr->cb_header.link    = (UINT32) AdapterInfo->rx_phy_addr;

  //
  // set the EL bit
  //
  tail_ptr->cb_header.command = 0xC000;
  AdapterInfo->RFDTailPtr = tail_ptr;
  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  rx_index                        TODO: add argument description

  @return TODO: add return values

**/
VOID
Recycle_RFD (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT16            rx_index
  )
{
  RxFD  *rx_ptr;
  RxFD  *tail_ptr;
  //
  // change the EL bit and change the AdapterInfo->RxTailPtr
  // rx_ptr is assumed to be the head of the Q
  // AdapterInfo->rx_forwarded[rx_index] = FALSE;
  //
  rx_ptr                    = &AdapterInfo->rx_ring[rx_index];
  tail_ptr                  = AdapterInfo->RFDTailPtr;
  //
  // set el_bit and suspend bit
  //
  rx_ptr->cb_header.command = 0xc000;
  rx_ptr->cb_header.status    = 0;
  rx_ptr->ActualCount         = 0;
  rx_ptr->forwarded           = FALSE;
  AdapterInfo->RFDTailPtr     = rx_ptr;
  //
  // resetting the el_bit.
  //
  tail_ptr->cb_header.command = 0;
  //
  // check the receive unit, fix if there is any problem
  //
  return ;
}
//
// Serial EEPROM section.
//
//  EEPROM_Ctrl bits.
//
#define EE_SHIFT_CLK  0x01  /* EEPROM shift clock. */
#define EE_CS         0x02  /* EEPROM chip select. */
#define EE_DI         0x04  /* EEPROM chip data in. */
#define EE_WRITE_0    0x01
#define EE_WRITE_1    0x05
#define EE_DO         0x08  /* EEPROM chip data out. */
#define EE_ENB        (0x4800 | EE_CS)

//
// Delay between EEPROM clock transitions.
// This will actually work with no delay on 33Mhz PCI.
//
#define eeprom_delay(nanosec) DelayIt (AdapterInfo, nanosec);

//
// The EEPROM commands include the alway-set leading bit.
//
#define EE_WRITE_CMD  5 // 101b
#define EE_READ_CMD   6 // 110b
#define EE_ERASE_CMD  (7 << 6)

VOID
shift_bits_out (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT16            val,
  IN UINT8             num_bits
  )
/*++

Routine Description:

  TODO: Add function description

Arguments:

  AdapterInfo - TODO: add argument description
  val         - TODO: add argument description
  num_bits    - TODO: add argument description

Returns:

  TODO: add return values

--*/
{
  INT32   Index;
  UINT8   Tmp;
  UINT32  EEAddr;

  EEAddr = AdapterInfo->ioaddr + SCBeeprom;

  for (Index = num_bits; Index >= 0; Index--) {
    INT16 dataval;

    //
    // will be 0 or 4
    //
    dataval = (INT16) ((val & (1 << Index)) ? EE_DI : 0);

    //
    // mask off the data_in bit
    //
    Tmp = (UINT8) (InByte (AdapterInfo, EEAddr) &~EE_DI);
    Tmp = (UINT8) (Tmp | dataval);
    OutByte (AdapterInfo, Tmp, EEAddr);
    eeprom_delay (100);
    //
    // raise the eeprom clock
    //
    OutByte (AdapterInfo, (UINT8) (Tmp | EE_SHIFT_CLK), EEAddr);
    eeprom_delay (150);
    //
    // lower the eeprom clock
    //
    OutByte (AdapterInfo, (UINT8) (Tmp &~EE_SHIFT_CLK), EEAddr);
    eeprom_delay (150);
  }
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT16
shift_bits_in (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT8   Tmp;
  INT32   Index;
  UINT16  retval;
  UINT32  EEAddr;

  EEAddr  = AdapterInfo->ioaddr + SCBeeprom;

  retval  = 0;
  for (Index = 15; Index >= 0; Index--) {
    //
    // raise the clock
    //

    //
    // mask off the data_in bit
    //
    Tmp = InByte (AdapterInfo, EEAddr);
    OutByte (AdapterInfo, (UINT8) (Tmp | EE_SHIFT_CLK), EEAddr);
    eeprom_delay (100);
    Tmp     = InByte (AdapterInfo, EEAddr);
    retval  = (UINT16) ((retval << 1) | ((Tmp & EE_DO) ? 1 : 0));
    //
    // lower the clock
    //
    OutByte (AdapterInfo, (UINT8) (Tmp &~EE_SHIFT_CLK), EEAddr);
    eeprom_delay (100);
  }

  return retval;
}


/**
  This routine sets the EEPROM lockout bit to gain exclusive access to the
  eeprom. the access bit is the most significant bit in the General Control
  Register 2 in the SCB space.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..

  @retval TRUE                            if it got the access
  @retval FALSE                           if it fails to get the exclusive access

**/
BOOLEAN
E100bSetEepromLockOut (
  IN NIC_DATA_INSTANCE  *AdapterInfo
  )
{
  UINTN wait;
  UINT8 tmp;

  if ((AdapterInfo->DeviceID == D102_DEVICE_ID) ||
      (AdapterInfo->RevID >= D102_REVID)) {

    wait = 500;

    while (wait--) {

      tmp = InByte (AdapterInfo, AdapterInfo->ioaddr + SCBGenCtrl2);
      tmp |= GCR2_EEPROM_ACCESS_SEMAPHORE;
      OutByte (AdapterInfo, tmp, AdapterInfo->ioaddr + SCBGenCtrl2);

      DelayIt (AdapterInfo, 50);
      tmp = InByte (AdapterInfo, AdapterInfo->ioaddr + SCBGenCtrl2);

      if (tmp & GCR2_EEPROM_ACCESS_SEMAPHORE) {
        return TRUE;
      }
    }

    return FALSE;
  }

  return TRUE;
}


/**
  This routine Resets the EEPROM lockout bit to giveup access to the
  eeprom. the access bit is the most significant bit in the General Control
  Register 2 in the SCB space.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..

  @return None

**/
VOID
E100bReSetEepromLockOut (
  IN NIC_DATA_INSTANCE  *AdapterInfo
  )
{
  UINT8 tmp;

  if ((AdapterInfo->DeviceID == D102_DEVICE_ID) ||
      (AdapterInfo->RevID >= D102_REVID)) {

    tmp = InByte (AdapterInfo, AdapterInfo->ioaddr + SCBGenCtrl2);
    tmp &= ~(GCR2_EEPROM_ACCESS_SEMAPHORE);
    OutByte (AdapterInfo, tmp, AdapterInfo->ioaddr + SCBGenCtrl2);

    DelayIt (AdapterInfo, 50);
  }
}


/**
  Using the NIC data structure information, read the EEPROM to get a Word of data for the MAC address.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..
  @param  Location                        Word offset into the MAC address to read.
  @param  AddrLen                         Number of bits of address length.

  @retval RetVal                          The word read from the EEPROM.

**/
UINT16
E100bReadEeprom (
  IN NIC_DATA_INSTANCE  *AdapterInfo,
  IN INT32              Location,
  IN UINT8              AddrLen
  )
{
  UINT16  RetVal;
  UINT8   Tmp;

  UINT32  EEAddr;
  UINT16  ReadCmd;

  EEAddr  = AdapterInfo->ioaddr + SCBeeprom;
  ReadCmd = (UINT16) (Location | (EE_READ_CMD << AddrLen));

  RetVal  = 0;

  //
  // get exclusive access to the eeprom first!
  //
  E100bSetEepromLockOut (AdapterInfo);

  //
  // eeprom control reg bits: x,x,x,x,DO,DI,CS,SK
  // to write the opcode+data value out one bit at a time in DI starting at msb
  // and then out a 1 to sk, wait, out 0 to SK and wait
  // repeat this for all the bits to be written
  //

  //
  // 11110010b
  //
  Tmp = (UINT8) (InByte (AdapterInfo, EEAddr) & 0xF2);
  OutByte (AdapterInfo, (UINT8) (Tmp | EE_CS), EEAddr);

  //
  // 3 for the read opcode 110b
  //
  shift_bits_out (AdapterInfo, ReadCmd, (UINT8) (3 + AddrLen));

  //
  // read the eeprom word one bit at a time
  //
  RetVal = shift_bits_in (AdapterInfo);

  //
  // Terminate the EEPROM access and leave eeprom in a clean state.
  //
  Tmp = InByte (AdapterInfo, EEAddr);
  Tmp &= ~(EE_CS | EE_DI);
  OutByte (AdapterInfo, Tmp, EEAddr);

  //
  // raise the clock and lower the eeprom shift clock
  //
  OutByte (AdapterInfo, (UINT8) (Tmp | EE_SHIFT_CLK), EEAddr);
  eeprom_delay (100);

  OutByte (AdapterInfo, (UINT8) (Tmp &~EE_SHIFT_CLK), EEAddr);
  eeprom_delay (100);

  //
  // giveup access to the eeprom
  //
  E100bReSetEepromLockOut (AdapterInfo);

  return RetVal;
}


/**
  Using the NIC data structure information, read the EEPROM to determine how many bits of address length
  this EEPROM is in Words.

  @param  AdapterInfo                     Pointer to the NIC data structure
                                          information which the UNDI driver is
                                          layering on..

  @retval RetVal                          The word read from the EEPROM.

**/
UINT8
E100bGetEepromAddrLen (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT8   Tmp;
  UINT8   AddrLen;
  UINT32  EEAddr;
  //
  // assume 64word eeprom (so,6 bits of address_length)
  //
  UINT16  ReadCmd;

  EEAddr  = AdapterInfo->ioaddr + SCBeeprom;
  ReadCmd = (EE_READ_CMD << 6);

  //
  // get exclusive access to the eeprom first!
  //
  E100bSetEepromLockOut (AdapterInfo);

  //
  // address we are trying to read is 0
  // eeprom control reg bits: x,x,x,x,DO,,DI,,CS,SK
  // to write the opcode+data value out one bit at a time in DI starting at msb
  // and then out a 1 to sk, wait, out 0 to SK and wait
  // repeat this for all the bits to be written
  //
  Tmp = (UINT8) (InByte (AdapterInfo, EEAddr) & 0xF2);

  //
  // enable eeprom access
  //
  OutByte (AdapterInfo, (UINT8) (Tmp | EE_CS), EEAddr);

  //
  // 3 for opcode, 6 for the default address len
  //
  shift_bits_out (AdapterInfo, ReadCmd, (UINT8) (3 + 6));

  //
  // (in case of a 64 word eeprom).
  // read the "dummy zero" from EE_DO to say that the address we wrote
  // (six 0s) is accepted, write more zeros (until 8) to get a "dummy zero"
  //

  //
  // assume the smallest
  //
  AddrLen = 6;
  Tmp     = InByte (AdapterInfo, EEAddr);
  while ((AddrLen < 8) && ((Tmp & EE_DO) != 0)) {
    OutByte (AdapterInfo, (UINT8) (Tmp &~EE_DI), EEAddr);
    eeprom_delay (100);

    //
    // raise the eeprom clock
    //
    OutByte (AdapterInfo, (UINT8) (Tmp | EE_SHIFT_CLK), EEAddr);
    eeprom_delay (150);

    //
    // lower the eeprom clock
    //
    OutByte (AdapterInfo, (UINT8) (Tmp &~EE_SHIFT_CLK), EEAddr);
    eeprom_delay (150);
    Tmp = InByte (AdapterInfo, EEAddr);
    AddrLen++;
  }

  //
  // read the eeprom word, even though we don't need this
  //
  shift_bits_in (AdapterInfo);

  //
  // Terminate the EEPROM access.
  //
  Tmp = InByte (AdapterInfo, EEAddr);
  Tmp &= ~(EE_CS | EE_DI);
  OutByte (AdapterInfo, Tmp, EEAddr);

  //
  // raise the clock and lower the eeprom shift clock
  //
  OutByte (AdapterInfo, (UINT8) (Tmp | EE_SHIFT_CLK), EEAddr);
  eeprom_delay (100);

  OutByte (AdapterInfo, (UINT8) (Tmp &~EE_SHIFT_CLK), EEAddr);
  eeprom_delay (100);

  //
  // giveup access to the eeprom!
  //
  E100bReSetEepromLockOut (AdapterInfo);

  return AddrLen;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  DBaddr                          TODO: add argument description
  @param  DBsize                          TODO: add argument description

  @return TODO: add return values

**/
UINTN
E100bStatistics (
  NIC_DATA_INSTANCE *AdapterInfo,
  UINT64            DBaddr,
  UINT16            DBsize
  )
{
  PXE_DB_STATISTICS db;
  //
  // wait upto one second (each wait is 100 micro s)
  //
  UINT32            Wait;
  Wait = 10000;
  wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);

  //
  // Clear statistics done marker.
  //
  AdapterInfo->statistics->done_marker = 0;

  //
  // Issue statistics dump (or dump w/ reset) command.
  //
  OutByte (
    AdapterInfo,
    (UINT8) (DBsize ? CU_SHOWSTATS : CU_DUMPSTATS),
    (UINT32) (AdapterInfo->ioaddr + SCBCmd)
    );

  //
  // Wait for command to complete.
  //
  // zero the db here just to chew up a little more time.
  //

  ZeroMem ((VOID *) &db, sizeof db);

  while (Wait != 0) {
    //
    // Wait a bit before checking.
    //

    DelayIt (AdapterInfo, 100);

    //
    // Look for done marker at end of statistics.
    //

    switch (AdapterInfo->statistics->done_marker) {
    case 0xA005:
    case 0xA007:
      break;

    default:
      Wait--;
      continue;
    }

    //
    // if we did not "continue" from the above switch, we are done,
    //
    break;
  }

  //
  // If this is a reset, we are out of here!
  //
  if (DBsize == 0) {
    return PXE_STATCODE_SUCCESS;
  }

  //
  // Convert NIC statistics counter format to EFI/UNDI
  // specification statistics counter format.
  //

  //
  //                54 3210 fedc ba98 7654 3210
  // db.Supported = 01 0000 0100 1101 0001 0111;
  //
  db.Supported = 0x104D17;

  //
  // Statistics from the NIC
  //

  db.Data[0x01] = AdapterInfo->statistics->rx_good_frames;

  db.Data[0x02] = AdapterInfo->statistics->rx_runt_errs;

  db.Data[0x08] = AdapterInfo->statistics->rx_crc_errs +
                  AdapterInfo->statistics->rx_align_errs;

  db.Data[0x04] = db.Data[0x02] +
                  db.Data[0x08] +
                  AdapterInfo->statistics->rx_resource_errs +
                  AdapterInfo->statistics->rx_overrun_errs;

  db.Data[0x00] = db.Data[0x01] + db.Data[0x04];

  db.Data[0x0B] = AdapterInfo->statistics->tx_good_frames;

  db.Data[0x0E] = AdapterInfo->statistics->tx_coll16_errs +
    AdapterInfo->statistics->tx_late_colls +
    AdapterInfo->statistics->tx_underruns +
    AdapterInfo->statistics->tx_one_colls +
    AdapterInfo->statistics->tx_multi_colls;

  db.Data[0x14] = AdapterInfo->statistics->tx_total_colls;

  db.Data[0x0A] = db.Data[0x0B] +
                  db.Data[0x0E] +
                  AdapterInfo->statistics->tx_lost_carrier;

  if (DBsize > sizeof db) {
    DBsize = sizeof db;
  }

  CopyMem ((VOID *) (UINTN) DBaddr, (VOID *) &db, (UINTN) DBsize);

  return PXE_STATCODE_SUCCESS;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description
  @param  OpFlags                         TODO: add argument description

  @return TODO: add return values

**/
UINTN
E100bReset (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN INT32             OpFlags
  )
{

  UINT16  save_filter;
  //
  // disable the interrupts
  //
  OutWord (AdapterInfo, INT_MASK, AdapterInfo->ioaddr + SCBCmd);

  //
  // wait for the tx queue to complete
  //
  CheckCBList (AdapterInfo);

  XmitWaitForCompletion (AdapterInfo);

  if (AdapterInfo->Receive_Started) {
    StopRU (AdapterInfo);
  }

  InitializeChip (AdapterInfo);

  //
  // check the opflags and restart receive filters
  //
  if ((OpFlags & PXE_OPFLAGS_RESET_DISABLE_FILTERS) == 0) {

    save_filter = AdapterInfo->Rx_Filter;
    //
    // if we give the filter same as Rx_Filter,
    // this routine will not set mcast list (it thinks there is no change)
    // to force it, we will reset that flag in the Rx_Filter
    //
    AdapterInfo->Rx_Filter &= (~PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST);
    E100bSetfilter (AdapterInfo, save_filter, (UINT64) 0, (UINT32) 0);
  }

  if ((OpFlags & PXE_OPFLAGS_RESET_DISABLE_INTERRUPTS) != 0) {
    //
    // disable the interrupts
    //
    AdapterInfo->int_mask = 0;
  }
  //
  // else leave the interrupt in the pre-set state!!!
  //
  E100bSetInterruptState (AdapterInfo);

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINTN
E100bShutdown (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  //
  // disable the interrupts
  //
  OutWord (AdapterInfo, INT_MASK, AdapterInfo->ioaddr + SCBCmd);

  //
  // stop the receive unit
  //
  if (AdapterInfo->Receive_Started) {
    StopRU (AdapterInfo);
  }

  //
  // wait for the tx queue to complete
  //
  CheckCBList (AdapterInfo);
  if (AdapterInfo->FreeCBCount != AdapterInfo->TxBufCnt) {
    wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);
  }

  //
  // we do not want to reset the phy, it takes a long time to renegotiate the
  // link after that (3-4 seconds)
  //
  InitializeChip (AdapterInfo);
  SelectiveReset (AdapterInfo);
  return 0;
}


/**
  This routine will write a value to the specified MII register
  of an external MDI compliant device (e.g. PHY 100).  The command will
  execute in polled mode.

  @param  AdapterInfo                     pointer to the structure that contains
                                          the NIC's context.
  @param  RegAddress                      The MII register that we are writing to
  @param  PhyAddress                      The MDI address of the Phy component.
  @param  DataValue                       The value that we are writing to the MII
                                          register.

  @return nothing

**/
VOID
MdiWrite (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT8             RegAddress,
  IN UINT8             PhyAddress,
  IN UINT16            DataValue
  )
{
  UINT32  WriteCommand;

  WriteCommand = ((UINT32) DataValue) |
                 ((UINT32)(RegAddress << 16)) |
                 ((UINT32)(PhyAddress << 21)) |
                 ((UINT32)(MDI_WRITE << 26));

  //
  // Issue the write command to the MDI control register.
  //
  OutLong (AdapterInfo, WriteCommand, AdapterInfo->ioaddr + SCBCtrlMDI);

  //
  // wait 20usec before checking status
  //
  DelayIt (AdapterInfo, 20);

  //
  // poll for the mdi write to complete
  while ((InLong (AdapterInfo, AdapterInfo->ioaddr + SCBCtrlMDI) &
                    MDI_PHY_READY) == 0){
    DelayIt (AdapterInfo, 20);
  }
}


/**
  This routine will read a value from the specified MII register
  of an external MDI compliant device (e.g. PHY 100), and return
  it to the calling routine.  The command will execute in polled mode.

  @param  AdapterInfo                     pointer to the structure that contains
                                          the NIC's context.
  @param  RegAddress                      The MII register that we are reading from
  @param  PhyAddress                      The MDI address of the Phy component.
  @param  DataValue                       pointer to the value that we read from
                                          the MII register.


**/
VOID
MdiRead (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT8             RegAddress,
  IN UINT8             PhyAddress,
  IN OUT UINT16        *DataValue
  )
{
  UINT32  ReadCommand;

  ReadCommand = ((UINT32) (RegAddress << 16)) |
                ((UINT32) (PhyAddress << 21)) |
                ((UINT32) (MDI_READ << 26));

  //
  // Issue the read command to the MDI control register.
  //
  OutLong (AdapterInfo, ReadCommand, AdapterInfo->ioaddr + SCBCtrlMDI);

  //
  // wait 20usec before checking status
  //
  DelayIt (AdapterInfo, 20);

  //
  // poll for the mdi read to complete
  //
  while ((InLong (AdapterInfo, AdapterInfo->ioaddr + SCBCtrlMDI) &
          MDI_PHY_READY) == 0) {
    DelayIt (AdapterInfo, 20);

  }

  *DataValue = InWord (AdapterInfo, AdapterInfo->ioaddr + SCBCtrlMDI);
}


/**
  This routine will reset the PHY that the adapter is currently
  configured to use.

  @param  AdapterInfo                     pointer to the structure that contains
                                          the NIC's context.


**/
VOID
PhyReset (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT16  MdiControlReg;

  MdiControlReg = (MDI_CR_AUTO_SELECT |
                  MDI_CR_RESTART_AUTO_NEG |
                  MDI_CR_RESET);

  //
  // Write the MDI control register with our new Phy configuration
  //
  MdiWrite (
    AdapterInfo,
    MDI_CONTROL_REG,
    AdapterInfo->PhyAddress,
    MdiControlReg
    );

  return ;
}


/**
  This routine will detect what phy we are using, set the line
  speed, FDX or HDX, and configure the phy if necessary.
  The following combinations are supported:
  - TX or T4 PHY alone at PHY address 1
  - T4 or TX PHY at address 1 and MII PHY at address 0
  - 82503 alone (10Base-T mode, no full duplex support)
  - 82503 and MII PHY (TX or T4) at address 0
  The sequence / priority of detection is as follows:
  - PHY 1 with cable termination
  - PHY 0 with cable termination
  - PHY 1 (if found) without cable termination
  - 503 interface
  Additionally auto-negotiation capable (NWAY) and parallel
  detection PHYs are supported. The flow-chart is described in
  the 82557 software writer's manual.
  NOTE:  1.  All PHY MDI registers are read in polled mode.
  2.  The routines assume that the 82557 has been RESET and we have
  obtained the virtual memory address of the CSR.
  3.  PhyDetect will not RESET the PHY.
  4.  If FORCEFDX is set, SPEED should also be set. The driver will
  check the values for inconsistency with the detected PHY
  technology.
  5.  PHY 1 (the PHY on the adapter) may have an address in the range
  1 through 31 inclusive. The driver will accept addresses in
  this range.
  6.  Driver ignores FORCEFDX and SPEED overrides if a 503 interface
  is detected.

  @param  AdapterInfo                     pointer to the structure that contains
                                          the NIC's context.

  @retval TRUE                            If a Phy was detected, and configured
                                          correctly.
  @retval FALSE                           If a valid phy could not be detected and
                                          configured.

**/
BOOLEAN
PhyDetect (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT16  *eedata;
  UINT16  MdiControlReg;
  UINT16  MdiStatusReg;
  BOOLEAN FoundPhy1;
  UINT8   ReNegotiateTime;

  eedata          = (UINT16 *) (&AdapterInfo->NVData[0]);

  FoundPhy1       = FALSE;
  ReNegotiateTime = 35;
  //
  // EEPROM word [6] contains the Primary PHY record in which the least 3 bits
  // indicate the PHY address
  // and word [7] contains the secondary PHY record
  //
  AdapterInfo->PhyRecord[0] = eedata[6];
  AdapterInfo->PhyRecord[1] = eedata[7];
  AdapterInfo->PhyAddress   = (UINT8) (AdapterInfo->PhyRecord[0] & 7);

  //
  // Check for a phy address over-ride of 32 which indicates force use of 82503
  // not detecting the link in this case
  //
  if (AdapterInfo->PhyAddress == 32) {
    //
    // 503 interface over-ride
    // Record the current speed and duplex.  We will be in half duplex
    // mode unless the user used the force full duplex over-ride.
    //
    AdapterInfo->LinkSpeed = 10;
    return (TRUE);
  }

  //
  // If the Phy Address is between 1-31 then we must first look for phy 1,
  // at that address.
  //
  if ((AdapterInfo->PhyAddress > 0) && (AdapterInfo->PhyAddress < 32)) {

    //
    // Read the MDI control and status registers at phy 1
    // and check if we found a valid phy
    //
    MdiRead (
      AdapterInfo,
      MDI_CONTROL_REG,
      AdapterInfo->PhyAddress,
      &MdiControlReg
      );

    MdiRead (
      AdapterInfo,
      MDI_STATUS_REG,
      AdapterInfo->PhyAddress,
      &MdiStatusReg
      );

    if (!((MdiControlReg == 0xffff) ||
          ((MdiStatusReg == 0) && (MdiControlReg == 0)))) {

      //
      // we have a valid phy1
      // Read the status register again because of sticky bits
      //
      FoundPhy1 = TRUE;
      MdiRead (
        AdapterInfo,
        MDI_STATUS_REG,
        AdapterInfo->PhyAddress,
        &MdiStatusReg
        );

      //
      // If there is a valid link then use this Phy.
      //
      if (MdiStatusReg & MDI_SR_LINK_STATUS) {
        return (SetupPhy(AdapterInfo));
      }
    }
  }

  //
  // Next try to detect a PHY at address 0x00 because there was no Phy 1,
  // or Phy 1 didn't have link, or we had a phy 0 over-ride
  //

  //
  // Read the MDI control and status registers at phy 0
  //
  MdiRead (AdapterInfo, MDI_CONTROL_REG, 0, &MdiControlReg);
  MdiRead (AdapterInfo, MDI_STATUS_REG, 0, &MdiStatusReg);

  //
  // check if we found a valid phy 0
  //
  if (((MdiControlReg == 0xffff) ||
       ((MdiStatusReg == 0) && (MdiControlReg == 0)))) {

    //
    // we don't have a valid phy at address 0
    // if phy address was forced to 0, then error out because we
    // didn't find a phy at that address
    //
    if (AdapterInfo->PhyAddress == 0x0000) {
      return (FALSE);
    } else {
      //
      // at this point phy1 does not have link and there is no phy 0 at all
      // if we are forced to detect the cable, error out here!
      //
      if (AdapterInfo->CableDetect != 0) {
        return FALSE;

      }

      if (FoundPhy1) {
        //
        // no phy 0, but there is a phy 1 (no link I guess), so use phy 1
        //
        return SetupPhy (AdapterInfo);
      } else {
        //
        // didn't find phy 0 or phy 1, so assume a 503 interface
        //
        AdapterInfo->PhyAddress = 32;

        //
        // Record the current speed and duplex.  We'll be in half duplex
        // mode unless the user used the force full duplex over-ride.
        //
        AdapterInfo->LinkSpeed = 10;
        return (TRUE);
      }
    }
  } else {
    //
    // We have a valid phy at address 0.  If phy 0 has a link then we use
    // phy 0.  If Phy 0 doesn't have a link then we use Phy 1 (no link)
    // if phy 1 is present, or phy 0 if phy 1 is not present
    // If phy 1 was present, then we must isolate phy 1 before we enable
    // phy 0 to see if Phy 0 has a link.
    //
    if (FoundPhy1) {
      //
      // isolate phy 1
      //
      MdiWrite (
        AdapterInfo,
        MDI_CONTROL_REG,
        AdapterInfo->PhyAddress,
        MDI_CR_ISOLATE
        );

      //
      // wait 100 microseconds for the phy to isolate.
      //
      DelayIt (AdapterInfo, 100);
    }

    //
    // Since this Phy is at address 0, we must enable it.  So clear
    // the isolate bit, and set the auto-speed select bit
    //
    MdiWrite (
      AdapterInfo,
      MDI_CONTROL_REG,
      0,
      MDI_CR_AUTO_SELECT
      );

    //
    // wait 100 microseconds for the phy to be enabled.
    //
    DelayIt (AdapterInfo, 100);

    //
    // restart the auto-negotion process
    //
    MdiWrite (
      AdapterInfo,
      MDI_CONTROL_REG,
      0,
      MDI_CR_RESTART_AUTO_NEG | MDI_CR_AUTO_SELECT
      );

    //
    // wait no more than 3.5 seconds for auto-negotiation to complete
    //
    while (ReNegotiateTime) {
      //
      // Read the status register twice because of sticky bits
      //
      MdiRead (AdapterInfo, MDI_STATUS_REG, 0, &MdiStatusReg);
      MdiRead (AdapterInfo, MDI_STATUS_REG, 0, &MdiStatusReg);

      if (MdiStatusReg & MDI_SR_AUTO_NEG_COMPLETE) {
        break;
      }

      DelayIt (AdapterInfo, 100);
      ReNegotiateTime--;
    }

    //
    // Read the status register again because of sticky bits
    //
    MdiRead (AdapterInfo, MDI_STATUS_REG, 0, &MdiStatusReg);

    //
    // If the link was not set
    //
    if ((MdiStatusReg & MDI_SR_LINK_STATUS) == 0) {
      //
      // PHY1 does not have a link and phy 0 does not have a link
      // do not proceed if we need to detect the link!
      //
      if (AdapterInfo->CableDetect != 0) {
        return FALSE;
      }

      //
      // the link wasn't set, so use phy 1 if phy 1 was present
      //
      if (FoundPhy1) {
        //
        // isolate phy 0
        //
        MdiWrite (AdapterInfo, MDI_CONTROL_REG, 0, MDI_CR_ISOLATE);

        //
        // wait 100 microseconds for the phy to isolate.
        //
        DelayIt (AdapterInfo, 100);

        //
        // Now re-enable PHY 1
        //
        MdiWrite (
          AdapterInfo,
          MDI_CONTROL_REG,
          AdapterInfo->PhyAddress,
          MDI_CR_AUTO_SELECT
          );

        //
        // wait 100 microseconds for the phy to be enabled
        //
        DelayIt (AdapterInfo, 100);

        //
        // restart the auto-negotion process
        //
        MdiWrite (
          AdapterInfo,
          MDI_CONTROL_REG,
          AdapterInfo->PhyAddress,
          MDI_CR_RESTART_AUTO_NEG | MDI_CR_AUTO_SELECT
          );

        //
        // Don't wait for it to complete (we didn't have link earlier)
        //
        return (SetupPhy (AdapterInfo));
      }
    }

    //
    // Definitely using Phy 0
    //
    AdapterInfo->PhyAddress = 0;
    return (SetupPhy(AdapterInfo));
  }
}


/**
  This routine will setup phy 1 or phy 0 so that it is configured
  to match a speed and duplex over-ride option.  If speed or
  duplex mode is not explicitly specified in the registry, the
  driver will skip the speed and duplex over-ride code, and
  assume the adapter is automatically setting the line speed, and
  the duplex mode.  At the end of this routine, any truly Phy
  specific code will be executed (each Phy has its own quirks,
  and some require that certain special bits are set).
  NOTE:  The driver assumes that SPEED and FORCEFDX are specified at the
  same time. If FORCEDPX is set without speed being set, the driver
  will encouter a fatal error and log a message into the event viewer.

  @param  AdapterInfo                     pointer to the structure that contains
                                          the NIC's context.

  @retval TRUE                            If the phy could be configured correctly
  @retval FALSE                           If the phy couldn't be configured
                                          correctly, because an unsupported
                                          over-ride option was used

**/
BOOLEAN
SetupPhy (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT16  MdiControlReg;
  UINT16  MdiStatusReg;
  UINT16  MdiIdLowReg;
  UINT16  MdiIdHighReg;
  UINT16  MdiMiscReg;
  UINT32  PhyId;
  BOOLEAN ForcePhySetting;

  ForcePhySetting = FALSE;

  //
  // If we are NOT forcing a setting for line speed or full duplex, then
  // we won't force a link setting, and we'll jump down to the phy
  // specific code.
  //
  if (((AdapterInfo->LinkSpeedReq) || (AdapterInfo->DuplexReq))) {
    //
    // Find out what kind of technology this Phy is capable of.
    //
    MdiRead (
      AdapterInfo,
      MDI_STATUS_REG,
      AdapterInfo->PhyAddress,
      &MdiStatusReg
      );

    //
    // Read the MDI control register at our phy
    //
    MdiRead (
      AdapterInfo,
      MDI_CONTROL_REG,
      AdapterInfo->PhyAddress,
      &MdiControlReg
      );

    //
    // Now check the validity of our forced option.  If the force option is
    // valid, then force the setting.  If the force option is not valid,
    // we'll set a flag indicating that we should error out.
    //

    //
    // If speed is forced to 10mb
    //
    if (AdapterInfo->LinkSpeedReq == 10) {
      //
      // If half duplex is forced
      //
      if ((AdapterInfo->DuplexReq & PXE_FORCE_HALF_DUPLEX) != 0) {
        if (MdiStatusReg & MDI_SR_10T_HALF_DPX) {

          MdiControlReg &= ~(MDI_CR_10_100 | MDI_CR_AUTO_SELECT | MDI_CR_FULL_HALF);
          ForcePhySetting = TRUE;
        }
      } else if ((AdapterInfo->DuplexReq & PXE_FORCE_FULL_DUPLEX) != 0) {

        //
        // If full duplex is forced
        //
        if (MdiStatusReg & MDI_SR_10T_FULL_DPX) {

          MdiControlReg &= ~(MDI_CR_10_100 | MDI_CR_AUTO_SELECT);
          MdiControlReg |= MDI_CR_FULL_HALF;
          ForcePhySetting = TRUE;
        }
      } else {
        //
        // If auto duplex (we actually set phy to 1/2)
        //
        if (MdiStatusReg & (MDI_SR_10T_FULL_DPX | MDI_SR_10T_HALF_DPX)) {

          MdiControlReg &= ~(MDI_CR_10_100 | MDI_CR_AUTO_SELECT | MDI_CR_FULL_HALF);
          ForcePhySetting = TRUE;
        }
      }
    }

    //
    // If speed is forced to 100mb
    //
    else if (AdapterInfo->LinkSpeedReq == 100) {
      //
      // If half duplex is forced
      //
      if ((AdapterInfo->DuplexReq & PXE_FORCE_HALF_DUPLEX) != 0) {
        if (MdiStatusReg & (MDI_SR_TX_HALF_DPX | MDI_SR_T4_CAPABLE)) {

          MdiControlReg &= ~(MDI_CR_AUTO_SELECT | MDI_CR_FULL_HALF);
          MdiControlReg |= MDI_CR_10_100;
          ForcePhySetting = TRUE;
        }
      } else if ((AdapterInfo->DuplexReq & PXE_FORCE_FULL_DUPLEX) != 0) {
        //
        // If full duplex is forced
        //
        if (MdiStatusReg & MDI_SR_TX_FULL_DPX) {
          MdiControlReg &= ~MDI_CR_AUTO_SELECT;
          MdiControlReg |= (MDI_CR_10_100 | MDI_CR_FULL_HALF);
          ForcePhySetting = TRUE;
        }
      } else {
        //
        // If auto duplex (we set phy to 1/2)
        //
        if (MdiStatusReg & (MDI_SR_TX_HALF_DPX | MDI_SR_T4_CAPABLE)) {

          MdiControlReg &= ~(MDI_CR_AUTO_SELECT | MDI_CR_FULL_HALF);
          MdiControlReg |= MDI_CR_10_100;
          ForcePhySetting = TRUE;
        }
      }
    }

    if (!ForcePhySetting) {
      return (FALSE);
    }

    //
    // Write the MDI control register with our new Phy configuration
    //
    MdiWrite (
      AdapterInfo,
      MDI_CONTROL_REG,
      AdapterInfo->PhyAddress,
      MdiControlReg
      );

    //
    // wait 100 milliseconds for auto-negotiation to complete
    //
    DelayIt (AdapterInfo, 100);
  }

  //
  // Find out specifically what Phy this is.  We do this because for certain
  // phys there are specific bits that must be set so that the phy and the
  // 82557 work together properly.
  //

  MdiRead (
    AdapterInfo,
    PHY_ID_REG_1,
    AdapterInfo->PhyAddress,
    &MdiIdLowReg
    );
  MdiRead (
    AdapterInfo,
    PHY_ID_REG_2,
    AdapterInfo->PhyAddress,
    &MdiIdHighReg
    );

  PhyId = ((UINT32) MdiIdLowReg | ((UINT32) MdiIdHighReg << 16));

  //
  // And out the revsion field of the Phy ID so that we'll be able to detect
  // future revs of the same Phy.
  //
  PhyId &= PHY_MODEL_REV_ID_MASK;

  //
  // Handle the National TX
  //
  if (PhyId == PHY_NSC_TX) {

    MdiRead (
      AdapterInfo,
      NSC_CONG_CONTROL_REG,
      AdapterInfo->PhyAddress,
      &MdiMiscReg
      );

    MdiMiscReg |= (NSC_TX_CONG_TXREADY | NSC_TX_CONG_F_CONNECT);

    MdiWrite (
      AdapterInfo,
      NSC_CONG_CONTROL_REG,
      AdapterInfo->PhyAddress,
      MdiMiscReg
      );
  }

  FindPhySpeedAndDpx (AdapterInfo, PhyId);

  //
  // We put a hardware fix on to our adapters to work-around the PHY_100 errata
  // described below.  The following code is only compiled in, if we wanted
  // to attempt a software workaround to the PHY_100 A/B step problem.
  //

  return (TRUE);
}


/**
  This routine will figure out what line speed and duplex mode
  the PHY is currently using.

  @param  AdapterInfo                     pointer to the structure that contains
                                          the NIC's context.
  @param  PhyId                           The ID of the PHY in question.

  @return NOTHING

**/
VOID
FindPhySpeedAndDpx (
  IN NIC_DATA_INSTANCE *AdapterInfo,
  IN UINT32            PhyId
  )
{
  UINT16  MdiStatusReg;
  UINT16  MdiMiscReg;
  UINT16  MdiOwnAdReg;
  UINT16  MdiLinkPartnerAdReg;

  //
  // If there was a speed and/or duplex override, then set our current
  // value accordingly
  //
  AdapterInfo->LinkSpeed  = AdapterInfo->LinkSpeedReq;
  AdapterInfo->Duplex = (UINT8) ((AdapterInfo->DuplexReq & PXE_FORCE_FULL_DUPLEX) ?
                        FULL_DUPLEX : HALF_DUPLEX);

  //
  // If speed and duplex were forced, then we know our current settings, so
  // we'll just return.  Otherwise, we'll need to figure out what NWAY set
  // us to.
  //
  if (AdapterInfo->LinkSpeed && AdapterInfo->Duplex) {
    return ;

  }
  //
  // If we didn't have a valid link, then we'll assume that our current
  // speed is 10mb half-duplex.
  //

  //
  // Read the status register twice because of sticky bits
  //
  MdiRead (
    AdapterInfo,
    MDI_STATUS_REG,
    AdapterInfo->PhyAddress,
    &MdiStatusReg
    );
  MdiRead (
    AdapterInfo,
    MDI_STATUS_REG,
    AdapterInfo->PhyAddress,
    &MdiStatusReg
    );

  //
  // If there wasn't a valid link then use default speed & duplex
  //
  if (!(MdiStatusReg & MDI_SR_LINK_STATUS)) {

    AdapterInfo->LinkSpeed  = 10;
    AdapterInfo->Duplex     = HALF_DUPLEX;
    return ;
  }

  //
  // If this is an Intel PHY (a T4 PHY_100 or a TX PHY_TX), then read bits
  // 1 and 0 of extended register 0, to get the current speed and duplex
  // settings.
  //
  if ((PhyId == PHY_100_A) || (PhyId == PHY_100_C) || (PhyId == PHY_TX_ID)) {
    //
    // Read extended register 0
    //
    MdiRead (
      AdapterInfo,
      EXTENDED_REG_0,
      AdapterInfo->PhyAddress,
      &MdiMiscReg
      );

    //
    // Get current speed setting
    //
    if (MdiMiscReg & PHY_100_ER0_SPEED_INDIC) {
      AdapterInfo->LinkSpeed = 100;
    } else {
      AdapterInfo->LinkSpeed = 10;
    }

    //
    // Get current duplex setting -- if bit is set then FDX is enabled
    //
    if (MdiMiscReg & PHY_100_ER0_FDX_INDIC) {
      AdapterInfo->Duplex = FULL_DUPLEX;
    } else {
      AdapterInfo->Duplex = HALF_DUPLEX;
    }

    return ;
  }
  //
  // Read our link partner's advertisement register
  //
  MdiRead (
    AdapterInfo,
    AUTO_NEG_LINK_PARTNER_REG,
    AdapterInfo->PhyAddress,
    &MdiLinkPartnerAdReg
    );

  //
  // See if Auto-Negotiation was complete (bit 5, reg 1)
  //
  MdiRead (
    AdapterInfo,
    MDI_STATUS_REG,
    AdapterInfo->PhyAddress,
    &MdiStatusReg
    );

  //
  // If a True NWAY connection was made, then we can detect speed/duplex by
  // ANDing our adapter's advertised abilities with our link partner's
  // advertised ablilities, and then assuming that the highest common
  // denominator was chosed by NWAY.
  //
  if ((MdiLinkPartnerAdReg & NWAY_LP_ABILITY) &&
      (MdiStatusReg & MDI_SR_AUTO_NEG_COMPLETE)) {

    //
    // Read our advertisement register
    //
    MdiRead (
      AdapterInfo,
      AUTO_NEG_ADVERTISE_REG,
      AdapterInfo->PhyAddress,
      &MdiOwnAdReg
      );

    //
    // AND the two advertisement registers together, and get rid of any
    // extraneous bits.
    //
    MdiOwnAdReg = (UINT16) (MdiOwnAdReg & (MdiLinkPartnerAdReg & NWAY_LP_ABILITY));

    //
    // Get speed setting
    //
    if (MdiOwnAdReg & (NWAY_AD_TX_HALF_DPX | NWAY_AD_TX_FULL_DPX | NWAY_AD_T4_CAPABLE)) {
      AdapterInfo->LinkSpeed = 100;
    } else {
      AdapterInfo->LinkSpeed = 10;
    }

    //
    // Get duplex setting -- use priority resolution algorithm
    //
    if (MdiOwnAdReg & (NWAY_AD_T4_CAPABLE)) {
      AdapterInfo->Duplex = HALF_DUPLEX;
      return ;
    } else if (MdiOwnAdReg & (NWAY_AD_TX_FULL_DPX)) {
      AdapterInfo->Duplex = FULL_DUPLEX;
      return ;
    } else if (MdiOwnAdReg & (NWAY_AD_TX_HALF_DPX)) {
      AdapterInfo->Duplex = HALF_DUPLEX;
      return ;
    } else if (MdiOwnAdReg & (NWAY_AD_10T_FULL_DPX)) {
      AdapterInfo->Duplex = FULL_DUPLEX;
      return ;
    } else {
      AdapterInfo->Duplex = HALF_DUPLEX;
      return ;
    }
  }

  //
  // If we are connected to a dumb (non-NWAY) repeater or hub, and the line
  // speed was determined automatically by parallel detection, then we have
  // no way of knowing exactly what speed the PHY is set to unless that PHY
  // has a propietary register which indicates speed in this situation.  The
  // NSC TX PHY does have such a register.  Also, since NWAY didn't establish
  // the connection, the duplex setting should HALF duplex.
  //
  AdapterInfo->Duplex = HALF_DUPLEX;

  if (PhyId == PHY_NSC_TX) {
    //
    // Read register 25 to get the SPEED_10 bit
    //
    MdiRead (
      AdapterInfo,
      NSC_SPEED_IND_REG,
      AdapterInfo->PhyAddress,
      &MdiMiscReg
      );

    //
    // If bit 6 was set then we're at 10mb
    //
    if (MdiMiscReg & NSC_TX_SPD_INDC_SPEED) {
      AdapterInfo->LinkSpeed = 10;
    } else {
      AdapterInfo->LinkSpeed = 100;
    }
  }

  //
  // If we don't know what line speed we are set at, then we'll default to
  // 10mbs
  //
  else {
    AdapterInfo->LinkSpeed = 10;
  }
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
VOID
XmitWaitForCompletion (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  TxCB  *TxPtr;

  if (AdapterInfo->FreeCBCount == AdapterInfo->TxBufCnt) {
    return ;
  }

  //
  // used xmit cb list starts right after the free tail (ends before the
  // free head ptr)
  //
  TxPtr = AdapterInfo->FreeTxTailPtr->NextTCBVirtualLinkPtr;
  while (TxPtr != AdapterInfo->FreeTxHeadPtr) {
    CommandWaitForCompletion (TxPtr, AdapterInfo);
    SetFreeCB (AdapterInfo, TxPtr);
    TxPtr = TxPtr->NextTCBVirtualLinkPtr;
  }
}


/**
  TODO: Add function description

  @param  cmd_ptr                         TODO: add argument description
  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
INT8
CommandWaitForCompletion (
  TxCB              *cmd_ptr,
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  INT16 wait;
  wait = 5000;
  while ((cmd_ptr->cb_header.status == 0) && (--wait > 0)) {
    DelayIt (AdapterInfo, 10);
  }

  if (cmd_ptr->cb_header.status == 0) {
    return -1;
  }

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
INT8
SoftwareReset (
  NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT8   tco_stat;
  UINT16  wait;

  tco_stat = 0;

  //
  // Reset the chip: stop Tx and Rx processes and clear counters.
  // This takes less than 10usec and will easily finish before the next
  // action.
  //

  OutLong (AdapterInfo, PORT_RESET, AdapterInfo->ioaddr + SCBPort);
  //
  // wait for 5 milli seconds here!
  //
  DelayIt (AdapterInfo, 5000);
  //
  // TCO Errata work around for 559s only
  // -----------------------------------------------------------------------------------
  // TCO Workaround Code
  //  haifa workaround
  // -----------------------------------------------------------------------------------
  //    1. Issue SW-RST ^^^ (already done above)
  //    2. Issue a redundant Set CU Base CMD immediately
  //       Do not set the General Pointer before the Set CU Base cycle
  //       Do not check the SCB CMD before the Set CU Base cycle
  //    3. Wait for the SCB-CMD to be cleared
  //       this indicates the transition to post-driver
  //    4. Poll the TCO-Req bit in the PMDR to be cleared
  //       this indicates the tco activity has stopped for real
  //    5. Proceed with the nominal Driver Init:
  //       Actual Set CU & RU Base ...
  //
  // Check for ICH2 device ID.  If this is an ICH2,
  // do the TCO workaround code.
  //
  if (AdapterInfo->VendorID == D102_DEVICE_ID ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_1 ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_2 ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_3 ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_4 ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_5 ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_6 ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_7 ||
      AdapterInfo->VendorID == ICH3_DEVICE_ID_8 ||
      AdapterInfo->RevID >= 8) {  // do the TCO fix
    //
    // donot load the scb pointer but just give load_cu cmd.
    //
    OutByte (AdapterInfo, CU_CMD_BASE, AdapterInfo->ioaddr + SCBCmd);
    //
    // wait for command to be accepted.
    //
    wait_for_cmd_done (AdapterInfo->ioaddr + SCBCmd);
    //
    // read PMDR register and check bit 1 in it to see if TCO is active
    //

    //
    // wait for 5 milli seconds
    //
    wait = 5000;
    while (wait) {
      tco_stat = InByte (AdapterInfo, AdapterInfo->ioaddr + 0x1b);
      if ((tco_stat & 2) == 0) {
        //
        // is the activity bit clear??
        //
        break;
      }

      wait--;
      DelayIt (AdapterInfo, 1);
    }

    if ((tco_stat & 2) != 0) {
      //
      // not zero??
      //
      return -1;
    }
  }

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT8
SelectiveReset (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT16  wait;
  UINT32  stat;

  wait  = 10;
  stat  = 0;
  OutLong (AdapterInfo, POR_SELECTIVE_RESET, AdapterInfo->ioaddr + SCBPort);
  //
  // wait for this to complete
  //

  //
  // wait for 2 milli seconds here!
  //
  DelayIt (AdapterInfo, 2000);
  while (wait > 0) {
    wait--;
    stat = InLong (AdapterInfo, AdapterInfo->ioaddr + SCBPort);
    if (stat == 0) {
      break;
    }

    //
    // wait for 1 milli second
    //
    DelayIt (AdapterInfo, 1000);
  }

  if (stat != 0) {
    return PXE_STATCODE_DEVICE_FAILURE;
  }

  return 0;
}


/**
  TODO: Add function description

  @param  AdapterInfo                     TODO: add argument description

  @return TODO: add return values

**/
UINT16
InitializeChip (
  IN NIC_DATA_INSTANCE *AdapterInfo
  )
{
  UINT16  ret_val;
  if (SoftwareReset (AdapterInfo) != 0) {
    return PXE_STATCODE_DEVICE_FAILURE;
  }

  //
  // disable interrupts
  //
  OutWord (AdapterInfo, INT_MASK, AdapterInfo->ioaddr + SCBCmd);

  //
  // Load the base registers with 0s (we will give the complete address as
  // offset later when we issue any command
  //
  if ((ret_val = Load_Base_Regs (AdapterInfo)) != 0) {
    return ret_val;
  }

  if ((ret_val = SetupCBlink (AdapterInfo)) != 0) {
    return ret_val;
  }

  if ((ret_val = SetupReceiveQueues (AdapterInfo)) != 0) {
    return ret_val;
  }

  //
  // detect the PHY only if we need to detect the cable as requested by the
  // initialize parameters
  //
  AdapterInfo->PhyAddress = 0xFF;

  if (AdapterInfo->CableDetect != 0) {
    if (!PhyDetect (AdapterInfo)) {
      return PXE_STATCODE_DEVICE_FAILURE;
    }
  }

  if ((ret_val = E100bSetupIAAddr (AdapterInfo)) != 0) {
    return ret_val;
  }

  if ((ret_val = Configure (AdapterInfo)) != 0) {
    return ret_val;
  }

  return 0;
}