blob: 6f5d343d251ca444b524476ace8cbe0b1491534e [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
Bjorn Helgaas4c859802014-01-13 17:01:11 -070037#ifdef CONFIG_DMI
Narendra K911e1c92010-07-26 05:56:50 -050038enum smbios_attr_enum {
39 SMBIOS_ATTR_NONE = 0,
40 SMBIOS_ATTR_LABEL_SHOW,
41 SMBIOS_ATTR_INSTANCE_SHOW,
42};
43
Al Viroed476412011-07-24 23:31:06 -040044static size_t
Narendra K911e1c92010-07-26 05:56:50 -050045find_smbios_instance_string(struct pci_dev *pdev, char *buf,
46 enum smbios_attr_enum attribute)
47{
48 const struct dmi_device *dmi;
49 struct dmi_dev_onboard *donboard;
50 int bus;
51 int devfn;
52
53 bus = pdev->bus->number;
54 devfn = pdev->devfn;
55
56 dmi = NULL;
57 while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
58 NULL, dmi)) != NULL) {
59 donboard = dmi->device_data;
60 if (donboard && donboard->bus == bus &&
61 donboard->devfn == devfn) {
62 if (buf) {
63 if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
64 return scnprintf(buf, PAGE_SIZE,
65 "%d\n",
66 donboard->instance);
67 else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
68 return scnprintf(buf, PAGE_SIZE,
69 "%s\n",
70 dmi->name);
71 }
72 return strlen(dmi->name);
73 }
74 }
75 return 0;
76}
77
Al Viro587a1f12011-07-23 23:11:19 -040078static umode_t
Narendra K911e1c92010-07-26 05:56:50 -050079smbios_instance_string_exist(struct kobject *kobj, struct attribute *attr,
80 int n)
81{
82 struct device *dev;
83 struct pci_dev *pdev;
84
85 dev = container_of(kobj, struct device, kobj);
86 pdev = to_pci_dev(dev);
87
88 return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
89 S_IRUGO : 0;
90}
91
92static ssize_t
93smbioslabel_show(struct device *dev, struct device_attribute *attr, char *buf)
94{
95 struct pci_dev *pdev;
96 pdev = to_pci_dev(dev);
97
98 return find_smbios_instance_string(pdev, buf,
99 SMBIOS_ATTR_LABEL_SHOW);
100}
101
102static ssize_t
103smbiosinstance_show(struct device *dev,
104 struct device_attribute *attr, char *buf)
105{
106 struct pci_dev *pdev;
107 pdev = to_pci_dev(dev);
108
109 return find_smbios_instance_string(pdev, buf,
110 SMBIOS_ATTR_INSTANCE_SHOW);
111}
112
113static struct device_attribute smbios_attr_label = {
Stephen Rothwell763e9db2010-08-04 14:25:31 +1000114 .attr = {.name = "label", .mode = 0444},
Narendra K911e1c92010-07-26 05:56:50 -0500115 .show = smbioslabel_show,
116};
117
118static struct device_attribute smbios_attr_instance = {
Stephen Rothwell763e9db2010-08-04 14:25:31 +1000119 .attr = {.name = "index", .mode = 0444},
Narendra K911e1c92010-07-26 05:56:50 -0500120 .show = smbiosinstance_show,
121};
122
123static struct attribute *smbios_attributes[] = {
124 &smbios_attr_label.attr,
125 &smbios_attr_instance.attr,
126 NULL,
127};
128
129static struct attribute_group smbios_attr_group = {
130 .attrs = smbios_attributes,
131 .is_visible = smbios_instance_string_exist,
132};
133
134static int
135pci_create_smbiosname_file(struct pci_dev *pdev)
136{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530137 return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
Narendra K911e1c92010-07-26 05:56:50 -0500138}
139
140static void
141pci_remove_smbiosname_file(struct pci_dev *pdev)
142{
143 sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
144}
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700145#else
146static inline int
147pci_create_smbiosname_file(struct pci_dev *pdev)
148{
149 return -1;
150}
Narendra K911e1c92010-07-26 05:56:50 -0500151
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700152static inline void
153pci_remove_smbiosname_file(struct pci_dev *pdev)
154{
155}
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530156#endif
157
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700158#ifdef CONFIG_ACPI
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530159static const char device_label_dsm_uuid[] = {
160 0xD0, 0x37, 0xC9, 0xE5, 0x53, 0x35, 0x7A, 0x4D,
161 0x91, 0x17, 0xEA, 0x4D, 0x19, 0xC3, 0x43, 0x4D
162};
163
164enum acpi_attr_enum {
165 ACPI_ATTR_NONE = 0,
166 ACPI_ATTR_LABEL_SHOW,
167 ACPI_ATTR_INDEX_SHOW,
168};
169
170static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
171{
172 int len;
173 len = utf16s_to_utf8s((const wchar_t *)obj->
174 package.elements[1].string.pointer,
175 obj->package.elements[1].string.length,
176 UTF16_LITTLE_ENDIAN,
177 buf, PAGE_SIZE);
178 buf[len] = '\n';
179}
180
181static int
182dsm_get_label(acpi_handle handle, int func,
183 struct acpi_buffer *output,
184 char *buf, enum acpi_attr_enum attribute)
185{
186 struct acpi_object_list input;
187 union acpi_object params[4];
188 union acpi_object *obj;
189 int len = 0;
190
191 int err;
192
193 input.count = 4;
194 input.pointer = params;
195 params[0].type = ACPI_TYPE_BUFFER;
196 params[0].buffer.length = sizeof(device_label_dsm_uuid);
197 params[0].buffer.pointer = (char *)device_label_dsm_uuid;
198 params[1].type = ACPI_TYPE_INTEGER;
199 params[1].integer.value = 0x02;
200 params[2].type = ACPI_TYPE_INTEGER;
201 params[2].integer.value = func;
202 params[3].type = ACPI_TYPE_PACKAGE;
203 params[3].package.count = 0;
204 params[3].package.elements = NULL;
205
206 err = acpi_evaluate_object(handle, "_DSM", &input, output);
207 if (err)
208 return -1;
209
210 obj = (union acpi_object *)output->pointer;
211
212 switch (obj->type) {
213 case ACPI_TYPE_PACKAGE:
214 if (obj->package.count != 2)
215 break;
216 len = obj->package.elements[0].integer.value;
217 if (buf) {
218 if (attribute == ACPI_ATTR_INDEX_SHOW)
219 scnprintf(buf, PAGE_SIZE, "%llu\n",
220 obj->package.elements[0].integer.value);
221 else if (attribute == ACPI_ATTR_LABEL_SHOW)
222 dsm_label_utf16s_to_utf8s(obj, buf);
223 kfree(output->pointer);
224 return strlen(buf);
225 }
226 kfree(output->pointer);
227 return len;
228 break;
229 default:
230 kfree(output->pointer);
231 }
232 return -1;
233}
234
235static bool
236device_has_dsm(struct device *dev)
237{
238 acpi_handle handle;
239 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
240
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100241 handle = ACPI_HANDLE(dev);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530242
243 if (!handle)
244 return FALSE;
245
246 if (dsm_get_label(handle, DEVICE_LABEL_DSM, &output, NULL,
247 ACPI_ATTR_NONE) > 0)
248 return TRUE;
249
250 return FALSE;
251}
252
Al Viro587a1f12011-07-23 23:11:19 -0400253static umode_t
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530254acpi_index_string_exist(struct kobject *kobj, struct attribute *attr, int n)
255{
256 struct device *dev;
257
258 dev = container_of(kobj, struct device, kobj);
259
260 if (device_has_dsm(dev))
261 return S_IRUGO;
262
263 return 0;
264}
265
266static ssize_t
267acpilabel_show(struct device *dev, struct device_attribute *attr, char *buf)
268{
269 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
270 acpi_handle handle;
271 int length;
272
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100273 handle = ACPI_HANDLE(dev);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530274
275 if (!handle)
276 return -1;
277
278 length = dsm_get_label(handle, DEVICE_LABEL_DSM,
279 &output, buf, ACPI_ATTR_LABEL_SHOW);
280
281 if (length < 1)
282 return -1;
283
284 return length;
285}
286
287static ssize_t
288acpiindex_show(struct device *dev, struct device_attribute *attr, char *buf)
289{
290 struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
291 acpi_handle handle;
292 int length;
293
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +0100294 handle = ACPI_HANDLE(dev);
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530295
296 if (!handle)
297 return -1;
298
299 length = dsm_get_label(handle, DEVICE_LABEL_DSM,
300 &output, buf, ACPI_ATTR_INDEX_SHOW);
301
302 if (length < 0)
303 return -1;
304
305 return length;
306
307}
308
309static struct device_attribute acpi_attr_label = {
310 .attr = {.name = "label", .mode = 0444},
311 .show = acpilabel_show,
312};
313
314static struct device_attribute acpi_attr_index = {
315 .attr = {.name = "acpi_index", .mode = 0444},
316 .show = acpiindex_show,
317};
318
319static struct attribute *acpi_attributes[] = {
320 &acpi_attr_label.attr,
321 &acpi_attr_index.attr,
322 NULL,
323};
324
325static struct attribute_group acpi_attr_group = {
326 .attrs = acpi_attributes,
327 .is_visible = acpi_index_string_exist,
328};
329
330static int
331pci_create_acpi_index_label_files(struct pci_dev *pdev)
332{
333 return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
334}
335
336static int
337pci_remove_acpi_index_label_files(struct pci_dev *pdev)
338{
339 sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
340 return 0;
341}
Bjorn Helgaas4c859802014-01-13 17:01:11 -0700342#else
343static inline int
344pci_create_acpi_index_label_files(struct pci_dev *pdev)
345{
346 return -1;
347}
348
349static inline int
350pci_remove_acpi_index_label_files(struct pci_dev *pdev)
351{
352 return -1;
353}
354
355static inline bool
356device_has_dsm(struct device *dev)
357{
358 return false;
359}
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530360#endif
361
Narendra K911e1c92010-07-26 05:56:50 -0500362void pci_create_firmware_label_files(struct pci_dev *pdev)
363{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530364 if (device_has_dsm(&pdev->dev))
365 pci_create_acpi_index_label_files(pdev);
366 else
367 pci_create_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500368}
369
370void pci_remove_firmware_label_files(struct pci_dev *pdev)
371{
Narendra_K@Dell.com60589892011-03-02 22:34:17 +0530372 if (device_has_dsm(&pdev->dev))
373 pci_remove_acpi_index_label_files(pdev);
374 else
375 pci_remove_smbiosname_file(pdev);
Narendra K911e1c92010-07-26 05:56:50 -0500376}