blob: f77956c3fd45f2756694f16afd688bba77346ceb [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 *
Zhao Yakuie92b2972010-12-08 10:10:18 +080020 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22
Zhao Yakuie92b2972010-12-08 10:10:18 +080023#include <linux/module.h>
Lv Zheng935a9e32013-09-13 13:14:41 +080024#include <linux/acpi.h>
Zhao Yakuie92b2972010-12-08 10:10:18 +080025#include <linux/ipmi.h>
Lv Zheng06a85662013-09-13 13:13:23 +080026#include <linux/spinlock.h>
Zhao Yakuie92b2972010-12-08 10:10:18 +080027
28MODULE_AUTHOR("Zhao Yakui");
29MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
30MODULE_LICENSE("GPL");
31
Zhao Yakuie92b2972010-12-08 10:10:18 +080032#define ACPI_IPMI_OK 0
33#define ACPI_IPMI_TIMEOUT 0x10
34#define ACPI_IPMI_UNKNOWN 0x07
35/* the IPMI timeout is 5s */
Lv Zheng8584ec62013-09-13 13:13:47 +080036#define IPMI_TIMEOUT (5000)
Lv Zheng6b68f032013-09-13 13:13:30 +080037#define ACPI_IPMI_MAX_MSG_LENGTH 64
Zhao Yakuie92b2972010-12-08 10:10:18 +080038
39struct acpi_ipmi_device {
40 /* the device list attached to driver_data.ipmi_devices */
41 struct list_head head;
Lv Zheng50065302013-09-13 13:15:00 +080042
Zhao Yakuie92b2972010-12-08 10:10:18 +080043 /* the IPMI request message list */
44 struct list_head tx_msg_list;
Lv Zheng50065302013-09-13 13:15:00 +080045
46 spinlock_t tx_msg_lock;
Zhao Yakuie92b2972010-12-08 10:10:18 +080047 acpi_handle handle;
Lv Zheng2fb037b2013-09-13 13:14:21 +080048 struct device *dev;
Lv Zheng50065302013-09-13 13:15:00 +080049 ipmi_user_t user_interface;
Zhao Yakuie92b2972010-12-08 10:10:18 +080050 int ipmi_ifnum; /* IPMI interface number */
51 long curr_msgid;
Lv Zhenga1a69b22013-09-13 13:13:54 +080052 bool dead;
53 struct kref kref;
Zhao Yakuie92b2972010-12-08 10:10:18 +080054};
55
56struct ipmi_driver_data {
Lv Zheng50065302013-09-13 13:15:00 +080057 struct list_head ipmi_devices;
58 struct ipmi_smi_watcher bmc_events;
59 struct ipmi_user_hndl ipmi_hndlrs;
60 struct mutex ipmi_lock;
61
Lv Zhenge96a94e2013-09-13 13:14:02 +080062 /*
63 * NOTE: IPMI System Interface Selection
64 * There is no system interface specified by the IPMI operation
65 * region access. We try to select one system interface with ACPI
66 * handle set. IPMI messages passed from the ACPI codes are sent
67 * to this selected global IPMI system interface.
68 */
69 struct acpi_ipmi_device *selected_smi;
Zhao Yakuie92b2972010-12-08 10:10:18 +080070};
71
72struct acpi_ipmi_msg {
73 struct list_head head;
Lv Zheng50065302013-09-13 13:15:00 +080074
Zhao Yakuie92b2972010-12-08 10:10:18 +080075 /*
76 * General speaking the addr type should be SI_ADDR_TYPE. And
77 * the addr channel should be BMC.
78 * In fact it can also be IPMB type. But we will have to
79 * parse it from the Netfn command buffer. It is so complex
80 * that it is skipped.
81 */
82 struct ipmi_addr addr;
83 long tx_msgid;
Lv Zheng50065302013-09-13 13:15:00 +080084
Zhao Yakuie92b2972010-12-08 10:10:18 +080085 /* it is used to track whether the IPMI message is finished */
86 struct completion tx_complete;
Lv Zheng50065302013-09-13 13:15:00 +080087
Zhao Yakuie92b2972010-12-08 10:10:18 +080088 struct kernel_ipmi_msg tx_message;
Lv Zheng50065302013-09-13 13:15:00 +080089 int msg_done;
90
Lv Zheng6b68f032013-09-13 13:13:30 +080091 /* tx/rx data . And copy it from/to ACPI object buffer */
Lv Zheng50065302013-09-13 13:15:00 +080092 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
93 u8 rx_len;
94
Zhao Yakuie92b2972010-12-08 10:10:18 +080095 struct acpi_ipmi_device *device;
Lv Zheng50065302013-09-13 13:15:00 +080096 struct kref kref;
Zhao Yakuie92b2972010-12-08 10:10:18 +080097};
98
99/* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
100struct acpi_ipmi_buffer {
101 u8 status;
102 u8 length;
Lv Zheng6b68f032013-09-13 13:13:30 +0800103 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
Zhao Yakuie92b2972010-12-08 10:10:18 +0800104};
105
106static void ipmi_register_bmc(int iface, struct device *dev);
107static void ipmi_bmc_gone(int iface);
108static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800109
110static struct ipmi_driver_data driver_data = {
111 .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
112 .bmc_events = {
113 .owner = THIS_MODULE,
114 .new_smi = ipmi_register_bmc,
115 .smi_gone = ipmi_bmc_gone,
116 },
117 .ipmi_hndlrs = {
118 .ipmi_recv_hndl = ipmi_msg_handler,
119 },
Lv Zhenga194aa42013-09-13 13:14:31 +0800120 .ipmi_lock = __MUTEX_INITIALIZER(driver_data.ipmi_lock)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800121};
122
Lv Zhenga1a69b22013-09-13 13:13:54 +0800123static struct acpi_ipmi_device *
Lv Zheng2fb037b2013-09-13 13:14:21 +0800124ipmi_dev_alloc(int iface, struct device *dev, acpi_handle handle)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800125{
126 struct acpi_ipmi_device *ipmi_device;
127 int err;
128 ipmi_user_t user;
129
130 ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
131 if (!ipmi_device)
132 return NULL;
133
134 kref_init(&ipmi_device->kref);
135 INIT_LIST_HEAD(&ipmi_device->head);
136 INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
137 spin_lock_init(&ipmi_device->tx_msg_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800138 ipmi_device->handle = handle;
Lv Zheng2fb037b2013-09-13 13:14:21 +0800139 ipmi_device->dev = get_device(dev);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800140 ipmi_device->ipmi_ifnum = iface;
141
142 err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
143 ipmi_device, &user);
144 if (err) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800145 put_device(dev);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800146 kfree(ipmi_device);
147 return NULL;
148 }
149 ipmi_device->user_interface = user;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800150
151 return ipmi_device;
152}
153
154static void ipmi_dev_release(struct acpi_ipmi_device *ipmi_device)
155{
Lv Zhenga1a69b22013-09-13 13:13:54 +0800156 ipmi_destroy_user(ipmi_device->user_interface);
Lv Zheng2fb037b2013-09-13 13:14:21 +0800157 put_device(ipmi_device->dev);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800158 kfree(ipmi_device);
159}
160
161static void ipmi_dev_release_kref(struct kref *kref)
162{
163 struct acpi_ipmi_device *ipmi =
164 container_of(kref, struct acpi_ipmi_device, kref);
165
166 ipmi_dev_release(ipmi);
167}
168
169static void __ipmi_dev_kill(struct acpi_ipmi_device *ipmi_device)
170{
171 list_del(&ipmi_device->head);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800172 if (driver_data.selected_smi == ipmi_device)
173 driver_data.selected_smi = NULL;
Lv Zheng50065302013-09-13 13:15:00 +0800174
Lv Zhenga1a69b22013-09-13 13:13:54 +0800175 /*
176 * Always setting dead flag after deleting from the list or
177 * list_for_each_entry() codes must get changed.
178 */
179 ipmi_device->dead = true;
180}
181
Lv Zhenge96a94e2013-09-13 13:14:02 +0800182static struct acpi_ipmi_device *acpi_ipmi_dev_get(void)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800183{
Lv Zhenge96a94e2013-09-13 13:14:02 +0800184 struct acpi_ipmi_device *ipmi_device = NULL;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800185
186 mutex_lock(&driver_data.ipmi_lock);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800187 if (driver_data.selected_smi) {
188 ipmi_device = driver_data.selected_smi;
189 kref_get(&ipmi_device->kref);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800190 }
191 mutex_unlock(&driver_data.ipmi_lock);
192
193 return ipmi_device;
194}
195
196static void acpi_ipmi_dev_put(struct acpi_ipmi_device *ipmi_device)
197{
198 kref_put(&ipmi_device->kref, ipmi_dev_release_kref);
199}
200
Lv Zheng7b984472013-09-13 13:14:11 +0800201static struct acpi_ipmi_msg *ipmi_msg_alloc(void)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800202{
Lv Zheng7b984472013-09-13 13:14:11 +0800203 struct acpi_ipmi_device *ipmi;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800204 struct acpi_ipmi_msg *ipmi_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800205
Lv Zheng7b984472013-09-13 13:14:11 +0800206 ipmi = acpi_ipmi_dev_get();
207 if (!ipmi)
208 return NULL;
Lv Zheng50065302013-09-13 13:15:00 +0800209
Zhao Yakuie92b2972010-12-08 10:10:18 +0800210 ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
Lv Zheng7b984472013-09-13 13:14:11 +0800211 if (!ipmi_msg) {
212 acpi_ipmi_dev_put(ipmi);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800213 return NULL;
214 }
Lv Zheng50065302013-09-13 13:15:00 +0800215
Lv Zheng7b984472013-09-13 13:14:11 +0800216 kref_init(&ipmi_msg->kref);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800217 init_completion(&ipmi_msg->tx_complete);
218 INIT_LIST_HEAD(&ipmi_msg->head);
219 ipmi_msg->device = ipmi;
Lv Zheng8584ec62013-09-13 13:13:47 +0800220 ipmi_msg->msg_done = ACPI_IPMI_UNKNOWN;
Lv Zheng50065302013-09-13 13:15:00 +0800221
Zhao Yakuie92b2972010-12-08 10:10:18 +0800222 return ipmi_msg;
223}
224
Lv Zheng7b984472013-09-13 13:14:11 +0800225static void ipmi_msg_release(struct acpi_ipmi_msg *tx_msg)
226{
227 acpi_ipmi_dev_put(tx_msg->device);
228 kfree(tx_msg);
229}
230
231static void ipmi_msg_release_kref(struct kref *kref)
232{
233 struct acpi_ipmi_msg *tx_msg =
234 container_of(kref, struct acpi_ipmi_msg, kref);
235
236 ipmi_msg_release(tx_msg);
237}
238
239static struct acpi_ipmi_msg *acpi_ipmi_msg_get(struct acpi_ipmi_msg *tx_msg)
240{
241 kref_get(&tx_msg->kref);
242
243 return tx_msg;
244}
245
246static void acpi_ipmi_msg_put(struct acpi_ipmi_msg *tx_msg)
247{
248 kref_put(&tx_msg->kref, ipmi_msg_release_kref);
249}
250
Lv Zheng50065302013-09-13 13:15:00 +0800251#define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
252#define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
Lv Zheng6b68f032013-09-13 13:13:30 +0800253static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
Lv Zheng50065302013-09-13 13:15:00 +0800254 acpi_physical_address address,
255 acpi_integer *value)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800256{
257 struct kernel_ipmi_msg *msg;
258 struct acpi_ipmi_buffer *buffer;
259 struct acpi_ipmi_device *device;
Lv Zheng06a85662013-09-13 13:13:23 +0800260 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800261
262 msg = &tx_msg->tx_message;
Lv Zheng50065302013-09-13 13:15:00 +0800263
Zhao Yakuie92b2972010-12-08 10:10:18 +0800264 /*
265 * IPMI network function and command are encoded in the address
266 * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
267 */
268 msg->netfn = IPMI_OP_RGN_NETFN(address);
269 msg->cmd = IPMI_OP_RGN_CMD(address);
Lv Zheng6b68f032013-09-13 13:13:30 +0800270 msg->data = tx_msg->data;
Lv Zheng50065302013-09-13 13:15:00 +0800271
Zhao Yakuie92b2972010-12-08 10:10:18 +0800272 /*
273 * value is the parameter passed by the IPMI opregion space handler.
274 * It points to the IPMI request message buffer
275 */
276 buffer = (struct acpi_ipmi_buffer *)value;
Lv Zheng50065302013-09-13 13:15:00 +0800277
Zhao Yakuie92b2972010-12-08 10:10:18 +0800278 /* copy the tx message data */
Lv Zheng6b68f032013-09-13 13:13:30 +0800279 if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800280 dev_WARN_ONCE(tx_msg->device->dev, true,
Lv Zheng6b68f032013-09-13 13:13:30 +0800281 "Unexpected request (msg len %d).\n",
282 buffer->length);
283 return -EINVAL;
284 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800285 msg->data_len = buffer->length;
Lv Zheng6b68f032013-09-13 13:13:30 +0800286 memcpy(tx_msg->data, buffer->data, msg->data_len);
Lv Zheng50065302013-09-13 13:15:00 +0800287
Zhao Yakuie92b2972010-12-08 10:10:18 +0800288 /*
289 * now the default type is SYSTEM_INTERFACE and channel type is BMC.
290 * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
291 * the addr type should be changed to IPMB. Then we will have to parse
292 * the IPMI request message buffer to get the IPMB address.
293 * If so, please fix me.
294 */
295 tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
296 tx_msg->addr.channel = IPMI_BMC_CHANNEL;
297 tx_msg->addr.data[0] = 0;
298
299 /* Get the msgid */
300 device = tx_msg->device;
Lv Zheng50065302013-09-13 13:15:00 +0800301
Lv Zheng06a85662013-09-13 13:13:23 +0800302 spin_lock_irqsave(&device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800303 device->curr_msgid++;
304 tx_msg->tx_msgid = device->curr_msgid;
Lv Zheng06a85662013-09-13 13:13:23 +0800305 spin_unlock_irqrestore(&device->tx_msg_lock, flags);
Lv Zheng50065302013-09-13 13:15:00 +0800306
Lv Zheng6b68f032013-09-13 13:13:30 +0800307 return 0;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800308}
309
310static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
Lv Zheng50065302013-09-13 13:15:00 +0800311 acpi_integer *value)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800312{
313 struct acpi_ipmi_buffer *buffer;
314
315 /*
316 * value is also used as output parameter. It represents the response
317 * IPMI message returned by IPMI command.
318 */
319 buffer = (struct acpi_ipmi_buffer *)value;
Lv Zheng50065302013-09-13 13:15:00 +0800320
Zhao Yakuie92b2972010-12-08 10:10:18 +0800321 /*
Lv Zheng8584ec62013-09-13 13:13:47 +0800322 * If the flag of msg_done is not set, it means that the IPMI command is
323 * not executed correctly.
Zhao Yakuie92b2972010-12-08 10:10:18 +0800324 */
Lv Zheng8584ec62013-09-13 13:13:47 +0800325 buffer->status = msg->msg_done;
326 if (msg->msg_done != ACPI_IPMI_OK)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800327 return;
Lv Zheng50065302013-09-13 13:15:00 +0800328
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;
Lv Zheng2fb037b2013-09-13 13:14:21 +0800392 struct device *dev = ipmi_device->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) {
Lv Zheng50065302013-09-13 13:15:00 +0800396 dev_warn(dev,
397 "Unexpected response is returned. 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 Zheng50065302013-09-13 13:15:00 +0800401
Lv Zheng06a85662013-09-13 13:13:23 +0800402 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
Lv Zheng7b984472013-09-13 13:14:11 +0800403 list_for_each_entry_safe(tx_msg, temp, &ipmi_device->tx_msg_list, head) {
Zhao Yakuie92b2972010-12-08 10:10:18 +0800404 if (msg->msgid == tx_msg->tx_msgid) {
Lv Zheng7b984472013-09-13 13:14:11 +0800405 msg_found = true;
406 list_del(&tx_msg->head);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800407 break;
408 }
409 }
Lv Zheng7b984472013-09-13 13:14:11 +0800410 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800411
Zhao Yakuie92b2972010-12-08 10:10:18 +0800412 if (!msg_found) {
Lv Zheng50065302013-09-13 13:15:00 +0800413 dev_warn(dev,
414 "Unexpected response (msg id %ld) is returned.\n",
415 msg->msgid);
Lv Zheng7b984472013-09-13 13:14:11 +0800416 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800417 }
418
Lv Zheng6b68f032013-09-13 13:13:30 +0800419 /* copy the response data to Rx_data buffer */
420 if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800421 dev_WARN_ONCE(dev, true,
Lv Zheng6b68f032013-09-13 13:13:30 +0800422 "Unexpected response (msg len %d).\n",
423 msg->msg.data_len);
Lv Zheng8584ec62013-09-13 13:13:47 +0800424 goto out_comp;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800425 }
Lv Zheng50065302013-09-13 13:15:00 +0800426
Lv Zheng8584ec62013-09-13 13:13:47 +0800427 /* response msg is an error msg */
428 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
429 if (msg->recv_type == IPMI_RESPONSE_RECV_TYPE &&
430 msg->msg.data_len == 1) {
431 if (msg->msg.data[0] == IPMI_TIMEOUT_COMPLETION_CODE) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800432 dev_WARN_ONCE(dev, true,
Lv Zheng8584ec62013-09-13 13:13:47 +0800433 "Unexpected response (timeout).\n");
434 tx_msg->msg_done = ACPI_IPMI_TIMEOUT;
435 }
436 goto out_comp;
437 }
Lv Zheng50065302013-09-13 13:15:00 +0800438
Lv Zheng8584ec62013-09-13 13:13:47 +0800439 tx_msg->rx_len = msg->msg.data_len;
440 memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
441 tx_msg->msg_done = ACPI_IPMI_OK;
Lv Zheng50065302013-09-13 13:15:00 +0800442
Lv Zheng8584ec62013-09-13 13:13:47 +0800443out_comp:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800444 complete(&tx_msg->tx_complete);
Lv Zheng7b984472013-09-13 13:14:11 +0800445 acpi_ipmi_msg_put(tx_msg);
Lv Zheng6b68f032013-09-13 13:13:30 +0800446out_msg:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800447 ipmi_free_recv_msg(msg);
Lv Zheng50065302013-09-13 13:15:00 +0800448}
Zhao Yakuie92b2972010-12-08 10:10:18 +0800449
450static void ipmi_register_bmc(int iface, struct device *dev)
451{
452 struct acpi_ipmi_device *ipmi_device, *temp;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800453 int err;
454 struct ipmi_smi_info smi_data;
455 acpi_handle handle;
456
457 err = ipmi_get_smi_info(iface, &smi_data);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800458 if (err)
459 return;
460
Lv Zhenga1a69b22013-09-13 13:13:54 +0800461 if (smi_data.addr_src != SI_ACPI)
462 goto err_ref;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800463 handle = smi_data.addr_info.acpi_info.acpi_handle;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800464 if (!handle)
465 goto err_ref;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800466
Lv Zheng2fb037b2013-09-13 13:14:21 +0800467 ipmi_device = ipmi_dev_alloc(iface, smi_data.dev, handle);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800468 if (!ipmi_device) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800469 dev_warn(smi_data.dev, "Can't create IPMI user interface\n");
Lv Zhenga1a69b22013-09-13 13:13:54 +0800470 goto err_ref;
471 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800472
473 mutex_lock(&driver_data.ipmi_lock);
474 list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
475 /*
476 * if the corresponding ACPI handle is already added
477 * to the device list, don't add it again.
478 */
479 if (temp->handle == handle)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800480 goto err_lock;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800481 }
Lv Zhenge96a94e2013-09-13 13:14:02 +0800482 if (!driver_data.selected_smi)
483 driver_data.selected_smi = ipmi_device;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800484 list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800485 mutex_unlock(&driver_data.ipmi_lock);
Lv Zheng50065302013-09-13 13:15:00 +0800486
Lv Zhenga1a69b22013-09-13 13:13:54 +0800487 put_device(smi_data.dev);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800488 return;
489
Lv Zhenga1a69b22013-09-13 13:13:54 +0800490err_lock:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800491 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800492 ipmi_dev_release(ipmi_device);
493err_ref:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800494 put_device(smi_data.dev);
495 return;
496}
497
498static void ipmi_bmc_gone(int iface)
499{
500 struct acpi_ipmi_device *ipmi_device, *temp;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800501 bool dev_found = false;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800502
503 mutex_lock(&driver_data.ipmi_lock);
504 list_for_each_entry_safe(ipmi_device, temp,
Lv Zheng50065302013-09-13 13:15:00 +0800505 &driver_data.ipmi_devices, head) {
Lv Zhenga1a69b22013-09-13 13:13:54 +0800506 if (ipmi_device->ipmi_ifnum != iface) {
507 dev_found = true;
508 __ipmi_dev_kill(ipmi_device);
509 break;
510 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800511 }
Lv Zhenge96a94e2013-09-13 13:14:02 +0800512 if (!driver_data.selected_smi)
513 driver_data.selected_smi = list_first_entry_or_null(
514 &driver_data.ipmi_devices,
515 struct acpi_ipmi_device, head);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800516 mutex_unlock(&driver_data.ipmi_lock);
Lv Zheng50065302013-09-13 13:15:00 +0800517
Lv Zhenga1a69b22013-09-13 13:13:54 +0800518 if (dev_found) {
519 ipmi_flush_tx_msg(ipmi_device);
520 acpi_ipmi_dev_put(ipmi_device);
521 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800522}
Lv Zheng50065302013-09-13 13:15:00 +0800523
Zhao Yakuie92b2972010-12-08 10:10:18 +0800524/*
525 * This is the IPMI opregion space handler.
526 * @function: indicates the read/write. In fact as the IPMI message is driven
527 * by command, only write is meaningful.
528 * @address: This contains the netfn/command of IPMI request message.
529 * @bits : not used.
530 * @value : it is an in/out parameter. It points to the IPMI message buffer.
531 * Before the IPMI message is sent, it represents the actual request
532 * IPMI message. After the IPMI message is finished, it represents
533 * the response IPMI message returned by IPMI command.
534 * @handler_context: IPMI device context.
535 */
Zhao Yakuie92b2972010-12-08 10:10:18 +0800536static acpi_status
537acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
Lv Zheng50065302013-09-13 13:15:00 +0800538 u32 bits, acpi_integer *value,
539 void *handler_context, void *region_context)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800540{
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;
Lv Zheng50065302013-09-13 13:15:00 +0800546
Zhao Yakuie92b2972010-12-08 10:10:18 +0800547 /*
548 * IPMI opregion message.
549 * IPMI message is firstly written to the BMC and system software
550 * can get the respsonse. So it is unmeaningful for the read access
551 * of IPMI opregion.
552 */
553 if ((function & ACPI_IO_MASK) == ACPI_READ)
554 return AE_TYPE;
555
Lv Zheng7b984472013-09-13 13:14:11 +0800556 tx_msg = ipmi_msg_alloc();
557 if (!tx_msg)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800558 return AE_NOT_EXIST;
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 Zheng50065302013-09-13 13:15:00 +0800565
Lv Zheng7b984472013-09-13 13:14:11 +0800566 acpi_ipmi_msg_get(tx_msg);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800567 mutex_lock(&driver_data.ipmi_lock);
568 /* Do not add a tx_msg that can not be flushed. */
569 if (ipmi_device->dead) {
Lv Zhenga1a69b22013-09-13 13:13:54 +0800570 mutex_unlock(&driver_data.ipmi_lock);
Lv Zheng7b984472013-09-13 13:14:11 +0800571 ipmi_msg_release(tx_msg);
572 return AE_NOT_EXIST;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800573 }
Lv Zheng06a85662013-09-13 13:13:23 +0800574 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800575 list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
Lv Zheng06a85662013-09-13 13:13:23 +0800576 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800577 mutex_unlock(&driver_data.ipmi_lock);
Lv Zheng50065302013-09-13 13:15:00 +0800578
Zhao Yakuie92b2972010-12-08 10:10:18 +0800579 err = ipmi_request_settime(ipmi_device->user_interface,
Lv Zheng50065302013-09-13 13:15:00 +0800580 &tx_msg->addr,
581 tx_msg->tx_msgid,
582 &tx_msg->tx_message,
583 NULL, 0, 0, IPMI_TIMEOUT);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800584 if (err) {
585 status = AE_ERROR;
Lv Zheng7b984472013-09-13 13:14:11 +0800586 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800587 }
Lv Zheng8584ec62013-09-13 13:13:47 +0800588 wait_for_completion(&tx_msg->tx_complete);
Lv Zheng50065302013-09-13 13:15:00 +0800589
Lv Zheng8584ec62013-09-13 13:13:47 +0800590 acpi_format_ipmi_response(tx_msg, value);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800591 status = AE_OK;
592
Lv Zheng6b68f032013-09-13 13:13:30 +0800593out_msg:
Lv Zheng7b984472013-09-13 13:14:11 +0800594 ipmi_cancel_tx_msg(ipmi_device, tx_msg);
595 acpi_ipmi_msg_put(tx_msg);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800596 return status;
597}
598
Zhao Yakuie92b2972010-12-08 10:10:18 +0800599static int __init acpi_ipmi_init(void)
600{
Lv Zhenga194aa42013-09-13 13:14:31 +0800601 int result;
Lv Zhenge96a94e2013-09-13 13:14:02 +0800602 acpi_status status;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800603
604 if (acpi_disabled)
Lv Zhenga194aa42013-09-13 13:14:31 +0800605 return 0;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800606
Lv Zhenge96a94e2013-09-13 13:14:02 +0800607 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
Lv Zheng50065302013-09-13 13:15:00 +0800608 ACPI_ADR_SPACE_IPMI,
609 &acpi_ipmi_space_handler,
610 NULL, NULL);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800611 if (ACPI_FAILURE(status)) {
612 pr_warn("Can't register IPMI opregion space handle\n");
613 return -EINVAL;
614 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800615 result = ipmi_smi_watcher_register(&driver_data.bmc_events);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800616 if (result)
617 pr_err("Can't register IPMI system interface watcher\n");
Zhao Yakuie92b2972010-12-08 10:10:18 +0800618
619 return result;
620}
621
622static void __exit acpi_ipmi_exit(void)
623{
Lv Zhenga1a69b22013-09-13 13:13:54 +0800624 struct acpi_ipmi_device *ipmi_device;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800625
626 if (acpi_disabled)
627 return;
628
629 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
630
631 /*
632 * When one smi_watcher is unregistered, it is only deleted
633 * from the smi_watcher list. But the smi_gone callback function
634 * is not called. So explicitly uninstall the ACPI IPMI oregion
635 * handler and free it.
636 */
637 mutex_lock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800638 while (!list_empty(&driver_data.ipmi_devices)) {
639 ipmi_device = list_first_entry(&driver_data.ipmi_devices,
640 struct acpi_ipmi_device,
641 head);
642 __ipmi_dev_kill(ipmi_device);
643 mutex_unlock(&driver_data.ipmi_lock);
644
645 ipmi_flush_tx_msg(ipmi_device);
646 acpi_ipmi_dev_put(ipmi_device);
647
648 mutex_lock(&driver_data.ipmi_lock);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800649 }
650 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800651 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
Lv Zheng50065302013-09-13 13:15:00 +0800652 ACPI_ADR_SPACE_IPMI,
653 &acpi_ipmi_space_handler);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800654}
655
656module_init(acpi_ipmi_init);
657module_exit(acpi_ipmi_exit);