xref: /linux/drivers/net/wireless/morsemicro/mm81x/mmrc.h (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2017-2026 Morse Micro
4  */
5 
6 #ifndef _MM81X_MMRC_H_
7 #define _MM81X_MMRC_H_
8 
9 #include <linux/version.h>
10 #include <linux/types.h>
11 #include <linux/slab.h>
12 #include <linux/bitops.h>
13 #include <linux/random.h>
14 #include <linux/time.h>
15 
16 /* The max length of a retry chain for a single packet transmission */
17 #define MMRC_MAX_CHAIN_LENGTH 4
18 
19 /* Rate minimum allowed attempts */
20 #define MMRC_MIN_CHAIN_ATTEMPTS 1
21 
22 /* Rate upper limit for attempts */
23 #define MMRC_MAX_CHAIN_ATTEMPTS 2
24 
25 /* The frequency of MMRC stat table updates */
26 #define MMRC_UPDATE_FREQUENCY_MS 100
27 
28 enum mmrc_flags {
29 	MMRC_FLAGS_CTS_RTS,
30 };
31 
32 enum mmrc_mcs_rate {
33 	MMRC_MCS0,
34 	MMRC_MCS1,
35 	MMRC_MCS2,
36 	MMRC_MCS3,
37 	MMRC_MCS4,
38 	MMRC_MCS5,
39 	MMRC_MCS6,
40 	MMRC_MCS7,
41 	MMRC_MCS8,
42 	MMRC_MCS9,
43 	MMRC_MCS10,
44 	MMRC_MCS_UNUSED,
45 };
46 
47 enum mmrc_bw {
48 	MMRC_BW_1MHZ = 0,
49 	MMRC_BW_2MHZ = 1,
50 	MMRC_BW_4MHZ = 2,
51 	MMRC_BW_8MHZ = 3,
52 	MMRC_BW_16MHZ = 4,
53 	MMRC_BW_MAX = 5,
54 };
55 
56 enum mmrc_spatial_stream {
57 	MMRC_SPATIAL_STREAM_1 = 0,
58 	MMRC_SPATIAL_STREAM_2 = 1,
59 	MMRC_SPATIAL_STREAM_3 = 2,
60 	MMRC_SPATIAL_STREAM_4 = 3,
61 	MMRC_SPATIAL_STREAM_MAX,
62 };
63 
64 enum mmrc_guard {
65 	MMRC_GUARD_LONG = 0,
66 	MMRC_GUARD_SHORT = 1,
67 	MMRC_GUARD_MAX,
68 };
69 
70 #define MMRC_RATE_TO_BITFIELD(x) ((x) & 0xF)
71 #define MMRC_ATTEMPTS_TO_BITFIELD(x) ((x) & 0x7)
72 #define MMRC_GUARD_TO_BITFIELD(x) ((x) & 0x1)
73 #define MMRC_SS_TO_BITFIELD(x) ((x) & 0x3)
74 #define MMRC_BW_TO_BITFIELD(x) ((x) & 0x7)
75 #define MMRC_FLAGS_TO_BITFIELD(x) ((x) & 0x7)
76 
77 struct mmrc_rate {
78 	u8 rate : 4;
79 	u8 attempts : 3;
80 	u8 guard : 1;
81 	u8 ss : 2;
82 	u8 bw : 3;
83 	u8 flags : 3;
84 	u16 index;
85 };
86 
87 struct mmrc_rate_table {
88 	struct mmrc_rate rates[MMRC_MAX_CHAIN_LENGTH];
89 };
90 
91 #define SGI_PER_BW(bw) (1 << (bw))
92 
93 struct mmrc_sta_capabilities {
94 	u8 max_rates : 3;
95 	u8 max_retries : 3;
96 	u8 bandwidth : 5;
97 	u8 spatial_streams : 4;
98 	u16 rates : 11;
99 	u8 guard : 2;
100 	u8 sta_flags : 4;
101 	u8 sgi_per_bw : 5;
102 };
103 
104 struct mmrc_stats_table {
105 	u32 avg_throughput_counter;
106 	u32 sum_throughput;
107 	u32 max_throughput;
108 	u16 sent;
109 	u16 sent_success;
110 	u16 back_mpdu_success;
111 	u16 back_mpdu_failure;
112 	u32 total_sent;
113 	u32 total_success;
114 	u16 evidence;
115 	u8 prob;
116 	bool have_sent_ampdus;
117 };
118 
119 struct mmrc_table {
120 	struct mmrc_sta_capabilities caps;
121 	struct mmrc_rate best_tp;
122 	struct mmrc_rate second_tp;
123 	struct mmrc_rate baseline;
124 	struct mmrc_rate best_prob;
125 	struct mmrc_rate fixed_rate;
126 	u32 cycle_cnt;
127 	u32 last_lookaround_cycle;
128 	u8 lookaround_cnt;
129 
130 	/* The ratio of using normal rate and sampling */
131 	u8 lookaround_wrap;
132 
133 	/*
134 	 * A counter that is used to determine when we should force a
135 	 * lookaround. Should be a portion of the above lookaround with
136 	 * less constraints
137 	 */
138 	u8 forced_lookaround;
139 
140 	u8 current_lookaround_rate_attempts;
141 	u16 current_lookaround_rate_index;
142 	u32 total_lookaround;
143 
144 	/*
145 	 * A counter to detect if the current best rate is optimal
146 	 * and may slow down sample frequency.
147 	 */
148 	u32 stability_cnt;
149 
150 	u32 stability_cnt_threshold;
151 	u8 probability_variation;
152 
153 	/* The difference in MCS from each of the last 2 rate changes */
154 	s8 best_rate_diff[2];
155 
156 	/* Indication of random versus consistently one-sided variation */
157 	s8 probability_variation_direction;
158 
159 	/* Has rate control detected possible interference */
160 	bool interference_likely;
161 
162 	/* Has rate control detected the best rate is no longer converged */
163 	bool unconverged;
164 
165 	/* Is rate control just entering unconverged state */
166 	bool newly_unconverged;
167 
168 	/*
169 	 * Number of rate control cycles the best rate has remained
170 	 * unchanged
171 	 */
172 	s32 best_rate_cycle_count;
173 
174 	/*
175 	 * The probability table for the STA. This MUST always be the last
176 	 * element in the struct.
177 	 */
178 	struct mmrc_stats_table table[];
179 };
180 
181 void mmrc_sta_init(struct mmrc_table *tb, struct mmrc_sta_capabilities *caps,
182 		   s8 rssi);
183 size_t mmrc_memory_required_for_caps(struct mmrc_sta_capabilities *caps);
184 void mmrc_get_rates(struct mmrc_table *tb, struct mmrc_rate_table *out,
185 		    size_t size);
186 void mmrc_feedback(struct mmrc_table *tb, struct mmrc_rate_table *rates,
187 		   s32 retry_count, bool was_aggregated);
188 void mmrc_update(struct mmrc_table *tb);
189 bool mmrc_set_fixed_rate(struct mmrc_table *tb, struct mmrc_rate fixed_rate);
190 u32 mmrc_calculate_theoretical_throughput(struct mmrc_rate rate);
191 u32 mmrc_calculate_rate_tx_time(struct mmrc_rate *rate, size_t size);
192 
193 #endif /* _MMRC_H_ */
194