interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / x86 / tg3_x86.c

889 lines · 30904 bytes Raw Blame History
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/* tg3_x86.c -- the 2017 iMac's Broadcom BCM57766 Ethernet chip (PCI
18 * 14e4:1686), polled, received frames never read. The default image uses
19 * it for link detection only; frame transmit (ck_x86_tg3_send,
20 * ck_x86_tg3_drain) is compiled only into the opt-in lab image
21 * (CK_X86_REPORT=1), for the lab-only network report.
22 *
23 * Written from Broadcom's register layout as the Linux tg3 driver documents
24 * it (no code taken from it); the order of every step follows tg3's
25 * bring-up for this chip family (57765 class, PCIe, CPMU, no NVRAM, no
26 * management firmware), resolved to the one chip on this machine. QEMU has
27 * no model of it, so the steps print what they found and the first one that
28 * fails stops the driver with its name on the screen.
29 *
30 * Bring-up: register access set up through config space; the station
31 * address read from what Apple's firmware left (the bootcode's SRAM mailbox,
32 * else MAC_ADDR_0) and never printed; PHY reset; every engine stopped and a
33 * core reset (no BAR access for 240 us around it); bootcode handshake (a
34 * timeout is allowed, as tg3 allows it); then buffer manager, rings, host
35 * coalescing, DMA engines and MAC enabled in tg3's order, all pointing at
36 * memory this driver owns; autonegotiation started (10/100/1000).
37 *
38 * Interrupts stay masked (MISC_HOST_CTRL MASK_PCI_INT, every interrupt
39 * mailbox 1, no MSI). Receive is set up only because the chip expects valid
40 * receive rings; nothing reads them, so incoming frames are dropped once the
41 * 64 receive buffers are used. Transmit: one 16-byte descriptor per frame,
42 * frames copied into one of 32 driver buffers, completion read from the
43 * status block (forced with HOSTCC "now"). */
44#include "../core/ck_internal.h"
45#include "../mm/frames.h"
46#include "x86.h"
47#include "bench_proto.h"
48
49/* PCI config space (ECAM) */
50#define CFG_MISC_HOST_CTRL 0x68
51#define CFG_PCISTATE 0x70
52#define CFG_REG_BASE 0x78
53#define CFG_MEM_WIN_BASE 0x7c
54#define CFG_PRODID_ASICREV 0xfc
55#define MHC_VALUE 0x29au /* MASK_PCI_INT | WORD_SWAP | PCISTATE_RW | INDIR_ACCESS | TAGGED_STATUS */
56
57/* BAR0 registers */
58#define MEM_WIN_BASE 0x7c
59#define MEM_WIN_DATA 0x84
60#define MBOX_INT0 0x204
61#define MBOX_RX_STD_PROD 0x26c
62#define MBOX_RX_JMB_PROD 0x274
63#define MBOX_RX_RET_CONS0 0x284
64#define MBOX_TX_PROD0 0x304
65#define MAC_MODE 0x400
66#define MAC_STATUS 0x404
67#define MAC_EVENT 0x408
68#define MAC_LED_CTRL 0x40c
69#define MAC_ADDR0_HI 0x410
70#define MAC_ADDR0_LO 0x414
71#define MAC_TX_BACKOFF 0x438
72#define MAC_RX_MTU 0x43c
73#define MAC_MI_COM 0x44c
74#define MAC_MI_STAT 0x450
75#define MAC_MI_MODE 0x454
76#define MAC_TX_MODE 0x45c
77#define MAC_TX_STATUS 0x460
78#define MAC_TX_LENGTHS 0x464
79#define MAC_RX_MODE 0x468
80#define MAC_HASH0 0x470
81#define MAC_RCV_RULE0 0x480
82#define MAC_RCV_RULE_CFG 0x500
83#define MAC_LOW_WMARK_MAX_RX 0x504
84#define SNDDATAI_MODE 0x0c00
85#define SNDDATAI_STATSCTRL 0x0c08
86#define SNDDATAI_STATSENAB 0x0c0c
87#define SNDDATAC_MODE 0x1000
88#define SNDBDS_MODE 0x1400
89#define SNDBDI_MODE 0x1800
90#define SNDBDC_MODE 0x1c00
91#define RCVLPC_MODE 0x2000
92#define RCVLPC_CONFIG 0x2010
93#define RCVLPC_STATSCTRL 0x2014
94#define RCVLPC_STATS_ENABLE 0x2018
95#define RCVDBDI_MODE 0x2400
96#define RCVDBDI_JUMBO_FLAGS 0x2448
97#define RCVDBDI_STD_BD 0x2450
98#define RCVDCC_MODE 0x2800
99#define RCVBDI_MODE 0x2c00
100#define RCVBDI_STD_THRESH 0x2c18
101#define RCVBDI_JUMBO_THRESH 0x2c1c
102#define STD_REPLENISH_LWM 0x2d00
103#define JMB_REPLENISH_LWM 0x2d04
104#define RCVCC_MODE 0x3000
105#define CPMU_LSPD_10MB_CLK 0x3604
106#define CPMU_PADRNG_CTL 0x3668
107#define HOSTCC_MODE 0x3c00
108#define HOSTCC_STATUS 0x3c04
109#define HOSTCC_RXCOL_TICKS 0x3c08
110#define HOSTCC_TXCOL_TICKS 0x3c0c
111#define HOSTCC_RXMAX_FRAMES 0x3c10
112#define HOSTCC_TXMAX_FRAMES 0x3c14
113#define HOSTCC_RXCOAL_MAXF_INT 0x3c20
114#define HOSTCC_TXCOAL_MAXF_INT 0x3c24
115#define HOSTCC_STATUS_BLK 0x3c38
116#define HOSTCC_VEC1 0x3d80
117#define MEMARB_MODE 0x4000
118#define BUFMGR_MODE 0x4400
119#define BUFMGR_STATUS 0x4404
120#define BUFMGR_MB_RDMA_LOW 0x4410
121#define BUFMGR_MB_MACRX_LOW 0x4414
122#define BUFMGR_MB_HIGH 0x4418
123#define BUFMGR_DMA_LOW 0x4434
124#define BUFMGR_DMA_HIGH 0x4438
125#define RDMAC_MODE 0x4800
126#define RDMAC_STATUS 0x4804
127#define RDMA_RSRVCTRL 0x4900
128#define WDMAC_MODE 0x4c00
129#define WDMAC_STATUS 0x4c04
130#define FTQ_RESET 0x5c00
131#define DMA_RW_CTRL 0x6c
132#define GRC_MODE 0x6800
133#define GRC_MISC_CFG 0x6804
134#define GRC_LOCAL_CTRL 0x6808
135#define GRC_FASTBOOT_PC 0x6894
136#define NVRAM_SWARB 0x7020
137#define PCIE_DL_FTSMAX 0x7c0c
138
139/* NIC SRAM (through the memory window) */
140#define SRAM_SEND_RCB 0x100
141#define SRAM_RET_RCB 0x200
142#define SRAM_FW_MBOX 0xb50
143#define SRAM_DATA_SIG 0xb54
144#define SRAM_DATA_CFG 0xb58
145#define SRAM_MAC_HI 0xc14
146#define SRAM_MAC_LO 0xc18
147#define SRAM_RX_STD_BDCACHE 0x6000
148#define FW_MAGIC 0x4b657654u
149
150/* MII registers of the BCM57765 PHY (MDIO address 1) */
151#define PHY_ADDR 1
152#define MII_BMCR 0
153#define MII_BMSR 1
154#define MII_ADVERTISE 4
155#define MII_CTRL1000 9
156#define PHY_DSP_FIFO 0x10
157#define PHY_AUXCTL 0x18
158#define PHY_AUX_STAT 0x19
159#define PHY_ISTAT 0x1a
160#define PHY_IMASK 0x1b
161#define PHY_SHADOW 0x1c
162
163#define TX_RING 512u
164#define RX_STD 512u
165#define RX_RET 1024u
166#define RX_POSTED 64u
167#define TX_BUFS 32u
168#define BUF_SZ 2048u
169#define RX_BUF_SZ 1536u
170
171/* Driver memory, one allocation, below 4 GiB: */
172#define OFF_STATUS 0x0000u /* 1 page */
173#define OFF_TX_RING 0x1000u /* 512 x 16 = 2 pages */
174#define OFF_RX_STD 0x3000u /* 512 x 32 = 4 pages */
175#define OFF_RX_RET 0x7000u /* 1024 x 32 = 8 pages */
176#define OFF_RX_BUF 0xf000u /* 64 x 2 KiB = 32 pages */
177#define OFF_TX_BUF (OFF_RX_BUF + RX_POSTED * BUF_SZ) /* 32 x 2 KiB = 16 pages */
178#define DMA_BYTES (OFF_TX_BUF + TX_BUFS * BUF_SZ)
179#define DMA_PAGES (DMA_BYTES / CK_PAGE)
180
181static volatile uint8_t *regs, *cfg;
182static uint64_t hz, dma;
183static uint8_t mac[6];
184static int ready, link_up;
185static unsigned tx_prod, pcie_cap;
186static uint32_t mac_mode;
187
188static void udelay(uint64_t us)
189{
190 uint64_t t0 = ck_rdtsc(), n = hz / 1000000 * us + 1;
191 while (ck_rdtsc() - t0 < n)
192 ck_pause();
193}
194static uint32_t rd(uint32_t off) { return *(volatile uint32_t *)(regs + off); }
195static void wr(uint32_t off, uint32_t v) { *(volatile uint32_t *)(regs + off) = v; }
196static void wrf(uint32_t off, uint32_t v)
197{
198 wr(off, v);
199 (void)rd(off);
200}
201static uint32_t cr32(uint32_t off) { return *(volatile uint32_t *)(cfg + off); }
202static uint16_t cr16(uint32_t off) { return *(volatile uint16_t *)(cfg + off); }
203static void cw32(uint32_t off, uint32_t v) { *(volatile uint32_t *)(cfg + off) = v; }
204static void cw16(uint32_t off, uint16_t v) { *(volatile uint16_t *)(cfg + off) = v; }
205
206static void sram_w(uint32_t a, uint32_t v)
207{
208 wrf(MEM_WIN_BASE, a);
209 wrf(MEM_WIN_DATA, v);
210 wrf(MEM_WIN_BASE, 0);
211}
212static uint32_t sram_r(uint32_t a)
213{
214 wrf(MEM_WIN_BASE, a);
215 uint32_t v = rd(MEM_WIN_DATA);
216 wrf(MEM_WIN_BASE, 0);
217 return v;
218}
219
220/* Clears `bit` and waits for it to read 0 (1000 x 100 us). 0 on success. */
221static int stop_block(uint32_t off, uint32_t bit)
222{
223 wrf(off, rd(off) & ~bit);
224 for (unsigned i = 0; i < 1000; i++) {
225 if (!(rd(off) & bit))
226 return 0;
227 udelay(100);
228 }
229 return -1;
230}
231
232static int mi_wait(uint32_t *v)
233{
234 udelay(10);
235 for (unsigned i = 0; i < 5000; i++) {
236 uint32_t c = rd(MAC_MI_COM);
237 if (!(c & 0x20000000)) {
238 udelay(5);
239 *v = rd(MAC_MI_COM);
240 return 0;
241 }
242 udelay(10);
243 }
244 return -1;
245}
246static int phy_r(unsigned reg, uint16_t *out)
247{
248 uint32_t v;
249 wrf(MAC_MI_COM, (uint32_t)PHY_ADDR << 21 | (reg & 0x1f) << 16 | 0x08000000 | 0x20000000);
250 if (mi_wait(&v))
251 return -1;
252 *out = (uint16_t)v;
253 return 0;
254}
255static int phy_w(unsigned reg, uint16_t val)
256{
257 uint32_t v;
258 wrf(MAC_MI_COM, (uint32_t)PHY_ADDR << 21 | (reg & 0x1f) << 16 | val | 0x04000000 | 0x20000000);
259 return mi_wait(&v);
260}
261static int auxctl_r(uint16_t sel, uint16_t *v)
262{
263 return phy_w(PHY_AUXCTL, (uint16_t)(sel << 12 | 0x7)) || phy_r(PHY_AUXCTL, v);
264}
265static int auxctl_w(uint16_t sel, uint16_t v)
266{
267 return phy_w(PHY_AUXCTL, (uint16_t)(v | sel | (sel == 0x7 ? 0x8000 : 0)));
268}
269
270/* tg3_phy_reset for this PHY: BMCR reset, power-down timers, extended
271 * packet length, FIFO elasticity, auto-MDIX, wirespeed. */
272static int phy_reset(void)
273{
274 uint16_t v;
275 if (phy_r(MII_BMSR, &v) || phy_r(MII_BMSR, &v) || phy_w(MII_BMCR, 0x8000))
276 return -1;
277 unsigned i = 0;
278 for (; i < 5000; i++) {
279 if (phy_r(MII_BMCR, &v))
280 return -1;
281 if (!(v & 0x8000))
282 break;
283 udelay(10);
284 }
285 if (i == 5000)
286 return -1;
287 udelay(40);
288 if (phy_w(PHY_SHADOW, 0x941f) || phy_w(PHY_SHADOW, 0xa801))
289 return -1;
290 if (auxctl_r(0x0, &v) || auxctl_w(0x0, v | 0x4000))
291 return -1;
292 if (phy_r(PHY_DSP_FIFO, &v) || phy_w(PHY_DSP_FIFO, v | 0x0001))
293 return -1;
294 if (auxctl_r(0x7, &v) || auxctl_w(0x7, v | 0x0200))
295 return -1;
296 if (auxctl_r(0x7, &v) || auxctl_w(0x7, v | 0x0010))
297 return -1;
298 return 0;
299}
300
301static const char *read_mac(void)
302{
303 uint32_t hi = sram_r(SRAM_MAC_HI), lo;
304 const char *from = "sram";
305 if (hi >> 16 == 0x484b) {
306 lo = sram_r(SRAM_MAC_LO);
307 } else {
308 hi = rd(MAC_ADDR0_HI);
309 lo = rd(MAC_ADDR0_LO);
310 from = "registers";
311 }
312 mac[0] = (uint8_t)(hi >> 8);
313 mac[1] = (uint8_t)hi;
314 mac[2] = (uint8_t)(lo >> 24);
315 mac[3] = (uint8_t)(lo >> 16);
316 mac[4] = (uint8_t)(lo >> 8);
317 mac[5] = (uint8_t)lo;
318 unsigned any = 0;
319 for (unsigned i = 0; i < 6; i++)
320 any |= mac[i];
321 if (any && !(mac[0] & 1) && !(mac[0] == 0xff && mac[1] == 0xff))
322 return from;
323 /* None left by the firmware: a locally administered address. */
324 uint64_t t = ck_rdtsc();
325 mac[0] = 0x02;
326 for (unsigned i = 1; i < 6; i++, t >>= 8)
327 mac[i] = (uint8_t)t;
328 return "generated";
329}
330
331static void fail(const char *stage)
332{
333 ck_printf("net_tg3: stopped at %s; no network report this run\n", stage);
334}
335
336int ck_x86_tg3_start(volatile uint8_t *r, volatile uint8_t *c, struct ck_frames *fr, uint64_t tsc_hz)
337{
338 regs = r;
339 cfg = c;
340 hz = tsc_hz;
341 ready = link_up = 0;
342
343 /* A: register access, identity, management firmware, station address. */
344 if (cr32(0) != 0x168614e4u) {
345 fail("identity (not 14e4:1686)");
346 return -1;
347 }
348 for (unsigned p = cr32(0x34) & 0xfc, n = 0; p && n < 48; n++) {
349 uint32_t h = cr32(p);
350 if ((h & 0xff) == 0x10) {
351 pcie_cap = p;
352 break;
353 }
354 p = (h >> 8) & 0xfc;
355 }
356 if (!pcie_cap) {
357 fail("PCIe capability");
358 return -1;
359 }
360 cw16(4, (uint16_t)(cr16(4) | 0x6));
361 cw32(CFG_MISC_HOST_CTRL, MHC_VALUE);
362 uint32_t asic = cr32(CFG_PRODID_ASICREV) >> 12;
363 if (asic != 0x57766) {
364 ck_printf("net_tg3: asic_rev=0x%x, only the 57766 is driven\n", asic);
365 return -1;
366 }
367 wr(MEMARB_MODE, rd(MEMARB_MODE) | 0x2);
368 wr(GRC_MODE, (rd(GRC_MODE) & 0x00010000) | 0x34);
369 cw32(CFG_MEM_WIN_BASE, 0);
370 cw32(CFG_REG_BASE, 0);
371 uint32_t sig = sram_r(SRAM_DATA_SIG), ncfg = sig == FW_MAGIC ? sram_r(SRAM_DATA_CFG) : 0;
372 if (ncfg & (0x80 | 0x00200000)) {
373 ck_printf("net_tg3: management firmware enabled (cfg=0x%x); left alone\n", ncfg);
374 return -1;
375 }
376 const char *mac_from = read_mac();
377 wrf(MAC_MI_MODE, 0x00008000);
378 udelay(80);
379
380 /* B: interrupts masked, bootcode signature, PHY reset, engines stopped,
381 * core reset. */
382 for (unsigned i = 0; i < 5; i++) {
383 wr(MBOX_INT0 + 8 * i, 1);
384 (void)rd(MBOX_INT0 + 8 * i);
385 }
386 sram_w(SRAM_FW_MBOX, FW_MAGIC);
387 if (phy_reset()) {
388 fail("PHY reset (MDIO)");
389 return -1;
390 }
391 unsigned stuck = 0;
392 wrf(MAC_RX_MODE, rd(MAC_RX_MODE) & ~0x2u);
393 udelay(10);
394 static const uint16_t blocks[] = {0x2c00, 0x2000, 0x2400, 0x2800, 0x3000, 0x1400,
395 0x1800, 0x0c00, 0x4800, 0x1000, 0x1c00};
396 for (unsigned i = 0; i < sizeof blocks / sizeof blocks[0]; i++)
397 stuck += stop_block(blocks[i], 0x2) != 0;
398 wrf(MAC_MODE, rd(MAC_MODE) & ~0x00200000u);
399 udelay(40);
400 stuck += stop_block(MAC_TX_MODE, 0x2) != 0;
401 stuck += stop_block(HOSTCC_MODE, 0x2) != 0;
402 stuck += stop_block(WDMAC_MODE, 0x2) != 0;
403 wr(FTQ_RESET, 0xffffffffu);
404 wr(FTQ_RESET, 0);
405
406 wr(NVRAM_SWARB, 0x2);
407 unsigned g = 0;
408 for (; g < 8000 && !(rd(NVRAM_SWARB) & 0x200); g++)
409 udelay(20);
410 if (g == 8000)
411 wr(NVRAM_SWARB, 0x20);
412 uint16_t pci_cmd = cr16(4);
413 wr(GRC_FASTBOOT_PC, 0);
414 wr(GRC_MISC_CFG, 1u << 29);
415 wr(GRC_MISC_CFG, (1u << 29) | 1u); /* core clock reset: no BAR access until restored */
416 udelay(120);
417 (void)cr16(4);
418 udelay(120);
419 uint16_t devctl = cr16(pcie_cap + 8);
420 cw16(pcie_cap + 8, (uint16_t)(devctl & ~(0x0010 | 0x0800)));
421 cw16(pcie_cap + 0xa, 0x000f);
422 cw32(CFG_MISC_HOST_CTRL, MHC_VALUE);
423 cw32(CFG_PCISTATE, 0x60);
424 cw16(4, pci_cmd);
425 wr(MEMARB_MODE, 0x2);
426 unsigned fw_ok = 0;
427 for (unsigned i = 0; i < 100000; i++) {
428 if (sram_r(SRAM_FW_MBOX) == ~FW_MAGIC) {
429 fw_ok = 1;
430 break;
431 }
432 udelay(10);
433 }
434 wr(GRC_MODE, 0x34);
435 wrf(MAC_MODE, 0);
436 udelay(40);
437 wrf(MAC_MI_MODE, 0x00008000);
438 udelay(80);
439 if (rd(CFG_MISC_HOST_CTRL) == 0xffffffffu) {
440 fail("core reset (registers read all-ones)");
441 return -1;
442 }
443 cw32(CFG_MISC_HOST_CTRL, MHC_VALUE);
444 cw32(CFG_MEM_WIN_BASE, 0);
445
446 /* D: tg3_reset_hw for the 57766. */
447 wr(CPMU_PADRNG_CTL, rd(CPMU_PADRNG_CTL) | 0x00040000);
448 uint32_t grc = rd(GRC_MODE);
449 wr(GRC_MODE, (grc & ~(0x00400000u | 0x20000000u)) | 0x20000000u);
450 wr(PCIE_DL_FTSMAX, (rd(PCIE_DL_FTSMAX) & ~0xffu) | 0x2c);
451 wr(GRC_MODE, grc);
452 wr(CPMU_LSPD_10MB_CLK, (rd(CPMU_LSPD_10MB_CLK) & ~0x001f0000u) | 0x00130000u);
453
454 if (!dma) {
455 uint64_t pa;
456 if (ck_frames_alloc(fr, DMA_PAGES, CK_PAGE, &pa) || pa + DMA_BYTES > 0x100000000ull) {
457 fail("DMA memory below 4 GiB");
458 return -1;
459 }
460 dma = pa;
461 }
462 memset((void *)(uintptr_t)dma, 0, DMA_BYTES);
463 volatile uint32_t *std = (volatile uint32_t *)(uintptr_t)(dma + OFF_RX_STD);
464 for (unsigned i = 0; i < RX_STD; i++) {
465 uint64_t b = dma + OFF_RX_BUF + (uint64_t)i * BUF_SZ;
466 std[i * 8 + 0] = i < RX_POSTED ? (uint32_t)(b >> 32) : 0;
467 std[i * 8 + 1] = i < RX_POSTED ? (uint32_t)b : 0;
468 std[i * 8 + 2] = RX_BUF_SZ;
469 std[i * 8 + 3] = 0x4;
470 std[i * 8 + 7] = 0x10000u | i;
471 }
472 __asm__ volatile("sfence" ::: "memory");
473
474 wr(DMA_RW_CTRL, rd(DMA_RW_CTRL) | 0x1);
475 wr(GRC_MODE, 0x00120034u | 0x04000000u | 0x00010000u);
476 wr(GRC_MISC_CFG, (rd(GRC_MISC_CFG) & ~0xffu) | (65u << 1));
477 wr(BUFMGR_MB_RDMA_LOW, 0x00);
478 wr(BUFMGR_MB_MACRX_LOW, 0x2a);
479 wr(BUFMGR_MB_HIGH, 0xa0);
480 wr(BUFMGR_DMA_LOW, 0x05);
481 wr(BUFMGR_DMA_HIGH, 0x0a);
482 wr(BUFMGR_MODE, 0x2 | 0x4);
483 unsigned b = 0;
484 for (; b < 2000 && !(rd(BUFMGR_MODE) & 0x2); b++)
485 udelay(10);
486 if (b == 2000) {
487 fail("buffer manager enable");
488 return -1;
489 }
490 wr(RCVBDI_STD_THRESH, RX_POSTED / 8);
491 wr(STD_REPLENISH_LWM, 128);
492 wr(RCVBDI_JUMBO_THRESH, 12);
493 wr(JMB_REPLENISH_LWM, 64);
494 wr(RCVDBDI_STD_BD + 0x0, (uint32_t)((dma + OFF_RX_STD) >> 32));
495 wr(RCVDBDI_STD_BD + 0x4, (uint32_t)(dma + OFF_RX_STD));
496 wr(RCVDBDI_STD_BD + 0xc, SRAM_RX_STD_BDCACHE);
497 wr(RCVDBDI_JUMBO_FLAGS, 0x2);
498 wr(RCVDBDI_STD_BD + 0x8, RX_STD << 16 | RX_BUF_SZ << 2);
499 wr(MBOX_RX_STD_PROD, RX_POSTED);
500 wr(MBOX_RX_JMB_PROD, 0);
501
502 sram_w(SRAM_SEND_RCB + 0x10 + 0x8, 0x2);
503 for (unsigned k = 1; k < 4; k++)
504 sram_w(SRAM_RET_RCB + 0x10 * k + 0x8, 0x2);
505 for (unsigned i = 0; i < 5; i++) {
506 wr(MBOX_INT0 + 8 * i, 1);
507 (void)rd(MBOX_INT0 + 8 * i);
508 }
509 for (unsigned i = 0; i < 4; i++)
510 wr(MBOX_RX_RET_CONS0 + 8 * i, 0);
511 wr(MBOX_TX_PROD0, 0);
512 tx_prod = 0;
513 wr(HOSTCC_STATUS_BLK, (uint32_t)((dma + OFF_STATUS) >> 32));
514 wr(HOSTCC_STATUS_BLK + 4, (uint32_t)(dma + OFF_STATUS));
515 sram_w(SRAM_SEND_RCB + 0x0, (uint32_t)((dma + OFF_TX_RING) >> 32));
516 sram_w(SRAM_SEND_RCB + 0x4, (uint32_t)(dma + OFF_TX_RING));
517 sram_w(SRAM_SEND_RCB + 0x8, TX_RING << 16);
518 sram_w(SRAM_RET_RCB + 0x0, (uint32_t)((dma + OFF_RX_RET) >> 32));
519 sram_w(SRAM_RET_RCB + 0x4, (uint32_t)(dma + OFF_RX_RET));
520 sram_w(SRAM_RET_RCB + 0x8, RX_RET << 16);
521
522 uint32_t mhi = (uint32_t)mac[0] << 8 | mac[1];
523 uint32_t mlo = (uint32_t)mac[2] << 24 | (uint32_t)mac[3] << 16 | (uint32_t)mac[4] << 8 | mac[5];
524 for (unsigned n = 0; n < 4; n++) {
525 wr(MAC_ADDR0_HI + 8 * n, mhi);
526 wr(MAC_ADDR0_LO + 8 * n, mlo);
527 }
528 wr(MAC_TX_BACKOFF, (uint32_t)(mac[0] + mac[1] + mac[2] + mac[3] + mac[4] + mac[5]) & 0x3ff);
529 wr(MAC_RX_MTU, 1522);
530 wr(MAC_TX_LENGTHS, 0x2620);
531 wr(MAC_RCV_RULE_CFG, 0x8);
532 wr(RCVLPC_CONFIG, 0x181);
533 wr(RDMA_RSRVCTRL, rd(RDMA_RSRVCTRL) | 0x4);
534 wr(RCVLPC_STATS_ENABLE, rd(RCVLPC_STATS_ENABLE) & ~0x00040000u);
535 wr(RCVLPC_STATSCTRL, 0x1);
536 wr(SNDDATAI_STATSENAB, 0xffffff);
537 wr(SNDDATAI_STATSCTRL, 0x1 | 0x2);
538
539 wr(HOSTCC_MODE, 0);
540 for (b = 0; b < 2000 && (rd(HOSTCC_MODE) & 0x2); b++)
541 udelay(10);
542 wr(HOSTCC_TXCOL_TICKS, 0x48);
543 wr(HOSTCC_TXMAX_FRAMES, 0x35);
544 wr(HOSTCC_TXCOAL_MAXF_INT, 0x05);
545 wr(HOSTCC_RXCOL_TICKS, 0x14);
546 wr(HOSTCC_RXMAX_FRAMES, 0x05);
547 wr(HOSTCC_RXCOAL_MAXF_INT, 0x05);
548 for (unsigned i = 0; i < 4; i++)
549 for (unsigned o = 0; o < 0x18; o += 4)
550 wr(HOSTCC_VEC1 + 0x18 * i + o, 0);
551 wr(HOSTCC_MODE, 0x702);
552 wr(RCVCC_MODE, 0x2 | 0x4);
553 wr(RCVLPC_MODE, 0x2);
554
555 mac_mode = 0x00e04800u;
556 wrf(MAC_MODE, mac_mode | 0x1000 | 0x8000);
557 udelay(40);
558 uint32_t gpio_mask = 0x0001f800u;
559 wrf(GRC_LOCAL_CTRL, (0x01009018u & ~gpio_mask) | (rd(GRC_LOCAL_CTRL) & gpio_mask) | 0x1000 | 0x8000);
560 udelay(100);
561 wrf(WDMAC_MODE, 0x200003feu);
562 udelay(40);
563 wrf(RDMAC_MODE, 0x188303feu);
564 udelay(40);
565 wr(RCVDCC_MODE, 0x2 | 0x4);
566 wr(SNDDATAC_MODE, 0x2);
567 wr(SNDBDC_MODE, 0x2 | 0x4);
568 wr(RCVBDI_MODE, 0x2 | 0x4);
569 wr(RCVDBDI_MODE, 0x2 | 0x10);
570 wr(SNDDATAI_MODE, 0x2);
571 wr(SNDDATAI_MODE, 0x2 | 0x8);
572 wr(SNDBDI_MODE, 0x2 | 0x4);
573 wr(SNDBDS_MODE, 0x2 | 0x4);
574 wrf(MAC_TX_MODE, 0x102);
575 udelay(100);
576 wrf(MAC_RX_MODE, 0x01000002u);
577 udelay(10);
578 wr(MAC_LED_CTRL, 0x800);
579 wr(MAC_MI_STAT, 0x1);
580 wrf(MAC_RX_MODE, 0x01000002u);
581 udelay(10);
582 wrf(MAC_LOW_WMARK_MAX_RX, 1);
583
584 /* E: copper PHY, autonegotiation (tg3_setup_copper_phy, link-down path). */
585 uint16_t v;
586 wr(MAC_EVENT, 0);
587 wrf(MAC_STATUS, 0x10 | 0x8 | 0x00400000 | 0x1000);
588 udelay(40);
589 if (auxctl_w(0x2, 0) || phy_r(PHY_ISTAT, &v) || phy_r(PHY_ISTAT, &v) || phy_w(PHY_IMASK, 0xffff) ||
590 phy_w(MII_ADVERTISE, 0x05e1) || phy_w(MII_CTRL1000, 0x0300) || phy_w(MII_BMCR, 0x1200)) {
591 fail("PHY autonegotiation setup (MDIO)");
592 return -1;
593 }
594 mac_mode = (mac_mode & ~0xeu) | 0x8; /* GMII until the link says otherwise */
595 wrf(MAC_MODE, mac_mode);
596 udelay(40);
597 wrf(MAC_EVENT, 0x1000);
598 udelay(40);
599
600 for (unsigned i = 0; i < 4; i++)
601 wr(MAC_HASH0 + 4 * i, 0);
602 wrf(MAC_RX_MODE, 0x01000002u);
603 wr(MAC_RCV_RULE0, 0x42000000u);
604 wr(MAC_RCV_RULE0 + 4, 0x7fffffffu);
605 wr(MAC_RCV_RULE0 + 8, 0x06000004u);
606 wr(MAC_RCV_RULE0 + 0xc, 0x7fffffffu);
607 for (unsigned o = 0x4a0; o < 0x4c0; o += 4)
608 wr(o, 0);
609
610 ready = 1;
611 ck_printf("net_tg3: chip=57766 mac_from=%s bootcode=%s engines_stuck=%u bufmgr=ok dma=0x%llx "
612 "autoneg=started\n",
613 mac_from, fw_ok ? "ready" : "silent", stuck, (unsigned long long)dma);
614 return 0;
615}
616
617int ck_x86_tg3_ready(void) { return ready; }
618
619void ck_x86_tg3_mac(uint8_t out[6]) { memcpy(out, mac, 6); }
620
621/* Link check (polled tg3_setup_copper_phy): on the first link-up, the MAC
622 * port mode and duplex follow the PHY's resolved speed. */
623int ck_x86_tg3_link(void)
624{
625 uint16_t bmsr, aux = 0;
626 if (!ready || phy_r(MII_BMSR, &bmsr) || phy_r(MII_BMSR, &bmsr))
627 return 0;
628 if (!(bmsr & 0x4)) {
629 if (link_up)
630 ck_puts("net_tg3: link down\n");
631 return link_up = 0;
632 }
633 if (link_up)
634 return 1;
635 for (unsigned i = 0; i < 2000; i++) {
636 if (phy_r(PHY_AUX_STAT, &aux))
637 return 0;
638 if (aux)
639 break;
640 udelay(10);
641 }
642 unsigned sd = (aux >> 8) & 7, speed = sd >= 6 ? 1000 : sd >= 3 ? 100 : 10;
643 int full = sd == 2 || sd == 5 || sd == 7;
644 mac_mode = (mac_mode & ~0xeu) | (speed == 1000 ? 0x8 : 0x4) | (full ? 0 : 0x2);
645 wrf(MAC_MODE, mac_mode);
646 udelay(40);
647 wr(MAC_TX_LENGTHS, 0x2600 | (speed == 1000 && !full ? 0xff : 32));
648 wrf(MAC_EVENT, 0x1000);
649 udelay(40);
650 link_up = 1;
651 ck_printf("net_tg3: link up %u Mb/s %s duplex\n", speed, full ? "full" : "half");
652 return 1;
653}
654
655#if defined(CK_X86_REPORT) && CK_X86_REPORT
656/* Transmit exists only in the opt-in lab image, for the network report
657 * (report_x86.c). The default image has no way to send a frame. */
658static int stalled_shown;
659
660static unsigned tx_cons(void)
661{
662 __asm__ volatile("" ::: "memory");
663 return *(volatile uint16_t *)(uintptr_t)(dma + OFF_STATUS + 0x12) & (TX_RING - 1);
664}
665
666int ck_x86_tg3_send(const uint8_t *frame, size_t len)
667{
668 if (!ready || !link_up || !frame || len > 1514 || len < 14)
669 return -1;
670 /* Room: fewer than TX_BUFS frames in flight (each frame is one BD). */
671 unsigned waited = 0;
672 while (((tx_prod - tx_cons()) & (TX_RING - 1)) >= TX_BUFS - 1) {
673 wr(HOSTCC_MODE, 0x702 | 0x8);
674 udelay(100);
675 if (++waited > 500) { /* 50 ms */
676 if (!stalled_shown++)
677 ck_printf("net_tg3: transmit stalled prod=%u cons=%u sndbds=0x%x sndbdi=0x%x rdmac=0x%x "
678 "wdmac=0x%x bufmgr=0x%x hostcc=0x%x mac_tx=0x%x\n",
679 tx_prod, tx_cons(), rd(SNDBDS_MODE + 4), rd(SNDBDI_MODE + 4), rd(RDMAC_STATUS),
680 rd(WDMAC_STATUS), rd(BUFMGR_STATUS), rd(HOSTCC_STATUS), rd(MAC_TX_STATUS));
681 return -1;
682 }
683 }
684 uint64_t buf = dma + OFF_TX_BUF + (uint64_t)(tx_prod % TX_BUFS) * BUF_SZ;
685 size_t n = len < 60 ? 60 : len;
686 memcpy((void *)(uintptr_t)buf, frame, len);
687 if (n > len)
688 memset((void *)(uintptr_t)(buf + len), 0, n - len);
689 volatile uint32_t *bd = (volatile uint32_t *)(uintptr_t)(dma + OFF_TX_RING + (uint64_t)tx_prod * 16);
690 bd[0] = (uint32_t)(buf >> 32);
691 bd[1] = (uint32_t)buf;
692 bd[2] = (uint32_t)n << 16 | 0x4;
693 bd[3] = 0;
694 __asm__ volatile("sfence" ::: "memory");
695 tx_prod = (tx_prod + 1) & (TX_RING - 1);
696 wr(MBOX_TX_PROD0, tx_prod);
697 return 0;
698}
699
700/* Waits up to `ms` for every queued frame to be sent. 0 when all are. */
701int ck_x86_tg3_drain(unsigned ms)
702{
703 if (!ready)
704 return -1;
705 for (unsigned i = 0; i < ms * 10; i++) {
706 if (tx_cons() == tx_prod)
707 return 0;
708 wr(HOSTCC_MODE, 0x702 | 0x8);
709 udelay(100);
710 }
711 return -1;
712}
713/* Receive (lab image only, like send): the next frame the chip has put in
714 * one of the 64 standard buffers, read from the return ring and the buffer
715 * handed back. tg3.h struct tg3_hw_status: idx[0].rx_producer at +0x10 of the
716 * status block; struct tg3_rx_buffer_desc words: idx_len [2], err_vlan [5],
717 * opaque [7]; tg3.c tg3_rx: desc_idx = opaque & RXD_OPAQUE_INDEX_MASK,
718 * len = (idx_len & RXD_LEN_MASK) - ETH_FCS_LEN, a frame with err_vlan &
719 * RXD_ERR_MASK dropped, then the consumer mailbox and the standard producer
720 * mailbox written (recorded with docs-read 2026-10-19). Bytes copied, 0 when
721 * none waits or the frame does not fit. */
722static unsigned rx_ret_cons, rx_std_prod = RX_POSTED;
723static size_t ring_take(uint8_t *out, size_t cap)
724{
725 if (!ready || !out)
726 return 0;
727 wr(HOSTCC_MODE, 0x702 | 0x8); /* status block now: interrupts are masked, coalescing would wait */
728 __asm__ volatile("" ::: "memory");
729 unsigned prod = *(volatile uint16_t *)(uintptr_t)(dma + OFF_STATUS + 0x10) & (RX_RET - 1);
730 size_t got = 0;
731 while (rx_ret_cons != prod && !got) {
732 volatile uint32_t *d = (volatile uint32_t *)(uintptr_t)(dma + OFF_RX_RET + (uint64_t)rx_ret_cons * 32);
733 unsigned len = d[2] & 0xffffu, idx = d[7] & 0xffffu;
734 uint32_t err = d[5] & 0x01ef0000u; /* err_vlan & RXD_ERR_MASK: every error but ODD_NIBBLE_RCVD_MII */
735 if (len > 4 && !err && len - 4 <= cap) {
736 got = len - 4;
737 memcpy(out, (const void *)(uintptr_t)(dma + OFF_RX_BUF + (uint64_t)(idx % RX_POSTED) * BUF_SZ), got);
738 }
739 /* the buffer goes back on the standard ring at its next slot */
740 uint64_t b = dma + OFF_RX_BUF + (uint64_t)(idx % RX_POSTED) * BUF_SZ;
741 volatile uint32_t *s = (volatile uint32_t *)(uintptr_t)(dma + OFF_RX_STD + (uint64_t)rx_std_prod * 32);
742 s[0] = (uint32_t)(b >> 32);
743 s[1] = (uint32_t)b;
744 s[2] = RX_BUF_SZ;
745 s[3] = 0x4;
746 s[7] = 0x10000u | rx_std_prod;
747 __asm__ volatile("sfence" ::: "memory");
748 rx_std_prod = (rx_std_prod + 1) & (RX_STD - 1);
749 rx_ret_cons = (rx_ret_cons + 1) & (RX_RET - 1);
750 wr(MBOX_RX_RET_CONS0, rx_ret_cons);
751 wr(MBOX_RX_STD_PROD, rx_std_prod);
752 }
753 return got;
754}
755
756/* Two readers share the one ring: Allen's courier (ck_x86_tg3_recv, whole
757 * frames) and the GPU bench (ck_x86_tg3_recv_bench, UDP port 47035). A frame
758 * one of them takes that belongs to the other is kept for it in a one-frame
759 * slot (a newer one replaces it), so neither eats the other's traffic. */
760static uint8_t held_bench[RX_BUF_SZ], held_other[RX_BUF_SZ];
761static size_t held_bench_len, held_other_len;
762static int for_bench(const uint8_t *f, size_t n)
763{
764 return n >= 42 && f[12] == 0x08 && f[13] == 0x00 && f[23] == 17 &&
765 ((unsigned)f[36] << 8 | f[37]) == (unsigned)CK_X86_BENCH_PORT;
766}
767
768size_t ck_x86_tg3_recv(uint8_t *out, size_t cap)
769{
770 if (!out)
771 return 0;
772 if (held_other_len) {
773 size_t n = held_other_len <= cap ? held_other_len : 0;
774 if (n)
775 memcpy(out, held_other, n);
776 held_other_len = 0;
777 if (n)
778 return n;
779 }
780 for (unsigned tries = 0; tries < 8; tries++) {
781 size_t n = ring_take(out, cap);
782 if (!n)
783 return 0;
784 if (!for_bench(out, n))
785 return n;
786 if (n <= sizeof held_bench) {
787 memcpy(held_bench, out, n);
788 held_bench_len = n;
789 }
790 }
791 return 0;
792}
793
794/* The GPU bench's reader: 0 with *len the UDP payload length (no Ethernet, IP
795 * or UDP header) and src_mac the sender when a bench datagram arrived; 0 with
796 * *len 0 when none waits; -1 when the chip is not up. */
797int ck_x86_tg3_recv_bench(uint8_t *pkt, size_t cap, size_t *len, uint8_t src_mac[6])
798{
799 static uint8_t f[RX_BUF_SZ];
800 size_t n = 0;
801 if (len)
802 *len = 0;
803 if (!ready)
804 return -1;
805 if (held_bench_len) {
806 n = held_bench_len;
807 memcpy(f, held_bench, n);
808 held_bench_len = 0;
809 } else {
810 for (unsigned tries = 0; tries < 8 && !n; tries++) {
811 size_t m = ring_take(f, sizeof f);
812 if (!m)
813 break;
814 if (for_bench(f, m))
815 n = m;
816 else {
817 memcpy(held_other, f, m);
818 held_other_len = m;
819 }
820 }
821 }
822 if (!n)
823 return 0;
824 size_t plen = n - 42;
825 if (plen > cap)
826 plen = cap;
827 if (pkt)
828 memcpy(pkt, f + 42, plen);
829 if (len)
830 *len = plen;
831 if (src_mac)
832 memcpy(src_mac, f + 6, 6);
833 return 0;
834}
835
836/* Broadcast a UDP payload in an Ethernet frame (src = our MAC, dst = broadcast). */
837void ck_x86_udp_send(const uint8_t dst_mac[6], const uint8_t *payload, size_t plen,
838 uint16_t src_port, uint16_t dst_port)
839{
840 if (!ready) return;
841 static uint8_t frame[1536];
842 if (plen + 42 > sizeof frame) return;
843 /* Ethernet header */
844 if (dst_mac)
845 memcpy(frame, dst_mac, 6);
846 else
847 memset(frame, 0xff, 6); /* broadcast */
848 memcpy(frame + 6, mac, 6);
849 frame[12] = 0x08; frame[13] = 0x00; /* IPv4 */
850 /* IPv4 header (20 bytes, no options) */
851 uint16_t ip_len = (uint16_t)(20 + 8 + plen);
852 frame[14] = 0x45; frame[15] = 0;
853 frame[16] = (uint8_t)(ip_len >> 8); frame[17] = (uint8_t)ip_len;
854 frame[18] = 0; frame[19] = 0; /* id */
855 frame[20] = 0x40; frame[21] = 0; /* DF, frag off = 0 */
856 frame[22] = 1; /* TTL */
857 frame[23] = 17; /* UDP */
858 frame[24] = 0; frame[25] = 0; /* checksum placeholder */
859 memset(frame + 26, 0, 8); /* src/dst IP = 0.0.0.0 / bcast */
860 frame[30] = 255; frame[31] = 255; frame[32] = 255; frame[33] = 255;
861 /* Compute IP checksum */
862 uint32_t cs = 0;
863 for (unsigned i = 14; i < 34; i += 2)
864 cs += (uint16_t)((uint16_t)frame[i] << 8 | frame[i+1]);
865 while (cs >> 16) cs = (cs & 0xffff) + (cs >> 16);
866 cs = ~cs & 0xffff;
867 frame[24] = (uint8_t)(cs >> 8); frame[25] = (uint8_t)cs;
868 /* UDP header */
869 frame[34] = (uint8_t)(src_port >> 8); frame[35] = (uint8_t)src_port;
870 frame[36] = (uint8_t)(dst_port >> 8); frame[37] = (uint8_t)dst_port;
871 uint16_t udp_len = (uint16_t)(8 + plen);
872 frame[38] = (uint8_t)(udp_len >> 8); frame[39] = (uint8_t)udp_len;
873 frame[40] = 0; frame[41] = 0; /* UDP checksum = 0 (optional for IPv4) */
874 memcpy(frame + 42, payload, plen);
875 ck_x86_tg3_send(frame, 42 + plen);
876 ck_x86_tg3_drain(20);
877}
878
879/* Stop the MAC gracefully (for swap quiesce). */
880void ck_x86_tg3_stop(void)
881{
882 if (!ready) return;
883 wrf(MAC_RX_MODE, rd(MAC_RX_MODE) & ~0x2u); /* RX off */
884 wrf(MAC_TX_MODE, rd(MAC_TX_MODE) & ~0x2u); /* TX off */
885 ready = 0;
886 ck_puts("net_tg3: stopped (swap quiesce)\n");
887}
888
889#endif /* CK_X86_REPORT */