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