blob: f12dcd1dc2080f5705bb79db7f35518378453b2b [file] [log] [blame]
Narendra K911e1c92010-07-26 05:56:50 -05001/*
2 * Purpose: Export the firmware instance and label associated with
3 * a pci device to sysfs
4 * Copyright (C) 2010 Dell Inc.
5 * by Narendra K <Narendra_K@dell.com>,
6 * Jordan Hargrave <Jordan_Hargrave@dell.com>
7 *
Narendra_K@Dell.com60589892011-03-02 22:34:17 +05308 * PCI Firmware Specification Revision 3.1 section 4.6.7 (DSM for Naming a
9 * PCI or PCI Express Device Under Operating Systems) defines an instance
10 * number and string name. This code retrieves them and exports them to sysfs.
11 * If the system firmware does not provide the ACPI _DSM (Device Specific
12 * Method), then the SMBIOS type 41 instance number and string is exported to
13 * sysfs.
14 *
Narendra K911e1c92010-07-26 05:56:50 -050015 * SMBIOS defines type 41 for onboard pci devices. This code retrieves
16 * the instance number and string from the type 41 record and exports
17 * it to sysfs.
18 *
19 * Please see http://linux.dell.com/wiki/index.php/Oss/libnetdevname for more
20 * information.
21 */
22
23#include <linux/dmi.h>
24#include <linux/sysfs.h>
25#include <linux/pci.h>
26#include <linux/pci_ids.h>
27#include <linux/module.h>
28#include <linux/device.h>
Narendra_K@Dell.com60589892011-03-02 22:34:17 +053029#include <linux/nls.h>
30#include <linux/acpi.h>
31#include <linux/pci-acpi.h>
Narendra_K@Dell.com60589892011-03-02 22:34:17 +053032#include <acpi/acpi_bus.h>
Narendra K911e1c92010-07-26 05:56:50 -050033#include "pci.h"
34
Narendra_K@Dell.com60589892011-03-02 22:34:17 +053035#define DEVICE_LABEL_DSM 0x07
36
37#ifndef CONFIG_DMI
38
39static inline int
40pci_create_smbiosname_file(struct pci_dev *pdev)
41{
42 return -1;
43}
44
45static inline void
46pci_remove_smbiosname_file(struct pci_dev *pdev)
47{
48}
49
50#else
51
Narendra K911e1c92010-07-26 05:56:50 -050052enum smbios_attr_enum {
53 SMBIOS_ATTR_NONE = 0,
54 SMBIOS_ATTR_LABEL_SHOW,
55 SMBIOS_ATTR_INSTANCE_SHOW,
56};
57
Al Viroed476412011-07-24 23:31:06 -040058static size_t
Narendra K911e1c92010-07-26 05:56:50 -050059find_smbios_instance_string(struct pci_dev *pdev, char *buf,
60 enum smbios_attr_enum attribute)
61{
62 const struct dmi_device *dmi;
63 struct dmi_dev_onboard *donboard;
64 int bus;
65 int devfn;
66
67 bus = pdev->bus->number;
68 devfn = pdev->devfn;
69
70 dmi = NULL;
71 while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
72 NULL, dmi)) != NULL) {
73 donboard = dmi->device_data;
74 if (donboard && donboard->bus == bus &&
75 donboard->devfn == devfn) {
76 if (buf) {
77 if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
78 return scnprintf(buf, PAGE_SIZE,
79 "%d\n",
80 donboard->instance);
81 else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
82 return scnprintf(buf, PAGE_SIZE,
83 "%s\n",
84 dmi->name);
85 }
86 return strlen(dmi->name);
87 }
88 }
89 return 0;
90}
91
Al Viro587a1f12011-07-23 23:11:19 -040092static umode_t
Narendra K911e1c92010-07-26 05:56:50 -050093smbios_instance_string_exist(struct kobject *kobj, struct attribute *attr,
94 int n)
95{
96 struct device *dev;
97 struct pci_dev *pdev;
98
99 dev = container_of(kobj, struct device, kobj);
100 pdev = to_pci_dev(dev);
101
102 return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
103 S_IRUGO : 0;
104}
105
106static ssize_t
107smbioslabel_show(struct device *dev, struct device_attribute *attr, char *buf)
108{
109 struct pci_dev *pdev;
110 pdev = to_pci_dev(dev);
111
112 return find_smbios_instance_string(pdev, buf,
113 SMBIOS_ATTR_LABEL_SHOW);
114}
115
116static ssize_t
117smbiosinstance_show(struct device *dev,
118 struct device_attribute *attr, char *buf)
119{
120 struct pci_dev *pdev;
121 pdev = to_pci_dev(dev);
122
123 return find_smbios_instance_string(pdev, buf,
124 SMBIOS_ATTR_INSTANCE_SHOW);
125}
126
127static struct device_attribute smbios_attr_label = {
Stephen Rothwell763e9db2010-08-04 14:25:31 +1000128 .attr = {.name = "label", .mode = 0444},
Narendra K911e1c92010-07-26 05:56:50 -0500129 .show = smbioslabel_show,
130};
131
132static struct device_attribute smbios_attr_instance = {
Stephen Rothwell763e9db2010-08-04 14:25:31 +1000133 .attr = {.name = "index", .mode = 0444},
Narendra K911e1c92010-07-26 05:56:50 -0500134 .show = smbiosinstance_show,
135};
136
137static struct attribute *smbios_attributes[] = {
138 &smbios_attr_label.attr,
139 &smbios_attr_instance.attr,
140 NULL,
141};
142
143static struct attribute_group smbios_attr_group = {
144 .attrs = smbios_attributes,
145 .is_visible = smbios_instance_string_exist,
146};
147
148static int
149pci_create_smbiosname_file(struct pci_dev *pdev)
150{
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
154static void
155pci_remove_smbiosname_file(struct pci_dev *pdev)
156{
157 sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
158}
159
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530160#endif
161
162#ifndef CONFIG_ACPI
163
164static inline int
165pci_create_acpi_index_label_files(struct pci_dev *pdev)
166{
167 return -1;
168}
169
170static inline int
171pci_remove_acpi_index_label_files(struct pci_dev *pdev)
172{
173 return -1;
174}
175
Narendra_K@Dell.com07eefe12011-03-07 12:55:56 -0800176static inline bool
177device_has_dsm(struct device *dev)
178{
179 return false;
180}
181
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530182#else
183
184static const char device_label_dsm_uuid[] = {
185 0xD0, 0x37, 0xC9, 0xE5, 0x53, 0x35, 0x7A, 0x4D,
186 0x91, 0x17, 0xEA, 0x4D, 0x19, 0xC3, 0x43, 0x4D
187};
188
189enum acpi_attr_enum {
190 ACPI_ATTR_NONE = 0,
191 ACPI_ATTR_LABEL_SHOW,
192 ACPI_ATTR_INDEX_SHOW,
193};
194
195static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
196{
197 int len;
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800198 len = utf16s_to_utf8s((const wchar_t *)obj->string.pointer,
199 obj->string.length,
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530200 UTF16_LITTLE_ENDIAN,
201 buf, PAGE_SIZE);
202 buf[len] = '\n';
203}
204
205static int
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800206dsm_get_label(struct device *dev, char *buf, enum acpi_attr_enum attr)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530207{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800208 acpi_handle handle;
209 union acpi_object *obj, *tmp;
210 int len = -1;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530211
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800212 handle = ACPI_HANDLE(dev);
213 if (!handle)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530214 return -1;
215
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800216 obj = acpi_evaluate_dsm(handle, device_label_dsm_uuid, 0x2,
217 DEVICE_LABEL_DSM, NULL);
218 if (!obj)
219 return -1;
220
221 tmp = obj->package.elements;
222 if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
223 tmp[0].type == ACPI_TYPE_INTEGER &&
224 tmp[1].type == ACPI_TYPE_STRING) {
225 len = tmp[0].integer.value;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530226 if (buf) {
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800227 /*
228 * This second string element is optional even when
229 * this _DSM is implemented; when not implemented,
230 * this entry must return a null string.
231 */
232 if (attr == ACPI_ATTR_INDEX_SHOW)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530233 scnprintf(buf, PAGE_SIZE, "%llu\n",
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800234 tmp->integer.value);
235 else if (attr == ACPI_ATTR_LABEL_SHOW)
236 dsm_label_utf16s_to_utf8s(tmp + 1, buf);
237 len = strlen(buf) > 0 ? strlen(buf) : -1;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530238 }
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530239 }
Jiang Liu54e5bb42013-12-19 20:38:12 +0800240
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800241 ACPI_FREE(obj);
Jiang Liu54e5bb42013-12-19 20:38:12 +0800242
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800243 return len;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530244}
245
246static bool
247device_has_dsm(struct device *dev)
248{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800249 return dsm_get_label(dev, NULL, ACPI_ATTR_NONE) > 0;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530250}
251
Al Viro587a1f12011-07-23 23:11:19 -0400252static umode_t
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530253acpi_index_string_exist(struct kobject *kobj, struct attribute *attr, int n)
254{
255 struct device *dev;
256
257 dev = container_of(kobj, struct device, kobj);
258
259 if (device_has_dsm(dev))
260 return S_IRUGO;
261
262 return 0;
263}
264
265static ssize_t
266acpilabel_show(struct device *dev, struct device_attribute *attr, char *buf)
267{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800268 return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530269}
270
271static ssize_t
272acpiindex_show(struct device *dev, struct device_attribute *attr, char *buf)
273{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800274 return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530275}
276
277static struct device_attribute acpi_attr_label = {
278 .attr = {.name = "label", .mode = 0444},
279 .show = acpilabel_show,
280};
281
282static struct device_attribute acpi_attr_index = {
283 .attr = {.name = "acpi_index", .mode = 0444},
284 .show = acpiindex_show,
285};
286
287static struct attribute *acpi_attributes[] = {
288 &acpi_attr_label.attr,
289 &acpi_attr_index.attr,
290 NULL,
291};
292
293static struct attribute_group acpi_attr_group = {
294 .attrs = acpi_attributes,
295 .is_visible = acpi_index_string_exist,
296};
297
298static int
299pci_create_acpi_index_label_files(struct pci_dev *pdev)
300{
301 return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
302}
303
304static int
305pci_remove_acpi_index_label_files(struct pci_dev *pdev)
306{
307 sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
308 return 0;
309}
310#endif
311
Narendra K911e1c92010-07-26 05:56:50 -0500312void pci_create_firmware_label_files(struct pci_dev *pdev)
313{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530314 if (device_has_dsm(&pdev->dev))
315 pci_create_acpi_index_label_files(pdev);
316 else
317 pci_create_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500318}
319
320void pci_remove_firmware_label_files(struct pci_dev *pdev)
321{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530322 if (device_has_dsm(&pdev->dev))
323 pci_remove_acpi_index_label_files(pdev);
324 else
325 pci_remove_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500326}