acpi_ipmi.c 17 KB

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