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