acpi_ipmi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * acpi_ipmi.c - ACPI IPMI opregion
  3. *
  4. * Copyright (C) 2010 Intel Corporation
  5. * Copyright (C) 2010 Zhao Yakui <yakui.zhao@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/delay.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/list.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/io.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/acpi_drivers.h>
  38. #include <linux/ipmi.h>
  39. #include <linux/device.h>
  40. #include <linux/pnp.h>
  41. MODULE_AUTHOR("Zhao Yakui");
  42. MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
  43. MODULE_LICENSE("GPL");
  44. #define IPMI_FLAGS_HANDLER_INSTALL 0
  45. #define ACPI_IPMI_OK 0
  46. #define ACPI_IPMI_TIMEOUT 0x10
  47. #define ACPI_IPMI_UNKNOWN 0x07
  48. /* the IPMI timeout is 5s */
  49. #define IPMI_TIMEOUT (5 * HZ)
  50. struct acpi_ipmi_device {
  51. /* the device list attached to driver_data.ipmi_devices */
  52. struct list_head head;
  53. /* the IPMI request message list */
  54. struct list_head tx_msg_list;
  55. struct mutex tx_msg_lock;
  56. acpi_handle handle;
  57. struct pnp_dev *pnp_dev;
  58. ipmi_user_t user_interface;
  59. int ipmi_ifnum; /* IPMI interface number */
  60. long curr_msgid;
  61. unsigned long flags;
  62. struct ipmi_smi_info smi_data;
  63. };
  64. struct ipmi_driver_data {
  65. struct list_head ipmi_devices;
  66. struct ipmi_smi_watcher bmc_events;
  67. struct ipmi_user_hndl ipmi_hndlrs;
  68. struct mutex ipmi_lock;
  69. };
  70. struct acpi_ipmi_msg {
  71. struct list_head head;
  72. /*
  73. * General speaking the addr type should be SI_ADDR_TYPE. And
  74. * the addr channel should be BMC.
  75. * In fact it can also be IPMB type. But we will have to
  76. * parse it from the Netfn command buffer. It is so complex
  77. * that it is skipped.
  78. */
  79. struct ipmi_addr addr;
  80. long tx_msgid;
  81. /* it is used to track whether the IPMI message is finished */
  82. struct completion tx_complete;
  83. struct kernel_ipmi_msg tx_message;
  84. int msg_done;
  85. /* tx data . And copy it from ACPI object buffer */
  86. u8 tx_data[64];
  87. int tx_len;
  88. u8 rx_data[64];
  89. int rx_len;
  90. struct acpi_ipmi_device *device;
  91. };
  92. /* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
  93. struct acpi_ipmi_buffer {
  94. u8 status;
  95. u8 length;
  96. u8 data[64];
  97. };
  98. static void ipmi_register_bmc(int iface, struct device *dev);
  99. static void ipmi_bmc_gone(int iface);
  100. static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
  101. static void acpi_add_ipmi_device(struct acpi_ipmi_device *ipmi_device);
  102. static void acpi_remove_ipmi_device(struct acpi_ipmi_device *ipmi_device);
  103. static struct ipmi_driver_data driver_data = {
  104. .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
  105. .bmc_events = {
  106. .owner = THIS_MODULE,
  107. .new_smi = ipmi_register_bmc,
  108. .smi_gone = ipmi_bmc_gone,
  109. },
  110. .ipmi_hndlrs = {
  111. .ipmi_recv_hndl = ipmi_msg_handler,
  112. },
  113. };
  114. static struct acpi_ipmi_msg *acpi_alloc_ipmi_msg(struct acpi_ipmi_device *ipmi)
  115. {
  116. struct acpi_ipmi_msg *ipmi_msg;
  117. struct pnp_dev *pnp_dev = ipmi->pnp_dev;
  118. ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
  119. if (!ipmi_msg) {
  120. dev_warn(&pnp_dev->dev, "Can't allocate memory for ipmi_msg\n");
  121. return NULL;
  122. }
  123. init_completion(&ipmi_msg->tx_complete);
  124. INIT_LIST_HEAD(&ipmi_msg->head);
  125. ipmi_msg->device = ipmi;
  126. return ipmi_msg;
  127. }
  128. #define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
  129. #define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
  130. static void acpi_format_ipmi_msg(struct acpi_ipmi_msg *tx_msg,
  131. acpi_physical_address address,
  132. acpi_integer *value)
  133. {
  134. struct kernel_ipmi_msg *msg;
  135. struct acpi_ipmi_buffer *buffer;
  136. struct acpi_ipmi_device *device;
  137. msg = &tx_msg->tx_message;
  138. /*
  139. * IPMI network function and command are encoded in the address
  140. * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
  141. */
  142. msg->netfn = IPMI_OP_RGN_NETFN(address);
  143. msg->cmd = IPMI_OP_RGN_CMD(address);
  144. msg->data = tx_msg->tx_data;
  145. /*
  146. * value is the parameter passed by the IPMI opregion space handler.
  147. * It points to the IPMI request message buffer
  148. */
  149. buffer = (struct acpi_ipmi_buffer *)value;
  150. /* copy the tx message data */
  151. msg->data_len = buffer->length;
  152. memcpy(tx_msg->tx_data, buffer->data, msg->data_len);
  153. /*
  154. * now the default type is SYSTEM_INTERFACE and channel type is BMC.
  155. * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
  156. * the addr type should be changed to IPMB. Then we will have to parse
  157. * the IPMI request message buffer to get the IPMB address.
  158. * If so, please fix me.
  159. */
  160. tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  161. tx_msg->addr.channel = IPMI_BMC_CHANNEL;
  162. tx_msg->addr.data[0] = 0;
  163. /* Get the msgid */
  164. device = tx_msg->device;
  165. mutex_lock(&device->tx_msg_lock);
  166. device->curr_msgid++;
  167. tx_msg->tx_msgid = device->curr_msgid;
  168. mutex_unlock(&device->tx_msg_lock);
  169. }
  170. static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
  171. acpi_integer *value, int rem_time)
  172. {
  173. struct acpi_ipmi_buffer *buffer;
  174. /*
  175. * value is also used as output parameter. It represents the response
  176. * IPMI message returned by IPMI command.
  177. */
  178. buffer = (struct acpi_ipmi_buffer *)value;
  179. if (!rem_time && !msg->msg_done) {
  180. buffer->status = ACPI_IPMI_TIMEOUT;
  181. return;
  182. }
  183. /*
  184. * If the flag of msg_done is not set or the recv length is zero, it
  185. * means that the IPMI command is not executed correctly.
  186. * The status code will be ACPI_IPMI_UNKNOWN.
  187. */
  188. if (!msg->msg_done || !msg->rx_len) {
  189. buffer->status = ACPI_IPMI_UNKNOWN;
  190. return;
  191. }
  192. /*
  193. * If the IPMI response message is obtained correctly, the status code
  194. * will be ACPI_IPMI_OK
  195. */
  196. buffer->status = ACPI_IPMI_OK;
  197. buffer->length = msg->rx_len;
  198. memcpy(buffer->data, msg->rx_data, msg->rx_len);
  199. }
  200. static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
  201. {
  202. struct acpi_ipmi_msg *tx_msg, *temp;
  203. int count = HZ / 10;
  204. struct pnp_dev *pnp_dev = ipmi->pnp_dev;
  205. list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
  206. /* wake up the sleep thread on the Tx msg */
  207. complete(&tx_msg->tx_complete);
  208. }
  209. /* wait for about 100ms to flush the tx message list */
  210. while (count--) {
  211. if (list_empty(&ipmi->tx_msg_list))
  212. break;
  213. schedule_timeout(1);
  214. }
  215. if (!list_empty(&ipmi->tx_msg_list))
  216. dev_warn(&pnp_dev->dev, "tx msg list is not NULL\n");
  217. }
  218. static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
  219. {
  220. struct acpi_ipmi_device *ipmi_device = user_msg_data;
  221. int msg_found = 0;
  222. struct acpi_ipmi_msg *tx_msg;
  223. struct pnp_dev *pnp_dev = ipmi_device->pnp_dev;
  224. if (msg->user != ipmi_device->user_interface) {
  225. dev_warn(&pnp_dev->dev, "Unexpected response is returned. "
  226. "returned user %p, expected user %p\n",
  227. msg->user, ipmi_device->user_interface);
  228. ipmi_free_recv_msg(msg);
  229. return;
  230. }
  231. mutex_lock(&ipmi_device->tx_msg_lock);
  232. list_for_each_entry(tx_msg, &ipmi_device->tx_msg_list, head) {
  233. if (msg->msgid == tx_msg->tx_msgid) {
  234. msg_found = 1;
  235. break;
  236. }
  237. }
  238. mutex_unlock(&ipmi_device->tx_msg_lock);
  239. if (!msg_found) {
  240. dev_warn(&pnp_dev->dev, "Unexpected response (msg id %ld) is "
  241. "returned.\n", msg->msgid);
  242. ipmi_free_recv_msg(msg);
  243. return;
  244. }
  245. if (msg->msg.data_len) {
  246. /* copy the response data to Rx_data buffer */
  247. memcpy(tx_msg->rx_data, msg->msg_data, msg->msg.data_len);
  248. tx_msg->rx_len = msg->msg.data_len;
  249. tx_msg->msg_done = 1;
  250. }
  251. complete(&tx_msg->tx_complete);
  252. ipmi_free_recv_msg(msg);
  253. };
  254. static void ipmi_register_bmc(int iface, struct device *dev)
  255. {
  256. struct acpi_ipmi_device *ipmi_device, *temp;
  257. struct pnp_dev *pnp_dev;
  258. ipmi_user_t user;
  259. int err;
  260. struct ipmi_smi_info smi_data;
  261. acpi_handle handle;
  262. err = ipmi_get_smi_info(iface, &smi_data);
  263. if (err)
  264. return;
  265. if (smi_data.addr_src != SI_ACPI) {
  266. put_device(smi_data.dev);
  267. return;
  268. }
  269. handle = smi_data.addr_info.acpi_info.acpi_handle;
  270. mutex_lock(&driver_data.ipmi_lock);
  271. list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
  272. /*
  273. * if the corresponding ACPI handle is already added
  274. * to the device list, don't add it again.
  275. */
  276. if (temp->handle == handle)
  277. goto out;
  278. }
  279. ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
  280. if (!ipmi_device)
  281. goto out;
  282. pnp_dev = to_pnp_dev(smi_data.dev);
  283. ipmi_device->handle = handle;
  284. ipmi_device->pnp_dev = pnp_dev;
  285. err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
  286. ipmi_device, &user);
  287. if (err) {
  288. dev_warn(&pnp_dev->dev, "Can't create IPMI user interface\n");
  289. kfree(ipmi_device);
  290. goto out;
  291. }
  292. acpi_add_ipmi_device(ipmi_device);
  293. ipmi_device->user_interface = user;
  294. ipmi_device->ipmi_ifnum = iface;
  295. mutex_unlock(&driver_data.ipmi_lock);
  296. memcpy(&ipmi_device->smi_data, &smi_data, sizeof(struct ipmi_smi_info));
  297. return;
  298. out:
  299. mutex_unlock(&driver_data.ipmi_lock);
  300. put_device(smi_data.dev);
  301. return;
  302. }
  303. static void ipmi_bmc_gone(int iface)
  304. {
  305. struct acpi_ipmi_device *ipmi_device, *temp;
  306. mutex_lock(&driver_data.ipmi_lock);
  307. list_for_each_entry_safe(ipmi_device, temp,
  308. &driver_data.ipmi_devices, head) {
  309. if (ipmi_device->ipmi_ifnum != iface)
  310. continue;
  311. acpi_remove_ipmi_device(ipmi_device);
  312. put_device(ipmi_device->smi_data.dev);
  313. kfree(ipmi_device);
  314. break;
  315. }
  316. mutex_unlock(&driver_data.ipmi_lock);
  317. }
  318. /* --------------------------------------------------------------------------
  319. * Address Space Management
  320. * -------------------------------------------------------------------------- */
  321. /*
  322. * This is the IPMI opregion space handler.
  323. * @function: indicates the read/write. In fact as the IPMI message is driven
  324. * by command, only write is meaningful.
  325. * @address: This contains the netfn/command of IPMI request message.
  326. * @bits : not used.
  327. * @value : it is an in/out parameter. It points to the IPMI message buffer.
  328. * Before the IPMI message is sent, it represents the actual request
  329. * IPMI message. After the IPMI message is finished, it represents
  330. * the response IPMI message returned by IPMI command.
  331. * @handler_context: IPMI device context.
  332. */
  333. static acpi_status
  334. acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
  335. u32 bits, acpi_integer *value,
  336. void *handler_context, void *region_context)
  337. {
  338. struct acpi_ipmi_msg *tx_msg;
  339. struct acpi_ipmi_device *ipmi_device = handler_context;
  340. int err, rem_time;
  341. acpi_status status;
  342. /*
  343. * IPMI opregion message.
  344. * IPMI message is firstly written to the BMC and system software
  345. * can get the respsonse. So it is unmeaningful for the read access
  346. * of IPMI opregion.
  347. */
  348. if ((function & ACPI_IO_MASK) == ACPI_READ)
  349. return AE_TYPE;
  350. if (!ipmi_device->user_interface)
  351. return AE_NOT_EXIST;
  352. tx_msg = acpi_alloc_ipmi_msg(ipmi_device);
  353. if (!tx_msg)
  354. return AE_NO_MEMORY;
  355. acpi_format_ipmi_msg(tx_msg, address, value);
  356. mutex_lock(&ipmi_device->tx_msg_lock);
  357. list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
  358. mutex_unlock(&ipmi_device->tx_msg_lock);
  359. err = ipmi_request_settime(ipmi_device->user_interface,
  360. &tx_msg->addr,
  361. tx_msg->tx_msgid,
  362. &tx_msg->tx_message,
  363. NULL, 0, 0, 0);
  364. if (err) {
  365. status = AE_ERROR;
  366. goto end_label;
  367. }
  368. rem_time = wait_for_completion_timeout(&tx_msg->tx_complete,
  369. IPMI_TIMEOUT);
  370. acpi_format_ipmi_response(tx_msg, value, rem_time);
  371. status = AE_OK;
  372. end_label:
  373. mutex_lock(&ipmi_device->tx_msg_lock);
  374. list_del(&tx_msg->head);
  375. mutex_unlock(&ipmi_device->tx_msg_lock);
  376. kfree(tx_msg);
  377. return status;
  378. }
  379. static void ipmi_remove_space_handler(struct acpi_ipmi_device *ipmi)
  380. {
  381. if (!test_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags))
  382. return;
  383. acpi_remove_address_space_handler(ipmi->handle,
  384. ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler);
  385. clear_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags);
  386. }
  387. static int ipmi_install_space_handler(struct acpi_ipmi_device *ipmi)
  388. {
  389. acpi_status status;
  390. if (test_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags))
  391. return 0;
  392. status = acpi_install_address_space_handler(ipmi->handle,
  393. ACPI_ADR_SPACE_IPMI,
  394. &acpi_ipmi_space_handler,
  395. NULL, ipmi);
  396. if (ACPI_FAILURE(status)) {
  397. struct pnp_dev *pnp_dev = ipmi->pnp_dev;
  398. dev_warn(&pnp_dev->dev, "Can't register IPMI opregion space "
  399. "handle\n");
  400. return -EINVAL;
  401. }
  402. set_bit(IPMI_FLAGS_HANDLER_INSTALL, &ipmi->flags);
  403. return 0;
  404. }
  405. static void acpi_add_ipmi_device(struct acpi_ipmi_device *ipmi_device)
  406. {
  407. INIT_LIST_HEAD(&ipmi_device->head);
  408. mutex_init(&ipmi_device->tx_msg_lock);
  409. INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
  410. ipmi_install_space_handler(ipmi_device);
  411. list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
  412. }
  413. static void acpi_remove_ipmi_device(struct acpi_ipmi_device *ipmi_device)
  414. {
  415. /*
  416. * If the IPMI user interface is created, it should be
  417. * destroyed.
  418. */
  419. if (ipmi_device->user_interface) {
  420. ipmi_destroy_user(ipmi_device->user_interface);
  421. ipmi_device->user_interface = NULL;
  422. }
  423. /* flush the Tx_msg list */
  424. if (!list_empty(&ipmi_device->tx_msg_list))
  425. ipmi_flush_tx_msg(ipmi_device);
  426. list_del(&ipmi_device->head);
  427. ipmi_remove_space_handler(ipmi_device);
  428. }
  429. static int __init acpi_ipmi_init(void)
  430. {
  431. int result = 0;
  432. if (acpi_disabled)
  433. return result;
  434. mutex_init(&driver_data.ipmi_lock);
  435. result = ipmi_smi_watcher_register(&driver_data.bmc_events);
  436. return result;
  437. }
  438. static void __exit acpi_ipmi_exit(void)
  439. {
  440. struct acpi_ipmi_device *ipmi_device, *temp;
  441. if (acpi_disabled)
  442. return;
  443. ipmi_smi_watcher_unregister(&driver_data.bmc_events);
  444. /*
  445. * When one smi_watcher is unregistered, it is only deleted
  446. * from the smi_watcher list. But the smi_gone callback function
  447. * is not called. So explicitly uninstall the ACPI IPMI oregion
  448. * handler and free it.
  449. */
  450. mutex_lock(&driver_data.ipmi_lock);
  451. list_for_each_entry_safe(ipmi_device, temp,
  452. &driver_data.ipmi_devices, head) {
  453. acpi_remove_ipmi_device(ipmi_device);
  454. put_device(ipmi_device->smi_data.dev);
  455. kfree(ipmi_device);
  456. }
  457. mutex_unlock(&driver_data.ipmi_lock);
  458. }
  459. module_init(acpi_ipmi_init);
  460. module_exit(acpi_ipmi_exit);