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>
14 #define assert ASSERT // because CBF updating code
17 #define VENDOR_ID 0x1022
18 #define DEVICE_ID 0x2000
19 #define TLEN_LOG2 6 // 64
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)
35 void *TxQueueBuffers[TLEN]; // Pointer to the tIPStackBuffer (STP only)
42 tSemaphore ReadSemaphore;
47 void *RxBuffers[NUM_RXBUF_PAGES]; // Pages
54 int PCnet3_Install(char **Options);
55 int PCnet3_Cleanup(void);
57 tIPStackBuffer *PCnet3_WaitForPacket(void *Ptr);
58 int PCnet3_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
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);
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);
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
78 int giPCnet3_CardCount;
79 tCard *gaPCnet3_Cards;
81 tInitBlock32 gPCnet3_StaticInitBlock;
82 tInitBlock32 *gpPCnet3_InitBlock;
86 * \brief Installs the PCnet3 Driver
88 int PCnet3_Install(char **Options)
95 giPCnet3_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
96 Log_Debug("PCnet3", "%i cards", giPCnet3_CardCount);
98 if( giPCnet3_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
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)
104 || MM_GetPhysAddr(gpPCnet3_InitBlock) > (1ULL<<32)
109 Log_Error("PCnet3", "TODO: Support 64-bit init / spanning init");
110 return MODULE_ERR_MISC;
113 gaPCnet3_Cards = calloc( giPCnet3_CardCount, sizeof(tCard) );
115 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
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 );
123 Log_Warning("PCnet3", "Driver does not support MMIO, skipping card");
130 card->IRQ = PCI_GetIRQ( id );
132 // Switch the card into DWord mode
133 // - TODO: Should the value of RAP matter here?
134 outd(card->IOBase + REG_RDP, 0);
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;
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;
160 // Install IRQ Handler
161 IRQ_AddHandler(card->IRQ, PCnet3_IRQHandler, card);
163 // Soft reset (read from RESET)
164 PCnet3_int_InitCard(card);
167 Semaphore_Init(&card->ReadSemaphore, 0, 0, "PCnet3", "CardRead");
170 card->IPStackHandle = IPStack_Adapter_Add(&gPCnet3_AdapterType, card, card->MacAddr);
175 if( gpPCnet3_InitBlock != &gPCnet3_StaticInitBlock ) {
176 MM_UnmapHWPages( (tVAddr)gpPCnet3_InitBlock, 1 );
179 return MODULE_ERR_OK;
182 int PCnet3_Cleanup(void)
184 // TODO: Kill IPStack adapters and clean up
188 // --- Root Functions ---
189 tIPStackBuffer *PCnet3_WaitForPacket(void *Ptr)
195 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
197 LEAVE_RET('n', NULL);
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) )
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);
214 nextp_td = (nextp_td+1) % RLEN;
215 assert(nextp_td != first_td);
217 nextp_td = (nextp_td+1) % RLEN;
218 card->RxPos = nextp_td;
219 Mutex_Release( &card->lRxPos );
221 int nDesc = (nextp_td - first_td + RLEN) % RLEN;
223 // Create buffer structure
224 // TODO: Could be more efficient by checking for buffers in the same page / fully contig allocations
226 tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(nDesc);
227 for( int idx = first_td; idx != nextp_td; idx = (idx+1) % RLEN )
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);
238 int PCnet3_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
242 if( IPStack_Buffer_GetLength(Buffer) > 1500 ) {
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
252 const void *sbuf_ptr;
254 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
258 if( MM_GetPhysAddr(sbuf_ptr) > (1ULL<<32) )
259 ; // will be bounce-buffered
262 if( MM_GetPhysAddr(sbuf_ptr)+sbuf_len != MM_GetPhysAddr(sbuf_ptr+sbuf_len) )
269 // - Obtain enough descriptors
270 int rv = Semaphore_Wait(&card->TxDescSem, nDesc);
272 Log_Notice("PCnet3", "Semaphore wait interrupted, restoring %i descriptors");
273 Semaphore_Signal(&card->TxDescSem, rv);
274 LEAVE_RET('i', EINTR);
276 Mutex_Acquire(&card->lTxPos);
277 int first_desc = card->FirstFreeTx;
278 card->FirstFreeTx = (card->FirstFreeTx + nDesc) % TLEN;
279 Mutex_Release(&card->lTxPos);
281 // - Set up descriptors
282 int td_idx = first_desc;
283 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
285 tTxDesc_3 *td = &card->TxQueue[td_idx];
286 assert( !(td->Flags1 & TXDESC_FLG1_OWN) );
287 td_idx = (td_idx + 1) % TLEN;
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);
294 if( start_phys > (1ULL<<32) || end_phys > (1ULL<<32) )
296 // TODO: Have a global set of bounce buffers
298 void *bounce_virt = MM_AllocDMA(1, 32, &bounce_phys);
299 memcpy(bounce_virt, sbuf_ptr, sbuf_len);
300 // Copy to bounce buffer
302 td->Flags1 = 0xF000 | sbuf_len;
303 td->Buffer = bounce_phys;
305 LOG("%i: Bounce buffer %P+%i (orig %P,%P) - %p",
306 idx, bounce_phys, sbuf_len, start_phys, end_phys, td);
310 if( start_phys+sbuf_len-1 != end_phys )
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;
318 td->Flags1 = 0xF000 | page1_maxsize;
319 td->Buffer = start_phys;
323 td2->Flags1 = 0xF000 | (sbuf_len - page1_maxsize);
324 td2->Buffer = end_phys - (sbuf_len-page1_maxsize-1);
326 td2->Flags1 |= TXDESC_FLG1_OWN;
328 LOG("%i: Split (%P,%P)+%i - %p,%p",
329 idx, td->Buffer, td2->Buffer, sbuf_len, td, td2);
334 td->Flags1 = 0xF000 | sbuf_len;
335 td->Buffer = start_phys;
337 LOG("%i: Straight %P+%i - %p",
338 idx, td->Buffer, sbuf_len, td);
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;
346 // - Lock buffer before allowing the card to continue
347 IPStack_Buffer_LockBuffer(Buffer);
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;
356 LOG("CSR0=0x%x", _ReadCSR(card, 0));
357 LOG("Transmit started, waiting for completion");
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);
368 int PCnet3_int_InitCard(tCard *Card)
370 // Allocate ring buffers
371 Card->TxQueue = (void*)MM_AllocDMA(1, 32, &Card->TxQueuePhys);
372 if( !Card->TxQueue ) {
373 return MODULE_ERR_MALLOC;
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;
382 Card->RxQueue = Card->TxQueue + TLEN;
383 Card->RxQueuePhys = Card->RxQueuePhys + TLEN*sizeof(*Card->TxQueue);
386 // Allocate Rx buffers
387 for( int i = 0; i < NUM_RXBUF_PAGES; i ++ )
390 Card->RxBuffers[i] = (void*)MM_AllocDMA(1, 32, &physaddr);
391 if( !Card->RxBuffers[i] ) {
392 return MODULE_ERR_MALLOC;
394 for( int j = 0; j < RXBUF_PER_PAGE; j ++ )
396 Card->RxQueue[i*RXBUF_PER_PAGE+j].Buffer = physaddr;
397 physaddr += RXBUFLEN;
401 // Initialise semaphores
402 Semaphore_Init(&Card->TxDescSem, TLEN, TLEN, "PCnet3", "Tx Descriptors");
403 Semaphore_Init(&Card->ReadSemaphore, 0, RLEN, "PCnet3", "Rx Descriptors");
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;
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));
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);
429 void PCnet3_IRQHandler(int Num, void *Ptr)
432 Uint16 status = _ReadCSR(card, CSR_STATUS);
434 LOG("status = 0x%02x", status);
435 status &= ~CSR_STATUS_INTR; // Read-only bit
438 // META - Check LAPPEN bit in CSR3
439 if( status & CSR_STATUS_RINT )
441 // TODO: Avoid issues when two packets arrive in one interrupt time
442 Semaphore_Signal(&card->ReadSemaphore, 1);
446 if( status & CSR_STATUS_TINT )
449 for( idx = card->FirstUsedTxD; idx != card->FirstFreeTx; idx = (idx+1)%TLEN )
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 )
456 if( td->Flags1 & (1<<30) )
458 LOG(" Flags0=0x%08x %s%s%s%s%s%s%i",
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 " : "",
469 if( td->Flags1 & TXDESC_FLG1_STP )
470 IPStack_Buffer_UnlockBuffer( card->TxQueueBuffers[idx] );
471 Semaphore_Signal(&card->TxDescSem, 1);
473 card->FirstUsedTxD = idx;
476 if( status & CSR_STATUS_IDON )
478 Log_Debug("PCnet3", "Card %p initialisation done", card);
482 if( status & 0xBC00 )
484 Log_Notice("PCnet3", "Error on %p: %s%s%s%s",
486 (status & (1<<15)) ? "ERR " : "",
487 (status & (1<<13)) ? "CERR " : "",
488 (status & (1<<12)) ? "MISS " : "",
489 (status & (1<<11)) ? "MERR " : ""
493 _WriteCSR(card, CSR_STATUS, status);
496 void PCnet3_ReleaseRxD(void *Arg, size_t HeadLen, size_t FootLen, const void *Data)
500 rd->Flags |= RXDESC_FLG_OWN;
503 static Uint16 _ReadCSR(tCard *Card, Uint8 Reg)
505 outd(Card->IOBase + REG_RAP, Reg);
506 return ind(Card->IOBase + REG_RDP);
508 static void _WriteCSR(tCard *Card, Uint8 Reg, Uint16 Value)
510 outd(Card->IOBase + REG_RAP, Reg);
511 outd(Card->IOBase + REG_RDP, Value);
513 static Uint16 _ReadBCR(tCard *Card, Uint8 Reg)
515 outd(Card->IOBase + REG_RAP, Reg);
516 return ind(Card->IOBase + REG_BDP);
518 void _WriteBCR(tCard *Card, Uint8 Reg, Uint16 Value)
520 outd(Card->IOBase + REG_RAP, Reg);
521 outd(Card->IOBase + REG_BDP, Value);