From a8523a21b57babd2828047d2a4dfd7e855187e65 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 24 Feb 2022 21:10:24 -0500 Subject: [PATCH] freebsd_network: PCI handling cleanup in preparation for USB support. Should not have any functional change. --- src/libs/compat/freebsd_network/bus.cpp | 21 +++---- src/libs/compat/freebsd_network/device.c | 2 - src/libs/compat/freebsd_network/device.h | 4 ++ src/libs/compat/freebsd_network/driver.c | 17 ++---- src/libs/compat/freebsd_network/pci.cpp | 70 +++++++++++++++++------- 5 files changed, 68 insertions(+), 46 deletions(-) diff --git a/src/libs/compat/freebsd_network/bus.cpp b/src/libs/compat/freebsd_network/bus.cpp index 372d2fdaba..207641b5c2 100644 --- a/src/libs/compat/freebsd_network/bus.cpp +++ b/src/libs/compat/freebsd_network/bus.cpp @@ -189,15 +189,13 @@ bus_alloc_resource(device_t dev, int type, int *rid, unsigned long start, result = bus_alloc_irq_resource(dev, res); } else { // msi or msi-x interrupt at index *rid - 1 - pci_info *info; - info = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); res->r_bustag = BUS_SPACE_TAG_MSI; res->r_bushandle = info->u.h0.interrupt_line + *rid - 1; result = 0; } } else if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { - pci_info *info - = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); int bar_index = bus_register_to_bar_index(info, *rid); if (bar_index >= 0) { if (type == SYS_RES_MEMORY) @@ -404,18 +402,17 @@ bus_setup_intr(device_t dev, struct resource *res, int flags, if (status == B_OK && res->r_bustag == BUS_SPACE_TAG_MSI && gPCIx86 != NULL) { // this is an msi, enable it - pci_info *info - = &((struct root_device_softc *)dev->root->softc)->pci_info; - if (((struct root_device_softc *)dev->root->softc)->is_msi) { - if (gPCIx86->enable_msi(info->bus, info->device, - info->function) != B_OK) { + struct root_device_softc* root_softc = ((struct root_device_softc *)dev->root->softc); + if (root_softc->is_msi) { + if (gPCIx86->enable_msi(root_softc->pci_info.bus, root_softc->pci_info.device, + root_softc->pci_info.function) != B_OK) { device_printf(dev, "enabling msi failed\n"); bus_teardown_intr(dev, res, intr); return ENODEV; } - } else if (((struct root_device_softc *)dev->root->softc)->is_msix) { - if (gPCIx86->enable_msix(info->bus, info->device, - info->function) != B_OK) { + } else if (root_softc->is_msix) { + if (gPCIx86->enable_msix(root_softc->pci_info.bus, root_softc->pci_info.device, + root_softc->pci_info.function) != B_OK) { device_printf(dev, "enabling msix failed\n"); bus_teardown_intr(dev, res, intr); return ENODEV; diff --git a/src/libs/compat/freebsd_network/device.c b/src/libs/compat/freebsd_network/device.c index ae17f039e8..7504d35cc0 100644 --- a/src/libs/compat/freebsd_network/device.c +++ b/src/libs/compat/freebsd_network/device.c @@ -26,8 +26,6 @@ spinlock __haiku_intr_spinlock; struct net_stack_module_info *gStack; -pci_module_info *gPci; -struct pci_x86_module_info *gPCIx86; static struct list sRootDevices; static int sNextUnit; diff --git a/src/libs/compat/freebsd_network/device.h b/src/libs/compat/freebsd_network/device.h index d1b169df66..e8a721997c 100644 --- a/src/libs/compat/freebsd_network/device.h +++ b/src/libs/compat/freebsd_network/device.h @@ -74,7 +74,11 @@ void uninit_hard_clock(void); status_t init_callout(void); void uninit_callout(void); +status_t init_pci(); +void uninit_pci(); + device_t find_root_device(int); +pci_info* get_device_pci_info(device_t dev); void driver_printf(const char *format, ...) __attribute__ ((format (__printf__, 1, 2))); diff --git a/src/libs/compat/freebsd_network/driver.c b/src/libs/compat/freebsd_network/driver.c index 10f1bcf32e..e750f8f7b9 100644 --- a/src/libs/compat/freebsd_network/driver.c +++ b/src/libs/compat/freebsd_network/driver.c @@ -111,13 +111,10 @@ _fbsd_init_hardware(driver_t *drivers[]) device_t root; status = get_module(B_PCI_MODULE_NAME, (module_info **)&gPci); + status = init_pci(); if (status != B_OK) return status; - // if it fails we just don't support x86 specific features (like MSIs) - if (get_module(B_PCI_X86_MODULE_NAME, (module_info **)&gPCIx86) != B_OK) - gPCIx86 = NULL; - status = init_root_device(&root); if (status != B_OK) return status; @@ -168,9 +165,7 @@ _fbsd_init_hardware(driver_t *drivers[]) if (p > 0) return B_OK; - put_module(B_PCI_MODULE_NAME); - if (gPCIx86 != NULL) - put_module(B_PCI_X86_MODULE_NAME); + uninit_pci(); return B_NOT_SUPPORTED; } @@ -254,9 +249,7 @@ err2: gDriverName, NULL); } - put_module(B_PCI_MODULE_NAME); - if (gPCIx86 != NULL) - put_module(B_PCI_X86_MODULE_NAME); + uninit_pci(); return status; } @@ -288,9 +281,7 @@ _fbsd_uninit_drivers(driver_t *drivers[]) gDriverName, NULL); } - put_module(B_PCI_MODULE_NAME); - if (gPCIx86 != NULL) - put_module(B_PCI_X86_MODULE_NAME); + uninit_pci(); return B_OK; } diff --git a/src/libs/compat/freebsd_network/pci.cpp b/src/libs/compat/freebsd_network/pci.cpp index 8de6ce8eb9..ecbd2e554c 100644 --- a/src/libs/compat/freebsd_network/pci.cpp +++ b/src/libs/compat/freebsd_network/pci.cpp @@ -23,10 +23,50 @@ extern "C" { #endif +pci_module_info *gPci; +struct pci_x86_module_info *gPCIx86; + + +status_t +init_pci() +{ + if (gPci != NULL) + return B_OK; + + status_t status = get_module(B_PCI_MODULE_NAME, (module_info **)&gPci); + if (status != B_OK) + return status; + + // if it fails we just don't support x86 specific features (like MSIs) + if (get_module(B_PCI_X86_MODULE_NAME, (module_info **)&gPCIx86) != B_OK) + gPCIx86 = NULL; + + return B_OK; +} + + +void +uninit_pci() +{ + if (gPci != NULL) + put_module(B_PCI_MODULE_NAME); + if (gPCIx86 != NULL) + put_module(B_PCI_X86_MODULE_NAME); +} + + +pci_info* +get_device_pci_info(device_t device) +{ + struct root_device_softc* root_softc = (struct root_device_softc*)device->root->softc; + return &root_softc->pci_info; +} + + uint32_t pci_read_config(device_t dev, int offset, int size) { - pci_info *info = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); uint32_t value = gPci->read_pci_config(info->bus, info->device, info->function, offset, size); @@ -38,7 +78,7 @@ pci_read_config(device_t dev, int offset, int size) void pci_write_config(device_t dev, int offset, uint32_t value, int size) { - pci_info *info = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); TRACE_PCI(dev, "pci_write_config(%i, 0x%x, %i)\n", offset, value, size); @@ -129,8 +169,7 @@ pci_get_slot(device_t dev) uint8_t pci_get_function(device_t dev) { - pci_info *info - = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); return info->function; } @@ -242,11 +281,10 @@ pci_find_extcap(device_t child, int capability, int *_capabilityRegister) int pci_msi_count(device_t dev) { - pci_info *info; if (gPCIx86 == NULL) return 0; - info = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); return gPCIx86->get_msi_count(info->bus, info->device, info->function); } @@ -254,13 +292,11 @@ pci_msi_count(device_t dev) int pci_alloc_msi(device_t dev, int *count) { - pci_info *info; - uint8 startVector = 0; if (gPCIx86 == NULL) return ENODEV; - info = &((struct root_device_softc *)dev->root->softc)->pci_info; - + pci_info* info = get_device_pci_info(dev); + uint8 startVector = 0; if (gPCIx86->configure_msi(info->bus, info->device, info->function, *count, &startVector) != B_OK) { return ENODEV; @@ -275,11 +311,10 @@ pci_alloc_msi(device_t dev, int *count) int pci_release_msi(device_t dev) { - pci_info *info; if (gPCIx86 == NULL) return ENODEV; - info = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); gPCIx86->unconfigure_msi(info->bus, info->device, info->function); ((struct root_device_softc *)dev->root->softc)->is_msi = false; ((struct root_device_softc *)dev->root->softc)->is_msix = false; @@ -290,7 +325,7 @@ pci_release_msi(device_t dev) int pci_msix_table_bar(device_t dev) { - pci_info *info = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); uint8 capability_offset; if (gPci->find_pci_capability(info->bus, info->device, info->function, @@ -308,11 +343,10 @@ pci_msix_table_bar(device_t dev) int pci_msix_count(device_t dev) { - pci_info *info; if (gPCIx86 == NULL) return 0; - info = &((struct root_device_softc *)dev->root->softc)->pci_info; + pci_info* info = get_device_pci_info(dev); return gPCIx86->get_msix_count(info->bus, info->device, info->function); } @@ -320,13 +354,11 @@ pci_msix_count(device_t dev) int pci_alloc_msix(device_t dev, int *count) { - pci_info *info; - uint8 startVector = 0; if (gPCIx86 == NULL) return ENODEV; - info = &((struct root_device_softc *)dev->root->softc)->pci_info; - + pci_info* info = get_device_pci_info(dev); + uint8 startVector = 0; if (gPCIx86->configure_msix(info->bus, info->device, info->function, *count, &startVector) != B_OK) { return ENODEV;