pci-label.c 7.1 KB

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