2 * Acess2 PCnet-FAST III Driver
3 * - By John Hodge (thePowersGang)
6 #define VERSION ((0<<8)|10)
11 #include <tpl_drv_network.h>
12 #include <semaphore.h>
15 #define VENDOR_ID 0x1022
16 #define DEVICE_ID 0x2000
24 REG_RDP = 0x10, // 16 bit
25 REG_RAP = 0x14, // 8-bit
26 REG_RESET = 0x18, // 16-bit
27 REG_BDP = 0x1C, // 16-bit
32 CSR_STATUS, // CSR0 - Am79C973/Am79C975 Controller Status
33 CSR_IBA0, // CSR1 - Initialization Block Address[15:0]
34 CSR_IBA1, // CSR2 - Initialization Block Address[31:16]
35 CSR_INTMASK, // CSR3 - Interrupt Masks and Deferral Control
37 CSR_MAC0 = 12, // CSR12 - Physical Address[15:0]
38 CSR_MAC1 = 13, // CSR13 - Physical Address[31:16]
39 CSR_MAC2 = 14, // CSR14 - Physical Address[47:32]
40 CSR_MODE = 15, // CSR15 - Mode
42 CSR_RXBASE0 = 24, // CSR24 - Base Address of Receive Ring Lower
43 CSR_RXBASE1 = 25, // CSR25 - Base Address of Receive Ring Upper
44 CSR_TXBASE0 = 30, // CSR26 - Base Address of Transmit Ring Lower
45 CSR_TXBASE1 = 31, // CSR27 - Base Address of Transmit Ring Upper
47 CSR_RXLENGTH = 76, // CSR76 - Receive Ring Length
48 CSR_TXLENGTH = 78, // CSR78 - Transmit Ring Length
53 BCR_PHYCS = 32, // BCR32 - Internal PHY Control and Status
54 BCR_PHYADDR, // BCR33 - Internal PHY Address
55 BCR_PHYMGMT, // BCR34 - Internal PHY Management Data
64 int NumWaitingPackets;
72 int PCnet3_Install(char **Options);
73 char *PCnet3_ReadDir(tVFS_Node *Node, int Pos);
74 tVFS_Node *PCnet3_FindDir(tVFS_Node *Node, const char *Filename);
75 int PCnet3_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
76 Uint64 PCnet3_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
77 Uint64 PCnet3_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
78 int PCnet3_IOCtl(tVFS_Node *Node, int ID, void *Arg);
79 void PCnet3_IRQHandler(int Num);
82 MODULE_DEFINE(0, VERSION, PCnet3, PCnet3_Install, NULL, NULL);
83 tDevFS_Driver gPCnet3_DriverInfo = {
87 .ACLs = &gVFS_ACL_EveryoneRX,
88 .Flags = VFS_FFLAG_DIRECTORY,
89 .ReadDir = PCnet3_ReadDir,
90 .FindDir = PCnet3_FindDir,
91 .IOCtl = PCnet3_RootIOCtl
94 int giPCnet3_CardCount;
95 tCard *gaPCnet3_Cards;
99 * \brief Installs the PCnet3 Driver
101 int PCnet3_Install(char **Options)
108 giPCnet3_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
109 Log_Debug("PCnet3", "%i cards", giPCnet3_CardCount);
111 if( giPCnet3_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
113 gaPCnet3_Cards = calloc( giPCnet3_CardCount, sizeof(tCard) );
115 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
117 card = &gaPCnet3_Cards[i];
118 base = PCI_GetBAR( id, 0 );
120 Log_Warning("PCnet3", "Driver does not support MMIO, skipping card");
127 card->IRQ = PCI_GetIRQ( id );
129 // Install IRQ Handler
130 IRQ_AddHandler(card->IRQ, PCnet3_IRQHandler);
134 Log_Log("PCnet3", "Card %i 0x%04x, IRQ %i %02x:%02x:%02x:%02x:%02x:%02x",
135 i, card->IOBase, card->IRQ,
136 card->MacAddr[0], card->MacAddr[1], card->MacAddr[2],
137 card->MacAddr[3], card->MacAddr[4], card->MacAddr[5]
143 gPCnet3_DriverInfo.RootNode.Size = giPCnet3_CardCount;
144 DevFS_AddDevice( &gPCnet3_DriverInfo );
146 return MODULE_ERR_OK;
149 // --- Root Functions ---
150 char *PCnet3_ReadDir(tVFS_Node *Node, int Pos)
152 if( Pos < 0 || Pos >= giPCnet3_CardCount ) return NULL;
154 return strdup( gaPCnet3_Cards[Pos].Name );
157 tVFS_Node *PCnet3_FindDir(tVFS_Node *Node, const char *Filename)
159 //TODO: It might be an idea to supprt >10 cards
160 if(Filename[0] == '\0' || Filename[1] != '\0') return NULL;
161 if(Filename[0] < '0' || Filename[0] > '9') return NULL;
162 return &gaPCnet3_Cards[ Filename[0]-'0' ].Node;
165 const char *csaPCnet3_RootIOCtls[] = {DRV_IOCTLNAMES, NULL};
166 int PCnet3_RootIOCtl(tVFS_Node *Node, int ID, void *Data)
168 ENTER("pNode iID pData", Node, ID, Data);
171 BASE_IOCTLS(DRV_TYPE_NETWORK, "PCnet3", VERSION, csaPCnet3_RootIOCtls);
177 // --- File Functions ---
178 Uint64 PCnet3_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
180 tCard *card = Node->ImplPtr;
181 Uint16 read_ofs, pkt_length;
184 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
187 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
192 Mutex_Acquire( &card->ReadMutex );
194 read_ofs = inw( card->IOBase + CAPR );
195 LOG("raw read_ofs = %i", read_ofs);
196 read_ofs = (read_ofs + 0x10) & 0xFFFF;
197 LOG("read_ofs = %i", read_ofs);
199 pkt_length = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
201 // Calculate new read offset
202 new_read_ofs = read_ofs + pkt_length + 4;
203 new_read_ofs = (new_read_ofs + 3) & ~3; // Align
204 if(new_read_ofs > card->ReceiveBufferLength) {
205 LOG("wrapping read_ofs");
206 new_read_ofs -= card->ReceiveBufferLength;
208 new_read_ofs -= 0x10; // I dunno
209 LOG("new_read_ofs = %i", new_read_ofs);
212 if( *(Uint16*)&card->ReceiveBuffer[read_ofs] & 0x1E ) {
214 outw(card->IOBase + CAPR, new_read_ofs);
215 Mutex_Release( &card->ReadMutex );
216 goto retry; // I feel evil
220 if( Length > pkt_length ) Length = pkt_length;
221 memcpy(Buffer, &card->ReceiveBuffer[read_ofs+4], Length);
224 outw(card->IOBase + CAPR, new_read_ofs);
226 Mutex_Release( &card->ReadMutex );
233 Uint64 PCnet3_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
237 tCard *card = Node->ImplPtr;
239 if( Length > 1500 ) return 0; // MTU exceeded
241 ENTER("pNode XLength pBuffer", Node, Length, Buffer);
243 // TODO: Implement a semaphore for avaliable transmit buffers
245 // Find an avaliable descriptor
246 Mutex_Acquire(&card->CurTXProtector);
247 td = card->CurTXDescriptor;
248 card->CurTXDescriptor ++;
249 card->CurTXDescriptor %= 4;
250 Mutex_Release(&card->CurTXProtector);
252 Mutex_Acquire( &card->TransmitInUse[td] );
256 // Transmit using descriptor `td`
257 LOG("card->PhysTransmitBuffers[td] = %P", card->PhysTransmitBuffers[td]);
258 outd(card->IOBase + TSAD0 + td*4, card->PhysTransmitBuffers[td]);
259 LOG("card->TransmitBuffers[td] = %p", card->TransmitBuffers[td]);
261 memcpy(card->TransmitBuffers[td], Buffer, Length);
264 status |= Length & 0x1FFF; // 0-12: Length
265 status |= 0 << 13; // 13: OWN bit
266 status |= (0 & 0x3F) << 16; // 16-21: Early TX threshold (zero atm, TODO: check)
267 LOG("status = 0x%08x", status);
268 outd(card->IOBase + TSD0 + td*4, status);
270 LEAVE('i', (int)Length);
275 const char *csaPCnet3_NodeIOCtls[] = {DRV_IOCTLNAMES, NULL};
276 int PCnet3_IOCtl(tVFS_Node *Node, int ID, void *Data)
278 tCard *card = Node->ImplPtr;
279 ENTER("pNode iID pData", Node, ID, Data);
282 BASE_IOCTLS(DRV_TYPE_NETWORK, "PCnet3", VERSION, csaPCnet3_NodeIOCtls);
283 case NET_IOCTL_GETMAC:
284 if( !CheckMem(Data, 6) ) {
288 memcpy( Data, card->MacAddr, 6 );
296 void PCnet3_IRQHandler(int Num)
302 LOG("Num = %i", Num);
304 for( i = 0; i < giPCnet3_CardCount; i ++ )
306 card = &gaPCnet3_Cards[i];
307 if( Num != card->IRQ ) break;
309 status = inw(card->IOBase + ISR);
310 LOG("status = 0x%02x", status);
312 // Transmit OK, a transmit descriptor is now free
313 if( status & FLAG_ISR_TOK )
315 for( j = 0; j < 4; j ++ )
317 if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
318 Mutex_Release( &card->TransmitInUse[j] );
319 // TODO: Update semaphore once implemented
322 outw(card->IOBase + ISR, FLAG_ISR_TOK);
325 // Recieve OK, inform read
326 if( status & FLAG_ISR_ROK )
328 int read_ofs, end_ofs;
329 int packet_count = 0;
332 // Scan recieve buffer for packets
333 end_ofs = inw(card->IOBase + CBA);
334 read_ofs = card->SeenOfs;
335 LOG("read_ofs = %i, end_ofs = %i", read_ofs, end_ofs);
336 if( read_ofs > end_ofs )
338 while( read_ofs < card->ReceiveBufferLength )
341 len = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
342 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
343 packet_count, read_ofs,
344 *(Uint16*)&card->ReceiveBuffer[read_ofs],
348 Log_Warning("PCnet3", "IRQ: Packet in buffer exceeds sanity (%i>2000)", len);
351 read_ofs = (read_ofs + 3) & ~3; // Align
353 read_ofs -= card->ReceiveBufferLength;
354 LOG("wrapped read_ofs");
356 while( read_ofs < end_ofs )
359 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
360 packet_count, read_ofs,
361 *(Uint16*)&card->ReceiveBuffer[read_ofs],
362 *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
364 read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
365 read_ofs = (read_ofs + 3) & ~3; // Align
367 if( read_ofs != end_ofs ) {
368 Log_Warning("PCnet3", "IRQ: read_ofs (%i) != end_ofs(%i)", read_ofs, end_ofs);
371 card->SeenOfs = read_ofs;
373 LOG("packet_count = %i, read_ofs = 0x%x", packet_count, read_ofs);
377 if( Semaphore_Signal( &card->ReadSemaphore, packet_count ) != packet_count ) {
380 VFS_MarkAvaliable( &card->Node, 1 );
383 outw(card->IOBase + ISR, FLAG_ISR_ROK);