pci-label.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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/wiki/index.php/Oss/libnetdevname 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. #define DEVICE_LABEL_DSM 0x07
  33. #ifdef CONFIG_DMI
  34. enum smbios_attr_enum {
  35. SMBIOS_ATTR_NONE = 0,
  36. SMBIOS_ATTR_LABEL_SHOW,
  37. SMBIOS_ATTR_INSTANCE_SHOW,
  38. };
  39. static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
  40. enum smbios_attr_enum attribute)
  41. {
  42. const struct dmi_device *dmi;
  43. struct dmi_dev_onboard *donboard;
  44. int bus;
  45. int devfn;
  46. bus = pdev->bus->number;
  47. devfn = pdev->devfn;
  48. dmi = NULL;
  49. while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
  50. NULL, dmi)) != NULL) {
  51. donboard = dmi->device_data;
  52. if (donboard && donboard->bus == bus &&
  53. donboard->devfn == devfn) {
  54. if (buf) {
  55. if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
  56. return scnprintf(buf, PAGE_SIZE,
  57. "%d\n",
  58. donboard->instance);
  59. else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
  60. return scnprintf(buf, PAGE_SIZE,
  61. "%s\n",
  62. dmi->name);
  63. }
  64. return strlen(dmi->name);
  65. }
  66. }
  67. return 0;
  68. }
  69. static umode_t smbios_instance_string_exist(struct kobject *kobj,
  70. struct attribute *attr, int n)
  71. {
  72. struct device *dev;
  73. struct pci_dev *pdev;
  74. dev = container_of(kobj, struct device, kobj);
  75. pdev = to_pci_dev(dev);
  76. return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
  77. S_IRUGO : 0;
  78. }
  79. static ssize_t smbioslabel_show(struct device *dev,
  80. struct device_attribute *attr, char *buf)
  81. {
  82. struct pci_dev *pdev;
  83. pdev = to_pci_dev(dev);
  84. return find_smbios_instance_string(pdev, buf,
  85. SMBIOS_ATTR_LABEL_SHOW);
  86. }
  87. static ssize_t smbiosinstance_show(struct device *dev,
  88. struct device_attribute *attr, char *buf)
  89. {
  90. struct pci_dev *pdev;
  91. pdev = to_pci_dev(dev);
  92. return find_smbios_instance_string(pdev, buf,
  93. SMBIOS_ATTR_INSTANCE_SHOW);
  94. }
  95. static struct device_attribute smbios_attr_label = {
  96. .attr = {.name = "label", .mode = 0444},
  97. .show = smbioslabel_show,
  98. };
  99. static struct device_attribute smbios_attr_instance = {
  100. .attr = {.name = "index", .mode = 0444},
  101. .show = smbiosinstance_show,
  102. };
  103. static struct attribute *smbios_attributes[] = {
  104. &smbios_attr_label.attr,
  105. &smbios_attr_instance.attr,
  106. NULL,
  107. };
  108. static struct attribute_group smbios_attr_group = {
  109. .attrs = smbios_attributes,
  110. .is_visible = smbios_instance_string_exist,
  111. };
  112. static int pci_create_smbiosname_file(struct pci_dev *pdev)
  113. {
  114. return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
  115. }
  116. static void pci_remove_smbiosname_file(struct pci_dev *pdev)
  117. {
  118. sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
  119. }
  120. #else
  121. static inline int pci_create_smbiosname_file(struct pci_dev *pdev)
  122. {
  123. return -1;
  124. }
  125. static inline void pci_remove_smbiosname_file(struct pci_dev *pdev)
  126. {
  127. }
  128. #endif
  129. #ifdef CONFIG_ACPI
  130. static const char device_label_dsm_uuid[] = {
  131. 0xD0, 0x37, 0xC9, 0xE5, 0x53, 0x35, 0x7A, 0x4D,
  132. 0x91, 0x17, 0xEA, 0x4D, 0x19, 0xC3, 0x43, 0x4D
  133. };
  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);
  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, device_label_dsm_uuid, 0x2,
  157. DEVICE_LABEL_DSM, 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, device_label_dsm_uuid, 0x2,
  191. 1 << DEVICE_LABEL_DSM);
  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 = container_of(kobj, struct device, 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 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. }