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

/*
 * Copyright (C) 2022-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

/** @page pg_dev_dp8390 NatSemi DP8390-Based Ethernet NIC Emulation.
 *
 * This software was written based on the following documents:
 *
 *     - National Semiconductor DP8390/NS32490 Network Interface Controller,
 *          1986
 *     - National Semiconductor DP8390D/NS32490D NIC Network Interface
 *          Controller datasheet, July 1995
 *     - National Semiconductor Application Note 729, DP839EB-ATN IBM PC-AT
 *          Compatible DP83901 SNIC Serial Network Interface Controller
 *          Evaluation Board, 1993
 *     - National Semiconductor Application Note 842, The Design and Operation
 *          of a Low Cost, 8-Bit PC-XT Compatible Ethernet Adapter Using
 *          the DP83902, May 1993
 *     - National Semiconductor Application Note 858, Guide to Loopback Using
 *          the DP8390 Chip Set, October 1992
 *     - National Semiconductor Application Note 875, DP83905EB-AT AT/LANTIC
 *          Evaluation Board, June 1993
 *     - Western Digital WD83C584 Bus Interface Controller Device datasheet,
 *          October 29, 1990
 *     - Western Digital WD83C690 Ethernet LAN Controller datasheet,
 *          November 2, 1990
 *     - 3Com EtherLink II Adapter Technical Reference Manual,
 *          March 1991
 *
 * This emulation is compatible with drivers for:
 *  - Novell/Eagle/Anthem NE1000 (8-bit)
 *  - Novell/Eagle/Anthem NE2000 (16-bit)
 *  - Western Digital/SMC WD8003E (8-bit)
 *  - Western Digital/SMC WD8013EBT (16-bit)
 *  - 3Com EtherLink II 3C503 (8-bit)
 *
 *
 * The National Semiconductor DP8390 was an early (circa 1986) low-cost
 * Ethernet controller, typically accompanied by the DP8391 Serial Network
 * Interface and the DP8392 Coaxial Transceiver Interface.
 *
 * Due to its relatively low cost, the DP8390 NIC was chosen for several
 * very widespread early PC Ethernet designs, namely the Novell NE1000/NE2000,
 * Western Digital (later SMC) WD8003 EtherCard Plus, and 3Com EtherLink II.
 * The popularity of these cards, especially the NE2000, in turn spawned
 * a bevy of compatible chips from National Semiconductor and many others.
 *
 * All common DP8390-based cards have onboard memory. The initial WD8003E and
 * NE1000 cards have one 8Kx8 SRAM; 16-bit cards like WD8013E or NE2000 have
 * two 8Kx8 SRAMs wired in 8Kx16 configuration to enable 16-bit wide transfers.
 * The DP8390 can address up to 64K or local memory and uses "Local DMA"
 * (similar to bus mastering) to access it. Some newer cards had 32K or more
 * onboard RAM. Note that an NE2000 in 8-bit mode can only address 8K local
 * memory, effectively reverting to an NE1000.
 *
 * The DP8390 uses "Remote DMA" to move data between local memory and the host
 * system. Remote DMA is quite similar to 8237-style third party DMA, except
 * the DMA controller is on the DP8390 chip in this case.
 *
 * The DP8390 has a control bit (DCR.WTS) which selects whether all DMA (both
 * Local and Remote) transfers are 8-bit or 16-bit. Word-wide transfers can
 * generally only be used on a 16-bit card in a 16-bit slot, because only then
 * can the host drive 16-bit I/O cycles to the data ports. That is why
 * an NE2000 in an 8-bit slot can only use half of its local RAM -- remote DMA
 * simply cannot access half of the 8Kx16 SRAM.
 *
 * The DP8390 maps its internal registers as sixteen 8-bit wide I/O ports.
 * There are four register pages, selectable through the Command Register (CR)
 * which is accessible at offset 0 in all pages.
 *
 * The NE1000/NE2000 cards only use I/O and IRQ resources, not memory
 * or DMA. In contrast, the Western Digital cards use memory-mapped buffers.
 * Later AT/LANTIC (DP83905) based NE2000-compatible cards can optionally
 * use memory as well. The 3Com EtherLink II (3C503) uses a custom gate array
 * in addition to the DP8390 and can use programmed I/O, 8237 DMA, as well
 * as optional direct memory mapping.
 *
 * Address decoding is typically incomplete, which causes the buffer RAM and
 * possibly PROM to be aliased multiple times in the DP8390's address space.
 *
 * Buffer overflow handling is slightly tricky. The DP8390 assumes that if
 * the receiver is enabled, there is space for at least one page (256 bytes).
 * Once it fills up the page and advances the CURR pointer, the DP8390 checks
 * whether CURR equals BNRY and if so, triggers an overflow condition. Note
 * that after the NIC is initialized, CURR *will* normally equal BNRY, with
 * both pointing at the beginning of the receive ring (PSTART). An overflow
 * is only triggered when CURR equals BNRY right after advancing.
 *
 * The documentation of the Send Packet command mentions that when CRDA crosses
 * the PSTOP register, the current remote DMA address (i.e. CRDA) is set to
 * the PSTART value, which is rather convenient when reading received packets
 * out of the ring buffer using remote DMA. The documentation does not mention
 * that the same logic applies for all remote DMA reads, a feature that several
 * NE1000/NE2000 drivers (packet drivers, Novell ODI) rely on. This is logical,
 * because reading out of the receive ring buffer address range always implies
 * reading received packets, and then the PSTOP->PSTART wraparound becomes
 * desirable. It is unclear whether the same wraparound handling also applies
 * for remote DMA writes within the receive ring buffer.
 *
 * The documentation is not very clear on how the CRDA register is managed.
 * One might be led to believe that starting remote DMA copies the remote DMA
 * start address (i.e. RSAR) to the CRDA register. However, the NE1000 ODI
 * driver for OS/2 1.0 (NE1000.SYS from early 1988) relies on restarting remote
 * DMA and continuing where it left off. The DP8390D datasheet only mentions
 * this in a passing fashion at the end of the "Remote Write with High Speed
 * Buses" section, saying that if a dummy remote read is executed before a
 * remote write, RSAR can be set up for the dummy read such that the CRDA
 * register contains the desired value for the following write.
 *
 * Conversely, it is not spelled out that writing RSAR also updates CRDA, but
 * at least Novell's NE2000 ODI driver v2.12 is known to rely on this behavior
 * and checks that a write to RSAR is reflected in CRDA.
 *
 * Loopback operation is limited in the DP8390. Because it is a half-duplex
 * device, it cannot truly transmit and receive simultaneously. When loopback
 * is in effect, the received data is *not* written into memory. Only the last
 * few bytes of the packet are visible in the FIFO.
 *
 * Likewise due to its half-duplex nature, the CRC circuitry during loopback
 * works either only on the transmit side (FCS is generated but not checked)
 * or the receive side (FCS is checked but not generated).
 *
 * The loopback behavior is even stranger when DCR.WTS is set to enabled 16-bit
 * DMA transfers. Even though the chip reads 16 bits at a time, only 8 bits are
 * actually transmitted; the DCR.BOS bit determines whether the low or high
 * 8 bits of each words are transmitted. As a consequence, the programmed length
 * of the transmit is also halved.
 *
 * Because loopback operation is so different from normal send/receive, loopback
 * packets are not run through the normal receive path and are treated specially
 * instead. The WD and especially 3C503 diagnostics exercise the loopback
 * functionality fairly thoroughly.
 *
 *
 * NE1000 and NE2000
 * -----------------
 *
 * Common NE1000/NE2000 configurations in Novell drivers:
 *   I/O Base = 300h, IRQ = 3 (default)
 *   I/O Base = 320h, IRQ = 2
 *   I/O Base = 340h, IRQ = 4
 *   I/O Base = 360h, IRQ = 5
 * The I/O base can be set to 300h/320h/340h/360h; the IRQ to 2, 3, 4, 5.
 * No memory or DMA is used.
 *
 * The NE1000/NE2000 adds a data register and a reset register to the I/O
 * space. A PROM containing the node address is mapped into the DP8390's local
 * address space.
 *
 * The mapping of the 32x8 PROM on an NE2000 card is quite non-obvious but
 * fortunately well explained in the AN-729 Application Note. Address lines
 * A4-A1 of the internal bus are connected to lines A3-A0 of the PROM
 * (enabling 16 distinct bytes of the 32-byte PROM to be addressed). However,
 * the negated EN16 signal, which is active when the NE2000 is in a 16-bit
 * slot, is connected to the PROM's address line A4. That means an NE2000 in
 * a 16-bit slot reads different PROM bytes than when the same card is in an
 * 8-bit slot. The PROM is structured such that an NE2000 in an 8-bit slot
 * reads a 'BB' signature (same as NE1000) at PROM offset 1Eh/1Fh, while
 * an NE2000 in a 16-bit slot returns a 'WW' signature from PROM offset
 * 0Eh/0Fh instead.
 *
 * The original NE1000 boards Assy. #950-054401 actually only had 6 bytes of
 * MAC address in the PROM, the rest was unused (0FFh). Software supporting the
 * NE1000 thus should not examine the PROM contents beyond the first 6 bytes.
 *
 * Novell's old OUI was 00:00:D8 but drivers are not known to check for it.
 *
 * Newer DP83905 AT/LANTIC based NE2000plus cards were optionally capable of
 * using shared RAM in a manner very similar to the WD8003/WD8013.
 *
 *
 * WD8003 and WD8013 EtherCard Plus
 * --------------------------------
 *
 * Common WD8013 configurations:
 *   I/O Base = 280h, IRQ = 3,  RAM D000-D3FF (default)
 *   I/O Base = 330h, IRQ = 10, RAM CC00-CFFF
 *   I/O Base = 240h, IRQ/RAM soft-configurable
 * The I/O base can be set anywhere in the 2xxh-3xxh range in 20h increments.
 * The IRQs available on a WD8013 are 2, 3, 4, 5, 7, 10, 11, 15. The shared
 * RAM can be anywhere between 80000h (512K) to FFC000h (16M-16K) in 16K
 * increments.
 *
 * The Western Digital WD8003E appeared at around the same time as Novell's
 * NE1000 (1987). It is likewise a short 8-bit ISA card with 8Kx8 onboard
 * SRAM. The major difference is that rather than using remote DMA to move
 * data between the host and local RAM, the WD8003 directly mapps the onboard
 * memory to the host's address space (often called shared memory). A later
 * 16-bit WD8013 model used 8Kx16 SRAM, and there were follow-on WD8003 models
 * with 16K or 32K local RAM.
 *
 * Instead of mapping the PROM into the DP8390's local address space, the
 * WD8003/WD8013 exposes the node address through the I/O space; the DP8390's
 * local address space only contains buffer RAM.
 *
 * The WD8003 cannot use remote DMA at all; the host must use shared memory.
 * Remote DMA can be programmed but there is no way to trigger RDMA transfers.
 *
 * Western Digital's brand name for WD8003/WD8013 was EtherCard. Circa 1991,
 * WD sold the networking business to SMC; SMC continued to sell and further
 * develop the cards under the Elite brand name, also designated as the
 * SMC8000 series.
 *
 * The original WD8003E/EBT/WT uses very simple glue logic around the DP8390
 * and must be configured through jumpers. Newer WD8003EB/EP/EW/W/WC uses an
 * interface chip (WD83C583, WD83C584, or later) with an EEPROM and can be
 * configured through a software utility.
 *
 * Similarly the 16-bit WD8013EBT is configured only though jumpers, while
 * the newer WD8013EB/W/EW/EWC/WC/EPC are software configurable.
 *
 * The "Board ID" byte (at offset 6 in the PROM) is used to distinguish
 * between the various models.
 *
 * Newer WD cards use the WD83C690 controller rather than DP8390. The
 * WD83C690 is close enough to DP8390 that old WD drivers should work with
 * it, but it has a number of differences. It has no support for Remote DMA
 * whatsoever, and does not implement multicast filtering.
 *
 * The WD83C690 also handles receive buffer overflows somewhat differently;
 * the DP8390 never fills the last remaining buffer page, meaning that
 * CURR=BNRY indicates an empty buffer while CURR=BNRY-1 means buffer full.
 * The WD83C690 can fill all pages and decides whether it is full or empty
 * based on whether CURR or BNRY was changed more recently.
 *
 * Old Western Digital utilities/drivers may require the card to have WD's
 * old OUI of 00:00:0C and refuse to recognize the hardware otherwise.
 *
 * The emulation passes WD diagnostics with no errors (DIAGNOSE.EXE Ver 1.11,
 * dated 12/12/1989).
 *
 *
 * 3C503 EtherLink II
 * ------------------
 *
 * Common 3C503 configurations in Novell drivers:
 *   I/O Base = 300h, IRQ = 3 (default)
 * The I/O base can be set via jumpers to 2E0h, 2A0h, 280h, 250h, 350h, 330h,
 * 310h, or 300h (default). The ROM/RAM can be optionally mapped to one of
 * DC000-DFFFF, D8000-DBFFF, CC000-CFFFF, or C8000-CBFFF, again configured
 * through jumpers. The available IRQs are 2, 3, 4, or 5, and DRQs 1, 2, or 3,
 * both soft-configurable (no IRQ/DRQ jumpers).
 *
 * Yet another design based on the DP8390 was the 3Com 3C503 EtherLink II,
 * available sometime in 1988. Unlike Novell and WD, 3Com added a custom
 * host interface ASIC ("Gate Array") which handles all transfers to and from
 * the 8Kx8 onboard SRAM. The 3C503 can map the card's local RAM directly
 * into the host's address space, alternatively software can use either PIO
 * or 8-bit DMA to transfer data.
 *
 * For reasons that are not entirely clear, 3Com decided that the Remote DMA
 * implementation on the DP3890 (successfully used by the NE1000/NE2000) was
 * too buggy and the Gate Array essentially duplicates the Remote DMA
 * functionality, while also adding 8327 style DMA support (like the DP839EB
 * had) and optional shared RAM.
 *
 * Just like the NE1000/NE2000 and WD8003/WD8013, the 3C503 exists in an
 * 8-bit variant (EtherLink II) and a 16-bit variant (EtherLink II/16),
 * although both types are called 3C503.
 *
 * Since the 3C503 does not require shared RAM to operate, 3Com decided to
 * use a single memory mapping for both a boot ROM (if present) and shared
 * RAM. It is possible to boot from the ROM utilizing PIO or DMA for data
 * transfers, and later switch to shared RAM. However, 3Com needed to add
 * a hack for warm boot; the Vector Pointer Registers (VPTR0/1/2) contain
 * a 20-bit address and the Gate Array monitors the ISA bus for a read cycle
 * to that address. When a read cycle from the VPTR address occurs, the
 * memory mapping is switched from RAM to ROM. The VPTR registers are meant
 * to be programmed with the warm boot vector (often F000:FFF0 or FFFF0h).
 *
 * Some UNIX 3C503 drivers may require the card to have 3Com's old OUI
 * of 02:60:8C and refuse to detect the hardware otherwise. Likewise the
 * 3C503 diagnostics fail if the OUI is not 3Com's.
 *
 * The emulation passes 3Com diagnostics with flying colors (3C503.EXE Version
 * 1.5, dated 11/26/1991).
 *
 *
 * Linux Drivers
 *
 * The DP8390 driver (shared by NE1000/NE2000, WD8003/WD8013, and 3C503 drivers)
 * in Linux has severe bugs in the receive path. The driver clears receive
 * interrupts *after* going through the receive ring; that causes it to race
 * against the DP8390 chip and sometimes dismiss receive interrupts without
 * handling them. The driver also only receives at most 9 packets at a time,
 * which again can cause already received packets to be "hanging" in the receive
 * queue without the driver processing them.
 * In addition, prior to Linux 1.3.47, the driver incorrectly cleared the
 * overflow warning interrupt after any receive, causing it to potentially
 * miss overflow interrupts.
 *
 * The above bugs cause received packets to be lost or retransmitted by sender,
 * causing major TCP/IP performance issues when the DP8390 receives packets
 * very quickly. Other operating systems do not exhibit these bugs.
 *
 *
 * BSD Drivers
 *
 * For reasons that are not obvious, BSD drivers have configuration defaults far
 * off from the hardware defaults. For NE2000 (ne1), it is I/O base 300h and
 * IRQ 10. For WD8003E (we0), it is I/O base 280h, IRQ 9, memory D0000-D1FFF.
 * For 3C503 (ec0), it is I/O base 250h, IRQ 9, memory D8000-D9FFF (no DMA).
 *
 * The resource assigments are difficult to configure (sometimes impossible on
 * installation CDs) and the high IRQs may clash with PCI devices.
 *
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_DP8390
#include <VBox/vmm/pdmdev.h>
#include <VBox/vmm/pdmnetifs.h>
#include <VBox/vmm/pgm.h>
#include <VBox/version.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/critsect.h>
#include <iprt/net.h>
#include <iprt/string.h>
#include <iprt/time.h>
#ifdef IN_RING3
# include <iprt/mem.h>
# include <iprt/semaphore.h>
# include <iprt/uuid.h>
#endif

#include "VBoxDD.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/

#define DPNIC_SAVEDSTATE_VERSION        1

/** Maximum number of times we report a link down to the guest (failure to send frame) */
#define DPNIC_MAX_LINKDOWN_REPORTED     3

/** Maximum number of times we postpone restoring a link that is temporarily down. */
#define DPNIC_MAX_LINKRST_POSTPONED     3

/** Maximum frame size we handle */
#define MAX_FRAME                       1536

/* Size of the local RAM. */
#define DPNIC_MEM_SIZE  16384u

#define DPNIC_MEM_MASK  (DPNIC_MEM_SIZE - 1)

/* Although it is a 16-bit adapter, the EtherLink II only supports 8-bit DMA
 * and therefore DMA channels 1 to 3 are available.
 */
#define ELNKII_MIN_VALID_DMA    1
#define ELNKII_MAX_VALID_DMA    3

/* EtherLink II Gate Array revision. */
#define ELNKII_GA_REV           1


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/


/**
 * Emulated device types.
 */
enum DP8390_DEVICE_TYPE
{
    DEV_NE1000      = 0,    /* Novell NE1000 compatible (8-bit). */
    DEV_NE2000      = 1,    /* Novell NE2000 compatible (16-bit). */
    DEV_WD8003      = 2,    /* Western Digital WD8003 EtherCard Plus compatible (8-bit). */
    DEV_WD8013      = 3,    /* Western Digital WD8013 EtherCard Plus compatible (16-bit). */
    DEV_3C503       = 4     /* 3Com 3C503 EtherLink II compatible. */
};

/** WD8003/WD80013 specific register offsets. */
#define WDR_CTRL1       0   /* Control register 1. */
#define WDR_ATDET       1   /* 16-bit slot detect. */
#define WDR_IOBASE      2   /* I/O base register. */
#define WDR_CTRL2       5   /* Control register 2. */
#define WDR_JP          6   /* Jumper settings. */
#define WDR_PROM        8   /* PROM offset in I/O space. */

/** WD8013 Control Register 1. */
typedef struct WD_CTRL1 {
    uint8_t     A13_18 : 6; /* Shared memory decoding A13-A18. */
    uint8_t     MEME   : 1; /* Enable memory access. */
    uint8_t     RESET  : 1; /* Reset NIC core. */
} WD_CTRL1;
AssertCompile(sizeof(WD_CTRL1) == sizeof(uint8_t));

/** WD8013 Control Register 2. */
typedef struct WD_CTRL2 {
    uint8_t     A19_23 : 5; /* Shared memory decoding A19-A23. */
    uint8_t     res    : 1; /* Reserved. */
    uint8_t     MEMW   : 1; /* Memory width (16-bit wide if set). */
    uint8_t     M16    : 1; /* Allow 16-bit host memory cycles if set. */
} WD_CTRL2;
AssertCompile(sizeof(WD_CTRL2) == sizeof(uint8_t));


/** 3C503 EtherLink II specific register offsets. */
#define GAR_PSTR        0
#define GAR_PSPR        1
#define GAR_DQTR        2
#define GAR_R_BCFR      3
#define GAR_R_PCFR      4
#define GAR_GACFR       5
#define GAR_GACR        6
#define GAR_STREG       7
#define GAR_IDCFR       8
#define GAR_DAMSB       9
#define GAR_DALSB       10
#define GAR_VPTR2       11
#define GAR_VPTR1       12
#define GAR_VPTR0       13
#define GAR_RFMSB       14
#define GAR_RFLSB       15

/** 3C503 EtherLink II Gate Array registers. */

/** Gate Array DRQ Timer Register. */
typedef struct EL_DQTR {
    uint8_t     tb  : 5;    /* Timer bits; should be multiple of 4. */
    uint8_t     res : 3;    /* Reserved. */
} GA_DQTR;
AssertCompile(sizeof(GA_DQTR) == sizeof(uint8_t));

/** Gate Array Configuration Register. */
typedef struct EL_GACFR {
    uint8_t     mbs  : 3;   /* Memory Bank Select. */
    uint8_t     rsel : 1;   /* RAM Select. */
    uint8_t     test : 1;   /* Makes GA counters run at 10 MHz. */
    uint8_t     ows  : 1;   /* 0 Wait State for Gate Array. */
    uint8_t     tcm  : 1;   /* Terminal Count Mask for DMA (block interrupt if set). */
    uint8_t     nim  : 1;   /* NIC Interrupt Mask (block interrupt if set). */
} GA_GACFR;
AssertCompile(sizeof(GA_GACFR) == sizeof(uint8_t));

/** Gate Array Configuration Register. */
typedef struct EL_GACR {
    uint8_t     rst   : 1;  /* Hard reset GA/NIC. */
    uint8_t     xsel  : 1;  /* Transceiver Select. */
    uint8_t     ealo  : 1;  /* Window low 16 bytes of PROM to I/O space. */
    uint8_t     eahi  : 1;  /* Window high 16 bytes of PROM to I/O space. */
    uint8_t     share : 1;  /* Enable interrupt sharing. */
    uint8_t     dbsel : 1;  /* Double Buffer Select for FIFOs. */
    uint8_t     ddir  : 1;  /* DMA Direction (1=host to adapter). */
    uint8_t     start : 1;  /* Start Gate Array DMA. */
} GA_GACR;
AssertCompile(sizeof(GA_GACR) == sizeof(uint8_t));

/** Gate Array Status Register. */
typedef struct EL_STREG {
    uint8_t     rev   : 3;  /* Gate Array Revision. */
    uint8_t     dip   : 1;  /* DMA In Progress. */
    uint8_t     dtc   : 1;  /* DMA Terminal Count. */
    uint8_t     oflw  : 1;  /* Data Overflow. */
    uint8_t     uflw  : 1;  /* Data Underflow. */
    uint8_t     dprdy : 1;  /* Data Port Ready. */
} GA_STREG;
AssertCompile(sizeof(GA_STREG) == sizeof(uint8_t));

/** Gate Array Interrupt/DMA Configuration. */
typedef struct EL_IDCFR {
    uint8_t     drq1 : 1;   /* Enable DRQ 1. */
    uint8_t     drq2 : 1;   /* Enable DRQ 2. */
    uint8_t     drq3 : 1;   /* Enable DRQ 3. */
    uint8_t     res  : 1;   /* Unused. */
    uint8_t     irq2 : 1;   /* Enable IRQ 2. */
    uint8_t     irq3 : 1;   /* Enable IRQ 3. */
    uint8_t     irq4 : 1;   /* Enable IRQ 4. */
    uint8_t     irq5 : 1;   /* Enable IRQ 5. */
} GA_IDCFR;
AssertCompile(sizeof(GA_IDCFR) == sizeof(uint8_t));

/** Current DMA Address. */
typedef struct EL_CDADR {
    uint8_t     cdadr_lsb;  /* Current DMA Address LSB. */
    uint8_t     cdadr_msb;  /* Current DMA Address MSB. */
} GA_CDADR;
AssertCompile(sizeof(GA_CDADR) == sizeof(uint16_t));

/** 3C503 Gate Array state. */
typedef struct EL_GA_s {
    uint8_t         PSTR;       /* Page Start Register. */
    uint8_t         PSPR;       /* Page Stop Register. */
    union {
        uint8_t     DQTR;       /* DRQ Timer Register. */
        GA_DQTR     dqtr;
    };
    uint8_t         BCFR;       /* Base Configuration Register (R/O). */
    uint8_t         PCFR;       /* Boot PROM Configuration Register (R/O). */
    union {
        uint8_t     GACFR;
        GA_GACFR    gacfr;      /* Gate Array Configuration Register. */
    };
    union {
        uint8_t     GACR;       /* Gate Array Control Register. */
        GA_GACR     gacr;
    };
    union {
        uint8_t     STREG;      /* Gate Array Status Register (R/O). */
        GA_STREG    streg;
    };
    union {
        uint8_t     IDCFR;      /* Interrupt/DMA Configuration Register. */
        GA_IDCFR    idcfr;
    };
    uint8_t         DAMSB;      /* DMA Address MSB. */
    uint8_t         DALSB;      /* DMA Address LSB. */
    uint8_t         VPTR2;      /* Vector Pointer 2. */
    uint8_t         VPTR1;      /* Vector Pointer 1. */
    uint8_t         VPTR0;      /* Vector Pointer 0. */
    union {
        uint16_t    CDADR;      /* Current DMA address (internal state). */
        GA_CDADR    cdadr;
    };
    bool            fGaIrq;     /* Gate Array IRQ (internal state). */
} EL_GA, *PEL_GA;

