2 * Acess2 E1000 Network Driver
3 * - By John Hodge (thePowersGang)
6 * - Intel 8254x Network Card Driver (core)
9 #define VERSION VER2(0,1)
14 #include <IPStack/include/adapters_api.h>
15 #include <timers.h> // Time_Delay
17 const struct sSupportedCard {
18 Uint16 Vendor, Device;
19 } caSupportedCards[] = {
20 {0x8086, 0x100E}, // 82540EM-A Desktop
21 {0x8086, 0x1010}, // 82546EB-A1 Copper Dual Port
22 {0x8086, 0x1012}, // 82546EB-A1 Fiber
23 {0x8086, 0x1019}, // 82547[EG]I Copper
24 {0x8086, 0x101A}, // 82547EI Mobile
25 {0x8086, 0x101D}, // 82546EB-A1 Copper Quad Port
27 const int ciNumSupportedCards = sizeof(caSupportedCards)/sizeof(caSupportedCards[0]);
30 int E1000_Install(char **Arguments);
31 int E1000_Cleanup(void);
32 tIPStackBuffer *E1000_WaitForPacket(void *Ptr);
33 int E1000_SendPacket(void *Ptr, tIPStackBuffer *Buffer);
34 void E1000_IRQHandler(int Num, void *Ptr);
35 int E1000_int_InitialiseCard(tCard *Card);
36 Uint16 E1000_int_ReadEEPROM(tCard *Card, Uint8 WordIdx);
39 MODULE_DEFINE(0, VERSION, E1000, E1000_Install, E1000_Cleanup, NULL);
40 tIPStack_AdapterType gE1000_AdapterType = {
42 .Type = ADAPTERTYPE_ETHERNET_1G, // TODO: Differentiate differnet wire protos and speeds
43 .Flags = ADAPTERFLAG_OFFLOAD_MAC, // TODO: IP/TCP/UDP checksum offloading
44 .SendPacket = E1000_SendPacket,
45 .WaitForPacket = E1000_WaitForPacket
50 int E1000_Install(char **Arguments)
53 for( int modelidx = 0; modelidx < ciNumSupportedCards; modelidx ++ )
55 const struct sSupportedCard *cardtype = &caSupportedCards[modelidx];
56 card_count += PCI_CountDevices(cardtype->Vendor, cardtype->Device);
58 LOG("card_count = %i", card_count);
59 if( card_count == 0 ) {
60 LOG("Zero cards located");
61 return MODULE_ERR_NOTNEEDED;
64 // Allocate card array
65 gaE1000_Cards = calloc(sizeof(tCard), card_count);
66 if( !gaE1000_Cards ) {
67 return MODULE_ERR_MALLOC;
72 for( int modelidx = 0; modelidx < ciNumSupportedCards; modelidx ++ )
74 const struct sSupportedCard *cardtype = &caSupportedCards[modelidx];
75 for( int id = -1, i = 0; (id = PCI_GetDevice(cardtype->Vendor, cardtype->Device, i)) != -1; i ++ )
77 tCard *card = &gaE1000_Cards[card_idx++];
78 card->MMIOBasePhys = PCI_GetValidBAR(id, 0, PCI_BARTYPE_MEMNP);
79 if( !card->MMIOBasePhys ) {
80 Log_Warning("E1000", "Dev %i: BAR0 should be non-prefetchable memory", id);
84 card->IRQ = PCI_GetIRQ(id);
85 IRQ_AddHandler(card->IRQ, E1000_IRQHandler, card);
86 PCI_SetCommand(id, PCI_CMD_MEMENABLE|PCI_CMD_BUSMASTER, 0);
88 Log_Debug("E1000", "Card %i: %P IRQ %i", card_idx, card->MMIOBasePhys, card->IRQ);
90 if( E1000_int_InitialiseCard(card) ) {
91 return MODULE_ERR_MALLOC;
94 card->IPStackHandle = IPStack_Adapter_Add(&gE1000_AdapterType, card, card->MacAddr);
100 int E1000_Cleanup(void)
105 void E1000_int_ReleaseRXD(void *Arg, size_t HeadLen, size_t FootLen, const void *Data)
107 tCard **cardptr = Arg;
108 tCard *Card = *cardptr;
109 int rxd = (Arg - (void*)Card->RXBackHandles) / sizeof(void*);
111 LOG("RXD %p %i being released", Card, rxd);
112 ASSERT(rxd >= 0 && rxd < NUM_RX_DESC);
114 Card->RXDescs[rxd].Status = 0;
115 Mutex_Acquire(&Card->lRXDescs);
116 if( rxd == REG32(Card, REG_RDT) ) {
117 while( rxd != Card->FirstUnseenRXD && !(Card->RXDescs[rxd].Status & RXD_STS_DD) ) {
119 if( rxd == NUM_RX_DESC )
122 REG32(Card, REG_RDT) = rxd;
123 LOG("Updated RDT=%i", rxd);
125 Mutex_Release(&Card->lRXDescs);
128 tIPStackBuffer *E1000_WaitForPacket(void *Ptr)
132 if( Semaphore_Wait(&Card->AvailPackets, 1) != 1 )
137 Mutex_Acquire(&Card->lRXDescs);
138 int first_rxd = Card->FirstUnseenRXD;
139 int last_rxd = first_rxd;
141 while( last_rxd != Card->LastUnseenRXD ) {
142 if( !(Card->RXDescs[last_rxd].Status & RXD_STS_DD) )
143 break; // Oops, should ahve found an EOP first
144 if( Card->RXDescs[last_rxd].Status & RXD_STS_EOP )
147 last_rxd = (last_rxd + 1) % NUM_RX_DESC;
149 Card->FirstUnseenRXD = (last_rxd + 1) % NUM_RX_DESC;
150 Mutex_Release(&Card->lRXDescs);
152 LOG("nDesc = %i, first_rxd = %i", nDesc, first_rxd);
153 tIPStackBuffer *ret = IPStack_Buffer_CreateBuffer(nDesc);
155 for( int i = 0; i < nDesc; i ++ )
157 IPStack_Buffer_AppendSubBuffer(ret, 0, Card->RXDescs[rxd].Length, Card->RXBuffers[rxd],
158 E1000_int_ReleaseRXD, &Card->RXBackHandles[rxd]);
165 int E1000_SendPacket(void *Ptr, tIPStackBuffer *Buffer)
169 ENTER("pPtr pBuffer", Ptr, Buffer);
174 // Count sub-buffers (including splitting cross-page entries)
176 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &len, &ptr)) != -1 )
178 if( len > PAGE_SIZE ) {
179 LOG("len=%i > PAGE_SIZE", len);
183 if( MM_GetPhysAddr(ptr) + len-1 != MM_GetPhysAddr((char*)ptr + len-1) ) {
184 LOG("Buffer %p+%i spans non-contig physical pages", ptr, len);
190 // Request set of TX descriptors
191 int rv = Semaphore_Wait(&Card->FreeTxDescs, nDesc);
196 Mutex_Acquire(&Card->lTXDescs);
197 int first_txd = Card->FirstFreeTXD;
198 Card->FirstFreeTXD = (first_txd + nDesc) % NUM_TX_DESC;
199 int last_txd = (first_txd + nDesc-1) % NUM_TX_DESC;
201 LOG("first_txd = %i, last_txd = %i", first_txd, last_txd);
206 while( (idx = IPStack_Buffer_GetBuffer(Buffer, idx, &len, &ptr)) != -1 )
208 //Debug_HexDump("E100 SendPacket", ptr, len);
209 if( MM_GetPhysAddr(ptr) + len-1 != MM_GetPhysAddr((char*)ptr + len-1) )
211 size_t remlen = PAGE_SIZE - ((tVAddr)ptr & (PAGE_SIZE-1));
214 Card->TXDescs[txd].Buffer = MM_GetPhysAddr(ptr);
215 Card->TXDescs[txd].Length = remlen;
216 Card->TXDescs[txd].CMD = TXD_CMD_RS;
217 txd = (txd + 1) % NUM_TX_DESC;
219 Card->TXDescs[txd].Buffer = MM_GetPhysAddr((char*)ptr + remlen);
220 Card->TXDescs[txd].Length = len - remlen;
221 Card->TXDescs[txd].CMD = TXD_CMD_RS;
226 volatile tTXDesc *txdp = &Card->TXDescs[txd];
227 txdp->Buffer = MM_GetPhysAddr(ptr);
229 txdp->CMD = TXD_CMD_RS;
230 LOG("%P: %llx %x %x", MM_GetPhysAddr((void*)txdp), txdp->Buffer, txdp->Length, txdp->CMD);
232 txd = (txd + 1) % NUM_TX_DESC;
234 Card->TXDescs[last_txd].CMD |= TXD_CMD_EOP|TXD_CMD_IDE|TXD_CMD_IFCS;
235 Card->TXSrcBuffers[last_txd] = Buffer;
237 __sync_synchronize();
240 volatile tTXDesc *txdp = Card->TXDescs + last_txd;
241 LOG("%p %P: %llx %x %x", txdp, MM_GetPhysAddr((void*)txdp), txdp->Buffer, txdp->Length, txdp->CMD);
242 volatile tTXDesc *txdp_base = MM_MapTemp(MM_GetPhysAddr((void*)Card->TXDescs));
243 txdp = txdp_base + last_txd;
244 LOG("%p %P: %llx %x %x", txdp, MM_GetPhysAddr((void*)txdp), txdp->Buffer, txdp->Length, txdp->CMD);
245 MM_FreeTemp( (void*)txdp_base);
249 IPStack_Buffer_LockBuffer(Buffer);
250 LOG("Triggering TX - Buffers[%i]=%p", last_txd, Buffer);
251 REG32(Card, REG_TDT) = Card->FirstFreeTXD;
252 Mutex_Release(&Card->lTXDescs);
253 LOG("Waiting for TX to complete");
255 // Wait for completion (lock will block, then release straight away)
256 IPStack_Buffer_LockBuffer(Buffer);
257 IPStack_Buffer_UnlockBuffer(Buffer);
259 // TODO: Check status bits
265 void E1000_IRQHandler(int Num, void *Ptr)
269 Uint32 icr = REG32(Card, REG_ICR);
272 LOG("icr = %x", icr);
274 // Transmit descriptor written
275 if( (icr & ICR_TXDW) || (icr & ICR_TXQE) )
278 int txd = Card->LastFreeTXD;
279 int nReleasedAtLastDD = 0;
280 int idxOfLastDD = txd;
281 // Walk descriptors looking for the first non-complete descriptor
282 LOG("TX %i:%i", Card->LastFreeTXD, Card->FirstFreeTXD);
283 while( txd != Card->FirstFreeTXD )
286 if(Card->TXDescs[txd].Status & TXD_STS_DD) {
287 nReleasedAtLastDD = nReleased;
291 if(txd == NUM_TX_DESC)
294 if( nReleasedAtLastDD )
297 txd = Card->LastFreeTXD;
298 LOG("TX unlocking range %i-%i", txd, idxOfLastDD);
299 while( txd != (idxOfLastDD+1)%NUM_TX_DESC )
301 if( Card->TXSrcBuffers[txd] ) {
302 LOG("- Unlocking %i:%p", txd, Card->TXSrcBuffers[txd]);
303 IPStack_Buffer_UnlockBuffer( Card->TXSrcBuffers[txd] );
304 Card->TXSrcBuffers[txd] = NULL;
307 if(txd == NUM_TX_DESC)
311 Card->LastFreeTXD = txd;
312 Semaphore_Signal(&Card->FreeTxDescs, nReleasedAtLastDD);
313 LOG("nReleased = %i", nReleasedAtLastDD);
317 LOG("No completed TXDs");
323 // Link status change
325 // TODO: Detect link drop/raise and poke IPStack
333 // Pending packet (s)
337 LOG("RX %i:%i", Card->LastUnseenRXD, Card->FirstUnseenRXD);
338 while( (Card->RXDescs[Card->LastUnseenRXD].Status & RXD_STS_DD) )
340 if( Card->RXDescs[Card->LastUnseenRXD].Status & RXD_STS_EOP )
342 Card->LastUnseenRXD ++;
343 if( Card->LastUnseenRXD == NUM_RX_DESC )
344 Card->LastUnseenRXD = 0;
346 if( Card->LastUnseenRXD == Card->FirstUnseenRXD )
349 Semaphore_Signal(&Card->AvailPackets, nPackets);
350 LOG("nPackets = %i", nPackets);
353 // Transmit Descriptor Low Threshold hit
354 if( icr & ICR_TXD_LOW )
359 // Receive Descriptor Minimum Threshold Reached
360 // - We're reading too slow
361 if( icr & ICR_RXDMT0 )
363 LOG("RX descs running out");
366 icr &= ~(ICR_RXT0|ICR_LSC|ICR_TXQE|ICR_TXDW|ICR_TXD_LOW|ICR_RXDMT0);
368 Log_Warning("E1000", "Unhandled ICR bits 0x%x", icr);
371 // TODO: Move this function into Kernel/drvutil.c
373 * \brief Allocate a set of buffers in hardware mapped space
375 * Allocates \a NumBufs buffers using shared pages (if \a BufSize is less than a page) or
376 * as a set of contiugious allocations.
378 int DrvUtil_AllocBuffers(void **Buffers, int NumBufs, int PhysBits, size_t BufSize)
380 if( BufSize >= PAGE_SIZE )
382 const int pages_per_buf = BufSize / PAGE_SIZE;
383 ASSERT(pages_per_buf * PAGE_SIZE == BufSize);
384 for( int i = 0; i < NumBufs; i ++ ) {
385 Buffers[i] = (void*)MM_AllocDMA(pages_per_buf, PhysBits, NULL);
386 if( !Buffers[i] ) return 1;
392 const int bufs_per_page = PAGE_SIZE / BufSize;
393 ASSERT(bufs_per_page * BufSize == PAGE_SIZE);
395 for( int i = 0; i < NumBufs; i ++ )
398 page = (void*)MM_AllocDMA(1, PhysBits, NULL);
399 if( !page ) return 1;
401 Buffers[i] = (char*)page + ofs;
403 if( ofs >= PAGE_SIZE )
410 int E1000_int_InitialiseCard(tCard *Card)
412 ENTER("pCard", Card);
414 // Map required structures
415 Card->MMIOBase = (void*)MM_MapHWPages( Card->MMIOBasePhys, 7 );
416 if( !Card->MMIOBase ) {
417 Log_Error("E1000", "%p: Failed to map MMIO Space (7 pages)", Card);
422 // --- Read MAC address from EEPROM ---
425 macword = E1000_int_ReadEEPROM(Card, 0);
426 Card->MacAddr[0] = macword & 0xFF;
427 Card->MacAddr[1] = macword >> 8;
428 macword = E1000_int_ReadEEPROM(Card, 1);
429 Card->MacAddr[2] = macword & 0xFF;
430 Card->MacAddr[3] = macword >> 8;
431 macword = E1000_int_ReadEEPROM(Card, 2);
432 Card->MacAddr[4] = macword & 0xFF;
433 Card->MacAddr[5] = macword >> 8;
435 Log_Log("E1000", "%p: MAC Address %02x:%02x:%02x:%02x:%02x:%02x",
437 Card->MacAddr[0], Card->MacAddr[1],
438 Card->MacAddr[2], Card->MacAddr[3],
439 Card->MacAddr[4], Card->MacAddr[5]);
441 // --- Prepare for RX ---
442 LOG("RX Preparation");
443 Card->RXDescs = (void*)MM_AllocDMA(1, 64, NULL);
444 if( !Card->RXDescs ) {
448 if( DrvUtil_AllocBuffers(Card->RXBuffers, NUM_RX_DESC, 64, RX_DESC_BSIZE) ) {
452 for( int i = 0; i < NUM_RX_DESC; i ++ )
454 Card->RXDescs[i].Buffer = MM_GetPhysAddr(Card->RXBuffers[i]);
455 Card->RXDescs[i].Status = 0; // Clear RXD_STS_DD, gives it to the card
456 Card->RXBackHandles[i] = Card;
459 REG64(Card, REG_RDBAL) = MM_GetPhysAddr((void*)Card->RXDescs);
460 REG32(Card, REG_RDLEN) = NUM_RX_DESC * 16;
461 REG32(Card, REG_RDH) = 0;
462 REG32(Card, REG_RDT) = NUM_RX_DESC;
463 // Hardware size, Multicast promisc, Accept broadcast, Interrupt at 1/4 Rx descs free
464 REG32(Card, REG_RCTL) = RX_DESC_BSIZEHW | RCTL_MPE | RCTL_BAM | RCTL_RDMTS_1_4;
465 Card->FirstUnseenRXD = 0;
466 Card->LastUnseenRXD = 0;
468 // --- Prepare for TX ---
469 LOG("TX Preparation");
470 Card->TXDescs = (void*)MM_AllocDMA(1, 64, NULL);
471 if( !Card->RXDescs ) {
475 LOG("Card->RXDescs = %p [%P]", Card->TXDescs, MM_GetPhysAddr((void*)Card->TXDescs));
476 for( int i = 0; i < NUM_TX_DESC; i ++ )
478 Card->TXDescs[i].Buffer = 0;
479 Card->TXDescs[i].CMD = 0;
481 REG64(Card, REG_TDBAL) = MM_GetPhysAddr((void*)Card->TXDescs);
482 REG32(Card, REG_TDLEN) = NUM_TX_DESC * 16;
483 REG32(Card, REG_TDH) = 0;
484 REG32(Card, REG_TDT) = 0;
485 // Enable, pad short packets
486 REG32(Card, REG_TCTL) = TCTL_EN | TCTL_PSP | (0x0F << TCTL_CT_ofs) | (0x40 << TCTL_COLD_ofs);
487 Card->FirstFreeTXD = 0;
489 // -- Prepare Semaphores
490 Semaphore_Init(&Card->FreeTxDescs, NUM_TX_DESC, NUM_TX_DESC, "E1000", "TXDescs");
491 Semaphore_Init(&Card->AvailPackets, 0, NUM_RX_DESC, "E1000", "RXDescs");
493 // --- Prepare for full operation ---
494 LOG("Starting card");
495 REG32(Card, REG_CTRL) = CTRL_SLU|CTRL_ASDE; // Link up, auto speed detection
496 REG32(Card, REG_IMS) = 0x1F6DC; // Interrupt mask
497 (void)REG32(Card, REG_ICR); // Discard pending interrupts
498 REG32(Card, REG_RCTL) |= RCTL_EN;
503 Uint16 E1000_int_ReadEEPROM(tCard *Card, Uint8 WordIdx)
505 REG32(Card, REG_EERD) = ((Uint32)WordIdx << 8) | 1;
507 while( !((tmp = REG32(Card, REG_EERD)) & (1 << 4)) ) {
508 // TODO: use something like Time_MicroDelay instead