Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/net/tests/rtl8127_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / net / tests / rtl8127_test.c` 677 lines ยท 27567 bytes Raw Blame History 1 /* rtl8127_test.c -- the RTL8127 driver core (aienos_rtl8127.c) against a chip 2 * model: a register array with the side effects the vendor reference relies 3 * on (reset bit, OCP windows, FIFO-empty bits, close pointer) and a ring 4 * simulator that walks the driver's TX and RX descriptors the way the chip 5 * would, refusing any DMA address outside the driver's region (the SMMU 6 * window on the Spark). The model is written from the same vendor source as 7 * the driver (DOCS SILENT); it checks that the driver does what the 8 * reference does, not that the reference is right for the chip. 9 * Prints RTL8127_MODEL: PASS / FAIL. */ 10 #include "../aienos_rtl8127.h" 11 #include "../aienos_report.h" 12 13 #include 14 #include 15 #include 16 17 static int failures, checks; 18 #define CHECK(c, ...) \ 19 do { \ 20 checks++; \ 21 if (!(c)) { \ 22 failures++; \ 23 printf("FAIL %s:%d: ", __FILE__, __LINE__); \ 24 printf(__VA_ARGS__); \ 25 printf("\n"); \ 26 } \ 27 } while (0) 28 29 /* ---- chip model ---- */ 30 typedef struct { 31 uint8_t reg[RTL_MMIO_MIN]; 32 uint16_t ocp[0x8000]; /* MAC OCP space, by address/2 */ 33 uint16_t phy[0x8000]; /* PHY OCP space, by address/2 */ 34 uint32_t ocp_latch, phy_latch; 35 int phy_busy; /* PHY OCP flag phase */ 36 /* configuration */ 37 uint32_t tcr_id; 38 int absent, reset_stuck, fifo_never_empty, clo_after_reset, hostile_clo; 39 /* DMA region the chip may touch (the SMMU window) */ 40 uint8_t *mem; 41 uint64_t dma; 42 size_t len; 43 unsigned dma_faults; 44 /* ring state inside the chip */ 45 uint32_t tx_hw, rx_hw, clo, tail; 46 uint32_t tx_origin; /* close pointer after reset: tx_hw, clo and tail count from it */ 47 int defer_tx; /* close descriptors only when asked */ 48 unsigned resets, writes, ocp_writes; 49 int mac_written_unlocked, mac_written_locked; 50 int enabled_before_rings; /* TE|RE written before both ring bases */ 51 int tnpds_set, rdsar_set; 52 uint8_t tx_frames[64][RTL_FRAME_MAX + 64]; 53 size_t tx_len[64]; 54 unsigned tx_count; 55 unsigned rx_overflow; 56 } chip; 57 58 static chip C; 59 60 static uint32_t get(uint32_t off, unsigned w) 61 { 62 uint32_t v = 0; 63 for (unsigned i = 0; i < w; i++) 64 v |= (uint32_t)C.reg[off + i] << (8 * i); 65 return v; 66 } 67 static void put(uint32_t off, unsigned w, uint32_t v) 68 { 69 for (unsigned i = 0; i < w; i++) 70 C.reg[off + i] = (uint8_t)(v >> (8 * i)); 71 } 72 73 static uint8_t *dma_ptr(uint64_t a, size_t n) 74 { 75 if (a < C.dma || a + n > C.dma + C.len || a + n < a) { 76 C.dma_faults++; 77 return 0; 78 } 79 return C.mem + (a - C.dma); 80 } 81 static uint32_t rd32le(const uint8_t *p) { return p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24; } 82 static void wr32le(uint8_t *p, uint32_t v) 83 { 84 for (int i = 0; i < 4; i++) 85 p[i] = (uint8_t)(v >> (8 * i)); 86 } 87 static uint64_t ring_base(uint32_t lo) 88 { 89 return (uint64_t)get(lo, 4) | (uint64_t)get(lo + 4, 4) << 32; 90 } 91 92 static void chip_reset_state(void) 93 { 94 C.rx_hw = 0; 95 C.tx_hw = C.tx_origin = (uint32_t)C.clo_after_reset; /* the chip's TX index counts from its close base */ 96 C.clo = (uint32_t)C.clo_after_reset; 97 C.tail = 0; 98 put(RTL_HW_CLO0, 4, C.clo); 99 C.resets++; 100 } 101 102 /* The chip fetches every descriptor between its index and the tail. */ 103 static void chip_tx_run(void) 104 { 105 uint8_t *ring = dma_ptr(ring_base(RTL_TNPDS_LO), RTL_TX_N * RTL_TXD_SIZE); 106 if (!ring || !(get(RTL_CHIPCMD, 1) & RTL_CMD_TXEN)) 107 return; 108 if (((C.tail - C.tx_hw) & RTL_CLO_MASK) > RTL_TX_N) { 109 C.dma_faults += 1000; /* tail more than a ring ahead of the chip: tail and close pointer disagree */ 110 return; 111 } 112 while (C.tx_hw != (C.tail & RTL_CLO_MASK)) { 113 uint32_t slot = (C.tx_hw - C.tx_origin) % RTL_TX_N; 114 uint8_t *d = ring + slot * RTL_TXD_SIZE; 115 uint32_t o1 = rd32le(d); 116 if (!(o1 & RTL_DESC_OWN)) 117 break; 118 uint64_t a = rd32le(d + 8) | (uint64_t)rd32le(d + 12) << 32; 119 size_t n = o1 & 0x3ffffu; 120 uint8_t *b = dma_ptr(a, n); 121 if (!b) 122 break; 123 if (C.tx_count < 64) { 124 memcpy(C.tx_frames[C.tx_count], b, n < sizeof C.tx_frames[0] ? n : sizeof C.tx_frames[0]); 125 C.tx_len[C.tx_count] = n; 126 } 127 C.tx_count++; 128 int eor = (o1 & RTL_DESC_EOR) != 0; 129 if (eor != (slot == RTL_TX_N - 1)) 130 C.dma_faults += 1000; /* EOR on the wrong slot: flagged loudly */ 131 C.tx_hw = (C.tx_hw + 1) & RTL_CLO_MASK; 132 if (!C.defer_tx) 133 C.clo++; 134 } 135 put(RTL_HW_CLO0, 4, C.clo & RTL_CLO_MASK); 136 } 137 138 static uint32_t m_read(void *ctx, uint32_t off, unsigned w) 139 { 140 (void)ctx; 141 if (C.absent) 142 return w == 4 ? 0xffffffffu : w == 2 ? 0xffffu : 0xffu; 143 if (off + w > RTL_MMIO_MIN) { 144 C.dma_faults += 10000; 145 return 0; 146 } 147 if (off == RTL_CHIPCMD && (C.reg[off] & RTL_CMD_RESET) && !C.reset_stuck) { 148 C.reg[off] &= (uint8_t)~RTL_CMD_RESET; /* reset finishes on the first poll */ 149 chip_reset_state(); 150 } 151 if (off == RTL_MACOCP && w == 4) 152 return C.ocp[(C.ocp_latch >> 16) & 0x7fff]; 153 if (off == RTL_PHYOCP && w == 4) { 154 if (C.phy_busy == 1) { /* write in flight: flag clears on first poll */ 155 C.phy_busy = 0; 156 return 0; 157 } 158 if (C.phy_busy == 2) { /* read: flag set with data */ 159 uint32_t a = (C.phy_latch >> 16) & 0x7fff; 160 uint32_t v = C.phy[a]; 161 if (a == 0xA420u / 2) /* PHY state: 3 once BMCR has autoneg on */ 162 v = (C.phy[0xA400u / 2] & 0x1000u) ? 3 : 1; 163 return RTL_OCP_FLAG | v; 164 } 165 return 0; 166 } 167 if (off == RTL_HW_CLO0 && C.hostile_clo) 168 return C.clo + 1000; 169 return get(off, w); 170 } 171 172 static void m_write(void *ctx, uint32_t off, unsigned w, uint32_t v) 173 { 174 (void)ctx; 175 C.writes++; 176 if (C.absent) 177 return; 178 if (off + w > RTL_MMIO_MIN) { 179 C.dma_faults += 10000; 180 return; 181 } 182 switch (off) { 183 case RTL_MACOCP: 184 C.ocp_latch = v; 185 if (v & RTL_OCP_WRITE) { 186 C.ocp[(v >> 16) & 0x7fff] = (uint16_t)v; 187 C.ocp_writes++; 188 } 189 return; 190 case RTL_PHYOCP: 191 C.phy_latch = v; 192 if (v & RTL_OCP_WRITE) { 193 C.phy[(v >> 16) & 0x7fff] = (uint16_t)v; 194 C.phy_busy = 1; 195 } else 196 C.phy_busy = 2; 197 return; 198 case RTL_MAC0: 199 case RTL_MAC4: 200 if (w == 4) { 201 if ((get(RTL_CFG9346, 1) & RTL_CFG9346_UNLOCK) == RTL_CFG9346_UNLOCK) 202 C.mac_written_unlocked++; 203 else 204 C.mac_written_locked++; 205 } 206 break; 207 case RTL_TNPDS_LO: 208 C.tnpds_set = 1; 209 break; 210 case RTL_RDSAR_LO: 211 C.rdsar_set = 1; 212 break; 213 case RTL_SW_TAIL0: 214 put(off, w, v); 215 C.tail = v; 216 chip_tx_run(); 217 return; 218 case RTL_CHIPCMD: 219 if ((v & (RTL_CMD_TXEN | RTL_CMD_RXEN)) == (RTL_CMD_TXEN | RTL_CMD_RXEN) && !(C.tnpds_set && C.rdsar_set)) 220 C.enabled_before_rings = 1; 221 break; 222 case RTL_ISR0: 223 put(off, w, get(off, w) & ~v); /* write one to clear */ 224 return; 225 } 226 put(off, w, v); 227 } 228 229 static void m_delay(void *ctx, uint32_t us) { (void)ctx, (void)us; } 230 static void m_barrier(void *ctx) { (void)ctx; } 231 static const rtl_ops OPS = {0, m_read, m_write, m_delay, m_barrier}; 232 233 /* Fixed registers of a freshly powered chip. */ 234 static void chip_init(uint8_t *mem, uint64_t dma, size_t len) 235 { 236 memset(&C, 0, sizeof C); 237 C.mem = mem; 238 C.dma = dma; 239 C.len = len; 240 C.tcr_id = RTL_TCR_ID_8127 | 0x00100000u; /* CFG_METHOD_2 */ 241 put(RTL_TCR, 4, C.tcr_id); 242 /* 0xD2 is 16 bit, so its bit 9 (LL share FIFO ready) is bit 1 of MCUCmd 0xD3 */ 243 put(RTL_LLFIFO, 2, 0x0200); 244 C.reg[RTL_MCUCMD] |= RTL_MCU_TXFIFO_EMPTY | RTL_MCU_RXFIFO_EMPTY | RTL_MCU_NOW_IS_OOB; 245 put(RTL_INTRMIT, 2, 0x0103); 246 put(RTL_RXDESC_TYPE, 1, 0x02); /* type 4 left on by a previous owner */ 247 put(RTL_TXNOCLOSE_EN, 1, 0); 248 put(RTL_RSS_CTRL, 4, 0x00000f01u); /* RSS on from a previous owner */ 249 put(RTL_Q_NUM_CTRL, 2, 0x0108u | 0x0008u); /* 4 RX queues (bits 4:2 = 2), other bits kept */ 250 C.ocp[RTL_OCP_TXDESC_FMT / 2] = 0x8002u; /* old 16-byte TX descriptor format selected */ 251 put(RTL_RXDV_GATE, 1, 0); 252 put(RTL_PHYSTATUS, 2, 0x4000 | RTL_PHYS_LINK | RTL_PHYS_FULLDUP); 253 put(RTL_ISR0, 4, 0x00000085u); 254 put(RTL_IMR0, 4, 0xffffffffu); 255 put(RTL_BACKUP_ADDR0, 4, 0x33221100u ^ 0x01u); /* 00:11:22:33 after the flip below */ 256 C.reg[RTL_BACKUP_ADDR0] = 0x48; /* 48:11:22:33:44:55, unicast */ 257 put(RTL_BACKUP_ADDR1, 2, 0x5544u); 258 for (int i = 0; i < 6; i++) 259 C.reg[RTL_MAC0 + i] = (uint8_t)(0xa0 + i); 260 } 261 262 /* Model receives a frame from the wire: needs a descriptor the chip owns, 263 * writes data + 4-byte FCS and the status word. */ 264 static int chip_rx(const uint8_t *f, size_t n, uint32_t extra_status, uint32_t force_len) 265 { 266 uint8_t *ring = dma_ptr(ring_base(RTL_RDSAR_LO), RTL_RX_N * RTL_RXD_SIZE); 267 if (!ring || !(get(RTL_CHIPCMD, 1) & RTL_CMD_RXEN)) 268 return -1; 269 uint8_t *d = ring + (C.rx_hw % RTL_RX_N) * RTL_RXD_SIZE; 270 uint32_t o1 = rd32le(d); 271 if (!(o1 & RTL_DESC_OWN)) { 272 C.rx_overflow++; 273 return -2; 274 } 275 uint32_t size = o1 & RTL_RX_LEN_MASK; 276 uint64_t a = rd32le(d + 8) | (uint64_t)rd32le(d + 12) << 32; 277 uint8_t *b = dma_ptr(a, size); 278 if (!b) 279 return -3; 280 if (n + 4 > size) 281 return -4; 282 memcpy(b, f, n); 283 memset(b + n, 0xcc, 4); 284 uint32_t l = force_len ? force_len : (uint32_t)(n + 4); 285 wr32le(d, RTL_DESC_FS | RTL_DESC_LS | extra_status | l); 286 C.rx_hw++; 287 return 0; 288 } 289 290 /* ---- helpers ---- */ 291 static uint8_t *region; 292 static const uint64_t DMA = 0x0000004500000000ull; /* above 4 GiB: hi words used */ 293 294 static rtl_err up(rtl_dev *d) 295 { 296 chip_init(region, DMA, RTL_MEM_SIZE); 297 return rtl_init(d, &OPS, region, DMA, RTL_MEM_SIZE); 298 } 299 300 static void frame_fill(uint8_t *f, size_t n, unsigned seed) 301 { 302 for (size_t i = 0; i < n; i++) 303 f[i] = (uint8_t)(seed * 31u + i * 7u); 304 } 305 306 /* ---- tests ---- */ 307 static void test_init(void) 308 { 309 rtl_dev d; 310 rtl_err e = up(&d); 311 CHECK(e == RTL_OK, "init %s", rtl_err_name(e)); 312 CHECK(d.ready && !d.broken, "ready"); 313 CHECK(d.mcfg == 2 && !d.ic_unknown, "mcfg %u", d.mcfg); 314 static const uint8_t want[6] = {0x48, 0x11, 0x22, 0x33, 0x44, 0x55}; 315 CHECK(memcmp(d.mac, want, 6) == 0 && d.mac_source == RTL_MAC_BACKUP, "mac from backup"); 316 CHECK(get(RTL_MAC0, 4) == 0x33221148u && get(RTL_MAC4, 2) == 0x5544u, "rar_set wrote the address"); 317 CHECK(C.mac_written_unlocked == 2 && C.mac_written_locked == 0, "MAC written only while Cfg9346 unlocked"); 318 CHECK((get(RTL_CFG9346, 1) & RTL_CFG9346_UNLOCK) == 0, "Cfg9346 locked at the end"); 319 CHECK(ring_base(RTL_TNPDS_LO) == DMA + RTL_OFF_TXD, "TX ring base"); 320 CHECK(ring_base(RTL_RDSAR_LO) == DMA + RTL_OFF_RXD, "RX ring base"); 321 CHECK(!C.enabled_before_rings, "TE|RE only after ring bases"); 322 CHECK(get(RTL_TCR, 4) == RTL_TCR_VALUE, "TCR 0x%08x", get(RTL_TCR, 4)); 323 CHECK(get(RTL_RCR, 4) == (RTL_RCR_CHIP | RTL_RCR_ACCEPT_BCAST | RTL_RCR_ACCEPT_MYPHYS), "RCR"); 324 CHECK(get(RTL_RMS, 2) == (RTL_RMS_VALUE | RTL_RMS_VLAN), "RMS"); 325 CHECK(get(RTL_TXNOCLOSE_EN, 1) & 0x04, "no-close enabled"); 326 CHECK(C.ocp[RTL_OCP_TXDESC_FMT / 2] == 0x8001u, "32-byte TX descriptor format, other bits kept (0x%04x)", 327 C.ocp[RTL_OCP_TXDESC_FMT / 2]); 328 CHECK(get(RTL_RSS_CTRL, 4) == 0, "RSS off"); 329 CHECK(get(RTL_Q_NUM_CTRL, 2) == 0x0100u, "one RX queue, other bits kept (0x%04x)", get(RTL_Q_NUM_CTRL, 2)); 330 CHECK(!(get(RTL_RXDESC_TYPE, 1) & 0x02), "RX descriptor type 1"); 331 CHECK(!(get(RTL_RXDV_GATE, 1) & 0x08), "RX DV gate open"); 332 CHECK(get(RTL_IMR0, 4) == 0, "interrupts masked"); 333 CHECK(get(RTL_ISR0, 4) == 0, "interrupt status acknowledged"); 334 CHECK(!(get(RTL_INT_CFG0, 1) & 0x61), "interrupt type 1"); 335 CHECK(get(RTL_CHIPCMD, 1) == (RTL_CMD_TXEN | RTL_CMD_RXEN), "TE|RE"); 336 CHECK(!(get(RTL_MCUCMD, 1) & RTL_MCU_NOW_IS_OOB), "now_is_oob cleared"); 337 CHECK(C.ocp[0xC0BC / 2] == 0x00FF && C.ocp[0xC0AA / 2] == 0x07D0 && C.ocp[0xC0A6 / 2] == 0x01B5 && 338 C.ocp[0xC01E / 2] == 0x5555, 339 "exit_oob MAC OCP values"); 340 CHECK(C.phy[0xA400 / 2] == 0x1000 && d.phy_state == 3, "PHY powered up, autoneg on"); 341 CHECK((get(RTL_PMCH, 1) & 0xC0) == 0xC0, "PLL on"); 342 CHECK(C.resets == 2, "two soft resets (exit_oob, hw_reset): %u", C.resets); 343 CHECK(d.wait_timeouts == 0, "no wait ran out: %u", d.wait_timeouts); 344 for (uint32_t i = 0; i < RTL_RX_N; i++) { 345 const uint8_t *x = region + RTL_OFF_RXD + i * RTL_RXD_SIZE; 346 uint32_t o1 = rd32le(x); 347 uint64_t a = rd32le(x + 8) | (uint64_t)rd32le(x + 12) << 32; 348 CHECK((o1 & RTL_DESC_OWN) && (o1 & RTL_RX_LEN_MASK) == RTL_RX_BUF, "RX slot %u posted", i); 349 CHECK(((o1 & RTL_DESC_EOR) != 0) == (i == RTL_RX_N - 1), "EOR only on the last RX slot"); 350 CHECK(a == DMA + RTL_OFF_RXBUF + (uint64_t)i * RTL_BUF_STRIDE, "RX buffer address %u", i); 351 } 352 uint32_t mbps = 0; 353 CHECK(rtl_link(&d, &mbps) == 1 && mbps == 10000, "link up 10G"); 354 CHECK(C.dma_faults == 0, "no DMA outside the window"); 355 } 356 357 static void test_refusals(void) 358 { 359 rtl_dev d; 360 chip_init(region, DMA, RTL_MEM_SIZE); 361 C.absent = 1; 362 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_E_ABSENT, "absent"); 363 CHECK(C.writes == 0, "nothing written to an absent device"); 364 CHECK(rtl_tx(&d, (const uint8_t *)"`0123456789abcdef`", 16) == RTL_E_NOT_READY, "tx before init"); 365 366 chip_init(region, DMA, RTL_MEM_SIZE); 367 put(RTL_TCR, 4, 0x54000000u); /* an RTL8125 id */ 368 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_E_CHIP, "wrong chip"); 369 CHECK(C.writes == 0, "nothing written to a wrong chip"); 370 CHECK(rtl_stop(&d) == RTL_E_NOT_READY && C.writes == 0, "stop writes nothing to a wrong chip"); 371 372 chip_init(region, DMA, RTL_MEM_SIZE); 373 put(RTL_TCR, 4, RTL_TCR_ID_8127 | 0x00300000u); 374 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_OK && d.ic_unknown && d.mcfg == 2, "unknown IC ver"); 375 376 chip_init(region, DMA, RTL_MEM_SIZE); 377 C.reset_stuck = 1; 378 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_E_RESET_TIMEOUT, "reset timeout"); 379 CHECK(!d.ready, "not ready after a reset timeout"); 380 381 chip_init(region, DMA, RTL_MEM_SIZE); 382 C.fifo_never_empty = 1; 383 put(RTL_MCUCMD, 1, 0); 384 put(RTL_INTRMIT, 2, 0); 385 put(RTL_LLFIFO, 2, 0); 386 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_OK && d.wait_timeouts == 2 * 2 + 2, 387 "FIFO waits counted, not fatal (vendor ignores them): %u", d.wait_timeouts); 388 389 chip_init(region, DMA, RTL_MEM_SIZE); 390 CHECK(rtl_init(&d, &OPS, region + 16, DMA, RTL_MEM_SIZE) == RTL_E_MEM, "misaligned mem"); 391 CHECK(rtl_init(&d, &OPS, region, DMA + 256, RTL_MEM_SIZE) == RTL_E_MEM, "misaligned dma"); 392 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE - 1) == RTL_E_MEM, "short region"); 393 CHECK(rtl_init(&d, &OPS, region, 0xfffffffffffff000ull, RTL_MEM_SIZE) == RTL_E_MEM, "bus range wraps"); 394 CHECK(C.writes == 0, "no register writes on a bad region"); 395 rtl_ops bad = OPS; 396 bad.barrier = 0; 397 CHECK(rtl_init(&d, &bad, region, DMA, RTL_MEM_SIZE) == RTL_E_ARG, "missing op"); 398 } 399 400 static void test_mac_sources(void) 401 { 402 rtl_dev d; 403 chip_init(region, DMA, RTL_MEM_SIZE); 404 put(RTL_BACKUP_ADDR0, 4, 0); 405 put(RTL_BACKUP_ADDR1, 2, 0); 406 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_OK && d.mac_source == RTL_MAC_MAC0 && d.mac[0] == 0xa0, 407 "MAC0 when the backup address is zero"); 408 chip_init(region, DMA, RTL_MEM_SIZE); 409 put(RTL_BACKUP_ADDR0, 4, 0x000000ffu); /* multicast */ 410 for (int i = 0; i < 6; i++) 411 C.reg[RTL_MAC0 + i] = 0xff; 412 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_OK && d.mac_source == RTL_MAC_FALLBACK && 413 (d.mac[0] & 2) && !(d.mac[0] & 1), 414 "locally administered fallback"); 415 } 416 417 static void test_tx(void) 418 { 419 rtl_dev d; 420 CHECK(up(&d) == RTL_OK, "up"); 421 uint8_t f[RTL_FRAME_MAX]; 422 frame_fill(f, sizeof f, 1); 423 CHECK(rtl_tx(&d, f, 13) == RTL_E_ARG && rtl_tx(&d, f, RTL_FRAME_MAX + 1) == RTL_E_ARG, "length bounds"); 424 CHECK(rtl_tx(&d, f, 42) == RTL_OK, "short frame"); 425 CHECK(C.tx_count == 1 && C.tx_len[0] == 60 && memcmp(C.tx_frames[0], f, 42) == 0, "sent and padded to 60"); 426 int zero = 1; 427 for (int i = 42; i < 60; i++) 428 zero &= C.tx_frames[0][i] == 0; 429 CHECK(zero, "padding is zero"); 430 CHECK(get(RTL_SW_TAIL0, 4) == 1, "tail moved to 1"); 431 unsigned r = 0; 432 CHECK(rtl_tx_reclaim(&d, &r) == RTL_OK && r == 1 && rtl_tx_pending(&d) == 0, "reclaimed 1"); 433 434 /* Ring full with completions held back, then released. */ 435 C.defer_tx = 1; 436 for (unsigned i = 0; i < RTL_TX_N; i++) 437 CHECK(rtl_tx(&d, f, 100 + i) == RTL_OK, "post %u", i); 438 CHECK(rtl_tx(&d, f, 100) == RTL_E_FULL, "full"); 439 CHECK(rtl_tx_reclaim(&d, &r) == RTL_OK && r == 0, "nothing closed yet"); 440 C.clo += 5; 441 put(RTL_HW_CLO0, 4, C.clo); 442 CHECK(rtl_tx_reclaim(&d, &r) == RTL_OK && r == 5 && rtl_tx_pending(&d) == RTL_TX_N - 5, "5 closed"); 443 C.defer_tx = 0; 444 C.clo += RTL_TX_N - 5; 445 put(RTL_HW_CLO0, 4, C.clo); 446 CHECK(rtl_tx_reclaim(&d, &r) == RTL_OK && rtl_tx_pending(&d) == 0, "all closed"); 447 448 /* Many trips around the ring: every frame arrives intact, in order. */ 449 unsigned before = C.tx_count, bad = 0; 450 for (unsigned k = 0; k < 200; k++) { 451 size_t n = 60 + (k * 37u) % (RTL_FRAME_MAX - 60); 452 frame_fill(f, n, k); 453 if (rtl_tx(&d, f, n) != RTL_OK) { 454 bad++; 455 continue; 456 } 457 unsigned idx = C.tx_count - 1; 458 if (idx < 64 && (C.tx_len[idx] != n || memcmp(C.tx_frames[idx], f, n) != 0)) 459 bad++; 460 if (rtl_tx_reclaim(&d, &r) != RTL_OK || r != 1) 461 bad++; 462 if (C.tx_count >= 64) 463 C.tx_count = 0, before = 0; 464 } 465 (void)before; 466 CHECK(bad == 0, "200 frames around the ring: %u bad", bad); 467 CHECK(C.dma_faults == 0, "TX stayed in the window (faults %u)", C.dma_faults); 468 } 469 470 static void test_tx_hostile(void) 471 { 472 rtl_dev d; 473 CHECK(up(&d) == RTL_OK, "up"); 474 uint8_t f[64] = {0}; 475 CHECK(rtl_tx(&d, f, 60) == RTL_OK, "tx"); 476 C.hostile_clo = 1; 477 CHECK(rtl_tx_reclaim(&d, 0) == RTL_E_DEVICE && d.broken, "close pointer ahead of posted"); 478 CHECK(rtl_tx(&d, f, 60) == RTL_E_BROKEN, "sticky on tx"); 479 size_t n; 480 int got; 481 uint8_t out[RTL_FRAME_MAX]; 482 CHECK(rtl_rx(&d, out, sizeof out, &n, &got) == RTL_E_BROKEN, "sticky on rx"); 483 C.hostile_clo = 0; 484 CHECK(rtl_stop(&d) == RTL_OK && !d.ready, "stop"); 485 chip_init(region, DMA, RTL_MEM_SIZE); 486 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_OK && !d.broken, "re-init clears broken"); 487 488 /* A close-pointer base the chip did not reset to zero. */ 489 chip_init(region, DMA, RTL_MEM_SIZE); 490 C.clo_after_reset = 7; 491 CHECK(rtl_init(&d, &OPS, region, DMA, RTL_MEM_SIZE) == RTL_OK && d.clo_base == 7, "close base 7"); 492 unsigned r = 0; 493 CHECK(rtl_tx(&d, f, 60) == RTL_OK && rtl_tx_reclaim(&d, &r) == RTL_OK && r == 1, "reclaim with base 7"); 494 CHECK(get(RTL_SW_TAIL0, 4) == 8 && C.tx_count == 1 && C.dma_faults == 0, "tail counts from base 7 (tail %u)", 495 get(RTL_SW_TAIL0, 4)); 496 for (unsigned k = 0; k < 3 * RTL_TX_N; k++) 497 if (rtl_tx(&d, f, 60) != RTL_OK || rtl_tx_reclaim(&d, 0) != RTL_OK) { 498 CHECK(0, "round %u with base 7", k); 499 break; 500 } 501 CHECK(C.tx_count == 1 + 3 * RTL_TX_N && C.dma_faults == 0, "three rings of frames with base 7 (%u sent)", 502 C.tx_count); 503 } 504 505 static void test_rx(void) 506 { 507 rtl_dev d; 508 CHECK(up(&d) == RTL_OK, "up"); 509 uint8_t f[RTL_FRAME_MAX], out[RTL_FRAME_MAX]; 510 size_t n = 0; 511 int got = 1; 512 CHECK(rtl_rx(&d, out, sizeof out, &n, &got) == RTL_OK && !got, "nothing pending"); 513 unsigned bad = 0; 514 for (unsigned k = 0; k < 100; k++) { 515 size_t len = 14 + (k * 53u) % (RTL_FRAME_MAX - 14); 516 frame_fill(f, len, k + 7); 517 if (chip_rx(f, len, 0, 0) != 0) { 518 bad++; 519 continue; 520 } 521 if (rtl_rx(&d, out, sizeof out, &n, &got) != RTL_OK || !got || n != len || memcmp(out, f, len) != 0) 522 bad++; 523 } 524 CHECK(bad == 0, "100 frames around the RX ring: %u bad", bad); 525 526 /* Errors, runts and split frames are dropped and the slot reposted. */ 527 frame_fill(f, 100, 3); 528 CHECK(chip_rx(f, 100, RTL_RX_RES, 0) == 0, "inject error frame"); 529 CHECK(chip_rx(f, 10, 0, 0) == 0, "inject runt"); 530 CHECK(chip_rx(f, 100, 0, 0) == 0, "inject good"); 531 uint8_t *ring = region + RTL_OFF_RXD; 532 uint32_t slot = (d.rx_cur + 2) % RTL_RX_N; /* the third frame: error, runt, then this one */ 533 wr32le(ring + slot * RTL_RXD_SIZE, rd32le(ring + slot * RTL_RXD_SIZE) & ~RTL_DESC_LS); /* not last */ 534 uint8_t f2[100]; 535 frame_fill(f2, 100, 4); 536 CHECK(chip_rx(f2, 100, 0, 0) == 0, "inject good 2"); 537 unsigned dropped = d.rx_dropped; 538 CHECK(rtl_rx(&d, out, sizeof out, &n, &got) == RTL_OK && got && n == 100 && memcmp(out, f2, 100) == 0, 539 "good 2 after drops"); 540 CHECK(d.rx_dropped == dropped + 3, "error frame, runt and split frame dropped"); 541 542 /* Output buffer smaller than the frame: dropped and reposted, the ring keeps moving. */ 543 CHECK(chip_rx(f, 100, 0, 0) == 0, "inject for small cap"); 544 dropped = d.rx_dropped; 545 CHECK(rtl_rx(&d, out, 50, &n, &got) == RTL_OK && !got && d.rx_dropped == dropped + 1, "cap too small: dropped"); 546 CHECK(chip_rx(f2, 100, 0, 0) == 0, "inject after small cap"); 547 CHECK(rtl_rx(&d, out, sizeof out, &n, &got) == RTL_OK && got && n == 100 && memcmp(out, f2, 100) == 0, 548 "next frame still arrives"); 549 550 /* Largest frame RMS 1522 lets through: 1518 bytes (VLAN tag) without FCS. */ 551 static uint8_t big[RTL_RX_BUF], bigout[RTL_RX_BUF]; 552 frame_fill(big, 1518, 9); 553 CHECK(chip_rx(big, 1518, 0, 0) == 0, "inject 1518-byte frame"); 554 CHECK(rtl_rx(&d, bigout, sizeof bigout, &n, &got) == RTL_OK && got && n == 1518 && memcmp(bigout, big, 1518) == 0, 555 "1518-byte frame received whole"); 556 557 /* Overflow: the chip finds no free slot when the driver never reads. */ 558 for (unsigned i = 0; i < RTL_RX_N + 3; i++) 559 chip_rx(f, 64, 0, 0); 560 CHECK(C.rx_overflow == 3, "chip saw 3 overflows"); 561 unsigned taken = 0; 562 while (rtl_rx(&d, out, sizeof out, &n, &got) == RTL_OK && got) 563 taken++; 564 CHECK(taken == RTL_RX_N, "driver drained %u", taken); 565 CHECK(chip_rx(f, 64, 0, 0) == 0, "ring usable after drain"); 566 567 /* Hostile length: more than the buffer the chip was given. */ 568 CHECK(rtl_rx(&d, out, sizeof out, &n, &got) == RTL_OK && got, "drain"); 569 CHECK(chip_rx(f, 64, 0, RTL_RX_BUF + 4) == 0, "inject oversize length"); 570 CHECK(rtl_rx(&d, out, sizeof out, &n, &got) == RTL_E_DEVICE && d.broken, "oversize length breaks the device"); 571 CHECK(C.dma_faults == 0, "RX stayed in the window (faults %u)", C.dma_faults); 572 } 573 574 /* Seeded random: the chip writes random status words into RX descriptors it 575 * does not own; the driver must never return a length beyond the buffer. */ 576 static void test_rx_random(void) 577 { 578 rtl_dev d; 579 uint8_t out[RTL_FRAME_MAX]; 580 unsigned long seed = 0x8127; 581 unsigned breaks = 0, frames = 0; 582 for (unsigned round = 0; round < 2000; round++) { 583 if (up(&d) != RTL_OK) { 584 CHECK(0, "up"); 585 return; 586 } 587 for (unsigned k = 0; k < 40; k++) { 588 seed = seed * 6364136223846793005ul + 1442695040888963407ul; 589 uint32_t s = (uint32_t)(seed >> 32) & ~RTL_DESC_OWN; 590 uint32_t i = d.rx_cur % RTL_RX_N; 591 wr32le(region + RTL_OFF_RXD + i * RTL_RXD_SIZE, s); 592 size_t n = 0; 593 int got = 0; 594 rtl_err e = rtl_rx(&d, out, sizeof out, &n, &got); 595 if (e == RTL_E_DEVICE) { 596 breaks++; 597 break; 598 } 599 if (e != RTL_OK || (got && (n > RTL_RX_BUF - 4 || n < 14))) { 600 CHECK(0, "random status 0x%08x gave %s n=%zu", s, rtl_err_name(e), n); 601 return; 602 } 603 frames += (unsigned)got; 604 } 605 } 606 CHECK(breaks > 0 && frames > 0, "random statuses: %u frames, %u breaks", frames, breaks); 607 } 608 609 static void test_link(void) 610 { 611 rtl_dev d; 612 CHECK(up(&d) == RTL_OK, "up"); 613 uint32_t m = 1; 614 put(RTL_PHYSTATUS, 2, 0); 615 CHECK(rtl_link(&d, &m) == 0 && m == 0, "down"); 616 put(RTL_PHYSTATUS, 2, 0x0400 | RTL_PHYS_LINK); 617 CHECK(rtl_link(&d, &m) == 1 && m == 2500, "2.5G"); 618 put(RTL_PHYSTATUS, 2, 0x0010 | RTL_PHYS_LINK); 619 CHECK(rtl_link(&d, &m) == 1 && m == 1000, "1G"); 620 put(RTL_PHYSTATUS, 2, 0xffff); 621 CHECK(rtl_link(&d, &m) == 0, "all ones is down"); 622 } 623 624 /* The boot report travels through the driver: every frame the chip puts on 625 * the wire must parse back to the report bytes. */ 626 static void test_report_over_driver(void) 627 { 628 rtl_dev d; 629 CHECK(up(&d) == RTL_OK, "up"); 630 static char log[5000]; 631 for (size_t i = 0; i < sizeof log; i++) 632 log[i] = (char)('a' + i % 26); 633 unsigned parts = net_report_parts(sizeof log); 634 CHECK(parts == 5, "parts %u", parts); 635 uint8_t fr[1600]; 636 C.tx_count = 0; 637 for (unsigned p = 0; p < parts; p++) { 638 size_t n = net_report_frame(NET_REPORT_TAG_SPARK, d.mac, 0x1122334455667788ull, log, sizeof log, p, fr, 639 sizeof fr); 640 CHECK(n > 0 && rtl_tx(&d, fr, n) == RTL_OK, "part %u", p); 641 rtl_tx_reclaim(&d, 0); 642 } 643 char back[sizeof log]; 644 size_t got = 0; 645 for (unsigned p = 0; p < parts && p < C.tx_count; p++) { 646 net_report_part rp; 647 int rc = net_report_parse(NET_REPORT_TAG_SPARK, C.tx_frames[p], C.tx_len[p], &rp); 648 CHECK(rc == 0 && rp.part == p && rp.parts == parts && rp.total == sizeof log && rp.run == 0x1122334455667788ull, 649 "parse part %u rc=%d", p, rc); 650 if (rc == 0 && rp.offset + rp.len <= sizeof back) { 651 memcpy(back + rp.offset, rp.data, rp.len); 652 got += rp.len; 653 } 654 } 655 CHECK(got == sizeof log && memcmp(back, log, sizeof log) == 0, "report rebuilt from the wire"); 656 CHECK(memcmp(C.tx_frames[0], "\xff\xff\xff\xff\xff\xff", 6) == 0 && memcmp(C.tx_frames[0] + 6, d.mac, 6) == 0, 657 "broadcast from the chip's MAC"); 658 } 659 660 int main(void) 661 { 662 region = aligned_alloc(4096, RTL_MEM_SIZE); 663 if (!region) 664 return 2; 665 test_init(); 666 test_refusals(); 667 test_mac_sources(); 668 test_tx(); 669 test_tx_hostile(); 670 test_rx(); 671 test_rx_random(); 672 test_link(); 673 test_report_over_driver(); 674 free(region); 675 printf("RTL8127_MODEL: %s (%d checks, %d failures)\n", failures ? "FAIL" : "PASS", checks, failures); 676 return failures ? 1 : 0; 677 }