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];
124 #ifdef CONFIG_MAC80211_DEBUGFS
125 struct dentry *rs_sta_dbgfs_scale_table_file;
129 static void rs_rate_scale_perform(struct iwl_priv *priv,
130 struct net_device *dev,
131 struct ieee80211_hdr *hdr,
132 struct sta_info *sta);
133 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
134 struct iwl_rate *tx_mcs,
135 struct iwl_link_quality_cmd *tbl,
136 struct sta_info *sta);
139 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
140 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
143 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
144 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
147 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
148 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
151 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
152 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
155 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
156 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
159 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
160 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
163 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
164 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
167 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
168 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
171 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
172 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
175 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
176 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
179 static int iwl_lq_sync_callback(struct iwl_priv *priv,
180 struct iwl_cmd *cmd, struct sk_buff *skb)
182 /*We didn't cache the SKB; let the caller free it */
186 static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
188 return (u8)(rate_n_flags & 0xFF);
191 static int rs_send_lq_cmd(struct iwl_priv *priv,
192 struct iwl_link_quality_cmd *lq, u8 flags)
194 #ifdef CONFIG_IWLWIFI_DEBUG
199 struct iwl_host_cmd cmd = {
200 .id = REPLY_TX_LINK_QUALITY_CMD,
201 .len = sizeof(struct iwl_link_quality_cmd),
206 if ((lq->sta_id == 0xFF) &&
207 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
210 if (lq->sta_id == 0xFF)
211 lq->sta_id = IWL_AP_ID;
213 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
214 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
215 lq->general_params.single_stream_ant_msk,
216 lq->general_params.dual_stream_ant_msk);
217 #ifdef CONFIG_IWLWIFI_DEBUG
218 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
219 IWL_DEBUG_RATE("lq index %d 0x%X\n",
220 i, lq->rs_table[i].rate_n_flags);
223 if (flags & CMD_ASYNC)
224 cmd.meta.u.callback = iwl_lq_sync_callback;
226 if (iwl_is_associated(priv) && priv->assoc_station_added &&
227 priv->lq_mngr.lq_ready)
228 rc = iwl_send_cmd(priv, &cmd);
233 static int rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
236 window->success_counter = 0;
237 window->success_ratio = IWL_INVALID_VALUE;
239 window->average_tpt = IWL_INVALID_VALUE;
245 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
246 int scale_index, s32 tpt, u32 status)
249 struct iwl_rate_scale_data *window = NULL;
251 u8 win_size = IWL_RATE_MAX_WINDOW;
257 if (scale_index >= IWL_RATE_COUNT)
260 window = &(windows[scale_index]);
262 if (window->counter >= win_size) {
264 window->counter = win_size - 1;
266 mask = (mask << (win_size - 1));
267 if ((window->data & mask)) {
268 window->data &= ~mask;
269 window->success_counter = window->success_counter - 1;
273 window->counter = window->counter + 1;
275 window->data = (mask << 1);
277 window->success_counter = window->success_counter + 1;
281 if (window->counter > 0)
282 window->success_ratio = 128 * (100 * window->success_counter)
285 window->success_ratio = IWL_INVALID_VALUE;
287 fail_count = window->counter - window->success_counter;
289 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
290 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
291 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
293 window->average_tpt = IWL_INVALID_VALUE;
295 window->stamp = jiffies;
300 int static rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
301 struct iwl_scale_tbl_info *tbl,
302 int index, u8 use_green)
306 if (is_legacy(tbl->lq_type)) {
307 mcs_rate->rate_n_flags = iwl_rates[index].plcp;
308 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
309 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
311 } else if (is_siso(tbl->lq_type)) {
312 if (index > IWL_LAST_OFDM_RATE)
313 index = IWL_LAST_OFDM_RATE;
314 mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
317 if (index > IWL_LAST_OFDM_RATE)
318 index = IWL_LAST_OFDM_RATE;
319 mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
323 switch (tbl->antenna_type) {
325 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
328 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
331 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
337 if (is_legacy(tbl->lq_type))
342 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
344 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
347 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
350 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
351 if (is_siso(tbl->lq_type))
352 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
357 static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
358 int phymode, struct iwl_scale_tbl_info *tbl,
364 index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
366 if (index == IWL_RATE_INVALID) {
373 tbl->antenna_type = ANT_BOTH;
375 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
376 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
378 if (ant_msk == RATE_MCS_ANT_AB_MSK)
379 tbl->lq_type = LQ_NONE;
382 if (phymode == MODE_IEEE80211A)
387 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
388 tbl->antenna_type = ANT_MAIN;
390 tbl->antenna_type = ANT_AUX;
394 } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
395 <= IWL_RATE_SISO_60M_PLCP) {
396 tbl->lq_type = LQ_SISO;
398 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
399 if (ant_msk == RATE_MCS_ANT_AB_MSK)
400 tbl->lq_type = LQ_NONE;
402 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
403 tbl->antenna_type = ANT_MAIN;
405 tbl->antenna_type = ANT_AUX;
407 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
410 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
411 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
414 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
419 tbl->lq_type = LQ_MIMO;
420 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
423 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
424 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
427 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
434 static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
435 struct iwl_scale_tbl_info *tbl)
437 if (tbl->antenna_type == ANT_AUX) {
438 tbl->antenna_type = ANT_MAIN;
439 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
440 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
442 tbl->antenna_type = ANT_AUX;
443 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
444 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
448 static inline s8 rs_use_green(struct iwl_priv *priv)
451 #ifdef CONFIG_IWLWIFI_HT
452 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
455 if ((priv->current_assoc_ht.is_green_field) &&
456 !(priv->current_assoc_ht.operating_mode & 0x4))
458 #endif /*CONFIG_IWLWIFI_HT */
463 * rs_get_supported_rates - get the available rates
465 * if management frame or broadcast frame only return
466 * basic available rates.
469 static void rs_get_supported_rates(struct iwl_rate_scale_priv *lq_data,
470 struct ieee80211_hdr *hdr,
471 enum iwl_table_type rate_type,
474 if (is_legacy(rate_type))
475 *data_rate = lq_data->active_rate;
477 if (is_siso(rate_type))
478 *data_rate = lq_data->active_siso_rate;
480 *data_rate = lq_data->active_mimo_rate;
483 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
484 lq_data->active_rate_basic)
485 *data_rate = lq_data->active_rate_basic;
488 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
490 u8 high = IWL_RATE_INVALID;
491 u8 low = IWL_RATE_INVALID;
493 /* 802.11A or ht walks to the next literal adjascent rate in
495 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
499 /* Find the previous rate that is in the rate mask */
501 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
502 if (rate_mask & mask) {
508 /* Find the next rate that is in the rate mask */
510 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
511 if (rate_mask & mask) {
517 return (high << 8) | low;
521 while (low != IWL_RATE_INVALID) {
522 low = iwl_rates[low].prev_rs;
523 if (low == IWL_RATE_INVALID)
525 if (rate_mask & (1 << low))
527 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
531 while (high != IWL_RATE_INVALID) {
532 high = iwl_rates[high].next_rs;
533 if (high == IWL_RATE_INVALID)
535 if (rate_mask & (1 << high))
537 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
540 return (high << 8) | low;
543 static int rs_get_lower_rate(struct iwl_rate_scale_priv *lq_data,
544 struct iwl_scale_tbl_info *tbl, u8 scale_index,
545 u8 ht_possible, struct iwl_rate *mcs_rate,
546 struct sta_info *sta)
548 u8 is_green = lq_data->is_green;
552 u8 switch_to_legacy = 0;
554 /* check if we need to switch from HT to legacy rates.
555 * assumption is that mandatory rates (1Mbps or 6Mbps)
556 * are always supported (spec demand) */
557 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
558 switch_to_legacy = 1;
559 scale_index = rs_ht_to_legacy[scale_index];
560 if (lq_data->phymode == MODE_IEEE80211A)
565 if ((tbl->antenna_type == ANT_BOTH) ||
566 (tbl->antenna_type == ANT_NONE))
567 tbl->antenna_type = ANT_MAIN;
573 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
575 /* mask with station rate restriction */
576 if (is_legacy(tbl->lq_type)) {
577 if (lq_data->phymode == (u8) MODE_IEEE80211A)
578 rate_mask = (u16)(rate_mask &
579 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
581 rate_mask = (u16)(rate_mask & sta->supp_rates);
584 /* if we did switched from HT to legacy check current rate */
585 if ((switch_to_legacy) &&
586 (rate_mask & (1 << scale_index))) {
587 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
591 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
592 low = high_low & 0xff;
594 if (low != IWL_RATE_INVALID)
595 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
597 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
602 static void rs_tx_status(void *priv_rate,
603 struct net_device *dev,
605 struct ieee80211_tx_status *tx_resp)
609 int rs_index, index = 0;
610 struct iwl_rate_scale_priv *lq;
611 struct iwl_link_quality_cmd *table;
612 struct sta_info *sta;
613 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
614 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
615 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
616 struct iwl_rate_scale_data *window = NULL;
617 struct iwl_rate_scale_data *search_win = NULL;
618 struct iwl_rate tx_mcs;
619 struct iwl_scale_tbl_info tbl_type;
620 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
622 u16 fc = le16_to_cpu(hdr->frame_control);
625 IWL_DEBUG_RATE("get frame ack response, update rate scale window\n");
627 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
630 retries = tx_resp->retry_count;
636 sta = sta_info_get(local, hdr->addr1);
638 if (!sta || !sta->rate_ctrl_priv) {
644 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
646 if (!priv->lq_mngr.lq_ready)
649 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
653 active_index = lq->active_tbl;
655 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
657 lq->antenna = lq->valid_antenna;
659 lq->antenna = lq->valid_antenna;
660 curr_tbl = &(lq->lq_info[active_index]);
661 search_tbl = &(lq->lq_info[(1 - active_index)]);
662 window = (struct iwl_rate_scale_data *)
664 search_win = (struct iwl_rate_scale_data *)
665 &(search_tbl->win[0]);
667 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
669 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
670 &tbl_type, &rs_index);
671 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
672 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
673 rs_index, tx_mcs.rate_n_flags);
679 (tx_mcs.rate_n_flags !=
680 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
681 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
683 le32_to_cpu(table->rs_table[0].rate_n_flags));
689 tx_mcs.rate_n_flags =
690 le32_to_cpu(table->rs_table[index].rate_n_flags);
691 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
692 &tbl_type, &rs_index);
694 if ((tbl_type.lq_type == search_tbl->lq_type) &&
695 (tbl_type.antenna_type == search_tbl->antenna_type) &&
696 (tbl_type.is_SGI == search_tbl->is_SGI)) {
697 if (search_tbl->expected_tpt)
698 tpt = search_tbl->expected_tpt[rs_index];
701 rs_collect_tx_data(search_win,
703 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
704 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
705 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
706 if (curr_tbl->expected_tpt)
707 tpt = curr_tbl->expected_tpt[rs_index];
710 rs_collect_tx_data(window, rs_index, tpt, 0);
719 if (!tx_resp->retry_count)
720 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
722 tx_mcs.rate_n_flags =
723 le32_to_cpu(table->rs_table[index].rate_n_flags);
725 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
726 &tbl_type, &rs_index);
728 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
733 if ((tbl_type.lq_type == search_tbl->lq_type) &&
734 (tbl_type.antenna_type == search_tbl->antenna_type) &&
735 (tbl_type.is_SGI == search_tbl->is_SGI)) {
736 if (search_tbl->expected_tpt)
737 tpt = search_tbl->expected_tpt[rs_index];
740 rs_collect_tx_data(search_win,
741 rs_index, tpt, status);
742 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
743 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
744 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
745 if (curr_tbl->expected_tpt)
746 tpt = curr_tbl->expected_tpt[rs_index];
749 rs_collect_tx_data(window, rs_index, tpt, status);
752 if (lq->stay_in_tbl) {
759 rs_rate_scale_perform(priv, dev, hdr, sta);
764 static u8 rs_is_ant_connected(u8 valid_antenna,
765 enum iwl_antenna_type antenna_type)
767 if (antenna_type == ANT_AUX)
768 return ((valid_antenna & 0x2) ? 1:0);
769 else if (antenna_type == ANT_MAIN)
770 return ((valid_antenna & 0x1) ? 1:0);
771 else if (antenna_type == ANT_BOTH) {
772 if ((valid_antenna & 0x3) == 0x3)
781 static u8 rs_is_other_ant_connected(u8 valid_antenna,
782 enum iwl_antenna_type antenna_type)
784 if (antenna_type == ANT_AUX)
785 return (rs_is_ant_connected(valid_antenna, ANT_MAIN));
787 return (rs_is_ant_connected(valid_antenna, ANT_AUX));
792 static void rs_set_stay_in_table(u8 is_legacy,
793 struct iwl_rate_scale_priv *lq_data)
795 IWL_DEBUG_HT("we are staying in the same table\n");
796 lq_data->stay_in_tbl = 1;
798 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
799 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
800 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
802 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
803 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
804 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
806 lq_data->table_count = 0;
807 lq_data->total_failed = 0;
808 lq_data->total_success = 0;
811 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
812 struct iwl_scale_tbl_info *tbl)
814 if (is_legacy(tbl->lq_type)) {
815 if (!is_a_band(tbl->lq_type))
816 tbl->expected_tpt = expected_tpt_G;
818 tbl->expected_tpt = expected_tpt_A;
819 } else if (is_siso(tbl->lq_type)) {
820 if (tbl->is_fat && !lq_data->is_dup)
822 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
824 tbl->expected_tpt = expected_tpt_siso40MHz;
825 else if (tbl->is_SGI)
826 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
828 tbl->expected_tpt = expected_tpt_siso20MHz;
830 } else if (is_mimo(tbl->lq_type)) {
831 if (tbl->is_fat && !lq_data->is_dup)
833 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
835 tbl->expected_tpt = expected_tpt_mimo40MHz;
836 else if (tbl->is_SGI)
837 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
839 tbl->expected_tpt = expected_tpt_mimo20MHz;
841 tbl->expected_tpt = expected_tpt_G;
844 #ifdef CONFIG_IWLWIFI_HT
845 static s32 rs_get_best_rate(struct iwl_priv *priv,
846 struct iwl_rate_scale_priv *lq_data,
847 struct iwl_scale_tbl_info *tbl,
848 u16 rate_mask, s8 index, s8 rate)
850 struct iwl_scale_tbl_info *active_tbl =
851 &(lq_data->lq_info[lq_data->active_tbl]);
852 s32 new_rate, high, low, start_hi;
853 s32 active_sr = active_tbl->win[index].success_ratio;
854 s32 *tpt_tbl = tbl->expected_tpt;
855 s32 active_tpt = active_tbl->expected_tpt[index];
858 new_rate = high = low = start_hi = IWL_RATE_INVALID;
861 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
863 low = high_low & 0xff;
864 high = (high_low >> 8) & 0xff;
866 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
867 ((active_sr > IWL_RATE_DECREASE_TH) &&
868 (active_sr <= IWL_RATE_HIGH_TH) &&
869 (tpt_tbl[rate] <= active_tpt))) ||
870 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
871 (tpt_tbl[rate] > active_tpt))) {
873 if (start_hi != IWL_RATE_INVALID) {
878 if (low != IWL_RATE_INVALID)
883 if (new_rate != IWL_RATE_INVALID)
885 else if (high != IWL_RATE_INVALID) {
897 #endif /* CONFIG_IWLWIFI_HT */
899 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
901 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
904 static int rs_switch_to_mimo(struct iwl_priv *priv,
905 struct iwl_rate_scale_priv *lq_data,
906 struct iwl_scale_tbl_info *tbl, int index)
909 #ifdef CONFIG_IWLWIFI_HT
912 s8 is_green = lq_data->is_green;
914 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
917 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
918 tbl->lq_type = LQ_MIMO;
919 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
922 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
925 if (!rs_is_both_ant_supp(lq_data->antenna))
929 tbl->is_dup = lq_data->is_dup;
931 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
937 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
941 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
946 rs_get_expected_tpt_table(lq_data, tbl);
948 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
950 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
951 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
953 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
955 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
956 tbl->current_rate.rate_n_flags, is_green);
958 #endif /*CONFIG_IWLWIFI_HT */
962 static int rs_switch_to_siso(struct iwl_priv *priv,
963 struct iwl_rate_scale_priv *lq_data,
964 struct iwl_scale_tbl_info *tbl, int index)
967 #ifdef CONFIG_IWLWIFI_HT
969 u8 is_green = lq_data->is_green;
972 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
973 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
977 tbl->is_dup = lq_data->is_dup;
978 tbl->lq_type = LQ_SISO;
980 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
983 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
989 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
993 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
1001 rs_get_expected_tpt_table(lq_data, tbl);
1002 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1004 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1005 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1006 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1010 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1011 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1012 tbl->current_rate.rate_n_flags, is_green);
1014 #endif /*CONFIG_IWLWIFI_HT */
1018 static int rs_move_legacy_other(struct iwl_priv *priv,
1019 struct iwl_rate_scale_priv *lq_data,
1023 struct iwl_scale_tbl_info *tbl =
1024 &(lq_data->lq_info[lq_data->active_tbl]);
1025 struct iwl_scale_tbl_info *search_tbl =
1026 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1027 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1028 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1029 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1030 u8 start_action = tbl->action;
1033 switch (tbl->action) {
1034 case IWL_LEGACY_SWITCH_ANTENNA:
1035 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1037 search_tbl->lq_type = LQ_NONE;
1038 lq_data->action_counter++;
1039 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1041 if (!rs_is_other_ant_connected(lq_data->antenna,
1045 memcpy(search_tbl, tbl, sz);
1047 rs_toggle_antenna(&(search_tbl->current_rate),
1049 rs_get_expected_tpt_table(lq_data, search_tbl);
1050 lq_data->search_better_tbl = 1;
1053 case IWL_LEGACY_SWITCH_SISO:
1054 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1055 memcpy(search_tbl, tbl, sz);
1056 search_tbl->lq_type = LQ_SISO;
1057 search_tbl->is_SGI = 0;
1058 search_tbl->is_fat = 0;
1059 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1062 lq_data->search_better_tbl = 1;
1063 lq_data->action_counter = 0;
1069 case IWL_LEGACY_SWITCH_MIMO:
1070 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1071 memcpy(search_tbl, tbl, sz);
1072 search_tbl->lq_type = LQ_MIMO;
1073 search_tbl->is_SGI = 0;
1074 search_tbl->is_fat = 0;
1075 search_tbl->antenna_type = ANT_BOTH;
1076 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1079 lq_data->search_better_tbl = 1;
1080 lq_data->action_counter = 0;
1087 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1088 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1090 if (tbl->action == start_action)
1098 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1099 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1104 static int rs_move_siso_to_other(struct iwl_priv *priv,
1105 struct iwl_rate_scale_priv *lq_data,
1109 u8 is_green = lq_data->is_green;
1110 struct iwl_scale_tbl_info *tbl =
1111 &(lq_data->lq_info[lq_data->active_tbl]);
1112 struct iwl_scale_tbl_info *search_tbl =
1113 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1114 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1115 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1116 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1117 u8 start_action = tbl->action;
1120 lq_data->action_counter++;
1121 switch (tbl->action) {
1122 case IWL_SISO_SWITCH_ANTENNA:
1123 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1124 search_tbl->lq_type = LQ_NONE;
1125 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1127 if (!rs_is_other_ant_connected(lq_data->antenna,
1131 memcpy(search_tbl, tbl, sz);
1132 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1133 rs_toggle_antenna(&(search_tbl->current_rate),
1135 lq_data->search_better_tbl = 1;
1139 case IWL_SISO_SWITCH_MIMO:
1140 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1141 memcpy(search_tbl, tbl, sz);
1142 search_tbl->lq_type = LQ_MIMO;
1143 search_tbl->is_SGI = 0;
1144 search_tbl->is_fat = 0;
1145 search_tbl->antenna_type = ANT_BOTH;
1146 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1149 lq_data->search_better_tbl = 1;
1154 case IWL_SISO_SWITCH_GI:
1155 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1156 memcpy(search_tbl, tbl, sz);
1157 search_tbl->action = 0;
1158 if (search_tbl->is_SGI)
1159 search_tbl->is_SGI = 0;
1161 search_tbl->is_SGI = 1;
1164 lq_data->search_better_tbl = 1;
1165 if ((tbl->lq_type == LQ_SISO) &&
1167 s32 tpt = lq_data->last_tpt / 100;
1168 if (((!tbl->is_fat) &&
1169 (tpt >= expected_tpt_siso20MHz[index])) ||
1171 (tpt >= expected_tpt_siso40MHz[index])))
1172 lq_data->search_better_tbl = 0;
1174 rs_get_expected_tpt_table(lq_data, search_tbl);
1175 rs_mcs_from_tbl(&search_tbl->current_rate,
1176 search_tbl, index, is_green);
1180 if (tbl->action > IWL_SISO_SWITCH_GI)
1181 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1183 if (tbl->action == start_action)
1190 if (tbl->action > IWL_SISO_SWITCH_GI)
1191 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1195 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1196 struct iwl_rate_scale_priv *lq_data,
1200 s8 is_green = lq_data->is_green;
1201 struct iwl_scale_tbl_info *tbl =
1202 &(lq_data->lq_info[lq_data->active_tbl]);
1203 struct iwl_scale_tbl_info *search_tbl =
1204 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1205 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1206 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1207 u8 start_action = tbl->action;
1210 lq_data->action_counter++;
1211 switch (tbl->action) {
1212 case IWL_MIMO_SWITCH_ANTENNA_A:
1213 case IWL_MIMO_SWITCH_ANTENNA_B:
1214 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1215 memcpy(search_tbl, tbl, sz);
1216 search_tbl->lq_type = LQ_SISO;
1217 search_tbl->is_SGI = 0;
1218 search_tbl->is_fat = 0;
1219 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1220 search_tbl->antenna_type = ANT_MAIN;
1222 search_tbl->antenna_type = ANT_AUX;
1224 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1227 lq_data->search_better_tbl = 1;
1232 case IWL_MIMO_SWITCH_GI:
1233 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1234 memcpy(search_tbl, tbl, sz);
1235 search_tbl->lq_type = LQ_MIMO;
1236 search_tbl->antenna_type = ANT_BOTH;
1237 search_tbl->action = 0;
1238 if (search_tbl->is_SGI)
1239 search_tbl->is_SGI = 0;
1241 search_tbl->is_SGI = 1;
1242 lq_data->search_better_tbl = 1;
1243 if ((tbl->lq_type == LQ_MIMO) &&
1245 s32 tpt = lq_data->last_tpt / 100;
1246 if (((!tbl->is_fat) &&
1247 (tpt >= expected_tpt_mimo20MHz[index])) ||
1249 (tpt >= expected_tpt_mimo40MHz[index])))
1250 lq_data->search_better_tbl = 0;
1252 rs_get_expected_tpt_table(lq_data, search_tbl);
1253 rs_mcs_from_tbl(&search_tbl->current_rate,
1254 search_tbl, index, is_green);
1259 if (tbl->action > IWL_MIMO_SWITCH_GI)
1260 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1262 if (tbl->action == start_action)
1269 if (tbl->action > IWL_MIMO_SWITCH_GI)
1270 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1275 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1277 struct iwl_scale_tbl_info *tbl;
1280 int flush_interval_passed = 0;
1282 active_tbl = lq_data->active_tbl;
1284 tbl = &(lq_data->lq_info[active_tbl]);
1286 if (lq_data->stay_in_tbl) {
1288 if (lq_data->flush_timer)
1289 flush_interval_passed =
1291 (unsigned long)(lq_data->flush_timer +
1292 IWL_RATE_SCALE_FLUSH_INTVL));
1294 flush_interval_passed = 0;
1295 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1296 (lq_data->total_success > lq_data->max_success_limit) ||
1297 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1298 && (flush_interval_passed))) {
1299 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1300 lq_data->total_failed,
1301 lq_data->total_success,
1302 flush_interval_passed);
1303 lq_data->stay_in_tbl = 0;
1304 lq_data->total_failed = 0;
1305 lq_data->total_success = 0;
1306 lq_data->flush_timer = 0;
1307 } else if (lq_data->table_count > 0) {
1308 lq_data->table_count++;
1309 if (lq_data->table_count >=
1310 lq_data->table_count_limit) {
1311 lq_data->table_count = 0;
1313 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1314 for (i = 0; i < IWL_RATE_COUNT; i++)
1315 rs_rate_scale_clear_window(
1320 if (!lq_data->stay_in_tbl) {
1321 for (i = 0; i < IWL_RATE_COUNT; i++)
1322 rs_rate_scale_clear_window(&(tbl->win[i]));
1327 static void rs_rate_scale_perform(struct iwl_priv *priv,
1328 struct net_device *dev,
1329 struct ieee80211_hdr *hdr,
1330 struct sta_info *sta)
1332 int low = IWL_RATE_INVALID;
1333 int high = IWL_RATE_INVALID;
1336 struct iwl_rate_scale_data *window = NULL;
1337 int current_tpt = IWL_INVALID_VALUE;
1338 int low_tpt = IWL_INVALID_VALUE;
1339 int high_tpt = IWL_INVALID_VALUE;
1341 s8 scale_action = 0;
1344 struct iwl_rate_scale_priv *lq_data;
1345 struct iwl_scale_tbl_info *tbl, *tbl1;
1346 u16 rate_scale_index_msk = 0;
1347 struct iwl_rate mcs_rate;
1353 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1355 fc = le16_to_cpu(hdr->frame_control);
1356 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1357 /* Send management frames and broadcast/multicast data using
1359 /* TODO: this could probably be improved.. */
1363 if (!sta || !sta->rate_ctrl_priv)
1366 if (!priv->lq_mngr.lq_ready) {
1367 IWL_DEBUG_RATE("still rate scaling not ready\n");
1370 lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1372 if (!lq_data->search_better_tbl)
1373 active_tbl = lq_data->active_tbl;
1375 active_tbl = 1 - lq_data->active_tbl;
1377 tbl = &(lq_data->lq_info[active_tbl]);
1378 is_green = lq_data->is_green;
1380 index = sta->last_txrate;
1382 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1385 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1388 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1390 /* mask with station rate restriction */
1391 if (is_legacy(tbl->lq_type)) {
1392 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1393 rate_scale_index_msk = (u16) (rate_mask &
1394 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
1396 rate_scale_index_msk = (u16) (rate_mask &
1400 rate_scale_index_msk = rate_mask;
1402 if (!rate_scale_index_msk)
1403 rate_scale_index_msk = rate_mask;
1405 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1406 index = IWL_INVALID_VALUE;
1409 /* get the lowest availabe rate */
1410 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1411 if ((1 << i) & rate_scale_index_msk)
1415 if (index == IWL_INVALID_VALUE) {
1416 IWL_WARNING("Can not find a suitable rate\n");
1421 if (!tbl->expected_tpt)
1422 rs_get_expected_tpt_table(lq_data, tbl);
1424 window = &(tbl->win[index]);
1426 fail_count = window->counter - window->success_counter;
1427 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1428 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1429 || (tbl->expected_tpt == NULL)) {
1430 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1432 window->success_counter, window->counter, index);
1433 window->average_tpt = IWL_INVALID_VALUE;
1434 rs_stay_in_table(lq_data);
1436 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1437 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1438 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1443 window->average_tpt = ((window->success_ratio *
1444 tbl->expected_tpt[index] + 64) / 128);
1446 if (lq_data->search_better_tbl) {
1447 int success_limit = IWL_RATE_SCALE_SWITCH;
1449 if ((window->success_ratio > success_limit) ||
1450 (window->average_tpt > lq_data->last_tpt)) {
1451 if (!is_legacy(tbl->lq_type)) {
1452 IWL_DEBUG_HT("LQ: we are switching to HT"
1453 " rate suc %d current tpt %d"
1455 window->success_ratio,
1456 window->average_tpt,
1458 lq_data->enable_counter = 1;
1460 lq_data->active_tbl = active_tbl;
1461 current_tpt = window->average_tpt;
1463 tbl->lq_type = LQ_NONE;
1464 active_tbl = lq_data->active_tbl;
1465 tbl = &(lq_data->lq_info[active_tbl]);
1467 index = iwl_rate_index_from_plcp(
1468 tbl->current_rate.rate_n_flags);
1471 current_tpt = lq_data->last_tpt;
1472 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1474 lq_data->search_better_tbl = 0;
1479 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1481 low = high_low & 0xff;
1482 high = (high_low >> 8) & 0xff;
1484 current_tpt = window->average_tpt;
1486 if (low != IWL_RATE_INVALID)
1487 low_tpt = tbl->win[low].average_tpt;
1489 if (high != IWL_RATE_INVALID)
1490 high_tpt = tbl->win[high].average_tpt;
1495 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1496 (current_tpt == 0)) {
1497 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1499 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1500 (high_tpt == IWL_INVALID_VALUE))
1502 else if ((low_tpt != IWL_INVALID_VALUE) &&
1503 (high_tpt != IWL_INVALID_VALUE) &&
1504 (low_tpt < current_tpt) &&
1505 (high_tpt < current_tpt))
1508 if (high_tpt != IWL_INVALID_VALUE) {
1509 if (high_tpt > current_tpt)
1513 ("decrease rate because of high tpt\n");
1516 } else if (low_tpt != IWL_INVALID_VALUE) {
1517 if (low_tpt > current_tpt) {
1519 ("decrease rate because of low tpt\n");
1526 if (scale_action == -1) {
1527 if ((low != IWL_RATE_INVALID) &&
1528 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1529 (current_tpt > (100 * tbl->expected_tpt[low]))))
1531 } else if ((scale_action == 1) &&
1532 (window->success_ratio < IWL_RATE_INCREASE_TH))
1535 switch (scale_action) {
1537 if (low != IWL_RATE_INVALID) {
1543 if (high != IWL_RATE_INVALID) {
1554 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1555 "high %d type %d\n",
1556 index, scale_action, low, high, tbl->lq_type);
1560 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1561 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1562 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1564 rs_stay_in_table(lq_data);
1566 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1567 lq_data->last_tpt = current_tpt;
1569 if (is_legacy(tbl->lq_type))
1570 rs_move_legacy_other(priv, lq_data, index);
1571 else if (is_siso(tbl->lq_type))
1572 rs_move_siso_to_other(priv, lq_data, index);
1574 rs_move_mimo_to_other(priv, lq_data, index);
1576 if (lq_data->search_better_tbl) {
1577 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1578 for (i = 0; i < IWL_RATE_COUNT; i++)
1579 rs_rate_scale_clear_window(&(tbl->win[i]));
1581 index = iwl_rate_index_from_plcp(
1582 tbl->current_rate.rate_n_flags);
1584 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1585 tbl->current_rate.rate_n_flags, index);
1586 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1587 &(lq_data->lq), sta);
1588 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1590 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1592 if (is_legacy(tbl1->lq_type) &&
1593 #ifdef CONFIG_IWLWIFI_HT
1594 !priv->current_assoc_ht.is_ht &&
1596 (lq_data->action_counter >= 1)) {
1597 lq_data->action_counter = 0;
1598 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1599 rs_set_stay_in_table(1, lq_data);
1602 if (lq_data->enable_counter &&
1603 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1604 #ifdef CONFIG_IWLWIFI_HT_AGG
1605 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1606 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1607 priv->lq_mngr.agg_ctrl.tid_retry =
1609 schedule_work(&priv->agg_work);
1611 #endif /*CONFIG_IWLWIFI_HT_AGG */
1612 lq_data->action_counter = 0;
1613 rs_set_stay_in_table(0, lq_data);
1616 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1617 lq_data->flush_timer = jiffies;
1621 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1623 sta->last_txrate = i;
1625 /* sta->txrate is an index to A mode rates which start
1626 * at IWL_FIRST_OFDM_RATE
1628 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1629 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1637 static void rs_initialize_lq(struct iwl_priv *priv,
1638 struct sta_info *sta)
1641 struct iwl_rate_scale_priv *lq;
1642 struct iwl_scale_tbl_info *tbl;
1645 u8 use_green = rs_use_green(priv);
1646 struct iwl_rate mcs_rate;
1648 if (!sta || !sta->rate_ctrl_priv)
1651 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1652 i = sta->last_txrate;
1654 if ((lq->lq.sta_id == 0xff) &&
1655 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1658 if (!lq->search_better_tbl)
1659 active_tbl = lq->active_tbl;
1661 active_tbl = 1 - lq->active_tbl;
1663 tbl = &(lq->lq_info[active_tbl]);
1665 if ((i < 0) || (i >= IWL_RATE_COUNT))
1668 mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1669 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1670 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1672 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1673 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1675 tbl->antenna_type = ANT_AUX;
1676 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1677 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1678 rs_toggle_antenna(&mcs_rate, tbl);
1680 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1681 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1682 rs_get_expected_tpt_table(lq, tbl);
1683 rs_fill_link_cmd(lq, &mcs_rate, &(lq->lq), sta);
1684 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1689 static struct ieee80211_rate *rs_get_lowest_rate(struct ieee80211_local
1692 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1695 for (i = 0; i < mode->num_rates; i++) {
1696 struct ieee80211_rate *rate = &mode->rates[i];
1698 if (rate->flags & IEEE80211_RATE_SUPPORTED)
1702 return &mode->rates[0];
1705 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
1706 struct net_device *dev,
1707 struct sk_buff *skb,
1708 struct rate_control_extra
1713 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1714 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1715 struct sta_info *sta;
1717 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1718 struct iwl_rate_scale_priv *lq;
1720 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1722 memset(extra, 0, sizeof(*extra));
1724 fc = le16_to_cpu(hdr->frame_control);
1725 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1726 /* Send management frames and broadcast/multicast data using
1728 /* TODO: this could probably be improved.. */
1729 return rs_get_lowest_rate(local);
1732 sta = sta_info_get(local, hdr->addr1);
1734 if (!sta || !sta->rate_ctrl_priv) {
1737 return rs_get_lowest_rate(local);
1740 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1741 i = sta->last_txrate;
1743 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1744 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1745 DECLARE_MAC_BUF(mac);
1747 if (sta_id == IWL_INVALID_STATION) {
1748 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1749 print_mac(mac, hdr->addr1));
1750 sta_id = iwl_add_station(priv,
1751 hdr->addr1, 0, CMD_ASYNC);
1753 if ((sta_id != IWL_INVALID_STATION)) {
1754 lq->lq.sta_id = sta_id;
1755 lq->lq.rs_table[0].rate_n_flags = 0;
1756 lq->ibss_sta_added = 1;
1757 rs_initialize_lq(priv, sta);
1759 if (!lq->ibss_sta_added)
1765 if ((i < 0) || (i > IWL_RATE_COUNT))
1766 return rs_get_lowest_rate(local);
1768 return &priv->ieee_rates[i];
1771 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1773 struct iwl_rate_scale_priv *crl;
1776 IWL_DEBUG_RATE("create station rate scale window\n");
1778 crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1782 crl->lq.sta_id = 0xff;
1785 for (j = 0; j < LQ_SIZE; j++)
1786 for (i = 0; i < IWL_RATE_COUNT; i++)
1787 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1792 static void rs_rate_init(void *priv_rate, void *priv_sta,
1793 struct ieee80211_local *local,
1794 struct sta_info *sta)
1797 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1798 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1799 struct iwl_rate_scale_priv *crl = priv_sta;
1801 crl->flush_timer = 0;
1803 for (j = 0; j < LQ_SIZE; j++)
1804 for (i = 0; i < IWL_RATE_COUNT; i++)
1805 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1807 IWL_DEBUG_RATE("rate scale global init\n");
1808 /* TODO: what is a good starting rate for STA? About middle? Maybe not
1809 * the lowest or the highest rate.. Could consider using RSSI from
1810 * previous packets? Need to have IEEE 802.1X auth succeed immediately
1813 crl->ibss_sta_added = 0;
1814 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1815 u8 sta_id = iwl_hw_find_station(priv, sta->addr);
1816 DECLARE_MAC_BUF(mac);
1818 /* for IBSS the call are from tasklet */
1819 IWL_DEBUG_HT("LQ: ADD station %s\n",
1820 print_mac(mac, sta->addr));
1822 if (sta_id == IWL_INVALID_STATION) {
1823 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1824 print_mac(mac, sta->addr));
1825 sta_id = iwl_add_station(priv,
1826 sta->addr, 0, CMD_ASYNC);
1828 if ((sta_id != IWL_INVALID_STATION)) {
1829 crl->lq.sta_id = sta_id;
1830 crl->lq.rs_table[0].rate_n_flags = 0;
1832 /* FIXME: this is w/a remove it later */
1833 priv->assoc_station_added = 1;
1836 for (i = 0; i < mode->num_rates; i++) {
1837 if ((sta->supp_rates & BIT(i)) &&
1838 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1841 sta->last_txrate = sta->txrate;
1842 /* For MODE_IEEE80211A mode cck rate are at end
1845 if (local->hw.conf.phymode == MODE_IEEE80211A)
1846 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1848 crl->is_dup = priv->is_dup;
1849 crl->valid_antenna = priv->valid_antenna;
1850 crl->antenna = priv->antenna;
1851 crl->is_green = rs_use_green(priv);
1852 crl->active_rate = priv->active_rate;
1853 crl->active_rate &= ~(0x1000);
1854 crl->active_rate_basic = priv->active_rate_basic;
1855 crl->phymode = priv->phymode;
1856 #ifdef CONFIG_IWLWIFI_HT
1857 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1858 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1859 crl->active_siso_rate &= ~((u16)0x2);
1860 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1862 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1863 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1864 crl->active_mimo_rate &= ~((u16)0x2);
1865 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1866 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1867 crl->active_mimo_rate);
1868 #endif /*CONFIG_IWLWIFI_HT*/
1870 if (priv->assoc_station_added)
1871 priv->lq_mngr.lq_ready = 1;
1873 rs_initialize_lq(priv, sta);
1876 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
1877 struct iwl_rate *tx_mcs,
1878 struct iwl_link_quality_cmd *lq_cmd,
1879 struct sta_info *sta)
1884 u8 ant_toggle_count = 0;
1885 u8 use_ht_possible = 1;
1886 u8 repeat_cur_rate = 0;
1887 struct iwl_rate new_rate;
1888 struct iwl_scale_tbl_info tbl_type = { 0 };
1890 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1891 &tbl_type, &rate_idx);
1893 if (is_legacy(tbl_type.lq_type)) {
1894 ant_toggle_count = 1;
1895 repeat_cur_rate = IWL_NUMBER_TRY;
1897 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1899 lq_cmd->general_params.mimo_delimiter =
1900 is_mimo(tbl_type.lq_type) ? 1 : 0;
1901 lq_cmd->rs_table[index].rate_n_flags =
1902 cpu_to_le32(tx_mcs->rate_n_flags);
1903 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1905 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1906 lq_cmd->general_params.single_stream_ant_msk = 1;
1908 lq_cmd->general_params.single_stream_ant_msk = 2;
1913 while (index < LINK_QUAL_MAX_RETRY_NUM) {
1914 while (repeat_cur_rate && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1915 if (is_legacy(tbl_type.lq_type)) {
1916 if (ant_toggle_count <
1917 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1920 rs_toggle_antenna(&new_rate, &tbl_type);
1921 ant_toggle_count = 1;
1924 lq_cmd->rs_table[index].rate_n_flags =
1925 cpu_to_le32(new_rate.rate_n_flags);
1930 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1933 if (is_mimo(tbl_type.lq_type))
1934 lq_cmd->general_params.mimo_delimiter = index;
1936 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1937 use_ht_possible, &new_rate, sta);
1939 if (is_legacy(tbl_type.lq_type)) {
1940 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1943 rs_toggle_antenna(&new_rate, &tbl_type);
1944 ant_toggle_count = 1;
1946 repeat_cur_rate = IWL_NUMBER_TRY;
1948 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1950 use_ht_possible = 0;
1952 lq_cmd->rs_table[index].rate_n_flags =
1953 cpu_to_le32(new_rate.rate_n_flags);
1954 /* lq_cmd->rs_table[index].rate_n_flags = 0x800d; */
1960 /* lq_cmd->rs_table[0].rate_n_flags = 0x800d; */
1962 lq_cmd->general_params.dual_stream_ant_msk = 3;
1963 lq_cmd->agg_params.agg_dis_start_th = 3;
1964 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1968 static void *rs_alloc(struct ieee80211_local *local)
1970 return local->hw.priv;
1972 /* rate scale requires free function to be implemented */
1973 static void rs_free(void *priv_rate)
1978 static void rs_clear(void *priv_rate)
1980 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1982 IWL_DEBUG_RATE("enter\n");
1984 priv->lq_mngr.lq_ready = 0;
1985 #ifdef CONFIG_IWLWIFI_HT
1986 #ifdef CONFIG_IWLWIFI_HT_AGG
1987 if (priv->lq_mngr.agg_ctrl.granted_ba)
1988 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
1989 #endif /*CONFIG_IWLWIFI_HT_AGG */
1990 #endif /* CONFIG_IWLWIFI_HT */
1992 IWL_DEBUG_RATE("leave\n");
1995 static void rs_free_sta(void *priv, void *priv_sta)
1997 struct iwl_rate_scale_priv *rs_priv = priv_sta;
1999 IWL_DEBUG_RATE("enter\n");
2001 IWL_DEBUG_RATE("leave\n");
2005 #ifdef CONFIG_MAC80211_DEBUGFS
2006 static int open_file_generic(struct inode *inode, struct file *file)
2008 file->private_data = inode->i_private;
2012 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2013 char __user *user_buf, size_t count, loff_t *ppos)
2019 struct iwl_rate_scale_priv *rs_priv = file->private_data;
2021 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
2022 desc += sprintf(buff+desc, "failed=%d success=%d rate=%X\n",
2023 rs_priv->total_failed, rs_priv->total_success,
2024 rs_priv->active_rate);
2025 desc += sprintf(buff+desc, "general:"
2026 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2027 rs_priv->lq.general_params.flags,
2028 rs_priv->lq.general_params.mimo_delimiter,
2029 rs_priv->lq.general_params.single_stream_ant_msk,
2030 rs_priv->lq.general_params.dual_stream_ant_msk);
2032 desc += sprintf(buff+desc, "agg:"
2033 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2034 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2035 rs_priv->lq.agg_params.agg_dis_start_th,
2036 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2038 desc += sprintf(buff+desc,
2039 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2040 rs_priv->lq.general_params.start_rate_index[0],
2041 rs_priv->lq.general_params.start_rate_index[1],
2042 rs_priv->lq.general_params.start_rate_index[2],
2043 rs_priv->lq.general_params.start_rate_index[3]);
2046 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2047 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2048 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2050 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2053 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2054 .read = rs_sta_dbgfs_scale_table_read,
2055 .open = open_file_generic,
2058 static void rs_add_debugfs(void *priv, void *priv_sta,
2061 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2062 rs_priv->rs_sta_dbgfs_scale_table_file =
2063 debugfs_create_file("rate_scale_table", 0444, dir,
2064 rs_priv, &rs_sta_dbgfs_scale_table_ops);
2067 static void rs_remove_debugfs(void *priv, void *priv_sta)
2069 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2070 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
2074 static struct rate_control_ops rs_ops = {
2077 .tx_status = rs_tx_status,
2078 .get_rate = rs_get_rate,
2079 .rate_init = rs_rate_init,
2083 .alloc_sta = rs_alloc_sta,
2084 .free_sta = rs_free_sta,
2085 #ifdef CONFIG_MAC80211_DEBUGFS
2086 .add_sta_debugfs = rs_add_debugfs,
2087 .remove_sta_debugfs = rs_remove_debugfs,
2091 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2093 struct ieee80211_local *local = hw_to_local(hw);
2094 struct iwl_priv *priv = hw->priv;
2095 struct iwl_rate_scale_priv *rs_priv;
2096 struct sta_info *sta;
2098 u32 samples = 0, success = 0, good = 0;
2099 unsigned long now = jiffies;
2101 u8 lq_type, antenna;
2103 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2104 if (!sta || !sta->rate_ctrl_priv) {
2107 IWL_DEBUG_RATE("leave - no private rate data!\n");
2109 IWL_DEBUG_RATE("leave - no station!\n");
2110 return sprintf(buf, "station %d not found\n", sta_id);
2113 rs_priv = (void *)sta->rate_ctrl_priv;
2115 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2116 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2118 if (is_legacy(lq_type))
2119 i = IWL_RATE_54M_INDEX;
2121 i = IWL_RATE_60M_INDEX;
2125 int active = rs_priv->active_tbl;
2128 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2130 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2131 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2133 (rs_priv->lq_info[active].win[i].data & mask)
2136 samples += rs_priv->lq_info[active].win[i].counter;
2137 good += rs_priv->lq_info[active].win[i].success_counter;
2138 success += rs_priv->lq_info[active].win[i].success_counter *
2141 if (rs_priv->lq_info[active].win[i].stamp) {
2143 jiffies_to_msecs(now -
2144 rs_priv->lq_info[active].win[i].stamp);
2146 if (delta > max_time)
2149 count += sprintf(&buf[count], "%5dms\n", delta);
2151 buf[count++] = '\n';
2153 j = iwl_get_prev_ieee_rate(i);
2159 /* Display the average rate of all samples taken.
2161 * NOTE: We multiple # of samples by 2 since the IEEE measurement
2162 * added from iwl_rates is actually 2X the rate */
2164 count += sprintf(&buf[count],
2165 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2166 "%3d%% success (%d good packets over %d tries)\n",
2167 success / (2 * samples), (success * 5 / samples) % 10,
2168 max_time, good * 100 / samples, good, samples);
2170 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2171 count += sprintf(&buf[count], "\nrate scale type %d anntena %d "
2172 "active_search %d rate index %d\n", lq_type, antenna,
2173 rs_priv->search_better_tbl, sta->last_txrate);
2179 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2181 struct iwl_priv *priv = hw->priv;
2183 priv->lq_mngr.lq_ready = 1;
2186 void iwl_rate_control_register(struct ieee80211_hw *hw)
2188 ieee80211_rate_control_register(&rs_ops);
2191 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2193 ieee80211_rate_control_unregister(&rs_ops);