Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 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 | |
Michael Ellerman | d319fe2 | 2013-09-25 19:24:17 +1000 | [diff] [blame] | 20 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 21 | |
| 22 | #include <linux/kernel.h> |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/hw_random.h> |
| 25 | #include <asm/vio.h> |
| 26 | |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 27 | |
Greg Kurz | 24c65bc | 2014-10-31 07:50:11 +0100 | [diff] [blame] | 28 | static int pseries_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 29 | { |
Greg Kurz | 24c65bc | 2014-10-31 07:50:11 +0100 | [diff] [blame] | 30 | u64 buffer[PLPAR_HCALL_BUFSIZE]; |
| 31 | size_t size = max < 8 ? max : 8; |
Michael Ellerman | d319fe2 | 2013-09-25 19:24:17 +1000 | [diff] [blame] | 32 | int rc; |
| 33 | |
Greg Kurz | 24c65bc | 2014-10-31 07:50:11 +0100 | [diff] [blame] | 34 | rc = plpar_hcall(H_RANDOM, (unsigned long *)buffer); |
Michael Ellerman | d319fe2 | 2013-09-25 19:24:17 +1000 | [diff] [blame] | 35 | if (rc != H_SUCCESS) { |
| 36 | pr_err_ratelimited("H_RANDOM call failed %d\n", rc); |
| 37 | return -EIO; |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 38 | } |
Greg Kurz | 24c65bc | 2014-10-31 07:50:11 +0100 | [diff] [blame] | 39 | memcpy(data, buffer, size); |
Michael Ellerman | d319fe2 | 2013-09-25 19:24:17 +1000 | [diff] [blame] | 40 | |
| 41 | /* The hypervisor interface returns 64 bits */ |
Greg Kurz | 24c65bc | 2014-10-31 07:50:11 +0100 | [diff] [blame] | 42 | return size; |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * pseries_rng_get_desired_dma - Return desired DMA allocate for CMO operations |
| 47 | * |
| 48 | * This is a required function for a driver to operate in a CMO environment |
| 49 | * but this device does not make use of DMA allocations, return 0. |
| 50 | * |
| 51 | * Return value: |
| 52 | * Number of bytes of IO data the driver will need to perform well -> 0 |
| 53 | */ |
| 54 | static unsigned long pseries_rng_get_desired_dma(struct vio_dev *vdev) |
| 55 | { |
| 56 | return 0; |
| 57 | }; |
| 58 | |
| 59 | static struct hwrng pseries_rng = { |
Michael Ellerman | 49cca7e | 2013-09-25 19:24:16 +1000 | [diff] [blame] | 60 | .name = KBUILD_MODNAME, |
Greg Kurz | 24c65bc | 2014-10-31 07:50:11 +0100 | [diff] [blame] | 61 | .read = pseries_rng_read, |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Dmitry Torokhov | 257bedd | 2015-03-09 10:36:38 -0700 | [diff] [blame] | 64 | static int pseries_rng_probe(struct vio_dev *dev, |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 65 | const struct vio_device_id *id) |
| 66 | { |
| 67 | return hwrng_register(&pseries_rng); |
| 68 | } |
| 69 | |
Dmitry Torokhov | 257bedd | 2015-03-09 10:36:38 -0700 | [diff] [blame] | 70 | static int pseries_rng_remove(struct vio_dev *dev) |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 71 | { |
| 72 | hwrng_unregister(&pseries_rng); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static struct vio_device_id pseries_rng_driver_ids[] = { |
| 77 | { "ibm,random-v1", "ibm,random"}, |
| 78 | { "", "" } |
| 79 | }; |
| 80 | MODULE_DEVICE_TABLE(vio, pseries_rng_driver_ids); |
| 81 | |
| 82 | static struct vio_driver pseries_rng_driver = { |
Michael Ellerman | 49cca7e | 2013-09-25 19:24:16 +1000 | [diff] [blame] | 83 | .name = KBUILD_MODNAME, |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 84 | .probe = pseries_rng_probe, |
| 85 | .remove = pseries_rng_remove, |
| 86 | .get_desired_dma = pseries_rng_get_desired_dma, |
| 87 | .id_table = pseries_rng_driver_ids |
| 88 | }; |
| 89 | |
| 90 | static int __init rng_init(void) |
| 91 | { |
Sudip Mukherjee | 7a1ae9c | 2014-09-15 20:31:20 +0530 | [diff] [blame] | 92 | pr_info("Registering IBM pSeries RNG driver\n"); |
Kent Yoder | 649e9ea | 2012-04-12 05:08:53 +0000 | [diff] [blame] | 93 | return vio_register_driver(&pseries_rng_driver); |
| 94 | } |
| 95 | |
| 96 | module_init(rng_init); |
| 97 | |
| 98 | static void __exit rng_exit(void) |
| 99 | { |
| 100 | vio_unregister_driver(&pseries_rng_driver); |
| 101 | } |
| 102 | module_exit(rng_exit); |
| 103 | |
| 104 | MODULE_LICENSE("GPL"); |
| 105 | MODULE_AUTHOR("Michael Neuling <mikey@neuling.org>"); |
| 106 | MODULE_DESCRIPTION("H/W RNG driver for IBM pSeries processors"); |