21 #include "Rtl8139Constants.h" 22 #include "modules/system/network-stack/NetworkStack.h" 23 #include "pedigree/kernel/Log.h" 24 #include "pedigree/kernel/machine/IrqManager.h" 25 #include "pedigree/kernel/machine/Machine.h" 26 #include "pedigree/kernel/machine/Network.h" 27 #include "pedigree/kernel/processor/Processor.h" 30 :
Network(pDev), m_pBase(0), m_StationInfo(), m_RxCurr(0), m_TxCurr(0),
31 m_RxLock(false), m_TxLock(), m_pRxBuffVirt(0), m_pTxBuffVirt(0),
32 m_pRxBuffPhys(0), m_pTxBuffPhys(0), m_RxBuffMR(
"rtl8139-rxbuffer"),
33 m_TxBuffMR(
"rtl8139-txbuffer")
35 setSpecificType(
String(
"rtl8139-card"));
43 ERROR(
"RTL8139: Couldn't allocate Rx Buffer!");
48 RTL_BUFF_SIZE / PAGE_SIZE + (RTL_BUFF_SIZE % PAGE_SIZE ? 1 : 0),
51 ERROR(
"RTL8139: Couldn't allocate Tx Buffer!");
55 m_pRxBuffVirt =
static_cast<uint8_t *
>(m_RxBuffMR.virtualAddress());
56 m_pTxBuffVirt =
static_cast<uint8_t *
>(m_TxBuffMR.virtualAddress());
57 m_pRxBuffPhys = m_RxBuffMR.physicalAddress();
58 m_pTxBuffPhys = m_TxBuffMR.physicalAddress();
61 m_pBase = m_Addresses[0]->m_Io;
67 for (
int i = 0; i < 6; i++)
68 m_StationInfo.mac.setMac(m_pBase->read16(RTL_MAC + i), i);
71 "RTL8139: MAC is " << m_StationInfo.mac[0] <<
":" 72 << m_StationInfo.mac[1] <<
":" 73 << m_StationInfo.mac[2] <<
":" 74 << m_StationInfo.mac[3] <<
":" 75 << m_StationInfo.mac[4] <<
":" 76 << m_StationInfo.mac[5] <<
".");
80 getInterruptNumber(), static_cast<IrqHandler *>(
this));
95 ByteSet(m_pRxBuffVirt, 0, RTL_BUFF_SIZE);
96 ByteSet(m_pTxBuffVirt, 0, RTL_BUFF_SIZE);
99 m_pBase->write8(RTL_CMD_RES, RTL_CMD);
100 while (m_pBase->read8(RTL_CMD) & RTL_CMD_RES)
104 m_pBase->write8(RTL_CFG9346_UNLOCK, RTL_CFG9346);
107 m_pBase->write8(RTL_CMD_RXEN | RTL_CMD_TXEN, RTL_CMD);
110 m_pBase->write8(0x00, RTL_CFG1);
113 m_pBase->write32(static_cast<uint32_t>(m_pRxBuffPhys), RTL_RXBUFF);
116 m_pBase->write8(0x00, RTL_RXMIS);
119 m_pBase->write16(RTL_BMCR_SPEED | RTL_BMCR_ANE | RTL_BMCR_DUPLEX, RTL_BMCR);
122 m_pBase->write8(RTL_MSR_RXFCE, RTL_MSR);
126 RTL_RXCFG_FTH_NONE | RTL_RXCFG_RBLN_64K | RTL_RXCFG_MDMA_UNLM |
127 RTL_RXCFG_AR | RTL_RXCFG_AB | RTL_RXCFG_AM | RTL_RXCFG_APM |
130 m_pBase->write32(RTL_TXCFG_MDMA_2K | RTL_TXCFG_RR_48, RTL_TXCFG);
133 m_pBase->write32(0xFFFFFFFF, RTL_MAR);
134 m_pBase->write32(0xFFFFFFFF, RTL_MAR + 4);
137 m_pBase->write8(RTL_CFG9346_LOCK, RTL_CFG9346);
140 m_pBase->write8(RTL_CMD_RXEN | RTL_CMD_TXEN, RTL_CMD);
143 m_pBase->write16(RTL_IMR_RXOK | RTL_IMR_RXERR, RTL_IMR);
144 m_pBase->write16(0xffff, RTL_ISR);
153 if (nBytes > RTL_PACK_MAX)
155 ERROR(
"RTL8139: Attempt to send a packet with size > 64 KB");
160 MemoryCopy(m_pTxBuffVirt, reinterpret_cast<void *>(buffer), nBytes);
161 for (
int i = nBytes; i < RTL_BUFF_SIZE; i++)
162 m_pTxBuffVirt[i] = 0;
166 static_cast<uint32_t>(m_pTxBuffPhys), RTL_TXADDR0 + m_TxCurr * 4);
167 m_pBase->write32(0x3F0000 | (nBytes & 0x1FFF), RTL_TXSTS0 + m_TxCurr * 4);
182 uintptr_t rxPacket =
reinterpret_cast<uintptr_t
>(m_pRxBuffVirt + m_RxCurr);
183 uint16_t status = *(
reinterpret_cast<uint16_t *
>(rxPacket));
185 uint16_t length = *(
reinterpret_cast<uint16_t *
>(rxPacket + 2));
188 if (!(status & RTL_RXSTS_RXOK) ||
189 (status & (RTL_RXSTS_ISE | RTL_RXSTS_CRC | RTL_RXSTS_FAE)) ||
190 (length >= RTL_PACK_MAX) || (length < RTL_PACK_MIN))
193 "RTL8139: Bad packet: len: " << length <<
", status: " << status
199 uint8_t *packBuff =
new uint8_t[length - 4];
202 if (m_RxCurr + length > RTL_BUFF_SIZE)
206 uint32_t left = RTL_BUFF_SIZE - (m_RxCurr + 4);
207 MemoryCopy(packBuff, reinterpret_cast<void *>(rxPacket + 4), left);
208 MemoryCopy(&packBuff[left], m_pRxBuffVirt, length - 4 - left);
211 m_RxCurr = (length - left + 3) & ~3;
217 packBuff, reinterpret_cast<void *>(rxPacket + 4), length - 4);
220 m_RxCurr = (m_RxCurr + length + 4 + 3) & ~3;
223 m_RxCurr %= RTL_BUFF_SIZE;
227 length - 4, reinterpret_cast<uintptr_t>(packBuff),
this, 0);
235 if (m_StationInfo.dnsServers)
236 delete[] m_StationInfo.dnsServers;
239 m_StationInfo.ipv4 = info.ipv4;
241 "RTL8139: Setting ipv4, " << info.ipv4.toString() <<
", " 242 << m_StationInfo.ipv4.toString() <<
"...");
243 m_StationInfo.ipv6 = info.ipv6;
245 m_StationInfo.subnetMask = info.subnetMask;
247 "RTL8139: Setting subnet mask, " << info.subnetMask.toString() <<
", " 248 << m_StationInfo.subnetMask.toString()
250 m_StationInfo.gateway = info.
gateway;
252 "RTL8139: Setting gateway, " << info.
gateway.toString() <<
", " 253 << m_StationInfo.gateway.toString()
257 m_StationInfo.dnsServers = info.dnsServers;
260 "RTL8139: Setting DNS servers [" <<
Dec << m_StationInfo.nDnsServers
261 <<
Hex <<
" servers being set]...");
268 return m_StationInfo;
273 return !(m_pBase->read8(RTL_MSR) & RTL_MSR_LINK);
281 uint16_t irqStatus = m_pBase->read16(RTL_ISR);
282 m_pBase->write16(irqStatus, RTL_ISR);
285 if ((irqStatus & (RTL_ISR_RXOK | RTL_ISR_TXOK | RTL_ISR_RXERR |
286 RTL_ISR_TXERR)) == 0)
290 if (irqStatus & RTL_ISR_RXOK)
294 if (irqStatus & RTL_ISR_RXERR)
300 if (irqStatus & RTL_ISR_TXERR)
static PhysicalMemoryManager & instance()
static const size_t continuous
virtual bool isConnected()
virtual bool irq(irq_id_t number, InterruptState &state)
IpAddress gateway
Automatically calculated?
void receive(size_t nBytes, uintptr_t packet, Network *pCard, uint32_t offset)
size_t nDnsServers
Can contain IPv6 addresses.
virtual StationInfo getStationInfo()
static NetworkStack & instance()
void registerDevice(Network *pDevice)
virtual irq_id_t registerIsaIrqHandler(uint8_t irq, IrqHandler *handler, bool bEdge=false)=0
virtual bool send(size_t nBytes, uintptr_t buffer)