Rev 1519 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1519 | Rev 1529 | ||
---|---|---|---|
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
2 | ;; ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;; |
3 | ;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
5 | ;; ;; |
6 | ;; ETHERNET.INC ;; |
6 | ;; ETHERNET.INC ;; |
7 | ;; ;; |
7 | ;; ;; |
8 | ;; Ethernet network layer for KolibriOS ;; |
8 | ;; Ethernet network layer for KolibriOS ;; |
9 | ;; ;; |
9 | ;; ;; |
10 | ;; Written by hidnplayr@kolibrios.org ;; |
10 | ;; Written by hidnplayr@kolibrios.org ;; |
11 | ;; ;; |
11 | ;; ;; |
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
12 | ;; GNU GENERAL PUBLIC LICENSE ;; |
13 | ;; Version 2, June 1991 ;; |
13 | ;; Version 2, June 1991 ;; |
14 | ;; ;; |
14 | ;; ;; |
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
15 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
16 | 16 | ||
17 | $Revision: 1519 $ |
17 | $Revision: 1529 $ |
18 | 18 | ||
19 | struct ETH_FRAME |
19 | struct ETH_FRAME |
20 | .DstMAC dp ? ; destination MAC-address |
20 | .DstMAC dp ? ; destination MAC-address |
21 | .SrcMAC dp ? ; source MAC-address |
21 | .SrcMAC dp ? ; source MAC-address |
22 | .Type dw ? ; type of the upper-layer protocol |
22 | .Type dw ? ; type of the upper-layer protocol |
23 | .Data: ; data (46-1500 bytes for a normal packet) |
23 | .Data: ; data (46-1500 bytes for a normal packet) |
24 | ends |
24 | ends |
25 | 25 | ||
26 | virtual at NET_DEVICE.end |
26 | virtual at NET_DEVICE.end |
27 | 27 | ||
28 | ETH_DEVICE: |
28 | ETH_DEVICE: |
29 | 29 | ||
30 | .set_mode dd ? |
30 | .set_mode dd ? |
31 | .get_mode dd ? |
31 | .get_mode dd ? |
32 | 32 | ||
33 | .set_MAC dd ? |
33 | .set_MAC dd ? |
34 | .get_MAC dd ? |
34 | .get_MAC dd ? |
35 | 35 | ||
36 | .mode dd ? |
36 | .mode dd ? |
37 | .mac dp ? |
37 | .mac dp ? |
38 | 38 | ||
39 | end virtual |
39 | end virtual |
40 | 40 | ||
41 | align 4 |
41 | align 4 |
42 | iglobal |
42 | iglobal |
43 | 43 | ||
44 | ETH_BROADCAST dp 0xffffffffffff |
44 | ETH_BROADCAST dp 0xffffffffffff |
45 | endg |
45 | endg |
46 | 46 | ||
47 | align 4 |
47 | align 4 |
48 | uglobal |
48 | uglobal |
49 | ETH_RUNNING dd ? |
49 | ETH_RUNNING dd ? |
50 | endg |
50 | endg |
51 | 51 | ||
52 | 52 | ||
53 | ;----------------------------------------------------------------- |
53 | ;----------------------------------------------------------------- |
54 | ; |
54 | ; |
55 | ; ETH_init |
55 | ; ETH_init |
56 | ; |
56 | ; |
57 | ; This function resets all ethernet variables |
57 | ; This function resets all ethernet variables |
58 | ; |
58 | ; |
59 | ; IN: / |
- | |
60 | ; OUT: / |
- | |
61 | ; |
- | |
62 | ;----------------------------------------------------------------- |
59 | ;----------------------------------------------------------------- |
63 | align 4 |
- | |
64 | ETH_init: |
60 | macro ETH_init { |
65 | 61 | ||
66 | mov [ETH_RUNNING], 0 |
62 | mov [ETH_RUNNING], 0 |
67 | 63 | ||
68 | ret |
64 | } |
69 | 65 | ||
70 | 66 | ||
71 | ;----------------------------------------------------------------- |
67 | ;----------------------------------------------------------------- |
72 | ; |
68 | ; |
73 | ; ETH_Receiver: |
69 | ; ETH_input |
74 | ; |
70 | ; |
75 | ; This function is called by ethernet drivers, |
71 | ; This function is called by ethernet drivers, |
76 | ; It pushes the received ethernet packets onto the eth_in_queue |
72 | ; It pushes the received ethernet packets onto the eth_in_queue |
77 | ; |
73 | ; |
78 | ; IN: [esp] = Pointer to buffer |
74 | ; IN: [esp] = Pointer to buffer |
79 | ; [esp-4] = size of buffer |
75 | ; [esp+4] = size of buffer |
80 | ; ebx = pointer to eth_device |
76 | ; ebx = pointer to eth_device |
81 | ; OUT: / |
77 | ; OUT: / |
82 | ; |
78 | ; |
83 | ;----------------------------------------------------------------- |
79 | ;----------------------------------------------------------------- |
84 | align 4 |
80 | align 4 |
85 | ETH_receiver: |
81 | ETH_input: |
86 | mov eax, [esp] |
82 | mov eax, [esp] |
87 | mov ecx, [esp+4] |
83 | mov ecx, [esp+4] |
88 | 84 | ||
89 | DEBUGF 1,"ETH_Handler - size: %u\n", ecx |
85 | DEBUGF 1,"ETH_input - size: %u\n", ecx |
90 | cmp ecx, 60 ; check packet length |
86 | cmp ecx, 60 ; check packet length |
91 | jl .dump |
87 | jl .dump |
92 | sub ecx, ETH_FRAME.Data |
88 | sub ecx, ETH_FRAME.Data |
93 | 89 | ||
94 | lea edx, [eax + ETH_FRAME.Data] |
90 | lea edx, [eax + ETH_FRAME.Data] |
95 | mov ax , [eax + ETH_FRAME.Type] |
91 | mov ax , [eax + ETH_FRAME.Type] |
96 | 92 | ||
97 | cmp ax, ETHER_IPv4 |
93 | cmp ax, ETHER_IPv4 |
98 | je IPv4_handler |
94 | je IPv4_input |
99 | 95 | ||
100 | cmp ax, ETHER_ARP |
96 | cmp ax, ETHER_ARP |
101 | je ARP_handler |
97 | je ARP_input |
102 | 98 | ||
103 | ; cmp ax, ETHER_PPP_DISCOVERY |
99 | ; cmp ax, ETHER_PPP_DISCOVERY |
104 | ; je PPPOE_discovery |
100 | ; je PPPOE_discovery |
105 | 101 | ||
106 | DEBUGF 2,"Unknown ethernet packet type %x\n", ax |
102 | DEBUGF 2,"Unknown ethernet packet type %x\n", ax |
107 | 103 | ||
108 | .dump: |
104 | .dump: |
109 | DEBUGF 2,"ETH_Handler - dumping\n" |
105 | DEBUGF 2,"ETH_input - dumping\n" |
110 | call kernel_free |
106 | call kernel_free |
111 | add esp, 4 |
107 | add esp, 4 |
112 | ret |
108 | ret |
113 | 109 | ||
114 | ;----------------------------------------------------------------- |
110 | ;----------------------------------------------------------------- |
115 | ; |
111 | ; |
116 | ; ETH_create_packet |
112 | ; ETH_output |
117 | ; |
113 | ; |
118 | ; IN: eax = pointer to source mac |
114 | ; IN: eax = pointer to source mac |
119 | ; ebx = pointer to destination mac |
115 | ; ebx = device ptr |
120 | ; ecx = packet size |
116 | ; ecx = packet size |
121 | ; edx = device number |
117 | ; edx = pointer to destination mac |
122 | ; di = protocol |
118 | ; di = protocol |
123 | ; |
119 | ; |
124 | ; OUT: edi = 0 on error, pointer to buffer otherwise |
120 | ; OUT: edi = 0 on error, pointer to buffer otherwise |
125 | ; eax = buffer start |
121 | ; eax = buffer start |
126 | ; ebx = to device structure |
122 | ; ebx = to device structure |
127 | ; ecx = unchanged (packet size of embedded data) |
123 | ; ecx = unchanged (packet size of embedded data) |
128 | ; edx = size of complete buffer |
124 | ; edx = size of complete buffer |
129 | ; |
125 | ; |
130 | ;----------------------------------------------------------------- |
126 | ;----------------------------------------------------------------- |
131 | align 4 |
127 | align 4 |
132 | ETH_create_packet: |
128 | ETH_output: |
133 | 129 | ||
134 | DEBUGF 1,"Creating Ethernet Packet (size=%u): \n", ecx |
- | |
135 | - | ||
136 | push esi |
130 | DEBUGF 1,"ETH_output: size=%u device:%x\n", ecx, ebx |
137 | mov esi, [NET_DRV_LIST] ;;; TODO: FIXME |
- | |
138 | cmp ecx, [esi + NET_DEVICE.mtu] |
131 | |
- | 132 | cmp ecx, [ebx + NET_DEVICE.mtu] |
|
139 | pop esi |
133 | jg .exit |
140 | jg .exit |
- | |
141 | 134 | ||
- | 135 | push ecx ; << 1 |
|
142 | push ecx di eax ebx edx |
136 | push di eax edx ; << 2 |
- | 137 | add ecx, ETH_FRAME.Data |
|
143 | 138 | ||
144 | add ecx, ETH_FRAME.Data |
139 | push ecx ; << 3 |
- | 140 | ||
- | 141 | push ecx ; << 4 |
|
145 | push ecx |
142 | call kernel_alloc ; >> 4 |
146 | push ecx |
- | |
147 | call kernel_alloc |
- | |
148 | mov edi, eax |
143 | test eax, eax |
149 | test edi, edi |
- | |
150 | jz .pop_exit |
144 | jz .out_of_ram |
151 | 145 | mov edi, eax |
|
152 | pop ecx |
146 | |
153 | pop edx |
147 | pop ecx ; >> 3 |
154 | 148 | ||
155 | pop esi |
149 | pop esi ; >> 2 |
156 | movsd |
150 | movsd |
157 | movsw |
151 | movsw |
158 | pop esi |
152 | pop esi ; >> 2 |
159 | movsd |
153 | movsd |
160 | movsw |
154 | movsw |
161 | pop ax |
155 | pop ax ; >> 2 |
162 | stosw |
156 | stosw |
163 | 157 | ||
164 | lea eax, [edi - ETH_FRAME.Data] ; Set eax to buffer start |
158 | lea eax, [edi - ETH_FRAME.Data] ; Set eax to buffer start |
165 | mov edx, ecx ; Set ebx to complete buffer size |
159 | mov edx, ecx ; Set edx to complete buffer size |
166 | pop ecx |
- | |
167 | 160 | ||
168 | xor ebx, ebx ;;;; TODO: Fixme |
- | |
169 | mov ebx, [NET_DRV_LIST + ebx] |
161 | pop ecx ; >> 1 |
170 | 162 | ||
171 | cmp edx, 46 + ETH_FRAME.Data ; If data size is less then 46, add padding bytes |
163 | cmp edx, 60-1 ; minimum ethernet packet size |
172 | jge .continue |
164 | jle .adjust_size |
- | 165 | DEBUGF 1,"ETH_output: done: %x total size: %u\n", eax, edx |
|
173 | mov edx, 46 + ETH_FRAME.Data |
166 | ret |
174 | .continue: |
167 | |
175 | 168 | .adjust_size: |
|
176 | DEBUGF 1,"done: %x size:%u device:%x\n", eax, edx, ebx |
169 | mov edx, 60 |
177 | ret |
170 | ret |
178 | 171 | ||
179 | .pop_exit: |
172 | .out_of_ram: |
180 | DEBUGF 2,"Out of ram space!!\n" |
173 | DEBUGF 2,"ETH_output: Out of ram space!!\n" |
181 | add esp, 18 |
174 | add esp, 3*4+2+4 |
182 | and edi, 0 |
175 | sub edi, edi |
183 | ret |
176 | ret |
184 | 177 | ||
185 | .exit: |
178 | .exit: |
186 | DEBUGF 2,"Packet too large!\n" |
179 | DEBUGF 2,"ETH_output: Packet too large!\n" |
187 | and edi, 0 |
180 | sub edi, edi |
- | 181 | ;;; dec edi |
|
188 | ret |
182 | ret |
189 | 183 | ||
190 | 184 | ||
191 | 185 | ||
192 | ;----------------------------------------------------------------- |
186 | ;----------------------------------------------------------------- |
193 | ; |
187 | ; |
194 | ; ETH_API |
188 | ; ETH_API |
195 | ; |
189 | ; |
196 | ; This function is called by system function 75 |
190 | ; This function is called by system function 75 |
197 | ; |
191 | ; |
198 | ; IN: subfunction number in bl |
192 | ; IN: subfunction number in bl |
199 | ; device number in bh |
193 | ; device number in bh |
200 | ; ecx, edx, .. depends on subfunction |
194 | ; ecx, edx, .. depends on subfunction |
201 | ; |
195 | ; |
202 | ; OUT: |
196 | ; OUT: |
203 | ; |
197 | ; |
204 | ;----------------------------------------------------------------- |
198 | ;----------------------------------------------------------------- |
205 | align 4 |
199 | align 4 |
206 | ETH_API: |
200 | ETH_API: |
207 | 201 | ||
208 | cmp bh, MAX_NET_DEVICES |
202 | cmp bh, MAX_NET_DEVICES |
209 | jg .error |
203 | jg .error |
210 | movzx eax, bh |
204 | movzx eax, bh |
211 | shl eax, 2 |
205 | shl eax, 2 |
212 | 206 | ||
213 | cmp bl, 7 |
207 | cmp bl, 7 |
214 | jz .out_queue |
208 | jz .out_queue |
215 | cmp bl, 6 |
209 | cmp bl, 6 |
216 | jz .in_queue |
210 | jz .in_queue |
217 | 211 | ||
218 | mov eax, dword [NET_DRV_LIST + eax] |
212 | mov eax, dword [NET_DRV_LIST + eax] |
219 | cmp [eax + NET_DEVICE.type], NET_TYPE_ETH |
213 | cmp [eax + NET_DEVICE.type], NET_TYPE_ETH |
220 | jne .error |
214 | jne .error |
221 | 215 | ||
222 | test bl, bl |
216 | test bl, bl |
223 | jz .packets_tx ; 0 |
217 | jz .packets_tx ; 0 |
224 | dec bl |
218 | dec bl |
225 | jz .packets_rx ; 1 |
219 | jz .packets_rx ; 1 |
226 | dec bl |
220 | dec bl |
227 | jz .bytes_tx ; 2 |
221 | jz .bytes_tx ; 2 |
228 | dec bl |
222 | dec bl |
229 | jz .bytes_rx ; 3 |
223 | jz .bytes_rx ; 3 |
230 | dec bl |
224 | dec bl |
231 | jz .read_mac ; 4 |
225 | jz .read_mac ; 4 |
232 | dec bl |
226 | dec bl |
233 | jz .write_mac ; 5 |
227 | jz .write_mac ; 5 |
234 | 228 | ||
235 | .error: |
229 | .error: |
236 | DEBUGF 2,"Device is not ethernet type\n" |
230 | DEBUGF 2,"Device is not ethernet type\n" |
237 | or eax, -1 |
231 | or eax, -1 |
238 | ret |
232 | ret |
239 | 233 | ||
240 | .packets_tx: |
234 | .packets_tx: |
241 | mov eax, dword [eax + NET_DEVICE.packets_tx] |
235 | mov eax, dword [eax + NET_DEVICE.packets_tx] |
242 | 236 | ||
243 | ret |
237 | ret |
244 | 238 | ||
245 | .packets_rx: |
239 | .packets_rx: |
246 | mov eax, dword [eax + NET_DEVICE.packets_rx] |
240 | mov eax, dword [eax + NET_DEVICE.packets_rx] |
247 | ret |
241 | ret |
248 | 242 | ||
249 | .bytes_tx: |
243 | .bytes_tx: |
250 | mov ebx, dword [eax + NET_DEVICE.bytes_tx + 4] |
244 | mov ebx, dword [eax + NET_DEVICE.bytes_tx + 4] |
251 | mov eax, dword [eax + NET_DEVICE.bytes_tx] |
245 | mov eax, dword [eax + NET_DEVICE.bytes_tx] |
252 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
246 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
253 | ret |
247 | ret |
254 | 248 | ||
255 | .bytes_rx: |
249 | .bytes_rx: |
256 | mov ebx, dword [eax + NET_DEVICE.bytes_rx + 4] |
250 | mov ebx, dword [eax + NET_DEVICE.bytes_rx + 4] |
257 | mov eax, dword [eax + NET_DEVICE.bytes_rx] |
251 | mov eax, dword [eax + NET_DEVICE.bytes_rx] |
258 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
252 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
259 | ret |
253 | ret |
260 | 254 | ||
261 | 255 | ||
262 | .read_mac: |
256 | .read_mac: |
263 | movzx ebx, word [eax + ETH_DEVICE.mac] |
257 | movzx ebx, word [eax + ETH_DEVICE.mac] |
264 | mov eax, dword [eax + ETH_DEVICE.mac + 2] |
258 | mov eax, dword [eax + ETH_DEVICE.mac + 2] |
265 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
259 | mov [esp+20+4], ebx ; TODO: fix this ugly code |
266 | ret |
260 | ret |
267 | 261 | ||
268 | .write_mac: |
262 | .write_mac: |
269 | push ecx |
263 | push ecx |
270 | push dx |
264 | push dx |
271 | call [eax + ETH_DEVICE.set_MAC] |
265 | call [eax + ETH_DEVICE.set_MAC] |
272 | ret |
266 | ret |
273 | 267 | ||
274 | .in_queue: |
268 | .in_queue: |
275 | if ETH_QUEUE |
269 | if ETH_QUEUE |
276 | add eax, ETH_IN_QUEUE |
270 | add eax, ETH_IN_QUEUE |
277 | mov eax, [eax + queue.size] |
271 | mov eax, [eax + queue.size] |
278 | else |
272 | else |
279 | or eax, -1 |
273 | or eax, -1 |
280 | end if |
274 | end if |
281 | ret |
275 | ret |
282 | 276 | ||
283 | .out_queue: |
277 | .out_queue: |
284 | if ETH_QUEUE |
278 | if ETH_QUEUE |
285 | add eax, ETH_OUT_QUEUE |
279 | add eax, ETH_OUT_QUEUE |
286 | mov eax, [eax + queue.size] |
280 | mov eax, [eax + queue.size] |
287 | else |
281 | else |
288 | or eax, -1 |
282 | or eax, -1 |
289 | end if |
283 | end if |
290 | ret |
284 | ret><>><>><>><> |