1911e1c9bSNarendra K /* 2911e1c9bSNarendra K * Purpose: Export the firmware instance and label associated with 3911e1c9bSNarendra K * a pci device to sysfs 4911e1c9bSNarendra K * Copyright (C) 2010 Dell Inc. 5911e1c9bSNarendra K * by Narendra K <Narendra_K@dell.com>, 6911e1c9bSNarendra K * Jordan Hargrave <Jordan_Hargrave@dell.com> 7911e1c9bSNarendra K * 86058989bSNarendra_K@Dell.com * PCI Firmware Specification Revision 3.1 section 4.6.7 (DSM for Naming a 96058989bSNarendra_K@Dell.com * PCI or PCI Express Device Under Operating Systems) defines an instance 106058989bSNarendra_K@Dell.com * number and string name. This code retrieves them and exports them to sysfs. 116058989bSNarendra_K@Dell.com * If the system firmware does not provide the ACPI _DSM (Device Specific 126058989bSNarendra_K@Dell.com * Method), then the SMBIOS type 41 instance number and string is exported to 136058989bSNarendra_K@Dell.com * sysfs. 146058989bSNarendra_K@Dell.com * 15911e1c9bSNarendra K * SMBIOS defines type 41 for onboard pci devices. This code retrieves 16911e1c9bSNarendra K * the instance number and string from the type 41 record and exports 17911e1c9bSNarendra K * it to sysfs. 18911e1c9bSNarendra K * 19911e1c9bSNarendra K * Please see http://linux.dell.com/wiki/index.php/Oss/libnetdevname for more 20911e1c9bSNarendra K * information. 21911e1c9bSNarendra K */ 22911e1c9bSNarendra K 23911e1c9bSNarendra K #include <linux/dmi.h> 24911e1c9bSNarendra K #include <linux/sysfs.h> 25911e1c9bSNarendra K #include <linux/pci.h> 26911e1c9bSNarendra K #include <linux/pci_ids.h> 27911e1c9bSNarendra K #include <linux/module.h> 28911e1c9bSNarendra K #include <linux/device.h> 296058989bSNarendra_K@Dell.com #include <linux/nls.h> 306058989bSNarendra_K@Dell.com #include <linux/acpi.h> 316058989bSNarendra_K@Dell.com #include <linux/pci-acpi.h> 326058989bSNarendra_K@Dell.com #include <acpi/acpi_bus.h> 33911e1c9bSNarendra K #include "pci.h" 34911e1c9bSNarendra K 356058989bSNarendra_K@Dell.com #define DEVICE_LABEL_DSM 0x07 366058989bSNarendra_K@Dell.com 376058989bSNarendra_K@Dell.com #ifndef CONFIG_DMI 386058989bSNarendra_K@Dell.com 396058989bSNarendra_K@Dell.com static inline int 406058989bSNarendra_K@Dell.com pci_create_smbiosname_file(struct pci_dev *pdev) 416058989bSNarendra_K@Dell.com { 426058989bSNarendra_K@Dell.com return -1; 436058989bSNarendra_K@Dell.com } 446058989bSNarendra_K@Dell.com 456058989bSNarendra_K@Dell.com static inline void 466058989bSNarendra_K@Dell.com pci_remove_smbiosname_file(struct pci_dev *pdev) 476058989bSNarendra_K@Dell.com { 486058989bSNarendra_K@Dell.com } 496058989bSNarendra_K@Dell.com 506058989bSNarendra_K@Dell.com #else 516058989bSNarendra_K@Dell.com 52911e1c9bSNarendra K enum smbios_attr_enum { 53911e1c9bSNarendra K SMBIOS_ATTR_NONE = 0, 54911e1c9bSNarendra K SMBIOS_ATTR_LABEL_SHOW, 55911e1c9bSNarendra K SMBIOS_ATTR_INSTANCE_SHOW, 56911e1c9bSNarendra K }; 57911e1c9bSNarendra K 58ed476418SAl Viro static size_t 59911e1c9bSNarendra K find_smbios_instance_string(struct pci_dev *pdev, char *buf, 60911e1c9bSNarendra K enum smbios_attr_enum attribute) 61911e1c9bSNarendra K { 62911e1c9bSNarendra K const struct dmi_device *dmi; 63911e1c9bSNarendra K struct dmi_dev_onboard *donboard; 64911e1c9bSNarendra K int bus; 65911e1c9bSNarendra K int devfn; 66911e1c9bSNarendra K 67911e1c9bSNarendra K bus = pdev->bus->number; 68911e1c9bSNarendra K devfn = pdev->devfn; 69911e1c9bSNarendra K 70911e1c9bSNarendra K dmi = NULL; 71911e1c9bSNarendra K while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, 72911e1c9bSNarendra K NULL, dmi)) != NULL) { 73911e1c9bSNarendra K donboard = dmi->device_data; 74911e1c9bSNarendra K if (donboard && donboard->bus == bus && 75911e1c9bSNarendra K donboard->devfn == devfn) { 76911e1c9bSNarendra K if (buf) { 77911e1c9bSNarendra K if (attribute == SMBIOS_ATTR_INSTANCE_SHOW) 78911e1c9bSNarendra K return scnprintf(buf, PAGE_SIZE, 79911e1c9bSNarendra K "%d\n", 80911e1c9bSNarendra K donboard->instance); 81911e1c9bSNarendra K else if (attribute == SMBIOS_ATTR_LABEL_SHOW) 82911e1c9bSNarendra K return scnprintf(buf, PAGE_SIZE, 83911e1c9bSNarendra K "%s\n", 84911e1c9bSNarendra K dmi->name); 85911e1c9bSNarendra K } 86911e1c9bSNarendra K return strlen(dmi->name); 87911e1c9bSNarendra K } 88911e1c9bSNarendra K } 89911e1c9bSNarendra K return 0; 90911e1c9bSNarendra K } 91911e1c9bSNarendra K 92587a1f16SAl Viro static umode_t 93911e1c9bSNarendra K smbios_instance_string_exist(struct kobject *kobj, struct attribute *attr, 94911e1c9bSNarendra K int n) 95911e1c9bSNarendra K { 96911e1c9bSNarendra K struct device *dev; 97911e1c9bSNarendra K struct pci_dev *pdev; 98911e1c9bSNarendra K 99911e1c9bSNarendra K dev = container_of(kobj, struct device, kobj); 100911e1c9bSNarendra K pdev = to_pci_dev(dev); 101911e1c9bSNarendra K 102911e1c9bSNarendra K return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ? 103911e1c9bSNarendra K S_IRUGO : 0; 104911e1c9bSNarendra K } 105911e1c9bSNarendra K 106911e1c9bSNarendra K static ssize_t 107911e1c9bSNarendra K smbioslabel_show(struct device *dev, struct device_attribute *attr, char *buf) 108911e1c9bSNarendra K { 109911e1c9bSNarendra K struct pci_dev *pdev; 110911e1c9bSNarendra K pdev = to_pci_dev(dev); 111911e1c9bSNarendra K 112911e1c9bSNarendra K return find_smbios_instance_string(pdev, buf, 113911e1c9bSNarendra K SMBIOS_ATTR_LABEL_SHOW); 114911e1c9bSNarendra K } 115911e1c9bSNarendra K 116911e1c9bSNarendra K static ssize_t 117911e1c9bSNarendra K smbiosinstance_show(struct device *dev, 118911e1c9bSNarendra K struct device_attribute *attr, char *buf) 119911e1c9bSNarendra K { 120911e1c9bSNarendra K struct pci_dev *pdev; 121911e1c9bSNarendra K pdev = to_pci_dev(dev); 122911e1c9bSNarendra K 123911e1c9bSNarendra K return find_smbios_instance_string(pdev, buf, 124911e1c9bSNarendra K SMBIOS_ATTR_INSTANCE_SHOW); 125911e1c9bSNarendra K } 126911e1c9bSNarendra K 127911e1c9bSNarendra K static struct device_attribute smbios_attr_label = { 128763e9db9SStephen Rothwell .attr = {.name = "label", .mode = 0444}, 129911e1c9bSNarendra K .show = smbioslabel_show, 130911e1c9bSNarendra K }; 131911e1c9bSNarendra K 132911e1c9bSNarendra K static struct device_attribute smbios_attr_instance = { 133763e9db9SStephen Rothwell .attr = {.name = "index", .mode = 0444}, 134911e1c9bSNarendra K .show = smbiosinstance_show, 135911e1c9bSNarendra K }; 136911e1c9bSNarendra K 137911e1c9bSNarendra K static struct attribute *smbios_attributes[] = { 138911e1c9bSNarendra K &smbios_attr_label.attr, 139911e1c9bSNarendra K &smbios_attr_instance.attr, 140911e1c9bSNarendra K NULL, 141911e1c9bSNarendra K }; 142911e1c9bSNarendra K 143911e1c9bSNarendra K static struct attribute_group smbios_attr_group = { 144911e1c9bSNarendra K .attrs = smbios_attributes, 145911e1c9bSNarendra K .is_visible = smbios_instance_string_exist, 146911e1c9bSNarendra K }; 147911e1c9bSNarendra K 148911e1c9bSNarendra K static int 149911e1c9bSNarendra K pci_create_smbiosname_file(struct pci_dev *pdev) 150911e1c9bSNarendra K { 1516058989bSNarendra_K@Dell.com return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group); 152911e1c9bSNarendra K } 153911e1c9bSNarendra K 154911e1c9bSNarendra K static void 155911e1c9bSNarendra K pci_remove_smbiosname_file(struct pci_dev *pdev) 156911e1c9bSNarendra K { 157911e1c9bSNarendra K sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group); 158911e1c9bSNarendra K } 159911e1c9bSNarendra K 1606058989bSNarendra_K@Dell.com #endif 1616058989bSNarendra_K@Dell.com 1626058989bSNarendra_K@Dell.com #ifndef CONFIG_ACPI 1636058989bSNarendra_K@Dell.com 1646058989bSNarendra_K@Dell.com static inline int 1656058989bSNarendra_K@Dell.com pci_create_acpi_index_label_files(struct pci_dev *pdev) 1666058989bSNarendra_K@Dell.com { 1676058989bSNarendra_K@Dell.com return -1; 1686058989bSNarendra_K@Dell.com } 1696058989bSNarendra_K@Dell.com 1706058989bSNarendra_K@Dell.com static inline int 1716058989bSNarendra_K@Dell.com pci_remove_acpi_index_label_files(struct pci_dev *pdev) 1726058989bSNarendra_K@Dell.com { 1736058989bSNarendra_K@Dell.com return -1; 1746058989bSNarendra_K@Dell.com } 1756058989bSNarendra_K@Dell.com 17607eefe1cSNarendra_K@Dell.com static inline bool 17707eefe1cSNarendra_K@Dell.com device_has_dsm(struct device *dev) 17807eefe1cSNarendra_K@Dell.com { 17907eefe1cSNarendra_K@Dell.com return false; 18007eefe1cSNarendra_K@Dell.com } 18107eefe1cSNarendra_K@Dell.com 1826058989bSNarendra_K@Dell.com #else 1836058989bSNarendra_K@Dell.com 1846058989bSNarendra_K@Dell.com static const char device_label_dsm_uuid[] = { 1856058989bSNarendra_K@Dell.com 0xD0, 0x37, 0xC9, 0xE5, 0x53, 0x35, 0x7A, 0x4D, 1866058989bSNarendra_K@Dell.com 0x91, 0x17, 0xEA, 0x4D, 0x19, 0xC3, 0x43, 0x4D 1876058989bSNarendra_K@Dell.com }; 1886058989bSNarendra_K@Dell.com 1896058989bSNarendra_K@Dell.com enum acpi_attr_enum { 1906058989bSNarendra_K@Dell.com ACPI_ATTR_LABEL_SHOW, 1916058989bSNarendra_K@Dell.com ACPI_ATTR_INDEX_SHOW, 1926058989bSNarendra_K@Dell.com }; 1936058989bSNarendra_K@Dell.com 1946058989bSNarendra_K@Dell.com static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf) 1956058989bSNarendra_K@Dell.com { 1966058989bSNarendra_K@Dell.com int len; 1971d0fcef7SJiang Liu len = utf16s_to_utf8s((const wchar_t *)obj->string.pointer, 1981d0fcef7SJiang Liu obj->string.length, 1996058989bSNarendra_K@Dell.com UTF16_LITTLE_ENDIAN, 2006058989bSNarendra_K@Dell.com buf, PAGE_SIZE); 2016058989bSNarendra_K@Dell.com buf[len] = '\n'; 2026058989bSNarendra_K@Dell.com } 2036058989bSNarendra_K@Dell.com 2046058989bSNarendra_K@Dell.com static int 2051d0fcef7SJiang Liu dsm_get_label(struct device *dev, char *buf, enum acpi_attr_enum attr) 2066058989bSNarendra_K@Dell.com { 2071d0fcef7SJiang Liu acpi_handle handle; 2081d0fcef7SJiang Liu union acpi_object *obj, *tmp; 2091d0fcef7SJiang Liu int len = -1; 2106058989bSNarendra_K@Dell.com 2111d0fcef7SJiang Liu handle = ACPI_HANDLE(dev); 2121d0fcef7SJiang Liu if (!handle) 2136058989bSNarendra_K@Dell.com return -1; 2146058989bSNarendra_K@Dell.com 2151d0fcef7SJiang Liu obj = acpi_evaluate_dsm(handle, device_label_dsm_uuid, 0x2, 2161d0fcef7SJiang Liu DEVICE_LABEL_DSM, NULL); 2171d0fcef7SJiang Liu if (!obj) 2181d0fcef7SJiang Liu return -1; 2191d0fcef7SJiang Liu 2201d0fcef7SJiang Liu tmp = obj->package.elements; 2211d0fcef7SJiang Liu if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 && 2221d0fcef7SJiang Liu tmp[0].type == ACPI_TYPE_INTEGER && 2231d0fcef7SJiang Liu tmp[1].type == ACPI_TYPE_STRING) { 2241d0fcef7SJiang Liu /* 225*2fc59fe2SJiang Liu * The second string element is optional even when 2261d0fcef7SJiang Liu * this _DSM is implemented; when not implemented, 2271d0fcef7SJiang Liu * this entry must return a null string. 2281d0fcef7SJiang Liu */ 2291d0fcef7SJiang Liu if (attr == ACPI_ATTR_INDEX_SHOW) 230*2fc59fe2SJiang Liu scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value); 2311d0fcef7SJiang Liu else if (attr == ACPI_ATTR_LABEL_SHOW) 2321d0fcef7SJiang Liu dsm_label_utf16s_to_utf8s(tmp + 1, buf); 2331d0fcef7SJiang Liu len = strlen(buf) > 0 ? strlen(buf) : -1; 2346058989bSNarendra_K@Dell.com } 2351d0fcef7SJiang Liu 2361d0fcef7SJiang Liu ACPI_FREE(obj); 2371d0fcef7SJiang Liu 2386058989bSNarendra_K@Dell.com return len; 2396058989bSNarendra_K@Dell.com } 24054e5bb46SJiang Liu 2416058989bSNarendra_K@Dell.com static bool 2426058989bSNarendra_K@Dell.com device_has_dsm(struct device *dev) 2436058989bSNarendra_K@Dell.com { 244*2fc59fe2SJiang Liu acpi_handle handle; 245*2fc59fe2SJiang Liu 246*2fc59fe2SJiang Liu handle = ACPI_HANDLE(dev); 247*2fc59fe2SJiang Liu if (!handle) 248*2fc59fe2SJiang Liu return false; 249*2fc59fe2SJiang Liu 250*2fc59fe2SJiang Liu return !!acpi_check_dsm(handle, device_label_dsm_uuid, 0x2, 251*2fc59fe2SJiang Liu 1 << DEVICE_LABEL_DSM); 2526058989bSNarendra_K@Dell.com } 2536058989bSNarendra_K@Dell.com 254587a1f16SAl Viro static umode_t 2556058989bSNarendra_K@Dell.com acpi_index_string_exist(struct kobject *kobj, struct attribute *attr, int n) 2566058989bSNarendra_K@Dell.com { 2576058989bSNarendra_K@Dell.com struct device *dev; 2586058989bSNarendra_K@Dell.com 2596058989bSNarendra_K@Dell.com dev = container_of(kobj, struct device, kobj); 2606058989bSNarendra_K@Dell.com 2616058989bSNarendra_K@Dell.com if (device_has_dsm(dev)) 2626058989bSNarendra_K@Dell.com return S_IRUGO; 2636058989bSNarendra_K@Dell.com 2646058989bSNarendra_K@Dell.com return 0; 2656058989bSNarendra_K@Dell.com } 2666058989bSNarendra_K@Dell.com 2676058989bSNarendra_K@Dell.com static ssize_t 2686058989bSNarendra_K@Dell.com acpilabel_show(struct device *dev, struct device_attribute *attr, char *buf) 2696058989bSNarendra_K@Dell.com { 2701d0fcef7SJiang Liu return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW); 2716058989bSNarendra_K@Dell.com } 2726058989bSNarendra_K@Dell.com 2736058989bSNarendra_K@Dell.com static ssize_t 2746058989bSNarendra_K@Dell.com acpiindex_show(struct device *dev, struct device_attribute *attr, char *buf) 2756058989bSNarendra_K@Dell.com { 2761d0fcef7SJiang Liu return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW); 2776058989bSNarendra_K@Dell.com } 2786058989bSNarendra_K@Dell.com 2796058989bSNarendra_K@Dell.com static struct device_attribute acpi_attr_label = { 2806058989bSNarendra_K@Dell.com .attr = {.name = "label", .mode = 0444}, 2816058989bSNarendra_K@Dell.com .show = acpilabel_show, 2826058989bSNarendra_K@Dell.com }; 2836058989bSNarendra_K@Dell.com 2846058989bSNarendra_K@Dell.com static struct device_attribute acpi_attr_index = { 2856058989bSNarendra_K@Dell.com .attr = {.name = "acpi_index", .mode = 0444}, 2866058989bSNarendra_K@Dell.com .show = acpiindex_show, 2876058989bSNarendra_K@Dell.com }; 2886058989bSNarendra_K@Dell.com 2896058989bSNarendra_K@Dell.com static struct attribute *acpi_attributes[] = { 2906058989bSNarendra_K@Dell.com &acpi_attr_label.attr, 2916058989bSNarendra_K@Dell.com &acpi_attr_index.attr, 2926058989bSNarendra_K@Dell.com NULL, 2936058989bSNarendra_K@Dell.com }; 2946058989bSNarendra_K@Dell.com 2956058989bSNarendra_K@Dell.com static struct attribute_group acpi_attr_group = { 2966058989bSNarendra_K@Dell.com .attrs = acpi_attributes, 2976058989bSNarendra_K@Dell.com .is_visible = acpi_index_string_exist, 2986058989bSNarendra_K@Dell.com }; 2996058989bSNarendra_K@Dell.com 3006058989bSNarendra_K@Dell.com static int 3016058989bSNarendra_K@Dell.com pci_create_acpi_index_label_files(struct pci_dev *pdev) 3026058989bSNarendra_K@Dell.com { 3036058989bSNarendra_K@Dell.com return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group); 3046058989bSNarendra_K@Dell.com } 3056058989bSNarendra_K@Dell.com 3066058989bSNarendra_K@Dell.com static int 3076058989bSNarendra_K@Dell.com pci_remove_acpi_index_label_files(struct pci_dev *pdev) 3086058989bSNarendra_K@Dell.com { 3096058989bSNarendra_K@Dell.com sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group); 3106058989bSNarendra_K@Dell.com return 0; 3116058989bSNarendra_K@Dell.com } 3126058989bSNarendra_K@Dell.com #endif 3136058989bSNarendra_K@Dell.com 314911e1c9bSNarendra K void pci_create_firmware_label_files(struct pci_dev *pdev) 315911e1c9bSNarendra K { 3166058989bSNarendra_K@Dell.com if (device_has_dsm(&pdev->dev)) 3176058989bSNarendra_K@Dell.com pci_create_acpi_index_label_files(pdev); 3186058989bSNarendra_K@Dell.com else 3196058989bSNarendra_K@Dell.com pci_create_smbiosname_file(pdev); 320911e1c9bSNarendra K } 321911e1c9bSNarendra K 322911e1c9bSNarendra K void pci_remove_firmware_label_files(struct pci_dev *pdev) 323911e1c9bSNarendra K { 3246058989bSNarendra_K@Dell.com if (device_has_dsm(&pdev->dev)) 3256058989bSNarendra_K@Dell.com pci_remove_acpi_index_label_files(pdev); 3266058989bSNarendra_K@Dell.com else 327911e1c9bSNarendra K pci_remove_smbiosname_file(pdev); 328911e1c9bSNarendra K } 329