VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DevE1000.cpp@ 86574

Last change on this file since 86574 was 86574, checked in by vboxsync, 4 years ago

Dev/E1000: (bugref:9829) Fixed division-by-zero issues

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 326.5 KB
Line 
1/* $Id: DevE1000.cpp 86574 2020-10-14 12:47:10Z vboxsync $ */
2/** @file
3 * DevE1000 - Intel 82540EM Ethernet Controller Emulation.
4 *
5 * Implemented in accordance with the specification:
6 *
7 * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual
8 * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx
9 *
10 * 317453-002 Revision 3.5
11 *
12 * @todo IPv6 checksum offloading support
13 * @todo Flexible Filter / Wakeup (optional?)
14 */
15
16/*
17 * Copyright (C) 2007-2020 Oracle Corporation
18 *
19 * This file is part of VirtualBox Open Source Edition (OSE), as
20 * available from http://www.virtualbox.org. This file is free software;
21 * you can redistribute it and/or modify it under the terms of the GNU
22 * General Public License (GPL) as published by the Free Software
23 * Foundation, in version 2 as it comes in the "COPYING" file of the
24 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
25 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DEV_E1000
33#include <iprt/crc.h>
34#include <iprt/ctype.h>
35#include <iprt/net.h>
36#include <iprt/semaphore.h>
37#include <iprt/string.h>
38#include <iprt/time.h>
39#include <iprt/uuid.h>
40#include <VBox/vmm/pdmdev.h>
41#include <VBox/vmm/pdmnetifs.h>
42#include <VBox/vmm/pdmnetinline.h>
43#include <VBox/param.h>
44#include "VBoxDD.h"
45
46#include "DevEEPROM.h"
47#include "DevE1000Phy.h"
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53/** @name E1000 Build Options
54 * @{ */
55/** @def E1K_INIT_RA0
56 * E1K_INIT_RA0 forces E1000 to set the first entry in Receive Address filter
57 * table to MAC address obtained from CFGM. Most guests read MAC address from
58 * EEPROM and write it to RA[0] explicitly, but Mac OS X seems to depend on it
59 * being already set (see @bugref{4657}).
60 */
61#define E1K_INIT_RA0
62/** @def E1K_LSC_ON_RESET
63 * E1K_LSC_ON_RESET causes e1000 to generate Link Status Change
64 * interrupt after hard reset. This makes the E1K_LSC_ON_SLU option unnecessary.
65 * With unplugged cable, LSC is triggerred for 82543GC only.
66 */
67#define E1K_LSC_ON_RESET
68/** @def E1K_LSC_ON_SLU
69 * E1K_LSC_ON_SLU causes E1000 to generate Link Status Change interrupt when
70 * the guest driver brings up the link via STATUS.LU bit. Again the only guest
71 * that requires it is Mac OS X (see @bugref{4657}).
72 */
73//#define E1K_LSC_ON_SLU
74/** @def E1K_INIT_LINKUP_DELAY
75 * E1K_INIT_LINKUP_DELAY prevents the link going up while the driver is still
76 * in init (see @bugref{8624}).
77 */
78#define E1K_INIT_LINKUP_DELAY_US (2000 * 1000)
79/** @def E1K_IMS_INT_DELAY_NS
80 * E1K_IMS_INT_DELAY_NS prevents interrupt storms in Windows guests on enabling
81 * interrupts (see @bugref{8624}).
82 */
83#define E1K_IMS_INT_DELAY_NS 100
84/** @def E1K_TX_DELAY
85 * E1K_TX_DELAY aims to improve guest-host transfer rate for TCP streams by
86 * preventing packets to be sent immediately. It allows to send several
87 * packets in a batch reducing the number of acknowledgments. Note that it
88 * effectively disables R0 TX path, forcing sending in R3.
89 */
90//#define E1K_TX_DELAY 150
91/** @def E1K_USE_TX_TIMERS
92 * E1K_USE_TX_TIMERS aims to reduce the number of generated TX interrupts if a
93 * guest driver set the delays via the Transmit Interrupt Delay Value (TIDV)
94 * register. Enabling it showed no positive effects on existing guests so it
95 * stays disabled. See sections 3.2.7.1 and 3.4.3.1 in "8254x Family of Gigabit
96 * Ethernet Controllers Software Developer’s Manual" for more detailed
97 * explanation.
98 */
99//#define E1K_USE_TX_TIMERS
100/** @def E1K_NO_TAD
101 * E1K_NO_TAD disables one of two timers enabled by E1K_USE_TX_TIMERS, the
102 * Transmit Absolute Delay time. This timer sets the maximum time interval
103 * during which TX interrupts can be postponed (delayed). It has no effect
104 * if E1K_USE_TX_TIMERS is not defined.
105 */
106//#define E1K_NO_TAD
107/** @def E1K_REL_DEBUG
108 * E1K_REL_DEBUG enables debug logging of l1, l2, l3 in release build.
109 */
110//#define E1K_REL_DEBUG
111/** @def E1K_INT_STATS
112 * E1K_INT_STATS enables collection of internal statistics used for
113 * debugging of delayed interrupts, etc.
114 */
115#define E1K_INT_STATS
116/** @def E1K_WITH_MSI
117 * E1K_WITH_MSI enables rudimentary MSI support. Not implemented.
118 */
119//#define E1K_WITH_MSI
120/** @def E1K_WITH_TX_CS
121 * E1K_WITH_TX_CS protects e1kXmitPending with a critical section.
122 */
123#define E1K_WITH_TX_CS
124/** @def E1K_WITH_TXD_CACHE
125 * E1K_WITH_TXD_CACHE causes E1000 to fetch multiple TX descriptors in a
126 * single physical memory read (or two if it wraps around the end of TX
127 * descriptor ring). It is required for proper functioning of bandwidth
128 * resource control as it allows to compute exact sizes of packets prior
129 * to allocating their buffers (see @bugref{5582}).
130 */
131#define E1K_WITH_TXD_CACHE
132/** @def E1K_WITH_RXD_CACHE
133 * E1K_WITH_RXD_CACHE causes E1000 to fetch multiple RX descriptors in a
134 * single physical memory read (or two if it wraps around the end of RX
135 * descriptor ring). Intel's packet driver for DOS needs this option in
136 * order to work properly (see @bugref{6217}).
137 */
138#define E1K_WITH_RXD_CACHE
139/** @def E1K_WITH_PREREG_MMIO
140 * E1K_WITH_PREREG_MMIO enables a new style MMIO registration and is
141 * currently only done for testing the relateted PDM, IOM and PGM code. */
142//#define E1K_WITH_PREREG_MMIO
143/* @} */
144/* End of Options ************************************************************/
145
146#ifdef E1K_WITH_TXD_CACHE
147/**
148 * E1K_TXD_CACHE_SIZE specifies the maximum number of TX descriptors stored
149 * in the state structure. It limits the amount of descriptors loaded in one
150 * batch read. For example, Linux guest may use up to 20 descriptors per
151 * TSE packet. The largest TSE packet seen (Windows guest) was 45 descriptors.
152 */
153# define E1K_TXD_CACHE_SIZE 64u
154#endif /* E1K_WITH_TXD_CACHE */
155
156#ifdef E1K_WITH_RXD_CACHE
157/**
158 * E1K_RXD_CACHE_SIZE specifies the maximum number of RX descriptors stored
159 * in the state structure. It limits the amount of descriptors loaded in one
160 * batch read. For example, XP guest adds 15 RX descriptors at a time.
161 */
162# define E1K_RXD_CACHE_SIZE 16u
163#endif /* E1K_WITH_RXD_CACHE */
164
165
166/* Little helpers ************************************************************/
167#undef htons
168#undef ntohs
169#undef htonl
170#undef ntohl
171#define htons(x) ((((x) & 0xff00) >> 8) | (((x) & 0x00ff) << 8))
172#define ntohs(x) htons(x)
173#define htonl(x) ASMByteSwapU32(x)
174#define ntohl(x) htonl(x)
175
176#ifndef DEBUG
177# ifdef E1K_REL_DEBUG
178# define DEBUG
179# define E1kLog(a) LogRel(a)
180# define E1kLog2(a) LogRel(a)
181# define E1kLog3(a) LogRel(a)
182# define E1kLogX(x, a) LogRel(a)
183//# define E1kLog3(a) do {} while (0)
184# else
185# define E1kLog(a) do {} while (0)
186# define E1kLog2(a) do {} while (0)
187# define E1kLog3(a) do {} while (0)
188# define E1kLogX(x, a) do {} while (0)
189# endif
190#else
191# define E1kLog(a) Log(a)
192# define E1kLog2(a) Log2(a)
193# define E1kLog3(a) Log3(a)
194# define E1kLogX(x, a) LogIt(x, LOG_GROUP, a)
195//# define E1kLog(a) do {} while (0)
196//# define E1kLog2(a) do {} while (0)
197//# define E1kLog3(a) do {} while (0)
198#endif
199
200#if 0
201# define LOG_ENABLED
202# define E1kLogRel(a) LogRel(a)
203# undef Log6
204# define Log6(a) LogRel(a)
205#else
206# define E1kLogRel(a) do { } while (0)
207#endif
208
209//#undef DEBUG
210
211#define E1K_RELOCATE(p, o) *(RTHCUINTPTR *)&p += o
212
213#define E1K_INC_CNT32(cnt) \
214do { \
215 if (cnt < UINT32_MAX) \
216 cnt++; \
217} while (0)
218
219#define E1K_ADD_CNT64(cntLo, cntHi, val) \
220do { \
221 uint64_t u64Cnt = RT_MAKE_U64(cntLo, cntHi); \
222 uint64_t tmp = u64Cnt; \
223 u64Cnt += val; \
224 if (tmp > u64Cnt ) \
225 u64Cnt = UINT64_MAX; \
226 cntLo = (uint32_t)u64Cnt; \
227 cntHi = (uint32_t)(u64Cnt >> 32); \
228} while (0)
229
230#ifdef E1K_INT_STATS
231# define E1K_INC_ISTAT_CNT(cnt) do { ++cnt; } while (0)
232#else /* E1K_INT_STATS */
233# define E1K_INC_ISTAT_CNT(cnt) do { } while (0)
234#endif /* E1K_INT_STATS */
235
236
237/*****************************************************************************/
238
239typedef uint32_t E1KCHIP;
240#define E1K_CHIP_82540EM 0
241#define E1K_CHIP_82543GC 1
242#define E1K_CHIP_82545EM 2
243
244#ifdef IN_RING3
245/** Different E1000 chips. */
246static const struct E1kChips
247{
248 uint16_t uPCIVendorId;
249 uint16_t uPCIDeviceId;
250 uint16_t uPCISubsystemVendorId;
251 uint16_t uPCISubsystemId;
252 const char *pcszName;
253} g_aChips[] =
254{
255 /* Vendor Device SSVendor SubSys Name */
256 { 0x8086,
257 /* Temporary code, as MSI-aware driver dislike 0x100E. How to do that right? */
258# ifdef E1K_WITH_MSI
259 0x105E,
260# else
261 0x100E,
262# endif
263 0x8086, 0x001E, "82540EM" }, /* Intel 82540EM-A in Intel PRO/1000 MT Desktop */
264 { 0x8086, 0x1004, 0x8086, 0x1004, "82543GC" }, /* Intel 82543GC in Intel PRO/1000 T Server */
265 { 0x8086, 0x100F, 0x15AD, 0x0750, "82545EM" } /* Intel 82545EM-A in VMWare Network Adapter */
266};
267#endif /* IN_RING3 */
268
269
270/* The size of register area mapped to I/O space */
271#define E1K_IOPORT_SIZE 0x8
272/* The size of memory-mapped register area */
273#define E1K_MM_SIZE 0x20000
274
275#define E1K_MAX_TX_PKT_SIZE 16288
276#define E1K_MAX_RX_PKT_SIZE 16384
277
278/*****************************************************************************/
279
280/** Gets the specfieid bits from the register. */
281#define GET_BITS(reg, bits) ((reg & reg##_##bits##_MASK) >> reg##_##bits##_SHIFT)
282#define GET_BITS_V(val, reg, bits) ((val & reg##_##bits##_MASK) >> reg##_##bits##_SHIFT)
283#define BITS(reg, bits, bitval) (bitval << reg##_##bits##_SHIFT)
284#define SET_BITS(reg, bits, bitval) do { reg = (reg & ~reg##_##bits##_MASK) | (bitval << reg##_##bits##_SHIFT); } while (0)
285#define SET_BITS_V(val, reg, bits, bitval) do { val = (val & ~reg##_##bits##_MASK) | (bitval << reg##_##bits##_SHIFT); } while (0)
286
287#define CTRL_SLU UINT32_C(0x00000040)
288#define CTRL_MDIO UINT32_C(0x00100000)
289#define CTRL_MDC UINT32_C(0x00200000)
290#define CTRL_MDIO_DIR UINT32_C(0x01000000)
291#define CTRL_MDC_DIR UINT32_C(0x02000000)
292#define CTRL_RESET UINT32_C(0x04000000)
293#define CTRL_VME UINT32_C(0x40000000)
294
295#define STATUS_LU UINT32_C(0x00000002)
296#define STATUS_TXOFF UINT32_C(0x00000010)
297
298#define EECD_EE_WIRES UINT32_C(0x0F)
299#define EECD_EE_REQ UINT32_C(0x40)
300#define EECD_EE_GNT UINT32_C(0x80)
301
302#define EERD_START UINT32_C(0x00000001)
303#define EERD_DONE UINT32_C(0x00000010)
304#define EERD_DATA_MASK UINT32_C(0xFFFF0000)
305#define EERD_DATA_SHIFT 16
306#define EERD_ADDR_MASK UINT32_C(0x0000FF00)
307#define EERD_ADDR_SHIFT 8
308
309#define MDIC_DATA_MASK UINT32_C(0x0000FFFF)
310#define MDIC_DATA_SHIFT 0
311#define MDIC_REG_MASK UINT32_C(0x001F0000)
312#define MDIC_REG_SHIFT 16
313#define MDIC_PHY_MASK UINT32_C(0x03E00000)
314#define MDIC_PHY_SHIFT 21
315#define MDIC_OP_WRITE UINT32_C(0x04000000)
316#define MDIC_OP_READ UINT32_C(0x08000000)
317#define MDIC_READY UINT32_C(0x10000000)
318#define MDIC_INT_EN UINT32_C(0x20000000)
319#define MDIC_ERROR UINT32_C(0x40000000)
320
321#define TCTL_EN UINT32_C(0x00000002)
322#define TCTL_PSP UINT32_C(0x00000008)
323
324#define RCTL_EN UINT32_C(0x00000002)
325#define RCTL_UPE UINT32_C(0x00000008)
326#define RCTL_MPE UINT32_C(0x00000010)
327#define RCTL_LPE UINT32_C(0x00000020)
328#define RCTL_LBM_MASK UINT32_C(0x000000C0)
329#define RCTL_LBM_SHIFT 6
330#define RCTL_RDMTS_MASK UINT32_C(0x00000300)
331#define RCTL_RDMTS_SHIFT 8
332#define RCTL_LBM_TCVR UINT32_C(3) /**< PHY or external SerDes loopback. */
333#define RCTL_MO_MASK UINT32_C(0x00003000)
334#define RCTL_MO_SHIFT 12
335#define RCTL_BAM UINT32_C(0x00008000)
336#define RCTL_BSIZE_MASK UINT32_C(0x00030000)
337#define RCTL_BSIZE_SHIFT 16
338#define RCTL_VFE UINT32_C(0x00040000)
339#define RCTL_CFIEN UINT32_C(0x00080000)
340#define RCTL_CFI UINT32_C(0x00100000)
341#define RCTL_BSEX UINT32_C(0x02000000)
342#define RCTL_SECRC UINT32_C(0x04000000)
343
344#define ICR_TXDW UINT32_C(0x00000001)
345#define ICR_TXQE UINT32_C(0x00000002)
346#define ICR_LSC UINT32_C(0x00000004)
347#define ICR_RXDMT0 UINT32_C(0x00000010)
348#define ICR_RXT0 UINT32_C(0x00000080)
349#define ICR_TXD_LOW UINT32_C(0x00008000)
350#define RDTR_FPD UINT32_C(0x80000000)
351
352#define PBA_st ((PBAST*)(pThis->auRegs + PBA_IDX))
353typedef struct
354{
355 unsigned rxa : 7;
356 unsigned rxa_r : 9;
357 unsigned txa : 16;
358} PBAST;
359AssertCompileSize(PBAST, 4);
360
361#define TXDCTL_WTHRESH_MASK 0x003F0000
362#define TXDCTL_WTHRESH_SHIFT 16
363#define TXDCTL_LWTHRESH_MASK 0xFE000000
364#define TXDCTL_LWTHRESH_SHIFT 25
365
366#define RXCSUM_PCSS_MASK UINT32_C(0x000000FF)
367#define RXCSUM_PCSS_SHIFT 0
368
369/** @name Register access macros
370 * @remarks These ASSUME alocal variable @a pThis of type PE1KSTATE.
371 * @{ */
372#define CTRL pThis->auRegs[CTRL_IDX]
373#define STATUS pThis->auRegs[STATUS_IDX]
374#define EECD pThis->auRegs[EECD_IDX]
375#define EERD pThis->auRegs[EERD_IDX]
376#define CTRL_EXT pThis->auRegs[CTRL_EXT_IDX]
377#define FLA pThis->auRegs[FLA_IDX]
378#define MDIC pThis->auRegs[MDIC_IDX]
379#define FCAL pThis->auRegs[FCAL_IDX]
380#define FCAH pThis->auRegs[FCAH_IDX]
381#define FCT pThis->auRegs[FCT_IDX]
382#define VET pThis->auRegs[VET_IDX]
383#define ICR pThis->auRegs[ICR_IDX]
384#define ITR pThis->auRegs[ITR_IDX]
385#define ICS pThis->auRegs[ICS_IDX]
386#define IMS pThis->auRegs[IMS_IDX]
387#define IMC pThis->auRegs[IMC_IDX]
388#define RCTL pThis->auRegs[RCTL_IDX]
389#define FCTTV pThis->auRegs[FCTTV_IDX]
390#define TXCW pThis->auRegs[TXCW_IDX]
391#define RXCW pThis->auRegs[RXCW_IDX]
392#define TCTL pThis->auRegs[TCTL_IDX]
393#define TIPG pThis->auRegs[TIPG_IDX]
394#define AIFS pThis->auRegs[AIFS_IDX]
395#define LEDCTL pThis->auRegs[LEDCTL_IDX]
396#define PBA pThis->auRegs[PBA_IDX]
397#define FCRTL pThis->auRegs[FCRTL_IDX]
398#define FCRTH pThis->auRegs[FCRTH_IDX]
399#define RDFH pThis->auRegs[RDFH_IDX]
400#define RDFT pThis->auRegs[RDFT_IDX]
401#define RDFHS pThis->auRegs[RDFHS_IDX]
402#define RDFTS pThis->auRegs[RDFTS_IDX]
403#define RDFPC pThis->auRegs[RDFPC_IDX]
404#define RDBAL pThis->auRegs[RDBAL_IDX]
405#define RDBAH pThis->auRegs[RDBAH_IDX]
406#define RDLEN pThis->auRegs[RDLEN_IDX]
407#define RDH pThis->auRegs[RDH_IDX]
408#define RDT pThis->auRegs[RDT_IDX]
409#define RDTR pThis->auRegs[RDTR_IDX]
410#define RXDCTL pThis->auRegs[RXDCTL_IDX]
411#define RADV pThis->auRegs[RADV_IDX]
412#define RSRPD pThis->auRegs[RSRPD_IDX]
413#define TXDMAC pThis->auRegs[TXDMAC_IDX]
414#define TDFH pThis->auRegs[TDFH_IDX]
415#define TDFT pThis->auRegs[TDFT_IDX]
416#define TDFHS pThis->auRegs[TDFHS_IDX]
417#define TDFTS pThis->auRegs[TDFTS_IDX]
418#define TDFPC pThis->auRegs[TDFPC_IDX]
419#define TDBAL pThis->auRegs[TDBAL_IDX]
420#define TDBAH pThis->auRegs[TDBAH_IDX]
421#define TDLEN pThis->auRegs[TDLEN_IDX]
422#define TDH pThis->auRegs[TDH_IDX]
423#define TDT pThis->auRegs[TDT_IDX]
424#define TIDV pThis->auRegs[TIDV_IDX]
425#define TXDCTL pThis->auRegs[TXDCTL_IDX]
426#define TADV pThis->auRegs[TADV_IDX]
427#define TSPMT pThis->auRegs[TSPMT_IDX]
428#define CRCERRS pThis->auRegs[CRCERRS_IDX]
429#define ALGNERRC pThis->auRegs[ALGNERRC_IDX]
430#define SYMERRS pThis->auRegs[SYMERRS_IDX]
431#define RXERRC pThis->auRegs[RXERRC_IDX]
432#define MPC pThis->auRegs[MPC_IDX]
433#define SCC pThis->auRegs[SCC_IDX]
434#define ECOL pThis->auRegs[ECOL_IDX]
435#define MCC pThis->auRegs[MCC_IDX]
436#define LATECOL pThis->auRegs[LATECOL_IDX]
437#define COLC pThis->auRegs[COLC_IDX]
438#define DC pThis->auRegs[DC_IDX]
439#define TNCRS pThis->auRegs[TNCRS_IDX]
440/* #define SEC pThis->auRegs[SEC_IDX] Conflict with sys/time.h */
441#define CEXTERR pThis->auRegs[CEXTERR_IDX]
442#define RLEC pThis->auRegs[RLEC_IDX]
443#define XONRXC pThis->auRegs[XONRXC_IDX]
444#define XONTXC pThis->auRegs[XONTXC_IDX]
445#define XOFFRXC pThis->auRegs[XOFFRXC_IDX]
446#define XOFFTXC pThis->auRegs[XOFFTXC_IDX]
447#define FCRUC pThis->auRegs[FCRUC_IDX]
448#define PRC64 pThis->auRegs[PRC64_IDX]
449#define PRC127 pThis->auRegs[PRC127_IDX]
450#define PRC255 pThis->auRegs[PRC255_IDX]
451#define PRC511 pThis->auRegs[PRC511_IDX]
452#define PRC1023 pThis->auRegs[PRC1023_IDX]
453#define PRC1522 pThis->auRegs[PRC1522_IDX]
454#define GPRC pThis->auRegs[GPRC_IDX]
455#define BPRC pThis->auRegs[BPRC_IDX]
456#define MPRC pThis->auRegs[MPRC_IDX]
457#define GPTC pThis->auRegs[GPTC_IDX]
458#define GORCL pThis->auRegs[GORCL_IDX]
459#define GORCH pThis->auRegs[GORCH_IDX]
460#define GOTCL pThis->auRegs[GOTCL_IDX]
461#define GOTCH pThis->auRegs[GOTCH_IDX]
462#define RNBC pThis->auRegs[RNBC_IDX]
463#define RUC pThis->auRegs[RUC_IDX]
464#define RFC pThis->auRegs[RFC_IDX]
465#define ROC pThis->auRegs[ROC_IDX]
466#define RJC pThis->auRegs[RJC_IDX]
467#define MGTPRC pThis->auRegs[MGTPRC_IDX]
468#define MGTPDC pThis->auRegs[MGTPDC_IDX]
469#define MGTPTC pThis->auRegs[MGTPTC_IDX]
470#define TORL pThis->auRegs[TORL_IDX]
471#define TORH pThis->auRegs[TORH_IDX]
472#define TOTL pThis->auRegs[TOTL_IDX]
473#define TOTH pThis->auRegs[TOTH_IDX]
474#define TPR pThis->auRegs[TPR_IDX]
475#define TPT pThis->auRegs[TPT_IDX]
476#define PTC64 pThis->auRegs[PTC64_IDX]
477#define PTC127 pThis->auRegs[PTC127_IDX]
478#define PTC255 pThis->auRegs[PTC255_IDX]
479#define PTC511 pThis->auRegs[PTC511_IDX]
480#define PTC1023 pThis->auRegs[PTC1023_IDX]
481#define PTC1522 pThis->auRegs[PTC1522_IDX]
482#define MPTC pThis->auRegs[MPTC_IDX]
483#define BPTC pThis->auRegs[BPTC_IDX]
484#define TSCTC pThis->auRegs[TSCTC_IDX]
485#define TSCTFC pThis->auRegs[TSCTFC_IDX]
486#define RXCSUM pThis->auRegs[RXCSUM_IDX]
487#define WUC pThis->auRegs[WUC_IDX]
488#define WUFC pThis->auRegs[WUFC_IDX]
489#define WUS pThis->auRegs[WUS_IDX]
490#define MANC pThis->auRegs[MANC_IDX]
491#define IPAV pThis->auRegs[IPAV_IDX]
492#define WUPL pThis->auRegs[WUPL_IDX]
493/** @} */
494
495/**
496 * Indices of memory-mapped registers in register table.
497 */
498typedef enum
499{
500 CTRL_IDX,
501 STATUS_IDX,
502 EECD_IDX,
503 EERD_IDX,
504 CTRL_EXT_IDX,
505 FLA_IDX,
506 MDIC_IDX,
507 FCAL_IDX,
508 FCAH_IDX,
509 FCT_IDX,
510 VET_IDX,
511 ICR_IDX,
512 ITR_IDX,
513 ICS_IDX,
514 IMS_IDX,
515 IMC_IDX,
516 RCTL_IDX,
517 FCTTV_IDX,
518 TXCW_IDX,
519 RXCW_IDX,
520 TCTL_IDX,
521 TIPG_IDX,
522 AIFS_IDX,
523 LEDCTL_IDX,
524 PBA_IDX,
525 FCRTL_IDX,
526 FCRTH_IDX,
527 RDFH_IDX,
528 RDFT_IDX,
529 RDFHS_IDX,
530 RDFTS_IDX,
531 RDFPC_IDX,
532 RDBAL_IDX,
533 RDBAH_IDX,
534 RDLEN_IDX,
535 RDH_IDX,
536 RDT_IDX,
537 RDTR_IDX,
538 RXDCTL_IDX,
539 RADV_IDX,
540 RSRPD_IDX,
541 TXDMAC_IDX,
542 TDFH_IDX,
543 TDFT_IDX,
544 TDFHS_IDX,
545 TDFTS_IDX,
546 TDFPC_IDX,
547 TDBAL_IDX,
548 TDBAH_IDX,
549 TDLEN_IDX,
550 TDH_IDX,
551 TDT_IDX,
552 TIDV_IDX,
553 TXDCTL_IDX,
554 TADV_IDX,
555 TSPMT_IDX,
556 CRCERRS_IDX,
557 ALGNERRC_IDX,
558 SYMERRS_IDX,
559 RXERRC_IDX,
560 MPC_IDX,
561 SCC_IDX,
562 ECOL_IDX,
563 MCC_IDX,
564 LATECOL_IDX,
565 COLC_IDX,
566 DC_IDX,
567 TNCRS_IDX,
568 SEC_IDX,
569 CEXTERR_IDX,
570 RLEC_IDX,
571 XONRXC_IDX,
572 XONTXC_IDX,
573 XOFFRXC_IDX,
574 XOFFTXC_IDX,
575 FCRUC_IDX,
576 PRC64_IDX,
577 PRC127_IDX,
578 PRC255_IDX,
579 PRC511_IDX,
580 PRC1023_IDX,
581 PRC1522_IDX,
582 GPRC_IDX,
583 BPRC_IDX,
584 MPRC_IDX,
585 GPTC_IDX,
586 GORCL_IDX,
587 GORCH_IDX,
588 GOTCL_IDX,
589 GOTCH_IDX,
590 RNBC_IDX,
591 RUC_IDX,
592 RFC_IDX,
593 ROC_IDX,
594 RJC_IDX,
595 MGTPRC_IDX,
596 MGTPDC_IDX,
597 MGTPTC_IDX,
598 TORL_IDX,
599 TORH_IDX,
600 TOTL_IDX,
601 TOTH_IDX,
602 TPR_IDX,
603 TPT_IDX,
604 PTC64_IDX,
605 PTC127_IDX,
606 PTC255_IDX,
607 PTC511_IDX,
608 PTC1023_IDX,
609 PTC1522_IDX,
610 MPTC_IDX,
611 BPTC_IDX,
612 TSCTC_IDX,
613 TSCTFC_IDX,
614 RXCSUM_IDX,
615 WUC_IDX,
616 WUFC_IDX,
617 WUS_IDX,
618 MANC_IDX,
619 IPAV_IDX,
620 WUPL_IDX,
621 MTA_IDX,
622 RA_IDX,
623 VFTA_IDX,
624 IP4AT_IDX,
625 IP6AT_IDX,
626 WUPM_IDX,
627 FFLT_IDX,
628 FFMT_IDX,
629 FFVT_IDX,
630 PBM_IDX,
631 RA_82542_IDX,
632 MTA_82542_IDX,
633 VFTA_82542_IDX,
634 E1K_NUM_OF_REGS
635} E1kRegIndex;
636
637#define E1K_NUM_OF_32BIT_REGS MTA_IDX
638/** The number of registers with strictly increasing offset. */
639#define E1K_NUM_OF_BINARY_SEARCHABLE (WUPL_IDX + 1)
640
641
642/**
643 * Define E1000-specific EEPROM layout.
644 */
645struct E1kEEPROM
646{
647 public:
648 EEPROM93C46 eeprom;
649
650#ifdef IN_RING3
651 /**
652 * Initialize EEPROM content.
653 *
654 * @param macAddr MAC address of E1000.
655 */
656 void init(RTMAC &macAddr)
657 {
658 eeprom.init();
659 memcpy(eeprom.m_au16Data, macAddr.au16, sizeof(macAddr.au16));
660 eeprom.m_au16Data[0x04] = 0xFFFF;
661 /*
662 * bit 3 - full support for power management
663 * bit 10 - full duplex
664 */
665 eeprom.m_au16Data[0x0A] = 0x4408;
666 eeprom.m_au16Data[0x0B] = 0x001E;
667 eeprom.m_au16Data[0x0C] = 0x8086;
668 eeprom.m_au16Data[0x0D] = 0x100E;
669 eeprom.m_au16Data[0x0E] = 0x8086;
670 eeprom.m_au16Data[0x0F] = 0x3040;
671 eeprom.m_au16Data[0x21] = 0x7061;
672 eeprom.m_au16Data[0x22] = 0x280C;
673 eeprom.m_au16Data[0x23] = 0x00C8;
674 eeprom.m_au16Data[0x24] = 0x00C8;
675 eeprom.m_au16Data[0x2F] = 0x0602;
676 updateChecksum();
677 };
678
679 /**
680 * Compute the checksum as required by E1000 and store it
681 * in the last word.
682 */
683 void updateChecksum()
684 {
685 uint16_t u16Checksum = 0;
686
687 for (int i = 0; i < eeprom.SIZE-1; i++)
688 u16Checksum += eeprom.m_au16Data[i];
689 eeprom.m_au16Data[eeprom.SIZE-1] = 0xBABA - u16Checksum;
690 };
691
692 /**
693 * First 6 bytes of EEPROM contain MAC address.
694 *
695 * @returns MAC address of E1000.
696 */
697 void getMac(PRTMAC pMac)
698 {
699 memcpy(pMac->au16, eeprom.m_au16Data, sizeof(pMac->au16));
700 };
701
702 uint32_t read()
703 {
704 return eeprom.read();
705 }
706
707 void write(uint32_t u32Wires)
708 {
709 eeprom.write(u32Wires);
710 }
711
712 bool readWord(uint32_t u32Addr, uint16_t *pu16Value)
713 {
714 return eeprom.readWord(u32Addr, pu16Value);
715 }
716
717 int load(PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM)
718 {
719 return eeprom.load(pHlp, pSSM);
720 }
721
722 void save(PCPDMDEVHLPR3 pHlp, PSSMHANDLE pSSM)
723 {
724 eeprom.save(pHlp, pSSM);
725 }
726#endif /* IN_RING3 */
727};
728
729
730#define E1K_SPEC_VLAN(s) (s & 0xFFF)
731#define E1K_SPEC_CFI(s) (!!((s>>12) & 0x1))
732#define E1K_SPEC_PRI(s) ((s>>13) & 0x7)
733
734struct E1kRxDStatus
735{
736 /** @name Descriptor Status field (3.2.3.1)
737 * @{ */
738 unsigned fDD : 1; /**< Descriptor Done. */
739 unsigned fEOP : 1; /**< End of packet. */
740 unsigned fIXSM : 1; /**< Ignore checksum indication. */
741 unsigned fVP : 1; /**< VLAN, matches VET. */
742 unsigned : 1;
743 unsigned fTCPCS : 1; /**< RCP Checksum calculated on the packet. */
744 unsigned fIPCS : 1; /**< IP Checksum calculated on the packet. */
745 unsigned fPIF : 1; /**< Passed in-exact filter */
746 /** @} */
747 /** @name Descriptor Errors field (3.2.3.2)
748 * (Only valid when fEOP and fDD are set.)
749 * @{ */
750 unsigned fCE : 1; /**< CRC or alignment error. */
751 unsigned : 4; /**< Reserved, varies with different models... */
752 unsigned fTCPE : 1; /**< TCP/UDP checksum error. */
753 unsigned fIPE : 1; /**< IP Checksum error. */
754 unsigned fRXE : 1; /**< RX Data error. */
755 /** @} */
756 /** @name Descriptor Special field (3.2.3.3)
757 * @{ */
758 unsigned u16Special : 16; /**< VLAN: Id, Canonical form, Priority. */
759 /** @} */
760};
761typedef struct E1kRxDStatus E1KRXDST;
762
763struct E1kRxDesc_st
764{
765 uint64_t u64BufAddr; /**< Address of data buffer */
766 uint16_t u16Length; /**< Length of data in buffer */
767 uint16_t u16Checksum; /**< Packet checksum */
768 E1KRXDST status;
769};
770typedef struct E1kRxDesc_st E1KRXDESC;
771AssertCompileSize(E1KRXDESC, 16);
772
773#define E1K_DTYP_LEGACY -1
774#define E1K_DTYP_CONTEXT 0
775#define E1K_DTYP_DATA 1
776
777struct E1kTDLegacy
778{
779 uint64_t u64BufAddr; /**< Address of data buffer */
780 struct TDLCmd_st
781 {
782 unsigned u16Length : 16;
783 unsigned u8CSO : 8;
784 /* CMD field : 8 */
785 unsigned fEOP : 1;
786 unsigned fIFCS : 1;
787 unsigned fIC : 1;
788 unsigned fRS : 1;
789 unsigned fRPS : 1;
790 unsigned fDEXT : 1;
791 unsigned fVLE : 1;
792 unsigned fIDE : 1;
793 } cmd;
794 struct TDLDw3_st
795 {
796 /* STA field */
797 unsigned fDD : 1;
798 unsigned fEC : 1;
799 unsigned fLC : 1;
800 unsigned fTURSV : 1;
801 /* RSV field */
802 unsigned u4RSV : 4;
803 /* CSS field */
804 unsigned u8CSS : 8;
805 /* Special field*/
806 unsigned u16Special: 16;
807 } dw3;
808};
809
810/**
811 * TCP/IP Context Transmit Descriptor, section 3.3.6.
812 */
813struct E1kTDContext
814{
815 struct CheckSum_st
816 {
817 /** TSE: Header start. !TSE: Checksum start. */
818 unsigned u8CSS : 8;
819 /** Checksum offset - where to store it. */
820 unsigned u8CSO : 8;
821 /** Checksum ending (inclusive) offset, 0 = end of packet. */
822 unsigned u16CSE : 16;
823 } ip;
824 struct CheckSum_st tu;
825 struct TDCDw2_st
826 {
827 /** TSE: The total number of payload bytes for this context. Sans header. */
828 unsigned u20PAYLEN : 20;
829 /** The descriptor type - E1K_DTYP_CONTEXT (0). */
830 unsigned u4DTYP : 4;
831 /** TUCMD field, 8 bits
832 * @{ */
833 /** TSE: TCP (set) or UDP (clear). */
834 unsigned fTCP : 1;
835 /** TSE: IPv4 (set) or IPv6 (clear) - for finding the payload length field in
836 * the IP header. Does not affect the checksumming.
837 * @remarks 82544GC/EI interprets a cleared field differently. */
838 unsigned fIP : 1;
839 /** TSE: TCP segmentation enable. When clear the context describes */
840 unsigned fTSE : 1;
841 /** Report status (only applies to dw3.fDD for here). */
842 unsigned fRS : 1;
843 /** Reserved, MBZ. */
844 unsigned fRSV1 : 1;
845 /** Descriptor extension, must be set for this descriptor type. */
846 unsigned fDEXT : 1;
847 /** Reserved, MBZ. */
848 unsigned fRSV2 : 1;
849 /** Interrupt delay enable. */
850 unsigned fIDE : 1;
851 /** @} */
852 } dw2;
853 struct TDCDw3_st
854 {
855 /** Descriptor Done. */
856 unsigned fDD : 1;
857 /** Reserved, MBZ. */
858 unsigned u7RSV : 7;
859 /** TSO: The header (prototype) length (Ethernet[, VLAN tag], IP, TCP/UDP. */
860 unsigned u8HDRLEN : 8;
861 /** TSO: Maximum segment size. */
862 unsigned u16MSS : 16;
863 } dw3;
864};
865typedef struct E1kTDContext E1KTXCTX;
866
867/**
868 * TCP/IP Data Transmit Descriptor, section 3.3.7.
869 */
870struct E1kTDData
871{
872 uint64_t u64BufAddr; /**< Address of data buffer */
873 struct TDDCmd_st
874 {
875 /** The total length of data pointed to by this descriptor. */
876 unsigned u20DTALEN : 20;
877 /** The descriptor type - E1K_DTYP_DATA (1). */
878 unsigned u4DTYP : 4;
879 /** @name DCMD field, 8 bits (3.3.7.1).
880 * @{ */
881 /** End of packet. Note TSCTFC update. */
882 unsigned fEOP : 1;
883 /** Insert Ethernet FCS/CRC (requires fEOP to be set). */
884 unsigned fIFCS : 1;
885 /** Use the TSE context when set and the normal when clear. */
886 unsigned fTSE : 1;
887 /** Report status (dw3.STA). */
888 unsigned fRS : 1;
889 /** Reserved. 82544GC/EI defines this report packet set (RPS). */
890 unsigned fRPS : 1;
891 /** Descriptor extension, must be set for this descriptor type. */
892 unsigned fDEXT : 1;
893 /** VLAN enable, requires CTRL.VME, auto enables FCS/CRC.
894 * Insert dw3.SPECIAL after ethernet header. */
895 unsigned fVLE : 1;
896 /** Interrupt delay enable. */
897 unsigned fIDE : 1;
898 /** @} */
899 } cmd;
900 struct TDDDw3_st
901 {
902 /** @name STA field (3.3.7.2)
903 * @{ */
904 unsigned fDD : 1; /**< Descriptor done. */
905 unsigned fEC : 1; /**< Excess collision. */
906 unsigned fLC : 1; /**< Late collision. */
907 /** Reserved, except for the usual oddball (82544GC/EI) where it's called TU. */
908 unsigned fTURSV : 1;
909 /** @} */
910 unsigned u4RSV : 4; /**< Reserved field, MBZ. */
911 /** @name POPTS (Packet Option) field (3.3.7.3)
912 * @{ */
913 unsigned fIXSM : 1; /**< Insert IP checksum. */
914 unsigned fTXSM : 1; /**< Insert TCP/UDP checksum. */
915 unsigned u6RSV : 6; /**< Reserved, MBZ. */
916 /** @} */
917 /** @name SPECIAL field - VLAN tag to be inserted after ethernet header.
918 * Requires fEOP, fVLE and CTRL.VME to be set.
919 * @{ */
920 unsigned u16Special: 16; /**< VLAN: Id, Canonical form, Priority. */
921 /** @} */
922 } dw3;
923};
924typedef struct E1kTDData E1KTXDAT;
925
926union E1kTxDesc
927{
928 struct E1kTDLegacy legacy;
929 struct E1kTDContext context;
930 struct E1kTDData data;
931};
932typedef union E1kTxDesc E1KTXDESC;
933AssertCompileSize(E1KTXDESC, 16);
934
935#define RA_CTL_AS 0x0003
936#define RA_CTL_AV 0x8000
937
938union E1kRecAddr
939{
940 uint32_t au32[32];
941 struct RAArray
942 {
943 uint8_t addr[6];
944 uint16_t ctl;
945 } array[16];
946};
947typedef struct E1kRecAddr::RAArray E1KRAELEM;
948typedef union E1kRecAddr E1KRA;
949AssertCompileSize(E1KRA, 8*16);
950
951#define E1K_IP_RF UINT16_C(0x8000) /**< reserved fragment flag */
952#define E1K_IP_DF UINT16_C(0x4000) /**< dont fragment flag */
953#define E1K_IP_MF UINT16_C(0x2000) /**< more fragments flag */
954#define E1K_IP_OFFMASK UINT16_C(0x1fff) /**< mask for fragmenting bits */
955
956/** @todo use+extend RTNETIPV4 */
957struct E1kIpHeader
958{
959 /* type of service / version / header length */
960 uint16_t tos_ver_hl;
961 /* total length */
962 uint16_t total_len;
963 /* identification */
964 uint16_t ident;
965 /* fragment offset field */
966 uint16_t offset;
967 /* time to live / protocol*/
968 uint16_t ttl_proto;
969 /* checksum */
970 uint16_t chksum;
971 /* source IP address */
972 uint32_t src;
973 /* destination IP address */
974 uint32_t dest;
975};
976AssertCompileSize(struct E1kIpHeader, 20);
977
978#define E1K_TCP_FIN UINT16_C(0x01)
979#define E1K_TCP_SYN UINT16_C(0x02)
980#define E1K_TCP_RST UINT16_C(0x04)
981#define E1K_TCP_PSH UINT16_C(0x08)
982#define E1K_TCP_ACK UINT16_C(0x10)
983#define E1K_TCP_URG UINT16_C(0x20)
984#define E1K_TCP_ECE UINT16_C(0x40)
985#define E1K_TCP_CWR UINT16_C(0x80)
986#define E1K_TCP_FLAGS UINT16_C(0x3f)
987
988/** @todo use+extend RTNETTCP */
989struct E1kTcpHeader
990{
991 uint16_t src;
992 uint16_t dest;
993 uint32_t seqno;
994 uint32_t ackno;
995 uint16_t hdrlen_flags;
996 uint16_t wnd;
997 uint16_t chksum;
998 uint16_t urgp;
999};
1000AssertCompileSize(struct E1kTcpHeader, 20);
1001
1002
1003#ifdef E1K_WITH_TXD_CACHE
1004/** The current Saved state version. */
1005# define E1K_SAVEDSTATE_VERSION 4
1006/** Saved state version for VirtualBox 4.2 with VLAN tag fields. */
1007# define E1K_SAVEDSTATE_VERSION_VBOX_42_VTAG 3
1008#else /* !E1K_WITH_TXD_CACHE */
1009/** The current Saved state version. */
1010# define E1K_SAVEDSTATE_VERSION 3
1011#endif /* !E1K_WITH_TXD_CACHE */
1012/** Saved state version for VirtualBox 4.1 and earlier.
1013 * These did not include VLAN tag fields. */
1014#define E1K_SAVEDSTATE_VERSION_VBOX_41 2
1015/** Saved state version for VirtualBox 3.0 and earlier.
1016 * This did not include the configuration part nor the E1kEEPROM. */
1017#define E1K_SAVEDSTATE_VERSION_VBOX_30 1
1018
1019/**
1020 * E1000 shared device state.
1021 *
1022 * This is shared between ring-0 and ring-3.
1023 */
1024typedef struct E1KSTATE
1025{
1026 char szPrf[8]; /**< Log prefix, e.g. E1000#1. */
1027
1028 /** Handle to PCI region \#0, the MMIO region. */
1029 IOMIOPORTHANDLE hMmioRegion;
1030 /** Handle to PCI region \#2, the I/O ports. */
1031 IOMIOPORTHANDLE hIoPorts;
1032
1033 /** Receive Interrupt Delay Timer. */
1034 TMTIMERHANDLE hRIDTimer;
1035 /** Receive Absolute Delay Timer. */
1036 TMTIMERHANDLE hRADTimer;
1037 /** Transmit Interrupt Delay Timer. */
1038 TMTIMERHANDLE hTIDTimer;
1039 /** Transmit Absolute Delay Timer. */
1040 TMTIMERHANDLE hTADTimer;
1041 /** Transmit Delay Timer. */
1042 TMTIMERHANDLE hTXDTimer;
1043 /** Late Interrupt Timer. */
1044 TMTIMERHANDLE hIntTimer;
1045 /** Link Up(/Restore) Timer. */
1046 TMTIMERHANDLE hLUTimer;
1047
1048 /** Transmit task. */
1049 PDMTASKHANDLE hTxTask;
1050
1051 /** Critical section - what is it protecting? */
1052 PDMCRITSECT cs;
1053 /** RX Critical section. */
1054 PDMCRITSECT csRx;
1055#ifdef E1K_WITH_TX_CS
1056 /** TX Critical section. */
1057 PDMCRITSECT csTx;
1058#endif /* E1K_WITH_TX_CS */
1059 /** MAC address obtained from the configuration. */
1060 RTMAC macConfigured;
1061 uint16_t u16Padding0;
1062 /** EMT: Last time the interrupt was acknowledged. */
1063 uint64_t u64AckedAt;
1064 /** All: Used for eliminating spurious interrupts. */
1065 bool fIntRaised;
1066 /** EMT: false if the cable is disconnected by the GUI. */
1067 bool fCableConnected;
1068 /** EMT: Compute Ethernet CRC for RX packets. */
1069 bool fEthernetCRC;
1070 /** All: throttle interrupts. */
1071 bool fItrEnabled;
1072 /** All: throttle RX interrupts. */
1073 bool fItrRxEnabled;
1074 /** All: Delay TX interrupts using TIDV/TADV. */
1075 bool fTidEnabled;
1076 bool afPadding[2];
1077 /** Link up delay (in milliseconds). */
1078 uint32_t cMsLinkUpDelay;
1079
1080 /** All: Device register storage. */
1081 uint32_t auRegs[E1K_NUM_OF_32BIT_REGS];
1082 /** TX/RX: Status LED. */
1083 PDMLED led;
1084 /** TX/RX: Number of packet being sent/received to show in debug log. */
1085 uint32_t u32PktNo;
1086
1087 /** EMT: Offset of the register to be read via IO. */
1088 uint32_t uSelectedReg;
1089 /** EMT: Multicast Table Array. */
1090 uint32_t auMTA[128];
1091 /** EMT: Receive Address registers. */
1092 E1KRA aRecAddr;
1093 /** EMT: VLAN filter table array. */
1094 uint32_t auVFTA[128];
1095 /** EMT: Receive buffer size. */
1096 uint16_t u16RxBSize;
1097 /** EMT: Locked state -- no state alteration possible. */
1098 bool fLocked;
1099 /** EMT: */
1100 bool fDelayInts;
1101 /** All: */
1102 bool fIntMaskUsed;
1103
1104 /** N/A: */
1105 bool volatile fMaybeOutOfSpace;
1106 /** EMT: Gets signalled when more RX descriptors become available. */
1107 SUPSEMEVENT hEventMoreRxDescAvail;
1108#ifdef E1K_WITH_RXD_CACHE
1109 /** RX: Fetched RX descriptors. */
1110 E1KRXDESC aRxDescriptors[E1K_RXD_CACHE_SIZE];
1111 //uint64_t aRxDescAddr[E1K_RXD_CACHE_SIZE];
1112 /** RX: Actual number of fetched RX descriptors. */
1113 uint32_t nRxDFetched;
1114 /** RX: Index in cache of RX descriptor being processed. */
1115 uint32_t iRxDCurrent;
1116#endif /* E1K_WITH_RXD_CACHE */
1117
1118 /** TX: Context used for TCP segmentation packets. */
1119 E1KTXCTX contextTSE;
1120 /** TX: Context used for ordinary packets. */
1121 E1KTXCTX contextNormal;
1122#ifdef E1K_WITH_TXD_CACHE
1123 /** TX: Fetched TX descriptors. */
1124 E1KTXDESC aTxDescriptors[E1K_TXD_CACHE_SIZE];
1125 /** TX: Actual number of fetched TX descriptors. */
1126 uint8_t nTxDFetched;
1127 /** TX: Index in cache of TX descriptor being processed. */
1128 uint8_t iTxDCurrent;
1129 /** TX: Will this frame be sent as GSO. */
1130 bool fGSO;
1131 /** Alignment padding. */
1132 bool fReserved;
1133 /** TX: Number of bytes in next packet. */
1134 uint32_t cbTxAlloc;
1135
1136#endif /* E1K_WITH_TXD_CACHE */
1137 /** GSO context. u8Type is set to PDMNETWORKGSOTYPE_INVALID when not
1138 * applicable to the current TSE mode. */
1139 PDMNETWORKGSO GsoCtx;
1140 /** Scratch space for holding the loopback / fallback scatter / gather
1141 * descriptor. */
1142 union
1143 {
1144 PDMSCATTERGATHER Sg;
1145 uint8_t padding[8 * sizeof(RTUINTPTR)];
1146 } uTxFallback;
1147 /** TX: Transmit packet buffer use for TSE fallback and loopback. */
1148 uint8_t aTxPacketFallback[E1K_MAX_TX_PKT_SIZE];
1149 /** TX: Number of bytes assembled in TX packet buffer. */
1150 uint16_t u16TxPktLen;
1151 /** TX: False will force segmentation in e1000 instead of sending frames as GSO. */
1152 bool fGSOEnabled;
1153 /** TX: IP checksum has to be inserted if true. */
1154 bool fIPcsum;
1155 /** TX: TCP/UDP checksum has to be inserted if true. */
1156 bool fTCPcsum;
1157 /** TX: VLAN tag has to be inserted if true. */
1158 bool fVTag;
1159 /** TX: TCI part of VLAN tag to be inserted. */
1160 uint16_t u16VTagTCI;
1161 /** TX TSE fallback: Number of payload bytes remaining in TSE context. */
1162 uint32_t u32PayRemain;
1163 /** TX TSE fallback: Number of header bytes remaining in TSE context. */
1164 uint16_t u16HdrRemain;
1165 /** TX TSE fallback: Flags from template header. */
1166 uint16_t u16SavedFlags;
1167 /** TX TSE fallback: Partial checksum from template header. */
1168 uint32_t u32SavedCsum;
1169 /** ?: Emulated controller type. */
1170 E1KCHIP eChip;
1171
1172 /** EMT: Physical interface emulation. */
1173 PHY phy;
1174
1175#if 0
1176 /** Alignment padding. */
1177 uint8_t Alignment[HC_ARCH_BITS == 64 ? 8 : 4];
1178#endif
1179
1180 STAMCOUNTER StatReceiveBytes;
1181 STAMCOUNTER StatTransmitBytes;
1182#if defined(VBOX_WITH_STATISTICS)
1183 STAMPROFILEADV StatMMIOReadRZ;
1184 STAMPROFILEADV StatMMIOReadR3;
1185 STAMPROFILEADV StatMMIOWriteRZ;
1186 STAMPROFILEADV StatMMIOWriteR3;
1187 STAMPROFILEADV StatEEPROMRead;
1188 STAMPROFILEADV StatEEPROMWrite;
1189 STAMPROFILEADV StatIOReadRZ;
1190 STAMPROFILEADV StatIOReadR3;
1191 STAMPROFILEADV StatIOWriteRZ;
1192 STAMPROFILEADV StatIOWriteR3;
1193 STAMPROFILEADV StatLateIntTimer;
1194 STAMCOUNTER StatLateInts;
1195 STAMCOUNTER StatIntsRaised;
1196 STAMCOUNTER StatIntsPrevented;
1197 STAMPROFILEADV StatReceive;
1198 STAMPROFILEADV StatReceiveCRC;
1199 STAMPROFILEADV StatReceiveFilter;
1200 STAMPROFILEADV StatReceiveStore;
1201 STAMPROFILEADV StatTransmitRZ;
1202 STAMPROFILEADV StatTransmitR3;
1203 STAMPROFILE StatTransmitSendRZ;
1204 STAMPROFILE StatTransmitSendR3;
1205 STAMPROFILE StatRxOverflow;
1206 STAMCOUNTER StatRxOverflowWakeupRZ;
1207 STAMCOUNTER StatRxOverflowWakeupR3;
1208 STAMCOUNTER StatTxDescCtxNormal;
1209 STAMCOUNTER StatTxDescCtxTSE;
1210 STAMCOUNTER StatTxDescLegacy;
1211 STAMCOUNTER StatTxDescData;
1212 STAMCOUNTER StatTxDescTSEData;
1213 STAMCOUNTER StatTxPathFallback;
1214 STAMCOUNTER StatTxPathGSO;
1215 STAMCOUNTER StatTxPathRegular;
1216 STAMCOUNTER StatPHYAccesses;
1217 STAMCOUNTER aStatRegWrites[E1K_NUM_OF_REGS];
1218 STAMCOUNTER aStatRegReads[E1K_NUM_OF_REGS];
1219#endif /* VBOX_WITH_STATISTICS */
1220
1221#ifdef E1K_INT_STATS
1222 /* Internal stats */
1223 uint64_t u64ArmedAt;
1224 uint64_t uStatMaxTxDelay;
1225 uint32_t uStatInt;
1226 uint32_t uStatIntTry;
1227 uint32_t uStatIntLower;
1228 uint32_t uStatNoIntICR;
1229 int32_t iStatIntLost;
1230 int32_t iStatIntLostOne;
1231 uint32_t uStatIntIMS;
1232 uint32_t uStatIntSkip;
1233 uint32_t uStatIntLate;
1234 uint32_t uStatIntMasked;
1235 uint32_t uStatIntEarly;
1236 uint32_t uStatIntRx;
1237 uint32_t uStatIntTx;
1238 uint32_t uStatIntICS;
1239 uint32_t uStatIntRDTR;
1240 uint32_t uStatIntRXDMT0;
1241 uint32_t uStatIntTXQE;
1242 uint32_t uStatTxNoRS;
1243 uint32_t uStatTxIDE;
1244 uint32_t uStatTxDelayed;
1245 uint32_t uStatTxDelayExp;
1246 uint32_t uStatTAD;
1247 uint32_t uStatTID;
1248 uint32_t uStatRAD;
1249 uint32_t uStatRID;
1250 uint32_t uStatRxFrm;
1251 uint32_t uStatTxFrm;
1252 uint32_t uStatDescCtx;
1253 uint32_t uStatDescDat;
1254 uint32_t uStatDescLeg;
1255 uint32_t uStatTx1514;
1256 uint32_t uStatTx2962;
1257 uint32_t uStatTx4410;
1258 uint32_t uStatTx5858;
1259 uint32_t uStatTx7306;
1260 uint32_t uStatTx8754;
1261 uint32_t uStatTx16384;
1262 uint32_t uStatTx32768;
1263 uint32_t uStatTxLarge;
1264 uint32_t uStatAlign;
1265#endif /* E1K_INT_STATS */
1266} E1KSTATE;
1267/** Pointer to the E1000 device state. */
1268typedef E1KSTATE *PE1KSTATE;
1269
1270/**
1271 * E1000 ring-3 device state
1272 *
1273 * @implements PDMINETWORKDOWN
1274 * @implements PDMINETWORKCONFIG
1275 * @implements PDMILEDPORTS
1276 */
1277typedef struct E1KSTATER3
1278{
1279 PDMIBASE IBase;
1280 PDMINETWORKDOWN INetworkDown;
1281 PDMINETWORKCONFIG INetworkConfig;
1282 /** LED interface */
1283 PDMILEDPORTS ILeds;
1284 /** Attached network driver. */
1285 R3PTRTYPE(PPDMIBASE) pDrvBase;
1286 R3PTRTYPE(PPDMILEDCONNECTORS) pLedsConnector;
1287
1288 /** Pointer to the shared state. */
1289 R3PTRTYPE(PE1KSTATE) pShared;
1290
1291 /** Device instance. */
1292 PPDMDEVINSR3 pDevInsR3;
1293 /** Attached network driver. */
1294 PPDMINETWORKUPR3 pDrvR3;
1295 /** The scatter / gather buffer used for the current outgoing packet. */
1296 R3PTRTYPE(PPDMSCATTERGATHER) pTxSgR3;
1297
1298 /** EMT: EEPROM emulation */
1299 E1kEEPROM eeprom;
1300} E1KSTATER3;
1301/** Pointer to the E1000 ring-3 device state. */
1302typedef E1KSTATER3 *PE1KSTATER3;
1303
1304
1305/**
1306 * E1000 ring-0 device state
1307 */
1308typedef struct E1KSTATER0
1309{
1310 /** Device instance. */
1311 PPDMDEVINSR0 pDevInsR0;
1312 /** Attached network driver. */
1313 PPDMINETWORKUPR0 pDrvR0;
1314 /** The scatter / gather buffer used for the current outgoing packet - R0. */
1315 R0PTRTYPE(PPDMSCATTERGATHER) pTxSgR0;
1316} E1KSTATER0;
1317/** Pointer to the E1000 ring-0 device state. */
1318typedef E1KSTATER0 *PE1KSTATER0;
1319
1320
1321/**
1322 * E1000 raw-mode device state
1323 */
1324typedef struct E1KSTATERC
1325{
1326 /** Device instance. */
1327 PPDMDEVINSRC pDevInsRC;
1328 /** Attached network driver. */
1329 PPDMINETWORKUPRC pDrvRC;
1330 /** The scatter / gather buffer used for the current outgoing packet. */
1331 RCPTRTYPE(PPDMSCATTERGATHER) pTxSgRC;
1332} E1KSTATERC;
1333/** Pointer to the E1000 raw-mode device state. */
1334typedef E1KSTATERC *PE1KSTATERC;
1335
1336
1337/** @def PE1KSTATECC
1338 * Pointer to the instance data for the current context. */
1339#ifdef IN_RING3
1340typedef E1KSTATER3 E1KSTATECC;
1341typedef PE1KSTATER3 PE1KSTATECC;
1342#elif defined(IN_RING0)
1343typedef E1KSTATER0 E1KSTATECC;
1344typedef PE1KSTATER0 PE1KSTATECC;
1345#elif defined(IN_RC)
1346typedef E1KSTATERC E1KSTATECC;
1347typedef PE1KSTATERC PE1KSTATECC;
1348#else
1349# error "Not IN_RING3, IN_RING0 or IN_RC"
1350#endif
1351
1352
1353#ifndef VBOX_DEVICE_STRUCT_TESTCASE
1354
1355/* Forward declarations ******************************************************/
1356static int e1kXmitPending(PPDMDEVINS pDevIns, PE1KSTATE pThis, bool fOnWorkerThread);
1357
1358/**
1359 * E1000 register read handler.
1360 */
1361typedef int (FNE1KREGREAD)(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value);
1362/**
1363 * E1000 register write handler.
1364 */
1365typedef int (FNE1KREGWRITE)(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t u32Value);
1366
1367static FNE1KREGREAD e1kRegReadUnimplemented;
1368static FNE1KREGWRITE e1kRegWriteUnimplemented;
1369static FNE1KREGREAD e1kRegReadAutoClear;
1370static FNE1KREGREAD e1kRegReadDefault;
1371static FNE1KREGWRITE e1kRegWriteDefault;
1372#if 0 /* unused */
1373static FNE1KREGREAD e1kRegReadCTRL;
1374#endif
1375static FNE1KREGWRITE e1kRegWriteCTRL;
1376static FNE1KREGREAD e1kRegReadEECD;
1377static FNE1KREGWRITE e1kRegWriteEECD;
1378static FNE1KREGWRITE e1kRegWriteEERD;
1379static FNE1KREGWRITE e1kRegWriteMDIC;
1380static FNE1KREGREAD e1kRegReadICR;
1381static FNE1KREGWRITE e1kRegWriteICR;
1382static FNE1KREGWRITE e1kRegWriteICS;
1383static FNE1KREGWRITE e1kRegWriteIMS;
1384static FNE1KREGWRITE e1kRegWriteIMC;
1385static FNE1KREGWRITE e1kRegWriteRCTL;
1386static FNE1KREGWRITE e1kRegWritePBA;
1387static FNE1KREGWRITE e1kRegWriteRDT;
1388static FNE1KREGWRITE e1kRegWriteRDTR;
1389static FNE1KREGWRITE e1kRegWriteTDT;
1390static FNE1KREGREAD e1kRegReadMTA;
1391static FNE1KREGWRITE e1kRegWriteMTA;
1392static FNE1KREGREAD e1kRegReadRA;
1393static FNE1KREGWRITE e1kRegWriteRA;
1394static FNE1KREGREAD e1kRegReadVFTA;
1395static FNE1KREGWRITE e1kRegWriteVFTA;
1396
1397/**
1398 * Register map table.
1399 *
1400 * Override pfnRead and pfnWrite to get register-specific behavior.
1401 */
1402static const struct E1kRegMap_st
1403{
1404 /** Register offset in the register space. */
1405 uint32_t offset;
1406 /** Size in bytes. Registers of size > 4 are in fact tables. */
1407 uint32_t size;
1408 /** Readable bits. */
1409 uint32_t readable;
1410 /** Writable bits. */
1411 uint32_t writable;
1412 /** Read callback. */
1413 FNE1KREGREAD *pfnRead;
1414 /** Write callback. */
1415 FNE1KREGWRITE *pfnWrite;
1416 /** Abbreviated name. */
1417 const char *abbrev;
1418 /** Full name. */
1419 const char *name;
1420} g_aE1kRegMap[E1K_NUM_OF_REGS] =
1421{
1422 /* offset size read mask write mask read callback write callback abbrev full name */
1423 /*------- ------- ---------- ---------- ----------------------- ------------------------ ---------- ------------------------------*/
1424 { 0x00000, 0x00004, 0xDBF31BE9, 0xDBF31BE9, e1kRegReadDefault , e1kRegWriteCTRL , "CTRL" , "Device Control" },
1425 { 0x00008, 0x00004, 0x0000FDFF, 0x00000000, e1kRegReadDefault , e1kRegWriteUnimplemented, "STATUS" , "Device Status" },
1426 { 0x00010, 0x00004, 0x000027F0, 0x00000070, e1kRegReadEECD , e1kRegWriteEECD , "EECD" , "EEPROM/Flash Control/Data" },
1427 { 0x00014, 0x00004, 0xFFFFFF10, 0xFFFFFF00, e1kRegReadDefault , e1kRegWriteEERD , "EERD" , "EEPROM Read" },
1428 { 0x00018, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "CTRL_EXT", "Extended Device Control" },
1429 { 0x0001c, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FLA" , "Flash Access (N/A)" },
1430 { 0x00020, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteMDIC , "MDIC" , "MDI Control" },
1431 { 0x00028, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FCAL" , "Flow Control Address Low" },
1432 { 0x0002c, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FCAH" , "Flow Control Address High" },
1433 { 0x00030, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FCT" , "Flow Control Type" },
1434 { 0x00038, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteDefault , "VET" , "VLAN EtherType" },
1435 { 0x000c0, 0x00004, 0x0001F6DF, 0x0001F6DF, e1kRegReadICR , e1kRegWriteICR , "ICR" , "Interrupt Cause Read" },
1436 { 0x000c4, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteDefault , "ITR" , "Interrupt Throttling" },
1437 { 0x000c8, 0x00004, 0x00000000, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteICS , "ICS" , "Interrupt Cause Set" },
1438 { 0x000d0, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteIMS , "IMS" , "Interrupt Mask Set/Read" },
1439 { 0x000d8, 0x00004, 0x00000000, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteIMC , "IMC" , "Interrupt Mask Clear" },
1440 { 0x00100, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteRCTL , "RCTL" , "Receive Control" },
1441 { 0x00170, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FCTTV" , "Flow Control Transmit Timer Value" },
1442 { 0x00178, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TXCW" , "Transmit Configuration Word (N/A)" },
1443 { 0x00180, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RXCW" , "Receive Configuration Word (N/A)" },
1444 { 0x00400, 0x00004, 0x017FFFFA, 0x017FFFFA, e1kRegReadDefault , e1kRegWriteDefault , "TCTL" , "Transmit Control" },
1445 { 0x00410, 0x00004, 0x3FFFFFFF, 0x3FFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "TIPG" , "Transmit IPG" },
1446 { 0x00458, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteDefault , "AIFS" , "Adaptive IFS Throttle - AIT" },
1447 { 0x00e00, 0x00004, 0xCFCFCFCF, 0xCFCFCFCF, e1kRegReadDefault , e1kRegWriteDefault , "LEDCTL" , "LED Control" },
1448 { 0x01000, 0x00004, 0xFFFF007F, 0x0000007F, e1kRegReadDefault , e1kRegWritePBA , "PBA" , "Packet Buffer Allocation" },
1449 { 0x02160, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FCRTL" , "Flow Control Receive Threshold Low" },
1450 { 0x02168, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FCRTH" , "Flow Control Receive Threshold High" },
1451 { 0x02410, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RDFH" , "Receive Data FIFO Head" },
1452 { 0x02418, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RDFT" , "Receive Data FIFO Tail" },
1453 { 0x02420, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RDFHS" , "Receive Data FIFO Head Saved Register" },
1454 { 0x02428, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RDFTS" , "Receive Data FIFO Tail Saved Register" },
1455 { 0x02430, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RDFPC" , "Receive Data FIFO Packet Count" },
1456 { 0x02800, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "RDBAL" , "Receive Descriptor Base Low" },
1457 { 0x02804, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "RDBAH" , "Receive Descriptor Base High" },
1458 { 0x02808, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "RDLEN" , "Receive Descriptor Length" },
1459 { 0x02810, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "RDH" , "Receive Descriptor Head" },
1460 { 0x02818, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteRDT , "RDT" , "Receive Descriptor Tail" },
1461 { 0x02820, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteRDTR , "RDTR" , "Receive Delay Timer" },
1462 { 0x02828, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RXDCTL" , "Receive Descriptor Control" },
1463 { 0x0282c, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteDefault , "RADV" , "Receive Interrupt Absolute Delay Timer" },
1464 { 0x02c00, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RSRPD" , "Receive Small Packet Detect Interrupt" },
1465 { 0x03000, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TXDMAC" , "TX DMA Control (N/A)" },
1466 { 0x03410, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TDFH" , "Transmit Data FIFO Head" },
1467 { 0x03418, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TDFT" , "Transmit Data FIFO Tail" },
1468 { 0x03420, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TDFHS" , "Transmit Data FIFO Head Saved Register" },
1469 { 0x03428, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TDFTS" , "Transmit Data FIFO Tail Saved Register" },
1470 { 0x03430, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TDFPC" , "Transmit Data FIFO Packet Count" },
1471 { 0x03800, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "TDBAL" , "Transmit Descriptor Base Low" },
1472 { 0x03804, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "TDBAH" , "Transmit Descriptor Base High" },
1473 { 0x03808, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "TDLEN" , "Transmit Descriptor Length" },
1474 { 0x03810, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteDefault , "TDH" , "Transmit Descriptor Head" },
1475 { 0x03818, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteTDT , "TDT" , "Transmit Descriptor Tail" },
1476 { 0x03820, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteDefault , "TIDV" , "Transmit Interrupt Delay Value" },
1477 { 0x03828, 0x00004, 0xFF3F3F3F, 0xFF3F3F3F, e1kRegReadDefault , e1kRegWriteDefault , "TXDCTL" , "Transmit Descriptor Control" },
1478 { 0x0382c, 0x00004, 0x0000FFFF, 0x0000FFFF, e1kRegReadDefault , e1kRegWriteDefault , "TADV" , "Transmit Absolute Interrupt Delay Timer" },
1479 { 0x03830, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "TSPMT" , "TCP Segmentation Pad and Threshold" },
1480 { 0x04000, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "CRCERRS" , "CRC Error Count" },
1481 { 0x04004, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "ALGNERRC", "Alignment Error Count" },
1482 { 0x04008, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "SYMERRS" , "Symbol Error Count" },
1483 { 0x0400c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RXERRC" , "RX Error Count" },
1484 { 0x04010, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "MPC" , "Missed Packets Count" },
1485 { 0x04014, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "SCC" , "Single Collision Count" },
1486 { 0x04018, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "ECOL" , "Excessive Collisions Count" },
1487 { 0x0401c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "MCC" , "Multiple Collision Count" },
1488 { 0x04020, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "LATECOL" , "Late Collisions Count" },
1489 { 0x04028, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "COLC" , "Collision Count" },
1490 { 0x04030, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "DC" , "Defer Count" },
1491 { 0x04034, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "TNCRS" , "Transmit - No CRS" },
1492 { 0x04038, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "SEC" , "Sequence Error Count" },
1493 { 0x0403c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "CEXTERR" , "Carrier Extension Error Count" },
1494 { 0x04040, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RLEC" , "Receive Length Error Count" },
1495 { 0x04048, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "XONRXC" , "XON Received Count" },
1496 { 0x0404c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "XONTXC" , "XON Transmitted Count" },
1497 { 0x04050, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "XOFFRXC" , "XOFF Received Count" },
1498 { 0x04054, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "XOFFTXC" , "XOFF Transmitted Count" },
1499 { 0x04058, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FCRUC" , "FC Received Unsupported Count" },
1500 { 0x0405c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PRC64" , "Packets Received (64 Bytes) Count" },
1501 { 0x04060, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PRC127" , "Packets Received (65-127 Bytes) Count" },
1502 { 0x04064, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PRC255" , "Packets Received (128-255 Bytes) Count" },
1503 { 0x04068, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PRC511" , "Packets Received (256-511 Bytes) Count" },
1504 { 0x0406c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PRC1023" , "Packets Received (512-1023 Bytes) Count" },
1505 { 0x04070, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PRC1522" , "Packets Received (1024-Max Bytes)" },
1506 { 0x04074, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "GPRC" , "Good Packets Received Count" },
1507 { 0x04078, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "BPRC" , "Broadcast Packets Received Count" },
1508 { 0x0407c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "MPRC" , "Multicast Packets Received Count" },
1509 { 0x04080, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "GPTC" , "Good Packets Transmitted Count" },
1510 { 0x04088, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "GORCL" , "Good Octets Received Count (Low)" },
1511 { 0x0408c, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "GORCH" , "Good Octets Received Count (Hi)" },
1512 { 0x04090, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "GOTCL" , "Good Octets Transmitted Count (Low)" },
1513 { 0x04094, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "GOTCH" , "Good Octets Transmitted Count (Hi)" },
1514 { 0x040a0, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RNBC" , "Receive No Buffers Count" },
1515 { 0x040a4, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RUC" , "Receive Undersize Count" },
1516 { 0x040a8, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RFC" , "Receive Fragment Count" },
1517 { 0x040ac, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "ROC" , "Receive Oversize Count" },
1518 { 0x040b0, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "RJC" , "Receive Jabber Count" },
1519 { 0x040b4, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "MGTPRC" , "Management Packets Received Count" },
1520 { 0x040b8, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "MGTPDC" , "Management Packets Dropped Count" },
1521 { 0x040bc, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "MGTPTC" , "Management Pkts Transmitted Count" },
1522 { 0x040c0, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TORL" , "Total Octets Received (Lo)" },
1523 { 0x040c4, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TORH" , "Total Octets Received (Hi)" },
1524 { 0x040c8, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TOTL" , "Total Octets Transmitted (Lo)" },
1525 { 0x040cc, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TOTH" , "Total Octets Transmitted (Hi)" },
1526 { 0x040d0, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TPR" , "Total Packets Received" },
1527 { 0x040d4, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TPT" , "Total Packets Transmitted" },
1528 { 0x040d8, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PTC64" , "Packets Transmitted (64 Bytes) Count" },
1529 { 0x040dc, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PTC127" , "Packets Transmitted (65-127 Bytes) Count" },
1530 { 0x040e0, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PTC255" , "Packets Transmitted (128-255 Bytes) Count" },
1531 { 0x040e4, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PTC511" , "Packets Transmitted (256-511 Bytes) Count" },
1532 { 0x040e8, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PTC1023" , "Packets Transmitted (512-1023 Bytes) Count" },
1533 { 0x040ec, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "PTC1522" , "Packets Transmitted (1024 Bytes or Greater) Count" },
1534 { 0x040f0, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "MPTC" , "Multicast Packets Transmitted Count" },
1535 { 0x040f4, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "BPTC" , "Broadcast Packets Transmitted Count" },
1536 { 0x040f8, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TSCTC" , "TCP Segmentation Context Transmitted Count" },
1537 { 0x040fc, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadAutoClear , e1kRegWriteUnimplemented, "TSCTFC" , "TCP Segmentation Context Tx Fail Count" },
1538 { 0x05000, 0x00004, 0x000007FF, 0x000007FF, e1kRegReadDefault , e1kRegWriteDefault , "RXCSUM" , "Receive Checksum Control" },
1539 { 0x05800, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "WUC" , "Wakeup Control" },
1540 { 0x05808, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "WUFC" , "Wakeup Filter Control" },
1541 { 0x05810, 0x00004, 0xFFFFFFFF, 0x00000000, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "WUS" , "Wakeup Status" },
1542 { 0x05820, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadDefault , e1kRegWriteDefault , "MANC" , "Management Control" },
1543 { 0x05838, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "IPAV" , "IP Address Valid" },
1544 { 0x05900, 0x00004, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "WUPL" , "Wakeup Packet Length" },
1545 { 0x05200, 0x00200, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadMTA , e1kRegWriteMTA , "MTA" , "Multicast Table Array (n)" },
1546 { 0x05400, 0x00080, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadRA , e1kRegWriteRA , "RA" , "Receive Address (64-bit) (n)" },
1547 { 0x05600, 0x00200, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadVFTA , e1kRegWriteVFTA , "VFTA" , "VLAN Filter Table Array (n)" },
1548 { 0x05840, 0x0001c, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "IP4AT" , "IPv4 Address Table" },
1549 { 0x05880, 0x00010, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "IP6AT" , "IPv6 Address Table" },
1550 { 0x05a00, 0x00080, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "WUPM" , "Wakeup Packet Memory" },
1551 { 0x05f00, 0x0001c, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FFLT" , "Flexible Filter Length Table" },
1552 { 0x09000, 0x003fc, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FFMT" , "Flexible Filter Mask Table" },
1553 { 0x09800, 0x003fc, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "FFVT" , "Flexible Filter Value Table" },
1554 { 0x10000, 0x10000, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadUnimplemented, e1kRegWriteUnimplemented, "PBM" , "Packet Buffer Memory (n)" },
1555 { 0x00040, 0x00080, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadRA , e1kRegWriteRA , "RA82542" , "Receive Address (64-bit) (n) (82542)" },
1556 { 0x00200, 0x00200, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadMTA , e1kRegWriteMTA , "MTA82542", "Multicast Table Array (n) (82542)" },
1557 { 0x00600, 0x00200, 0xFFFFFFFF, 0xFFFFFFFF, e1kRegReadVFTA , e1kRegWriteVFTA , "VFTA82542", "VLAN Filter Table Array (n) (82542)" }
1558};
1559
1560#ifdef LOG_ENABLED
1561
1562/**
1563 * Convert U32 value to hex string. Masked bytes are replaced with dots.
1564 *
1565 * @remarks The mask has half-byte byte (not bit) granularity (e.g. 0000000F).
1566 *
1567 * @returns The buffer.
1568 *
1569 * @param u32 The word to convert into string.
1570 * @param mask Selects which bytes to convert.
1571 * @param buf Where to put the result.
1572 */
1573static char *e1kU32toHex(uint32_t u32, uint32_t mask, char *buf)
1574{
1575 for (char *ptr = buf + 7; ptr >= buf; --ptr, u32 >>=4, mask >>=4)
1576 {
1577 if (mask & 0xF)
1578 *ptr = (u32 & 0xF) + ((u32 & 0xF) > 9 ? '7' : '0');
1579 else
1580 *ptr = '.';
1581 }
1582 buf[8] = 0;
1583 return buf;
1584}
1585
1586/**
1587 * Returns timer name for debug purposes.
1588 *
1589 * @returns The timer name.
1590 *
1591 * @param pThis The device state structure.
1592 * @param hTimer The timer to name.
1593 */
1594DECLINLINE(const char *) e1kGetTimerName(PE1KSTATE pThis, TMTIMERHANDLE hTimer)
1595{
1596 if (hTimer == pThis->hTIDTimer)
1597 return "TID";
1598 if (hTimer == pThis->hTADTimer)
1599 return "TAD";
1600 if (hTimer == pThis->hRIDTimer)
1601 return "RID";
1602 if (hTimer == pThis->hRADTimer)
1603 return "RAD";
1604 if (hTimer == pThis->hIntTimer)
1605 return "Int";
1606 if (hTimer == pThis->hTXDTimer)
1607 return "TXD";
1608 if (hTimer == pThis->hLUTimer)
1609 return "LinkUp";
1610 return "unknown";
1611}
1612
1613#endif /* LOG_ENABLED */
1614
1615/**
1616 * Arm a timer.
1617 *
1618 * @param pDevIns The device instance.
1619 * @param pThis Pointer to the device state structure.
1620 * @param hTimer The timer to arm.
1621 * @param uExpireIn Expiration interval in microseconds.
1622 */
1623DECLINLINE(void) e1kArmTimer(PPDMDEVINS pDevIns, PE1KSTATE pThis, TMTIMERHANDLE hTimer, uint32_t uExpireIn)
1624{
1625 if (pThis->fLocked)
1626 return;
1627
1628 E1kLog2(("%s Arming %s timer to fire in %d usec...\n",
1629 pThis->szPrf, e1kGetTimerName(pThis, hTimer), uExpireIn));
1630 int rc = PDMDevHlpTimerSetMicro(pDevIns, hTimer, uExpireIn);
1631 AssertRC(rc);
1632}
1633
1634#ifdef IN_RING3
1635/**
1636 * Cancel a timer.
1637 *
1638 * @param pDevIns The device instance.
1639 * @param pThis Pointer to the device state structure.
1640 * @param pTimer Pointer to the timer.
1641 */
1642DECLINLINE(void) e1kCancelTimer(PPDMDEVINS pDevIns, PE1KSTATE pThis, TMTIMERHANDLE hTimer)
1643{
1644 E1kLog2(("%s Stopping %s timer...\n",
1645 pThis->szPrf, e1kGetTimerName(pThis, hTimer)));
1646 int rc = PDMDevHlpTimerStop(pDevIns, hTimer);
1647 if (RT_FAILURE(rc))
1648 E1kLog2(("%s e1kCancelTimer: TMTimerStop(%s) failed with %Rrc\n",
1649 pThis->szPrf, e1kGetTimerName(pThis, hTimer), rc));
1650 RT_NOREF_PV(pThis);
1651}
1652#endif /* IN_RING3 */
1653
1654#define e1kCsEnter(ps, rc) PDMDevHlpCritSectEnter(pDevIns, &ps->cs, rc)
1655#define e1kCsLeave(ps) PDMDevHlpCritSectLeave(pDevIns, &ps->cs)
1656
1657#define e1kCsRxEnter(ps, rc) PDMDevHlpCritSectEnter(pDevIns, &ps->csRx, rc)
1658#define e1kCsRxLeave(ps) PDMDevHlpCritSectLeave(pDevIns, &ps->csRx)
1659#define e1kCsRxIsOwner(ps) PDMDevHlpCritSectIsOwner(pDevIns, &ps->csRx)
1660
1661#ifndef E1K_WITH_TX_CS
1662# define e1kCsTxEnter(ps, rc) VINF_SUCCESS
1663# define e1kCsTxLeave(ps) do { } while (0)
1664#else /* E1K_WITH_TX_CS */
1665# define e1kCsTxEnter(ps, rc) PDMDevHlpCritSectEnter(pDevIns, &ps->csTx, rc)
1666# define e1kCsTxLeave(ps) PDMDevHlpCritSectLeave(pDevIns, &ps->csTx)
1667#endif /* E1K_WITH_TX_CS */
1668
1669
1670/**
1671 * Wakeup the RX thread.
1672 */
1673static void e1kWakeupReceive(PPDMDEVINS pDevIns, PE1KSTATE pThis)
1674{
1675 if ( pThis->fMaybeOutOfSpace
1676 && pThis->hEventMoreRxDescAvail != NIL_SUPSEMEVENT)
1677 {
1678 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatRxOverflowWakeup));
1679 E1kLog(("%s Waking up Out-of-RX-space semaphore\n", pThis->szPrf));
1680 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEventMoreRxDescAvail);
1681 AssertRC(rc);
1682 }
1683}
1684
1685#ifdef IN_RING3
1686
1687/**
1688 * Hardware reset. Revert all registers to initial values.
1689 *
1690 * @param pDevIns The device instance.
1691 * @param pThis The device state structure.
1692 * @param pThisCC The current context instance data.
1693 */
1694static void e1kR3HardReset(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC)
1695{
1696 E1kLog(("%s Hard reset triggered\n", pThis->szPrf));
1697 /* No interrupts should survive device reset, see @bugref(9556). */
1698 if (pThis->fIntRaised)
1699 {
1700 /* Lower(0) INTA(0) */
1701 PDMDevHlpPCISetIrq(pDevIns, 0, 0);
1702 pThis->fIntRaised = false;
1703 E1kLog(("%s e1kR3HardReset: Lowered IRQ: ICR=%08x\n", pThis->szPrf, ICR));
1704 }
1705 memset(pThis->auRegs, 0, sizeof(pThis->auRegs));
1706 memset(pThis->aRecAddr.au32, 0, sizeof(pThis->aRecAddr.au32));
1707#ifdef E1K_INIT_RA0
1708 memcpy(pThis->aRecAddr.au32, pThis->macConfigured.au8,
1709 sizeof(pThis->macConfigured.au8));
1710 pThis->aRecAddr.array[0].ctl |= RA_CTL_AV;
1711#endif /* E1K_INIT_RA0 */
1712 STATUS = 0x0081; /* SPEED=10b (1000 Mb/s), FD=1b (Full Duplex) */
1713 EECD = 0x0100; /* EE_PRES=1b (EEPROM present) */
1714 CTRL = 0x0a09; /* FRCSPD=1b SPEED=10b LRST=1b FD=1b */
1715 TSPMT = 0x01000400;/* TSMT=0400h TSPBP=0100h */
1716 Assert(GET_BITS(RCTL, BSIZE) == 0);
1717 pThis->u16RxBSize = 2048;
1718
1719 uint16_t u16LedCtl = 0x0602; /* LED0/LINK_UP#, LED2/LINK100# */
1720 pThisCC->eeprom.readWord(0x2F, &u16LedCtl); /* Read LEDCTL defaults from EEPROM */
1721 LEDCTL = 0x07008300 | (((uint32_t)u16LedCtl & 0xCF00) << 8) | (u16LedCtl & 0xCF); /* Only LED0 and LED2 defaults come from EEPROM */
1722
1723 /* Reset promiscuous mode */
1724 if (pThisCC->pDrvR3)
1725 pThisCC->pDrvR3->pfnSetPromiscuousMode(pThisCC->pDrvR3, false);
1726
1727#ifdef E1K_WITH_TXD_CACHE
1728 int rc = e1kCsTxEnter(pThis, VERR_SEM_BUSY);
1729 if (RT_LIKELY(rc == VINF_SUCCESS))
1730 {
1731 pThis->nTxDFetched = 0;
1732 pThis->iTxDCurrent = 0;
1733 pThis->fGSO = false;
1734 pThis->cbTxAlloc = 0;
1735 e1kCsTxLeave(pThis);
1736 }
1737#endif /* E1K_WITH_TXD_CACHE */
1738#ifdef E1K_WITH_RXD_CACHE
1739 if (RT_LIKELY(e1kCsRxEnter(pThis, VERR_SEM_BUSY) == VINF_SUCCESS))
1740 {
1741 pThis->iRxDCurrent = pThis->nRxDFetched = 0;
1742 e1kCsRxLeave(pThis);
1743 }
1744#endif /* E1K_WITH_RXD_CACHE */
1745#ifdef E1K_LSC_ON_RESET
1746 E1kLog(("%s Will trigger LSC in %d seconds...\n",
1747 pThis->szPrf, pThis->cMsLinkUpDelay / 1000));
1748 e1kArmTimer(pDevIns, pThis, pThis->hLUTimer, pThis->cMsLinkUpDelay * 1000);
1749#endif /* E1K_LSC_ON_RESET */
1750}
1751
1752#endif /* IN_RING3 */
1753
1754/**
1755 * Compute Internet checksum.
1756 *
1757 * @remarks Refer to http://www.netfor2.com/checksum.html for short intro.
1758 *
1759 * @param pThis The device state structure.
1760 * @param cpPacket The packet.
1761 * @param cb The size of the packet.
1762 * @param pszText A string denoting direction of packet transfer.
1763 *
1764 * @return The 1's complement of the 1's complement sum.
1765 *
1766 * @thread E1000_TX
1767 */
1768static uint16_t e1kCSum16(const void *pvBuf, size_t cb)
1769{
1770 uint32_t csum = 0;
1771 uint16_t *pu16 = (uint16_t *)pvBuf;
1772
1773 while (cb > 1)
1774 {
1775 csum += *pu16++;
1776 cb -= 2;
1777 }
1778 if (cb)
1779 csum += *(uint8_t*)pu16;
1780 while (csum >> 16)
1781 csum = (csum >> 16) + (csum & 0xFFFF);
1782 Assert(csum < 65536);
1783 return (uint16_t)~csum;
1784}
1785
1786/**
1787 * Dump a packet to debug log.
1788 *
1789 * @param pDevIns The device instance.
1790 * @param pThis The device state structure.
1791 * @param cpPacket The packet.
1792 * @param cb The size of the packet.
1793 * @param pszText A string denoting direction of packet transfer.
1794 * @thread E1000_TX
1795 */
1796DECLINLINE(void) e1kPacketDump(PPDMDEVINS pDevIns, PE1KSTATE pThis, const uint8_t *cpPacket, size_t cb, const char *pszText)
1797{
1798#ifdef DEBUG
1799 if (RT_LIKELY(e1kCsEnter(pThis, VERR_SEM_BUSY) == VINF_SUCCESS))
1800 {
1801 Log4(("%s --- %s packet #%d: %RTmac => %RTmac (%d bytes) ---\n",
1802 pThis->szPrf, pszText, ++pThis->u32PktNo, cpPacket+6, cpPacket, cb));
1803 if (ntohs(*(uint16_t*)(cpPacket+12)) == 0x86DD)
1804 {
1805 Log4(("%s --- IPv6: %RTnaipv6 => %RTnaipv6\n",
1806 pThis->szPrf, cpPacket+14+8, cpPacket+14+24));
1807 if (*(cpPacket+14+6) == 0x6)
1808 Log4(("%s --- TCP: seq=%x ack=%x\n", pThis->szPrf,
1809 ntohl(*(uint32_t*)(cpPacket+14+40+4)), ntohl(*(uint32_t*)(cpPacket+14+40+8))));
1810 }
1811 else if (ntohs(*(uint16_t*)(cpPacket+12)) == 0x800)
1812 {
1813 Log4(("%s --- IPv4: %RTnaipv4 => %RTnaipv4\n",
1814 pThis->szPrf, *(uint32_t*)(cpPacket+14+12), *(uint32_t*)(cpPacket+14+16)));
1815 if (*(cpPacket+14+6) == 0x6)
1816 Log4(("%s --- TCP: seq=%x ack=%x\n", pThis->szPrf,
1817 ntohl(*(uint32_t*)(cpPacket+14+20+4)), ntohl(*(uint32_t*)(cpPacket+14+20+8))));
1818 }
1819 E1kLog3(("%.*Rhxd\n", cb, cpPacket));
1820 e1kCsLeave(pThis);
1821 }
1822#else
1823 if (RT_LIKELY(e1kCsEnter(pThis, VERR_SEM_BUSY) == VINF_SUCCESS))
1824 {
1825 if (ntohs(*(uint16_t*)(cpPacket+12)) == 0x86DD)
1826 E1kLogRel(("E1000: %s packet #%d, %RTmac => %RTmac, %RTnaipv6 => %RTnaipv6, seq=%x ack=%x\n",
1827 pszText, ++pThis->u32PktNo, cpPacket+6, cpPacket, cpPacket+14+8, cpPacket+14+24,
1828 ntohl(*(uint32_t*)(cpPacket+14+40+4)), ntohl(*(uint32_t*)(cpPacket+14+40+8))));
1829 else
1830 E1kLogRel(("E1000: %s packet #%d, %RTmac => %RTmac, %RTnaipv4 => %RTnaipv4, seq=%x ack=%x\n",
1831 pszText, ++pThis->u32PktNo, cpPacket+6, cpPacket,
1832 *(uint32_t*)(cpPacket+14+12), *(uint32_t*)(cpPacket+14+16),
1833 ntohl(*(uint32_t*)(cpPacket+14+20+4)), ntohl(*(uint32_t*)(cpPacket+14+20+8))));
1834 e1kCsLeave(pThis);
1835 }
1836 RT_NOREF2(cb, pszText);
1837#endif
1838}
1839
1840/**
1841 * Determine the type of transmit descriptor.
1842 *
1843 * @returns Descriptor type. See E1K_DTYP_XXX defines.
1844 *
1845 * @param pDesc Pointer to descriptor union.
1846 * @thread E1000_TX
1847 */
1848DECLINLINE(int) e1kGetDescType(E1KTXDESC *pDesc)
1849{
1850 if (pDesc->legacy.cmd.fDEXT)
1851 return pDesc->context.dw2.u4DTYP;
1852 return E1K_DTYP_LEGACY;
1853}
1854
1855
1856#ifdef E1K_WITH_RXD_CACHE
1857/**
1858 * Return the number of RX descriptor that belong to the hardware.
1859 *
1860 * @returns the number of available descriptors in RX ring.
1861 * @param pThis The device state structure.
1862 * @thread ???
1863 */
1864DECLINLINE(uint32_t) e1kGetRxLen(PE1KSTATE pThis)
1865{
1866 /**
1867 * Make sure RDT won't change during computation. EMT may modify RDT at
1868 * any moment.
1869 */
1870 uint32_t rdt = RDT;
1871 return (RDH > rdt ? RDLEN/sizeof(E1KRXDESC) : 0) + rdt - RDH;
1872}
1873
1874DECLINLINE(unsigned) e1kRxDInCache(PE1KSTATE pThis)
1875{
1876 return pThis->nRxDFetched > pThis->iRxDCurrent ?
1877 pThis->nRxDFetched - pThis->iRxDCurrent : 0;
1878}
1879
1880DECLINLINE(unsigned) e1kRxDIsCacheEmpty(PE1KSTATE pThis)
1881{
1882 return pThis->iRxDCurrent >= pThis->nRxDFetched;
1883}
1884
1885/**
1886 * Load receive descriptors from guest memory. The caller needs to be in Rx
1887 * critical section.
1888 *
1889 * We need two physical reads in case the tail wrapped around the end of RX
1890 * descriptor ring.
1891 *
1892 * @returns the actual number of descriptors fetched.
1893 * @param pDevIns The device instance.
1894 * @param pThis The device state structure.
1895 * @thread EMT, RX
1896 */
1897DECLINLINE(unsigned) e1kRxDPrefetch(PPDMDEVINS pDevIns, PE1KSTATE pThis)
1898{
1899 /* We've already loaded pThis->nRxDFetched descriptors past RDH. */
1900 unsigned nDescsAvailable = e1kGetRxLen(pThis) - e1kRxDInCache(pThis);
1901 unsigned nDescsToFetch = RT_MIN(nDescsAvailable, E1K_RXD_CACHE_SIZE - pThis->nRxDFetched);
1902 unsigned nDescsTotal = RDLEN / sizeof(E1KRXDESC);
1903 Assert(nDescsTotal != 0);
1904 if (nDescsTotal == 0)
1905 return 0;
1906 unsigned nFirstNotLoaded = (RDH + e1kRxDInCache(pThis)) % nDescsTotal;
1907 unsigned nDescsInSingleRead = RT_MIN(nDescsToFetch, nDescsTotal - nFirstNotLoaded);
1908 E1kLog3(("%s e1kRxDPrefetch: nDescsAvailable=%u nDescsToFetch=%u "
1909 "nDescsTotal=%u nFirstNotLoaded=0x%x nDescsInSingleRead=%u\n",
1910 pThis->szPrf, nDescsAvailable, nDescsToFetch, nDescsTotal,
1911 nFirstNotLoaded, nDescsInSingleRead));
1912 if (nDescsToFetch == 0)
1913 return 0;
1914 E1KRXDESC* pFirstEmptyDesc = &pThis->aRxDescriptors[pThis->nRxDFetched];
1915 PDMDevHlpPCIPhysRead(pDevIns,
1916 ((uint64_t)RDBAH << 32) + RDBAL + nFirstNotLoaded * sizeof(E1KRXDESC),
1917 pFirstEmptyDesc, nDescsInSingleRead * sizeof(E1KRXDESC));
1918 // uint64_t addrBase = ((uint64_t)RDBAH << 32) + RDBAL;
1919 // unsigned i, j;
1920 // for (i = pThis->nRxDFetched; i < pThis->nRxDFetched + nDescsInSingleRead; ++i)
1921 // {
1922 // pThis->aRxDescAddr[i] = addrBase + (nFirstNotLoaded + i - pThis->nRxDFetched) * sizeof(E1KRXDESC);
1923 // E1kLog3(("%s aRxDescAddr[%d] = %p\n", pThis->szPrf, i, pThis->aRxDescAddr[i]));
1924 // }
1925 E1kLog3(("%s Fetched %u RX descriptors at %08x%08x(0x%x), RDLEN=%08x, RDH=%08x, RDT=%08x\n",
1926 pThis->szPrf, nDescsInSingleRead,
1927 RDBAH, RDBAL + RDH * sizeof(E1KRXDESC),
1928 nFirstNotLoaded, RDLEN, RDH, RDT));
1929 if (nDescsToFetch > nDescsInSingleRead)
1930 {
1931 PDMDevHlpPCIPhysRead(pDevIns,
1932 ((uint64_t)RDBAH << 32) + RDBAL,
1933 pFirstEmptyDesc + nDescsInSingleRead,
1934 (nDescsToFetch - nDescsInSingleRead) * sizeof(E1KRXDESC));
1935 // Assert(i == pThis->nRxDFetched + nDescsInSingleRead);
1936 // for (j = 0; i < pThis->nRxDFetched + nDescsToFetch; ++i, ++j)
1937 // {
1938 // pThis->aRxDescAddr[i] = addrBase + j * sizeof(E1KRXDESC);
1939 // E1kLog3(("%s aRxDescAddr[%d] = %p\n", pThis->szPrf, i, pThis->aRxDescAddr[i]));
1940 // }
1941 E1kLog3(("%s Fetched %u RX descriptors at %08x%08x\n",
1942 pThis->szPrf, nDescsToFetch - nDescsInSingleRead,
1943 RDBAH, RDBAL));
1944 }
1945 pThis->nRxDFetched += nDescsToFetch;
1946 return nDescsToFetch;
1947}
1948
1949# ifdef IN_RING3 /* currently only used in ring-3 due to stack space requirements of the caller */
1950/**
1951 * Dump receive descriptor to debug log.
1952 *
1953 * @param pThis The device state structure.
1954 * @param pDesc Pointer to the descriptor.
1955 * @thread E1000_RX
1956 */
1957static void e1kPrintRDesc(PE1KSTATE pThis, E1KRXDESC *pDesc)
1958{
1959 RT_NOREF2(pThis, pDesc);
1960 E1kLog2(("%s <-- Receive Descriptor (%d bytes):\n", pThis->szPrf, pDesc->u16Length));
1961 E1kLog2((" Address=%16LX Length=%04X Csum=%04X\n",
1962 pDesc->u64BufAddr, pDesc->u16Length, pDesc->u16Checksum));
1963 E1kLog2((" STA: %s %s %s %s %s %s %s ERR: %s %s %s %s SPECIAL: %s VLAN=%03x PRI=%x\n",
1964 pDesc->status.fPIF ? "PIF" : "pif",
1965 pDesc->status.fIPCS ? "IPCS" : "ipcs",
1966 pDesc->status.fTCPCS ? "TCPCS" : "tcpcs",
1967 pDesc->status.fVP ? "VP" : "vp",
1968 pDesc->status.fIXSM ? "IXSM" : "ixsm",
1969 pDesc->status.fEOP ? "EOP" : "eop",
1970 pDesc->status.fDD ? "DD" : "dd",
1971 pDesc->status.fRXE ? "RXE" : "rxe",
1972 pDesc->status.fIPE ? "IPE" : "ipe",
1973 pDesc->status.fTCPE ? "TCPE" : "tcpe",
1974 pDesc->status.fCE ? "CE" : "ce",
1975 E1K_SPEC_CFI(pDesc->status.u16Special) ? "CFI" :"cfi",
1976 E1K_SPEC_VLAN(pDesc->status.u16Special),
1977 E1K_SPEC_PRI(pDesc->status.u16Special)));
1978}
1979# endif /* IN_RING3 */
1980#endif /* E1K_WITH_RXD_CACHE */
1981
1982/**
1983 * Dump transmit descriptor to debug log.
1984 *
1985 * @param pThis The device state structure.
1986 * @param pDesc Pointer to descriptor union.
1987 * @param pszDir A string denoting direction of descriptor transfer
1988 * @thread E1000_TX
1989 */
1990static void e1kPrintTDesc(PE1KSTATE pThis, E1KTXDESC *pDesc, const char *pszDir,
1991 unsigned uLevel = RTLOGGRPFLAGS_LEVEL_2)
1992{
1993 RT_NOREF4(pThis, pDesc, pszDir, uLevel);
1994
1995 /*
1996 * Unfortunately we cannot use our format handler here, we want R0 logging
1997 * as well.
1998 */
1999 switch (e1kGetDescType(pDesc))
2000 {
2001 case E1K_DTYP_CONTEXT:
2002 E1kLogX(uLevel, ("%s %s Context Transmit Descriptor %s\n",
2003 pThis->szPrf, pszDir, pszDir));
2004 E1kLogX(uLevel, (" IPCSS=%02X IPCSO=%02X IPCSE=%04X TUCSS=%02X TUCSO=%02X TUCSE=%04X\n",
2005 pDesc->context.ip.u8CSS, pDesc->context.ip.u8CSO, pDesc->context.ip.u16CSE,
2006 pDesc->context.tu.u8CSS, pDesc->context.tu.u8CSO, pDesc->context.tu.u16CSE));
2007 E1kLogX(uLevel, (" TUCMD:%s%s%s %s %s PAYLEN=%04x HDRLEN=%04x MSS=%04x STA: %s\n",
2008 pDesc->context.dw2.fIDE ? " IDE":"",
2009 pDesc->context.dw2.fRS ? " RS" :"",
2010 pDesc->context.dw2.fTSE ? " TSE":"",
2011 pDesc->context.dw2.fIP ? "IPv4":"IPv6",
2012 pDesc->context.dw2.fTCP ? "TCP":"UDP",
2013 pDesc->context.dw2.u20PAYLEN,
2014 pDesc->context.dw3.u8HDRLEN,
2015 pDesc->context.dw3.u16MSS,
2016 pDesc->context.dw3.fDD?"DD":""));
2017 break;
2018 case E1K_DTYP_DATA:
2019 E1kLogX(uLevel, ("%s %s Data Transmit Descriptor (%d bytes) %s\n",
2020 pThis->szPrf, pszDir, pDesc->data.cmd.u20DTALEN, pszDir));
2021 E1kLogX(uLevel, (" Address=%16LX DTALEN=%05X\n",
2022 pDesc->data.u64BufAddr,
2023 pDesc->data.cmd.u20DTALEN));
2024 E1kLogX(uLevel, (" DCMD:%s%s%s%s%s%s%s STA:%s%s%s POPTS:%s%s SPECIAL:%s VLAN=%03x PRI=%x\n",
2025 pDesc->data.cmd.fIDE ? " IDE" :"",
2026 pDesc->data.cmd.fVLE ? " VLE" :"",
2027 pDesc->data.cmd.fRPS ? " RPS" :"",
2028 pDesc->data.cmd.fRS ? " RS" :"",
2029 pDesc->data.cmd.fTSE ? " TSE" :"",
2030 pDesc->data.cmd.fIFCS? " IFCS":"",
2031 pDesc->data.cmd.fEOP ? " EOP" :"",
2032 pDesc->data.dw3.fDD ? " DD" :"",
2033 pDesc->data.dw3.fEC ? " EC" :"",
2034 pDesc->data.dw3.fLC ? " LC" :"",
2035 pDesc->data.dw3.fTXSM? " TXSM":"",
2036 pDesc->data.dw3.fIXSM? " IXSM":"",
2037 E1K_SPEC_CFI(pDesc->data.dw3.u16Special) ? "CFI" :"cfi",
2038 E1K_SPEC_VLAN(pDesc->data.dw3.u16Special),
2039 E1K_SPEC_PRI(pDesc->data.dw3.u16Special)));
2040 break;
2041 case E1K_DTYP_LEGACY:
2042 E1kLogX(uLevel, ("%s %s Legacy Transmit Descriptor (%d bytes) %s\n",
2043 pThis->szPrf, pszDir, pDesc->legacy.cmd.u16Length, pszDir));
2044 E1kLogX(uLevel, (" Address=%16LX DTALEN=%05X\n",
2045 pDesc->data.u64BufAddr,
2046 pDesc->legacy.cmd.u16Length));
2047 E1kLogX(uLevel, (" CMD:%s%s%s%s%s%s%s STA:%s%s%s CSO=%02x CSS=%02x SPECIAL:%s VLAN=%03x PRI=%x\n",
2048 pDesc->legacy.cmd.fIDE ? " IDE" :"",
2049 pDesc->legacy.cmd.fVLE ? " VLE" :"",
2050 pDesc->legacy.cmd.fRPS ? " RPS" :"",
2051 pDesc->legacy.cmd.fRS ? " RS" :"",
2052 pDesc->legacy.cmd.fIC ? " IC" :"",
2053 pDesc->legacy.cmd.fIFCS? " IFCS":"",
2054 pDesc->legacy.cmd.fEOP ? " EOP" :"",
2055 pDesc->legacy.dw3.fDD ? " DD" :"",
2056 pDesc->legacy.dw3.fEC ? " EC" :"",
2057 pDesc->legacy.dw3.fLC ? " LC" :"",
2058 pDesc->legacy.cmd.u8CSO,
2059 pDesc->legacy.dw3.u8CSS,
2060 E1K_SPEC_CFI(pDesc->legacy.dw3.u16Special) ? "CFI" :"cfi",
2061 E1K_SPEC_VLAN(pDesc->legacy.dw3.u16Special),
2062 E1K_SPEC_PRI(pDesc->legacy.dw3.u16Special)));
2063 break;
2064 default:
2065 E1kLog(("%s %s Invalid Transmit Descriptor %s\n",
2066 pThis->szPrf, pszDir, pszDir));
2067 break;
2068 }
2069}
2070
2071/**
2072 * Raise an interrupt later.
2073 *
2074 * @param pThis The device state structure.
2075 */
2076DECLINLINE(void) e1kPostponeInterrupt(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint64_t nsDeadline)
2077{
2078 if (!PDMDevHlpTimerIsActive(pDevIns, pThis->hIntTimer))
2079 PDMDevHlpTimerSetNano(pDevIns, pThis->hIntTimer, nsDeadline);
2080}
2081
2082/**
2083 * Raise interrupt if not masked.
2084 *
2085 * @param pThis The device state structure.
2086 */
2087static int e1kRaiseInterrupt(PPDMDEVINS pDevIns, PE1KSTATE pThis, int rcBusy, uint32_t u32IntCause = 0)
2088{
2089 int rc = e1kCsEnter(pThis, rcBusy);
2090 if (RT_UNLIKELY(rc != VINF_SUCCESS))
2091 return rc;
2092
2093 E1K_INC_ISTAT_CNT(pThis->uStatIntTry);
2094 ICR |= u32IntCause;
2095 if (ICR & IMS)
2096 {
2097 if (pThis->fIntRaised)
2098 {
2099 E1K_INC_ISTAT_CNT(pThis->uStatIntSkip);
2100 E1kLog2(("%s e1kRaiseInterrupt: Already raised, skipped. ICR&IMS=%08x\n",
2101 pThis->szPrf, ICR & IMS));
2102 }
2103 else
2104 {
2105 uint64_t tsNow = PDMDevHlpTimerGet(pDevIns, pThis->hIntTimer);
2106 if (!!ITR && tsNow - pThis->u64AckedAt < ITR * 256
2107 && pThis->fItrEnabled && (pThis->fItrRxEnabled || !(ICR & ICR_RXT0)))
2108 {
2109 E1K_INC_ISTAT_CNT(pThis->uStatIntEarly);
2110 E1kLog2(("%s e1kRaiseInterrupt: Too early to raise again: %d ns < %d ns.\n",
2111 pThis->szPrf, (uint32_t)(tsNow - pThis->u64AckedAt), ITR * 256));
2112 e1kPostponeInterrupt(pDevIns, pThis, ITR * 256);
2113 }
2114 else
2115 {
2116
2117 /* Since we are delivering the interrupt now
2118 * there is no need to do it later -- stop the timer.
2119 */
2120 PDMDevHlpTimerStop(pDevIns, pThis->hIntTimer);
2121 E1K_INC_ISTAT_CNT(pThis->uStatInt);
2122 STAM_COUNTER_INC(&pThis->StatIntsRaised);
2123 /* Got at least one unmasked interrupt cause */
2124 pThis->fIntRaised = true;
2125 /* Raise(1) INTA(0) */
2126 E1kLogRel(("E1000: irq RAISED icr&mask=0x%x, icr=0x%x\n", ICR & IMS, ICR));
2127 PDMDevHlpPCISetIrq(pDevIns, 0, 1);
2128 E1kLog(("%s e1kRaiseInterrupt: Raised. ICR&IMS=%08x\n",
2129 pThis->szPrf, ICR & IMS));
2130 }
2131 }
2132 }
2133 else
2134 {
2135 E1K_INC_ISTAT_CNT(pThis->uStatIntMasked);
2136 E1kLog2(("%s e1kRaiseInterrupt: Not raising, ICR=%08x, IMS=%08x\n",
2137 pThis->szPrf, ICR, IMS));
2138 }
2139 e1kCsLeave(pThis);
2140 return VINF_SUCCESS;
2141}
2142
2143/**
2144 * Compute the physical address of the descriptor.
2145 *
2146 * @returns the physical address of the descriptor.
2147 *
2148 * @param baseHigh High-order 32 bits of descriptor table address.
2149 * @param baseLow Low-order 32 bits of descriptor table address.
2150 * @param idxDesc The descriptor index in the table.
2151 */
2152DECLINLINE(RTGCPHYS) e1kDescAddr(uint32_t baseHigh, uint32_t baseLow, uint32_t idxDesc)
2153{
2154 AssertCompile(sizeof(E1KRXDESC) == sizeof(E1KTXDESC));
2155 return ((uint64_t)baseHigh << 32) + baseLow + idxDesc * sizeof(E1KRXDESC);
2156}
2157
2158#ifdef IN_RING3 /* currently only used in ring-3 due to stack space requirements of the caller */
2159/**
2160 * Advance the head pointer of the receive descriptor queue.
2161 *
2162 * @remarks RDH always points to the next available RX descriptor.
2163 *
2164 * @param pDevIns The device instance.
2165 * @param pThis The device state structure.
2166 */
2167DECLINLINE(void) e1kAdvanceRDH(PPDMDEVINS pDevIns, PE1KSTATE pThis)
2168{
2169 Assert(e1kCsRxIsOwner(pThis));
2170 //e1kCsEnter(pThis, RT_SRC_POS);
2171 if (++RDH * sizeof(E1KRXDESC) >= RDLEN)
2172 RDH = 0;
2173#ifdef E1K_WITH_RXD_CACHE
2174 /*
2175 * We need to fetch descriptors now as the guest may advance RDT all the way
2176 * to RDH as soon as we generate RXDMT0 interrupt. This is mostly to provide
2177 * compatibility with Phar Lap ETS, see @bugref(7346). Note that we do not
2178 * check if the receiver is enabled. It must be, otherwise we won't get here
2179 * in the first place.
2180 *
2181 * Note that we should have moved both RDH and iRxDCurrent by now.
2182 */
2183 if (e1kRxDIsCacheEmpty(pThis))
2184 {
2185 /* Cache is empty, reset it and check if we can fetch more. */
2186 pThis->iRxDCurrent = pThis->nRxDFetched = 0;
2187 E1kLog3(("%s e1kAdvanceRDH: Rx cache is empty, RDH=%x RDT=%x "
2188 "iRxDCurrent=%x nRxDFetched=%x\n",
2189 pThis->szPrf, RDH, RDT, pThis->iRxDCurrent, pThis->nRxDFetched));
2190 e1kRxDPrefetch(pDevIns, pThis);
2191 }
2192#endif /* E1K_WITH_RXD_CACHE */
2193 /*
2194 * Compute current receive queue length and fire RXDMT0 interrupt
2195 * if we are low on receive buffers
2196 */
2197 uint32_t uRQueueLen = RDH>RDT ? RDLEN/sizeof(E1KRXDESC)-RDH+RDT : RDT-RDH;
2198 /*
2199 * The minimum threshold is controlled by RDMTS bits of RCTL:
2200 * 00 = 1/2 of RDLEN
2201 * 01 = 1/4 of RDLEN
2202 * 10 = 1/8 of RDLEN
2203 * 11 = reserved
2204 */
2205 uint32_t uMinRQThreshold = RDLEN / sizeof(E1KRXDESC) / (2 << GET_BITS(RCTL, RDMTS));
2206 if (uRQueueLen <= uMinRQThreshold)
2207 {
2208 E1kLogRel(("E1000: low on RX descriptors, RDH=%x RDT=%x len=%x threshold=%x\n", RDH, RDT, uRQueueLen, uMinRQThreshold));
2209 E1kLog2(("%s Low on RX descriptors, RDH=%x RDT=%x len=%x threshold=%x, raise an interrupt\n",
2210 pThis->szPrf, RDH, RDT, uRQueueLen, uMinRQThreshold));
2211 E1K_INC_ISTAT_CNT(pThis->uStatIntRXDMT0);
2212 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_RXDMT0);
2213 }
2214 E1kLog2(("%s e1kAdvanceRDH: at exit RDH=%x RDT=%x len=%x\n",
2215 pThis->szPrf, RDH, RDT, uRQueueLen));
2216 //e1kCsLeave(pThis);
2217}
2218#endif /* IN_RING3 */
2219
2220#ifdef E1K_WITH_RXD_CACHE
2221
2222# ifdef IN_RING3 /* currently only used in ring-3 due to stack space requirements of the caller */
2223
2224/**
2225 * Obtain the next RX descriptor from RXD cache, fetching descriptors from the
2226 * RX ring if the cache is empty.
2227 *
2228 * Note that we cannot advance the cache pointer (iRxDCurrent) yet as it will
2229 * go out of sync with RDH which will cause trouble when EMT checks if the
2230 * cache is empty to do pre-fetch @bugref(6217).
2231 *
2232 * @param pDevIns The device instance.
2233 * @param pThis The device state structure.
2234 * @thread RX
2235 */
2236DECLINLINE(E1KRXDESC *) e1kRxDGet(PPDMDEVINS pDevIns, PE1KSTATE pThis)
2237{
2238 Assert(e1kCsRxIsOwner(pThis));
2239 /* Check the cache first. */
2240 if (pThis->iRxDCurrent < pThis->nRxDFetched)
2241 return &pThis->aRxDescriptors[pThis->iRxDCurrent];
2242 /* Cache is empty, reset it and check if we can fetch more. */
2243 pThis->iRxDCurrent = pThis->nRxDFetched = 0;
2244 if (e1kRxDPrefetch(pDevIns, pThis))
2245 return &pThis->aRxDescriptors[pThis->iRxDCurrent];
2246 /* Out of Rx descriptors. */
2247 return NULL;
2248}
2249
2250
2251/**
2252 * Return the RX descriptor obtained with e1kRxDGet() and advance the cache
2253 * pointer. The descriptor gets written back to the RXD ring.
2254 *
2255 * @param pDevIns The device instance.
2256 * @param pThis The device state structure.
2257 * @param pDesc The descriptor being "returned" to the RX ring.
2258 * @thread RX
2259 */
2260DECLINLINE(void) e1kRxDPut(PPDMDEVINS pDevIns, PE1KSTATE pThis, E1KRXDESC* pDesc)
2261{
2262 Assert(e1kCsRxIsOwner(pThis));
2263 pThis->iRxDCurrent++;
2264 // Assert(pDesc >= pThis->aRxDescriptors);
2265 // Assert(pDesc < pThis->aRxDescriptors + E1K_RXD_CACHE_SIZE);
2266 // uint64_t addr = e1kDescAddr(RDBAH, RDBAL, RDH);
2267 // uint32_t rdh = RDH;
2268 // Assert(pThis->aRxDescAddr[pDesc - pThis->aRxDescriptors] == addr);
2269 PDMDevHlpPCIPhysWrite(pDevIns, e1kDescAddr(RDBAH, RDBAL, RDH), pDesc, sizeof(E1KRXDESC));
2270 /*
2271 * We need to print the descriptor before advancing RDH as it may fetch new
2272 * descriptors into the cache.
2273 */
2274 e1kPrintRDesc(pThis, pDesc);
2275 e1kAdvanceRDH(pDevIns, pThis);
2276}
2277
2278/**
2279 * Store a fragment of received packet at the specifed address.
2280 *
2281 * @param pDevIns The device instance.
2282 * @param pThis The device state structure.
2283 * @param pDesc The next available RX descriptor.
2284 * @param pvBuf The fragment.
2285 * @param cb The size of the fragment.
2286 */
2287static void e1kStoreRxFragment(PPDMDEVINS pDevIns, PE1KSTATE pThis, E1KRXDESC *pDesc, const void *pvBuf, size_t cb)
2288{
2289 STAM_PROFILE_ADV_START(&pThis->StatReceiveStore, a);
2290 E1kLog2(("%s e1kStoreRxFragment: store fragment of %04X at %016LX, EOP=%d\n",
2291 pThis->szPrf, cb, pDesc->u64BufAddr, pDesc->status.fEOP));
2292 PDMDevHlpPCIPhysWrite(pDevIns, pDesc->u64BufAddr, pvBuf, cb);
2293 pDesc->u16Length = (uint16_t)cb;
2294 Assert(pDesc->u16Length == cb);
2295 STAM_PROFILE_ADV_STOP(&pThis->StatReceiveStore, a);
2296 RT_NOREF(pThis);
2297}
2298
2299# endif /* IN_RING3 */
2300
2301#else /* !E1K_WITH_RXD_CACHE */
2302
2303/**
2304 * Store a fragment of received packet that fits into the next available RX
2305 * buffer.
2306 *
2307 * @remarks Trigger the RXT0 interrupt if it is the last fragment of the packet.
2308 *
2309 * @param pDevIns The device instance.
2310 * @param pThis The device state structure.
2311 * @param pDesc The next available RX descriptor.
2312 * @param pvBuf The fragment.
2313 * @param cb The size of the fragment.
2314 */
2315static void e1kStoreRxFragment(PPDMDEVINS pDevIns, PE1KSTATE pThis, E1KRXDESC *pDesc, const void *pvBuf, size_t cb)
2316{
2317 STAM_PROFILE_ADV_START(&pThis->StatReceiveStore, a);
2318 E1kLog2(("%s e1kStoreRxFragment: store fragment of %04X at %016LX, EOP=%d\n", pThis->szPrf, cb, pDesc->u64BufAddr, pDesc->status.fEOP));
2319 PDMDevHlpPCIPhysWrite(pDevIns, pDesc->u64BufAddr, pvBuf, cb);
2320 pDesc->u16Length = (uint16_t)cb; Assert(pDesc->u16Length == cb);
2321 /* Write back the descriptor */
2322 PDMDevHlpPCIPhysWrite(pDevIns, e1kDescAddr(RDBAH, RDBAL, RDH), pDesc, sizeof(E1KRXDESC));
2323 e1kPrintRDesc(pThis, pDesc);
2324 E1kLogRel(("E1000: Wrote back RX desc, RDH=%x\n", RDH));
2325 /* Advance head */
2326 e1kAdvanceRDH(pDevIns, pThis);
2327 //E1kLog2(("%s e1kStoreRxFragment: EOP=%d RDTR=%08X RADV=%08X\n", pThis->szPrf, pDesc->fEOP, RDTR, RADV));
2328 if (pDesc->status.fEOP)
2329 {
2330 /* Complete packet has been stored -- it is time to let the guest know. */
2331#ifdef E1K_USE_RX_TIMERS
2332 if (RDTR)
2333 {
2334 /* Arm the timer to fire in RDTR usec (discard .024) */
2335 e1kArmTimer(pDevIns, pThis, pThis->hRIDTimer, RDTR);
2336 /* If absolute timer delay is enabled and the timer is not running yet, arm it. */
2337 if (RADV != 0 && !PDMDevHlpTimerIsActive(pDevIns, pThis->CTX_SUFF(pRADTimer)))
2338 e1kArmTimer(pThis, pThis->hRADTimer, RADV);
2339 }
2340 else
2341 {
2342#endif
2343 /* 0 delay means immediate interrupt */
2344 E1K_INC_ISTAT_CNT(pThis->uStatIntRx);
2345 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_RXT0);
2346#ifdef E1K_USE_RX_TIMERS
2347 }
2348#endif
2349 }
2350 STAM_PROFILE_ADV_STOP(&pThis->StatReceiveStore, a);
2351}
2352
2353#endif /* !E1K_WITH_RXD_CACHE */
2354
2355/**
2356 * Returns true if it is a broadcast packet.
2357 *
2358 * @returns true if destination address indicates broadcast.
2359 * @param pvBuf The ethernet packet.
2360 */
2361DECLINLINE(bool) e1kIsBroadcast(const void *pvBuf)
2362{
2363 static const uint8_t s_abBcastAddr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
2364 return memcmp(pvBuf, s_abBcastAddr, sizeof(s_abBcastAddr)) == 0;
2365}
2366
2367/**
2368 * Returns true if it is a multicast packet.
2369 *
2370 * @remarks returns true for broadcast packets as well.
2371 * @returns true if destination address indicates multicast.
2372 * @param pvBuf The ethernet packet.
2373 */
2374DECLINLINE(bool) e1kIsMulticast(const void *pvBuf)
2375{
2376 return (*(char*)pvBuf) & 1;
2377}
2378
2379#ifdef IN_RING3 /* currently only used in ring-3 due to stack space requirements of the caller */
2380/**
2381 * Set IXSM, IPCS and TCPCS flags according to the packet type.
2382 *
2383 * @remarks We emulate checksum offloading for major packets types only.
2384 *
2385 * @returns VBox status code.
2386 * @param pThis The device state structure.
2387 * @param pFrame The available data.
2388 * @param cb Number of bytes available in the buffer.
2389 * @param status Bit fields containing status info.
2390 */
2391static int e1kRxChecksumOffload(PE1KSTATE pThis, const uint8_t *pFrame, size_t cb, E1KRXDST *pStatus)
2392{
2393 /** @todo
2394 * It is not safe to bypass checksum verification for packets coming
2395 * from real wire. We currently unable to tell where packets are
2396 * coming from so we tell the driver to ignore our checksum flags
2397 * and do verification in software.
2398 */
2399# if 0
2400 uint16_t uEtherType = ntohs(*(uint16_t*)(pFrame + 12));
2401
2402 E1kLog2(("%s e1kRxChecksumOffload: EtherType=%x\n", pThis->szPrf, uEtherType));
2403
2404 switch (uEtherType)
2405 {
2406 case 0x800: /* IPv4 */
2407 {
2408 pStatus->fIXSM = false;
2409 pStatus->fIPCS = true;
2410 PRTNETIPV4 pIpHdr4 = (PRTNETIPV4)(pFrame + 14);
2411 /* TCP/UDP checksum offloading works with TCP and UDP only */
2412 pStatus->fTCPCS = pIpHdr4->ip_p == 6 || pIpHdr4->ip_p == 17;
2413 break;
2414 }
2415 case 0x86DD: /* IPv6 */
2416 pStatus->fIXSM = false;
2417 pStatus->fIPCS = false;
2418 pStatus->fTCPCS = true;
2419 break;
2420 default: /* ARP, VLAN, etc. */
2421 pStatus->fIXSM = true;
2422 break;
2423 }
2424# else
2425 pStatus->fIXSM = true;
2426 RT_NOREF_PV(pThis); RT_NOREF_PV(pFrame); RT_NOREF_PV(cb);
2427# endif
2428 return VINF_SUCCESS;
2429}
2430#endif /* IN_RING3 */
2431
2432/**
2433 * Pad and store received packet.
2434 *
2435 * @remarks Make sure that the packet appears to upper layer as one coming
2436 * from real Ethernet: pad it and insert FCS.
2437 *
2438 * @returns VBox status code.
2439 * @param pDevIns The device instance.
2440 * @param pThis The device state structure.
2441 * @param pvBuf The available data.
2442 * @param cb Number of bytes available in the buffer.
2443 * @param status Bit fields containing status info.
2444 */
2445static int e1kHandleRxPacket(PPDMDEVINS pDevIns, PE1KSTATE pThis, const void *pvBuf, size_t cb, E1KRXDST status)
2446{
2447#if defined(IN_RING3) /** @todo Remove this extra copying, it's gonna make us run out of kernel / hypervisor stack! */
2448 uint8_t rxPacket[E1K_MAX_RX_PKT_SIZE];
2449 uint8_t *ptr = rxPacket;
2450
2451 int rc = e1kCsRxEnter(pThis, VERR_SEM_BUSY);
2452 if (RT_UNLIKELY(rc != VINF_SUCCESS))
2453 return rc;
2454
2455 if (cb > 70) /* unqualified guess */
2456 pThis->led.Asserted.s.fReading = pThis->led.Actual.s.fReading = 1;
2457
2458 Assert(cb <= E1K_MAX_RX_PKT_SIZE);
2459 Assert(cb > 16);
2460 size_t cbMax = ((RCTL & RCTL_LPE) ? E1K_MAX_RX_PKT_SIZE - 4 : 1518) - (status.fVP ? 0 : 4);
2461 E1kLog3(("%s Max RX packet size is %u\n", pThis->szPrf, cbMax));
2462 if (status.fVP)
2463 {
2464 /* VLAN packet -- strip VLAN tag in VLAN mode */
2465 if ((CTRL & CTRL_VME) && cb > 16)
2466 {
2467 uint16_t *u16Ptr = (uint16_t*)pvBuf;
2468 memcpy(rxPacket, pvBuf, 12); /* Copy src and dst addresses */
2469 status.u16Special = RT_BE2H_U16(u16Ptr[7]); /* Extract VLAN tag */
2470 memcpy(rxPacket + 12, (uint8_t*)pvBuf + 16, cb - 16); /* Copy the rest of the packet */
2471 cb -= 4;
2472 E1kLog3(("%s Stripped tag for VLAN %u (cb=%u)\n",
2473 pThis->szPrf, status.u16Special, cb));
2474 }
2475 else
2476 {
2477 status.fVP = false; /* Set VP only if we stripped the tag */
2478 memcpy(rxPacket, pvBuf, cb);
2479 }
2480 }
2481 else
2482 memcpy(rxPacket, pvBuf, cb);
2483 /* Pad short packets */
2484 if (cb < 60)
2485 {
2486 memset(rxPacket + cb, 0, 60 - cb);
2487 cb = 60;
2488 }
2489 if (!(RCTL & RCTL_SECRC) && cb <= cbMax)
2490 {
2491 STAM_PROFILE_ADV_START(&pThis->StatReceiveCRC, a);
2492 /*
2493 * Add FCS if CRC stripping is not enabled. Since the value of CRC
2494 * is ignored by most of drivers we may as well save us the trouble
2495 * of calculating it (see EthernetCRC CFGM parameter).
2496 */
2497 if (pThis->fEthernetCRC)
2498 *(uint32_t*)(rxPacket + cb) = RTCrc32(rxPacket, cb);
2499 cb += sizeof(uint32_t);
2500 STAM_PROFILE_ADV_STOP(&pThis->StatReceiveCRC, a);
2501 E1kLog3(("%s Added FCS (cb=%u)\n", pThis->szPrf, cb));
2502 }
2503 /* Compute checksum of complete packet */
2504 size_t cbCSumStart = RT_MIN(GET_BITS(RXCSUM, PCSS), cb);
2505 uint16_t checksum = e1kCSum16(rxPacket + cbCSumStart, cb - cbCSumStart);
2506 e1kRxChecksumOffload(pThis, rxPacket, cb, &status);
2507
2508 /* Update stats */
2509 E1K_INC_CNT32(GPRC);
2510 if (e1kIsBroadcast(pvBuf))
2511 E1K_INC_CNT32(BPRC);
2512 else if (e1kIsMulticast(pvBuf))
2513 E1K_INC_CNT32(MPRC);
2514 /* Update octet receive counter */
2515 E1K_ADD_CNT64(GORCL, GORCH, cb);
2516 STAM_REL_COUNTER_ADD(&pThis->StatReceiveBytes, cb);
2517 if (cb == 64)
2518 E1K_INC_CNT32(PRC64);
2519 else if (cb < 128)
2520 E1K_INC_CNT32(PRC127);
2521 else if (cb < 256)
2522 E1K_INC_CNT32(PRC255);
2523 else if (cb < 512)
2524 E1K_INC_CNT32(PRC511);
2525 else if (cb < 1024)
2526 E1K_INC_CNT32(PRC1023);
2527 else
2528 E1K_INC_CNT32(PRC1522);
2529
2530 E1K_INC_ISTAT_CNT(pThis->uStatRxFrm);
2531
2532# ifdef E1K_WITH_RXD_CACHE
2533 while (cb > 0)
2534 {
2535 E1KRXDESC *pDesc = e1kRxDGet(pDevIns, pThis);
2536
2537 if (pDesc == NULL)
2538 {
2539 E1kLog(("%s Out of receive buffers, dropping the packet "
2540 "(cb=%u, in_cache=%u, RDH=%x RDT=%x)\n",
2541 pThis->szPrf, cb, e1kRxDInCache(pThis), RDH, RDT));
2542 break;
2543 }
2544# else /* !E1K_WITH_RXD_CACHE */
2545 if (RDH == RDT)
2546 {
2547 E1kLog(("%s Out of receive buffers, dropping the packet\n",
2548 pThis->szPrf));
2549 }
2550 /* Store the packet to receive buffers */
2551 while (RDH != RDT)
2552 {
2553 /* Load the descriptor pointed by head */
2554 E1KRXDESC desc, *pDesc = &desc;
2555 PDMDevHlpPCIPhysRead(pDevIns, e1kDescAddr(RDBAH, RDBAL, RDH), &desc, sizeof(desc));
2556# endif /* !E1K_WITH_RXD_CACHE */
2557 if (pDesc->u64BufAddr)
2558 {
2559 uint16_t u16RxBufferSize = pThis->u16RxBSize; /* see @bugref{9427} */
2560
2561 /* Update descriptor */
2562 pDesc->status = status;
2563 pDesc->u16Checksum = checksum;
2564 pDesc->status.fDD = true;
2565
2566 /*
2567 * We need to leave Rx critical section here or we risk deadlocking
2568 * with EMT in e1kRegWriteRDT when the write is to an unallocated
2569 * page or has an access handler associated with it.
2570 * Note that it is safe to leave the critical section here since
2571 * e1kRegWriteRDT() never modifies RDH. It never touches already
2572 * fetched RxD cache entries either.
2573 */
2574 if (cb > u16RxBufferSize)
2575 {
2576 pDesc->status.fEOP = false;
2577 e1kCsRxLeave(pThis);
2578 e1kStoreRxFragment(pDevIns, pThis, pDesc, ptr, u16RxBufferSize);
2579 rc = e1kCsRxEnter(pThis, VERR_SEM_BUSY);
2580 if (RT_UNLIKELY(rc != VINF_SUCCESS))
2581 return rc;
2582 ptr += u16RxBufferSize;
2583 cb -= u16RxBufferSize;
2584 }
2585 else
2586 {
2587 pDesc->status.fEOP = true;
2588 e1kCsRxLeave(pThis);
2589 e1kStoreRxFragment(pDevIns, pThis, pDesc, ptr, cb);
2590# ifdef E1K_WITH_RXD_CACHE
2591 rc = e1kCsRxEnter(pThis, VERR_SEM_BUSY);
2592 if (RT_UNLIKELY(rc != VINF_SUCCESS))
2593 return rc;
2594 cb = 0;
2595# else /* !E1K_WITH_RXD_CACHE */
2596 pThis->led.Actual.s.fReading = 0;
2597 return VINF_SUCCESS;
2598# endif /* !E1K_WITH_RXD_CACHE */
2599 }
2600 /*
2601 * Note: RDH is advanced by e1kStoreRxFragment if E1K_WITH_RXD_CACHE
2602 * is not defined.
2603 */
2604 }
2605# ifdef E1K_WITH_RXD_CACHE
2606 /* Write back the descriptor. */
2607 pDesc->status.fDD = true;
2608 e1kRxDPut(pDevIns, pThis, pDesc);
2609# else /* !E1K_WITH_RXD_CACHE */
2610 else
2611 {
2612 /* Write back the descriptor. */
2613 pDesc->status.fDD = true;
2614 PDMDevHlpPCIPhysWrite(pDevIns, e1kDescAddr(RDBAH, RDBAL, RDH), pDesc, sizeof(E1KRXDESC));
2615 e1kAdvanceRDH(pDevIns, pThis);
2616 }
2617# endif /* !E1K_WITH_RXD_CACHE */
2618 }
2619
2620 if (cb > 0)
2621 E1kLog(("%s Out of receive buffers, dropping %u bytes", pThis->szPrf, cb));
2622
2623 pThis->led.Actual.s.fReading = 0;
2624
2625 e1kCsRxLeave(pThis);
2626# ifdef E1K_WITH_RXD_CACHE
2627 /* Complete packet has been stored -- it is time to let the guest know. */
2628# ifdef E1K_USE_RX_TIMERS
2629 if (RDTR)
2630 {
2631 /* Arm the timer to fire in RDTR usec (discard .024) */
2632 e1kArmTimer(pThis, pThis->hRIDTimer, RDTR);
2633 /* If absolute timer delay is enabled and the timer is not running yet, arm it. */
2634 if (RADV != 0 && !PDMDevHlpTimerIsActive(pDevIns, pThis->hRADTimer))
2635 e1kArmTimer(pThis, pThis->hRADTimer, RADV);
2636 }
2637 else
2638 {
2639# endif /* E1K_USE_RX_TIMERS */
2640 /* 0 delay means immediate interrupt */
2641 E1K_INC_ISTAT_CNT(pThis->uStatIntRx);
2642 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_RXT0);
2643# ifdef E1K_USE_RX_TIMERS
2644 }
2645# endif /* E1K_USE_RX_TIMERS */
2646# endif /* E1K_WITH_RXD_CACHE */
2647
2648 return VINF_SUCCESS;
2649#else /* !IN_RING3 */
2650 RT_NOREF(pDevIns, pThis, pvBuf, cb, status);
2651 return VERR_INTERNAL_ERROR_2;
2652#endif /* !IN_RING3 */
2653}
2654
2655
2656#ifdef IN_RING3
2657/**
2658 * Bring the link up after the configured delay, 5 seconds by default.
2659 *
2660 * @param pDevIns The device instance.
2661 * @param pThis The device state structure.
2662 * @thread any
2663 */
2664DECLINLINE(void) e1kBringLinkUpDelayed(PPDMDEVINS pDevIns, PE1KSTATE pThis)
2665{
2666 E1kLog(("%s Will bring up the link in %d seconds...\n",
2667 pThis->szPrf, pThis->cMsLinkUpDelay / 1000));
2668 e1kArmTimer(pDevIns, pThis, pThis->hLUTimer, pThis->cMsLinkUpDelay * 1000);
2669}
2670
2671/**
2672 * Bring up the link immediately.
2673 *
2674 * @param pDevIns The device instance.
2675 * @param pThis The device state structure.
2676 * @param pThisCC The current context instance data.
2677 */
2678DECLINLINE(void) e1kR3LinkUp(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC)
2679{
2680 E1kLog(("%s Link is up\n", pThis->szPrf));
2681 STATUS |= STATUS_LU;
2682 Phy::setLinkStatus(&pThis->phy, true);
2683 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_LSC);
2684 if (pThisCC->pDrvR3)
2685 pThisCC->pDrvR3->pfnNotifyLinkChanged(pThisCC->pDrvR3, PDMNETWORKLINKSTATE_UP);
2686 /* Trigger processing of pending TX descriptors (see @bugref{8942}). */
2687 PDMDevHlpTaskTrigger(pDevIns, pThis->hTxTask);
2688}
2689
2690/**
2691 * Bring down the link immediately.
2692 *
2693 * @param pDevIns The device instance.
2694 * @param pThis The device state structure.
2695 * @param pThisCC The current context instance data.
2696 */
2697DECLINLINE(void) e1kR3LinkDown(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC)
2698{
2699 E1kLog(("%s Link is down\n", pThis->szPrf));
2700 STATUS &= ~STATUS_LU;
2701#ifdef E1K_LSC_ON_RESET
2702 Phy::setLinkStatus(&pThis->phy, false);
2703#endif /* E1K_LSC_ON_RESET */
2704 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_LSC);
2705 if (pThisCC->pDrvR3)
2706 pThisCC->pDrvR3->pfnNotifyLinkChanged(pThisCC->pDrvR3, PDMNETWORKLINKSTATE_DOWN);
2707}
2708
2709/**
2710 * Bring down the link temporarily.
2711 *
2712 * @param pDevIns The device instance.
2713 * @param pThis The device state structure.
2714 * @param pThisCC The current context instance data.
2715 */
2716DECLINLINE(void) e1kR3LinkDownTemp(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC)
2717{
2718 E1kLog(("%s Link is down temporarily\n", pThis->szPrf));
2719 STATUS &= ~STATUS_LU;
2720 Phy::setLinkStatus(&pThis->phy, false);
2721 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_LSC);
2722 /*
2723 * Notifying the associated driver that the link went down (even temporarily)
2724 * seems to be the right thing, but it was not done before. This may cause
2725 * a regression if the driver does not expect the link to go down as a result
2726 * of sending PDMNETWORKLINKSTATE_DOWN_RESUME to this device. Earlier versions
2727 * of code notified the driver that the link was up! See @bugref{7057}.
2728 */
2729 if (pThisCC->pDrvR3)
2730 pThisCC->pDrvR3->pfnNotifyLinkChanged(pThisCC->pDrvR3, PDMNETWORKLINKSTATE_DOWN);
2731 e1kBringLinkUpDelayed(pDevIns, pThis);
2732}
2733#endif /* IN_RING3 */
2734
2735#if 0 /* unused */
2736/**
2737 * Read handler for Device Status register.
2738 *
2739 * Get the link status from PHY.
2740 *
2741 * @returns VBox status code.
2742 *
2743 * @param pThis The device state structure.
2744 * @param offset Register offset in memory-mapped frame.
2745 * @param index Register index in register array.
2746 * @param mask Used to implement partial reads (8 and 16-bit).
2747 */
2748static int e1kRegReadCTRL(PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
2749{
2750 E1kLog(("%s e1kRegReadCTRL: mdio dir=%s mdc dir=%s mdc=%d\n",
2751 pThis->szPrf, (CTRL & CTRL_MDIO_DIR)?"OUT":"IN ",
2752 (CTRL & CTRL_MDC_DIR)?"OUT":"IN ", !!(CTRL & CTRL_MDC)));
2753 if ((CTRL & CTRL_MDIO_DIR) == 0 && (CTRL & CTRL_MDC))
2754 {
2755 /* MDC is high and MDIO pin is used for input, read MDIO pin from PHY */
2756 if (Phy::readMDIO(&pThis->phy))
2757 *pu32Value = CTRL | CTRL_MDIO;
2758 else
2759 *pu32Value = CTRL & ~CTRL_MDIO;
2760 E1kLog(("%s e1kRegReadCTRL: Phy::readMDIO(%d)\n",
2761 pThis->szPrf, !!(*pu32Value & CTRL_MDIO)));
2762 }
2763 else
2764 {
2765 /* MDIO pin is used for output, ignore it */
2766 *pu32Value = CTRL;
2767 }
2768 return VINF_SUCCESS;
2769}
2770#endif /* unused */
2771
2772/**
2773 * A callback used by PHY to indicate that the link needs to be updated due to
2774 * reset of PHY.
2775 *
2776 * @param pDevIns The device instance.
2777 * @thread any
2778 */
2779void e1kPhyLinkResetCallback(PPDMDEVINS pDevIns)
2780{
2781 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
2782
2783 /* Make sure we have cable connected and MAC can talk to PHY */
2784 if (pThis->fCableConnected && (CTRL & CTRL_SLU))
2785 e1kArmTimer(pDevIns, pThis, pThis->hLUTimer, E1K_INIT_LINKUP_DELAY_US);
2786}
2787
2788/**
2789 * Write handler for Device Control register.
2790 *
2791 * Handles reset.
2792 *
2793 * @param pThis The device state structure.
2794 * @param offset Register offset in memory-mapped frame.
2795 * @param index Register index in register array.
2796 * @param value The value to store.
2797 * @param mask Used to implement partial writes (8 and 16-bit).
2798 * @thread EMT
2799 */
2800static int e1kRegWriteCTRL(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
2801{
2802 int rc = VINF_SUCCESS;
2803
2804 if (value & CTRL_RESET)
2805 { /* RST */
2806#ifndef IN_RING3
2807 return VINF_IOM_R3_MMIO_WRITE;
2808#else
2809 e1kR3HardReset(pDevIns, pThis, PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC));
2810#endif
2811 }
2812 else
2813 {
2814#ifdef E1K_LSC_ON_SLU
2815 /*
2816 * When the guest changes 'Set Link Up' bit from 0 to 1 we check if
2817 * the link is down and the cable is connected, and if they are we
2818 * bring the link up, see @bugref{8624}.
2819 */
2820 if ( (value & CTRL_SLU)
2821 && !(CTRL & CTRL_SLU)
2822 && pThis->fCableConnected
2823 && !(STATUS & STATUS_LU))
2824 {
2825 /* It should take about 2 seconds for the link to come up */
2826 e1kArmTimer(pDevIns, pThis, pThis->hLUTimer, E1K_INIT_LINKUP_DELAY_US);
2827 }
2828#else /* !E1K_LSC_ON_SLU */
2829 if ( (value & CTRL_SLU)
2830 && !(CTRL & CTRL_SLU)
2831 && pThis->fCableConnected
2832 && !PDMDevHlpTimerIsActive(pDevIns, pThis->hLUTimer))
2833 {
2834 /* PXE does not use LSC interrupts, see @bugref{9113}. */
2835 STATUS |= STATUS_LU;
2836 }
2837#endif /* !E1K_LSC_ON_SLU */
2838 if ((value & CTRL_VME) != (CTRL & CTRL_VME))
2839 {
2840 E1kLog(("%s VLAN Mode %s\n", pThis->szPrf, (value & CTRL_VME) ? "Enabled" : "Disabled"));
2841 }
2842 Log7(("%s e1kRegWriteCTRL: mdio dir=%s mdc dir=%s mdc=%s mdio=%d\n",
2843 pThis->szPrf, (value & CTRL_MDIO_DIR)?"OUT":"IN ",
2844 (value & CTRL_MDC_DIR)?"OUT":"IN ", (value & CTRL_MDC)?"HIGH":"LOW ", !!(value & CTRL_MDIO)));
2845 if (value & CTRL_MDC)
2846 {
2847 if (value & CTRL_MDIO_DIR)
2848 {
2849 Log7(("%s e1kRegWriteCTRL: Phy::writeMDIO(%d)\n", pThis->szPrf, !!(value & CTRL_MDIO)));
2850 /* MDIO direction pin is set to output and MDC is high, write MDIO pin value to PHY */
2851 Phy::writeMDIO(&pThis->phy, !!(value & CTRL_MDIO), pDevIns);
2852 }
2853 else
2854 {
2855 if (Phy::readMDIO(&pThis->phy))
2856 value |= CTRL_MDIO;
2857 else
2858 value &= ~CTRL_MDIO;
2859 Log7(("%s e1kRegWriteCTRL: Phy::readMDIO(%d)\n", pThis->szPrf, !!(value & CTRL_MDIO)));
2860 }
2861 }
2862 rc = e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
2863 }
2864
2865 return rc;
2866}
2867
2868/**
2869 * Write handler for EEPROM/Flash Control/Data register.
2870 *
2871 * Handles EEPROM access requests; forwards writes to EEPROM device if access has been granted.
2872 *
2873 * @param pThis The device state structure.
2874 * @param offset Register offset in memory-mapped frame.
2875 * @param index Register index in register array.
2876 * @param value The value to store.
2877 * @param mask Used to implement partial writes (8 and 16-bit).
2878 * @thread EMT
2879 */
2880static int e1kRegWriteEECD(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
2881{
2882 RT_NOREF(pDevIns, offset, index);
2883#ifdef IN_RING3
2884 /* So far we are concerned with lower byte only */
2885 if ((EECD & EECD_EE_GNT) || pThis->eChip == E1K_CHIP_82543GC)
2886 {
2887 /* Access to EEPROM granted -- forward 4-wire bits to EEPROM device */
2888 /* Note: 82543GC does not need to request EEPROM access */
2889 STAM_PROFILE_ADV_START(&pThis->StatEEPROMWrite, a);
2890 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
2891 pThisCC->eeprom.write(value & EECD_EE_WIRES);
2892 STAM_PROFILE_ADV_STOP(&pThis->StatEEPROMWrite, a);
2893 }
2894 if (value & EECD_EE_REQ)
2895 EECD |= EECD_EE_REQ|EECD_EE_GNT;
2896 else
2897 EECD &= ~EECD_EE_GNT;
2898 //e1kRegWriteDefault(pThis, offset, index, value );
2899
2900 return VINF_SUCCESS;
2901#else /* !IN_RING3 */
2902 RT_NOREF(pThis, value);
2903 return VINF_IOM_R3_MMIO_WRITE;
2904#endif /* !IN_RING3 */
2905}
2906
2907/**
2908 * Read handler for EEPROM/Flash Control/Data register.
2909 *
2910 * Lower 4 bits come from EEPROM device if EEPROM access has been granted.
2911 *
2912 * @returns VBox status code.
2913 *
2914 * @param pThis The device state structure.
2915 * @param offset Register offset in memory-mapped frame.
2916 * @param index Register index in register array.
2917 * @param mask Used to implement partial reads (8 and 16-bit).
2918 * @thread EMT
2919 */
2920static int e1kRegReadEECD(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
2921{
2922#ifdef IN_RING3
2923 uint32_t value = 0; /* Get rid of false positive in parfait. */
2924 int rc = e1kRegReadDefault(pDevIns, pThis, offset, index, &value);
2925 if (RT_SUCCESS(rc))
2926 {
2927 if ((value & EECD_EE_GNT) || pThis->eChip == E1K_CHIP_82543GC)
2928 {
2929 /* Note: 82543GC does not need to request EEPROM access */
2930 /* Access to EEPROM granted -- get 4-wire bits to EEPROM device */
2931 STAM_PROFILE_ADV_START(&pThis->StatEEPROMRead, a);
2932 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
2933 value |= pThisCC->eeprom.read();
2934 STAM_PROFILE_ADV_STOP(&pThis->StatEEPROMRead, a);
2935 }
2936 *pu32Value = value;
2937 }
2938
2939 return rc;
2940#else /* !IN_RING3 */
2941 RT_NOREF_PV(pDevIns); RT_NOREF_PV(pThis); RT_NOREF_PV(offset); RT_NOREF_PV(index); RT_NOREF_PV(pu32Value);
2942 return VINF_IOM_R3_MMIO_READ;
2943#endif /* !IN_RING3 */
2944}
2945
2946/**
2947 * Write handler for EEPROM Read register.
2948 *
2949 * Handles EEPROM word access requests, reads EEPROM and stores the result
2950 * into DATA field.
2951 *
2952 * @param pThis The device state structure.
2953 * @param offset Register offset in memory-mapped frame.
2954 * @param index Register index in register array.
2955 * @param value The value to store.
2956 * @param mask Used to implement partial writes (8 and 16-bit).
2957 * @thread EMT
2958 */
2959static int e1kRegWriteEERD(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
2960{
2961#ifdef IN_RING3
2962 /* Make use of 'writable' and 'readable' masks. */
2963 e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
2964 /* DONE and DATA are set only if read was triggered by START. */
2965 if (value & EERD_START)
2966 {
2967 STAM_PROFILE_ADV_START(&pThis->StatEEPROMRead, a);
2968 uint16_t tmp;
2969 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
2970 if (pThisCC->eeprom.readWord(GET_BITS_V(value, EERD, ADDR), &tmp))
2971 SET_BITS(EERD, DATA, tmp);
2972 EERD |= EERD_DONE;
2973 STAM_PROFILE_ADV_STOP(&pThis->StatEEPROMRead, a);
2974 }
2975
2976 return VINF_SUCCESS;
2977#else /* !IN_RING3 */
2978 RT_NOREF_PV(pDevIns); RT_NOREF_PV(pThis); RT_NOREF_PV(offset); RT_NOREF_PV(index); RT_NOREF_PV(value);
2979 return VINF_IOM_R3_MMIO_WRITE;
2980#endif /* !IN_RING3 */
2981}
2982
2983
2984/**
2985 * Write handler for MDI Control register.
2986 *
2987 * Handles PHY read/write requests; forwards requests to internal PHY device.
2988 *
2989 * @param pThis The device state structure.
2990 * @param offset Register offset in memory-mapped frame.
2991 * @param index Register index in register array.
2992 * @param value The value to store.
2993 * @param mask Used to implement partial writes (8 and 16-bit).
2994 * @thread EMT
2995 */
2996static int e1kRegWriteMDIC(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
2997{
2998 if (value & MDIC_INT_EN)
2999 {
3000 E1kLog(("%s ERROR! Interrupt at the end of an MDI cycle is not supported yet.\n",
3001 pThis->szPrf));
3002 }
3003 else if (value & MDIC_READY)
3004 {
3005 E1kLog(("%s ERROR! Ready bit is not reset by software during write operation.\n",
3006 pThis->szPrf));
3007 }
3008 else if (GET_BITS_V(value, MDIC, PHY) != 1)
3009 {
3010 E1kLog(("%s WARNING! Access to invalid PHY detected, phy=%d.\n",
3011 pThis->szPrf, GET_BITS_V(value, MDIC, PHY)));
3012 /*
3013 * Some drivers scan the MDIO bus for a PHY. We can work with these
3014 * drivers if we set MDIC_READY and MDIC_ERROR when there isn't a PHY
3015 * at the requested address, see @bugref{7346}.
3016 */
3017 MDIC = MDIC_READY | MDIC_ERROR;
3018 }
3019 else
3020 {
3021 /* Store the value */
3022 e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
3023 STAM_COUNTER_INC(&pThis->StatPHYAccesses);
3024 /* Forward op to PHY */
3025 if (value & MDIC_OP_READ)
3026 SET_BITS(MDIC, DATA, Phy::readRegister(&pThis->phy, GET_BITS_V(value, MDIC, REG), pDevIns));
3027 else
3028 Phy::writeRegister(&pThis->phy, GET_BITS_V(value, MDIC, REG), value & MDIC_DATA_MASK, pDevIns);
3029 /* Let software know that we are done */
3030 MDIC |= MDIC_READY;
3031 }
3032
3033 return VINF_SUCCESS;
3034}
3035
3036/**
3037 * Write handler for Interrupt Cause Read register.
3038 *
3039 * Bits corresponding to 1s in 'value' will be cleared in ICR register.
3040 *
3041 * @param pThis The device state structure.
3042 * @param offset Register offset in memory-mapped frame.
3043 * @param index Register index in register array.
3044 * @param value The value to store.
3045 * @param mask Used to implement partial writes (8 and 16-bit).
3046 * @thread EMT
3047 */
3048static int e1kRegWriteICR(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3049{
3050 ICR &= ~value;
3051
3052 RT_NOREF_PV(pDevIns); RT_NOREF_PV(pThis); RT_NOREF_PV(offset); RT_NOREF_PV(index);
3053 return VINF_SUCCESS;
3054}
3055
3056/**
3057 * Read handler for Interrupt Cause Read register.
3058 *
3059 * Reading this register acknowledges all interrupts.
3060 *
3061 * @returns VBox status code.
3062 *
3063 * @param pThis The device state structure.
3064 * @param offset Register offset in memory-mapped frame.
3065 * @param index Register index in register array.
3066 * @param mask Not used.
3067 * @thread EMT
3068 */
3069static int e1kRegReadICR(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
3070{
3071 int rc = e1kCsEnter(pThis, VINF_IOM_R3_MMIO_READ);
3072 if (RT_UNLIKELY(rc != VINF_SUCCESS))
3073 return rc;
3074
3075 uint32_t value = 0;
3076 rc = e1kRegReadDefault(pDevIns, pThis, offset, index, &value);
3077 if (RT_SUCCESS(rc))
3078 {
3079 if (value)
3080 {
3081 if (!pThis->fIntRaised)
3082 E1K_INC_ISTAT_CNT(pThis->uStatNoIntICR);
3083 /*
3084 * Not clearing ICR causes QNX to hang as it reads ICR in a loop
3085 * with disabled interrupts.
3086 */
3087 //if (IMS)
3088 if (1)
3089 {
3090 /*
3091 * Interrupts were enabled -- we are supposedly at the very
3092 * beginning of interrupt handler
3093 */
3094 E1kLogRel(("E1000: irq lowered, icr=0x%x\n", ICR));
3095 E1kLog(("%s e1kRegReadICR: Lowered IRQ (%08x)\n", pThis->szPrf, ICR));
3096 /* Clear all pending interrupts */
3097 ICR = 0;
3098 pThis->fIntRaised = false;
3099 /* Lower(0) INTA(0) */
3100 PDMDevHlpPCISetIrq(pDevIns, 0, 0);
3101
3102 pThis->u64AckedAt = PDMDevHlpTimerGet(pDevIns, pThis->hIntTimer);
3103 if (pThis->fIntMaskUsed)
3104 pThis->fDelayInts = true;
3105 }
3106 else
3107 {
3108 /*
3109 * Interrupts are disabled -- in windows guests ICR read is done
3110 * just before re-enabling interrupts
3111 */
3112 E1kLog(("%s e1kRegReadICR: Suppressing auto-clear due to disabled interrupts (%08x)\n", pThis->szPrf, ICR));
3113 }
3114 }
3115 *pu32Value = value;
3116 }
3117 e1kCsLeave(pThis);
3118
3119 return rc;
3120}
3121
3122/**
3123 * Write handler for Interrupt Cause Set register.
3124 *
3125 * Bits corresponding to 1s in 'value' will be set in ICR register.
3126 *
3127 * @param pThis The device state structure.
3128 * @param offset Register offset in memory-mapped frame.
3129 * @param index Register index in register array.
3130 * @param value The value to store.
3131 * @param mask Used to implement partial writes (8 and 16-bit).
3132 * @thread EMT
3133 */
3134static int e1kRegWriteICS(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3135{
3136 RT_NOREF_PV(offset); RT_NOREF_PV(index);
3137 E1K_INC_ISTAT_CNT(pThis->uStatIntICS);
3138 return e1kRaiseInterrupt(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE, value & g_aE1kRegMap[ICS_IDX].writable);
3139}
3140
3141/**
3142 * Write handler for Interrupt Mask Set register.
3143 *
3144 * Will trigger pending interrupts.
3145 *
3146 * @param pThis The device state structure.
3147 * @param offset Register offset in memory-mapped frame.
3148 * @param index Register index in register array.
3149 * @param value The value to store.
3150 * @param mask Used to implement partial writes (8 and 16-bit).
3151 * @thread EMT
3152 */
3153static int e1kRegWriteIMS(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3154{
3155 RT_NOREF_PV(offset); RT_NOREF_PV(index);
3156
3157 IMS |= value;
3158 E1kLogRel(("E1000: irq enabled, RDH=%x RDT=%x TDH=%x TDT=%x\n", RDH, RDT, TDH, TDT));
3159 E1kLog(("%s e1kRegWriteIMS: IRQ enabled\n", pThis->szPrf));
3160 /*
3161 * We cannot raise an interrupt here as it will occasionally cause an interrupt storm
3162 * in Windows guests (see @bugref{8624}, @bugref{5023}).
3163 */
3164 if ((ICR & IMS) && !pThis->fLocked)
3165 {
3166 E1K_INC_ISTAT_CNT(pThis->uStatIntIMS);
3167 e1kPostponeInterrupt(pDevIns, pThis, E1K_IMS_INT_DELAY_NS);
3168 }
3169
3170 return VINF_SUCCESS;
3171}
3172
3173/**
3174 * Write handler for Interrupt Mask Clear register.
3175 *
3176 * Bits corresponding to 1s in 'value' will be cleared in IMS register.
3177 *
3178 * @param pThis The device state structure.
3179 * @param offset Register offset in memory-mapped frame.
3180 * @param index Register index in register array.
3181 * @param value The value to store.
3182 * @param mask Used to implement partial writes (8 and 16-bit).
3183 * @thread EMT
3184 */
3185static int e1kRegWriteIMC(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3186{
3187 RT_NOREF_PV(offset); RT_NOREF_PV(index);
3188
3189 int rc = e1kCsEnter(pThis, VINF_IOM_R3_MMIO_WRITE);
3190 if (RT_UNLIKELY(rc != VINF_SUCCESS))
3191 return rc;
3192 if (pThis->fIntRaised)
3193 {
3194 /*
3195 * Technically we should reset fIntRaised in ICR read handler, but it will cause
3196 * Windows to freeze since it may receive an interrupt while still in the very beginning
3197 * of interrupt handler.
3198 */
3199 E1K_INC_ISTAT_CNT(pThis->uStatIntLower);
3200 STAM_COUNTER_INC(&pThis->StatIntsPrevented);
3201 E1kLogRel(("E1000: irq lowered (IMC), icr=0x%x\n", ICR));
3202 /* Lower(0) INTA(0) */
3203 PDMDevHlpPCISetIrq(pDevIns, 0, 0);
3204 pThis->fIntRaised = false;
3205 E1kLog(("%s e1kRegWriteIMC: Lowered IRQ: ICR=%08x\n", pThis->szPrf, ICR));
3206 }
3207 IMS &= ~value;
3208 E1kLog(("%s e1kRegWriteIMC: IRQ disabled\n", pThis->szPrf));
3209 e1kCsLeave(pThis);
3210
3211 return VINF_SUCCESS;
3212}
3213
3214/**
3215 * Write handler for Receive Control register.
3216 *
3217 * @param pThis The device state structure.
3218 * @param offset Register offset in memory-mapped frame.
3219 * @param index Register index in register array.
3220 * @param value The value to store.
3221 * @param mask Used to implement partial writes (8 and 16-bit).
3222 * @thread EMT
3223 */
3224static int e1kRegWriteRCTL(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3225{
3226 /* Update promiscuous mode */
3227 bool fBecomePromiscous = !!(value & (RCTL_UPE | RCTL_MPE));
3228 if (fBecomePromiscous != !!( RCTL & (RCTL_UPE | RCTL_MPE)))
3229 {
3230 /* Promiscuity has changed, pass the knowledge on. */
3231#ifndef IN_RING3
3232 return VINF_IOM_R3_MMIO_WRITE;
3233#else
3234 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
3235 if (pThisCC->pDrvR3)
3236 pThisCC->pDrvR3->pfnSetPromiscuousMode(pThisCC->pDrvR3, fBecomePromiscous);
3237#endif
3238 }
3239
3240 /* Adjust receive buffer size */
3241 unsigned cbRxBuf = 2048 >> GET_BITS_V(value, RCTL, BSIZE);
3242 if (value & RCTL_BSEX)
3243 cbRxBuf *= 16;
3244 if (cbRxBuf > E1K_MAX_RX_PKT_SIZE)
3245 cbRxBuf = E1K_MAX_RX_PKT_SIZE;
3246 if (cbRxBuf != pThis->u16RxBSize)
3247 E1kLog2(("%s e1kRegWriteRCTL: Setting receive buffer size to %d (old %d)\n",
3248 pThis->szPrf, cbRxBuf, pThis->u16RxBSize));
3249 Assert(cbRxBuf < 65536);
3250 pThis->u16RxBSize = (uint16_t)cbRxBuf;
3251
3252 /* Update the register */
3253 return e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
3254}
3255
3256/**
3257 * Write handler for Packet Buffer Allocation register.
3258 *
3259 * TXA = 64 - RXA.
3260 *
3261 * @param pThis The device state structure.
3262 * @param offset Register offset in memory-mapped frame.
3263 * @param index Register index in register array.
3264 * @param value The value to store.
3265 * @param mask Used to implement partial writes (8 and 16-bit).
3266 * @thread EMT
3267 */
3268static int e1kRegWritePBA(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3269{
3270 e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
3271 PBA_st->txa = 64 - PBA_st->rxa;
3272
3273 return VINF_SUCCESS;
3274}
3275
3276/**
3277 * Write handler for Receive Descriptor Tail register.
3278 *
3279 * @remarks Write into RDT forces switch to HC and signal to
3280 * e1kR3NetworkDown_WaitReceiveAvail().
3281 *
3282 * @returns VBox status code.
3283 *
3284 * @param pThis The device state structure.
3285 * @param offset Register offset in memory-mapped frame.
3286 * @param index Register index in register array.
3287 * @param value The value to store.
3288 * @param mask Used to implement partial writes (8 and 16-bit).
3289 * @thread EMT
3290 */
3291static int e1kRegWriteRDT(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3292{
3293#ifndef IN_RING3
3294 /* XXX */
3295// return VINF_IOM_R3_MMIO_WRITE;
3296#endif
3297 int rc = e1kCsRxEnter(pThis, VINF_IOM_R3_MMIO_WRITE);
3298 if (RT_LIKELY(rc == VINF_SUCCESS))
3299 {
3300 E1kLog(("%s e1kRegWriteRDT\n", pThis->szPrf));
3301#ifndef E1K_WITH_RXD_CACHE
3302 /*
3303 * Some drivers advance RDT too far, so that it equals RDH. This
3304 * somehow manages to work with real hardware but not with this
3305 * emulated device. We can work with these drivers if we just
3306 * write 1 less when we see a driver writing RDT equal to RDH,
3307 * see @bugref{7346}.
3308 */
3309 if (value == RDH)
3310 {
3311 if (RDH == 0)
3312 value = (RDLEN / sizeof(E1KRXDESC)) - 1;
3313 else
3314 value = RDH - 1;
3315 }
3316#endif /* !E1K_WITH_RXD_CACHE */
3317 rc = e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
3318#ifdef E1K_WITH_RXD_CACHE
3319 /*
3320 * We need to fetch descriptors now as RDT may go whole circle
3321 * before we attempt to store a received packet. For example,
3322 * Intel's DOS drivers use 2 (!) RX descriptors with the total ring
3323 * size being only 8 descriptors! Note that we fetch descriptors
3324 * only when the cache is empty to reduce the number of memory reads
3325 * in case of frequent RDT writes. Don't fetch anything when the
3326 * receiver is disabled either as RDH, RDT, RDLEN can be in some
3327 * messed up state.
3328 * Note that despite the cache may seem empty, meaning that there are
3329 * no more available descriptors in it, it may still be used by RX
3330 * thread which has not yet written the last descriptor back but has
3331 * temporarily released the RX lock in order to write the packet body
3332 * to descriptor's buffer. At this point we still going to do prefetch
3333 * but it won't actually fetch anything if there are no unused slots in
3334 * our "empty" cache (nRxDFetched==E1K_RXD_CACHE_SIZE). We must not
3335 * reset the cache here even if it appears empty. It will be reset at
3336 * a later point in e1kRxDGet().
3337 */
3338 if (e1kRxDIsCacheEmpty(pThis) && (RCTL & RCTL_EN))
3339 e1kRxDPrefetch(pDevIns, pThis);
3340#endif /* E1K_WITH_RXD_CACHE */
3341 e1kCsRxLeave(pThis);
3342 if (RT_SUCCESS(rc))
3343 {
3344 /* Signal that we have more receive descriptors available. */
3345 e1kWakeupReceive(pDevIns, pThis);
3346 }
3347 }
3348 return rc;
3349}
3350
3351/**
3352 * Write handler for Receive Delay Timer register.
3353 *
3354 * @param pThis The device state structure.
3355 * @param offset Register offset in memory-mapped frame.
3356 * @param index Register index in register array.
3357 * @param value The value to store.
3358 * @param mask Used to implement partial writes (8 and 16-bit).
3359 * @thread EMT
3360 */
3361static int e1kRegWriteRDTR(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
3362{
3363 e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
3364 if (value & RDTR_FPD)
3365 {
3366 /* Flush requested, cancel both timers and raise interrupt */
3367#ifdef E1K_USE_RX_TIMERS
3368 e1kCancelTimer(pDevIns, pThis, pThis->hRIDTimer);
3369 e1kCancelTimer(pDevIns, pThis, pThis->hRADTimer);
3370#endif
3371 E1K_INC_ISTAT_CNT(pThis->uStatIntRDTR);
3372 return e1kRaiseInterrupt(pDevIns, pThis, VINF_IOM_R3_MMIO_WRITE, ICR_RXT0);
3373 }
3374
3375 return VINF_SUCCESS;
3376}
3377
3378DECLINLINE(uint32_t) e1kGetTxLen(PE1KSTATE pThis)
3379{
3380 /**
3381 * Make sure TDT won't change during computation. EMT may modify TDT at
3382 * any moment.
3383 */
3384 uint32_t tdt = TDT;
3385 return (TDH>tdt ? TDLEN/sizeof(E1KTXDESC) : 0) + tdt - TDH;
3386}
3387
3388#ifdef IN_RING3
3389
3390# ifdef E1K_TX_DELAY
3391/**
3392 * Transmit Delay Timer handler.
3393 *
3394 * @remarks We only get here when the timer expires.
3395 *
3396 * @param pDevIns Pointer to device instance structure.
3397 * @param pTimer Pointer to the timer.
3398 * @param pvUser NULL.
3399 * @thread EMT
3400 */
3401static DECLCALLBACK(void) e1kR3TxDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
3402{
3403 PE1KSTATE pThis = (PE1KSTATE)pvUser;
3404 Assert(PDMCritSectIsOwner(&pThis->csTx));
3405
3406 E1K_INC_ISTAT_CNT(pThis->uStatTxDelayExp);
3407# ifdef E1K_INT_STATS
3408 uint64_t u64Elapsed = RTTimeNanoTS() - pThis->u64ArmedAt;
3409 if (u64Elapsed > pThis->uStatMaxTxDelay)
3410 pThis->uStatMaxTxDelay = u64Elapsed;
3411# endif
3412 int rc = e1kXmitPending(pDevIns, pThis, false /*fOnWorkerThread*/);
3413 AssertMsg(RT_SUCCESS(rc) || rc == VERR_TRY_AGAIN, ("%Rrc\n", rc));
3414}
3415# endif /* E1K_TX_DELAY */
3416
3417//# ifdef E1K_USE_TX_TIMERS
3418
3419/**
3420 * Transmit Interrupt Delay Timer handler.
3421 *
3422 * @remarks We only get here when the timer expires.
3423 *
3424 * @param pDevIns Pointer to device instance structure.
3425 * @param pTimer Pointer to the timer.
3426 * @param pvUser NULL.
3427 * @thread EMT
3428 */
3429static DECLCALLBACK(void) e1kR3TxIntDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
3430{
3431 RT_NOREF(pDevIns);
3432 RT_NOREF(pTimer);
3433 PE1KSTATE pThis = (PE1KSTATE)pvUser;
3434
3435 E1K_INC_ISTAT_CNT(pThis->uStatTID);
3436 /* Cancel absolute delay timer as we have already got attention */
3437# ifndef E1K_NO_TAD
3438 e1kCancelTimer(pDevIns, pThis, pThis->hTADTimer);
3439# endif
3440 e1kRaiseInterrupt(pDevIns, pThis, ICR_TXDW);
3441}
3442
3443/**
3444 * Transmit Absolute Delay Timer handler.
3445 *
3446 * @remarks We only get here when the timer expires.
3447 *
3448 * @param pDevIns Pointer to device instance structure.
3449 * @param pTimer Pointer to the timer.
3450 * @param pvUser NULL.
3451 * @thread EMT
3452 */
3453static DECLCALLBACK(void) e1kR3TxAbsDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
3454{
3455 RT_NOREF(pDevIns);
3456 RT_NOREF(pTimer);
3457 PE1KSTATE pThis = (PE1KSTATE)pvUser;
3458
3459 E1K_INC_ISTAT_CNT(pThis->uStatTAD);
3460 /* Cancel interrupt delay timer as we have already got attention */
3461 e1kCancelTimer(pDevIns, pThis, pThis->hTIDTimer);
3462 e1kRaiseInterrupt(pDevIns, pThis, ICR_TXDW);
3463}
3464
3465//# endif /* E1K_USE_TX_TIMERS */
3466# ifdef E1K_USE_RX_TIMERS
3467
3468/**
3469 * Receive Interrupt Delay Timer handler.
3470 *
3471 * @remarks We only get here when the timer expires.
3472 *
3473 * @param pDevIns Pointer to device instance structure.
3474 * @param pTimer Pointer to the timer.
3475 * @param pvUser NULL.
3476 * @thread EMT
3477 */
3478static DECLCALLBACK(void) e1kR3RxIntDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
3479{
3480 PE1KSTATE pThis = (PE1KSTATE)pvUser;
3481
3482 E1K_INC_ISTAT_CNT(pThis->uStatRID);
3483 /* Cancel absolute delay timer as we have already got attention */
3484 e1kCancelTimer(pDevIns, pThis, pThis->hRADTimer);
3485 e1kRaiseInterrupt(pDevIns, pThis, ICR_RXT0);
3486}
3487
3488/**
3489 * Receive Absolute Delay Timer handler.
3490 *
3491 * @remarks We only get here when the timer expires.
3492 *
3493 * @param pDevIns Pointer to device instance structure.
3494 * @param pTimer Pointer to the timer.
3495 * @param pvUser NULL.
3496 * @thread EMT
3497 */
3498static DECLCALLBACK(void) e1kR3RxAbsDelayTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
3499{
3500 PE1KSTATE pThis = (PE1KSTATE)pvUser;
3501
3502 E1K_INC_ISTAT_CNT(pThis->uStatRAD);
3503 /* Cancel interrupt delay timer as we have already got attention */
3504 e1kCancelTimer(pDevIns, pThis, pThis->hRIDTimer);
3505 e1kRaiseInterrupt(pDevIns, pThis, ICR_RXT0);
3506}
3507
3508# endif /* E1K_USE_RX_TIMERS */
3509
3510/**
3511 * Late Interrupt Timer handler.
3512 *
3513 * @param pDevIns Pointer to device instance structure.
3514 * @param pTimer Pointer to the timer.
3515 * @param pvUser NULL.
3516 * @thread EMT
3517 */
3518static DECLCALLBACK(void) e1kR3LateIntTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
3519{
3520 RT_NOREF(pDevIns, pTimer);
3521 PE1KSTATE pThis = (PE1KSTATE)pvUser;
3522
3523 STAM_PROFILE_ADV_START(&pThis->StatLateIntTimer, a);
3524 STAM_COUNTER_INC(&pThis->StatLateInts);
3525 E1K_INC_ISTAT_CNT(pThis->uStatIntLate);
3526# if 0
3527 if (pThis->iStatIntLost > -100)
3528 pThis->iStatIntLost--;
3529# endif
3530 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, 0);
3531 STAM_PROFILE_ADV_STOP(&pThis->StatLateIntTimer, a);
3532}
3533
3534/**
3535 * Link Up Timer handler.
3536 *
3537 * @param pDevIns Pointer to device instance structure.
3538 * @param pTimer Pointer to the timer.
3539 * @param pvUser NULL.
3540 * @thread EMT
3541 */
3542static DECLCALLBACK(void) e1kR3LinkUpTimer(PPDMDEVINS pDevIns, PTMTIMER pTimer, void *pvUser)
3543{
3544 RT_NOREF(pTimer);
3545 PE1KSTATE pThis = (PE1KSTATE)pvUser;
3546 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
3547
3548 /*
3549 * This can happen if we set the link status to down when the Link up timer was
3550 * already armed (shortly after e1kLoadDone() or when the cable was disconnected
3551 * and connect+disconnect the cable very quick. Moreover, 82543GC triggers LSC
3552 * on reset even if the cable is unplugged (see @bugref{8942}).
3553 */
3554 if (pThis->fCableConnected)
3555 {
3556 /* 82543GC does not have an internal PHY */
3557 if (pThis->eChip == E1K_CHIP_82543GC || (CTRL & CTRL_SLU))
3558 e1kR3LinkUp(pDevIns, pThis, pThisCC);
3559 }
3560# ifdef E1K_LSC_ON_RESET
3561 else if (pThis->eChip == E1K_CHIP_82543GC)
3562 e1kR3LinkDown(pDevIns, pThis, pThisCC);
3563# endif /* E1K_LSC_ON_RESET */
3564}
3565
3566#endif /* IN_RING3 */
3567
3568/**
3569 * Sets up the GSO context according to the TSE new context descriptor.
3570 *
3571 * @param pGso The GSO context to setup.
3572 * @param pCtx The context descriptor.
3573 */
3574DECLINLINE(void) e1kSetupGsoCtx(PPDMNETWORKGSO pGso, E1KTXCTX const *pCtx)
3575{
3576 pGso->u8Type = PDMNETWORKGSOTYPE_INVALID;
3577
3578 /*
3579 * See if the context descriptor describes something that could be TCP or
3580 * UDP over IPv[46].
3581 */
3582 /* Check the header ordering and spacing: 1. Ethernet, 2. IP, 3. TCP/UDP. */
3583 if (RT_UNLIKELY( pCtx->ip.u8CSS < sizeof(RTNETETHERHDR) ))
3584 {
3585 E1kLog(("e1kSetupGsoCtx: IPCSS=%#x\n", pCtx->ip.u8CSS));
3586 return;
3587 }
3588 if (RT_UNLIKELY( pCtx->tu.u8CSS < (size_t)pCtx->ip.u8CSS + (pCtx->dw2.fIP ? RTNETIPV4_MIN_LEN : RTNETIPV6_MIN_LEN) ))
3589 {
3590 E1kLog(("e1kSetupGsoCtx: TUCSS=%#x\n", pCtx->tu.u8CSS));
3591 return;
3592 }
3593 if (RT_UNLIKELY( pCtx->dw2.fTCP
3594 ? pCtx->dw3.u8HDRLEN < (size_t)pCtx->tu.u8CSS + RTNETTCP_MIN_LEN
3595 : pCtx->dw3.u8HDRLEN != (size_t)pCtx->tu.u8CSS + RTNETUDP_MIN_LEN ))
3596 {
3597 E1kLog(("e1kSetupGsoCtx: HDRLEN=%#x TCP=%d\n", pCtx->dw3.u8HDRLEN, pCtx->dw2.fTCP));
3598 return;
3599 }
3600
3601 /* The end of the TCP/UDP checksum should stop at the end of the packet or at least after the headers. */
3602 if (RT_UNLIKELY( pCtx->tu.u16CSE > 0 && pCtx->tu.u16CSE <= pCtx->dw3.u8HDRLEN ))
3603 {
3604 E1kLog(("e1kSetupGsoCtx: TUCSE=%#x HDRLEN=%#x\n", pCtx->tu.u16CSE, pCtx->dw3.u8HDRLEN));
3605 return;
3606 }
3607
3608 /* IPv4 checksum offset. */
3609 if (RT_UNLIKELY( pCtx->dw2.fIP && (size_t)pCtx->ip.u8CSO - pCtx->ip.u8CSS != RT_UOFFSETOF(RTNETIPV4, ip_sum) ))
3610 {
3611 E1kLog(("e1kSetupGsoCtx: IPCSO=%#x IPCSS=%#x\n", pCtx->ip.u8CSO, pCtx->ip.u8CSS));
3612 return;
3613 }
3614
3615 /* TCP/UDP checksum offsets. */
3616 if (RT_UNLIKELY( (size_t)pCtx->tu.u8CSO - pCtx->tu.u8CSS
3617 != ( pCtx->dw2.fTCP
3618 ? RT_UOFFSETOF(RTNETTCP, th_sum)
3619 : RT_UOFFSETOF(RTNETUDP, uh_sum) ) ))
3620 {
3621 E1kLog(("e1kSetupGsoCtx: TUCSO=%#x TUCSS=%#x TCP=%d\n", pCtx->ip.u8CSO, pCtx->ip.u8CSS, pCtx->dw2.fTCP));
3622 return;
3623 }
3624
3625 /*
3626 * Because of internal networking using a 16-bit size field for GSO context
3627 * plus frame, we have to make sure we don't exceed this.
3628 */
3629 if (RT_UNLIKELY( pCtx->dw3.u8HDRLEN + pCtx->dw2.u20PAYLEN > VBOX_MAX_GSO_SIZE ))
3630 {
3631 E1kLog(("e1kSetupGsoCtx: HDRLEN(=%#x) + PAYLEN(=%#x) = %#x, max is %#x\n",
3632 pCtx->dw3.u8HDRLEN, pCtx->dw2.u20PAYLEN, pCtx->dw3.u8HDRLEN + pCtx->dw2.u20PAYLEN, VBOX_MAX_GSO_SIZE));
3633 return;
3634 }
3635
3636 /*
3637 * We're good for now - we'll do more checks when seeing the data.
3638 * So, figure the type of offloading and setup the context.
3639 */
3640 if (pCtx->dw2.fIP)
3641 {
3642 if (pCtx->dw2.fTCP)
3643 {
3644 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_TCP;
3645 pGso->cbHdrsSeg = pCtx->dw3.u8HDRLEN;
3646 }
3647 else
3648 {
3649 pGso->u8Type = PDMNETWORKGSOTYPE_IPV4_UDP;
3650 pGso->cbHdrsSeg = pCtx->tu.u8CSS; /* IP header only */
3651 }
3652 /** @todo Detect IPv4-IPv6 tunneling (need test setup since linux doesn't do
3653 * this yet it seems)... */
3654 }
3655 else
3656 {
3657 pGso->cbHdrsSeg = pCtx->dw3.u8HDRLEN; /** @todo IPv6 UFO */
3658 if (pCtx->dw2.fTCP)
3659 pGso->u8Type = PDMNETWORKGSOTYPE_IPV6_TCP;
3660 else
3661 pGso->u8Type = PDMNETWORKGSOTYPE_IPV6_UDP;
3662 }
3663 pGso->offHdr1 = pCtx->ip.u8CSS;
3664 pGso->offHdr2 = pCtx->tu.u8CSS;
3665 pGso->cbHdrsTotal = pCtx->dw3.u8HDRLEN;
3666 pGso->cbMaxSeg = pCtx->dw3.u16MSS + (pGso->u8Type == PDMNETWORKGSOTYPE_IPV4_UDP ? pGso->offHdr2 : 0);
3667 Assert(PDMNetGsoIsValid(pGso, sizeof(*pGso), pGso->cbMaxSeg * 5));
3668 E1kLog2(("e1kSetupGsoCtx: mss=%#x hdr=%#x hdrseg=%#x hdr1=%#x hdr2=%#x %s\n",
3669 pGso->cbMaxSeg, pGso->cbHdrsTotal, pGso->cbHdrsSeg, pGso->offHdr1, pGso->offHdr2, PDMNetGsoTypeName((PDMNETWORKGSOTYPE)pGso->u8Type) ));
3670}
3671
3672/**
3673 * Checks if we can use GSO processing for the current TSE frame.
3674 *
3675 * @param pThis The device state structure.
3676 * @param pGso The GSO context.
3677 * @param pData The first data descriptor of the frame.
3678 * @param pCtx The TSO context descriptor.
3679 */
3680DECLINLINE(bool) e1kCanDoGso(PE1KSTATE pThis, PCPDMNETWORKGSO pGso, E1KTXDAT const *pData, E1KTXCTX const *pCtx)
3681{
3682 if (!pData->cmd.fTSE)
3683 {
3684 E1kLog2(("e1kCanDoGso: !TSE\n"));
3685 return false;
3686 }
3687 if (pData->cmd.fVLE) /** @todo VLAN tagging. */
3688 {
3689 E1kLog(("e1kCanDoGso: VLE\n"));
3690 return false;
3691 }
3692 if (RT_UNLIKELY(!pThis->fGSOEnabled))
3693 {
3694 E1kLog3(("e1kCanDoGso: GSO disabled via CFGM\n"));
3695 return false;
3696 }
3697
3698 switch ((PDMNETWORKGSOTYPE)pGso->u8Type)
3699 {
3700 case PDMNETWORKGSOTYPE_IPV4_TCP:
3701 case PDMNETWORKGSOTYPE_IPV4_UDP:
3702 if (!pData->dw3.fIXSM)
3703 {
3704 E1kLog(("e1kCanDoGso: !IXSM (IPv4)\n"));
3705 return false;
3706 }
3707 if (!pData->dw3.fTXSM)
3708 {
3709 E1kLog(("e1kCanDoGso: !TXSM (IPv4)\n"));
3710 return false;
3711 }
3712 /** @todo what more check should we perform here? Ethernet frame type? */
3713 E1kLog2(("e1kCanDoGso: OK, IPv4\n"));
3714 return true;
3715
3716 case PDMNETWORKGSOTYPE_IPV6_TCP:
3717 case PDMNETWORKGSOTYPE_IPV6_UDP:
3718 if (pData->dw3.fIXSM && pCtx->ip.u8CSO)
3719 {
3720 E1kLog(("e1kCanDoGso: IXSM (IPv6)\n"));
3721 return false;
3722 }
3723 if (!pData->dw3.fTXSM)
3724 {
3725 E1kLog(("e1kCanDoGso: TXSM (IPv6)\n"));
3726 return false;
3727 }
3728 /** @todo what more check should we perform here? Ethernet frame type? */
3729 E1kLog2(("e1kCanDoGso: OK, IPv4\n"));
3730 return true;
3731
3732 default:
3733 Assert(pGso->u8Type == PDMNETWORKGSOTYPE_INVALID);
3734 E1kLog2(("e1kCanDoGso: e1kSetupGsoCtx failed\n"));
3735 return false;
3736 }
3737}
3738
3739/**
3740 * Frees the current xmit buffer.
3741 *
3742 * @param pThis The device state structure.
3743 */
3744static void e1kXmitFreeBuf(PE1KSTATE pThis, PE1KSTATECC pThisCC)
3745{
3746 PPDMSCATTERGATHER pSg = pThisCC->CTX_SUFF(pTxSg);
3747 if (pSg)
3748 {
3749 pThisCC->CTX_SUFF(pTxSg) = NULL;
3750
3751 if (pSg->pvAllocator != pThis)
3752 {
3753 PPDMINETWORKUP pDrv = pThisCC->CTX_SUFF(pDrv);
3754 if (pDrv)
3755 pDrv->pfnFreeBuf(pDrv, pSg);
3756 }
3757 else
3758 {
3759 /* loopback */
3760 AssertCompileMemberSize(E1KSTATE, uTxFallback.Sg, 8 * sizeof(size_t));
3761 Assert(pSg->fFlags == (PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_3));
3762 pSg->fFlags = 0;
3763 pSg->pvAllocator = NULL;
3764 }
3765 }
3766}
3767
3768#ifndef E1K_WITH_TXD_CACHE
3769/**
3770 * Allocates an xmit buffer.
3771 *
3772 * @returns See PDMINETWORKUP::pfnAllocBuf.
3773 * @param pThis The device state structure.
3774 * @param cbMin The minimum frame size.
3775 * @param fExactSize Whether cbMin is exact or if we have to max it
3776 * out to the max MTU size.
3777 * @param fGso Whether this is a GSO frame or not.
3778 */
3779DECLINLINE(int) e1kXmitAllocBuf(PE1KSTATE pThis, PE1KSTATECC pThisCC, size_t cbMin, bool fExactSize, bool fGso)
3780{
3781 /* Adjust cbMin if necessary. */
3782 if (!fExactSize)
3783 cbMin = RT_MAX(cbMin, E1K_MAX_TX_PKT_SIZE);
3784
3785 /* Deal with existing buffer (descriptor screw up, reset, etc). */
3786 if (RT_UNLIKELY(pThisCC->CTX_SUFF(pTxSg)))
3787 e1kXmitFreeBuf(pThis, pThisCC);
3788 Assert(pThisCC->CTX_SUFF(pTxSg) == NULL);
3789
3790 /*
3791 * Allocate the buffer.
3792 */
3793 PPDMSCATTERGATHER pSg;
3794 if (RT_LIKELY(GET_BITS(RCTL, LBM) != RCTL_LBM_TCVR))
3795 {
3796 PPDMINETWORKUP pDrv = pThisCC->CTX_SUFF(pDrv);
3797 if (RT_UNLIKELY(!pDrv))
3798 return VERR_NET_DOWN;
3799 int rc = pDrv->pfnAllocBuf(pDrv, cbMin, fGso ? &pThis->GsoCtx : NULL, &pSg);
3800 if (RT_FAILURE(rc))
3801 {
3802 /* Suspend TX as we are out of buffers atm */
3803 STATUS |= STATUS_TXOFF;
3804 return rc;
3805 }
3806 }
3807 else
3808 {
3809 /* Create a loopback using the fallback buffer and preallocated SG. */
3810 AssertCompileMemberSize(E1KSTATE, uTxFallback.Sg, 8 * sizeof(size_t));
3811 pSg = &pThis->uTxFallback.Sg;
3812 pSg->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_3;
3813 pSg->cbUsed = 0;
3814 pSg->cbAvailable = 0;
3815 pSg->pvAllocator = pThis;
3816 pSg->pvUser = NULL; /* No GSO here. */
3817 pSg->cSegs = 1;
3818 pSg->aSegs[0].pvSeg = pThis->aTxPacketFallback;
3819 pSg->aSegs[0].cbSeg = sizeof(pThis->aTxPacketFallback);
3820 }
3821
3822 pThisCC->CTX_SUFF(pTxSg) = pSg;
3823 return VINF_SUCCESS;
3824}
3825#else /* E1K_WITH_TXD_CACHE */
3826/**
3827 * Allocates an xmit buffer.
3828 *
3829 * @returns See PDMINETWORKUP::pfnAllocBuf.
3830 * @param pThis The device state structure.
3831 * @param cbMin The minimum frame size.
3832 * @param fExactSize Whether cbMin is exact or if we have to max it
3833 * out to the max MTU size.
3834 * @param fGso Whether this is a GSO frame or not.
3835 */
3836DECLINLINE(int) e1kXmitAllocBuf(PE1KSTATE pThis, PE1KSTATECC pThisCC, bool fGso)
3837{
3838 /* Deal with existing buffer (descriptor screw up, reset, etc). */
3839 if (RT_UNLIKELY(pThisCC->CTX_SUFF(pTxSg)))
3840 e1kXmitFreeBuf(pThis, pThisCC);
3841 Assert(pThisCC->CTX_SUFF(pTxSg) == NULL);
3842
3843 /*
3844 * Allocate the buffer.
3845 */
3846 PPDMSCATTERGATHER pSg;
3847 if (RT_LIKELY(GET_BITS(RCTL, LBM) != RCTL_LBM_TCVR))
3848 {
3849 if (pThis->cbTxAlloc == 0)
3850 {
3851 /* Zero packet, no need for the buffer */
3852 return VINF_SUCCESS;
3853 }
3854 if (fGso && pThis->GsoCtx.u8Type == PDMNETWORKGSOTYPE_INVALID)
3855 {
3856 E1kLog3(("Invalid GSO context, won't allocate this packet, cb=%u %s%s\n",
3857 pThis->cbTxAlloc, pThis->fVTag ? "VLAN " : "", pThis->fGSO ? "GSO " : ""));
3858 /* No valid GSO context is available, ignore this packet. */
3859 pThis->cbTxAlloc = 0;
3860 return VINF_SUCCESS;
3861 }
3862
3863 PPDMINETWORKUP pDrv = pThisCC->CTX_SUFF(pDrv);
3864 if (RT_UNLIKELY(!pDrv))
3865 return VERR_NET_DOWN;
3866 int rc = pDrv->pfnAllocBuf(pDrv, pThis->cbTxAlloc, fGso ? &pThis->GsoCtx : NULL, &pSg);
3867 if (RT_FAILURE(rc))
3868 {
3869 /* Suspend TX as we are out of buffers atm */
3870 STATUS |= STATUS_TXOFF;
3871 return rc;
3872 }
3873 E1kLog3(("%s Allocated buffer for TX packet: cb=%u %s%s\n",
3874 pThis->szPrf, pThis->cbTxAlloc,
3875 pThis->fVTag ? "VLAN " : "",
3876 pThis->fGSO ? "GSO " : ""));
3877 }
3878 else
3879 {
3880 /* Create a loopback using the fallback buffer and preallocated SG. */
3881 AssertCompileMemberSize(E1KSTATE, uTxFallback.Sg, 8 * sizeof(size_t));
3882 pSg = &pThis->uTxFallback.Sg;
3883 pSg->fFlags = PDMSCATTERGATHER_FLAGS_MAGIC | PDMSCATTERGATHER_FLAGS_OWNER_3;
3884 pSg->cbUsed = 0;
3885 pSg->cbAvailable = sizeof(pThis->aTxPacketFallback);
3886 pSg->pvAllocator = pThis;
3887 pSg->pvUser = NULL; /* No GSO here. */
3888 pSg->cSegs = 1;
3889 pSg->aSegs[0].pvSeg = pThis->aTxPacketFallback;
3890 pSg->aSegs[0].cbSeg = sizeof(pThis->aTxPacketFallback);
3891 }
3892 pThis->cbTxAlloc = 0;
3893
3894 pThisCC->CTX_SUFF(pTxSg) = pSg;
3895 return VINF_SUCCESS;
3896}
3897#endif /* E1K_WITH_TXD_CACHE */
3898
3899/**
3900 * Checks if it's a GSO buffer or not.
3901 *
3902 * @returns true / false.
3903 * @param pTxSg The scatter / gather buffer.
3904 */
3905DECLINLINE(bool) e1kXmitIsGsoBuf(PDMSCATTERGATHER const *pTxSg)
3906{
3907#if 0
3908 if (!pTxSg)
3909 E1kLog(("e1kXmitIsGsoBuf: pTxSG is NULL\n"));
3910 if (pTxSg && pTxSg->pvUser)
3911 E1kLog(("e1kXmitIsGsoBuf: pvUser is NULL\n"));
3912#endif
3913 return pTxSg && pTxSg->pvUser /* GSO indicator */;
3914}
3915
3916#ifndef E1K_WITH_TXD_CACHE
3917/**
3918 * Load transmit descriptor from guest memory.
3919 *
3920 * @param pDevIns The device instance.
3921 * @param pDesc Pointer to descriptor union.
3922 * @param addr Physical address in guest context.
3923 * @thread E1000_TX
3924 */
3925DECLINLINE(void) e1kLoadDesc(PPDMDEVINS pDevIns, E1KTXDESC *pDesc, RTGCPHYS addr)
3926{
3927 PDMDevHlpPCIPhysRead(pDevIns, addr, pDesc, sizeof(E1KTXDESC));
3928}
3929#else /* E1K_WITH_TXD_CACHE */
3930/**
3931 * Load transmit descriptors from guest memory.
3932 *
3933 * We need two physical reads in case the tail wrapped around the end of TX
3934 * descriptor ring.
3935 *
3936 * @returns the actual number of descriptors fetched.
3937 * @param pDevIns The device instance.
3938 * @param pThis The device state structure.
3939 * @thread E1000_TX
3940 */
3941DECLINLINE(unsigned) e1kTxDLoadMore(PPDMDEVINS pDevIns, PE1KSTATE pThis)
3942{
3943 Assert(pThis->iTxDCurrent == 0);
3944 /* We've already loaded pThis->nTxDFetched descriptors past TDH. */
3945 unsigned nDescsAvailable = e1kGetTxLen(pThis) - pThis->nTxDFetched;
3946 /* The following two lines ensure that pThis->nTxDFetched never overflows. */
3947 AssertCompile(E1K_TXD_CACHE_SIZE < (256 * sizeof(pThis->nTxDFetched)));
3948 unsigned nDescsToFetch = RT_MIN(nDescsAvailable, E1K_TXD_CACHE_SIZE - pThis->nTxDFetched);
3949 unsigned nDescsTotal = TDLEN / sizeof(E1KTXDESC);
3950 Assert(nDescsTotal != 0);
3951 if (nDescsTotal == 0)
3952 return 0;
3953 unsigned nFirstNotLoaded = (TDH + pThis->nTxDFetched) % nDescsTotal;
3954 unsigned nDescsInSingleRead = RT_MIN(nDescsToFetch, nDescsTotal - nFirstNotLoaded);
3955 E1kLog3(("%s e1kTxDLoadMore: nDescsAvailable=%u nDescsToFetch=%u nDescsTotal=%u nFirstNotLoaded=0x%x nDescsInSingleRead=%u\n",
3956 pThis->szPrf, nDescsAvailable, nDescsToFetch, nDescsTotal,
3957 nFirstNotLoaded, nDescsInSingleRead));
3958 if (nDescsToFetch == 0)
3959 return 0;
3960 E1KTXDESC* pFirstEmptyDesc = &pThis->aTxDescriptors[pThis->nTxDFetched];
3961 PDMDevHlpPCIPhysRead(pDevIns,
3962 ((uint64_t)TDBAH << 32) + TDBAL + nFirstNotLoaded * sizeof(E1KTXDESC),
3963 pFirstEmptyDesc, nDescsInSingleRead * sizeof(E1KTXDESC));
3964 E1kLog3(("%s Fetched %u TX descriptors at %08x%08x(0x%x), TDLEN=%08x, TDH=%08x, TDT=%08x\n",
3965 pThis->szPrf, nDescsInSingleRead,
3966 TDBAH, TDBAL + TDH * sizeof(E1KTXDESC),
3967 nFirstNotLoaded, TDLEN, TDH, TDT));
3968 if (nDescsToFetch > nDescsInSingleRead)
3969 {
3970 PDMDevHlpPCIPhysRead(pDevIns,
3971 ((uint64_t)TDBAH << 32) + TDBAL,
3972 pFirstEmptyDesc + nDescsInSingleRead,
3973 (nDescsToFetch - nDescsInSingleRead) * sizeof(E1KTXDESC));
3974 E1kLog3(("%s Fetched %u TX descriptors at %08x%08x\n",
3975 pThis->szPrf, nDescsToFetch - nDescsInSingleRead,
3976 TDBAH, TDBAL));
3977 }
3978 pThis->nTxDFetched += (uint8_t)nDescsToFetch;
3979 return nDescsToFetch;
3980}
3981
3982/**
3983 * Load transmit descriptors from guest memory only if there are no loaded
3984 * descriptors.
3985 *
3986 * @returns true if there are descriptors in cache.
3987 * @param pDevIns The device instance.
3988 * @param pThis The device state structure.
3989 * @thread E1000_TX
3990 */
3991DECLINLINE(bool) e1kTxDLazyLoad(PPDMDEVINS pDevIns, PE1KSTATE pThis)
3992{
3993 if (pThis->nTxDFetched == 0)
3994 return e1kTxDLoadMore(pDevIns, pThis) != 0;
3995 return true;
3996}
3997#endif /* E1K_WITH_TXD_CACHE */
3998
3999/**
4000 * Write back transmit descriptor to guest memory.
4001 *
4002 * @param pDevIns The device instance.
4003 * @param pThis The device state structure.
4004 * @param pDesc Pointer to descriptor union.
4005 * @param addr Physical address in guest context.
4006 * @thread E1000_TX
4007 */
4008DECLINLINE(void) e1kWriteBackDesc(PPDMDEVINS pDevIns, PE1KSTATE pThis, E1KTXDESC *pDesc, RTGCPHYS addr)
4009{
4010 /* Only the last half of the descriptor has to be written back. */
4011 e1kPrintTDesc(pThis, pDesc, "^^^");
4012 PDMDevHlpPCIPhysWrite(pDevIns, addr, pDesc, sizeof(E1KTXDESC));
4013}
4014
4015/**
4016 * Transmit complete frame.
4017 *
4018 * @remarks We skip the FCS since we're not responsible for sending anything to
4019 * a real ethernet wire.
4020 *
4021 * @param pDevIns The device instance.
4022 * @param pThis The device state structure.
4023 * @param pThisCC The current context instance data.
4024 * @param fOnWorkerThread Whether we're on a worker thread or an EMT.
4025 * @thread E1000_TX
4026 */
4027static void e1kTransmitFrame(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC, bool fOnWorkerThread)
4028{
4029 PPDMSCATTERGATHER pSg = pThisCC->CTX_SUFF(pTxSg);
4030 uint32_t cbFrame = pSg ? (uint32_t)pSg->cbUsed : 0;
4031 Assert(!pSg || pSg->cSegs == 1);
4032
4033 if (cbFrame > 70) /* unqualified guess */
4034 pThis->led.Asserted.s.fWriting = pThis->led.Actual.s.fWriting = 1;
4035
4036#ifdef E1K_INT_STATS
4037 if (cbFrame <= 1514)
4038 E1K_INC_ISTAT_CNT(pThis->uStatTx1514);
4039 else if (cbFrame <= 2962)
4040 E1K_INC_ISTAT_CNT(pThis->uStatTx2962);
4041 else if (cbFrame <= 4410)
4042 E1K_INC_ISTAT_CNT(pThis->uStatTx4410);
4043 else if (cbFrame <= 5858)
4044 E1K_INC_ISTAT_CNT(pThis->uStatTx5858);
4045 else if (cbFrame <= 7306)
4046 E1K_INC_ISTAT_CNT(pThis->uStatTx7306);
4047 else if (cbFrame <= 8754)
4048 E1K_INC_ISTAT_CNT(pThis->uStatTx8754);
4049 else if (cbFrame <= 16384)
4050 E1K_INC_ISTAT_CNT(pThis->uStatTx16384);
4051 else if (cbFrame <= 32768)
4052 E1K_INC_ISTAT_CNT(pThis->uStatTx32768);
4053 else
4054 E1K_INC_ISTAT_CNT(pThis->uStatTxLarge);
4055#endif /* E1K_INT_STATS */
4056
4057 /* Add VLAN tag */
4058 if (cbFrame > 12 && pThis->fVTag)
4059 {
4060 E1kLog3(("%s Inserting VLAN tag %08x\n",
4061 pThis->szPrf, RT_BE2H_U16((uint16_t)VET) | (RT_BE2H_U16(pThis->u16VTagTCI) << 16)));
4062 memmove((uint8_t*)pSg->aSegs[0].pvSeg + 16, (uint8_t*)pSg->aSegs[0].pvSeg + 12, cbFrame - 12);
4063 *((uint32_t*)pSg->aSegs[0].pvSeg + 3) = RT_BE2H_U16((uint16_t)VET) | (RT_BE2H_U16(pThis->u16VTagTCI) << 16);
4064 pSg->cbUsed += 4;
4065 cbFrame += 4;
4066 Assert(pSg->cbUsed == cbFrame);
4067 Assert(pSg->cbUsed <= pSg->cbAvailable);
4068 }
4069/* E1kLog2(("%s < < < Outgoing packet. Dump follows: > > >\n"
4070 "%.*Rhxd\n"
4071 "%s < < < < < < < < < < < < < End of dump > > > > > > > > > > > >\n",
4072 pThis->szPrf, cbFrame, pSg->aSegs[0].pvSeg, pThis->szPrf));*/
4073
4074 /* Update the stats */
4075 E1K_INC_CNT32(TPT);
4076 E1K_ADD_CNT64(TOTL, TOTH, cbFrame);
4077 E1K_INC_CNT32(GPTC);
4078 if (pSg && e1kIsBroadcast(pSg->aSegs[0].pvSeg))
4079 E1K_INC_CNT32(BPTC);
4080 else if (pSg && e1kIsMulticast(pSg->aSegs[0].pvSeg))
4081 E1K_INC_CNT32(MPTC);
4082 /* Update octet transmit counter */
4083 E1K_ADD_CNT64(GOTCL, GOTCH, cbFrame);
4084 if (pThisCC->CTX_SUFF(pDrv))
4085 STAM_REL_COUNTER_ADD(&pThis->StatTransmitBytes, cbFrame);
4086 if (cbFrame == 64)
4087 E1K_INC_CNT32(PTC64);
4088 else if (cbFrame < 128)
4089 E1K_INC_CNT32(PTC127);
4090 else if (cbFrame < 256)
4091 E1K_INC_CNT32(PTC255);
4092 else if (cbFrame < 512)
4093 E1K_INC_CNT32(PTC511);
4094 else if (cbFrame < 1024)
4095 E1K_INC_CNT32(PTC1023);
4096 else
4097 E1K_INC_CNT32(PTC1522);
4098
4099 E1K_INC_ISTAT_CNT(pThis->uStatTxFrm);
4100
4101 /*
4102 * Dump and send the packet.
4103 */
4104 int rc = VERR_NET_DOWN;
4105 if (pSg && pSg->pvAllocator != pThis)
4106 {
4107 e1kPacketDump(pDevIns, pThis, (uint8_t const *)pSg->aSegs[0].pvSeg, cbFrame, "--> Outgoing");
4108
4109 pThisCC->CTX_SUFF(pTxSg) = NULL;
4110 PPDMINETWORKUP pDrv = pThisCC->CTX_SUFF(pDrv);
4111 if (pDrv)
4112 {
4113 /* Release critical section to avoid deadlock in CanReceive */
4114 //e1kCsLeave(pThis);
4115 STAM_PROFILE_START(&pThis->CTX_SUFF_Z(StatTransmitSend), a);
4116 rc = pDrv->pfnSendBuf(pDrv, pSg, fOnWorkerThread);
4117 STAM_PROFILE_STOP(&pThis->CTX_SUFF_Z(StatTransmitSend), a);
4118 //e1kCsEnter(pThis, RT_SRC_POS);
4119 }
4120 }
4121 else if (pSg)
4122 {
4123 Assert(pSg->aSegs[0].pvSeg == pThis->aTxPacketFallback);
4124 e1kPacketDump(pDevIns, pThis, (uint8_t const *)pSg->aSegs[0].pvSeg, cbFrame, "--> Loopback");
4125
4126 /** @todo do we actually need to check that we're in loopback mode here? */
4127 if (GET_BITS(RCTL, LBM) == RCTL_LBM_TCVR)
4128 {
4129 E1KRXDST status;
4130 RT_ZERO(status);
4131 status.fPIF = true;
4132 e1kHandleRxPacket(pDevIns, pThis, pSg->aSegs[0].pvSeg, cbFrame, status);
4133 rc = VINF_SUCCESS;
4134 }
4135 e1kXmitFreeBuf(pThis, pThisCC);
4136 }
4137 else
4138 rc = VERR_NET_DOWN;
4139 if (RT_FAILURE(rc))
4140 {
4141 E1kLogRel(("E1000: ERROR! pfnSend returned %Rrc\n", rc));
4142 /** @todo handle VERR_NET_DOWN and VERR_NET_NO_BUFFER_SPACE. Signal error ? */
4143 }
4144
4145 pThis->led.Actual.s.fWriting = 0;
4146}
4147
4148/**
4149 * Compute and write internet checksum (e1kCSum16) at the specified offset.
4150 *
4151 * @param pThis The device state structure.
4152 * @param pPkt Pointer to the packet.
4153 * @param u16PktLen Total length of the packet.
4154 * @param cso Offset in packet to write checksum at.
4155 * @param css Offset in packet to start computing
4156 * checksum from.
4157 * @param cse Offset in packet to stop computing
4158 * checksum at.
4159 * @thread E1000_TX
4160 */
4161static void e1kInsertChecksum(PE1KSTATE pThis, uint8_t *pPkt, uint16_t u16PktLen, uint8_t cso, uint8_t css, uint16_t cse)
4162{
4163 RT_NOREF1(pThis);
4164
4165 if (css >= u16PktLen)
4166 {
4167 E1kLog2(("%s css(%X) is greater than packet length-1(%X), checksum is not inserted\n",
4168 pThis->szPrf, cso, u16PktLen));
4169 return;
4170 }
4171
4172 if (cso >= u16PktLen - 1)
4173 {
4174 E1kLog2(("%s cso(%X) is greater than packet length-2(%X), checksum is not inserted\n",
4175 pThis->szPrf, cso, u16PktLen));
4176 return;
4177 }
4178
4179 if (cse == 0 || cse >= u16PktLen)
4180 cse = u16PktLen - 1;
4181 else if (cse < css)
4182 {
4183 E1kLog2(("%s css(%X) is greater than cse(%X), checksum is not inserted\n",
4184 pThis->szPrf, css, cse));
4185 return;
4186 }
4187
4188 uint16_t u16ChkSum = e1kCSum16(pPkt + css, cse - css + 1);
4189 E1kLog2(("%s Inserting csum: %04X at %02X, old value: %04X\n", pThis->szPrf,
4190 u16ChkSum, cso, *(uint16_t*)(pPkt + cso)));
4191 *(uint16_t*)(pPkt + cso) = u16ChkSum;
4192}
4193
4194/**
4195 * Add a part of descriptor's buffer to transmit frame.
4196 *
4197 * @remarks data.u64BufAddr is used unconditionally for both data
4198 * and legacy descriptors since it is identical to
4199 * legacy.u64BufAddr.
4200 *
4201 * @param pDevIns The device instance.
4202 * @param pThis The device state structure.
4203 * @param pDesc Pointer to the descriptor to transmit.
4204 * @param u16Len Length of buffer to the end of segment.
4205 * @param fSend Force packet sending.
4206 * @param fOnWorkerThread Whether we're on a worker thread or an EMT.
4207 * @thread E1000_TX
4208 */
4209#ifndef E1K_WITH_TXD_CACHE
4210static void e1kFallbackAddSegment(PPDMDEVINS pDevIns, PE1KSTATE pThis, RTGCPHYS PhysAddr, uint16_t u16Len, bool fSend, bool fOnWorkerThread)
4211{
4212 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
4213 /* TCP header being transmitted */
4214 struct E1kTcpHeader *pTcpHdr = (struct E1kTcpHeader *)(pThis->aTxPacketFallback + pThis->contextTSE.tu.u8CSS);
4215 /* IP header being transmitted */
4216 struct E1kIpHeader *pIpHdr = (struct E1kIpHeader *)(pThis->aTxPacketFallback + pThis->contextTSE.ip.u8CSS);
4217
4218 E1kLog3(("%s e1kFallbackAddSegment: Length=%x, remaining payload=%x, header=%x, send=%RTbool\n",
4219 pThis->szPrf, u16Len, pThis->u32PayRemain, pThis->u16HdrRemain, fSend));
4220 Assert(pThis->u32PayRemain + pThis->u16HdrRemain > 0);
4221
4222 PDMDevHlpPCIPhysRead(pDevIns, PhysAddr, pThis->aTxPacketFallback + pThis->u16TxPktLen, u16Len);
4223 E1kLog3(("%s Dump of the segment:\n"
4224 "%.*Rhxd\n"
4225 "%s --- End of dump ---\n",
4226 pThis->szPrf, u16Len, pThis->aTxPacketFallback + pThis->u16TxPktLen, pThis->szPrf));
4227 pThis->u16TxPktLen += u16Len;
4228 E1kLog3(("%s e1kFallbackAddSegment: pThis->u16TxPktLen=%x\n",
4229 pThis->szPrf, pThis->u16TxPktLen));
4230 if (pThis->u16HdrRemain > 0)
4231 {
4232 /* The header was not complete, check if it is now */
4233 if (u16Len >= pThis->u16HdrRemain)
4234 {
4235 /* The rest is payload */
4236 u16Len -= pThis->u16HdrRemain;
4237 pThis->u16HdrRemain = 0;
4238 /* Save partial checksum and flags */
4239 pThis->u32SavedCsum = pTcpHdr->chksum;
4240 pThis->u16SavedFlags = pTcpHdr->hdrlen_flags;
4241 /* Clear FIN and PSH flags now and set them only in the last segment */
4242 pTcpHdr->hdrlen_flags &= ~htons(E1K_TCP_FIN | E1K_TCP_PSH);
4243 }
4244 else
4245 {
4246 /* Still not */
4247 pThis->u16HdrRemain -= u16Len;
4248 E1kLog3(("%s e1kFallbackAddSegment: Header is still incomplete, 0x%x bytes remain.\n",
4249 pThis->szPrf, pThis->u16HdrRemain));
4250 return;
4251 }
4252 }
4253
4254 pThis->u32PayRemain -= u16Len;
4255
4256 if (fSend)
4257 {
4258 /* Leave ethernet header intact */
4259 /* IP Total Length = payload + headers - ethernet header */
4260 pIpHdr->total_len = htons(pThis->u16TxPktLen - pThis->contextTSE.ip.u8CSS);
4261 E1kLog3(("%s e1kFallbackAddSegment: End of packet, pIpHdr->total_len=%x\n",
4262 pThis->szPrf, ntohs(pIpHdr->total_len)));
4263 /* Update IP Checksum */
4264 pIpHdr->chksum = 0;
4265 e1kInsertChecksum(pThis, pThis->aTxPacketFallback, pThis->u16TxPktLen,
4266 pThis->contextTSE.ip.u8CSO,
4267 pThis->contextTSE.ip.u8CSS,
4268 pThis->contextTSE.ip.u16CSE);
4269
4270 /* Update TCP flags */
4271 /* Restore original FIN and PSH flags for the last segment */
4272 if (pThis->u32PayRemain == 0)
4273 {
4274 pTcpHdr->hdrlen_flags = pThis->u16SavedFlags;
4275 E1K_INC_CNT32(TSCTC);
4276 }
4277 /* Add TCP length to partial pseudo header sum */
4278 uint32_t csum = pThis->u32SavedCsum
4279 + htons(pThis->u16TxPktLen - pThis->contextTSE.tu.u8CSS);
4280 while (csum >> 16)
4281 csum = (csum >> 16) + (csum & 0xFFFF);
4282 pTcpHdr->chksum = csum;
4283 /* Compute final checksum */
4284 e1kInsertChecksum(pThis, pThis->aTxPacketFallback, pThis->u16TxPktLen,
4285 pThis->contextTSE.tu.u8CSO,
4286 pThis->contextTSE.tu.u8CSS,
4287 pThis->contextTSE.tu.u16CSE);
4288
4289 /*
4290 * Transmit it. If we've use the SG already, allocate a new one before
4291 * we copy of the data.
4292 */
4293 PPDMSCATTERGATHER pTxSg = pThisCC->CTX_SUFF(pTxSg);
4294 if (!pTxSg)
4295 {
4296 e1kXmitAllocBuf(pThis, pThisCC, pThis->u16TxPktLen + (pThis->fVTag ? 4 : 0), true /*fExactSize*/, false /*fGso*/);
4297 pTxSg = pThisCC->CTX_SUFF(pTxSg);
4298 }
4299 if (pTxSg)
4300 {
4301 Assert(pThis->u16TxPktLen <= pThisCC->CTX_SUFF(pTxSg)->cbAvailable);
4302 Assert(pTxSg->cSegs == 1);
4303 if (pThis->CCCTX_SUFF(pTxSg)->aSegs[0].pvSeg != pThis->aTxPacketFallback)
4304 memcpy(pTxSg->aSegs[0].pvSeg, pThis->aTxPacketFallback, pThis->u16TxPktLen);
4305 pTxSg->cbUsed = pThis->u16TxPktLen;
4306 pTxSg->aSegs[0].cbSeg = pThis->u16TxPktLen;
4307 }
4308 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
4309
4310 /* Update Sequence Number */
4311 pTcpHdr->seqno = htonl(ntohl(pTcpHdr->seqno) + pThis->u16TxPktLen
4312 - pThis->contextTSE.dw3.u8HDRLEN);
4313 /* Increment IP identification */
4314 pIpHdr->ident = htons(ntohs(pIpHdr->ident) + 1);
4315 }
4316}
4317#else /* E1K_WITH_TXD_CACHE */
4318static int e1kFallbackAddSegment(PPDMDEVINS pDevIns, PE1KSTATE pThis, RTGCPHYS PhysAddr, uint16_t u16Len, bool fSend, bool fOnWorkerThread)
4319{
4320 int rc = VINF_SUCCESS;
4321 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
4322 /* TCP header being transmitted */
4323 struct E1kTcpHeader *pTcpHdr = (struct E1kTcpHeader *)(pThis->aTxPacketFallback + pThis->contextTSE.tu.u8CSS);
4324 /* IP header being transmitted */
4325 struct E1kIpHeader *pIpHdr = (struct E1kIpHeader *)(pThis->aTxPacketFallback + pThis->contextTSE.ip.u8CSS);
4326
4327 E1kLog3(("%s e1kFallbackAddSegment: Length=%x, remaining payload=%x, header=%x, send=%RTbool\n",
4328 pThis->szPrf, u16Len, pThis->u32PayRemain, pThis->u16HdrRemain, fSend));
4329 AssertReturn(pThis->u32PayRemain + pThis->u16HdrRemain > 0, VINF_SUCCESS);
4330
4331 if (pThis->u16TxPktLen + u16Len <= sizeof(pThis->aTxPacketFallback))
4332 PDMDevHlpPCIPhysRead(pDevIns, PhysAddr, pThis->aTxPacketFallback + pThis->u16TxPktLen, u16Len);
4333 else
4334 E1kLog(("%s e1kFallbackAddSegment: writing beyond aTxPacketFallback, u16TxPktLen=%d(0x%x) + u16Len=%d(0x%x) > %d\n",
4335 pThis->szPrf, pThis->u16TxPktLen, pThis->u16TxPktLen, u16Len, u16Len, sizeof(pThis->aTxPacketFallback)));
4336 E1kLog3(("%s Dump of the segment:\n"
4337 "%.*Rhxd\n"
4338 "%s --- End of dump ---\n",
4339 pThis->szPrf, u16Len, pThis->aTxPacketFallback + pThis->u16TxPktLen, pThis->szPrf));
4340 pThis->u16TxPktLen += u16Len;
4341 E1kLog3(("%s e1kFallbackAddSegment: pThis->u16TxPktLen=%x\n",
4342 pThis->szPrf, pThis->u16TxPktLen));
4343 if (pThis->u16HdrRemain > 0)
4344 {
4345 /* The header was not complete, check if it is now */
4346 if (u16Len >= pThis->u16HdrRemain)
4347 {
4348 /* The rest is payload */
4349 u16Len -= pThis->u16HdrRemain;
4350 pThis->u16HdrRemain = 0;
4351 /* Save partial checksum and flags */
4352 pThis->u32SavedCsum = pTcpHdr->chksum;
4353 pThis->u16SavedFlags = pTcpHdr->hdrlen_flags;
4354 /* Clear FIN and PSH flags now and set them only in the last segment */
4355 pTcpHdr->hdrlen_flags &= ~htons(E1K_TCP_FIN | E1K_TCP_PSH);
4356 }
4357 else
4358 {
4359 /* Still not */
4360 pThis->u16HdrRemain -= u16Len;
4361 E1kLog3(("%s e1kFallbackAddSegment: Header is still incomplete, 0x%x bytes remain.\n",
4362 pThis->szPrf, pThis->u16HdrRemain));
4363 return rc;
4364 }
4365 }
4366
4367 if (u16Len > pThis->u32PayRemain)
4368 pThis->u32PayRemain = 0;
4369 else
4370 pThis->u32PayRemain -= u16Len;
4371
4372 if (fSend)
4373 {
4374 /* Leave ethernet header intact */
4375 /* IP Total Length = payload + headers - ethernet header */
4376 pIpHdr->total_len = htons(pThis->u16TxPktLen - pThis->contextTSE.ip.u8CSS);
4377 E1kLog3(("%s e1kFallbackAddSegment: End of packet, pIpHdr->total_len=%x\n",
4378 pThis->szPrf, ntohs(pIpHdr->total_len)));
4379 /* Update IP Checksum */
4380 pIpHdr->chksum = 0;
4381 e1kInsertChecksum(pThis, pThis->aTxPacketFallback, pThis->u16TxPktLen,
4382 pThis->contextTSE.ip.u8CSO,
4383 pThis->contextTSE.ip.u8CSS,
4384 pThis->contextTSE.ip.u16CSE);
4385
4386 /* Update TCP flags */
4387 /* Restore original FIN and PSH flags for the last segment */
4388 if (pThis->u32PayRemain == 0)
4389 {
4390 pTcpHdr->hdrlen_flags = pThis->u16SavedFlags;
4391 E1K_INC_CNT32(TSCTC);
4392 }
4393 /* Add TCP length to partial pseudo header sum */
4394 uint32_t csum = pThis->u32SavedCsum
4395 + htons(pThis->u16TxPktLen - pThis->contextTSE.tu.u8CSS);
4396 while (csum >> 16)
4397 csum = (csum >> 16) + (csum & 0xFFFF);
4398 Assert(csum < 65536);
4399 pTcpHdr->chksum = (uint16_t)csum;
4400 /* Compute final checksum */
4401 e1kInsertChecksum(pThis, pThis->aTxPacketFallback, pThis->u16TxPktLen,
4402 pThis->contextTSE.tu.u8CSO,
4403 pThis->contextTSE.tu.u8CSS,
4404 pThis->contextTSE.tu.u16CSE);
4405
4406 /*
4407 * Transmit it.
4408 */
4409 PPDMSCATTERGATHER pTxSg = pThisCC->CTX_SUFF(pTxSg);
4410 if (pTxSg)
4411 {
4412 /* Make sure the packet fits into the allocated buffer */
4413 size_t cbCopy = RT_MIN(pThis->u16TxPktLen, pThisCC->CTX_SUFF(pTxSg)->cbAvailable);
4414#ifdef DEBUG
4415 if (pThis->u16TxPktLen > pTxSg->cbAvailable)
4416 E1kLog(("%s e1kFallbackAddSegment: truncating packet, u16TxPktLen=%d(0x%x) > cbAvailable=%d(0x%x)\n",
4417 pThis->szPrf, pThis->u16TxPktLen, pThis->u16TxPktLen, pTxSg->cbAvailable, pTxSg->cbAvailable));
4418#endif /* DEBUG */
4419 Assert(pTxSg->cSegs == 1);
4420 if (pTxSg->aSegs[0].pvSeg != pThis->aTxPacketFallback)
4421 memcpy(pTxSg->aSegs[0].pvSeg, pThis->aTxPacketFallback, cbCopy);
4422 pTxSg->cbUsed = cbCopy;
4423 pTxSg->aSegs[0].cbSeg = cbCopy;
4424 }
4425 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
4426
4427 /* Update Sequence Number */
4428 pTcpHdr->seqno = htonl(ntohl(pTcpHdr->seqno) + pThis->u16TxPktLen
4429 - pThis->contextTSE.dw3.u8HDRLEN);
4430 /* Increment IP identification */
4431 pIpHdr->ident = htons(ntohs(pIpHdr->ident) + 1);
4432
4433 /* Allocate new buffer for the next segment. */
4434 if (pThis->u32PayRemain)
4435 {
4436 pThis->cbTxAlloc = RT_MIN(pThis->u32PayRemain,
4437 pThis->contextTSE.dw3.u16MSS)
4438 + pThis->contextTSE.dw3.u8HDRLEN;
4439 /* Do not add VLAN tags to empty packets. */
4440 if (pThis->fVTag && pThis->cbTxAlloc > 0)
4441 pThis->cbTxAlloc += 4;
4442 rc = e1kXmitAllocBuf(pThis, pThisCC, false /* fGSO */);
4443 }
4444 }
4445
4446 return rc;
4447}
4448#endif /* E1K_WITH_TXD_CACHE */
4449
4450#ifndef E1K_WITH_TXD_CACHE
4451/**
4452 * TCP segmentation offloading fallback: Add descriptor's buffer to transmit
4453 * frame.
4454 *
4455 * We construct the frame in the fallback buffer first and the copy it to the SG
4456 * buffer before passing it down to the network driver code.
4457 *
4458 * @returns true if the frame should be transmitted, false if not.
4459 *
4460 * @param pThis The device state structure.
4461 * @param pDesc Pointer to the descriptor to transmit.
4462 * @param cbFragment Length of descriptor's buffer.
4463 * @param fOnWorkerThread Whether we're on a worker thread or an EMT.
4464 * @thread E1000_TX
4465 */
4466static bool e1kFallbackAddToFrame(PE1KSTATE pThis, E1KTXDESC *pDesc, uint32_t cbFragment, bool fOnWorkerThread)
4467{
4468 PPDMSCATTERGATHER pTxSg = pThisCC->CTX_SUFF(pTxSg);
4469 Assert(e1kGetDescType(pDesc) == E1K_DTYP_DATA);
4470 Assert(pDesc->data.cmd.fTSE);
4471 Assert(!e1kXmitIsGsoBuf(pTxSg));
4472
4473 uint16_t u16MaxPktLen = pThis->contextTSE.dw3.u8HDRLEN + pThis->contextTSE.dw3.u16MSS;
4474 Assert(u16MaxPktLen != 0);
4475 Assert(u16MaxPktLen < E1K_MAX_TX_PKT_SIZE);
4476
4477 /*
4478 * Carve out segments.
4479 */
4480 do
4481 {
4482 /* Calculate how many bytes we have left in this TCP segment */
4483 uint32_t cb = u16MaxPktLen - pThis->u16TxPktLen;
4484 if (cb > cbFragment)
4485 {
4486 /* This descriptor fits completely into current segment */
4487 cb = cbFragment;
4488 e1kFallbackAddSegment(pDevIns, pThis, pDesc->data.u64BufAddr, cb, pDesc->data.cmd.fEOP /*fSend*/, fOnWorkerThread);
4489 }
4490 else
4491 {
4492 e1kFallbackAddSegment(pDevIns, pThis, pDesc->data.u64BufAddr, cb, true /*fSend*/, fOnWorkerThread);
4493 /*
4494 * Rewind the packet tail pointer to the beginning of payload,
4495 * so we continue writing right beyond the header.
4496 */
4497 pThis->u16TxPktLen = pThis->contextTSE.dw3.u8HDRLEN;
4498 }
4499
4500 pDesc->data.u64BufAddr += cb;
4501 cbFragment -= cb;
4502 } while (cbFragment > 0);
4503
4504 if (pDesc->data.cmd.fEOP)
4505 {
4506 /* End of packet, next segment will contain header. */
4507 if (pThis->u32PayRemain != 0)
4508 E1K_INC_CNT32(TSCTFC);
4509 pThis->u16TxPktLen = 0;
4510 e1kXmitFreeBuf(pThis, PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC));
4511 }
4512
4513 return false;
4514}
4515#else /* E1K_WITH_TXD_CACHE */
4516/**
4517 * TCP segmentation offloading fallback: Add descriptor's buffer to transmit
4518 * frame.
4519 *
4520 * We construct the frame in the fallback buffer first and the copy it to the SG
4521 * buffer before passing it down to the network driver code.
4522 *
4523 * @returns error code
4524 *
4525 * @param pDevIns The device instance.
4526 * @param pThis The device state structure.
4527 * @param pDesc Pointer to the descriptor to transmit.
4528 * @param cbFragment Length of descriptor's buffer.
4529 * @param fOnWorkerThread Whether we're on a worker thread or an EMT.
4530 * @thread E1000_TX
4531 */
4532static int e1kFallbackAddToFrame(PPDMDEVINS pDevIns, PE1KSTATE pThis, E1KTXDESC *pDesc, bool fOnWorkerThread)
4533{
4534#ifdef VBOX_STRICT
4535 PPDMSCATTERGATHER pTxSg = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC)->CTX_SUFF(pTxSg);
4536 Assert(e1kGetDescType(pDesc) == E1K_DTYP_DATA);
4537 Assert(pDesc->data.cmd.fTSE);
4538 Assert(!e1kXmitIsGsoBuf(pTxSg));
4539#endif
4540
4541 uint16_t u16MaxPktLen = pThis->contextTSE.dw3.u8HDRLEN + pThis->contextTSE.dw3.u16MSS;
4542 /* We cannot produce empty packets, ignore all TX descriptors (see @bugref{9571}) */
4543 if (u16MaxPktLen == 0)
4544 return VINF_SUCCESS;
4545
4546 /*
4547 * Carve out segments.
4548 */
4549 int rc = VINF_SUCCESS;
4550 do
4551 {
4552 /* Calculate how many bytes we have left in this TCP segment */
4553 uint16_t cb = u16MaxPktLen - pThis->u16TxPktLen;
4554 if (cb > pDesc->data.cmd.u20DTALEN)
4555 {
4556 /* This descriptor fits completely into current segment */
4557 cb = (uint16_t)pDesc->data.cmd.u20DTALEN; /* u20DTALEN at this point is guarantied to fit into 16 bits. */
4558 rc = e1kFallbackAddSegment(pDevIns, pThis, pDesc->data.u64BufAddr, cb, pDesc->data.cmd.fEOP /*fSend*/, fOnWorkerThread);
4559 }
4560 else
4561 {
4562 rc = e1kFallbackAddSegment(pDevIns, pThis, pDesc->data.u64BufAddr, cb, true /*fSend*/, fOnWorkerThread);
4563 /*
4564 * Rewind the packet tail pointer to the beginning of payload,
4565 * so we continue writing right beyond the header.
4566 */
4567 pThis->u16TxPktLen = pThis->contextTSE.dw3.u8HDRLEN;
4568 }
4569
4570 pDesc->data.u64BufAddr += cb;
4571 pDesc->data.cmd.u20DTALEN -= cb;
4572 } while (pDesc->data.cmd.u20DTALEN > 0 && RT_SUCCESS(rc));
4573
4574 if (pDesc->data.cmd.fEOP)
4575 {
4576 /* End of packet, next segment will contain header. */
4577 if (pThis->u32PayRemain != 0)
4578 E1K_INC_CNT32(TSCTFC);
4579 pThis->u16TxPktLen = 0;
4580 e1kXmitFreeBuf(pThis, PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC));
4581 }
4582
4583 return VINF_SUCCESS; /// @todo consider rc;
4584}
4585#endif /* E1K_WITH_TXD_CACHE */
4586
4587
4588/**
4589 * Add descriptor's buffer to transmit frame.
4590 *
4591 * This deals with GSO and normal frames, e1kFallbackAddToFrame deals with the
4592 * TSE frames we cannot handle as GSO.
4593 *
4594 * @returns true on success, false on failure.
4595 *
4596 * @param pDevIns The device instance.
4597 * @param pThisCC The current context instance data.
4598 * @param pThis The device state structure.
4599 * @param PhysAddr The physical address of the descriptor buffer.
4600 * @param cbFragment Length of descriptor's buffer.
4601 * @thread E1000_TX
4602 */
4603static bool e1kAddToFrame(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC, RTGCPHYS PhysAddr, uint32_t cbFragment)
4604{
4605 PPDMSCATTERGATHER pTxSg = pThisCC->CTX_SUFF(pTxSg);
4606 bool const fGso = e1kXmitIsGsoBuf(pTxSg);
4607 uint32_t const cbNewPkt = cbFragment + pThis->u16TxPktLen;
4608
4609 LogFlow(("%s e1kAddToFrame: ENTER cbFragment=%d u16TxPktLen=%d cbUsed=%d cbAvailable=%d fGSO=%s\n",
4610 pThis->szPrf, cbFragment, pThis->u16TxPktLen, pTxSg->cbUsed, pTxSg->cbAvailable,
4611 fGso ? "true" : "false"));
4612 PCPDMNETWORKGSO pGso = (PCPDMNETWORKGSO)pTxSg->pvUser;
4613 if (pGso)
4614 {
4615 if (RT_UNLIKELY(pGso->cbMaxSeg == 0))
4616 {
4617 E1kLog(("%s zero-sized fragments are not allowed\n", pThis->szPrf));
4618 return false;
4619 }
4620 if (RT_UNLIKELY(pGso->u8Type == PDMNETWORKGSOTYPE_IPV4_UDP))
4621 {
4622 E1kLog(("%s UDP fragmentation is no longer supported\n", pThis->szPrf));
4623 return false;
4624 }
4625 }
4626 if (RT_UNLIKELY( !fGso && cbNewPkt > E1K_MAX_TX_PKT_SIZE ))
4627 {
4628 E1kLog(("%s Transmit packet is too large: %u > %u(max)\n", pThis->szPrf, cbNewPkt, E1K_MAX_TX_PKT_SIZE));
4629 return false;
4630 }
4631 if (RT_UNLIKELY( cbNewPkt > pTxSg->cbAvailable ))
4632 {
4633 E1kLog(("%s Transmit packet is too large: %u > %u(max)\n", pThis->szPrf, cbNewPkt, pTxSg->cbAvailable));
4634 return false;
4635 }
4636
4637 if (RT_LIKELY(pTxSg))
4638 {
4639 Assert(pTxSg->cSegs == 1);
4640 if (pTxSg->cbUsed != pThis->u16TxPktLen)
4641 E1kLog(("%s e1kAddToFrame: pTxSg->cbUsed=%d(0x%x) != u16TxPktLen=%d(0x%x)\n",
4642 pThis->szPrf, pTxSg->cbUsed, pTxSg->cbUsed, pThis->u16TxPktLen, pThis->u16TxPktLen));
4643
4644 PDMDevHlpPCIPhysRead(pDevIns, PhysAddr, (uint8_t *)pTxSg->aSegs[0].pvSeg + pThis->u16TxPktLen, cbFragment);
4645
4646 pTxSg->cbUsed = cbNewPkt;
4647 }
4648 pThis->u16TxPktLen = cbNewPkt;
4649
4650 return true;
4651}
4652
4653
4654/**
4655 * Write the descriptor back to guest memory and notify the guest.
4656 *
4657 * @param pThis The device state structure.
4658 * @param pDesc Pointer to the descriptor have been transmitted.
4659 * @param addr Physical address of the descriptor in guest memory.
4660 * @thread E1000_TX
4661 */
4662static void e1kDescReport(PPDMDEVINS pDevIns, PE1KSTATE pThis, E1KTXDESC *pDesc, RTGCPHYS addr)
4663{
4664 /*
4665 * We fake descriptor write-back bursting. Descriptors are written back as they are
4666 * processed.
4667 */
4668 /* Let's pretend we process descriptors. Write back with DD set. */
4669 /*
4670 * Prior to r71586 we tried to accomodate the case when write-back bursts
4671 * are enabled without actually implementing bursting by writing back all
4672 * descriptors, even the ones that do not have RS set. This caused kernel
4673 * panics with Linux SMP kernels, as the e1000 driver tried to free up skb
4674 * associated with written back descriptor if it happened to be a context
4675 * descriptor since context descriptors do not have skb associated to them.
4676 * Starting from r71586 we write back only the descriptors with RS set,
4677 * which is a little bit different from what the real hardware does in
4678 * case there is a chain of data descritors where some of them have RS set
4679 * and others do not. It is very uncommon scenario imho.
4680 * We need to check RPS as well since some legacy drivers use it instead of
4681 * RS even with newer cards.
4682 */
4683 if (pDesc->legacy.cmd.fRS || pDesc->legacy.cmd.fRPS)
4684 {
4685 pDesc->legacy.dw3.fDD = 1; /* Descriptor Done */
4686 e1kWriteBackDesc(pDevIns, pThis, pDesc, addr);
4687 if (pDesc->legacy.cmd.fEOP)
4688 {
4689//#ifdef E1K_USE_TX_TIMERS
4690 if (pThis->fTidEnabled && pDesc->legacy.cmd.fIDE)
4691 {
4692 E1K_INC_ISTAT_CNT(pThis->uStatTxIDE);
4693 //if (pThis->fIntRaised)
4694 //{
4695 // /* Interrupt is already pending, no need for timers */
4696 // ICR |= ICR_TXDW;
4697 //}
4698 //else {
4699 /* Arm the timer to fire in TIVD usec (discard .024) */
4700 e1kArmTimer(pDevIns, pThis, pThis->hTIDTimer, TIDV);
4701# ifndef E1K_NO_TAD
4702 /* If absolute timer delay is enabled and the timer is not running yet, arm it. */
4703 E1kLog2(("%s Checking if TAD timer is running\n",
4704 pThis->szPrf));
4705 if (TADV != 0 && !PDMDevHlpTimerIsActive(pDevIns, pThis->hTADTimer))
4706 e1kArmTimer(pDevIns, pThis, pThis->hTADTimer, TADV);
4707# endif /* E1K_NO_TAD */
4708 }
4709 else
4710 {
4711 if (pThis->fTidEnabled)
4712 {
4713 E1kLog2(("%s No IDE set, cancel TAD timer and raise interrupt\n",
4714 pThis->szPrf));
4715 /* Cancel both timers if armed and fire immediately. */
4716# ifndef E1K_NO_TAD
4717 PDMDevHlpTimerStop(pDevIns, pThis->hTADTimer);
4718# endif
4719 PDMDevHlpTimerStop(pDevIns, pThis->hTIDTimer);
4720 }
4721//#endif /* E1K_USE_TX_TIMERS */
4722 E1K_INC_ISTAT_CNT(pThis->uStatIntTx);
4723 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_TXDW);
4724//#ifdef E1K_USE_TX_TIMERS
4725 }
4726//#endif /* E1K_USE_TX_TIMERS */
4727 }
4728 }
4729 else
4730 {
4731 E1K_INC_ISTAT_CNT(pThis->uStatTxNoRS);
4732 }
4733}
4734
4735#ifndef E1K_WITH_TXD_CACHE
4736
4737/**
4738 * Process Transmit Descriptor.
4739 *
4740 * E1000 supports three types of transmit descriptors:
4741 * - legacy data descriptors of older format (context-less).
4742 * - data the same as legacy but providing new offloading capabilities.
4743 * - context sets up the context for following data descriptors.
4744 *
4745 * @param pDevIns The device instance.
4746 * @param pThis The device state structure.
4747 * @param pThisCC The current context instance data.
4748 * @param pDesc Pointer to descriptor union.
4749 * @param addr Physical address of descriptor in guest memory.
4750 * @param fOnWorkerThread Whether we're on a worker thread or an EMT.
4751 * @thread E1000_TX
4752 */
4753static int e1kXmitDesc(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC, E1KTXDESC *pDesc,
4754 RTGCPHYS addr, bool fOnWorkerThread)
4755{
4756 int rc = VINF_SUCCESS;
4757 uint32_t cbVTag = 0;
4758
4759 e1kPrintTDesc(pThis, pDesc, "vvv");
4760
4761//#ifdef E1K_USE_TX_TIMERS
4762 if (pThis->fTidEnabled)
4763 e1kCancelTimer(pDevIns, pThis, pThis->hTIDTimer);
4764//#endif /* E1K_USE_TX_TIMERS */
4765
4766 switch (e1kGetDescType(pDesc))
4767 {
4768 case E1K_DTYP_CONTEXT:
4769 if (pDesc->context.dw2.fTSE)
4770 {
4771 pThis->contextTSE = pDesc->context;
4772 pThis->u32PayRemain = pDesc->context.dw2.u20PAYLEN;
4773 pThis->u16HdrRemain = pDesc->context.dw3.u8HDRLEN;
4774 e1kSetupGsoCtx(&pThis->GsoCtx, &pDesc->context);
4775 STAM_COUNTER_INC(&pThis->StatTxDescCtxTSE);
4776 }
4777 else
4778 {
4779 pThis->contextNormal = pDesc->context;
4780 STAM_COUNTER_INC(&pThis->StatTxDescCtxNormal);
4781 }
4782 E1kLog2(("%s %s context updated: IP CSS=%02X, IP CSO=%02X, IP CSE=%04X"
4783 ", TU CSS=%02X, TU CSO=%02X, TU CSE=%04X\n", pThis->szPrf,
4784 pDesc->context.dw2.fTSE ? "TSE" : "Normal",
4785 pDesc->context.ip.u8CSS,
4786 pDesc->context.ip.u8CSO,
4787 pDesc->context.ip.u16CSE,
4788 pDesc->context.tu.u8CSS,
4789 pDesc->context.tu.u8CSO,
4790 pDesc->context.tu.u16CSE));
4791 E1K_INC_ISTAT_CNT(pThis->uStatDescCtx);
4792 e1kDescReport(pThis, pDesc, addr);
4793 break;
4794
4795 case E1K_DTYP_DATA:
4796 {
4797 if (pDesc->data.cmd.u20DTALEN == 0 || pDesc->data.u64BufAddr == 0)
4798 {
4799 E1kLog2(("% Empty data descriptor, skipped.\n", pThis->szPrf));
4800 /** @todo Same as legacy when !TSE. See below. */
4801 break;
4802 }
4803 STAM_COUNTER_INC(pDesc->data.cmd.fTSE?
4804 &pThis->StatTxDescTSEData:
4805 &pThis->StatTxDescData);
4806 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatTransmit), a);
4807 E1K_INC_ISTAT_CNT(pThis->uStatDescDat);
4808
4809 /*
4810 * The last descriptor of non-TSE packet must contain VLE flag.
4811 * TSE packets have VLE flag in the first descriptor. The later
4812 * case is taken care of a bit later when cbVTag gets assigned.
4813 *
4814 * 1) pDesc->data.cmd.fEOP && !pDesc->data.cmd.fTSE
4815 */
4816 if (pDesc->data.cmd.fEOP && !pDesc->data.cmd.fTSE)
4817 {
4818 pThis->fVTag = pDesc->data.cmd.fVLE;
4819 pThis->u16VTagTCI = pDesc->data.dw3.u16Special;
4820 }
4821 /*
4822 * First fragment: Allocate new buffer and save the IXSM and TXSM
4823 * packet options as these are only valid in the first fragment.
4824 */
4825 if (pThis->u16TxPktLen == 0)
4826 {
4827 pThis->fIPcsum = pDesc->data.dw3.fIXSM;
4828 pThis->fTCPcsum = pDesc->data.dw3.fTXSM;
4829 E1kLog2(("%s Saving checksum flags:%s%s; \n", pThis->szPrf,
4830 pThis->fIPcsum ? " IP" : "",
4831 pThis->fTCPcsum ? " TCP/UDP" : ""));
4832 if (pDesc->data.cmd.fTSE)
4833 {
4834 /* 2) pDesc->data.cmd.fTSE && pThis->u16TxPktLen == 0 */
4835 pThis->fVTag = pDesc->data.cmd.fVLE;
4836 pThis->u16VTagTCI = pDesc->data.dw3.u16Special;
4837 cbVTag = pThis->fVTag ? 4 : 0;
4838 }
4839 else if (pDesc->data.cmd.fEOP)
4840 cbVTag = pDesc->data.cmd.fVLE ? 4 : 0;
4841 else
4842 cbVTag = 4;
4843 E1kLog3(("%s About to allocate TX buffer: cbVTag=%u\n", pThis->szPrf, cbVTag));
4844 if (e1kCanDoGso(pThis, &pThis->GsoCtx, &pDesc->data, &pThis->contextTSE))
4845 rc = e1kXmitAllocBuf(pThis, pThisCC, pThis->contextTSE.dw2.u20PAYLEN + pThis->contextTSE.dw3.u8HDRLEN + cbVTag,
4846 true /*fExactSize*/, true /*fGso*/);
4847 else if (pDesc->data.cmd.fTSE)
4848 rc = e1kXmitAllocBuf(pThis, pThisCC, , pThis->contextTSE.dw3.u16MSS + pThis->contextTSE.dw3.u8HDRLEN + cbVTag,
4849 pDesc->data.cmd.fTSE /*fExactSize*/, false /*fGso*/);
4850 else
4851 rc = e1kXmitAllocBuf(pThis, pThisCC, pDesc->data.cmd.u20DTALEN + cbVTag,
4852 pDesc->data.cmd.fEOP /*fExactSize*/, false /*fGso*/);
4853
4854 /**
4855 * @todo: Perhaps it is not that simple for GSO packets! We may
4856 * need to unwind some changes.
4857 */
4858 if (RT_FAILURE(rc))
4859 {
4860 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
4861 break;
4862 }
4863 /** @todo Is there any way to indicating errors other than collisions? Like
4864 * VERR_NET_DOWN. */
4865 }
4866
4867 /*
4868 * Add the descriptor data to the frame. If the frame is complete,
4869 * transmit it and reset the u16TxPktLen field.
4870 */
4871 if (e1kXmitIsGsoBuf(pThisCC->CTX_SUFF(pTxSg)))
4872 {
4873 STAM_COUNTER_INC(&pThis->StatTxPathGSO);
4874 bool fRc = e1kAddToFrame(pDevIns, pThis, pThisCC, pDesc->data.u64BufAddr, pDesc->data.cmd.u20DTALEN);
4875 if (pDesc->data.cmd.fEOP)
4876 {
4877 if ( fRc
4878 && pThisCC->CTX_SUFF(pTxSg)
4879 && pThisCC->CTX_SUFF(pTxSg)->cbUsed == (size_t)pThis->contextTSE.dw3.u8HDRLEN + pThis->contextTSE.dw2.u20PAYLEN)
4880 {
4881 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
4882 E1K_INC_CNT32(TSCTC);
4883 }
4884 else
4885 {
4886 if (fRc)
4887 E1kLog(("%s bad GSO/TSE %p or %u < %u\n" , pThis->szPrf,
4888 pThisCC->CTX_SUFF(pTxSg), pThisCC->CTX_SUFF(pTxSg) ? pThisCC->CTX_SUFF(pTxSg)->cbUsed : 0,
4889 pThis->contextTSE.dw3.u8HDRLEN + pThis->contextTSE.dw2.u20PAYLEN));
4890 e1kXmitFreeBuf(pThis);
4891 E1K_INC_CNT32(TSCTFC);
4892 }
4893 pThis->u16TxPktLen = 0;
4894 }
4895 }
4896 else if (!pDesc->data.cmd.fTSE)
4897 {
4898 STAM_COUNTER_INC(&pThis->StatTxPathRegular);
4899 bool fRc = e1kAddToFrame(pDevIns, pThis, pThisCC, pDesc->data.u64BufAddr, pDesc->data.cmd.u20DTALEN);
4900 if (pDesc->data.cmd.fEOP)
4901 {
4902 if (fRc && pThisCC->CTX_SUFF(pTxSg))
4903 {
4904 Assert(pThisCC->CTX_SUFF(pTxSg)->cSegs == 1);
4905 if (pThis->fIPcsum)
4906 e1kInsertChecksum(pThis, (uint8_t *)pThisCC->CTX_SUFF(pTxSg)->aSegs[0].pvSeg, pThis->u16TxPktLen,
4907 pThis->contextNormal.ip.u8CSO,
4908 pThis->contextNormal.ip.u8CSS,
4909 pThis->contextNormal.ip.u16CSE);
4910 if (pThis->fTCPcsum)
4911 e1kInsertChecksum(pThis, (uint8_t *)pThisCC->CTX_SUFF(pTxSg)->aSegs[0].pvSeg, pThis->u16TxPktLen,
4912 pThis->contextNormal.tu.u8CSO,
4913 pThis->contextNormal.tu.u8CSS,
4914 pThis->contextNormal.tu.u16CSE);
4915 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
4916 }
4917 else
4918 e1kXmitFreeBuf(pThis);
4919 pThis->u16TxPktLen = 0;
4920 }
4921 }
4922 else
4923 {
4924 STAM_COUNTER_INC(&pThis->StatTxPathFallback);
4925 e1kFallbackAddToFrame(pDevIns, pThis, pDesc, pDesc->data.cmd.u20DTALEN, fOnWorkerThread);
4926 }
4927
4928 e1kDescReport(pThis, pDesc, addr);
4929 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
4930 break;
4931 }
4932
4933 case E1K_DTYP_LEGACY:
4934 if (pDesc->legacy.cmd.u16Length == 0 || pDesc->legacy.u64BufAddr == 0)
4935 {
4936 E1kLog(("%s Empty legacy descriptor, skipped.\n", pThis->szPrf));
4937 /** @todo 3.3.3, Length/Buffer Address: RS set -> write DD when processing. */
4938 break;
4939 }
4940 STAM_COUNTER_INC(&pThis->StatTxDescLegacy);
4941 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatTransmit), a);
4942
4943 /* First fragment: allocate new buffer. */
4944 if (pThis->u16TxPktLen == 0)
4945 {
4946 if (pDesc->legacy.cmd.fEOP)
4947 cbVTag = pDesc->legacy.cmd.fVLE ? 4 : 0;
4948 else
4949 cbVTag = 4;
4950 E1kLog3(("%s About to allocate TX buffer: cbVTag=%u\n", pThis->szPrf, cbVTag));
4951 /** @todo reset status bits? */
4952 rc = e1kXmitAllocBuf(pThis, pThisCC, pDesc->legacy.cmd.u16Length + cbVTag, pDesc->legacy.cmd.fEOP, false /*fGso*/);
4953 if (RT_FAILURE(rc))
4954 {
4955 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
4956 break;
4957 }
4958
4959 /** @todo Is there any way to indicating errors other than collisions? Like
4960 * VERR_NET_DOWN. */
4961 }
4962
4963 /* Add fragment to frame. */
4964 if (e1kAddToFrame(pDevIns, pThis, pThisCC, pDesc->data.u64BufAddr, pDesc->legacy.cmd.u16Length))
4965 {
4966 E1K_INC_ISTAT_CNT(pThis->uStatDescLeg);
4967
4968 /* Last fragment: Transmit and reset the packet storage counter. */
4969 if (pDesc->legacy.cmd.fEOP)
4970 {
4971 pThis->fVTag = pDesc->legacy.cmd.fVLE;
4972 pThis->u16VTagTCI = pDesc->legacy.dw3.u16Special;
4973 /** @todo Offload processing goes here. */
4974 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
4975 pThis->u16TxPktLen = 0;
4976 }
4977 }
4978 /* Last fragment + failure: free the buffer and reset the storage counter. */
4979 else if (pDesc->legacy.cmd.fEOP)
4980 {
4981 e1kXmitFreeBuf(pThis);
4982 pThis->u16TxPktLen = 0;
4983 }
4984
4985 e1kDescReport(pThis, pDesc, addr);
4986 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
4987 break;
4988
4989 default:
4990 E1kLog(("%s ERROR Unsupported transmit descriptor type: 0x%04x\n",
4991 pThis->szPrf, e1kGetDescType(pDesc)));
4992 break;
4993 }
4994
4995 return rc;
4996}
4997
4998#else /* E1K_WITH_TXD_CACHE */
4999
5000/**
5001 * Process Transmit Descriptor.
5002 *
5003 * E1000 supports three types of transmit descriptors:
5004 * - legacy data descriptors of older format (context-less).
5005 * - data the same as legacy but providing new offloading capabilities.
5006 * - context sets up the context for following data descriptors.
5007 *
5008 * @param pDevIns The device instance.
5009 * @param pThis The device state structure.
5010 * @param pThisCC The current context instance data.
5011 * @param pDesc Pointer to descriptor union.
5012 * @param addr Physical address of descriptor in guest memory.
5013 * @param fOnWorkerThread Whether we're on a worker thread or an EMT.
5014 * @param cbPacketSize Size of the packet as previously computed.
5015 * @thread E1000_TX
5016 */
5017static int e1kXmitDesc(PPDMDEVINS pDevIns, PE1KSTATE pThis, PE1KSTATECC pThisCC, E1KTXDESC *pDesc,
5018 RTGCPHYS addr, bool fOnWorkerThread)
5019{
5020 int rc = VINF_SUCCESS;
5021
5022 e1kPrintTDesc(pThis, pDesc, "vvv");
5023
5024 if (pDesc->legacy.dw3.fDD)
5025 {
5026 E1kLog(("%s e1kXmitDesc: skipping bad descriptor ^^^\n", pThis->szPrf));
5027 e1kDescReport(pDevIns, pThis, pDesc, addr);
5028 return VINF_SUCCESS;
5029 }
5030
5031//#ifdef E1K_USE_TX_TIMERS
5032 if (pThis->fTidEnabled)
5033 PDMDevHlpTimerStop(pDevIns, pThis->hTIDTimer);
5034//#endif /* E1K_USE_TX_TIMERS */
5035
5036 switch (e1kGetDescType(pDesc))
5037 {
5038 case E1K_DTYP_CONTEXT:
5039 /* The caller have already updated the context */
5040 E1K_INC_ISTAT_CNT(pThis->uStatDescCtx);
5041 e1kDescReport(pDevIns, pThis, pDesc, addr);
5042 break;
5043
5044 case E1K_DTYP_DATA:
5045 {
5046 STAM_COUNTER_INC(pDesc->data.cmd.fTSE?
5047 &pThis->StatTxDescTSEData:
5048 &pThis->StatTxDescData);
5049 E1K_INC_ISTAT_CNT(pThis->uStatDescDat);
5050 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatTransmit), a);
5051 if (pDesc->data.cmd.u20DTALEN == 0 || pDesc->data.u64BufAddr == 0)
5052 {
5053 E1kLog2(("% Empty data descriptor, skipped.\n", pThis->szPrf));
5054 if (pDesc->data.cmd.fEOP)
5055 {
5056 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
5057 pThis->u16TxPktLen = 0;
5058 }
5059 }
5060 else
5061 {
5062 /*
5063 * Add the descriptor data to the frame. If the frame is complete,
5064 * transmit it and reset the u16TxPktLen field.
5065 */
5066 if (e1kXmitIsGsoBuf(pThisCC->CTX_SUFF(pTxSg)))
5067 {
5068 STAM_COUNTER_INC(&pThis->StatTxPathGSO);
5069 bool fRc = e1kAddToFrame(pDevIns, pThis, pThisCC, pDesc->data.u64BufAddr, pDesc->data.cmd.u20DTALEN);
5070 if (pDesc->data.cmd.fEOP)
5071 {
5072 if ( fRc
5073 && pThisCC->CTX_SUFF(pTxSg)
5074 && pThisCC->CTX_SUFF(pTxSg)->cbUsed == (size_t)pThis->contextTSE.dw3.u8HDRLEN + pThis->contextTSE.dw2.u20PAYLEN)
5075 {
5076 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
5077 E1K_INC_CNT32(TSCTC);
5078 }
5079 else
5080 {
5081 if (fRc)
5082 E1kLog(("%s bad GSO/TSE %p or %u < %u\n" , pThis->szPrf,
5083 pThisCC->CTX_SUFF(pTxSg), pThisCC->CTX_SUFF(pTxSg) ? pThisCC->CTX_SUFF(pTxSg)->cbUsed : 0,
5084 pThis->contextTSE.dw3.u8HDRLEN + pThis->contextTSE.dw2.u20PAYLEN));
5085 e1kXmitFreeBuf(pThis, pThisCC);
5086 E1K_INC_CNT32(TSCTFC);
5087 }
5088 pThis->u16TxPktLen = 0;
5089 }
5090 }
5091 else if (!pDesc->data.cmd.fTSE)
5092 {
5093 STAM_COUNTER_INC(&pThis->StatTxPathRegular);
5094 bool fRc = e1kAddToFrame(pDevIns, pThis, pThisCC, pDesc->data.u64BufAddr, pDesc->data.cmd.u20DTALEN);
5095 if (pDesc->data.cmd.fEOP)
5096 {
5097 if (fRc && pThisCC->CTX_SUFF(pTxSg))
5098 {
5099 Assert(pThisCC->CTX_SUFF(pTxSg)->cSegs == 1);
5100 if (pThis->fIPcsum)
5101 e1kInsertChecksum(pThis, (uint8_t *)pThisCC->CTX_SUFF(pTxSg)->aSegs[0].pvSeg, pThis->u16TxPktLen,
5102 pThis->contextNormal.ip.u8CSO,
5103 pThis->contextNormal.ip.u8CSS,
5104 pThis->contextNormal.ip.u16CSE);
5105 if (pThis->fTCPcsum)
5106 e1kInsertChecksum(pThis, (uint8_t *)pThisCC->CTX_SUFF(pTxSg)->aSegs[0].pvSeg, pThis->u16TxPktLen,
5107 pThis->contextNormal.tu.u8CSO,
5108 pThis->contextNormal.tu.u8CSS,
5109 pThis->contextNormal.tu.u16CSE);
5110 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
5111 }
5112 else
5113 e1kXmitFreeBuf(pThis, pThisCC);
5114 pThis->u16TxPktLen = 0;
5115 }
5116 }
5117 else
5118 {
5119 STAM_COUNTER_INC(&pThis->StatTxPathFallback);
5120 rc = e1kFallbackAddToFrame(pDevIns, pThis, pDesc, fOnWorkerThread);
5121 }
5122 }
5123 e1kDescReport(pDevIns, pThis, pDesc, addr);
5124 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
5125 break;
5126 }
5127
5128 case E1K_DTYP_LEGACY:
5129 STAM_COUNTER_INC(&pThis->StatTxDescLegacy);
5130 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatTransmit), a);
5131 if (pDesc->legacy.cmd.u16Length == 0 || pDesc->legacy.u64BufAddr == 0)
5132 {
5133 E1kLog(("%s Empty legacy descriptor, skipped.\n", pThis->szPrf));
5134 }
5135 else
5136 {
5137 /* Add fragment to frame. */
5138 if (e1kAddToFrame(pDevIns, pThis, pThisCC, pDesc->data.u64BufAddr, pDesc->legacy.cmd.u16Length))
5139 {
5140 E1K_INC_ISTAT_CNT(pThis->uStatDescLeg);
5141
5142 /* Last fragment: Transmit and reset the packet storage counter. */
5143 if (pDesc->legacy.cmd.fEOP)
5144 {
5145 if (pDesc->legacy.cmd.fIC)
5146 {
5147 e1kInsertChecksum(pThis,
5148 (uint8_t *)pThisCC->CTX_SUFF(pTxSg)->aSegs[0].pvSeg,
5149 pThis->u16TxPktLen,
5150 pDesc->legacy.cmd.u8CSO,
5151 pDesc->legacy.dw3.u8CSS,
5152 0);
5153 }
5154 e1kTransmitFrame(pDevIns, pThis, pThisCC, fOnWorkerThread);
5155 pThis->u16TxPktLen = 0;
5156 }
5157 }
5158 /* Last fragment + failure: free the buffer and reset the storage counter. */
5159 else if (pDesc->legacy.cmd.fEOP)
5160 {
5161 e1kXmitFreeBuf(pThis, pThisCC);
5162 pThis->u16TxPktLen = 0;
5163 }
5164 }
5165 e1kDescReport(pDevIns, pThis, pDesc, addr);
5166 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
5167 break;
5168
5169 default:
5170 E1kLog(("%s ERROR Unsupported transmit descriptor type: 0x%04x\n",
5171 pThis->szPrf, e1kGetDescType(pDesc)));
5172 break;
5173 }
5174
5175 return rc;
5176}
5177
5178DECLINLINE(void) e1kUpdateTxContext(PE1KSTATE pThis, E1KTXDESC *pDesc)
5179{
5180 if (pDesc->context.dw2.fTSE)
5181 {
5182 pThis->contextTSE = pDesc->context;
5183 uint32_t cbMaxSegmentSize = pThis->contextTSE.dw3.u16MSS + pThis->contextTSE.dw3.u8HDRLEN + 4; /*VTAG*/
5184 if (RT_UNLIKELY(cbMaxSegmentSize > E1K_MAX_TX_PKT_SIZE))
5185 {
5186 pThis->contextTSE.dw3.u16MSS = E1K_MAX_TX_PKT_SIZE - pThis->contextTSE.dw3.u8HDRLEN - 4; /*VTAG*/
5187 LogRelMax(10, ("%s: Transmit packet is too large: %u > %u(max). Adjusted MSS to %u.\n",
5188 pThis->szPrf, cbMaxSegmentSize, E1K_MAX_TX_PKT_SIZE, pThis->contextTSE.dw3.u16MSS));
5189 }
5190 pThis->u32PayRemain = pThis->contextTSE.dw2.u20PAYLEN;
5191 pThis->u16HdrRemain = pThis->contextTSE.dw3.u8HDRLEN;
5192 e1kSetupGsoCtx(&pThis->GsoCtx, &pThis->contextTSE);
5193 STAM_COUNTER_INC(&pThis->StatTxDescCtxTSE);
5194 }
5195 else
5196 {
5197 pThis->contextNormal = pDesc->context;
5198 STAM_COUNTER_INC(&pThis->StatTxDescCtxNormal);
5199 }
5200 E1kLog2(("%s %s context updated: IP CSS=%02X, IP CSO=%02X, IP CSE=%04X"
5201 ", TU CSS=%02X, TU CSO=%02X, TU CSE=%04X\n", pThis->szPrf,
5202 pDesc->context.dw2.fTSE ? "TSE" : "Normal",
5203 pDesc->context.ip.u8CSS,
5204 pDesc->context.ip.u8CSO,
5205 pDesc->context.ip.u16CSE,
5206 pDesc->context.tu.u8CSS,
5207 pDesc->context.tu.u8CSO,
5208 pDesc->context.tu.u16CSE));
5209}
5210
5211static bool e1kLocateTxPacket(PE1KSTATE pThis)
5212{
5213 LogFlow(("%s e1kLocateTxPacket: ENTER cbTxAlloc=%d\n",
5214 pThis->szPrf, pThis->cbTxAlloc));
5215 /* Check if we have located the packet already. */
5216 if (pThis->cbTxAlloc)
5217 {
5218 LogFlow(("%s e1kLocateTxPacket: RET true cbTxAlloc=%d\n",
5219 pThis->szPrf, pThis->cbTxAlloc));
5220 return true;
5221 }
5222
5223 bool fTSE = false;
5224 uint32_t cbPacket = 0;
5225
5226 for (int i = pThis->iTxDCurrent; i < pThis->nTxDFetched; ++i)
5227 {
5228 E1KTXDESC *pDesc = &pThis->aTxDescriptors[i];
5229 switch (e1kGetDescType(pDesc))
5230 {
5231 case E1K_DTYP_CONTEXT:
5232 if (cbPacket == 0)
5233 e1kUpdateTxContext(pThis, pDesc);
5234 else
5235 E1kLog(("%s e1kLocateTxPacket: ignoring a context descriptor in the middle of a packet, cbPacket=%d\n",
5236 pThis->szPrf, cbPacket));
5237 continue;
5238 case E1K_DTYP_LEGACY:
5239 /* Skip invalid descriptors. */
5240 if (cbPacket > 0 && (pThis->fGSO || fTSE))
5241 {
5242 E1kLog(("%s e1kLocateTxPacket: ignoring a legacy descriptor in the segmentation context, cbPacket=%d\n",
5243 pThis->szPrf, cbPacket));
5244 pDesc->legacy.dw3.fDD = true; /* Make sure it is skipped by processing */
5245 continue;
5246 }
5247 /* Skip empty descriptors. */
5248 if (!pDesc->legacy.u64BufAddr || !pDesc->legacy.cmd.u16Length)
5249 break;
5250 cbPacket += pDesc->legacy.cmd.u16Length;
5251 pThis->fGSO = false;
5252 break;
5253 case E1K_DTYP_DATA:
5254 /* Skip invalid descriptors. */
5255 if (cbPacket > 0 && (bool)pDesc->data.cmd.fTSE != fTSE)
5256 {
5257 E1kLog(("%s e1kLocateTxPacket: ignoring %sTSE descriptor in the %ssegmentation context, cbPacket=%d\n",
5258 pThis->szPrf, pDesc->data.cmd.fTSE ? "" : "non-", fTSE ? "" : "non-", cbPacket));
5259 pDesc->data.dw3.fDD = true; /* Make sure it is skipped by processing */
5260 continue;
5261 }
5262 /* Skip empty descriptors. */
5263 if (!pDesc->data.u64BufAddr || !pDesc->data.cmd.u20DTALEN)
5264 break;
5265 if (cbPacket == 0)
5266 {
5267 /*
5268 * The first fragment: save IXSM and TXSM options
5269 * as these are only valid in the first fragment.
5270 */
5271 pThis->fIPcsum = pDesc->data.dw3.fIXSM;
5272 pThis->fTCPcsum = pDesc->data.dw3.fTXSM;
5273 fTSE = pDesc->data.cmd.fTSE;
5274 /*
5275 * TSE descriptors have VLE bit properly set in
5276 * the first fragment.
5277 */
5278 if (fTSE)
5279 {
5280 pThis->fVTag = pDesc->data.cmd.fVLE;
5281 pThis->u16VTagTCI = pDesc->data.dw3.u16Special;
5282 }
5283 pThis->fGSO = e1kCanDoGso(pThis, &pThis->GsoCtx, &pDesc->data, &pThis->contextTSE);
5284 }
5285 cbPacket += pDesc->data.cmd.u20DTALEN;
5286 break;
5287 default:
5288 AssertMsgFailed(("Impossible descriptor type!"));
5289 continue;
5290 }
5291 if (pDesc->legacy.cmd.fEOP)
5292 {
5293 /*
5294 * Non-TSE descriptors have VLE bit properly set in
5295 * the last fragment.
5296 */
5297 if (!fTSE)
5298 {
5299 pThis->fVTag = pDesc->data.cmd.fVLE;
5300 pThis->u16VTagTCI = pDesc->data.dw3.u16Special;
5301 }
5302 /*
5303 * Compute the required buffer size. If we cannot do GSO but still
5304 * have to do segmentation we allocate the first segment only.
5305 */
5306 pThis->cbTxAlloc = (!fTSE || pThis->fGSO) ?
5307 cbPacket :
5308 RT_MIN(cbPacket, pThis->contextTSE.dw3.u16MSS + pThis->contextTSE.dw3.u8HDRLEN);
5309 /* Do not add VLAN tags to empty packets. */
5310 if (pThis->fVTag && pThis->cbTxAlloc > 0)
5311 pThis->cbTxAlloc += 4;
5312 LogFlow(("%s e1kLocateTxPacket: RET true cbTxAlloc=%d cbPacket=%d%s%s\n",
5313 pThis->szPrf, pThis->cbTxAlloc, cbPacket,
5314 pThis->fGSO ? " GSO" : "", fTSE ? " TSE" : ""));
5315 return true;
5316 }
5317 }
5318
5319 if (cbPacket == 0 && pThis->nTxDFetched - pThis->iTxDCurrent > 0)
5320 {
5321 /* All descriptors were empty, we need to process them as a dummy packet */
5322 LogFlow(("%s e1kLocateTxPacket: RET true cbTxAlloc=%d, zero packet!\n",
5323 pThis->szPrf, pThis->cbTxAlloc));
5324 return true;
5325 }
5326 LogFlow(("%s e1kLocateTxPacket: RET false cbTxAlloc=%d cbPacket=%d\n",
5327 pThis->szPrf, pThis->cbTxAlloc, cbPacket));
5328 return false;
5329}
5330
5331static int e1kXmitPacket(PPDMDEVINS pDevIns, PE1KSTATE pThis, bool fOnWorkerThread)
5332{
5333 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
5334 int rc = VINF_SUCCESS;
5335
5336 LogFlow(("%s e1kXmitPacket: ENTER current=%d fetched=%d\n",
5337 pThis->szPrf, pThis->iTxDCurrent, pThis->nTxDFetched));
5338
5339 while (pThis->iTxDCurrent < pThis->nTxDFetched)
5340 {
5341 E1KTXDESC *pDesc = &pThis->aTxDescriptors[pThis->iTxDCurrent];
5342 E1kLog3(("%s About to process new TX descriptor at %08x%08x, TDLEN=%08x, TDH=%08x, TDT=%08x\n",
5343 pThis->szPrf, TDBAH, TDBAL + TDH * sizeof(E1KTXDESC), TDLEN, TDH, TDT));
5344 rc = e1kXmitDesc(pDevIns, pThis, pThisCC, pDesc, e1kDescAddr(TDBAH, TDBAL, TDH), fOnWorkerThread);
5345 if (RT_FAILURE(rc))
5346 break;
5347 if (++TDH * sizeof(E1KTXDESC) >= TDLEN)
5348 TDH = 0;
5349 uint32_t uLowThreshold = GET_BITS(TXDCTL, LWTHRESH)*8;
5350 if (uLowThreshold != 0 && e1kGetTxLen(pThis) <= uLowThreshold)
5351 {
5352 E1kLog2(("%s Low on transmit descriptors, raise ICR.TXD_LOW, len=%x thresh=%x\n",
5353 pThis->szPrf, e1kGetTxLen(pThis), GET_BITS(TXDCTL, LWTHRESH)*8));
5354 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_TXD_LOW);
5355 }
5356 ++pThis->iTxDCurrent;
5357 if (e1kGetDescType(pDesc) != E1K_DTYP_CONTEXT && pDesc->legacy.cmd.fEOP)
5358 break;
5359 }
5360
5361 LogFlow(("%s e1kXmitPacket: RET %Rrc current=%d fetched=%d\n",
5362 pThis->szPrf, rc, pThis->iTxDCurrent, pThis->nTxDFetched));
5363 return rc;
5364}
5365
5366#endif /* E1K_WITH_TXD_CACHE */
5367#ifndef E1K_WITH_TXD_CACHE
5368
5369/**
5370 * Transmit pending descriptors.
5371 *
5372 * @returns VBox status code. VERR_TRY_AGAIN is returned if we're busy.
5373 *
5374 * @param pDevIns The device instance.
5375 * @param pThis The E1000 state.
5376 * @param fOnWorkerThread Whether we're on a worker thread or on an EMT.
5377 */
5378static int e1kXmitPending(PPDMDEVINS pDevIns, PE1KSTATE pThis, bool fOnWorkerThread)
5379{
5380 int rc = VINF_SUCCESS;
5381 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
5382
5383 /* Check if transmitter is enabled. */
5384 if (!(TCTL & TCTL_EN))
5385 return VINF_SUCCESS;
5386 /*
5387 * Grab the xmit lock of the driver as well as the E1K device state.
5388 */
5389 rc = e1kCsTxEnter(pThis, VERR_SEM_BUSY);
5390 if (RT_LIKELY(rc == VINF_SUCCESS))
5391 {
5392 PPDMINETWORKUP pDrv = pThis->CTX_SUFF(pDrv);
5393 if (pDrv)
5394 {
5395 rc = pDrv->pfnBeginXmit(pDrv, fOnWorkerThread);
5396 if (RT_FAILURE(rc))
5397 {
5398 e1kCsTxLeave(pThis);
5399 return rc;
5400 }
5401 }
5402 /*
5403 * Process all pending descriptors.
5404 * Note! Do not process descriptors in locked state
5405 */
5406 while (TDH != TDT && !pThis->fLocked)
5407 {
5408 E1KTXDESC desc;
5409 E1kLog3(("%s About to process new TX descriptor at %08x%08x, TDLEN=%08x, TDH=%08x, TDT=%08x\n",
5410 pThis->szPrf, TDBAH, TDBAL + TDH * sizeof(desc), TDLEN, TDH, TDT));
5411
5412 e1kLoadDesc(pDevIns, &desc, ((uint64_t)TDBAH << 32) + TDBAL + TDH * sizeof(desc));
5413 rc = e1kXmitDesc(pDevIns, pThis, pThisCC, &desc, e1kDescAddr(TDBAH, TDBAL, TDH), fOnWorkerThread);
5414 /* If we failed to transmit descriptor we will try it again later */
5415 if (RT_FAILURE(rc))
5416 break;
5417 if (++TDH * sizeof(desc) >= TDLEN)
5418 TDH = 0;
5419
5420 if (e1kGetTxLen(pThis) <= GET_BITS(TXDCTL, LWTHRESH)*8)
5421 {
5422 E1kLog2(("%s Low on transmit descriptors, raise ICR.TXD_LOW, len=%x thresh=%x\n",
5423 pThis->szPrf, e1kGetTxLen(pThis), GET_BITS(TXDCTL, LWTHRESH)*8));
5424 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_TXD_LOW);
5425 }
5426
5427 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
5428 }
5429
5430 /// @todo uncomment: pThis->uStatIntTXQE++;
5431 /// @todo uncomment: e1kRaiseInterrupt(pDevIns, pThis, ICR_TXQE);
5432 /*
5433 * Release the lock.
5434 */
5435 if (pDrv)
5436 pDrv->pfnEndXmit(pDrv);
5437 e1kCsTxLeave(pThis);
5438 }
5439
5440 return rc;
5441}
5442
5443#else /* E1K_WITH_TXD_CACHE */
5444
5445static void e1kDumpTxDCache(PPDMDEVINS pDevIns, PE1KSTATE pThis)
5446{
5447 unsigned i, cDescs = TDLEN / sizeof(E1KTXDESC);
5448 uint32_t tdh = TDH;
5449 LogRel(("E1000: -- Transmit Descriptors (%d total) --\n", cDescs));
5450 for (i = 0; i < cDescs; ++i)
5451 {
5452 E1KTXDESC desc;
5453 PDMDevHlpPCIPhysRead(pDevIns , e1kDescAddr(TDBAH, TDBAL, i), &desc, sizeof(desc));
5454 if (i == tdh)
5455 LogRel(("E1000: >>> "));
5456 LogRel(("E1000: %RGp: %R[e1ktxd]\n", e1kDescAddr(TDBAH, TDBAL, i), &desc));
5457 }
5458 LogRel(("E1000: -- Transmit Descriptors in Cache (at %d (TDH %d)/ fetched %d / max %d) --\n",
5459 pThis->iTxDCurrent, TDH, pThis->nTxDFetched, E1K_TXD_CACHE_SIZE));
5460 if (tdh > pThis->iTxDCurrent)
5461 tdh -= pThis->iTxDCurrent;
5462 else
5463 tdh = cDescs + tdh - pThis->iTxDCurrent;
5464 for (i = 0; i < pThis->nTxDFetched; ++i)
5465 {
5466 if (i == pThis->iTxDCurrent)
5467 LogRel(("E1000: >>> "));
5468 if (cDescs)
5469 LogRel(("E1000: %RGp: %R[e1ktxd]\n", e1kDescAddr(TDBAH, TDBAL, tdh++ % cDescs), &pThis->aTxDescriptors[i]));
5470 else
5471 LogRel(("E1000: <lost>: %R[e1ktxd]\n", &pThis->aTxDescriptors[i]));
5472 }
5473}
5474
5475/**
5476 * Transmit pending descriptors.
5477 *
5478 * @returns VBox status code. VERR_TRY_AGAIN is returned if we're busy.
5479 *
5480 * @param pDevIns The device instance.
5481 * @param pThis The E1000 state.
5482 * @param fOnWorkerThread Whether we're on a worker thread or on an EMT.
5483 */
5484static int e1kXmitPending(PPDMDEVINS pDevIns, PE1KSTATE pThis, bool fOnWorkerThread)
5485{
5486 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
5487 int rc = VINF_SUCCESS;
5488
5489 /* Check if transmitter is enabled. */
5490 if (!(TCTL & TCTL_EN))
5491 return VINF_SUCCESS;
5492 /*
5493 * Grab the xmit lock of the driver as well as the E1K device state.
5494 */
5495 PPDMINETWORKUP pDrv = pThisCC->CTX_SUFF(pDrv);
5496 if (pDrv)
5497 {
5498 rc = pDrv->pfnBeginXmit(pDrv, fOnWorkerThread);
5499 if (RT_FAILURE(rc))
5500 return rc;
5501 }
5502
5503 /*
5504 * Process all pending descriptors.
5505 * Note! Do not process descriptors in locked state
5506 */
5507 rc = e1kCsTxEnter(pThis, VERR_SEM_BUSY);
5508 if (RT_LIKELY(rc == VINF_SUCCESS))
5509 {
5510 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatTransmit), a);
5511 /*
5512 * fIncomplete is set whenever we try to fetch additional descriptors
5513 * for an incomplete packet. If fail to locate a complete packet on
5514 * the next iteration we need to reset the cache or we risk to get
5515 * stuck in this loop forever.
5516 */
5517 bool fIncomplete = false;
5518 while (!pThis->fLocked && e1kTxDLazyLoad(pDevIns, pThis))
5519 {
5520 while (e1kLocateTxPacket(pThis))
5521 {
5522 fIncomplete = false;
5523 /* Found a complete packet, allocate it. */
5524 rc = e1kXmitAllocBuf(pThis, pThisCC, pThis->fGSO);
5525 /* If we're out of bandwidth we'll come back later. */
5526 if (RT_FAILURE(rc))
5527 goto out;
5528 /* Copy the packet to allocated buffer and send it. */
5529 rc = e1kXmitPacket(pDevIns, pThis, fOnWorkerThread);
5530 /* If we're out of bandwidth we'll come back later. */
5531 if (RT_FAILURE(rc))
5532 goto out;
5533 }
5534 uint8_t u8Remain = pThis->nTxDFetched - pThis->iTxDCurrent;
5535 if (RT_UNLIKELY(fIncomplete))
5536 {
5537 static bool fTxDCacheDumped = false;
5538 /*
5539 * The descriptor cache is full, but we were unable to find
5540 * a complete packet in it. Drop the cache and hope that
5541 * the guest driver can recover from network card error.
5542 */
5543 LogRel(("%s: No complete packets in%s TxD cache! "
5544 "Fetched=%d, current=%d, TX len=%d.\n",
5545 pThis->szPrf,
5546 u8Remain == E1K_TXD_CACHE_SIZE ? " full" : "",
5547 pThis->nTxDFetched, pThis->iTxDCurrent,
5548 e1kGetTxLen(pThis)));
5549 if (!fTxDCacheDumped)
5550 {
5551 fTxDCacheDumped = true;
5552 e1kDumpTxDCache(pDevIns, pThis);
5553 }
5554 pThis->iTxDCurrent = pThis->nTxDFetched = 0;
5555 /*
5556 * Returning an error at this point means Guru in R0
5557 * (see @bugref{6428}).
5558 */
5559# ifdef IN_RING3
5560 rc = VERR_NET_INCOMPLETE_TX_PACKET;
5561# else /* !IN_RING3 */
5562 rc = VINF_IOM_R3_MMIO_WRITE;
5563# endif /* !IN_RING3 */
5564 goto out;
5565 }
5566 if (u8Remain > 0)
5567 {
5568 Log4(("%s Incomplete packet at %d. Already fetched %d, "
5569 "%d more are available\n",
5570 pThis->szPrf, pThis->iTxDCurrent, u8Remain,
5571 e1kGetTxLen(pThis) - u8Remain));
5572
5573 /*
5574 * A packet was partially fetched. Move incomplete packet to
5575 * the beginning of cache buffer, then load more descriptors.
5576 */
5577 memmove(pThis->aTxDescriptors,
5578 &pThis->aTxDescriptors[pThis->iTxDCurrent],
5579 u8Remain * sizeof(E1KTXDESC));
5580 pThis->iTxDCurrent = 0;
5581 pThis->nTxDFetched = u8Remain;
5582 e1kTxDLoadMore(pDevIns, pThis);
5583 fIncomplete = true;
5584 }
5585 else
5586 pThis->nTxDFetched = 0;
5587 pThis->iTxDCurrent = 0;
5588 }
5589 if (!pThis->fLocked && GET_BITS(TXDCTL, LWTHRESH) == 0)
5590 {
5591 E1kLog2(("%s Out of transmit descriptors, raise ICR.TXD_LOW\n",
5592 pThis->szPrf));
5593 e1kRaiseInterrupt(pDevIns, pThis, VERR_SEM_BUSY, ICR_TXD_LOW);
5594 }
5595out:
5596 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatTransmit), a);
5597
5598 /// @todo uncomment: pThis->uStatIntTXQE++;
5599 /// @todo uncomment: e1kRaiseInterrupt(pDevIns, pThis, ICR_TXQE);
5600
5601 e1kCsTxLeave(pThis);
5602 }
5603
5604
5605 /*
5606 * Release the lock.
5607 */
5608 if (pDrv)
5609 pDrv->pfnEndXmit(pDrv);
5610 return rc;
5611}
5612
5613#endif /* E1K_WITH_TXD_CACHE */
5614#ifdef IN_RING3
5615
5616/**
5617 * @interface_method_impl{PDMINETWORKDOWN,pfnXmitPending}
5618 */
5619static DECLCALLBACK(void) e1kR3NetworkDown_XmitPending(PPDMINETWORKDOWN pInterface)
5620{
5621 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, INetworkDown);
5622 PE1KSTATE pThis = pThisCC->pShared;
5623 /* Resume suspended transmission */
5624 STATUS &= ~STATUS_TXOFF;
5625 e1kXmitPending(pThisCC->pDevInsR3, pThis, true /*fOnWorkerThread*/);
5626}
5627
5628/**
5629 * @callback_method_impl{FNPDMTASKDEV,
5630 * Executes e1kXmitPending at the behest of ring-0/raw-mode.}
5631 * @note Not executed on EMT.
5632 */
5633static DECLCALLBACK(void) e1kR3TxTaskCallback(PPDMDEVINS pDevIns, void *pvUser)
5634{
5635 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
5636 E1kLog2(("%s e1kR3TxTaskCallback:\n", pThis->szPrf));
5637
5638 int rc = e1kXmitPending(pDevIns, pThis, false /*fOnWorkerThread*/);
5639 AssertMsg(RT_SUCCESS(rc) || rc == VERR_TRY_AGAIN || rc == VERR_NET_DOWN, ("%Rrc\n", rc));
5640
5641 RT_NOREF(rc, pvUser);
5642}
5643
5644#endif /* IN_RING3 */
5645
5646/**
5647 * Write handler for Transmit Descriptor Tail register.
5648 *
5649 * @param pThis The device state structure.
5650 * @param offset Register offset in memory-mapped frame.
5651 * @param index Register index in register array.
5652 * @param value The value to store.
5653 * @param mask Used to implement partial writes (8 and 16-bit).
5654 * @thread EMT
5655 */
5656static int e1kRegWriteTDT(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
5657{
5658 int rc = e1kRegWriteDefault(pDevIns, pThis, offset, index, value);
5659
5660 /* All descriptors starting with head and not including tail belong to us. */
5661 /* Process them. */
5662 E1kLog2(("%s e1kRegWriteTDT: TDBAL=%08x, TDBAH=%08x, TDLEN=%08x, TDH=%08x, TDT=%08x\n",
5663 pThis->szPrf, TDBAL, TDBAH, TDLEN, TDH, TDT));
5664
5665 /* Ignore TDT writes when the link is down. */
5666 if (TDH != TDT && (STATUS & STATUS_LU))
5667 {
5668 Log5(("E1000: TDT write: TDH=%08x, TDT=%08x, %d descriptors to process\n", TDH, TDT, e1kGetTxLen(pThis)));
5669 E1kLog(("%s e1kRegWriteTDT: %d descriptors to process\n",
5670 pThis->szPrf, e1kGetTxLen(pThis)));
5671
5672 /* Transmit pending packets if possible, defer it if we cannot do it
5673 in the current context. */
5674#ifdef E1K_TX_DELAY
5675 rc = e1kCsTxEnter(pThis, VERR_SEM_BUSY);
5676 if (RT_LIKELY(rc == VINF_SUCCESS))
5677 {
5678 if (!PDMDevInsTimerIsActive(pDevIns, pThis->hTXDTimer))
5679 {
5680# ifdef E1K_INT_STATS
5681 pThis->u64ArmedAt = RTTimeNanoTS();
5682# endif
5683 e1kArmTimer(pDevIns, pThis, pThis->hTXDTimer, E1K_TX_DELAY);
5684 }
5685 E1K_INC_ISTAT_CNT(pThis->uStatTxDelayed);
5686 e1kCsTxLeave(pThis);
5687 return rc;
5688 }
5689 /* We failed to enter the TX critical section -- transmit as usual. */
5690#endif /* E1K_TX_DELAY */
5691#ifndef IN_RING3
5692 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
5693 if (!pThisCC->CTX_SUFF(pDrv))
5694 {
5695 PDMDevHlpTaskTrigger(pDevIns, pThis->hTxTask);
5696 rc = VINF_SUCCESS;
5697 }
5698 else
5699#endif
5700 {
5701 rc = e1kXmitPending(pDevIns, pThis, false /*fOnWorkerThread*/);
5702 if (rc == VERR_TRY_AGAIN)
5703 rc = VINF_SUCCESS;
5704#ifndef IN_RING3
5705 else if (rc == VERR_SEM_BUSY)
5706 rc = VINF_IOM_R3_MMIO_WRITE;
5707#endif
5708 AssertRC(rc);
5709 }
5710 }
5711
5712 return rc;
5713}
5714
5715/**
5716 * Write handler for Multicast Table Array registers.
5717 *
5718 * @param pThis The device state structure.
5719 * @param offset Register offset in memory-mapped frame.
5720 * @param index Register index in register array.
5721 * @param value The value to store.
5722 * @thread EMT
5723 */
5724static int e1kRegWriteMTA(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
5725{
5726 RT_NOREF_PV(pDevIns);
5727 AssertReturn(offset - g_aE1kRegMap[index].offset < sizeof(pThis->auMTA), VERR_DEV_IO_ERROR);
5728 pThis->auMTA[(offset - g_aE1kRegMap[index].offset) / sizeof(pThis->auMTA[0])] = value;
5729
5730 return VINF_SUCCESS;
5731}
5732
5733/**
5734 * Read handler for Multicast Table Array registers.
5735 *
5736 * @returns VBox status code.
5737 *
5738 * @param pThis The device state structure.
5739 * @param offset Register offset in memory-mapped frame.
5740 * @param index Register index in register array.
5741 * @thread EMT
5742 */
5743static int e1kRegReadMTA(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
5744{
5745 RT_NOREF_PV(pDevIns);
5746 AssertReturn(offset - g_aE1kRegMap[index].offset < sizeof(pThis->auMTA), VERR_DEV_IO_ERROR);
5747 *pu32Value = pThis->auMTA[(offset - g_aE1kRegMap[index].offset)/sizeof(pThis->auMTA[0])];
5748
5749 return VINF_SUCCESS;
5750}
5751
5752/**
5753 * Write handler for Receive Address registers.
5754 *
5755 * @param pThis The device state structure.
5756 * @param offset Register offset in memory-mapped frame.
5757 * @param index Register index in register array.
5758 * @param value The value to store.
5759 * @thread EMT
5760 */
5761static int e1kRegWriteRA(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
5762{
5763 RT_NOREF_PV(pDevIns);
5764 AssertReturn(offset - g_aE1kRegMap[index].offset < sizeof(pThis->aRecAddr.au32), VERR_DEV_IO_ERROR);
5765 pThis->aRecAddr.au32[(offset - g_aE1kRegMap[index].offset)/sizeof(pThis->aRecAddr.au32[0])] = value;
5766
5767 return VINF_SUCCESS;
5768}
5769
5770/**
5771 * Read handler for Receive Address registers.
5772 *
5773 * @returns VBox status code.
5774 *
5775 * @param pThis The device state structure.
5776 * @param offset Register offset in memory-mapped frame.
5777 * @param index Register index in register array.
5778 * @thread EMT
5779 */
5780static int e1kRegReadRA(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
5781{
5782 RT_NOREF_PV(pDevIns);
5783 AssertReturn(offset - g_aE1kRegMap[index].offset< sizeof(pThis->aRecAddr.au32), VERR_DEV_IO_ERROR);
5784 *pu32Value = pThis->aRecAddr.au32[(offset - g_aE1kRegMap[index].offset)/sizeof(pThis->aRecAddr.au32[0])];
5785
5786 return VINF_SUCCESS;
5787}
5788
5789/**
5790 * Write handler for VLAN Filter Table Array registers.
5791 *
5792 * @param pThis The device state structure.
5793 * @param offset Register offset in memory-mapped frame.
5794 * @param index Register index in register array.
5795 * @param value The value to store.
5796 * @thread EMT
5797 */
5798static int e1kRegWriteVFTA(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
5799{
5800 RT_NOREF_PV(pDevIns);
5801 AssertReturn(offset - g_aE1kRegMap[index].offset < sizeof(pThis->auVFTA), VINF_SUCCESS);
5802 pThis->auVFTA[(offset - g_aE1kRegMap[index].offset)/sizeof(pThis->auVFTA[0])] = value;
5803
5804 return VINF_SUCCESS;
5805}
5806
5807/**
5808 * Read handler for VLAN Filter Table Array registers.
5809 *
5810 * @returns VBox status code.
5811 *
5812 * @param pThis The device state structure.
5813 * @param offset Register offset in memory-mapped frame.
5814 * @param index Register index in register array.
5815 * @thread EMT
5816 */
5817static int e1kRegReadVFTA(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
5818{
5819 RT_NOREF_PV(pDevIns);
5820 AssertReturn(offset - g_aE1kRegMap[index].offset< sizeof(pThis->auVFTA), VERR_DEV_IO_ERROR);
5821 *pu32Value = pThis->auVFTA[(offset - g_aE1kRegMap[index].offset)/sizeof(pThis->auVFTA[0])];
5822
5823 return VINF_SUCCESS;
5824}
5825
5826/**
5827 * Read handler for unimplemented registers.
5828 *
5829 * Merely reports reads from unimplemented registers.
5830 *
5831 * @returns VBox status code.
5832 *
5833 * @param pThis The device state structure.
5834 * @param offset Register offset in memory-mapped frame.
5835 * @param index Register index in register array.
5836 * @thread EMT
5837 */
5838static int e1kRegReadUnimplemented(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
5839{
5840 RT_NOREF(pDevIns, pThis, offset, index);
5841 E1kLog(("%s At %08X read (00000000) attempt from unimplemented register %s (%s)\n",
5842 pThis->szPrf, offset, g_aE1kRegMap[index].abbrev, g_aE1kRegMap[index].name));
5843 *pu32Value = 0;
5844
5845 return VINF_SUCCESS;
5846}
5847
5848/**
5849 * Default register read handler with automatic clear operation.
5850 *
5851 * Retrieves the value of register from register array in device state structure.
5852 * Then resets all bits.
5853 *
5854 * @remarks The 'mask' parameter is simply ignored as masking and shifting is
5855 * done in the caller.
5856 *
5857 * @returns VBox status code.
5858 *
5859 * @param pThis The device state structure.
5860 * @param offset Register offset in memory-mapped frame.
5861 * @param index Register index in register array.
5862 * @thread EMT
5863 */
5864static int e1kRegReadAutoClear(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
5865{
5866 AssertReturn(index < E1K_NUM_OF_32BIT_REGS, VERR_DEV_IO_ERROR);
5867 int rc = e1kRegReadDefault(pDevIns, pThis, offset, index, pu32Value);
5868 pThis->auRegs[index] = 0;
5869
5870 return rc;
5871}
5872
5873/**
5874 * Default register read handler.
5875 *
5876 * Retrieves the value of register from register array in device state structure.
5877 * Bits corresponding to 0s in 'readable' mask will always read as 0s.
5878 *
5879 * @remarks The 'mask' parameter is simply ignored as masking and shifting is
5880 * done in the caller.
5881 *
5882 * @returns VBox status code.
5883 *
5884 * @param pThis The device state structure.
5885 * @param offset Register offset in memory-mapped frame.
5886 * @param index Register index in register array.
5887 * @thread EMT
5888 */
5889static int e1kRegReadDefault(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t *pu32Value)
5890{
5891 RT_NOREF_PV(pDevIns); RT_NOREF_PV(offset);
5892
5893 AssertReturn(index < E1K_NUM_OF_32BIT_REGS, VERR_DEV_IO_ERROR);
5894 *pu32Value = pThis->auRegs[index] & g_aE1kRegMap[index].readable;
5895
5896 return VINF_SUCCESS;
5897}
5898
5899/**
5900 * Write handler for unimplemented registers.
5901 *
5902 * Merely reports writes to unimplemented registers.
5903 *
5904 * @param pThis The device state structure.
5905 * @param offset Register offset in memory-mapped frame.
5906 * @param index Register index in register array.
5907 * @param value The value to store.
5908 * @thread EMT
5909 */
5910
5911 static int e1kRegWriteUnimplemented(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
5912{
5913 RT_NOREF_PV(pDevIns); RT_NOREF_PV(pThis); RT_NOREF_PV(offset); RT_NOREF_PV(index); RT_NOREF_PV(value);
5914
5915 E1kLog(("%s At %08X write attempt (%08X) to unimplemented register %s (%s)\n",
5916 pThis->szPrf, offset, value, g_aE1kRegMap[index].abbrev, g_aE1kRegMap[index].name));
5917
5918 return VINF_SUCCESS;
5919}
5920
5921/**
5922 * Default register write handler.
5923 *
5924 * Stores the value to the register array in device state structure. Only bits
5925 * corresponding to 1s both in 'writable' and 'mask' will be stored.
5926 *
5927 * @returns VBox status code.
5928 *
5929 * @param pThis The device state structure.
5930 * @param offset Register offset in memory-mapped frame.
5931 * @param index Register index in register array.
5932 * @param value The value to store.
5933 * @param mask Used to implement partial writes (8 and 16-bit).
5934 * @thread EMT
5935 */
5936
5937static int e1kRegWriteDefault(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offset, uint32_t index, uint32_t value)
5938{
5939 RT_NOREF(pDevIns, offset);
5940
5941 AssertReturn(index < E1K_NUM_OF_32BIT_REGS, VERR_DEV_IO_ERROR);
5942 pThis->auRegs[index] = (value & g_aE1kRegMap[index].writable)
5943 | (pThis->auRegs[index] & ~g_aE1kRegMap[index].writable);
5944
5945 return VINF_SUCCESS;
5946}
5947
5948/**
5949 * Search register table for matching register.
5950 *
5951 * @returns Index in the register table or -1 if not found.
5952 *
5953 * @param offReg Register offset in memory-mapped region.
5954 * @thread EMT
5955 */
5956static int e1kRegLookup(uint32_t offReg)
5957{
5958
5959#if 0
5960 int index;
5961
5962 for (index = 0; index < E1K_NUM_OF_REGS; index++)
5963 {
5964 if (g_aE1kRegMap[index].offset <= offReg && offReg < g_aE1kRegMap[index].offset + g_aE1kRegMap[index].size)
5965 {
5966 return index;
5967 }
5968 }
5969#else
5970 int iStart = 0;
5971 int iEnd = E1K_NUM_OF_BINARY_SEARCHABLE;
5972 for (;;)
5973 {
5974 int i = (iEnd - iStart) / 2 + iStart;
5975 uint32_t offCur = g_aE1kRegMap[i].offset;
5976 if (offReg < offCur)
5977 {
5978 if (i == iStart)
5979 break;
5980 iEnd = i;
5981 }
5982 else if (offReg >= offCur + g_aE1kRegMap[i].size)
5983 {
5984 i++;
5985 if (i == iEnd)
5986 break;
5987 iStart = i;
5988 }
5989 else
5990 return i;
5991 Assert(iEnd > iStart);
5992 }
5993
5994 for (unsigned i = E1K_NUM_OF_BINARY_SEARCHABLE; i < RT_ELEMENTS(g_aE1kRegMap); i++)
5995 if (offReg - g_aE1kRegMap[i].offset < g_aE1kRegMap[i].size)
5996 return (int)i;
5997
5998# ifdef VBOX_STRICT
5999 for (unsigned i = 0; i < RT_ELEMENTS(g_aE1kRegMap); i++)
6000 Assert(offReg - g_aE1kRegMap[i].offset >= g_aE1kRegMap[i].size);
6001# endif
6002
6003#endif
6004
6005 return -1;
6006}
6007
6008/**
6009 * Handle unaligned register read operation.
6010 *
6011 * Looks up and calls appropriate handler.
6012 *
6013 * @returns VBox status code.
6014 *
6015 * @param pDevIns The device instance.
6016 * @param pThis The device state structure.
6017 * @param offReg Register offset in memory-mapped frame.
6018 * @param pv Where to store the result.
6019 * @param cb Number of bytes to read.
6020 * @thread EMT
6021 * @remarks IOM takes care of unaligned and small reads via MMIO. For I/O port
6022 * accesses we have to take care of that ourselves.
6023 */
6024static int e1kRegReadUnaligned(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offReg, void *pv, uint32_t cb)
6025{
6026 uint32_t u32 = 0;
6027 uint32_t shift;
6028 int rc = VINF_SUCCESS;
6029 int index = e1kRegLookup(offReg);
6030#ifdef LOG_ENABLED
6031 char buf[9];
6032#endif
6033
6034 /*
6035 * From the spec:
6036 * For registers that should be accessed as 32-bit double words, partial writes (less than a 32-bit
6037 * double word) is ignored. Partial reads return all 32 bits of data regardless of the byte enables.
6038 */
6039
6040 /*
6041 * To be able to read bytes and short word we convert them to properly
6042 * shifted 32-bit words and masks. The idea is to keep register-specific
6043 * handlers simple. Most accesses will be 32-bit anyway.
6044 */
6045 uint32_t mask;
6046 switch (cb)
6047 {
6048 case 4: mask = 0xFFFFFFFF; break;
6049 case 2: mask = 0x0000FFFF; break;
6050 case 1: mask = 0x000000FF; break;
6051 default:
6052 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "unsupported op size: offset=%#10x cb=%#10x\n", offReg, cb);
6053 }
6054 if (index >= 0)
6055 {
6056 RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia because of port I/O. */
6057 if (g_aE1kRegMap[index].readable)
6058 {
6059 /* Make the mask correspond to the bits we are about to read. */
6060 shift = (offReg - g_aE1kRegMap[index].offset) % sizeof(uint32_t) * 8;
6061 mask <<= shift;
6062 if (!mask)
6063 return PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "Zero mask: offset=%#10x cb=%#10x\n", offReg, cb);
6064 /*
6065 * Read it. Pass the mask so the handler knows what has to be read.
6066 * Mask out irrelevant bits.
6067 */
6068 //rc = e1kCsEnter(pThis, VERR_SEM_BUSY, RT_SRC_POS);
6069 if (RT_UNLIKELY(rc != VINF_SUCCESS))
6070 return rc;
6071 //pThis->fDelayInts = false;
6072 //pThis->iStatIntLost += pThis->iStatIntLostOne;
6073 //pThis->iStatIntLostOne = 0;
6074 rc = g_aE1kRegMap[index].pfnRead(pDevIns, pThis, offReg & 0xFFFFFFFC, (uint32_t)index, &u32);
6075 u32 &= mask;
6076 //e1kCsLeave(pThis);
6077 E1kLog2(("%s At %08X read %s from %s (%s)\n",
6078 pThis->szPrf, offReg, e1kU32toHex(u32, mask, buf), g_aE1kRegMap[index].abbrev, g_aE1kRegMap[index].name));
6079 Log6(("%s At %08X read %s from %s (%s) [UNALIGNED]\n",
6080 pThis->szPrf, offReg, e1kU32toHex(u32, mask, buf), g_aE1kRegMap[index].abbrev, g_aE1kRegMap[index].name));
6081 /* Shift back the result. */
6082 u32 >>= shift;
6083 }
6084 else
6085 E1kLog(("%s At %08X read (%s) attempt from write-only register %s (%s)\n",
6086 pThis->szPrf, offReg, e1kU32toHex(u32, mask, buf), g_aE1kRegMap[index].abbrev, g_aE1kRegMap[index].name));
6087 if (IOM_SUCCESS(rc))
6088 STAM_COUNTER_INC(&pThis->aStatRegReads[index]);
6089 }
6090 else
6091 E1kLog(("%s At %08X read (%s) attempt from non-existing register\n",
6092 pThis->szPrf, offReg, e1kU32toHex(u32, mask, buf)));
6093
6094 memcpy(pv, &u32, cb);
6095 return rc;
6096}
6097
6098/**
6099 * Handle 4 byte aligned and sized read operation.
6100 *
6101 * Looks up and calls appropriate handler.
6102 *
6103 * @returns VBox status code.
6104 *
6105 * @param pDevIns The device instance.
6106 * @param pThis The device state structure.
6107 * @param offReg Register offset in memory-mapped frame.
6108 * @param pu32 Where to store the result.
6109 * @thread EMT
6110 */
6111static VBOXSTRICTRC e1kRegReadAlignedU32(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offReg, uint32_t *pu32)
6112{
6113 Assert(!(offReg & 3));
6114
6115 /*
6116 * Lookup the register and check that it's readable.
6117 */
6118 VBOXSTRICTRC rc = VINF_SUCCESS;
6119 int idxReg = e1kRegLookup(offReg);
6120 if (RT_LIKELY(idxReg >= 0))
6121 {
6122 RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia because of port I/O. */
6123 if (RT_UNLIKELY(g_aE1kRegMap[idxReg].readable))
6124 {
6125 /*
6126 * Read it. Pass the mask so the handler knows what has to be read.
6127 * Mask out irrelevant bits.
6128 */
6129 //rc = e1kCsEnter(pThis, VERR_SEM_BUSY, RT_SRC_POS);
6130 //if (RT_UNLIKELY(rc != VINF_SUCCESS))
6131 // return rc;
6132 //pThis->fDelayInts = false;
6133 //pThis->iStatIntLost += pThis->iStatIntLostOne;
6134 //pThis->iStatIntLostOne = 0;
6135 rc = g_aE1kRegMap[idxReg].pfnRead(pDevIns, pThis, offReg & 0xFFFFFFFC, (uint32_t)idxReg, pu32);
6136 //e1kCsLeave(pThis);
6137 Log6(("%s At %08X read %08X from %s (%s)\n",
6138 pThis->szPrf, offReg, *pu32, g_aE1kRegMap[idxReg].abbrev, g_aE1kRegMap[idxReg].name));
6139 if (IOM_SUCCESS(rc))
6140 STAM_COUNTER_INC(&pThis->aStatRegReads[idxReg]);
6141 }
6142 else
6143 E1kLog(("%s At %08X read attempt from non-readable register %s (%s)\n",
6144 pThis->szPrf, offReg, g_aE1kRegMap[idxReg].abbrev, g_aE1kRegMap[idxReg].name));
6145 }
6146 else
6147 E1kLog(("%s At %08X read attempt from non-existing register\n", pThis->szPrf, offReg));
6148 return rc;
6149}
6150
6151/**
6152 * Handle 4 byte sized and aligned register write operation.
6153 *
6154 * Looks up and calls appropriate handler.
6155 *
6156 * @returns VBox status code.
6157 *
6158 * @param pDevIns The device instance.
6159 * @param pThis The device state structure.
6160 * @param offReg Register offset in memory-mapped frame.
6161 * @param u32Value The value to write.
6162 * @thread EMT
6163 */
6164static VBOXSTRICTRC e1kRegWriteAlignedU32(PPDMDEVINS pDevIns, PE1KSTATE pThis, uint32_t offReg, uint32_t u32Value)
6165{
6166 VBOXSTRICTRC rc = VINF_SUCCESS;
6167 int index = e1kRegLookup(offReg);
6168 if (RT_LIKELY(index >= 0))
6169 {
6170 RT_UNTRUSTED_VALIDATED_FENCE(); /* paranoia because of port I/O. */
6171 if (RT_LIKELY(g_aE1kRegMap[index].writable))
6172 {
6173 /*
6174 * Write it. Pass the mask so the handler knows what has to be written.
6175 * Mask out irrelevant bits.
6176 */
6177 Log6(("%s At %08X write %08X to %s (%s)\n",
6178 pThis->szPrf, offReg, u32Value, g_aE1kRegMap[index].abbrev, g_aE1kRegMap[index].name));
6179 //rc = e1kCsEnter(pThis, VERR_SEM_BUSY, RT_SRC_POS);
6180 //if (RT_UNLIKELY(rc != VINF_SUCCESS))
6181 // return rc;
6182 //pThis->fDelayInts = false;
6183 //pThis->iStatIntLost += pThis->iStatIntLostOne;
6184 //pThis->iStatIntLostOne = 0;
6185 rc = g_aE1kRegMap[index].pfnWrite(pDevIns, pThis, offReg, (uint32_t)index, u32Value);
6186 //e1kCsLeave(pThis);
6187 }
6188 else
6189 E1kLog(("%s At %08X write attempt (%08X) to read-only register %s (%s)\n",
6190 pThis->szPrf, offReg, u32Value, g_aE1kRegMap[index].abbrev, g_aE1kRegMap[index].name));
6191 if (IOM_SUCCESS(rc))
6192 STAM_COUNTER_INC(&pThis->aStatRegWrites[index]);
6193 }
6194 else
6195 E1kLog(("%s At %08X write attempt (%08X) to non-existing register\n",
6196 pThis->szPrf, offReg, u32Value));
6197 return rc;
6198}
6199
6200
6201/* -=-=-=-=- MMIO and I/O Port Callbacks -=-=-=-=- */
6202
6203/**
6204 * @callback_method_impl{FNIOMMMIONEWREAD}
6205 */
6206static DECLCALLBACK(VBOXSTRICTRC) e1kMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, uint32_t cb)
6207{
6208 RT_NOREF2(pvUser, cb);
6209 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6210 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatMMIORead), a);
6211
6212 Assert(off < E1K_MM_SIZE);
6213 Assert(cb == 4);
6214 Assert(!(off & 3));
6215
6216 VBOXSTRICTRC rcStrict = e1kRegReadAlignedU32(pDevIns, pThis, (uint32_t)off, (uint32_t *)pv);
6217
6218 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatMMIORead), a);
6219 return rcStrict;
6220}
6221
6222/**
6223 * @callback_method_impl{FNIOMMMIONEWWRITE}
6224 */
6225static DECLCALLBACK(VBOXSTRICTRC) e1kMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, uint32_t cb)
6226{
6227 RT_NOREF2(pvUser, cb);
6228 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6229 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatMMIOWrite), a);
6230
6231 Assert(off < E1K_MM_SIZE);
6232 Assert(cb == 4);
6233 Assert(!(off & 3));
6234
6235 VBOXSTRICTRC rcStrict = e1kRegWriteAlignedU32(pDevIns, pThis, (uint32_t)off, *(uint32_t const *)pv);
6236
6237 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatMMIOWrite), a);
6238 return rcStrict;
6239}
6240
6241/**
6242 * @callback_method_impl{FNIOMIOPORTNEWIN}
6243 */
6244static DECLCALLBACK(VBOXSTRICTRC) e1kIOPortIn(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
6245{
6246 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6247 VBOXSTRICTRC rc;
6248 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIORead), a);
6249 RT_NOREF_PV(pvUser);
6250
6251 if (RT_LIKELY(cb == 4))
6252 switch (offPort)
6253 {
6254 case 0x00: /* IOADDR */
6255 *pu32 = pThis->uSelectedReg;
6256 E1kLog2(("%s e1kIOPortIn: IOADDR(0), selecting register %#010x, val=%#010x\n", pThis->szPrf, pThis->uSelectedReg, *pu32));
6257 rc = VINF_SUCCESS;
6258 break;
6259
6260 case 0x04: /* IODATA */
6261 if (!(pThis->uSelectedReg & 3))
6262 rc = e1kRegReadAlignedU32(pDevIns, pThis, pThis->uSelectedReg, pu32);
6263 else /** @todo r=bird: I wouldn't be surprised if this unaligned branch wasn't necessary. */
6264 rc = e1kRegReadUnaligned(pDevIns, pThis, pThis->uSelectedReg, pu32, cb);
6265 if (rc == VINF_IOM_R3_MMIO_READ)
6266 rc = VINF_IOM_R3_IOPORT_READ;
6267 E1kLog2(("%s e1kIOPortIn: IODATA(4), reading from selected register %#010x, val=%#010x\n", pThis->szPrf, pThis->uSelectedReg, *pu32));
6268 break;
6269
6270 default:
6271 E1kLog(("%s e1kIOPortIn: invalid port %#010x\n", pThis->szPrf, offPort));
6272 /** @todo r=bird: Check what real hardware returns here. */
6273 //rc = VERR_IOM_IOPORT_UNUSED; /* Why not? */
6274 rc = VINF_IOM_MMIO_UNUSED_00; /* used to return VINF_SUCCESS and not touch *pu32, which amounted to this. */
6275 break;
6276 }
6277 else
6278 {
6279 E1kLog(("%s e1kIOPortIn: invalid op size: offPort=%RTiop cb=%08x", pThis->szPrf, offPort, cb));
6280 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "%s e1kIOPortIn: invalid op size: offPort=%RTiop cb=%08x\n", pThis->szPrf, offPort, cb);
6281 *pu32 = 0; /** @todo r=bird: Check what real hardware returns here. (Didn't used to set a value here, picked zero as that's what we'd end up in most cases.) */
6282 }
6283 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIORead), a);
6284 return rc;
6285}
6286
6287
6288/**
6289 * @callback_method_impl{FNIOMIOPORTNEWOUT}
6290 */
6291static DECLCALLBACK(VBOXSTRICTRC) e1kIOPortOut(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
6292{
6293 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6294 VBOXSTRICTRC rc;
6295 STAM_PROFILE_ADV_START(&pThis->CTX_SUFF_Z(StatIOWrite), a);
6296 RT_NOREF_PV(pvUser);
6297
6298 E1kLog2(("%s e1kIOPortOut: offPort=%RTiop value=%08x\n", pThis->szPrf, offPort, u32));
6299 if (RT_LIKELY(cb == 4))
6300 {
6301 switch (offPort)
6302 {
6303 case 0x00: /* IOADDR */
6304 pThis->uSelectedReg = u32;
6305 E1kLog2(("%s e1kIOPortOut: IOADDR(0), selected register %08x\n", pThis->szPrf, pThis->uSelectedReg));
6306 rc = VINF_SUCCESS;
6307 break;
6308
6309 case 0x04: /* IODATA */
6310 E1kLog2(("%s e1kIOPortOut: IODATA(4), writing to selected register %#010x, value=%#010x\n", pThis->szPrf, pThis->uSelectedReg, u32));
6311 if (RT_LIKELY(!(pThis->uSelectedReg & 3)))
6312 {
6313 rc = e1kRegWriteAlignedU32(pDevIns, pThis, pThis->uSelectedReg, u32);
6314 if (rc == VINF_IOM_R3_MMIO_WRITE)
6315 rc = VINF_IOM_R3_IOPORT_WRITE;
6316 }
6317 else
6318 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS,
6319 "Spec violation: misaligned offset: %#10x, ignored.\n", pThis->uSelectedReg);
6320 break;
6321
6322 default:
6323 E1kLog(("%s e1kIOPortOut: invalid port %#010x\n", pThis->szPrf, offPort));
6324 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "invalid port %#010x\n", offPort);
6325 }
6326 }
6327 else
6328 {
6329 E1kLog(("%s e1kIOPortOut: invalid op size: offPort=%RTiop cb=%08x\n", pThis->szPrf, offPort, cb));
6330 rc = PDMDevHlpDBGFStop(pDevIns, RT_SRC_POS, "%s: invalid op size: offPort=%RTiop cb=%#x\n", pThis->szPrf, offPort, cb);
6331 }
6332
6333 STAM_PROFILE_ADV_STOP(&pThis->CTX_SUFF_Z(StatIOWrite), a);
6334 return rc;
6335}
6336
6337#ifdef IN_RING3
6338
6339/**
6340 * Dump complete device state to log.
6341 *
6342 * @param pThis Pointer to device state.
6343 */
6344static void e1kDumpState(PE1KSTATE pThis)
6345{
6346 RT_NOREF(pThis);
6347 for (int i = 0; i < E1K_NUM_OF_32BIT_REGS; ++i)
6348 E1kLog2(("%s: %8.8s = %08x\n", pThis->szPrf, g_aE1kRegMap[i].abbrev, pThis->auRegs[i]));
6349# ifdef E1K_INT_STATS
6350 LogRel(("%s: Interrupt attempts: %d\n", pThis->szPrf, pThis->uStatIntTry));
6351 LogRel(("%s: Interrupts raised : %d\n", pThis->szPrf, pThis->uStatInt));
6352 LogRel(("%s: Interrupts lowered: %d\n", pThis->szPrf, pThis->uStatIntLower));
6353 LogRel(("%s: ICR outside ISR : %d\n", pThis->szPrf, pThis->uStatNoIntICR));
6354 LogRel(("%s: IMS raised ints : %d\n", pThis->szPrf, pThis->uStatIntIMS));
6355 LogRel(("%s: Interrupts skipped: %d\n", pThis->szPrf, pThis->uStatIntSkip));
6356 LogRel(("%s: Masked interrupts : %d\n", pThis->szPrf, pThis->uStatIntMasked));
6357 LogRel(("%s: Early interrupts : %d\n", pThis->szPrf, pThis->uStatIntEarly));
6358 LogRel(("%s: Late interrupts : %d\n", pThis->szPrf, pThis->uStatIntLate));
6359 LogRel(("%s: Lost interrupts : %d\n", pThis->szPrf, pThis->iStatIntLost));
6360 LogRel(("%s: Interrupts by RX : %d\n", pThis->szPrf, pThis->uStatIntRx));
6361 LogRel(("%s: Interrupts by TX : %d\n", pThis->szPrf, pThis->uStatIntTx));
6362 LogRel(("%s: Interrupts by ICS : %d\n", pThis->szPrf, pThis->uStatIntICS));
6363 LogRel(("%s: Interrupts by RDTR: %d\n", pThis->szPrf, pThis->uStatIntRDTR));
6364 LogRel(("%s: Interrupts by RDMT: %d\n", pThis->szPrf, pThis->uStatIntRXDMT0));
6365 LogRel(("%s: Interrupts by TXQE: %d\n", pThis->szPrf, pThis->uStatIntTXQE));
6366 LogRel(("%s: TX int delay asked: %d\n", pThis->szPrf, pThis->uStatTxIDE));
6367 LogRel(("%s: TX delayed: %d\n", pThis->szPrf, pThis->uStatTxDelayed));
6368 LogRel(("%s: TX delay expired: %d\n", pThis->szPrf, pThis->uStatTxDelayExp));
6369 LogRel(("%s: TX no report asked: %d\n", pThis->szPrf, pThis->uStatTxNoRS));
6370 LogRel(("%s: TX abs timer expd : %d\n", pThis->szPrf, pThis->uStatTAD));
6371 LogRel(("%s: TX int timer expd : %d\n", pThis->szPrf, pThis->uStatTID));
6372 LogRel(("%s: RX abs timer expd : %d\n", pThis->szPrf, pThis->uStatRAD));
6373 LogRel(("%s: RX int timer expd : %d\n", pThis->szPrf, pThis->uStatRID));
6374 LogRel(("%s: TX CTX descriptors: %d\n", pThis->szPrf, pThis->uStatDescCtx));
6375 LogRel(("%s: TX DAT descriptors: %d\n", pThis->szPrf, pThis->uStatDescDat));
6376 LogRel(("%s: TX LEG descriptors: %d\n", pThis->szPrf, pThis->uStatDescLeg));
6377 LogRel(("%s: Received frames : %d\n", pThis->szPrf, pThis->uStatRxFrm));
6378 LogRel(("%s: Transmitted frames: %d\n", pThis->szPrf, pThis->uStatTxFrm));
6379 LogRel(("%s: TX frames up to 1514: %d\n", pThis->szPrf, pThis->uStatTx1514));
6380 LogRel(("%s: TX frames up to 2962: %d\n", pThis->szPrf, pThis->uStatTx2962));
6381 LogRel(("%s: TX frames up to 4410: %d\n", pThis->szPrf, pThis->uStatTx4410));
6382 LogRel(("%s: TX frames up to 5858: %d\n", pThis->szPrf, pThis->uStatTx5858));
6383 LogRel(("%s: TX frames up to 7306: %d\n", pThis->szPrf, pThis->uStatTx7306));
6384 LogRel(("%s: TX frames up to 8754: %d\n", pThis->szPrf, pThis->uStatTx8754));
6385 LogRel(("%s: TX frames up to 16384: %d\n", pThis->szPrf, pThis->uStatTx16384));
6386 LogRel(("%s: TX frames up to 32768: %d\n", pThis->szPrf, pThis->uStatTx32768));
6387 LogRel(("%s: Larger TX frames : %d\n", pThis->szPrf, pThis->uStatTxLarge));
6388 LogRel(("%s: Max TX Delay : %lld\n", pThis->szPrf, pThis->uStatMaxTxDelay));
6389# endif /* E1K_INT_STATS */
6390}
6391
6392
6393/* -=-=-=-=- PDMINETWORKDOWN -=-=-=-=- */
6394
6395/**
6396 * Check if the device can receive data now.
6397 * This must be called before the pfnRecieve() method is called.
6398 *
6399 * @returns Number of bytes the device can receive.
6400 * @param pDevIns The device instance.
6401 * @param pThis The instance data.
6402 * @thread EMT
6403 */
6404static int e1kCanReceive(PPDMDEVINS pDevIns, PE1KSTATE pThis)
6405{
6406#ifndef E1K_WITH_RXD_CACHE
6407 size_t cb;
6408
6409 if (RT_UNLIKELY(e1kCsRxEnter(pThis, VERR_SEM_BUSY) != VINF_SUCCESS))
6410 return VERR_NET_NO_BUFFER_SPACE;
6411
6412 if (RT_UNLIKELY(RDLEN == sizeof(E1KRXDESC)))
6413 {
6414 E1KRXDESC desc;
6415 PDMDevHlpPCIPhysRead(pDevIns, e1kDescAddr(RDBAH, RDBAL, RDH), &desc, sizeof(desc));
6416 if (desc.status.fDD)
6417 cb = 0;
6418 else
6419 cb = pThis->u16RxBSize;
6420 }
6421 else if (RDH < RDT)
6422 cb = (RDT - RDH) * pThis->u16RxBSize;
6423 else if (RDH > RDT)
6424 cb = (RDLEN/sizeof(E1KRXDESC) - RDH + RDT) * pThis->u16RxBSize;
6425 else
6426 {
6427 cb = 0;
6428 E1kLogRel(("E1000: OUT of RX descriptors!\n"));
6429 }
6430 E1kLog2(("%s e1kCanReceive: at exit RDH=%d RDT=%d RDLEN=%d u16RxBSize=%d cb=%lu\n",
6431 pThis->szPrf, RDH, RDT, RDLEN, pThis->u16RxBSize, cb));
6432
6433 e1kCsRxLeave(pThis);
6434 return cb > 0 ? VINF_SUCCESS : VERR_NET_NO_BUFFER_SPACE;
6435#else /* E1K_WITH_RXD_CACHE */
6436 int rc = VINF_SUCCESS;
6437
6438 if (RT_UNLIKELY(e1kCsRxEnter(pThis, VERR_SEM_BUSY) != VINF_SUCCESS))
6439 return VERR_NET_NO_BUFFER_SPACE;
6440
6441 if (RT_UNLIKELY(RDLEN == sizeof(E1KRXDESC)))
6442 {
6443 E1KRXDESC desc;
6444 PDMDevHlpPCIPhysRead(pDevIns, e1kDescAddr(RDBAH, RDBAL, RDH), &desc, sizeof(desc));
6445 if (desc.status.fDD)
6446 rc = VERR_NET_NO_BUFFER_SPACE;
6447 }
6448 else if (e1kRxDIsCacheEmpty(pThis) && RDH == RDT)
6449 {
6450 /* Cache is empty, so is the RX ring. */
6451 rc = VERR_NET_NO_BUFFER_SPACE;
6452 }
6453 E1kLog2(("%s e1kCanReceive: at exit in_cache=%d RDH=%d RDT=%d RDLEN=%d"
6454 " u16RxBSize=%d rc=%Rrc\n", pThis->szPrf,
6455 e1kRxDInCache(pThis), RDH, RDT, RDLEN, pThis->u16RxBSize, rc));
6456
6457 e1kCsRxLeave(pThis);
6458 return rc;
6459#endif /* E1K_WITH_RXD_CACHE */
6460}
6461
6462/**
6463 * @interface_method_impl{PDMINETWORKDOWN,pfnWaitReceiveAvail}
6464 */
6465static DECLCALLBACK(int) e1kR3NetworkDown_WaitReceiveAvail(PPDMINETWORKDOWN pInterface, RTMSINTERVAL cMillies)
6466{
6467 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, INetworkDown);
6468 PE1KSTATE pThis = pThisCC->pShared;
6469 PPDMDEVINS pDevIns = pThisCC->pDevInsR3;
6470
6471 int rc = e1kCanReceive(pDevIns, pThis);
6472
6473 if (RT_SUCCESS(rc))
6474 return VINF_SUCCESS;
6475 if (RT_UNLIKELY(cMillies == 0))
6476 return VERR_NET_NO_BUFFER_SPACE;
6477
6478 rc = VERR_INTERRUPTED;
6479 ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, true);
6480 STAM_PROFILE_START(&pThis->StatRxOverflow, a);
6481 VMSTATE enmVMState;
6482 while (RT_LIKELY( (enmVMState = PDMDevHlpVMState(pDevIns)) == VMSTATE_RUNNING
6483 || enmVMState == VMSTATE_RUNNING_LS))
6484 {
6485 int rc2 = e1kCanReceive(pDevIns, pThis);
6486 if (RT_SUCCESS(rc2))
6487 {
6488 rc = VINF_SUCCESS;
6489 break;
6490 }
6491 E1kLogRel(("E1000: e1kR3NetworkDown_WaitReceiveAvail: waiting cMillies=%u...\n", cMillies));
6492 E1kLog(("%s: e1kR3NetworkDown_WaitReceiveAvail: waiting cMillies=%u...\n", pThis->szPrf, cMillies));
6493 PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEventMoreRxDescAvail, cMillies);
6494 }
6495 STAM_PROFILE_STOP(&pThis->StatRxOverflow, a);
6496 ASMAtomicXchgBool(&pThis->fMaybeOutOfSpace, false);
6497
6498 return rc;
6499}
6500
6501
6502/**
6503 * Matches the packet addresses against Receive Address table. Looks for
6504 * exact matches only.
6505 *
6506 * @returns true if address matches.
6507 * @param pThis Pointer to the state structure.
6508 * @param pvBuf The ethernet packet.
6509 * @param cb Number of bytes available in the packet.
6510 * @thread EMT
6511 */
6512static bool e1kPerfectMatch(PE1KSTATE pThis, const void *pvBuf)
6513{
6514 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aRecAddr.array); i++)
6515 {
6516 E1KRAELEM* ra = pThis->aRecAddr.array + i;
6517
6518 /* Valid address? */
6519 if (ra->ctl & RA_CTL_AV)
6520 {
6521 Assert((ra->ctl & RA_CTL_AS) < 2);
6522 //unsigned char *pAddr = (unsigned char*)pvBuf + sizeof(ra->addr)*(ra->ctl & RA_CTL_AS);
6523 //E1kLog3(("%s Matching %02x:%02x:%02x:%02x:%02x:%02x against %02x:%02x:%02x:%02x:%02x:%02x...\n",
6524 // pThis->szPrf, pAddr[0], pAddr[1], pAddr[2], pAddr[3], pAddr[4], pAddr[5],
6525 // ra->addr[0], ra->addr[1], ra->addr[2], ra->addr[3], ra->addr[4], ra->addr[5]));
6526 /*
6527 * Address Select:
6528 * 00b = Destination address
6529 * 01b = Source address
6530 * 10b = Reserved
6531 * 11b = Reserved
6532 * Since ethernet header is (DA, SA, len) we can use address
6533 * select as index.
6534 */
6535 if (memcmp((char*)pvBuf + sizeof(ra->addr)*(ra->ctl & RA_CTL_AS),
6536 ra->addr, sizeof(ra->addr)) == 0)
6537 return true;
6538 }
6539 }
6540
6541 return false;
6542}
6543
6544/**
6545 * Matches the packet addresses against Multicast Table Array.
6546 *
6547 * @remarks This is imperfect match since it matches not exact address but
6548 * a subset of addresses.
6549 *
6550 * @returns true if address matches.
6551 * @param pThis Pointer to the state structure.
6552 * @param pvBuf The ethernet packet.
6553 * @param cb Number of bytes available in the packet.
6554 * @thread EMT
6555 */
6556static bool e1kImperfectMatch(PE1KSTATE pThis, const void *pvBuf)
6557{
6558 /* Get bits 32..47 of destination address */
6559 uint16_t u16Bit = ((uint16_t*)pvBuf)[2];
6560
6561 unsigned offset = GET_BITS(RCTL, MO);
6562 /*
6563 * offset means:
6564 * 00b = bits 36..47
6565 * 01b = bits 35..46
6566 * 10b = bits 34..45
6567 * 11b = bits 32..43
6568 */
6569 if (offset < 3)
6570 u16Bit = u16Bit >> (4 - offset);
6571 return ASMBitTest(pThis->auMTA, u16Bit & 0xFFF);
6572}
6573
6574/**
6575 * Determines if the packet is to be delivered to upper layer.
6576 *
6577 * The following filters supported:
6578 * - Exact Unicast/Multicast
6579 * - Promiscuous Unicast/Multicast
6580 * - Multicast
6581 * - VLAN
6582 *
6583 * @returns true if packet is intended for this node.
6584 * @param pThis Pointer to the state structure.
6585 * @param pvBuf The ethernet packet.
6586 * @param cb Number of bytes available in the packet.
6587 * @param pStatus Bit field to store status bits.
6588 * @thread EMT
6589 */
6590static bool e1kAddressFilter(PE1KSTATE pThis, const void *pvBuf, size_t cb, E1KRXDST *pStatus)
6591{
6592 Assert(cb > 14);
6593 /* Assume that we fail to pass exact filter. */
6594 pStatus->fPIF = false;
6595 pStatus->fVP = false;
6596 /* Discard oversized packets */
6597 if (cb > E1K_MAX_RX_PKT_SIZE)
6598 {
6599 E1kLog(("%s ERROR: Incoming packet is too big, cb=%d > max=%d\n",
6600 pThis->szPrf, cb, E1K_MAX_RX_PKT_SIZE));
6601 E1K_INC_CNT32(ROC);
6602 return false;
6603 }
6604 else if (!(RCTL & RCTL_LPE) && cb > 1522)
6605 {
6606 /* When long packet reception is disabled packets over 1522 are discarded */
6607 E1kLog(("%s Discarding incoming packet (LPE=0), cb=%d\n",
6608 pThis->szPrf, cb));
6609 E1K_INC_CNT32(ROC);
6610 return false;
6611 }
6612
6613 uint16_t *u16Ptr = (uint16_t*)pvBuf;
6614 /* Compare TPID with VLAN Ether Type */
6615 if (RT_BE2H_U16(u16Ptr[6]) == VET)
6616 {
6617 pStatus->fVP = true;
6618 /* Is VLAN filtering enabled? */
6619 if (RCTL & RCTL_VFE)
6620 {
6621 /* It is 802.1q packet indeed, let's filter by VID */
6622 if (RCTL & RCTL_CFIEN)
6623 {
6624 E1kLog3(("%s VLAN filter: VLAN=%d CFI=%d RCTL_CFI=%d\n", pThis->szPrf,
6625 E1K_SPEC_VLAN(RT_BE2H_U16(u16Ptr[7])),
6626 E1K_SPEC_CFI(RT_BE2H_U16(u16Ptr[7])),
6627 !!(RCTL & RCTL_CFI)));
6628 if (E1K_SPEC_CFI(RT_BE2H_U16(u16Ptr[7])) != !!(RCTL & RCTL_CFI))
6629 {
6630 E1kLog2(("%s Packet filter: CFIs do not match in packet and RCTL (%d!=%d)\n",
6631 pThis->szPrf, E1K_SPEC_CFI(RT_BE2H_U16(u16Ptr[7])), !!(RCTL & RCTL_CFI)));
6632 return false;
6633 }
6634 }
6635 else
6636 E1kLog3(("%s VLAN filter: VLAN=%d\n", pThis->szPrf,
6637 E1K_SPEC_VLAN(RT_BE2H_U16(u16Ptr[7]))));
6638 if (!ASMBitTest(pThis->auVFTA, E1K_SPEC_VLAN(RT_BE2H_U16(u16Ptr[7]))))
6639 {
6640 E1kLog2(("%s Packet filter: no VLAN match (id=%d)\n",
6641 pThis->szPrf, E1K_SPEC_VLAN(RT_BE2H_U16(u16Ptr[7]))));
6642 return false;
6643 }
6644 }
6645 }
6646 /* Broadcast filtering */
6647 if (e1kIsBroadcast(pvBuf) && (RCTL & RCTL_BAM))
6648 return true;
6649 E1kLog2(("%s Packet filter: not a broadcast\n", pThis->szPrf));
6650 if (e1kIsMulticast(pvBuf))
6651 {
6652 /* Is multicast promiscuous enabled? */
6653 if (RCTL & RCTL_MPE)
6654 return true;
6655 E1kLog2(("%s Packet filter: no promiscuous multicast\n", pThis->szPrf));
6656 /* Try perfect matches first */
6657 if (e1kPerfectMatch(pThis, pvBuf))
6658 {
6659 pStatus->fPIF = true;
6660 return true;
6661 }
6662 E1kLog2(("%s Packet filter: no perfect match\n", pThis->szPrf));
6663 if (e1kImperfectMatch(pThis, pvBuf))
6664 return true;
6665 E1kLog2(("%s Packet filter: no imperfect match\n", pThis->szPrf));
6666 }
6667 else {
6668 /* Is unicast promiscuous enabled? */
6669 if (RCTL & RCTL_UPE)
6670 return true;
6671 E1kLog2(("%s Packet filter: no promiscuous unicast\n", pThis->szPrf));
6672 if (e1kPerfectMatch(pThis, pvBuf))
6673 {
6674 pStatus->fPIF = true;
6675 return true;
6676 }
6677 E1kLog2(("%s Packet filter: no perfect match\n", pThis->szPrf));
6678 }
6679 E1kLog2(("%s Packet filter: packet discarded\n", pThis->szPrf));
6680 return false;
6681}
6682
6683/**
6684 * @interface_method_impl{PDMINETWORKDOWN,pfnReceive}
6685 */
6686static DECLCALLBACK(int) e1kR3NetworkDown_Receive(PPDMINETWORKDOWN pInterface, const void *pvBuf, size_t cb)
6687{
6688 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, INetworkDown);
6689 PE1KSTATE pThis = pThisCC->pShared;
6690 PPDMDEVINS pDevIns = pThisCC->pDevInsR3;
6691 int rc = VINF_SUCCESS;
6692
6693 /*
6694 * Drop packets if the VM is not running yet/anymore.
6695 */
6696 VMSTATE enmVMState = PDMDevHlpVMState(pDevIns);
6697 if ( enmVMState != VMSTATE_RUNNING
6698 && enmVMState != VMSTATE_RUNNING_LS)
6699 {
6700 E1kLog(("%s Dropping incoming packet as VM is not running.\n", pThis->szPrf));
6701 return VINF_SUCCESS;
6702 }
6703
6704 /* Discard incoming packets in locked state */
6705 if (!(RCTL & RCTL_EN) || pThis->fLocked || !(STATUS & STATUS_LU))
6706 {
6707 E1kLog(("%s Dropping incoming packet as receive operation is disabled.\n", pThis->szPrf));
6708 return VINF_SUCCESS;
6709 }
6710
6711 STAM_PROFILE_ADV_START(&pThis->StatReceive, a);
6712
6713 //if (!e1kCsEnter(pThis, RT_SRC_POS))
6714 // return VERR_PERMISSION_DENIED;
6715
6716 e1kPacketDump(pDevIns, pThis, (const uint8_t*)pvBuf, cb, "<-- Incoming");
6717
6718 /* Update stats */
6719 if (RT_LIKELY(e1kCsEnter(pThis, VERR_SEM_BUSY) == VINF_SUCCESS))
6720 {
6721 E1K_INC_CNT32(TPR);
6722 E1K_ADD_CNT64(TORL, TORH, cb < 64? 64 : cb);
6723 e1kCsLeave(pThis);
6724 }
6725 STAM_PROFILE_ADV_START(&pThis->StatReceiveFilter, a);
6726 E1KRXDST status;
6727 RT_ZERO(status);
6728 bool fPassed = e1kAddressFilter(pThis, pvBuf, cb, &status);
6729 STAM_PROFILE_ADV_STOP(&pThis->StatReceiveFilter, a);
6730 if (fPassed)
6731 {
6732 rc = e1kHandleRxPacket(pDevIns, pThis, pvBuf, cb, status);
6733 }
6734 //e1kCsLeave(pThis);
6735 STAM_PROFILE_ADV_STOP(&pThis->StatReceive, a);
6736
6737 return rc;
6738}
6739
6740
6741/* -=-=-=-=- PDMILEDPORTS -=-=-=-=- */
6742
6743/**
6744 * @interface_method_impl{PDMILEDPORTS,pfnQueryStatusLed}
6745 */
6746static DECLCALLBACK(int) e1kR3QueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
6747{
6748 if (iLUN == 0)
6749 {
6750 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, ILeds);
6751 *ppLed = &pThisCC->pShared->led;
6752 return VINF_SUCCESS;
6753 }
6754 return VERR_PDM_LUN_NOT_FOUND;
6755}
6756
6757
6758/* -=-=-=-=- PDMINETWORKCONFIG -=-=-=-=- */
6759
6760/**
6761 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetMac}
6762 */
6763static DECLCALLBACK(int) e1kR3GetMac(PPDMINETWORKCONFIG pInterface, PRTMAC pMac)
6764{
6765 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, INetworkConfig);
6766 pThisCC->eeprom.getMac(pMac);
6767 return VINF_SUCCESS;
6768}
6769
6770/**
6771 * @interface_method_impl{PDMINETWORKCONFIG,pfnGetLinkState}
6772 */
6773static DECLCALLBACK(PDMNETWORKLINKSTATE) e1kR3GetLinkState(PPDMINETWORKCONFIG pInterface)
6774{
6775 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, INetworkConfig);
6776 PE1KSTATE pThis = pThisCC->pShared;
6777 if (STATUS & STATUS_LU)
6778 return PDMNETWORKLINKSTATE_UP;
6779 return PDMNETWORKLINKSTATE_DOWN;
6780}
6781
6782/**
6783 * @interface_method_impl{PDMINETWORKCONFIG,pfnSetLinkState}
6784 */
6785static DECLCALLBACK(int) e1kR3SetLinkState(PPDMINETWORKCONFIG pInterface, PDMNETWORKLINKSTATE enmState)
6786{
6787 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, INetworkConfig);
6788 PE1KSTATE pThis = pThisCC->pShared;
6789 PPDMDEVINS pDevIns = pThisCC->pDevInsR3;
6790
6791 E1kLog(("%s e1kR3SetLinkState: enmState=%d\n", pThis->szPrf, enmState));
6792 switch (enmState)
6793 {
6794 case PDMNETWORKLINKSTATE_UP:
6795 pThis->fCableConnected = true;
6796 /* If link was down, bring it up after a while. */
6797 if (!(STATUS & STATUS_LU))
6798 e1kBringLinkUpDelayed(pDevIns, pThis);
6799 break;
6800 case PDMNETWORKLINKSTATE_DOWN:
6801 pThis->fCableConnected = false;
6802 /* Always set the phy link state to down, regardless of the STATUS_LU bit.
6803 * We might have to set the link state before the driver initializes us. */
6804 Phy::setLinkStatus(&pThis->phy, false);
6805 /* If link was up, bring it down. */
6806 if (STATUS & STATUS_LU)
6807 e1kR3LinkDown(pDevIns, pThis, pThisCC);
6808 break;
6809 case PDMNETWORKLINKSTATE_DOWN_RESUME:
6810 /*
6811 * There is not much sense in bringing down the link if it has not come up yet.
6812 * If it is up though, we bring it down temporarely, then bring it up again.
6813 */
6814 if (STATUS & STATUS_LU)
6815 e1kR3LinkDownTemp(pDevIns, pThis, pThisCC);
6816 break;
6817 default:
6818 ;
6819 }
6820 return VINF_SUCCESS;
6821}
6822
6823
6824/* -=-=-=-=- PDMIBASE -=-=-=-=- */
6825
6826/**
6827 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
6828 */
6829static DECLCALLBACK(void *) e1kR3QueryInterface(struct PDMIBASE *pInterface, const char *pszIID)
6830{
6831 PE1KSTATECC pThisCC = RT_FROM_MEMBER(pInterface, E1KSTATECC, IBase);
6832 Assert(&pThisCC->IBase == pInterface);
6833
6834 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
6835 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKDOWN, &pThisCC->INetworkDown);
6836 PDMIBASE_RETURN_INTERFACE(pszIID, PDMINETWORKCONFIG, &pThisCC->INetworkConfig);
6837 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThisCC->ILeds);
6838 return NULL;
6839}
6840
6841
6842/* -=-=-=-=- Saved State -=-=-=-=- */
6843
6844/**
6845 * Saves the configuration.
6846 *
6847 * @param pThis The E1K state.
6848 * @param pSSM The handle to the saved state.
6849 */
6850static void e1kSaveConfig(PCPDMDEVHLPR3 pHlp, PE1KSTATE pThis, PSSMHANDLE pSSM)
6851{
6852 pHlp->pfnSSMPutMem(pSSM, &pThis->macConfigured, sizeof(pThis->macConfigured));
6853 pHlp->pfnSSMPutU32(pSSM, pThis->eChip);
6854}
6855
6856/**
6857 * @callback_method_impl{FNSSMDEVLIVEEXEC,Save basic configuration.}
6858 */
6859static DECLCALLBACK(int) e1kLiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
6860{
6861 RT_NOREF(uPass);
6862 e1kSaveConfig(pDevIns->pHlpR3, PDMDEVINS_2_DATA(pDevIns, PE1KSTATE), pSSM);
6863 return VINF_SSM_DONT_CALL_AGAIN;
6864}
6865
6866/**
6867 * @callback_method_impl{FNSSMDEVSAVEPREP,Synchronize.}
6868 */
6869static DECLCALLBACK(int) e1kSavePrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
6870{
6871 RT_NOREF(pSSM);
6872 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6873
6874 int rc = e1kCsEnter(pThis, VERR_SEM_BUSY);
6875 if (RT_UNLIKELY(rc != VINF_SUCCESS))
6876 return rc;
6877 e1kCsLeave(pThis);
6878 return VINF_SUCCESS;
6879#if 0
6880 /* 1) Prevent all threads from modifying the state and memory */
6881 //pThis->fLocked = true;
6882 /* 2) Cancel all timers */
6883#ifdef E1K_TX_DELAY
6884 e1kCancelTimer(pThis, pThis->CTX_SUFF(pTXDTimer));
6885#endif /* E1K_TX_DELAY */
6886//#ifdef E1K_USE_TX_TIMERS
6887 if (pThis->fTidEnabled)
6888 {
6889 e1kCancelTimer(pThis, pThis->CTX_SUFF(pTIDTimer));
6890#ifndef E1K_NO_TAD
6891 e1kCancelTimer(pThis, pThis->CTX_SUFF(pTADTimer));
6892#endif /* E1K_NO_TAD */
6893 }
6894//#endif /* E1K_USE_TX_TIMERS */
6895#ifdef E1K_USE_RX_TIMERS
6896 e1kCancelTimer(pThis, pThis->CTX_SUFF(pRIDTimer));
6897 e1kCancelTimer(pThis, pThis->CTX_SUFF(pRADTimer));
6898#endif /* E1K_USE_RX_TIMERS */
6899 e1kCancelTimer(pThis, pThis->CTX_SUFF(pIntTimer));
6900 /* 3) Did I forget anything? */
6901 E1kLog(("%s Locked\n", pThis->szPrf));
6902 return VINF_SUCCESS;
6903#endif
6904}
6905
6906/**
6907 * @callback_method_impl{FNSSMDEVSAVEEXEC}
6908 */
6909static DECLCALLBACK(int) e1kSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
6910{
6911 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6912 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
6913 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
6914
6915 e1kSaveConfig(pHlp, pThis, pSSM);
6916 pThisCC->eeprom.save(pHlp, pSSM);
6917 e1kDumpState(pThis);
6918 pHlp->pfnSSMPutMem(pSSM, pThis->auRegs, sizeof(pThis->auRegs));
6919 pHlp->pfnSSMPutBool(pSSM, pThis->fIntRaised);
6920 Phy::saveState(pHlp, pSSM, &pThis->phy);
6921 pHlp->pfnSSMPutU32(pSSM, pThis->uSelectedReg);
6922 pHlp->pfnSSMPutMem(pSSM, pThis->auMTA, sizeof(pThis->auMTA));
6923 pHlp->pfnSSMPutMem(pSSM, &pThis->aRecAddr, sizeof(pThis->aRecAddr));
6924 pHlp->pfnSSMPutMem(pSSM, pThis->auVFTA, sizeof(pThis->auVFTA));
6925 pHlp->pfnSSMPutU64(pSSM, pThis->u64AckedAt);
6926 pHlp->pfnSSMPutU16(pSSM, pThis->u16RxBSize);
6927 //pHlp->pfnSSMPutBool(pSSM, pThis->fDelayInts);
6928 //pHlp->pfnSSMPutBool(pSSM, pThis->fIntMaskUsed);
6929 pHlp->pfnSSMPutU16(pSSM, pThis->u16TxPktLen);
6930/** @todo State wrt to the TSE buffer is incomplete, so little point in
6931 * saving this actually. */
6932 pHlp->pfnSSMPutMem(pSSM, pThis->aTxPacketFallback, pThis->u16TxPktLen);
6933 pHlp->pfnSSMPutBool(pSSM, pThis->fIPcsum);
6934 pHlp->pfnSSMPutBool(pSSM, pThis->fTCPcsum);
6935 pHlp->pfnSSMPutMem(pSSM, &pThis->contextTSE, sizeof(pThis->contextTSE));
6936 pHlp->pfnSSMPutMem(pSSM, &pThis->contextNormal, sizeof(pThis->contextNormal));
6937 pHlp->pfnSSMPutBool(pSSM, pThis->fVTag);
6938 pHlp->pfnSSMPutU16(pSSM, pThis->u16VTagTCI);
6939#ifdef E1K_WITH_TXD_CACHE
6940# if 0
6941 pHlp->pfnSSMPutU8(pSSM, pThis->nTxDFetched);
6942 pHlp->pfnSSMPutMem(pSSM, pThis->aTxDescriptors,
6943 pThis->nTxDFetched * sizeof(pThis->aTxDescriptors[0]));
6944# else
6945 /*
6946 * There is no point in storing TX descriptor cache entries as we can simply
6947 * fetch them again. Moreover, normally the cache is always empty when we
6948 * save the state. Store zero entries for compatibility.
6949 */
6950 pHlp->pfnSSMPutU8(pSSM, 0);
6951# endif
6952#endif /* E1K_WITH_TXD_CACHE */
6953/** @todo GSO requires some more state here. */
6954 E1kLog(("%s State has been saved\n", pThis->szPrf));
6955 return VINF_SUCCESS;
6956}
6957
6958#if 0
6959/**
6960 * @callback_method_impl{FNSSMDEVSAVEDONE}
6961 */
6962static DECLCALLBACK(int) e1kSaveDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
6963{
6964 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6965
6966 /* If VM is being powered off unlocking will result in assertions in PGM */
6967 if (PDMDevHlpGetVM(pDevIns)->enmVMState == VMSTATE_RUNNING)
6968 pThis->fLocked = false;
6969 else
6970 E1kLog(("%s VM is not running -- remain locked\n", pThis->szPrf));
6971 E1kLog(("%s Unlocked\n", pThis->szPrf));
6972 return VINF_SUCCESS;
6973}
6974#endif
6975
6976/**
6977 * @callback_method_impl{FNSSMDEVLOADPREP,Synchronize.}
6978 */
6979static DECLCALLBACK(int) e1kLoadPrep(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
6980{
6981 RT_NOREF(pSSM);
6982 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6983
6984 int rc = e1kCsEnter(pThis, VERR_SEM_BUSY);
6985 if (RT_UNLIKELY(rc != VINF_SUCCESS))
6986 return rc;
6987 e1kCsLeave(pThis);
6988 return VINF_SUCCESS;
6989}
6990
6991/**
6992 * @callback_method_impl{FNSSMDEVLOADEXEC}
6993 */
6994static DECLCALLBACK(int) e1kLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
6995{
6996 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
6997 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
6998 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
6999 int rc;
7000
7001 if ( uVersion != E1K_SAVEDSTATE_VERSION
7002#ifdef E1K_WITH_TXD_CACHE
7003 && uVersion != E1K_SAVEDSTATE_VERSION_VBOX_42_VTAG
7004#endif /* E1K_WITH_TXD_CACHE */
7005 && uVersion != E1K_SAVEDSTATE_VERSION_VBOX_41
7006 && uVersion != E1K_SAVEDSTATE_VERSION_VBOX_30)
7007 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
7008
7009 if ( uVersion > E1K_SAVEDSTATE_VERSION_VBOX_30
7010 || uPass != SSM_PASS_FINAL)
7011 {
7012 /* config checks */
7013 RTMAC macConfigured;
7014 rc = pHlp->pfnSSMGetMem(pSSM, &macConfigured, sizeof(macConfigured));
7015 AssertRCReturn(rc, rc);
7016 if ( memcmp(&macConfigured, &pThis->macConfigured, sizeof(macConfigured))
7017 && (uPass == 0 || !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)) )
7018 LogRel(("%s: The mac address differs: config=%RTmac saved=%RTmac\n", pThis->szPrf, &pThis->macConfigured, &macConfigured));
7019
7020 E1KCHIP eChip;
7021 rc = pHlp->pfnSSMGetU32(pSSM, &eChip);
7022 AssertRCReturn(rc, rc);
7023 if (eChip != pThis->eChip)
7024 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("The chip type differs: config=%u saved=%u"), pThis->eChip, eChip);
7025 }
7026
7027 if (uPass == SSM_PASS_FINAL)
7028 {
7029 if (uVersion > E1K_SAVEDSTATE_VERSION_VBOX_30)
7030 {
7031 rc = pThisCC->eeprom.load(pHlp, pSSM);
7032 AssertRCReturn(rc, rc);
7033 }
7034 /* the state */
7035 pHlp->pfnSSMGetMem(pSSM, &pThis->auRegs, sizeof(pThis->auRegs));
7036 pHlp->pfnSSMGetBool(pSSM, &pThis->fIntRaised);
7037 /** @todo PHY could be made a separate device with its own versioning */
7038 Phy::loadState(pHlp, pSSM, &pThis->phy);
7039 pHlp->pfnSSMGetU32(pSSM, &pThis->uSelectedReg);
7040 pHlp->pfnSSMGetMem(pSSM, &pThis->auMTA, sizeof(pThis->auMTA));
7041 pHlp->pfnSSMGetMem(pSSM, &pThis->aRecAddr, sizeof(pThis->aRecAddr));
7042 pHlp->pfnSSMGetMem(pSSM, &pThis->auVFTA, sizeof(pThis->auVFTA));
7043 pHlp->pfnSSMGetU64(pSSM, &pThis->u64AckedAt);
7044 pHlp->pfnSSMGetU16(pSSM, &pThis->u16RxBSize);
7045 //pHlp->pfnSSMGetBool(pSSM, pThis->fDelayInts);
7046 //pHlp->pfnSSMGetBool(pSSM, pThis->fIntMaskUsed);
7047 rc = pHlp->pfnSSMGetU16(pSSM, &pThis->u16TxPktLen);
7048 AssertRCReturn(rc, rc);
7049 if (pThis->u16TxPktLen > sizeof(pThis->aTxPacketFallback))
7050 pThis->u16TxPktLen = sizeof(pThis->aTxPacketFallback);
7051 pHlp->pfnSSMGetMem(pSSM, &pThis->aTxPacketFallback[0], pThis->u16TxPktLen);
7052 pHlp->pfnSSMGetBool(pSSM, &pThis->fIPcsum);
7053 pHlp->pfnSSMGetBool(pSSM, &pThis->fTCPcsum);
7054 pHlp->pfnSSMGetMem(pSSM, &pThis->contextTSE, sizeof(pThis->contextTSE));
7055 rc = pHlp->pfnSSMGetMem(pSSM, &pThis->contextNormal, sizeof(pThis->contextNormal));
7056 AssertRCReturn(rc, rc);
7057 if (uVersion > E1K_SAVEDSTATE_VERSION_VBOX_41)
7058 {
7059 pHlp->pfnSSMGetBool(pSSM, &pThis->fVTag);
7060 rc = pHlp->pfnSSMGetU16(pSSM, &pThis->u16VTagTCI);
7061 AssertRCReturn(rc, rc);
7062 }
7063 else
7064 {
7065 pThis->fVTag = false;
7066 pThis->u16VTagTCI = 0;
7067 }
7068#ifdef E1K_WITH_TXD_CACHE
7069 if (uVersion > E1K_SAVEDSTATE_VERSION_VBOX_42_VTAG)
7070 {
7071 rc = pHlp->pfnSSMGetU8(pSSM, &pThis->nTxDFetched);
7072 AssertRCReturn(rc, rc);
7073 if (pThis->nTxDFetched)
7074 pHlp->pfnSSMGetMem(pSSM, pThis->aTxDescriptors,
7075 pThis->nTxDFetched * sizeof(pThis->aTxDescriptors[0]));
7076 }
7077 else
7078 pThis->nTxDFetched = 0;
7079 /**
7080 * @todo Perhaps we should not store TXD cache as the entries can be
7081 * simply fetched again from guest's memory. Or can't they?
7082 */
7083#endif /* E1K_WITH_TXD_CACHE */
7084#ifdef E1K_WITH_RXD_CACHE
7085 /*
7086 * There is no point in storing the RX descriptor cache in the saved
7087 * state, we just need to make sure it is empty.
7088 */
7089 pThis->iRxDCurrent = pThis->nRxDFetched = 0;
7090#endif /* E1K_WITH_RXD_CACHE */
7091 rc = pHlp->pfnSSMHandleGetStatus(pSSM);
7092 AssertRCReturn(rc, rc);
7093
7094 /* derived state */
7095 e1kSetupGsoCtx(&pThis->GsoCtx, &pThis->contextTSE);
7096
7097 E1kLog(("%s State has been restored\n", pThis->szPrf));
7098 e1kDumpState(pThis);
7099 }
7100 return VINF_SUCCESS;
7101}
7102
7103/**
7104 * @callback_method_impl{FNSSMDEVLOADDONE, Link status adjustments after loading.}
7105 */
7106static DECLCALLBACK(int) e1kLoadDone(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
7107{
7108 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
7109 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
7110 RT_NOREF(pSSM);
7111
7112 /* Update promiscuous mode */
7113 if (pThisCC->pDrvR3)
7114 pThisCC->pDrvR3->pfnSetPromiscuousMode(pThisCC->pDrvR3, !!(RCTL & (RCTL_UPE | RCTL_MPE)));
7115
7116 /*
7117 * Force the link down here, since PDMNETWORKLINKSTATE_DOWN_RESUME is never
7118 * passed to us. We go through all this stuff if the link was up and we
7119 * wasn't teleported.
7120 */
7121 if ( (STATUS & STATUS_LU)
7122 && !PDMDevHlpVMTeleportedAndNotFullyResumedYet(pDevIns)
7123 && pThis->cMsLinkUpDelay)
7124 {
7125 e1kR3LinkDownTemp(pDevIns, pThis, pThisCC);
7126 }
7127 return VINF_SUCCESS;
7128}
7129
7130
7131
7132/* -=-=-=-=- Debug Info + Log Types -=-=-=-=- */
7133
7134/**
7135 * @callback_method_impl{FNRTSTRFORMATTYPE}
7136 */
7137static DECLCALLBACK(size_t) e1kFmtRxDesc(PFNRTSTROUTPUT pfnOutput,
7138 void *pvArgOutput,
7139 const char *pszType,
7140 void const *pvValue,
7141 int cchWidth,
7142 int cchPrecision,
7143 unsigned fFlags,
7144 void *pvUser)
7145{
7146 RT_NOREF(cchWidth, cchPrecision, fFlags, pvUser);
7147 AssertReturn(strcmp(pszType, "e1krxd") == 0, 0);
7148 E1KRXDESC* pDesc = (E1KRXDESC*)pvValue;
7149 if (!pDesc)
7150 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "NULL_RXD");
7151
7152 size_t cbPrintf = 0;
7153 cbPrintf += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "Address=%16LX Length=%04X Csum=%04X\n",
7154 pDesc->u64BufAddr, pDesc->u16Length, pDesc->u16Checksum);
7155 cbPrintf += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, " STA: %s %s %s %s %s %s %s ERR: %s %s %s %s SPECIAL: %s VLAN=%03x PRI=%x",
7156 pDesc->status.fPIF ? "PIF" : "pif",
7157 pDesc->status.fIPCS ? "IPCS" : "ipcs",
7158 pDesc->status.fTCPCS ? "TCPCS" : "tcpcs",
7159 pDesc->status.fVP ? "VP" : "vp",
7160 pDesc->status.fIXSM ? "IXSM" : "ixsm",
7161 pDesc->status.fEOP ? "EOP" : "eop",
7162 pDesc->status.fDD ? "DD" : "dd",
7163 pDesc->status.fRXE ? "RXE" : "rxe",
7164 pDesc->status.fIPE ? "IPE" : "ipe",
7165 pDesc->status.fTCPE ? "TCPE" : "tcpe",
7166 pDesc->status.fCE ? "CE" : "ce",
7167 E1K_SPEC_CFI(pDesc->status.u16Special) ? "CFI" :"cfi",
7168 E1K_SPEC_VLAN(pDesc->status.u16Special),
7169 E1K_SPEC_PRI(pDesc->status.u16Special));
7170 return cbPrintf;
7171}
7172
7173/**
7174 * @callback_method_impl{FNRTSTRFORMATTYPE}
7175 */
7176static DECLCALLBACK(size_t) e1kFmtTxDesc(PFNRTSTROUTPUT pfnOutput,
7177 void *pvArgOutput,
7178 const char *pszType,
7179 void const *pvValue,
7180 int cchWidth,
7181 int cchPrecision,
7182 unsigned fFlags,
7183 void *pvUser)
7184{
7185 RT_NOREF(cchWidth, cchPrecision, fFlags, pvUser);
7186 AssertReturn(strcmp(pszType, "e1ktxd") == 0, 0);
7187 E1KTXDESC *pDesc = (E1KTXDESC*)pvValue;
7188 if (!pDesc)
7189 return RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "NULL_TXD");
7190
7191 size_t cbPrintf = 0;
7192 switch (e1kGetDescType(pDesc))
7193 {
7194 case E1K_DTYP_CONTEXT:
7195 cbPrintf += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "Type=Context\n"
7196 " IPCSS=%02X IPCSO=%02X IPCSE=%04X TUCSS=%02X TUCSO=%02X TUCSE=%04X\n"
7197 " TUCMD:%s%s%s %s %s PAYLEN=%04x HDRLEN=%04x MSS=%04x STA: %s",
7198 pDesc->context.ip.u8CSS, pDesc->context.ip.u8CSO, pDesc->context.ip.u16CSE,
7199 pDesc->context.tu.u8CSS, pDesc->context.tu.u8CSO, pDesc->context.tu.u16CSE,
7200 pDesc->context.dw2.fIDE ? " IDE":"",
7201 pDesc->context.dw2.fRS ? " RS" :"",
7202 pDesc->context.dw2.fTSE ? " TSE":"",
7203 pDesc->context.dw2.fIP ? "IPv4":"IPv6",
7204 pDesc->context.dw2.fTCP ? "TCP":"UDP",
7205 pDesc->context.dw2.u20PAYLEN,
7206 pDesc->context.dw3.u8HDRLEN,
7207 pDesc->context.dw3.u16MSS,
7208 pDesc->context.dw3.fDD?"DD":"");
7209 break;
7210 case E1K_DTYP_DATA:
7211 cbPrintf += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "Type=Data Address=%16LX DTALEN=%05X\n"
7212 " DCMD:%s%s%s%s%s%s%s STA:%s%s%s POPTS:%s%s SPECIAL:%s VLAN=%03x PRI=%x",
7213 pDesc->data.u64BufAddr,
7214 pDesc->data.cmd.u20DTALEN,
7215 pDesc->data.cmd.fIDE ? " IDE" :"",
7216 pDesc->data.cmd.fVLE ? " VLE" :"",
7217 pDesc->data.cmd.fRPS ? " RPS" :"",
7218 pDesc->data.cmd.fRS ? " RS" :"",
7219 pDesc->data.cmd.fTSE ? " TSE" :"",
7220 pDesc->data.cmd.fIFCS? " IFCS":"",
7221 pDesc->data.cmd.fEOP ? " EOP" :"",
7222 pDesc->data.dw3.fDD ? " DD" :"",
7223 pDesc->data.dw3.fEC ? " EC" :"",
7224 pDesc->data.dw3.fLC ? " LC" :"",
7225 pDesc->data.dw3.fTXSM? " TXSM":"",
7226 pDesc->data.dw3.fIXSM? " IXSM":"",
7227 E1K_SPEC_CFI(pDesc->data.dw3.u16Special) ? "CFI" :"cfi",
7228 E1K_SPEC_VLAN(pDesc->data.dw3.u16Special),
7229 E1K_SPEC_PRI(pDesc->data.dw3.u16Special));
7230 break;
7231 case E1K_DTYP_LEGACY:
7232 cbPrintf += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "Type=Legacy Address=%16LX DTALEN=%05X\n"
7233 " CMD:%s%s%s%s%s%s%s STA:%s%s%s CSO=%02x CSS=%02x SPECIAL:%s VLAN=%03x PRI=%x",
7234 pDesc->data.u64BufAddr,
7235 pDesc->legacy.cmd.u16Length,
7236 pDesc->legacy.cmd.fIDE ? " IDE" :"",
7237 pDesc->legacy.cmd.fVLE ? " VLE" :"",
7238 pDesc->legacy.cmd.fRPS ? " RPS" :"",
7239 pDesc->legacy.cmd.fRS ? " RS" :"",
7240 pDesc->legacy.cmd.fIC ? " IC" :"",
7241 pDesc->legacy.cmd.fIFCS? " IFCS":"",
7242 pDesc->legacy.cmd.fEOP ? " EOP" :"",
7243 pDesc->legacy.dw3.fDD ? " DD" :"",
7244 pDesc->legacy.dw3.fEC ? " EC" :"",
7245 pDesc->legacy.dw3.fLC ? " LC" :"",
7246 pDesc->legacy.cmd.u8CSO,
7247 pDesc->legacy.dw3.u8CSS,
7248 E1K_SPEC_CFI(pDesc->legacy.dw3.u16Special) ? "CFI" :"cfi",
7249 E1K_SPEC_VLAN(pDesc->legacy.dw3.u16Special),
7250 E1K_SPEC_PRI(pDesc->legacy.dw3.u16Special));
7251 break;
7252 default:
7253 cbPrintf += RTStrFormat(pfnOutput, pvArgOutput, NULL, 0, "Invalid Transmit Descriptor");
7254 break;
7255 }
7256
7257 return cbPrintf;
7258}
7259
7260/** Initializes debug helpers (logging format types). */
7261static int e1kInitDebugHelpers(void)
7262{
7263 int rc = VINF_SUCCESS;
7264 static bool s_fHelpersRegistered = false;
7265 if (!s_fHelpersRegistered)
7266 {
7267 s_fHelpersRegistered = true;
7268 rc = RTStrFormatTypeRegister("e1krxd", e1kFmtRxDesc, NULL);
7269 AssertRCReturn(rc, rc);
7270 rc = RTStrFormatTypeRegister("e1ktxd", e1kFmtTxDesc, NULL);
7271 AssertRCReturn(rc, rc);
7272 }
7273 return rc;
7274}
7275
7276/**
7277 * Status info callback.
7278 *
7279 * @param pDevIns The device instance.
7280 * @param pHlp The output helpers.
7281 * @param pszArgs The arguments.
7282 */
7283static DECLCALLBACK(void) e1kInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
7284{
7285 RT_NOREF(pszArgs);
7286 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
7287 unsigned i;
7288 // bool fRcvRing = false;
7289 // bool fXmtRing = false;
7290
7291 /*
7292 * Parse args.
7293 if (pszArgs)
7294 {
7295 fRcvRing = strstr(pszArgs, "verbose") || strstr(pszArgs, "rcv");
7296 fXmtRing = strstr(pszArgs, "verbose") || strstr(pszArgs, "xmt");
7297 }
7298 */
7299
7300 /*
7301 * Show info.
7302 */
7303 pHlp->pfnPrintf(pHlp, "E1000 #%d: port=%04x mmio=%RGp mac-cfg=%RTmac %s%s%s\n",
7304 pDevIns->iInstance,
7305 PDMDevHlpIoPortGetMappingAddress(pDevIns, pThis->hIoPorts),
7306 PDMDevHlpMmioGetMappingAddress(pDevIns, pThis->hMmioRegion),
7307 &pThis->macConfigured, g_aChips[pThis->eChip].pcszName,
7308 pDevIns->fRCEnabled ? " RC" : "", pDevIns->fR0Enabled ? " R0" : "");
7309
7310 e1kCsEnter(pThis, VERR_INTERNAL_ERROR); /* Not sure why but PCNet does it */
7311
7312 for (i = 0; i < E1K_NUM_OF_32BIT_REGS; ++i)
7313 pHlp->pfnPrintf(pHlp, "%8.8s = %08x\n", g_aE1kRegMap[i].abbrev, pThis->auRegs[i]);
7314
7315 for (i = 0; i < RT_ELEMENTS(pThis->aRecAddr.array); i++)
7316 {
7317 E1KRAELEM* ra = pThis->aRecAddr.array + i;
7318 if (ra->ctl & RA_CTL_AV)
7319 {
7320 const char *pcszTmp;
7321 switch (ra->ctl & RA_CTL_AS)
7322 {
7323 case 0: pcszTmp = "DST"; break;
7324 case 1: pcszTmp = "SRC"; break;
7325 default: pcszTmp = "reserved";
7326 }
7327 pHlp->pfnPrintf(pHlp, "RA%02d: %s %RTmac\n", i, pcszTmp, ra->addr);
7328 }
7329 }
7330 unsigned cDescs = RDLEN / sizeof(E1KRXDESC);
7331 uint32_t rdh = RDH;
7332 pHlp->pfnPrintf(pHlp, "\n-- Receive Descriptors (%d total) --\n", cDescs);
7333 for (i = 0; i < cDescs; ++i)
7334 {
7335 E1KRXDESC desc;
7336 PDMDevHlpPCIPhysRead(pDevIns, e1kDescAddr(RDBAH, RDBAL, i),
7337 &desc, sizeof(desc));
7338 if (i == rdh)
7339 pHlp->pfnPrintf(pHlp, ">>> ");
7340 pHlp->pfnPrintf(pHlp, "%RGp: %R[e1krxd]\n", e1kDescAddr(RDBAH, RDBAL, i), &desc);
7341 }
7342#ifdef E1K_WITH_RXD_CACHE
7343 pHlp->pfnPrintf(pHlp, "\n-- Receive Descriptors in Cache (at %d (RDH %d)/ fetched %d / max %d) --\n",
7344 pThis->iRxDCurrent, RDH, pThis->nRxDFetched, E1K_RXD_CACHE_SIZE);
7345 if (rdh > pThis->iRxDCurrent)
7346 rdh -= pThis->iRxDCurrent;
7347 else
7348 rdh = cDescs + rdh - pThis->iRxDCurrent;
7349 for (i = 0; i < pThis->nRxDFetched; ++i)
7350 {
7351 if (i == pThis->iRxDCurrent)
7352 pHlp->pfnPrintf(pHlp, ">>> ");
7353 if (cDescs)
7354 pHlp->pfnPrintf(pHlp, "%RGp: %R[e1krxd]\n",
7355 e1kDescAddr(RDBAH, RDBAL, rdh++ % cDescs),
7356 &pThis->aRxDescriptors[i]);
7357 else
7358 pHlp->pfnPrintf(pHlp, "<lost>: %R[e1krxd]\n",
7359 &pThis->aRxDescriptors[i]);
7360 }
7361#endif /* E1K_WITH_RXD_CACHE */
7362
7363 cDescs = TDLEN / sizeof(E1KTXDESC);
7364 uint32_t tdh = TDH;
7365 pHlp->pfnPrintf(pHlp, "\n-- Transmit Descriptors (%d total) --\n", cDescs);
7366 for (i = 0; i < cDescs; ++i)
7367 {
7368 E1KTXDESC desc;
7369 PDMDevHlpPCIPhysRead(pDevIns, e1kDescAddr(TDBAH, TDBAL, i),
7370 &desc, sizeof(desc));
7371 if (i == tdh)
7372 pHlp->pfnPrintf(pHlp, ">>> ");
7373 pHlp->pfnPrintf(pHlp, "%RGp: %R[e1ktxd]\n", e1kDescAddr(TDBAH, TDBAL, i), &desc);
7374 }
7375#ifdef E1K_WITH_TXD_CACHE
7376 pHlp->pfnPrintf(pHlp, "\n-- Transmit Descriptors in Cache (at %d (TDH %d)/ fetched %d / max %d) --\n",
7377 pThis->iTxDCurrent, TDH, pThis->nTxDFetched, E1K_TXD_CACHE_SIZE);
7378 if (tdh > pThis->iTxDCurrent)
7379 tdh -= pThis->iTxDCurrent;
7380 else
7381 tdh = cDescs + tdh - pThis->iTxDCurrent;
7382 for (i = 0; i < pThis->nTxDFetched; ++i)
7383 {
7384 if (i == pThis->iTxDCurrent)
7385 pHlp->pfnPrintf(pHlp, ">>> ");
7386 if (cDescs)
7387 pHlp->pfnPrintf(pHlp, "%RGp: %R[e1ktxd]\n",
7388 e1kDescAddr(TDBAH, TDBAL, tdh++ % cDescs),
7389 &pThis->aTxDescriptors[i]);
7390 else
7391 pHlp->pfnPrintf(pHlp, "<lost>: %R[e1ktxd]\n",
7392 &pThis->aTxDescriptors[i]);
7393 }
7394#endif /* E1K_WITH_TXD_CACHE */
7395
7396
7397#ifdef E1K_INT_STATS
7398 pHlp->pfnPrintf(pHlp, "Interrupt attempts: %d\n", pThis->uStatIntTry);
7399 pHlp->pfnPrintf(pHlp, "Interrupts raised : %d\n", pThis->uStatInt);
7400 pHlp->pfnPrintf(pHlp, "Interrupts lowered: %d\n", pThis->uStatIntLower);
7401 pHlp->pfnPrintf(pHlp, "ICR outside ISR : %d\n", pThis->uStatNoIntICR);
7402 pHlp->pfnPrintf(pHlp, "IMS raised ints : %d\n", pThis->uStatIntIMS);
7403 pHlp->pfnPrintf(pHlp, "Interrupts skipped: %d\n", pThis->uStatIntSkip);
7404 pHlp->pfnPrintf(pHlp, "Masked interrupts : %d\n", pThis->uStatIntMasked);
7405 pHlp->pfnPrintf(pHlp, "Early interrupts : %d\n", pThis->uStatIntEarly);
7406 pHlp->pfnPrintf(pHlp, "Late interrupts : %d\n", pThis->uStatIntLate);
7407 pHlp->pfnPrintf(pHlp, "Lost interrupts : %d\n", pThis->iStatIntLost);
7408 pHlp->pfnPrintf(pHlp, "Interrupts by RX : %d\n", pThis->uStatIntRx);
7409 pHlp->pfnPrintf(pHlp, "Interrupts by TX : %d\n", pThis->uStatIntTx);
7410 pHlp->pfnPrintf(pHlp, "Interrupts by ICS : %d\n", pThis->uStatIntICS);
7411 pHlp->pfnPrintf(pHlp, "Interrupts by RDTR: %d\n", pThis->uStatIntRDTR);
7412 pHlp->pfnPrintf(pHlp, "Interrupts by RDMT: %d\n", pThis->uStatIntRXDMT0);
7413 pHlp->pfnPrintf(pHlp, "Interrupts by TXQE: %d\n", pThis->uStatIntTXQE);
7414 pHlp->pfnPrintf(pHlp, "TX int delay asked: %d\n", pThis->uStatTxIDE);
7415 pHlp->pfnPrintf(pHlp, "TX delayed: %d\n", pThis->uStatTxDelayed);
7416 pHlp->pfnPrintf(pHlp, "TX delayed expired: %d\n", pThis->uStatTxDelayExp);
7417 pHlp->pfnPrintf(pHlp, "TX no report asked: %d\n", pThis->uStatTxNoRS);
7418 pHlp->pfnPrintf(pHlp, "TX abs timer expd : %d\n", pThis->uStatTAD);
7419 pHlp->pfnPrintf(pHlp, "TX int timer expd : %d\n", pThis->uStatTID);
7420 pHlp->pfnPrintf(pHlp, "RX abs timer expd : %d\n", pThis->uStatRAD);
7421 pHlp->pfnPrintf(pHlp, "RX int timer expd : %d\n", pThis->uStatRID);
7422 pHlp->pfnPrintf(pHlp, "TX CTX descriptors: %d\n", pThis->uStatDescCtx);
7423 pHlp->pfnPrintf(pHlp, "TX DAT descriptors: %d\n", pThis->uStatDescDat);
7424 pHlp->pfnPrintf(pHlp, "TX LEG descriptors: %d\n", pThis->uStatDescLeg);
7425 pHlp->pfnPrintf(pHlp, "Received frames : %d\n", pThis->uStatRxFrm);
7426 pHlp->pfnPrintf(pHlp, "Transmitted frames: %d\n", pThis->uStatTxFrm);
7427 pHlp->pfnPrintf(pHlp, "TX frames up to 1514: %d\n", pThis->uStatTx1514);
7428 pHlp->pfnPrintf(pHlp, "TX frames up to 2962: %d\n", pThis->uStatTx2962);
7429 pHlp->pfnPrintf(pHlp, "TX frames up to 4410: %d\n", pThis->uStatTx4410);
7430 pHlp->pfnPrintf(pHlp, "TX frames up to 5858: %d\n", pThis->uStatTx5858);
7431 pHlp->pfnPrintf(pHlp, "TX frames up to 7306: %d\n", pThis->uStatTx7306);
7432 pHlp->pfnPrintf(pHlp, "TX frames up to 8754: %d\n", pThis->uStatTx8754);
7433 pHlp->pfnPrintf(pHlp, "TX frames up to 16384: %d\n", pThis->uStatTx16384);
7434 pHlp->pfnPrintf(pHlp, "TX frames up to 32768: %d\n", pThis->uStatTx32768);
7435 pHlp->pfnPrintf(pHlp, "Larger TX frames : %d\n", pThis->uStatTxLarge);
7436#endif /* E1K_INT_STATS */
7437
7438 e1kCsLeave(pThis);
7439}
7440
7441
7442
7443/* -=-=-=-=- PDMDEVREG -=-=-=-=- */
7444
7445/**
7446 * Detach notification.
7447 *
7448 * One port on the network card has been disconnected from the network.
7449 *
7450 * @param pDevIns The device instance.
7451 * @param iLUN The logical unit which is being detached.
7452 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
7453 */
7454static DECLCALLBACK(void) e1kR3Detach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
7455{
7456 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
7457 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
7458 Log(("%s e1kR3Detach:\n", pThis->szPrf));
7459 RT_NOREF(fFlags);
7460
7461 AssertLogRelReturnVoid(iLUN == 0);
7462
7463 PDMDevHlpCritSectEnter(pDevIns, &pThis->cs, VERR_SEM_BUSY);
7464
7465 /** @todo r=pritesh still need to check if i missed
7466 * to clean something in this function
7467 */
7468
7469 /*
7470 * Zero some important members.
7471 */
7472 pThisCC->pDrvBase = NULL;
7473 pThisCC->pDrvR3 = NULL;
7474#if 0 /** @todo @bugref{9218} ring-0 driver stuff */
7475 pThisR0->pDrvR0 = NIL_RTR0PTR;
7476 pThisRC->pDrvRC = NIL_RTRCPTR;
7477#endif
7478
7479 PDMDevHlpCritSectLeave(pDevIns, &pThis->cs);
7480}
7481
7482/**
7483 * Attach the Network attachment.
7484 *
7485 * One port on the network card has been connected to a network.
7486 *
7487 * @returns VBox status code.
7488 * @param pDevIns The device instance.
7489 * @param iLUN The logical unit which is being attached.
7490 * @param fFlags Flags, combination of the PDMDEVATT_FLAGS_* \#defines.
7491 *
7492 * @remarks This code path is not used during construction.
7493 */
7494static DECLCALLBACK(int) e1kR3Attach(PPDMDEVINS pDevIns, unsigned iLUN, uint32_t fFlags)
7495{
7496 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
7497 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
7498 LogFlow(("%s e1kR3Attach:\n", pThis->szPrf));
7499 RT_NOREF(fFlags);
7500
7501 AssertLogRelReturn(iLUN == 0, VERR_PDM_NO_SUCH_LUN);
7502
7503 PDMDevHlpCritSectEnter(pDevIns, &pThis->cs, VERR_SEM_BUSY);
7504
7505 /*
7506 * Attach the driver.
7507 */
7508 int rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
7509 if (RT_SUCCESS(rc))
7510 {
7511 pThisCC->pDrvR3 = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
7512 AssertMsgStmt(pThisCC->pDrvR3, ("Failed to obtain the PDMINETWORKUP interface!\n"),
7513 rc = VERR_PDM_MISSING_INTERFACE_BELOW);
7514 if (RT_SUCCESS(rc))
7515 {
7516#if 0 /** @todo @bugref{9218} ring-0 driver stuff */
7517 pThisR0->pDrvR0 = PDMIBASER0_QUERY_INTERFACE(PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMIBASER0), PDMINETWORKUP);
7518 pThisRC->pDrvRC = PDMIBASERC_QUERY_INTERFACE(PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMIBASERC), PDMINETWORKUP);
7519#endif
7520 }
7521 }
7522 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
7523 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
7524 {
7525 /* This should never happen because this function is not called
7526 * if there is no driver to attach! */
7527 Log(("%s No attached driver!\n", pThis->szPrf));
7528 }
7529
7530 /*
7531 * Temporary set the link down if it was up so that the guest will know
7532 * that we have change the configuration of the network card
7533 */
7534 if ((STATUS & STATUS_LU) && RT_SUCCESS(rc))
7535 e1kR3LinkDownTemp(pDevIns, pThis, pThisCC);
7536
7537 PDMDevHlpCritSectLeave(pDevIns, &pThis->cs);
7538 return rc;
7539}
7540
7541/**
7542 * @copydoc FNPDMDEVPOWEROFF
7543 */
7544static DECLCALLBACK(void) e1kR3PowerOff(PPDMDEVINS pDevIns)
7545{
7546 /* Poke thread waiting for buffer space. */
7547 e1kWakeupReceive(pDevIns, PDMDEVINS_2_DATA(pDevIns, PE1KSTATE));
7548}
7549
7550/**
7551 * @copydoc FNPDMDEVRESET
7552 */
7553static DECLCALLBACK(void) e1kR3Reset(PPDMDEVINS pDevIns)
7554{
7555 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
7556 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
7557#ifdef E1K_TX_DELAY
7558 e1kCancelTimer(pDevIns, pThis, pThis->hTXDTimer);
7559#endif /* E1K_TX_DELAY */
7560 e1kCancelTimer(pDevIns, pThis, pThis->hIntTimer);
7561 e1kCancelTimer(pDevIns, pThis, pThis->hLUTimer);
7562 e1kXmitFreeBuf(pThis, pThisCC);
7563 pThis->u16TxPktLen = 0;
7564 pThis->fIPcsum = false;
7565 pThis->fTCPcsum = false;
7566 pThis->fIntMaskUsed = false;
7567 pThis->fDelayInts = false;
7568 pThis->fLocked = false;
7569 pThis->u64AckedAt = 0;
7570 e1kR3HardReset(pDevIns, pThis, pThisCC);
7571}
7572
7573/**
7574 * @copydoc FNPDMDEVSUSPEND
7575 */
7576static DECLCALLBACK(void) e1kR3Suspend(PPDMDEVINS pDevIns)
7577{
7578 /* Poke thread waiting for buffer space. */
7579 e1kWakeupReceive(pDevIns, PDMDEVINS_2_DATA(pDevIns, PE1KSTATE));
7580}
7581
7582/**
7583 * Device relocation callback.
7584 *
7585 * When this callback is called the device instance data, and if the
7586 * device have a GC component, is being relocated, or/and the selectors
7587 * have been changed. The device must use the chance to perform the
7588 * necessary pointer relocations and data updates.
7589 *
7590 * Before the GC code is executed the first time, this function will be
7591 * called with a 0 delta so GC pointer calculations can be one in one place.
7592 *
7593 * @param pDevIns Pointer to the device instance.
7594 * @param offDelta The relocation delta relative to the old location.
7595 *
7596 * @remark A relocation CANNOT fail.
7597 */
7598static DECLCALLBACK(void) e1kR3Relocate(PPDMDEVINS pDevIns, RTGCINTPTR offDelta)
7599{
7600 PE1KSTATERC pThisRC = PDMINS_2_DATA_RC(pDevIns, PE1KSTATERC);
7601 if (pThisRC)
7602 pThisRC->pDevInsRC = PDMDEVINS_2_RCPTR(pDevIns);
7603 RT_NOREF(offDelta);
7604}
7605
7606/**
7607 * Destruct a device instance.
7608 *
7609 * We need to free non-VM resources only.
7610 *
7611 * @returns VBox status code.
7612 * @param pDevIns The device instance data.
7613 * @thread EMT
7614 */
7615static DECLCALLBACK(int) e1kR3Destruct(PPDMDEVINS pDevIns)
7616{
7617 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
7618 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
7619
7620 e1kDumpState(pThis);
7621 E1kLog(("%s Destroying instance\n", pThis->szPrf));
7622 if (PDMDevHlpCritSectIsInitialized(pDevIns, &pThis->cs))
7623 {
7624 if (pThis->hEventMoreRxDescAvail != NIL_SUPSEMEVENT)
7625 {
7626 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEventMoreRxDescAvail);
7627 RTThreadYield();
7628 PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEventMoreRxDescAvail);
7629 pThis->hEventMoreRxDescAvail = NIL_SUPSEMEVENT;
7630 }
7631#ifdef E1K_WITH_TX_CS
7632 PDMDevHlpCritSectDelete(pDevIns, &pThis->csTx);
7633#endif /* E1K_WITH_TX_CS */
7634 PDMDevHlpCritSectDelete(pDevIns, &pThis->csRx);
7635 PDMDevHlpCritSectDelete(pDevIns, &pThis->cs);
7636 }
7637 return VINF_SUCCESS;
7638}
7639
7640
7641/**
7642 * Set PCI configuration space registers.
7643 *
7644 * @param pci Reference to PCI device structure.
7645 * @thread EMT
7646 */
7647static void e1kR3ConfigurePciDev(PPDMPCIDEV pPciDev, E1KCHIP eChip)
7648{
7649 Assert(eChip < RT_ELEMENTS(g_aChips));
7650 /* Configure PCI Device, assume 32-bit mode ******************************/
7651 PDMPciDevSetVendorId(pPciDev, g_aChips[eChip].uPCIVendorId);
7652 PDMPciDevSetDeviceId(pPciDev, g_aChips[eChip].uPCIDeviceId);
7653 PDMPciDevSetWord( pPciDev, VBOX_PCI_SUBSYSTEM_VENDOR_ID, g_aChips[eChip].uPCISubsystemVendorId);
7654 PDMPciDevSetWord( pPciDev, VBOX_PCI_SUBSYSTEM_ID, g_aChips[eChip].uPCISubsystemId);
7655
7656 PDMPciDevSetWord( pPciDev, VBOX_PCI_COMMAND, 0x0000);
7657 /* DEVSEL Timing (medium device), 66 MHz Capable, New capabilities */
7658 PDMPciDevSetWord( pPciDev, VBOX_PCI_STATUS,
7659 VBOX_PCI_STATUS_DEVSEL_MEDIUM | VBOX_PCI_STATUS_CAP_LIST | VBOX_PCI_STATUS_66MHZ);
7660 /* Stepping A2 */
7661 PDMPciDevSetByte( pPciDev, VBOX_PCI_REVISION_ID, 0x02);
7662 /* Ethernet adapter */
7663 PDMPciDevSetByte( pPciDev, VBOX_PCI_CLASS_PROG, 0x00);
7664 PDMPciDevSetWord( pPciDev, VBOX_PCI_CLASS_DEVICE, 0x0200);
7665 /* normal single function Ethernet controller */
7666 PDMPciDevSetByte( pPciDev, VBOX_PCI_HEADER_TYPE, 0x00);
7667 /* Memory Register Base Address */
7668 PDMPciDevSetDWord(pPciDev, VBOX_PCI_BASE_ADDRESS_0, 0x00000000);
7669 /* Memory Flash Base Address */
7670 PDMPciDevSetDWord(pPciDev, VBOX_PCI_BASE_ADDRESS_1, 0x00000000);
7671 /* IO Register Base Address */
7672 PDMPciDevSetDWord(pPciDev, VBOX_PCI_BASE_ADDRESS_2, 0x00000001);
7673 /* Expansion ROM Base Address */
7674 PDMPciDevSetDWord(pPciDev, VBOX_PCI_ROM_ADDRESS, 0x00000000);
7675 /* Capabilities Pointer */
7676 PDMPciDevSetByte( pPciDev, VBOX_PCI_CAPABILITY_LIST, 0xDC);
7677 /* Interrupt Pin: INTA# */
7678 PDMPciDevSetByte( pPciDev, VBOX_PCI_INTERRUPT_PIN, 0x01);
7679 /* Max_Lat/Min_Gnt: very high priority and time slice */
7680 PDMPciDevSetByte( pPciDev, VBOX_PCI_MIN_GNT, 0xFF);
7681 PDMPciDevSetByte( pPciDev, VBOX_PCI_MAX_LAT, 0x00);
7682
7683 /* PCI Power Management Registers ****************************************/
7684 /* Capability ID: PCI Power Management Registers */
7685 PDMPciDevSetByte( pPciDev, 0xDC, VBOX_PCI_CAP_ID_PM);
7686 /* Next Item Pointer: PCI-X */
7687 PDMPciDevSetByte( pPciDev, 0xDC + 1, 0xE4);
7688 /* Power Management Capabilities: PM disabled, DSI */
7689 PDMPciDevSetWord( pPciDev, 0xDC + 2,
7690 0x0002 | VBOX_PCI_PM_CAP_DSI);
7691 /* Power Management Control / Status Register: PM disabled */
7692 PDMPciDevSetWord( pPciDev, 0xDC + 4, 0x0000);
7693 /* PMCSR_BSE Bridge Support Extensions: Not supported */
7694 PDMPciDevSetByte( pPciDev, 0xDC + 6, 0x00);
7695 /* Data Register: PM disabled, always 0 */
7696 PDMPciDevSetByte( pPciDev, 0xDC + 7, 0x00);
7697
7698 /* PCI-X Configuration Registers *****************************************/
7699 /* Capability ID: PCI-X Configuration Registers */
7700 PDMPciDevSetByte( pPciDev, 0xE4, VBOX_PCI_CAP_ID_PCIX);
7701#ifdef E1K_WITH_MSI
7702 PDMPciDevSetByte( pPciDev, 0xE4 + 1, 0x80);
7703#else
7704 /* Next Item Pointer: None (Message Signalled Interrupts are disabled) */
7705 PDMPciDevSetByte( pPciDev, 0xE4 + 1, 0x00);
7706#endif
7707 /* PCI-X Command: Enable Relaxed Ordering */
7708 PDMPciDevSetWord( pPciDev, 0xE4 + 2, VBOX_PCI_X_CMD_ERO);
7709 /* PCI-X Status: 32-bit, 66MHz*/
7710 /** @todo is this value really correct? fff8 doesn't look like actual PCI address */
7711 PDMPciDevSetDWord(pPciDev, 0xE4 + 4, 0x0040FFF8);
7712}
7713
7714/**
7715 * @interface_method_impl{PDMDEVREG,pfnConstruct}
7716 */
7717static DECLCALLBACK(int) e1kR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
7718{
7719 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
7720 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
7721 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
7722 int rc;
7723
7724 /*
7725 * Initialize the instance data (state).
7726 * Note! Caller has initialized it to ZERO already.
7727 */
7728 RTStrPrintf(pThis->szPrf, sizeof(pThis->szPrf), "E1000#%d", iInstance);
7729 E1kLog(("%s Constructing new instance sizeof(E1KRXDESC)=%d\n", pThis->szPrf, sizeof(E1KRXDESC)));
7730 pThis->hEventMoreRxDescAvail = NIL_SUPSEMEVENT;
7731 pThis->u16TxPktLen = 0;
7732 pThis->fIPcsum = false;
7733 pThis->fTCPcsum = false;
7734 pThis->fIntMaskUsed = false;
7735 pThis->fDelayInts = false;
7736 pThis->fLocked = false;
7737 pThis->u64AckedAt = 0;
7738 pThis->led.u32Magic = PDMLED_MAGIC;
7739 pThis->u32PktNo = 1;
7740
7741 pThisCC->pDevInsR3 = pDevIns;
7742 pThisCC->pShared = pThis;
7743
7744 /* Interfaces */
7745 pThisCC->IBase.pfnQueryInterface = e1kR3QueryInterface;
7746
7747 pThisCC->INetworkDown.pfnWaitReceiveAvail = e1kR3NetworkDown_WaitReceiveAvail;
7748 pThisCC->INetworkDown.pfnReceive = e1kR3NetworkDown_Receive;
7749 pThisCC->INetworkDown.pfnXmitPending = e1kR3NetworkDown_XmitPending;
7750
7751 pThisCC->ILeds.pfnQueryStatusLed = e1kR3QueryStatusLed;
7752
7753 pThisCC->INetworkConfig.pfnGetMac = e1kR3GetMac;
7754 pThisCC->INetworkConfig.pfnGetLinkState = e1kR3GetLinkState;
7755 pThisCC->INetworkConfig.pfnSetLinkState = e1kR3SetLinkState;
7756
7757 /*
7758 * Internal validations.
7759 */
7760 for (uint32_t iReg = 1; iReg < E1K_NUM_OF_BINARY_SEARCHABLE; iReg++)
7761 AssertLogRelMsgReturn( g_aE1kRegMap[iReg].offset > g_aE1kRegMap[iReg - 1].offset
7762 && g_aE1kRegMap[iReg].offset + g_aE1kRegMap[iReg].size
7763 >= g_aE1kRegMap[iReg - 1].offset + g_aE1kRegMap[iReg - 1].size,
7764 ("%s@%#xLB%#x vs %s@%#xLB%#x\n",
7765 g_aE1kRegMap[iReg].abbrev, g_aE1kRegMap[iReg].offset, g_aE1kRegMap[iReg].size,
7766 g_aE1kRegMap[iReg - 1].abbrev, g_aE1kRegMap[iReg - 1].offset, g_aE1kRegMap[iReg - 1].size),
7767 VERR_INTERNAL_ERROR_4);
7768
7769 /*
7770 * Validate configuration.
7771 */
7772 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns,
7773 "MAC|"
7774 "CableConnected|"
7775 "AdapterType|"
7776 "LineSpeed|"
7777 "ItrEnabled|"
7778 "ItrRxEnabled|"
7779 "EthernetCRC|"
7780 "GSOEnabled|"
7781 "LinkUpDelay|"
7782 "StatNo",
7783 "");
7784
7785 /** @todo LineSpeed unused! */
7786
7787 /*
7788 * Get config params
7789 */
7790 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
7791 rc = pHlp->pfnCFGMQueryBytes(pCfg, "MAC", pThis->macConfigured.au8, sizeof(pThis->macConfigured.au8));
7792 if (RT_FAILURE(rc))
7793 return PDMDEV_SET_ERROR(pDevIns, rc,
7794 N_("Configuration error: Failed to get MAC address"));
7795 rc = pHlp->pfnCFGMQueryBool(pCfg, "CableConnected", &pThis->fCableConnected);
7796 if (RT_FAILURE(rc))
7797 return PDMDEV_SET_ERROR(pDevIns, rc,
7798 N_("Configuration error: Failed to get the value of 'CableConnected'"));
7799 rc = pHlp->pfnCFGMQueryU32(pCfg, "AdapterType", (uint32_t*)&pThis->eChip);
7800 if (RT_FAILURE(rc))
7801 return PDMDEV_SET_ERROR(pDevIns, rc,
7802 N_("Configuration error: Failed to get the value of 'AdapterType'"));
7803 Assert(pThis->eChip <= E1K_CHIP_82545EM);
7804
7805 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "EthernetCRC", &pThis->fEthernetCRC, true);
7806 if (RT_FAILURE(rc))
7807 return PDMDEV_SET_ERROR(pDevIns, rc,
7808 N_("Configuration error: Failed to get the value of 'EthernetCRC'"));
7809
7810 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "GSOEnabled", &pThis->fGSOEnabled, true);
7811 if (RT_FAILURE(rc))
7812 return PDMDEV_SET_ERROR(pDevIns, rc,
7813 N_("Configuration error: Failed to get the value of 'GSOEnabled'"));
7814
7815 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "ItrEnabled", &pThis->fItrEnabled, false);
7816 if (RT_FAILURE(rc))
7817 return PDMDEV_SET_ERROR(pDevIns, rc,
7818 N_("Configuration error: Failed to get the value of 'ItrEnabled'"));
7819
7820 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "ItrRxEnabled", &pThis->fItrRxEnabled, true);
7821 if (RT_FAILURE(rc))
7822 return PDMDEV_SET_ERROR(pDevIns, rc,
7823 N_("Configuration error: Failed to get the value of 'ItrRxEnabled'"));
7824
7825 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "TidEnabled", &pThis->fTidEnabled, false);
7826 if (RT_FAILURE(rc))
7827 return PDMDEV_SET_ERROR(pDevIns, rc,
7828 N_("Configuration error: Failed to get the value of 'TidEnabled'"));
7829
7830 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "LinkUpDelay", (uint32_t*)&pThis->cMsLinkUpDelay, 3000); /* ms */
7831 if (RT_FAILURE(rc))
7832 return PDMDEV_SET_ERROR(pDevIns, rc,
7833 N_("Configuration error: Failed to get the value of 'LinkUpDelay'"));
7834 Assert(pThis->cMsLinkUpDelay <= 300000); /* less than 5 minutes */
7835 if (pThis->cMsLinkUpDelay > 5000)
7836 LogRel(("%s: WARNING! Link up delay is set to %u seconds!\n", pThis->szPrf, pThis->cMsLinkUpDelay / 1000));
7837 else if (pThis->cMsLinkUpDelay == 0)
7838 LogRel(("%s: WARNING! Link up delay is disabled!\n", pThis->szPrf));
7839
7840 uint32_t uStatNo = (uint32_t)iInstance;
7841 rc = pHlp->pfnCFGMQueryU32Def(pCfg, "StatNo", &uStatNo, (uint32_t)iInstance);
7842 if (RT_FAILURE(rc))
7843 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Configuration error: Failed to get the \"StatNo\" value"));
7844
7845 LogRel(("%s: Chip=%s LinkUpDelay=%ums EthernetCRC=%s GSO=%s Itr=%s ItrRx=%s TID=%s R0=%s RC=%s\n", pThis->szPrf,
7846 g_aChips[pThis->eChip].pcszName, pThis->cMsLinkUpDelay,
7847 pThis->fEthernetCRC ? "on" : "off",
7848 pThis->fGSOEnabled ? "enabled" : "disabled",
7849 pThis->fItrEnabled ? "enabled" : "disabled",
7850 pThis->fItrRxEnabled ? "enabled" : "disabled",
7851 pThis->fTidEnabled ? "enabled" : "disabled",
7852 pDevIns->fR0Enabled ? "enabled" : "disabled",
7853 pDevIns->fRCEnabled ? "enabled" : "disabled"));
7854
7855 /*
7856 * Initialize sub-components and register everything with the VMM.
7857 */
7858
7859 /* Initialize the EEPROM. */
7860 pThisCC->eeprom.init(pThis->macConfigured);
7861
7862 /* Initialize internal PHY. */
7863 Phy::init(&pThis->phy, iInstance, pThis->eChip == E1K_CHIP_82543GC ? PHY_EPID_M881000 : PHY_EPID_M881011);
7864
7865 /* Initialize critical sections. We do our own locking. */
7866 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
7867 AssertRCReturn(rc, rc);
7868
7869 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->cs, RT_SRC_POS, "E1000#%d", iInstance);
7870 AssertRCReturn(rc, rc);
7871 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->csRx, RT_SRC_POS, "E1000#%dRX", iInstance);
7872 AssertRCReturn(rc, rc);
7873#ifdef E1K_WITH_TX_CS
7874 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->csTx, RT_SRC_POS, "E1000#%dTX", iInstance);
7875 AssertRCReturn(rc, rc);
7876#endif
7877
7878 /* Saved state registration. */
7879 rc = PDMDevHlpSSMRegisterEx(pDevIns, E1K_SAVEDSTATE_VERSION, sizeof(E1KSTATE), NULL,
7880 NULL, e1kLiveExec, NULL,
7881 e1kSavePrep, e1kSaveExec, NULL,
7882 e1kLoadPrep, e1kLoadExec, e1kLoadDone);
7883 AssertRCReturn(rc, rc);
7884
7885 /* Set PCI config registers and register ourselves with the PCI bus. */
7886 PDMPCIDEV_ASSERT_VALID(pDevIns, pDevIns->apPciDevs[0]);
7887 e1kR3ConfigurePciDev(pDevIns->apPciDevs[0], pThis->eChip);
7888 rc = PDMDevHlpPCIRegister(pDevIns, pDevIns->apPciDevs[0]);
7889 AssertRCReturn(rc, rc);
7890
7891#ifdef E1K_WITH_MSI
7892 PDMMSIREG MsiReg;
7893 RT_ZERO(MsiReg);
7894 MsiReg.cMsiVectors = 1;
7895 MsiReg.iMsiCapOffset = 0x80;
7896 MsiReg.iMsiNextOffset = 0x0;
7897 MsiReg.fMsi64bit = false;
7898 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
7899 AssertRCReturn(rc, rc);
7900#endif
7901
7902 /*
7903 * Map our registers to memory space (region 0, see e1kR3ConfigurePciDev)
7904 * From the spec (regarding flags):
7905 * For registers that should be accessed as 32-bit double words,
7906 * partial writes (less than a 32-bit double word) is ignored.
7907 * Partial reads return all 32 bits of data regardless of the
7908 * byte enables.
7909 */
7910 rc = PDMDevHlpMmioCreateEx(pDevIns, E1K_MM_SIZE, IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_ONLY_DWORD,
7911 pDevIns->apPciDevs[0], 0 /*iPciRegion*/,
7912 e1kMMIOWrite, e1kMMIORead, NULL /*pfnFill*/, NULL /*pvUser*/, "E1000", &pThis->hMmioRegion);
7913 AssertRCReturn(rc, rc);
7914 rc = PDMDevHlpPCIIORegionRegisterMmio(pDevIns, 0, E1K_MM_SIZE, PCI_ADDRESS_SPACE_MEM, pThis->hMmioRegion, NULL);
7915 AssertRCReturn(rc, rc);
7916
7917 /* Map our registers to IO space (region 2, see e1kR3ConfigurePciDev) */
7918 static IOMIOPORTDESC const s_aExtDescs[] =
7919 {
7920 { "IOADDR", "IOADDR", NULL, NULL }, { "unused", "unused", NULL, NULL }, { "unused", "unused", NULL, NULL }, { "unused", "unused", NULL, NULL },
7921 { "IODATA", "IODATA", NULL, NULL }, { "unused", "unused", NULL, NULL }, { "unused", "unused", NULL, NULL }, { "unused", "unused", NULL, NULL },
7922 { NULL, NULL, NULL, NULL }
7923 };
7924 rc = PDMDevHlpIoPortCreate(pDevIns, E1K_IOPORT_SIZE, pDevIns->apPciDevs[0], 2 /*iPciRegion*/,
7925 e1kIOPortOut, e1kIOPortIn, NULL /*pvUser*/, "E1000", s_aExtDescs, &pThis->hIoPorts);
7926 AssertRCReturn(rc, rc);
7927 rc = PDMDevHlpPCIIORegionRegisterIo(pDevIns, 2, E1K_IOPORT_SIZE, pThis->hIoPorts);
7928 AssertRCReturn(rc, rc);
7929
7930 /* Create transmit queue */
7931 rc = PDMDevHlpTaskCreate(pDevIns, PDMTASK_F_RZ, "E1000-Xmit", e1kR3TxTaskCallback, NULL, &pThis->hTxTask);
7932 AssertRCReturn(rc, rc);
7933
7934#ifdef E1K_TX_DELAY
7935 /* Create Transmit Delay Timer */
7936 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, e1kR3TxDelayTimer, pThis, TMTIMER_FLAGS_NO_CRIT_SECT,
7937 "E1000 Transmit Delay Timer", &pThis->hTXDTimer);
7938 AssertRCReturn(rc, rc);
7939 rc = PDMDevHlpTimerSetCritSect(pDevIns, pThis->hTXDTimer, &pThis->csTx);
7940 AssertRCReturn(rc, rc);
7941#endif /* E1K_TX_DELAY */
7942
7943//#ifdef E1K_USE_TX_TIMERS
7944 if (pThis->fTidEnabled)
7945 {
7946 /* Create Transmit Interrupt Delay Timer */
7947 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, e1kR3TxIntDelayTimer, pThis, TMTIMER_FLAGS_NO_CRIT_SECT,
7948 "E1000 Transmit Interrupt Delay Timer", &pThis->hTIDTimer);
7949 AssertRCReturn(rc, rc);
7950
7951# ifndef E1K_NO_TAD
7952 /* Create Transmit Absolute Delay Timer */
7953 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, e1kR3TxAbsDelayTimer, pThis, TMTIMER_FLAGS_NO_CRIT_SECT,
7954 "E1000 Transmit Absolute Delay Timer", &pThis->hTADTimer);
7955 AssertRCReturn(rc, rc);
7956# endif /* E1K_NO_TAD */
7957 }
7958//#endif /* E1K_USE_TX_TIMERS */
7959
7960#ifdef E1K_USE_RX_TIMERS
7961 /* Create Receive Interrupt Delay Timer */
7962 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, e1kR3RxIntDelayTimer, pThis, TMTIMER_FLAGS_NO_CRIT_SECT,
7963 "E1000 Receive Interrupt Delay Timer", &pThis->hRIDTimer);
7964 AssertRCReturn(rc, rc);
7965
7966 /* Create Receive Absolute Delay Timer */
7967 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, e1kR3RxAbsDelayTimer, pThis, TMTIMER_FLAGS_NO_CRIT_SECT,
7968 "E1000 Receive Absolute Delay Timer", &pThis->hRADTimer);
7969 AssertRCReturn(rc, rc);
7970#endif /* E1K_USE_RX_TIMERS */
7971
7972 /* Create Late Interrupt Timer */
7973 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, e1kR3LateIntTimer, pThis, TMTIMER_FLAGS_NO_CRIT_SECT,
7974 "E1000 Late Interrupt Timer", &pThis->hIntTimer);
7975 AssertRCReturn(rc, rc);
7976
7977 /* Create Link Up Timer */
7978 rc = PDMDevHlpTimerCreate(pDevIns, TMCLOCK_VIRTUAL, e1kR3LinkUpTimer, pThis, TMTIMER_FLAGS_NO_CRIT_SECT,
7979 "E1000 Link Up Timer", &pThis->hLUTimer);
7980 AssertRCReturn(rc, rc);
7981
7982 /* Register the info item */
7983 char szTmp[20];
7984 RTStrPrintf(szTmp, sizeof(szTmp), "e1k%d", iInstance);
7985 PDMDevHlpDBGFInfoRegister(pDevIns, szTmp, "E1000 info.", e1kInfo);
7986
7987 /* Status driver */
7988 PPDMIBASE pBase;
7989 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThisCC->IBase, &pBase, "Status Port");
7990 if (RT_FAILURE(rc))
7991 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the status LUN"));
7992 pThisCC->pLedsConnector = PDMIBASE_QUERY_INTERFACE(pBase, PDMILEDCONNECTORS);
7993
7994 /* Network driver */
7995 rc = PDMDevHlpDriverAttach(pDevIns, 0, &pThisCC->IBase, &pThisCC->pDrvBase, "Network Port");
7996 if (RT_SUCCESS(rc))
7997 {
7998 pThisCC->pDrvR3 = PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMINETWORKUP);
7999 AssertMsgReturn(pThisCC->pDrvR3, ("Failed to obtain the PDMINETWORKUP interface!\n"), VERR_PDM_MISSING_INTERFACE_BELOW);
8000
8001#if 0 /** @todo @bugref{9218} ring-0 driver stuff */
8002 pThisR0->pDrvR0 = PDMIBASER0_QUERY_INTERFACE(PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMIBASER0), PDMINETWORKUP);
8003 pThisRC->pDrvRC = PDMIBASERC_QUERY_INTERFACE(PDMIBASE_QUERY_INTERFACE(pThisCC->pDrvBase, PDMIBASERC), PDMINETWORKUP);
8004#endif
8005 }
8006 else if ( rc == VERR_PDM_NO_ATTACHED_DRIVER
8007 || rc == VERR_PDM_CFG_MISSING_DRIVER_NAME)
8008 {
8009 /* No error! */
8010 E1kLog(("%s This adapter is not attached to any network!\n", pThis->szPrf));
8011 }
8012 else
8013 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the network LUN"));
8014
8015 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEventMoreRxDescAvail);
8016 AssertRCReturn(rc, rc);
8017
8018 rc = e1kInitDebugHelpers();
8019 AssertRCReturn(rc, rc);
8020
8021 e1kR3HardReset(pDevIns, pThis, pThisCC);
8022
8023 /*
8024 * Register statistics.
8025 * The /Public/ bits are official and used by session info in the GUI.
8026 */
8027 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
8028 "Amount of data received", "/Public/NetAdapter/%u/BytesReceived", uStatNo);
8029 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
8030 "Amount of data transmitted", "/Public/NetAdapter/%u/BytesTransmitted", uStatNo);
8031 PDMDevHlpSTAMRegisterF(pDevIns, &pDevIns->iInstance, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_NONE,
8032 "Device instance number", "/Public/NetAdapter/%u/%s", uStatNo, pDevIns->pReg->szName);
8033
8034 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveBytes, STAMTYPE_COUNTER, "ReceiveBytes", STAMUNIT_BYTES, "Amount of data received");
8035 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitBytes, STAMTYPE_COUNTER, "TransmitBytes", STAMUNIT_BYTES, "Amount of data transmitted");
8036
8037#if defined(VBOX_WITH_STATISTICS)
8038 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadRZ, STAMTYPE_PROFILE, "MMIO/ReadRZ", STAMUNIT_TICKS_PER_CALL, "Profiling MMIO reads in RZ");
8039 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOReadR3, STAMTYPE_PROFILE, "MMIO/ReadR3", STAMUNIT_TICKS_PER_CALL, "Profiling MMIO reads in R3");
8040 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteRZ, STAMTYPE_PROFILE, "MMIO/WriteRZ", STAMUNIT_TICKS_PER_CALL, "Profiling MMIO writes in RZ");
8041 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMMIOWriteR3, STAMTYPE_PROFILE, "MMIO/WriteR3", STAMUNIT_TICKS_PER_CALL, "Profiling MMIO writes in R3");
8042 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatEEPROMRead, STAMTYPE_PROFILE, "EEPROM/Read", STAMUNIT_TICKS_PER_CALL, "Profiling EEPROM reads");
8043 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatEEPROMWrite, STAMTYPE_PROFILE, "EEPROM/Write", STAMUNIT_TICKS_PER_CALL, "Profiling EEPROM writes");
8044 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIOReadRZ, STAMTYPE_PROFILE, "IO/ReadRZ", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in RZ");
8045 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIOReadR3, STAMTYPE_PROFILE, "IO/ReadR3", STAMUNIT_TICKS_PER_CALL, "Profiling IO reads in R3");
8046 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIOWriteRZ, STAMTYPE_PROFILE, "IO/WriteRZ", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in RZ");
8047 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIOWriteR3, STAMTYPE_PROFILE, "IO/WriteR3", STAMUNIT_TICKS_PER_CALL, "Profiling IO writes in R3");
8048 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatLateIntTimer, STAMTYPE_PROFILE, "LateInt/Timer", STAMUNIT_TICKS_PER_CALL, "Profiling late int timer");
8049 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatLateInts, STAMTYPE_COUNTER, "LateInt/Occured", STAMUNIT_OCCURENCES, "Number of late interrupts");
8050 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntsRaised, STAMTYPE_COUNTER, "Interrupts/Raised", STAMUNIT_OCCURENCES, "Number of raised interrupts");
8051 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntsPrevented, STAMTYPE_COUNTER, "Interrupts/Prevented", STAMUNIT_OCCURENCES, "Number of prevented interrupts");
8052 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceive, STAMTYPE_PROFILE, "Receive/Total", STAMUNIT_TICKS_PER_CALL, "Profiling receive");
8053 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveCRC, STAMTYPE_PROFILE, "Receive/CRC", STAMUNIT_TICKS_PER_CALL, "Profiling receive checksumming");
8054 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveFilter, STAMTYPE_PROFILE, "Receive/Filter", STAMUNIT_TICKS_PER_CALL, "Profiling receive filtering");
8055 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatReceiveStore, STAMTYPE_PROFILE, "Receive/Store", STAMUNIT_TICKS_PER_CALL, "Profiling receive storing");
8056 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRxOverflow, STAMTYPE_PROFILE, "RxOverflow", STAMUNIT_TICKS_PER_OCCURENCE, "Profiling RX overflows");
8057 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRxOverflowWakeupRZ, STAMTYPE_COUNTER, "RxOverflowWakeupRZ", STAMUNIT_OCCURENCES, "Nr of RX overflow wakeups in RZ");
8058 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatRxOverflowWakeupR3, STAMTYPE_COUNTER, "RxOverflowWakeupR3", STAMUNIT_OCCURENCES, "Nr of RX overflow wakeups in R3");
8059 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitRZ, STAMTYPE_PROFILE, "Transmit/TotalRZ", STAMUNIT_TICKS_PER_CALL, "Profiling transmits in RZ");
8060 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitR3, STAMTYPE_PROFILE, "Transmit/TotalR3", STAMUNIT_TICKS_PER_CALL, "Profiling transmits in R3");
8061 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitSendRZ, STAMTYPE_PROFILE, "Transmit/SendRZ", STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in RZ");
8062 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTransmitSendR3, STAMTYPE_PROFILE, "Transmit/SendR3", STAMUNIT_TICKS_PER_CALL, "Profiling send transmit in R3");
8063
8064 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxDescCtxNormal, STAMTYPE_COUNTER, "TxDesc/ContexNormal", STAMUNIT_OCCURENCES, "Number of normal context descriptors");
8065 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxDescCtxTSE, STAMTYPE_COUNTER, "TxDesc/ContextTSE", STAMUNIT_OCCURENCES, "Number of TSE context descriptors");
8066 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxDescData, STAMTYPE_COUNTER, "TxDesc/Data", STAMUNIT_OCCURENCES, "Number of TX data descriptors");
8067 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxDescLegacy, STAMTYPE_COUNTER, "TxDesc/Legacy", STAMUNIT_OCCURENCES, "Number of TX legacy descriptors");
8068 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxDescTSEData, STAMTYPE_COUNTER, "TxDesc/TSEData", STAMUNIT_OCCURENCES, "Number of TX TSE data descriptors");
8069 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxPathFallback, STAMTYPE_COUNTER, "TxPath/Fallback", STAMUNIT_OCCURENCES, "Fallback TSE descriptor path");
8070 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxPathGSO, STAMTYPE_COUNTER, "TxPath/GSO", STAMUNIT_OCCURENCES, "GSO TSE descriptor path");
8071 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatTxPathRegular, STAMTYPE_COUNTER, "TxPath/Normal", STAMUNIT_OCCURENCES, "Regular descriptor path");
8072 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPHYAccesses, STAMTYPE_COUNTER, "PHYAccesses", STAMUNIT_OCCURENCES, "Number of PHY accesses");
8073 for (unsigned iReg = 0; iReg < E1K_NUM_OF_REGS; iReg++)
8074 {
8075 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->aStatRegReads[iReg], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
8076 g_aE1kRegMap[iReg].name, "Regs/%s-Reads", g_aE1kRegMap[iReg].abbrev);
8077 PDMDevHlpSTAMRegisterF(pDevIns, &pThis->aStatRegWrites[iReg], STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES,
8078 g_aE1kRegMap[iReg].name, "Regs/%s-Writes", g_aE1kRegMap[iReg].abbrev);
8079 }
8080#endif /* VBOX_WITH_STATISTICS */
8081
8082#ifdef E1K_INT_STATS
8083 PDMDevHlpSTAMRegister(pDevIns, &pThis->u64ArmedAt, STAMTYPE_U64, "u64ArmedAt", STAMUNIT_NS, NULL);
8084 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatMaxTxDelay, STAMTYPE_U64, "uStatMaxTxDelay", STAMUNIT_NS, NULL);
8085 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatInt, STAMTYPE_U32, "uStatInt", STAMUNIT_NS, NULL);
8086 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntTry, STAMTYPE_U32, "uStatIntTry", STAMUNIT_NS, NULL);
8087 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntLower, STAMTYPE_U32, "uStatIntLower", STAMUNIT_NS, NULL);
8088 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatNoIntICR, STAMTYPE_U32, "uStatNoIntICR", STAMUNIT_NS, NULL);
8089 PDMDevHlpSTAMRegister(pDevIns, &pThis->iStatIntLost, STAMTYPE_U32, "iStatIntLost", STAMUNIT_NS, NULL);
8090 PDMDevHlpSTAMRegister(pDevIns, &pThis->iStatIntLostOne, STAMTYPE_U32, "iStatIntLostOne", STAMUNIT_NS, NULL);
8091 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntIMS, STAMTYPE_U32, "uStatIntIMS", STAMUNIT_NS, NULL);
8092 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntSkip, STAMTYPE_U32, "uStatIntSkip", STAMUNIT_NS, NULL);
8093 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntLate, STAMTYPE_U32, "uStatIntLate", STAMUNIT_NS, NULL);
8094 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntMasked, STAMTYPE_U32, "uStatIntMasked", STAMUNIT_NS, NULL);
8095 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntEarly, STAMTYPE_U32, "uStatIntEarly", STAMUNIT_NS, NULL);
8096 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntRx, STAMTYPE_U32, "uStatIntRx", STAMUNIT_NS, NULL);
8097 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntTx, STAMTYPE_U32, "uStatIntTx", STAMUNIT_NS, NULL);
8098 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntICS, STAMTYPE_U32, "uStatIntICS", STAMUNIT_NS, NULL);
8099 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntRDTR, STAMTYPE_U32, "uStatIntRDTR", STAMUNIT_NS, NULL);
8100 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntRXDMT0, STAMTYPE_U32, "uStatIntRXDMT0", STAMUNIT_NS, NULL);
8101 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatIntTXQE, STAMTYPE_U32, "uStatIntTXQE", STAMUNIT_NS, NULL);
8102 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTxNoRS, STAMTYPE_U32, "uStatTxNoRS", STAMUNIT_NS, NULL);
8103 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTxIDE, STAMTYPE_U32, "uStatTxIDE", STAMUNIT_NS, NULL);
8104 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTxDelayed, STAMTYPE_U32, "uStatTxDelayed", STAMUNIT_NS, NULL);
8105 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTxDelayExp, STAMTYPE_U32, "uStatTxDelayExp", STAMUNIT_NS, NULL);
8106 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTAD, STAMTYPE_U32, "uStatTAD", STAMUNIT_NS, NULL);
8107 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTID, STAMTYPE_U32, "uStatTID", STAMUNIT_NS, NULL);
8108 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatRAD, STAMTYPE_U32, "uStatRAD", STAMUNIT_NS, NULL);
8109 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatRID, STAMTYPE_U32, "uStatRID", STAMUNIT_NS, NULL);
8110 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatRxFrm, STAMTYPE_U32, "uStatRxFrm", STAMUNIT_NS, NULL);
8111 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTxFrm, STAMTYPE_U32, "uStatTxFrm", STAMUNIT_NS, NULL);
8112 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatDescCtx, STAMTYPE_U32, "uStatDescCtx", STAMUNIT_NS, NULL);
8113 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatDescDat, STAMTYPE_U32, "uStatDescDat", STAMUNIT_NS, NULL);
8114 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatDescLeg, STAMTYPE_U32, "uStatDescLeg", STAMUNIT_NS, NULL);
8115 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx1514, STAMTYPE_U32, "uStatTx1514", STAMUNIT_NS, NULL);
8116 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx2962, STAMTYPE_U32, "uStatTx2962", STAMUNIT_NS, NULL);
8117 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx4410, STAMTYPE_U32, "uStatTx4410", STAMUNIT_NS, NULL);
8118 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx5858, STAMTYPE_U32, "uStatTx5858", STAMUNIT_NS, NULL);
8119 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx7306, STAMTYPE_U32, "uStatTx7306", STAMUNIT_NS, NULL);
8120 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx8754, STAMTYPE_U32, "uStatTx8754", STAMUNIT_NS, NULL);
8121 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx16384, STAMTYPE_U32, "uStatTx16384", STAMUNIT_NS, NULL);
8122 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTx32768, STAMTYPE_U32, "uStatTx32768", STAMUNIT_NS, NULL);
8123 PDMDevHlpSTAMRegister(pDevIns, &pThis->uStatTxLarge, STAMTYPE_U32, "uStatTxLarge", STAMUNIT_NS, NULL);
8124#endif /* E1K_INT_STATS */
8125
8126 return VINF_SUCCESS;
8127}
8128
8129#else /* !IN_RING3 */
8130
8131/**
8132 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
8133 */
8134static DECLCALLBACK(int) e1kRZConstruct(PPDMDEVINS pDevIns)
8135{
8136 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
8137 PE1KSTATE pThis = PDMDEVINS_2_DATA(pDevIns, PE1KSTATE);
8138 PE1KSTATECC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PE1KSTATECC);
8139
8140 /* Initialize context specific state data: */
8141 pThisCC->CTX_SUFF(pDevIns) = pDevIns;
8142 /** @todo @bugref{9218} ring-0 driver stuff */
8143 pThisCC->CTX_SUFF(pDrv) = NULL;
8144 pThisCC->CTX_SUFF(pTxSg) = NULL;
8145
8146 /* Configure critical sections the same way: */
8147 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
8148 AssertRCReturn(rc, rc);
8149
8150 /* Set up MMIO and I/O port callbacks for this context: */
8151 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmioRegion, e1kMMIOWrite, e1kMMIORead, NULL /*pvUser*/);
8152 AssertRCReturn(rc, rc);
8153
8154 rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPorts, e1kIOPortOut, e1kIOPortIn, NULL /*pvUser*/);
8155 AssertRCReturn(rc, rc);
8156
8157 return VINF_SUCCESS;
8158}
8159
8160#endif /* !IN_RING3 */
8161
8162/**
8163 * The device registration structure.
8164 */
8165const PDMDEVREG g_DeviceE1000 =
8166{
8167 /* .u32version = */ PDM_DEVREG_VERSION,
8168 /* .uReserved0 = */ 0,
8169 /* .szName = */ "e1000",
8170 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
8171 /* .fClass = */ PDM_DEVREG_CLASS_NETWORK,
8172 /* .cMaxInstances = */ ~0U,
8173 /* .uSharedVersion = */ 42,
8174 /* .cbInstanceShared = */ sizeof(E1KSTATE),
8175 /* .cbInstanceCC = */ sizeof(E1KSTATECC),
8176 /* .cbInstanceRC = */ sizeof(E1KSTATERC),
8177 /* .cMaxPciDevices = */ 1,
8178 /* .cMaxMsixVectors = */ 0,
8179 /* .pszDescription = */ "Intel PRO/1000 MT Desktop Ethernet.",
8180#if defined(IN_RING3)
8181 /* .pszRCMod = */ "VBoxDDRC.rc",
8182 /* .pszR0Mod = */ "VBoxDDR0.r0",
8183 /* .pfnConstruct = */ e1kR3Construct,
8184 /* .pfnDestruct = */ e1kR3Destruct,
8185 /* .pfnRelocate = */ e1kR3Relocate,
8186 /* .pfnMemSetup = */ NULL,
8187 /* .pfnPowerOn = */ NULL,
8188 /* .pfnReset = */ e1kR3Reset,
8189 /* .pfnSuspend = */ e1kR3Suspend,
8190 /* .pfnResume = */ NULL,
8191 /* .pfnAttach = */ e1kR3Attach,
8192 /* .pfnDeatch = */ e1kR3Detach,
8193 /* .pfnQueryInterface = */ NULL,
8194 /* .pfnInitComplete = */ NULL,
8195 /* .pfnPowerOff = */ e1kR3PowerOff,
8196 /* .pfnSoftReset = */ NULL,
8197 /* .pfnReserved0 = */ NULL,
8198 /* .pfnReserved1 = */ NULL,
8199 /* .pfnReserved2 = */ NULL,
8200 /* .pfnReserved3 = */ NULL,
8201 /* .pfnReserved4 = */ NULL,
8202 /* .pfnReserved5 = */ NULL,
8203 /* .pfnReserved6 = */ NULL,
8204 /* .pfnReserved7 = */ NULL,
8205#elif defined(IN_RING0)
8206 /* .pfnEarlyConstruct = */ NULL,
8207 /* .pfnConstruct = */ e1kRZConstruct,
8208 /* .pfnDestruct = */ NULL,
8209 /* .pfnFinalDestruct = */ NULL,
8210 /* .pfnRequest = */ NULL,
8211 /* .pfnReserved0 = */ NULL,
8212 /* .pfnReserved1 = */ NULL,
8213 /* .pfnReserved2 = */ NULL,
8214 /* .pfnReserved3 = */ NULL,
8215 /* .pfnReserved4 = */ NULL,
8216 /* .pfnReserved5 = */ NULL,
8217 /* .pfnReserved6 = */ NULL,
8218 /* .pfnReserved7 = */ NULL,
8219#elif defined(IN_RC)
8220 /* .pfnConstruct = */ e1kRZConstruct,
8221 /* .pfnReserved0 = */ NULL,
8222 /* .pfnReserved1 = */ NULL,
8223 /* .pfnReserved2 = */ NULL,
8224 /* .pfnReserved3 = */ NULL,
8225 /* .pfnReserved4 = */ NULL,
8226 /* .pfnReserved5 = */ NULL,
8227 /* .pfnReserved6 = */ NULL,
8228 /* .pfnReserved7 = */ NULL,
8229#else
8230# error "Not in IN_RING3, IN_RING0 or IN_RC!"
8231#endif
8232 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
8233};
8234
8235#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