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 / kernel / dev / pi5_gem.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* pi5_gem.c -- see pi5_gem.h. Pi 5 network cut 3: one frame through the RP1 GEM with descriptors only in the |
| 18 | * ADR 0020 bounce window. software-only DMA confinement (Pi 5, weaker than the Spark). */ |
| 19 | #include "pi5_gem.h" |
| 20 | |
| 21 | /* TEST-only mutants (make pi5-gem-mutants): each removes one decision and must make test_pi5_gem FAIL. |
| 22 | * Host builds only; never part of an image. */ |
| 23 | #if defined(CK_GEM_MUTANT_NO_VALIDATE) || defined(CK_GEM_MUTANT_BME_EARLY) || defined(CK_GEM_MUTANT_BME_LEFT_ON) || \ |
| 24 | defined(CK_GEM_MUTANT_NO_QUIESCE_WAIT) || defined(CK_GEM_MUTANT_NO_RETE_OFF) || defined(CK_GEM_MUTANT_NO_BARRIER) || \ |
| 25 | defined(CK_GEM_MUTANT_NO_TIEOFF) || defined(CK_GEM_MUTANT_NO_RXLEN_CHECK) || defined(CK_GEM_MUTANT_NO_TX_LAST) || \ |
| 26 | defined(CK_GEM_MUTANT_NO_SENTINEL_USED) || defined(CK_GEM_MUTANT_NO_ADDR64) || defined(CK_GEM_MUTANT_NO_HWWIN) || \ |
| 27 | defined(CK_GEM_MUTANT_NOT_STICKY) || defined(CK_GEM_MUTANT_NO_FRAMECAP) || defined(CK_GEM_MUTANT_NO_SEQ) || \ |
| 28 | defined(CK_GEM_MUTANT_NO_DIFF) || defined(CK_GEM_MUTANT_MDIO_OP) || defined(CK_GEM_MUTANT_PHYLB_BIT) || \ |
| 29 | defined(CK_GEM_MUTANT_NO_WRAP) || defined(CK_GEM_MUTANT_NO_GBE) || defined(CK_GEM_MUTANT_NO_DRFCS) || \ |
| 30 | defined(CK_GEM_MUTANT_NO_BME_VERIFY) || defined(CK_GEM_MUTANT_NO_LLB_CLEAR) |
| 31 | #if !__STDC_HOSTED__ |
| 32 | #error "CK_GEM_MUTANT_* are host test mutants only" |
| 33 | #endif |
| 34 | #ifdef CK_HARDWARE_STAGING |
| 35 | #error "CK_GEM_MUTANT_* cannot be combined with CK_HARDWARE_STAGING" |
| 36 | #endif |
| 37 | #endif |
| 38 | |
| 39 | #define GEM(off) (CK_RP1_ETH + (off)) |
| 40 | static uint32_t grd(const struct ck_gem *g, uint32_t off) { return g->rio->rd(g->rio->ctx, GEM(off)); } |
| 41 | static void gwr(const struct ck_gem *g, uint32_t off, uint32_t v) { g->rio->wr(g->rio->ctx, GEM(off), v); } |
| 42 | static void gdelay(const struct ck_gem *g, uint32_t us) |
| 43 | { |
| 44 | if (g->rio->delay_us && us) |
| 45 | g->rio->delay_us(g->rio->ctx, us); |
| 46 | } |
| 47 | static void gbarrier(const struct ck_gem *g) |
| 48 | { |
| 49 | #ifndef CK_GEM_MUTANT_NO_BARRIER |
| 50 | if (g->barrier) |
| 51 | g->barrier(); |
| 52 | #else |
| 53 | (void)g; |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | /* ---------------- probe ---------------- */ |
| 58 | |
| 59 | int ck_gem_probe(const struct ck_rp1_io *io, struct ck_gem_caps *c) |
| 60 | { |
| 61 | uint32_t dbwdef, i; |
| 62 | if (!io || !c) |
| 63 | return CK_GEM_E_ARG; |
| 64 | *c = (struct ck_gem_caps){0}; |
| 65 | c->mid = io->rd(io->ctx, GEM(CK_GEM_MID)); |
| 66 | c->is_gem = ((c->mid >> CK_GEM_MID_IDNUM_SHIFT) & 0xfffu) >= 2u; /* hw_is_gem [M2] */ |
| 67 | if (!c->is_gem) |
| 68 | return CK_GEM_E_NOGEM; |
| 69 | c->dcfg1 = io->rd(io->ctx, GEM(CK_GEM_DCFG1)); |
| 70 | c->dcfg2 = io->rd(io->ctx, GEM(CK_GEM_DCFG2)); |
| 71 | c->dcfg6 = io->rd(io->ctx, GEM(CK_GEM_DCFG6)); |
| 72 | c->daw64 = (c->dcfg6 & CK_GEM_DCFG6_DAW64) != 0; |
| 73 | c->queue_mask = 1u | (c->dcfg6 & 0xffu); /* macb_probe_queues: bit 0 is never set but queue 0 always exists */ |
| 74 | for (i = 0; i < CK_GEM_MAX_QUEUES; i++) |
| 75 | if (c->queue_mask & (1u << i)) |
| 76 | c->nqueues++; |
| 77 | c->desc_bytes = c->daw64 ? 16u : 8u; |
| 78 | dbwdef = (c->dcfg1 >> CK_GEM_DCFG1_DBWDEF_SHIFT) & 7u; |
| 79 | c->dbw = dbwdef == 4 ? 2u : dbwdef == 2 ? 1u : 0u; /* macb_dbw: 128 / 64 / 32 bit */ |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | void ck_gem_init(struct ck_gem *g, const struct ck_rp1_io *rio, struct ck_dmaw *w, volatile uint8_t *va, |
| 84 | int (*bme)(void *, int), void *bme_ctx, void (*barrier)(void), int hw_window_ok) |
| 85 | { |
| 86 | *g = (struct ck_gem){0}; |
| 87 | g->rio = rio; |
| 88 | g->w = w; |
| 89 | g->va = va; |
| 90 | g->bme = bme; |
| 91 | g->bme_ctx = bme_ctx; |
| 92 | g->barrier = barrier; |
| 93 | g->hw_window_ok = hw_window_ok; |
| 94 | } |
| 95 | |
| 96 | /* ---------------- window memory (CPU side) ---------------- */ |
| 97 | |
| 98 | static int in_win(const struct ck_gem *g, uint64_t bus, uint64_t len) |
| 99 | { |
| 100 | return bus >= g->w->base && len <= g->w->size && (bus - g->w->base) <= (g->w->size - len); |
| 101 | } |
| 102 | static uint32_t m_rd(const struct ck_gem *g, uint64_t bus) |
| 103 | { |
| 104 | return in_win(g, bus, 4) ? *(volatile uint32_t *)(g->va + (bus - g->w->base)) : 0xdeadbeefu; |
| 105 | } |
| 106 | static void m_wr(const struct ck_gem *g, uint64_t bus, uint32_t v) |
| 107 | { |
| 108 | if (in_win(g, bus, 4)) |
| 109 | *(volatile uint32_t *)(g->va + (bus - g->w->base)) = v; |
| 110 | } |
| 111 | static void m_zero(const struct ck_gem *g, uint64_t bus, uint64_t len) |
| 112 | { |
| 113 | uint64_t i; |
| 114 | if (!in_win(g, bus, len)) |
| 115 | return; |
| 116 | for (i = 0; i < len; i++) |
| 117 | g->va[(bus - g->w->base) + i] = 0; |
| 118 | } |
| 119 | |
| 120 | /* ---------------- frames ---------------- */ |
| 121 | |
| 122 | static const char k_magic[] = "AIENOS-PI5-NET3"; /* 15 chars + NUL = 16 bytes */ |
| 123 | |
| 124 | uint32_t ck_gem_frame_build(int kind, const uint8_t mac[6], uint32_t seq, const uint8_t tpa[4], uint8_t *out, uint32_t cap) |
| 125 | { |
| 126 | uint32_t i; |
| 127 | if (!mac || !out || cap < 60u) |
| 128 | return 0; |
| 129 | for (i = 0; i < 60u; i++) |
| 130 | out[i] = 0; |
| 131 | for (i = 0; i < 6; i++) { |
| 132 | out[i] = 0xff; |
| 133 | out[6 + i] = mac[i]; |
| 134 | } |
| 135 | if (kind == CK_GEMF_RAW) { |
| 136 | out[12] = (uint8_t)(CK_GEMF_ETHERTYPE_EXP >> 8); |
| 137 | out[13] = (uint8_t)CK_GEMF_ETHERTYPE_EXP; |
| 138 | for (i = 0; i < 16; i++) |
| 139 | out[14 + i] = (uint8_t)k_magic[i]; |
| 140 | #ifndef CK_GEM_MUTANT_NO_SEQ |
| 141 | out[30] = (uint8_t)(seq >> 24); |
| 142 | out[31] = (uint8_t)(seq >> 16); |
| 143 | out[32] = (uint8_t)(seq >> 8); |
| 144 | out[33] = (uint8_t)seq; |
| 145 | #endif |
| 146 | for (i = 34; i < 60u; i++) |
| 147 | out[i] = (uint8_t)(seq + i * 7u + 1u); |
| 148 | return 60u; |
| 149 | } |
| 150 | if (kind == CK_GEMF_ARP) { |
| 151 | static const uint8_t hdr[8] = {0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01}; /* htype 1, ptype IPv4, hlen 6, plen 4, request */ |
| 152 | out[12] = 0x08; |
| 153 | out[13] = 0x06; |
| 154 | for (i = 0; i < 8; i++) |
| 155 | out[14 + i] = hdr[i]; |
| 156 | for (i = 0; i < 6; i++) |
| 157 | out[22 + i] = mac[i]; /* sender hardware address; sender IP (28..31) and target hardware (32..37) stay zero: a probe */ |
| 158 | if (tpa) |
| 159 | for (i = 0; i < 4; i++) |
| 160 | out[38 + i] = tpa[i]; |
| 161 | out[42] = 'A'; out[43] = 'I'; out[44] = 'E'; out[45] = 'N'; out[46] = '3'; |
| 162 | #ifndef CK_GEM_MUTANT_NO_SEQ |
| 163 | out[47] = (uint8_t)(seq >> 24); |
| 164 | out[48] = (uint8_t)(seq >> 16); |
| 165 | out[49] = (uint8_t)(seq >> 8); |
| 166 | out[50] = (uint8_t)seq; |
| 167 | #endif |
| 168 | for (i = 51; i < 60u; i++) |
| 169 | out[i] = (uint8_t)(seq + i * 7u + 1u); |
| 170 | return 60u; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | uint32_t ck_gem_frame_diff(const uint8_t *a, uint32_t alen, const uint8_t *b, uint32_t blen) |
| 176 | { |
| 177 | uint32_t i, n = alen < blen ? alen : blen; |
| 178 | #ifdef CK_GEM_MUTANT_NO_DIFF |
| 179 | if (n) |
| 180 | n--; /* mutant: ignores the last byte */ |
| 181 | if (alen != blen && n == 0) |
| 182 | return 1; |
| 183 | for (i = 0; i < n; i++) |
| 184 | if (a[i] != b[i]) |
| 185 | return i + 1; |
| 186 | return 0; |
| 187 | #else |
| 188 | for (i = 0; i < n; i++) |
| 189 | if (a[i] != b[i]) |
| 190 | return i + 1; |
| 191 | return alen == blen ? 0 : n + 1; |
| 192 | #endif |
| 193 | } |
| 194 | |
| 195 | /* ---------------- bus-master ---------------- */ |
| 196 | |
| 197 | int ck_pi5gem_bme_set(const struct ck_pi5pcie_io *io, int on) |
| 198 | { |
| 199 | uint32_t v = 0, nv; |
| 200 | if (!io || ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v)) |
| 201 | return CK_GEM_E_ARG; |
| 202 | nv = (v & 0xffffu); /* status half written as zero: RW1C, nothing cleared */ |
| 203 | nv = on ? (nv | CK_PI5NET_CMD_BME) : (nv & ~(uint32_t)CK_PI5NET_CMD_BME); |
| 204 | if (ck_pi5net_cfg_write32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, nv)) |
| 205 | return CK_GEM_E_ARG; |
| 206 | #ifndef CK_GEM_MUTANT_NO_BME_VERIFY |
| 207 | if (ck_pi5pcie_cfg_read32(io, 1, 0, 0, CK_PI5NET_PCI_COMMAND, &v)) |
| 208 | return CK_GEM_E_ARG; |
| 209 | if (((v & CK_PI5NET_CMD_BME) != 0) != (on != 0)) |
| 210 | return CK_GEM_E_BME; |
| 211 | #endif |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /* ---------------- descriptors (CPU writes into the window) ---------------- */ |
| 216 | |
| 217 | /* Hand a TX slot to the device: address words first, ctrl (USED clear) last [M2 macb_tx_map]. */ |
| 218 | static void tx_desc_write(const struct ck_gem *g, uint64_t slot_bus, uint64_t buf, uint32_t len, uint32_t flags_ctrl) |
| 219 | { |
| 220 | uint32_t dsz = g->caps.desc_bytes; |
| 221 | m_wr(g, slot_bus + 0, (uint32_t)buf); |
| 222 | if (dsz == 16) { |
| 223 | m_wr(g, slot_bus + 8, (uint32_t)(buf >> 32)); |
| 224 | m_wr(g, slot_bus + 12, 0); |
| 225 | } |
| 226 | gbarrier(g); |
| 227 | m_wr(g, slot_bus + 4, (len & CK_GEMD_TX_FRMLEN_MASK) | flags_ctrl); |
| 228 | } |
| 229 | |
| 230 | /* RX slot: ctrl 0 and the high address first, the low address word (USED clear) last [M2 gem_rx_refill]. */ |
| 231 | static void rx_desc_write(const struct ck_gem *g, uint64_t slot_bus, uint64_t buf, uint32_t addr_flags) |
| 232 | { |
| 233 | uint32_t dsz = g->caps.desc_bytes; |
| 234 | m_wr(g, slot_bus + 4, 0); |
| 235 | if (dsz == 16) { |
| 236 | m_wr(g, slot_bus + 8, (uint32_t)(buf >> 32)); |
| 237 | m_wr(g, slot_bus + 12, 0); |
| 238 | } |
| 239 | gbarrier(g); |
| 240 | m_wr(g, slot_bus + 0, ((uint32_t)buf & ~3u) | addr_flags); |
| 241 | } |
| 242 | |
| 243 | static int post(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint64_t addr, uint32_t len, uint32_t flags, uint32_t *idx, int caller_chosen) |
| 244 | { |
| 245 | #ifdef CK_GEM_MUTANT_NO_VALIDATE |
| 246 | if (caller_chosen) { /* mutant: a caller-chosen address is trusted */ |
| 247 | struct ck_dmaw_slot *s = &r->slots[r->head]; |
| 248 | s->d.addr = addr; s->d.len = len; s->d.flags = flags; s->state = CK_DMAW_SLOT_INFLIGHT; |
| 249 | *idx = r->head; r->head = (r->head + 1) & (r->n - 1); r->inflight++; |
| 250 | return CK_DMAW_OK; |
| 251 | } |
| 252 | #else |
| 253 | (void)caller_chosen; |
| 254 | #endif |
| 255 | return ck_dmaw_ring_post(r, w, addr, len, flags, idx); |
| 256 | } |
| 257 | |
| 258 | #ifdef CK_GEM_MUTANT_NO_VALIDATE |
| 259 | static int emit(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t i, struct ck_dmaw_desc *d) |
| 260 | { |
| 261 | (void)w; |
| 262 | *d = r->slots[i].d; /* mutant: shadow trusted without re-validation */ |
| 263 | return 0; |
| 264 | } |
| 265 | #else |
| 266 | #define emit ck_dmaw_ring_emit |
| 267 | #endif |
| 268 | |
| 269 | /* ---------------- hardware programming ---------------- */ |
| 270 | |
| 271 | static void gem_reset_hw(const struct ck_gem *g) |
| 272 | { |
| 273 | uint32_t ncr = grd(g, CK_GEM_NCR), q; |
| 274 | ncr &= ~(CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_TSTART); /* macb_reset_hw [M2] */ |
| 275 | #ifndef CK_GEM_MUTANT_NO_LLB_CLEAR |
| 276 | ncr &= ~CK_GEM_NCR_LLB; |
| 277 | #endif |
| 278 | ncr |= CK_GEM_NCR_CLRSTAT; |
| 279 | gwr(g, CK_GEM_NCR, ncr); |
| 280 | gwr(g, CK_GEM_TSR, 0xffffffffu); |
| 281 | gwr(g, CK_GEM_RSR, 0xffffffffu); |
| 282 | gwr(g, CK_GEM_PBUFRXCUT, 0); |
| 283 | gwr(g, CK_GEM_IDR, 0xffffffffu); |
| 284 | (void)grd(g, CK_GEM_ISR); /* read clears (no MACB_CAPS_ISR_CLEAR_ON_WRITE for RP1) */ |
| 285 | for (q = 1; q < CK_GEM_MAX_QUEUES; q++) { |
| 286 | if (!(g->caps.queue_mask & (1u << q))) |
| 287 | continue; |
| 288 | gwr(g, CK_GEM_QN_IDR(q), 0xffffffffu); |
| 289 | (void)grd(g, CK_GEM_QN_ISR(q)); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static void gem_set_mac(const struct ck_gem *g, const uint8_t m[6]) |
| 294 | { |
| 295 | gwr(g, CK_GEM_SA1B, (uint32_t)m[0] | ((uint32_t)m[1] << 8) | ((uint32_t)m[2] << 16) | ((uint32_t)m[3] << 24)); |
| 296 | gwr(g, CK_GEM_SA1T, (uint32_t)m[4] | ((uint32_t)m[5] << 8)); |
| 297 | gwr(g, CK_GEM_SA2B, 0); gwr(g, CK_GEM_SA2B + 4, 0); |
| 298 | gwr(g, CK_GEM_SA2B + 8, 0); gwr(g, CK_GEM_SA2B + 12, 0); |
| 299 | gwr(g, CK_GEM_SA2B + 16, 0); gwr(g, CK_GEM_SA2B + 20, 0); |
| 300 | gwr(g, CK_GEM_HRB, 0); |
| 301 | gwr(g, CK_GEM_HRT, 0); |
| 302 | } |
| 303 | |
| 304 | static void gem_config(const struct ck_gem *g, const struct ck_gem_xfer *x) |
| 305 | { |
| 306 | uint32_t cfg = grd(g, CK_GEM_NCFGR), dma, amp; |
| 307 | /* NCFGR [M2 macb_init_hw + macb_mac_link_up]: keep the MDC divider the MDIO code set; data bus width from DCFG1; |
| 308 | * discard the FCS on receive; broadcast accepted (NBC clear); no copy-all; standard frame size (no BIG/JFRAME). */ |
| 309 | cfg &= (uint32_t)CK_GEM_NCFGR_CLK_MASK; |
| 310 | cfg |= g->caps.dbw << CK_GEM_NCFGR_DBW_SHIFT; |
| 311 | #ifndef CK_GEM_MUTANT_NO_DRFCS |
| 312 | cfg |= CK_GEM_NCFGR_DRFCS; |
| 313 | #endif |
| 314 | if (x->full_duplex) |
| 315 | cfg |= CK_GEM_NCFGR_FD; |
| 316 | if (x->speed == 100) |
| 317 | cfg |= CK_GEM_NCFGR_SPD; |
| 318 | #ifndef CK_GEM_MUTANT_NO_GBE |
| 319 | if (x->speed == 1000) |
| 320 | cfg |= CK_GEM_NCFGR_GBE; |
| 321 | #endif |
| 322 | gwr(g, CK_GEM_NCFGR, cfg); |
| 323 | /* gem_init_axi [M2]: AXI pipelines as rp1.dtsi sets them. */ |
| 324 | amp = grd(g, CK_GEM_AMP); |
| 325 | amp |= CK_GEM_AMP_AW2B_FILL; |
| 326 | amp = (amp & ~(uint32_t)CK_GEM_AMP_AW2W_MASK) | (CK_GEM_AXI_PIPE << CK_GEM_AMP_AW2W_SHIFT); |
| 327 | amp = (amp & ~(uint32_t)CK_GEM_AMP_AR2R_MASK) | CK_GEM_AXI_PIPE; |
| 328 | gwr(g, CK_GEM_AMP, amp); |
| 329 | /* macb_configure_dma [M2] */ |
| 330 | dma = grd(g, CK_GEM_DMACFG); |
| 331 | dma &= ~(uint32_t)(CK_GEM_DMACFG_RXBS_MASK | CK_GEM_DMACFG_FBLDO_MASK | CK_GEM_DMACFG_ENDIA_PKT | |
| 332 | CK_GEM_DMACFG_ENDIA_DESC | CK_GEM_DMACFG_TXCOEN | CK_GEM_DMACFG_RXEXT | CK_GEM_DMACFG_TXEXT | |
| 333 | CK_GEM_DMACFG_ADDR64); |
| 334 | dma |= ((x->rx_buf_len ? x->rx_buf_len : 64u) / CK_GEM_RXBS_UNIT) << CK_GEM_DMACFG_RXBS_SHIFT; |
| 335 | dma |= CK_GEM_BURST; |
| 336 | dma |= CK_GEM_DMACFG_TXPBMS | CK_GEM_DMACFG_RXBMS_MASK; |
| 337 | #ifndef CK_GEM_MUTANT_NO_ADDR64 |
| 338 | if (g->caps.daw64) |
| 339 | dma |= CK_GEM_DMACFG_ADDR64; |
| 340 | #endif |
| 341 | gwr(g, CK_GEM_DMACFG, dma); |
| 342 | /* macb_init: USRIO for an RGMII phy-mode (rp1.dtsi: rgmii-id). */ |
| 343 | gwr(g, CK_GEM_USRIO, CK_GEM_USRIO_RGMII); |
| 344 | } |
| 345 | |
| 346 | /* ---------------- one transfer ---------------- */ |
| 347 | |
| 348 | static int refuse(struct ck_gem *g, int rc) |
| 349 | { |
| 350 | (void)g; |
| 351 | #ifndef CK_GEM_MUTANT_NOT_STICKY |
| 352 | g->halted = 1; |
| 353 | #endif |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | int ck_gem_xfer(struct ck_gem *g, struct ck_gem_xfer *x) |
| 358 | { |
| 359 | struct ck_dmaw *w; |
| 360 | struct ck_dmaw_slot txs[CK_GEM_TXRING], rxs[CK_GEM_RXRING]; |
| 361 | struct ck_dmaw_ring txr, rxr; |
| 362 | struct ck_dmaw_desc d; |
| 363 | uint64_t next0, txring, rxring, tie_tx, tie_rx, txbuf, sentbuf, rxbuf[3] = {0, 0, 0}; |
| 364 | uint32_t dsz, i, idx, polls, q, ncr, flags; |
| 365 | int rc = 0, qrc = 0, prc; |
| 366 | |
| 367 | if (!g || !g->w || !g->rio || !x || !x->frame) |
| 368 | return CK_GEM_E_ARG; |
| 369 | w = g->w; |
| 370 | x->tx_done = x->rx_done = 0; |
| 371 | x->tx_ctrl = x->rx_word0 = x->rx_ctrl = x->rx_len = 0; |
| 372 | x->tsr = x->rsr = x->isr = x->ncr_run = x->ncfgr_run = x->dmacfg_run = x->ncr_end = x->tsr_end = 0; |
| 373 | x->tx_bus = x->rx_bus = 0; |
| 374 | x->rx_copied = x->rx_frames = x->rx_slot = 0; |
| 375 | x->bme_enabled_for_us = 0; |
| 376 | x->bme_off_at_end = 0; |
| 377 | #ifndef CK_GEM_MUTANT_NOT_STICKY |
| 378 | if (g->halted || ck_dmaw_halted(w)) |
| 379 | return CK_GEM_E_HALTED; |
| 380 | #endif |
| 381 | if (x->mode < CK_GEMX_LLB || x->mode > CK_GEMX_WIRE || (x->speed != 10 && x->speed != 100 && x->speed != 1000) || |
| 382 | x->rx_count > 3 || (x->rx_count && (x->rx_buf_len < 64u || x->rx_buf_len > CK_DMAW_MAX_FRAME || (x->rx_buf_len % 64u)))) |
| 383 | return CK_GEM_E_ARG; |
| 384 | #ifdef CK_GEM_MUTANT_NO_FRAMECAP |
| 385 | if (x->frame_len < 60u) |
| 386 | #else |
| 387 | if (x->frame_len < 60u || x->frame_len > 1514u) |
| 388 | #endif |
| 389 | return CK_GEM_E_ARG; |
| 390 | if (!g->caps.is_gem) |
| 391 | return CK_GEM_E_NOGEM; |
| 392 | #ifndef CK_GEM_MUTANT_NO_HWWIN |
| 393 | if (!g->hw_window_ok) |
| 394 | return CK_GEM_E_HWWIN; /* no root-complex backstop confirmed: bus-master stays off, nothing touched */ |
| 395 | #endif |
| 396 | #ifdef CK_GEM_MUTANT_BME_EARLY |
| 397 | if (g->bme) |
| 398 | (void)g->bme(g->bme_ctx, 1); /* mutant: bus-master before anything is validated */ |
| 399 | #endif |
| 400 | dsz = g->caps.desc_bytes; |
| 401 | next0 = w->next; |
| 402 | |
| 403 | /* ---- 1. layout and validation: memory only, no register is touched, bus-master stays off ---- */ |
| 404 | if (ck_dmaw_alloc(w, CK_GEM_TXRING * dsz, 64, &txring) || ck_dmaw_alloc(w, CK_GEM_RXRING * dsz, 64, &rxring) || |
| 405 | ck_dmaw_alloc(w, dsz, 64, &tie_tx) || ck_dmaw_alloc(w, dsz, 64, &tie_rx) || |
| 406 | ck_dmaw_alloc(w, 64, 64, &sentbuf) || ck_dmaw_alloc(w, (uint64_t)((x->frame_len + 63u) & ~63u), 64, &txbuf)) { |
| 407 | w->next = next0; |
| 408 | return CK_GEM_E_WINDOW; |
| 409 | } |
| 410 | for (i = 0; i < x->rx_count; i++) |
| 411 | if (ck_dmaw_alloc(w, x->rx_buf_len, 64, &rxbuf[i])) { |
| 412 | w->next = next0; |
| 413 | return CK_GEM_E_WINDOW; |
| 414 | } |
| 415 | if (x->tx_addr) |
| 416 | txbuf = x->tx_addr; |
| 417 | if (x->rx_count && x->rx_addr) |
| 418 | rxbuf[0] = x->rx_addr; |
| 419 | if (!g->caps.daw64 && ((w->base + w->size - 1) >> 32)) |
| 420 | return refuse(g, CK_GEM_E_WINDOW); /* 32-bit descriptors cannot name this window */ |
| 421 | /* the four tables the device reads or writes, and every buffer, through the ADR 0020 validator */ |
| 422 | if (ck_dmaw_validate_aligned(w, txring, CK_GEM_TXRING * dsz, 64) || ck_dmaw_validate_aligned(w, rxring, CK_GEM_RXRING * dsz, 64) || |
| 423 | ck_dmaw_validate_aligned(w, tie_tx, dsz, 64) || ck_dmaw_validate_aligned(w, tie_rx, dsz, 64)) |
| 424 | return refuse(g, CK_GEM_E_WINDOW); |
| 425 | if (ck_dmaw_ring_init(&txr, txs, CK_GEM_TXRING) || ck_dmaw_ring_init(&rxr, rxs, CK_GEM_RXRING)) |
| 426 | return refuse(g, CK_GEM_E_ARG); |
| 427 | prc = post(&txr, w, txbuf, x->frame_len, CK_GEMD_TX_LAST, &idx, x->tx_addr != 0); |
| 428 | if (prc) |
| 429 | return refuse(g, CK_GEM_E_WINDOW); |
| 430 | prc = post(&txr, w, sentbuf, 64, CK_GEMD_TX_USED | CK_GEMD_TX_WRAP, &idx, 0); /* sentinel: never executed (USED) */ |
| 431 | if (prc) |
| 432 | return refuse(g, CK_GEM_E_WINDOW); |
| 433 | for (i = 0; i < CK_GEM_RXRING; i++) { |
| 434 | if (i < x->rx_count) |
| 435 | prc = post(&rxr, w, rxbuf[i], x->rx_buf_len, 0, &idx, i == 0 && x->rx_addr != 0); |
| 436 | else |
| 437 | prc = post(&rxr, w, sentbuf, 64, CK_GEMD_RX_USED, &idx, 0); |
| 438 | if (prc) |
| 439 | return refuse(g, CK_GEM_E_WINDOW); |
| 440 | } |
| 441 | |
| 442 | /* ---- 2. write the descriptors from the shadow only ---- */ |
| 443 | m_zero(g, txring, CK_GEM_TXRING * dsz); |
| 444 | m_zero(g, rxring, CK_GEM_RXRING * dsz); |
| 445 | m_zero(g, tie_tx, dsz); |
| 446 | m_zero(g, tie_rx, dsz); |
| 447 | m_zero(g, sentbuf, 64); |
| 448 | m_zero(g, txbuf, (uint64_t)((x->frame_len + 63u) & ~63u)); |
| 449 | for (i = 0; i < x->rx_count; i++) |
| 450 | m_zero(g, rxbuf[i], x->rx_buf_len); |
| 451 | if (in_win(g, txbuf, x->frame_len)) |
| 452 | for (i = 0; i < x->frame_len; i++) |
| 453 | g->va[(txbuf - w->base) + i] = x->frame[i]; /* copy only (ADR 0020 rule 5) */ |
| 454 | x->tx_bus = txbuf; |
| 455 | x->rx_bus = x->rx_count ? rxbuf[0] : 0; |
| 456 | |
| 457 | for (i = 0; i < CK_GEM_TXRING; i++) { |
| 458 | if (emit(&txr, w, i, &d)) |
| 459 | return refuse(g, CK_GEM_E_WINDOW); |
| 460 | flags = i == 0 ? 0 : (CK_GEMD_TX_USED | CK_GEMD_TX_WRAP); |
| 461 | #ifdef CK_GEM_MUTANT_NO_SENTINEL_USED |
| 462 | flags = i == 0 ? 0 : CK_GEMD_TX_WRAP; |
| 463 | #endif |
| 464 | if (i == 0) { |
| 465 | #ifndef CK_GEM_MUTANT_NO_TX_LAST |
| 466 | flags |= CK_GEMD_TX_LAST; |
| 467 | #endif |
| 468 | } |
| 469 | if (i == 0 && x->raw_tx_outside) |
| 470 | d.addr = x->raw_tx_outside; /* confinement test only */ |
| 471 | tx_desc_write(g, txring + (uint64_t)i * dsz, d.addr, d.len, flags); |
| 472 | } |
| 473 | for (i = 0; i < CK_GEM_RXRING; i++) { |
| 474 | if (emit(&rxr, w, i, &d)) |
| 475 | return refuse(g, CK_GEM_E_WINDOW); |
| 476 | flags = (i >= x->rx_count) ? CK_GEMD_RX_USED : 0; |
| 477 | #ifndef CK_GEM_MUTANT_NO_WRAP |
| 478 | if (i == CK_GEM_RXRING - 1) |
| 479 | flags |= CK_GEMD_RX_WRAP; |
| 480 | #endif |
| 481 | if (i == 0 && x->raw_rx_outside) |
| 482 | d.addr = x->raw_rx_outside; /* confinement test only */ |
| 483 | rx_desc_write(g, rxring + (uint64_t)i * dsz, d.addr, flags); |
| 484 | } |
| 485 | /* tie-off descriptors for every other hardware queue [M2 macb_init_tieoff]: USED so the device never uses them */ |
| 486 | tx_desc_write(g, tie_tx, sentbuf, 64, CK_GEMD_TX_USED | CK_GEMD_TX_WRAP); |
| 487 | rx_desc_write(g, tie_rx, sentbuf, CK_GEMD_RX_USED | CK_GEMD_RX_WRAP); |
| 488 | gbarrier(g); |
| 489 | |
| 490 | /* ---- 3. program the MAC (bus-master still off) ---- */ |
| 491 | gem_reset_hw(g); |
| 492 | gem_set_mac(g, x->mac); |
| 493 | gem_config(g, x); |
| 494 | gwr(g, CK_GEM_RBQP, (uint32_t)rxring); |
| 495 | gwr(g, CK_GEM_TBQP, (uint32_t)txring); |
| 496 | if (g->caps.daw64) { |
| 497 | gwr(g, CK_GEM_RBQPH, (uint32_t)(rxring >> 32)); |
| 498 | gwr(g, CK_GEM_TBQPH, (uint32_t)(txring >> 32)); |
| 499 | } |
| 500 | #ifndef CK_GEM_MUTANT_NO_TIEOFF |
| 501 | for (q = 1; q < CK_GEM_MAX_QUEUES; q++) { |
| 502 | if (!(g->caps.queue_mask & (1u << q))) |
| 503 | continue; |
| 504 | gwr(g, CK_GEM_QN_TBQP(q), (uint32_t)tie_tx); |
| 505 | gwr(g, CK_GEM_QN_RBQP(q), (uint32_t)tie_rx); |
| 506 | gwr(g, CK_GEM_QN_RBQS(q), (x->rx_buf_len ? x->rx_buf_len : 64u) / CK_GEM_RXBS_UNIT); |
| 507 | } |
| 508 | #else |
| 509 | (void)q; (void)tie_tx; (void)tie_rx; |
| 510 | #endif |
| 511 | x->dmacfg_run = grd(g, CK_GEM_DMACFG); |
| 512 | x->ncfgr_run = grd(g, CK_GEM_NCFGR); |
| 513 | gbarrier(g); |
| 514 | |
| 515 | /* ---- 4. bus-master on ONLY now, after every descriptor passed the validator ---- */ |
| 516 | if (!g->bme) |
| 517 | return refuse(g, CK_GEM_E_BME); |
| 518 | #ifndef CK_GEM_MUTANT_BME_EARLY |
| 519 | if (g->bme(g->bme_ctx, 1)) { |
| 520 | (void)g->bme(g->bme_ctx, 0); |
| 521 | return refuse(g, CK_GEM_E_BME); |
| 522 | } |
| 523 | #endif |
| 524 | g->bme_on = 1; |
| 525 | x->bme_enabled_for_us = 1; |
| 526 | ncr = grd(g, CK_GEM_NCR); |
| 527 | ncr |= CK_GEM_NCR_TE; |
| 528 | if (x->rx_count) |
| 529 | ncr |= CK_GEM_NCR_RE; |
| 530 | if (x->mode == CK_GEMX_LLB) |
| 531 | ncr |= CK_GEM_NCR_LLB; |
| 532 | gwr(g, CK_GEM_NCR, ncr); |
| 533 | gbarrier(g); |
| 534 | gwr(g, CK_GEM_NCR, ncr | CK_GEM_NCR_TSTART); |
| 535 | x->ncr_run = grd(g, CK_GEM_NCR); |
| 536 | g->xfers++; |
| 537 | |
| 538 | /* ---- 5. poll (bounded) ---- */ |
| 539 | for (polls = 0; polls <= x->tx_wait_us / 10u; polls++) { |
| 540 | if (m_rd(g, txring + 4) & CK_GEMD_TX_USED) { |
| 541 | x->tx_done = 1; |
| 542 | break; |
| 543 | } |
| 544 | gdelay(g, 10); |
| 545 | } |
| 546 | if (x->rx_count) { |
| 547 | for (polls = 0; polls <= x->rx_wait_us / 10u; polls++) { |
| 548 | if ((m_rd(g, rxring) & CK_GEMD_RX_USED) && (m_rd(g, rxring + 4) & (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) == |
| 549 | (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) |
| 550 | break; |
| 551 | gdelay(g, 10); |
| 552 | } |
| 553 | } |
| 554 | x->tsr = grd(g, CK_GEM_TSR); |
| 555 | x->rsr = grd(g, CK_GEM_RSR); |
| 556 | x->isr = grd(g, CK_GEM_ISR); |
| 557 | |
| 558 | /* ---- 6. quiesce: stop RE/TE (and loopback), wait for the transmitter, settle, THEN bus-master off ---- */ |
| 559 | ncr = grd(g, CK_GEM_NCR); |
| 560 | #ifndef CK_GEM_MUTANT_NO_RETE_OFF |
| 561 | ncr &= ~(CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_LLB | CK_GEM_NCR_TSTART); |
| 562 | gwr(g, CK_GEM_NCR, ncr); |
| 563 | #endif |
| 564 | #ifndef CK_GEM_MUTANT_NO_QUIESCE_WAIT |
| 565 | for (polls = 0; polls < 1000u; polls++) { |
| 566 | if (!(grd(g, CK_GEM_TSR) & CK_GEM_TSR_TGO)) |
| 567 | break; |
| 568 | gdelay(g, 10); |
| 569 | } |
| 570 | if (grd(g, CK_GEM_TSR) & CK_GEM_TSR_TGO) |
| 571 | qrc = CK_GEM_E_QUIESCE; |
| 572 | #endif |
| 573 | gdelay(g, 2000); /* let anything already on the bus finish (same margin as the window change) */ |
| 574 | gwr(g, CK_GEM_TSR, 0xffffffffu); |
| 575 | gwr(g, CK_GEM_RSR, 0xffffffffu); |
| 576 | x->ncr_end = grd(g, CK_GEM_NCR); |
| 577 | x->tsr_end = grd(g, CK_GEM_TSR); |
| 578 | #ifndef CK_GEM_MUTANT_BME_LEFT_ON |
| 579 | if (g->bme(g->bme_ctx, 0)) { |
| 580 | g->halted = 1; |
| 581 | return CK_GEM_E_BME; |
| 582 | } |
| 583 | g->bme_on = 0; |
| 584 | x->bme_off_at_end = 1; |
| 585 | #endif |
| 586 | if (qrc) { |
| 587 | g->halted = 1; |
| 588 | rc = qrc; |
| 589 | } |
| 590 | |
| 591 | /* ---- 7. results: device-written words are read as data only; lengths are checked against the shadow ---- */ |
| 592 | x->tx_ctrl = m_rd(g, txring + 4); |
| 593 | x->tx_done = (x->tx_ctrl & CK_GEMD_TX_USED) != 0; |
| 594 | for (i = 0; i < x->rx_count; i++) { |
| 595 | uint32_t w0 = m_rd(g, rxring + (uint64_t)i * dsz), c = m_rd(g, rxring + (uint64_t)i * dsz + 4); |
| 596 | if ((w0 & CK_GEMD_RX_USED) && (c & (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) == (CK_GEMD_RX_SOF | CK_GEMD_RX_EOF)) { |
| 597 | uint32_t len = c & CK_GEMD_RX_FRMLEN_MASK, n; |
| 598 | #ifndef CK_GEM_MUTANT_NO_RXLEN_CHECK |
| 599 | if (ck_dmaw_ring_complete(&rxr, w, i, len)) { |
| 600 | g->halted = 1; /* the device claims more bytes than the buffer holds */ |
| 601 | return CK_GEM_E_DEVICE; |
| 602 | } |
| 603 | #endif |
| 604 | if (x->rx_frames++ == 0) { |
| 605 | x->rx_slot = i; |
| 606 | x->rx_done = 1; |
| 607 | x->rx_word0 = w0; |
| 608 | x->rx_ctrl = c; |
| 609 | x->rx_len = len; |
| 610 | n = len < CK_GEM_RXCOPY ? len : CK_GEM_RXCOPY; |
| 611 | if (n > x->rx_buf_len) |
| 612 | n = x->rx_buf_len; |
| 613 | if (in_win(g, rxbuf[i], n)) { |
| 614 | uint32_t k; |
| 615 | for (k = 0; k < n; k++) |
| 616 | x->rx_data[k] = g->va[(rxbuf[i] - w->base) + k]; |
| 617 | } else { |
| 618 | n = 0; |
| 619 | } |
| 620 | x->rx_copied = n; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | if (x->tx_done && ck_dmaw_ring_complete(&txr, w, 0, x->frame_len)) { |
| 625 | g->halted = 1; |
| 626 | return CK_GEM_E_DEVICE; |
| 627 | } |
| 628 | /* give the window space back, zeroed, so nothing the device left is read later as if it were new */ |
| 629 | m_zero(g, w->base + next0, w->next - next0); |
| 630 | w->next = next0; |
| 631 | return rc; |
| 632 | } |
| 633 | |
| 634 | void ck_gem_park(struct ck_gem *g) |
| 635 | { |
| 636 | uint32_t ncr = grd(g, CK_GEM_NCR); |
| 637 | ncr &= ~(CK_GEM_NCR_RE | CK_GEM_NCR_TE | CK_GEM_NCR_LLB | CK_GEM_NCR_TSTART); |
| 638 | gwr(g, CK_GEM_NCR, ncr); |
| 639 | } |
| 640 | |
| 641 | /* ---------------- PHY ---------------- */ |
| 642 | |
| 643 | uint32_t ck_gem_mdio_frame_write(uint32_t phy, uint32_t reg, uint16_t val) |
| 644 | { |
| 645 | #ifdef CK_GEM_MUTANT_MDIO_OP |
| 646 | uint32_t op = 2u; /* mutant: the READ op code */ |
| 647 | #else |
| 648 | uint32_t op = 1u; /* MACB_MAN_C22_WRITE */ |
| 649 | #endif |
| 650 | return (1u << 30) | (op << 28) | ((phy & 31u) << 23) | ((reg & 31u) << 18) | (2u << 16) | val; |
| 651 | } |
| 652 | |
| 653 | int ck_gem_mdio_write(const struct ck_rp1_io *io, uint32_t phy, uint32_t reg, uint16_t val) |
| 654 | { |
| 655 | uint32_t i; |
| 656 | if (!io || phy > 31u || reg > 31u) |
| 657 | return CK_GEM_E_ARG; |
| 658 | for (i = 0; i < CK_GEM_MDIO_POLLS; i++) { |
| 659 | if (io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NSR) & CK_GEM_NSR_IDLE) |
| 660 | break; |
| 661 | if (io->delay_us) |
| 662 | io->delay_us(io->ctx, CK_GEM_MDIO_POLL_US); |
| 663 | } |
| 664 | if (i == CK_GEM_MDIO_POLLS) |
| 665 | return CK_GEM_E_TIMEOUT; |
| 666 | io->wr(io->ctx, CK_RP1_ETH + CK_GEM_MAN, ck_gem_mdio_frame_write(phy, reg, val)); |
| 667 | for (i = 0; i < CK_GEM_MDIO_POLLS; i++) { |
| 668 | if (io->rd(io->ctx, CK_RP1_ETH + CK_GEM_NSR) & CK_GEM_NSR_IDLE) |
| 669 | return 0; |
| 670 | if (io->delay_us) |
| 671 | io->delay_us(io->ctx, CK_GEM_MDIO_POLL_US); |
| 672 | } |
| 673 | return CK_GEM_E_TIMEOUT; |
| 674 | } |
| 675 | |
| 676 | void ck_eth_cfg_status_decode(uint32_t st, int *link, uint32_t *speed, int *full_duplex) |
| 677 | { |
| 678 | uint32_t s = (st >> 1) & 3u; |
| 679 | if (link) |
| 680 | *link = (st & 1u) != 0; |
| 681 | if (speed) |
| 682 | *speed = s == 0 ? 10u : s == 1 ? 100u : s == 2 ? 1000u : 0u; |
| 683 | if (full_duplex) |
| 684 | *full_duplex = (st & 8u) != 0; |
| 685 | } |
| 686 | |
| 687 | int ck_phy_link_read(const struct ck_rp1_io *io, uint32_t phy, struct ck_phy_link *l) |
| 688 | { |
| 689 | int e = 0; |
| 690 | if (!io || !l) |
| 691 | return CK_GEM_E_ARG; |
| 692 | *l = (struct ck_phy_link){0}; |
| 693 | e |= ck_gem_mdio_read(io, phy, CK_MII_BMCR, &l->bmcr); |
| 694 | e |= ck_gem_mdio_read(io, phy, CK_MII_BMSR, &l->bmsr_first); /* latched low: the first read reports a past drop */ |
| 695 | e |= ck_gem_mdio_read(io, phy, CK_MII_BMSR, &l->bmsr); |
| 696 | e |= ck_gem_mdio_read(io, phy, CK_MII_ADVERTISE, &l->adv); |
| 697 | e |= ck_gem_mdio_read(io, phy, CK_MII_LPA, &l->lpa); |
| 698 | e |= ck_gem_mdio_read(io, phy, CK_MII_CTRL1000, &l->ctrl1000); |
| 699 | e |= ck_gem_mdio_read(io, phy, CK_MII_STAT1000, &l->stat1000); |
| 700 | if (e) |
| 701 | return CK_GEM_E_TIMEOUT; |
| 702 | l->link = (l->bmsr & CK_MII_BMSR_LSTATUS) != 0; |
| 703 | l->aneg_done = (l->bmsr & CK_MII_BMSR_ANEGCOMPLETE) != 0; |
| 704 | if (!l->link) |
| 705 | return 0; |
| 706 | if (l->bmcr & CK_MII_BMCR_ANENABLE) { |
| 707 | uint16_t gb, com; |
| 708 | if (!l->aneg_done) |
| 709 | return 0; |
| 710 | gb = (uint16_t)(l->stat1000 & (l->ctrl1000 << 2)); /* LPA_1000FULL = ADVERTISE_1000FULL << 2 */ |
| 711 | com = (uint16_t)(l->adv & l->lpa); |
| 712 | if (gb & 0x0800u) { l->speed = 1000; l->full_duplex = 1; } |
| 713 | else if (gb & 0x0400u) { l->speed = 1000; l->full_duplex = 0; } |
| 714 | else if (com & 0x0100u) { l->speed = 100; l->full_duplex = 1; } |
| 715 | else if (com & 0x0080u) { l->speed = 100; l->full_duplex = 0; } |
| 716 | else if (com & 0x0040u) { l->speed = 10; l->full_duplex = 1; } |
| 717 | else if (com & 0x0020u) { l->speed = 10; l->full_duplex = 0; } |
| 718 | } else { |
| 719 | l->speed = (l->bmcr & CK_MII_BMCR_SPEED1000) ? 1000u : (l->bmcr & CK_MII_BMCR_SPEED100) ? 100u : 10u; |
| 720 | l->full_duplex = (l->bmcr & CK_MII_BMCR_FULLDPLX) != 0; |
| 721 | } |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | int ck_phy_rgmii_id_config(const struct ck_rp1_io *io, uint32_t phy, uint16_t *auxctl_before, uint16_t *clk_before) |
| 726 | { |
| 727 | /* Linux bcm54xx_config_clock_delay for PHY_INTERFACE_MODE_RGMII_ID [P1]: |
| 728 | * RX skew: AUXCTL shadow MISC (0x07) read-modify-write with WREN (0x8000) and RGMII_SKEW_EN (0x0100). |
| 729 | * TX delay: shadow CLK_CTL (0x03) |= GTXCLK_EN (1 << 9). */ |
| 730 | uint16_t v = 0, s = 0; |
| 731 | int e; |
| 732 | if (!io) |
| 733 | return CK_GEM_E_ARG; |
| 734 | e = ck_gem_mdio_write(io, phy, 0x18u, (uint16_t)(0x0007u | (0x0007u << 12))); /* select + read-select MISC */ |
| 735 | e |= ck_gem_mdio_read(io, phy, 0x18u, &v); |
| 736 | if (e) |
| 737 | return CK_GEM_E_TIMEOUT; |
| 738 | if (auxctl_before) |
| 739 | *auxctl_before = v; |
| 740 | v = (uint16_t)(v | 0x8000u | 0x0100u); |
| 741 | e = ck_gem_mdio_write(io, phy, 0x18u, (uint16_t)(0x0007u | v)); |
| 742 | e |= ck_gem_mdio_write(io, phy, 0x1cu, (uint16_t)(0x03u << 10)); /* shadow read select CLK_CTL */ |
| 743 | e |= ck_gem_mdio_read(io, phy, 0x1cu, &s); |
| 744 | if (e) |
| 745 | return CK_GEM_E_TIMEOUT; |
| 746 | s &= 0x3ffu; |
| 747 | if (clk_before) |
| 748 | *clk_before = s; |
| 749 | s |= (uint16_t)(1u << 9); |
| 750 | e = ck_gem_mdio_write(io, phy, 0x1cu, (uint16_t)(0x8000u | (0x03u << 10) | s)); |
| 751 | return e ? CK_GEM_E_TIMEOUT : 0; |
| 752 | } |
| 753 | |
| 754 | int ck_phy_loopback_enter(const struct ck_rp1_io *io, uint32_t phy, uint32_t speed, uint16_t *bmcr_saved, uint16_t *bmsr_out) |
| 755 | { |
| 756 | uint16_t cur = 0, bmsr = 0; |
| 757 | uint32_t i; |
| 758 | uint16_t want; |
| 759 | if (!io || (speed != 10 && speed != 100 && speed != 1000)) |
| 760 | return CK_GEM_E_ARG; |
| 761 | if (ck_gem_mdio_read(io, phy, CK_MII_BMCR, &cur)) |
| 762 | return CK_GEM_E_TIMEOUT; |
| 763 | if (bmcr_saved) |
| 764 | *bmcr_saved = cur; |
| 765 | #ifdef CK_GEM_MUTANT_PHYLB_BIT |
| 766 | want = CK_MII_BMCR_FULLDPLX | 0x2000u; |
| 767 | #else |
| 768 | want = (uint16_t)(CK_MII_BMCR_LOOPBACK | CK_MII_BMCR_FULLDPLX); |
| 769 | #endif |
| 770 | if (speed == 1000) |
| 771 | want |= CK_MII_BMCR_SPEED1000; |
| 772 | else if (speed == 100) |
| 773 | want |= CK_MII_BMCR_SPEED100; |
| 774 | if (ck_gem_mdio_write(io, phy, CK_MII_BMCR, want)) |
| 775 | return CK_GEM_E_TIMEOUT; |
| 776 | for (i = 0; i < 100u; i++) { /* genphy_loopback waits up to 500 ms for link, 5 ms apart */ |
| 777 | (void)ck_gem_mdio_read(io, phy, CK_MII_BMSR, &bmsr); |
| 778 | (void)ck_gem_mdio_read(io, phy, CK_MII_BMSR, &bmsr); |
| 779 | if (bmsr & CK_MII_BMSR_LSTATUS) |
| 780 | break; |
| 781 | if (io->delay_us) |
| 782 | io->delay_us(io->ctx, 5000u); |
| 783 | } |
| 784 | if (bmsr_out) |
| 785 | *bmsr_out = bmsr; |
| 786 | return (bmsr & CK_MII_BMSR_LSTATUS) ? 0 : CK_GEM_E_TIMEOUT; |
| 787 | } |
| 788 | |
| 789 | int ck_phy_loopback_exit(const struct ck_rp1_io *io, uint32_t phy, uint16_t bmcr_saved) |
| 790 | { |
| 791 | uint16_t v = (uint16_t)(bmcr_saved & ~CK_MII_BMCR_LOOPBACK); |
| 792 | if (!io) |
| 793 | return CK_GEM_E_ARG; |
| 794 | if (v & CK_MII_BMCR_ANENABLE) |
| 795 | v |= CK_MII_BMCR_ANRESTART; |
| 796 | return ck_gem_mdio_write(io, phy, CK_MII_BMCR, v) ? CK_GEM_E_TIMEOUT : 0; |
| 797 | } |
| 798 | |
| 799 | /* ================= image glue (CK_PI5_NET only) ================= */ |
| 800 | #ifdef CK_PI5_NET |
| 801 | #include "ck.h" |
| 802 | |
| 803 | static int glue_bme(void *ctx, int on) { return ck_pi5gem_bme_set((const struct ck_pi5pcie_io *)ctx, on); } |
| 804 | static void glue_barrier(void) { ck_mb(); } |
| 805 | |
| 806 | /* Locally administered unicast (bit 1 of the first octet set), not the board's own address: this cut does not read |
| 807 | * the OTP MAC (UNVERIFIED which one the board owns). */ |
| 808 | static const uint8_t k_mac[6] = {0x02, 0x41, 0x49, 0x45, 0x4e, 0x03}; |
| 809 | |
| 810 | static const char *verdict(int ok) { return ok ? "PASS" : "FAIL"; } |
| 811 | |
| 812 | static void print_xfer(const char *tag, int rc, const struct ck_gem_xfer *x, const uint8_t *tx) |
| 813 | { |
| 814 | uint32_t diff = x->rx_done ? ck_gem_frame_diff(tx, x->frame_len, x->rx_data, x->rx_copied) : 0xffffffffu; |
| 815 | ck_printf("pi5_gem: %s rc=%d tx_done=%u rx_done=%u rx_frames=%u tx_ctrl=0x%08x rx_word0=0x%08x rx_ctrl=0x%08x rx_len=%u frame_diff=%u\n", |
| 816 | tag, rc, x->tx_done, x->rx_done, x->rx_frames, x->tx_ctrl, x->rx_word0, x->rx_ctrl, x->rx_len, |
| 817 | x->rx_done ? diff : 0xffffffffu); |
| 818 | ck_printf("pi5_gem: %s regs: tsr=0x%08x rsr=0x%08x isr=0x%08x ncr_run=0x%08x ncfgr=0x%08x dmacfg=0x%08x ncr_end=0x%08x tsr_end=0x%08x bme_on_for_us=%u bme_off_at_end=%u tx_bus=0x%llx rx_bus=0x%llx\n", |
| 819 | tag, x->tsr, x->rsr, x->isr, x->ncr_run, x->ncfgr_run, x->dmacfg_run, x->ncr_end, x->tsr_end, |
| 820 | x->bme_enabled_for_us, x->bme_off_at_end, (unsigned long long)x->tx_bus, (unsigned long long)x->rx_bus); |
| 821 | } |
| 822 | |
| 823 | static void print_link(const char *tag, const struct ck_phy_link *l, uint32_t eth_status) |
| 824 | { |
| 825 | int el = 0, efd = 0; |
| 826 | uint32_t es = 0; |
| 827 | ck_eth_cfg_status_decode(eth_status, &el, &es, &efd); |
| 828 | ck_printf("pi5_gem: %s: BMCR=0x%04x BMSR=0x%04x/0x%04x ADV=0x%04x LPA=0x%04x CTRL1000=0x%04x STAT1000=0x%04x link=%u aneg_done=%u speed=%u duplex=%s | eth_cfg STATUS=0x%08x link=%u speed=%u duplex=%s\n", |
| 829 | tag, l->bmcr, l->bmsr_first, l->bmsr, l->adv, l->lpa, l->ctrl1000, l->stat1000, l->link, l->aneg_done, l->speed, |
| 830 | l->full_duplex ? "full" : "half", eth_status, el, es, efd ? "full" : "half"); |
| 831 | } |
| 832 | |
| 833 | static uint32_t pci_cmd(const struct ck_pi5gem_ctx *c) |
| 834 | { |
| 835 | uint32_t v = 0; |
| 836 | (void)ck_pi5pcie_cfg_read32(c->rc, 1, 0, 0, 4, &v); |
| 837 | return v; |
| 838 | } |
| 839 | |
| 840 | static uint32_t eth_status(const struct ck_rp1_io *rio) { return rio->rd(rio->ctx, CK_RP1_ETH_CFG + 0x04u); } |
| 841 | |
| 842 | /* Wait (bounded) for the PHY to report link with autonegotiation complete. */ |
| 843 | static int wait_link(const struct ck_pi5gem_ctx *c, uint32_t max_ms, struct ck_phy_link *l) |
| 844 | { |
| 845 | uint32_t t; |
| 846 | for (t = 0; t <= max_ms; t += 100) { |
| 847 | if (ck_phy_link_read(c->rio, c->phy_addr, l) == 0 && l->link && l->aneg_done) |
| 848 | return 1; |
| 849 | if (t < max_ms) |
| 850 | c->rio->delay_us(c->rio->ctx, 100000u); |
| 851 | } |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | static uint32_t canary_changed(volatile const uint8_t *p, uint32_t n, uint8_t pat, uint32_t *first) |
| 856 | { |
| 857 | uint32_t i, c = 0; |
| 858 | *first = 0xffffffffu; |
| 859 | for (i = 0; i < n; i++) |
| 860 | if (p[i] != pat) { |
| 861 | if (c == 0) |
| 862 | *first = i; |
| 863 | c++; |
| 864 | } |
| 865 | return c; |
| 866 | } |
| 867 | |
| 868 | void ck_pi5gem_run(const struct ck_pi5gem_ctx *c) |
| 869 | { |
| 870 | struct ck_gem g; |
| 871 | struct ck_gem_xfer x; |
| 872 | struct ck_phy_link lk; |
| 873 | uint8_t txf[64], tpa[4] = {192, 168, 1, 1}; |
| 874 | uint16_t aux0 = 0, clk0 = 0, bmcr0 = 0, bmsr_lb = 0; |
| 875 | int rc, link_up, llb_ok = 0, phylb_ok = 0, ok; |
| 876 | uint32_t n; |
| 877 | volatile uint8_t *va = (volatile uint8_t *)c->bounce; |
| 878 | |
| 879 | ck_printf("pi5_gem: cut 3: one frame through the RP1 GEM with descriptors only inside the bounce window; software-only DMA confinement (Pi 5, weaker than the Spark)\n"); |
| 880 | if (!c->hw_window_confined) { |
| 881 | ck_printf("pi5_gem: STOP root-complex inbound window is not CONFINED; bus-master stays off, no GEM DMA\n"); |
| 882 | return; |
| 883 | } |
| 884 | ck_gem_init(&g, c->rio, c->dw, va, glue_bme, (void *)c->rc, glue_barrier, 1); |
| 885 | rc = ck_gem_probe(c->rio, &g.caps); |
| 886 | ck_printf("pi5_gem: caps rc=%d MID=0x%08x DCFG1=0x%08x DCFG2=0x%08x DCFG6=0x%08x gem=%u daw64=%u queue_mask=0x%x queues=%u desc_bytes=%u dbw_code=%u\n", |
| 887 | rc, g.caps.mid, g.caps.dcfg1, g.caps.dcfg2, g.caps.dcfg6, g.caps.is_gem, g.caps.daw64, g.caps.queue_mask, |
| 888 | g.caps.nqueues, g.caps.desc_bytes, g.caps.dbw); |
| 889 | if (rc) { |
| 890 | ck_printf("pi5_gem: STOP not a GEM; nothing programmed\n"); |
| 891 | return; |
| 892 | } |
| 893 | ck_printf("pi5_gem: state before: NCR=0x%08x NCFGR=0x%08x USRIO=0x%08x DMACFG=0x%08x AMP=0x%08x TBQP=0x%08x RBQP=0x%08x eth_cfg STATUS=0x%08x CLKGEN=0x%08x rp1 pci cmd=0x%08x\n", |
| 894 | c->rio->rd(0, CK_RP1_ETH + CK_GEM_NCR), c->rio->rd(0, CK_RP1_ETH + CK_GEM_NCFGR), |
| 895 | c->rio->rd(0, CK_RP1_ETH + CK_GEM_USRIO), c->rio->rd(0, CK_RP1_ETH + CK_GEM_DMACFG), |
| 896 | c->rio->rd(0, CK_RP1_ETH + CK_GEM_AMP), c->rio->rd(0, CK_RP1_ETH + CK_GEM_TBQP), |
| 897 | c->rio->rd(0, CK_RP1_ETH + CK_GEM_RBQP), eth_status(c->rio), c->rio->rd(0, CK_RP1_ETH_CFG + 0x14u), |
| 898 | pci_cmd(c)); |
| 899 | |
| 900 | /* ---- 1. link status first, nothing assumed ---- */ |
| 901 | rc = ck_phy_link_read(c->rio, c->phy_addr, &lk); |
| 902 | print_link("link (first read, read-only)", &lk, eth_status(c->rio)); |
| 903 | link_up = wait_link(c, 6000, &lk); |
| 904 | print_link(link_up ? "link UP" : "link NOT UP after 6000 ms (no cable or no partner, or autoneg not complete)", &lk, eth_status(c->rio)); |
| 905 | ck_printf("pi5_gem: LINK %s\n", link_up ? "FOUND" : "ABSENT"); |
| 906 | |
| 907 | /* ---- PHY RGMII-ID delays as Linux sets them (broadcom.c), then read back ---- */ |
| 908 | rc = ck_phy_rgmii_id_config(c->rio, c->phy_addr, &aux0, &clk0); |
| 909 | { |
| 910 | uint16_t a1 = 0, s1 = 0; |
| 911 | (void)ck_gem_mdio_write(c->rio, c->phy_addr, 0x18u, (uint16_t)(0x0007u | (0x0007u << 12))); |
| 912 | (void)ck_gem_mdio_read(c->rio, c->phy_addr, 0x18u, &a1); |
| 913 | (void)ck_gem_mdio_write(c->rio, c->phy_addr, 0x1cu, (uint16_t)(0x03u << 10)); |
| 914 | (void)ck_gem_mdio_read(c->rio, c->phy_addr, 0x1cu, &s1); |
| 915 | ck_printf("pi5_gem: phy rgmii-id delays rc=%d AUXCTL_MISC before=0x%04x after=0x%04x CLK_CTL before=0x%03x after=0x%03x\n", |
| 916 | rc, aux0, a1, clk0, s1 & 0x3ff); |
| 917 | } |
| 918 | |
| 919 | /* ---- 2. GEM local loopback ---- */ |
| 920 | ck_gem_frame_build(CK_GEMF_RAW, k_mac, 1, tpa, txf, sizeof txf); |
| 921 | x = (struct ck_gem_xfer){0}; |
| 922 | x.mode = CK_GEMX_LLB; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 2; x.rx_buf_len = 1536; |
| 923 | x.tx_wait_us = 20000; x.rx_wait_us = 20000; |
| 924 | for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; |
| 925 | rc = ck_gem_xfer(&g, &x); |
| 926 | print_xfer("loopback gem-local(NCR.LLB) seq=1", rc, &x, txf); |
| 927 | llb_ok = rc == 0 && x.tx_done && x.rx_done && x.rx_frames == 1 && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 && |
| 928 | x.rx_len == 60; |
| 929 | ck_printf("pi5_gem: LOOPBACK gem-local %s (sent seq=1 60 bytes, received %u bytes, byte compare %s)\n", verdict(llb_ok), x.rx_len, |
| 930 | x.rx_done && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 ? "identical" : "different or none"); |
| 931 | |
| 932 | /* ---- 3. PHY loopback (BMCR bit 14) ---- */ |
| 933 | ck_gem_frame_build(CK_GEMF_RAW, k_mac, 2, tpa, txf, sizeof txf); |
| 934 | rc = ck_phy_loopback_enter(c->rio, c->phy_addr, 1000, &bmcr0, &bmsr_lb); |
| 935 | ck_printf("pi5_gem: phy loopback enter rc=%d BMCR_was=0x%04x BMSR_now=0x%04x (link in loopback=%u)\n", rc, bmcr0, bmsr_lb, |
| 936 | (bmsr_lb & CK_MII_BMSR_LSTATUS) != 0); |
| 937 | x = (struct ck_gem_xfer){0}; |
| 938 | x.mode = CK_GEMX_PHYLB; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 2; x.rx_buf_len = 1536; |
| 939 | x.tx_wait_us = 20000; x.rx_wait_us = 50000; |
| 940 | for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; |
| 941 | rc = ck_gem_xfer(&g, &x); |
| 942 | print_xfer("loopback phy(BMCR.LOOPBACK) seq=2", rc, &x, txf); |
| 943 | phylb_ok = rc == 0 && x.tx_done && x.rx_done && x.rx_frames == 1 && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 && |
| 944 | x.rx_len == 60; |
| 945 | ck_printf("pi5_gem: LOOPBACK phy %s (sent seq=2 60 bytes, received %u bytes, byte compare %s)\n", verdict(phylb_ok), x.rx_len, |
| 946 | x.rx_done && ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied) == 0 ? "identical" : "different or none"); |
| 947 | rc = ck_phy_loopback_exit(c->rio, c->phy_addr, bmcr0); |
| 948 | ck_printf("pi5_gem: phy loopback exit rc=%d BMCR restored to 0x%04x\n", rc, (uint16_t)(bmcr0 & ~CK_MII_BMCR_LOOPBACK)); |
| 949 | |
| 950 | /* ---- 4. one frame on the wire, only if a link exists ---- */ |
| 951 | if (link_up) { |
| 952 | struct ck_phy_link l2; |
| 953 | uint32_t speed, fd, es = 0; |
| 954 | int el = 0, efd = 0; |
| 955 | link_up = wait_link(c, 8000, &l2); |
| 956 | print_link(link_up ? "link after loopback (UP)" : "link after loopback (NOT UP)", &l2, eth_status(c->rio)); |
| 957 | if (link_up) { |
| 958 | speed = l2.speed; |
| 959 | fd = (uint32_t)l2.full_duplex; |
| 960 | ck_eth_cfg_status_decode(eth_status(c->rio), &el, &es, &efd); |
| 961 | if (!speed) { speed = es; fd = (uint32_t)efd; } |
| 962 | ck_gem_frame_build(CK_GEMF_ARP, k_mac, 3, tpa, txf, sizeof txf); |
| 963 | x = (struct ck_gem_xfer){0}; |
| 964 | x.mode = CK_GEMX_WIRE; x.speed = speed ? speed : 100; x.full_duplex = (int)fd; x.frame = txf; x.frame_len = 60; |
| 965 | x.rx_count = 3; x.rx_buf_len = 1536; x.tx_wait_us = 50000; x.rx_wait_us = 400000; |
| 966 | for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; |
| 967 | rc = ck_gem_xfer(&g, &x); |
| 968 | print_xfer("wire arp-probe 192.168.1.1 seq=3", rc, &x, txf); |
| 969 | ck_printf("pi5_gem: WIRE tx %s (ARP probe 60 bytes at %u Mbit %s duplex, TX complete=%u, tx_ctrl error bits=0x%08x)\n", |
| 970 | verdict(rc == 0 && x.tx_done && !(x.tx_ctrl & (CK_GEMD_TX_ERROR | CK_GEMD_TX_UNDERRUN | CK_GEMD_TX_BUF_EXHAUSTED))), |
| 971 | x.speed, x.full_duplex ? "full" : "half", x.tx_done, x.tx_ctrl & (CK_GEMD_TX_ERROR | CK_GEMD_TX_UNDERRUN | CK_GEMD_TX_BUF_EXHAUSTED)); |
| 972 | if (x.rx_done) { |
| 973 | ck_printf("pi5_gem: WIRE rx (bonus) frames=%u first: len=%u dst=%02x:%02x:%02x:%02x:%02x:%02x src=%02x:%02x:%02x:%02x:%02x:%02x type=%02x%02x\n", |
| 974 | x.rx_frames, x.rx_len, x.rx_data[0], x.rx_data[1], x.rx_data[2], x.rx_data[3], x.rx_data[4], x.rx_data[5], |
| 975 | x.rx_data[6], x.rx_data[7], x.rx_data[8], x.rx_data[9], x.rx_data[10], x.rx_data[11], x.rx_data[12], x.rx_data[13]); |
| 976 | } else { |
| 977 | ck_printf("pi5_gem: WIRE rx (bonus) none within 400 ms\n"); |
| 978 | } |
| 979 | } |
| 980 | } else { |
| 981 | ck_printf("pi5_gem: WIRE SKIPPED (no link partner found)\n"); |
| 982 | } |
| 983 | |
| 984 | /* ---- 5. the confinement proof (ADR 0020 test 5): descriptors deliberately OUTSIDE the window ---- */ |
| 985 | { |
| 986 | int mode = llb_ok ? CK_GEMX_LLB : phylb_ok ? CK_GEMX_PHYLB : 0; |
| 987 | uint64_t cphys = 0, cphys2 = 0; |
| 988 | volatile uint8_t *can = 0, *can2 = 0; |
| 989 | struct ck_dmaw scratch = *c->dw; |
| 990 | int vr; |
| 991 | if (!mode) { |
| 992 | ck_printf("pi5_gem: CONFINEMENT SKIPPED: neither loopback delivered a frame, so there is no frame to push at an outside address; UNVERIFIED\n"); |
| 993 | } else { |
| 994 | if (mode == CK_GEMX_PHYLB) { |
| 995 | rc = ck_phy_loopback_enter(c->rio, c->phy_addr, 1000, &bmcr0, &bmsr_lb); |
| 996 | ck_printf("pi5_gem: confinement: phy loopback re-entered rc=%d\n", rc); |
| 997 | } |
| 998 | can = (volatile uint8_t *)ck_dma_alloc(4096, 4096, &cphys); |
| 999 | can2 = (volatile uint8_t *)ck_dma_alloc(4096, 4096, &cphys2); |
| 1000 | ok = can && can2 && cphys && cphys2 && cphys != cphys2 && |
| 1001 | (cphys + 4096 <= c->bounce_phys || cphys >= c->bounce_phys + c->dw->size) && |
| 1002 | (cphys2 + 4096 <= c->bounce_phys || cphys2 >= c->bounce_phys + c->dw->size); |
| 1003 | if (!ok) { |
| 1004 | ck_printf("pi5_gem: CONFINEMENT SKIPPED: could not get two private pages outside the window (can=%llx can2=%llx); UNVERIFIED\n", |
| 1005 | (unsigned long long)cphys, (unsigned long long)cphys2); |
| 1006 | } else { |
| 1007 | uint32_t i, changed, first; |
| 1008 | for (i = 0; i < 4096; i++) { can[i] = 0xa5; can2[i] = 0; } |
| 1009 | vr = ck_dmaw_validate(&scratch, cphys, 64); |
| 1010 | ck_printf("pi5_gem: confinement setup: canary page cpu=0x%llx (outside window 0x%llx..+0x%llx); validator on a scratch copy of the window says %d (2 = E_RANGE, refused) for a 64 byte buffer there\n", |
| 1011 | (unsigned long long)cphys, (unsigned long long)c->bounce_phys, (unsigned long long)c->dw->size, vr); |
| 1012 | if (vr != CK_DMAW_E_RANGE) { |
| 1013 | ck_printf("pi5_gem: CONFINEMENT STOP validator did not refuse the outside address; nothing run\n"); |
| 1014 | } else { |
| 1015 | /* A. device WRITE outside: RX buffer 64 bytes, one slot, the page is ours and bounded at 64 bytes */ |
| 1016 | ck_gem_frame_build(CK_GEMF_RAW, k_mac, 4, tpa, txf, sizeof txf); |
| 1017 | x = (struct ck_gem_xfer){0}; |
| 1018 | x.mode = mode; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 1; x.rx_buf_len = 64; |
| 1019 | x.tx_wait_us = 20000; x.rx_wait_us = 50000; x.raw_rx_outside = cphys; |
| 1020 | for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; |
| 1021 | ck_printf("pi5_gem: CONFINEMENT A: the RX descriptor points at 0x%llx outside the window, buffer size 64 bytes (the device can write at most 64 bytes there)\n", |
| 1022 | (unsigned long long)cphys); |
| 1023 | rc = ck_gem_xfer(&g, &x); |
| 1024 | print_xfer("confinement A (rx write outside) seq=4", rc, &x, txf); |
| 1025 | changed = canary_changed(can, 4096, 0xa5, &first); |
| 1026 | ck_printf("pi5_gem: CONFINEMENT A result: canary bytes changed=%u first_changed_offset=%d device_says_rx_done=%u hresp=%u\n", |
| 1027 | changed, (int)first, x.rx_done, (x.isr & CK_GEM_ISR_HRESP) != 0); |
| 1028 | ck_printf("pi5_gem: CONFINEMENT A %s\n", changed == 0 ? "BLOCKED: the write did not reach the outside page" : |
| 1029 | "NOT BLOCKED: the device wrote outside the window (hardware backstop NOT effective)"); |
| 1030 | /* B. device READ outside: TX descriptor points at a page holding a different frame */ |
| 1031 | ck_gem_frame_build(CK_GEMF_RAW, k_mac, 0xdead0005u, tpa, (uint8_t *)can2, 64); |
| 1032 | ck_gem_frame_build(CK_GEMF_RAW, k_mac, 5, tpa, txf, sizeof txf); |
| 1033 | x = (struct ck_gem_xfer){0}; |
| 1034 | x.mode = mode; x.speed = 1000; x.full_duplex = 1; x.frame = txf; x.frame_len = 60; x.rx_count = 1; x.rx_buf_len = 1536; |
| 1035 | x.tx_wait_us = 20000; x.rx_wait_us = 50000; x.raw_tx_outside = cphys2; |
| 1036 | for (n = 0; n < 6; n++) x.mac[n] = k_mac[n]; |
| 1037 | ck_printf("pi5_gem: CONFINEMENT B: the TX descriptor points at 0x%llx outside the window, which holds a different frame (marker seq=0xdead0005)\n", |
| 1038 | (unsigned long long)cphys2); |
| 1039 | rc = ck_gem_xfer(&g, &x); |
| 1040 | print_xfer("confinement B (tx read outside) seq=5", rc, &x, txf); |
| 1041 | { |
| 1042 | uint8_t outf[64]; |
| 1043 | uint32_t dm = 1, dt = 1; |
| 1044 | ck_gem_frame_build(CK_GEMF_RAW, k_mac, 0xdead0005u, tpa, outf, sizeof outf); |
| 1045 | if (x.rx_done) { |
| 1046 | dm = ck_gem_frame_diff(outf, 60, x.rx_data, x.rx_copied); |
| 1047 | dt = ck_gem_frame_diff(txf, 60, x.rx_data, x.rx_copied); |
| 1048 | } |
| 1049 | ck_printf("pi5_gem: CONFINEMENT B result: rx_done=%u diff_vs_outside_marker=%u diff_vs_inside_frame=%u tsr_bex=%u hresp=%u\n", |
| 1050 | x.rx_done, x.rx_done ? dm : 0xffffffffu, x.rx_done ? dt : 0xffffffffu, (x.tsr & CK_GEM_TSR_BEX) != 0, |
| 1051 | (x.isr & CK_GEM_ISR_HRESP) != 0); |
| 1052 | ck_printf("pi5_gem: CONFINEMENT B %s\n", (x.rx_done && dm == 0) ? "NOT BLOCKED: the device read the outside page (hardware backstop NOT effective for reads)" : |
| 1053 | "BLOCKED: the outside page was not delivered as the frame"); |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | if (mode == CK_GEMX_PHYLB) { |
| 1059 | rc = ck_phy_loopback_exit(c->rio, c->phy_addr, bmcr0); |
| 1060 | ck_printf("pi5_gem: phy loopback exit rc=%d\n", rc); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | ck_gem_park(&g); |
| 1065 | ck_printf("pi5_gem: done: GEM RE/TE/LLB off, halted=%u bme_on=%u rp1 pci cmd=0x%08x NCR=0x%08x\n", g.halted, g.bme_on, |
| 1066 | pci_cmd(c), c->rio->rd(0, CK_RP1_ETH + CK_GEM_NCR)); |
| 1067 | } |
| 1068 | #endif |