xref: /openbmc/linux/drivers/pci/pci-label.c (revision df1af7cb)
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
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 
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)
76911e1c9bSNarendra K 					return scnprintf(buf, PAGE_SIZE,
77911e1c9bSNarendra K 							 "%d\n",
78911e1c9bSNarendra K 							 donboard->instance);
79911e1c9bSNarendra K 				else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
80911e1c9bSNarendra K 					return scnprintf(buf, PAGE_SIZE,
81911e1c9bSNarendra K 							 "%s\n",
82911e1c9bSNarendra K 							 dmi->name);
83911e1c9bSNarendra K 			}
84911e1c9bSNarendra K 			return strlen(dmi->name);
85911e1c9bSNarendra K 		}
86911e1c9bSNarendra K 	}
87911e1c9bSNarendra K 	return 0;
88911e1c9bSNarendra K }
89911e1c9bSNarendra K 
904dd7dfa1SKrzysztof Wilczyński static ssize_t smbios_label_show(struct device *dev,
913c78bc61SRyan Desfosses 				 struct device_attribute *attr, char *buf)
92911e1c9bSNarendra K {
93362fb766SKrzysztof Wilczyński 	struct pci_dev *pdev = to_pci_dev(dev);
94911e1c9bSNarendra K 
95911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, buf,
96911e1c9bSNarendra K 					   SMBIOS_ATTR_LABEL_SHOW);
97911e1c9bSNarendra K }
984dd7dfa1SKrzysztof Wilczyński static struct device_attribute dev_attr_smbios_label = __ATTR(label, 0444,
994dd7dfa1SKrzysztof Wilczyński 						    smbios_label_show, NULL);
100911e1c9bSNarendra K 
1014dd7dfa1SKrzysztof Wilczyński static ssize_t index_show(struct device *dev, struct device_attribute *attr,
1024dd7dfa1SKrzysztof Wilczyński 			  char *buf)
103911e1c9bSNarendra K {
104362fb766SKrzysztof Wilczyński 	struct pci_dev *pdev = to_pci_dev(dev);
105911e1c9bSNarendra K 
106911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, buf,
107911e1c9bSNarendra K 					   SMBIOS_ATTR_INSTANCE_SHOW);
108911e1c9bSNarendra K }
1094dd7dfa1SKrzysztof Wilczyński static DEVICE_ATTR_RO(index);
110911e1c9bSNarendra K 
1114dd7dfa1SKrzysztof Wilczyński static struct attribute *smbios_attrs[] = {
1124dd7dfa1SKrzysztof Wilczyński 	&dev_attr_smbios_label.attr,
1134dd7dfa1SKrzysztof Wilczyński 	&dev_attr_index.attr,
114911e1c9bSNarendra K 	NULL,
115911e1c9bSNarendra K };
116911e1c9bSNarendra K 
117*df1af7cbSKrzysztof Wilczyński static umode_t smbios_attr_is_visible(struct kobject *kobj, struct attribute *a,
118*df1af7cbSKrzysztof Wilczyński 				      int n)
119*df1af7cbSKrzysztof Wilczyński {
120*df1af7cbSKrzysztof Wilczyński 	struct device *dev = kobj_to_dev(kobj);
121*df1af7cbSKrzysztof Wilczyński 	struct pci_dev *pdev = to_pci_dev(dev);
122*df1af7cbSKrzysztof Wilczyński 
123*df1af7cbSKrzysztof Wilczyński 	if (device_has_acpi_name(dev))
124*df1af7cbSKrzysztof Wilczyński 		return 0;
125*df1af7cbSKrzysztof Wilczyński 
126*df1af7cbSKrzysztof Wilczyński 	if (!find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE))
127*df1af7cbSKrzysztof Wilczyński 		return 0;
128*df1af7cbSKrzysztof Wilczyński 
129*df1af7cbSKrzysztof Wilczyński 	return a->mode;
130*df1af7cbSKrzysztof Wilczyński }
131*df1af7cbSKrzysztof Wilczyński 
132506140f9SKrzysztof Wilczyński const struct attribute_group pci_dev_smbios_attr_group = {
1334dd7dfa1SKrzysztof Wilczyński 	.attrs = smbios_attrs,
1344dd7dfa1SKrzysztof Wilczyński 	.is_visible = smbios_attr_is_visible,
135911e1c9bSNarendra K };
1366058989bSNarendra_K@Dell.com #endif
1376058989bSNarendra_K@Dell.com 
1384c859804SBjorn Helgaas #ifdef CONFIG_ACPI
1396058989bSNarendra_K@Dell.com enum acpi_attr_enum {
1406058989bSNarendra_K@Dell.com 	ACPI_ATTR_LABEL_SHOW,
1416058989bSNarendra_K@Dell.com 	ACPI_ATTR_INDEX_SHOW,
1426058989bSNarendra_K@Dell.com };
1436058989bSNarendra_K@Dell.com 
1446058989bSNarendra_K@Dell.com static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
1456058989bSNarendra_K@Dell.com {
1466058989bSNarendra_K@Dell.com 	int len;
147dcfa9be8SSimone Gotti 	len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer,
148dcfa9be8SSimone Gotti 			      obj->buffer.length,
1496058989bSNarendra_K@Dell.com 			      UTF16_LITTLE_ENDIAN,
1506058989bSNarendra_K@Dell.com 			      buf, PAGE_SIZE);
1516058989bSNarendra_K@Dell.com 	buf[len] = '\n';
1526058989bSNarendra_K@Dell.com }
1536058989bSNarendra_K@Dell.com 
1543c78bc61SRyan Desfosses static int dsm_get_label(struct device *dev, char *buf,
1553c78bc61SRyan Desfosses 			 enum acpi_attr_enum attr)
1566058989bSNarendra_K@Dell.com {
157362fb766SKrzysztof Wilczyński 	acpi_handle handle = ACPI_HANDLE(dev);
1581d0fcef7SJiang Liu 	union acpi_object *obj, *tmp;
1591d0fcef7SJiang Liu 	int len = -1;
1606058989bSNarendra_K@Dell.com 
1611d0fcef7SJiang Liu 	if (!handle)
1626058989bSNarendra_K@Dell.com 		return -1;
1636058989bSNarendra_K@Dell.com 
16494116f81SAndy Shevchenko 	obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 0x2,
1653910ebacSKrzysztof Wilczyński 				DSM_PCI_DEVICE_NAME, NULL);
1661d0fcef7SJiang Liu 	if (!obj)
1671d0fcef7SJiang Liu 		return -1;
1686058989bSNarendra_K@Dell.com 
1691d0fcef7SJiang Liu 	tmp = obj->package.elements;
1701d0fcef7SJiang Liu 	if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
1711d0fcef7SJiang Liu 	    tmp[0].type == ACPI_TYPE_INTEGER &&
172dcfa9be8SSimone Gotti 	    (tmp[1].type == ACPI_TYPE_STRING ||
173dcfa9be8SSimone Gotti 	     tmp[1].type == ACPI_TYPE_BUFFER)) {
1741d0fcef7SJiang Liu 		/*
1752fc59fe2SJiang Liu 		 * The second string element is optional even when
1761d0fcef7SJiang Liu 		 * this _DSM is implemented; when not implemented,
1771d0fcef7SJiang Liu 		 * this entry must return a null string.
1781d0fcef7SJiang Liu 		 */
179dcfa9be8SSimone Gotti 		if (attr == ACPI_ATTR_INDEX_SHOW) {
1802fc59fe2SJiang Liu 			scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value);
181dcfa9be8SSimone Gotti 		} else if (attr == ACPI_ATTR_LABEL_SHOW) {
182dcfa9be8SSimone Gotti 			if (tmp[1].type == ACPI_TYPE_STRING)
183dcfa9be8SSimone Gotti 				scnprintf(buf, PAGE_SIZE, "%s\n",
184dcfa9be8SSimone Gotti 					  tmp[1].string.pointer);
185dcfa9be8SSimone Gotti 			else if (tmp[1].type == ACPI_TYPE_BUFFER)
1861d0fcef7SJiang Liu 				dsm_label_utf16s_to_utf8s(tmp + 1, buf);
187dcfa9be8SSimone Gotti 		}
1881d0fcef7SJiang Liu 		len = strlen(buf) > 0 ? strlen(buf) : -1;
1896058989bSNarendra_K@Dell.com 	}
1901d0fcef7SJiang Liu 
1911d0fcef7SJiang Liu 	ACPI_FREE(obj);
1921d0fcef7SJiang Liu 
1936058989bSNarendra_K@Dell.com 	return len;
1946058989bSNarendra_K@Dell.com }
1956058989bSNarendra_K@Dell.com 
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 
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 
216*df1af7cbSKrzysztof Wilczyński static umode_t acpi_attr_is_visible(struct kobject *kobj, struct attribute *a,
217*df1af7cbSKrzysztof Wilczyński 				    int n)
218*df1af7cbSKrzysztof Wilczyński {
219*df1af7cbSKrzysztof Wilczyński 	struct device *dev = kobj_to_dev(kobj);
220*df1af7cbSKrzysztof Wilczyński 
221*df1af7cbSKrzysztof Wilczyński 	if (!device_has_acpi_name(dev))
222*df1af7cbSKrzysztof Wilczyński 		return 0;
223*df1af7cbSKrzysztof Wilczyński 
224*df1af7cbSKrzysztof Wilczyński 	return a->mode;
225*df1af7cbSKrzysztof Wilczyński }
226*df1af7cbSKrzysztof 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