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