xref: /openbmc/linux/drivers/acpi/acpi_ipmi.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e92b297cSZhao Yakui /*
3e92b297cSZhao Yakui  *  acpi_ipmi.c - ACPI IPMI opregion
4e92b297cSZhao Yakui  *
5a1a69b29SLv Zheng  *  Copyright (C) 2010, 2013 Intel Corporation
6a1a69b29SLv Zheng  *    Author: Zhao Yakui <yakui.zhao@intel.com>
7a1a69b29SLv Zheng  *            Lv Zheng <lv.zheng@intel.com>
8e92b297cSZhao Yakui  */
9e92b297cSZhao Yakui 
10e92b297cSZhao Yakui #include <linux/module.h>
11935a9e3fSLv Zheng #include <linux/acpi.h>
12e92b297cSZhao Yakui #include <linux/ipmi.h>
1306a8566bSLv Zheng #include <linux/spinlock.h>
14e92b297cSZhao Yakui 
15e92b297cSZhao Yakui MODULE_AUTHOR("Zhao Yakui");
16e92b297cSZhao Yakui MODULE_DESCRIPTION("ACPI IPMI Opregion driver");
17e92b297cSZhao Yakui MODULE_LICENSE("GPL");
18e92b297cSZhao Yakui 
19e92b297cSZhao Yakui #define ACPI_IPMI_OK			0
20e92b297cSZhao Yakui #define ACPI_IPMI_TIMEOUT		0x10
21e92b297cSZhao Yakui #define ACPI_IPMI_UNKNOWN		0x07
22e92b297cSZhao Yakui /* the IPMI timeout is 5s */
238584ec6aSLv Zheng #define IPMI_TIMEOUT			(5000)
246b68f03fSLv Zheng #define ACPI_IPMI_MAX_MSG_LENGTH	64
25e92b297cSZhao Yakui 
26e92b297cSZhao Yakui struct acpi_ipmi_device {
27e92b297cSZhao Yakui 	/* the device list attached to driver_data.ipmi_devices */
28e92b297cSZhao Yakui 	struct list_head head;
2950065300SLv Zheng 
30e92b297cSZhao Yakui 	/* the IPMI request message list */
31e92b297cSZhao Yakui 	struct list_head tx_msg_list;
3250065300SLv Zheng 
3306a8566bSLv Zheng 	spinlock_t tx_msg_lock;
34e92b297cSZhao Yakui 	acpi_handle handle;
352fb037b8SLv Zheng 	struct device *dev;
36ebba75fdSCorey Minyard 	struct ipmi_user *user_interface;
37e92b297cSZhao Yakui 	int ipmi_ifnum; /* IPMI interface number */
38e92b297cSZhao Yakui 	long curr_msgid;
39a1a69b29SLv Zheng 	bool dead;
40a1a69b29SLv Zheng 	struct kref kref;
41e92b297cSZhao Yakui };
42e92b297cSZhao Yakui 
43e92b297cSZhao Yakui struct ipmi_driver_data {
44e92b297cSZhao Yakui 	struct list_head ipmi_devices;
45e92b297cSZhao Yakui 	struct ipmi_smi_watcher bmc_events;
46c690d141SCorey Minyard 	const struct ipmi_user_hndl ipmi_hndlrs;
47e92b297cSZhao Yakui 	struct mutex ipmi_lock;
4850065300SLv Zheng 
49e96a94edSLv Zheng 	/*
50e96a94edSLv Zheng 	 * NOTE: IPMI System Interface Selection
51e96a94edSLv Zheng 	 * There is no system interface specified by the IPMI operation
52e96a94edSLv Zheng 	 * region access.  We try to select one system interface with ACPI
53e96a94edSLv Zheng 	 * handle set.  IPMI messages passed from the ACPI codes are sent
54e96a94edSLv Zheng 	 * to this selected global IPMI system interface.
55e96a94edSLv Zheng 	 */
56e96a94edSLv Zheng 	struct acpi_ipmi_device *selected_smi;
57e92b297cSZhao Yakui };
58e92b297cSZhao Yakui 
59e92b297cSZhao Yakui struct acpi_ipmi_msg {
60e92b297cSZhao Yakui 	struct list_head head;
6150065300SLv Zheng 
62e92b297cSZhao Yakui 	/*
63e92b297cSZhao Yakui 	 * General speaking the addr type should be SI_ADDR_TYPE. And
64e92b297cSZhao Yakui 	 * the addr channel should be BMC.
65e92b297cSZhao Yakui 	 * In fact it can also be IPMB type. But we will have to
66e92b297cSZhao Yakui 	 * parse it from the Netfn command buffer. It is so complex
67e92b297cSZhao Yakui 	 * that it is skipped.
68e92b297cSZhao Yakui 	 */
69e92b297cSZhao Yakui 	struct ipmi_addr addr;
70e92b297cSZhao Yakui 	long tx_msgid;
7150065300SLv Zheng 
72e92b297cSZhao Yakui 	/* it is used to track whether the IPMI message is finished */
73e92b297cSZhao Yakui 	struct completion tx_complete;
7450065300SLv Zheng 
75e92b297cSZhao Yakui 	struct kernel_ipmi_msg tx_message;
76e92b297cSZhao Yakui 	int msg_done;
7750065300SLv Zheng 
786b68f03fSLv Zheng 	/* tx/rx data . And copy it from/to ACPI object buffer */
796b68f03fSLv Zheng 	u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
806b68f03fSLv Zheng 	u8 rx_len;
8150065300SLv Zheng 
82e92b297cSZhao Yakui 	struct acpi_ipmi_device *device;
837b984477SLv Zheng 	struct kref kref;
84e92b297cSZhao Yakui };
85e92b297cSZhao Yakui 
86e92b297cSZhao Yakui /* IPMI request/response buffer per ACPI 4.0, sec 5.5.2.4.3.2 */
87e92b297cSZhao Yakui struct acpi_ipmi_buffer {
88e92b297cSZhao Yakui 	u8 status;
89e92b297cSZhao Yakui 	u8 length;
906b68f03fSLv Zheng 	u8 data[ACPI_IPMI_MAX_MSG_LENGTH];
91e92b297cSZhao Yakui };
92e92b297cSZhao Yakui 
93e92b297cSZhao Yakui static void ipmi_register_bmc(int iface, struct device *dev);
94e92b297cSZhao Yakui static void ipmi_bmc_gone(int iface);
95e92b297cSZhao Yakui static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data);
96e92b297cSZhao Yakui 
97e92b297cSZhao Yakui static struct ipmi_driver_data driver_data = {
98e92b297cSZhao Yakui 	.ipmi_devices = LIST_HEAD_INIT(driver_data.ipmi_devices),
99e92b297cSZhao Yakui 	.bmc_events = {
100e92b297cSZhao Yakui 		.owner = THIS_MODULE,
101e92b297cSZhao Yakui 		.new_smi = ipmi_register_bmc,
102e92b297cSZhao Yakui 		.smi_gone = ipmi_bmc_gone,
103e92b297cSZhao Yakui 	},
104e92b297cSZhao Yakui 	.ipmi_hndlrs = {
105e92b297cSZhao Yakui 		.ipmi_recv_hndl = ipmi_msg_handler,
106e92b297cSZhao Yakui 	},
107a194aa43SLv Zheng 	.ipmi_lock = __MUTEX_INITIALIZER(driver_data.ipmi_lock)
108e92b297cSZhao Yakui };
109e92b297cSZhao Yakui 
110a1a69b29SLv Zheng static struct acpi_ipmi_device *
ipmi_dev_alloc(int iface,struct device * dev,acpi_handle handle)1112fb037b8SLv Zheng ipmi_dev_alloc(int iface, struct device *dev, acpi_handle handle)
112a1a69b29SLv Zheng {
113a1a69b29SLv Zheng 	struct acpi_ipmi_device *ipmi_device;
114a1a69b29SLv Zheng 	int err;
115ebba75fdSCorey Minyard 	struct ipmi_user *user;
116a1a69b29SLv Zheng 
117a1a69b29SLv Zheng 	ipmi_device = kzalloc(sizeof(*ipmi_device), GFP_KERNEL);
118a1a69b29SLv Zheng 	if (!ipmi_device)
119a1a69b29SLv Zheng 		return NULL;
120a1a69b29SLv Zheng 
121a1a69b29SLv Zheng 	kref_init(&ipmi_device->kref);
122a1a69b29SLv Zheng 	INIT_LIST_HEAD(&ipmi_device->head);
123a1a69b29SLv Zheng 	INIT_LIST_HEAD(&ipmi_device->tx_msg_list);
124a1a69b29SLv Zheng 	spin_lock_init(&ipmi_device->tx_msg_lock);
125a1a69b29SLv Zheng 	ipmi_device->handle = handle;
1262fb037b8SLv Zheng 	ipmi_device->dev = get_device(dev);
127a1a69b29SLv Zheng 	ipmi_device->ipmi_ifnum = iface;
128a1a69b29SLv Zheng 
129a1a69b29SLv Zheng 	err = ipmi_create_user(iface, &driver_data.ipmi_hndlrs,
130a1a69b29SLv Zheng 			       ipmi_device, &user);
131a1a69b29SLv Zheng 	if (err) {
1322fb037b8SLv Zheng 		put_device(dev);
133a1a69b29SLv Zheng 		kfree(ipmi_device);
134a1a69b29SLv Zheng 		return NULL;
135a1a69b29SLv Zheng 	}
136a1a69b29SLv Zheng 	ipmi_device->user_interface = user;
137a1a69b29SLv Zheng 
138a1a69b29SLv Zheng 	return ipmi_device;
139a1a69b29SLv Zheng }
140a1a69b29SLv Zheng 
ipmi_dev_release(struct acpi_ipmi_device * ipmi_device)141a1a69b29SLv Zheng static void ipmi_dev_release(struct acpi_ipmi_device *ipmi_device)
142a1a69b29SLv Zheng {
143a1a69b29SLv Zheng 	ipmi_destroy_user(ipmi_device->user_interface);
1442fb037b8SLv Zheng 	put_device(ipmi_device->dev);
145a1a69b29SLv Zheng 	kfree(ipmi_device);
146a1a69b29SLv Zheng }
147a1a69b29SLv Zheng 
ipmi_dev_release_kref(struct kref * kref)148a1a69b29SLv Zheng static void ipmi_dev_release_kref(struct kref *kref)
149a1a69b29SLv Zheng {
150a1a69b29SLv Zheng 	struct acpi_ipmi_device *ipmi =
151a1a69b29SLv Zheng 		container_of(kref, struct acpi_ipmi_device, kref);
152a1a69b29SLv Zheng 
153a1a69b29SLv Zheng 	ipmi_dev_release(ipmi);
154a1a69b29SLv Zheng }
155a1a69b29SLv Zheng 
__ipmi_dev_kill(struct acpi_ipmi_device * ipmi_device)156a1a69b29SLv Zheng static void __ipmi_dev_kill(struct acpi_ipmi_device *ipmi_device)
157a1a69b29SLv Zheng {
158a1a69b29SLv Zheng 	list_del(&ipmi_device->head);
159e96a94edSLv Zheng 	if (driver_data.selected_smi == ipmi_device)
160e96a94edSLv Zheng 		driver_data.selected_smi = NULL;
16150065300SLv Zheng 
162a1a69b29SLv Zheng 	/*
163a1a69b29SLv Zheng 	 * Always setting dead flag after deleting from the list or
164a1a69b29SLv Zheng 	 * list_for_each_entry() codes must get changed.
165a1a69b29SLv Zheng 	 */
166a1a69b29SLv Zheng 	ipmi_device->dead = true;
167a1a69b29SLv Zheng }
168a1a69b29SLv Zheng 
acpi_ipmi_dev_get(void)169e96a94edSLv Zheng static struct acpi_ipmi_device *acpi_ipmi_dev_get(void)
170a1a69b29SLv Zheng {
171e96a94edSLv Zheng 	struct acpi_ipmi_device *ipmi_device = NULL;
172a1a69b29SLv Zheng 
173a1a69b29SLv Zheng 	mutex_lock(&driver_data.ipmi_lock);
174e96a94edSLv Zheng 	if (driver_data.selected_smi) {
175e96a94edSLv Zheng 		ipmi_device = driver_data.selected_smi;
176a1a69b29SLv Zheng 		kref_get(&ipmi_device->kref);
177a1a69b29SLv Zheng 	}
178a1a69b29SLv Zheng 	mutex_unlock(&driver_data.ipmi_lock);
179a1a69b29SLv Zheng 
180a1a69b29SLv Zheng 	return ipmi_device;
181a1a69b29SLv Zheng }
182a1a69b29SLv Zheng 
acpi_ipmi_dev_put(struct acpi_ipmi_device * ipmi_device)183a1a69b29SLv Zheng static void acpi_ipmi_dev_put(struct acpi_ipmi_device *ipmi_device)
184a1a69b29SLv Zheng {
185a1a69b29SLv Zheng 	kref_put(&ipmi_device->kref, ipmi_dev_release_kref);
186a1a69b29SLv Zheng }
187a1a69b29SLv Zheng 
ipmi_msg_alloc(void)1887b984477SLv Zheng static struct acpi_ipmi_msg *ipmi_msg_alloc(void)
189e92b297cSZhao Yakui {
1907b984477SLv Zheng 	struct acpi_ipmi_device *ipmi;
191e92b297cSZhao Yakui 	struct acpi_ipmi_msg *ipmi_msg;
192e92b297cSZhao Yakui 
1937b984477SLv Zheng 	ipmi = acpi_ipmi_dev_get();
1947b984477SLv Zheng 	if (!ipmi)
1957b984477SLv Zheng 		return NULL;
19650065300SLv Zheng 
197e92b297cSZhao Yakui 	ipmi_msg = kzalloc(sizeof(struct acpi_ipmi_msg), GFP_KERNEL);
198e92b297cSZhao Yakui 	if (!ipmi_msg) {
1997b984477SLv Zheng 		acpi_ipmi_dev_put(ipmi);
200e92b297cSZhao Yakui 		return NULL;
201e92b297cSZhao Yakui 	}
20250065300SLv Zheng 
2037b984477SLv Zheng 	kref_init(&ipmi_msg->kref);
204e92b297cSZhao Yakui 	init_completion(&ipmi_msg->tx_complete);
205e92b297cSZhao Yakui 	INIT_LIST_HEAD(&ipmi_msg->head);
206e92b297cSZhao Yakui 	ipmi_msg->device = ipmi;
2078584ec6aSLv Zheng 	ipmi_msg->msg_done = ACPI_IPMI_UNKNOWN;
20850065300SLv Zheng 
209e92b297cSZhao Yakui 	return ipmi_msg;
210e92b297cSZhao Yakui }
211e92b297cSZhao Yakui 
ipmi_msg_release(struct acpi_ipmi_msg * tx_msg)2127b984477SLv Zheng static void ipmi_msg_release(struct acpi_ipmi_msg *tx_msg)
2137b984477SLv Zheng {
2147b984477SLv Zheng 	acpi_ipmi_dev_put(tx_msg->device);
2157b984477SLv Zheng 	kfree(tx_msg);
2167b984477SLv Zheng }
2177b984477SLv Zheng 
ipmi_msg_release_kref(struct kref * kref)2187b984477SLv Zheng static void ipmi_msg_release_kref(struct kref *kref)
2197b984477SLv Zheng {
2207b984477SLv Zheng 	struct acpi_ipmi_msg *tx_msg =
2217b984477SLv Zheng 		container_of(kref, struct acpi_ipmi_msg, kref);
2227b984477SLv Zheng 
2237b984477SLv Zheng 	ipmi_msg_release(tx_msg);
2247b984477SLv Zheng }
2257b984477SLv Zheng 
acpi_ipmi_msg_get(struct acpi_ipmi_msg * tx_msg)2267b984477SLv Zheng static struct acpi_ipmi_msg *acpi_ipmi_msg_get(struct acpi_ipmi_msg *tx_msg)
2277b984477SLv Zheng {
2287b984477SLv Zheng 	kref_get(&tx_msg->kref);
2297b984477SLv Zheng 
2307b984477SLv Zheng 	return tx_msg;
2317b984477SLv Zheng }
2327b984477SLv Zheng 
acpi_ipmi_msg_put(struct acpi_ipmi_msg * tx_msg)2337b984477SLv Zheng static void acpi_ipmi_msg_put(struct acpi_ipmi_msg *tx_msg)
2347b984477SLv Zheng {
2357b984477SLv Zheng 	kref_put(&tx_msg->kref, ipmi_msg_release_kref);
2367b984477SLv Zheng }
2377b984477SLv Zheng 
238e92b297cSZhao Yakui #define IPMI_OP_RGN_NETFN(offset)	((offset >> 8) & 0xff)
239e92b297cSZhao Yakui #define IPMI_OP_RGN_CMD(offset)		(offset & 0xff)
acpi_format_ipmi_request(struct acpi_ipmi_msg * tx_msg,acpi_physical_address address,acpi_integer * value)2406b68f03fSLv Zheng static int acpi_format_ipmi_request(struct acpi_ipmi_msg *tx_msg,
241e92b297cSZhao Yakui 				    acpi_physical_address address,
242e92b297cSZhao Yakui 				    acpi_integer *value)
243e92b297cSZhao Yakui {
244e92b297cSZhao Yakui 	struct kernel_ipmi_msg *msg;
245e92b297cSZhao Yakui 	struct acpi_ipmi_buffer *buffer;
246e92b297cSZhao Yakui 	struct acpi_ipmi_device *device;
24706a8566bSLv Zheng 	unsigned long flags;
248e92b297cSZhao Yakui 
249e92b297cSZhao Yakui 	msg = &tx_msg->tx_message;
25050065300SLv Zheng 
251e92b297cSZhao Yakui 	/*
252e92b297cSZhao Yakui 	 * IPMI network function and command are encoded in the address
253e92b297cSZhao Yakui 	 * within the IPMI OpRegion; see ACPI 4.0, sec 5.5.2.4.3.
254e92b297cSZhao Yakui 	 */
255e92b297cSZhao Yakui 	msg->netfn = IPMI_OP_RGN_NETFN(address);
256e92b297cSZhao Yakui 	msg->cmd = IPMI_OP_RGN_CMD(address);
2576b68f03fSLv Zheng 	msg->data = tx_msg->data;
25850065300SLv Zheng 
259e92b297cSZhao Yakui 	/*
260e92b297cSZhao Yakui 	 * value is the parameter passed by the IPMI opregion space handler.
261e92b297cSZhao Yakui 	 * It points to the IPMI request message buffer
262e92b297cSZhao Yakui 	 */
263e92b297cSZhao Yakui 	buffer = (struct acpi_ipmi_buffer *)value;
26450065300SLv Zheng 
265e92b297cSZhao Yakui 	/* copy the tx message data */
2666b68f03fSLv Zheng 	if (buffer->length > ACPI_IPMI_MAX_MSG_LENGTH) {
2672fb037b8SLv Zheng 		dev_WARN_ONCE(tx_msg->device->dev, true,
2686b68f03fSLv Zheng 			      "Unexpected request (msg len %d).\n",
2696b68f03fSLv Zheng 			      buffer->length);
2706b68f03fSLv Zheng 		return -EINVAL;
2716b68f03fSLv Zheng 	}
272e92b297cSZhao Yakui 	msg->data_len = buffer->length;
2736b68f03fSLv Zheng 	memcpy(tx_msg->data, buffer->data, msg->data_len);
27450065300SLv Zheng 
275e92b297cSZhao Yakui 	/*
276e92b297cSZhao Yakui 	 * now the default type is SYSTEM_INTERFACE and channel type is BMC.
277e92b297cSZhao Yakui 	 * If the netfn is APP_REQUEST and the cmd is SEND_MESSAGE,
278e92b297cSZhao Yakui 	 * the addr type should be changed to IPMB. Then we will have to parse
279e92b297cSZhao Yakui 	 * the IPMI request message buffer to get the IPMB address.
280e92b297cSZhao Yakui 	 * If so, please fix me.
281e92b297cSZhao Yakui 	 */
282e92b297cSZhao Yakui 	tx_msg->addr.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE;
283e92b297cSZhao Yakui 	tx_msg->addr.channel = IPMI_BMC_CHANNEL;
284e92b297cSZhao Yakui 	tx_msg->addr.data[0] = 0;
285e92b297cSZhao Yakui 
286e92b297cSZhao Yakui 	/* Get the msgid */
287e92b297cSZhao Yakui 	device = tx_msg->device;
28850065300SLv Zheng 
28906a8566bSLv Zheng 	spin_lock_irqsave(&device->tx_msg_lock, flags);
290e92b297cSZhao Yakui 	device->curr_msgid++;
291e92b297cSZhao Yakui 	tx_msg->tx_msgid = device->curr_msgid;
29206a8566bSLv Zheng 	spin_unlock_irqrestore(&device->tx_msg_lock, flags);
29350065300SLv Zheng 
2946b68f03fSLv Zheng 	return 0;
295e92b297cSZhao Yakui }
296e92b297cSZhao Yakui 
acpi_format_ipmi_response(struct acpi_ipmi_msg * msg,acpi_integer * value)297e92b297cSZhao Yakui static void acpi_format_ipmi_response(struct acpi_ipmi_msg *msg,
2988584ec6aSLv Zheng 				      acpi_integer *value)
299e92b297cSZhao Yakui {
300e92b297cSZhao Yakui 	struct acpi_ipmi_buffer *buffer;
301e92b297cSZhao Yakui 
302e92b297cSZhao Yakui 	/*
303e92b297cSZhao Yakui 	 * value is also used as output parameter. It represents the response
304e92b297cSZhao Yakui 	 * IPMI message returned by IPMI command.
305e92b297cSZhao Yakui 	 */
306e92b297cSZhao Yakui 	buffer = (struct acpi_ipmi_buffer *)value;
30750065300SLv Zheng 
308e92b297cSZhao Yakui 	/*
3098584ec6aSLv Zheng 	 * If the flag of msg_done is not set, it means that the IPMI command is
3108584ec6aSLv Zheng 	 * not executed correctly.
311e92b297cSZhao Yakui 	 */
3128584ec6aSLv Zheng 	buffer->status = msg->msg_done;
3138584ec6aSLv Zheng 	if (msg->msg_done != ACPI_IPMI_OK)
314e92b297cSZhao Yakui 		return;
31550065300SLv Zheng 
316e92b297cSZhao Yakui 	/*
317e92b297cSZhao Yakui 	 * If the IPMI response message is obtained correctly, the status code
318e92b297cSZhao Yakui 	 * will be ACPI_IPMI_OK
319e92b297cSZhao Yakui 	 */
320e92b297cSZhao Yakui 	buffer->length = msg->rx_len;
3216b68f03fSLv Zheng 	memcpy(buffer->data, msg->data, msg->rx_len);
322e92b297cSZhao Yakui }
323e92b297cSZhao Yakui 
ipmi_flush_tx_msg(struct acpi_ipmi_device * ipmi)324e92b297cSZhao Yakui static void ipmi_flush_tx_msg(struct acpi_ipmi_device *ipmi)
325e92b297cSZhao Yakui {
3267b984477SLv Zheng 	struct acpi_ipmi_msg *tx_msg;
3275ac557efSLv Zheng 	unsigned long flags;
328e92b297cSZhao Yakui 
329a1a69b29SLv Zheng 	/*
330a1a69b29SLv Zheng 	 * NOTE: On-going ipmi_recv_msg
331a1a69b29SLv Zheng 	 * ipmi_msg_handler() may still be invoked by ipmi_si after
332a1a69b29SLv Zheng 	 * flushing.  But it is safe to do a fast flushing on module_exit()
333a1a69b29SLv Zheng 	 * without waiting for all ipmi_recv_msg(s) to complete from
334a1a69b29SLv Zheng 	 * ipmi_msg_handler() as it is ensured by ipmi_si that all
335a1a69b29SLv Zheng 	 * ipmi_recv_msg(s) are freed after invoking ipmi_destroy_user().
336a1a69b29SLv Zheng 	 */
3375ac557efSLv Zheng 	spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
3387b984477SLv Zheng 	while (!list_empty(&ipmi->tx_msg_list)) {
3397b984477SLv Zheng 		tx_msg = list_first_entry(&ipmi->tx_msg_list,
3407b984477SLv Zheng 					  struct acpi_ipmi_msg,
3417b984477SLv Zheng 					  head);
3427b984477SLv Zheng 		list_del(&tx_msg->head);
3437b984477SLv Zheng 		spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
3447b984477SLv Zheng 
345e92b297cSZhao Yakui 		/* wake up the sleep thread on the Tx msg */
346e92b297cSZhao Yakui 		complete(&tx_msg->tx_complete);
3477b984477SLv Zheng 		acpi_ipmi_msg_put(tx_msg);
3487b984477SLv Zheng 		spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
349e92b297cSZhao Yakui 	}
3505ac557efSLv Zheng 	spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
351e92b297cSZhao Yakui }
352e92b297cSZhao Yakui 
ipmi_cancel_tx_msg(struct acpi_ipmi_device * ipmi,struct acpi_ipmi_msg * msg)3537b984477SLv Zheng static void ipmi_cancel_tx_msg(struct acpi_ipmi_device *ipmi,
3547b984477SLv Zheng 			       struct acpi_ipmi_msg *msg)
3557b984477SLv Zheng {
356*26de0ab9SJakob Koschel 	struct acpi_ipmi_msg *tx_msg = NULL, *iter, *temp;
3577b984477SLv Zheng 	unsigned long flags;
3587b984477SLv Zheng 
3597b984477SLv Zheng 	spin_lock_irqsave(&ipmi->tx_msg_lock, flags);
360*26de0ab9SJakob Koschel 	list_for_each_entry_safe(iter, temp, &ipmi->tx_msg_list, head) {
361*26de0ab9SJakob Koschel 		if (msg == iter) {
362*26de0ab9SJakob Koschel 			tx_msg = iter;
363*26de0ab9SJakob Koschel 			list_del(&iter->head);
3647b984477SLv Zheng 			break;
3657b984477SLv Zheng 		}
3667b984477SLv Zheng 	}
3677b984477SLv Zheng 	spin_unlock_irqrestore(&ipmi->tx_msg_lock, flags);
3687b984477SLv Zheng 
369*26de0ab9SJakob Koschel 	if (tx_msg)
3707b984477SLv Zheng 		acpi_ipmi_msg_put(tx_msg);
3717b984477SLv Zheng }
3727b984477SLv Zheng 
ipmi_msg_handler(struct ipmi_recv_msg * msg,void * user_msg_data)373e92b297cSZhao Yakui static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data)
374e92b297cSZhao Yakui {
375e92b297cSZhao Yakui 	struct acpi_ipmi_device *ipmi_device = user_msg_data;
376*26de0ab9SJakob Koschel 	struct acpi_ipmi_msg *tx_msg = NULL, *iter, *temp;
3772fb037b8SLv Zheng 	struct device *dev = ipmi_device->dev;
37806a8566bSLv Zheng 	unsigned long flags;
379e92b297cSZhao Yakui 
380e92b297cSZhao Yakui 	if (msg->user != ipmi_device->user_interface) {
38150065300SLv Zheng 		dev_warn(dev,
38250065300SLv Zheng 			 "Unexpected response is returned. returned user %p, expected user %p\n",
383e92b297cSZhao Yakui 			 msg->user, ipmi_device->user_interface);
3846b68f03fSLv Zheng 		goto out_msg;
385e92b297cSZhao Yakui 	}
38650065300SLv Zheng 
38706a8566bSLv Zheng 	spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
388*26de0ab9SJakob Koschel 	list_for_each_entry_safe(iter, temp, &ipmi_device->tx_msg_list, head) {
389*26de0ab9SJakob Koschel 		if (msg->msgid == iter->tx_msgid) {
390*26de0ab9SJakob Koschel 			tx_msg = iter;
391*26de0ab9SJakob Koschel 			list_del(&iter->head);
392e92b297cSZhao Yakui 			break;
393e92b297cSZhao Yakui 		}
394e92b297cSZhao Yakui 	}
3957b984477SLv Zheng 	spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
396e92b297cSZhao Yakui 
397*26de0ab9SJakob Koschel 	if (!tx_msg) {
39850065300SLv Zheng 		dev_warn(dev,
39950065300SLv Zheng 			 "Unexpected response (msg id %ld) is returned.\n",
40050065300SLv Zheng 			 msg->msgid);
4017b984477SLv Zheng 		goto out_msg;
402e92b297cSZhao Yakui 	}
403e92b297cSZhao Yakui 
404e92b297cSZhao Yakui 	/* copy the response data to Rx_data buffer */
4056b68f03fSLv Zheng 	if (msg->msg.data_len > ACPI_IPMI_MAX_MSG_LENGTH) {
4062fb037b8SLv Zheng 		dev_WARN_ONCE(dev, true,
4076b68f03fSLv Zheng 			      "Unexpected response (msg len %d).\n",
4086b68f03fSLv Zheng 			      msg->msg.data_len);
4098584ec6aSLv Zheng 		goto out_comp;
4108584ec6aSLv Zheng 	}
41150065300SLv Zheng 
4128584ec6aSLv Zheng 	/* response msg is an error msg */
4138584ec6aSLv Zheng 	msg->recv_type = IPMI_RESPONSE_RECV_TYPE;
4148584ec6aSLv Zheng 	if (msg->recv_type == IPMI_RESPONSE_RECV_TYPE &&
4158584ec6aSLv Zheng 	    msg->msg.data_len == 1) {
4168584ec6aSLv Zheng 		if (msg->msg.data[0] == IPMI_TIMEOUT_COMPLETION_CODE) {
417f5135614SSinan Kaya 			dev_dbg_once(dev, "Unexpected response (timeout).\n");
4188584ec6aSLv Zheng 			tx_msg->msg_done = ACPI_IPMI_TIMEOUT;
4198584ec6aSLv Zheng 		}
4208584ec6aSLv Zheng 		goto out_comp;
4218584ec6aSLv Zheng 	}
42250065300SLv Zheng 
423e92b297cSZhao Yakui 	tx_msg->rx_len = msg->msg.data_len;
4246b68f03fSLv Zheng 	memcpy(tx_msg->data, msg->msg.data, tx_msg->rx_len);
4258584ec6aSLv Zheng 	tx_msg->msg_done = ACPI_IPMI_OK;
42650065300SLv Zheng 
4278584ec6aSLv Zheng out_comp:
428e92b297cSZhao Yakui 	complete(&tx_msg->tx_complete);
4297b984477SLv Zheng 	acpi_ipmi_msg_put(tx_msg);
4306b68f03fSLv Zheng out_msg:
431e92b297cSZhao Yakui 	ipmi_free_recv_msg(msg);
43250065300SLv Zheng }
433e92b297cSZhao Yakui 
ipmi_register_bmc(int iface,struct device * dev)434e92b297cSZhao Yakui static void ipmi_register_bmc(int iface, struct device *dev)
435e92b297cSZhao Yakui {
436e92b297cSZhao Yakui 	struct acpi_ipmi_device *ipmi_device, *temp;
437e92b297cSZhao Yakui 	int err;
438e92b297cSZhao Yakui 	struct ipmi_smi_info smi_data;
439e92b297cSZhao Yakui 	acpi_handle handle;
440e92b297cSZhao Yakui 
441e92b297cSZhao Yakui 	err = ipmi_get_smi_info(iface, &smi_data);
442e92b297cSZhao Yakui 	if (err)
443e92b297cSZhao Yakui 		return;
444e92b297cSZhao Yakui 
445a1a69b29SLv Zheng 	if (smi_data.addr_src != SI_ACPI)
446a1a69b29SLv Zheng 		goto err_ref;
447e92b297cSZhao Yakui 	handle = smi_data.addr_info.acpi_info.acpi_handle;
448a1a69b29SLv Zheng 	if (!handle)
449a1a69b29SLv Zheng 		goto err_ref;
450a1a69b29SLv Zheng 
4512fb037b8SLv Zheng 	ipmi_device = ipmi_dev_alloc(iface, smi_data.dev, handle);
452a1a69b29SLv Zheng 	if (!ipmi_device) {
4532fb037b8SLv Zheng 		dev_warn(smi_data.dev, "Can't create IPMI user interface\n");
454a1a69b29SLv Zheng 		goto err_ref;
455a1a69b29SLv Zheng 	}
456e92b297cSZhao Yakui 
457e92b297cSZhao Yakui 	mutex_lock(&driver_data.ipmi_lock);
458e92b297cSZhao Yakui 	list_for_each_entry(temp, &driver_data.ipmi_devices, head) {
459e92b297cSZhao Yakui 		/*
460e92b297cSZhao Yakui 		 * if the corresponding ACPI handle is already added
461e92b297cSZhao Yakui 		 * to the device list, don't add it again.
462e92b297cSZhao Yakui 		 */
463e92b297cSZhao Yakui 		if (temp->handle == handle)
464a1a69b29SLv Zheng 			goto err_lock;
465e92b297cSZhao Yakui 	}
466e96a94edSLv Zheng 	if (!driver_data.selected_smi)
467e96a94edSLv Zheng 		driver_data.selected_smi = ipmi_device;
468a1a69b29SLv Zheng 	list_add_tail(&ipmi_device->head, &driver_data.ipmi_devices);
469e92b297cSZhao Yakui 	mutex_unlock(&driver_data.ipmi_lock);
47050065300SLv Zheng 
471a1a69b29SLv Zheng 	put_device(smi_data.dev);
472e92b297cSZhao Yakui 	return;
473e92b297cSZhao Yakui 
474a1a69b29SLv Zheng err_lock:
475e92b297cSZhao Yakui 	mutex_unlock(&driver_data.ipmi_lock);
476a1a69b29SLv Zheng 	ipmi_dev_release(ipmi_device);
477a1a69b29SLv Zheng err_ref:
478e92b297cSZhao Yakui 	put_device(smi_data.dev);
479e92b297cSZhao Yakui }
480e92b297cSZhao Yakui 
ipmi_bmc_gone(int iface)481e92b297cSZhao Yakui static void ipmi_bmc_gone(int iface)
482e92b297cSZhao Yakui {
483*26de0ab9SJakob Koschel 	struct acpi_ipmi_device *ipmi_device = NULL, *iter, *temp;
484e92b297cSZhao Yakui 
485e92b297cSZhao Yakui 	mutex_lock(&driver_data.ipmi_lock);
486*26de0ab9SJakob Koschel 	list_for_each_entry_safe(iter, temp,
487e92b297cSZhao Yakui 				 &driver_data.ipmi_devices, head) {
488*26de0ab9SJakob Koschel 		if (iter->ipmi_ifnum != iface) {
489*26de0ab9SJakob Koschel 			ipmi_device = iter;
490*26de0ab9SJakob Koschel 			__ipmi_dev_kill(iter);
491e92b297cSZhao Yakui 			break;
492e92b297cSZhao Yakui 		}
493a1a69b29SLv Zheng 	}
494e96a94edSLv Zheng 	if (!driver_data.selected_smi)
495e96a94edSLv Zheng 		driver_data.selected_smi = list_first_entry_or_null(
496e96a94edSLv Zheng 					&driver_data.ipmi_devices,
497e96a94edSLv Zheng 					struct acpi_ipmi_device, head);
498e92b297cSZhao Yakui 	mutex_unlock(&driver_data.ipmi_lock);
49950065300SLv Zheng 
500*26de0ab9SJakob Koschel 	if (ipmi_device) {
501a1a69b29SLv Zheng 		ipmi_flush_tx_msg(ipmi_device);
502a1a69b29SLv Zheng 		acpi_ipmi_dev_put(ipmi_device);
503a1a69b29SLv Zheng 	}
504e92b297cSZhao Yakui }
50550065300SLv Zheng 
506e92b297cSZhao Yakui /*
507e92b297cSZhao Yakui  * This is the IPMI opregion space handler.
508e92b297cSZhao Yakui  * @function: indicates the read/write. In fact as the IPMI message is driven
509e92b297cSZhao Yakui  * by command, only write is meaningful.
510e92b297cSZhao Yakui  * @address: This contains the netfn/command of IPMI request message.
511e92b297cSZhao Yakui  * @bits   : not used.
512e92b297cSZhao Yakui  * @value  : it is an in/out parameter. It points to the IPMI message buffer.
513e92b297cSZhao Yakui  *	     Before the IPMI message is sent, it represents the actual request
514e92b297cSZhao Yakui  *	     IPMI message. After the IPMI message is finished, it represents
515e92b297cSZhao Yakui  *	     the response IPMI message returned by IPMI command.
516e92b297cSZhao Yakui  * @handler_context: IPMI device context.
517e92b297cSZhao Yakui  */
518e92b297cSZhao Yakui static acpi_status
acpi_ipmi_space_handler(u32 function,acpi_physical_address address,u32 bits,acpi_integer * value,void * handler_context,void * region_context)519e92b297cSZhao Yakui acpi_ipmi_space_handler(u32 function, acpi_physical_address address,
520e92b297cSZhao Yakui 			u32 bits, acpi_integer *value,
521e92b297cSZhao Yakui 			void *handler_context, void *region_context)
522e92b297cSZhao Yakui {
523e92b297cSZhao Yakui 	struct acpi_ipmi_msg *tx_msg;
524a1a69b29SLv Zheng 	struct acpi_ipmi_device *ipmi_device;
5258584ec6aSLv Zheng 	int err;
526e92b297cSZhao Yakui 	acpi_status status;
52706a8566bSLv Zheng 	unsigned long flags;
52850065300SLv Zheng 
529e92b297cSZhao Yakui 	/*
530e92b297cSZhao Yakui 	 * IPMI opregion message.
531e92b297cSZhao Yakui 	 * IPMI message is firstly written to the BMC and system software
532e92b297cSZhao Yakui 	 * can get the respsonse. So it is unmeaningful for the read access
533e92b297cSZhao Yakui 	 * of IPMI opregion.
534e92b297cSZhao Yakui 	 */
535e92b297cSZhao Yakui 	if ((function & ACPI_IO_MASK) == ACPI_READ)
536e92b297cSZhao Yakui 		return AE_TYPE;
537e92b297cSZhao Yakui 
5387b984477SLv Zheng 	tx_msg = ipmi_msg_alloc();
5397b984477SLv Zheng 	if (!tx_msg)
540e92b297cSZhao Yakui 		return AE_NOT_EXIST;
5417b984477SLv Zheng 	ipmi_device = tx_msg->device;
542e92b297cSZhao Yakui 
5436b68f03fSLv Zheng 	if (acpi_format_ipmi_request(tx_msg, address, value) != 0) {
5447b984477SLv Zheng 		ipmi_msg_release(tx_msg);
5457b984477SLv Zheng 		return AE_TYPE;
5466b68f03fSLv Zheng 	}
54750065300SLv Zheng 
5487b984477SLv Zheng 	acpi_ipmi_msg_get(tx_msg);
549a1a69b29SLv Zheng 	mutex_lock(&driver_data.ipmi_lock);
550a1a69b29SLv Zheng 	/* Do not add a tx_msg that can not be flushed. */
551a1a69b29SLv Zheng 	if (ipmi_device->dead) {
552a1a69b29SLv Zheng 		mutex_unlock(&driver_data.ipmi_lock);
5537b984477SLv Zheng 		ipmi_msg_release(tx_msg);
5547b984477SLv Zheng 		return AE_NOT_EXIST;
555a1a69b29SLv Zheng 	}
55606a8566bSLv Zheng 	spin_lock_irqsave(&ipmi_device->tx_msg_lock, flags);
557e92b297cSZhao Yakui 	list_add_tail(&tx_msg->head, &ipmi_device->tx_msg_list);
55806a8566bSLv Zheng 	spin_unlock_irqrestore(&ipmi_device->tx_msg_lock, flags);
559a1a69b29SLv Zheng 	mutex_unlock(&driver_data.ipmi_lock);
56050065300SLv Zheng 
561e92b297cSZhao Yakui 	err = ipmi_request_settime(ipmi_device->user_interface,
562e92b297cSZhao Yakui 				   &tx_msg->addr,
563e92b297cSZhao Yakui 				   tx_msg->tx_msgid,
564e92b297cSZhao Yakui 				   &tx_msg->tx_message,
5658584ec6aSLv Zheng 				   NULL, 0, 0, IPMI_TIMEOUT);
566e92b297cSZhao Yakui 	if (err) {
567e92b297cSZhao Yakui 		status = AE_ERROR;
5687b984477SLv Zheng 		goto out_msg;
569e92b297cSZhao Yakui 	}
5708584ec6aSLv Zheng 	wait_for_completion(&tx_msg->tx_complete);
57150065300SLv Zheng 
5728584ec6aSLv Zheng 	acpi_format_ipmi_response(tx_msg, value);
573e92b297cSZhao Yakui 	status = AE_OK;
574e92b297cSZhao Yakui 
5756b68f03fSLv Zheng out_msg:
5767b984477SLv Zheng 	ipmi_cancel_tx_msg(ipmi_device, tx_msg);
5777b984477SLv Zheng 	acpi_ipmi_msg_put(tx_msg);
578e92b297cSZhao Yakui 	return status;
579e92b297cSZhao Yakui }
580e92b297cSZhao Yakui 
acpi_ipmi_init(void)581e92b297cSZhao Yakui static int __init acpi_ipmi_init(void)
582e92b297cSZhao Yakui {
583a194aa43SLv Zheng 	int result;
584e96a94edSLv Zheng 	acpi_status status;
585e92b297cSZhao Yakui 
586e92b297cSZhao Yakui 	if (acpi_disabled)
587a194aa43SLv Zheng 		return 0;
588e92b297cSZhao Yakui 
589e96a94edSLv Zheng 	status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
59050065300SLv Zheng 						    ACPI_ADR_SPACE_IPMI,
59150065300SLv Zheng 						    &acpi_ipmi_space_handler,
592e96a94edSLv Zheng 						    NULL, NULL);
593e96a94edSLv Zheng 	if (ACPI_FAILURE(status)) {
594e96a94edSLv Zheng 		pr_warn("Can't register IPMI opregion space handle\n");
595e96a94edSLv Zheng 		return -EINVAL;
596e96a94edSLv Zheng 	}
59764887bbdSHanjun Guo 
598e92b297cSZhao Yakui 	result = ipmi_smi_watcher_register(&driver_data.bmc_events);
59964887bbdSHanjun Guo 	if (result) {
60064887bbdSHanjun Guo 		acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
60164887bbdSHanjun Guo 										  ACPI_ADR_SPACE_IPMI,
60264887bbdSHanjun Guo 										  &acpi_ipmi_space_handler);
603e96a94edSLv Zheng 		pr_err("Can't register IPMI system interface watcher\n");
60464887bbdSHanjun Guo 	}
605e92b297cSZhao Yakui 
606e92b297cSZhao Yakui 	return result;
607e92b297cSZhao Yakui }
608e92b297cSZhao Yakui 
acpi_ipmi_exit(void)609e92b297cSZhao Yakui static void __exit acpi_ipmi_exit(void)
610e92b297cSZhao Yakui {
611a1a69b29SLv Zheng 	struct acpi_ipmi_device *ipmi_device;
612e92b297cSZhao Yakui 
613e92b297cSZhao Yakui 	if (acpi_disabled)
614e92b297cSZhao Yakui 		return;
615e92b297cSZhao Yakui 
616e92b297cSZhao Yakui 	ipmi_smi_watcher_unregister(&driver_data.bmc_events);
617e92b297cSZhao Yakui 
618e92b297cSZhao Yakui 	/*
619e92b297cSZhao Yakui 	 * When one smi_watcher is unregistered, it is only deleted
620e92b297cSZhao Yakui 	 * from the smi_watcher list. But the smi_gone callback function
621e92b297cSZhao Yakui 	 * is not called. So explicitly uninstall the ACPI IPMI oregion
622e92b297cSZhao Yakui 	 * handler and free it.
623e92b297cSZhao Yakui 	 */
624e92b297cSZhao Yakui 	mutex_lock(&driver_data.ipmi_lock);
625a1a69b29SLv Zheng 	while (!list_empty(&driver_data.ipmi_devices)) {
626a1a69b29SLv Zheng 		ipmi_device = list_first_entry(&driver_data.ipmi_devices,
627a1a69b29SLv Zheng 					       struct acpi_ipmi_device,
628a1a69b29SLv Zheng 					       head);
629a1a69b29SLv Zheng 		__ipmi_dev_kill(ipmi_device);
630a1a69b29SLv Zheng 		mutex_unlock(&driver_data.ipmi_lock);
631a1a69b29SLv Zheng 
632a1a69b29SLv Zheng 		ipmi_flush_tx_msg(ipmi_device);
633a1a69b29SLv Zheng 		acpi_ipmi_dev_put(ipmi_device);
634a1a69b29SLv Zheng 
635a1a69b29SLv Zheng 		mutex_lock(&driver_data.ipmi_lock);
636e92b297cSZhao Yakui 	}
637e92b297cSZhao Yakui 	mutex_unlock(&driver_data.ipmi_lock);
638e96a94edSLv Zheng 	acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
63950065300SLv Zheng 					  ACPI_ADR_SPACE_IPMI,
64050065300SLv Zheng 					  &acpi_ipmi_space_handler);
641e92b297cSZhao Yakui }
642e92b297cSZhao Yakui 
643e92b297cSZhao Yakui module_init(acpi_ipmi_init);
644e92b297cSZhao Yakui module_exit(acpi_ipmi_exit);
645