rng.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2015 Qualcomm Atheros, Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/hw_random.h>
  17. #include <linux/kthread.h>
  18. #include "ath9k.h"
  19. #include "hw.h"
  20. #include "ar9003_phy.h"
  21. #define ATH9K_RNG_BUF_SIZE 320
  22. #define ATH9K_RNG_ENTROPY(x) (((x) * 8 * 10) >> 5) /* quality: 10/32 */
  23. static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size)
  24. {
  25. int i, j;
  26. u32 v1, v2, rng_last = sc->rng_last;
  27. struct ath_hw *ah = sc->sc_ah;
  28. ath9k_ps_wakeup(sc);
  29. REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 1);
  30. REG_CLR_BIT(ah, AR_PHY_TEST, AR_PHY_TEST_RX_OBS_SEL_BIT5);
  31. REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS, AR_PHY_TEST_CTL_RX_OBS_SEL, 0);
  32. for (i = 0, j = 0; i < buf_size; i++) {
  33. v1 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff;
  34. v2 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff;
  35. /* wait for data ready */
  36. if (v1 && v2 && rng_last != v1 && v1 != v2 && v1 != 0xffff &&
  37. v2 != 0xffff)
  38. buf[j++] = (v1 << 16) | v2;
  39. rng_last = v2;
  40. }
  41. ath9k_ps_restore(sc);
  42. sc->rng_last = rng_last;
  43. return j << 2;
  44. }
  45. static u32 ath9k_rng_delay_get(u32 fail_stats)
  46. {
  47. u32 delay;
  48. if (fail_stats < 100)
  49. delay = 10;
  50. else if (fail_stats < 105)
  51. delay = 1000;
  52. else
  53. delay = 10000;
  54. return delay;
  55. }
  56. static int ath9k_rng_kthread(void *data)
  57. {
  58. int bytes_read;
  59. struct ath_softc *sc = data;
  60. u32 *rng_buf;
  61. u32 delay, fail_stats = 0;
  62. rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL);
  63. if (!rng_buf)
  64. goto out;
  65. while (!kthread_should_stop()) {
  66. bytes_read = ath9k_rng_data_read(sc, rng_buf,
  67. ATH9K_RNG_BUF_SIZE);
  68. if (unlikely(!bytes_read)) {
  69. delay = ath9k_rng_delay_get(++fail_stats);
  70. msleep_interruptible(delay);
  71. continue;
  72. }
  73. fail_stats = 0;
  74. /* sleep until entropy bits under write_wakeup_threshold */
  75. add_hwgenerator_randomness((void *)rng_buf, bytes_read,
  76. ATH9K_RNG_ENTROPY(bytes_read));
  77. }
  78. kfree(rng_buf);
  79. out:
  80. sc->rng_task = NULL;
  81. return 0;
  82. }
  83. void ath9k_rng_start(struct ath_softc *sc)
  84. {
  85. struct ath_hw *ah = sc->sc_ah;
  86. if (sc->rng_task)
  87. return;
  88. if (!AR_SREV_9300_20_OR_LATER(ah))
  89. return;
  90. sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng");
  91. if (IS_ERR(sc->rng_task))
  92. sc->rng_task = NULL;
  93. }
  94. void ath9k_rng_stop(struct ath_softc *sc)
  95. {
  96. if (sc->rng_task)
  97. kthread_stop(sc->rng_task);
  98. }