Subversion Repositories Kolibri OS

Rev

Rev 1249 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
1196 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2009. All rights reserved.    ;;
1159 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  ETHERNET.INC                                                   ;;
7
;;                                                                 ;;
8
;;  Ethernet network layer for KolibriOS                           ;;
9
;;                                                                 ;;
10
;;    Written by hidnplayr@kolibrios.org                           ;;
11
;;                                                                 ;;
12
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;             Version 2, June 1991                                ;;
14
;;                                                                 ;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
 
1206 hidnplayr 17
$Revision: 1254 $
1159 hidnplayr 18
 
19
MAX_ETH_DEVICES 	equ MAX_NET_DEVICES
20
ETH_QUEUE_SIZE		equ 16
21
 
22
struct	ETH_FRAME
23
	.DstMAC 	dp  ?  ; destination MAC-address [6 bytes]
24
	.SrcMAC 	dp  ?  ; source MAC-address [6 bytes]
25
	.Type		dw  ?  ; type of the upper-layer protocol [2 bytes]
26
	.Data:		       ; data [46-1500 bytes]
27
ends
28
 
29
struct	ETH_DEVICE
30
	.unload 	dd ?
31
	.reset		dd ?
32
	.transmit	dd ?
33
	.set_MAC	dd ?
34
	.get_MAC	dd ?
35
	.set_mode	dd ?
36
	.get_mode	dd ?
37
 
38
	.bytes_tx	dq ?
39
	.bytes_rx	dq ?
40
	.packets_tx	dd ?
41
	.packets_rx	dd ?
42
	.mode		dd ?  ; This dword contains cable status (10mbit/100mbit, full/half duplex, auto negotiation or not,..)
43
	.name		dd ?
44
	.mac		dp ?
45
ends			      ; the rest of the device struct depends on the type of device
46
 
47
 
48
align 4
1196 hidnplayr 49
iglobal
50
 
51
	ETH_BROADCAST	dp  0xffffffffffff
52
endg
53
 
54
align 4
1159 hidnplayr 55
uglobal
56
 
57
	ETH_RUNNING	dd  ?
58
	ETH_DRV_LIST	rd  MAX_ETH_DEVICES
59
	ETH_IN_QUEUE	rd  3*ETH_QUEUE_SIZE+3
60
	ETH_OUT_QUEUE	rd  3*ETH_QUEUE_SIZE+3
61
endg
62
 
63
 
64
;-----------------------------------------------
65
;
66
; ETH_init
67
;
68
;  This function resets all ethernet variables
69
;
70
;  IN:  /
71
;  OUT: /
72
;
73
;-----------------------------------------------
74
 
75
align 4
76
ETH_init:
77
 
78
	xor	eax, eax
79
	mov	edi, ETH_RUNNING
80
	mov	ecx, (1+MAX_ETH_DEVICES)
81
	rep	stosd
82
 
1249 hidnplayr 83
	init_queue ETH_IN_QUEUE
84
	init_queue ETH_OUT_QUEUE
1159 hidnplayr 85
 
86
	ret
87
 
88
 
89
;---------------------------------------------------------
90
;
91
; ETH_Add_Device:
92
;
93
;  This function is called by ethernet drivers,
94
;  to register each running ethernet device to the kernel
95
;
96
;  IN:  Pointer to device structure in ebx
97
;  OUT: Device num in eax, -1 on error
98
;
99
;---------------------------------------------------------
100
 
101
align 4
1249 hidnplayr 102
ETH_add_device:
1159 hidnplayr 103
 
1185 hidnplayr 104
	DEBUGF	1,"ETH_Add_Device: %x ", ebx
1174 hidnplayr 105
 
106
	mov	eax, [ETH_RUNNING]
107
	cmp	eax, MAX_ETH_DEVICES
1159 hidnplayr 108
	jge	.error
109
 
1174 hidnplayr 110
	test	eax, eax
111
	jnz	.notfirst
112
	mov	dword [ETH_IN_QUEUE], eax
113
	mov	dword [ETH_OUT_QUEUE], eax
