2 * Acess2 RTL8139 Driver
3 * - By John Hodge (thePowersGang)
6 #define VERSION ((0<<8)|20)
10 #include <semaphore.h>
11 #include <IPStack/include/adapters_api.h>
14 #define VENDOR_ID 0x10EC
15 #define DEVICE_ID 0x8139
23 // Multicast Registers
24 MAR0 = 0x08, MAR1, MAR2, MAR3,
25 MAR4, MAR5, MAR6, MAR7,
27 // Transmit status of descriptors 0 - 3
28 TSD0 = 0x10, TSD1 = 0x14,
29 TSD2 = 0x18, TSD3 = 0x1C,
30 // Transmit start addresses
31 TSAD0 = 0x20, TSAD1 = 0x24,
32 TSAD2 = 0x28, TSAD3 = 0x2C,
34 RBSTART = 0x30, //!< Recieve Buffer Start (DWord)
35 // Early Recieve Byte Count
36 ERBCR = 0x34, // 16-bits
37 // Early RX Status Register
40 // -, -, -, RST, RE, TE, -, BUFE
43 CAPR = 0x38, // Current address of packet read
44 CBA = 0x3A, // Current Buffer Address - Total byte count in RX buffer
46 IMR = 0x3C, // Interrupt mask register
47 ISR = 0x3E, // Interrupt status register
49 TCR = 0x40, // Transmit Configuration Register
50 RCR = 0x44, // Recieve Configuration Register
51 TCTR = 0x48, // 32-bit timer (count)
52 MPC = 0x4C, // Missed packet count (due to RX overflow)
58 TIMERINT = 0x54, // Fires a timeout when TCTR equals this value
62 #define FLAG_ISR_SERR 0x8000 // System error
63 #define FLAG_ISR_TIMEO 0x4000 // Timer timeout (See TIMERINT)
64 #define FLAG_ISR_LENCHG 0x2000 // Cable length changed
65 #define FLAG_ISR_FOVW 0x0040 // Rx FIFO Underflow
66 #define FLAG_ISR_PUN 0x0020 // Packet Underrung
67 #define FLAG_ISR_RXOVW 0x0010 // Rx Buffer Overflow
68 #define FLAG_ISR_TER 0x0008 // Tx Error
69 #define FLAG_ISR_TOK 0x0004 // Tx OK
70 #define FLAG_ISR_RER 0x0002 // Rx Error
71 #define FLAG_ISR_ROK 0x0001 // Rx OK
79 int NumWaitingPackets;
82 tPAddr PhysReceiveBuffer;
83 int ReceiveBufferLength;
84 int SeenOfs; //!< End of the most recently seen packet (by IRQ)
86 tSemaphore ReadSemaphore;
88 char *TransmitBuffers[4];
89 tPAddr PhysTransmitBuffers[4];
90 tMutex TransmitInUse[4];
91 tMutex CurTXProtector; //!< Protects \a .CurTXDescriptor
99 int RTL8139_Install(char **Options);
100 tIPStackBuffer *RTL8139_WaitForPacket(void *Ptr);
101 void RTL8139_int_UpdateCAPR(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr);
102 int RTL8139_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
103 void RTL8139_IRQHandler(int Num, void *Ptr);
106 MODULE_DEFINE(0, VERSION, RTL8139, RTL8139_Install, NULL, NULL);
107 tIPStack_AdapterType gRTL8139_AdapterType = {
109 .Type = 0, // TODO: Differentiate differnet wire protos and speeds
110 .Flags = 0, // TODO: IP checksum offloading, MAC checksum offloading etc
111 .SendPacket = RTL8139_SendPacket,
112 .WaitForPacket = RTL8139_WaitForPacket
114 int giRTL8139_CardCount;
115 tCard *gaRTL8139_Cards;
119 * \brief Installs the RTL8139 Driver
121 int RTL8139_Install(char **Options)
128 giRTL8139_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
130 if( giRTL8139_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
132 Log_Debug("RTL8139", "%i cards", giRTL8139_CardCount);
133 gaRTL8139_Cards = calloc( giRTL8139_CardCount, sizeof(tCard) );
135 for( i = 0 ; (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1; i ++ )
137 card = &gaRTL8139_Cards[i];
138 base = PCI_GetBAR( id, 0 );
140 Log_Warning("RTL8139", "Driver does not support MMIO, skipping card (addr %x)",
148 card->IRQ = PCI_GetIRQ( id );
150 // Install IRQ Handler
151 IRQ_AddHandler(card->IRQ, RTL8139_IRQHandler, card);
154 outb( base + CONFIG1, 0x00 );
156 // Reset (0x10 to CMD)
157 outb( base + CMD, 0x10 );
158 while( inb(base + CMD) & 0x10 ) ;
160 // Set IMR to all interrupts!
161 outw(base + IMR, 0xE07F);
163 // Set up recieve buffer
164 // - Allocate 3 pages below 4GiB for the recieve buffer (Allows 8k+16+1500)
165 card->ReceiveBuffer = (void*)MM_AllocDMA( 3, 32, &card->PhysReceiveBuffer );
166 card->ReceiveBufferLength = 8*1024;
167 outd(base + RBSTART, (Uint32)card->PhysReceiveBuffer);
169 outd(base + CAPR, 0);
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 // Bits 10: 8 set the max DMA burst size (6=1024)
191 // Bits 12:11 set the size (8k+16 -> 64k+16)
192 // Bits 15:13 set the FIFO threshold (6 = 1024)
193 outd(base + RCR, (6<<13)|(6<<8)|0x80|0x1F); // All valid + runt packets
195 // Recive Enable and Transmit Enable
196 outb(base + CMD, 0x0C);
198 // Get the card's MAC address
199 card->MacAddr[0] = inb(base+MAC0);
200 card->MacAddr[1] = inb(base+MAC1);
201 card->MacAddr[2] = inb(base+MAC2);
202 card->MacAddr[3] = inb(base+MAC3);
203 card->MacAddr[4] = inb(base+MAC4);
204 card->MacAddr[5] = inb(base+MAC5);
206 // Interface with IPStack
207 card->IPStackHandle = IPStack_Adapter_Add(&gRTL8139_AdapterType, card, card->MacAddr);
209 Log_Log("RTL8139", "Card %i 0x%04x, IRQ %i %02x:%02x:%02x:%02x:%02x:%02x",
210 i, card->IOBase, card->IRQ,
211 card->MacAddr[0], card->MacAddr[1], card->MacAddr[2],
212 card->MacAddr[3], card->MacAddr[4], card->MacAddr[5]
216 return MODULE_ERR_OK;
219 // --- File Functions ---
220 tIPStackBuffer *RTL8139_WaitForPacket(void *Ptr)
223 Uint16 read_ofs, pkt_length;
229 LOG("IMR = %x, ISR = %x", inw(card->IOBase + IMR), inw(card->IOBase + ISR));
230 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
236 Mutex_Acquire( &card->ReadMutex );
238 read_ofs = inw( card->IOBase + CAPR );
239 LOG("raw read_ofs = %i", read_ofs);
240 read_ofs = (read_ofs + 0x10) & 0xFFFF;
241 LOG("read_ofs = %i", read_ofs);
243 pkt_length = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
246 if( *(Uint16*)&card->ReceiveBuffer[read_ofs] & 0x1E )
249 RTL8139_int_UpdateCAPR( card, pkt_length, 0, &card->ReceiveBuffer[read_ofs+4] );
250 Mutex_Release( &card->ReadMutex );
251 goto retry; // I feel evil
254 ret = IPStack_Buffer_CreateBuffer(1);
255 IPStack_Buffer_AppendSubBuffer(ret,
256 pkt_length, 0, &card->ReceiveBuffer[read_ofs+4],
257 RTL8139_int_UpdateCAPR, card
260 Mutex_Release( &card->ReadMutex );
267 * \brief Updates CAPR after a packet has been read
268 * \note Assumes that buffers are freed in the order we allocate them
270 void RTL8139_int_UpdateCAPR(void *Ptr, size_t pkt_length, size_t Unused, const void *DataPtr)
273 int read_ofs = DataPtr - (const void *)card->ReceiveBuffer - 4;
276 // Calculate new read offset
277 new_read_ofs = read_ofs + pkt_length + 4;
278 new_read_ofs = (new_read_ofs + 3) & ~3; // Align
279 if(new_read_ofs > card->ReceiveBufferLength) {
280 LOG("wrapping read_ofs");
281 new_read_ofs -= card->ReceiveBufferLength;
283 new_read_ofs -= 0x10; // I dunno
284 LOG("new_read_ofs = %i", new_read_ofs);
287 outw(card->IOBase + CAPR, new_read_ofs);
290 int RTL8139_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
296 ENTER("pPtr pBuffer", Ptr, Buffer);
298 length = IPStack_Buffer_GetLength(Buffer);
299 if( length > 1500 ) {
301 return -1; // MTU exceeded
304 // TODO: Implement a semaphore for avaliable transmit buffers
306 // Find an avaliable descriptor
307 Mutex_Acquire(&card->CurTXProtector);
308 td = card->CurTXDescriptor;
309 card->CurTXDescriptor ++;
310 card->CurTXDescriptor %= 4;
311 Mutex_Release(&card->CurTXProtector);
313 Mutex_Acquire( &card->TransmitInUse[td] );
317 // Transmit using descriptor `td`
318 LOG("card->PhysTransmitBuffers[td] = %P", card->PhysTransmitBuffers[td]);
319 outd(card->IOBase + TSAD0 + td*4, card->PhysTransmitBuffers[td]);
322 LOG("card->TransmitBuffers[td] = %p", card->TransmitBuffers[td]);
323 IPStack_Buffer_GetData(Buffer, card->TransmitBuffers[td], length);
327 status |= length & 0x1FFF; // 0-12: Length
328 status |= 0 << 13; // 13: OWN bit
329 status |= (0 & 0x3F) << 16; // 16-21: Early TX threshold (zero atm, TODO: check)
330 LOG("status = 0x%08x", status);
331 outd(card->IOBase + TSD0 + td*4, status);
337 void RTL8139_IRQHandler(int Num, void *Ptr)
343 if( Num != card->IRQ ) return;
345 status = inw(card->IOBase + ISR);
346 if( !status ) return ;
347 LOG("status = 0x%02x", status);
349 // Transmit OK, a transmit descriptor is now free
350 if( status & FLAG_ISR_TOK )
352 for( j = 0; j < 4; j ++ )
354 if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
355 Mutex_Release( &card->TransmitInUse[j] );
356 // TODO: Update semaphore once implemented
359 outw(card->IOBase + ISR, FLAG_ISR_TOK);
362 // Transmit error, ... oops
363 if( status & FLAG_ISR_TER )
365 Log_Error("RTK8139", "Tx Error, dunno what to do");
366 outw(card->IOBase + ISR, FLAG_ISR_TER);
369 // Recieve OK, inform read
370 if( status & FLAG_ISR_ROK )
372 int read_ofs, end_ofs;
373 int packet_count = 0;
376 // Scan recieve buffer for packets
377 end_ofs = inw(card->IOBase + CBA);
378 read_ofs = card->SeenOfs;
379 LOG("read_ofs = %i, end_ofs = %i", read_ofs, end_ofs);
380 if( read_ofs > end_ofs )
382 while( read_ofs < card->ReceiveBufferLength )
385 len = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
386 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
387 packet_count, read_ofs,
388 *(Uint16*)&card->ReceiveBuffer[read_ofs],
392 Log_Warning("RTL8139", "IRQ: Packet in buffer exceeds sanity (%i>2000)", len);
395 read_ofs = (read_ofs + 3) & ~3; // Align
397 read_ofs -= card->ReceiveBufferLength;
398 LOG("wrapped read_ofs");
400 while( read_ofs < end_ofs )
403 // Log_Debug("RTL8139", "RX %i at 0x%x", packet_count, read_ofs);
404 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
405 packet_count, read_ofs,
406 *(Uint16*)&card->ReceiveBuffer[read_ofs],
407 *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
409 read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
410 read_ofs = (read_ofs + 3) & ~3; // Align
412 if( read_ofs != end_ofs ) {
413 Log_Warning("RTL8139", "IRQ: read_ofs (%i) != end_ofs(%i)", read_ofs, end_ofs);
416 card->SeenOfs = read_ofs;
418 LOG("packet_count = %i, read_ofs = 0x%x", packet_count, read_ofs);
422 if( Semaphore_Signal( &card->ReadSemaphore, packet_count ) != packet_count ) {
424 Log_Warning("RTL8139", "IRQ: signalling read semaphore failed, Oops?");
428 outw(card->IOBase + ISR, FLAG_ISR_ROK);
431 // Recieve error, ... oops
432 if( status & FLAG_ISR_RER )
434 Log_Error("RTL8139", "Rx Error, dunno what to do");
435 outw(card->IOBase + ISR, FLAG_ISR_RER);
438 // Packet Underrun/Link Change
439 if( status & FLAG_ISR_PUN )
441 // Set when CAPR is written but Rx is empty, OR when the link status changes
442 Log_Notice("RTL8139", "ISR[PUN] set... hmmm");
443 outw(card->IOBase + ISR, FLAG_ISR_PUN);
447 if( status & FLAG_ISR_RXOVW )
449 Log_Error("RTL8139", "Rx Overflow... oh fsck");
450 outw(card->IOBase + ISR, FLAG_ISR_RXOVW);
454 if( status & FLAG_ISR_FOVW )
456 Log_Error("RTL8139", "Rx FIFO Overflow... huh?");
457 outw(card->IOBase + ISR, FLAG_ISR_FOVW);