Merge branch 'master' of github.com:thepowersgang/acess2
[tpg/acess2.git] / KernelLand / Modules / Network / PCnetFAST3 / pcnet-fast3.c
1 /*
2  * Acess2 PCnet-FAST III Driver
3  * - By John Hodge (thePowersGang)
4  */
5 #define DEBUG   1
6 #define VERSION ((1<<8)|0)
7 #include <acess.h>
8 #include <modules.h>
9 #include <drv_pci.h>
10 #include <semaphore.h>
11 #include <IPStack/include/adapters_api.h>
12 #include "hw.h"
13
14 #define assert  ASSERT  // because CBF updating code
15
16 // === CONSTANTS ===
17 #define VENDOR_ID       0x1022
18 #define DEVICE_ID       0x2000
19 #define TLEN_LOG2       6       // 64
20 #define RLEN_LOG2       7
21 #define TLEN    (1 << TLEN_LOG2)
22 #define RLEN    (1 << RLEN_LOG2)
23 #define RXBUFLEN        128     // 128*128 = 16K total RX buffer
24 #define RXBUF_PER_PAGE  (PAGE_SIZE/RXBUFLEN)
25 #define NUM_RXBUF_PAGES ((RLEN*RXBUFLEN)/PAGE_SIZE)
26
27 // === TYPES ===
28 typedef struct sCard
29 {
30         Uint16  IOBase;
31         Uint8   IRQ;
32
33         tPAddr  TxQueuePhys;
34         tTxDesc_3       *TxQueue;
35         void    *TxQueueBuffers[TLEN];  // Pointer to the tIPStackBuffer (STP only)
36
37         tMutex  lTxPos;
38         tSemaphore      TxDescSem;
39          int    FirstUsedTxD;
40          int    FirstFreeTx;
41         
42         tSemaphore      ReadSemaphore;  
43         tMutex  lRxPos;
44          int    RxPos;
45         tPAddr  RxQueuePhys;
46         tRxDesc_3       *RxQueue;
47         void    *RxBuffers[NUM_RXBUF_PAGES];    // Pages
48         
49         Uint8   MacAddr[6];
50         void    *IPStackHandle;
51 }       tCard;
52
53 // === PROTOTYPES ===
54  int    PCnet3_Install(char **Options);
55  int    PCnet3_Cleanup(void);
56
57 tIPStackBuffer  *PCnet3_WaitForPacket(void *Ptr);
58  int    PCnet3_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
59
60  int    PCnet3_int_InitCard(tCard *Card);
61 void    PCnet3_IRQHandler(int Num, void *Ptr);
62 void    PCnet3_ReleaseRxD(void *Arg, size_t HeadLen, size_t FootLen, const void *Data);
63
64 static Uint16   _ReadCSR(tCard *Card, Uint8 Reg);
65 static void     _WriteCSR(tCard *Card, Uint8 Reg, Uint16 Value);
66 static Uint16   _ReadBCR(tCard *Card, Uint8 Reg);
67 static void     _WriteBCR(tCard *Card, Uint8 Reg, Uint16 Value);
68
69 // === GLOBALS ===
70 MODULE_DEFINE(0, VERSION, Network_PCnetFAST3, PCnet3_Install, PCnet3_Cleanup, "IPStack", NULL);
71 tIPStack_AdapterType    gPCnet3_AdapterType = {
72         .Name = "PCnet-FAST III",
73         .Type = ADAPTERTYPE_ETHERNET_100M,
74         //.Flags = ADAPTERFLAG_OFFLOAD_MAC,
75         .Flags = 0,
76         .SendPacket = PCnet3_SendPacket,
77         .WaitForPacket = PCnet3_WaitForPacket
78 };
79  int    giPCnet3_CardCount;
80 tCard   *gaPCnet3_Cards;
81 // - Init
82 tInitBlock32    gPCnet3_StaticInitBlock;
83 tInitBlock32    *gpPCnet3_InitBlock;
84
85 // === CODE ===
86 /**
87  * \brief Installs the PCnet3 Driver
88  */
89 int PCnet3_Install(char **Options)
90 {
91          int    id = -1;
92          int    i = 0;
93         Uint16  base;
94         tCard   *card;
95         
96         giPCnet3_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
97         Log_Debug("PCnet3", "%i cards", giPCnet3_CardCount);
98         
99         if( giPCnet3_CardCount == 0 )   return MODULE_ERR_NOTNEEDED;
100
101         gpPCnet3_InitBlock = &gPCnet3_StaticInitBlock;
102         // TODO: Edge case bug here with the block on the end of a page
103         if( MM_GetPhysAddr(gpPCnet3_InitBlock) + sizeof(tInitBlock32) != MM_GetPhysAddr(gpPCnet3_InitBlock+1)
104         #if PHYS_BITS > 32
105                 ||  MM_GetPhysAddr(gpPCnet3_InitBlock) > (1ULL<<32)
106         #endif
107                 )
108         {
109                 // allocate
110                 Log_Error("PCnet3", "TODO: Support 64-bit init / spanning init");
111                 return MODULE_ERR_MISC;
112         }
113         
114         gaPCnet3_Cards = calloc( giPCnet3_CardCount, sizeof(tCard) );
115         
116         while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
117         {
118                 // Set up card addresses
119                 // - BAR0: IO base address
120                 // - BAR1: MMIO base address
121                 card = &gaPCnet3_Cards[i];
122                 base = PCI_GetBAR( id, 0 );
123                 if( !(base & 1) ) {
124                         Log_Warning("PCnet3", "Driver does not support MMIO, skipping card");
125                         card->IOBase = 0;
126                         card->IRQ = 0;
127                         continue ;
128                 }
129                 base &= ~1;
130                 card->IOBase = base;
131                 card->IRQ = PCI_GetIRQ( id );
132
133                 // Switch the card into DWord mode
134                 // - TODO: Should the value of RAP matter here?
135                 outd(card->IOBase + REG_RDP, 0);
136
137                 // Get MAC address
138                 Uint32  macword;
139                 macword = ind(card->IOBase + REG_APROM0);
140                 card->MacAddr[0] = macword & 0xFF;
141                 card->MacAddr[1] = macword >> 8;
142                 card->MacAddr[2] = macword >> 16;
143                 card->MacAddr[3] = macword >> 24;
144                 macword = ind(card->IOBase + REG_APROM4);
145                 card->MacAddr[4] = macword & 0xFF;
146                 card->MacAddr[5] = macword >> 8;
147
148                 // Install IRQ Handler
149                 IRQ_AddHandler(card->IRQ, PCnet3_IRQHandler, card);
150                 
151                 // Initialise the card state
152                 PCnet3_int_InitCard(card);
153
154                 // Register
155                 card->IPStackHandle = IPStack_Adapter_Add(&gPCnet3_AdapterType, card, card->MacAddr);
156                 
157                 i ++;
158         }
159
160         if( gpPCnet3_InitBlock != &gPCnet3_StaticInitBlock ) {
161                 MM_UnmapHWPages( (tVAddr)gpPCnet3_InitBlock, 1 );
162         }
163         
164         return MODULE_ERR_OK;
165 }
166
167 int PCnet3_Cleanup(void)
168 {
169         // TODO: Kill IPStack adapters and clean up
170         return -1;
171 }
172
173 // --- Root Functions ---
174 tIPStackBuffer *PCnet3_WaitForPacket(void *Ptr)
175 {
176         tCard   *card = Ptr;
177
178         ENTER("pPtr", Ptr);
179
180         if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
181         {
182                 LEAVE_RET('n', NULL);
183         }
184
185         // Get descriptor range for packet
186         // TODO: Replace asserts with something a little more permissive        
187         Mutex_Acquire( &card->lRxPos );
188          int    first_td = card->RxPos;
189          int    nextp_td = first_td;
190         assert( card->RxQueue[first_td].Flags & RXDESC_FLG_STP );
191         while( !(card->RxQueue[nextp_td].Flags & RXDESC_FLG_ENP) )
192         {
193                 tRxDesc_3 *rd = &card->RxQueue[nextp_td];
194                 assert( !(rd->Flags & RXDESC_FLG_OWN) );
195                 // TODO: Check error bits properly
196                 if( rd->Flags & 0x7C000000 ) {
197                         Log_Notice("PCnet3", "Error bits set: 0x%x", (rd->Flags>>24) & 0x7C);
198                 }
199                 nextp_td = (nextp_td+1) % RLEN;
200                 assert(nextp_td != first_td);
201         }
202         nextp_td = (nextp_td+1) % RLEN;
203         card->RxPos = nextp_td;
204         Mutex_Release( &card->lRxPos );
205         
206          int    nDesc = (nextp_td - first_td + RLEN) % RLEN;
207
208         // Create buffer structure      
209         // TODO: Could be more efficient by checking for buffers in the same page / fully contig allocations
210         // - Meh
211         tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(nDesc);
212         for( int idx = first_td; idx != nextp_td; idx = (idx+1) % RLEN )
213         {
214                 tRxDesc_3 *rd = &card->RxQueue[idx];
215                 void    *ptr = card->RxBuffers[idx/RXBUF_PER_PAGE] + (idx%RXBUF_PER_PAGE)*RXBUFLEN;
216                 IPStack_Buffer_AppendSubBuffer(ret, (rd->Count & 0xFFF), 0, ptr, PCnet3_ReleaseRxD, rd);
217         }
218
219         LEAVE('p', ret);
220         return ret;
221 }
222
223 int PCnet3_int_FillTD(tTxDesc_3 *td, Uint32 BufAddr, Uint32 Len, int bBounced)
224 {
225         td->Flags0 = 0;
226         td->Flags1 = 0xF000 | (4096 - (Len & 0xFFF));
227         td->Buffer = BufAddr;
228         td->_avail = bBounced;
229         return 0;
230 }
231
232 int PCnet3_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
233 {
234         tCard   *card = Ptr;
235         
236         if( IPStack_Buffer_GetLength(Buffer) > 1500 ) {
237                 // MTU exceeded
238                 return EINVAL;
239         }
240         
241         ENTER("pPtr pBuffer", Ptr, Buffer);
242         // Need a sequence of `n` transmit descriptors
243         // - Can assume that descriptors are consumed FIFO from the current descriptor point
244          int    idx = 0;
245          int    nDesc = 0;
246         const void *sbuf_ptr;
247         size_t  sbuf_len;
248         while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
249         {
250                 nDesc ++;
251                 #if PHYS_BITS > 32
252                 if( MM_GetPhysAddr(sbuf_ptr) > (1ULL<<32) )
253                         ;       // will be bounce-buffered
254                 else
255                 #endif
256                 if( MM_GetPhysAddr(sbuf_ptr)+sbuf_len-1 != MM_GetPhysAddr(sbuf_ptr+sbuf_len-1) )
257                 {
258                         // Split
259                         nDesc ++;
260                 }
261         }
262
263         // - Obtain enough descriptors
264         int rv = Semaphore_Wait(&card->TxDescSem, nDesc);
265         if( rv != nDesc ) {
266                 Log_Notice("PCnet3", "Semaphore wait interrupted, restoring %i descriptors");
267                 Semaphore_Signal(&card->TxDescSem, rv);
268                 LEAVE_RET('i', EINTR);
269         }
270         Mutex_Acquire(&card->lTxPos);
271         int first_desc = card->FirstFreeTx;
272         card->FirstFreeTx = (card->FirstFreeTx + nDesc) % TLEN;
273         Mutex_Release(&card->lTxPos);
274         
275         // - Set up descriptors
276          int    td_idx = first_desc;
277         while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
278         {
279                 tTxDesc_3 *td = &card->TxQueue[td_idx];
280                 assert( !(td->Flags1 & TXDESC_FLG1_OWN) );
281                 td_idx = (td_idx + 1) % TLEN;
282                 
283                 tPAddr  start_phys = MM_GetPhysAddr(sbuf_ptr);
284                 size_t  page1_maxsize = PAGE_SIZE - (start_phys % PAGE_SIZE);
285                 tPAddr  end_phys = MM_GetPhysAddr(sbuf_ptr + sbuf_len-1);
286
287                 #if PHYS_BITS > 32
288                 if( start_phys > (1ULL<<32) || end_phys > (1ULL<<32) )
289                 {
290                         // TODO: Have a global set of bounce buffers
291                         tPAddr bounce_phys;
292                         void *bounce_virt = MM_AllocDMA(1, 32, &bounce_phys);
293                         memcpy(bounce_virt, sbuf_ptr, sbuf_len);
294                         // Copy to bounce buffer
295                         PCnet3_int_FillTD(td, bounce_phys, sbuf_len, 1);
296                         LOG("%i: Bounce buffer %P+%i (orig %P,%P) - %p",
297                                 idx, bounce_phys, sbuf_len, start_phys, end_phys, td);
298                 }
299                 else
300                 #endif
301                 if( start_phys+sbuf_len-1 != end_phys )
302                 {
303                         // Split buffer into two descriptors
304                         tTxDesc_3 *td2 = &card->TxQueue[td_idx];
305                         assert( !(td2->Flags1 & TXDESC_FLG1_OWN) );
306                         td_idx = (td_idx + 1) % TLEN;
307                         
308                         PCnet3_int_FillTD(td, start_phys, page1_maxsize, 0);
309                         
310                         size_t  page2_size = sbuf_len - page1_maxsize;
311                         PCnet3_int_FillTD(td2, end_phys - (page2_size-1), page2_size, 0);
312                         // - Explicitly set OWN on td2 because it's never the first, and `td` gets set below
313                         td2->Flags1 |= TXDESC_FLG1_OWN;
314                         
315                         LOG("%i: Split (%P,%P)+%i - %p,%p",
316                                 idx, td->Buffer, td2->Buffer, sbuf_len, td, td2);
317                 }
318                 else
319                 {
320                         PCnet3_int_FillTD(td, start_phys, sbuf_len, 0);
321                         LOG("%i: Straight %P+%i - %p",
322                                 idx, td->Buffer, sbuf_len, td);
323                 }
324                 // On every descriptor except the first, set OWN
325                 // - OWN set later once all are filled
326                 if( td != &card->TxQueue[first_desc] )
327                         td->Flags1 |= TXDESC_FLG1_OWN;
328         }
329
330         // - Lock buffer before allowing the card to continue
331         IPStack_Buffer_LockBuffer(Buffer);
332         
333         // - Set STP/ENP
334         card->TxQueue[first_desc].Flags1 |= TXDESC_FLG1_STP;
335         card->TxQueue[(td_idx+TLEN-1)%TLEN].Flags1 |= TXDESC_FLG1_ENP|TXDESC_FLG1_ADDFCS;
336         // - Set OWN on the first descriptor
337         card->TxQueue[first_desc].Flags1 |= TXDESC_FLG1_OWN;
338         card->TxQueueBuffers[first_desc] = Buffer;
339
340         LOG("CSR0=0x%x", _ReadCSR(card, 0));
341         LOG("Transmit started, waiting for completion");
342         
343         // Block here until packet is sent
344         // TODO: Should be able to return, but just in case
345         IPStack_Buffer_LockBuffer(Buffer);
346         IPStack_Buffer_UnlockBuffer(Buffer);
347         
348         LEAVE('i', 0);
349         return 0;
350 }
351
352 int PCnet3_int_InitCard(tCard *Card)
353 {
354         // Allocate ring buffers
355         Card->TxQueue = (void*)MM_AllocDMA(1, 32, &Card->TxQueuePhys);
356         if( !Card->TxQueue ) {
357                 return MODULE_ERR_MALLOC;
358         }
359         memset(Card->TxQueue, 0, TLEN*sizeof(*Card->TxQueue));
360         #if TLEN + RLEN <= PAGE_SIZE / (4*4)
361         Card->RxQueue = (void*)MM_AllocDMA(1, 32, &Card->RxQueuePhys);
362         if( !Card->RxQueue ) {
363                 return MODULE_ERR_MALLOC;
364         }
365         #else
366         Card->RxQueue = Card->TxQueue + TLEN;
367         Card->RxQueuePhys = Card->RxQueuePhys + TLEN*sizeof(*Card->TxQueue);
368         #endif
369
370         // Allocate Rx buffers
371         for( int i = 0; i < NUM_RXBUF_PAGES; i ++ )
372         {
373                 tPAddr  physaddr;
374                 Card->RxBuffers[i] = (void*)MM_AllocDMA(1, 32, &physaddr);
375                 if( !Card->RxBuffers[i] ) {
376                         return MODULE_ERR_MALLOC;
377                 }
378                 for( int j = 0; j < RXBUF_PER_PAGE; j ++ )
379                 {
380                         Card->RxQueue[i*RXBUF_PER_PAGE+j].Buffer = physaddr;
381                         physaddr += RXBUFLEN;
382                 }
383         }
384         
385         // Initialise semaphores
386         Semaphore_Init(&Card->TxDescSem, TLEN, TLEN, "PCnet3", "Tx Descriptors");
387         Semaphore_Init(&Card->ReadSemaphore, 0, RLEN, "PCnet3", "Rx Descriptors");
388         
389         // Fill Init Block for this card
390         gpPCnet3_InitBlock->Mode = (TLEN_LOG2 << 28) | (RLEN_LOG2 << 20);
391         gpPCnet3_InitBlock->PhysAddr1 = 0;
392         memcpy(&gpPCnet3_InitBlock->PhysAddr0, Card->MacAddr, 6);
393         gpPCnet3_InitBlock->LAdrF0 = -1;        // TODO: Allow these to be set by the IPStack
394         gpPCnet3_InitBlock->LAdrF1 = -1;
395         gpPCnet3_InitBlock->RDRA = Card->RxQueuePhys;
396         gpPCnet3_InitBlock->TDRA = Card->TxQueuePhys;
397
398         // Reset card
399         inw(Card->IOBase + REG_RESET);
400         _WriteBCR(Card, BCR_SWSTYLE, (1<<8)|3); // Set SSIZE32
401         LOG("BCR_SWSTYLE reads as 0x%x", _ReadBCR(Card, BCR_SWSTYLE));
402         LOG("CSR4 reads as 0x%x", _ReadCSR(Card, 4));
403
404         // Initialise
405         tPAddr  paddr = MM_GetPhysAddr(gpPCnet3_InitBlock);
406         _WriteCSR(Card, CSR_IBA0, paddr & 0xFFFF);
407         _WriteCSR(Card, CSR_IBA1, paddr >> 16);
408         _WriteCSR(Card, CSR_STATUS, CSR_STATUS_INIT|CSR_STATUS_IENA|CSR_STATUS_STRT);
409
410         return 0;
411 }
412
413 void PCnet3_IRQHandler(int Num, void *Ptr)
414 {
415         tCard   *card = Ptr;
416         Uint16  status = _ReadCSR(card, CSR_STATUS);
417
418         LOG("status = 0x%02x", status);
419         status &= ~CSR_STATUS_INTR;     // Read-only bit
420         
421         // Rx Interrupt
422         // META - Check LAPPEN bit in CSR3
423         if( status & CSR_STATUS_RINT )
424         {
425                 // TODO: Avoid issues when two packets arrive in one interrupt time
426                 Semaphore_Signal(&card->ReadSemaphore, 1);
427         }
428         
429         // Tx Interrupt
430         if( status & CSR_STATUS_TINT )
431         {
432                  int    idx;
433                 for( idx = card->FirstUsedTxD; idx != card->FirstFreeTx; idx = (idx+1)%TLEN )
434                 {
435                         tTxDesc_3 *td = &card->TxQueue[idx];
436                         // Stop on the first chip-owned TxD
437                         LOG("idx=%i, Flags1=0x%08x", idx, td->Flags1);
438                         if( td->Flags1 & TXDESC_FLG1_OWN )
439                                 break;
440                         if( td->Flags1 & (1<<30) )
441                         {
442                                 LOG(" Flags0=0x%08x %s%s%s%s%s%s%i",
443                                         td->Flags0,
444                                         (td->Flags0 & (1<<31)) ? "BUFF " : "",
445                                         (td->Flags0 & (1<<30)) ? "UFLO " : "",
446                                         (td->Flags0 & (1<<29)) ? "EXDEF " : "",
447                                         (td->Flags0 & (1<<28)) ? "LCOL " : "",
448                                         (td->Flags0 & (1<<27)) ? "LCAR " : "",
449                                         (td->Flags0 & (1<<26)) ? "RTRY " : "",
450                                         td->Flags0 & 15
451                                         );
452                         }
453                         if( td->Flags1 & TXDESC_FLG1_STP )
454                                 IPStack_Buffer_UnlockBuffer( card->TxQueueBuffers[idx] );
455                         Semaphore_Signal(&card->TxDescSem, 1);
456                 }
457                 card->FirstUsedTxD = idx;
458         }
459
460         if( status & CSR_STATUS_IDON )
461         {
462                 Log_Debug("PCnet3", "Card %p initialisation done", card);
463                 LOG("CSR15 reads as 0x%x", _ReadCSR(card, 15));
464         }
465
466         // ERR set?
467         if( status & 0xBC00 )
468         {
469                 Log_Notice("PCnet3", "Error on %p: %s%s%s%s",
470                         card,
471                         (status & (1<<15)) ? "ERR " : "",
472                         (status & (1<<13)) ? "CERR " : "",
473                         (status & (1<<12)) ? "MISS " : "",
474                         (status & (1<<11)) ? "MERR " : ""
475                         );
476         }
477
478         _WriteCSR(card, CSR_STATUS, status);
479 }
480
481 void PCnet3_ReleaseRxD(void *Arg, size_t HeadLen, size_t FootLen, const void *Data)
482 {
483         tRxDesc_3       *rd = Arg;
484         rd->Flags &= 0xFFFF;
485         rd->Flags |= RXDESC_FLG_OWN;
486 }
487
488 static Uint16 _ReadCSR(tCard *Card, Uint8 Reg)
489 {
490         outd(Card->IOBase + REG_RAP, Reg);
491         return ind(Card->IOBase + REG_RDP);
492 }
493 static void _WriteCSR(tCard *Card, Uint8 Reg, Uint16 Value)
494 {
495         outd(Card->IOBase + REG_RAP, Reg);
496         outd(Card->IOBase + REG_RDP, Value);
497 }
498 static Uint16 _ReadBCR(tCard *Card, Uint8 Reg)
499 {
500         outd(Card->IOBase + REG_RAP, Reg);
501         return ind(Card->IOBase + REG_BDP);
502 }
503 void _WriteBCR(tCard *Card, Uint8 Reg, Uint16 Value)
504 {
505         outd(Card->IOBase + REG_RAP, Reg);
506         outd(Card->IOBase + REG_BDP, Value);
507 }
508

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