33#include <aquila/errno.h>
36#define E1000_CTRL 0x0000
37#define E1000_STATUS 0x0008
38#define E1000_EERD 0x0014
39#define E1000_ICR 0x00C0
40#define E1000_IMS 0x00D0
41#define E1000_IMC 0x00D8
42#define E1000_RCTL 0x0100
43#define E1000_TCTL 0x0400
44#define E1000_TIPG 0x0410
45#define E1000_RDBAL 0x2800
46#define E1000_RDBAH 0x2804
47#define E1000_RDLEN 0x2808
48#define E1000_RDH 0x2810
49#define E1000_RDT 0x2818
50#define E1000_TDBAL 0x3800
51#define E1000_TDBAH 0x3804
52#define E1000_TDLEN 0x3808
53#define E1000_TDH 0x3810
54#define E1000_TDT 0x3818
55#define E1000_MTA 0x5200
56#define E1000_RAL 0x5400
57#define E1000_RAH 0x5404
59#define CTRL_ASDE (1 << 5)
60#define CTRL_SLU (1 << 6)
61#define CTRL_RST (1 << 26)
63#define STATUS_LU (1 << 1)
65#define EERD_START (1 << 0)
66#define EERD_DONE (1 << 4)
68#define ICR_TXDW (1 << 0)
69#define ICR_LSC (1 << 2)
70#define ICR_RXDMT0 (1 << 4)
71#define ICR_RXO (1 << 6)
72#define ICR_RXT0 (1 << 7)
74#define RCTL_EN (1 << 1)
75#define RCTL_BAM (1 << 15)
76#define RCTL_SECRC (1 << 26)
79#define TCTL_EN (1 << 1)
80#define TCTL_PSP (1 << 3)
81#define TCTL_CT (0x0F << 4)
82#define TCTL_COLD (0x40 << 12)
84#define RAH_AV (1U << 31)
107#define DESC_DD (1 << 0)
108#define DESC_EOP (1 << 1)
110#define TXCMD_EOP (1 << 0)
111#define TXCMD_IFCS (1 << 1)
112#define TXCMD_RS (1 << 3)
166 panic(
"e1000: no memory for its rings");
178 for (
int i = 0; i < 100000; ++i) {
195 for (
int i = 0; i < 4; ++i)
196 mac[i] = ral >> (8 * i);
203 for (
int i = 0; i < 3; ++i) {
206 mac[2 * i + 1] = w >> 8;
303 asm volatile(
"" :::
"memory");
320 size_t len = desc->
length;
341 asm volatile(
"" :::
"memory");
377 for (i = 0; i < n; ++i)
385 printk(
"e1000: no registers (BAR 0)\n");
397 printk(
"e1000: no room to map its registers\n");
414 for (
int m = 0; m < 128; ++m)
431 printk(
"e1000: no interrupt\n");
static uint32_t io_in32(struct ioaddr *io, uintptr_t off)
static void io_out32(struct ioaddr *io, uintptr_t off, uint32_t val)
static void rx_poll(void)
Gives the frames the controller received to the network stack, and their descriptors back to the cont...
static uint32_t reg_read(uint32_t reg)
Reads one of the controller's registers.
static const uint16_t device_ids[]
static int e1000_probe(void)
Finds a controller of the family on the PCI bus, sets it up, and registers its network interface.
static void tx_init(void)
Sets up the transmit ring, every descriptor with its buffer and free, and turns the transmitter on.
static int e1000_transmit(struct netif *netif, struct netbuf *nb)
Sends a frame: it is copied into the next transmit buffer, and the controller is told.
static void link_check(void)
Marks the interface running or not, from whether the link is up.
static void reg_write(uint32_t reg, uint32_t val)
Writes one of the controller's registers.
static void mac_read(uint8_t mac[6])
Reads the card's MAC address: from the receive address registers if they have it, from the EEPROM oth...
static paddr_t page_get(void **virt)
Allocates a zeroed page for the rings or buffers, once, at boot.
static void e1000_irq(void *arg)
Handles the controller's interrupt: a change of the link, or frames received.
static uint16_t eeprom_read(uint8_t addr)
Reads a 16-bit word of the EEPROM, or returns 0 if the read doesn't finish in time.
static void rx_init(void)
Sets up the receive ring, every descriptor with its buffer, all of them the controller's,...
struct vm_page * mm_page_alloc(void)
Allocates a free page of physical memory.
void * memset(void *dst, int c, size_t n)
Sets n bytes to c.
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
#define MODULE_INIT(name, i, f)
struct netbuf * netbuf_alloc(void)
Allocates a netbuf, empty, with room for the headers that will be put in front of its data.
int netif_register(struct netif *netif)
Registers a network interface.
void netif_input(struct netif *netif, struct netbuf *nb)
Hands the stack a frame that a driver received, possibly from an interrupt.
int pci_irq(struct pci_dev *dev)
Finds the IRQ that a function's interrupt pin comes to.
int pci_device_scan(uint16_t vendor_id, uint16_t device_id, struct pci_dev *ref)
Finds a device by its vendor and device IDs, on bus 0.
int pci_msi_enable(struct pci_dev *dev, irq_handler_t handler, void *arg)
Has a function send its interrupts as messages (MSI) instead of on its interrupt line: one message,...
void pci_enable(struct pci_dev *dev)
Turns a function's memory and port decoding on, and bus mastering, so that it can do DMA.
void * arch_phys_map(uintptr_t paddr, size_t size)
Returns a kernel address for a range of physical memory: its address in the direct map if it's there,...
void * arch_mmio_map(uintptr_t paddr, size_t size)
Maps a device's registers into the kernel's address space, uncached (memory-mapped I/O).
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
unsigned long long int uint64_t
unsigned short int uint16_t
The controller: there is one.
volatile struct rx_desc * rx
uint8_t * rx_buf[RX_DESCS]
volatile struct tx_desc * tx
uint8_t * tx_buf[TX_DESCS]
Where a device's registers are.
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
int(*) transmit(struct netif *netif, struct netbuf *nb)
struct netif::@020302303320120142126121344055047003105154122231 stats
A base address register (BAR): where the device's registers are, how big they are,...
A PCI function: its bus, device (slot) and function numbers.
A receive descriptor: a buffer, and what the controller put in it.
A transmit descriptor: a frame to send, and whether it was sent.
A page of physical memory (pages[] has one for every page of RAM).
paddr_t paddr
its physical address