/** DP8390 core register offsets. */
#define DPR_CR          0

#define DPR_P0_R_CLDA0  1
#define DPR_P0_W_PSTART 1
#define DPR_P0_R_CLDA1  2
#define DPR_P0_W_PSTOP  2
#define DPR_P0_BNRY     3
#define DPR_P0_R_TSR    4
#define DPR_P0_W_TPSR   4
#define DPR_P0_R_NCR    5
#define DPR_P0_W_TBCR0  5
#define DPR_P0_R_FIFO   6
#define DPR_P0_W_TBCR1  6
#define DPR_P0_ISR      7
#define DPR_P0_R_CRDA0  8
#define DPR_P0_W_RSAR0  8
#define DPR_P0_R_CRDA1  9
#define DPR_P0_W_RSAR1  9
#define DPR_P0_W_RBCR0  10
#define DPR_P0_W_RBCR1  11
#define DPR_P0_R_RSR    12
#define DPR_P0_W_RCR    12
#define DPR_P0_R_CNTR0  13
#define DPR_P0_W_TCR    13
#define DPR_P0_R_CNTR1  14
#define DPR_P0_W_DCR    14
#define DPR_P0_R_CNTR2  15
#define DPR_P0_W_IMR    15

#define DPR_P1_CURR     7

#define DPR_P2_R_PSTART 1
#define DPR_P2_W_CLDA0  1
#define DPR_P2_R_PSTOP  2
#define DPR_P2_W_CLDA1  2
#define DPR_P2_RNXTPP   3   /* Remote Next Packet Pointer. */
#define DPR_P2_R_TPSR   4
#define DPR_P2_LNXTPP   5   /* Local Next Packet Pointer. */
#define DPR_P2_ADRCU    6   /* Address Counter (Upper). */
#define DPR_P2_ADRCL    7   /* Address Counter (Lower). */
#define DPR_P2_R_RCR    12
#define DPR_P2_R_TCR    13
#define DPR_P2_R_DCR    14
#define DPR_P2_R_IMR    15


/** DP8390 Packet Header. */
typedef struct DP_PKT_HDR {
    uint8_t     rcv_stat;   /* Receive Status. */
    uint8_t     next_ptr;   /* Next Packet Pointer. */
    uint16_t    byte_cnt;   /* Receive byte count. */
} DP_PKT_HDR;

/** Select values for CR.RD field. */
#define DP_CR_RDMA_INVL 0   /* Invalid value. */
#define DP_CR_RDMA_RD   1   /* Remote Read. */
#define DP_CR_RDMA_WR   2   /* Remote Write. */
#define DP_CR_RDMA_SP   3   /* Send Packet. */
#define DP_CR_RDMA_ABRT 4   /* Abort Remote DMA. */

/** DP8390 Command Register (CR). */
typedef struct DP_CR {
    uint8_t     STP : 1;    /* Stop. */
    uint8_t     STA : 1;    /* Start. */
    uint8_t     TXP : 1;    /* Transmit Packet. */
    uint8_t     RD  : 3;    /* Remote DMA Command. */
    uint8_t     PS  : 2;    /* Page Select. */
} DP_CR;
AssertCompile(sizeof(DP_CR) == sizeof(uint8_t));

/** DP8390 Interrupt Status Register (ISR). */
typedef struct DP_ISR {
    uint8_t     PRX : 1;    /* Packet Received. */
    uint8_t     PTX : 1;    /* Packet Transmitted. */
    uint8_t     RXE : 1;    /* Receive Error. */
    uint8_t     TXE : 1;    /* Transmit Error. */
    uint8_t     OVW : 1;    /* Overwrite Warning (no receive buffers). */
    uint8_t     CNT : 1;    /* Counter Overflow. */
    uint8_t     RDC : 1;    /* Remote DMA Complete. */
    uint8_t     RST : 1;    /* Reset Status. */
} DP_ISR;
AssertCompile(sizeof(DP_ISR) == sizeof(uint8_t));

/** DP8390 Interrupt Mask Register (IMR). */
typedef struct DP_IMR {
    uint8_t     PRXE : 1;   /* Packet Received Interrupt Enable. */
    uint8_t     PTXE : 1;   /* Packet Transmitted Interrupt Enable. */
    uint8_t     RXEE : 1;   /* Receive Error Interrupt Enable. */
    uint8_t     TXEE : 1;   /* Transmit Error Interrupt Enable. */
    uint8_t     OVWE : 1;   /* Overwrite Warning  Interrupt Enable. */
    uint8_t     CNTE : 1;   /* Counter Overflow Interrupt Enable. */
    uint8_t     RDCE : 1;   /* DMA Complete Interrupt Enable. */
    uint8_t     res  : 1;   /* Reserved. */
} DP_IMR;
AssertCompile(sizeof(DP_IMR) == sizeof(uint8_t));

/** DP8390 Data Configuration Register (DCR). */
typedef struct DP_DCR {
    uint8_t     WTS : 1;    /* Word Transfer Select. */
    uint8_t     BOS : 1;    /* Byte Order Select. */
    uint8_t     LAS : 1;    /* Long Address Select. */
    uint8_t     LS  : 1;    /* Loopback Select. */
    uint8_t     ARM : 1;    /* Auto-Initialize Remote. */
    uint8_t     FT  : 2;    /* Fifo Threshold Select. */
    uint8_t     res : 1;    /* Reserved. */
} DP_DCR;
AssertCompile(sizeof(DP_DCR) == sizeof(uint8_t));

/** Transmit Configuration Register (TCR). */
typedef struct DP_TCR {
    uint8_t     CRC  : 1;   /* Inhibit CRC. */
    uint8_t     LB   : 2;   /* Loopback Control. */
    uint8_t     ATD  : 1;   /* Auto Transmit Disable. */
    uint8_t     OFST : 1;   /* Collision Offset Enable. */
    uint8_t     res  : 3;   /* Reserved. */
} DP_TCR;
AssertCompile(sizeof(DP_TCR) == sizeof(uint8_t));

/** Transmit Status Register (TSR). */
typedef struct DP_TSR {
    uint8_t     PTX : 1;    /* Packet Transmitted. */
    uint8_t     DFR : 1;    /* Non-Deferred Transmission (reserved in DP83901A). */
    uint8_t     COL : 1;    /* Transmit Collided. */
    uint8_t     ABT : 1;    /* Transmit Aborted. */
    uint8_t     CRS : 1;    /* Carrier Sense Lost. */
    uint8_t     FU  : 1;    /* FIFO Underrun. */
    uint8_t     CDH : 1;    /* CD Heartbeat. */
    uint8_t     OWC : 1;    /* Out of Window Collision. */
} DP_TSR;
AssertCompile(sizeof(DP_TSR) == sizeof(uint8_t));

/** Receive Configuration Register (RCR). */
typedef struct DP_RCR {
    uint8_t     SEP  : 1;   /* Save Errored Packets. */
    uint8_t     AR   : 1;   /* Accept Runt Packets. */
    uint8_t     AB   : 1;   /* Accept Broadcast. */
    uint8_t     AM   : 1;   /* Accept Multicast. */
    uint8_t     PRO  : 1;   /* Promiscuous Physical. */
    uint8_t     MON  : 1;   /* Monitor Mode. */
    uint8_t     res  : 2;   /* Reserved. */
} DP_RCR;
AssertCompile(sizeof(DP_RCR) == sizeof(uint8_t));

/** Receive Status Register (RSR). */
typedef struct DP_RSR {
    uint8_t     PRX  : 1;   /* Packet Received Intact. */
    uint8_t     CRC  : 1;   /* CRC Error. */
    uint8_t     FAE  : 1;   /* Frame Alignment Error. */
    uint8_t     FO   : 1;   /* FIFO Overrun. */
    uint8_t     MPA  : 1;   /* Missed Packet. */
    uint8_t     PHY  : 1;   /* Physical/Multicast Address. */
    uint8_t     DIS  : 1;   /* Receiver Disabled. */
    uint8_t     DFR  : 1;   /* Deferring. */
} DP_RSR;
AssertCompile(sizeof(DP_RSR) == sizeof(uint8_t));

/** Transmit Byte Count Register. */
typedef struct DP_TBCR {
    uint8_t     TBCR0;
    uint8_t     TBCR1;
} DP_TBCR;
AssertCompile(sizeof(DP_TBCR) == sizeof(uint16_t));

/** Current Local DMA Address. */
typedef struct DP_CLDA {
    uint8_t     CLDA0;
    uint8_t     CLDA1;
} DP_CLDA;
AssertCompile(sizeof(DP_CLDA) == sizeof(uint16_t));

/** Remote Start Address Register. */
typedef struct DP_RSAR {
    uint8_t     RSAR0;
    uint8_t     RSAR1;
} DP_RSAR;
AssertCompile(sizeof(DP_RSAR) == sizeof(uint16_t));

/** Remote Byte Count Register. */
typedef struct DP_RBCR {
    uint8_t     RBCR0;
    uint8_t     RBCR1;
} DP_RBCR;
AssertCompile(sizeof(DP_RBCR) == sizeof(uint16_t));

/** Current Remote DMA Address. */
typedef struct DP_CRDA {
    uint8_t     CRDA0;
    uint8_t     CRDA1;
} DP_CRDA;
AssertCompile(sizeof(DP_CRDA) == sizeof(uint16_t));

/** Page 1 registers. */
/* All registers read/write without side effects, unlike pages 0/2. */
typedef struct DP_PG1 {
    uint8_t     dummy_cr;
    uint8_t     PAR[6];     /* Physical Address PAR0-PAR5. */
    uint8_t     dummy_curr; /* Current Page Register. */
    uint8_t     MAR[8];     /* Multicast Address Register MAR0-MAR7. */
} DP_PG1;
AssertCompile(sizeof(DP_PG1) == 16);

/** DP8390 FIFO. Not all of the state is explicitly accessible. */
typedef struct DP_FIFO {
    uint8_t     rp;             /* Read pointer. */
    uint8_t     wp;             /* Write pointer. */
    uint8_t     fifo[16];       /* 16 bytes of FIFO. */
} DP_FIFO;

/**
 * Core DP8390 chip state.
 */
typedef struct DP8390CORE
{
    union {
        uint8_t     CR;         /* Command Register. */
        DP_CR       cr;
    };
    union {
        uint8_t     DCR;        /* Data Control Register. */
        DP_DCR      dcr;
    };
    /* Interrupt control. */
    union {
        uint8_t     ISR;        /* Interrupt Status Register. */
        DP_ISR      isr;
    };
    union {
        uint8_t     IMR;        /* Interrupt Mask Register. */
        DP_IMR      imr;
    };
    /* Receive state. */
    union {
        uint8_t     RCR;        /* Receive Control Register. */
        DP_RCR      rcr;
    };
    union {
        uint8_t     RSR;        /* Receive Status register. */
        DP_RSR      rsr;
    };
    /* Transmit State. */
    union {
        uint8_t     TCR;        /* Transmit Control Register. */
        DP_TCR      tcr;
    };
    union {
        uint8_t     TSR;        /* Transmit Status register. */
        DP_TSR      tsr;
    };
    uint8_t         NCR;        /* Number of Collisions Register. */
    /* Local DMA transmit state. */
    uint8_t         TPSR;       /* Transmit Page Start. */
    union {
        uint16_t    TBCR;       /* Transmit Byte Count. */
        DP_TBCR     tbcr;
    };
    /* Local DMA receive state. */
    union {
        uint16_t    CLDA;       /* Current Local DMA Address. */
        DP_CLDA     clda;
    };
    uint8_t         PSTART;     /* Page Start. */
    uint8_t         PSTOP;      /* Page Stop. */
    uint8_t         CURR;       /* Current Page. */
    uint8_t         BNRY;       /* Boundary Page. Also spelled BNDRY. */
    /* Remote DMA state. */
    union {
        uint16_t    RSAR;       /* Remote Start Address Register. */
        DP_RSAR     rsar;
    };
    union {
        uint16_t    RBCR;       /* Remote Byte Count Register. */
        DP_RBCR     rbcr;
    };
    union {
        uint16_t    CRDA;       /* Current Remote DMA Address. */
        DP_CRDA     crda;
    };
    /* Miscellaneous state. */
    uint8_t         lnxtpp;     /* Local Next Packet Pointer. */
    uint8_t         rnxtpp;     /* Remote Next Packet Pointer. */
    /* Tally counters. */
    uint8_t         CNTR0;      /* Frame Alignment Errors. */
    uint8_t         CNTR1;      /* CRC Errors. */
    uint8_t         CNTR2;      /* Missed Packet Errors. */
    union {
        uint8_t PG1[sizeof(DP_PG1)];
        DP_PG1      pg1;        /* All Page 1 Registers. */
    };
    DP_FIFO         fifo;       /* The internal FIFO. */
} DP8390CORE, *PDP8390CORE;

/**
 * DP8390-based card state.
 */
typedef struct DPNICSTATE
{
    /** Restore timer.
     *  This is used to disconnect and reconnect the link after a restore. */
    TMTIMERHANDLE                       hTimerRestore;

    /** Transmit signaller. */
    PDMTASKHANDLE                       hXmitTask;
    /** Receive ready signaller. */
    PDMTASKHANDLE                       hCanRxTask;

    /** Emulated device type. */
    uint8_t                             uDevType;
    /** State of the card's interrupt request signal. */
    bool                                fNicIrqActive;

    /** Core DP8390 chip state. */
    DP8390CORE                          core;

    /** WD80x3 Control Register 1. */
    union {
        uint8_t                         CTRL1;
        WD_CTRL1                        ctrl1;
    };
    /** WD80x3 Control Register 2. */
    union {
        uint8_t                         CTRL2;
        WD_CTRL2                        ctrl2;
    };

    /** 3C503 Gate Array state. */
    EL_GA                               ga;
    /** The 3C503 soft-configured ISA DMA channel. */
    uint8_t                             uElIsaDma;

    /** The PROM contents. 32 bytes addressable, R/O. */
    uint8_t                             aPROM[32];

    /** Shared RAM base. */
    RTGCPHYS                            MemBase;
    /** Shared RAM MMIO region handle. */
    PGMMMIO2HANDLE                      hSharedMem;
    /** Shared RAM size. */
    RTGCPHYS                            cbMemSize;

    /** Base port of the I/O space region. */
    RTIOPORT                            IOPortBase;
    /** The configured ISA IRQ. */
    uint8_t                             uIsaIrq;
    /** The configured ISA DMA channel. */
    uint8_t                             uIsaDma;
    /** If set the link is currently up. */
    bool                                fLinkUp;
    /** If set the link is temporarily down because of a saved state load. */
    bool                                fLinkTempDown;
    /** Number of times we've reported the link down. */
    uint16_t                            cLinkDownReported;
    /** Number of times we've postponed the link restore. */
    uint16_t                            cLinkRestorePostponed;

    /** The "hardware" MAC address. */
    RTMAC                               MacConfigured;

    /** Set if DPNICSTATER3::pDrv is not NULL. */
    bool                                fDriverAttached;
    /** The LED. */
    PDMLED                              Led;
    /** Status LUN: The LED ports. */
    PDMILEDPORTS                        ILeds;
    /** Partner of ILeds. */
    R3PTRTYPE(PPDMILEDCONNECTORS)       pLedsConnector;

    /** Access critical section. */
    PDMCRITSECT                         CritSect;
    /** Event semaphore for blocking on receive. */
    RTSEMEVENT                          hEventOutOfRxSpace;
    /** We are waiting/about to start waiting for more receive buffers. */
    bool volatile                       fMaybeOutOfSpace;

    /* MS to wait before we enable the link. */
    uint32_t                            cMsLinkUpDelay;
    /** The device instance number (for logging). */
    uint32_t                            iInstance;

    STAMCOUNTER                         StatReceiveBytes;
    STAMCOUNTER                         StatTransmitBytes;
#ifdef VBOX_WITH_STATISTICS
    STAMPROFILEADV                      StatIOReadRZ;
    STAMPROFILEADV                      StatIOReadR3;
    STAMPROFILEADV                      StatIOWriteRZ;
    STAMPROFILEADV                      StatIOWriteR3;
    STAMPROFILEADV                      StatReceive;
    STAMPROFILEADV                      StatTransmitR3;
    STAMPROFILEADV                      StatTransmitRZ;
    STAMPROFILE                         StatTransmitSendR3;
    STAMPROFILE                         StatTransmitSendRZ;
    STAMPROFILE                         StatRxOverflow;
    STAMCOUNTER                         StatRxOverflowWakeup;
    STAMCOUNTER                         StatRxCanReceiveNow;
    STAMCOUNTER                         StatRxCannotReceiveNow;
    STAMPROFILEADV                      StatInterrupt;
    STAMCOUNTER                         StatDropPktMonitor;
    STAMCOUNTER                         StatDropPktRcvrDis;
    STAMCOUNTER                         StatDropPktVeryShort;
    STAMCOUNTER                         StatDropPktVMNotRunning;
    STAMCOUNTER                         StatDropPktNoLink;
    STAMCOUNTER                         StatDropPktNoMatch;
    STAMCOUNTER                         StatDropPktNoBuffer;
#endif /* VBOX_WITH_STATISTICS */

    /** NIC-specific ISA I/O ports. */
    IOMIOPORTHANDLE                     hIoPortsNic;

    /** Common DP8390 core I/O ports. */
    IOMIOPORTHANDLE                     hIoPortsCore;

    /** The runt pad buffer (only really needs 60 bytes). */
    uint8_t                             abRuntBuf[64];

    /** The packet buffer. */
    uint8_t                             abLocalRAM[DPNIC_MEM_SIZE];

    /** The loopback transmit buffer (avoid stack allocations). */
    uint8_t                             abLoopBuf[DPNIC_MEM_SIZE];  /// @todo Can this be smaller?
} DPNICSTATE, *PDPNICSTATE;


/**
 * DP8390 state for ring-3.
 *
 * @implements  PDMIBASE
 * @implements  PDMINETWORKDOWN
 * @implements  PDMINETWORKCONFIG
 * @implements  PDMILEDPORTS
 */
typedef struct DPNICSTATER3
{
    /** Pointer to the device instance. */
    PPDMDEVINSR3                        pDevIns;
    /** Pointer to the connector of the attached network driver. */
    PPDMINETWORKUPR3                    pDrv;
    /** Pointer to the attached network driver. */
    R3PTRTYPE(PPDMIBASE)                pDrvBase;
    /** LUN\#0 + status LUN: The base interface. */
    PDMIBASE                            IBase;
    /** LUN\#0: The network port interface. */
    PDMINETWORKDOWN                     INetworkDown;
    /** LUN\#0: The network config port interface. */
    PDMINETWORKCONFIG                   INetworkConfig;

    /** Status LUN: The LED ports. */
    PDMILEDPORTS                        ILeds;
    /** Partner of ILeds. */
    R3PTRTYPE(PPDMILEDCONNECTORS)       pLedsConnector;
} DPNICSTATER3;
/** Pointer to a DP8390 state structure for ring-3. */
typedef DPNICSTATER3 *PDPNICSTATER3;


/**
 * DP8390 state for ring-0.
 */
typedef struct DPNICSTATER0
{
    /** Pointer to the connector of the attached network driver. */
    PPDMINETWORKUPR0                    pDrv;
} DPNICSTATER0;
/** Pointer to a DP8390 state structure for ring-0. */
typedef DPNICSTATER0 *PDPNICSTATER0;


/**
 * DP8390 state for raw-mode.
 */
typedef struct DPNICSTATERC
{
    /** Pointer to the connector of the attached network driver. */
    PPDMINETWORKUPRC                    pDrv;
} DPNICSTATERC;
/** Pointer to a DP8390 state structure for raw-mode. */
typedef DPNICSTATERC *PDPNICSTATERC;


/** The DP8390 state structure for the current context. */
typedef CTX_SUFF(DPNICSTATE) DPNICSTATECC;
/** Pointer to a DP8390 state structure for the current
 *  context. */
typedef CTX_SUFF(PDPNICSTATE) PDPNICSTATECC;


#ifndef VBOX_DEVICE_STRUCT_TESTCASE


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/

static int dp8390CoreAsyncXmitLocked(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PDPNICSTATECC pThisCC, bool fOnWorkerThread);

/**
 * Checks if the link is up.
 * @returns true if the link is up.
 * @returns false if the link is down.
 */
DECLINLINE(bool) dp8390IsLinkUp(PDPNICSTATE pThis)
{
    return pThis->fDriverAttached && !pThis->fLinkTempDown && pThis->fLinkUp;
}


/* Table and macro borrowed from DevPCNet.cpp. */
#define CRC(crc, ch)  (crc = (crc >> 8) ^ crctab[(crc ^ (ch)) & 0xff])

/* generated using the AUTODIN II polynomial
 *   x^32 + x^26 + x^23 + x^22 + x^16 +
 *   x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1
 */
static const uint32_t crctab[256] =
{
    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
    0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
    0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
    0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
    0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
    0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
    0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
    0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
    0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
    0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
    0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
    0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
    0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
    0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
    0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
    0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
    0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
    0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
    0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
    0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
    0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
    0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
    0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
    0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
    0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
    0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
    0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
    0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
    0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
    0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
    0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
    0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
    0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
    0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
    0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
    0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
    0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
    0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
    0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
    0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
    0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
    0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
    0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
    0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
    0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
    0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
    0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
    0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
    0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
    0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
    0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
    0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
    0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
    0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
    0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
    0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
    0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
    0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
    0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
    0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
};


#ifndef ETHER_IS_MULTICAST /* Net/Open BSD macro it seems */
#define ETHER_IS_MULTICAST(a) ((*(uint8_t *)(a)) & 1)
#endif


/**
 * Check if incoming frame matches the station address.
 */
DECLINLINE(int) padr_match(PDPNICSTATE pThis, const uint8_t *buf)
{
    RTNETETHERHDR   *hdr = (RTNETETHERHDR *)buf;
    int             result;

    /* Checks own address only; always enabled if receiver on. */
    result = !memcmp(hdr->DstMac.au8, pThis->core.pg1.PAR, 6);

    return result;
}


/**
 * Check if incoming frame is an accepted broadcast frame.
 */
