pci-label.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Export the firmware instance and label associated with a PCI device to
  4. * sysfs
  5. *
  6. * Copyright (C) 2010 Dell Inc.
  7. * by Narendra K <[email protected]>,
  8. * Jordan Hargrave <[email protected]>
  9. *
  10. * PCI Firmware Specification Revision 3.1 section 4.6.7 (DSM for Naming a
  11. * PCI or PCI Express Device Under Operating Systems) defines an instance
  12. * number and string name. This code retrieves them and exports them to sysfs.
  13. * If the system firmware does not provide the ACPI _DSM (Device Specific
  14. * Method), then the SMBIOS type 41 instance number and string is exported to
  15. * sysfs.
  16. *
  17. * SMBIOS defines type 41 for onboard pci devices. This code retrieves
  18. * the instance number and string from the type 41 record and exports
  19. * it to sysfs.
  20. *
  21. * Please see https://linux.dell.com/files/biosdevname/ for more
  22. * information.
  23. */
  24. #include <linux/dmi.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/pci.h>
  27. #include <linux/pci_ids.h>
  28. #include <linux/module.h>
  29. #include <linux/device.h>
  30. #include <linux/nls.h>
  31. #include <linux/acpi.h>
  32. #include <linux/pci-acpi.h>
  33. #include "pci.h"
  34. static bool device_has_acpi_name(struct device *dev)
  35. {
  36. #ifdef CONFIG_ACPI
  37. acpi_handle handle = ACPI_HANDLE(dev);
  38. if (!handle)
  39. return false;
  40. return acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2,
  41. 1 << DSM_PCI_DEVICE_NAME);
  42. #else
  43. return false;
  44. #endif
  45. }
  46. #ifdef CONFIG_DMI
  47. enum smbios_attr_enum {
  48. SMBIOS_ATTR_NONE = 0,
  49. SMBIOS_ATTR_LABEL_SHOW,
  50. SMBIOS_ATTR_INSTANCE_SHOW,
  51. };
  52. static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
  53. enum smbios_attr_enum attribute)
  54. {
  55. const struct dmi_device *dmi;
  56. struct dmi_dev_onboard *donboard;
  57. int domain_nr = pci_domain_nr(pdev->bus);
  58. int bus = pdev->bus->number;
  59. int devfn = pdev->devfn;
  60. dmi = NULL;
  61. while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
  62. NULL, dmi)) != NULL) {
  63. donboard = dmi->device_data;
  64. if (donboard && donboard->segment == domain_nr &&
  65. donboard->bus == bus &&
  66. donboard->devfn == devfn) {
  67. if (buf) {
  68. if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
  69. return sysfs_emit(buf, "%d\n",
  70. donboard->instance);
  71. else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
  72. return sysfs_emit(buf, "%s\n",
  73. dmi->name);
  74. }
  75. return strlen(dmi->name);
  76. }
  77. }
  78. return 0;
  79. }
  80. static ssize_t smbios_label_show(struct device *dev,
  81. struct device_attribute *attr, char *buf)
  82. {
  83. struct pci_dev *pdev = to_pci_dev(dev);
  84. return find_smbios_instance_string(pdev, buf,
  85. SMBIOS_ATTR_LABEL_SHOW);
  86. }
  87. static struct device_attribute dev_attr_smbios_label = __ATTR(label, 0444,
  88. smbios_label_show, NULL);
  89. static ssize_t index_show(struct device *dev, struct device_attribute *attr,
  90. char *buf)
  91. {
  92. struct pci_dev *pdev = to_pci_dev(dev);
  93. return find_smbios_instance_string(pdev, buf,
  94. SMBIOS_ATTR_INSTANCE_SHOW);
  95. }
  96. static DEVICE_ATTR_RO(index);
  97. static struct attribute *smbios_attrs[] = {
  98. &dev_attr_smbios_label.attr,
  99. &dev_attr_index.attr,
  100. NULL,
  101. };
  102. static umode_t smbios_attr_is_visible(struct kobject *kobj, struct attribute *a,
  103. int n)
  104. {
  105. struct device *dev = kobj_to_dev(kobj);
  106. struct pci_dev *pdev = to_pci_dev(dev);
  107. if (device_has_acpi_name(dev))
  108. return 0;
  109. if (!find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE))
  110. return 0;
  111. return a->mode;
  112. }
  113. const struct attribute_group pci_dev_smbios_attr_group = {
  114. .attrs = smbios_attrs,
  115. .is_visible = smbios_attr_is_visible,
  116. };
  117. #endif
  118. #ifdef CONFIG_ACPI
  119. enum acpi_attr_enum {
  120. ACPI_ATTR_LABEL_SHOW,
  121. ACPI_ATTR_INDEX_SHOW,
  122. };
  123. static int dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
  124. {
  125. int len;
  126. len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer,
  127. obj->buffer.length,
  128. UTF16_LITTLE_ENDIAN,
  129. buf, PAGE_SIZE - 1);
  130. buf[len++] = '\n';
  131. return len;
  132. }
  133. static int dsm_get_label(struct device *dev, char *buf,
  134. enum acpi_attr_enum attr)
  135. {
  136. acpi_handle handle = ACPI_HANDLE(dev);
  137. union acpi_object *obj, *tmp;
  138. int len = 0;
  139. if (!handle)
  140. return -1;
  141. obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 0x2,
  142. DSM_PCI_DEVICE_NAME, NULL);
  143. if (!obj)
  144. return -1;
  145. tmp = obj->package.elements;
  146. if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
  147. tmp[0].type == ACPI_TYPE_INTEGER &&
  148. (tmp[1].type == ACPI_TYPE_STRING ||
  149. tmp[1].type == ACPI_TYPE_BUFFER)) {
  150. /*
  151. * The second string element is optional even when
  152. * this _DSM is implemented; when not implemented,
  153. * this entry must return a null string.
  154. */
  155. if (attr == ACPI_ATTR_INDEX_SHOW) {
  156. len = sysfs_emit(buf, "%llu\n", tmp->integer.value);
  157. } else if (attr == ACPI_ATTR_LABEL_SHOW) {
  158. if (tmp[1].type == ACPI_TYPE_STRING)
  159. len = sysfs_emit(buf, "%s\n",
  160. tmp[1].string.pointer);
  161. else if (tmp[1].type == ACPI_TYPE_BUFFER)
  162. len = dsm_label_utf16s_to_utf8s(tmp + 1, buf);
  163. }
  164. }
  165. ACPI_FREE(obj);
  166. return len > 0 ? len : -1;
  167. }
  168. static ssize_t label_show(struct device *dev, struct device_attribute *attr,
  169. char *buf)
  170. {
  171. return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
  172. }
  173. static DEVICE_ATTR_RO(label);
  174. static ssize_t acpi_index_show(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
  178. }
  179. static DEVICE_ATTR_RO(acpi_index);
  180. static struct attribute *acpi_attrs[] = {
  181. &dev_attr_label.attr,
  182. &dev_attr_acpi_index.attr,
  183. NULL,
  184. };
  185. static umode_t acpi_attr_is_visible(struct kobject *kobj, struct attribute *a,
  186. int n)
  187. {
  188. struct device *dev = kobj_to_dev(kobj);
  189. if (!device_has_acpi_name(dev))
  190. return 0;
  191. return a->mode;
  192. }
  193. const struct attribute_group pci_dev_acpi_attr_group = {
  194. .attrs = acpi_attrs,
  195. .is_visible = acpi_attr_is_visible,
  196. };
  197. #endif