rng.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 * 320) >> 10) /* quality: 320/1024 */
  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 int ath9k_rng_kthread(void *data)
  46. {
  47. int bytes_read;
  48. struct ath_softc *sc = data;
  49. u32 *rng_buf;
  50. rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL);
  51. if (!rng_buf)
  52. goto out;
  53. while (!kthread_should_stop()) {
  54. bytes_read = ath9k_rng_data_read(sc, rng_buf,
  55. ATH9K_RNG_BUF_SIZE);
  56. if (unlikely(!bytes_read)) {
  57. msleep_interruptible(10);
  58. continue;
  59. }
  60. /* sleep until entropy bits under write_wakeup_threshold */
  61. add_hwgenerator_randomness((void *)rng_buf, bytes_read,
  62. ATH9K_RNG_ENTROPY(bytes_read));
  63. }
  64. kfree(rng_buf);
  65. out:
  66. sc->rng_task = NULL;
  67. return 0;
  68. }
  69. void ath9k_rng_start(struct ath_softc *sc)
  70. {
  71. struct ath_hw *ah = sc->sc_ah;
  72. if (sc->rng_task)
  73. return;
  74. if (!AR_SREV_9300_20_OR_LATER(ah))
  75. return;
  76. sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng");
  77. if (IS_ERR(sc->rng_task))
  78. sc->rng_task = NULL;
  79. }
  80. void ath9k_rng_stop(struct ath_softc *sc)
  81. {
  82. if (sc->rng_task)
  83. kthread_stop(sc->rng_task);
  84. }