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>
15 #define RX_BUF_SIZE 1024
16 #define N_RX_PAGES ((N_RX_DESCS*RX_BUF_SIZE)/PAGE_SIZE)
17 #define N_TX_DESCS ((PAGE_SIZE/16)-N_RX_DESCS)
20 #define VENDOR_ID 0x1106
21 #define DEVICE_ID 0x3065
29 tSemaphore ReadSemaphore;
34 } RXBuffers[N_RX_PAGES];
39 struct sTXDesc *TXDescs;
41 struct sTXDesc *FirstTX;
42 struct sTXDesc *LastTX; // Most recent unsent packet
44 struct sRXDesc *FirstRX; // Most recent unread packet
45 struct sRXDesc *LastRX; // End of RX descriptor queue
52 int Rhine2_Install(char **Options);
53 void Rhine2_int_InitialiseCard(tCard *Card);
54 tIPStackBuffer *Rhine2_WaitPacket(void *Ptr);
55 int Rhine2_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
56 void Rhine2_IRQHandler(int Num, void *Pt);
58 struct sRXDesc *Rhine2_int_GetDescFromPhys(tCard *Card, Uint32 Addr);
59 void *Rhine2_int_GetBufferFromPhys(tCard *Card, Uint32 Addr);
60 void Rhine2_int_FreeRXDesc(void *Desc, size_t, size_t, const void*);
61 struct sTXDesc *Rhine2_int_AllocTXDesc(tCard *Card);
64 MODULE_DEFINE(0, VERSION, VIARhineII, Rhine2_Install, NULL, NULL);
65 tIPStack_AdapterType gRhine2_AdapterType = {
66 .Name = "VIA Rhine II",
67 .SendPacket = Rhine2_SendPacket,
68 .WaitForPacket = Rhine2_WaitPacket,
70 int giRhine2_CardCount;
71 tCard *gaRhine2_Cards;
75 * \brief Initialises the driver
77 int Rhine2_Install(char **Options)
84 giRhine2_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
85 Log_Debug("Rhine2", "%i cards", giRhine2_CardCount);
87 if( giRhine2_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
89 gaRhine2_Cards = calloc( giRhine2_CardCount, sizeof(tCard) );
91 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
93 card = &gaRhine2_Cards[i];
95 LOG("BAR0 = 0x%08x", PCI_GetBAR(id, 0));
96 LOG("BAR1 = 0x%08x", PCI_GetBAR(id, 1));
97 LOG("BAR2 = 0x%08x", PCI_GetBAR(id, 2));
98 LOG("BAR3 = 0x%08x", PCI_GetBAR(id, 3));
99 LOG("BAR4 = 0x%08x", PCI_GetBAR(id, 4));
100 LOG("BAR5 = 0x%08x", PCI_GetBAR(id, 5));
102 card->IOBase = PCI_GetBAR(id, 0);
103 if( !(card->IOBase & 1) ) {
105 Log_Warning("Rhine2", "BAR0 is not in IO space");
109 card->IRQ = PCI_GetIRQ( id );
111 // Install IRQ Handler
112 IRQ_AddHandler(card->IRQ, Rhine2_IRQHandler, card);
114 Rhine2_int_InitialiseCard(card);
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 outb(Card->IOBase + REG_CR1, CR1_SFRST);
141 while( inb(Card->IOBase + REG_CR1) & CR1_SFRST ) ;
143 // Allocate memory for things
144 for( int i = 0; i < N_RX_PAGES; i ++ )
146 Card->RXBuffers[i].Virt = (void*)MM_AllocDMA(1, 32, &phys);
147 Card->RXBuffers[i].Phys = phys;
150 Card->DescTable = (void*)MM_AllocDMA(1, 32, &phys);
151 Card->DescTablePhys = phys;
153 // Initialise RX Descriptors
154 struct sRXDesc *rxdescs = Card->DescTable;
155 for( int i = 0; i < N_RX_DESCS; i ++ )
158 rxdescs[i].BufferSize = RX_BUF_SIZE;
159 rxdescs[i].RXBufferStart = Card->RXBuffers[i/(PAGE_SIZE/RX_BUF_SIZE)].Phys
160 + (i % (PAGE_SIZE/RX_BUF_SIZE)) * RX_BUF_SIZE;
161 rxdescs[i].RDBranchAddress = Card->DescTablePhys + (i+1) * sizeof(struct sRXDesc);
162 rxdescs[i].Length = (1 << 15); // set OWN
164 rxdescs[ N_RX_DESCS - 1 ].RDBranchAddress = 0;
165 Card->FirstRX = &rxdescs[0];
166 Card->LastRX = &rxdescs[N_RX_DESCS - 1];
168 Card->TXDescs = (void*)(rxdescs + N_RX_DESCS);
169 memset(Card->TXDescs, 0, sizeof(struct sTXDesc)*N_TX_DESCS);
172 // - Initialise card state
173 outb(Card->IOBase + REG_IMR0, 0xFF);
174 outb(Card->IOBase + REG_IMR1, 0xFF);
175 outd(Card->IOBase + REG_CUR_RX_DESC, Card->DescTablePhys);
177 outb(Card->IOBase + REG_CR0, CR0_STRT|CR0_RXON);
180 // --- File Functions ---
181 tIPStackBuffer *Rhine2_WaitPacket(void *Ptr)
185 struct sRXDesc *desc;
190 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
197 desc = card->FirstRX;
198 while( desc->BufferSize & (1 << 15) )
200 desc = Rhine2_int_GetDescFromPhys(card, desc->RDBranchAddress);
204 LOG("%i descriptors in packet", nDesc);
206 ret = IPStack_Buffer_CreateBuffer(nDesc);
207 desc = card->FirstRX;
208 while( desc->BufferSize & (1 << 15) )
210 void *data = Rhine2_int_GetBufferFromPhys(card, desc->RXBufferStart);
211 IPStack_Buffer_AppendSubBuffer(ret,
212 desc->Length, 0, data,
213 Rhine2_int_FreeRXDesc, desc
215 desc = Rhine2_int_GetDescFromPhys(card, desc->RDBranchAddress);
217 card->FirstRX = desc;
223 int Rhine2_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
228 struct sTXDesc *first_desc = NULL;
229 struct sTXDesc *last_desc = NULL;
231 ENTER("pPtr pBuffer", Ptr, Buffer);
234 for( int id = -1; -1 != (id = IPStack_Buffer_GetBuffer(Buffer, id, &len, &data)); )
236 tPAddr pdata = MM_GetPhysAddr( (tVAddr)data );
237 struct sTXDesc *desc;
244 desc = Rhine2_int_AllocTXDesc(card);
248 last_desc->TDBranchAddress = MM_GetPhysAddr( (tVAddr)desc );
250 desc->TXBufferStart = pdata;
251 desc->BufferSize = len;
254 desc->TSR = TD_TSR_OWN;
255 desc->TDBranchAddress = 0;
265 first_desc->TCR |= TD_TCR_STP;
266 last_desc->TCR |= TD_TCR_EDP;
269 card->LastTX->TDBranchAddress = MM_GetPhysAddr( (tVAddr)first_desc );
271 card->FirstTX = first_desc;
272 card->LastTX = first_desc;
273 outd(card->IOBase + REG_CUR_TX_DESC, MM_GetPhysAddr( (tVAddr)first_desc ));
276 outb(card->IOBase + REG_CR1, CR1_TDMD);
278 // TODO: Wait until the packet has sent, then clean up
279 while( inb(card->IOBase + REG_CR1) & CR1_TDMD )
285 void Rhine2_IRQHandler(int Num, void *Ptr)
291 struct sRXDesc *Rhine2_int_GetDescFromPhys(tCard *Card, Uint32 Addr)
293 if( Card->DescTablePhys > Addr ) return NULL;
294 if( Card->DescTablePhys + PAGE_SIZE <= Addr ) return NULL;
295 if( Addr & 15 ) return NULL;
296 return (struct sRXDesc*)Card->DescTable + ((Addr & (PAGE_SIZE-1)) / 16);
299 void *Rhine2_int_GetBufferFromPhys(tCard *Card, Uint32 Addr)
301 for( int i = 0; i < N_RX_PAGES; i ++ )
303 if( Card->RXBuffers[i].Phys > Addr ) continue;
304 if( Card->RXBuffers[i].Phys + PAGE_SIZE <= Addr ) continue;
305 return Card->RXBuffers[i].Virt + (Addr & (PAGE_SIZE-1));
310 void Rhine2_int_FreeRXDesc(void *Ptr, size_t u1, size_t u2, const void *u3)
312 struct sRXDesc *desc = Ptr;
315 desc->Length = (1 << 15); // Reset OWN
318 struct sTXDesc *Rhine2_int_AllocTXDesc(tCard *Card)
320 for( int i = 0; i < N_TX_DESCS; i ++ )
322 if( Card->TXDescs[i].TSR & TD_TSR_OWN ) continue ;
323 Card->TXDescs[i].TSR = TD_TSR_OWN;
324 return &Card->TXDescs[i];