diff options
author | Peter Schiffer <pschiffe@redhat.com> | 2013-09-10 17:36:14 +0200 |
---|---|---|
committer | Peter Schiffer <pschiffe@redhat.com> | 2013-09-24 15:52:00 +0200 |
commit | 2e20f92522b659b206c907007d6f5757b406bb61 (patch) | |
tree | eaaa6d440a2a7a16a8dd61d80a8518d31f2190da | |
parent | 4df9822196d53a1aaeba3ec0709c86389133bd5a (diff) | |
download | openlmi-providers-2e20f92522b659b206c907007d6f5757b406bb61.tar.gz openlmi-providers-2e20f92522b659b206c907007d6f5757b406bb61.tar.xz openlmi-providers-2e20f92522b659b206c907007d6f5757b406bb61.zip |
Hardware: Move PCI bus initialization to the Initialize() function
Move initialize and cleanup code to the Initialize() and Cleanup() functions.
-rw-r--r-- | src/hardware/LMI_PCIBridgeProvider.c | 42 | ||||
-rw-r--r-- | src/hardware/LMI_PCIBridgeSystemDeviceProvider.c | 21 | ||||
-rw-r--r-- | src/hardware/LMI_PCIDeviceProvider.c | 40 | ||||
-rw-r--r-- | src/hardware/LMI_PCIDeviceSystemDeviceProvider.c | 21 | ||||
-rw-r--r-- | src/hardware/PCIDev.c | 39 | ||||
-rw-r--r-- | src/hardware/PCIDev.h | 15 |
6 files changed, 109 insertions, 69 deletions
diff --git a/src/hardware/LMI_PCIBridgeProvider.c b/src/hardware/LMI_PCIBridgeProvider.c index 3392772..ed97a7b 100644 --- a/src/hardware/LMI_PCIBridgeProvider.c +++ b/src/hardware/LMI_PCIBridgeProvider.c @@ -28,8 +28,19 @@ CMPIUint16 get_bridge_type(const char *bridge_type); static const CMPIBroker* _cb = NULL; +struct pci_access *acc_bridge = NULL; + static void LMI_PCIBridgeInitialize() { + if (init_pci_access(&acc_bridge, PCI_FILL_IDENT + | PCI_FILL_IRQ + | PCI_FILL_BASES + | PCI_FILL_ROM_BASE + | PCI_FILL_CLASS + | PCI_FILL_CAPS) != 0) { + error("Failed to access the PCI bus."); + abort(); + } } static CMPIStatus LMI_PCIBridgeCleanup( @@ -37,6 +48,8 @@ static CMPIStatus LMI_PCIBridgeCleanup( const CMPIContext* cc, CMPIBoolean term) { + cleanup_pci_access(&acc_bridge); + CMReturn(CMPI_RC_OK); } @@ -66,7 +79,6 @@ static CMPIStatus LMI_PCIBridgeEnumInstances( u16 svid, subid, status, command_reg, sec_status; u32 io_base, io_limit, io_type, mem_base, mem_limit, mem_type, pref_base, pref_limit, pref_type, pref_base_upper = 0, pref_limit_upper = 0; - struct pci_access *acc; struct pci_dev *dev; struct pci_cap *cap; char vendor_buf[NAME_BUF_SIZE], *vendor_name; @@ -77,38 +89,24 @@ static CMPIStatus LMI_PCIBridgeEnumInstances( char device_id_str[PCI_DEVID_STR_SIZE]; char instance_id[INSTANCE_ID_LEN]; - if (!(acc = pci_alloc())) { - KReturn2(_cb, ERR_FAILED, "Cannot access the PCI bus."); - } - pci_init(acc); - pci_scan_bus(acc); - - for (dev = acc->devices; dev; dev = dev->next) { - pci_fill_info(dev, - PCI_FILL_IDENT - | PCI_FILL_IRQ - | PCI_FILL_BASES - | PCI_FILL_ROM_BASE - | PCI_FILL_CLASS - | PCI_FILL_CAPS); - + for (dev = acc_bridge->devices; dev; dev = dev->next) { /* Use only PCI Bridges */ /* Throw away the lower 8 bits denoting the subclass */ if (((dev->device_class) >> 8) != LMI_PCIBridge_ClassCode_Bridge) { continue; } - vendor_name = pci_lookup_name(acc, vendor_buf, NAME_BUF_SIZE, + vendor_name = pci_lookup_name(acc_bridge, vendor_buf, NAME_BUF_SIZE, PCI_LOOKUP_VENDOR, dev->vendor_id); - device_name = pci_lookup_name(acc, device_buf, NAME_BUF_SIZE, + device_name = pci_lookup_name(acc_bridge, device_buf, NAME_BUF_SIZE, PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id); get_subid(dev, &svid, &subid); - subsys_name = pci_lookup_name(acc, subsys_buf, NAME_BUF_SIZE, + subsys_name = pci_lookup_name(acc_bridge, subsys_buf, NAME_BUF_SIZE, PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM, dev->vendor_id, dev->device_id, svid, subid); - svendor_name = pci_lookup_name(acc, svendor_buf, NAME_BUF_SIZE, + svendor_name = pci_lookup_name(acc_bridge, svendor_buf, NAME_BUF_SIZE, PCI_LOOKUP_VENDOR | PCI_LOOKUP_SUBSYSTEM, svid); - class = pci_lookup_name(acc, class_buf, NAME_BUF_SIZE, + class = pci_lookup_name(acc_bridge, class_buf, NAME_BUF_SIZE, PCI_LOOKUP_CLASS, dev->device_class); status = pci_read_word(dev, PCI_STATUS); rev = pci_read_byte(dev, PCI_REVISION_ID); @@ -364,8 +362,6 @@ static CMPIStatus LMI_PCIBridgeEnumInstances( KReturnInstance(cr, lmi_dev); } - pci_cleanup(acc); - CMReturn(CMPI_RC_OK); } diff --git a/src/hardware/LMI_PCIBridgeSystemDeviceProvider.c b/src/hardware/LMI_PCIBridgeSystemDeviceProvider.c index 2151dd5..6f947ab 100644 --- a/src/hardware/LMI_PCIBridgeSystemDeviceProvider.c +++ b/src/hardware/LMI_PCIBridgeSystemDeviceProvider.c @@ -26,8 +26,14 @@ static const CMPIBroker* _cb; +struct pci_access *acc_system_bridge = NULL; + static void LMI_PCIBridgeSystemDeviceInitialize() { + if (init_pci_access(&acc_system_bridge, PCI_FILL_CLASS) != 0) { + error("Failed to access the PCI bus."); + abort(); + } } static CMPIStatus LMI_PCIBridgeSystemDeviceCleanup( @@ -35,6 +41,8 @@ static CMPIStatus LMI_PCIBridgeSystemDeviceCleanup( const CMPIContext* cc, CMPIBoolean term) { + cleanup_pci_access(&acc_system_bridge); + CMReturn(CMPI_RC_OK); } @@ -61,7 +69,6 @@ static CMPIStatus LMI_PCIBridgeSystemDeviceEnumInstances( CMPIObjectPath *o; CMPIStatus st; const char *ns = KNameSpace(cop); - struct pci_access *acc; struct pci_dev *dev; char device_id_str[PCI_DEVID_STR_SIZE]; @@ -72,15 +79,7 @@ static CMPIStatus LMI_PCIBridgeSystemDeviceEnumInstances( o = CIM_ComputerSystemRef_ToObjectPath(&cim_cs, &st); CMSetClassName(o, get_system_creation_class_name()); - if (!(acc = pci_alloc())) { - KReturn2(_cb, ERR_FAILED, "Can't access the PCI bus."); - } - pci_init(acc); - pci_scan_bus(acc); - - for (dev = acc->devices; dev; dev = dev->next) { - pci_fill_info(dev, PCI_FILL_CLASS); - + for (dev = acc_system_bridge->devices; dev; dev = dev->next) { /* Use only PCI Bridges */ /* Throw away the lower 8 bits denoting the subclass */ if (((dev->device_class) >> 8) != LMI_PCIBridge_ClassCode_Bridge) { @@ -108,8 +107,6 @@ static CMPIStatus LMI_PCIBridgeSystemDeviceEnumInstances( KReturnInstance(cr, lmi_pci_sys_device); } - pci_cleanup(acc); - CMReturn(CMPI_RC_OK); } diff --git a/src/hardware/LMI_PCIDeviceProvider.c b/src/hardware/LMI_PCIDeviceProvider.c index cc6adc8..e2a159b 100644 --- a/src/hardware/LMI_PCIDeviceProvider.c +++ b/src/hardware/LMI_PCIDeviceProvider.c @@ -27,8 +27,19 @@ static const CMPIBroker* _cb = NULL; +struct pci_access *acc_dev = NULL; + static void LMI_PCIDeviceInitialize() { + if (init_pci_access(&acc_dev, PCI_FILL_IDENT + | PCI_FILL_IRQ + | PCI_FILL_BASES + | PCI_FILL_ROM_BASE + | PCI_FILL_CLASS + | PCI_FILL_CAPS) != 0) { + error("Failed to access the PCI bus."); + abort(); + } } static CMPIStatus LMI_PCIDeviceCleanup( @@ -36,6 +47,8 @@ static CMPIStatus LMI_PCIDeviceCleanup( const CMPIContext* cc, CMPIBoolean term) { + cleanup_pci_access(&acc_dev); + CMReturn(CMPI_RC_OK); } @@ -62,7 +75,6 @@ static CMPIStatus LMI_PCIDeviceEnumInstances( CMPIUint16 pci_cap; u8 rev, cache_line; u16 svid, subid, status, command_reg; - struct pci_access *acc; struct pci_dev *dev; struct pci_cap *cap; char vendor_buf[NAME_BUF_SIZE], *vendor_name; @@ -72,36 +84,22 @@ static CMPIStatus LMI_PCIDeviceEnumInstances( char device_id_str[PCI_DEVID_STR_SIZE]; char instance_id[INSTANCE_ID_LEN]; - if (!(acc = pci_alloc())) { - KReturn2(_cb, ERR_FAILED, "Cannot access the PCI bus."); - } - pci_init(acc); - pci_scan_bus(acc); - - for (dev = acc->devices; dev; dev = dev->next) { - pci_fill_info(dev, - PCI_FILL_IDENT - | PCI_FILL_IRQ - | PCI_FILL_BASES - | PCI_FILL_ROM_BASE - | PCI_FILL_CLASS - | PCI_FILL_CAPS); - + for (dev = acc_dev->devices; dev; dev = dev->next) { /* Ignore PCI Bridges */ /* Throw away the lower 8 bits denoting the subclass */ if (((dev->device_class) >> 8) == LMI_PCIDevice_ClassCode_Bridge) { continue; } - vendor_name = pci_lookup_name(acc, vendor_buf, NAME_BUF_SIZE, + vendor_name = pci_lookup_name(acc_dev, vendor_buf, NAME_BUF_SIZE, PCI_LOOKUP_VENDOR, dev->vendor_id); - device_name = pci_lookup_name(acc, device_buf, NAME_BUF_SIZE, + device_name = pci_lookup_name(acc_dev, device_buf, NAME_BUF_SIZE, PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id); get_subid(dev, &svid, &subid); - subsys_name = pci_lookup_name(acc, subsys_buf, NAME_BUF_SIZE, + subsys_name = pci_lookup_name(acc_dev, subsys_buf, NAME_BUF_SIZE, PCI_LOOKUP_DEVICE | PCI_LOOKUP_SUBSYSTEM, dev->vendor_id, dev->device_id, svid, subid); - svendor_name = pci_lookup_name(acc, svendor_buf, NAME_BUF_SIZE, + svendor_name = pci_lookup_name(acc_dev, svendor_buf, NAME_BUF_SIZE, PCI_LOOKUP_VENDOR | PCI_LOOKUP_SUBSYSTEM, svid); status = pci_read_word(dev, PCI_STATUS); rev = pci_read_byte(dev, PCI_REVISION_ID); @@ -265,8 +263,6 @@ static CMPIStatus LMI_PCIDeviceEnumInstances( KReturnInstance(cr, lmi_dev); } - pci_cleanup(acc); - CMReturn(CMPI_RC_OK); } diff --git a/src/hardware/LMI_PCIDeviceSystemDeviceProvider.c b/src/hardware/LMI_PCIDeviceSystemDeviceProvider.c index 897f7bc..d7af669 100644 --- a/src/hardware/LMI_PCIDeviceSystemDeviceProvider.c +++ b/src/hardware/LMI_PCIDeviceSystemDeviceProvider.c @@ -26,8 +26,14 @@ static const CMPIBroker* _cb; +struct pci_access *acc_system_dev = NULL; + static void LMI_PCIDeviceSystemDeviceInitialize() { + if (init_pci_access(&acc_system_dev, PCI_FILL_CLASS) != 0) { + error("Failed to access the PCI bus."); + abort(); + } } static CMPIStatus LMI_PCIDeviceSystemDeviceCleanup( @@ -35,6 +41,8 @@ static CMPIStatus LMI_PCIDeviceSystemDeviceCleanup( const CMPIContext* cc, CMPIBoolean term) { + cleanup_pci_access(&acc_system_dev); + CMReturn(CMPI_RC_OK); } @@ -61,7 +69,6 @@ static CMPIStatus LMI_PCIDeviceSystemDeviceEnumInstances( CMPIObjectPath *o; CMPIStatus st; const char *ns = KNameSpace(cop); - struct pci_access *acc; struct pci_dev *dev; char device_id_str[PCI_DEVID_STR_SIZE]; @@ -72,15 +79,7 @@ static CMPIStatus LMI_PCIDeviceSystemDeviceEnumInstances( o = CIM_ComputerSystemRef_ToObjectPath(&cim_cs, &st); CMSetClassName(o, get_system_creation_class_name()); - if (!(acc = pci_alloc())) { - KReturn2(_cb, ERR_FAILED, "Can't access the PCI bus."); - } - pci_init(acc); - pci_scan_bus(acc); - - for (dev = acc->devices; dev; dev = dev->next) { - pci_fill_info(dev, PCI_FILL_CLASS); - + for (dev = acc_system_dev->devices; dev; dev = dev->next) { /* Ignore PCI Bridges */ /* Throw away the lower 8 bits denoting the subclass */ if (((dev->device_class) >> 8) == LMI_PCIDevice_ClassCode_Bridge) { @@ -108,8 +107,6 @@ static CMPIStatus LMI_PCIDeviceSystemDeviceEnumInstances( KReturnInstance(cr, lmi_pci_sys_device); } - pci_cleanup(acc); - CMReturn(CMPI_RC_OK); } diff --git a/src/hardware/PCIDev.c b/src/hardware/PCIDev.c index 40051b3..f703bc1 100644 --- a/src/hardware/PCIDev.c +++ b/src/hardware/PCIDev.c @@ -20,6 +20,45 @@ #include "PCIDev.h" +short init_pci_access(struct pci_access **acc, const int flags) +{ + struct pci_dev *dev; + + if (!acc) { + return -1; + } + + if (*acc) { + return 0; + } + + if (!(*acc = pci_alloc())) { + return -1; + } + + pci_init(*acc); + pci_scan_bus(*acc); + + for (dev = (*acc)->devices; dev; dev = dev->next) { + pci_fill_info(dev, flags); + } + + return 0; +} + +void cleanup_pci_access(struct pci_access **acc) +{ + if (!acc) { + return; + } + + if (*acc) { + pci_cleanup(*acc); + } + + *acc = NULL; +} + void get_subid(struct pci_dev *d, u16 *subvp, u16 *subdp) { u8 htype = pci_read_byte(d, PCI_HEADER_TYPE) & 0x7f; diff --git a/src/hardware/PCIDev.h b/src/hardware/PCIDev.h index 7e4cf0d..cd686d6 100644 --- a/src/hardware/PCIDev.h +++ b/src/hardware/PCIDev.h @@ -21,12 +21,27 @@ #ifndef PCIDEV_H_ #define PCIDEV_H_ +#include <stddef.h> #include <pci/pci.h> #define PCI_DEVID_STR_SIZE 55 #define NAME_BUF_SIZE 128 /* + * Initialize pci access structure. + * @param acc pci access structure + * @param flags flags to be passed to pci_fill_info() fn + * @return 0 on success, negative value otherwise + */ +short init_pci_access(struct pci_access **acc, const int flags); + +/* + * Cleanup pci access structure. + * @param acc pci access structure + */ +void cleanup_pci_access(struct pci_access **acc); + +/* * Get subsystem IDs of pci device. * @param d pci device * @param subvp subsystem vendor id |