blob: 4fef6fc0740fdb6b670cc28dfb2da44acbe2886f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Narendra K911e1c92010-07-26 05:56:50 -05002/*
Bjorn Helgaasdf62ab52018-03-09 16:36:33 -06003 * Export the firmware instance and label associated with a PCI device to
4 * sysfs
5 *
Narendra K911e1c92010-07-26 05:56:50 -05006 * Copyright (C) 2010 Dell Inc.
7 * by Narendra K <Narendra_K@dell.com>,
8 * Jordan Hargrave <Jordan_Hargrave@dell.com>
9 *
Narendra_K@Dell.com60589892011-03-02 22:34:17 +053010 * PCI Firmware Specification Revision 3.1 section 4.6.7 (DSM for Naming a
11 * PCI or PCI Express Device Under Operating Systems) defines an instance
12 * number and string name. This code retrieves them and exports them to sysfs.
13 * If the system firmware does not provide the ACPI _DSM (Device Specific
14 * Method), then the SMBIOS type 41 instance number and string is exported to
15 * sysfs.
16 *
Narendra K911e1c92010-07-26 05:56:50 -050017 * SMBIOS defines type 41 for onboard pci devices. This code retrieves
18 * the instance number and string from the type 41 record and exports
19 * it to sysfs.
20 *
Alexander A. Klimov7ecd4a82020-06-27 12:30:50 +020021 * Please see https://linux.dell.com/files/biosdevname/ for more
Narendra K911e1c92010-07-26 05:56:50 -050022 * information.
23 */
24
25#include <linux/dmi.h>
26#include <linux/sysfs.h>
27#include <linux/pci.h>
28#include <linux/pci_ids.h>
29#include <linux/module.h>
30#include <linux/device.h>
Narendra_K@Dell.com60589892011-03-02 22:34:17 +053031#include <linux/nls.h>
32#include <linux/acpi.h>
33#include <linux/pci-acpi.h>
Narendra K911e1c92010-07-26 05:56:50 -050034#include "pci.h"
35
Krzysztof Wilczyński10172752021-04-27 10:39:16 -050036static bool device_has_acpi_name(struct device *dev)
37{
38#ifdef CONFIG_ACPI
39 acpi_handle handle;
40
41 handle = ACPI_HANDLE(dev);
42 if (!handle)
43 return false;
44
45 return acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2,
46 1 << DSM_PCI_DEVICE_NAME);
47#else
48 return false;
49#endif
50}
51
Bjorn Helgaas4c859802014-01-13 17:01:11 -070052#ifdef CONFIG_DMI
Narendra K911e1c92010-07-26 05:56:50 -050053enum smbios_attr_enum {
54 SMBIOS_ATTR_NONE = 0,
55 SMBIOS_ATTR_LABEL_SHOW,
56 SMBIOS_ATTR_INSTANCE_SHOW,
57};
58
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040059static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
60 enum smbios_attr_enum attribute)
Narendra K911e1c92010-07-26 05:56:50 -050061{
62 const struct dmi_device *dmi;
63 struct dmi_dev_onboard *donboard;
Sujith Pandel6c51c822017-05-02 06:55:40 -040064 int domain_nr;
Narendra K911e1c92010-07-26 05:56:50 -050065 int bus;
66 int devfn;
67
Sujith Pandel6c51c822017-05-02 06:55:40 -040068 domain_nr = pci_domain_nr(pdev->bus);
Narendra K911e1c92010-07-26 05:56:50 -050069 bus = pdev->bus->number;
70 devfn = pdev->devfn;
71
72 dmi = NULL;
73 while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
74 NULL, dmi)) != NULL) {
75 donboard = dmi->device_data;
Sujith Pandel6c51c822017-05-02 06:55:40 -040076 if (donboard && donboard->segment == domain_nr &&
77 donboard->bus == bus &&
78 donboard->devfn == devfn) {
Narendra K911e1c92010-07-26 05:56:50 -050079 if (buf) {
80 if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
81 return scnprintf(buf, PAGE_SIZE,
82 "%d\n",
83 donboard->instance);
84 else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
85 return scnprintf(buf, PAGE_SIZE,
86 "%s\n",
87 dmi->name);
88 }
89 return strlen(dmi->name);
90 }
91 }
92 return 0;
93}
94
Ryan Desfosses3c78bc62014-04-18 20:13:49 -040095static umode_t smbios_instance_string_exist(struct kobject *kobj,
96 struct attribute *attr, int n)
Narendra K911e1c92010-07-26 05:56:50 -050097{
98 struct device *dev;
99 struct pci_dev *pdev;
100
Geliang Tang554a6032015-12-23 20:28:13 +0800101 dev = kobj_to_dev(kobj);
Narendra K911e1c92010-07-26 05:56:50 -0500102 pdev = to_pci_dev(dev);
103
104 return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
105 S_IRUGO : 0;
106}
107
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400108static ssize_t smbioslabel_show(struct device *dev,
109 struct device_attribute *attr, char *buf)
Narendra K911e1c92010-07-26 05:56:50 -0500110{
111 struct pci_dev *pdev;
112 pdev = to_pci_dev(dev);
113
114 return find_smbios_instance_string(pdev, buf,
115 SMBIOS_ATTR_LABEL_SHOW);
116}
117
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400118static ssize_t smbiosinstance_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
Narendra K911e1c92010-07-26 05:56:50 -0500120{
121 struct pci_dev *pdev;
122 pdev = to_pci_dev(dev);
123
124 return find_smbios_instance_string(pdev, buf,
125 SMBIOS_ATTR_INSTANCE_SHOW);
126}
127
128static struct device_attribute smbios_attr_label = {
Stephen Rothwell763e9db2010-08-04 14:25:31 +1000129 .attr = {.name = "label", .mode = 0444},
Narendra K911e1c92010-07-26 05:56:50 -0500130 .show = smbioslabel_show,
131};
132
133static struct device_attribute smbios_attr_instance = {
Stephen Rothwell763e9db2010-08-04 14:25:31 +1000134 .attr = {.name = "index", .mode = 0444},
Narendra K911e1c92010-07-26 05:56:50 -0500135 .show = smbiosinstance_show,
136};
137
138static struct attribute *smbios_attributes[] = {
139 &smbios_attr_label.attr,
140 &smbios_attr_instance.attr,
141 NULL,
142};
143
Arvind Yadavf4841282017-07-11 14:57:08 +0530144static const struct attribute_group smbios_attr_group = {
Narendra K911e1c92010-07-26 05:56:50 -0500145 .attrs = smbios_attributes,
146 .is_visible = smbios_instance_string_exist,
147};
148
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400149static int pci_create_smbiosname_file(struct pci_dev *pdev)
Narendra K911e1c92010-07-26 05:56:50 -0500150{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530151 return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
Narendra K911e1c92010-07-26 05:56:50 -0500152}
153
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400154static void pci_remove_smbiosname_file(struct pci_dev *pdev)
Narendra K911e1c92010-07-26 05:56:50 -0500155{
156 sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
157}
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700158#else
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400159static inline int pci_create_smbiosname_file(struct pci_dev *pdev)
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700160{
161 return -1;
162}
Narendra K911e1c92010-07-26 05:56:50 -0500163
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400164static inline void pci_remove_smbiosname_file(struct pci_dev *pdev)
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700165{
166}
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530167#endif
168
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700169#ifdef CONFIG_ACPI
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530170enum acpi_attr_enum {
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530171 ACPI_ATTR_LABEL_SHOW,
172 ACPI_ATTR_INDEX_SHOW,
173};
174
175static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
176{
177 int len;
Simone Gottidcfa9be2014-06-18 16:55:30 +0200178 len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer,
179 obj->buffer.length,
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530180 UTF16_LITTLE_ENDIAN,
181 buf, PAGE_SIZE);
182 buf[len] = '\n';
183}
184
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400185static int dsm_get_label(struct device *dev, char *buf,
186 enum acpi_attr_enum attr)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530187{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800188 acpi_handle handle;
189 union acpi_object *obj, *tmp;
190 int len = -1;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530191
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800192 handle = ACPI_HANDLE(dev);
193 if (!handle)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530194 return -1;
195
Andy Shevchenko94116f82017-06-05 19:40:46 +0300196 obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 0x2,
Krzysztof Wilczyński3910eba2020-05-26 21:39:05 +0000197 DSM_PCI_DEVICE_NAME, NULL);
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800198 if (!obj)
199 return -1;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530200
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800201 tmp = obj->package.elements;
202 if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
203 tmp[0].type == ACPI_TYPE_INTEGER &&
Simone Gottidcfa9be2014-06-18 16:55:30 +0200204 (tmp[1].type == ACPI_TYPE_STRING ||
205 tmp[1].type == ACPI_TYPE_BUFFER)) {
Jiang Liu2fc59fe2013-12-19 20:38:14 +0800206 /*
207 * The second string element is optional even when
208 * this _DSM is implemented; when not implemented,
209 * this entry must return a null string.
210 */
Simone Gottidcfa9be2014-06-18 16:55:30 +0200211 if (attr == ACPI_ATTR_INDEX_SHOW) {
Jiang Liu2fc59fe2013-12-19 20:38:14 +0800212 scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value);
Simone Gottidcfa9be2014-06-18 16:55:30 +0200213 } else if (attr == ACPI_ATTR_LABEL_SHOW) {
214 if (tmp[1].type == ACPI_TYPE_STRING)
215 scnprintf(buf, PAGE_SIZE, "%s\n",
216 tmp[1].string.pointer);
217 else if (tmp[1].type == ACPI_TYPE_BUFFER)
218 dsm_label_utf16s_to_utf8s(tmp + 1, buf);
219 }
Jiang Liu2fc59fe2013-12-19 20:38:14 +0800220 len = strlen(buf) > 0 ? strlen(buf) : -1;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530221 }
Jiang Liu54e5bb42013-12-19 20:38:12 +0800222
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800223 ACPI_FREE(obj);
Jiang Liu54e5bb42013-12-19 20:38:12 +0800224
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800225 return len;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530226}
227
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500228static umode_t acpi_attr_is_visible(struct kobject *kobj, struct attribute *a,
229 int n)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530230{
231 struct device *dev;
232
Geliang Tang554a6032015-12-23 20:28:13 +0800233 dev = kobj_to_dev(kobj);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530234
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500235 if (!device_has_acpi_name(dev))
236 return 0;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530237
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500238 return a->mode;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530239}
240
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500241static ssize_t label_show(struct device *dev, struct device_attribute *attr,
242 char *buf)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530243{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800244 return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530245}
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500246static DEVICE_ATTR_RO(label);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530247
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500248static ssize_t acpi_index_show(struct device *dev,
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400249 struct device_attribute *attr, char *buf)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530250{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800251 return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530252}
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500253static DEVICE_ATTR_RO(acpi_index);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530254
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500255static struct attribute *acpi_attrs[] = {
256 &dev_attr_label.attr,
257 &dev_attr_acpi_index.attr,
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530258 NULL,
259};
260
Arvind Yadavf4841282017-07-11 14:57:08 +0530261static const struct attribute_group acpi_attr_group = {
Krzysztof Wilczyński2ed64942021-04-27 13:48:51 -0500262 .attrs = acpi_attrs,
263 .is_visible = acpi_attr_is_visible,
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530264};
265
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400266static int pci_create_acpi_index_label_files(struct pci_dev *pdev)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530267{
268 return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
269}
270
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400271static int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530272{
273 sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
274 return 0;
275}
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700276#else
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400277static inline int pci_create_acpi_index_label_files(struct pci_dev *pdev)
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700278{
279 return -1;
280}
281
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400282static inline int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700283{
284 return -1;
285}
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530286#endif
287
Narendra K911e1c92010-07-26 05:56:50 -0500288void pci_create_firmware_label_files(struct pci_dev *pdev)
289{
Krzysztof Wilczyński10172752021-04-27 10:39:16 -0500290 if (device_has_acpi_name(&pdev->dev))
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530291 pci_create_acpi_index_label_files(pdev);
292 else
293 pci_create_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500294}
295
296void pci_remove_firmware_label_files(struct pci_dev *pdev)
297{
Krzysztof Wilczyński10172752021-04-27 10:39:16 -0500298 if (device_has_acpi_name(&pdev->dev))
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530299 pci_remove_acpi_index_label_files(pdev);
300 else
301 pci_remove_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500302}