xref: /openbmc/linux/drivers/pci/pci-label.c (revision 554a6037)
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>
32911e1c9bSNarendra K #include "pci.h"
33911e1c9bSNarendra K 
344c859804SBjorn Helgaas #ifdef CONFIG_DMI
35911e1c9bSNarendra K enum smbios_attr_enum {
36911e1c9bSNarendra K 	SMBIOS_ATTR_NONE = 0,
37911e1c9bSNarendra K 	SMBIOS_ATTR_LABEL_SHOW,
38911e1c9bSNarendra K 	SMBIOS_ATTR_INSTANCE_SHOW,
39911e1c9bSNarendra K };
40911e1c9bSNarendra K 
413c78bc61SRyan Desfosses static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
42911e1c9bSNarendra K 					  enum smbios_attr_enum attribute)
43911e1c9bSNarendra K {
44911e1c9bSNarendra K 	const struct dmi_device *dmi;
45911e1c9bSNarendra K 	struct dmi_dev_onboard *donboard;
46911e1c9bSNarendra K 	int bus;
47911e1c9bSNarendra K 	int devfn;
48911e1c9bSNarendra K 
49911e1c9bSNarendra K 	bus = pdev->bus->number;
50911e1c9bSNarendra K 	devfn = pdev->devfn;
51911e1c9bSNarendra K 
52911e1c9bSNarendra K 	dmi = NULL;
53911e1c9bSNarendra K 	while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
54911e1c9bSNarendra K 				      NULL, dmi)) != NULL) {
55911e1c9bSNarendra K 		donboard = dmi->device_data;
56911e1c9bSNarendra K 		if (donboard && donboard->bus == bus &&
57911e1c9bSNarendra K 					donboard->devfn == devfn) {
58911e1c9bSNarendra K 			if (buf) {
59911e1c9bSNarendra K 				if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
60911e1c9bSNarendra K 					return scnprintf(buf, PAGE_SIZE,
61911e1c9bSNarendra K 							 "%d\n",
62911e1c9bSNarendra K 							 donboard->instance);
63911e1c9bSNarendra K 				else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
64911e1c9bSNarendra K 					return scnprintf(buf, PAGE_SIZE,
65911e1c9bSNarendra K 							 "%s\n",
66911e1c9bSNarendra K 							 dmi->name);
67911e1c9bSNarendra K 			}
68911e1c9bSNarendra K 			return strlen(dmi->name);
69911e1c9bSNarendra K 		}
70911e1c9bSNarendra K 	}
71911e1c9bSNarendra K 	return 0;
72911e1c9bSNarendra K }
73911e1c9bSNarendra K 
743c78bc61SRyan Desfosses static umode_t smbios_instance_string_exist(struct kobject *kobj,
753c78bc61SRyan Desfosses 					    struct attribute *attr, int n)
76911e1c9bSNarendra K {
77911e1c9bSNarendra K 	struct device *dev;
78911e1c9bSNarendra K 	struct pci_dev *pdev;
79911e1c9bSNarendra K 
80554a6037SGeliang Tang 	dev = kobj_to_dev(kobj);
81911e1c9bSNarendra K 	pdev = to_pci_dev(dev);
82911e1c9bSNarendra K 
83911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
84911e1c9bSNarendra K 					   S_IRUGO : 0;
85911e1c9bSNarendra K }
86911e1c9bSNarendra K 
873c78bc61SRyan Desfosses static ssize_t smbioslabel_show(struct device *dev,
883c78bc61SRyan Desfosses 				struct device_attribute *attr, char *buf)
89911e1c9bSNarendra K {
90911e1c9bSNarendra K 	struct pci_dev *pdev;
91911e1c9bSNarendra K 	pdev = to_pci_dev(dev);
92911e1c9bSNarendra K 
93911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, buf,
94911e1c9bSNarendra K 					   SMBIOS_ATTR_LABEL_SHOW);
95911e1c9bSNarendra K }
96911e1c9bSNarendra K 
973c78bc61SRyan Desfosses static ssize_t smbiosinstance_show(struct device *dev,
98911e1c9bSNarendra K 				   struct device_attribute *attr, char *buf)
99911e1c9bSNarendra K {
100911e1c9bSNarendra K 	struct pci_dev *pdev;
101911e1c9bSNarendra K 	pdev = to_pci_dev(dev);
102911e1c9bSNarendra K 
103911e1c9bSNarendra K 	return find_smbios_instance_string(pdev, buf,
104911e1c9bSNarendra K 					   SMBIOS_ATTR_INSTANCE_SHOW);
105911e1c9bSNarendra K }
106911e1c9bSNarendra K 
107911e1c9bSNarendra K static struct device_attribute smbios_attr_label = {
108763e9db9SStephen Rothwell 	.attr = {.name = "label", .mode = 0444},
109911e1c9bSNarendra K 	.show = smbioslabel_show,
110911e1c9bSNarendra K };
111911e1c9bSNarendra K 
112911e1c9bSNarendra K static struct device_attribute smbios_attr_instance = {
113763e9db9SStephen Rothwell 	.attr = {.name = "index", .mode = 0444},
114911e1c9bSNarendra K 	.show = smbiosinstance_show,
115911e1c9bSNarendra K };
116911e1c9bSNarendra K 
117911e1c9bSNarendra K static struct attribute *smbios_attributes[] = {
118911e1c9bSNarendra K 	&smbios_attr_label.attr,
119911e1c9bSNarendra K 	&smbios_attr_instance.attr,
120911e1c9bSNarendra K 	NULL,
121911e1c9bSNarendra K };
122911e1c9bSNarendra K 
123911e1c9bSNarendra K static struct attribute_group smbios_attr_group = {
124911e1c9bSNarendra K 	.attrs = smbios_attributes,
125911e1c9bSNarendra K 	.is_visible = smbios_instance_string_exist,
126911e1c9bSNarendra K };
127911e1c9bSNarendra K 
1283c78bc61SRyan Desfosses static int pci_create_smbiosname_file(struct pci_dev *pdev)
129911e1c9bSNarendra K {
1306058989bSNarendra_K@Dell.com 	return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
131911e1c9bSNarendra K }
132911e1c9bSNarendra K 
1333c78bc61SRyan Desfosses static void pci_remove_smbiosname_file(struct pci_dev *pdev)
134911e1c9bSNarendra K {
135911e1c9bSNarendra K 	sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
136911e1c9bSNarendra K }
1374c859804SBjorn Helgaas #else
1383c78bc61SRyan Desfosses static inline int pci_create_smbiosname_file(struct pci_dev *pdev)
1394c859804SBjorn Helgaas {
1404c859804SBjorn Helgaas 	return -1;
1414c859804SBjorn Helgaas }
142911e1c9bSNarendra K 
1433c78bc61SRyan Desfosses static inline void pci_remove_smbiosname_file(struct pci_dev *pdev)
1444c859804SBjorn Helgaas {
1454c859804SBjorn Helgaas }
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 
17518e94a33SAaron Lu 	obj = acpi_evaluate_dsm(handle, pci_acpi_dsm_uuid, 0x2,
1761d0fcef7SJiang Liu 				DEVICE_LABEL_DSM, 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 
2073c78bc61SRyan Desfosses static bool device_has_dsm(struct device *dev)
2086058989bSNarendra_K@Dell.com {
2096058989bSNarendra_K@Dell.com 	acpi_handle handle;
2106058989bSNarendra_K@Dell.com 
2113a83f992SRafael J. Wysocki 	handle = ACPI_HANDLE(dev);
2126058989bSNarendra_K@Dell.com 	if (!handle)
2132fc59fe2SJiang Liu 		return false;
2146058989bSNarendra_K@Dell.com 
21518e94a33SAaron Lu 	return !!acpi_check_dsm(handle, pci_acpi_dsm_uuid, 0x2,
2162fc59fe2SJiang Liu 				1 << DEVICE_LABEL_DSM);
2176058989bSNarendra_K@Dell.com }
2186058989bSNarendra_K@Dell.com 
2193c78bc61SRyan Desfosses static umode_t acpi_index_string_exist(struct kobject *kobj,
2203c78bc61SRyan Desfosses 				       struct attribute *attr, int n)
2216058989bSNarendra_K@Dell.com {
2226058989bSNarendra_K@Dell.com 	struct device *dev;
2236058989bSNarendra_K@Dell.com 
224554a6037SGeliang Tang 	dev = kobj_to_dev(kobj);
2256058989bSNarendra_K@Dell.com 
2266058989bSNarendra_K@Dell.com 	if (device_has_dsm(dev))
2276058989bSNarendra_K@Dell.com 		return S_IRUGO;
2286058989bSNarendra_K@Dell.com 
2296058989bSNarendra_K@Dell.com 	return 0;
2306058989bSNarendra_K@Dell.com }
2316058989bSNarendra_K@Dell.com 
2323c78bc61SRyan Desfosses static ssize_t acpilabel_show(struct device *dev,
2333c78bc61SRyan Desfosses 			      struct device_attribute *attr, char *buf)
2346058989bSNarendra_K@Dell.com {
2351d0fcef7SJiang Liu 	return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
2366058989bSNarendra_K@Dell.com }
2376058989bSNarendra_K@Dell.com 
2383c78bc61SRyan Desfosses static ssize_t acpiindex_show(struct device *dev,
2393c78bc61SRyan Desfosses 			      struct device_attribute *attr, char *buf)
2406058989bSNarendra_K@Dell.com {
2411d0fcef7SJiang Liu 	return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
2426058989bSNarendra_K@Dell.com }
2436058989bSNarendra_K@Dell.com 
2446058989bSNarendra_K@Dell.com static struct device_attribute acpi_attr_label = {
2456058989bSNarendra_K@Dell.com 	.attr = {.name = "label", .mode = 0444},
2466058989bSNarendra_K@Dell.com 	.show = acpilabel_show,
2476058989bSNarendra_K@Dell.com };
2486058989bSNarendra_K@Dell.com 
2496058989bSNarendra_K@Dell.com static struct device_attribute acpi_attr_index = {
2506058989bSNarendra_K@Dell.com 	.attr = {.name = "acpi_index", .mode = 0444},
2516058989bSNarendra_K@Dell.com 	.show = acpiindex_show,
2526058989bSNarendra_K@Dell.com };
2536058989bSNarendra_K@Dell.com 
2546058989bSNarendra_K@Dell.com static struct attribute *acpi_attributes[] = {
2556058989bSNarendra_K@Dell.com 	&acpi_attr_label.attr,
2566058989bSNarendra_K@Dell.com 	&acpi_attr_index.attr,
2576058989bSNarendra_K@Dell.com 	NULL,
2586058989bSNarendra_K@Dell.com };
2596058989bSNarendra_K@Dell.com 
2606058989bSNarendra_K@Dell.com static struct attribute_group acpi_attr_group = {
2616058989bSNarendra_K@Dell.com 	.attrs = acpi_attributes,
2626058989bSNarendra_K@Dell.com 	.is_visible = acpi_index_string_exist,
2636058989bSNarendra_K@Dell.com };
2646058989bSNarendra_K@Dell.com 
2653c78bc61SRyan Desfosses static int pci_create_acpi_index_label_files(struct pci_dev *pdev)
2666058989bSNarendra_K@Dell.com {
2676058989bSNarendra_K@Dell.com 	return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
2686058989bSNarendra_K@Dell.com }
2696058989bSNarendra_K@Dell.com 
2703c78bc61SRyan Desfosses static int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
2716058989bSNarendra_K@Dell.com {
2726058989bSNarendra_K@Dell.com 	sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
2736058989bSNarendra_K@Dell.com 	return 0;
2746058989bSNarendra_K@Dell.com }
2754c859804SBjorn Helgaas #else
2763c78bc61SRyan Desfosses static inline int pci_create_acpi_index_label_files(struct pci_dev *pdev)
2774c859804SBjorn Helgaas {
2784c859804SBjorn Helgaas 	return -1;
2794c859804SBjorn Helgaas }
2804c859804SBjorn Helgaas 
2813c78bc61SRyan Desfosses static inline int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
2824c859804SBjorn Helgaas {
2834c859804SBjorn Helgaas 	return -1;
2844c859804SBjorn Helgaas }
2854c859804SBjorn Helgaas 
2863c78bc61SRyan Desfosses static inline bool device_has_dsm(struct device *dev)
2874c859804SBjorn Helgaas {
2884c859804SBjorn Helgaas 	return false;
2894c859804SBjorn Helgaas }
2906058989bSNarendra_K@Dell.com #endif
2916058989bSNarendra_K@Dell.com 
292911e1c9bSNarendra K void pci_create_firmware_label_files(struct pci_dev *pdev)
293911e1c9bSNarendra K {
2946058989bSNarendra_K@Dell.com 	if (device_has_dsm(&pdev->dev))
2956058989bSNarendra_K@Dell.com 		pci_create_acpi_index_label_files(pdev);
2966058989bSNarendra_K@Dell.com 	else
2976058989bSNarendra_K@Dell.com 		pci_create_smbiosname_file(pdev);
298911e1c9bSNarendra K }
299911e1c9bSNarendra K 
300911e1c9bSNarendra K void pci_remove_firmware_label_files(struct pci_dev *pdev)
301911e1c9bSNarendra K {
3026058989bSNarendra_K@Dell.com 	if (device_has_dsm(&pdev->dev))
3036058989bSNarendra_K@Dell.com 		pci_remove_acpi_index_label_files(pdev);
3046058989bSNarendra_K@Dell.com 	else
305911e1c9bSNarendra K 		pci_remove_smbiosname_file(pdev);
306911e1c9bSNarendra K }
307