acpi_ipmi.c 15 KB

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