Skip to content
This repository has been archived by the owner on May 31, 2021. It is now read-only.

Commit

Permalink
Cleanup e1000 driver, support multiple instances
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed May 25, 2021
1 parent cc5ab34 commit 8e37ae1
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 324 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ EMU_ARGS += -serial mon:stdio
EMU_ARGS += -rtc base=localtime
EMU_ARGS += -soundhw pcspk,ac97
EMU_ARGS += -netdev user,id=u1,hostfwd=tcp::5555-:23 -device e1000,netdev=u1 -object filter-dump,id=f1,netdev=u1,file=qemu.pcap
EMU_ARGS += -netdev user,id=u2,hostfwd=tcp::5580-:80 -device e1000,netdev=u2
#EMU_ARGS += -hda toaruos-disk.img
EMU_KVM = -enable-kvm

Expand Down
18 changes: 16 additions & 2 deletions apps/dhcp_bitbanger.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,23 @@ int main(int argc, char * argv[]) {

/* Let's make a socket. */
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) { perror("socket"); return 1; }
//if (sockfd < 0) { perror("socket"); return 1; }

int netdev = open("/dev/eth0", O_RDWR);
/* Bind the socket to the requested device. */
//if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,

char * if_name = "enp0s4";
char if_path[100];

if (argc > 1) {
if_name = argv[1];
}

snprintf(if_path, 100, "/dev/net/%s", if_name);

int netdev = open(if_path, O_RDWR);

fprintf(stderr, "Configuring %s\n", if_name);

uint8_t mac_addr[6];
if (ioctl(netdev, 0x12340001, &mac_addr)) {
Expand Down
11 changes: 11 additions & 0 deletions apps/ifconfig.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @file apps/ifconfig.c
* @brief Network interface configuration tool.
*
* Manipulates and enumerates network interfaces.
*/
#include <stdio.h>

int main(int argc, char * argv[]) {
/* I have no idea how this works on Linux or BSD, but based on some manpages... */
}
8 changes: 5 additions & 3 deletions apps/lspci.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ struct {
{0x15ad, 0x0405, "SVGA II Adapter"},
{0x15ad, 0x0790, "PCI bridge"},
{0x15ad, 0x07a0, "PCI Express Root Port"},
{0x8086, 0x100e, "Gigabit Ethernet Controller (e1000)"},
{0x8086, 0x100f, "Gigabit Ethernet Controller (e1000)"},
{0x8086, 0x1004, "82543GC Gigabit Ethernet Controller (e1000)"},
{0x8086, 0x100e, "82540EM Gigabit Ethernet Controller (e1000)"},
{0x8086, 0x100f, "82545EM Gigabit Ethernet Controller (e1000)"},
{0x8086, 0x10ea, "82577LM Gigabit Ethernet Controller (e1000)"},
{0x8086, 0x1237, "PCI & Memory"},
{0x8086, 0x2415, "AC'97 Audio Chipset"},
{0x8086, 0x2415, "82801AA AC'97 Audio Controller"},
{0x8086, 0x7000, "PCI-to-ISA Bridge"},
{0x8086, 0x7010, "IDE Interface"},
{0x8086, 0x7110, "PIIX4 ISA"},
Expand Down
102 changes: 102 additions & 0 deletions base/usr/include/kernel/net/e1000.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#pragma once

#include <stdint.h>

#define E1000_REG_CTRL 0x0000
#define E1000_REG_STATUS 0x0008
#define E1000_REG_EEPROM 0x0014
#define E1000_REG_CTRL_EXT 0x0018
#define E1000_REG_ICR 0x00C0

#define E1000_REG_RCTRL 0x0100
#define E1000_REG_RXDESCLO 0x2800
#define E1000_REG_RXDESCHI 0x2804
#define E1000_REG_RXDESCLEN 0x2808
#define E1000_REG_RXDESCHEAD 0x2810
#define E1000_REG_RXDESCTAIL 0x2818

#define E1000_REG_TCTRL 0x0400
#define E1000_REG_TXDESCLO 0x3800
#define E1000_REG_TXDESCHI 0x3804
#define E1000_REG_TXDESCLEN 0x3808
#define E1000_REG_TXDESCHEAD 0x3810
#define E1000_REG_TXDESCTAIL 0x3818

#define E1000_REG_RXADDR 0x5400

#define E1000_NUM_RX_DESC 32
#define E1000_NUM_TX_DESC 8

#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 RCTL_RDMTS_HALF (0 << 8) /* Free Buffer Threshold is 1/2 of RDLEN */
#define RCTL_RDMTS_QUARTER (1 << 8) /* Free Buffer Threshold is 1/4 of RDLEN */
#define RCTL_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 */

#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))

#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 */

#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 */

#define ICR_TXDW (1 << 0)
#define ICR_TXQE (1 << 1) /* Transmit queue is empty */
#define ICR_LSC (1 << 2) /* Link status changed */
#define ICR_RXSEQ (1 << 3) /* Receive sequence count error */
#define ICR_RXDMT0 (1 << 4) /* Receive descriptor minimum threshold */
/* what's 5 (0x20)? */
#define ICR_RXO (1 << 6) /* Receive overrun */
#define ICR_RXT0 (1 << 7) /* Receive timer interrupt? */

struct e1000_rx_desc {
volatile uint64_t addr;
volatile uint16_t length;
volatile uint16_t checksum;
volatile uint8_t status;
volatile uint8_t errors;
volatile uint16_t special;
} __attribute__((packed)); /* this looks like it should pack fine as-is */

struct e1000_tx_desc {
volatile uint64_t addr;
volatile uint16_t length;
volatile uint8_t cso;
volatile uint8_t cmd;
volatile uint8_t status;
volatile uint8_t css;
volatile uint16_t special;
} __attribute__((packed));

2 changes: 2 additions & 0 deletions kernel/generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern void shm_install(void);
extern void random_initialize(void);
extern int system(const char * path, int argc, const char ** argv, const char ** envin);
extern void snd_install(void);
extern void net_install(void);

void generic_startup(void) {
args_parse(arch_get_cmdline());
Expand All @@ -40,6 +41,7 @@ void generic_startup(void) {
procfs_initialize();
random_initialize();
snd_install();
net_install();
tasking_start();
}

Expand Down
Loading

0 comments on commit 8e37ae1

Please sign in to comment.