Kernel: Use IOAddress class in Network adapters' drivers

Also, kprintf() calls were replaced with klog() calls.
This commit is contained in:
Liav A 2020-03-01 16:16:26 +02:00 committed by Andreas Kling
parent 7d39e380f9
commit 15dfca4a79
Notes: sideshowbarker 2024-07-19 08:55:54 +09:00
4 changed files with 52 additions and 50 deletions

View file

@ -135,10 +135,11 @@ void E1000NetworkAdapter::detect(const PCI::Address& address)
E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq)
: PCI::Device(address, irq)
, m_io_base(PCI::get_BAR1(pci_address()) & ~1)
{
set_interface_name("e1k");
kprintf("E1000: Found at PCI address @ %w:%b:%b.%b\n", pci_address().seg(), pci_address().bus(), pci_address().slot(), pci_address().function());
klog() << "E1000: Found at PCI address @ " << String::format("%w", pci_address().seg()) << ":" << String::format("%b", pci_address().bus()) << ":" << String::format("%b", pci_address().slot()) << "." << String::format("%b", pci_address().function());
enable_bus_mastering(pci_address());
@ -146,17 +147,16 @@ E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq)
m_mmio_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of(PCI::get_BAR0(pci_address()))), PAGE_ROUND_UP(mmio_base_size), "E1000 MMIO", Region::Access::Read | Region::Access::Write, false, false);
m_mmio_base = m_mmio_region->vaddr();
m_use_mmio = true;
m_io_base = PCI::get_BAR1(pci_address()) & ~1;
m_interrupt_line = PCI::get_interrupt_line(pci_address());
kprintf("E1000: IO port base: %w\n", m_io_base);
kprintf("E1000: MMIO base: P%x\n", PCI::get_BAR0(pci_address()) & 0xfffffffc);
kprintf("E1000: MMIO base size: %u bytes\n", mmio_base_size);
kprintf("E1000: Interrupt line: %u\n", m_interrupt_line);
klog() << "E1000: port base: " << m_io_base;
klog() << "E1000: MMIO base: " << PhysicalAddress(PCI::get_BAR0(pci_address()) & 0xfffffffc);
klog() << "E1000: MMIO base size: " << mmio_base_size << " bytes";
klog() << "E1000: Interrupt line: " << m_interrupt_line;
detect_eeprom();
kprintf("E1000: Has EEPROM? %u\n", m_has_eeprom);
klog() << "E1000: Has EEPROM? " << m_has_eeprom;
read_mac_address();
const auto& mac = mac_address();
kprintf("E1000: MAC address: %b:%b:%b:%b:%b:%b\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
klog() << "E1000: MAC address: " << String::format("%b", mac[0]) << ":" << String::format("%b", mac[1]) << ":" << String::format("%b", mac[2]) << ":" << String::format("%b", mac[3]) << ":" << String::format("%b", mac[4]) << ":" << String::format("%b", mac[5]);
u32 flags = in32(REG_CTRL);
out32(REG_CTRL, flags | ECTRL_SLU);
@ -309,7 +309,7 @@ void E1000NetworkAdapter::out8(u16 address, u8 data)
*ptr = data;
return;
}
IO::out8(m_io_base + address, data);
m_io_base.offset(address).out(data);
}
void E1000NetworkAdapter::out16(u16 address, u16 data)
@ -322,7 +322,7 @@ void E1000NetworkAdapter::out16(u16 address, u16 data)
*ptr = data;
return;
}
IO::out16(m_io_base + address, data);
m_io_base.offset(address).out(data);
}
void E1000NetworkAdapter::out32(u16 address, u32 data)
@ -335,7 +335,7 @@ void E1000NetworkAdapter::out32(u16 address, u32 data)
*ptr = data;
return;
}
IO::out32(m_io_base + address, data);
m_io_base.offset(address).out(data);
}
u8 E1000NetworkAdapter::in8(u16 address)
@ -345,7 +345,7 @@ u8 E1000NetworkAdapter::in8(u16 address)
#endif
if (m_use_mmio)
return *(volatile u8*)(m_mmio_base.get() + address);
return IO::in8(m_io_base + address);
return m_io_base.offset(address).in<u8>();
}
u16 E1000NetworkAdapter::in16(u16 address)
@ -355,7 +355,7 @@ u16 E1000NetworkAdapter::in16(u16 address)
#endif
if (m_use_mmio)
return *(volatile u16*)(m_mmio_base.get() + address);
return IO::in16(m_io_base + address);
return m_io_base.offset(address).in<u16>();
}
u32 E1000NetworkAdapter::in32(u16 address)
@ -365,7 +365,7 @@ u32 E1000NetworkAdapter::in32(u16 address)
#endif
if (m_use_mmio)
return *(volatile u32*)(m_mmio_base.get() + address);
return IO::in32(m_io_base + address);
return m_io_base.offset(address).in<u32>();
}
void E1000NetworkAdapter::send_raw(const u8* data, size_t length)
@ -373,7 +373,7 @@ void E1000NetworkAdapter::send_raw(const u8* data, size_t length)
disable_irq();
u32 tx_current = in32(REG_TXDESCTAIL);
#ifdef E1000_DEBUG
kprintf("E1000: Sending packet (%zu bytes)\n", length);
klog() << "E1000: Sending packet (" << length << " bytes)";
#endif
auto& descriptor = m_tx_descriptors[tx_current];
ASSERT(length <= 8192);
@ -383,7 +383,7 @@ void E1000NetworkAdapter::send_raw(const u8* data, size_t length)
descriptor.status = 0;
descriptor.cmd = CMD_EOP | CMD_IFCS | CMD_RS;
#ifdef E1000_DEBUG
kprintf("E1000: Using tx descriptor %d (head is at %d)\n", tx_current, in32(REG_TXDESCHEAD));
klog() << "E1000: Using tx descriptor " << tx_current << " (head is at " << in32(REG_TXDESCHEAD) << ")";
#endif
tx_current = (tx_current + 1) % number_of_tx_descriptors;
out32(REG_TXDESCTAIL, tx_current);
@ -397,7 +397,7 @@ void E1000NetworkAdapter::send_raw(const u8* data, size_t length)
Thread::current->wait_on(m_wait_queue);
}
#ifdef E1000_DEBUG
kprintf("E1000: Sent packet, status is now %b!\n", descriptor.status);
klog() << "E1000: Sent packet, status is now " << String::format("%b",descriptor.status) << "!";
#endif
}
@ -414,7 +414,7 @@ void E1000NetworkAdapter::receive()
auto* buffer = (u8*)(m_rx_descriptors[rx_current].addr + 0xc0000000);
u16 length = m_rx_descriptors[rx_current].length;
#ifdef E1000_DEBUG
kprintf("E1000: Received 1 packet @ %p (%zu) bytes!\n", buffer, length);
klog() << "E1000: Received 1 packet @ " << buffer << " (" << length << ") bytes!";
#endif
did_receive(buffer, length);
m_rx_descriptors[rx_current].status = 0;

