From 1502cd7ff9db3d82815bdc4f04ac7a55eece3a19 Mon Sep 17 00:00:00 2001 From: Scalpel_cp <165da7fc5536ee16440a98f161bfa866a8b94595G55xazV5> Date: Sat, 29 Mar 2008 11:16:24 +0000 Subject: [PATCH] RTL - Promiscuous mode and some TCR changes. --- source/Boot/KudzuTest/Program.cs | 2 +- source/Cosmos.Build.Windows/Builder.cs | 1 + .../Network/Devices/RTL8139/RTL8139.cs | 61 ++++++++++++++----- .../Register/ReceiveConfigurationRegister.cs | 25 +++++++- .../Register/TransmitConfigurationRegister.cs | 45 +++++++------- source/FrodeTest/Shell/Session.cs | 36 +++++++++-- source/FrodeTest/Test/RTL8139Test.cs | 4 +- 7 files changed, 130 insertions(+), 44 deletions(-) diff --git a/source/Boot/KudzuTest/Program.cs b/source/Boot/KudzuTest/Program.cs index 64f7669d7..41e05b2b7 100644 --- a/source/Boot/KudzuTest/Program.cs +++ b/source/Boot/KudzuTest/Program.cs @@ -34,7 +34,7 @@ namespace KudzuTest { //Cosmos.Kernel.Temp.Kudzu.PCI.Test(); // Load - var xNICs = RTLDriver.RTL8139.FindRTL8139Devices(); + var xNICs = RTLDriver.RTL8139.FindAll(); if (xNICs.Count == 0) { throw new Exception("Unable to find RTL8139 network card!"); } diff --git a/source/Cosmos.Build.Windows/Builder.cs b/source/Cosmos.Build.Windows/Builder.cs index be620657e..2318f5474 100644 --- a/source/Cosmos.Build.Windows/Builder.cs +++ b/source/Cosmos.Build.Windows/Builder.cs @@ -168,6 +168,7 @@ namespace Cosmos.Build.Windows { // Ethernet card - Later the model should be a QEMU option on // options screen + " -net nic,model=rtl8139,macaddr=52:54:00:12:34:57" + //+ " -net tap,ifname=CosmosTAP" //for network testing + " -net user" , ToolsPath + @"qemu\", false, true); diff --git a/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/RTL8139.cs b/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/RTL8139.cs index 7194dffcf..cfe3a403c 100644 --- a/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/RTL8139.cs +++ b/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/RTL8139.cs @@ -17,7 +17,7 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 /// Retrieve all Realtek 8139 network cards found on computer. /// /// - public static List FindRTL8139Devices() + public static List FindAll() { List found = new List(); @@ -39,11 +39,6 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 private byte[] TxBuffer2; private byte[] TxBuffer3; private byte[] RxBuffer; - //private byte[] RxBuffer2 = new byte[2048]; - //private byte[] RxBuffer3 = new byte[2048]; - //private byte[] RxBuffer4 = new byte[2048]; - - public RTL8139(PCIDevice device) { @@ -101,6 +96,7 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 //Setting Receive configuration var rcr = Register.ReceiveConfigurationRegister.Load(pciCard); rcr.Init(); + rcr.PromiscuousMode = true; //Enable IRQ Interrupt SetIRQMaskRegister(); @@ -108,22 +104,43 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 Cosmos.Hardware.PC.Interrupts.IRQ11 = new Cosmos.Hardware.PC.Interrupts.InterruptDelegate(HandleNetworkInterrupt); } + #region Operational properties + /// /// Changes the Loopback mode. /// /// True to enable Loopback. False for normal operation. - public void SetLoopbackMode(bool value) + public bool LoopbackMode { - var tcr = Register.TransmitConfigurationRegister.Load(pciCard); - tcr.LoopbackMode = value; + get + { + var tcr = Register.TransmitConfigurationRegister.Load(pciCard); + return tcr.LoopbackMode; + } + set + { + var tcr = Register.TransmitConfigurationRegister.Load(pciCard); + tcr.LoopbackMode = value; + } } - public bool GetLoopbackMode() + public bool PromiscuousMode { - var tcr = Register.TransmitConfigurationRegister.Load(pciCard); - return tcr.LoopbackMode; + get + { + var rcr = Register.ReceiveConfigurationRegister.Load(pciCard); + return rcr.PromiscuousMode; + } + set + { + var rcr = Register.ReceiveConfigurationRegister.Load(pciCard); + rcr.PromiscuousMode = value; + } + } + #endregion + public override bool QueueBytes(byte[] buffer, int offset, int length) { throw new NotImplementedException(); @@ -155,11 +172,18 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 get { return "Generic RTL8139 Network device"; } } + #region Power and Initilization + + /// + /// Enables RTL network card by setting CONFIG_1 register. + /// + /// public override bool Enable() { //Writes 0x00 to CONFIG_1 registers to enable card - regs.Config1 = 0x00; - return true; + regs.Config1 = 0x00; + + return base.Enable(); //enables PCI card as well } /// @@ -181,6 +205,11 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 Console.WriteLine("Reset Complete!"); } + #endregion + + #region Receive and Interrupt + + /// /// (Should be) Called when the PCI network card raises an Interrupt. /// @@ -221,6 +250,8 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 throw new NotImplementedException(); } + #endregion + /// /// Enable the NIC to be able to Recieve data. /// @@ -369,7 +400,7 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139 return true; } - + public bool TransmitRaw(byte[] aData) { WriteAddressToPCI(ref aData, pciCard.BaseAddress1 + (byte)Register.MainRegister.Bit.TSAD0); diff --git a/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/ReceiveConfigurationRegister.cs b/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/ReceiveConfigurationRegister.cs index cf0df9f63..87dd1eba4 100644 --- a/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/ReceiveConfigurationRegister.cs +++ b/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/ReceiveConfigurationRegister.cs @@ -9,6 +9,7 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139.Register /// /// Receive Configuration Register is used to set receive configuration. /// Offset 44h from main memory. + /// Is 32 bits wide. /// public class ReceiveConfigurationRegister { @@ -34,13 +35,35 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139.Register IOSpace.Write32(rcrAddress, data); } + /// + /// Get or Sets all 32 bits in Receive Configuration Register + /// public UInt32 RCR { get { return IOSpace.Read32(rcrAddress); } - private set { ;} + private set + { + IOSpace.Write32(rcrAddress, value); + } + } + + /// + /// Gets or Sets the promiscuous mode. When Promisuous mode set ALL detected packets on network are put into Receive buffer, not + /// just the packets sent directly to us. + /// + public bool PromiscuousMode + { + get + { + return BinaryHelper.CheckBit(IOSpace.Read32(rcrAddress), 0); + } + set + { + this.RCR = BinaryHelper.FlipBit(this.RCR, 0); + } } public enum BitValue : uint diff --git a/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/TransmitConfigurationRegister.cs b/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/TransmitConfigurationRegister.cs index 7591a49a0..19778b34f 100644 --- a/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/TransmitConfigurationRegister.cs +++ b/source/Cosmos/Cosmos.Hardware/Network/Devices/RTL8139/Register/TransmitConfigurationRegister.cs @@ -14,12 +14,10 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139.Register /// public class TransmitConfigurationRegister { - private UInt32 tcr; private PCIDevice pci; private UInt32 tcrAddress; private TransmitConfigurationRegister(UInt32 data, PCIDevice hw, UInt32 adr) { - tcr = data; pci = hw; tcrAddress = adr; } @@ -35,40 +33,45 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139.Register { //Set Interframe Gap and Max Burst Size (to 128 bytes) UInt32 data = (UInt32)(BitValue.IFG0 | BitValue.IFG1 | BitValue.MAXDMA0 | BitValue.MAXDMA1); - //Console.WriteLine("Data in INIT for TX:" + data); - IOSpace.Write32(tcrAddress, data); + this.TCR = data; } /// - /// Retrieves 6 bits + /// Get or Sets all 32 bits in Transmit Configuration Register + /// + public UInt32 TCR + { + get + { + return IOSpace.Read32(tcrAddress); + } + private set + { + IOSpace.Write32(tcrAddress, value); + } + } + + /// + /// Retrieves a number which indicates the Hardware Revision ID of the RTL card. /// /// public byte GetHWVERID() { byte mask = 249; // 1111 1001 - byte hwverid = BinaryHelper.GetByteFrom32bit(tcr, (byte)(23)); + byte hwverid = BinaryHelper.GetByteFrom32bit(this.TCR, (byte)(23)); return (byte)(mask & hwverid); } - //internal void SetLoopBack(bool value) - //{ - // //Change bits LBK0 and LBK1 to HIGH for Loopback, or LOW for Normal mode. - // if (value) - - - - //} - public bool LoopbackMode { get { - UInt32 data = IOSpace.Read32(tcrAddress); + UInt32 data = this.TCR; bool low = BinaryHelper.CheckBit(data, 17); bool high = BinaryHelper.CheckBit(data, 18); - + if (low != high) - Console.WriteLine("Warning: Loopback bits should always be the same!"); + throw new Exception("Loopback bits are mismatched in RTL drivers TCR PCI register!"); if (low && high) return true; @@ -76,14 +79,14 @@ namespace Cosmos.Hardware.Network.Devices.RTL8139.Register return false; } set - { - UInt32 data = IOSpace.Read32(tcrAddress); + { + UInt32 data = this.TCR; if (value) //turn ON data = (UInt32)(data | (uint)BitValue.LBK0 | (uint)BitValue.LBK1); else //turn OFF data = (UInt32)(data & (uint)~BitValue.LBK0 & (uint)~BitValue.LBK1); - IOSpace.Write32(tcrAddress, data); + this.TCR = data; } } diff --git a/source/FrodeTest/Shell/Session.cs b/source/FrodeTest/Shell/Session.cs index 191011daf..bc2ed9bb6 100644 --- a/source/FrodeTest/Shell/Session.cs +++ b/source/FrodeTest/Shell/Session.cs @@ -34,7 +34,7 @@ namespace FrodeTest.Shell } */ else if(command.Equals("load")) { - var list = RTL8139.FindRTL8139Devices(); + var list = RTL8139.FindAll(); if (list.Count != 0) nic = list[0]; else @@ -46,6 +46,7 @@ namespace FrodeTest.Shell Console.WriteLine(nic.Name); Console.WriteLine("Revision: " + nic.GetHardwareRevision()); Console.WriteLine("MAC: " + nic.MACAddress); + nic.Enable(); nic.InitializeDriver(); @@ -78,6 +79,29 @@ namespace FrodeTest.Shell } //List incomingPackets = nic.Recieve(); } + else if (command.Equals("info")) + { + if (nic == null) + { + Console.WriteLine("Network card not initialized yet."); + return; + } + Console.WriteLine("Network card: " + nic.Name); + Console.WriteLine("Hardware revision: " + nic.GetHardwareRevision()); + Console.WriteLine("MAC Address: " + nic.MACAddress); + Console.WriteLine(); + Console.WriteLine("Loopback enabled?: " + nic.LoopbackMode.ToString()); + Console.WriteLine("NIC enabled?: " + nic.IsEnabled.ToString()); + Console.WriteLine("Promiscuous mode?: " + nic.PromiscuousMode.ToString()); + + int xByteCount = 0; + foreach (byte b in nic.ReadReceiveBuffer()) + { + if (b != 0x00) + xByteCount++; + } + Console.WriteLine("Read buffer contains " + xByteCount.ToString() + " bytes with data."); + } else if (command.Equals("reset")) { //nic.TimerCount = 1; @@ -87,15 +111,19 @@ namespace FrodeTest.Shell } else if (command.Equals("loop")) { - Console.WriteLine("Toggeling loopback mode from : " + nic.GetLoopbackMode().ToString()); - nic.SetLoopbackMode(!nic.GetLoopbackMode()); - Console.WriteLine("to: " + nic.GetLoopbackMode().ToString()); + Console.WriteLine("Toggeling loopback mode from : " + nic.LoopbackMode.ToString()); + nic.LoopbackMode = !nic.LoopbackMode; + Console.WriteLine("to: " + nic.LoopbackMode.ToString()); } else if (command.Equals("crash")) { throw new Exception("User forced an Exception", new Exception("Inner bug")); } + else if (command.Equals("prom")) + { + nic.PromiscuousMode = !nic.PromiscuousMode; + } else if (command.Equals("help")) { diff --git a/source/FrodeTest/Test/RTL8139Test.cs b/source/FrodeTest/Test/RTL8139Test.cs index 344e36544..59d9dbb2b 100644 --- a/source/FrodeTest/Test/RTL8139Test.cs +++ b/source/FrodeTest/Test/RTL8139Test.cs @@ -7,11 +7,12 @@ namespace FrodeTest.Test { public class RTL8139Test { + [Obsolete] public static void RunTest() { // Testing RTL8139 PCI networkcard //Load card - var nics = RTL8139.FindRTL8139Devices(); + var nics = RTL8139.FindAll(); if (nics.Count == 0) { @@ -29,7 +30,6 @@ namespace FrodeTest.Test //Console.WriteLine("BaseAddress0 is : " + pciNic.BaseAddress0); Console.WriteLine("BaseAddress1 is : " + nic.PCICard.BaseAddress1); Console.WriteLine("Enabling card..."); - //nic.SoftReset(); nic.Enable(); Console.WriteLine("Initializing driver..."); nic.InitializeDriver();