2 * Acess2 VIA Rhine II Driver (VT6102)
3 * - By John Hodge (thePowersGang)
6 #define VERSION ((0<<8)|10)
11 #include <api_drv_network.h>
12 #include <semaphore.h>
15 #define VENDOR_ID 0x1106
16 #define DEVICE_ID 0x3065
36 int NumWaitingPackets;
44 int Rhine2_Install(char **Options);
45 char *Rhine2_ReadDir(tVFS_Node *Node, int Pos);
46 tVFS_Node *Rhine2_FindDir(tVFS_Node *Node, const char *Filename);
47 int Rhine2_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
48 Uint64 Rhine2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
49 Uint64 Rhine2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
50 int Rhine2_IOCtl(tVFS_Node *Node, int ID, void *Arg);
51 void Rhine2_IRQHandler(int Num);
54 MODULE_DEFINE(0, VERSION, VIARhineII, Rhine2_Install, NULL, NULL);
55 tVFS_NodeType gRhine2_DirType = {
56 .ReadDir = Rhine2_ReadDir,
57 .FindDir = Rhine2_FindDir,
58 .IOCtl = Rhine2_RootIOCtl
60 tDevFS_Driver gRhine2_DriverInfo = {
64 .ACLs = &gVFS_ACL_EveryoneRX,
65 .Flags = VFS_FFLAG_DIRECTORY,
66 .Type = &gRhine2_DirType
69 int giRhine2_CardCount;
70 tCard *gaRhine2_Cards;
74 * \brief Installs the PCnet3 Driver
76 int Rhine2_Install(char **Options)
83 giRhine2_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
84 Log_Debug("PCnet3", "%i cards", giRhine2_CardCount);
86 if( giRhine2_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
88 gaRhine2_Cards = calloc( giRhine2_CardCount, sizeof(tCard) );
90 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
92 card = &gaRhine2_Cards[i];
94 LOG("BAR0 = 0x%08x", PCI_GetBAR(id, 0));
95 LOG("BAR1 = 0x%08x", PCI_GetBAR(id, 1));
96 LOG("BAR2 = 0x%08x", PCI_GetBAR(id, 2));
97 LOG("BAR3 = 0x%08x", PCI_GetBAR(id, 3));
98 LOG("BAR4 = 0x%08x", PCI_GetBAR(id, 4));
99 LOG("BAR5 = 0x%08x", PCI_GetBAR(id, 5));
101 // card->IOBase = base;
102 // card->IRQ = PCI_GetIRQ( id );
104 // Install IRQ Handler
105 // IRQ_AddHandler(card->IRQ, Rhine2_IRQHandler);
109 // Log_Log("PCnet3", "Card %i 0x%04x, IRQ %i %02x:%02x:%02x:%02x:%02x:%02x",
110 // i, card->IOBase, card->IRQ,
111 // card->MacAddr[0], card->MacAddr[1], card->MacAddr[2],
112 // card->MacAddr[3], card->MacAddr[4], card->MacAddr[5]
118 gRhine2_DriverInfo.RootNode.Size = giRhine2_CardCount;
119 DevFS_AddDevice( &gRhine2_DriverInfo );
121 return MODULE_ERR_OK;
124 // --- Root Functions ---
125 char *Rhine2_ReadDir(tVFS_Node *Node, int Pos)
127 if( Pos < 0 || Pos >= giRhine2_CardCount ) return NULL;
129 return strdup( gaRhine2_Cards[Pos].Name );
132 tVFS_Node *Rhine2_FindDir(tVFS_Node *Node, const char *Filename)
134 //TODO: It might be an idea to supprt >10 cards
135 if(Filename[0] == '\0' || Filename[1] != '\0') return NULL;
136 if(Filename[0] < '0' || Filename[0] > '9') return NULL;
137 return &gaRhine2_Cards[ Filename[0]-'0' ].Node;
140 const char *csaRhine2_RootIOCtls[] = {DRV_IOCTLNAMES, NULL};
141 int Rhine2_RootIOCtl(tVFS_Node *Node, int ID, void *Data)
143 ENTER("pNode iID pData", Node, ID, Data);
146 BASE_IOCTLS(DRV_TYPE_NETWORK, "PCnet3", VERSION, csaRhine2_RootIOCtls);
152 // --- File Functions ---
153 Uint64 Rhine2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
156 tCard *card = Node->ImplPtr;
157 Uint16 read_ofs, pkt_length;
160 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
163 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
168 Mutex_Acquire( &card->ReadMutex );
170 read_ofs = inw( card->IOBase + CAPR );
171 LOG("raw read_ofs = %i", read_ofs);
172 read_ofs = (read_ofs + 0x10) & 0xFFFF;
173 LOG("read_ofs = %i", read_ofs);
175 pkt_length = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
177 // Calculate new read offset
178 new_read_ofs = read_ofs + pkt_length + 4;
179 new_read_ofs = (new_read_ofs + 3) & ~3; // Align
180 if(new_read_ofs > card->ReceiveBufferLength) {
181 LOG("wrapping read_ofs");
182 new_read_ofs -= card->ReceiveBufferLength;
184 new_read_ofs -= 0x10; // I dunno
185 LOG("new_read_ofs = %i", new_read_ofs);
188 if( *(Uint16*)&card->ReceiveBuffer[read_ofs] & 0x1E ) {
190 outw(card->IOBase + CAPR, new_read_ofs);
191 Mutex_Release( &card->ReadMutex );
192 goto retry; // I feel evil
196 if( Length > pkt_length ) Length = pkt_length;
197 memcpy(Buffer, &card->ReceiveBuffer[read_ofs+4], Length);
200 outw(card->IOBase + CAPR, new_read_ofs);
202 Mutex_Release( &card->ReadMutex );
210 Uint64 Rhine2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
215 tCard *card = Node->ImplPtr;
217 if( Length > 1500 ) return 0; // MTU exceeded
219 ENTER("pNode XLength pBuffer", Node, Length, Buffer);
221 // TODO: Implement a semaphore for avaliable transmit buffers
223 // Find an avaliable descriptor
224 Mutex_Acquire(&card->CurTXProtector);
225 td = card->CurTXDescriptor;
226 card->CurTXDescriptor ++;
227 card->CurTXDescriptor %= 4;
228 Mutex_Release(&card->CurTXProtector);
230 Mutex_Acquire( &card->TransmitInUse[td] );
234 // Transmit using descriptor `td`
235 LOG("card->PhysTransmitBuffers[td] = %P", card->PhysTransmitBuffers[td]);
236 outd(card->IOBase + TSAD0 + td*4, card->PhysTransmitBuffers[td]);
237 LOG("card->TransmitBuffers[td] = %p", card->TransmitBuffers[td]);
239 memcpy(card->TransmitBuffers[td], Buffer, Length);
242 status |= Length & 0x1FFF; // 0-12: Length
243 status |= 0 << 13; // 13: OWN bit
244 status |= (0 & 0x3F) << 16; // 16-21: Early TX threshold (zero atm, TODO: check)
245 LOG("status = 0x%08x", status);
246 outd(card->IOBase + TSD0 + td*4, status);
248 LEAVE('i', (int)Length);
254 const char *csaRhine2_NodeIOCtls[] = {DRV_IOCTLNAMES, NULL};
255 int Rhine2_IOCtl(tVFS_Node *Node, int ID, void *Data)
257 tCard *card = Node->ImplPtr;
258 ENTER("pNode iID pData", Node, ID, Data);
261 BASE_IOCTLS(DRV_TYPE_NETWORK, "PCnet3", VERSION, csaRhine2_NodeIOCtls);
262 case NET_IOCTL_GETMAC:
263 if( !CheckMem(Data, 6) ) {
267 memcpy( Data, card->MacAddr, 6 );
275 void Rhine2_IRQHandler(int Num)
282 LOG("Num = %i", Num);
284 for( i = 0; i < giRhine2_CardCount; i ++ )
286 card = &gaRhine2_Cards[i];
287 if( Num != card->IRQ ) break;
289 status = inw(card->IOBase + ISR);
290 LOG("status = 0x%02x", status);
292 // Transmit OK, a transmit descriptor is now free
293 if( status & FLAG_ISR_TOK )
295 for( j = 0; j < 4; j ++ )
297 if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
298 Mutex_Release( &card->TransmitInUse[j] );
299 // TODO: Update semaphore once implemented
302 outw(card->IOBase + ISR, FLAG_ISR_TOK);
305 // Recieve OK, inform read
306 if( status & FLAG_ISR_ROK )
308 int read_ofs, end_ofs;
309 int packet_count = 0;
312 // Scan recieve buffer for packets
313 end_ofs = inw(card->IOBase + CBA);
314 read_ofs = card->SeenOfs;
315 LOG("read_ofs = %i, end_ofs = %i", read_ofs, end_ofs);
316 if( read_ofs > end_ofs )
318 while( read_ofs < card->ReceiveBufferLength )
321 len = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
322 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
323 packet_count, read_ofs,
324 *(Uint16*)&card->ReceiveBuffer[read_ofs],
328 Log_Warning("PCnet3", "IRQ: Packet in buffer exceeds sanity (%i>2000)", len);
331 read_ofs = (read_ofs + 3) & ~3; // Align
333 read_ofs -= card->ReceiveBufferLength;
334 LOG("wrapped read_ofs");
336 while( read_ofs < end_ofs )
339 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
340 packet_count, read_ofs,
341 *(Uint16*)&card->ReceiveBuffer[read_ofs],
342 *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
344 read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
345 read_ofs = (read_ofs + 3) & ~3; // Align
347 if( read_ofs != end_ofs ) {
348 Log_Warning("PCnet3", "IRQ: read_ofs (%i) != end_ofs(%i)", read_ofs, end_ofs);
351 card->SeenOfs = read_ofs;
353 LOG("packet_count = %i, read_ofs = 0x%x", packet_count, read_ofs);
357 if( Semaphore_Signal( &card->ReadSemaphore, packet_count ) != packet_count ) {
360 VFS_MarkAvaliable( &card->Node, 1 );
363 outw(card->IOBase + ISR, FLAG_ISR_ROK);