2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-03-10 00:07:41 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-11-17 03:41:46 +03:00
|
|
|
#include <AK/MACAddress.h>
|
Kernel/PCI: Simplify the entire subsystem
A couple of things were changed:
1. Semantic changes - PCI segments are now called PCI domains, to better
match what they are really. It's also the name that Linux gave, and it
seems that Wikipedia also uses this name.
We also remove PCI::ChangeableAddress, because it was used in the past
but now it's no longer being used.
2. There are no WindowedMMIOAccess or MMIOAccess classes anymore, as
they made a bunch of unnecessary complexity. Instead, Windowed access is
removed entirely (this was tested, but never was benchmarked), so we are
left with IO access and memory access options. The memory access option
is essentially mapping the PCI bus (from the chosen PCI domain), to
virtual memory as-is. This means that unless needed, at any time, there
is only one PCI bus being mapped, and this is changed if access to
another PCI bus in the same PCI domain is needed. For now, we don't
support mapping of different PCI buses from different PCI domains at the
same time, because basically it's still a non-issue for most machines
out there.
2. OOM-safety is increased, especially when constructing the Access
object. It means that we pre-allocating any needed resources, and we try
to find PCI domains (if requested to initialize memory access) after we
attempt to construct the Access object, so it's possible to fail at this
point "gracefully".
3. All PCI API functions are now separated into a different header file,
which means only "clients" of the PCI subsystem API will need to include
that header file.
4. Functional changes - we only allow now to enumerate the bus after
a hardware scan. This means that the old method "enumerate_hardware"
is removed, so, when initializing an Access object, the initializing
function must call rescan on it to force it to find devices. This makes
it possible to fail rescan, and also to defer it after construction from
both OOM-safety terms and hotplug capabilities.
2021-09-07 12:08:38 +03:00
|
|
|
#include <Kernel/Bus/PCI/API.h>
|
2021-06-25 09:46:17 +03:00
|
|
|
#include <Kernel/Bus/PCI/IDs.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/E1000NetworkAdapter.h>
|
2021-06-22 18:40:16 +03:00
|
|
|
#include <Kernel/Sections.h>
|
2019-03-10 17:25:33 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
#define REG_CTRL 0x0000
|
|
|
|
#define REG_STATUS 0x0008
|
|
|
|
#define REG_EEPROM 0x0014
|
|
|
|
#define REG_CTRL_EXT 0x0018
|
2020-03-20 01:44:42 +03:00
|
|
|
#define REG_INTERRUPT_CAUSE_READ 0x00C0
|
|
|
|
#define REG_INTERRUPT_RATE 0x00C4
|
|
|
|
#define REG_INTERRUPT_MASK_SET 0x00D0
|
|
|
|
#define REG_INTERRUPT_MASK_CLEAR 0x00D8
|
2019-06-07 12:43:58 +03:00
|
|
|
#define REG_RCTRL 0x0100
|
|
|
|
#define REG_RXDESCLO 0x2800
|
|
|
|
#define REG_RXDESCHI 0x2804
|
|
|
|
#define REG_RXDESCLEN 0x2808
|
|
|
|
#define REG_RXDESCHEAD 0x2810
|
|
|
|
#define REG_RXDESCTAIL 0x2818
|
|
|
|
#define REG_TCTRL 0x0400
|
|
|
|
#define REG_TXDESCLO 0x3800
|
|
|
|
#define REG_TXDESCHI 0x3804
|
|
|
|
#define REG_TXDESCLEN 0x3808
|
|
|
|
#define REG_TXDESCHEAD 0x3810
|
|
|
|
#define REG_TXDESCTAIL 0x3818
|
|
|
|
#define REG_RDTR 0x2820 // RX Delay Timer Register
|
|
|
|
#define REG_RXDCTL 0x3828 // RX Descriptor Control
|
|
|
|
#define REG_RADV 0x282C // RX Int. Absolute Delay Timer
|
|
|
|
#define REG_RSRPD 0x2C00 // RX Small Packet Detect Interrupt
|
|
|
|
#define REG_TIPG 0x0410 // Transmit Inter Packet Gap
|
|
|
|
#define ECTRL_SLU 0x40 //set link up
|
|
|
|
#define RCTL_EN (1 << 1) // Receiver Enable
|
|
|
|
#define RCTL_SBP (1 << 2) // Store Bad Packets
|
|
|
|
#define RCTL_UPE (1 << 3) // Unicast Promiscuous Enabled
|
|
|
|
#define RCTL_MPE (1 << 4) // Multicast Promiscuous Enabled
|
|
|
|
#define RCTL_LPE (1 << 5) // Long Packet Reception Enable
|
|
|
|
#define RCTL_LBM_NONE (0 << 6) // No Loopback
|
|
|
|
#define RCTL_LBM_PHY (3 << 6) // PHY or external SerDesc loopback
|
|
|
|
#define RTCL_RDMTS_HALF (0 << 8) // Free Buffer Threshold is 1/2 of RDLEN
|
|
|
|
#define RTCL_RDMTS_QUARTER (1 << 8) // Free Buffer Threshold is 1/4 of RDLEN
|
|
|
|
#define RTCL_RDMTS_EIGHTH (2 << 8) // Free Buffer Threshold is 1/8 of RDLEN
|
|
|
|
#define RCTL_MO_36 (0 << 12) // Multicast Offset - bits 47:36
|
|
|
|
#define RCTL_MO_35 (1 << 12) // Multicast Offset - bits 46:35
|
|
|
|
#define RCTL_MO_34 (2 << 12) // Multicast Offset - bits 45:34
|
|
|
|
#define RCTL_MO_32 (3 << 12) // Multicast Offset - bits 43:32
|
|
|
|
#define RCTL_BAM (1 << 15) // Broadcast Accept Mode
|
|
|
|
#define RCTL_VFE (1 << 18) // VLAN Filter Enable
|
|
|
|
#define RCTL_CFIEN (1 << 19) // Canonical Form Indicator Enable
|
|
|
|
#define RCTL_CFI (1 << 20) // Canonical Form Indicator Bit Value
|
|
|
|
#define RCTL_DPF (1 << 22) // Discard Pause Frames
|
|
|
|
#define RCTL_PMCF (1 << 23) // Pass MAC Control Frames
|
|
|
|
#define RCTL_SECRC (1 << 26) // Strip Ethernet CRC
|
2019-03-10 22:59:23 +03:00
|
|
|
|
|
|
|
// Buffer Sizes
|
2019-06-07 12:43:58 +03:00
|
|
|
#define RCTL_BSIZE_256 (3 << 16)
|
|
|
|
#define RCTL_BSIZE_512 (2 << 16)
|
|
|
|
#define RCTL_BSIZE_1024 (1 << 16)
|
|
|
|
#define RCTL_BSIZE_2048 (0 << 16)
|
|
|
|
#define RCTL_BSIZE_4096 ((3 << 16) | (1 << 25))
|
|
|
|
#define RCTL_BSIZE_8192 ((2 << 16) | (1 << 25))
|
|
|
|
#define RCTL_BSIZE_16384 ((1 << 16) | (1 << 25))
|
2019-03-10 22:59:23 +03:00
|
|
|
|
|
|
|
// Transmit Command
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
#define CMD_EOP (1 << 0) // End of Packet
|
|
|
|
#define CMD_IFCS (1 << 1) // Insert FCS
|
|
|
|
#define CMD_IC (1 << 2) // Insert Checksum
|
|
|
|
#define CMD_RS (1 << 3) // Report Status
|
|
|
|
#define CMD_RPS (1 << 4) // Report Packet Sent
|
|
|
|
#define CMD_VLE (1 << 6) // VLAN Packet Enable
|
|
|
|
#define CMD_IDE (1 << 7) // Interrupt Delay Enable
|
2019-03-10 22:59:23 +03:00
|
|
|
|
|
|
|
// TCTL Register
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
#define TCTL_EN (1 << 1) // Transmit Enable
|
|
|
|
#define TCTL_PSP (1 << 3) // Pad Short Packets
|
|
|
|
#define TCTL_CT_SHIFT 4 // Collision Threshold
|
|
|
|
#define TCTL_COLD_SHIFT 12 // Collision Distance
|
|
|
|
#define TCTL_SWXOFF (1 << 22) // Software XOFF Transmission
|
|
|
|
#define TCTL_RTLC (1 << 24) // Re-transmit on Late Collision
|
2019-03-10 22:59:23 +03:00
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
#define TSTA_DD (1 << 0) // Descriptor Done
|
|
|
|
#define TSTA_EC (1 << 1) // Excess Collisions
|
|
|
|
#define TSTA_LC (1 << 2) // Late Collision
|
|
|
|
#define LSTA_TU (1 << 3) // Transmit Underrun
|
2019-03-10 17:25:33 +03:00
|
|
|
|
2019-08-21 17:52:50 +03:00
|
|
|
// STATUS Register
|
|
|
|
|
|
|
|
#define STATUS_FD 0x01
|
|
|
|
#define STATUS_LU 0x02
|
|
|
|
#define STATUS_TXOFF 0x08
|
|
|
|
#define STATUS_SPEED 0xC0
|
|
|
|
#define STATUS_SPEED_10MB 0x00
|
|
|
|
#define STATUS_SPEED_100MB 0x40
|
|
|
|
#define STATUS_SPEED_1000MB1 0x80
|
|
|
|
#define STATUS_SPEED_1000MB2 0xC0
|
|
|
|
|
2020-03-20 01:44:42 +03:00
|
|
|
// Interrupt Masks
|
|
|
|
|
|
|
|
#define INTERRUPT_TXDW (1 << 0)
|
|
|
|
#define INTERRUPT_TXQE (1 << 1)
|
|
|
|
#define INTERRUPT_LSC (1 << 2)
|
|
|
|
#define INTERRUPT_RXSEQ (1 << 3)
|
|
|
|
#define INTERRUPT_RXDMT0 (1 << 4)
|
|
|
|
#define INTERRUPT_RXO (1 << 6)
|
|
|
|
#define INTERRUPT_RXT0 (1 << 7)
|
|
|
|
#define INTERRUPT_MDAC (1 << 9)
|
|
|
|
#define INTERRUPT_RXCFG (1 << 10)
|
|
|
|
#define INTERRUPT_PHYINT (1 << 12)
|
|
|
|
#define INTERRUPT_TXD_LOW (1 << 15)
|
|
|
|
#define INTERRUPT_SRPD (1 << 16)
|
|
|
|
|
2020-12-22 13:50:07 +03:00
|
|
|
// https://www.intel.com/content/dam/doc/manual/pci-pci-x-family-gbe-controllers-software-dev-manual.pdf Section 5.2
|
2021-06-09 10:50:21 +03:00
|
|
|
UNMAP_AFTER_INIT static bool is_valid_device_id(u16 device_id)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2020-12-22 13:50:07 +03:00
|
|
|
// FIXME: It would be nice to distinguish which particular device it is.
|
|
|
|
// Especially since it's needed to determine which registers we can access.
|
|
|
|
// The reason I haven't done it now is because there's some IDs with multiple devices
|
|
|
|
// and some devices with multiple IDs.
|
|
|
|
switch (device_id) {
|
|
|
|
case 0x1019: // 82547EI-A0, 82547EI-A1, 82547EI-B0, 82547GI-B0
|
|
|
|
case 0x101A: // 82547EI-B0
|
|
|
|
case 0x1010: // 82546EB-A1
|
|
|
|
case 0x1012: // 82546EB-A1
|
|
|
|
case 0x101D: // 82546EB-A1
|
|
|
|
case 0x1079: // 82546GB-B0
|
|
|
|
case 0x107A: // 82546GB-B0
|
|
|
|
case 0x107B: // 82546GB-B0
|
|
|
|
case 0x100F: // 82545EM-A
|
|
|
|
case 0x1011: // 82545EM-A
|
|
|
|
case 0x1026: // 82545GM-B
|
|
|
|
case 0x1027: // 82545GM-B
|
|
|
|
case 0x1028: // 82545GM-B
|
|
|
|
case 0x1107: // 82544EI-A4
|
|
|
|
case 0x1112: // 82544GC-A4
|
|
|
|
case 0x1013: // 82541EI-A0, 82541EI-B0
|
|
|
|
case 0x1018: // 82541EI-B0
|
|
|
|
case 0x1076: // 82541GI-B1, 82541PI-C0
|
|
|
|
case 0x1077: // 82541GI-B1
|
|
|
|
case 0x1078: // 82541ER-C0
|
|
|
|
case 0x1017: // 82540EP-A
|
|
|
|
case 0x1016: // 82540EP-A
|
|
|
|
case 0x100E: // 82540EM-A
|
|
|
|
case 0x1015: // 82540EM-A
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 20:33:30 +03:00
|
|
|
|
2021-09-23 10:20:54 +03:00
|
|
|
UNMAP_AFTER_INIT RefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
|
2020-12-22 13:50:07 +03:00
|
|
|
{
|
2021-09-23 10:20:54 +03:00
|
|
|
if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Intel)
|
2021-06-04 07:43:16 +03:00
|
|
|
return {};
|
2021-09-23 10:20:54 +03:00
|
|
|
if (!is_valid_device_id(pci_device_identifier.hardware_id().device_id))
|
2021-06-04 07:43:16 +03:00
|
|
|
return {};
|
2021-09-23 10:50:45 +03:00
|
|
|
u8 irq = pci_device_identifier.interrupt_line().value();
|
2021-09-23 10:20:54 +03:00
|
|
|
auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000NetworkAdapter(pci_device_identifier.address(), irq));
|
2021-06-04 10:11:37 +03:00
|
|
|
if (!adapter)
|
|
|
|
return {};
|
|
|
|
if (adapter->initialize())
|
|
|
|
return adapter;
|
|
|
|
return {};
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2021-06-04 10:11:37 +03:00
|
|
|
UNMAP_AFTER_INIT void E1000NetworkAdapter::setup_link()
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2021-06-04 10:11:37 +03:00
|
|
|
u32 flags = in32(REG_CTRL);
|
|
|
|
out32(REG_CTRL, flags | ECTRL_SLU);
|
|
|
|
}
|
2019-06-16 08:06:49 +03:00
|
|
|
|
2021-06-04 10:11:37 +03:00
|
|
|
UNMAP_AFTER_INIT void E1000NetworkAdapter::setup_interrupts()
|
|
|
|
{
|
|
|
|
out32(REG_INTERRUPT_RATE, 6000); // Interrupt rate of 1.536 milliseconds
|
|
|
|
out32(REG_INTERRUPT_MASK_SET, INTERRUPT_LSC | INTERRUPT_RXT0 | INTERRUPT_RXO);
|
|
|
|
in32(REG_INTERRUPT_CAUSE_READ);
|
|
|
|
enable_irq();
|
|
|
|
}
|
2019-03-11 13:11:29 +03:00
|
|
|
|
2021-06-04 10:11:37 +03:00
|
|
|
UNMAP_AFTER_INIT bool E1000NetworkAdapter::initialize()
|
|
|
|
{
|
|
|
|
dmesgln("E1000: Found @ {}", pci_address());
|
2020-02-23 00:56:00 +03:00
|
|
|
enable_bus_mastering(pci_address());
|
2019-03-11 13:11:29 +03:00
|
|
|
|
2021-06-04 10:11:37 +03:00
|
|
|
m_io_base = IOAddress(PCI::get_BAR1(pci_address()) & ~1);
|
|
|
|
|
2020-04-08 17:56:36 +03:00
|
|
|
size_t mmio_base_size = PCI::get_BAR_space_size(pci_address(), 0);
|
2021-09-06 02:36:14 +03:00
|
|
|
auto region_or_error = MM.allocate_kernel_region(PhysicalAddress(page_base_of(PCI::get_BAR0(pci_address()))), Memory::page_round_up(mmio_base_size), "E1000 MMIO", Memory::Region::Access::ReadWrite, Memory::Region::Cacheable::No);
|
|
|
|
if (region_or_error.is_error())
|
2021-06-04 10:11:37 +03:00
|
|
|
return false;
|
2021-09-06 02:36:14 +03:00
|
|
|
m_mmio_region = region_or_error.release_value();
|
2020-01-10 00:34:26 +03:00
|
|
|
m_mmio_base = m_mmio_region->vaddr();
|
2019-03-10 17:25:33 +03:00
|
|
|
m_use_mmio = true;
|
2021-03-10 00:07:41 +03:00
|
|
|
dmesgln("E1000: port base: {}", m_io_base);
|
|
|
|
dmesgln("E1000: MMIO base: {}", PhysicalAddress(PCI::get_BAR0(pci_address()) & 0xfffffffc));
|
|
|
|
dmesgln("E1000: MMIO base size: {} bytes", mmio_base_size);
|
2021-09-23 10:50:45 +03:00
|
|
|
dmesgln("E1000: Interrupt line: {}", interrupt_number());
|
2019-03-10 17:25:33 +03:00
|
|
|
detect_eeprom();
|
2021-03-10 00:07:41 +03:00
|
|
|
dmesgln("E1000: Has EEPROM? {}", m_has_eeprom);
|
2019-03-10 17:25:33 +03:00
|
|
|
read_mac_address();
|
2019-03-10 21:15:22 +03:00
|
|
|
const auto& mac = mac_address();
|
2021-03-10 00:07:41 +03:00
|
|
|
dmesgln("E1000: MAC address: {}", mac.to_string());
|
2019-03-10 22:59:23 +03:00
|
|
|
|
|
|
|
initialize_rx_descriptors();
|
|
|
|
initialize_tx_descriptors();
|
|
|
|
|
2021-06-04 10:11:37 +03:00
|
|
|
setup_link();
|
|
|
|
setup_interrupts();
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-10 22:59:23 +03:00
|
|
|
|
2021-06-04 10:11:37 +03:00
|
|
|
UNMAP_AFTER_INIT E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq)
|
2021-08-21 06:58:43 +03:00
|
|
|
: PCI::Device(address)
|
Kernel/PCI: Delete PCI::Device in its current form
I created this class a long time ago just to be able to quickly make a
PCI device to also represent an interrupt handler (because PCI devices
have this capability for most devices).
Then after a while I introduced the PCI::DeviceController, which is
really almost the same thing (a PCI device class that has Address member
in it), but is not tied to interrupts so it can have no interrupts, or
spawn interrupt handlers however it wants to seems fit.
However I decided it's time to say goodbye for this class for
a couple of reasons:
1. It made a whole bunch of weird patterns where you had a PCI::Device
and a PCI::DeviceController being used in the topic of implementation,
where originally, they meant to be used mutually exclusively (you
can't and really don't want to use both).
2. We can really make all the classes that inherit from PCI::Device
to inherit from IRQHandler at this point. Later on, when we have MSI
interrupts support, we can go further and untie things even more.
3. It makes it possible to simplify the VirtIO implementation to a great
extent. While this commit almost doesn't change it, future changes
can untangle some complexity in the VirtIO code.
For UHCIController, E1000NetworkAdapter, NE2000NetworkAdapter,
RTL8139NetworkAdapter, RTL8168NetworkAdapter, E1000ENetworkAdapter we
are simply making them to inherit the IRQHandler. This makes some sense,
because the first 3 devices will never support anything besides IRQs.
For the last 2, they might have MSI support, so when we start to utilize
those, we might need to untie these classes from IRQHandler and spawn
IRQHandler(s) or MSIHandler(s) as needed.
The VirtIODevice class is also a case where we currently need to use
both PCI::DeviceController and IRQHandler classes as parents, but it
could also be untied from the latter.
2021-08-21 06:55:25 +03:00
|
|
|
, IRQHandler(irq)
|
2021-09-06 02:36:14 +03:00
|
|
|
, m_rx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(e1000_rx_desc) * number_of_rx_descriptors + 16), "E1000 RX Descriptors", Memory::Region::Access::ReadWrite).release_value())
|
|
|
|
, m_tx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(e1000_tx_desc) * number_of_tx_descriptors + 16), "E1000 TX Descriptors", Memory::Region::Access::ReadWrite).release_value())
|
2021-06-04 10:11:37 +03:00
|
|
|
{
|
|
|
|
set_interface_name(pci_address());
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:29:46 +03:00
|
|
|
UNMAP_AFTER_INIT E1000NetworkAdapter::~E1000NetworkAdapter()
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-05 09:00:18 +03:00
|
|
|
bool E1000NetworkAdapter::handle_irq(const RegisterState&)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2020-03-20 01:44:42 +03:00
|
|
|
u32 status = in32(REG_INTERRUPT_CAUSE_READ);
|
2020-06-24 23:07:28 +03:00
|
|
|
|
|
|
|
m_entropy_source.add_random_event(status);
|
|
|
|
|
2021-06-05 09:00:18 +03:00
|
|
|
if (status == 0)
|
|
|
|
return false;
|
|
|
|
|
2021-04-26 19:34:49 +03:00
|
|
|
if (status & INTERRUPT_LSC) {
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 flags = in32(REG_CTRL);
|
2019-03-10 22:59:23 +03:00
|
|
|
out32(REG_CTRL, flags | ECTRL_SLU);
|
|
|
|
}
|
2021-04-26 19:34:49 +03:00
|
|
|
if (status & INTERRUPT_RXDMT0) {
|
2020-03-20 01:44:42 +03:00
|
|
|
// Threshold OK?
|
|
|
|
}
|
2021-04-26 19:36:43 +03:00
|
|
|
if (status & INTERRUPT_RXO) {
|
|
|
|
dbgln_if(E1000_DEBUG, "E1000: RX buffer overrun");
|
|
|
|
}
|
2021-04-26 19:34:49 +03:00
|
|
|
if (status & INTERRUPT_RXT0) {
|
|
|
|
receive();
|
|
|
|
}
|
2019-12-14 12:47:17 +03:00
|
|
|
|
|
|
|
m_wait_queue.wake_all();
|
2020-03-20 01:44:42 +03:00
|
|
|
|
2021-04-26 19:37:40 +03:00
|
|
|
out32(REG_INTERRUPT_CAUSE_READ, 0xffffffff);
|
2021-06-05 09:00:18 +03:00
|
|
|
return true;
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2021-02-20 02:00:19 +03:00
|
|
|
UNMAP_AFTER_INIT void E1000NetworkAdapter::detect_eeprom()
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
|
|
|
out32(REG_EEPROM, 0x1);
|
2020-05-16 11:41:27 +03:00
|
|
|
for (int i = 0; i < 999; ++i) {
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 data = in32(REG_EEPROM);
|
2019-03-10 17:25:33 +03:00
|
|
|
if (data & 0x10) {
|
|
|
|
m_has_eeprom = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_has_eeprom = false;
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:00:19 +03:00
|
|
|
UNMAP_AFTER_INIT u32 E1000NetworkAdapter::read_eeprom(u8 address)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 data = 0;
|
|
|
|
u32 tmp = 0;
|
2019-03-10 17:25:33 +03:00
|
|
|
if (m_has_eeprom) {
|
2019-07-03 22:17:35 +03:00
|
|
|
out32(REG_EEPROM, ((u32)address << 8) | 1);
|
2019-03-10 17:25:33 +03:00
|
|
|
while (!((tmp = in32(REG_EEPROM)) & (1 << 4)))
|
|
|
|
;
|
|
|
|
} else {
|
2019-07-03 22:17:35 +03:00
|
|
|
out32(REG_EEPROM, ((u32)address << 2) | 1);
|
2019-03-10 17:25:33 +03:00
|
|
|
while (!((tmp = in32(REG_EEPROM)) & (1 << 1)))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
data = (tmp >> 16) & 0xffff;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:00:19 +03:00
|
|
|
UNMAP_AFTER_INIT void E1000NetworkAdapter::read_mac_address()
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
|
|
|
if (m_has_eeprom) {
|
2020-11-17 03:41:46 +03:00
|
|
|
MACAddress mac {};
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 tmp = read_eeprom(0);
|
2019-03-10 17:25:33 +03:00
|
|
|
mac[0] = tmp & 0xff;
|
|
|
|
mac[1] = tmp >> 8;
|
|
|
|
tmp = read_eeprom(1);
|
|
|
|
mac[2] = tmp & 0xff;
|
|
|
|
mac[3] = tmp >> 8;
|
|
|
|
tmp = read_eeprom(2);
|
|
|
|
mac[4] = tmp & 0xff;
|
|
|
|
mac[5] = tmp >> 8;
|
|
|
|
set_mac_address(mac);
|
|
|
|
} else {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:52:50 +03:00
|
|
|
bool E1000NetworkAdapter::link_up()
|
|
|
|
{
|
|
|
|
return (in32(REG_STATUS) & STATUS_LU);
|
|
|
|
}
|
|
|
|
|
2021-02-20 02:00:19 +03:00
|
|
|
UNMAP_AFTER_INIT void E1000NetworkAdapter::initialize_rx_descriptors()
|
2019-03-10 22:59:23 +03:00
|
|
|
{
|
2020-03-06 22:40:36 +03:00
|
|
|
auto* rx_descriptors = (e1000_tx_desc*)m_rx_descriptors_region->vaddr().as_ptr();
|
2021-07-19 02:30:31 +03:00
|
|
|
constexpr auto rx_buffer_size = 8192;
|
|
|
|
constexpr auto rx_buffer_page_count = rx_buffer_size / PAGE_SIZE;
|
|
|
|
|
2021-09-06 02:36:14 +03:00
|
|
|
m_rx_buffer_region = MM.allocate_contiguous_kernel_region(rx_buffer_size * number_of_rx_descriptors, "E1000 RX buffers", Memory::Region::Access::ReadWrite).release_value();
|
2020-04-28 21:24:44 +03:00
|
|
|
for (size_t i = 0; i < number_of_rx_descriptors; ++i) {
|
2020-03-06 22:40:36 +03:00
|
|
|
auto& descriptor = rx_descriptors[i];
|
2021-07-19 02:30:31 +03:00
|
|
|
m_rx_buffers[i] = m_rx_buffer_region->vaddr().as_ptr() + rx_buffer_size * i;
|
|
|
|
descriptor.addr = m_rx_buffer_region->physical_page(rx_buffer_page_count * i)->paddr().get();
|
2019-03-10 22:59:23 +03:00
|
|
|
descriptor.status = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-28 17:19:50 +03:00
|
|
|
out32(REG_RXDESCLO, m_rx_descriptors_region->physical_page(0)->paddr().get());
|
2019-03-10 22:59:23 +03:00
|
|
|
out32(REG_RXDESCHI, 0);
|
|
|
|
out32(REG_RXDESCLEN, number_of_rx_descriptors * sizeof(e1000_rx_desc));
|
|
|
|
out32(REG_RXDESCHEAD, 0);
|
|
|
|
out32(REG_RXDESCTAIL, number_of_rx_descriptors - 1);
|
|
|
|
|
2019-06-07 12:43:58 +03:00
|
|
|
out32(REG_RCTRL, RCTL_EN | RCTL_SBP | RCTL_UPE | RCTL_MPE | RCTL_LBM_NONE | RTCL_RDMTS_HALF | RCTL_BAM | RCTL_SECRC | RCTL_BSIZE_8192);
|
2019-03-10 22:59:23 +03:00
|
|
|
}
|
|
|
|
|
2021-02-20 02:00:19 +03:00
|
|
|
UNMAP_AFTER_INIT void E1000NetworkAdapter::initialize_tx_descriptors()
|
2019-03-10 22:59:23 +03:00
|
|
|
{
|
2020-03-06 22:40:36 +03:00
|
|
|
auto* tx_descriptors = (e1000_tx_desc*)m_tx_descriptors_region->vaddr().as_ptr();
|
2021-07-19 02:30:31 +03:00
|
|
|
|
|
|
|
constexpr auto tx_buffer_size = 8192;
|
|
|
|
constexpr auto tx_buffer_page_count = tx_buffer_size / PAGE_SIZE;
|
2021-09-06 02:36:14 +03:00
|
|
|
m_tx_buffer_region = MM.allocate_contiguous_kernel_region(tx_buffer_size * number_of_tx_descriptors, "E1000 TX buffers", Memory::Region::Access::ReadWrite).release_value();
|
2021-07-19 02:30:31 +03:00
|
|
|
|
2020-04-28 21:24:44 +03:00
|
|
|
for (size_t i = 0; i < number_of_tx_descriptors; ++i) {
|
2020-03-06 22:40:36 +03:00
|
|
|
auto& descriptor = tx_descriptors[i];
|
2021-07-19 02:30:31 +03:00
|
|
|
m_tx_buffers[i] = m_tx_buffer_region->vaddr().as_ptr() + tx_buffer_size * i;
|
|
|
|
descriptor.addr = m_tx_buffer_region->physical_page(tx_buffer_page_count * i)->paddr().get();
|
2019-03-10 22:59:23 +03:00
|
|
|
descriptor.cmd = 0;
|
|
|
|
}
|
|
|
|
|
2020-04-28 17:19:50 +03:00
|
|
|
out32(REG_TXDESCLO, m_tx_descriptors_region->physical_page(0)->paddr().get());
|
2019-03-10 22:59:23 +03:00
|
|
|
out32(REG_TXDESCHI, 0);
|
|
|
|
out32(REG_TXDESCLEN, number_of_tx_descriptors * sizeof(e1000_tx_desc));
|
|
|
|
out32(REG_TXDESCHEAD, 0);
|
2019-03-12 01:21:38 +03:00
|
|
|
out32(REG_TXDESCTAIL, 0);
|
2019-03-10 22:59:23 +03:00
|
|
|
|
2019-03-12 01:21:38 +03:00
|
|
|
out32(REG_TCTRL, in32(REG_TCTRL) | TCTL_EN | TCTL_PSP);
|
2019-03-10 22:59:23 +03:00
|
|
|
out32(REG_TIPG, 0x0060200A);
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void E1000NetworkAdapter::out8(u16 address, u8 data)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: OUT8 {:#02x} @ {:#04x}", data, address);
|
2019-03-10 17:25:33 +03:00
|
|
|
if (m_use_mmio) {
|
2019-07-03 22:17:35 +03:00
|
|
|
auto* ptr = (volatile u8*)(m_mmio_base.get() + address);
|
2019-03-10 17:25:33 +03:00
|
|
|
*ptr = data;
|
|
|
|
return;
|
|
|
|
}
|
2020-03-01 17:16:26 +03:00
|
|
|
m_io_base.offset(address).out(data);
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void E1000NetworkAdapter::out16(u16 address, u16 data)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: OUT16 {:#04x} @ {:#04x}", data, address);
|
2019-03-10 17:25:33 +03:00
|
|
|
if (m_use_mmio) {
|
2019-07-03 22:17:35 +03:00
|
|
|
auto* ptr = (volatile u16*)(m_mmio_base.get() + address);
|
2019-03-10 17:25:33 +03:00
|
|
|
*ptr = data;
|
|
|
|
return;
|
|
|
|
}
|
2020-03-01 17:16:26 +03:00
|
|
|
m_io_base.offset(address).out(data);
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void E1000NetworkAdapter::out32(u16 address, u32 data)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: OUT32 {:#08x} @ {:#04x}", data, address);
|
2019-03-10 17:25:33 +03:00
|
|
|
if (m_use_mmio) {
|
2019-07-03 22:17:35 +03:00
|
|
|
auto* ptr = (volatile u32*)(m_mmio_base.get() + address);
|
2019-03-10 17:25:33 +03:00
|
|
|
*ptr = data;
|
|
|
|
return;
|
|
|
|
}
|
2020-03-01 17:16:26 +03:00
|
|
|
m_io_base.offset(address).out(data);
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 E1000NetworkAdapter::in8(u16 address)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: IN8 @ {:#04x}", address);
|
2019-03-10 17:25:33 +03:00
|
|
|
if (m_use_mmio)
|
2019-07-03 22:17:35 +03:00
|
|
|
return *(volatile u8*)(m_mmio_base.get() + address);
|
2020-03-01 17:16:26 +03:00
|
|
|
return m_io_base.offset(address).in<u8>();
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 E1000NetworkAdapter::in16(u16 address)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: IN16 @ {:#04x}", address);
|
2019-03-10 17:25:33 +03:00
|
|
|
if (m_use_mmio)
|
2019-07-03 22:17:35 +03:00
|
|
|
return *(volatile u16*)(m_mmio_base.get() + address);
|
2020-03-01 17:16:26 +03:00
|
|
|
return m_io_base.offset(address).in<u16>();
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 E1000NetworkAdapter::in32(u16 address)
|
2019-03-10 17:25:33 +03:00
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: IN32 @ {:#04x}", address);
|
2019-03-10 17:25:33 +03:00
|
|
|
if (m_use_mmio)
|
2019-07-03 22:17:35 +03:00
|
|
|
return *(volatile u32*)(m_mmio_base.get() + address);
|
2020-03-01 17:16:26 +03:00
|
|
|
return m_io_base.offset(address).in<u32>();
|
2019-03-10 17:25:33 +03:00
|
|
|
}
|
2019-03-10 22:59:23 +03:00
|
|
|
|
2020-07-28 21:19:22 +03:00
|
|
|
void E1000NetworkAdapter::send_raw(ReadonlyBytes payload)
|
2019-03-10 22:59:23 +03:00
|
|
|
{
|
2020-01-23 00:23:50 +03:00
|
|
|
disable_irq();
|
2020-04-28 21:24:44 +03:00
|
|
|
size_t tx_current = in32(REG_TXDESCTAIL) % number_of_tx_descriptors;
|
2021-03-10 00:07:41 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: Sending packet ({} bytes)", payload.size());
|
2020-03-06 22:40:36 +03:00
|
|
|
auto* tx_descriptors = (e1000_tx_desc*)m_tx_descriptors_region->vaddr().as_ptr();
|
|
|
|
auto& descriptor = tx_descriptors[tx_current];
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(payload.size() <= 8192);
|
2021-07-19 02:30:31 +03:00
|
|
|
auto* vptr = (void*)m_tx_buffers[tx_current];
|
2020-07-28 21:19:22 +03:00
|
|
|
memcpy(vptr, payload.data(), payload.size());
|
|
|
|
descriptor.length = payload.size();
|
2019-03-12 01:21:38 +03:00
|
|
|
descriptor.status = 0;
|
2019-03-11 14:43:45 +03:00
|
|
|
descriptor.cmd = CMD_EOP | CMD_IFCS | CMD_RS;
|
2021-03-10 00:07:41 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: Using tx descriptor {} (head is at {})", tx_current, in32(REG_TXDESCHEAD));
|
2019-03-12 01:21:38 +03:00
|
|
|
tx_current = (tx_current + 1) % number_of_tx_descriptors;
|
2019-12-14 15:02:40 +03:00
|
|
|
cli();
|
2020-01-23 00:23:50 +03:00
|
|
|
enable_irq();
|
2020-03-06 16:52:44 +03:00
|
|
|
out32(REG_TXDESCTAIL, tx_current);
|
2019-12-14 12:47:17 +03:00
|
|
|
for (;;) {
|
2019-12-14 15:02:40 +03:00
|
|
|
if (descriptor.status) {
|
|
|
|
sti();
|
2019-12-14 12:47:17 +03:00
|
|
|
break;
|
2019-12-14 15:02:40 +03:00
|
|
|
}
|
2021-02-15 02:02:14 +03:00
|
|
|
m_wait_queue.wait_forever("E1000NetworkAdapter");
|
2019-12-14 12:47:17 +03:00
|
|
|
}
|
2021-03-10 00:07:41 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: Sent packet, status is now {:#02x}!", (u8)descriptor.status);
|
2019-03-10 22:59:23 +03:00
|
|
|
}
|
2019-03-11 01:40:09 +03:00
|
|
|
|
|
|
|
void E1000NetworkAdapter::receive()
|
|
|
|
{
|
2020-03-06 22:40:36 +03:00
|
|
|
auto* rx_descriptors = (e1000_tx_desc*)m_rx_descriptors_region->vaddr().as_ptr();
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 rx_current;
|
2021-05-01 20:51:24 +03:00
|
|
|
for (;;) {
|
2020-04-28 21:24:44 +03:00
|
|
|
rx_current = in32(REG_RXDESCTAIL) % number_of_rx_descriptors;
|
2019-03-12 01:21:38 +03:00
|
|
|
rx_current = (rx_current + 1) % number_of_rx_descriptors;
|
2020-03-06 22:40:36 +03:00
|
|
|
if (!(rx_descriptors[rx_current].status & 1))
|
2019-03-12 01:21:38 +03:00
|
|
|
break;
|
2021-07-19 02:30:31 +03:00
|
|
|
auto* buffer = m_rx_buffers[rx_current];
|
2020-03-06 22:40:36 +03:00
|
|
|
u16 length = rx_descriptors[rx_current].length;
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(length <= 8192);
|
2021-03-10 00:07:41 +03:00
|
|
|
dbgln_if(E1000_DEBUG, "E1000: Received 1 packet @ {:p} ({} bytes)", buffer, length);
|
2020-07-28 21:19:22 +03:00
|
|
|
did_receive({ buffer, length });
|
2020-03-06 22:40:36 +03:00
|
|
|
rx_descriptors[rx_current].status = 0;
|
2019-03-12 01:21:38 +03:00
|
|
|
out32(REG_RXDESCTAIL, rx_current);
|
2019-03-11 01:40:09 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
2021-07-25 21:16:42 +03:00
|
|
|
i32 E1000NetworkAdapter::link_speed()
|
|
|
|
{
|
|
|
|
if (!link_up())
|
|
|
|
return NetworkAdapter::LINKSPEED_INVALID;
|
|
|
|
|
|
|
|
u32 speed = in32(REG_STATUS) & STATUS_SPEED;
|
|
|
|
switch (speed) {
|
|
|
|
case STATUS_SPEED_10MB:
|
|
|
|
return 10;
|
|
|
|
case STATUS_SPEED_100MB:
|
|
|
|
return 100;
|
|
|
|
case STATUS_SPEED_1000MB1:
|
|
|
|
case STATUS_SPEED_1000MB2:
|
|
|
|
return 1000;
|
|
|
|
default:
|
|
|
|
return NetworkAdapter::LINKSPEED_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool E1000NetworkAdapter::link_full_duplex()
|
|
|
|
{
|
|
|
|
u32 status = in32(REG_STATUS);
|
|
|
|
return !!(status & STATUS_FD);
|
|
|
|
}
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|