Kernel - Slight reworks to timer code
[tpg/acess2.git] / Modules / Network / RTL8139 / rtl8139.c
index af4ace7..1e9ac89 100644 (file)
@@ -8,7 +8,7 @@
 #include <modules.h>
 #include <fs_devfs.h>
 #include <drv_pci.h>
-#include <tpl_drv_network.h>
+#include <api_drv_network.h>
 #include <semaphore.h>
 
 // === CONSTANTS ===
@@ -95,21 +95,29 @@ char        *RTL8139_ReadDir(tVFS_Node *Node, int Pos);
 tVFS_Node      *RTL8139_FindDir(tVFS_Node *Node, const char *Filename);
  int   RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
 Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
-Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
+Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer);
  int   RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Arg);
-void   RTL8139_IRQHandler(int Num);
+void   RTL8139_IRQHandler(int Num, void *Ptr);
 
 // === GLOBALS ===
 MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
+tVFS_NodeType  gRTL8139_RootNodeType = {
+       .ReadDir = RTL8139_ReadDir,
+       .FindDir = RTL8139_FindDir,
+       .IOCtl = RTL8139_IOCtl
+       };
+tVFS_NodeType  gRTL8139_DevNodeType = {
+       .Write = RTL8139_Write,
+       .Read = RTL8139_Read,
+       .IOCtl = RTL8139_IOCtl  
+       };
 tDevFS_Driver  gRTL8139_DriverInfo = {
        NULL, "RTL8139",
        {
        .NumACLs = 1,
        .ACLs = &gVFS_ACL_EveryoneRX,
        .Flags = VFS_FFLAG_DIRECTORY,
-       .ReadDir = RTL8139_ReadDir,
-       .FindDir = RTL8139_FindDir,
-       .IOCtl = RTL8139_RootIOCtl
+       .Type = &gRTL8139_RootNodeType
        }
 };
  int   giRTL8139_CardCount;
@@ -127,19 +135,19 @@ int RTL8139_Install(char **Options)
        tCard   *card;
        
        giRTL8139_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
-       Log_Debug("RTL8139", "%i cards", giRTL8139_CardCount);
        
        if( giRTL8139_CardCount == 0 )  return MODULE_ERR_NOTNEEDED;
-       
+
+       Log_Debug("RTL8139", "%i cards", giRTL8139_CardCount);  
        gaRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
        
