ipmi_powernv.c 7.5 KB

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