pci-label.c 7.2 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/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->string.pointer,
  142. obj->string.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. /*
  165. * The second string element is optional even when
  166. * this _DSM is implemented; when not implemented,
  167. * this entry must return a null string.
  168. */
  169. if (attr == ACPI_ATTR_INDEX_SHOW)
  170. scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value);
  171. else if (attr == ACPI_ATTR_LABEL_SHOW)
  172. dsm_label_utf16s_to_utf8s(tmp + 1, buf);
  173. len = strlen(buf) > 0 ? strlen(buf) : -1;
  174. }
  175. ACPI_FREE(obj);
  176. return len;
  177. }
  178. static bool device_has_dsm(struct device *dev)
  179. {
  180. acpi_handle handle;
  181. handle = ACPI_HANDLE(dev);
  182. if (!handle)
  183. return false;
  184. return !!acpi_check_dsm(handle, device_label_dsm_uuid, 0x2,
  185. 1 << DEVICE_LABEL_DSM);
  186. }
  187. static umode_t acpi_index_string_exist(struct kobject *kobj,
  188. struct attribute *attr, int n)
  189. {
  190. struct device *dev;
  191. dev = container_of(kobj, struct device, kobj);
  192. if (device_has_dsm(dev))
  193. return S_IRUGO;
  194. return 0;
  195. }
  196. static ssize_t acpilabel_show(struct device *dev,
  197. struct device_attribute *attr, char *buf)
  198. {
  199. return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW);
  200. }
  201. static ssize_t acpiindex_show(struct device *dev,
  202. struct device_attribute *attr, char *buf)
  203. {
  204. return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW);
  205. }
  206. static struct device_attribute acpi_attr_label = {
  207. .attr = {.name = "label", .mode = 0444},
  208. .show = acpilabel_show,
  209. };
  210. static struct device_attribute acpi_attr_index = {
  211. .attr = {.name = "acpi_index", .mode = 0444},
  212. .show = acpiindex_show,
  213. };
  214. static struct attribute *acpi_attributes[] = {
  215. &acpi_attr_label.attr,
  216. &acpi_attr_index.attr,
  217. NULL,
  218. };
  219. static struct attribute_group acpi_attr_group = {
  220. .attrs = acpi_attributes,
  221. .is_visible = acpi_index_string_exist,
  222. };
  223. static int pci_create_acpi_index_label_files(struct pci_dev *pdev)
  224. {
  225. return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
  226. }
  227. static int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
  228. {
  229. sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
  230. return 0;
  231. }
  232. #else
  233. static inline int pci_create_acpi_index_label_files(struct pci_dev *pdev)
  234. {
  235. return -1;
  236. }
  237. static inline int pci_remove_acpi_index_label_files(struct pci_dev *pdev)
  238. {
  239. return -1;
  240. }
  241. static inline bool device_has_dsm(struct device *dev)
  242. {
  243. return false;
  244. }
  245. #endif
  246. void pci_create_firmware_label_files(struct pci_dev *pdev)
  247. {
  248. if (device_has_dsm(&pdev->dev))
  249. pci_create_acpi_index_label_files(pdev);
  250. else
  251. pci_create_smbiosname_file(pdev);
  252. }
  253. void pci_remove_firmware_label_files(struct pci_dev *pdev)
  254. {
  255. if (device_has_dsm(&pdev->dev))
  256. pci_remove_acpi_index_label_files(pdev);
  257. else
  258. pci_remove_smbiosname_file(pdev);
  259. }