Skip to content

Commit

Permalink
Establish baseline BSD code
Browse files Browse the repository at this point in the history
Goal: make it easier to incorporate changes to the BSD driver into this
driver by creating a BSD-driver baseline and minimizing this driver's
differences from that baseline. This way, incorporating BSD driver
changes into this driver can be done by updating the baseline and then
making the same changes to this driver.

With the exception of using BCryptGenRandom instead of GetTickCount when
picking the random ethernet address, I expect this to have no impact on
the actual behavior of the driver -- this is just refactoring.

DETAILS:

import:

- Add a copy of the files from the BSD driver. (I did the best I could to
  find the BSD driver version that looked like the baseline of this driver.)

if_re_bsd.h:

- Recreate. Start with the BSD driver's if_rereg.h, then apply changes
  needed to get this driver to build. Wrap all changes in `#ifdef _WIN32`.
  Goal is that every difference between this file and `if_rereg.h` are
  additions that are wrapped in the appropriate `#ifdef`.
- Moved the "Our Additions" section of `re_softc` up to the top to be
  together with the `dev` field.
- Moved `mtu`, `if_capenable`, and `if_hwassist` inside of a struct to
  help reduce the code differences between BSD and this driver.
- Moved some definitions from `bsd.h` into this header so that they are
  closer to the corresponding BSD definitions.
- Changed the Windows definitions of the DBGPRINT macros to more-closely
  match the BSD behavior.

rtlhw.cpp:

- Recreate. Similar to what was done with if_re_bsd.h - start with the
  BSD version then apply minimal changes to make it build, with all
  changes wrapped in appropriate `#ifdef _WIN32`.
- Lots of whitespace changes, which can be ignored.
- Changed the implementation of `random_ether_addr` to use
  `BCryptGenRandom` instead of `GetTickCount`.
- Replaced `max_rx_mbuf_sz` with `MJUM9BYTES` to match the BSD driver.
- Restored original function ordering (unfortunately this makes the diff
  harder to read).
- Moving the `if_` variables inside of a field allows more of the code
  to match, so we end up with things like `ifp->if_mtu` instead of
  `sc->mtu`, but nothing has really changed in practice.

adapter.cpp:
device.cpp:

- Some `re_softc` fields moved inside struct, so update references.

bsd.h:

- `stdint.h` isn't supported in kernel mode yet, so just define our own
  `uintN_t` types.
- Move `intrin.h` into `precomp.h`.
- Prefer typedef over macro.
- Move some macros into if_re_bsd.h to be closer to the definition of
  the BSD version of the macro so it's easier to see how the BSD
  definition compares to the WIN32 definition.
- Move the definition of random_ether_addr back into rtlhw.cpp in the
  same place as the BSD version.
- Define placeholder types so we don't have to ifdef-out structs that
  use them.

if_re.vcxproj:

- Add dependency on Ksecdd.lib since we need BCryptGenRandom.
- Fix precompiled header usage. (We should compile "precomp.cpp" with
  "CREATE", and then everything else in the project should compile with
  "USE".)

rxqueue.cpp:
txqueue.cpp:

- Fix a few compiler warnings.
  • Loading branch information
idigdoug committed Oct 21, 2024
1 parent ef86259 commit 95ea056
Show file tree
Hide file tree
Showing 15 changed files with 73,366 additions and 31,406 deletions.
10 changes: 5 additions & 5 deletions adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ RtAdapterSetLinkLayerCapabilities(
adapter->MaxSpeed);