114
      .notfirst:
115
 
1159 hidnplayr 116
	mov	eax, ebx
117
	mov	ecx, MAX_ETH_DEVICES	  ; We need to check whole list because a device may be removed without re-organizing list
118
	mov	edi, ETH_DRV_LIST
119
 
120
	repne	scasd			  ; See if device is already in the list
121
	jz	.error
122
 
123
	xor	eax, eax
124
	mov	ecx, MAX_ETH_DEVICES
125
	mov	edi, ETH_DRV_LIST
126
 
127
	repne	scasd			  ; Find empty spot in the list
128
	jnz	.error
129
 
130
	sub	edi, 4
131
	mov	[edi], ebx		  ; add device to list
132
 
133
	sub	edi, ETH_DRV_LIST	  ; edi = 4*device num       Calculate device number in eax
134
	mov	eax, edi		  ; edx = 4*device num
135
	shr	eax, 2
136
 
137
	inc	[ETH_RUNNING]		  ; Indicate that one more ethernet device is up and running
1174 hidnplayr 138
 
1159 hidnplayr 139
	DEBUGF	1,"- succes: %u\n",eax
140
	ret
141
 
142
       .error:
143
	or	eax, -1
144
	DEBUGF	1,"- fail\n"
145
 
146
	ret
147
 
148
 
149
 
150
 
151
;--------------------------------
152
;
153
; ETH_Remove_Device:
154
;
155
;  This function is called by ethernet drivers,
156
;  to unregister ethernet devices from the kernel
157
;
158
;  IN:  Pointer to device structure in ebx
159
;  OUT: eax: -1 on error
160
;
161
;--------------------------------
162
 
163
align 4
1249 hidnplayr 164
ETH_remove_device:
1159 hidnplayr 165
 
166
	cmp	[ETH_RUNNING], 0
167
	je	.error
168
 
169
	mov	eax, ebx
170
	mov	ecx, MAX_ETH_DEVICES
171
	mov	edi, ETH_DRV_LIST
172
 
173
	repne	scasd
174
	jnz	.error
175
 
176
	xor	eax, eax
177
	mov	dword [edi-4], eax
178
 
179
	dec	[ETH_RUNNING]
1174 hidnplayr 180
	jnz	.notlast
1159 hidnplayr 181
 
1174 hidnplayr 182
	mov	dword [ETH_IN_QUEUE], ETH_QUEUE_SIZE
183
	mov	dword [ETH_OUT_QUEUE], ETH_QUEUE_SIZE
184
 
185
       .notlast:
186
 
1159 hidnplayr 187
	ret
188
 
189
       .error:
190
	or	eax, -1
191
 
192
	ret
193
 
194
 
195
 
196
;-------------------------------------------------------------
197
;
198
; ETH_Receiver:
199
;
200
;  This function is called by ethernet drivers,
201
;  It pushes the received ethernet packets onto the eth_in_queue
202
;
203
;  IN:  Pointer to buffer in [esp], size of buffer in [esp-4], pointer to eth_device in ebx
204
;  OUT: /
205
;
206
;-------------------------------------------------------------
207
 
208
align 4
1249 hidnplayr 209
ETH_receiver:
210
	DEBUGF	1,"ETH_Receiver: "
1159 hidnplayr 211
 
1249 hidnplayr 212
	push	ebx
213
	mov	esi, esp
214
	add_to_queue ETH_IN_QUEUE, ETH_QUEUE_SIZE, eth_queue_entry.size, .fail
215
	DEBUGF	1,"Queued packet successfully\n"
216
	add	esp, 4*3
217
	ret
1159 hidnplayr 218
 
1249 hidnplayr 219
  .fail:
220
	DEBUGF	1,"ETH_IN_QUEUE is full!\n"
221
	add	esp, 4
222
	call	kernel_free
223
	add	esp, 4
1159 hidnplayr 224
	ret
225
 
226
 
227
 
228
 
229
 
