opal-dump.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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, S_IRUGO, dump_id_show, NULL);
  93. static struct dump_attribute type_attribute =
  94. __ATTR(type, S_IRUGO, 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. *dump_id = be32_to_cpu(id);
  188. *dump_size = be32_to_cpu(size);
  189. *dump_type = be32_to_cpu(type);
  190. if (rc)
  191. pr_warn("%s: Failed to get dump info (%d)\n",
  192. __func__, rc);
  193. return rc;
  194. }
  195. static int64_t dump_read_data(struct dump_obj *dump)
  196. {
  197. struct opal_sg_list *list;
  198. uint64_t addr;
  199. int64_t rc;
  200. /* Allocate memory */
  201. dump->buffer = vzalloc(PAGE_ALIGN(dump->size));
  202. if (!dump->buffer) {
  203. pr_err("%s : Failed to allocate memory\n", __func__);
  204. rc = -ENOMEM;
  205. goto out;
  206. }
  207. /* Generate SG list */
  208. list = opal_vmalloc_to_sg_list(dump->buffer, dump->size);
  209. if (!list) {
  210. rc = -ENOMEM;
  211. goto out;
  212. }
  213. /* First entry address */
  214. addr = __pa(list);
  215. /* Fetch data */
  216. rc = OPAL_BUSY_EVENT;
  217. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  218. rc = opal_dump_read(dump->id, addr);
  219. if (rc == OPAL_BUSY_EVENT) {
  220. opal_poll_events(NULL);
  221. msleep(20);
  222. }
  223. }
  224. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL)
  225. pr_warn("%s: Extract dump failed for ID 0x%x\n",
  226. __func__, dump->id);
  227. /* Free SG list */
  228. opal_free_sg_list(list);
  229. out:
  230. return rc;
  231. }
  232. static ssize_t dump_attr_read(struct file *filep, struct kobject *kobj,
  233. struct bin_attribute *bin_attr,
  234. char *buffer, loff_t pos, size_t count)
  235. {
  236. ssize_t rc;
  237. struct dump_obj *dump = to_dump_obj(kobj);
  238. if (!dump->buffer) {
  239. rc = dump_read_data(dump);
  240. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
  241. vfree(dump->buffer);
  242. dump->buffer = NULL;
  243. return -EIO;
  244. }
  245. if (rc == OPAL_PARTIAL) {
  246. /* On a partial read, we just return EIO
  247. * and rely on userspace to ask us to try
  248. * again.
  249. */
  250. pr_info("%s: Platform dump partially read. ID = 0x%x\n",
  251. __func__, dump->id);
  252. return -EIO;
  253. }
  254. }
  255. memcpy(buffer, dump->buffer + pos, count);
  256. /* You may think we could free the dump buffer now and retrieve
  257. * it again later if needed, but due to current firmware limitation,
  258. * that's not the case. So, once read into userspace once,
  259. * we keep the dump around until it's acknowledged by userspace.
  260. */
  261. return count;
  262. }
  263. static struct dump_obj *create_dump_obj(uint32_t id, size_t size,
  264. uint32_t type)
  265. {
  266. struct dump_obj *dump;
  267. int rc;
  268. dump = kzalloc(sizeof(*dump), GFP_KERNEL);
  269. if (!dump)
  270. return NULL;
  271. dump->kobj.kset = dump_kset;
  272. kobject_init(&dump->kobj, &dump_ktype);
  273. sysfs_bin_attr_init(&dump->dump_attr);
  274. dump->dump_attr.attr.name = "dump";
  275. dump->dump_attr.attr.mode = 0400;
  276. dump->dump_attr.size = size;
  277. dump->dump_attr.read = dump_attr_read;
  278. dump->id = id;
  279. dump->size = size;
  280. dump->type = type;
  281. rc = kobject_add(&dump->kobj, NULL, "0x%x-0x%x", type, id);
  282. if (rc) {
  283. kobject_put(&dump->kobj);
  284. return NULL;
  285. }
  286. rc = sysfs_create_bin_file(&dump->kobj, &dump->dump_attr);
  287. if (rc) {
  288. kobject_put(&dump->kobj);
  289. return NULL;
  290. }
  291. pr_info("%s: New platform dump. ID = 0x%x Size %u\n",
  292. __func__, dump->id, dump->size);
  293. kobject_uevent(&dump->kobj, KOBJ_ADD);
  294. return dump;
  295. }
  296. static irqreturn_t process_dump(int irq, void *data)
  297. {
  298. int rc;
  299. uint32_t dump_id, dump_size, dump_type;
  300. struct dump_obj *dump;
  301. char name[22];
  302. struct kobject *kobj;
  303. rc = dump_read_info(&dump_id, &dump_size, &dump_type);
  304. if (rc != OPAL_SUCCESS)
  305. return rc;
  306. sprintf(name, "0x%x-0x%x", dump_type, dump_id);
  307. /* we may get notified twice, let's handle
  308. * that gracefully and not create two conflicting
  309. * entries.
  310. */
  311. kobj = kset_find_obj(dump_kset, name);
  312. if (kobj) {
  313. /* Drop reference added by kset_find_obj() */
  314. kobject_put(kobj);
  315. return 0;
  316. }
  317. dump = create_dump_obj(dump_id, dump_size, dump_type);
  318. if (!dump)
  319. return -1;
  320. return IRQ_HANDLED;
  321. }
  322. void __init opal_platform_dump_init(void)
  323. {
  324. int rc;
  325. int dump_irq;
  326. /* ELOG not supported by firmware */
  327. if (!opal_check_token(OPAL_DUMP_READ))
  328. return;
  329. dump_kset = kset_create_and_add("dump", NULL, opal_kobj);
  330. if (!dump_kset) {
  331. pr_warn("%s: Failed to create dump kset\n", __func__);
  332. return;
  333. }
  334. rc = sysfs_create_group(&dump_kset->kobj, &initiate_attr_group);
  335. if (rc) {
  336. pr_warn("%s: Failed to create initiate dump attr group\n",
  337. __func__);
  338. kobject_put(&dump_kset->kobj);
  339. return;
  340. }
  341. dump_irq = opal_event_request(ilog2(OPAL_EVENT_DUMP_AVAIL));
  342. if (!dump_irq) {
  343. pr_err("%s: Can't register OPAL event irq (%d)\n",
  344. __func__, dump_irq);
  345. return;
  346. }
  347. rc = request_threaded_irq(dump_irq, NULL, process_dump,
  348. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  349. "opal-dump", NULL);
  350. if (rc) {
  351. pr_err("%s: Can't request OPAL event irq (%d)\n",
  352. __func__, rc);
  353. return;
  354. }
  355. if (opal_check_token(OPAL_DUMP_RESEND))
  356. opal_dump_resend_notification();
  357. }