Intel Ethernet i217: Difference between revisions

add maintenance template
[unchecked revision][unchecked revision]
m (→‎Manuals: "Fix" broken link)
(add maintenance template)
 
(10 intermediate revisions by 8 users not shown)
Line 1:
{{FirstPerson}}
== Network Driver for Intel Ethernet Cards I217 and 82577LM ==
 
Line 8 ⟶ 9:
 
To start with, lets state some macro definitions that we are going to use in the code.
<sourcesyntaxhighlight lang="c">
 
#define INTEL_VEND 0x8086 // Vendor ID for Intel
Line 39 ⟶ 40:
 
#define REG_RDTR 0x2820 // RX Delay Timer Register
#define REG_RXDCTL 0x38280x2828 // RX Descriptor Control
#define REG_RADV 0x282C // RX Int. Absolute Delay Timer
#define REG_RSRPD 0x2C00 // RX Small Packet Detect Interrupt
Line 105 ⟶ 106:
#define TSTA_LC (1 << 2) // Late Collision
#define LSTA_TU (1 << 3) // Transmit Underrun
</syntaxhighlight>
</source>
 
Now lets define the data structures for the transmit and receive buffers
 
<sourcesyntaxhighlight lang="c">
 
#define E1000_NUM_RX_DESC 32
Line 133 ⟶ 134:
} __attribute__((packed));
 
</syntaxhighlight>
</source>
 
And finally some helper static methods for MMIO read/write operations and Ports I/O
 
<sourcesyntaxhighlight lang="c">
 
class MMIOUtils
Line 151 ⟶ 152:
static void write64 (uint64_t p_address,uint64_t p_value);
};
 
#endif /* MMIOUTILS_H_ */
 
 
 
Line 192 ⟶ 190:
}
 
</syntaxhighlight>
</source>
 
<sourcesyntaxhighlight lang="c">
#ifndef PORTS_H_
#define PORTS_H_
Line 329 ⟶ 327:
 
 
</syntaxhighlight>
</source>
 
 
== The Driver Class Header (Class Definition)==
 
 
<sourcesyntaxhighlight lang="c">
class E1000 : public NetworkDriver
{
private:
uint8_t bar_type; // Type of BOR0BAR0
uint16_t io_base; // IO Base Address
uint64_t mem_base; // MMIO Base Address
Line 373 ⟶ 370:
~E1000(); // Default Destructor
};
</syntaxhighlight>
</source>
 
== How the Gears Move (Class methods implementation) ==
Line 379 ⟶ 376:
First of all we need to be able to send commands and read results from the NIC. It is important to detect the type of BAR0 and based on that the correct communication mechanism should be adopted. The following two methods encapsulate the read/write commands and uses MMIO or IO ports based on the value in BAR0 which is reflected in bar_type data member flag.
 
<sourcesyntaxhighlight lang="c">
void E1000::writeCommand( uint16_t p_address, uint32_t p_value)
{
Line 404 ⟶ 401:
}
}
</syntaxhighlight>
</source>
 
 
Now we need to detect if the card has an EEPROM or not. The Qemu and Bochs emulate EEPROM, but the I217 and 82577LM do not. The following first method tries to read the status field of the EEPROM, the status field should contain the value 0x10, and based on the result the internal data member eerprom_exists. The second method performs a 2-bytes read operation from the EEPROM
 
<sourcesyntaxhighlight lang="c">
bool E1000::detectEEProm()
{
Line 444 ⟶ 441:
}
 
</syntaxhighlight>
</source>
 
 
{{You|section=1}}
 
The first thing you will need to do after detecting the BAR0 type and the existence of the EEPROM is to read the hardware MAC address of the NIC. The following method reads the hardware mac address based. If an EEPROM exists it will read it from the EEPROM else it will read it from address 0x5400 where it should be located in that case. It is very important to detect if an EEPROM exists or not prior to reading the MAC address.
 
<sourcesyntaxhighlight lang="c">
 
