Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * TODO: Split the /dev/nvram part (that one can use |
| 14 | * drivers/char/generic_nvram.c) from the arch & partition |
| 15 | * parsing code. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/fcntl.h> |
| 25 | #include <linux/nvram.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <asm/uaccess.h> |
| 30 | #include <asm/nvram.h> |
| 31 | #include <asm/rtas.h> |
| 32 | #include <asm/prom.h> |
| 33 | #include <asm/machdep.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #undef DEBUG_NVRAM |
| 36 | |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 37 | #define NVRAM_HEADER_LEN sizeof(struct nvram_header) |
| 38 | #define NVRAM_BLOCK_LEN NVRAM_HEADER_LEN |
| 39 | #define NVRAM_MAX_REQ 2079 |
| 40 | #define NVRAM_MIN_REQ 1055 |
Benjamin Herrenschmidt | 74d51d0 | 2010-07-29 14:45:24 +1000 | [diff] [blame] | 41 | |
| 42 | /* If change this size, then change the size of NVNAME_LEN */ |
| 43 | struct nvram_header { |
| 44 | unsigned char signature; |
| 45 | unsigned char checksum; |
| 46 | unsigned short length; |
| 47 | char name[12]; |
| 48 | }; |
| 49 | |
| 50 | struct nvram_partition { |
| 51 | struct list_head partition; |
| 52 | struct nvram_header header; |
| 53 | unsigned int index; |
| 54 | }; |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | static struct nvram_partition * nvram_part; |
| 57 | static long nvram_error_log_index = -1; |
| 58 | static long nvram_error_log_size = 0; |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct err_log_info { |
| 61 | int error_type; |
| 62 | unsigned int seq_num; |
| 63 | }; |
| 64 | |
| 65 | static loff_t dev_nvram_llseek(struct file *file, loff_t offset, int origin) |
| 66 | { |
| 67 | int size; |
| 68 | |
| 69 | if (ppc_md.nvram_size == NULL) |
| 70 | return -ENODEV; |
| 71 | size = ppc_md.nvram_size(); |
| 72 | |
| 73 | switch (origin) { |
| 74 | case 1: |
| 75 | offset += file->f_pos; |
| 76 | break; |
| 77 | case 2: |
| 78 | offset += size; |
| 79 | break; |
| 80 | } |
| 81 | if (offset < 0) |
| 82 | return -EINVAL; |
| 83 | file->f_pos = offset; |
| 84 | return file->f_pos; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | static ssize_t dev_nvram_read(struct file *file, char __user *buf, |
| 89 | size_t count, loff_t *ppos) |
| 90 | { |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 91 | ssize_t ret; |
| 92 | char *tmp = NULL; |
| 93 | ssize_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 95 | ret = -ENODEV; |
| 96 | if (!ppc_md.nvram_size) |
| 97 | goto out; |
| 98 | |
| 99 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | size = ppc_md.nvram_size(); |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 101 | if (*ppos >= size || size < 0) |
| 102 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 104 | count = min_t(size_t, count, size - *ppos); |
| 105 | count = min(count, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 107 | ret = -ENOMEM; |
| 108 | tmp = kmalloc(count, GFP_KERNEL); |
| 109 | if (!tmp) |
| 110 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 112 | ret = ppc_md.nvram_read(tmp, count, ppos); |
| 113 | if (ret <= 0) |
| 114 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 116 | if (copy_to_user(buf, tmp, ret)) |
| 117 | ret = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 119 | out: |
| 120 | kfree(tmp); |
| 121 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | } |
| 124 | |
| 125 | static ssize_t dev_nvram_write(struct file *file, const char __user *buf, |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 126 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 128 | ssize_t ret; |
| 129 | char *tmp = NULL; |
| 130 | ssize_t size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 132 | ret = -ENODEV; |
| 133 | if (!ppc_md.nvram_size) |
| 134 | goto out; |
| 135 | |
| 136 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | size = ppc_md.nvram_size(); |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 138 | if (*ppos >= size || size < 0) |
| 139 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 141 | count = min_t(size_t, count, size - *ppos); |
| 142 | count = min(count, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 144 | ret = -ENOMEM; |
| 145 | tmp = kmalloc(count, GFP_KERNEL); |
| 146 | if (!tmp) |
| 147 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 149 | ret = -EFAULT; |
| 150 | if (copy_from_user(tmp, buf, count)) |
| 151 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Arnd Bergmann | f9ce299 | 2005-12-09 19:21:44 +0100 | [diff] [blame] | 153 | ret = ppc_md.nvram_write(tmp, count, ppos); |
| 154 | |
| 155 | out: |
| 156 | kfree(tmp); |
| 157 | return ret; |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Thomas Gleixner | 3b03fec | 2009-10-14 22:42:28 +0000 | [diff] [blame] | 161 | static long dev_nvram_ioctl(struct file *file, unsigned int cmd, |
| 162 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
| 164 | switch(cmd) { |
| 165 | #ifdef CONFIG_PPC_PMAC |
| 166 | case OBSOLETE_PMAC_NVRAM_GET_OFFSET: |
| 167 | printk(KERN_WARNING "nvram: Using obsolete PMAC_NVRAM_GET_OFFSET ioctl\n"); |
| 168 | case IOC_NVRAM_GET_OFFSET: { |
| 169 | int part, offset; |
| 170 | |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 171 | if (!machine_is(powermac)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | return -EINVAL; |
| 173 | if (copy_from_user(&part, (void __user*)arg, sizeof(part)) != 0) |
| 174 | return -EFAULT; |
| 175 | if (part < pmac_nvram_OF || part > pmac_nvram_NR) |
| 176 | return -EINVAL; |
| 177 | offset = pmac_get_partition(part); |
| 178 | if (offset < 0) |
| 179 | return offset; |
| 180 | if (copy_to_user((void __user*)arg, &offset, sizeof(offset)) != 0) |
| 181 | return -EFAULT; |
| 182 | return 0; |
| 183 | } |
| 184 | #endif /* CONFIG_PPC_PMAC */ |
Stephen Rothwell | af30837 | 2006-03-23 17:38:10 +1100 | [diff] [blame] | 185 | default: |
| 186 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 190 | const struct file_operations nvram_fops = { |
Thomas Gleixner | 3b03fec | 2009-10-14 22:42:28 +0000 | [diff] [blame] | 191 | .owner = THIS_MODULE, |
| 192 | .llseek = dev_nvram_llseek, |
| 193 | .read = dev_nvram_read, |
| 194 | .write = dev_nvram_write, |
| 195 | .unlocked_ioctl = dev_nvram_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | static struct miscdevice nvram_dev = { |
| 199 | NVRAM_MINOR, |
| 200 | "nvram", |
| 201 | &nvram_fops |
| 202 | }; |
| 203 | |
| 204 | |
| 205 | #ifdef DEBUG_NVRAM |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 206 | static void __init nvram_print_partitions(char * label) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
| 208 | struct list_head * p; |
| 209 | struct nvram_partition * tmp_part; |
| 210 | |
| 211 | printk(KERN_WARNING "--------%s---------\n", label); |
| 212 | printk(KERN_WARNING "indx\t\tsig\tchks\tlen\tname\n"); |
| 213 | list_for_each(p, &nvram_part->partition) { |
| 214 | tmp_part = list_entry(p, struct nvram_partition, partition); |
Will Schmidt | 5a43ee6 | 2006-04-26 11:09:46 -0500 | [diff] [blame] | 215 | printk(KERN_WARNING "%4d \t%02x\t%02x\t%d\t%s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | tmp_part->index, tmp_part->header.signature, |
| 217 | tmp_part->header.checksum, tmp_part->header.length, |
| 218 | tmp_part->header.name); |
| 219 | } |
| 220 | } |
| 221 | #endif |
| 222 | |
| 223 | |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 224 | static int __init nvram_write_header(struct nvram_partition * part) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | loff_t tmp_index; |
| 227 | int rc; |
| 228 | |
| 229 | tmp_index = part->index; |
| 230 | rc = ppc_md.nvram_write((char *)&part->header, NVRAM_HEADER_LEN, &tmp_index); |
| 231 | |
| 232 | return rc; |
| 233 | } |
| 234 | |
| 235 | |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 236 | static unsigned char __init nvram_checksum(struct nvram_header *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | unsigned int c_sum, c_sum2; |
| 239 | unsigned short *sp = (unsigned short *)p->name; /* assume 6 shorts */ |
| 240 | c_sum = p->signature + p->length + sp[0] + sp[1] + sp[2] + sp[3] + sp[4] + sp[5]; |
| 241 | |
| 242 | /* The sum may have spilled into the 3rd byte. Fold it back. */ |
| 243 | c_sum = ((c_sum & 0xffff) + (c_sum >> 16)) & 0xffff; |
| 244 | /* The sum cannot exceed 2 bytes. Fold it into a checksum */ |
| 245 | c_sum2 = (c_sum >> 8) + (c_sum << 8); |
| 246 | c_sum = ((c_sum + c_sum2) >> 8) & 0xff; |
| 247 | return c_sum; |
| 248 | } |
| 249 | |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 250 | static int __init nvram_remove_os_partition(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | { |
| 252 | struct list_head *i; |
| 253 | struct list_head *j; |
| 254 | struct nvram_partition * part; |
| 255 | struct nvram_partition * cur_part; |
| 256 | int rc; |
| 257 | |
| 258 | list_for_each(i, &nvram_part->partition) { |
| 259 | part = list_entry(i, struct nvram_partition, partition); |
| 260 | if (part->header.signature != NVRAM_SIG_OS) |
| 261 | continue; |
| 262 | |
| 263 | /* Make os partition a free partition */ |
| 264 | part->header.signature = NVRAM_SIG_FREE; |
| 265 | sprintf(part->header.name, "wwwwwwwwwwww"); |
| 266 | part->header.checksum = nvram_checksum(&part->header); |
| 267 | |
| 268 | /* Merge contiguous free partitions backwards */ |
| 269 | list_for_each_prev(j, &part->partition) { |
| 270 | cur_part = list_entry(j, struct nvram_partition, partition); |
| 271 | if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) { |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | part->header.length += cur_part->header.length; |
| 276 | part->header.checksum = nvram_checksum(&part->header); |
| 277 | part->index = cur_part->index; |
| 278 | |
| 279 | list_del(&cur_part->partition); |
| 280 | kfree(cur_part); |
| 281 | j = &part->partition; /* fixup our loop */ |
| 282 | } |
| 283 | |
| 284 | /* Merge contiguous free partitions forwards */ |
| 285 | list_for_each(j, &part->partition) { |
| 286 | cur_part = list_entry(j, struct nvram_partition, partition); |
| 287 | if (cur_part == nvram_part || cur_part->header.signature != NVRAM_SIG_FREE) { |
| 288 | break; |
| 289 | } |
| 290 | |
| 291 | part->header.length += cur_part->header.length; |
| 292 | part->header.checksum = nvram_checksum(&part->header); |
| 293 | |
| 294 | list_del(&cur_part->partition); |
| 295 | kfree(cur_part); |
| 296 | j = &part->partition; /* fixup our loop */ |
| 297 | } |
| 298 | |
| 299 | rc = nvram_write_header(part); |
| 300 | if (rc <= 0) { |
| 301 | printk(KERN_ERR "nvram_remove_os_partition: nvram_write failed (%d)\n", rc); |
| 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 310 | /** |
| 311 | * nvram_create_partition - Create a partition in nvram |
| 312 | * @name: name of the partition to create |
| 313 | * @sig: signature of the partition to create |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 314 | * @req_size: size of data to allocate in bytes |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 315 | * @min_size: minimum acceptable size (0 means req_size) |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 316 | * |
| 317 | * Returns a negative error code or a positive nvram index |
| 318 | * of the beginning of the data area of the newly created |
| 319 | * partition. If you provided a min_size smaller than req_size |
| 320 | * you need to query for the actual size yourself after the |
| 321 | * call using nvram_partition_get_size(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | */ |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 323 | static loff_t __init nvram_create_partition(const char *name, int sig, |
| 324 | int req_size, int min_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
Arnd Bergmann | a341ad9 | 2005-06-28 20:33:49 +1000 | [diff] [blame] | 326 | struct nvram_partition *part; |
| 327 | struct nvram_partition *new_part; |
akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 328 | struct nvram_partition *free_part = NULL; |
Benjamin Herrenschmidt | cef0d5a | 2010-07-29 17:22:34 +1000 | [diff] [blame] | 329 | static char nv_init_vals[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | loff_t tmp_index; |
| 331 | long size = 0; |
| 332 | int rc; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 333 | |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 334 | /* Convert sizes from bytes to blocks */ |
| 335 | req_size = _ALIGN_UP(req_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN; |
| 336 | min_size = _ALIGN_UP(min_size, NVRAM_BLOCK_LEN) / NVRAM_BLOCK_LEN; |
| 337 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 338 | /* If no minimum size specified, make it the same as the |
| 339 | * requested size |
| 340 | */ |
| 341 | if (min_size == 0) |
| 342 | min_size = req_size; |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 343 | if (min_size > req_size) |
| 344 | return -EINVAL; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 345 | |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 346 | /* Now add one block to each for the header */ |
| 347 | req_size += 1; |
| 348 | min_size += 1; |
| 349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | /* Find a free partition that will give us the maximum needed size |
| 351 | If can't find one that will give us the minimum size needed */ |
Arnd Bergmann | a341ad9 | 2005-06-28 20:33:49 +1000 | [diff] [blame] | 352 | list_for_each_entry(part, &nvram_part->partition, partition) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (part->header.signature != NVRAM_SIG_FREE) |
| 354 | continue; |
| 355 | |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 356 | if (part->header.length >= req_size) { |
| 357 | size = req_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | free_part = part; |
| 359 | break; |
| 360 | } |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 361 | if (part->header.length > size && |
| 362 | part->header.length >= min_size) { |
| 363 | size = part->header.length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | free_part = part; |
| 365 | } |
| 366 | } |
akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 367 | if (!size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | /* Create our OS partition */ |
akpm@osdl.org | 0339ad7 | 2005-05-01 08:58:44 -0700 | [diff] [blame] | 371 | new_part = kmalloc(sizeof(*new_part), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (!new_part) { |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 373 | pr_err("nvram_create_os_partition: kmalloc failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | return -ENOMEM; |
| 375 | } |
| 376 | |
| 377 | new_part->index = free_part->index; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 378 | new_part->header.signature = sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | new_part->header.length = size; |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 380 | strncpy(new_part->header.name, name, 12); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | new_part->header.checksum = nvram_checksum(&new_part->header); |
| 382 | |
| 383 | rc = nvram_write_header(new_part); |
| 384 | if (rc <= 0) { |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 385 | pr_err("nvram_create_os_partition: nvram_write_header " |
| 386 | "failed (%d)\n", rc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | return rc; |
| 388 | } |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 389 | list_add_tail(&new_part->partition, &free_part->partition); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 391 | /* Adjust or remove the partition we stole the space from */ |
| 392 | if (free_part->header.length > size) { |
| 393 | free_part->index += size * NVRAM_BLOCK_LEN; |
| 394 | free_part->header.length -= size; |
| 395 | free_part->header.checksum = nvram_checksum(&free_part->header); |
| 396 | rc = nvram_write_header(free_part); |
| 397 | if (rc <= 0) { |
| 398 | pr_err("nvram_create_os_partition: nvram_write_header " |
| 399 | "failed (%d)\n", rc); |
| 400 | return rc; |
| 401 | } |
| 402 | } else { |
| 403 | list_del(&free_part->partition); |
| 404 | kfree(free_part); |
| 405 | } |
| 406 | |
| 407 | /* Clear the new partition */ |
Benjamin Herrenschmidt | cef0d5a | 2010-07-29 17:22:34 +1000 | [diff] [blame] | 408 | for (tmp_index = new_part->index + NVRAM_HEADER_LEN; |
| 409 | tmp_index < ((size - 1) * NVRAM_BLOCK_LEN); |
| 410 | tmp_index += NVRAM_BLOCK_LEN) { |
| 411 | rc = ppc_md.nvram_write(nv_init_vals, NVRAM_BLOCK_LEN, &tmp_index); |
| 412 | if (rc <= 0) { |
| 413 | pr_err("nvram_create_partition: nvram_write failed (%d)\n", rc); |
| 414 | return rc; |
| 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 418 | return new_part->index + NVRAM_HEADER_LEN; |
| 419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 421 | /** |
| 422 | * nvram_get_partition_size - Get the data size of an nvram partition |
| 423 | * @data_index: This is the offset of the start of the data of |
| 424 | * the partition. The same value that is returned by |
| 425 | * nvram_create_partition(). |
| 426 | */ |
| 427 | static int nvram_get_partition_size(loff_t data_index) |
| 428 | { |
| 429 | struct nvram_partition *part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 431 | list_for_each_entry(part, &nvram_part->partition, partition) { |
| 432 | if (part->index + NVRAM_HEADER_LEN == data_index) |
| 433 | return (part->header.length - 1) * NVRAM_BLOCK_LEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 435 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
| 439 | /* nvram_setup_partition |
| 440 | * |
| 441 | * This will setup the partition we need for buffering the |
| 442 | * error logs and cleanup partitions if needed. |
| 443 | * |
| 444 | * The general strategy is the following: |
| 445 | * 1.) If there is ppc64,linux partition large enough then use it. |
| 446 | * 2.) If there is not a ppc64,linux partition large enough, search |
| 447 | * for a free partition that is large enough. |
| 448 | * 3.) If there is not a free partition large enough remove |
| 449 | * _all_ OS partitions and consolidate the space. |
| 450 | * 4.) Will first try getting a chunk that will satisfy the maximum |
| 451 | * error log size (NVRAM_MAX_REQ). |
| 452 | * 5.) If the max chunk cannot be allocated then try finding a chunk |
| 453 | * that will satisfy the minum needed (NVRAM_MIN_REQ). |
| 454 | */ |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 455 | static int __init nvram_setup_partition(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
| 457 | struct list_head * p; |
| 458 | struct nvram_partition * part; |
| 459 | int rc; |
| 460 | |
| 461 | /* For now, we don't do any of this on pmac, until I |
| 462 | * have figured out if it's worth killing some unused stuffs |
| 463 | * in our nvram, as Apple defined partitions use pretty much |
| 464 | * all of the space |
| 465 | */ |
Benjamin Herrenschmidt | e822250 | 2006-03-28 23:15:54 +1100 | [diff] [blame] | 466 | if (machine_is(powermac)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | return -ENOSPC; |
| 468 | |
| 469 | /* see if we have an OS partition that meets our needs. |
| 470 | will try getting the max we need. If not we'll delete |
| 471 | partitions and try again. */ |
| 472 | list_for_each(p, &nvram_part->partition) { |
| 473 | part = list_entry(p, struct nvram_partition, partition); |
| 474 | if (part->header.signature != NVRAM_SIG_OS) |
| 475 | continue; |
| 476 | |
| 477 | if (strcmp(part->header.name, "ppc64,linux")) |
| 478 | continue; |
| 479 | |
Benjamin Herrenschmidt | 3667330 | 2010-07-29 18:18:44 +1000 | [diff] [blame] | 480 | if ((part->header.length - 1) * NVRAM_BLOCK_LEN >= NVRAM_MIN_REQ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | /* found our partition */ |
| 482 | nvram_error_log_index = part->index + NVRAM_HEADER_LEN; |
| 483 | nvram_error_log_size = ((part->header.length - 1) * |
| 484 | NVRAM_BLOCK_LEN) - sizeof(struct err_log_info); |
| 485 | return 0; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | /* try creating a partition with the free space we have */ |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 490 | rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS, |
Benjamin Herrenschmidt | 4e7c77a | 2010-07-29 15:28:20 +1000 | [diff] [blame] | 491 | NVRAM_MAX_REQ, NVRAM_MIN_REQ); |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 492 | if (rc < 0) { |
| 493 | /* need to free up some space */ |
| 494 | rc = nvram_remove_os_partition(); |
| 495 | if (rc) |
| 496 | return rc; |
| 497 | /* create a partition in this new space */ |
| 498 | rc = nvram_create_partition("ppc64,linux", NVRAM_SIG_OS, |
| 499 | NVRAM_MAX_REQ, NVRAM_MIN_REQ); |
| 500 | if (rc < 0) { |
| 501 | pr_err("nvram_create_partition: Could not find" |
| 502 | " enough space in NVRAM for partition\n"); |
| 503 | return rc; |
| 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Benjamin Herrenschmidt | e49e2e8 | 2010-07-29 17:38:55 +1000 | [diff] [blame^] | 507 | nvram_error_log_index = rc; |
| 508 | nvram_error_log_size = nvram_get_partition_size(rc) - sizeof(struct err_log_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
Thomas Gleixner | 32c105c | 2009-10-14 22:54:46 +0000 | [diff] [blame] | 512 | static int __init nvram_scan_partitions(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | { |
| 514 | loff_t cur_index = 0; |
| 515 | struct nvram_header phead; |
| 516 | struct nvram_partition * tmp_part; |
| 517 | unsigned char c_sum; |
| 518 | char * header; |
| 519 | int total_size; |
| 520 | int err; |
| 521 | |
| 522 | if (ppc_md.nvram_size == NULL) |
| 523 | return -ENODEV; |
| 524 | total_size = ppc_md.nvram_size(); |
| 525 | |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 526 | header = kmalloc(NVRAM_HEADER_LEN, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | if (!header) { |
| 528 | printk(KERN_ERR "nvram_scan_partitions: Failed kmalloc\n"); |
| 529 | return -ENOMEM; |
| 530 | } |
| 531 | |
| 532 | while (cur_index < total_size) { |
| 533 | |
| 534 | err = ppc_md.nvram_read(header, NVRAM_HEADER_LEN, &cur_index); |
| 535 | if (err != NVRAM_HEADER_LEN) { |
| 536 | printk(KERN_ERR "nvram_scan_partitions: Error parsing " |
| 537 | "nvram partitions\n"); |
| 538 | goto out; |
| 539 | } |
| 540 | |
| 541 | cur_index -= NVRAM_HEADER_LEN; /* nvram_read will advance us */ |
| 542 | |
| 543 | memcpy(&phead, header, NVRAM_HEADER_LEN); |
| 544 | |
| 545 | err = 0; |
| 546 | c_sum = nvram_checksum(&phead); |
| 547 | if (c_sum != phead.checksum) { |
| 548 | printk(KERN_WARNING "WARNING: nvram partition checksum" |
| 549 | " was %02x, should be %02x!\n", |
| 550 | phead.checksum, c_sum); |
| 551 | printk(KERN_WARNING "Terminating nvram partition scan\n"); |
| 552 | goto out; |
| 553 | } |
| 554 | if (!phead.length) { |
| 555 | printk(KERN_WARNING "WARNING: nvram corruption " |
| 556 | "detected: 0-length partition\n"); |
| 557 | goto out; |
| 558 | } |
| 559 | tmp_part = (struct nvram_partition *) |
| 560 | kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); |
| 561 | err = -ENOMEM; |
| 562 | if (!tmp_part) { |
| 563 | printk(KERN_ERR "nvram_scan_partitions: kmalloc failed\n"); |
| 564 | goto out; |
| 565 | } |
| 566 | |
| 567 | memcpy(&tmp_part->header, &phead, NVRAM_HEADER_LEN); |
| 568 | tmp_part->index = cur_index; |
| 569 | list_add_tail(&tmp_part->partition, &nvram_part->partition); |
| 570 | |
| 571 | cur_index += phead.length * NVRAM_BLOCK_LEN; |
| 572 | } |
| 573 | err = 0; |
| 574 | |
| 575 | out: |
| 576 | kfree(header); |
| 577 | return err; |
| 578 | } |
| 579 | |
| 580 | static int __init nvram_init(void) |
| 581 | { |
| 582 | int error; |
| 583 | int rc; |
| 584 | |
Benjamin Herrenschmidt | 578914c | 2010-07-29 17:21:17 +1000 | [diff] [blame] | 585 | BUILD_BUG_ON(NVRAM_BLOCK_LEN != 16); |
| 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | if (ppc_md.nvram_size == NULL || ppc_md.nvram_size() <= 0) |
| 588 | return -ENODEV; |
| 589 | |
| 590 | rc = misc_register(&nvram_dev); |
| 591 | if (rc != 0) { |
| 592 | printk(KERN_ERR "nvram_init: failed to register device\n"); |
| 593 | return rc; |
| 594 | } |
| 595 | |
| 596 | /* initialize our anchor for the nvram partition list */ |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 597 | nvram_part = kmalloc(sizeof(struct nvram_partition), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | if (!nvram_part) { |
| 599 | printk(KERN_ERR "nvram_init: Failed kmalloc\n"); |
| 600 | return -ENOMEM; |
| 601 | } |
| 602 | INIT_LIST_HEAD(&nvram_part->partition); |
| 603 | |
| 604 | /* Get all the NVRAM partitions */ |
| 605 | error = nvram_scan_partitions(); |
| 606 | if (error) { |
| 607 | printk(KERN_ERR "nvram_init: Failed nvram_scan_partitions\n"); |
| 608 | return error; |
| 609 | } |
| 610 | |
| 611 | if(nvram_setup_partition()) |
| 612 | printk(KERN_WARNING "nvram_init: Could not find nvram partition" |
| 613 | " for nvram buffered error logging.\n"); |
| 614 | |
| 615 | #ifdef DEBUG_NVRAM |
| 616 | nvram_print_partitions("NVRAM Partitions"); |
| 617 | #endif |
| 618 | |
| 619 | return rc; |
| 620 | } |
| 621 | |
| 622 | void __exit nvram_cleanup(void) |
| 623 | { |
| 624 | misc_deregister( &nvram_dev ); |
| 625 | } |
| 626 | |
| 627 | |
| 628 | #ifdef CONFIG_PPC_PSERIES |
| 629 | |
| 630 | /* nvram_write_error_log |
| 631 | * |
| 632 | * We need to buffer the error logs into nvram to ensure that we have |
| 633 | * the failure information to decode. If we have a severe error there |
| 634 | * is no way to guarantee that the OS or the machine is in a state to |
| 635 | * get back to user land and write the error to disk. For example if |
| 636 | * the SCSI device driver causes a Machine Check by writing to a bad |
| 637 | * IO address, there is no way of guaranteeing that the device driver |
| 638 | * is in any state that is would also be able to write the error data |
| 639 | * captured to disk, thus we buffer it in NVRAM for analysis on the |
| 640 | * next boot. |
| 641 | * |
| 642 | * In NVRAM the partition containing the error log buffer will looks like: |
| 643 | * Header (in bytes): |
| 644 | * +-----------+----------+--------+------------+------------------+ |
| 645 | * | signature | checksum | length | name | data | |
| 646 | * |0 |1 |2 3|4 15|16 length-1| |
| 647 | * +-----------+----------+--------+------------+------------------+ |
| 648 | * |
| 649 | * The 'data' section would look like (in bytes): |
| 650 | * +--------------+------------+-----------------------------------+ |
| 651 | * | event_logged | sequence # | error log | |
| 652 | * |0 3|4 7|8 nvram_error_log_size-1| |
| 653 | * +--------------+------------+-----------------------------------+ |
| 654 | * |
| 655 | * event_logged: 0 if event has not been logged to syslog, 1 if it has |
| 656 | * sequence #: The unique sequence # for each event. (until it wraps) |
| 657 | * error log: The error log from event_scan |
| 658 | */ |
Linas Vepstas | 0f2342c | 2007-08-10 06:56:41 +1000 | [diff] [blame] | 659 | int nvram_write_error_log(char * buff, int length, |
| 660 | unsigned int err_type, unsigned int error_log_cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | { |
| 662 | int rc; |
| 663 | loff_t tmp_index; |
| 664 | struct err_log_info info; |
| 665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | if (nvram_error_log_index == -1) { |
| 667 | return -ESPIPE; |
| 668 | } |
| 669 | |
| 670 | if (length > nvram_error_log_size) { |
| 671 | length = nvram_error_log_size; |
| 672 | } |
| 673 | |
| 674 | info.error_type = err_type; |
| 675 | info.seq_num = error_log_cnt; |
| 676 | |
| 677 | tmp_index = nvram_error_log_index; |
| 678 | |
| 679 | rc = ppc_md.nvram_write((char *)&info, sizeof(struct err_log_info), &tmp_index); |
| 680 | if (rc <= 0) { |
| 681 | printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc); |
| 682 | return rc; |
| 683 | } |
| 684 | |
| 685 | rc = ppc_md.nvram_write(buff, length, &tmp_index); |
| 686 | if (rc <= 0) { |
| 687 | printk(KERN_ERR "nvram_write_error_log: Failed nvram_write (%d)\n", rc); |
| 688 | return rc; |
| 689 | } |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | /* nvram_read_error_log |
| 695 | * |
| 696 | * Reads nvram for error log for at most 'length' |
| 697 | */ |
Linas Vepstas | 0f2342c | 2007-08-10 06:56:41 +1000 | [diff] [blame] | 698 | int nvram_read_error_log(char * buff, int length, |
| 699 | unsigned int * err_type, unsigned int * error_log_cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | { |
| 701 | int rc; |
| 702 | loff_t tmp_index; |
| 703 | struct err_log_info info; |
| 704 | |
| 705 | if (nvram_error_log_index == -1) |
| 706 | return -1; |
| 707 | |
| 708 | if (length > nvram_error_log_size) |
| 709 | length = nvram_error_log_size; |
| 710 | |
| 711 | tmp_index = nvram_error_log_index; |
| 712 | |
| 713 | rc = ppc_md.nvram_read((char *)&info, sizeof(struct err_log_info), &tmp_index); |
| 714 | if (rc <= 0) { |
| 715 | printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc); |
| 716 | return rc; |
| 717 | } |
| 718 | |
| 719 | rc = ppc_md.nvram_read(buff, length, &tmp_index); |
| 720 | if (rc <= 0) { |
| 721 | printk(KERN_ERR "nvram_read_error_log: Failed nvram_read (%d)\n", rc); |
| 722 | return rc; |
| 723 | } |
| 724 | |
Linas Vepstas | 0f2342c | 2007-08-10 06:56:41 +1000 | [diff] [blame] | 725 | *error_log_cnt = info.seq_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | *err_type = info.error_type; |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | /* This doesn't actually zero anything, but it sets the event_logged |
| 732 | * word to tell that this event is safely in syslog. |
| 733 | */ |
| 734 | int nvram_clear_error_log(void) |
| 735 | { |
| 736 | loff_t tmp_index; |
| 737 | int clear_word = ERR_FLAG_ALREADY_LOGGED; |
| 738 | int rc; |
| 739 | |
Thomas Gleixner | fd62c6c | 2009-10-14 22:54:40 +0000 | [diff] [blame] | 740 | if (nvram_error_log_index == -1) |
| 741 | return -1; |
| 742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | tmp_index = nvram_error_log_index; |
| 744 | |
| 745 | rc = ppc_md.nvram_write((char *)&clear_word, sizeof(int), &tmp_index); |
| 746 | if (rc <= 0) { |
| 747 | printk(KERN_ERR "nvram_clear_error_log: Failed nvram_write (%d)\n", rc); |
| 748 | return rc; |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | #endif /* CONFIG_PPC_PSERIES */ |
| 755 | |
| 756 | module_init(nvram_init); |
| 757 | module_exit(nvram_cleanup); |
| 758 | MODULE_LICENSE("GPL"); |