1/*
2 * PowerNV OPAL IPMI driver
3 *
4 * Copyright 2014 IBM Corp.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 */
11
12#define pr_fmt(fmt)        "ipmi-powernv: " fmt
13
14#include <linux/ipmi_smi.h>
15#include <linux/list.h>
16#include <linux/module.h>
17#include <linux/of.h>
18
19#include <asm/opal.h>
20
21
22struct ipmi_smi_powernv {
23	u64			interface_id;
24	struct ipmi_device_id	ipmi_id;
25	ipmi_smi_t		intf;
26	u64			event;
27	struct notifier_block	event_nb;
28
29	/**
30	 * We assume that there can only be one outstanding request, so
31	 * keep the pending message in cur_msg. We protect this from concurrent
32	 * updates through send & recv calls, (and consequently opal_msg, which
33	 * is in-use when cur_msg is set) with msg_lock
34	 */
35	spinlock_t		msg_lock;
36	struct ipmi_smi_msg	*cur_msg;
37	struct opal_ipmi_msg	*opal_msg;
38};
39
40static int ipmi_powernv_start_processing(void *send_info, ipmi_smi_t intf)
41{
42	struct ipmi_smi_powernv *smi = send_info;
43
44	smi->intf = intf;
45	return 0;
46}
47
48static void send_error_reply(struct ipmi_smi_powernv *smi,
49		struct ipmi_smi_msg *msg, u8 completion_code)
50{
51	msg->rsp[0] = msg->data[0] | 0x4;
52	msg->rsp[1] = msg->data[1];
53	msg->rsp[2] = completion_code;
54	msg->rsp_size = 3;
55	ipmi_smi_msg_received(smi->intf, msg);
56}
57
58static void ipmi_powernv_send(void *send_info, struct ipmi_smi_msg *msg)
59{
60	struct ipmi_smi_powernv *smi = send_info;
61	struct opal_ipmi_msg *opal_msg;
62	unsigned long flags;
63	int comp, rc;
64	size_t size;
65
66	/* ensure data_len will fit in the opal_ipmi_msg buffer... */
67	if (msg->data_size > IPMI_MAX_MSG_LENGTH) {
68		comp = IPMI_REQ_LEN_EXCEEDED_ERR;
69		goto err;
70	}
71
72	/* ... and that we at least have netfn and cmd bytes */
73	if (msg->data_size < 2) {
74		comp = IPMI_REQ_LEN_INVALID_ERR;
75		goto err;
76	}
77
78	spin_lock_irqsave(&smi->msg_lock, flags);
79
80	if (smi->cur_msg) {
81		comp = IPMI_NODE_BUSY_ERR;
82		goto err_unlock;
83	}
84
85	/* format our data for the OPAL API */
86	opal_msg = smi->opal_msg;
87	opal_msg->version = OPAL_IPMI_MSG_FORMAT_VERSION_1;
88	opal_msg->netfn = msg->data[0];
89	opal_msg->cmd = msg->data[1];
90	if (msg->data_size > 2)
91		memcpy(opal_msg->data, msg->data + 2, msg->data_size - 2);
92
93	/* data_size already includes the netfn and cmd bytes */
94	size = sizeof(*opal_msg) + msg->data_size - 2;
95
96	pr_devel("%s: opal_ipmi_send(0x%llx, %p, %ld)\n", __func__,
97			smi->interface_id, opal_msg, size);
98	rc = opal_ipmi_send(smi->interface_id, opal_msg, size);
99	pr_devel("%s:  -> %d\n", __func__, rc);
100
101	if (!rc) {
102		smi->cur_msg = msg;
103		spin_unlock_irqrestore(&smi->msg_lock, flags);
104		return;
105	}
106
107	comp = IPMI_ERR_UNSPECIFIED;
108err_unlock:
109	spin_unlock_irqrestore(&smi->msg_lock, flags);
110err:
111	send_error_reply(smi, msg, comp);
112}
113
114static int ipmi_powernv_recv(struct ipmi_smi_powernv *smi)
115{
116	struct opal_ipmi_msg *opal_msg;
117	struct ipmi_smi_msg *msg;
118	unsigned long flags;
119	uint64_t size;
120	int rc;
121
122	pr_devel("%s: opal_ipmi_recv(%llx, msg, sz)\n", __func__,
123			smi->interface_id);
124
125	spin_lock_irqsave(&smi->msg_lock, flags);
126
127	if (!smi->cur_msg) {
128		spin_unlock_irqrestore(&smi->msg_lock, flags);
129		pr_warn("no current message?\n");
130		return 0;
131	}
132
133	msg = smi->cur_msg;
134	opal_msg = smi->opal_msg;
135
136	size = cpu_to_be64(sizeof(*opal_msg) + IPMI_MAX_MSG_LENGTH);
137
138	rc = opal_ipmi_recv(smi->interface_id,
139			opal_msg,
140			&size);
141	size = be64_to_cpu(size);
142	pr_devel("%s:   -> %d (size %lld)\n", __func__,
143			rc, rc == 0 ? size : 0);
144	if (rc) {
145		spin_unlock_irqrestore(&smi->msg_lock, flags);
146		ipmi_free_smi_msg(msg);
147		return 0;
148	}
149
150	if (size < sizeof(*opal_msg)) {
151		spin_unlock_irqrestore(&smi->msg_lock, flags);
152		pr_warn("unexpected IPMI message size %lld\n", size);
153		return 0;
154	}
155
156	if (opal_msg->version != OPAL_IPMI_MSG_FORMAT_VERSION_1) {
157		spin_unlock_irqrestore(&smi->msg_lock, flags);
158		pr_warn("unexpected IPMI message format (version %d)\n",
159				opal_msg->version);
160		return 0;
161	}
162
163	msg->rsp[0] = opal_msg->netfn;
164	msg->rsp[1] = opal_msg->cmd;
165	if (size > sizeof(*opal_msg))
166		memcpy(&msg->rsp[2], opal_msg->data, size - sizeof(*opal_msg));
167	msg->rsp_size = 2 + size - sizeof(*opal_msg);
168
169	smi->cur_msg = NULL;
170	spin_unlock_irqrestore(&smi->msg_lock, flags);
171	ipmi_smi_msg_received(smi->intf, msg);
172	return 0;
173}
174
175static void ipmi_powernv_request_events(void *send_info)
176{
177}
178
179static void ipmi_powernv_set_run_to_completion(void *send_info,
180		bool run_to_completion)
181{
182}
183
184static void ipmi_powernv_poll(void *send_info)
185{
186	struct ipmi_smi_powernv *smi = send_info;
187
188	ipmi_powernv_recv(smi);
189}
190
191static struct ipmi_smi_handlers ipmi_powernv_smi_handlers = {
192	.owner			= THIS_MODULE,
193	.start_processing	= ipmi_powernv_start_processing,
194	.sender			= ipmi_powernv_send,
195	.request_events		= ipmi_powernv_request_events,
196	.set_run_to_completion	= ipmi_powernv_set_run_to_completion,
197	.poll			= ipmi_powernv_poll,
198};
199
200static int ipmi_opal_event(struct notifier_block *nb,
201			  unsigned long events, void *change)
202{
203	struct ipmi_smi_powernv *smi = container_of(nb,
204					struct ipmi_smi_powernv, event_nb);
205
206	if (events & smi->event)
207		ipmi_powernv_recv(smi);
208	return 0;
209}
210
211static int ipmi_powernv_probe(struct platform_device *pdev)
212{
213	struct ipmi_smi_powernv *ipmi;
214	struct device *dev;
215	u32 prop;
216	int rc;
217
218	if (!pdev || !pdev->dev.of_node)
219		return -ENODEV;
220
221	dev = &pdev->dev;
222
223	ipmi = devm_kzalloc(dev, sizeof(*ipmi), GFP_KERNEL);
224	if (!ipmi)
225		return -ENOMEM;
226
227	spin_lock_init(&ipmi->msg_lock);
228
229	rc = of_property_read_u32(dev->of_node, "ibm,ipmi-interface-id",
230			&prop);
231	if (rc) {
232		dev_warn(dev, "No interface ID property\n");
233		goto err_free;
234	}
235	ipmi->interface_id = prop;
236
237	rc = of_property_read_u32(dev->of_node, "interrupts", &prop);
238	if (rc) {
239		dev_warn(dev, "No interrupts property\n");
240		goto err_free;
241	}
242
243	ipmi->event = 1ull << prop;
244	ipmi->event_nb.notifier_call = ipmi_opal_event;
245
246	rc = opal_notifier_register(&ipmi->event_nb);
247	if (rc) {
248		dev_warn(dev, "OPAL notifier registration failed (%d)\n", rc);
249		goto err_free;
250	}
251
252	ipmi->opal_msg = devm_kmalloc(dev,
253			sizeof(*ipmi->opal_msg) + IPMI_MAX_MSG_LENGTH,
254			GFP_KERNEL);
255	if (!ipmi->opal_msg) {
256		rc = -ENOMEM;
257		goto err_unregister;
258	}
259
260	/* todo: query actual ipmi_device_id */
261	rc = ipmi_register_smi(&ipmi_powernv_smi_handlers, ipmi,
262			&ipmi->ipmi_id, dev, 0);
263	if (rc) {
264		dev_warn(dev, "IPMI SMI registration failed (%d)\n", rc);
265		goto err_free_msg;
266	}
267
268	dev_set_drvdata(dev, ipmi);
269	return 0;
270
271err_free_msg:
272	devm_kfree(dev, ipmi->opal_msg);
273err_unregister:
274	opal_notifier_unregister(&ipmi->event_nb);
275err_free:
276	devm_kfree(dev, ipmi);
277	return rc;
278}
279
280static int ipmi_powernv_remove(struct platform_device *pdev)
281{
282	struct ipmi_smi_powernv *smi = dev_get_drvdata(&pdev->dev);
283
284	ipmi_unregister_smi(smi->intf);
285	opal_notifier_unregister(&smi->event_nb);
286	return 0;
287}
288
289static const struct of_device_id ipmi_powernv_match[] = {
290	{ .compatible = "ibm,opal-ipmi" },
291	{ },
292};
293
294
295static struct platform_driver powernv_ipmi_driver = {
296	.driver = {
297		.name		= "ipmi-powernv",
298		.owner		= THIS_MODULE,
299		.of_match_table	= ipmi_powernv_match,
300	},
301	.probe	= ipmi_powernv_probe,
302	.remove	= ipmi_powernv_remove,
303};
304
305
306module_platform_driver(powernv_ipmi_driver);
307
308MODULE_DEVICE_TABLE(of, ipmi_powernv_match);
309MODULE_DESCRIPTION("powernv IPMI driver");
310MODULE_AUTHOR("Jeremy Kerr <jk@ozlabs.org>");
311MODULE_LICENSE("GPL");
312