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/rtl8127-omega/blob/main/om/rtl8127/rtl8127.om # rtl8127-omega aien-dev/rtl8127-omega 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 `rtl8127-omega / om / rtl8127 / rtl8127.om` 703 lines ยท 14859 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note rtl8127.om: Realtek RTL8127 10 GbE (PCI 10ec:8127) driver core in Omega. Polled, no interrupts, one TX and one RX ring. 3 note 4 note EXPERIMENTAL. Never run on a real chip. Realtek publishes no register document for this chip; every offset, bit and 5 note sequence below comes from Realtek's own Linux driver r8127 v11.014.00 (github.com/openwrt/rtl8127 `ec2f285`, r8127_n.c 6 note and r8127.h, line numbers in the notes). It is a port of the C driver core in aien-dev/aienux PR 450 (native/net/aienos_rtl8127.c). 7 note 8 note Program-mode companion unit (it owns globals). Build it together with ONE backend unit that defines 9 note rtlrd off w read width 1, 2 or 4 bytes at register offset off (BAR 2) 10 note rtlwr off w v write 11 note rtldly us wait about us microseconds 12 note rtlbar full memory barrier 13 note (model.om for the tests, hw.om for a real chip) and one main. Every register read and write goes through those four. 14 note Everything the chip may DMA to or from is the one region rdma (rings at 0 and 1024, TX buffers from 4096, RX buffers from 36864). 15 note The chip must see rdma at bus address dma, 4096-aligned. Omega traps (exit 103) on any index outside an array. 16 const MAC0 ulong 0 17 const MAC4 ulong 4 18 const TNPDSLO ulong 32 19 const TNPDSHI ulong 36 20 const INTCFG0 ulong 52 21 const CHIPCMD ulong 55 22 const IMR0 ulong 56 23 const ISR0 ulong 60 24 const TCR ulong 64 25 const RCR ulong 68 26 const CFG9346 ulong 80 27 const TIMINT0 ulong 88 28 const TIMINT1 ulong 92 29 const PHYSTAT ulong 108 30 const PMCH ulong 111 31 const TIMINT2 ulong 140 32 const MACOCP ulong 176 33 const PHYOCP ulong 184 34 const LLFIFO ulong 210 35 const MCUCMD ulong 211 36 const RXDTYPE ulong 216 37 const RMS ulong 218 38 const INTRMIT ulong 226 39 const RDSARLO ulong 228 40 const RDSARHI ulong 232 41 const RXDVGATE ulong 242 42 const TIMINT3 ulong 244 43 const SWTAIL0 ulong 3376 44 const HWCLO0 ulong 3380 45 const BAKADR0 ulong 6624 46 const BAKADR1 ulong 6628 47 const TXNOCLO ulong 8420 48 const RSSCTRL ulong 17664 49 const QNUMCTL ulong 18432 50 const OCPFMT ulong 60248 51 const MMIOMIN ulong 20480 52 const CMDSTOP ulong 128 53 const CMDRST ulong 16 54 const CMDRXEN ulong 8 55 const CMDTXEN ulong 4 56 const UNLOCK ulong 192 57 const RCRMASK ulong 63 58 const RCRBCST ulong 8 59 const RCRMYPH ulong 2 60 const RCRCHIP ulong `1088425216` 61 const RCRRST ulong 1280 62 const TCRVAL ulong `50333504` 63 const TCRIDM ulong `2088763392` 64 const TCRID ulong `1820327936` 65 const TCRICM ulong `7340032` 66 const MCUOOB ulong 128 67 const MCUTXE ulong 32 68 const MCURXE ulong 16 69 const OCPWRB ulong `2147483648` 70 const OCPFLAG ulong `2147483648` 71 const PHYSLNK ulong 2 72 const PHYSFD ulong 1 73 const RMSVLAN ulong 32768 74 const DOWN ulong `2147483648` 75 const DEOR ulong `1073741824` 76 const DFS ulong `536870912` 77 const DLS ulong `268435456` 78 const RXRES ulong `2097152` 79 const RXLEN ulong 16383 80 const CLOMASK ulong `2147483647` 81 const TXN ulong 16 82 const RXN ulong 16 83 const FMIN ulong 60 84 const FMAX ulong 1514 85 const RXBUF ulong 1536 86 const RMSVAL ulong 1522 87 const STRIDE ulong 2048 88 const OFFTXD ulong 0 89 const OFFRXD ulong 1024 90 const OFFTXB ulong 4096 91 const OFFRXB ulong 36864 92 const MEMSIZE ulong 69632 93 94 note State cells (global rs). One device per program. 95 const S_READY ulong 0 96 const S_BROKE ulong 1 97 const S_MCFG ulong 2 98 const S_ICUNK ulong 3 99 const S_MACSRC ulong 4 100 const S_UPSRES ulong 5 101 const S_PHYST ulong 6 102 const S_TCRID ulong 7 103 const S_WTO ulong 8 104 const S_TXCUR ulong 9 105 const S_TXDIR ulong 10 106 const S_CLOB ulong 11 107 const S_RXCUR ulong 12 108 const S_RXDRP ulong 13 109 const S_DMA ulong 14 110 const S_MAC0 ulong 15 111 const S_RXLEN ulong 21 112 const S_RECL ulong 22 113 const S_MBPS ulong 23 114 115 note Error codes returned by rtlinit, rtltx, rtltxrec, rtlrx, rtlstop. 116 const E_ARG ulong 1 117 const E_MEM ulong 2 118 const E_ABSENT ulong 3 119 const E_CHIP ulong 4 120 const E_RSTTMO ulong 5 121 const E_FULL ulong 6 122 const E_DEVICE ulong 7 123 const E_BROKEN ulong 8 124 const E_NOTRDY ulong 9 125 126 note MAC address sources, rs[S_MACSRC]. 127 const MS_BACK ulong 1 128 const MS_MAC0 ulong 2 129 const MS_FALL ulong 3 130 131 note rs: driver state. rdma: the one DMA region (rings and packet buffers). fbuf: frame to send. 132 note obuf: frame received. 133 global rs ulong 32 134 global rdma uchar 69632 135 global fbuf uchar 2048 136 global obuf uchar 2048 137 138 note ---- register access: width 1, 2 or 4, through the backend (rtlrd, rtlwr, rtldly, rtlbar) ---- 139 fn rtlr8 o ulong to ulong 140 ret rtlrd o 1 141 end 142 fn rtlr16 o ulong to ulong 143 ret rtlrd o 2 144 end 145 fn rtlr32 o ulong to ulong 146 ret rtlrd o 4 147 end 148 fn rtlw8 o ulong v ulong to void 149 rtlwr o 1 and v 255 150 end 151 fn rtlw16 o ulong v ulong to void 152 rtlwr o 2 and v 65535 153 end 154 fn rtlw32 o ulong v ulong to void 155 rtlwr o 4 and v `4294967295` 156 end 157 158 fn rtlwto to void 159 put rs S_WTO add get rs S_WTO 1 160 end 161 162 note ---- little-endian 32 bit words in the DMA region ---- 163 fn dld32 o ulong to ulong 164 a = ulong get rdma o 165 b = ulong get rdma add o 1 166 c = ulong get rdma add o 2 167 d = ulong get rdma add o 3 168 ret or or or a shl b 8 shl c 16 shl d 24 169 end 170 fn dst32 o ulong v ulong to void 171 put rdma o uchar and v 255 172 put rdma add o 1 uchar and shr v 8 255 173 put rdma add o 2 uchar and shr v 16 255 174 put rdma add o 3 uchar and shr v 24 255 175 end 176 177 note ---- MAC OCP window, r8127_n.c:3287-3314 ---- 178 fn rtlocrd a ulong to ulong 179 rtlw32 MACOCP shl div a 2 16 180 r = rtlr32 MACOCP 181 ret and r 65535 182 end 183 fn rtlocwr a ulong v ulong to void 184 rtlw32 MACOCP or or shl div a 2 16 and v 65535 OCPWRB 185 end 186 187 note ---- PHY OCP window, r8127_n.c:3057-3073 (write waits for the flag to clear) and 3155-3167 (read waits for it to set) ---- 188 fn rtlphwr a ulong v ulong to void 189 rtlw32 PHYOCP or or shl div a 2 16 and v 65535 OCPWRB 190 i = ulong 0 191 while lt i 20000 bound 20000 192 rtldly 1 193 f = rtlr32 PHYOCP 194 if eq and f OCPFLAG 0 195 ret 196 end 197 i = add i 1 198 end 199 rtlwto 200 end 201 fn rtlphrd a ulong to ulong 202 rtlw32 PHYOCP shl div a 2 16 203 i = ulong 0 204 while lt i 20000 bound 20000 205 rtldly 1 206 f = rtlr32 PHYOCP 207 if ne and f OCPFLAG 0 208 v = rtlr32 PHYOCP 209 ret and v 65535 210 end 211 i = add i 1 212 end 213 rtlwto 214 ret 65535 215 end 216 217 note r8127_n.c:4872-4882 218 fn rtlwaitll to void 219 i = ulong 0 220 while lt i 10 bound 10 221 rtldly 100 222 v = rtlr16 LLFIFO 223 if ne and v 512 0 224 ret 225 end 226 i = add i 1 227 end 228 rtlwto 229 end 230 231 note r8127_n.c:4287-4293 232 fn rtlrxoff to void 233 v = rtlr32 RCR 234 rtlw32 RCR and v not RCRMASK 235 end 236 237 note r8127_n.c:4296-4321: gate RX, stop request, wait for both FIFOs and the mitigation idle bits, clear the stop request, soft reset, reset RCR. 238 note The vendor ignores a reset that never completes; this driver fails with E_RSTTMO instead. 239 fn rtlnicrst to ulong 240 rtlrxoff 241 v = rtlr8 RXDVGATE 242 rtlw8 RXDVGATE or v 8 243 v = rtlr8 CHIPCMD 244 rtlw8 CHIPCMD or v CMDSTOP 245 rtldly 200 246 i = ulong 0 247 while lt i 3000 bound 3000 248 rtldly 50 249 v = rtlr8 MCUCMD 250 w = or MCUTXE MCURXE 251 if eq and v w w 252 break 253 end 254 i = add i 1 255 end 256 if eq i 3000 257 rtlwto 258 end 259 i = ulong 0 260 while lt i 3000 bound 3000 261 rtldly 50 262 v = rtlr16 INTRMIT 263 if eq and v 259 259 264 break 265 end 266 i = add i 1 267 end 268 if eq i 3000 269 rtlwto 270 end 271 v = rtlr8 CHIPCMD 272 rtlw8 CHIPCMD and v or CMDTXEN CMDRXEN 273 rtlw8 CHIPCMD CMDRST 274 i = ulong 0 275 while lt i 100 bound 100 276 rtldly 100 277 v = rtlr8 CHIPCMD 278 if eq and v CMDRST 0 279 break 280 end 281 i = add i 1 282 end 283 if eq i 100 284 ret E_RSTTMO 285 end 286 rtlw32 RCR RCRRST 287 ret 0 288 end 289 290 note r8127_n.c:4507-4519 with interrupt version 1 (no MSI-X). 291 fn rtlhwrst to ulong 292 v = rtlr8 INTCFG0 293 rtlw8 INTCFG0 and v not ulong 97 294 rtlw32 IMR0 0 295 v = rtlr32 ISR0 296 rtlw32 ISR0 v 297 rtlw32 TIMINT0 0 298 rtlw32 TIMINT1 0 299 rtlw32 TIMINT2 0 300 rtlw32 TIMINT3 0 301 ret rtlnicrst 302 end 303 304 note r8127_n.c:7340-7384 without DASH and RealWoW. The UPS-resume PHY wait is not done, only recorded (UNKNOWN on the chip). 305 fn rtlexoob to ulong 306 rtlrxoff 307 rtlocwr 49340 255 308 e = rtlnicrst 309 if ne e 0 310 ret e 311 end 312 v = rtlr8 MCUCMD 313 rtlw8 MCUCMD and v not MCUOOB 314 v = rtlocrd 59614 315 rtlocwr 59614 and v not ulong 16384 316 rtlwaitll 317 rtlocwr 49322 2000 318 rtlocwr 49318 437 319 rtlocwr 49182 21845 320 rtlwaitll 321 v = rtlocrd 54316 322 if ne and v 256 0 323 put rs S_UPSRES 1 324 else 325 put rs S_UPSRES 0 326 end 327 ret 0 328 end 329 330 note r8127_n.c:5512-5519 and 13306-13318: PLL on, PHY out of power-down with autonegotiation, unless the board holds the PHY in disable mode (4029-4072). 331 note PHY OCP 0xA400 (41984) is BMCR, 0xA420 (42016) holds the PHY state in its low 3 bits (3 = up), MAC OCP 0xDC04 (56324) bit 13 is the disable-mode flag. 332 fn rtlpwrup to void 333 v = rtlr8 PMCH 334 rtlw8 PMCH or v 192 335 g = rtlr8 RXDVGATE 336 d = rtlocrd 56324 337 if ne and g 32 0 338 if eq and d 8192 0 339 ret 340 end 341 end 342 rtlphwr 41984 4096 343 i = ulong 0 344 while lt i 100 bound 100 345 s = rtlphrd 42016 346 s = and s 7 347 put rs S_PHYST s 348 if eq s 3 349 ret 350 end 351 rtldly 1000 352 i = add i 1 353 end 354 rtlwto 355 end 356 357 note 1 when the six bytes are a usable unicast address. 358 fn rtlmacok m view uchar 6 to ulong 359 if ne and ulong get m 0 1 0 360 ret 0 361 end 362 z = ulong 0 363 for i ulong 6 364 z = or z ulong get m i 365 end 366 if eq z 0 367 ret 0 368 end 369 ret 1 370 end 371 372 note r8127_n.c:12418-12441 and 12482-12500 (rar_set): backup address, else MAC0, else a locally administered fallback. 373 fn rtlrdmac to void 374 m = new uchar 6 0 375 lo = rtlr32 BAKADR0 376 hi = rtlr16 BAKADR1 377 for i ulong 4 378 put m i uchar and shr lo mul i 8 255 379 end 380 put m 4 uchar and hi 255 381 put m 5 uchar and shr hi 8 255 382 src = MS_BACK 383 if eq rtlmacok m 0 384 for i ulong 6 385 put m i uchar rtlr8 add MAC0 i 386 end 387 src = MS_MAC0 388 end 389 if eq rtlmacok m 0 390 put m 0 2 391 put m 1 65 392 put m 2 73 393 put m 3 69 394 put m 4 78 395 put m 5 88 396 src = MS_FALL 397 end 398 put rs S_MACSRC src 399 for i ulong 6 400 put rs add S_MAC0 i ulong get m i 401 end 402 v = rtlr8 CFG9346 403 rtlw8 CFG9346 or v UNLOCK 404 a = or or ulong get m 0 shl ulong get m 1 8 shl ulong get m 2 16 405 rtlw32 MAC0 or a shl ulong get m 3 24 406 b = or ulong get m 4 shl ulong get m 5 8 407 rtlw32 MAC4 b 408 v = rtlr8 CFG9346 409 rtlw8 CFG9346 and v not UNLOCK 410 end 411 412 note ---- descriptors: little-endian words in rdma, opts1 written last ---- 413 fn rtlrxpost i ulong to void 414 a = add add get rs S_DMA OFFRXB mul i STRIDE 415 d = add OFFRXD mul i 16 416 dst32 add d 4 0 417 dst32 add d 8 and a `4294967295` 418 dst32 add d 12 shr a 32 419 rtlbar 420 f = ulong 0 421 if eq i sub RXN 1 422 f = DEOR 423 end 424 dst32 d or or DOWN f RXBUF 425 end 426 427 note Identify, stop, reset and bring up the chip: exit OOB, reset, PHY power up, MAC address, rings, TX and RX enabled, interrupts masked. 428 note dma is the bus address the chip sees for rdma (4096-aligned, dma + MEMSIZE must not wrap). Returns 0 or an error code. 429 fn rtlinit dma ulong to ulong 430 for i ulong 32 431 put rs i 0 432 end 433 if ne and dma 4095 0 434 ret E_MEM 435 end 436 if lt add dma MEMSIZE dma 437 ret E_MEM 438 end 439 put rs S_DMA dma 440 for i ulong MEMSIZE 441 put rdma i 0 442 end 443 tcr = rtlr32 TCR 444 put rs S_TCRID tcr 445 if eq tcr `4294967295` 446 ret E_ABSENT 447 end 448 if ne and tcr TCRIDM TCRID 449 ret E_CHIP 450 end 451 ic = and tcr TCRICM 452 if eq ic 0 453 put rs S_MCFG 1 454 else 455 put rs S_MCFG 2 456 end 457 if and ne ic 0 ne ic `1048576` 458 put rs S_ICUNK 1 459 end 460 e = rtlexoob 461 if ne e 0 462 ret e 463 end 464 rtlpwrup 465 e = rtlhwrst 466 if ne e 0 467 ret e 468 end 469 rtlrdmac 470 for i ulong TXN 471 f = ulong 0 472 if eq i sub TXN 1 473 f = DEOR 474 end 475 dst32 mul i 32 f 476 end 477 for i ulong RXN 478 rtlrxpost i 479 end 480 rtlbar 481 v = rtlr8 CFG9346 482 rtlw8 CFG9346 or v UNLOCK 483 tx = add dma OFFTXD 484 rx = add dma OFFRXD 485 rtlw32 TNPDSLO and tx `4294967295` 486 rtlw32 TNPDSHI shr tx 32 487 rtlw32 RDSARLO and rx `4294967295` 488 rtlw32 RDSARHI shr rx 32 489 rtlw32 TCR TCRVAL 490 rtlw32 RSSCTRL 0 491 v = rtlr16 QNUMCTL 492 rtlw16 QNUMCTL and v not ulong 28 493 v = rtlocrd OCPFMT 494 rtlocwr OCPFMT or and v not ulong 3 1 495 v = rtlr8 TXNOCLO 496 rtlw8 TXNOCLO or v 4 497 v = rtlr8 RXDTYPE 498 rtlw8 RXDTYPE and v not ulong 2 499 rtlw16 RMS or RMSVAL RMSVLAN 500 v = rtlr8 RXDVGATE 501 rtlw8 RXDVGATE and v not ulong 8 502 rtlw32 RCR or or RCRCHIP RCRBCST RCRMYPH 503 v = rtlr8 CFG9346 504 rtlw8 CFG9346 and v not UNLOCK 505 v = rtlr32 HWCLO0 506 put rs S_CLOB and v CLOMASK 507 rtlw8 CHIPCMD or CMDTXEN CMDRXEN 508 put rs S_READY 1 509 ret 0 510 end 511 512 note PHYstatus (r8127_n.c:4537-4547): 1 when the link is up. rs[S_MBPS] gets 10000/5000/2500/1000/100/10, or 0 when down or unknown. 513 fn rtllink to ulong 514 put rs S_MBPS 0 515 s = rtlr16 PHYSTAT 516 if eq s 65535 517 ret 0 518 end 519 if eq and s PHYSLNK 0 520 ret 0 521 end 522 m = ulong 0 523 if ne and s 16384 0 524 m = 10000 525 else 526 if ne and s 4096 0 527 m = 5000 528 else 529 if ne and s 1024 0 530 m = 2500 531 else 532 if ne and s 16 0 533 m = 1000 534 else 535 if ne and s 8 0 536 m = 100 537 else 538 if ne and s 4 0 539 m = 10 540 end 541 end 542 end 543 end 544 end 545 end 546 put rs S_MBPS m 547 ret 1 548 end 549 550 fn rtlusable to ulong 551 if ne get rs S_BROKE 0 552 ret E_BROKEN 553 end 554 if eq get rs S_READY 0 555 ret E_NOTRDY 556 end 557 ret 0 558 end 559 560 fn rtltxpend to ulong 561 ret and sub get rs S_TXCUR get rs S_TXDIR `4294967295` 562 end 563 564 note Send fbuf[0..n): 14..1514 bytes, no FCS, padded to 60. One descriptor, then the tail pointer (r8127_n.c:16427-16560 and the no-close doorbell 16412-16419). 565 fn rtltx n ulong to ulong 566 e = rtlusable 567 if ne e 0 568 ret e 569 end 570 if lt n 14 571 ret E_ARG 572 end 573 if gt n FMAX 574 ret E_ARG 575 end 576 if ge rtltxpend TXN 577 ret E_FULL 578 end 579 cur = get rs S_TXCUR 580 i = rem cur TXN 581 sz = n 582 if lt n FMIN 583 sz = FMIN 584 end 585 b = add OFFTXB mul i STRIDE 586 for k ulong sz 587 if lt k n 588 put rdma add b k get fbuf k 589 else 590 put rdma add b k 0 591 end 592 end 593 a = add add get rs S_DMA OFFTXB mul i STRIDE 594 d = mul i 32 595 dst32 add d 4 0 596 dst32 add d 8 and a `4294967295` 597 dst32 add d 12 shr a 32 598 rtlbar 599 f = ulong 0 600 if eq i sub TXN 1 601 f = DEOR 602 end 603 dst32 d or or or DOWN DFS DLS or f sz 604 cur = and add cur 1 `4294967295` 605 put rs S_TXCUR cur 606 rtlw32 SWTAIL0 and add get rs S_CLOB cur CLOMASK 607 ret 0 608 end 609 610 note TX completions: the close pointer's advance (r8127_n.c:16590-16620). rs[S_RECL] gets the count. A pointer that runs ahead of what was posted marks the device broken. 611 fn rtltxrec to ulong 612 put rs S_RECL 0 613 e = rtlusable 614 if ne e 0 615 ret e 616 end 617 clo = rtlr32 HWCLO0 618 done = and sub sub clo get rs S_CLOB get rs S_TXDIR CLOMASK 619 if eq clo `4294967295` 620 put rs S_BROKE 1 621 ret E_DEVICE 622 end 623 if gt done rtltxpend 624 put rs S_BROKE 1 625 ret E_DEVICE 626 end 627 put rs S_TXDIR and add get rs S_TXDIR done `4294967295` 628 put rs S_RECL done 629 ret 0 630 end 631 632 note Copy f bytes of RX buffer i to obuf and publish the length. 633 fn rxcopy i ulong f ulong to void 634 b = add OFFRXB mul i STRIDE 635 for k ulong f 636 put obuf k get rdma add b k 637 end 638 put rs S_RXLEN f 639 end 640 fn rxdrop to void 641 put rs S_RXDRP add get rs S_RXDRP 1 642 end 643 644 note Handle one RX slot. 0: nothing pending (the chip still owns it). 1: a frame was delivered. 2: a frame was dropped (error, runt or 645 note multi-descriptor; slot reposted). 3: the chip reported a length past the buffer, so the device is marked broken. 646 fn rxone to ulong 647 i = rem get rs S_RXCUR RXN 648 s = dld32 add OFFRXD mul i 16 649 if ne and s DOWN 0 650 ret 0 651 end 652 rtlbar 653 n = and s RXLEN 654 if gt n RXBUF 655 put rs S_BROKE 1 656 ret 3 657 end 658 w = or DFS DLS 659 good = and and eq and s w w eq and s RXRES 0 ge n 18 660 r = ulong 2 661 if good 662 rxcopy i sub n 4 663 r = 1 664 else 665 rxdrop 666 end 667 rtlrxpost i 668 put rs S_RXCUR and add get rs S_RXCUR 1 `4294967295` 669 ret r 670 end 671 672 note Take at most one received frame into obuf (FCS stripped); rs[S_RXLEN] is its length, 0 when none is pending. r8127_n.c:17036-17250. 673 fn rtlrx to ulong 674 put rs S_RXLEN 0 675 e = rtlusable 676 if ne e 0 677 ret e 678 end 679 t = ulong 0 680 while lt t RXN bound 16 681 r = rxone 682 if eq r 0 683 ret 0 684 end 685 if eq r 3 686 ret E_DEVICE 687 end 688 if eq r 1 689 ret 0 690 end 691 t = add t 1 692 end 693 ret 0 694 end 695 696 note Stop DMA only on a chip rtlinit identified (E_NOTRDY otherwise, nothing written): mask interrupts, close the RX gate, stop request, wait for the FIFOs, soft reset. 697 fn rtlstop to ulong 698 if eq get rs S_MCFG 0 699 ret E_NOTRDY 700 end 701 put rs S_READY 0 702 ret rtlhwrst 703 end