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