Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2010 Michael Neuling IBM Corporation
4 *
5 * Driver for the pseries hardware RNG for POWER7+ and above
6 */
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/hw_random.h>
13#include <asm/vio.h>
14
15
16static int pseries_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
17{
18 u64 buffer[PLPAR_HCALL_BUFSIZE];
19 int rc;
20
21 rc = plpar_hcall(H_RANDOM, (unsigned long *)buffer);
22 if (rc != H_SUCCESS) {
23 pr_err_ratelimited("H_RANDOM call failed %d\n", rc);
24 return -EIO;
25 }
26 memcpy(data, buffer, 8);
27
28 /* The hypervisor interface returns 64 bits */
29 return 8;
30}
31
32/*
33 * pseries_rng_get_desired_dma - Return desired DMA allocate for CMO operations
34 *
35 * This is a required function for a driver to operate in a CMO environment
36 * but this device does not make use of DMA allocations, return 0.
37 *
38 * Return value:
39 * Number of bytes of IO data the driver will need to perform well -> 0
40 */
41static unsigned long pseries_rng_get_desired_dma(struct vio_dev *vdev)
42{
43 return 0;
44};
45
46static struct hwrng pseries_rng = {
47 .name = KBUILD_MODNAME,
48 .read = pseries_rng_read,
49};
50
51static int pseries_rng_probe(struct vio_dev *dev,
52 const struct vio_device_id *id)
53{
54 return hwrng_register(&pseries_rng);
55}
56
57static void pseries_rng_remove(struct vio_dev *dev)
58{
59 hwrng_unregister(&pseries_rng);
60}
61
62static const struct vio_device_id pseries_rng_driver_ids[] = {
63 { "ibm,random-v1", "ibm,random"},
64 { "", "" }
65};
66MODULE_DEVICE_TABLE(vio, pseries_rng_driver_ids);
67
68static struct vio_driver pseries_rng_driver = {
69 .name = KBUILD_MODNAME,
70 .probe = pseries_rng_probe,
71 .remove = pseries_rng_remove,
72 .get_desired_dma = pseries_rng_get_desired_dma,
73 .id_table = pseries_rng_driver_ids
74};
75
76static int __init rng_init(void)
77{
78 pr_info("Registering IBM pSeries RNG driver\n");
79 return vio_register_driver(&pseries_rng_driver);
80}
81
82module_init(rng_init);
83
84static void __exit rng_exit(void)
85{
86 vio_unregister_driver(&pseries_rng_driver);
87}
88module_exit(rng_exit);
89
90MODULE_LICENSE("GPL");
91MODULE_AUTHOR("Michael Neuling <mikey@neuling.org>");
92MODULE_DESCRIPTION("H/W RNG driver for IBM pSeries processors");
1/*
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
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/hw_random.h>
25#include <asm/vio.h>
26
27
28static int pseries_rng_data_read(struct hwrng *rng, u32 *data)
29{
30 int rc;
31
32 rc = plpar_hcall(H_RANDOM, (unsigned long *)data);
33 if (rc != H_SUCCESS) {
34 pr_err_ratelimited("H_RANDOM call failed %d\n", rc);
35 return -EIO;
36 }
37
38 /* The hypervisor interface returns 64 bits */
39 return 8;
40}
41
42/**
43 * pseries_rng_get_desired_dma - Return desired DMA allocate for CMO operations
44 *
45 * This is a required function for a driver to operate in a CMO environment
46 * but this device does not make use of DMA allocations, return 0.
47 *
48 * Return value:
49 * Number of bytes of IO data the driver will need to perform well -> 0
50 */
51static unsigned long pseries_rng_get_desired_dma(struct vio_dev *vdev)
52{
53 return 0;
54};
55
56static struct hwrng pseries_rng = {
57 .name = KBUILD_MODNAME,
58 .data_read = pseries_rng_data_read,
59};
60
61static int __init pseries_rng_probe(struct vio_dev *dev,
62 const struct vio_device_id *id)
63{
64 return hwrng_register(&pseries_rng);
65}
66
67static int __exit pseries_rng_remove(struct vio_dev *dev)
68{
69 hwrng_unregister(&pseries_rng);
70 return 0;
71}
72
73static struct vio_device_id pseries_rng_driver_ids[] = {
74 { "ibm,random-v1", "ibm,random"},
75 { "", "" }
76};
77MODULE_DEVICE_TABLE(vio, pseries_rng_driver_ids);
78
79static struct vio_driver pseries_rng_driver = {
80 .name = KBUILD_MODNAME,
81 .probe = pseries_rng_probe,
82 .remove = pseries_rng_remove,
83 .get_desired_dma = pseries_rng_get_desired_dma,
84 .id_table = pseries_rng_driver_ids
85};
86
87static int __init rng_init(void)
88{
89 printk(KERN_INFO "Registering IBM pSeries RNG driver\n");
90 return vio_register_driver(&pseries_rng_driver);
91}
92
93module_init(rng_init);
94
95static void __exit rng_exit(void)
96{
97 vio_unregister_driver(&pseries_rng_driver);
98}
99module_exit(rng_exit);
100
101MODULE_LICENSE("GPL");
102MODULE_AUTHOR("Michael Neuling <mikey@neuling.org>");
103MODULE_DESCRIPTION("H/W RNG driver for IBM pSeries processors");