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/aienos_rtl8127.h # 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 / aienos_rtl8127.h` 214 lines ยท 10546 bytes Raw Blame History 1 /* Realtek RTL8127 10 GbE (PCI 10ec:8127, the Spark's only wired NIC) driver 2 * core, polled, hosted-testable. Gap D5 of docs/SPARK_REPLACE_LINUX.md. 3 * 4 * DOCS SILENT: Realtek publishes no register document for this chip (see 5 * docs/SPARK_RTL8127.md). Every offset and bit below comes from Realtek's own 6 * Linux driver r8127 v11.014.00, the version the Spark runs, read-only at 7 * github.com/openwrt/rtl8127 `ec2f285`; comments cite r8127_n.c / r8127.h 8 * lines there. None of it has been run on the chip. The register sequences 9 * still UNKNOWN on the real chip are listed in docs/SPARK_RTL8127.md. 10 * 11 * Scope: MAC bring-up without the vendor's MCU patch code, PCIe EPHY tuning 12 * and PHY micro-code (Aienux relies on the state the UEFI firmware leaves), 13 * one TX queue in the vendor's "no close" mode (tail and close pointers, the 14 * vendor build default), one RX queue with type 1 descriptors (RSS off, as 15 * in the Spark's build), interrupts masked, no offloads, no jumbo frames. 16 * 17 * Device access goes through rtl_ops only: register reads and writes inside 18 * BAR 2, a microsecond delay, and a memory barrier. No heap, no clock, no 19 * I/O of its own. Both rings and every packet buffer live in ONE 20 * caller-provided region (mem, len) that the chip sees at bus address `dma` 21 * (the kernel makes it the device's only SMMU window). The driver writes 22 * every descriptor from its own index (never trusts a descriptor address the 23 * chip wrote back). Anything the chip reports is untrusted: a close pointer 24 * that runs ahead of what was posted, or an RX length larger than the 25 * buffer, marks the device broken (sticky); the caller must stop and 26 * re-init. */ 27 #ifndef AIENOS_RTL8127_H 28 #define AIENOS_RTL8127_H 29 30 #include 31 #include 32 33 #define RTL_PCI_VENDOR 0x10ecu 34 #define RTL_PCI_DEVICE 0x8127u 35 #define RTL_MMIO_BAR 2u /* r8127_n.c init_board: BAR 2 is the MMIO region */ 36 #define RTL_MMIO_MIN 0x5000u /* highest register used is 0x4800 (Q_NUM_CTRL); Spark BAR 2 is 256 KiB */ 37 38 /* Registers (offsets in BAR 2) */ 39 #define RTL_MAC0 0x00u /* r8127.h:1438 */ 40 #define RTL_MAC4 0x04u 41 #define RTL_TNPDS_LO 0x20u /* TxDescStartAddrLow, r8127.h:1444 */ 42 #define RTL_TNPDS_HI 0x24u 43 #define RTL_INT_CFG0 0x34u /* INT_CFG0_8125: bit 0 enable, 5 auto-clear IMR, 6 avoid-miss */ 44 #define RTL_CHIPCMD 0x37u /* r8127.h:1452 */ 45 #define RTL_IMR0 0x38u /* IMR0_8125 (32 bit, ISR version 1) */ 46 #define RTL_ISR0 0x3Cu /* ISR0_8125 */ 47 #define RTL_TCR 0x40u /* TxConfig */ 48 #define RTL_RCR 0x44u /* RxConfig */ 49 #define RTL_CFG9346 0x50u 50 #define RTL_TIMER_INT0 0x58u /* r8127_n.c:4360-4368 */ 51 #define RTL_TIMER_INT1 0x5Cu 52 #define RTL_PHYSTATUS 0x6Cu /* 16 bit */ 53 #define RTL_PMCH 0x6Fu 54 #define RTL_TIMER_INT2 0x8Cu 55 #define RTL_MACOCP 0xB0u /* MAC OCP window, r8127_n.c:3287-3314 */ 56 #define RTL_PHYOCP 0xB8u /* PHY OCP window, r8127_n.c:3057-3073 */ 57 #define RTL_LLFIFO 0xD2u /* 16 bit, bit 9 = LL share FIFO ready */ 58 #define RTL_MCUCMD 0xD3u 59 #define RTL_RXDESC_TYPE 0xD8u /* bit 1 = RX descriptor type 4 */ 60 #define RTL_RMS 0xDAu /* RxMaxSize, 16 bit */ 61 #define RTL_INTRMIT 0xE2u /* IntrMitigate, 16 bit */ 62 #define RTL_RDSAR_LO 0xE4u /* RxDescAddrLow */ 63 #define RTL_RDSAR_HI 0xE8u 64 #define RTL_RXDV_GATE 0xF2u /* bit 3 = gate closed */ 65 #define RTL_TIMER_INT3 0xF4u 66 #define RTL_SW_TAIL0 0x0D30u /* SW_TAIL_PTR0_8125BP, no-close version 6 */ 67 #define RTL_HW_CLO0 0x0D34u /* HW_CLO_PTR0_8125BP */ 68 #define RTL_BACKUP_ADDR0 0x19E0u /* 32 bit, the MAC address Linux uses */ 69 #define RTL_BACKUP_ADDR1 0x19E4u /* 16 bit */ 70 #define RTL_TXNOCLOSE_EN 0x20E4u /* bit 2 */ 71 #define RTL_RSS_CTRL 0x4500u /* RSS_CTRL_8125, r8127.h:1553 */ 72 #define RTL_Q_NUM_CTRL 0x4800u /* Q_NUM_CTRL_8125, r8127.h:1554; bits 4:2 = ilog2(RX queues) */ 73 #define RTL_OCP_TXDESC_FMT 0xEB58u /* MAC OCP, bits 1:0 = 01 "new tx desc format" (32 B), r8127_n.c:14992 */ 74 75 /* Bits */ 76 #define RTL_CMD_STOPREQ 0x80u 77 #define RTL_CMD_RESET 0x10u 78 #define RTL_CMD_RXEN 0x08u 79 #define RTL_CMD_TXEN 0x04u 80 #define RTL_CFG9346_UNLOCK 0xC0u 81 #define RTL_RCR_ACCEPT_MASK 0x3Fu /* Err Runt Bcast Mcast MyPhys AllPhys */ 82 #define RTL_RCR_ACCEPT_BCAST 0x08u 83 #define RTL_RCR_ACCEPT_MYPHYS 0x02u 84 /* Chip table RCR for RTL8127 (r8127_n.c:119-122): Rx_Fetch_Number_8 | 85 * Rx_Close_Multiple | RxCfg_pause_slot_en | EnableInnerVlan | 86 * EnableOuterVlan | RX_DMA_BURST_512 << 8 */ 87 #define RTL_RCR_CHIP 0x40E00D00u 88 #define RTL_RCR_RESET 0x00000500u /* RX_DMA_BURST_512 << 8, r8127_n.c:4320-4321 */ 89 /* TX_DMA_BURST_unlimited << 8 | InterFrameGap 3 << 24, plus bit 6 no-close */ 90 #define RTL_TCR_VALUE 0x03000740u 91 #define RTL_TCR_ID_MASK 0x7c800000u 92 #define RTL_TCR_ID_8127 0x6C800000u 93 #define RTL_TCR_ICVER_MASK 0x00700000u 94 #define RTL_MCU_NOW_IS_OOB 0x80u 95 #define RTL_MCU_TXFIFO_EMPTY 0x20u 96 #define RTL_MCU_RXFIFO_EMPTY 0x10u 97 #define RTL_OCP_WRITE 0x80000000u 98 #define RTL_OCP_FLAG 0x80000000u 99 #define RTL_PHYS_LINK 0x0002u 100 #define RTL_PHYS_FULLDUP 0x0001u 101 #define RTL_RMS_VLAN 0x8000u /* AcceppVlanPhys */ 102 103 /* Descriptors (little-endian in memory) */ 104 #define RTL_DESC_OWN 0x80000000u 105 #define RTL_DESC_EOR 0x40000000u /* RingEnd */ 106 #define RTL_DESC_FS 0x20000000u 107 #define RTL_DESC_LS 0x10000000u 108 #define RTL_RX_RES 0x00200000u /* receive error summary (type 1) */ 109 #define RTL_RX_LEN_MASK 0x3FFFu /* includes the 4-byte FCS */ 110 #define RTL_TXD_SIZE 32u /* struct TxDesc, r8127.h:2056 */ 111 #define RTL_RXD_SIZE 16u /* struct RxDesc (type 1), r8127.h:2066 */ 112 #define RTL_CLO_MASK 0x7FFFFFFFu /* MAX_TX_NO_CLOSE_DESC_PTR_MASK_V4 */ 113 114 /* Ring geometry used by this driver */ 115 #define RTL_TX_N 16u 116 #define RTL_RX_N 16u 117 #define RTL_FRAME_MIN 60u /* frames shorter than this are zero-padded */ 118 #define RTL_FRAME_MAX 1514u /* Ethernet II without FCS, no VLAN tag */ 119 #define RTL_RX_BUF 1536u /* posted RX buffer size */ 120 #define RTL_RMS_VALUE 1522u /* 1514 + 4 VLAN + 4 FCS */ 121 #define RTL_BUF_STRIDE 2048u 122 #define RTL_OFF_TXD 0u 123 #define RTL_OFF_RXD 1024u 124 #define RTL_OFF_TXBUF 4096u 125 #define RTL_OFF_RXBUF (RTL_OFF_TXBUF + RTL_TX_N * RTL_BUF_STRIDE) 126 #define RTL_MEM_SIZE (RTL_OFF_RXBUF + RTL_RX_N * RTL_BUF_STRIDE) 127 128 typedef struct { 129 void *ctx; 130 /* width is 1, 2 or 4; off + width <= RTL_MMIO_MIN */ 131 uint32_t (*read)(void *ctx, uint32_t off, unsigned width); 132 /* must make every earlier write to the shared region visible to the 133 * device before the register write (the kernel issues a barrier first) */ 134 void (*write)(void *ctx, uint32_t off, unsigned width, uint32_t val); 135 void (*delay_us)(void *ctx, uint32_t us); 136 /* full memory barrier between descriptor reads and buffer reads */ 137 void (*barrier)(void *ctx); 138 } rtl_ops; 139 140 typedef enum { 141 RTL_OK = 0, 142 RTL_E_ARG = 1, /* NULL, frame too short/long, buffer too small */ 143 RTL_E_MEM = 2, /* region too small, misaligned, or bus range wraps */ 144 RTL_E_ABSENT = 3, /* registers read all ones: no device behind the BAR */ 145 RTL_E_CHIP = 4, /* TxConfig id is not an RTL8127 */ 146 RTL_E_RESET_TIMEOUT = 5, /* ChipCmd reset bit never cleared */ 147 RTL_E_FULL = 6, /* no free TX descriptor (reclaim first) */ 148 RTL_E_DEVICE = 7, /* hostile or broken completion; device is now broken */ 149 RTL_E_BROKEN = 8, /* sticky: an earlier RTL_E_DEVICE; stop and re-init */ 150 RTL_E_NOT_READY = 9, /* rtl_init has not succeeded */ 151 } rtl_err; 152 153 typedef enum { RTL_MAC_BACKUP = 1, RTL_MAC_MAC0 = 2, RTL_MAC_FALLBACK = 3 } rtl_mac_source; 154 155 typedef struct { 156 rtl_ops ops; 157 uint8_t *mem; 158 uint64_t dma; 159 size_t mem_len; 160 uint8_t ready; 161 uint8_t broken; 162 uint8_t mcfg; /* 1 or 2 (CFG_METHOD_n), from TxConfig */ 163 uint8_t ic_unknown; /* TxConfig IC version neither 0 nor 1 */ 164 uint8_t mac[6]; 165 uint8_t mac_source; /* rtl_mac_source */ 166 uint8_t ups_resume; /* MAC OCP 0xD42C bit 8 was set (not handled) */ 167 uint8_t phy_state; /* PHY OCP 0xA420 & 7 after power-up (3 = up) */ 168 uint32_t tcr_id; /* TxConfig as read before reset */ 169 uint32_t wait_timeouts; /* vendor waits that ran out (the vendor ignores them too) */ 170 uint32_t tx_cur; /* frames posted (free running) */ 171 uint32_t tx_dirty; /* frames known closed (free running) */ 172 uint32_t clo_base; /* hardware close pointer read after reset */ 173 uint32_t rx_cur; /* frames consumed (free running) */ 174 uint32_t rx_dropped; /* error, runt or multi-descriptor frames dropped */ 175 } rtl_dev; 176 177 /* Bytes of shared region the driver needs (RTL_MEM_SIZE). */ 178 size_t rtl_mem_size(void); 179 180 /* Identify, stop, reset and bring up the chip: exit OOB, reset, PHY power 181 * up, MAC address, rings, TX and RX enabled, interrupts masked. mem and dma 182 * must be 4096-byte aligned, mem_len >= rtl_mem_size(), dma + mem_len must 183 * not wrap. The region is zeroed. */ 184 rtl_err rtl_init(rtl_dev *d, const rtl_ops *ops, void *mem, uint64_t dma, size_t mem_len); 185 186 /* Link state from PHYstatus: 1 up, 0 down or absent. *mbps (optional) gets 187 * 10000/5000/2500/1000/100/10, or 0 when down or unknown. */ 188 int rtl_link(rtl_dev *d, uint32_t *mbps); 189 190 /* Copy one Ethernet frame (14..RTL_FRAME_MAX bytes, no FCS; padded to 60) 191 * into a free TX buffer, hand the descriptor to the chip, move the tail. */ 192 rtl_err rtl_tx(rtl_dev *d, const uint8_t *frame, size_t len); 193 194 /* Consume TX completions from the close pointer; *reclaimed (optional). */ 195 rtl_err rtl_tx_reclaim(rtl_dev *d, unsigned *reclaimed); 196 197 /* Frames posted and not yet closed. */ 198 unsigned rtl_tx_pending(const rtl_dev *d); 199 200 /* Take at most one received frame: copies it (FCS stripped) to out, sets 201 * *len and *got = 1, and gives the buffer back to the chip. *got 0: nothing 202 * pending. Error, runt, multi-descriptor and larger-than-cap frames are 203 * dropped (counted in rx_dropped) and their slot is reposted. A cap of 204 * RTL_RX_BUF holds every frame the chip can deliver (VLAN tag included). */ 205 rtl_err rtl_rx(rtl_dev *d, uint8_t *out, size_t cap, size_t *len, int *got); 206 207 /* Stop DMA (only on a chip rtl_init identified; RTL_E_NOT_READY otherwise, 208 * nothing written): mask interrupts, close the RX gate, stop request, wait for the 209 * FIFOs, soft reset. The rings are no longer in use when it returns OK. */ 210 rtl_err rtl_stop(rtl_dev *d); 211 212 const char *rtl_err_name(rtl_err e); 213 214 #endif