bool E1000::readMACAddress()
Line 482 ⟶ 479:
return true;
}
</syntaxhighlight>
</source>
 
 
Now, we need to configure the transmit and receive descriptor buffers, here are the implementation of the corresponding methods. The rxinit method is identical to the one I use for my e1000 driver. The difference is in txinit
 
<sourcesyntaxhighlight lang="c">
 
void E1000::rxinit()
Line 518 ⟶ 515:
writeCommand(REG_RXDESCTAIL, E1000_NUM_RX_DESC-1);
rx_cur = 0;
writeCommand(REG_RCTRL, RCTL_EN| RCTL_SBP| RCTL_UPE | RCTL_MPE | RCTL_LBM_NONE | RTCL_RDMTS_HALF | RCTL_BAM | RCTL_SECRC | RCTL_BSIZE_2048RCTL_BSIZE_8192);
}
Line 567 ⟶ 564:
 
 
</syntaxhighlight>
</source>
 
 
To enable interrupts
<sourcesyntaxhighlight lang="c">
void E1000::enableInterrupt()
{
Line 579 ⟶ 576:
 
}
</syntaxhighlight>
</source>
 
As we have defined most of the building blocks and the helper methods lets define the main methods of the class.
Line 586 ⟶ 583:
The constructor is responsible for fetching PCI related data and initialize the object internal state
 
<sourcesyntaxhighlight lang="c">
E1000::E1000(PCIConfigHeader * p_pciConfigHeader) : NetworkDriver(p_pciConfigHeader)
{
Line 600 ⟶ 597:
eerprom_exists = false;
}
</syntaxhighlight>
</source>
 
The start method basically detects the EEPROM, reads the MAC addresses, setup rx and tx buffers, register the interrupt handler, and enable NIC interrupts
 
 
<sourcesyntaxhighlight lang="c">
 
bool E1000::start ()
Line 628 ⟶ 625:
 
 
</syntaxhighlight>
</source>
 
 
Your interrupt handler should eventually call the fire method which handles the NIC's events
<sourcesyntaxhighlight lang="c">
 
void E1000::fire (InterruptContext * p_interruptContext)
Line 638 ⟶ 635:
if ( p_interruptContext->getInteruptNumber() == pciConfigHeader->getIntLine()+IRQ0)
{
/* This might be needed here if your handler doesn't clear interrupts from each device and must be done before EOI if using the PIC.
Without this, the card will spam interrupts as the int-line will stay high. */
writeCommand(REG_IMASK, 0x1);
uint32_t status = readCommand(0xc0);
if(status & 0x04)
Line 675 ⟶ 676:
}
 
</syntaxhighlight>
</source>
 
Finally we define the sendPacket method as follows
 
<sourcesyntaxhighlight lang="c">
 
int E1000::sendPacket(const void * p_data, uint16_t p_len)
Line 685 ⟶ 686:
tx_descs[tx_cur]->addr = (uint64_t)p_data;
tx_descs[tx_cur]->length = p_len;
tx_descs[tx_cur]->cmd = CMD_EOP | CMD_IFCS | CMD_RS | CMD_RPS;
tx_descs[tx_cur]->status = 0;
uint8_t old_cur = tx_cur;
Line 694 ⟶ 695:
}
 
</syntaxhighlight>
</source>
 
 
This is an example of how to instantiate an object of this class and startup you driver. I assume that you have scanned your PCI buses and loaded the found devices parameters into some data structures; in our example this is done by the PCIConfigManager class, which is outside the scope of this tutorial
 
<sourcesyntaxhighlight lang="c">
 
pciConfigHeaderManager.initialize(); // Initialize the PCIConfigHeaderManager Object and scan PCI devices
Line 717 ⟶ 718:
}
 
</syntaxhighlight>
</source>
 
 
== Summary and Wrap Up ==
{{You|section=1}}
 
I have presented in this Wiki the steps I followed to make an e1000 driver work with the two e1000e NICs Intel I217 and 82577LM. The wiki does not show how to utilize all the features of the NICs, but basically primitive setup and send/receive packets. Three important issues that I faced: