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
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>
44 #include "../net/mac80211/ieee80211_rate.h"
47 #include "iwl-helpers.h"
49 #define RS_NAME "iwl-4965-rs"
51 #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
52 #define IWL_NUMBER_TRY 1
53 #define IWL_HT_NUMBER_TRY 3
55 #define IWL_RATE_MAX_WINDOW 62
56 #define IWL_RATE_HIGH_TH 10880
57 #define IWL_RATE_MIN_FAILURE_TH 6
58 #define IWL_RATE_MIN_SUCCESS_TH 8
59 #define IWL_RATE_DECREASE_TH 1920
60 #define IWL_RATE_INCREASE_TH 8960
61 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ) /*2 seconds */
63 static u8 rs_ht_to_legacy[] = {
64 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
65 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
67 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
68 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
69 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
70 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
75 } __attribute__ ((packed));
77 struct iwl_rate_scale_data {
86 struct iwl_scale_tbl_info {
87 enum iwl_table_type lq_type;
88 enum iwl_antenna_type antenna_type;
94 struct iwl_rate current_rate;
95 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
98 struct iwl_rate_scale_priv {
102 u8 search_better_tbl;
104 u32 table_count_limit;
105 u32 max_failure_limit;
106 u32 max_success_limit;
119 u16 active_siso_rate;
120 u16 active_mimo_rate;
121 u16 active_rate_basic;
122 struct iwl_link_quality_cmd lq;
123 struct iwl_scale_tbl_info lq_info[LQ_SIZE];
126 static void rs_rate_scale_perform(struct iwl_priv *priv,
127 struct net_device *dev,
128 struct ieee80211_hdr *hdr,
129 struct sta_info *sta);
130 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
131 struct iwl_rate *tx_mcs,
132 struct iwl_link_quality_cmd *tbl,
133 struct sta_info *sta);
136 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
137 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
140 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
141 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
144 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
145 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
148 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
149 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
152 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
153 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
156 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
157 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
160 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
161 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
164 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
165 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
168 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
169 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
172 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
173 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
176 static int iwl_lq_sync_callback(struct iwl_priv *priv,
177 struct iwl_cmd *cmd, struct sk_buff *skb)
179 /*We didn't cache the SKB; let the caller free it */
183 static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
185 return (u8)(rate_n_flags & 0xFF);
188 static int rs_send_lq_cmd(struct iwl_priv *priv,
189 struct iwl_link_quality_cmd *lq, u8 flags)
191 #ifdef CONFIG_IWLWIFI_DEBUG
196 struct iwl_host_cmd cmd = {
197 .id = REPLY_TX_LINK_QUALITY_CMD,
198 .len = sizeof(struct iwl_link_quality_cmd),
203 if ((lq->sta_id == 0xFF) &&
204 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
207 if (lq->sta_id == 0xFF)
208 lq->sta_id = IWL_AP_ID;
210 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
211 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
212 lq->general_params.single_stream_ant_msk,
213 lq->general_params.dual_stream_ant_msk);
214 #ifdef CONFIG_IWLWIFI_DEBUG
215 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
216 IWL_DEBUG_RATE("lq index %d 0x%X\n",
217 i, lq->rs_table[i].rate_n_flags);
220 if (flags & CMD_ASYNC)
221 cmd.meta.u.callback = iwl_lq_sync_callback;
223 if (iwl_is_associated(priv) && priv->assoc_station_added &&
224 priv->lq_mngr.lq_ready)
225 rc = iwl_send_cmd(priv, &cmd);
230 static int rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
233 window->success_counter = 0;
234 window->success_ratio = IWL_INVALID_VALUE;
236 window->average_tpt = IWL_INVALID_VALUE;
242 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
243 int scale_index, s32 tpt, u32 status)
246 struct iwl_rate_scale_data *window = NULL;
248 u8 win_size = IWL_RATE_MAX_WINDOW;
254 if (scale_index >= IWL_RATE_COUNT)
257 window = &(windows[scale_index]);
259 if (window->counter >= win_size) {
261 window->counter = win_size - 1;
263 mask = (mask << (win_size - 1));
264 if ((window->data & mask)) {
265 window->data &= ~mask;
266 window->success_counter = window->success_counter - 1;
270 window->counter = window->counter + 1;
272 window->data = (mask << 1);
274 window->success_counter = window->success_counter + 1;
278 if (window->counter > 0)
279 window->success_ratio = 128 * (100 * window->success_counter)
282 window->success_ratio = IWL_INVALID_VALUE;
284 fail_count = window->counter - window->success_counter;
286 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
287 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
288 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
290 window->average_tpt = IWL_INVALID_VALUE;
292 window->stamp = jiffies;
297 int static rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
298 struct iwl_scale_tbl_info *tbl,
299 int index, u8 use_green)
303 if (is_legacy(tbl->lq_type)) {
304 mcs_rate->rate_n_flags = iwl_rates[index].plcp;
305 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
306 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
308 } else if (is_siso(tbl->lq_type)) {
309 if (index > IWL_LAST_OFDM_RATE)
310 index = IWL_LAST_OFDM_RATE;
311 mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
314 if (index > IWL_LAST_OFDM_RATE)
315 index = IWL_LAST_OFDM_RATE;
316 mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
320 switch (tbl->antenna_type) {
322 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
325 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
328 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
334 if (is_legacy(tbl->lq_type))
339 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
341 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
344 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
347 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
348 if (is_siso(tbl->lq_type))
349 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
354 static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
355 int phymode, struct iwl_scale_tbl_info *tbl,
361 index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
363 if (index == IWL_RATE_INVALID) {
370 tbl->antenna_type = ANT_BOTH;
372 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
373 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
375 if (ant_msk == RATE_MCS_ANT_AB_MSK)
376 tbl->lq_type = LQ_NONE;
379 if (phymode == MODE_IEEE80211A)
384 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
385 tbl->antenna_type = ANT_MAIN;
387 tbl->antenna_type = ANT_AUX;
391 } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
392 <= IWL_RATE_SISO_60M_PLCP) {
393 tbl->lq_type = LQ_SISO;
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;
399 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
400 tbl->antenna_type = ANT_MAIN;
402 tbl->antenna_type = ANT_AUX;
404 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
407 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
408 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
411 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
416 tbl->lq_type = LQ_MIMO;
417 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
420 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
421 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
424 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
431 static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
432 struct iwl_scale_tbl_info *tbl)
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;
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;
445 static inline s8 rs_use_green(struct iwl_priv *priv)
448 #ifdef CONFIG_IWLWIFI_HT
449 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
452 if ((priv->current_assoc_ht.is_green_field) &&
453 !(priv->current_assoc_ht.operating_mode & 0x4))
455 #endif /*CONFIG_IWLWIFI_HT */
460 * rs_get_supported_rates - get the available rates
462 * if management frame or broadcast frame only return
463 * basic available rates.
466 static void rs_get_supported_rates(struct iwl_rate_scale_priv *lq_data,
467 struct ieee80211_hdr *hdr,
468 enum iwl_table_type rate_type,
471 if (is_legacy(rate_type))
472 *data_rate = lq_data->active_rate;
474 if (is_siso(rate_type))
475 *data_rate = lq_data->active_siso_rate;
477 *data_rate = lq_data->active_mimo_rate;
480 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
481 lq_data->active_rate_basic)
482 *data_rate = lq_data->active_rate_basic;
485 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
487 u8 high = IWL_RATE_INVALID;
488 u8 low = IWL_RATE_INVALID;
490 /* 802.11A or ht walks to the next literal adjascent rate in
492 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
496 /* Find the previous rate that is in the rate mask */
498 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
499 if (rate_mask & mask) {
505 /* Find the next rate that is in the rate mask */
507 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
508 if (rate_mask & mask) {
514 return (high << 8) | low;
518 while (low != IWL_RATE_INVALID) {
519 low = iwl_rates[low].prev_rs;
520 if (low == IWL_RATE_INVALID)
522 if (rate_mask & (1 << low))
524 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
528 while (high != IWL_RATE_INVALID) {
529 high = iwl_rates[high].next_rs;
530 if (high == IWL_RATE_INVALID)
532 if (rate_mask & (1 << high))
534 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
537 return (high << 8) | low;
540 static int rs_get_lower_rate(struct iwl_rate_scale_priv *lq_data,
541 struct iwl_scale_tbl_info *tbl, u8 scale_index,
542 u8 ht_possible, struct iwl_rate *mcs_rate,
543 struct sta_info *sta)
545 u8 is_green = lq_data->is_green;
549 u8 switch_to_legacy = 0;
551 /* check if we need to switch from HT to legacy rates.
552 * assumption is that mandatory rates (1Mbps or 6Mbps)
553 * are always supported (spec demand) */
554 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
555 switch_to_legacy = 1;
556 scale_index = rs_ht_to_legacy[scale_index];
557 if (lq_data->phymode == MODE_IEEE80211A)
562 if ((tbl->antenna_type == ANT_BOTH) ||
563 (tbl->antenna_type == ANT_NONE))
564 tbl->antenna_type = ANT_MAIN;
570 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
572 /* mask with station rate restriction */
573 if (is_legacy(tbl->lq_type)) {
574 if (lq_data->phymode == (u8) MODE_IEEE80211A)
575 rate_mask = (u16)(rate_mask &
576 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
578 rate_mask = (u16)(rate_mask & sta->supp_rates);
581 /* if we did switched from HT to legacy check current rate */
582 if ((switch_to_legacy) &&
583 (rate_mask & (1 << scale_index))) {
584 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
588 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
589 low = high_low & 0xff;
591 if (low != IWL_RATE_INVALID)
592 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
594 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
599 static void rs_tx_status(void *priv_rate,
600 struct net_device *dev,
602 struct ieee80211_tx_status *tx_resp)
606 int rs_index, index = 0;
607 struct iwl_rate_scale_priv *lq;
608 struct iwl_link_quality_cmd *table;
609 struct sta_info *sta;
610 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
611 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
612 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
613 struct iwl_rate_scale_data *window = NULL;
614 struct iwl_rate_scale_data *search_win = NULL;
615 struct iwl_rate tx_mcs;
616 struct iwl_scale_tbl_info tbl_type;
617 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
619 u16 fc = le16_to_cpu(hdr->frame_control);
622 IWL_DEBUG_RATE("get frame ack response, update rate scale window\n");
624 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
627 retries = tx_resp->retry_count;
633 sta = sta_info_get(local, hdr->addr1);
635 if (!sta || !sta->rate_ctrl_priv) {
641 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
643 if (!priv->lq_mngr.lq_ready)
646 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
650 active_index = lq->active_tbl;
652 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
654 lq->antenna = lq->valid_antenna;
656 lq->antenna = lq->valid_antenna;
657 curr_tbl = &(lq->lq_info[active_index]);
658 search_tbl = &(lq->lq_info[(1 - active_index)]);
659 window = (struct iwl_rate_scale_data *)
661 search_win = (struct iwl_rate_scale_data *)
662 &(search_tbl->win[0]);
664 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
666 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
667 &tbl_type, &rs_index);
668 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
669 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
670 rs_index, tx_mcs.rate_n_flags);
676 (tx_mcs.rate_n_flags !=
677 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
678 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
680 le32_to_cpu(table->rs_table[0].rate_n_flags));
686 tx_mcs.rate_n_flags =
687 le32_to_cpu(table->rs_table[index].rate_n_flags);
688 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
689 &tbl_type, &rs_index);
691 if ((tbl_type.lq_type == search_tbl->lq_type) &&
692 (tbl_type.antenna_type == search_tbl->antenna_type) &&
693 (tbl_type.is_SGI == search_tbl->is_SGI)) {
694 if (search_tbl->expected_tpt)
695 tpt = search_tbl->expected_tpt[rs_index];
698 rs_collect_tx_data(search_win,
700 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
701 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
702 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
703 if (curr_tbl->expected_tpt)
704 tpt = curr_tbl->expected_tpt[rs_index];
707 rs_collect_tx_data(window, rs_index, tpt, 0);
716 if (!tx_resp->retry_count)
717 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
719 tx_mcs.rate_n_flags =
720 le32_to_cpu(table->rs_table[index].rate_n_flags);
722 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
723 &tbl_type, &rs_index);
725 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
730 if ((tbl_type.lq_type == search_tbl->lq_type) &&
731 (tbl_type.antenna_type == search_tbl->antenna_type) &&
732 (tbl_type.is_SGI == search_tbl->is_SGI)) {
733 if (search_tbl->expected_tpt)
734 tpt = search_tbl->expected_tpt[rs_index];
737 rs_collect_tx_data(search_win,
738 rs_index, tpt, status);
739 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
740 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
741 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
742 if (curr_tbl->expected_tpt)
743 tpt = curr_tbl->expected_tpt[rs_index];
746 rs_collect_tx_data(window, rs_index, tpt, status);
749 if (lq->stay_in_tbl) {
756 rs_rate_scale_perform(priv, dev, hdr, sta);
761 static u8 rs_is_ant_connected(u8 valid_antenna,
762 enum iwl_antenna_type antenna_type)
764 if (antenna_type == ANT_AUX)
765 return ((valid_antenna & 0x2) ? 1:0);
766 else if (antenna_type == ANT_MAIN)
767 return ((valid_antenna & 0x1) ? 1:0);
768 else if (antenna_type == ANT_BOTH) {
769 if ((valid_antenna & 0x3) == 0x3)
778 static u8 rs_is_other_ant_connected(u8 valid_antenna,
779 enum iwl_antenna_type antenna_type)
781 if (antenna_type == ANT_AUX)
782 return (rs_is_ant_connected(valid_antenna, ANT_MAIN));
784 return (rs_is_ant_connected(valid_antenna, ANT_AUX));
789 static void rs_set_stay_in_table(u8 is_legacy,
790 struct iwl_rate_scale_priv *lq_data)
792 IWL_DEBUG_HT("we are staying in the same table\n");
793 lq_data->stay_in_tbl = 1;
795 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
796 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
797 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
799 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
800 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
801 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
803 lq_data->table_count = 0;
804 lq_data->total_failed = 0;
805 lq_data->total_success = 0;
808 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
809 struct iwl_scale_tbl_info *tbl)
811 if (is_legacy(tbl->lq_type)) {
812 if (!is_a_band(tbl->lq_type))
813 tbl->expected_tpt = expected_tpt_G;
815 tbl->expected_tpt = expected_tpt_A;
816 } else if (is_siso(tbl->lq_type)) {
817 if (tbl->is_fat && !lq_data->is_dup)
819 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
821 tbl->expected_tpt = expected_tpt_siso40MHz;
822 else if (tbl->is_SGI)
823 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
825 tbl->expected_tpt = expected_tpt_siso20MHz;
827 } else if (is_mimo(tbl->lq_type)) {
828 if (tbl->is_fat && !lq_data->is_dup)
830 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
832 tbl->expected_tpt = expected_tpt_mimo40MHz;
833 else if (tbl->is_SGI)
834 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
836 tbl->expected_tpt = expected_tpt_mimo20MHz;
838 tbl->expected_tpt = expected_tpt_G;
841 #ifdef CONFIG_IWLWIFI_HT
842 static s32 rs_get_best_rate(struct iwl_priv *priv,
843 struct iwl_rate_scale_priv *lq_data,
844 struct iwl_scale_tbl_info *tbl,
845 u16 rate_mask, s8 index, s8 rate)
847 struct iwl_scale_tbl_info *active_tbl =
848 &(lq_data->lq_info[lq_data->active_tbl]);
849 s32 new_rate, high, low, start_hi;
850 s32 active_sr = active_tbl->win[index].success_ratio;
851 s32 *tpt_tbl = tbl->expected_tpt;
852 s32 active_tpt = active_tbl->expected_tpt[index];
855 new_rate = high = low = start_hi = IWL_RATE_INVALID;
858 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
860 low = high_low & 0xff;
861 high = (high_low >> 8) & 0xff;
863 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
864 ((active_sr > IWL_RATE_DECREASE_TH) &&
865 (active_sr <= IWL_RATE_HIGH_TH) &&
866 (tpt_tbl[rate] <= active_tpt))) ||
867 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
868 (tpt_tbl[rate] > active_tpt))) {
870 if (start_hi != IWL_RATE_INVALID) {
875 if (low != IWL_RATE_INVALID)
880 if (new_rate != IWL_RATE_INVALID)
882 else if (high != IWL_RATE_INVALID) {
894 #endif /* CONFIG_IWLWIFI_HT */
896 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
898 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
901 static int rs_switch_to_mimo(struct iwl_priv *priv,
902 struct iwl_rate_scale_priv *lq_data,
903 struct iwl_scale_tbl_info *tbl, int index)
906 #ifdef CONFIG_IWLWIFI_HT
909 s8 is_green = lq_data->is_green;
911 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
914 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
915 tbl->lq_type = LQ_MIMO;
916 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
919 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
922 if (!rs_is_both_ant_supp(lq_data->antenna))
926 tbl->is_dup = lq_data->is_dup;
928 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
934 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
938 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
943 rs_get_expected_tpt_table(lq_data, tbl);
945 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
947 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
948 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
950 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
952 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
953 tbl->current_rate.rate_n_flags, is_green);
955 #endif /*CONFIG_IWLWIFI_HT */
959 static int rs_switch_to_siso(struct iwl_priv *priv,
960 struct iwl_rate_scale_priv *lq_data,
961 struct iwl_scale_tbl_info *tbl, int index)
964 #ifdef CONFIG_IWLWIFI_HT
966 u8 is_green = lq_data->is_green;
969 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
970 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
974 tbl->is_dup = lq_data->is_dup;
975 tbl->lq_type = LQ_SISO;
977 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
980 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
986 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
990 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
998 rs_get_expected_tpt_table(lq_data, tbl);
999 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1001 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1002 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1003 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1007 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1008 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1009 tbl->current_rate.rate_n_flags, is_green);
1011 #endif /*CONFIG_IWLWIFI_HT */
1015 static int rs_move_legacy_other(struct iwl_priv *priv,
1016 struct iwl_rate_scale_priv *lq_data,
1020 struct iwl_scale_tbl_info *tbl =
1021 &(lq_data->lq_info[lq_data->active_tbl]);
1022 struct iwl_scale_tbl_info *search_tbl =
1023 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1024 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1025 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1026 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1027 u8 start_action = tbl->action;
1030 switch (tbl->action) {
1031 case IWL_LEGACY_SWITCH_ANTENNA:
1032 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1034 search_tbl->lq_type = LQ_NONE;
1035 lq_data->action_counter++;
1036 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1038 if (!rs_is_other_ant_connected(lq_data->antenna,
1042 memcpy(search_tbl, tbl, sz);
1044 rs_toggle_antenna(&(search_tbl->current_rate),
1046 rs_get_expected_tpt_table(lq_data, search_tbl);
1047 lq_data->search_better_tbl = 1;
1050 case IWL_LEGACY_SWITCH_SISO:
1051 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1052 memcpy(search_tbl, tbl, sz);
1053 search_tbl->lq_type = LQ_SISO;
1054 search_tbl->is_SGI = 0;
1055 search_tbl->is_fat = 0;
1056 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1059 lq_data->search_better_tbl = 1;
1060 lq_data->action_counter = 0;
1066 case IWL_LEGACY_SWITCH_MIMO:
1067 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1068 memcpy(search_tbl, tbl, sz);
1069 search_tbl->lq_type = LQ_MIMO;
1070 search_tbl->is_SGI = 0;
1071 search_tbl->is_fat = 0;
1072 search_tbl->antenna_type = ANT_BOTH;
1073 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1076 lq_data->search_better_tbl = 1;
1077 lq_data->action_counter = 0;
1084 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1085 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1087 if (tbl->action == start_action)
1095 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1096 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1101 static int rs_move_siso_to_other(struct iwl_priv *priv,
1102 struct iwl_rate_scale_priv *lq_data,
1106 u8 is_green = lq_data->is_green;
1107 struct iwl_scale_tbl_info *tbl =
1108 &(lq_data->lq_info[lq_data->active_tbl]);
1109 struct iwl_scale_tbl_info *search_tbl =
1110 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1111 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1112 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1113 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1114 u8 start_action = tbl->action;
1117 lq_data->action_counter++;
1118 switch (tbl->action) {
1119 case IWL_SISO_SWITCH_ANTENNA:
1120 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1121 search_tbl->lq_type = LQ_NONE;
1122 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1124 if (!rs_is_other_ant_connected(lq_data->antenna,
1128 memcpy(search_tbl, tbl, sz);
1129 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1130 rs_toggle_antenna(&(search_tbl->current_rate),
1132 lq_data->search_better_tbl = 1;
1136 case IWL_SISO_SWITCH_MIMO:
1137 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1138 memcpy(search_tbl, tbl, sz);
1139 search_tbl->lq_type = LQ_MIMO;
1140 search_tbl->is_SGI = 0;
1141 search_tbl->is_fat = 0;
1142 search_tbl->antenna_type = ANT_BOTH;
1143 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1146 lq_data->search_better_tbl = 1;
1151 case IWL_SISO_SWITCH_GI:
1152 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1153 memcpy(search_tbl, tbl, sz);
1154 search_tbl->action = 0;
1155 if (search_tbl->is_SGI)
1156 search_tbl->is_SGI = 0;
1158 search_tbl->is_SGI = 1;
1161 lq_data->search_better_tbl = 1;
1162 if ((tbl->lq_type == LQ_SISO) &&
1164 s32 tpt = lq_data->last_tpt / 100;
1165 if (((!tbl->is_fat) &&
1166 (tpt >= expected_tpt_siso20MHz[index])) ||
1168 (tpt >= expected_tpt_siso40MHz[index])))
1169 lq_data->search_better_tbl = 0;
1171 rs_get_expected_tpt_table(lq_data, search_tbl);
1172 rs_mcs_from_tbl(&search_tbl->current_rate,
1173 search_tbl, index, is_green);
1177 if (tbl->action > IWL_SISO_SWITCH_GI)
1178 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1180 if (tbl->action == start_action)
1187 if (tbl->action > IWL_SISO_SWITCH_GI)
1188 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1192 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1193 struct iwl_rate_scale_priv *lq_data,
1197 s8 is_green = lq_data->is_green;
1198 struct iwl_scale_tbl_info *tbl =
1199 &(lq_data->lq_info[lq_data->active_tbl]);
1200 struct iwl_scale_tbl_info *search_tbl =
1201 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1202 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1203 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1204 u8 start_action = tbl->action;
1207 lq_data->action_counter++;
1208 switch (tbl->action) {
1209 case IWL_MIMO_SWITCH_ANTENNA_A:
1210 case IWL_MIMO_SWITCH_ANTENNA_B:
1211 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1212 memcpy(search_tbl, tbl, sz);
1213 search_tbl->lq_type = LQ_SISO;
1214 search_tbl->is_SGI = 0;
1215 search_tbl->is_fat = 0;
1216 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1217 search_tbl->antenna_type = ANT_MAIN;
1219 search_tbl->antenna_type = ANT_AUX;
1221 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1224 lq_data->search_better_tbl = 1;
1229 case IWL_MIMO_SWITCH_GI:
1230 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1231 memcpy(search_tbl, tbl, sz);
1232 search_tbl->lq_type = LQ_MIMO;
1233 search_tbl->antenna_type = ANT_BOTH;
1234 search_tbl->action = 0;
1235 if (search_tbl->is_SGI)
1236 search_tbl->is_SGI = 0;
1238 search_tbl->is_SGI = 1;
1239 lq_data->search_better_tbl = 1;
1240 if ((tbl->lq_type == LQ_MIMO) &&
1242 s32 tpt = lq_data->last_tpt / 100;
1243 if (((!tbl->is_fat) &&
1244 (tpt >= expected_tpt_mimo20MHz[index])) ||
1246 (tpt >= expected_tpt_mimo40MHz[index])))
1247 lq_data->search_better_tbl = 0;
1249 rs_get_expected_tpt_table(lq_data, search_tbl);
1250 rs_mcs_from_tbl(&search_tbl->current_rate,
1251 search_tbl, index, is_green);
1256 if (tbl->action > IWL_MIMO_SWITCH_GI)
1257 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1259 if (tbl->action == start_action)
1266 if (tbl->action > IWL_MIMO_SWITCH_GI)
1267 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1272 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1274 struct iwl_scale_tbl_info *tbl;
1277 int flush_interval_passed = 0;
1279 active_tbl = lq_data->active_tbl;
1281 tbl = &(lq_data->lq_info[active_tbl]);
1283 if (lq_data->stay_in_tbl) {
1285 if (lq_data->flush_timer)
1286 flush_interval_passed =
1288 (unsigned long)(lq_data->flush_timer +
1289 IWL_RATE_SCALE_FLUSH_INTVL));
1291 flush_interval_passed = 0;
1292 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1293 (lq_data->total_success > lq_data->max_success_limit) ||
1294 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1295 && (flush_interval_passed))) {
1296 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1297 lq_data->total_failed,
1298 lq_data->total_success,
1299 flush_interval_passed);
1300 lq_data->stay_in_tbl = 0;
1301 lq_data->total_failed = 0;
1302 lq_data->total_success = 0;
1303 lq_data->flush_timer = 0;
1304 } else if (lq_data->table_count > 0) {
1305 lq_data->table_count++;
1306 if (lq_data->table_count >=
1307 lq_data->table_count_limit) {
1308 lq_data->table_count = 0;
1310 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1311 for (i = 0; i < IWL_RATE_COUNT; i++)
1312 rs_rate_scale_clear_window(
1317 if (!lq_data->stay_in_tbl) {
1318 for (i = 0; i < IWL_RATE_COUNT; i++)
1319 rs_rate_scale_clear_window(&(tbl->win[i]));
1324 static void rs_rate_scale_perform(struct iwl_priv *priv,
1325 struct net_device *dev,
1326 struct ieee80211_hdr *hdr,
1327 struct sta_info *sta)
1329 int low = IWL_RATE_INVALID;
1330 int high = IWL_RATE_INVALID;
1333 struct iwl_rate_scale_data *window = NULL;
1334 int current_tpt = IWL_INVALID_VALUE;
1335 int low_tpt = IWL_INVALID_VALUE;
1336 int high_tpt = IWL_INVALID_VALUE;
1338 s8 scale_action = 0;
1341 struct iwl_rate_scale_priv *lq_data;
1342 struct iwl_scale_tbl_info *tbl, *tbl1;
1343 u16 rate_scale_index_msk = 0;
1344 struct iwl_rate mcs_rate;
1350 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1352 fc = le16_to_cpu(hdr->frame_control);
1353 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1354 /* Send management frames and broadcast/multicast data using
1356 /* TODO: this could probably be improved.. */
1360 if (!sta || !sta->rate_ctrl_priv)
1363 if (!priv->lq_mngr.lq_ready) {
1364 IWL_DEBUG_RATE("still rate scaling not ready\n");
1367 lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1369 if (!lq_data->search_better_tbl)
1370 active_tbl = lq_data->active_tbl;
1372 active_tbl = 1 - lq_data->active_tbl;
1374 tbl = &(lq_data->lq_info[active_tbl]);
1375 is_green = lq_data->is_green;
1377 index = sta->last_txrate;
1379 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1382 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1385 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1387 /* mask with station rate restriction */
1388 if (is_legacy(tbl->lq_type)) {
1389 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1390 rate_scale_index_msk = (u16) (rate_mask &
1391 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
1393 rate_scale_index_msk = (u16) (rate_mask &
1397 rate_scale_index_msk = rate_mask;
1399 if (!rate_scale_index_msk)
1400 rate_scale_index_msk = rate_mask;
1402 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1403 index = IWL_INVALID_VALUE;
1406 /* get the lowest availabe rate */
1407 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1408 if ((1 << i) & rate_scale_index_msk)
1412 if (index == IWL_INVALID_VALUE) {
1413 IWL_WARNING("Can not find a suitable rate\n");
1418 if (!tbl->expected_tpt)
1419 rs_get_expected_tpt_table(lq_data, tbl);
1421 window = &(tbl->win[index]);
1423 fail_count = window->counter - window->success_counter;
1424 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1425 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1426 || (tbl->expected_tpt == NULL)) {
1427 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1429 window->success_counter, window->counter, index);
1430 window->average_tpt = IWL_INVALID_VALUE;
1431 rs_stay_in_table(lq_data);
1433 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1434 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1435 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1440 window->average_tpt = ((window->success_ratio *
1441 tbl->expected_tpt[index] + 64) / 128);
1443 if (lq_data->search_better_tbl) {
1444 int success_limit = IWL_RATE_SCALE_SWITCH;
1446 if ((window->success_ratio > success_limit) ||
1447 (window->average_tpt > lq_data->last_tpt)) {
1448 if (!is_legacy(tbl->lq_type)) {
1449 IWL_DEBUG_HT("LQ: we are switching to HT"
1450 " rate suc %d current tpt %d"
1452 window->success_ratio,
1453 window->average_tpt,
1455 lq_data->enable_counter = 1;
1457 lq_data->active_tbl = active_tbl;
1458 current_tpt = window->average_tpt;
1460 tbl->lq_type = LQ_NONE;
1461 active_tbl = lq_data->active_tbl;
1462 tbl = &(lq_data->lq_info[active_tbl]);
1464 index = iwl_rate_index_from_plcp(
1465 tbl->current_rate.rate_n_flags);
1468 current_tpt = lq_data->last_tpt;
1469 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1471 lq_data->search_better_tbl = 0;
1476 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1478 low = high_low & 0xff;
1479 high = (high_low >> 8) & 0xff;
1481 current_tpt = window->average_tpt;
1483 if (low != IWL_RATE_INVALID)
1484 low_tpt = tbl->win[low].average_tpt;
1486 if (high != IWL_RATE_INVALID)
1487 high_tpt = tbl->win[high].average_tpt;
1492 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1493 (current_tpt == 0)) {
1494 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1496 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1497 (high_tpt == IWL_INVALID_VALUE))
1499 else if ((low_tpt != IWL_INVALID_VALUE) &&
1500 (high_tpt != IWL_INVALID_VALUE) &&
1501 (low_tpt < current_tpt) &&
1502 (high_tpt < current_tpt))
1505 if (high_tpt != IWL_INVALID_VALUE) {
1506 if (high_tpt > current_tpt)
1510 ("decrease rate because of high tpt\n");
1513 } else if (low_tpt != IWL_INVALID_VALUE) {
1514 if (low_tpt > current_tpt) {
1516 ("decrease rate because of low tpt\n");
1523 if (scale_action == -1) {
1524 if ((low != IWL_RATE_INVALID) &&
1525 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1526 (current_tpt > (100 * tbl->expected_tpt[low]))))
1528 } else if ((scale_action == 1) &&
1529 (window->success_ratio < IWL_RATE_INCREASE_TH))
1532 switch (scale_action) {
1534 if (low != IWL_RATE_INVALID) {
1540 if (high != IWL_RATE_INVALID) {
1551 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1552 "high %d type %d\n",
1553 index, scale_action, low, high, tbl->lq_type);
1557 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1558 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1559 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1561 rs_stay_in_table(lq_data);
1563 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1564 lq_data->last_tpt = current_tpt;
1566 if (is_legacy(tbl->lq_type))
1567 rs_move_legacy_other(priv, lq_data, index);
1568 else if (is_siso(tbl->lq_type))
1569 rs_move_siso_to_other(priv, lq_data, index);
1571 rs_move_mimo_to_other(priv, lq_data, index);
1573 if (lq_data->search_better_tbl) {
1574 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1575 for (i = 0; i < IWL_RATE_COUNT; i++)
1576 rs_rate_scale_clear_window(&(tbl->win[i]));
1578 index = iwl_rate_index_from_plcp(
1579 tbl->current_rate.rate_n_flags);
1581 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1582 tbl->current_rate.rate_n_flags, index);
1583 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1584 &(lq_data->lq), sta);
1585 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1587 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1589 if (is_legacy(tbl1->lq_type) &&
1590 #ifdef CONFIG_IWLWIFI_HT
1591 !priv->current_assoc_ht.is_ht &&
1593 (lq_data->action_counter >= 1)) {
1594 lq_data->action_counter = 0;
1595 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1596 rs_set_stay_in_table(1, lq_data);
1599 if (lq_data->enable_counter &&
1600 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1601 #ifdef CONFIG_IWLWIFI_HT_AGG
1602 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1603 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1604 priv->lq_mngr.agg_ctrl.tid_retry =
1606 schedule_work(&priv->agg_work);
1608 #endif /*CONFIG_IWLWIFI_HT_AGG */
1609 lq_data->action_counter = 0;
1610 rs_set_stay_in_table(0, lq_data);
1613 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1614 lq_data->flush_timer = jiffies;
1618 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1620 sta->last_txrate = i;
1622 /* sta->txrate is an index to A mode rates which start
1623 * at IWL_FIRST_OFDM_RATE
1625 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1626 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1634 static void rs_initialize_lq(struct iwl_priv *priv,
1635 struct sta_info *sta)
1638 struct iwl_rate_scale_priv *lq;
1639 struct iwl_scale_tbl_info *tbl;
1642 u8 use_green = rs_use_green(priv);
1643 struct iwl_rate mcs_rate;
1645 if (!sta || !sta->rate_ctrl_priv)
1648 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1649 i = sta->last_txrate;
1651 if ((lq->lq.sta_id == 0xff) &&
1652 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1655 if (!lq->search_better_tbl)
1656 active_tbl = lq->active_tbl;
1658 active_tbl = 1 - lq->active_tbl;
1660 tbl = &(lq->lq_info[active_tbl]);
1662 if ((i < 0) || (i >= IWL_RATE_COUNT))
1665 mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1666 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1667 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1669 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1670 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1672 tbl->antenna_type = ANT_AUX;
1673 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1674 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1675 rs_toggle_antenna(&mcs_rate, tbl),
1677 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1678 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1679 rs_get_expected_tpt_table(lq, tbl);
1680 rs_fill_link_cmd(lq, &mcs_rate, &(lq->lq), sta);
1681 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1686 static struct ieee80211_rate *rs_get_lowest_rate(struct ieee80211_local
1689 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1692 for (i = 0; i < mode->num_rates; i++) {
1693 struct ieee80211_rate *rate = &mode->rates[i];
1695 if (rate->flags & IEEE80211_RATE_SUPPORTED)
1699 return &mode->rates[0];
1702 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
1703 struct net_device *dev,
1704 struct sk_buff *skb,
1705 struct rate_control_extra
1710 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1711 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1712 struct sta_info *sta;
1714 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1715 struct iwl_rate_scale_priv *lq;
1717 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1719 memset(extra, 0, sizeof(*extra));
1721 fc = le16_to_cpu(hdr->frame_control);
1722 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1723 /* Send management frames and broadcast/multicast data using
1725 /* TODO: this could probably be improved.. */
1726 return rs_get_lowest_rate(local);
1729 sta = sta_info_get(local, hdr->addr1);
1731 if (!sta || !sta->rate_ctrl_priv) {
1734 return rs_get_lowest_rate(local);
1737 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1738 i = sta->last_txrate;
1740 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1741 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1742 DECLARE_MAC_BUF(mac);
1744 if (sta_id == IWL_INVALID_STATION) {
1745 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1746 print_mac(mac, hdr->addr1));
1747 sta_id = iwl_add_station(priv,
1748 hdr->addr1, 0, CMD_ASYNC);
1750 if ((sta_id != IWL_INVALID_STATION)) {
1751 lq->lq.sta_id = sta_id;
1752 lq->lq.rs_table[0].rate_n_flags = 0;
1753 lq->ibss_sta_added = 1;
1754 rs_initialize_lq(priv, sta);
1756 if (!lq->ibss_sta_added)
1762 if ((i < 0) || (i > IWL_RATE_COUNT))
1763 return rs_get_lowest_rate(local);
1765 return &priv->ieee_rates[i];
1768 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1770 struct iwl_rate_scale_priv *crl;
1773 IWL_DEBUG_RATE("create station rate scale window\n");
1775 crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1780 memset(crl, 0, sizeof(struct iwl_rate_scale_priv));
1781 crl->lq.sta_id = 0xff;
1783 for (j = 0; j < LQ_SIZE; j++)
1784 for (i = 0; i < IWL_RATE_COUNT; i++)
1785 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1790 static void rs_rate_init(void *priv_rate, void *priv_sta,
1791 struct ieee80211_local *local,
1792 struct sta_info *sta)
1795 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1796 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1797 struct iwl_rate_scale_priv *crl = priv_sta;
1799 memset(crl, 0, sizeof(struct iwl_rate_scale_priv));
1801 crl->lq.sta_id = 0xff;
1802 crl->flush_timer = 0;
1804 for (j = 0; j < LQ_SIZE; j++)
1805 for (i = 0; i < IWL_RATE_COUNT; i++)
1806 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1808 IWL_DEBUG_RATE("rate scale global init\n");
1809 /* TODO: what is a good starting rate for STA? About middle? Maybe not
1810 * the lowest or the highest rate.. Could consider using RSSI from
1811 * previous packets? Need to have IEEE 802.1X auth succeed immediately
1814 crl->ibss_sta_added = 0;
1815 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1816 u8 sta_id = iwl_hw_find_station(priv, sta->addr);
1817 DECLARE_MAC_BUF(mac);
1819 /* for IBSS the call are from tasklet */
1820 IWL_DEBUG_HT("LQ: ADD station %s\n",
1821 print_mac(mac, sta->addr));
1823 if (sta_id == IWL_INVALID_STATION) {
1824 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1825 print_mac(mac, sta->addr));
1826 sta_id = iwl_add_station(priv,
1827 sta->addr, 0, CMD_ASYNC);
1829 if ((sta_id != IWL_INVALID_STATION)) {
1830 crl->lq.sta_id = sta_id;
1831 crl->lq.rs_table[0].rate_n_flags = 0;
1833 /* FIXME: this is w/a remove it later */
1834 priv->assoc_station_added = 1;
1837 for (i = 0; i < mode->num_rates; i++) {
1838 if ((sta->supp_rates & BIT(i)) &&
1839 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1842 sta->last_txrate = sta->txrate;
1843 /* For MODE_IEEE80211A mode cck rate are at end
1846 if (local->hw.conf.phymode == MODE_IEEE80211A)
1847 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1849 crl->is_dup = priv->is_dup;
1850 crl->valid_antenna = priv->valid_antenna;
1851 crl->antenna = priv->antenna;
1852 crl->is_green = rs_use_green(priv);
1853 crl->active_rate = priv->active_rate;
1854 crl->active_rate &= ~(0x1000);
1855 crl->active_rate_basic = priv->active_rate_basic;
1856 crl->phymode = priv->phymode;
1857 #ifdef CONFIG_IWLWIFI_HT
1858 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1859 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1860 crl->active_siso_rate &= ~((u16)0x2);
1861 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1863 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1864 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1865 crl->active_mimo_rate &= ~((u16)0x2);
1866 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1867 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1868 crl->active_mimo_rate);
1869 #endif /*CONFIG_IWLWIFI_HT*/
1871 if (priv->assoc_station_added)
1872 priv->lq_mngr.lq_ready = 1;
1874 rs_initialize_lq(priv, sta);
1877 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
1878 struct iwl_rate *tx_mcs,
1879 struct iwl_link_quality_cmd *lq_cmd,
1880 struct sta_info *sta)
1885 u8 ant_toggle_count = 0;
1886 u8 use_ht_possible = 1;
1887 u8 repeat_cur_rate = 0;
1888 struct iwl_rate new_rate;
1889 struct iwl_scale_tbl_info tbl_type = { 0 };
1891 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1892 &tbl_type, &rate_idx);
1894 if (is_legacy(tbl_type.lq_type)) {
1895 ant_toggle_count = 1;
1896 repeat_cur_rate = IWL_NUMBER_TRY;
1898 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1900 lq_cmd->general_params.mimo_delimiter =
1901 is_mimo(tbl_type.lq_type) ? 1 : 0;
1902 lq_cmd->rs_table[index].rate_n_flags =
1903 cpu_to_le32(tx_mcs->rate_n_flags);
1904 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1906 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1907 lq_cmd->general_params.single_stream_ant_msk = 1;
1909 lq_cmd->general_params.single_stream_ant_msk = 2;
1914 while (index < LINK_QUAL_MAX_RETRY_NUM) {
1915 while (repeat_cur_rate && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1916 if (is_legacy(tbl_type.lq_type)) {
1917 if (ant_toggle_count <
1918 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1921 rs_toggle_antenna(&new_rate, &tbl_type);
1922 ant_toggle_count = 1;
1925 lq_cmd->rs_table[index].rate_n_flags =
1926 cpu_to_le32(new_rate.rate_n_flags);
1931 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1934 if (is_mimo(tbl_type.lq_type))
1935 lq_cmd->general_params.mimo_delimiter = index;
1937 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1938 use_ht_possible, &new_rate, sta);
1940 if (is_legacy(tbl_type.lq_type)) {
1941 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1944 rs_toggle_antenna(&new_rate, &tbl_type);
1945 ant_toggle_count = 1;
1947 repeat_cur_rate = IWL_NUMBER_TRY;
1949 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1951 use_ht_possible = 0;
1953 lq_cmd->rs_table[index].rate_n_flags =
1954 cpu_to_le32(new_rate.rate_n_flags);
1955 /* lq_cmd->rs_table[index].rate_n_flags = 0x800d; */
1961 /* lq_cmd->rs_table[0].rate_n_flags = 0x800d; */
1963 lq_cmd->general_params.dual_stream_ant_msk = 3;
1964 lq_cmd->agg_params.agg_dis_start_th = 3;
1965 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1969 static void *rs_alloc(struct ieee80211_local *local)
1971 return local->hw.priv;
1973 /* rate scale requires free function to be implemented */
1974 static void rs_free(void *priv_rate)
1979 static void rs_clear(void *priv_rate)
1981 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1983 IWL_DEBUG_RATE("enter\n");
1985 priv->lq_mngr.lq_ready = 0;
1986 #ifdef CONFIG_IWLWIFI_HT
1987 #ifdef CONFIG_IWLWIFI_HT_AGG
1988 if (priv->lq_mngr.agg_ctrl.granted_ba)
1989 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
1990 #endif /*CONFIG_IWLWIFI_HT_AGG */
1991 #endif /* CONFIG_IWLWIFI_HT */
1993 IWL_DEBUG_RATE("leave\n");
1996 static void rs_free_sta(void *priv, void *priv_sta)
1998 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2000 IWL_DEBUG_RATE("enter\n");
2002 IWL_DEBUG_RATE("leave\n");
2006 static struct rate_control_ops rs_ops = {
2009 .tx_status = rs_tx_status,
2010 .get_rate = rs_get_rate,
2011 .rate_init = rs_rate_init,
2015 .alloc_sta = rs_alloc_sta,
2016 .free_sta = rs_free_sta,
2019 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2021 struct ieee80211_local *local = hw_to_local(hw);
2022 struct iwl_priv *priv = hw->priv;
2023 struct iwl_rate_scale_priv *rs_priv;
2024 struct sta_info *sta;
2026 u32 samples = 0, success = 0, good = 0;
2027 unsigned long now = jiffies;
2029 u8 lq_type, antenna;
2031 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2032 if (!sta || !sta->rate_ctrl_priv) {
2035 IWL_DEBUG_RATE("leave - no private rate data!\n");
2037 IWL_DEBUG_RATE("leave - no station!\n");
2038 return sprintf(buf, "station %d not found\n", sta_id);
2041 rs_priv = (void *)sta->rate_ctrl_priv;
2043 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2044 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2046 if (is_legacy(lq_type))
2047 i = IWL_RATE_54M_INDEX;
2049 i = IWL_RATE_60M_INDEX;
2053 int active = rs_priv->active_tbl;
2056 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2058 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2059 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2061 (rs_priv->lq_info[active].win[i].data & mask)
2064 samples += rs_priv->lq_info[active].win[i].counter;
2065 good += rs_priv->lq_info[active].win[i].success_counter;
2066 success += rs_priv->lq_info[active].win[i].success_counter *
2069 if (rs_priv->lq_info[active].win[i].stamp) {
2071 jiffies_to_msecs(now -
2072 rs_priv->lq_info[active].win[i].stamp);
2074 if (delta > max_time)
2077 count += sprintf(&buf[count], "%5dms\n", delta);
2079 buf[count++] = '\n';
2081 j = iwl_get_prev_ieee_rate(i);
2087 /* Display the average rate of all samples taken.
2089 * NOTE: We multiple # of samples by 2 since the IEEE measurement
2090 * added from iwl_rates is actually 2X the rate */
2092 count += sprintf(&buf[count],
2093 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2094 "%3d%% success (%d good packets over %d tries)\n",
2095 success / (2 * samples), (success * 5 / samples) % 10,
2096 max_time, good * 100 / samples, good, samples);
2098 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2099 count += sprintf(&buf[count], "\nrate scale type %d anntena %d "
2100 "active_search %d rate index %d\n", lq_type, antenna,
2101 rs_priv->search_better_tbl, sta->last_txrate);
2107 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2109 struct iwl_priv *priv = hw->priv;
2111 priv->lq_mngr.lq_ready = 1;
2114 void iwl_rate_control_register(struct ieee80211_hw *hw)
2116 ieee80211_rate_control_register(&rs_ops);
2119 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2121 ieee80211_rate_control_unregister(&rs_ops);