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