1010; Initialize an Intel 8254x NIC
1111; IN: RDX = Packed Bus address (as per syscalls/bus.asm)
1212net_i8254x_init:
13+ push rdi
1314 push rsi
1415 push rdx
1516 push rcx
1617 push rax
1718
19+ mov rdi , net_table
20+ xor eax , eax
21+ mov al , [ os_net_icount ]
22+ shl eax , 7 ; Quick multiply by 128
23+ add rdi , rax
24+
25+ mov ax , 0x8254 ; Driver tag for i8254x
26+ stosw
27+ add rdi , 14
28+
1829 ; Get the Base Memory Address of the device
1930 mov al , 0 ; Read BAR0
2031 call os_bus_read_bar
21- mov [ os_NetIOBaseMem ], rax ; Save it as the base
22- mov [ os_NetIOLength ], rcx ; Save the length
32+ stosq ; Save the base
33+ push rax ; Save the base for gathering the MAC later
34+ mov rax , rcx
35+ stosq ; Save the length
2336
2437 ; Set PCI Status/Command values
2538 mov dl , 0x01 ; Read Status/Command
@@ -30,43 +43,74 @@ net_i8254x_init:
3043 call os_bus_write ; Write updated Status/Command
3144
3245 ; Get the MAC address
33- mov rsi , [ os_NetIOBaseMem ]
46+ pop rsi ; Restore the base
47+ sub rdi , 24 ; 8 bytes into net table entry
3448 mov eax , [ rsi + i8254x_RAL ] ; RAL
35- mov [ os_NetMAC ], al
49+ stosb
3650 shr eax , 8
37- mov [ os_NetMAC + 1 ], al
51+ stosb
3852 shr eax , 8
39- mov [ os_NetMAC + 2 ], al
53+ stosb
4054 shr eax , 8
41- mov [ os_NetMAC + 3 ], al
55+ stosb
4256 mov eax , [ rsi + i8254x_RAH ] ; RAH
43- mov [ os_NetMAC + 4 ], al
57+ stosb
4458 shr eax , 8
45- mov [ os_NetMAC + 5 ], al
59+ stosb
60+
61+ ; Set base addresses for TX and RX descriptors
62+ xor ecx , ecx
63+ mov cl , byte [ os_net_icount ]
64+ shl ecx , 15
65+
66+ add rdi , 0x22
67+ mov rax , os_tx_desc
68+ add rax , rcx
69+ stosq
70+ mov rax , os_rx_desc
71+ add rax , rcx
72+ stosq
4673
4774 ; Reset the device
75+ xor edx , edx
76+ mov dl , [ os_net_icount ]
4877 call net_i8254x_reset
4978
79+ ; Store call addresses
80+ sub rdi , 0x20
81+ mov rax , net_i8254x_transmit
82+ stosq
83+ mov rax , net_i8254x_poll
84+ stosq
85+
5086net_i8254x_init_error:
5187
5288 pop rax
5389 pop rcx
5490 pop rdx
5591 pop rsi
92+ pop rdi
5693 ret
5794; -----------------------------------------------------------------------------
5895
5996
6097; -----------------------------------------------------------------------------
6198; net_i8254x_reset - Reset an Intel 8254x NIC
62- ; IN: Nothing
99+ ; IN: RDX = Interface ID
63100; OUT: Nothing, all registers preserved
64101net_i8254x_reset:
65102 push rdi
66103 push rsi
67104 push rax
68105
69- mov rsi , [ os_NetIOBaseMem ]
106+ ; Gather Base Address from net_table
107+ mov rsi , net_table
108+ xor eax , eax
109+ mov al , [ os_net_icount ]
110+ shl eax , 7 ; Quick multiply by 128
111+ add rsi , rax
112+ add rsi , 16
113+ mov rsi , [ rsi ]
70114 mov rdi , rsi
71115
72116 ; Disable Interrupts
@@ -110,8 +154,12 @@ net_i8254x_reset:
110154 ; Create RX descriptors
111155 push rdi
112156 mov ecx , i8254x_MAX_DESC
157+ xor eax , eax
158+ mov al , byte [ os_net_icount ]
159+ shl eax , 15
113160 mov rdi , os_rx_desc
114- net_i8254x_reset_nextdesc:
161+ add rdi , rax
162+ net_i8254x_reset_nextdesc:
115163 mov rax , os_PacketBuffers ; Default packet will go here
116164 stosq
117165 xor eax , eax
@@ -121,7 +169,10 @@ net_i8254x_reset_nextdesc:
121169 pop rdi
122170
123171 ; Initialize receive
124- mov rax , os_rx_desc
172+ xor eax , eax
173+ mov al , byte [ os_net_icount ]
174+ shl eax , 15
175+ add rax , os_rx_desc
125176 mov [ rsi + i8254x_RDBAL ], eax ; Receive Descriptor Base Address Low
126177 shr rax , 32
127178 mov [ rsi + i8254x_RDBAH ], eax ; Receive Descriptor Base Address High
@@ -135,7 +186,10 @@ net_i8254x_reset_nextdesc:
135186 mov [ rsi + i8254x_RCTL ], eax ; Receive Control Register
136187
137188 ; Initialize transmit
138- mov rax , os_tx_desc
189+ xor eax , eax
190+ mov al , byte [ os_net_icount ]
191+ shl eax , 15
192+ add rax , os_tx_desc
139193 mov [ rsi + i8254x_TDBAL ], eax ; Transmit Descriptor Base Address Low
140194 shr rax , 32
141195 mov [ rsi + i8254x_TDBAH ], eax ; Transmit Descriptor Base Address High
@@ -164,6 +218,7 @@ net_i8254x_reset_nextdesc:
164218; -----------------------------------------------------------------------------
165219; net_i8254x_transmit - Transmit a packet via an Intel 8254x NIC
166220; IN: RSI = Location of packet
221+ ; RDX = Interface ID
167222; RCX = Length of packet
168223; OUT: Nothing
169224; Note: This driver uses the "legacy format" so TDESC.CMD.DEXT (5) is cleared to 0
@@ -182,10 +237,10 @@ net_i8254x_transmit:
182237 push rdi
183238 push rax
184239
185- mov rdi , os_tx_desc ; Transmit Descriptor Base Address
240+ mov rdi , [ rdx + nt_tx_desc ] ; Transmit Descriptor Base Address
186241
187242 ; Calculate the descriptor to write to
188- mov eax , [ i8254x_tx_lasttail ]
243+ mov eax , [ rdx + nt_tx_tail ] ; Get tx_lasttail
189244 push rax ; Save lasttail
190245 shl eax , 4 ; Quick multiply by 16
191246 add rdi , rax ; Add offset to RDI
@@ -203,8 +258,8 @@ net_i8254x_transmit:
203258 pop rax ; Restore lasttail
204259 add eax , 1
205260 and eax , i8254x_MAX_DESC - 1
206- mov [ i8254x_tx_lasttail ], eax
207- mov rdi , [ os_NetIOBaseMem ]
261+ mov [ rdx + nt_tx_tail ], eax ; Set tx_lasttail
262+ mov rdi , [ rdx + nt_base ] ; Load the base MMIO of the NIC
208263 mov [ rdi + i8254x_TDT ], eax ; TDL - Transmit Descriptor Tail
209264
210265 pop rax
@@ -216,6 +271,7 @@ net_i8254x_transmit:
216271; -----------------------------------------------------------------------------
217272; net_i8254x_poll - Polls the Intel 8254x NIC for a received packet
218273; IN: RDI = Location to store packet
274+ ; RDX = Interface ID
219275; OUT: RCX = Length of packet
220276; Note: RDESC Descriptor Format:
221277; First Qword:
@@ -231,11 +287,11 @@ net_i8254x_poll:
231287 push rsi ; Used for the base MMIO of the NIC
232288 push rax
233289
234- mov rdi , os_rx_desc
235- 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
236292
237293 ; Calculate the descriptor to read from
238- mov eax , [ i8254x_rx_lasthead ]
294+ mov eax , [ rdx + nt_rx_head ] ; Get rx_lasthead
239295 shl eax , 4 ; Quick multiply by 16
240296 add eax , 8 ; Offset to bytes received
241297 add rdi , rax ; Add offset to RDI
@@ -249,10 +305,11 @@ net_i8254x_poll:
249305 stosq ; Clear the descriptor length and status
250306
251307 ; Increment i8254x_rx_lasthead and the Receive Descriptor Tail
252- mov eax , [ i8254x_rx_lasthead ]
308+ mov eax , [ rdx + nt_rx_head ] ; Get rx_lasthead
253309 add eax , 1
254310 and eax , i8254x_MAX_DESC - 1
255- mov [ i8254x_rx_lasthead ], eax
311+ mov [ rdx + nt_rx_head ], eax ; Set rx_lasthead
312+
256313 mov eax , [ rsi + i8254x_RDT ] ; Read the current Receive Descriptor Tail
257314 add eax , 1 ; Add 1 to the Receive Descriptor Tail
258315 and eax , i8254x_MAX_DESC - 1
@@ -288,10 +345,6 @@ net_i8254x_poll_end:
288345; -----------------------------------------------------------------------------
289346
290347
291- ; Variables
292- i8254x_tx_lasttail: dd 0
293- i8254x_rx_lasthead: dd 0
294-
295348; Constants
296349i8254x_MAX_PKT_SIZE equ 16384
297350i8254x_MAX_DESC equ 16 ; Must be 16, 32, 64, 128, etc.
0 commit comments