interplane.Get Omega
Read in your language

rtl8127-omega

aien-dev/rtl8127-omega

Brought by drakestapleton

branch main Branches Tags History

rtl8127-omega / om / rtl8127 / rtl8127.om

703 lines · 14859 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note rtl8127.om: Realtek RTL8127 10 GbE (PCI 10ec:8127) driver core in Omega. Polled, no interrupts, one TX and one RX ring.
3note
4note EXPERIMENTAL. Never run on a real chip. Realtek publishes no register document for this chip; every offset, bit and
5note sequence below comes from Realtek's own Linux driver r8127 v11.014.00 (github.com/openwrt/rtl8127 ec2f285, r8127_n.c
6note and r8127.h, line numbers in the notes). It is a port of the C driver core in aien-dev/aienux PR 450 (native/net/aienos_rtl8127.c).
7note
8note Program-mode companion unit (it owns globals). Build it together with ONE backend unit that defines
9note rtlrd off w read width 1, 2 or 4 bytes at register offset off (BAR 2)
10note rtlwr off w v write
11note rtldly us wait about us microseconds
12note rtlbar full memory barrier
13note (model.om for the tests, hw.om for a real chip) and one main. Every register read and write goes through those four.
14note Everything the chip may DMA to or from is the one region rdma (rings at 0 and 1024, TX buffers from 4096, RX buffers from 36864).
15note The chip must see rdma at bus address dma, 4096-aligned. Omega traps (exit 103) on any index outside an array.
16const MAC0 ulong 0
17const MAC4 ulong 4
18const TNPDSLO ulong 32
19const TNPDSHI ulong 36
20const INTCFG0 ulong 52
21const CHIPCMD ulong 55
22const IMR0 ulong 56
23const ISR0 ulong 60
24const TCR ulong 64
25const RCR ulong 68
26const CFG9346 ulong 80
27const TIMINT0 ulong 88
28const TIMINT1 ulong 92
29const PHYSTAT ulong 108
30const PMCH ulong 111
31const TIMINT2 ulong 140
32const MACOCP ulong 176
33const PHYOCP ulong 184
34const LLFIFO ulong 210
35const MCUCMD ulong 211
36const RXDTYPE ulong 216
37const RMS ulong 218
38const INTRMIT ulong 226
39const RDSARLO ulong 228
40const RDSARHI ulong 232
41const RXDVGATE ulong 242
42const TIMINT3 ulong 244
43const SWTAIL0 ulong 3376
44const HWCLO0 ulong 3380
45const BAKADR0 ulong 6624
46const BAKADR1 ulong 6628
47const TXNOCLO ulong 8420
48const RSSCTRL ulong 17664
49const QNUMCTL ulong 18432
50const OCPFMT ulong 60248
51const MMIOMIN ulong 20480
52const CMDSTOP ulong 128
53const CMDRST ulong 16
54const CMDRXEN ulong 8
55const CMDTXEN ulong 4
56const UNLOCK ulong 192
57const RCRMASK ulong 63
58const RCRBCST ulong 8
59const RCRMYPH ulong 2
60const RCRCHIP ulong 1088425216
61const RCRRST ulong 1280
62const TCRVAL ulong 50333504
63const TCRIDM ulong 2088763392
64const TCRID ulong 1820327936
65const TCRICM ulong 7340032
66const MCUOOB ulong 128
67const MCUTXE ulong 32
68const MCURXE ulong 16
69const OCPWRB ulong 2147483648
70const OCPFLAG ulong 2147483648
71const PHYSLNK ulong 2
72const PHYSFD ulong 1
73const RMSVLAN ulong 32768
74const DOWN ulong 2147483648
75const DEOR ulong 1073741824
76const DFS ulong 536870912
77const DLS ulong 268435456
78const RXRES ulong 2097152
79const RXLEN ulong 16383
80const CLOMASK ulong 2147483647
81const TXN ulong 16
82const RXN ulong 16
83const FMIN ulong 60
84const FMAX ulong 1514
85const RXBUF ulong 1536
86const RMSVAL ulong 1522
87const STRIDE ulong 2048
88const OFFTXD ulong 0
89const OFFRXD ulong 1024
90const OFFTXB ulong 4096
91const OFFRXB ulong 36864
92const MEMSIZE ulong 69632
93
94note State cells (global rs). One device per program.
95const S_READY ulong 0
96const S_BROKE ulong 1
97const S_MCFG ulong 2
98const S_ICUNK ulong 3
99const S_MACSRC ulong 4
100const S_UPSRES ulong 5
101const S_PHYST ulong 6
102const S_TCRID ulong 7
103const S_WTO ulong 8
104const S_TXCUR ulong 9
105const S_TXDIR ulong 10
106const S_CLOB ulong 11
107const S_RXCUR ulong 12
108const S_RXDRP ulong 13
109const S_DMA ulong 14
110const S_MAC0 ulong 15
111const S_RXLEN ulong 21
112const S_RECL ulong 22
113const S_MBPS ulong 23
114
115note Error codes returned by rtlinit, rtltx, rtltxrec, rtlrx, rtlstop.
116const E_ARG ulong 1
117const E_MEM ulong 2
118const E_ABSENT ulong 3
119const E_CHIP ulong 4
120const E_RSTTMO ulong 5
121const E_FULL ulong 6
122const E_DEVICE ulong 7
123const E_BROKEN ulong 8
124const E_NOTRDY ulong 9
125
126note MAC address sources, rs[S_MACSRC].
127const MS_BACK ulong 1
128const MS_MAC0 ulong 2
129const MS_FALL ulong 3
130
131note rs: driver state. rdma: the one DMA region (rings and packet buffers). fbuf: frame to send.
132note obuf: frame received.
133global rs ulong 32
134global rdma uchar 69632
135global fbuf uchar 2048
136global obuf uchar 2048
137
138note ---- register access: width 1, 2 or 4, through the backend (rtlrd, rtlwr, rtldly, rtlbar) ----
139fn rtlr8 o ulong to ulong
140ret rtlrd o 1
141end
142fn rtlr16 o ulong to ulong
143ret rtlrd o 2
144end
145fn rtlr32 o ulong to ulong
146ret rtlrd o 4
147end
148fn rtlw8 o ulong v ulong to void
149rtlwr o 1 and v 255
150end
151fn rtlw16 o ulong v ulong to void
152rtlwr o 2 and v 65535
153end
154fn rtlw32 o ulong v ulong to void
155rtlwr o 4 and v 4294967295
156end
157
158fn rtlwto to void
159put rs S_WTO add get rs S_WTO 1
160end
161
162note ---- little-endian 32 bit words in the DMA region ----
163fn dld32 o ulong to ulong
164a = ulong get rdma o
165b = ulong get rdma add o 1
166c = ulong get rdma add o 2
167d = ulong get rdma add o 3
168ret or or or a shl b 8 shl c 16 shl d 24
169end
170fn dst32 o ulong v ulong to void
171put rdma o uchar and v 255
172put rdma add o 1 uchar and shr v 8 255
173put rdma add o 2 uchar and shr v 16 255
174put rdma add o 3 uchar and shr v 24 255
175end
176
177note ---- MAC OCP window, r8127_n.c:3287-3314 ----
178fn rtlocrd a ulong to ulong
179rtlw32 MACOCP shl div a 2 16
180r = rtlr32 MACOCP
181ret and r 65535
182end
183fn rtlocwr a ulong v ulong to void
184rtlw32 MACOCP or or shl div a 2 16 and v 65535 OCPWRB
185end
186
187note ---- PHY OCP window, r8127_n.c:3057-3073 (write waits for the flag to clear) and 3155-3167 (read waits for it to set) ----
188fn rtlphwr a ulong v ulong to void
189rtlw32 PHYOCP or or shl div a 2 16 and v 65535 OCPWRB
190i = ulong 0
191while lt i 20000 bound 20000
192rtldly 1
193f = rtlr32 PHYOCP
194if eq and f OCPFLAG 0
195ret
196end
197i = add i 1
198end
199rtlwto
200end
201fn rtlphrd a ulong to ulong
202rtlw32 PHYOCP shl div a 2 16
203i = ulong 0
204while lt i 20000 bound 20000
205rtldly 1
206f = rtlr32 PHYOCP
207if ne and f OCPFLAG 0
208v = rtlr32 PHYOCP
209ret and v 65535
210end
211i = add i 1
212end
213rtlwto
214ret 65535
215end
216
217note r8127_n.c:4872-4882
218fn rtlwaitll to void
219i = ulong 0
220while lt i 10 bound 10
221rtldly 100
222v = rtlr16 LLFIFO
223if ne and v 512 0
224ret
225end
226i = add i 1
227end
228rtlwto
229end
230
231note r8127_n.c:4287-4293
232fn rtlrxoff to void
233v = rtlr32 RCR
234rtlw32 RCR and v not RCRMASK
235end
236
237note r8127_n.c:4296-4321: gate RX, stop request, wait for both FIFOs and the mitigation idle bits, clear the stop request, soft reset, reset RCR.
238note The vendor ignores a reset that never completes; this driver fails with E_RSTTMO instead.
239fn rtlnicrst to ulong
240rtlrxoff
241v = rtlr8 RXDVGATE
242rtlw8 RXDVGATE or v 8
243v = rtlr8 CHIPCMD
244rtlw8 CHIPCMD or v CMDSTOP
245rtldly 200
246i = ulong 0
247while lt i 3000 bound 3000
248rtldly 50
249v = rtlr8 MCUCMD
250w = or MCUTXE MCURXE
251if eq and v w w
252break
253end
254i = add i 1
255end
256if eq i 3000
257rtlwto
258end
259i = ulong 0
260while lt i 3000 bound 3000
261rtldly 50
262v = rtlr16 INTRMIT
263if eq and v 259 259
264break
265end
266i = add i 1
267end
268if eq i 3000
269rtlwto
270end
271v = rtlr8 CHIPCMD
272rtlw8 CHIPCMD and v or CMDTXEN CMDRXEN
273rtlw8 CHIPCMD CMDRST
274i = ulong 0
275while lt i 100 bound 100
276rtldly 100
277v = rtlr8 CHIPCMD
278if eq and v CMDRST 0
279break
280end
281i = add i 1
282end
283if eq i 100
284ret E_RSTTMO
285end
286rtlw32 RCR RCRRST
287ret 0
288end
289
290note r8127_n.c:4507-4519 with interrupt version 1 (no MSI-X).
291fn rtlhwrst to ulong
292v = rtlr8 INTCFG0
293rtlw8 INTCFG0 and v not ulong 97
294rtlw32 IMR0 0
295v = rtlr32 ISR0
296rtlw32 ISR0 v
297rtlw32 TIMINT0 0
298rtlw32 TIMINT1 0
299rtlw32 TIMINT2 0
300rtlw32 TIMINT3 0
301ret rtlnicrst
302end
303
304note r8127_n.c:7340-7384 without DASH and RealWoW. The UPS-resume PHY wait is not done, only recorded (UNKNOWN on the chip).
305fn rtlexoob to ulong
306rtlrxoff
307rtlocwr 49340 255
308e = rtlnicrst
309if ne e 0
310ret e
311end
312v = rtlr8 MCUCMD
313rtlw8 MCUCMD and v not MCUOOB
314v = rtlocrd 59614
315rtlocwr 59614 and v not ulong 16384
316rtlwaitll
317rtlocwr 49322 2000
318rtlocwr 49318 437
319rtlocwr 49182 21845
320rtlwaitll
321v = rtlocrd 54316
322if ne and v 256 0
323put rs S_UPSRES 1
324else
325put rs S_UPSRES 0
326end
327ret 0
328end
329
330note r8127_n.c:5512-5519 and 13306-13318: PLL on, PHY out of power-down with autonegotiation, unless the board holds the PHY in disable mode (4029-4072).
331note PHY OCP 0xA400 (41984) is BMCR, 0xA420 (42016) holds the PHY state in its low 3 bits (3 = up), MAC OCP 0xDC04 (56324) bit 13 is the disable-mode flag.
332fn rtlpwrup to void
333v = rtlr8 PMCH
334rtlw8 PMCH or v 192
335g = rtlr8 RXDVGATE
336d = rtlocrd 56324
337if ne and g 32 0
338if eq and d 8192 0
339ret
340end
341end
342rtlphwr 41984 4096
343i = ulong 0
344while lt i 100 bound 100
345s = rtlphrd 42016
346s = and s 7
347put rs S_PHYST s
348if eq s 3
349ret
350end
351rtldly 1000
352i = add i 1
353end
354rtlwto
355end
356
357note 1 when the six bytes are a usable unicast address.
358fn rtlmacok m view uchar 6 to ulong
359if ne and ulong get m 0 1 0
360ret 0
361end
362z = ulong 0
363for i ulong 6
364z = or z ulong get m i
365end
366if eq z 0
367ret 0
368end
369ret 1
370end
371
372note r8127_n.c:12418-12441 and 12482-12500 (rar_set): backup address, else MAC0, else a locally administered fallback.
373fn rtlrdmac to void
374m = new uchar 6 0
375lo = rtlr32 BAKADR0
376hi = rtlr16 BAKADR1
377for i ulong 4
378put m i uchar and shr lo mul i 8 255
379end
380put m 4 uchar and hi 255
381put m 5 uchar and shr hi 8 255
382src = MS_BACK
383if eq rtlmacok m 0
384for i ulong 6
385put m i uchar rtlr8 add MAC0 i
386end
387src = MS_MAC0
388end
389if eq rtlmacok m 0
390put m 0 2
391put m 1 65
392put m 2 73
393put m 3 69
394put m 4 78
395put m 5 88
396src = MS_FALL
397end
398put rs S_MACSRC src
399for i ulong 6
400put rs add S_MAC0 i ulong get m i
401end
402v = rtlr8 CFG9346
403rtlw8 CFG9346 or v UNLOCK
404a = or or ulong get m 0 shl ulong get m 1 8 shl ulong get m 2 16
405rtlw32 MAC0 or a shl ulong get m 3 24
406b = or ulong get m 4 shl ulong get m 5 8
407rtlw32 MAC4 b
408v = rtlr8 CFG9346
409rtlw8 CFG9346 and v not UNLOCK
410end
411
412note ---- descriptors: little-endian words in rdma, opts1 written last ----
413fn rtlrxpost i ulong to void
414a = add add get rs S_DMA OFFRXB mul i STRIDE
415d = add OFFRXD mul i 16
416dst32 add d 4 0
417dst32 add d 8 and a 4294967295
418dst32 add d 12 shr a 32
419rtlbar
420f = ulong 0
421if eq i sub RXN 1
422f = DEOR
423end
424dst32 d or or DOWN f RXBUF
425end
426
427note Identify, stop, reset and bring up the chip: exit OOB, reset, PHY power up, MAC address, rings, TX and RX enabled, interrupts masked.
428note dma is the bus address the chip sees for rdma (4096-aligned, dma + MEMSIZE must not wrap). Returns 0 or an error code.
429fn rtlinit dma ulong to ulong
430for i ulong 32
431put rs i 0
432end
433if ne and dma 4095 0
434ret E_MEM
435end
436if lt add dma MEMSIZE dma
437ret E_MEM
438end
439put rs S_DMA dma
440for i ulong MEMSIZE
441put rdma i 0
442end
443tcr = rtlr32 TCR
444put rs S_TCRID tcr
445if eq tcr 4294967295
446ret E_ABSENT
447end
448if ne and tcr TCRIDM TCRID
449ret E_CHIP
450end
451ic = and tcr TCRICM
452if eq ic 0
453put rs S_MCFG 1
454else
455put rs S_MCFG 2
456end
457if and ne ic 0 ne ic 1048576
458put rs S_ICUNK 1
459end
460e = rtlexoob
461if ne e 0
462ret e
463end
464rtlpwrup
465e = rtlhwrst
466if ne e 0
467ret e
468end
469rtlrdmac
470for i ulong TXN
471f = ulong 0
472if eq i sub TXN 1
473f = DEOR
474end
475dst32 mul i 32 f
476end
477for i ulong RXN
478rtlrxpost i
479end
480rtlbar
481v = rtlr8 CFG9346
482rtlw8 CFG9346 or v UNLOCK
483tx = add dma OFFTXD
484rx = add dma OFFRXD
485rtlw32 TNPDSLO and tx 4294967295
486rtlw32 TNPDSHI shr tx 32
487rtlw32 RDSARLO and rx 4294967295
488rtlw32 RDSARHI shr rx 32
489rtlw32 TCR TCRVAL
490rtlw32 RSSCTRL 0
491v = rtlr16 QNUMCTL
492rtlw16 QNUMCTL and v not ulong 28
493v = rtlocrd OCPFMT
494rtlocwr OCPFMT or and v not ulong 3 1
495v = rtlr8 TXNOCLO
496rtlw8 TXNOCLO or v 4
497v = rtlr8 RXDTYPE
498rtlw8 RXDTYPE and v not ulong 2
499rtlw16 RMS or RMSVAL RMSVLAN
500v = rtlr8 RXDVGATE
501rtlw8 RXDVGATE and v not ulong 8
502rtlw32 RCR or or RCRCHIP RCRBCST RCRMYPH
503v = rtlr8 CFG9346
504rtlw8 CFG9346 and v not UNLOCK
505v = rtlr32 HWCLO0
506put rs S_CLOB and v CLOMASK
507rtlw8 CHIPCMD or CMDTXEN CMDRXEN
508put rs S_READY 1
509ret 0
510end
511
512note PHYstatus (r8127_n.c:4537-4547): 1 when the link is up. rs[S_MBPS] gets 10000/5000/2500/1000/100/10, or 0 when down or unknown.
513fn rtllink to ulong
514put rs S_MBPS 0
515s = rtlr16 PHYSTAT
516if eq s 65535
517ret 0
518end
519if eq and s PHYSLNK 0
520ret 0
521end
522m = ulong 0
523if ne and s 16384 0
524m = 10000
525else
526if ne and s 4096 0
527m = 5000
528else
529if ne and s 1024 0
530m = 2500
531else
532if ne and s 16 0
533m = 1000
534else
535if ne and s 8 0
536m = 100
537else
538if ne and s 4 0
539m = 10
540end
541end
542end
543end
544end
545end
546put rs S_MBPS m
547ret 1
548end
549
550fn rtlusable to ulong
551if ne get rs S_BROKE 0
552ret E_BROKEN
553end
554if eq get rs S_READY 0
555ret E_NOTRDY
556end
557ret 0
558end
559
560fn rtltxpend to ulong
561ret and sub get rs S_TXCUR get rs S_TXDIR 4294967295
562end
563
564note Send fbuf[0..n): 14..1514 bytes, no FCS, padded to 60. One descriptor, then the tail pointer (r8127_n.c:16427-16560 and the no-close doorbell 16412-16419).
565fn rtltx n ulong to ulong
566e = rtlusable
567if ne e 0
568ret e
569end
570if lt n 14
571ret E_ARG
572end
573if gt n FMAX
574ret E_ARG
575end
576if ge rtltxpend TXN
577ret E_FULL
578end
579cur = get rs S_TXCUR
580i = rem cur TXN
581sz = n
582if lt n FMIN
583sz = FMIN
584end
585b = add OFFTXB mul i STRIDE
586for k ulong sz
587if lt k n
588put rdma add b k get fbuf k
589else
590put rdma add b k 0
591end
592end
593a = add add get rs S_DMA OFFTXB mul i STRIDE
594d = mul i 32
595dst32 add d 4 0
596dst32 add d 8 and a 4294967295
597dst32 add d 12 shr a 32
598rtlbar
599f = ulong 0
600if eq i sub TXN 1
601f = DEOR
602end
603dst32 d or or or DOWN DFS DLS or f sz
604cur = and add cur 1 4294967295
605put rs S_TXCUR cur
606rtlw32 SWTAIL0 and add get rs S_CLOB cur CLOMASK
607ret 0
608end
609
610note TX completions: the close pointer's advance (r8127_n.c:16590-16620). rs[S_RECL] gets the count. A pointer that runs ahead of what was posted marks the device broken.
611fn rtltxrec to ulong
612put rs S_RECL 0
613e = rtlusable
614if ne e 0
615ret e
616end
617clo = rtlr32 HWCLO0
618done = and sub sub clo get rs S_CLOB get rs S_TXDIR CLOMASK
619if eq clo 4294967295
620put rs S_BROKE 1
621ret E_DEVICE
622end
623if gt done rtltxpend
624put rs S_BROKE 1
625ret E_DEVICE
626end
627put rs S_TXDIR and add get rs S_TXDIR done 4294967295
628put rs S_RECL done
629ret 0
630end
631
632note Copy f bytes of RX buffer i to obuf and publish the length.
633fn rxcopy i ulong f ulong to void
634b = add OFFRXB mul i STRIDE
635for k ulong f
636put obuf k get rdma add b k
637end
638put rs S_RXLEN f
639end
640fn rxdrop to void
641put rs S_RXDRP add get rs S_RXDRP 1
642end
643
644note Handle one RX slot. 0: nothing pending (the chip still owns it). 1: a frame was delivered. 2: a frame was dropped (error, runt or
645note multi-descriptor; slot reposted). 3: the chip reported a length past the buffer, so the device is marked broken.
646fn rxone to ulong
647i = rem get rs S_RXCUR RXN
648s = dld32 add OFFRXD mul i 16
649if ne and s DOWN 0
650ret 0
651end
652rtlbar
653n = and s RXLEN
654if gt n RXBUF
655put rs S_BROKE 1
656ret 3
657end
658w = or DFS DLS
659good = and and eq and s w w eq and s RXRES 0 ge n 18
660r = ulong 2
661if good
662rxcopy i sub n 4
663r = 1
664else
665rxdrop
666end
667rtlrxpost i
668put rs S_RXCUR and add get rs S_RXCUR 1 4294967295
669ret r
670end
671
672note Take at most one received frame into obuf (FCS stripped); rs[S_RXLEN] is its length, 0 when none is pending. r8127_n.c:17036-17250.
673fn rtlrx to ulong
674put rs S_RXLEN 0
675e = rtlusable
676if ne e 0
677ret e
678end
679t = ulong 0
680while lt t RXN bound 16
681r = rxone
682if eq r 0
683ret 0
684end
685if eq r 3
686ret E_DEVICE
687end
688if eq r 1
689ret 0
690end
691t = add t 1
692end
693ret 0
694end
695
696note Stop DMA only on a chip rtlinit identified (E_NOTRDY otherwise, nothing written): mask interrupts, close the RX gate, stop request, wait for the FIFOs, soft reset.
697fn rtlstop to ulong
698if eq get rs S_MCFG 0
699ret E_NOTRDY
700end
701put rs S_READY 0
702ret rtlhwrst
703end