View file

@ -29,6 +29,7 @@
#include <AK/OwnPtr.h>
#include <Kernel/Interrupts/IRQHandler.h>
#include <Kernel/Net/NetworkAdapter.h>
#include <LibBareMetal/IO.h>
#include <Kernel/PCI/Access.h>
#include <Kernel/PCI/Device.h>
@ -89,7 +90,7 @@ private:
void receive();
u16 m_io_base { 0 };
IOAddress m_io_base;
VirtualAddress m_mmio_base;
OwnPtr<Region> m_mmio_region;
u8 m_interrupt_line { 0 };

View file

@ -139,28 +139,28 @@ void RTL8139NetworkAdapter::detect(const PCI::Address& address)
RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq)
: PCI::Device(address, irq)
, m_io_base(PCI::get_BAR0(pci_address()) & ~1)
{
set_interface_name("rtl8139");
kprintf("RTL8139: Found at PCI address %b:%b:%b\n", pci_address().bus(), pci_address().slot(), pci_address().function());
klog() << "RTL8139: Found at PCI address " << String::format("%w", pci_address().seg()) << ":" << String::format("%b", pci_address().bus()) << ":" << String::format("%b", pci_address().slot()) << "." << String::format("%b", pci_address().function());
enable_bus_mastering(pci_address());
m_io_base = PCI::get_BAR0(pci_address()) & ~1;
m_interrupt_line = PCI::get_interrupt_line(pci_address());
kprintf("RTL8139: IO port base: %w\n", m_io_base);
kprintf("RTL8139: Interrupt line: %u\n", m_interrupt_line);
klog() << "RTL8139: port base: " << m_io_base;
klog() << "RTL8139: Interrupt line: " << m_interrupt_line;
// we add space to account for overhang from the last packet - the rtl8139
// can optionally guarantee that packets will be contiguous by
// purposefully overrunning the rx buffer
m_rx_buffer_addr = (uintptr_t)virtual_to_low_physical(kmalloc_aligned(RX_BUFFER_SIZE + PACKET_SIZE_MAX, 16));
kprintf("RTL8139: RX buffer: P%p\n", m_rx_buffer_addr);
klog() << "RTL8139: RX buffer: " << PhysicalAddress(m_rx_buffer_addr);
auto tx_buffer_addr = (uintptr_t)virtual_to_low_physical(kmalloc_aligned(TX_BUFFER_SIZE * 4, 16));
for (int i = 0; i < RTL8139_TX_BUFFER_COUNT; i++) {
m_tx_buffer_addr[i] = tx_buffer_addr + TX_BUFFER_SIZE * i;
kprintf("RTL8139: TX buffer %d: P%p\n", i, m_tx_buffer_addr[i]);
klog() << "RTL8139: TX buffer " << i << ": " << PhysicalAddress(m_tx_buffer_addr[i]);
}
m_packet_buffer = (uintptr_t)kmalloc(PACKET_SIZE_MAX);
@ -169,7 +169,7 @@ RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq)
read_mac_address();
const auto& mac = mac_address();
kprintf("RTL8139: MAC address: %s\n", mac.to_string().characters());
klog() << "RTL8139: MAC address: " << mac.to_string().characters();
enable_irq();
}
@ -185,7 +185,7 @@ void RTL8139NetworkAdapter::handle_irq(RegisterState&)
out16(REG_ISR, status);
#ifdef RTL8139_DEBUG
kprintf("RTL8139NetworkAdapter::handle_irq status=%#04x\n", status);
klog() << "RTL8139NetworkAdapter::handle_irq status=0x" << String::format("%x", status);
#endif
if ((status & (INT_RXOK | INT_RXERR | INT_TXOK | INT_TXERR | INT_RX_BUFFER_OVERFLOW | INT_LINK_CHANGE | INT_RX_FIFO_OVERFLOW | INT_LENGTH_CHANGE | INT_SYSTEM_ERROR)) == 0)
@ -193,38 +193,38 @@ void RTL8139NetworkAdapter::handle_irq(RegisterState&)
if (status & INT_RXOK) {
#ifdef RTL8139_DEBUG
kprintf("RTL8139NetworkAdapter: rx ready\n");
klog() << "RTL8139NetworkAdapter: rx ready";
#endif
receive();
}
if (status & INT_RXERR) {
kprintf("RTL8139NetworkAdapter: rx error - resetting device\n");
klog() << "RTL8139NetworkAdapter: rx error - resetting device";
reset();
}
if (status & INT_TXOK) {
#ifdef RTL8139_DEBUG
kprintf("RTL8139NetworkAdapter: tx complete\n");
klog() << "RTL8139NetworkAdapter: tx complete";
#endif
}
if (status & INT_TXERR) {
kprintf("RTL8139NetworkAdapter: tx error - resetting device\n");
klog() << "RTL8139NetworkAdapter: tx error - resetting device";
reset();
}
if (status & INT_RX_BUFFER_OVERFLOW) {
kprintf("RTL8139NetworkAdapter: rx buffer overflow\n");
klog() << "RTL8139NetworkAdapter: rx buffer overflow";
}
if (status & INT_LINK_CHANGE) {
m_link_up = (in8(REG_MSR) & MSR_LINKB) == 0;
kprintf("RTL8139NetworkAdapter: link status changed up=%d\n", m_link_up);
klog() << "RTL8139NetworkAdapter: link status changed up=" << m_link_up;
}
if (status & INT_RX_FIFO_OVERFLOW) {
kprintf("RTL8139NetworkAdapter: rx fifo overflow\n");
klog() << "RTL8139NetworkAdapter: rx fifo overflow";
}
if (status & INT_LENGTH_CHANGE) {
kprintf("RTL8139NetworkAdapter: cable length change\n");
klog() << "RTL8139NetworkAdapter: cable length change";
}
if (status & INT_SYSTEM_ERROR) {
kprintf("RTL8139NetworkAdapter: system error - resetting device\n");
klog() << "RTL8139NetworkAdapter: system error - resetting device";
reset();
}
}
@ -291,11 +291,11 @@ void RTL8139NetworkAdapter::read_mac_address()
void RTL8139NetworkAdapter::send_raw(const u8* data, size_t length)
{
#ifdef RTL8139_DEBUG
kprintf("RTL8139NetworkAdapter::send_raw length=%d\n", length);
klog() << "RTL8139NetworkAdapter::send_raw length=" << length;
#endif
if (length > PACKET_SIZE_MAX) {
kprintf("RTL8139NetworkAdapter: packet was too big; discarding\n");
klog() << "RTL8139NetworkAdapter: packet was too big; discarding";
return;
}
@ -311,11 +311,11 @@ void RTL8139NetworkAdapter::send_raw(const u8* data, size_t length)
}
if (hw_buffer == -1) {
kprintf("RTL8139NetworkAdapter: hardware buffers full; discarding packet\n");
klog() << "RTL8139NetworkAdapter: hardware buffers full; discarding packet";
return;
} else {
#ifdef RTL8139_DEBUG
kprintf("RTL8139NetworkAdapter: chose buffer %d @ %p\n", hw_buffer, m_tx_buffer_addr[hw_buffer]);
klog() << "RTL8139NetworkAdapter: chose buffer " << hw_buffer << " @ " << PhysicalAddress(m_tx_buffer_addr[hw_buffer]);
#endif
m_tx_next_buffer = (hw_buffer + 1) % 4;
}
@ -329,7 +329,7 @@ void RTL8139NetworkAdapter::send_raw(const u8* data, size_t length)
// 60 bytes if necessary to make sure the whole thing is large enough.
if (length < 60) {
#ifdef RTL8139_DEBUG
kprintf("RTL8139NetworkAdapter: adjusting payload size from %zu to 60\n", length);
klog() << "RTL8139NetworkAdapter: adjusting payload size from " << length << " to 60";
#endif
length = 60;
}
@ -345,11 +345,11 @@ void RTL8139NetworkAdapter::receive()
u16 length = *(const u16*)(start_of_packet + 2);
#ifdef RTL8139_DEBUG
kprintf("RTL8139NetworkAdapter::receive status=%04x length=%d offset=%d\n", status, length, m_rx_buffer_offset);
klog() << "RTL8139NetworkAdapter::receive status=0x" << String::format("%x",status) << " length=" << length << " offset=" << m_rx_buffer_offset;
#endif
if (!(status & RX_OK) || (status & (RX_INVALID_SYMBOL_ERROR | RX_CRC_ERROR | RX_FRAME_ALIGNMENT_ERROR)) || (length >= PACKET_SIZE_MAX) || (length < PACKET_SIZE_MIN)) {
kprintf("RTL8139NetworkAdapter::receive got bad packet status=%04x length=%d\n", status, length);
klog() << "RTL8139NetworkAdapter::receive got bad packet status=0x" << String::format("%x",status) << " length=" << length;
reset();
return;
}
@ -368,32 +368,32 @@ void RTL8139NetworkAdapter::receive()
void RTL8139NetworkAdapter::out8(u16 address, u8 data)
{
IO::out8(m_io_base + address, data);
m_io_base.offset(address).out(data);
}
void RTL8139NetworkAdapter::out16(u16 address, u16 data)
{
IO::out16(m_io_base + address, data);
m_io_base.offset(address).out(data);
}
void RTL8139NetworkAdapter::out32(u16 address, u32 data)
{
IO::out32(m_io_base + address, data);
m_io_base.offset(address).out(data);
}
u8 RTL8139NetworkAdapter::in8(u16 address)
{
return IO::in8(m_io_base + address);
return m_io_base.offset(address).in<u8>();
}
u16 RTL8139NetworkAdapter::in16(u16 address)
{
return IO::in16(m_io_base + address);
return m_io_base.offset(address).in<u16>();
}
u32 RTL8139NetworkAdapter::in32(u16 address)
{
return IO::in32(m_io_base + address);
return m_io_base.offset(address).in<u32>();
}
}

View file

@ -30,6 +30,7 @@
#include <Kernel/Net/NetworkAdapter.h>
#include <Kernel/PCI/Access.h>
#include <Kernel/PCI/Device.h>
#include <LibBareMetal/IO.h>
namespace Kernel {
@ -62,7 +63,7 @@ private:
u16 in16(u16 address);
u32 in32(u16 address);
u16 m_io_base { 0 };
IOAddress m_io_base;
u8 m_interrupt_line { 0 };
u32 m_rx_buffer_addr { 0 };
u16 m_rx_buffer_offset { 0 };