NetAdapterSetLinkLayerCapabilities(adapter->NetAdapter, &linkLayerCapabilities);
NetAdapterSetLinkLayerMtuSize(adapter->NetAdapter, adapter->bsdData.mtu);
NetAdapterSetLinkLayerMtuSize(adapter->NetAdapter, adapter->bsdData.if_net.if_mtu);
NetAdapterSetPermanentLinkLayerAddress(adapter->NetAdapter, &adapter->PermanentAddress);
NetAdapterSetCurrentLinkLayerAddress(adapter->NetAdapter, &adapter->CurrentAddress);
}
Expand Down Expand Up @@ -200,7 +200,7 @@ RtAdapterSetOffloadCapabilities(

const struct re_softc* sc = &adapter->bsdData;

BOOLEAN txSupported = (sc->if_capenable & IFCAP_TXCSUM) != 0;
BOOLEAN txSupported = (sc->if_net.if_capenable & IFCAP_TXCSUM) != 0;
if (txSupported) {
auto const layer3Flags = NetAdapterOffloadLayer3FlagIPv4NoOptions |
NetAdapterOffloadLayer3FlagIPv4WithOptions |
Expand All @@ -222,7 +222,7 @@ RtAdapterSetOffloadCapabilities(
NetAdapterOffloadSetTxChecksumCapabilities(adapter->NetAdapter, &txChecksumOffloadCapabilities);
}

BOOLEAN rxSupported = (sc->if_capenable & IFCAP_RXCSUM) != 0;
BOOLEAN rxSupported = (sc->if_net.if_capenable & IFCAP_RXCSUM) != 0;
if (rxSupported) {
NET_ADAPTER_OFFLOAD_RX_CHECKSUM_CAPABILITIES rxChecksumOffloadCapabilities;

Expand All @@ -233,7 +233,7 @@ RtAdapterSetOffloadCapabilities(
NetAdapterOffloadSetRxChecksumCapabilities(adapter->NetAdapter, &rxChecksumOffloadCapabilities);
}

BOOLEAN gsoSupported = (sc->if_capenable & IFCAP_TSO) != 0;
BOOLEAN gsoSupported = (sc->if_net.if_capenable & IFCAP_TSO) != 0;
if (gsoSupported) {
NET_ADAPTER_OFFLOAD_GSO_CAPABILITIES gsoOffloadCapabilities;

Expand All @@ -258,7 +258,7 @@ RtAdapterSetOffloadCapabilities(
NetAdapterOffloadSetGsoCapabilities(adapter->NetAdapter, &gsoOffloadCapabilities);
}

BOOLEAN ieee8021qSupported = (sc->if_capenable & IFCAP_VLAN_HWTAGGING) != 0;
BOOLEAN ieee8021qSupported = (sc->if_net.if_capenable & IFCAP_VLAN_HWTAGGING) != 0;
if (ieee8021qSupported) {
const NET_ADAPTER_OFFLOAD_IEEE8021Q_TAG_FLAGS ieee8021qTaggingFlag = NetAdapterOffloadIeee8021PriorityTaggingFlag |
NetAdapterOffloadIeee8021VlanTaggingFlag;
Expand Down
3 changes: 2 additions & 1 deletion adapter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "bsd.h"
#include "if_re_bsd.h"
#include "bsdexport.h"

Expand Down Expand Up @@ -140,4 +141,4 @@ GetMulticastBit(
_In_ NET_ADAPTER_LINK_LAYER_ADDRESS const* address,
_Out_ _Post_satisfies_(*byte < MAX_NIC_MULTICAST_REG) UCHAR* byte,
_Out_ UCHAR* value
);
);
91 changes: 31 additions & 60 deletions bsd.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#pragma once

#pragma warning (disable: 4005)
#pragma warning (disable: 4083)
#include <stdint.h>
#pragma warning(default: 4005)
#pragma warning(default: 4083)
#include <intrin.h>
#define __NO_STRICT_ALIGNMENT

// Driver Options

Expand All @@ -14,23 +9,18 @@
#define interrupt_mitigation 1
#define phy_power_saving 1
#define phy_mdix_mode RE_ETH_PHY_AUTO_MDI_MDIX
#define max_rx_mbuf_sz MJUM9BYTES

#define RE_CSUM_FEATURES_IPV4 (CSUM_IP | CSUM_TCP | CSUM_UDP)
#define RE_CSUM_FEATURES_IPV6 (CSUM_TCP_IPV6 | CSUM_UDP_IPV6)
#define RE_CSUM_FEATURES (RE_CSUM_FEATURES_IPV4 | RE_CSUM_FEATURES_IPV6)


// BSD Compat

#define __FBSDID(id)

#define MJUM9BYTES (9 * 1024) /* jumbo cluster 9k */
#define MJUM16BYTES (16 * 1024) /* jumbo cluster 16k */

#define u_int8_t uint8_t
#define u_int16_t uint16_t
#define u_int32_t uint32_t
#define u_int64_t uint64_t

#define __P(x) x

#define DELAY(x) KeStallExecutionProcessor(x)
Expand All @@ -43,8 +33,6 @@

#define ntohs(x) RtlUshortByteSwap(x)

#define caddr_t uintptr_t

#define ENXIO 6 /* Device not configured */
#define EOPNOTSUPP 45 /* Operation not supported */

Expand All @@ -58,8 +46,6 @@
#define device_printf(dev, x, ...) __nop()
#endif

#define DBGPRINT1(dev, x, ...) DbgPrint(x, __VA_ARGS__)

#if !defined(_ARM_) && !defined(_ARM64_)
#define WRITE_REGISTER_NOFENCE_ULONG WRITE_REGISTER_ULONG
#define WRITE_REGISTER_NOFENCE_USHORT WRITE_REGISTER_USHORT
Expand All @@ -70,51 +56,36 @@
#define READ_REGISTER_NOFENCE_UCHAR READ_REGISTER_UCHAR
#endif

/*
* register space access macros
*/
#define CSR_WRITE_4(sc, reg, val) ((sc->prohibit_access_reg)?__nop():WRITE_REGISTER_NOFENCE_ULONG((PULONG)MmioAddr(sc, reg), val))
#define CSR_WRITE_2(sc, reg, val) ((sc->prohibit_access_reg)?__nop():WRITE_REGISTER_NOFENCE_USHORT((PUSHORT)MmioAddr(sc, reg), val))
#define CSR_WRITE_1(sc, reg, val) ((sc->prohibit_access_reg)?__nop():WRITE_REGISTER_NOFENCE_UCHAR((PUCHAR)MmioAddr(sc, reg), val))

#define CSR_READ_4(sc, reg) ((sc->prohibit_access_reg)?0xFFFFFFFF:READ_REGISTER_NOFENCE_ULONG((PULONG)MmioAddr(sc, reg)))
#define CSR_READ_2(sc, reg) ((sc->prohibit_access_reg)?0xFFFF:READ_REGISTER_NOFENCE_USHORT((PUSHORT)MmioAddr(sc, reg)))
#define CSR_READ_1(sc, reg) ((sc->prohibit_access_reg)?0xFF:READ_REGISTER_NOFENCE_UCHAR((PUCHAR)MmioAddr(sc, reg)))

#if DISABLED_CODE
/* cmac write/read MMIO register */
#define RE_CMAC_WRITE_1(sc, reg, val) ((sc->prohibit_access_reg)?:bus_space_write_1(sc->re_cmac_tag, sc->re_cmac_handle, reg, val))
#define RE_CMAC_WRITE_2(sc, reg, val) ((sc->prohibit_access_reg)?:bus_space_write_2(sc->re_cmac_tag, sc->re_cmac_handle, reg, val))
#define RE_CMAC_WRITE_4(sc, reg, val) ((sc->prohibit_access_reg)?:bus_space_write_4(sc->re_cmac_tag, sc->re_cmac_handle, reg, val))
#define RE_CMAC_READ_1(sc, reg) ((sc->prohibit_access_reg)?0xFF:bus_space_read_1(sc->re_cmac_tag, sc->re_cmac_handle, reg))
#define RE_CMAC_READ_2(sc, reg) ((sc->prohibit_access_reg)?0xFFFF:bus_space_read_2(sc->re_cmac_tag, sc->re_cmac_handle, reg))
#define RE_CMAC_READ_4(sc, reg) (sc->prohibit_access_reg)?0xFFFFFFFF:bus_space_read_4(sc->re_cmac_tag, sc->re_cmac_handle, reg))
#endif

#define RE_LOCK(_sc) WdfSpinLockAcquire(_sc->dev->Lock)
#define RE_UNLOCK(_sc) WdfSpinLockRelease(_sc->dev->Lock)
#define RE_LOCK_ASSERT(_sc)

#include "mii.h"
#include "ethernet.h"
#include "if.h"
#include "mbuf.h"

extern "C" NTSYSAPI ULONG RtlRandomEx(
_Inout_ PULONG Seed
);

static inline void
random_ether_addr(u_int8_t* dst)
{
LARGE_INTEGER TickCount;
/* Try to generate a more random seed */
KeQueryTickCount(&TickCount);

for (int i = 0; i < 6; i++) {
dst[i] = RtlRandomEx(&TickCount.LowPart) % 0xff;
}

dst[0] &= 0xfe;
dst[0] |= 0x02;
}
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed long long int64_t;
typedef unsigned long long uint64_t;

typedef uint8_t u_int8_t;
typedef uint16_t u_int16_t;
typedef uint32_t u_int32_t;
typedef uint64_t u_int64_t;
typedef uintptr_t caddr_t;

struct ifnet {
uint16_t if_mtu;
int if_capenable;
int if_hwassist;
};

// Placeholder types - fields are not used on WIN32.

typedef char bus_dma_tag_t;
typedef char bus_dmamap_t;
typedef char bus_dma_segment_t;
typedef char bus_addr_t;
struct bus_dma_tag_common { char x; };
2 changes: 1 addition & 1 deletion bsdexport.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ int re_ifmedia_upd(struct re_softc* sc);
int re_ifmedia_upd_8125(struct re_softc* sc);

void re_rar_set(struct re_softc* sc, u_int8_t* eaddr);
void re_set_rx_packet_filter(struct re_softc* sc);
void re_set_rx_packet_filter(struct re_softc* sc);
18 changes: 9 additions & 9 deletions device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ RtInitializeHardware(
RtlZeroMemory(&adapter->bsdData, sizeof(adapter->bsdData));

adapter->bsdData.dev = adapter;
adapter->bsdData.mtu = ETHERMTU;
adapter->bsdData.if_net.if_mtu = ETHERMTU;
adapter->bsdData.eee_enable = adapter->EEEEnable ? 1 : 0;

UINT16 devID = ConfigRead16(adapter, 2);
Expand All @@ -149,23 +149,23 @@ RtInitializeHardware(
re_init_software_variable(&adapter->bsdData);

if ((sc->re_type == MACFG_24) || (sc->re_type == MACFG_25) || (sc->re_type == MACFG_26))
sc->if_hwassist |= CSUM_TCP | CSUM_UDP;
sc->if_net.if_hwassist |= CSUM_TCP | CSUM_UDP;
else
sc->if_hwassist |= RE_CSUM_FEATURES;
sc->if_capenable = IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
sc->if_net.if_hwassist |= RE_CSUM_FEATURES;
sc->if_net.if_capenable = IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
/* TSO capability setup */
if (sc->re_if_flags & RL_FLAG_8168G_PLUS) {
sc->if_hwassist |= CSUM_TSO;
sc->if_capenable |= IFCAP_TSO;
sc->if_net.if_hwassist |= CSUM_TSO;
sc->if_net.if_capenable |= IFCAP_TSO;
}
/* RTL8169/RTL8101E/RTL8168B not support TSO v6 */
if (!(sc->re_if_flags & RL_FLAG_DESCV2)) {
sc->if_hwassist &= ~(CSUM_IP6_TSO |
sc->if_net.if_hwassist &= ~(CSUM_IP6_TSO |
CSUM_TCP_IPV6 |
CSUM_UDP_IPV6);
sc->if_capenable &= ~(IFCAP_TSO6 | IFCAP_HWCSUM_IPV6);
sc->if_net.if_capenable &= ~(IFCAP_TSO6 | IFCAP_HWCSUM_IPV6);
}
sc->if_capenable |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
sc->if_net.if_capenable |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;

adapter->bsdData.re_rx_cstag = 1;
adapter->bsdData.re_tx_cstag = 1;
Expand Down
Loading

0 comments on commit 95ea056

Please sign in to comment.