[BITS 32]
%include "network\i825xx.inc"
[SECTION .bss align=16]
I825XX_BaseIO:		resd	1
I825XX_MAC:		resb	6

I825XX_rx_buffer:	resb	32*16
I825XX_tx_buffer	resb	32*16


I825XX_dbg_buffer	resb	2000		;test buffer, will be removed !

I825XX_packet_buffer:	resb	2048		;2048 bytes long packet buffer

I825XX_link_status	resb	1

[SECTION .data]
txt_i825xx_found:	db	'i825xx: found at PCI $'
txt_mac_address:	db	' MAC address: $'
txt_length:		db	'Packet length: $'
txt_dbg_poll:		db	'i825xx: polling for a packet',10,13,'$'
txt_dbg_poll2:		db	'i825xx: polling for a packet (2nd way)',10,13,'$'
txt_dbg_link_change:	db	'i825xx: link status changed to $'
txt_dbg_irq:		db	'i825xx: irq event',10,13,'$'
txt_dbg_link_down:	db	'down',10,13,'$'
txt_dbg_link_up:	db	'up',10,13,'$'
txt_dbg:		db	'read mac$'

test_packet:		
			db	0x00,0xe0,0x28,0x62,0x05,0x8b	;hydra MAC
			db	0xde,0xad,0xbe,0xef,0xde,0xad	;src MAC
			db	0x08,0x00			;type 0x800			
			db	0x45
			db	0x00	;diff. service field
			db	0x00,0x3c			;total length
			db	0x7a,0xbd			;identification
			db	0x00				;flags
			db	0x00				;fragment offset
			db	0x80				;TTL
			db	0x01				;proto=ICMP
			db	0x3c,0x2f			;checksum
			db	0xc0,0xa8,0x01,0x20		;SRC 192.168.1.32
			db	0xc0,0xa8,0x01,0x64		;DST 192.168.1.100
			db	0x08				;echo request
			db	0x00				;code
			db	0x4d,0x58			;checksum
			db	0x00,0x01			;identifier BE
			db	0x00,0x03			;seq.num BE
			db	'abcdefghijklmnopqrstuvwabcdefghi'

			
[SECTION .text]
;============================================================================
;__I825XX_Init
;action: I825XX NICs initialization (searching for cards, reset, registering)
;============================================================================
__I825XX_Init:
	pushad

	mov	ecx,32*16
	mov	edi,I825XX_rx_buffer
	mov	al,0
.zeroRX:mov	byte[gs:edi],al
	inc	edi
	loop	.zeroRX

	mov	ecx,32*16
	mov	edi,I825XX_tx_buffer
	mov	al,0
.zeroTX:mov	byte[gs:edi],al
	inc	edi
	loop	.zeroTX
	
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	
	mov	bl,DEVTYPE_PCI_DEVICE
	mov	bh,PCI_TYPE_NETWORK_CONTROLLER
	mov	dl,1
	mov	dh,1	
	call	__DEVMAN_get_device	
	jnc	.found_dev
	pop	ds
	popad
	stc				;no matching device found, exit !
	ret
.found_dev:
	call	__DEVMAN_get_DevStruc_pointer

	mov	edi,I825XX_ident_list
.look_for_dev:
	mov	ax,word[gs:edi]
	cmp	ax,0xffff
	jz	.search_next			;device not found in list
	cmp	word[ds:esi+DevStruc.pci_vendor],ax
	jnz	.search_next			;another vendor found
	mov	ax,word[gs:edi+2]
	cmp	word[ds:esi+DevStruc.pci_device],ax
	jz	.dev_ok
	add	edi,4
	jmp	short .look_for_dev
	
