Skip to content

Commit

Permalink
Merge pull request CosmosOS#974 from valentinbreiz/fix-PCNETII
Browse files Browse the repository at this point in the history
PCNETII Driver now working again.
  • Loading branch information
jp2masa authored May 8, 2018
2 parents 7ad7083 + 0a06a9a commit 28647a8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 48 deletions.
2 changes: 1 addition & 1 deletion source/Cosmos.Core/PIC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public PIC()
//Init(Master, 0x20, 4, 0xFD | 0x08);
//Init(Slave, 0x28, 2, 0xFF);
//for now enable keyboard, mouse(ps2)
Remap(0x20, 0xF8 | 0x08, 0x28, 0xEF);
Remap(0x20, 0xF8 | 0x08, 0x28, 0xEB);
}

private void Remap(byte masterStart, byte masterMask, byte slaveStart, byte slaveMask)
Expand Down
58 changes: 27 additions & 31 deletions source/Cosmos.HAL2/Drivers/PCI/Network/AMDPCNetII.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
using Cosmos.Core;
using Cosmos.Core.IOGroup.Network;
using Cosmos.HAL.Network;
using IL2CPU.API.Attribs;

namespace Cosmos.HAL.Drivers.PCI.Network
{
public class AMDPCNetII : NetworkDevice
{
protected PCIDeviceNormal pciCard;
protected PCIDevice pciCard;
protected AMDPCNetIIIOGroup io;
protected MACAddress mac;
protected bool mInitDone;
Expand All @@ -24,7 +23,7 @@ public class AMDPCNetII : NetworkDevice
protected Queue<byte[]> mTransmitBuffer;
private int mNextTXDesc;

public AMDPCNetII(PCIDeviceNormal device)
public AMDPCNetII(PCIDevice device)
: base()
{
if (device == null)
Expand All @@ -33,10 +32,10 @@ public AMDPCNetII(PCIDeviceNormal device)
}

this.pciCard = device;
// this.pciCard.Claimed = true;
//this.pciCard.EnableDevice();
this.pciCard.Claimed = true;
this.pciCard.EnableDevice();

//this.io = new AMDPCNetIIIOGroup((ushort) this.pciCard.BaseAddresses[0].BaseAddress());
this.io = new AMDPCNetIIIOGroup((ushort)this.pciCard.BaseAddressBar[0].BaseAddress);
this.io.RegisterData.DWord = 0;

// Get the EEPROM MAC Address and set it as the devices MAC
Expand All @@ -58,8 +57,8 @@ public AMDPCNetII(PCIDeviceNormal device)

mInitBlock.Write32(0x00, (0x4 << 28) | (0x4 << 20));
mInitBlock.Write32(0x04,
(UInt32) (eeprom_mac[0] | (eeprom_mac[1] << 8) | (eeprom_mac[2] << 16) | (eeprom_mac[3] << 24)));
mInitBlock.Write32(0x08, (UInt32) (eeprom_mac[4] | (eeprom_mac[5] << 8)));
(UInt32)(eeprom_mac[0] | (eeprom_mac[1] << 8) | (eeprom_mac[2] << 16) | (eeprom_mac[3] << 24)));
mInitBlock.Write32(0x08, (UInt32)(eeprom_mac[4] | (eeprom_mac[5] << 8)));
mInitBlock.Write32(0x0C, 0x0);
mInitBlock.Write32(0x10, 0x0);
mInitBlock.Write32(0x14, mRxDescriptor.Offset);
Expand All @@ -76,9 +75,9 @@ public AMDPCNetII(PCIDeviceNormal device)

ManagedMemoryBlock buffer = new ManagedMemoryBlock(2048);
mRxDescriptor.Write32(xOffset + 8, buffer.Offset);
UInt16 buffer_len = (UInt16) (~buffer.Size);
UInt16 buffer_len = (UInt16)(~buffer.Size);
buffer_len++;
UInt32 flags = (UInt32) (buffer_len & 0x0FFF) | 0xF000 | 0x80000000;
UInt32 flags = (UInt32)(buffer_len & 0x0FFF) | 0xF000 | 0x80000000;
mRxDescriptor.Write32(xOffset + 4, flags);
mRxBuffers.Add(buffer);
}
Expand All @@ -97,7 +96,7 @@ public AMDPCNetII(PCIDeviceNormal device)
mTransmitBuffer = new Queue<byte[]>();
mRecvBuffer = new Queue<byte[]>();

//INTs.SetIrqHandler(device.InterruptLine, HandleNetworkInterrupt);
INTs.SetIrqHandler(device.InterruptLine, HandleNetworkInterrupt);
}

