[IWLWIFI]: add iwlwifi wireless drivers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-4965-rs.c
blobf3638607d641310c2d23027e9e8f688b49efeec1
1 /******************************************************************************
3 * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved.
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.
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.
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
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
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
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>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
37 #include <linux/workqueue.h>
39 #include <net/mac80211.h>
40 #include <linux/wireless.h>
42 #include "../net/mac80211/ieee80211_rate.h"
44 #include "iwlwifi.h"
45 #include "iwl-helpers.h"
47 #define RS_NAME "iwl-4965-rs"
49 #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
50 #define IWL_NUMBER_TRY 1
51 #define IWL_HT_NUMBER_TRY 3
53 #define IWL_RATE_MAX_WINDOW 62
54 #define IWL_RATE_HIGH_TH 10880
55 #define IWL_RATE_MIN_FAILURE_TH 6
56 #define IWL_RATE_MIN_SUCCESS_TH 8
57 #define IWL_RATE_DECREASE_TH 1920
58 #define IWL_RATE_INCREASE_TH 8960
59 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ) /*2 seconds */
61 static u8 rs_ht_to_legacy[] = {
62 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
63 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
64 IWL_RATE_6M_INDEX,
65 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
66 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
67 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
68 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
71 struct iwl_rate {
72 u32 rate_n_flags;
73 } __attribute__ ((packed));
75 struct iwl_rate_scale_data {
76 u64 data;
77 s32 success_counter;
78 s32 success_ratio;
79 s32 counter;
80 s32 average_tpt;
81 unsigned long stamp;
84 struct iwl_scale_tbl_info {
85 enum iwl_table_type lq_type;
86 enum iwl_antenna_type antenna_type;
87 u8 is_SGI;
88 u8 is_fat;
89 u8 is_dup;
90 u8 action;
91 s32 *expected_tpt;
92 struct iwl_rate current_rate;
93 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
96 struct iwl_rate_scale_priv {
97 u8 active_tbl;
98 u8 enable_counter;
99 u8 stay_in_tbl;
100 u8 search_better_tbl;
101 s32 last_tpt;
102 u32 table_count_limit;
103 u32 max_failure_limit;
104 u32 max_success_limit;
105 u32 table_count;
106 u32 total_failed;
107 u32 total_success;
108 u32 flush_timer;
109 u8 action_counter;
110 u8 antenna;
111 u8 valid_antenna;
112 u8 is_green;
113 u8 is_dup;
114 u8 phymode;
115 u8 ibss_sta_added;
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];
124 static void rs_rate_scale_perform(struct iwl_priv *priv,
125 struct net_device *dev,
126 struct ieee80211_hdr *hdr,
127 struct sta_info *sta);
128 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
129 struct iwl_rate *tx_mcs,
130 struct iwl_link_quality_cmd *tbl,
131 struct sta_info *sta);
134 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
135 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
138 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
139 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
142 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
143 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
146 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
147 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
150 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
151 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
154 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
155 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
158 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
159 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
162 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
163 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
166 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
167 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
170 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
171 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
174 static int iwl_lq_sync_callback(struct iwl_priv *priv,
175 struct iwl_cmd *cmd, struct sk_buff *skb)
177 /*We didn't cache the SKB; let the caller free it */
178 return 1;
181 static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
183 return (u8)(rate_n_flags & 0xFF);
186 static int rs_send_lq_cmd(struct iwl_priv *priv,
187 struct iwl_link_quality_cmd *lq, u8 flags)
189 #ifdef CONFIG_IWLWIFI_DEBUG
190 int i;
191 #endif
192 int rc = -1;
194 struct iwl_host_cmd cmd = {
195 .id = REPLY_TX_LINK_QUALITY_CMD,
196 .len = sizeof(struct iwl_link_quality_cmd),
197 .meta.flags = flags,
198 .data = lq,
201 if ((lq->sta_id == 0xFF) &&
202 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
203 return rc;
205 if (lq->sta_id == 0xFF)
206 lq->sta_id = IWL_AP_ID;
208 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
209 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
210 lq->general_params.single_stream_ant_msk,
211 lq->general_params.dual_stream_ant_msk);
212 #ifdef CONFIG_IWLWIFI_DEBUG
213 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
214 IWL_DEBUG_RATE("lq index %d 0x%X\n",
215 i, lq->rs_table[i].rate_n_flags);
216 #endif
218 if (flags & CMD_ASYNC)
219 cmd.meta.u.callback = iwl_lq_sync_callback;
221 if (iwl_is_associated(priv) && priv->assoc_station_added &&
222 priv->lq_mngr.lq_ready)
223 rc = iwl_send_cmd(priv, &cmd);
225 return rc;
228 static int rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
230 window->data = 0;
231 window->success_counter = 0;
232 window->success_ratio = IWL_INVALID_VALUE;
233 window->counter = 0;
234 window->average_tpt = IWL_INVALID_VALUE;
235 window->stamp = 0;
237 return 0;
240 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
241 int scale_index, s32 tpt, u32 status)
243 int rc = 0;
244 struct iwl_rate_scale_data *window = NULL;
245 u64 mask;
246 u8 win_size = IWL_RATE_MAX_WINDOW;
247 s32 fail_count;
249 if (scale_index < 0)
250 return -1;
252 if (scale_index >= IWL_RATE_COUNT)
253 return -1;
255 window = &(windows[scale_index]);
257 if (window->counter >= win_size) {
259 window->counter = win_size - 1;
260 mask = 1;
261 mask = (mask << (win_size - 1));
262 if ((window->data & mask)) {
263 window->data &= ~mask;
264 window->success_counter = window->success_counter - 1;
268 window->counter = window->counter + 1;
269 mask = window->data;
270 window->data = (mask << 1);
271 if (status != 0) {
272 window->success_counter = window->success_counter + 1;
273 window->data |= 0x1;
276 if (window->counter > 0)
277 window->success_ratio = 128 * (100 * window->success_counter)
278 / window->counter;
279 else
280 window->success_ratio = IWL_INVALID_VALUE;
282 fail_count = window->counter - window->success_counter;
284 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
285 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
286 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
287 else
288 window->average_tpt = IWL_INVALID_VALUE;
290 window->stamp = jiffies;
292 return rc;
295 int static rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
296 struct iwl_scale_tbl_info *tbl,
297 int index, u8 use_green)
299 int rc = 0;
301 if (is_legacy(tbl->lq_type)) {
302 mcs_rate->rate_n_flags = iwl_rates[index].plcp;
303 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
304 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
306 } else if (is_siso(tbl->lq_type)) {
307 if (index > IWL_LAST_OFDM_RATE)
308 index = IWL_LAST_OFDM_RATE;
309 mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
310 RATE_MCS_HT_MSK;
311 } else {
312 if (index > IWL_LAST_OFDM_RATE)
313 index = IWL_LAST_OFDM_RATE;
314 mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
315 RATE_MCS_HT_MSK;
318 switch (tbl->antenna_type) {
319 case ANT_BOTH:
320 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
321 break;
322 case ANT_MAIN:
323 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
324 break;
325 case ANT_AUX:
326 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
327 break;
328 case ANT_NONE:
329 break;
332 if (is_legacy(tbl->lq_type))
333 return rc;
335 if (tbl->is_fat) {
336 if (tbl->is_dup)
337 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
338 else
339 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
341 if (tbl->is_SGI)
342 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
344 if (use_green) {
345 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
346 if (is_siso(tbl->lq_type))
347 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
349 return rc;
352 static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
353 int phymode, struct iwl_scale_tbl_info *tbl,
354 int *rate_idx)
356 int index;
357 u32 ant_msk;
359 index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
361 if (index == IWL_RATE_INVALID) {
362 *rate_idx = -1;
363 return -1;
365 tbl->is_SGI = 0;
366 tbl->is_fat = 0;
367 tbl->is_dup = 0;
368 tbl->antenna_type = ANT_BOTH;
370 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
371 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
373 if (ant_msk == RATE_MCS_ANT_AB_MSK)
374 tbl->lq_type = LQ_NONE;
375 else {
377 if (phymode == MODE_IEEE80211A)
378 tbl->lq_type = LQ_A;
379 else
380 tbl->lq_type = LQ_G;
382 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
383 tbl->antenna_type = ANT_MAIN;
384 else
385 tbl->antenna_type = ANT_AUX;
387 *rate_idx = index;
389 } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
390 <= IWL_RATE_SISO_60M_PLCP) {
391 tbl->lq_type = LQ_SISO;
393 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
394 if (ant_msk == RATE_MCS_ANT_AB_MSK)
395 tbl->lq_type = LQ_NONE;
396 else {
397 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
398 tbl->antenna_type = ANT_MAIN;
399 else
400 tbl->antenna_type = ANT_AUX;
402 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
403 tbl->is_SGI = 1;
405 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
406 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
407 tbl->is_fat = 1;
409 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
410 tbl->is_dup = 1;
412 *rate_idx = index;
413 } else {
414 tbl->lq_type = LQ_MIMO;
415 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
416 tbl->is_SGI = 1;
418 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
419 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
420 tbl->is_fat = 1;
422 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
423 tbl->is_dup = 1;
424 *rate_idx = index;
426 return 0;
429 static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
430 struct iwl_scale_tbl_info *tbl)
432 if (tbl->antenna_type == ANT_AUX) {
433 tbl->antenna_type = ANT_MAIN;
434 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
435 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
436 } else {
437 tbl->antenna_type = ANT_AUX;
438 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
439 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
443 static inline s8 rs_use_green(struct iwl_priv *priv)
445 s8 rc = 0;
446 #ifdef CONFIG_IWLWIFI_HT
447 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
448 return 0;
450 if ((priv->current_assoc_ht.is_green_field) &&
451 !(priv->current_assoc_ht.operating_mode & 0x4))
452 rc = 1;
453 #endif /*CONFIG_IWLWIFI_HT */
454 return rc;
458 * rs_get_supported_rates - get the available rates
460 * if management frame or broadcast frame only return
461 * basic available rates.
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)
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;
478 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
479 lq_data->active_rate_basic)
480 *data_rate = lq_data->active_rate_basic;
483 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
485 u8 high = IWL_RATE_INVALID;
486 u8 low = IWL_RATE_INVALID;
488 /* 802.11A or ht walks to the next literal adjascent rate in
489 * the rate table */
490 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
491 int i;
492 u32 mask;
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;
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;
512 return (high << 8) | low;
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);
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);
535 return (high << 8) | low;
538 static int 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 struct sta_info *sta)
543 u8 is_green = lq_data->is_green;
544 s32 low;
545 u16 rate_mask;
546 u16 high_low;
547 u8 switch_to_legacy = 0;
549 /* check if we need to switch from HT to legacy rates.
550 * assumption is that mandatory rates (1Mbps or 6Mbps)
551 * are always supported (spec demand) */
552 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
553 switch_to_legacy = 1;
554 scale_index = rs_ht_to_legacy[scale_index];
555 if (lq_data->phymode == MODE_IEEE80211A)
556 tbl->lq_type = LQ_A;
557 else
558 tbl->lq_type = LQ_G;
560 if ((tbl->antenna_type == ANT_BOTH) ||
561 (tbl->antenna_type == ANT_NONE))
562 tbl->antenna_type = ANT_MAIN;
564 tbl->is_fat = 0;
565 tbl->is_SGI = 0;
568 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
570 /* mask with station rate restriction */
571 if (is_legacy(tbl->lq_type)) {
572 if (lq_data->phymode == (u8) MODE_IEEE80211A)
573 rate_mask = (u16)(rate_mask &
574 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
575 else
576 rate_mask = (u16)(rate_mask & sta->supp_rates);
579 /* if we did switched from HT to legacy check current rate */
580 if ((switch_to_legacy) &&
581 (rate_mask & (1 << scale_index))) {
582 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
583 return 0;
586 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
587 low = high_low & 0xff;
589 if (low != IWL_RATE_INVALID)
590 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
591 else
592 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
594 return 0;
597 static void rs_tx_status(void *priv_rate,
598 struct net_device *dev,
599 struct sk_buff *skb,
600 struct ieee80211_tx_status *tx_resp)
602 int status;
603 u8 retries;
604 int rs_index, index = 0;
605 struct iwl_rate_scale_priv *lq;
606 struct iwl_link_quality_cmd *table;
607 struct sta_info *sta;
608 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
609 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
611 struct iwl_rate_scale_data *window = NULL;
612 struct iwl_rate_scale_data *search_win = NULL;
613 struct iwl_rate tx_mcs;
614 struct iwl_scale_tbl_info tbl_type;
615 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
616 u8 active_index = 0;
617 u16 fc = le16_to_cpu(hdr->frame_control);
618 s32 tpt = 0;
620 IWL_DEBUG_RATE("get frame ack response, update rate scale window\n");
622 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
623 return;
625 retries = tx_resp->retry_count;
627 if (retries > 15)
628 retries = 15;
631 sta = sta_info_get(local, hdr->addr1);
633 if (!sta || !sta->rate_ctrl_priv) {
634 if (sta)
635 sta_info_put(sta);
636 return;
639 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
641 if (!priv->lq_mngr.lq_ready)
642 return;
644 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
645 return;
647 table = &lq->lq;
648 active_index = lq->active_tbl;
650 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
651 if (!lq->antenna)
652 lq->antenna = lq->valid_antenna;
654 lq->antenna = lq->valid_antenna;
655 curr_tbl = &(lq->lq_info[active_index]);
656 search_tbl = &(lq->lq_info[(1 - active_index)]);
657 window = (struct iwl_rate_scale_data *)
658 &(curr_tbl->win[0]);
659 search_win = (struct iwl_rate_scale_data *)
660 &(search_tbl->win[0]);
662 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
664 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
665 &tbl_type, &rs_index);
666 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
667 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
668 rs_index, tx_mcs.rate_n_flags);
669 sta_info_put(sta);
670 return;
673 if (retries &&
674 (tx_mcs.rate_n_flags !=
675 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
676 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
677 tx_mcs.rate_n_flags,
678 le32_to_cpu(table->rs_table[0].rate_n_flags));
679 sta_info_put(sta);
680 return;
683 while (retries) {
684 tx_mcs.rate_n_flags =
685 le32_to_cpu(table->rs_table[index].rate_n_flags);
686 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
687 &tbl_type, &rs_index);
689 if ((tbl_type.lq_type == search_tbl->lq_type) &&
690 (tbl_type.antenna_type == search_tbl->antenna_type) &&
691 (tbl_type.is_SGI == search_tbl->is_SGI)) {
692 if (search_tbl->expected_tpt)
693 tpt = search_tbl->expected_tpt[rs_index];
694 else
695 tpt = 0;
696 rs_collect_tx_data(search_win,
697 rs_index, tpt, 0);
698 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
699 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
700 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
701 if (curr_tbl->expected_tpt)
702 tpt = curr_tbl->expected_tpt[rs_index];
703 else
704 tpt = 0;
705 rs_collect_tx_data(window, rs_index, tpt, 0);
707 if (lq->stay_in_tbl)
708 lq->total_failed++;
709 --retries;
710 index++;
714 if (!tx_resp->retry_count)
715 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
716 else
717 tx_mcs.rate_n_flags =
718 le32_to_cpu(table->rs_table[index].rate_n_flags);
720 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
721 &tbl_type, &rs_index);
723 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
724 status = 1;
725 else
726 status = 0;
728 if ((tbl_type.lq_type == search_tbl->lq_type) &&
729 (tbl_type.antenna_type == search_tbl->antenna_type) &&
730 (tbl_type.is_SGI == search_tbl->is_SGI)) {
731 if (search_tbl->expected_tpt)
732 tpt = search_tbl->expected_tpt[rs_index];
733 else
734 tpt = 0;
735 rs_collect_tx_data(search_win,
736 rs_index, tpt, status);
737 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
738 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
739 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
740 if (curr_tbl->expected_tpt)
741 tpt = curr_tbl->expected_tpt[rs_index];
742 else
743 tpt = 0;
744 rs_collect_tx_data(window, rs_index, tpt, status);
747 if (lq->stay_in_tbl) {
748 if (status)
749 lq->total_success++;
750 else
751 lq->total_failed++;
754 rs_rate_scale_perform(priv, dev, hdr, sta);
755 sta_info_put(sta);
756 return;
759 static u8 rs_is_ant_connected(u8 valid_antenna,
760 enum iwl_antenna_type antenna_type)
762 if (antenna_type == ANT_AUX)
763 return ((valid_antenna & 0x2) ? 1:0);
764 else if (antenna_type == ANT_MAIN)
765 return ((valid_antenna & 0x1) ? 1:0);
766 else if (antenna_type == ANT_BOTH) {
767 if ((valid_antenna & 0x3) == 0x3)
768 return 1;
769 else
770 return 0;
773 return 1;
776 static u8 rs_is_other_ant_connected(u8 valid_antenna,
777 enum iwl_antenna_type antenna_type)
779 if (antenna_type == ANT_AUX)
780 return (rs_is_ant_connected(valid_antenna, ANT_MAIN));
781 else
782 return (rs_is_ant_connected(valid_antenna, ANT_AUX));
784 return 0;
787 static void rs_set_stay_in_table(u8 is_legacy,
788 struct iwl_rate_scale_priv *lq_data)
790 IWL_DEBUG_HT("we are staying in the same table\n");
791 lq_data->stay_in_tbl = 1;
792 if (is_legacy) {
793 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
794 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
795 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
796 } else {
797 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
798 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
799 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
801 lq_data->table_count = 0;
802 lq_data->total_failed = 0;
803 lq_data->total_success = 0;
806 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
807 struct iwl_scale_tbl_info *tbl)
809 if (is_legacy(tbl->lq_type)) {
810 if (!is_a_band(tbl->lq_type))
811 tbl->expected_tpt = expected_tpt_G;
812 else
813 tbl->expected_tpt = expected_tpt_A;
814 } else if (is_siso(tbl->lq_type)) {
815 if (tbl->is_fat && !lq_data->is_dup)
816 if (tbl->is_SGI)
817 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
818 else
819 tbl->expected_tpt = expected_tpt_siso40MHz;
820 else if (tbl->is_SGI)
821 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
822 else
823 tbl->expected_tpt = expected_tpt_siso20MHz;
825 } else if (is_mimo(tbl->lq_type)) {
826 if (tbl->is_fat && !lq_data->is_dup)
827 if (tbl->is_SGI)
828 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
829 else
830 tbl->expected_tpt = expected_tpt_mimo40MHz;
831 else if (tbl->is_SGI)
832 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
833 else
834 tbl->expected_tpt = expected_tpt_mimo20MHz;
835 } else
836 tbl->expected_tpt = expected_tpt_G;
839 #ifdef CONFIG_IWLWIFI_HT
840 static s32 rs_get_best_rate(struct iwl_priv *priv,
841 struct iwl_rate_scale_priv *lq_data,
842 struct iwl_scale_tbl_info *tbl,
843 u16 rate_mask, s8 index, s8 rate)
845 struct iwl_scale_tbl_info *active_tbl =
846 &(lq_data->lq_info[lq_data->active_tbl]);
847 s32 new_rate, high, low, start_hi;
848 s32 active_sr = active_tbl->win[index].success_ratio;
849 s32 *tpt_tbl = tbl->expected_tpt;
850 s32 active_tpt = active_tbl->expected_tpt[index];
851 u16 high_low;
853 new_rate = high = low = start_hi = IWL_RATE_INVALID;
855 for (; ;) {
856 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
858 low = high_low & 0xff;
859 high = (high_low >> 8) & 0xff;
861 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
862 ((active_sr > IWL_RATE_DECREASE_TH) &&
863 (active_sr <= IWL_RATE_HIGH_TH) &&
864 (tpt_tbl[rate] <= active_tpt))) ||
865 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
866 (tpt_tbl[rate] > active_tpt))) {
868 if (start_hi != IWL_RATE_INVALID) {
869 new_rate = start_hi;
870 break;
872 new_rate = rate;
873 if (low != IWL_RATE_INVALID)
874 rate = low;
875 else
876 break;
877 } else {
878 if (new_rate != IWL_RATE_INVALID)
879 break;
880 else if (high != IWL_RATE_INVALID) {
881 start_hi = high;
882 rate = high;
883 } else {
884 new_rate = rate;
885 break;
890 return new_rate;
892 #endif /* CONFIG_IWLWIFI_HT */
894 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
896 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
899 static int rs_switch_to_mimo(struct iwl_priv *priv,
900 struct iwl_rate_scale_priv *lq_data,
901 struct iwl_scale_tbl_info *tbl, int index)
903 int rc = -1;
904 #ifdef CONFIG_IWLWIFI_HT
905 u16 rate_mask;
906 s32 rate;
907 s8 is_green = lq_data->is_green;
909 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
910 return -1;
912 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
913 tbl->lq_type = LQ_MIMO;
914 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
915 &rate_mask);
917 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
918 return -1;
920 if (!rs_is_both_ant_supp(lq_data->antenna))
921 return -1;
923 rc = 0;
924 tbl->is_dup = lq_data->is_dup;
925 tbl->action = 0;
926 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
927 tbl->is_fat = 1;
928 else
929 tbl->is_fat = 0;
931 if (tbl->is_fat) {
932 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
933 tbl->is_SGI = 1;
934 else
935 tbl->is_SGI = 0;
936 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
937 tbl->is_SGI = 1;
938 else
939 tbl->is_SGI = 0;
941 rs_get_expected_tpt_table(lq_data, tbl);
943 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
945 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
946 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
947 return -1;
948 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
950 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
951 tbl->current_rate.rate_n_flags, is_green);
953 #endif /*CONFIG_IWLWIFI_HT */
954 return rc;
957 static int rs_switch_to_siso(struct iwl_priv *priv,
958 struct iwl_rate_scale_priv *lq_data,
959 struct iwl_scale_tbl_info *tbl, int index)
961 int rc = -1;
962 #ifdef CONFIG_IWLWIFI_HT
963 u16 rate_mask;
964 u8 is_green = lq_data->is_green;
965 s32 rate;
967 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
968 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
969 return -1;
971 rc = 0;
972 tbl->is_dup = lq_data->is_dup;
973 tbl->lq_type = LQ_SISO;
974 tbl->action = 0;
975 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
976 &rate_mask);
978 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
979 tbl->is_fat = 1;
980 else
981 tbl->is_fat = 0;
983 if (tbl->is_fat) {
984 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
985 tbl->is_SGI = 1;
986 else
987 tbl->is_SGI = 0;
988 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
989 tbl->is_SGI = 1;
990 else
991 tbl->is_SGI = 0;
993 if (is_green)
994 tbl->is_SGI = 0;
996 rs_get_expected_tpt_table(lq_data, tbl);
997 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
999 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1000 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1001 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1002 rate, rate_mask);
1003 return -1;
1005 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1006 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1007 tbl->current_rate.rate_n_flags, is_green);
1009 #endif /*CONFIG_IWLWIFI_HT */
1010 return rc;
1013 static int rs_move_legacy_other(struct iwl_priv *priv,
1014 struct iwl_rate_scale_priv *lq_data,
1015 int index)
1017 int rc = 0;
1018 struct iwl_scale_tbl_info *tbl =
1019 &(lq_data->lq_info[lq_data->active_tbl]);
1020 struct iwl_scale_tbl_info *search_tbl =
1021 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1022 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1023 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1024 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1025 u8 start_action = tbl->action;
1027 for (; ;) {
1028 switch (tbl->action) {
1029 case IWL_LEGACY_SWITCH_ANTENNA:
1030 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1032 search_tbl->lq_type = LQ_NONE;
1033 lq_data->action_counter++;
1034 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1035 break;
1036 if (!rs_is_other_ant_connected(lq_data->antenna,
1037 tbl->antenna_type))
1038 break;
1040 memcpy(search_tbl, tbl, sz);
1042 rs_toggle_antenna(&(search_tbl->current_rate),
1043 search_tbl);
1044 rs_get_expected_tpt_table(lq_data, search_tbl);
1045 lq_data->search_better_tbl = 1;
1046 goto out;
1048 case IWL_LEGACY_SWITCH_SISO:
1049 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1050 memcpy(search_tbl, tbl, sz);
1051 search_tbl->lq_type = LQ_SISO;
1052 search_tbl->is_SGI = 0;
1053 search_tbl->is_fat = 0;
1054 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1055 index);
1056 if (!rc) {
1057 lq_data->search_better_tbl = 1;
1058 lq_data->action_counter = 0;
1060 if (!rc)
1061 goto out;
1063 break;
1064 case IWL_LEGACY_SWITCH_MIMO:
1065 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1066 memcpy(search_tbl, tbl, sz);
1067 search_tbl->lq_type = LQ_MIMO;
1068 search_tbl->is_SGI = 0;
1069 search_tbl->is_fat = 0;
1070 search_tbl->antenna_type = ANT_BOTH;
1071 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1072 index);
1073 if (!rc) {
1074 lq_data->search_better_tbl = 1;
1075 lq_data->action_counter = 0;
1077 if (!rc)
1078 goto out;
1079 break;
1081 tbl->action++;
1082 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1083 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1085 if (tbl->action == start_action)
1086 break;
1089 return 0;
1091 out:
1092 tbl->action++;
1093 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1094 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1095 return 0;
1099 static int rs_move_siso_to_other(struct iwl_priv *priv,
1100 struct iwl_rate_scale_priv *lq_data,
1101 int index)
1103 int rc = -1;
1104 u8 is_green = lq_data->is_green;
1105 struct iwl_scale_tbl_info *tbl =
1106 &(lq_data->lq_info[lq_data->active_tbl]);
1107 struct iwl_scale_tbl_info *search_tbl =
1108 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1109 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1110 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1111 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1112 u8 start_action = tbl->action;
1114 for (;;) {
1115 lq_data->action_counter++;
1116 switch (tbl->action) {
1117 case IWL_SISO_SWITCH_ANTENNA:
1118 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1119 search_tbl->lq_type = LQ_NONE;
1120 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1121 break;
1122 if (!rs_is_other_ant_connected(lq_data->antenna,
1123 tbl->antenna_type))
1124 break;
1126 memcpy(search_tbl, tbl, sz);
1127 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1128 rs_toggle_antenna(&(search_tbl->current_rate),
1129 search_tbl);
1130 lq_data->search_better_tbl = 1;
1132 goto out;
1134 case IWL_SISO_SWITCH_MIMO:
1135 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1136 memcpy(search_tbl, tbl, sz);
1137 search_tbl->lq_type = LQ_MIMO;
1138 search_tbl->is_SGI = 0;
1139 search_tbl->is_fat = 0;
1140 search_tbl->antenna_type = ANT_BOTH;
1141 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1142 index);
1143 if (!rc)
1144 lq_data->search_better_tbl = 1;
1146 if (!rc)
1147 goto out;
1148 break;
1149 case IWL_SISO_SWITCH_GI:
1150 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1151 memcpy(search_tbl, tbl, sz);
1152 search_tbl->action = 0;
1153 if (search_tbl->is_SGI)
1154 search_tbl->is_SGI = 0;
1155 else if (!is_green)
1156 search_tbl->is_SGI = 1;
1157 else
1158 break;
1159 lq_data->search_better_tbl = 1;
1160 if ((tbl->lq_type == LQ_SISO) &&
1161 (tbl->is_SGI)) {
1162 s32 tpt = lq_data->last_tpt / 100;
1163 if (((!tbl->is_fat) &&
1164 (tpt >= expected_tpt_siso20MHz[index])) ||
1165 ((tbl->is_fat) &&
1166 (tpt >= expected_tpt_siso40MHz[index])))
1167 lq_data->search_better_tbl = 0;
1169 rs_get_expected_tpt_table(lq_data, search_tbl);
1170 rs_mcs_from_tbl(&search_tbl->current_rate,
1171 search_tbl, index, is_green);
1172 goto out;
1174 tbl->action++;
1175 if (tbl->action > IWL_SISO_SWITCH_GI)
1176 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1178 if (tbl->action == start_action)
1179 break;
1181 return 0;
1183 out:
1184 tbl->action++;
1185 if (tbl->action > IWL_SISO_SWITCH_GI)
1186 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1187 return 0;
1190 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1191 struct iwl_rate_scale_priv *lq_data,
1192 int index)
1194 int rc = -1;
1195 s8 is_green = lq_data->is_green;
1196 struct iwl_scale_tbl_info *tbl =
1197 &(lq_data->lq_info[lq_data->active_tbl]);
1198 struct iwl_scale_tbl_info *search_tbl =
1199 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1200 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1201 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1202 u8 start_action = tbl->action;
1204 for (;;) {
1205 lq_data->action_counter++;
1206 switch (tbl->action) {
1207 case IWL_MIMO_SWITCH_ANTENNA_A:
1208 case IWL_MIMO_SWITCH_ANTENNA_B:
1209 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1210 memcpy(search_tbl, tbl, sz);
1211 search_tbl->lq_type = LQ_SISO;
1212 search_tbl->is_SGI = 0;
1213 search_tbl->is_fat = 0;
1214 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1215 search_tbl->antenna_type = ANT_MAIN;
1216 else
1217 search_tbl->antenna_type = ANT_AUX;
1219 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1220 index);
1221 if (!rc) {
1222 lq_data->search_better_tbl = 1;
1223 goto out;
1225 break;
1227 case IWL_MIMO_SWITCH_GI:
1228 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1229 memcpy(search_tbl, tbl, sz);
1230 search_tbl->lq_type = LQ_MIMO;
1231 search_tbl->antenna_type = ANT_BOTH;
1232 search_tbl->action = 0;
1233 if (search_tbl->is_SGI)
1234 search_tbl->is_SGI = 0;
1235 else
1236 search_tbl->is_SGI = 1;
1237 lq_data->search_better_tbl = 1;
1238 if ((tbl->lq_type == LQ_MIMO) &&
1239 (tbl->is_SGI)) {
1240 s32 tpt = lq_data->last_tpt / 100;
1241 if (((!tbl->is_fat) &&
1242 (tpt >= expected_tpt_mimo20MHz[index])) ||
1243 ((tbl->is_fat) &&
1244 (tpt >= expected_tpt_mimo40MHz[index])))
1245 lq_data->search_better_tbl = 0;
1247 rs_get_expected_tpt_table(lq_data, search_tbl);
1248 rs_mcs_from_tbl(&search_tbl->current_rate,
1249 search_tbl, index, is_green);
1250 goto out;
1253 tbl->action++;
1254 if (tbl->action > IWL_MIMO_SWITCH_GI)
1255 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1257 if (tbl->action == start_action)
1258 break;
1261 return 0;
1262 out:
1263 tbl->action++;
1264 if (tbl->action > IWL_MIMO_SWITCH_GI)
1265 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1266 return 0;
1270 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1272 struct iwl_scale_tbl_info *tbl;
1273 int i;
1274 int active_tbl;
1275 int flush_interval_passed = 0;
1277 active_tbl = lq_data->active_tbl;
1279 tbl = &(lq_data->lq_info[active_tbl]);
1281 if (lq_data->stay_in_tbl) {
1283 if (lq_data->flush_timer)
1284 flush_interval_passed =
1285 time_after(jiffies,
1286 (unsigned long)(lq_data->flush_timer +
1287 IWL_RATE_SCALE_FLUSH_INTVL));
1289 flush_interval_passed = 0;
1290 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1291 (lq_data->total_success > lq_data->max_success_limit) ||
1292 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1293 && (flush_interval_passed))) {
1294 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1295 lq_data->total_failed,
1296 lq_data->total_success,
1297 flush_interval_passed);
1298 lq_data->stay_in_tbl = 0;
1299 lq_data->total_failed = 0;
1300 lq_data->total_success = 0;
1301 lq_data->flush_timer = 0;
1302 } else if (lq_data->table_count > 0) {
1303 lq_data->table_count++;
1304 if (lq_data->table_count >=
1305 lq_data->table_count_limit) {
1306 lq_data->table_count = 0;
1308 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1309 for (i = 0; i < IWL_RATE_COUNT; i++)
1310 rs_rate_scale_clear_window(
1311 &(tbl->win[i]));
1315 if (!lq_data->stay_in_tbl) {
1316 for (i = 0; i < IWL_RATE_COUNT; i++)
1317 rs_rate_scale_clear_window(&(tbl->win[i]));
1322 static void rs_rate_scale_perform(struct iwl_priv *priv,
1323 struct net_device *dev,
1324 struct ieee80211_hdr *hdr,
1325 struct sta_info *sta)
1327 int low = IWL_RATE_INVALID;
1328 int high = IWL_RATE_INVALID;
1329 int index;
1330 int i;
1331 struct iwl_rate_scale_data *window = NULL;
1332 int current_tpt = IWL_INVALID_VALUE;
1333 int low_tpt = IWL_INVALID_VALUE;
1334 int high_tpt = IWL_INVALID_VALUE;
1335 u32 fail_count;
1336 s8 scale_action = 0;
1337 u16 fc, rate_mask;
1338 u8 update_lq = 0;
1339 struct iwl_rate_scale_priv *lq_data;
1340 struct iwl_scale_tbl_info *tbl, *tbl1;
1341 u16 rate_scale_index_msk = 0;
1342 struct iwl_rate mcs_rate;
1343 u8 is_green = 0;
1344 u8 active_tbl = 0;
1345 u8 done_search = 0;
1346 u16 high_low;
1348 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1350 fc = le16_to_cpu(hdr->frame_control);
1351 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1352 /* Send management frames and broadcast/multicast data using
1353 * lowest rate. */
1354 /* TODO: this could probably be improved.. */
1355 return;
1358 if (!sta || !sta->rate_ctrl_priv)
1359 return;
1361 if (!priv->lq_mngr.lq_ready) {
1362 IWL_DEBUG_RATE("still rate scaling not ready\n");
1363 return;
1365 lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1367 if (!lq_data->search_better_tbl)
1368 active_tbl = lq_data->active_tbl;
1369 else
1370 active_tbl = 1 - lq_data->active_tbl;
1372 tbl = &(lq_data->lq_info[active_tbl]);
1373 is_green = lq_data->is_green;
1375 index = sta->last_txrate;
1377 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1378 tbl->lq_type);
1380 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1381 &rate_mask);
1383 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1385 /* mask with station rate restriction */
1386 if (is_legacy(tbl->lq_type)) {
1387 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1388 rate_scale_index_msk = (u16) (rate_mask &
1389 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
1390 else
1391 rate_scale_index_msk = (u16) (rate_mask &
1392 sta->supp_rates);
1394 } else
1395 rate_scale_index_msk = rate_mask;
1397 if (!rate_scale_index_msk)
1398 rate_scale_index_msk = rate_mask;
1400 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1401 index = IWL_INVALID_VALUE;
1402 update_lq = 1;
1404 /* get the lowest availabe rate */
1405 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1406 if ((1 << i) & rate_scale_index_msk)
1407 index = i;
1410 if (index == IWL_INVALID_VALUE) {
1411 IWL_WARNING("Can not find a suitable rate\n");
1412 return;
1416 if (!tbl->expected_tpt)
1417 rs_get_expected_tpt_table(lq_data, tbl);
1419 window = &(tbl->win[index]);
1421 fail_count = window->counter - window->success_counter;
1422 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1423 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1424 || (tbl->expected_tpt == NULL)) {
1425 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1426 "for index %d\n",
1427 window->success_counter, window->counter, index);
1428 window->average_tpt = IWL_INVALID_VALUE;
1429 rs_stay_in_table(lq_data);
1430 if (update_lq) {
1431 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1432 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1433 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1435 goto out;
1437 } else
1438 window->average_tpt = ((window->success_ratio *
1439 tbl->expected_tpt[index] + 64) / 128);
1441 if (lq_data->search_better_tbl) {
1442 int success_limit = IWL_RATE_SCALE_SWITCH;
1444 if ((window->success_ratio > success_limit) ||
1445 (window->average_tpt > lq_data->last_tpt)) {
1446 if (!is_legacy(tbl->lq_type)) {
1447 IWL_DEBUG_HT("LQ: we are switching to HT"
1448 " rate suc %d current tpt %d"
1449 " old tpt %d\n",
1450 window->success_ratio,
1451 window->average_tpt,
1452 lq_data->last_tpt);
1453 lq_data->enable_counter = 1;
1455 lq_data->active_tbl = active_tbl;
1456 current_tpt = window->average_tpt;
1457 } else {
1458 tbl->lq_type = LQ_NONE;
1459 active_tbl = lq_data->active_tbl;
1460 tbl = &(lq_data->lq_info[active_tbl]);
1462 index = iwl_rate_index_from_plcp(
1463 tbl->current_rate.rate_n_flags);
1465 update_lq = 1;
1466 current_tpt = lq_data->last_tpt;
1467 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1469 lq_data->search_better_tbl = 0;
1470 done_search = 1;
1471 goto lq_update;
1474 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1475 tbl->lq_type);
1476 low = high_low & 0xff;
1477 high = (high_low >> 8) & 0xff;
1479 current_tpt = window->average_tpt;
1481 if (low != IWL_RATE_INVALID)
1482 low_tpt = tbl->win[low].average_tpt;
1484 if (high != IWL_RATE_INVALID)
1485 high_tpt = tbl->win[high].average_tpt;
1488 scale_action = 1;
1490 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1491 (current_tpt == 0)) {
1492 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1493 scale_action = -1;
1494 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1495 (high_tpt == IWL_INVALID_VALUE))
1496 scale_action = 1;
1497 else if ((low_tpt != IWL_INVALID_VALUE) &&
1498 (high_tpt != IWL_INVALID_VALUE) &&
1499 (low_tpt < current_tpt) &&
1500 (high_tpt < current_tpt))
1501 scale_action = 0;
1502 else {
1503 if (high_tpt != IWL_INVALID_VALUE) {
1504 if (high_tpt > current_tpt)
1505 scale_action = 1;
1506 else {
1507 IWL_DEBUG_RATE
1508 ("decrease rate because of high tpt\n");
1509 scale_action = -1;
1511 } else if (low_tpt != IWL_INVALID_VALUE) {
1512 if (low_tpt > current_tpt) {
1513 IWL_DEBUG_RATE
1514 ("decrease rate because of low tpt\n");
1515 scale_action = -1;
1516 } else
1517 scale_action = 1;
1521 if (scale_action == -1) {
1522 if ((low != IWL_RATE_INVALID) &&
1523 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1524 (current_tpt > (100 * tbl->expected_tpt[low]))))
1525 scale_action = 0;
1526 } else if ((scale_action == 1) &&
1527 (window->success_ratio < IWL_RATE_INCREASE_TH))
1528 scale_action = 0;
1530 switch (scale_action) {
1531 case -1:
1532 if (low != IWL_RATE_INVALID) {
1533 update_lq = 1;
1534 index = low;
1536 break;
1537 case 1:
1538 if (high != IWL_RATE_INVALID) {
1539 update_lq = 1;
1540 index = high;
1543 break;
1544 case 0:
1545 default:
1546 break;
1549 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1550 "high %d type %d\n",
1551 index, scale_action, low, high, tbl->lq_type);
1553 lq_update:
1554 if (update_lq) {
1555 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1556 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1557 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1559 rs_stay_in_table(lq_data);
1561 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1562 lq_data->last_tpt = current_tpt;
1564 if (is_legacy(tbl->lq_type))
1565 rs_move_legacy_other(priv, lq_data, index);
1566 else if (is_siso(tbl->lq_type))
1567 rs_move_siso_to_other(priv, lq_data, index);
1568 else
1569 rs_move_mimo_to_other(priv, lq_data, index);
1571 if (lq_data->search_better_tbl) {
1572 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1573 for (i = 0; i < IWL_RATE_COUNT; i++)
1574 rs_rate_scale_clear_window(&(tbl->win[i]));
1576 index = iwl_rate_index_from_plcp(
1577 tbl->current_rate.rate_n_flags);
1579 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1580 tbl->current_rate.rate_n_flags, index);
1581 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1582 &(lq_data->lq), sta);
1583 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1585 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1587 if (is_legacy(tbl1->lq_type) &&
1588 #ifdef CONFIG_IWLWIFI_HT
1589 !priv->current_assoc_ht.is_ht &&
1590 #endif
1591 (lq_data->action_counter >= 1)) {
1592 lq_data->action_counter = 0;
1593 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1594 rs_set_stay_in_table(1, lq_data);
1597 if (lq_data->enable_counter &&
1598 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1599 #ifdef CONFIG_IWLWIFI_HT_AGG
1600 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1601 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1602 priv->lq_mngr.agg_ctrl.tid_retry =
1603 TID_ALL_SPECIFIED;
1604 schedule_work(&priv->agg_work);
1606 #endif /*CONFIG_IWLWIFI_HT_AGG */
1607 lq_data->action_counter = 0;
1608 rs_set_stay_in_table(0, lq_data);
1610 } else {
1611 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1612 lq_data->flush_timer = jiffies;
1615 out:
1616 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1617 i = index;
1618 sta->last_txrate = i;
1620 /* sta->txrate is an index to A mode rates which start
1621 * at IWL_FIRST_OFDM_RATE
1623 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1624 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1625 else
1626 sta->txrate = i;
1628 return;
1632 static void rs_initialize_lq(struct iwl_priv *priv,
1633 struct sta_info *sta)
1635 int i;
1636 struct iwl_rate_scale_priv *lq;
1637 struct iwl_scale_tbl_info *tbl;
1638 u8 active_tbl = 0;
1639 int rate_idx;
1640 u8 use_green = rs_use_green(priv);
1641 struct iwl_rate mcs_rate;
1643 if (!sta || !sta->rate_ctrl_priv)
1644 goto out;
1646 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1647 i = sta->last_txrate;
1649 if ((lq->lq.sta_id == 0xff) &&
1650 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1651 goto out;
1653 if (!lq->search_better_tbl)
1654 active_tbl = lq->active_tbl;
1655 else
1656 active_tbl = 1 - lq->active_tbl;
1658 tbl = &(lq->lq_info[active_tbl]);
1660 if ((i < 0) || (i >= IWL_RATE_COUNT))
1661 i = 0;
1663 mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1664 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1665 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1667 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1668 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1670 tbl->antenna_type = ANT_AUX;
1671 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1672 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1673 rs_toggle_antenna(&mcs_rate, tbl),
1675 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1676 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1677 rs_get_expected_tpt_table(lq, tbl);
1678 rs_fill_link_cmd(lq, &mcs_rate, &(lq->lq), sta);
1679 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1680 out:
1681 return;
1684 static struct ieee80211_rate *rs_get_lowest_rate(struct ieee80211_local
1685 *local)
1687 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1688 int i;
1690 for (i = 0; i < mode->num_rates; i++) {
1691 struct ieee80211_rate *rate = &mode->rates[i];
1693 if (rate->flags & IEEE80211_RATE_SUPPORTED)
1694 return rate;
1697 return &mode->rates[0];
1700 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
1701 struct net_device *dev,
1702 struct sk_buff *skb,
1703 struct rate_control_extra
1704 *extra)
1707 int i;
1708 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1709 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1710 struct sta_info *sta;
1711 u16 fc;
1712 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1713 struct iwl_rate_scale_priv *lq;
1715 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1717 memset(extra, 0, sizeof(*extra));
1719 fc = le16_to_cpu(hdr->frame_control);
1720 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1721 /* Send management frames and broadcast/multicast data using
1722 * lowest rate. */
1723 /* TODO: this could probably be improved.. */
1724 return rs_get_lowest_rate(local);
1727 sta = sta_info_get(local, hdr->addr1);
1729 if (!sta || !sta->rate_ctrl_priv) {
1730 if (sta)
1731 sta_info_put(sta);
1732 return rs_get_lowest_rate(local);
1735 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1736 i = sta->last_txrate;
1738 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1739 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1741 if (sta_id == IWL_INVALID_STATION) {
1742 IWL_DEBUG_RATE("LQ: ADD station " MAC_FMT "\n",
1743 MAC_ARG(hdr->addr1));
1744 sta_id = iwl_add_station(priv,
1745 hdr->addr1, 0, CMD_ASYNC);
1747 if ((sta_id != IWL_INVALID_STATION)) {
1748 lq->lq.sta_id = sta_id;
1749 lq->lq.rs_table[0].rate_n_flags = 0;
1750 lq->ibss_sta_added = 1;
1751 rs_initialize_lq(priv, sta);
1753 if (!lq->ibss_sta_added)
1754 goto done;
1757 done:
1758 sta_info_put(sta);
1759 if ((i < 0) || (i > IWL_RATE_COUNT))
1760 return rs_get_lowest_rate(local);
1762 return &priv->ieee_rates[i];
1765 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1767 struct iwl_rate_scale_priv *crl;
1768 int i, j;
1770 IWL_DEBUG_RATE("create station rate scale window\n");
1772 crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1774 if (crl == NULL)
1775 return NULL;
1777 memset(crl, 0, sizeof(struct iwl_rate_scale_priv));
1778 crl->lq.sta_id = 0xff;
1780 for (j = 0; j < LQ_SIZE; j++)
1781 for (i = 0; i < IWL_RATE_COUNT; i++)
1782 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1784 return crl;
1787 static void rs_rate_init(void *priv_rate, void *priv_sta,
1788 struct ieee80211_local *local,
1789 struct sta_info *sta)
1791 int i, j;
1792 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1793 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1794 struct iwl_rate_scale_priv *crl = priv_sta;
1796 memset(crl, 0, sizeof(struct iwl_rate_scale_priv));
1798 crl->lq.sta_id = 0xff;
1799 crl->flush_timer = 0;
1800 sta->txrate = 3;
1801 for (j = 0; j < LQ_SIZE; j++)
1802 for (i = 0; i < IWL_RATE_COUNT; i++)
1803 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1805 IWL_DEBUG_RATE("rate scale global init\n");
1806 /* TODO: what is a good starting rate for STA? About middle? Maybe not
1807 * the lowest or the highest rate.. Could consider using RSSI from
1808 * previous packets? Need to have IEEE 802.1X auth succeed immediately
1809 * after assoc.. */
1811 crl->ibss_sta_added = 0;
1812 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1813 u8 sta_id = iwl_hw_find_station(priv, sta->addr);
1814 /* for IBSS the call are from tasklet */
1815 IWL_DEBUG_HT("LQ: ADD station " MAC_FMT " \n",
1816 MAC_ARG(sta->addr));
1818 if (sta_id == IWL_INVALID_STATION) {
1819 IWL_DEBUG_RATE("LQ: ADD station " MAC_FMT "\n",
1820 MAC_ARG(sta->addr));
1821 sta_id = iwl_add_station(priv,
1822 sta->addr, 0, CMD_ASYNC);
1824 if ((sta_id != IWL_INVALID_STATION)) {
1825 crl->lq.sta_id = sta_id;
1826 crl->lq.rs_table[0].rate_n_flags = 0;
1828 /* FIXME: this is w/a remove it later */
1829 priv->assoc_station_added = 1;
1832 for (i = 0; i < mode->num_rates; i++) {
1833 if ((sta->supp_rates & BIT(i)) &&
1834 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1835 sta->txrate = i;
1837 sta->last_txrate = sta->txrate;
1838 /* For MODE_IEEE80211A mode cck rate are at end
1839 * rate table
1841 if (local->hw.conf.phymode == MODE_IEEE80211A)
1842 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1844 crl->is_dup = priv->is_dup;
1845 crl->valid_antenna = priv->valid_antenna;
1846 crl->antenna = priv->antenna;
1847 crl->is_green = rs_use_green(priv);
1848 crl->active_rate = priv->active_rate;
1849 crl->active_rate &= ~(0x1000);
1850 crl->active_rate_basic = priv->active_rate_basic;
1851 crl->phymode = priv->phymode;
1852 #ifdef CONFIG_IWLWIFI_HT
1853 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1854 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1855 crl->active_siso_rate &= ~((u16)0x2);
1856 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1858 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1859 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1860 crl->active_mimo_rate &= ~((u16)0x2);
1861 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1862 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1863 crl->active_mimo_rate);
1864 #endif /*CONFIG_IWLWIFI_HT*/
1866 if (priv->assoc_station_added)
1867 priv->lq_mngr.lq_ready = 1;
1869 rs_initialize_lq(priv, sta);
1872 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
1873 struct iwl_rate *tx_mcs,
1874 struct iwl_link_quality_cmd *lq_cmd,
1875 struct sta_info *sta)
1877 int index = 0;
1878 int rc = 0;
1879 int rate_idx;
1880 u8 ant_toggle_count = 0;
1881 u8 use_ht_possible = 1;
1882 u8 repeat_cur_rate = 0;
1883 struct iwl_rate new_rate;
1884 struct iwl_scale_tbl_info tbl_type = { 0 };
1886 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1887 &tbl_type, &rate_idx);
1889 if (is_legacy(tbl_type.lq_type)) {
1890 ant_toggle_count = 1;
1891 repeat_cur_rate = IWL_NUMBER_TRY;
1892 } else
1893 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1895 lq_cmd->general_params.mimo_delimiter =
1896 is_mimo(tbl_type.lq_type) ? 1 : 0;
1897 lq_cmd->rs_table[index].rate_n_flags =
1898 cpu_to_le32(tx_mcs->rate_n_flags);
1899 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1901 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1902 lq_cmd->general_params.single_stream_ant_msk = 1;
1903 else
1904 lq_cmd->general_params.single_stream_ant_msk = 2;
1906 index++;
1907 repeat_cur_rate--;
1909 while (index < LINK_QUAL_MAX_RETRY_NUM) {
1910 while (repeat_cur_rate && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1911 if (is_legacy(tbl_type.lq_type)) {
1912 if (ant_toggle_count <
1913 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1914 ant_toggle_count++;
1915 else {
1916 rs_toggle_antenna(&new_rate, &tbl_type);
1917 ant_toggle_count = 1;
1920 lq_cmd->rs_table[index].rate_n_flags =
1921 cpu_to_le32(new_rate.rate_n_flags);
1922 repeat_cur_rate--;
1923 index++;
1926 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1927 &rate_idx);
1929 if (is_mimo(tbl_type.lq_type))
1930 lq_cmd->general_params.mimo_delimiter = index;
1932 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1933 use_ht_possible, &new_rate, sta);
1935 if (is_legacy(tbl_type.lq_type)) {
1936 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1937 ant_toggle_count++;
1938 else {
1939 rs_toggle_antenna(&new_rate, &tbl_type);
1940 ant_toggle_count = 1;
1942 repeat_cur_rate = IWL_NUMBER_TRY;
1943 } else
1944 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1946 use_ht_possible = 0;
1948 lq_cmd->rs_table[index].rate_n_flags =
1949 cpu_to_le32(new_rate.rate_n_flags);
1950 /* lq_cmd->rs_table[index].rate_n_flags = 0x800d; */
1952 index++;
1953 repeat_cur_rate--;
1956 /* lq_cmd->rs_table[0].rate_n_flags = 0x800d; */
1958 lq_cmd->general_params.dual_stream_ant_msk = 3;
1959 lq_cmd->agg_params.agg_dis_start_th = 3;
1960 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1961 return rc;
1964 static void *rs_alloc(struct ieee80211_local *local)
1966 return local->hw.priv;
1968 /* rate scale requires free function to be implemented */
1969 static void rs_free(void *priv_rate)
1971 return;
1974 static void rs_clear(void *priv_rate)
1976 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1978 IWL_DEBUG_RATE("enter\n");
1980 priv->lq_mngr.lq_ready = 0;
1981 #ifdef CONFIG_IWLWIFI_HT
1982 #ifdef CONFIG_IWLWIFI_HT_AGG
1983 if (priv->lq_mngr.agg_ctrl.granted_ba)
1984 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
1985 #endif /*CONFIG_IWLWIFI_HT_AGG */
1986 #endif /* CONFIG_IWLWIFI_HT */
1988 IWL_DEBUG_RATE("leave\n");
1991 static void rs_free_sta(void *priv, void *priv_sta)
1993 struct iwl_rate_scale_priv *rs_priv = priv_sta;
1995 IWL_DEBUG_RATE("enter\n");
1996 kfree(rs_priv);
1997 IWL_DEBUG_RATE("leave\n");
2001 static struct rate_control_ops rs_ops = {
2002 .module = NULL,
2003 .name = RS_NAME,
2004 .tx_status = rs_tx_status,
2005 .get_rate = rs_get_rate,
2006 .rate_init = rs_rate_init,
2007 .clear = rs_clear,
2008 .alloc = rs_alloc,
2009 .free = rs_free,
2010 .alloc_sta = rs_alloc_sta,
2011 .free_sta = rs_free_sta,
2014 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2016 struct ieee80211_local *local = hw_to_local(hw);
2017 struct iwl_priv *priv = hw->priv;
2018 struct iwl_rate_scale_priv *rs_priv;
2019 struct sta_info *sta;
2020 int count = 0, i;
2021 u32 samples = 0, success = 0, good = 0;
2022 unsigned long now = jiffies;
2023 u32 max_time = 0;
2024 u8 lq_type, antenna;
2026 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2027 if (!sta || !sta->rate_ctrl_priv) {
2028 if (sta) {
2029 sta_info_put(sta);
2030 IWL_DEBUG_RATE("leave - no private rate data!\n");
2031 } else
2032 IWL_DEBUG_RATE("leave - no station!\n");
2033 return sprintf(buf, "station %d not found\n", sta_id);
2036 rs_priv = (void *)sta->rate_ctrl_priv;
2038 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2039 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2041 if (is_legacy(lq_type))
2042 i = IWL_RATE_54M_INDEX;
2043 else
2044 i = IWL_RATE_60M_INDEX;
2045 while (1) {
2046 u64 mask;
2047 int j;
2048 int active = rs_priv->active_tbl;
2050 count +=
2051 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2053 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2054 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2055 buf[count++] =
2056 (rs_priv->lq_info[active].win[i].data & mask)
2057 ? '1' : '0';
2059 samples += rs_priv->lq_info[active].win[i].counter;
2060 good += rs_priv->lq_info[active].win[i].success_counter;
2061 success += rs_priv->lq_info[active].win[i].success_counter *
2062 iwl_rates[i].ieee;
2064 if (rs_priv->lq_info[active].win[i].stamp) {
2065 int delta =
2066 jiffies_to_msecs(now -
2067 rs_priv->lq_info[active].win[i].stamp);
2069 if (delta > max_time)
2070 max_time = delta;
2072 count += sprintf(&buf[count], "%5dms\n", delta);
2073 } else
2074 buf[count++] = '\n';
2076 j = iwl_get_prev_ieee_rate(i);
2077 if (j == i)
2078 break;
2079 i = j;
2082 /* Display the average rate of all samples taken.
2084 * NOTE: We multiple # of samples by 2 since the IEEE measurement
2085 * added from iwl_rates is actually 2X the rate */
2086 if (samples)
2087 count += sprintf(&buf[count],
2088 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2089 "%3d%% success (%d good packets over %d tries)\n",
2090 success / (2 * samples), (success * 5 / samples) % 10,
2091 max_time, good * 100 / samples, good, samples);
2092 else
2093 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2094 count += sprintf(&buf[count], "\nrate scale type %d anntena %d "
2095 "active_search %d rate index %d\n", lq_type, antenna,
2096 rs_priv->search_better_tbl, sta->last_txrate);
2098 sta_info_put(sta);
2099 return count;
2102 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2104 struct iwl_priv *priv = hw->priv;
2106 priv->lq_mngr.lq_ready = 1;
2109 void iwl_rate_control_register(struct ieee80211_hw *hw)
2111 ieee80211_rate_control_register(&rs_ops);
2114 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2116 ieee80211_rate_control_unregister(&rs_ops);