pci-label.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <Narendra_K@dell.com>,
  8. * Jordan Hargrave <Jordan_Hargrave@dell.com>
  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. #ifdef CONFIG_DMI
  35. enum smbios_attr_enum {
  36. SMBIOS_ATTR_NONE = 0,
  37. SMBIOS_ATTR_LABEL_SHOW,
  38. SMBIOS_ATTR_INSTANCE_SHOW,
  39. };
  40. static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
  41. enum smbios_attr_enum attribute)
  42. {
  43. const struct dmi_device *dmi;
  44. struct dmi_dev_onboard *donboard;
  45. int domain_nr;
  46. int bus;
  47. int devfn;
  48. domain_nr = pci_domain_nr(pdev->bus);
  49. bus = pdev->bus->number;
  50. devfn = pdev->devfn;
  51. dmi = NULL;
  52. while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
  53. NULL, dmi)) != NULL) {
  54. donboard = dmi->device_data;
  55. if (donboard && donboard->segment == domain_nr &&
  56. donboard->bus == bus &&
  57. donboard->devfn == devfn) {
  58. if (buf) {
  59. if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
  60. return scnprintf(buf, PAGE_SIZE,
  61. "%d\n",
  62. donboard->instance);
  63. else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
  64. return scnprintf(buf, PAGE_SIZE,
  65. "%s\n",
  66. dmi->name);
  67. }
  68. return strlen(dmi->name);
  69. }
  70. }
  71. return 0;
  72. }
  73. static umode_t smbios_instance_string_exist(struct kobject *kobj,
  74. struct attribute *attr, int n)
  75. {
  76. struct device *dev;
  77. struct pci_dev *pdev;
  78. dev = kobj_to_dev(kobj);
  79. pdev = to_pci_dev(dev);
  80. return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
  81. S_IRUGO : 0;
  82. }
  83. static ssize_t smbioslabel_show(struct device *dev,
  84. struct device_attribute *attr, char *buf)
  85. {
  86. struct pci_dev *pdev;
  87. pdev = to_pci_dev(dev);
  88. return find_smbios_instance_string(pdev, buf,
  89. SMBIOS_ATTR_LABEL_SHOW);
  90. }
  91. static ssize_t smbiosinstance_show(struct device *dev,
  92. struct device_attribute *attr, char *buf)
  93. {
  94. struct pci_dev *pdev;
  95. pdev = to_pci_dev(dev);
  96. return find_smbios_instance_string(pdev, buf,
  97. SMBIOS_ATTR_INSTANCE_SHOW);
  98. }
  99. static struct device_attribute smbios_attr_label = {
  100. .attr = {.name = "label", .mode = 0444},
  101. .show = smbioslabel_show,
  102. };
  103. static struct device_attribute smbios_attr_instance = {
  104. .attr = {.name = "index", .mode = 0444},
  105. .show = smbiosinstance_show,
  106. };
  107. static struct attribute *smbios_attributes[] = {
  108. &smbios_attr_label.attr,
  109. &smbios_attr_instance.attr,
  110. NULL,
  111. };
  112. static const struct attribute_group smbios_attr_group = {
  113. .attrs = smbios_attributes,
  114. .is_visible = smbios_instance_string_exist,
  115. };
  116. static int pci_create_smbiosname_file(struct pci_dev *pdev)
  117. {
  118. return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
  119. }
  120. static void pci_remove_smbiosname_file(struct pci_dev *pdev)
  121. {
  122. sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
  123. }
  124. #else
  125. static inline int pci_create_smbiosname_file(struct pci_dev *pdev)
  126. {
  127. return -1;
  128. }
  129. static inline void pci_remove_smbiosname_file(struct pci_dev *pdev)
  130. {
  131. }
  132. #endif
  133. #ifdef CONFIG_ACPI
  134. enum acpi_attr_enum {
  135. ACPI_ATTR_LABEL_SHOW,
  136. ACPI_ATTR_INDEX_SHOW,
  137. };
  138. static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
  139. {
  140. int len;
  141. len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer,
  142. obj->buffer.length,
  143. UTF16_LITTLE_ENDIAN,
  144. buf, PAGE_SIZE - 1);
  145. buf[len] = '\n';
  146. }
  147. static int dsm_get_label(struct device *dev, char *buf,
  148. enum acpi_attr_enum attr)
  149. {
  150. acpi_handle handle;
  151. union acpi_object *obj, *tmp;
  152. int len = -1;
  153. handle = ACPI_HANDLE(dev);
  154. if (!handle)
  155. return -1;
  156. obj = acpi_evaluate_dsm(handle, &pci_acpi_dsm_guid, 0x2,
  157. DSM_PCI_DEVICE_NAME, NULL);
  158. if (!obj)
  159. return -1;
  160. tmp = obj->package.elements;
  161. if (obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 2 &&
  162. tmp[0].type == ACPI_TYPE_INTEGER &&
  163. (tmp[1].type == ACPI_TYPE_STRING ||
  164. tmp[1].type == ACPI_TYPE_BUFFER)) {
  165. /*
  166. * The second string element is optional even when
  167. * this _DSM is implemented; when not implemented,
  168. * this entry must return a null string.
  169. */
  170. if (attr == ACPI_ATTR_INDEX_SHOW) {
  171. scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value);
  172. } else if (attr == ACPI_ATTR_LABEL_SHOW) {
  173. if (tmp[1].type == ACPI_TYPE_STRING)
  174. scnprintf(buf, PAGE_SIZE, "%s\n",
  175. tmp[1].string.pointer);
  176. else if (tmp[1].type == ACPI_TYPE_BUFFER)
  177. dsm_label_utf16s_to_utf8s(tmp + 1, buf);
  178. }
  179. len = strlen(buf) > 0 ? strlen(buf) : -1;
  180. }
  181. ACPI_FREE(obj);
  182. return len;
  183. }
  184. static bool device_has_dsm(struct device *dev)
  185. {
  186. acpi_handle handle;
  187. handle = ACPI_HANDLE(dev);
  188. if (!handle)
  189. return false;
  190. return !!acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2,
  191. 1 << DSM_PCI_DEVICE_NAME);
  192. }
  193. static umode_t acpi_index_string_exist(struct kobject *kobj,
  194. struct attribute *attr, int n)
  195. {
  196. struct device *dev;
  197. dev = kobj_to_dev(kobj);
  198. if (device_has_dsm(dev))
  199. return S_IRUGO;
  200. return 0;
  201. }
  202. static ssize_t acpilabel_show(struct device *dev,
  203. struct device_attribute *attr, char *buf)
  204. {
  205. return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
  206. }
  207. static ssize_t acpiindex_show(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
  211. }
  212. static struct device_attribute acpi_attr_label = {
  213. .attr = {.name = "label", .mode = 0444},
  214. .show = acpilabel_show,
  215. };
  216. static struct device_attribute acpi_attr_index = {
  217. .attr = {.name = "acpi_index", .mode = 0444},
  218. .show = acpiindex_show,
  219. };
  220. static struct attribute *acpi_attributes[] = {
  221. &acpi_attr_label.attr,
  222. &acpi_attr_index.attr,
  223. NULL,
  224. };
  225. static const struct attribute_group acpi_attr_group = {
  226. .attrs = acpi_attributes,
  227. .is_visible = acpi_index_string_exist,
  228. };
  229. static int pci_create_acpi_index_label_files(struct pci_dev *pdev)
  230. {
  231. return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
  232. }
  233. static int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
  234. {
  235. sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
  236. return 0;
  237. }
  238. #else
  239. static inline int pci_create_acpi_index_label_files(struct pci_dev *pdev)
  240. {
  241. return -1;
  242. }
  243. static inline int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
  244. {
  245. return -1;
  246. }
  247. static inline bool device_has_dsm(struct device *dev)
  248. {
  249. return false;
  250. }
  251. #endif
  252. void pci_create_firmware_label_files(struct pci_dev *pdev)
  253. {
  254. if (device_has_dsm(&pdev->dev))
  255. pci_create_acpi_index_label_files(pdev);
  256. else
  257. pci_create_smbiosname_file(pdev);
  258. }
  259. void pci_remove_firmware_label_files(struct pci_dev *pdev)
  260. {
  261. if (device_has_dsm(&pdev->dev))
  262. pci_remove_acpi_index_label_files(pdev);
  263. else
  264. pci_remove_smbiosname_file(pdev);
  265. }