pci-label.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <acpi/acpi_bus.h>
  32. #include "pci.h"
  33. #define DEVICE_LABEL_DSM 0x07
  34. #ifndef CONFIG_DMI
  35. static inline int
  36. pci_create_smbiosname_file(struct pci_dev *pdev)
  37. {
  38. return -1;
  39. }
  40. static inline void
  41. pci_remove_smbiosname_file(struct pci_dev *pdev)
  42. {
  43. }
  44. #else
  45. enum smbios_attr_enum {
  46. SMBIOS_ATTR_NONE = 0,
  47. SMBIOS_ATTR_LABEL_SHOW,
  48. SMBIOS_ATTR_INSTANCE_SHOW,
  49. };
  50. static mode_t
  51. find_smbios_instance_string(struct pci_dev *pdev, char *buf,
  52. enum smbios_attr_enum attribute)
  53. {
  54. const struct dmi_device *dmi;
  55. struct dmi_dev_onboard *donboard;
  56. int bus;
  57. int devfn;
  58. bus = pdev->bus->number;
  59. devfn = pdev->devfn;
  60. dmi = NULL;
  61. while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD,
  62. NULL, dmi)) != NULL) {
  63. donboard = dmi->device_data;
  64. if (donboard && donboard->bus == bus &&
  65. donboard->devfn == devfn) {
  66. if (buf) {
  67. if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
  68. return scnprintf(buf, PAGE_SIZE,
  69. "%d\n",
  70. donboard->instance);
  71. else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
  72. return scnprintf(buf, PAGE_SIZE,
  73. "%s\n",
  74. dmi->name);
  75. }
  76. return strlen(dmi->name);
  77. }
  78. }
  79. return 0;
  80. }
  81. static mode_t
  82. smbios_instance_string_exist(struct kobject *kobj, struct attribute *attr,
  83. int n)
  84. {
  85. struct device *dev;
  86. struct pci_dev *pdev;
  87. dev = container_of(kobj, struct device, kobj);
  88. pdev = to_pci_dev(dev);
  89. return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ?
  90. S_IRUGO : 0;
  91. }
  92. static ssize_t
  93. smbioslabel_show(struct device *dev, struct device_attribute *attr, char *buf)
  94. {
  95. struct pci_dev *pdev;
  96. pdev = to_pci_dev(dev);
  97. return find_smbios_instance_string(pdev, buf,
  98. SMBIOS_ATTR_LABEL_SHOW);
  99. }
  100. static ssize_t
  101. smbiosinstance_show(struct device *dev,
  102. struct device_attribute *attr, char *buf)
  103. {
  104. struct pci_dev *pdev;
  105. pdev = to_pci_dev(dev);
  106. return find_smbios_instance_string(pdev, buf,
  107. SMBIOS_ATTR_INSTANCE_SHOW);
  108. }
  109. static struct device_attribute smbios_attr_label = {
  110. .attr = {.name = "label", .mode = 0444},
  111. .show = smbioslabel_show,
  112. };
  113. static struct device_attribute smbios_attr_instance = {
  114. .attr = {.name = "index", .mode = 0444},
  115. .show = smbiosinstance_show,
  116. };
  117. static struct attribute *smbios_attributes[] = {
  118. &smbios_attr_label.attr,
  119. &smbios_attr_instance.attr,
  120. NULL,
  121. };
  122. static struct attribute_group smbios_attr_group = {
  123. .attrs = smbios_attributes,
  124. .is_visible = smbios_instance_string_exist,
  125. };
  126. static int
  127. pci_create_smbiosname_file(struct pci_dev *pdev)
  128. {
  129. return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group);
  130. }
  131. static void
  132. pci_remove_smbiosname_file(struct pci_dev *pdev)
  133. {
  134. sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group);
  135. }
  136. #endif
  137. #ifndef CONFIG_ACPI
  138. static inline int
  139. pci_create_acpi_index_label_files(struct pci_dev *pdev)
  140. {
  141. return -1;
  142. }
  143. static inline int
  144. pci_remove_acpi_index_label_files(struct pci_dev *pdev)
  145. {
  146. return -1;
  147. }
  148. static inline bool
  149. device_has_dsm(struct device *dev)
  150. {
  151. return false;
  152. }
  153. #else
  154. static const char device_label_dsm_uuid[] = {
  155. 0xD0, 0x37, 0xC9, 0xE5, 0x53, 0x35, 0x7A, 0x4D,
  156. 0x91, 0x17, 0xEA, 0x4D, 0x19, 0xC3, 0x43, 0x4D
  157. };
  158. enum acpi_attr_enum {
  159. ACPI_ATTR_NONE = 0,
  160. ACPI_ATTR_LABEL_SHOW,
  161. ACPI_ATTR_INDEX_SHOW,
  162. };
  163. static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf)
  164. {
  165. int len;
  166. len = utf16s_to_utf8s((const wchar_t *)obj->
  167. package.elements[1].string.pointer,
  168. obj->package.elements[1].string.length,
  169. UTF16_LITTLE_ENDIAN,
  170. buf, PAGE_SIZE);
  171. buf[len] = '\n';
  172. }
  173. static int
  174. dsm_get_label(acpi_handle handle, int func,
  175. struct acpi_buffer *output,
  176. char *buf, enum acpi_attr_enum attribute)
  177. {
  178. struct acpi_object_list input;
  179. union acpi_object params[4];
  180. union acpi_object *obj;
  181. int len = 0;
  182. int err;
  183. input.count = 4;
  184. input.pointer = params;
  185. params[0].type = ACPI_TYPE_BUFFER;
  186. params[0].buffer.length = sizeof(device_label_dsm_uuid);
  187. params[0].buffer.pointer = (char *)device_label_dsm_uuid;
  188. params[1].type = ACPI_TYPE_INTEGER;
  189. params[1].integer.value = 0x02;
  190. params[2].type = ACPI_TYPE_INTEGER;
  191. params[2].integer.value = func;
  192. params[3].type = ACPI_TYPE_PACKAGE;
  193. params[3].package.count = 0;
  194. params[3].package.elements = NULL;
  195. err = acpi_evaluate_object(handle, "_DSM", &input, output);
  196. if (err)
  197. return -1;
  198. obj = (union acpi_object *)output->pointer;
  199. switch (obj->type) {
  200. case ACPI_TYPE_PACKAGE:
  201. if (obj->package.count != 2)
  202. break;
  203. len = obj->package.elements[0].integer.value;
  204. if (buf) {
  205. if (attribute == ACPI_ATTR_INDEX_SHOW)
  206. scnprintf(buf, PAGE_SIZE, "%llu\n",
  207. obj->package.elements[0].integer.value);
  208. else if (attribute == ACPI_ATTR_LABEL_SHOW)
  209. dsm_label_utf16s_to_utf8s(obj, buf);
  210. kfree(output->pointer);
  211. return strlen(buf);
  212. }
  213. kfree(output->pointer);
  214. return len;
  215. break;
  216. default:
  217. kfree(output->pointer);
  218. }
  219. return -1;
  220. }
  221. static bool
  222. device_has_dsm(struct device *dev)
  223. {
  224. acpi_handle handle;
  225. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  226. handle = DEVICE_ACPI_HANDLE(dev);
  227. if (!handle)
  228. return FALSE;
  229. if (dsm_get_label(handle, DEVICE_LABEL_DSM, &output, NULL,
  230. ACPI_ATTR_NONE) > 0)
  231. return TRUE;
  232. return FALSE;
  233. }
  234. static mode_t
  235. acpi_index_string_exist(struct kobject *kobj, struct attribute *attr, int n)
  236. {
  237. struct device *dev;
  238. dev = container_of(kobj, struct device, kobj);
  239. if (device_has_dsm(dev))
  240. return S_IRUGO;
  241. return 0;
  242. }
  243. static ssize_t
  244. acpilabel_show(struct device *dev, struct device_attribute *attr, char *buf)
  245. {
  246. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  247. acpi_handle handle;
  248. int length;
  249. handle = DEVICE_ACPI_HANDLE(dev);
  250. if (!handle)
  251. return -1;
  252. length = dsm_get_label(handle, DEVICE_LABEL_DSM,
  253. &output, buf, ACPI_ATTR_LABEL_SHOW);
  254. if (length < 1)
  255. return -1;
  256. return length;
  257. }
  258. static ssize_t
  259. acpiindex_show(struct device *dev, struct device_attribute *attr, char *buf)
  260. {
  261. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  262. acpi_handle handle;
  263. int length;
  264. handle = DEVICE_ACPI_HANDLE(dev);
  265. if (!handle)
  266. return -1;
  267. length = dsm_get_label(handle, DEVICE_LABEL_DSM,
  268. &output, buf, ACPI_ATTR_INDEX_SHOW);
  269. if (length < 0)
  270. return -1;
  271. return length;
  272. }
  273. static struct device_attribute acpi_attr_label = {
  274. .attr = {.name = "label", .mode = 0444},
  275. .show = acpilabel_show,
  276. };
  277. static struct device_attribute acpi_attr_index = {
  278. .attr = {.name = "acpi_index", .mode = 0444},
  279. .show = acpiindex_show,
  280. };
  281. static struct attribute *acpi_attributes[] = {
  282. &acpi_attr_label.attr,
  283. &acpi_attr_index.attr,
  284. NULL,
  285. };
  286. static struct attribute_group acpi_attr_group = {
  287. .attrs = acpi_attributes,
  288. .is_visible = acpi_index_string_exist,
  289. };
  290. static int
  291. pci_create_acpi_index_label_files(struct pci_dev *pdev)
  292. {
  293. return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group);
  294. }
  295. static int
  296. pci_remove_acpi_index_label_files(struct pci_dev *pdev)
  297. {
  298. sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group);
  299. return 0;
  300. }
  301. #endif
  302. void pci_create_firmware_label_files(struct pci_dev *pdev)
  303. {
  304. if (device_has_dsm(&pdev->dev))
  305. pci_create_acpi_index_label_files(pdev);
  306. else
  307. pci_create_smbiosname_file(pdev);
  308. }
  309. void pci_remove_firmware_label_files(struct pci_dev *pdev)
  310. {
  311. if (device_has_dsm(&pdev->dev))
  312. pci_remove_acpi_index_label_files(pdev);
  313. else
  314. pci_remove_smbiosname_file(pdev);
  315. }