Export SMBIOS provided firmware instance and label to sysfs
Subsystems (4)
PATCH 1/1 Export SMBIOS provided firmware instance and label to sysfs
Hello,
This post is in continuation of the discussions in the thread
http://marc.info/?l=linux-pci&m7852633816819&w=3.
The patch has the following changes -
1.The patch would be compiled only if CONFIG_DMI is set 2.The .test method has been changed to use .is_visible which is part of already existing infrastructure.
Please find the patch with the above changes here -
From: Narendra K <[email protected]>
Subject: [PATCH] Export SMBIOS provided firmware instance and label to sysfs
This patch exports SMBIOS provided firmware instance and label of onboard pci devices to sysfs
--- drivers/firmware/dmi_scan.c | 26 ++++++++ drivers/pci/Makefile | 3 + drivers/pci/pci-label.c | 142 +++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci-sysfs.c | 5 ++ drivers/pci/pci.h | 9 +++ include/linux/dmi.h | 9 +++ 6 files changed, 194 insertions(+), 0 deletions(-) create mode 100644 drivers/pci/pci-label.c
drivers/firmware/dmi_scan.c +26 -0
@@ -277,6 +277,29 @@ static void __init dmi_save_ipmi_device(const struct dmi_header *dm)list_add_tail (&dev->list, &dmi_devices); } +static void __init dmi_save_dev_onboard (int instance,int segment,int bus, +int devfn,const char *name) +{ +struct dmi_dev_onboard *onboard_dev; + + onboard_dev =dmi_alloc (sizeof (*onboard_dev) +strlen (name) +1 ); +if (!onboard_dev) { +printk (KERN_ERR "dmi_save_dev_onboard: out of memory.\n" ); +return ; + } + onboard_dev->instance = instance; + onboard_dev->segment = segment; + onboard_dev->bus = bus; + onboard_dev->devfn = devfn; + +strcpy ((char *)&onboard_dev[1 ], name); + onboard_dev->dev.type =DMI_DEV_TYPE_DEV_ONBOARD ; + onboard_dev->dev.name = (char *)&onboard_dev[1 ]; + onboard_dev->dev.device_data = onboard_dev; + +list_add (&onboard_dev->dev.list, &dmi_devices); +} +static void __init dmi_save_extended_devices (const struct dmi_header *dm) {const u8 *d = (u8 *) dm +5 ;
@@ -285,6 +308,8 @@ static void __init dmi_save_extended_devices(const struct dmi_header *dm)if ((*d &0x80 ) =0 )return ; +dmi_save_dev_onboard (*(d+1 ), *(u16 *)(d+2 ), *(d+4 ), *(d+5 ), +dmi_string_nosave (dm, *(d-1 )));dmi_save_one_device (*d &0x7f ,dmi_string_nosave (dm, *(d -1 ))); }
@@ -333,6 +358,7 @@ static void __init dmi_decode(const struct dmi_header *dm, void *dummy)break ;case 41 :/* Onboard Devices Extended Information */ dmi_save_extended_devices (dm); +break ; } }
drivers/pci/Makefile +3 -0
@@ -55,6 +55,9 @@ obj-$(CONFIG_MICROBLAZE) += setup-bus.o # obj-$(CONFIG_ACPI) += pci-acpi.o +# SMBIOS provided firmware instance and labels +obj-$(CONFIG_DMI) += pci-label.o + # Cardbus & CompactPCI use setup-bus obj-$(CONFIG_HOTPLUG) += setup-bus.o
drivers/pci/pci-label.c new file +142 -0
142 lines not on this page · raw message
drivers/pci/pci-sysfs.c +5 -0
@@ -1132,6 +1132,8 @@ int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)pci_create_slot_links (pdev); +pci_create_firmware_label_files (pdev); +return 0 ; err_vga_file:
@@ -1201,6 +1203,9 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)sysfs_remove_bin_file (&pdev->dev.kobj, pdev->rom_attr);kfree (pdev->rom_attr); } + +pci_remove_firmware_label_files (pdev); + }static int __init pci_sysfs_init (void )
drivers/pci/pci.h +9 -0
@@ -11,6 +11,15 @@extern int pci_uevent (struct device *dev,struct kobj_uevent_env *env);extern int pci_create_sysfs_dev_files (struct pci_dev *pdev);extern void pci_remove_sysfs_dev_files (struct pci_dev *pdev); +#ifndef CONFIG_DMI +static inline int pci_create_firmware_label_files (struct pci_dev *pdev) +{return 0 ; } +static inline int pci_remove_firmware_label_files (struct pci_dev *pdev) +{return 0 ; } +#else +extern int pci_create_firmware_label_files (struct pci_dev *pdev); +extern int pci_remove_firmware_label_files (struct pci_dev *pdev); +#endif extern void pci_cleanup_rom (struct pci_dev *dev);#ifdef HAVE_PCI_MMAP extern int pci_mmap_fits(struct pci_dev *pdev, int resno,
include/linux/dmi.h +9 -0
@@ -20,6 +20,7 @@ enum dmi_device_type {DMI_DEV_TYPE_SAS ,DMI_DEV_TYPE_IPMI =-1 ,DMI_DEV_TYPE_OEM_STRING =-2 , + DMI_DEV_TYPE_DEV_ONBOARD = -3, }; struct dmi_header {
@@ -37,6 +38,14 @@ struct dmi_device {#ifdef CONFIG_DMI +struct dmi_dev_onboard { +struct dmi_device dev; +int instance; +int segment; +int bus; +int devfn; +}; +extern int dmi_check_system (const struct dmi_system_id *list);const struct dmi_system_id *dmi_first_match (const struct dmi_system_id *list);extern const char *dmi_get_system_info (int field);
Signature · 4 lines
1.6.5.2 With regards, Narendra K
Quoted 1 line · Narendra K
+static char smbios_attr[4096];
Interesting size, too bad you don't reference it again:
drivers/pci/pci-label.c line 59
Quoted 6 lines
drivers/pci/pci-label.c new file +6
-0+if (attribute =SMBIOS_ATTR_INSTANCE_SHOW ) +return scnprintf (smbios_attr,PAGE_SIZE , +"%d\n" , donboard->instance); +else if (attribute =SMBIOS_ATTR_LABEL_SHOW ) +return scnprintf (smbios_attr,PAGE_SIZE , +"%s\n" , dmi->name);
PAGE_SIZE here?
Which is it (note, some arches PAGE_SIZE is not 4k...)
Quoted 42 lines
drivers/pci/pci-label.c new file +42
-0+return strlen (dmi->name); + } + } +return 0 ; +} + +static ssize_t +smbiosname_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +ssize_t result; + result =smbios_instance_string_exist (&dev->kobj, &attr->attr, +SMBIOS_ATTR_LABEL_SHOW ); +strncpy (buf, smbios_attr, result); +return result; +} + +static ssize_t +smbiosinstance_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +ssize_t result; + result =smbios_instance_string_exist (&dev->kobj, &attr->attr, +SMBIOS_ATTR_INSTANCE_SHOW ); +strncpy (buf, smbios_attr, result); +return result; +} + +static struct device_attribute smbios_attr_label = { + .attr = {.name ="label" , .mode =0444 , .owner =THIS_MODULE }, + .show = smbiosname_show, +}; + +static struct device_attribute smbios_attr_instance = { + .attr = {.name ="index" , .mode =0444 , .owner =THIS_MODULE }, + .show = smbiosinstance_show, +}; + +static struct attribute *smbios_attributes[] = { + &smbios_attr_label.attr, + &smbios_attr_instance.attr, +NULL , +};
drivers/pci/pci-label.c line 111
Quoted 10 lines
drivers/pci/pci-label.c new file +10
-0+ +static struct attribute_group smbios_attr_group = { + .attrs = smbios_attributes, + .is_visible = smbios_instance_string_exist, +}; + +static int +pci_create_smbiosname_file (struct pci_dev *pdev) +{ + if (smbios_attr_group.is_visible &&
You set .is_visable above, how could it not be set?
drivers/pci/pci-label.c line 114
Quoted 3 lines
drivers/pci/pci-label.c new file +3
-0+ smbios_attr_group.is_visible(&pdev->dev.kobj, NULL, 0)) { +if (!sysfs_create_group (&pdev->dev.kobj, &smbios_attr_group)) +return 0 ;
No, sysfs_create_group will call .is_visable, right? You shouldn't need to call it yourself or check it, at all.
drivers/pci/pci-label.c line 125
Quoted 7 lines
drivers/pci/pci-label.c new file +7
-0+static int +pci_remove_smbiosname_file (struct pci_dev *pdev) +{ +if (smbios_attr_group.is_visible && + smbios_attr_group.is_visible (&pdev->dev.kobj,NULL ,0 )) { +sysfs_remove_group (&pdev->dev.kobj, &smbios_attr_group); +return 0 ;
Same here, you shouldn't have to check .is_visable.
Review
Quoted 2 lines
(quoted diff) +2
-0+ } + return -1;
Why -1? Please use a real error value.
thanks,
greg k-h
Quoted 26 lines · Narendra K
drivers/pci/pci-label.c new file +26
-0+static char smbios_attr[4096 ]; + +enum smbios_attr_enum { +SMBIOS_ATTR_LABEL_SHOW =1 , +SMBIOS_ATTR_INSTANCE_SHOW , +}; + +static mode_t +smbios_instance_string_exist(struct kobject *kobj, struct attribute *attr, + int attribute) +{ + struct device *dev; + struct pci_dev *pdev; + const struct dmi_device *dmi; + struct dmi_dev_onboard *donboard; + int bus; + int devfn; + + dev = container_of(kobj, struct device, kobj); + pdev = to_pci_dev(dev); + + bus = pdev->bus->number; + devfn = pdev->devfn; + + dmi = NULL; + while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
drivers/pci/pci-label.c line 59
Quoted 10 lines
drivers/pci/pci-label.c new file +10
-0+ NULL, dmi)) != NULL) { + donboard = dmi->device_data; +if (donboard && donboard->bus = bus && + donboard->devfn = devfn) { +if (attribute =SMBIOS_ATTR_INSTANCE_SHOW ) +return scnprintf (smbios_attr,PAGE_SIZE , +"%d\n" , donboard->instance); +else if (attribute =SMBIOS_ATTR_LABEL_SHOW ) +return scnprintf (smbios_attr,PAGE_SIZE , +"%s\n" , dmi->name);
Wait, depending on the attribute you are looking at, you are placing the data in a single buffer? What happens if userspace opens and reads both files at once?
Please don't use this function for your show attributes, just properly copy the correct string into the userspace buffer. This logic just complicates things a lot more, right?
thanks,
greg k-h