acpi_ipmi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * acpi_ipmi.c - ACPI IPMI opregion
  3. *
  4. * Copyright (C) 2010, 2013 Intel Corporation
  5. * Author: Zhao Yakui <yakui.zhao@intel.com>
  6. * Lv Zheng <lv.zheng@intel.com>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/types.h>
  30. #include <linux/delay.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/list.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/io.h>
  37. #include <acpi/acpi_bus.h>
  38. #include <acpi/acpi_drivers.h>
  39. #include <linux/ipmi.h>
  40. #include <linux/device.h>
  41. #include <linux/pnp.h>
  42. #include <linux/spinlock.h>
  43. MODULE_AUTHOR("Zhao Yakui");
  44. MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
  45. MODULE_LICENSE("GPL");
  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 (5000)
  51. #define ACPI_IPMI_MAX_MSG_LENGTH 64
  52. struct acpi_ipmi_device {
  53. /* the device list attached to driver_data.ipmi_devices */
  54. struct list_head head;
  55. /* the IPMI request message list */
  56. struct list_head tx_msg_list;
  57. spinlock_t tx_msg_lock;
  58. acpi_handle handle;
  59. struct device *dev;
  60. ipmi_user_t user_interface;
  61. int ipmi_ifnum; /* IPMI interface number */
  62. long curr_msgid;
  63. bool dead;
  64. struct kref kref;
  65. };
  66. struct ipmi_driver_data {
  67. struct list_head ipmi_devices;
  68. struct ipmi_smi_watcher bmc_events;
  69. struct ipmi_user_hndl ipmi_hndlrs;
  70. struct mutex ipmi_lock;
  71. /*
  72. * NOTE: IPMI System Interface Selection
  73. * There is no system interface specified by the IPMI operation
  74. * region access. We try to select one system interface with ACPI
  75. * handle set. IPMI messages passed from the ACPI codes are sent
  76. * to this selected global IPMI system interface.
  77. */
  78. struct acpi_ipmi_device *selected_smi;
  79. };
  80. struct acpi_ipmi_msg {
  81. struct list_head head;
  82. /*
  83. * General speaking the addr type should be SI_ADDR_TYPE. And
  84. * the addr channel should be BMC.
  85. * In fact it can also be IPMB type. But we will have to
  86. * parse it from the Netfn command buffer. It is so complex
  87. * that it is skipped.
  88. */
  89. struct ipmi_addr addr;
  90. long tx_msgid;
  91. /* it is used to track whether the IPMI message is finished */
  92. struct completion tx_complete;
  93. struct kernel_ipmi_msg tx_message;
  94. int msg_done;
  95. /* tx/rx data . And copy it from/to ACPI object buffer */
  96. u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
  97. u8 rx_len;
  98. struct acpi_ipmi_device *device;
  99. struct kref kref;
  100. };
  101. /* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
  102. struct acpi_ipmi_buffer {
  103. u8 status;
  104. u8 length;
  105. u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
  106. };
  107. static void ipmi_register_bmc(int iface, struct device *dev);
  108. static void ipmi_bmc_gone(int iface);
  109. static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
  110. static struct ipmi_driver_data driver_data = {
  111. .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
  112. .bmc_events = {
  113. .owner = THIS_MODULE,
  114. .new_smi = ipmi_register_bmc,
  115. .smi_gone = ipmi_bmc_gone,
  116. },
  117. .ipmi_hndlrs = {
  118. .ipmi_recv_hndl = ipmi_msg_handler,
  119. },
  120. };
  121. static struct acpi_ipmi_device *
  122. ipmi_dev_alloc(int iface, struct device *dev, acpi_handle handle)
  123. {
  124. struct acpi_ipmi_device *ipmi_device;
  125. int err;
  126. ipmi_user_t user;
  127. ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
  128. if (!ipmi_device)
  129. return NULL;
  130. kref_init(&ipmi_device->kref);
  131. INIT_LIST_HEAD(&ipmi_device->head);
  132. INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
  133. spin_lock_init(&ipmi_device->tx_msg_lock);
  134. ipmi_device->handle = handle;
  135. ipmi_device->dev = get_device(dev);
  136. ipmi_device->ipmi_ifnum = iface;
  137. err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
  138. ipmi_device, &user);
  139. if (err) {
  140. put_device(dev);
  141. kfree(ipmi_device);
  142. return NULL;
  143. }
  144. ipmi_device->user_interface = user;
  145. return ipmi_device;
  146. }
  147. static void ipmi_dev_release(struct acpi_ipmi_device *ipmi_device)
  148. {
  149. ipmi_destroy_user(ipmi_device->user_interface);
  150. put_device(ipmi_device->dev);
  151. kfree(ipmi_device);
  152. }
  153. static void ipmi_dev_release_kref(struct kref *kref)
  154. {
  155. struct acpi_ipmi_device *ipmi =
  156. container_of(kref, struct acpi_ipmi_device, kref);
  157. ipmi_dev_release(ipmi);
  158. }
  159. static void __ipmi_dev_kill(struct acpi_ipmi_device *ipmi_device)
  160. {
  161. list_del(&ipmi_device->head);
  162. if (driver_data.selected_smi == ipmi_device)
  163. driver_data.selected_smi = NULL;
  164. /*
  165. * Always setting dead flag after deleting from the list or
  166. * list_for_each_entry() codes must get changed.
  167. */
  168. ipmi_device->dead = true;
  169. }
  170. static struct acpi_ipmi_device *acpi_ipmi_dev_get(void)
  171. {
  172. struct acpi_ipmi_device *ipmi_device = NULL;
  173. mutex_lock(&driver_data.ipmi_lock);
  174. if (driver_data.selected_smi) {
  175. ipmi_device = driver_data.selected_smi;
  176. kref_get(&ipmi_device->kref);
  177. }
  178. mutex_unlock(&driver_data.ipmi_lock);
  179. return ipmi_device;
  180. }
  181. static void acpi_ipmi_dev_put(struct acpi_ipmi_device *ipmi_device)
  182. {
  183. kref_put(&ipmi_device->kref, ipmi_dev_release_kref);
  184. }
  185. static struct acpi_ipmi_msg *ipmi_msg_alloc(void)
  186. {
  187. struct acpi_ipmi_device *ipmi;
  188. struct acpi_ipmi_msg *ipmi_msg;
  189. ipmi = acpi_ipmi_dev_get();
  190. if (!ipmi)
  191. return NULL;
  192. ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
  193. if (!ipmi_msg) {
  194. acpi_ipmi_dev_put(ipmi);
  195. return NULL;
  196. }
  197. kref_init(&ipmi_msg->kref);
  198. init_completion(&ipmi_msg->tx_complete);
  199. INIT_LIST_HEAD(&ipmi_msg->head);
  200. ipmi_msg->device = ipmi;
  201. ipmi_msg->msg_done = ACPI_IPMI_UNKNOWN;
  202. return ipmi_msg;
  203. }
  204. static void ipmi_msg_release(struct acpi_ipmi_msg *tx_msg)
  205. {
  206. acpi_ipmi_dev_put(tx_msg->device);
  207. kfree(tx_msg);
  208. }
  209. static void ipmi_msg_release_kref(struct kref *kref)
  210. {
  211. struct acpi_ipmi_msg *tx_msg =
  212. container_of(kref, struct acpi_ipmi_msg, kref);
  213. ipmi_msg_release(tx_msg);
  214. }
  215. static struct acpi_ipmi_msg *acpi_ipmi_msg_get(struct acpi_ipmi_msg *tx_msg)
  216. {
  217. kref_get(&tx_msg->kref);
  218. return tx_msg;
  219. }
  220. static void acpi_ipmi_msg_put(struct acpi_ipmi_msg *tx_msg)
  221. {
  222. kref_put(&tx_msg->kref, ipmi_msg_release_kref);
  223. }
  224. #define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
  225. #define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
  226. static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
  227. acpi_physical_address address,
  228. acpi_integer *value)
  229. {
  230. struct kernel_ipmi_msg *msg;
  231. struct acpi_ipmi_buffer *buffer;
  232. struct acpi_ipmi_device *device;
  233. unsigned long flags;
  234. msg = &tx_msg->tx_message;
  235. /*
  236. * IPMI network function and command are encoded in the address
  237. * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
  238. */
  239. msg->netfn = IPMI_OP_RGN_NETFN(address);
  240. msg->cmd = IPMI_OP_RGN_CMD(address);
  241. msg->data = tx_msg->data;
  242. /*
  243. * value is the parameter passed by the IPMI opregion space handler.
  244. * It points to the IPMI request message buffer
  245. */
  246. buffer = (struct acpi_ipmi_buffer *)value;
  247. /* copy the tx message data */
  248. if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
  249. dev_WARN_ONCE(tx_msg->device->dev, true,
  250. "Unexpected request (msg len %d).\n",
  251. buffer->length);
  252. return -EINVAL;
  253. }
  254. msg->data_len = buffer->length;
  255. memcpy(tx_msg->data, buffer->data, msg->data_len);
  256. /*
  257. * now the default type is SYSTEM_INTERFACE and channel type is BMC.
  258. * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
  259. * the addr type should be changed to IPMB. Then we will have to parse
  260. * the IPMI request message buffer to get the IPMB address.
  261. * If so, please fix me.
  262. */
  263. tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  264. tx_msg->addr.channel = IPMI_BMC_CHANNEL;
  265. tx_msg->addr.data[0] = 0;
  266. /* Get the msgid */
  267. device = tx_msg->device;
  268. spin_lock_irqsave(&device->tx_msg_lock, flags);
  269. device->curr_msgid++;
  270. tx_msg->tx_msgid = device->curr_msgid;
  271. spin_unlock_irqrestore(&device->tx_msg_lock, flags);
  272. return 0;
  273. }
  274. static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
  275. acpi_integer *value)
  276. {
  277. struct acpi_ipmi_buffer *buffer;
  278. /*
  279. * value is also used as output parameter. It represents the response
  280. * IPMI message returned by IPMI command.
  281. */
  282. buffer = (struct acpi_ipmi_buffer *)value;
  283. /*
  284. * If the flag of msg_done is not set, it means that the IPMI command is
  285. * not executed correctly.
  286. */
  287. buffer->status = msg->msg_done;
  288. if (msg->msg_done != ACPI_IPMI_OK)
  289. return;
  290. /*
  291. * If the IPMI response message is obtained correctly, the status code
  292. * will be ACPI_IPMI_OK
  293. */
  294. buffer->length = msg->rx_len;
  295. memcpy(buffer->data, msg->data, msg->rx_len);
  296. }
  297. static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
  298. {
  299. struct acpi_ipmi_msg *tx_msg;
  300. unsigned long flags;
  301. /*
  302. * NOTE: On-going ipmi_recv_msg
  303. * ipmi_msg_handler() may still be invoked by ipmi_si after
  304. * flushing. But it is safe to do a fast flushing on module_exit()
  305. * without waiting for all ipmi_recv_msg(s) to complete from
  306. * ipmi_msg_handler() as it is ensured by ipmi_si that all
  307. * ipmi_recv_msg(s) are freed after invoking ipmi_destroy_user().
  308. */
  309. spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
  310. while (!list_empty(&ipmi->tx_msg_list)) {
  311. tx_msg = list_first_entry(&ipmi->tx_msg_list,
  312. struct acpi_ipmi_msg,
  313. head);
  314. list_del(&tx_msg->head);
  315. spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
  316. /* wake up the sleep thread on the Tx msg */
  317. complete(&tx_msg->tx_complete);
  318. acpi_ipmi_msg_put(tx_msg);
  319. spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
  320. }
  321. spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
  322. }
  323. static void ipmi_cancel_tx_msg(struct acpi_ipmi_device *ipmi,
  324. struct acpi_ipmi_msg *msg)
  325. {
  326. struct acpi_ipmi_msg *tx_msg, *temp;
  327. bool msg_found = false;
  328. unsigned long flags;
  329. spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
  330. list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
  331. if (msg == tx_msg) {
  332. msg_found = true;
  333. list_del(&tx_msg->head);
  334. break;
  335. }
  336. }
  337. spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
  338. if (msg_found)
  339. acpi_ipmi_msg_put(tx_msg);
  340. }
  341. static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
  342. {
  343. struct acpi_ipmi_device *ipmi_device = user_msg_data;
  344. bool msg_found = false;
  345. struct acpi_ipmi_msg *tx_msg, *temp;
  346. struct device *dev = ipmi_device->dev;
  347. unsigned long flags;
  348. if (msg->user != ipmi_device->user_interface) {
  349. dev_warn(dev, "Unexpected response is returned. "
  350. "returned user %p, expected user %p\n",
  351. msg->user, ipmi_device->user_interface);
  352. goto out_msg;
  353. }
  354. spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
  355. list_for_each_entry_safe(tx_msg, temp, &ipmi_device->tx_msg_list, head) {
  356. if (msg->msgid == tx_msg->tx_msgid) {
  357. msg_found = true;
  358. list_del(&tx_msg->head);
  359. break;
  360. }
  361. }
  362. spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
  363. if (!msg_found) {
  364. dev_warn(dev, "Unexpected response (msg id %ld) is "
  365. "returned.\n", msg->msgid);
  366. goto out_msg;
  367. }
  368. /* copy the response data to Rx_data buffer */
  369. if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
  370. dev_WARN_ONCE(dev, true,
  371. "Unexpected response (msg len %d).\n",
  372. msg->msg.data_len);
  373. goto out_comp;
  374. }
  375. /* response msg is an error msg */
  376. msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  377. if (msg->recv_type == IPMI_RESPONSE_RECV_TYPE &&
  378. msg->msg.data_len == 1) {
  379. if (msg->msg.data[0] == IPMI_TIMEOUT_COMPLETION_CODE) {
  380. dev_WARN_ONCE(dev, true,
  381. "Unexpected response (timeout).\n");
  382. tx_msg->msg_done = ACPI_IPMI_TIMEOUT;
  383. }
  384. goto out_comp;
  385. }
  386. tx_msg->rx_len = msg->msg.data_len;
  387. memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
  388. tx_msg->msg_done = ACPI_IPMI_OK;
  389. out_comp:
  390. complete(&tx_msg->tx_complete);
  391. acpi_ipmi_msg_put(tx_msg);
  392. out_msg:
  393. ipmi_free_recv_msg(msg);
  394. };
  395. static void ipmi_register_bmc(int iface, struct device *dev)
  396. {
  397. struct acpi_ipmi_device *ipmi_device, *temp;
  398. int err;
  399. struct ipmi_smi_info smi_data;
  400. acpi_handle handle;
  401. err = ipmi_get_smi_info(iface, &smi_data);
  402. if (err)
  403. return;
  404. if (smi_data.addr_src != SI_ACPI)
  405. goto err_ref;
  406. handle = smi_data.addr_info.acpi_info.acpi_handle;
  407. if (!handle)
  408. goto err_ref;
  409. ipmi_device = ipmi_dev_alloc(iface, smi_data.dev, handle);
  410. if (!ipmi_device) {
  411. dev_warn(smi_data.dev, "Can't create IPMI user interface\n");
  412. goto err_ref;
  413. }
  414. mutex_lock(&driver_data.ipmi_lock);
  415. list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
  416. /*
  417. * if the corresponding ACPI handle is already added
  418. * to the device list, don't add it again.
  419. */
  420. if (temp->handle == handle)
  421. goto err_lock;
  422. }
  423. if (!driver_data.selected_smi)
  424. driver_data.selected_smi = ipmi_device;
  425. list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
  426. mutex_unlock(&driver_data.ipmi_lock);
  427. put_device(smi_data.dev);
  428. return;
  429. err_lock:
  430. mutex_unlock(&driver_data.ipmi_lock);
  431. ipmi_dev_release(ipmi_device);
  432. err_ref:
  433. put_device(smi_data.dev);
  434. return;
  435. }
  436. static void ipmi_bmc_gone(int iface)
  437. {
  438. struct acpi_ipmi_device *ipmi_device, *temp;
  439. bool dev_found = false;
  440. mutex_lock(&driver_data.ipmi_lock);
  441. list_for_each_entry_safe(ipmi_device, temp,
  442. &driver_data.ipmi_devices, head) {
  443. if (ipmi_device->ipmi_ifnum != iface) {
  444. dev_found = true;
  445. __ipmi_dev_kill(ipmi_device);
  446. break;
  447. }
  448. }
  449. if (!driver_data.selected_smi)
  450. driver_data.selected_smi = list_first_entry_or_null(
  451. &driver_data.ipmi_devices,
  452. struct acpi_ipmi_device, head);
  453. mutex_unlock(&driver_data.ipmi_lock);
  454. if (dev_found) {
  455. ipmi_flush_tx_msg(ipmi_device);
  456. acpi_ipmi_dev_put(ipmi_device);
  457. }
  458. }
  459. /* --------------------------------------------------------------------------
  460. * Address Space Management
  461. * -------------------------------------------------------------------------- */
  462. /*
  463. * This is the IPMI opregion space handler.
  464. * @function: indicates the read/write. In fact as the IPMI message is driven
  465. * by command, only write is meaningful.
  466. * @address: This contains the netfn/command of IPMI request message.
  467. * @bits : not used.
  468. * @value : it is an in/out parameter. It points to the IPMI message buffer.
  469. * Before the IPMI message is sent, it represents the actual request
  470. * IPMI message. After the IPMI message is finished, it represents
  471. * the response IPMI message returned by IPMI command.
  472. * @handler_context: IPMI device context.
  473. */
  474. static acpi_status
  475. acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
  476. u32 bits, acpi_integer *value,
  477. void *handler_context, void *region_context)
  478. {
  479. struct acpi_ipmi_msg *tx_msg;
  480. struct acpi_ipmi_device *ipmi_device;
  481. int err;
  482. acpi_status status;
  483. unsigned long flags;
  484. /*
  485. * IPMI opregion message.
  486. * IPMI message is firstly written to the BMC and system software
  487. * can get the respsonse. So it is unmeaningful for the read access
  488. * of IPMI opregion.
  489. */
  490. if ((function & ACPI_IO_MASK) == ACPI_READ)
  491. return AE_TYPE;
  492. tx_msg = ipmi_msg_alloc();
  493. if (!tx_msg)
  494. return AE_NOT_EXIST;
  495. ipmi_device = tx_msg->device;
  496. if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
  497. ipmi_msg_release(tx_msg);
  498. return AE_TYPE;
  499. }
  500. acpi_ipmi_msg_get(tx_msg);
  501. mutex_lock(&driver_data.ipmi_lock);
  502. /* Do not add a tx_msg that can not be flushed. */
  503. if (ipmi_device->dead) {
  504. mutex_unlock(&driver_data.ipmi_lock);
  505. ipmi_msg_release(tx_msg);
  506. return AE_NOT_EXIST;
  507. }
  508. spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
  509. list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
  510. spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
  511. mutex_unlock(&driver_data.ipmi_lock);
  512. err = ipmi_request_settime(ipmi_device->user_interface,
  513. &tx_msg->addr,
  514. tx_msg->tx_msgid,
  515. &tx_msg->tx_message,
  516. NULL, 0, 0, IPMI_TIMEOUT);
  517. if (err) {
  518. status = AE_ERROR;
  519. goto out_msg;
  520. }
  521. wait_for_completion(&tx_msg->tx_complete);
  522. acpi_format_ipmi_response(tx_msg, value);
  523. status = AE_OK;
  524. out_msg:
  525. ipmi_cancel_tx_msg(ipmi_device, tx_msg);
  526. acpi_ipmi_msg_put(tx_msg);
  527. return status;
  528. }
  529. static int __init acpi_ipmi_init(void)
  530. {
  531. int result = 0;
  532. acpi_status status;
  533. if (acpi_disabled)
  534. return result;
  535. mutex_init(&driver_data.ipmi_lock);
  536. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  537. ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler,
  538. NULL, NULL);
  539. if (ACPI_FAILURE(status)) {
  540. pr_warn("Can't register IPMI opregion space handle\n");
  541. return -EINVAL;
  542. }
  543. result = ipmi_smi_watcher_register(&driver_data.bmc_events);
  544. if (result)
  545. pr_err("Can't register IPMI system interface watcher\n");
  546. return result;
  547. }
  548. static void __exit acpi_ipmi_exit(void)
  549. {
  550. struct acpi_ipmi_device *ipmi_device;
  551. if (acpi_disabled)
  552. return;
  553. ipmi_smi_watcher_unregister(&driver_data.bmc_events);
  554. /*
  555. * When one smi_watcher is unregistered, it is only deleted
  556. * from the smi_watcher list. But the smi_gone callback function
  557. * is not called. So explicitly uninstall the ACPI IPMI oregion
  558. * handler and free it.
  559. */
  560. mutex_lock(&driver_data.ipmi_lock);
  561. while (!list_empty(&driver_data.ipmi_devices)) {
  562. ipmi_device = list_first_entry(&driver_data.ipmi_devices,
  563. struct acpi_ipmi_device,
  564. head);
  565. __ipmi_dev_kill(ipmi_device);
  566. mutex_unlock(&driver_data.ipmi_lock);
  567. ipmi_flush_tx_msg(ipmi_device);
  568. acpi_ipmi_dev_put(ipmi_device);
  569. mutex_lock(&driver_data.ipmi_lock);
  570. }
  571. mutex_unlock(&driver_data.ipmi_lock);
  572. acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  573. ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler);
  574. }
  575. module_init(acpi_ipmi_init);
  576. module_exit(acpi_ipmi_exit);