230
;-------------------------------------------------------------
231
;
232
; ETH_Handler:
233
;
234
;  Handles all queued eth packets (called from kernel's main_loop)
235
;
236
;  IN:  /
237
;  OUT: /
238
;
239
;-------------------------------------------------------------
240
 
241
align 4
1196 hidnplayr 242
ETH_handler:
1159 hidnplayr 243
 
1249 hidnplayr 244
	get_from_queue ETH_IN_QUEUE, ETH_QUEUE_SIZE, eth_queue_entry.size, .gohome
1159 hidnplayr 245
 
1249 hidnplayr 246
	push	ETH_handler
247
 
248
	lodsd
249
	mov	ebx, eax
250
	lodsd
251
	mov	ecx, eax
252
	lodsd
253
	xchg	eax, ecx
254
	push	ecx
255
	push	eax
256
 
1159 hidnplayr 257
	DEBUGF	1,"ETH_Handler - size: %u\n", ecx
258
	cmp	ecx, 60    ; check packet length
259
	jl	.dump
260
	sub	ecx, ETH_FRAME.Data
261
 
262
	lea	edx, [eax + ETH_FRAME.Data]
263
	mov	ax , [eax + ETH_FRAME.Type]
264
 
265
	cmp	ax, ETHER_IPv4
1196 hidnplayr 266
	je	IPv4_handler
1159 hidnplayr 267
 
268
	cmp	ax, ETHER_ARP
1196 hidnplayr 269
	je	ARP_handler
1159 hidnplayr 270
 
271
	DEBUGF	1,"Unknown ethernet packet type %x\n", ax
272
 
273
  .dump:
274
	DEBUGF	1,"Dumping packet\n"
275
	call	kernel_free
276
	add	esp, 4
277
 
278
  .gohome:
1249 hidnplayr 279
	ret				; return to get more from queue / to caller
1159 hidnplayr 280
 
281
 
282
 
283
;-----------------------------------------------------------------
284
;
1249 hidnplayr 285
; ETH_sender:
1159 hidnplayr 286
;
287
;  This function sends an ethernet packet to the correct driver.
288
;
289
;  IN:  Pointer to buffer in [esp]
290
;       size of buffer in [esp+4]
291
;       pointer to device struct in ebx
292
;  OUT: /
293
;
294
;-----------------------------------------------------------------
295
 
296
align 4
1249 hidnplayr 297
ETH_sender:
298
	DEBUGF	1,"ETH_Sender: queuing for device: %x, %u bytes\n", [esp], [esp + 4]
1159 hidnplayr 299
 
1249 hidnplayr 300
	push	ebx
301
	mov	esi, esp
302
	add_to_queue ETH_OUT_QUEUE, ETH_QUEUE_SIZE, eth_queue_entry.size, .fail
303
	DEBUGF	1,"Queued packet successfully\n"
304
	add	esp, 3*4
305
	ret
1159 hidnplayr 306
 
1249 hidnplayr 307
  .fail:
308
	DEBUGF	1,"ETH_OUT_QUEUE is full!\n"
309
	add	esp, 4
310
	call	kernel_free
311
	add	esp, 4
1159 hidnplayr 312
	ret
313
 
314
 
1249 hidnplayr 315
 
316
;-----------------------------------------------------------------
317
;
318
; ETH_send_queued:
319
;
320
;  IN:  /
321
;  OUT: /
322
;
323
;-----------------------------------------------------------------
324
 
1159 hidnplayr 325
align 4
326
ETH_send_queued:
327
 
1249 hidnplayr 328
	get_from_queue ETH_OUT_QUEUE, ETH_QUEUE_SIZE, eth_queue_entry.size, .gohome
1159 hidnplayr 329
 
1254 hidnplayr 330
	push	ETH_send_queued 		; this will cause the procedure to check for more packets
331
						; when a single packet is handled
1249 hidnplayr 332
 
1254 hidnplayr 333
	mov	ebx, [esi]
334
	pushd	[esi + 8]
335
	pushd	[esi + 4]
1249 hidnplayr 336
 
337
	DEBUGF	1,"dequeued packet for device %x\n", ebx
338
 
1254 hidnplayr 339
	call	[ebx+ETH_DEVICE.transmit]	; we will return to get_from_queue macro after transmitting packet
1159 hidnplayr 340
	call	kernel_free
341
	add	esp, 4 ; pop (balance stack)
1249 hidnplayr 342
 
1159 hidnplayr 343
     .gohome:
344
	ret
345
 
1249 hidnplayr 346
 
1159 hidnplayr 347
;---------------------------------------------------------------------------
348
;
349
; ETH_struc2dev
350
;
351
; IN: pointer to device struct in ebx
352
;
353
; OUT: edi is -1 on error, device number otherwise
354
;
355
;---------------------------------------------------------------------------
356
 
357
align 4
358
ETH_struc2dev:
359
	push	eax ecx
360
 
361
	mov	eax, ebx
362
	mov	ecx, MAX_ETH_DEVICES
363
	mov	edi, ETH_DRV_LIST
364
 
365
	repne	scasd
366
	jnz	.error
367
 
368
	sub	edi, ETH_DRV_LIST+4
369
	shr	edi, 2
370
 
371
	pop	ecx eax
372
	ret
373
  .error:
374
	or	edi, -1
375
	pop	ecx eax
376
 
377
	ret
378
 
379
 
380
;---------------------------------------------------------------------------
381
;
382
; ETH_create_Packet
383
;
384
; IN: pointer to source mac in eax
385
;     pointer to destination mac in ebx
386
;     packet size in ecx
387
;     device number in edx
388
;     protocol in di
389
;
1206 hidnplayr 390
; OUT: edi is -1 on error, pointer to buffer otherwise
1159 hidnplayr 391
;      eax points to buffer start
1206 hidnplayr 392
;      ebx is pointer to device structure
1159 hidnplayr 393
;      ecx is unchanged (packet size of embedded data)
1206 hidnplayr 394
;      edx is size of complete buffer
1159 hidnplayr 395
;      esi points to procedure wich needs to be called to send packet
396
;
397
;---------------------------------------------------------------------------
398
 
399
align 4
1249 hidnplayr 400
ETH_create_packet:
1159 hidnplayr 401
 
1206 hidnplayr 402
	DEBUGF 1,"Creating Ethernet Packet (size=%u): \n", ecx
1159 hidnplayr 403
 
1206 hidnplayr 404
	cmp	ecx, 1500
1159 hidnplayr 405
	jg	.exit
406
 
407
	push	ecx di eax ebx edx
408
 
409
	add	ecx, ETH_FRAME.Data
410
	push	ecx
411
	push	ecx
412
	call	kernel_alloc
413
	test	eax, eax
414
	jz	.pop_exit
415
 
416
	pop	ecx
417
	pop	edx
418
 
419
	mov	edi, eax
420
	pop	esi
421
	movsd
422
	movsw
423
	pop	esi
424
	movsd
425
	movsw
426
	pop	ax
427
	stosw
428
 
429
	lea	eax, [edi - ETH_FRAME.Data]  ; Set eax to buffer start
1206 hidnplayr 430
	mov	edx, ecx		     ; Set ebx to complete buffer size
1159 hidnplayr 431
	pop	ecx
1249 hidnplayr 432
	mov	esi, ETH_sender
1159 hidnplayr 433
 
1206 hidnplayr 434
	xor	ebx, ebx			;;;; TODO: Fixme
435
	mov	ebx, [ETH_DRV_LIST + ebx]
1159 hidnplayr 436
 
1206 hidnplayr 437
	cmp	edx, 46 + ETH_FRAME.Data    ; If data size is less then 46, add padding bytes
438
	jg	.continue
439
	mov	edx, 46 + ETH_FRAME.Data
440
       .continue:
441
 
442
	DEBUGF 1,"done: %x size:%u device:%x\n", eax, edx, ebx
1159 hidnplayr 443
	ret
444
 
445
  .pop_exit:
1206 hidnplayr 446
	DEBUGF 1,"Out of ram space!!\n"
1159 hidnplayr 447
	add	esp, 18
1206 hidnplayr 448
	or	edi,-1
449
	ret
450
 
1159 hidnplayr 451
  .exit:
1206 hidnplayr 452
	DEBUGF 1,"Packet too large!\n"
1159 hidnplayr 453
	or	edi, -1
454
	ret
455
 
456
 
457
 
458
;---------------------------------------------------------------------------
459
;
460
; ETH_API
461
;
462
; This function is called by system function 75
463
;
464
; IN:  subfunction number in bl
465
;      device number in bh
466
;      ecx, edx, .. depends on subfunction
467
;
468
; OUT:
469
;
470
;---------------------------------------------------------------------------
471
 
472
align 4
473
ETH_API:
474
 
475
	movzx	eax, bh
476
	shl	eax, 2
477
 
478
	test	bl, bl
479
	jz	.packets_tx	; 0
480
	dec	bl
481
	jz	.packets_rx	; 1
482
	dec	bl
483
	jz	.bytes_tx	; 2
484
	dec	bl
485
	jz	.bytes_rx	; 3
486
	dec	bl
487
	jz	.read_mac	; 4
488
	dec	bl
489
	jz	.write_mac	; 5
490
	dec	bl
491
	jz	.in_queue	; 6
492
	dec	bl
493
	jz	.out_queue	; 7
494
 
495
.error:
496
	mov	eax, -1
497
	ret
498
 
499
.packets_tx:
500
	add	eax, ETH_DRV_LIST
1171 hidnplayr 501
	mov	eax, dword [eax]
502
	mov	eax, dword [eax + ETH_DEVICE.packets_tx]
503
 
1159 hidnplayr 504
	ret
505
 
506
.packets_rx:
507
	add	eax, ETH_DRV_LIST
1171 hidnplayr 508
	mov	eax, dword [eax]
509
	mov	eax, dword [eax + ETH_DEVICE.packets_rx]
1159 hidnplayr 510
	ret
511
 
512
.bytes_tx:
513
	add	eax, ETH_DRV_LIST
1171 hidnplayr 514
	mov	eax, dword [eax]
1241 clevermous 515
	mov	ebx, dword [eax + ETH_DEVICE.bytes_tx + 4]
1174 hidnplayr 516
	mov	eax, dword [eax + ETH_DEVICE.bytes_tx]
517
	mov	[esp+20+4], ebx 			; TODO: fix this ugly code
1159 hidnplayr 518
	ret
519
 
520
.bytes_rx:
521
	add	eax, ETH_DRV_LIST
1171 hidnplayr 522
	mov	eax, dword [eax]
1174 hidnplayr 523
	mov	ebx, dword [eax + ETH_DEVICE.bytes_rx + 4]
524
	mov	eax, dword [eax + ETH_DEVICE.bytes_rx]
525
	mov	[esp+20+4], ebx 			; TODO: fix this ugly code
1159 hidnplayr 526
	ret
527
 
1174 hidnplayr 528
 
1159 hidnplayr 529
.read_mac:
530
	add	eax, ETH_DRV_LIST
531
	mov	eax, [eax]
532
;        push    eax
533
;        call    dword [eax + ETH_DEVICE.get_MAC]
534
;        pop     eax
535
	movzx	ebx, word [eax + ETH_DEVICE.mac]
536
	mov	eax, dword [eax + ETH_DEVICE.mac + 2]
1171 hidnplayr 537
	mov	[esp+20+4], ebx 			; TODO: fix this ugly code
1159 hidnplayr 538
	ret
539
 
540
.write_mac:
541
	push	ecx
542
	push	dx
543
	add	eax, ETH_DRV_LIST
544
	mov	eax, [eax]
545
	mov	eax, dword [eax + ETH_DEVICE.set_MAC]
546
	call	eax
547
	ret
548
 
549
.in_queue:
550
	add	eax, ETH_IN_QUEUE
551
	mov	eax, [eax + queue.size]
552
	ret
553
 
554
.out_queue:
555
	add	eax, ETH_OUT_QUEUE
556
	mov	eax, [eax + queue.size]
557
	ret