pci-label.c 7.2 KB

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