2 * Copyright (c) 2004 Video54 Technologies, Inc.
3 * Copyright (c) 2004-2008 Atheros Communications, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Atheros rate control algorithm
23 /* FIXME: remove this include! */
24 #include "../net/mac80211/rate.h"
26 static u32 tx_triglevel_max;
28 static struct ath_rate_table ar5416_11na_ratetable = {
31 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
33 0, 2, 1, 0, 0, 0, 0, 0 },
34 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
36 0, 3, 1, 1, 1, 1, 1, 0 },
37 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
38 10000, 0x0a, 0x00, 24,
39 2, 4, 2, 2, 2, 2, 2, 0 },
40 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
41 13900, 0x0e, 0x00, 36,
42 2, 6, 2, 3, 3, 3, 3, 0 },
43 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
44 17300, 0x09, 0x00, 48,
45 4, 10, 3, 4, 4, 4, 4, 0 },
46 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
47 23000, 0x0d, 0x00, 72,
48 4, 14, 3, 5, 5, 5, 5, 0 },
49 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
50 27400, 0x08, 0x00, 96,
51 4, 20, 3, 6, 6, 6, 6, 0 },
52 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
53 29300, 0x0c, 0x00, 108,
54 4, 23, 3, 7, 7, 7, 7, 0 },
55 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 6500, /* 6.5 Mb */
57 0, 2, 3, 8, 24, 8, 24, 3216 },
58 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 13000, /* 13 Mb */
60 2, 4, 3, 9, 25, 9, 25, 6434 },
61 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 19500, /* 19.5 Mb */
63 2, 6, 3, 10, 26, 10, 26, 9650 },
64 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 26000, /* 26 Mb */
66 4, 10, 3, 11, 27, 11, 27, 12868 },
67 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 39000, /* 39 Mb */
69 4, 14, 3, 12, 28, 12, 28, 19304 },
70 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 52000, /* 52 Mb */
72 4, 20, 3, 13, 29, 13, 29, 25740 },
73 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 58500, /* 58.5 Mb */
75 4, 23, 3, 14, 30, 14, 30, 28956 },
76 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 65000, /* 65 Mb */
78 4, 25, 3, 15, 31, 15, 32, 32180 },
79 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 13000, /* 13 Mb */
81 8, 0, 2, 3, 16, 33, 16, 33, 6430 },
82 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 26000, /* 26 Mb */
84 2, 4, 3, 17, 34, 17, 34, 12860 },
85 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 39000, /* 39 Mb */
86 36600, 0x8a, 0x00, 10,
87 2, 6, 3, 18, 35, 18, 35, 19300 },
88 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 52000, /* 52 Mb */
89 48100, 0x8b, 0x00, 11,
90 4, 10, 3, 19, 36, 19, 36, 25736 },
91 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 78000, /* 78 Mb */
92 69500, 0x8c, 0x00, 12,
93 4, 14, 3, 20, 37, 20, 37, 38600 },
94 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 104000, /* 104 Mb */
95 89500, 0x8d, 0x00, 13,
96 4, 20, 3, 21, 38, 21, 38, 51472 },
97 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 117000, /* 117 Mb */
98 98900, 0x8e, 0x00, 14,
99 4, 23, 3, 22, 39, 22, 39, 57890 },
100 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 130000, /* 130 Mb */
101 108300, 0x8f, 0x00, 15,
102 4, 25, 3, 23, 40, 23, 41, 64320 },
103 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 13500, /* 13.5 Mb */
104 13200, 0x80, 0x00, 0,
105 0, 2, 3, 8, 24, 24, 24, 6684 },
106 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 27500, /* 27.0 Mb */
107 25900, 0x81, 0x00, 1,
108 2, 4, 3, 9, 25, 25, 25, 13368 },
109 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 40500, /* 40.5 Mb */
110 38600, 0x82, 0x00, 2,
111 2, 6, 3, 10, 26, 26, 26, 20052 },
112 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 54000, /* 54 Mb */
113 49800, 0x83, 0x00, 3,
114 4, 10, 3, 11, 27, 27, 27, 26738 },
115 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 81500, /* 81 Mb */
116 72200, 0x84, 0x00, 4,
117 4, 14, 3, 12, 28, 28, 28, 40104 },
118 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 108000, /* 108 Mb */
119 92900, 0x85, 0x00, 5,
120 4, 20, 3, 13, 29, 29, 29, 53476 },
121 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 121500, /* 121.5 Mb */
122 102700, 0x86, 0x00, 6,
123 4, 23, 3, 14, 30, 30, 30, 60156 },
124 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 135000, /* 135 Mb */
125 112000, 0x87, 0x00, 7,
126 4, 25, 3, 15, 31, 32, 32, 66840 },
127 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
128 122000, 0x87, 0x00, 7,
129 4, 25, 3, 15, 31, 32, 32, 74200 },
130 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 27000, /* 27 Mb */
131 25800, 0x88, 0x00, 8,
132 0, 2, 3, 16, 33, 33, 33, 13360 },
133 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 54000, /* 54 Mb */
134 49800, 0x89, 0x00, 9,
135 2, 4, 3, 17, 34, 34, 34, 26720 },
136 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 81000, /* 81 Mb */
137 71900, 0x8a, 0x00, 10,
138 2, 6, 3, 18, 35, 35, 35, 40080 },
139 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 108000, /* 108 Mb */
140 92500, 0x8b, 0x00, 11,
141 4, 10, 3, 19, 36, 36, 36, 53440 },
142 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 162000, /* 162 Mb */
143 130300, 0x8c, 0x00, 12,
144 4, 14, 3, 20, 37, 37, 37, 80160 },
145 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 216000, /* 216 Mb */
146 162800, 0x8d, 0x00, 13,
147 4, 20, 3, 21, 38, 38, 38, 106880 },
148 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 243000, /* 243 Mb */
149 178200, 0x8e, 0x00, 14,
150 4, 23, 3, 22, 39, 39, 39, 120240 },
151 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 270000, /* 270 Mb */
152 192100, 0x8f, 0x00, 15,
153 4, 25, 3, 23, 40, 41, 41, 133600 },
154 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
155 207000, 0x8f, 0x00, 15,
156 4, 25, 3, 23, 40, 41, 41, 148400 },
158 50, /* probe interval */
159 50, /* rssi reduce interval */
160 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
163 /* TRUE_ALL - valid for 20/40/Legacy,
164 * TRUE - Legacy only,
165 * TRUE_20 - HT 20 only,
166 * TRUE_40 - HT 40 only */
168 /* 4ms frame limit not used for NG mode. The values filled
169 * for HT are the 64K max aggregate limit */
171 static struct ath_rate_table ar5416_11ng_ratetable = {
174 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 1000, /* 1 Mb */
176 0, 0, 1, 0, 0, 0, 0, 0 },
177 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 2000, /* 2 Mb */
179 1, 1, 1, 1, 1, 1, 1, 0 },
180 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
181 4900, 0x19, 0x04, 11,
182 2, 2, 2, 2, 2, 2, 2, 0 },
183 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 11000, /* 11 Mb */
184 8100, 0x18, 0x04, 22,
185 3, 3, 2, 3, 3, 3, 3, 0 },
186 { FALSE, FALSE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
187 5400, 0x0b, 0x00, 12,
188 4, 2, 1, 4, 4, 4, 4, 0 },
189 { FALSE, FALSE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
190 7800, 0x0f, 0x00, 18,
191 4, 3, 1, 5, 5, 5, 5, 0 },
192 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
193 10100, 0x0a, 0x00, 24,
194 6, 4, 1, 6, 6, 6, 6, 0 },
195 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
196 14100, 0x0e, 0x00, 36,
197 6, 6, 2, 7, 7, 7, 7, 0 },
198 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
199 17700, 0x09, 0x00, 48,
200 8, 10, 3, 8, 8, 8, 8, 0 },
201 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
202 23700, 0x0d, 0x00, 72,
203 8, 14, 3, 9, 9, 9, 9, 0 },
204 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
205 27400, 0x08, 0x00, 96,
206 8, 20, 3, 10, 10, 10, 10, 0 },
207 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
208 30900, 0x0c, 0x00, 108,
209 8, 23, 3, 11, 11, 11, 11, 0 },
210 { FALSE, FALSE, WLAN_PHY_HT_20_SS, 6500, /* 6.5 Mb */
212 4, 2, 3, 12, 28, 12, 28, 3216 },
213 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 13000, /* 13 Mb */
214 12700, 0x81, 0x00, 1,
215 6, 4, 3, 13, 29, 13, 29, 6434 },
216 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 19500, /* 19.5 Mb */
217 18800, 0x82, 0x00, 2,
218 6, 6, 3, 14, 30, 14, 30, 9650 },
219 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 26000, /* 26 Mb */
220 25000, 0x83, 0x00, 3,
221 8, 10, 3, 15, 31, 15, 31, 12868 },
222 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 39000, /* 39 Mb */
223 36700, 0x84, 0x00, 4,
224 8, 14, 3, 16, 32, 16, 32, 19304 },
225 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 52000, /* 52 Mb */
226 48100, 0x85, 0x00, 5,
227 8, 20, 3, 17, 33, 17, 33, 25740 },
228 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 58500, /* 58.5 Mb */
229 53500, 0x86, 0x00, 6,
230 8, 23, 3, 18, 34, 18, 34, 28956 },
231 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 65000, /* 65 Mb */
232 59000, 0x87, 0x00, 7,
233 8, 25, 3, 19, 35, 19, 36, 32180 },
234 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 13000, /* 13 Mb */
235 12700, 0x88, 0x00, 8,
236 4, 2, 3, 20, 37, 20, 37, 6430 },
237 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 26000, /* 26 Mb */
238 24800, 0x89, 0x00, 9,
239 6, 4, 3, 21, 38, 21, 38, 12860 },
240 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 39000, /* 39 Mb */
241 36600, 0x8a, 0x00, 10,
242 6, 6, 3, 22, 39, 22, 39, 19300 },
243 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 52000, /* 52 Mb */
244 48100, 0x8b, 0x00, 11,
245 8, 10, 3, 23, 40, 23, 40, 25736 },
246 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 78000, /* 78 Mb */
247 69500, 0x8c, 0x00, 12,
248 8, 14, 3, 24, 41, 24, 41, 38600 },
249 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 104000, /* 104 Mb */
250 89500, 0x8d, 0x00, 13,
251 8, 20, 3, 25, 42, 25, 42, 51472 },
252 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 117000, /* 117 Mb */
253 98900, 0x8e, 0x00, 14,
254 8, 23, 3, 26, 43, 26, 44, 57890 },
255 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 130000, /* 130 Mb */
256 108300, 0x8f, 0x00, 15,
257 8, 25, 3, 27, 44, 27, 45, 64320 },
258 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 13500, /* 13.5 Mb */
259 13200, 0x80, 0x00, 0,
260 8, 2, 3, 12, 28, 28, 28, 6684 },
261 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 27500, /* 27.0 Mb */
262 25900, 0x81, 0x00, 1,
263 8, 4, 3, 13, 29, 29, 29, 13368 },
264 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 40500, /* 40.5 Mb */
265 38600, 0x82, 0x00, 2,
266 8, 6, 3, 14, 30, 30, 30, 20052 },
267 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 54000, /* 54 Mb */
268 49800, 0x83, 0x00, 3,
269 8, 10, 3, 15, 31, 31, 31, 26738 },
270 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 81500, /* 81 Mb */
271 72200, 0x84, 0x00, 4,
272 8, 14, 3, 16, 32, 32, 32, 40104 },
273 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 108000, /* 108 Mb */
274 92900, 0x85, 0x00, 5,
275 8, 20, 3, 17, 33, 33, 33, 53476 },
276 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 121500, /* 121.5 Mb */
277 102700, 0x86, 0x00, 6,
278 8, 23, 3, 18, 34, 34, 34, 60156 },
279 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 135000, /* 135 Mb */
280 112000, 0x87, 0x00, 7,
281 8, 23, 3, 19, 35, 36, 36, 66840 },
282 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
283 122000, 0x87, 0x00, 7,
284 8, 25, 3, 19, 35, 36, 36, 74200 },
285 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 27000, /* 27 Mb */
286 25800, 0x88, 0x00, 8,
287 8, 2, 3, 20, 37, 37, 37, 13360 },
288 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 54000, /* 54 Mb */
289 49800, 0x89, 0x00, 9,
290 8, 4, 3, 21, 38, 38, 38, 26720 },
291 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 81000, /* 81 Mb */
292 71900, 0x8a, 0x00, 10,
293 8, 6, 3, 22, 39, 39, 39, 40080 },
294 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 108000, /* 108 Mb */
295 92500, 0x8b, 0x00, 11,
296 8, 10, 3, 23, 40, 40, 40, 53440 },
297 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 162000, /* 162 Mb */
298 130300, 0x8c, 0x00, 12,
299 8, 14, 3, 24, 41, 41, 41, 80160 },
300 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 216000, /* 216 Mb */
301 162800, 0x8d, 0x00, 13,
302 8, 20, 3, 25, 42, 42, 42, 106880 },
303 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 243000, /* 243 Mb */
304 178200, 0x8e, 0x00, 14,
305 8, 23, 3, 26, 43, 43, 43, 120240 },
306 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 270000, /* 270 Mb */
307 192100, 0x8f, 0x00, 15,
308 8, 23, 3, 27, 44, 45, 45, 133600 },
309 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
310 207000, 0x8f, 0x00, 15,
311 8, 25, 3, 27, 44, 45, 45, 148400 },
313 50, /* probe interval */
314 50, /* rssi reduce interval */
315 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
318 static struct ath_rate_table ar5416_11a_ratetable = {
321 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
322 5400, 0x0b, 0x00, (0x80|12),
324 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
325 7800, 0x0f, 0x00, 18,
327 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
328 10000, 0x0a, 0x00, (0x80|24),
330 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
331 13900, 0x0e, 0x00, 36,
333 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
334 17300, 0x09, 0x00, (0x80|48),
336 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
337 23000, 0x0d, 0x00, 72,
339 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
340 27400, 0x08, 0x00, 96,
342 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
343 29300, 0x0c, 0x00, 108,
346 50, /* probe interval */
347 50, /* rssi reduce interval */
348 0, /* Phy rates allowed initially */
351 static struct ath_rate_table ar5416_11g_ratetable = {
354 { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
357 { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
360 { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
361 4900, 0x19, 0x04, 11,
363 { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
364 8100, 0x18, 0x04, 22,
366 { FALSE, FALSE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
367 5400, 0x0b, 0x00, 12,
369 { FALSE, FALSE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
370 7800, 0x0f, 0x00, 18,
372 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
373 10000, 0x0a, 0x00, 24,
375 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
376 13900, 0x0e, 0x00, 36,
378 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
379 17300, 0x09, 0x00, 48,
381 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
382 23000, 0x0d, 0x00, 72,
384 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
385 27400, 0x08, 0x00, 96,
387 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
388 29300, 0x0c, 0x00, 108,
391 50, /* probe interval */
392 50, /* rssi reduce interval */
393 0, /* Phy rates allowed initially */
396 static struct ath_rate_table ar5416_11b_ratetable = {
399 { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
400 900, 0x1b, 0x00, (0x80|2),
402 { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
403 1800, 0x1a, 0x04, (0x80|4),
405 { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
406 4300, 0x19, 0x04, (0x80|11),
408 { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
409 7100, 0x18, 0x04, (0x80|22),
412 100, /* probe interval */
413 100, /* rssi reduce interval */
414 0, /* Phy rates allowed initially */
418 * Return the median of three numbers
420 static inline int8_t median(int8_t a, int8_t b, int8_t c)
439 static void ath_rc_sort_validrates(const struct ath_rate_table *rate_table,
440 struct ath_rate_node *ath_rc_priv)
442 u8 i, j, idx, idx_next;
444 for (i = ath_rc_priv->max_valid_rate - 1; i > 0; i--) {
445 for (j = 0; j <= i-1; j++) {
446 idx = ath_rc_priv->valid_rate_index[j];
447 idx_next = ath_rc_priv->valid_rate_index[j+1];
449 if (rate_table->info[idx].ratekbps >
450 rate_table->info[idx_next].ratekbps) {
451 ath_rc_priv->valid_rate_index[j] = idx_next;
452 ath_rc_priv->valid_rate_index[j+1] = idx;
458 /* Access functions for valid_txrate_mask */
460 static void ath_rc_init_valid_txmask(struct ath_rate_node *ath_rc_priv)
464 for (i = 0; i < ath_rc_priv->rate_table_size; i++)
465 ath_rc_priv->valid_rate_index[i] = FALSE;
468 static inline void ath_rc_set_valid_txmask(struct ath_rate_node *ath_rc_priv,
469 u8 index, int valid_tx_rate)
471 ASSERT(index <= ath_rc_priv->rate_table_size);
472 ath_rc_priv->valid_rate_index[index] = valid_tx_rate ? TRUE : FALSE;
475 static inline int ath_rc_isvalid_txmask(struct ath_rate_node *ath_rc_priv,
478 ASSERT(index <= ath_rc_priv->rate_table_size);
479 return ath_rc_priv->valid_rate_index[index];
482 /* Iterators for valid_txrate_mask */
484 ath_rc_get_nextvalid_txrate(const struct ath_rate_table *rate_table,
485 struct ath_rate_node *ath_rc_priv,
491 for (i = 0; i < ath_rc_priv->max_valid_rate - 1; i++) {
492 if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
493 *next_idx = ath_rc_priv->valid_rate_index[i+1];
498 /* No more valid rates */
503 /* Return true only for single stream */
505 static int ath_rc_valid_phyrate(u32 phy, u32 capflag, int ignore_cw)
507 if (WLAN_RC_PHY_HT(phy) & !(capflag & WLAN_RC_HT_FLAG))
509 if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))
511 if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_SGI_FLAG))
513 if (!ignore_cw && WLAN_RC_PHY_HT(phy))
514 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG))
516 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG))
522 ath_rc_get_nextlowervalid_txrate(const struct ath_rate_table *rate_table,
523 struct ath_rate_node *ath_rc_priv,
524 u8 cur_valid_txrate, u8 *next_idx)
528 for (i = 1; i < ath_rc_priv->max_valid_rate ; i++) {
529 if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
530 *next_idx = ath_rc_priv->valid_rate_index[i-1];
538 * Initialize the Valid Rate Index from valid entries in Rate Table
541 ath_rc_sib_init_validrates(struct ath_rate_node *ath_rc_priv,
542 const struct ath_rate_table *rate_table,
548 for (i = 0; i < rate_table->rate_cnt; i++) {
549 valid = (ath_rc_priv->single_stream ?
550 rate_table->info[i].valid_single_stream :
551 rate_table->info[i].valid);
553 u32 phy = rate_table->info[i].phy;
554 u8 valid_rate_count = 0;
556 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
559 valid_rate_count = ath_rc_priv->valid_phy_ratecnt[phy];
561 ath_rc_priv->valid_phy_rateidx[phy][valid_rate_count] = i;
562 ath_rc_priv->valid_phy_ratecnt[phy] += 1;
563 ath_rc_set_valid_txmask(ath_rc_priv, i, TRUE);
571 * Initialize the Valid Rate Index from Rate Set
574 ath_rc_sib_setvalid_rates(struct ath_rate_node *ath_rc_priv,
575 const struct ath_rate_table *rate_table,
576 struct ath_rateset *rateset,
579 /* XXX: Clean me up and make identation friendly */
582 /* Use intersection of working rates and valid rates */
583 for (i = 0; i < rateset->rs_nrates; i++) {
584 for (j = 0; j < rate_table->rate_cnt; j++) {
585 u32 phy = rate_table->info[j].phy;
586 u32 valid = (ath_rc_priv->single_stream ?
587 rate_table->info[j].valid_single_stream :
588 rate_table->info[j].valid);
590 /* We allow a rate only if its valid and the
591 * capflag matches one of the validity
592 * (TRUE/TRUE_20/TRUE_40) flags */
594 /* XXX: catch the negative of this branch
595 * first and then continue */
596 if (((rateset->rs_rates[i] & 0x7F) ==
597 (rate_table->info[j].dot11rate & 0x7F)) &&
598 ((valid & WLAN_RC_CAP_MODE(capflag)) ==
599 WLAN_RC_CAP_MODE(capflag)) &&
600 !WLAN_RC_PHY_HT(phy)) {
602 u8 valid_rate_count = 0;
604 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
608 ath_rc_priv->valid_phy_ratecnt[phy];
610 ath_rc_priv->valid_phy_rateidx[phy]
611 [valid_rate_count] = j;
612 ath_rc_priv->valid_phy_ratecnt[phy] += 1;
613 ath_rc_set_valid_txmask(ath_rc_priv, j, TRUE);
622 ath_rc_sib_setvalid_htrates(struct ath_rate_node *ath_rc_priv,
623 const struct ath_rate_table *rate_table,
624 u8 *mcs_set, u32 capflag)
628 /* Use intersection of working rates and valid rates */
629 for (i = 0; i < ((struct ath_rateset *)mcs_set)->rs_nrates; i++) {
630 for (j = 0; j < rate_table->rate_cnt; j++) {
631 u32 phy = rate_table->info[j].phy;
632 u32 valid = (ath_rc_priv->single_stream ?
633 rate_table->info[j].valid_single_stream :
634 rate_table->info[j].valid);
636 if (((((struct ath_rateset *)
637 mcs_set)->rs_rates[i] & 0x7F) !=
638 (rate_table->info[j].dot11rate & 0x7F)) ||
639 !WLAN_RC_PHY_HT(phy) ||
640 !WLAN_RC_PHY_HT_VALID(valid, capflag))
643 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
646 ath_rc_priv->valid_phy_rateidx[phy]
647 [ath_rc_priv->valid_phy_ratecnt[phy]] = j;
648 ath_rc_priv->valid_phy_ratecnt[phy] += 1;
649 ath_rc_set_valid_txmask(ath_rc_priv, j, TRUE);
656 struct ath_rate_softc *ath_rate_attach(struct ath_softc *sc)
658 struct ath_rate_softc *asc;
660 asc = kzalloc(sizeof(struct ath_rate_softc), GFP_KERNEL);
664 asc->hw_rate_table[ATH9K_MODE_11B] = &ar5416_11b_ratetable;
665 asc->hw_rate_table[ATH9K_MODE_11A] = &ar5416_11a_ratetable;
666 asc->hw_rate_table[ATH9K_MODE_11G] = &ar5416_11g_ratetable;
668 asc->hw_rate_table[ATH9K_MODE_11NA_HT20] = &ar5416_11na_ratetable;
669 asc->hw_rate_table[ATH9K_MODE_11NG_HT20] = &ar5416_11ng_ratetable;
671 asc->hw_rate_table[ATH9K_MODE_11NA_HT40PLUS] =
672 &ar5416_11na_ratetable;
673 asc->hw_rate_table[ATH9K_MODE_11NA_HT40MINUS] =
674 &ar5416_11na_ratetable;
675 asc->hw_rate_table[ATH9K_MODE_11NG_HT40PLUS] =
676 &ar5416_11ng_ratetable;
677 asc->hw_rate_table[ATH9K_MODE_11NG_HT40MINUS] =
678 &ar5416_11ng_ratetable;
680 /* Save Maximum TX Trigger Level (used for 11n) */
681 tx_triglevel_max = sc->sc_ah->ah_caps.tx_triglevel_max;
686 void ath_rate_detach(struct ath_rate_softc *asc)
692 u8 ath_rate_findrateix(struct ath_softc *sc,
695 const struct ath_rate_table *ratetable;
696 struct ath_rate_softc *rsc = sc->sc_rc;
699 ratetable = rsc->hw_rate_table[sc->sc_curmode];
701 if (WARN_ON(!ratetable))
704 for (i = 0; i < ratetable->rate_cnt; i++) {
705 if ((ratetable->info[i].dot11rate & 0x7f) == (dot11rate & 0x7f))
712 static u8 ath_rc_ratefind_ht(struct ath_softc *sc,
713 struct ath_rate_node *ath_rc_priv,
714 const struct ath_rate_table *rate_table,
715 int probe_allowed, int *is_probing,
718 u32 dt, best_thruput, this_thruput, now_msec;
719 u8 rate, next_rate, best_rate, maxindex, minindex;
720 int8_t rssi_last, rssi_reduce = 0, index = 0;
724 rssi_last = median(ath_rc_priv->rssi_last,
725 ath_rc_priv->rssi_last_prev,
726 ath_rc_priv->rssi_last_prev2);
729 * Age (reduce) last ack rssi based on how old it is.
730 * The bizarre numbers are so the delta is 160msec,
731 * meaning we divide by 16.
732 * 0msec <= dt <= 25msec: don't derate
733 * 25msec <= dt <= 185msec: derate linearly from 0 to 10dB
734 * 185msec <= dt: derate by 10dB
737 now_msec = jiffies_to_msecs(jiffies);
738 dt = now_msec - ath_rc_priv->rssi_time;
743 rssi_reduce = (u8)((dt - 25) >> 4);
745 /* Now reduce rssi_last by rssi_reduce */
746 if (rssi_last < rssi_reduce)
749 rssi_last -= rssi_reduce;
752 * Now look up the rate in the rssi table and return it.
753 * If no rates match then we return 0 (lowest rate)
757 maxindex = ath_rc_priv->max_valid_rate-1;
760 best_rate = minindex;
763 * Try the higher rate first. It will reduce memory moving time
764 * if we have very good channel characteristics.
766 for (index = maxindex; index >= minindex ; index--) {
769 rate = ath_rc_priv->valid_rate_index[index];
770 if (rate > ath_rc_priv->rate_max_phy)
774 * For TCP the average collision rate is around 11%,
775 * so we ignore PERs less than this. This is to
776 * prevent the rate we are currently using (whose
777 * PER might be in the 10-15 range because of TCP
778 * collisions) looking worse than the next lower
779 * rate whose PER has decayed close to 0. If we
780 * used to next lower rate, its PER would grow to
781 * 10-15 and we would be worse off then staying
782 * at the current rate.
784 per_thres = ath_rc_priv->state[rate].per;
788 this_thruput = rate_table->info[rate].user_ratekbps *
791 if (best_thruput <= this_thruput) {
792 best_thruput = this_thruput;
799 /* if we are retrying for more than half the number
800 * of max retries, use the min rate for the next retry
803 rate = ath_rc_priv->valid_rate_index[minindex];
805 ath_rc_priv->rssi_last_lookup = rssi_last;
808 * Must check the actual rate (ratekbps) to account for
809 * non-monoticity of 11g's rate table
812 if (rate >= ath_rc_priv->rate_max_phy && probe_allowed) {
813 rate = ath_rc_priv->rate_max_phy;
815 /* Probe the next allowed phy state */
816 /* FIXME:XXXX Check to make sure ratMax is checked properly */
817 if (ath_rc_get_nextvalid_txrate(rate_table,
818 ath_rc_priv, rate, &next_rate) &&
819 (now_msec - ath_rc_priv->probe_time >
820 rate_table->probe_interval) &&
821 (ath_rc_priv->hw_maxretry_pktcnt >= 1)) {
823 ath_rc_priv->probe_rate = rate;
824 ath_rc_priv->probe_time = now_msec;
825 ath_rc_priv->hw_maxretry_pktcnt = 0;
831 * Make sure rate is not higher than the allowed maximum.
832 * We should also enforce the min, but I suspect the min is
833 * normally 1 rather than 0 because of the rate 9 vs 6 issue
836 if (rate > (ath_rc_priv->rate_table_size - 1))
837 rate = ath_rc_priv->rate_table_size - 1;
839 ASSERT((rate_table->info[rate].valid && !ath_rc_priv->single_stream) ||
840 (rate_table->info[rate].valid_single_stream &&
841 ath_rc_priv->single_stream));
846 static void ath_rc_rate_set_series(const struct ath_rate_table *rate_table ,
847 struct ath_rc_series *series,
852 series->tries = tries;
853 series->flags = (rtsctsenable ? ATH_RC_RTSCTS_FLAG : 0) |
854 (WLAN_RC_PHY_DS(rate_table->info[rix].phy) ?
855 ATH_RC_DS_FLAG : 0) |
856 (WLAN_RC_PHY_40(rate_table->info[rix].phy) ?
857 ATH_RC_CW40_FLAG : 0) |
858 (WLAN_RC_PHY_SGI(rate_table->info[rix].phy) ?
859 ATH_RC_SGI_FLAG : 0);
861 series->rix = rate_table->info[rix].base_index;
862 series->max_4ms_framelen = rate_table->info[rix].max_4ms_framelen;
865 static u8 ath_rc_rate_getidx(struct ath_softc *sc,
866 struct ath_rate_node *ath_rc_priv,
867 const struct ath_rate_table *rate_table,
868 u8 rix, u16 stepdown,
875 for (j = RATE_TABLE_SIZE; j > 0; j--) {
876 if (ath_rc_get_nextlowervalid_txrate(rate_table,
877 ath_rc_priv, rix, &nextindex))
883 for (j = stepdown; j > 0; j--) {
884 if (ath_rc_get_nextlowervalid_txrate(rate_table,
885 ath_rc_priv, rix, &nextindex))
894 static void ath_rc_ratefind(struct ath_softc *sc,
895 struct ath_rate_node *ath_rc_priv,
896 int num_tries, int num_rates, unsigned int rcflag,
897 struct ath_rc_series series[], int *is_probe,
900 u8 try_per_rate = 0, i = 0, rix, nrix;
901 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
902 struct ath_rate_table *rate_table;
905 (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
906 rix = ath_rc_ratefind_ht(sc, ath_rc_priv, rate_table,
907 (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
911 if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*is_probe)) {
912 /* set one try for probe rates. For the
913 * probes don't enable rts */
914 ath_rc_rate_set_series(rate_table,
915 &series[i++], 1, nrix, FALSE);
917 try_per_rate = (num_tries/num_rates);
918 /* Get the next tried/allowed rate. No RTS for the next series
919 * after the probe rate
921 nrix = ath_rc_rate_getidx(sc,
922 ath_rc_priv, rate_table, nrix, 1, FALSE);
923 ath_rc_rate_set_series(rate_table,
924 &series[i++], try_per_rate, nrix, 0);
926 try_per_rate = (num_tries/num_rates);
927 /* Set the choosen rate. No RTS for first series entry. */
928 ath_rc_rate_set_series(rate_table,
929 &series[i++], try_per_rate, nrix, FALSE);
932 /* Fill in the other rates for multirate retry */
933 for ( ; i < num_rates; i++) {
937 try_num = ((i + 1) == num_rates) ?
938 num_tries - (try_per_rate * i) : try_per_rate ;
939 min_rate = (((i + 1) == num_rates) &&
940 (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
942 nrix = ath_rc_rate_getidx(sc, ath_rc_priv,
943 rate_table, nrix, 1, min_rate);
944 /* All other rates in the series have RTS enabled */
945 ath_rc_rate_set_series(rate_table,
946 &series[i], try_num, nrix, TRUE);
950 * NB:Change rate series to enable aggregation when operating
951 * at lower MCS rates. When first rate in series is MCS2
952 * in HT40 @ 2.4GHz, series should look like:
954 * {MCS2, MCS1, MCS0, MCS0}.
956 * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
959 * {MCS3, MCS2, MCS1, MCS1}
961 * So, set fourth rate in series to be same as third one for
964 if ((sc->sc_curmode == ATH9K_MODE_11NG_HT20) ||
965 (sc->sc_curmode == ATH9K_MODE_11NG_HT40PLUS) ||
966 (sc->sc_curmode == ATH9K_MODE_11NG_HT40MINUS)) {
967 u8 dot11rate = rate_table->info[rix].dot11rate;
968 u8 phy = rate_table->info[rix].phy;
970 ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
971 (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
972 series[3].rix = series[2].rix;
973 series[3].flags = series[2].flags;
974 series[3].max_4ms_framelen = series[2].max_4ms_framelen;
980 * Return the Tx rate series.
982 static void ath_rate_findrate(struct ath_softc *sc,
983 struct ath_rate_node *ath_rc_priv,
987 struct ath_rc_series series[],
991 if (!num_rates || !num_tries)
994 ath_rc_ratefind(sc, ath_rc_priv, num_tries, num_rates,
995 rcflag, series, is_probe, is_retry);
998 static void ath_rc_update_ht(struct ath_softc *sc,
999 struct ath_rate_node *ath_rc_priv,
1000 struct ath_tx_info_priv *info_priv,
1001 int tx_rate, int xretries, int retries)
1003 u32 now_msec = jiffies_to_msecs(jiffies);
1004 int state_change = FALSE, rate, count;
1006 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1007 struct ath_rate_table *rate_table =
1008 (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
1010 static u32 nretry_to_per_lookup[10] = {
1026 ASSERT(tx_rate >= 0);
1030 /* To compensate for some imbalance between ctrl and ext. channel */
1032 if (WLAN_RC_PHY_40(rate_table->info[tx_rate].phy))
1033 info_priv->tx.ts_rssi =
1034 info_priv->tx.ts_rssi < 3 ? 0 :
1035 info_priv->tx.ts_rssi - 3;
1037 last_per = ath_rc_priv->state[tx_rate].per;
1040 /* Update the PER. */
1041 if (xretries == 1) {
1042 ath_rc_priv->state[tx_rate].per += 30;
1043 if (ath_rc_priv->state[tx_rate].per > 100)
1044 ath_rc_priv->state[tx_rate].per = 100;
1047 count = ARRAY_SIZE(nretry_to_per_lookup);
1048 if (retries >= count)
1049 retries = count - 1;
1050 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
1051 ath_rc_priv->state[tx_rate].per =
1052 (u8)(ath_rc_priv->state[tx_rate].per -
1053 (ath_rc_priv->state[tx_rate].per >> 3) +
1057 /* xretries == 1 or 2 */
1059 if (ath_rc_priv->probe_rate == tx_rate)
1060 ath_rc_priv->probe_rate = 0;
1062 } else { /* xretries == 0 */
1063 /* Update the PER. */
1064 /* Make sure it doesn't index out of array's bounds. */
1065 count = ARRAY_SIZE(nretry_to_per_lookup);
1066 if (retries >= count)
1067 retries = count - 1;
1068 if (info_priv->n_bad_frames) {
1069 /* new_PER = 7/8*old_PER + 1/8*(currentPER)
1070 * Assuming that n_frames is not 0. The current PER
1071 * from the retries is 100 * retries / (retries+1),
1072 * since the first retries attempts failed, and the
1073 * next one worked. For the one that worked,
1074 * n_bad_frames subframes out of n_frames wored,
1075 * so the PER for that part is
1076 * 100 * n_bad_frames / n_frames, and it contributes
1077 * 100 * n_bad_frames / (n_frames * (retries+1)) to
1078 * the above PER. The expression below is a
1079 * simplified version of the sum of these two terms.
1081 if (info_priv->n_frames > 0)
1082 ath_rc_priv->state[tx_rate].per
1084 (ath_rc_priv->state[tx_rate].per -
1085 (ath_rc_priv->state[tx_rate].per >> 3) +
1086 ((100*(retries*info_priv->n_frames +
1087 info_priv->n_bad_frames) /
1088 (info_priv->n_frames *
1089 (retries+1))) >> 3));
1091 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
1093 ath_rc_priv->state[tx_rate].per = (u8)
1094 (ath_rc_priv->state[tx_rate].per -
1095 (ath_rc_priv->state[tx_rate].per >> 3) +
1096 (nretry_to_per_lookup[retries] >> 3));
1099 ath_rc_priv->rssi_last_prev2 = ath_rc_priv->rssi_last_prev;
1100 ath_rc_priv->rssi_last_prev = ath_rc_priv->rssi_last;
1101 ath_rc_priv->rssi_last = info_priv->tx.ts_rssi;
1102 ath_rc_priv->rssi_time = now_msec;
1105 * If we got at most one retry then increase the max rate if
1106 * this was a probe. Otherwise, ignore the probe.
1109 if (ath_rc_priv->probe_rate && ath_rc_priv->probe_rate == tx_rate) {
1110 if (retries > 0 || 2 * info_priv->n_bad_frames >
1111 info_priv->n_frames) {
1113 * Since we probed with just a single attempt,
1114 * any retries means the probe failed. Also,
1115 * if the attempt worked, but more than half
1116 * the subframes were bad then also consider
1117 * the probe a failure.
1119 ath_rc_priv->probe_rate = 0;
1123 ath_rc_priv->rate_max_phy = ath_rc_priv->probe_rate;
1124 probe_rate = ath_rc_priv->probe_rate;
1126 if (ath_rc_priv->state[probe_rate].per > 30)
1127 ath_rc_priv->state[probe_rate].per = 20;
1129 ath_rc_priv->probe_rate = 0;
1132 * Since this probe succeeded, we allow the next
1133 * probe twice as soon. This allows the maxRate
1134 * to move up faster if the probes are
1137 ath_rc_priv->probe_time = now_msec -
1138 rate_table->probe_interval / 2;
1144 * Don't update anything. We don't know if
1145 * this was because of collisions or poor signal.
1147 * Later: if rssi_ack is close to
1148 * ath_rc_priv->state[txRate].rssi_thres and we see lots
1149 * of retries, then we could increase
1150 * ath_rc_priv->state[txRate].rssi_thres.
1152 ath_rc_priv->hw_maxretry_pktcnt = 0;
1155 * It worked with no retries. First ignore bogus (small)
1158 if (tx_rate == ath_rc_priv->rate_max_phy &&
1159 ath_rc_priv->hw_maxretry_pktcnt < 255) {
1160 ath_rc_priv->hw_maxretry_pktcnt++;
1163 if (info_priv->tx.ts_rssi >=
1164 rate_table->info[tx_rate].rssi_ack_validmin) {
1165 /* Average the rssi */
1166 if (tx_rate != ath_rc_priv->rssi_sum_rate) {
1167 ath_rc_priv->rssi_sum_rate = tx_rate;
1168 ath_rc_priv->rssi_sum =
1169 ath_rc_priv->rssi_sum_cnt = 0;
1172 ath_rc_priv->rssi_sum += info_priv->tx.ts_rssi;
1173 ath_rc_priv->rssi_sum_cnt++;
1175 if (ath_rc_priv->rssi_sum_cnt > 4) {
1176 int32_t rssi_ackAvg =
1177 (ath_rc_priv->rssi_sum + 2) / 4;
1179 ath_rc_priv->state[tx_rate].
1181 int8_t rssi_ack_vmin =
1182 rate_table->info[tx_rate].
1185 ath_rc_priv->rssi_sum =
1186 ath_rc_priv->rssi_sum_cnt = 0;
1188 /* Now reduce the current
1189 * rssi threshold. */
1190 if ((rssi_ackAvg < rssi_thres + 2) &&
1191 (rssi_thres > rssi_ack_vmin)) {
1192 ath_rc_priv->state[tx_rate].
1196 state_change = TRUE;
1205 * If this rate looks bad (high PER) then stop using it for
1206 * a while (except if we are probing).
1208 if (ath_rc_priv->state[tx_rate].per >= 55 && tx_rate > 0 &&
1209 rate_table->info[tx_rate].ratekbps <=
1210 rate_table->info[ath_rc_priv->rate_max_phy].ratekbps) {
1211 ath_rc_get_nextlowervalid_txrate(rate_table, ath_rc_priv,
1212 (u8) tx_rate, &ath_rc_priv->rate_max_phy);
1214 /* Don't probe for a little while. */
1215 ath_rc_priv->probe_time = now_msec;
1220 * Make sure the rates above this have higher rssi thresholds.
1221 * (Note: Monotonicity is kept within the OFDM rates and
1222 * within the CCK rates. However, no adjustment is
1223 * made to keep the rssi thresholds monotonically
1224 * increasing between the CCK and OFDM rates.)
1226 for (rate = tx_rate; rate <
1227 ath_rc_priv->rate_table_size - 1; rate++) {
1228 if (rate_table->info[rate+1].phy !=
1229 rate_table->info[tx_rate].phy)
1232 if (ath_rc_priv->state[rate].rssi_thres +
1233 rate_table->info[rate].rssi_ack_deltamin >
1234 ath_rc_priv->state[rate+1].rssi_thres) {
1235 ath_rc_priv->state[rate+1].rssi_thres =
1236 ath_rc_priv->state[rate].
1238 rate_table->info[rate].
1243 /* Make sure the rates below this have lower rssi thresholds. */
1244 for (rate = tx_rate - 1; rate >= 0; rate--) {
1245 if (rate_table->info[rate].phy !=
1246 rate_table->info[tx_rate].phy)
1249 if (ath_rc_priv->state[rate].rssi_thres +
1250 rate_table->info[rate].rssi_ack_deltamin >
1251 ath_rc_priv->state[rate+1].rssi_thres) {
1252 if (ath_rc_priv->state[rate+1].rssi_thres <
1253 rate_table->info[rate].
1255 ath_rc_priv->state[rate].rssi_thres = 0;
1257 ath_rc_priv->state[rate].rssi_thres =
1258 ath_rc_priv->state[rate+1].
1260 rate_table->info[rate].
1264 if (ath_rc_priv->state[rate].rssi_thres <
1265 rate_table->info[rate].
1266 rssi_ack_validmin) {
1267 ath_rc_priv->state[rate].rssi_thres =
1268 rate_table->info[rate].
1275 /* Make sure the rates below this have lower PER */
1276 /* Monotonicity is kept only for rates below the current rate. */
1277 if (ath_rc_priv->state[tx_rate].per < last_per) {
1278 for (rate = tx_rate - 1; rate >= 0; rate--) {
1279 if (rate_table->info[rate].phy !=
1280 rate_table->info[tx_rate].phy)
1283 if (ath_rc_priv->state[rate].per >
1284 ath_rc_priv->state[rate+1].per) {
1285 ath_rc_priv->state[rate].per =
1286 ath_rc_priv->state[rate+1].per;
1291 /* Maintain monotonicity for rates above the current rate */
1292 for (rate = tx_rate; rate < ath_rc_priv->rate_table_size - 1; rate++) {
1293 if (ath_rc_priv->state[rate+1].per < ath_rc_priv->state[rate].per)
1294 ath_rc_priv->state[rate+1].per =
1295 ath_rc_priv->state[rate].per;
1298 /* Every so often, we reduce the thresholds and
1299 * PER (different for CCK and OFDM). */
1300 if (now_msec - ath_rc_priv->rssi_down_time >=
1301 rate_table->rssi_reduce_interval) {
1303 for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
1304 if (ath_rc_priv->state[rate].rssi_thres >
1305 rate_table->info[rate].rssi_ack_validmin)
1306 ath_rc_priv->state[rate].rssi_thres -= 1;
1308 ath_rc_priv->rssi_down_time = now_msec;
1311 /* Every so often, we reduce the thresholds
1312 * and PER (different for CCK and OFDM). */
1313 if (now_msec - ath_rc_priv->per_down_time >=
1314 rate_table->rssi_reduce_interval) {
1315 for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
1316 ath_rc_priv->state[rate].per =
1317 7 * ath_rc_priv->state[rate].per / 8;
1320 ath_rc_priv->per_down_time = now_msec;
1325 * This routine is called in rate control callback tx_status() to give
1326 * the status of previous frames.
1328 static void ath_rc_update(struct ath_softc *sc,
1329 struct ath_rate_node *ath_rc_priv,
1330 struct ath_tx_info_priv *info_priv, int final_ts_idx,
1331 int xretries, int long_retry)
1333 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1334 struct ath_rate_table *rate_table;
1335 struct ath_rc_series rcs[4];
1337 u32 series = 0, rix;
1339 memcpy(rcs, info_priv->rcs, 4 * sizeof(rcs[0]));
1340 rate_table = (struct ath_rate_table *)
1341 asc->hw_rate_table[sc->sc_curmode];
1342 ASSERT(rcs[0].tries != 0);
1345 * If the first rate is not the final index, there
1346 * are intermediate rate failures to be processed.
1348 if (final_ts_idx != 0) {
1349 /* Process intermediate rates that failed.*/
1350 for (series = 0; series < final_ts_idx ; series++) {
1351 if (rcs[series].tries != 0) {
1352 flags = rcs[series].flags;
1353 /* If HT40 and we have switched mode from
1354 * 40 to 20 => don't update */
1355 if ((flags & ATH_RC_CW40_FLAG) &&
1356 (ath_rc_priv->rc_phy_mode !=
1357 (flags & ATH_RC_CW40_FLAG)))
1359 if ((flags & ATH_RC_CW40_FLAG) &&
1360 (flags & ATH_RC_SGI_FLAG))
1361 rix = rate_table->info[
1362 rcs[series].rix].ht_index;
1363 else if (flags & ATH_RC_SGI_FLAG)
1364 rix = rate_table->info[
1365 rcs[series].rix].sgi_index;
1366 else if (flags & ATH_RC_CW40_FLAG)
1367 rix = rate_table->info[
1368 rcs[series].rix].cw40index;
1370 rix = rate_table->info[
1371 rcs[series].rix].base_index;
1372 ath_rc_update_ht(sc, ath_rc_priv,
1380 * Handle the special case of MIMO PS burst, where the second
1381 * aggregate is sent out with only one rate and one try.
1382 * Treating it as an excessive retry penalizes the rate
1385 if (rcs[0].tries == 1 && xretries == 1)
1389 flags = rcs[series].flags;
1390 /* If HT40 and we have switched mode from 40 to 20 => don't update */
1391 if ((flags & ATH_RC_CW40_FLAG) &&
1392 (ath_rc_priv->rc_phy_mode != (flags & ATH_RC_CW40_FLAG)))
1395 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_SGI_FLAG))
1396 rix = rate_table->info[rcs[series].rix].ht_index;
1397 else if (flags & ATH_RC_SGI_FLAG)
1398 rix = rate_table->info[rcs[series].rix].sgi_index;
1399 else if (flags & ATH_RC_CW40_FLAG)
1400 rix = rate_table->info[rcs[series].rix].cw40index;
1402 rix = rate_table->info[rcs[series].rix].base_index;
1404 ath_rc_update_ht(sc, ath_rc_priv, info_priv, rix,
1405 xretries, long_retry);
1409 * Process a tx descriptor for a completed transmit (success or failure).
1411 static void ath_rate_tx_complete(struct ath_softc *sc,
1412 struct ath_node *an,
1413 struct ath_rate_node *rc_priv,
1414 struct ath_tx_info_priv *info_priv)
1416 int final_ts_idx = info_priv->tx.ts_rateindex;
1417 int tx_status = 0, is_underrun = 0;
1419 if (info_priv->tx.ts_status & ATH9K_TXERR_FILT)
1422 if (info_priv->tx.ts_rssi > 0) {
1423 ATH_RSSI_LPF(an->an_chainmask_sel.tx_avgrssi,
1424 info_priv->tx.ts_rssi);
1428 * If underrun error is seen assume it as an excessive retry only
1429 * if prefetch trigger level have reached the max (0x3f for 5416)
1430 * Adjust the long retry as if the frame was tried ATH_11N_TXMAXTRY
1431 * times. This affects how ratectrl updates PER for the failed rate.
1433 if (info_priv->tx.ts_flags &
1434 (ATH9K_TX_DATA_UNDERRUN | ATH9K_TX_DELIM_UNDERRUN) &&
1435 ((sc->sc_ah->ah_txTrigLevel) >= tx_triglevel_max)) {
1440 if ((info_priv->tx.ts_status & ATH9K_TXERR_XRETRY) ||
1441 (info_priv->tx.ts_status & ATH9K_TXERR_FIFO))
1444 ath_rc_update(sc, rc_priv, info_priv, final_ts_idx, tx_status,
1445 (is_underrun) ? ATH_11N_TXMAXTRY :
1446 info_priv->tx.ts_longretry);
1450 * Update the SIB's rate control information
1452 * This should be called when the supported rates change
1453 * (e.g. SME operation, wireless mode change)
1455 * It will determine which rates are valid for use.
1457 static void ath_rc_sib_update(struct ath_softc *sc,
1458 struct ath_rate_node *ath_rc_priv,
1459 u32 capflag, int keep_state,
1460 struct ath_rateset *negotiated_rates,
1461 struct ath_rateset *negotiated_htrates)
1463 struct ath_rate_table *rate_table = NULL;
1464 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1465 struct ath_rateset *rateset = negotiated_rates;
1466 u8 *ht_mcs = (u8 *)negotiated_htrates;
1467 u8 i, j, k, hi = 0, hthi = 0;
1469 rate_table = (struct ath_rate_table *)
1470 asc->hw_rate_table[sc->sc_curmode];
1472 /* Initial rate table size. Will change depending
1473 * on the working rate set */
1474 ath_rc_priv->rate_table_size = MAX_TX_RATE_TBL;
1476 /* Initialize thresholds according to the global rate table */
1477 for (i = 0 ; (i < ath_rc_priv->rate_table_size) && (!keep_state); i++) {
1478 ath_rc_priv->state[i].rssi_thres =
1479 rate_table->info[i].rssi_ack_validmin;
1480 ath_rc_priv->state[i].per = 0;
1483 /* Determine the valid rates */
1484 ath_rc_init_valid_txmask(ath_rc_priv);
1486 for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
1487 for (j = 0; j < MAX_TX_RATE_PHY; j++)
1488 ath_rc_priv->valid_phy_rateidx[i][j] = 0;
1489 ath_rc_priv->valid_phy_ratecnt[i] = 0;
1491 ath_rc_priv->rc_phy_mode = (capflag & WLAN_RC_40_FLAG);
1493 /* Set stream capability */
1494 ath_rc_priv->single_stream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
1496 if (!rateset->rs_nrates) {
1497 /* No working rate, just initialize valid rates */
1498 hi = ath_rc_sib_init_validrates(ath_rc_priv, rate_table,
1501 /* Use intersection of working rates and valid rates */
1502 hi = ath_rc_sib_setvalid_rates(ath_rc_priv, rate_table,
1504 if (capflag & WLAN_RC_HT_FLAG) {
1505 hthi = ath_rc_sib_setvalid_htrates(ath_rc_priv,
1510 hi = A_MAX(hi, hthi);
1513 ath_rc_priv->rate_table_size = hi + 1;
1514 ath_rc_priv->rate_max_phy = 0;
1515 ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
1517 for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
1518 for (j = 0; j < ath_rc_priv->valid_phy_ratecnt[i]; j++) {
1519 ath_rc_priv->valid_rate_index[k++] =
1520 ath_rc_priv->valid_phy_rateidx[i][j];
1523 if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, TRUE)
1524 || !ath_rc_priv->valid_phy_ratecnt[i])
1527 ath_rc_priv->rate_max_phy = ath_rc_priv->valid_phy_rateidx[i][j-1];
1529 ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
1530 ASSERT(k <= MAX_TX_RATE_TBL);
1532 ath_rc_priv->max_valid_rate = k;
1534 * Some third party vendors don't send the supported rate series in
1535 * order. So sorting to make sure its in order, otherwise our RateFind
1536 * Algo will select wrong rates
1538 ath_rc_sort_validrates(rate_table, ath_rc_priv);
1539 ath_rc_priv->rate_max_phy = ath_rc_priv->valid_rate_index[k-4];
1542 void ath_rc_node_update(struct ieee80211_hw *hw, struct ath_rate_node *rc_priv)
1544 struct ath_softc *sc = hw->priv;
1547 if (hw->conf.ht.enabled) {
1548 capflag |= ATH_RC_HT_FLAG | ATH_RC_DS_FLAG;
1549 if (sc->sc_ht_info.tx_chan_width == ATH9K_HT_MACMODE_2040)
1550 capflag |= ATH_RC_CW40_FLAG;
1554 ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
1555 ((capflag & ATH_RC_SGI_FLAG) ? WLAN_RC_SGI_FLAG : 0) |
1556 ((capflag & ATH_RC_HT_FLAG) ? WLAN_RC_HT_FLAG : 0) |
1557 ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0);
1560 ath_rc_sib_update(sc, rc_priv, rc_priv->ht_cap, 0,
1561 &rc_priv->neg_rates,
1562 &rc_priv->neg_ht_rates);
1565 /* Rate Control callbacks */
1566 static void ath_tx_status(void *priv, struct ieee80211_supported_band *sband,
1567 struct ieee80211_sta *sta, void *priv_sta,
1568 struct sk_buff *skb)
1570 struct ath_softc *sc = priv;
1571 struct ath_tx_info_priv *tx_info_priv;
1572 struct ath_node *an;
1573 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1574 struct ieee80211_hdr *hdr;
1577 hdr = (struct ieee80211_hdr *)skb->data;
1578 fc = hdr->frame_control;
1579 /* XXX: UGLY HACK!! */
1580 tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
1582 an = (struct ath_node *)sta->drv_priv;
1584 if (tx_info_priv == NULL)
1587 if (an && priv_sta && ieee80211_is_data(fc))
1588 ath_rate_tx_complete(sc, an, priv_sta, tx_info_priv);
1590 kfree(tx_info_priv);
1591 tx_info->control.vif = NULL;
1594 static void ath_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1595 struct ieee80211_tx_rate_control *txrc)
1597 struct ieee80211_supported_band *sband = txrc->sband;
1598 struct sk_buff *skb = txrc->skb;
1599 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1600 struct ath_softc *sc = priv;
1601 struct ieee80211_hw *hw = sc->hw;
1602 struct ath_tx_info_priv *tx_info_priv;
1603 struct ath_rate_node *ath_rc_priv = priv_sta;
1604 struct ath_node *an;
1605 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1606 int is_probe = FALSE;
1608 __le16 fc = hdr->frame_control;
1611 DPRINTF(sc, ATH_DBG_RATE, "%s\n", __func__);
1613 /* allocate driver private area of tx_info, XXX: UGLY HACK! */
1614 tx_info->control.vif = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1615 tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
1616 ASSERT(tx_info_priv != NULL);
1618 lowest_idx = rate_lowest_index(sband, sta);
1619 tx_info_priv->min_rate = (sband->bitrates[lowest_idx].bitrate * 2) / 10;
1620 /* lowest rate for management and multicast/broadcast frames */
1621 if (!ieee80211_is_data(fc) ||
1622 is_multicast_ether_addr(hdr->addr1) || !sta) {
1623 tx_info->control.rates[0].idx = lowest_idx;
1627 /* Find tx rate for unicast frames */
1628 ath_rate_findrate(sc, ath_rc_priv,
1629 ATH_11N_TXMAXTRY, 4,
1630 ATH_RC_PROBE_ALLOWED,
1636 sel->probe_idx = ath_rc_priv->tx_ratectrl.probe_rate;
1639 /* Ratecontrol sometimes returns invalid rate index */
1640 if (tx_info_priv->rcs[0].rix != 0xff)
1641 ath_rc_priv->prev_data_rix = tx_info_priv->rcs[0].rix;
1643 tx_info_priv->rcs[0].rix = ath_rc_priv->prev_data_rix;
1645 tx_info->control.rates[0].idx = tx_info_priv->rcs[0].rix;
1647 /* Check if aggregation has to be enabled for this tid */
1649 if (hw->conf.ht.enabled) {
1650 if (ieee80211_is_data_qos(fc)) {
1651 qc = ieee80211_get_qos_ctl(hdr);
1653 an = (struct ath_node *)sta->drv_priv;
1655 if(ath_tx_aggr_check(sc, an, tid))
1656 ieee80211_start_tx_ba_session(hw, hdr->addr1, tid);
1661 static void ath_rate_init(void *priv, struct ieee80211_supported_band *sband,
1662 struct ieee80211_sta *sta, void *priv_sta)
1664 struct ath_softc *sc = priv;
1665 struct ath_rate_node *ath_rc_priv = priv_sta;
1668 for (i = 0; i < sband->n_bitrates; i++) {
1669 if (sta->supp_rates[sband->band] & BIT(i)) {
1670 ath_rc_priv->neg_rates.rs_rates[j]
1671 = (sband->bitrates[i].bitrate * 2) / 10;
1675 ath_rc_priv->neg_rates.rs_nrates = j;
1677 if (sta->ht_cap.ht_supported) {
1678 for (i = 0, j = 0; i < 77; i++) {
1679 if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
1680 ath_rc_priv->neg_ht_rates.rs_rates[j++] = i;
1681 if (j == ATH_RATE_MAX)
1684 ath_rc_priv->neg_ht_rates.rs_nrates = j;
1687 ath_rc_node_update(sc->hw, priv_sta);
1690 static void *ath_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
1695 static void ath_rate_free(void *priv)
1700 static void *ath_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
1702 struct ieee80211_vif *vif;
1703 struct ath_softc *sc = priv;
1704 struct ath_vap *avp;
1705 struct ath_rate_node *rate_priv;
1707 vif = sc->sc_vaps[0];
1710 avp = (void *)vif->drv_priv;
1712 rate_priv = kzalloc(sizeof(struct ath_rate_node), gfp);
1714 DPRINTF(sc, ATH_DBG_FATAL,
1715 "%s: Unable to allocate private rc structure\n",
1720 rate_priv->avp = avp;
1721 rate_priv->asc = sc->sc_rc;
1722 avp->rc_node = rate_priv;
1723 rate_priv->rssi_down_time = jiffies_to_msecs(jiffies);
1728 static void ath_rate_free_sta(void *priv, struct ieee80211_sta *sta,
1731 struct ath_rate_node *rate_priv = priv_sta;
1736 static struct rate_control_ops ath_rate_ops = {
1738 .name = "ath9k_rate_control",
1739 .tx_status = ath_tx_status,
1740 .get_rate = ath_get_rate,
1741 .rate_init = ath_rate_init,
1742 .alloc = ath_rate_alloc,
1743 .free = ath_rate_free,
1744 .alloc_sta = ath_rate_alloc_sta,
1745 .free_sta = ath_rate_free_sta,
1748 int ath_rate_control_register(void)
1750 return ieee80211_rate_control_register(&ath_rate_ops);
1753 void ath_rate_control_unregister(void)
1755 ieee80211_rate_control_unregister(&ath_rate_ops);