Lines Matching +full:break +full:- +full:control

1 // SPDX-License-Identifier: GPL-2.0-only
2 /* n2-drv.c: Niagara-2 RNG driver.
36 /* The Niagara2 RNG provides a 64-bit read-only random number
37 * register, plus a control register. Access to the RNG is
38 * virtualized through the hypervisor so that both guests and control
48 * which is the 64-bit read-only register.
51 * such that a full 64-bits of entropy are available in the register.
56 * A control register is provided for adjusting various aspects of RNG
74 * separately. This is why 4 control register values must be provided
80 * control 0: (1 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES1
81 * control 1: (2 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES2
82 * control 2: (3 << RNG_CTL_VCO_SHIFT) | RNG_CTL_ES3
94 return -EAGAIN; in n2rng_hv_err_trans()
96 return -EPERM; in n2rng_hv_err_trans()
98 return -EIO; in n2rng_hv_err_trans()
100 return -EBUSY; in n2rng_hv_err_trans()
103 return -EFAULT; in n2rng_hv_err_trans()
105 return -EINVAL; in n2rng_hv_err_trans()
121 break; in n2rng_generic_read_control_v2()
125 break; in n2rng_generic_read_control_v2()
130 break; in n2rng_generic_read_control_v2()
134 break; in n2rng_generic_read_control_v2()
140 /* In multi-socket situations, the hypervisor might need to
141 * queue up the RNG control register write if it's for a unit
144 * We poll here waiting for a successful read of that control
149 unsigned long ra = __pa(&np->scratch_control[0]); in n2rng_control_settle_v2()
162 if (np->hvapi_major == 1) { in n2rng_write_ctl_one()
188 return -EWOULDBLOCK; in n2rng_generic_read_data()
191 return -EPERM; in n2rng_generic_read_data()
194 return -EIO; in n2rng_generic_read_data()
197 return -ENODEV; in n2rng_generic_read_data()
209 if (np->hvapi_major == 1) { in n2rng_read_diag_data_one()
237 return -EWOULDBLOCK; in n2rng_generic_read_diag_data()
240 return -EPERM; in n2rng_generic_read_diag_data()
242 return -EIO; in n2rng_generic_read_diag_data()
244 return -ENODEV; in n2rng_generic_read_diag_data()
259 np->wd_timeo, &ticks); in n2rng_generic_write_control()
265 return -EWOULDBLOCK; in n2rng_generic_write_control()
269 return -EBUSY; in n2rng_generic_write_control()
272 return -ENODEV; in n2rng_generic_write_control()
276 /* Just try to see if we can successfully access the control register
284 if (np->hvapi_major == 1) { in n2rng_try_read_ctl()
296 break; in n2rng_try_read_ctl()
299 break; in n2rng_try_read_ctl()
310 if (np->data->chip_version == 1) { in n2rng_control_default()
318 break; in n2rng_control_default()
321 break; in n2rng_control_default()
324 break; in n2rng_control_default()
327 break; in n2rng_control_default()
329 break; in n2rng_control_default()
340 break; in n2rng_control_default()
343 break; in n2rng_control_default()
346 break; in n2rng_control_default()
349 break; in n2rng_control_default()
351 break; in n2rng_control_default()
362 np->flags |= N2RNG_FLAG_CONTROL; in n2rng_control_swstate_init()
364 np->health_check_sec = N2RNG_HEALTH_CHECK_SEC_DEFAULT; in n2rng_control_swstate_init()
365 np->accum_cycles = N2RNG_ACCUM_CYCLES_DEFAULT; in n2rng_control_swstate_init()
366 np->wd_timeo = N2RNG_WD_TIMEO_DEFAULT; in n2rng_control_swstate_init()
368 for (i = 0; i < np->num_units; i++) { in n2rng_control_swstate_init()
369 struct n2rng_unit *up = &np->units[i]; in n2rng_control_swstate_init()
371 up->control[0] = n2rng_control_default(np, 0); in n2rng_control_swstate_init()
372 up->control[1] = n2rng_control_default(np, 1); in n2rng_control_swstate_init()
373 up->control[2] = n2rng_control_default(np, 2); in n2rng_control_swstate_init()
374 up->control[3] = n2rng_control_default(np, 3); in n2rng_control_swstate_init()
377 np->hv_state = HV_RNG_STATE_UNCONFIGURED; in n2rng_control_swstate_init()
382 int i, busy_count, err = -ENODEV; in n2rng_grab_diag_control()
387 if (err != -EAGAIN) in n2rng_grab_diag_control()
388 break; in n2rng_grab_diag_control()
391 dev_err(&np->op->dev, in n2rng_grab_diag_control()
392 "Grab diag control timeout.\n"); in n2rng_grab_diag_control()
393 return -ENODEV; in n2rng_grab_diag_control()
406 /* Not in the control domain, that's OK we are only a consumer in n2rng_init_control()
409 if (err == -EPERM) in n2rng_init_control()
421 struct n2rng *np = (struct n2rng *) rng->priv; in n2rng_data_read()
422 unsigned long ra = __pa(&np->test_data); in n2rng_data_read()
425 if (!(np->flags & N2RNG_FLAG_READY)) { in n2rng_data_read()
427 } else if (np->flags & N2RNG_FLAG_BUFFER_VALID) { in n2rng_data_read()
428 np->flags &= ~N2RNG_FLAG_BUFFER_VALID; in n2rng_data_read()
429 *data = np->buffer; in n2rng_data_read()
434 np->flags |= N2RNG_FLAG_BUFFER_VALID; in n2rng_data_read()
435 np->buffer = np->test_data >> 32; in n2rng_data_read()
436 *data = np->test_data & 0xffffffff; in n2rng_data_read()
439 dev_err(&np->op->dev, "RNG error, retesting\n"); in n2rng_data_read()
440 np->flags &= ~N2RNG_FLAG_READY; in n2rng_data_read()
441 if (!(np->flags & N2RNG_FLAG_SHUTDOWN)) in n2rng_data_read()
442 schedule_delayed_work(&np->work, 0); in n2rng_data_read()
451 * If a control node reboots or reloads it's n2rng driver, this won't
457 unsigned long ra = __pa(&np->test_data); in n2rng_guest_check()
506 if (np->test_buffer[i] == val) in n2rng_test_buffer_find()
517 dev_err(&np->op->dev, "Test buffer slot %d [0x%016llx]\n", in n2rng_dump_test_buffer()
518 i, np->test_buffer[i]); in n2rng_dump_test_buffer()
526 switch (np->data->id) { in n2rng_check_selftest_buffer()
532 break; in n2rng_check_selftest_buffer()
535 break; in n2rng_check_selftest_buffer()
542 break; in n2rng_check_selftest_buffer()
548 err = -ENODEV; in n2rng_check_selftest_buffer()
549 dev_err(&np->op->dev, "Selftest failed on unit %lu\n", unit); in n2rng_check_selftest_buffer()
552 dev_info(&np->op->dev, "Selftest passed on unit %lu\n", unit); in n2rng_check_selftest_buffer()
562 switch (np->data->id) { in n2rng_control_selftest()
568 ((RNG_v1_SELFTEST_TICKS - 2) << RNG_v1_CTL_WAIT_SHIFT); in n2rng_control_selftest()
569 break; in n2rng_control_selftest()
573 ((RNG_v1_SELFTEST_TICKS - 2) << RNG_v2_CTL_WAIT_SHIFT); in n2rng_control_selftest()
574 break; in n2rng_control_selftest()
579 break; in n2rng_control_selftest()
582 np->test_control[0] = base; in n2rng_control_selftest()
583 np->test_control[1] = base; in n2rng_control_selftest()
584 np->test_control[2] = base; in n2rng_control_selftest()
585 np->test_control[3] = base3; in n2rng_control_selftest()
587 err = n2rng_entropy_diag_read(np, unit, np->test_control, in n2rng_control_selftest()
589 np->test_buffer, in n2rng_control_selftest()
590 sizeof(np->test_buffer), in n2rng_control_selftest()
591 &np->units[unit].control[0], in n2rng_control_selftest()
592 np->hv_state); in n2rng_control_selftest()
603 for (i = 0; i < np->num_units; i++) { in n2rng_control_check()
619 for (unit = 0; unit < np->num_units; unit++) { in n2rng_control_configure_units()
620 struct n2rng_unit *up = &np->units[unit]; in n2rng_control_configure_units()
621 unsigned long ctl_ra = __pa(&up->control[0]); in n2rng_control_configure_units()
625 if (np->data->chip_version == 1) { in n2rng_control_configure_units()
626 base = ((np->accum_cycles << RNG_v1_CTL_WAIT_SHIFT) | in n2rng_control_configure_units()
631 base = ((np->accum_cycles << RNG_v2_CTL_WAIT_SHIFT) | in n2rng_control_configure_units()
643 up->control[esrc] = base | in n2rng_control_configure_units()
647 up->control[3] = base | in n2rng_control_configure_units()
653 break; in n2rng_control_configure_units()
665 if (!(np->flags & N2RNG_FLAG_CONTROL)) { in n2rng_work()
677 np->flags |= N2RNG_FLAG_READY; in n2rng_work()
678 dev_info(&np->op->dev, "RNG ready\n"); in n2rng_work()
681 if (--retries == 0) in n2rng_work()
682 dev_err(&np->op->dev, "Self-test retries failed, RNG not ready\n"); in n2rng_work()
683 else if (err && !(np->flags & N2RNG_FLAG_SHUTDOWN)) in n2rng_work()
684 schedule_delayed_work(&np->work, HZ * 2); in n2rng_work()
699 int err = -ENOMEM; in n2rng_probe()
702 match = of_match_device(n2rng_match, &op->dev); in n2rng_probe()
704 return -EINVAL; in n2rng_probe()
707 np = devm_kzalloc(&op->dev, sizeof(*np), GFP_KERNEL); in n2rng_probe()
710 np->op = op; in n2rng_probe()
711 np->data = (struct n2rng_template *)match->data; in n2rng_probe()
713 INIT_DELAYED_WORK(&np->work, n2rng_work); in n2rng_probe()
715 if (np->data->multi_capable) in n2rng_probe()
716 np->flags |= N2RNG_FLAG_MULTI; in n2rng_probe()
718 err = -ENODEV; in n2rng_probe()
719 np->hvapi_major = 2; in n2rng_probe()
721 np->hvapi_major, in n2rng_probe()
722 &np->hvapi_minor)) { in n2rng_probe()
723 np->hvapi_major = 1; in n2rng_probe()
725 np->hvapi_major, in n2rng_probe()
726 &np->hvapi_minor)) { in n2rng_probe()
727 dev_err(&op->dev, "Cannot register suitable " in n2rng_probe()
733 if (np->flags & N2RNG_FLAG_MULTI) { in n2rng_probe()
734 if (np->hvapi_major < 2) { in n2rng_probe()
735 dev_err(&op->dev, "multi-unit-capable RNG requires " in n2rng_probe()
737 np->hvapi_major); in n2rng_probe()
740 np->num_units = of_getintprop_default(op->dev.of_node, in n2rng_probe()
741 "rng-#units", 0); in n2rng_probe()
742 if (!np->num_units) { in n2rng_probe()
743 dev_err(&op->dev, "VF RNG lacks rng-#units property\n"); in n2rng_probe()
747 np->num_units = 1; in n2rng_probe()
750 dev_info(&op->dev, "Registered RNG HVAPI major %lu minor %lu\n", in n2rng_probe()
751 np->hvapi_major, np->hvapi_minor); in n2rng_probe()
752 np->units = devm_kcalloc(&op->dev, np->num_units, sizeof(*np->units), in n2rng_probe()
754 err = -ENOMEM; in n2rng_probe()
755 if (!np->units) in n2rng_probe()
762 dev_info(&op->dev, "Found %s RNG, units: %d\n", in n2rng_probe()
763 ((np->flags & N2RNG_FLAG_MULTI) ? in n2rng_probe()
764 "multi-unit-capable" : "single-unit"), in n2rng_probe()
765 np->num_units); in n2rng_probe()
767 np->hwrng.name = DRV_MODULE_NAME; in n2rng_probe()
768 np->hwrng.data_read = n2rng_data_read; in n2rng_probe()
769 np->hwrng.priv = (unsigned long) np; in n2rng_probe()
771 err = devm_hwrng_register(&op->dev, &np->hwrng); in n2rng_probe()
777 schedule_delayed_work(&np->work, 0); in n2rng_probe()
792 np->flags |= N2RNG_FLAG_SHUTDOWN; in n2rng_remove()
794 cancel_delayed_work_sync(&np->work); in n2rng_remove()
833 .name = "random-number-generator",
834 .compatible = "SUNW,n2-rng",
838 .name = "random-number-generator",
839 .compatible = "SUNW,vf-rng",
843 .name = "random-number-generator",
844 .compatible = "SUNW,kt-rng",
848 .name = "random-number-generator",
849 .compatible = "ORCL,m4-rng",
853 .name = "random-number-generator",
854 .compatible = "ORCL,m7-rng",