Commit | Line | Data |
---|---|---|
8ca151b5 JB |
1 | /****************************************************************************** |
2 | * | |
51368bf7 | 3 | * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved. |
8b4139dc | 4 | * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH |
8ca151b5 JB |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of version 2 of the GNU General Public License as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA | |
18 | * | |
19 | * The full GNU General Public License is included in this distribution in the | |
20 | * file called LICENSE. | |
21 | * | |
22 | * Contact Information: | |
23 | * Intel Linux Wireless <ilw@linux.intel.com> | |
24 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 | |
25 | * | |
26 | *****************************************************************************/ | |
27 | #include <linux/kernel.h> | |
8ca151b5 JB |
28 | #include <linux/skbuff.h> |
29 | #include <linux/slab.h> | |
30 | #include <net/mac80211.h> | |
31 | ||
32 | #include <linux/netdevice.h> | |
33 | #include <linux/etherdevice.h> | |
34 | #include <linux/delay.h> | |
35 | ||
36 | #include <linux/workqueue.h> | |
37 | #include "rs.h" | |
38 | #include "fw-api.h" | |
39 | #include "sta.h" | |
40 | #include "iwl-op-mode.h" | |
41 | #include "mvm.h" | |
42 | ||
43 | #define RS_NAME "iwl-mvm-rs" | |
44 | ||
e07be6d3 ES |
45 | #define NUM_TRY_BEFORE_ANT_TOGGLE 1 |
46 | #define RS_LEGACY_RETRIES_PER_RATE 1 | |
47 | #define RS_HT_VHT_RETRIES_PER_RATE 2 | |
48 | #define RS_HT_VHT_RETRIES_PER_RATE_TW 1 | |
49 | #define RS_INITIAL_MIMO_NUM_RATES 3 | |
50 | #define RS_INITIAL_SISO_NUM_RATES 3 | |
51 | #define RS_INITIAL_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM | |
52 | #define RS_SECONDARY_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM | |
53 | #define RS_SECONDARY_SISO_NUM_RATES 3 | |
54 | #define RS_SECONDARY_SISO_RETRIES 1 | |
8ca151b5 JB |
55 | |
56 | #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */ | |
d17334c4 | 57 | #define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */ |
8ca151b5 JB |
58 | #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */ |
59 | ||
60 | /* max allowed rate miss before sync LQ cmd */ | |
61 | #define IWL_MISSED_RATE_MAX 15 | |
32b01726 | 62 | #define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ) |
87d5e415 | 63 | #define RS_IDLE_TIMEOUT (5*HZ) |
8ca151b5 JB |
64 | |
65 | static u8 rs_ht_to_legacy[] = { | |
da87d7d5 ES |
66 | [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX, |
67 | [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX, | |
68 | [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX, | |
69 | [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX, | |
70 | [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX, | |
71 | [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX, | |
72 | [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX, | |
73 | [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX, | |
74 | [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX, | |
75 | [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX, | |
8ca151b5 JB |
76 | }; |
77 | ||
78 | static const u8 ant_toggle_lookup[] = { | |
ef4394b9 EP |
79 | [ANT_NONE] = ANT_NONE, |
80 | [ANT_A] = ANT_B, | |
81 | [ANT_B] = ANT_C, | |
82 | [ANT_AB] = ANT_BC, | |
83 | [ANT_C] = ANT_A, | |
84 | [ANT_AC] = ANT_AB, | |
85 | [ANT_BC] = ANT_AC, | |
86 | [ANT_ABC] = ANT_ABC, | |
8ca151b5 JB |
87 | }; |
88 | ||
d310e405 ES |
89 | #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \ |
90 | [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \ | |
91 | IWL_RATE_HT_SISO_MCS_##s##_PLCP, \ | |
92 | IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \ | |
93 | IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \ | |
94 | IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\ | |
95 | IWL_RATE_##rp##M_INDEX, \ | |
ab055ce9 | 96 | IWL_RATE_##rn##M_INDEX } |
8ca151b5 | 97 | |
d310e405 ES |
98 | #define IWL_DECLARE_MCS_RATE(s) \ |
99 | [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \ | |
100 | IWL_RATE_HT_SISO_MCS_##s##_PLCP, \ | |
101 | IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \ | |
102 | IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \ | |
103 | IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \ | |
104 | IWL_RATE_INVM_INDEX, \ | |
105 | IWL_RATE_INVM_INDEX } | |
106 | ||
8ca151b5 JB |
107 | /* |
108 | * Parameter order: | |
ab055ce9 | 109 | * rate, ht rate, prev rate, next rate |
8ca151b5 JB |
110 | * |
111 | * If there isn't a valid next or previous rate then INV is used which | |
112 | * maps to IWL_RATE_INVALID | |
113 | * | |
114 | */ | |
115 | static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = { | |
ab055ce9 ES |
116 | IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */ |
117 | IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */ | |
118 | IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */ | |
119 | IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */ | |
d310e405 ES |
120 | IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */ |
121 | IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */ | |
122 | IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */ | |
123 | IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */ | |
124 | IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */ | |
125 | IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */ | |
126 | IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */ | |
127 | IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */ | |
128 | IWL_DECLARE_MCS_RATE(7), /* MCS 7 */ | |
129 | IWL_DECLARE_MCS_RATE(8), /* MCS 8 */ | |
130 | IWL_DECLARE_MCS_RATE(9), /* MCS 9 */ | |
8ca151b5 JB |
131 | }; |
132 | ||
4e4b815c ES |
133 | enum rs_action { |
134 | RS_ACTION_STAY = 0, | |
135 | RS_ACTION_DOWNSCALE = -1, | |
136 | RS_ACTION_UPSCALE = 1, | |
137 | }; | |
138 | ||
b3b06a32 ES |
139 | enum rs_column_mode { |
140 | RS_INVALID = 0, | |
141 | RS_LEGACY, | |
142 | RS_SISO, | |
143 | RS_MIMO2, | |
144 | }; | |
145 | ||
fd7dbee5 | 146 | #define MAX_NEXT_COLUMNS 7 |
b3b06a32 ES |
147 | #define MAX_COLUMN_CHECKS 3 |
148 | ||
149 | typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm, | |
150 | struct ieee80211_sta *sta, | |
151 | struct iwl_scale_tbl_info *tbl); | |
152 | ||
153 | struct rs_tx_column { | |
154 | enum rs_column_mode mode; | |
155 | u8 ant; | |
156 | bool sgi; | |
157 | enum rs_column next_columns[MAX_NEXT_COLUMNS]; | |
158 | allow_column_func_t checks[MAX_COLUMN_CHECKS]; | |
159 | }; | |
160 | ||
161 | static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, | |
162 | struct iwl_scale_tbl_info *tbl) | |
163 | { | |
164 | if (!sta->ht_cap.ht_supported) | |
165 | return false; | |
166 | ||
167 | if (sta->smps_mode == IEEE80211_SMPS_STATIC) | |
168 | return false; | |
169 | ||
4ed735e7 | 170 | if (num_of_ant(mvm->fw->valid_tx_ant) < 2) |
b3b06a32 ES |
171 | return false; |
172 | ||
173 | if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) | |
174 | return false; | |
175 | ||
176 | return true; | |
177 | } | |
178 | ||
179 | static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, | |
180 | struct iwl_scale_tbl_info *tbl) | |
181 | { | |
182 | if (!sta->ht_cap.ht_supported) | |
183 | return false; | |
184 | ||
185 | return true; | |
186 | } | |
187 | ||
188 | static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta, | |
189 | struct iwl_scale_tbl_info *tbl) | |
190 | { | |
191 | struct rs_rate *rate = &tbl->rate; | |
192 | struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap; | |
193 | struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap; | |
194 | ||
195 | if (is_ht20(rate) && (ht_cap->cap & | |
196 | IEEE80211_HT_CAP_SGI_20)) | |
197 | return true; | |
198 | if (is_ht40(rate) && (ht_cap->cap & | |
199 | IEEE80211_HT_CAP_SGI_40)) | |
200 | return true; | |
201 | if (is_ht80(rate) && (vht_cap->cap & | |
202 | IEEE80211_VHT_CAP_SHORT_GI_80)) | |
203 | return true; | |
204 | ||
205 | return false; | |
206 | } | |
207 | ||
208 | static const struct rs_tx_column rs_tx_columns[] = { | |
209 | [RS_COLUMN_LEGACY_ANT_A] = { | |
210 | .mode = RS_LEGACY, | |
211 | .ant = ANT_A, | |
212 | .next_columns = { | |
213 | RS_COLUMN_LEGACY_ANT_B, | |
214 | RS_COLUMN_SISO_ANT_A, | |
31b20452 | 215 | RS_COLUMN_MIMO2, |
d8fff919 ES |
216 | RS_COLUMN_INVALID, |
217 | RS_COLUMN_INVALID, | |
fd7dbee5 ES |
218 | RS_COLUMN_INVALID, |
219 | RS_COLUMN_INVALID, | |
b3b06a32 ES |
220 | }, |
221 | }, | |
222 | [RS_COLUMN_LEGACY_ANT_B] = { | |
223 | .mode = RS_LEGACY, | |
224 | .ant = ANT_B, | |
225 | .next_columns = { | |
226 | RS_COLUMN_LEGACY_ANT_A, | |
227 | RS_COLUMN_SISO_ANT_B, | |
31b20452 | 228 | RS_COLUMN_MIMO2, |
d8fff919 ES |
229 | RS_COLUMN_INVALID, |
230 | RS_COLUMN_INVALID, | |
fd7dbee5 ES |
231 | RS_COLUMN_INVALID, |
232 | RS_COLUMN_INVALID, | |
b3b06a32 ES |
233 | }, |
234 | }, | |
235 | [RS_COLUMN_SISO_ANT_A] = { | |
236 | .mode = RS_SISO, | |
237 | .ant = ANT_A, | |
238 | .next_columns = { | |
239 | RS_COLUMN_SISO_ANT_B, | |
240 | RS_COLUMN_MIMO2, | |
241 | RS_COLUMN_SISO_ANT_A_SGI, | |
fd7dbee5 ES |
242 | RS_COLUMN_LEGACY_ANT_A, |
243 | RS_COLUMN_LEGACY_ANT_B, | |
d8fff919 | 244 | RS_COLUMN_INVALID, |
31b20452 | 245 | RS_COLUMN_INVALID, |
b3b06a32 ES |
246 | }, |
247 | .checks = { | |
248 | rs_siso_allow, | |
249 | }, | |
250 | }, | |
251 | [RS_COLUMN_SISO_ANT_B] = { | |
252 | .mode = RS_SISO, | |
253 | .ant = ANT_B, | |
254 | .next_columns = { | |
255 | RS_COLUMN_SISO_ANT_A, | |
256 | RS_COLUMN_MIMO2, | |
257 | RS_COLUMN_SISO_ANT_B_SGI, | |
fd7dbee5 ES |
258 | RS_COLUMN_LEGACY_ANT_A, |
259 | RS_COLUMN_LEGACY_ANT_B, | |
d8fff919 | 260 | RS_COLUMN_INVALID, |
31b20452 | 261 | RS_COLUMN_INVALID, |
b3b06a32 ES |
262 | }, |
263 | .checks = { | |
264 | rs_siso_allow, | |
265 | }, | |
266 | }, | |
267 | [RS_COLUMN_SISO_ANT_A_SGI] = { | |
268 | .mode = RS_SISO, | |
269 | .ant = ANT_A, | |
270 | .sgi = true, | |
271 | .next_columns = { | |
272 | RS_COLUMN_SISO_ANT_B_SGI, | |
273 | RS_COLUMN_MIMO2_SGI, | |
274 | RS_COLUMN_SISO_ANT_A, | |
fd7dbee5 ES |
275 | RS_COLUMN_LEGACY_ANT_A, |
276 | RS_COLUMN_LEGACY_ANT_B, | |
31b20452 ES |
277 | RS_COLUMN_INVALID, |
278 | RS_COLUMN_INVALID, | |
b3b06a32 ES |
279 | }, |
280 | .checks = { | |
281 | rs_siso_allow, | |
282 | rs_sgi_allow, | |
283 | }, | |
284 | }, | |
285 | [RS_COLUMN_SISO_ANT_B_SGI] = { | |
286 | .mode = RS_SISO, | |
287 | .ant = ANT_B, | |
288 | .sgi = true, | |
289 | .next_columns = { | |
290 | RS_COLUMN_SISO_ANT_A_SGI, | |
291 | RS_COLUMN_MIMO2_SGI, | |
292 | RS_COLUMN_SISO_ANT_B, | |
fd7dbee5 ES |
293 | RS_COLUMN_LEGACY_ANT_A, |
294 | RS_COLUMN_LEGACY_ANT_B, | |
31b20452 ES |
295 | RS_COLUMN_INVALID, |
296 | RS_COLUMN_INVALID, | |
b3b06a32 ES |
297 | }, |
298 | .checks = { | |
299 | rs_siso_allow, | |
300 | rs_sgi_allow, | |
301 | }, | |
302 | }, | |
303 | [RS_COLUMN_MIMO2] = { | |
304 | .mode = RS_MIMO2, | |
305 | .ant = ANT_AB, | |
306 | .next_columns = { | |
307 | RS_COLUMN_SISO_ANT_A, | |
308 | RS_COLUMN_MIMO2_SGI, | |
fd7dbee5 ES |
309 | RS_COLUMN_LEGACY_ANT_A, |
310 | RS_COLUMN_LEGACY_ANT_B, | |
31b20452 ES |
311 | RS_COLUMN_INVALID, |
312 | RS_COLUMN_INVALID, | |
313 | RS_COLUMN_INVALID, | |
b3b06a32 ES |
314 | }, |
315 | .checks = { | |
316 | rs_mimo_allow, | |
317 | }, | |
318 | }, | |
319 | [RS_COLUMN_MIMO2_SGI] = { | |
320 | .mode = RS_MIMO2, | |
321 | .ant = ANT_AB, | |
322 | .sgi = true, | |
323 | .next_columns = { | |
324 | RS_COLUMN_SISO_ANT_A_SGI, | |
325 | RS_COLUMN_MIMO2, | |
fd7dbee5 ES |
326 | RS_COLUMN_LEGACY_ANT_A, |
327 | RS_COLUMN_LEGACY_ANT_B, | |
31b20452 ES |
328 | RS_COLUMN_INVALID, |
329 | RS_COLUMN_INVALID, | |
330 | RS_COLUMN_INVALID, | |
b3b06a32 ES |
331 | }, |
332 | .checks = { | |
333 | rs_mimo_allow, | |
334 | rs_sgi_allow, | |
335 | }, | |
336 | }, | |
337 | }; | |
338 | ||
8ca151b5 JB |
339 | static inline u8 rs_extract_rate(u32 rate_n_flags) |
340 | { | |
341 | /* also works for HT because bits 7:6 are zero there */ | |
342 | return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK); | |
343 | } | |
344 | ||
345 | static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags) | |
346 | { | |
347 | int idx = 0; | |
348 | ||
8ca151b5 | 349 | if (rate_n_flags & RATE_MCS_HT_MSK) { |
d310e405 ES |
350 | idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK; |
351 | idx += IWL_RATE_MCS_0_INDEX; | |
8ca151b5 | 352 | |
d310e405 | 353 | /* skip 9M not supported in HT*/ |
8ca151b5 JB |
354 | if (idx >= IWL_RATE_9M_INDEX) |
355 | idx += 1; | |
4e82dd3a | 356 | if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE)) |
8ca151b5 | 357 | return idx; |
d310e405 ES |
358 | } else if (rate_n_flags & RATE_MCS_VHT_MSK) { |
359 | idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK; | |
360 | idx += IWL_RATE_MCS_0_INDEX; | |
8ca151b5 | 361 | |
d310e405 ES |
362 | /* skip 9M not supported in VHT*/ |
363 | if (idx >= IWL_RATE_9M_INDEX) | |
364 | idx++; | |
365 | if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE)) | |
366 | return idx; | |
8ca151b5 | 367 | } else { |
d310e405 ES |
368 | /* legacy rate format, search for match in table */ |
369 | ||
370 | u8 legacy_rate = rs_extract_rate(rate_n_flags); | |
8ca151b5 | 371 | for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++) |
d310e405 | 372 | if (iwl_rates[idx].plcp == legacy_rate) |
8ca151b5 JB |
373 | return idx; |
374 | } | |
375 | ||
560843f4 | 376 | return IWL_RATE_INVALID; |
8ca151b5 JB |
377 | } |
378 | ||
379 | static void rs_rate_scale_perform(struct iwl_mvm *mvm, | |
380 | struct sk_buff *skb, | |
381 | struct ieee80211_sta *sta, | |
382 | struct iwl_lq_sta *lq_sta); | |
8fc7c58c ES |
383 | static void rs_fill_lq_cmd(struct iwl_mvm *mvm, |
384 | struct ieee80211_sta *sta, | |
385 | struct iwl_lq_sta *lq_sta, | |
386 | const struct rs_rate *initial_rate); | |
8ca151b5 | 387 | static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search); |
8ca151b5 | 388 | |
8ca151b5 JB |
389 | /** |
390 | * The following tables contain the expected throughput metrics for all rates | |
391 | * | |
392 | * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits | |
393 | * | |
394 | * where invalid entries are zeros. | |
395 | * | |
396 | * CCK rates are only valid in legacy table and will only be used in G | |
397 | * (2.4 GHz) band. | |
398 | */ | |
399 | ||
a34529e8 | 400 | static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = { |
4e82dd3a | 401 | 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0 |
8ca151b5 JB |
402 | }; |
403 | ||
4e82dd3a ES |
404 | /* Expected TpT tables. 4 indexes: |
405 | * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI | |
406 | */ | |
a34529e8 | 407 | static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = { |
4e82dd3a ES |
408 | {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0}, |
409 | {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0}, | |
410 | {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0}, | |
411 | {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0}, | |
8ca151b5 JB |
412 | }; |
413 | ||
a34529e8 | 414 | static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = { |
4e82dd3a ES |
415 | {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275}, |
416 | {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280}, | |
417 | {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173}, | |
418 | {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284}, | |
8ca151b5 JB |
419 | }; |
420 | ||
a34529e8 | 421 | static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = { |
d310e405 ES |
422 | {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308}, |
423 | {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312}, | |
424 | {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466}, | |
425 | {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691}, | |
426 | }; | |
427 | ||
a34529e8 | 428 | static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = { |
4e82dd3a ES |
429 | {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0}, |
430 | {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0}, | |
431 | {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0}, | |
432 | {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0}, | |
8ca151b5 JB |
433 | }; |
434 | ||
a34529e8 | 435 | static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = { |
4e82dd3a ES |
436 | {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300}, |
437 | {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303}, | |
438 | {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053}, | |
439 | {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221}, | |
8ca151b5 JB |
440 | }; |
441 | ||
a34529e8 | 442 | static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = { |
d310e405 ES |
443 | {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319}, |
444 | {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320}, | |
445 | {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219}, | |
446 | {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545}, | |
447 | }; | |
448 | ||
8ca151b5 JB |
449 | /* mbps, mcs */ |
450 | static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = { | |
451 | { "1", "BPSK DSSS"}, | |
452 | { "2", "QPSK DSSS"}, | |
453 | {"5.5", "BPSK CCK"}, | |
454 | { "11", "QPSK CCK"}, | |
455 | { "6", "BPSK 1/2"}, | |
456 | { "9", "BPSK 1/2"}, | |
457 | { "12", "QPSK 1/2"}, | |
458 | { "18", "QPSK 3/4"}, | |
459 | { "24", "16QAM 1/2"}, | |
460 | { "36", "16QAM 3/4"}, | |
461 | { "48", "64QAM 2/3"}, | |
462 | { "54", "64QAM 3/4"}, | |
463 | { "60", "64QAM 5/6"}, | |
464 | }; | |
465 | ||
466 | #define MCS_INDEX_PER_STREAM (8) | |
467 | ||
9d10849e EG |
468 | static const char *rs_pretty_ant(u8 ant) |
469 | { | |
470 | static const char * const ant_name[] = { | |
471 | [ANT_NONE] = "None", | |
472 | [ANT_A] = "A", | |
473 | [ANT_B] = "B", | |
474 | [ANT_AB] = "AB", | |
475 | [ANT_C] = "C", | |
476 | [ANT_AC] = "AC", | |
477 | [ANT_BC] = "BC", | |
478 | [ANT_ABC] = "ABC", | |
479 | }; | |
480 | ||
481 | if (ant > ANT_ABC) | |
482 | return "UNKNOWN"; | |
483 | ||
484 | return ant_name[ant]; | |
485 | } | |
486 | ||
487 | static const char *rs_pretty_lq_type(enum iwl_table_type type) | |
488 | { | |
489 | static const char * const lq_types[] = { | |
490 | [LQ_NONE] = "NONE", | |
491 | [LQ_LEGACY_A] = "LEGACY_A", | |
492 | [LQ_LEGACY_G] = "LEGACY_G", | |
493 | [LQ_HT_SISO] = "HT SISO", | |
494 | [LQ_HT_MIMO2] = "HT MIMO", | |
495 | [LQ_VHT_SISO] = "VHT SISO", | |
496 | [LQ_VHT_MIMO2] = "VHT MIMO", | |
497 | }; | |
498 | ||
499 | if (type < LQ_NONE || type >= LQ_MAX) | |
500 | return "UNKNOWN"; | |
501 | ||
502 | return lq_types[type]; | |
503 | } | |
504 | ||
5aa33553 ES |
505 | static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate, |
506 | const char *prefix) | |
507 | { | |
a3576ff2 | 508 | IWL_DEBUG_RATE(mvm, "%s: (%s: %d) ANT: %s BW: %d SGI: %d LDPC: %d\n", |
5aa33553 ES |
509 | prefix, rs_pretty_lq_type(rate->type), |
510 | rate->index, rs_pretty_ant(rate->ant), | |
a3576ff2 | 511 | rate->bw, rate->sgi, rate->ldpc); |
5aa33553 ES |
512 | } |
513 | ||
8ca151b5 JB |
514 | static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window) |
515 | { | |
516 | window->data = 0; | |
517 | window->success_counter = 0; | |
518 | window->success_ratio = IWL_INVALID_VALUE; | |
519 | window->counter = 0; | |
520 | window->average_tpt = IWL_INVALID_VALUE; | |
8ca151b5 JB |
521 | } |
522 | ||
b804eeb6 ES |
523 | static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm, |
524 | struct iwl_scale_tbl_info *tbl) | |
3ca71f60 EP |
525 | { |
526 | int i; | |
527 | ||
b804eeb6 | 528 | IWL_DEBUG_RATE(mvm, "Clearing up window stats\n"); |
3ca71f60 EP |
529 | for (i = 0; i < IWL_RATE_COUNT; i++) |
530 | rs_rate_scale_clear_window(&tbl->win[i]); | |
2fd647f8 EP |
531 | |
532 | for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++) | |
533 | rs_rate_scale_clear_window(&tbl->tpc_win[i]); | |
3ca71f60 EP |
534 | } |
535 | ||
8ca151b5 JB |
536 | static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type) |
537 | { | |
538 | return (ant_type & valid_antenna) == ant_type; | |
539 | } | |
540 | ||
8ca151b5 JB |
541 | static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm, |
542 | struct iwl_lq_sta *lq_data, u8 tid, | |
543 | struct ieee80211_sta *sta) | |
544 | { | |
545 | int ret = -EAGAIN; | |
8ca151b5 | 546 | |
44cc429c EG |
547 | IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n", |
548 | sta->addr, tid); | |
549 | ret = ieee80211_start_tx_ba_session(sta, tid, 5000); | |
550 | if (ret == -EAGAIN) { | |
551 | /* | |
552 | * driver and mac80211 is out of sync | |
553 | * this might be cause by reloading firmware | |
554 | * stop the tx ba session here | |
555 | */ | |
556 | IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n", | |
557 | tid); | |
558 | ieee80211_stop_tx_ba_session(sta, tid); | |
8ca151b5 JB |
559 | } |
560 | return ret; | |
561 | } | |
562 | ||
563 | static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid, | |
564 | struct iwl_lq_sta *lq_data, | |
565 | struct ieee80211_sta *sta) | |
566 | { | |
567 | if (tid < IWL_MAX_TID_COUNT) | |
568 | rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta); | |
569 | else | |
570 | IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n", | |
571 | tid, IWL_MAX_TID_COUNT); | |
572 | } | |
573 | ||
574 | static inline int get_num_of_ant_from_rate(u32 rate_n_flags) | |
575 | { | |
576 | return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) + | |
577 | !!(rate_n_flags & RATE_MCS_ANT_B_MSK) + | |
578 | !!(rate_n_flags & RATE_MCS_ANT_C_MSK); | |
579 | } | |
580 | ||
581 | /* | |
582 | * Static function to get the expected throughput from an iwl_scale_tbl_info | |
583 | * that wraps a NULL pointer check | |
584 | */ | |
585 | static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index) | |
586 | { | |
587 | if (tbl->expected_tpt) | |
588 | return tbl->expected_tpt[rs_index]; | |
589 | return 0; | |
590 | } | |
591 | ||
592 | /** | |
593 | * rs_collect_tx_data - Update the success/failure sliding window | |
594 | * | |
595 | * We keep a sliding window of the last 62 packets transmitted | |
596 | * at this rate. window->data contains the bitmask of successful | |
597 | * packets. | |
598 | */ | |
0bd3c5a7 EP |
599 | static int _rs_collect_tx_data(struct iwl_scale_tbl_info *tbl, |
600 | int scale_index, int attempts, int successes, | |
601 | struct iwl_rate_scale_data *window) | |
8ca151b5 | 602 | { |
8ca151b5 JB |
603 | static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1)); |
604 | s32 fail_count, tpt; | |
605 | ||
8ca151b5 JB |
606 | /* Get expected throughput */ |
607 | tpt = get_expected_tpt(tbl, scale_index); | |
608 | ||
609 | /* | |
610 | * Keep track of only the latest 62 tx frame attempts in this rate's | |
611 | * history window; anything older isn't really relevant any more. | |
612 | * If we have filled up the sliding window, drop the oldest attempt; | |
613 | * if the oldest attempt (highest bit in bitmap) shows "success", | |
614 | * subtract "1" from the success counter (this is the main reason | |
615 | * we keep these bitmaps!). | |
616 | */ | |
617 | while (attempts > 0) { | |
618 | if (window->counter >= IWL_RATE_MAX_WINDOW) { | |
619 | /* remove earliest */ | |
620 | window->counter = IWL_RATE_MAX_WINDOW - 1; | |
621 | ||
622 | if (window->data & mask) { | |
623 | window->data &= ~mask; | |
624 | window->success_counter--; | |
625 | } | |
626 | } | |
627 | ||
628 | /* Increment frames-attempted counter */ | |
629 | window->counter++; | |
630 | ||
631 | /* Shift bitmap by one frame to throw away oldest history */ | |
632 | window->data <<= 1; | |
633 | ||
634 | /* Mark the most recent #successes attempts as successful */ | |
635 | if (successes > 0) { | |
636 | window->success_counter++; | |
637 | window->data |= 0x1; | |
638 | successes--; | |
639 | } | |
640 | ||
641 | attempts--; | |
642 | } | |
643 | ||
644 | /* Calculate current success ratio, avoid divide-by-0! */ | |
645 | if (window->counter > 0) | |
646 | window->success_ratio = 128 * (100 * window->success_counter) | |
647 | / window->counter; | |
648 | else | |
649 | window->success_ratio = IWL_INVALID_VALUE; | |
650 | ||
651 | fail_count = window->counter - window->success_counter; | |
652 | ||
653 | /* Calculate average throughput, if we have enough history. */ | |
654 | if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) || | |
655 | (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH)) | |
656 | window->average_tpt = (window->success_ratio * tpt + 64) / 128; | |
657 | else | |
658 | window->average_tpt = IWL_INVALID_VALUE; | |
659 | ||
8ca151b5 JB |
660 | return 0; |
661 | } | |
662 | ||
757cf23b ES |
663 | static int rs_collect_tx_data(struct iwl_lq_sta *lq_sta, |
664 | struct iwl_scale_tbl_info *tbl, | |
2fd647f8 EP |
665 | int scale_index, int attempts, int successes, |
666 | u8 reduced_txp) | |
0bd3c5a7 EP |
667 | { |
668 | struct iwl_rate_scale_data *window = NULL; | |
2fd647f8 | 669 | int ret; |
0bd3c5a7 EP |
670 | |
671 | if (scale_index < 0 || scale_index >= IWL_RATE_COUNT) | |
672 | return -EINVAL; | |
673 | ||
757cf23b | 674 | if (tbl->column != RS_COLUMN_INVALID) { |
f58220f6 ES |
675 | struct lq_sta_pers *pers = &lq_sta->pers; |
676 | ||
677 | pers->tx_stats[tbl->column][scale_index].total += attempts; | |
678 | pers->tx_stats[tbl->column][scale_index].success += successes; | |
757cf23b ES |
679 | } |
680 | ||
0bd3c5a7 EP |
681 | /* Select window for current tx bit rate */ |
682 | window = &(tbl->win[scale_index]); | |
683 | ||
2fd647f8 EP |
684 | ret = _rs_collect_tx_data(tbl, scale_index, attempts, successes, |
685 | window); | |
686 | if (ret) | |
687 | return ret; | |
688 | ||
689 | if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION)) | |
690 | return -EINVAL; | |
691 | ||
692 | window = &tbl->tpc_win[reduced_txp]; | |
0bd3c5a7 EP |
693 | return _rs_collect_tx_data(tbl, scale_index, attempts, successes, |
694 | window); | |
695 | } | |
696 | ||
5aa33553 ES |
697 | /* Convert rs_rate object into ucode rate bitmask */ |
698 | static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm, | |
8fc7c58c | 699 | struct rs_rate *rate) |
8ca151b5 | 700 | { |
5aa33553 ES |
701 | u32 ucode_rate = 0; |
702 | int index = rate->index; | |
8ca151b5 | 703 | |
5aa33553 | 704 | ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) & |
d310e405 ES |
705 | RATE_MCS_ANT_ABC_MSK); |
706 | ||
5aa33553 ES |
707 | if (is_legacy(rate)) { |
708 | ucode_rate |= iwl_rates[index].plcp; | |
8ca151b5 | 709 | if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE) |
5aa33553 ES |
710 | ucode_rate |= RATE_MCS_CCK_MSK; |
711 | return ucode_rate; | |
d310e405 ES |
712 | } |
713 | ||
5aa33553 | 714 | if (is_ht(rate)) { |
d310e405 | 715 | if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) { |
8ca151b5 | 716 | IWL_ERR(mvm, "Invalid HT rate index %d\n", index); |
4e82dd3a | 717 | index = IWL_LAST_HT_RATE; |
8ca151b5 | 718 | } |
5aa33553 | 719 | ucode_rate |= RATE_MCS_HT_MSK; |
8ca151b5 | 720 | |
5aa33553 ES |
721 | if (is_ht_siso(rate)) |
722 | ucode_rate |= iwl_rates[index].plcp_ht_siso; | |
723 | else if (is_ht_mimo2(rate)) | |
724 | ucode_rate |= iwl_rates[index].plcp_ht_mimo2; | |
8ca151b5 | 725 | else |
d972ab31 | 726 | WARN_ON_ONCE(1); |
5aa33553 | 727 | } else if (is_vht(rate)) { |
d310e405 ES |
728 | if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) { |
729 | IWL_ERR(mvm, "Invalid VHT rate index %d\n", index); | |
730 | index = IWL_LAST_VHT_RATE; | |
731 | } | |
5aa33553 ES |
732 | ucode_rate |= RATE_MCS_VHT_MSK; |
733 | if (is_vht_siso(rate)) | |
734 | ucode_rate |= iwl_rates[index].plcp_vht_siso; | |
735 | else if (is_vht_mimo2(rate)) | |
736 | ucode_rate |= iwl_rates[index].plcp_vht_mimo2; | |
d310e405 ES |
737 | else |
738 | WARN_ON_ONCE(1); | |
739 | ||
8ca151b5 | 740 | } else { |
5aa33553 | 741 | IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type); |
8ca151b5 JB |
742 | } |
743 | ||
5aa33553 ES |
744 | ucode_rate |= rate->bw; |
745 | if (rate->sgi) | |
746 | ucode_rate |= RATE_MCS_SGI_MSK; | |
a3576ff2 ES |
747 | if (rate->ldpc) |
748 | ucode_rate |= RATE_MCS_LDPC_MSK; | |
d310e405 | 749 | |
5aa33553 | 750 | return ucode_rate; |
8ca151b5 JB |
751 | } |
752 | ||
5aa33553 ES |
753 | /* Convert a ucode rate into an rs_rate object */ |
754 | static int rs_rate_from_ucode_rate(const u32 ucode_rate, | |
755 | enum ieee80211_band band, | |
756 | struct rs_rate *rate) | |
8ca151b5 | 757 | { |
5aa33553 ES |
758 | u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK; |
759 | u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate); | |
d310e405 | 760 | u8 nss; |
8ca151b5 | 761 | |
9e898f1b | 762 | memset(rate, 0, sizeof(*rate)); |
5aa33553 | 763 | rate->index = iwl_hwrate_to_plcp_idx(ucode_rate); |
8ca151b5 | 764 | |
560843f4 | 765 | if (rate->index == IWL_RATE_INVALID) |
8ca151b5 | 766 | return -EINVAL; |
5aa33553 ES |
767 | |
768 | rate->ant = (ant_msk >> RATE_MCS_ANT_POS); | |
8ca151b5 | 769 | |
d310e405 | 770 | /* Legacy */ |
5aa33553 ES |
771 | if (!(ucode_rate & RATE_MCS_HT_MSK) && |
772 | !(ucode_rate & RATE_MCS_VHT_MSK)) { | |
8ca151b5 JB |
773 | if (num_of_ant == 1) { |
774 | if (band == IEEE80211_BAND_5GHZ) | |
5aa33553 | 775 | rate->type = LQ_LEGACY_A; |
8ca151b5 | 776 | else |
5aa33553 | 777 | rate->type = LQ_LEGACY_G; |
8ca151b5 | 778 | } |
d310e405 ES |
779 | |
780 | return 0; | |
781 | } | |
782 | ||
783 | /* HT or VHT */ | |
5aa33553 ES |
784 | if (ucode_rate & RATE_MCS_SGI_MSK) |
785 | rate->sgi = true; | |
a3576ff2 ES |
786 | if (ucode_rate & RATE_MCS_LDPC_MSK) |
787 | rate->ldpc = true; | |
d310e405 | 788 | |
5aa33553 | 789 | rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK; |
d310e405 | 790 | |
5aa33553 ES |
791 | if (ucode_rate & RATE_MCS_HT_MSK) { |
792 | nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >> | |
d310e405 ES |
793 | RATE_HT_MCS_NSS_POS) + 1; |
794 | ||
795 | if (nss == 1) { | |
5aa33553 | 796 | rate->type = LQ_HT_SISO; |
d310e405 ES |
797 | WARN_ON_ONCE(num_of_ant != 1); |
798 | } else if (nss == 2) { | |
5aa33553 | 799 | rate->type = LQ_HT_MIMO2; |
d310e405 | 800 | WARN_ON_ONCE(num_of_ant != 2); |
8ca151b5 | 801 | } else { |
d310e405 ES |
802 | WARN_ON_ONCE(1); |
803 | } | |
5aa33553 ES |
804 | } else if (ucode_rate & RATE_MCS_VHT_MSK) { |
805 | nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >> | |
d310e405 ES |
806 | RATE_VHT_MCS_NSS_POS) + 1; |
807 | ||
808 | if (nss == 1) { | |
5aa33553 | 809 | rate->type = LQ_VHT_SISO; |
d310e405 ES |
810 | WARN_ON_ONCE(num_of_ant != 1); |
811 | } else if (nss == 2) { | |
5aa33553 | 812 | rate->type = LQ_VHT_MIMO2; |
d310e405 ES |
813 | WARN_ON_ONCE(num_of_ant != 2); |
814 | } else { | |
815 | WARN_ON_ONCE(1); | |
8ca151b5 JB |
816 | } |
817 | } | |
d310e405 | 818 | |
5aa33553 ES |
819 | WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160); |
820 | WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 && | |
821 | !is_vht(rate)); | |
d310e405 | 822 | |
8ca151b5 JB |
823 | return 0; |
824 | } | |
825 | ||
826 | /* switch to another antenna/antennas and return 1 */ | |
827 | /* if no other valid antenna found, return 0 */ | |
8fc7c58c | 828 | static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate) |
8ca151b5 JB |
829 | { |
830 | u8 new_ant_type; | |
831 | ||
5aa33553 | 832 | if (!rate->ant || rate->ant > ANT_ABC) |
8ca151b5 JB |
833 | return 0; |
834 | ||
5aa33553 | 835 | if (!rs_is_valid_ant(valid_ant, rate->ant)) |
8ca151b5 JB |
836 | return 0; |
837 | ||
5aa33553 | 838 | new_ant_type = ant_toggle_lookup[rate->ant]; |
8ca151b5 | 839 | |
5aa33553 | 840 | while ((new_ant_type != rate->ant) && |
8ca151b5 JB |
841 | !rs_is_valid_ant(valid_ant, new_ant_type)) |
842 | new_ant_type = ant_toggle_lookup[new_ant_type]; | |
843 | ||
5aa33553 | 844 | if (new_ant_type == rate->ant) |
8ca151b5 JB |
845 | return 0; |
846 | ||
5aa33553 ES |
847 | rate->ant = new_ant_type; |
848 | ||
8ca151b5 JB |
849 | return 1; |
850 | } | |
851 | ||
8ca151b5 | 852 | static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta, |
5aa33553 | 853 | struct rs_rate *rate) |
8ca151b5 | 854 | { |
5aa33553 | 855 | if (is_legacy(rate)) |
8ca151b5 | 856 | return lq_sta->active_legacy_rate; |
5aa33553 | 857 | else if (is_siso(rate)) |
d310e405 | 858 | return lq_sta->active_siso_rate; |
5aa33553 | 859 | else if (is_mimo2(rate)) |
d310e405 ES |
860 | return lq_sta->active_mimo2_rate; |
861 | ||
862 | WARN_ON_ONCE(1); | |
863 | return 0; | |
8ca151b5 JB |
864 | } |
865 | ||
866 | static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask, | |
867 | int rate_type) | |
868 | { | |
869 | u8 high = IWL_RATE_INVALID; | |
870 | u8 low = IWL_RATE_INVALID; | |
871 | ||
872 | /* 802.11A or ht walks to the next literal adjacent rate in | |
873 | * the rate table */ | |
5aa33553 | 874 | if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) { |
8ca151b5 JB |
875 | int i; |
876 | u32 mask; | |
877 | ||
878 | /* Find the previous rate that is in the rate mask */ | |
879 | i = index - 1; | |
880 | for (mask = (1 << i); i >= 0; i--, mask >>= 1) { | |
881 | if (rate_mask & mask) { | |
882 | low = i; | |
883 | break; | |
884 | } | |
885 | } | |
886 | ||
887 | /* Find the next rate that is in the rate mask */ | |
888 | i = index + 1; | |
889 | for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) { | |
890 | if (rate_mask & mask) { | |
891 | high = i; | |
892 | break; | |
893 | } | |
894 | } | |
895 | ||
896 | return (high << 8) | low; | |
897 | } | |
898 | ||
899 | low = index; | |
900 | while (low != IWL_RATE_INVALID) { | |
901 | low = iwl_rates[low].prev_rs; | |
902 | if (low == IWL_RATE_INVALID) | |
903 | break; | |
904 | if (rate_mask & (1 << low)) | |
905 | break; | |
906 | IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low); | |
907 | } | |
908 | ||
909 | high = index; | |
910 | while (high != IWL_RATE_INVALID) { | |
911 | high = iwl_rates[high].next_rs; | |
912 | if (high == IWL_RATE_INVALID) | |
913 | break; | |
914 | if (rate_mask & (1 << high)) | |
915 | break; | |
916 | IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high); | |
917 | } | |
918 | ||
919 | return (high << 8) | low; | |
920 | } | |
921 | ||
e07be6d3 ES |
922 | static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta, |
923 | struct rs_rate *rate) | |
8ca151b5 | 924 | { |
e07be6d3 ES |
925 | return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate); |
926 | } | |
927 | ||
928 | /* Get the next supported lower rate in the current column. | |
929 | * Return true if bottom rate in the current column was reached | |
930 | */ | |
931 | static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta, | |
932 | struct rs_rate *rate) | |
933 | { | |
934 | u8 low; | |
8ca151b5 | 935 | u16 high_low; |
e07be6d3 | 936 | u16 rate_mask; |
ae969afe | 937 | struct iwl_mvm *mvm = lq_sta->pers.drv; |
e07be6d3 ES |
938 | |
939 | rate_mask = rs_get_supported_rates(lq_sta, rate); | |
940 | high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask, | |
941 | rate->type); | |
942 | low = high_low & 0xff; | |
943 | ||
944 | /* Bottom rate of column reached */ | |
945 | if (low == IWL_RATE_INVALID) | |
946 | return true; | |
947 | ||
948 | rate->index = low; | |
949 | return false; | |
950 | } | |
951 | ||
952 | /* Get the next rate to use following a column downgrade */ | |
953 | static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta, | |
954 | struct rs_rate *rate) | |
955 | { | |
ae969afe | 956 | struct iwl_mvm *mvm = lq_sta->pers.drv; |
8ca151b5 | 957 | |
e07be6d3 ES |
958 | if (is_legacy(rate)) { |
959 | /* No column to downgrade from Legacy */ | |
960 | return; | |
961 | } else if (is_siso(rate)) { | |
962 | /* Downgrade to Legacy if we were in SISO */ | |
8ca151b5 | 963 | if (lq_sta->band == IEEE80211_BAND_5GHZ) |
5aa33553 | 964 | rate->type = LQ_LEGACY_A; |
8ca151b5 | 965 | else |
5aa33553 | 966 | rate->type = LQ_LEGACY_G; |
8ca151b5 | 967 | |
a56db7d1 | 968 | rate->bw = RATE_MCS_CHAN_WIDTH_20; |
8ca151b5 | 969 | |
b800040d | 970 | WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX || |
e07be6d3 | 971 | rate->index > IWL_RATE_MCS_9_INDEX); |
8ca151b5 | 972 | |
e07be6d3 | 973 | rate->index = rs_ht_to_legacy[rate->index]; |
a3576ff2 | 974 | rate->ldpc = false; |
e07be6d3 ES |
975 | } else { |
976 | /* Downgrade to SISO with same MCS if in MIMO */ | |
977 | rate->type = is_vht_mimo2(rate) ? | |
978 | LQ_VHT_SISO : LQ_HT_SISO; | |
8ca151b5 JB |
979 | } |
980 | ||
e07be6d3 | 981 | if (num_of_ant(rate->ant) > 1) |
4ed735e7 | 982 | rate->ant = first_antenna(mvm->fw->valid_tx_ant); |
8ca151b5 | 983 | |
e07be6d3 ES |
984 | /* Relevant in both switching to SISO or Legacy */ |
985 | rate->sgi = false; | |
986 | ||
987 | if (!rs_rate_supported(lq_sta, rate)) | |
988 | rs_get_lower_rate_in_column(lq_sta, rate); | |
8ca151b5 JB |
989 | } |
990 | ||
5aa33553 ES |
991 | /* Simple function to compare two rate scale table types */ |
992 | static inline bool rs_rate_match(struct rs_rate *a, | |
993 | struct rs_rate *b) | |
8ca151b5 | 994 | { |
5aa33553 | 995 | return (a->type == b->type) && (a->ant == b->ant) && (a->sgi == b->sgi); |
8ca151b5 JB |
996 | } |
997 | ||
d310e405 ES |
998 | static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags) |
999 | { | |
1000 | if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH) | |
1001 | return RATE_MCS_CHAN_WIDTH_40; | |
1002 | else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH) | |
1003 | return RATE_MCS_CHAN_WIDTH_80; | |
1004 | else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH) | |
1005 | return RATE_MCS_CHAN_WIDTH_160; | |
1006 | ||
1007 | return RATE_MCS_CHAN_WIDTH_20; | |
1008 | } | |
1009 | ||
8ca151b5 JB |
1010 | /* |
1011 | * mac80211 sends us Tx status | |
1012 | */ | |
1013 | static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband, | |
1014 | struct ieee80211_sta *sta, void *priv_sta, | |
1015 | struct sk_buff *skb) | |
1016 | { | |
1017 | int legacy_success; | |
1018 | int retries; | |
5aa33553 | 1019 | int mac_index, i; |
8ca151b5 JB |
1020 | struct iwl_lq_sta *lq_sta = priv_sta; |
1021 | struct iwl_lq_cmd *table; | |
1022 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; | |
1023 | struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r; | |
1024 | struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode); | |
1025 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
1026 | enum mac80211_rate_control_flags mac_flags; | |
5aa33553 ES |
1027 | u32 ucode_rate; |
1028 | struct rs_rate rate; | |
8ca151b5 | 1029 | struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl; |
2fd647f8 | 1030 | u8 reduced_txp = (uintptr_t)info->status.status_driver_data[0]; |
8ca151b5 | 1031 | |
8ca151b5 JB |
1032 | /* Treat uninitialized rate scaling data same as non-existing. */ |
1033 | if (!lq_sta) { | |
1034 | IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n"); | |
1035 | return; | |
ae969afe | 1036 | } else if (!lq_sta->pers.drv) { |
8ca151b5 JB |
1037 | IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n"); |
1038 | return; | |
1039 | } | |
1040 | ||
8e96440e | 1041 | #ifdef CONFIG_MAC80211_DEBUGFS |
87d5e415 | 1042 | /* Disable last tx check if we are debugging with fixed rate */ |
ae969afe | 1043 | if (lq_sta->pers.dbg_fixed_rate) { |
87d5e415 ES |
1044 | IWL_DEBUG_RATE(mvm, "Fixed rate. avoid rate scaling\n"); |
1045 | return; | |
1046 | } | |
1047 | #endif | |
8ca151b5 JB |
1048 | if (!ieee80211_is_data(hdr->frame_control) || |
1049 | info->flags & IEEE80211_TX_CTL_NO_ACK) | |
1050 | return; | |
1051 | ||
1052 | /* This packet was aggregated but doesn't carry status info */ | |
1053 | if ((info->flags & IEEE80211_TX_CTL_AMPDU) && | |
1054 | !(info->flags & IEEE80211_TX_STAT_AMPDU)) | |
1055 | return; | |
1056 | ||
1057 | /* | |
1058 | * Ignore this Tx frame response if its initial rate doesn't match | |
1059 | * that of latest Link Quality command. There may be stragglers | |
1060 | * from a previous Link Quality command, but we're no longer interested | |
1061 | * in those; they're either from the "active" mode while we're trying | |
1062 | * to check "search" mode, or a prior "search" mode after we've moved | |
1063 | * to a new "search" mode (which might become the new "active" mode). | |
1064 | */ | |
1065 | table = &lq_sta->lq; | |
5aa33553 ES |
1066 | ucode_rate = le32_to_cpu(table->rs_table[0]); |
1067 | rs_rate_from_ucode_rate(ucode_rate, info->band, &rate); | |
8ca151b5 | 1068 | if (info->band == IEEE80211_BAND_5GHZ) |
5aa33553 | 1069 | rate.index -= IWL_FIRST_OFDM_RATE; |
8ca151b5 JB |
1070 | mac_flags = info->status.rates[0].flags; |
1071 | mac_index = info->status.rates[0].idx; | |
1072 | /* For HT packets, map MCS to PLCP */ | |
1073 | if (mac_flags & IEEE80211_TX_RC_MCS) { | |
1074 | /* Remove # of streams */ | |
1075 | mac_index &= RATE_HT_MCS_RATE_CODE_MSK; | |
1076 | if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE)) | |
1077 | mac_index++; | |
1078 | /* | |
1079 | * mac80211 HT index is always zero-indexed; we need to move | |
1080 | * HT OFDM rates after CCK rates in 2.4 GHz band | |
1081 | */ | |
1082 | if (info->band == IEEE80211_BAND_2GHZ) | |
1083 | mac_index += IWL_FIRST_OFDM_RATE; | |
d310e405 ES |
1084 | } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) { |
1085 | mac_index &= RATE_VHT_MCS_RATE_CODE_MSK; | |
1086 | if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE)) | |
1087 | mac_index++; | |
8ca151b5 | 1088 | } |
d310e405 | 1089 | |
87d5e415 ES |
1090 | if (time_after(jiffies, |
1091 | (unsigned long)(lq_sta->last_tx + RS_IDLE_TIMEOUT))) { | |
1092 | int tid; | |
1093 | IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n"); | |
1094 | for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) | |
1095 | ieee80211_stop_tx_ba_session(sta, tid); | |
1096 | ||
1097 | iwl_mvm_rs_rate_init(mvm, sta, sband->band, false); | |
1098 | return; | |
1099 | } | |
1100 | lq_sta->last_tx = jiffies; | |
1101 | ||
8ca151b5 JB |
1102 | /* Here we actually compare this rate to the latest LQ command */ |
1103 | if ((mac_index < 0) || | |
5aa33553 ES |
1104 | (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) || |
1105 | (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) || | |
1106 | (rate.ant != info->status.antenna) || | |
1107 | (!!(ucode_rate & RATE_MCS_HT_MSK) != | |
d310e405 | 1108 | !!(mac_flags & IEEE80211_TX_RC_MCS)) || |
5aa33553 | 1109 | (!!(ucode_rate & RATE_MCS_VHT_MSK) != |
d310e405 | 1110 | !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) || |
5aa33553 | 1111 | (!!(ucode_rate & RATE_HT_MCS_GF_MSK) != |
d310e405 | 1112 | !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) || |
5aa33553 | 1113 | (rate.index != mac_index)) { |
8ca151b5 JB |
1114 | IWL_DEBUG_RATE(mvm, |
1115 | "initial rate %d does not match %d (0x%x)\n", | |
5aa33553 | 1116 | mac_index, rate.index, ucode_rate); |
8ca151b5 JB |
1117 | /* |
1118 | * Since rates mis-match, the last LQ command may have failed. | |
1119 | * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with | |
1120 | * ... driver. | |
1121 | */ | |
1122 | lq_sta->missed_rate_counter++; | |
1123 | if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) { | |
1124 | lq_sta->missed_rate_counter = 0; | |
b3b06a32 ES |
1125 | IWL_DEBUG_RATE(mvm, |
1126 | "Too many rates mismatch. Send sync LQ. rs_state %d\n", | |
1127 | lq_sta->rs_state); | |
9e680946 | 1128 | iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false); |
8ca151b5 JB |
1129 | } |
1130 | /* Regardless, ignore this status info for outdated rate */ | |
1131 | return; | |
1132 | } else | |
1133 | /* Rate did match, so reset the missed_rate_counter */ | |
1134 | lq_sta->missed_rate_counter = 0; | |
1135 | ||
1136 | /* Figure out if rate scale algorithm is in active or search table */ | |
5aa33553 ES |
1137 | if (rs_rate_match(&rate, |
1138 | &(lq_sta->lq_info[lq_sta->active_tbl].rate))) { | |
8ca151b5 JB |
1139 | curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); |
1140 | other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]); | |
5aa33553 ES |
1141 | } else if (rs_rate_match(&rate, |
1142 | &lq_sta->lq_info[1 - lq_sta->active_tbl].rate)) { | |
8ca151b5 JB |
1143 | curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]); |
1144 | other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); | |
1145 | } else { | |
1146 | IWL_DEBUG_RATE(mvm, | |
1147 | "Neither active nor search matches tx rate\n"); | |
1148 | tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); | |
5aa33553 | 1149 | rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE"); |
8ca151b5 | 1150 | tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]); |
5aa33553 ES |
1151 | rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH"); |
1152 | rs_dump_rate(mvm, &rate, "ACTUAL"); | |
1153 | ||
8ca151b5 JB |
1154 | /* |
1155 | * no matching table found, let's by-pass the data collection | |
1156 | * and continue to perform rate scale to find the rate table | |
1157 | */ | |
1158 | rs_stay_in_table(lq_sta, true); | |
1159 | goto done; | |
1160 | } | |
1161 | ||
1162 | /* | |
1163 | * Updating the frame history depends on whether packets were | |
1164 | * aggregated. | |
1165 | * | |
1166 | * For aggregation, all packets were transmitted at the same rate, the | |
1167 | * first index into rate scale table. | |
1168 | */ | |
1169 | if (info->flags & IEEE80211_TX_STAT_AMPDU) { | |
5aa33553 ES |
1170 | ucode_rate = le32_to_cpu(table->rs_table[0]); |
1171 | rs_rate_from_ucode_rate(ucode_rate, info->band, &rate); | |
757cf23b | 1172 | rs_collect_tx_data(lq_sta, curr_tbl, rate.index, |
8ca151b5 | 1173 | info->status.ampdu_len, |
2fd647f8 EP |
1174 | info->status.ampdu_ack_len, |
1175 | reduced_txp); | |
8ca151b5 JB |
1176 | |
1177 | /* Update success/fail counts if not searching for new mode */ | |
b3b06a32 | 1178 | if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) { |
8ca151b5 JB |
1179 | lq_sta->total_success += info->status.ampdu_ack_len; |
1180 | lq_sta->total_failed += (info->status.ampdu_len - | |
1181 | info->status.ampdu_ack_len); | |
1182 | } | |
1183 | } else { | |
1184 | /* | |
1185 | * For legacy, update frame history with for each Tx retry. | |
1186 | */ | |
1187 | retries = info->status.rates[0].count - 1; | |
1188 | /* HW doesn't send more than 15 retries */ | |
1189 | retries = min(retries, 15); | |
1190 | ||
1191 | /* The last transmission may have been successful */ | |
1192 | legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK); | |
1193 | /* Collect data for each rate used during failed TX attempts */ | |
1194 | for (i = 0; i <= retries; ++i) { | |
5aa33553 ES |
1195 | ucode_rate = le32_to_cpu(table->rs_table[i]); |
1196 | rs_rate_from_ucode_rate(ucode_rate, info->band, &rate); | |
8ca151b5 JB |
1197 | /* |
1198 | * Only collect stats if retried rate is in the same RS | |
1199 | * table as active/search. | |
1200 | */ | |
5aa33553 | 1201 | if (rs_rate_match(&rate, &curr_tbl->rate)) |
8ca151b5 | 1202 | tmp_tbl = curr_tbl; |
5aa33553 | 1203 | else if (rs_rate_match(&rate, &other_tbl->rate)) |
8ca151b5 | 1204 | tmp_tbl = other_tbl; |
4107dbd2 | 1205 | else |
8ca151b5 | 1206 | continue; |
4107dbd2 | 1207 | |
757cf23b | 1208 | rs_collect_tx_data(lq_sta, tmp_tbl, rate.index, 1, |
2fd647f8 EP |
1209 | i < retries ? 0 : legacy_success, |
1210 | reduced_txp); | |
8ca151b5 JB |
1211 | } |
1212 | ||
1213 | /* Update success/fail counts if not searching for new mode */ | |
b3b06a32 | 1214 | if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) { |
8ca151b5 JB |
1215 | lq_sta->total_success += legacy_success; |
1216 | lq_sta->total_failed += retries + (1 - legacy_success); | |
1217 | } | |
1218 | } | |
1219 | /* The last TX rate is cached in lq_sta; it's set in if/else above */ | |
5aa33553 | 1220 | lq_sta->last_rate_n_flags = ucode_rate; |
2fd647f8 | 1221 | IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp); |
8ca151b5 JB |
1222 | done: |
1223 | /* See if there's a better rate or modulation mode to try. */ | |
1224 | if (sta && sta->supp_rates[sband->band]) | |
1225 | rs_rate_scale_perform(mvm, skb, sta, lq_sta); | |
1226 | } | |
1227 | ||
1228 | /* | |
1229 | * Begin a period of staying with a selected modulation mode. | |
1230 | * Set "stay_in_tbl" flag to prevent any mode switches. | |
1231 | * Set frame tx success limits according to legacy vs. high-throughput, | |
1232 | * and reset overall (spanning all rates) tx success history statistics. | |
1233 | * These control how long we stay using same modulation mode before | |
1234 | * searching for a new mode. | |
1235 | */ | |
1236 | static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy, | |
1237 | struct iwl_lq_sta *lq_sta) | |
1238 | { | |
b3b06a32 ES |
1239 | IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n"); |
1240 | lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN; | |
8ca151b5 JB |
1241 | if (is_legacy) { |
1242 | lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT; | |
1243 | lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT; | |
1244 | lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT; | |
1245 | } else { | |
1246 | lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT; | |
1247 | lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT; | |
1248 | lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT; | |
1249 | } | |
1250 | lq_sta->table_count = 0; | |
1251 | lq_sta->total_failed = 0; | |
1252 | lq_sta->total_success = 0; | |
1253 | lq_sta->flush_timer = jiffies; | |
b3b06a32 | 1254 | lq_sta->visited_columns = 0; |
8ca151b5 JB |
1255 | } |
1256 | ||
198266a3 ES |
1257 | static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta, |
1258 | const struct rs_tx_column *column) | |
1259 | { | |
1260 | switch (column->mode) { | |
1261 | case RS_LEGACY: | |
1262 | return lq_sta->max_legacy_rate_idx; | |
1263 | case RS_SISO: | |
1264 | return lq_sta->max_siso_rate_idx; | |
1265 | case RS_MIMO2: | |
1266 | return lq_sta->max_mimo2_rate_idx; | |
1267 | default: | |
1268 | WARN_ON_ONCE(1); | |
1269 | } | |
1270 | ||
1271 | return lq_sta->max_legacy_rate_idx; | |
1272 | } | |
1273 | ||
a34529e8 | 1274 | static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta, |
198266a3 ES |
1275 | const struct rs_tx_column *column, |
1276 | u32 bw) | |
8ca151b5 JB |
1277 | { |
1278 | /* Used to choose among HT tables */ | |
a34529e8 | 1279 | const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT]; |
8ca151b5 | 1280 | |
b3b06a32 ES |
1281 | if (WARN_ON_ONCE(column->mode != RS_LEGACY && |
1282 | column->mode != RS_SISO && | |
1283 | column->mode != RS_MIMO2)) | |
1284 | return expected_tpt_legacy; | |
8ca151b5 JB |
1285 | |
1286 | /* Legacy rates have only one table */ | |
b3b06a32 ES |
1287 | if (column->mode == RS_LEGACY) |
1288 | return expected_tpt_legacy; | |
8ca151b5 | 1289 | |
d310e405 | 1290 | ht_tbl_pointer = expected_tpt_mimo2_20MHz; |
8ca151b5 | 1291 | /* Choose among many HT tables depending on number of streams |
d310e405 | 1292 | * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation |
8ca151b5 | 1293 | * status */ |
b3b06a32 ES |
1294 | if (column->mode == RS_SISO) { |
1295 | switch (bw) { | |
d310e405 ES |
1296 | case RATE_MCS_CHAN_WIDTH_20: |
1297 | ht_tbl_pointer = expected_tpt_siso_20MHz; | |
1298 | break; | |
1299 | case RATE_MCS_CHAN_WIDTH_40: | |
1300 | ht_tbl_pointer = expected_tpt_siso_40MHz; | |
1301 | break; | |
1302 | case RATE_MCS_CHAN_WIDTH_80: | |
1303 | ht_tbl_pointer = expected_tpt_siso_80MHz; | |
1304 | break; | |
1305 | default: | |
1306 | WARN_ON_ONCE(1); | |
1307 | } | |
b3b06a32 ES |
1308 | } else if (column->mode == RS_MIMO2) { |
1309 | switch (bw) { | |
d310e405 ES |
1310 | case RATE_MCS_CHAN_WIDTH_20: |
1311 | ht_tbl_pointer = expected_tpt_mimo2_20MHz; | |
1312 | break; | |
1313 | case RATE_MCS_CHAN_WIDTH_40: | |
1314 | ht_tbl_pointer = expected_tpt_mimo2_40MHz; | |
1315 | break; | |
1316 | case RATE_MCS_CHAN_WIDTH_80: | |
1317 | ht_tbl_pointer = expected_tpt_mimo2_80MHz; | |
1318 | break; | |
1319 | default: | |
1320 | WARN_ON_ONCE(1); | |
1321 | } | |
1322 | } else { | |
1323 | WARN_ON_ONCE(1); | |
d972ab31 | 1324 | } |
8ca151b5 | 1325 | |
b3b06a32 ES |
1326 | if (!column->sgi && !lq_sta->is_agg) /* Normal */ |
1327 | return ht_tbl_pointer[0]; | |
1328 | else if (column->sgi && !lq_sta->is_agg) /* SGI */ | |
1329 | return ht_tbl_pointer[1]; | |
1330 | else if (!column->sgi && lq_sta->is_agg) /* AGG */ | |
1331 | return ht_tbl_pointer[2]; | |
8ca151b5 | 1332 | else /* AGG+SGI */ |
b3b06a32 ES |
1333 | return ht_tbl_pointer[3]; |
1334 | } | |
1335 | ||
1336 | static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta, | |
1337 | struct iwl_scale_tbl_info *tbl) | |
1338 | { | |
1339 | struct rs_rate *rate = &tbl->rate; | |
1340 | const struct rs_tx_column *column = &rs_tx_columns[tbl->column]; | |
1341 | ||
1342 | tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw); | |
8ca151b5 JB |
1343 | } |
1344 | ||
8ca151b5 JB |
1345 | static s32 rs_get_best_rate(struct iwl_mvm *mvm, |
1346 | struct iwl_lq_sta *lq_sta, | |
1347 | struct iwl_scale_tbl_info *tbl, /* "search" */ | |
80763515 | 1348 | unsigned long rate_mask, s8 index) |
8ca151b5 | 1349 | { |
8ca151b5 JB |
1350 | struct iwl_scale_tbl_info *active_tbl = |
1351 | &(lq_sta->lq_info[lq_sta->active_tbl]); | |
80763515 ES |
1352 | s32 success_ratio = active_tbl->win[index].success_ratio; |
1353 | u16 expected_current_tpt = active_tbl->expected_tpt[index]; | |
a34529e8 | 1354 | const u16 *tpt_tbl = tbl->expected_tpt; |
8ca151b5 | 1355 | u16 high_low; |
80763515 ES |
1356 | u32 target_tpt; |
1357 | int rate_idx; | |
8ca151b5 | 1358 | |
80763515 ES |
1359 | if (success_ratio > RS_SR_NO_DECREASE) { |
1360 | target_tpt = 100 * expected_current_tpt; | |
1361 | IWL_DEBUG_RATE(mvm, | |
1362 | "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n", | |
1363 | success_ratio, target_tpt); | |
1364 | } else { | |
1365 | target_tpt = lq_sta->last_tpt; | |
1366 | IWL_DEBUG_RATE(mvm, | |
1367 | "SR %d not thag good. Find rate exceeding ACTUAL_TPT %d\n", | |
1368 | success_ratio, target_tpt); | |
1369 | } | |
8ca151b5 | 1370 | |
80763515 | 1371 | rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG); |
8ca151b5 | 1372 | |
80763515 ES |
1373 | while (rate_idx != IWL_RATE_INVALID) { |
1374 | if (target_tpt < (100 * tpt_tbl[rate_idx])) | |
1375 | break; | |
8ca151b5 | 1376 | |
80763515 ES |
1377 | high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask, |
1378 | tbl->rate.type); | |
8ca151b5 | 1379 | |
80763515 | 1380 | rate_idx = (high_low >> 8) & 0xff; |
8ca151b5 JB |
1381 | } |
1382 | ||
80763515 ES |
1383 | IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n", |
1384 | rate_idx, target_tpt, | |
1385 | rate_idx != IWL_RATE_INVALID ? | |
1386 | 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE); | |
1387 | ||
1388 | return rate_idx; | |
8ca151b5 JB |
1389 | } |
1390 | ||
5aa33553 | 1391 | static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta) |
d310e405 ES |
1392 | { |
1393 | if (sta->bandwidth >= IEEE80211_STA_RX_BW_80) | |
5aa33553 | 1394 | return RATE_MCS_CHAN_WIDTH_80; |
d310e405 | 1395 | else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) |
5aa33553 ES |
1396 | return RATE_MCS_CHAN_WIDTH_40; |
1397 | ||
1398 | return RATE_MCS_CHAN_WIDTH_20; | |
d310e405 ES |
1399 | } |
1400 | ||
8ca151b5 JB |
1401 | /* |
1402 | * Check whether we should continue using same modulation mode, or | |
1403 | * begin search for a new mode, based on: | |
1404 | * 1) # tx successes or failures while using this mode | |
1405 | * 2) # times calling this function | |
1406 | * 3) elapsed time in this mode (not used, for now) | |
1407 | */ | |
1408 | static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search) | |
1409 | { | |
1410 | struct iwl_scale_tbl_info *tbl; | |
8ca151b5 JB |
1411 | int active_tbl; |
1412 | int flush_interval_passed = 0; | |
1413 | struct iwl_mvm *mvm; | |
1414 | ||
ae969afe | 1415 | mvm = lq_sta->pers.drv; |
8ca151b5 JB |
1416 | active_tbl = lq_sta->active_tbl; |
1417 | ||
1418 | tbl = &(lq_sta->lq_info[active_tbl]); | |
1419 | ||
1420 | /* If we've been disallowing search, see if we should now allow it */ | |
b3b06a32 | 1421 | if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) { |
8ca151b5 JB |
1422 | /* Elapsed time using current modulation mode */ |
1423 | if (lq_sta->flush_timer) | |
1424 | flush_interval_passed = | |
1425 | time_after(jiffies, | |
1426 | (unsigned long)(lq_sta->flush_timer + | |
32b01726 | 1427 | RS_STAY_IN_COLUMN_TIMEOUT)); |
8ca151b5 JB |
1428 | |
1429 | /* | |
1430 | * Check if we should allow search for new modulation mode. | |
1431 | * If many frames have failed or succeeded, or we've used | |
1432 | * this same modulation for a long time, allow search, and | |
1433 | * reset history stats that keep track of whether we should | |
1434 | * allow a new search. Also (below) reset all bitmaps and | |
1435 | * stats in active history. | |
1436 | */ | |
1437 | if (force_search || | |
1438 | (lq_sta->total_failed > lq_sta->max_failure_limit) || | |
1439 | (lq_sta->total_success > lq_sta->max_success_limit) || | |
1440 | ((!lq_sta->search_better_tbl) && | |
1441 | (lq_sta->flush_timer) && (flush_interval_passed))) { | |
1442 | IWL_DEBUG_RATE(mvm, | |
1443 | "LQ: stay is expired %d %d %d\n", | |
1444 | lq_sta->total_failed, | |
1445 | lq_sta->total_success, | |
1446 | flush_interval_passed); | |
1447 | ||
1448 | /* Allow search for new mode */ | |
b3b06a32 ES |
1449 | lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED; |
1450 | IWL_DEBUG_RATE(mvm, | |
1451 | "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n"); | |
8ca151b5 JB |
1452 | lq_sta->total_failed = 0; |
1453 | lq_sta->total_success = 0; | |
1454 | lq_sta->flush_timer = 0; | |
b3b06a32 ES |
1455 | /* mark the current column as visited */ |
1456 | lq_sta->visited_columns = BIT(tbl->column); | |
8ca151b5 JB |
1457 | /* |
1458 | * Else if we've used this modulation mode enough repetitions | |
1459 | * (regardless of elapsed time or success/failure), reset | |
1460 | * history bitmaps and rate-specific stats for all rates in | |
1461 | * active table. | |
1462 | */ | |
1463 | } else { | |
1464 | lq_sta->table_count++; | |
1465 | if (lq_sta->table_count >= | |
1466 | lq_sta->table_count_limit) { | |
1467 | lq_sta->table_count = 0; | |
1468 | ||
1469 | IWL_DEBUG_RATE(mvm, | |
1470 | "LQ: stay in table clear win\n"); | |
b804eeb6 | 1471 | rs_rate_scale_clear_tbl_windows(mvm, tbl); |
8ca151b5 JB |
1472 | } |
1473 | } | |
1474 | ||
1475 | /* If transitioning to allow "search", reset all history | |
1476 | * bitmaps and stats in active table (this will become the new | |
1477 | * "search" table). */ | |
b3b06a32 | 1478 | if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) { |
b804eeb6 | 1479 | rs_rate_scale_clear_tbl_windows(mvm, tbl); |
8ca151b5 JB |
1480 | } |
1481 | } | |
1482 | } | |
1483 | ||
1484 | /* | |
1485 | * setup rate table in uCode | |
1486 | */ | |
1487 | static void rs_update_rate_tbl(struct iwl_mvm *mvm, | |
9145d151 | 1488 | struct ieee80211_sta *sta, |
8ca151b5 | 1489 | struct iwl_lq_sta *lq_sta, |
5aa33553 | 1490 | struct rs_rate *rate) |
8ca151b5 | 1491 | { |
8fc7c58c | 1492 | rs_fill_lq_cmd(mvm, sta, lq_sta, rate); |
9e680946 | 1493 | iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false); |
8ca151b5 JB |
1494 | } |
1495 | ||
977342bc ES |
1496 | static u8 rs_get_tid(struct iwl_lq_sta *lq_data, |
1497 | struct ieee80211_hdr *hdr) | |
1498 | { | |
1499 | u8 tid = IWL_MAX_TID_COUNT; | |
1500 | ||
1501 | if (ieee80211_is_data_qos(hdr->frame_control)) { | |
1502 | u8 *qc = ieee80211_get_qos_ctl(hdr); | |
1503 | tid = qc[0] & 0xf; | |
1504 | } | |
1505 | ||
1506 | if (unlikely(tid > IWL_MAX_TID_COUNT)) | |
1507 | tid = IWL_MAX_TID_COUNT; | |
1508 | ||
1509 | return tid; | |
1510 | } | |
1511 | ||
b3b06a32 ES |
1512 | static enum rs_column rs_get_next_column(struct iwl_mvm *mvm, |
1513 | struct iwl_lq_sta *lq_sta, | |
1514 | struct ieee80211_sta *sta, | |
1515 | struct iwl_scale_tbl_info *tbl) | |
1516 | { | |
198266a3 | 1517 | int i, j, max_rate; |
b3b06a32 ES |
1518 | enum rs_column next_col_id; |
1519 | const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column]; | |
1520 | const struct rs_tx_column *next_col; | |
1521 | allow_column_func_t allow_func; | |
4ed735e7 | 1522 | u8 valid_ants = mvm->fw->valid_tx_ant; |
a34529e8 | 1523 | const u16 *expected_tpt_tbl; |
198266a3 | 1524 | u16 tpt, max_expected_tpt; |
b3b06a32 ES |
1525 | |
1526 | for (i = 0; i < MAX_NEXT_COLUMNS; i++) { | |
1527 | next_col_id = curr_col->next_columns[i]; | |
1528 | ||
1529 | if (next_col_id == RS_COLUMN_INVALID) | |
1530 | continue; | |
1531 | ||
1532 | if (lq_sta->visited_columns & BIT(next_col_id)) { | |
1533 | IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n", | |
1534 | next_col_id); | |
1535 | continue; | |
1536 | } | |
1537 | ||
1538 | next_col = &rs_tx_columns[next_col_id]; | |
1539 | ||
1540 | if (!rs_is_valid_ant(valid_ants, next_col->ant)) { | |
1541 | IWL_DEBUG_RATE(mvm, | |
1542 | "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n", | |
1543 | next_col_id, valid_ants, next_col->ant); | |
1544 | continue; | |
1545 | } | |
1546 | ||
1547 | for (j = 0; j < MAX_COLUMN_CHECKS; j++) { | |
1548 | allow_func = next_col->checks[j]; | |
1549 | if (allow_func && !allow_func(mvm, sta, tbl)) | |
1550 | break; | |
1551 | } | |
1552 | ||
1553 | if (j != MAX_COLUMN_CHECKS) { | |
1554 | IWL_DEBUG_RATE(mvm, | |
1555 | "Skip column %d: not allowed (check %d failed)\n", | |
1556 | next_col_id, j); | |
1557 | ||
1558 | continue; | |
1559 | } | |
1560 | ||
1561 | tpt = lq_sta->last_tpt / 100; | |
1562 | expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col, | |
049e0c33 | 1563 | rs_bw_from_sta_bw(sta)); |
b3b06a32 ES |
1564 | if (WARN_ON_ONCE(!expected_tpt_tbl)) |
1565 | continue; | |
1566 | ||
198266a3 ES |
1567 | max_rate = rs_get_max_allowed_rate(lq_sta, next_col); |
1568 | if (WARN_ON_ONCE(max_rate == IWL_RATE_INVALID)) | |
1569 | continue; | |
b3b06a32 | 1570 | |
198266a3 | 1571 | max_expected_tpt = expected_tpt_tbl[max_rate]; |
b3b06a32 ES |
1572 | if (tpt >= max_expected_tpt) { |
1573 | IWL_DEBUG_RATE(mvm, | |
1574 | "Skip column %d: can't beat current TPT. Max expected %d current %d\n", | |
1575 | next_col_id, max_expected_tpt, tpt); | |
1576 | continue; | |
1577 | } | |
1578 | ||
198266a3 ES |
1579 | IWL_DEBUG_RATE(mvm, |
1580 | "Found potential column %d. Max expected %d current %d\n", | |
1581 | next_col_id, max_expected_tpt, tpt); | |
b3b06a32 ES |
1582 | break; |
1583 | } | |
1584 | ||
1585 | if (i == MAX_NEXT_COLUMNS) | |
1586 | return RS_COLUMN_INVALID; | |
1587 | ||
b3b06a32 ES |
1588 | return next_col_id; |
1589 | } | |
1590 | ||
1591 | static int rs_switch_to_column(struct iwl_mvm *mvm, | |
1592 | struct iwl_lq_sta *lq_sta, | |
1593 | struct ieee80211_sta *sta, | |
1594 | enum rs_column col_id) | |
1595 | { | |
1596 | struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); | |
1597 | struct iwl_scale_tbl_info *search_tbl = | |
1598 | &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]); | |
1599 | struct rs_rate *rate = &search_tbl->rate; | |
1600 | const struct rs_tx_column *column = &rs_tx_columns[col_id]; | |
1601 | const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column]; | |
1602 | u32 sz = (sizeof(struct iwl_scale_tbl_info) - | |
1603 | (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT)); | |
80763515 | 1604 | unsigned long rate_mask = 0; |
b3b06a32 ES |
1605 | u32 rate_idx = 0; |
1606 | ||
1607 | memcpy(search_tbl, tbl, sz); | |
1608 | ||
1609 | rate->sgi = column->sgi; | |
1610 | rate->ant = column->ant; | |
1611 | ||
1612 | if (column->mode == RS_LEGACY) { | |
1613 | if (lq_sta->band == IEEE80211_BAND_5GHZ) | |
1614 | rate->type = LQ_LEGACY_A; | |
1615 | else | |
1616 | rate->type = LQ_LEGACY_G; | |
1617 | ||
1618 | rate_mask = lq_sta->active_legacy_rate; | |
1619 | } else if (column->mode == RS_SISO) { | |
1620 | rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO; | |
1621 | rate_mask = lq_sta->active_siso_rate; | |
1622 | } else if (column->mode == RS_MIMO2) { | |
1623 | rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2; | |
1624 | rate_mask = lq_sta->active_mimo2_rate; | |
1625 | } else { | |
1626 | WARN_ON_ONCE("Bad column mode"); | |
1627 | } | |
1628 | ||
1629 | rate->bw = rs_bw_from_sta_bw(sta); | |
a3576ff2 | 1630 | rate->ldpc = lq_sta->ldpc; |
b3b06a32 ES |
1631 | search_tbl->column = col_id; |
1632 | rs_set_expected_tpt_table(lq_sta, search_tbl); | |
1633 | ||
75d3e2d6 ES |
1634 | lq_sta->visited_columns |= BIT(col_id); |
1635 | ||
b3b06a32 ES |
1636 | /* Get the best matching rate if we're changing modes. e.g. |
1637 | * SISO->MIMO, LEGACY->SISO, MIMO->SISO | |
1638 | */ | |
1639 | if (curr_column->mode != column->mode) { | |
1640 | rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl, | |
1641 | rate_mask, rate->index); | |
1642 | ||
1643 | if ((rate_idx == IWL_RATE_INVALID) || | |
1644 | !(BIT(rate_idx) & rate_mask)) { | |
1645 | IWL_DEBUG_RATE(mvm, | |
1646 | "can not switch with index %d" | |
80763515 | 1647 | " rate mask %lx\n", |
b3b06a32 ES |
1648 | rate_idx, rate_mask); |
1649 | ||
1650 | goto err; | |
1651 | } | |
1652 | ||
1653 | rate->index = rate_idx; | |
1654 | } | |
1655 | ||
b3b06a32 ES |
1656 | IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n", |
1657 | col_id, rate->index); | |
1658 | ||
b3b06a32 ES |
1659 | return 0; |
1660 | ||
1661 | err: | |
1662 | rate->type = LQ_NONE; | |
1663 | return -1; | |
1664 | } | |
1665 | ||
4e4b815c ES |
1666 | static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm, |
1667 | struct iwl_scale_tbl_info *tbl, | |
1668 | s32 sr, int low, int high, | |
1669 | int current_tpt, | |
1670 | int low_tpt, int high_tpt) | |
1671 | { | |
1672 | enum rs_action action = RS_ACTION_STAY; | |
1673 | ||
4e4b815c ES |
1674 | if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) { |
1675 | IWL_DEBUG_RATE(mvm, | |
e53839eb ES |
1676 | "Decrease rate because of low SR\n"); |
1677 | return RS_ACTION_DOWNSCALE; | |
4e4b815c ES |
1678 | } |
1679 | ||
e53839eb ES |
1680 | if ((low_tpt == IWL_INVALID_VALUE) && |
1681 | (high_tpt == IWL_INVALID_VALUE) && | |
1682 | (high != IWL_RATE_INVALID)) { | |
4e4b815c | 1683 | IWL_DEBUG_RATE(mvm, |
e53839eb ES |
1684 | "No data about high/low rates. Increase rate\n"); |
1685 | return RS_ACTION_UPSCALE; | |
4e4b815c ES |
1686 | } |
1687 | ||
e53839eb ES |
1688 | if ((high_tpt == IWL_INVALID_VALUE) && |
1689 | (high != IWL_RATE_INVALID) && | |
1690 | (low_tpt != IWL_INVALID_VALUE) && | |
1691 | (low_tpt < current_tpt)) { | |
1692 | IWL_DEBUG_RATE(mvm, | |
1693 | "No data about high rate and low rate is worse. Increase rate\n"); | |
1694 | return RS_ACTION_UPSCALE; | |
1695 | } | |
4e4b815c | 1696 | |
e53839eb ES |
1697 | if ((high_tpt != IWL_INVALID_VALUE) && |
1698 | (high_tpt > current_tpt)) { | |
1699 | IWL_DEBUG_RATE(mvm, | |
1700 | "Higher rate is better. Increate rate\n"); | |
1701 | return RS_ACTION_UPSCALE; | |
4e4b815c ES |
1702 | } |
1703 | ||
e53839eb ES |
1704 | if ((low_tpt != IWL_INVALID_VALUE) && |
1705 | (high_tpt != IWL_INVALID_VALUE) && | |
1706 | (low_tpt < current_tpt) && | |
1707 | (high_tpt < current_tpt)) { | |
4e4b815c | 1708 | IWL_DEBUG_RATE(mvm, |
e53839eb ES |
1709 | "Both high and low are worse. Maintain rate\n"); |
1710 | return RS_ACTION_STAY; | |
1711 | } | |
1712 | ||
1713 | if ((low_tpt != IWL_INVALID_VALUE) && | |
1714 | (low_tpt > current_tpt)) { | |
1715 | IWL_DEBUG_RATE(mvm, | |
1716 | "Lower rate is better\n"); | |
1717 | action = RS_ACTION_DOWNSCALE; | |
1718 | goto out; | |
1719 | } | |
1720 | ||
1721 | if ((low_tpt == IWL_INVALID_VALUE) && | |
1722 | (low != IWL_RATE_INVALID)) { | |
4e4b815c | 1723 | IWL_DEBUG_RATE(mvm, |
e53839eb ES |
1724 | "No data about lower rate\n"); |
1725 | action = RS_ACTION_DOWNSCALE; | |
1726 | goto out; | |
1727 | } | |
1728 | ||
1729 | IWL_DEBUG_RATE(mvm, "Maintain rate\n"); | |
1730 | ||
1731 | out: | |
1732 | if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) { | |
1733 | if (sr >= RS_SR_NO_DECREASE) { | |
1734 | IWL_DEBUG_RATE(mvm, | |
1735 | "SR is above NO DECREASE. Avoid downscale\n"); | |
1736 | action = RS_ACTION_STAY; | |
1737 | } else if (current_tpt > (100 * tbl->expected_tpt[low])) { | |
1738 | IWL_DEBUG_RATE(mvm, | |
1739 | "Current TPT is higher than max expected in low rate. Avoid downscale\n"); | |
1740 | action = RS_ACTION_STAY; | |
1741 | } else { | |
1742 | IWL_DEBUG_RATE(mvm, "Decrease rate\n"); | |
1743 | } | |
4e4b815c ES |
1744 | } |
1745 | ||
1746 | return action; | |
1747 | } | |
b3b06a32 | 1748 | |
2fd647f8 EP |
1749 | static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index, |
1750 | int *weaker, int *stronger) | |
1751 | { | |
1752 | *weaker = index + TPC_TX_POWER_STEP; | |
1753 | if (*weaker > TPC_MAX_REDUCTION) | |
1754 | *weaker = TPC_INVALID; | |
1755 | ||
1756 | *stronger = index - TPC_TX_POWER_STEP; | |
1757 | if (*stronger < 0) | |
1758 | *stronger = TPC_INVALID; | |
1759 | } | |
1760 | ||
1e9551de ES |
1761 | static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif, |
1762 | struct rs_rate *rate, enum ieee80211_band band) | |
2fd647f8 EP |
1763 | { |
1764 | int index = rate->index; | |
1e9551de ES |
1765 | bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM); |
1766 | bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION && | |
1767 | !vif->bss_conf.ps); | |
2fd647f8 | 1768 | |
1e9551de ES |
1769 | IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n", |
1770 | cam, sta_ps_disabled); | |
2fd647f8 EP |
1771 | /* |
1772 | * allow tpc only if power management is enabled, or bt coex | |
1773 | * activity grade allows it and we are on 2.4Ghz. | |
1774 | */ | |
1e9551de | 1775 | if ((cam || sta_ps_disabled) && |
2fd647f8 EP |
1776 | !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band)) |
1777 | return false; | |
1778 | ||
1779 | IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type); | |
1780 | if (is_legacy(rate)) | |
1781 | return index == IWL_RATE_54M_INDEX; | |
1782 | if (is_ht(rate)) | |
1783 | return index == IWL_RATE_MCS_7_INDEX; | |
1784 | if (is_vht(rate)) | |
1785 | return index == IWL_RATE_MCS_7_INDEX || | |
1786 | index == IWL_RATE_MCS_8_INDEX || | |
1787 | index == IWL_RATE_MCS_9_INDEX; | |
1788 | ||
1789 | WARN_ON_ONCE(1); | |
1790 | return false; | |
1791 | } | |
1792 | ||
1793 | enum tpc_action { | |
1794 | TPC_ACTION_STAY, | |
1795 | TPC_ACTION_DECREASE, | |
1796 | TPC_ACTION_INCREASE, | |
1797 | TPC_ACTION_NO_RESTIRCTION, | |
1798 | }; | |
1799 | ||
1800 | static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm, | |
1801 | s32 sr, int weak, int strong, | |
1802 | int current_tpt, | |
1803 | int weak_tpt, int strong_tpt) | |
1804 | { | |
1805 | /* stay until we have valid tpt */ | |
1806 | if (current_tpt == IWL_INVALID_VALUE) { | |
1807 | IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n"); | |
1808 | return TPC_ACTION_STAY; | |
1809 | } | |
1810 | ||
1811 | /* Too many failures, increase txp */ | |
1812 | if (sr <= TPC_SR_FORCE_INCREASE || current_tpt == 0) { | |
1813 | IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n"); | |
1814 | return TPC_ACTION_NO_RESTIRCTION; | |
1815 | } | |
1816 | ||
1817 | /* try decreasing first if applicable */ | |
1818 | if (weak != TPC_INVALID) { | |
1819 | if (weak_tpt == IWL_INVALID_VALUE && | |
1820 | (strong_tpt == IWL_INVALID_VALUE || | |
1821 | current_tpt >= strong_tpt)) { | |
1822 | IWL_DEBUG_RATE(mvm, | |
1823 | "no weak txp measurement. decrease txp\n"); | |
1824 | return TPC_ACTION_DECREASE; | |
1825 | } | |
1826 | ||
1827 | if (weak_tpt > current_tpt) { | |
1828 | IWL_DEBUG_RATE(mvm, | |
1829 | "lower txp has better tpt. decrease txp\n"); | |
1830 | return TPC_ACTION_DECREASE; | |
1831 | } | |
1832 | } | |
1833 | ||
1834 | /* next, increase if needed */ | |
1835 | if (sr < TPC_SR_NO_INCREASE && strong != TPC_INVALID) { | |
1836 | if (weak_tpt == IWL_INVALID_VALUE && | |
1837 | strong_tpt != IWL_INVALID_VALUE && | |
1838 | current_tpt < strong_tpt) { | |
1839 | IWL_DEBUG_RATE(mvm, | |
1840 | "higher txp has better tpt. increase txp\n"); | |
1841 | return TPC_ACTION_INCREASE; | |
1842 | } | |
1843 | ||
1844 | if (weak_tpt < current_tpt && | |
1845 | (strong_tpt == IWL_INVALID_VALUE || | |
1846 | strong_tpt > current_tpt)) { | |
1847 | IWL_DEBUG_RATE(mvm, | |
1848 | "lower txp has worse tpt. increase txp\n"); | |
1849 | return TPC_ACTION_INCREASE; | |
1850 | } | |
1851 | } | |
1852 | ||
1853 | IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n"); | |
1854 | return TPC_ACTION_STAY; | |
1855 | } | |
1856 | ||
1857 | static bool rs_tpc_perform(struct iwl_mvm *mvm, | |
1858 | struct ieee80211_sta *sta, | |
1859 | struct iwl_lq_sta *lq_sta, | |
1860 | struct iwl_scale_tbl_info *tbl) | |
1861 | { | |
1862 | struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv; | |
1863 | struct ieee80211_vif *vif = mvm_sta->vif; | |
1864 | struct ieee80211_chanctx_conf *chanctx_conf; | |
1865 | enum ieee80211_band band; | |
1866 | struct iwl_rate_scale_data *window; | |
1867 | struct rs_rate *rate = &tbl->rate; | |
1868 | enum tpc_action action; | |
1869 | s32 sr; | |
1870 | u8 cur = lq_sta->lq.reduced_tpc; | |
1871 | int current_tpt; | |
1872 | int weak, strong; | |
1873 | int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE; | |
1874 | ||
9a75b3df | 1875 | #ifdef CONFIG_MAC80211_DEBUGFS |
ae969afe | 1876 | if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) { |
3c6acb61 | 1877 | IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n", |
ae969afe EP |
1878 | lq_sta->pers.dbg_fixed_txp_reduction); |
1879 | lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction; | |
1880 | return cur != lq_sta->pers.dbg_fixed_txp_reduction; | |
9a75b3df EP |
1881 | } |
1882 | #endif | |
1883 | ||
2fd647f8 EP |
1884 | rcu_read_lock(); |
1885 | chanctx_conf = rcu_dereference(vif->chanctx_conf); | |
1886 | if (WARN_ON(!chanctx_conf)) | |
1887 | band = IEEE80211_NUM_BANDS; | |
1888 | else | |
1889 | band = chanctx_conf->def.chan->band; | |
1890 | rcu_read_unlock(); | |
1891 | ||
1e9551de | 1892 | if (!rs_tpc_allowed(mvm, vif, rate, band)) { |
2fd647f8 | 1893 | IWL_DEBUG_RATE(mvm, |
3c6acb61 | 1894 | "tpc is not allowed. remove txp restrictions\n"); |
2fd647f8 EP |
1895 | lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION; |
1896 | return cur != TPC_NO_REDUCTION; | |
1897 | } | |
1898 | ||
1899 | rs_get_adjacent_txp(mvm, cur, &weak, &strong); | |
1900 | ||
1901 | /* Collect measured throughputs for current and adjacent rates */ | |
1902 | window = tbl->tpc_win; | |
1903 | sr = window[cur].success_ratio; | |
1904 | current_tpt = window[cur].average_tpt; | |
1905 | if (weak != TPC_INVALID) | |
1906 | weak_tpt = window[weak].average_tpt; | |
1907 | if (strong != TPC_INVALID) | |
1908 | strong_tpt = window[strong].average_tpt; | |
1909 | ||
1910 | IWL_DEBUG_RATE(mvm, | |
1911 | "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n", | |
1912 | cur, current_tpt, sr, weak, strong, | |
1913 | weak_tpt, strong_tpt); | |
1914 | ||
1915 | action = rs_get_tpc_action(mvm, sr, weak, strong, | |
1916 | current_tpt, weak_tpt, strong_tpt); | |
1917 | ||
1918 | /* override actions if we are on the edge */ | |
1919 | if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) { | |
3c6acb61 | 1920 | IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n"); |
2fd647f8 EP |
1921 | action = TPC_ACTION_STAY; |
1922 | } else if (strong == TPC_INVALID && | |
1923 | (action == TPC_ACTION_INCREASE || | |
1924 | action == TPC_ACTION_NO_RESTIRCTION)) { | |
3c6acb61 | 1925 | IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n"); |
2fd647f8 EP |
1926 | action = TPC_ACTION_STAY; |
1927 | } | |
1928 | ||
1929 | switch (action) { | |
1930 | case TPC_ACTION_DECREASE: | |
1931 | lq_sta->lq.reduced_tpc = weak; | |
1932 | return true; | |
1933 | case TPC_ACTION_INCREASE: | |
1934 | lq_sta->lq.reduced_tpc = strong; | |
1935 | return true; | |
1936 | case TPC_ACTION_NO_RESTIRCTION: | |
1937 | lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION; | |
1938 | return true; | |
1939 | case TPC_ACTION_STAY: | |
1940 | /* do nothing */ | |
1941 | break; | |
1942 | } | |
1943 | return false; | |
1944 | } | |
1945 | ||
8ca151b5 JB |
1946 | /* |
1947 | * Do rate scaling and search for new modulation mode. | |
1948 | */ | |
1949 | static void rs_rate_scale_perform(struct iwl_mvm *mvm, | |
1950 | struct sk_buff *skb, | |
1951 | struct ieee80211_sta *sta, | |
1952 | struct iwl_lq_sta *lq_sta) | |
1953 | { | |
1954 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
1955 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; | |
1956 | int low = IWL_RATE_INVALID; | |
1957 | int high = IWL_RATE_INVALID; | |
1958 | int index; | |
8ca151b5 JB |
1959 | struct iwl_rate_scale_data *window = NULL; |
1960 | int current_tpt = IWL_INVALID_VALUE; | |
1961 | int low_tpt = IWL_INVALID_VALUE; | |
1962 | int high_tpt = IWL_INVALID_VALUE; | |
1963 | u32 fail_count; | |
4e4b815c | 1964 | enum rs_action scale_action = RS_ACTION_STAY; |
8ca151b5 JB |
1965 | u16 rate_mask; |
1966 | u8 update_lq = 0; | |
1967 | struct iwl_scale_tbl_info *tbl, *tbl1; | |
8ca151b5 JB |
1968 | u8 active_tbl = 0; |
1969 | u8 done_search = 0; | |
1970 | u16 high_low; | |
1971 | s32 sr; | |
1972 | u8 tid = IWL_MAX_TID_COUNT; | |
9d015a03 | 1973 | u8 prev_agg = lq_sta->is_agg; |
8ca151b5 JB |
1974 | struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv; |
1975 | struct iwl_mvm_tid_data *tid_data; | |
5aa33553 | 1976 | struct rs_rate *rate; |
8ca151b5 | 1977 | |
8ca151b5 JB |
1978 | /* Send management frames and NO_ACK data using lowest rate. */ |
1979 | /* TODO: this could probably be improved.. */ | |
1980 | if (!ieee80211_is_data(hdr->frame_control) || | |
1981 | info->flags & IEEE80211_TX_CTL_NO_ACK) | |
1982 | return; | |
1983 | ||
977342bc | 1984 | tid = rs_get_tid(lq_sta, hdr); |
8ca151b5 JB |
1985 | if ((tid != IWL_MAX_TID_COUNT) && |
1986 | (lq_sta->tx_agg_tid_en & (1 << tid))) { | |
1987 | tid_data = &sta_priv->tid_data[tid]; | |
1988 | if (tid_data->state == IWL_AGG_OFF) | |
1989 | lq_sta->is_agg = 0; | |
1990 | else | |
1991 | lq_sta->is_agg = 1; | |
1992 | } else { | |
1993 | lq_sta->is_agg = 0; | |
1994 | } | |
1995 | ||
1996 | /* | |
1997 | * Select rate-scale / modulation-mode table to work with in | |
1998 | * the rest of this function: "search" if searching for better | |
1999 | * modulation mode, or "active" if doing rate scaling within a mode. | |
2000 | */ | |
2001 | if (!lq_sta->search_better_tbl) | |
2002 | active_tbl = lq_sta->active_tbl; | |
2003 | else | |
2004 | active_tbl = 1 - lq_sta->active_tbl; | |
2005 | ||
2006 | tbl = &(lq_sta->lq_info[active_tbl]); | |
5aa33553 | 2007 | rate = &tbl->rate; |
8ca151b5 | 2008 | |
9d015a03 ES |
2009 | if (prev_agg != lq_sta->is_agg) { |
2010 | IWL_DEBUG_RATE(mvm, | |
2011 | "Aggregation changed: prev %d current %d. Update expected TPT table\n", | |
2012 | prev_agg, lq_sta->is_agg); | |
2013 | rs_set_expected_tpt_table(lq_sta, tbl); | |
b804eeb6 | 2014 | rs_rate_scale_clear_tbl_windows(mvm, tbl); |
9d015a03 ES |
2015 | } |
2016 | ||
8ca151b5 JB |
2017 | /* current tx rate */ |
2018 | index = lq_sta->last_txrate_idx; | |
2019 | ||
8ca151b5 | 2020 | /* rates available for this association, and for modulation mode */ |
a8ff14f5 | 2021 | rate_mask = rs_get_supported_rates(lq_sta, rate); |
8ca151b5 | 2022 | |
ca6f38ff | 2023 | if (!(BIT(index) & rate_mask)) { |
8ca151b5 JB |
2024 | IWL_ERR(mvm, "Current Rate is not valid\n"); |
2025 | if (lq_sta->search_better_tbl) { | |
2026 | /* revert to active table if search table is not valid*/ | |
5aa33553 | 2027 | rate->type = LQ_NONE; |
8ca151b5 JB |
2028 | lq_sta->search_better_tbl = 0; |
2029 | tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); | |
5aa33553 | 2030 | rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate); |
8ca151b5 JB |
2031 | } |
2032 | return; | |
2033 | } | |
2034 | ||
2035 | /* Get expected throughput table and history window for current rate */ | |
2036 | if (!tbl->expected_tpt) { | |
2037 | IWL_ERR(mvm, "tbl->expected_tpt is NULL\n"); | |
2038 | return; | |
2039 | } | |
2040 | ||
2536e7c3 | 2041 | /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */ |
8ca151b5 JB |
2042 | window = &(tbl->win[index]); |
2043 | ||
2044 | /* | |
2045 | * If there is not enough history to calculate actual average | |
2046 | * throughput, keep analyzing results of more tx frames, without | |
2047 | * changing rate or mode (bypass most of the rest of this function). | |
2048 | * Set up new rate table in uCode only if old rate is not supported | |
2049 | * in current association (use new rate found above). | |
2050 | */ | |
2051 | fail_count = window->counter - window->success_counter; | |
2052 | if ((fail_count < IWL_RATE_MIN_FAILURE_TH) && | |
2053 | (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) { | |
2054 | IWL_DEBUG_RATE(mvm, | |
4d30ee82 | 2055 | "(%s: %d): Test Window: succ %d total %d\n", |
5aa33553 | 2056 | rs_pretty_lq_type(rate->type), |
4d30ee82 | 2057 | index, window->success_counter, window->counter); |
8ca151b5 JB |
2058 | |
2059 | /* Can't calculate this yet; not enough history */ | |
2060 | window->average_tpt = IWL_INVALID_VALUE; | |
2061 | ||
2062 | /* Should we stay with this modulation mode, | |
2063 | * or search for a new one? */ | |
2064 | rs_stay_in_table(lq_sta, false); | |
2065 | ||
2066 | goto out; | |
2067 | } | |
2068 | /* Else we have enough samples; calculate estimate of | |
2069 | * actual average throughput */ | |
2070 | if (window->average_tpt != ((window->success_ratio * | |
2071 | tbl->expected_tpt[index] + 64) / 128)) { | |
8ca151b5 JB |
2072 | window->average_tpt = ((window->success_ratio * |
2073 | tbl->expected_tpt[index] + 64) / 128); | |
2074 | } | |
2075 | ||
2076 | /* If we are searching for better modulation mode, check success. */ | |
2077 | if (lq_sta->search_better_tbl) { | |
2078 | /* If good success, continue using the "search" mode; | |
2079 | * no need to send new link quality command, since we're | |
2080 | * continuing to use the setup that we've been trying. */ | |
2081 | if (window->average_tpt > lq_sta->last_tpt) { | |
2082 | IWL_DEBUG_RATE(mvm, | |
4d30ee82 ES |
2083 | "SWITCHING TO NEW TABLE SR: %d " |
2084 | "cur-tpt %d old-tpt %d\n", | |
8ca151b5 JB |
2085 | window->success_ratio, |
2086 | window->average_tpt, | |
2087 | lq_sta->last_tpt); | |
2088 | ||
8ca151b5 JB |
2089 | /* Swap tables; "search" becomes "active" */ |
2090 | lq_sta->active_tbl = active_tbl; | |
2091 | current_tpt = window->average_tpt; | |
2092 | /* Else poor success; go back to mode in "active" table */ | |
2093 | } else { | |
2094 | IWL_DEBUG_RATE(mvm, | |
4d30ee82 ES |
2095 | "GOING BACK TO THE OLD TABLE: SR %d " |
2096 | "cur-tpt %d old-tpt %d\n", | |
8ca151b5 JB |
2097 | window->success_ratio, |
2098 | window->average_tpt, | |
2099 | lq_sta->last_tpt); | |
2100 | ||
2101 | /* Nullify "search" table */ | |
5aa33553 | 2102 | rate->type = LQ_NONE; |
8ca151b5 JB |
2103 | |
2104 | /* Revert to "active" table */ | |
2105 | active_tbl = lq_sta->active_tbl; | |
2106 | tbl = &(lq_sta->lq_info[active_tbl]); | |
2107 | ||
2108 | /* Revert to "active" rate and throughput info */ | |
8fc7c58c | 2109 | index = tbl->rate.index; |
8ca151b5 JB |
2110 | current_tpt = lq_sta->last_tpt; |
2111 | ||
2112 | /* Need to set up a new rate table in uCode */ | |
2113 | update_lq = 1; | |
2114 | } | |
2115 | ||
2116 | /* Either way, we've made a decision; modulation mode | |
2117 | * search is done, allow rate adjustment next time. */ | |
2118 | lq_sta->search_better_tbl = 0; | |
2119 | done_search = 1; /* Don't switch modes below! */ | |
2120 | goto lq_update; | |
2121 | } | |
2122 | ||
2123 | /* (Else) not in search of better modulation mode, try for better | |
2124 | * starting rate, while staying in this mode. */ | |
ca6f38ff | 2125 | high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type); |
8ca151b5 JB |
2126 | low = high_low & 0xff; |
2127 | high = (high_low >> 8) & 0xff; | |
2128 | ||
2536e7c3 | 2129 | /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */ |
8ca151b5 JB |
2130 | |
2131 | sr = window->success_ratio; | |
2132 | ||
2133 | /* Collect measured throughputs for current and adjacent rates */ | |
2134 | current_tpt = window->average_tpt; | |
2135 | if (low != IWL_RATE_INVALID) | |
2136 | low_tpt = tbl->win[low].average_tpt; | |
2137 | if (high != IWL_RATE_INVALID) | |
2138 | high_tpt = tbl->win[high].average_tpt; | |
2139 | ||
4d30ee82 ES |
2140 | IWL_DEBUG_RATE(mvm, |
2141 | "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n", | |
5aa33553 ES |
2142 | rs_pretty_lq_type(rate->type), index, current_tpt, sr, |
2143 | low, high, low_tpt, high_tpt); | |
4d30ee82 | 2144 | |
4e4b815c ES |
2145 | scale_action = rs_get_rate_action(mvm, tbl, sr, low, high, |
2146 | current_tpt, low_tpt, high_tpt); | |
8ca151b5 | 2147 | |
393b9e5b | 2148 | /* Force a search in case BT doesn't like us being in MIMO */ |
5aa33553 | 2149 | if (is_mimo(rate) && |
393b9e5b | 2150 | !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) { |
4d30ee82 ES |
2151 | IWL_DEBUG_RATE(mvm, |
2152 | "BT Coex forbids MIMO. Search for new config\n"); | |
36946ce6 EG |
2153 | rs_stay_in_table(lq_sta, true); |
2154 | goto lq_update; | |
2155 | } | |
2156 | ||
8ca151b5 | 2157 | switch (scale_action) { |
4e4b815c | 2158 | case RS_ACTION_DOWNSCALE: |
8ca151b5 JB |
2159 | /* Decrease starting rate, update uCode's rate table */ |
2160 | if (low != IWL_RATE_INVALID) { | |
2161 | update_lq = 1; | |
2162 | index = low; | |
4d30ee82 ES |
2163 | } else { |
2164 | IWL_DEBUG_RATE(mvm, | |
2165 | "At the bottom rate. Can't decrease\n"); | |
8ca151b5 JB |
2166 | } |
2167 | ||
2168 | break; | |
4e4b815c | 2169 | case RS_ACTION_UPSCALE: |
8ca151b5 JB |
2170 | /* Increase starting rate, update uCode's rate table */ |
2171 | if (high != IWL_RATE_INVALID) { | |
2172 | update_lq = 1; | |
2173 | index = high; | |
4d30ee82 ES |
2174 | } else { |
2175 | IWL_DEBUG_RATE(mvm, | |
2176 | "At the top rate. Can't increase\n"); | |
8ca151b5 JB |
2177 | } |
2178 | ||
2179 | break; | |
4e4b815c | 2180 | case RS_ACTION_STAY: |
8ca151b5 | 2181 | /* No change */ |
e6c8d602 ES |
2182 | if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) |
2183 | update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl); | |
2fd647f8 | 2184 | break; |
8ca151b5 JB |
2185 | default: |
2186 | break; | |
2187 | } | |
2188 | ||
8ca151b5 JB |
2189 | lq_update: |
2190 | /* Replace uCode's rate table for the destination station. */ | |
5aa33553 ES |
2191 | if (update_lq) { |
2192 | tbl->rate.index = index; | |
2193 | rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate); | |
2194 | } | |
8ca151b5 JB |
2195 | |
2196 | rs_stay_in_table(lq_sta, false); | |
2197 | ||
2198 | /* | |
2199 | * Search for new modulation mode if we're: | |
2200 | * 1) Not changing rates right now | |
2201 | * 2) Not just finishing up a search | |
2202 | * 3) Allowing a new search | |
2203 | */ | |
2204 | if (!update_lq && !done_search && | |
b3b06a32 ES |
2205 | lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED |
2206 | && window->counter) { | |
2207 | enum rs_column next_column; | |
2208 | ||
8ca151b5 JB |
2209 | /* Save current throughput to compare with "search" throughput*/ |
2210 | lq_sta->last_tpt = current_tpt; | |
2211 | ||
4d30ee82 | 2212 | IWL_DEBUG_RATE(mvm, |
b3b06a32 ES |
2213 | "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n", |
2214 | update_lq, done_search, lq_sta->rs_state, | |
4d30ee82 | 2215 | window->counter); |
b3b06a32 ES |
2216 | |
2217 | next_column = rs_get_next_column(mvm, lq_sta, sta, tbl); | |
2218 | if (next_column != RS_COLUMN_INVALID) { | |
2219 | int ret = rs_switch_to_column(mvm, lq_sta, sta, | |
2220 | next_column); | |
2221 | if (!ret) | |
2222 | lq_sta->search_better_tbl = 1; | |
2223 | } else { | |
2224 | IWL_DEBUG_RATE(mvm, | |
2225 | "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n"); | |
2226 | lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED; | |
2227 | } | |
8ca151b5 JB |
2228 | |
2229 | /* If new "search" mode was selected, set up in uCode table */ | |
2230 | if (lq_sta->search_better_tbl) { | |
2231 | /* Access the "search" table, clear its history. */ | |
2232 | tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]); | |
b804eeb6 | 2233 | rs_rate_scale_clear_tbl_windows(mvm, tbl); |
8ca151b5 JB |
2234 | |
2235 | /* Use new "search" start rate */ | |
8fc7c58c | 2236 | index = tbl->rate.index; |
8ca151b5 | 2237 | |
b3b06a32 ES |
2238 | rs_dump_rate(mvm, &tbl->rate, |
2239 | "Switch to SEARCH TABLE:"); | |
8fc7c58c | 2240 | rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate); |
9e680946 | 2241 | iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false); |
8ca151b5 JB |
2242 | } else { |
2243 | done_search = 1; | |
2244 | } | |
2245 | } | |
2246 | ||
b3b06a32 | 2247 | if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) { |
8ca151b5 JB |
2248 | /* If the "active" (non-search) mode was legacy, |
2249 | * and we've tried switching antennas, | |
2250 | * but we haven't been able to try HT modes (not available), | |
2251 | * stay with best antenna legacy modulation for a while | |
2252 | * before next round of mode comparisons. */ | |
2253 | tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]); | |
fd7dbee5 | 2254 | if (is_legacy(&tbl1->rate)) { |
8ca151b5 | 2255 | IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n"); |
fd7dbee5 ES |
2256 | |
2257 | if (tid != IWL_MAX_TID_COUNT) { | |
2258 | tid_data = &sta_priv->tid_data[tid]; | |
2259 | if (tid_data->state != IWL_AGG_OFF) { | |
2260 | IWL_DEBUG_RATE(mvm, | |
2261 | "Stop aggregation on tid %d\n", | |
2262 | tid); | |
2263 | ieee80211_stop_tx_ba_session(sta, tid); | |
2264 | } | |
2265 | } | |
8ca151b5 | 2266 | rs_set_stay_in_table(mvm, 1, lq_sta); |
b3b06a32 | 2267 | } else { |
8ca151b5 JB |
2268 | /* If we're in an HT mode, and all 3 mode switch actions |
2269 | * have been tried and compared, stay in this best modulation | |
2270 | * mode for a while before next round of mode comparisons. */ | |
8ca151b5 JB |
2271 | if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) && |
2272 | (lq_sta->tx_agg_tid_en & (1 << tid)) && | |
2273 | (tid != IWL_MAX_TID_COUNT)) { | |
2274 | tid_data = &sta_priv->tid_data[tid]; | |
2275 | if (tid_data->state == IWL_AGG_OFF) { | |
2276 | IWL_DEBUG_RATE(mvm, | |
2277 | "try to aggregate tid %d\n", | |
2278 | tid); | |
2279 | rs_tl_turn_on_agg(mvm, tid, | |
2280 | lq_sta, sta); | |
2281 | } | |
2282 | } | |
2283 | rs_set_stay_in_table(mvm, 0, lq_sta); | |
2284 | } | |
2285 | } | |
2286 | ||
2287 | out: | |
8ca151b5 JB |
2288 | lq_sta->last_txrate_idx = index; |
2289 | } | |
2290 | ||
361dbec8 ES |
2291 | struct rs_init_rate_info { |
2292 | s8 rssi; | |
2293 | u8 rate_idx; | |
2294 | }; | |
2295 | ||
2296 | static const struct rs_init_rate_info rs_init_rates_24ghz[] = { | |
2297 | { -60, IWL_RATE_54M_INDEX }, | |
2298 | { -64, IWL_RATE_48M_INDEX }, | |
2299 | { -68, IWL_RATE_36M_INDEX }, | |
2300 | { -80, IWL_RATE_24M_INDEX }, | |
2301 | { -84, IWL_RATE_18M_INDEX }, | |
2302 | { -85, IWL_RATE_12M_INDEX }, | |
2303 | { -86, IWL_RATE_11M_INDEX }, | |
2304 | { -88, IWL_RATE_5M_INDEX }, | |
2305 | { -90, IWL_RATE_2M_INDEX }, | |
2306 | { S8_MIN, IWL_RATE_1M_INDEX }, | |
2307 | }; | |
2308 | ||
2309 | static const struct rs_init_rate_info rs_init_rates_5ghz[] = { | |
2310 | { -60, IWL_RATE_54M_INDEX }, | |
2311 | { -64, IWL_RATE_48M_INDEX }, | |
2312 | { -72, IWL_RATE_36M_INDEX }, | |
2313 | { -80, IWL_RATE_24M_INDEX }, | |
2314 | { -84, IWL_RATE_18M_INDEX }, | |
2315 | { -85, IWL_RATE_12M_INDEX }, | |
2316 | { -87, IWL_RATE_9M_INDEX }, | |
2317 | { S8_MIN, IWL_RATE_6M_INDEX }, | |
2318 | }; | |
2319 | ||
2320 | /* Choose an initial legacy rate and antenna to use based on the RSSI | |
2321 | * of last Rx | |
2322 | */ | |
2323 | static void rs_get_initial_rate(struct iwl_mvm *mvm, | |
2324 | struct iwl_lq_sta *lq_sta, | |
2325 | enum ieee80211_band band, | |
2326 | struct rs_rate *rate) | |
2327 | { | |
2328 | int i, nentries; | |
2329 | s8 best_rssi = S8_MIN; | |
2330 | u8 best_ant = ANT_NONE; | |
2331 | u8 valid_tx_ant = mvm->fw->valid_tx_ant; | |
2332 | const struct rs_init_rate_info *initial_rates; | |
2333 | ||
2334 | for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) { | |
2335 | if (!(lq_sta->pers.chains & BIT(i))) | |
2336 | continue; | |
2337 | ||
2338 | if (lq_sta->pers.chain_signal[i] > best_rssi) { | |
2339 | best_rssi = lq_sta->pers.chain_signal[i]; | |
2340 | best_ant = BIT(i); | |
2341 | } | |
2342 | } | |
2343 | ||
2344 | IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n", | |
2345 | rs_pretty_ant(best_ant), best_rssi); | |
2346 | ||
2347 | if (best_ant != ANT_A && best_ant != ANT_B) | |
2348 | rate->ant = first_antenna(valid_tx_ant); | |
2349 | else | |
2350 | rate->ant = best_ant; | |
2351 | ||
2352 | rate->sgi = false; | |
2353 | rate->ldpc = false; | |
2354 | rate->bw = RATE_MCS_CHAN_WIDTH_20; | |
2355 | ||
2356 | rate->index = find_first_bit(&lq_sta->active_legacy_rate, | |
2357 | BITS_PER_LONG); | |
2358 | ||
2359 | if (band == IEEE80211_BAND_5GHZ) { | |
2360 | rate->type = LQ_LEGACY_A; | |
2361 | initial_rates = rs_init_rates_5ghz; | |
2362 | nentries = ARRAY_SIZE(rs_init_rates_5ghz); | |
2363 | } else { | |
2364 | rate->type = LQ_LEGACY_G; | |
2365 | initial_rates = rs_init_rates_24ghz; | |
2366 | nentries = ARRAY_SIZE(rs_init_rates_24ghz); | |
2367 | } | |
2368 | ||
2369 | if (IWL_MVM_RS_RSSI_BASED_INIT_RATE) { | |
2370 | for (i = 0; i < nentries; i++) { | |
2371 | int rate_idx = initial_rates[i].rate_idx; | |
2372 | if ((best_rssi >= initial_rates[i].rssi) && | |
2373 | (BIT(rate_idx) & lq_sta->active_legacy_rate)) { | |
2374 | rate->index = rate_idx; | |
2375 | break; | |
2376 | } | |
2377 | } | |
2378 | } | |
2379 | ||
2380 | IWL_DEBUG_RATE(mvm, "rate_idx %d ANT %s\n", rate->index, | |
2381 | rs_pretty_ant(rate->ant)); | |
2382 | } | |
2383 | ||
2384 | /* Save info about RSSI of last Rx */ | |
2385 | void rs_update_last_rssi(struct iwl_mvm *mvm, | |
2386 | struct iwl_lq_sta *lq_sta, | |
2387 | struct ieee80211_rx_status *rx_status) | |
2388 | { | |
2389 | lq_sta->pers.chains = rx_status->chains; | |
2390 | lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0]; | |
2391 | lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1]; | |
2392 | lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2]; | |
2393 | } | |
2394 | ||
8ca151b5 JB |
2395 | /** |
2396 | * rs_initialize_lq - Initialize a station's hardware rate table | |
2397 | * | |
2398 | * The uCode's station table contains a table of fallback rates | |
2399 | * for automatic fallback during transmission. | |
2400 | * | |
2401 | * NOTE: This sets up a default set of values. These will be replaced later | |
2402 | * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of | |
2403 | * rc80211_simple. | |
2404 | * | |
2405 | * NOTE: Run REPLY_ADD_STA command to set up station table entry, before | |
2406 | * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD, | |
2407 | * which requires station table entry to exist). | |
2408 | */ | |
2409 | static void rs_initialize_lq(struct iwl_mvm *mvm, | |
2410 | struct ieee80211_sta *sta, | |
2411 | struct iwl_lq_sta *lq_sta, | |
b87c2179 ES |
2412 | enum ieee80211_band band, |
2413 | bool init) | |
8ca151b5 JB |
2414 | { |
2415 | struct iwl_scale_tbl_info *tbl; | |
5aa33553 | 2416 | struct rs_rate *rate; |
8ca151b5 | 2417 | u8 active_tbl = 0; |
8ca151b5 JB |
2418 | |
2419 | if (!sta || !lq_sta) | |
2420 | return; | |
2421 | ||
8ca151b5 JB |
2422 | if (!lq_sta->search_better_tbl) |
2423 | active_tbl = lq_sta->active_tbl; | |
2424 | else | |
2425 | active_tbl = 1 - lq_sta->active_tbl; | |
2426 | ||
2427 | tbl = &(lq_sta->lq_info[active_tbl]); | |
5aa33553 | 2428 | rate = &tbl->rate; |
8ca151b5 | 2429 | |
361dbec8 ES |
2430 | rs_get_initial_rate(mvm, lq_sta, band, rate); |
2431 | lq_sta->last_txrate_idx = rate->index; | |
8ca151b5 | 2432 | |
b3b06a32 ES |
2433 | WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B); |
2434 | if (rate->ant == ANT_A) | |
2435 | tbl->column = RS_COLUMN_LEGACY_ANT_A; | |
2436 | else | |
2437 | tbl->column = RS_COLUMN_LEGACY_ANT_B; | |
2438 | ||
8ca151b5 | 2439 | rs_set_expected_tpt_table(lq_sta, tbl); |
80e9e5cd | 2440 | rs_fill_lq_cmd(mvm, sta, lq_sta, rate); |
8ca151b5 | 2441 | /* TODO restore station should remember the lq cmd */ |
b87c2179 | 2442 | iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init); |
8ca151b5 JB |
2443 | } |
2444 | ||
2445 | static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta, | |
2446 | struct ieee80211_tx_rate_control *txrc) | |
2447 | { | |
2448 | struct sk_buff *skb = txrc->skb; | |
8ca151b5 JB |
2449 | struct iwl_op_mode *op_mode __maybe_unused = |
2450 | (struct iwl_op_mode *)mvm_r; | |
2451 | struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode); | |
2452 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
2453 | struct iwl_lq_sta *lq_sta = mvm_sta; | |
8ca151b5 | 2454 | |
2536e7c3 | 2455 | /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */ |
8ca151b5 JB |
2456 | |
2457 | /* Treat uninitialized rate scaling data same as non-existing. */ | |
ae969afe | 2458 | if (lq_sta && !lq_sta->pers.drv) { |
8ca151b5 JB |
2459 | IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n"); |
2460 | mvm_sta = NULL; | |
2461 | } | |
2462 | ||
2463 | /* Send management frames and NO_ACK data using lowest rate. */ | |
2464 | if (rate_control_send_low(sta, mvm_sta, txrc)) | |
2465 | return; | |
2466 | ||
d310e405 ES |
2467 | iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags, |
2468 | info->band, &info->control.rates[0]); | |
2469 | ||
622ebe99 | 2470 | info->control.rates[0].count = 1; |
8ca151b5 JB |
2471 | } |
2472 | ||
2473 | static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta, | |
2474 | gfp_t gfp) | |
2475 | { | |
2476 | struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv; | |
ae969afe EP |
2477 | struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate; |
2478 | struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode); | |
2479 | struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta; | |
8ca151b5 JB |
2480 | |
2481 | IWL_DEBUG_RATE(mvm, "create station rate scale window\n"); | |
2482 | ||
ae969afe EP |
2483 | lq_sta->pers.drv = mvm; |
2484 | #ifdef CONFIG_MAC80211_DEBUGFS | |
2485 | lq_sta->pers.dbg_fixed_rate = 0; | |
2486 | lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID; | |
2487 | #endif | |
361dbec8 ES |
2488 | lq_sta->pers.chains = 0; |
2489 | memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal)); | |
ae969afe | 2490 | |
8ca151b5 JB |
2491 | return &sta_priv->lq_sta; |
2492 | } | |
2493 | ||
d310e405 ES |
2494 | static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap, |
2495 | int nss) | |
2496 | { | |
2497 | u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) & | |
2498 | (0x3 << (2 * (nss - 1))); | |
2499 | rx_mcs >>= (2 * (nss - 1)); | |
2500 | ||
2501 | if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7) | |
2502 | return IWL_RATE_MCS_7_INDEX; | |
2503 | else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8) | |
2504 | return IWL_RATE_MCS_8_INDEX; | |
2505 | else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9) | |
2506 | return IWL_RATE_MCS_9_INDEX; | |
2507 | ||
2508 | WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED); | |
2509 | return -1; | |
2510 | } | |
2511 | ||
750a1b45 ES |
2512 | static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta, |
2513 | struct ieee80211_sta_vht_cap *vht_cap, | |
2514 | struct iwl_lq_sta *lq_sta) | |
2515 | { | |
2516 | int i; | |
2517 | int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1); | |
2518 | ||
2519 | if (highest_mcs >= IWL_RATE_MCS_0_INDEX) { | |
2520 | for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) { | |
2521 | if (i == IWL_RATE_9M_INDEX) | |
2522 | continue; | |
2523 | ||
271518ad ES |
2524 | /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */ |
2525 | if (i == IWL_RATE_MCS_9_INDEX && | |
2526 | sta->bandwidth == IEEE80211_STA_RX_BW_20) | |
2527 | continue; | |
2528 | ||
750a1b45 ES |
2529 | lq_sta->active_siso_rate |= BIT(i); |
2530 | } | |
2531 | } | |
2532 | ||
ecc90e79 ES |
2533 | if (sta->rx_nss < 2) |
2534 | return; | |
2535 | ||
750a1b45 ES |
2536 | highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2); |
2537 | if (highest_mcs >= IWL_RATE_MCS_0_INDEX) { | |
2538 | for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) { | |
2539 | if (i == IWL_RATE_9M_INDEX) | |
2540 | continue; | |
2541 | ||
271518ad ES |
2542 | /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */ |
2543 | if (i == IWL_RATE_MCS_9_INDEX && | |
2544 | sta->bandwidth == IEEE80211_STA_RX_BW_20) | |
2545 | continue; | |
2546 | ||
750a1b45 ES |
2547 | lq_sta->active_mimo2_rate |= BIT(i); |
2548 | } | |
2549 | } | |
2550 | } | |
2551 | ||
5fc0f76c ES |
2552 | #ifdef CONFIG_IWLWIFI_DEBUGFS |
2553 | static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm, | |
2554 | struct iwl_mvm_frame_stats *stats) | |
2555 | { | |
2556 | spin_lock_bh(&mvm->drv_stats_lock); | |
2557 | memset(stats, 0, sizeof(*stats)); | |
2558 | spin_unlock_bh(&mvm->drv_stats_lock); | |
2559 | } | |
2560 | ||
2561 | void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm, | |
2562 | struct iwl_mvm_frame_stats *stats, | |
2563 | u32 rate, bool agg) | |
2564 | { | |
2565 | u8 nss = 0, mcs = 0; | |
2566 | ||
2567 | spin_lock(&mvm->drv_stats_lock); | |
2568 | ||
2569 | if (agg) | |
2570 | stats->agg_frames++; | |
2571 | ||
2572 | stats->success_frames++; | |
2573 | ||
2574 | switch (rate & RATE_MCS_CHAN_WIDTH_MSK) { | |
2575 | case RATE_MCS_CHAN_WIDTH_20: | |
2576 | stats->bw_20_frames++; | |
2577 | break; | |
2578 | case RATE_MCS_CHAN_WIDTH_40: | |
2579 | stats->bw_40_frames++; | |
2580 | break; | |
2581 | case RATE_MCS_CHAN_WIDTH_80: | |
2582 | stats->bw_80_frames++; | |
2583 | break; | |
2584 | default: | |
2585 | WARN_ONCE(1, "bad BW. rate 0x%x", rate); | |
2586 | } | |
2587 | ||
2588 | if (rate & RATE_MCS_HT_MSK) { | |
2589 | stats->ht_frames++; | |
2590 | mcs = rate & RATE_HT_MCS_RATE_CODE_MSK; | |
2591 | nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1; | |
2592 | } else if (rate & RATE_MCS_VHT_MSK) { | |
2593 | stats->vht_frames++; | |
2594 | mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK; | |
2595 | nss = ((rate & RATE_VHT_MCS_NSS_MSK) >> | |
2596 | RATE_VHT_MCS_NSS_POS) + 1; | |
2597 | } else { | |
2598 | stats->legacy_frames++; | |
2599 | } | |
2600 | ||
2601 | if (nss == 1) | |
2602 | stats->siso_frames++; | |
2603 | else if (nss == 2) | |
2604 | stats->mimo2_frames++; | |
2605 | ||
2606 | if (rate & RATE_MCS_SGI_MSK) | |
2607 | stats->sgi_frames++; | |
2608 | else | |
2609 | stats->ngi_frames++; | |
2610 | ||
2611 | stats->last_rates[stats->last_frame_idx] = rate; | |
2612 | stats->last_frame_idx = (stats->last_frame_idx + 1) % | |
2613 | ARRAY_SIZE(stats->last_rates); | |
2614 | ||
2615 | spin_unlock(&mvm->drv_stats_lock); | |
2616 | } | |
2617 | #endif | |
2618 | ||
8ca151b5 JB |
2619 | /* |
2620 | * Called after adding a new station to initialize rate scaling | |
2621 | */ | |
2622 | void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta, | |
b87c2179 | 2623 | enum ieee80211_band band, bool init) |
8ca151b5 JB |
2624 | { |
2625 | int i, j; | |
2626 | struct ieee80211_hw *hw = mvm->hw; | |
2627 | struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap; | |
d310e405 | 2628 | struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap; |
8ca151b5 JB |
2629 | struct iwl_mvm_sta *sta_priv; |
2630 | struct iwl_lq_sta *lq_sta; | |
2631 | struct ieee80211_supported_band *sband; | |
2632 | unsigned long supp; /* must be unsigned long for for_each_set_bit */ | |
2633 | ||
2634 | sta_priv = (struct iwl_mvm_sta *)sta->drv_priv; | |
2635 | lq_sta = &sta_priv->lq_sta; | |
ae969afe EP |
2636 | |
2637 | /* clear all non-persistent lq data */ | |
2638 | memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers)); | |
b87c2179 | 2639 | |
8ca151b5 JB |
2640 | sband = hw->wiphy->bands[band]; |
2641 | ||
2642 | lq_sta->lq.sta_id = sta_priv->sta_id; | |
2643 | ||
2644 | for (j = 0; j < LQ_SIZE; j++) | |
b804eeb6 | 2645 | rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]); |
8ca151b5 JB |
2646 | |
2647 | lq_sta->flush_timer = 0; | |
87d5e415 | 2648 | lq_sta->last_tx = jiffies; |
8ca151b5 JB |
2649 | |
2650 | IWL_DEBUG_RATE(mvm, | |
2651 | "LQ: *** rate scale station global init for station %d ***\n", | |
2652 | sta_priv->sta_id); | |
2653 | /* TODO: what is a good starting rate for STA? About middle? Maybe not | |
2654 | * the lowest or the highest rate.. Could consider using RSSI from | |
2655 | * previous packets? Need to have IEEE 802.1X auth succeed immediately | |
2656 | * after assoc.. */ | |
2657 | ||
8ca151b5 | 2658 | lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX; |
8ca151b5 JB |
2659 | lq_sta->band = sband->band; |
2660 | /* | |
2661 | * active legacy rates as per supported rates bitmap | |
2662 | */ | |
2663 | supp = sta->supp_rates[sband->band]; | |
2664 | lq_sta->active_legacy_rate = 0; | |
2665 | for_each_set_bit(i, &supp, BITS_PER_LONG) | |
2666 | lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value); | |
2667 | ||
d310e405 ES |
2668 | /* TODO: should probably account for rx_highest for both HT/VHT */ |
2669 | if (!vht_cap || !vht_cap->vht_supported) { | |
2670 | /* active_siso_rate mask includes 9 MBits (bit 5), | |
2671 | * and CCK (bits 0-3), supp_rates[] does not; | |
2672 | * shift to convert format, force 9 MBits off. | |
2673 | */ | |
2674 | lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1; | |
2675 | lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1; | |
2676 | lq_sta->active_siso_rate &= ~((u16)0x2); | |
2677 | lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE; | |
2678 | ||
2679 | /* Same here */ | |
2680 | lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1; | |
2681 | lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1; | |
2682 | lq_sta->active_mimo2_rate &= ~((u16)0x2); | |
2683 | lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE; | |
2684 | ||
2685 | lq_sta->is_vht = false; | |
a3576ff2 ES |
2686 | if (mvm->cfg->ht_params->ldpc && |
2687 | (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)) | |
2688 | lq_sta->ldpc = true; | |
d310e405 | 2689 | } else { |
750a1b45 | 2690 | rs_vht_set_enabled_rates(sta, vht_cap, lq_sta); |
d310e405 | 2691 | lq_sta->is_vht = true; |
a3576ff2 ES |
2692 | |
2693 | if (mvm->cfg->ht_params->ldpc && | |
2694 | (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC)) | |
2695 | lq_sta->ldpc = true; | |
d310e405 | 2696 | } |
8ca151b5 | 2697 | |
198266a3 ES |
2698 | lq_sta->max_legacy_rate_idx = find_last_bit(&lq_sta->active_legacy_rate, |
2699 | BITS_PER_LONG); | |
2700 | lq_sta->max_siso_rate_idx = find_last_bit(&lq_sta->active_siso_rate, | |
2701 | BITS_PER_LONG); | |
2702 | lq_sta->max_mimo2_rate_idx = find_last_bit(&lq_sta->active_mimo2_rate, | |
2703 | BITS_PER_LONG); | |
2704 | ||
a3576ff2 ES |
2705 | IWL_DEBUG_RATE(mvm, |
2706 | "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d\n", | |
198266a3 | 2707 | lq_sta->active_legacy_rate, |
8ca151b5 | 2708 | lq_sta->active_siso_rate, |
d310e405 | 2709 | lq_sta->active_mimo2_rate, |
a3576ff2 | 2710 | lq_sta->is_vht, lq_sta->ldpc); |
198266a3 ES |
2711 | IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n", |
2712 | lq_sta->max_legacy_rate_idx, | |
2713 | lq_sta->max_siso_rate_idx, | |
2714 | lq_sta->max_mimo2_rate_idx); | |
8ca151b5 JB |
2715 | |
2716 | /* These values will be overridden later */ | |
2717 | lq_sta->lq.single_stream_ant_msk = | |
4ed735e7 | 2718 | first_antenna(mvm->fw->valid_tx_ant); |
809bccfa | 2719 | lq_sta->lq.dual_stream_ant_msk = ANT_AB; |
8ca151b5 JB |
2720 | |
2721 | /* as default allow aggregation for all tids */ | |
2722 | lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID; | |
8ca151b5 | 2723 | lq_sta->is_agg = 0; |
5fc0f76c ES |
2724 | #ifdef CONFIG_IWLWIFI_DEBUGFS |
2725 | iwl_mvm_reset_frame_stats(mvm, &mvm->drv_rx_stats); | |
2726 | #endif | |
b87c2179 ES |
2727 | rs_initialize_lq(mvm, sta, lq_sta, band, init); |
2728 | } | |
2729 | ||
2730 | static void rs_rate_update(void *mvm_r, | |
2731 | struct ieee80211_supported_band *sband, | |
2732 | struct cfg80211_chan_def *chandef, | |
2733 | struct ieee80211_sta *sta, void *priv_sta, | |
2734 | u32 changed) | |
2735 | { | |
2736 | u8 tid; | |
2737 | struct iwl_op_mode *op_mode = | |
2738 | (struct iwl_op_mode *)mvm_r; | |
2739 | struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode); | |
2740 | ||
2741 | /* Stop any ongoing aggregations as rs starts off assuming no agg */ | |
2742 | for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) | |
2743 | ieee80211_stop_tx_ba_session(sta, tid); | |
2744 | ||
2745 | iwl_mvm_rs_rate_init(mvm, sta, sband->band, false); | |
8ca151b5 JB |
2746 | } |
2747 | ||
8fc7c58c ES |
2748 | #ifdef CONFIG_MAC80211_DEBUGFS |
2749 | static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm, | |
2750 | struct iwl_lq_cmd *lq_cmd, | |
2751 | enum ieee80211_band band, | |
2752 | u32 ucode_rate) | |
2753 | { | |
2754 | struct rs_rate rate; | |
2755 | int i; | |
2756 | int num_rates = ARRAY_SIZE(lq_cmd->rs_table); | |
2757 | __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate); | |
7ceb98a3 | 2758 | u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS; |
8fc7c58c ES |
2759 | |
2760 | for (i = 0; i < num_rates; i++) | |
2761 | lq_cmd->rs_table[i] = ucode_rate_le32; | |
2762 | ||
2763 | rs_rate_from_ucode_rate(ucode_rate, band, &rate); | |
2764 | ||
2765 | if (is_mimo(&rate)) | |
2766 | lq_cmd->mimo_delim = num_rates - 1; | |
2767 | else | |
2768 | lq_cmd->mimo_delim = 0; | |
7ceb98a3 EG |
2769 | |
2770 | lq_cmd->reduced_tpc = 0; | |
2771 | ||
2772 | if (num_of_ant(ant) == 1) | |
2773 | lq_cmd->single_stream_ant_msk = ant; | |
2774 | ||
2775 | lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF; | |
8fc7c58c ES |
2776 | } |
2777 | #endif /* CONFIG_MAC80211_DEBUGFS */ | |
2778 | ||
e07be6d3 ES |
2779 | static void rs_fill_rates_for_column(struct iwl_mvm *mvm, |
2780 | struct iwl_lq_sta *lq_sta, | |
2781 | struct rs_rate *rate, | |
2782 | __le32 *rs_table, int *rs_table_index, | |
2783 | int num_rates, int num_retries, | |
2784 | u8 valid_tx_ant, bool toggle_ant) | |
2785 | { | |
2786 | int i, j; | |
2787 | __le32 ucode_rate; | |
2788 | bool bottom_reached = false; | |
2789 | int prev_rate_idx = rate->index; | |
2790 | int end = LINK_QUAL_MAX_RETRY_NUM; | |
2791 | int index = *rs_table_index; | |
2792 | ||
2793 | for (i = 0; i < num_rates && index < end; i++) { | |
2794 | ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, rate)); | |
2795 | for (j = 0; j < num_retries && index < end; j++, index++) | |
2796 | rs_table[index] = ucode_rate; | |
2797 | ||
2798 | if (toggle_ant) | |
2799 | rs_toggle_antenna(valid_tx_ant, rate); | |
2800 | ||
2801 | prev_rate_idx = rate->index; | |
2802 | bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate); | |
2803 | if (bottom_reached && !is_legacy(rate)) | |
2804 | break; | |
2805 | } | |
2806 | ||
2807 | if (!bottom_reached) | |
2808 | rate->index = prev_rate_idx; | |
2809 | ||
2810 | *rs_table_index = index; | |
2811 | } | |
2812 | ||
2813 | /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI | |
2814 | * column the rate table should look like this: | |
2815 | * | |
2816 | * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI | |
2817 | * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI | |
2818 | * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI | |
2819 | * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI | |
2820 | * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI | |
2821 | * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI | |
2822 | * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI | |
2823 | * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI | |
2824 | * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI | |
2825 | * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps | |
2826 | * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps | |
2827 | * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps | |
2828 | * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps | |
2829 | * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps | |
2830 | * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps | |
2831 | * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps | |
2832 | */ | |
8fc7c58c ES |
2833 | static void rs_build_rates_table(struct iwl_mvm *mvm, |
2834 | struct iwl_lq_sta *lq_sta, | |
2835 | const struct rs_rate *initial_rate) | |
8ca151b5 | 2836 | { |
5aa33553 | 2837 | struct rs_rate rate; |
e07be6d3 | 2838 | int num_rates, num_retries, index = 0; |
8ca151b5 JB |
2839 | u8 valid_tx_ant = 0; |
2840 | struct iwl_lq_cmd *lq_cmd = &lq_sta->lq; | |
e07be6d3 | 2841 | bool toggle_ant = false; |
8ca151b5 | 2842 | |
9e898f1b | 2843 | memcpy(&rate, initial_rate, sizeof(rate)); |
8fc7c58c | 2844 | |
4ed735e7 | 2845 | valid_tx_ant = mvm->fw->valid_tx_ant; |
8ca151b5 | 2846 | |
e07be6d3 ES |
2847 | if (is_siso(&rate)) { |
2848 | num_rates = RS_INITIAL_SISO_NUM_RATES; | |
2849 | num_retries = RS_HT_VHT_RETRIES_PER_RATE; | |
2850 | } else if (is_mimo(&rate)) { | |
2851 | num_rates = RS_INITIAL_MIMO_NUM_RATES; | |
2852 | num_retries = RS_HT_VHT_RETRIES_PER_RATE; | |
8ca151b5 | 2853 | } else { |
e07be6d3 ES |
2854 | num_rates = RS_INITIAL_LEGACY_NUM_RATES; |
2855 | num_retries = RS_LEGACY_RETRIES_PER_RATE; | |
2856 | toggle_ant = true; | |
8ca151b5 JB |
2857 | } |
2858 | ||
e07be6d3 ES |
2859 | rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index, |
2860 | num_rates, num_retries, valid_tx_ant, | |
2861 | toggle_ant); | |
8ca151b5 | 2862 | |
e07be6d3 | 2863 | rs_get_lower_rate_down_column(lq_sta, &rate); |
8ca151b5 | 2864 | |
e07be6d3 ES |
2865 | if (is_siso(&rate)) { |
2866 | num_rates = RS_SECONDARY_SISO_NUM_RATES; | |
2867 | num_retries = RS_SECONDARY_SISO_RETRIES; | |
d9088f60 | 2868 | lq_cmd->mimo_delim = index; |
e07be6d3 ES |
2869 | } else if (is_legacy(&rate)) { |
2870 | num_rates = RS_SECONDARY_LEGACY_NUM_RATES; | |
2871 | num_retries = RS_LEGACY_RETRIES_PER_RATE; | |
2872 | } else { | |
2873 | WARN_ON_ONCE(1); | |
2874 | } | |
8ca151b5 | 2875 | |
e07be6d3 | 2876 | toggle_ant = true; |
8ca151b5 | 2877 | |
e07be6d3 ES |
2878 | rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index, |
2879 | num_rates, num_retries, valid_tx_ant, | |
2880 | toggle_ant); | |
8ca151b5 | 2881 | |
e07be6d3 | 2882 | rs_get_lower_rate_down_column(lq_sta, &rate); |
8ca151b5 | 2883 | |
e07be6d3 ES |
2884 | num_rates = RS_SECONDARY_LEGACY_NUM_RATES; |
2885 | num_retries = RS_LEGACY_RETRIES_PER_RATE; | |
8ca151b5 | 2886 | |
e07be6d3 ES |
2887 | rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index, |
2888 | num_rates, num_retries, valid_tx_ant, | |
2889 | toggle_ant); | |
8ca151b5 | 2890 | |
8fc7c58c ES |
2891 | } |
2892 | ||
2893 | static void rs_fill_lq_cmd(struct iwl_mvm *mvm, | |
2894 | struct ieee80211_sta *sta, | |
2895 | struct iwl_lq_sta *lq_sta, | |
2896 | const struct rs_rate *initial_rate) | |
2897 | { | |
2898 | struct iwl_lq_cmd *lq_cmd = &lq_sta->lq; | |
7ceb98a3 EG |
2899 | struct iwl_mvm_sta *mvmsta; |
2900 | struct iwl_mvm_vif *mvmvif; | |
2901 | ||
2902 | lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF; | |
2903 | lq_cmd->agg_time_limit = | |
2904 | cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF); | |
8fc7c58c ES |
2905 | |
2906 | #ifdef CONFIG_MAC80211_DEBUGFS | |
ae969afe | 2907 | if (lq_sta->pers.dbg_fixed_rate) { |
8fc7c58c ES |
2908 | rs_build_rates_table_from_fixed(mvm, lq_cmd, |
2909 | lq_sta->band, | |
ae969afe | 2910 | lq_sta->pers.dbg_fixed_rate); |
7ceb98a3 EG |
2911 | return; |
2912 | } | |
8fc7c58c | 2913 | #endif |
7ceb98a3 EG |
2914 | if (WARN_ON_ONCE(!sta || !initial_rate)) |
2915 | return; | |
8fc7c58c | 2916 | |
7ceb98a3 | 2917 | rs_build_rates_table(mvm, lq_sta, initial_rate); |
8ca151b5 | 2918 | |
7ceb98a3 EG |
2919 | if (num_of_ant(initial_rate->ant) == 1) |
2920 | lq_cmd->single_stream_ant_msk = initial_rate->ant; | |
8ca151b5 | 2921 | |
7ceb98a3 EG |
2922 | mvmsta = iwl_mvm_sta_from_mac80211(sta); |
2923 | mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif); | |
e96d551f | 2924 | |
7ceb98a3 EG |
2925 | if (num_of_ant(initial_rate->ant) == 1) |
2926 | lq_cmd->single_stream_ant_msk = initial_rate->ant; | |
2927 | ||
2928 | lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize; | |
2929 | ||
2930 | /* | |
2931 | * In case of low latency, tell the firwmare to leave a frame in the | |
2932 | * Tx Fifo so that it can start a transaction in the same TxOP. This | |
2933 | * basically allows the firmware to send bursts. | |
2934 | */ | |
9ecd051e | 2935 | if (iwl_mvm_vif_low_latency(mvmvif)) { |
7ceb98a3 EG |
2936 | lq_cmd->agg_frame_cnt_limit--; |
2937 | ||
9ecd051e ES |
2938 | if (mvm->low_latency_agg_frame_limit) |
2939 | lq_cmd->agg_frame_cnt_limit = | |
2940 | min(lq_cmd->agg_frame_cnt_limit, | |
2941 | mvm->low_latency_agg_frame_limit); | |
2942 | } | |
2943 | ||
a4db848f EG |
2944 | if (mvmsta->vif->p2p) |
2945 | lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK; | |
2946 | ||
7ceb98a3 | 2947 | lq_cmd->agg_time_limit = |
5b7ff615 | 2948 | cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta)); |
8ca151b5 JB |
2949 | } |
2950 | ||
2951 | static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir) | |
2952 | { | |
2953 | return hw->priv; | |
2954 | } | |
2955 | /* rate scale requires free function to be implemented */ | |
2956 | static void rs_free(void *mvm_rate) | |
2957 | { | |
2958 | return; | |
2959 | } | |
2960 | ||
2961 | static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta, | |
2962 | void *mvm_sta) | |
2963 | { | |
2964 | struct iwl_op_mode *op_mode __maybe_unused = mvm_r; | |
2965 | struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode); | |
2966 | ||
2967 | IWL_DEBUG_RATE(mvm, "enter\n"); | |
2968 | IWL_DEBUG_RATE(mvm, "leave\n"); | |
2969 | } | |
2970 | ||
2971 | #ifdef CONFIG_MAC80211_DEBUGFS | |
5fc0f76c | 2972 | int rs_pretty_print_rate(char *buf, const u32 rate) |
5aa33553 ES |
2973 | { |
2974 | ||
1a61b347 ES |
2975 | char *type, *bw; |
2976 | u8 mcs = 0, nss = 0; | |
2977 | u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS; | |
2978 | ||
2979 | if (!(rate & RATE_MCS_HT_MSK) && | |
2980 | !(rate & RATE_MCS_VHT_MSK)) { | |
2981 | int index = iwl_hwrate_to_plcp_idx(rate); | |
2982 | ||
2983 | return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n", | |
560843f4 ES |
2984 | rs_pretty_ant(ant), |
2985 | index == IWL_RATE_INVALID ? "BAD" : | |
2986 | iwl_rate_mcs[index].mbps); | |
1a61b347 ES |
2987 | } |
2988 | ||
2989 | if (rate & RATE_MCS_VHT_MSK) { | |
2990 | type = "VHT"; | |
2991 | mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK; | |
2992 | nss = ((rate & RATE_VHT_MCS_NSS_MSK) | |
2993 | >> RATE_VHT_MCS_NSS_POS) + 1; | |
2994 | } else if (rate & RATE_MCS_HT_MSK) { | |
2995 | type = "HT"; | |
2996 | mcs = rate & RATE_HT_MCS_INDEX_MSK; | |
2997 | } else { | |
2998 | type = "Unknown"; /* shouldn't happen */ | |
2999 | } | |
3000 | ||
3001 | switch (rate & RATE_MCS_CHAN_WIDTH_MSK) { | |
3002 | case RATE_MCS_CHAN_WIDTH_20: | |
3003 | bw = "20Mhz"; | |
3004 | break; | |
3005 | case RATE_MCS_CHAN_WIDTH_40: | |
3006 | bw = "40Mhz"; | |
3007 | break; | |
3008 | case RATE_MCS_CHAN_WIDTH_80: | |
3009 | bw = "80Mhz"; | |
3010 | break; | |
3011 | case RATE_MCS_CHAN_WIDTH_160: | |
3012 | bw = "160Mhz"; | |
3013 | break; | |
3014 | default: | |
3015 | bw = "BAD BW"; | |
3016 | } | |
3017 | ||
3018 | return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n", | |
5aa33553 | 3019 | type, rs_pretty_ant(ant), bw, mcs, nss, |
1a61b347 | 3020 | (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ", |
7b1dd048 | 3021 | (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "", |
1a61b347 ES |
3022 | (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "", |
3023 | (rate & RATE_MCS_BF_MSK) ? "BF " : "", | |
3024 | (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : ""); | |
3025 | } | |
3026 | ||
a0c38b21 ES |
3027 | /** |
3028 | * Program the device to use fixed rate for frame transmit | |
3029 | * This is for debugging/testing only | |
3030 | * once the device start use fixed rate, we need to reload the module | |
3031 | * to being back the normal operation. | |
3032 | */ | |
3033 | static void rs_program_fix_rate(struct iwl_mvm *mvm, | |
3034 | struct iwl_lq_sta *lq_sta) | |
3035 | { | |
3036 | lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */ | |
3037 | lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */ | |
3038 | lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */ | |
3039 | ||
3040 | IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n", | |
ae969afe | 3041 | lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate); |
a0c38b21 | 3042 | |
ae969afe | 3043 | if (lq_sta->pers.dbg_fixed_rate) { |
7ceb98a3 | 3044 | rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL); |
ae969afe | 3045 | iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false); |
a0c38b21 ES |
3046 | } |
3047 | } | |
3048 | ||
9d10849e EG |
3049 | static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file, |
3050 | const char __user *user_buf, size_t count, loff_t *ppos) | |
3051 | { | |
3052 | struct iwl_lq_sta *lq_sta = file->private_data; | |
3053 | struct iwl_mvm *mvm; | |
3054 | char buf[64]; | |
3055 | size_t buf_size; | |
3056 | u32 parsed_rate; | |
3057 | ||
ae969afe | 3058 | mvm = lq_sta->pers.drv; |
9d10849e EG |
3059 | memset(buf, 0, sizeof(buf)); |
3060 | buf_size = min(count, sizeof(buf) - 1); | |
3061 | if (copy_from_user(buf, user_buf, buf_size)) | |
3062 | return -EFAULT; | |
3063 | ||
3064 | if (sscanf(buf, "%x", &parsed_rate) == 1) | |
ae969afe | 3065 | lq_sta->pers.dbg_fixed_rate = parsed_rate; |
9d10849e | 3066 | else |
ae969afe | 3067 | lq_sta->pers.dbg_fixed_rate = 0; |
9d10849e EG |
3068 | |
3069 | rs_program_fix_rate(mvm, lq_sta); | |
3070 | ||
3071 | return count; | |
3072 | } | |
3073 | ||
8ca151b5 JB |
3074 | static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file, |
3075 | char __user *user_buf, size_t count, loff_t *ppos) | |
3076 | { | |
3077 | char *buff; | |
3078 | int desc = 0; | |
3079 | int i = 0; | |
8ca151b5 JB |
3080 | ssize_t ret; |
3081 | ||
3082 | struct iwl_lq_sta *lq_sta = file->private_data; | |
3083 | struct iwl_mvm *mvm; | |
3084 | struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]); | |
5aa33553 | 3085 | struct rs_rate *rate = &tbl->rate; |
ae969afe | 3086 | mvm = lq_sta->pers.drv; |
1a61b347 | 3087 | buff = kmalloc(2048, GFP_KERNEL); |
8ca151b5 JB |
3088 | if (!buff) |
3089 | return -ENOMEM; | |
3090 | ||
3091 | desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id); | |
198266a3 | 3092 | desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n", |
8ca151b5 JB |
3093 | lq_sta->total_failed, lq_sta->total_success, |
3094 | lq_sta->active_legacy_rate); | |
3095 | desc += sprintf(buff+desc, "fixed rate 0x%X\n", | |
ae969afe | 3096 | lq_sta->pers.dbg_fixed_rate); |
8ca151b5 | 3097 | desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n", |
4ed735e7 JB |
3098 | (mvm->fw->valid_tx_ant & ANT_A) ? "ANT_A," : "", |
3099 | (mvm->fw->valid_tx_ant & ANT_B) ? "ANT_B," : "", | |
3100 | (mvm->fw->valid_tx_ant & ANT_C) ? "ANT_C" : ""); | |
8ca151b5 | 3101 | desc += sprintf(buff+desc, "lq type %s\n", |
5aa33553 ES |
3102 | (is_legacy(rate)) ? "legacy" : |
3103 | is_vht(rate) ? "VHT" : "HT"); | |
3104 | if (!is_legacy(rate)) { | |
8ca151b5 | 3105 | desc += sprintf(buff+desc, " %s", |
5aa33553 | 3106 | (is_siso(rate)) ? "SISO" : "MIMO2"); |
8ca151b5 | 3107 | desc += sprintf(buff+desc, " %s", |
5aa33553 ES |
3108 | (is_ht20(rate)) ? "20MHz" : |
3109 | (is_ht40(rate)) ? "40MHz" : | |
3110 | (is_ht80(rate)) ? "80Mhz" : "BAD BW"); | |
a3576ff2 | 3111 | desc += sprintf(buff+desc, " %s %s %s\n", |
5aa33553 | 3112 | (rate->sgi) ? "SGI" : "NGI", |
a3576ff2 | 3113 | (rate->ldpc) ? "LDPC" : "BCC", |
6a524f48 | 3114 | (lq_sta->is_agg) ? "AGG on" : ""); |
8ca151b5 JB |
3115 | } |
3116 | desc += sprintf(buff+desc, "last tx rate=0x%X\n", | |
3117 | lq_sta->last_rate_n_flags); | |
3118 | desc += sprintf(buff+desc, | |
1a61b347 | 3119 | "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n", |
8ca151b5 JB |
3120 | lq_sta->lq.flags, |
3121 | lq_sta->lq.mimo_delim, | |
3122 | lq_sta->lq.single_stream_ant_msk, | |
3123 | lq_sta->lq.dual_stream_ant_msk); | |
3124 | ||
3125 | desc += sprintf(buff+desc, | |
3126 | "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n", | |
3127 | le16_to_cpu(lq_sta->lq.agg_time_limit), | |
3128 | lq_sta->lq.agg_disable_start_th, | |
3129 | lq_sta->lq.agg_frame_cnt_limit); | |
3130 | ||
2fd647f8 | 3131 | desc += sprintf(buff+desc, "reduced tpc=%d\n", lq_sta->lq.reduced_tpc); |
8ca151b5 JB |
3132 | desc += sprintf(buff+desc, |
3133 | "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n", | |
3134 | lq_sta->lq.initial_rate_index[0], | |
3135 | lq_sta->lq.initial_rate_index[1], | |
3136 | lq_sta->lq.initial_rate_index[2], | |
3137 | lq_sta->lq.initial_rate_index[3]); | |
3138 | ||
3139 | for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) { | |
cf4ef654 | 3140 | u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]); |
1a61b347 | 3141 | |
cf4ef654 JB |
3142 | desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r); |
3143 | desc += rs_pretty_print_rate(buff+desc, r); | |
8ca151b5 JB |
3144 | } |
3145 | ||
3146 | ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc); | |
3147 | kfree(buff); | |
3148 | return ret; | |
3149 | } | |
3150 | ||
3151 | static const struct file_operations rs_sta_dbgfs_scale_table_ops = { | |
3152 | .write = rs_sta_dbgfs_scale_table_write, | |
3153 | .read = rs_sta_dbgfs_scale_table_read, | |
3154 | .open = simple_open, | |
3155 | .llseek = default_llseek, | |
3156 | }; | |
3157 | static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file, | |
3158 | char __user *user_buf, size_t count, loff_t *ppos) | |
3159 | { | |
3160 | char *buff; | |
3161 | int desc = 0; | |
3162 | int i, j; | |
3163 | ssize_t ret; | |
d310e405 | 3164 | struct iwl_scale_tbl_info *tbl; |
5aa33553 | 3165 | struct rs_rate *rate; |
8ca151b5 JB |
3166 | struct iwl_lq_sta *lq_sta = file->private_data; |
3167 | ||
3168 | buff = kmalloc(1024, GFP_KERNEL); | |
3169 | if (!buff) | |
3170 | return -ENOMEM; | |
3171 | ||
3172 | for (i = 0; i < LQ_SIZE; i++) { | |
d310e405 | 3173 | tbl = &(lq_sta->lq_info[i]); |
5aa33553 | 3174 | rate = &tbl->rate; |
8ca151b5 | 3175 | desc += sprintf(buff+desc, |
6a524f48 | 3176 | "%s type=%d SGI=%d BW=%s DUP=0\n" |
8fc7c58c | 3177 | "index=%d\n", |
8ca151b5 | 3178 | lq_sta->active_tbl == i ? "*" : "x", |
5aa33553 ES |
3179 | rate->type, |
3180 | rate->sgi, | |
3181 | is_ht20(rate) ? "20Mhz" : | |
3182 | is_ht40(rate) ? "40Mhz" : | |
3183 | is_ht80(rate) ? "80Mhz" : "ERR", | |
8fc7c58c | 3184 | rate->index); |
8ca151b5 JB |
3185 | for (j = 0; j < IWL_RATE_COUNT; j++) { |
3186 | desc += sprintf(buff+desc, | |
3187 | "counter=%d success=%d %%=%d\n", | |
d310e405 ES |
3188 | tbl->win[j].counter, |
3189 | tbl->win[j].success_counter, | |
3190 | tbl->win[j].success_ratio); | |
8ca151b5 JB |
3191 | } |
3192 | } | |
3193 | ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc); | |
3194 | kfree(buff); | |
3195 | return ret; | |
3196 | } | |
3197 | ||
3198 | static const struct file_operations rs_sta_dbgfs_stats_table_ops = { | |
3199 | .read = rs_sta_dbgfs_stats_table_read, | |
3200 | .open = simple_open, | |
3201 | .llseek = default_llseek, | |
3202 | }; | |
3203 | ||
757cf23b ES |
3204 | static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file, |
3205 | char __user *user_buf, | |
3206 | size_t count, loff_t *ppos) | |
3207 | { | |
3208 | static const char * const column_name[] = { | |
3209 | [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A", | |
3210 | [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B", | |
3211 | [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A", | |
3212 | [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B", | |
3213 | [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI", | |
3214 | [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI", | |
3215 | [RS_COLUMN_MIMO2] = "MIMO2", | |
3216 | [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI", | |
3217 | }; | |
3218 | ||
3219 | static const char * const rate_name[] = { | |
3220 | [IWL_RATE_1M_INDEX] = "1M", | |
3221 | [IWL_RATE_2M_INDEX] = "2M", | |
3222 | [IWL_RATE_5M_INDEX] = "5.5M", | |
3223 | [IWL_RATE_11M_INDEX] = "11M", | |
3224 | [IWL_RATE_6M_INDEX] = "6M|MCS0", | |
3225 | [IWL_RATE_9M_INDEX] = "9M", | |
3226 | [IWL_RATE_12M_INDEX] = "12M|MCS1", | |
3227 | [IWL_RATE_18M_INDEX] = "18M|MCS2", | |
3228 | [IWL_RATE_24M_INDEX] = "24M|MCS3", | |
3229 | [IWL_RATE_36M_INDEX] = "36M|MCS4", | |
3230 | [IWL_RATE_48M_INDEX] = "48M|MCS5", | |
3231 | [IWL_RATE_54M_INDEX] = "54M|MCS6", | |
3232 | [IWL_RATE_MCS_7_INDEX] = "MCS7", | |
3233 | [IWL_RATE_MCS_8_INDEX] = "MCS8", | |
3234 | [IWL_RATE_MCS_9_INDEX] = "MCS9", | |
3235 | }; | |
3236 | ||
3237 | char *buff, *pos, *endpos; | |
3238 | int col, rate; | |
3239 | ssize_t ret; | |
3240 | struct iwl_lq_sta *lq_sta = file->private_data; | |
3241 | struct rs_rate_stats *stats; | |
3242 | static const size_t bufsz = 1024; | |
3243 | ||
3244 | buff = kmalloc(bufsz, GFP_KERNEL); | |
3245 | if (!buff) | |
3246 | return -ENOMEM; | |
3247 | ||
3248 | pos = buff; | |
3249 | endpos = pos + bufsz; | |
3250 | ||
3251 | pos += scnprintf(pos, endpos - pos, "COLUMN,"); | |
3252 | for (rate = 0; rate < IWL_RATE_COUNT; rate++) | |
3253 | pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]); | |
3254 | pos += scnprintf(pos, endpos - pos, "\n"); | |
3255 | ||
3256 | for (col = 0; col < RS_COLUMN_COUNT; col++) { | |
3257 | pos += scnprintf(pos, endpos - pos, | |
3258 | "%s,", column_name[col]); | |
3259 | ||
3260 | for (rate = 0; rate < IWL_RATE_COUNT; rate++) { | |
f58220f6 | 3261 | stats = &(lq_sta->pers.tx_stats[col][rate]); |
757cf23b ES |
3262 | pos += scnprintf(pos, endpos - pos, |
3263 | "%llu/%llu,", | |
3264 | stats->success, | |
3265 | stats->total); | |
3266 | } | |
3267 | pos += scnprintf(pos, endpos - pos, "\n"); | |
3268 | } | |
3269 | ||
3270 | ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff); | |
3271 | kfree(buff); | |
3272 | return ret; | |
3273 | } | |
3274 | ||
3275 | static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file, | |
3276 | const char __user *user_buf, | |
3277 | size_t count, loff_t *ppos) | |
3278 | { | |
3279 | struct iwl_lq_sta *lq_sta = file->private_data; | |
f58220f6 | 3280 | memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats)); |
757cf23b ES |
3281 | |
3282 | return count; | |
3283 | } | |
3284 | ||
3285 | static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = { | |
3286 | .read = rs_sta_dbgfs_drv_tx_stats_read, | |
3287 | .write = rs_sta_dbgfs_drv_tx_stats_write, | |
3288 | .open = simple_open, | |
3289 | .llseek = default_llseek, | |
3290 | }; | |
3291 | ||
8ca151b5 JB |
3292 | static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir) |
3293 | { | |
3294 | struct iwl_lq_sta *lq_sta = mvm_sta; | |
c6e1faad EP |
3295 | debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir, |
3296 | lq_sta, &rs_sta_dbgfs_scale_table_ops); | |
3297 | debugfs_create_file("rate_stats_table", S_IRUSR, dir, | |
3298 | lq_sta, &rs_sta_dbgfs_stats_table_ops); | |
3299 | debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir, | |
3300 | lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops); | |
3301 | debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir, | |
3302 | &lq_sta->tx_agg_tid_en); | |
3303 | debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir, | |
3304 | &lq_sta->pers.dbg_fixed_txp_reduction); | |
8ca151b5 JB |
3305 | } |
3306 | ||
3307 | static void rs_remove_debugfs(void *mvm, void *mvm_sta) | |
3308 | { | |
8ca151b5 JB |
3309 | } |
3310 | #endif | |
3311 | ||
3312 | /* | |
3313 | * Initialization of rate scaling information is done by driver after | |
3314 | * the station is added. Since mac80211 calls this function before a | |
3315 | * station is added we ignore it. | |
3316 | */ | |
3317 | static void rs_rate_init_stub(void *mvm_r, | |
3de805cf SW |
3318 | struct ieee80211_supported_band *sband, |
3319 | struct cfg80211_chan_def *chandef, | |
3320 | struct ieee80211_sta *sta, void *mvm_sta) | |
8ca151b5 JB |
3321 | { |
3322 | } | |
631ad703 JB |
3323 | |
3324 | static const struct rate_control_ops rs_mvm_ops = { | |
8ca151b5 JB |
3325 | .name = RS_NAME, |
3326 | .tx_status = rs_tx_status, | |
3327 | .get_rate = rs_get_rate, | |
3328 | .rate_init = rs_rate_init_stub, | |
3329 | .alloc = rs_alloc, | |
3330 | .free = rs_free, | |
3331 | .alloc_sta = rs_alloc_sta, | |
3332 | .free_sta = rs_free_sta, | |
b87c2179 | 3333 | .rate_update = rs_rate_update, |
8ca151b5 JB |
3334 | #ifdef CONFIG_MAC80211_DEBUGFS |
3335 | .add_sta_debugfs = rs_add_debugfs, | |
3336 | .remove_sta_debugfs = rs_remove_debugfs, | |
3337 | #endif | |
3338 | }; | |
3339 | ||
3340 | int iwl_mvm_rate_control_register(void) | |
3341 | { | |
3342 | return ieee80211_rate_control_register(&rs_mvm_ops); | |
3343 | } | |
3344 | ||
3345 | void iwl_mvm_rate_control_unregister(void) | |
3346 | { | |
3347 | ieee80211_rate_control_unregister(&rs_mvm_ops); | |
3348 | } | |
9ee718aa EL |
3349 | |
3350 | /** | |
3351 | * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable | |
3352 | * Tx protection, according to this rquest and previous requests, | |
3353 | * and send the LQ command. | |
9ee718aa EL |
3354 | * @mvmsta: The station |
3355 | * @enable: Enable Tx protection? | |
3356 | */ | |
e126b5d9 JB |
3357 | int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, |
3358 | bool enable) | |
9ee718aa | 3359 | { |
e126b5d9 JB |
3360 | struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq; |
3361 | ||
9ee718aa EL |
3362 | lockdep_assert_held(&mvm->mutex); |
3363 | ||
3364 | if (enable) { | |
3365 | if (mvmsta->tx_protection == 0) | |
d307ec8d | 3366 | lq->flags |= LQ_FLAG_USE_RTS_MSK; |
9ee718aa EL |
3367 | mvmsta->tx_protection++; |
3368 | } else { | |
3369 | mvmsta->tx_protection--; | |
3370 | if (mvmsta->tx_protection == 0) | |
d307ec8d | 3371 | lq->flags &= ~LQ_FLAG_USE_RTS_MSK; |
9ee718aa EL |
3372 | } |
3373 | ||
9e680946 | 3374 | return iwl_mvm_send_lq_cmd(mvm, lq, false); |
9ee718aa | 3375 | } |