AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
e1000.c File Reference

The driver for Intel's 8254x (e1000) and 82574L Ethernet controllers. More...

#include <core/system.h>
#include <core/arch.h>
#include <core/module.h>
#include <core/printk.h>
#include <core/string.h>
#include <cpu/io.h>
#include <dev/pci.h>
#include <mm/mm.h>
#include <net/netif.h>
#include <dev/irq.h>
#include <aquila/errno.h>

Go to the source code of this file.

Data Structures

struct  rx_desc
 A receive descriptor: a buffer, and what the controller put in it. More...
struct  tx_desc
 A transmit descriptor: a frame to send, and whether it was sent. More...
struct  e1000
 The controller: there is one. More...

Macros

#define E1000_CTRL   0x0000
#define E1000_STATUS   0x0008
#define E1000_EERD   0x0014
#define E1000_ICR   0x00C0
#define E1000_IMS   0x00D0
#define E1000_IMC   0x00D8
#define E1000_RCTL   0x0100
#define E1000_TCTL   0x0400
#define E1000_TIPG   0x0410
#define E1000_RDBAL   0x2800
#define E1000_RDBAH   0x2804
#define E1000_RDLEN   0x2808
#define E1000_RDH   0x2810
#define E1000_RDT   0x2818
#define E1000_TDBAL   0x3800
#define E1000_TDBAH   0x3804
#define E1000_TDLEN   0x3808
#define E1000_TDH   0x3810
#define E1000_TDT   0x3818
#define E1000_MTA   0x5200
#define E1000_RAL   0x5400
#define E1000_RAH   0x5404
#define CTRL_ASDE   (1 << 5)
#define CTRL_SLU   (1 << 6)
#define CTRL_RST   (1 << 26)
#define STATUS_LU   (1 << 1)
#define EERD_START   (1 << 0)
#define EERD_DONE   (1 << 4)
#define ICR_TXDW   (1 << 0)
#define ICR_LSC   (1 << 2)
#define ICR_RXDMT0   (1 << 4)
#define ICR_RXO   (1 << 6)
#define ICR_RXT0   (1 << 7)
#define RCTL_EN   (1 << 1)
#define RCTL_BAM   (1 << 15)
#define RCTL_SECRC   (1 << 26)
#define TCTL_EN   (1 << 1)
#define TCTL_PSP   (1 << 3)
#define TCTL_CT   (0x0F << 4)
#define TCTL_COLD   (0x40 << 12)
#define RAH_AV   (1U << 31)
#define DESC_DD   (1 << 0)
#define DESC_EOP   (1 << 1)
#define TXCMD_EOP   (1 << 0)
#define TXCMD_IFCS   (1 << 1)
#define TXCMD_RS   (1 << 3)
#define RX_DESCS   32
#define TX_DESCS   32
#define BUF_SIZE   2048

Functions

static uint32_t reg_read (uint32_t reg)
 Reads one of the controller's registers.
static void reg_write (uint32_t reg, uint32_t val)
 Writes one of the controller's registers.
static paddr_t page_get (void **virt)
 Allocates a zeroed page for the rings or buffers, once, at boot.
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 mac_read (uint8_t mac[6])
 Reads the card's MAC address: from the receive address registers if they have it, from the EEPROM otherwise (and then tells the receiver).
static void rx_init (void)
 Sets up the receive ring, every descriptor with its buffer, all of them the controller's, and turns the receiver on.
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 rx_poll (void)
 Gives the frames the controller received to the network stack, and their descriptors back to the controller.
static void link_check (void)
 Marks the interface running or not, from whether the link is up.
static void e1000_irq (void *arg)
 Handles the controller's interrupt: a change of the link, or frames received.
static int e1000_probe (void)
 Finds a controller of the family on the PCI bus, sets it up, and registers its network interface.

Variables

static struct e1000 e1000
static const uint16_t device_ids []

Detailed Description

The driver for Intel's 8254x (e1000) and 82574L Ethernet controllers.

The 82540EM is QEMU's default network card on x86. The controller receives frames into the buffers of a ring of descriptors, which it fills and interrupts for, and sends frames from a second ring. Both rings and their buffers are pages of memory, which the driver reaches through the direct map and whose physical addresses it gives the controller. A received frame is copied into a netbuf and given to the network stack; a frame to send is copied into a transmit buffer.

