blob: b761459a3436c25d31321f5bb46037f7faaf9292 [file] [log] [blame]
Kent Yoder649e9ea2012-04-12 05:08:53 +00001/*
2 * Copyright (C) 2010 Michael Neuling IBM Corporation
3 *
4 * Driver for the pseries hardware RNG for POWER7+ and above
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Michael Ellermanf95dabe2013-09-25 19:24:17 +100020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/kernel.h>
Kent Yoder649e9ea2012-04-12 05:08:53 +000023#include <linux/module.h>
24#include <linux/hw_random.h>
25#include <asm/vio.h>
26
27#define MODULE_NAME "pseries-rng"
28
29static int pseries_rng_data_read(struct hwrng *rng, u32 *data)
30{
Michael Ellermanf95dabe2013-09-25 19:24:17 +100031 int rc;
32
33 rc = plpar_hcall(H_RANDOM, (unsigned long *)data);
34 if (rc != H_SUCCESS) {
35 pr_err_ratelimited("H_RANDOM call failed %d\n", rc);
36 return -EIO;
Kent Yoder649e9ea2012-04-12 05:08:53 +000037 }
Michael Ellermanf95dabe2013-09-25 19:24:17 +100038
39 /* The hypervisor interface returns 64 bits */
Kent Yoder649e9ea2012-04-12 05:08:53 +000040 return 8;
41}
42
43/**
44 * pseries_rng_get_desired_dma - Return desired DMA allocate for CMO operations
45 *
46 * This is a required function for a driver to operate in a CMO environment
47 * but this device does not make use of DMA allocations, return 0.
48 *
49 * Return value:
50 * Number of bytes of IO data the driver will need to perform well -> 0
51 */
52static unsigned long pseries_rng_get_desired_dma(struct vio_dev *vdev)
53{
54 return 0;
55};
56
57static struct hwrng pseries_rng = {
58 .name = MODULE_NAME,
59 .data_read = pseries_rng_data_read,
60};
61
62static int __init pseries_rng_probe(struct vio_dev *dev,
63 const struct vio_device_id *id)
64{
65 return hwrng_register(&pseries_rng);
66}
67
68static int __exit pseries_rng_remove(struct vio_dev *dev)
69{
70 hwrng_unregister(&pseries_rng);
71 return 0;
72}
73
74static struct vio_device_id pseries_rng_driver_ids[] = {
75 { "ibm,random-v1", "ibm,random"},
76 { "", "" }
77};
78MODULE_DEVICE_TABLE(vio, pseries_rng_driver_ids);
79
80static struct vio_driver pseries_rng_driver = {
81 .name = MODULE_NAME,
82 .probe = pseries_rng_probe,
83 .remove = pseries_rng_remove,
84 .get_desired_dma = pseries_rng_get_desired_dma,
85 .id_table = pseries_rng_driver_ids
86};
87
88static int __init rng_init(void)
89{
90 printk(KERN_INFO "Registering IBM pSeries RNG driver\n");
91 return vio_register_driver(&pseries_rng_driver);
92}
93
94module_init(rng_init);
95
96static void __exit rng_exit(void)
97{
98 vio_unregister_driver(&pseries_rng_driver);
99}
100module_exit(rng_exit);
101
102MODULE_LICENSE("GPL");
103MODULE_AUTHOR("Michael Neuling <mikey@neuling.org>");
104MODULE_DESCRIPTION("H/W RNG driver for IBM pSeries processors");