2 * Acess2 RTL8139 Driver
3 * - By John Hodge (thePowersGang)
6 #define VERSION ((0<<8)|20)
11 #include <api_drv_network.h>
12 #include <semaphore.h>
15 #define VENDOR_ID 0x10EC
16 #define DEVICE_ID 0x8139
24 // Multicast Registers
25 MAR0 = 0x08, MAR1, MAR2, MAR3,
26 MAR4, MAR5, MAR6, MAR7,
28 // Transmit status of descriptors 0 - 3
29 TSD0 = 0x10, TSD1 = 0x14,
30 TSD2 = 0x18, TSD3 = 0x1C,
31 // Transmit start addresses
32 TSAD0 = 0x20, TSAD1 = 0x24,
33 TSAD2 = 0x28, TSAD3 = 0x2C,
35 RBSTART = 0x30, //!< Recieve Buffer Start (DWord)
36 // Early Recieve Byte Count
37 ERBCR = 0x34, // 16-bits
38 // Early RX Status Register
41 // ??, ??, ??, RST, RE, TE, ??, ??
44 CAPR = 0x38, // Current address of packet read
45 CBA = 0x3A, // Current Buffer Address - Total byte count in RX buffer
47 IMR = 0x3C, // Interrupt mask register
48 ISR = 0x3E, // Interrupt status register
50 TCR = 0x40, // Transmit Configuration Register
51 RCR = 0x44, // Recieve Configuration Register
52 TCTR = 0x48, // 32-bit timer (count)
53 MPC = 0x4C, // Missed packet count (due to RX overflow)
59 TIMERINT = 0x54, // Fires a timeout when TCTR equals this value
63 #define FLAG_ISR_TOK 0x04
64 #define FLAG_ISR_ROK 0x01
72 int NumWaitingPackets;
75 tPAddr PhysReceiveBuffer;
76 int ReceiveBufferLength;
77 int SeenOfs; //!< End of the most recently seen packet (by IRQ)
79 tSemaphore ReadSemaphore;
81 char *TransmitBuffers[4];
82 tPAddr PhysTransmitBuffers[4];
83 tMutex TransmitInUse[4];
84 tMutex CurTXProtector; //!< Protects \a .CurTXDescriptor
93 int RTL8139_Install(char **Options);
94 char *RTL8139_ReadDir(tVFS_Node *Node, int Pos);
95 tVFS_Node *RTL8139_FindDir(tVFS_Node *Node, const char *Filename);
96 int RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
97 Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
98 Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
99 int RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Arg);
100 void RTL8139_IRQHandler(int Num, void *Ptr);
103 MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
104 tDevFS_Driver gRTL8139_DriverInfo = {
108 .ACLs = &gVFS_ACL_EveryoneRX,
109 .Flags = VFS_FFLAG_DIRECTORY,
110 .ReadDir = RTL8139_ReadDir,
111 .FindDir = RTL8139_FindDir,
112 .IOCtl = RTL8139_RootIOCtl
115 int giRTL8139_CardCount;
116 tCard *gaRTL8139_Cards;
120 * \brief Installs the RTL8139 Driver
122 int RTL8139_Install(char **Options)
129 giRTL8139_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
131 if( giRTL8139_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
133 Log_Debug("RTL8139", "%i cards", giRTL8139_CardCount);
134 gaRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
136 for( i = 0 ; (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1; i ++ )
138 card = &gaRTL8139_Cards[i];
139 base = PCI_GetBAR( id, 0 );
141 Log_Warning("RTL8139", "Driver does not support MMIO, skipping card (addr %x)",
149 card->IRQ = PCI_GetIRQ( id );
151 // Install IRQ Handler
152 IRQ_AddHandler(card->IRQ, RTL8139_IRQHandler, card);
155 outb( base + CONFIG1, 0x00 );
157 // Reset (0x10 to CMD)
158 outb( base + CMD, 0x10 );
159 while( inb(base + CMD) & 0x10 ) ;
161 // Set up recieve buffer
162 // - Allocate 3 pages below 4GiB for the recieve buffer (Allows 8k+16+1500)
163 card->ReceiveBuffer = (void*)MM_AllocDMA( 3, 32, &card->PhysReceiveBuffer );
164 card->ReceiveBufferLength = 8*1024;
165 outd(base + RBSTART, (Uint32)card->PhysReceiveBuffer);
167 outd(base + CAPR, 0);
168 // Set IMR to Transmit OK and Receive OK
169 outw(base + IMR, 0x5);
171 // Set up transmit buffers
172 // - 2 non-contiguous pages (each page can fit 2 1500 byte packets)
173 card->TransmitBuffers[0] = (void*)MM_AllocDMA( 1, 32, &card->PhysTransmitBuffers[0] );
174 card->TransmitBuffers[1] = card->TransmitBuffers[0] + 0x800;
175 card->PhysTransmitBuffers[1] = card->PhysTransmitBuffers[0] + 0x800;
177 card->TransmitBuffers[2] = (void*)MM_AllocDMA( 1, 32, &card->PhysTransmitBuffers[2] );
178 card->TransmitBuffers[3] = card->TransmitBuffers[2] + 0x800;
179 card->PhysTransmitBuffers[3] = card->PhysTransmitBuffers[2] + 0x800;
181 outd(base + TSAD0, card->PhysTransmitBuffers[0]);
182 outd(base + TSAD1, card->PhysTransmitBuffers[1]);
183 outd(base + TSAD2, card->PhysTransmitBuffers[2]);
184 outd(base + TSAD3, card->PhysTransmitBuffers[3]);
186 // Set recieve buffer size and recieve mask
187 // - Bit 7 being set tells the card to overflow the recieve buffer if needed
188 // (i.e. when the packet starts at the end of the bufffer, it overflows up
190 outd(base + RCR, 0x8F);
192 // Recive Enable and Transmit Enable
193 outb(base + CMD, 0x0C);
195 // Get the card's MAC address
196 card->MacAddr[0] = inb(base+MAC0);
197 card->MacAddr[1] = inb(base+MAC1);
198 card->MacAddr[2] = inb(base+MAC2);
199 card->MacAddr[3] = inb(base+MAC3);
200 card->MacAddr[4] = inb(base+MAC4);
201 card->MacAddr[5] = inb(base+MAC5);
204 card->Name[0] = '0'+i;
205 card->Name[1] = '\0';
206 card->Node.ImplPtr = card;
207 card->Node.NumACLs = 0;
208 card->Node.CTime = now();
209 card->Node.Write = RTL8139_Write;
210 card->Node.Read = RTL8139_Read;
211 card->Node.IOCtl = RTL8139_IOCtl;
213 Log_Log("RTL8139", "Card %i 0x%04x, IRQ %i %02x:%02x:%02x:%02x:%02x:%02x",
214 i, card->IOBase, card->IRQ,
215 card->MacAddr[0], card->MacAddr[1], card->MacAddr[2],
216 card->MacAddr[3], card->MacAddr[4], card->MacAddr[5]
220 gRTL8139_DriverInfo.RootNode.Size = giRTL8139_CardCount;
221 DevFS_AddDevice( &gRTL8139_DriverInfo );
223 return MODULE_ERR_OK;
226 // --- Root Functions ---
227 char *RTL8139_ReadDir(tVFS_Node *Node, int Pos)
229 if( Pos < 0 || Pos >= giRTL8139_CardCount ) return NULL;
231 return strdup( gaRTL8139_Cards[Pos].Name );
234 tVFS_Node *RTL8139_FindDir(tVFS_Node *Node, const char *Filename)
236 //TODO: It might be an idea to supprt >10 cards
237 if(Filename[0] == '\0' || Filename[1] != '\0') return NULL;
238 if(Filename[0] < '0' || Filename[0] > '9') return NULL;
239 return &gaRTL8139_Cards[ Filename[0]-'0' ].Node;
242 const char *csaRTL8139_RootIOCtls[] = {DRV_IOCTLNAMES, NULL};
243 int RTL8139_RootIOCtl(tVFS_Node *Node, int ID, void *Data)
245 ENTER("pNode iID pData", Node, ID, Data);
248 BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_RootIOCtls);
254 // --- File Functions ---
255 Uint64 RTL8139_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
257 tCard *card = Node->ImplPtr;
258 Uint16 read_ofs, pkt_length;
261 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
264 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
269 Mutex_Acquire( &card->ReadMutex );
271 read_ofs = inw( card->IOBase + CAPR );
272 LOG("raw read_ofs = %i", read_ofs);
273 read_ofs = (read_ofs + 0x10) & 0xFFFF;
274 LOG("read_ofs = %i", read_ofs);
276 pkt_length = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
278 // Calculate new read offset
279 new_read_ofs = read_ofs + pkt_length + 4;
280 new_read_ofs = (new_read_ofs + 3) & ~3; // Align
281 if(new_read_ofs > card->ReceiveBufferLength) {
282 LOG("wrapping read_ofs");
283 new_read_ofs -= card->ReceiveBufferLength;
285 new_read_ofs -= 0x10; // I dunno
286 LOG("new_read_ofs = %i", new_read_ofs);
289 if( *(Uint16*)&card->ReceiveBuffer[read_ofs] & 0x1E ) {
291 outw(card->IOBase + CAPR, new_read_ofs);
292 Mutex_Release( &card->ReadMutex );
293 goto retry; // I feel evil
297 if( Length > pkt_length ) Length = pkt_length;
298 memcpy(Buffer, &card->ReceiveBuffer[read_ofs+4], Length);
301 outw(card->IOBase + CAPR, new_read_ofs);
303 Mutex_Release( &card->ReadMutex );
310 Uint64 RTL8139_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
314 tCard *card = Node->ImplPtr;
316 if( Length > 1500 ) return 0; // MTU exceeded
318 ENTER("pNode XLength pBuffer", Node, Length, Buffer);
320 // TODO: Implement a semaphore for avaliable transmit buffers
322 // Find an avaliable descriptor
323 Mutex_Acquire(&card->CurTXProtector);
324 td = card->CurTXDescriptor;
325 card->CurTXDescriptor ++;
326 card->CurTXDescriptor %= 4;
327 Mutex_Release(&card->CurTXProtector);
329 Mutex_Acquire( &card->TransmitInUse[td] );
333 // Transmit using descriptor `td`
334 LOG("card->PhysTransmitBuffers[td] = %P", card->PhysTransmitBuffers[td]);
335 outd(card->IOBase + TSAD0 + td*4, card->PhysTransmitBuffers[td]);
336 LOG("card->TransmitBuffers[td] = %p", card->TransmitBuffers[td]);
338 memcpy(card->TransmitBuffers[td], Buffer, Length);
341 status |= Length & 0x1FFF; // 0-12: Length
342 status |= 0 << 13; // 13: OWN bit
343 status |= (0 & 0x3F) << 16; // 16-21: Early TX threshold (zero atm, TODO: check)
344 LOG("status = 0x%08x", status);
345 outd(card->IOBase + TSD0 + td*4, status);
347 LEAVE('i', (int)Length);
352 const char *csaRTL8139_NodeIOCtls[] = {DRV_IOCTLNAMES, NULL};
353 int RTL8139_IOCtl(tVFS_Node *Node, int ID, void *Data)
355 tCard *card = Node->ImplPtr;
356 ENTER("pNode iID pData", Node, ID, Data);
359 BASE_IOCTLS(DRV_TYPE_NETWORK, "RTL8139", VERSION, csaRTL8139_NodeIOCtls);
360 case NET_IOCTL_GETMAC:
361 if( !CheckMem(Data, 6) ) {
365 memcpy( Data, card->MacAddr, 6 );
373 void RTL8139_IRQHandler(int Num, void *Ptr)
379 LOG("Num = %i", Num);
381 if( Num != card->IRQ ) return;
383 status = inw(card->IOBase + ISR);
384 LOG("status = 0x%02x", status);
386 // Transmit OK, a transmit descriptor is now free
387 if( status & FLAG_ISR_TOK )
389 for( j = 0; j < 4; j ++ )
391 if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
392 Mutex_Release( &card->TransmitInUse[j] );
393 // TODO: Update semaphore once implemented
396 outw(card->IOBase + ISR, FLAG_ISR_TOK);
399 // Recieve OK, inform read
400 if( status & FLAG_ISR_ROK )
402 int read_ofs, end_ofs;
403 int packet_count = 0;
406 // Scan recieve buffer for packets
407 end_ofs = inw(card->IOBase + CBA);
408 read_ofs = card->SeenOfs;
409 LOG("read_ofs = %i, end_ofs = %i", read_ofs, end_ofs);
410 if( read_ofs > end_ofs )
412 while( read_ofs < card->ReceiveBufferLength )
415 len = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
416 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
417 packet_count, read_ofs,
418 *(Uint16*)&card->ReceiveBuffer[read_ofs],
422 Log_Warning("RTL8139", "IRQ: Packet in buffer exceeds sanity (%i>2000)", len);
425 read_ofs = (read_ofs + 3) & ~3; // Align
427 read_ofs -= card->ReceiveBufferLength;
428 LOG("wrapped read_ofs");
430 while( read_ofs < end_ofs )
433 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
434 packet_count, read_ofs,
435 *(Uint16*)&card->ReceiveBuffer[read_ofs],
436 *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
438 read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
439 read_ofs = (read_ofs + 3) & ~3; // Align
441 if( read_ofs != end_ofs ) {
442 Log_Warning("RTL8139", "IRQ: read_ofs (%i) != end_ofs(%i)", read_ofs, end_ofs);
445 card->SeenOfs = read_ofs;
447 LOG("packet_count = %i, read_ofs = 0x%x", packet_count, read_ofs);
451 if( Semaphore_Signal( &card->ReadSemaphore, packet_count ) != packet_count ) {
454 VFS_MarkAvaliable( &card->Node, 1 );
457 outw(card->IOBase + ISR, FLAG_ISR_ROK);