pstore/ram: Factor dmesg przs initialization out of probe()
[linux-2.6-block.git] / fs / pstore / ram.c
CommitLineData
56d611a0
MS
1/*
2 * RAM Oops/Panic logger
3 *
4 * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
9ba80d99 5 * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
56d611a0
MS
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 *
21 */
22
0169256e
MS
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
56d611a0 25#include <linux/kernel.h>
83c1b317 26#include <linux/err.h>
56d611a0 27#include <linux/module.h>
9ba80d99 28#include <linux/pstore.h>
56d611a0
MS
29#include <linux/time.h>
30#include <linux/io.h>
31#include <linux/ioport.h>
c3b92ce9 32#include <linux/platform_device.h>
13aefd72 33#include <linux/slab.h>
1894a253 34#include <linux/pstore_ram.h>
56d611a0
MS
35
36#define RAMOOPS_KERNMSG_HDR "===="
3e5c4fad 37#define MIN_MEM_SIZE 4096UL
56d611a0 38
3e5c4fad
SI
39static ulong record_size = MIN_MEM_SIZE;
40module_param(record_size, ulong, 0400);
41MODULE_PARM_DESC(record_size,
42 "size of each dump done on oops/panic");
56d611a0
MS
43
44static ulong mem_address;
45module_param(mem_address, ulong, 0400);
46MODULE_PARM_DESC(mem_address,
47 "start of reserved RAM used to store oops/panic logs");
48
49static ulong mem_size;
50module_param(mem_size, ulong, 0400);
51MODULE_PARM_DESC(mem_size,
52 "size of reserved RAM used to store oops/panic logs");
53
54static int dump_oops = 1;
55module_param(dump_oops, int, 0600);
56MODULE_PARM_DESC(dump_oops,
57 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
58
39eb7e97
AV
59static int ramoops_ecc;
60module_param_named(ecc, ramoops_ecc, int, 0600);
61MODULE_PARM_DESC(ramoops_ecc,
62 "set to 1 to enable ECC support");
63
9ba80d99 64struct ramoops_context {
896fc1f0 65 struct persistent_ram_zone **przs;
56d611a0
MS
66 phys_addr_t phys_addr;
67 unsigned long size;
9ba80d99 68 size_t record_size;
6b4d2a27 69 int dump_oops;
39eb7e97 70 bool ecc;
cac2eb7b
AV
71 unsigned int max_dump_cnt;
72 unsigned int dump_write_cnt;
73 unsigned int dump_read_cnt;
9ba80d99
KC
74 struct pstore_info pstore;
75};
56d611a0 76
13aefd72
MS
77static struct platform_device *dummy;
78static struct ramoops_platform_data *dummy_data;
79
9ba80d99
KC
80static int ramoops_pstore_open(struct pstore_info *psi)
81{
82 struct ramoops_context *cxt = psi->data;
83
cac2eb7b 84 cxt->dump_read_cnt = 0;
9ba80d99
KC
85 return 0;
86}
87
88static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
89 struct timespec *time,
90 char **buf,
91 struct pstore_info *psi)
56d611a0 92{
9ba80d99 93 ssize_t size;
9ba80d99 94 struct ramoops_context *cxt = psi->data;
896fc1f0 95 struct persistent_ram_zone *prz;
9ba80d99 96
cac2eb7b 97 if (cxt->dump_read_cnt >= cxt->max_dump_cnt)
9ba80d99 98 return -EINVAL;
896fc1f0 99
cac2eb7b 100 *id = cxt->dump_read_cnt++;
896fc1f0
AV
101 prz = cxt->przs[*id];
102
9ba80d99
KC
103 /* Only supports dmesg output so far. */
104 *type = PSTORE_TYPE_DMESG;
105 /* TODO(kees): Bogus time for the moment. */
106 time->tv_sec = 0;
107 time->tv_nsec = 0;
108
201e4aca
AV
109 /* Update old/shadowed buffer. */
110 persistent_ram_save_old(prz);
896fc1f0 111 size = persistent_ram_old_size(prz);
9ba80d99
KC
112 *buf = kmalloc(size, GFP_KERNEL);
113 if (*buf == NULL)
114 return -ENOMEM;
896fc1f0 115 memcpy(*buf, persistent_ram_old(prz), size);
9ba80d99
KC
116
117 return size;
118}
119
896fc1f0
AV
120static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
121{
122 char *hdr;
123 struct timeval timestamp;
124 size_t len;
125
126 do_gettimeofday(&timestamp);
127 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
128 (long)timestamp.tv_sec, (long)timestamp.tv_usec);
129 WARN_ON_ONCE(!hdr);
130 len = hdr ? strlen(hdr) : 0;
131 persistent_ram_write(prz, hdr, len);
132 kfree(hdr);
133
134 return len;
135}
136
9ba80d99
KC
137static int ramoops_pstore_write(enum pstore_type_id type,
138 enum kmsg_dump_reason reason,
139 u64 *id,
140 unsigned int part,
141 size_t size, struct pstore_info *psi)
142{
9ba80d99 143 struct ramoops_context *cxt = psi->data;
cac2eb7b 144 struct persistent_ram_zone *prz = cxt->przs[cxt->dump_write_cnt];
896fc1f0 145 size_t hlen;
9ba80d99
KC
146
147 /* Currently ramoops is designed to only store dmesg dumps. */
148 if (type != PSTORE_TYPE_DMESG)
149 return -EINVAL;
56d611a0 150
9ba80d99
KC
151 /* Out of the various dmesg dump types, ramoops is currently designed
152 * to only store crash logs, rather than storing general kernel logs.
153 */
fc2d557c 154 if (reason != KMSG_DUMP_OOPS &&
a3dd3323 155 reason != KMSG_DUMP_PANIC)
9ba80d99 156 return -EINVAL;
fc2d557c 157
9ba80d99 158 /* Skip Oopes when configured to do so. */
6b4d2a27 159 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
9ba80d99
KC
160 return -EINVAL;
161
162 /* Explicitly only take the first part of any new crash.
163 * If our buffer is larger than kmsg_bytes, this can never happen,
164 * and if our buffer is smaller than kmsg_bytes, we don't want the
165 * report split across multiple records.
166 */
167 if (part != 1)
168 return -ENOSPC;
56d611a0 169
896fc1f0
AV
170 hlen = ramoops_write_kmsg_hdr(prz);
171 if (size + hlen > prz->buffer_size)
172 size = prz->buffer_size - hlen;
173 persistent_ram_write(prz, cxt->pstore.buf, size);
56d611a0 174
cac2eb7b 175 cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
9ba80d99
KC
176
177 return 0;
178}
179
180static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
181 struct pstore_info *psi)
182{
9ba80d99
KC
183 struct ramoops_context *cxt = psi->data;
184
cac2eb7b 185 if (id >= cxt->max_dump_cnt)
9ba80d99
KC
186 return -EINVAL;
187
896fc1f0 188 persistent_ram_free_old(cxt->przs[id]);
93cce049 189 persistent_ram_zap(cxt->przs[id]);
9ba80d99
KC
190
191 return 0;
56d611a0
MS
192}
193
9ba80d99
KC
194static struct ramoops_context oops_cxt = {
195 .pstore = {
196 .owner = THIS_MODULE,
197 .name = "ramoops",
198 .open = ramoops_pstore_open,
199 .read = ramoops_pstore_read,
200 .write = ramoops_pstore_write,
201 .erase = ramoops_pstore_erase,
202 },
203};
204
f4c5d242
AV
205static void ramoops_free_przs(struct ramoops_context *cxt)
206{
207 int i;
208
209 if (!cxt->przs)
210 return;
211
212 for (i = 0; cxt->przs[i]; i++)
213 persistent_ram_free(cxt->przs[i]);
214 kfree(cxt->przs);
215}
216
217static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
218 phys_addr_t *paddr, size_t dump_mem_sz)
219{
220 int err = -ENOMEM;
221 int i;
222
223 if (!cxt->record_size)
224 return 0;
225
226 cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
227 if (!cxt->max_dump_cnt)
228 return -ENOMEM;
229
230 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
231 GFP_KERNEL);
232 if (!cxt->przs) {
233 dev_err(dev, "failed to initialize a prz array for dumps\n");
234 return -ENOMEM;
235 }
236
237 for (i = 0; i < cxt->max_dump_cnt; i++) {
238 size_t sz = cxt->record_size;
239
240 cxt->przs[i] = persistent_ram_new(*paddr, sz, cxt->ecc);
241 if (IS_ERR(cxt->przs[i])) {
242 err = PTR_ERR(cxt->przs[i]);
243 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
244 sz, (unsigned long long)*paddr, err);
245 goto fail_prz;
246 }
247 *paddr += sz;
248 }
249
250 return 0;
251fail_prz:
252 ramoops_free_przs(cxt);
253 return err;
254}
255
c3b92ce9 256static int __init ramoops_probe(struct platform_device *pdev)
56d611a0 257{
896fc1f0 258 struct device *dev = &pdev->dev;
c3b92ce9 259 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
56d611a0 260 struct ramoops_context *cxt = &oops_cxt;
f4c5d242
AV
261 size_t dump_mem_sz;
262 phys_addr_t paddr;
56d611a0
MS
263 int err = -EINVAL;
264
9ba80d99
KC
265 /* Only a single ramoops area allowed at a time, so fail extra
266 * probes.
267 */
cac2eb7b 268 if (cxt->max_dump_cnt)
9ba80d99
KC
269 goto fail_out;
270
3e5c4fad
SI
271 if (!pdata->mem_size || !pdata->record_size) {
272 pr_err("The memory size and the record size must be "
273 "non-zero\n");
9ba80d99 274 goto fail_out;
56d611a0
MS
275 }
276
fdb59507
MS
277 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
278 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
56d611a0 279
cac2eb7b 280 cxt->dump_read_cnt = 0;
13aefd72
MS
281 cxt->size = pdata->mem_size;
282 cxt->phys_addr = pdata->mem_address;
3e5c4fad 283 cxt->record_size = pdata->record_size;
6b4d2a27 284 cxt->dump_oops = pdata->dump_oops;
39eb7e97 285 cxt->ecc = pdata->ecc;
56d611a0 286
f4c5d242 287 paddr = cxt->phys_addr;
896fc1f0 288
f4c5d242
AV
289 dump_mem_sz = cxt->size;
290 err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
291 if (err) {
292 pr_err("memory size too small, minimum is %lu\n",
293 cxt->record_size);
294 goto fail_count;
896fc1f0
AV
295 }
296
9ba80d99 297 cxt->pstore.data = cxt;
896fc1f0 298 cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
9ba80d99
KC
299 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
300 spin_lock_init(&cxt->pstore.buf_lock);
301 if (!cxt->pstore.buf) {
302 pr_err("cannot allocate pstore buffer\n");
303 goto fail_clear;
304 }
305
9ba80d99 306 err = pstore_register(&cxt->pstore);
56d611a0 307 if (err) {
9ba80d99 308 pr_err("registering with pstore failed\n");
896fc1f0 309 goto fail_buf;
56d611a0
MS
310 }
311
c755201e
KC
312 /*
313 * Update the module parameter variables as well so they are visible
314 * through /sys/module/ramoops/parameters/
315 */
316 mem_size = pdata->mem_size;
317 mem_address = pdata->mem_address;
318 record_size = pdata->record_size;
319 dump_oops = pdata->dump_oops;
320
39eb7e97 321 pr_info("attached 0x%lx@0x%llx (%ux0x%zx), ecc: %s\n",
d109a674 322 cxt->size, (unsigned long long)cxt->phys_addr,
cac2eb7b 323 cxt->max_dump_cnt, cxt->record_size,
39eb7e97 324 ramoops_ecc ? "on" : "off");
9ba80d99 325
56d611a0
MS
326 return 0;
327
9ba80d99
KC
328fail_buf:
329 kfree(cxt->pstore.buf);
330fail_clear:
331 cxt->pstore.bufsize = 0;
cac2eb7b 332 cxt->max_dump_cnt = 0;
f4c5d242
AV
333fail_count:
334 ramoops_free_przs(cxt);
9ba80d99 335fail_out:
56d611a0
MS
336 return err;
337}
338
c3b92ce9 339static int __exit ramoops_remove(struct platform_device *pdev)
56d611a0 340{
9ba80d99
KC
341#if 0
342 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
343 * unregistering yet.
344 */
56d611a0
MS
345 struct ramoops_context *cxt = &oops_cxt;
346
56d611a0
MS
347 iounmap(cxt->virt_addr);
348 release_mem_region(cxt->phys_addr, cxt->size);
cac2eb7b 349 cxt->max_dump_cnt = 0;
9ba80d99
KC
350
351 /* TODO(kees): When pstore supports unregistering, call it here. */
352 kfree(cxt->pstore.buf);
353 cxt->pstore.bufsize = 0;
354
c3b92ce9 355 return 0;
9ba80d99
KC
356#endif
357 return -EBUSY;
56d611a0
MS
358}
359
c3b92ce9
KP
360static struct platform_driver ramoops_driver = {
361 .remove = __exit_p(ramoops_remove),
362 .driver = {
363 .name = "ramoops",
364 .owner = THIS_MODULE,
365 },
366};
367
368static int __init ramoops_init(void)
369{
13aefd72
MS
370 int ret;
371 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
372 if (ret == -ENODEV) {
373 /*
374 * If we didn't find a platform device, we use module parameters
375 * building platform data on the fly.
376 */
0169256e 377 pr_info("platform device not found, using module parameters\n");
13aefd72
MS
378 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
379 GFP_KERNEL);
380 if (!dummy_data)
381 return -ENOMEM;
382 dummy_data->mem_size = mem_size;
383 dummy_data->mem_address = mem_address;
3e5c4fad 384 dummy_data->record_size = record_size;
6b4d2a27 385 dummy_data->dump_oops = dump_oops;
39eb7e97 386 dummy_data->ecc = ramoops_ecc;
13aefd72
MS
387 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
388 NULL, 0, dummy_data,
389 sizeof(struct ramoops_platform_data));
390
391 if (IS_ERR(dummy))
392 ret = PTR_ERR(dummy);
393 else
394 ret = 0;
395 }
396
397 return ret;
c3b92ce9
KP
398}
399
400static void __exit ramoops_exit(void)
401{
402 platform_driver_unregister(&ramoops_driver);
13aefd72 403 kfree(dummy_data);
c3b92ce9 404}
56d611a0
MS
405
406module_init(ramoops_init);
407module_exit(ramoops_exit);
408
409MODULE_LICENSE("GPL");
410MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
411MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");