protected void HandleNetworkInterrupt(ref INTs.IRQContext aContext)
Expand Down Expand Up @@ -134,17 +133,17 @@ protected void HandleNetworkInterrupt(ref INTs.IRQContext aContext)
/// </summary>
public static void FindAll()
{
Console.WriteLine("Scanning for AMD PCNetII cards...");
// PCIDevice device = Cosmos.HAL.PCI.GetDevice(VendorID.AMD, DeviceID.PCNETII);
// if (device != null)
// {
// AMDPCNetII nic = new AMDPCNetII((PCIDeviceNormal) device);
//
// Console.WriteLine("Found AMD PCNetII NIC on PCI " + device.bus + ":" + device.slot + ":" +
// device.function);
// Console.WriteLine("NIC IRQ: " + device.InterruptLine);
// Console.WriteLine("NIC MAC Address: " + nic.MACAddress.ToString());
// }
Console.WriteLine("Scanning for AMD PCNetII cards...");
PCIDevice device = Cosmos.HAL.PCI.GetDevice(VendorID.AMD, DeviceID.PCNETII);
if (device != null)
{
AMDPCNetII nic = new AMDPCNetII((PCIDevice)device);

Console.WriteLine("Found AMD PCNetII NIC on PCI " + device.bus + ":" + device.slot + ":" +
device.function);
Console.WriteLine("NIC IRQ: " + device.InterruptLine);
Console.WriteLine("NIC MAC Address: " + nic.MACAddress.ToString());
}
}

#region Register Access Properties
Expand Down Expand Up @@ -220,7 +219,6 @@ public override bool Enable()
return true;
}

[DebugStub(Off = true)]
public override bool QueueBytes(byte[] buffer, int offset, int length)
{
byte[] data = new byte[length];
Expand Down Expand Up @@ -277,7 +275,6 @@ public override bool IsReceiveBufferFull()

#region Helper Functions

[DebugStub(Off = true)]
protected bool SendBytes(ref byte[] aData)
{
int txd = mNextTXDesc++;
Expand All @@ -286,7 +283,7 @@ protected bool SendBytes(ref byte[] aData)
mNextTXDesc = 0;
}

uint xOffset = (uint) (txd * 16);
uint xOffset = (uint)(txd * 16);
UInt32 status = mTxDescriptor.Read32(xOffset + 4);
if ((status & 0x80000000) == 0)
{
Expand All @@ -295,11 +292,11 @@ protected bool SendBytes(ref byte[] aData)
mTxBuffers[txd][b] = aData[b];
}
//UInt16 buffer_len = (UInt16)(aData.Length < 64 ? 64 : aData.Length);
UInt16 buffer_len = (UInt16) aData.Length;
buffer_len = (UInt16) (~buffer_len);
UInt16 buffer_len = (UInt16)aData.Length;
buffer_len = (UInt16)(~buffer_len);
buffer_len++;

UInt32 flags = (UInt32) ((buffer_len) & 0x0FFF) | 0x0300F000 | 0x80000000;
UInt32 flags = (UInt32)((buffer_len) & 0x0FFF) | 0x0300F000 | 0x80000000;

mTxDescriptor.Write32(xOffset + 4, flags);

Expand All @@ -309,7 +306,6 @@ protected bool SendBytes(ref byte[] aData)
return false;
}