DECLINLINE(int) padr_bcast(PDPNICSTATE pThis, const uint8_t *buf)
{
    static uint8_t  aBCAST[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    RTNETETHERHDR   *hdr = (RTNETETHERHDR *)buf;
    int result = pThis->core.rcr.AB && !memcmp(hdr->DstMac.au8, aBCAST, 6);
    return result;
}


/**
 * Check if incoming frame is an accepted multicast frame.
 */
DECLINLINE(int) padr_mcast(PDPNICSTATE pThis, const uint8_t *buf, int *mcast_type)
{
    uint32_t        crc = UINT32_MAX;
    RTNETETHERHDR   *hdr = (RTNETETHERHDR *)buf;
    int             result = 0;

    /* If multicast addresses are enabled, and the destination
     * address is in fact multicast, the address must be run through
     * the CRC generator and matched against the multicast filter
     * array.
     */
    if (pThis->core.rcr.AM && ETHER_IS_MULTICAST(hdr->DstMac.au8))
    {
        unsigned        i;
        const uint8_t   *p = buf;
        unsigned        crc_frag, crc_rev;
        unsigned        ma_bit_mask, ma_byte_idx;

        /* Indicate to caller that the address is a multicast one, regardless
         * of whether it's accepted or not.
         */
        *mcast_type = 1;

        for (i = 0; i < sizeof(hdr->DstMac); ++i)
            CRC(crc, *p++);

        /* The top 6 bits of the CRC calculated from the destination address
         * becomes an index into the 64-bit multicast address register. Sadly
         * our CRC algorithm is bit-reversed (Ethernet shifts bits out MSB first)
         * so instead of the top 6 bits of the CRC we have to take the bottom 6
         * and reverse the bits.
         */
        crc_frag  = crc & 63;

        for (i = 0, crc_rev = 0; i < 6; ++i)
            crc_rev |= ((crc_frag >> i) & 1) * (0x20 >> i);

        ma_bit_mask = 1 << (crc_rev & 7);
        ma_byte_idx = crc_rev / 8;
        Log3Func(("crc=%08X, crc_frag=%u, crc_rev=%u, ma_byte_idx=%u, ma_bit_mask=%02X\n", crc, crc_frag, crc_rev, ma_byte_idx, ma_bit_mask));
        Log3Func(("MAR: %02X:%02X:%02X:%02X %02X:%02X:%02X:%02X\n", pThis->core.pg1.MAR[0], pThis->core.pg1.MAR[1], pThis->core.pg1.MAR[2], pThis->core.pg1.MAR[3], pThis->core.pg1.MAR[4], pThis->core.pg1.MAR[5], pThis->core.pg1.MAR[6], pThis->core.pg1.MAR[7]));

        /* The multicast filtering logic is fairly extensively
         * verified by EtherLink II diagnostics (3C503.EXE).
         */
        if (pThis->core.pg1.MAR[ma_byte_idx] & ma_bit_mask)
        {
            Log3Func(("Passed multicast filter\n"));
            result = 1;
        }
    }

    return result;
}


/**
 * Check if incoming frame is an accepted promiscuous frame.
 */
DECLINLINE(int) padr_promi(PDPNICSTATE pThis, const uint8_t *buf)
{
    RTNETETHERHDR   *hdr = (RTNETETHERHDR *)buf;
    int result = pThis->core.rcr.PRO && !ETHER_IS_MULTICAST(hdr->DstMac.au8);
    return result;
}


/**
 * Update the device IRQ line based on internal state.
 */
static void dp8390CoreUpdateIrq(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    bool     fCoreIrqActive = false;
    bool     fNicIrqActive  = false;

    STAM_PROFILE_ADV_START(&pThis->StatInterrupt, a);

    /* Set the ISR.CNT bit based on the counter state (top counter bits ANDed together). */
    pThis->core.isr.CNT = (pThis->core.CNTR0 & pThis->core.CNTR1 & pThis->core.CNTR2) >> 7;

    /* IRQ is active if a bit is set in ISR and the corresponding bit
     * is set in IMR. No additional internal state needed.
     */
    Assert(!pThis->core.imr.res);
    if (pThis->core.ISR & pThis->core.IMR)
        fCoreIrqActive = true;

    /* The 3C503 has additional interrupt sources and control. For other device
     * types, the extras magically work out to be a no-op.
     */
    pThis->ga.fGaIrq = pThis->ga.streg.dtc && !pThis->ga.gacfr.tcm;
    fNicIrqActive = (fCoreIrqActive && !pThis->ga.gacfr.nim) || (pThis->ga.streg.dtc && !pThis->ga.gacfr.tcm);

    Log2Func(("#%d set irq fNicIrqActive=%d (fCoreIrqActive=%d, fGaIrq=%d)\n", pThis->iInstance, fNicIrqActive, fCoreIrqActive, pThis->ga.fGaIrq));

    /* The IRQ line typically does not change. */
    if (RT_UNLIKELY(fNicIrqActive != pThis->fNicIrqActive))
    {
        LogFunc(("#%d IRQ=%d, state=%d\n", pThis->iInstance, pThis->uIsaIrq, fNicIrqActive));
        /// @todo Handle IRQ 2/9 elsewhere
        PDMDevHlpISASetIrq(pDevIns, pThis->uIsaIrq == 2 ? 9 : pThis->uIsaIrq, fNicIrqActive);
        pThis->fNicIrqActive = fNicIrqActive;
    }
    STAM_PROFILE_ADV_STOP(&pThis->StatInterrupt, a);
}


/**
 * Perform a software reset of the NIC.
 */
static void dp8390CoreReset(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    LogFlowFunc(("#%d:\n", pThis->iInstance));

    /* DP8390 or DP83901A datasheet, section 11.0. */
    pThis->core.cr.TXP  = 0;
    pThis->core.cr.STA  = 0;
    pThis->core.cr.STP  = 1;
    pThis->core.cr.RD   = DP_CR_RDMA_ABRT;
    pThis->core.isr.RST = 1;
    pThis->core.IMR     = 0;
    pThis->core.dcr.LAS = 0;
    pThis->core.tcr.LB  = 0;

    /// @todo Check if this really happens on soft reset
    /* Clear the internal FIFO including r/w pointers. */
    memset(&pThis->core.fifo, 0, sizeof(pThis->core.fifo));

    /* Make sure the IRQ line us updated. */
    dp8390CoreUpdateIrq(pDevIns, pThis);
}

#ifdef IN_RING3

static DECLCALLBACK(void) dp8390R3WakeupReceive(PPDMDEVINS pDevIns)
{
    PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    LogFlowFunc(("#%d\n", pThis->iInstance));
    STAM_COUNTER_INC(&pThis->StatRxOverflowWakeup);
    if (pThis->hEventOutOfRxSpace != NIL_RTSEMEVENT)
        RTSemEventSignal(pThis->hEventOutOfRxSpace);
}

/**
 * @callback_method_impl{FNPDMTASKDEV,
 * Signal to R3 that NIC is ready to receive a packet.
 */
static DECLCALLBACK(void) dpNicR3CanRxTaskCallback(PPDMDEVINS pDevIns, void *pvUser)
{
    RT_NOREF(pvUser);
    dp8390R3WakeupReceive(pDevIns);
}

#endif /* IN_RING3 */

/**
 * Read up to 256 bytes from a single page of local RAM.
 */
static void dpLocalRAMReadBuf(PDPNICSTATE pThis, uint16_t addr, unsigned cb, uint8_t *pDst)
{
    if ((RT_LOBYTE(addr) + cb) > 256)
    {
        LogFunc(("#%d: addr=%04X, cb=%X, cb!!\n", pThis->iInstance, addr, cb));
        cb = 256 - RT_LOBYTE(addr);
    }

    /* A single page is always either entirely inside or outside local RAM. */
    if (pThis->uDevType == DEV_NE1000)
    {
        /* Only 14 bits of address are decoded. */
        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            memcpy(pDst, &pThis->abLocalRAM[addr], cb);
        }
        else
            LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else if (pThis->uDevType == DEV_NE2000)
    {
        /* Only 15 bits of address are decoded. */
        addr &= 0x7fff;
        if (addr >= 0x4000)
        {
            /* Local RAM is mapped at 4000h-7FFFh. */
            addr -= 0x4000;
            memcpy(pDst, &pThis->abLocalRAM[addr], cb);
        }
        else
            LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
    {
        /* Local RAM is mapped starting at address zero. */
        addr &= DPNIC_MEM_MASK;
        if (addr + cb <= DPNIC_MEM_SIZE)
            memcpy(pDst, &pThis->abLocalRAM[addr], cb);
        else
            LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else if (pThis->uDevType == DEV_3C503)
    {
        /* Only 14 bits of address are decoded. */
        /// @todo Is there any internal wrap-around in the 3C503 too?
        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            memcpy(pDst, &pThis->abLocalRAM[addr], cb);
        }
        else
            LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else
    {
        Assert(0);
    }
}


#ifdef IN_RING3

/**
 * Write up to 256 bytes into a single page of local RAM.
 */
static void dpLocalRAMWriteBuf(PDPNICSTATE pThis, uint16_t addr, unsigned cb, const uint8_t *pSrc)
{
    if ((RT_LOBYTE(addr) + cb) > 256)
    {
        LogFunc(("#%d: addr=%04X, cb=%X, cb!!\n", pThis->iInstance, addr, cb));
        cb = 256 - RT_LOBYTE(addr);
    }

    /* A single page is always either entirely inside or outside local RAM. */
    if (pThis->uDevType == DEV_NE1000)
    {
        /* Only 14 bits of address are decoded. */
        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
        }
        else
            LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else if (pThis->uDevType == DEV_NE2000)
    {
        /* Only 14 bits of address are decoded. */
        addr &= 0x7fff;
        if (addr >= 0x4000)
        {
            /* Local RAM is mapped at 4000h-7FFFh. */
            addr -= 0x4000;
            memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
        }
        else
            LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
    {
        /* Local RAM is mapped starting at address zero. */
        addr &= DPNIC_MEM_MASK;
        if (addr + cb <= DPNIC_MEM_SIZE)
            memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
        else
            LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else if (pThis->uDevType == DEV_3C503)
    {
        /* Only 14 bits of address are decoded. */
        /// @todo Is there any internal wrap-around in the 3C503 too?
        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
        }
        else
            LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
    }
    else
    {
        Assert(0);
    }
}


/**
 * Receive an arbitrarily long buffer into the receive ring starting at CLDA.
 * Update RSR, CLDA, and other state in the process.
 */
static void dp8390CoreReceiveBuf(PDPNICSTATE pThis, DP_RSR *pRsr, const uint8_t *src, unsigned cbLeft, bool fLast)
{
    LogFlow(("#%d: Initial CURR=%02X00 CLDA=%04X\n", pThis->iInstance, pThis->core.CURR, pThis->core.CLDA));

    while (cbLeft)
    {
        unsigned    cbWrite;
        unsigned    cbPage;

        /* Write at most up to the end of a page. */
        cbPage = cbWrite = 256 - pThis->core.clda.CLDA0;
        if (cbWrite > cbLeft)
            cbWrite = cbLeft;
        Log2Func(("#%d: cbLeft=%d CURR=%02X00 CLDA=%04X\n", pThis->iInstance, cbLeft, pThis->core.CURR, pThis->core.CLDA));
        dpLocalRAMWriteBuf(pThis, pThis->core.CLDA, cbWrite, src);
        src += cbWrite;

        /* If this is the last fragment of a received frame, we need to
         * round CLDA up to the next page boundary to correctly evaluate
         * buffer overflows and the next pointer. Otherwise we just
         * add however much data we had so that we can continue writing
         * at the CLDA position.
         */
        if (fLast && (cbWrite == cbLeft))
        {
            Log3Func(("#%d: Round up: CLDA=%04X cbPage=%X\n", pThis->iInstance, pThis->core.CLDA, cbPage));
            pThis->core.CLDA += cbPage;
        }
        else
            pThis->core.CLDA += cbWrite;

        Log3Func(("#%d: Final CURR=%02X00 CLDA=%04X\n", pThis->iInstance, pThis->core.CURR, pThis->core.CLDA));
        /* If at end of ring, wrap around. */
        if (pThis->core.clda.CLDA1 == pThis->core.PSTOP)
            pThis->core.clda.CLDA1 = pThis->core.PSTART;

        /* Check for buffer overflow. */
        if (pThis->core.clda.CLDA1 == pThis->core.BNRY)
        {
            pThis->core.isr.OVW = 1;
            pThis->core.isr.RST = 1;
            pRsr->MPA = 1;  /* Indicates to caller that receive was aborted. */
            STAM_COUNTER_INC(&pThis->StatDropPktNoBuffer);
            Log3Func(("#%d: PSTART=%02X00 PSTOP=%02X00 BNRY=%02X00 CURR=%02X00 -- overflow!\n", pThis->iInstance, pThis->core.PSTART, pThis->core.PSTOP, pThis->core.BNRY, pThis->core.CURR));
            break;
        }
        cbLeft -= cbWrite;
    }
}

/**
 * Write incoming data into the packet buffer.
 */
static void dp8390CoreReceiveLocked(PPDMDEVINS pDevIns, PDPNICSTATE pThis, const uint8_t *src, size_t cbToRecv)
{
    int is_padr = 0, is_bcast = 0, is_mcast = 0, is_prom = 0;
    int mc_type = 0;

    /*
     * Drop all packets if the VM is not running yet/anymore.
     */
    VMSTATE enmVMState = PDMDevHlpVMState(pDevIns);
    if (    enmVMState != VMSTATE_RUNNING
        &&  enmVMState != VMSTATE_RUNNING_LS)
    {
        STAM_COUNTER_INC(&pThis->StatDropPktVMNotRunning);
        return;
    }

    /*
     * Drop all packets if the cable is not connected.
     */
    if (RT_UNLIKELY(!dp8390IsLinkUp(pThis)))
    {
        STAM_COUNTER_INC(&pThis->StatDropPktNoLink);
        return;
    }

    /*
     * Drop everything if NIC is not started or in reset.
     */
    if (RT_UNLIKELY(!pThis->core.cr.STA || pThis->core.cr.STP))
    {
        STAM_COUNTER_INC(&pThis->StatDropPktRcvrDis);
        return;
    }

    /* Drop impossibly short packets. The DP8390 requires a packet to have
     * at least 8 bytes to even qualify as a runt. We can also assume that
     * there is a complete destination address at that point.
     */
    if (RT_UNLIKELY(cbToRecv < 8))
    {
        STAM_COUNTER_INC(&pThis->StatDropPktVeryShort);
        return;
    }

    LogFlowFunc(("#%d: size on wire=%d\n", pThis->iInstance, cbToRecv));

    /*
     * Perform address matching. Packets which do not pass any address
     * matching logic are ignored.
     */
    if (   (is_padr  = padr_match(pThis, src))
        || (is_bcast = padr_bcast(pThis, src))
        || (is_mcast = padr_mcast(pThis, src, &mc_type))
        || (is_prom  = padr_promi(pThis, src)))
    {
        union {
            uint8_t     nRSR;
            DP_RSR      nRsr;
        };
        uint32_t    fcs = 0;

        nRSR = 0;
        Log2Func(("#%d Packet passed address filter (is_padr=%d, is_bcast=%d, is_mcast=%d, is_prom=%d), size=%d\n", pThis->iInstance, is_padr, is_bcast, is_mcast, is_prom, cbToRecv));

        if (is_bcast || mc_type)
            nRsr.PHY = 1;

        /* In Monitor Mode, just increment the tally counter. */
        if (RT_UNLIKELY(pThis->core.rcr.MON))
        {
            STAM_COUNTER_INC(&pThis->StatDropPktMonitor);
            nRsr.MPA = 1;
            if (pThis->core.CNTR2 <= 192)
                pThis->core.CNTR2++;    /* Relies on UpdateIrq to be run. */
        }
        else
        {
            /* Error detection: FCS and frame alignment errors cannot happen,
             * likewise FIFO overruns can't.
             * Runts are padded up to the required minimum. Note that the DP8390
             * documentation considers packets smaller than 64 bytes to be runts,
             * but that includes 32 bits of FCS.
             */

            /* See if we need to pad, and how much. Note that if there's any
             * room left in the receive buffers, a runt will fit even after padding.
             */
            if (RT_UNLIKELY(cbToRecv < 60))
            {
                /// @todo This really is kind of stupid. We shouldn't be doing any
                /// padding here, it should be done by the sending side!
                memset(pThis->abRuntBuf, 0, sizeof(pThis->abRuntBuf));
                memcpy(pThis->abRuntBuf, src, cbToRecv);
                cbToRecv = 60;
                src = pThis->abRuntBuf;
            }

            LogFlowFunc(("#%d: PSTART=%02X00 PSTOP=%02X00 BNRY=%02X00 CURR=%02X00\n", pThis->iInstance, pThis->core.PSTART, pThis->core.PSTOP, pThis->core.BNRY, pThis->core.CURR));

            /* All packets that passed the address filter are copied to local RAM.
             * Since the DP8390 does not know how long the frame is until it detects
             * end of frame, it can only detect an out-of-buffer condition after
             * filling up all available space. It then reports an error and rewinds
             * back to where it was before.
             *
             * We do not limit the incoming frame size except by available buffer space. /// @todo Except we do??
             */

            STAM_REL_COUNTER_ADD(&pThis->StatReceiveBytes, cbToRecv);

            /* Copy incoming data to the packet buffer. Start by setting CLDA
             * to CURR + 4, leaving room for header.
             */
            pThis->core.CLDA = RT_MAKE_U16(4, pThis->core.CURR);

            /* Receive the incoming frame. */
            Assert(cbToRecv < MAX_FRAME);   /// @todo Can we actually do bigger?
            dp8390CoreReceiveBuf(pThis, &nRsr, src, (unsigned)cbToRecv, false);
            /// @todo Use the same method for runt padding?

            /* If there was no overflow, add the FCS. */
            if (!nRsr.MPA)
            {
                fcs = 0xBADF00D;    // Just fake it, does anyone care?
                dp8390CoreReceiveBuf(pThis, &nRsr, (uint8_t *)&fcs, sizeof(fcs), true);
            }

            /* Error-free packets are considered intact. */
            if (!nRsr.CRC && !nRsr.FAE && !nRsr.FO && !nRsr.MPA)
            {
                nRsr.PRX = 1;
                pThis->core.isr.PRX = 1;
            }
            else
                pThis->core.isr.RXE = 1;

            /* For 'intact' packets, write the packet header. */
            if (nRsr.PRX)
            {
                DP_PKT_HDR  header;

                /* Round up CLDA to the next page. */
                if (pThis->core.clda.CLDA0)
                    pThis->core.CLDA = RT_MAKE_U16(0, pThis->core.clda.CLDA1 + 1);

                /* If entire frame was successfully received, write the packet header at the old CURR. */
                header.rcv_stat = nRSR;
                header.next_ptr = pThis->core.clda.CLDA1;
                /// @todo big endian (WTS)
                header.byte_cnt = (uint16_t)cbToRecv + sizeof(fcs);

                pThis->core.CLDA = RT_MAKE_U16(0, pThis->core.CURR);
                dpLocalRAMWriteBuf(pThis, pThis->core.CLDA, sizeof(header), (uint8_t *)&header);
                pThis->core.CLDA += sizeof(header);

                pThis->core.CURR = header.next_ptr;
            }
        }

        pThis->core.RSR = nRSR;

        Log2Func(("Receive completed, size=%d, CURR=%02X00, RSR=%02X, ISR=%02X\n", cbToRecv, pThis->core.CURR, pThis->core.RSR, pThis->core.ISR));
        dp8390CoreUpdateIrq(pDevIns, pThis);
    }
    else
    {
        Log3Func(("#%d Packet did not pass address filter, size=%d\n", pThis->iInstance, cbToRecv));
        STAM_COUNTER_INC(&pThis->StatDropPktNoMatch);
    }
}

#endif /* IN_RING3 */


/**
 * Transmit a packet from local memory.
 *
 * @returns VBox status code.  VERR_TRY_AGAIN is returned if we're busy.
 *
 * @param   pDevIns             The device instance data.
 * @param   pThis               The device state data.
 * @param   fOnWorkerThread     Whether we're on a worker thread or on an EMT.
 */
static int dp8390CoreXmitPacket(PPDMDEVINS pDevIns, PDPNICSTATE pThis, bool fOnWorkerThread)
{
    PDPNICSTATECC   pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
    RT_NOREF_PV(fOnWorkerThread);
    int rc;

    /*
     * Grab the xmit lock of the driver as well as the DP8390 device state.
     */
    PPDMINETWORKUP pDrv = pThisCC->pDrv;
    if (pDrv)
    {
        rc = pDrv->pfnBeginXmit(pDrv, false /*fOnWorkerThread*/);
        if (RT_FAILURE(rc))
            return rc;
    }
    rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    if (RT_SUCCESS(rc))
    {
        /*
         * Do the transmitting.
         */
        int rc2 = dp8390CoreAsyncXmitLocked(pDevIns, pThis, pThisCC, false /*fOnWorkerThread*/);
        AssertReleaseRC(rc2);

        /*
         * Release the locks.
         */
        PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
    }
    else
        AssertLogRelRC(rc);
    if (pDrv)
        pDrv->pfnEndXmit(pDrv);

    return rc;
}


#ifdef IN_RING3

/**
 * @callback_method_impl{FNPDMTASKDEV,
 * This is just a very simple way of delaying sending to R3.
 */
static DECLCALLBACK(void) dpNicR3XmitTaskCallback(PPDMDEVINS pDevIns, void *pvUser)
{
    PDPNICSTATE   pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    NOREF(pvUser);

    /*
     * Transmit if we can.
     */
    dp8390CoreXmitPacket(pDevIns, pThis, true /*fOnWorkerThread*/);
}

#endif /* IN_RING3 */


/**
 * Allocates a scatter/gather buffer for a transfer.
 *
 * @returns See PPDMINETWORKUP::pfnAllocBuf.
 * @param   pThis       The device instance.
 * @param   pThisCC     The device state for current context.
 * @param   cbMin       The minimum buffer size.
 * @param   fLoopback   Set if we're in loopback mode.
 * @param   pSgLoop     Pointer to stack storage for the loopback SG.
 * @param   ppSgBuf     Where to return the SG buffer descriptor on success.
 *                      Always set.
 */
DECLINLINE(int) dp8390XmitAllocBuf(PDPNICSTATE pThis, PDPNICSTATECC pThisCC, size_t cbMin, bool fLoopback,
                                   PPDMSCATTERGATHER pSgLoop, PPPDMSCATTERGATHER ppSgBuf)
{
    int rc;

    if (!fLoopback)
    {
        PPDMINETWORKUP pDrv = pThisCC->pDrv;
        if (RT_LIKELY(pDrv))
        {
            rc = pDrv->pfnAllocBuf(pDrv, cbMin, NULL /*pGso*/, ppSgBuf);
            AssertMsg(rc == VINF_SUCCESS || rc == VERR_TRY_AGAIN || rc == VERR_NET_DOWN || rc == VERR_NO_MEMORY, ("%Rrc\n", rc));
            if (RT_FAILURE(rc))
                *ppSgBuf = NULL;
        }
        else
        {
            rc = VERR_NET_DOWN;
            *ppSgBuf = NULL;
        }
    }
    else
    {
        /* Fake loopback allocator. */
        pSgLoop->fFlags      = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1;
        pSgLoop->cbUsed      = 0;
        pSgLoop->cbAvailable = sizeof(pThis->abLoopBuf);
        pSgLoop->pvAllocator = pThis;
        pSgLoop->pvUser      = NULL;
        pSgLoop->cSegs       = 1;
        pSgLoop->aSegs[0].cbSeg = sizeof(pThis->abLoopBuf);
        pSgLoop->aSegs[0].pvSeg = pThis->abLoopBuf;
        *ppSgBuf = pSgLoop;
        rc = VINF_SUCCESS;
    }
    return rc;
}


/**
 * Sends the scatter/gather buffer.
 *
 * Wrapper around PDMINETWORKUP::pfnSendBuf, so check it out for the fine print.
 *
 * @returns See PDMINETWORKUP::pfnSendBuf.
 * @param   pDevIns         The device instance.
 * @param   pThisCC         The current context device state.
 * @param   fLoopback       Set if we're in loopback mode.
 * @param   pSgBuf          The SG to send.
 * @param   fOnWorkerThread Set if we're being called on a work thread.  Clear
 *                          if an EMT.
 */
DECLINLINE(int) dp8390CoreXmitSendBuf(PPDMDEVINS pDevIns, PDPNICSTATECC pThisCC, bool fLoopback, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int rc;
    STAM_REL_COUNTER_ADD(&pThis->StatTransmitBytes, pSgBuf->cbUsed);
    if (!fLoopback)
    {
        STAM_PROFILE_START(&pThis->CTX_SUFF_Z(StatTransmitSend), a);
        if (pSgBuf->cbUsed > 70) /* unqualified guess */
            pThis->Led.Asserted.s.fWriting = pThis->Led.Actual.s.fWriting = 1;

        PPDMINETWORKUP pDrv = pThisCC->pDrv;
        if (RT_LIKELY(pDrv))
        {
            rc = pDrv->pfnSendBuf(pDrv, pSgBuf, fOnWorkerThread);
            AssertMsg(rc == VINF_SUCCESS || rc == VERR_NET_DOWN || rc == VERR_NET_NO_BUFFER_SPACE, ("%Rrc\n", rc));
        }
        else
            rc = VERR_NET_DOWN;

        pThis->Led.Actual.s.fWriting = 0;
        STAM_PROFILE_STOP(&pThis->CTX_SUFF_Z(StatTransmitSend), a);
    }
    else
    {
        PDP8390CORE     pCore = &pThis->core;
        union {
            uint8_t     nRSR;
            DP_RSR      nRsr;
        };
        unsigned        ofs;
        uint32_t        fcs = UINT32_MAX;

        nRSR = 0;

        /* Loopback on the DP8390 is so strange that it must be handled specially. */
        Assert(pSgBuf->pvAllocator == (void *)pThis);
        pThis->Led.Asserted.s.fReading = pThis->Led.Actual.s.fReading = 1;

        LogFlowFunc(("#%d: loopback (DCR=%02X LB=%u TCR=%02X RCR=%02X, %u bytes)\n", pThis->iInstance, pCore->DCR, pCore->tcr.LB, pCore->TCR, pCore->RCR, pSgBuf->cbUsed));
        for (ofs = 0; ofs < pSgBuf->cbUsed; ofs += 16)
            Log(("  %04X: %.*Rhxs\n", ofs, ofs + 16 < pSgBuf->cbUsed ? 16 : pSgBuf->cbUsed - ofs, &pThis->abLoopBuf[ofs]));

        /* A packet shorter than 8 bytes is ignored by the receiving side. */
        if (pSgBuf->cbUsed < 8)
            return VINF_SUCCESS;

        /* The loopback mode affects transmit status bits. */
        switch (pCore->tcr.LB)
        {
        case 1: /* Internal loopback within DP8390. */
            pCore->tsr.CDH = 1;
            pCore->tsr.CRS = 1;
            break;
        case 2: /* Loopback through serializer. */
            pCore->tsr.CDH = 1;
            break;
        case 3: /* External loopback. Requires a cable. */
            break;
        default:
            Assert(0);
        }

        /* The CRC Inhibit controls whether transmit or receive path uses the
         * CRC circuitry. If transmit side uses CRC, receive always fails.
         * We always need to calculate the FCS because either the sending or
         * the receiving side uses it.
         */
        uint8_t     *p;
        uint8_t     *pktbuf = pThis->abLoopBuf; /// @todo Point into sgbuf instead?
        uint16_t    pktlen = (uint16_t)pSgBuf->cbUsed;
        uint16_t    fcslen = pktlen;
        uint8_t     abFcs[4];
        bool        fAddrMatched = true;

        /* If the receiver side is calculating FCS, it needs to skip the last
         * bytes (which are the transmit-side FCS).
         */
        if (pCore->tcr.CRC && (pktlen > 4))
            fcslen -= 4;

        p = pktbuf;
        while (p != &pktbuf[fcslen])
            CRC(fcs, *p++);

        fcs = ~fcs;
        Log3Func(("FCS: %08X\n", fcs));
        for (ofs = 0; ofs < sizeof(abFcs); ++ofs)
        {
            abFcs[ofs] = (uint8_t)fcs;
            fcs >>= 8;
        }

        /* The FIFO write pointer gets zeroed on each receive,
         * but the read pointer does not.
         */
        pCore->fifo.wp = 0;

        if (pCore->tcr.CRC)
        {
            bool    fGoodFcs = true;
            int     is_padr = 0, is_bcast = 0, is_mcast = 0, is_prom = 0;
            int     mc_type = 0;

            /* Always put the first 8 bytes of the packet in the FIFO. */
            for (ofs = 0; ofs < 8; ++ofs)
                pCore->fifo.fifo[pCore->fifo.wp++ & 7] = pktbuf[ofs];


            /* If the receiving side uses the CRC circuitry, it also performs
             * destination address matching.
             */
            if (   (is_padr  = padr_match(pThis, pktbuf))
                || (is_bcast = padr_bcast(pThis, pktbuf))
                || (is_mcast = padr_mcast(pThis, pktbuf, &mc_type))
                || (is_prom  = padr_promi(pThis, pktbuf)))
            {
                /* Receiving side checks the FCS. */
                fGoodFcs = !memcmp(&pktbuf[pktlen - 4], abFcs, sizeof(abFcs));
                Log2Func(("#%d: Address matched (is_padr=%d, is_bcast=%d, is_mcast=%d, is_prom=%d), checking FCS (fGoodFcs=%RTbool)\n", pThis->iInstance, is_padr, is_bcast, is_mcast, is_prom, fGoodFcs));

                /* Now we have to update the FIFO. Since only 8 bytes are visible
                 * in the FIFO after a receive, we can skip most of it.
                 */
                for ( ; ofs < pktlen; ++ofs)
                    pCore->fifo.fifo[pCore->fifo.wp++ & 7] = pktbuf[ofs];

            }
            else
            {
                nRsr.PRX = 1;   /* Weird but true, for non-matching address only! */
                fAddrMatched = false;
                Log3Func(("#%d: Address NOT matched, ignoring FCS errors.\n", pThis->iInstance));
            }

            /* The PHY bit is set when when an enabled broadcast packet is accepted,
             * but also when an enabled multicast packet arrives regardless of whether
             * it passes the MAR filter or not.
             */
            if (is_bcast || mc_type)
                nRsr.PHY = 1;

            if (!fGoodFcs)
                nRsr.CRC = 1;
        }
        else
        {
            nRsr.CRC = 1;   /* Always report CRC error if receiver isn't checking. */

            /* Now we have to update the FIFO. Since only 8 bytes are visible
             * in the FIFO after a receive, we can skip most of it.
             */
            for (ofs = 0; ofs < pktlen; ++ofs)
                pCore->fifo.fifo[pCore->fifo.wp++ & 7] = pktbuf[ofs];

            /* Stuff the generated FCS in the FIFO. */
            for (ofs = 0; ofs < sizeof(abFcs); ++ofs)
                pCore->fifo.fifo[pCore->fifo.wp++ & 7] = abFcs[ofs];
        }

        /* And now put the packet length in the FIFO. */
        if (fAddrMatched || 1)
        {
            pCore->fifo.fifo[pCore->fifo.wp++ & 7] = RT_LOBYTE(pktlen);
            pCore->fifo.fifo[pCore->fifo.wp++ & 7] = RT_HIBYTE(pktlen);
            pCore->fifo.fifo[pCore->fifo.wp++ & 7] = RT_HIBYTE(pktlen); /* Yes, written twice! */
        }

        Log(("FIFO: rp=%u, wp=%u\n", pCore->fifo.rp & 7, pCore->fifo.wp & 7));
        Log(("  %Rhxs\n", &pCore->fifo.fifo));

        if (nRsr.CRC)
            pCore->isr.RXE = 1;
        pCore->RSR = nRSR;

        pThis->Led.Actual.s.fReading = 0;

        /* Return success so that caller sets TSR.PTX and ISR.PTX. */
        rc = VINF_SUCCESS;
    }
    return rc;
}


/**
 * Reads the entire frame into the scatter gather buffer.
 */
DECLINLINE(void) dp8390CoreXmitRead(PPDMDEVINS pDevIns, const unsigned uLocalAddr, const unsigned cbFrame, PPDMSCATTERGATHER pSgBuf, bool fLoopback)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    unsigned        uOfs = 0;
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    Assert(pSgBuf->cbAvailable >= cbFrame);

    pSgBuf->cbUsed = cbFrame;

    LogFlowFunc(("#%d: uLocalAddr=%04X cbFrame=%d\n", pThis->iInstance, uLocalAddr, cbFrame));
    /* Have to figure out where the address is in local RAM. */
    if (pThis->uDevType == DEV_NE1000)
    {
        /* Only 14 bits of address are decoded. */
        uOfs = uLocalAddr & 0x3fff;
        if (uOfs >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            uOfs -= 0x2000;
        }
        else
        {
            /// @todo What are we supposed to do?!
            LogFunc(("#%d: uOfs=%u, don't know what to do!!\n", pThis->iInstance, uOfs));
        }
    }
    else if (pThis->uDevType == DEV_NE2000)
    {
        /* Only 15 bits of address are decoded. */
        uOfs = uLocalAddr & 0x7fff;
        if (uOfs >= 0x4000)
        {
            /* Local RAM is mapped at 4000h-7FFFh. */
            uOfs -= 0x4000;
        }
        else
        {
            /// @todo What are we supposed to do?!
            LogFunc(("#%d: uOfs=%u, don't know what to do!!\n", pThis->iInstance, uOfs));
        }
    }
    else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
    {
        /* Not much to do, WD was nice enough to put the RAM at the start of DP8390's address space. */
        uOfs = uLocalAddr & DPNIC_MEM_MASK;
    }
    else if (pThis->uDevType == DEV_3C503)
    {
        /* Only 14 bits of address are decoded. */
        uOfs = uLocalAddr & 0x3fff;
        if (uOfs >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            uOfs -= 0x2000;
        }
        else
        {
            /// @todo What are we supposed to do?!
            LogFunc(("#%d: uOfs=%u, don't know what to do!!\n", pThis->iInstance, uOfs));
        }
    }
    else
    {
        Assert(0);
    }

    if (!fLoopback)
    {
        /* Fast path for normal transmit, ignores DCR.WTS. */
        if (uOfs + cbFrame <= sizeof(pThis->abLocalRAM))
            memcpy(pSgBuf->aSegs[0].pvSeg, &pThis->abLocalRAM[uOfs], cbFrame);
        else
            memset(pSgBuf->aSegs[0].pvSeg, 0xEE, cbFrame);
    }
    else
    {
        /* If DCR.WTS is set, only every other byte actually goes through loopback. */
        const uint8_t   *src  = &pThis->abLocalRAM[uOfs];
        uint8_t         *dst  = (uint8_t *)pSgBuf->aSegs[0].pvSeg;
        int             cbDst = cbFrame;
        int             step  = 1 << pThis->core.dcr.WTS;

        /* Depending on DCR.BOS, take either odd or even bytes when DCR.WTS is set. */
        if (pThis->core.dcr.WTS && !pThis->core.dcr.BOS)
            ++src;

        while (cbDst-- && (src <= &pThis->abLocalRAM[DPNIC_MEM_SIZE]))
        {
            *dst++ = *src;
            src   += step;
        }

        /* The address should perhaps wrap around -- depends on card design. */
        if (cbDst != -1)
        {
            while (cbDst--)
                *dst++ = 0xEE;
        }
        Assert(cbDst == -1);
    }
}

/**
 * Try to transmit a frame.
 */
static void dp8390CoreStartTransmit(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    /*
     * Transmit the packet if possible, defer it if we cannot do it
     * in the current context.
     */
    pThis->core.TSR = 0;    /* Clear transmit status. */
    pThis->core.NCR = 0;    /* Clear collision counter. */
#if defined(IN_RING0) || defined(IN_RC)
    PDPNICSTATECC   pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
    if (!pThisCC->pDrv)
    {
        int rc = PDMDevHlpTaskTrigger(pDevIns, pThis->hXmitTask);
        AssertRC(rc);
    }
    else
#endif
    {
        int rc = dp8390CoreXmitPacket(pDevIns, pThis, false /*fOnWorkerThread*/);
        if (rc == VERR_TRY_AGAIN)
            rc = VINF_SUCCESS;
        AssertRC(rc);
    }
}


/**
 * If a packet is waiting, poke the receiving machinery.
 *
 * @threads EMT.
 */
static void dp8390CoreKickReceive(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    if (pThis->fMaybeOutOfSpace)
    {
        LogFlow(("Poking receive thread.\n"));
#ifdef IN_RING3
        dp8390R3WakeupReceive(pDevIns);
#else
        int rc = PDMDevHlpTaskTrigger(pDevIns, pThis->hCanRxTask);
        AssertRC(rc);
#endif
    }
}

/**
 * Try transmitting a frame.
 *
 * @threads TX or EMT.
 */
static int dp8390CoreAsyncXmitLocked(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PDPNICSTATECC pThisCC, bool fOnWorkerThread)
{
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));

    /*
     * Just drop it if not transmitting. Can happen with delayed transmits
     * if transmit was disabled in the meantime.
     */
    if (RT_UNLIKELY(!pThis->core.cr.TXP))
    {
        LogFunc(("#%d: Nope, CR.TXP is off (fOnWorkerThread=%RTbool)\n", pThis->iInstance, fOnWorkerThread));
        return VINF_SUCCESS;
    }

    /*
     * Blast out data from the packet buffer.
     */
    int         rc;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatTransmit), a);
    do
    {
        /* Don't send anything when the link is down. */
        if (RT_UNLIKELY(   !dp8390IsLinkUp(pThis)
                        &&  pThis->cLinkDownReported > DPNIC_MAX_LINKDOWN_REPORTED)
            )
            break;

        bool const          fLoopback = pThis->core.tcr.LB != 0;
        PDMSCATTERGATHER    SgLoop;
        PPDMSCATTERGATHER   pSgBuf;

        /*
         * Sending is easy peasy, there is by definition always
         * a complete packet on hand.
         */
        unsigned    cb  = pThis->core.TBCR; /* Packet size. */
        const int   adr = RT_MAKE_U16(0, pThis->core.TPSR);
        LogFunc(("#%d: cb=%d, adr=%04X\n", pThis->iInstance, cb, adr));

        if (RT_LIKELY(dp8390IsLinkUp(pThis) || fLoopback))
        {
            if (RT_LIKELY(cb <= MAX_FRAME))
            {
                /* Loopback fun! */
                if (RT_UNLIKELY(fLoopback && pThis->core.dcr.WTS))
                {
                    cb /= 2;
                    Log(("Loopback with DCR.WTS set -> cb=%d\n", cb));
                }

                rc = dp8390XmitAllocBuf(pThis, pThisCC, cb, fLoopback, &SgLoop, &pSgBuf);
                if (RT_SUCCESS(rc))
                {
                    dp8390CoreXmitRead(pDevIns, adr, cb, pSgBuf, fLoopback);
                    rc = dp8390CoreXmitSendBuf(pDevIns, pThisCC, fLoopback, pSgBuf, fOnWorkerThread);
                    Log2Func(("#%d: rc=%Rrc\n", pThis->iInstance, rc));
                }
                else if (rc == VERR_TRY_AGAIN)
                {
                    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
                    LogFunc(("#%d: rc=%Rrc\n", pThis->iInstance, rc));
                    return VINF_SUCCESS;
                }
                if (RT_SUCCESS(rc))
                {
                    pThis->core.tsr.PTX = 1;
                    pThis->core.isr.PTX = 1;
                }
                else
                {
                    pThis->core.tsr.COL = 1;    /* Pretend there was a collision. */
                    pThis->core.isr.TXE = 1;
                }
            }
            else
            {
                /* Signal error, as this violates the Ethernet specs. Note that the DP8390
                 * hardware does *not* limit the packet length.
                 */
                LogRel(("DPNIC#%d: Attempt to transmit illegal giant frame (%u bytes) -> signaling error\n", pThis->iInstance, cb));
                pThis->core.tsr.OWC = 1;    /* Pretend there was an out-of-window collision. */
                pThis->core.isr.TXE = 1;
            }
        }
        else
        {
            /* Signal a transmit error pretending there was a collision. */
            pThis->core.tsr.COL = 1;
            pThis->core.isr.TXE = 1;
            pThis->cLinkDownReported++;
        }
        /* Transmit officially done, update register state. */
        pThis->core.cr.TXP = 0;
        pThis->core.TBCR   = 0;
        LogFlowFunc(("#%d: TSR=%02X, ISR=%02X\n", pThis->iInstance, pThis->core.TSR, pThis->core.ISR));

    } while (0);    /* No loop, because there isn't ever more than one packet to transmit. */

    dp8390CoreUpdateIrq(pDevIns, pThis);

    /* If there's anything waiting, this should be a good time to recheck. */
    dp8390CoreKickReceive(pDevIns, pThis);

    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);

    return VINF_SUCCESS;
}

