blob: aafd10df9279563f6ff1c57f3e8103f1039922ea [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
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"
Johannes Berg4b7679a2008-09-18 18:14:18 +020023/* FIXME: remove this include! */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070024#include "../net/mac80211/rate.h"
25
26static u32 tx_triglevel_max;
27
28static struct ath_rate_table ar5416_11na_ratetable = {
29 42,
30 {
31 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
32 5400, 0x0b, 0x00, 12,
33 0, 2, 1, 0, 0, 0, 0, 0 },
34 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
35 7800, 0x0f, 0x00, 18,
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 */
56 6400, 0x80, 0x00, 0,
57 0, 2, 3, 8, 24, 8, 24, 3216 },
58 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 13000, /* 13 Mb */
59 12700, 0x81, 0x00, 1,
60 2, 4, 3, 9, 25, 9, 25, 6434 },
61 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 19500, /* 19.5 Mb */
62 18800, 0x82, 0x00, 2,
63 2, 6, 3, 10, 26, 10, 26, 9650 },
64 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 26000, /* 26 Mb */
65 25000, 0x83, 0x00, 3,
66 4, 10, 3, 11, 27, 11, 27, 12868 },
67 { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 39000, /* 39 Mb */
68 36700, 0x84, 0x00, 4,
69 4, 14, 3, 12, 28, 12, 28, 19304 },
70 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 52000, /* 52 Mb */
71 48100, 0x85, 0x00, 5,
72 4, 20, 3, 13, 29, 13, 29, 25740 },
73 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 58500, /* 58.5 Mb */
74 53500, 0x86, 0x00, 6,
75 4, 23, 3, 14, 30, 14, 30, 28956 },
76 { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 65000, /* 65 Mb */
77 59000, 0x87, 0x00, 7,
78 4, 25, 3, 15, 31, 15, 32, 32180 },
79 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 13000, /* 13 Mb */
80 12700, 0x88, 0x00,
81 8, 0, 2, 3, 16, 33, 16, 33, 6430 },
82 { FALSE, FALSE, WLAN_PHY_HT_20_DS, 26000, /* 26 Mb */
83 24800, 0x89, 0x00, 9,
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 },
157 },
158 50, /* probe interval */
159 50, /* rssi reduce interval */
160 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
161};
162
163/* TRUE_ALL - valid for 20/40/Legacy,
164 * TRUE - Legacy only,
165 * TRUE_20 - HT 20 only,
166 * TRUE_40 - HT 40 only */
167
168/* 4ms frame limit not used for NG mode. The values filled
169 * for HT are the 64K max aggregate limit */
170
171static struct ath_rate_table ar5416_11ng_ratetable = {
172 46,
173 {
174 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 1000, /* 1 Mb */
175 900, 0x1b, 0x00, 2,
176 0, 0, 1, 0, 0, 0, 0, 0 },
177 { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 2000, /* 2 Mb */
178 1900, 0x1a, 0x04, 4,
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 */
211 6400, 0x80, 0x00, 0,
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 },
312 },
313 50, /* probe interval */
314 50, /* rssi reduce interval */
315 WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
316};
317
318static struct ath_rate_table ar5416_11a_ratetable = {
319 8,
320 {
321 { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
322 5400, 0x0b, 0x00, (0x80|12),
323 0, 2, 1, 0, 0 },
324 { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
325 7800, 0x0f, 0x00, 18,
326 0, 3, 1, 1, 0 },
327 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
328 10000, 0x0a, 0x00, (0x80|24),
329 2, 4, 2, 2, 0 },
330 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
331 13900, 0x0e, 0x00, 36,
332 2, 6, 2, 3, 0 },
333 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
334 17300, 0x09, 0x00, (0x80|48),
335 4, 10, 3, 4, 0 },
336 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
337 23000, 0x0d, 0x00, 72,
338 4, 14, 3, 5, 0 },
339 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
340 27400, 0x08, 0x00, 96,
341 4, 19, 3, 6, 0 },
342 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
343 29300, 0x0c, 0x00, 108,
344 4, 23, 3, 7, 0 },
345 },
346 50, /* probe interval */
347 50, /* rssi reduce interval */
348 0, /* Phy rates allowed initially */
349};
350
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700351static struct ath_rate_table ar5416_11g_ratetable = {
352 12,
353 {
354 { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
355 900, 0x1b, 0x00, 2,
356 0, 0, 1, 0, 0 },
357 { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
358 1900, 0x1a, 0x04, 4,
359 1, 1, 1, 1, 0 },
360 { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
361 4900, 0x19, 0x04, 11,
362 2, 2, 2, 2, 0 },
363 { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
364 8100, 0x18, 0x04, 22,
365 3, 3, 2, 3, 0 },
366 { FALSE, FALSE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
367 5400, 0x0b, 0x00, 12,
368 4, 2, 1, 4, 0 },
369 { FALSE, FALSE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
370 7800, 0x0f, 0x00, 18,
371 4, 3, 1, 5, 0 },
372 { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
373 10000, 0x0a, 0x00, 24,
374 6, 4, 1, 6, 0 },
375 { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
376 13900, 0x0e, 0x00, 36,
377 6, 6, 2, 7, 0 },
378 { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
379 17300, 0x09, 0x00, 48,
380 8, 10, 3, 8, 0 },
381 { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
382 23000, 0x0d, 0x00, 72,
383 8, 14, 3, 9, 0 },
384 { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
385 27400, 0x08, 0x00, 96,
386 8, 19, 3, 10, 0 },
387 { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
388 29300, 0x0c, 0x00, 108,
389 8, 23, 3, 11, 0 },
390 },
391 50, /* probe interval */
392 50, /* rssi reduce interval */
393 0, /* Phy rates allowed initially */
394};
395
396static struct ath_rate_table ar5416_11b_ratetable = {
397 4,
398 {
399 { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
400 900, 0x1b, 0x00, (0x80|2),
401 0, 0, 1, 0, 0 },
402 { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
403 1800, 0x1a, 0x04, (0x80|4),
404 1, 1, 1, 1, 0 },
405 { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
406 4300, 0x19, 0x04, (0x80|11),
407 1, 2, 2, 2, 0 },
408 { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
409 7100, 0x18, 0x04, (0x80|22),
410 1, 4, 100, 3, 0 },
411 },
412 100, /* probe interval */
413 100, /* rssi reduce interval */
414 0, /* Phy rates allowed initially */
415};
416
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700417/*
418 * Return the median of three numbers
419 */
420static inline int8_t median(int8_t a, int8_t b, int8_t c)
421{
422 if (a >= b) {
423 if (b >= c)
424 return b;
425 else if (a > c)
426 return c;
427 else
428 return a;
429 } else {
430 if (a >= c)
431 return a;
432 else if (b >= c)
433 return c;
434 else
435 return b;
436 }
437}
438
439static void ath_rc_sort_validrates(const struct ath_rate_table *rate_table,
Sujith256b77592008-11-18 09:03:12 +0530440 struct ath_rate_node *ath_rc_priv)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700441{
442 u8 i, j, idx, idx_next;
443
Sujith256b77592008-11-18 09:03:12 +0530444 for (i = ath_rc_priv->max_valid_rate - 1; i > 0; i--) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700445 for (j = 0; j <= i-1; j++) {
Sujith256b77592008-11-18 09:03:12 +0530446 idx = ath_rc_priv->valid_rate_index[j];
447 idx_next = ath_rc_priv->valid_rate_index[j+1];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700448
449 if (rate_table->info[idx].ratekbps >
450 rate_table->info[idx_next].ratekbps) {
Sujith256b77592008-11-18 09:03:12 +0530451 ath_rc_priv->valid_rate_index[j] = idx_next;
452 ath_rc_priv->valid_rate_index[j+1] = idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700453 }
454 }
455 }
456}
457
458/* Access functions for valid_txrate_mask */
459
Sujith256b77592008-11-18 09:03:12 +0530460static void ath_rc_init_valid_txmask(struct ath_rate_node *ath_rc_priv)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700461{
462 u8 i;
463
Sujith256b77592008-11-18 09:03:12 +0530464 for (i = 0; i < ath_rc_priv->rate_table_size; i++)
465 ath_rc_priv->valid_rate_index[i] = FALSE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700466}
467
Sujith256b77592008-11-18 09:03:12 +0530468static inline void ath_rc_set_valid_txmask(struct ath_rate_node *ath_rc_priv,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700469 u8 index, int valid_tx_rate)
470{
Sujith256b77592008-11-18 09:03:12 +0530471 ASSERT(index <= ath_rc_priv->rate_table_size);
472 ath_rc_priv->valid_rate_index[index] = valid_tx_rate ? TRUE : FALSE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700473}
474
Sujith256b77592008-11-18 09:03:12 +0530475static inline int ath_rc_isvalid_txmask(struct ath_rate_node *ath_rc_priv,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700476 u8 index)
477{
Sujith256b77592008-11-18 09:03:12 +0530478 ASSERT(index <= ath_rc_priv->rate_table_size);
479 return ath_rc_priv->valid_rate_index[index];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700480}
481
482/* Iterators for valid_txrate_mask */
483static inline int
484ath_rc_get_nextvalid_txrate(const struct ath_rate_table *rate_table,
Sujith256b77592008-11-18 09:03:12 +0530485 struct ath_rate_node *ath_rc_priv,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700486 u8 cur_valid_txrate,
487 u8 *next_idx)
488{
489 u8 i;
490
Sujith256b77592008-11-18 09:03:12 +0530491 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];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700494 return TRUE;
495 }
496 }
497
498 /* No more valid rates */
499 *next_idx = 0;
500 return FALSE;
501}
502
503/* Return true only for single stream */
504
505static int ath_rc_valid_phyrate(u32 phy, u32 capflag, int ignore_cw)
506{
507 if (WLAN_RC_PHY_HT(phy) & !(capflag & WLAN_RC_HT_FLAG))
508 return FALSE;
509 if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))
510 return FALSE;
511 if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_SGI_FLAG))
512 return FALSE;
513 if (!ignore_cw && WLAN_RC_PHY_HT(phy))
514 if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG))
515 return FALSE;
516 if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG))
517 return FALSE;
518 return TRUE;
519}
520
521static inline int
522ath_rc_get_nextlowervalid_txrate(const struct ath_rate_table *rate_table,
Sujith256b77592008-11-18 09:03:12 +0530523 struct ath_rate_node *ath_rc_priv,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700524 u8 cur_valid_txrate, u8 *next_idx)
525{
526 int8_t i;
527
Sujith256b77592008-11-18 09:03:12 +0530528 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];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700531 return TRUE;
532 }
533 }
534 return FALSE;
535}
536
537/*
538 * Initialize the Valid Rate Index from valid entries in Rate Table
539 */
540static u8
541ath_rc_sib_init_validrates(struct ath_rate_node *ath_rc_priv,
542 const struct ath_rate_table *rate_table,
543 u32 capflag)
544{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700545 u8 i, hi = 0;
546 u32 valid;
547
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700548 for (i = 0; i < rate_table->rate_cnt; i++) {
549 valid = (ath_rc_priv->single_stream ?
Sujithdc2222a2008-08-14 13:26:55 +0530550 rate_table->info[i].valid_single_stream :
551 rate_table->info[i].valid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700552 if (valid == TRUE) {
553 u32 phy = rate_table->info[i].phy;
554 u8 valid_rate_count = 0;
555
556 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
557 continue;
558
Sujith256b77592008-11-18 09:03:12 +0530559 valid_rate_count = ath_rc_priv->valid_phy_ratecnt[phy];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700560
Sujith256b77592008-11-18 09:03:12 +0530561 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);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700564 hi = A_MAX(hi, i);
565 }
566 }
567 return hi;
568}
569
570/*
571 * Initialize the Valid Rate Index from Rate Set
572 */
573static u8
574ath_rc_sib_setvalid_rates(struct ath_rate_node *ath_rc_priv,
575 const struct ath_rate_table *rate_table,
576 struct ath_rateset *rateset,
577 u32 capflag)
578{
579 /* XXX: Clean me up and make identation friendly */
580 u8 i, j, hi = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700581
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);
589
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 */
593
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)) {
601
602 u8 valid_rate_count = 0;
603
604 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
605 continue;
606
607 valid_rate_count =
Sujith256b77592008-11-18 09:03:12 +0530608 ath_rc_priv->valid_phy_ratecnt[phy];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700609
Sujith256b77592008-11-18 09:03:12 +0530610 ath_rc_priv->valid_phy_rateidx[phy]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700611 [valid_rate_count] = j;
Sujith256b77592008-11-18 09:03:12 +0530612 ath_rc_priv->valid_phy_ratecnt[phy] += 1;
613 ath_rc_set_valid_txmask(ath_rc_priv, j, TRUE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700614 hi = A_MAX(hi, j);
615 }
616 }
617 }
618 return hi;
619}
620
621static u8
622ath_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)
625{
626 u8 i, j, hi = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700627
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 ?
Sujithdc2222a2008-08-14 13:26:55 +0530633 rate_table->info[j].valid_single_stream :
634 rate_table->info[j].valid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700635
636 if (((((struct ath_rateset *)
Sujithdc2222a2008-08-14 13:26:55 +0530637 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))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641 continue;
642
643 if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
644 continue;
645
Sujith256b77592008-11-18 09:03:12 +0530646 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);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700650 hi = A_MAX(hi, j);
651 }
652 }
653 return hi;
654}
655
Sujith46494e62008-11-18 09:05:13 +0530656struct ath_rate_softc *ath_rate_attach(struct ath_softc *sc)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700657{
658 struct ath_rate_softc *asc;
659
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700660 asc = kzalloc(sizeof(struct ath_rate_softc), GFP_KERNEL);
661 if (asc == NULL)
662 return NULL;
663
Sujith46494e62008-11-18 09:05:13 +0530664 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;
667
668 asc->hw_rate_table[ATH9K_MODE_11NA_HT20] = &ar5416_11na_ratetable;
669 asc->hw_rate_table[ATH9K_MODE_11NG_HT20] = &ar5416_11ng_ratetable;
670
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;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700679
680 /* Save Maximum TX Trigger Level (used for 11n) */
Sujith46494e62008-11-18 09:05:13 +0530681 tx_triglevel_max = sc->sc_ah->ah_caps.tx_triglevel_max;
682
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700683 return asc;
684}
685
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700686void ath_rate_detach(struct ath_rate_softc *asc)
687{
688 if (asc != NULL)
689 kfree(asc);
690}
691
692u8 ath_rate_findrateix(struct ath_softc *sc,
Sujith102e0572008-10-29 10:15:16 +0530693 u8 dot11rate)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700694{
695 const struct ath_rate_table *ratetable;
696 struct ath_rate_softc *rsc = sc->sc_rc;
697 int i;
698
699 ratetable = rsc->hw_rate_table[sc->sc_curmode];
700
701 if (WARN_ON(!ratetable))
702 return 0;
703
704 for (i = 0; i < ratetable->rate_cnt; i++) {
705 if ((ratetable->info[i].dot11rate & 0x7f) == (dot11rate & 0x7f))
706 return i;
707 }
708
709 return 0;
710}
711
712/*
713 * Update rate-control state on a device state change. When
714 * operating as a station this includes associate/reassociate
715 * with an AP. Otherwise this gets called, for example, when
716 * the we transition to run state when operating as an AP.
717 */
718void ath_rate_newstate(struct ath_softc *sc, struct ath_vap *avp)
719{
720 struct ath_rate_softc *asc = sc->sc_rc;
721
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700722 if (avp->av_config.av_fixed_rateset != IEEE80211_FIXED_RATE_NONE) {
723 asc->fixedrix =
724 sc->sc_rixmap[avp->av_config.av_fixed_rateset & 0xff];
725 /* NB: check the fixed rate exists */
726 if (asc->fixedrix == 0xff)
727 asc->fixedrix = IEEE80211_FIXED_RATE_NONE;
728 } else {
729 asc->fixedrix = IEEE80211_FIXED_RATE_NONE;
730 }
731}
732
733static u8 ath_rc_ratefind_ht(struct ath_softc *sc,
Sujithdc2222a2008-08-14 13:26:55 +0530734 struct ath_rate_node *ath_rc_priv,
735 const struct ath_rate_table *rate_table,
736 int probe_allowed, int *is_probing,
737 int is_retry)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700738{
739 u32 dt, best_thruput, this_thruput, now_msec;
740 u8 rate, next_rate, best_rate, maxindex, minindex;
741 int8_t rssi_last, rssi_reduce = 0, index = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700742
743 *is_probing = FALSE;
744
Sujith256b77592008-11-18 09:03:12 +0530745 rssi_last = median(ath_rc_priv->rssi_last,
746 ath_rc_priv->rssi_last_prev,
747 ath_rc_priv->rssi_last_prev2);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700748
749 /*
750 * Age (reduce) last ack rssi based on how old it is.
751 * The bizarre numbers are so the delta is 160msec,
752 * meaning we divide by 16.
753 * 0msec <= dt <= 25msec: don't derate
754 * 25msec <= dt <= 185msec: derate linearly from 0 to 10dB
755 * 185msec <= dt: derate by 10dB
756 */
757
758 now_msec = jiffies_to_msecs(jiffies);
Sujith256b77592008-11-18 09:03:12 +0530759 dt = now_msec - ath_rc_priv->rssi_time;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700760
761 if (dt >= 185)
762 rssi_reduce = 10;
763 else if (dt >= 25)
764 rssi_reduce = (u8)((dt - 25) >> 4);
765
766 /* Now reduce rssi_last by rssi_reduce */
767 if (rssi_last < rssi_reduce)
768 rssi_last = 0;
769 else
770 rssi_last -= rssi_reduce;
771
772 /*
773 * Now look up the rate in the rssi table and return it.
774 * If no rates match then we return 0 (lowest rate)
775 */
776
777 best_thruput = 0;
Sujith256b77592008-11-18 09:03:12 +0530778 maxindex = ath_rc_priv->max_valid_rate-1;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700779
780 minindex = 0;
781 best_rate = minindex;
782
783 /*
784 * Try the higher rate first. It will reduce memory moving time
785 * if we have very good channel characteristics.
786 */
787 for (index = maxindex; index >= minindex ; index--) {
788 u8 per_thres;
789
Sujith256b77592008-11-18 09:03:12 +0530790 rate = ath_rc_priv->valid_rate_index[index];
791 if (rate > ath_rc_priv->rate_max_phy)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700792 continue;
793
794 /*
795 * For TCP the average collision rate is around 11%,
796 * so we ignore PERs less than this. This is to
797 * prevent the rate we are currently using (whose
798 * PER might be in the 10-15 range because of TCP
799 * collisions) looking worse than the next lower
800 * rate whose PER has decayed close to 0. If we
801 * used to next lower rate, its PER would grow to
802 * 10-15 and we would be worse off then staying
803 * at the current rate.
804 */
Sujith256b77592008-11-18 09:03:12 +0530805 per_thres = ath_rc_priv->state[rate].per;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700806 if (per_thres < 12)
807 per_thres = 12;
808
809 this_thruput = rate_table->info[rate].user_ratekbps *
810 (100 - per_thres);
811
812 if (best_thruput <= this_thruput) {
813 best_thruput = this_thruput;
814 best_rate = rate;
815 }
816 }
817
818 rate = best_rate;
819
820 /* if we are retrying for more than half the number
821 * of max retries, use the min rate for the next retry
822 */
823 if (is_retry)
Sujith256b77592008-11-18 09:03:12 +0530824 rate = ath_rc_priv->valid_rate_index[minindex];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700825
Sujith256b77592008-11-18 09:03:12 +0530826 ath_rc_priv->rssi_last_lookup = rssi_last;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700827
828 /*
829 * Must check the actual rate (ratekbps) to account for
830 * non-monoticity of 11g's rate table
831 */
832
Sujith256b77592008-11-18 09:03:12 +0530833 if (rate >= ath_rc_priv->rate_max_phy && probe_allowed) {
834 rate = ath_rc_priv->rate_max_phy;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700835
836 /* Probe the next allowed phy state */
837 /* FIXME:XXXX Check to make sure ratMax is checked properly */
838 if (ath_rc_get_nextvalid_txrate(rate_table,
Sujith256b77592008-11-18 09:03:12 +0530839 ath_rc_priv, rate, &next_rate) &&
840 (now_msec - ath_rc_priv->probe_time >
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700841 rate_table->probe_interval) &&
Sujith256b77592008-11-18 09:03:12 +0530842 (ath_rc_priv->hw_maxretry_pktcnt >= 1)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700843 rate = next_rate;
Sujith256b77592008-11-18 09:03:12 +0530844 ath_rc_priv->probe_rate = rate;
845 ath_rc_priv->probe_time = now_msec;
846 ath_rc_priv->hw_maxretry_pktcnt = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700847 *is_probing = TRUE;
848 }
849 }
850
851 /*
852 * Make sure rate is not higher than the allowed maximum.
853 * We should also enforce the min, but I suspect the min is
854 * normally 1 rather than 0 because of the rate 9 vs 6 issue
855 * in the old code.
856 */
Sujith256b77592008-11-18 09:03:12 +0530857 if (rate > (ath_rc_priv->rate_table_size - 1))
858 rate = ath_rc_priv->rate_table_size - 1;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700859
860 ASSERT((rate_table->info[rate].valid && !ath_rc_priv->single_stream) ||
Sujithdc2222a2008-08-14 13:26:55 +0530861 (rate_table->info[rate].valid_single_stream &&
862 ath_rc_priv->single_stream));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700863
864 return rate;
865}
866
867static void ath_rc_rate_set_series(const struct ath_rate_table *rate_table ,
868 struct ath_rc_series *series,
869 u8 tries,
870 u8 rix,
871 int rtsctsenable)
872{
873 series->tries = tries;
874 series->flags = (rtsctsenable ? ATH_RC_RTSCTS_FLAG : 0) |
875 (WLAN_RC_PHY_DS(rate_table->info[rix].phy) ?
876 ATH_RC_DS_FLAG : 0) |
877 (WLAN_RC_PHY_40(rate_table->info[rix].phy) ?
878 ATH_RC_CW40_FLAG : 0) |
879 (WLAN_RC_PHY_SGI(rate_table->info[rix].phy) ?
880 ATH_RC_SGI_FLAG : 0);
881
882 series->rix = rate_table->info[rix].base_index;
883 series->max_4ms_framelen = rate_table->info[rix].max_4ms_framelen;
884}
885
886static u8 ath_rc_rate_getidx(struct ath_softc *sc,
Sujithdc2222a2008-08-14 13:26:55 +0530887 struct ath_rate_node *ath_rc_priv,
888 const struct ath_rate_table *rate_table,
889 u8 rix, u16 stepdown,
890 u16 min_rate)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700891{
892 u32 j;
893 u8 nextindex;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700894
895 if (min_rate) {
896 for (j = RATE_TABLE_SIZE; j > 0; j--) {
897 if (ath_rc_get_nextlowervalid_txrate(rate_table,
Sujith256b77592008-11-18 09:03:12 +0530898 ath_rc_priv, rix, &nextindex))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700899 rix = nextindex;
900 else
901 break;
902 }
903 } else {
904 for (j = stepdown; j > 0; j--) {
905 if (ath_rc_get_nextlowervalid_txrate(rate_table,
Sujith256b77592008-11-18 09:03:12 +0530906 ath_rc_priv, rix, &nextindex))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700907 rix = nextindex;
908 else
909 break;
910 }
911 }
912 return rix;
913}
914
915static void ath_rc_ratefind(struct ath_softc *sc,
916 struct ath_rate_node *ath_rc_priv,
917 int num_tries, int num_rates, unsigned int rcflag,
918 struct ath_rc_series series[], int *is_probe,
919 int is_retry)
920{
921 u8 try_per_rate = 0, i = 0, rix, nrix;
922 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
923 struct ath_rate_table *rate_table;
924
925 rate_table =
926 (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
927 rix = ath_rc_ratefind_ht(sc, ath_rc_priv, rate_table,
Sujithdc2222a2008-08-14 13:26:55 +0530928 (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
929 is_probe, is_retry);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700930 nrix = rix;
931
932 if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*is_probe)) {
933 /* set one try for probe rates. For the
934 * probes don't enable rts */
935 ath_rc_rate_set_series(rate_table,
936 &series[i++], 1, nrix, FALSE);
937
938 try_per_rate = (num_tries/num_rates);
939 /* Get the next tried/allowed rate. No RTS for the next series
940 * after the probe rate
941 */
942 nrix = ath_rc_rate_getidx(sc,
943 ath_rc_priv, rate_table, nrix, 1, FALSE);
944 ath_rc_rate_set_series(rate_table,
945 &series[i++], try_per_rate, nrix, 0);
946 } else {
947 try_per_rate = (num_tries/num_rates);
948 /* Set the choosen rate. No RTS for first series entry. */
949 ath_rc_rate_set_series(rate_table,
950 &series[i++], try_per_rate, nrix, FALSE);
951 }
952
953 /* Fill in the other rates for multirate retry */
954 for ( ; i < num_rates; i++) {
955 u8 try_num;
956 u8 min_rate;
957
958 try_num = ((i + 1) == num_rates) ?
959 num_tries - (try_per_rate * i) : try_per_rate ;
960 min_rate = (((i + 1) == num_rates) &&
Sujithdc2222a2008-08-14 13:26:55 +0530961 (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700962
963 nrix = ath_rc_rate_getidx(sc, ath_rc_priv,
Sujithdc2222a2008-08-14 13:26:55 +0530964 rate_table, nrix, 1, min_rate);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700965 /* All other rates in the series have RTS enabled */
966 ath_rc_rate_set_series(rate_table,
Sujithdc2222a2008-08-14 13:26:55 +0530967 &series[i], try_num, nrix, TRUE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700968 }
969
970 /*
971 * NB:Change rate series to enable aggregation when operating
972 * at lower MCS rates. When first rate in series is MCS2
973 * in HT40 @ 2.4GHz, series should look like:
974 *
975 * {MCS2, MCS1, MCS0, MCS0}.
976 *
977 * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
978 * look like:
979 *
980 * {MCS3, MCS2, MCS1, MCS1}
981 *
982 * So, set fourth rate in series to be same as third one for
983 * above conditions.
984 */
Sujith86b89ee2008-08-07 10:54:57 +0530985 if ((sc->sc_curmode == ATH9K_MODE_11NG_HT20) ||
Sujithdc2222a2008-08-14 13:26:55 +0530986 (sc->sc_curmode == ATH9K_MODE_11NG_HT40PLUS) ||
987 (sc->sc_curmode == ATH9K_MODE_11NG_HT40MINUS)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700988 u8 dot11rate = rate_table->info[rix].dot11rate;
989 u8 phy = rate_table->info[rix].phy;
990 if (i == 4 &&
991 ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
Sujithdc2222a2008-08-14 13:26:55 +0530992 (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700993 series[3].rix = series[2].rix;
994 series[3].flags = series[2].flags;
995 series[3].max_4ms_framelen = series[2].max_4ms_framelen;
996 }
997 }
998}
999
1000/*
1001 * Return the Tx rate series.
1002 */
Sujith5701ed82008-08-26 08:11:26 +05301003static void ath_rate_findrate(struct ath_softc *sc,
1004 struct ath_rate_node *ath_rc_priv,
1005 int num_tries,
1006 int num_rates,
1007 unsigned int rcflag,
1008 struct ath_rc_series series[],
1009 int *is_probe,
1010 int is_retry)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001011{
1012 struct ath_vap *avp = ath_rc_priv->avp;
1013
Sujithdc2222a2008-08-14 13:26:55 +05301014 DPRINTF(sc, ATH_DBG_RATE, "%s\n", __func__);
1015
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001016 if (!num_rates || !num_tries)
1017 return;
1018
1019 if (avp->av_config.av_fixed_rateset == IEEE80211_FIXED_RATE_NONE) {
1020 ath_rc_ratefind(sc, ath_rc_priv, num_tries, num_rates,
1021 rcflag, series, is_probe, is_retry);
1022 } else {
1023 /* Fixed rate */
1024 int idx;
1025 u8 flags;
1026 u32 rix;
1027 struct ath_rate_softc *asc = ath_rc_priv->asc;
1028 struct ath_rate_table *rate_table;
1029
1030 rate_table = (struct ath_rate_table *)
1031 asc->hw_rate_table[sc->sc_curmode];
1032
1033 for (idx = 0; idx < 4; idx++) {
1034 unsigned int mcs;
1035 u8 series_rix = 0;
1036
Sujithdc2222a2008-08-14 13:26:55 +05301037 series[idx].tries = IEEE80211_RATE_IDX_ENTRY(
1038 avp->av_config.av_fixed_retryset, idx);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001039
1040 mcs = IEEE80211_RATE_IDX_ENTRY(
1041 avp->av_config.av_fixed_rateset, idx);
1042
1043 if (idx == 3 && (mcs & 0xf0) == 0x70)
1044 mcs = (mcs & ~0xf0)|0x80;
1045
1046 if (!(mcs & 0x80))
1047 flags = 0;
1048 else
1049 flags = ((ath_rc_priv->ht_cap &
1050 WLAN_RC_DS_FLAG) ?
1051 ATH_RC_DS_FLAG : 0) |
1052 ((ath_rc_priv->ht_cap &
1053 WLAN_RC_40_FLAG) ?
1054 ATH_RC_CW40_FLAG : 0) |
1055 ((ath_rc_priv->ht_cap &
1056 WLAN_RC_SGI_FLAG) ?
1057 ((ath_rc_priv->ht_cap &
1058 WLAN_RC_40_FLAG) ?
1059 ATH_RC_SGI_FLAG : 0) : 0);
1060
1061 series[idx].rix = sc->sc_rixmap[mcs];
1062 series_rix = series[idx].rix;
1063
1064 /* XXX: Give me some cleanup love */
1065 if ((flags & ATH_RC_CW40_FLAG) &&
1066 (flags & ATH_RC_SGI_FLAG))
1067 rix = rate_table->info[series_rix].ht_index;
1068 else if (flags & ATH_RC_SGI_FLAG)
1069 rix = rate_table->info[series_rix].sgi_index;
1070 else if (flags & ATH_RC_CW40_FLAG)
1071 rix = rate_table->info[series_rix].cw40index;
1072 else
1073 rix = rate_table->info[series_rix].base_index;
1074 series[idx].max_4ms_framelen =
1075 rate_table->info[rix].max_4ms_framelen;
1076 series[idx].flags = flags;
1077 }
1078 }
1079}
1080
1081static void ath_rc_update_ht(struct ath_softc *sc,
1082 struct ath_rate_node *ath_rc_priv,
1083 struct ath_tx_info_priv *info_priv,
1084 int tx_rate, int xretries, int retries)
1085{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001086 u32 now_msec = jiffies_to_msecs(jiffies);
1087 int state_change = FALSE, rate, count;
1088 u8 last_per;
Sujithdc2222a2008-08-14 13:26:55 +05301089 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001090 struct ath_rate_table *rate_table =
1091 (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
1092
1093 static u32 nretry_to_per_lookup[10] = {
1094 100 * 0 / 1,
1095 100 * 1 / 4,
1096 100 * 1 / 2,
1097 100 * 3 / 4,
1098 100 * 4 / 5,
1099 100 * 5 / 6,
1100 100 * 6 / 7,
1101 100 * 7 / 8,
1102 100 * 8 / 9,
1103 100 * 9 / 10
1104 };
1105
1106 if (!ath_rc_priv)
1107 return;
1108
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001109 ASSERT(tx_rate >= 0);
1110 if (tx_rate < 0)
1111 return;
1112
1113 /* To compensate for some imbalance between ctrl and ext. channel */
1114
1115 if (WLAN_RC_PHY_40(rate_table->info[tx_rate].phy))
1116 info_priv->tx.ts_rssi =
1117 info_priv->tx.ts_rssi < 3 ? 0 :
1118 info_priv->tx.ts_rssi - 3;
1119
Sujith256b77592008-11-18 09:03:12 +05301120 last_per = ath_rc_priv->state[tx_rate].per;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001121
1122 if (xretries) {
1123 /* Update the PER. */
1124 if (xretries == 1) {
Sujith256b77592008-11-18 09:03:12 +05301125 ath_rc_priv->state[tx_rate].per += 30;
1126 if (ath_rc_priv->state[tx_rate].per > 100)
1127 ath_rc_priv->state[tx_rate].per = 100;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001128 } else {
1129 /* xretries == 2 */
Julia Lawall87c16872008-11-09 17:56:10 +01001130 count = ARRAY_SIZE(nretry_to_per_lookup);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001131 if (retries >= count)
1132 retries = count - 1;
1133 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
Sujith256b77592008-11-18 09:03:12 +05301134 ath_rc_priv->state[tx_rate].per =
1135 (u8)(ath_rc_priv->state[tx_rate].per -
1136 (ath_rc_priv->state[tx_rate].per >> 3) +
Sujithdc2222a2008-08-14 13:26:55 +05301137 ((100) >> 3));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001138 }
1139
1140 /* xretries == 1 or 2 */
1141
Sujith256b77592008-11-18 09:03:12 +05301142 if (ath_rc_priv->probe_rate == tx_rate)
1143 ath_rc_priv->probe_rate = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001144
1145 } else { /* xretries == 0 */
1146 /* Update the PER. */
1147 /* Make sure it doesn't index out of array's bounds. */
Julia Lawall87c16872008-11-09 17:56:10 +01001148 count = ARRAY_SIZE(nretry_to_per_lookup);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001149 if (retries >= count)
1150 retries = count - 1;
1151 if (info_priv->n_bad_frames) {
Sujithdc2222a2008-08-14 13:26:55 +05301152 /* new_PER = 7/8*old_PER + 1/8*(currentPER)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001153 * Assuming that n_frames is not 0. The current PER
1154 * from the retries is 100 * retries / (retries+1),
1155 * since the first retries attempts failed, and the
1156 * next one worked. For the one that worked,
1157 * n_bad_frames subframes out of n_frames wored,
1158 * so the PER for that part is
1159 * 100 * n_bad_frames / n_frames, and it contributes
1160 * 100 * n_bad_frames / (n_frames * (retries+1)) to
1161 * the above PER. The expression below is a
1162 * simplified version of the sum of these two terms.
1163 */
1164 if (info_priv->n_frames > 0)
Sujith256b77592008-11-18 09:03:12 +05301165 ath_rc_priv->state[tx_rate].per
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001166 = (u8)
Sujith256b77592008-11-18 09:03:12 +05301167 (ath_rc_priv->state[tx_rate].per -
1168 (ath_rc_priv->state[tx_rate].per >> 3) +
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001169 ((100*(retries*info_priv->n_frames +
1170 info_priv->n_bad_frames) /
1171 (info_priv->n_frames *
1172 (retries+1))) >> 3));
1173 } else {
1174 /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
1175
Sujith256b77592008-11-18 09:03:12 +05301176 ath_rc_priv->state[tx_rate].per = (u8)
1177 (ath_rc_priv->state[tx_rate].per -
1178 (ath_rc_priv->state[tx_rate].per >> 3) +
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001179 (nretry_to_per_lookup[retries] >> 3));
1180 }
1181
Sujith256b77592008-11-18 09:03:12 +05301182 ath_rc_priv->rssi_last_prev2 = ath_rc_priv->rssi_last_prev;
1183 ath_rc_priv->rssi_last_prev = ath_rc_priv->rssi_last;
1184 ath_rc_priv->rssi_last = info_priv->tx.ts_rssi;
1185 ath_rc_priv->rssi_time = now_msec;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001186
1187 /*
1188 * If we got at most one retry then increase the max rate if
1189 * this was a probe. Otherwise, ignore the probe.
1190 */
1191
Sujith256b77592008-11-18 09:03:12 +05301192 if (ath_rc_priv->probe_rate && ath_rc_priv->probe_rate == tx_rate) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001193 if (retries > 0 || 2 * info_priv->n_bad_frames >
1194 info_priv->n_frames) {
1195 /*
1196 * Since we probed with just a single attempt,
1197 * any retries means the probe failed. Also,
1198 * if the attempt worked, but more than half
1199 * the subframes were bad then also consider
1200 * the probe a failure.
1201 */
Sujith256b77592008-11-18 09:03:12 +05301202 ath_rc_priv->probe_rate = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001203 } else {
1204 u8 probe_rate = 0;
1205
Sujith256b77592008-11-18 09:03:12 +05301206 ath_rc_priv->rate_max_phy = ath_rc_priv->probe_rate;
1207 probe_rate = ath_rc_priv->probe_rate;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001208
Sujith256b77592008-11-18 09:03:12 +05301209 if (ath_rc_priv->state[probe_rate].per > 30)
1210 ath_rc_priv->state[probe_rate].per = 20;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001211
Sujith256b77592008-11-18 09:03:12 +05301212 ath_rc_priv->probe_rate = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001213
1214 /*
1215 * Since this probe succeeded, we allow the next
1216 * probe twice as soon. This allows the maxRate
1217 * to move up faster if the probes are
1218 * succesful.
1219 */
Sujith256b77592008-11-18 09:03:12 +05301220 ath_rc_priv->probe_time = now_msec -
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001221 rate_table->probe_interval / 2;
1222 }
1223 }
1224
1225 if (retries > 0) {
1226 /*
1227 * Don't update anything. We don't know if
1228 * this was because of collisions or poor signal.
1229 *
1230 * Later: if rssi_ack is close to
Sujith256b77592008-11-18 09:03:12 +05301231 * ath_rc_priv->state[txRate].rssi_thres and we see lots
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001232 * of retries, then we could increase
Sujith256b77592008-11-18 09:03:12 +05301233 * ath_rc_priv->state[txRate].rssi_thres.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001234 */
Sujith256b77592008-11-18 09:03:12 +05301235 ath_rc_priv->hw_maxretry_pktcnt = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001236 } else {
1237 /*
1238 * It worked with no retries. First ignore bogus (small)
1239 * rssi_ack values.
1240 */
Sujith256b77592008-11-18 09:03:12 +05301241 if (tx_rate == ath_rc_priv->rate_max_phy &&
1242 ath_rc_priv->hw_maxretry_pktcnt < 255) {
1243 ath_rc_priv->hw_maxretry_pktcnt++;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001244 }
1245
1246 if (info_priv->tx.ts_rssi >=
1247 rate_table->info[tx_rate].rssi_ack_validmin) {
1248 /* Average the rssi */
Sujith256b77592008-11-18 09:03:12 +05301249 if (tx_rate != ath_rc_priv->rssi_sum_rate) {
1250 ath_rc_priv->rssi_sum_rate = tx_rate;
1251 ath_rc_priv->rssi_sum =
1252 ath_rc_priv->rssi_sum_cnt = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001253 }
1254
Sujith256b77592008-11-18 09:03:12 +05301255 ath_rc_priv->rssi_sum += info_priv->tx.ts_rssi;
1256 ath_rc_priv->rssi_sum_cnt++;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001257
Sujith256b77592008-11-18 09:03:12 +05301258 if (ath_rc_priv->rssi_sum_cnt > 4) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001259 int32_t rssi_ackAvg =
Sujith256b77592008-11-18 09:03:12 +05301260 (ath_rc_priv->rssi_sum + 2) / 4;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001261 int8_t rssi_thres =
Sujith256b77592008-11-18 09:03:12 +05301262 ath_rc_priv->state[tx_rate].
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001263 rssi_thres;
1264 int8_t rssi_ack_vmin =
1265 rate_table->info[tx_rate].
1266 rssi_ack_validmin;
1267
Sujith256b77592008-11-18 09:03:12 +05301268 ath_rc_priv->rssi_sum =
1269 ath_rc_priv->rssi_sum_cnt = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001270
1271 /* Now reduce the current
1272 * rssi threshold. */
1273 if ((rssi_ackAvg < rssi_thres + 2) &&
Sujithdc2222a2008-08-14 13:26:55 +05301274 (rssi_thres > rssi_ack_vmin)) {
Sujith256b77592008-11-18 09:03:12 +05301275 ath_rc_priv->state[tx_rate].
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001276 rssi_thres--;
1277 }
1278
1279 state_change = TRUE;
1280 }
1281 }
1282 }
1283 }
1284
1285 /* For all cases */
1286
1287 /*
1288 * If this rate looks bad (high PER) then stop using it for
1289 * a while (except if we are probing).
1290 */
Sujith256b77592008-11-18 09:03:12 +05301291 if (ath_rc_priv->state[tx_rate].per >= 55 && tx_rate > 0 &&
Sujithdc2222a2008-08-14 13:26:55 +05301292 rate_table->info[tx_rate].ratekbps <=
Sujith256b77592008-11-18 09:03:12 +05301293 rate_table->info[ath_rc_priv->rate_max_phy].ratekbps) {
1294 ath_rc_get_nextlowervalid_txrate(rate_table, ath_rc_priv,
1295 (u8) tx_rate, &ath_rc_priv->rate_max_phy);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001296
1297 /* Don't probe for a little while. */
Sujith256b77592008-11-18 09:03:12 +05301298 ath_rc_priv->probe_time = now_msec;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001299 }
1300
1301 if (state_change) {
1302 /*
1303 * Make sure the rates above this have higher rssi thresholds.
1304 * (Note: Monotonicity is kept within the OFDM rates and
1305 * within the CCK rates. However, no adjustment is
1306 * made to keep the rssi thresholds monotonically
1307 * increasing between the CCK and OFDM rates.)
1308 */
1309 for (rate = tx_rate; rate <
Sujith256b77592008-11-18 09:03:12 +05301310 ath_rc_priv->rate_table_size - 1; rate++) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001311 if (rate_table->info[rate+1].phy !=
1312 rate_table->info[tx_rate].phy)
1313 break;
1314
Sujith256b77592008-11-18 09:03:12 +05301315 if (ath_rc_priv->state[rate].rssi_thres +
Sujithdc2222a2008-08-14 13:26:55 +05301316 rate_table->info[rate].rssi_ack_deltamin >
Sujith256b77592008-11-18 09:03:12 +05301317 ath_rc_priv->state[rate+1].rssi_thres) {
1318 ath_rc_priv->state[rate+1].rssi_thres =
1319 ath_rc_priv->state[rate].
Sujithdc2222a2008-08-14 13:26:55 +05301320 rssi_thres +
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001321 rate_table->info[rate].
Sujithdc2222a2008-08-14 13:26:55 +05301322 rssi_ack_deltamin;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001323 }
1324 }
1325
1326 /* Make sure the rates below this have lower rssi thresholds. */
1327 for (rate = tx_rate - 1; rate >= 0; rate--) {
1328 if (rate_table->info[rate].phy !=
Sujithdc2222a2008-08-14 13:26:55 +05301329 rate_table->info[tx_rate].phy)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001330 break;
1331
Sujith256b77592008-11-18 09:03:12 +05301332 if (ath_rc_priv->state[rate].rssi_thres +
Sujithdc2222a2008-08-14 13:26:55 +05301333 rate_table->info[rate].rssi_ack_deltamin >
Sujith256b77592008-11-18 09:03:12 +05301334 ath_rc_priv->state[rate+1].rssi_thres) {
1335 if (ath_rc_priv->state[rate+1].rssi_thres <
Sujithdc2222a2008-08-14 13:26:55 +05301336 rate_table->info[rate].
1337 rssi_ack_deltamin)
Sujith256b77592008-11-18 09:03:12 +05301338 ath_rc_priv->state[rate].rssi_thres = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001339 else {
Sujith256b77592008-11-18 09:03:12 +05301340 ath_rc_priv->state[rate].rssi_thres =
1341 ath_rc_priv->state[rate+1].
Sujithdc2222a2008-08-14 13:26:55 +05301342 rssi_thres -
1343 rate_table->info[rate].
1344 rssi_ack_deltamin;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001345 }
1346
Sujith256b77592008-11-18 09:03:12 +05301347 if (ath_rc_priv->state[rate].rssi_thres <
Sujithdc2222a2008-08-14 13:26:55 +05301348 rate_table->info[rate].
1349 rssi_ack_validmin) {
Sujith256b77592008-11-18 09:03:12 +05301350 ath_rc_priv->state[rate].rssi_thres =
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001351 rate_table->info[rate].
Sujithdc2222a2008-08-14 13:26:55 +05301352 rssi_ack_validmin;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001353 }
1354 }
1355 }
1356 }
1357
1358 /* Make sure the rates below this have lower PER */
1359 /* Monotonicity is kept only for rates below the current rate. */
Sujith256b77592008-11-18 09:03:12 +05301360 if (ath_rc_priv->state[tx_rate].per < last_per) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001361 for (rate = tx_rate - 1; rate >= 0; rate--) {
1362 if (rate_table->info[rate].phy !=
Sujithdc2222a2008-08-14 13:26:55 +05301363 rate_table->info[tx_rate].phy)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001364 break;
1365
Sujith256b77592008-11-18 09:03:12 +05301366 if (ath_rc_priv->state[rate].per >
1367 ath_rc_priv->state[rate+1].per) {
1368 ath_rc_priv->state[rate].per =
1369 ath_rc_priv->state[rate+1].per;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001370 }
1371 }
1372 }
1373
1374 /* Maintain monotonicity for rates above the current rate */
Sujith256b77592008-11-18 09:03:12 +05301375 for (rate = tx_rate; rate < ath_rc_priv->rate_table_size - 1; rate++) {
1376 if (ath_rc_priv->state[rate+1].per < ath_rc_priv->state[rate].per)
1377 ath_rc_priv->state[rate+1].per =
1378 ath_rc_priv->state[rate].per;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001379 }
1380
1381 /* Every so often, we reduce the thresholds and
1382 * PER (different for CCK and OFDM). */
Sujith256b77592008-11-18 09:03:12 +05301383 if (now_msec - ath_rc_priv->rssi_down_time >=
Sujithdc2222a2008-08-14 13:26:55 +05301384 rate_table->rssi_reduce_interval) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001385
Sujith256b77592008-11-18 09:03:12 +05301386 for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
1387 if (ath_rc_priv->state[rate].rssi_thres >
Sujithdc2222a2008-08-14 13:26:55 +05301388 rate_table->info[rate].rssi_ack_validmin)
Sujith256b77592008-11-18 09:03:12 +05301389 ath_rc_priv->state[rate].rssi_thres -= 1;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001390 }
Sujith256b77592008-11-18 09:03:12 +05301391 ath_rc_priv->rssi_down_time = now_msec;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001392 }
1393
1394 /* Every so often, we reduce the thresholds
1395 * and PER (different for CCK and OFDM). */
Sujith256b77592008-11-18 09:03:12 +05301396 if (now_msec - ath_rc_priv->per_down_time >=
Sujithdc2222a2008-08-14 13:26:55 +05301397 rate_table->rssi_reduce_interval) {
Sujith256b77592008-11-18 09:03:12 +05301398 for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
1399 ath_rc_priv->state[rate].per =
1400 7 * ath_rc_priv->state[rate].per / 8;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001401 }
1402
Sujith256b77592008-11-18 09:03:12 +05301403 ath_rc_priv->per_down_time = now_msec;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001404 }
1405}
1406
1407/*
1408 * This routine is called in rate control callback tx_status() to give
1409 * the status of previous frames.
1410 */
1411static void ath_rc_update(struct ath_softc *sc,
1412 struct ath_rate_node *ath_rc_priv,
1413 struct ath_tx_info_priv *info_priv, int final_ts_idx,
1414 int xretries, int long_retry)
1415{
Sujithdc2222a2008-08-14 13:26:55 +05301416 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001417 struct ath_rate_table *rate_table;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001418 struct ath_rc_series rcs[4];
1419 u8 flags;
1420 u32 series = 0, rix;
1421
1422 memcpy(rcs, info_priv->rcs, 4 * sizeof(rcs[0]));
1423 rate_table = (struct ath_rate_table *)
1424 asc->hw_rate_table[sc->sc_curmode];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001425 ASSERT(rcs[0].tries != 0);
1426
1427 /*
1428 * If the first rate is not the final index, there
1429 * are intermediate rate failures to be processed.
1430 */
1431 if (final_ts_idx != 0) {
1432 /* Process intermediate rates that failed.*/
1433 for (series = 0; series < final_ts_idx ; series++) {
1434 if (rcs[series].tries != 0) {
1435 flags = rcs[series].flags;
1436 /* If HT40 and we have switched mode from
1437 * 40 to 20 => don't update */
1438 if ((flags & ATH_RC_CW40_FLAG) &&
Sujith256b77592008-11-18 09:03:12 +05301439 (ath_rc_priv->rc_phy_mode !=
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001440 (flags & ATH_RC_CW40_FLAG)))
1441 return;
1442 if ((flags & ATH_RC_CW40_FLAG) &&
1443 (flags & ATH_RC_SGI_FLAG))
1444 rix = rate_table->info[
1445 rcs[series].rix].ht_index;
1446 else if (flags & ATH_RC_SGI_FLAG)
1447 rix = rate_table->info[
1448 rcs[series].rix].sgi_index;
1449 else if (flags & ATH_RC_CW40_FLAG)
1450 rix = rate_table->info[
1451 rcs[series].rix].cw40index;
1452 else
1453 rix = rate_table->info[
1454 rcs[series].rix].base_index;
1455 ath_rc_update_ht(sc, ath_rc_priv,
1456 info_priv, rix,
1457 xretries ? 1 : 2,
1458 rcs[series].tries);
1459 }
1460 }
1461 } else {
1462 /*
1463 * Handle the special case of MIMO PS burst, where the second
1464 * aggregate is sent out with only one rate and one try.
1465 * Treating it as an excessive retry penalizes the rate
1466 * inordinately.
1467 */
1468 if (rcs[0].tries == 1 && xretries == 1)
1469 xretries = 2;
1470 }
1471
1472 flags = rcs[series].flags;
1473 /* If HT40 and we have switched mode from 40 to 20 => don't update */
1474 if ((flags & ATH_RC_CW40_FLAG) &&
Sujith256b77592008-11-18 09:03:12 +05301475 (ath_rc_priv->rc_phy_mode != (flags & ATH_RC_CW40_FLAG)))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001476 return;
1477
1478 if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_SGI_FLAG))
1479 rix = rate_table->info[rcs[series].rix].ht_index;
1480 else if (flags & ATH_RC_SGI_FLAG)
1481 rix = rate_table->info[rcs[series].rix].sgi_index;
1482 else if (flags & ATH_RC_CW40_FLAG)
1483 rix = rate_table->info[rcs[series].rix].cw40index;
1484 else
1485 rix = rate_table->info[rcs[series].rix].base_index;
1486
1487 ath_rc_update_ht(sc, ath_rc_priv, info_priv, rix,
1488 xretries, long_retry);
1489}
1490
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001491/*
1492 * Process a tx descriptor for a completed transmit (success or failure).
1493 */
1494static void ath_rate_tx_complete(struct ath_softc *sc,
1495 struct ath_node *an,
1496 struct ath_rate_node *rc_priv,
1497 struct ath_tx_info_priv *info_priv)
1498{
1499 int final_ts_idx = info_priv->tx.ts_rateindex;
1500 int tx_status = 0, is_underrun = 0;
1501 struct ath_vap *avp;
1502
1503 avp = rc_priv->avp;
Sujithdc2222a2008-08-14 13:26:55 +05301504 if ((avp->av_config.av_fixed_rateset != IEEE80211_FIXED_RATE_NONE) ||
1505 (info_priv->tx.ts_status & ATH9K_TXERR_FILT))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001506 return;
1507
1508 if (info_priv->tx.ts_rssi > 0) {
1509 ATH_RSSI_LPF(an->an_chainmask_sel.tx_avgrssi,
Sujithdc2222a2008-08-14 13:26:55 +05301510 info_priv->tx.ts_rssi);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001511 }
1512
1513 /*
1514 * If underrun error is seen assume it as an excessive retry only
1515 * if prefetch trigger level have reached the max (0x3f for 5416)
1516 * Adjust the long retry as if the frame was tried ATH_11N_TXMAXTRY
1517 * times. This affects how ratectrl updates PER for the failed rate.
1518 */
1519 if (info_priv->tx.ts_flags &
1520 (ATH9K_TX_DATA_UNDERRUN | ATH9K_TX_DELIM_UNDERRUN) &&
1521 ((sc->sc_ah->ah_txTrigLevel) >= tx_triglevel_max)) {
1522 tx_status = 1;
1523 is_underrun = 1;
1524 }
1525
1526 if ((info_priv->tx.ts_status & ATH9K_TXERR_XRETRY) ||
1527 (info_priv->tx.ts_status & ATH9K_TXERR_FIFO))
1528 tx_status = 1;
1529
1530 ath_rc_update(sc, rc_priv, info_priv, final_ts_idx, tx_status,
1531 (is_underrun) ? ATH_11N_TXMAXTRY :
1532 info_priv->tx.ts_longretry);
1533}
1534
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001535/*
1536 * Update the SIB's rate control information
1537 *
1538 * This should be called when the supported rates change
1539 * (e.g. SME operation, wireless mode change)
1540 *
1541 * It will determine which rates are valid for use.
1542 */
1543static void ath_rc_sib_update(struct ath_softc *sc,
1544 struct ath_rate_node *ath_rc_priv,
1545 u32 capflag, int keep_state,
1546 struct ath_rateset *negotiated_rates,
1547 struct ath_rateset *negotiated_htrates)
1548{
1549 struct ath_rate_table *rate_table = NULL;
1550 struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
1551 struct ath_rateset *rateset = negotiated_rates;
1552 u8 *ht_mcs = (u8 *)negotiated_htrates;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001553 u8 i, j, k, hi = 0, hthi = 0;
1554
1555 rate_table = (struct ath_rate_table *)
1556 asc->hw_rate_table[sc->sc_curmode];
1557
1558 /* Initial rate table size. Will change depending
1559 * on the working rate set */
Sujith256b77592008-11-18 09:03:12 +05301560 ath_rc_priv->rate_table_size = MAX_TX_RATE_TBL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001561
1562 /* Initialize thresholds according to the global rate table */
Sujith256b77592008-11-18 09:03:12 +05301563 for (i = 0 ; (i < ath_rc_priv->rate_table_size) && (!keep_state); i++) {
1564 ath_rc_priv->state[i].rssi_thres =
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001565 rate_table->info[i].rssi_ack_validmin;
Sujith256b77592008-11-18 09:03:12 +05301566 ath_rc_priv->state[i].per = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001567 }
1568
1569 /* Determine the valid rates */
Sujith256b77592008-11-18 09:03:12 +05301570 ath_rc_init_valid_txmask(ath_rc_priv);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001571
1572 for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
1573 for (j = 0; j < MAX_TX_RATE_PHY; j++)
Sujith256b77592008-11-18 09:03:12 +05301574 ath_rc_priv->valid_phy_rateidx[i][j] = 0;
1575 ath_rc_priv->valid_phy_ratecnt[i] = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001576 }
Sujith256b77592008-11-18 09:03:12 +05301577 ath_rc_priv->rc_phy_mode = (capflag & WLAN_RC_40_FLAG);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001578
1579 /* Set stream capability */
1580 ath_rc_priv->single_stream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
1581
1582 if (!rateset->rs_nrates) {
1583 /* No working rate, just initialize valid rates */
1584 hi = ath_rc_sib_init_validrates(ath_rc_priv, rate_table,
1585 capflag);
1586 } else {
1587 /* Use intersection of working rates and valid rates */
1588 hi = ath_rc_sib_setvalid_rates(ath_rc_priv, rate_table,
1589 rateset, capflag);
1590 if (capflag & WLAN_RC_HT_FLAG) {
1591 hthi = ath_rc_sib_setvalid_htrates(ath_rc_priv,
1592 rate_table,
1593 ht_mcs,
1594 capflag);
1595 }
1596 hi = A_MAX(hi, hthi);
1597 }
1598
Sujith256b77592008-11-18 09:03:12 +05301599 ath_rc_priv->rate_table_size = hi + 1;
1600 ath_rc_priv->rate_max_phy = 0;
1601 ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001602
1603 for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
Sujith256b77592008-11-18 09:03:12 +05301604 for (j = 0; j < ath_rc_priv->valid_phy_ratecnt[i]; j++) {
1605 ath_rc_priv->valid_rate_index[k++] =
1606 ath_rc_priv->valid_phy_rateidx[i][j];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001607 }
1608
1609 if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, TRUE)
Sujith256b77592008-11-18 09:03:12 +05301610 || !ath_rc_priv->valid_phy_ratecnt[i])
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001611 continue;
1612
Sujith256b77592008-11-18 09:03:12 +05301613 ath_rc_priv->rate_max_phy = ath_rc_priv->valid_phy_rateidx[i][j-1];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001614 }
Sujith256b77592008-11-18 09:03:12 +05301615 ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001616 ASSERT(k <= MAX_TX_RATE_TBL);
1617
Sujith256b77592008-11-18 09:03:12 +05301618 ath_rc_priv->max_valid_rate = k;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001619 /*
1620 * Some third party vendors don't send the supported rate series in
1621 * order. So sorting to make sure its in order, otherwise our RateFind
1622 * Algo will select wrong rates
1623 */
Sujith256b77592008-11-18 09:03:12 +05301624 ath_rc_sort_validrates(rate_table, ath_rc_priv);
1625 ath_rc_priv->rate_max_phy = ath_rc_priv->valid_rate_index[k-4];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001626}
1627
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001628void ath_rc_node_update(struct ieee80211_hw *hw, struct ath_rate_node *rc_priv)
1629{
1630 struct ath_softc *sc = hw->priv;
1631 u32 capflag = 0;
1632
Johannes Bergae5eb022008-10-14 16:58:37 +02001633 if (hw->conf.ht.enabled) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001634 capflag |= ATH_RC_HT_FLAG | ATH_RC_DS_FLAG;
1635 if (sc->sc_ht_info.tx_chan_width == ATH9K_HT_MACMODE_2040)
1636 capflag |= ATH_RC_CW40_FLAG;
1637 }
1638
Sujith4df8ec62008-11-18 09:04:21 +05301639 rc_priv->ht_cap =
1640 ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
1641 ((capflag & ATH_RC_SGI_FLAG) ? WLAN_RC_SGI_FLAG : 0) |
1642 ((capflag & ATH_RC_HT_FLAG) ? WLAN_RC_HT_FLAG : 0) |
1643 ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0);
1644
1645
1646 ath_rc_sib_update(sc, rc_priv, rc_priv->ht_cap, 0,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001647 &rc_priv->neg_rates,
1648 &rc_priv->neg_ht_rates);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001649}
1650
1651/* Rate Control callbacks */
Johannes Berg4b7679a2008-09-18 18:14:18 +02001652static void ath_tx_status(void *priv, struct ieee80211_supported_band *sband,
1653 struct ieee80211_sta *sta, void *priv_sta,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001654 struct sk_buff *skb)
1655{
1656 struct ath_softc *sc = priv;
1657 struct ath_tx_info_priv *tx_info_priv;
1658 struct ath_node *an;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001659 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1660 struct ieee80211_hdr *hdr;
1661 __le16 fc;
1662
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001663 hdr = (struct ieee80211_hdr *)skb->data;
1664 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +02001665 /* XXX: UGLY HACK!! */
1666 tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001667
Sujithb5aa9bf2008-10-29 10:13:31 +05301668 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001669
Johannes Berge6a98542008-10-21 12:40:02 +02001670 if (tx_info_priv == NULL)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001671 return;
Johannes Berge6a98542008-10-21 12:40:02 +02001672
1673 if (an && priv_sta && ieee80211_is_data(fc))
Johannes Berg4b7679a2008-09-18 18:14:18 +02001674 ath_rate_tx_complete(sc, an, priv_sta, tx_info_priv);
Johannes Berge6a98542008-10-21 12:40:02 +02001675
1676 kfree(tx_info_priv);
1677 tx_info->control.vif = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001678}
1679
Johannes Berge6a98542008-10-21 12:40:02 +02001680static void ath_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
1681 struct ieee80211_tx_rate_control *txrc)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001682{
Johannes Berge6a98542008-10-21 12:40:02 +02001683 struct ieee80211_supported_band *sband = txrc->sband;
1684 struct sk_buff *skb = txrc->skb;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001685 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berg4b7679a2008-09-18 18:14:18 +02001686 struct ath_softc *sc = priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001687 struct ieee80211_hw *hw = sc->hw;
1688 struct ath_tx_info_priv *tx_info_priv;
Johannes Berg4b7679a2008-09-18 18:14:18 +02001689 struct ath_rate_node *ath_rc_priv = priv_sta;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001690 struct ath_node *an;
1691 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
Sujithccc75c52008-10-29 10:18:14 +05301692 int is_probe = FALSE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001693 s8 lowest_idx;
1694 __le16 fc = hdr->frame_control;
1695 u8 *qc, tid;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001696
1697 DPRINTF(sc, ATH_DBG_RATE, "%s\n", __func__);
1698
Johannes Berge6a98542008-10-21 12:40:02 +02001699 /* allocate driver private area of tx_info, XXX: UGLY HACK! */
1700 tx_info->control.vif = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
1701 tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
1702 ASSERT(tx_info_priv != NULL);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001703
Johannes Berg4b7679a2008-09-18 18:14:18 +02001704 lowest_idx = rate_lowest_index(sband, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001705 tx_info_priv->min_rate = (sband->bitrates[lowest_idx].bitrate * 2) / 10;
1706 /* lowest rate for management and multicast/broadcast frames */
1707 if (!ieee80211_is_data(fc) ||
Sujithdc2222a2008-08-14 13:26:55 +05301708 is_multicast_ether_addr(hdr->addr1) || !sta) {
Johannes Berge6a98542008-10-21 12:40:02 +02001709 tx_info->control.rates[0].idx = lowest_idx;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001710 return;
1711 }
1712
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001713 /* Find tx rate for unicast frames */
1714 ath_rate_findrate(sc, ath_rc_priv,
1715 ATH_11N_TXMAXTRY, 4,
1716 ATH_RC_PROBE_ALLOWED,
1717 tx_info_priv->rcs,
1718 &is_probe,
1719 false);
Johannes Berge6a98542008-10-21 12:40:02 +02001720#if 0
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001721 if (is_probe)
Johannes Berg4b7679a2008-09-18 18:14:18 +02001722 sel->probe_idx = ath_rc_priv->tx_ratectrl.probe_rate;
Johannes Berge6a98542008-10-21 12:40:02 +02001723#endif
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001724
1725 /* Ratecontrol sometimes returns invalid rate index */
1726 if (tx_info_priv->rcs[0].rix != 0xff)
1727 ath_rc_priv->prev_data_rix = tx_info_priv->rcs[0].rix;
1728 else
1729 tx_info_priv->rcs[0].rix = ath_rc_priv->prev_data_rix;
1730
Johannes Berge6a98542008-10-21 12:40:02 +02001731 tx_info->control.rates[0].idx = tx_info_priv->rcs[0].rix;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001732
1733 /* Check if aggregation has to be enabled for this tid */
1734
Johannes Bergae5eb022008-10-14 16:58:37 +02001735 if (hw->conf.ht.enabled) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001736 if (ieee80211_is_data_qos(fc)) {
1737 qc = ieee80211_get_qos_ctl(hdr);
1738 tid = qc[0] & 0xf;
Sujithb5aa9bf2008-10-29 10:13:31 +05301739 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001740
Sujithccc75c52008-10-29 10:18:14 +05301741 if(ath_tx_aggr_check(sc, an, tid))
1742 ieee80211_start_tx_ba_session(hw, hdr->addr1, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001743 }
1744 }
1745}
1746
Johannes Berg4b7679a2008-09-18 18:14:18 +02001747static void ath_rate_init(void *priv, struct ieee80211_supported_band *sband,
1748 struct ieee80211_sta *sta, void *priv_sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001749{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001750 struct ath_softc *sc = priv;
Sujithdc2222a2008-08-14 13:26:55 +05301751 struct ath_rate_node *ath_rc_priv = priv_sta;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001752 int i, j = 0;
1753
Sujith7b4d2732008-11-18 09:04:00 +05301754 for (i = 0; i < sband->n_bitrates; i++) {
1755 if (sta->supp_rates[sband->band] & BIT(i)) {
1756 ath_rc_priv->neg_rates.rs_rates[j]
1757 = (sband->bitrates[i].bitrate * 2) / 10;
1758 j++;
1759 }
1760 }
1761 ath_rc_priv->neg_rates.rs_nrates = j;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001762
Sujitha4510bf2008-10-23 12:15:19 +05301763 if (sta->ht_cap.ht_supported) {
Sujith7b4d2732008-11-18 09:04:00 +05301764 for (i = 0, j = 0; i < 77; i++) {
Johannes Bergae5eb022008-10-14 16:58:37 +02001765 if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
Sujithdc2222a2008-08-14 13:26:55 +05301766 ath_rc_priv->neg_ht_rates.rs_rates[j++] = i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001767 if (j == ATH_RATE_MAX)
1768 break;
1769 }
Sujithdc2222a2008-08-14 13:26:55 +05301770 ath_rc_priv->neg_ht_rates.rs_nrates = j;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001771 }
Sujith7b4d2732008-11-18 09:04:00 +05301772
Johannes Berg4b7679a2008-09-18 18:14:18 +02001773 ath_rc_node_update(sc->hw, priv_sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001774}
1775
Johannes Berg4b7679a2008-09-18 18:14:18 +02001776static void *ath_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001777{
Johannes Berg4b7679a2008-09-18 18:14:18 +02001778 return hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001779}
1780
1781static void ath_rate_free(void *priv)
1782{
1783 return;
1784}
1785
Johannes Berg4b7679a2008-09-18 18:14:18 +02001786static void *ath_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001787{
Sujith5640b082008-10-29 10:16:06 +05301788 struct ieee80211_vif *vif;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001789 struct ath_softc *sc = priv;
Sujith5640b082008-10-29 10:16:06 +05301790 struct ath_vap *avp;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001791 struct ath_rate_node *rate_priv;
1792
Sujith5640b082008-10-29 10:16:06 +05301793 vif = sc->sc_vaps[0];
1794 ASSERT(vif);
1795
1796 avp = (void *)vif->drv_priv;
1797
Sujithfe605942008-11-18 09:03:36 +05301798 rate_priv = kzalloc(sizeof(struct ath_rate_node), gfp);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001799 if (!rate_priv) {
Sujithdc2222a2008-08-14 13:26:55 +05301800 DPRINTF(sc, ATH_DBG_FATAL,
1801 "%s: Unable to allocate private rc structure\n",
1802 __func__);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001803 return NULL;
1804 }
Sujithfe605942008-11-18 09:03:36 +05301805
1806 rate_priv->avp = avp;
1807 rate_priv->asc = sc->sc_rc;
1808 avp->rc_node = rate_priv;
1809 rate_priv->rssi_down_time = jiffies_to_msecs(jiffies);
Sujithdc2222a2008-08-14 13:26:55 +05301810
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001811 return rate_priv;
1812}
1813
Johannes Berg4b7679a2008-09-18 18:14:18 +02001814static void ath_rate_free_sta(void *priv, struct ieee80211_sta *sta,
1815 void *priv_sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001816{
1817 struct ath_rate_node *rate_priv = priv_sta;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001818
Sujithfe605942008-11-18 09:03:36 +05301819 kfree(rate_priv);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001820}
1821
1822static struct rate_control_ops ath_rate_ops = {
1823 .module = NULL,
1824 .name = "ath9k_rate_control",
1825 .tx_status = ath_tx_status,
1826 .get_rate = ath_get_rate,
1827 .rate_init = ath_rate_init,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001828 .alloc = ath_rate_alloc,
1829 .free = ath_rate_free,
1830 .alloc_sta = ath_rate_alloc_sta,
Johannes Berg4b7679a2008-09-18 18:14:18 +02001831 .free_sta = ath_rate_free_sta,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001832};
1833
1834int ath_rate_control_register(void)
1835{
1836 return ieee80211_rate_control_register(&ath_rate_ops);
1837}
1838
1839void ath_rate_control_unregister(void)
1840{
1841 ieee80211_rate_control_unregister(&ath_rate_ops);
1842}
1843