1313; Initialize an Intel 8257x NIC
1414; IN: RDX = Packed Bus address (as per syscalls/bus.asm)
1515net_i8257x_init:
16+ push rdi
1617 push rsi
1718 push rdx
1819 push rcx
1920 push rax
2021
22+ mov rdi , net_table
23+ xor eax , eax
24+ mov al , [ os_net_icount ]
25+ shl eax , 7 ; Quick multiply by 128
26+ add rdi , rax
27+
28+ mov ax , 0x8257 ; Driver tag for i8257x
29+ stosw
30+ add rdi , 14
31+
2132 ; Get the Base Memory Address of the device
2233 mov al , 0 ; Read BAR0
2334 call os_bus_read_bar
24- mov [ os_NetIOBaseMem ], rax ; Save it as the base
25- mov [ os_NetIOLength ], rcx ; Save the length
35+ stosq ; Save the base
36+ push rax ; Save the base for gathering the MAC later
37+ mov rax , rcx
38+ stosq ; Save the length
2639
2740 ; Set PCI Status/Command values
2841 mov dl , 0x01 ; Read Status/Command
@@ -33,29 +46,53 @@ net_i8257x_init:
3346 call os_bus_write ; Write updated Status/Command
3447
3548 ; Get the MAC address
36- mov rsi , [ os_NetIOBaseMem ]
49+ pop rsi ; Restore the base
50+ sub rdi , 24 ; 8 bytes into net table entry
3751 mov eax , [ rsi + i8257x_RAL ] ; RAL
38- mov [ os_NetMAC ], al
52+ stosb
3953 shr eax , 8
40- mov [ os_NetMAC + 1 ], al
54+ stosb
4155 shr eax , 8
42- mov [ os_NetMAC + 2 ], al
56+ stosb
4357 shr eax , 8
44- mov [ os_NetMAC + 3 ], al
58+ stosb
4559 mov eax , [ rsi + i8257x_RAH ] ; RAH
46- mov [ os_NetMAC + 4 ], al
60+ stosb
4761 shr eax , 8
48- mov [ os_NetMAC + 5 ], al
62+ stosb
63+
64+ ; Set base addresses for TX and RX descriptors
65+ xor ecx , ecx
66+ mov cl , byte [ os_net_icount ]
67+ shl ecx , 15
68+
69+ add rdi , 0x22
70+ mov rax , os_tx_desc
71+ add rax , rcx
72+ stosq
73+ mov rax , os_rx_desc
74+ add rax , rcx
75+ stosq
4976
5077 ; Reset the device
78+ xor edx , edx
79+ mov dl , [ os_net_icount ]
5180 call net_i8257x_reset
5281
82+ ; Store call addresses
83+ sub rdi , 0x20
84+ mov rax , net_i8257x_transmit
85+ stosq
86+ mov rax , net_i8257x_poll
87+ stosq
88+
5389net_i8257x_init_error:
5490
5591 pop rax
5692 pop rcx
5793 pop rdx
5894 pop rsi
95+ pop rdi
5996 ret
6097; -----------------------------------------------------------------------------
6198
@@ -69,7 +106,14 @@ net_i8257x_reset:
69106 push rsi
70107 push rax
71108
72- mov rsi , [ os_NetIOBaseMem ]
109+ ; Gather Base Address from net_table
110+ mov rsi , net_table
111+ xor eax , eax
112+ mov al , [ os_net_icount ]
113+ shl eax , 7 ; Quick multiply by 128
114+ add rsi , rax
115+ add rsi , 16
116+ mov rsi , [ rsi ]
73117 mov rdi , rsi
74118
75119 ; Disable Interrupts (14.4)
@@ -114,7 +158,11 @@ net_i8257x_init_reset_wait:
114158 ; Create RX descriptors
115159 push rdi
116160 mov ecx , i8257x_MAX_DESC
161+ xor eax , eax
162+ mov al , byte [ os_net_icount ]
163+ shl eax , 15
117164 mov rdi , os_rx_desc
165+ add rdi , rax
118166net_i8257x_reset_nextdesc:
119167 mov rax , os_PacketBuffers ; Default packet will go here
120168 stosq
@@ -125,7 +173,10 @@ net_i8257x_reset_nextdesc:
125173 pop rdi
126174
127175 ; Initialize receive (14.6)
128- mov rax , os_rx_desc
176+ xor eax , eax
177+ mov al , byte [ os_net_icount ]
178+ shl eax , 15
179+ add rax , os_rx_desc
129180 mov [ rsi + i8257x_RDBAL ], eax ; Receive Descriptor Base Address Low
130181 shr rax , 32
131182 mov [ rsi + i8257x_RDBAH ], eax ; Receive Descriptor Base Address High
@@ -139,7 +190,10 @@ net_i8257x_reset_nextdesc:
139190 mov [ rsi + i8257x_RCTL ], eax ; Receive Control Register
140191
141192 ; Initialize transmit (14.7)
142- mov rax , os_tx_desc
193+ xor eax , eax
194+ mov al , byte [ os_net_icount ]
195+ shl eax , 15
196+ add rax , os_tx_desc
143197 mov [ rsi + i8257x_TDBAL ], eax ; Transmit Descriptor Base Address Low
144198 shr rax , 32
145199 mov [ rsi + i8257x_TDBAH ], eax ; Transmit Descriptor Base Address High
@@ -184,10 +238,10 @@ net_i8257x_transmit:
184238 push rdi
185239 push rax
186240
187- mov rdi , os_tx_desc ; Transmit Descriptor Base Address
241+ mov rdi , [ rdx + nt_tx_desc ] ; Transmit Descriptor Base Address
188242
189243 ; Calculate the descriptor to write to
190- mov eax , [ i8257x_tx_lasttail ]
244+ mov eax , [ rdx + nt_tx_head ] ; Get tx_lasttail
191245 push rax ; Save lasttail
192246 shl eax , 4 ; Quick multiply by 16
193247 add rdi , rax ; Add offset to RDI
@@ -205,8 +259,8 @@ net_i8257x_transmit:
205259 pop rax ; Restore lasttail
206260 add eax , 1
207261 and eax , i8257x_MAX_DESC - 1
208- mov [ i8257x_tx_lasttail ], eax
209- mov rdi , [ os_NetIOBaseMem ]
262+ mov [ rdx + nt_tx_head ], eax ; Set tx_lasttail
263+ mov rdi , [ rdx + nt_base ] ; Load the base MMIO of the NIC
210264 mov [ rdi + i8257x_TDT ], eax ; TDL - Transmit Descriptor Tail
211265
212266 pop rax
@@ -233,11 +287,11 @@ net_i8257x_poll:
233287 push rsi ; Used for the base MMIO of the NIC
234288 push rax
235289
236- mov rdi , os_rx_desc
237- mov rsi , [ os_NetIOBaseMem ] ; Load the base MMIO of the NIC
290+ mov rdi , [ rdx + nt_rx_desc ]
291+ mov rsi , [ rdx + nt_base ] ; Load the base MMIO of the NIC
238292
239293 ; Calculate the descriptor to read from
240- mov eax , [ i8257x_rx_lasthead ]
294+ mov eax , [ rdx + nt_rx_head ] ; Get rx_lasthead
241295 shl eax , 4 ; Quick multiply by 16
242296 add eax , 8 ; Offset to bytes received
243297 add rdi , rax ; Add offset to RDI
@@ -251,10 +305,11 @@ net_i8257x_poll:
251305 stosq ; Clear the descriptor length and status
252306
253307 ; Increment i8257x_rx_lasthead and the Receive Descriptor Tail
254- mov eax , [ i8257x_rx_lasthead ]
308+ mov eax , [ rdx + nt_rx_head ] ; Get rx_lasthead
255309 add eax , 1
256310 and eax , i8257x_MAX_DESC - 1
257- mov [ i8257x_rx_lasthead ], eax
311+ mov [ rdx + nt_rx_head ], eax ; Set rx_lasthead
312+
258313 mov eax , [ rsi + i8257x_RDT ] ; Read the current Receive Descriptor Tail
259314 add eax , 1 ; Add 1 to the Receive Descriptor Tail
260315 and eax , i8257x_MAX_DESC - 1
@@ -274,10 +329,6 @@ net_i8257x_poll_end:
274329; -----------------------------------------------------------------------------
275330
276331
277- ; Variables
278- i8257x_tx_lasttail: dd 0
279- i8257x_rx_lasthead: dd 0
280-
281332; Constants
282333i8257x_MAX_PKT_SIZE equ 16384
283334i8257x_MAX_DESC equ 16 ; Must be 16, 32, 64, 128, etc.
0 commit comments