1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2911e1c9bSNarendra K /* 3df62ab5eSBjorn Helgaas * Export the firmware instance and label associated with a PCI device to 4df62ab5eSBjorn Helgaas * sysfs 5df62ab5eSBjorn Helgaas * 6911e1c9bSNarendra K * Copyright (C) 2010 Dell Inc. 7911e1c9bSNarendra K * by Narendra K <Narendra_K@dell.com>, 8911e1c9bSNarendra K * Jordan Hargrave <Jordan_Hargrave@dell.com> 9911e1c9bSNarendra K * 106058989bSNarendra_K@Dell.com * PCI Firmware Specification Revision 3.1 section 4.6.7 (DSM for Naming a 116058989bSNarendra_K@Dell.com * PCI or PCI Express Device Under Operating Systems) defines an instance 126058989bSNarendra_K@Dell.com * number and string name. This code retrieves them and exports them to sysfs. 136058989bSNarendra_K@Dell.com * If the system firmware does not provide the ACPI _DSM (Device Specific 146058989bSNarendra_K@Dell.com * Method), then the SMBIOS type 41 instance number and string is exported to 156058989bSNarendra_K@Dell.com * sysfs. 166058989bSNarendra_K@Dell.com * 17911e1c9bSNarendra K * SMBIOS defines type 41 for onboard pci devices. This code retrieves 18911e1c9bSNarendra K * the instance number and string from the type 41 record and exports 19911e1c9bSNarendra K * it to sysfs. 20911e1c9bSNarendra K * 217ecd4a81SAlexander A. Klimov * Please see https://linux.dell.com/files/biosdevname/ for more 22911e1c9bSNarendra K * information. 23911e1c9bSNarendra K */ 24911e1c9bSNarendra K 25911e1c9bSNarendra K #include <linux/dmi.h> 26911e1c9bSNarendra K #include <linux/sysfs.h> 27911e1c9bSNarendra K #include <linux/pci.h> 28911e1c9bSNarendra K #include <linux/pci_ids.h> 29911e1c9bSNarendra K #include <linux/module.h> 30911e1c9bSNarendra K #include <linux/device.h> 316058989bSNarendra_K@Dell.com #include <linux/nls.h> 326058989bSNarendra_K@Dell.com #include <linux/acpi.h> 336058989bSNarendra_K@Dell.com #include <linux/pci-acpi.h> 34911e1c9bSNarendra K #include "pci.h" 35911e1c9bSNarendra K 361017275dSKrzysztof Wilczyński static bool device_has_acpi_name(struct device *dev) 371017275dSKrzysztof Wilczyński { 381017275dSKrzysztof Wilczyński #ifdef CONFIG_ACPI 391017275dSKrzysztof Wilczyński acpi_handle handle; 401017275dSKrzysztof Wilczyński 411017275dSKrzysztof Wilczyński handle = ACPI_HANDLE(dev); 421017275dSKrzysztof Wilczyński if (!handle) 431017275dSKrzysztof Wilczyński return false; 441017275dSKrzysztof Wilczyński 451017275dSKrzysztof Wilczyński return acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2, 461017275dSKrzysztof Wilczyński 1 << DSM_PCI_DEVICE_NAME); 471017275dSKrzysztof Wilczyński #else 481017275dSKrzysztof Wilczyński return false; 491017275dSKrzysztof Wilczyński #endif 501017275dSKrzysztof Wilczyński } 511017275dSKrzysztof Wilczyński 524c859804SBjorn Helgaas #ifdef CONFIG_DMI 53911e1c9bSNarendra K enum smbios_attr_enum { 54911e1c9bSNarendra K SMBIOS_ATTR_NONE = 0, 55911e1c9bSNarendra K SMBIOS_ATTR_LABEL_SHOW, 56911e1c9bSNarendra K SMBIOS_ATTR_INSTANCE_SHOW, 57911e1c9bSNarendra K }; 58911e1c9bSNarendra K 593c78bc61SRyan Desfosses static size_t 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; 646c51c82cSSujith Pandel int domain_nr; 65911e1c9bSNarendra K int bus; 66911e1c9bSNarendra K int devfn; 67911e1c9bSNarendra K 686c51c82cSSujith Pandel domain_nr = pci_domain_nr(pdev->bus); 69911e1c9bSNarendra K bus = pdev->bus->number; 70911e1c9bSNarendra K devfn = pdev->devfn; 71911e1c9bSNarendra K 72911e1c9bSNarendra K dmi = NULL; 73911e1c9bSNarendra K while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, 74911e1c9bSNarendra K NULL, dmi)) != NULL) { 75911e1c9bSNarendra K donboard = dmi->device_data; 766c51c82cSSujith Pandel if (donboard && donboard->segment == domain_nr && 776c51c82cSSujith Pandel donboard->bus == bus && 78911e1c9bSNarendra K donboard->devfn == devfn) { 79911e1c9bSNarendra K if (buf) { 80911e1c9bSNarendra K if (attribute == SMBIOS_ATTR_INSTANCE_SHOW) 81911e1c9bSNarendra K return scnprintf(buf, PAGE_SIZE, 82911e1c9bSNarendra K "%d\n", 83911e1c9bSNarendra K donboard->instance); 84911e1c9bSNarendra K else if (attribute == SMBIOS_ATTR_LABEL_SHOW) 85911e1c9bSNarendra K return scnprintf(buf, PAGE_SIZE, 86911e1c9bSNarendra K "%s\n", 87911e1c9bSNarendra K dmi->name); 88911e1c9bSNarendra K } 89911e1c9bSNarendra K return strlen(dmi->name); 90911e1c9bSNarendra K } 91911e1c9bSNarendra K } 92911e1c9bSNarendra K return 0; 93911e1c9bSNarendra K } 94911e1c9bSNarendra K 954dd7dfa1SKrzysztof Wilczyński static umode_t smbios_attr_is_visible(struct kobject *kobj, struct attribute *a, 964dd7dfa1SKrzysztof Wilczyński int n) 97911e1c9bSNarendra K { 98911e1c9bSNarendra K struct device *dev; 99911e1c9bSNarendra K struct pci_dev *pdev; 100911e1c9bSNarendra K 101554a6037SGeliang Tang dev = kobj_to_dev(kobj); 102911e1c9bSNarendra K pdev = to_pci_dev(dev); 103911e1c9bSNarendra K 104*506140f9SKrzysztof Wilczyński if (device_has_acpi_name(dev)) 105*506140f9SKrzysztof Wilczyński return 0; 106*506140f9SKrzysztof Wilczyński 1074dd7dfa1SKrzysztof Wilczyński if (!find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE)) 1084dd7dfa1SKrzysztof Wilczyński return 0; 1094dd7dfa1SKrzysztof Wilczyński 1104dd7dfa1SKrzysztof Wilczyński return a->mode; 111911e1c9bSNarendra K } 112911e1c9bSNarendra K 1134dd7dfa1SKrzysztof Wilczyński static ssize_t smbios_label_show(struct device *dev, 1143c78bc61SRyan Desfosses struct device_attribute *attr, char *buf) 115911e1c9bSNarendra K { 116911e1c9bSNarendra K struct pci_dev *pdev; 117911e1c9bSNarendra K pdev = to_pci_dev(dev); 118911e1c9bSNarendra K 119911e1c9bSNarendra K return find_smbios_instance_string(pdev, buf, 120911e1c9bSNarendra K SMBIOS_ATTR_LABEL_SHOW); 121911e1c9bSNarendra K } 1224dd7dfa1SKrzysztof Wilczyński static struct device_attribute dev_attr_smbios_label = __ATTR(label, 0444, 1234dd7dfa1SKrzysztof Wilczyński smbios_label_show, NULL); 124911e1c9bSNarendra K 1254dd7dfa1SKrzysztof Wilczyński static ssize_t index_show(struct device *dev, struct device_attribute *attr, 1264dd7dfa1SKrzysztof Wilczyński char *buf) 127911e1c9bSNarendra K { 128911e1c9bSNarendra K struct pci_dev *pdev; 129911e1c9bSNarendra K pdev = to_pci_dev(dev); 130911e1c9bSNarendra K 131911e1c9bSNarendra K return find_smbios_instance_string(pdev, buf, 132911e1c9bSNarendra K SMBIOS_ATTR_INSTANCE_SHOW); 133911e1c9bSNarendra K } 1344dd7dfa1SKrzysztof Wilczyński static DEVICE_ATTR_RO(index); 135911e1c9bSNarendra K 1364dd7dfa1SKrzysztof Wilczyński static struct attribute *smbios_attrs[] = { 1374dd7dfa1SKrzysztof Wilczyński &dev_attr_smbios_label.attr, 1384dd7dfa1SKrzysztof Wilczyński &dev_attr_index.attr, 139911e1c9bSNarendra K NULL, 140911e1c9bSNarendra K }; 141911e1c9bSNarendra K 142*506140f9SKrzysztof Wilczyński const struct attribute_group pci_dev_smbios_attr_group = { 1434dd7dfa1SKrzysztof Wilczyński .attrs = smbios_attrs, 1444dd7dfa1SKrzysztof Wilczyński .is_visible = smbios_attr_is_visible, 145911e1c9bSNarendra K }; 1466058989bSNarendra_K@Dell.com #endif 1476058989bSNarendra_K@Dell.com 1484c859804SBjorn Helgaas #ifdef CONFIG_ACPI 1496058989bSNarendra_K@Dell.com enum acpi_attr_enum { 1506058989bSNarendra_K@Dell.com ACPI_ATTR_LABEL_SHOW, 1516058989bSNarendra_K@Dell.com ACPI_ATTR_INDEX_SHOW, 1526058989bSNarendra_K@Dell.com }; 1536058989bSNarendra_K@Dell.com 1546058989bSNarendra_K@Dell.com static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf) 1556058989bSNarendra_K@Dell.com { 1566058989bSNarendra_K@Dell.com int len; 157dcfa9be8SSimone Gotti len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer, 158dcfa9be8SSimone Gotti obj->buffer.length, 1596058989bSNarendra_K@Dell.com UTF16_LITTLE_ENDIAN, 1606058989bSNarendra_K@Dell.com buf, PAGE_SIZE); 1616058989bSNarendra_K@Dell.com buf[len] = '\n'; 1626058989bSNarendra_K@Dell.com } 1636058989bSNarendra_K@Dell.com 1643c78bc61SRyan Desfosses static int dsm_get_label(struct device *dev, char *buf, 1653c78bc61SRyan Desfosses enum acpi_attr_enum attr) 1666058989bSNarendra_K@Dell.com { 1671d0fcef7SJiang Liu acpi_handle handle; 1681d0fcef7SJiang Liu union acpi_object *obj, *tmp; 1691d0fcef7SJiang Liu int len = -1; 1706058989bSNarendra_K@Dell.com 1711d0fcef7SJiang Liu handle = ACPI_HANDLE(dev); 1721d0fcef7SJiang Liu if (!handle) 1736058989bSNarendra_K@Dell.com return -1; 1746058989bSNarendra_K@Dell.com 17594116f81SAndy Shevchenko obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 0x2, 1763910ebacSKrzysztof Wilczyński DSM_PCI_DEVICE_NAME, NULL); 1771d0fcef7SJiang Liu if (!obj) 1781d0fcef7SJiang Liu return -1; 1796058989bSNarendra_K@Dell.com 1801d0fcef7SJiang Liu tmp = obj->package.elements; 1811d0fcef7SJiang Liu if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 && 1821d0fcef7SJiang Liu tmp[0].type == ACPI_TYPE_INTEGER && 183dcfa9be8SSimone Gotti (tmp[1].type == ACPI_TYPE_STRING || 184dcfa9be8SSimone Gotti tmp[1].type == ACPI_TYPE_BUFFER)) { 1851d0fcef7SJiang Liu /* 1862fc59fe2SJiang Liu * The second string element is optional even when 1871d0fcef7SJiang Liu * this _DSM is implemented; when not implemented, 1881d0fcef7SJiang Liu * this entry must return a null string. 1891d0fcef7SJiang Liu */ 190dcfa9be8SSimone Gotti if (attr == ACPI_ATTR_INDEX_SHOW) { 1912fc59fe2SJiang Liu scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value); 192dcfa9be8SSimone Gotti } else if (attr == ACPI_ATTR_LABEL_SHOW) { 193dcfa9be8SSimone Gotti if (tmp[1].type == ACPI_TYPE_STRING) 194dcfa9be8SSimone Gotti scnprintf(buf, PAGE_SIZE, "%s\n", 195dcfa9be8SSimone Gotti tmp[1].string.pointer); 196dcfa9be8SSimone Gotti else if (tmp[1].type == ACPI_TYPE_BUFFER) 1971d0fcef7SJiang Liu dsm_label_utf16s_to_utf8s(tmp + 1, buf); 198dcfa9be8SSimone Gotti } 1991d0fcef7SJiang Liu len = strlen(buf) > 0 ? strlen(buf) : -1; 2006058989bSNarendra_K@Dell.com } 2011d0fcef7SJiang Liu 2021d0fcef7SJiang Liu ACPI_FREE(obj); 2031d0fcef7SJiang Liu 2046058989bSNarendra_K@Dell.com return len; 2056058989bSNarendra_K@Dell.com } 2066058989bSNarendra_K@Dell.com 2072ed64941SKrzysztof Wilczyński static umode_t acpi_attr_is_visible(struct kobject *kobj, struct attribute *a, 2082ed64941SKrzysztof Wilczyński int n) 2096058989bSNarendra_K@Dell.com { 2106058989bSNarendra_K@Dell.com struct device *dev; 2116058989bSNarendra_K@Dell.com 212554a6037SGeliang Tang dev = kobj_to_dev(kobj); 2136058989bSNarendra_K@Dell.com 2142ed64941SKrzysztof Wilczyński if (!device_has_acpi_name(dev)) 2156058989bSNarendra_K@Dell.com return 0; 2162ed64941SKrzysztof Wilczyński 2172ed64941SKrzysztof Wilczyński return a->mode; 2186058989bSNarendra_K@Dell.com } 2196058989bSNarendra_K@Dell.com 2202ed64941SKrzysztof Wilczyński static ssize_t label_show(struct device *dev, struct device_attribute *attr, 2212ed64941SKrzysztof Wilczyński char *buf) 2226058989bSNarendra_K@Dell.com { 2231d0fcef7SJiang Liu return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW); 2246058989bSNarendra_K@Dell.com } 2252ed64941SKrzysztof Wilczyński static DEVICE_ATTR_RO(label); 2266058989bSNarendra_K@Dell.com 2272ed64941SKrzysztof Wilczyński static ssize_t acpi_index_show(struct device *dev, 2283c78bc61SRyan Desfosses struct device_attribute *attr, char *buf) 2296058989bSNarendra_K@Dell.com { 2301d0fcef7SJiang Liu return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW); 2316058989bSNarendra_K@Dell.com } 2322ed64941SKrzysztof Wilczyński static DEVICE_ATTR_RO(acpi_index); 2336058989bSNarendra_K@Dell.com 2342ed64941SKrzysztof Wilczyński static struct attribute *acpi_attrs[] = { 2352ed64941SKrzysztof Wilczyński &dev_attr_label.attr, 2362ed64941SKrzysztof Wilczyński &dev_attr_acpi_index.attr, 2376058989bSNarendra_K@Dell.com NULL, 2386058989bSNarendra_K@Dell.com }; 2396058989bSNarendra_K@Dell.com 240*506140f9SKrzysztof Wilczyński const struct attribute_group pci_dev_acpi_attr_group = { 2412ed64941SKrzysztof Wilczyński .attrs = acpi_attrs, 2422ed64941SKrzysztof Wilczyński .is_visible = acpi_attr_is_visible, 2436058989bSNarendra_K@Dell.com }; 2446058989bSNarendra_K@Dell.com #endif 245