6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/types.h>
11 #include <linux/slab.h>
19 #include <asm/hypervisor.h>
23 #define DRV_MODULE_NAME "n2rng"
24 #define PFX DRV_MODULE_NAME ": "
25 #define DRV_MODULE_VERSION "0.2"
26 #define DRV_MODULE_RELDATE "July 27, 2011"
88 static int n2rng_hv_err_trans(
unsigned long hv_err)
109 static unsigned long n2rng_generic_read_control_v2(
unsigned long ra,
112 unsigned long hv_err,
state,
ticks, watchdog_delta, watchdog_status;
147 static unsigned long n2rng_control_settle_v2(
struct n2rng *np,
int unit)
151 return n2rng_generic_read_control_v2(ra, unit);
154 static unsigned long n2rng_write_ctl_one(
struct n2rng *np,
int unit,
156 unsigned long control_ra,
157 unsigned long watchdog_timeout,
158 unsigned long *ticks)
160 unsigned long hv_err;
164 watchdog_timeout, ticks);
167 watchdog_timeout, unit);
169 hv_err = n2rng_control_settle_v2(np, unit);
176 static int n2rng_generic_read_data(
unsigned long data_ra)
178 unsigned long ticks, hv_err;
179 int block = 0, hcheck = 0;
192 }
else if (hv_err ==
HV_EIO) {
201 static unsigned long n2rng_read_diag_data_one(
struct n2rng *np,
203 unsigned long data_ra,
205 unsigned long *ticks)
207 unsigned long hv_err;
220 static int n2rng_generic_read_diag_data(
struct n2rng *np,
222 unsigned long data_ra,
223 unsigned long data_len)
225 unsigned long ticks, hv_err;
229 hv_err = n2rng_read_diag_data_one(np, unit,
241 }
else if (hv_err ==
HV_EIO) {
249 static int n2rng_generic_write_control(
struct n2rng *np,
250 unsigned long control_ra,
254 unsigned long hv_err,
ticks;
255 int block = 0,
busy = 0;
258 hv_err = n2rng_write_ctl_one(np, unit, state, control_ra,
279 static int n2rng_try_read_ctl(
struct n2rng *np)
281 unsigned long hv_err;
303 return n2rng_hv_err_trans(hv_err);
306 #define CONTROL_DEFAULT_BASE \
307 ((2 << RNG_CTL_ASEL_SHIFT) | \
308 (N2RNG_ACCUM_CYCLES_DEFAULT << RNG_CTL_WAIT_SHIFT) | \
311 #define CONTROL_DEFAULT_0 \
312 (CONTROL_DEFAULT_BASE | \
313 (1 << RNG_CTL_VCO_SHIFT) | \
315 #define CONTROL_DEFAULT_1 \
316 (CONTROL_DEFAULT_BASE | \
317 (2 << RNG_CTL_VCO_SHIFT) | \
319 #define CONTROL_DEFAULT_2 \
320 (CONTROL_DEFAULT_BASE | \
321 (3 << RNG_CTL_VCO_SHIFT) | \
323 #define CONTROL_DEFAULT_3 \
324 (CONTROL_DEFAULT_BASE | \
325 RNG_CTL_ES1 | RNG_CTL_ES2 | RNG_CTL_ES3)
327 static void n2rng_control_swstate_init(
struct n2rng *np)
349 static int n2rng_grab_diag_control(
struct n2rng *np)
354 for (i = 0; i < 100; i++) {
355 err = n2rng_try_read_ctl(np);
359 if (++busy_count > 100) {
361 "Grab diag control timeout.\n");
371 static int n2rng_init_control(
struct n2rng *np)
373 int err = n2rng_grab_diag_control(np);
383 n2rng_control_swstate_init(np);
388 static int n2rng_data_read(
struct hwrng *rng,
u32 *
data)
401 int err = n2rng_generic_read_data(ra);
407 dev_err(&np->
op->dev,
"RNG error, restesting\n");
423 static int n2rng_guest_check(
struct n2rng *np)
427 return n2rng_generic_read_data(ra);
430 static int n2rng_entropy_diag_read(
struct n2rng *np,
unsigned long unit,
431 u64 *pre_control,
u64 pre_state,
433 u64 *post_control,
u64 post_state)
435 unsigned long post_ctl_ra =
__pa(post_control);
436 unsigned long pre_ctl_ra =
__pa(pre_control);
437 unsigned long buffer_ra =
__pa(buffer);
440 err = n2rng_generic_write_control(np, pre_ctl_ra, unit, pre_state);
444 err = n2rng_generic_read_diag_data(np, unit,
447 (
void) n2rng_generic_write_control(np, post_ctl_ra, unit,
457 for (i = 0; i <
count; i++) {
458 int highbit_set = ((
s64)val < 0);
468 static int n2rng_test_buffer_find(
struct n2rng *np,
u64 val)
480 static void n2rng_dump_test_buffer(
struct n2rng *np)
485 dev_err(&np->
op->dev,
"Test buffer slot %d [0x%016llx]\n",
489 static int n2rng_check_selftest_buffer(
struct n2rng *np,
unsigned long unit)
496 matches += n2rng_test_buffer_find(np, val);
503 if (limit >= SELFTEST_LOOPS_MAX) {
505 dev_err(&np->
op->dev,
"Selftest failed on unit %lu\n", unit);
506 n2rng_dump_test_buffer(np);
508 dev_info(&np->
op->dev,
"Selftest passed on unit %lu\n", unit);
513 static int n2rng_control_selftest(
struct n2rng *np,
unsigned long unit)
525 err = n2rng_entropy_diag_read(np, unit, np->
test_control,
529 &np->
units[unit].control[0],
534 return n2rng_check_selftest_buffer(np, unit);
537 static int n2rng_control_check(
struct n2rng *np)
542 int err = n2rng_control_selftest(np, i);
552 static int n2rng_control_configure_units(
struct n2rng *np)
557 for (unit = 0; unit < np->
num_units; unit++) {
572 for (esrc = 0; esrc < 3; esrc++)
580 err = n2rng_generic_write_control(np, ctl_ra, unit,
595 err = n2rng_guest_check(np);
598 err = n2rng_control_check(np);
602 err = n2rng_control_configure_units(np);
614 static void __devinit n2rng_driver_version(
void)
616 static int n2rng_version_printed;
618 if (n2rng_version_printed++ == 0)
633 multi_capable = (match->
data !=
NULL);
635 n2rng_driver_version();
663 dev_err(&op->
dev,
"multi-unit-capable RNG requires "
664 "HVAPI major version 2 or later, got %lu\n",
666 goto out_hvapi_unregister;
671 dev_err(&op->
dev,
"VF RNG lacks rng-#units property\n");
672 goto out_hvapi_unregister;
677 dev_info(&op->
dev,
"Registered RNG HVAPI major %lu minor %lu\n",
684 goto out_hvapi_unregister;
686 err = n2rng_init_control(np);
692 "multi-unit-capable" :
"single-unit"),
695 np->
hwrng.name =
"n2rng";
696 np->
hwrng.data_read = n2rng_data_read;
713 out_hvapi_unregister:
746 .name =
"random-number-generator",
747 .compatible =
"SUNW,n2-rng",
750 .name =
"random-number-generator",
751 .compatible =
"SUNW,vf-rng",
755 .name =
"random-number-generator",
756 .compatible =
"SUNW,kt-rng",
767 .of_match_table = n2rng_match,
769 .probe = n2rng_probe,