.dev_ok:
	push	ds	
	mov	ax,SelFlatData
	mov	ds,ax
	mov	edx,txt_i825xx_found
	call	__CRT_print
	pop	ds

	push	ecx
	
	movzx	ecx,byte[ds:esi+DevStruc.pci_bus_no]
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	call	__CRT_print_dec
	mov	al,':'
	mov	bl,7
	call	__CRT_printchr
	pop	ds

	movzx	ecx,byte[ds:esi+DevStruc.pci_dev_no]
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	call	__CRT_print_dec
	mov	al,':'
	mov	bl,7
	call	__CRT_printchr
	pop	ds

	movzx	ecx,byte[ds:esi+DevStruc.pci_func_no]
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	call	__CRT_print_dec
	push	ebx
	mov	al,','
	mov	bl,7
	call	__CRT_printchr
	pop	ebx
	pop	ds	

	pop	ecx

	mov	eax,dword[ds:esi+DevStruc.pci_bar0]
	test	eax,1
	jz	.is_mmio
	
	jmp	.search_next
.is_mmio:
	and	eax,0xfffffff0
	mov	dword[gs:I825XX_BaseIO],eax
	

	and	eax,110b
	cmp	eax,010b			;located under 1MB phys. mem ?
	jz	.search_next
	
	push	ebx
	push	ecx
	push	edx
	movzx	bx,byte[ds:esi+DevStruc.pci_bus_no]
	movzx	cx,byte[ds:esi+DevStruc.pci_dev_no]
	movzx	dx,byte[ds:esi+DevStruc.pci_func_no]
	call	__PCI_adjust_pci_device		;adjust PCI device settings
	pop	edx
	pop	ecx
	pop	ebx

	mov	eax,dword[ds:esi+DevStruc.pci_bar1]
	test	eax,1
	jnz	.io_found			;if bit0 = 1 
	mov	eax,dword[ds:esi+DevStruc.pci_bar2]
	test	eax,1
	jnz	.io_found
	mov	eax,dword[ds:esi+DevStruc.pci_bar3]
	test	eax,1
	jnz	.io_found
	mov	eax,dword[ds:esi+DevStruc.pci_bar4]
	test	eax,1
	jnz	.io_found
	mov	eax,dword[ds:esi+DevStruc.pci_bar5]
	test	eax,1
	jnz	.io_found
	jmp	.search_next
	
.io_found:
	push	eax
	push	ebx
	push	ecx
	mov	al,byte[ds:esi+DevStruc.pci_interrupt]
	add	al,32
	mov	ah,11101110b
	mov	bx,cs
	mov	ecx,__I825XX_IRQ_handler
	call	__Kernel_Set_Interrupt_Handler
	pop	ecx
	pop	ebx
	pop	eax

	call	__I825XX_read_MAC		;read the MAC address
	call	__I825XX_reset			;reset the NIC
	
	call	test_send	
	
.search_next:
	mov	bl,DEVTYPE_PCI_DEVICE
	mov	bh,PCI_TYPE_NETWORK_CONTROLLER
	mov	dl,1
	mov	dh,1	
	call	__DEVMAN_get_next_device
	jnc	.found_dev
	
	pop	ds
	popad
	clc
	ret
	
;============================================================================
;__I825XX_read_MAC
;action: reads MAC address from registers or EEPROM
;============================================================================
__I825XX_read_MAC:
	pushad
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	
	mov	edi,dword[gs:I825XX_BaseIO]

	mov	eax,dword[gs:edi+I825XX_RAL]	;RAL
	cmp	eax,0
	jz	.get_from_eeprom
	mov	byte[gs:I825XX_MAC],al
	mov	byte[gs:I825XX_MAC+1],ah
	shr	eax,16
	mov	byte[gs:I825XX_MAC+2],al
	mov	byte[gs:I825XX_MAC+3],ah	
	
	mov	eax,dword[gs:edi+I825XX_RAH]	;RAH
	mov	byte[gs:I825XX_MAC+4],al
	mov	byte[gs:I825XX_MAC+5],ah

	call	__I825XX_print_MAC

		
	pop	ds
	popad
	ret
