ipmi_dmi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * A hack to create a platform device from a DMI entry. This will
  3. * allow autoloading of the IPMI drive based on SMBIOS entries.
  4. */
  5. #include <linux/ipmi.h>
  6. #include <linux/init.h>
  7. #include <linux/dmi.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/property.h>
  10. #include "ipmi_dmi.h"
  11. struct ipmi_dmi_info {
  12. int type;
  13. u32 flags;
  14. unsigned long addr;
  15. u8 slave_addr;
  16. struct ipmi_dmi_info *next;
  17. };
  18. static struct ipmi_dmi_info *ipmi_dmi_infos;
  19. static int ipmi_dmi_nr __initdata;
  20. static void __init dmi_add_platform_ipmi(unsigned long base_addr,
  21. u32 flags,
  22. u8 slave_addr,
  23. int irq,
  24. int offset,
  25. int type)
  26. {
  27. struct platform_device *pdev;
  28. struct resource r[4];
  29. unsigned int num_r = 1, size;
  30. struct property_entry p[4] = {
  31. PROPERTY_ENTRY_U8("slave-addr", slave_addr),
  32. PROPERTY_ENTRY_U8("ipmi-type", type),
  33. PROPERTY_ENTRY_U16("i2c-addr", base_addr),
  34. { }
  35. };
  36. char *name, *override;
  37. int rv;
  38. struct ipmi_dmi_info *info;
  39. info = kmalloc(sizeof(*info), GFP_KERNEL);
  40. if (!info) {
  41. pr_warn("ipmi:dmi: Could not allocate dmi info\n");
  42. } else {
  43. info->type = type;
  44. info->flags = flags;
  45. info->addr = base_addr;
  46. info->slave_addr = slave_addr;
  47. info->next = ipmi_dmi_infos;
  48. ipmi_dmi_infos = info;
  49. }
  50. name = "dmi-ipmi-si";
  51. override = "ipmi_si";
  52. switch (type) {
  53. case IPMI_DMI_TYPE_SSIF:
  54. name = "dmi-ipmi-ssif";
  55. override = "ipmi_ssif";
  56. offset = 1;
  57. size = 1;
  58. break;
  59. case IPMI_DMI_TYPE_BT:
  60. size = 3;
  61. break;
  62. case IPMI_DMI_TYPE_KCS:
  63. case IPMI_DMI_TYPE_SMIC:
  64. size = 2;
  65. break;
  66. default:
  67. pr_err("ipmi:dmi: Invalid IPMI type: %d", type);
  68. return;
  69. }
  70. pdev = platform_device_alloc(name, ipmi_dmi_nr);
  71. if (!pdev) {
  72. pr_err("ipmi:dmi: Error allocation IPMI platform device");
  73. return;
  74. }
  75. pdev->driver_override = override;
  76. if (type == IPMI_DMI_TYPE_SSIF)
  77. goto add_properties;
  78. memset(r, 0, sizeof(r));
  79. r[0].start = base_addr;
  80. r[0].end = r[0].start + offset - 1;
  81. r[0].name = "IPMI Address 1";
  82. r[0].flags = flags;
  83. if (size > 1) {
  84. r[1].start = r[0].start + offset;
  85. r[1].end = r[1].start + offset - 1;
  86. r[1].name = "IPMI Address 2";
  87. r[1].flags = flags;
  88. num_r++;
  89. }
  90. if (size > 2) {
  91. r[2].start = r[1].start + offset;
  92. r[2].end = r[2].start + offset - 1;
  93. r[2].name = "IPMI Address 3";
  94. r[2].flags = flags;
  95. num_r++;
  96. }
  97. if (irq) {
  98. r[num_r].start = irq;
  99. r[num_r].end = irq;
  100. r[num_r].name = "IPMI IRQ";
  101. r[num_r].flags = IORESOURCE_IRQ;
  102. num_r++;
  103. }
  104. rv = platform_device_add_resources(pdev, r, num_r);
  105. if (rv) {
  106. dev_err(&pdev->dev,
  107. "ipmi:dmi: Unable to add resources: %d\n", rv);
  108. goto err;
  109. }
  110. add_properties:
  111. rv = platform_device_add_properties(pdev, p);
  112. if (rv) {
  113. dev_err(&pdev->dev,
  114. "ipmi:dmi: Unable to add properties: %d\n", rv);
  115. goto err;
  116. }
  117. rv = platform_device_add(pdev);
  118. if (rv) {
  119. dev_err(&pdev->dev, "ipmi:dmi: Unable to add device: %d\n", rv);
  120. goto err;
  121. }
  122. ipmi_dmi_nr++;
  123. return;
  124. err:
  125. platform_device_put(pdev);
  126. }
  127. /*
  128. * Look up the slave address for a given interface. This is here
  129. * because ACPI doesn't have a slave address while SMBIOS does, but we
  130. * prefer using ACPI so the ACPI code can use the IPMI namespace.
  131. * This function allows an ACPI-specified IPMI device to look up the
  132. * slave address from the DMI table.
  133. */
  134. int ipmi_dmi_get_slave_addr(int type, u32 flags, unsigned long base_addr)
  135. {
  136. struct ipmi_dmi_info *info = ipmi_dmi_infos;
  137. while (info) {
  138. if (info->type == type &&
  139. info->flags == flags &&
  140. info->addr == base_addr)
  141. return info->slave_addr;
  142. info = info->next;
  143. }
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
  147. #define DMI_IPMI_MIN_LENGTH 0x10
  148. #define DMI_IPMI_VER2_LENGTH 0x12
  149. #define DMI_IPMI_TYPE 4
  150. #define DMI_IPMI_SLAVEADDR 6
  151. #define DMI_IPMI_ADDR 8
  152. #define DMI_IPMI_ACCESS 0x10
  153. #define DMI_IPMI_IRQ 0x11
  154. #define DMI_IPMI_IO_MASK 0xfffe
  155. static void __init dmi_decode_ipmi(const struct dmi_header *dm)
  156. {
  157. const u8 *data = (const u8 *) dm;
  158. u32 flags = IORESOURCE_IO;
  159. unsigned long base_addr;
  160. u8 len = dm->length;
  161. u8 slave_addr;
  162. int irq = 0, offset;
  163. int type;
  164. if (len < DMI_IPMI_MIN_LENGTH)
  165. return;
  166. type = data[DMI_IPMI_TYPE];
  167. slave_addr = data[DMI_IPMI_SLAVEADDR];
  168. memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
  169. if (len >= DMI_IPMI_VER2_LENGTH) {
  170. if (type == IPMI_DMI_TYPE_SSIF) {
  171. offset = 0;
  172. flags = 0;
  173. base_addr = data[DMI_IPMI_ADDR] >> 1;
  174. if (base_addr == 0) {
  175. /*
  176. * Some broken systems put the I2C address in
  177. * the slave address field. We try to
  178. * accommodate them here.
  179. */
  180. base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
  181. slave_addr = 0;
  182. }
  183. } else {
  184. if (base_addr & 1) {
  185. /* I/O */
  186. base_addr &= DMI_IPMI_IO_MASK;
  187. } else {
  188. /* Memory */
  189. flags = IORESOURCE_MEM;
  190. }
  191. /*
  192. * If bit 4 of byte 0x10 is set, then the lsb
  193. * for the address is odd.
  194. */
  195. base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
  196. irq = data[DMI_IPMI_IRQ];
  197. /*
  198. * The top two bits of byte 0x10 hold the
  199. * register spacing.
  200. */
  201. switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
  202. case 0: /* Byte boundaries */
  203. offset = 1;
  204. break;
  205. case 1: /* 32-bit boundaries */
  206. offset = 4;
  207. break;
  208. case 2: /* 16-byte boundaries */
  209. offset = 16;
  210. break;
  211. default:
  212. pr_err("ipmi:dmi: Invalid offset: 0");
  213. return;
  214. }
  215. }
  216. } else {
  217. /* Old DMI spec. */
  218. /*
  219. * Note that technically, the lower bit of the base
  220. * address should be 1 if the address is I/O and 0 if
  221. * the address is in memory. So many systems get that
  222. * wrong (and all that I have seen are I/O) so we just
  223. * ignore that bit and assume I/O. Systems that use
  224. * memory should use the newer spec, anyway.
  225. */
  226. base_addr = base_addr & DMI_IPMI_IO_MASK;
  227. offset = 1;
  228. }
  229. dmi_add_platform_ipmi(base_addr, flags, slave_addr, irq,
  230. offset, type);
  231. }
  232. static int __init scan_for_dmi_ipmi(void)
  233. {
  234. const struct dmi_device *dev = NULL;
  235. while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
  236. dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
  237. return 0;
  238. }
  239. subsys_initcall(scan_for_dmi_ipmi);