From 87c6e5917949a488da4bded5b5912fa3c69347c3 Mon Sep 17 00:00:00 2001 From: GutPuncher Date: Wed, 15 Mar 2023 02:30:04 -0400 Subject: [PATCH] Make network driver registration dynamic. --- src/Home/Net/Drivers/Run.ZC | 65 +++++++++++++++++++++++++++---------- src/System/DevInfo.ZC | 4 +-- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/Home/Net/Drivers/Run.ZC b/src/Home/Net/Drivers/Run.ZC index 94c0451f..f994e784 100755 --- a/src/Home/Net/Drivers/Run.ZC +++ b/src/Home/Net/Drivers/Run.ZC @@ -7,9 +7,8 @@ #define PCIV_E1000 0x8086 #define PCID_82545EM 0x100F - #define PCIV_VIRTIO 0x1AF4 -//#define PCID_VIRTIO_NET 0x1000 +#define PCID_VIRTIO_NET 0x1000 U0 NetDriverInclude(U8 *driver) { @@ -19,33 +18,63 @@ U0 NetDriverInclude(U8 *driver) Free(filename); } +class CNetDriver:CQueue +{ + U16 vendor_id; + U16 device_id; + U8 *filename; // relative to Drivers/ folder, not absolute +}; + +CQueue *net_drivers = CAlloc(sizeof(CQueue)); +QueueInit(net_drivers); + +U0 NetDriverRegister(U16 vendor_id=NULL, U16 device_id=NULL, U8 *filename) +{ + + CNetDriver *driver; + + if (!vendor_id && !device_id) + return; + + driver = CAlloc(sizeof(CNetDriver)); + + driver->vendor_id = vendor_id; + driver->device_id = device_id; + driver->filename = StrNew(filename); + + QueueInsertRev(driver, net_drivers); +} + + U0 NetDriverInit() { - CPCIDev *net_driver_pci = PCIDevFind(PCIC_NETWORK); - Bool found = FALSE; + Bool found = FALSE; + CNetDriver *net_driver; + CPCIDev *net_pci; - if (net_driver_pci) + // register NIC PCI details with driver (file)name + NetDriverRegister(PCIV_VIRTIO, PCID_VIRTIO_NET, "VirtIONet"); + NetDriverRegister(PCIV_E1000, PCID_82545EM, "E1000"); + NetDriverRegister(PCIV_PCNET, PCID_PCNET, "PCNet"); + + // iterate registered drivers until match is found, if any found + net_driver = net_drivers->next; + + while (net_driver != net_drivers) { - if (net_driver_pci=PCIDevFind(, , PCIV_PCNET, PCID_PCNET)) + net_pci = PCIDevFind(,, net_driver->vendor_id, net_driver->device_id); + if (net_pci) { - NetDriverInclude("PCNet"); - found = TRUE; - } - else if (net_driver_pci=PCIDevFind(, , PCIV_E1000, PCID_82545EM)) - { - NetDriverInclude("E1000"); - found = TRUE; - } - else if (net_driver_pci=PCIDevFind(PCIC_NETWORK, , PCIV_VIRTIO, )) - { - NetDriverInclude("VirtIONet"); found = TRUE; + NetDriverInclude(net_driver->filename); + break; } + net_driver = net_driver->next; } if (!found) { - ClassRep(net_driver_pci); + ClassRep(net_pci); throw('NODRIVER'); } } diff --git a/src/System/DevInfo.ZC b/src/System/DevInfo.ZC index b3578df3..b401be15 100755 --- a/src/System/DevInfo.ZC +++ b/src/System/DevInfo.ZC @@ -55,7 +55,7 @@ lud_done: public CPCIDev *PCIDevFind(U16 class_code=NULL, U16 sub_code=NULL, U16 vendor_id=NULL, U16 device_id=NULL, U8 _bus=0xFF, U8 _dev=0xFF, U8 _fun=0xFF) -{//return first device with matching class & subcode, vendor & device id, class & device id, class & vendor id or a specific device. +{//return first device with matching class & subcode, vendor & device id, or a specific device. PCILookUpDevs; CPCIDev *p = dev.pci_head.next; @@ -63,8 +63,6 @@ public CPCIDev *PCIDevFind(U16 class_code=NULL, U16 sub_code=NULL, { if (p->vendor_id == vendor_id && p->device_id == device_id || p->class_code == class_code && p->sub_code == sub_code || - p->class_code == class_code && p->device_id == device_id && vendor_id == NULL || - p->class_code == class_code && p->vendor_id == vendor_id && device_id == NULL || p->bus == _bus && p->dev == _dev && p->fun == _fun) return p;