opal-dump.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * PowerNV OPAL Dump Interface
  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/kobject.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/delay.h>
  17. #include <asm/opal.h>
  18. #define DUMP_TYPE_FSP 0x01
  19. struct dump_obj {
  20. struct kobject kobj;
  21. struct bin_attribute dump_attr;
  22. uint32_t id; /* becomes object name */
  23. uint32_t type;
  24. uint32_t size;
  25. char *buffer;
  26. };
  27. #define to_dump_obj(x) container_of(x, struct dump_obj, kobj)
  28. struct dump_attribute {
  29. struct attribute attr;
  30. ssize_t (*show)(struct dump_obj *dump, struct dump_attribute *attr,
  31. char *buf);
  32. ssize_t (*store)(struct dump_obj *dump, struct dump_attribute *attr,
  33. const char *buf, size_t count);
  34. };
  35. #define to_dump_attr(x) container_of(x, struct dump_attribute, attr)
  36. static ssize_t dump_id_show(struct dump_obj *dump_obj,
  37. struct dump_attribute *attr,
  38. char *buf)
  39. {
  40. return sprintf(buf, "0x%x\n", dump_obj->id);
  41. }
  42. static const char* dump_type_to_string(uint32_t type)
  43. {
  44. switch (type) {
  45. case 0x01: return "SP Dump";
  46. case 0x02: return "System/Platform Dump";
  47. case 0x03: return "SMA Dump";
  48. default: return "unknown";
  49. }
  50. }
  51. static ssize_t dump_type_show(struct dump_obj *dump_obj,
  52. struct dump_attribute *attr,
  53. char *buf)
  54. {
  55. return sprintf(buf, "0x%x %s\n", dump_obj->type,
  56. dump_type_to_string(dump_obj->type));
  57. }
  58. static ssize_t dump_ack_show(struct dump_obj *dump_obj,
  59. struct dump_attribute *attr,
  60. char *buf)
  61. {
  62. return sprintf(buf, "ack - acknowledge dump\n");
  63. }
  64. /*
  65. * Send acknowledgement to OPAL
  66. */
  67. static int64_t dump_send_ack(uint32_t dump_id)
  68. {
  69. int rc;
  70. rc = opal_dump_ack(dump_id);
  71. if (rc)
  72. pr_warn("%s: Failed to send ack to Dump ID 0x%x (%d)\n",
  73. __func__, dump_id, rc);
  74. return rc;
  75. }
  76. static ssize_t dump_ack_store(struct dump_obj *dump_obj,
  77. struct dump_attribute *attr,
  78. const char *buf,
  79. size_t count)
  80. {
  81. dump_send_ack(dump_obj->id);
  82. sysfs_remove_file_self(&dump_obj->kobj, &attr->attr);
  83. kobject_put(&dump_obj->kobj);
  84. return count;
  85. }
  86. /* Attributes of a dump
  87. * The binary attribute of the dump itself is dynamic
  88. * due to the dynamic size of the dump
  89. */
  90. static struct dump_attribute id_attribute =
  91. __ATTR(id, S_IRUGO, dump_id_show, NULL);
  92. static struct dump_attribute type_attribute =
  93. __ATTR(type, S_IRUGO, dump_type_show, NULL);
  94. static struct dump_attribute ack_attribute =
  95. __ATTR(acknowledge, 0660, dump_ack_show, dump_ack_store);
  96. static ssize_t init_dump_show(struct dump_obj *dump_obj,
  97. struct dump_attribute *attr,
  98. char *buf)
  99. {
  100. return sprintf(buf, "1 - initiate Service Processor(FSP) dump\n");
  101. }
  102. static int64_t dump_fips_init(uint8_t type)
  103. {
  104. int rc;
  105. rc = opal_dump_init(type);
  106. if (rc)
  107. pr_warn("%s: Failed to initiate FSP dump (%d)\n",
  108. __func__, rc);
  109. return rc;
  110. }
  111. static ssize_t init_dump_store(struct dump_obj *dump_obj,
  112. struct dump_attribute *attr,
  113. const char *buf,
  114. size_t count)
  115. {
  116. int rc;
  117. rc = dump_fips_init(DUMP_TYPE_FSP);
  118. if (rc == OPAL_SUCCESS)
  119. pr_info("%s: Initiated FSP dump\n", __func__);
  120. return count;
  121. }
  122. static struct dump_attribute initiate_attribute =
  123. __ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
  124. static struct attribute *initiate_attrs[] = {
  125. &initiate_attribute.attr,
  126. NULL,
  127. };
  128. static struct attribute_group initiate_attr_group = {
  129. .attrs = initiate_attrs,
  130. };
  131. static struct kset *dump_kset;
  132. static ssize_t dump_attr_show(struct kobject *kobj,
  133. struct attribute *attr,
  134. char *buf)
  135. {
  136. struct dump_attribute *attribute;
  137. struct dump_obj *dump;
  138. attribute = to_dump_attr(attr);
  139. dump = to_dump_obj(kobj);
  140. if (!attribute->show)
  141. return -EIO;
  142. return attribute->show(dump, attribute, buf);
  143. }
  144. static ssize_t dump_attr_store(struct kobject *kobj,
  145. struct attribute *attr,
  146. const char *buf, size_t len)
  147. {
  148. struct dump_attribute *attribute;
  149. struct dump_obj *dump;
  150. attribute = to_dump_attr(attr);
  151. dump = to_dump_obj(kobj);
  152. if (!attribute->store)
  153. return -EIO;
  154. return attribute->store(dump, attribute, buf, len);
  155. }
  156. static const struct sysfs_ops dump_sysfs_ops = {
  157. .show = dump_attr_show,
  158. .store = dump_attr_store,
  159. };
  160. static void dump_release(struct kobject *kobj)
  161. {
  162. struct dump_obj *dump;
  163. dump = to_dump_obj(kobj);
  164. vfree(dump->buffer);
  165. kfree(dump);
  166. }
  167. static struct attribute *dump_default_attrs[] = {
  168. &id_attribute.attr,
  169. &type_attribute.attr,
  170. &ack_attribute.attr,
  171. NULL,
  172. };
  173. static struct kobj_type dump_ktype = {
  174. .sysfs_ops = &dump_sysfs_ops,
  175. .release = &dump_release,
  176. .default_attrs = dump_default_attrs,
  177. };
  178. static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
  179. {
  180. __be32 id, size, type;
  181. int rc;
  182. type = cpu_to_be32(0xffffffff);
  183. rc = opal_dump_info2(&id, &size, &type);
  184. if (rc == OPAL_PARAMETER)
  185. rc = opal_dump_info(&id, &size);
  186. *dump_id = be32_to_cpu(id);
  187. *dump_size = be32_to_cpu(size);
  188. *dump_type = be32_to_cpu(type);
  189. if (rc)
  190. pr_warn("%s: Failed to get dump info (%d)\n",
  191. __func__, rc);
  192. return rc;
  193. }
  194. static int64_t dump_read_data(struct dump_obj *dump)
  195. {
  196. struct opal_sg_list *list;
  197. uint64_t addr;
  198. int64_t rc;
  199. /* Allocate memory */
  200. dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
  201. if (!dump->buffer) {
  202. pr_err("%s : Failed to allocate memory\n", __func__);
  203. rc = -ENOMEM;
  204. goto out;
  205. }
  206. /* Generate SG list */
  207. list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
  208. if (!list) {
  209. rc = -ENOMEM;
  210. goto out;
  211. }
  212. /* First entry address */
  213. addr = __pa(list);
  214. /* Fetch data */
  215. rc = OPAL_BUSY_EVENT;
  216. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  217. rc = opal_dump_read(dump->id, addr);
  218. if (rc == OPAL_BUSY_EVENT) {
  219. opal_poll_events(NULL);
  220. msleep(20);
  221. }
  222. }
  223. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
  224. pr_warn("%s: Extract dump failed for ID 0x%x\n",
  225. __func__, dump->id);
  226. /* Free SG list */
  227. opal_free_sg_list(list);
  228. out:
  229. return rc;
  230. }
  231. static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
  232. struct bin_attribute *bin_attr,
  233. char *buffer, loff_t pos, size_t count)
  234. {
  235. ssize_t rc;
  236. struct dump_obj *dump = to_dump_obj(kobj);
  237. if (!dump->buffer) {
  238. rc = dump_read_data(dump);
  239. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
  240. vfree(dump->buffer);
  241. dump->buffer = NULL;
  242. return -EIO;
  243. }
  244. if (rc == OPAL_PARTIAL) {
  245. /* On a partial read, we just return EIO
  246. * and rely on userspace to ask us to try
  247. * again.
  248. */
  249. pr_info("%s: Platform dump partially read. ID = 0x%x\n",
  250. __func__, dump->id);
  251. return -EIO;
  252. }
  253. }
  254. memcpy(buffer, dump->buffer + pos, count);
  255. /* You may think we could free the dump buffer now and retrieve
  256. * it again later if needed, but due to current firmware limitation,
  257. * that's not the case. So, once read into userspace once,
  258. * we keep the dump around until it's acknowledged by userspace.
  259. */
  260. return count;
  261. }
  262. static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
  263. uint32_t type)
  264. {
  265. struct dump_obj *dump;
  266. int rc;
  267. dump = kzalloc(sizeof(*dump), GFP_KERNEL);
  268. if (!dump)
  269. return NULL;
  270. dump->kobj.kset = dump_kset;
  271. kobject_init(&dump->kobj, &dump_ktype);
  272. sysfs_bin_attr_init(&dump->dump_attr);
  273. dump->dump_attr.attr.name = "dump";
  274. dump->dump_attr.attr.mode = 0400;
  275. dump->dump_attr.size = size;
  276. dump->dump_attr.read = dump_attr_read;
  277. dump->id = id;
  278. dump->size = size;
  279. dump->type = type;
  280. rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
  281. if (rc) {
  282. kobject_put(&dump->kobj);
  283. return NULL;
  284. }
  285. rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
  286. if (rc) {
  287. kobject_put(&dump->kobj);
  288. return NULL;
  289. }
  290. pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
  291. __func__, dump->id, dump->size);
  292. kobject_uevent(&dump->kobj, KOBJ_ADD);
  293. return dump;
  294. }
  295. static int process_dump(void)
  296. {
  297. int rc;
  298. uint32_t dump_id, dump_size, dump_type;
  299. struct dump_obj *dump;
  300. char name[22];
  301. rc = dump_read_info(&dump_id, &dump_size, &dump_type);
  302. if (rc != OPAL_SUCCESS)
  303. return rc;
  304. sprintf(name, "0x%x-0x%x", dump_type, dump_id);
  305. /* we may get notified twice, let's handle
  306. * that gracefully and not create two conflicting
  307. * entries.
  308. */
  309. if (kset_find_obj(dump_kset, name))
  310. return 0;
  311. dump = create_dump_obj(dump_id, dump_size, dump_type);
  312. if (!dump)
  313. return -1;
  314. return 0;
  315. }
  316. static void dump_work_fn(struct work_struct *work)
  317. {
  318. process_dump();
  319. }
  320. static DECLARE_WORK(dump_work, dump_work_fn);
  321. static void schedule_process_dump(void)
  322. {
  323. schedule_work(&dump_work);
  324. }
  325. /*
  326. * New dump available notification
  327. *
  328. * Once we get notification, we add sysfs entries for it.
  329. * We only fetch the dump on demand, and create sysfs asynchronously.
  330. */
  331. static int dump_event(struct notifier_block *nb,
  332. unsigned long events, void *change)
  333. {
  334. if (events & OPAL_EVENT_DUMP_AVAIL)
  335. schedule_process_dump();
  336. return 0;
  337. }
  338. static struct notifier_block dump_nb = {
  339. .notifier_call = dump_event,
  340. .next = NULL,
  341. .priority = 0
  342. };
  343. void __init opal_platform_dump_init(void)
  344. {
  345. int rc;
  346. /* ELOG not supported by firmware */
  347. if (!opal_check_token(OPAL_DUMP_READ))
  348. return;
  349. dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
  350. if (!dump_kset) {
  351. pr_warn("%s: Failed to create dump kset\n", __func__);
  352. return;
  353. }
  354. rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
  355. if (rc) {
  356. pr_warn("%s: Failed to create initiate dump attr group\n",
  357. __func__);
  358. kobject_put(&dump_kset->kobj);
  359. return;
  360. }
  361. rc = opal_notifier_register(&dump_nb);
  362. if (rc) {
  363. pr_warn("%s: Can't register OPAL event notifier (%d)\n",
  364. __func__, rc);
  365. return;
  366. }
  367. if (opal_check_token(OPAL_DUMP_RESEND))
  368. opal_dump_resend_notification();
  369. }