]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/pstore/ram_core.c
pstore: Headers should include all stuff they use
[mirror_ubuntu-artful-kernel.git] / fs / pstore / ram_core.c
CommitLineData
c672528a
CC
1/*
2 * Copyright (C) 2012 Google, Inc.
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
404a6043
CC
15#include <linux/device.h>
16#include <linux/err.h>
c672528a
CC
17#include <linux/errno.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/io.h>
404a6043
CC
21#include <linux/list.h>
22#include <linux/memblock.h>
9cc05ad9 23#include <linux/rslib.h>
c672528a 24#include <linux/slab.h>
404a6043 25#include <linux/vmalloc.h>
cddb8751 26#include <linux/pstore_ram.h>
24c3d2f3 27#include <asm/page.h>
c672528a 28
c672528a
CC
29struct persistent_ram_buffer {
30 uint32_t sig;
808d0387
CC
31 atomic_t start;
32 atomic_t size;
c672528a
CC
33 uint8_t data[0];
34};
35
36#define PERSISTENT_RAM_SIG (0x43474244) /* DBGC */
37
808d0387
CC
38static inline size_t buffer_size(struct persistent_ram_zone *prz)
39{
40 return atomic_read(&prz->buffer->size);
41}
42
43static inline size_t buffer_start(struct persistent_ram_zone *prz)
44{
45 return atomic_read(&prz->buffer->start);
46}
47
48/* increase and wrap the start pointer, returning the old value */
49static inline size_t buffer_start_add(struct persistent_ram_zone *prz, size_t a)
50{
51 int old;
52 int new;
53
54 do {
55 old = atomic_read(&prz->buffer->start);
56 new = old + a;
57 while (unlikely(new > prz->buffer_size))
58 new -= prz->buffer_size;
59 } while (atomic_cmpxchg(&prz->buffer->start, old, new) != old);
60
61 return old;
62}
63
64/* increase the size counter until it hits the max size */
65static inline void buffer_size_add(struct persistent_ram_zone *prz, size_t a)
66{
67 size_t old;
68 size_t new;
69
70 if (atomic_read(&prz->buffer->size) == prz->buffer_size)
71 return;
72
73 do {
74 old = atomic_read(&prz->buffer->size);
75 new = old + a;
76 if (new > prz->buffer_size)
77 new = prz->buffer_size;
78 } while (atomic_cmpxchg(&prz->buffer->size, old, new) != old);
79}
80
a15d0b36 81static void notrace persistent_ram_encode_rs8(struct persistent_ram_zone *prz,
c672528a
CC
82 uint8_t *data, size_t len, uint8_t *ecc)
83{
84 int i;
9cc05ad9
CC
85 uint16_t par[prz->ecc_size];
86
c672528a
CC
87 /* Initialize the parity buffer */
88 memset(par, 0, sizeof(par));
89 encode_rs8(prz->rs_decoder, data, len, par, 0);
9cc05ad9 90 for (i = 0; i < prz->ecc_size; i++)
c672528a
CC
91 ecc[i] = par[i];
92}
93
94static int persistent_ram_decode_rs8(struct persistent_ram_zone *prz,
95 void *data, size_t len, uint8_t *ecc)
96{
97 int i;
9cc05ad9
CC
98 uint16_t par[prz->ecc_size];
99
100 for (i = 0; i < prz->ecc_size; i++)
c672528a
CC
101 par[i] = ecc[i];
102 return decode_rs8(prz->rs_decoder, data, par, len,
103 NULL, 0, NULL, 0, NULL);
104}
c672528a 105
a15d0b36 106static void notrace persistent_ram_update_ecc(struct persistent_ram_zone *prz,
808d0387 107 unsigned int start, unsigned int count)
c672528a
CC
108{
109 struct persistent_ram_buffer *buffer = prz->buffer;
c672528a
CC
110 uint8_t *buffer_end = buffer->data + prz->buffer_size;
111 uint8_t *block;
112 uint8_t *par;
9cc05ad9
CC
113 int ecc_block_size = prz->ecc_block_size;
114 int ecc_size = prz->ecc_size;
115 int size = prz->ecc_block_size;
116
c1743cbc 117 if (!prz->ecc_size)
9cc05ad9
CC
118 return;
119
808d0387
CC
120 block = buffer->data + (start & ~(ecc_block_size - 1));
121 par = prz->par_buffer + (start / ecc_block_size) * prz->ecc_size;
122
c672528a 123 do {
9cc05ad9 124 if (block + ecc_block_size > buffer_end)
c672528a
CC
125 size = buffer_end - block;
126 persistent_ram_encode_rs8(prz, block, size, par);
9cc05ad9
CC
127 block += ecc_block_size;
128 par += ecc_size;
808d0387 129 } while (block < buffer->data + start + count);
c672528a
CC
130}
131
9cc05ad9 132static void persistent_ram_update_header_ecc(struct persistent_ram_zone *prz)
c672528a 133{
c672528a
CC
134 struct persistent_ram_buffer *buffer = prz->buffer;
135
c1743cbc 136 if (!prz->ecc_size)
9cc05ad9
CC
137 return;
138
c672528a
CC
139 persistent_ram_encode_rs8(prz, (uint8_t *)buffer, sizeof(*buffer),
140 prz->par_header);
c672528a
CC
141}
142
9cc05ad9 143static void persistent_ram_ecc_old(struct persistent_ram_zone *prz)
c672528a
CC
144{
145 struct persistent_ram_buffer *buffer = prz->buffer;
c672528a
CC
146 uint8_t *block;
147 uint8_t *par;
148
c1743cbc 149 if (!prz->ecc_size)
9cc05ad9
CC
150 return;
151
c672528a
CC
152 block = buffer->data;
153 par = prz->par_buffer;
808d0387 154 while (block < buffer->data + buffer_size(prz)) {
c672528a 155 int numerr;
9cc05ad9 156 int size = prz->ecc_block_size;
c672528a
CC
157 if (block + size > buffer->data + prz->buffer_size)
158 size = buffer->data + prz->buffer_size - block;
159 numerr = persistent_ram_decode_rs8(prz, block, size, par);
160 if (numerr > 0) {
9cc05ad9 161 pr_devel("persistent_ram: error in block %p, %d\n",
c672528a 162 block, numerr);
c672528a
CC
163 prz->corrected_bytes += numerr;
164 } else if (numerr < 0) {
9cc05ad9 165 pr_devel("persistent_ram: uncorrectable error in block %p\n",
c672528a 166 block);
c672528a
CC
167 prz->bad_blocks++;
168 }
9cc05ad9
CC
169 block += prz->ecc_block_size;
170 par += prz->ecc_size;
171 }
172}
173
5ca5d4e6
AV
174static int persistent_ram_init_ecc(struct persistent_ram_zone *prz,
175 int ecc_size)
9cc05ad9
CC
176{
177 int numerr;
178 struct persistent_ram_buffer *buffer = prz->buffer;
179 int ecc_blocks;
1e6a9e56 180 size_t ecc_total;
4a53ffae
AV
181 int ecc_symsize = 8;
182 int ecc_poly = 0x11d;
9cc05ad9 183
c1743cbc 184 if (!ecc_size)
9cc05ad9
CC
185 return 0;
186
187 prz->ecc_block_size = 128;
5ca5d4e6 188 prz->ecc_size = ecc_size;
9cc05ad9
CC
189
190 ecc_blocks = DIV_ROUND_UP(prz->buffer_size, prz->ecc_block_size);
1e6a9e56
AV
191 ecc_total = (ecc_blocks + 1) * prz->ecc_size;
192 if (ecc_total >= prz->buffer_size) {
193 pr_err("%s: invalid ecc_size %u (total %zu, buffer size %zu)\n",
194 __func__, prz->ecc_size, ecc_total, prz->buffer_size);
9cc05ad9
CC
195 return -EINVAL;
196 }
197
1e6a9e56 198 prz->buffer_size -= ecc_total;
9cc05ad9
CC
199 prz->par_buffer = buffer->data + prz->buffer_size;
200 prz->par_header = prz->par_buffer + ecc_blocks * prz->ecc_size;
201
202 /*
203 * first consecutive root is 0
204 * primitive element to generate roots = 1
205 */
4a53ffae 206 prz->rs_decoder = init_rs(ecc_symsize, ecc_poly, 0, 1, prz->ecc_size);
9cc05ad9
CC
207 if (prz->rs_decoder == NULL) {
208 pr_info("persistent_ram: init_rs failed\n");
209 return -EINVAL;
c672528a 210 }
9cc05ad9
CC
211
212 prz->corrected_bytes = 0;
213 prz->bad_blocks = 0;
214
215 numerr = persistent_ram_decode_rs8(prz, buffer, sizeof(*buffer),
216 prz->par_header);
217 if (numerr > 0) {
218 pr_info("persistent_ram: error in header, %d\n", numerr);
219 prz->corrected_bytes += numerr;
220 } else if (numerr < 0) {
221 pr_info("persistent_ram: uncorrectable error in header\n");
222 prz->bad_blocks++;
223 }
224
225 return 0;
226}
227
228ssize_t persistent_ram_ecc_string(struct persistent_ram_zone *prz,
229 char *str, size_t len)
230{
231 ssize_t ret;
232
233 if (prz->corrected_bytes || prz->bad_blocks)
234 ret = snprintf(str, len, ""
235 "\n%d Corrected bytes, %d unrecoverable blocks\n",
236 prz->corrected_bytes, prz->bad_blocks);
237 else
238 ret = snprintf(str, len, "\nNo errors detected\n");
239
240 return ret;
241}
242
a15d0b36 243static void notrace persistent_ram_update(struct persistent_ram_zone *prz,
808d0387 244 const void *s, unsigned int start, unsigned int count)
9cc05ad9
CC
245{
246 struct persistent_ram_buffer *buffer = prz->buffer;
808d0387
CC
247 memcpy(buffer->data + start, s, count);
248 persistent_ram_update_ecc(prz, start, count);
9cc05ad9
CC
249}
250
201e4aca 251void persistent_ram_save_old(struct persistent_ram_zone *prz)
9cc05ad9
CC
252{
253 struct persistent_ram_buffer *buffer = prz->buffer;
808d0387
CC
254 size_t size = buffer_size(prz);
255 size_t start = buffer_start(prz);
9cc05ad9 256
201e4aca
AV
257 if (!size)
258 return;
c672528a 259
201e4aca
AV
260 if (!prz->old_log) {
261 persistent_ram_ecc_old(prz);
262 prz->old_log = kmalloc(size, GFP_KERNEL);
263 }
264 if (!prz->old_log) {
c672528a
CC
265 pr_err("persistent_ram: failed to allocate buffer\n");
266 return;
267 }
268
808d0387
CC
269 prz->old_log_size = size;
270 memcpy(prz->old_log, &buffer->data[start], size - start);
271 memcpy(prz->old_log + size - start, &buffer->data[0], start);
c672528a
CC
272}
273
a15d0b36 274int notrace persistent_ram_write(struct persistent_ram_zone *prz,
c672528a
CC
275 const void *s, unsigned int count)
276{
277 int rem;
278 int c = count;
808d0387 279 size_t start;
c672528a 280
808d0387 281 if (unlikely(c > prz->buffer_size)) {
c672528a
CC
282 s += c - prz->buffer_size;
283 c = prz->buffer_size;
284 }
808d0387 285
484dd30e 286 buffer_size_add(prz, c);
808d0387
CC
287
288 start = buffer_start_add(prz, c);
289
290 rem = prz->buffer_size - start;
291 if (unlikely(rem < c)) {
292 persistent_ram_update(prz, s, start, rem);
c672528a
CC
293 s += rem;
294 c -= rem;
808d0387 295 start = 0;
c672528a 296 }
808d0387 297 persistent_ram_update(prz, s, start, c);
c672528a 298
9cc05ad9 299 persistent_ram_update_header_ecc(prz);
c672528a
CC
300
301 return count;
302}
303
c672528a
CC
304size_t persistent_ram_old_size(struct persistent_ram_zone *prz)
305{
306 return prz->old_log_size;
307}
308
309void *persistent_ram_old(struct persistent_ram_zone *prz)
310{
311 return prz->old_log;
312}
313
314void persistent_ram_free_old(struct persistent_ram_zone *prz)
315{
316 kfree(prz->old_log);
317 prz->old_log = NULL;
318 prz->old_log_size = 0;
319}
320
fce39793
AV
321void persistent_ram_zap(struct persistent_ram_zone *prz)
322{
323 atomic_set(&prz->buffer->start, 0);
324 atomic_set(&prz->buffer->size, 0);
325 persistent_ram_update_header_ecc(prz);
326}
327
2b1321e4 328static void *persistent_ram_vmap(phys_addr_t start, size_t size)
c672528a 329{
404a6043
CC
330 struct page **pages;
331 phys_addr_t page_start;
332 unsigned int page_count;
333 pgprot_t prot;
334 unsigned int i;
2b1321e4 335 void *vaddr;
404a6043
CC
336
337 page_start = start - offset_in_page(start);
338 page_count = DIV_ROUND_UP(size + offset_in_page(start), PAGE_SIZE);
339
340 prot = pgprot_noncached(PAGE_KERNEL);
341
342 pages = kmalloc(sizeof(struct page *) * page_count, GFP_KERNEL);
343 if (!pages) {
344 pr_err("%s: Failed to allocate array for %u pages\n", __func__,
345 page_count);
2b1321e4 346 return NULL;
404a6043
CC
347 }
348
349 for (i = 0; i < page_count; i++) {
350 phys_addr_t addr = page_start + i * PAGE_SIZE;
351 pages[i] = pfn_to_page(addr >> PAGE_SHIFT);
352 }
2b1321e4 353 vaddr = vmap(pages, page_count, VM_MAP, prot);
404a6043 354 kfree(pages);
2b1321e4
AV
355
356 return vaddr;
357}
358
24c3d2f3
AV
359static void *persistent_ram_iomap(phys_addr_t start, size_t size)
360{
361 if (!request_mem_region(start, size, "persistent_ram")) {
362 pr_err("request mem region (0x%llx@0x%llx) failed\n",
363 (unsigned long long)size, (unsigned long long)start);
364 return NULL;
365 }
366
367 return ioremap(start, size);
368}
369
2b1321e4
AV
370static int persistent_ram_buffer_map(phys_addr_t start, phys_addr_t size,
371 struct persistent_ram_zone *prz)
372{
d3b48769
AV
373 prz->paddr = start;
374 prz->size = size;
375
24c3d2f3
AV
376 if (pfn_valid(start >> PAGE_SHIFT))
377 prz->vaddr = persistent_ram_vmap(start, size);
378 else
379 prz->vaddr = persistent_ram_iomap(start, size);
380
404a6043 381 if (!prz->vaddr) {
2b1321e4
AV
382 pr_err("%s: Failed to map 0x%llx pages at 0x%llx\n", __func__,
383 (unsigned long long)size, (unsigned long long)start);
404a6043
CC
384 return -ENOMEM;
385 }
386
387 prz->buffer = prz->vaddr + offset_in_page(start);
388 prz->buffer_size = size - sizeof(struct persistent_ram_buffer);
389
390 return 0;
391}
392
924d3711 393static int __devinit persistent_ram_post_init(struct persistent_ram_zone *prz,
5ca5d4e6 394 int ecc_size)
404a6043 395{
bb4206f2 396 int ret;
c672528a 397
5ca5d4e6 398 ret = persistent_ram_init_ecc(prz, ecc_size);
9cc05ad9 399 if (ret)
bb4206f2 400 return ret;
c672528a 401
404a6043 402 if (prz->buffer->sig == PERSISTENT_RAM_SIG) {
808d0387
CC
403 if (buffer_size(prz) > prz->buffer_size ||
404 buffer_start(prz) > buffer_size(prz))
405 pr_info("persistent_ram: found existing invalid buffer,"
f56d711b 406 " size %zu, start %zu\n",
808d0387 407 buffer_size(prz), buffer_start(prz));
c672528a 408 else {
602b5be4 409 pr_debug("persistent_ram: found existing buffer,"
f56d711b 410 " size %zu, start %zu\n",
808d0387 411 buffer_size(prz), buffer_start(prz));
c672528a 412 persistent_ram_save_old(prz);
25b63da6 413 return 0;
c672528a
CC
414 }
415 } else {
602b5be4 416 pr_debug("persistent_ram: no valid data in buffer"
808d0387 417 " (sig = 0x%08x)\n", prz->buffer->sig);
c672528a
CC
418 }
419
404a6043 420 prz->buffer->sig = PERSISTENT_RAM_SIG;
fce39793 421 persistent_ram_zap(prz);
c672528a 422
bb4206f2
AV
423 return 0;
424}
425
d3b48769
AV
426void persistent_ram_free(struct persistent_ram_zone *prz)
427{
beeb9432
AV
428 if (!prz)
429 return;
430
431 if (prz->vaddr) {
432 if (pfn_valid(prz->paddr >> PAGE_SHIFT)) {
433 vunmap(prz->vaddr);
434 } else {
435 iounmap(prz->vaddr);
436 release_mem_region(prz->paddr, prz->size);
437 }
438 prz->vaddr = NULL;
d3b48769
AV
439 }
440 persistent_ram_free_old(prz);
441 kfree(prz);
442}
443
924d3711
AV
444struct persistent_ram_zone * __devinit persistent_ram_new(phys_addr_t start,
445 size_t size,
5ca5d4e6 446 int ecc_size)
8cf5aff8
AV
447{
448 struct persistent_ram_zone *prz;
449 int ret = -ENOMEM;
450
451 prz = kzalloc(sizeof(struct persistent_ram_zone), GFP_KERNEL);
452 if (!prz) {
453 pr_err("persistent_ram: failed to allocate persistent ram zone\n");
454 goto err;
455 }
456
457 ret = persistent_ram_buffer_map(start, size, prz);
458 if (ret)
459 goto err;
460
5ca5d4e6 461 ret = persistent_ram_post_init(prz, ecc_size);
beeb9432
AV
462 if (ret)
463 goto err;
8cf5aff8
AV
464
465 return prz;
466err:
beeb9432 467 persistent_ram_free(prz);
8cf5aff8
AV
468 return ERR_PTR(ret);
469}