RT8139 - Fixed to use Mutexes to control access to TXDescs
[tpg/acess2.git] / Modules / Network / RTL8139 / rtl8139.c
index d77fe27..af4ace7 100644 (file)
@@ -80,8 +80,9 @@ typedef struct sCard
        
        char    *TransmitBuffers[4];
        tPAddr  PhysTransmitBuffers[4];
-       BOOL    TransmitInUse;  // Flags for each transmit descriptor
-        int    CurTXDecscriptor;
+       tMutex  TransmitInUse[4];
+       tMutex  CurTXProtector; //!< Protects \a .CurTXDescriptor
+        int    CurTXDescriptor;
        
        char    Name[2];
        tVFS_Node       Node;
@@ -125,7 +126,7 @@ int RTL8139_Install(char **Options)
        Uint16  base;
        tCard   *card;
        
-       giRTL8139_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID, 0);
+       giRTL8139_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
        Log_Debug("RTL8139", "%i cards", giRTL8139_CardCount);
        
        if( giRTL8139_CardCount == 0 )  return MODULE_ERR_NOTNEEDED;
@@ -133,10 +134,17 @@ int RTL8139_Install(char **Options)
        gaRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
        
        //while( (id = PCI_GetDevice(0x10EC, 0x8139, 0, id)) != -1 )
-       while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, 0, i)) != -1 )
+       while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
        {
                card = &gaRTL8139_Cards[i];
-               base = PCI_AssignPort( id, 0, 0x100 );
+               base = PCI_GetBAR( id, 0 );
+               if( !(base & 1) ) {
+                       Log_Warning("RTL8139", "Driver does not support MMIO, skipping card");
+                       card->IOBase = 0;
+                       card->IRQ = 0;
+                       continue ;
+               }
+               base &= ~1;
                card->IOBase = base;
                card->IRQ = PCI_GetIRQ( id );
                
@@ -306,19 +314,22 @@ Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer
        ENTER("pNode XLength pBuffer", Node, Length, Buffer);
        
        // TODO: Implement a semaphore for avaliable transmit buffers
-       
-       td = card->CurTXDecscriptor;
-       
+
        // Find an avaliable descriptor
-       while( card->TransmitInUse & (1 << td) )
-               Threads_Yield();
+       Mutex_Acquire(&card->CurTXProtector);
+       td = card->CurTXDescriptor;
+       card->CurTXDescriptor ++;
+       card->CurTXDescriptor %= 4;
+       Mutex_Release(&card->CurTXProtector);
+       // - Lock it
+       Mutex_Acquire( &card->TransmitInUse[td] );
        
        LOG("td = %i", td);
        
        // Transmit using descriptor `td`
-       card->TransmitInUse |= (1 << td);
+       LOG("card->PhysTransmitBuffers[td] = %P", card->PhysTransmitBuffers[td]);
        outd(card->IOBase + TSAD0 + td*4, card->PhysTransmitBuffers[td]);
-       LOG("card->PhysTransmitBuffers[td] = 0x%llx", card->PhysTransmitBuffers[td]);
+       LOG("card->TransmitBuffers[td] = %p", card->TransmitBuffers[td]);
        // Copy to buffer
        memcpy(card->TransmitBuffers[td], Buffer, Length);
        // Start
@@ -329,9 +340,6 @@ Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer
        LOG("status = 0x%08x", status);
        outd(card->IOBase + TSD0 + td*4, status);
        
-       card->CurTXDecscriptor ++;
-       card->CurTXDecscriptor %= 4;
-       
        LEAVE('i', (int)Length);
        
        return Length;
@@ -363,6 +371,8 @@ void RTL8139_IRQHandler(int Num)
         int    i, j;
        tCard   *card;
        Uint16  status;
+
+       LOG("Num = %i", Num);
        
        for( i = 0; i < giRTL8139_CardCount; i ++ )
        {
@@ -378,7 +388,7 @@ void RTL8139_IRQHandler(int Num)
                        for( j = 0; j < 4; j ++ )
                        {
                                if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
-                                       card->TransmitInUse &= ~(1 << j);
+                                       Mutex_Release( &card->TransmitInUse[j] );
                                        // TODO: Update semaphore once implemented
                                }
                        }

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