add the new ath9k driver (loads successfully on an AR9160 card, but still seems to...
[openwrt/svn-archive/archive.git] / package / ath9k / src / drivers / net / wireless / ath9k / rc.c
1 /*
2 * Copyright (c) 2004 Video54 Technologies, Inc.
3 * Copyright (c) 2004-2008 Atheros Communications, Inc.
4 *
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.
8 *
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.
16 */
17
18 /*
19 * Atheros rate control algorithm
20 */
21
22 #include "core.h"
23 #include "../net/mac80211/rate.h"
24
25 static u_int32_t tx_triglevel_max;
26
27 static struct ath_rate_table ar5416_11na_ratetable = {
28 42,
29 {
30 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
31 5400, 0x0b, 0x00, 12,
32 0, 2, 1, 0, 0, 0, 0, 0 },
33 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
34 7800, 0x0f, 0x00, 18,
35 0, 3, 1, 1, 1, 1, 1, 0 },
36 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
37 10000, 0x0a, 0x00, 24,
38 2, 4, 2, 2, 2, 2, 2, 0 },
39 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
40 13900, 0x0e, 0x00, 36,
41 2, 6, 2, 3, 3, 3, 3, 0 },
42 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
43 17300, 0x09, 0x00, 48,
44 4, 10, 3, 4, 4, 4, 4, 0 },
45 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
46 23000, 0x0d, 0x00, 72,
47 4, 14, 3, 5, 5, 5, 5, 0 },
48 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
49 27400, 0x08, 0x00, 96,
50 4, 20, 3, 6, 6, 6, 6, 0 },
51 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
52 29300, 0x0c, 0x00, 108,
53 4, 23, 3, 7, 7, 7, 7, 0 },
54 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 6500, /* 6.5 Mb */
55 6400, 0x80, 0x00, 0,
56 0, 2, 3, 8, 24, 8, 24, 3216 },
57 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 13000, /* 13 Mb */
58 12700, 0x81, 0x00, 1,
59 2, 4, 3, 9, 25, 9, 25, 6434 },
60 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 19500, /* 19.5 Mb */
61 18800, 0x82, 0x00, 2,
62 2, 6, 3, 10, 26, 10, 26, 9650 },
63 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 26000, /* 26 Mb */
64 25000, 0x83, 0x00, 3,
65 4, 10, 3, 11, 27, 11, 27, 12868 },
66 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 39000, /* 39 Mb */
67 36700, 0x84, 0x00, 4,
68 4, 14, 3, 12, 28, 12, 28, 19304 },
69 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 52000, /* 52 Mb */
70 48100, 0x85, 0x00, 5,
71 4, 20, 3, 13, 29, 13, 29, 25740 },
72 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 58500, /* 58.5 Mb */
73 53500, 0x86, 0x00, 6,
74 4, 23, 3, 14, 30, 14, 30, 28956 },
75 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 65000, /* 65 Mb */
76 59000, 0x87, 0x00, 7,
77 4, 25, 3, 15, 31, 15, 32, 32180 },
78 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 13000, /* 13 Mb */
79 12700, 0x88, 0x00,
80 8, 0, 2, 3, 16, 33, 16, 33, 6430 },
81 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 26000, /* 26 Mb */
82 24800, 0x89, 0x00, 9,
83 2, 4, 3, 17, 34, 17, 34, 12860 },
84 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 39000, /* 39 Mb */
85 36600, 0x8a, 0x00, 10,
86 2, 6, 3, 18, 35, 18, 35, 19300 },
87 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 52000, /* 52 Mb */
88 48100, 0x8b, 0x00, 11,
89 4, 10, 3, 19, 36, 19, 36, 25736 },
90 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 78000, /* 78 Mb */
91 69500, 0x8c, 0x00, 12,
92 4, 14, 3, 20, 37, 20, 37, 38600 },
93 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 104000, /* 104 Mb */
94 89500, 0x8d, 0x00, 13,
95 4, 20, 3, 21, 38, 21, 38, 51472 },
96 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 117000, /* 117 Mb */
97 98900, 0x8e, 0x00, 14,
98 4, 23, 3, 22, 39, 22, 39, 57890 },
99 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 130000, /* 130 Mb */
100 108300, 0x8f, 0x00, 15,
101 4, 25, 3, 23, 40, 23, 41, 64320 },
102 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 13500, /* 13.5 Mb */
103 13200, 0x80, 0x00, 0,
104 0, 2, 3, 8, 24, 24, 24, 6684 },
105 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 27500, /* 27.0 Mb */
106 25900, 0x81, 0x00, 1,
107 2, 4, 3, 9, 25, 25, 25, 13368 },
108 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 40500, /* 40.5 Mb */
109 38600, 0x82, 0x00, 2,
110 2, 6, 3, 10, 26, 26, 26, 20052 },
111 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 54000, /* 54 Mb */
112 49800, 0x83, 0x00, 3,
113 4, 10, 3, 11, 27, 27, 27, 26738 },
114 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 81500, /* 81 Mb */
115 72200, 0x84, 0x00, 4,
116 4, 14, 3, 12, 28, 28, 28, 40104 },
117 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 108000, /* 108 Mb */
118 92900, 0x85, 0x00, 5,
119 4, 20, 3, 13, 29, 29, 29, 53476 },
120 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 121500, /* 121.5 Mb */
121 102700, 0x86, 0x00, 6,
122 4, 23, 3, 14, 30, 30, 30, 60156 },
123 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 135000, /* 135 Mb */
124 112000, 0x87, 0x00, 7,
125 4, 25, 3, 15, 31, 32, 32, 66840 },
126 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
127 122000, 0x87, 0x00, 7,
128 4, 25, 3, 15, 31, 32, 32, 74200 },
129 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 27000, /* 27 Mb */
130 25800, 0x88, 0x00, 8,
131 0, 2, 3, 16, 33, 33, 33, 13360 },
132 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 54000, /* 54 Mb */
133 49800, 0x89, 0x00, 9,
134 2, 4, 3, 17, 34, 34, 34, 26720 },
135 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 81000, /* 81 Mb */
136 71900, 0x8a, 0x00, 10,
137 2, 6, 3, 18, 35, 35, 35, 40080 },
138 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 108000, /* 108 Mb */
139 92500, 0x8b, 0x00, 11,
140 4, 10, 3, 19, 36, 36, 36, 53440 },
141 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 162000, /* 162 Mb */
142 130300, 0x8c, 0x00, 12,
143 4, 14, 3, 20, 37, 37, 37, 80160 },
144 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 216000, /* 216 Mb */
145 162800, 0x8d, 0x00, 13,
146 4, 20, 3, 21, 38, 38, 38, 106880 },
147 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 243000, /* 243 Mb */
148 178200, 0x8e, 0x00, 14,
149 4, 23, 3, 22, 39, 39, 39, 120240 },
150 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 270000, /* 270 Mb */
151 192100, 0x8f, 0x00, 15,
152 4, 25, 3, 23, 40, 41, 41, 133600 },
153 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
154 207000, 0x8f, 0x00, 15,
155 4, 25, 3, 23, 40, 41, 41, 148400 },
156 },
157 50, /* probe interval */
158 50, /* rssi reduce interval */
159 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
160 };
161
162 /* TRUE_ALL - valid for 20/40/Legacy,
163 * TRUE - Legacy only,
164 * TRUE_20 - HT 20 only,
165 * TRUE_40 - HT 40 only */
166
167 /* 4ms frame limit not used for NG mode. The values filled
168 * for HT are the 64K max aggregate limit */
169
170 static struct ath_rate_table ar5416_11ng_ratetable = {
171 46,
172 {
173 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 1000, /* 1 Mb */
174 900, 0x1b, 0x00, 2,
175 0, 0, 1, 0, 0, 0, 0, 0 },
176 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 2000, /* 2 Mb */
177 1900, 0x1a, 0x04, 4,
178 1, 1, 1, 1, 1, 1, 1, 0 },
179 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
180 4900, 0x19, 0x04, 11,
181 2, 2, 2, 2, 2, 2, 2, 0 },
182 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 11000, /* 11 Mb */
183 8100, 0x18, 0x04, 22,
184 3, 3, 2, 3, 3, 3, 3, 0 },
185 { FALSE, FALSE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
186 5400, 0x0b, 0x00, 12,
187 4, 2, 1, 4, 4, 4, 4, 0 },
188 { FALSE, FALSE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
189 7800, 0x0f, 0x00, 18,
190 4, 3, 1, 5, 5, 5, 5, 0 },
191 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
192 10100, 0x0a, 0x00, 24,
193 6, 4, 1, 6, 6, 6, 6, 0 },
194 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
195 14100, 0x0e, 0x00, 36,
196 6, 6, 2, 7, 7, 7, 7, 0 },
197 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
198 17700, 0x09, 0x00, 48,
199 8, 10, 3, 8, 8, 8, 8, 0 },
200 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
201 23700, 0x0d, 0x00, 72,
202 8, 14, 3, 9, 9, 9, 9, 0 },
203 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
204 27400, 0x08, 0x00, 96,
205 8, 20, 3, 10, 10, 10, 10, 0 },
206 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
207 30900, 0x0c, 0x00, 108,
208 8, 23, 3, 11, 11, 11, 11, 0 },
209 { FALSE, FALSE, WLAN_PHY_HT_20_SS, 6500, /* 6.5 Mb */
210 6400, 0x80, 0x00, 0,
211 4, 2, 3, 12, 28, 12, 28, 3216 },
212 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 13000, /* 13 Mb */
213 12700, 0x81, 0x00, 1,
214 6, 4, 3, 13, 29, 13, 29, 6434 },
215 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 19500, /* 19.5 Mb */
216 18800, 0x82, 0x00, 2,
217 6, 6, 3, 14, 30, 14, 30, 9650 },
218 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 26000, /* 26 Mb */
219 25000, 0x83, 0x00, 3,
220 8, 10, 3, 15, 31, 15, 31, 12868 },
221 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 39000, /* 39 Mb */
222 36700, 0x84, 0x00, 4,
223 8, 14, 3, 16, 32, 16, 32, 19304 },
224 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 52000, /* 52 Mb */
225 48100, 0x85, 0x00, 5,
226 8, 20, 3, 17, 33, 17, 33, 25740 },
227 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 58500, /* 58.5 Mb */
228 53500, 0x86, 0x00, 6,
229 8, 23, 3, 18, 34, 18, 34, 28956 },
230 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 65000, /* 65 Mb */
231 59000, 0x87, 0x00, 7,
232 8, 25, 3, 19, 35, 19, 36, 32180 },
233 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 13000, /* 13 Mb */
234 12700, 0x88, 0x00, 8,
235 4, 2, 3, 20, 37, 20, 37, 6430 },
236 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 26000, /* 26 Mb */
237 24800, 0x89, 0x00, 9,
238 6, 4, 3, 21, 38, 21, 38, 12860 },
239 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 39000, /* 39 Mb */
240 36600, 0x8a, 0x00, 10,
241 6, 6, 3, 22, 39, 22, 39, 19300 },
242 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 52000, /* 52 Mb */
243 48100, 0x8b, 0x00, 11,
244 8, 10, 3, 23, 40, 23, 40, 25736 },
245 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 78000, /* 78 Mb */
246 69500, 0x8c, 0x00, 12,
247 8, 14, 3, 24, 41, 24, 41, 38600 },
248 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 104000, /* 104 Mb */
249 89500, 0x8d, 0x00, 13,
250 8, 20, 3, 25, 42, 25, 42, 51472 },
251 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 117000, /* 117 Mb */
252 98900, 0x8e, 0x00, 14,
253 8, 23, 3, 26, 43, 26, 44, 57890 },
254 { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 130000, /* 130 Mb */
255 108300, 0x8f, 0x00, 15,
256 8, 25, 3, 27, 44, 27, 45, 64320 },
257 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 13500, /* 13.5 Mb */
258 13200, 0x80, 0x00, 0,
259 8, 2, 3, 12, 28, 28, 28, 6684 },
260 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 27500, /* 27.0 Mb */
261 25900, 0x81, 0x00, 1,
262 8, 4, 3, 13, 29, 29, 29, 13368 },
263 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 40500, /* 40.5 Mb */
264 38600, 0x82, 0x00, 2,
265 8, 6, 3, 14, 30, 30, 30, 20052 },
266 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 54000, /* 54 Mb */
267 49800, 0x83, 0x00, 3,
268 8, 10, 3, 15, 31, 31, 31, 26738 },
269 { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 81500, /* 81 Mb */
270 72200, 0x84, 0x00, 4,
271 8, 14, 3, 16, 32, 32, 32, 40104 },
272 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 108000, /* 108 Mb */
273 92900, 0x85, 0x00, 5,
274 8, 20, 3, 17, 33, 33, 33, 53476 },
275 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 121500, /* 121.5 Mb */
276 102700, 0x86, 0x00, 6,
277 8, 23, 3, 18, 34, 34, 34, 60156 },
278 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 135000, /* 135 Mb */
279 112000, 0x87, 0x00, 7,
280 8, 23, 3, 19, 35, 36, 36, 66840 },
281 { FALSE, TRUE_40, WLAN_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
282 122000, 0x87, 0x00, 7,
283 8, 25, 3, 19, 35, 36, 36, 74200 },
284 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 27000, /* 27 Mb */
285 25800, 0x88, 0x00, 8,
286 8, 2, 3, 20, 37, 37, 37, 13360 },
287 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 54000, /* 54 Mb */
288 49800, 0x89, 0x00, 9,
289 8, 4, 3, 21, 38, 38, 38, 26720 },
290 { FALSE, FALSE, WLAN_PHY_HT_40_DS, 81000, /* 81 Mb */
291 71900, 0x8a, 0x00, 10,
292 8, 6, 3, 22, 39, 39, 39, 40080 },
293 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 108000, /* 108 Mb */
294 92500, 0x8b, 0x00, 11,
295 8, 10, 3, 23, 40, 40, 40, 53440 },
296 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 162000, /* 162 Mb */
297 130300, 0x8c, 0x00, 12,
298 8, 14, 3, 24, 41, 41, 41, 80160 },
299 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 216000, /* 216 Mb */
300 162800, 0x8d, 0x00, 13,
301 8, 20, 3, 25, 42, 42, 42, 106880 },
302 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 243000, /* 243 Mb */
303 178200, 0x8e, 0x00, 14,
304 8, 23, 3, 26, 43, 43, 43, 120240 },
305 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 270000, /* 270 Mb */
306 192100, 0x8f, 0x00, 15,
307 8, 23, 3, 27, 44, 45, 45, 133600 },
308 { TRUE_40, FALSE, WLAN_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
309 207000, 0x8f, 0x00, 15,
310 8, 25, 3, 27, 44, 45, 45, 148400 },
311 },
312 50, /* probe interval */
313 50, /* rssi reduce interval */
314 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
315 };
316
317 static struct ath_rate_table ar5416_11a_ratetable = {
318 8,
319 {
320 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
321 5400, 0x0b, 0x00, (0x80|12),
322 0, 2, 1, 0, 0 },
323 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
324 7800, 0x0f, 0x00, 18,
325 0, 3, 1, 1, 0 },
326 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
327 10000, 0x0a, 0x00, (0x80|24),
328 2, 4, 2, 2, 0 },
329 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
330 13900, 0x0e, 0x00, 36,
331 2, 6, 2, 3, 0 },
332 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
333 17300, 0x09, 0x00, (0x80|48),
334 4, 10, 3, 4, 0 },
335 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
336 23000, 0x0d, 0x00, 72,
337 4, 14, 3, 5, 0 },
338 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
339 27400, 0x08, 0x00, 96,
340 4, 19, 3, 6, 0 },
341 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
342 29300, 0x0c, 0x00, 108,
343 4, 23, 3, 7, 0 },
344 },
345 50, /* probe interval */
346 50, /* rssi reduce interval */
347 0, /* Phy rates allowed initially */
348 };
349
350 static struct ath_rate_table ar5416_11a_ratetable_Half = {
351 8,
352 {
353 { TRUE, TRUE, WLAN_PHY_OFDM, 3000, /* 6 Mb */
354 2700, 0x0b, 0x00, (0x80|6),
355 0, 2, 1, 0, 0},
356 { TRUE, TRUE, WLAN_PHY_OFDM, 4500, /* 9 Mb */
357 3900, 0x0f, 0x00, 9,
358 0, 3, 1, 1, 0 },
359 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 12 Mb */
360 5000, 0x0a, 0x00, (0x80|12),
361 2, 4, 2, 2, 0 },
362 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 18 Mb */
363 6950, 0x0e, 0x00, 18,
364 2, 6, 2, 3, 0 },
365 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 24 Mb */
366 8650, 0x09, 0x00, (0x80|24),
367 4, 10, 3, 4, 0 },
368 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 36 Mb */
369 11500, 0x0d, 0x00, 36,
370 4, 14, 3, 5, 0 },
371 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 48 Mb */
372 13700, 0x08, 0x00, 48,
373 4, 19, 3, 6, 0 },
374 { TRUE, TRUE, WLAN_PHY_OFDM, 27000, /* 54 Mb */
375 14650, 0x0c, 0x00, 54,
376 4, 23, 3, 7, 0 },
377 },
378 50, /* probe interval */
379 50, /* rssi reduce interval */
380 0, /* Phy rates allowed initially */
381 };
382
383 static struct ath_rate_table ar5416_11a_ratetable_Quarter = {
384 8,
385 {
386 { TRUE, TRUE, WLAN_PHY_OFDM, 1500, /* 6 Mb */
387 1350, 0x0b, 0x00, (0x80|3),
388 0, 2, 1, 0, 0 },
389 { TRUE, TRUE, WLAN_PHY_OFDM, 2250, /* 9 Mb */
390 1950, 0x0f, 0x00, 4,
391 0, 3, 1, 1, 0 },
392 { TRUE, TRUE, WLAN_PHY_OFDM, 3000, /* 12 Mb */
393 2500, 0x0a, 0x00, (0x80|6),
394 2, 4, 2, 2, 0 },
395 { TRUE, TRUE, WLAN_PHY_OFDM, 4500, /* 18 Mb */
396 3475, 0x0e, 0x00, 9,
397 2, 6, 2, 3, 0 },
398 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 25 Mb */
399 4325, 0x09, 0x00, (0x80|12),
400 4, 10, 3, 4, 0 },
401 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 36 Mb */
402 5750, 0x0d, 0x00, 18,
403 4, 14, 3, 5, 0 },
404 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 48 Mb */
405 6850, 0x08, 0x00, 24,
406 4, 19, 3, 6, 0 },
407 { TRUE, TRUE, WLAN_PHY_OFDM, 13500, /* 54 Mb */
408 7325, 0x0c, 0x00, 27,
409 4, 23, 3, 7, 0 },
410 },
411 50, /* probe interval */
412 50, /* rssi reduce interval */
413 0, /* Phy rates allowed initially */
414 };
415
416 static struct ath_rate_table ar5416_11g_ratetable = {
417 12,
418 {
419 { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
420 900, 0x1b, 0x00, 2,
421 0, 0, 1, 0, 0 },
422 { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
423 1900, 0x1a, 0x04, 4,
424 1, 1, 1, 1, 0 },
425 { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
426 4900, 0x19, 0x04, 11,
427 2, 2, 2, 2, 0 },
428 { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
429 8100, 0x18, 0x04, 22,
430 3, 3, 2, 3, 0 },
431 { FALSE, FALSE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
432 5400, 0x0b, 0x00, 12,
433 4, 2, 1, 4, 0 },
434 { FALSE, FALSE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
435 7800, 0x0f, 0x00, 18,
436 4, 3, 1, 5, 0 },
437 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
438 10000, 0x0a, 0x00, 24,
439 6, 4, 1, 6, 0 },
440 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
441 13900, 0x0e, 0x00, 36,
442 6, 6, 2, 7, 0 },
443 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
444 17300, 0x09, 0x00, 48,
445 8, 10, 3, 8, 0 },
446 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
447 23000, 0x0d, 0x00, 72,
448 8, 14, 3, 9, 0 },
449 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
450 27400, 0x08, 0x00, 96,
451 8, 19, 3, 10, 0 },
452 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
453 29300, 0x0c, 0x00, 108,
454 8, 23, 3, 11, 0 },
455 },
456 50, /* probe interval */
457 50, /* rssi reduce interval */
458 0, /* Phy rates allowed initially */
459 };
460
461 static struct ath_rate_table ar5416_11b_ratetable = {
462 4,
463 {
464 { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
465 900, 0x1b, 0x00, (0x80|2),
466 0, 0, 1, 0, 0 },
467 { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
468 1800, 0x1a, 0x04, (0x80|4),
469 1, 1, 1, 1, 0 },
470 { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
471 4300, 0x19, 0x04, (0x80|11),
472 1, 2, 2, 2, 0 },
473 { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
474 7100, 0x18, 0x04, (0x80|22),
475 1, 4, 100, 3, 0 },
476 },
477 100, /* probe interval */
478 100, /* rssi reduce interval */
479 0, /* Phy rates allowed initially */
480 };
481
482 static void ar5416_attach_ratetables(struct ath_rate_softc *sc)
483 {
484 /*
485 * Attach rate tables.
486 */
487 sc->hw_rate_table[WIRELESS_MODE_11b] = &ar5416_11b_ratetable;
488 sc->hw_rate_table[WIRELESS_MODE_11a] = &ar5416_11a_ratetable;
489 sc->hw_rate_table[WIRELESS_MODE_11g] = &ar5416_11g_ratetable;
490
491 sc->hw_rate_table[WIRELESS_MODE_11NA_HT20] = &ar5416_11na_ratetable;
492 sc->hw_rate_table[WIRELESS_MODE_11NG_HT20] = &ar5416_11ng_ratetable;
493 sc->hw_rate_table[WIRELESS_MODE_11NA_HT40PLUS] =
494 &ar5416_11na_ratetable;
495 sc->hw_rate_table[WIRELESS_MODE_11NA_HT40MINUS] =
496 &ar5416_11na_ratetable;
497 sc->hw_rate_table[WIRELESS_MODE_11NG_HT40PLUS] =
498 &ar5416_11ng_ratetable;
499 sc->hw_rate_table[WIRELESS_MODE_11NG_HT40MINUS] =
500 &ar5416_11ng_ratetable;
501 }
502
503 static void ar5416_setquarter_ratetable(struct ath_rate_softc *sc)
504 {
505 sc->hw_rate_table[WIRELESS_MODE_11a] = &ar5416_11a_ratetable_Quarter;
506 return;
507 }
508
509 static void ar5416_sethalf_ratetable(struct ath_rate_softc *sc)
510 {
511 sc->hw_rate_table[WIRELESS_MODE_11a] = &ar5416_11a_ratetable_Half;
512 return;
513 }
514
515 static void ar5416_setfull_ratetable(struct ath_rate_softc *sc)
516 {
517 sc->hw_rate_table[WIRELESS_MODE_11a] = &ar5416_11a_ratetable;
518 return;
519 }
520
521 /*
522 * Return the median of three numbers
523 */
524 static inline int8_t median(int8_t a, int8_t b, int8_t c)
525 {
526 if (a >= b) {
527 if (b >= c)
528 return b;
529 else if (a > c)
530 return c;
531 else
532 return a;
533 } else {
534 if (a >= c)
535 return a;
536 else if (b >= c)
537 return c;
538 else
539 return b;
540 }
541 }
542
543 static void ath_rc_sort_validrates(const struct ath_rate_table *rate_table,
544 struct ath_tx_ratectrl *rate_ctrl)
545 {
546 u_int8_t i, j, idx, idx_next;
547
548 for (i = rate_ctrl->max_valid_rate - 1; i > 0; i--) {
549 for (j = 0; j <= i-1; j++) {
550
551 idx = rate_ctrl->valid_rate_index[j];
552 idx_next = rate_ctrl->valid_rate_index[j+1];
553
554 if (rate_table->info[idx].ratekbps >
555 rate_table->info[idx_next].ratekbps) {
556
557 rate_ctrl->valid_rate_index[j] = idx_next;
558 rate_ctrl->valid_rate_index[j+1] = idx;
559 }
560 }
561 }
562 }
563
564 /* Access functions for valid_txrate_mask */
565
566 static void ath_rc_init_valid_txmask(struct ath_tx_ratectrl *rate_ctrl)
567 {
568 u_int8_t i;
569
570 for (i = 0; i < rate_ctrl->rate_table_size; i++)
571 rate_ctrl->valid_rate_index[i] = FALSE;
572 }
573
574 static inline void ath_rc_set_valid_txmask(struct ath_tx_ratectrl *rate_ctrl,
575 u_int8_t index, int valid_tx_rate)
576 {
577 ASSERT(index <= rate_ctrl->rate_table_size);
578 rate_ctrl->valid_rate_index[index] = valid_tx_rate ? TRUE : FALSE;
579 }
580
581 static inline int ath_rc_isvalid_txmask(struct ath_tx_ratectrl *rate_ctrl,
582 u_int8_t index)
583 {
584 ASSERT(index <= rate_ctrl->rate_table_size);
585 return rate_ctrl->valid_rate_index[index];
586 }
587
588 /* Iterators for valid_txrate_mask */
589 static inline int ath_rc_get_nextvalid_txrate(
590 const struct ath_rate_table *rate_table,
591 struct ath_tx_ratectrl *rate_ctrl,
592 u_int8_t cur_valid_txrate,
593 u_int8_t *next_idx)
594 {
595 u_int8_t i;
596
597 for (i = 0; i < rate_ctrl->max_valid_rate - 1; i++) {
598 if (rate_ctrl->valid_rate_index[i] == cur_valid_txrate) {
599 *next_idx = rate_ctrl->valid_rate_index[i+1];
600 return TRUE;
601 }
602 }
603
604 /* No more valid rates */
605 *next_idx = 0;
606 return FALSE;
607 }
608
609 /* Return true only for single stream */
610
611 static int ath_rc_valid_phyrate(u_int32_t phy, u_int32_t capflag, int ignore_cw)
612 {
613 if (WLAN_RC_PHY_HT(phy) & !(capflag & WLAN_RC_HT_FLAG))
614 return FALSE;
615 if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))
616 return FALSE;
617 if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_SGI_FLAG))
618 return FALSE;
619 if (!ignore_cw && WLAN_RC_PHY_HT(phy))
620 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG))
621 return FALSE;
622 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG))
623 return FALSE;
624 return TRUE;
625 }
626
627 static inline int ath_rc_get_nextlowervalid_txrate(
628 const struct ath_rate_table *rate_table,
629 struct ath_tx_ratectrl *rate_ctrl,
630 u_int8_t cur_valid_txrate, u_int8_t *next_idx)
631 {
632 int8_t i;
633
634 for (i = 1; i < rate_ctrl->max_valid_rate ; i++) {
635 if (rate_ctrl->valid_rate_index[i] == cur_valid_txrate) {
636 *next_idx = rate_ctrl->valid_rate_index[i-1];
637 return TRUE;
638 }
639 }
640 return FALSE;
641 }
642
643 /*
644 * Initialize the Valid Rate Index from valid entries in Rate Table
645 */
646 static u_int8_t ath_rc_sib_init_validrates(struct ath_rate_node *ath_rc_priv,
647 const struct ath_rate_table *rate_table,
648 u_int32_t capflag)
649 {
650 struct ath_tx_ratectrl *rate_ctrl;
651 u_int8_t i, hi = 0;
652 u_int32_t valid;
653
654 rate_ctrl = (struct ath_tx_ratectrl *)(ath_rc_priv);
655 for (i = 0; i < rate_table->rate_cnt; i++) {
656 valid = (ath_rc_priv->single_stream ?
657 rate_table->info[i].valid_single_stream :
658 rate_table->info[i].valid);
659 if (valid == TRUE) {
660 u_int32_t phy = rate_table->info[i].phy;
661 u_int8_t valid_rate_count = 0;
662
663 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
664 continue;
665
666 valid_rate_count = rate_ctrl->valid_phy_ratecnt[phy];
667
668 rate_ctrl->valid_phy_rateidx[phy][valid_rate_count] = i;
669 rate_ctrl->valid_phy_ratecnt[phy] += 1;
670 ath_rc_set_valid_txmask(rate_ctrl, i, TRUE);
671 hi = A_MAX(hi, i);
672 }
673 }
674 return hi;
675 }
676
677 /*
678 * Initialize the Valid Rate Index from Rate Set
679 */
680 static u_int8_t ath_rc_sib_setvalid_rates(struct ath_rate_node *ath_rc_priv,
681 const struct ath_rate_table *rate_table,
682 struct ath_rateset *rateset,
683 u_int32_t capflag)
684 {
685 /* XXX: Clean me up and make identation friendly */
686 u_int8_t i, j, hi = 0;
687 struct ath_tx_ratectrl *rate_ctrl =
688 (struct ath_tx_ratectrl *)(ath_rc_priv);
689
690 /* Use intersection of working rates and valid rates */
691 for (i = 0; i < rateset->rs_nrates; i++) {
692 for (j = 0; j < rate_table->rate_cnt; j++) {
693 u_int32_t phy = rate_table->info[j].phy;
694 u_int32_t valid = (ath_rc_priv->single_stream ?
695 rate_table->info[j].valid_single_stream :
696 rate_table->info[j].valid);
697
698 /* We allow a rate only if its valid and the
699 * capflag matches one of the validity
700 * (TRUE/TRUE_20/TRUE_40) flags */
701
702 /* XXX: catch the negative of this branch
703 * first and then continue */
704 if (((rateset->rs_rates[i] & 0x7F) ==
705 (rate_table->info[j].dot11rate & 0x7F)) &&
706 ((valid & WLAN_RC_CAP_MODE(capflag)) ==
707 WLAN_RC_CAP_MODE(capflag)) &&
708 !WLAN_RC_PHY_HT(phy)) {
709
710 u_int8_t valid_rate_count = 0;
711
712 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
713 continue;
714
715 valid_rate_count =
716 rate_ctrl->valid_phy_ratecnt[phy];
717
718 rate_ctrl->valid_phy_rateidx[phy]
719 [valid_rate_count] = j;
720 rate_ctrl->valid_phy_ratecnt[phy] += 1;
721 ath_rc_set_valid_txmask(rate_ctrl, j, TRUE);
722 hi = A_MAX(hi, j);
723 }
724 }
725 }
726 return hi;
727 }
728
729 static u_int8_t ath_rc_sib_setvalid_htrates(struct ath_rate_node *ath_rc_priv,
730 const struct ath_rate_table *rate_table,
731 u_int8_t *mcs_set, u_int32_t capflag)
732 {
733 u_int8_t i, j, hi = 0;
734 struct ath_tx_ratectrl *rate_ctrl =
735 (struct ath_tx_ratectrl *)(ath_rc_priv);
736
737 /* Use intersection of working rates and valid rates */
738 for (i = 0; i < ((struct ath_rateset *)mcs_set)->rs_nrates; i++) {
739 for (j = 0; j < rate_table->rate_cnt; j++) {
740 u_int32_t phy = rate_table->info[j].phy;
741 u_int32_t valid = (ath_rc_priv->single_stream ?
742 rate_table->info[j].valid_single_stream :
743 rate_table->info[j].valid);
744
745 if (((((struct ath_rateset *)
746 mcs_set)->rs_rates[i] & 0x7F) !=
747 (rate_table->info[j].dot11rate & 0x7F)) ||
748 !WLAN_RC_PHY_HT(phy) ||
749 !WLAN_RC_PHY_HT_VALID(valid, capflag))
750 continue;
751
752 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
753 continue;
754
755 rate_ctrl->valid_phy_rateidx[phy]
756 [rate_ctrl->valid_phy_ratecnt[phy]] = j;
757 rate_ctrl->valid_phy_ratecnt[phy] += 1;
758 ath_rc_set_valid_txmask(rate_ctrl, j, TRUE);
759 hi = A_MAX(hi, j);
760 }
761 }
762 return hi;
763 }
764
765 /*
766 * Attach to a device instance. Setup the public definition
767 * of how much per-node space we need and setup the private
768 * phy tables that have rate control parameters.
769 */
770 struct ath_rate_softc *ath_rate_attach(struct ath_hal *ah)
771 {
772 struct ath_rate_softc *asc;
773
774 /* we are only in user context so we can sleep for memory */
775 asc = kzalloc(sizeof(struct ath_rate_softc), GFP_KERNEL);
776 if (asc == NULL)
777 return NULL;
778
779 ar5416_attach_ratetables(asc);
780
781 /* Save Maximum TX Trigger Level (used for 11n) */
782 tx_triglevel_max = ah->ah_caps.halTxTrigLevelMax;
783 /* return alias for ath_rate_softc * */
784 return asc;
785 }
786
787 static struct ath_rate_node *ath_rate_node_alloc(struct ath_vap *avp,
788 struct ath_rate_softc *rsc,
789 gfp_t gfp)
790 {
791 struct ath_rate_node *anode;
792
793 anode = kzalloc(sizeof(struct ath_rate_node), gfp);
794 if (anode == NULL)
795 return NULL;
796
797 anode->avp = avp;
798 anode->asc = rsc;
799 return anode;
800 }
801
802 static void ath_rate_node_free(struct ath_rate_node *anode)
803 {
804 if (anode != NULL)
805 kfree(anode);
806 }
807
808 void ath_rate_detach(struct ath_rate_softc *asc)
809 {
810 if (asc != NULL)
811 kfree(asc);
812 }
813
814 u_int8_t ath_rate_findrateix(struct ath_softc *sc,
815 u_int8_t dot11rate)
816 {
817 const struct ath_rate_table *ratetable;
818 struct ath_rate_softc *rsc = sc->sc_rc;
819 int i;
820
821 ratetable = rsc->hw_rate_table[sc->sc_curmode];
822
823 if (WARN_ON(!ratetable))
824 return 0;
825
826 for (i = 0; i < ratetable->rate_cnt; i++) {
827 if ((ratetable->info[i].dot11rate & 0x7f) ==
828 (dot11rate & 0x7f))
829 return i;
830 }
831 return 0;
832 }
833
834 /*
835 * Update rate-control state on a device state change. When
836 * operating as a station this includes associate/reassociate
837 * with an AP. Otherwise this gets called, for example, when
838 * the we transition to run state when operating as an AP.
839 */
840 void ath_rate_newstate(struct ath_softc *sc, struct ath_vap *avp, int up)
841 {
842 struct ath_rate_softc *asc = sc->sc_rc;
843
844 /* For half and quarter rate channles use different
845 * rate tables
846 */
847 if (sc->sc_curchan.channelFlags & CHANNEL_HALF)
848 ar5416_sethalf_ratetable(asc);
849 else if (sc->sc_curchan.channelFlags & CHANNEL_QUARTER)
850 ar5416_setquarter_ratetable(asc);
851 else /* full rate */
852 ar5416_setfull_ratetable(asc);
853
854 if (avp->av_config.av_fixed_rateset != IEEE80211_FIXED_RATE_NONE) {
855 asc->fixedrix =
856 sc->sc_rixmap[avp->av_config.av_fixed_rateset & 0xff];
857 /* NB: check the fixed rate exists */
858 if (asc->fixedrix == 0xff)
859 asc->fixedrix = IEEE80211_FIXED_RATE_NONE;
860 } else {
861 asc->fixedrix = IEEE80211_FIXED_RATE_NONE;
862 }
863 }
864
865 static u_int8_t ath_rc_ratefind_ht(struct ath_softc *sc,
866 struct ath_rate_node *ath_rc_priv,
867 const struct ath_rate_table *rate_table,
868 int probe_allowed, int *is_probing, int is_retry)
869 {
870 u_int32_t dt, best_thruput, this_thruput, now_msec;
871 u_int8_t rate, next_rate, best_rate, maxindex, minindex;
872 int8_t rssi_last, rssi_reduce = 0, index = 0;
873 struct ath_tx_ratectrl *rate_ctrl = NULL;
874
875 rate_ctrl = (struct ath_tx_ratectrl *)(ath_rc_priv ?
876 (ath_rc_priv) : NULL);
877
878 *is_probing = FALSE;
879
880 rssi_last = median(rate_ctrl->rssi_last,
881 rate_ctrl->rssi_last_prev,
882 rate_ctrl->rssi_last_prev2);
883
884 /*
885 * Age (reduce) last ack rssi based on how old it is.
886 * The bizarre numbers are so the delta is 160msec,
887 * meaning we divide by 16.
888 * 0msec <= dt <= 25msec: don't derate
889 * 25msec <= dt <= 185msec: derate linearly from 0 to 10dB
890 * 185msec <= dt: derate by 10dB
891 */
892
893 now_msec = jiffies_to_msecs(jiffies);
894 dt = now_msec - rate_ctrl->rssi_time;
895
896 if (dt >= 185)
897 rssi_reduce = 10;
898 else if (dt >= 25)
899 rssi_reduce = (u_int8_t)((dt - 25) >> 4);
900
901 /* Now reduce rssi_last by rssi_reduce */
902 if (rssi_last < rssi_reduce)
903 rssi_last = 0;
904 else
905 rssi_last -= rssi_reduce;
906
907 /*
908 * Now look up the rate in the rssi table and return it.
909 * If no rates match then we return 0 (lowest rate)
910 */
911
912 best_thruput = 0;
913 maxindex = rate_ctrl->max_valid_rate-1;
914
915 minindex = 0;
916 best_rate = minindex;
917 /*
918 * Try the higher rate first. It will reduce memory moving time
919 * if we have very good channel characteristics.
920 */
921
922 for (index = maxindex; index >= minindex ; index--) {
923 u_int8_t per_thres;
924
925 rate = rate_ctrl->valid_rate_index[index];
926
927 if (rate > rate_ctrl->rate_max_phy)
928 continue;
929
930 /*
931 * For TCP the average collision rate is around 11%,
932 * so we ignore PERs less than this. This is to
933 * prevent the rate we are currently using (whose
934 * PER might be in the 10-15 range because of TCP
935 * collisions) looking worse than the next lower
936 * rate whose PER has decayed close to 0. If we
937 * used to next lower rate, its PER would grow to
938 * 10-15 and we would be worse off then staying
939 * at the current rate.
940 */
941 per_thres = rate_ctrl->state[rate].per;
942 if (per_thres < 12)
943 per_thres = 12;
944
945 this_thruput = rate_table->info[rate].user_ratekbps *
946 (100 - per_thres);
947
948 if (best_thruput <= this_thruput) {
949 best_thruput = this_thruput;
950 best_rate = rate;
951 }
952 }
953
954 rate = best_rate;
955
956 /* if we are retrying for more than half the number
957 * of max retries, use the min rate for the next retry
958 */
959 if (is_retry)
960 rate = rate_ctrl->valid_rate_index[minindex];
961
962 rate_ctrl->rssi_last_lookup = rssi_last;
963
964 /*
965 * Must check the actual rate (ratekbps) to account for
966 * non-monoticity of 11g's rate table
967 */
968
969 if (rate >= rate_ctrl->rate_max_phy && probe_allowed) {
970
971 rate = rate_ctrl->rate_max_phy;
972
973 /* Probe the next allowed phy state */
974 /* FIXME:XXXX Check to make sure ratMax is checked properly */
975 if (ath_rc_get_nextvalid_txrate(rate_table,
976 rate_ctrl, rate, &next_rate) &&
977 (now_msec - rate_ctrl->probe_time >
978 rate_table->probe_interval) &&
979 (rate_ctrl->hw_maxretry_pktcnt >= 1)) {
980 rate = next_rate;
981 rate_ctrl->probe_rate = rate;
982 rate_ctrl->probe_time = now_msec;
983 rate_ctrl->hw_maxretry_pktcnt = 0;
984 *is_probing = TRUE;
985 }
986 }
987
988 /*
989 * Make sure rate is not higher than the allowed maximum.
990 * We should also enforce the min, but I suspect the min is
991 * normally 1 rather than 0 because of the rate 9 vs 6 issue
992 * in the old code.
993 */
994 if (rate > (rate_ctrl->rate_table_size - 1))
995 rate = rate_ctrl->rate_table_size - 1;
996
997 ASSERT((rate_table->info[rate].valid && !ath_rc_priv->single_stream) ||
998 (rate_table->info[rate].valid_single_stream &&
999 ath_rc_priv->single_stream));
1000
1001 return rate;
1002 }
1003
1004 static void ath_rc_rate_set_series(const struct ath_rate_table *rate_table ,
1005 struct ath_rc_series *series, u_int8_t tries, u_int8_t rix,
1006 int rtsctsenable)
1007 {
1008 series->tries = tries;
1009 series->flags = (rtsctsenable? ATH_RC_RTSCTS_FLAG : 0) |
1010 (WLAN_RC_PHY_DS(rate_table->info[rix].phy) ?
1011 ATH_RC_DS_FLAG : 0) |
1012 (WLAN_RC_PHY_40(rate_table->info[rix].phy) ?
1013 ATH_RC_CW40_FLAG : 0) |
1014 (WLAN_RC_PHY_SGI(rate_table->info[rix].phy) ?
1015 ATH_RC_SGI_FLAG : 0);
1016
1017 series->rix = rate_table->info[rix].base_index;
1018 series->max_4ms_framelen = rate_table->info[rix].max_4ms_framelen;
1019 }
1020
1021 static u_int8_t ath_rc_rate_getidx(struct ath_softc *sc,
1022 struct ath_rate_node *ath_rc_priv,
1023 const struct ath_rate_table *rate_table,
1024 u_int8_t rix, u_int16_t stepdown, u_int16_t min_rate)
1025 {
1026 u_int32_t j;
1027 u_int8_t nextindex;
1028 struct ath_tx_ratectrl *rate_ctrl =
1029 (struct ath_tx_ratectrl *)(ath_rc_priv);
1030
1031 if (min_rate) {
1032 for (j = RATE_TABLE_SIZE; j > 0; j--) {
1033 if (ath_rc_get_nextlowervalid_txrate(rate_table,
1034 rate_ctrl, rix, &nextindex))
1035 rix = nextindex;
1036 else
1037 break;
1038 }
1039 } else {
1040 for (j = stepdown; j > 0; j--) {
1041 if (ath_rc_get_nextlowervalid_txrate(rate_table,
1042 rate_ctrl, rix, &nextindex))
1043 rix = nextindex;
1044 else
1045 break;
1046 }
1047 }
1048 return rix;
1049 }
1050
1051 static void ath_rc_ratefind(struct ath_softc *sc,
1052 struct ath_rate_node *ath_rc_priv,
1053 int num_tries, int num_rates, unsigned int rcflag,
1054 struct ath_rc_series series[], int *is_probe, int is_retry)
1055 {
1056 u_int8_t try_per_rate = 0, i = 0, rix, nrix;
1057 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1058 struct ath_rate_table *rate_table;
1059
1060 rate_table =
1061 (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
1062 rix = ath_rc_ratefind_ht(sc, ath_rc_priv, rate_table,
1063 (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
1064 is_probe, is_retry);
1065 nrix = rix;
1066
1067 if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*is_probe)) {
1068 /* set one try for probe rates. For the
1069 * probes don't enable rts */
1070 ath_rc_rate_set_series(rate_table,
1071 &series[i++], 1, nrix, FALSE);
1072
1073 try_per_rate = (num_tries/num_rates);
1074 /* Get the next tried/allowed rate. No RTS for the next series
1075 * after the probe rate
1076 */
1077 nrix = ath_rc_rate_getidx(sc,
1078 ath_rc_priv, rate_table, nrix, 1, FALSE);
1079 ath_rc_rate_set_series(rate_table,
1080 &series[i++], try_per_rate, nrix, 0);
1081 } else {
1082 try_per_rate = (num_tries/num_rates);
1083 /* Set the choosen rate. No RTS for first series entry. */
1084 ath_rc_rate_set_series(rate_table,
1085 &series[i++], try_per_rate, nrix, FALSE);
1086 }
1087
1088 /* Fill in the other rates for multirate retry */
1089 for ( ; i < num_rates; i++) {
1090 u_int8_t try_num;
1091 u_int8_t min_rate;
1092
1093 try_num = ((i + 1) == num_rates) ?
1094 num_tries - (try_per_rate * i) : try_per_rate ;
1095 min_rate = (((i + 1) == num_rates) &&
1096 (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
1097
1098 nrix = ath_rc_rate_getidx(sc, ath_rc_priv,
1099 rate_table, nrix, 1, min_rate);
1100 /* All other rates in the series have RTS enabled */
1101 ath_rc_rate_set_series(rate_table,
1102 &series[i], try_num, nrix, TRUE);
1103 }
1104
1105 /*
1106 * NB:Change rate series to enable aggregation when operating
1107 * at lower MCS rates. When first rate in series is MCS2
1108 * in HT40 @ 2.4GHz, series should look like:
1109 *
1110 * {MCS2, MCS1, MCS0, MCS0}.
1111 *
1112 * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
1113 * look like:
1114 *
1115 * {MCS3, MCS2, MCS1, MCS1}
1116 *
1117 * So, set fourth rate in series to be same as third one for
1118 * above conditions.
1119 */
1120 if ((sc->sc_curmode == WIRELESS_MODE_11NG_HT20) ||
1121 (sc->sc_curmode == WIRELESS_MODE_11NG_HT40PLUS) ||
1122 (sc->sc_curmode == WIRELESS_MODE_11NG_HT40MINUS)) {
1123 u_int8_t dot11rate = rate_table->info[rix].dot11rate;
1124 u_int8_t phy = rate_table->info[rix].phy;
1125 if (i == 4 &&
1126 ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
1127 (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
1128 series[3].rix = series[2].rix;
1129 series[3].flags = series[2].flags;
1130 series[3].max_4ms_framelen = series[2].max_4ms_framelen;
1131 }
1132 }
1133 }
1134
1135 /*
1136 * Return the Tx rate series.
1137 */
1138
1139
1140 void ath_rate_findrate(struct ath_softc *sc,
1141 struct ath_rate_node *ath_rc_priv,
1142 int num_tries,
1143 int num_rates,
1144 unsigned int rcflag,
1145 struct ath_rc_series series[],
1146 int *is_probe,
1147 int is_retry
1148 )
1149 {
1150 struct ath_vap *avp = ath_rc_priv->avp;
1151
1152 DPRINTF(sc, ATH_DEBUG_RATE, "%s", __func__);
1153 if (!num_rates || !num_tries)
1154 return;
1155
1156 if (avp->av_config.av_fixed_rateset == IEEE80211_FIXED_RATE_NONE) {
1157 ath_rc_ratefind(sc, ath_rc_priv, num_tries, num_rates,
1158 rcflag, series, is_probe, is_retry);
1159 } else {
1160 /* Fixed rate */
1161 int idx;
1162 u_int8_t flags;
1163 u_int32_t rix;
1164 struct ath_rate_softc *asc = ath_rc_priv->asc;
1165 struct ath_rate_table *rate_table;
1166
1167 rate_table = (struct ath_rate_table *)
1168 asc->hw_rate_table[sc->sc_curmode];
1169
1170 for (idx = 0; idx < 4; idx++) {
1171 unsigned int mcs;
1172 u_int8_t series_rix = 0;
1173
1174 series[idx].tries =
1175 IEEE80211_RATE_IDX_ENTRY(
1176 avp->av_config.av_fixed_retryset, idx);
1177
1178 mcs = IEEE80211_RATE_IDX_ENTRY(
1179 avp->av_config.av_fixed_rateset, idx);
1180
1181 if (idx == 3 && (mcs & 0xf0) == 0x70)
1182 mcs = (mcs & ~0xf0)|0x80;
1183
1184 if (!(mcs & 0x80))
1185 flags = 0;
1186 else
1187 flags = ((ath_rc_priv->ht_cap &
1188 WLAN_RC_DS_FLAG) ?
1189 ATH_RC_DS_FLAG : 0) |
1190 ((ath_rc_priv->ht_cap &
1191 WLAN_RC_40_FLAG) ?
1192 ATH_RC_CW40_FLAG : 0) |
1193 ((ath_rc_priv->ht_cap &
1194 WLAN_RC_SGI_FLAG) ?
1195 ((ath_rc_priv->ht_cap &
1196 WLAN_RC_40_FLAG) ?
1197 ATH_RC_SGI_FLAG : 0) : 0);
1198
1199 series[idx].rix = sc->sc_rixmap[mcs];
1200 series_rix = series[idx].rix;
1201
1202 /* XXX: Give me some cleanup love */
1203 if ((flags & ATH_RC_CW40_FLAG) &&
1204 (flags & ATH_RC_SGI_FLAG))
1205 rix = rate_table->info[series_rix].ht_index;
1206 else if (flags & ATH_RC_SGI_FLAG)
1207 rix = rate_table->info[series_rix].sgi_index;
1208 else if (flags & ATH_RC_CW40_FLAG)
1209 rix = rate_table->info[series_rix].cw40index;
1210 else
1211 rix = rate_table->info[series_rix].base_index;
1212 series[idx].max_4ms_framelen =
1213 rate_table->info[rix].max_4ms_framelen;
1214 series[idx].flags = flags;
1215 }
1216 }
1217 }
1218
1219 static void ath_rc_update_ht(struct ath_softc *sc,
1220 struct ath_rate_node *ath_rc_priv,
1221 struct ath_tx_info_priv *info_priv,
1222 int tx_rate, int xretries, int retries)
1223 {
1224 struct ath_tx_ratectrl *rate_ctrl;
1225 u_int32_t now_msec = jiffies_to_msecs(jiffies);
1226 int state_change = FALSE, rate, count;
1227 u_int8_t last_per;
1228 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1229 struct ath_rate_table *rate_table =
1230 (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
1231
1232 static u_int32_t nretry_to_per_lookup[10] = {
1233 100 * 0 / 1,
1234 100 * 1 / 4,
1235 100 * 1 / 2,
1236 100 * 3 / 4,
1237 100 * 4 / 5,
1238 100 * 5 / 6,
1239 100 * 6 / 7,
1240 100 * 7 / 8,
1241 100 * 8 / 9,
1242 100 * 9 / 10
1243 };
1244
1245 if (!ath_rc_priv)
1246 return;
1247
1248 rate_ctrl = (struct ath_tx_ratectrl *)(ath_rc_priv);
1249
1250 ASSERT(tx_rate >= 0);
1251 if (tx_rate < 0)
1252 return;
1253
1254 /* To compensate for some imbalance between ctrl and ext. channel */
1255
1256 if (WLAN_RC_PHY_40(rate_table->info[tx_rate].phy))
1257 info_priv->tx.ts_rssi =
1258 info_priv->tx.ts_rssi < 3? 0: info_priv->tx.ts_rssi - 3;
1259
1260 last_per = rate_ctrl->state[tx_rate].per;
1261
1262 if (xretries) {
1263 /* Update the PER. */
1264 if (xretries == 1) {
1265 rate_ctrl->state[tx_rate].per += 30;
1266 if (rate_ctrl->state[tx_rate].per > 100)
1267 rate_ctrl->state[tx_rate].per = 100;
1268
1269 } else {
1270 /* xretries == 2 */
1271 count = sizeof(nretry_to_per_lookup) /
1272 sizeof(nretry_to_per_lookup[0]);
1273 if (retries >= count)
1274 retries = count - 1;
1275 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
1276 rate_ctrl->state[tx_rate].per =
1277 (u_int8_t)(rate_ctrl->state[tx_rate].per -
1278 (rate_ctrl->state[tx_rate].per >> 3) +
1279 ((100) >> 3));
1280 }
1281
1282 /* xretries == 1 or 2 */
1283
1284 if (rate_ctrl->probe_rate == tx_rate)
1285 rate_ctrl->probe_rate = 0;
1286
1287 } else { /* xretries == 0 */
1288
1289 /* Update the PER. */
1290 /* Make sure it doesn't index out of array's bounds. */
1291 count = sizeof(nretry_to_per_lookup) /
1292 sizeof(nretry_to_per_lookup[0]);
1293 if (retries >= count)
1294 retries = count - 1;
1295 if (info_priv->n_bad_frames) {
1296 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
1297 /*
1298 * Assuming that n_frames is not 0. The current PER
1299 * from the retries is 100 * retries / (retries+1),
1300 * since the first retries attempts failed, and the
1301 * next one worked. For the one that worked,
1302 * n_bad_frames subframes out of n_frames wored,
1303 * so the PER for that part is
1304 * 100 * n_bad_frames / n_frames, and it contributes
1305 * 100 * n_bad_frames / (n_frames * (retries+1)) to
1306 * the above PER. The expression below is a
1307 * simplified version of the sum of these two terms.
1308 */
1309 if (info_priv->n_frames > 0)
1310 rate_ctrl->state[tx_rate].per
1311 = (u_int8_t)
1312 (rate_ctrl->state[tx_rate].per -
1313 (rate_ctrl->state[tx_rate].per >> 3) +
1314 ((100*(retries*info_priv->n_frames +
1315 info_priv->n_bad_frames) /
1316 (info_priv->n_frames *
1317 (retries+1))) >> 3));
1318 } else {
1319 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
1320
1321 rate_ctrl->state[tx_rate].per = (u_int8_t)
1322 (rate_ctrl->state[tx_rate].per -
1323 (rate_ctrl->state[tx_rate].per >> 3) +
1324 (nretry_to_per_lookup[retries] >> 3));
1325 }
1326
1327 rate_ctrl->rssi_last_prev2 = rate_ctrl->rssi_last_prev;
1328 rate_ctrl->rssi_last_prev = rate_ctrl->rssi_last;
1329 rate_ctrl->rssi_last = info_priv->tx.ts_rssi;
1330 rate_ctrl->rssi_time = now_msec;
1331
1332 /*
1333 * If we got at most one retry then increase the max rate if
1334 * this was a probe. Otherwise, ignore the probe.
1335 */
1336
1337 if (rate_ctrl->probe_rate && rate_ctrl->probe_rate == tx_rate) {
1338 if (retries > 0 || 2 * info_priv->n_bad_frames >
1339 info_priv->n_frames) {
1340 /*
1341 * Since we probed with just a single attempt,
1342 * any retries means the probe failed. Also,
1343 * if the attempt worked, but more than half
1344 * the subframes were bad then also consider
1345 * the probe a failure.
1346 */
1347 rate_ctrl->probe_rate = 0;
1348 } else {
1349 u_int8_t probe_rate = 0;
1350
1351 rate_ctrl->rate_max_phy = rate_ctrl->probe_rate;
1352 probe_rate = rate_ctrl->probe_rate;
1353
1354 if (rate_ctrl->state[probe_rate].per > 30)
1355 rate_ctrl->state[probe_rate].per = 20;
1356
1357 rate_ctrl->probe_rate = 0;
1358
1359 /*
1360 * Since this probe succeeded, we allow the next
1361 * probe twice as soon. This allows the maxRate
1362 * to move up faster if the probes are
1363 * succesful.
1364 */
1365 rate_ctrl->probe_time = now_msec -
1366 rate_table->probe_interval / 2;
1367 }
1368 }
1369
1370 if (retries > 0) {
1371 /*
1372 * Don't update anything. We don't know if
1373 * this was because of collisions or poor signal.
1374 *
1375 * Later: if rssi_ack is close to
1376 * rate_ctrl->state[txRate].rssi_thres and we see lots
1377 * of retries, then we could increase
1378 * rate_ctrl->state[txRate].rssi_thres.
1379 */
1380 rate_ctrl->hw_maxretry_pktcnt = 0;
1381 } else {
1382 /*
1383 * It worked with no retries. First ignore bogus (small)
1384 * rssi_ack values.
1385 */
1386 if (tx_rate == rate_ctrl->rate_max_phy &&
1387 rate_ctrl->hw_maxretry_pktcnt < 255) {
1388 rate_ctrl->hw_maxretry_pktcnt++;
1389 }
1390
1391 if (info_priv->tx.ts_rssi >=
1392 rate_table->info[tx_rate].rssi_ack_validmin) {
1393 /* Average the rssi */
1394 if (tx_rate != rate_ctrl->rssi_sum_rate) {
1395 rate_ctrl->rssi_sum_rate = tx_rate;
1396 rate_ctrl->rssi_sum =
1397 rate_ctrl->rssi_sum_cnt = 0;
1398 }
1399
1400 rate_ctrl->rssi_sum += info_priv->tx.ts_rssi;
1401 rate_ctrl->rssi_sum_cnt++;
1402
1403 if (rate_ctrl->rssi_sum_cnt > 4) {
1404 int32_t rssi_ackAvg =
1405 (rate_ctrl->rssi_sum + 2) / 4;
1406 int8_t rssi_thres =
1407 rate_ctrl->state[tx_rate].
1408 rssi_thres;
1409 int8_t rssi_ack_vmin =
1410 rate_table->info[tx_rate].
1411 rssi_ack_validmin;
1412
1413 rate_ctrl->rssi_sum =
1414 rate_ctrl->rssi_sum_cnt = 0;
1415
1416 /* Now reduce the current
1417 * rssi threshold. */
1418 if ((rssi_ackAvg < rssi_thres + 2) &&
1419 (rssi_thres > rssi_ack_vmin)) {
1420 rate_ctrl->state[tx_rate].
1421 rssi_thres--;
1422 }
1423
1424 state_change = TRUE;
1425 }
1426 }
1427 }
1428 }
1429
1430
1431 /* For all cases */
1432
1433 /*
1434 * If this rate looks bad (high PER) then stop using it for
1435 * a while (except if we are probing).
1436 */
1437 if (rate_ctrl->state[tx_rate].per >= 55 && tx_rate > 0 &&
1438 rate_table->info[tx_rate].ratekbps <=
1439 rate_table->info[rate_ctrl->rate_max_phy].ratekbps) {
1440 ath_rc_get_nextlowervalid_txrate(rate_table, rate_ctrl,
1441 (u_int8_t) tx_rate, &rate_ctrl->rate_max_phy);
1442
1443 /* Don't probe for a little while. */
1444 rate_ctrl->probe_time = now_msec;
1445 }
1446
1447 if (state_change) {
1448 /*
1449 * Make sure the rates above this have higher rssi thresholds.
1450 * (Note: Monotonicity is kept within the OFDM rates and
1451 * within the CCK rates. However, no adjustment is
1452 * made to keep the rssi thresholds monotonically
1453 * increasing between the CCK and OFDM rates.)
1454 */
1455 for (rate = tx_rate; rate <
1456 rate_ctrl->rate_table_size - 1; rate++) {
1457 if (rate_table->info[rate+1].phy !=
1458 rate_table->info[tx_rate].phy)
1459 break;
1460
1461 if (rate_ctrl->state[rate].rssi_thres +
1462 rate_table->info[rate].rssi_ack_deltamin >
1463 rate_ctrl->state[rate+1].rssi_thres) {
1464 rate_ctrl->state[rate+1].rssi_thres =
1465 rate_ctrl->state[rate].
1466 rssi_thres +
1467 rate_table->info[rate].
1468 rssi_ack_deltamin;
1469 }
1470 }
1471
1472 /* Make sure the rates below this have lower rssi thresholds. */
1473 for (rate = tx_rate - 1; rate >= 0; rate--) {
1474 if (rate_table->info[rate].phy !=
1475 rate_table->info[tx_rate].phy)
1476 break;
1477
1478 if (rate_ctrl->state[rate].rssi_thres +
1479 rate_table->info[rate].rssi_ack_deltamin >
1480 rate_ctrl->state[rate+1].rssi_thres) {
1481 if (rate_ctrl->state[rate+1].rssi_thres <
1482 rate_table->info[rate].
1483 rssi_ack_deltamin)
1484 rate_ctrl->state[rate].rssi_thres = 0;
1485 else {
1486 rate_ctrl->state[rate].rssi_thres =
1487 rate_ctrl->state[rate+1].
1488 rssi_thres -
1489 rate_table->info[rate].
1490 rssi_ack_deltamin;
1491 }
1492
1493 if (rate_ctrl->state[rate].rssi_thres <
1494 rate_table->info[rate].
1495 rssi_ack_validmin) {
1496 rate_ctrl->state[rate].rssi_thres =
1497 rate_table->info[rate].
1498 rssi_ack_validmin;
1499 }
1500 }
1501 }
1502 }
1503
1504 /* Make sure the rates below this have lower PER */
1505 /* Monotonicity is kept only for rates below the current rate. */
1506 if (rate_ctrl->state[tx_rate].per < last_per) {
1507 for (rate = tx_rate - 1; rate >= 0; rate--) {
1508 if (rate_table->info[rate].phy !=
1509 rate_table->info[tx_rate].phy)
1510 break;
1511
1512 if (rate_ctrl->state[rate].per >
1513 rate_ctrl->state[rate+1].per) {
1514 rate_ctrl->state[rate].per =
1515 rate_ctrl->state[rate+1].per;
1516 }
1517 }
1518 }
1519
1520 /* Maintain monotonicity for rates above the current rate */
1521 for (rate = tx_rate; rate < rate_ctrl->rate_table_size - 1; rate++) {
1522 if (rate_ctrl->state[rate+1].per < rate_ctrl->state[rate].per)
1523 rate_ctrl->state[rate+1].per =
1524 rate_ctrl->state[rate].per;
1525 }
1526
1527 /* Every so often, we reduce the thresholds and
1528 * PER (different for CCK and OFDM). */
1529 if (now_msec - rate_ctrl->rssi_down_time >=
1530 rate_table->rssi_reduce_interval) {
1531
1532 for (rate = 0; rate < rate_ctrl->rate_table_size; rate++) {
1533 if (rate_ctrl->state[rate].rssi_thres >
1534 rate_table->info[rate].rssi_ack_validmin)
1535 rate_ctrl->state[rate].rssi_thres -= 1;
1536 }
1537 rate_ctrl->rssi_down_time = now_msec;
1538 }
1539
1540 /* Every so often, we reduce the thresholds
1541 * and PER (different for CCK and OFDM). */
1542 if (now_msec - rate_ctrl->per_down_time >=
1543 rate_table->rssi_reduce_interval) {
1544 for (rate = 0; rate < rate_ctrl->rate_table_size; rate++) {
1545 rate_ctrl->state[rate].per =
1546 7 * rate_ctrl->state[rate].per / 8;
1547 }
1548
1549 rate_ctrl->per_down_time = now_msec;
1550 }
1551 }
1552
1553 /*
1554 * This routine is called in rate control callback tx_status() to give
1555 * the status of previous frames.
1556 */
1557 static void ath_rc_update(struct ath_softc *sc,
1558 struct ath_rate_node *ath_rc_priv,
1559 struct ath_tx_info_priv *info_priv, int final_ts_idx,
1560 int xretries, int long_retry)
1561 {
1562 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1563 struct ath_rate_table *rate_table;
1564 struct ath_tx_ratectrl *rate_ctrl;
1565 struct ath_rc_series rcs[4];
1566 u_int8_t flags;
1567 u_int32_t series = 0, rix;
1568
1569 memcpy(rcs, info_priv->rcs, 4 * sizeof(rcs[0]));
1570 rate_table = (struct ath_rate_table *)
1571 asc->hw_rate_table[sc->sc_curmode];
1572 rate_ctrl = (struct ath_tx_ratectrl *)(ath_rc_priv);
1573 ASSERT(rcs[0].tries != 0);
1574
1575 /*
1576 * If the first rate is not the final index, there
1577 * are intermediate rate failures to be processed.
1578 */
1579 if (final_ts_idx != 0) {
1580
1581 /* Process intermediate rates that failed.*/
1582 for (series = 0; series < final_ts_idx ; series++) {
1583 if (rcs[series].tries != 0) {
1584 flags = rcs[series].flags;
1585 /* If HT40 and we have switched mode from
1586 * 40 to 20 => don't update */
1587 if ((flags & ATH_RC_CW40_FLAG) &&
1588 (rate_ctrl->rc_phy_mode !=
1589 (flags & ATH_RC_CW40_FLAG)))
1590 return;
1591 if ((flags & ATH_RC_CW40_FLAG) &&
1592 (flags & ATH_RC_SGI_FLAG))
1593 rix = rate_table->info[
1594 rcs[series].rix].ht_index;
1595 else if (flags & ATH_RC_SGI_FLAG)
1596 rix = rate_table->info[
1597 rcs[series].rix].sgi_index;
1598 else if (flags & ATH_RC_CW40_FLAG)
1599 rix = rate_table->info[
1600 rcs[series].rix].cw40index;
1601 else
1602 rix = rate_table->info[
1603 rcs[series].rix].base_index;
1604 ath_rc_update_ht(sc, ath_rc_priv,
1605 info_priv, rix,
1606 xretries? 1 : 2,
1607 rcs[series].tries);
1608 }
1609 }
1610 } else {
1611 /*
1612 * Handle the special case of MIMO PS burst, where the second
1613 * aggregate is sent out with only one rate and one try.
1614 * Treating it as an excessive retry penalizes the rate
1615 * inordinately.
1616 */
1617 if (rcs[0].tries == 1 && xretries == 1)
1618 xretries = 2;
1619 }
1620
1621 flags = rcs[series].flags;
1622 /* If HT40 and we have switched mode from 40 to 20 => don't update */
1623 if ((flags & ATH_RC_CW40_FLAG) &&
1624 (rate_ctrl->rc_phy_mode != (flags & ATH_RC_CW40_FLAG)))
1625 return;
1626
1627 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_SGI_FLAG))
1628 rix = rate_table->info[rcs[series].rix].ht_index;
1629 else if (flags & ATH_RC_SGI_FLAG)
1630 rix = rate_table->info[rcs[series].rix].sgi_index;
1631 else if (flags & ATH_RC_CW40_FLAG)
1632 rix = rate_table->info[rcs[series].rix].cw40index;
1633 else
1634 rix = rate_table->info[rcs[series].rix].base_index;
1635
1636 ath_rc_update_ht(sc, ath_rc_priv, info_priv, rix,
1637 xretries, long_retry);
1638 }
1639
1640
1641 /*
1642 * Process a tx descriptor for a completed transmit (success or failure).
1643 */
1644
1645 static void ath_rate_tx_complete(struct ath_softc *sc,
1646 struct ath_node *an, struct ath_rate_node *rc_priv,
1647 struct ath_tx_info_priv *info_priv)
1648 {
1649 int final_ts_idx = info_priv->tx.ts_rateindex;
1650 int tx_status = 0, is_underrun = 0;
1651 struct ath_vap *avp;
1652
1653 avp = rc_priv->avp;
1654 if ((avp->av_config.av_fixed_rateset != IEEE80211_FIXED_RATE_NONE)
1655 || info_priv->tx.ts_status & HAL_TXERR_FILT)
1656 return;
1657
1658 if (info_priv->tx.ts_rssi > 0) {
1659 ATH_RSSI_LPF(an->an_chainmask_sel.tx_avgrssi,
1660 info_priv->tx.ts_rssi);
1661 }
1662
1663 /*
1664 * If underrun error is seen assume it as an excessive retry only
1665 * if prefetch trigger level have reached the max (0x3f for 5416)
1666 * Adjust the long retry as if the frame was tried ATH_11N_TXMAXTRY
1667 * times. This affects how ratectrl updates PER for the failed rate.
1668 */
1669 if (info_priv->tx.ts_flags &
1670 (HAL_TX_DATA_UNDERRUN | HAL_TX_DELIM_UNDERRUN) &&
1671 ((sc->sc_ah->ah_txTrigLevel) >= tx_triglevel_max)) {
1672 tx_status = 1;
1673 is_underrun = 1;
1674 }
1675
1676 if ((info_priv->tx.ts_status & HAL_TXERR_XRETRY) ||
1677 (info_priv->tx.ts_status & HAL_TXERR_FIFO))
1678 tx_status = 1;
1679
1680 ath_rc_update(sc, rc_priv, info_priv, final_ts_idx, tx_status,
1681 (is_underrun) ? ATH_11N_TXMAXTRY
1682 :info_priv->tx.ts_longretry);
1683 }
1684
1685
1686 /*
1687 * Update the SIB's rate control information
1688 *
1689 * This should be called when the supported rates change
1690 * (e.g. SME operation, wireless mode change)
1691 *
1692 * It will determine which rates are valid for use.
1693 */
1694 static void ath_rc_sib_update(struct ath_softc *sc,
1695 struct ath_rate_node *ath_rc_priv,
1696 u_int32_t capflag, int keep_state,
1697 struct ath_rateset *negotiated_rates,
1698 struct ath_rateset *negotiated_htrates)
1699 {
1700 struct ath_rate_table *rate_table = NULL;
1701 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1702 struct ath_rateset *rateset = negotiated_rates;
1703 u_int8_t *ht_mcs = (u_int8_t *)negotiated_htrates;
1704 struct ath_tx_ratectrl *rate_ctrl = (struct ath_tx_ratectrl *)
1705 (ath_rc_priv);
1706 u_int8_t i, j, k, hi = 0, hthi = 0;
1707
1708 rate_table = (struct ath_rate_table *)
1709 asc->hw_rate_table[sc->sc_curmode];
1710
1711 /* Initial rate table size. Will change depending
1712 * on the working rate set */
1713 rate_ctrl->rate_table_size = MAX_TX_RATE_TBL;
1714
1715 /* Initialize thresholds according to the global rate table */
1716 for (i = 0 ; (i < rate_ctrl->rate_table_size) && (!keep_state); i++) {
1717 rate_ctrl->state[i].rssi_thres =
1718 rate_table->info[i].rssi_ack_validmin;
1719 rate_ctrl->state[i].per = 0;
1720 }
1721
1722 /* Determine the valid rates */
1723 ath_rc_init_valid_txmask(rate_ctrl);
1724
1725 for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
1726 for (j = 0; j < MAX_TX_RATE_PHY; j++)
1727 rate_ctrl->valid_phy_rateidx[i][j] = 0;
1728 rate_ctrl->valid_phy_ratecnt[i] = 0;
1729 }
1730 rate_ctrl->rc_phy_mode = (capflag & WLAN_RC_40_FLAG);
1731
1732 /* Set stream capability */
1733 ath_rc_priv->single_stream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
1734
1735 if (!rateset->rs_nrates) {
1736 /* No working rate, just initialize valid rates */
1737 hi = ath_rc_sib_init_validrates(ath_rc_priv, rate_table,
1738 capflag);
1739 } else {
1740 /* Use intersection of working rates and valid rates */
1741 hi = ath_rc_sib_setvalid_rates(ath_rc_priv, rate_table,
1742 rateset, capflag);
1743 if (capflag & WLAN_RC_HT_FLAG) {
1744 hthi = ath_rc_sib_setvalid_htrates(ath_rc_priv,
1745 rate_table, ht_mcs, capflag);
1746 }
1747 hi = A_MAX(hi, hthi);
1748 }
1749
1750 rate_ctrl->rate_table_size = hi + 1;
1751 rate_ctrl->rate_max_phy = 0;
1752 ASSERT(rate_ctrl->rate_table_size <= MAX_TX_RATE_TBL);
1753
1754 for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
1755 for (j = 0; j < rate_ctrl->valid_phy_ratecnt[i]; j++) {
1756 rate_ctrl->valid_rate_index[k++] =
1757 rate_ctrl->valid_phy_rateidx[i][j];
1758 }
1759
1760 if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, TRUE)
1761 || !rate_ctrl->valid_phy_ratecnt[i])
1762 continue;
1763
1764 rate_ctrl->rate_max_phy = rate_ctrl->valid_phy_rateidx[i][j-1];
1765 }
1766 ASSERT(rate_ctrl->rate_table_size <= MAX_TX_RATE_TBL);
1767 ASSERT(k <= MAX_TX_RATE_TBL);
1768
1769 rate_ctrl->max_valid_rate = k;
1770 /*
1771 * Some third party vendors don't send the supported rate series in
1772 * order. So sorting to make sure its in order, otherwise our RateFind
1773 * Algo will select wrong rates
1774 */
1775 ath_rc_sort_validrates(rate_table, rate_ctrl);
1776 rate_ctrl->rate_max_phy = rate_ctrl->valid_rate_index[k-4];
1777 }
1778
1779 /*
1780 * Update rate-control state on station associate/reassociate.
1781 */
1782 static int ath_rate_newassoc(struct ath_softc *sc,
1783 struct ath_rate_node *ath_rc_priv,
1784 unsigned int capflag,
1785 struct ath_rateset *negotiated_rates,
1786 struct ath_rateset *negotiated_htrates)
1787 {
1788
1789
1790 ath_rc_priv->ht_cap =
1791 ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
1792 ((capflag & ATH_RC_SGI_FLAG) ? WLAN_RC_SGI_FLAG : 0) |
1793 ((capflag & ATH_RC_HT_FLAG) ? WLAN_RC_HT_FLAG : 0) |
1794 ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0);
1795
1796 ath_rc_sib_update(sc, ath_rc_priv, ath_rc_priv->ht_cap, 0,
1797 negotiated_rates, negotiated_htrates);
1798
1799 return 0;
1800 }
1801
1802 /*
1803 * This routine is called to initialize the rate control parameters
1804 * in the SIB. It is called initially during system initialization
1805 * or when a station is associated with the AP.
1806 */
1807 static void ath_rc_sib_init(struct ath_rate_node *ath_rc_priv)
1808 {
1809 struct ath_tx_ratectrl *rate_ctrl;
1810
1811 rate_ctrl = (struct ath_tx_ratectrl *)(ath_rc_priv);
1812 rate_ctrl->rssi_down_time = jiffies_to_msecs(jiffies);
1813 }
1814
1815
1816 static void ath_setup_rates(struct ieee80211_local *local, struct sta_info *sta)
1817
1818 {
1819 struct ieee80211_supported_band *sband;
1820 struct ieee80211_hw *hw = local_to_hw(local);
1821 struct ath_softc *sc = hw->priv;
1822 struct ath_rate_node *rc_priv = sta->rate_ctrl_priv;
1823 int i, j = 0;
1824
1825 DPRINTF(sc, ATH_DEBUG_RATE, "%s", __func__);
1826 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
1827 for (i = 0; i < sband->n_bitrates; i++) {
1828 if (sta->supp_rates[local->hw.conf.channel->band] & BIT(i)) {
1829 rc_priv->neg_rates.rs_rates[j]
1830 = (sband->bitrates[i].bitrate * 2) / 10;
1831 j++;
1832 }
1833 }
1834 rc_priv->neg_rates.rs_nrates = j;
1835 }
1836
1837 static void ath_rc_node_update(struct ieee80211_hw *hw,
1838 struct ath_rate_node *rc_priv)
1839 {
1840 struct ath_softc *sc = hw->priv;
1841 u_int32_t capflag = 0;
1842
1843 if (hw->conf.ht_conf.ht_supported) {
1844 capflag |= ATH_RC_HT_FLAG | ATH_RC_DS_FLAG;
1845 if (sc->sc_ht_info.tx_chan_width == HAL_HT_MACMODE_2040)
1846 capflag |= ATH_RC_CW40_FLAG;
1847 }
1848
1849 ath_rate_newassoc(sc, rc_priv, capflag,
1850 &rc_priv->neg_rates,
1851 &rc_priv->neg_ht_rates);
1852
1853 }
1854
1855 /* Rate Control callbacks */
1856 static void ath_tx_status(void *priv, struct net_device *dev,
1857 struct sk_buff *skb)
1858 {
1859 struct ath_softc *sc = priv;
1860 struct ath_tx_info_priv *tx_info_priv;
1861 struct ath_node *an;
1862 struct sta_info *sta;
1863 struct ieee80211_local *local;
1864 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1865 struct ieee80211_hdr *hdr;
1866 __le16 fc;
1867
1868 local = hw_to_local(sc->hw);
1869 hdr = (struct ieee80211_hdr *)skb->data;
1870 fc = hdr->frame_control;
1871 tx_info_priv = (struct ath_tx_info_priv *)tx_info->driver_data[0];
1872
1873 spin_lock_bh(&sc->node_lock);
1874 an = ath_node_find(sc, hdr->addr1);
1875 spin_unlock_bh(&sc->node_lock);
1876
1877 sta = sta_info_get(local, hdr->addr1);
1878 if (!an || !sta || !ieee80211_is_data(fc)) {
1879 if (tx_info->driver_data[0] != NULL) {
1880 kfree(tx_info->driver_data[0]);
1881 tx_info->driver_data[0] = NULL;
1882 }
1883 return;
1884 }
1885 if (tx_info->driver_data[0] != NULL) {
1886 ath_rate_tx_complete(sc, an, sta->rate_ctrl_priv, tx_info_priv);
1887 kfree(tx_info->driver_data[0]);
1888 tx_info->driver_data[0] = NULL;
1889 }
1890 }
1891
1892 static void ath_tx_aggr_resp(struct ath_softc *sc,
1893 struct sta_info *sta,
1894 struct ath_node *an,
1895 u8 tidno)
1896 {
1897 struct ieee80211_hw *hw = sc->hw;
1898 struct ieee80211_local *local;
1899 struct ath_atx_tid *txtid;
1900 struct ieee80211_supported_band *sband;
1901 u16 buffersize = 0;
1902 int state;
1903 DECLARE_MAC_BUF(mac);
1904
1905 if (!sc->sc_txaggr)
1906 return;
1907
1908 txtid = ATH_AN_2_TID(an, tidno);
1909 if (!txtid->paused)
1910 return;
1911
1912 local = hw_to_local(sc->hw);
1913 sband = hw->wiphy->bands[hw->conf.channel->band];
1914 buffersize = IEEE80211_MIN_AMPDU_BUF <<
1915 sband->ht_info.ampdu_factor; /* FIXME */
1916 state = sta->ampdu_mlme.tid_state_tx[tidno];
1917
1918 if (state & HT_ADDBA_RECEIVED_MSK) {
1919 txtid->addba_exchangecomplete = 1;
1920 txtid->addba_exchangeinprogress = 0;
1921 txtid->baw_size = buffersize;
1922
1923 DPRINTF(sc, ATH_DEBUG_AGGR,
1924 "%s: Resuming tid, buffersize: %d\n",
1925 __func__,
1926 buffersize);
1927
1928 ath_tx_resume_tid(sc, txtid);
1929 }
1930 }
1931
1932 static void ath_get_rate(void *priv, struct net_device *dev,
1933 struct ieee80211_supported_band *sband,
1934 struct sk_buff *skb,
1935 struct rate_selection *sel)
1936 {
1937 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1938 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1939 struct sta_info *sta;
1940 struct ath_softc *sc = (struct ath_softc *)priv;
1941 struct ieee80211_hw *hw = sc->hw;
1942 struct ath_tx_info_priv *tx_info_priv;
1943 struct ath_rate_node *ath_rc_priv;
1944 struct ath_node *an;
1945 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1946 int is_probe, chk, ret;
1947 s8 lowest_idx;
1948 __le16 fc = hdr->frame_control;
1949 u8 *qc, tid;
1950 DECLARE_MAC_BUF(mac);
1951
1952 DPRINTF(sc, ATH_DEBUG_RATE, "%s\n", __func__);
1953
1954 /* allocate driver private area of tx_info */
1955 tx_info->driver_data[0] = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1956 ASSERT(tx_info->driver_data[0] != NULL);
1957 tx_info_priv = (struct ath_tx_info_priv *)tx_info->driver_data[0];
1958
1959 sta = sta_info_get(local, hdr->addr1);
1960 lowest_idx = rate_lowest_index(local, sband, sta);
1961 tx_info_priv->min_rate = (sband->bitrates[lowest_idx].bitrate * 2) / 10;
1962 /* lowest rate for management and multicast/broadcast frames */
1963 if (!ieee80211_is_data(fc) ||
1964 is_multicast_ether_addr(hdr->addr1) || !sta) {
1965 sel->rate_idx = lowest_idx;
1966 return;
1967 }
1968
1969 ath_rc_priv = sta->rate_ctrl_priv;
1970
1971 /* Find tx rate for unicast frames */
1972 ath_rate_findrate(sc, ath_rc_priv,
1973 ATH_11N_TXMAXTRY, 4,
1974 ATH_RC_PROBE_ALLOWED,
1975 tx_info_priv->rcs,
1976 &is_probe,
1977 AH_FALSE);
1978 if (is_probe == AH_TRUE)
1979 sel->probe_idx = ((struct ath_tx_ratectrl *)
1980 sta->rate_ctrl_priv)->probe_rate;
1981
1982 /* Ratecontrol sometimes returns invalid rate index */
1983 if (tx_info_priv->rcs[0].rix != 0xff)
1984 ath_rc_priv->prev_data_rix = tx_info_priv->rcs[0].rix;
1985 else
1986 tx_info_priv->rcs[0].rix = ath_rc_priv->prev_data_rix;
1987
1988 sel->rate_idx = tx_info_priv->rcs[0].rix;
1989
1990 /* Check if aggregation has to be enabled for this tid */
1991
1992 if (hw->conf.ht_conf.ht_supported) {
1993 if (ieee80211_is_data_qos(fc)) {
1994 qc = ieee80211_get_qos_ctl(hdr);
1995 tid = qc[0] & 0xf;
1996
1997 spin_lock_bh(&sc->node_lock);
1998 an = ath_node_find(sc, hdr->addr1);
1999 spin_unlock_bh(&sc->node_lock);
2000
2001 if (!an) {
2002 DPRINTF(sc, ATH_DEBUG_AGGR,
2003 "%s: Node not found to "
2004 "init/chk TX aggr\n", __func__);
2005 return;
2006 }
2007
2008 chk = ath_tx_aggr_check(sc, an, tid);
2009 if (chk == AGGR_REQUIRED) {
2010 ret = ieee80211_start_tx_ba_session(hw,
2011 hdr->addr1, tid);
2012 if (ret)
2013 DPRINTF(sc, ATH_DEBUG_AGGR,
2014 "%s: Unable to start tx "
2015 "aggr for: %s\n",
2016 __func__,
2017 print_mac(mac, hdr->addr1));
2018 else
2019 DPRINTF(sc, ATH_DEBUG_AGGR,
2020 "%s: Started tx aggr for: %s\n",
2021 __func__,
2022 print_mac(mac, hdr->addr1));
2023 } else if (chk == AGGR_EXCHANGE_PROGRESS)
2024 ath_tx_aggr_resp(sc, sta, an, tid);
2025 }
2026 }
2027 }
2028
2029 static void ath_rate_init(void *priv, void *priv_sta,
2030 struct ieee80211_local *local,
2031 struct sta_info *sta)
2032 {
2033 struct ieee80211_supported_band *sband;
2034 struct ieee80211_hw *hw = local_to_hw(local);
2035 struct ieee80211_conf *conf = &local->hw.conf;
2036 struct ath_softc *sc = hw->priv;
2037 struct hal_channel hchan;
2038 int i, j = 0;
2039
2040 DPRINTF(sc, ATH_DEBUG_RATE, "%s", __func__);
2041
2042 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2043 sta->txrate_idx = rate_lowest_index(local, sband, sta);
2044
2045 hchan.channel = conf->channel->center_freq;
2046 hchan.channelFlags = ath_chan2flags(conf->channel, sc);
2047 if (ath_set_channel(sc, &hchan) < 0)
2048 DPRINTF(sc, ATH_DEBUG_FATAL, "%s: Unable to set channel\n",
2049 __func__);
2050 ath_setup_rates(local, sta);
2051 if (conf->flags&IEEE80211_CONF_SUPPORT_HT_MODE) {
2052 for (i = 0; i < MCS_SET_SIZE; i++) {
2053 if (conf->ht_conf.supp_mcs_set[i/8] & (1<<(i%8)))
2054 ((struct ath_rate_node *)
2055 priv_sta)->neg_ht_rates.rs_rates[j++] = i;
2056 if (j == ATH_RATE_MAX)
2057 break;
2058 }
2059 ((struct ath_rate_node *)priv_sta)->neg_ht_rates.rs_nrates = j;
2060 }
2061 ath_rc_node_update(hw, priv_sta);
2062 }
2063
2064 static void ath_rate_clear(void *priv)
2065 {
2066 return;
2067 }
2068
2069 static void *ath_rate_alloc(struct ieee80211_local *local)
2070 {
2071 struct ieee80211_hw *hw = local_to_hw(local);
2072 struct ath_softc *sc = hw->priv;
2073
2074 DPRINTF(sc, ATH_DEBUG_RATE, "%s", __func__);
2075 return local->hw.priv;
2076 }
2077
2078 static void ath_rate_free(void *priv)
2079 {
2080 return;
2081 }
2082
2083 static void *ath_rate_alloc_sta(void *priv, gfp_t gfp)
2084 {
2085 struct ath_softc *sc = priv;
2086 struct ath_vap *avp = sc->sc_vaps[0];
2087 struct ath_rate_node *rate_priv;
2088
2089 DPRINTF(sc, ATH_DEBUG_RATE, "%s", __func__);
2090 rate_priv = ath_rate_node_alloc(avp, sc->sc_rc, gfp);
2091 if (!rate_priv) {
2092 DPRINTF(sc, ATH_DEBUG_FATAL, "%s:Unable to allocate"
2093 "private rate control structure", __func__);
2094 return NULL;
2095 }
2096 ath_rc_sib_init(rate_priv);
2097 return rate_priv;
2098 }
2099
2100 static void ath_rate_free_sta(void *priv, void *priv_sta)
2101 {
2102 struct ath_rate_node *rate_priv = priv_sta;
2103 struct ath_softc *sc = priv;
2104
2105 DPRINTF(sc, ATH_DEBUG_RATE, "%s", __func__);
2106 ath_rate_node_free(rate_priv);
2107 }
2108
2109 static struct rate_control_ops ath_rate_ops = {
2110 .module = NULL,
2111 .name = "ath9k_rate_control",
2112 .tx_status = ath_tx_status,
2113 .get_rate = ath_get_rate,
2114 .rate_init = ath_rate_init,
2115 .clear = ath_rate_clear,
2116 .alloc = ath_rate_alloc,
2117 .free = ath_rate_free,
2118 .alloc_sta = ath_rate_alloc_sta,
2119 .free_sta = ath_rate_free_sta
2120 };
2121
2122 int ath_rate_control_register(void)
2123 {
2124 return ieee80211_rate_control_register(&ath_rate_ops);
2125 }
2126
2127 void ath_rate_control_unregister(void)
2128 {
2129 ieee80211_rate_control_unregister(&ath_rate_ops);
2130 }
2131