dell-smbios.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Common functions for kernel modules using Dell SMBIOS
  3. *
  4. * Copyright (c) Red Hat <mjg@redhat.com>
  5. * Copyright (c) 2014 Gabriele Mazzotta <gabriele.mzt@gmail.com>
  6. * Copyright (c) 2014 Pali Rohár <pali.rohar@gmail.com>
  7. *
  8. * Based on documentation in the libsmbios package:
  9. * Copyright (C) 2005-2014 Dell Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/dmi.h>
  18. #include <linux/err.h>
  19. #include <linux/gfp.h>
  20. #include <linux/mutex.h>
  21. #include <linux/slab.h>
  22. #include <linux/io.h>
  23. #include "../../firmware/dcdbas.h"
  24. #include "dell-smbios.h"
  25. struct calling_interface_structure {
  26. struct dmi_header header;
  27. u16 cmdIOAddress;
  28. u8 cmdIOCode;
  29. u32 supportedCmds;
  30. struct calling_interface_token tokens[];
  31. } __packed;
  32. static struct calling_interface_buffer *buffer;
  33. static DEFINE_MUTEX(buffer_mutex);
  34. static int da_command_address;
  35. static int da_command_code;
  36. static int da_num_tokens;
  37. static struct calling_interface_token *da_tokens;
  38. int dell_smbios_error(int value)
  39. {
  40. switch (value) {
  41. case 0: /* Completed successfully */
  42. return 0;
  43. case -1: /* Completed with error */
  44. return -EIO;
  45. case -2: /* Function not supported */
  46. return -ENXIO;
  47. default: /* Unknown error */
  48. return -EINVAL;
  49. }
  50. }
  51. EXPORT_SYMBOL_GPL(dell_smbios_error);
  52. struct calling_interface_buffer *dell_smbios_get_buffer(void)
  53. {
  54. mutex_lock(&buffer_mutex);
  55. dell_smbios_clear_buffer();
  56. return buffer;
  57. }
  58. EXPORT_SYMBOL_GPL(dell_smbios_get_buffer);
  59. void dell_smbios_clear_buffer(void)
  60. {
  61. memset(buffer, 0, sizeof(struct calling_interface_buffer));
  62. }
  63. EXPORT_SYMBOL_GPL(dell_smbios_clear_buffer);
  64. void dell_smbios_release_buffer(void)
  65. {
  66. mutex_unlock(&buffer_mutex);
  67. }
  68. EXPORT_SYMBOL_GPL(dell_smbios_release_buffer);
  69. void dell_smbios_send_request(int class, int select)
  70. {
  71. struct smi_cmd command;
  72. command.magic = SMI_CMD_MAGIC;
  73. command.command_address = da_command_address;
  74. command.command_code = da_command_code;
  75. command.ebx = virt_to_phys(buffer);
  76. command.ecx = 0x42534931;
  77. buffer->class = class;
  78. buffer->select = select;
  79. dcdbas_smi_request(&command);
  80. }
  81. EXPORT_SYMBOL_GPL(dell_smbios_send_request);
  82. struct calling_interface_token *dell_smbios_find_token(int tokenid)
  83. {
  84. int i;
  85. for (i = 0; i < da_num_tokens; i++) {
  86. if (da_tokens[i].tokenID == tokenid)
  87. return &da_tokens[i];
  88. }
  89. return NULL;
  90. }
  91. EXPORT_SYMBOL_GPL(dell_smbios_find_token);
  92. static void __init parse_da_table(const struct dmi_header *dm)
  93. {
  94. /* Final token is a terminator, so we don't want to copy it */
  95. int tokens = (dm->length-11)/sizeof(struct calling_interface_token)-1;
  96. struct calling_interface_token *new_da_tokens;
  97. struct calling_interface_structure *table =
  98. container_of(dm, struct calling_interface_structure, header);
  99. /* 4 bytes of table header, plus 7 bytes of Dell header, plus at least
  100. 6 bytes of entry */
  101. if (dm->length < 17)
  102. return;
  103. da_command_address = table->cmdIOAddress;
  104. da_command_code = table->cmdIOCode;
  105. new_da_tokens = krealloc(da_tokens, (da_num_tokens + tokens) *
  106. sizeof(struct calling_interface_token),
  107. GFP_KERNEL);
  108. if (!new_da_tokens)
  109. return;
  110. da_tokens = new_da_tokens;
  111. memcpy(da_tokens+da_num_tokens, table->tokens,
  112. sizeof(struct calling_interface_token) * tokens);
  113. da_num_tokens += tokens;
  114. }
  115. static void __init find_tokens(const struct dmi_header *dm, void *dummy)
  116. {
  117. switch (dm->type) {
  118. case 0xd4: /* Indexed IO */
  119. case 0xd5: /* Protected Area Type 1 */
  120. case 0xd6: /* Protected Area Type 2 */
  121. break;
  122. case 0xda: /* Calling interface */
  123. parse_da_table(dm);
  124. break;
  125. }
  126. }
  127. static int __init dell_smbios_init(void)
  128. {
  129. int ret;
  130. dmi_walk(find_tokens, NULL);
  131. if (!da_tokens) {
  132. pr_info("Unable to find dmi tokens\n");
  133. return -ENODEV;
  134. }
  135. /*
  136. * Allocate buffer below 4GB for SMI data--only 32-bit physical addr
  137. * is passed to SMI handler.
  138. */
  139. buffer = (void *)__get_free_page(GFP_KERNEL | GFP_DMA32);
  140. if (!buffer) {
  141. ret = -ENOMEM;
  142. goto fail_buffer;
  143. }
  144. return 0;
  145. fail_buffer:
  146. kfree(da_tokens);
  147. return ret;
  148. }
  149. static void __exit dell_smbios_exit(void)
  150. {
  151. kfree(da_tokens);
  152. free_page((unsigned long)buffer);
  153. }
  154. subsys_initcall(dell_smbios_init);
  155. module_exit(dell_smbios_exit);
  156. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  157. MODULE_AUTHOR("Gabriele Mazzotta <gabriele.mzt@gmail.com>");
  158. MODULE_AUTHOR("Pali Rohár <pali.rohar@gmail.com>");
  159. MODULE_DESCRIPTION("Common functions for kernel modules using Dell SMBIOS");
  160. MODULE_LICENSE("GPL");