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