VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DevDP8390.cpp@ 94264

Last change on this file since 94264 was 93675, checked in by vboxsync, 3 years ago

Dev3C501/DevDP8390: Fixed saved state loading; force link restore after about 6 seconds because old drivers may not expect cable disconnects at all and will never notice.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 196.7 KB
Line 
1/* $Id: DevDP8390.cpp 93675 2022-02-10 09:45:11Z vboxsync $ */
2/** @file
3 * DevDP8390 - National Semiconductor DP8390-based Ethernet Adapter Emulation.
4 */
5
6/*
7 * Copyright (C) 2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/** @page pg_dev_dp8390 NatSemi DP8390-Based Ethernet NIC Emulation.
19 *
20 * This software was written based on the following documents:
21 *
22 * - National Semiconductor DP8390/NS32490 Network Interface Controller,
23 * 1986
24 * - National Semiconductor DP8390D/NS32490D NIC Network Interface
25 * Controller datasheet, July 1995
26 * - National Semiconductor Application Note 729, DP839EB-ATN IBM PC-AT
27 * Compatible DP83901 SNIC Serial Network Interface Controller
28 * Evaluation Board, 1993
29 * - National Semiconductor Application Note 842, The Design and Operation
30 * of a Low Cost, 8-Bit PC-XT Compatible Ethernet Adapter Using
31 * the DP83902, May 1993
32 * - National Semiconductor Application Note 858, Guide to Loopback Using
33 * the DP8390 Chip Set, October 1992
34 * - National Semiconductor Application Note 875, DP83905EB-AT AT/LANTIC
35 * Evaluation Board, June 1993
36 * - Western Digital WD83C584 Bus Interface Controller Device datasheet,
37 * October 29, 1990
38 * - Western Digital WD83C690 Ethernet LAN Controller datasheet,
39 * November 2, 1990
40 * - 3Com EtherLink II Adapter Technical Reference Manual,
41 * March 1991
42 *
43 * This emulation is compatible with drivers for:
44 * - Novell/Eagle/Anthem NE1000 (8-bit)
45 * - Novell/Eagle/Anthem NE2000 (16-bit)
46 * - Western Digital/SMC WD8003E (8-bit)
47 * - Western Digital/SMC WD8013EBT (16-bit)
48 * - 3Com EtherLink II 3C503 (8-bit)
49 *
50 *
51 * The National Semiconductor DP8390 was an early (circa 1986) low-cost
52 * Ethernet controller, typically accompanied by the DP8391 Serial Network
53 * Interface and the DP8392 Coaxial Transceiver Interface.
54 *
55 * Due to its relatively low cost, the DP8390 NIC was chosen for several
56 * very widespread early PC Ethernet designs, namely the Novell NE1000/NE2000,
57 * Western Digital (later SMC) WD8003 EtherCard Plus, and 3Com EtherLink II.
58 * The popularity of these cards, especially the NE2000, in turn spawned
59 * a bevy of compatible chips from National Semiconductor and many others.
60 *
61 * All common DP8390-based cards have onboard memory. The initial WD8003E and
62 * NE1000 cards have one 8Kx8 SRAM; 16-bit cards like WD8013E or NE2000 have
63 * two 8Kx8 SRAMs wired in 8Kx16 configuration to enable 16-bit wide transfers.
64 * The DP8390 can address up to 64K or local memory and uses "Local DMA"
65 * (similar to bus mastering) to access it. Some newer cards had 32K or more
66 * onboard RAM. Note that an NE2000 in 8-bit mode can only address 8K local
67 * memory, effectively reverting to an NE1000.
68 *
69 * The DP8390 uses "Remote DMA" to move data between local memory and the host
70 * system. Remote DMA is quite similar to 8237-style third party DMA, except
71 * the DMA controller is on the DP8390 chip in this case.
72 *
73 * The DP8390 has a control bit (DCR.WTS) which selects whether all DMA (both
74 * Local and Remote) transfers are 8-bit or 16-bit. Word-wide transfers can
75 * generally only be used on a 16-bit card in a 16-bit slot, because only then
76 * can the host drive 16-bit I/O cycles to the data ports. That is why
77 * an NE2000 in an 8-bit slot can only use half of its local RAM -- remote DMA
78 * simply cannot access half of the 8Kx16 SRAM.
79 *
80 * The DP8390 maps its internal registers as sixteen 8-bit wide I/O ports.
81 * There are four register pages, selectable through the Command Register (CR)
82 * which is accessible at offset 0 in all pages.
83 *
84 * The NE1000/NE2000 cards only use I/O and IRQ resources, not memory
85 * or DMA. In contrast, the Western Digital cards use memory-mapped buffers.
86 * Later AT/LANTIC (DP83905) based NE2000-compatible cards can optionally
87 * use memory as well. The 3Com EtherLink II (3C503) uses a custom gate array
88 * in addition to the DP8390 and can use programmed I/O, 8237 DMA, as well
89 * as optional direct memory mapping.
90 *
91 * Address decoding is typically incomplete, which causes the buffer RAM and
92 * possibly PROM to be aliased multiple times in the DP8390's address space.
93 *
94 * Buffer overflow handling is slightly tricky. The DP8390 assumes that if
95 * the receiver is enabled, there is space for at least one page (256 bytes).
96 * Once it fills up the page and advances the CURR pointer, the DP8390 checks
97 * whether CURR equals BNRY and if so, triggers an overflow condition. Note
98 * that after the NIC is initialized, CURR *will* normally equal BNRY, with
99 * both pointing at the beginning of the receive ring (PSTART). An overflow
100 * is only triggered when CURR equals BNRY right after advancing.
101 *
102 * The documentation of the Send Packet command mentions that when CRDA crosses
103 * the PSTOP register, the current remote DMA address (i.e. CRDA) is set to
104 * the PSTART value, which is rather convenient when reading received packets
105 * out of the ring buffer using remote DMA. The documentation does not mention
106 * that the same logic applies for all remote DMA reads, a feature that several
107 * NE1000/NE2000 drivers (packet drivers, Novell ODI) rely on. This is logical,
108 * because reading out of the receive ring buffer address range always implies
109 * reading received packets, and then the PSTOP->PSTART wraparound becomes
110 * desirable. It is unclear whether the same wraparound handling also applies
111 * for remote DMA writes within the receive ring buffer.
112 *
113 * The documentation is not very clear on how the CRDA register is managed.
114 * One might be led to believe that starting remote DMA copies the remote DMA
115 * start address (i.e. RSAR) to the CRDA register. However, the NE1000 ODI
116 * driver for OS/2 1.0 (NE1000.SYS from early 1988) relies on restarting remote
117 * DMA and continuing where it left off. The DP8390D datasheet only mentions
118 * this in a passing fashion at the end of the "Remote Write with High Speed
119 * Buses" section, saying that if a dummy remote read is executed before a
120 * remote write, RSAR can be set up for the dummy read such that the CRDA
121 * register contains the desired value for the following write.
122 *
123 * Conversely, it is not spelled out that writing RSAR also updates CRDA, but
124 * at least Novell's NE2000 ODI driver v2.12 is known to rely on this behavior
125 * and checks that a write to RSAR is reflected in CRDA.
126 *
127 * Loopback operation is limited in the DP8390. Because it is a half-duplex
128 * device, it cannot truly transmit and receive simultaneously. When loopback
129 * is in effect, the received data is *not* written into memory. Only the last
130 * few bytes of the packet are visible in the FIFO.
131 *
132 * Likewise due to its half-duplex nature, the CRC circuitry during loopback
133 * works either only on the transmit side (FCS is generated but not checked)
134 * or the receive side (FCS is checked but not generated).
135 *
136 * The loopback behavior is even stranger when DCR.WTS is set to enabled 16-bit
137 * DMA transfers. Even though the chip reads 16 bits at a time, only 8 bits are
138 * actually transmitted; the DCR.BOS bit determines whether the low or high
139 * 8 bits of each words are transmitted. As a consequence, the programmed length
140 * of the transmit is also halved.
141 *
142 * Because loopback operation is so different from normal send/receive, loopback
143 * packets are not run through the normal receive path and are treated specially
144 * instead. The WD and especially 3C503 diagnostics exercise the loopback
145 * functionality fairly thoroughly.
146 *
147 *
148 * NE1000 and NE2000
149 * -----------------
150 *
151 * Common NE1000/NE2000 configurations in Novell drivers:
152 * I/O Base = 300h, IRQ = 3 (default)
153 * I/O Base = 320h, IRQ = 2
154 * I/O Base = 340h, IRQ = 4
155 * I/O Base = 360h, IRQ = 5
156 * The I/O base can be set to 300h/320h/340h/360h; the IRQ to 2, 3, 4, 5.
157 * No memory or DMA is used.
158 *
159 * The NE1000/NE2000 adds a data register and a reset register to the I/O
160 * space. A PROM containing the node address is mapped into the DP8390's local
161 * address space.
162 *
163 * The mapping of the 32x8 PROM on an NE2000 card is quite non-obvious but
164 * fortunately well explained in the AN-729 Application Note. Address lines
165 * A4-A1 of the internal bus are connected to lines A3-A0 of the PROM
166 * (enabling 16 distinct bytes of the 32-byte PROM to be addressed). However,
167 * the negated EN16 signal, which is active when the NE2000 is in a 16-bit
168 * slot, is connected to the PROM's address line A4. That means an NE2000 in
169 * a 16-bit slot reads different PROM bytes than when the same card is in an
170 * 8-bit slot. The PROM is structured such that an NE2000 in an 8-bit slot
171 * reads a 'BB' signature (same as NE1000) at PROM offset 1Eh/1Fh, while
172 * an NE2000 in a 16-bit slot returns a 'WW' signature from PROM offset
173 * 0Eh/0Fh instead.
174 *
175 * The original NE1000 boards Assy. #950-054401 actually only had 6 bytes of
176 * MAC address in the PROM, the rest was unused (0FFh). Software supporting the
177 * NE1000 thus should not examine the PROM contents beyond the first 6 bytes.
178 *
179 * Novell's old OUI was 00:00:D8 but drivers are not known to check for it.
180 *
181 * Newer DP83905 AT/LANTIC based NE2000plus cards were optionally capable of
182 * using shared RAM in a manner very similar to the WD8003/WD8013.
183 *
184 *
185 * WD8003 and WD8013 EtherCard Plus
186 * --------------------------------
187 *
188 * Common WD8013 configurations:
189 * I/O Base = 280h, IRQ = 3, RAM D000-D3FF (default)
190 * I/O Base = 330h, IRQ = 10, RAM CC00-CFFF
191 * I/O Base = 240h, IRQ/RAM soft-configurable
192 * The I/O base can be set anywhere in the 2xxh-3xxh range in 20h increments.
193 * The IRQs available on a WD8013 are 2, 3, 4, 5, 7, 10, 11, 15. The shared
194 * RAM can be anywhere between 80000h (512K) to FFC000h (16M-16K) in 16K
195 * increments.
196 *
197 * The Western Digital WD8003E appeared at around the same time as Novell's
198 * NE1000 (1987). It is likewise a short 8-bit ISA card with 8Kx8 onboard
199 * SRAM. The major difference is that rather than using remote DMA to move
200 * data between the host and local RAM, the WD8003 directly mapps the onboard
201 * memory to the host's address space (often called shared memory). A later
202 * 16-bit WD8013 model used 8Kx16 SRAM, and there were follow-on WD8003 models
203 * with 16K or 32K local RAM.
204 *
205 * Instead of mapping the PROM into the DP8390's local address space, the
206 * WD8003/WD8013 exposes the node address through the I/O space; the DP8390's
207 * local address space only contains buffer RAM.
208 *
209 * The WD8003 cannot use remote DMA at all; the host must use shared memory.
210 * Remote DMA can be programmed but there is no way to trigger RDMA transfers.
211 *
212 * Western Digital's brand name for WD8003/WD8013 was EtherCard. Circa 1991,
213 * WD sold the networking business to SMC; SMC continued to sell and further
214 * develop the cards under the Elite brand name, also designated as the
215 * SMC8000 series.
216 *
217 * The original WD8003E/EBT/WT uses very simple glue logic around the DP8390
218 * and must be configured through jumpers. Newer WD8003EB/EP/EW/W/WC uses an
219 * interface chip (WD83C583, WD83C584, or later) with an EEPROM and can be
220 * configured through a software utility.
221 *
222 * Similarly the 16-bit WD8013EBT is configured only though jumpers, while
223 * the newer WD8013EB/W/EW/EWC/WC/EPC are software configurable.
224 *
225 * The "Board ID" byte (at offset 6 in the PROM) is used to distinguish
226 * between the various models.
227 *
228 * Newer WD cards use the WD83C690 controller rather than DP8390. The
229 * WD83C690 is close enough to DP8390 that old WD drivers should work with
230 * it, but it has a number of differences. It has no support for Remote DMA
231 * whatsoever, and does not implement multicast filtering.
232 *
233 * The WD83C690 also handles receive buffer overflows somewhat differently;
234 * the DP8390 never fills the last remaining buffer page, meaning that
235 * CURR=BNRY indicates an empty buffer while CURR=BNRY-1 means buffer full.
236 * The WD83C690 can fill all pages and decides whether it is full or empty
237 * based on whether CURR or BNRY was changed more recently.
238 *
239 * Old Western Digital utilities/drivers may require the card to have WD's
240 * old OUI of 00:00:0C and refuse to recognize the hardware otherwise.
241 *
242 * The emulation passes WD diagnostics with no errors (DIAGNOSE.EXE Ver 1.11,
243 * dated 12/12/1989).
244 *
245 *
246 * 3C503 EtherLink II
247 * ------------------
248 *
249 * Common 3C503 configurations in Novell drivers:
250 * I/O Base = 300h, IRQ = 3 (default)
251 * The I/O base can be set via jumpers to 2E0h, 2A0h, 280h, 250h, 350h, 330h,
252 * 310h, or 300h (default). The ROM/RAM can be optionally mapped to one of
253 * DC000-DFFFF, D8000-DBFFF, CC000-CFFFF, or C8000-CBFFF, again configured
254 * through jumpers. The available IRQs are 2, 3, 4, or 5, and DRQs 1, 2, or 3,
255 * both soft-configurable (no IRQ/DRQ jumpers).
256 *
257 * Yet another design based on the DP8390 was the 3Com 3C503 EtherLink II,
258 * available sometime in 1988. Unlike Novell and WD, 3Com added a custom
259 * host interface ASIC ("Gate Array") which handles all transfers to and from
260 * the 8Kx8 onboard SRAM. The 3C503 can map the card's local RAM directly
261 * into the host's address space, alternatively software can use either PIO
262 * or 8-bit DMA to transfer data.
263 *
264 * For reasons that are not entirely clear, 3Com decided that the Remote DMA
265 * implementation on the DP3890 (successfully used by the NE1000/NE2000) was
266 * too buggy and the Gate Array essentially duplicates the Remote DMA
267 * functionality, while also adding 8327 style DMA support (like the DP839EB
268 * had) and optional shared RAM.
269 *
270 * Just like the NE1000/NE2000 and WD8003/WD8013, the 3C503 exists in an
271 * 8-bit variant (EtherLink II) and a 16-bit variant (EtherLink II/16),
272 * although both types are called 3C503.
273 *
274 * Since the 3C503 does not require shared RAM to operate, 3Com decided to
275 * use a single memory mapping for both a boot ROM (if present) and shared
276 * RAM. It is possible to boot from the ROM utilizing PIO or DMA for data
277 * transfers, and later switch to shared RAM. However, 3Com needed to add
278 * a hack for warm boot; the Vector Pointer Registers (VPTR0/1/2) contain
279 * a 20-bit address and the Gate Array monitors the ISA bus for a read cycle
280 * to that address. When a read cycle from the VPTR address occurs, the
281 * memory mapping is switched from RAM to ROM. The VPTR registers are meant
282 * to be programmed with the warm boot vector (often F000:FFF0 or FFFF0h).
283 *
284 * Some UNIX 3C503 drivers may require the card to have 3Com's old OUI
285 * of 02:60:8C and refuse to detect the hardware otherwise. Likewise the
286 * 3C503 diagnostics fail if the OUI is not 3Com's.
287 *
288 * The emulation passes 3Com diagnostics with flying colors (3C503.EXE Version
289 * 1.5, dated 11/26/1991).
290 *
291 *
292 * Linux Drivers
293 *
294 * The DP8390 driver (shared by NE1000/NE2000, WD8003/WD8013, and 3C503 drivers)
295 * in Linux has severe bugs in the receive path. The driver clears receive
296 * interrupts *after* going through the receive ring; that causes it to race
297 * against the DP8390 chip and sometimes dismiss receive interrupts without
298 * handling them. The driver also only receives at most 9 packets at a time,
299 * which again can cause already received packets to be "hanging" in the receive
300 * queue without the driver processing them.
301 * In addition, prior to Linux 1.3.47, the driver incorrectly cleared the
302 * overflow warning interrupt after any receive, causing it to potentially
303 * miss overflow interrupts.
304 *
305 * The above bugs cause received packets to be lost or retransmitted by sender,
306 * causing major TCP/IP performance issues when the DP8390 receives packets
307 * very quickly. Other operating systems do not exhibit these bugs.
308 *
309 *
310 * BSD Drivers
311 *
312 * For reasons that are not obvious, BSD drivers have configuration defaults far
313 * off from the hardware defaults. For NE2000 (ne1), it is I/O base 300h and
314 * IRQ 10. For WD8003E (we0), it is I/O base 280h, IRQ 9, memory D0000-D1FFF.
315 * For 3C503 (ec0), it is I/O base 250h, IRQ 9, memory D8000-D9FFF (no DMA).
316 *
317 * The resource assigments are difficult to configure (sometimes impossible on
318 * installation CDs) and the high IRQs may clash with PCI devices.
319 *
320 */
321
322
323/*********************************************************************************************************************************
324* Header Files *
325*********************************************************************************************************************************/
326#define LOG_GROUP LOG_GROUP_DEV_DP8390
327#include <VBox/vmm/pdmdev.h>
328#include <VBox/vmm/pdmnetifs.h>
329#include <VBox/vmm/pgm.h>
330#include <VBox/version.h>
331#include <iprt/asm.h>
332#include <iprt/assert.h>
333#include <iprt/critsect.h>
334#include <iprt/net.h>
335#include <iprt/string.h>
336#include <iprt/time.h>
337#ifdef IN_RING3
338# include <iprt/mem.h>
339# include <iprt/semaphore.h>
340# include <iprt/uuid.h>
341#endif
342
343#include "VBoxDD.h"
344
345
346/*********************************************************************************************************************************
347* Defined Constants And Macros *
348*********************************************************************************************************************************/
349
350#define DPNIC_SAVEDSTATE_VERSION 1
351
352/** Maximum number of times we report a link down to the guest (failure to send frame) */
353#define DPNIC_MAX_LINKDOWN_REPORTED 3
354
355/** Maximum number of times we postpone restoring a link that is temporarily down. */
356#define DPNIC_MAX_LINKRST_POSTPONED 3
357
358/** Maximum frame size we handle */
359#define MAX_FRAME 1536
360
361/* Size of the local RAM. */
362#define DPNIC_MEM_SIZE 16384u
363
364#define DPNIC_MEM_MASK (DPNIC_MEM_SIZE - 1)
365
366/* Although it is a 16-bit adapter, the EtherLink II only supports 8-bit DMA
367 * and therefore DMA channels 1 to 3 are available.
368 */
369#define ELNKII_MIN_VALID_DMA 1
370#define ELNKII_MAX_VALID_DMA 3
371
372/* EtherLink II Gate Array revision. */
373#define ELNKII_GA_REV 1
374
375
376/*********************************************************************************************************************************
377* Structures and Typedefs *
378*********************************************************************************************************************************/
379
380
381/**
382 * Emulated device types.
383 */
384enum DP8390_DEVICE_TYPE
385{
386 DEV_NE1000 = 0, /* Novell NE1000 compatible (8-bit). */
387 DEV_NE2000 = 1, /* Novell NE2000 compatible (16-bit). */
388 DEV_WD8003 = 2, /* Western Digital WD8003 EtherCard Plus compatible (8-bit). */
389 DEV_WD8013 = 3, /* Western Digital WD8013 EtherCard Plus compatible (16-bit). */
390 DEV_3C503 = 4 /* 3Com 3C503 EtherLink II compatible. */
391};
392
393/** WD8003/WD80013 specific register offsets. */
394#define WDR_CTRL1 0 /* Control register 1. */
395#define WDR_ATDET 1 /* 16-bit slot detect. */
396#define WDR_IOBASE 2 /* I/O base register. */
397#define WDR_CTRL2 5 /* Control register 2. */
398#define WDR_JP 6 /* Jumper settings. */
399#define WDR_PROM 8 /* PROM offset in I/O space. */
400
401/** WD8013 Control Register 1. */
402typedef struct WD_CTRL1 {
403 uint8_t A13_18 : 6; /* Shared memory decoding A13-A18. */
404 uint8_t MEME : 1; /* Enable memory access. */
405 uint8_t RESET : 1; /* Reset NIC core. */
406} WD_CTRL1;
407AssertCompile(sizeof(WD_CTRL1) == sizeof(uint8_t));
408
409/** WD8013 Control Register 2. */
410typedef struct WD_CTRL2 {
411 uint8_t A19_23 : 5; /* Shared memory decoding A19-A23. */
412 uint8_t res : 1; /* Reserved. */
413 uint8_t MEMW : 1; /* Memory width (16-bit wide if set). */
414 uint8_t M16 : 1; /* Allow 16-bit host memory cycles if set. */
415} WD_CTRL2;
416AssertCompile(sizeof(WD_CTRL2) == sizeof(uint8_t));
417
418
419/** 3C503 EtherLink II specific register offsets. */
420#define GAR_PSTR 0
421#define GAR_PSPR 1
422#define GAR_DQTR 2
423#define GAR_R_BCFR 3
424#define GAR_R_PCFR 4
425#define GAR_GACFR 5
426#define GAR_GACR 6
427#define GAR_STREG 7
428#define GAR_IDCFR 8
429#define GAR_DAMSB 9
430#define GAR_DALSB 10
431#define GAR_VPTR2 11
432#define GAR_VPTR1 12
433#define GAR_VPTR0 13
434#define GAR_RFMSB 14
435#define GAR_RFLSB 15
436
437/** 3C503 EtherLink II Gate Array registers. */
438
439/** Gate Array DRQ Timer Register. */
440typedef struct EL_DQTR {
441 uint8_t tb : 5; /* Timer bits; should be multiple of 4. */
442 uint8_t res : 3; /* Reserved. */
443} GA_DQTR;
444AssertCompile(sizeof(GA_DQTR) == sizeof(uint8_t));
445
446/** Gate Array Configuration Register. */
447typedef struct EL_GACFR {
448 uint8_t mbs : 3; /* Memory Bank Select. */
449 uint8_t rsel : 1; /* RAM Select. */
450 uint8_t test : 1; /* Makes GA counters run at 10 MHz. */
451 uint8_t ows : 1; /* 0 Wait State for Gate Array. */
452 uint8_t tcm : 1; /* Terminal Count Mask for DMA (block interrupt if set). */
453 uint8_t nim : 1; /* NIC Interrupt Mask (block interrupt if set). */
454} GA_GACFR;
455AssertCompile(sizeof(GA_GACFR) == sizeof(uint8_t));
456
457/** Gate Array Configuration Register. */
458typedef struct EL_GACR {
459 uint8_t rst : 1; /* Hard reset GA/NIC. */
460 uint8_t xsel : 1; /* Transceiver Select. */
461 uint8_t ealo : 1; /* Window low 16 bytes of PROM to I/O space. */
462 uint8_t eahi : 1; /* Window high 16 bytes of PROM to I/O space. */
463 uint8_t share : 1; /* Enable interrupt sharing. */
464 uint8_t dbsel : 1; /* Double Buffer Select for FIFOs. */
465 uint8_t ddir : 1; /* DMA Direction (1=host to adapter). */
466 uint8_t start : 1; /* Start Gate Array DMA. */
467} GA_GACR;
468AssertCompile(sizeof(GA_GACR) == sizeof(uint8_t));
469
470/** Gate Array Status Register. */
471typedef struct EL_STREG {
472 uint8_t rev : 3; /* Gate Array Revision. */
473 uint8_t dip : 1; /* DMA In Progress. */
474 uint8_t dtc : 1; /* DMA Terminal Count. */
475 uint8_t oflw : 1; /* Data Overflow. */
476 uint8_t uflw : 1; /* Data Underflow. */
477 uint8_t dprdy : 1; /* Data Port Ready. */
478} GA_STREG;
479AssertCompile(sizeof(GA_STREG) == sizeof(uint8_t));
480
481/** Gate Array Interrupt/DMA Configuration. */
482typedef struct EL_IDCFR {
483 uint8_t drq1 : 1; /* Enable DRQ 1. */
484 uint8_t drq2 : 1; /* Enable DRQ 2. */
485 uint8_t drq3 : 1; /* Enable DRQ 3. */
486 uint8_t res : 1; /* Unused. */
487 uint8_t irq2 : 1; /* Enable IRQ 2. */
488 uint8_t irq3 : 1; /* Enable IRQ 3. */
489 uint8_t irq4 : 1; /* Enable IRQ 4. */
490 uint8_t irq5 : 1; /* Enable IRQ 5. */
491} GA_IDCFR;
492AssertCompile(sizeof(GA_IDCFR) == sizeof(uint8_t));
493
494/** Current DMA Address. */
495typedef struct EL_CDADR {
496 uint8_t cdadr_lsb; /* Current DMA Address LSB. */
497 uint8_t cdadr_msb; /* Current DMA Address MSB. */
498} GA_CDADR;
499AssertCompile(sizeof(GA_CDADR) == sizeof(uint16_t));
500
501/** 3C503 Gate Array state. */
502typedef struct EL_GA_s {
503 uint8_t PSTR; /* Page Start Register. */
504 uint8_t PSPR; /* Page Stop Register. */
505 union {
506 uint8_t DQTR; /* DRQ Timer Register. */
507 GA_DQTR dqtr;
508 };
509 uint8_t BCFR; /* Base Configuration Register (R/O). */
510 uint8_t PCFR; /* Boot PROM Configuration Register (R/O). */
511 union {
512 uint8_t GACFR;
513 GA_GACFR gacfr; /* Gate Array Configuration Register. */
514 };
515 union {
516 uint8_t GACR; /* Gate Array Control Register. */
517 GA_GACR gacr;
518 };
519 union {
520 uint8_t STREG; /* Gate Array Status Register (R/O). */
521 GA_STREG streg;
522 };
523 union {
524 uint8_t IDCFR; /* Interrupt/DMA Configuration Register. */
525 GA_IDCFR idcfr;
526 };
527 uint8_t DAMSB; /* DMA Address MSB. */
528 uint8_t DALSB; /* DMA Address LSB. */
529 uint8_t VPTR2; /* Vector Pointer 2. */
530 uint8_t VPTR1; /* Vector Pointer 1. */
531 uint8_t VPTR0; /* Vector Pointer 0. */
532 union {
533 uint16_t CDADR; /* Current DMA address (internal state). */
534 GA_CDADR cdadr;
535 };
536 bool fGaIrq; /* Gate Array IRQ (internal state). */
537} EL_GA, *PEL_GA;
538
539/** DP8390 core register offsets. */
540#define DPR_CR 0
541
542#define DPR_P0_R_CLDA0 1
543#define DPR_P0_W_PSTART 1
544#define DPR_P0_R_CLDA1 2
545#define DPR_P0_W_PSTOP 2
546#define DPR_P0_BNRY 3
547#define DPR_P0_R_TSR 4
548#define DPR_P0_W_TPSR 4
549#define DPR_P0_R_NCR 5
550#define DPR_P0_W_TBCR0 5
551#define DPR_P0_R_FIFO 6
552#define DPR_P0_W_TBCR1 6
553#define DPR_P0_ISR 7
554#define DPR_P0_R_CRDA0 8
555#define DPR_P0_W_RSAR0 8
556#define DPR_P0_R_CRDA1 9
557#define DPR_P0_W_RSAR1 9
558#define DPR_P0_W_RBCR0 10
559#define DPR_P0_W_RBCR1 11
560#define DPR_P0_R_RSR 12
561#define DPR_P0_W_RCR 12
562#define DPR_P0_R_CNTR0 13
563#define DPR_P0_W_TCR 13
564#define DPR_P0_R_CNTR1 14
565#define DPR_P0_W_DCR 14
566#define DPR_P0_R_CNTR2 15
567#define DPR_P0_W_IMR 15
568
569#define DPR_P1_CURR 7
570
571#define DPR_P2_R_PSTART 1
572#define DPR_P2_W_CLDA0 1
573#define DPR_P2_R_PSTOP 2
574#define DPR_P2_W_CLDA1 2
575#define DPR_P2_RNXTPP 3 /* Remote Next Packet Pointer. */
576#define DPR_P2_R_TPSR 4
577#define DPR_P2_LNXTPP 5 /* Local Next Packet Pointer. */
578#define DPR_P2_ADRCU 6 /* Address Counter (Upper). */
579#define DPR_P2_ADRCL 7 /* Address Counter (Lower). */
580#define DPR_P2_R_RCR 12
581#define DPR_P2_R_TCR 13
582#define DPR_P2_R_DCR 14
583#define DPR_P2_R_IMR 15
584
585
586/** DP8390 Packet Header. */
587typedef struct DP_PKT_HDR {
588 uint8_t rcv_stat; /* Receive Status. */
589 uint8_t next_ptr; /* Next Packet Pointer. */
590 uint16_t byte_cnt; /* Receive byte count. */
591} DP_PKT_HDR;
592
593/** Select values for CR.RD field. */
594#define DP_CR_RDMA_INVL 0 /* Invalid value. */
595#define DP_CR_RDMA_RD 1 /* Remote Read. */
596#define DP_CR_RDMA_WR 2 /* Remote Write. */
597#define DP_CR_RDMA_SP 3 /* Send Packet. */
598#define DP_CR_RDMA_ABRT 4 /* Abort Remote DMA. */
599
600/** DP8390 Command Register (CR). */
601typedef struct DP_CR {
602 uint8_t STP : 1; /* Stop. */
603 uint8_t STA : 1; /* Start. */
604 uint8_t TXP : 1; /* Transmit Packet. */
605 uint8_t RD : 3; /* Remote DMA Command. */
606 uint8_t PS : 2; /* Page Select. */
607} DP_CR;
608AssertCompile(sizeof(DP_CR) == sizeof(uint8_t));
609
610/** DP8390 Interrupt Status Register (ISR). */
611typedef struct DP_ISR {
612 uint8_t PRX : 1; /* Packet Received. */
613 uint8_t PTX : 1; /* Packet Transmitted. */
614 uint8_t RXE : 1; /* Receive Error. */
615 uint8_t TXE : 1; /* Transmit Error. */
616 uint8_t OVW : 1; /* Overwrite Warning (no receive buffers). */
617 uint8_t CNT : 1; /* Counter Overflow. */
618 uint8_t RDC : 1; /* Remote DMA Complete. */
619 uint8_t RST : 1; /* Reset Status. */
620} DP_ISR;
621AssertCompile(sizeof(DP_ISR) == sizeof(uint8_t));
622
623/** DP8390 Interrupt Mask Register (IMR). */
624typedef struct DP_IMR {
625 uint8_t PRXE : 1; /* Packet Received Interrupt Enable. */
626 uint8_t PTXE : 1; /* Packet Transmitted Interrupt Enable. */
627 uint8_t RXEE : 1; /* Receive Error Interrupt Enable. */
628 uint8_t TXEE : 1; /* Transmit Error Interrupt Enable. */
629 uint8_t OVWE : 1; /* Overwrite Warning Interrupt Enable. */
630 uint8_t CNTE : 1; /* Counter Overflow Interrupt Enable. */
631 uint8_t RDCE : 1; /* DMA Complete Interrupt Enable. */
632 uint8_t res : 1; /* Reserved. */
633} DP_IMR;
634AssertCompile(sizeof(DP_IMR) == sizeof(uint8_t));
635
636/** DP8390 Data Configuration Register (DCR). */
637typedef struct DP_DCR {
638 uint8_t WTS : 1; /* Word Transfer Select. */
639 uint8_t BOS : 1; /* Byte Order Select. */
640 uint8_t LAS : 1; /* Long Address Select. */
641 uint8_t LS : 1; /* Loopback Select. */
642 uint8_t ARM : 1; /* Auto-Initialize Remote. */
643 uint8_t FT : 2; /* Fifo Threshold Select. */
644 uint8_t res : 1; /* Reserved. */
645} DP_DCR;
646AssertCompile(sizeof(DP_DCR) == sizeof(uint8_t));
647
648/** Transmit Configuration Register (TCR). */
649typedef struct DP_TCR {
650 uint8_t CRC : 1; /* Inhibit CRC. */
651 uint8_t LB : 2; /* Loopback Control. */
652 uint8_t ATD : 1; /* Auto Transmit Disable. */
653 uint8_t OFST : 1; /* Collision Offset Enable. */
654 uint8_t res : 3; /* Reserved. */
655} DP_TCR;
656AssertCompile(sizeof(DP_TCR) == sizeof(uint8_t));
657
658/** Transmit Status Register (TSR). */
659typedef struct DP_TSR {
660 uint8_t PTX : 1; /* Packet Transmitted. */
661 uint8_t DFR : 1; /* Non-Deferred Transmission (reserved in DP83901A). */
662 uint8_t COL : 1; /* Transmit Collided. */
663 uint8_t ABT : 1; /* Transmit Aborted. */
664 uint8_t CRS : 1; /* Carrier Sense Lost. */
665 uint8_t FU : 1; /* FIFO Underrun. */
666 uint8_t CDH : 1; /* CD Heartbeat. */
667 uint8_t OWC : 1; /* Out of Window Collision. */
668} DP_TSR;
669AssertCompile(sizeof(DP_TSR) == sizeof(uint8_t));
670
671/** Receive Configuration Register (RCR). */
672typedef struct DP_RCR {
673 uint8_t SEP : 1; /* Save Errored Packets. */
674 uint8_t AR : 1; /* Accept Runt Packets. */
675 uint8_t AB : 1; /* Accept Broadcast. */
676 uint8_t AM : 1; /* Accept Multicast. */
677 uint8_t PRO : 1; /* Promiscuous Physical. */
678 uint8_t MON : 1; /* Monitor Mode. */
679 uint8_t res : 2; /* Reserved. */
680} DP_RCR;
681AssertCompile(sizeof(DP_RCR) == sizeof(uint8_t));
682
683/** Receive Status Register (RSR). */
684typedef struct DP_RSR {
685 uint8_t PRX : 1; /* Packet Received Intact. */
686 uint8_t CRC : 1; /* CRC Error. */
687 uint8_t FAE : 1; /* Frame Alignment Error. */
688 uint8_t FO : 1; /* FIFO Overrun. */
689 uint8_t MPA : 1; /* Missed Packet. */
690 uint8_t PHY : 1; /* Physical/Multicast Address. */
691 uint8_t DIS : 1; /* Receiver Disabled. */
692 uint8_t DFR : 1; /* Deferring. */
693} DP_RSR;
694AssertCompile(sizeof(DP_RSR) == sizeof(uint8_t));
695
696/** Transmit Byte Count Register. */
697typedef struct DP_TBCR {
698 uint8_t TBCR0;
699 uint8_t TBCR1;
700} DP_TBCR;
701AssertCompile(sizeof(DP_TBCR) == sizeof(uint16_t));
702
703/** Current Local DMA Address. */
704typedef struct DP_CLDA {
705 uint8_t CLDA0;
706 uint8_t CLDA1;
707} DP_CLDA;
708AssertCompile(sizeof(DP_CLDA) == sizeof(uint16_t));
709
710/** Remote Start Address Register. */
711typedef struct DP_RSAR {
712 uint8_t RSAR0;
713 uint8_t RSAR1;
714} DP_RSAR;
715AssertCompile(sizeof(DP_RSAR) == sizeof(uint16_t));
716
717/** Remote Byte Count Register. */
718typedef struct DP_RBCR {
719 uint8_t RBCR0;
720 uint8_t RBCR1;
721} DP_RBCR;
722AssertCompile(sizeof(DP_RBCR) == sizeof(uint16_t));
723
724/** Current Remote DMA Address. */
725typedef struct DP_CRDA {
726 uint8_t CRDA0;
727 uint8_t CRDA1;
728} DP_CRDA;
729AssertCompile(sizeof(DP_CRDA) == sizeof(uint16_t));
730
731/** Page 1 registers. */
732/* All registers read/write without side effects, unlike pages 0/2. */
733typedef struct DP_PG1 {
734 uint8_t dummy_cr;
735 uint8_t PAR[6]; /* Physical Address PAR0-PAR5. */
736 uint8_t dummy_curr; /* Current Page Register. */
737 uint8_t MAR[8]; /* Multicast Address Register MAR0-MAR7. */
738} DP_PG1;
739AssertCompile(sizeof(DP_PG1) == 16);
740
741/** DP8390 FIFO. Not all of the state is explicitly accessible. */
742typedef struct DP_FIFO {
743 uint8_t rp; /* Read pointer. */
744 uint8_t wp; /* Write pointer. */
745 uint8_t fifo[16]; /* 16 bytes of FIFO. */
746} DP_FIFO;
747
748/**
749 * Core DP8390 chip state.
750 */
751typedef struct DP8390CORE
752{
753 union {
754 uint8_t CR; /* Command Register. */
755 DP_CR cr;
756 };
757 union {
758 uint8_t DCR; /* Data Control Register. */
759 DP_DCR dcr;
760 };
761 /* Interrupt control. */
762 union {
763 uint8_t ISR; /* Interrupt Status Register. */
764 DP_ISR isr;
765 };
766 union {
767 uint8_t IMR; /* Interrupt Mask Register. */
768 DP_IMR imr;
769 };
770 /* Receive state. */
771 union {
772 uint8_t RCR; /* Receive Control Register. */
773 DP_RCR rcr;
774 };
775 union {
776 uint8_t RSR; /* Receive Status register. */
777 DP_RSR rsr;
778 };
779 /* Transmit State. */
780 union {
781 uint8_t TCR; /* Transmit Control Register. */
782 DP_TCR tcr;
783 };
784 union {
785 uint8_t TSR; /* Transmit Status register. */
786 DP_TSR tsr;
787 };
788 uint8_t NCR; /* Number of Collisions Register. */
789 /* Local DMA transmit state. */
790 uint8_t TPSR; /* Transmit Page Start. */
791 union {
792 uint16_t TBCR; /* Transmit Byte Count. */
793 DP_TBCR tbcr;
794 };
795 /* Local DMA receive state. */
796 union {
797 uint16_t CLDA; /* Current Local DMA Address. */
798 DP_CLDA clda;
799 };
800 uint8_t PSTART; /* Page Start. */
801 uint8_t PSTOP; /* Page Stop. */
802 uint8_t CURR; /* Current Page. */
803 uint8_t BNRY; /* Boundary Page. Also spelled BNDRY. */
804 /* Remote DMA state. */
805 union {
806 uint16_t RSAR; /* Remote Start Address Register. */
807 DP_RSAR rsar;
808 };
809 union {
810 uint16_t RBCR; /* Remote Byte Count Register. */
811 DP_RBCR rbcr;
812 };
813 union {
814 uint16_t CRDA; /* Current Remote DMA Address. */
815 DP_CRDA crda;
816 };
817 /* Miscellaneous state. */
818 uint8_t lnxtpp; /* Local Next Packet Pointer. */
819 uint8_t rnxtpp; /* Remote Next Packet Pointer. */
820 /* Tally counters. */
821 uint8_t CNTR0; /* Frame Alignment Errors. */
822 uint8_t CNTR1; /* CRC Errors. */
823 uint8_t CNTR2; /* Missed Packet Errors. */
824 union {
825 uint8_t PG1[sizeof(DP_PG1)];
826 DP_PG1 pg1; /* All Page 1 Registers. */
827 };
828 DP_FIFO fifo; /* The internal FIFO. */
829} DP8390CORE, *PDP8390CORE;
830
831/**
832 * DP8390-based card state.
833 */
834typedef struct DPNICSTATE
835{
836 /** Restore timer.
837 * This is used to disconnect and reconnect the link after a restore. */
838 TMTIMERHANDLE hTimerRestore;
839
840 /** Transmit signaller. */
841 PDMTASKHANDLE hXmitTask;
842 /** Receive ready signaller. */
843 PDMTASKHANDLE hCanRxTask;
844
845 /** Emulated device type. */
846 uint8_t uDevType;
847 /** State of the card's interrupt request signal. */
848 bool fNicIrqActive;
849
850 /** Core DP8390 chip state. */
851 DP8390CORE core;
852
853 /** WD80x3 Control Register 1. */
854 union {
855 uint8_t CTRL1;
856 WD_CTRL1 ctrl1;
857 };
858 /** WD80x3 Control Register 2. */
859 union {
860 uint8_t CTRL2;
861 WD_CTRL2 ctrl2;
862 };
863
864 /** 3C503 Gate Array state. */
865 EL_GA ga;
866 /** The 3C503 soft-configured ISA DMA channel. */
867 uint8_t uElIsaDma;
868
869 /** The PROM contents. 32 bytes addressable, R/O. */
870 uint8_t aPROM[32];
871
872 /** Shared RAM base. */
873 RTGCPHYS MemBase;
874 /** Shared RAM MMIO region handle. */
875 PGMMMIO2HANDLE hSharedMem;
876 /** Shared RAM size. */
877 RTGCPHYS cbMemSize;
878
879 /** Base port of the I/O space region. */
880 RTIOPORT IOPortBase;
881 /** The configured ISA IRQ. */
882 uint8_t uIsaIrq;
883 /** The configured ISA DMA channel. */
884 uint8_t uIsaDma;
885 /** If set the link is currently up. */
886 bool fLinkUp;
887 /** If set the link is temporarily down because of a saved state load. */
888 bool fLinkTempDown;
889 /** Number of times we've reported the link down. */
890 uint16_t cLinkDownReported;
891 /** Number of times we've postponed the link restore. */
892 uint16_t cLinkRestorePostponed;
893
894 /** The "hardware" MAC address. */
895 RTMAC MacConfigured;
896
897 /** Set if DPNICSTATER3::pDrv is not NULL. */
898 bool fDriverAttached;
899 /** The LED. */
900 PDMLED Led;
901 /** Status LUN: The LED ports. */
902 PDMILEDPORTS ILeds;
903 /** Partner of ILeds. */
904 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
905
906 /** Access critical section. */
907 PDMCRITSECT CritSect;
908 /** Event semaphore for blocking on receive. */
909 RTSEMEVENT hEventOutOfRxSpace;
910 /** We are waiting/about to start waiting for more receive buffers. */
911 bool volatile fMaybeOutOfSpace;
912
913 /* MS to wait before we enable the link. */
914 uint32_t cMsLinkUpDelay;
915 /** The device instance number (for logging). */
916 uint32_t iInstance;
917
918 STAMCOUNTER StatReceiveBytes;
919 STAMCOUNTER StatTransmitBytes;
920#ifdef VBOX_WITH_STATISTICS
921 STAMPROFILEADV StatIOReadRZ;
922 STAMPROFILEADV StatIOReadR3;
923 STAMPROFILEADV StatIOWriteRZ;
924 STAMPROFILEADV StatIOWriteR3;
925 STAMPROFILEADV StatReceive;
926 STAMPROFILEADV StatTransmitR3;
927 STAMPROFILEADV StatTransmitRZ;
928 STAMPROFILE StatTransmitSendR3;
929 STAMPROFILE StatTransmitSendRZ;
930 STAMPROFILE StatRxOverflow;
931 STAMCOUNTER StatRxOverflowWakeup;
932 STAMCOUNTER StatRxCanReceiveNow;
933 STAMCOUNTER StatRxCannotReceiveNow;
934 STAMPROFILEADV StatInterrupt;
935 STAMCOUNTER StatDropPktMonitor;
936 STAMCOUNTER StatDropPktRcvrDis;
937 STAMCOUNTER StatDropPktVeryShort;
938 STAMCOUNTER StatDropPktVMNotRunning;
939 STAMCOUNTER StatDropPktNoLink;
940 STAMCOUNTER StatDropPktNoMatch;
941 STAMCOUNTER StatDropPktNoBuffer;
942#endif /* VBOX_WITH_STATISTICS */
943
944 /** NIC-specific ISA I/O ports. */
945 IOMIOPORTHANDLE hIoPortsNic;
946
947 /** Common DP8390 core I/O ports. */
948 IOMIOPORTHANDLE hIoPortsCore;
949
950 /** The runt pad buffer (only really needs 60 bytes). */
951 uint8_t abRuntBuf[64];
952
953 /** The packet buffer. */
954 uint8_t abLocalRAM[DPNIC_MEM_SIZE];
955
956 /** The loopback transmit buffer (avoid stack allocations). */
957 uint8_t abLoopBuf[DPNIC_MEM_SIZE]; /// @todo Can this be smaller?
958} DPNICSTATE, *PDPNICSTATE;
959
960
961/**
962 * DP8390 state for ring-3.
963 *
964 * @implements PDMIBASE
965 * @implements PDMINETWORKDOWN
966 * @implements PDMINETWORKCONFIG
967 * @implements PDMILEDPORTS
968 */
969typedef struct DPNICSTATER3
970{
971 /** Pointer to the device instance. */
972 PPDMDEVINSR3 pDevIns;
973 /** Pointer to the connector of the attached network driver. */
974 PPDMINETWORKUPR3 pDrv;
975 /** Pointer to the attached network driver. */
976 R3PTRTYPE(PPDMIBASE) pDrvBase;
977 /** LUN\#0 + status LUN: The base interface. */
978 PDMIBASE IBase;
979 /** LUN\#0: The network port interface. */
980 PDMINETWORKDOWN INetworkDown;
981 /** LUN\#0: The network config port interface. */
982 PDMINETWORKCONFIG INetworkConfig;
983
984 /** Status LUN: The LED ports. */
985 PDMILEDPORTS ILeds;
986 /** Partner of ILeds. */
987 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
988} DPNICSTATER3;
989/** Pointer to a DP8390 state structure for ring-3. */
990typedef DPNICSTATER3 *PDPNICSTATER3;
991
992
993/**
994 * DP8390 state for ring-0.
995 */
996typedef struct DPNICSTATER0
997{
998 /** Pointer to the connector of the attached network driver. */
999 PPDMINETWORKUPR0 pDrv;
1000} DPNICSTATER0;
1001/** Pointer to a DP8390 state structure for ring-0. */
1002typedef DPNICSTATER0 *PDPNICSTATER0;
1003
1004
1005/**
1006 * DP8390 state for raw-mode.
1007 */
1008typedef struct DPNICSTATERC
1009{
1010 /** Pointer to the connector of the attached network driver. */
1011 PPDMINETWORKUPRC pDrv;
1012} DPNICSTATERC;
1013/** Pointer to a DP8390 state structure for raw-mode. */
1014typedef DPNICSTATERC *PDPNICSTATERC;
1015
1016
1017/** The DP8390 state structure for the current context. */
1018typedef CTX_SUFF(DPNICSTATE) DPNICSTATECC;
1019/** Pointer to a DP8390 state structure for the current
1020 * context. */
1021typedef CTX_SUFF(PDPNICSTATE) PDPNICSTATECC;
1022
1023
1024#ifndef VBOX_DEVICE_STRUCT_TESTCASE
1025
1026
1027/*********************************************************************************************************************************
1028* Internal Functions *
1029*********************************************************************************************************************************/
1030
1031static int dp8390CoreAsyncXmitLocked(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PDPNICSTATECC pThisCC, bool fOnWorkerThread);
1032
1033/**
1034 * Checks if the link is up.
1035 * @returns true if the link is up.
1036 * @returns false if the link is down.
1037 */
1038DECLINLINE(bool) dp8390IsLinkUp(PDPNICSTATE pThis)
1039{
1040 return pThis->fDriverAttached && !pThis->fLinkTempDown && pThis->fLinkUp;
1041}
1042
1043
1044/* Table and macro borrowed from DevPCNet.cpp. */
1045#define CRC(crc, ch) (crc = (crc >> 8) ^ crctab[(crc ^ (ch)) & 0xff])
1046
1047/* generated using the AUTODIN II polynomial
1048 * x^32 + x^26 + x^23 + x^22 + x^16 +
1049 * x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x^1 + 1
1050 */
1051static const uint32_t crctab[256] =
1052{
1053 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
1054 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
1055 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
1056 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
1057 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
1058 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
1059 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
1060 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
1061 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
1062 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
1063 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
1064 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
1065 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
1066 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
1067 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
1068 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
1069 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
1070 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
1071 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
1072 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
1073 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
1074 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
1075 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
1076 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
1077 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
1078 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
1079 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
1080 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
1081 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
1082 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
1083 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
1084 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
1085 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
1086 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
1087 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
1088 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
1089 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
1090 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
1091 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
1092 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
1093 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
1094 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
1095 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
1096 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
1097 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
1098 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
1099 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
1100 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
1101 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
1102 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
1103 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
1104 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
1105 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
1106 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
1107 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
1108 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
1109 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
1110 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
1111 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
1112 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
1113 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
1114 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
1115 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
1116 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
1117};
1118
1119
1120#ifndef ETHER_IS_MULTICAST /* Net/Open BSD macro it seems */
1121#define ETHER_IS_MULTICAST(a) ((*(uint8_t *)(a)) & 1)
1122#endif
1123
1124
1125/**
1126 * Check if incoming frame matches the station address.
1127 */
1128DECLINLINE(int) padr_match(PDPNICSTATE pThis, const uint8_t *buf)
1129{
1130 RTNETETHERHDR *hdr = (RTNETETHERHDR *)buf;
1131 int result;
1132
1133 /* Checks own address only; always enabled if receiver on. */
1134 result = !memcmp(hdr->DstMac.au8, pThis->core.pg1.PAR, 6);
1135
1136 return result;
1137}
1138
1139
1140/**
1141 * Check if incoming frame is an accepted broadcast frame.
1142 */
1143DECLINLINE(int) padr_bcast(PDPNICSTATE pThis, const uint8_t *buf)
1144{
1145 static uint8_t aBCAST[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1146 RTNETETHERHDR *hdr = (RTNETETHERHDR *)buf;
1147 int result = pThis->core.rcr.AB && !memcmp(hdr->DstMac.au8, aBCAST, 6);
1148 return result;
1149}
1150
1151
1152/**
1153 * Check if incoming frame is an accepted multicast frame.
1154 */
1155DECLINLINE(int) padr_mcast(PDPNICSTATE pThis, const uint8_t *buf, int *mcast_type)
1156{
1157 uint32_t crc = UINT32_MAX;
1158 RTNETETHERHDR *hdr = (RTNETETHERHDR *)buf;
1159 int result = 0;
1160
1161 /* If multicast addresses are enabled, and the destination
1162 * address is in fact multicast, the address must be run through
1163 * the CRC generator and matched against the multicast filter
1164 * array.
1165 */
1166 if (pThis->core.rcr.AM && ETHER_IS_MULTICAST(hdr->DstMac.au8))
1167 {
1168 unsigned i;
1169 const uint8_t *p = buf;
1170 unsigned crc_frag, crc_rev;
1171 unsigned ma_bit_mask, ma_byte_idx;
1172
1173 /* Indicate to caller that the address is a multicast one, regardless
1174 * of whether it's accepted or not.
1175 */
1176 *mcast_type = 1;
1177
1178 for (i = 0; i < sizeof(hdr->DstMac); ++i)
1179 CRC(crc, *p++);
1180
1181 /* The top 6 bits of the CRC calculated from the destination address
1182 * becomes an index into the 64-bit multicast address register. Sadly
1183 * our CRC algorithm is bit-reversed (Ethernet shifts bits out MSB first)
1184 * so instead of the top 6 bits of the CRC we have to take the bottom 6
1185 * and reverse the bits.
1186 */
1187 crc_frag = crc & 63;
1188
1189 for (i = 0, crc_rev = 0; i < 6; ++i)
1190 crc_rev |= ((crc_frag >> i) & 1) * (0x20 >> i);
1191
1192 ma_bit_mask = 1 << (crc_rev & 7);
1193 ma_byte_idx = crc_rev / 8;
1194 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));
1195 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]));
1196
1197 /* The multicast filtering logic is fairly extensively
1198 * verified by EtherLink II diagnostics (3C503.EXE).
1199 */
1200 if (pThis->core.pg1.MAR[ma_byte_idx] & ma_bit_mask)
1201 {
1202 Log3Func(("Passed multicast filter\n"));
1203 result = 1;
1204 }
1205 }
1206
1207 return result;
1208}
1209
1210
1211/**
1212 * Check if incoming frame is an accepted promiscuous frame.
1213 */
1214DECLINLINE(int) padr_promi(PDPNICSTATE pThis, const uint8_t *buf)
1215{
1216 RTNETETHERHDR *hdr = (RTNETETHERHDR *)buf;
1217 int result = pThis->core.rcr.PRO && !ETHER_IS_MULTICAST(hdr->DstMac.au8);
1218 return result;
1219}
1220
1221
1222/**
1223 * Update the device IRQ line based on internal state.
1224 */
1225static void dp8390CoreUpdateIrq(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
1226{
1227 bool fCoreIrqActive = false;
1228 bool fNicIrqActive = false;
1229
1230 STAM_PROFILE_ADV_START(&pThis->StatInterrupt, a);
1231
1232 /* Set the ISR.CNT bit based on the counter state (top counter bits ANDed together). */
1233 pThis->core.isr.CNT = (pThis->core.CNTR0 & pThis->core.CNTR1 & pThis->core.CNTR2) >> 7;
1234
1235 /* IRQ is active if a bit is set in ISR and the corresponding bit
1236 * is set in IMR. No additional internal state needed.
1237 */
1238 Assert(!pThis->core.imr.res);
1239 if (pThis->core.ISR & pThis->core.IMR)
1240 fCoreIrqActive = true;
1241
1242 /* The 3C503 has additional interrupt sources and control. For other device
1243 * types, the extras magically work out to be a no-op.
1244 */
1245 pThis->ga.fGaIrq = pThis->ga.streg.dtc && !pThis->ga.gacfr.tcm;
1246 fNicIrqActive = (fCoreIrqActive && !pThis->ga.gacfr.nim) || (pThis->ga.streg.dtc && !pThis->ga.gacfr.tcm);
1247
1248 Log2Func(("#%d set irq fNicIrqActive=%d (fCoreIrqActive=%d, fGaIrq=%d)\n", pThis->iInstance, fNicIrqActive, fCoreIrqActive, pThis->ga.fGaIrq));
1249
1250 /* The IRQ line typically does not change. */
1251 if (RT_UNLIKELY(fNicIrqActive != pThis->fNicIrqActive))
1252 {
1253 LogFunc(("#%d IRQ=%d, state=%d\n", pThis->iInstance, pThis->uIsaIrq, fNicIrqActive));
1254 /// @todo Handle IRQ 2/9 elsewhere
1255 PDMDevHlpISASetIrq(pDevIns, pThis->uIsaIrq == 2 ? 9 : pThis->uIsaIrq, fNicIrqActive);
1256 pThis->fNicIrqActive = fNicIrqActive;
1257 }
1258 STAM_PROFILE_ADV_STOP(&pThis->StatInterrupt, a);
1259}
1260
1261
1262/**
1263 * Perform a software reset of the NIC.
1264 */
1265static void dp8390CoreReset(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
1266{
1267 LogFlowFunc(("#%d:\n", pThis->iInstance));
1268
1269 /* DP8390 or DP83901A datasheet, section 11.0. */
1270 pThis->core.cr.TXP = 0;
1271 pThis->core.cr.STA = 0;
1272 pThis->core.cr.STP = 1;
1273 pThis->core.cr.RD = DP_CR_RDMA_ABRT;
1274 pThis->core.isr.RST = 1;
1275 pThis->core.IMR = 0;
1276 pThis->core.dcr.LAS = 0;
1277 pThis->core.tcr.LB = 0;
1278
1279 /// @todo Check if this really happens on soft reset
1280 /* Clear the internal FIFO including r/w pointers. */
1281 memset(&pThis->core.fifo, 0, sizeof(pThis->core.fifo));
1282
1283 /* Make sure the IRQ line us updated. */
1284 dp8390CoreUpdateIrq(pDevIns, pThis);
1285}
1286
1287#ifdef IN_RING3
1288
1289static DECLCALLBACK(void) dp8390R3WakeupReceive(PPDMDEVINS pDevIns)
1290{
1291 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
1292 LogFlowFunc(("#%d\n", pThis->iInstance));
1293 STAM_COUNTER_INC(&pThis->StatRxOverflowWakeup);
1294 if (pThis->hEventOutOfRxSpace != NIL_RTSEMEVENT)
1295 RTSemEventSignal(pThis->hEventOutOfRxSpace);
1296}
1297
1298/**
1299 * @callback_method_impl{FNPDMTASKDEV,
1300 * Signal to R3 that NIC is ready to receive a packet.
1301 */
1302static DECLCALLBACK(void) dpNicR3CanRxTaskCallback(PPDMDEVINS pDevIns, void *pvUser)
1303{
1304 RT_NOREF(pvUser);
1305 dp8390R3WakeupReceive(pDevIns);
1306}
1307
1308#endif /* IN_RING3 */
1309
1310/**
1311 * Read up to 256 bytes from a single page of local RAM.
1312 */
1313static void dpLocalRAMReadBuf(PDPNICSTATE pThis, uint16_t addr, unsigned cb, uint8_t *pDst)
1314{
1315 if ((RT_LOBYTE(addr) + cb) > 256)
1316 {
1317 LogFunc(("#%d: addr=%04X, cb=%X, cb!!\n", pThis->iInstance, addr, cb));
1318 cb = 256 - RT_LOBYTE(addr);
1319 }
1320
1321 /* A single page is always either entirely inside or outside local RAM. */
1322 if (pThis->uDevType == DEV_NE1000)
1323 {
1324 /* Only 14 bits of address are decoded. */
1325 addr &= 0x3fff;
1326 if (addr >= 0x2000)
1327 {
1328 /* Local RAM is mapped at 2000h-3FFFh. */
1329 addr -= 0x2000;
1330 memcpy(pDst, &pThis->abLocalRAM[addr], cb);
1331 }
1332 else
1333 LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1334 }
1335 else if (pThis->uDevType == DEV_NE2000)
1336 {
1337 /* Only 15 bits of address are decoded. */
1338 addr &= 0x7fff;
1339 if (addr >= 0x4000)
1340 {
1341 /* Local RAM is mapped at 4000h-7FFFh. */
1342 addr -= 0x4000;
1343 memcpy(pDst, &pThis->abLocalRAM[addr], cb);
1344 }
1345 else
1346 LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1347 }
1348 else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
1349 {
1350 /* Local RAM is mapped starting at address zero. */
1351 addr &= DPNIC_MEM_MASK;
1352 if (addr + cb <= DPNIC_MEM_SIZE)
1353 memcpy(pDst, &pThis->abLocalRAM[addr], cb);
1354 else
1355 LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1356 }
1357 else if (pThis->uDevType == DEV_3C503)
1358 {
1359 /* Only 14 bits of address are decoded. */
1360 /// @todo Is there any internal wrap-around in the 3C503 too?
1361 addr &= 0x3fff;
1362 if (addr >= 0x2000)
1363 {
1364 /* Local RAM is mapped at 2000h-3FFFh. */
1365 addr -= 0x2000;
1366 memcpy(pDst, &pThis->abLocalRAM[addr], cb);
1367 }
1368 else
1369 LogFunc(("#%d: Ignoring read at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1370 }
1371 else
1372 {
1373 Assert(0);
1374 }
1375}
1376
1377
1378#ifdef IN_RING3
1379
1380/**
1381 * Write up to 256 bytes into a single page of local RAM.
1382 */
1383static void dpLocalRAMWriteBuf(PDPNICSTATE pThis, uint16_t addr, unsigned cb, const uint8_t *pSrc)
1384{
1385 if ((RT_LOBYTE(addr) + cb) > 256)
1386 {
1387 LogFunc(("#%d: addr=%04X, cb=%X, cb!!\n", pThis->iInstance, addr, cb));
1388 cb = 256 - RT_LOBYTE(addr);
1389 }
1390
1391 /* A single page is always either entirely inside or outside local RAM. */
1392 if (pThis->uDevType == DEV_NE1000)
1393 {
1394 /* Only 14 bits of address are decoded. */
1395 addr &= 0x3fff;
1396 if (addr >= 0x2000)
1397 {
1398 /* Local RAM is mapped at 2000h-3FFFh. */
1399 addr -= 0x2000;
1400 memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
1401 }
1402 else
1403 LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1404 }
1405 else if (pThis->uDevType == DEV_NE2000)
1406 {
1407 /* Only 14 bits of address are decoded. */
1408 addr &= 0x7fff;
1409 if (addr >= 0x4000)
1410 {
1411 /* Local RAM is mapped at 4000h-7FFFh. */
1412 addr -= 0x4000;
1413 memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
1414 }
1415 else
1416 LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1417 }
1418 else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
1419 {
1420 /* Local RAM is mapped starting at address zero. */
1421 addr &= DPNIC_MEM_MASK;
1422 if (addr + cb <= DPNIC_MEM_SIZE)
1423 memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
1424 else
1425 LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1426 }
1427 else if (pThis->uDevType == DEV_3C503)
1428 {
1429 /* Only 14 bits of address are decoded. */
1430 /// @todo Is there any internal wrap-around in the 3C503 too?
1431 addr &= 0x3fff;
1432 if (addr >= 0x2000)
1433 {
1434 /* Local RAM is mapped at 2000h-3FFFh. */
1435 addr -= 0x2000;
1436 memcpy(&pThis->abLocalRAM[addr], pSrc, cb);
1437 }
1438 else
1439 LogFunc(("#%d: Ignoring write at addr=%04X cb=%u!\n", pThis->iInstance, addr, cb));
1440 }
1441 else
1442 {
1443 Assert(0);
1444 }
1445}
1446
1447
1448/**
1449 * Receive an arbitrarily long buffer into the receive ring starting at CLDA.
1450 * Update RSR, CLDA, and other state in the process.
1451 */
1452static void dp8390CoreReceiveBuf(PDPNICSTATE pThis, DP_RSR *pRsr, const uint8_t *src, unsigned cbLeft, bool fLast)
1453{
1454 LogFlow(("#%d: Initial CURR=%02X00 CLDA=%04X\n", pThis->iInstance, pThis->core.CURR, pThis->core.CLDA));
1455
1456 while (cbLeft)
1457 {
1458 unsigned cbWrite;
1459 unsigned cbPage;
1460
1461 /* Write at most up to the end of a page. */
1462 cbPage = cbWrite = 256 - pThis->core.clda.CLDA0;
1463 if (cbWrite > cbLeft)
1464 cbWrite = cbLeft;
1465 Log2Func(("#%d: cbLeft=%d CURR=%02X00 CLDA=%04X\n", pThis->iInstance, cbLeft, pThis->core.CURR, pThis->core.CLDA));
1466 dpLocalRAMWriteBuf(pThis, pThis->core.CLDA, cbWrite, src);
1467 src += cbWrite;
1468
1469 /* If this is the last fragment of a received frame, we need to
1470 * round CLDA up to the next page boundary to correctly evaluate
1471 * buffer overflows and the next pointer. Otherwise we just
1472 * add however much data we had so that we can continue writing
1473 * at the CLDA position.
1474 */
1475 if (fLast && (cbWrite == cbLeft))
1476 {
1477 Log3Func(("#%d: Round up: CLDA=%04X cbPage=%X\n", pThis->iInstance, pThis->core.CLDA, cbPage));
1478 pThis->core.CLDA += cbPage;
1479 }
1480 else
1481 pThis->core.CLDA += cbWrite;
1482
1483 Log3Func(("#%d: Final CURR=%02X00 CLDA=%04X\n", pThis->iInstance, pThis->core.CURR, pThis->core.CLDA));
1484 /* If at end of ring, wrap around. */
1485 if (pThis->core.clda.CLDA1 == pThis->core.PSTOP)
1486 pThis->core.clda.CLDA1 = pThis->core.PSTART;
1487
1488 /* Check for buffer overflow. */
1489 if (pThis->core.clda.CLDA1 == pThis->core.BNRY)
1490 {
1491 pThis->core.isr.OVW = 1;
1492 pThis->core.isr.RST = 1;
1493 pRsr->MPA = 1; /* Indicates to caller that receive was aborted. */
1494 STAM_COUNTER_INC(&pThis->StatDropPktNoBuffer);
1495 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));
1496 break;
1497 }
1498 cbLeft -= cbWrite;
1499 }
1500}
1501
1502/**
1503 * Write incoming data into the packet buffer.
1504 */
1505static void dp8390CoreReceiveLocked(PPDMDEVINS pDevIns, PDPNICSTATE pThis, const uint8_t *src, size_t cbToRecv)
1506{
1507 int is_padr = 0, is_bcast = 0, is_mcast = 0, is_prom = 0;
1508 int mc_type = 0;
1509
1510 /*
1511 * Drop all packets if the VM is not running yet/anymore.
1512 */
1513 VMSTATE enmVMState = PDMDevHlpVMState(pDevIns);
1514 if ( enmVMState != VMSTATE_RUNNING
1515 && enmVMState != VMSTATE_RUNNING_LS)
1516 {
1517 STAM_COUNTER_INC(&pThis->StatDropPktVMNotRunning);
1518 return;
1519 }
1520
1521 /*
1522 * Drop all packets if the cable is not connected.
1523 */
1524 if (RT_UNLIKELY(!dp8390IsLinkUp(pThis)))
1525 {
1526 STAM_COUNTER_INC(&pThis->StatDropPktNoLink);
1527 return;
1528 }
1529
1530 /*
1531 * Drop everything if NIC is not started or in reset.
1532 */
1533 if (RT_UNLIKELY(!pThis->core.cr.STA || pThis->core.cr.STP))
1534 {
1535 STAM_COUNTER_INC(&pThis->StatDropPktRcvrDis);
1536 return;
1537 }
1538
1539 /* Drop impossibly short packets. The DP8390 requires a packet to have
1540 * at least 8 bytes to even qualify as a runt. We can also assume that
1541 * there is a complete destination address at that point.
1542 */
1543 if (RT_UNLIKELY(cbToRecv < 8))
1544 {
1545 STAM_COUNTER_INC(&pThis->StatDropPktVeryShort);
1546 return;
1547 }
1548
1549 LogFlowFunc(("#%d: size on wire=%d\n", pThis->iInstance, cbToRecv));
1550
1551 /*
1552 * Perform address matching. Packets which do not pass any address
1553 * matching logic are ignored.
1554 */
1555 if ( (is_padr = padr_match(pThis, src))
1556 || (is_bcast = padr_bcast(pThis, src))
1557 || (is_mcast = padr_mcast(pThis, src, &mc_type))
1558 || (is_prom = padr_promi(pThis, src)))
1559 {
1560 union {
1561 uint8_t nRSR;
1562 DP_RSR nRsr;
1563 };
1564 uint32_t fcs = 0;
1565
1566 nRSR = 0;
1567 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));
1568
1569 if (is_bcast || mc_type)
1570 nRsr.PHY = 1;
1571
1572 /* In Monitor Mode, just increment the tally counter. */
1573 if (RT_UNLIKELY(pThis->core.rcr.MON))
1574 {
1575 STAM_COUNTER_INC(&pThis->StatDropPktMonitor);
1576 nRsr.MPA = 1;
1577 if (pThis->core.CNTR2 <= 192)
1578 pThis->core.CNTR2++; /* Relies on UpdateIrq to be run. */
1579 }
1580 else
1581 {
1582 /* Error detection: FCS and frame alignment errors cannot happen,
1583 * likewise FIFO overruns can't.
1584 * Runts are padded up to the required minimum. Note that the DP8390
1585 * documentation considers packets smaller than 64 bytes to be runts,
1586 * but that includes 32 bits of FCS.
1587 */
1588
1589 /* See if we need to pad, and how much. Note that if there's any
1590 * room left in the receive buffers, a runt will fit even after padding.
1591 */
1592 if (RT_UNLIKELY(cbToRecv < 60))
1593 {
1594 /// @todo This really is kind of stupid. We shouldn't be doing any
1595 /// padding here, it should be done by the sending side!
1596 memset(pThis->abRuntBuf, 0, sizeof(pThis->abRuntBuf));
1597 memcpy(pThis->abRuntBuf, src, cbToRecv);
1598 cbToRecv = 60;
1599 src = pThis->abRuntBuf;
1600 }
1601
1602 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));
1603
1604 /* All packets that passed the address filter are copied to local RAM.
1605 * Since the DP8390 does not know how long the frame is until it detects
1606 * end of frame, it can only detect an out-of-buffer condition after
1607 * filling up all available space. It then reports an error and rewinds
1608 * back to where it was before.
1609 *
1610 * We do not limit the incoming frame size except by available buffer space. /// @todo Except we do??
1611 */
1612
1613 STAM_REL_COUNTER_ADD(&pThis->StatReceiveBytes, cbToRecv);
1614
1615 /* Copy incoming data to the packet buffer. Start by setting CLDA
1616 * to CURR + 4, leaving room for header.
1617 */
1618 pThis->core.CLDA = RT_MAKE_U16(4, pThis->core.CURR);
1619
1620 /* Receive the incoming frame. */
1621 Assert(cbToRecv < MAX_FRAME); /// @todo Can we actually do bigger?
1622 dp8390CoreReceiveBuf(pThis, &nRsr, src, (unsigned)cbToRecv, false);
1623 /// @todo Use the same method for runt padding?
1624
1625 /* If there was no overflow, add the FCS. */
1626 if (!nRsr.MPA)
1627 {
1628 fcs = 0xBADF00D; // Just fake it, does anyone care?
1629 dp8390CoreReceiveBuf(pThis, &nRsr, (uint8_t *)&fcs, sizeof(fcs), true);
1630 }
1631
1632 /* Error-free packets are considered intact. */
1633 if (!nRsr.CRC && !nRsr.FAE && !nRsr.FO && !nRsr.MPA)
1634 {
1635 nRsr.PRX = 1;
1636 pThis->core.isr.PRX = 1;
1637 }
1638 else
1639 pThis->core.isr.RXE = 1;
1640
1641 /* For 'intact' packets, write the packet header. */
1642 if (nRsr.PRX)
1643 {
1644 DP_PKT_HDR header;
1645
1646 /* Round up CLDA to the next page. */
1647 if (pThis->core.clda.CLDA0)
1648 pThis->core.CLDA = RT_MAKE_U16(0, pThis->core.clda.CLDA1 + 1);
1649
1650 /* If entire frame was successfully received, write the packet header at the old CURR. */
1651 header.rcv_stat = nRSR;
1652 header.next_ptr = pThis->core.clda.CLDA1;
1653 /// @todo big endian (WTS)
1654 header.byte_cnt = (uint16_t)cbToRecv + sizeof(fcs);
1655
1656 pThis->core.CLDA = RT_MAKE_U16(0, pThis->core.CURR);
1657 dpLocalRAMWriteBuf(pThis, pThis->core.CLDA, sizeof(header), (uint8_t *)&header);
1658 pThis->core.CLDA += sizeof(header);
1659
1660 pThis->core.CURR = header.next_ptr;
1661 }
1662 }
1663
1664 pThis->core.RSR = nRSR;
1665
1666 Log2Func(("Receive completed, size=%d, CURR=%02X00, RSR=%02X, ISR=%02X\n", cbToRecv, pThis->core.CURR, pThis->core.RSR, pThis->core.ISR));
1667 dp8390CoreUpdateIrq(pDevIns, pThis);
1668 }
1669 else
1670 {
1671 Log3Func(("#%d Packet did not pass address filter, size=%d\n", pThis->iInstance, cbToRecv));
1672 STAM_COUNTER_INC(&pThis->StatDropPktNoMatch);
1673 }
1674}
1675
1676#endif /* IN_RING3 */
1677
1678
1679/**
1680 * Transmit a packet from local memory.
1681 *
1682 * @returns VBox status code. VERR_TRY_AGAIN is returned if we're busy.
1683 *
1684 * @param pDevIns The device instance data.
1685 * @param pThis The device state data.
1686 * @param fOnWorkerThread Whether we're on a worker thread or on an EMT.
1687 */
1688static int dp8390CoreXmitPacket(PPDMDEVINS pDevIns, PDPNICSTATE pThis, bool fOnWorkerThread)
1689{
1690 PDPNICSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
1691 RT_NOREF_PV(fOnWorkerThread);
1692 int rc;
1693
1694 /*
1695 * Grab the xmit lock of the driver as well as the DP8390 device state.
1696 */
1697 PPDMINETWORKUP pDrv = pThisCC->pDrv;
1698 if (pDrv)
1699 {
1700 rc = pDrv->pfnBeginXmit(pDrv, false /*fOnWorkerThread*/);
1701 if (RT_FAILURE(rc))
1702 return rc;
1703 }
1704 rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
1705 if (RT_SUCCESS(rc))
1706 {
1707 /*
1708 * Do the transmitting.
1709 */
1710 int rc2 = dp8390CoreAsyncXmitLocked(pDevIns, pThis, pThisCC, false /*fOnWorkerThread*/);
1711 AssertReleaseRC(rc2);
1712
1713 /*
1714 * Release the locks.
1715 */
1716 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
1717 }
1718 else
1719 AssertLogRelRC(rc);
1720 if (pDrv)
1721 pDrv->pfnEndXmit(pDrv);
1722
1723 return rc;
1724}
1725
1726
1727#ifdef IN_RING3
1728
1729/**
1730 * @callback_method_impl{FNPDMTASKDEV,
1731 * This is just a very simple way of delaying sending to R3.
1732 */
1733static DECLCALLBACK(void) dpNicR3XmitTaskCallback(PPDMDEVINS pDevIns, void *pvUser)
1734{
1735 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
1736 NOREF(pvUser);
1737
1738 /*
1739 * Transmit if we can.
1740 */
1741 dp8390CoreXmitPacket(pDevIns, pThis, true /*fOnWorkerThread*/);
1742}
1743
1744#endif /* IN_RING3 */
1745
1746
1747/**
1748 * Allocates a scatter/gather buffer for a transfer.
1749 *
1750 * @returns See PPDMINETWORKUP::pfnAllocBuf.
1751 * @param pThis The device instance.
1752 * @param pThisCC The device state for current context.
1753 * @param cbMin The minimum buffer size.
1754 * @param fLoopback Set if we're in loopback mode.
1755 * @param pSgLoop Pointer to stack storage for the loopback SG.
1756 * @param ppSgBuf Where to return the SG buffer descriptor on success.
1757 * Always set.
1758 */
1759DECLINLINE(int) dp8390XmitAllocBuf(PDPNICSTATE pThis, PDPNICSTATECC pThisCC, size_t cbMin, bool fLoopback,
1760 PPDMSCATTERGATHER pSgLoop, PPPDMSCATTERGATHER ppSgBuf)
1761{
1762 int rc;
1763
1764 if (!fLoopback)
1765 {
1766 PPDMINETWORKUP pDrv = pThisCC->pDrv;
1767 if (RT_LIKELY(pDrv))
1768 {
1769 rc = pDrv->pfnAllocBuf(pDrv, cbMin, NULL /*pGso*/, ppSgBuf);
1770 AssertMsg(rc == VINF_SUCCESS || rc == VERR_TRY_AGAIN || rc == VERR_NET_DOWN || rc == VERR_NO_MEMORY, ("%Rrc\n", rc));
1771 if (RT_FAILURE(rc))
1772 *ppSgBuf = NULL;
1773 }
1774 else
1775 {
1776 rc = VERR_NET_DOWN;
1777 *ppSgBuf = NULL;
1778 }
1779 }
1780 else
1781 {
1782 /* Fake loopback allocator. */
1783 pSgLoop->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_1;
1784 pSgLoop->cbUsed = 0;
1785 pSgLoop->cbAvailable = sizeof(pThis->abLoopBuf);
1786 pSgLoop->pvAllocator = pThis;
1787 pSgLoop->pvUser = NULL;
1788 pSgLoop->cSegs = 1;
1789 pSgLoop->aSegs[0].cbSeg = sizeof(pThis->abLoopBuf);
1790 pSgLoop->aSegs[0].pvSeg = pThis->abLoopBuf;
1791 *ppSgBuf = pSgLoop;
1792 rc = VINF_SUCCESS;
1793 }
1794 return rc;
1795}
1796
1797
1798/**
1799 * Sends the scatter/gather buffer.
1800 *
1801 * Wrapper around PDMINETWORKUP::pfnSendBuf, so check it out for the fine print.
1802 *
1803 * @returns See PDMINETWORKUP::pfnSendBuf.
1804 * @param pDevIns The device instance.
1805 * @param pThisCC The current context device state.
1806 * @param fLoopback Set if we're in loopback mode.
1807 * @param pSgBuf The SG to send.
1808 * @param fOnWorkerThread Set if we're being called on a work thread. Clear
1809 * if an EMT.
1810 */
1811DECLINLINE(int) dp8390CoreXmitSendBuf(PPDMDEVINS pDevIns, PDPNICSTATECC pThisCC, bool fLoopback, PPDMSCATTERGATHER pSgBuf, bool fOnWorkerThread)
1812{
1813 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
1814 int rc;
1815 STAM_REL_COUNTER_ADD(&pThis->StatTransmitBytes, pSgBuf->cbUsed);
1816 if (!fLoopback)
1817 {
1818 STAM_PROFILE_START(&pThis->CTX_SUFF_Z(StatTransmitSend), a);
1819 if (pSgBuf->cbUsed > 70) /* unqualified guess */
1820 pThis->Led.Asserted.s.fWriting = pThis->Led.Actual.s.fWriting = 1;
1821
1822 PPDMINETWORKUP pDrv = pThisCC->pDrv;
1823 if (RT_LIKELY(pDrv))
1824 {
1825 rc = pDrv->pfnSendBuf(pDrv, pSgBuf, fOnWorkerThread);
1826 AssertMsg(rc == VINF_SUCCESS || rc == VERR_NET_DOWN || rc == VERR_NET_NO_BUFFER_SPACE, ("%Rrc\n", rc));
1827 }
1828 else
1829 rc = VERR_NET_DOWN;
1830
1831 pThis->Led.Actual.s.fWriting = 0;
1832 STAM_PROFILE_STOP(&pThis->CTX_SUFF_Z(StatTransmitSend), a);
1833 }
1834 else
1835 {
1836 PDP8390CORE pCore = &pThis->core;
1837 union {
1838 uint8_t nRSR;
1839 DP_RSR nRsr;
1840 };
1841 unsigned ofs;
1842 uint32_t fcs = UINT32_MAX;
1843
1844 nRSR = 0;
1845
1846 /* Loopback on the DP8390 is so strange that it must be handled specially. */
1847 Assert(pSgBuf->pvAllocator == (void *)pThis);
1848 pThis->Led.Asserted.s.fReading = pThis->Led.Actual.s.fReading = 1;
1849
1850 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));
1851 for (ofs = 0; ofs < pSgBuf->cbUsed; ofs += 16)
1852 Log((" %04X: %.*Rhxs\n", ofs, ofs + 16 < pSgBuf->cbUsed ? 16 : pSgBuf->cbUsed - ofs, &pThis->abLoopBuf[ofs]));
1853
1854 /* A packet shorter than 8 bytes is ignored by the receiving side. */
1855 if (pSgBuf->cbUsed < 8)
1856 return VINF_SUCCESS;
1857
1858 /* The loopback mode affects transmit status bits. */
1859 switch (pCore->tcr.LB)
1860 {
1861 case 1: /* Internal loopback within DP8390. */
1862 pCore->tsr.CDH = 1;
1863 pCore->tsr.CRS = 1;
1864 break;
1865 case 2: /* Loopback through serializer. */
1866 pCore->tsr.CDH = 1;
1867 break;
1868 case 3: /* External loopback. Requires a cable. */
1869 break;
1870 default:
1871 Assert(0);
1872 }
1873
1874 /* The CRC Inhibit controls whether transmit or receive path uses the
1875 * CRC circuitry. If transmit side uses CRC, receive always fails.
1876 * We always need to calculate the FCS because either the sending or
1877 * the receiving side uses it.
1878 */
1879 uint8_t *p;
1880 uint8_t *pktbuf = pThis->abLoopBuf; /// @todo Point into sgbuf instead?
1881 uint16_t pktlen = (uint16_t)pSgBuf->cbUsed;
1882 uint16_t fcslen = pktlen;
1883 uint8_t abFcs[4];
1884 bool fAddrMatched = true;
1885
1886 /* If the receiver side is calculating FCS, it needs to skip the last
1887 * bytes (which are the transmit-side FCS).
1888 */
1889 if (pCore->tcr.CRC && (pktlen > 4))
1890 fcslen -= 4;
1891
1892 p = pktbuf;
1893 while (p != &pktbuf[fcslen])
1894 CRC(fcs, *p++);
1895
1896 fcs = ~fcs;
1897 Log3Func(("FCS: %08X\n", fcs));
1898 for (ofs = 0; ofs < sizeof(abFcs); ++ofs)
1899 {
1900 abFcs[ofs] = (uint8_t)fcs;
1901 fcs >>= 8;
1902 }
1903
1904 /* The FIFO write pointer gets zeroed on each receive,
1905 * but the read pointer does not.
1906 */
1907 pCore->fifo.wp = 0;
1908
1909 if (pCore->tcr.CRC)
1910 {
1911 bool fGoodFcs = true;
1912 int is_padr = 0, is_bcast = 0, is_mcast = 0, is_prom = 0;
1913 int mc_type = 0;
1914
1915 /* Always put the first 8 bytes of the packet in the FIFO. */
1916 for (ofs = 0; ofs < 8; ++ofs)
1917 pCore->fifo.fifo[pCore->fifo.wp++ & 7] = pktbuf[ofs];
1918
1919
1920 /* If the receiving side uses the CRC circuitry, it also performs
1921 * destination address matching.
1922 */
1923 if ( (is_padr = padr_match(pThis, pktbuf))
1924 || (is_bcast = padr_bcast(pThis, pktbuf))
1925 || (is_mcast = padr_mcast(pThis, pktbuf, &mc_type))
1926 || (is_prom = padr_promi(pThis, pktbuf)))
1927 {
1928 /* Receiving side checks the FCS. */
1929 fGoodFcs = !memcmp(&pktbuf[pktlen - 4], abFcs, sizeof(abFcs));
1930 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));
1931
1932 /* Now we have to update the FIFO. Since only 8 bytes are visible
1933 * in the FIFO after a receive, we can skip most of it.
1934 */
1935 for ( ; ofs < pktlen; ++ofs)
1936 pCore->fifo.fifo[pCore->fifo.wp++ & 7] = pktbuf[ofs];
1937
1938 }
1939 else
1940 {
1941 nRsr.PRX = 1; /* Weird but true, for non-matching address only! */
1942 fAddrMatched = false;
1943 Log3Func(("#%d: Address NOT matched, ignoring FCS errors.\n", pThis->iInstance));
1944 }
1945
1946 /* The PHY bit is set when when an enabled broadcast packet is accepted,
1947 * but also when an enabled multicast packet arrives regardless of whether
1948 * it passes the MAR filter or not.
1949 */
1950 if (is_bcast || mc_type)
1951 nRsr.PHY = 1;
1952
1953 if (!fGoodFcs)
1954 nRsr.CRC = 1;
1955 }
1956 else
1957 {
1958 nRsr.CRC = 1; /* Always report CRC error if receiver isn't checking. */
1959
1960 /* Now we have to update the FIFO. Since only 8 bytes are visible
1961 * in the FIFO after a receive, we can skip most of it.
1962 */
1963 for (ofs = 0; ofs < pktlen; ++ofs)
1964 pCore->fifo.fifo[pCore->fifo.wp++ & 7] = pktbuf[ofs];
1965
1966 /* Stuff the generated FCS in the FIFO. */
1967 for (ofs = 0; ofs < sizeof(abFcs); ++ofs)
1968 pCore->fifo.fifo[pCore->fifo.wp++ & 7] = abFcs[ofs];
1969 }
1970
1971 /* And now put the packet length in the FIFO. */
1972 if (fAddrMatched || 1)
1973 {
1974 pCore->fifo.fifo[pCore->fifo.wp++ & 7] = RT_LOBYTE(pktlen);
1975 pCore->fifo.fifo[pCore->fifo.wp++ & 7] = RT_HIBYTE(pktlen);
1976 pCore->fifo.fifo[pCore->fifo.wp++ & 7] = RT_HIBYTE(pktlen); /* Yes, written twice! */
1977 }
1978
1979 Log(("FIFO: rp=%u, wp=%u\n", pCore->fifo.rp & 7, pCore->fifo.wp & 7));
1980 Log((" %Rhxs\n", &pCore->fifo.fifo));
1981
1982 if (nRsr.CRC)
1983 pCore->isr.RXE = 1;
1984 pCore->RSR = nRSR;
1985
1986 pThis->Led.Actual.s.fReading = 0;
1987
1988 /* Return success so that caller sets TSR.PTX and ISR.PTX. */
1989 rc = VINF_SUCCESS;
1990 }
1991 return rc;
1992}
1993
1994
1995/**
1996 * Reads the entire frame into the scatter gather buffer.
1997 */
1998DECLINLINE(void) dp8390CoreXmitRead(PPDMDEVINS pDevIns, const unsigned uLocalAddr, const unsigned cbFrame, PPDMSCATTERGATHER pSgBuf, bool fLoopback)
1999{
2000 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
2001 unsigned uOfs = 0;
2002 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
2003 Assert(pSgBuf->cbAvailable >= cbFrame);
2004
2005 pSgBuf->cbUsed = cbFrame;
2006
2007 LogFlowFunc(("#%d: uLocalAddr=%04X cbFrame=%d\n", pThis->iInstance, uLocalAddr, cbFrame));
2008 /* Have to figure out where the address is in local RAM. */
2009 if (pThis->uDevType == DEV_NE1000)
2010 {
2011 /* Only 14 bits of address are decoded. */
2012 uOfs = uLocalAddr & 0x3fff;
2013 if (uOfs >= 0x2000)
2014 {
2015 /* Local RAM is mapped at 2000h-3FFFh. */
2016 uOfs -= 0x2000;
2017 }
2018 else
2019 {
2020 /// @todo What are we supposed to do?!
2021 LogFunc(("#%d: uOfs=%u, don't know what to do!!\n", pThis->iInstance, uOfs));
2022 }
2023 }
2024 else if (pThis->uDevType == DEV_NE2000)
2025 {
2026 /* Only 15 bits of address are decoded. */
2027 uOfs = uLocalAddr & 0x7fff;
2028 if (uOfs >= 0x4000)
2029 {
2030 /* Local RAM is mapped at 4000h-7FFFh. */
2031 uOfs -= 0x4000;
2032 }
2033 else
2034 {
2035 /// @todo What are we supposed to do?!
2036 LogFunc(("#%d: uOfs=%u, don't know what to do!!\n", pThis->iInstance, uOfs));
2037 }
2038 }
2039 else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
2040 {
2041 /* Not much to do, WD was nice enough to put the RAM at the start of DP8390's address space. */
2042 uOfs = uLocalAddr & DPNIC_MEM_MASK;
2043 }
2044 else if (pThis->uDevType == DEV_3C503)
2045 {
2046 /* Only 14 bits of address are decoded. */
2047 uOfs = uLocalAddr & 0x3fff;
2048 if (uOfs >= 0x2000)
2049 {
2050 /* Local RAM is mapped at 2000h-3FFFh. */
2051 uOfs -= 0x2000;
2052 }
2053 else
2054 {
2055 /// @todo What are we supposed to do?!
2056 LogFunc(("#%d: uOfs=%u, don't know what to do!!\n", pThis->iInstance, uOfs));
2057 }
2058 }
2059 else
2060 {
2061 Assert(0);
2062 }
2063
2064 if (!fLoopback)
2065 {
2066 /* Fast path for normal transmit, ignores DCR.WTS. */
2067 if (uOfs + cbFrame <= sizeof(pThis->abLocalRAM))
2068 memcpy(pSgBuf->aSegs[0].pvSeg, &pThis->abLocalRAM[uOfs], cbFrame);
2069 else
2070 memset(pSgBuf->aSegs[0].pvSeg, 0xEE, cbFrame);
2071 }
2072 else
2073 {
2074 /* If DCR.WTS is set, only every other byte actually goes through loopback. */
2075 const uint8_t *src = &pThis->abLocalRAM[uOfs];
2076 uint8_t *dst = (uint8_t *)pSgBuf->aSegs[0].pvSeg;
2077 int cbDst = cbFrame;
2078 int step = 1 << pThis->core.dcr.WTS;
2079
2080 /* Depending on DCR.BOS, take either odd or even bytes when DCR.WTS is set. */
2081 if (pThis->core.dcr.WTS && !pThis->core.dcr.BOS)
2082 ++src;
2083
2084 while (cbDst-- && (src <= &pThis->abLocalRAM[DPNIC_MEM_SIZE]))
2085 {
2086 *dst++ = *src;
2087 src += step;
2088 }
2089
2090 /* The address should perhaps wrap around -- depends on card design. */
2091 if (cbDst != -1)
2092 {
2093 while (cbDst--)
2094 *dst++ = 0xEE;
2095 }
2096 Assert(cbDst == -1);
2097 }
2098}
2099
2100/**
2101 * Try to transmit a frame.
2102 */
2103static void dp8390CoreStartTransmit(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
2104{
2105 /*
2106 * Transmit the packet if possible, defer it if we cannot do it
2107 * in the current context.
2108 */
2109 pThis->core.TSR = 0; /* Clear transmit status. */
2110 pThis->core.NCR = 0; /* Clear collision counter. */
2111#if defined(IN_RING0) || defined(IN_RC)
2112 PDPNICSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
2113 if (!pThisCC->pDrv)
2114 {
2115 int rc = PDMDevHlpTaskTrigger(pDevIns, pThis->hXmitTask);
2116 AssertRC(rc);
2117 }
2118 else
2119#endif
2120 {
2121 int rc = dp8390CoreXmitPacket(pDevIns, pThis, false /*fOnWorkerThread*/);
2122 if (rc == VERR_TRY_AGAIN)
2123 rc = VINF_SUCCESS;
2124 AssertRC(rc);
2125 }
2126}
2127
2128
2129/**
2130 * If a packet is waiting, poke the receiving machinery.
2131 *
2132 * @threads EMT.
2133 */
2134static void dp8390CoreKickReceive(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
2135{
2136 if (pThis->fMaybeOutOfSpace)
2137 {
2138 LogFlow(("Poking receive thread.\n"));
2139#ifdef IN_RING3
2140 dp8390R3WakeupReceive(pDevIns);
2141#else
2142 int rc = PDMDevHlpTaskTrigger(pDevIns, pThis->hCanRxTask);
2143 AssertRC(rc);
2144#endif
2145 }
2146}
2147
2148/**
2149 * Try transmitting a frame.
2150 *
2151 * @threads TX or EMT.
2152 */
2153static int dp8390CoreAsyncXmitLocked(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PDPNICSTATECC pThisCC, bool fOnWorkerThread)
2154{
2155 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
2156
2157 /*
2158 * Just drop it if not transmitting. Can happen with delayed transmits
2159 * if transmit was disabled in the meantime.
2160 */
2161 if (RT_UNLIKELY(!pThis->core.cr.TXP))
2162 {
2163 LogFunc(("#%d: Nope, CR.TXP is off (fOnWorkerThread=%RTbool)\n", pThis->iInstance, fOnWorkerThread));
2164 return VINF_SUCCESS;
2165 }
2166
2167 /*
2168 * Blast out data from the packet buffer.
2169 */
2170 int rc;
2171 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatTransmit), a);
2172 do
2173 {
2174 /* Don't send anything when the link is down. */
2175 if (RT_UNLIKELY( !dp8390IsLinkUp(pThis)
2176 && pThis->cLinkDownReported > DPNIC_MAX_LINKDOWN_REPORTED)
2177 )
2178 break;
2179
2180 bool const fLoopback = pThis->core.tcr.LB != 0;
2181 PDMSCATTERGATHER SgLoop;
2182 PPDMSCATTERGATHER pSgBuf;
2183
2184 /*
2185 * Sending is easy peasy, there is by definition always
2186 * a complete packet on hand.
2187 */
2188 unsigned cb = pThis->core.TBCR; /* Packet size. */
2189 const int adr = RT_MAKE_U16(0, pThis->core.TPSR);
2190 LogFunc(("#%d: cb=%d, adr=%04X\n", pThis->iInstance, cb, adr));
2191
2192 if (RT_LIKELY(dp8390IsLinkUp(pThis) || fLoopback))
2193 {
2194 if (RT_LIKELY(cb <= MAX_FRAME))
2195 {
2196 /* Loopback fun! */
2197 if (RT_UNLIKELY(fLoopback && pThis->core.dcr.WTS))
2198 {
2199 cb /= 2;
2200 Log(("Loopback with DCR.WTS set -> cb=%d\n", cb));
2201 }
2202
2203 rc = dp8390XmitAllocBuf(pThis, pThisCC, cb, fLoopback, &SgLoop, &pSgBuf);
2204 if (RT_SUCCESS(rc))
2205 {
2206 dp8390CoreXmitRead(pDevIns, adr, cb, pSgBuf, fLoopback);
2207 rc = dp8390CoreXmitSendBuf(pDevIns, pThisCC, fLoopback, pSgBuf, fOnWorkerThread);
2208 Log2Func(("#%d: rc=%Rrc\n", pThis->iInstance, rc));
2209 }
2210 else if (rc == VERR_TRY_AGAIN)
2211 {
2212 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
2213 LogFunc(("#%d: rc=%Rrc\n", pThis->iInstance, rc));
2214 return VINF_SUCCESS;
2215 }
2216 if (RT_SUCCESS(rc))
2217 {
2218 pThis->core.tsr.PTX = 1;
2219 pThis->core.isr.PTX = 1;
2220 }
2221 else
2222 {
2223 pThis->core.tsr.COL = 1; /* Pretend there was a collision. */
2224 pThis->core.isr.TXE = 1;
2225 }
2226 }
2227 else
2228 {
2229 /* Signal error, as this violates the Ethernet specs. Note that the DP8390
2230 * hardware does *not* limit the packet length.
2231 */
2232 LogRel(("DPNIC#%d: Attempt to transmit illegal giant frame (%u bytes) -> signaling error\n", pThis->iInstance, cb));
2233 pThis->core.tsr.OWC = 1; /* Pretend there was an out-of-window collision. */
2234 pThis->core.isr.TXE = 1;
2235 }
2236 }
2237 else
2238 {
2239 /* Signal a transmit error pretending there was a collision. */
2240 pThis->core.tsr.COL = 1;
2241 pThis->core.isr.TXE = 1;
2242 pThis->cLinkDownReported++;
2243 }
2244 /* Transmit officially done, update register state. */
2245 pThis->core.cr.TXP = 0;
2246 pThis->core.TBCR = 0;
2247 LogFlowFunc(("#%d: TSR=%02X, ISR=%02X\n", pThis->iInstance, pThis->core.TSR, pThis->core.ISR));
2248
2249 } while (0); /* No loop, because there isn't ever more than one packet to transmit. */
2250
2251 dp8390CoreUpdateIrq(pDevIns, pThis);
2252
2253 /* If there's anything waiting, this should be a good time to recheck. */
2254 dp8390CoreKickReceive(pDevIns, pThis);
2255
2256 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
2257
2258 return VINF_SUCCESS;
2259}
2260
2261/* -=-=-=-=-=- I/O Port access -=-=-=-=-=- */
2262
2263
2264static uint32_t dp8390CoreRead(PPDMDEVINS pDevIns, PDPNICSTATE pThis, int ofs)
2265{
2266 uint8_t val;
2267
2268 /* The 3C503 can read the PROM instead of the DP8390 registers. */
2269 if (pThis->ga.gacr.ealo)
2270 return pThis->aPROM[ofs % 0xf];
2271 else if (pThis->ga.gacr.eahi)
2272 return pThis->aPROM[16 + (ofs % 0xf)];
2273
2274 /* Command Register exists in all pages. */
2275 if (ofs == DPR_CR)
2276 return pThis->core.CR;
2277
2278 if (pThis->core.cr.PS == 0)
2279 {
2280 switch (ofs)
2281 {
2282 case DPR_P0_R_CLDA0:
2283 return pThis->core.clda.CLDA0;
2284 case DPR_P0_R_CLDA1:
2285 return pThis->core.clda.CLDA1;
2286 case DPR_P0_BNRY:
2287 return pThis->core.BNRY;
2288 case DPR_P0_R_TSR:
2289 return pThis->core.TSR;
2290 case DPR_P0_R_NCR:
2291 return pThis->core.NCR;
2292 case DPR_P0_R_FIFO:
2293 return pThis->core.fifo.fifo[pThis->core.fifo.rp++ & 7]; /// @todo Abstract the mask somehow?
2294 case DPR_P0_ISR:
2295 return pThis->core.ISR;
2296 case DPR_P0_R_CRDA0:
2297 return pThis->core.crda.CRDA0;
2298 case DPR_P0_R_CRDA1:
2299 return pThis->core.crda.CRDA1;
2300 case DPR_P0_R_RSR:
2301 return pThis->core.RSR;
2302 case DPR_P0_R_CNTR0:
2303 val = pThis->core.CNTR0;
2304 pThis->core.CNTR0 = 0; /* Cleared by reading. */
2305 dp8390CoreUpdateIrq(pDevIns, pThis);
2306 return val;
2307 case DPR_P0_R_CNTR1:
2308 val = pThis->core.CNTR1;
2309 pThis->core.CNTR1 = 0; /* Cleared by reading. */
2310 dp8390CoreUpdateIrq(pDevIns, pThis);
2311 return val;
2312 case DPR_P0_R_CNTR2:
2313 val = pThis->core.CNTR2;
2314 pThis->core.CNTR2 = 0; /* Cleared by reading. */
2315 dp8390CoreUpdateIrq(pDevIns, pThis);
2316 return val;
2317 default:
2318 return 0; /// @todo or 0xFF? or something else?
2319 }
2320 }
2321 else if (pThis->core.cr.PS == 1)
2322 {
2323 /* Page 1 is easy, most registers are stored directly. */
2324 if (ofs == DPR_P1_CURR)
2325 return pThis->core.CURR;
2326 else
2327 return pThis->core.PG1[ofs];
2328 }
2329 else if (pThis->core.cr.PS == 2)
2330 {
2331 /* Page 2 is for diagnostics. Reads many registers that
2332 * are write-only in Page 0.
2333 */
2334 switch (ofs)
2335 {
2336 case DPR_P2_R_PSTART:
2337 return pThis->core.PSTART;
2338 case DPR_P2_R_PSTOP:
2339 return pThis->core.PSTOP;
2340 case DPR_P2_RNXTPP:
2341 return pThis->core.rnxtpp;
2342 case DPR_P2_R_TPSR:
2343 return pThis->core.TPSR;
2344 case DPR_P2_LNXTPP:
2345 return pThis->core.lnxtpp;
2346 case DPR_P2_ADRCU:
2347 case DPR_P2_ADRCL:
2348 return 0; /// @todo What's this?
2349 case DPR_P2_R_RCR:
2350 return pThis->core.RCR;
2351 case DPR_P2_R_TCR:
2352 return pThis->core.TCR;
2353 case DPR_P2_R_DCR:
2354 return pThis->core.DCR;
2355 case DPR_P2_R_IMR:
2356 return pThis->core.IMR;
2357 default:
2358 return 0; /// @todo Or 0xFF? Or something else?
2359 }
2360 }
2361 else
2362 {
2363 /* Page 3 is undocumented and unimplemented. */
2364 LogFunc(("Reading page 3 register: ofs=%X!\n", ofs));
2365 return 0;
2366 }
2367}
2368
2369
2370static int dp8390CoreWriteCR(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t val)
2371{
2372 union {
2373 uint8_t nCR;
2374 DP_CR nCr;
2375 };
2376
2377 nCR = val;
2378 LogFlow(("val=%02X, old=%02X\n", val, pThis->core.CR));
2379 if (nCr.STP != pThis->core.cr.STP)
2380 {
2381 if (nCr.STP)
2382 {
2383 /* Stop the engine -- software reset. */
2384 pThis->core.cr.STP = 1;
2385 pThis->core.isr.RST = 1;
2386 }
2387 else
2388 {
2389 /* Clear the stop condition. */
2390 pThis->core.cr.STP = 0;
2391
2392 /* And possibly start up right away. */
2393 if (nCr.STA)
2394 pThis->core.cr.STA = 1;
2395
2396 /* The STA bit may have been set all along. */
2397 if (pThis->core.cr.STA)
2398 pThis->core.isr.RST = 0;
2399 }
2400
2401 /* Unblock receive thread if necessary, possibly drop any packets. */
2402 dp8390CoreKickReceive(pDevIns, pThis);
2403 }
2404 if (nCr.STA && !pThis->core.cr.STA)
2405 {
2406 /* Start the engine. It is not clearly documented but the STA bit is
2407 * sticky, and once it's set only a hard reset can clear it. Setting the
2408 * STP bit doesn't clear it.
2409 */
2410 pThis->core.cr.STA = 1;
2411 pThis->core.isr.RST = 0;
2412
2413 /* Unblock receive thread. */
2414 dp8390CoreKickReceive(pDevIns, pThis);
2415 }
2416 if (nCr.TXP && !pThis->core.cr.TXP)
2417 {
2418 /* Kick off a transmit. */
2419 pThis->core.cr.TXP = 1; /* Indicate transmit in progress. */
2420 dp8390CoreStartTransmit(pDevIns, pThis);
2421 }
2422
2423 /* It is not possible to write a zero (invalid value) to the RD bits. */
2424 if (nCr.RD == DP_CR_RDMA_INVL)
2425 nCr.RD = DP_CR_RDMA_ABRT;
2426
2427 if (nCr.RD != pThis->core.cr.RD)
2428 {
2429 /* Remote DMA state change. */
2430 if (nCr.RD & DP_CR_RDMA_ABRT)
2431 {
2432 /* Abort. */
2433 LogFunc(("RDMA Abort! RD=%d RSAR=%04X RBCR=%04X CRDA=%04X\n", nCr.RD, pThis->core.RSAR, pThis->core.RBCR, pThis->core.CRDA));
2434 }
2435 else if (nCr.RD == DP_CR_RDMA_SP)
2436 {
2437 DP_PKT_HDR header;
2438
2439 /* Read a packet header from memory at BNRY. */
2440 dpLocalRAMReadBuf(pThis, pThis->core.BNRY, sizeof(header), (uint8_t*)&header);
2441
2442 pThis->core.CRDA = RT_MAKE_U16(0, pThis->core.BNRY);
2443 pThis->core.RBCR = header.byte_cnt;
2444
2445 LogFunc(("RDMA SP: RD=%d RSAR=%04X RBCR=%04X CRDA=%04X\n", nCr.RD, pThis->core.RSAR, pThis->core.RBCR, pThis->core.CRDA));
2446 }
2447 else
2448 {
2449 /* Starting remote DMA read or write. */
2450 LogFunc(("RDMA: RD=%d RSAR=%04X RBCR=%04X\n", nCr.RD, pThis->core.RSAR, pThis->core.RBCR));
2451 }
2452 pThis->core.cr.RD = nCr.RD;
2453 /* NB: The current DMA address (CRDA) is not modified here. */
2454 }
2455 /* Set the page select bits. */
2456 pThis->core.cr.PS = nCr.PS;
2457
2458 return VINF_SUCCESS;
2459}
2460
2461static int dp8390CoreWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, int ofs, uint32_t val)
2462{
2463 int rc = VINF_SUCCESS;
2464 bool fUpdateIRQ = false;
2465
2466 Log2Func(("#%d: page=%d reg=%X val=%02X\n", pThis->iInstance, pThis->core.cr.PS, ofs, val));
2467
2468 /* Command Register exists in all pages. */
2469 if (ofs == DPR_CR)
2470 {
2471 rc = dp8390CoreWriteCR(pDevIns, pThis, val);
2472 }
2473 else if (pThis->core.cr.PS == 0)
2474 {
2475 switch (ofs)
2476 {
2477 case DPR_P0_W_PSTART:
2478 pThis->core.PSTART = val;
2479 pThis->core.CURR = val;
2480 break;
2481 case DPR_P0_W_PSTOP:
2482 pThis->core.PSTOP = val;
2483 break;
2484 case DPR_P0_BNRY:
2485 if (pThis->core.BNRY != val)
2486 {
2487 pThis->core.BNRY = val;
2488 /* Probably made more room in receive ring. */
2489 dp8390CoreKickReceive(pDevIns, pThis);
2490 }
2491 break;
2492 case DPR_P0_W_TPSR:
2493 pThis->core.TPSR = val;
2494 break;
2495 case DPR_P0_W_TBCR0:
2496 pThis->core.tbcr.TBCR0 = val;
2497 break;
2498 case DPR_P0_W_TBCR1:
2499 pThis->core.tbcr.TBCR1 = val;
2500 break;
2501 case DPR_P0_ISR:
2502 /* Bits are cleared by writing 1 to them, except for bit 7 (RST). */
2503 pThis->core.ISR &= ~val | RT_BIT(7);
2504 fUpdateIRQ = true;
2505 break;
2506 case DPR_P0_W_RSAR0:
2507 /* NE2000 ODI driver v2.12 detects card presence by writing RSAR0
2508 * and checking if CRDA0 changes to the same value.
2509 */
2510 pThis->core.rsar.RSAR0 = val;
2511 pThis->core.crda.CRDA0 = val;
2512 break;
2513 case DPR_P0_W_RSAR1:
2514 pThis->core.rsar.RSAR1 = val;
2515 pThis->core.crda.CRDA1 = val;
2516 break;
2517 case DPR_P0_W_RBCR0:
2518 pThis->core.rbcr.RBCR0 = val;
2519 break;
2520 case DPR_P0_W_RBCR1:
2521 pThis->core.rbcr.RBCR1 = val;
2522 break;
2523 case DPR_P0_W_RCR:
2524 pThis->core.RCR = val;
2525 pThis->core.rsr.DIS = pThis->core.rcr.MON;
2526 break;
2527 case DPR_P0_W_TCR:
2528 pThis->core.TCR = val;
2529 break;
2530 case DPR_P0_W_DCR:
2531 pThis->core.DCR = val;
2532 break;
2533 case DPR_P0_W_IMR:
2534 pThis->core.IMR = val & 0x7f; /* Don't let the high bit get set. */
2535 fUpdateIRQ = true;
2536 break;
2537 default:
2538 Assert(0);
2539 break;
2540 }
2541 }
2542 else if (pThis->core.cr.PS == 1)
2543 {
2544 /* Page 1 is easy, most registers are stored directly. */
2545 if (ofs == DPR_P1_CURR)
2546 {
2547 pThis->core.CURR = val;
2548 }
2549 else
2550 pThis->core.PG1[ofs] = val;
2551 }
2552 else if (pThis->core.cr.PS == 2)
2553 {
2554 switch (ofs)
2555 {
2556 case DPR_P2_W_CLDA0:
2557 pThis->core.clda.CLDA0 = val;
2558 break;
2559 case DPR_P2_W_CLDA1:
2560 pThis->core.clda.CLDA1 = val;
2561 break;
2562 case DPR_P2_RNXTPP:
2563 pThis->core.rnxtpp = val;
2564 break;
2565 case DPR_P2_LNXTPP:
2566 pThis->core.lnxtpp = val;
2567 break;
2568 case DPR_P2_ADRCU:
2569 case DPR_P2_ADRCL:
2570 /// @todo What are these?
2571 break;
2572 default:
2573 LogFunc(("Writing unimplemented register: Page 2, offset=%d, val=%02X!\n", ofs, val));
2574 break;
2575 }
2576 }
2577 else
2578 {
2579 /* Page 3 is undocumented and unimplemented. */
2580 LogFunc(("Writing page 3 register: offset=%d, val=%02X!\n", ofs, val));
2581 }
2582
2583 if (fUpdateIRQ)
2584 dp8390CoreUpdateIrq(pDevIns, pThis);
2585
2586 return rc;
2587}
2588
2589
2590static void neLocalRAMWrite8(PDPNICSTATE pThis, uint16_t addr, uint8_t val)
2591{
2592 if (pThis->uDevType == DEV_NE1000)
2593 {
2594 /* Only 14 bits of address are decoded. */
2595 addr &= 0x3fff;
2596 if (addr >= 0x2000)
2597 {
2598 /* Local RAM is mapped at 2000h-3FFFh. */
2599 addr -= 0x2000;
2600 pThis->abLocalRAM[addr] = val;
2601 }
2602 }
2603 else if (pThis->uDevType == DEV_NE2000)
2604 {
2605 /* Only 15 bits of address are decoded. */
2606 addr &= 0x7fff;
2607 if (addr >= 0x4000)
2608 {
2609 /* Local RAM is mapped at 4000h-7FFFh. */
2610 addr -= 0x4000;
2611 pThis->abLocalRAM[addr] = val;
2612 }
2613 }
2614 else
2615 {
2616 Assert(0);
2617 }
2618}
2619
2620
2621static void neLocalRAMWrite16(PDPNICSTATE pThis, uint16_t addr, uint16_t val)
2622{
2623 if (pThis->uDevType == DEV_NE2000)
2624 {
2625 /* Only 14 bits of address are decoded, word aligned. */
2626 addr &= 0x7ffe;
2627 if (addr >= 0x4000)
2628 {
2629 /* Local RAM is mapped at 4000h-7FFFh. */
2630 addr -= 0x4000;
2631 pThis->abLocalRAM[addr+0] = RT_LOBYTE(val);
2632 pThis->abLocalRAM[addr+1] = RT_HIBYTE(val);
2633 }
2634 }
2635 else
2636 {
2637 Assert(0);
2638 }
2639}
2640
2641
2642static uint8_t neLocalRAMRead8(PDPNICSTATE pThis, uint16_t addr)
2643{
2644 uint8_t val = 0xff;
2645
2646 if (pThis->uDevType == DEV_NE1000)
2647 {
2648 /* Only 14 bits of address are decoded. */
2649 addr &= 0x3fff;
2650 if (addr >= 0x2000)
2651 {
2652 /* Local RAM is mapped at 2000h-3FFFh. */
2653 addr -= 0x2000;
2654 val = pThis->abLocalRAM[addr];
2655 }
2656 else
2657 {
2658 /* The PROM is mapped below 2000h, effectively only 4 bits decoded.
2659 * NE1000 emulation uses top 16 bytes of the PROM.
2660 */
2661 val = pThis->aPROM[(addr & 0x0f) + 16]; /// @todo Use a constant
2662 }
2663 }
2664 else if (pThis->uDevType == DEV_NE2000)
2665 {
2666 /* Only 15 bits of address are decoded. */
2667 addr &= 0x7fff;
2668 if (addr >= 0x4000)
2669 {
2670 /* Local RAM is mapped at 4000h-7FFFh. */
2671 addr -= 0x4000;
2672 val = pThis->abLocalRAM[addr];
2673 }
2674 else
2675 {
2676 /* The PROM is mapped below 4000h, effectively only 4 bits decoded.
2677 * Address bits 1:4 from the bus are connected to address pins 0:3
2678 * on the PROM.
2679 */
2680 val = pThis->aPROM[(addr & 0x1f) >> 1]; /// @todo use a constant
2681 }
2682 }
2683 else
2684 {
2685 Assert(0);
2686 }
2687 return val;
2688}
2689
2690
2691static uint16_t neLocalRAMRead16(PDPNICSTATE pThis, uint16_t addr)
2692{
2693 uint16_t val = 0xffff;
2694
2695 if (pThis->uDevType == DEV_NE2000)
2696 {
2697 /* Only 14 bits of address are decoded, word aligned. */
2698 addr &= 0x7ffe;
2699 if (addr >= 0x4000)
2700 {
2701 /* Local RAM is mapped at 4000h-7FFFh. */
2702 addr -= 0x4000;
2703 val = RT_MAKE_U16(pThis->abLocalRAM[addr], pThis->abLocalRAM[addr+1]);
2704 }
2705 else
2706 {
2707 uint8_t uPromByte;
2708
2709 /* The PROM is mapped below 4000h, effectively only 4 bits decoded.
2710 * Address bits 1:4 from the bus are connected to address pins 0:3
2711 * on the PROM.
2712 */
2713 uPromByte = pThis->aPROM[(addr & 0x1f) >> 1];
2714 val = RT_MAKE_U16(uPromByte, uPromByte);
2715 }
2716 }
2717 else
2718 {
2719 Assert(0);
2720 }
2721 return val;
2722}
2723
2724
2725static int neDataPortWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint16_t val)
2726{
2727 /* Remote Write; ignored if Remote DMA command is not 'Write'. */
2728 if (pThis->core.cr.RD == DP_CR_RDMA_WR)
2729 {
2730 /// @todo Also do nothing if DCR.LAS set?
2731 if (pThis->core.dcr.WTS)
2732 {
2733 Log3Func(("RDMA16 write %04X to local addr %04X\n", val, pThis->core.CRDA));
2734 neLocalRAMWrite16(pThis, pThis->core.CRDA, val);
2735 }
2736 else
2737 {
2738 Log3Func(("RDMA8 write %02X to local addr %04X\n", val, pThis->core.CRDA));
2739 neLocalRAMWrite8(pThis, pThis->core.CRDA, val);
2740 }
2741 pThis->core.CRDA += 1 << pThis->core.dcr.WTS;
2742 if ((pThis->core.crda.CRDA1 == pThis->core.PSTOP) && (pThis->core.PSTOP != pThis->core.PSTART))
2743 {
2744 LogFunc(("RDMA wrap / write!! (CRDA=%04X PSTOP=%02X00 PSTART=%02X00)\n", pThis->core.CRDA, pThis->core.PSTOP, pThis->core.PSTART));
2745 Assert(!pThis->core.crda.CRDA0); /// @todo Can misalignment actually happen?
2746 pThis->core.crda.CRDA1 = pThis->core.PSTART;
2747 }
2748 pThis->core.RBCR -= 1;
2749
2750 /* Carefully decrement if WTS set so we don't overshoot and miss EOP. */
2751 if (pThis->core.dcr.WTS && pThis->core.RBCR)
2752 pThis->core.RBCR -= 1;
2753
2754 if (!pThis->core.RBCR)
2755 {
2756 LogFunc(("RDMA EOP / write\n"));
2757 pThis->core.isr.RDC = 1;
2758 pThis->core.cr.RD = 0;
2759 dp8390CoreUpdateIrq(pDevIns, pThis);
2760 }
2761 }
2762 return VINF_SUCCESS;
2763}
2764
2765
2766static uint16_t neDataPortRead(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
2767{
2768 uint16_t val = 0x1234;
2769
2770 /* Remote Read; ignored if Remote DMA command is not 'Read'. */
2771 if (pThis->core.cr.RD == DP_CR_RDMA_RD)
2772 {
2773 /// @todo Also do nothing if DCR.LAS set?
2774 if (pThis->core.dcr.WTS)
2775 {
2776 val = neLocalRAMRead16(pThis, pThis->core.CRDA);
2777 Log3Func(("RDMA16 read from local addr %04X: %04X\n", pThis->core.CRDA, val));
2778 }
2779 else
2780 {
2781 val = neLocalRAMRead8(pThis, pThis->core.CRDA);
2782 Log3Func(("RDMA8 read from local addr %04X: %02X\n", pThis->core.CRDA, val));
2783 }
2784 pThis->core.CRDA += 1 << pThis->core.dcr.WTS;
2785 /// @todo explain that PSTOP=PSTART check is only to reduce logging/busywork
2786 if ((pThis->core.crda.CRDA1 == pThis->core.PSTOP) && (pThis->core.PSTOP != pThis->core.PSTART))
2787 {
2788 Log3Func(("RDMA wrap / read (CRDA=%04X PSTOP=%02X00 PSTART=%02X00)\n", pThis->core.CRDA, pThis->core.PSTOP, pThis->core.PSTART));
2789 Assert(!pThis->core.crda.CRDA0); /// @todo can misalignment happen?
2790 pThis->core.crda.CRDA1 = pThis->core.PSTART;
2791 }
2792 pThis->core.RBCR -= 1;
2793
2794 /* Carefully decrement if WTS set so we don't overshoot and miss EOP. */
2795 if (pThis->core.dcr.WTS && pThis->core.RBCR)
2796 pThis->core.RBCR -= 1;
2797
2798 if (!pThis->core.RBCR)
2799 {
2800 LogFunc(("RDMA EOP / read\n"));
2801 pThis->core.isr.RDC = 1;
2802 pThis->core.cr.RD = 0;
2803 dp8390CoreUpdateIrq(pDevIns, pThis);
2804 }
2805 }
2806 return val;
2807}
2808
2809
2810static int neResetPortWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
2811{
2812 LogFlowFunc(("\n"));
2813 dp8390CoreReset(pDevIns, pThis);
2814 return VINF_SUCCESS;
2815}
2816
2817
2818static int dpNeIoWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr, uint32_t val)
2819{
2820 int reg = addr & 0x0f;
2821 int rc = VINF_SUCCESS;
2822
2823 Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
2824
2825 /* The NE2000 has 8 bytes of data port followed by 8 bytes of reset port.
2826 * In contrast, the NE1000 has 4 bytes of data port followed by 4 bytes
2827 * of reset port, aliased twice within the 16-byte range.
2828 */
2829 if (pThis->uDevType == DEV_NE2000)
2830 reg >>= 1;
2831 if (reg & 0x04)
2832 rc = neResetPortWrite(pDevIns, pThis);
2833 else
2834 rc = neDataPortWrite(pDevIns, pThis, val);
2835
2836 return rc;
2837}
2838
2839
2840static uint32_t neIoRead(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr)
2841{
2842 uint32_t val = UINT32_MAX;
2843 int reg = addr & 0x0f;
2844
2845 /* The NE2000 has 8 bytes of data port followed by 8 bytes of reset port.
2846 * In contrast, the NE1000 has 4 bytes of data port followed by 4 bytes
2847 * of reset port, aliased twice within the 16-byte range.
2848 */
2849 if (pThis->uDevType == DEV_NE2000)
2850 reg >>= 1;
2851 if (reg & 0x04)
2852 val = 0x52; /// @todo Check what really happens
2853 else
2854 val = neDataPortRead(pDevIns, pThis);
2855
2856 Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
2857 return val;
2858}
2859
2860
2861static int wdIoWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr, uint32_t val)
2862{
2863 int reg = addr & 0xf;
2864 int rc = VINF_SUCCESS;
2865 union {
2866 uint8_t nCTRL1;
2867 WD_CTRL1 nCtrl1;
2868 };
2869 union {
2870 uint8_t nCTRL2;
2871 WD_CTRL2 nCtrl2;
2872 };
2873
2874 Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
2875
2876 switch (reg)
2877 {
2878 case WDR_CTRL1:
2879 nCTRL1 = val;
2880 if (nCtrl1.MEME != pThis->ctrl1.MEME)
2881 {
2882 LogFunc(("CTRL1.MEME=%u\n", nCtrl1.MEME));
2883 pThis->ctrl1.MEME = nCtrl1.MEME;
2884 }
2885 if (nCtrl1.RESET)
2886 {
2887 dp8390CoreReset(pDevIns, pThis);
2888 pThis->CTRL1 = 0;
2889 }
2890 break;
2891 case WDR_CTRL2:
2892 /* NYI. */
2893 nCTRL2 = val;
2894 if (nCTRL2 != pThis->CTRL2)
2895 {
2896 LogFunc(("CTRL2=%02X, new=%02X\n", pThis->CTRL2, nCTRL2));
2897 pThis->CTRL2 = nCTRL2;
2898 }
2899 break;
2900 default:
2901 /* Most of the WD registers are read-only. */
2902 break;
2903 }
2904
2905 return rc;
2906}
2907
2908
2909static uint32_t wdIoRead(PDPNICSTATE pThis, uint32_t addr)
2910{
2911 uint32_t val = UINT32_MAX;
2912 int reg = addr & 0x0f;
2913
2914 if (reg >= WDR_PROM)
2915 {
2916 val = pThis->aPROM[reg & 7];
2917 }
2918 else
2919 {
2920 if (pThis->uDevType == DEV_WD8013)
2921 {
2922 switch (reg)
2923 {
2924 case WDR_CTRL1:
2925 val = pThis->CTRL1;
2926 break;
2927 case WDR_ATDET:
2928 val = pThis->uDevType == DEV_WD8013 ? 1 : 0;
2929 break;
2930 case WDR_IOBASE:
2931 val = pThis->aPROM[WDR_IOBASE]; //val = pThis->IOPortBase >> 5;
2932 break;
2933 case WDR_CTRL2:
2934 val = pThis->CTRL2;
2935 break;
2936 case WDR_JP:
2937 val = 0xa0;
2938 break;
2939 default:
2940 val = 0x00; /// @todo What should it be really?
2941 break;
2942 }
2943 }
2944 else
2945 {
2946 /* Old WD adapters (including 8003E) aliased the PROM for
2947 * unimplemented control register reads.
2948 */
2949 switch (reg)
2950 {
2951 case WDR_CTRL2:
2952 val = 1; //pThis->CTRL2;
2953 break;
2954 case WDR_JP:
2955 val = 0xa0;
2956 break;
2957 default:
2958 val = pThis->aPROM[reg & 7];
2959 break;
2960 }
2961 }
2962
2963 }
2964
2965 Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
2966 return val;
2967}
2968
2969
2970static uint8_t elGetIrqFromIdcfr(uint8_t val)
2971{
2972 union {
2973 uint8_t IDCFR;
2974 EL_IDCFR idcfr;
2975 };
2976 uint8_t irq = 0;
2977
2978 IDCFR = val;
2979
2980 /* Lowest set IRQ bit wins (might not match hardware).
2981 * NB: It is valid to not enable any IRQ line!
2982 */
2983 if (idcfr.irq2)
2984 irq = 2;
2985 else if (idcfr.irq3)
2986 irq = 3;
2987 else if (idcfr.irq4)
2988 irq = 4;
2989 else if (idcfr.irq5)
2990 irq = 5;
2991
2992 return irq;
2993}
2994
2995static uint8_t elGetDrqFromIdcfr(uint8_t val)
2996{
2997 union {
2998 uint8_t IDCFR;
2999 EL_IDCFR idcfr;
3000 };
3001 uint8_t drq = 0;
3002
3003 IDCFR = val;
3004
3005 /* Lowest set DRQ bit wins; it is valid to not set any. */
3006 if (idcfr.drq1)
3007 drq = 1;
3008 else if (idcfr.drq2)
3009 drq = 2;
3010 else if (idcfr.drq3)
3011 drq = 3;
3012
3013 return drq;
3014}
3015
3016static void elWriteIdcfr(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PEL_GA pGa, uint8_t val)
3017{
3018 uint8_t uOldIrq = pThis->uIsaIrq;
3019 uint8_t uNewIrq;
3020 uint8_t uOldDrq = pThis->uElIsaDma;
3021 uint8_t uNewDrq;
3022
3023 /* If the IRQ is currently active, have to switch it. */
3024 uNewIrq = elGetIrqFromIdcfr(val);
3025 if (uOldIrq != uNewIrq)
3026 {
3027 LogFunc(("#%d Switching IRQ=%d -> IRQ=%d\n", pThis->iInstance, uOldIrq, uNewIrq));
3028 if (pThis->fNicIrqActive)
3029 {
3030 /* This probably isn't supposed to happen. */
3031 LogFunc(("#%d Moving active IRQ!\n", pThis->iInstance));
3032 if (uOldIrq)
3033 PDMDevHlpISASetIrq(pDevIns, uOldIrq, 0);
3034 if (uNewIrq)
3035 PDMDevHlpISASetIrq(pDevIns, uNewIrq, 1);
3036 }
3037 pThis->uIsaIrq = uNewIrq;
3038 }
3039
3040 /* And now the same dance for DMA. */
3041 uNewDrq = elGetDrqFromIdcfr(val);
3042 if (uOldDrq != uNewDrq)
3043 {
3044 /// @todo We can't really move the DRQ, what can we do?
3045 LogFunc(("#%d Switching DRQ=%d -> DRQ=%d\n", pThis->iInstance, uOldDrq, uNewDrq));
3046 pThis->uElIsaDma = uNewDrq;
3047 }
3048
3049 pGa->IDCFR = val;
3050}
3051
3052
3053static void elWriteGacfr(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PEL_GA pGa, uint8_t val)
3054{
3055 union {
3056 uint8_t nGACFR;
3057 GA_GACFR nGacfr;
3058 };
3059
3060 nGACFR = val;
3061
3062 if (nGacfr.nim != pGa->gacfr.nim)
3063 {
3064 /// @todo Should we just run UpdateInterrupts?
3065 if (pThis->fNicIrqActive && !nGacfr.nim)
3066 {
3067 LogFunc(("#%d: Unmasking active IRQ!\n", pThis->iInstance));
3068 PDMDevHlpISASetIrq(pDevIns, pThis->uIsaIrq, 1);
3069 }
3070 else if (pThis->fNicIrqActive && nGacfr.nim)
3071 {
3072 LogFunc(("#%d: Masking active IRQ\n", pThis->iInstance));
3073 PDMDevHlpISASetIrq(pDevIns, pThis->uIsaIrq, 0);
3074 }
3075 }
3076
3077 /// @todo rsel/mbs bit change?
3078 if (nGacfr.rsel != pGa->gacfr.rsel)
3079 {
3080 LogFunc(("#%d: rsel=%u mbs=%u\n", pThis->iInstance, nGacfr.rsel, nGacfr.mbs));
3081 }
3082
3083 pGa->GACFR = nGACFR;
3084}
3085
3086
3087static void elSoftReset(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
3088{
3089 PEL_GA pGa = &pThis->ga;
3090
3091 LogFlow(("Resetting ASIC GA\n"));
3092 /* Most GA registers are zeroed. */
3093 pGa->PSTR = pGa->PSPR = 0;
3094 pGa->DQTR = 0;
3095 elWriteGacfr(pDevIns, pThis, pGa, 0);
3096 pGa->STREG = ELNKII_GA_REV;
3097 pGa->VPTR0 = pGa->VPTR1 = pGa->VPTR2 = 0;
3098 pGa->DALSB = pGa->DAMSB = 0;
3099 elWriteIdcfr(pDevIns, pThis, pGa, 0);
3100 pGa->GACR = 0x0B; /* Low bit set = in reset state. */
3101 pGa->fGaIrq = false;
3102
3103 /* Reset the NIC core. */
3104 dp8390CoreReset(pDevIns, pThis);
3105}
3106
3107
3108static int elWriteGacr(PPDMDEVINS pDevIns, PDPNICSTATE pThis, PEL_GA pGa, uint8_t val)
3109{
3110 union {
3111 uint8_t nGACR;
3112 GA_GACR nGacr;
3113 };
3114
3115 nGACR = val;
3116
3117 if (nGacr.rst != pGa->gacr.rst)
3118 {
3119 /* When going out of reset, only clear the rst bit. 3C503 diagnostics checks for this. */
3120 if (nGacr.rst)
3121 elSoftReset(pDevIns, pThis);
3122 else
3123 pGa->gacr.rst = 0;
3124 }
3125 else
3126 {
3127#ifdef IN_RING0
3128 /* Force a trip to R3. */
3129 if (pThis->uElIsaDma == pThis->uIsaDma)
3130 return VINF_IOM_R3_IOPORT_WRITE;
3131#endif
3132
3133 /* Make the data registers "ready" as long as transfers are started. */
3134 if (nGacr.start)
3135 {
3136 pGa->cdadr.cdadr_lsb = pGa->DALSB;
3137 pGa->cdadr.cdadr_msb = pGa->DAMSB;
3138 LogFunc(("DMA started, ddir=%u, cdadr=%04X\n", pGa->gacr.ddir, pGa->CDADR));
3139 pGa->streg.dprdy = 1;
3140 pGa->streg.dip = 1;
3141 pGa->streg.dtc = 0;
3142 }
3143 else
3144 {
3145 pGa->streg.dprdy = 0;
3146 pGa->streg.dip = 0;
3147 }
3148
3149 /* Only do anything if the software configured DMA channel matches the emulation config. */
3150 if (pThis->uElIsaDma == pThis->uIsaDma)
3151 {
3152#ifdef IN_RING3
3153 PDMDevHlpDMASetDREQ(pDevIns, pThis->uIsaDma, pGa->streg.dprdy);
3154 if (pGa->streg.dprdy)
3155 PDMDevHlpDMASchedule(pDevIns);
3156 LogFunc(("#%d: DREQ for channel %u set to %u\n", pThis->iInstance, pThis->uIsaDma, pGa->streg.dprdy));
3157#else
3158 /* Must not get here. */
3159 Assert(0);
3160#endif
3161 }
3162
3163 pGa->GACR = nGACR;
3164 LogFunc(("GACR=%02X ealo=%u eahi=%u\n", pGa->GACR, pGa->gacr.ealo, pGa->gacr.eahi));
3165 }
3166
3167 return VINF_SUCCESS;
3168}
3169
3170
3171static int elGaDataWrite(PDPNICSTATE pThis, PEL_GA pGa, uint16_t val)
3172{
3173 /* Data write; ignored if not started and in "download" mode. */
3174 if (pGa->gacr.start && pGa->gacr.ddir)
3175 {
3176 uint16_t addr = pGa->CDADR;
3177
3178 addr &= 0x3fff;
3179 if (addr >= 0x2000)
3180 {
3181 /* Local RAM is mapped at 2000h-3FFFh. */
3182 addr -= 0x2000;
3183 pThis->abLocalRAM[addr] = val;
3184 }
3185
3186 pGa->CDADR++;
3187 /// @todo Does this really apply to writes or only reads?
3188 if ((pGa->cdadr.cdadr_msb == pGa->PSPR) && (pGa->PSPR != pGa->PSTR))
3189 {
3190 LogFunc(("GA DMA wrap / write!! (cdadr=%04X PSPR=%02X00 PSTR=%02X00)\n", pGa->CDADR, pGa->PSPR, pGa->PSTR));
3191 pGa->cdadr.cdadr_msb = pGa->PSTR;
3192 }
3193 }
3194 return VINF_SUCCESS;
3195}
3196
3197
3198static uint8_t elGaDataRead(PDPNICSTATE pThis, PEL_GA pGa)
3199{
3200 uint8_t val = 0xcd;
3201
3202 /* Data read; ignored if not started and in "upload" mode. */
3203 if (pGa->gacr.start && !pGa->gacr.ddir)
3204 {
3205 uint16_t addr = pGa->CDADR;
3206
3207 addr &= 0x3fff;
3208 if (addr >= 0x2000)
3209 {
3210 /* Local RAM is mapped at 2000h-3FFFh. */
3211 addr -= 0x2000;
3212 val = pThis->abLocalRAM[addr];
3213 }
3214
3215 pGa->CDADR++;
3216 if ((pGa->cdadr.cdadr_msb == pGa->PSPR) && (pGa->PSPR != pGa->PSTR))
3217 {
3218 LogFunc(("GA DMA wrap / read!! (cdadr=%04X PSPR=%02X00 PSTR=%02X00)\n", pGa->CDADR, pGa->PSPR, pGa->PSTR));
3219 pGa->cdadr.cdadr_msb = pGa->PSTR;
3220 }
3221 }
3222 return val;
3223}
3224
3225
3226static int elGaIoWrite(PPDMDEVINS pDevIns, PDPNICSTATE pThis, uint32_t addr, uint32_t val)
3227{
3228 int reg = addr & 0xf;
3229 int rc = VINF_SUCCESS;
3230 PEL_GA pGa = &pThis->ga;
3231
3232 Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
3233
3234 switch (reg)
3235 {
3236 case GAR_PSTR:
3237 pGa->PSTR = val;
3238 break;
3239 case GAR_PSPR:
3240 pGa->PSPR = val;
3241 break;
3242 case GAR_DQTR:
3243 pGa->DQTR = val;
3244 break;
3245 case GAR_GACFR:
3246 elWriteGacfr(pDevIns, pThis, pGa, val);
3247 break;
3248 case GAR_GACR:
3249 rc = elWriteGacr(pDevIns, pThis, pGa, val);
3250 break;
3251 case GAR_STREG:
3252 /* Writing anything to STREG clears ASIC interrupt. */
3253 pThis->ga.streg.dtc = 0;
3254 pThis->ga.fGaIrq = false;
3255 dp8390CoreUpdateIrq(pDevIns, pThis);
3256 break;
3257 case GAR_IDCFR:
3258 elWriteIdcfr(pDevIns, pThis, pGa, val);
3259 break;
3260 case GAR_DAMSB:
3261 pGa->DAMSB = val;
3262 break;
3263 case GAR_DALSB:
3264 pGa->DALSB = val;
3265 break;
3266 case GAR_VPTR2:
3267 pGa->VPTR2 = val;
3268 break;
3269 case GAR_VPTR1:
3270 pGa->VPTR1 = val;
3271 break;
3272 case GAR_VPTR0:
3273 pGa->VPTR0 = val;
3274 break;
3275 case GAR_RFMSB:
3276 case GAR_RFLSB:
3277 elGaDataWrite(pThis, pGa, val);
3278 break;
3279 case GAR_R_BCFR:
3280 case GAR_R_PCFR:
3281 /* Read-only registers, ignored. */
3282 break;
3283 default:
3284 Assert(0);
3285 break;
3286 }
3287
3288 return rc;
3289}
3290
3291
3292static uint32_t elGaIoRead(PDPNICSTATE pThis, uint32_t addr)
3293{
3294 uint32_t val = UINT32_MAX;
3295 int reg = addr & 0x0f;
3296 PEL_GA pGa = &pThis->ga;
3297
3298 switch (reg)
3299 {
3300 case GAR_PSTR:
3301 val = pGa->PSTR;
3302 break;
3303 case GAR_PSPR:
3304 val = pGa->PSPR;
3305 break;
3306 case GAR_DQTR:
3307 val = pGa->DQTR;
3308 break;
3309 case GAR_R_BCFR:
3310 val = pGa->BCFR;
3311 break;
3312 case GAR_R_PCFR:
3313 val = pGa->PCFR;
3314 break;
3315 case GAR_GACFR:
3316 val = pGa->GACFR;
3317 break;
3318 case GAR_GACR:
3319 val = pGa->GACR;
3320 break;
3321 case GAR_STREG:
3322 val = pGa->STREG;
3323 break;
3324 case GAR_IDCFR:
3325 val = pGa->IDCFR;
3326 break;
3327 case GAR_DAMSB:
3328 val = pGa->DAMSB;
3329 break;
3330 case GAR_DALSB:
3331 val = pGa->DALSB;
3332 break;
3333 case GAR_VPTR2:
3334 val = pGa->VPTR2;
3335 break;
3336 case GAR_VPTR1:
3337 val = pGa->VPTR1;
3338 break;
3339 case GAR_VPTR0:
3340 val = pGa->VPTR0;
3341 break;
3342 case GAR_RFMSB:
3343 case GAR_RFLSB:
3344 val = elGaDataRead(pThis, pGa);
3345 break;
3346 default:
3347 Assert(0);
3348 break;
3349 }
3350
3351 Log2Func(("#%d: addr=%#06x val=%#04x\n", pThis->iInstance, addr, val & 0xff));
3352 return val;
3353}
3354
3355
3356/**
3357 * @callback_method_impl{FNIOMIOPORTIN}
3358 */
3359static DECLCALLBACK(VBOXSTRICTRC)
3360neIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3361{
3362 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3363 int rc = VINF_SUCCESS;
3364 int reg = Port & 0xf;
3365 uint8_t u8Lo, u8Hi = 0;
3366 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
3367 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3368 RT_NOREF_PV(pvUser);
3369
3370 switch (cb)
3371 {
3372 case 1:
3373 *pu32 = neIoRead(pDevIns, pThis, reg);
3374 break;
3375 case 2:
3376 /* Manually split word access if necessary if it's an NE1000. Perhaps overkill. */
3377 if (pThis->uDevType == DEV_NE1000)
3378 {
3379 u8Lo = neIoRead(pDevIns, pThis, reg);
3380 if (reg < 0xf) // This logic is not entirely accurate (wraparound).
3381 u8Hi = neIoRead(pDevIns, pThis, reg + 1);
3382 *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
3383 }
3384 else
3385 *pu32 = neIoRead(pDevIns, pThis, reg);
3386 break;
3387 default:
3388 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3389 "neIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
3390 Port, cb);
3391 }
3392
3393 Log2Func(("#%d: NE Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
3394 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
3395 return rc;
3396}
3397
3398
3399/**
3400 * @callback_method_impl{FNIOMIOPORTIN}
3401 */
3402static DECLCALLBACK(VBOXSTRICTRC)
3403wdIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3404{
3405 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3406 int rc = VINF_SUCCESS;
3407 int reg = Port & 0xf;
3408 uint8_t u8Lo, u8Hi = 0;
3409 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
3410 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3411 RT_NOREF_PV(pvUser);
3412
3413 switch (cb)
3414 {
3415 case 1:
3416 *pu32 = wdIoRead(pThis, reg);
3417 break;
3418 case 2:
3419 /* Manually split word access. */
3420 u8Lo = wdIoRead(pThis, reg);
3421 if (reg < 0xf) // This logic is not entirely accurate (wraparound).
3422 u8Hi = wdIoRead(pThis, reg + 1);
3423 *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
3424 break;
3425 default:
3426 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3427 "wdIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
3428 Port, cb);
3429 }
3430
3431 Log2Func(("#%d: WD Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
3432 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
3433 return rc;
3434}
3435
3436
3437/**
3438 * @callback_method_impl{FNIOMIOPORTIN}
3439 */
3440static DECLCALLBACK(VBOXSTRICTRC)
3441elIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3442{
3443 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3444 int rc = VINF_SUCCESS;
3445 int reg = Port & 0xf;
3446 uint8_t u8Lo, u8Hi = 0;
3447 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
3448 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3449 RT_NOREF_PV(pvUser);
3450
3451 switch (cb)
3452 {
3453 case 1:
3454 *pu32 = elGaIoRead(pThis, reg);
3455 break;
3456 case 2:
3457 /* Manually split word access. */
3458 u8Lo = elGaIoRead(pThis, reg);
3459 if (reg < 0xf) // This logic is not entirely accurate (wraparound).
3460 u8Hi = elGaIoRead(pThis, reg + 1);
3461 *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
3462 break;
3463 default:
3464 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3465 "elIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
3466 Port, cb);
3467 }
3468
3469 Log2Func(("#%d: EL Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
3470 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
3471 return rc;
3472}
3473
3474
3475/**
3476 * @callback_method_impl{FNIOMIOPORTIN}
3477 */
3478static DECLCALLBACK(VBOXSTRICTRC)
3479dp8390CoreIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
3480{
3481 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3482 int rc = VINF_SUCCESS;
3483 int reg = Port & 0xf;
3484 uint8_t u8Lo, u8Hi;
3485 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
3486 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3487 RT_NOREF_PV(pvUser);
3488
3489 switch (cb)
3490 {
3491 case 1:
3492 *pu32 = dp8390CoreRead(pDevIns, pThis, reg);
3493 break;
3494 case 2:
3495 /* Manually split word access. */
3496 u8Lo = dp8390CoreRead(pDevIns, pThis, reg + 0);
3497 /* This logic is not entirely accurate. */
3498 if (reg < 0xf)
3499 u8Hi = dp8390CoreRead(pDevIns, pThis, reg + 1);
3500 else
3501 u8Hi = 0;
3502 *pu32 = RT_MAKE_U16(u8Lo, u8Hi);
3503 break;
3504 default:
3505 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3506 "dp8390CoreIOPortRead: unsupported operation size: offset=%#10x cb=%u\n",
3507 Port, cb);
3508 }
3509
3510 Log2Func(("#%d: Port=%RTiop *pu32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, *pu32, cb, rc));
3511 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
3512 return rc;
3513}
3514
3515
3516/**
3517 * @callback_method_impl{FNIOMIOPORTOUT}
3518 */
3519static DECLCALLBACK(VBOXSTRICTRC)
3520neIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3521{
3522 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3523 int rc = VINF_SUCCESS;
3524 int reg = Port & 0xf;
3525 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3526 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3527 RT_NOREF_PV(pvUser);
3528
3529 switch (cb)
3530 {
3531 case 1:
3532 rc = dpNeIoWrite(pDevIns, pThis, Port, RT_LOBYTE(u32));
3533 break;
3534 case 2:
3535 /* Manually split word access if necessary. */
3536 if (pThis->uDevType == DEV_NE2000)
3537 {
3538 rc = dpNeIoWrite(pDevIns, pThis, Port, RT_LOWORD(u32));
3539 }
3540 else
3541 {
3542 rc = dpNeIoWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
3543 if (RT_SUCCESS(rc) && (reg < 0xf))
3544 rc = dpNeIoWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
3545 }
3546 break;
3547 default:
3548 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3549 "neIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
3550 Port, cb);
3551 }
3552
3553 Log2Func(("#%d: NE Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
3554 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3555 return rc;
3556}
3557
3558
3559/**
3560 * @callback_method_impl{FNIOMIOPORTOUT}
3561 */
3562static DECLCALLBACK(VBOXSTRICTRC)
3563wdIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3564{
3565 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3566 int rc = VINF_SUCCESS;
3567 int reg = Port & 0xf;
3568 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3569 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3570 RT_NOREF_PV(pvUser);
3571
3572 switch (cb)
3573 {
3574 case 1:
3575 rc = wdIoWrite(pDevIns, pThis, Port, RT_LOBYTE(u32));
3576 break;
3577 case 2:
3578 /* Manually split word access. */
3579 rc = wdIoWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
3580 if (RT_SUCCESS(rc) && (reg < 0xf))
3581 rc = wdIoWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
3582 break;
3583 default:
3584 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3585 "wdIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
3586 Port, cb);
3587 }
3588
3589 Log2Func(("#%d: WD Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
3590 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3591 return rc;
3592}
3593
3594
3595/**
3596 * @callback_method_impl{FNIOMIOPORTOUT}
3597 */
3598static DECLCALLBACK(VBOXSTRICTRC)
3599elIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3600{
3601 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3602 int rc = VINF_SUCCESS;
3603 int reg = Port & 0xf;
3604 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3605 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3606 RT_NOREF_PV(pvUser);
3607
3608 switch (cb)
3609 {
3610 case 1:
3611 rc = elGaIoWrite(pDevIns, pThis, Port, RT_LOBYTE(u32));
3612 break;
3613 case 2:
3614 /* Manually split word access. */
3615 rc = elGaIoWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
3616 if (RT_SUCCESS(rc) && (reg < 0xf))
3617 rc = elGaIoWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
3618 break;
3619 default:
3620 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3621 "elIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
3622 Port, cb);
3623 }
3624
3625 Log2Func(("#%d: EL Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
3626 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3627 return rc;
3628}
3629
3630
3631/**
3632 * @callback_method_impl{FNIOMIOPORTOUT}
3633 */
3634static DECLCALLBACK(VBOXSTRICTRC)
3635dp8390CoreIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
3636{
3637 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3638 int rc = VINF_SUCCESS;
3639 int reg = Port & 0xf;
3640 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3641 Assert(PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
3642 RT_NOREF_PV(pvUser);
3643
3644 switch (cb)
3645 {
3646 case 1:
3647 rc = dp8390CoreWrite(pDevIns, pThis, reg, RT_LOBYTE(u32));
3648 break;
3649 case 2:
3650 /* Manually split word access. */
3651 rc = dp8390CoreWrite(pDevIns, pThis, reg + 0, RT_LOBYTE(u32));
3652 if (!RT_SUCCESS(rc))
3653 break;
3654 rc = dp8390CoreWrite(pDevIns, pThis, reg + 1, RT_HIBYTE(u32));
3655 break;
3656 default:
3657 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
3658 "dp8390CoreIOPortWrite: unsupported operation size: offset=%#10x cb=%u\n",
3659 Port, cb);
3660 }
3661
3662 Log2Func(("#%d: Port=%RTiop u32=%#RX32 cb=%d rc=%Rrc\n", pThis->iInstance, Port, u32, cb, rc));
3663 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
3664 return rc;
3665}
3666
3667
3668#if 0
3669/**
3670 * @callback_method_impl{FNIOMMMIONEWFILL,
3671 * Local RAM write hook\, to be called from IOM. This is the advanced version of
3672 * wdMemWrite function.}
3673 */
3674static DECLCALLBACK(VBOXSTRICTRC)
3675dpWdMmioFill(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, uint32_t u32Item, unsigned cbItem, unsigned cItems)
3676{
3677 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3678 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));
3679
3680 !!
3681 return VINF_SUCCESS
3682}
3683#endif
3684
3685
3686/**
3687 * @callback_method_impl{FNIOMMMIONEWREAD,
3688 * Local RAM read hook\, to be called from IOM.}
3689 */
3690static DECLCALLBACK(VBOXSTRICTRC) wdMemRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
3691{
3692 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3693 uint8_t *pbData = (uint8_t *)pv;
3694 NOREF(pvUser);
3695
3696// STAM_PROFILE_START(&pThis->CTX_MID_Z(Stat,MemoryRead), a);
3697 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));
3698
3699 if (pThis->ctrl1.MEME)
3700 {
3701 Log3Func(("#%d: Reading %u bytes from address %X: [%.*Rhxs]\n", pThis->iInstance, cb, off, cb, &pThis->abLocalRAM[off & DPNIC_MEM_MASK]));
3702 while (cb-- > 0)
3703 *pbData++ = pThis->abLocalRAM[off++ & DPNIC_MEM_MASK];
3704 }
3705 else
3706 memset(pv, 0xff, cb);
3707
3708// STAM_PROFILE_STOP(&pThis->CTX_MID_Z(Stat,MemoryRead), a);
3709 return VINF_SUCCESS;
3710}
3711
3712/**
3713 * @callback_method_impl{FNIOMMMIONEWWRITE,
3714 * Local RAM write hook\, to be called from IOM.}
3715 */
3716static DECLCALLBACK(VBOXSTRICTRC) wdMemWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
3717{
3718 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3719 uint8_t const *pbSrc = (uint8_t const *)pv;
3720 NOREF(pvUser);
3721
3722// STAM_PROFILE_START(&pThis->CTX_MID_Z(Stat,MemoryWrite), a);
3723 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));
3724
3725 if (pThis->ctrl1.MEME)
3726 {
3727 Log3Func(("#%d: Writing %u bytes to address %X: [%.*Rhxs]\n", pThis->iInstance, cb, off, cb, pbSrc));
3728 while (cb-- > 0)
3729 pThis->abLocalRAM[off++ & DPNIC_MEM_MASK] = *pbSrc++;
3730 }
3731
3732// STAM_PROFILE_STOP(&pThis->CTX_MID_Z(Stat,MemoryWrite), a);
3733 return VINF_SUCCESS;
3734}
3735
3736
3737/**
3738 * @callback_method_impl{FNIOMMMIONEWREAD,
3739 * Local RAM read hook\, to be called from IOM.}
3740 */
3741static DECLCALLBACK(VBOXSTRICTRC) elMemRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
3742{
3743 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3744 uint8_t *pbData = (uint8_t *)pv;
3745 NOREF(pvUser);
3746
3747 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));
3748
3749 if (pThis->ga.gacfr.rsel)
3750 {
3751 Log3Func(("#%d: Reading %u bytes from address %X\n", pThis->iInstance, cb, off));
3752 while (cb-- > 0)
3753 *pbData++ = pThis->abLocalRAM[off++ & DPNIC_MEM_MASK];
3754 }
3755 else
3756 {
3757 Log3Func(("#%d: Ignoring read of %u bytes from address %X\n", pThis->iInstance, cb, off));
3758 memset(pv, 0xff, cb);
3759 }
3760 return VINF_SUCCESS;
3761}
3762
3763
3764/**
3765 * @callback_method_impl{FNIOMMMIONEWWRITE,
3766 * Local RAM write hook\, to be called from IOM.}
3767 */
3768static DECLCALLBACK(VBOXSTRICTRC) elMemWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
3769{
3770 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3771 uint8_t const *pbSrc = (uint8_t const *)pv;
3772 NOREF(pvUser);
3773
3774 Assert(PDMDevHlpCritSectIsOwner(pDevIns, pDevIns->CTX_SUFF(pCritSectRo)));
3775
3776 if (pThis->ga.gacfr.rsel)
3777 {
3778 Log3Func(("#%d: Writing %u bytes to address %X\n", pThis->iInstance, cb, off));
3779 while (cb-- > 0)
3780 pThis->abLocalRAM[off++ & DPNIC_MEM_MASK] = *pbSrc++;
3781 }
3782 else
3783 {
3784 Log3Func(("#%d: Ignoring write of %u bytes to address %X\n", pThis->iInstance, cb, off));
3785 }
3786 return VINF_SUCCESS;
3787}
3788
3789
3790#ifdef IN_RING3
3791
3792/* Shamelessly stolen from DevDMA.cpp */
3793
3794/* Test the decrement bit of mode register. */
3795#define IS_MODE_DEC(c) ((c) & 0x20)
3796/* Test the auto-init bit of mode register. */
3797#define IS_MODE_AI(c) ((c) & 0x10)
3798/* Extract the transfer type bits of mode register. */
3799#define GET_MODE_XTYP(c) (((c) & 0x0c) >> 2)
3800
3801/* DMA transfer modes. */
3802enum {
3803 DMODE_DEMAND, /* Demand transfer mode. */
3804 DMODE_SINGLE, /* Single transfer mode. */
3805 DMODE_BLOCK, /* Block transfer mode. */
3806 DMODE_CASCADE /* Cascade mode. */
3807};
3808
3809/* DMA transfer types. */
3810enum {
3811 DTYPE_VERIFY, /* Verify transfer type. */
3812 DTYPE_WRITE, /* Write transfer type. */
3813 DTYPE_READ, /* Read transfer type. */
3814 DTYPE_ILLEGAL /* Undefined. */
3815};
3816
3817static DECLCALLBACK(uint32_t) elnk3R3DMAXferHandler(PPDMDEVINS pDevIns, void *opaque,
3818 unsigned nchan, uint32_t dma_pos, uint32_t dma_len)
3819{
3820 PDPNICSTATE pThis = (PDPNICSTATE)opaque;
3821 int dma_mode;
3822 int dma_type;
3823 uint16_t cbToXfer;
3824 uint32_t cbXferred = 0;
3825 uint16_t uDmaAddr;
3826 int rc;
3827
3828 /*
3829 * The 3C503 EtherLink II uses DMA as an alternative to shared RAM
3830 * or PIO. The Gate Array tracks its own current DMA address within
3831 * the adapter's local address space.
3832 */
3833 dma_mode = PDMDevHlpDMAGetChannelMode(pDevIns, pThis->uIsaDma);
3834 dma_type = GET_MODE_XTYP(dma_mode);
3835 uDmaAddr = pThis->ga.CDADR;
3836 cbToXfer = dma_len;
3837 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));
3838
3839 /* Skip any accesses below local memory start. */
3840 if ((0x2000 > 0) && (uDmaAddr < 0x2000)) /// @todo Should keep track in variables
3841 {
3842 uint16_t cbToSkip = 0x2000 - uDmaAddr;
3843
3844 uDmaAddr += cbToSkip;
3845 /// @todo Should this write junk to host memory when reading from device?
3846 if (cbToSkip < cbToXfer)
3847 {
3848 cbToXfer -= cbToSkip;
3849 Assert(uDmaAddr == 0x2000);
3850 LogFunc(("DMA skipping %u bytes!\n", cbToSkip));
3851 }
3852 else
3853 {
3854 cbToXfer = 0; /* Transfer entirely below valid address range. */
3855 LogFunc(("DMA below valid address range!\n"));
3856 }
3857 }
3858
3859 if (cbToXfer)
3860 {
3861 uint16_t cbToSkip = 0;
3862
3863 /* Clip transfer size so it falls within local RAM. */
3864 if ((uDmaAddr - 0x2000 + cbToXfer) > (int)sizeof(pThis->abLocalRAM))
3865 {
3866 /* Calculate how much to skip anything at the end. */
3867 cbToSkip = sizeof(pThis->abLocalRAM) - (0x2000 - uDmaAddr + cbToXfer);
3868 LogFunc(("DMA above valid address range uDmaAddr=%04X cbToXfer=%u cbToSkip=%u!\n", uDmaAddr, cbToXfer, cbToSkip));
3869 cbToXfer -= cbToSkip;
3870 }
3871
3872 if (dma_type == DTYPE_WRITE)
3873 {
3874 /* Write transfer type. Reading from device, writing to memory. */
3875 if (!pThis->ga.gacr.ddir)
3876 {
3877 Log2Func(("DMAWriteMemory uDmaAddr=%04X cbToXfer=%u\n", uDmaAddr, cbToXfer));
3878 rc = PDMDevHlpDMAWriteMemory(pDevIns, nchan,
3879 &pThis->abLocalRAM[uDmaAddr - 0x2000],
3880 dma_pos, cbToXfer, &cbXferred);
3881 AssertMsgRC(rc, ("DMAWriteMemory -> %Rrc\n", rc));
3882 }
3883 else
3884 {
3885 // Do nothing, direction does not match.
3886 /// @todo Bug in DevDMA?
3887 LogFunc(("DTYPE_WRITE but GACR.ddir set, do nothing!\n"));
3888 }
3889 }
3890 else
3891 {
3892 /* Read of Verify transfer type. Reading from memory, writing to device. */
3893 if (pThis->ga.gacr.ddir)
3894 {
3895 Log2Func(("DMAReadMemory uDmaAddr=%04X cbToXfer=%u\n", uDmaAddr, cbToXfer));
3896 rc = PDMDevHlpDMAReadMemory(pDevIns, nchan,
3897 &pThis->abLocalRAM[uDmaAddr - 0x2000],
3898 dma_pos, cbToXfer, &cbXferred);
3899 AssertMsgRC(rc, ("DMAReadMemory -> %Rrc\n", rc));
3900 }
3901 else
3902 {
3903 // Do nothing, direction does not match.
3904 /// @todo Bug in DevDMA?
3905 LogFunc(("DTYPE_READ but GACR.ddir clear, do nothing!\n"));
3906 }
3907 }
3908
3909 /* NB: This might wrap. In theory it might wrap back to valid
3910 * memory but... just no.
3911 */
3912 /// @todo Actually... what would really happen?
3913 uDmaAddr += cbToXfer + cbToSkip;
3914 }
3915 Log2Func(("After DMA transfer: uDmaAddr=%04X, cbXferred=%u\n", uDmaAddr, cbXferred));
3916
3917 /* Advance the DMA address and see if transfer completed (it almost certainly did). */
3918 if (1)
3919 {
3920 Log2Func(("DMA completed\n"));
3921 PDMDevHlpDMASetDREQ(pDevIns, pThis->uIsaDma, 0);
3922 pThis->ga.streg.dtc = 1;
3923 pThis->ga.fGaIrq = true;
3924 dp8390CoreUpdateIrq(pDevIns, pThis);
3925 }
3926 else
3927 {
3928 LogFunc(("DMA continuing: uDmaAddr=%04X, cbXferred=%u\n", uDmaAddr, cbXferred));
3929 PDMDevHlpDMASchedule(pDevIns);
3930 }
3931
3932 /* Returns the updated transfer count. */
3933 return dma_pos + dma_len;
3934}
3935
3936
3937/* -=-=-=-=-=- Timer Callbacks -=-=-=-=-=- */
3938
3939/**
3940 * @callback_method_impl{FNTMTIMERDEV, Restore timer callback}
3941 *
3942 * This is only called when we restore a saved state and temporarily
3943 * disconnected the network link to inform the guest that network connections
3944 * should be considered lost.
3945 */
3946static DECLCALLBACK(void) dpNicR3TimerRestore(PPDMDEVINS pDevIns, TMTIMERHANDLE hTimer, void *pvUser)
3947{
3948 RT_NOREF(pvUser);
3949 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3950 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
3951 AssertReleaseRC(rc);
3952
3953 rc = VERR_GENERAL_FAILURE;
3954
3955 /* The DP8390 based cards have no concept of link state. Reporting collisions on all transmits
3956 * is the best approximation of a disconnected cable that we can do. Some drivers (3C503) warn
3957 * of possible disconnected cable, some don't. Many cards with DP8390 chips had permanently
3958 * attached cables (AUI or BNC) and their drivers do not expect cables to be disconnected and
3959 * re-connected at runtime. Guests which are waiting for a receive have no way to notice any
3960 * problem, therefore we only postpone restoring a link a couple of times, and then reconnect
3961 * regardless of whether the guest noticed anything or not.
3962 */
3963 if ( (pThis->cLinkDownReported <= DPNIC_MAX_LINKDOWN_REPORTED)
3964 && (pThis->cLinkRestorePostponed <= DPNIC_MAX_LINKRST_POSTPONED))
3965 rc = PDMDevHlpTimerSetMillies(pDevIns, hTimer, 1500);
3966 if (RT_FAILURE(rc))
3967 {
3968 pThis->fLinkTempDown = false;
3969 if (pThis->fLinkUp)
3970 {
3971 LogRel(("DPNIC#%d: The link is back up again after the restore.\n",
3972 pThis->iInstance));
3973 LogFunc(("#%d: cLinkDownReported=%d\n", pThis->iInstance, pThis->cLinkDownReported));
3974 pThis->Led.Actual.s.fError = 0;
3975 }
3976 }
3977 else
3978 {
3979 LogFunc(("#%d: cLinkDownReported=%d, cLinkRestorePostponed=%d, wait another 1500ms...\n",
3980 pThis->iInstance, pThis->cLinkDownReported, pThis->cLinkRestorePostponed));
3981 pThis->cLinkRestorePostponed++;
3982 }
3983
3984 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
3985}
3986
3987
3988/* -=-=-=-=-=- Debug Info Handler -=-=-=-=-=- */
3989
3990/**
3991 * @callback_method_impl{FNDBGFHANDLERDEV}
3992 */
3993static DECLCALLBACK(void) dpNicR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
3994{
3995 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
3996 bool fRecvBuffer = false;
3997 bool fSendBuffer = false;
3998 unsigned uFreePages;
3999 DP8390CORE *pCore = &pThis->core;
4000 const char *aszModels[] = {"NE1000", "NE2000", "WD8003E", "WD8013E", "3C503"};
4001
4002 /*
4003 * Parse args.
4004 */
4005 if (pszArgs)
4006 {
4007 fRecvBuffer = strstr(pszArgs, "verbose") || strstr(pszArgs, "recvbuf");
4008 fSendBuffer = strstr(pszArgs, "verbose") || strstr(pszArgs, "sendbuf");
4009 }
4010
4011 /*
4012 * Show device information.
4013 */
4014 pHlp->pfnPrintf(pHlp, "DPNIC #%d: %s port=%RTiop IRQ=%u",
4015 pThis->iInstance,
4016 aszModels[pThis->uDevType],
4017 pThis->IOPortBase,
4018 pThis->uIsaIrq);
4019 if (pThis->MemBase)
4020 pHlp->pfnPrintf(pHlp, " mem=%05X-%05X", pThis->MemBase, pThis->MemBase + pThis->cbMemSize - 1);
4021 if (pThis->uIsaDma)
4022 pHlp->pfnPrintf(pHlp, " DMA=%u", pThis->uIsaDma);
4023 pHlp->pfnPrintf(pHlp, " mac-cfg=%RTmac%s %s\n",
4024 &pThis->MacConfigured,
4025 pDevIns->fR0Enabled ? " RZ" : "",
4026 pThis->fDriverAttached ? "attached" : "unattached!");
4027
4028 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_INTERNAL_ERROR); /* Take it here so we know why we're hanging... */
4029 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);
4030
4031 pHlp->pfnPrintf(pHlp, "\nDP3890 NIC Core\n");
4032 pHlp->pfnPrintf(pHlp, " CR=%02X: %s%s%s RD=%d PS=%d\n", pCore->CR,
4033 pCore->cr.STP ? "STP " : "",
4034 pCore->cr.STA ? "STA " : "",
4035 pCore->cr.TXP ? "TXP " : "",
4036 pCore->cr.RD, pCore->cr.PS);
4037 pHlp->pfnPrintf(pHlp, " ISR=%02X: %s%s%s%s%s%s%s%s\n", pCore->ISR,
4038 pCore->isr.PRX ? "PRX " : "",
4039 pCore->isr.PTX ? "PTX " : "",
4040 pCore->isr.RXE ? "RXE " : "",
4041 pCore->isr.TXE ? "TXE " : "",
4042 pCore->isr.OVW ? "OVW " : "",
4043 pCore->isr.CNT ? "CNT " : "",
4044 pCore->isr.RDC ? "RDC " : "",
4045 pCore->isr.RST ? "RST " : "");
4046 pHlp->pfnPrintf(pHlp, " IMR=%02X: %s%s%s%s%s%s%s%s\n", pCore->IMR,
4047 pCore->imr.PRXE ? "PRXE " : "",
4048 pCore->imr.PTXE ? "PTXE " : "",
4049 pCore->imr.RXEE ? "RXEE " : "",
4050 pCore->imr.TXEE ? "TXEE " : "",
4051 pCore->imr.OVWE ? "OVWE " : "",
4052 pCore->imr.CNTE ? "CNTE " : "",
4053 pCore->imr.RDCE ? "RDCE " : "",
4054 pCore->imr.res ? "Reserved bit set!!" : "");
4055 pHlp->pfnPrintf(pHlp, " DCR=%02X: %s%s%s%s%sFT=%d %s\n", pCore->DCR,
4056 pCore->dcr.WTS ? "WTS " : "",
4057 pCore->dcr.BOS ? "BOS " : "",
4058 pCore->dcr.LAS ? "LAS " : "",
4059 pCore->dcr.LS ? "LS " : "",
4060 pCore->dcr.ARM ? "ARM " : "",
4061 pCore->dcr.FT,
4062 pCore->dcr.res ? "Reserved bit set!!" : "");
4063 pHlp->pfnPrintf(pHlp, " TCR=%02X: %sLB=%d %s%s\n", pCore->TCR,
4064 pCore->tcr.CRC ? "CRC " : "",
4065 pCore->tcr.LB,
4066 pCore->tcr.ATD ? "ATD " : "",
4067 pCore->tcr.OFST ? "OFST" : "");
4068 pHlp->pfnPrintf(pHlp, " TSR=%02X: %s%s%s%s%s%s%s%s\n", pCore->TSR,
4069 pCore->tsr.PTX ? "PTX " : "",
4070 pCore->tsr.DFR ? "DFR " : "",
4071 pCore->tsr.COL ? "COL " : "",
4072 pCore->tsr.ABT ? "ABT " : "",
4073 pCore->tsr.CRS ? "CRS " : "",
4074 pCore->tsr.FU ? "FU " : "",
4075 pCore->tsr.CDH ? "CDH " : "",
4076 pCore->tsr.OWC ? "OWC " : "");
4077 pHlp->pfnPrintf(pHlp, " RCR=%02X: %s%s%s%s%s%s\n", pCore->RCR,
4078 pCore->rcr.SEP ? "SEP " : "",
4079 pCore->rcr.AR ? "AR " : "",
4080 pCore->rcr.AB ? "AB " : "",
4081 pCore->rcr.AM ? "AM " : "",
4082 pCore->rcr.PRO ? "PRO " : "",
4083 pCore->rcr.MON ? "MON " : "");
4084 pHlp->pfnPrintf(pHlp, " RSR=%02X: %s%s%s%s%s%s%s%s\n", pCore->RSR,
4085 pCore->rsr.PRX ? "PRX " : "",
4086 pCore->rsr.CRC ? "CRC " : "",
4087 pCore->rsr.FAE ? "FAE " : "",
4088 pCore->rsr.FO ? "FO " : "",
4089 pCore->rsr.MPA ? "MPA " : "",
4090 pCore->rsr.PHY ? "PHY " : "",
4091 pCore->rsr.DIS ? "DIS " : "",
4092 pCore->rsr.DFR ? "DFR " : "");
4093 pHlp->pfnPrintf(pHlp, " ActIntSrc: %02X\n", pCore->ISR & pCore->IMR);
4094 pHlp->pfnPrintf(pHlp, " Receiving: %s%s%s%s%s%s\n",
4095 pCore->rcr.AB ? "Broadcast " : "",
4096 pCore->rcr.AM ? "Multicast " : "",
4097 pCore->rcr.PRO ? "Promiscuous " : "",
4098 pCore->rcr.MON ? "Monitor " : "",
4099 pCore->cr.STA ? "Started " : "Not started ",
4100 pCore->isr.RST ? "Reset!" : "");
4101
4102 /* Dump the currently programmed station address. */
4103 pHlp->pfnPrintf(pHlp, " MAC Addr : %RTmac\n", &pCore->pg1.PAR);
4104
4105 /* Dump the currently programmed multicast filter. */
4106 pHlp->pfnPrintf(pHlp, " Multicast: %02X:%02X:%02X:%02X %02X:%02X:%02X:%02X\n",
4107 pCore->pg1.MAR[0], pCore->pg1.MAR[1], pCore->pg1.MAR[2], pCore->pg1.MAR[3],
4108 pCore->pg1.MAR[4], pCore->pg1.MAR[5], pCore->pg1.MAR[6], pCore->pg1.MAR[7]);
4109
4110 /* Dump the DMA state. */
4111 pHlp->pfnPrintf(pHlp, " Local DMA : TPSR=%02X00 TBCR=%04X CLDA=%04X\n",
4112 pCore->TPSR, pCore->TBCR, pCore->CLDA);
4113 pHlp->pfnPrintf(pHlp, " : PSTART=%02X00 PSTOP=%02X00 CURR=%02X00 BNRY=%02X00\n",
4114 pCore->PSTART, pCore->PSTOP, pCore->CURR, pCore->BNRY);
4115 pHlp->pfnPrintf(pHlp, " Remote DMA: RSAR=%04X RBCR=%04X CRDA=%04X\n",
4116 pCore->RSAR, pCore->RBCR, pCore->CRDA);
4117
4118 /* Try to figure out how much available space there is in the receive ring. */
4119 if (pCore->BNRY <= pCore->CURR)
4120 uFreePages = pCore->PSTOP - pCore->PSTART - (pCore->CURR - pCore->BNRY);
4121 else
4122 uFreePages = pCore->BNRY - pCore->CURR;
4123 pHlp->pfnPrintf(pHlp, " Estimated %u free pages (%u bytes) in receive ring\n", uFreePages, uFreePages * 256);
4124
4125 if (pThis->fMaybeOutOfSpace)
4126 pHlp->pfnPrintf(pHlp, " Waiting for receive space\n");
4127 if (pThis->fLinkTempDown)
4128 {
4129 pHlp->pfnPrintf(pHlp, " Link down count %d\n", pThis->cLinkDownReported);
4130 pHlp->pfnPrintf(pHlp, " Postpone count %d\n", pThis->cLinkRestorePostponed);
4131 }
4132
4133 if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
4134 {
4135 /* Dump the WD specific registers. */
4136 pHlp->pfnPrintf(pHlp, "\nWD80x3 Control Registers\n");
4137 pHlp->pfnPrintf(pHlp, " CTRL1=%02X: %s%s A18-A13=%02X\n", pThis->CTRL1,
4138 pThis->ctrl1.RESET ? "RESET " : "",
4139 pThis->ctrl1.MEME ? "MEME " : "",
4140 pThis->ctrl1.A13_18);
4141 pHlp->pfnPrintf(pHlp, " CTRL2=%02X: %s%s A23-A19=%02X\n", pThis->CTRL2,
4142 pThis->ctrl2.M16 ? "M16 " : "",
4143 pThis->ctrl2.MEMW ? "MEMW " : "",
4144 pThis->ctrl2.A19_23);
4145 }
4146
4147 if (pThis->uDevType == DEV_3C503)
4148 {
4149 PEL_GA pGa = &pThis->ga;
4150
4151 /* Dump the Gate Array state. */
4152 pHlp->pfnPrintf(pHlp, "\n3C503 ASIC Gate Array\n");
4153 pHlp->pfnPrintf(pHlp, " PSTR=%02X00 PSPR=%02X00 cdadr=%04X\n",
4154 pGa->PSTR, pGa->PSTR, pGa->CDADR);
4155 pHlp->pfnPrintf(pHlp, " DQTR=%02X: tb=%d\n", pGa->DQTR,
4156 pGa->dqtr.tb);
4157 pHlp->pfnPrintf(pHlp, " BCFR=%02X PCFR=%02X\n",
4158 pGa->BCFR, pGa->PCFR);
4159 pHlp->pfnPrintf(pHlp, " GACFR=%02X: mbs=%d %s%s%s%s%s\n", pGa->GACFR,
4160 pGa->gacfr.mbs,
4161 pGa->gacfr.rsel ? "rsel " : "",
4162 pGa->gacfr.test ? "test " : "",
4163 pGa->gacfr.ows ? "ows " : "",
4164 pGa->gacfr.tcm ? "tcm " : "",
4165 pGa->gacfr.nim ? "nim " : "");
4166 pHlp->pfnPrintf(pHlp, " GACR=%02X: %s%s%s%s%s%s%s%s\n", pGa->GACR,
4167 pGa->gacr.rst ? "rst " : "",
4168 pGa->gacr.xsel ? "xsel " : "",
4169 pGa->gacr.ealo ? "ealo " : "",
4170 pGa->gacr.eahi ? "eahi " : "",
4171 pGa->gacr.share ? "share " : "",
4172 pGa->gacr.dbsel ? "dbsel " : "",
4173 pGa->gacr.ddir ? "ddir " : "",
4174 pGa->gacr.start ? "start " : "");
4175 pHlp->pfnPrintf(pHlp, " STREG=%02X: rev=%d %s%s%s%s%s\n", pGa->STREG,
4176 pGa->streg.rev,
4177 pGa->streg.dip ? "dip " : "",
4178 pGa->streg.dtc ? "dtc " : "",
4179 pGa->streg.oflw ? "oflw " : "",
4180 pGa->streg.uflw ? "uflw " : "",
4181 pGa->streg.dprdy ? "dprdy " : "");
4182 pHlp->pfnPrintf(pHlp, " IDCFR=%02X: %s%s%s%s%s%s%s\n", pGa->IDCFR,
4183 pGa->idcfr.drq1 ? "drq1 " : "",
4184 pGa->idcfr.drq2 ? "drq2 " : "",
4185 pGa->idcfr.drq3 ? "drq3 " : "",
4186 pGa->idcfr.irq2 ? "irq2 " : "",
4187 pGa->idcfr.irq3 ? "irq3 " : "",
4188 pGa->idcfr.irq4 ? "irq4 " : "",
4189 pGa->idcfr.irq5 ? "irq5 " : "");
4190 pHlp->pfnPrintf(pHlp, " DALSB=%02X DAMSB=%02X addr=%04X\n",
4191 pGa->DALSB, pGa->DAMSB,
4192 RT_MAKE_U16(pGa->DALSB, pGa->DAMSB));
4193 pHlp->pfnPrintf(pHlp, " VPTR0=%02X VPTR1=%02X VPTR2=%02X, VPTR=%X\n",
4194 pGa->VPTR0, pGa->VPTR1, pGa->VPTR2,
4195 (pGa->VPTR2 << 12) | (pGa->VPTR1 << 4) | (pGa->VPTR0 >> 4));
4196
4197
4198
4199 }
4200
4201 /* Dump the beginning of the send buffer. */
4202 if (fSendBuffer)
4203 {
4204 pHlp->pfnPrintf(pHlp, "Send buffer (start at %u):\n", 0);
4205 unsigned dump_end = RT_MIN(0 + 64, sizeof(pThis->abLocalRAM) - 16);
4206 for (unsigned ofs = 0; ofs < dump_end; ofs += 16)
4207 pHlp->pfnPrintf(pHlp, " %04X: %Rhxs\n", ofs, &pThis->abLocalRAM[ofs]);
4208 }
4209
4210 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4211}
4212
4213
4214/* -=-=-=-=-=- Helper(s) -=-=-=-=-=- */
4215
4216
4217static void dpNicR3HardReset(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
4218{
4219 LogFlowFunc(("#%d:\n", pThis->iInstance));
4220
4221 /* Initialize the PROM. Covers both NE1000 and NE2000. */
4222 Assert(sizeof(pThis->MacConfigured) == 6);
4223 memset(pThis->aPROM, 0, sizeof(pThis->aPROM));
4224 /* The first 6 bytes of PROM always contain the configured MAC address. */
4225 memcpy(&pThis->aPROM[0x00], &pThis->MacConfigured, sizeof(pThis->MacConfigured));
4226
4227 if ((pThis->uDevType == DEV_NE1000) || (pThis->uDevType == DEV_NE2000))
4228 {
4229 /* The NE1000/NE2000 repeats the MAC address and also includes BB/WW signature. */
4230 memcpy(&pThis->aPROM[0x10], &pThis->MacConfigured, sizeof(pThis->MacConfigured));
4231 pThis->aPROM[0x0E] = pThis->aPROM[0x0F] = 'W'; /* Word-wide. */
4232 pThis->aPROM[0x1E] = pThis->aPROM[0x1F] = 'B'; /* Byte-wide. */
4233 }
4234 else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
4235 {
4236 /* The WD8003/WD8013 only uses 8 bytes of the PROM. The 7th byte
4237 * contains a board ID and the last byte is a checksum calculated
4238 * such that a two's complement sum of the 8 bytes equals FFh.
4239 */
4240 int i;
4241 uint8_t sum;
4242
4243 /* The board ID is 2 for 8003S, 3 for 8003E, 4 for 8003WT, 5 for 8013EBT. */
4244 pThis->aPROM[0x06] = 3;
4245 if (pThis->uDevType == DEV_WD8013)
4246 pThis->aPROM[0x06] = 5;
4247
4248 for (i = 0, sum = 0; i < 7; ++i)
4249 sum += pThis->aPROM[i];
4250
4251 pThis->aPROM[0x07] = 0xff - sum;
4252 }
4253 else if (pThis->uDevType == DEV_3C503)
4254 {
4255 const uint16_t el_io_bases[] = { 0x2E0, 0x2A0, 0x280, 0x250, 0x350, 0x330, 0x310, 0x300, 0 };
4256 const uint32_t el_mem_bases[] = { 0xDC000, 0xD8000, 0xCC000, 0xC8000, 0 };
4257 int i;
4258
4259 /* Zap the Gate Array state. */
4260 memset(&pThis->ga, 0, sizeof(pThis->ga));
4261
4262 /* Find the BCFR value. */
4263 for (i = 0; el_io_bases[i]; ++i)
4264 {
4265 if (pThis->IOPortBase == el_io_bases[i])
4266 break;
4267 }
4268 /// @todo Make sure we somehow disallow values that a 3C503 can't do
4269 if (i < 8)
4270 pThis->ga.BCFR = 1 << i;
4271
4272 /* Find the PCFR value. */
4273 for (i = 0; el_mem_bases[i]; ++i)
4274 {
4275 if (pThis->MemBase == el_mem_bases[i])
4276 break;
4277 }
4278 /// @todo Make sure we somehow disallow values that a 3C503 can't do
4279 if (i < 4)
4280 pThis->ga.PCFR = RT_BIT(7) >> i;
4281 }
4282
4283 /* Clear the local RAM. */
4284 memset(pThis->abLocalRAM, 0, sizeof(pThis->abLocalRAM));
4285
4286 /* Wipe out all of the DP8390 core state. */
4287 memset(&pThis->core, 0, sizeof(pThis->core));
4288
4289 dp8390CoreReset(pDevIns, pThis);
4290}
4291
4292/**
4293 * Takes down the link temporarily if it's current status is up.
4294 *
4295 * This is used during restore and when replumbing the network link.
4296 *
4297 * The temporary link outage is supposed to indicate to the OS that all network
4298 * connections have been lost and that it for instance is appropriate to
4299 * renegotiate any DHCP lease.
4300 *
4301 * @param pDevIns The device instance data.
4302 * @param pThis The device state.
4303 */
4304static void dp8390TempLinkDown(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
4305{
4306 if (pThis->fLinkUp)
4307 {
4308 pThis->fLinkTempDown = true;
4309 pThis->cLinkDownReported = 0;
4310 pThis->cLinkRestorePostponed = 0;
4311 pThis->Led.Asserted.s.fError = pThis->Led.Actual.s.fError = 1;
4312 int rc = PDMDevHlpTimerSetMillies(pDevIns, pThis->hTimerRestore, pThis->cMsLinkUpDelay);
4313 AssertRC(rc);
4314 }
4315}
4316
4317
4318/* -=-=-=-=-=- Saved State -=-=-=-=-=- */
4319
4320/**
4321 * @callback_method_impl{FNSSMDEVLIVEEXEC, Pass 0 only.}
4322 */
4323static DECLCALLBACK(int) dpNicLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
4324{
4325 RT_NOREF(uPass);
4326 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4327 pDevIns->pHlpR3->pfnSSMPutMem(pSSM, &pThis->MacConfigured, sizeof(pThis->MacConfigured));
4328 return VINF_SSM_DONT_CALL_AGAIN;
4329}
4330
4331
4332/**
4333 * @callback_method_impl{FNSSMDEVSAVEPREP,
4334 * Serializes the receive thread, it may be working inside the critsect.}
4335 */
4336static DECLCALLBACK(int) dpNicSavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4337{
4338 RT_NOREF(pSSM);
4339 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4340
4341 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
4342 AssertRC(rc);
4343 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4344
4345 return VINF_SUCCESS;
4346}
4347
4348
4349/**
4350 * @callback_method_impl{FNSSMDEVSAVEEXEC}
4351 */
4352static DECLCALLBACK(int) dpNicSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4353{
4354 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4355 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
4356
4357 /* Start with saving the generic bits. */
4358 pHlp->pfnSSMPutBool(pSSM, pThis->fLinkUp);
4359 pHlp->pfnSSMPutBool(pSSM, pThis->fNicIrqActive);
4360
4361 /* Continue with DP8390 core. */
4362 pHlp->pfnSSMPutU8(pSSM, pThis->core.CR);
4363 pHlp->pfnSSMPutU8(pSSM, pThis->core.DCR);
4364 pHlp->pfnSSMPutU8(pSSM, pThis->core.ISR);
4365 pHlp->pfnSSMPutU8(pSSM, pThis->core.IMR);
4366 pHlp->pfnSSMPutU8(pSSM, pThis->core.RCR);
4367 pHlp->pfnSSMPutU8(pSSM, pThis->core.RSR);
4368 pHlp->pfnSSMPutU8(pSSM, pThis->core.TCR);
4369 pHlp->pfnSSMPutU8(pSSM, pThis->core.TSR);
4370 pHlp->pfnSSMPutU8(pSSM, pThis->core.NCR);
4371 pHlp->pfnSSMPutU8(pSSM, pThis->core.TPSR);
4372 pHlp->pfnSSMPutU16(pSSM, pThis->core.TBCR);
4373 pHlp->pfnSSMPutU16(pSSM, pThis->core.CLDA);
4374 pHlp->pfnSSMPutU8(pSSM, pThis->core.PSTART);
4375 pHlp->pfnSSMPutU8(pSSM, pThis->core.PSTOP);
4376 pHlp->pfnSSMPutU8(pSSM, pThis->core.CURR);
4377 pHlp->pfnSSMPutU8(pSSM, pThis->core.BNRY);
4378 pHlp->pfnSSMPutU16(pSSM, pThis->core.RSAR);
4379 pHlp->pfnSSMPutU16(pSSM, pThis->core.RBCR);
4380 pHlp->pfnSSMPutU16(pSSM, pThis->core.CRDA);
4381 pHlp->pfnSSMPutU8(pSSM, pThis->core.lnxtpp);
4382 pHlp->pfnSSMPutU8(pSSM, pThis->core.rnxtpp);
4383 pHlp->pfnSSMPutU8(pSSM, pThis->core.CNTR0);
4384 pHlp->pfnSSMPutU8(pSSM, pThis->core.CNTR1);
4385 pHlp->pfnSSMPutU8(pSSM, pThis->core.CNTR2);
4386 pHlp->pfnSSMPutMem(pSSM, &pThis->core.pg1.PAR, sizeof(pThis->core.pg1.PAR));
4387 pHlp->pfnSSMPutMem(pSSM, &pThis->core.pg1.MAR, sizeof(pThis->core.pg1.MAR));
4388 pHlp->pfnSSMPutU8(pSSM, pThis->core.fifo.rp);
4389 pHlp->pfnSSMPutU8(pSSM, pThis->core.fifo.wp);
4390 pHlp->pfnSSMPutMem(pSSM, &pThis->core.fifo.fifo, sizeof(pThis->core.fifo.fifo));
4391
4392 /* Now the WD80x3 state. */
4393 pHlp->pfnSSMPutU8(pSSM, pThis->CTRL1);
4394 pHlp->pfnSSMPutU8(pSSM, pThis->CTRL2);
4395
4396 /* Finally the 3C503-specific state. */
4397 pHlp->pfnSSMPutU8(pSSM, pThis->ga.PSTR);
4398 pHlp->pfnSSMPutU8(pSSM, pThis->ga.PSPR);
4399 pHlp->pfnSSMPutU8(pSSM, pThis->ga.DQTR);
4400 pHlp->pfnSSMPutU8(pSSM, pThis->ga.BCFR);
4401 pHlp->pfnSSMPutU8(pSSM, pThis->ga.PCFR);
4402 pHlp->pfnSSMPutU8(pSSM, pThis->ga.GACFR);
4403 pHlp->pfnSSMPutU8(pSSM, pThis->ga.GACR);
4404 pHlp->pfnSSMPutU8(pSSM, pThis->ga.STREG);
4405 pHlp->pfnSSMPutU8(pSSM, pThis->ga.IDCFR);
4406 pHlp->pfnSSMPutU8(pSSM, pThis->ga.DAMSB);
4407 pHlp->pfnSSMPutU8(pSSM, pThis->ga.DALSB);
4408 pHlp->pfnSSMPutU8(pSSM, pThis->ga.VPTR2);
4409 pHlp->pfnSSMPutU8(pSSM, pThis->ga.VPTR1);
4410 pHlp->pfnSSMPutU8(pSSM, pThis->ga.VPTR0);
4411 pHlp->pfnSSMPutU16(pSSM, pThis->ga.CDADR);
4412 pHlp->pfnSSMPutBool(pSSM, pThis->ga.fGaIrq);
4413
4414 /* Save the configured MAC address. */
4415 pHlp->pfnSSMPutMem(pSSM, &pThis->MacConfigured, sizeof(pThis->MacConfigured));
4416
4417 return VINF_SUCCESS;
4418}
4419
4420
4421/**
4422 * @callback_method_impl{FNSSMDEVLOADPREP},
4423 * Serializes the receive thread, it may be working inside the critsect.}
4424 */
4425static DECLCALLBACK(int) dpNicLoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
4426{
4427 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4428 RT_NOREF(pSSM);
4429
4430 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
4431 AssertRC(rc);
4432
4433 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4434
4435 return rc;
4436}
4437
4438
4439/**
4440 * @callback_method_impl{FNSSMDEVLOADEXEC}
4441 */
4442static DECLCALLBACK(int) dpNicLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
4443{
4444 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4445 PDPNICSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
4446 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
4447
4448 if (SSM_VERSION_MAJOR_CHANGED(uVersion, DPNIC_SAVEDSTATE_VERSION))
4449 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
4450
4451 if (uPass == SSM_PASS_FINAL)
4452 {
4453 /* Restore data, first the generic bits. */
4454 pHlp->pfnSSMGetBool(pSSM, &pThis->fLinkUp);
4455 pHlp->pfnSSMGetBool(pSSM, &pThis->fNicIrqActive);
4456
4457 /* Now the DP8390 core. */
4458 pHlp->pfnSSMGetU8(pSSM, &pThis->core.CR);
4459 pHlp->pfnSSMGetU8(pSSM, &pThis->core.DCR);
4460 pHlp->pfnSSMGetU8(pSSM, &pThis->core.ISR);
4461 pHlp->pfnSSMGetU8(pSSM, &pThis->core.IMR);
4462 pHlp->pfnSSMGetU8(pSSM, &pThis->core.RCR);
4463 pHlp->pfnSSMGetU8(pSSM, &pThis->core.RSR);
4464 pHlp->pfnSSMGetU8(pSSM, &pThis->core.TCR);
4465 pHlp->pfnSSMGetU8(pSSM, &pThis->core.TSR);
4466 pHlp->pfnSSMGetU8(pSSM, &pThis->core.NCR);
4467 pHlp->pfnSSMGetU8(pSSM, &pThis->core.TPSR);
4468 pHlp->pfnSSMGetU16(pSSM, &pThis->core.TBCR);
4469 pHlp->pfnSSMGetU16(pSSM, &pThis->core.CLDA);
4470 pHlp->pfnSSMGetU8(pSSM, &pThis->core.PSTART);
4471 pHlp->pfnSSMGetU8(pSSM, &pThis->core.PSTOP);
4472 pHlp->pfnSSMGetU8(pSSM, &pThis->core.CURR);
4473 pHlp->pfnSSMGetU8(pSSM, &pThis->core.BNRY);
4474 pHlp->pfnSSMGetU16(pSSM, &pThis->core.RSAR);
4475 pHlp->pfnSSMGetU16(pSSM, &pThis->core.RBCR);
4476 pHlp->pfnSSMGetU16(pSSM, &pThis->core.CRDA);
4477 pHlp->pfnSSMGetU8(pSSM, &pThis->core.lnxtpp);
4478 pHlp->pfnSSMGetU8(pSSM, &pThis->core.rnxtpp);
4479 pHlp->pfnSSMGetU8(pSSM, &pThis->core.CNTR0);
4480 pHlp->pfnSSMGetU8(pSSM, &pThis->core.CNTR1);
4481 pHlp->pfnSSMGetU8(pSSM, &pThis->core.CNTR2);
4482 pHlp->pfnSSMGetMem(pSSM, &pThis->core.pg1.PAR, sizeof(pThis->core.pg1.PAR));
4483 pHlp->pfnSSMGetMem(pSSM, &pThis->core.pg1.MAR, sizeof(pThis->core.pg1.MAR));
4484 pHlp->pfnSSMGetU8(pSSM, &pThis->core.fifo.rp);
4485 pHlp->pfnSSMGetU8(pSSM, &pThis->core.fifo.wp);
4486 pHlp->pfnSSMGetMem(pSSM, &pThis->core.fifo.fifo, sizeof(pThis->core.fifo.fifo));
4487
4488 /* WD80x3-specific state. */
4489 pHlp->pfnSSMGetU8(pSSM, &pThis->CTRL1);
4490 pHlp->pfnSSMGetU8(pSSM, &pThis->CTRL2);
4491
4492 /* 3C503-specific state. */
4493 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.PSTR);
4494 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.PSPR);
4495 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.DQTR);
4496 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.BCFR);
4497 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.PCFR);
4498 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.GACFR);
4499 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.GACR);
4500 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.STREG);
4501 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.IDCFR);
4502 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.DAMSB);
4503 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.DALSB);
4504 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.VPTR2);
4505 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.VPTR1);
4506 pHlp->pfnSSMGetU8(pSSM, &pThis->ga.VPTR0);
4507 pHlp->pfnSSMGetU16(pSSM, &pThis->ga.CDADR);
4508 pHlp->pfnSSMGetBool(pSSM, &pThis->ga.fGaIrq);
4509
4510 /* Set IRQ and DMA based on IDCFR if this is a 3C503. */
4511 if (pThis->uDevType == DEV_3C503)
4512 {
4513 pThis->uIsaIrq = elGetIrqFromIdcfr(pThis->ga.IDCFR);
4514 pThis->uElIsaDma = elGetDrqFromIdcfr(pThis->ga.IDCFR);
4515 }
4516 }
4517
4518 /* check config */
4519 RTMAC Mac;
4520 int rc = pHlp->pfnSSMGetMem(pSSM, &Mac, sizeof(Mac));
4521 AssertRCReturn(rc, rc);
4522 if ( memcmp(&Mac, &pThis->MacConfigured, sizeof(Mac))
4523 && (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)) )
4524 LogRel(("DPNIC#%u: The mac address differs: config=%RTmac saved=%RTmac\n", pThis->iInstance, &pThis->MacConfigured, &Mac));
4525
4526 if (uPass == SSM_PASS_FINAL)
4527 {
4528 /* update promiscuous mode. */
4529 if (pThisCC->pDrv)
4530 pThisCC->pDrv->pfnSetPromiscuousMode(pThisCC->pDrv, 0 /* promiscuous enabled */);
4531
4532 /* Indicate link down to the guest OS that all network connections have
4533 been lost, unless we've been teleported here. */
4534 if (!PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns))
4535 dp8390TempLinkDown(pDevIns, pThis);
4536 }
4537
4538 return VINF_SUCCESS;
4539}
4540
4541
4542/* -=-=-=-=-=- DPNICSTATE::INetworkDown -=-=-=-=-=- */
4543
4544/**
4545 * Check if the device/driver can receive data now.
4546 *
4547 * Worker for dpNicNet_WaitReceiveAvail(). This must be called before
4548 * the pfnRecieve() method is called.
4549 *
4550 * @returns VBox status code.
4551 * @param pDevIns The device instance.
4552 * @param pThis The device instance data.
4553 */
4554static int dp8390CanReceive(PPDMDEVINS pDevIns, PDPNICSTATE pThis)
4555{
4556 DP8390CORE *pCore = &pThis->core;
4557 int rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
4558 AssertReleaseRC(rc);
4559
4560 rc = VINF_SUCCESS;
4561
4562 /*
4563 * The card has typically room for several full-size Ethernet frames but
4564 * the buffers can overflow. We cheat a bit and try to hold off when it
4565 * looks like there is temporarily not enough buffer spave.
4566 *
4567 * If the receiver is disabled, accept packets and drop them to avoid
4568 * pile-ups. If the receiver is enabled, take a closer look.
4569 */
4570 if (pCore->cr.STA && !pCore->cr.STP)
4571 {
4572 /* Receiver is enabled. Find out if we're low on buffer space.
4573 * But if the receive buffer isn't at least 4K big (16 pages),
4574 * don't bother. Typically there will be 5K or more in the
4575 * receive buffer.
4576 */
4577 if (pCore->PSTART + 16 <= pCore->PSTOP)
4578 {
4579 uint16_t free_pages;
4580
4581 /* Free space is between BNRY (host's read pointer) and CURR
4582 * (NIC's write pointer).
4583 */
4584 if (pCore->BNRY <= pCore->CURR)
4585 {
4586 /* Free space wraps around. This might technically give
4587 * the wrong answer if the buffer is empty (BNRY = CURR)
4588 * but in that case there's plenty of room anyway.
4589 */
4590 free_pages = pCore->PSTOP - pCore->PSTART - (pCore->CURR - pCore->BNRY);
4591 }
4592 else
4593 {
4594 /* Free space does not wrap. */
4595 free_pages = pCore->BNRY - pCore->CURR;
4596 }
4597 Log2Func(("#%d: %u free pages (%u bytes)\n", pThis->iInstance, free_pages, free_pages * 256));
4598
4599 /* Six pages (1,536 bytes) is enough for the longest standard Ethernet frame
4600 * (1522 bytes including FCS) plus packet header (4 bytes).
4601 */
4602 if (free_pages < 6)
4603 {
4604 rc = VERR_NET_NO_BUFFER_SPACE;
4605 Log2Func(("#%d: Buffer space low, returning %Rrc!\n", pThis->iInstance, rc));
4606 }
4607 }
4608 }
4609
4610 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4611 return rc;
4612}
4613
4614
4615/**
4616 * @interface_method_impl{PDMINETWORKDOWN,pfnWaitReceiveAvail}
4617 */
4618static DECLCALLBACK(int) dpNicNet_WaitReceiveAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL cMillies)
4619{
4620 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkDown);
4621 PPDMDEVINS pDevIns = pThisCC->pDevIns;
4622 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4623
4624 int rc = dp8390CanReceive(pDevIns, pThis);
4625 if (RT_SUCCESS(rc))
4626 {
4627 STAM_COUNTER_INC(&pThis->StatRxCanReceiveNow);
4628 return VINF_SUCCESS;
4629 }
4630 if (RT_UNLIKELY(cMillies == 0))
4631 {
4632 STAM_COUNTER_INC(&pThis->StatRxCannotReceiveNow);
4633 return VINF_SUCCESS; //VERR_NET_NO_BUFFER_SPACE;
4634 }
4635
4636 rc = VERR_INTERRUPTED;
4637 ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, true);
4638 STAM_PROFILE_START(&pThis->StatRxOverflow, a);
4639 VMSTATE enmVMState;
4640 while (RT_LIKELY( (enmVMState = PDMDevHlpVMState(pDevIns)) == VMSTATE_RUNNING
4641 || enmVMState == VMSTATE_RUNNING_LS))
4642 {
4643 int rc2 = dp8390CanReceive(pDevIns, pThis);
4644 if (RT_SUCCESS(rc2))
4645 {
4646 rc = VINF_SUCCESS;
4647 break;
4648 }
4649 if (cMillies > 666)
4650 cMillies = 666;
4651 LogFlowFunc(("Waiting cMillies=%u...\n", cMillies));
4652
4653 rc2 = RTSemEventWait(pThis->hEventOutOfRxSpace, cMillies);
4654//LogRelFunc(("RTSemEventWait: rc=%Rrc\n", rc2));
4655// if (rc2 == VERR_TIMEOUT)
4656// break;
4657 }
4658 STAM_PROFILE_STOP(&pThis->StatRxOverflow, a);
4659 ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, false);
4660
4661 return rc;
4662}
4663
4664
4665/**
4666 * @interface_method_impl{PDMINETWORKDOWN,pfnReceive}
4667 */
4668static DECLCALLBACK(int) dpNicNet_Receive(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
4669{
4670 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkDown);
4671 PPDMDEVINS pDevIns = pThisCC->pDevIns;
4672 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4673 int rc;
4674
4675 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
4676 rc = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
4677 AssertReleaseRC(rc);
4678
4679 if (cb > 50) /* unqualified guess */
4680 pThis->Led.Asserted.s.fReading = pThis->Led.Actual.s.fReading = 1;
4681 dp8390CoreReceiveLocked(pDevIns, pThis, (const uint8_t *)pvBuf, cb);
4682 pThis->Led.Actual.s.fReading = 0;
4683
4684 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4685 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
4686
4687 return VINF_SUCCESS;
4688}
4689
4690
4691/**
4692 * @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
4693 */
4694static DECLCALLBACK(void) dpNicNet_XmitPending(PPDMINETWORKDOWN pInterface)
4695{
4696 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkDown);
4697 PPDMDEVINS pDevIns = pThisCC->pDevIns;
4698 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4699 dp8390CoreXmitPacket(pDevIns, pThis, true /*fOnWorkerThread*/);
4700}
4701
4702
4703/* -=-=-=-=-=- DPNICSTATE::INetworkConfig -=-=-=-=-=- */
4704
4705/**
4706 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetMac}
4707 */
4708static DECLCALLBACK(int) dpNicGetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
4709{
4710 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkConfig);
4711 PPDMDEVINS pDevIns = pThisCC->pDevIns;
4712 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4713
4714 LogFlowFunc(("#%d\n", pThis->iInstance));
4715 /// @todo This is broken!! We can't properly get the MAC address set by the guest
4716#if 0
4717 memcpy(pMac, pThis->core.pg1.PAR, sizeof(*pMac));
4718#else
4719 memcpy(pMac, pThis->aPROM, sizeof(*pMac));
4720#endif
4721 return VINF_SUCCESS;
4722}
4723
4724
4725/**
4726 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetLinkState}
4727 */
4728static DECLCALLBACK(PDMNETWORKLINKSTATE) dpNicGetLinkState(PPDMINETWORKCONFIG pInterface)
4729{
4730 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkConfig);
4731 PPDMDEVINS pDevIns = pThisCC->pDevIns;
4732 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4733
4734 if (pThis->fLinkUp && !pThis->fLinkTempDown)
4735 return PDMNETWORKLINKSTATE_UP;
4736 if (!pThis->fLinkUp)
4737 return PDMNETWORKLINKSTATE_DOWN;
4738 if (pThis->fLinkTempDown)
4739 return PDMNETWORKLINKSTATE_DOWN_RESUME;
4740 AssertMsgFailed(("Invalid link state!\n"));
4741 return PDMNETWORKLINKSTATE_INVALID;
4742}
4743
4744
4745/**
4746 * @interface_method_impl{PDMINETWORKCONFIG,pfnSetLinkState}
4747 */
4748static DECLCALLBACK(int) dpNicSetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
4749{
4750 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, INetworkConfig);
4751 PPDMDEVINS pDevIns = pThisCC->pDevIns;
4752 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4753 bool fLinkUp;
4754
4755 LogFlowFunc(("#%d\n", pThis->iInstance));
4756 AssertMsgReturn(enmState > PDMNETWORKLINKSTATE_INVALID && enmState <= PDMNETWORKLINKSTATE_DOWN_RESUME,
4757 ("Invalid link state: enmState=%d\n", enmState), VERR_INVALID_PARAMETER);
4758
4759 if (enmState == PDMNETWORKLINKSTATE_DOWN_RESUME)
4760 {
4761 dp8390TempLinkDown(pDevIns, pThis);
4762 /*
4763 * Note that we do not notify the driver about the link state change because
4764 * the change is only temporary and can be disregarded from the driver's
4765 * point of view (see @bugref{7057}).
4766 */
4767 return VINF_SUCCESS;
4768 }
4769 /* has the state changed? */
4770 fLinkUp = enmState == PDMNETWORKLINKSTATE_UP;
4771 if (pThis->fLinkUp != fLinkUp)
4772 {
4773 pThis->fLinkUp = fLinkUp;
4774 if (fLinkUp)
4775 {
4776 /* Connect with a configured delay. */
4777 pThis->fLinkTempDown = true;
4778 pThis->cLinkDownReported = 0;
4779 pThis->cLinkRestorePostponed = 0;
4780 pThis->Led.Asserted.s.fError = pThis->Led.Actual.s.fError = 1;
4781 int rc = PDMDevHlpTimerSetMillies(pDevIns, pThis->hTimerRestore, pThis->cMsLinkUpDelay);
4782 AssertRC(rc);
4783 }
4784 else
4785 {
4786 /* Disconnect. */
4787 pThis->cLinkDownReported = 0;
4788 pThis->cLinkRestorePostponed = 0;
4789 pThis->Led.Asserted.s.fError = pThis->Led.Actual.s.fError = 1;
4790 }
4791 Assert(!PDMDevHlpCritSectIsOwner(pDevIns, &pThis->CritSect));
4792 if (pThisCC->pDrv)
4793 pThisCC->pDrv->pfnNotifyLinkChanged(pThisCC->pDrv, enmState);
4794 }
4795 return VINF_SUCCESS;
4796}
4797
4798
4799/* -=-=-=-=-=- DPNICSTATE::ILeds (LUN#0) -=-=-=-=-=- */
4800
4801/**
4802 * @interface_method_impl{PDMILEDPORTS,pfnQueryStatusLed}
4803 */
4804static DECLCALLBACK(int) dpNicQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
4805{
4806 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, ILeds);
4807 PPDMDEVINS pDevIns = pThisCC->pDevIns;
4808 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4809 if (iLUN == 0)
4810 {
4811 *ppLed = &pThis->Led;
4812 return VINF_SUCCESS;
4813 }
4814 return VERR_PDM_LUN_NOT_FOUND;
4815}
4816
4817
4818/* -=-=-=-=-=- DPNICSTATE::IBase (LUN#0) -=-=-=-=-=- */
4819
4820/**
4821 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
4822 */
4823static DECLCALLBACK(void *) dpNicQueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
4824{
4825 PDPNICSTATECC pThisCC = RT_FROM_MEMBER(pInterface, DPNICSTATECC, IBase);
4826 Assert(&pThisCC->IBase == pInterface);
4827 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
4828 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKDOWN, &pThisCC->INetworkDown);
4829 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThisCC->INetworkConfig);
4830 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThisCC->ILeds);
4831 return NULL;
4832}
4833
4834
4835/* -=-=-=-=-=- PDMDEVREG -=-=-=-=-=- */
4836
4837/**
4838 * @interface_method_impl{PDMDEVREG,pfnPowerOff}
4839 */
4840static DECLCALLBACK(void) dpNicR3PowerOff(PPDMDEVINS pDevIns)
4841{
4842 /* Poke thread waiting for buffer space. */
4843 dp8390R3WakeupReceive(pDevIns);
4844}
4845
4846
4847/**
4848 * @interface_method_impl{PDMDEVREG,pfnDetach}
4849 *
4850 * One port on the network card has been disconnected from the network.
4851 */
4852static DECLCALLBACK(void) dpNicR3Detach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
4853{
4854 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4855 PDPNICSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
4856 RT_NOREF(fFlags);
4857 LogFlowFunc(("#%d\n", pThis->iInstance));
4858
4859 AssertLogRelReturnVoid(iLUN == 0);
4860
4861 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
4862 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);
4863
4864 /*
4865 * Zero some important members.
4866 */
4867 pThis->fDriverAttached = false;
4868 pThisCC->pDrvBase = NULL;
4869 pThisCC->pDrv = NULL;
4870
4871 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4872}
4873
4874
4875/**
4876 * @interface_method_impl{PDMDEVREG,pfnAttach}
4877 * One port on the network card has been connected to a network.
4878 */
4879static DECLCALLBACK(int) dpNicR3Attach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
4880{
4881 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4882 PDPNICSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
4883 RT_NOREF(fFlags);
4884 LogFlowFunc(("#%d\n", pThis->iInstance));
4885
4886 AssertLogRelReturn(iLUN == 0, VERR_PDM_NO_SUCH_LUN);
4887
4888 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, &pThis->CritSect, VERR_SEM_BUSY);
4889 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, &pThis->CritSect, rcLock);
4890
4891 /*
4892 * Attach the driver.
4893 */
4894 int rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
4895 if (RT_SUCCESS(rc))
4896 {
4897 pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
4898 AssertMsgStmt(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
4899 rc = VERR_PDM_MISSING_INTERFACE_BELOW);
4900 pThis->fDriverAttached = true;
4901 }
4902 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
4903 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
4904 {
4905 /* This should never happen because this function is not called
4906 * if there is no driver to attach! */
4907 LogFunc(("#%d No attached driver!\n", pThis->iInstance));
4908 }
4909
4910 /*
4911 * Temporarily drop the link if it was up so that the guest
4912 * will know that we have changed the configuration of the
4913 * network card
4914 */
4915 if (RT_SUCCESS(rc))
4916 dp8390TempLinkDown(pDevIns, pThis);
4917
4918 PDMDevHlpCritSectLeave(pDevIns, &pThis->CritSect);
4919 return rc;
4920}
4921
4922
4923/**
4924 * @interface_method_impl{PDMDEVREG,pfnSuspend}
4925 */
4926static DECLCALLBACK(void) dpNicR3Suspend(PPDMDEVINS pDevIns)
4927{
4928 /* Poke thread waiting for buffer space. */
4929 dp8390R3WakeupReceive(pDevIns);
4930}
4931
4932
4933/**
4934 * @interface_method_impl{PDMDEVREG,pfnReset}
4935 */
4936static DECLCALLBACK(void) dpNicR3Reset(PPDMDEVINS pDevIns)
4937{
4938 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4939 LogFlowFunc(("#%d\n", pThis->iInstance));
4940 if (pThis->fLinkTempDown)
4941 {
4942 pThis->cLinkDownReported = 0x1000;
4943 pThis->cLinkRestorePostponed = 0x1000;
4944 PDMDevHlpTimerStop(pDevIns, pThis->hTimerRestore);
4945 dpNicR3TimerRestore(pDevIns, pThis->hTimerRestore, pThis);
4946 }
4947
4948 dpNicR3HardReset(pDevIns, pThis);
4949}
4950
4951
4952/**
4953 * @interface_method_impl{PDMDEVREG,pfnRelocate}
4954 */
4955static DECLCALLBACK(void) dpNicR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
4956{
4957 PDPNICSTATERC pThisRC = PDMINS_2_DATA_RC(pDevIns, PDPNICSTATERC);
4958 pThisRC->pDrv += offDelta;
4959}
4960
4961
4962/**
4963 * @interface_method_impl{PDMDEVREG,pfnDestruct}
4964 */
4965static DECLCALLBACK(int) dpNicR3Destruct(PPDMDEVINS pDevIns)
4966{
4967 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
4968 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4969
4970 if (PDMDevHlpCritSectIsInitialized(pDevIns, &pThis->CritSect))
4971 {
4972 RTSemEventSignal(pThis->hEventOutOfRxSpace);
4973 RTSemEventDestroy(pThis->hEventOutOfRxSpace);
4974 pThis->hEventOutOfRxSpace = NIL_RTSEMEVENT;
4975 PDMDevHlpCritSectDelete(pDevIns, &pThis->CritSect);
4976 }
4977 return VINF_SUCCESS;
4978}
4979
4980
4981/**
4982 * @interface_method_impl{PDMDEVREG,pfnConstruct}
4983 */
4984static DECLCALLBACK(int) dpNicR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
4985{
4986 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
4987 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
4988 PDPNICSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDPNICSTATECC);
4989 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
4990 PPDMIBASE pBase;
4991 char szTmp[128];
4992 int rc;
4993
4994 /*
4995 * Init what's required to make the destructor safe.
4996 */
4997 pThis->iInstance = iInstance;
4998 pThis->hEventOutOfRxSpace = NIL_RTSEMEVENT;
4999 pThis->hIoPortsNic = NIL_IOMIOPORTHANDLE;
5000 pThis->hIoPortsCore = NIL_IOMIOPORTHANDLE;
5001 pThisCC->pDevIns = pDevIns;
5002
5003 /*
5004 * Validate configuration.
5005 */
5006 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "MAC|CableConnected|Port|MemBase|IRQ|DMA|DeviceType|LinkUpDelay|LineSpeed", "");
5007
5008 /*
5009 * Read the configuration.
5010 */
5011 rc = pHlp->pfnCFGMQueryBytes(pCfg, "MAC", &pThis->MacConfigured, sizeof(pThis->MacConfigured));
5012 if (RT_FAILURE(rc))
5013 return PDMDEV_SET_ERROR(pDevIns, rc,
5014 N_("Configuration error: Failed to get the \"MAC\" value"));
5015 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "CableConnected", &pThis->fLinkUp, true);
5016 if (RT_FAILURE(rc))
5017 return PDMDEV_SET_ERROR(pDevIns, rc,
5018 N_("Configuration error: Failed to get the \"CableConnected\" value"));
5019
5020 /*
5021 * Determine the model.
5022 */
5023 char szDeviceType[16];
5024 rc = pHlp->pfnCFGMQueryStringDef(pCfg, "DeviceType", &szDeviceType[0], sizeof(szDeviceType), "NE2000");
5025 if (RT_FAILURE(rc))
5026 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Querying \"ChipType\" as string failed"));
5027
5028 if (!strcmp(szDeviceType, "NE1000"))
5029 pThis->uDevType = DEV_NE1000; /* Novell NE1000. */
5030 else if (!strcmp(szDeviceType, "NE2000"))
5031 pThis->uDevType = DEV_NE2000; /* Novell NE2000. */
5032 else if (!strcmp(szDeviceType, "WD8003"))
5033 pThis->uDevType = DEV_WD8003; /* WD EtherCard Plus. */
5034 else if (!strcmp(szDeviceType, "WD8013"))
5035 pThis->uDevType = DEV_WD8013; /* WD EtherCard Plus 16. */
5036 else if (!strcmp(szDeviceType, "3C503"))
5037 pThis->uDevType = DEV_3C503; /* 3Com 3C503 EtherLink II. */
5038 else
5039 return PDMDevHlpVMSetError(pDevIns, VERR_PDM_DEVINS_UNKNOWN_CFG_VALUES, RT_SRC_POS,
5040 N_("Configuration error: The \"DeviceType\" value \"%s\" is unsupported"),
5041 szDeviceType);
5042
5043
5044 /*
5045 * Default resource assignments depend on the device type.
5046 */
5047 unsigned uDefIoPort = 0; /* To be overridden. */
5048 unsigned uDefIrq = 0;
5049 unsigned uDefDma = 0; /* Default to no DMA. */
5050 unsigned uDefMemBase = 0; /* Default to no shared memory. */
5051
5052 if ((pThis->uDevType == DEV_NE1000) || (pThis->uDevType == DEV_NE2000))
5053 {
5054 uDefIoPort = 0x300;
5055 uDefIrq = 3;
5056 }
5057 else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
5058 {
5059 uDefIoPort = 0x280;
5060 uDefIrq = 3;
5061 uDefMemBase = 0xd0000;
5062 pThis->cbMemSize = _8K;
5063 if (pThis->uDevType == DEV_WD8013)
5064 pThis->cbMemSize = _16K;
5065 }
5066 else if (pThis->uDevType == DEV_3C503)
5067 {
5068 uDefIoPort = 0x300;
5069 uDefIrq = 3;
5070 uDefDma = 1;
5071 uDefMemBase = 0xdc000;
5072 pThis->cbMemSize = _8K;
5073 }
5074
5075 /*
5076 * Process ISA configuration options.
5077 */
5078 rc = pHlp->pfnCFGMQueryPortDef(pCfg, "Port", &pThis->IOPortBase, uDefIoPort);
5079 if (RT_FAILURE(rc))
5080 return PDMDEV_SET_ERROR(pDevIns, rc,
5081 N_("Configuration error: Failed to get the \"Port\" value"));
5082
5083 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "IRQ", &pThis->uIsaIrq, uDefIrq);
5084 if (RT_FAILURE(rc))
5085 return PDMDEV_SET_ERROR(pDevIns, rc,
5086 N_("Configuration error: Failed to get the \"IRQ\" value"));
5087
5088 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "DMA", &pThis->uIsaDma, uDefDma);
5089 if (RT_FAILURE(rc))
5090 return PDMDEV_SET_ERROR(pDevIns, rc,
5091 N_("Configuration error: Failed to get the \"DMA\" value"));
5092
5093 rc = pHlp->pfnCFGMQueryGCPtrDef(pCfg, "MemBase", &pThis->MemBase, uDefMemBase);
5094 if (RT_FAILURE(rc))
5095 return PDMDEV_SET_ERROR(pDevIns, rc,
5096 N_("Configuration error: Failed to get the \"MemBase\" value"));
5097
5098
5099
5100 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "LinkUpDelay", (uint32_t*)&pThis->cMsLinkUpDelay, 5000); /* ms */
5101 if (RT_FAILURE(rc))
5102 return PDMDEV_SET_ERROR(pDevIns, rc,
5103 N_("Configuration error: Failed to get the value of 'LinkUpDelay'"));
5104 Assert(pThis->cMsLinkUpDelay <= 300000); /* less than 5 minutes */
5105 if (pThis->cMsLinkUpDelay > 5000 || pThis->cMsLinkUpDelay < 100)
5106 {
5107 LogRel(("DPNIC#%d WARNING! Link up delay is set to %u seconds!\n",
5108 iInstance, pThis->cMsLinkUpDelay / 1000));
5109 }
5110 LogFunc(("#%d Link up delay is set to %u seconds\n",
5111 iInstance, pThis->cMsLinkUpDelay / 1000));
5112
5113
5114 /*
5115 * Initialize data (most of it anyway).
5116 */
5117 pThis->Led.u32Magic = PDMLED_MAGIC;
5118 /* IBase */
5119 pThisCC->IBase.pfnQueryInterface = dpNicQueryInterface;
5120 /* INetworkPort */
5121 pThisCC->INetworkDown.pfnWaitReceiveAvail = dpNicNet_WaitReceiveAvail;
5122 pThisCC->INetworkDown.pfnReceive = dpNicNet_Receive;
5123 pThisCC->INetworkDown.pfnXmitPending = dpNicNet_XmitPending;
5124 /* INetworkConfig */
5125 pThisCC->INetworkConfig.pfnGetMac = dpNicGetMac;
5126 pThisCC->INetworkConfig.pfnGetLinkState = dpNicGetLinkState;
5127 pThisCC->INetworkConfig.pfnSetLinkState = dpNicSetLinkState;
5128 /* ILeds */
5129 pThisCC->ILeds.pfnQueryStatusLed = dpNicQueryStatusLed;
5130
5131 pThis->hIoPortsCore = NIL_IOMIOPORTHANDLE;
5132 pThis->hIoPortsNic = NIL_IOMIOPORTHANDLE;
5133 pThis->hSharedMem = NIL_IOMMMIOHANDLE;
5134
5135 /*
5136 * We use our own critical section (historical reasons).
5137 */
5138 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSect, RT_SRC_POS, "DPNIC#%u", iInstance);
5139 AssertRCReturn(rc, rc);
5140 rc = PDMDevHlpSetDeviceCritSect(pDevIns, &pThis->CritSect);
5141 AssertRCReturn(rc, rc);
5142
5143 rc = RTSemEventCreate(&pThis->hEventOutOfRxSpace);
5144 AssertRCReturn(rc, rc);
5145
5146 /*
5147 * Register ISA I/O ranges. This depends on the device type.
5148 */
5149 if ((pThis->uDevType == DEV_NE1000) || (pThis->uDevType == DEV_NE2000))
5150 {
5151 /* The NE1000 and NE2000 map the DP8390 at the beginning of the port range,
5152 * followed by the data/reset ports.
5153 */
5154 rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase, 0x10 /*cPorts*/, dp8390CoreIOPortWrite, dp8390CoreIOPortRead,
5155 "DP8390-Core", NULL /*paExtDesc*/, &pThis->hIoPortsCore);
5156 if (RT_FAILURE(rc))
5157 return rc;
5158 rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase + 0x10, 0x10 /*cPorts*/, neIOPortWrite, neIOPortRead,
5159 "DPNIC-NE", NULL /*paExtDesc*/, &pThis->hIoPortsNic);
5160 if (RT_FAILURE(rc))
5161 return rc;
5162 }
5163 else if ((pThis->uDevType == DEV_WD8003) || (pThis->uDevType == DEV_WD8013))
5164 {
5165 /* The WD8003 and WD8013 map the DP8390 at the end of the port range
5166 * (16 bytes into it). The first 8 bytes of the range are largely unused
5167 * while the second 8 bytes map the PROM.
5168 */
5169 rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase, 0x10 /*cPorts*/, wdIOPortWrite, wdIOPortRead,
5170 "DPNIC-WD", NULL /*paExtDesc*/, &pThis->hIoPortsNic);
5171 if (RT_FAILURE(rc))
5172 return rc;
5173 rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase + 0x10, 0x10 /*cPorts*/, dp8390CoreIOPortWrite, dp8390CoreIOPortRead,
5174 "DP8390-Core", NULL /*paExtDesc*/, &pThis->hIoPortsCore);
5175 if (RT_FAILURE(rc))
5176 return rc;
5177
5178 /*
5179 * Shared memory MMIO area. This is rather lame.
5180 */
5181 rc = PDMDevHlpMmioCreateExAndMap(pDevIns, pThis->MemBase, pThis->cbMemSize,
5182 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU | IOMMMIO_FLAGS_ABS,
5183 NULL /*pPciDev*/, UINT32_MAX /*iPciRegion*/,
5184 wdMemWrite, wdMemRead, NULL /*wdMmioFill*/, NULL /*pvUser*/,
5185 "DPNIC - WD Shared RAM", &pThis->hSharedMem);
5186 AssertRCReturn(rc, rc);
5187
5188 /* Hack to make WD drivers happy. */
5189 memcpy(&pThis->MacConfigured, "\x00\x00\xC0", 3);
5190 }
5191 else if (pThis->uDevType == DEV_3C503)
5192 {
5193 /* The 3C503 maps the DP8390 at the base I/O address, except the first
5194 * or second 16 bytes of PROM can be mapped into the same space. The
5195 * custom Gate Array is mapped at I/O base + 400h.
5196 */
5197 rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase, 0x10 /*cPorts*/, dp8390CoreIOPortWrite, dp8390CoreIOPortRead,
5198 "DP8390-Core", NULL /*paExtDesc*/, &pThis->hIoPortsCore);
5199 if (RT_FAILURE(rc))
5200 return rc;
5201
5202 rc = PDMDevHlpIoPortCreateAndMap(pDevIns, pThis->IOPortBase + 0x400, 0x10 /*cPorts*/, elIOPortWrite, elIOPortRead,
5203 "DPNIC-EL", NULL /*paExtDesc*/, &pThis->hIoPortsNic);
5204 if (RT_FAILURE(rc))
5205 return rc;
5206
5207 /*
5208 * Shared memory MMIO area. The same lame thing.
5209 */
5210 rc = PDMDevHlpMmioCreateExAndMap(pDevIns, pThis->MemBase, pThis->cbMemSize,
5211 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU | IOMMMIO_FLAGS_ABS,
5212 NULL /*pPciDev*/, UINT32_MAX /*iPciRegion*/,
5213 elMemWrite, elMemRead, NULL /*elMmioFill*/, NULL /*pvUser*/,
5214 "DPNIC - 3C503 Shared RAM", &pThis->hSharedMem);
5215 AssertRCReturn(rc, rc);
5216
5217 /*
5218 * Register DMA channel.
5219 */
5220 if ((pThis->uIsaDma >= ELNKII_MIN_VALID_DMA) && (pThis->uIsaDma <= ELNKII_MAX_VALID_DMA))
5221 {
5222 rc = PDMDevHlpDMARegister(pDevIns, pThis->uIsaDma, elnk3R3DMAXferHandler, pThis);
5223 if (RT_FAILURE(rc))
5224 return rc;
5225 LogRel(("DPNIC#%d: Enabling 3C503 DMA channel %u\n", iInstance, pThis->uIsaDma));
5226 }
5227 else
5228 LogRel(("DPNIC#%d: Disabling 3C503 DMA\n", iInstance));
5229
5230 /* Hack to make 3C503 diagnostics happy. */
5231 memcpy(&pThis->MacConfigured, "\x02\x60\x8C", 3);
5232 }
5233
5234
5235 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, dpNicR3TimerRestore, NULL, TMTIMER_FLAGS_NO_CRIT_SECT | TMTIMER_FLAGS_NO_RING0,
5236 "DPNIC Link Restore Timer", &pThis->hTimerRestore);
5237 if (RT_FAILURE(rc))
5238 return rc;
5239
5240 rc = PDMDevHlpSSMRegisterEx(pDevIns, DPNIC_SAVEDSTATE_VERSION, sizeof(*pThis), NULL,
5241 NULL, dpNicLiveExec, NULL,
5242 dpNicSavePrep, dpNicSaveExec, NULL,
5243 dpNicLoadPrep, dpNicLoadExec, NULL);
5244 if (RT_FAILURE(rc))
5245 return rc;
5246
5247 /*
5248 * Create the transmit notifier signaller.
5249 */
5250 rc = PDMDevHlpTaskCreate(pDevIns, PDMTASK_F_RZ, "DPNIC-Xmit", dpNicR3XmitTaskCallback, NULL /*pvUser*/, &pThis->hXmitTask);
5251 if (RT_FAILURE(rc))
5252 return rc;
5253
5254 /*
5255 * Create the RX notifier signaller.
5256 */
5257 rc = PDMDevHlpTaskCreate(pDevIns, PDMTASK_F_RZ, "DPNIC-Rcv", dpNicR3CanRxTaskCallback, NULL /*pvUser*/, &pThis->hCanRxTask);
5258 if (RT_FAILURE(rc))
5259 return rc;
5260
5261 /*
5262 * Register the info item.
5263 */
5264 RTStrPrintf(szTmp, sizeof(szTmp), "dpnic%d", pThis->iInstance);
5265 PDMDevHlpDBGFInfoRegister(pDevIns, szTmp, "dpnic info", dpNicR3Info);
5266
5267 /*
5268 * Attach status driver (optional).
5269 */
5270 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThisCC->IBase, &pBase, "Status Port");
5271 if (RT_SUCCESS(rc))
5272 pThisCC->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
5273 else if ( rc != VERR_PDM_NO_ATTACHED_DRIVER
5274 && rc != VERR_PDM_CFG_MISSING_DRIVER_NAME)
5275 {
5276 AssertMsgFailed(("Failed to attach to status driver. rc=%Rrc\n", rc));
5277 return rc;
5278 }
5279
5280 /*
5281 * Attach driver.
5282 */
5283 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
5284 if (RT_SUCCESS(rc))
5285 {
5286 pThisCC->pDrv = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
5287 AssertMsgReturn(pThisCC->pDrv, ("Failed to obtain the PDMINETWORKUP interface!\n"),
5288 VERR_PDM_MISSING_INTERFACE_BELOW);
5289 pThis->fDriverAttached = true;
5290 }
5291 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
5292 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
5293 {
5294 /* No error! */
5295 LogFunc(("No attached driver!\n"));
5296 }
5297 else
5298 return rc;
5299
5300 /*
5301 * Reset the device state. (Do after attaching.)
5302 */
5303 dpNicR3HardReset(pDevIns, pThis);
5304
5305 /*
5306 * Register statistics counters.
5307 */
5308 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data received", "/Public/Net/DPNIC%u/BytesReceived", iInstance);
5309 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data transmitted", "/Public/Net/DPNIC%u/BytesTransmitted", iInstance);
5310
5311 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data received", "/Devices/DPNIC%d/ReceiveBytes", iInstance);
5312 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES, "Amount of data transmitted", "/Devices/DPNIC%d/TransmitBytes", iInstance);
5313
5314#ifdef VBOX_WITH_STATISTICS
5315 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOReadRZ, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in RZ", "/Devices/DPNIC%d/IO/ReadRZ", iInstance);
5316 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOReadR3, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in R3", "/Devices/DPNIC%d/IO/ReadR3", iInstance);
5317 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOWriteRZ, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in RZ", "/Devices/DPNIC%d/IO/WriteRZ", iInstance);
5318 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatIOWriteR3, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in R3", "/Devices/DPNIC%d/IO/WriteR3", iInstance);
5319 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceive, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling receive", "/Devices/DPNIC%d/Receive", iInstance);
5320 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxOverflow, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_OCCURENCE, "Profiling RX overflows", "/Devices/DPNIC%d/RxOverflow", iInstance);
5321 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxOverflowWakeup, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES , "Nr of RX overflow wakeups", "/Devices/DPNIC%d/RxOverflowWakeup", iInstance);
5322 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxCanReceiveNow, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES , "Can receive immediately", "/Devices/DPNIC%d/RxCanReceiveNow", iInstance);
5323 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatRxCannotReceiveNow, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES , "Cannot receive, not waiting", "/Devices/DPNIC%d/RxCannotReceiveNow", iInstance);
5324 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitRZ, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling transmits in RZ", "/Devices/DPNIC%d/Transmit/TotalRZ", iInstance);
5325 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitR3, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling transmits in R3", "/Devices/DPNIC%d/Transmit/TotalR3", iInstance);
5326 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitSendRZ, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in RZ", "/Devices/DPNIC%d/Transmit/SendRZ", iInstance);
5327 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitSendR3, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in R3", "/Devices/DPNIC%d/Transmit/SendR3", iInstance);
5328
5329 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatInterrupt, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, "Profiling interrupt checks", "/Devices/DPNIC%d/UpdateIRQ", iInstance);
5330
5331 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktMonitor, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Dropped packet, monitor mode", "/Devices/DPNIC%d/DropPktMonitor", iInstance);
5332 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktRcvrDis, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Dropped packet, receiver not enabled", "/Devices/DPNIC%d/DropPktRcvrDis", iInstance);
5333 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktVeryShort, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Dropped packet less than 8 bytes long", "/Devices/DPNIC%d/DropPktVeryShort", iInstance);
5334 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktVMNotRunning,STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Dropped packet, VM not running", "/Devices/DPNIC%d/DropPktVMNotRunning", iInstance);
5335 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktNoLink, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Dropped packet, no link", "/Devices/DPNIC%d/DropPktNoLink", iInstance);
5336 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktNoMatch, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Dropped packet, address match reject", "/Devices/DPNIC%d/DropPktNoMatch", iInstance);
5337 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatDropPktNoBuffer, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, "Dropped packet, DP8390 buffer overflow", "/Devices/DPNIC%d/DropPktNoBuffer", iInstance);
5338#endif /* VBOX_WITH_STATISTICS */
5339
5340 return VINF_SUCCESS;
5341}
5342
5343#else
5344
5345/**
5346 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
5347 */
5348static DECLCALLBACK(int) dpNicRZConstruct(PPDMDEVINS pDevIns)
5349{
5350 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
5351 PDPNICSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PDPNICSTATE);
5352
5353 /* Critical section setup: */
5354 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, &pThis->CritSect);
5355 AssertRCReturn(rc, rc);
5356
5357 /* NIC-specific ISA I/O ports: */
5358 if (pThis->hIoPortsNic != NIL_IOMIOPORTHANDLE)
5359 {
5360 switch (pThis->uDevType)
5361 {
5362 case DEV_NE1000:
5363 case DEV_NE2000:
5364 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsNic, neIOPortWrite, neIOPortRead, NULL /*pvUser*/);
5365 AssertRCReturn(rc, rc);
5366 break;
5367 case DEV_WD8003:
5368 case DEV_WD8013:
5369 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsNic, wdIOPortWrite, wdIOPortRead, NULL /*pvUser*/);
5370 AssertRCReturn(rc, rc);
5371 break;
5372 case DEV_3C503:
5373 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsNic, elIOPortWrite, elIOPortRead, NULL /*pvUser*/);
5374 AssertRCReturn(rc, rc);
5375 break;
5376 default:
5377 /* Must not happen. */
5378 return VERR_INTERNAL_ERROR;
5379 }
5380 }
5381
5382 /* Common DP8390 core I/O ports: */
5383 if (pThis->hIoPortsCore != NIL_IOMIOPORTHANDLE)
5384 {
5385 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPortsCore, dp8390CoreIOPortWrite, dp8390CoreIOPortRead, NULL /*pvUser*/);
5386 AssertRCReturn(rc, rc);
5387 }
5388
5389 /* Shared RAM, if used: */
5390 if (pThis->hSharedMem != NIL_IOMMMIOHANDLE)
5391 {
5392 AssertRCReturn(rc, rc);
5393 switch (pThis->uDevType)
5394 {
5395 case DEV_WD8003:
5396 case DEV_WD8013:
5397 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hSharedMem, wdMemWrite, wdMemRead, NULL /*pvUser*/);
5398 AssertRCReturn(rc, rc);
5399 break;
5400 case DEV_3C503:
5401 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hSharedMem, elMemWrite, elMemRead, NULL /*pvUser*/);
5402 AssertRCReturn(rc, rc);
5403 break;
5404 case DEV_NE1000:
5405 case DEV_NE2000:
5406 default:
5407 /* Must not happen. */
5408 return VERR_INTERNAL_ERROR;
5409 }
5410 }
5411
5412 return VINF_SUCCESS;
5413}
5414
5415#endif /* IN_RING3 */
5416
5417/**
5418 * The device registration structure.
5419 */
5420const PDMDEVREG g_DeviceDP8390 =
5421{
5422 /* .u32Version = */ PDM_DEVREG_VERSION,
5423 /* .uReserved0 = */ 0,
5424 /* .szName = */ "dp8390",
5425 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
5426 /* .fClass = */ PDM_DEVREG_CLASS_NETWORK,
5427 /* .cMaxInstances = */ ~0U,
5428 /* .uSharedVersion = */ 42,
5429 /* .cbInstanceShared = */ sizeof(DPNICSTATE),
5430 /* .cbInstanceCC = */ sizeof(DPNICSTATECC),
5431 /* .cbInstanceRC = */ sizeof(DPNICSTATERC),
5432 /* .cMaxPciDevices = */ 0,
5433 /* .cMaxMsixVectors = */ 0,
5434 /* .pszDescription = */ "National Semiconductor DP8390 based adapter.\n",
5435#if defined(IN_RING3)
5436 /* .pszRCMod = */ "VBoxDDRC.rc",
5437 /* .pszR0Mod = */ "VBoxDDR0.r0",
5438 /* .pfnConstruct = */ dpNicR3Construct,
5439 /* .pfnDestruct = */ dpNicR3Destruct,
5440 /* .pfnRelocate = */ dpNicR3Relocate,
5441 /* .pfnMemSetup = */ NULL,
5442 /* .pfnPowerOn = */ NULL,
5443 /* .pfnReset = */ dpNicR3Reset,
5444 /* .pfnSuspend = */ dpNicR3Suspend,
5445 /* .pfnResume = */ NULL,
5446 /* .pfnAttach = */ dpNicR3Attach,
5447 /* .pfnDetach = */ dpNicR3Detach,
5448 /* .pfnQueryInterface = */ NULL,
5449 /* .pfnInitComplete = */ NULL,
5450 /* .pfnPowerOff = */ dpNicR3PowerOff,
5451 /* .pfnSoftReset = */ NULL,
5452 /* .pfnReserved0 = */ NULL,
5453 /* .pfnReserved1 = */ NULL,
5454 /* .pfnReserved2 = */ NULL,
5455 /* .pfnReserved3 = */ NULL,
5456 /* .pfnReserved4 = */ NULL,
5457 /* .pfnReserved5 = */ NULL,
5458 /* .pfnReserved6 = */ NULL,
5459 /* .pfnReserved7 = */ NULL,
5460#elif defined(IN_RING0)
5461 /* .pfnEarlyConstruct = */ NULL,
5462 /* .pfnConstruct = */ dpNicRZConstruct,
5463 /* .pfnDestruct = */ NULL,
5464 /* .pfnFinalDestruct = */ NULL,
5465 /* .pfnRequest = */ NULL,
5466 /* .pfnReserved0 = */ NULL,
5467 /* .pfnReserved1 = */ NULL,
5468 /* .pfnReserved2 = */ NULL,
5469 /* .pfnReserved3 = */ NULL,
5470 /* .pfnReserved4 = */ NULL,
5471 /* .pfnReserved5 = */ NULL,
5472 /* .pfnReserved6 = */ NULL,
5473 /* .pfnReserved7 = */ NULL,
5474#elif defined(IN_RC)
5475 /* .pfnConstruct = */ NULL,
5476 /* .pfnReserved0 = */ NULL,
5477 /* .pfnReserved1 = */ NULL,
5478 /* .pfnReserved2 = */ NULL,
5479 /* .pfnReserved3 = */ NULL,
5480 /* .pfnReserved4 = */ NULL,
5481 /* .pfnReserved5 = */ NULL,
5482 /* .pfnReserved6 = */ NULL,
5483 /* .pfnReserved7 = */ NULL,
5484#else
5485# error "Not in IN_RING3, IN_RING0 or IN_RC!"
5486#endif
5487 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
5488};
5489
5490#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette