AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
e1000.c
Go to the documentation of this file.
1
20
21#include <core/system.h>
22#include <core/arch.h>
23#include <core/module.h>
24#include <core/printk.h>
25#include <core/string.h>
26
27#include <cpu/io.h>
28#include <dev/pci.h>
29#include <mm/mm.h>
30#include <net/netif.h>
31#include <dev/irq.h>
32
33#include <aquila/errno.h>
34
35/* the controller's registers, from its BAR 0 */
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 /* the multicast table: 128 registers */
56#define E1000_RAL 0x5400
57#define E1000_RAH 0x5404
58
59#define CTRL_ASDE (1 << 5) /* auto-speed detection */
60#define CTRL_SLU (1 << 6) /* set link up */
61#define CTRL_RST (1 << 26)
62
63#define STATUS_LU (1 << 1) /* link up */
64
65#define EERD_START (1 << 0)
66#define EERD_DONE (1 << 4)
67
68#define ICR_TXDW (1 << 0)
69#define ICR_LSC (1 << 2) /* link status change */
70#define ICR_RXDMT0 (1 << 4) /* receive descriptors running low */
71#define ICR_RXO (1 << 6) /* receiver overrun */
72#define ICR_RXT0 (1 << 7) /* receive timer: frames came */
73
74#define RCTL_EN (1 << 1)
75#define RCTL_BAM (1 << 15) /* accept broadcasts */
76#define RCTL_SECRC (1 << 26) /* strip the CRC */
77 /* BSIZE is 0: buffers of 2048 bytes */
78
79#define TCTL_EN (1 << 1)
80#define TCTL_PSP (1 << 3) /* pad short packets */
81#define TCTL_CT (0x0F << 4) /* collision threshold */
82#define TCTL_COLD (0x40 << 12)/* collision distance, full duplex */
83
84#define RAH_AV (1U << 31) /* the address is valid */
85
95
106
107#define DESC_DD (1 << 0) /* descriptor done */
108#define DESC_EOP (1 << 1) /* end of packet (rx status) */
109
110#define TXCMD_EOP (1 << 0)
111#define TXCMD_IFCS (1 << 1) /* insert the CRC */
112#define TXCMD_RS (1 << 3) /* report status (DD) */
113
114#define RX_DESCS 32
115#define TX_DESCS 32
116#define BUF_SIZE 2048
117
119static struct e1000 {
120 struct pci_dev pci;
121 struct ioaddr regs;
122 int irq;
123
124 volatile struct rx_desc *rx;
125 volatile struct tx_desc *tx;
127 unsigned rx_next; /* the next descriptor the controller fills */
128 unsigned tx_next; /* the next descriptor to send a frame with */
129
130 struct netif netif;
132
133/* the PCI devices (8086:xxxx) of the family */
134static const uint16_t device_ids[] = {
135 0x100E, /* 82540EM (QEMU's e1000) */
136 0x10D3, /* 82574L (QEMU's e1000e): MSI */
137 0x100F, /* 82545EM copper */
138 0x1004, /* 82543GC copper */
139 0x1015, /* 82540EM LOM */
140 0x1026, /* 82545GM copper */
141 0x1076, /* 82541GI */
142 0x107C, /* 82541PI */
143};
144
146static inline uint32_t reg_read(uint32_t reg)
147{
148 return io_in32(&e1000.regs, reg);
149}
150
152static inline void reg_write(uint32_t reg, uint32_t val)
153{
154 io_out32(&e1000.regs, reg, val);
155}
156
161static paddr_t page_get(void **virt)
162{
163 struct vm_page *vm_page = mm_page_alloc();
164
165 if (!vm_page || !(*virt = arch_phys_map(vm_page->paddr, PAGE_SIZE)))
166 panic("e1000: no memory for its rings");
167
168 memset(*virt, 0, PAGE_SIZE);
169 return vm_page->paddr;
170}
171
175{
176 reg_write(E1000_EERD, EERD_START | (uint32_t) addr << 8);
177
178 for (int i = 0; i < 100000; ++i) {
180
181 if (v & EERD_DONE)
182 return v >> 16;
183 }
184
185 return 0;
186}
187
190static void mac_read(uint8_t mac[6])
191{
193
194 if (rah & RAH_AV) {
195 for (int i = 0; i < 4; ++i)
196 mac[i] = ral >> (8 * i);
197
198 mac[4] = rah;
199 mac[5] = rah >> 8;
200 return;
201 }
202
203 for (int i = 0; i < 3; ++i) {
204 uint16_t w = eeprom_read(i);
205 mac[2 * i] = w;
206 mac[2 * i + 1] = w >> 8;
207 }
208
209 /* the address the receiver accepts frames for */
210 reg_write(E1000_RAL, mac[0] | mac[1] << 8 | mac[2] << 16 | (uint32_t) mac[3] << 24);
211 reg_write(E1000_RAH, mac[4] | mac[5] << 8 | RAH_AV);
212}
213
216static void rx_init(void)
217{
218 void *ring;
219 paddr_t ring_paddr = page_get(&ring);
220
221 e1000.rx = ring;
222
223 for (int i = 0; i < RX_DESCS; i += PAGE_SIZE / BUF_SIZE) {
224 void *page;
225 paddr_t paddr = page_get(&page);
226
227 for (int j = 0; j < PAGE_SIZE / BUF_SIZE; ++j) {
228 e1000.rx_buf[i + j] = (uint8_t *) page + j * BUF_SIZE;
229 e1000.rx[i + j].addr = paddr + j * BUF_SIZE;
230 }
231 }
232
233 reg_write(E1000_RDBAL, (uint32_t) ring_paddr);
234 reg_write(E1000_RDBAH, (uint32_t) ((uint64_t) ring_paddr >> 32));
235 reg_write(E1000_RDLEN, RX_DESCS * sizeof(struct rx_desc));
237 reg_write(E1000_RDT, RX_DESCS - 1); /* every descriptor is the controller's */
238
239 e1000.rx_next = 0;
240
242}
243
246static void tx_init(void)
247{
248 void *ring;
249 paddr_t ring_paddr = page_get(&ring);
250
251 e1000.tx = ring;
252
253 for (int i = 0; i < TX_DESCS; i += PAGE_SIZE / BUF_SIZE) {
254 void *page;
255 paddr_t paddr = page_get(&page);
256
257 for (int j = 0; j < PAGE_SIZE / BUF_SIZE; ++j) {
258 e1000.tx_buf[i + j] = (uint8_t *) page + j * BUF_SIZE;
259 e1000.tx[i + j].addr = paddr + j * BUF_SIZE;
260 e1000.tx[i + j].status = DESC_DD; /* free */
261 }
262 }
263
264 reg_write(E1000_TDBAL, (uint32_t) ring_paddr);
265 reg_write(E1000_TDBAH, (uint32_t) ((uint64_t) ring_paddr >> 32));
266 reg_write(E1000_TDLEN, TX_DESCS * sizeof(struct tx_desc));
269
270 e1000.tx_next = 0;
271
273 reg_write(E1000_TIPG, 10 | 8 << 10 | 6 << 20);
274}
275
279static int e1000_transmit(struct netif *netif, struct netbuf *nb)
280{
281 unsigned i = e1000.tx_next;
282 volatile struct tx_desc *desc = &e1000.tx[i];
283
284 if (nb->len > BUF_SIZE)
285 return -EMSGSIZE;
286
287 /* the ring is full: the controller hasn't sent this descriptor's
288 * frame yet */
289 if (!(desc->status & DESC_DD)) {
290 netif->stats.tx_dropped++;
291 return -ENOBUFS;
292 }
293
294 memcpy(e1000.tx_buf[i], nb->data, nb->len);
295
296 desc->length = nb->len;
297 desc->cmd = TXCMD_EOP | TXCMD_IFCS | TXCMD_RS;
298 desc->status = 0;
299
300 e1000.tx_next = (i + 1) % TX_DESCS;
301
302 /* the descriptor must be written before the controller is told */
303 asm volatile("" ::: "memory");
305
306 return 0;
307}
308
311static void rx_poll(void)
312{
313 for (;;) {
314 unsigned i = e1000.rx_next;
315 volatile struct rx_desc *desc = &e1000.rx[i];
316
317 if (!(desc->status & DESC_DD))
318 break;
319
320 size_t len = desc->length;
321
322 /* a frame that spans buffers (longer than 2048 bytes) isn't
323 * taken */
324 if ((desc->status & DESC_EOP) && !desc->errors && len <= NETBUF_SIZE - 256) {
325 struct netbuf *nb = netbuf_alloc();
326
327 if (nb) {
328 memcpy(netbuf_put(nb, len), e1000.rx_buf[i], len);
329 netif_input(&e1000.netif, nb);
330 } else {
331 e1000.netif.stats.rx_dropped++;
332 }
333 } else {
334 e1000.netif.stats.rx_errors++;
335 }
336
337 /* the descriptor is the controller's again */
338 desc->status = 0;
339 e1000.rx_next = (i + 1) % RX_DESCS;
340
341 asm volatile("" ::: "memory");
343 }
344}
345
347static void link_check(void)
348{
351 else
353}
354
357static void e1000_irq(void *arg __unused)
358{
359 /* reading the cause register acknowledges the interrupts */
361
362 if (icr & ICR_LSC)
363 link_check();
364
365 if (icr & (ICR_RXT0 | ICR_RXDMT0 | ICR_RXO))
366 rx_poll();
367}
368
371static int e1000_probe(void)
372{
373 struct pci_bar bar;
374 size_t n = sizeof(device_ids) / sizeof(device_ids[0]);
375 size_t i;
376
377 for (i = 0; i < n; ++i)
378 if (!pci_device_scan(0x8086, device_ids[i], &e1000.pci))
379 break;
380
381 if (i == n)
382 return 0;
383
384 if (pci_bar(&e1000.pci, 0, &bar) || bar.io) {
385 printk("e1000: no registers (BAR 0)\n");
386 return -ENODEV;
387 }
388
390
391 /* the registers are at byte offsets (MMIO16 and MMIO32 would space
392 * them out), read and written 32 bits at a time */
395
396 if (!e1000.regs.addr) {
397 printk("e1000: no room to map its registers\n");
398 return -ENOMEM;
399 }
400
401 /* reset the controller, with no interrupts until all is set up */
402 reg_write(E1000_IMC, 0xFFFFFFFF);
404
405 for (int t = 0; t < 100000 && (reg_read(E1000_CTRL) & CTRL_RST); ++t)
406 ;
407
408 reg_write(E1000_IMC, 0xFFFFFFFF);
410
412
413 /* no multicast addresses */
414 for (int m = 0; m < 128; ++m)
415 reg_write(E1000_MTA + 4 * m, 0);
416
417 rx_init();
418 tx_init();
419
422 link_check();
423
425
426 /* its interrupts as messages (MSI) if it can, otherwise on its line */
427 int irq = pci_msi_enable(&e1000.pci, e1000_irq, NULL);
428 int msi = irq >= 0;
429
430 if (!msi && ((irq = pci_irq(&e1000.pci)) < 0 || irq_install(irq, e1000_irq, NULL)))
431 printk("e1000: no interrupt\n");
432
433 e1000.irq = irq;
434
435 printk("e1000: %x:%x at %p, irq %d%s, link %s\n", 0x8086, device_ids[i],
436 (void *) (uintptr_t) bar.addr, e1000.irq, msi ? " (msi)" : "",
437 e1000.netif.flags & NETIF_RUNNING ? "up" : "down");
438
441
442 return 0;
443}
444
int irq_install(unsigned irq, irq_handler_t fn, void *arg)
Installs a handler for an IRQ.
Definition irq.c:32
#define PAGE_SIZE
Definition system.h:7
static uint32_t io_in32(struct ioaddr *io, uintptr_t off)
Definition io.h:38
static void io_out32(struct ioaddr *io, uintptr_t off, uint32_t val)
Definition io.h:54
uint64_t paddr_t
Definition mm.h:39
#define panic(s)
Definition panic.h:15
#define IOADDR_MMIO8
Definition io.h:90
#define E1000_IMS
Definition e1000.c:40
#define TXCMD_IFCS
Definition e1000.c:111
static void rx_poll(void)
Gives the frames the controller received to the network stack, and their descriptors back to the cont...
Definition e1000.c:311
static uint32_t reg_read(uint32_t reg)
Reads one of the controller's registers.
Definition e1000.c:146
#define RCTL_BAM
Definition e1000.c:75
#define DESC_EOP
Definition e1000.c:108
#define CTRL_SLU
Definition e1000.c:60
static const uint16_t device_ids[]
Definition e1000.c:134
#define TX_DESCS
Definition e1000.c:115
#define RAH_AV
Definition e1000.c:84
#define DESC_DD
Definition e1000.c:107
#define E1000_RDBAH
Definition e1000.c:46
static int e1000_probe(void)
Finds a controller of the family on the PCI bus, sets it up, and registers its network interface.
Definition e1000.c:371
#define ICR_LSC
Definition e1000.c:69
static void tx_init(void)
Sets up the transmit ring, every descriptor with its buffer and free, and turns the transmitter on.
Definition e1000.c:246
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.
Definition e1000.c:279
static void link_check(void)
Marks the interface running or not, from whether the link is up.
Definition e1000.c:347
#define ICR_RXO
Definition e1000.c:71
static void reg_write(uint32_t reg, uint32_t val)
Writes one of the controller's registers.
Definition e1000.c:152
#define E1000_RDLEN
Definition e1000.c:47
#define E1000_RAL
Definition e1000.c:56
#define TCTL_PSP
Definition e1000.c:80
#define RCTL_EN
Definition e1000.c:74
#define E1000_RCTL
Definition e1000.c:42
#define E1000_TDH
Definition e1000.c:53
#define E1000_EERD
Definition e1000.c:38
#define E1000_TCTL
Definition e1000.c:43
#define RCTL_SECRC
Definition e1000.c:76
#define E1000_RDH
Definition e1000.c:48
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...
Definition e1000.c:190
#define E1000_STATUS
Definition e1000.c:37
static paddr_t page_get(void **virt)
Allocates a zeroed page for the rings or buffers, once, at boot.
Definition e1000.c:161
#define TXCMD_EOP
Definition e1000.c:110
#define E1000_TDBAL
Definition e1000.c:50
#define CTRL_ASDE
Definition e1000.c:59
#define E1000_RDBAL
Definition e1000.c:45
#define E1000_CTRL
Definition e1000.c:36
#define E1000_TIPG
Definition e1000.c:44
#define TCTL_COLD
Definition e1000.c:82
#define TCTL_EN
Definition e1000.c:79
#define E1000_IMC
Definition e1000.c:41
#define EERD_START
Definition e1000.c:65
#define CTRL_RST
Definition e1000.c:61
static void e1000_irq(void *arg)
Handles the controller's interrupt: a change of the link, or frames received.
Definition e1000.c:357
#define TXCMD_RS
Definition e1000.c:112
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.
Definition e1000.c:174
#define EERD_DONE
Definition e1000.c:66
#define E1000_TDBAH
Definition e1000.c:51
#define ICR_RXT0
Definition e1000.c:72
#define E1000_RDT
Definition e1000.c:49
#define E1000_RAH
Definition e1000.c:57
#define E1000_MTA
Definition e1000.c:55
#define E1000_ICR
Definition e1000.c:39
#define TCTL_CT
Definition e1000.c:81
#define STATUS_LU
Definition e1000.c:63
static void rx_init(void)
Sets up the receive ring, every descriptor with its buffer, all of them the controller's,...
Definition e1000.c:216
#define E1000_TDT
Definition e1000.c:54
#define E1000_TDLEN
Definition e1000.c:52
#define ICR_RXDMT0
Definition e1000.c:70
#define RX_DESCS
Definition e1000.c:114
#define __packed
Definition system.h:30
struct vm_page * mm_page_alloc(void)
Allocates a free page of physical memory.
Definition mm.c:78
void * memset(void *dst, int c, size_t n)
Sets n bytes to c.
Definition libc.c:23
void * memcpy(void *dst, const void *src, size_t n)
Copies n bytes; dst and src must not overlap.
Definition libc.c:11
#define MODULE_INIT(name, i, f)
Definition module.h:10
#define NETBUF_SIZE
Definition netbuf.h:40
struct netbuf * netbuf_alloc(void)
Allocates a netbuf, empty, with room for the headers that will be put in front of its data.
Definition netbuf.c:58
int netif_register(struct netif *netif)
Registers a network interface.
Definition netif.c:206
#define NETIF_RUNNING
Definition netif.h:9
void netif_input(struct netif *netif, struct netbuf *nb)
Hands the stack a frame that a driver received, possibly from an interrupt.
Definition netif.c:323
int pci_irq(struct pci_dev *dev)
Finds the IRQ that a function's interrupt pin comes to.
Definition pci.c:367
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.
Definition pci.c:87
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,...
Definition pci.c:517
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.
Definition pci.c:349
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,...
Definition pmap.c:497
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).
Definition pmap.c:508
int printk(const char *fmt,...)
Writes a kernel message, to the message buffer and the consoles.
Definition printk.c:249
#define BUF_SIZE
Definition ps2mouse.c:40
#define NULL
Definition stddef.h:27
__SIZE_TYPE__ uintptr_t
Definition stddef.h:12
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned long long int uint64_t
Definition stddef.h:23
unsigned short int uint16_t
Definition stddef.h:21
The controller: there is one.
Definition e1000.c:119
unsigned rx_next
Definition e1000.c:127
volatile struct rx_desc * rx
Definition e1000.c:124
unsigned tx_next
Definition e1000.c:128
struct ioaddr regs
Definition e1000.c:121
struct pci_dev pci
Definition e1000.c:120
uint8_t * rx_buf[RX_DESCS]
Definition e1000.c:126
int irq
Definition e1000.c:122
volatile struct tx_desc * tx
Definition e1000.c:125
uint8_t * tx_buf[TX_DESCS]
Definition e1000.c:126
struct netif netif
Definition e1000.c:130
Where a device's registers are.
Definition io.h:95
uintptr_t addr
Definition io.h:97
char type
Definition io.h:96
A packet (net/netbuf.c).
Definition netbuf.h:16
uint8_t * data
Definition netbuf.h:19
size_t len
Definition netbuf.h:20
A network interface (net/netif.c): a device that sends and receives Ethernet frames,...
Definition netif.h:16
int flags
Definition netif.h:20
int(*) transmit(struct netif *netif, struct netbuf *nb)
Definition netif.h:28
struct netif::@020302303320120142126121344055047003105154122231 stats
void * priv
Definition netif.h:30
uint8_t mac[6]
Definition netif.h:18
A base address register (BAR): where the device's registers are, how big they are,...
Definition pci.h:81
uint64_t addr
Definition pci.h:82
uint64_t size
Definition pci.h:83
int io
Definition pci.h:84
A PCI function: its bus, device (slot) and function numbers.
Definition pci.h:18
A receive descriptor: a buffer, and what the controller put in it.
Definition e1000.c:87
uint64_t addr
Definition e1000.c:88
uint8_t errors
Definition e1000.c:92
uint8_t status
Definition e1000.c:91
uint16_t length
Definition e1000.c:89
uint16_t checksum
Definition e1000.c:90
uint16_t special
Definition e1000.c:93
A transmit descriptor: a frame to send, and whether it was sent.
Definition e1000.c:97
uint8_t css
Definition e1000.c:103
uint16_t length
Definition e1000.c:99
uint16_t special
Definition e1000.c:104
uint8_t cmd
Definition e1000.c:101
uint8_t cso
Definition e1000.c:100
uint64_t addr
Definition e1000.c:98
uint8_t status
Definition e1000.c:102
A page of physical memory (pages[] has one for every page of RAM).
Definition vm.h:150
paddr_t paddr
its physical address
Definition vm.h:151