pci-label.c 7.1 KB

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