/* -=-=-=-=-=- I/O Port access -=-=-=-=-=- */


static uint32_t dp8390CoreRead(PPDMDEVINS pDevIns, PDPNICSTATE pThis, int ofs)
{
    uint8_t     val;

    /* The 3C503 can read the PROM instead of the DP8390 registers. */
    if (pThis->ga.gacr.ealo)
        return pThis->aPROM[ofs % 0xf];
    else if (pThis->ga.gacr.eahi)
        return pThis->aPROM[16 + (ofs % 0xf)];

    /* Command Register exists in all pages. */
    if (ofs == DPR_CR)
        return pThis->core.CR;

    if (pThis->core.cr.PS == 0)
    {
        switch (ofs)
        {
        case DPR_P0_R_CLDA0:
            return pThis->core.clda.CLDA0;
        case DPR_P0_R_CLDA1:
            return pThis->core.clda.CLDA1;
        case DPR_P0_BNRY:
            return pThis->core.BNRY;
        case DPR_P0_R_TSR:
            return pThis->core.TSR;
        case DPR_P0_R_NCR:
            return pThis->core.NCR;
        case DPR_P0_R_FIFO:
            return pThis->core.fifo.fifo[pThis->core.fifo.rp++ & 7];    /// @todo Abstract the mask somehow?
        case DPR_P0_ISR:
            return pThis->core.ISR;
        case DPR_P0_R_CRDA0:
            return pThis->core.crda.CRDA0;
        case DPR_P0_R_CRDA1:
            return pThis->core.crda.CRDA1;
        case DPR_P0_R_RSR:
            return pThis->core.RSR;
        case DPR_P0_R_CNTR0:
            val = pThis->core.CNTR0;
            pThis->core.CNTR0 = 0;  /* Cleared by reading. */
            dp8390CoreUpdateIrq(pDevIns, pThis);
            return val;
        case DPR_P0_R_CNTR1:
            val = pThis->core.CNTR1;
            pThis->core.CNTR1 = 0;  /* Cleared by reading. */
            dp8390CoreUpdateIrq(pDevIns, pThis);
            return val;
        case DPR_P0_R_CNTR2:
            val = pThis->core.CNTR2;
            pThis->core.CNTR2 = 0;  /* Cleared by reading. */
            dp8390CoreUpdateIrq(pDevIns, pThis);
            return val;
        default:
            return 0;   /// @todo or 0xFF? or something else?
        }
    }
    else if (pThis->core.cr.PS == 1)
    {
        /* Page 1 is easy, most registers are stored directly. */
        if (ofs == DPR_P1_CURR)
            return pThis->core.CURR;
        else
            return pThis->core.PG1[ofs];
    }
    else if (pThis->core.cr.PS == 2)
    {
        /* Page 2 is for diagnostics. Reads many registers that
         * are write-only in Page 0.
         */
        switch (ofs)
        {
        case DPR_P2_R_PSTART:
            return pThis->core.PSTART;
        case DPR_P2_R_PSTOP:
            return pThis->core.PSTOP;
        case DPR_P2_RNXTPP:
            return pThis->core.rnxtpp;
        case DPR_P2_R_TPSR:
            return pThis->core.TPSR;
        case DPR_P2_LNXTPP:
            return pThis->core.lnxtpp;
        case DPR_P2_ADRCU:
        case DPR_P2_ADRCL:
            return 0;   /// @todo What's this?
        case DPR_P2_R_RCR:
            return pThis->core.RCR;
        case DPR_P2_R_TCR:
            return pThis->core.TCR;
        case DPR_P2_R_DCR:
            return pThis->core.DCR;
        case DPR_P2_R_IMR:
            return pThis->core.IMR;
        default:
            return 0;   /// @todo Or 0xFF? Or something else?
        }
    }
    else
    {
        /* Page 3 is undocumented and unimplemented. */
        LogFunc(("Reading page 3 register: ofs=%X!\n", ofs));
        return 0;
    }
}


static int dp8390CoreWriteCR(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t val)
{
    union {
        uint8_t     nCR;
        DP_CR       nCr;
    };

    nCR = val;
    LogFlow(("val=%02X, old=%02X\n", val, pThis->core.CR));
    if (nCr.STP != pThis->core.cr.STP)
    {
        if (nCr.STP)
        {
            /* Stop the engine -- software reset. */
            pThis->core.cr.STP  = 1;
            pThis->core.isr.RST = 1;
        }
        else
        {
            /* Clear the stop condition. */
            pThis->core.cr.STP  = 0;

            /* And possibly start up right away. */
            if (nCr.STA)
                pThis->core.cr.STA  = 1;

            /* The STA bit may have been set all along. */
            if (pThis->core.cr.STA)
                pThis->core.isr.RST = 0;
        }

        /* Unblock receive thread if necessary, possibly drop any packets. */
        dp8390CoreKickReceive(pDevIns, pThis);
    }
    if (nCr.STA && !pThis->core.cr.STA)
    {
        /* Start the engine. It is not clearly documented but the STA bit is
         * sticky, and once it's set only a hard reset can clear it. Setting the
         * STP bit doesn't clear it.
         */
        pThis->core.cr.STA  = 1;
        pThis->core.isr.RST = 0;

        /* Unblock receive thread. */
        dp8390CoreKickReceive(pDevIns, pThis);
    }
    if (nCr.TXP && !pThis->core.cr.TXP)
    {
        /* Kick off a transmit. */
        pThis->core.cr.TXP = 1;     /* Indicate transmit in progress. */
        dp8390CoreStartTransmit(pDevIns, pThis);
    }

    /* It is not possible to write a zero (invalid value) to the RD bits. */
    if (nCr.RD == DP_CR_RDMA_INVL)
        nCr.RD = DP_CR_RDMA_ABRT;

    if (nCr.RD != pThis->core.cr.RD)
    {
        /* Remote DMA state change. */
        if (nCr.RD & DP_CR_RDMA_ABRT)
        {
            /* Abort. */
            LogFunc(("RDMA Abort! RD=%d RSAR=%04X RBCR=%04X CRDA=%04X\n", nCr.RD, pThis->core.RSAR, pThis->core.RBCR, pThis->core.CRDA));
        }
        else if (nCr.RD == DP_CR_RDMA_SP)
        {
            DP_PKT_HDR  header;

            /* Read a packet header from memory at BNRY. */
            dpLocalRAMReadBuf(pThis, pThis->core.BNRY, sizeof(header), (uint8_t*)&header);

            pThis->core.CRDA = RT_MAKE_U16(0, pThis->core.BNRY);
            pThis->core.RBCR = header.byte_cnt;

            LogFunc(("RDMA SP: RD=%d RSAR=%04X RBCR=%04X CRDA=%04X\n", nCr.RD, pThis->core.RSAR, pThis->core.RBCR, pThis->core.CRDA));
        }
        else
        {
            /* Starting remote DMA read or write. */
            LogFunc(("RDMA: RD=%d RSAR=%04X RBCR=%04X\n", nCr.RD, pThis->core.RSAR, pThis->core.RBCR));
        }
        pThis->core.cr.RD = nCr.RD;
        /* NB: The current DMA address (CRDA) is not modified here. */
    }
    /* Set the page select bits. */
    pThis->core.cr.PS = nCr.PS;

    return VINF_SUCCESS;
}

static int dp8390CoreWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, int ofs, uint32_t val)
{
    int     rc = VINF_SUCCESS;
    bool    fUpdateIRQ = false;

    Log2Func(("#%d: page=%d reg=%X val=%02X\n", pThis->iInstance, pThis->core.cr.PS, ofs, val));

    /* Command Register exists in all pages. */
    if (ofs == DPR_CR)
    {
        rc = dp8390CoreWriteCR(pDevIns, pThis, val);
    }
    else if (pThis->core.cr.PS == 0)
    {
        switch (ofs)
        {
        case DPR_P0_W_PSTART:
            pThis->core.PSTART = val;
            pThis->core.CURR   = val;
            break;
        case DPR_P0_W_PSTOP:
            pThis->core.PSTOP = val;
            break;
        case DPR_P0_BNRY:
            if (pThis->core.BNRY != val)
            {
                pThis->core.BNRY = val;
                /* Probably made more room in receive ring. */
                dp8390CoreKickReceive(pDevIns, pThis);
            }
            break;
        case DPR_P0_W_TPSR:
            pThis->core.TPSR = val;
            break;
        case DPR_P0_W_TBCR0:
            pThis->core.tbcr.TBCR0 = val;
            break;
        case DPR_P0_W_TBCR1:
            pThis->core.tbcr.TBCR1 = val;
            break;
        case DPR_P0_ISR:
            /* Bits are cleared by writing 1 to them, except for bit 7 (RST). */
            pThis->core.ISR &= ~val | RT_BIT(7);
            fUpdateIRQ = true;
            break;
        case DPR_P0_W_RSAR0:
            /* NE2000 ODI driver v2.12 detects card presence by writing RSAR0
             * and checking if CRDA0 changes to the same value.
             */
            pThis->core.rsar.RSAR0 = val;
            pThis->core.crda.CRDA0 = val;
            break;
        case DPR_P0_W_RSAR1:
            pThis->core.rsar.RSAR1 = val;
            pThis->core.crda.CRDA1 = val;
            break;
        case DPR_P0_W_RBCR0:
            pThis->core.rbcr.RBCR0 = val;
            break;
        case DPR_P0_W_RBCR1:
            pThis->core.rbcr.RBCR1 = val;
            break;
        case DPR_P0_W_RCR:
            pThis->core.RCR     = val;
            pThis->core.rsr.DIS = pThis->core.rcr.MON;
            break;
        case DPR_P0_W_TCR:
            pThis->core.TCR = val;
            break;
        case DPR_P0_W_DCR:
            pThis->core.DCR = val;
            break;
        case DPR_P0_W_IMR:
            pThis->core.IMR = val & 0x7f;   /* Don't let the high bit get set. */
            fUpdateIRQ = true;
            break;
        default:
            Assert(0);
            break;
        }
    }
    else if (pThis->core.cr.PS == 1)
    {
        /* Page 1 is easy, most registers are stored directly. */
        if (ofs == DPR_P1_CURR)
        {
            pThis->core.CURR = val;
        }
        else
            pThis->core.PG1[ofs] = val;
    }
    else if (pThis->core.cr.PS == 2)
    {
        switch (ofs)
        {
        case DPR_P2_W_CLDA0:
            pThis->core.clda.CLDA0 = val;
            break;
        case DPR_P2_W_CLDA1:
            pThis->core.clda.CLDA1 = val;
            break;
        case DPR_P2_RNXTPP:
            pThis->core.rnxtpp = val;
            break;
        case DPR_P2_LNXTPP:
            pThis->core.lnxtpp = val;
            break;
        case DPR_P2_ADRCU:
        case DPR_P2_ADRCL:
            /// @todo What are these?
            break;
        default:
            LogFunc(("Writing unimplemented register: Page 2, offset=%d, val=%02X!\n", ofs, val));
            break;
        }
    }
    else
    {
        /* Page 3 is undocumented and unimplemented. */
        LogFunc(("Writing page 3 register: offset=%d, val=%02X!\n", ofs, val));
    }

    if (fUpdateIRQ)
        dp8390CoreUpdateIrq(pDevIns, pThis);

    return rc;
}


