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

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