blob: b9da5ef390240afbadacc834b0ea141fdca499a6 [file] [log] [blame]
Zhao Yakuie92b2972010-12-08 10:10:18 +08001/*
2 * acpi_ipmi.c - ACPI IPMI opregion
3 *
Lv Zhenga1a69b22013-09-13 13:13:54 +08004 * Copyright (C) 2010, 2013 Intel Corporation
5 * Author: Zhao Yakui <yakui.zhao@intel.com>
6 * Lv Zheng <lv.zheng@intel.com>
Zhao Yakuie92b2972010-12-08 10:10:18 +08007 *
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>
Lv Zheng06a85662013-09-13 13:13:23 +080043#include <linux/spinlock.h>
Zhao Yakuie92b2972010-12-08 10:10:18 +080044
45MODULE_AUTHOR("Zhao Yakui");
46MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
47MODULE_LICENSE("GPL");
48
Zhao Yakuie92b2972010-12-08 10:10:18 +080049
50#define ACPI_IPMI_OK 0
51#define ACPI_IPMI_TIMEOUT 0x10
52#define ACPI_IPMI_UNKNOWN 0x07
53/* the IPMI timeout is 5s */
Lv Zheng8584ec62013-09-13 13:13:47 +080054#define IPMI_TIMEOUT (5000)
Lv Zheng6b68f032013-09-13 13:13:30 +080055#define ACPI_IPMI_MAX_MSG_LENGTH 64
Zhao Yakuie92b2972010-12-08 10:10:18 +080056
57struct acpi_ipmi_device {
58 /* the device list attached to driver_data.ipmi_devices */
59 struct list_head head;
60 /* the IPMI request message list */
61 struct list_head tx_msg_list;
Lv Zheng06a85662013-09-13 13:13:23 +080062 spinlock_t tx_msg_lock;
Zhao Yakuie92b2972010-12-08 10:10:18 +080063 acpi_handle handle;
64 struct pnp_dev *pnp_dev;
65 ipmi_user_t user_interface;
66 int ipmi_ifnum; /* IPMI interface number */
67 long curr_msgid;
Zhao Yakuie92b2972010-12-08 10:10:18 +080068 struct ipmi_smi_info smi_data;
Lv Zhenga1a69b22013-09-13 13:13:54 +080069 bool dead;
70 struct kref kref;
Zhao Yakuie92b2972010-12-08 10:10:18 +080071};
72
73struct ipmi_driver_data {
74 struct list_head ipmi_devices;
75 struct ipmi_smi_watcher bmc_events;
76 struct ipmi_user_hndl ipmi_hndlrs;
77 struct mutex ipmi_lock;
Lv Zhenge96a94e2013-09-13 13:14:02 +080078 /*
79 * NOTE: IPMI System Interface Selection
80 * There is no system interface specified by the IPMI operation
81 * region access. We try to select one system interface with ACPI
82 * handle set. IPMI messages passed from the ACPI codes are sent
83 * to this selected global IPMI system interface.
84 */
85 struct acpi_ipmi_device *selected_smi;
Zhao Yakuie92b2972010-12-08 10:10:18 +080086};
87
88struct acpi_ipmi_msg {
89 struct list_head head;
90 /*
91 * General speaking the addr type should be SI_ADDR_TYPE. And
92 * the addr channel should be BMC.
93 * In fact it can also be IPMB type. But we will have to
94 * parse it from the Netfn command buffer. It is so complex
95 * that it is skipped.
96 */
97 struct ipmi_addr addr;
98 long tx_msgid;
99 /* it is used to track whether the IPMI message is finished */
100 struct completion tx_complete;
101 struct kernel_ipmi_msg tx_message;
102 int msg_done;
Lv Zheng6b68f032013-09-13 13:13:30 +0800103 /* tx/rx data . And copy it from/to ACPI object buffer */
104 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
105 u8 rx_len;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800106 struct acpi_ipmi_device *device;
Lv Zheng7b984472013-09-13 13:14:11 +0800107 struct kref kref;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800108};
109
110/* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
111struct acpi_ipmi_buffer {
112 u8 status;
113 u8 length;
Lv Zheng6b68f032013-09-13 13:13:30 +0800114 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
Zhao Yakuie92b2972010-12-08 10:10:18 +0800115};
116
117static void ipmi_register_bmc(int iface, struct device *dev);
118static void ipmi_bmc_gone(int iface);
119static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800120
121static struct ipmi_driver_data driver_data = {
122 .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
123 .bmc_events = {
124 .owner = THIS_MODULE,
125 .new_smi = ipmi_register_bmc,
126 .smi_gone = ipmi_bmc_gone,
127 },
128 .ipmi_hndlrs = {
129 .ipmi_recv_hndl = ipmi_msg_handler,
130 },
131};
132
Lv Zhenga1a69b22013-09-13 13:13:54 +0800133static struct acpi_ipmi_device *
134ipmi_dev_alloc(int iface, struct ipmi_smi_info *smi_data, acpi_handle handle)
135{
136 struct acpi_ipmi_device *ipmi_device;
137 int err;
138 ipmi_user_t user;
139
140 ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
141 if (!ipmi_device)
142 return NULL;
143
144 kref_init(&ipmi_device->kref);
145 INIT_LIST_HEAD(&ipmi_device->head);
146 INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
147 spin_lock_init(&ipmi_device->tx_msg_lock);
148
149 ipmi_device->handle = handle;
150 ipmi_device->pnp_dev = to_pnp_dev(get_device(smi_data->dev));
151 memcpy(&ipmi_device->smi_data, smi_data, sizeof(struct ipmi_smi_info));
152 ipmi_device->ipmi_ifnum = iface;
153
154 err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
155 ipmi_device, &user);
156 if (err) {
157 put_device(smi_data->dev);
158 kfree(ipmi_device);
159 return NULL;
160 }
161 ipmi_device->user_interface = user;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800162
163 return ipmi_device;
164}
165
166static void ipmi_dev_release(struct acpi_ipmi_device *ipmi_device)
167{
Lv Zhenga1a69b22013-09-13 13:13:54 +0800168 ipmi_destroy_user(ipmi_device->user_interface);
169 put_device(ipmi_device->smi_data.dev);
170 kfree(ipmi_device);
171}
172
173static void ipmi_dev_release_kref(struct kref *kref)
174{
175 struct acpi_ipmi_device *ipmi =
176 container_of(kref, struct acpi_ipmi_device, kref);
177
178 ipmi_dev_release(ipmi);
179}
180
181static void __ipmi_dev_kill(struct acpi_ipmi_device *ipmi_device)
182{
183 list_del(&ipmi_device->head);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800184 if (driver_data.selected_smi == ipmi_device)
185 driver_data.selected_smi = NULL;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800186 /*
187 * Always setting dead flag after deleting from the list or
188 * list_for_each_entry() codes must get changed.
189 */
190 ipmi_device->dead = true;
191}
192
Lv Zhenge96a94e2013-09-13 13:14:02 +0800193static struct acpi_ipmi_device *acpi_ipmi_dev_get(void)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800194{
Lv Zhenge96a94e2013-09-13 13:14:02 +0800195 struct acpi_ipmi_device *ipmi_device = NULL;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800196
197 mutex_lock(&driver_data.ipmi_lock);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800198 if (driver_data.selected_smi) {
199 ipmi_device = driver_data.selected_smi;
200 kref_get(&ipmi_device->kref);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800201 }
202 mutex_unlock(&driver_data.ipmi_lock);
203
204 return ipmi_device;
205}
206
207static void acpi_ipmi_dev_put(struct acpi_ipmi_device *ipmi_device)
208{
209 kref_put(&ipmi_device->kref, ipmi_dev_release_kref);
210}
211
Lv Zheng7b984472013-09-13 13:14:11 +0800212static struct acpi_ipmi_msg *ipmi_msg_alloc(void)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800213{
Lv Zheng7b984472013-09-13 13:14:11 +0800214 struct acpi_ipmi_device *ipmi;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800215 struct acpi_ipmi_msg *ipmi_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800216
Lv Zheng7b984472013-09-13 13:14:11 +0800217 ipmi = acpi_ipmi_dev_get();
218 if (!ipmi)
219 return NULL;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800220 ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
Lv Zheng7b984472013-09-13 13:14:11 +0800221 if (!ipmi_msg) {
222 acpi_ipmi_dev_put(ipmi);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800223 return NULL;
224 }
Lv Zheng7b984472013-09-13 13:14:11 +0800225 kref_init(&ipmi_msg->kref);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800226 init_completion(&ipmi_msg->tx_complete);
227 INIT_LIST_HEAD(&ipmi_msg->head);
228 ipmi_msg->device = ipmi;
Lv Zheng8584ec62013-09-13 13:13:47 +0800229 ipmi_msg->msg_done = ACPI_IPMI_UNKNOWN;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800230 return ipmi_msg;
231}
232
Lv Zheng7b984472013-09-13 13:14:11 +0800233static void ipmi_msg_release(struct acpi_ipmi_msg *tx_msg)
234{
235 acpi_ipmi_dev_put(tx_msg->device);
236 kfree(tx_msg);
237}
238
239static void ipmi_msg_release_kref(struct kref *kref)
240{
241 struct acpi_ipmi_msg *tx_msg =
242 container_of(kref, struct acpi_ipmi_msg, kref);
243
244 ipmi_msg_release(tx_msg);
245}
246
247static struct acpi_ipmi_msg *acpi_ipmi_msg_get(struct acpi_ipmi_msg *tx_msg)
248{
249 kref_get(&tx_msg->kref);
250
251 return tx_msg;
252}
253
254static void acpi_ipmi_msg_put(struct acpi_ipmi_msg *tx_msg)
255{
256 kref_put(&tx_msg->kref, ipmi_msg_release_kref);
257}
258
Zhao Yakuie92b2972010-12-08 10:10:18 +0800259#define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
260#define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
Lv Zheng6b68f032013-09-13 13:13:30 +0800261static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
Zhao Yakuie92b2972010-12-08 10:10:18 +0800262 acpi_physical_address address,
263 acpi_integer *value)
264{
265 struct kernel_ipmi_msg *msg;
266 struct acpi_ipmi_buffer *buffer;
267 struct acpi_ipmi_device *device;
Lv Zheng06a85662013-09-13 13:13:23 +0800268 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800269
270 msg = &tx_msg->tx_message;
271 /*
272 * IPMI network function and command are encoded in the address
273 * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
274 */
275 msg->netfn = IPMI_OP_RGN_NETFN(address);
276 msg->cmd = IPMI_OP_RGN_CMD(address);
Lv Zheng6b68f032013-09-13 13:13:30 +0800277 msg->data = tx_msg->data;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800278 /*
279 * value is the parameter passed by the IPMI opregion space handler.
280 * It points to the IPMI request message buffer
281 */
282 buffer = (struct acpi_ipmi_buffer *)value;
283 /* copy the tx message data */
Lv Zheng6b68f032013-09-13 13:13:30 +0800284 if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
285 dev_WARN_ONCE(&tx_msg->device->pnp_dev->dev, true,
286 "Unexpected request (msg len %d).\n",
287 buffer->length);
288 return -EINVAL;
289 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800290 msg->data_len = buffer->length;
Lv Zheng6b68f032013-09-13 13:13:30 +0800291 memcpy(tx_msg->data, buffer->data, msg->data_len);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800292 /*
293 * now the default type is SYSTEM_INTERFACE and channel type is BMC.
294 * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
295 * the addr type should be changed to IPMB. Then we will have to parse
296 * the IPMI request message buffer to get the IPMB address.
297 * If so, please fix me.
298 */
299 tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
300 tx_msg->addr.channel = IPMI_BMC_CHANNEL;
301 tx_msg->addr.data[0] = 0;
302
303 /* Get the msgid */
304 device = tx_msg->device;
Lv Zheng06a85662013-09-13 13:13:23 +0800305 spin_lock_irqsave(&device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800306 device->curr_msgid++;
307 tx_msg->tx_msgid = device->curr_msgid;
Lv Zheng06a85662013-09-13 13:13:23 +0800308 spin_unlock_irqrestore(&device->tx_msg_lock, flags);
Lv Zheng6b68f032013-09-13 13:13:30 +0800309 return 0;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800310}
311
312static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
Lv Zheng8584ec62013-09-13 13:13:47 +0800313 acpi_integer *value)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800314{
315 struct acpi_ipmi_buffer *buffer;
316
317 /*
318 * value is also used as output parameter. It represents the response
319 * IPMI message returned by IPMI command.
320 */
321 buffer = (struct acpi_ipmi_buffer *)value;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800322 /*
Lv Zheng8584ec62013-09-13 13:13:47 +0800323 * If the flag of msg_done is not set, it means that the IPMI command is
324 * not executed correctly.
Zhao Yakuie92b2972010-12-08 10:10:18 +0800325 */
Lv Zheng8584ec62013-09-13 13:13:47 +0800326 buffer->status = msg->msg_done;
327 if (msg->msg_done != ACPI_IPMI_OK)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800328 return;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800329 /*
330 * If the IPMI response message is obtained correctly, the status code
331 * will be ACPI_IPMI_OK
332 */
Zhao Yakuie92b2972010-12-08 10:10:18 +0800333 buffer->length = msg->rx_len;
Lv Zheng6b68f032013-09-13 13:13:30 +0800334 memcpy(buffer->data, msg->data, msg->rx_len);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800335}
336
337static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
338{
Lv Zheng7b984472013-09-13 13:14:11 +0800339 struct acpi_ipmi_msg *tx_msg;
Lv Zheng5ac557e2013-09-13 13:13:39 +0800340 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800341
Lv Zhenga1a69b22013-09-13 13:13:54 +0800342 /*
343 * NOTE: On-going ipmi_recv_msg
344 * ipmi_msg_handler() may still be invoked by ipmi_si after
345 * flushing. But it is safe to do a fast flushing on module_exit()
346 * without waiting for all ipmi_recv_msg(s) to complete from
347 * ipmi_msg_handler() as it is ensured by ipmi_si that all
348 * ipmi_recv_msg(s) are freed after invoking ipmi_destroy_user().
349 */
Lv Zheng5ac557e2013-09-13 13:13:39 +0800350 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
Lv Zheng7b984472013-09-13 13:14:11 +0800351 while (!list_empty(&ipmi->tx_msg_list)) {
352 tx_msg = list_first_entry(&ipmi->tx_msg_list,
353 struct acpi_ipmi_msg,
354 head);
355 list_del(&tx_msg->head);
356 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
357
Zhao Yakuie92b2972010-12-08 10:10:18 +0800358 /* wake up the sleep thread on the Tx msg */
359 complete(&tx_msg->tx_complete);
Lv Zheng7b984472013-09-13 13:14:11 +0800360 acpi_ipmi_msg_put(tx_msg);
361 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800362 }
Lv Zheng5ac557e2013-09-13 13:13:39 +0800363 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800364}
365
Lv Zheng7b984472013-09-13 13:14:11 +0800366static void ipmi_cancel_tx_msg(struct acpi_ipmi_device *ipmi,
367 struct acpi_ipmi_msg *msg)
368{
369 struct acpi_ipmi_msg *tx_msg, *temp;
370 bool msg_found = false;
371 unsigned long flags;
372
373 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
374 list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
375 if (msg == tx_msg) {
376 msg_found = true;
377 list_del(&tx_msg->head);
378 break;
379 }
380 }
381 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
382
383 if (msg_found)
384 acpi_ipmi_msg_put(tx_msg);
385}
386
Zhao Yakuie92b2972010-12-08 10:10:18 +0800387static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
388{
389 struct acpi_ipmi_device *ipmi_device = user_msg_data;
Lv Zheng7b984472013-09-13 13:14:11 +0800390 bool msg_found = false;
391 struct acpi_ipmi_msg *tx_msg, *temp;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800392 struct pnp_dev *pnp_dev = ipmi_device->pnp_dev;
Lv Zheng06a85662013-09-13 13:13:23 +0800393 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800394
395 if (msg->user != ipmi_device->user_interface) {
396 dev_warn(&pnp_dev->dev, "Unexpected response is returned. "
397 "returned user %p, expected user %p\n",
398 msg->user, ipmi_device->user_interface);
Lv Zheng6b68f032013-09-13 13:13:30 +0800399 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800400 }
Lv Zheng06a85662013-09-13 13:13:23 +0800401 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
Lv Zheng7b984472013-09-13 13:14:11 +0800402 list_for_each_entry_safe(tx_msg, temp, &ipmi_device->tx_msg_list, head) {
Zhao Yakuie92b2972010-12-08 10:10:18 +0800403 if (msg->msgid == tx_msg->tx_msgid) {
Lv Zheng7b984472013-09-13 13:14:11 +0800404 msg_found = true;
405 list_del(&tx_msg->head);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800406 break;
407 }
408 }
Lv Zheng7b984472013-09-13 13:14:11 +0800409 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800410
Zhao Yakuie92b2972010-12-08 10:10:18 +0800411 if (!msg_found) {
412 dev_warn(&pnp_dev->dev, "Unexpected response (msg id %ld) is "
413 "returned.\n", msg->msgid);
Lv Zheng7b984472013-09-13 13:14:11 +0800414 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800415 }
416
Lv Zheng6b68f032013-09-13 13:13:30 +0800417 /* copy the response data to Rx_data buffer */
418 if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
419 dev_WARN_ONCE(&pnp_dev->dev, true,
420 "Unexpected response (msg len %d).\n",
421 msg->msg.data_len);
Lv Zheng8584ec62013-09-13 13:13:47 +0800422 goto out_comp;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800423 }
Lv Zheng8584ec62013-09-13 13:13:47 +0800424 /* response msg is an error msg */
425 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
426 if (msg->recv_type == IPMI_RESPONSE_RECV_TYPE &&
427 msg->msg.data_len == 1) {
428 if (msg->msg.data[0] == IPMI_TIMEOUT_COMPLETION_CODE) {
429 dev_WARN_ONCE(&pnp_dev->dev, true,
430 "Unexpected response (timeout).\n");
431 tx_msg->msg_done = ACPI_IPMI_TIMEOUT;
432 }
433 goto out_comp;
434 }
435 tx_msg->rx_len = msg->msg.data_len;
436 memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
437 tx_msg->msg_done = ACPI_IPMI_OK;
438out_comp:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800439 complete(&tx_msg->tx_complete);
Lv Zheng7b984472013-09-13 13:14:11 +0800440 acpi_ipmi_msg_put(tx_msg);
Lv Zheng6b68f032013-09-13 13:13:30 +0800441out_msg:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800442 ipmi_free_recv_msg(msg);
443};
444
445static void ipmi_register_bmc(int iface, struct device *dev)
446{
447 struct acpi_ipmi_device *ipmi_device, *temp;
448 struct pnp_dev *pnp_dev;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800449 int err;
450 struct ipmi_smi_info smi_data;
451 acpi_handle handle;
452
453 err = ipmi_get_smi_info(iface, &smi_data);
454
455 if (err)
456 return;
457
Lv Zhenga1a69b22013-09-13 13:13:54 +0800458 if (smi_data.addr_src != SI_ACPI)
459 goto err_ref;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800460 handle = smi_data.addr_info.acpi_info.acpi_handle;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800461 if (!handle)
462 goto err_ref;
463 pnp_dev = to_pnp_dev(smi_data.dev);
464
465 ipmi_device = ipmi_dev_alloc(iface, &smi_data, handle);
466 if (!ipmi_device) {
467 dev_warn(&pnp_dev->dev, "Can't create IPMI user interface\n");
468 goto err_ref;
469 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800470
471 mutex_lock(&driver_data.ipmi_lock);
472 list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
473 /*
474 * if the corresponding ACPI handle is already added
475 * to the device list, don't add it again.
476 */
477 if (temp->handle == handle)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800478 goto err_lock;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800479 }
480
Lv Zhenge96a94e2013-09-13 13:14:02 +0800481 if (!driver_data.selected_smi)
482 driver_data.selected_smi = ipmi_device;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800483 list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800484 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800485 put_device(smi_data.dev);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800486 return;
487
Lv Zhenga1a69b22013-09-13 13:13:54 +0800488err_lock:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800489 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800490 ipmi_dev_release(ipmi_device);
491err_ref:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800492 put_device(smi_data.dev);
493 return;
494}
495
496static void ipmi_bmc_gone(int iface)
497{
498 struct acpi_ipmi_device *ipmi_device, *temp;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800499 bool dev_found = false;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800500
501 mutex_lock(&driver_data.ipmi_lock);
502 list_for_each_entry_safe(ipmi_device, temp,
503 &driver_data.ipmi_devices, head) {
Lv Zhenga1a69b22013-09-13 13:13:54 +0800504 if (ipmi_device->ipmi_ifnum != iface) {
505 dev_found = true;
506 __ipmi_dev_kill(ipmi_device);
507 break;
508 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800509 }
Lv Zhenge96a94e2013-09-13 13:14:02 +0800510 if (!driver_data.selected_smi)
511 driver_data.selected_smi = list_first_entry_or_null(
512 &driver_data.ipmi_devices,
513 struct acpi_ipmi_device, head);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800514 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800515 if (dev_found) {
516 ipmi_flush_tx_msg(ipmi_device);
517 acpi_ipmi_dev_put(ipmi_device);
518 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800519}
520/* --------------------------------------------------------------------------
521 * Address Space Management
522 * -------------------------------------------------------------------------- */
523/*
524 * This is the IPMI opregion space handler.
525 * @function: indicates the read/write. In fact as the IPMI message is driven
526 * by command, only write is meaningful.
527 * @address: This contains the netfn/command of IPMI request message.
528 * @bits : not used.
529 * @value : it is an in/out parameter. It points to the IPMI message buffer.
530 * Before the IPMI message is sent, it represents the actual request
531 * IPMI message. After the IPMI message is finished, it represents
532 * the response IPMI message returned by IPMI command.
533 * @handler_context: IPMI device context.
534 */
535
536static acpi_status
537acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
538 u32 bits, acpi_integer *value,
539 void *handler_context, void *region_context)
540{
541 struct acpi_ipmi_msg *tx_msg;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800542 struct acpi_ipmi_device *ipmi_device;
Lv Zheng8584ec62013-09-13 13:13:47 +0800543 int err;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800544 acpi_status status;
Lv Zheng06a85662013-09-13 13:13:23 +0800545 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800546 /*
547 * IPMI opregion message.
548 * IPMI message is firstly written to the BMC and system software
549 * can get the respsonse. So it is unmeaningful for the read access
550 * of IPMI opregion.
551 */
552 if ((function & ACPI_IO_MASK) == ACPI_READ)
553 return AE_TYPE;
554
Lv Zheng7b984472013-09-13 13:14:11 +0800555 tx_msg = ipmi_msg_alloc();
556 if (!tx_msg)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800557 return AE_NOT_EXIST;
558
Lv Zheng7b984472013-09-13 13:14:11 +0800559 ipmi_device = tx_msg->device;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800560
Lv Zheng6b68f032013-09-13 13:13:30 +0800561 if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
Lv Zheng7b984472013-09-13 13:14:11 +0800562 ipmi_msg_release(tx_msg);
563 return AE_TYPE;
Lv Zheng6b68f032013-09-13 13:13:30 +0800564 }
Lv Zheng7b984472013-09-13 13:14:11 +0800565 acpi_ipmi_msg_get(tx_msg);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800566 mutex_lock(&driver_data.ipmi_lock);
567 /* Do not add a tx_msg that can not be flushed. */
568 if (ipmi_device->dead) {
Lv Zhenga1a69b22013-09-13 13:13:54 +0800569 mutex_unlock(&driver_data.ipmi_lock);
Lv Zheng7b984472013-09-13 13:14:11 +0800570 ipmi_msg_release(tx_msg);
571 return AE_NOT_EXIST;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800572 }
Lv Zheng06a85662013-09-13 13:13:23 +0800573 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800574 list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
Lv Zheng06a85662013-09-13 13:13:23 +0800575 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800576 mutex_unlock(&driver_data.ipmi_lock);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800577 err = ipmi_request_settime(ipmi_device->user_interface,
578 &tx_msg->addr,
579 tx_msg->tx_msgid,
580 &tx_msg->tx_message,
Lv Zheng8584ec62013-09-13 13:13:47 +0800581 NULL, 0, 0, IPMI_TIMEOUT);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800582 if (err) {
583 status = AE_ERROR;
Lv Zheng7b984472013-09-13 13:14:11 +0800584 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800585 }
Lv Zheng8584ec62013-09-13 13:13:47 +0800586 wait_for_completion(&tx_msg->tx_complete);
587 acpi_format_ipmi_response(tx_msg, value);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800588 status = AE_OK;
589
Lv Zheng6b68f032013-09-13 13:13:30 +0800590out_msg:
Lv Zheng7b984472013-09-13 13:14:11 +0800591 ipmi_cancel_tx_msg(ipmi_device, tx_msg);
592 acpi_ipmi_msg_put(tx_msg);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800593 return status;
594}
595
Zhao Yakuie92b2972010-12-08 10:10:18 +0800596static int __init acpi_ipmi_init(void)
597{
598 int result = 0;
Lv Zhenge96a94e2013-09-13 13:14:02 +0800599 acpi_status status;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800600
601 if (acpi_disabled)
602 return result;
603
604 mutex_init(&driver_data.ipmi_lock);
605
Lv Zhenge96a94e2013-09-13 13:14:02 +0800606 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
607 ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler,
608 NULL, NULL);
609 if (ACPI_FAILURE(status)) {
610 pr_warn("Can't register IPMI opregion space handle\n");
611 return -EINVAL;
612 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800613 result = ipmi_smi_watcher_register(&driver_data.bmc_events);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800614 if (result)
615 pr_err("Can't register IPMI system interface watcher\n");
Zhao Yakuie92b2972010-12-08 10:10:18 +0800616
617 return result;
618}
619
620static void __exit acpi_ipmi_exit(void)
621{
Lv Zhenga1a69b22013-09-13 13:13:54 +0800622 struct acpi_ipmi_device *ipmi_device;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800623
624 if (acpi_disabled)
625 return;
626
627 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
628
629 /*
630 * When one smi_watcher is unregistered, it is only deleted
631 * from the smi_watcher list. But the smi_gone callback function
632 * is not called. So explicitly uninstall the ACPI IPMI oregion
633 * handler and free it.
634 */
635 mutex_lock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800636 while (!list_empty(&driver_data.ipmi_devices)) {
637 ipmi_device = list_first_entry(&driver_data.ipmi_devices,
638 struct acpi_ipmi_device,
639 head);
640 __ipmi_dev_kill(ipmi_device);
641 mutex_unlock(&driver_data.ipmi_lock);
642
643 ipmi_flush_tx_msg(ipmi_device);
644 acpi_ipmi_dev_put(ipmi_device);
645
646 mutex_lock(&driver_data.ipmi_lock);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800647 }
648 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800649 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
650 ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800651}
652
653module_init(acpi_ipmi_init);
654module_exit(acpi_ipmi_exit);