-       //while( (id = PCI_GetDevice(0x10EC, 0x8139, 0, id)) != -1 )
-       while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
+       for( i = 0 ; (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1; i ++ )
        {
                card = &gaRTL8139_Cards[i];
                base = PCI_GetBAR( id, 0 );
                if( !(base & 1) ) {
-                       Log_Warning("RTL8139", "Driver does not support MMIO, skipping card");
+                       Log_Warning("RTL8139", "Driver does not support MMIO, skipping card (addr %x)",
+                               base);
                        card->IOBase = 0;
                        card->IRQ = 0;
                        continue ;
@@ -149,7 +157,7 @@ int RTL8139_Install(char **Options)
                card->IRQ = PCI_GetIRQ( id );
                
                // Install IRQ Handler
-               IRQ_AddHandler(card->IRQ, RTL8139_IRQHandler);
+               IRQ_AddHandler(card->IRQ, RTL8139_IRQHandler, card);
                
                // Power on
                outb( base + CONFIG1, 0x00 );
@@ -161,7 +169,7 @@ int RTL8139_Install(char **Options)
                // Set up recieve buffer
                // - Allocate 3 pages below 4GiB for the recieve buffer (Allows 8k+16+1500)
                card->ReceiveBuffer = (void*)MM_AllocDMA( 3, 32, &card->PhysReceiveBuffer );
-               card->ReceiveBufferLength = 8*1024+16;
+               card->ReceiveBufferLength = 8*1024;
                outd(base + RBSTART, (Uint32)card->PhysReceiveBuffer);
                outd(base + CBA, 0);
                outd(base + CAPR, 0);
@@ -184,10 +192,10 @@ int RTL8139_Install(char **Options)
                outd(base + TSAD3, card->PhysTransmitBuffers[3]);
                
                // Set recieve buffer size and recieve mask
-               // - Bit 7 being unset tells the card to overflow the recieve buffer if needed
+               // - Bit 7 being set tells the card to overflow the recieve buffer if needed
                //   (i.e. when the packet starts at the end of the bufffer, it overflows up
                //    to 1500 bytes)
-               outd(base + RCR, 0x0F);
+               outd(base + RCR, 0x8F);
        
                // Recive Enable and Transmit Enable    
                outb(base + CMD, 0x0C);
@@ -206,17 +214,13 @@ int RTL8139_Install(char **Options)
                card->Node.ImplPtr = card;
                card->Node.NumACLs = 0;
                card->Node.CTime = now();
-               card->Node.Write = RTL8139_Write;
-               card->Node.Read = RTL8139_Read;
-               card->Node.IOCtl = RTL8139_IOCtl;
+               card->Node.Type = &gRTL8139_DevNodeType;
                
                Log_Log("RTL8139", "Card %i 0x%04x, IRQ %i %02x:%02x:%02x:%02x:%02x:%02x",
                        i, card->IOBase, card->IRQ,
                        card->MacAddr[0], card->MacAddr[1], card->MacAddr[2],
                        card->MacAddr[3], card->MacAddr[4], card->MacAddr[5]
                        );
-               
-               i ++;
        }
        
        gRTL8139_DriverInfo.RootNode.Size = giRTL8139_CardCount;
@@ -270,7 +274,9 @@ retry:
        
        Mutex_Acquire( &card->ReadMutex );
        
-       read_ofs = (inw( card->IOBase + CAPR ) + 0x10) & 0xFFFF;
+       read_ofs = inw( card->IOBase + CAPR );
+       LOG("raw read_ofs = %i", read_ofs);
+       read_ofs = (read_ofs + 0x10) & 0xFFFF;
        LOG("read_ofs = %i", read_ofs);
        
        pkt_length = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
@@ -278,13 +284,17 @@ retry:
        // Calculate new read offset
        new_read_ofs = read_ofs + pkt_length + 4;
        new_read_ofs = (new_read_ofs + 3) & ~3; // Align
-       if(new_read_ofs > card->ReceiveBufferLength)    new_read_ofs = 0;
+       if(new_read_ofs > card->ReceiveBufferLength) {
+               LOG("wrapping read_ofs");
+               new_read_ofs -= card->ReceiveBufferLength;
+       }
        new_read_ofs -= 0x10;   // I dunno
+       LOG("new_read_ofs = %i", new_read_ofs);
        
        // Check for errors
        if( *(Uint16*)&card->ReceiveBuffer[read_ofs] & 0x1E ) {
                // Update CAPR
-               outd(card->IOBase + CAPR, new_read_ofs);
+               outw(card->IOBase + CAPR, new_read_ofs);
                Mutex_Release( &card->ReadMutex );
                goto retry;     // I feel evil
        }
@@ -303,7 +313,7 @@ retry:
        return Length;
 }
 
-Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
+Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, const void *Buffer)
 {
         int    td;
        Uint32  status;
@@ -366,85 +376,90 @@ int RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Data)
        return 0;
 }
 
