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 / dev / rtl8127_bind.c

202 lines · 8546 bytes Raw Blame History
1/* rtl8127_bind.c -- RTL8127 binding and UDP boot report (see rtl8127_bind.h).
2 * Freestanding. */
3#include "rtl8127_bind.h"
4#include "aienos_report.h"
5#include "aienos_rtl8127.h"
6#include "ck.h"
7#include <string.h>
8
9#define ROUND_US 2000000u
10#define CLOSE_WAIT_US 100000u
11
12static struct {
13 pci_func fn;
14 const pci_func *pf;
15 volatile uint8_t *bar;
16 rtl_dev dev;
17 int bm_on, confined;
18 uint32_t stream_id;
19} g_rtl;
20/* ck_dma_alloc memory is never freed: allocate once, reuse on a later bind. */
21static uint8_t *g_mem;
22static uint64_t g_phys;
23static uint8_t frame[1600];
24static uint8_t rxbuf[RTL_RX_BUF]; /* every frame RMS 1522 lets through, VLAN tag included */
25
26static uint32_t o_read(void *ctx, uint32_t off, unsigned w)
27{
28 volatile uint8_t *p = (volatile uint8_t *)ctx + off;
29 if (w == 1) return *p;
30 if (w == 2) return *(volatile uint16_t *)p;
31 return *(volatile uint32_t *)p;
32}
33static void o_write(void *ctx, uint32_t off, unsigned w, uint32_t v)
34{
35 volatile uint8_t *p = (volatile uint8_t *)ctx + off;
36 ck_mb(); /* descriptors and buffers visible before the register write */
37 if (w == 1) *p = (uint8_t)v;
38 else if (w == 2) *(volatile uint16_t *)p = (uint16_t)v;
39 else *(volatile uint32_t *)p = v;
40 ck_mb();
41}
42static void o_delay(void *ctx, uint32_t us) { (void)ctx; ck_udelay(us); }
43static void o_barrier(void *ctx) { (void)ctx; ck_mb(); }
44
45int ck_rtl8127_live(void) { return g_rtl.pf && (g_rtl.bm_on || g_rtl.confined); }
46
47void ck_rtl8127_release(void)
48{
49 if (!g_rtl.pf) return;
50 if (g_rtl.bm_on) {
51 /* Only a chip rtl_init identified as an RTL8127 (mcfg set) is written to. */
52 if (g_rtl.bar && g_rtl.dev.mcfg)
53 ck_printf("rtl8127: chip stop %s\n", rtl_err_name(rtl_stop(&g_rtl.dev)));
54 else
55 ck_printf("rtl8127: chip stop skipped (not identified as an RTL8127, nothing written)\n");
56 if (pci_bus_master_off(g_rtl.pf) == 0) {
57 g_rtl.bm_on = 0;
58 ck_printf("dma_gate: rtl8127 bus master revoked\n");
59 } else
60 ck_printf("dma_gate: rtl8127 bus master revoke FAILED (command register still has BME)\n");
61 }
62 if (g_rtl.confined) {
63 int urc = ck_dma_unconfine(g_rtl.stream_id);
64 if (urc == 0) g_rtl.confined = 0;
65 ck_printf("smmu: rtl8127 stream 0x%x %s (rc=%d%s%s)\n", g_rtl.stream_id,
66 urc == 0 ? "returned to abort" : "abort FAILED, stays confined", urc, urc ? ": " : "",
67 urc ? ck_dma_unconfine_why(urc) : "");
68 if (urc != 0) ck_dma_fault_report("rtl8127", g_rtl.stream_id);
69 }
70}
71
72int ck_rtl8127_find(const pci_found *found, uint32_t n, pci_func *out)
73{
74 if (!out) return CK_RTL_E_ARG;
75 for (uint32_t i = 0; found && i < n; i++) {
76 const pci_found *c = &found[i];
77 if (c->vendor != RTL_PCI_VENDOR || c->device != RTL_PCI_DEVICE) continue;
78 ck_printf("rtl8127: found seg %04x %02x:%02x.%u rev 0x%02x\n", c->segment, c->bus, c->dev, c->fn, c->revision);
79 pci_bus_access acc;
80 if (pci_stage_disc_access(c->segment, c->bus, &acc) || pci_func_from_found(out, c, &acc)) {
81 ck_printf("rtl8127: unavailable (cannot read the function as an endpoint; nothing written)\n");
82 return CK_RTL_E_ARG;
83 }
84 return CK_RTL_OK;
85 }
86 ck_printf("rtl8127: absent (no 10ec:8127 function)\n");
87 return CK_RTL_ABSENT;
88}
89
90/* Send every part once, then wait (bounded) for the chip to close them. */
91static int send_round(rtl_dev *d, const char *log, size_t len, uint64_t run, unsigned *failed)
92{
93 unsigned parts = net_report_parts(len);
94 for (unsigned p = 0; p < parts; p++) {
95 size_t n = net_report_frame(NET_REPORT_TAG_SPARK, d->mac, run, log, len, p, frame, sizeof frame);
96 rtl_err e = n ? rtl_tx(d, frame, n) : RTL_E_ARG;
97 uint64_t t0 = ck_time_us();
98 while (e == RTL_E_FULL && ck_time_us() - t0 < CLOSE_WAIT_US) {
99 if (rtl_tx_reclaim(d, 0) != RTL_OK) return -1;
100 e = rtl_tx(d, frame, n);
101 }
102 if (e != RTL_OK) {
103 (*failed)++;
104 if (e == RTL_E_DEVICE || e == RTL_E_BROKEN) return -1;
105 }
106 }
107 uint64_t t0 = ck_time_us();
108 while (rtl_tx_pending(d) && ck_time_us() - t0 < CLOSE_WAIT_US)
109 if (rtl_tx_reclaim(d, 0) != RTL_OK) return -1;
110 if (rtl_tx_pending(d)) (*failed)++;
111 return 0;
112}
113
114int ck_rtl8127_report(const pci_func *f, const char *log, size_t len, uint32_t seconds)
115{
116 if (!f || !log || !net_report_parts(len)) return CK_RTL_E_ARG;
117 if (ck_rtl8127_live()) ck_rtl8127_release();
118 if (ck_rtl8127_live()) {
119 ck_printf("rtl8127: release incomplete; retaining confined stream, refusing rebind\n");
120 return CK_RTL_E_RELEASE;
121 }
122 memset(&g_rtl, 0, sizeof g_rtl);
123 g_rtl.fn = *f;
124 const pci_func *pf = g_rtl.pf = &g_rtl.fn;
125 const pci_bar *b = &pf->bar[RTL_MMIO_BAR];
126 if (b->io || !b->addr || b->size < RTL_MMIO_MIN) {
127 ck_printf("rtl8127: unavailable (BAR 2 not a memory BAR of at least 0x%x bytes)\n", (unsigned)RTL_MMIO_MIN);
128 return CK_RTL_E_ARG;
129 }
130 if (!g_mem) g_mem = ck_dma_alloc(RTL_MEM_SIZE, 4096, &g_phys);
131 if (!g_mem) {
132 ck_printf("rtl8127: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)RTL_MEM_SIZE);
133 return CK_RTL_E_DMA;
134 }
135 uint32_t rid = ((uint32_t)pf->bus << 8) | ((uint32_t)pf->dev << 3) | (uint32_t)pf->fn;
136 uint64_t win = ((uint64_t)RTL_MEM_SIZE + 4095u) & ~(uint64_t)4095u;
137 struct ck_dma_confinement cf;
138 int src = ck_dma_confine(pf->segment, rid, g_phys, win, &cf);
139 if (src != 0) {
140 ck_printf("dma_gate: rtl8127 denied (%s rc=%d), bus master stays off\n",
141 src == CK_SMMU_ABSENT ? "NoSmmu" : "SmmuNotReady", src);
142 return CK_RTL_E_DENIED;
143 }
144 g_rtl.confined = 1;
145 g_rtl.stream_id = cf.stream_id;
146 ck_printf("smmu_dma_window: rtl8127 only iova=0x%llx len=0x%llx rid=0x%x stream_id=0x%x\n",
147 (unsigned long long)cf.iova, (unsigned long long)cf.len, rid, cf.stream_id);
148 g_rtl.bar = (volatile uint8_t *)ck_mmio_map(b->addr, (size_t)b->size);
149 pci_enable(pf, 1);
150 g_rtl.bm_on = 1;
151 ck_printf("dma_gate: rtl8127 granted (Confined), bus master on\n");
152
153 rtl_ops ops = {(void *)g_rtl.bar, o_read, o_write, o_delay, o_barrier};
154 rtl_dev *d = &g_rtl.dev;
155 rtl_err e = rtl_init(d, &ops, g_mem, cf.iova, RTL_MEM_SIZE);
156 ck_printf("rtl8127: init %s tcr=0x%08x mcfg=%u ic_unknown=%u ups_resume=%u phy_state=%u wait_timeouts=%u\n",
157 rtl_err_name(e), d->tcr_id, d->mcfg, d->ic_unknown, d->ups_resume, d->phy_state, d->wait_timeouts);
158 if (e != RTL_OK) {
159 ck_rtl8127_release();
160 return CK_RTL_E_INIT;
161 }
162 ck_printf("rtl8127: mac %02x:%02x:%02x:%02x:%02x:%02x source=%s clo_base=%u\n", d->mac[0], d->mac[1], d->mac[2],
163 d->mac[3], d->mac[4], d->mac[5],
164 d->mac_source == RTL_MAC_BACKUP ? "backup" : d->mac_source == RTL_MAC_MAC0 ? "mac0" : "fallback",
165 d->clo_base);
166 uint32_t mbps = 0;
167 uint64_t t0 = ck_time_us();
168 while (!rtl_link(d, &mbps) && ck_time_us() - t0 < CK_RTL_LINK_WAIT_US) ck_udelay(1000);
169 ck_printf("rtl8127: link %s %u Mb/s after %u ms\n", mbps ? "up" : "DOWN", mbps,
170 (unsigned)((ck_time_us() - t0) / 1000u));
171 if (!mbps) {
172 ck_rtl8127_release();
173 return CK_RTL_E_LINK;
174 }
175 ck_printf("report: sending %u bytes to UDP port %u (broadcast, tag %s) every 2 s for %u s\n", (unsigned)len,
176 (unsigned)NET_REPORT_PORT, NET_REPORT_TAG_SPARK, seconds);
177 uint64_t run = ck_time_us() ^ (uint64_t)len << 48;
178 unsigned rounds = 0, failed = 0, rx = 0;
179 uint64_t start = ck_time_us(), next = start;
180 int broken = 0;
181 do {
182 if (ck_time_us() >= next) {
183 next += ROUND_US;
184 if (send_round(d, log, len, run, &failed)) {
185 broken = 1;
186 break;
187 }
188 rounds++;
189 }
190 size_t n = 0;
191 int got = 0;
192 if (rtl_rx(d, rxbuf, sizeof rxbuf, &n, &got) != RTL_OK) {
193 broken = 1;
194 break;
195 }
196 rx += (unsigned)got;
197 } while (ck_time_us() - start < (uint64_t)seconds * 1000000u);
198 ck_printf("report: rounds=%u parts=%u send_failures=%u rx_frames=%u rx_dropped=%u device=%s\n", rounds,
199 net_report_parts(len), failed, rx, d->rx_dropped, broken ? "BROKEN" : "ok");
200 ck_rtl8127_release();
201 return broken || failed || !rounds ? CK_RTL_E_TX : CK_RTL_OK;
202}