static void neLocalRAMWrite8(PDPNICSTATE pThis, uint16_t addr, uint8_t val)
{
    if (pThis->uDevType == DEV_NE1000)
    {
        /* Only 14 bits of address are decoded. */
        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            pThis->abLocalRAM[addr] = val;
        }
    }
    else if (pThis->uDevType == DEV_NE2000)
    {
        /* Only 15 bits of address are decoded. */
        addr &= 0x7fff;
        if (addr >= 0x4000)
        {
            /* Local RAM is mapped at 4000h-7FFFh. */
            addr -= 0x4000;
            pThis->abLocalRAM[addr] = val;
        }
    }
    else
    {
        Assert(0);
    }
}


static void neLocalRAMWrite16(PDPNICSTATE pThis, uint16_t addr, uint16_t val)
{
    if (pThis->uDevType == DEV_NE2000)
    {
        /* Only 14 bits of address are decoded, word aligned. */
        addr &= 0x7ffe;
        if (addr >= 0x4000)
        {
            /* Local RAM is mapped at 4000h-7FFFh. */
            addr -= 0x4000;
            pThis->abLocalRAM[addr+0] = RT_LOBYTE(val);
            pThis->abLocalRAM[addr+1] = RT_HIBYTE(val);
        }
    }
    else
    {
        Assert(0);
    }
}


static uint8_t neLocalRAMRead8(PDPNICSTATE pThis, uint16_t addr)
{
    uint8_t     val = 0xff;

    if (pThis->uDevType == DEV_NE1000)
    {
        /* Only 14 bits of address are decoded. */
        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            val = pThis->abLocalRAM[addr];
        }
        else
        {
            /* The PROM is mapped below 2000h, effectively only 4 bits decoded.
             * NE1000 emulation uses top 16 bytes of the PROM.
             */
            val = pThis->aPROM[(addr & 0x0f) + 16]; /// @todo Use a constant
        }
    }
    else if (pThis->uDevType == DEV_NE2000)
    {
        /* Only 15 bits of address are decoded. */
        addr &= 0x7fff;
        if (addr >= 0x4000)
        {
            /* Local RAM is mapped at 4000h-7FFFh. */
            addr -= 0x4000;
            val = pThis->abLocalRAM[addr];
        }
        else
        {
            /* The PROM is mapped below 4000h, effectively only 4 bits decoded.
             * Address bits 1:4 from the bus are connected to address pins 0:3
             * on the PROM.
             */
            val = pThis->aPROM[(addr & 0x1f) >> 1]; /// @todo use a constant
        }
    }
    else
    {
        Assert(0);
    }
    return val;
}


static uint16_t neLocalRAMRead16(PDPNICSTATE pThis, uint16_t addr)
{
    uint16_t    val = 0xffff;

    if (pThis->uDevType == DEV_NE2000)
    {
        /* Only 14 bits of address are decoded, word aligned. */
        addr &= 0x7ffe;
        if (addr >= 0x4000)
        {
            /* Local RAM is mapped at 4000h-7FFFh. */
            addr -= 0x4000;
            val = RT_MAKE_U16(pThis->abLocalRAM[addr], pThis->abLocalRAM[addr+1]);
        }
        else
        {
            uint8_t     uPromByte;

            /* The PROM is mapped below 4000h, effectively only 4 bits decoded.
             * Address bits 1:4 from the bus are connected to address pins 0:3
             * on the PROM.
             */
            uPromByte = pThis->aPROM[(addr & 0x1f) >> 1];
            val = RT_MAKE_U16(uPromByte, uPromByte);
        }
    }
    else
    {
        Assert(0);
    }
    return val;
}


static int neDataPortWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint16_t val)
{
    /* Remote Write; ignored if Remote DMA command is not 'Write'. */
    if (pThis->core.cr.RD == DP_CR_RDMA_WR)
    {
        /// @todo Also do nothing if DCR.LAS set?
        if (pThis->core.dcr.WTS)
        {
            Log3Func(("RDMA16 write %04X to local addr %04X\n", val, pThis->core.CRDA));
            neLocalRAMWrite16(pThis, pThis->core.CRDA, val);
        }
        else
        {
            Log3Func(("RDMA8 write %02X to local addr %04X\n", val, pThis->core.CRDA));
            neLocalRAMWrite8(pThis, pThis->core.CRDA, val);
        }
        pThis->core.CRDA += 1 << pThis->core.dcr.WTS;
        if ((pThis->core.crda.CRDA1 == pThis->core.PSTOP) && (pThis->core.PSTOP != pThis->core.PSTART))
        {
            LogFunc(("RDMA wrap / write!! (CRDA=%04X PSTOP=%02X00 PSTART=%02X00)\n", pThis->core.CRDA, pThis->core.PSTOP, pThis->core.PSTART));
            Assert(!pThis->core.crda.CRDA0);    /// @todo Can misalignment actually happen?
            pThis->core.crda.CRDA1 = pThis->core.PSTART;
        }
        pThis->core.RBCR -= 1;

        /* Carefully decrement if WTS set so we don't overshoot and miss EOP. */
        if (pThis->core.dcr.WTS && pThis->core.RBCR)
            pThis->core.RBCR -= 1;

        if (!pThis->core.RBCR)
        {
            LogFunc(("RDMA EOP / write\n"));
            pThis->core.isr.RDC = 1;
            pThis->core.cr.RD   = 0;
            dp8390CoreUpdateIrq(pDevIns, pThis);
        }
    }
    return VINF_SUCCESS;
}


static uint16_t neDataPortRead(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    uint16_t    val = 0x1234;

    /* Remote Read; ignored if Remote DMA command is not 'Read'. */
    if (pThis->core.cr.RD == DP_CR_RDMA_RD)
    {
        /// @todo Also do nothing if DCR.LAS set?
        if (pThis->core.dcr.WTS)
        {
            val = neLocalRAMRead16(pThis, pThis->core.CRDA);
            Log3Func(("RDMA16 read from local addr %04X: %04X\n", pThis->core.CRDA, val));
        }
        else
        {
            val = neLocalRAMRead8(pThis, pThis->core.CRDA);
            Log3Func(("RDMA8 read from local addr %04X: %02X\n", pThis->core.CRDA, val));
        }
        pThis->core.CRDA += 1 << pThis->core.dcr.WTS;
        /// @todo explain that PSTOP=PSTART check is only to reduce logging/busywork
        if ((pThis->core.crda.CRDA1 == pThis->core.PSTOP) && (pThis->core.PSTOP != pThis->core.PSTART))
        {
            Log3Func(("RDMA wrap / read (CRDA=%04X PSTOP=%02X00 PSTART=%02X00)\n", pThis->core.CRDA, pThis->core.PSTOP, pThis->core.PSTART));
            Assert(!pThis->core.crda.CRDA0);    /// @todo can misalignment happen?
            pThis->core.crda.CRDA1 = pThis->core.PSTART;
        }
        pThis->core.RBCR -= 1;

        /* Carefully decrement if WTS set so we don't overshoot and miss EOP. */
        if (pThis->core.dcr.WTS && pThis->core.RBCR)
            pThis->core.RBCR -= 1;

        if (!pThis->core.RBCR)
        {
            LogFunc(("RDMA EOP / read\n"));
            pThis->core.isr.RDC = 1;
            pThis->core.cr.RD   = 0;
            dp8390CoreUpdateIrq(pDevIns, pThis);
        }
    }
    return val;
}


static int neResetPortWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    LogFlowFunc(("\n"));
    dp8390CoreReset(pDevIns, pThis);
    return VINF_SUCCESS;
}


static int dpNeIoWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr, uint32_t val)
{
    int     reg = addr & 0x0f;
    int     rc = VINF_SUCCESS;

    Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));

    /* The NE2000 has 8 bytes of data port followed by 8 bytes of reset port.
     * In contrast, the NE1000 has 4 bytes of data port followed by 4 bytes
     * of reset port, aliased twice within the 16-byte range.
     */
    if (pThis->uDevType == DEV_NE2000)
        reg >>= 1;
    if (reg & 0x04)
        rc = neResetPortWrite(pDevIns, pThis);
    else
        rc = neDataPortWrite(pDevIns, pThis, val);

    return rc;
}


static uint32_t neIoRead(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr)
{
    uint32_t    val = UINT32_MAX;
    int         reg = addr & 0x0f;

    /* The NE2000 has 8 bytes of data port followed by 8 bytes of reset port.
     * In contrast, the NE1000 has 4 bytes of data port followed by 4 bytes
     * of reset port, aliased twice within the 16-byte range.
     */
    if (pThis->uDevType == DEV_NE2000)
        reg >>= 1;
    if (reg & 0x04)
        val = 0x52; /// @todo Check what really happens
    else
        val = neDataPortRead(pDevIns, pThis);

    Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
    return val;
}


static int wdIoWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr, uint32_t val)
{
    int             reg = addr & 0xf;
    int             rc = VINF_SUCCESS;
    union {
        uint8_t     nCTRL1;
        WD_CTRL1    nCtrl1;
    };
    union {
        uint8_t     nCTRL2;
        WD_CTRL2    nCtrl2;
    };

    Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));

    switch (reg)
    {
    case WDR_CTRL1:
        nCTRL1 = val;
        if (nCtrl1.MEME != pThis->ctrl1.MEME)
        {
            LogFunc(("CTRL1.MEME=%u\n", nCtrl1.MEME));
            pThis->ctrl1.MEME = nCtrl1.MEME;
        }
        if (nCtrl1.RESET)
        {
            dp8390CoreReset(pDevIns, pThis);
            pThis->CTRL1 = 0;
        }
        break;
    case WDR_CTRL2:
        /* NYI. */
        nCTRL2 = val;
        if (nCTRL2 != pThis->CTRL2)
        {
            LogFunc(("CTRL2=%02X, new=%02X\n", pThis->CTRL2, nCTRL2));
            pThis->CTRL2 = nCTRL2;
        }
        break;
    default:
        /* Most of the WD registers are read-only. */
        break;
    }

    return rc;
}


static uint32_t wdIoRead(PDPNICSTATE pThis, uint32_t addr)
{
    uint32_t    val = UINT32_MAX;
    int         reg = addr & 0x0f;

    if (reg >= WDR_PROM)
    {
        val = pThis->aPROM[reg & 7];
    }
    else
    {
        if (pThis->uDevType == DEV_WD8013)
        {
            switch (reg)
            {
            case WDR_CTRL1:
                val = pThis->CTRL1;
                break;
            case WDR_ATDET:
                val = pThis->uDevType == DEV_WD8013 ? 1 : 0;
                break;
            case WDR_IOBASE:
                val = pThis->aPROM[WDR_IOBASE]; //val = pThis->IOPortBase >> 5;
                break;
            case WDR_CTRL2:
                val = pThis->CTRL2;
                break;
            case WDR_JP:
                val = 0xa0;
                break;
            default:
                val = 0x00; /// @todo What should it be really?
                break;
            }
        }
        else
        {
            /* Old WD adapters (including 8003E) aliased the PROM for
             * unimplemented control register reads.
             */
            switch (reg)
            {
            case WDR_CTRL2:
                val = 1; //pThis->CTRL2;
                break;
            case WDR_JP:
                val = 0xa0;
                break;
            default:
                val = pThis->aPROM[reg & 7];
                break;
            }
        }

    }

    Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
    return val;
}


static uint8_t elGetIrqFromIdcfr(uint8_t val)
{
    union {
        uint8_t     IDCFR;
        EL_IDCFR    idcfr;
    };
    uint8_t         irq = 0;

    IDCFR = val;

    /* Lowest set IRQ bit wins (might not match hardware).
     * NB: It is valid to not enable any IRQ line!
     */
    if (idcfr.irq2)
        irq = 2;
    else if (idcfr.irq3)
        irq = 3;
    else if (idcfr.irq4)
        irq = 4;
    else if (idcfr.irq5)
        irq = 5;

    return irq;
}

static uint8_t elGetDrqFromIdcfr(uint8_t val)
{
    union {
        uint8_t     IDCFR;
        EL_IDCFR    idcfr;
    };
    uint8_t         drq = 0;

    IDCFR = val;

    /* Lowest set DRQ bit wins; it is valid to not set any. */
    if (idcfr.drq1)
        drq = 1;
    else if (idcfr.drq2)
        drq = 2;
    else if (idcfr.drq3)
        drq = 3;

    return drq;
}

static void elWriteIdcfr(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PEL_GA pGa, uint8_t val)
{
    uint8_t     uOldIrq = pThis->uIsaIrq;
    uint8_t     uNewIrq;
    uint8_t     uOldDrq = pThis->uElIsaDma;
    uint8_t     uNewDrq;

    /* If the IRQ is currently active, have to switch it. */
    uNewIrq = elGetIrqFromIdcfr(val);
    if (uOldIrq != uNewIrq)
    {
        LogFunc(("#%d Switching IRQ=%d -> IRQ=%d\n", pThis->iInstance, uOldIrq, uNewIrq));
        if (pThis->fNicIrqActive)
        {
            /* This probably isn't supposed to happen. */
            LogFunc(("#%d Moving active IRQ!\n", pThis->iInstance));
            if (uOldIrq)
                PDMDevHlpISASetIrq(pDevIns, uOldIrq, 0);
            if (uNewIrq)
                PDMDevHlpISASetIrq(pDevIns, uNewIrq, 1);
        }
        pThis->uIsaIrq = uNewIrq;
    }

    /* And now the same dance for DMA. */
    uNewDrq = elGetDrqFromIdcfr(val);
    if (uOldDrq != uNewDrq)
    {
        /// @todo We can't really move the DRQ, what can we do?
        LogFunc(("#%d Switching DRQ=%d -> DRQ=%d\n", pThis->iInstance, uOldDrq, uNewDrq));
        pThis->uElIsaDma = uNewDrq;
    }

    pGa->IDCFR = val;
}


static void elWriteGacfr(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PEL_GA pGa, uint8_t val)
{
    union {
        uint8_t     nGACFR;
        GA_GACFR    nGacfr;
    };

    nGACFR = val;

    if (nGacfr.nim != pGa->gacfr.nim)
    {
        /// @todo Should we just run UpdateInterrupts?
        if (pThis->fNicIrqActive && !nGacfr.nim)
        {
            LogFunc(("#%d: Unmasking active IRQ!\n", pThis->iInstance));
            PDMDevHlpISASetIrq(pDevIns, pThis->uIsaIrq, 1);
        }
        else if (pThis->fNicIrqActive && nGacfr.nim)
        {
            LogFunc(("#%d: Masking active IRQ\n", pThis->iInstance));
            PDMDevHlpISASetIrq(pDevIns, pThis->uIsaIrq, 0);
        }
    }

    /// @todo rsel/mbs bit change?
    if (nGacfr.rsel != pGa->gacfr.rsel)
    {
        LogFunc(("#%d: rsel=%u mbs=%u\n", pThis->iInstance, nGacfr.rsel, nGacfr.mbs));
    }

    pGa->GACFR = nGACFR;
}


static void elSoftReset(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    PEL_GA      pGa = &pThis->ga;

    LogFlow(("Resetting ASIC GA\n"));
    /* Most GA registers are zeroed. */
    pGa->PSTR = pGa->PSPR = 0;
    pGa->DQTR = 0;
    elWriteGacfr(pDevIns, pThis, pGa, 0);
    pGa->STREG = ELNKII_GA_REV;
    pGa->VPTR0 = pGa->VPTR1 = pGa->VPTR2 = 0;
    pGa->DALSB = pGa->DAMSB = 0;
    elWriteIdcfr(pDevIns, pThis, pGa, 0);
    pGa->GACR = 0x0B;   /* Low bit set = in reset state. */
    pGa->fGaIrq = false;

    /* Reset the NIC core. */
    dp8390CoreReset(pDevIns, pThis);
}


static int elWriteGacr(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PEL_GA pGa, uint8_t val)
{
    union {
        uint8_t     nGACR;
        GA_GACR     nGacr;
    };

    nGACR = val;

    if (nGacr.rst != pGa->gacr.rst)
    {
        /* When going out of reset, only clear the rst bit. 3C503 diagnostics checks for this. */
        if (nGacr.rst)
            elSoftReset(pDevIns, pThis);
        else
            pGa->gacr.rst = 0;
    }
    else
    {
#ifdef IN_RING0
        /* Force a trip to R3. */
        if (pThis->uElIsaDma == pThis->uIsaDma)
            return VINF_IOM_R3_IOPORT_WRITE;
#endif

        /* Make the data registers "ready" as long as transfers are started. */
        if (nGacr.start)
        {
            pGa->cdadr.cdadr_lsb = pGa->DALSB;
            pGa->cdadr.cdadr_msb = pGa->DAMSB;
            LogFunc(("DMA started, ddir=%u, cdadr=%04X\n", pGa->gacr.ddir, pGa->CDADR));
            pGa->streg.dprdy = 1;
            pGa->streg.dip   = 1;
            pGa->streg.dtc   = 0;
        }
        else
        {
            pGa->streg.dprdy = 0;
            pGa->streg.dip   = 0;
        }

        /* Only do anything if the software configured DMA channel matches the emulation config. */
        if (pThis->uElIsaDma == pThis->uIsaDma)
        {
#ifdef IN_RING3
            PDMDevHlpDMASetDREQ(pDevIns, pThis->uIsaDma, pGa->streg.dprdy);
            if (pGa->streg.dprdy)
                PDMDevHlpDMASchedule(pDevIns);
            LogFunc(("#%d: DREQ for channel %u set to %u\n", pThis->iInstance, pThis->uIsaDma, pGa->streg.dprdy));
#else
            /* Must not get here. */
            Assert(0);
#endif
        }

        pGa->GACR = nGACR;
        LogFunc(("GACR=%02X ealo=%u eahi=%u\n", pGa->GACR, pGa->gacr.ealo, pGa->gacr.eahi));
    }

    return VINF_SUCCESS;
}


static int elGaDataWrite(PDPNICSTATE pThis, PEL_GA pGa, uint16_t val)
{
    /* Data write; ignored if not started and in "download" mode. */
    if (pGa->gacr.start && pGa->gacr.ddir)
    {
        uint16_t    addr = pGa->CDADR;

        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            pThis->abLocalRAM[addr] = val;
        }

        pGa->CDADR++;
        /// @todo Does this really apply to writes or only reads?
        if ((pGa->cdadr.cdadr_msb == pGa->PSPR) && (pGa->PSPR != pGa->PSTR))
        {
            LogFunc(("GA DMA wrap / write!! (cdadr=%04X PSPR=%02X00 PSTR=%02X00)\n", pGa->CDADR, pGa->PSPR, pGa->PSTR));
            pGa->cdadr.cdadr_msb = pGa->PSTR;
        }
    }
    return VINF_SUCCESS;
}


static uint8_t elGaDataRead(PDPNICSTATE pThis, PEL_GA pGa)
{
    uint8_t     val = 0xcd;

    /* Data read; ignored if not started and in "upload" mode. */
    if (pGa->gacr.start && !pGa->gacr.ddir)
    {
        uint16_t    addr = pGa->CDADR;

        addr &= 0x3fff;
        if (addr >= 0x2000)
        {
            /* Local RAM is mapped at 2000h-3FFFh. */
            addr -= 0x2000;
            val = pThis->abLocalRAM[addr];
        }

        pGa->CDADR++;
        if ((pGa->cdadr.cdadr_msb == pGa->PSPR) && (pGa->PSPR != pGa->PSTR))
        {
            LogFunc(("GA DMA wrap / read!! (cdadr=%04X PSPR=%02X00 PSTR=%02X00)\n", pGa->CDADR, pGa->PSPR, pGa->PSTR));
            pGa->cdadr.cdadr_msb = pGa->PSTR;
        }
    }
    return val;
}


static int elGaIoWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr, uint32_t val)
{
    int         reg = addr & 0xf;
    int         rc = VINF_SUCCESS;
    PEL_GA      pGa = &pThis->ga;

    Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));

    switch (reg)
    {
    case GAR_PSTR:
        pGa->PSTR = val;
        break;
    case GAR_PSPR:
        pGa->PSPR = val;
        break;
    case GAR_DQTR:
        pGa->DQTR = val;
        break;
    case GAR_GACFR:
        elWriteGacfr(pDevIns, pThis, pGa, val);
        break;
    case GAR_GACR:
        rc = elWriteGacr(pDevIns, pThis, pGa, val);
        break;
    case GAR_STREG:
        /* Writing anything to STREG clears ASIC interrupt. */
        pThis->ga.streg.dtc = 0;
        pThis->ga.fGaIrq    = false;
        dp8390CoreUpdateIrq(pDevIns, pThis);
        break;
    case GAR_IDCFR:
        elWriteIdcfr(pDevIns, pThis, pGa, val);
        break;
    case GAR_DAMSB:
        pGa->DAMSB = val;
        break;
    case GAR_DALSB:
        pGa->DALSB = val;
        break;
    case GAR_VPTR2:
        pGa->VPTR2 = val;
        break;
    case GAR_VPTR1:
        pGa->VPTR1 = val;
        break;
    case GAR_VPTR0:
        pGa->VPTR0 = val;
        break;
    case GAR_RFMSB:
    case GAR_RFLSB:
        elGaDataWrite(pThis, pGa, val);
        break;
    case GAR_R_BCFR:
    case GAR_R_PCFR:
        /* Read-only registers, ignored. */
        break;
    default:
        Assert(0);
        break;
    }

    return rc;
}


static uint32_t elGaIoRead(PDPNICSTATE pThis, uint32_t addr)
{
    uint32_t    val = UINT32_MAX;
    int         reg = addr & 0x0f;
    PEL_GA      pGa = &pThis->ga;

    switch (reg)
    {
    case GAR_PSTR:
        val = pGa->PSTR;
        break;
    case GAR_PSPR:
        val = pGa->PSPR;
        break;
    case GAR_DQTR:
        val = pGa->DQTR;
        break;
    case GAR_R_BCFR:
        val = pGa->BCFR;
        break;
    case GAR_R_PCFR:
        val = pGa->PCFR;
        break;
    case GAR_GACFR:
        val = pGa->GACFR;
        break;
    case GAR_GACR:
        val = pGa->GACR;
        break;
    case GAR_STREG:
        val = pGa->STREG;
        break;
    case GAR_IDCFR:
        val = pGa->IDCFR;
        break;
    case GAR_DAMSB:
        val = pGa->DAMSB;
        break;
    case GAR_DALSB:
        val = pGa->DALSB;
        break;
    case GAR_VPTR2:
        val = pGa->VPTR2;
        break;
    case GAR_VPTR1:
        val = pGa->VPTR1;
        break;
    case GAR_VPTR0:
        val = pGa->VPTR0;
        break;
    case GAR_RFMSB:
    case GAR_RFLSB:
        val = elGaDataRead(pThis, pGa);
        break;
    default:
        Assert(0);
        break;
    }

    Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
    return val;
}


/**
 * @callback_method_impl{FNIOMIOPORTIN}
 */
static DECLCALLBACK(VBOXSTRICTRC)
neIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    uint8_t         u8Lo, u8Hi = 0;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            *pu32 = neIoRead(pDevIns, pThis, reg);
            break;
        case 2:
            /* Manually split word access if necessary if it's an NE1000. Perhaps overkill. */
            if (pThis->uDevType == DEV_NE1000)
            {
                u8Lo = neIoRead(pDevIns, pThis, reg);
                if (reg < 0xf)  // This logic is not entirely accurate (wraparound).
                    u8Hi = neIoRead(pDevIns, pThis, reg + 1);
                *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
            }
            else
                *pu32 = neIoRead(pDevIns, pThis, reg);
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "neIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: NE Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
    return rc;
}


/**
 * @callback_method_impl{FNIOMIOPORTIN}
 */
static DECLCALLBACK(VBOXSTRICTRC)
wdIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    uint8_t         u8Lo, u8Hi = 0;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            *pu32 = wdIoRead(pThis, reg);
            break;
        case 2:
            /* Manually split word access. */
            u8Lo = wdIoRead(pThis, reg);
            if (reg < 0xf)  // This logic is not entirely accurate (wraparound).
                u8Hi = wdIoRead(pThis, reg + 1);
            *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "wdIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: WD Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
    return rc;
}


/**
 * @callback_method_impl{FNIOMIOPORTIN}
 */
static DECLCALLBACK(VBOXSTRICTRC)
elIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    uint8_t         u8Lo, u8Hi = 0;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            *pu32 = elGaIoRead(pThis, reg);
            break;
        case 2:
            /* Manually split word access. */
            u8Lo = elGaIoRead(pThis, reg);
            if (reg < 0xf)  // This logic is not entirely accurate (wraparound).
                u8Hi = elGaIoRead(pThis, reg + 1);
            *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "elIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: EL Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
    return rc;
}


/**
 * @callback_method_impl{FNIOMIOPORTIN}
 */
static DECLCALLBACK(VBOXSTRICTRC)
dp8390CoreIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    uint8_t         u8Lo, u8Hi;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            *pu32 = dp8390CoreRead(pDevIns, pThis, reg);
            break;
        case 2:
            /* Manually split word access. */
            u8Lo = dp8390CoreRead(pDevIns, pThis, reg + 0);
            /* This logic is not entirely accurate. */
            if (reg < 0xf)
                u8Hi = dp8390CoreRead(pDevIns, pThis, reg + 1);
            else
                u8Hi = 0;
            *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "dp8390CoreIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
    return rc;
}


