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