opal-dump.c 10 KB

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