blob: 0260b14c8d945acd431291ed868930a3ae0297cd [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 {
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530190 ACPI_ATTR_LABEL_SHOW,
191 ACPI_ATTR_INDEX_SHOW,
192};
193
194static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
195{
196 int len;
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800197 len = utf16s_to_utf8s((const wchar_t *)obj->string.pointer,
198 obj->string.length,
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530199 UTF16_LITTLE_ENDIAN,
200 buf, PAGE_SIZE);
201 buf[len] = '\n';
202}
203
204static int
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800205dsm_get_label(struct device *dev, char *buf, enum acpi_attr_enum attr)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530206{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800207 acpi_handle handle;
208 union acpi_object *obj, *tmp;
209 int len = -1;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530210
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800211 handle = ACPI_HANDLE(dev);
212 if (!handle)
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530213 return -1;
214
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800215 obj = acpi_evaluate_dsm(handle, device_label_dsm_uuid, 0x2,
216 DEVICE_LABEL_DSM, NULL);
217 if (!obj)
218 return -1;
219
220 tmp = obj->package.elements;
221 if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
222 tmp[0].type == ACPI_TYPE_INTEGER &&
223 tmp[1].type == ACPI_TYPE_STRING) {
Jiang Liu2fc59fe2013-12-19 20:38:14 +0800224 /*
225 * The second string element is optional even when
226 * this _DSM is implemented; when not implemented,
227 * this entry must return a null string.
228 */
229 if (attr == ACPI_ATTR_INDEX_SHOW)
230 scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value);
231 else if (attr == ACPI_ATTR_LABEL_SHOW)
232 dsm_label_utf16s_to_utf8s(tmp + 1, buf);
233 len = strlen(buf) > 0 ? strlen(buf) : -1;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530234 }
Jiang Liu54e5bb42013-12-19 20:38:12 +0800235
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800236 ACPI_FREE(obj);
Jiang Liu54e5bb42013-12-19 20:38:12 +0800237
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800238 return len;
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530239}
240
241static bool
242device_has_dsm(struct device *dev)
243{
Jiang Liu2fc59fe2013-12-19 20:38:14 +0800244 acpi_handle handle;
245
246 handle = ACPI_HANDLE(dev);
247 if (!handle)
248 return false;
249
250 return !!acpi_check_dsm(handle, device_label_dsm_uuid, 0x2,
251 1 << DEVICE_LABEL_DSM);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530252}
253
Al Viro587a1f12011-07-23 23:11:19 -0400254static umode_t
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530255acpi_index_string_exist(struct kobject *kobj, struct attribute *attr, int n)
256{
257 struct device *dev;
258
259 dev = container_of(kobj, struct device, kobj);
260
261 if (device_has_dsm(dev))
262 return S_IRUGO;
263
264 return 0;
265}
266
267static ssize_t
268acpilabel_show(struct device *dev, struct device_attribute *attr, char *buf)
269{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800270 return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530271}
272
273static ssize_t
274acpiindex_show(struct device *dev, struct device_attribute *attr, char *buf)
275{
Jiang Liu1d0fcef2013-12-19 20:38:13 +0800276 return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530277}
278
279static struct device_attribute acpi_attr_label = {
280 .attr = {.name = "label", .mode = 0444},
281 .show = acpilabel_show,
282};
283
284static struct device_attribute acpi_attr_index = {
285 .attr = {.name = "acpi_index", .mode = 0444},
286 .show = acpiindex_show,
287};
288
289static struct attribute *acpi_attributes[] = {
290 &acpi_attr_label.attr,
291 &acpi_attr_index.attr,
292 NULL,
293};
294
295static struct attribute_group acpi_attr_group = {
296 .attrs = acpi_attributes,
297 .is_visible = acpi_index_string_exist,
298};
299
300static int
301pci_create_acpi_index_label_files(struct pci_dev *pdev)
302{
303 return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
304}
305
306static int
307pci_remove_acpi_index_label_files(struct pci_dev *pdev)
308{
309 sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
310 return 0;
311}
312#endif
313
Narendra K911e1c92010-07-26 05:56:50 -0500314void pci_create_firmware_label_files(struct pci_dev *pdev)
315{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530316 if (device_has_dsm(&pdev->dev))
317 pci_create_acpi_index_label_files(pdev);
318 else
319 pci_create_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500320}
321
322void pci_remove_firmware_label_files(struct pci_dev *pdev)
323{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530324 if (device_has_dsm(&pdev->dev))
325 pci_remove_acpi_index_label_files(pdev);
326 else
327 pci_remove_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500328}