aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / net / aienos_rtl8127.c
| 1 | /* RTL8127 driver core (see aienos_rtl8127.h). Freestanding: no heap, no |
| 2 | * clock, no libc beyond memcpy/memset. Line numbers in comments refer to |
| 3 | * Realtek r8127 v11.014.00 (github.com/openwrt/rtl8127 ec2f285), the only |
| 4 | * reference: Realtek publishes no register document (DOCS SILENT). */ |
| 5 | #include "aienos_rtl8127.h" |
| 6 | |
| 7 | #include <string.h> |
| 8 | |
| 9 | #define WAIT_LL_TRIES 10u /* x100 us, r8127_n.c:4872-4882 */ |
| 10 | #define WAIT_FIFO_TRIES 3000u /* x50 us, r8127_n.c:4100-4116 */ |
| 11 | #define WAIT_RESET_TRIES 100u /* x100 us, r8127_n.c:4312-4318 */ |
| 12 | #define WAIT_OCP_TRIES 20000u /* x1 us, R8127_CHANNEL_WAIT_COUNT */ |
| 13 | #define WAIT_PHY_STATE_TRIES 100u /* x1 ms, r8127_n.c:7308-7318 */ |
| 14 | #define PHY_BMCR 0xA400u /* MII BMCR via PHY OCP: page 0xA40, reg 0x10 */ |
| 15 | #define PHY_STATE 0xA420u /* low 3 bits: 3 = powered up (r8127_n.c:7300-7304) */ |
| 16 | #define BMCR_ANENABLE 0x1000u /* IEEE 802.3 22.2.4.1.4 */ |
| 17 | |
| 18 | static const uint8_t fallback_mac[6] = {0x02, 0x41, 0x49, 0x45, 0x4e, 0x58}; /* locally administered */ |
| 19 | |
| 20 | static uint32_t r8(rtl_dev *d, uint32_t o) { return d->ops.read(d->ops.ctx, o, 1); } |
| 21 | static uint32_t r16(rtl_dev *d, uint32_t o) { return d->ops.read(d->ops.ctx, o, 2); } |
| 22 | static uint32_t r32(rtl_dev *d, uint32_t o) { return d->ops.read(d->ops.ctx, o, 4); } |
| 23 | static void w8(rtl_dev *d, uint32_t o, uint32_t v) { d->ops.write(d->ops.ctx, o, 1, v & 0xffu); } |
| 24 | static void w16(rtl_dev *d, uint32_t o, uint32_t v) { d->ops.write(d->ops.ctx, o, 2, v & 0xffffu); } |
| 25 | static void w32(rtl_dev *d, uint32_t o, uint32_t v) { d->ops.write(d->ops.ctx, o, 4, v); } |
| 26 | static void delay(rtl_dev *d, uint32_t us) { d->ops.delay_us(d->ops.ctx, us); } |
| 27 | |
| 28 | /* MAC OCP: r8127_n.c:3287-3314 */ |
| 29 | static uint32_t ocp_rd(rtl_dev *d, uint32_t addr) |
| 30 | { |
| 31 | w32(d, RTL_MACOCP, (addr / 2u) << 16); |
| 32 | return r32(d, RTL_MACOCP) & 0xffffu; |
| 33 | } |
| 34 | static void ocp_wr(rtl_dev *d, uint32_t addr, uint32_t v) |
| 35 | { |
| 36 | w32(d, RTL_MACOCP, ((addr / 2u) << 16) | (v & 0xffffu) | RTL_OCP_WRITE); |
| 37 | } |
| 38 | |
| 39 | /* PHY OCP: r8127_n.c:3057-3073 (write waits for the flag to clear), 3155-3167 |
| 40 | * (read waits for the flag to set). */ |
| 41 | static void phy_wr(rtl_dev *d, uint32_t addr, uint32_t v) |
| 42 | { |
| 43 | w32(d, RTL_PHYOCP, ((addr / 2u) << 16) | (v & 0xffffu) | RTL_OCP_WRITE); |
| 44 | for (uint32_t i = 0; i < WAIT_OCP_TRIES; i++) { |
| 45 | delay(d, 1); |
| 46 | if (!(r32(d, RTL_PHYOCP) & RTL_OCP_FLAG)) |
| 47 | return; |
| 48 | } |
| 49 | d->wait_timeouts++; |
| 50 | } |
| 51 | static uint32_t phy_rd(rtl_dev *d, uint32_t addr) |
| 52 | { |
| 53 | w32(d, RTL_PHYOCP, (addr / 2u) << 16); |
| 54 | for (uint32_t i = 0; i < WAIT_OCP_TRIES; i++) { |
| 55 | delay(d, 1); |
| 56 | if (r32(d, RTL_PHYOCP) & RTL_OCP_FLAG) |
| 57 | return r32(d, RTL_PHYOCP) & 0xffffu; |
| 58 | } |
| 59 | d->wait_timeouts++; |
| 60 | return 0xffffu; |
| 61 | } |
| 62 | |
| 63 | static void wait_ll_share_fifo(rtl_dev *d) /* r8127_n.c:4872-4882 */ |
| 64 | { |
| 65 | for (uint32_t i = 0; i < WAIT_LL_TRIES; i++) { |
| 66 | delay(d, 100); |
| 67 | if (r16(d, RTL_LLFIFO) & 0x0200u) |
| 68 | return; |
| 69 | } |
| 70 | d->wait_timeouts++; |
| 71 | } |
| 72 | |
| 73 | static void rx_filter_off(rtl_dev *d) /* r8127_n.c:4287-4293 */ |
| 74 | { |
| 75 | w32(d, RTL_RCR, r32(d, RTL_RCR) & ~RTL_RCR_ACCEPT_MASK); |
| 76 | } |
| 77 | |
| 78 | /* r8127_n.c:4296-4321: gate RX, stop request, wait for both FIFOs and the |
| 79 | * mitigation idle bits, clear the stop request, soft reset, reset RCR. The |
| 80 | * vendor ignores a reset that never completes; this driver fails instead. */ |
| 81 | static rtl_err nic_reset(rtl_dev *d) |
| 82 | { |
| 83 | rx_filter_off(d); |
| 84 | w8(d, RTL_RXDV_GATE, r8(d, RTL_RXDV_GATE) | 0x08u); |
| 85 | w8(d, RTL_CHIPCMD, r8(d, RTL_CHIPCMD) | RTL_CMD_STOPREQ); |
| 86 | delay(d, 200); |
| 87 | uint32_t i; |
| 88 | for (i = 0; i < WAIT_FIFO_TRIES; i++) { |
| 89 | delay(d, 50); |
| 90 | uint32_t want = RTL_MCU_TXFIFO_EMPTY | RTL_MCU_RXFIFO_EMPTY; |
| 91 | if ((r8(d, RTL_MCUCMD) & want) == want) |
| 92 | break; |
| 93 | } |
| 94 | if (i == WAIT_FIFO_TRIES) |
| 95 | d->wait_timeouts++; |
| 96 | for (i = 0; i < WAIT_FIFO_TRIES; i++) { |
| 97 | delay(d, 50); |
| 98 | if ((r16(d, RTL_INTRMIT) & 0x0103u) == 0x0103u) |
| 99 | break; |
| 100 | } |
| 101 | if (i == WAIT_FIFO_TRIES) |
| 102 | d->wait_timeouts++; |
| 103 | w8(d, RTL_CHIPCMD, r8(d, RTL_CHIPCMD) & (RTL_CMD_TXEN | RTL_CMD_RXEN)); |
| 104 | w8(d, RTL_CHIPCMD, RTL_CMD_RESET); |
| 105 | for (i = 0; i < WAIT_RESET_TRIES; i++) { |
| 106 | delay(d, 100); |
| 107 | if (!(r8(d, RTL_CHIPCMD) & RTL_CMD_RESET)) |
| 108 | break; |
| 109 | } |
| 110 | if (i == WAIT_RESET_TRIES) |
| 111 | return RTL_E_RESET_TIMEOUT; |
| 112 | w32(d, RTL_RCR, RTL_RCR_RESET); |
| 113 | return RTL_OK; |
| 114 | } |
| 115 | |
| 116 | /* r8127_n.c:4507-4519 with ISR version 1 (no MSI-X): interrupt type set to |
| 117 | * version 1 (4324-4350), mask, acknowledge, timers off, then nic_reset. */ |
| 118 | static rtl_err hw_reset(rtl_dev *d) |
| 119 | { |
| 120 | w8(d, RTL_INT_CFG0, r8(d, RTL_INT_CFG0) & ~0x61u); |
| 121 | w32(d, RTL_IMR0, 0); |
| 122 | w32(d, RTL_ISR0, r32(d, RTL_ISR0)); |
| 123 | w32(d, RTL_TIMER_INT0, 0); |
| 124 | w32(d, RTL_TIMER_INT1, 0); |
| 125 | w32(d, RTL_TIMER_INT2, 0); |
| 126 | w32(d, RTL_TIMER_INT3, 0); |
| 127 | return nic_reset(d); |
| 128 | } |
| 129 | |
| 130 | /* r8127_n.c:7340-7384 without DASH and RealWoW; the UPS-resume PHY wait is |
| 131 | * not done, only recorded (UNKNOWN on the chip). */ |
| 132 | static rtl_err exit_oob(rtl_dev *d) |
| 133 | { |
| 134 | rx_filter_off(d); |
| 135 | ocp_wr(d, 0xC0BC, 0x00FF); |
| 136 | rtl_err e = nic_reset(d); |
| 137 | if (e != RTL_OK) |
| 138 | return e; |
| 139 | w8(d, RTL_MCUCMD, r8(d, RTL_MCUCMD) & ~RTL_MCU_NOW_IS_OOB); |
| 140 | ocp_wr(d, 0xE8DE, ocp_rd(d, 0xE8DE) & ~0x4000u); |
| 141 | wait_ll_share_fifo(d); |
| 142 | ocp_wr(d, 0xC0AA, 0x07D0); |
| 143 | ocp_wr(d, 0xC0A6, 0x01B5); |
| 144 | ocp_wr(d, 0xC01E, 0x5555); |
| 145 | wait_ll_share_fifo(d); |
| 146 | d->ups_resume = (ocp_rd(d, 0xD42C) & 0x0100u) ? 1 : 0; |
| 147 | return RTL_OK; |
| 148 | } |
| 149 | |
| 150 | /* r8127_n.c:5512-5519 and 13306-13318: PLL on, PHY out of power-down with |
| 151 | * autonegotiation enabled, unless the board holds the PHY in disable mode |
| 152 | * (4029-4072: 0xF2 bit 5 and MAC OCP 0xDC04 bit 13 low). */ |
| 153 | static void powerup(rtl_dev *d) |
| 154 | { |
| 155 | w8(d, RTL_PMCH, r8(d, RTL_PMCH) | 0xC0u); |
| 156 | if ((r8(d, RTL_RXDV_GATE) & 0x20u) && !(ocp_rd(d, 0xDC04) & 0x2000u)) |
| 157 | return; |
| 158 | phy_wr(d, PHY_BMCR, BMCR_ANENABLE); |
| 159 | for (uint32_t i = 0; i < WAIT_PHY_STATE_TRIES; i++) { |
| 160 | d->phy_state = (uint8_t)(phy_rd(d, PHY_STATE) & 7u); |
| 161 | if (d->phy_state == 3) |
| 162 | return; |
| 163 | delay(d, 1000); |
| 164 | } |
| 165 | d->wait_timeouts++; |
| 166 | } |
| 167 | |
| 168 | static int mac_valid(const uint8_t m[6]) |
| 169 | { |
| 170 | if (m[0] & 1u) |
| 171 | return 0; /* multicast */ |
| 172 | return (m[0] | m[1] | m[2] | m[3] | m[4] | m[5]) != 0; |
| 173 | } |
| 174 | |
| 175 | /* r8127_n.c:12418-12441 and 12482-12500 (rar_set). */ |
| 176 | static void read_mac(rtl_dev *d) |
| 177 | { |
| 178 | uint8_t m[6]; |
| 179 | uint32_t lo = r32(d, RTL_BACKUP_ADDR0), hi = r16(d, RTL_BACKUP_ADDR1); |
| 180 | for (int i = 0; i < 4; i++) |
| 181 | m[i] = (uint8_t)(lo >> (8 * i)); |
| 182 | m[4] = (uint8_t)hi; |
| 183 | m[5] = (uint8_t)(hi >> 8); |
| 184 | d->mac_source = RTL_MAC_BACKUP; |
| 185 | if (!mac_valid(m)) { |
| 186 | for (uint32_t i = 0; i < 6; i++) |
| 187 | m[i] = (uint8_t)r8(d, RTL_MAC0 + i); |
| 188 | d->mac_source = RTL_MAC_MAC0; |
| 189 | } |
| 190 | if (!mac_valid(m)) { |
| 191 | memcpy(m, fallback_mac, 6); |
| 192 | d->mac_source = RTL_MAC_FALLBACK; |
| 193 | } |
| 194 | memcpy(d->mac, m, 6); |
| 195 | w8(d, RTL_CFG9346, r8(d, RTL_CFG9346) | RTL_CFG9346_UNLOCK); |
| 196 | w32(d, RTL_MAC0, (uint32_t)m[0] | (uint32_t)m[1] << 8 | (uint32_t)m[2] << 16 | (uint32_t)m[3] << 24); |
| 197 | w32(d, RTL_MAC4, (uint32_t)m[4] | (uint32_t)m[5] << 8); |
| 198 | w8(d, RTL_CFG9346, r8(d, RTL_CFG9346) & ~RTL_CFG9346_UNLOCK); |
| 199 | } |
| 200 | |
| 201 | /* Descriptor words are little-endian; opts1 is one 32-bit store so the OWN |
| 202 | * bit and the length change together. */ |
| 203 | static volatile uint32_t *word(rtl_dev *d, size_t off) { return (volatile uint32_t *)(void *)(d->mem + off); } |
| 204 | static uint32_t le(uint32_t v) |
| 205 | { |
| 206 | #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 207 | return __builtin_bswap32(v); |
| 208 | #else |
| 209 | return v; |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | static size_t txd(uint32_t i) { return RTL_OFF_TXD + (size_t)i * RTL_TXD_SIZE; } |
| 214 | static size_t rxd(uint32_t i) { return RTL_OFF_RXD + (size_t)i * RTL_RXD_SIZE; } |
| 215 | |
| 216 | /* Give RX slot i to the chip: buffer address from the slot index, then |
| 217 | * opts1 = OWN | EOR (last slot) | size (r8127_n.c:15263-15268). */ |
| 218 | static void rx_post(rtl_dev *d, uint32_t i) |
| 219 | { |
| 220 | uint64_t a = d->dma + RTL_OFF_RXBUF + (uint64_t)i * RTL_BUF_STRIDE; |
| 221 | *word(d, rxd(i) + 4) = 0; |
| 222 | *word(d, rxd(i) + 8) = le((uint32_t)a); |
| 223 | *word(d, rxd(i) + 12) = le((uint32_t)(a >> 32)); |
| 224 | d->ops.barrier(d->ops.ctx); |
| 225 | *word(d, rxd(i)) = le(RTL_DESC_OWN | (i == RTL_RX_N - 1 ? RTL_DESC_EOR : 0) | RTL_RX_BUF); |
| 226 | } |
| 227 | |
| 228 | size_t rtl_mem_size(void) { return RTL_MEM_SIZE; } |
| 229 | |
| 230 | rtl_err rtl_init(rtl_dev *d, const rtl_ops *ops, void *mem, uint64_t dma, size_t mem_len) |
| 231 | { |
| 232 | if (!d || !ops || !ops->read || !ops->write || !ops->delay_us || !ops->barrier || !mem) |
| 233 | return RTL_E_ARG; |
| 234 | memset(d, 0, sizeof *d); |
| 235 | d->ops = *ops; |
| 236 | if (mem_len < RTL_MEM_SIZE || ((uintptr_t)mem & 4095u) || (dma & 4095u) || dma + mem_len < dma) |
| 237 | return RTL_E_MEM; |
| 238 | d->mem = mem; |
| 239 | d->dma = dma; |
| 240 | d->mem_len = mem_len; |
| 241 | memset(mem, 0, RTL_MEM_SIZE); |
| 242 | |
| 243 | /* Identify (r8127_n.c:7210-7235) before writing anything. */ |
| 244 | uint32_t tcr = r32(d, RTL_TCR); |
| 245 | d->tcr_id = tcr; |
| 246 | if (tcr == 0xffffffffu) |
| 247 | return RTL_E_ABSENT; |
| 248 | if ((tcr & RTL_TCR_ID_MASK) != RTL_TCR_ID_8127) |
| 249 | return RTL_E_CHIP; |
| 250 | uint32_t icver = tcr & RTL_TCR_ICVER_MASK; |
| 251 | d->mcfg = icver == 0 ? 1 : 2; |
| 252 | d->ic_unknown = icver != 0 && icver != 0x00100000u; |
| 253 | |
| 254 | /* Probe order of r8127_n.c:14328-14342, then the parts of open/hw_config |
| 255 | * this driver needs (14935-15182, 15182-15190). */ |
| 256 | rtl_err e = exit_oob(d); |
| 257 | if (e != RTL_OK) |
| 258 | return e; |
| 259 | powerup(d); |
| 260 | if ((e = hw_reset(d)) != RTL_OK) |
| 261 | return e; |
| 262 | read_mac(d); |
| 263 | |
| 264 | for (uint32_t i = 0; i < RTL_TX_N; i++) |
| 265 | *word(d, txd(i)) = le(i == RTL_TX_N - 1 ? RTL_DESC_EOR : 0); |
| 266 | for (uint32_t i = 0; i < RTL_RX_N; i++) |
| 267 | rx_post(d, i); |
| 268 | d->ops.barrier(d->ops.ctx); |
| 269 | |
| 270 | w8(d, RTL_CFG9346, r8(d, RTL_CFG9346) | RTL_CFG9346_UNLOCK); |
| 271 | uint64_t tx_ring = dma + RTL_OFF_TXD, rx_ring = dma + RTL_OFF_RXD; |
| 272 | w32(d, RTL_TNPDS_LO, (uint32_t)tx_ring); /* desc_addr_fill, 15585-15598 */ |
| 273 | w32(d, RTL_TNPDS_HI, (uint32_t)(tx_ring >> 32)); |
| 274 | w32(d, RTL_RDSAR_LO, (uint32_t)rx_ring); |
| 275 | w32(d, RTL_RDSAR_HI, (uint32_t)(rx_ring >> 32)); |
| 276 | w32(d, RTL_TCR, RTL_TCR_VALUE); /* 14958-14963: burst, IFG, no-close bit 6 */ |
| 277 | w32(d, RTL_RSS_CTRL, 0); /* 14983: no RSS, one RX queue */ |
| 278 | w16(d, RTL_Q_NUM_CTRL, r16(d, RTL_Q_NUM_CTRL) & ~0x001Cu); /* 14876-14886: rx_q_num = ilog2(1) */ |
| 279 | ocp_wr(d, RTL_OCP_TXDESC_FMT, (ocp_rd(d, RTL_OCP_TXDESC_FMT) & ~0x3u) | 0x1u); /* 14992-14996: 32 B TX desc */ |
| 280 | w8(d, RTL_TXNOCLOSE_EN, r8(d, RTL_TXNOCLOSE_EN) | 0x04u); /* 14998-15001 */ |
| 281 | w8(d, RTL_RXDESC_TYPE, r8(d, RTL_RXDESC_TYPE) & ~0x02u); /* 15003-15010: type 1 */ |
| 282 | w16(d, RTL_RMS, RTL_RMS_VALUE | RTL_RMS_VLAN); /* 14470-14473 */ |
| 283 | w8(d, RTL_RXDV_GATE, r8(d, RTL_RXDV_GATE) & ~0x08u); /* 4023-4027 */ |
| 284 | w32(d, RTL_RCR, RTL_RCR_CHIP | RTL_RCR_ACCEPT_BCAST | RTL_RCR_ACCEPT_MYPHYS); |
| 285 | w8(d, RTL_CFG9346, r8(d, RTL_CFG9346) & ~RTL_CFG9346_UNLOCK); |
| 286 | |
| 287 | /* The vendor starts its close-pointer count at 0 after reset (4596-4605); |
| 288 | * this driver uses whatever the chip reports as its base instead. */ |
| 289 | d->clo_base = r32(d, RTL_HW_CLO0) & RTL_CLO_MASK; |
| 290 | w8(d, RTL_CHIPCMD, RTL_CMD_TXEN | RTL_CMD_RXEN); /* hw_start, 15182-15190; IRQs stay masked */ |
| 291 | d->ready = 1; |
| 292 | return RTL_OK; |
| 293 | } |
| 294 | |
| 295 | int rtl_link(rtl_dev *d, uint32_t *mbps) |
| 296 | { |
| 297 | if (mbps) |
| 298 | *mbps = 0; |
| 299 | if (!d || !d->ops.read) |
| 300 | return 0; |
| 301 | uint32_t s = r16(d, RTL_PHYSTATUS); /* r8127_n.c:4537-4547 */ |
| 302 | if (s == 0xffffu || !(s & RTL_PHYS_LINK)) |
| 303 | return 0; |
| 304 | if (mbps) |
| 305 | *mbps = (s & 0x4000u) ? 10000 : (s & 0x1000u) ? 5000 : (s & 0x0400u) ? 2500 : (s & 0x0010u) ? 1000 |
| 306 | : (s & 0x0008u) ? 100 : (s & 0x0004u) ? 10 : 0; |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | static rtl_err usable(rtl_dev *d) |
| 311 | { |
| 312 | if (!d) |
| 313 | return RTL_E_ARG; |
| 314 | if (d->broken) |
| 315 | return RTL_E_BROKEN; |
| 316 | return d->ready ? RTL_OK : RTL_E_NOT_READY; |
| 317 | } |
| 318 | |
| 319 | unsigned rtl_tx_pending(const rtl_dev *d) { return d ? d->tx_cur - d->tx_dirty : 0; } |
| 320 | |
| 321 | /* r8127_n.c:16427-16560 (one fragment) and the no-close doorbell 16412-16419. */ |
| 322 | rtl_err rtl_tx(rtl_dev *d, const uint8_t *frame, size_t len) |
| 323 | { |
| 324 | rtl_err e = usable(d); |
| 325 | if (e != RTL_OK) |
| 326 | return e; |
| 327 | if (!frame || len < 14 || len > RTL_FRAME_MAX) |
| 328 | return RTL_E_ARG; |
| 329 | if (d->tx_cur - d->tx_dirty >= RTL_TX_N) |
| 330 | return RTL_E_FULL; |
| 331 | uint32_t i = d->tx_cur % RTL_TX_N; |
| 332 | size_t n = len < RTL_FRAME_MIN ? RTL_FRAME_MIN : len; |
| 333 | uint8_t *buf = d->mem + RTL_OFF_TXBUF + (size_t)i * RTL_BUF_STRIDE; |
| 334 | memcpy(buf, frame, len); |
| 335 | memset(buf + len, 0, n - len); |
| 336 | uint64_t a = d->dma + RTL_OFF_TXBUF + (uint64_t)i * RTL_BUF_STRIDE; |
| 337 | *word(d, txd(i) + 4) = 0; |
| 338 | *word(d, txd(i) + 8) = le((uint32_t)a); |
| 339 | *word(d, txd(i) + 12) = le((uint32_t)(a >> 32)); |
| 340 | d->ops.barrier(d->ops.ctx); |
| 341 | *word(d, txd(i)) = le(RTL_DESC_OWN | RTL_DESC_FS | RTL_DESC_LS | (i == RTL_TX_N - 1 ? RTL_DESC_EOR : 0) | |
| 342 | (uint32_t)n); |
| 343 | d->tx_cur++; |
| 344 | /* Same origin as the close pointer (clo_base), so tail - close = frames in flight. */ |
| 345 | w32(d, RTL_SW_TAIL0, (d->clo_base + d->tx_cur) & RTL_CLO_MASK); /* write op orders the descriptor first */ |
| 346 | return RTL_OK; |
| 347 | } |
| 348 | |
| 349 | /* r8127_n.c:16590-16620: the closed count is the close pointer's advance. */ |
| 350 | rtl_err rtl_tx_reclaim(rtl_dev *d, unsigned *reclaimed) |
| 351 | { |
| 352 | if (reclaimed) |
| 353 | *reclaimed = 0; |
| 354 | rtl_err e = usable(d); |
| 355 | if (e != RTL_OK) |
| 356 | return e; |
| 357 | uint32_t clo = r32(d, RTL_HW_CLO0); |
| 358 | uint32_t done = (clo - d->clo_base - d->tx_dirty) & RTL_CLO_MASK; |
| 359 | if (clo == 0xffffffffu || done > d->tx_cur - d->tx_dirty) { |
| 360 | d->broken = 1; |
| 361 | return RTL_E_DEVICE; |
| 362 | } |
| 363 | d->tx_dirty += done; |
| 364 | if (reclaimed) |
| 365 | *reclaimed = done; |
| 366 | return RTL_OK; |
| 367 | } |
| 368 | |
| 369 | /* r8127_n.c:17036-17250 for one queue, type 1 descriptors, no fragments. */ |
| 370 | rtl_err rtl_rx(rtl_dev *d, uint8_t *out, size_t cap, size_t *len, int *got) |
| 371 | { |
| 372 | if (got) |
| 373 | *got = 0; |
| 374 | if (len) |
| 375 | *len = 0; |
| 376 | rtl_err e = usable(d); |
| 377 | if (e != RTL_OK) |
| 378 | return e; |
| 379 | if (!out || !len || !got) |
| 380 | return RTL_E_ARG; |
| 381 | for (uint32_t tries = 0; tries < RTL_RX_N; tries++) { |
| 382 | uint32_t i = d->rx_cur % RTL_RX_N; |
| 383 | uint32_t s = le(*word(d, rxd(i))); |
| 384 | if (s & RTL_DESC_OWN) |
| 385 | return RTL_OK; |
| 386 | d->ops.barrier(d->ops.ctx); |
| 387 | uint32_t n = s & RTL_RX_LEN_MASK; |
| 388 | if (n > RTL_RX_BUF) { |
| 389 | d->broken = 1; /* the chip wrote past the size it was given */ |
| 390 | return RTL_E_DEVICE; |
| 391 | } |
| 392 | int whole = (s & (RTL_DESC_FS | RTL_DESC_LS)) == (RTL_DESC_FS | RTL_DESC_LS); |
| 393 | size_t f = n >= 4 ? n - 4 : 0; /* FCS, r8127_n.c:17119-17126 */ |
| 394 | if (!whole || (s & RTL_RX_RES) || n < 14 + 4 || cap < f) { |
| 395 | d->rx_dropped++; /* a frame too big for `cap` is dropped too, so the slot never blocks the ring */ |
| 396 | } else { |
| 397 | memcpy(out, d->mem + RTL_OFF_RXBUF + (size_t)i * RTL_BUF_STRIDE, f); |
| 398 | *len = f; |
| 399 | *got = 1; |
| 400 | } |
| 401 | rx_post(d, i); |
| 402 | d->rx_cur++; |
| 403 | if (*got) |
| 404 | return RTL_OK; |
| 405 | } |
| 406 | return RTL_OK; |
| 407 | } |
| 408 | |
| 409 | rtl_err rtl_stop(rtl_dev *d) |
| 410 | { |
| 411 | if (!d || !d->ops.write) |
| 412 | return RTL_E_ARG; |
| 413 | if (!d->mcfg) |
| 414 | return RTL_E_NOT_READY; /* never identified as an RTL8127: write nothing */ |
| 415 | d->ready = 0; |
| 416 | return hw_reset(d); |
| 417 | } |
| 418 | |
| 419 | const char *rtl_err_name(rtl_err e) |
| 420 | { |
| 421 | switch (e) { |
| 422 | case RTL_OK: return "OK"; |
| 423 | case RTL_E_ARG: return "ARG"; |
| 424 | case RTL_E_MEM: return "MEM"; |
| 425 | case RTL_E_ABSENT: return "ABSENT"; |
| 426 | case RTL_E_CHIP: return "CHIP"; |
| 427 | case RTL_E_RESET_TIMEOUT: return "RESET_TIMEOUT"; |
| 428 | case RTL_E_FULL: return "FULL"; |
| 429 | case RTL_E_DEVICE: return "DEVICE"; |
| 430 | case RTL_E_BROKEN: return "BROKEN"; |
| 431 | case RTL_E_NOT_READY: return "NOT_READY"; |
| 432 | } |
| 433 | return "?"; |
| 434 | } |