opal-dump.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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, 0666, dump_id_show, NULL);
  92. static struct dump_attribute type_attribute =
  93. __ATTR(type, 0666, 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 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 FipS 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. dump_fips_init(DUMP_TYPE_FSP);
  117. pr_info("%s: Initiated FSP dump\n", __func__);
  118. return count;
  119. }
  120. static struct dump_attribute initiate_attribute =
  121. __ATTR(initiate_dump, 0600, init_dump_show, init_dump_store);
  122. static struct attribute *initiate_attrs[] = {
  123. &initiate_attribute.attr,
  124. NULL,
  125. };
  126. static struct attribute_group initiate_attr_group = {
  127. .attrs = initiate_attrs,
  128. };
  129. static struct kset *dump_kset;
  130. static ssize_t dump_attr_show(struct kobject *kobj,
  131. struct attribute *attr,
  132. char *buf)
  133. {
  134. struct dump_attribute *attribute;
  135. struct dump_obj *dump;
  136. attribute = to_dump_attr(attr);
  137. dump = to_dump_obj(kobj);
  138. if (!attribute->show)
  139. return -EIO;
  140. return attribute->show(dump, attribute, buf);
  141. }
  142. static ssize_t dump_attr_store(struct kobject *kobj,
  143. struct attribute *attr,
  144. const char *buf, size_t len)
  145. {
  146. struct dump_attribute *attribute;
  147. struct dump_obj *dump;
  148. attribute = to_dump_attr(attr);
  149. dump = to_dump_obj(kobj);
  150. if (!attribute->store)
  151. return -EIO;
  152. return attribute->store(dump, attribute, buf, len);
  153. }
  154. static const struct sysfs_ops dump_sysfs_ops = {
  155. .show = dump_attr_show,
  156. .store = dump_attr_store,
  157. };
  158. static void dump_release(struct kobject *kobj)
  159. {
  160. struct dump_obj *dump;
  161. dump = to_dump_obj(kobj);
  162. vfree(dump->buffer);
  163. kfree(dump);
  164. }
  165. static struct attribute *dump_default_attrs[] = {
  166. &id_attribute.attr,
  167. &type_attribute.attr,
  168. &ack_attribute.attr,
  169. NULL,
  170. };
  171. static struct kobj_type dump_ktype = {
  172. .sysfs_ops = &dump_sysfs_ops,
  173. .release = &dump_release,
  174. .default_attrs = dump_default_attrs,
  175. };
  176. static int64_t dump_read_info(uint32_t *dump_id, uint32_t *dump_size, uint32_t *dump_type)
  177. {
  178. __be32 id, size, type;
  179. int rc;
  180. type = cpu_to_be32(0xffffffff);
  181. rc = opal_dump_info2(&id, &size, &type);
  182. if (rc == OPAL_PARAMETER)
  183. rc = opal_dump_info(&id, &size);
  184. *dump_id = be32_to_cpu(id);
  185. *dump_size = be32_to_cpu(size);
  186. *dump_type = be32_to_cpu(type);
  187. if (rc)
  188. pr_warn("%s: Failed to get dump info (%d)\n",
  189. __func__, rc);
  190. return rc;
  191. }
  192. static int64_t dump_read_data(struct dump_obj *dump)
  193. {
  194. struct opal_sg_list *list;
  195. uint64_t addr;
  196. int64_t rc;
  197. /* Allocate memory */
  198. dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
  199. if (!dump->buffer) {
  200. pr_err("%s : Failed to allocate memory\n", __func__);
  201. rc = -ENOMEM;
  202. goto out;
  203. }
  204. /* Generate SG list */
  205. list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
  206. if (!list) {
  207. rc = -ENOMEM;
  208. goto out;
  209. }
  210. /* First entry address */
  211. addr = __pa(list);
  212. /* Fetch data */
  213. rc = OPAL_BUSY_EVENT;
  214. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  215. rc = opal_dump_read(dump->id, addr);
  216. if (rc == OPAL_BUSY_EVENT) {
  217. opal_poll_events(NULL);
  218. msleep(20);
  219. }
  220. }
  221. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
  222. pr_warn("%s: Extract dump failed for ID 0x%x\n",
  223. __func__, dump->id);
  224. /* Free SG list */
  225. opal_free_sg_list(list);
  226. out:
  227. return rc;
  228. }
  229. static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
  230. struct bin_attribute *bin_attr,
  231. char *buffer, loff_t pos, size_t count)
  232. {
  233. ssize_t rc;
  234. struct dump_obj *dump = to_dump_obj(kobj);
  235. if (!dump->buffer) {
  236. rc = dump_read_data(dump);
  237. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
  238. vfree(dump->buffer);
  239. dump->buffer = NULL;
  240. return -EIO;
  241. }
  242. if (rc == OPAL_PARTIAL) {
  243. /* On a partial read, we just return EIO
  244. * and rely on userspace to ask us to try
  245. * again.
  246. */
  247. pr_info("%s: Platform dump partially read.ID = 0x%x\n",
  248. __func__, dump->id);
  249. return -EIO;
  250. }
  251. }
  252. memcpy(buffer, dump->buffer + pos, count);
  253. /* You may think we could free the dump buffer now and retrieve
  254. * it again later if needed, but due to current firmware limitation,
  255. * that's not the case. So, once read into userspace once,
  256. * we keep the dump around until it's acknowledged by userspace.
  257. */
  258. return count;
  259. }
  260. static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
  261. uint32_t type)
  262. {
  263. struct dump_obj *dump;
  264. int rc;
  265. dump = kzalloc(sizeof(*dump), GFP_KERNEL);
  266. if (!dump)
  267. return NULL;
  268. dump->kobj.kset = dump_kset;
  269. kobject_init(&dump->kobj, &dump_ktype);
  270. sysfs_bin_attr_init(&dump->dump_attr);
  271. dump->dump_attr.attr.name = "dump";
  272. dump->dump_attr.attr.mode = 0400;
  273. dump->dump_attr.size = size;
  274. dump->dump_attr.read = dump_attr_read;
  275. dump->id = id;
  276. dump->size = size;
  277. dump->type = type;
  278. rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
  279. if (rc) {
  280. kobject_put(&dump->kobj);
  281. return NULL;
  282. }
  283. rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
  284. if (rc) {
  285. kobject_put(&dump->kobj);
  286. return NULL;
  287. }
  288. pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
  289. __func__, dump->id, dump->size);
  290. kobject_uevent(&dump->kobj, KOBJ_ADD);
  291. return dump;
  292. }
  293. static int process_dump(void)
  294. {
  295. int rc;
  296. uint32_t dump_id, dump_size, dump_type;
  297. struct dump_obj *dump;
  298. char name[22];
  299. rc = dump_read_info(&dump_id, &dump_size, &dump_type);
  300. if (rc != OPAL_SUCCESS)
  301. return rc;
  302. sprintf(name, "0x%x-0x%x", dump_type, dump_id);
  303. /* we may get notified twice, let's handle
  304. * that gracefully and not create two conflicting
  305. * entries.
  306. */
  307. if (kset_find_obj(dump_kset, name))
  308. return 0;
  309. dump = create_dump_obj(dump_id, dump_size, dump_type);
  310. if (!dump)
  311. return -1;
  312. return 0;
  313. }
  314. static void dump_work_fn(struct work_struct *work)
  315. {
  316. process_dump();
  317. }
  318. static DECLARE_WORK(dump_work, dump_work_fn);
  319. static void schedule_process_dump(void)
  320. {
  321. schedule_work(&dump_work);
  322. }
  323. /*
  324. * New dump available notification
  325. *
  326. * Once we get notification, we add sysfs entries for it.
  327. * We only fetch the dump on demand, and create sysfs asynchronously.
  328. */
  329. static int dump_event(struct notifier_block *nb,
  330. unsigned long events, void *change)
  331. {
  332. if (events & OPAL_EVENT_DUMP_AVAIL)
  333. schedule_process_dump();
  334. return 0;
  335. }
  336. static struct notifier_block dump_nb = {
  337. .notifier_call = dump_event,
  338. .next = NULL,
  339. .priority = 0
  340. };
  341. void __init opal_platform_dump_init(void)
  342. {
  343. int rc;
  344. dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
  345. if (!dump_kset) {
  346. pr_warn("%s: Failed to create dump kset\n", __func__);
  347. return;
  348. }
  349. rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
  350. if (rc) {
  351. pr_warn("%s: Failed to create initiate dump attr group\n",
  352. __func__);
  353. kobject_put(&dump_kset->kobj);
  354. return;
  355. }
  356. rc = opal_notifier_register(&dump_nb);
  357. if (rc) {
  358. pr_warn("%s: Can't register OPAL event notifier (%d)\n",
  359. __func__, rc);
  360. return;
  361. }
  362. opal_dump_resend_notification();
  363. }