blob: f7b6598bd7c2a11bce0ee7004b6d0d34645b2526 [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;
Lv Zheng2fb037b2013-09-13 13:14:21 +080064 struct device *dev;
Zhao Yakuie92b2972010-12-08 10:10:18 +080065 ipmi_user_t user_interface;
66 int ipmi_ifnum; /* IPMI interface number */
67 long curr_msgid;
Lv Zhenga1a69b22013-09-13 13:13:54 +080068 bool dead;
69 struct kref kref;
Zhao Yakuie92b2972010-12-08 10:10:18 +080070};
71
72struct ipmi_driver_data {
73 struct list_head ipmi_devices;
74 struct ipmi_smi_watcher bmc_events;
75 struct ipmi_user_hndl ipmi_hndlrs;
76 struct mutex ipmi_lock;
Lv Zhenge96a94e2013-09-13 13:14:02 +080077 /*
78 * NOTE: IPMI System Interface Selection
79 * There is no system interface specified by the IPMI operation
80 * region access. We try to select one system interface with ACPI
81 * handle set. IPMI messages passed from the ACPI codes are sent
82 * to this selected global IPMI system interface.
83 */
84 struct acpi_ipmi_device *selected_smi;
Zhao Yakuie92b2972010-12-08 10:10:18 +080085};
86
87struct acpi_ipmi_msg {
88 struct list_head head;
89 /*
90 * General speaking the addr type should be SI_ADDR_TYPE. And
91 * the addr channel should be BMC.
92 * In fact it can also be IPMB type. But we will have to
93 * parse it from the Netfn command buffer. It is so complex
94 * that it is skipped.
95 */
96 struct ipmi_addr addr;
97 long tx_msgid;
98 /* it is used to track whether the IPMI message is finished */
99 struct completion tx_complete;
100 struct kernel_ipmi_msg tx_message;
101 int msg_done;
Lv Zheng6b68f032013-09-13 13:13:30 +0800102 /* tx/rx data . And copy it from/to ACPI object buffer */
103 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
104 u8 rx_len;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800105 struct acpi_ipmi_device *device;
Lv Zheng7b984472013-09-13 13:14:11 +0800106 struct kref kref;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800107};
108
109/* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
110struct acpi_ipmi_buffer {
111 u8 status;
112 u8 length;
Lv Zheng6b68f032013-09-13 13:13:30 +0800113 u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
Zhao Yakuie92b2972010-12-08 10:10:18 +0800114};
115
116static void ipmi_register_bmc(int iface, struct device *dev);
117static void ipmi_bmc_gone(int iface);
118static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800119
120static struct ipmi_driver_data driver_data = {
121 .ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
122 .bmc_events = {
123 .owner = THIS_MODULE,
124 .new_smi = ipmi_register_bmc,
125 .smi_gone = ipmi_bmc_gone,
126 },
127 .ipmi_hndlrs = {
128 .ipmi_recv_hndl = ipmi_msg_handler,
129 },
Lv Zhenga194aa42013-09-13 13:14:31 +0800130 .ipmi_lock = __MUTEX_INITIALIZER(driver_data.ipmi_lock)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800131};
132
Lv Zhenga1a69b22013-09-13 13:13:54 +0800133static struct acpi_ipmi_device *
Lv Zheng2fb037b2013-09-13 13:14:21 +0800134ipmi_dev_alloc(int iface, struct device *dev, acpi_handle handle)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800135{
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;
Lv Zheng2fb037b2013-09-13 13:14:21 +0800150 ipmi_device->dev = get_device(dev);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800151 ipmi_device->ipmi_ifnum = iface;
152
153 err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
154 ipmi_device, &user);
155 if (err) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800156 put_device(dev);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800157 kfree(ipmi_device);
158 return NULL;
159 }
160 ipmi_device->user_interface = user;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800161
162 return ipmi_device;
163}
164
165static void ipmi_dev_release(struct acpi_ipmi_device *ipmi_device)
166{
Lv Zhenga1a69b22013-09-13 13:13:54 +0800167 ipmi_destroy_user(ipmi_device->user_interface);
Lv Zheng2fb037b2013-09-13 13:14:21 +0800168 put_device(ipmi_device->dev);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800169 kfree(ipmi_device);
170}
171
172static void ipmi_dev_release_kref(struct kref *kref)
173{
174 struct acpi_ipmi_device *ipmi =
175 container_of(kref, struct acpi_ipmi_device, kref);
176
177 ipmi_dev_release(ipmi);
178}
179
180static void __ipmi_dev_kill(struct acpi_ipmi_device *ipmi_device)
181{
182 list_del(&ipmi_device->head);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800183 if (driver_data.selected_smi == ipmi_device)
184 driver_data.selected_smi = NULL;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800185 /*
186 * Always setting dead flag after deleting from the list or
187 * list_for_each_entry() codes must get changed.
188 */
189 ipmi_device->dead = true;
190}
191
Lv Zhenge96a94e2013-09-13 13:14:02 +0800192static struct acpi_ipmi_device *acpi_ipmi_dev_get(void)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800193{
Lv Zhenge96a94e2013-09-13 13:14:02 +0800194 struct acpi_ipmi_device *ipmi_device = NULL;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800195
196 mutex_lock(&driver_data.ipmi_lock);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800197 if (driver_data.selected_smi) {
198 ipmi_device = driver_data.selected_smi;
199 kref_get(&ipmi_device->kref);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800200 }
201 mutex_unlock(&driver_data.ipmi_lock);
202
203 return ipmi_device;
204}
205
206static void acpi_ipmi_dev_put(struct acpi_ipmi_device *ipmi_device)
207{
208 kref_put(&ipmi_device->kref, ipmi_dev_release_kref);
209}
210
Lv Zheng7b984472013-09-13 13:14:11 +0800211static struct acpi_ipmi_msg *ipmi_msg_alloc(void)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800212{
Lv Zheng7b984472013-09-13 13:14:11 +0800213 struct acpi_ipmi_device *ipmi;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800214 struct acpi_ipmi_msg *ipmi_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800215
Lv Zheng7b984472013-09-13 13:14:11 +0800216 ipmi = acpi_ipmi_dev_get();
217 if (!ipmi)
218 return NULL;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800219 ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
Lv Zheng7b984472013-09-13 13:14:11 +0800220 if (!ipmi_msg) {
221 acpi_ipmi_dev_put(ipmi);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800222 return NULL;
223 }
Lv Zheng7b984472013-09-13 13:14:11 +0800224 kref_init(&ipmi_msg->kref);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800225 init_completion(&ipmi_msg->tx_complete);
226 INIT_LIST_HEAD(&ipmi_msg->head);
227 ipmi_msg->device = ipmi;
Lv Zheng8584ec62013-09-13 13:13:47 +0800228 ipmi_msg->msg_done = ACPI_IPMI_UNKNOWN;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800229 return ipmi_msg;
230}
231
Lv Zheng7b984472013-09-13 13:14:11 +0800232static void ipmi_msg_release(struct acpi_ipmi_msg *tx_msg)
233{
234 acpi_ipmi_dev_put(tx_msg->device);
235 kfree(tx_msg);
236}
237
238static void ipmi_msg_release_kref(struct kref *kref)
239{
240 struct acpi_ipmi_msg *tx_msg =
241 container_of(kref, struct acpi_ipmi_msg, kref);
242
243 ipmi_msg_release(tx_msg);
244}
245
246static struct acpi_ipmi_msg *acpi_ipmi_msg_get(struct acpi_ipmi_msg *tx_msg)
247{
248 kref_get(&tx_msg->kref);
249
250 return tx_msg;
251}
252
253static void acpi_ipmi_msg_put(struct acpi_ipmi_msg *tx_msg)
254{
255 kref_put(&tx_msg->kref, ipmi_msg_release_kref);
256}
257
Zhao Yakuie92b2972010-12-08 10:10:18 +0800258#define IPMI_OP_RGN_NETFN(offset) ((offset >> 8) & 0xff)
259#define IPMI_OP_RGN_CMD(offset) (offset & 0xff)
Lv Zheng6b68f032013-09-13 13:13:30 +0800260static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
Zhao Yakuie92b2972010-12-08 10:10:18 +0800261 acpi_physical_address address,
262 acpi_integer *value)
263{
264 struct kernel_ipmi_msg *msg;
265 struct acpi_ipmi_buffer *buffer;
266 struct acpi_ipmi_device *device;
Lv Zheng06a85662013-09-13 13:13:23 +0800267 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800268
269 msg = &tx_msg->tx_message;
270 /*
271 * IPMI network function and command are encoded in the address
272 * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
273 */
274 msg->netfn = IPMI_OP_RGN_NETFN(address);
275 msg->cmd = IPMI_OP_RGN_CMD(address);
Lv Zheng6b68f032013-09-13 13:13:30 +0800276 msg->data = tx_msg->data;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800277 /*
278 * value is the parameter passed by the IPMI opregion space handler.
279 * It points to the IPMI request message buffer
280 */
281 buffer = (struct acpi_ipmi_buffer *)value;
282 /* copy the tx message data */
Lv Zheng6b68f032013-09-13 13:13:30 +0800283 if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800284 dev_WARN_ONCE(tx_msg->device->dev, true,
Lv Zheng6b68f032013-09-13 13:13:30 +0800285 "Unexpected request (msg len %d).\n",
286 buffer->length);
287 return -EINVAL;
288 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800289 msg->data_len = buffer->length;
Lv Zheng6b68f032013-09-13 13:13:30 +0800290 memcpy(tx_msg->data, buffer->data, msg->data_len);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800291 /*
292 * now the default type is SYSTEM_INTERFACE and channel type is BMC.
293 * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
294 * the addr type should be changed to IPMB. Then we will have to parse
295 * the IPMI request message buffer to get the IPMB address.
296 * If so, please fix me.
297 */
298 tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
299 tx_msg->addr.channel = IPMI_BMC_CHANNEL;
300 tx_msg->addr.data[0] = 0;
301
302 /* Get the msgid */
303 device = tx_msg->device;
Lv Zheng06a85662013-09-13 13:13:23 +0800304 spin_lock_irqsave(&device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800305 device->curr_msgid++;
306 tx_msg->tx_msgid = device->curr_msgid;
Lv Zheng06a85662013-09-13 13:13:23 +0800307 spin_unlock_irqrestore(&device->tx_msg_lock, flags);
Lv Zheng6b68f032013-09-13 13:13:30 +0800308 return 0;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800309}
310
311static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
Lv Zheng8584ec62013-09-13 13:13:47 +0800312 acpi_integer *value)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800313{
314 struct acpi_ipmi_buffer *buffer;
315
316 /*
317 * value is also used as output parameter. It represents the response
318 * IPMI message returned by IPMI command.
319 */
320 buffer = (struct acpi_ipmi_buffer *)value;
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;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800328 /*
329 * If the IPMI response message is obtained correctly, the status code
330 * will be ACPI_IPMI_OK
331 */
Zhao Yakuie92b2972010-12-08 10:10:18 +0800332 buffer->length = msg->rx_len;
Lv Zheng6b68f032013-09-13 13:13:30 +0800333 memcpy(buffer->data, msg->data, msg->rx_len);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800334}
335
336static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
337{
Lv Zheng7b984472013-09-13 13:14:11 +0800338 struct acpi_ipmi_msg *tx_msg;
Lv Zheng5ac557e2013-09-13 13:13:39 +0800339 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800340
Lv Zhenga1a69b22013-09-13 13:13:54 +0800341 /*
342 * NOTE: On-going ipmi_recv_msg
343 * ipmi_msg_handler() may still be invoked by ipmi_si after
344 * flushing. But it is safe to do a fast flushing on module_exit()
345 * without waiting for all ipmi_recv_msg(s) to complete from
346 * ipmi_msg_handler() as it is ensured by ipmi_si that all
347 * ipmi_recv_msg(s) are freed after invoking ipmi_destroy_user().
348 */
Lv Zheng5ac557e2013-09-13 13:13:39 +0800349 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
Lv Zheng7b984472013-09-13 13:14:11 +0800350 while (!list_empty(&ipmi->tx_msg_list)) {
351 tx_msg = list_first_entry(&ipmi->tx_msg_list,
352 struct acpi_ipmi_msg,
353 head);
354 list_del(&tx_msg->head);
355 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
356
Zhao Yakuie92b2972010-12-08 10:10:18 +0800357 /* wake up the sleep thread on the Tx msg */
358 complete(&tx_msg->tx_complete);
Lv Zheng7b984472013-09-13 13:14:11 +0800359 acpi_ipmi_msg_put(tx_msg);
360 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800361 }
Lv Zheng5ac557e2013-09-13 13:13:39 +0800362 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800363}
364
Lv Zheng7b984472013-09-13 13:14:11 +0800365static void ipmi_cancel_tx_msg(struct acpi_ipmi_device *ipmi,
366 struct acpi_ipmi_msg *msg)
367{
368 struct acpi_ipmi_msg *tx_msg, *temp;
369 bool msg_found = false;
370 unsigned long flags;
371
372 spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
373 list_for_each_entry_safe(tx_msg, temp, &ipmi->tx_msg_list, head) {
374 if (msg == tx_msg) {
375 msg_found = true;
376 list_del(&tx_msg->head);
377 break;
378 }
379 }
380 spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
381
382 if (msg_found)
383 acpi_ipmi_msg_put(tx_msg);
384}
385
Zhao Yakuie92b2972010-12-08 10:10:18 +0800386static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
387{
388 struct acpi_ipmi_device *ipmi_device = user_msg_data;
Lv Zheng7b984472013-09-13 13:14:11 +0800389 bool msg_found = false;
390 struct acpi_ipmi_msg *tx_msg, *temp;
Lv Zheng2fb037b2013-09-13 13:14:21 +0800391 struct device *dev = ipmi_device->dev;
Lv Zheng06a85662013-09-13 13:13:23 +0800392 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800393
394 if (msg->user != ipmi_device->user_interface) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800395 dev_warn(dev, "Unexpected response is returned. "
Zhao Yakuie92b2972010-12-08 10:10:18 +0800396 "returned user %p, expected user %p\n",
397 msg->user, ipmi_device->user_interface);
Lv Zheng6b68f032013-09-13 13:13:30 +0800398 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800399 }
Lv Zheng06a85662013-09-13 13:13:23 +0800400 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
Lv Zheng7b984472013-09-13 13:14:11 +0800401 list_for_each_entry_safe(tx_msg, temp, &ipmi_device->tx_msg_list, head) {
Zhao Yakuie92b2972010-12-08 10:10:18 +0800402 if (msg->msgid == tx_msg->tx_msgid) {
Lv Zheng7b984472013-09-13 13:14:11 +0800403 msg_found = true;
404 list_del(&tx_msg->head);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800405 break;
406 }
407 }
Lv Zheng7b984472013-09-13 13:14:11 +0800408 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800409
Zhao Yakuie92b2972010-12-08 10:10:18 +0800410 if (!msg_found) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800411 dev_warn(dev, "Unexpected response (msg id %ld) is "
Zhao Yakuie92b2972010-12-08 10:10:18 +0800412 "returned.\n", msg->msgid);
Lv Zheng7b984472013-09-13 13:14:11 +0800413 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800414 }
415
Lv Zheng6b68f032013-09-13 13:13:30 +0800416 /* copy the response data to Rx_data buffer */
417 if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800418 dev_WARN_ONCE(dev, true,
Lv Zheng6b68f032013-09-13 13:13:30 +0800419 "Unexpected response (msg len %d).\n",
420 msg->msg.data_len);
Lv Zheng8584ec62013-09-13 13:13:47 +0800421 goto out_comp;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800422 }
Lv Zheng8584ec62013-09-13 13:13:47 +0800423 /* response msg is an error msg */
424 msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
425 if (msg->recv_type == IPMI_RESPONSE_RECV_TYPE &&
426 msg->msg.data_len == 1) {
427 if (msg->msg.data[0] == IPMI_TIMEOUT_COMPLETION_CODE) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800428 dev_WARN_ONCE(dev, true,
Lv Zheng8584ec62013-09-13 13:13:47 +0800429 "Unexpected response (timeout).\n");
430 tx_msg->msg_done = ACPI_IPMI_TIMEOUT;
431 }
432 goto out_comp;
433 }
434 tx_msg->rx_len = msg->msg.data_len;
435 memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
436 tx_msg->msg_done = ACPI_IPMI_OK;
437out_comp:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800438 complete(&tx_msg->tx_complete);
Lv Zheng7b984472013-09-13 13:14:11 +0800439 acpi_ipmi_msg_put(tx_msg);
Lv Zheng6b68f032013-09-13 13:13:30 +0800440out_msg:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800441 ipmi_free_recv_msg(msg);
442};
443
444static void ipmi_register_bmc(int iface, struct device *dev)
445{
446 struct acpi_ipmi_device *ipmi_device, *temp;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800447 int err;
448 struct ipmi_smi_info smi_data;
449 acpi_handle handle;
450
451 err = ipmi_get_smi_info(iface, &smi_data);
452
453 if (err)
454 return;
455
Lv Zhenga1a69b22013-09-13 13:13:54 +0800456 if (smi_data.addr_src != SI_ACPI)
457 goto err_ref;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800458 handle = smi_data.addr_info.acpi_info.acpi_handle;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800459 if (!handle)
460 goto err_ref;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800461
Lv Zheng2fb037b2013-09-13 13:14:21 +0800462 ipmi_device = ipmi_dev_alloc(iface, smi_data.dev, handle);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800463 if (!ipmi_device) {
Lv Zheng2fb037b2013-09-13 13:14:21 +0800464 dev_warn(smi_data.dev, "Can't create IPMI user interface\n");
Lv Zhenga1a69b22013-09-13 13:13:54 +0800465 goto err_ref;
466 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800467
468 mutex_lock(&driver_data.ipmi_lock);
469 list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
470 /*
471 * if the corresponding ACPI handle is already added
472 * to the device list, don't add it again.
473 */
474 if (temp->handle == handle)
Lv Zhenga1a69b22013-09-13 13:13:54 +0800475 goto err_lock;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800476 }
477
Lv Zhenge96a94e2013-09-13 13:14:02 +0800478 if (!driver_data.selected_smi)
479 driver_data.selected_smi = ipmi_device;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800480 list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800481 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800482 put_device(smi_data.dev);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800483 return;
484
Lv Zhenga1a69b22013-09-13 13:13:54 +0800485err_lock:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800486 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800487 ipmi_dev_release(ipmi_device);
488err_ref:
Zhao Yakuie92b2972010-12-08 10:10:18 +0800489 put_device(smi_data.dev);
490 return;
491}
492
493static void ipmi_bmc_gone(int iface)
494{
495 struct acpi_ipmi_device *ipmi_device, *temp;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800496 bool dev_found = false;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800497
498 mutex_lock(&driver_data.ipmi_lock);
499 list_for_each_entry_safe(ipmi_device, temp,
500 &driver_data.ipmi_devices, head) {
Lv Zhenga1a69b22013-09-13 13:13:54 +0800501 if (ipmi_device->ipmi_ifnum != iface) {
502 dev_found = true;
503 __ipmi_dev_kill(ipmi_device);
504 break;
505 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800506 }
Lv Zhenge96a94e2013-09-13 13:14:02 +0800507 if (!driver_data.selected_smi)
508 driver_data.selected_smi = list_first_entry_or_null(
509 &driver_data.ipmi_devices,
510 struct acpi_ipmi_device, head);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800511 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800512 if (dev_found) {
513 ipmi_flush_tx_msg(ipmi_device);
514 acpi_ipmi_dev_put(ipmi_device);
515 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800516}
517/* --------------------------------------------------------------------------
518 * Address Space Management
519 * -------------------------------------------------------------------------- */
520/*
521 * This is the IPMI opregion space handler.
522 * @function: indicates the read/write. In fact as the IPMI message is driven
523 * by command, only write is meaningful.
524 * @address: This contains the netfn/command of IPMI request message.
525 * @bits : not used.
526 * @value : it is an in/out parameter. It points to the IPMI message buffer.
527 * Before the IPMI message is sent, it represents the actual request
528 * IPMI message. After the IPMI message is finished, it represents
529 * the response IPMI message returned by IPMI command.
530 * @handler_context: IPMI device context.
531 */
532
533static acpi_status
534acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
535 u32 bits, acpi_integer *value,
536 void *handler_context, void *region_context)
537{
538 struct acpi_ipmi_msg *tx_msg;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800539 struct acpi_ipmi_device *ipmi_device;
Lv Zheng8584ec62013-09-13 13:13:47 +0800540 int err;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800541 acpi_status status;
Lv Zheng06a85662013-09-13 13:13:23 +0800542 unsigned long flags;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800543 /*
544 * IPMI opregion message.
545 * IPMI message is firstly written to the BMC and system software
546 * can get the respsonse. So it is unmeaningful for the read access
547 * of IPMI opregion.
548 */
549 if ((function & ACPI_IO_MASK) == ACPI_READ)
550 return AE_TYPE;
551
Lv Zheng7b984472013-09-13 13:14:11 +0800552 tx_msg = ipmi_msg_alloc();
553 if (!tx_msg)
Zhao Yakuie92b2972010-12-08 10:10:18 +0800554 return AE_NOT_EXIST;
555
Lv Zheng7b984472013-09-13 13:14:11 +0800556 ipmi_device = tx_msg->device;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800557
Lv Zheng6b68f032013-09-13 13:13:30 +0800558 if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
Lv Zheng7b984472013-09-13 13:14:11 +0800559 ipmi_msg_release(tx_msg);
560 return AE_TYPE;
Lv Zheng6b68f032013-09-13 13:13:30 +0800561 }
Lv Zheng7b984472013-09-13 13:14:11 +0800562 acpi_ipmi_msg_get(tx_msg);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800563 mutex_lock(&driver_data.ipmi_lock);
564 /* Do not add a tx_msg that can not be flushed. */
565 if (ipmi_device->dead) {
Lv Zhenga1a69b22013-09-13 13:13:54 +0800566 mutex_unlock(&driver_data.ipmi_lock);
Lv Zheng7b984472013-09-13 13:14:11 +0800567 ipmi_msg_release(tx_msg);
568 return AE_NOT_EXIST;
Lv Zhenga1a69b22013-09-13 13:13:54 +0800569 }
Lv Zheng06a85662013-09-13 13:13:23 +0800570 spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800571 list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
Lv Zheng06a85662013-09-13 13:13:23 +0800572 spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800573 mutex_unlock(&driver_data.ipmi_lock);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800574 err = ipmi_request_settime(ipmi_device->user_interface,
575 &tx_msg->addr,
576 tx_msg->tx_msgid,
577 &tx_msg->tx_message,
Lv Zheng8584ec62013-09-13 13:13:47 +0800578 NULL, 0, 0, IPMI_TIMEOUT);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800579 if (err) {
580 status = AE_ERROR;
Lv Zheng7b984472013-09-13 13:14:11 +0800581 goto out_msg;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800582 }
Lv Zheng8584ec62013-09-13 13:13:47 +0800583 wait_for_completion(&tx_msg->tx_complete);
584 acpi_format_ipmi_response(tx_msg, value);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800585 status = AE_OK;
586
Lv Zheng6b68f032013-09-13 13:13:30 +0800587out_msg:
Lv Zheng7b984472013-09-13 13:14:11 +0800588 ipmi_cancel_tx_msg(ipmi_device, tx_msg);
589 acpi_ipmi_msg_put(tx_msg);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800590 return status;
591}
592
Zhao Yakuie92b2972010-12-08 10:10:18 +0800593static int __init acpi_ipmi_init(void)
594{
Lv Zhenga194aa42013-09-13 13:14:31 +0800595 int result;
Lv Zhenge96a94e2013-09-13 13:14:02 +0800596 acpi_status status;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800597
598 if (acpi_disabled)
Lv Zhenga194aa42013-09-13 13:14:31 +0800599 return 0;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800600
Lv Zhenge96a94e2013-09-13 13:14:02 +0800601 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
602 ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler,
603 NULL, NULL);
604 if (ACPI_FAILURE(status)) {
605 pr_warn("Can't register IPMI opregion space handle\n");
606 return -EINVAL;
607 }
Zhao Yakuie92b2972010-12-08 10:10:18 +0800608 result = ipmi_smi_watcher_register(&driver_data.bmc_events);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800609 if (result)
610 pr_err("Can't register IPMI system interface watcher\n");
Zhao Yakuie92b2972010-12-08 10:10:18 +0800611
612 return result;
613}
614
615static void __exit acpi_ipmi_exit(void)
616{
Lv Zhenga1a69b22013-09-13 13:13:54 +0800617 struct acpi_ipmi_device *ipmi_device;
Zhao Yakuie92b2972010-12-08 10:10:18 +0800618
619 if (acpi_disabled)
620 return;
621
622 ipmi_smi_watcher_unregister(&driver_data.bmc_events);
623
624 /*
625 * When one smi_watcher is unregistered, it is only deleted
626 * from the smi_watcher list. But the smi_gone callback function
627 * is not called. So explicitly uninstall the ACPI IPMI oregion
628 * handler and free it.
629 */
630 mutex_lock(&driver_data.ipmi_lock);
Lv Zhenga1a69b22013-09-13 13:13:54 +0800631 while (!list_empty(&driver_data.ipmi_devices)) {
632 ipmi_device = list_first_entry(&driver_data.ipmi_devices,
633 struct acpi_ipmi_device,
634 head);
635 __ipmi_dev_kill(ipmi_device);
636 mutex_unlock(&driver_data.ipmi_lock);
637
638 ipmi_flush_tx_msg(ipmi_device);
639 acpi_ipmi_dev_put(ipmi_device);
640
641 mutex_lock(&driver_data.ipmi_lock);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800642 }
643 mutex_unlock(&driver_data.ipmi_lock);
Lv Zhenge96a94e2013-09-13 13:14:02 +0800644 acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
645 ACPI_ADR_SPACE_IPMI, &acpi_ipmi_space_handler);
Zhao Yakuie92b2972010-12-08 10:10:18 +0800646}
647
648module_init(acpi_ipmi_init);
649module_exit(acpi_ipmi_exit);