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

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