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