Kernel/IPCPipe - Add queue length limit, watch for _ERROR in select
[tpg/acess2.git] / KernelLand / Modules / Network / VIARhineII / rhine2.c
1 /*
2  * Acess2 VIA Rhine II Driver (VT6102)
3  * - By John Hodge (thePowersGang)
4  */
5 #define DEBUG   0
6 #define VERSION ((0<<8)|10)
7 #include <acess.h>
8 #include <modules.h>
9 #include <drv_pci.h>
10 #include <semaphore.h>
11 #include "rhine2_hw.h"
12 #include <IPStack/include/adapters_api.h>
13
14 #define DESC_SIZE       16
15 #define N_RX_DESCS      16
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)
20
21 #define CR0_BASEVAL     (CR0_STRT|CR0_TXON|CR0_RXON)
22
23 // === CONSTANTS ===
24 #define VENDOR_ID       0x1106
25 #define DEVICE_ID       0x3065
26
27 // === TYPES ===
28 typedef struct sCard
29 {
30         Uint16  IOBase;
31         Uint8   IRQ;
32         
33         tSemaphore      ReadSemaphore;
34         tSemaphore      SendSemaphore;
35
36         struct {
37                 Uint32  Phys;
38                 void    *Virt;
39         } RXBuffers[N_RX_BUF_PAGES];
40
41         Uint32  DescTablePhys;
42         void    *DescTable;
43
44         struct sTXDesc  *TXDescs;
45          int    NextTX;
46          int    nFreeTX;
47         
48         struct sRXDesc  *NextRX;        // Most recent unread packet
49
50         void    *IPHandle;      
51         Uint8   MacAddr[6];
52 }       tCard;
53
54 // === PROTOTYPES ===
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);
60 // --- Helpers ---
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*);
64
65 // === GLOBALS ===
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,
71 };
72  int    giRhine2_CardCount;
73 tCard   *gaRhine2_Cards;
74
75 // === CODE ===
76 /**
77  * \brief Initialises the driver
78  */
79 int Rhine2_Install(char **Options)
80 {
81          int    id = -1;
82          int    i = 0;
83         
84         giRhine2_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
85         if( giRhine2_CardCount == 0 )   return MODULE_ERR_NOTNEEDED;
86         
87         gaRhine2_Cards = calloc( giRhine2_CardCount, sizeof(tCard) );
88         
89         while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
90         {
91                 tCard   *card = &gaRhine2_Cards[i];
92                 
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));
99                 
100                 card->IOBase = PCI_GetBAR(id, 0);
101                 if( !(card->IOBase & 1) ) {
102                         // Oops?
103                         Log_Warning("Rhine2", "BAR0 is not in IO space");
104                         continue ;
105                 }
106                 card->IOBase &= ~1;
107                 card->IRQ = PCI_GetIRQ( id );
108                 
109                 // Install IRQ Handler
110                 IRQ_AddHandler(card->IRQ, Rhine2_IRQHandler, card);
111                 
112                 Rhine2_int_InitialiseCard(card);
113         
114                 card->IPHandle = IPStack_Adapter_Add(&gRhine2_AdapterType, card, card->MacAddr);
115         
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]
120                         );
121                 
122                 i ++;
123         }
124         
125         return MODULE_ERR_OK;
126 }
127
128 void Rhine2_int_InitialiseCard(tCard *Card)
129 {
130         tPAddr  phys;
131         
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);
138
139         LOG("Resetting card");  
140         outb(Card->IOBase + REG_CR1, CR1_SFRST);
141         // TODO: Timeout
142         while( inb(Card->IOBase + REG_CR1) & CR1_SFRST ) ;
143         
144         LOG("Allocaating RX buffers");
145         // Allocate memory for things
146         for( int i = 0; i < N_RX_BUF_PAGES; i ++ )
147         {
148                 Card->RXBuffers[i].Virt = (void*)MM_AllocDMA(1, 32, &phys);
149                 Card->RXBuffers[i].Phys = phys;
150         }
151         
152         LOG("Allocating and filling RX/TX Descriptors");
153         Card->DescTable = (void*)MM_AllocDMA(1, 32, &phys);
154         Card->DescTablePhys = phys;
155
156         // Initialise RX Descriptors
157         struct sRXDesc  *rxdescs = Card->DescTable;
158         for( int i = 0; i < N_RX_DESCS; i ++ )
159         {
160                 rxdescs[i].RSR = 0;
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
168                         );
169         }
170         rxdescs[ N_RX_DESCS - 1 ].RDBranchAddress = Card->DescTablePhys;
171         Card->NextRX = &rxdescs[0];
172
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 ++ )
176         {
177                 Card->TXDescs[i].TDBranchAddress = Card->DescTablePhys + (N_RX_DESCS + i + 1) * DESC_SIZE;
178         }
179         Card->TXDescs[N_TX_DESCS-1].TDBranchAddress = Card->DescTablePhys + N_RX_DESCS * DESC_SIZE;
180         Card->nFreeTX = N_TX_DESCS;
181         Card->NextTX = 0;
182         
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);
189
190         outb(Card->IOBase + REG_TCR, TCR_TRSF(4));
191         
192         LOG("RX started");
193         outb(Card->IOBase + REG_CR0, CR0_BASEVAL);
194         outb(Card->IOBase + REG_CR1, 0);        // Disabled TX polling only?
195         
196         LOG("ISR state: %02x %02x", inb(Card->IOBase + REG_ISR0), inb(Card->IOBase + REG_ISR1));
197 }
198
199 // --- File Functions ---
200 tIPStackBuffer *Rhine2_WaitPacket(void *Ptr)
201 {
202         tCard   *card = Ptr;
203         struct sRXDesc  *desc;
204          int    nDesc;
205         
206         ENTER("pPtr", Ptr);
207
208         LOG("CR0 state: %02x", inb(card->IOBase + REG_CR0));
209         if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
210         {
211                 LEAVE('n');
212                 return NULL;
213         }
214         
215         nDesc = 0;
216         desc = card->NextRX;
217         while( !(desc->Length & (1 << 15)) )
218         {
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);
223                 nDesc ++;
224         }
225
226         LOG("%i descriptors in packet", nDesc);
227
228         tIPStackBuffer  *ret = IPStack_Buffer_CreateBuffer(nDesc);
229         desc = card->NextRX;
230         while( !(desc->Length & (1 << 15)) )
231         {
232                 void    *data = Rhine2_int_GetBufferFromPhys(card, desc->RXBufferStart);
233                 IPStack_Buffer_AppendSubBuffer(ret,
234                         desc->Length, 0, data,
235                         Rhine2_int_FreeRXDesc, desc
236                         );
237                 desc = Rhine2_int_GetDescFromPhys(card, desc->RDBranchAddress);
238         }       
239         card->NextRX = desc;
240
241         LEAVE('p', ret);
242         return ret;
243 }
244
245 int Rhine2_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
246 {
247         tCard   *card = Ptr;
248         size_t  len;
249         const void      *data;
250          int    nDescs, first_desc_id;
251         struct sTXDesc  *desc = NULL;
252         struct sTXDesc  *first_desc = NULL;
253         struct sTXDesc  *last_desc = NULL;
254
255         ENTER("pPtr pBuffer", Ptr, Buffer);     
256
257         #if 0
258         // Iterate buffers
259         nDescs = 0;
260         for( int id = -1; -1 != (id = IPStack_Buffer_GetBuffer(Buffer, id, &len, &data)); )
261         {
262                 if( ((tVAddr)data & (PAGE_SIZE-1)) + len > PAGE_SIZE )
263                         nDescs ++;
264                 nDescs ++;
265         }
266         if( nDescs == 0 ) {
267                 LEAVE('i', -1);
268                 return -1;
269         }
270
271         LOG("%i descriptors needed", nDescs);
272         
273         if( card->nFreeTX < nDescs ) {
274                 // Oops... wait?
275                 // TODO: Semaphore instead?
276                 LEAVE('i', 1);
277                 return 1;
278         }
279
280         first_desc_id = card->NextTX;
281         card->NextTX = (card->NextTX + nDescs) % N_TX_DESCS;
282
283         desc = card->TXDescs + first_desc_id;
284         
285         nDescs = 0;
286         for( int id = -1; -1 != (id = IPStack_Buffer_GetBuffer(Buffer, id, &len, &data)); )
287         {
288                 tPAddr  pdata = MM_GetPhysAddr( (tVAddr)data );
289                 #if PHYS_BITS > 32
290                 if( pdata >> 32 ) {
291                         // TODO: re-map
292                         Log_Warning("Rhine2", "TODO: Bounce-buffer >32 pbit buffers");
293                 } 
294                 #endif
295         
296                 if( (pdata & (PAGE_SIZE-1)) + len > PAGE_SIZE )
297                 {
298                         // Need to split into to descriptors
299                         Log_Warning("Rhine2", "TODO: Split cross-page buffers");
300                 }
301                 
302                 LOG("Buffer %i (%p+0x%x) placed in %p", id-1, data, len, desc);
303
304                 // TODO: Rhine I requires 4-byte alignment (fsck that, I don't have one)
305         
306                 desc->TXBufferStart = pdata;
307                 desc->BufferSize = len | (1 << 15);
308                 // TODO: TCR
309                 desc->TCR = 0;
310                 if( nDescs == 0 )
311                         desc->TSR = 0;  // OWN will be set below
312                 else
313                         desc->TSR = TD_TSR_OWN;
314
315                 nDescs ++;
316                 if(first_desc_id + nDescs == N_TX_DESCS)
317                         desc = card->TXDescs;
318                 else
319                         desc ++;
320         }
321         #else
322         data = IPStack_Buffer_CompactBuffer(Buffer, &len);
323         
324         nDescs = 1;
325         first_desc_id = card->NextTX;
326         card->NextTX = (card->NextTX + nDescs) % N_TX_DESCS;
327         desc = card->TXDescs + first_desc_id;
328         
329         desc->TXBufferStart = MM_GetPhysAddr( data );
330         desc->BufferSize = len | (1 << 15);
331         desc->TSR = 0;
332         desc->TCR = 0;
333         #endif
334         
335         first_desc = card->TXDescs + first_desc_id;
336         last_desc = desc;
337         
338         first_desc->TCR |= TD_TCR_STP;
339         last_desc->TCR |= TD_TCR_EDP|TD_TCR_IC;
340 //      last_desc->BufferSize &= ~(1 << 15);
341
342         first_desc->TSR |= TD_TSR_OWN;
343
344         LOG("%i descriptors allocated, first = %p, last = %p", nDescs, first_desc, last_desc);
345
346
347         LOG("Waiting for TX to complete");
348         outb(card->IOBase + REG_CR0, CR0_TDMD|CR0_BASEVAL);
349         Semaphore_Wait(&card->SendSemaphore, 1);
350
351         #if 1
352         free((void*)data);
353         #endif
354
355         LEAVE('i', 0);
356         return 0;
357 }
358
359 void Rhine2_IRQHandler(int Num, void *Ptr)
360 {
361         tCard   *card = Ptr;
362         Uint8   isr0 = inb(card->IOBase + REG_ISR0);
363         Uint8   isr1 = inb(card->IOBase + REG_ISR1);
364
365         if( isr0 == 0 ) return ;        
366
367         LOG("ISR0 = 0x%02x, ISR1 = 0x%02x", isr0, isr1);
368
369         if( isr0 & ISR0_PRX )
370         {
371                 LOG("PRX");
372                 Semaphore_Signal(&card->ReadSemaphore, 1);
373         }
374         
375         if( isr0 & ISR0_PTX )
376         {
377                 LOG("PTX");
378                 Semaphore_Signal(&card->SendSemaphore, 1);
379         }
380         
381         if( isr0 & ISR0_TXE )
382         {
383                 LOG("TX Error... oops");
384                 Semaphore_Signal(&card->SendSemaphore, 1);
385         }
386         
387         if( isr0 & ISR0_TU )
388         {
389                 LOG("Transmit buffer underflow");
390         }
391
392         LOG("Acking interrupts");
393         outb(card->IOBase + REG_ISR0, isr0);
394         outb(card->IOBase + REG_ISR1, isr1);
395 }
396
397 // --- Helpers ---
398 struct sRXDesc *Rhine2_int_GetDescFromPhys(tCard *Card, Uint32 Addr)
399 {
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);
404 }
405
406 void *Rhine2_int_GetBufferFromPhys(tCard *Card, Uint32 Addr)
407 {
408         for( int i = 0; i < N_RX_BUF_PAGES; i ++ )
409         {
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));
413         }
414         return NULL;
415 }
416
417 void Rhine2_int_FreeRXDesc(void *Ptr, size_t u1, size_t u2, const void *u3)
418 {
419         struct sRXDesc  *desc = Ptr;
420
421         LOG("Descriptor %p returned to card", desc);
422         
423         desc->RSR = 0;
424         desc->Length = (1 << 15);       // Reset OWN
425 }
426

UCC git Repository :: git.ucc.asn.au