2 * Acess2 VIA Rhine II Driver (VT6102)
3 * - By John Hodge (thePowersGang)
6 #define VERSION ((0<<8)|10)
10 #include <semaphore.h>
11 #include "rhine2_hw.h"
12 #include <IPStack/include/adapters_api.h>
16 #define RX_BUF_SIZE 1024
17 #define N_RX_BUF_PER_PAGE (PAGE_SIZE/RX_BUF_SIZE)
18 #define N_RX_BUF_PAGES ((N_RX_DESCS*RX_BUF_SIZE)/PAGE_SIZE)
19 #define N_TX_DESCS ((PAGE_SIZE/DESC_SIZE)-N_RX_DESCS)
21 #define CR0_BASEVAL (CR0_STRT|CR0_TXON|CR0_RXON)
24 #define VENDOR_ID 0x1106
25 #define DEVICE_ID 0x3065
33 tSemaphore ReadSemaphore;
34 tSemaphore SendSemaphore;
39 } RXBuffers[N_RX_BUF_PAGES];
44 struct sTXDesc *TXDescs;
48 struct sRXDesc *NextRX; // Most recent unread packet
55 int Rhine2_Install(char **Options);
56 void Rhine2_int_InitialiseCard(tCard *Card);
57 tIPStackBuffer *Rhine2_WaitPacket(void *Ptr);
58 int Rhine2_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
59 void Rhine2_IRQHandler(int Num, void *Pt);
61 struct sRXDesc *Rhine2_int_GetDescFromPhys(tCard *Card, Uint32 Addr);
62 void *Rhine2_int_GetBufferFromPhys(tCard *Card, Uint32 Addr);
63 void Rhine2_int_FreeRXDesc(void *Desc, size_t, size_t, const void*);
66 MODULE_DEFINE(0, VERSION, VIARhineII, Rhine2_Install, NULL, NULL);
67 tIPStack_AdapterType gRhine2_AdapterType = {
68 .Name = "VIA Rhine II",
69 .SendPacket = Rhine2_SendPacket,
70 .WaitForPacket = Rhine2_WaitPacket,
72 int giRhine2_CardCount;
73 tCard *gaRhine2_Cards;
77 * \brief Initialises the driver
79 int Rhine2_Install(char **Options)
84 giRhine2_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
85 if( giRhine2_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
87 gaRhine2_Cards = calloc( giRhine2_CardCount, sizeof(tCard) );
89 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
91 tCard *card = &gaRhine2_Cards[i];
93 LOG("BAR0 = 0x%08x", PCI_GetBAR(id, 0));
94 LOG("BAR1 = 0x%08x", PCI_GetBAR(id, 1));
95 LOG("BAR2 = 0x%08x", PCI_GetBAR(id, 2));
96 LOG("BAR3 = 0x%08x", PCI_GetBAR(id, 3));
97 LOG("BAR4 = 0x%08x", PCI_GetBAR(id, 4));
98 LOG("BAR5 = 0x%08x", PCI_GetBAR(id, 5));
100 card->IOBase = PCI_GetBAR(id, 0);
101 if( !(card->IOBase & 1) ) {
103 Log_Warning("Rhine2", "BAR0 is not in IO space");
107 card->IRQ = PCI_GetIRQ( id );
109 // Install IRQ Handler
110 IRQ_AddHandler(card->IRQ, Rhine2_IRQHandler, card);
112 Rhine2_int_InitialiseCard(card);
114 card->IPHandle = IPStack_Adapter_Add(&gRhine2_AdapterType, card, card->MacAddr);
116 Log_Log("Rhine2", "Card %i 0x%04x, IRQ %i %02x:%02x:%02x:%02x:%02x:%02x",
117 i, card->IOBase, card->IRQ,
118 card->MacAddr[0], card->MacAddr[1], card->MacAddr[2],
119 card->MacAddr[3], card->MacAddr[4], card->MacAddr[5]
125 return MODULE_ERR_OK;
128 void Rhine2_int_InitialiseCard(tCard *Card)
132 Card->MacAddr[0] = inb(Card->IOBase + REG_PAR0);
133 Card->MacAddr[1] = inb(Card->IOBase + REG_PAR1);
134 Card->MacAddr[2] = inb(Card->IOBase + REG_PAR2);
135 Card->MacAddr[3] = inb(Card->IOBase + REG_PAR3);
136 Card->MacAddr[4] = inb(Card->IOBase + REG_PAR4);
137 Card->MacAddr[5] = inb(Card->IOBase + REG_PAR5);
139 LOG("Resetting card");
140 outb(Card->IOBase + REG_CR1, CR1_SFRST);
142 while( inb(Card->IOBase + REG_CR1) & CR1_SFRST ) ;
144 LOG("Allocaating RX buffers");
145 // Allocate memory for things
146 for( int i = 0; i < N_RX_BUF_PAGES; i ++ )
148 Card->RXBuffers[i].Virt = (void*)MM_AllocDMA(1, 32, &phys);
149 Card->RXBuffers[i].Phys = phys;
152 LOG("Allocating and filling RX/TX Descriptors");
153 Card->DescTable = (void*)MM_AllocDMA(1, 32, &phys);
154 Card->DescTablePhys = phys;
156 // Initialise RX Descriptors
157 struct sRXDesc *rxdescs = Card->DescTable;
158 for( int i = 0; i < N_RX_DESCS; i ++ )
161 rxdescs[i].BufferSize = RX_BUF_SIZE;
162 rxdescs[i].RXBufferStart = Card->RXBuffers[i/N_RX_BUF_PER_PAGE].Phys
163 + (i % N_RX_BUF_PER_PAGE) * RX_BUF_SIZE;
164 rxdescs[i].RDBranchAddress = Card->DescTablePhys + (i+1) * DESC_SIZE;
165 rxdescs[i].Length = (1 << 15); // set OWN
166 LOG("RX Desc %p = {Buf:0x%8x, Next:0x%x}", rxdescs,
167 rxdescs[i].RXBufferStart, rxdescs[i].RDBranchAddress
170 rxdescs[ N_RX_DESCS - 1 ].RDBranchAddress = Card->DescTablePhys;
171 Card->NextRX = &rxdescs[0];
173 Card->TXDescs = (void*)(rxdescs + N_RX_DESCS);
174 memset(Card->TXDescs, 0, sizeof(struct sTXDesc)*N_TX_DESCS);
175 for( int i = 0; i < N_TX_DESCS; i ++ )
177 Card->TXDescs[i].TDBranchAddress = Card->DescTablePhys + (N_RX_DESCS + i + 1) * DESC_SIZE;
179 Card->TXDescs[N_TX_DESCS-1].TDBranchAddress = Card->DescTablePhys + N_RX_DESCS * DESC_SIZE;
180 Card->nFreeTX = N_TX_DESCS;
183 // - Initialise card state
184 LOG("Initialising card state");
185 outb(Card->IOBase + REG_IMR0, 0xFF);
186 outb(Card->IOBase + REG_IMR1, 0xFF);
187 outd(Card->IOBase + REG_CUR_RX_DESC, Card->DescTablePhys);
188 outd(Card->IOBase + REG_CUR_TX_DESC, Card->DescTablePhys + N_RX_DESCS * DESC_SIZE);
190 outb(Card->IOBase + REG_TCR, TCR_TRSF(4));
193 outb(Card->IOBase + REG_CR0, CR0_BASEVAL);
194 outb(Card->IOBase + REG_CR1, 0); // Disabled TX polling only?
196 LOG("ISR state: %02x %02x", inb(Card->IOBase + REG_ISR0), inb(Card->IOBase + REG_ISR1));
199 // --- File Functions ---
200 tIPStackBuffer *Rhine2_WaitPacket(void *Ptr)
203 struct sRXDesc *desc;
208 LOG("CR0 state: %02x", inb(card->IOBase + REG_CR0));
209 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
217 while( !(desc->Length & (1 << 15)) )
219 // LOG("desc(%p) = {RSR:%04x,Length:%04x,BufferSize:%04x,RXBufferStart:%08x,RDBranchAddress:%08x}",
220 // desc, desc->RSR, desc->Length, desc->BufferSize, desc->RXBufferStart, desc->RDBranchAddress);
221 // TODO: This kinda expensive call can be changed for an increment, but cbf
222 desc = Rhine2_int_GetDescFromPhys(card, desc->RDBranchAddress);
226 LOG("%i descriptors in packet", nDesc);
228 tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(nDesc);
230 while( !(desc->Length & (1 << 15)) )
232 void *data = Rhine2_int_GetBufferFromPhys(card, desc->RXBufferStart);
233 IPStack_Buffer_AppendSubBuffer(ret,
234 desc->Length, 0, data,
235 Rhine2_int_FreeRXDesc, desc
237 desc = Rhine2_int_GetDescFromPhys(card, desc->RDBranchAddress);
245 int Rhine2_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
250 int nDescs, first_desc_id;
251 struct sTXDesc *desc = NULL;
252 struct sTXDesc *first_desc = NULL;
253 struct sTXDesc *last_desc = NULL;
255 ENTER("pPtr pBuffer", Ptr, Buffer);
260 for( int id = -1; -1 != (id = IPStack_Buffer_GetBuffer(Buffer, id, &len, &data)); )
262 if( ((tVAddr)data & (PAGE_SIZE-1)) + len > PAGE_SIZE )
271 LOG("%i descriptors needed", nDescs);
273 if( card->nFreeTX < nDescs ) {
275 // TODO: Semaphore instead?
280 first_desc_id = card->NextTX;
281 card->NextTX = (card->NextTX + nDescs) % N_TX_DESCS;
283 desc = card->TXDescs + first_desc_id;
286 for( int id = -1; -1 != (id = IPStack_Buffer_GetBuffer(Buffer, id, &len, &data)); )
288 tPAddr pdata = MM_GetPhysAddr( (tVAddr)data );
292 Log_Warning("Rhine2", "TODO: Bounce-buffer >32 pbit buffers");
296 if( (pdata & (PAGE_SIZE-1)) + len > PAGE_SIZE )
298 // Need to split into to descriptors
299 Log_Warning("Rhine2", "TODO: Split cross-page buffers");
302 LOG("Buffer %i (%p+0x%x) placed in %p", id-1, data, len, desc);
304 // TODO: Rhine I requires 4-byte alignment (fsck that, I don't have one)
306 desc->TXBufferStart = pdata;
307 desc->BufferSize = len | (1 << 15);
311 desc->TSR = 0; // OWN will be set below
313 desc->TSR = TD_TSR_OWN;
316 if(first_desc_id + nDescs == N_TX_DESCS)
317 desc = card->TXDescs;
322 data = IPStack_Buffer_CompactBuffer(Buffer, &len);
325 first_desc_id = card->NextTX;
326 card->NextTX = (card->NextTX + nDescs) % N_TX_DESCS;
327 desc = card->TXDescs + first_desc_id;
329 desc->TXBufferStart = MM_GetPhysAddr( data );
330 desc->BufferSize = len | (1 << 15);
335 first_desc = card->TXDescs + first_desc_id;
338 first_desc->TCR |= TD_TCR_STP;
339 last_desc->TCR |= TD_TCR_EDP|TD_TCR_IC;
340 // last_desc->BufferSize &= ~(1 << 15);
342 first_desc->TSR |= TD_TSR_OWN;
344 LOG("%i descriptors allocated, first = %p, last = %p", nDescs, first_desc, last_desc);
347 LOG("Waiting for TX to complete");
348 outb(card->IOBase + REG_CR0, CR0_TDMD|CR0_BASEVAL);
349 Semaphore_Wait(&card->SendSemaphore, 1);
359 void Rhine2_IRQHandler(int Num, void *Ptr)
362 Uint8 isr0 = inb(card->IOBase + REG_ISR0);
363 Uint8 isr1 = inb(card->IOBase + REG_ISR1);
365 if( isr0 == 0 ) return ;
367 LOG("ISR0 = 0x%02x, ISR1 = 0x%02x", isr0, isr1);
369 if( isr0 & ISR0_PRX )
372 Semaphore_Signal(&card->ReadSemaphore, 1);
375 if( isr0 & ISR0_PTX )
378 Semaphore_Signal(&card->SendSemaphore, 1);
381 if( isr0 & ISR0_TXE )
383 LOG("TX Error... oops");
384 Semaphore_Signal(&card->SendSemaphore, 1);
389 LOG("Transmit buffer underflow");
392 LOG("Acking interrupts");
393 outb(card->IOBase + REG_ISR0, isr0);
394 outb(card->IOBase + REG_ISR1, isr1);
398 struct sRXDesc *Rhine2_int_GetDescFromPhys(tCard *Card, Uint32 Addr)
400 if( Card->DescTablePhys > Addr ) return NULL;
401 if( Card->DescTablePhys + PAGE_SIZE <= Addr ) return NULL;
402 if( Addr & 15 ) return NULL;
403 return (struct sRXDesc*)Card->DescTable + ((Addr & (PAGE_SIZE-1)) / 16);
406 void *Rhine2_int_GetBufferFromPhys(tCard *Card, Uint32 Addr)
408 for( int i = 0; i < N_RX_BUF_PAGES; i ++ )
410 if( Card->RXBuffers[i].Phys > Addr ) continue;
411 if( Card->RXBuffers[i].Phys + PAGE_SIZE <= Addr ) continue;
412 return Card->RXBuffers[i].Virt + (Addr & (PAGE_SIZE-1));
417 void Rhine2_int_FreeRXDesc(void *Ptr, size_t u1, size_t u2, const void *u3)
419 struct sRXDesc *desc = Ptr;
421 LOG("Descriptor %p returned to card", desc);
424 desc->Length = (1 << 15); // Reset OWN