]> Pileus Git - ~andy/linux/blob - drivers/pci/pci-label.c
Merge tag 'clk-for-linus-3.14-part1' of git://git.linaro.org/people/mike.turquette...
[~andy/linux] / drivers / pci / pci-label.c
1 /*
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  *
8  * 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  *
15  * 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>
29 #include <linux/nls.h>
30 #include <linux/acpi.h>
31 #include <linux/pci-acpi.h>
32 #include <acpi/acpi_bus.h>
33 #include "pci.h"
34
35 #define DEVICE_LABEL_DSM        0x07
36
37 #ifdef CONFIG_DMI
38 enum smbios_attr_enum {
39         SMBIOS_ATTR_NONE = 0,
40         SMBIOS_ATTR_LABEL_SHOW,
41         SMBIOS_ATTR_INSTANCE_SHOW,
42 };
43
44 static size_t
45 find_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
78 static umode_t
79 smbios_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
92 static ssize_t
93 smbioslabel_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
102 static ssize_t
103 smbiosinstance_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
113 static struct device_attribute smbios_attr_label = {
114         .attr = {.name = "label", .mode = 0444},
115         .show = smbioslabel_show,
116 };
117
118 static struct device_attribute smbios_attr_instance = {
119         .attr = {.name = "index", .mode = 0444},
120         .show = smbiosinstance_show,
121 };
122
123 static struct attribute *smbios_attributes[] = {
124         &smbios_attr_label.attr,
125         &smbios_attr_instance.attr,
126         NULL,
127 };
128
129 static struct attribute_group smbios_attr_group = {
130         .attrs = smbios_attributes,
131         .is_visible = smbios_instance_string_exist,
132 };
133
134 static int
135 pci_create_smbiosname_file(struct pci_dev *pdev)
136 {
137         return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
138 }
139
140 static void
141 pci_remove_smbiosname_file(struct pci_dev *pdev)
142 {
143         sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
144 }
145 #else
146 static inline int
147 pci_create_smbiosname_file(struct pci_dev *pdev)
148 {
149         return -1;
150 }
151
152 static inline void
153 pci_remove_smbiosname_file(struct pci_dev *pdev)
154 {
155 }
156 #endif
157
158 #ifdef CONFIG_ACPI
159 static 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
164 enum acpi_attr_enum {
165         ACPI_ATTR_NONE = 0,
166         ACPI_ATTR_LABEL_SHOW,
167         ACPI_ATTR_INDEX_SHOW,
168 };
169
170 static 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
181 static int
182 dsm_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
235 static bool
236 device_has_dsm(struct device *dev)
237 {
238         acpi_handle handle;
239         struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
240
241         handle = ACPI_HANDLE(dev);
242
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
253 static umode_t
254 acpi_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
266 static ssize_t
267 acpilabel_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
273         handle = ACPI_HANDLE(dev);
274
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
287 static ssize_t
288 acpiindex_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
294         handle = ACPI_HANDLE(dev);
295
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
309 static struct device_attribute acpi_attr_label = {
310         .attr = {.name = "label", .mode = 0444},
311         .show = acpilabel_show,
312 };
313
314 static struct device_attribute acpi_attr_index = {
315         .attr = {.name = "acpi_index", .mode = 0444},
316         .show = acpiindex_show,
317 };
318
319 static struct attribute *acpi_attributes[] = {
320         &acpi_attr_label.attr,
321         &acpi_attr_index.attr,
322         NULL,
323 };
324
325 static struct attribute_group acpi_attr_group = {
326         .attrs = acpi_attributes,
327         .is_visible = acpi_index_string_exist,
328 };
329
330 static int
331 pci_create_acpi_index_label_files(struct pci_dev *pdev)
332 {
333         return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
334 }
335
336 static int
337 pci_remove_acpi_index_label_files(struct pci_dev *pdev)
338 {
339         sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
340         return 0;
341 }
342 #else
343 static inline int
344 pci_create_acpi_index_label_files(struct pci_dev *pdev)
345 {
346         return -1;
347 }
348
349 static inline int
350 pci_remove_acpi_index_label_files(struct pci_dev *pdev)
351 {
352         return -1;
353 }
354
355 static inline bool
356 device_has_dsm(struct device *dev)
357 {
358         return false;
359 }
360 #endif
361
362 void pci_create_firmware_label_files(struct pci_dev *pdev)
363 {
364         if (device_has_dsm(&pdev->dev))
365                 pci_create_acpi_index_label_files(pdev);
366         else
367                 pci_create_smbiosname_file(pdev);
368 }
369
370 void pci_remove_firmware_label_files(struct pci_dev *pdev)
371 {
372         if (device_has_dsm(&pdev->dev))
373                 pci_remove_acpi_index_label_files(pdev);
374         else
375                 pci_remove_smbiosname_file(pdev);
376 }