rng.c (6301566e0b2dafa7d6779598621bca867962a0a2) rng.c (ed14dc0af7ccea867b479feb88efdfe43ca2a0f9)
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
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
17#include <linux/hw_random.h>
18#include <linux/kthread.h>
19
17#include "ath9k.h"
18#include "hw.h"
19#include "ar9003_phy.h"
20
20#include "ath9k.h"
21#include "hw.h"
22#include "ar9003_phy.h"
23
21static int ath9k_rng_data_read(struct hwrng *rng, u32 *data)
24#define ATH9K_RNG_BUF_SIZE 320
25#define ATH9K_RNG_ENTROPY(x) (((x) * 8 * 320) >> 10) /* quality: 320/1024 */
26
27static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size)
22{
28{
23 u32 v1, v2;
24 struct ath_softc *sc = (struct ath_softc *)rng->priv;
29 int i, j;
30 u32 v1, v2, rng_last = sc->rng_last;
25 struct ath_hw *ah = sc->sc_ah;
26
27 ath9k_ps_wakeup(sc);
28
31 struct ath_hw *ah = sc->sc_ah;
32
33 ath9k_ps_wakeup(sc);
34
29 REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 5);
35 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
36 REG_CLR_BIT(ah, AR_PHY_TEST, AR_PHY_TEST_RX_OBS_SEL_BIT5);
37 REG_RMW_FIELD(ah, AR_PHY_TEST_CTL_STATUS, AR_PHY_TEST_CTL_RX_OBS_SEL, 0);
38
33 v1 = REG_READ(ah, AR_PHY_TST_ADC);
34 v2 = REG_READ(ah, AR_PHY_TST_ADC);
39 for (i = 0, j = 0; i < buf_size; i++) {
40 v1 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff;
41 v2 = REG_READ(ah, AR_PHY_TST_ADC) & 0xffff;
35
42
43 /* wait for data ready */
44 if (v1 && v2 && rng_last != v1 && v1 != v2 && v1 != 0xffff &&
45 v2 != 0xffff)
46 buf[j++] = (v1 << 16) | v2;
47
48 rng_last = v2;
49 }
50
36 ath9k_ps_restore(sc);
37
51 ath9k_ps_restore(sc);
52
38 /* wait for data ready */
39 if (v1 && v2 && sc->rng_last != v1 && v1 != v2) {
40 *data = (v1 & 0xffff) | (v2 << 16);
41 sc->rng_last = v2;
53 sc->rng_last = rng_last;
42
54
43 return sizeof(u32);
55 return j << 2;
56}
57
58static int ath9k_rng_kthread(void *data)
59{
60 int bytes_read;
61 struct ath_softc *sc = data;
62 u32 *rng_buf;
63
64 rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL);
65 if (!rng_buf)
66 goto out;
67
68 while (!kthread_should_stop()) {
69 bytes_read = ath9k_rng_data_read(sc, rng_buf,
70 ATH9K_RNG_BUF_SIZE);
71 if (unlikely(!bytes_read)) {
72 msleep_interruptible(10);
73 continue;
74 }
75
76 /* sleep until entropy bits under write_wakeup_threshold */
77 add_hwgenerator_randomness((void *)rng_buf, bytes_read,
78 ATH9K_RNG_ENTROPY(bytes_read));
44 }
45
79 }
80
46 sc->rng_last = v2;
81 kfree(rng_buf);
82out:
83 sc->rng_task = NULL;
47
48 return 0;
49}
50
84
85 return 0;
86}
87
51void ath9k_rng_register(struct ath_softc *sc)
88void ath9k_rng_start(struct ath_softc *sc)
52{
53 struct ath_hw *ah = sc->sc_ah;
54
89{
90 struct ath_hw *ah = sc->sc_ah;
91
55 if (WARN_ON(sc->rng_initialized))
92 if (sc->rng_task)
56 return;
57
58 if (!AR_SREV_9300_20_OR_LATER(ah))
59 return;
60
93 return;
94
95 if (!AR_SREV_9300_20_OR_LATER(ah))
96 return;
97
61 sc->rng.name = "ath9k";
62 sc->rng.data_read = ath9k_rng_data_read;
63 sc->rng.priv = (unsigned long)sc;
64
65 if (!hwrng_register(&sc->rng))
66 sc->rng_initialized = true;
98 sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng");
99 if (IS_ERR(sc->rng_task))
100 sc->rng_task = NULL;
67}
68
101}
102
69void ath9k_rng_unregister(struct ath_softc *sc)
103void ath9k_rng_stop(struct ath_softc *sc)
70{
104{
71 if (sc->rng_initialized) {
72 hwrng_unregister(&sc->rng);
73 sc->rng_initialized = false;
74 }
105 if (sc->rng_task)
106 kthread_stop(sc->rng_task);
75}
107}