.get_from_eeprom:
	mov	edi,dword[gs:I825XX_BaseIO]
	mov	eax,1
	mov	dword[gs:edi+I825XX_EERD],eax		;EEPROM read
	mov	eax,dword[gs:edi+I825XX_EERD]
	shr	eax,16
	mov	byte[gs:I825XX_MAC],al
	shr	eax,8
	mov	byte[gs:I825XX_MAC+1],al
	mov	eax,0x00000101
	mov	dword[gs:edi+I825XX_EERD],eax		;EEPROM read
	mov	eax,dword[gs:edi+I825XX_EERD]
	shr	eax,16
	mov	byte[gs:I825XX_MAC+2],al
	shr	eax,8
	mov	byte[gs:I825XX_MAC+3],al
	mov	eax,0x00000201
	mov	dword[gs:edi+I825XX_EERD],eax
	mov	eax,dword[gs:edi+I825XX_EERD]
	shr	eax,16
	mov	byte[gs:I825XX_MAC+4],al
	shr	eax,8
	mov	byte[gs:I825XX_MAC+5],al

	mov	edx,txt_mac_address
	call	__CRT_print
	
	call	__I825XX_print_MAC

	pop	ds	
	popad
	ret

__I825XX_print_MAC:
	pushad
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	mov	ecx,6
	mov	esi,I825XX_MAC
.print_mac:
	movzx	edx,byte[gs:esi]
	call	__CRT_print_hex
	cmp	ecx,1
	jz	.skip_dcolon
	mov	al,':'
	mov	bl,7
	call	__CRT_printchr	
.skip_dcolon:
	inc	esi
	loop	.print_mac
	
	mov	edx,crlf
	call	__CRT_print
	pop	ds
	popad
	ret
	
;============================================================================
;__I825XX_reset
;resets the NIC controller
;============================================================================
__I825XX_reset:
	cli
	pushad
	
	mov	esi,dword[gs:I825XX_BaseIO]
	mov	eax,0xffffffff

;	mov	dword[gs:esi+I825XX_IMC],eax	;disable interrupts
;	mov	eax,dword[gs:esi+I825XX_ICR]	;clear pending interrupts
;	xor	eax,eax
;	mov	dword[gs:esi+I825XX_ITR],eax	;disable int throttling logic
	
	
	mov	eax,0x08008060
	mov	dword[gs:esi+I825XX_TXCW],eax	;set ANE (auto negotiation enable)
						;,TxConfigWord:
						; - Half/Full duplex
						; - Next Page Request
	
	mov	eax,dword[gs:esi+I825XX_CTRL]
	and	eax,~(CTRL.LRST|CTRL.PHY_RST|CTRL.VME|CTRL.ILOS)
	or	eax,CTRL.SLU|CTRL.ASDE
	mov	dword[gs:esi+I825XX_CTRL],eax	;clear LRST (link reset)
						;set SLU (set link up)
						;set ASDE (auto speed detection)
						;clear RSTPHY  (PHY reset = no reset)
						;clear VME (VLAN Mode Enable - disabled)
						;clear ILOS (do not invert loss of signal)

	xor	eax,eax				;zero the flowcontrol registers
	mov	dword[gs:esi+I825XX_FCAH],eax
	mov	dword[gs:esi+I825XX_FCAL],eax
	mov	dword[gs:esi+I825XX_FCT],eax
	mov	dword[gs:esi+I825XX_FCTTV],eax
	

	push	esi
	mov	ecx,8
.clear_RAL:
	mov	dword[gs:esi+I825XX_RAL],0
	add	esi,8
	loop	.clear_RAL
	pop	esi

	push	esi
	mov	ecx,8
.clear_RAH:
	mov	dword[gs:esi+I825XX_RAH],0
	add	esi,8
	loop	.clear_RAH
	pop	esi
	

	mov	al,byte[gs:I825XX_MAC+2]
	mov	ah,byte[gs:I825XX_MAC+3]	
	shl	eax,16
	mov	al,byte[gs:I825XX_MAC]
	mov	ah,byte[gs:I825XX_MAC+1]
	mov	dword[gs:esi+I825XX_RAL],eax	;RAL

	xor	eax,eax
	mov	al,byte[gs:I825XX_MAC+4]
	mov	ah,byte[gs:I825XX_MAC+5]
	or	eax,0x80000000			;set AV (address valid)
						;AS (address select) already 00b - destination addr
	mov	dword[gs:esi+I825XX_RAH],eax	;RAH

	push	esi				;initialize MTA table with 0's
	mov	ecx,128