[DebugStub(Off = true)]
private void ReadRawData()
{
uint status;
Expand All @@ -318,11 +314,11 @@ private void ReadRawData()

for (int rxd = 0; rxd < 16; rxd++)
{
uint xOffset = (uint) (rxd * 16);
uint xOffset = (uint)(rxd * 16);
status = mRxDescriptor.Read32(xOffset + 4);
if ((status & 0x80000000) == 0)
{
recv_size = (UInt16) (mRxDescriptor[xOffset + 0] & 0xFFF);
recv_size = (UInt16)(mRxDescriptor[xOffset + 0] & 0xFFF);
recv_data = new byte[recv_size];
for (uint b = 0; b < recv_size; b++)
{
Expand Down
13 changes: 8 additions & 5 deletions source/Cosmos.HAL2/PCI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum ClassID
Coprocessor = 0x40,
Unclassified = 0xFF
}

public enum SubclassID
{
// MassStorageController:
Expand All @@ -46,7 +46,7 @@ public enum SubclassID
NVMController = 0x08,
UnknownMassStorage = 0x09,
}

public enum ProgramIF
{
// MassStorageController:
Expand All @@ -57,25 +57,28 @@ public enum ProgramIF
NVM_NVMHCI = 0x01,
NVM_NVMExpress = 0x02
}

public enum VendorID
{
Intel = 0x8086,
AMD = 0x1022,
VMWare = 0x15AD,
Bochs = 0x1234,
VirtualBox = 0x80EE
}

public enum DeviceID
{
SVGAIIAdapter = 0x0405,
PCNETII = 0x2000,
BGA = 0x1111,
VBVGA = 0xBEEF,
VBoxGuest = 0xCAFE
}

public class PCI
{
private static List<PCIDevice> Devices;
public static List<PCIDevice> Devices;

public static uint Count
{
Expand Down
67 changes: 56 additions & 11 deletions source/Cosmos.HAL2/PCIDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public enum PCIHeaderType : byte

public enum PCIBist : byte
{
CocdMask = 0x0f,
Start = 0x40,
Capable = 0x80
};
CocdMask = 0x0f, /* Return result */
Start = 0x40, /* 1 to start BIST, 2 secs or less */
Capable = 0x80 /* 1 if BIST capable */
}

public enum PCIInterruptPIN : byte
{
Expand All @@ -35,6 +35,20 @@ public enum PCIInterruptPIN : byte
INTD = 0x04
};

public enum PCICommand : short
{
IO = 0x1, /* Enable response in I/O space */
Memory = 0x2, /* Enable response in Memory space */
Master = 0x4, /* Enable bus mastering */
Special = 0x8, /* Enable response to special cycles */
Invalidate = 0x10, /* Use memory write and invalidate */
VGA_Pallete = 0x20, /* Enable palette snooping */
Parity = 0x40, /* Enable parity checking */
Wait = 0x80, /* Enable address/data stepping */
SERR = 0x100, /* Enable SERR */
Fast_Back = 0x200, /* Enable back-to-back writes */
}

public enum Config : byte
{
VendorID = 0, DeviceID = 2,
Expand Down Expand Up @@ -63,7 +77,6 @@ public enum Config : byte
public readonly ushort VendorID;
public readonly ushort DeviceID;

public readonly ushort Command;
public readonly ushort Status;

public readonly byte RevisionID;
Expand All @@ -85,6 +98,14 @@ public enum Config : byte

protected static Core.IOGroup.PCI IO = new Core.IOGroup.PCI();

public byte InterruptLine { get; private set; }
public PCICommand Command { get { return (PCICommand)ReadRegister16(0x04); } set { WriteRegister16(0x04, (ushort)value); } }

/// <summary>
/// Has this device been claimed by a driver
/// </summary>
public bool Claimed { get; set; }

public PCIDevice(uint bus, uint slot, uint function)
{
this.bus = bus;
Expand All @@ -94,9 +115,8 @@ public PCIDevice(uint bus, uint slot, uint function)
VendorID = ReadRegister16((byte)Config.VendorID);
DeviceID = ReadRegister16((byte)Config.DeviceID);

Command = ReadRegister16((byte)Config.Command);
Status = ReadRegister16((byte)Config.Status);

//Command = ReadRegister16((byte)Config.Command);
//Status = ReadRegister16((byte)Config.Status);

RevisionID = ReadRegister8((byte)Config.RevisionID);
ProgIF = ReadRegister8((byte)Config.ProgIF);
Expand All @@ -107,6 +127,8 @@ public PCIDevice(uint bus, uint slot, uint function)
HeaderType = (PCIHeaderType)ReadRegister8((byte)Config.HeaderType);
BIST = (PCIBist)ReadRegister8((byte)Config.BIST);
InterruptPIN = (PCIInterruptPIN)ReadRegister8((byte)Config.InterruptPIN);
InterruptLine = ReadRegister8((byte)Config.InterruptLine);

if ((uint)VendorID == 0xFF && (uint)DeviceID == 0xFFFF)
{
DeviceExists = false;
Expand All @@ -127,6 +149,11 @@ public PCIDevice(uint bus, uint slot, uint function)
}
}

public void EnableDevice()
{
Command |= PCICommand.Master | PCICommand.IO | PCICommand.Memory;
}

public static ushort GetHeaderType(ushort Bus, ushort Slot, ushort Function)
{
UInt32 xAddr = GetAddressBase(Bus, Slot, Function) | 0xE & 0xFC;
Expand Down Expand Up @@ -240,6 +267,14 @@ public static string GetDeviceString(PCIDevice device)
default:
return "Mylex Unknown device";
}
case 0x1234: //Bochs
switch (device.DeviceID)
{
case 0x1111:
return "Bochs BGA";
default:
return "Bochs Unknown device";
}
case 0x1274: //Ensoniq
switch (device.DeviceID)
{
Expand Down Expand Up @@ -282,6 +317,16 @@ public static string GetDeviceString(PCIDevice device)
default:
return "Intel Unknown device";
}
case 0x80EE: //VirtualBox
switch (device.DeviceID)
{
case 0xBEEF:
return "VirtualBox Graphics Adapter";
case 0xCAFE:
return "VirtualBox Guest Service";
default:
return "VirtualBox Unknown device";
}
default:
return "Unknown device";
}
Expand All @@ -300,7 +345,7 @@ public static string GetTypeString(PCIDevice device)
return "0x00 Subclass";
}
case 0x01:
switch(device.Subclass)
switch (device.Subclass)
{
case 0x00:
return "SCSI Bus Controller";
Expand Down Expand Up @@ -392,7 +437,7 @@ public static string GetTypeString(PCIDevice device)
return "Memory Controller";
}
case 0x06:
switch(device.Subclass)
switch (device.Subclass)
{
case 0x00:
return "Host Bridge";
Expand Down Expand Up @@ -763,7 +808,7 @@ private static string ToHex(uint aNumber, byte aBits)
return "0x" + aNumber.ToHex(aBits / 4);
}
}

public class PCIBaseAddressBar
{
private uint baseAddress = 0;
Expand Down

0 comments on commit 28647a8

Please sign in to comment.