kernel: Remove some more unused kmalloc types.
[dragonfly.git] / sys / dev / crypto / rdrand / rdrand.c
1 /*
2  * Copyright (c) 2012 Alex Hornung <alex@alexhornung.com>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *    the documentation and/or other materials provided with the
14  *    distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/kobj.h>
33 #include <sys/libkern.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/random.h>
37
38 #include <machine/specialreg.h>
39
40 #define RDRAND_ALIGN(p) (void *)(roundup2((uintptr_t)(p), 16))
41
42
43 struct rdrand_softc {
44         struct callout  sc_rng_co;
45         int32_t         sc_rng_ticks;
46 };
47
48
49 static void rdrand_rng_harvest(void *);
50 int rdrand_rng(uint8_t *out, int limit);
51
52
53 static void
54 rdrand_identify(driver_t *drv, device_t parent)
55 {
56
57         /* NB: order 10 is so we get attached after h/w devices */
58         if (device_find_child(parent, "rdrand", -1) == NULL &&
59             BUS_ADD_CHILD(parent, parent, 10, "rdrand", -1) == 0)
60                 panic("rdrand: could not attach");
61 }
62
63
64 static int
65 rdrand_probe(device_t dev)
66 {
67
68         if ((cpu_feature2 & CPUID2_RDRAND) == 0) {
69                 device_printf(dev, "No RdRand support.\n");
70                 return (EINVAL);
71         }
72
73         device_set_desc(dev, "RdRand RNG");
74         return 0;
75 }
76
77
78 static int
79 rdrand_attach(device_t dev)
80 {
81         struct rdrand_softc *sc;
82
83         sc = device_get_softc(dev);
84
85         if (hz > 100)
86                 sc->sc_rng_ticks = hz/100;
87         else
88                 sc->sc_rng_ticks = 1;
89
90         callout_init_mp(&sc->sc_rng_co);
91         callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks,
92             rdrand_rng_harvest, sc);
93
94         return 0;
95 }
96
97
98 static int
99 rdrand_detach(device_t dev)
100 {
101         struct rdrand_softc *sc;
102
103         sc = device_get_softc(dev);
104
105         callout_stop_sync(&sc->sc_rng_co);
106
107         return (0);
108 }
109
110
111 static int random_count = 512; /* in bytes */
112
113 static void
114 rdrand_rng_harvest(void *arg)
115 {
116         struct rdrand_softc *sc = arg;
117         uint8_t randomness[2048];
118         uint8_t *arandomness; /* randomness aligned */
119         int i, cnt;
120
121         arandomness = RDRAND_ALIGN(randomness);
122         cnt = rdrand_rng(arandomness, random_count);
123
124         for (i = 0; i < cnt; i++)
125                 add_true_randomness((int)arandomness[i]);
126
127         callout_reset(&sc->sc_rng_co, sc->sc_rng_ticks,
128             rdrand_rng_harvest, sc);
129 }
130
131
132 static device_method_t rdrand_methods[] = {
133         DEVMETHOD(device_identify, rdrand_identify),
134         DEVMETHOD(device_probe, rdrand_probe),
135         DEVMETHOD(device_attach, rdrand_attach),
136         DEVMETHOD(device_detach, rdrand_detach),
137
138         DEVMETHOD_END
139 };
140
141
142 static driver_t rdrand_driver = {
143         "rdrand",
144         rdrand_methods,
145         sizeof(struct rdrand_softc),
146 };
147
148 static devclass_t rdrand_devclass;
149
150 DRIVER_MODULE(rdrand, nexus, rdrand_driver, rdrand_devclass, NULL, NULL);
151 MODULE_VERSION(rdrand, 1);
152 MODULE_DEPEND(rdrand, crypto, 1, 1, 1);