The 82574L (QEMU's e1000e) is driven the same way, with its legacy descriptors; it interrupts with MSI when there is an APIC to take it.

References: [1] - PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer's Manual (8254x), Intel, 317453 [2] - 82574 GbE Controller Family Datasheet, Intel, 317694

Definition in file e1000.c.

Macro Definition Documentation

◆ BUF_SIZE

#define BUF_SIZE   2048

Definition at line 116 of file e1000.c.

◆ CTRL_ASDE

#define CTRL_ASDE   (1 << 5)

Definition at line 59 of file e1000.c.

Referenced by e1000_probe().

◆ CTRL_RST

#define CTRL_RST   (1 << 26)

Definition at line 61 of file e1000.c.

Referenced by e1000_probe().

◆ CTRL_SLU

#define CTRL_SLU   (1 << 6)

Definition at line 60 of file e1000.c.

Referenced by e1000_probe().

◆ DESC_DD

#define DESC_DD   (1 << 0)

Definition at line 107 of file e1000.c.

Referenced by e1000_transmit(), rx_poll(), and tx_init().

◆ DESC_EOP

#define DESC_EOP   (1 << 1)

Definition at line 108 of file e1000.c.

Referenced by rx_poll().

◆ E1000_CTRL

#define E1000_CTRL   0x0000

Definition at line 36 of file e1000.c.

Referenced by e1000_probe().

◆ E1000_EERD

#define E1000_EERD   0x0014

Definition at line 38 of file e1000.c.

Referenced by eeprom_read().

◆ E1000_ICR

#define E1000_ICR   0x00C0

Definition at line 39 of file e1000.c.

Referenced by e1000_irq(), and e1000_probe().

◆ E1000_IMC

#define E1000_IMC   0x00D8

Definition at line 41 of file e1000.c.

Referenced by e1000_probe().

◆ E1000_IMS

#define E1000_IMS   0x00D0

Definition at line 40 of file e1000.c.

Referenced by e1000_probe().

◆ E1000_MTA

#define E1000_MTA   0x5200

Definition at line 55 of file e1000.c.

Referenced by e1000_probe().

◆ E1000_RAH

#define E1000_RAH   0x5404

Definition at line 57 of file e1000.c.

Referenced by mac_read().

◆ E1000_RAL

#define E1000_RAL   0x5400

Definition at line 56 of file e1000.c.

Referenced by mac_read().

◆ E1000_RCTL

#define E1000_RCTL   0x0100

Definition at line 42 of file e1000.c.

Referenced by rx_init().

◆ E1000_RDBAH

#define E1000_RDBAH   0x2804

Definition at line 46 of file e1000.c.

Referenced by rx_init().

◆ E1000_RDBAL

#define E1000_RDBAL   0x2800

Definition at line 45 of file e1000.c.

Referenced by rx_init().

◆ E1000_RDH

#define E1000_RDH   0x2810

Definition at line 48 of file e1000.c.

Referenced by rx_init().

◆ E1000_RDLEN

#define E1000_RDLEN   0x2808

Definition at line 47 of file e1000.c.

Referenced by rx_init().

◆ E1000_RDT

#define E1000_RDT   0x2818

Definition at line 49 of file e1000.c.

Referenced by rx_init(), and rx_poll().

◆ E1000_STATUS

#define E1000_STATUS   0x0008

Definition at line 37 of file e1000.c.

Referenced by link_check().

◆ E1000_TCTL

#define E1000_TCTL   0x0400

Definition at line 43 of file e1000.c.

Referenced by tx_init().

◆ E1000_TDBAH

#define E1000_TDBAH   0x3804

Definition at line 51 of file e1000.c.

Referenced by tx_init().

◆ E1000_TDBAL

#define E1000_TDBAL   0x3800

Definition at line 50 of file e1000.c.

Referenced by tx_init().

◆ E1000_TDH

#define E1000_TDH   0x3810

Definition at line 53 of file e1000.c.

Referenced by tx_init().

◆ E1000_TDLEN

#define E1000_TDLEN   0x3808

Definition at line 52 of file e1000.c.

Referenced by tx_init().

◆ E1000_TDT

#define E1000_TDT   0x3818

Definition at line 54 of file e1000.c.

Referenced by e1000_transmit(), and tx_init().

◆ E1000_TIPG

#define E1000_TIPG   0x0410

Definition at line 44 of file e1000.c.

Referenced by tx_init().

◆ EERD_DONE

#define EERD_DONE   (1 << 4)

Definition at line 66 of file e1000.c.

Referenced by eeprom_read().

◆ EERD_START

#define EERD_START   (1 << 0)

Definition at line 65 of file e1000.c.

Referenced by eeprom_read().

◆ ICR_LSC

#define ICR_LSC   (1 << 2)

Definition at line 69 of file e1000.c.

Referenced by e1000_irq(), and e1000_probe().

◆ ICR_RXDMT0

#define ICR_RXDMT0   (1 << 4)

Definition at line 70 of file e1000.c.

Referenced by e1000_irq(), and e1000_probe().

◆ ICR_RXO

#define ICR_RXO   (1 << 6)

Definition at line 71 of file e1000.c.

Referenced by e1000_irq(), and e1000_probe().

◆ ICR_RXT0

#define ICR_RXT0   (1 << 7)

Definition at line 72 of file e1000.c.

Referenced by e1000_irq(), and e1000_probe().

◆ ICR_TXDW

#define ICR_TXDW   (1 << 0)

Definition at line 68 of file e1000.c.

◆ RAH_AV

#define RAH_AV   (1U << 31)

Definition at line 84 of file e1000.c.

Referenced by mac_read().

◆ RCTL_BAM

#define RCTL_BAM   (1 << 15)

Definition at line 75 of file e1000.c.

Referenced by rx_init().

◆ RCTL_EN

#define RCTL_EN   (1 << 1)

Definition at line 74 of file e1000.c.

Referenced by rx_init().

◆ RCTL_SECRC

#define RCTL_SECRC   (1 << 26)

Definition at line 76 of file e1000.c.

Referenced by rx_init().

◆ RX_DESCS

#define RX_DESCS   32

Definition at line 114 of file e1000.c.

Referenced by rx_init(), and rx_poll().

◆ STATUS_LU

#define STATUS_LU   (1 << 1)

Definition at line 63 of file e1000.c.

Referenced by link_check().

◆ TCTL_COLD

#define TCTL_COLD   (0x40 << 12)

Definition at line 82 of file e1000.c.

Referenced by tx_init().

◆ TCTL_CT

#define TCTL_CT   (0x0F << 4)

Definition at line 81 of file e1000.c.

Referenced by tx_init().

◆ TCTL_EN

#define TCTL_EN   (1 << 1)

Definition at line 79 of file e1000.c.

Referenced by tx_init().

◆ TCTL_PSP

#define TCTL_PSP   (1 << 3)

Definition at line 80 of file e1000.c.

Referenced by tx_init().

◆ TX_DESCS

#define TX_DESCS   32

Definition at line 115 of file e1000.c.

Referenced by e1000_transmit(), and tx_init().

◆ TXCMD_EOP

#define TXCMD_EOP   (1 << 0)

Definition at line 110 of file e1000.c.

Referenced by e1000_transmit().

◆ TXCMD_IFCS

#define TXCMD_IFCS   (1 << 1)

Definition at line 111 of file e1000.c.

Referenced by e1000_transmit().

◆ TXCMD_RS

#define TXCMD_RS   (1 << 3)

Definition at line 112 of file e1000.c.

Referenced by e1000_transmit().

Function Documentation

◆ e1000_irq()

void e1000_irq ( void * arg)
static

Handles the controller's interrupt: a change of the link, or frames received.

Definition at line 357 of file e1000.c.

References E1000_ICR, e1000_irq(), ICR_LSC, ICR_RXDMT0, ICR_RXO, ICR_RXT0, link_check(), reg_read(), and rx_poll().

Referenced by e1000_irq(), and e1000_probe().

◆ e1000_probe()

◆ e1000_transmit()

int e1000_transmit ( struct netif * netif,
struct netbuf * nb )
static

Sends a frame: it is copied into the next transmit buffer, and the controller is told.

Returns 0, -EMSGSIZE for a frame too big, or -ENOBUFS if the ring is full (the frame is dropped).

Definition at line 279 of file e1000.c.

References BUF_SIZE, tx_desc::cmd, netbuf::data, DESC_DD, E1000_TDT, e1000_transmit(), netbuf::len, tx_desc::length, memcpy(), reg_write(), netif::stats, tx_desc::status, e1000::tx, e1000::tx_buf, TX_DESCS, e1000::tx_next, TXCMD_EOP, TXCMD_IFCS, and TXCMD_RS.

Referenced by e1000_probe(), and e1000_transmit().

◆ eeprom_read()

uint16_t eeprom_read ( uint8_t addr)
static

Reads a 16-bit word of the EEPROM, or returns 0 if the read doesn't finish in time.

Definition at line 174 of file e1000.c.

References E1000_EERD, eeprom_read(), EERD_DONE, EERD_START, reg_read(), and reg_write().

Referenced by eeprom_read(), and mac_read().

◆ link_check()

void link_check ( void )
static

Marks the interface running or not, from whether the link is up.

Definition at line 347 of file e1000.c.

References E1000_STATUS, netif::flags, link_check(), e1000::netif, NETIF_RUNNING, reg_read(), and STATUS_LU.

Referenced by e1000_irq(), e1000_probe(), and link_check().

◆ mac_read()

void mac_read ( uint8_t mac[6])
static

Reads the card's MAC address: from the receive address registers if they have it, from the EEPROM otherwise (and then tells the receiver).

Definition at line 190 of file e1000.c.

References E1000_RAH, E1000_RAL, eeprom_read(), mac_read(), RAH_AV, reg_read(), and reg_write().

Referenced by e1000_probe(), and mac_read().

◆ page_get()

paddr_t page_get ( void ** virt)
static

Allocates a zeroed page for the rings or buffers, once, at boot.

Returns its physical address, and sets *virt to its kernel address. It panics if there is no memory: the kernel can't boot without that little.

Definition at line 161 of file e1000.c.

References arch_phys_map(), memset(), mm_page_alloc(), vm_page::paddr, page_get(), PAGE_SIZE, and panic.

Referenced by page_get(), rx_init(), and tx_init().

◆ reg_read()

uint32_t reg_read ( uint32_t reg)
inlinestatic

Reads one of the controller's registers.

Definition at line 146 of file e1000.c.

References io_in32(), reg_read(), and e1000::regs.

Referenced by e1000_irq(), e1000_probe(), eeprom_read(), link_check(), mac_read(), and reg_read().

◆ reg_write()

void reg_write ( uint32_t reg,
uint32_t val )
inlinestatic

Writes one of the controller's registers.

Definition at line 152 of file e1000.c.

References io_out32(), reg_write(), and e1000::regs.

Referenced by e1000_probe(), e1000_transmit(), eeprom_read(), mac_read(), reg_write(), rx_init(), rx_poll(), and tx_init().

◆ rx_init()

void rx_init ( void )
static

Sets up the receive ring, every descriptor with its buffer, all of them the controller's, and turns the receiver on.

Definition at line 216 of file e1000.c.

References rx_desc::addr, BUF_SIZE, E1000_RCTL, E1000_RDBAH, E1000_RDBAL, E1000_RDH, E1000_RDLEN, E1000_RDT, vm_page::paddr, page_get(), PAGE_SIZE, RCTL_BAM, RCTL_EN, RCTL_SECRC, reg_write(), e1000::rx, e1000::rx_buf, RX_DESCS, rx_init(), and e1000::rx_next.

Referenced by e1000_probe(), and rx_init().

◆ rx_poll()

void rx_poll ( void )
static

Gives the frames the controller received to the network stack, and their descriptors back to the controller.

Definition at line 311 of file e1000.c.

References DESC_DD, DESC_EOP, E1000_RDT, rx_desc::errors, netbuf::len, rx_desc::length, memcpy(), netbuf_alloc(), NETBUF_SIZE, e1000::netif, netif_input(), reg_write(), e1000::rx, e1000::rx_buf, RX_DESCS, e1000::rx_next, rx_poll(), netif::stats, and rx_desc::status.

Referenced by e1000_irq(), and rx_poll().

◆ tx_init()

void tx_init ( void )
static

Sets up the transmit ring, every descriptor with its buffer and free, and turns the transmitter on.

Definition at line 246 of file e1000.c.

References tx_desc::addr, BUF_SIZE, DESC_DD, E1000_TCTL, E1000_TDBAH, E1000_TDBAL, E1000_TDH, E1000_TDLEN, E1000_TDT, E1000_TIPG, vm_page::paddr, page_get(), PAGE_SIZE, reg_write(), tx_desc::status, TCTL_COLD, TCTL_CT, TCTL_EN, TCTL_PSP, e1000::tx, e1000::tx_buf, TX_DESCS, tx_init(), and e1000::tx_next.

Referenced by e1000_probe(), and tx_init().

Variable Documentation

◆ device_ids

const uint16_t device_ids[]
static
Initial value:
= {
0x100E,
0x10D3,
0x100F,
0x1004,
0x1015,
0x1026,
0x1076,
0x107C,
}

Definition at line 134 of file e1000.c.

Referenced by e1000_probe().

◆ e1000

struct e1000 e1000
static