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 = ADAPTERTYPE_ETHERNET_100M,
74 //.Flags = ADAPTERFLAG_OFFLOAD_MAC,
76 .SendPacket = PCnet3_SendPacket,
77 .WaitForPacket = PCnet3_WaitForPacket
79 int giPCnet3_CardCount;
80 tCard *gaPCnet3_Cards;
82 tInitBlock32 gPCnet3_StaticInitBlock;
83 tInitBlock32 *gpPCnet3_InitBlock;
87 * \brief Installs the PCnet3 Driver
89 int PCnet3_Install(char **Options)
96 giPCnet3_CardCount = PCI_CountDevices(VENDOR_ID, DEVICE_ID);
97 Log_Debug("PCnet3", "%i cards", giPCnet3_CardCount);
99 if( giPCnet3_CardCount == 0 ) return MODULE_ERR_NOTNEEDED;
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)
105 || MM_GetPhysAddr(gpPCnet3_InitBlock) > (1ULL<<32)
110 Log_Error("PCnet3", "TODO: Support 64-bit init / spanning init");
111 return MODULE_ERR_MISC;
114 gaPCnet3_Cards = calloc( giPCnet3_CardCount, sizeof(tCard) );
116 while( (id = PCI_GetDevice(VENDOR_ID, DEVICE_ID, i)) != -1 )
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 );
124 Log_Warning("PCnet3", "Driver does not support MMIO, skipping card");
131 card->IRQ = PCI_GetIRQ( id );
133 // Switch the card into DWord mode
134 // - TODO: Should the value of RAP matter here?
135 outd(card->IOBase + REG_RDP, 0);
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;
148 // Install IRQ Handler
149 IRQ_AddHandler(card->IRQ, PCnet3_IRQHandler, card);
151 // Initialise the card state
152 PCnet3_int_InitCard(card);
155 card->IPStackHandle = IPStack_Adapter_Add(&gPCnet3_AdapterType, card, card->MacAddr);
160 if( gpPCnet3_InitBlock != &gPCnet3_StaticInitBlock ) {
161 MM_UnmapHWPages( (tVAddr)gpPCnet3_InitBlock, 1 );
164 return MODULE_ERR_OK;
167 int PCnet3_Cleanup(void)
169 // TODO: Kill IPStack adapters and clean up
173 // --- Root Functions ---
174 tIPStackBuffer *PCnet3_WaitForPacket(void *Ptr)
180 if( Semaphore_Wait( &card->ReadSemaphore, 1 ) != 1 )
182 LEAVE_RET('n', NULL);
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) )
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);
199 nextp_td = (nextp_td+1) % RLEN;
200 assert(nextp_td != first_td);
202 nextp_td = (nextp_td+1) % RLEN;
203 card->RxPos = nextp_td;
204 Mutex_Release( &card->lRxPos );
206 int nDesc = (nextp_td - first_td + RLEN) % RLEN;
208 // Create buffer structure
209 // TODO: Could be more efficient by checking for buffers in the same page / fully contig allocations
211 tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(nDesc);
212 for( int idx = first_td; idx != nextp_td; idx = (idx+1) % RLEN )
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);
223 int PCnet3_int_FillTD(tTxDesc_3 *td, Uint32 BufAddr, Uint32 Len, int bBounced)
226 td->Flags1 = 0xF000 | (4096 - (Len & 0xFFF));
227 td->Buffer = BufAddr;
228 td->_avail = bBounced;
232 int PCnet3_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
236 if( IPStack_Buffer_GetLength(Buffer) > 1500 ) {
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
246 const void *sbuf_ptr;
248 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
252 if( MM_GetPhysAddr(sbuf_ptr) > (1ULL<<32) )
253 ; // will be bounce-buffered
256 if( MM_GetPhysAddr(sbuf_ptr)+sbuf_len-1 != MM_GetPhysAddr(sbuf_ptr+sbuf_len-1) )
263 // - Obtain enough descriptors
264 int rv = Semaphore_Wait(&card->TxDescSem, nDesc);
266 Log_Notice("PCnet3", "Semaphore wait interrupted, restoring %i descriptors");
267 Semaphore_Signal(&card->TxDescSem, rv);
268 LEAVE_RET('i', EINTR);
270 Mutex_Acquire(&card->lTxPos);
271 int first_desc = card->FirstFreeTx;
272 card->FirstFreeTx = (card->FirstFreeTx + nDesc) % TLEN;
273 Mutex_Release(&card->lTxPos);
275 // - Set up descriptors
276 int td_idx = first_desc;
277 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &sbuf_len, &sbuf_ptr)) != -1 )
279 tTxDesc_3 *td = &card->TxQueue[td_idx];
280 assert( !(td->Flags1 & TXDESC_FLG1_OWN) );
281 td_idx = (td_idx + 1) % TLEN;
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);
288 if( start_phys > (1ULL<<32) || end_phys > (1ULL<<32) )
290 // TODO: Have a global set of bounce buffers
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);
301 if( start_phys+sbuf_len-1 != end_phys )
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;
308 PCnet3_int_FillTD(td, start_phys, page1_maxsize, 0);
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;
315 LOG("%i: Split (%P,%P)+%i - %p,%p",
316 idx, td->Buffer, td2->Buffer, sbuf_len, td, td2);
320 PCnet3_int_FillTD(td, start_phys, sbuf_len, 0);
321 LOG("%i: Straight %P+%i - %p",
322 idx, td->Buffer, sbuf_len, td);
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;
330 // - Lock buffer before allowing the card to continue
331 IPStack_Buffer_LockBuffer(Buffer);
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;
340 LOG("CSR0=0x%x", _ReadCSR(card, 0));
341 LOG("Transmit started, waiting for completion");
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);
352 int PCnet3_int_InitCard(tCard *Card)
354 // Allocate ring buffers
355 Card->TxQueue = (void*)MM_AllocDMA(1, 32, &Card->TxQueuePhys);
356 if( !Card->TxQueue ) {
357 return MODULE_ERR_MALLOC;
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;
366 Card->RxQueue = Card->TxQueue + TLEN;
367 Card->RxQueuePhys = Card->RxQueuePhys + TLEN*sizeof(*Card->TxQueue);
370 // Allocate Rx buffers
371 for( int i = 0; i < NUM_RXBUF_PAGES; i ++ )
374 Card->RxBuffers[i] = (void*)MM_AllocDMA(1, 32, &physaddr);
375 if( !Card->RxBuffers[i] ) {
376 return MODULE_ERR_MALLOC;
378 for( int j = 0; j < RXBUF_PER_PAGE; j ++ )
380 Card->RxQueue[i*RXBUF_PER_PAGE+j].Buffer = physaddr;
381 physaddr += RXBUFLEN;
385 // Initialise semaphores
386 Semaphore_Init(&Card->TxDescSem, TLEN, TLEN, "PCnet3", "Tx Descriptors");
387 Semaphore_Init(&Card->ReadSemaphore, 0, RLEN, "PCnet3", "Rx Descriptors");
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;
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));
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);
413 void PCnet3_IRQHandler(int Num, void *Ptr)
416 Uint16 status = _ReadCSR(card, CSR_STATUS);
418 LOG("status = 0x%02x", status);
419 status &= ~CSR_STATUS_INTR; // Read-only bit
422 // META - Check LAPPEN bit in CSR3
423 if( status & CSR_STATUS_RINT )
425 // TODO: Avoid issues when two packets arrive in one interrupt time
426 Semaphore_Signal(&card->ReadSemaphore, 1);
430 if( status & CSR_STATUS_TINT )
433 for( idx = card->FirstUsedTxD; idx != card->FirstFreeTx; idx = (idx+1)%TLEN )
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 )
440 if( td->Flags1 & (1<<30) )
442 LOG(" Flags0=0x%08x %s%s%s%s%s%s%i",
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 " : "",
453 if( td->Flags1 & TXDESC_FLG1_STP )
454 IPStack_Buffer_UnlockBuffer( card->TxQueueBuffers[idx] );
455 Semaphore_Signal(&card->TxDescSem, 1);
457 card->FirstUsedTxD = idx;
460 if( status & CSR_STATUS_IDON )
462 Log_Debug("PCnet3", "Card %p initialisation done", card);
463 LOG("CSR15 reads as 0x%x", _ReadCSR(card, 15));
467 if( status & 0xBC00 )
469 Log_Notice("PCnet3", "Error on %p: %s%s%s%s",
471 (status & (1<<15)) ? "ERR " : "",
472 (status & (1<<13)) ? "CERR " : "",
473 (status & (1<<12)) ? "MISS " : "",
474 (status & (1<<11)) ? "MERR " : ""
478 _WriteCSR(card, CSR_STATUS, status);
481 void PCnet3_ReleaseRxD(void *Arg, size_t HeadLen, size_t FootLen, const void *Data)
485 rd->Flags |= RXDESC_FLG_OWN;
488 static Uint16 _ReadCSR(tCard *Card, Uint8 Reg)
490 outd(Card->IOBase + REG_RAP, Reg);
491 return ind(Card->IOBase + REG_RDP);
493 static void _WriteCSR(tCard *Card, Uint8 Reg, Uint16 Value)
495 outd(Card->IOBase + REG_RAP, Reg);
496 outd(Card->IOBase + REG_RDP, Value);
498 static Uint16 _ReadBCR(tCard *Card, Uint8 Reg)
500 outd(Card->IOBase + REG_RAP, Reg);
501 return ind(Card->IOBase + REG_BDP);
503 void _WriteBCR(tCard *Card, Uint8 Reg, Uint16 Value)
505 outd(Card->IOBase + REG_RAP, Reg);
506 outd(Card->IOBase + REG_BDP, Value);