2 * Acess2 PCnet-FAST III Driver
3 * - By John Hodge (thePowersGang)
6 #define VERSION ((1<<8)|0)
10 #include <semaphore.h>
11 #include <IPStack/include/adapters_api.h>
15 #define VENDOR_ID 0x1022
16 #define DEVICE_ID 0x2000
17 #define TLEN_LOG2 6 // 64
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)
33 void *TxQueueBuffers[TLEN]; // Pointer to the tIPStackBuffer (STP only)
40 tSemaphore ReadSemaphore;
45 void *RxBuffers[NUM_RXBUF_PAGES]; // Pages
52 int PCnet3_Install(char **Options);
53 int PCnet3_Cleanup(void);
55 tIPStackBuffer *PCnet3_WaitForPacket(void *Ptr);
56 int PCnet3_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
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);
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);
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,
74 .SendPacket = PCnet3_SendPacket,
75 .WaitForPacket = PCnet3_WaitForPacket
77 int giPCnet3_CardCount;
78 tCard *gaPCnet3_Cards;
80 tInitBlock32 gPCnet3_StaticInitBlock;
81 tInitBlock32 *gpPCnet3_InitBlock;
85 * \brief Installs the PCnet3 Driver
87 int PCnet3_Install(char **Options)
94 giPCnet3_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
95 if( giPCnet3_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
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)
101 || MM_GetPhysAddr(gpPCnet3_InitBlock) > (1ULL<<32)
106 Log_Error("PCnet3", "TODO: Support 64-bit init / spanning init");
107 return MODULE_ERR_MISC;
110 gaPCnet3_Cards = calloc( giPCnet3_CardCount, sizeof(tCard) );
112 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
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 );
120 Log_Warning("PCnet3", "Driver does not support MMIO, skipping card");
127 card->IRQ = PCI_GetIRQ( id );
129 // Switch the card into DWord mode
130 // - TODO: Should the value of RAP matter here?
131 outd(card->IOBase + REG_RDP, 0);
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;
144 // Install IRQ Handler
145 IRQ_AddHandler(card->IRQ, PCnet3_IRQHandler, card);
147 // Initialise the card state
148 PCnet3_int_InitCard(card);
151 card->IPStackHandle = IPStack_Adapter_Add(&gPCnet3_AdapterType, card, card->MacAddr);
156 if( gpPCnet3_InitBlock != &gPCnet3_StaticInitBlock )
158 MM_UnmapHWPages( gpPCnet3_InitBlock, 1 );
161 return MODULE_ERR_OK;
164 int PCnet3_Cleanup(void)
166 // TODO: Kill IPStack adapters and clean up
170 // --- Root Functions ---
171 tIPStackBuffer *PCnet3_WaitForPacket(void *Ptr)
177 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
179 LEAVE_RET('n', NULL);
182 // Get descriptor range for packet
183 // TODO: Replace asserts with something a little more permissive
184 Mutex_Acquire( &card->lRxPos );
185 int first_td = card->RxPos;
186 int nextp_td = first_td;
187 assert( card->RxQueue[first_td].Flags & RXDESC_FLG_STP );
188 while( !(card->RxQueue[nextp_td].Flags & RXDESC_FLG_ENP) )
190 tRxDesc_3 *rd = &card->RxQueue[nextp_td];
191 assert( !(rd->Flags & RXDESC_FLG_OWN) );
192 // TODO: Check error bits properly
193 if( rd->Flags & 0x7C000000 ) {
194 Log_Notice("PCnet3", "Error bits set: 0x%x", (rd->Flags>>24) & 0x7C);
196 nextp_td = (nextp_td+1) % RLEN;
197 assert(nextp_td != first_td);
199 nextp_td = (nextp_td+1) % RLEN;
200 card->RxPos = nextp_td;
201 Mutex_Release( &card->lRxPos );
203 int nDesc = (nextp_td - first_td + RLEN) % RLEN;
205 // Create buffer structure
206 // TODO: Could be more efficient by checking for buffers in the same page / fully contig allocations
208 tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(nDesc);
209 for( int idx = first_td; idx != nextp_td; idx = (idx+1) % RLEN )
211 tRxDesc_3 *rd = &card->RxQueue[idx];
212 void *ptr = card->RxBuffers[idx/RXBUF_PER_PAGE] + (idx%RXBUF_PER_PAGE)*RXBUFLEN;
213 IPStack_Buffer_AppendSubBuffer(ret, (rd->Count & 0xFFF), 0, ptr, PCnet3_ReleaseRxD, rd);
220 int PCnet3_int_FillTD(tTxDesc_3 *td, Uint32 BufAddr, Uint32 Len, int bBounced)
223 td->Flags1 = 0xF000 | (4096 - (Len & 0xFFF));
224 td->Buffer = BufAddr;
225 td->_avail = bBounced;
229 int PCnet3_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
233 if( IPStack_Buffer_GetLength(Buffer) > 1500 ) {
238 ENTER("pPtr pBuffer", Ptr, Buffer);
239 // Need a sequence of `n` transmit descriptors
240 // - Can assume that descriptors are consumed FIFO from the current descriptor point
243 const void *sbuf_ptr;
245 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
249 if( MM_GetPhysAddr(sbuf_ptr) > (1ULL<<32) )
250 ; // will be bounce-buffered
253 if( MM_GetPhysAddr(sbuf_ptr)+sbuf_len-1 != MM_GetPhysAddr(sbuf_ptr+sbuf_len-1) )
260 // - Obtain enough descriptors
261 int rv = Semaphore_Wait(&card->TxDescSem, nDesc);
263 Log_Notice("PCnet3", "Semaphore wait interrupted, restoring %i descriptors");
264 Semaphore_Signal(&card->TxDescSem, rv);
265 LEAVE_RET('i', EINTR);
267 Mutex_Acquire(&card->lTxPos);
268 int first_desc = card->FirstFreeTx;
269 card->FirstFreeTx = (card->FirstFreeTx + nDesc) % TLEN;
270 Mutex_Release(&card->lTxPos);
272 // - Set up descriptors
273 int td_idx = first_desc;
274 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
276 tTxDesc_3 *td = &card->TxQueue[td_idx];
277 assert( !(td->Flags1 & TXDESC_FLG1_OWN) );
278 td_idx = (td_idx + 1) % TLEN;
280 tPAddr start_phys = MM_GetPhysAddr(sbuf_ptr);
281 size_t page1_maxsize = PAGE_SIZE - (start_phys % PAGE_SIZE);
282 tPAddr end_phys = MM_GetPhysAddr(sbuf_ptr + sbuf_len-1);
285 if( start_phys > (1ULL<<32) || end_phys > (1ULL<<32) )
287 // TODO: Have a global set of bounce buffers
289 void *bounce_virt = MM_AllocDMA(1, 32, &bounce_phys);
290 memcpy(bounce_virt, sbuf_ptr, sbuf_len);
291 // Copy to bounce buffer
292 PCnet3_int_FillTD(td, bounce_phys, sbuf_len, 1);
293 LOG("%i: Bounce buffer %P+%i (orig %P,%P) - %p",
294 idx, bounce_phys, sbuf_len, start_phys, end_phys, td);
298 if( start_phys+sbuf_len-1 != end_phys )
300 // Split buffer into two descriptors
301 tTxDesc_3 *td2 = &card->TxQueue[td_idx];
302 assert( !(td2->Flags1 & TXDESC_FLG1_OWN) );
303 td_idx = (td_idx + 1) % TLEN;
305 PCnet3_int_FillTD(td, start_phys, page1_maxsize, 0);
307 size_t page2_size = sbuf_len - page1_maxsize;
308 PCnet3_int_FillTD(td2, end_phys - (page2_size-1), page2_size, 0);
309 // - Explicitly set OWN on td2 because it's never the first, and `td` gets set below
310 td2->Flags1 |= TXDESC_FLG1_OWN;
312 LOG("%i: Split (%P,%P)+%i - %p,%p",
313 idx, td->Buffer, td2->Buffer, sbuf_len, td, td2);
317 PCnet3_int_FillTD(td, start_phys, sbuf_len, 0);
318 LOG("%i: Straight %P+%i - %p",
319 idx, td->Buffer, sbuf_len, td);
321 // On every descriptor except the first, set OWN
322 // - OWN set later once all are filled
323 if( td != &card->TxQueue[first_desc] )
324 td->Flags1 |= TXDESC_FLG1_OWN;
327 // - Lock buffer before allowing the card to continue
328 IPStack_Buffer_LockBuffer(Buffer);
331 card->TxQueue[first_desc].Flags1 |= TXDESC_FLG1_STP;
332 card->TxQueue[(td_idx+TLEN-1)%TLEN].Flags1 |= TXDESC_FLG1_ENP|TXDESC_FLG1_ADDFCS;
333 // - Set OWN on the first descriptor
334 card->TxQueue[first_desc].Flags1 |= TXDESC_FLG1_OWN;
335 card->TxQueueBuffers[first_desc] = Buffer;
337 LOG("CSR0=0x%x", _ReadCSR(card, 0));
338 LOG("Transmit started, waiting for completion");
340 // Block here until packet is sent
341 // TODO: Should be able to return, but just in case
342 IPStack_Buffer_LockBuffer(Buffer);
343 IPStack_Buffer_UnlockBuffer(Buffer);
349 int PCnet3_int_InitCard(tCard *Card)
351 // Allocate ring buffers
352 Card->TxQueue = (void*)MM_AllocDMA(1, 32, &Card->TxQueuePhys);
353 if( !Card->TxQueue ) {
354 return MODULE_ERR_MALLOC;
356 memset(Card->TxQueue, 0, TLEN*sizeof(*Card->TxQueue));
357 #if TLEN + RLEN <= PAGE_SIZE / (4*4)
358 Card->RxQueue = (void*)MM_AllocDMA(1, 32, &Card->RxQueuePhys);
359 if( !Card->RxQueue ) {
360 return MODULE_ERR_MALLOC;
363 Card->RxQueue = Card->TxQueue + TLEN;
364 Card->RxQueuePhys = Card->RxQueuePhys + TLEN*sizeof(*Card->TxQueue);
367 // Allocate Rx buffers
368 for( int i = 0; i < NUM_RXBUF_PAGES; i ++ )
371 Card->RxBuffers[i] = (void*)MM_AllocDMA(1, 32, &physaddr);
372 if( !Card->RxBuffers[i] ) {
373 return MODULE_ERR_MALLOC;
375 for( int j = 0; j < RXBUF_PER_PAGE; j ++ )
377 Card->RxQueue[i*RXBUF_PER_PAGE+j].Buffer = physaddr;
378 physaddr += RXBUFLEN;
382 // Initialise semaphores
383 Semaphore_Init(&Card->TxDescSem, TLEN, TLEN, "PCnet3", "Tx Descriptors");
384 Semaphore_Init(&Card->ReadSemaphore, 0, RLEN, "PCnet3", "Rx Descriptors");
386 // Fill Init Block for this card
387 gpPCnet3_InitBlock->Mode = (TLEN_LOG2 << 28) | (RLEN_LOG2 << 20);
388 gpPCnet3_InitBlock->PhysAddr1 = 0;
389 memcpy(&gpPCnet3_InitBlock->PhysAddr0, Card->MacAddr, 6);
390 gpPCnet3_InitBlock->LAdrF0 = -1; // TODO: Allow these to be set by the IPStack
391 gpPCnet3_InitBlock->LAdrF1 = -1;
392 gpPCnet3_InitBlock->RDRA = Card->RxQueuePhys;
393 gpPCnet3_InitBlock->TDRA = Card->TxQueuePhys;
396 inw(Card->IOBase + REG_RESET);
397 _WriteBCR(Card, BCR_SWSTYLE, (1<<8)|3); // Set SSIZE32
398 LOG("BCR_SWSTYLE reads as 0x%x", _ReadBCR(Card, BCR_SWSTYLE));
399 LOG("CSR4 reads as 0x%x", _ReadCSR(Card, 4));
402 tPAddr paddr = MM_GetPhysAddr(gpPCnet3_InitBlock);
403 _WriteCSR(Card, CSR_IBA0, paddr & 0xFFFF);
404 _WriteCSR(Card, CSR_IBA1, paddr >> 16);
405 _WriteCSR(Card, CSR_STATUS, CSR_STATUS_INIT|CSR_STATUS_IENA|CSR_STATUS_STRT);
410 void PCnet3_IRQHandler(int Num, void *Ptr)
413 Uint16 status = _ReadCSR(card, CSR_STATUS);
415 LOG("status = 0x%02x", status);
416 status &= ~CSR_STATUS_INTR; // Read-only bit
419 // META - Check LAPPEN bit in CSR3
420 if( status & CSR_STATUS_RINT )
422 // TODO: Avoid issues when two packets arrive in one interrupt time
423 Semaphore_Signal(&card->ReadSemaphore, 1);
427 if( status & CSR_STATUS_TINT )
430 for( idx = card->FirstUsedTxD; idx != card->FirstFreeTx; idx = (idx+1)%TLEN )
432 tTxDesc_3 *td = &card->TxQueue[idx];
433 // Stop on the first chip-owned TxD
434 LOG("idx=%i, Flags1=0x%08x", idx, td->Flags1);
435 if( td->Flags1 & TXDESC_FLG1_OWN )
437 if( td->Flags1 & (1<<30) )
439 LOG(" Flags0=0x%08x %s%s%s%s%s%s%i",
441 (td->Flags0 & (1<<31)) ? "BUFF " : "",
442 (td->Flags0 & (1<<30)) ? "UFLO " : "",
443 (td->Flags0 & (1<<29)) ? "EXDEF " : "",
444 (td->Flags0 & (1<<28)) ? "LCOL " : "",
445 (td->Flags0 & (1<<27)) ? "LCAR " : "",
446 (td->Flags0 & (1<<26)) ? "RTRY " : "",
450 if( td->Flags1 & TXDESC_FLG1_STP )
451 IPStack_Buffer_UnlockBuffer( card->TxQueueBuffers[idx] );
452 Semaphore_Signal(&card->TxDescSem, 1);
454 card->FirstUsedTxD = idx;
457 if( status & CSR_STATUS_IDON )
459 Log_Debug("PCnet3", "Card %p initialisation done", card);
460 LOG("CSR15 reads as 0x%x", _ReadCSR(card, 15));
464 if( status & 0xBC00 )
466 Log_Notice("PCnet3", "Error on %p: %s%s%s%s",
468 (status & (1<<15)) ? "ERR " : "",
469 (status & (1<<13)) ? "CERR " : "",
470 (status & (1<<12)) ? "MISS " : "",
471 (status & (1<<11)) ? "MERR " : ""
475 _WriteCSR(card, CSR_STATUS, status);
478 void PCnet3_ReleaseRxD(void *Arg, size_t HeadLen, size_t FootLen, const void *Data)
482 rd->Flags |= RXDESC_FLG_OWN;
485 static Uint16 _ReadCSR(tCard *Card, Uint8 Reg)
487 outd(Card->IOBase + REG_RAP, Reg);
488 return ind(Card->IOBase + REG_RDP);
490 static void _WriteCSR(tCard *Card, Uint8 Reg, Uint16 Value)
492 outd(Card->IOBase + REG_RAP, Reg);
493 outd(Card->IOBase + REG_RDP, Value);
495 static Uint16 _ReadBCR(tCard *Card, Uint8 Reg)
497 outd(Card->IOBase + REG_RAP, Reg);
498 return ind(Card->IOBase + REG_BDP);
500 void _WriteBCR(tCard *Card, Uint8 Reg, Uint16 Value)
502 outd(Card->IOBase + REG_RAP, Reg);
503 outd(Card->IOBase + REG_BDP, Value);