/**
 * @callback_method_impl{FNIOMIOPORTOUT}
 */
static DECLCALLBACK(VBOXSTRICTRC)
neIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            rc = dpNeIoWrite(pDevIns, pThis, Port, RT_LOBYTE(u32));
            break;
        case 2:
            /* Manually split word access if necessary. */
            if (pThis->uDevType == DEV_NE2000)
            {
                rc = dpNeIoWrite(pDevIns, pThis, Port, RT_LOWORD(u32));
            }
            else
            {
                rc = dpNeIoWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
                if (RT_SUCCESS(rc) && (reg < 0xf))
                    rc = dpNeIoWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
            }
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "neIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: NE Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    return rc;
}


/**
 * @callback_method_impl{FNIOMIOPORTOUT}
 */
static DECLCALLBACK(VBOXSTRICTRC)
wdIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            rc = wdIoWrite(pDevIns, pThis, Port, RT_LOBYTE(u32));
            break;
        case 2:
            /* Manually split word access. */
            rc = wdIoWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
            if (RT_SUCCESS(rc) && (reg < 0xf))
                rc = wdIoWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "wdIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: WD Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    return rc;
}


/**
 * @callback_method_impl{FNIOMIOPORTOUT}
 */
static DECLCALLBACK(VBOXSTRICTRC)
elIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            rc = elGaIoWrite(pDevIns, pThis, Port, RT_LOBYTE(u32));
            break;
        case 2:
            /* Manually split word access. */
            rc = elGaIoWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
            if (RT_SUCCESS(rc) && (reg < 0xf))
                rc = elGaIoWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "elIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: EL Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    return rc;
}


/**
 * @callback_method_impl{FNIOMIOPORTOUT}
 */
static DECLCALLBACK(VBOXSTRICTRC)
dp8390CoreIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc    = VINF_SUCCESS;
    int             reg   = Port & 0xf;
    STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
    RT_NOREF_PV(pvUser);

    switch (cb)
    {
        case 1:
            rc = dp8390CoreWrite(pDevIns, pThis, reg, RT_LOBYTE(u32));
            break;
        case 2:
            /* Manually split word access. */
            rc = dp8390CoreWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
            if (!RT_SUCCESS(rc))
                break;
            rc = dp8390CoreWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
            break;
        default:
            rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
                                   "dp8390CoreIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
                                   Port, cb);
    }

    Log2Func(("#%d: Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
    STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
    return rc;
}


#if 0
/**
 * @callback_method_impl{FNIOMMMIONEWFILL,
 * Local RAM write hook\, to be called from IOM. This is the advanced version of
 * wdMemWrite function.}
 */
static DECLCALLBACK(VBOXSTRICTRC)
dpWdMmioFill(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, uint32_t u32Item, unsigned cbItem, unsigned cItems)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));

    !!
    return VINF_SUCCESS
}
#endif


/**
 * @callback_method_impl{FNIOMMMIONEWREAD,
 * Local RAM read hook\, to be called from IOM.}
 */
static DECLCALLBACK(VBOXSTRICTRC) wdMemRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
{
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    uint8_t         *pbData = (uint8_t *)pv;
    NOREF(pvUser);

//    STAM_PROFILE_START(&pThis->CTX_MID_Z(Stat,MemoryRead), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));

    if (pThis->ctrl1.MEME)
    {
        Log3Func(("#%d: Reading %u bytes from address %X: [%.*Rhxs]\n", pThis->iInstance, cb, off, cb, &pThis->abLocalRAM[off & DPNIC_MEM_MASK]));
        while (cb-- > 0)
            *pbData++ = pThis->abLocalRAM[off++ & DPNIC_MEM_MASK];
    }
    else
        memset(pv, 0xff, cb);

//    STAM_PROFILE_STOP(&pThis->CTX_MID_Z(Stat,MemoryRead), a);
    return VINF_SUCCESS;
}

/**
 * @callback_method_impl{FNIOMMMIONEWWRITE,
 * Local RAM write hook\, to be called from IOM.}
 */
static DECLCALLBACK(VBOXSTRICTRC) wdMemWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
{
    PDPNICSTATE     pThis  = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    uint8_t const   *pbSrc = (uint8_t const *)pv;
    NOREF(pvUser);

//    STAM_PROFILE_START(&pThis->CTX_MID_Z(Stat,MemoryWrite), a);
    Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));

    if (pThis->ctrl1.MEME)
    {
        Log3Func(("#%d: Writing %u bytes to address %X: [%.*Rhxs]\n", pThis->iInstance, cb, off, cb, pbSrc));
        while (cb-- > 0)
            pThis->abLocalRAM[off++ & DPNIC_MEM_MASK] = *pbSrc++;
    }

//    STAM_PROFILE_STOP(&pThis->CTX_MID_Z(Stat,MemoryWrite), a);
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNIOMMMIONEWREAD,
 * Local RAM read hook\, to be called from IOM.}
 */
static DECLCALLBACK(VBOXSTRICTRC) elMemRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
{
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    uint8_t         *pbData = (uint8_t *)pv;
    NOREF(pvUser);

    Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));

    if (pThis->ga.gacfr.rsel)
    {
        Log3Func(("#%d: Reading %u bytes from address %X\n", pThis->iInstance, cb, off));
        while (cb-- > 0)
            *pbData++ = pThis->abLocalRAM[off++ & DPNIC_MEM_MASK];
    }
    else
    {
        Log3Func(("#%d: Ignoring read of %u bytes from address %X\n", pThis->iInstance, cb, off));
        memset(pv, 0xff, cb);
    }
    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNIOMMMIONEWWRITE,
 * Local RAM write hook\, to be called from IOM.}
 */
static DECLCALLBACK(VBOXSTRICTRC) elMemWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
{
    PDPNICSTATE     pThis  = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    uint8_t const   *pbSrc = (uint8_t const *)pv;
    NOREF(pvUser);

    Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));

    if (pThis->ga.gacfr.rsel)
    {
        Log3Func(("#%d: Writing %u bytes to address %X\n", pThis->iInstance, cb, off));
        while (cb-- > 0)
            pThis->abLocalRAM[off++ & DPNIC_MEM_MASK] = *pbSrc++;
    }
    else
    {
        Log3Func(("#%d: Ignoring write of %u bytes to address %X\n", pThis->iInstance, cb, off));
    }
    return VINF_SUCCESS;
}


#ifdef IN_RING3

/* Shamelessly stolen from DevDMA.cpp */

/* Test the decrement bit of mode register. */
#define IS_MODE_DEC(c)      ((c) & 0x20)
/* Test the auto-init bit of mode register. */
#define IS_MODE_AI(c)       ((c) & 0x10)
/* Extract the transfer type bits of mode register. */
#define GET_MODE_XTYP(c)    (((c) & 0x0c) >> 2)

/* DMA transfer modes. */
enum {
    DMODE_DEMAND,   /* Demand transfer mode. */
    DMODE_SINGLE,   /* Single transfer mode. */
    DMODE_BLOCK,    /* Block transfer mode. */
    DMODE_CASCADE   /* Cascade mode. */
};

/* DMA transfer types. */
enum {
    DTYPE_VERIFY,   /* Verify transfer type. */
    DTYPE_WRITE,    /* Write transfer type. */
    DTYPE_READ,     /* Read transfer type. */
    DTYPE_ILLEGAL   /* Undefined. */
};

static DECLCALLBACK(uint32_t) elnk3R3DMAXferHandler(PPDMDEVINS pDevIns, void *opaque,
                                                    unsigned nchan, uint32_t dma_pos, uint32_t dma_len)
{
    PDPNICSTATE     pThis = (PDPNICSTATE)opaque;
    int             dma_mode;
    int             dma_type;
    uint16_t        cbToXfer;
    uint32_t        cbXferred = 0;
    uint16_t        uDmaAddr;
    int             rc;

    /*
     * The 3C503 EtherLink II uses DMA as an alternative to shared RAM
     * or PIO. The Gate Array tracks its own current DMA address within
     * the adapter's local address space.
     */
    dma_mode = PDMDevHlpDMAGetChannelMode(pDevIns, pThis->uIsaDma);
    dma_type = GET_MODE_XTYP(dma_mode);
    uDmaAddr = pThis->ga.CDADR;
    cbToXfer = dma_len;
    LogFlowFunc(("dma_mode=%d, dma_type=%d, dma_pos=%u, dma_len=%u, cdadr=%04X\n", dma_mode, dma_type, dma_pos, dma_len, uDmaAddr));

    /* Skip any accesses below local memory start. */
    if ((0x2000 > 0) && (uDmaAddr < 0x2000))  /// @todo Should keep track in variables
    {
        uint16_t    cbToSkip = 0x2000 - uDmaAddr;

        uDmaAddr += cbToSkip;
        /// @todo Should this write junk to host memory when reading from device?
        if (cbToSkip < cbToXfer)
        {
            cbToXfer -= cbToSkip;
            Assert(uDmaAddr == 0x2000);
            LogFunc(("DMA skipping %u bytes!\n", cbToSkip));
        }
        else
        {
            cbToXfer = 0;   /* Transfer entirely below valid address range. */
            LogFunc(("DMA below valid address range!\n"));
        }
    }

    if (cbToXfer)
    {
        uint16_t    cbToSkip = 0;

        /* Clip transfer size so it falls within local RAM. */
        if ((uDmaAddr - 0x2000 + cbToXfer) > (int)sizeof(pThis->abLocalRAM))
        {
            /* Calculate how much to skip anything at the end. */
            cbToSkip = sizeof(pThis->abLocalRAM) - (0x2000 - uDmaAddr + cbToXfer);
            LogFunc(("DMA above valid address range uDmaAddr=%04X cbToXfer=%u cbToSkip=%u!\n", uDmaAddr, cbToXfer, cbToSkip));
            cbToXfer -= cbToSkip;
        }

        if (dma_type == DTYPE_WRITE)
        {
            /* Write transfer type. Reading from device, writing to memory. */
            if (!pThis->ga.gacr.ddir)
            {
                Log2Func(("DMAWriteMemory uDmaAddr=%04X cbToXfer=%u\n", uDmaAddr, cbToXfer));
                rc = PDMDevHlpDMAWriteMemory(pDevIns, nchan,
                                             &pThis->abLocalRAM[uDmaAddr - 0x2000],
                                             dma_pos, cbToXfer, &cbXferred);
                AssertMsgRC(rc, ("DMAWriteMemory -> %Rrc\n", rc));
            }
            else
            {
                // Do nothing, direction does not match.
                /// @todo Bug in DevDMA?
                LogFunc(("DTYPE_WRITE but GACR.ddir set, do nothing!\n"));
            }
        }
        else
        {
            /* Read of Verify transfer type. Reading from memory, writing to device. */
            if (pThis->ga.gacr.ddir)
            {
                Log2Func(("DMAReadMemory uDmaAddr=%04X cbToXfer=%u\n", uDmaAddr, cbToXfer));
                rc = PDMDevHlpDMAReadMemory(pDevIns, nchan,
                                            &pThis->abLocalRAM[uDmaAddr - 0x2000],
                                            dma_pos, cbToXfer, &cbXferred);
                AssertMsgRC(rc, ("DMAReadMemory -> %Rrc\n", rc));
            }
            else
            {
                // Do nothing, direction does not match.
                /// @todo Bug in DevDMA?
                LogFunc(("DTYPE_READ but GACR.ddir clear, do nothing!\n"));
            }
        }

        /* NB: This might wrap. In theory it might wrap back to valid
         * memory but... just no.
         */
        /// @todo Actually... what would really happen?
        uDmaAddr += cbToXfer + cbToSkip;
    }
    Log2Func(("After DMA transfer: uDmaAddr=%04X, cbXferred=%u\n", uDmaAddr, cbXferred));

    /* Advance the DMA address and see if transfer completed (it almost certainly did). */
    if (1)
    {
        Log2Func(("DMA completed\n"));
        PDMDevHlpDMASetDREQ(pDevIns, pThis->uIsaDma, 0);
        pThis->ga.streg.dtc = 1;
        pThis->ga.fGaIrq    = true;
        dp8390CoreUpdateIrq(pDevIns, pThis);
    }
    else
    {
        LogFunc(("DMA continuing: uDmaAddr=%04X, cbXferred=%u\n", uDmaAddr, cbXferred));
        PDMDevHlpDMASchedule(pDevIns);
    }

    /* Returns the updated transfer count. */
    return dma_pos + dma_len;
}


/* -=-=-=-=-=- Timer Callbacks -=-=-=-=-=- */

/**
 * @callback_method_impl{FNTMTIMERDEV, Restore timer callback}
 *
 * This is only called when we restore a saved state and temporarily
 * disconnected the network link to inform the guest that network connections
 * should be considered lost.
 */
static DECLCALLBACK(void) dpNicR3TimerRestore(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
{
    RT_NOREF(pvUser);
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    AssertReleaseRC(rc);

    rc = VERR_GENERAL_FAILURE;

    /* The DP8390 based cards have no concept of link state. Reporting collisions on all transmits
     * is the best approximation of a disconnected cable that we can do. Some drivers (3C503) warn
     * of possible disconnected cable, some don't. Many cards with DP8390 chips had permanently
     * attached cables (AUI or BNC) and their drivers do not expect cables to be disconnected and
     * re-connected at runtime. Guests which are waiting for a receive have no way to notice any
     * problem, therefore we only postpone restoring a link a couple of times, and then reconnect
     * regardless of whether the guest noticed anything or not.
     */
    if (   (pThis->cLinkDownReported <= DPNIC_MAX_LINKDOWN_REPORTED)
        && (pThis->cLinkRestorePostponed <= DPNIC_MAX_LINKRST_POSTPONED))
        rc = PDMDevHlpTimerSetMillies(pDevIns, hTimer, 1500);
    if (RT_FAILURE(rc))
    {
        pThis->fLinkTempDown = false;
        if (pThis->fLinkUp)
        {
            LogRel(("DPNIC#%d: The link is back up again after the restore.\n",
                    pThis->iInstance));
            LogFunc(("#%d: cLinkDownReported=%d\n", pThis->iInstance, pThis->cLinkDownReported));
            pThis->Led.Actual.s.fError = 0;
        }
    }
    else
    {
        LogFunc(("#%d: cLinkDownReported=%d, cLinkRestorePostponed=%d, wait another 1500ms...\n",
                 pThis->iInstance, pThis->cLinkDownReported, pThis->cLinkRestorePostponed));
        pThis->cLinkRestorePostponed++;
    }

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
}


/* -=-=-=-=-=- Debug Info Handler -=-=-=-=-=- */

/**
 * @callback_method_impl{FNDBGFHANDLERDEV}
 */
static DECLCALLBACK(void) dpNicR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    bool            fRecvBuffer  = false;
    bool            fSendBuffer  = false;
    unsigned        uFreePages;
    DP8390CORE      *pCore = &pThis->core;
    const char      *aszModels[] = {"NE1000", "NE2000", "WD8003E", "WD8013E", "3C503"};

    /*
     * Parse args.
     */
    if (pszArgs)
    {
        fRecvBuffer  = strstr(pszArgs, "verbose") || strstr(pszArgs, "recvbuf");
        fSendBuffer  = strstr(pszArgs, "verbose") || strstr(pszArgs, "sendbuf");
    }

    /*
     * Show device information.
     */
    pHlp->pfnPrintf(pHlp, "DPNIC #%d: %s port=%RTiop IRQ=%u",
                    pThis->iInstance,
                    aszModels[pThis->uDevType],
                    pThis->IOPortBase,
                    pThis->uIsaIrq);
    if (pThis->MemBase)
        pHlp->pfnPrintf(pHlp, " mem=%05X-%05X", pThis->MemBase, pThis->MemBase + pThis->cbMemSize - 1);
    if (pThis->uIsaDma)
        pHlp->pfnPrintf(pHlp, " DMA=%u", pThis->uIsaDma);
    pHlp->pfnPrintf(pHlp, " mac-cfg=%RTmac%s %s\n",
                    &pThis->MacConfigured,
                    pDevIns->fR0Enabled ? " RZ" : "",
                    pThis->fDriverAttached ? "attached" : "unattached!");

    int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_INTERNAL_ERROR); /* Take it here so we know why we're hanging... */
    PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);

    pHlp->pfnPrintf(pHlp, "\nDP3890 NIC Core\n");
    pHlp->pfnPrintf(pHlp, "   CR=%02X: %s%s%s RD=%d PS=%d\n", pCore->CR,
                    pCore->cr.STP ? "STP " : "",
                    pCore->cr.STA ? "STA " : "",
                    pCore->cr.TXP ? "TXP " : "",
                    pCore->cr.RD, pCore->cr.PS);
    pHlp->pfnPrintf(pHlp, "  ISR=%02X: %s%s%s%s%s%s%s%s\n", pCore->ISR,
                    pCore->isr.PRX ? "PRX " : "",
                    pCore->isr.PTX ? "PTX " : "",
                    pCore->isr.RXE ? "RXE " : "",
                    pCore->isr.TXE ? "TXE " : "",
                    pCore->isr.OVW ? "OVW " : "",
                    pCore->isr.CNT ? "CNT " : "",
                    pCore->isr.RDC ? "RDC " : "",
                    pCore->isr.RST ? "RST " : "");
    pHlp->pfnPrintf(pHlp, "  IMR=%02X: %s%s%s%s%s%s%s%s\n", pCore->IMR,
                    pCore->imr.PRXE ? "PRXE " : "",
                    pCore->imr.PTXE ? "PTXE " : "",
                    pCore->imr.RXEE ? "RXEE " : "",
                    pCore->imr.TXEE ? "TXEE " : "",
                    pCore->imr.OVWE ? "OVWE " : "",
                    pCore->imr.CNTE ? "CNTE " : "",
                    pCore->imr.RDCE ? "RDCE " : "",
                    pCore->imr.res ? "Reserved bit set!!" : "");
    pHlp->pfnPrintf(pHlp, "  DCR=%02X: %s%s%s%s%sFT=%d %s\n", pCore->DCR,
                    pCore->dcr.WTS ? "WTS " : "",
                    pCore->dcr.BOS ? "BOS " : "",
                    pCore->dcr.LAS ? "LAS " : "",
                    pCore->dcr.LS  ? "LS "  : "",
                    pCore->dcr.ARM ? "ARM " : "",
                    pCore->dcr.FT,
                    pCore->dcr.res ? "Reserved bit set!!" : "");
    pHlp->pfnPrintf(pHlp, "  TCR=%02X: %sLB=%d %s%s\n", pCore->TCR,
                    pCore->tcr.CRC  ? "CRC " : "",
                    pCore->tcr.LB,
                    pCore->tcr.ATD  ? "ATD " : "",
                    pCore->tcr.OFST ? "OFST" : "");
    pHlp->pfnPrintf(pHlp, "  TSR=%02X: %s%s%s%s%s%s%s%s\n", pCore->TSR,
                    pCore->tsr.PTX ? "PTX " : "",
                    pCore->tsr.DFR ? "DFR " : "",
                    pCore->tsr.COL ? "COL " : "",
                    pCore->tsr.ABT ? "ABT " : "",
                    pCore->tsr.CRS ? "CRS " : "",
                    pCore->tsr.FU  ? "FU "  : "",
                    pCore->tsr.CDH ? "CDH " : "",
                    pCore->tsr.OWC ? "OWC " : "");
    pHlp->pfnPrintf(pHlp, "  RCR=%02X: %s%s%s%s%s%s\n", pCore->RCR,
                    pCore->rcr.SEP ? "SEP " : "",
                    pCore->rcr.AR  ? "AR "  : "",
                    pCore->rcr.AB  ? "AB "  : "",
                    pCore->rcr.AM  ? "AM "  : "",
                    pCore->rcr.PRO ? "PRO " : "",
                    pCore->rcr.MON ? "MON " : "");
    pHlp->pfnPrintf(pHlp, "  RSR=%02X: %s%s%s%s%s%s%s%s\n", pCore->RSR,
                    pCore->rsr.PRX ? "PRX " : "",
                    pCore->rsr.CRC ? "CRC " : "",
                    pCore->rsr.FAE ? "FAE " : "",
                    pCore->rsr.FO  ? "FO "  : "",
                    pCore->rsr.MPA ? "MPA " : "",
                    pCore->rsr.PHY ? "PHY " : "",
                    pCore->rsr.DIS ? "DIS " : "",
                    pCore->rsr.DFR ? "DFR " : "");
    pHlp->pfnPrintf(pHlp, "  ActIntSrc: %02X\n", pCore->ISR & pCore->IMR);
    pHlp->pfnPrintf(pHlp, "  Receiving: %s%s%s%s%s%s\n",
                    pCore->rcr.AB  ? "Broadcast " : "",
                    pCore->rcr.AM  ? "Multicast " : "",
                    pCore->rcr.PRO ? "Promiscuous " : "",
                    pCore->rcr.MON ? "Monitor " : "",
                    pCore->cr.STA  ? "Started " : "Not started ",
                    pCore->isr.RST ? "Reset!" : "");

    /* Dump the currently programmed station address. */
    pHlp->pfnPrintf(pHlp, "  MAC Addr : %RTmac\n", &pCore->pg1.PAR);

    /* Dump the currently programmed multicast filter. */
    pHlp->pfnPrintf(pHlp, "  Multicast: %02X:%02X:%02X:%02X %02X:%02X:%02X:%02X\n",
                    pCore->pg1.MAR[0], pCore->pg1.MAR[1], pCore->pg1.MAR[2], pCore->pg1.MAR[3],
                    pCore->pg1.MAR[4], pCore->pg1.MAR[5], pCore->pg1.MAR[6], pCore->pg1.MAR[7]);

    /* Dump the DMA state. */
    pHlp->pfnPrintf(pHlp, "  Local DMA : TPSR=%02X00 TBCR=%04X CLDA=%04X\n",
                    pCore->TPSR, pCore->TBCR, pCore->CLDA);
    pHlp->pfnPrintf(pHlp, "            : PSTART=%02X00 PSTOP=%02X00 CURR=%02X00 BNRY=%02X00\n",
                    pCore->PSTART, pCore->PSTOP, pCore->CURR, pCore->BNRY);
    pHlp->pfnPrintf(pHlp, "  Remote DMA: RSAR=%04X RBCR=%04X CRDA=%04X\n",
                    pCore->RSAR, pCore->RBCR, pCore->CRDA);

    /* Try to figure out how much available space there is in the receive ring. */
    if (pCore->BNRY <= pCore->CURR)
        uFreePages = pCore->PSTOP - pCore->PSTART - (pCore->CURR - pCore->BNRY);
    else
        uFreePages = pCore->BNRY - pCore->CURR;
    pHlp->pfnPrintf(pHlp, "  Estimated %u free pages (%u bytes) in receive ring\n", uFreePages, uFreePages * 256);

    if (pThis->fMaybeOutOfSpace)
        pHlp->pfnPrintf(pHlp, "  Waiting for receive space\n");
    if (pThis->fLinkTempDown)
    {
        pHlp->pfnPrintf(pHlp, "  Link down count %d\n", pThis->cLinkDownReported);
        pHlp->pfnPrintf(pHlp, "  Postpone count  %d\n", pThis->cLinkRestorePostponed);
    }

    if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
    {
        /* Dump the WD specific registers. */
        pHlp->pfnPrintf(pHlp, "\nWD80x3 Control Registers\n");
        pHlp->pfnPrintf(pHlp, "  CTRL1=%02X: %s%s A18-A13=%02X\n", pThis->CTRL1,
                        pThis->ctrl1.RESET ? "RESET " : "",
                        pThis->ctrl1.MEME  ? "MEME " : "",
                        pThis->ctrl1.A13_18);
        pHlp->pfnPrintf(pHlp, "  CTRL2=%02X: %s%s A23-A19=%02X\n", pThis->CTRL2,
                        pThis->ctrl2.M16  ? "M16 "  : "",
                        pThis->ctrl2.MEMW ? "MEMW " : "",
                        pThis->ctrl2.A19_23);
    }

    if (pThis->uDevType == DEV_3C503)
    {
        PEL_GA      pGa = &pThis->ga;

        /* Dump the Gate Array state. */
        pHlp->pfnPrintf(pHlp, "\n3C503 ASIC Gate Array\n");
        pHlp->pfnPrintf(pHlp, "   PSTR=%02X00 PSPR=%02X00 cdadr=%04X\n",
                        pGa->PSTR, pGa->PSTR, pGa->CDADR);
        pHlp->pfnPrintf(pHlp, "   DQTR=%02X: tb=%d\n", pGa->DQTR,
                        pGa->dqtr.tb);
        pHlp->pfnPrintf(pHlp, "   BCFR=%02X PCFR=%02X\n",
                        pGa->BCFR, pGa->PCFR);
        pHlp->pfnPrintf(pHlp, "  GACFR=%02X: mbs=%d %s%s%s%s%s\n", pGa->GACFR,
                        pGa->gacfr.mbs,
                        pGa->gacfr.rsel ? "rsel " : "",
                        pGa->gacfr.test ? "test " : "",
                        pGa->gacfr.ows  ? "ows "  : "",
                        pGa->gacfr.tcm  ? "tcm "  : "",
                        pGa->gacfr.nim  ? "nim "  : "");
        pHlp->pfnPrintf(pHlp, "   GACR=%02X: %s%s%s%s%s%s%s%s\n", pGa->GACR,
                        pGa->gacr.rst   ? "rst "   : "",
                        pGa->gacr.xsel  ? "xsel "  : "",
                        pGa->gacr.ealo  ? "ealo "  : "",
                        pGa->gacr.eahi  ? "eahi "  : "",
                        pGa->gacr.share ? "share " : "",
                        pGa->gacr.dbsel ? "dbsel " : "",
                        pGa->gacr.ddir  ? "ddir "  : "",
                        pGa->gacr.start ? "start " : "");
        pHlp->pfnPrintf(pHlp, "  STREG=%02X: rev=%d %s%s%s%s%s\n", pGa->STREG,
                        pGa->streg.rev,
                        pGa->streg.dip   ? "dip "   : "",
                        pGa->streg.dtc   ? "dtc "   : "",
                        pGa->streg.oflw  ? "oflw "  : "",
                        pGa->streg.uflw  ? "uflw "  : "",
                        pGa->streg.dprdy ? "dprdy " : "");
        pHlp->pfnPrintf(pHlp, "  IDCFR=%02X: %s%s%s%s%s%s%s\n", pGa->IDCFR,
                        pGa->idcfr.drq1 ? "drq1 " : "",
                        pGa->idcfr.drq2 ? "drq2 " : "",
                        pGa->idcfr.drq3 ? "drq3 " : "",
                        pGa->idcfr.irq2 ? "irq2 " : "",
                        pGa->idcfr.irq3 ? "irq3 " : "",
                        pGa->idcfr.irq4 ? "irq4 " : "",
                        pGa->idcfr.irq5 ? "irq5 " : "");
        pHlp->pfnPrintf(pHlp, "  DALSB=%02X DAMSB=%02X addr=%04X\n",
                        pGa->DALSB, pGa->DAMSB,
                        RT_MAKE_U16(pGa->DALSB, pGa->DAMSB));
        pHlp->pfnPrintf(pHlp, "  VPTR0=%02X VPTR1=%02X VPTR2=%02X, VPTR=%X\n",
                        pGa->VPTR0, pGa->VPTR1, pGa->VPTR2,
                        (pGa->VPTR2 << 12) | (pGa->VPTR1 << 4) | (pGa->VPTR0 >> 4));



    }

    /* Dump the beginning of the send buffer. */
    if (fSendBuffer)
    {
        pHlp->pfnPrintf(pHlp, "Send buffer (start at %u):\n", 0);
        unsigned dump_end = RT_MIN(0 + 64, sizeof(pThis->abLocalRAM) - 16);
        for (unsigned ofs = 0; ofs < dump_end; ofs += 16)
            pHlp->pfnPrintf(pHlp, "  %04X: %Rhxs\n", ofs, &pThis->abLocalRAM[ofs]);
    }

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
}


