1 /* 2 * c 2001 PPC 64 Team, IBM Corp 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 * 9 * /dev/nvram driver for PPC64 10 * 11 * This perhaps should live in drivers/char 12 */ 13 14 15 #include <linux/types.h> 16 #include <linux/errno.h> 17 #include <linux/init.h> 18 #include <linux/spinlock.h> 19 #include <linux/slab.h> 20 #include <linux/kmsg_dump.h> 21 #include <asm/uaccess.h> 22 #include <asm/nvram.h> 23 #include <asm/rtas.h> 24 #include <asm/prom.h> 25 #include <asm/machdep.h> 26 27 /* Max bytes to read/write in one go */ 28 #define NVRW_CNT 0x20 29 30 static unsigned int nvram_size; 31 static int nvram_fetch, nvram_store; 32 static char nvram_buf[NVRW_CNT]; /* assume this is in the first 4GB */ 33 static DEFINE_SPINLOCK(nvram_lock); 34 35 struct err_log_info { 36 int error_type; 37 unsigned int seq_num; 38 }; 39 40 struct nvram_os_partition { 41 const char *name; 42 int req_size; /* desired size, in bytes */ 43 int min_size; /* minimum acceptable size (0 means req_size) */ 44 long size; /* size of data portion (excluding err_log_info) */ 45 long index; /* offset of data portion of partition */ 46 }; 47 48 static struct nvram_os_partition rtas_log_partition = { 49 .name = "ibm,rtas-log", 50 .req_size = 2079, 51 .min_size = 1055, 52 .index = -1 53 }; 54 55 static struct nvram_os_partition oops_log_partition = { 56 .name = "lnx,oops-log", 57 .req_size = 4000, 58 .min_size = 2000, 59 .index = -1 60 }; 61 62 static const char *pseries_nvram_os_partitions[] = { 63 "ibm,rtas-log", 64 "lnx,oops-log", 65 NULL 66 }; 67 68 static void oops_to_nvram(struct kmsg_dumper *dumper, 69 enum kmsg_dump_reason reason, 70 const char *old_msgs, unsigned long old_len, 71 const char *new_msgs, unsigned long new_len); 72 73 static struct kmsg_dumper nvram_kmsg_dumper = { 74 .dump = oops_to_nvram 75 }; 76 77 /* See clobbering_unread_rtas_event() */ 78 #define NVRAM_RTAS_READ_TIMEOUT 5 /* seconds */ 79 static unsigned long last_unread_rtas_event; /* timestamp */ 80 81 /* We preallocate oops_buf during init to avoid kmalloc during oops/panic. */ 82 static char *oops_buf; 83 84 static ssize_t pSeries_nvram_read(char *buf, size_t count, loff_t *index) 85 { 86 unsigned int i; 87 unsigned long len; 88 int done; 89 unsigned long flags; 90 char *p = buf; 91 92 93 if (nvram_size == 0 || nvram_fetch == RTAS_UNKNOWN_SERVICE) 94 return -ENODEV; 95 96 if (*index >= nvram_size) 97 return 0; 98 99 i = *index; 100 if (i + count > nvram_size) 101 count = nvram_size - i; 102 103 spin_lock_irqsave(&nvram_lock, flags); 104 105 for (; count != 0; count -= len) { 106 len = count; 107 if (len > NVRW_CNT) 108 len = NVRW_CNT; 109 110 if ((rtas_call(nvram_fetch, 3, 2, &done, i, __pa(nvram_buf), 111 len) != 0) || len != done) { 112 spin_unlock_irqrestore(&nvram_lock, flags); 113 return -EIO; 114 } 115 116 memcpy(p, nvram_buf, len); 117 118 p += len; 119 i += len; 120 } 121 122 spin_unlock_irqrestore(&nvram_lock, flags); 123 124 *index = i; 125 return p - buf; 126 } 127 128 static ssize_t pSeries_nvram_write(char *buf, size_t count, loff_t *index) 129 { 130 unsigned int i; 131 unsigned long len; 132 int done; 133 unsigned long flags; 134 const char *p = buf; 135 136 if (nvram_size == 0 || nvram_store == RTAS_UNKNOWN_SERVICE) 137 return -ENODEV; 138 139 if (*index >= nvram_size) 140 return 0; 141 142 i = *index; 143 if (i + count > nvram_size) 144 count = nvram_size - i; 145 146 spin_lock_irqsave(&nvram_lock, flags); 147 148 for (; count != 0; count -= len) { 149 len = count; 150 if (len > NVRW_CNT) 151 len = NVRW_CNT; 152 153 memcpy(nvram_buf, p, len); 154 155 if ((rtas_call(nvram_store, 3, 2, &done, i, __pa(nvram_buf), 156 len) != 0) || len != done) { 157 spin_unlock_irqrestore(&nvram_lock, flags); 158 return -EIO; 159 } 160 161 p += len; 162 i += len; 163 } 164 spin_unlock_irqrestore(&nvram_lock, flags); 165 166 *index = i; 167 return p - buf; 168 } 169 170 static ssize_t pSeries_nvram_get_size(void) 171 { 172 return nvram_size ? nvram_size : -ENODEV; 173 } 174 175 176 /* nvram_write_os_partition, nvram_write_error_log 177 * 178 * We need to buffer the error logs into nvram to ensure that we have 179 * the failure information to decode. If we have a severe error there 180 * is no way to guarantee that the OS or the machine is in a state to 181 * get back to user land and write the error to disk. For example if 182 * the SCSI device driver causes a Machine Check by writing to a bad 183 * IO address, there is no way of guaranteeing that the device driver 184 * is in any state that is would also be able to write the error data 185 * captured to disk, thus we buffer it in NVRAM for analysis on the 186 * next boot. 187 * 188 * In NVRAM the partition containing the error log buffer will looks like: 189 * Header (in bytes): 190 * +-----------+----------+--------+------------+------------------+ 191 * | signature | checksum | length | name | data | 192 * |0 |1 |2 3|4 15|16 length-1| 193 * +-----------+----------+--------+------------+------------------+ 194 * 195 * The 'data' section would look like (in bytes): 196 * +--------------+------------+-----------------------------------+ 197 * | event_logged | sequence # | error log | 198 * |0 3|4 7|8 error_log_size-1| 199 * +--------------+------------+-----------------------------------+ 200 * 201 * event_logged: 0 if event has not been logged to syslog, 1 if it has 202 * sequence #: The unique sequence # for each event. (until it wraps) 203 * error log: The error log from event_scan 204 */ 205 int nvram_write_os_partition(struct nvram_os_partition *part, char * buff, 206 int length, unsigned int err_type, unsigned int error_log_cnt) 207 { 208 int rc; 209 loff_t tmp_index; 210 struct err_log_info info; 211 212 if (part->index == -1) { 213 return -ESPIPE; 214 } 215 216 if (length > part->size) { 217 length = part->size; 218 } 219 220 info.error_type = err_type; 221 info.seq_num = error_log_cnt; 222 223 tmp_index = part->index; 224 225 rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index); 226 if (rc <= 0) { 227 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); 228 return rc; 229 } 230 231 rc = ppc_md.nvram_write(buff, length, &tmp_index); 232 if (rc <= 0) { 233 pr_err("%s: Failed nvram_write (%d)\n", __FUNCTION__, rc); 234 return rc; 235 } 236 237 return 0; 238 } 239 240 int nvram_write_error_log(char * buff, int length, 241 unsigned int err_type, unsigned int error_log_cnt) 242 { 243 int rc = nvram_write_os_partition(&rtas_log_partition, buff, length, 244 err_type, error_log_cnt); 245 if (!rc) 246 last_unread_rtas_event = get_seconds(); 247 return rc; 248 } 249 250 /* nvram_read_error_log 251 * 252 * Reads nvram for error log for at most 'length' 253 */ 254 int nvram_read_error_log(char * buff, int length, 255 unsigned int * err_type, unsigned int * error_log_cnt) 256 { 257 int rc; 258 loff_t tmp_index; 259 struct err_log_info info; 260 261 if (rtas_log_partition.index == -1) 262 return -1; 263 264 if (length > rtas_log_partition.size) 265 length = rtas_log_partition.size; 266 267 tmp_index = rtas_log_partition.index; 268 269 rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index); 270 if (rc <= 0) { 271 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc); 272 return rc; 273 } 274 275 rc = ppc_md.nvram_read(buff, length, &tmp_index); 276 if (rc <= 0) { 277 printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc); 278 return rc; 279 } 280 281 *error_log_cnt = info.seq_num; 282 *err_type = info.error_type; 283 284 return 0; 285 } 286 287 /* This doesn't actually zero anything, but it sets the event_logged 288 * word to tell that this event is safely in syslog. 289 */ 290 int nvram_clear_error_log(void) 291 { 292 loff_t tmp_index; 293 int clear_word = ERR_FLAG_ALREADY_LOGGED; 294 int rc; 295 296 if (rtas_log_partition.index == -1) 297 return -1; 298 299 tmp_index = rtas_log_partition.index; 300 301 rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index); 302 if (rc <= 0) { 303 printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); 304 return rc; 305 } 306 last_unread_rtas_event = 0; 307 308 return 0; 309 } 310 311 /* pseries_nvram_init_os_partition 312 * 313 * This sets up a partition with an "OS" signature. 314 * 315 * The general strategy is the following: 316 * 1.) If a partition with the indicated name already exists... 317 * - If it's large enough, use it. 318 * - Otherwise, recycle it and keep going. 319 * 2.) Search for a free partition that is large enough. 320 * 3.) If there's not a free partition large enough, recycle any obsolete 321 * OS partitions and try again. 322 * 4.) Will first try getting a chunk that will satisfy the requested size. 323 * 5.) If a chunk of the requested size cannot be allocated, then try finding 324 * a chunk that will satisfy the minum needed. 325 * 326 * Returns 0 on success, else -1. 327 */ 328 static int __init pseries_nvram_init_os_partition(struct nvram_os_partition 329 *part) 330 { 331 loff_t p; 332 int size; 333 334 /* Scan nvram for partitions */ 335 nvram_scan_partitions(); 336 337 /* Look for ours */ 338 p = nvram_find_partition(part->name, NVRAM_SIG_OS, &size); 339 340 /* Found one but too small, remove it */ 341 if (p && size < part->min_size) { 342 pr_info("nvram: Found too small %s partition," 343 " removing it...\n", part->name); 344 nvram_remove_partition(part->name, NVRAM_SIG_OS, NULL); 345 p = 0; 346 } 347 348 /* Create one if we didn't find */ 349 if (!p) { 350 p = nvram_create_partition(part->name, NVRAM_SIG_OS, 351 part->req_size, part->min_size); 352 if (p == -ENOSPC) { 353 pr_info("nvram: No room to create %s partition, " 354 "deleting any obsolete OS partitions...\n", 355 part->name); 356 nvram_remove_partition(NULL, NVRAM_SIG_OS, 357 pseries_nvram_os_partitions); 358 p = nvram_create_partition(part->name, NVRAM_SIG_OS, 359 part->req_size, part->min_size); 360 } 361 } 362 363 if (p <= 0) { 364 pr_err("nvram: Failed to find or create %s" 365 " partition, err %d\n", part->name, (int)p); 366 return -1; 367 } 368 369 part->index = p; 370 part->size = nvram_get_partition_size(p) - sizeof(struct err_log_info); 371 372 return 0; 373 } 374 375 static void __init nvram_init_oops_partition(int rtas_partition_exists) 376 { 377 int rc; 378 379 rc = pseries_nvram_init_os_partition(&oops_log_partition); 380 if (rc != 0) { 381 if (!rtas_partition_exists) 382 return; 383 pr_notice("nvram: Using %s partition to log both" 384 " RTAS errors and oops/panic reports\n", 385 rtas_log_partition.name); 386 memcpy(&oops_log_partition, &rtas_log_partition, 387 sizeof(rtas_log_partition)); 388 } 389 oops_buf = kmalloc(oops_log_partition.size, GFP_KERNEL); 390 rc = kmsg_dump_register(&nvram_kmsg_dumper); 391 if (rc != 0) { 392 pr_err("nvram: kmsg_dump_register() failed; returned %d\n", rc); 393 kfree(oops_buf); 394 return; 395 } 396 } 397 398 static int __init pseries_nvram_init_log_partitions(void) 399 { 400 int rc; 401 402 rc = pseries_nvram_init_os_partition(&rtas_log_partition); 403 nvram_init_oops_partition(rc == 0); 404 return 0; 405 } 406 machine_arch_initcall(pseries, pseries_nvram_init_log_partitions); 407 408 int __init pSeries_nvram_init(void) 409 { 410 struct device_node *nvram; 411 const unsigned int *nbytes_p; 412 unsigned int proplen; 413 414 nvram = of_find_node_by_type(NULL, "nvram"); 415 if (nvram == NULL) 416 return -ENODEV; 417 418 nbytes_p = of_get_property(nvram, "#bytes", &proplen); 419 if (nbytes_p == NULL || proplen != sizeof(unsigned int)) { 420 of_node_put(nvram); 421 return -EIO; 422 } 423 424 nvram_size = *nbytes_p; 425 426 nvram_fetch = rtas_token("nvram-fetch"); 427 nvram_store = rtas_token("nvram-store"); 428 printk(KERN_INFO "PPC64 nvram contains %d bytes\n", nvram_size); 429 of_node_put(nvram); 430 431 ppc_md.nvram_read = pSeries_nvram_read; 432 ppc_md.nvram_write = pSeries_nvram_write; 433 ppc_md.nvram_size = pSeries_nvram_get_size; 434 435 return 0; 436 } 437 438 /* 439 * Try to capture the last capture_len bytes of the printk buffer. Return 440 * the amount actually captured. 441 */ 442 static size_t capture_last_msgs(const char *old_msgs, size_t old_len, 443 const char *new_msgs, size_t new_len, 444 char *captured, size_t capture_len) 445 { 446 if (new_len >= capture_len) { 447 memcpy(captured, new_msgs + (new_len - capture_len), 448 capture_len); 449 return capture_len; 450 } else { 451 /* Grab the end of old_msgs. */ 452 size_t old_tail_len = min(old_len, capture_len - new_len); 453 memcpy(captured, old_msgs + (old_len - old_tail_len), 454 old_tail_len); 455 memcpy(captured + old_tail_len, new_msgs, new_len); 456 return old_tail_len + new_len; 457 } 458 } 459 460 /* 461 * Are we using the ibm,rtas-log for oops/panic reports? And if so, 462 * would logging this oops/panic overwrite an RTAS event that rtas_errd 463 * hasn't had a chance to read and process? Return 1 if so, else 0. 464 * 465 * We assume that if rtas_errd hasn't read the RTAS event in 466 * NVRAM_RTAS_READ_TIMEOUT seconds, it's probably not going to. 467 */ 468 static int clobbering_unread_rtas_event(void) 469 { 470 return (oops_log_partition.index == rtas_log_partition.index 471 && last_unread_rtas_event 472 && get_seconds() - last_unread_rtas_event <= 473 NVRAM_RTAS_READ_TIMEOUT); 474 } 475 476 /* our kmsg_dump callback */ 477 static void oops_to_nvram(struct kmsg_dumper *dumper, 478 enum kmsg_dump_reason reason, 479 const char *old_msgs, unsigned long old_len, 480 const char *new_msgs, unsigned long new_len) 481 { 482 static unsigned int oops_count = 0; 483 size_t text_len; 484 485 if (clobbering_unread_rtas_event()) 486 return; 487 488 text_len = capture_last_msgs(old_msgs, old_len, new_msgs, new_len, 489 oops_buf, oops_log_partition.size); 490 (void) nvram_write_os_partition(&oops_log_partition, oops_buf, 491 (int) text_len, ERR_TYPE_KERNEL_PANIC, ++oops_count); 492 } 493