rng.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size)
  22. {
  23. int i, j;
  24. u32 v1, v2, rng_last = sc->rng_last;
  25. struct ath_hw *ah = sc->sc_ah;
  26. ath9k_ps_wakeup(sc);
  27. REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 1);
  28. REG_CLR_BIT(ah, AR_PHY_TEST, AR_PHY_TEST_RX_OBS_SEL_BIT5);
  29. REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS, AR_PHY_TEST_CTL_RX_OBS_SEL, 0);
  30. for (i = 0, j = 0; i < buf_size; i++) {
  31. v1 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff;
  32. v2 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff;
  33. /* wait for data ready */
  34. if (v1 && v2 && rng_last != v1 && v1 != v2 && v1 != 0xffff &&
  35. v2 != 0xffff)
  36. buf[j++] = (v1 << 16) | v2;
  37. rng_last = v2;
  38. }
  39. ath9k_ps_restore(sc);
  40. sc->rng_last = rng_last;
  41. return j << 2;
  42. }
  43. static u32 ath9k_rng_delay_get(u32 fail_stats)
  44. {
  45. u32 delay;
  46. if (fail_stats < 100)
  47. delay = 10;
  48. else if (fail_stats < 105)
  49. delay = 1000;
  50. else
  51. delay = 10000;
  52. return delay;
  53. }
  54. static int ath9k_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
  55. {
  56. struct ath_softc *sc = container_of(rng, struct ath_softc, rng_ops);
  57. u32 fail_stats = 0, word;
  58. int bytes_read = 0;
  59. for (;;) {
  60. if (max & ~3UL)
  61. bytes_read = ath9k_rng_data_read(sc, buf, max >> 2);
  62. if ((max & 3UL) && ath9k_rng_data_read(sc, &word, 1)) {
  63. memcpy(buf + bytes_read, &word, max & 3UL);
  64. bytes_read += max & 3UL;
  65. memzero_explicit(&word, sizeof(word));
  66. }
  67. if (!wait || !max || likely(bytes_read) || fail_stats > 110)
  68. break;
  69. if (hwrng_msleep(rng, ath9k_rng_delay_get(++fail_stats)))
  70. break;
  71. }
  72. if (wait && !bytes_read && max)
  73. bytes_read = -EIO;
  74. return bytes_read;
  75. }
  76. void ath9k_rng_start(struct ath_softc *sc)
  77. {
  78. static atomic_t serial = ATOMIC_INIT(0);
  79. struct ath_hw *ah = sc->sc_ah;
  80. if (sc->rng_ops.read)
  81. return;
  82. if (!AR_SREV_9300_20_OR_LATER(ah))
  83. return;
  84. snprintf(sc->rng_name, sizeof(sc->rng_name), "ath9k_%u",
  85. (atomic_inc_return(&serial) - 1) & U16_MAX);
  86. sc->rng_ops.name = sc->rng_name;
  87. sc->rng_ops.read = ath9k_rng_read;
  88. sc->rng_ops.quality = 320;
  89. if (devm_hwrng_register(sc->dev, &sc->rng_ops))
  90. sc->rng_ops.read = NULL;
  91. }
  92. void ath9k_rng_stop(struct ath_softc *sc)
  93. {
  94. if (sc->rng_ops.read) {
  95. devm_hwrng_unregister(sc->dev, &sc->rng_ops);
  96. sc->rng_ops.read = NULL;
  97. }
  98. }