iwlwifi: rs-4965 fix return values
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / drivers / net / wireless / iwlwifi / iwl-4965-rs.c
1 /******************************************************************************
2 *
3 * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/skbuff.h>
29 #include <linux/wireless.h>
30 #include <net/mac80211.h>
31 #include <net/ieee80211.h>
32
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
36
37 #include <linux/workqueue.h>
38
39 #define IWL 4965
40
41 #include "../net/mac80211/ieee80211_rate.h"
42
43 #include "iwlwifi.h"
44 #include "iwl-helpers.h"
45
46 #define RS_NAME "iwl-4965-rs"
47
48 #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
49 #define IWL_NUMBER_TRY 1
50 #define IWL_HT_NUMBER_TRY 3
51
52 #define IWL_RATE_MAX_WINDOW 62
53 #define IWL_RATE_HIGH_TH 10880
54 #define IWL_RATE_MIN_FAILURE_TH 6
55 #define IWL_RATE_MIN_SUCCESS_TH 8
56 #define IWL_RATE_DECREASE_TH 1920
57 #define IWL_RATE_INCREASE_TH 8960
58 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ) /*2 seconds */
59
60 static u8 rs_ht_to_legacy[] = {
61 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
63 IWL_RATE_6M_INDEX,
64 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
65 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
66 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
67 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
68 };
69
70 struct iwl_rate {
71 u32 rate_n_flags;
72 } __attribute__ ((packed));
73
74 struct iwl_rate_scale_data {
75 u64 data;
76 s32 success_counter;
77 s32 success_ratio;
78 s32 counter;
79 s32 average_tpt;
80 unsigned long stamp;
81 };
82
83 struct iwl_scale_tbl_info {
84 enum iwl_table_type lq_type;
85 enum iwl_antenna_type antenna_type;
86 u8 is_SGI;
87 u8 is_fat;
88 u8 is_dup;
89 u8 action;
90 s32 *expected_tpt;
91 struct iwl_rate current_rate;
92 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
93 };
94
95 struct iwl_rate_scale_priv {
96 u8 active_tbl;
97 u8 enable_counter;
98 u8 stay_in_tbl;
99 u8 search_better_tbl;
100 s32 last_tpt;
101 u32 table_count_limit;
102 u32 max_failure_limit;
103 u32 max_success_limit;
104 u32 table_count;
105 u32 total_failed;
106 u32 total_success;
107 u32 flush_timer;
108 u8 action_counter;
109 u8 antenna;
110 u8 valid_antenna;
111 u8 is_green;
112 u8 is_dup;
113 u8 phymode;
114 u8 ibss_sta_added;
115 u32 supp_rates;
116 u16 active_rate;
117 u16 active_siso_rate;
118 u16 active_mimo_rate;
119 u16 active_rate_basic;
120 struct iwl_link_quality_cmd lq;
121 struct iwl_scale_tbl_info lq_info[LQ_SIZE];
122 #ifdef CONFIG_MAC80211_DEBUGFS
123 struct dentry *rs_sta_dbgfs_scale_table_file;
124 struct dentry *rs_sta_dbgfs_stats_table_file;
125 struct iwl_rate dbg_fixed;
126 struct iwl_priv *drv;
127 #endif
128 };
129
130 static void rs_rate_scale_perform(struct iwl_priv *priv,
131 struct net_device *dev,
132 struct ieee80211_hdr *hdr,
133 struct sta_info *sta);
134 static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
135 struct iwl_rate *tx_mcs,
136 struct iwl_link_quality_cmd *tbl);
137
138
139 #ifdef CONFIG_MAC80211_DEBUGFS
140 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
141 struct iwl_rate *mcs, int index);
142 #else
143 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
144 struct iwl_rate *mcs, int index)
145 {}
146 #endif
147 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
148 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
149 };
150
151 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
152 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
153 };
154
155 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
156 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
157 };
158
159 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
160 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
161 };
162
163 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
164 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
165 };
166
167 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
168 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
169 };
170
171 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
172 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
173 };
174
175 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
176 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
177 };
178
179 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
180 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
181 };
182
183 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
184 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
185 };
186
187 static int iwl_lq_sync_callback(struct iwl_priv *priv,
188 struct iwl_cmd *cmd, struct sk_buff *skb)
189 {
190 /*We didn't cache the SKB; let the caller free it */
191 return 1;
192 }
193
194 static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
195 {
196 return (u8)(rate_n_flags & 0xFF);
197 }
198
199 static int rs_send_lq_cmd(struct iwl_priv *priv,
200 struct iwl_link_quality_cmd *lq, u8 flags)
201 {
202 #ifdef CONFIG_IWLWIFI_DEBUG
203 int i;
204 #endif
205 struct iwl_host_cmd cmd = {
206 .id = REPLY_TX_LINK_QUALITY_CMD,
207 .len = sizeof(struct iwl_link_quality_cmd),
208 .meta.flags = flags,
209 .data = lq,
210 };
211
212 if ((lq->sta_id == 0xFF) &&
213 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
214 return -EINVAL;
215
216 if (lq->sta_id == 0xFF)
217 lq->sta_id = IWL_AP_ID;
218
219 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
220 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
221 lq->general_params.single_stream_ant_msk,
222 lq->general_params.dual_stream_ant_msk);
223 #ifdef CONFIG_IWLWIFI_DEBUG
224 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
225 IWL_DEBUG_RATE("lq index %d 0x%X\n",
226 i, lq->rs_table[i].rate_n_flags);
227 #endif
228
229 if (flags & CMD_ASYNC)
230 cmd.meta.u.callback = iwl_lq_sync_callback;
231
232 if (iwl_is_associated(priv) && priv->assoc_station_added &&
233 priv->lq_mngr.lq_ready)
234 return iwl_send_cmd(priv, &cmd);
235
236 return 0;
237 }
238
239 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
240 {
241 window->data = 0;
242 window->success_counter = 0;
243 window->success_ratio = IWL_INVALID_VALUE;
244 window->counter = 0;
245 window->average_tpt = IWL_INVALID_VALUE;
246 window->stamp = 0;
247 }
248
249 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
250 int scale_index, s32 tpt, u32 status)
251 {
252 struct iwl_rate_scale_data *window = NULL;
253 u64 mask;
254 u8 win_size = IWL_RATE_MAX_WINDOW;
255 s32 fail_count;
256
257 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
258 return -EINVAL;
259
260 window = &(windows[scale_index]);
261
262 if (window->counter >= win_size) {
263
264 window->counter = win_size - 1;
265 mask = 1;
266 mask = (mask << (win_size - 1));
267 if ((window->data & mask)) {
268 window->data &= ~mask;
269 window->success_counter = window->success_counter - 1;
270 }
271 }
272
273 window->counter = window->counter + 1;
274 mask = window->data;
275 window->data = (mask << 1);
276 if (status != 0) {
277 window->success_counter = window->success_counter + 1;
278 window->data |= 0x1;
279 }
280
281 if (window->counter > 0)
282 window->success_ratio = 128 * (100 * window->success_counter)
283 / window->counter;
284 else
285 window->success_ratio = IWL_INVALID_VALUE;
286
287 fail_count = window->counter - window->success_counter;
288
289 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
290 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
291 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
292 else
293 window->average_tpt = IWL_INVALID_VALUE;
294
295 window->stamp = jiffies;
296
297 return 0;
298 }
299
300 static void rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
301 struct iwl_scale_tbl_info *tbl,
302 int index, u8 use_green)
303 {
304 if (is_legacy(tbl->lq_type)) {
305 mcs_rate->rate_n_flags = iwl_rates[index].plcp;
306 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
307 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
308
309 } else if (is_siso(tbl->lq_type)) {
310 if (index > IWL_LAST_OFDM_RATE)
311 index = IWL_LAST_OFDM_RATE;
312 mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
313 RATE_MCS_HT_MSK;
314 } else {
315 if (index > IWL_LAST_OFDM_RATE)
316 index = IWL_LAST_OFDM_RATE;
317 mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
318 RATE_MCS_HT_MSK;
319 }
320
321 switch (tbl->antenna_type) {
322 case ANT_BOTH:
323 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
324 break;
325 case ANT_MAIN:
326 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
327 break;
328 case ANT_AUX:
329 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
330 break;
331 case ANT_NONE:
332 break;
333 }
334
335 if (is_legacy(tbl->lq_type))
336 return;
337
338 if (tbl->is_fat) {
339 if (tbl->is_dup)
340 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
341 else
342 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
343 }
344 if (tbl->is_SGI)
345 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
346
347 if (use_green) {
348 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
349 if (is_siso(tbl->lq_type))
350 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
351 }
352 }
353
354 static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
355 int phymode, struct iwl_scale_tbl_info *tbl,
356 int *rate_idx)
357 {
358 int index;
359 u32 ant_msk;
360
361 index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
362
363 if (index == IWL_RATE_INVALID) {
364 *rate_idx = -1;
365 return -EINVAL;
366 }
367 tbl->is_SGI = 0;
368 tbl->is_fat = 0;
369 tbl->is_dup = 0;
370 tbl->antenna_type = ANT_BOTH;
371
372 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
373 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
374
375 if (ant_msk == RATE_MCS_ANT_AB_MSK)
376 tbl->lq_type = LQ_NONE;
377 else {
378
379 if (phymode == MODE_IEEE80211A)
380 tbl->lq_type = LQ_A;
381 else
382 tbl->lq_type = LQ_G;
383
384 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
385 tbl->antenna_type = ANT_MAIN;
386 else
387 tbl->antenna_type = ANT_AUX;
388 }
389 *rate_idx = index;
390
391 } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
392 <= IWL_RATE_SISO_60M_PLCP) {
393 tbl->lq_type = LQ_SISO;
394
395 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
396 if (ant_msk == RATE_MCS_ANT_AB_MSK)
397 tbl->lq_type = LQ_NONE;
398 else {
399 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
400 tbl->antenna_type = ANT_MAIN;
401 else
402 tbl->antenna_type = ANT_AUX;
403 }
404 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
405 tbl->is_SGI = 1;
406
407 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
408 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
409 tbl->is_fat = 1;
410
411 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
412 tbl->is_dup = 1;
413
414 *rate_idx = index;
415 } else {
416 tbl->lq_type = LQ_MIMO;
417 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
418 tbl->is_SGI = 1;
419
420 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
421 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
422 tbl->is_fat = 1;
423
424 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
425 tbl->is_dup = 1;
426 *rate_idx = index;
427 }
428 return 0;
429 }
430
431 static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
432 struct iwl_scale_tbl_info *tbl)
433 {
434 if (tbl->antenna_type == ANT_AUX) {
435 tbl->antenna_type = ANT_MAIN;
436 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
437 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
438 } else {
439 tbl->antenna_type = ANT_AUX;
440 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
441 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
442 }
443 }
444
445 static inline u8 rs_use_green(struct iwl_priv *priv)
446 {
447 #ifdef CONFIG_IWLWIFI_HT
448 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
449 return 0;
450
451 return ((priv->current_assoc_ht.is_green_field) &&
452 !(priv->current_assoc_ht.operating_mode & 0x4));
453 #endif /*CONFIG_IWLWIFI_HT */
454 return 0;
455 }
456
457 /**
458 * rs_get_supported_rates - get the available rates
459 *
460 * if management frame or broadcast frame only return
461 * basic available rates.
462 *
463 */
464 static void rs_get_supported_rates(struct iwl_rate_scale_priv *lq_data,
465 struct ieee80211_hdr *hdr,
466 enum iwl_table_type rate_type,
467 u16 *data_rate)
468 {
469 if (is_legacy(rate_type))
470 *data_rate = lq_data->active_rate;
471 else {
472 if (is_siso(rate_type))
473 *data_rate = lq_data->active_siso_rate;
474 else
475 *data_rate = lq_data->active_mimo_rate;
476 }
477
478 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
479 lq_data->active_rate_basic)
480 *data_rate = lq_data->active_rate_basic;
481 }
482
483 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
484 {
485 u8 high = IWL_RATE_INVALID;
486 u8 low = IWL_RATE_INVALID;
487
488 /* 802.11A or ht walks to the next literal adjacent rate in
489 * the rate table */
490 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
491 int i;
492 u32 mask;
493
494 /* Find the previous rate that is in the rate mask */
495 i = index - 1;
496 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
497 if (rate_mask & mask) {
498 low = i;
499 break;
500 }
501 }
502
503 /* Find the next rate that is in the rate mask */
504 i = index + 1;
505 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
506 if (rate_mask & mask) {
507 high = i;
508 break;
509 }
510 }
511
512 return (high << 8) | low;
513 }
514
515 low = index;
516 while (low != IWL_RATE_INVALID) {
517 low = iwl_rates[low].prev_rs;
518 if (low == IWL_RATE_INVALID)
519 break;
520 if (rate_mask & (1 << low))
521 break;
522 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
523 }
524
525 high = index;
526 while (high != IWL_RATE_INVALID) {
527 high = iwl_rates[high].next_rs;
528 if (high == IWL_RATE_INVALID)
529 break;
530 if (rate_mask & (1 << high))
531 break;
532 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
533 }
534
535 return (high << 8) | low;
536 }
537
538 static void rs_get_lower_rate(struct iwl_rate_scale_priv *lq_data,
539 struct iwl_scale_tbl_info *tbl, u8 scale_index,
540 u8 ht_possible, struct iwl_rate *mcs_rate)
541 {
542 s32 low;
543 u16 rate_mask;
544 u16 high_low;
545 u8 switch_to_legacy = 0;
546 u8 is_green = lq_data->is_green;
547
548 /* check if we need to switch from HT to legacy rates.
549 * assumption is that mandatory rates (1Mbps or 6Mbps)
550 * are always supported (spec demand) */
551 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
552 switch_to_legacy = 1;
553 scale_index = rs_ht_to_legacy[scale_index];
554 if (lq_data->phymode == MODE_IEEE80211A)
555 tbl->lq_type = LQ_A;
556 else
557 tbl->lq_type = LQ_G;
558
559 if ((tbl->antenna_type == ANT_BOTH) ||
560 (tbl->antenna_type == ANT_NONE))
561 tbl->antenna_type = ANT_MAIN;
562
563 tbl->is_fat = 0;
564 tbl->is_SGI = 0;
565 }
566
567 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
568
569 /* mask with station rate restriction */
570 if (is_legacy(tbl->lq_type)) {
571 if (lq_data->phymode == (u8) MODE_IEEE80211A)
572 rate_mask = (u16)(rate_mask &
573 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
574 else
575 rate_mask = (u16)(rate_mask & lq_data->supp_rates);
576 }
577
578 /* if we did switched from HT to legacy check current rate */
579 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
580 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
581 return;
582 }
583
584 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
585 low = high_low & 0xff;
586
587 if (low != IWL_RATE_INVALID)
588 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
589 else
590 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
591 }
592
593 static void rs_tx_status(void *priv_rate,
594 struct net_device *dev,
595 struct sk_buff *skb,
596 struct ieee80211_tx_status *tx_resp)
597 {
598 int status;
599 u8 retries;
600 int rs_index, index = 0;
601 struct iwl_rate_scale_priv *lq;
602 struct iwl_link_quality_cmd *table;
603 struct sta_info *sta;
604 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
605 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
606 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
607 struct iwl_rate_scale_data *window = NULL;
608 struct iwl_rate_scale_data *search_win = NULL;
609 struct iwl_rate tx_mcs;
610 struct iwl_scale_tbl_info tbl_type;
611 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
612 u8 active_index = 0;
613 u16 fc = le16_to_cpu(hdr->frame_control);
614 s32 tpt = 0;
615
616 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
617
618 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
619 return;
620
621 retries = tx_resp->retry_count;
622
623 if (retries > 15)
624 retries = 15;
625
626
627 sta = sta_info_get(local, hdr->addr1);
628
629 if (!sta || !sta->rate_ctrl_priv) {
630 if (sta)
631 sta_info_put(sta);
632 return;
633 }
634
635 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
636
637 if (!priv->lq_mngr.lq_ready)
638 return;
639
640 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
641 return;
642
643 table = &lq->lq;
644 active_index = lq->active_tbl;
645
646 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
647 if (!lq->antenna)
648 lq->antenna = lq->valid_antenna;
649
650 lq->antenna = lq->valid_antenna;
651 curr_tbl = &(lq->lq_info[active_index]);
652 search_tbl = &(lq->lq_info[(1 - active_index)]);
653 window = (struct iwl_rate_scale_data *)
654 &(curr_tbl->win[0]);
655 search_win = (struct iwl_rate_scale_data *)
656 &(search_tbl->win[0]);
657
658 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
659
660 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
661 &tbl_type, &rs_index);
662 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
663 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
664 rs_index, tx_mcs.rate_n_flags);
665 sta_info_put(sta);
666 return;
667 }
668
669 if (retries &&
670 (tx_mcs.rate_n_flags !=
671 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
672 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
673 tx_mcs.rate_n_flags,
674 le32_to_cpu(table->rs_table[0].rate_n_flags));
675 sta_info_put(sta);
676 return;
677 }
678
679 while (retries) {
680 tx_mcs.rate_n_flags =
681 le32_to_cpu(table->rs_table[index].rate_n_flags);
682 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
683 &tbl_type, &rs_index);
684
685 if ((tbl_type.lq_type == search_tbl->lq_type) &&
686 (tbl_type.antenna_type == search_tbl->antenna_type) &&
687 (tbl_type.is_SGI == search_tbl->is_SGI)) {
688 if (search_tbl->expected_tpt)
689 tpt = search_tbl->expected_tpt[rs_index];
690 else
691 tpt = 0;
692 rs_collect_tx_data(search_win,
693 rs_index, tpt, 0);
694 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
695 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
696 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
697 if (curr_tbl->expected_tpt)
698 tpt = curr_tbl->expected_tpt[rs_index];
699 else
700 tpt = 0;
701 rs_collect_tx_data(window, rs_index, tpt, 0);
702 }
703 if (lq->stay_in_tbl)
704 lq->total_failed++;
705 --retries;
706 index++;
707
708 }
709
710 if (!tx_resp->retry_count)
711 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
712 else
713 tx_mcs.rate_n_flags =
714 le32_to_cpu(table->rs_table[index].rate_n_flags);
715
716 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
717 &tbl_type, &rs_index);
718
719 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
720 status = 1;
721 else
722 status = 0;
723
724 if ((tbl_type.lq_type == search_tbl->lq_type) &&
725 (tbl_type.antenna_type == search_tbl->antenna_type) &&
726 (tbl_type.is_SGI == search_tbl->is_SGI)) {
727 if (search_tbl->expected_tpt)
728 tpt = search_tbl->expected_tpt[rs_index];
729 else
730 tpt = 0;
731 rs_collect_tx_data(search_win,
732 rs_index, tpt, status);
733 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
734 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
735 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
736 if (curr_tbl->expected_tpt)
737 tpt = curr_tbl->expected_tpt[rs_index];
738 else
739 tpt = 0;
740 rs_collect_tx_data(window, rs_index, tpt, status);
741 }
742
743 if (lq->stay_in_tbl) {
744 if (status)
745 lq->total_success++;
746 else
747 lq->total_failed++;
748 }
749
750 rs_rate_scale_perform(priv, dev, hdr, sta);
751 sta_info_put(sta);
752 return;
753 }
754
755 static u8 rs_is_ant_connected(u8 valid_antenna,
756 enum iwl_antenna_type antenna_type)
757 {
758 if (antenna_type == ANT_AUX)
759 return ((valid_antenna & 0x2) ? 1:0);
760 else if (antenna_type == ANT_MAIN)
761 return ((valid_antenna & 0x1) ? 1:0);
762 else if (antenna_type == ANT_BOTH)
763 return ((valid_antenna & 0x3) == 0x3);
764
765 return 1;
766 }
767
768 static u8 rs_is_other_ant_connected(u8 valid_antenna,
769 enum iwl_antenna_type antenna_type)
770 {
771 if (antenna_type == ANT_AUX)
772 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
773 else
774 return rs_is_ant_connected(valid_antenna, ANT_AUX);
775
776 return 0;
777 }
778
779 static void rs_set_stay_in_table(u8 is_legacy,
780 struct iwl_rate_scale_priv *lq_data)
781 {
782 IWL_DEBUG_HT("we are staying in the same table\n");
783 lq_data->stay_in_tbl = 1;
784 if (is_legacy) {
785 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
786 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
787 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
788 } else {
789 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
790 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
791 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
792 }
793 lq_data->table_count = 0;
794 lq_data->total_failed = 0;
795 lq_data->total_success = 0;
796 }
797
798 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
799 struct iwl_scale_tbl_info *tbl)
800 {
801 if (is_legacy(tbl->lq_type)) {
802 if (!is_a_band(tbl->lq_type))
803 tbl->expected_tpt = expected_tpt_G;
804 else
805 tbl->expected_tpt = expected_tpt_A;
806 } else if (is_siso(tbl->lq_type)) {
807 if (tbl->is_fat && !lq_data->is_dup)
808 if (tbl->is_SGI)
809 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
810 else
811 tbl->expected_tpt = expected_tpt_siso40MHz;
812 else if (tbl->is_SGI)
813 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
814 else
815 tbl->expected_tpt = expected_tpt_siso20MHz;
816
817 } else if (is_mimo(tbl->lq_type)) {
818 if (tbl->is_fat && !lq_data->is_dup)
819 if (tbl->is_SGI)
820 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
821 else
822 tbl->expected_tpt = expected_tpt_mimo40MHz;
823 else if (tbl->is_SGI)
824 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
825 else
826 tbl->expected_tpt = expected_tpt_mimo20MHz;
827 } else
828 tbl->expected_tpt = expected_tpt_G;
829 }
830
831 #ifdef CONFIG_IWLWIFI_HT
832 static s32 rs_get_best_rate(struct iwl_priv *priv,
833 struct iwl_rate_scale_priv *lq_data,
834 struct iwl_scale_tbl_info *tbl,
835 u16 rate_mask, s8 index, s8 rate)
836 {
837 struct iwl_scale_tbl_info *active_tbl =
838 &(lq_data->lq_info[lq_data->active_tbl]);
839 s32 new_rate, high, low, start_hi;
840 s32 active_sr = active_tbl->win[index].success_ratio;
841 s32 *tpt_tbl = tbl->expected_tpt;
842 s32 active_tpt = active_tbl->expected_tpt[index];
843 u16 high_low;
844
845 new_rate = high = low = start_hi = IWL_RATE_INVALID;
846
847 for (; ;) {
848 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
849
850 low = high_low & 0xff;
851 high = (high_low >> 8) & 0xff;
852
853 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
854 ((active_sr > IWL_RATE_DECREASE_TH) &&
855 (active_sr <= IWL_RATE_HIGH_TH) &&
856 (tpt_tbl[rate] <= active_tpt))) ||
857 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
858 (tpt_tbl[rate] > active_tpt))) {
859
860 if (start_hi != IWL_RATE_INVALID) {
861 new_rate = start_hi;
862 break;
863 }
864 new_rate = rate;
865 if (low != IWL_RATE_INVALID)
866 rate = low;
867 else
868 break;
869 } else {
870 if (new_rate != IWL_RATE_INVALID)
871 break;
872 else if (high != IWL_RATE_INVALID) {
873 start_hi = high;
874 rate = high;
875 } else {
876 new_rate = rate;
877 break;
878 }
879 }
880 }
881
882 return new_rate;
883 }
884 #endif /* CONFIG_IWLWIFI_HT */
885
886 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
887 {
888 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
889 }
890
891 static int rs_switch_to_mimo(struct iwl_priv *priv,
892 struct iwl_rate_scale_priv *lq_data,
893 struct iwl_scale_tbl_info *tbl, int index)
894 {
895 #ifdef CONFIG_IWLWIFI_HT
896 u16 rate_mask;
897 s32 rate;
898 s8 is_green = lq_data->is_green;
899
900 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
901 return -1;
902
903 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
904 tbl->lq_type = LQ_MIMO;
905 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
906 &rate_mask);
907
908 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
909 return -1;
910
911 if (!rs_is_both_ant_supp(lq_data->antenna))
912 return -1;
913
914 tbl->is_dup = lq_data->is_dup;
915 tbl->action = 0;
916 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
917 tbl->is_fat = 1;
918 else
919 tbl->is_fat = 0;
920
921 if (tbl->is_fat) {
922 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
923 tbl->is_SGI = 1;
924 else
925 tbl->is_SGI = 0;
926 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
927 tbl->is_SGI = 1;
928 else
929 tbl->is_SGI = 0;
930
931 rs_get_expected_tpt_table(lq_data, tbl);
932
933 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
934
935 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
936 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
937 return -1;
938 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
939
940 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
941 tbl->current_rate.rate_n_flags, is_green);
942
943 #endif /*CONFIG_IWLWIFI_HT */
944 return 0;
945 }
946
947 static int rs_switch_to_siso(struct iwl_priv *priv,
948 struct iwl_rate_scale_priv *lq_data,
949 struct iwl_scale_tbl_info *tbl, int index)
950 {
951 #ifdef CONFIG_IWLWIFI_HT
952 u16 rate_mask;
953 u8 is_green = lq_data->is_green;
954 s32 rate;
955
956 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
957 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
958 return -1;
959
960 tbl->is_dup = lq_data->is_dup;
961 tbl->lq_type = LQ_SISO;
962 tbl->action = 0;
963 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
964 &rate_mask);
965
966 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
967 tbl->is_fat = 1;
968 else
969 tbl->is_fat = 0;
970
971 if (tbl->is_fat) {
972 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
973 tbl->is_SGI = 1;
974 else
975 tbl->is_SGI = 0;
976 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
977 tbl->is_SGI = 1;
978 else
979 tbl->is_SGI = 0;
980
981 if (is_green)
982 tbl->is_SGI = 0;
983
984 rs_get_expected_tpt_table(lq_data, tbl);
985 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
986
987 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
988 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
989 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
990 rate, rate_mask);
991 return -1;
992 }
993 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
994 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
995 tbl->current_rate.rate_n_flags, is_green);
996
997 #endif /*CONFIG_IWLWIFI_HT */
998 return 0;
999 }
1000
1001 static int rs_move_legacy_other(struct iwl_priv *priv,
1002 struct iwl_rate_scale_priv *lq_data,
1003 int index)
1004 {
1005 int ret = 0;
1006 struct iwl_scale_tbl_info *tbl =
1007 &(lq_data->lq_info[lq_data->active_tbl]);
1008 struct iwl_scale_tbl_info *search_tbl =
1009 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1010 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1011 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1012 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1013 u8 start_action = tbl->action;
1014
1015 for (; ;) {
1016 switch (tbl->action) {
1017 case IWL_LEGACY_SWITCH_ANTENNA:
1018 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1019
1020 search_tbl->lq_type = LQ_NONE;
1021 lq_data->action_counter++;
1022 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1023 break;
1024 if (!rs_is_other_ant_connected(lq_data->antenna,
1025 tbl->antenna_type))
1026 break;
1027
1028 memcpy(search_tbl, tbl, sz);
1029
1030 rs_toggle_antenna(&(search_tbl->current_rate),
1031 search_tbl);
1032 rs_get_expected_tpt_table(lq_data, search_tbl);
1033 lq_data->search_better_tbl = 1;
1034 goto out;
1035
1036 case IWL_LEGACY_SWITCH_SISO:
1037 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1038 memcpy(search_tbl, tbl, sz);
1039 search_tbl->lq_type = LQ_SISO;
1040 search_tbl->is_SGI = 0;
1041 search_tbl->is_fat = 0;
1042 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
1043 index);
1044 if (!ret) {
1045 lq_data->search_better_tbl = 1;
1046 lq_data->action_counter = 0;
1047 goto out;
1048 }
1049
1050 break;
1051 case IWL_LEGACY_SWITCH_MIMO:
1052 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1053 memcpy(search_tbl, tbl, sz);
1054 search_tbl->lq_type = LQ_MIMO;
1055 search_tbl->is_SGI = 0;
1056 search_tbl->is_fat = 0;
1057 search_tbl->antenna_type = ANT_BOTH;
1058 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
1059 index);
1060 if (!ret) {
1061 lq_data->search_better_tbl = 1;
1062 lq_data->action_counter = 0;
1063 goto out;
1064 }
1065 break;
1066 }
1067 tbl->action++;
1068 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1069 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1070
1071 if (tbl->action == start_action)
1072 break;
1073
1074 }
1075 return 0;
1076
1077 out:
1078 tbl->action++;
1079 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1080 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1081 return 0;
1082
1083 }
1084
1085 static int rs_move_siso_to_other(struct iwl_priv *priv,
1086 struct iwl_rate_scale_priv *lq_data,
1087 int index)
1088 {
1089 int ret;
1090 u8 is_green = lq_data->is_green;
1091 struct iwl_scale_tbl_info *tbl =
1092 &(lq_data->lq_info[lq_data->active_tbl]);
1093 struct iwl_scale_tbl_info *search_tbl =
1094 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1095 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1096 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1097 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1098 u8 start_action = tbl->action;
1099
1100 for (;;) {
1101 lq_data->action_counter++;
1102 switch (tbl->action) {
1103 case IWL_SISO_SWITCH_ANTENNA:
1104 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1105 search_tbl->lq_type = LQ_NONE;
1106 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1107 break;
1108 if (!rs_is_other_ant_connected(lq_data->antenna,
1109 tbl->antenna_type))
1110 break;
1111
1112 memcpy(search_tbl, tbl, sz);
1113 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1114 rs_toggle_antenna(&(search_tbl->current_rate),
1115 search_tbl);
1116 lq_data->search_better_tbl = 1;
1117
1118 goto out;
1119
1120 case IWL_SISO_SWITCH_MIMO:
1121 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1122 memcpy(search_tbl, tbl, sz);
1123 search_tbl->lq_type = LQ_MIMO;
1124 search_tbl->is_SGI = 0;
1125 search_tbl->is_fat = 0;
1126 search_tbl->antenna_type = ANT_BOTH;
1127 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
1128 index);
1129 if (!ret) {
1130 lq_data->search_better_tbl = 1;
1131 goto out;
1132 }
1133 break;
1134 case IWL_SISO_SWITCH_GI:
1135 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1136 memcpy(search_tbl, tbl, sz);
1137 search_tbl->action = 0;
1138 if (search_tbl->is_SGI)
1139 search_tbl->is_SGI = 0;
1140 else if (!is_green)
1141 search_tbl->is_SGI = 1;
1142 else
1143 break;
1144 lq_data->search_better_tbl = 1;
1145 if ((tbl->lq_type == LQ_SISO) &&
1146 (tbl->is_SGI)) {
1147 s32 tpt = lq_data->last_tpt / 100;
1148 if (((!tbl->is_fat) &&
1149 (tpt >= expected_tpt_siso20MHz[index])) ||
1150 ((tbl->is_fat) &&
1151 (tpt >= expected_tpt_siso40MHz[index])))
1152 lq_data->search_better_tbl = 0;
1153 }
1154 rs_get_expected_tpt_table(lq_data, search_tbl);
1155 rs_mcs_from_tbl(&search_tbl->current_rate,
1156 search_tbl, index, is_green);
1157 goto out;
1158 }
1159 tbl->action++;
1160 if (tbl->action > IWL_SISO_SWITCH_GI)
1161 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1162
1163 if (tbl->action == start_action)
1164 break;
1165 }
1166 return 0;
1167
1168 out:
1169 tbl->action++;
1170 if (tbl->action > IWL_SISO_SWITCH_GI)
1171 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1172 return 0;
1173 }
1174
1175 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1176 struct iwl_rate_scale_priv *lq_data,
1177 int index)
1178 {
1179 int ret;
1180 s8 is_green = lq_data->is_green;
1181 struct iwl_scale_tbl_info *tbl =
1182 &(lq_data->lq_info[lq_data->active_tbl]);
1183 struct iwl_scale_tbl_info *search_tbl =
1184 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1185 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1186 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1187 u8 start_action = tbl->action;
1188
1189 for (;;) {
1190 lq_data->action_counter++;
1191 switch (tbl->action) {
1192 case IWL_MIMO_SWITCH_ANTENNA_A:
1193 case IWL_MIMO_SWITCH_ANTENNA_B:
1194 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1195 memcpy(search_tbl, tbl, sz);
1196 search_tbl->lq_type = LQ_SISO;
1197 search_tbl->is_SGI = 0;
1198 search_tbl->is_fat = 0;
1199 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1200 search_tbl->antenna_type = ANT_MAIN;
1201 else
1202 search_tbl->antenna_type = ANT_AUX;
1203
1204 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
1205 index);
1206 if (!ret) {
1207 lq_data->search_better_tbl = 1;
1208 goto out;
1209 }
1210 break;
1211
1212 case IWL_MIMO_SWITCH_GI:
1213 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1214 memcpy(search_tbl, tbl, sz);
1215 search_tbl->lq_type = LQ_MIMO;
1216 search_tbl->antenna_type = ANT_BOTH;
1217 search_tbl->action = 0;
1218 if (search_tbl->is_SGI)
1219 search_tbl->is_SGI = 0;
1220 else
1221 search_tbl->is_SGI = 1;
1222 lq_data->search_better_tbl = 1;
1223 if ((tbl->lq_type == LQ_MIMO) &&
1224 (tbl->is_SGI)) {
1225 s32 tpt = lq_data->last_tpt / 100;
1226 if (((!tbl->is_fat) &&
1227 (tpt >= expected_tpt_mimo20MHz[index])) ||
1228 ((tbl->is_fat) &&
1229 (tpt >= expected_tpt_mimo40MHz[index])))
1230 lq_data->search_better_tbl = 0;
1231 }
1232 rs_get_expected_tpt_table(lq_data, search_tbl);
1233 rs_mcs_from_tbl(&search_tbl->current_rate,
1234 search_tbl, index, is_green);
1235 goto out;
1236
1237 }
1238 tbl->action++;
1239 if (tbl->action > IWL_MIMO_SWITCH_GI)
1240 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1241
1242 if (tbl->action == start_action)
1243 break;
1244 }
1245
1246 return 0;
1247 out:
1248 tbl->action++;
1249 if (tbl->action > IWL_MIMO_SWITCH_GI)
1250 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1251 return 0;
1252
1253 }
1254
1255 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1256 {
1257 struct iwl_scale_tbl_info *tbl;
1258 int i;
1259 int active_tbl;
1260 int flush_interval_passed = 0;
1261
1262 active_tbl = lq_data->active_tbl;
1263
1264 tbl = &(lq_data->lq_info[active_tbl]);
1265
1266 if (lq_data->stay_in_tbl) {
1267
1268 if (lq_data->flush_timer)
1269 flush_interval_passed =
1270 time_after(jiffies,
1271 (unsigned long)(lq_data->flush_timer +
1272 IWL_RATE_SCALE_FLUSH_INTVL));
1273
1274 flush_interval_passed = 0;
1275 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1276 (lq_data->total_success > lq_data->max_success_limit) ||
1277 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1278 && (flush_interval_passed))) {
1279 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1280 lq_data->total_failed,
1281 lq_data->total_success,
1282 flush_interval_passed);
1283 lq_data->stay_in_tbl = 0;
1284 lq_data->total_failed = 0;
1285 lq_data->total_success = 0;
1286 lq_data->flush_timer = 0;
1287 } else if (lq_data->table_count > 0) {
1288 lq_data->table_count++;
1289 if (lq_data->table_count >=
1290 lq_data->table_count_limit) {
1291 lq_data->table_count = 0;
1292
1293 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1294 for (i = 0; i < IWL_RATE_COUNT; i++)
1295 rs_rate_scale_clear_window(
1296 &(tbl->win[i]));
1297 }
1298 }
1299
1300 if (!lq_data->stay_in_tbl) {
1301 for (i = 0; i < IWL_RATE_COUNT; i++)
1302 rs_rate_scale_clear_window(&(tbl->win[i]));
1303 }
1304 }
1305 }
1306
1307 static void rs_rate_scale_perform(struct iwl_priv *priv,
1308 struct net_device *dev,
1309 struct ieee80211_hdr *hdr,
1310 struct sta_info *sta)
1311 {
1312 int low = IWL_RATE_INVALID;
1313 int high = IWL_RATE_INVALID;
1314 int index;
1315 int i;
1316 struct iwl_rate_scale_data *window = NULL;
1317 int current_tpt = IWL_INVALID_VALUE;
1318 int low_tpt = IWL_INVALID_VALUE;
1319 int high_tpt = IWL_INVALID_VALUE;
1320 u32 fail_count;
1321 s8 scale_action = 0;
1322 u16 fc, rate_mask;
1323 u8 update_lq = 0;
1324 struct iwl_rate_scale_priv *lq_data;
1325 struct iwl_scale_tbl_info *tbl, *tbl1;
1326 u16 rate_scale_index_msk = 0;
1327 struct iwl_rate mcs_rate;
1328 u8 is_green = 0;
1329 u8 active_tbl = 0;
1330 u8 done_search = 0;
1331 u16 high_low;
1332
1333 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1334
1335 fc = le16_to_cpu(hdr->frame_control);
1336 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1337 /* Send management frames and broadcast/multicast data using
1338 * lowest rate. */
1339 /* TODO: this could probably be improved.. */
1340 return;
1341 }
1342
1343 if (!sta || !sta->rate_ctrl_priv)
1344 return;
1345
1346 if (!priv->lq_mngr.lq_ready) {
1347 IWL_DEBUG_RATE("still rate scaling not ready\n");
1348 return;
1349 }
1350 lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1351
1352 if (!lq_data->search_better_tbl)
1353 active_tbl = lq_data->active_tbl;
1354 else
1355 active_tbl = 1 - lq_data->active_tbl;
1356
1357 tbl = &(lq_data->lq_info[active_tbl]);
1358 is_green = lq_data->is_green;
1359
1360 index = sta->last_txrate;
1361
1362 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1363 tbl->lq_type);
1364
1365 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1366 &rate_mask);
1367
1368 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1369
1370 /* mask with station rate restriction */
1371 if (is_legacy(tbl->lq_type)) {
1372 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1373 rate_scale_index_msk = (u16) (rate_mask &
1374 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
1375 else
1376 rate_scale_index_msk = (u16) (rate_mask &
1377 lq_data->supp_rates);
1378
1379 } else
1380 rate_scale_index_msk = rate_mask;
1381
1382 if (!rate_scale_index_msk)
1383 rate_scale_index_msk = rate_mask;
1384
1385 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1386 index = IWL_INVALID_VALUE;
1387 update_lq = 1;
1388
1389 /* get the lowest available rate */
1390 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1391 if ((1 << i) & rate_scale_index_msk)
1392 index = i;
1393 }
1394
1395 if (index == IWL_INVALID_VALUE) {
1396 IWL_WARNING("Can not find a suitable rate\n");
1397 return;
1398 }
1399 }
1400
1401 if (!tbl->expected_tpt)
1402 rs_get_expected_tpt_table(lq_data, tbl);
1403
1404 window = &(tbl->win[index]);
1405
1406 fail_count = window->counter - window->success_counter;
1407 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1408 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1409 || (tbl->expected_tpt == NULL)) {
1410 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1411 "for index %d\n",
1412 window->success_counter, window->counter, index);
1413 window->average_tpt = IWL_INVALID_VALUE;
1414 rs_stay_in_table(lq_data);
1415 if (update_lq) {
1416 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1417 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1418 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1419 }
1420 goto out;
1421
1422 } else
1423 window->average_tpt = ((window->success_ratio *
1424 tbl->expected_tpt[index] + 64) / 128);
1425
1426 if (lq_data->search_better_tbl) {
1427 int success_limit = IWL_RATE_SCALE_SWITCH;
1428
1429 if ((window->success_ratio > success_limit) ||
1430 (window->average_tpt > lq_data->last_tpt)) {
1431 if (!is_legacy(tbl->lq_type)) {
1432 IWL_DEBUG_HT("LQ: we are switching to HT"
1433 " rate suc %d current tpt %d"
1434 " old tpt %d\n",
1435 window->success_ratio,
1436 window->average_tpt,
1437 lq_data->last_tpt);
1438 lq_data->enable_counter = 1;
1439 }
1440 lq_data->active_tbl = active_tbl;
1441 current_tpt = window->average_tpt;
1442 } else {
1443 tbl->lq_type = LQ_NONE;
1444 active_tbl = lq_data->active_tbl;
1445 tbl = &(lq_data->lq_info[active_tbl]);
1446
1447 index = iwl_rate_index_from_plcp(
1448 tbl->current_rate.rate_n_flags);
1449
1450 update_lq = 1;
1451 current_tpt = lq_data->last_tpt;
1452 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1453 }
1454 lq_data->search_better_tbl = 0;
1455 done_search = 1;
1456 goto lq_update;
1457 }
1458
1459 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1460 tbl->lq_type);
1461 low = high_low & 0xff;
1462 high = (high_low >> 8) & 0xff;
1463
1464 current_tpt = window->average_tpt;
1465
1466 if (low != IWL_RATE_INVALID)
1467 low_tpt = tbl->win[low].average_tpt;
1468
1469 if (high != IWL_RATE_INVALID)
1470 high_tpt = tbl->win[high].average_tpt;
1471
1472
1473 scale_action = 1;
1474
1475 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1476 (current_tpt == 0)) {
1477 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1478 scale_action = -1;
1479 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1480 (high_tpt == IWL_INVALID_VALUE))
1481 scale_action = 1;
1482 else if ((low_tpt != IWL_INVALID_VALUE) &&
1483 (high_tpt != IWL_INVALID_VALUE) &&
1484 (low_tpt < current_tpt) &&
1485 (high_tpt < current_tpt))
1486 scale_action = 0;
1487 else {
1488 if (high_tpt != IWL_INVALID_VALUE) {
1489 if (high_tpt > current_tpt)
1490 scale_action = 1;
1491 else {
1492 IWL_DEBUG_RATE
1493 ("decrease rate because of high tpt\n");
1494 scale_action = -1;
1495 }
1496 } else if (low_tpt != IWL_INVALID_VALUE) {
1497 if (low_tpt > current_tpt) {
1498 IWL_DEBUG_RATE
1499 ("decrease rate because of low tpt\n");
1500 scale_action = -1;
1501 } else
1502 scale_action = 1;
1503 }
1504 }
1505
1506 if (scale_action == -1) {
1507 if ((low != IWL_RATE_INVALID) &&
1508 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1509 (current_tpt > (100 * tbl->expected_tpt[low]))))
1510 scale_action = 0;
1511 } else if ((scale_action == 1) &&
1512 (window->success_ratio < IWL_RATE_INCREASE_TH))
1513 scale_action = 0;
1514
1515 switch (scale_action) {
1516 case -1:
1517 if (low != IWL_RATE_INVALID) {
1518 update_lq = 1;
1519 index = low;
1520 }
1521 break;
1522 case 1:
1523 if (high != IWL_RATE_INVALID) {
1524 update_lq = 1;
1525 index = high;
1526 }
1527
1528 break;
1529 case 0:
1530 default:
1531 break;
1532 }
1533
1534 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1535 "high %d type %d\n",
1536 index, scale_action, low, high, tbl->lq_type);
1537
1538 lq_update:
1539 if (update_lq) {
1540 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1541 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1542 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1543 }
1544 rs_stay_in_table(lq_data);
1545
1546 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1547 lq_data->last_tpt = current_tpt;
1548
1549 if (is_legacy(tbl->lq_type))
1550 rs_move_legacy_other(priv, lq_data, index);
1551 else if (is_siso(tbl->lq_type))
1552 rs_move_siso_to_other(priv, lq_data, index);
1553 else
1554 rs_move_mimo_to_other(priv, lq_data, index);
1555
1556 if (lq_data->search_better_tbl) {
1557 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1558 for (i = 0; i < IWL_RATE_COUNT; i++)
1559 rs_rate_scale_clear_window(&(tbl->win[i]));
1560
1561 index = iwl_rate_index_from_plcp(
1562 tbl->current_rate.rate_n_flags);
1563
1564 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1565 tbl->current_rate.rate_n_flags, index);
1566 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1567 &lq_data->lq);
1568 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1569 }
1570 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1571
1572 if (is_legacy(tbl1->lq_type) &&
1573 #ifdef CONFIG_IWLWIFI_HT
1574 !priv->current_assoc_ht.is_ht &&
1575 #endif
1576 (lq_data->action_counter >= 1)) {
1577 lq_data->action_counter = 0;
1578 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1579 rs_set_stay_in_table(1, lq_data);
1580 }
1581
1582 if (lq_data->enable_counter &&
1583 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1584 #ifdef CONFIG_IWLWIFI_HT_AGG
1585 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1586 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1587 priv->lq_mngr.agg_ctrl.tid_retry =
1588 TID_ALL_SPECIFIED;
1589 schedule_work(&priv->agg_work);
1590 }
1591 #endif /*CONFIG_IWLWIFI_HT_AGG */
1592 lq_data->action_counter = 0;
1593 rs_set_stay_in_table(0, lq_data);
1594 }
1595 } else {
1596 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1597 lq_data->flush_timer = jiffies;
1598 }
1599
1600 out:
1601 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1602 i = index;
1603 sta->last_txrate = i;
1604
1605 /* sta->txrate is an index to A mode rates which start
1606 * at IWL_FIRST_OFDM_RATE
1607 */
1608 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1609 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1610 else
1611 sta->txrate = i;
1612
1613 return;
1614 }
1615
1616
1617 static void rs_initialize_lq(struct iwl_priv *priv,
1618 struct sta_info *sta)
1619 {
1620 int i;
1621 struct iwl_rate_scale_priv *lq;
1622 struct iwl_scale_tbl_info *tbl;
1623 u8 active_tbl = 0;
1624 int rate_idx;
1625 u8 use_green = rs_use_green(priv);
1626 struct iwl_rate mcs_rate;
1627
1628 if (!sta || !sta->rate_ctrl_priv)
1629 goto out;
1630
1631 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1632 i = sta->last_txrate;
1633
1634 if ((lq->lq.sta_id == 0xff) &&
1635 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1636 goto out;
1637
1638 if (!lq->search_better_tbl)
1639 active_tbl = lq->active_tbl;
1640 else
1641 active_tbl = 1 - lq->active_tbl;
1642
1643 tbl = &(lq->lq_info[active_tbl]);
1644
1645 if ((i < 0) || (i >= IWL_RATE_COUNT))
1646 i = 0;
1647
1648 mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1649 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1650 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1651
1652 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1653 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1654
1655 tbl->antenna_type = ANT_AUX;
1656 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1657 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1658 rs_toggle_antenna(&mcs_rate, tbl);
1659
1660 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1661 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1662 rs_get_expected_tpt_table(lq, tbl);
1663 rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
1664 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1665 out:
1666 return;
1667 }
1668
1669 static void rs_get_rate(void *priv_rate, struct net_device *dev,
1670 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
1671 struct rate_selection *sel)
1672 {
1673
1674 int i;
1675 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1676 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1677 struct sta_info *sta;
1678 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1679 struct iwl_rate_scale_priv *lq;
1680
1681 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
1682
1683 sta = sta_info_get(local, hdr->addr1);
1684
1685 if (!sta || !sta->rate_ctrl_priv) {
1686 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
1687 if (sta)
1688 sta_info_put(sta);
1689 return;
1690 }
1691
1692 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1693 i = sta->last_txrate;
1694
1695 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1696 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1697 DECLARE_MAC_BUF(mac);
1698
1699 if (sta_id == IWL_INVALID_STATION) {
1700 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1701 print_mac(mac, hdr->addr1));
1702 sta_id = iwl_add_station(priv,
1703 hdr->addr1, 0, CMD_ASYNC);
1704 }
1705 if ((sta_id != IWL_INVALID_STATION)) {
1706 lq->lq.sta_id = sta_id;
1707 lq->lq.rs_table[0].rate_n_flags = 0;
1708 lq->ibss_sta_added = 1;
1709 rs_initialize_lq(priv, sta);
1710 }
1711 if (!lq->ibss_sta_added)
1712 goto done;
1713 }
1714
1715 done:
1716 if ((i < 0) || (i > IWL_RATE_COUNT)) {
1717 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
1718 return;
1719 }
1720 sta_info_put(sta);
1721
1722 sel->rate = &priv->ieee_rates[i];
1723 }
1724
1725 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1726 {
1727 struct iwl_rate_scale_priv *crl;
1728 int i, j;
1729
1730 IWL_DEBUG_RATE("create station rate scale window\n");
1731
1732 crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1733
1734 if (crl == NULL)
1735 return NULL;
1736 crl->lq.sta_id = 0xff;
1737
1738
1739 for (j = 0; j < LQ_SIZE; j++)
1740 for (i = 0; i < IWL_RATE_COUNT; i++)
1741 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1742
1743 return crl;
1744 }
1745
1746 static void rs_rate_init(void *priv_rate, void *priv_sta,
1747 struct ieee80211_local *local,
1748 struct sta_info *sta)
1749 {
1750 int i, j;
1751 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1752 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1753 struct iwl_rate_scale_priv *crl = priv_sta;
1754
1755 crl->flush_timer = 0;
1756 crl->supp_rates = sta->supp_rates;
1757 sta->txrate = 3;
1758 for (j = 0; j < LQ_SIZE; j++)
1759 for (i = 0; i < IWL_RATE_COUNT; i++)
1760 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1761
1762 IWL_DEBUG_RATE("rate scale global init\n");
1763 /* TODO: what is a good starting rate for STA? About middle? Maybe not
1764 * the lowest or the highest rate.. Could consider using RSSI from
1765 * previous packets? Need to have IEEE 802.1X auth succeed immediately
1766 * after assoc.. */
1767
1768 crl->ibss_sta_added = 0;
1769 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1770 u8 sta_id = iwl_hw_find_station(priv, sta->addr);
1771 DECLARE_MAC_BUF(mac);
1772
1773 /* for IBSS the call are from tasklet */
1774 IWL_DEBUG_HT("LQ: ADD station %s\n",
1775 print_mac(mac, sta->addr));
1776
1777 if (sta_id == IWL_INVALID_STATION) {
1778 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1779 print_mac(mac, sta->addr));
1780 sta_id = iwl_add_station(priv,
1781 sta->addr, 0, CMD_ASYNC);
1782 }
1783 if ((sta_id != IWL_INVALID_STATION)) {
1784 crl->lq.sta_id = sta_id;
1785 crl->lq.rs_table[0].rate_n_flags = 0;
1786 }
1787 /* FIXME: this is w/a remove it later */
1788 priv->assoc_station_added = 1;
1789 }
1790
1791 for (i = 0; i < mode->num_rates; i++) {
1792 if ((sta->supp_rates & BIT(i)) &&
1793 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1794 sta->txrate = i;
1795 }
1796 sta->last_txrate = sta->txrate;
1797 /* For MODE_IEEE80211A mode cck rate are at end
1798 * rate table
1799 */
1800 if (local->hw.conf.phymode == MODE_IEEE80211A)
1801 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1802
1803 crl->is_dup = priv->is_dup;
1804 crl->valid_antenna = priv->valid_antenna;
1805 crl->antenna = priv->antenna;
1806 crl->is_green = rs_use_green(priv);
1807 crl->active_rate = priv->active_rate;
1808 crl->active_rate &= ~(0x1000);
1809 crl->active_rate_basic = priv->active_rate_basic;
1810 crl->phymode = priv->phymode;
1811 #ifdef CONFIG_IWLWIFI_HT
1812 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1813 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1814 crl->active_siso_rate &= ~((u16)0x2);
1815 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1816
1817 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1818 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1819 crl->active_mimo_rate &= ~((u16)0x2);
1820 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1821 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1822 crl->active_mimo_rate);
1823 #endif /*CONFIG_IWLWIFI_HT*/
1824 #ifdef CONFIG_MAC80211_DEBUGFS
1825 crl->drv = priv;
1826 #endif
1827
1828 if (priv->assoc_station_added)
1829 priv->lq_mngr.lq_ready = 1;
1830
1831 rs_initialize_lq(priv, sta);
1832 }
1833
1834 static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
1835 struct iwl_rate *tx_mcs,
1836 struct iwl_link_quality_cmd *lq_cmd)
1837 {
1838 int index = 0;
1839 int rate_idx;
1840 int repeat_rate = 0;
1841 u8 ant_toggle_count = 0;
1842 u8 use_ht_possible = 1;
1843 struct iwl_rate new_rate;
1844 struct iwl_scale_tbl_info tbl_type = { 0 };
1845
1846 rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
1847
1848 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1849 &tbl_type, &rate_idx);
1850
1851 if (is_legacy(tbl_type.lq_type)) {
1852 ant_toggle_count = 1;
1853 repeat_rate = IWL_NUMBER_TRY;
1854 } else
1855 repeat_rate = IWL_HT_NUMBER_TRY;
1856
1857 lq_cmd->general_params.mimo_delimiter =
1858 is_mimo(tbl_type.lq_type) ? 1 : 0;
1859 lq_cmd->rs_table[index].rate_n_flags =
1860 cpu_to_le32(tx_mcs->rate_n_flags);
1861 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1862
1863 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1864 lq_cmd->general_params.single_stream_ant_msk = 1;
1865 else
1866 lq_cmd->general_params.single_stream_ant_msk = 2;
1867
1868 index++;
1869 repeat_rate--;
1870
1871 while (index < LINK_QUAL_MAX_RETRY_NUM) {
1872 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1873 if (is_legacy(tbl_type.lq_type)) {
1874 if (ant_toggle_count <
1875 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1876 ant_toggle_count++;
1877 else {
1878 rs_toggle_antenna(&new_rate, &tbl_type);
1879 ant_toggle_count = 1;
1880 }
1881 }
1882
1883 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
1884 lq_cmd->rs_table[index].rate_n_flags =
1885 cpu_to_le32(new_rate.rate_n_flags);
1886 repeat_rate--;
1887 index++;
1888 }
1889
1890 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1891 &rate_idx);
1892
1893 if (is_mimo(tbl_type.lq_type))
1894 lq_cmd->general_params.mimo_delimiter = index;
1895
1896 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1897 use_ht_possible, &new_rate);
1898
1899 if (is_legacy(tbl_type.lq_type)) {
1900 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1901 ant_toggle_count++;
1902 else {
1903 rs_toggle_antenna(&new_rate, &tbl_type);
1904 ant_toggle_count = 1;
1905 }
1906 repeat_rate = IWL_NUMBER_TRY;
1907 } else
1908 repeat_rate = IWL_HT_NUMBER_TRY;
1909
1910 use_ht_possible = 0;
1911
1912 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
1913 lq_cmd->rs_table[index].rate_n_flags =
1914 cpu_to_le32(new_rate.rate_n_flags);
1915
1916 index++;
1917 repeat_rate--;
1918 }
1919
1920 lq_cmd->general_params.dual_stream_ant_msk = 3;
1921 lq_cmd->agg_params.agg_dis_start_th = 3;
1922 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1923 }
1924
1925 static void *rs_alloc(struct ieee80211_local *local)
1926 {
1927 return local->hw.priv;
1928 }
1929 /* rate scale requires free function to be implemented */
1930 static void rs_free(void *priv_rate)
1931 {
1932 return;
1933 }
1934
1935 static void rs_clear(void *priv_rate)
1936 {
1937 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1938
1939 IWL_DEBUG_RATE("enter\n");
1940
1941 priv->lq_mngr.lq_ready = 0;
1942 #ifdef CONFIG_IWLWIFI_HT
1943 #ifdef CONFIG_IWLWIFI_HT_AGG
1944 if (priv->lq_mngr.agg_ctrl.granted_ba)
1945 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
1946 #endif /*CONFIG_IWLWIFI_HT_AGG */
1947 #endif /* CONFIG_IWLWIFI_HT */
1948
1949 IWL_DEBUG_RATE("leave\n");
1950 }
1951
1952 static void rs_free_sta(void *priv, void *priv_sta)
1953 {
1954 struct iwl_rate_scale_priv *rs_priv = priv_sta;
1955
1956 IWL_DEBUG_RATE("enter\n");
1957 kfree(rs_priv);
1958 IWL_DEBUG_RATE("leave\n");
1959 }
1960
1961
1962 #ifdef CONFIG_MAC80211_DEBUGFS
1963 static int open_file_generic(struct inode *inode, struct file *file)
1964 {
1965 file->private_data = inode->i_private;
1966 return 0;
1967 }
1968 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
1969 struct iwl_rate *mcs, int index)
1970 {
1971 u32 base_rate;
1972
1973 if (rs_priv->phymode == (u8) MODE_IEEE80211A)
1974 base_rate = 0x800D;
1975 else
1976 base_rate = 0x820A;
1977
1978 if (rs_priv->dbg_fixed.rate_n_flags) {
1979 if (index < 12)
1980 mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
1981 else
1982 mcs->rate_n_flags = base_rate;
1983 IWL_DEBUG_RATE("Fixed rate ON\n");
1984 return;
1985 }
1986
1987 IWL_DEBUG_RATE("Fixed rate OFF\n");
1988 }
1989
1990 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
1991 const char __user *user_buf, size_t count, loff_t *ppos)
1992 {
1993 struct iwl_rate_scale_priv *rs_priv = file->private_data;
1994 char buf[64];
1995 int buf_size;
1996 u32 parsed_rate;
1997
1998 memset(buf, 0, sizeof(buf));
1999 buf_size = min(count, sizeof(buf) - 1);
2000 if (copy_from_user(buf, user_buf, buf_size))
2001 return -EFAULT;
2002
2003 if (sscanf(buf, "%x", &parsed_rate) == 1)
2004 rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
2005 else
2006 rs_priv->dbg_fixed.rate_n_flags = 0;
2007
2008 rs_priv->active_rate = 0x0FFF;
2009 rs_priv->active_siso_rate = 0x1FD0;
2010 rs_priv->active_mimo_rate = 0x1FD0;
2011
2012 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2013 rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
2014
2015 if (rs_priv->dbg_fixed.rate_n_flags) {
2016 rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
2017 rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
2018 }
2019
2020 return count;
2021 }
2022
2023 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2024 char __user *user_buf, size_t count, loff_t *ppos)
2025 {
2026 char buff[1024];
2027 int desc = 0;
2028 int i = 0;
2029
2030 struct iwl_rate_scale_priv *rs_priv = file->private_data;
2031
2032 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
2033 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2034 rs_priv->total_failed, rs_priv->total_success,
2035 rs_priv->active_rate);
2036 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2037 rs_priv->dbg_fixed.rate_n_flags);
2038 desc += sprintf(buff+desc, "general:"
2039 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2040 rs_priv->lq.general_params.flags,
2041 rs_priv->lq.general_params.mimo_delimiter,
2042 rs_priv->lq.general_params.single_stream_ant_msk,
2043 rs_priv->lq.general_params.dual_stream_ant_msk);
2044
2045 desc += sprintf(buff+desc, "agg:"
2046 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2047 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2048 rs_priv->lq.agg_params.agg_dis_start_th,
2049 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2050
2051 desc += sprintf(buff+desc,
2052 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2053 rs_priv->lq.general_params.start_rate_index[0],
2054 rs_priv->lq.general_params.start_rate_index[1],
2055 rs_priv->lq.general_params.start_rate_index[2],
2056 rs_priv->lq.general_params.start_rate_index[3]);
2057
2058
2059 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2060 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2061 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2062
2063 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2064 }
2065
2066 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2067 .write = rs_sta_dbgfs_scale_table_write,
2068 .read = rs_sta_dbgfs_scale_table_read,
2069 .open = open_file_generic,
2070 };
2071 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2072 char __user *user_buf, size_t count, loff_t *ppos)
2073 {
2074 char buff[1024];
2075 int desc = 0;
2076 int i, j;
2077
2078 struct iwl_rate_scale_priv *rs_priv = file->private_data;
2079 for (i = 0; i < LQ_SIZE; i++) {
2080 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2081 "rate=0x%X\n",
2082 rs_priv->active_tbl == i?"*":"x",
2083 rs_priv->lq_info[i].lq_type,
2084 rs_priv->lq_info[i].is_SGI,
2085 rs_priv->lq_info[i].is_fat,
2086 rs_priv->lq_info[i].is_dup,
2087 rs_priv->lq_info[i].current_rate.rate_n_flags);
2088 for (j = 0; j < IWL_RATE_COUNT; j++) {
2089 desc += sprintf(buff+desc,
2090 "counter=%d success=%d %%=%d\n",
2091 rs_priv->lq_info[i].win[j].counter,
2092 rs_priv->lq_info[i].win[j].success_counter,
2093 rs_priv->lq_info[i].win[j].success_ratio);
2094 }
2095 }
2096 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2097 }
2098
2099 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2100 .read = rs_sta_dbgfs_stats_table_read,
2101 .open = open_file_generic,
2102 };
2103
2104 static void rs_add_debugfs(void *priv, void *priv_sta,
2105 struct dentry *dir)
2106 {
2107 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2108 rs_priv->rs_sta_dbgfs_scale_table_file =
2109 debugfs_create_file("rate_scale_table", 0600, dir,
2110 rs_priv, &rs_sta_dbgfs_scale_table_ops);
2111 rs_priv->rs_sta_dbgfs_stats_table_file =
2112 debugfs_create_file("rate_stats_table", 0600, dir,
2113 rs_priv, &rs_sta_dbgfs_stats_table_ops);
2114 }
2115
2116 static void rs_remove_debugfs(void *priv, void *priv_sta)
2117 {
2118 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2119 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
2120 debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
2121 }
2122 #endif
2123
2124 static struct rate_control_ops rs_ops = {
2125 .module = NULL,
2126 .name = RS_NAME,
2127 .tx_status = rs_tx_status,
2128 .get_rate = rs_get_rate,
2129 .rate_init = rs_rate_init,
2130 .clear = rs_clear,
2131 .alloc = rs_alloc,
2132 .free = rs_free,
2133 .alloc_sta = rs_alloc_sta,
2134 .free_sta = rs_free_sta,
2135 #ifdef CONFIG_MAC80211_DEBUGFS
2136 .add_sta_debugfs = rs_add_debugfs,
2137 .remove_sta_debugfs = rs_remove_debugfs,
2138 #endif
2139 };
2140
2141 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2142 {
2143 struct ieee80211_local *local = hw_to_local(hw);
2144 struct iwl_priv *priv = hw->priv;
2145 struct iwl_rate_scale_priv *rs_priv;
2146 struct sta_info *sta;
2147 int count = 0, i;
2148 u32 samples = 0, success = 0, good = 0;
2149 unsigned long now = jiffies;
2150 u32 max_time = 0;
2151 u8 lq_type, antenna;
2152
2153 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2154 if (!sta || !sta->rate_ctrl_priv) {
2155 if (sta) {
2156 sta_info_put(sta);
2157 IWL_DEBUG_RATE("leave - no private rate data!\n");
2158 } else
2159 IWL_DEBUG_RATE("leave - no station!\n");
2160 return sprintf(buf, "station %d not found\n", sta_id);
2161 }
2162
2163 rs_priv = (void *)sta->rate_ctrl_priv;
2164
2165 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2166 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2167
2168 if (is_legacy(lq_type))
2169 i = IWL_RATE_54M_INDEX;
2170 else
2171 i = IWL_RATE_60M_INDEX;
2172 while (1) {
2173 u64 mask;
2174 int j;
2175 int active = rs_priv->active_tbl;
2176
2177 count +=
2178 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2179
2180 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2181 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2182 buf[count++] =
2183 (rs_priv->lq_info[active].win[i].data & mask)
2184 ? '1' : '0';
2185
2186 samples += rs_priv->lq_info[active].win[i].counter;
2187 good += rs_priv->lq_info[active].win[i].success_counter;
2188 success += rs_priv->lq_info[active].win[i].success_counter *
2189 iwl_rates[i].ieee;
2190
2191 if (rs_priv->lq_info[active].win[i].stamp) {
2192 int delta =
2193 jiffies_to_msecs(now -
2194 rs_priv->lq_info[active].win[i].stamp);
2195
2196 if (delta > max_time)
2197 max_time = delta;
2198
2199 count += sprintf(&buf[count], "%5dms\n", delta);
2200 } else
2201 buf[count++] = '\n';
2202
2203 j = iwl_get_prev_ieee_rate(i);
2204 if (j == i)
2205 break;
2206 i = j;
2207 }
2208
2209 /* Display the average rate of all samples taken.
2210 *
2211 * NOTE: We multiple # of samples by 2 since the IEEE measurement
2212 * added from iwl_rates is actually 2X the rate */
2213 if (samples)
2214 count += sprintf(&buf[count],
2215 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2216 "%3d%% success (%d good packets over %d tries)\n",
2217 success / (2 * samples), (success * 5 / samples) % 10,
2218 max_time, good * 100 / samples, good, samples);
2219 else
2220 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2221 count += sprintf(&buf[count], "\nrate scale type %d antenna %d "
2222 "active_search %d rate index %d\n", lq_type, antenna,
2223 rs_priv->search_better_tbl, sta->last_txrate);
2224
2225 sta_info_put(sta);
2226 return count;
2227 }
2228
2229 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2230 {
2231 struct iwl_priv *priv = hw->priv;
2232
2233 priv->lq_mngr.lq_ready = 1;
2234 }
2235
2236 void iwl_rate_control_register(struct ieee80211_hw *hw)
2237 {
2238 ieee80211_rate_control_register(&rs_ops);
2239 }
2240
2241 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2242 {
2243 ieee80211_rate_control_unregister(&rs_ops);
2244 }
2245