/* -=-=-=-=-=- Helper(s) -=-=-=-=-=- */


static void dpNicR3HardReset(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    LogFlowFunc(("#%d:\n", pThis->iInstance));

    /* Initialize the PROM. Covers both NE1000 and NE2000. */
    Assert(sizeof(pThis->MacConfigured) == 6);
    memset(pThis->aPROM, 0, sizeof(pThis->aPROM));
    /* The first 6 bytes of PROM always contain the configured MAC address. */
    memcpy(&pThis->aPROM[0x00], &pThis->MacConfigured, sizeof(pThis->MacConfigured));

    if ((pThis->uDevType == DEV_NE1000) || (pThis->uDevType == DEV_NE2000))
    {
        /* The NE1000/NE2000 repeats the MAC address and also includes BB/WW signature. */
        memcpy(&pThis->aPROM[0x10], &pThis->MacConfigured, sizeof(pThis->MacConfigured));
        pThis->aPROM[0x0E] = pThis->aPROM[0x0F] = 'W';  /* Word-wide. */
        pThis->aPROM[0x1E] = pThis->aPROM[0x1F] = 'B';  /* Byte-wide. */
    }
    else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
    {
        /* The WD8003/WD8013 only uses 8 bytes of the PROM. The 7th byte
         * contains a board ID and the last byte is a checksum calculated
         * such that a two's complement sum of the 8 bytes equals FFh.
         */
        int     i;
        uint8_t sum;

        /* The board ID is 2 for 8003S, 3 for 8003E, 4 for 8003WT, 5 for 8013EBT. */
        pThis->aPROM[0x06] = 3;
        if (pThis->uDevType == DEV_WD8013)
            pThis->aPROM[0x06] = 5;

        for (i = 0, sum = 0; i < 7; ++i)
            sum += pThis->aPROM[i];

        pThis->aPROM[0x07] = 0xff - sum;
    }
    else if (pThis->uDevType == DEV_3C503)
    {
        const uint16_t  el_io_bases[]  = { 0x2E0, 0x2A0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300, 0 };
        const uint32_t  el_mem_bases[] = { 0xDC000, 0xD8000, 0xCC000, 0xC8000, 0 };
        int             i;

        /* Zap the Gate Array state. */
        memset(&pThis->ga, 0, sizeof(pThis->ga));

        /* Find the BCFR value. */
        for (i = 0; el_io_bases[i]; ++i)
        {
            if (pThis->IOPortBase == el_io_bases[i])
                break;
        }
        /// @todo Make sure we somehow disallow values that a 3C503 can't do
        if (i < 8)
            pThis->ga.BCFR = 1 << i;

        /* Find the PCFR value. */
        for (i = 0; el_mem_bases[i]; ++i)
        {
            if (pThis->MemBase == el_mem_bases[i])
                break;
        }
        /// @todo Make sure we somehow disallow values that a 3C503 can't do
        if (i < 4)
            pThis->ga.PCFR = RT_BIT(7) >> i;
    }

    /* Clear the local RAM. */
    memset(pThis->abLocalRAM, 0, sizeof(pThis->abLocalRAM));

    /* Wipe out all of the DP8390 core state. */
    memset(&pThis->core, 0, sizeof(pThis->core));

    dp8390CoreReset(pDevIns, pThis);
}

/**
 * Takes down the link temporarily if it's current status is up.
 *
 * This is used during restore and when replumbing the network link.
 *
 * The temporary link outage is supposed to indicate to the OS that all network
 * connections have been lost and that it for instance is appropriate to
 * renegotiate any DHCP lease.
 *
 * @param  pDevIns      The device instance data.
 * @param  pThis        The device state.
 */
static void dp8390TempLinkDown(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    if (pThis->fLinkUp)
    {
        pThis->fLinkTempDown = true;
        pThis->cLinkDownReported = 0;
        pThis->cLinkRestorePostponed = 0;
        pThis->Led.Asserted.s.fError = pThis->Led.Actual.s.fError = 1;
        int rc = PDMDevHlpTimerSetMillies(pDevIns, pThis->hTimerRestore, pThis->cMsLinkUpDelay);
        AssertRC(rc);
    }
}


/* -=-=-=-=-=- Saved State -=-=-=-=-=- */

/**
 * @callback_method_impl{FNSSMDEVLIVEEXEC, Pass 0 only.}
 */
static DECLCALLBACK(int) dpNicLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
{
    RT_NOREF(uPass);
    PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    pDevIns->pHlpR3->pfnSSMPutMem(pSSM, &pThis->MacConfigured, sizeof(pThis->MacConfigured));
    return VINF_SSM_DONT_CALL_AGAIN;
}


/**
 * @callback_method_impl{FNSSMDEVSAVEPREP,
 *      Serializes the receive thread, it may be working inside the critsect.}
 */
static DECLCALLBACK(int) dpNicSavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    RT_NOREF(pSSM);
    PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);

    int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    AssertRC(rc);
    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);

    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNSSMDEVSAVEEXEC}
 */
static DECLCALLBACK(int) dpNicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    PDPNICSTATE     pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    PCPDMDEVHLPR3   pHlp  = pDevIns->pHlpR3;

    /* Start with saving the generic bits. */
    pHlp->pfnSSMPutBool(pSSM, pThis->fLinkUp);
    pHlp->pfnSSMPutBool(pSSM, pThis->fNicIrqActive);

    /* Continue with DP8390 core. */
    pHlp->pfnSSMPutU8(pSSM, pThis->core.CR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.DCR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.ISR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.IMR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.RCR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.RSR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.TCR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.TSR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.NCR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.TPSR);
    pHlp->pfnSSMPutU16(pSSM, pThis->core.TBCR);
    pHlp->pfnSSMPutU16(pSSM, pThis->core.CLDA);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.PSTART);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.PSTOP);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.CURR);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.BNRY);
    pHlp->pfnSSMPutU16(pSSM, pThis->core.RSAR);
    pHlp->pfnSSMPutU16(pSSM, pThis->core.RBCR);
    pHlp->pfnSSMPutU16(pSSM, pThis->core.CRDA);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.lnxtpp);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.rnxtpp);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.CNTR0);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.CNTR1);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.CNTR2);
    pHlp->pfnSSMPutMem(pSSM, &pThis->core.pg1.PAR, sizeof(pThis->core.pg1.PAR));
    pHlp->pfnSSMPutMem(pSSM, &pThis->core.pg1.MAR, sizeof(pThis->core.pg1.MAR));
    pHlp->pfnSSMPutU8(pSSM, pThis->core.fifo.rp);
    pHlp->pfnSSMPutU8(pSSM, pThis->core.fifo.wp);
    pHlp->pfnSSMPutMem(pSSM, &pThis->core.fifo.fifo, sizeof(pThis->core.fifo.fifo));

    /* Now the WD80x3 state. */
    pHlp->pfnSSMPutU8(pSSM, pThis->CTRL1);
    pHlp->pfnSSMPutU8(pSSM, pThis->CTRL2);

    /* Finally the 3C503-specific state. */
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.PSTR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.PSPR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.DQTR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.BCFR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.PCFR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.GACFR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.GACR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.STREG);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.IDCFR);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.DAMSB);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.DALSB);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.VPTR2);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.VPTR1);
    pHlp->pfnSSMPutU8(pSSM, pThis->ga.VPTR0);
    pHlp->pfnSSMPutU16(pSSM, pThis->ga.CDADR);
    pHlp->pfnSSMPutBool(pSSM, pThis->ga.fGaIrq);

    /* Save the configured MAC address. */
    pHlp->pfnSSMPutMem(pSSM, &pThis->MacConfigured, sizeof(pThis->MacConfigured));

    return VINF_SUCCESS;
}


/**
 * @callback_method_impl{FNSSMDEVLOADPREP},
 *      Serializes the receive thread, it may be working inside the critsect.}
 */
static DECLCALLBACK(int) dpNicLoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
{
    PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    RT_NOREF(pSSM);

    int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    AssertRC(rc);

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);

    return rc;
}


/**
 * @callback_method_impl{FNSSMDEVLOADEXEC}
 */
static DECLCALLBACK(int) dpNicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
{
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    PDPNICSTATECC   pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
    PCPDMDEVHLPR3   pHlp    = pDevIns->pHlpR3;

    if (SSM_VERSION_MAJOR_CHANGED(uVersion, DPNIC_SAVEDSTATE_VERSION))
        return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;

    if (uPass == SSM_PASS_FINAL)
    {
        /* Restore data, first the generic bits. */
        pHlp->pfnSSMGetBool(pSSM, &pThis->fLinkUp);
        pHlp->pfnSSMGetBool(pSSM, &pThis->fNicIrqActive);

        /* Now the DP8390 core. */
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.CR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.DCR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.ISR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.IMR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.RCR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.RSR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.TCR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.TSR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.NCR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.TPSR);
        pHlp->pfnSSMGetU16(pSSM, &pThis->core.TBCR);
        pHlp->pfnSSMGetU16(pSSM, &pThis->core.CLDA);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.PSTART);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.PSTOP);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.CURR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.BNRY);
        pHlp->pfnSSMGetU16(pSSM, &pThis->core.RSAR);
        pHlp->pfnSSMGetU16(pSSM, &pThis->core.RBCR);
        pHlp->pfnSSMGetU16(pSSM, &pThis->core.CRDA);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.lnxtpp);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.rnxtpp);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.CNTR0);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.CNTR1);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.CNTR2);
        pHlp->pfnSSMGetMem(pSSM, &pThis->core.pg1.PAR, sizeof(pThis->core.pg1.PAR));
        pHlp->pfnSSMGetMem(pSSM, &pThis->core.pg1.MAR, sizeof(pThis->core.pg1.MAR));
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.fifo.rp);
        pHlp->pfnSSMGetU8(pSSM, &pThis->core.fifo.wp);
        pHlp->pfnSSMGetMem(pSSM, &pThis->core.fifo.fifo, sizeof(pThis->core.fifo.fifo));

        /* WD80x3-specific state. */
        pHlp->pfnSSMGetU8(pSSM, &pThis->CTRL1);
        pHlp->pfnSSMGetU8(pSSM, &pThis->CTRL2);

        /* 3C503-specific state. */
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.PSTR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.PSPR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.DQTR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.BCFR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.PCFR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.GACFR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.GACR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.STREG);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.IDCFR);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.DAMSB);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.DALSB);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.VPTR2);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.VPTR1);
        pHlp->pfnSSMGetU8(pSSM, &pThis->ga.VPTR0);
        pHlp->pfnSSMGetU16(pSSM, &pThis->ga.CDADR);
        pHlp->pfnSSMGetBool(pSSM, &pThis->ga.fGaIrq);

        /* Set IRQ and DMA based on IDCFR if this is a 3C503. */
        if (pThis->uDevType == DEV_3C503)
        {
            pThis->uIsaIrq   = elGetIrqFromIdcfr(pThis->ga.IDCFR);
            pThis->uElIsaDma = elGetDrqFromIdcfr(pThis->ga.IDCFR);
        }
    }

    /* check config */
    RTMAC       Mac;
    int rc = pHlp->pfnSSMGetMem(pSSM, &Mac, sizeof(Mac));
    AssertRCReturn(rc, rc);
    if (    memcmp(&Mac, &pThis->MacConfigured, sizeof(Mac))
        && (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)) )
        LogRel(("DPNIC#%u: The mac address differs: config=%RTmac saved=%RTmac\n", pThis->iInstance, &pThis->MacConfigured, &Mac));

    if (uPass == SSM_PASS_FINAL)
    {
        /* update promiscuous mode. */
        if (pThisCC->pDrv)
            pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, 0 /* promiscuous enabled */);

        /* Indicate link down to the guest OS that all network connections have
           been lost, unless we've been teleported here. */
        if (!PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns))
            dp8390TempLinkDown(pDevIns, pThis);
    }

    return VINF_SUCCESS;
}


/* -=-=-=-=-=- DPNICSTATE::INetworkDown -=-=-=-=-=- */

/**
 * Check if the device/driver can receive data now.
 *
 * Worker for dpNicNet_WaitReceiveAvail().  This must be called before
 * the pfnRecieve() method is called.
 *
 * @returns VBox status code.
 * @param   pDevIns         The device instance.
 * @param   pThis           The device instance data.
 */
static int dp8390CanReceive(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
{
    DP8390CORE  *pCore = &pThis->core;
    int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    AssertReleaseRC(rc);

    rc = VINF_SUCCESS;

    /*
     * The card has typically room for several full-size Ethernet frames but
     * the buffers can overflow. We cheat a bit and try to hold off when it
     * looks like there is temporarily not enough buffer spave.
     *
     * If the receiver is disabled, accept packets and drop them to avoid
     * pile-ups. If the receiver is enabled, take a closer look.
     */
    if (pCore->cr.STA && !pCore->cr.STP)
    {
        /* Receiver is enabled. Find out if we're low on buffer space.
         * But if the receive buffer isn't at least 4K big (16 pages),
         * don't bother. Typically there will be 5K or more in the
         * receive buffer.
         */
        if (pCore->PSTART + 16 <= pCore->PSTOP)
        {
            uint16_t    free_pages;

            /* Free space is between BNRY (host's read pointer) and CURR
             * (NIC's write pointer).
             */
            if (pCore->BNRY <= pCore->CURR)
            {
                /* Free space wraps around. This might technically give
                 * the wrong answer if the buffer is empty (BNRY = CURR)
                 * but in that case there's plenty of room anyway.
                 */
                free_pages = pCore->PSTOP - pCore->PSTART - (pCore->CURR - pCore->BNRY);
            }
            else
            {
                /* Free space does not wrap. */
                free_pages = pCore->BNRY - pCore->CURR;
            }
            Log2Func(("#%d: %u free pages (%u bytes)\n", pThis->iInstance, free_pages, free_pages * 256));

            /* Six pages (1,536 bytes) is enough for the longest standard Ethernet frame
             * (1522 bytes including FCS) plus packet header (4 bytes).
             */
            if (free_pages < 6)
            {
                rc = VERR_NET_NO_BUFFER_SPACE;
                Log2Func(("#%d: Buffer space low, returning %Rrc!\n", pThis->iInstance, rc));
            }
        }
    }

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
    return rc;
}


/**
 * @interface_method_impl{PDMINETWORKDOWN,pfnWaitReceiveAvail}
 */
static DECLCALLBACK(int) dpNicNet_WaitReceiveAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL cMillies)
{
    PDPNICSTATECC   pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkDown);
    PPDMDEVINS      pDevIns = pThisCC->pDevIns;
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);

    int rc = dp8390CanReceive(pDevIns, pThis);
    if (RT_SUCCESS(rc))
    {
        STAM_COUNTER_INC(&pThis->StatRxCanReceiveNow);
        return VINF_SUCCESS;
    }
    if (RT_UNLIKELY(cMillies == 0))
    {
        STAM_COUNTER_INC(&pThis->StatRxCannotReceiveNow);
        return VINF_SUCCESS; //VERR_NET_NO_BUFFER_SPACE;
    }

    rc = VERR_INTERRUPTED;
    ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, true);
    STAM_PROFILE_START(&pThis->StatRxOverflow, a);
    VMSTATE enmVMState;
    while (RT_LIKELY(   (enmVMState = PDMDevHlpVMState(pDevIns)) == VMSTATE_RUNNING
                     || enmVMState == VMSTATE_RUNNING_LS))
    {
        int rc2 = dp8390CanReceive(pDevIns, pThis);
        if (RT_SUCCESS(rc2))
        {
            rc = VINF_SUCCESS;
            break;
        }
        if (cMillies > 666)
            cMillies = 666;
        LogFlowFunc(("Waiting cMillies=%u...\n", cMillies));

        rc2 = RTSemEventWait(pThis->hEventOutOfRxSpace, cMillies);
//LogRelFunc(("RTSemEventWait: rc=%Rrc\n", rc2));
//        if (rc2 == VERR_TIMEOUT)
//            break;
    }
    STAM_PROFILE_STOP(&pThis->StatRxOverflow, a);
    ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, false);

    return rc;
}


/**
 * @interface_method_impl{PDMINETWORKDOWN,pfnReceive}
 */
static DECLCALLBACK(int) dpNicNet_Receive(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
{
    PDPNICSTATECC   pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkDown);
    PPDMDEVINS      pDevIns = pThisCC->pDevIns;
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    int             rc;

    STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
    rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    AssertReleaseRC(rc);

    if (cb > 50) /* unqualified guess */
        pThis->Led.Asserted.s.fReading = pThis->Led.Actual.s.fReading = 1;
    dp8390CoreReceiveLocked(pDevIns, pThis, (const uint8_t *)pvBuf, cb);
    pThis->Led.Actual.s.fReading = 0;

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
    STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);

    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
 */
static DECLCALLBACK(void) dpNicNet_XmitPending(PPDMINETWORKDOWN pInterface)
{
    PDPNICSTATECC   pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkDown);
    PPDMDEVINS      pDevIns = pThisCC->pDevIns;
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    dp8390CoreXmitPacket(pDevIns, pThis, true /*fOnWorkerThread*/);
}


/* -=-=-=-=-=- DPNICSTATE::INetworkConfig -=-=-=-=-=- */

/**
 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetMac}
 */
static DECLCALLBACK(int) dpNicGetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
{
    PDPNICSTATECC   pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkConfig);
    PPDMDEVINS      pDevIns = pThisCC->pDevIns;
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);

    LogFlowFunc(("#%d\n", pThis->iInstance));
    /// @todo This is broken!! We can't properly get the MAC address set by the guest
#if 0
    memcpy(pMac, pThis->core.pg1.PAR, sizeof(*pMac));
#else
    memcpy(pMac, pThis->aPROM, sizeof(*pMac));
#endif
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetLinkState}
 */
static DECLCALLBACK(PDMNETWORKLINKSTATE) dpNicGetLinkState(PPDMINETWORKCONFIG pInterface)
{
    PDPNICSTATECC   pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkConfig);
    PPDMDEVINS      pDevIns = pThisCC->pDevIns;
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);

    if (pThis->fLinkUp && !pThis->fLinkTempDown)
        return PDMNETWORKLINKSTATE_UP;
    if (!pThis->fLinkUp)
        return PDMNETWORKLINKSTATE_DOWN;
    if (pThis->fLinkTempDown)
        return PDMNETWORKLINKSTATE_DOWN_RESUME;
    AssertMsgFailed(("Invalid link state!\n"));
    return PDMNETWORKLINKSTATE_INVALID;
}


/**
 * @interface_method_impl{PDMINETWORKCONFIG,pfnSetLinkState}
 */
static DECLCALLBACK(int) dpNicSetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
{
    PDPNICSTATECC   pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkConfig);
    PPDMDEVINS      pDevIns = pThisCC->pDevIns;
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    bool            fLinkUp;

    LogFlowFunc(("#%d\n", pThis->iInstance));
    AssertMsgReturn(enmState > PDMNETWORKLINKSTATE_INVALID && enmState <= PDMNETWORKLINKSTATE_DOWN_RESUME,
                    ("Invalid link state: enmState=%d\n", enmState), VERR_INVALID_PARAMETER);

    if (enmState == PDMNETWORKLINKSTATE_DOWN_RESUME)
    {
        dp8390TempLinkDown(pDevIns, pThis);
        /*
         * Note that we do not notify the driver about the link state change because
         * the change is only temporary and can be disregarded from the driver's
         * point of view (see @bugref{7057}).
         */
        return VINF_SUCCESS;
    }
    /* has the state changed? */
    fLinkUp = enmState == PDMNETWORKLINKSTATE_UP;
    if (pThis->fLinkUp != fLinkUp)
    {
        pThis->fLinkUp = fLinkUp;
        if (fLinkUp)
        {
            /* Connect with a configured delay. */
            pThis->fLinkTempDown = true;
            pThis->cLinkDownReported = 0;
            pThis->cLinkRestorePostponed = 0;
            pThis->Led.Asserted.s.fError = pThis->Led.Actual.s.fError = 1;
            int rc = PDMDevHlpTimerSetMillies(pDevIns, pThis->hTimerRestore, pThis->cMsLinkUpDelay);
            AssertRC(rc);
        }
        else
        {
            /* Disconnect. */
            pThis->cLinkDownReported = 0;
            pThis->cLinkRestorePostponed = 0;
            pThis->Led.Asserted.s.fError = pThis->Led.Actual.s.fError = 1;
        }
        Assert(!PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
        if (pThisCC->pDrv)
            pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, enmState);
    }
    return VINF_SUCCESS;
}


/* -=-=-=-=-=- DPNICSTATE::ILeds (LUN#0) -=-=-=-=-=- */

/**
 * @interface_method_impl{PDMILEDPORTS,pfnQueryStatusLed}
 */
static DECLCALLBACK(int) dpNicQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
{
    PDPNICSTATECC   pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, ILeds);
    PPDMDEVINS      pDevIns = pThisCC->pDevIns;
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    if (iLUN == 0)
    {
        *ppLed = &pThis->Led;
        return VINF_SUCCESS;
    }
    return VERR_PDM_LUN_NOT_FOUND;
}


/* -=-=-=-=-=- DPNICSTATE::IBase (LUN#0) -=-=-=-=-=- */

/**
 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
 */
static DECLCALLBACK(void *) dpNicQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
{
    PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, IBase);
    Assert(&pThisCC->IBase == pInterface);
    PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
    PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKDOWN, &pThisCC->INetworkDown);
    PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThisCC->INetworkConfig);
    PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThisCC->ILeds);
    return NULL;
}


/* -=-=-=-=-=- PDMDEVREG -=-=-=-=-=- */

/**
 * @interface_method_impl{PDMDEVREG,pfnPowerOff}
 */
static DECLCALLBACK(void) dpNicR3PowerOff(PPDMDEVINS pDevIns)
{
    /* Poke thread waiting for buffer space. */
    dp8390R3WakeupReceive(pDevIns);
}


/**
 * @interface_method_impl{PDMDEVREG,pfnDetach}
 *
 * One port on the network card has been disconnected from the network.
 */
