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
device_has_acpi_name(struct device * dev)361017275dSKrzysztof Wilczyński static bool device_has_acpi_name(struct device *dev)
371017275dSKrzysztof Wilczyński {
381017275dSKrzysztof Wilczyński #ifdef CONFIG_ACPI
39362fb766SKrzysztof Wilczyński acpi_handle handle = ACPI_HANDLE(dev);
401017275dSKrzysztof Wilczyński
411017275dSKrzysztof Wilczyński if (!handle)
421017275dSKrzysztof Wilczyński return false;
431017275dSKrzysztof Wilczyński
441017275dSKrzysztof Wilczyński return acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2,
451017275dSKrzysztof Wilczyński 1 << DSM_PCI_DEVICE_NAME);
461017275dSKrzysztof Wilczyński #else
471017275dSKrzysztof Wilczyński return false;
481017275dSKrzysztof Wilczyński #endif
491017275dSKrzysztof Wilczyński }
501017275dSKrzysztof Wilczyński
514c859804SBjorn Helgaas #ifdef CONFIG_DMI
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
find_smbios_instance_string(struct pci_dev * pdev,char * buf,enum smbios_attr_enum attribute)583c78bc61SRyan Desfosses static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
59911e1c9bSNarendra K enum smbios_attr_enum attribute)
60911e1c9bSNarendra K {
61911e1c9bSNarendra K const struct dmi_device *dmi;
62911e1c9bSNarendra K struct dmi_dev_onboard *donboard;
63362fb766SKrzysztof Wilczyński int domain_nr = pci_domain_nr(pdev->bus);
64362fb766SKrzysztof Wilczyński int bus = pdev->bus->number;
65362fb766SKrzysztof Wilczyński int devfn = pdev->devfn;
66911e1c9bSNarendra K
67911e1c9bSNarendra K dmi = NULL;
68911e1c9bSNarendra K while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
69911e1c9bSNarendra K NULL, dmi)) != NULL) {
70911e1c9bSNarendra K donboard = dmi->device_data;
716c51c82cSSujith Pandel if (donboard && donboard->segment == domain_nr &&
726c51c82cSSujith Pandel donboard->bus == bus &&
73911e1c9bSNarendra K donboard->devfn == devfn) {
74911e1c9bSNarendra K if (buf) {
75911e1c9bSNarendra K if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
76ad025f8eSKrzysztof Wilczyński return sysfs_emit(buf, "%d\n",
77911e1c9bSNarendra K donboard->instance);
78911e1c9bSNarendra K else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
79ad025f8eSKrzysztof Wilczyński return sysfs_emit(buf, "%s\n",
80911e1c9bSNarendra K dmi->name);
81911e1c9bSNarendra K }
82911e1c9bSNarendra K return strlen(dmi->name);
83911e1c9bSNarendra K }
84911e1c9bSNarendra K }
85911e1c9bSNarendra K return 0;
86911e1c9bSNarendra K }
87911e1c9bSNarendra K
smbios_label_show(struct device * dev,struct device_attribute * attr,char * buf)884dd7dfa1SKrzysztof Wilczyński static ssize_t smbios_label_show(struct device *dev,
893c78bc61SRyan Desfosses struct device_attribute *attr, char *buf)
90911e1c9bSNarendra K {
91362fb766SKrzysztof Wilczyński struct pci_dev *pdev = to_pci_dev(dev);
92911e1c9bSNarendra K
93911e1c9bSNarendra K return find_smbios_instance_string(pdev, buf,
94911e1c9bSNarendra K SMBIOS_ATTR_LABEL_SHOW);
95911e1c9bSNarendra K }
964dd7dfa1SKrzysztof Wilczyński static struct device_attribute dev_attr_smbios_label = __ATTR(label, 0444,
974dd7dfa1SKrzysztof Wilczyński smbios_label_show, NULL);
98911e1c9bSNarendra K
index_show(struct device * dev,struct device_attribute * attr,char * buf)994dd7dfa1SKrzysztof Wilczyński static ssize_t index_show(struct device *dev, struct device_attribute *attr,
1004dd7dfa1SKrzysztof Wilczyński char *buf)
101911e1c9bSNarendra K {
102362fb766SKrzysztof Wilczyński struct pci_dev *pdev = to_pci_dev(dev);
103911e1c9bSNarendra K
104911e1c9bSNarendra K return find_smbios_instance_string(pdev, buf,
105911e1c9bSNarendra K SMBIOS_ATTR_INSTANCE_SHOW);
106911e1c9bSNarendra K }
1074dd7dfa1SKrzysztof Wilczyński static DEVICE_ATTR_RO(index);
108911e1c9bSNarendra K
1094dd7dfa1SKrzysztof Wilczyński static struct attribute *smbios_attrs[] = {
1104dd7dfa1SKrzysztof Wilczyński &dev_attr_smbios_label.attr,
1114dd7dfa1SKrzysztof Wilczyński &dev_attr_index.attr,
112911e1c9bSNarendra K NULL,
113911e1c9bSNarendra K };
114911e1c9bSNarendra K
smbios_attr_is_visible(struct kobject * kobj,struct attribute * a,int n)115df1af7cbSKrzysztof Wilczyński static umode_t smbios_attr_is_visible(struct kobject *kobj, struct attribute *a,
116df1af7cbSKrzysztof Wilczyński int n)
117df1af7cbSKrzysztof Wilczyński {
118df1af7cbSKrzysztof Wilczyński struct device *dev = kobj_to_dev(kobj);
119df1af7cbSKrzysztof Wilczyński struct pci_dev *pdev = to_pci_dev(dev);
120df1af7cbSKrzysztof Wilczyński
121df1af7cbSKrzysztof Wilczyński if (device_has_acpi_name(dev))
122df1af7cbSKrzysztof Wilczyński return 0;
123df1af7cbSKrzysztof Wilczyński
124df1af7cbSKrzysztof Wilczyński if (!find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE))
125df1af7cbSKrzysztof Wilczyński return 0;
126df1af7cbSKrzysztof Wilczyński
127df1af7cbSKrzysztof Wilczyński return a->mode;
128df1af7cbSKrzysztof Wilczyński }
129df1af7cbSKrzysztof Wilczyński
130506140f9SKrzysztof Wilczyński const struct attribute_group pci_dev_smbios_attr_group = {
1314dd7dfa1SKrzysztof Wilczyński .attrs = smbios_attrs,
1324dd7dfa1SKrzysztof Wilczyński .is_visible = smbios_attr_is_visible,
133911e1c9bSNarendra K };
1346058989bSNarendra_K@Dell.com #endif
1356058989bSNarendra_K@Dell.com
1364c859804SBjorn Helgaas #ifdef CONFIG_ACPI
1376058989bSNarendra_K@Dell.com enum acpi_attr_enum {
1386058989bSNarendra_K@Dell.com ACPI_ATTR_LABEL_SHOW,
1396058989bSNarendra_K@Dell.com ACPI_ATTR_INDEX_SHOW,
1406058989bSNarendra_K@Dell.com };
1416058989bSNarendra_K@Dell.com
dsm_label_utf16s_to_utf8s(union acpi_object * obj,char * buf)142316ae330SKrzysztof Wilczyński static int dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
1436058989bSNarendra_K@Dell.com {
1446058989bSNarendra_K@Dell.com int len;
145316ae330SKrzysztof Wilczyński
146dcfa9be8SSimone Gotti len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer,
147dcfa9be8SSimone Gotti obj->buffer.length,
1486058989bSNarendra_K@Dell.com UTF16_LITTLE_ENDIAN,
149bdcdaa13SKrzysztof Wilczyński buf, PAGE_SIZE - 1);
150316ae330SKrzysztof Wilczyński buf[len++] = '\n';
151316ae330SKrzysztof Wilczyński
152316ae330SKrzysztof Wilczyński return len;
1536058989bSNarendra_K@Dell.com }
1546058989bSNarendra_K@Dell.com
dsm_get_label(struct device * dev,char * buf,enum acpi_attr_enum attr)1553c78bc61SRyan Desfosses static int dsm_get_label(struct device *dev, char *buf,
1563c78bc61SRyan Desfosses enum acpi_attr_enum attr)
1576058989bSNarendra_K@Dell.com {
158362fb766SKrzysztof Wilczyński acpi_handle handle = ACPI_HANDLE(dev);
1591d0fcef7SJiang Liu union acpi_object *obj, *tmp;
160316ae330SKrzysztof Wilczyński int len = 0;
1616058989bSNarendra_K@Dell.com
1621d0fcef7SJiang Liu if (!handle)
1636058989bSNarendra_K@Dell.com return -1;
1646058989bSNarendra_K@Dell.com
16594116f81SAndy Shevchenko obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 0x2,
1663910ebacSKrzysztof Wilczyński DSM_PCI_DEVICE_NAME, NULL);
1671d0fcef7SJiang Liu if (!obj)
1681d0fcef7SJiang Liu return -1;
1696058989bSNarendra_K@Dell.com
1701d0fcef7SJiang Liu tmp = obj->package.elements;
1711d0fcef7SJiang Liu if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
1721d0fcef7SJiang Liu tmp[0].type == ACPI_TYPE_INTEGER &&
173dcfa9be8SSimone Gotti (tmp[1].type == ACPI_TYPE_STRING ||
174dcfa9be8SSimone Gotti tmp[1].type == ACPI_TYPE_BUFFER)) {
1751d0fcef7SJiang Liu /*
1762fc59fe2SJiang Liu * The second string element is optional even when
1771d0fcef7SJiang Liu * this _DSM is implemented; when not implemented,
1781d0fcef7SJiang Liu * this entry must return a null string.
1791d0fcef7SJiang Liu */
180dcfa9be8SSimone Gotti if (attr == ACPI_ATTR_INDEX_SHOW) {
181*f8cf6e51SKrzysztof Wilczyński len = sysfs_emit(buf, "%llu\n", tmp->integer.value);
182dcfa9be8SSimone Gotti } else if (attr == ACPI_ATTR_LABEL_SHOW) {
183dcfa9be8SSimone Gotti if (tmp[1].type == ACPI_TYPE_STRING)
184*f8cf6e51SKrzysztof Wilczyński len = sysfs_emit(buf, "%s\n",
185dcfa9be8SSimone Gotti tmp[1].string.pointer);
186dcfa9be8SSimone Gotti else if (tmp[1].type == ACPI_TYPE_BUFFER)
187316ae330SKrzysztof Wilczyński len = dsm_label_utf16s_to_utf8s(tmp + 1, buf);
188dcfa9be8SSimone Gotti }
1896058989bSNarendra_K@Dell.com }
1901d0fcef7SJiang Liu
1911d0fcef7SJiang Liu ACPI_FREE(obj);
1921d0fcef7SJiang Liu
193316ae330SKrzysztof Wilczyński return len > 0 ? len : -1;
1946058989bSNarendra_K@Dell.com }
1956058989bSNarendra_K@Dell.com
label_show(struct device * dev,struct device_attribute * attr,char * buf)1962ed64941SKrzysztof Wilczyński static ssize_t label_show(struct device *dev, struct device_attribute *attr,
1972ed64941SKrzysztof Wilczyński char *buf)
1986058989bSNarendra_K@Dell.com {
1991d0fcef7SJiang Liu return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
2006058989bSNarendra_K@Dell.com }
2012ed64941SKrzysztof Wilczyński static DEVICE_ATTR_RO(label);
2026058989bSNarendra_K@Dell.com
acpi_index_show(struct device * dev,struct device_attribute * attr,char * buf)2032ed64941SKrzysztof Wilczyński static ssize_t acpi_index_show(struct device *dev,
2043c78bc61SRyan Desfosses struct device_attribute *attr, char *buf)
2056058989bSNarendra_K@Dell.com {
2061d0fcef7SJiang Liu return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
2076058989bSNarendra_K@Dell.com }
2082ed64941SKrzysztof Wilczyński static DEVICE_ATTR_RO(acpi_index);
2096058989bSNarendra_K@Dell.com
2102ed64941SKrzysztof Wilczyński static struct attribute *acpi_attrs[] = {
2112ed64941SKrzysztof Wilczyński &dev_attr_label.attr,
2122ed64941SKrzysztof Wilczyński &dev_attr_acpi_index.attr,
2136058989bSNarendra_K@Dell.com NULL,
2146058989bSNarendra_K@Dell.com };
2156058989bSNarendra_K@Dell.com
acpi_attr_is_visible(struct kobject * kobj,struct attribute * a,int n)216df1af7cbSKrzysztof Wilczyński static umode_t acpi_attr_is_visible(struct kobject *kobj, struct attribute *a,
217df1af7cbSKrzysztof Wilczyński int n)
218df1af7cbSKrzysztof Wilczyński {
219df1af7cbSKrzysztof Wilczyński struct device *dev = kobj_to_dev(kobj);
220df1af7cbSKrzysztof Wilczyński
221df1af7cbSKrzysztof Wilczyński if (!device_has_acpi_name(dev))
222df1af7cbSKrzysztof Wilczyński return 0;
223df1af7cbSKrzysztof Wilczyński
224df1af7cbSKrzysztof Wilczyński return a->mode;
225df1af7cbSKrzysztof Wilczyński }
226df1af7cbSKrzysztof Wilczyński
227506140f9SKrzysztof Wilczyński const struct attribute_group pci_dev_acpi_attr_group = {
2282ed64941SKrzysztof Wilczyński .attrs = acpi_attrs,
2292ed64941SKrzysztof Wilczyński .is_visible = acpi_attr_is_visible,
2306058989bSNarendra_K@Dell.com };
2316058989bSNarendra_K@Dell.com #endif
232