.init_MTA:
	mov	dword[gs:esi+I825XX_MTA],0
	add	esi,4
	loop	.init_MTA
	pop	esi

	
	mov	eax,IMS.RXT0|IMS.RXO|IMS.RXDMT0|IMS.RXSEQ|IMS.LSC
	mov	dword[gs:esi+I825XX_IMS],eax	;mask the interrupts

	mov	eax,ICS.RXT0|ICS.RXO|ICS.RXDMT0|ICS.RXSEQ|ICS.LSC
	mov	dword[gs:esi+I825XX_ICS],eax	;mask the interrupts


	mov	eax,I825XX_rx_buffer		;receive buffer address
	mov	dword[gs:esi+I825XX_RDBAL],eax
	mov	eax,0				;32bit addressing only
	mov	dword[gs:esi+I825XX_RDBAH],eax
	mov	eax,32*16			;length of the buffer (512 = 32*16)
	mov	dword[gs:esi+I825XX_RDLEN],eax

	xor	eax,eax				;(first valid descriptor nr.)
	mov	dword[gs:esi+I825XX_RDH],eax	;receive descriptor head	
	mov	eax,1				;(id +1 of last valid descriptor)
	mov	dword[gs:esi+I825XX_RDT],eax	;receive descriptor tail


	mov	eax,RCTL.EN|RCTL.BAM|RCTL.BSIZE_2K|RCTL.SECRC
	mov	dword[gs:esi+I825XX_RCTL],eax

	mov	eax,0x30
	mov	dword[gs:esi+I825XX_PBA],eax	;set RX buffer to 48KB
	
;	mov	eax,0x04008002
;	mov	eax,0x0400800A
;	mov	dword[gs:esi+I825XX_RCTL],eax	;set EN (receiver enable)
						;set BAM (broadcast accept mode)
						;set SECRC (strip ethernet CRC field)
						
	mov	eax,0				
	mov	edi,I825XX_rx_buffer
	mov	dword[gs:edi+4],eax
	mov	eax,I825XX_packet_buffer
	mov	dword[gs:edi],eax		;set Buffer Address

;transmit setup 
	
	mov	eax,I825XX_tx_buffer
	mov	dword[gs:esi+I825XX_TDBAL],eax	;transmit descriptor base address low
	mov	eax,0
	mov	dword[gs:esi+I825XX_TDBAH],eax	;transmit descriptor base address high
	mov	eax,32*16			;512 bytes long
	mov	dword[gs:esi+I825XX_TDLEN],eax	;transmit descriptor length

	xor	eax,eax
	mov	dword[gs:esi+I825XX_TDH],eax	;transmit descriptor head
	mov	dword[gs:esi+I825XX_TDT],eax	;transmit descriptor tail

;	mov	eax,0x010400fa			;enabled,pad short packets,15 retrys, 64-byte COLD, re-transmit on late collision
	
	mov	eax,TCTL.EN|TCTL.PSP
	or	eax,0x10<<TCTL.CT
	or	eax,0x40<<TCTL.COLD
	mov	dword[gs:esi+I825XX_TCTL],eax	;transmit control register

;	mov	eax,0x0060200a			;IPGT 10, IPGR1 8, IPGR2 6
	mov	eax,10<<TIPG.IPGT
	or	eax,10<<TIPG.IPGR1
	or	eax,10<<TIPG.IPGR2
	mov	dword[gs:esi+I825XX_TIPG],eax	;transmit IPG register
	
	xor	eax,eax
	mov	dword[gs:esi+I825XX_RDTR],eax	;clear receive delay timer register
	mov	dword[gs:esi+I825XX_RADV],eax	;clear receive interrupt absolute delay timer
	mov	dword[gs:esi+I825XX_RSRPD],eax	;clear receive small packet detect interrupt
	
	mov	al,17
	call	__I825XX_read_PHY
	shr	eax,10
	and	eax,1
	mov	byte[gs:I825XX_link_status],al

	popad	
	sti
	ret	
	
