xref: /openbmc/linux/drivers/char/hw_random/core.c (revision 275876e2)
1 /*
2         Added support for the AMD Geode LX RNG
3 	(c) Copyright 2004-2005 Advanced Micro Devices, Inc.
4 
5 	derived from
6 
7  	Hardware driver for the Intel/AMD/VIA Random Number Generators (RNG)
8 	(c) Copyright 2003 Red Hat Inc <jgarzik@redhat.com>
9 
10  	derived from
11 
12         Hardware driver for the AMD 768 Random Number Generator (RNG)
13         (c) Copyright 2001 Red Hat Inc <alan@redhat.com>
14 
15  	derived from
16 
17 	Hardware driver for Intel i810 Random Number Generator (RNG)
18 	Copyright 2000,2001 Jeff Garzik <jgarzik@pobox.com>
19 	Copyright 2000,2001 Philipp Rumpf <prumpf@mandrakesoft.com>
20 
21 	Added generic RNG API
22 	Copyright 2006 Michael Buesch <m@bues.ch>
23 	Copyright 2005 (c) MontaVista Software, Inc.
24 
25 	Please read Documentation/hw_random.txt for details on use.
26 
27 	----------------------------------------------------------
28 	This software may be used and distributed according to the terms
29         of the GNU General Public License, incorporated herein by reference.
30 
31  */
32 
33 
34 #include <linux/device.h>
35 #include <linux/hw_random.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/fs.h>
39 #include <linux/sched.h>
40 #include <linux/miscdevice.h>
41 #include <linux/kthread.h>
42 #include <linux/delay.h>
43 #include <linux/slab.h>
44 #include <linux/random.h>
45 #include <asm/uaccess.h>
46 
47 
48 #define RNG_MODULE_NAME		"hw_random"
49 #define PFX			RNG_MODULE_NAME ": "
50 #define RNG_MISCDEV_MINOR	183 /* official */
51 
52 
53 static struct hwrng *current_rng;
54 static struct task_struct *hwrng_fill;
55 static LIST_HEAD(rng_list);
56 static DEFINE_MUTEX(rng_mutex);
57 static int data_avail;
58 static u8 *rng_buffer, *rng_fillbuf;
59 static unsigned short current_quality;
60 static unsigned short default_quality; /* = 0; default to "off" */
61 
62 module_param(current_quality, ushort, 0644);
63 MODULE_PARM_DESC(current_quality,
64 		 "current hwrng entropy estimation per mill");
65 module_param(default_quality, ushort, 0644);
66 MODULE_PARM_DESC(default_quality,
67 		 "default entropy content of hwrng per mill");
68 
69 static void start_khwrngd(void);
70 
71 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
72 			       int wait);
73 
74 static size_t rng_buffer_size(void)
75 {
76 	return SMP_CACHE_BYTES < 32 ? 32 : SMP_CACHE_BYTES;
77 }
78 
79 static void add_early_randomness(struct hwrng *rng)
80 {
81 	unsigned char bytes[16];
82 	int bytes_read;
83 
84 	bytes_read = rng_get_data(rng, bytes, sizeof(bytes), 1);
85 	if (bytes_read > 0)
86 		add_device_randomness(bytes, bytes_read);
87 }
88 
89 static inline int hwrng_init(struct hwrng *rng)
90 {
91 	if (rng->init) {
92 		int ret;
93 
94 		ret =  rng->init(rng);
95 		if (ret)
96 			return ret;
97 	}
98 	add_early_randomness(rng);
99 
100 	current_quality = rng->quality ? : default_quality;
101 	current_quality &= 1023;
102 
103 	if (current_quality == 0 && hwrng_fill)
104 		kthread_stop(hwrng_fill);
105 	if (current_quality > 0 && !hwrng_fill)
106 		start_khwrngd();
107 
108 	return 0;
109 }
110 
111 static inline void hwrng_cleanup(struct hwrng *rng)
112 {
113 	if (rng && rng->cleanup)
114 		rng->cleanup(rng);
115 }
116 
117 static int rng_dev_open(struct inode *inode, struct file *filp)
118 {
119 	/* enforce read-only access to this chrdev */
120 	if ((filp->f_mode & FMODE_READ) == 0)
121 		return -EINVAL;
122 	if (filp->f_mode & FMODE_WRITE)
123 		return -EINVAL;
124 	return 0;
125 }
126 
127 static inline int rng_get_data(struct hwrng *rng, u8 *buffer, size_t size,
128 			int wait) {
129 	int present;
130 
131 	if (rng->read)
132 		return rng->read(rng, (void *)buffer, size, wait);
133 
134 	if (rng->data_present)
135 		present = rng->data_present(rng, wait);
136 	else
137 		present = 1;
138 
139 	if (present)
140 		return rng->data_read(rng, (u32 *)buffer);
141 
142 	return 0;
143 }
144 
145 static ssize_t rng_dev_read(struct file *filp, char __user *buf,
146 			    size_t size, loff_t *offp)
147 {
148 	ssize_t ret = 0;
149 	int err = 0;
150 	int bytes_read, len;
151 
152 	while (size) {
153 		if (mutex_lock_interruptible(&rng_mutex)) {
154 			err = -ERESTARTSYS;
155 			goto out;
156 		}
157 
158 		if (!current_rng) {
159 			err = -ENODEV;
160 			goto out_unlock;
161 		}
162 
163 		if (!data_avail) {
164 			bytes_read = rng_get_data(current_rng, rng_buffer,
165 				rng_buffer_size(),
166 				!(filp->f_flags & O_NONBLOCK));
167 			if (bytes_read < 0) {
168 				err = bytes_read;
169 				goto out_unlock;
170 			}
171 			data_avail = bytes_read;
172 		}
173 
174 		if (!data_avail) {
175 			if (filp->f_flags & O_NONBLOCK) {
176 				err = -EAGAIN;
177 				goto out_unlock;
178 			}
179 		} else {
180 			len = data_avail;
181 			if (len > size)
182 				len = size;
183 
184 			data_avail -= len;
185 
186 			if (copy_to_user(buf + ret, rng_buffer + data_avail,
187 								len)) {
188 				err = -EFAULT;
189 				goto out_unlock;
190 			}
191 
192 			size -= len;
193 			ret += len;
194 		}
195 
196 		mutex_unlock(&rng_mutex);
197 
198 		if (need_resched())
199 			schedule_timeout_interruptible(1);
200 
201 		if (signal_pending(current)) {
202 			err = -ERESTARTSYS;
203 			goto out;
204 		}
205 	}
206 out:
207 	return ret ? : err;
208 out_unlock:
209 	mutex_unlock(&rng_mutex);
210 	goto out;
211 }
212 
213 
214 static const struct file_operations rng_chrdev_ops = {
215 	.owner		= THIS_MODULE,
216 	.open		= rng_dev_open,
217 	.read		= rng_dev_read,
218 	.llseek		= noop_llseek,
219 };
220 
221 static struct miscdevice rng_miscdev = {
222 	.minor		= RNG_MISCDEV_MINOR,
223 	.name		= RNG_MODULE_NAME,
224 	.nodename	= "hwrng",
225 	.fops		= &rng_chrdev_ops,
226 };
227 
228 
229 static ssize_t hwrng_attr_current_store(struct device *dev,
230 					struct device_attribute *attr,
231 					const char *buf, size_t len)
232 {
233 	int err;
234 	struct hwrng *rng;
235 
236 	err = mutex_lock_interruptible(&rng_mutex);
237 	if (err)
238 		return -ERESTARTSYS;
239 	err = -ENODEV;
240 	list_for_each_entry(rng, &rng_list, list) {
241 		if (strcmp(rng->name, buf) == 0) {
242 			if (rng == current_rng) {
243 				err = 0;
244 				break;
245 			}
246 			err = hwrng_init(rng);
247 			if (err)
248 				break;
249 			hwrng_cleanup(current_rng);
250 			current_rng = rng;
251 			err = 0;
252 			break;
253 		}
254 	}
255 	mutex_unlock(&rng_mutex);
256 
257 	return err ? : len;
258 }
259 
260 static ssize_t hwrng_attr_current_show(struct device *dev,
261 				       struct device_attribute *attr,
262 				       char *buf)
263 {
264 	int err;
265 	ssize_t ret;
266 	const char *name = "none";
267 
268 	err = mutex_lock_interruptible(&rng_mutex);
269 	if (err)
270 		return -ERESTARTSYS;
271 	if (current_rng)
272 		name = current_rng->name;
273 	ret = snprintf(buf, PAGE_SIZE, "%s\n", name);
274 	mutex_unlock(&rng_mutex);
275 
276 	return ret;
277 }
278 
279 static ssize_t hwrng_attr_available_show(struct device *dev,
280 					 struct device_attribute *attr,
281 					 char *buf)
282 {
283 	int err;
284 	ssize_t ret = 0;
285 	struct hwrng *rng;
286 
287 	err = mutex_lock_interruptible(&rng_mutex);
288 	if (err)
289 		return -ERESTARTSYS;
290 	buf[0] = '\0';
291 	list_for_each_entry(rng, &rng_list, list) {
292 		strncat(buf, rng->name, PAGE_SIZE - ret - 1);
293 		ret += strlen(rng->name);
294 		strncat(buf, " ", PAGE_SIZE - ret - 1);
295 		ret++;
296 	}
297 	strncat(buf, "\n", PAGE_SIZE - ret - 1);
298 	ret++;
299 	mutex_unlock(&rng_mutex);
300 
301 	return ret;
302 }
303 
304 static DEVICE_ATTR(rng_current, S_IRUGO | S_IWUSR,
305 		   hwrng_attr_current_show,
306 		   hwrng_attr_current_store);
307 static DEVICE_ATTR(rng_available, S_IRUGO,
308 		   hwrng_attr_available_show,
309 		   NULL);
310 
311 
312 static void unregister_miscdev(void)
313 {
314 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_available);
315 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
316 	misc_deregister(&rng_miscdev);
317 }
318 
319 static int register_miscdev(void)
320 {
321 	int err;
322 
323 	err = misc_register(&rng_miscdev);
324 	if (err)
325 		goto out;
326 	err = device_create_file(rng_miscdev.this_device,
327 				 &dev_attr_rng_current);
328 	if (err)
329 		goto err_misc_dereg;
330 	err = device_create_file(rng_miscdev.this_device,
331 				 &dev_attr_rng_available);
332 	if (err)
333 		goto err_remove_current;
334 out:
335 	return err;
336 
337 err_remove_current:
338 	device_remove_file(rng_miscdev.this_device, &dev_attr_rng_current);
339 err_misc_dereg:
340 	misc_deregister(&rng_miscdev);
341 	goto out;
342 }
343 
344 static int hwrng_fillfn(void *unused)
345 {
346 	long rc;
347 
348 	while (!kthread_should_stop()) {
349 		if (!current_rng)
350 			break;
351 		rc = rng_get_data(current_rng, rng_fillbuf,
352 				  rng_buffer_size(), 1);
353 		if (rc <= 0) {
354 			pr_warn("hwrng: no data available\n");
355 			msleep_interruptible(10000);
356 			continue;
357 		}
358 		add_hwgenerator_randomness((void *)rng_fillbuf, rc,
359 					   rc * current_quality * 8 >> 10);
360 	}
361 	hwrng_fill = NULL;
362 	return 0;
363 }
364 
365 static void start_khwrngd(void)
366 {
367 	hwrng_fill = kthread_run(hwrng_fillfn, NULL, "hwrng");
368 	if (hwrng_fill == ERR_PTR(-ENOMEM)) {
369 		pr_err("hwrng_fill thread creation failed");
370 		hwrng_fill = NULL;
371 	}
372 }
373 
374 int hwrng_register(struct hwrng *rng)
375 {
376 	int err = -EINVAL;
377 	struct hwrng *old_rng, *tmp;
378 
379 	if (rng->name == NULL ||
380 	    (rng->data_read == NULL && rng->read == NULL))
381 		goto out;
382 
383 	mutex_lock(&rng_mutex);
384 
385 	/* kmalloc makes this safe for virt_to_page() in virtio_rng.c */
386 	err = -ENOMEM;
387 	if (!rng_buffer) {
388 		rng_buffer = kmalloc(rng_buffer_size(), GFP_KERNEL);
389 		if (!rng_buffer)
390 			goto out_unlock;
391 	}
392 	if (!rng_fillbuf) {
393 		rng_fillbuf = kmalloc(rng_buffer_size(), GFP_KERNEL);
394 		if (!rng_fillbuf) {
395 			kfree(rng_buffer);
396 			goto out_unlock;
397 		}
398 	}
399 
400 	/* Must not register two RNGs with the same name. */
401 	err = -EEXIST;
402 	list_for_each_entry(tmp, &rng_list, list) {
403 		if (strcmp(tmp->name, rng->name) == 0)
404 			goto out_unlock;
405 	}
406 
407 	old_rng = current_rng;
408 	if (!old_rng) {
409 		err = hwrng_init(rng);
410 		if (err)
411 			goto out_unlock;
412 		current_rng = rng;
413 	}
414 	err = 0;
415 	if (!old_rng) {
416 		err = register_miscdev();
417 		if (err) {
418 			hwrng_cleanup(rng);
419 			current_rng = NULL;
420 			goto out_unlock;
421 		}
422 	}
423 	INIT_LIST_HEAD(&rng->list);
424 	list_add_tail(&rng->list, &rng_list);
425 
426 	if (old_rng && !rng->init) {
427 		/*
428 		 * Use a new device's input to add some randomness to
429 		 * the system.  If this rng device isn't going to be
430 		 * used right away, its init function hasn't been
431 		 * called yet; so only use the randomness from devices
432 		 * that don't need an init callback.
433 		 */
434 		add_early_randomness(rng);
435 	}
436 
437 out_unlock:
438 	mutex_unlock(&rng_mutex);
439 out:
440 	return err;
441 }
442 EXPORT_SYMBOL_GPL(hwrng_register);
443 
444 void hwrng_unregister(struct hwrng *rng)
445 {
446 	int err;
447 
448 	mutex_lock(&rng_mutex);
449 
450 	list_del(&rng->list);
451 	if (current_rng == rng) {
452 		hwrng_cleanup(rng);
453 		if (list_empty(&rng_list)) {
454 			current_rng = NULL;
455 		} else {
456 			current_rng = list_entry(rng_list.prev, struct hwrng, list);
457 			err = hwrng_init(current_rng);
458 			if (err)
459 				current_rng = NULL;
460 		}
461 	}
462 	if (list_empty(&rng_list)) {
463 		unregister_miscdev();
464 		if (hwrng_fill)
465 			kthread_stop(hwrng_fill);
466 	}
467 
468 	mutex_unlock(&rng_mutex);
469 }
470 EXPORT_SYMBOL_GPL(hwrng_unregister);
471 
472 static void __exit hwrng_exit(void)
473 {
474 	mutex_lock(&rng_mutex);
475 	BUG_ON(current_rng);
476 	kfree(rng_buffer);
477 	kfree(rng_fillbuf);
478 	mutex_unlock(&rng_mutex);
479 }
480 
481 module_exit(hwrng_exit);
482 
483 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver");
484 MODULE_LICENSE("GPL");
485