xref: /openbmc/linux/drivers/net/wireless/ath/ath9k/rng.c (revision bc5aa3a0)
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 
20 #include "ath9k.h"
21 #include "hw.h"
22 #include "ar9003_phy.h"
23 
24 #define ATH9K_RNG_BUF_SIZE	320
25 #define ATH9K_RNG_ENTROPY(x)	(((x) * 8 * 320) >> 10) /* quality: 320/1024 */
26 
27 static int ath9k_rng_data_read(struct ath_softc *sc, u32 *buf, u32 buf_size)
28 {
29 	int i, j;
30 	u32  v1, v2, rng_last = sc->rng_last;
31 	struct ath_hw *ah = sc->sc_ah;
32 
33 	ath9k_ps_wakeup(sc);
34 
35 	REG_RMW_FIELD(ah, AR_PHY_TEST, AR_PHY_TEST_BBB_OBS_SEL, 1);
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 
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;
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 
51 	ath9k_ps_restore(sc);
52 
53 	sc->rng_last = rng_last;
54 
55 	return j << 2;
56 }
57 
58 static u32 ath9k_rng_delay_get(u32 fail_stats)
59 {
60 	u32 delay;
61 
62 	if (fail_stats < 100)
63 		delay = 10;
64 	else if (fail_stats < 105)
65 		delay = 1000;
66 	else
67 		delay = 10000;
68 
69 	return delay;
70 }
71 
72 static int ath9k_rng_kthread(void *data)
73 {
74 	int bytes_read;
75 	struct ath_softc *sc = data;
76 	u32 *rng_buf;
77 	u32 delay, fail_stats = 0;
78 
79 	rng_buf = kmalloc_array(ATH9K_RNG_BUF_SIZE, sizeof(u32), GFP_KERNEL);
80 	if (!rng_buf)
81 		goto out;
82 
83 	while (!kthread_should_stop()) {
84 		bytes_read = ath9k_rng_data_read(sc, rng_buf,
85 						 ATH9K_RNG_BUF_SIZE);
86 		if (unlikely(!bytes_read)) {
87 			delay = ath9k_rng_delay_get(++fail_stats);
88 			msleep_interruptible(delay);
89 			continue;
90 		}
91 
92 		fail_stats = 0;
93 
94 		/* sleep until entropy bits under write_wakeup_threshold */
95 		add_hwgenerator_randomness((void *)rng_buf, bytes_read,
96 					   ATH9K_RNG_ENTROPY(bytes_read));
97 	}
98 
99 	kfree(rng_buf);
100 out:
101 	sc->rng_task = NULL;
102 
103 	return 0;
104 }
105 
106 void ath9k_rng_start(struct ath_softc *sc)
107 {
108 	struct ath_hw *ah = sc->sc_ah;
109 
110 	if (sc->rng_task)
111 		return;
112 
113 	if (!AR_SREV_9300_20_OR_LATER(ah))
114 		return;
115 
116 	sc->rng_task = kthread_run(ath9k_rng_kthread, sc, "ath9k-hwrng");
117 	if (IS_ERR(sc->rng_task))
118 		sc->rng_task = NULL;
119 }
120 
121 void ath9k_rng_stop(struct ath_softc *sc)
122 {
123 	if (sc->rng_task)
124 		kthread_stop(sc->rng_task);
125 }
126