;============================================================================
;__I825XX_IRQ_handler
;interrupt handler for Intel Pro/1000 cards
;============================================================================
__I825XX_IRQ_handler:
	cli					;good idea ?
	pushad
	push	gs
	mov	ax,SelFlatData
	mov	gs,ax
	
	push	ds
	mov	ds,ax
	mov	edx,txt_dbg_irq
	call	__CRT_print
	mov	ds,ax
	pop	ds

	mov	edi,dword[gs:I825XX_BaseIO]
	mov	eax,dword[gs:edi+I825XX_ICR]	;acknowledge the interrupt to NIC

	and	eax,0xfffffffc			;throw away lowest 2 bits
	

	test	eax,0x00000004			;ICR_LSC (Link Status Change)
	jz	.no_link_change

	and	eax,0xfffffffb
	mov	dword[gs:edi+I825XX_ICR],eax
	
	mov	eax,dword[gs:edi+I825XX_CTRL]
	or	eax,0x40			;CTRL_SLU (set link up)
	mov	dword[gs:edi+I825XX_CTRL],eax

	mov	al,17				;PHY specific status register
	call	__I825XX_read_PHY
	and	eax,0x400			;check realtime link status
	jz	.link_down
	mov	edx,txt_dbg_link_up
	cmp	byte[gs:I825XX_link_status],1
	jz	.irq_handler_end
	mov	byte[gs:I825XX_link_status],1
	jmp	.print_link_status
.link_down:
	cmp	byte[gs:I825XX_link_status],0
	jz	.irq_handler_end		;change from down to down :)
	mov	byte[gs:I825XX_link_status],0
	mov	edx,txt_dbg_link_down
.print_link_status:
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	push	edx
	mov	edx,txt_dbg_link_change
	call	__CRT_print
	pop	edx
	call	__CRT_print	

	pop	ds
	jmp	.irq_handler_end
	
.no_link_change:


	test	eax,0x00000080			;is packet pending ?
	jnz	.no_poll

	and	eax,0x0080			;polling packet...
	mov	dword[gs:edi+I825XX_ICR],eax	

	push	edi
	mov	edi,I825XX_dbg_buffer
	call	__I825XX_poll_packet
	pop	edi	

	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	mov	edx,txt_dbg_poll
	call	__CRT_print
	pop	ds
	jmp	.irq_handler_end	
	
.no_poll:	
	test	eax,00000000000000000000000001010000b	;RX underrun/min treshold
	jz	.no_underrun_treshold
	and	eax,11111111111111111111111110101111b
	mov	dword[gs:edi+I825XX_ICR],eax
	
	push	edi
	mov	edi,I825XX_dbg_buffer
	call	__I825XX_poll_packet
	pop	edi	

	push	ds
	mov	ax,SelFlatData
	mov	ds,ax
	mov	edx,txt_dbg_poll2
	call	__CRT_print
	pop	ds
	jmp	.irq_handler_end		
	
.no_underrun_treshold:

.irq_handler_end:	
	mov	eax,dword[gs:edi+I825XX_ICR]
	mov	al,0x20
	out	0x20,al
	out	0xa0,al
	pop	gs
	popad
	sti
	iretd

;============================================================================
;__I825XX_transmit_packet
;input	ESI - buffer with the packet to be transmitted
;	ECX - length
;============================================================================
__I825XX_transmit_packet:
	pushad
	
	mov	edi,I825XX_tx_buffer
	mov	eax,esi
	mov	dword[gs:edi],eax
	mov	eax,0
	mov	dword[gs:edi+4],eax
	mov	eax,ecx
	bts	eax,24				;EOP
	bts	eax,25				;IFCS
	bts	eax,27				;RS
	mov	dword[gs:edi+8],eax
	xor	eax,eax
	mov	dword[gs:edi+12],eax
	mov	edi,dword[gs:I825XX_BaseIO]
	mov	dword[gs:edi+I825XX_TDH],eax
	mov	eax,1
	mov	dword[gs:edi+I825XX_TDT],eax
	popad
	ret

