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