-void RTL8139_IRQHandler(int Num)
+void RTL8139_IRQHandler(int Num, void *Ptr)
 {
-        int    i, j;
-       tCard   *card;
+        int    j;
+       tCard   *card = Ptr;
        Uint16  status;
 
        LOG("Num = %i", Num);
        
-       for( i = 0; i < giRTL8139_CardCount; i ++ )
-       {
-               card = &gaRTL8139_Cards[i];
-               if( Num != card->IRQ )  break;
+       if( Num != card->IRQ )  return;
                
-               status = inw(card->IOBase + ISR);
-               LOG("status = 0x%02x", status);
+       status = inw(card->IOBase + ISR);
+       LOG("status = 0x%02x", status);
                
-               // Transmit OK, a transmit descriptor is now free
-               if( status & FLAG_ISR_TOK )
+       // Transmit OK, a transmit descriptor is now free
+       if( status & FLAG_ISR_TOK )
+       {
+               for( j = 0; j < 4; j ++ )
                {
-                       for( j = 0; j < 4; j ++ )
-                       {
-                               if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
-                                       Mutex_Release( &card->TransmitInUse[j] );
-                                       // TODO: Update semaphore once implemented
-                               }
+                       if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
+                               Mutex_Release( &card->TransmitInUse[j] );
+                               // TODO: Update semaphore once implemented
                        }
-                       outw(card->IOBase + ISR, FLAG_ISR_TOK);
                }
+               outw(card->IOBase + ISR, FLAG_ISR_TOK);
+       }
+       
+       // Recieve OK, inform read
+       if( status & FLAG_ISR_ROK )
+       {
+                int    read_ofs, end_ofs;
+                int    packet_count = 0;
+                int    len;
                
-               // Recieve OK, inform read
-               if( status & FLAG_ISR_ROK )
+               // Scan recieve buffer for packets
+               end_ofs = inw(card->IOBase + CBA);
+               read_ofs = card->SeenOfs;
+               LOG("read_ofs = %i, end_ofs = %i", read_ofs, end_ofs);
+               if( read_ofs > end_ofs )
                {
-                        int    read_ofs, end_ofs;
-                        int    packet_count = 0;
-                       
-                       // Scan recieve buffer for packets
-                       end_ofs = inw(card->IOBase + CBA);
-                       read_ofs = card->SeenOfs;
-                       LOG("read_ofs = %i, end_ofs = %i", read_ofs, end_ofs);
-                       if( read_ofs > end_ofs )
-                       {
-                               while( read_ofs < card->ReceiveBufferLength )
-                               {
-                                       packet_count ++;
-                                       LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
-                                               packet_count, read_ofs,
-                                               *(Uint16*)&card->ReceiveBuffer[read_ofs],
-                                               *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
-                                               );
-                                       read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
-                                       read_ofs = (read_ofs + 3) & ~3; // Align
-                                       
-                               }
-                               read_ofs = 0;
-                       }
-                       while( read_ofs < end_ofs )
+                       while( read_ofs < card->ReceiveBufferLength )
                        {
+                               packet_count ++;
+                               len = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
                                LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
                                        packet_count, read_ofs,
                                        *(Uint16*)&card->ReceiveBuffer[read_ofs],
-                                       *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
+                                       len
                                        );
-                               packet_count ++;
-                               read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
+                               if(len > 2000) {
+                                       Log_Warning("RTL8139", "IRQ: Packet in buffer exceeds sanity (%i>2000)", len);
+                               }
+                               read_ofs += len + 4;
                                read_ofs = (read_ofs + 3) & ~3; // Align
                        }
-                       card->SeenOfs = read_ofs;
-                       
-                       LOG("packet_count = %i, read_ofs = 0x%x", packet_count, read_ofs);
-                       
-                       if( packet_count )
-                       {
-                               if( Semaphore_Signal( &card->ReadSemaphore, packet_count ) != packet_count ) {
-                                       // Oops?
-                               }
-                               VFS_MarkAvaliable( &card->Node, 1 );
+                       read_ofs -= card->ReceiveBufferLength;
+                       LOG("wrapped read_ofs");
+               }
+               while( read_ofs < end_ofs )
+               {
+                       packet_count ++;
+                       LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
+                               packet_count, read_ofs,
+                               *(Uint16*)&card->ReceiveBuffer[read_ofs],
+                               *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
+                               );
+                       read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
+                       read_ofs = (read_ofs + 3) & ~3; // Align
+               }
+               if( read_ofs != end_ofs ) {
+                       Log_Warning("RTL8139", "IRQ: read_ofs (%i) != end_ofs(%i)", read_ofs, end_ofs);
+                       read_ofs = end_ofs;
+               }
+               card->SeenOfs = read_ofs;
+               
+               LOG("packet_count = %i, read_ofs = 0x%x", packet_count, read_ofs);
+               
+               if( packet_count )
+               {
+                       if( Semaphore_Signal( &card->ReadSemaphore, packet_count ) != packet_count ) {
+                               // Oops?
                        }
-                       
-                       outw(card->IOBase + ISR, FLAG_ISR_ROK);
+                       VFS_MarkAvaliable( &card->Node, 1 );
                }
-       }
+               
+               outw(card->IOBase + ISR, FLAG_ISR_ROK);
+       }       
 }

UCC git Repository :: git.ucc.asn.au