opal-elog.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Error log support on PowerNV.
  3. *
  4. * Copyright 2013,2014 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/of.h>
  15. #include <linux/slab.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/fs.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/fcntl.h>
  20. #include <linux/kobject.h>
  21. #include <asm/uaccess.h>
  22. #include <asm/opal.h>
  23. struct elog_obj {
  24. struct kobject kobj;
  25. struct bin_attribute raw_attr;
  26. uint64_t id;
  27. uint64_t type;
  28. size_t size;
  29. char *buffer;
  30. };
  31. #define to_elog_obj(x) container_of(x, struct elog_obj, kobj)
  32. struct elog_attribute {
  33. struct attribute attr;
  34. ssize_t (*show)(struct elog_obj *elog, struct elog_attribute *attr,
  35. char *buf);
  36. ssize_t (*store)(struct elog_obj *elog, struct elog_attribute *attr,
  37. const char *buf, size_t count);
  38. };
  39. #define to_elog_attr(x) container_of(x, struct elog_attribute, attr)
  40. static ssize_t elog_id_show(struct elog_obj *elog_obj,
  41. struct elog_attribute *attr,
  42. char *buf)
  43. {
  44. return sprintf(buf, "0x%llx\n", elog_obj->id);
  45. }
  46. static const char *elog_type_to_string(uint64_t type)
  47. {
  48. switch (type) {
  49. case 0: return "PEL";
  50. default: return "unknown";
  51. }
  52. }
  53. static ssize_t elog_type_show(struct elog_obj *elog_obj,
  54. struct elog_attribute *attr,
  55. char *buf)
  56. {
  57. return sprintf(buf, "0x%llx %s\n",
  58. elog_obj->type,
  59. elog_type_to_string(elog_obj->type));
  60. }
  61. static ssize_t elog_ack_show(struct elog_obj *elog_obj,
  62. struct elog_attribute *attr,
  63. char *buf)
  64. {
  65. return sprintf(buf, "ack - acknowledge log message\n");
  66. }
  67. static ssize_t elog_ack_store(struct elog_obj *elog_obj,
  68. struct elog_attribute *attr,
  69. const char *buf,
  70. size_t count)
  71. {
  72. opal_send_ack_elog(elog_obj->id);
  73. sysfs_remove_file_self(&elog_obj->kobj, &attr->attr);
  74. kobject_put(&elog_obj->kobj);
  75. return count;
  76. }
  77. static struct elog_attribute id_attribute =
  78. __ATTR(id, S_IRUGO, elog_id_show, NULL);
  79. static struct elog_attribute type_attribute =
  80. __ATTR(type, S_IRUGO, elog_type_show, NULL);
  81. static struct elog_attribute ack_attribute =
  82. __ATTR(acknowledge, 0660, elog_ack_show, elog_ack_store);
  83. static struct kset *elog_kset;
  84. static ssize_t elog_attr_show(struct kobject *kobj,
  85. struct attribute *attr,
  86. char *buf)
  87. {
  88. struct elog_attribute *attribute;
  89. struct elog_obj *elog;
  90. attribute = to_elog_attr(attr);
  91. elog = to_elog_obj(kobj);
  92. if (!attribute->show)
  93. return -EIO;
  94. return attribute->show(elog, attribute, buf);
  95. }
  96. static ssize_t elog_attr_store(struct kobject *kobj,
  97. struct attribute *attr,
  98. const char *buf, size_t len)
  99. {
  100. struct elog_attribute *attribute;
  101. struct elog_obj *elog;
  102. attribute = to_elog_attr(attr);
  103. elog = to_elog_obj(kobj);
  104. if (!attribute->store)
  105. return -EIO;
  106. return attribute->store(elog, attribute, buf, len);
  107. }
  108. static const struct sysfs_ops elog_sysfs_ops = {
  109. .show = elog_attr_show,
  110. .store = elog_attr_store,
  111. };
  112. static void elog_release(struct kobject *kobj)
  113. {
  114. struct elog_obj *elog;
  115. elog = to_elog_obj(kobj);
  116. kfree(elog->buffer);
  117. kfree(elog);
  118. }
  119. static struct attribute *elog_default_attrs[] = {
  120. &id_attribute.attr,
  121. &type_attribute.attr,
  122. &ack_attribute.attr,
  123. NULL,
  124. };
  125. static struct kobj_type elog_ktype = {
  126. .sysfs_ops = &elog_sysfs_ops,
  127. .release = &elog_release,
  128. .default_attrs = elog_default_attrs,
  129. };
  130. /* Maximum size of a single log on FSP is 16KB */
  131. #define OPAL_MAX_ERRLOG_SIZE 16384
  132. static ssize_t raw_attr_read(struct file *filep, struct kobject *kobj,
  133. struct bin_attribute *bin_attr,
  134. char *buffer, loff_t pos, size_t count)
  135. {
  136. int opal_rc;
  137. struct elog_obj *elog = to_elog_obj(kobj);
  138. /* We may have had an error reading before, so let's retry */
  139. if (!elog->buffer) {
  140. elog->buffer = kzalloc(elog->size, GFP_KERNEL);
  141. if (!elog->buffer)
  142. return -EIO;
  143. opal_rc = opal_read_elog(__pa(elog->buffer),
  144. elog->size, elog->id);
  145. if (opal_rc != OPAL_SUCCESS) {
  146. pr_err("ELOG: log read failed for log-id=%llx\n",
  147. elog->id);
  148. kfree(elog->buffer);
  149. elog->buffer = NULL;
  150. return -EIO;
  151. }
  152. }
  153. memcpy(buffer, elog->buffer + pos, count);
  154. return count;
  155. }
  156. static struct elog_obj *create_elog_obj(uint64_t id, size_t size, uint64_t type)
  157. {
  158. struct elog_obj *elog;
  159. int rc;
  160. elog = kzalloc(sizeof(*elog), GFP_KERNEL);
  161. if (!elog)
  162. return NULL;
  163. elog->kobj.kset = elog_kset;
  164. kobject_init(&elog->kobj, &elog_ktype);
  165. sysfs_bin_attr_init(&elog->raw_attr);
  166. elog->raw_attr.attr.name = "raw";
  167. elog->raw_attr.attr.mode = 0400;
  168. elog->raw_attr.size = size;
  169. elog->raw_attr.read = raw_attr_read;
  170. elog->id = id;
  171. elog->size = size;
  172. elog->type = type;
  173. elog->buffer = kzalloc(elog->size, GFP_KERNEL);
  174. if (elog->buffer) {
  175. rc = opal_read_elog(__pa(elog->buffer),
  176. elog->size, elog->id);
  177. if (rc != OPAL_SUCCESS) {
  178. pr_err("ELOG: log read failed for log-id=%llx\n",
  179. elog->id);
  180. kfree(elog->buffer);
  181. elog->buffer = NULL;
  182. }
  183. }
  184. rc = kobject_add(&elog->kobj, NULL, "0x%llx", id);
  185. if (rc) {
  186. kobject_put(&elog->kobj);
  187. return NULL;
  188. }
  189. rc = sysfs_create_bin_file(&elog->kobj, &elog->raw_attr);
  190. if (rc) {
  191. kobject_put(&elog->kobj);
  192. return NULL;
  193. }
  194. kobject_uevent(&elog->kobj, KOBJ_ADD);
  195. return elog;
  196. }
  197. static irqreturn_t elog_event(int irq, void *data)
  198. {
  199. __be64 size;
  200. __be64 id;
  201. __be64 type;
  202. uint64_t elog_size;
  203. uint64_t log_id;
  204. uint64_t elog_type;
  205. int rc;
  206. char name[2+16+1];
  207. struct kobject *kobj;
  208. rc = opal_get_elog_size(&id, &size, &type);
  209. if (rc != OPAL_SUCCESS) {
  210. pr_err("ELOG: OPAL log info read failed\n");
  211. return IRQ_HANDLED;
  212. }
  213. elog_size = be64_to_cpu(size);
  214. log_id = be64_to_cpu(id);
  215. elog_type = be64_to_cpu(type);
  216. WARN_ON(elog_size > OPAL_MAX_ERRLOG_SIZE);
  217. if (elog_size >= OPAL_MAX_ERRLOG_SIZE)
  218. elog_size = OPAL_MAX_ERRLOG_SIZE;
  219. sprintf(name, "0x%llx", log_id);
  220. /* we may get notified twice, let's handle
  221. * that gracefully and not create two conflicting
  222. * entries.
  223. */
  224. kobj = kset_find_obj(elog_kset, name);
  225. if (kobj) {
  226. /* Drop reference added by kset_find_obj() */
  227. kobject_put(kobj);
  228. return IRQ_HANDLED;
  229. }
  230. create_elog_obj(log_id, elog_size, elog_type);
  231. return IRQ_HANDLED;
  232. }
  233. int __init opal_elog_init(void)
  234. {
  235. int rc = 0, irq;
  236. /* ELOG not supported by firmware */
  237. if (!opal_check_token(OPAL_ELOG_READ))
  238. return -1;
  239. elog_kset = kset_create_and_add("elog", NULL, opal_kobj);
  240. if (!elog_kset) {
  241. pr_warn("%s: failed to create elog kset\n", __func__);
  242. return -1;
  243. }
  244. irq = opal_event_request(ilog2(OPAL_EVENT_ERROR_LOG_AVAIL));
  245. if (!irq) {
  246. pr_err("%s: Can't register OPAL event irq (%d)\n",
  247. __func__, irq);
  248. return irq;
  249. }
  250. rc = request_threaded_irq(irq, NULL, elog_event,
  251. IRQF_TRIGGER_HIGH | IRQF_ONESHOT, "opal-elog", NULL);
  252. if (rc) {
  253. pr_err("%s: Can't request OPAL event irq (%d)\n",
  254. __func__, rc);
  255. return rc;
  256. }
  257. /* We are now ready to pull error logs from opal. */
  258. if (opal_check_token(OPAL_ELOG_RESEND))
  259. opal_resend_pending_logs();
  260. return 0;
  261. }