diff options
author | Anton Kling <anton@kling.gg> | 2023-10-26 22:35:58 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-10-30 21:49:48 +0100 |
commit | f8e15da04472f5ed6a26e588de4a23cb3e1ba20b (patch) | |
tree | 9e646d6ba72dcc8f2eb95d04bac2a8b4a91ad946 /drivers | |
parent | cf60045bdb969f6fe44fe7dc9bf7cec593a0b05c (diff) |
Kernel: Fixup rtl8139, add basic ARP and ethernet support
Currently the ARP appears to be able to respond to requests.
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/rtl8139.c | 109 | ||||
-rw-r--r-- | drivers/rtl8139.h | 3 |
2 files changed, 88 insertions, 24 deletions
diff --git a/drivers/rtl8139.c b/drivers/rtl8139.c index 0d665b1..0c1606b 100644 --- a/drivers/rtl8139.c +++ b/drivers/rtl8139.c @@ -4,6 +4,7 @@ #include <drivers/pci.h> #include <drivers/rtl8139.h> #include <mmu.h> +#include <network/ethernet.h> #define RBSTART 0x30 #define CMD 0x37 @@ -13,31 +14,84 @@ #define TSAD0 0x20 // transmit start address struct PCI_DEVICE rtl8139; -uint8_t device_buffer[8192 + 16]; -uint32_t g_base_address; +uint8_t *device_buffer; uint8_t *send_buffers[4]; -#define ROK (1 << 0) -#define TOK (1 << 2) +struct _INT_PACKET_HEADER { + uint8_t ROK : 1; + uint8_t FAE : 1; + uint8_t CRC : 1; + uint8_t LONG : 1; + uint8_t RUNT : 1; + uint8_t ISE : 1; + uint8_t reserved : 5; + uint8_t BAR : 1; + uint8_t PAM : 1; + uint8_t MAR : 1; +}; + +struct PACKET_HEADER { + union { + uint16_t raw; + struct _INT_PACKET_HEADER data; + }; +}; + +uint16_t current_packet_read = 0; + +void handle_packet(void) { + assert(sizeof(struct _INT_PACKET_HEADER) == sizeof(uint16_t)); + + uint16_t *buf = (uint16_t *)(device_buffer + current_packet_read); + struct PACKET_HEADER packet_header; + packet_header.raw = *buf; + assert(packet_header.data.ROK); + kprintf("packet_header.raw: %x\n", packet_header.raw); + uint16_t packet_length = *(buf + 1); + kprintf("packet_length: %x\n", packet_length); + + uint8_t packet_buffer[8192 + 16]; + if (current_packet_read + packet_length >= 8192 + 16) { + uint32_t first_run = ((uint8_t *)buf + (8192 + 16)) - device_buffer; + memcpy(packet_buffer, buf, first_run); + memcpy(packet_buffer, device_buffer, packet_length - first_run); + } else { + memcpy(packet_buffer, buf, packet_length); + } + + handle_ethernet((uint8_t *)packet_buffer + 4, packet_length); + + // Thanks to exscape + // https://github.com/exscape/exscapeOS/blob/master/src/kernel/net/rtl8139.c + // and the programmers guide + // https://www.cs.usfca.edu/~cruse/cs326f04/RTL8139_ProgrammersGuide.pdf I + // have no clue what this calculation, I can't find anything possibly relating + // to this in the manual, but it does work I guess. + current_packet_read = (current_packet_read + packet_length + 4 + 3) & (~3); + current_packet_read %= 8192 + 16; + outw(rtl8139.gen.base_mem_io + 0x38, current_packet_read - 0x10); +} __attribute__((interrupt)) void rtl8139_handler(void *regs) { (void)regs; uint16_t status = inw(rtl8139.gen.base_mem_io + 0x3e); + kprintf("status: %x\n", status); - if (status & TOK) { + outw(rtl8139.gen.base_mem_io + 0x3E, 0x5); + if (status & (1 << 2)) { kprintf("Packet sent\n"); } - if (status & ROK) { + if (status & (1 << 0)) { kprintf("Received packet\n"); + handle_packet(); } - outw(rtl8139.gen.base_mem_io + 0x3E, 0x5); EOI(0xB); } -int rtl8139_send_data(const struct PCI_DEVICE *device, uint8_t *data, - uint16_t data_size) { +int rtl8139_send_data(uint8_t *data, uint16_t data_size) { + const struct PCI_DEVICE *device = &rtl8139; // FIXME: It should block or fail if there is too little space for the // buffer if (data_size > 0x1000) @@ -54,6 +108,19 @@ int rtl8139_send_data(const struct PCI_DEVICE *device, uint8_t *data, return 1; } +void get_mac_address(uint8_t mac[6]) { + uint32_t base_address = rtl8139.gen.base_mem_io; + // Read the MAC address + uint64_t mac_address; + { + uint32_t low_mac = inl(base_address); + uint16_t high_mac = inw(base_address + 0x4); + mac_address = ((uint64_t)high_mac << 32) | low_mac; + } + kprintf("mac_address: %x\n", mac_address); + memcpy(mac, &mac_address, sizeof(uint8_t[6])); +} + uint8_t rtl8139_get_transmit_status(uint32_t base_address) { uint32_t status_register = inl(base_address + 0x3E); if ((status_register >> 3) & 0x1) @@ -76,14 +143,10 @@ void rtl8139_init(void) { uint32_t base_address = rtl8139.gen.base_mem_io; uint8_t interrupt_line = pci_get_interrupt_line(&rtl8139); - // Read the MAC address - uint64_t mac_address; - { - uint32_t low_mac = inl(base_address); - uint16_t high_mac = inw(base_address + 0x4); - mac_address = ((uint64_t)high_mac << 32) | low_mac; - } - kprintf("mac_address: %x\n", mac_address); + // Enable bus mastering + uint32_t register1 = pci_config_read32(&rtl8139, 0, 0x4); + register1 |= (1 << 2); + pci_config_write32(&rtl8139, 0, 0x4, register1); // Turning on the device outb(base_address + 0x52, 0x0); @@ -92,19 +155,21 @@ void rtl8139_init(void) { outb(base_address + CMD, 0x10); for (; 0 != (inb(base_address + CMD) & 0x10);) ; - + device_buffer = ksbrk(8192 + 16); + memset(device_buffer, 0, 8192 + 16); // Setupt the recieve buffer uint32_t rx_buffer = (uint32_t)virtual_to_physical(device_buffer, NULL); outl(base_address + RBSTART, rx_buffer); // Set IMR + ISR - outw(base_address + IMR, (1 << 2) | (1 << 3)); + outw(base_address + IMR, (1 << 2) | (1 << 3) | (1 << 0)); + // Set transmit and reciever enable outb(base_address + 0x37, (1 << 2) | (1 << 3)); // Configure the recieve buffer outl(base_address + 0x44, - 0xf | (1 << 7)); // (1 << 7) is the WRAP bit, 0xf is AB+AM+APM+AAP + 0xf); // 0xf is AB+AM+APM+AAP install_handler(rtl8139_handler, INT_32_INTERRUPT_GATE(0x0), 0x20 + interrupt_line); @@ -114,10 +179,6 @@ void rtl8139_init(void) { for (int i = 0; i < 4; i++) send_buffers[i] = ksbrk(0x1000); asm("sti"); - for (int i = 0; i < 10; i++) { - char buffer[512]; - rtl8139_send_data(&rtl8139, (uint8_t *)buffer, 512); - } for (;;) asm("sti"); } diff --git a/drivers/rtl8139.h b/drivers/rtl8139.h index 1f3d84e..6e13fdd 100644 --- a/drivers/rtl8139.h +++ b/drivers/rtl8139.h @@ -1 +1,4 @@ +#include <stdint.h> +void get_mac_address(uint8_t mac[6]); void rtl8139_init(void); +int rtl8139_send_data(uint8_t *data, uint16_t data_size); |