static DECLCALLBACK(void) dpNicR3Detach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
{
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    PDPNICSTATECC   pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
    RT_NOREF(fFlags);
    LogFlowFunc(("#%d\n", pThis->iInstance));

    AssertLogRelReturnVoid(iLUN == 0);

    int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);

    /*
     * Zero some important members.
     */
    pThis->fDriverAttached = false;
    pThisCC->pDrvBase = NULL;
    pThisCC->pDrv     = NULL;

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
}


/**
 * @interface_method_impl{PDMDEVREG,pfnAttach}
 * One port on the network card has been connected to a network.
 */
static DECLCALLBACK(int) dpNicR3Attach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
{
    PDPNICSTATE      pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    PDPNICSTATECC    pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
    RT_NOREF(fFlags);
    LogFlowFunc(("#%d\n", pThis->iInstance));

    AssertLogRelReturn(iLUN == 0, VERR_PDM_NO_SUCH_LUN);

    int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
    PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);

    /*
     * Attach the driver.
     */
    int rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
    if (RT_SUCCESS(rc))
    {
        pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
        AssertMsgStmt(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
                      rc = VERR_PDM_MISSING_INTERFACE_BELOW);
        pThis->fDriverAttached = true;
    }
    else if (   rc == VERR_PDM_NO_ATTACHED_DRIVER
             || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
    {
        /* This should never happen because this function is not called
         * if there is no driver to attach! */
        LogFunc(("#%d No attached driver!\n", pThis->iInstance));
    }

    /*
     * Temporarily drop the link if it was up so that the guest
     * will know that we have changed the configuration of the
     * network card
     */
    if (RT_SUCCESS(rc))
        dp8390TempLinkDown(pDevIns, pThis);

    PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
    return rc;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnSuspend}
 */
static DECLCALLBACK(void) dpNicR3Suspend(PPDMDEVINS pDevIns)
{
    /* Poke thread waiting for buffer space. */
    dp8390R3WakeupReceive(pDevIns);
}


/**
 * @interface_method_impl{PDMDEVREG,pfnReset}
 */
static DECLCALLBACK(void) dpNicR3Reset(PPDMDEVINS pDevIns)
{
    PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    LogFlowFunc(("#%d\n", pThis->iInstance));
    if (pThis->fLinkTempDown)
    {
        pThis->cLinkDownReported = 0x1000;
        pThis->cLinkRestorePostponed = 0x1000;
        PDMDevHlpTimerStop(pDevIns, pThis->hTimerRestore);
        dpNicR3TimerRestore(pDevIns, pThis->hTimerRestore, pThis);
    }

    dpNicR3HardReset(pDevIns, pThis);
}


/**
 * @interface_method_impl{PDMDEVREG,pfnRelocate}
 */
static DECLCALLBACK(void) dpNicR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
{
    PDPNICSTATERC pThisRC = PDMINS_2_DATA_RC(pDevIns, PDPNICSTATERC);
    pThisRC->pDrv += offDelta;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnDestruct}
 */
static DECLCALLBACK(int) dpNicR3Destruct(PPDMDEVINS pDevIns)
{
    PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
    PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);

    if (PDMDevHlpCritSectIsInitialized(pDevIns, &pThis->CritSect))
    {
        RTSemEventSignal(pThis->hEventOutOfRxSpace);
        RTSemEventDestroy(pThis->hEventOutOfRxSpace);
        pThis->hEventOutOfRxSpace = NIL_RTSEMEVENT;
        PDMDevHlpCritSectDelete(pDevIns, &pThis->CritSect);
    }
    return VINF_SUCCESS;
}


/**
 * @interface_method_impl{PDMDEVREG,pfnConstruct}
 */
static DECLCALLBACK(int) dpNicR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
    PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
    PDPNICSTATE     pThis   = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
    PDPNICSTATECC   pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
    PCPDMDEVHLPR3   pHlp  = pDevIns->pHlpR3;
    PPDMIBASE       pBase;
    char            szTmp[128];
    int             rc;

    /*
     * Init what's required to make the destructor safe.
     */
    pThis->iInstance            = iInstance;
    pThis->hEventOutOfRxSpace   = NIL_RTSEMEVENT;
    pThis->hIoPortsNic          = NIL_IOMIOPORTHANDLE;
    pThis->hIoPortsCore         = NIL_IOMIOPORTHANDLE;
    pThisCC->pDevIns            = pDevIns;

    /*
     * Validate configuration.
     */
    PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "MAC|CableConnected|Port|MemBase|IRQ|DMA|DeviceType|LinkUpDelay|LineSpeed", "");

    /*
     * Read the configuration.
     */
    rc = pHlp->pfnCFGMQueryBytes(pCfg, "MAC", &pThis->MacConfigured, sizeof(pThis->MacConfigured));
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Failed to get the \"MAC\" value"));
    rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "CableConnected", &pThis->fLinkUp, true);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Failed to get the \"CableConnected\" value"));

    /*
     * Determine the model.
     */
    char szDeviceType[16];
    rc = pHlp->pfnCFGMQueryStringDef(pCfg, "DeviceType", &szDeviceType[0], sizeof(szDeviceType), "NE2000");
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"ChipType\" as string failed"));

    if (!strcmp(szDeviceType, "NE1000"))
        pThis->uDevType = DEV_NE1000;   /* Novell NE1000. */
    else if (!strcmp(szDeviceType, "NE2000"))
        pThis->uDevType = DEV_NE2000;   /* Novell NE2000. */
    else if (!strcmp(szDeviceType, "WD8003"))
        pThis->uDevType = DEV_WD8003;   /* WD EtherCard Plus. */
    else if (!strcmp(szDeviceType, "WD8013"))
        pThis->uDevType = DEV_WD8013;   /* WD EtherCard Plus 16. */
    else if (!strcmp(szDeviceType, "3C503"))
        pThis->uDevType = DEV_3C503;    /* 3Com 3C503 EtherLink II. */
    else
        return PDMDevHlpVMSetError(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES, RT_SRC_POS,
                                   N_("Configuration error: The \"DeviceType\" value \"%s\" is unsupported"),
                                   szDeviceType);


    /*
     * Default resource assignments depend on the device type.
     */
    unsigned uDefIoPort  = 0;   /* To be overridden. */
    unsigned uDefIrq     = 0;
    unsigned uDefDma     = 0;   /* Default to no DMA. */
    unsigned uDefMemBase = 0;   /* Default to no shared memory. */

    if ((pThis->uDevType == DEV_NE1000) || (pThis->uDevType == DEV_NE2000))
    {
        uDefIoPort = 0x300;
        uDefIrq    = 3;
    }
    else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
    {
        uDefIoPort  = 0x280;
        uDefIrq     = 3;
        uDefMemBase = 0xd0000;
        pThis->cbMemSize = _8K;
        if (pThis->uDevType == DEV_WD8013)
            pThis->cbMemSize = _16K;
    }
    else if (pThis->uDevType == DEV_3C503)
    {
        uDefIoPort  = 0x300;
        uDefIrq     = 3;
        uDefDma     = 1;
        uDefMemBase = 0xdc000;
        pThis->cbMemSize = _8K;
    }

    /*
     * Process ISA configuration options.
     */
    rc = pHlp->pfnCFGMQueryPortDef(pCfg, "Port", &pThis->IOPortBase, uDefIoPort);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Failed to get the \"Port\" value"));

    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "IRQ", &pThis->uIsaIrq, uDefIrq);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Failed to get the \"IRQ\" value"));

    rc = pHlp->pfnCFGMQueryU8Def(pCfg, "DMA", &pThis->uIsaDma, uDefDma);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Failed to get the \"DMA\" value"));

    rc = pHlp->pfnCFGMQueryGCPtrDef(pCfg, "MemBase", &pThis->MemBase, uDefMemBase);
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Failed to get the \"MemBase\" value"));



    rc = pHlp->pfnCFGMQueryU32Def(pCfg, "LinkUpDelay", (uint32_t*)&pThis->cMsLinkUpDelay, 5000); /* ms */
    if (RT_FAILURE(rc))
        return PDMDEV_SET_ERROR(pDevIns, rc,
                                N_("Configuration error: Failed to get the value of 'LinkUpDelay'"));
    Assert(pThis->cMsLinkUpDelay <= 300000); /* less than 5 minutes */
    if (pThis->cMsLinkUpDelay > 5000 || pThis->cMsLinkUpDelay < 100)
    {
        LogRel(("DPNIC#%d WARNING! Link up delay is set to %u seconds!\n",
                iInstance, pThis->cMsLinkUpDelay / 1000));
    }
    LogFunc(("#%d Link up delay is set to %u seconds\n",
         iInstance, pThis->cMsLinkUpDelay / 1000));


    /*
     * Initialize data (most of it anyway).
     */
    pThis->Led.u32Magic                       = PDMLED_MAGIC;
    /* IBase */
    pThisCC->IBase.pfnQueryInterface          = dpNicQueryInterface;
    /* INetworkPort */
    pThisCC->INetworkDown.pfnWaitReceiveAvail = dpNicNet_WaitReceiveAvail;
    pThisCC->INetworkDown.pfnReceive          = dpNicNet_Receive;
    pThisCC->INetworkDown.pfnXmitPending      = dpNicNet_XmitPending;
    /* INetworkConfig */
    pThisCC->INetworkConfig.pfnGetMac         = dpNicGetMac;
    pThisCC->INetworkConfig.pfnGetLinkState   = dpNicGetLinkState;
    pThisCC->INetworkConfig.pfnSetLinkState   = dpNicSetLinkState;
    /* ILeds */
    pThisCC->ILeds.pfnQueryStatusLed          = dpNicQueryStatusLed;

    pThis->hIoPortsCore = NIL_IOMIOPORTHANDLE;
    pThis->hIoPortsNic  = NIL_IOMIOPORTHANDLE;
    pThis->hSharedMem   = NIL_IOMMMIOHANDLE;

    /*
     * We use our own critical section (historical reasons).
     */
    rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "DPNIC#%u", iInstance);
    AssertRCReturn(rc, rc);
    rc = PDMDevHlpSetDeviceCritSect(pDevIns, &pThis->CritSect);
    AssertRCReturn(rc, rc);

    rc = RTSemEventCreate(&pThis->hEventOutOfRxSpace);
    AssertRCReturn(rc, rc);

    /*
     * Register ISA I/O ranges. This depends on the device type.
     */
    if ((pThis->uDevType == DEV_NE1000) || (pThis->uDevType == DEV_NE2000))
    {
        /* The NE1000 and NE2000 map the DP8390 at the beginning of the port range,
         * followed by the data/reset ports.
         */
        rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase, 0x10 /*cPorts*/, dp8390CoreIOPortWrite, dp8390CoreIOPortRead,
                                         "DP8390-Core", NULL /*paExtDesc*/, &pThis->hIoPortsCore);
        if (RT_FAILURE(rc))
            return rc;
        rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase + 0x10, 0x10 /*cPorts*/, neIOPortWrite, neIOPortRead,
                                         "DPNIC-NE", NULL /*paExtDesc*/, &pThis->hIoPortsNic);
        if (RT_FAILURE(rc))
            return rc;
    }
    else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
    {
        /* The WD8003 and WD8013 map the DP8390 at the end of the port range
         * (16 bytes into it). The first 8 bytes of the range are largely unused
         * while the second 8 bytes map the PROM.
         */
        rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase, 0x10 /*cPorts*/, wdIOPortWrite, wdIOPortRead,
                                         "DPNIC-WD", NULL /*paExtDesc*/, &pThis->hIoPortsNic);
        if (RT_FAILURE(rc))
            return rc;
        rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase + 0x10, 0x10 /*cPorts*/, dp8390CoreIOPortWrite, dp8390CoreIOPortRead,
                                         "DP8390-Core", NULL /*paExtDesc*/, &pThis->hIoPortsCore);
        if (RT_FAILURE(rc))
            return rc;

        /*
         * Shared memory MMIO area. This is rather lame.
         */
        rc = PDMDevHlpMmioCreateExAndMap(pDevIns, pThis->MemBase, pThis->cbMemSize,
                                         IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU | IOMMMIO_FLAGS_ABS,
                                         NULL /*pPciDev*/, UINT32_MAX /*iPciRegion*/,
                                         wdMemWrite, wdMemRead, NULL /*wdMmioFill*/, NULL /*pvUser*/,
                                         "DPNIC - WD Shared RAM", &pThis->hSharedMem);
        AssertRCReturn(rc, rc);

        /* Hack to make WD drivers happy. */
        memcpy(&pThis->MacConfigured, "\x00\x00\xC0", 3);
    }
    else if (pThis->uDevType == DEV_3C503)
    {
        /* The 3C503 maps the DP8390 at the base I/O address, except the first
         * or second 16 bytes of PROM can be mapped into the same space. The
         * custom Gate Array is mapped at I/O base + 400h.
         */
        rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase, 0x10 /*cPorts*/, dp8390CoreIOPortWrite, dp8390CoreIOPortRead,
                                         "DP8390-Core", NULL /*paExtDesc*/, &pThis->hIoPortsCore);
        if (RT_FAILURE(rc))
            return rc;

        rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase + 0x400, 0x10 /*cPorts*/, elIOPortWrite, elIOPortRead,
                                         "DPNIC-EL", NULL /*paExtDesc*/, &pThis->hIoPortsNic);
        if (RT_FAILURE(rc))
            return rc;

        /*
         * Shared memory MMIO area. The same lame thing.
         */
        rc = PDMDevHlpMmioCreateExAndMap(pDevIns, pThis->MemBase, pThis->cbMemSize,
                                         IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU | IOMMMIO_FLAGS_ABS,
                                         NULL /*pPciDev*/, UINT32_MAX /*iPciRegion*/,
                                         elMemWrite, elMemRead, NULL /*elMmioFill*/, NULL /*pvUser*/,
                                         "DPNIC - 3C503 Shared RAM", &pThis->hSharedMem);
        AssertRCReturn(rc, rc);

        /*
         * Register DMA channel.
         */
        if ((pThis->uIsaDma >= ELNKII_MIN_VALID_DMA) && (pThis->uIsaDma <= ELNKII_MAX_VALID_DMA))
        {
            rc = PDMDevHlpDMARegister(pDevIns, pThis->uIsaDma, elnk3R3DMAXferHandler, pThis);
            if (RT_FAILURE(rc))
                return rc;
            LogRel(("DPNIC#%d: Enabling 3C503 DMA channel %u\n", iInstance, pThis->uIsaDma));
        }
        else
            LogRel(("DPNIC#%d: Disabling 3C503 DMA\n", iInstance));

        /* Hack to make 3C503 diagnostics happy. */
        memcpy(&pThis->MacConfigured, "\x02\x60\x8C", 3);
    }


    rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, dpNicR3TimerRestore, NULL, TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
                              "DPNIC Link Restore Timer", &pThis->hTimerRestore);
    if (RT_FAILURE(rc))
        return rc;

    rc = PDMDevHlpSSMRegisterEx(pDevIns, DPNIC_SAVEDSTATE_VERSION, sizeof(*pThis), NULL,
                                NULL,           dpNicLiveExec, NULL,
                                dpNicSavePrep, dpNicSaveExec, NULL,
                                dpNicLoadPrep, dpNicLoadExec, NULL);
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Create the transmit notifier signaller.
     */
    rc = PDMDevHlpTaskCreate(pDevIns, PDMTASK_F_RZ, "DPNIC-Xmit", dpNicR3XmitTaskCallback, NULL /*pvUser*/, &pThis->hXmitTask);
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Create the RX notifier signaller.
     */
    rc = PDMDevHlpTaskCreate(pDevIns, PDMTASK_F_RZ, "DPNIC-Rcv", dpNicR3CanRxTaskCallback, NULL /*pvUser*/, &pThis->hCanRxTask);
    if (RT_FAILURE(rc))
        return rc;

    /*
     * Register the info item.
     */
    RTStrPrintf(szTmp, sizeof(szTmp), "dpnic%d", pThis->iInstance);
    PDMDevHlpDBGFInfoRegister(pDevIns, szTmp, "dpnic info", dpNicR3Info);

    /*
     * Attach status driver (optional).
     */
    rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThisCC->IBase, &pBase, "Status Port");
    if (RT_SUCCESS(rc))
        pThisCC->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
    else if (   rc != VERR_PDM_NO_ATTACHED_DRIVER
             && rc != VERR_PDM_CFG_MISSING_DRIVER_NAME)
    {
        AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
        return rc;
    }

    /*
     * Attach driver.
     */
    rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
    if (RT_SUCCESS(rc))
    {
        pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
        AssertMsgReturn(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
                        VERR_PDM_MISSING_INTERFACE_BELOW);
        pThis->fDriverAttached = true;
    }
    else if (   rc == VERR_PDM_NO_ATTACHED_DRIVER
             || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
    {
        /* No error! */
        LogFunc(("No attached driver!\n"));
    }
    else
        return rc;

    /*
     * Reset the device state. (Do after attaching.)
     */
    dpNicR3HardReset(pDevIns, pThis);

    /*
     * Register statistics counters.
     */
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceiveBytes,       STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,          "Amount of data received",                "/Public/Net/DPNIC%u/BytesReceived", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitBytes,      STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,          "Amount of data transmitted",             "/Public/Net/DPNIC%u/BytesTransmitted", iInstance);

    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceiveBytes,       STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,          "Amount of data received",                "/Devices/DPNIC%d/ReceiveBytes", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitBytes,      STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,          "Amount of data transmitted",             "/Devices/DPNIC%d/TransmitBytes", iInstance);

#ifdef VBOX_WITH_STATISTICS
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOReadRZ,           STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in RZ",               "/Devices/DPNIC%d/IO/ReadRZ", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOReadR3,           STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in R3",               "/Devices/DPNIC%d/IO/ReadR3", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOWriteRZ,          STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in RZ",              "/Devices/DPNIC%d/IO/WriteRZ", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOWriteR3,          STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in R3",              "/Devices/DPNIC%d/IO/WriteR3", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceive,            STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling receive",                      "/Devices/DPNIC%d/Receive", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxOverflow,         STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling RX overflows",            "/Devices/DPNIC%d/RxOverflow", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxOverflowWakeup,   STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES    , "Nr of RX overflow wakeups",              "/Devices/DPNIC%d/RxOverflowWakeup", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxCanReceiveNow,    STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES    , "Can receive immediately",                "/Devices/DPNIC%d/RxCanReceiveNow", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxCannotReceiveNow, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES    , "Cannot receive, not waiting",            "/Devices/DPNIC%d/RxCannotReceiveNow", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitRZ,         STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling transmits in RZ",              "/Devices/DPNIC%d/Transmit/TotalRZ", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitR3,         STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling transmits in R3",              "/Devices/DPNIC%d/Transmit/TotalR3", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitSendRZ,     STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in RZ",          "/Devices/DPNIC%d/Transmit/SendRZ", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitSendR3,     STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in R3",          "/Devices/DPNIC%d/Transmit/SendR3", iInstance);

    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatInterrupt,          STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling interrupt checks",             "/Devices/DPNIC%d/UpdateIRQ", iInstance);

    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktMonitor,     STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,     "Dropped packet, monitor mode",           "/Devices/DPNIC%d/DropPktMonitor", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktRcvrDis,     STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,     "Dropped packet, receiver not enabled",   "/Devices/DPNIC%d/DropPktRcvrDis", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktVeryShort,   STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,     "Dropped packet less than 8 bytes long",  "/Devices/DPNIC%d/DropPktVeryShort", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktVMNotRunning,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,     "Dropped packet, VM not running",         "/Devices/DPNIC%d/DropPktVMNotRunning", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktNoLink,      STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,     "Dropped packet, no link",                "/Devices/DPNIC%d/DropPktNoLink", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktNoMatch,     STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,     "Dropped packet, address match reject",   "/Devices/DPNIC%d/DropPktNoMatch", iInstance);
    PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktNoBuffer,    STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,     "Dropped packet, DP8390 buffer overflow", "/Devices/DPNIC%d/DropPktNoBuffer", iInstance);
#endif /* VBOX_WITH_STATISTICS */

    return VINF_SUCCESS;
}

#else

/**
 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
 */
static DECLCALLBACK(int) dpNicRZConstruct(PPDMDEVINS pDevIns)
{
    PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
    PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);

    /* Critical section setup: */
    int rc = PDMDevHlpSetDeviceCritSect(pDevIns, &pThis->CritSect);
    AssertRCReturn(rc, rc);

    /* NIC-specific ISA I/O ports: */
    if (pThis->hIoPortsNic != NIL_IOMIOPORTHANDLE)
    {
        switch (pThis->uDevType)
        {
        case DEV_NE1000:
        case DEV_NE2000:
            rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsNic, neIOPortWrite, neIOPortRead, NULL /*pvUser*/);
            AssertRCReturn(rc, rc);
            break;
        case DEV_WD8003:
        case DEV_WD8013:
            rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsNic, wdIOPortWrite, wdIOPortRead, NULL /*pvUser*/);
            AssertRCReturn(rc, rc);
            break;
        case DEV_3C503:
            rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsNic, elIOPortWrite, elIOPortRead, NULL /*pvUser*/);
            AssertRCReturn(rc, rc);
            break;
        default:
            /* Must not happen. */
            return VERR_INTERNAL_ERROR;
        }
    }

    /* Common DP8390 core I/O ports: */
    if (pThis->hIoPortsCore != NIL_IOMIOPORTHANDLE)
    {
        rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsCore, dp8390CoreIOPortWrite, dp8390CoreIOPortRead, NULL /*pvUser*/);
        AssertRCReturn(rc, rc);
    }

    /* Shared RAM, if used: */
    if (pThis->hSharedMem != NIL_IOMMMIOHANDLE)
    {
        AssertRCReturn(rc, rc);
        switch (pThis->uDevType)
        {
        case DEV_WD8003:
        case DEV_WD8013:
            rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hSharedMem, wdMemWrite, wdMemRead, NULL /*pvUser*/);
            AssertRCReturn(rc, rc);
            break;
        case DEV_3C503:
            rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hSharedMem, elMemWrite, elMemRead, NULL /*pvUser*/);
            AssertRCReturn(rc, rc);
            break;
        case DEV_NE1000:
        case DEV_NE2000:
        default:
            /* Must not happen. */
            return VERR_INTERNAL_ERROR;
        }
    }

    return VINF_SUCCESS;
}

#endif /* IN_RING3 */

/**
 * The device registration structure.
 */
const PDMDEVREG g_DeviceDP8390 =
{
    /* .u32Version = */             PDM_DEVREG_VERSION,
    /* .uReserved0 = */             0,
    /* .szName = */                 "dp8390",
    /* .fFlags = */                 PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
    /* .fClass = */                 PDM_DEVREG_CLASS_NETWORK,
    /* .cMaxInstances = */          ~0U,
    /* .uSharedVersion = */         42,
    /* .cbInstanceShared = */       sizeof(DPNICSTATE),
    /* .cbInstanceCC = */           sizeof(DPNICSTATECC),
    /* .cbInstanceRC = */           sizeof(DPNICSTATERC),
    /* .cMaxPciDevices = */         0,
    /* .cMaxMsixVectors = */        0,
    /* .pszDescription = */         "National Semiconductor DP8390 based adapter.\n",
#if defined(IN_RING3)
    /* .pszRCMod = */               "VBoxDDRC.rc",
    /* .pszR0Mod = */               "VBoxDDR0.r0",
    /* .pfnConstruct = */           dpNicR3Construct,
    /* .pfnDestruct = */            dpNicR3Destruct,
    /* .pfnRelocate = */            dpNicR3Relocate,
    /* .pfnMemSetup = */            NULL,
    /* .pfnPowerOn = */             NULL,
    /* .pfnReset = */               dpNicR3Reset,
    /* .pfnSuspend = */             dpNicR3Suspend,
    /* .pfnResume = */              NULL,
    /* .pfnAttach = */              dpNicR3Attach,
    /* .pfnDetach = */              dpNicR3Detach,
    /* .pfnQueryInterface = */      NULL,
    /* .pfnInitComplete = */        NULL,
    /* .pfnPowerOff = */            dpNicR3PowerOff,
    /* .pfnSoftReset = */           NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#elif defined(IN_RING0)
    /* .pfnEarlyConstruct = */      NULL,
    /* .pfnConstruct = */           dpNicRZConstruct,
    /* .pfnDestruct = */            NULL,
    /* .pfnFinalDestruct = */       NULL,
    /* .pfnRequest = */             NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#elif defined(IN_RC)
    /* .pfnConstruct = */           NULL,
    /* .pfnReserved0 = */           NULL,
    /* .pfnReserved1 = */           NULL,
    /* .pfnReserved2 = */           NULL,
    /* .pfnReserved3 = */           NULL,
    /* .pfnReserved4 = */           NULL,
    /* .pfnReserved5 = */           NULL,
    /* .pfnReserved6 = */           NULL,
    /* .pfnReserved7 = */           NULL,
#else
# error "Not in IN_RING3, IN_RING0 or IN_RC!"
#endif
    /* .u32VersionEnd = */          PDM_DEVREG_VERSION
};

#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */