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>
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
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
19 * Please see http://linux.dell.com/wiki/index.php/Oss/libnetdevname for more
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>
35 #define DEVICE_LABEL_DSM 0x07
38 enum smbios_attr_enum
{
40 SMBIOS_ATTR_LABEL_SHOW
,
41 SMBIOS_ATTR_INSTANCE_SHOW
,
45 find_smbios_instance_string(struct pci_dev
*pdev
, char *buf
,
46 enum smbios_attr_enum attribute
)
48 const struct dmi_device
*dmi
;
49 struct dmi_dev_onboard
*donboard
;
53 bus
= pdev
->bus
->number
;
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
) {
63 if (attribute
== SMBIOS_ATTR_INSTANCE_SHOW
)
64 return scnprintf(buf
, PAGE_SIZE
,
67 else if (attribute
== SMBIOS_ATTR_LABEL_SHOW
)
68 return scnprintf(buf
, PAGE_SIZE
,
72 return strlen(dmi
->name
);
79 smbios_instance_string_exist(struct kobject
*kobj
, struct attribute
*attr
,
85 dev
= container_of(kobj
, struct device
, kobj
);
86 pdev
= to_pci_dev(dev
);
88 return find_smbios_instance_string(pdev
, NULL
, SMBIOS_ATTR_NONE
) ?
93 smbioslabel_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
96 pdev
= to_pci_dev(dev
);
98 return find_smbios_instance_string(pdev
, buf
,
99 SMBIOS_ATTR_LABEL_SHOW
);
103 smbiosinstance_show(struct device
*dev
,
104 struct device_attribute
*attr
, char *buf
)
106 struct pci_dev
*pdev
;
107 pdev
= to_pci_dev(dev
);
109 return find_smbios_instance_string(pdev
, buf
,
110 SMBIOS_ATTR_INSTANCE_SHOW
);
113 static struct device_attribute smbios_attr_label
= {
114 .attr
= {.name
= "label", .mode
= 0444},
115 .show
= smbioslabel_show
,
118 static struct device_attribute smbios_attr_instance
= {
119 .attr
= {.name
= "index", .mode
= 0444},
120 .show
= smbiosinstance_show
,
123 static struct attribute
*smbios_attributes
[] = {
124 &smbios_attr_label
.attr
,
125 &smbios_attr_instance
.attr
,
129 static struct attribute_group smbios_attr_group
= {
130 .attrs
= smbios_attributes
,
131 .is_visible
= smbios_instance_string_exist
,
135 pci_create_smbiosname_file(struct pci_dev
*pdev
)
137 return sysfs_create_group(&pdev
->dev
.kobj
, &smbios_attr_group
);
141 pci_remove_smbiosname_file(struct pci_dev
*pdev
)
143 sysfs_remove_group(&pdev
->dev
.kobj
, &smbios_attr_group
);
147 pci_create_smbiosname_file(struct pci_dev
*pdev
)
153 pci_remove_smbiosname_file(struct pci_dev
*pdev
)
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
164 enum acpi_attr_enum
{
166 ACPI_ATTR_LABEL_SHOW
,
167 ACPI_ATTR_INDEX_SHOW
,
170 static void dsm_label_utf16s_to_utf8s(union acpi_object
*obj
, char *buf
)
173 len
= utf16s_to_utf8s((const wchar_t *)obj
->
174 package
.elements
[1].string
.pointer
,
175 obj
->package
.elements
[1].string
.length
,
182 dsm_get_label(acpi_handle handle
, int func
,
183 struct acpi_buffer
*output
,
184 char *buf
, enum acpi_attr_enum attribute
)
186 struct acpi_object_list input
;
187 union acpi_object params
[4];
188 union acpi_object
*obj
;
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
;
206 err
= acpi_evaluate_object(handle
, "_DSM", &input
, output
);
210 obj
= (union acpi_object
*)output
->pointer
;
213 case ACPI_TYPE_PACKAGE
:
214 if (obj
->package
.count
!= 2)
216 len
= obj
->package
.elements
[0].integer
.value
;
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
);
226 kfree(output
->pointer
);
230 kfree(output
->pointer
);
236 device_has_dsm(struct device
*dev
)
239 struct acpi_buffer output
= {ACPI_ALLOCATE_BUFFER
, NULL
};
241 handle
= ACPI_HANDLE(dev
);
246 if (dsm_get_label(handle
, DEVICE_LABEL_DSM
, &output
, NULL
,
254 acpi_index_string_exist(struct kobject
*kobj
, struct attribute
*attr
, int n
)
258 dev
= container_of(kobj
, struct device
, kobj
);
260 if (device_has_dsm(dev
))
267 acpilabel_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
269 struct acpi_buffer output
= {ACPI_ALLOCATE_BUFFER
, NULL
};
273 handle
= ACPI_HANDLE(dev
);
278 length
= dsm_get_label(handle
, DEVICE_LABEL_DSM
,
279 &output
, buf
, ACPI_ATTR_LABEL_SHOW
);
288 acpiindex_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
290 struct acpi_buffer output
= {ACPI_ALLOCATE_BUFFER
, NULL
};
294 handle
= ACPI_HANDLE(dev
);
299 length
= dsm_get_label(handle
, DEVICE_LABEL_DSM
,
300 &output
, buf
, ACPI_ATTR_INDEX_SHOW
);
309 static struct device_attribute acpi_attr_label
= {
310 .attr
= {.name
= "label", .mode
= 0444},
311 .show
= acpilabel_show
,
314 static struct device_attribute acpi_attr_index
= {
315 .attr
= {.name
= "acpi_index", .mode
= 0444},
316 .show
= acpiindex_show
,
319 static struct attribute
*acpi_attributes
[] = {
320 &acpi_attr_label
.attr
,
321 &acpi_attr_index
.attr
,
325 static struct attribute_group acpi_attr_group
= {
326 .attrs
= acpi_attributes
,
327 .is_visible
= acpi_index_string_exist
,
331 pci_create_acpi_index_label_files(struct pci_dev
*pdev
)
333 return sysfs_create_group(&pdev
->dev
.kobj
, &acpi_attr_group
);
337 pci_remove_acpi_index_label_files(struct pci_dev
*pdev
)
339 sysfs_remove_group(&pdev
->dev
.kobj
, &acpi_attr_group
);
344 pci_create_acpi_index_label_files(struct pci_dev
*pdev
)
350 pci_remove_acpi_index_label_files(struct pci_dev
*pdev
)
356 device_has_dsm(struct device
*dev
)
362 void pci_create_firmware_label_files(struct pci_dev
*pdev
)
364 if (device_has_dsm(&pdev
->dev
))
365 pci_create_acpi_index_label_files(pdev
);
367 pci_create_smbiosname_file(pdev
);
370 void pci_remove_firmware_label_files(struct pci_dev
*pdev
)
372 if (device_has_dsm(&pdev
->dev
))
373 pci_remove_acpi_index_label_files(pdev
);
375 pci_remove_smbiosname_file(pdev
);