acpi_ipmi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. .ipmi_lock = __MUTEX_INITIALIZER(driver_data.ipmi_lock)
  121. };
  122. static struct acpi_ipmi_device *
  123. ipmi_dev_alloc(int iface, struct device *dev, acpi_handle handle)
  124. {
  125. struct acpi_ipmi_device *ipmi_device;
  126. int err;
  127. ipmi_user_t user;
  128. ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
  129. if (!ipmi_device)
  130. return NULL;
  131. kref_init(&ipmi_device->kref);
  132. INIT_LIST_HEAD(&ipmi_device->head);
  133. INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
  134. spin_lock_init(&ipmi_device->tx_msg_lock);
  135. ipmi_device->handle = handle;
  136. ipmi_device->dev = get_device(dev);
  137. ipmi_device->ipmi_ifnum = iface;
  138. err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
  139. ipmi_device, &user);
  140. if (err) {
  141. put_device(dev);
  142. kfree(ipmi_device);
  143. return NULL;
  144. }
  145. ipmi_device->user_interface = user;
  146. return ipmi_device;
  147. }
  148. static void ipmi_dev_release(struct acpi_ipmi_device *ipmi_device)
  149. {
  150. ipmi_destroy_user(ipmi_device->user_interface);
  151. put_device(ipmi_device->dev);
  152. kfree(ipmi_device);
  153. }
  154. static void ipmi_dev_release_kref(struct kref *kref)
  155. {
  156. struct acpi_ipmi_device *ipmi =
  157. container_of(kref, struct acpi_ipmi_device, kref);
  158. ipmi_dev_release(ipmi);
  159. }
  160. static void __ipmi_dev_kill(struct acpi_ipmi_device *ipmi_device)
  161. {
  162. list_del(&ipmi_device->head);
  163. if (driver_data.selected_smi == ipmi_device)
  164. driver_data.selected_smi = NULL;
  165. /*
  166. * Always setting dead flag after deleting from the list or
  167. * list_for_each_entry() codes must get changed.
  168. */
  169. ipmi_device->dead = true;
  170. }
  171. static struct acpi_ipmi_device *acpi_ipmi_dev_get(void)
  172. {
  173. struct acpi_ipmi_device *ipmi_device = NULL;
  174. mutex_lock(&driver_data.ipmi_lock);
  175. if (driver_data.selected_smi) {
  176. ipmi_device = driver_data.selected_smi;
  177. kref_get(&ipmi_device->kref);
  178. }
  179. mutex_unlock(&driver_data.ipmi_lock);
  180. return ipmi_device;
  181. }
  182. static void acpi_ipmi_dev_put(struct acpi_ipmi_device *ipmi_device)
  183. {
  184. kref_put(&ipmi_device->kref, ipmi_dev_release_kref);
  185. }
  186. static struct acpi_ipmi_msg *ipmi_msg_alloc(void)
  187. {
  188. struct acpi_ipmi_device *ipmi;
  189. struct acpi_ipmi_msg *ipmi_msg;
  190. ipmi = acpi_ipmi_dev_get();
  191. if (!ipmi)
  192. return NULL;
  193. ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
  194. if (!ipmi_msg) {
  195. acpi_ipmi_dev_put(ipmi);
  196. return NULL;
  197. }
  198. kref_init(&ipmi_msg->kref);
  199. init_completion(&ipmi_msg->tx_complete);
  200. INIT_LIST_HEAD(&ipmi_msg->head);
  201. ipmi_msg->device = ipmi;
  202. ipmi_msg->msg_done = ACPI_IPMI_UNKNOWN;
  203. return ipmi_msg;
  204. }
  205. static void ipmi_msg_release(struct acpi_ipmi_msg *tx_msg)
  206. {
  207. acpi_ipmi_dev_put(tx_msg->device);
  208. kfree(tx_msg);
  209. }
  210. static void ipmi_msg_release_kref(struct kref *kref)
  211. {
  212. struct acpi_ipmi_msg *tx_msg =
  213. container_of(kref, struct acpi_ipmi_msg, kref);
  214. ipmi_msg_release(tx_msg);
  215. }
  216. static struct acpi_ipmi_msg *acpi_ipmi_msg_get(struct acpi_ipmi_msg *tx_msg)
  217. {
  218. kref_get(&tx_msg->kref);
  219. return tx_msg;
  220. }
  221. static void acpi_ipmi_msg_put(struct acpi_ipmi_msg *tx_msg)
  222. {
  223. kref_put(&tx_msg->kref, ipmi_msg_release_kref);
  224. }
  225. #define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
  226. #define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
  227. static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
  228. acpi_physical_address address,
  229. acpi_integer *value)
  230. {
  231. struct kernel_ipmi_msg *msg;
  232. struct acpi_ipmi_buffer *buffer;
  233. struct acpi_ipmi_device *device;
  234. unsigned long flags;
  235. msg = &tx_msg->tx_message;
  236. /*
  237. * IPMI network function and command are encoded in the address
  238. * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
  239. */
  240. msg->netfn = IPMI_OP_RGN_NETFN(address);
  241. msg->cmd = IPMI_OP_RGN_CMD(address);
  242. msg->data = tx_msg->data;
  243. /*
  244. * value is the parameter passed by the IPMI opregion space handler.
  245. * It points to the IPMI request message buffer
  246. */
  247. buffer = (struct acpi_ipmi_buffer *)value;
  248. /* copy the tx message data */
  249. if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
  250. dev_WARN_ONCE(tx_msg->device->dev, true,
  251. "Unexpected request (msg len %d).\n",
  252. buffer->length);
  253. return -EINVAL;
  254. }
  255. msg->data_len = buffer->length;
  256. memcpy(tx_msg->data, buffer->data, msg->data_len);
  257. /*
  258. * now the default type is SYSTEM_INTERFACE and channel type is BMC.
  259. * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
  260. * the addr type should be changed to IPMB. Then we will have to parse
  261. * the IPMI request message buffer to get the IPMB address.
  262. * If so, please fix me.
  263. */
  264. tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
  265. tx_msg->addr.channel = IPMI_BMC_CHANNEL;
  266. tx_msg->addr.data[0] = 0;
  267. /* Get the msgid */
  268. device = tx_msg->device;
  269. spin_lock_irqsave(&device->tx_msg_lock, flags);
  270. device->curr_msgid++;
  271. tx_msg->tx_msgid = device->curr_msgid;
  272. spin_unlock_irqrestore(&device->tx_msg_lock, flags);
  273. return 0;
  274. }
  275. static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
  276. acpi_integer *value)
  277. {
  278. struct acpi_ipmi_buffer *buffer;
  279. /*
  280. * value is also used as output parameter. It represents the response
  281. * IPMI message returned by IPMI command.
  282. */
  283. buffer = (struct acpi_ipmi_buffer *)value;
  284. /*
  285. * If the flag of msg_done is not set, it means that the IPMI command is
  286. * not executed correctly.
  287. */
  288. buffer->status = msg->msg_done;
  289. if (msg->msg_done != ACPI_IPMI_OK)
  290. return;
  291. /*
  292. * If the IPMI response message is obtained correctly, the status code
  293. * will be ACPI_IPMI_OK
  294. */
  295. buffer->length = msg->rx_len;
  296. memcpy(buffer->data, msg->data, msg->rx_len);
  297. }
  298. static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
  299. {
  300. struct acpi_ipmi_msg *tx_msg;
  301. unsigned long flags;
  302. /*
  303. * NOTE: On-going ipmi_recv_msg
  304. * ipmi_msg_handler() may still be invoked by ipmi_si after
  305. * flushing. But it is safe to do a fast flushing on module_exit()
  306. * without waiting for all ipmi_recv_msg(s) to complete from
  307. * ipmi_msg_handler() as it is ensured by ipmi_si that all
  308. * ipmi_recv_msg(s) are freed after invoking ipmi_destroy_user().
  309. */
  310. spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
  311. while (!list_empty(&ipmi->tx_msg_list)) {
  312. tx_msg = list_first_entry(&ipmi->tx_msg_list,
  313. struct acpi_ipmi_msg,
  314. head);
  315. list_del(&tx_msg->head);
  316. spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
  317. /* wake up the sleep thread on the Tx msg */
  318. complete(&tx_msg->tx_complete);
  319. acpi_ipmi_msg_put(tx_msg);
  320. spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
  321. }
  322. spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
  323. }
  324. static void ipmi_cancel_tx_msg(struct acpi_ipmi_device *ipmi,
  325. struct acpi_ipmi_msg *msg)
  326. {
  327. struct acpi_ipmi_msg *tx_msg, *temp;
  328. bool msg_found = false;
  329. unsigned long flags;
  330. spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
  331. list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
  332. if (msg == tx_msg) {
  333. msg_found = true;
  334. list_del(&tx_msg->head);
  335. break;
  336. }
  337. }
  338. spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
  339. if (msg_found)
  340. acpi_ipmi_msg_put(tx_msg);
  341. }
  342. static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
  343. {
  344. struct acpi_ipmi_device *ipmi_device = user_msg_data;
  345. bool msg_found = false;
  346. struct acpi_ipmi_msg *tx_msg, *temp;
  347. struct device *dev = ipmi_device->dev;
  348. unsigned long flags;
  349. if (msg->user != ipmi_device->user_interface) {
  350. dev_warn(dev, "Unexpected response is returned. "
  351. "returned user %p, expected user %p\n",
  352. msg->user, ipmi_device->user_interface);
  353. goto out_msg;
  354. }
  355. spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
  356. list_for_each_entry_safe(tx_msg, temp, &ipmi_device->tx_msg_list, head) {
  357. if (msg->msgid == tx_msg->tx_msgid) {
  358. msg_found = true;
  359. list_del(&tx_msg->head);
  360. break;
  361. }
  362. }
  363. spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
  364. if (!msg_found) {
  365. dev_warn(dev, "Unexpected response (msg id %ld) is "
  366. "returned.\n", msg->msgid);
  367. goto out_msg;
  368. }
  369. /* copy the response data to Rx_data buffer */
  370. if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
  371. dev_WARN_ONCE(dev, true,
  372. "Unexpected response (msg len %d).\n",
  373. msg->msg.data_len);
  374. goto out_comp;
  375. }
  376. /* response msg is an error msg */
  377. msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
  378. if (msg->recv_type == IPMI_RESPONSE_RECV_TYPE &&
  379. msg->msg.data_len == 1) {
  380. if (msg->msg.data[0] == IPMI_TIMEOUT_COMPLETION_CODE) {
  381. dev_WARN_ONCE(dev, true,
  382. "Unexpected response (timeout).\n");
  383. tx_msg->msg_done = ACPI_IPMI_TIMEOUT;
  384. }
  385. goto out_comp;
  386. }
  387. tx_msg->rx_len = msg->msg.data_len;
  388. memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
  389. tx_msg->msg_done = ACPI_IPMI_OK;
  390. out_comp:
  391. complete(&tx_msg->tx_complete);
  392. acpi_ipmi_msg_put(tx_msg);
  393. out_msg:
  394. ipmi_free_recv_msg(msg);
  395. };
  396. static void ipmi_register_bmc(int iface, struct device *dev)
  397. {
  398. struct acpi_ipmi_device *ipmi_device, *temp;
  399. int err;
  400. struct ipmi_smi_info smi_data;
  401. acpi_handle handle;
  402. err = ipmi_get_smi_info(iface, &smi_data);
  403. if (err)
  404. return;
  405. if (smi_data.addr_src != SI_ACPI)
  406. goto err_ref;
  407. handle = smi_data.addr_info.acpi_info.acpi_handle;
  408. if (!handle)
  409. goto err_ref;
  410. ipmi_device = ipmi_dev_alloc(iface, smi_data.dev, handle);
  411. if (!ipmi_device) {
  412. dev_warn(smi_data.dev, "Can't create IPMI user interface\n");
  413. goto err_ref;
  414. }
  415. mutex_lock(&driver_data.ipmi_lock);
  416. list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
  417. /*
  418. * if the corresponding ACPI handle is already added
  419. * to the device list, don't add it again.
  420. */
  421. if (temp->handle == handle)
  422. goto err_lock;
  423. }
  424. if (!driver_data.selected_smi)
  425. driver_data.selected_smi = ipmi_device;
  426. list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
  427. mutex_unlock(&driver_data.ipmi_lock);
  428. put_device(smi_data.dev);
  429. return;
  430. err_lock:
  431. mutex_unlock(&driver_data.ipmi_lock);
  432. ipmi_dev_release(ipmi_device);
  433. err_ref:
  434. put_device(smi_data.dev);
  435. return;
  436. }
  437. static void ipmi_bmc_gone(int iface)
  438. {
  439. struct acpi_ipmi_device *ipmi_device, *temp;
  440. bool dev_found = false;
  441. mutex_lock(&driver_data.ipmi_lock);
  442. list_for_each_entry_safe(ipmi_device, temp,
  443. &driver_data.ipmi_devices, head) {
  444. if (ipmi_device->ipmi_ifnum != iface) {
  445. dev_found = true;
  446. __ipmi_dev_kill(ipmi_device);
  447. break;
  448. }
  449. }
  450. if (!driver_data.selected_smi)
  451. driver_data.selected_smi = list_first_entry_or_null(
  452. &driver_data.ipmi_devices,
  453. struct acpi_ipmi_device, head);
  454. mutex_unlock(&driver_data.ipmi_lock);
  455. if (dev_found) {
  456. ipmi_flush_tx_msg(ipmi_device);
  457. acpi_ipmi_dev_put(ipmi_device);
  458. }
  459. }
  460. /* --------------------------------------------------------------------------
  461. * Address Space Management
  462. * -------------------------------------------------------------------------- */
  463. /*
  464. * This is the IPMI opregion space handler.
  465. * @function: indicates the read/write. In fact as the IPMI message is driven
  466. * by command, only write is meaningful.
  467. * @address: This contains the netfn/command of IPMI request message.
  468. * @bits : not used.
  469. * @value : it is an in/out parameter. It points to the IPMI message buffer.
  470. * Before the IPMI message is sent, it represents the actual request
  471. * IPMI message. After the IPMI message is finished, it represents
  472. * the response IPMI message returned by IPMI command.
  473. * @handler_context: IPMI device context.
  474. */
  475. static acpi_status
  476. acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
  477. u32 bits, acpi_integer *value,
  478. void *handler_context, void *region_context)
  479. {
  480. struct acpi_ipmi_msg *tx_msg;
  481. struct acpi_ipmi_device *ipmi_device;
  482. int err;
  483. acpi_status status;
  484. unsigned long flags;
  485. /*
  486. * IPMI opregion message.
  487. * IPMI message is firstly written to the BMC and system software
  488. * can get the respsonse. So it is unmeaningful for the read access
  489. * of IPMI opregion.
  490. */
  491. if ((function & ACPI_IO_MASK) == ACPI_READ)
  492. return AE_TYPE;
  493. tx_msg = ipmi_msg_alloc();
  494. if (!tx_msg)
  495. return AE_NOT_EXIST;
  496. ipmi_device = tx_msg->device;
  497. if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
  498. ipmi_msg_release(tx_msg);
  499. return AE_TYPE;
  500. }
  501. acpi_ipmi_msg_get(tx_msg);
  502. mutex_lock(&driver_data.ipmi_lock);
  503. /* Do not add a tx_msg that can not be flushed. */
  504. if (ipmi_device->dead) {
  505. mutex_unlock(&driver_data.ipmi_lock);
  506. ipmi_msg_release(tx_msg);
  507. return AE_NOT_EXIST;
  508. }
  509. spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
  510. list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
  511. spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
  512. mutex_unlock(&driver_data.ipmi_lock);
  513. err = ipmi_request_settime(ipmi_device->user_interface,
  514. &tx_msg->addr,
  515. tx_msg->tx_msgid,
  516. &tx_msg->tx_message,
  517. NULL, 0, 0, IPMI_TIMEOUT);
  518. if (err) {
  519. status = AE_ERROR;
  520. goto out_msg;
  521. }
  522. wait_for_completion(&tx_msg->tx_complete);
  523. acpi_format_ipmi_response(tx_msg, value);
  524. status = AE_OK;
  525. out_msg:
  526. ipmi_cancel_tx_msg(ipmi_device, tx_msg);
  527. acpi_ipmi_msg_put(tx_msg);
  528. return status;
  529. }
  530. static int __init acpi_ipmi_init(void)
  531. {
  532. int result;
  533. acpi_status status;
  534. if (acpi_disabled)
  535. return 0;
  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);