;============================================================================
;__I825XX_poll_packet
;input:	EDI - buffer where the packet will be stored
;============================================================================
__I825XX_poll_packet:
	pushad
	
	xor	ecx,ecx
	movzx	ecx,word[gs:I825XX_rx_buffer+8]
	mov	esi,I825XX_packet_buffer
	push	ecx
.move_data:
	mov	al,byte[gs:esi]
	mov	byte[gs:edi],al
	loop	.move_data
	pop	ecx
	mov	esi,dword[gs:I825XX_BaseIO]
	xor	eax,eax
	mov	dword[gs:esi+I825XX_RDH],eax
	mov	eax,1
	mov	dword[gs:esi+I825XX_RDT],eax

	mov	eax,32*16			;512 bytes long
	mov	dword[gs:esi+I825XX_TDLEN],eax	;transmit descriptor length
	mov	eax,RCTL.EN|RCTL.BAM|RCTL.BSIZE_2K|RCTL.SECRC
	mov	dword[gs:esi+I825XX_RCTL],eax
	
	mov	eax,I825XX_packet_buffer
	mov	dword[gs:I825XX_rx_buffer],eax
	mov	eax,0
	mov	dword[gs:I825XX_rx_buffer+4],eax
	
	push	ds
	mov	ax,SelFlatData
	mov	ds,ax

	mov	edx,txt_length
	call	__CRT_print
	
	call	__CRT_print_dec
	mov	edx,crlf
	call	__CRT_print

	mov	ecx,6
	mov	esi,I825XX_packet_buffer+6
.print_MAC1:
	movzx	edx,byte[gs:esi]
	call	__CRT_print_hex
	cmp	ecx,1
	jz	.no_dcol
	mov	al,':'
	mov	bl,7
	call	__CRT_printchr
.no_dcol:
	inc	esi
	loop	.print_MAC1

	mov	al,'>'
	mov	bl,7
	call	__CRT_printchr

	
	mov	ecx,6
	mov	esi,I825XX_packet_buffer
.print_MAC2:
	movzx	edx,byte[gs:esi]
	call	__CRT_print_hex
	cmp	ecx,1
	jz	.nodcl2
	mov	al,':'
	mov	bl,7
	call	__CRT_printchr
.nodcl2:inc	esi
	loop	.print_MAC2
	
;	mov	edx,dword[gs:I825XX_packet_buffer]
;	call	__CRT_print_hex
;	mov	edx,dword[gs:I825XX_packet_buffer+4]
;	call	__CRT_print_hex
	mov	edx,crlf
	call	__CRT_print
	pop	ds
	
	popad
	ret	

;============================================================================
;__I825XX_read_PHY
;input:	AL - PHY register
;output:CY=0 -> AX - value read from the register
;	CY=1 -> error
;============================================================================
__I825XX_read_PHY:
	;bits 20:16 - PHY register address (OR with this value below)
	push	esi
	
	movzx	eax,al
	shl	eax,16
	or	eax,00001000001000000000000000000000b	
	mov	esi,dword[gs:I825XX_BaseIO]
	mov	dword[gs:esi+I825XX_MDIC],eax

.wait_result:
;	mov	eax,1
;	call	__8253_wait_generic	
	
	mov	eax,dword[gs:esi+I825XX_MDIC]	
	and	eax,01010000000000000000000000000000b	;check Ready or Error
	jz	.wait_result
	
	cmp	eax,01000000000000000000000000000000b
	jnz	.no_error
	pop	esi
	stc
	ret
.no_error:
	mov	eax,dword[gs:esi+I825XX_MDIC]		
	pop	esi
	clc
	ret
	
	
	
test_send:
	pushad
;	mov	eax,10
;	call	__8253_wait_generic

	mov	esi,test_packet
	mov	ecx,74
	call	__I825XX_transmit_packet
	
	popad
	ret