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>
13 #include "rhine2_hw.h"
16 #define VENDOR_ID 0x1106
17 #define DEVICE_ID 0x3065
25 int NumWaitingPackets;
27 Uint32 DescTablePAddr;
36 int Rhine2_Install(char **Options);
37 char *Rhine2_ReadDir(tVFS_Node *Node, int Pos);
38 tVFS_Node *Rhine2_FindDir(tVFS_Node *Node, const char *Filename);
39 int Rhine2_RootIOCtl(tVFS_Node *Node, int ID, void *Arg);
40 Uint64 Rhine2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
41 Uint64 Rhine2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer);
42 int Rhine2_IOCtl(tVFS_Node *Node, int ID, void *Arg);
43 void Rhine2_IRQHandler(int Num);
46 MODULE_DEFINE(0, VERSION, VIARhineII, Rhine2_Install, NULL, NULL);
47 tVFS_NodeType gRhine2_DirType = {
48 .ReadDir = Rhine2_ReadDir,
49 .FindDir = Rhine2_FindDir,
50 .IOCtl = Rhine2_RootIOCtl
52 tDevFS_Driver gRhine2_DriverInfo = {
56 .ACLs = &gVFS_ACL_EveryoneRX,
57 .Flags = VFS_FFLAG_DIRECTORY,
58 .Type = &gRhine2_DirType
61 int giRhine2_CardCount;
62 tCard *gaRhine2_Cards;
66 * \brief Initialises the driver
68 int Rhine2_Install(char **Options)
75 giRhine2_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
76 Log_Debug("Rhine2", "%i cards", giRhine2_CardCount);
78 if( giRhine2_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
80 gaRhine2_Cards = calloc( giRhine2_CardCount, sizeof(tCard) );
82 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
84 card = &gaRhine2_Cards[i];
86 LOG("BAR0 = 0x%08x", PCI_GetBAR(id, 0));
87 LOG("BAR1 = 0x%08x", PCI_GetBAR(id, 1));
88 LOG("BAR2 = 0x%08x", PCI_GetBAR(id, 2));
89 LOG("BAR3 = 0x%08x", PCI_GetBAR(id, 3));
90 LOG("BAR4 = 0x%08x", PCI_GetBAR(id, 4));
91 LOG("BAR5 = 0x%08x", PCI_GetBAR(id, 5));
93 // card->IOBase = base;
94 // card->IRQ = PCI_GetIRQ( id );
96 // Install IRQ Handler
97 // IRQ_AddHandler(card->IRQ, Rhine2_IRQHandler);
101 // Log_Log("PCnet3", "Card %i 0x%04x, IRQ %i %02x:%02x:%02x:%02x:%02x:%02x",
102 // i, card->IOBase, card->IRQ,
103 // card->MacAddr[0], card->MacAddr[1], card->MacAddr[2],
104 // card->MacAddr[3], card->MacAddr[4], card->MacAddr[5]
110 gRhine2_DriverInfo.RootNode.Size = giRhine2_CardCount;
111 DevFS_AddDevice( &gRhine2_DriverInfo );
113 return MODULE_ERR_OK;
116 // --- Root Functions ---
117 char *Rhine2_ReadDir(tVFS_Node *Node, int Pos)
119 if( Pos < 0 || Pos >= giRhine2_CardCount ) return NULL;
121 return strdup( gaRhine2_Cards[Pos].Name );
124 tVFS_Node *Rhine2_FindDir(tVFS_Node *Node, const char *Filename)
126 //TODO: It might be an idea to supprt >10 cards
127 if(Filename[0] == '\0' || Filename[1] != '\0') return NULL;
128 if(Filename[0] < '0' || Filename[0] > '9') return NULL;
129 return &gaRhine2_Cards[ Filename[0]-'0' ].Node;
132 const char *csaRhine2_RootIOCtls[] = {DRV_IOCTLNAMES, NULL};
133 int Rhine2_RootIOCtl(tVFS_Node *Node, int ID, void *Data)
135 ENTER("pNode iID pData", Node, ID, Data);
138 BASE_IOCTLS(DRV_TYPE_NETWORK, "Rhine2", VERSION, csaRhine2_RootIOCtls);
144 // --- File Functions ---
145 Uint64 Rhine2_Read(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
148 tCard *card = Node->ImplPtr;
149 Uint16 read_ofs, pkt_length;
152 ENTER("pNode XOffset XLength pBuffer", Node, Offset, Length, Buffer);
155 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
160 Mutex_Acquire( &card->ReadMutex );
162 read_ofs = inw( card->IOBase + CAPR );
163 LOG("raw read_ofs = %i", read_ofs);
164 read_ofs = (read_ofs + 0x10) & 0xFFFF;
165 LOG("read_ofs = %i", read_ofs);
167 pkt_length = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
169 // Calculate new read offset
170 new_read_ofs = read_ofs + pkt_length + 4;
171 new_read_ofs = (new_read_ofs + 3) & ~3; // Align
172 if(new_read_ofs > card->ReceiveBufferLength) {
173 LOG("wrapping read_ofs");
174 new_read_ofs -= card->ReceiveBufferLength;
176 new_read_ofs -= 0x10; // I dunno
177 LOG("new_read_ofs = %i", new_read_ofs);
180 if( *(Uint16*)&card->ReceiveBuffer[read_ofs] & 0x1E ) {
182 outw(card->IOBase + CAPR, new_read_ofs);
183 Mutex_Release( &card->ReadMutex );
184 goto retry; // I feel evil
188 if( Length > pkt_length ) Length = pkt_length;
189 memcpy(Buffer, &card->ReceiveBuffer[read_ofs+4], Length);
192 outw(card->IOBase + CAPR, new_read_ofs);
194 Mutex_Release( &card->ReadMutex );
202 Uint64 Rhine2_Write(tVFS_Node *Node, Uint64 Offset, Uint64 Length, void *Buffer)
207 tCard *card = Node->ImplPtr;
209 if( Length > 1500 ) return 0; // MTU exceeded
211 ENTER("pNode XLength pBuffer", Node, Length, Buffer);
213 // TODO: Implement a semaphore for avaliable transmit buffers
215 // Find an avaliable descriptor
216 Mutex_Acquire(&card->CurTXProtector);
217 td = card->CurTXDescriptor;
218 card->CurTXDescriptor ++;
219 card->CurTXDescriptor %= 4;
220 Mutex_Release(&card->CurTXProtector);
222 Mutex_Acquire( &card->TransmitInUse[td] );
226 // Transmit using descriptor `td`
227 LOG("card->PhysTransmitBuffers[td] = %P", card->PhysTransmitBuffers[td]);
228 outd(card->IOBase + TSAD0 + td*4, card->PhysTransmitBuffers[td]);
229 LOG("card->TransmitBuffers[td] = %p", card->TransmitBuffers[td]);
231 memcpy(card->TransmitBuffers[td], Buffer, Length);
234 status |= Length & 0x1FFF; // 0-12: Length
235 status |= 0 << 13; // 13: OWN bit
236 status |= (0 & 0x3F) << 16; // 16-21: Early TX threshold (zero atm, TODO: check)
237 LOG("status = 0x%08x", status);
238 outd(card->IOBase + TSD0 + td*4, status);
240 LEAVE('i', (int)Length);
246 const char *csaRhine2_NodeIOCtls[] = {DRV_IOCTLNAMES, NULL};
247 int Rhine2_IOCtl(tVFS_Node *Node, int ID, void *Data)
249 tCard *card = Node->ImplPtr;
250 ENTER("pNode iID pData", Node, ID, Data);
253 BASE_IOCTLS(DRV_TYPE_NETWORK, "Rhine2", VERSION, csaRhine2_NodeIOCtls);
254 case NET_IOCTL_GETMAC:
255 if( !CheckMem(Data, 6) ) {
259 memcpy( Data, card->MacAddr, 6 );
267 void Rhine2_IRQHandler(int Num)
274 LOG("Num = %i", Num);
276 for( i = 0; i < giRhine2_CardCount; i ++ )
278 card = &gaRhine2_Cards[i];
279 if( Num != card->IRQ ) break;
281 status = inw(card->IOBase + ISR);
282 LOG("status = 0x%02x", status);
284 // Transmit OK, a transmit descriptor is now free
285 if( status & FLAG_ISR_TOK )
287 for( j = 0; j < 4; j ++ )
289 if( ind(card->IOBase + TSD0 + j*4) & 0x8000 ) { // TSD TOK
290 Mutex_Release( &card->TransmitInUse[j] );
291 // TODO: Update semaphore once implemented
294 outw(card->IOBase + ISR, FLAG_ISR_TOK);
297 // Recieve OK, inform read
298 if( status & FLAG_ISR_ROK )
300 int read_ofs, end_ofs;
301 int packet_count = 0;
304 // Scan recieve buffer for packets
305 end_ofs = inw(card->IOBase + CBA);
306 read_ofs = card->SeenOfs;
307 LOG("read_ofs = %i, end_ofs = %i", read_ofs, end_ofs);
308 if( read_ofs > end_ofs )
310 while( read_ofs < card->ReceiveBufferLength )
313 len = *(Uint16*)&card->ReceiveBuffer[read_ofs+2];
314 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
315 packet_count, read_ofs,
316 *(Uint16*)&card->ReceiveBuffer[read_ofs],
320 Log_Warning("PCnet3", "IRQ: Packet in buffer exceeds sanity (%i>2000)", len);
323 read_ofs = (read_ofs + 3) & ~3; // Align
325 read_ofs -= card->ReceiveBufferLength;
326 LOG("wrapped read_ofs");
328 while( read_ofs < end_ofs )
331 LOG("%i 0x%x Pkt Hdr: 0x%04x, len: 0x%04x",
332 packet_count, read_ofs,
333 *(Uint16*)&card->ReceiveBuffer[read_ofs],
334 *(Uint16*)&card->ReceiveBuffer[read_ofs+2]
336 read_ofs += *(Uint16*)&card->ReceiveBuffer[read_ofs+2] + 4;
337 read_ofs = (read_ofs + 3) & ~3; // Align
339 if( read_ofs != end_ofs ) {
340 Log_Warning("PCnet3", "IRQ: read_ofs (%i) != end_ofs(%i)", read_ofs, end_ofs);
343 card->SeenOfs = read_ofs;
345 LOG("packet_count = %i, read_ofs = 0x%x", packet_count, read_ofs);
349 if( Semaphore_Signal( &card->ReadSemaphore, packet_count ) != packet_count ) {
352 VFS_MarkAvaliable( &card->Node, 1 );
355 outw(card->IOBase + ISR, FLAG_ISR_ROK);