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;
120 u16 active_siso_rate;
121 u16 active_mimo_rate;
122 u16 active_rate_basic;
123 struct iwl_link_quality_cmd lq;
124 struct iwl_scale_tbl_info lq_info[LQ_SIZE];
125 #ifdef CONFIG_MAC80211_DEBUGFS
126 struct dentry *rs_sta_dbgfs_scale_table_file;
130 static void rs_rate_scale_perform(struct iwl_priv *priv,
131 struct net_device *dev,
132 struct ieee80211_hdr *hdr,
133 struct sta_info *sta);
134 static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
135 struct iwl_rate *tx_mcs,
136 struct iwl_link_quality_cmd *tbl);
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)
550 u8 switch_to_legacy = 0;
551 u8 is_green = lq_data->is_green;
553 /* check if we need to switch from HT to legacy rates.
554 * assumption is that mandatory rates (1Mbps or 6Mbps)
555 * are always supported (spec demand) */
556 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
557 switch_to_legacy = 1;
558 scale_index = rs_ht_to_legacy[scale_index];
559 if (lq_data->phymode == MODE_IEEE80211A)
564 if ((tbl->antenna_type == ANT_BOTH) ||
565 (tbl->antenna_type == ANT_NONE))
566 tbl->antenna_type = ANT_MAIN;
572 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
574 /* mask with station rate restriction */
575 if (is_legacy(tbl->lq_type)) {
576 if (lq_data->phymode == (u8) MODE_IEEE80211A)
577 rate_mask = (u16)(rate_mask &
578 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
580 rate_mask = (u16)(rate_mask & lq_data->supp_rates);
583 /* if we did switched from HT to legacy check current rate */
584 if ((switch_to_legacy) &&
585 (rate_mask & (1 << scale_index))) {
586 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
590 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
591 low = high_low & 0xff;
593 if (low != IWL_RATE_INVALID)
594 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
596 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
601 static void rs_tx_status(void *priv_rate,
602 struct net_device *dev,
604 struct ieee80211_tx_status *tx_resp)
608 int rs_index, index = 0;
609 struct iwl_rate_scale_priv *lq;
610 struct iwl_link_quality_cmd *table;
611 struct sta_info *sta;
612 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
613 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
614 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
615 struct iwl_rate_scale_data *window = NULL;
616 struct iwl_rate_scale_data *search_win = NULL;
617 struct iwl_rate tx_mcs;
618 struct iwl_scale_tbl_info tbl_type;
619 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
621 u16 fc = le16_to_cpu(hdr->frame_control);
624 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
626 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
629 retries = tx_resp->retry_count;
635 sta = sta_info_get(local, hdr->addr1);
637 if (!sta || !sta->rate_ctrl_priv) {
643 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
645 if (!priv->lq_mngr.lq_ready)
648 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
652 active_index = lq->active_tbl;
654 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
656 lq->antenna = lq->valid_antenna;
658 lq->antenna = lq->valid_antenna;
659 curr_tbl = &(lq->lq_info[active_index]);
660 search_tbl = &(lq->lq_info[(1 - active_index)]);
661 window = (struct iwl_rate_scale_data *)
663 search_win = (struct iwl_rate_scale_data *)
664 &(search_tbl->win[0]);
666 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
668 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
669 &tbl_type, &rs_index);
670 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
671 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
672 rs_index, tx_mcs.rate_n_flags);
678 (tx_mcs.rate_n_flags !=
679 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
680 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
682 le32_to_cpu(table->rs_table[0].rate_n_flags));
688 tx_mcs.rate_n_flags =
689 le32_to_cpu(table->rs_table[index].rate_n_flags);
690 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
691 &tbl_type, &rs_index);
693 if ((tbl_type.lq_type == search_tbl->lq_type) &&
694 (tbl_type.antenna_type == search_tbl->antenna_type) &&
695 (tbl_type.is_SGI == search_tbl->is_SGI)) {
696 if (search_tbl->expected_tpt)
697 tpt = search_tbl->expected_tpt[rs_index];
700 rs_collect_tx_data(search_win,
702 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
703 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
704 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
705 if (curr_tbl->expected_tpt)
706 tpt = curr_tbl->expected_tpt[rs_index];
709 rs_collect_tx_data(window, rs_index, tpt, 0);
718 if (!tx_resp->retry_count)
719 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
721 tx_mcs.rate_n_flags =
722 le32_to_cpu(table->rs_table[index].rate_n_flags);
724 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
725 &tbl_type, &rs_index);
727 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
732 if ((tbl_type.lq_type == search_tbl->lq_type) &&
733 (tbl_type.antenna_type == search_tbl->antenna_type) &&
734 (tbl_type.is_SGI == search_tbl->is_SGI)) {
735 if (search_tbl->expected_tpt)
736 tpt = search_tbl->expected_tpt[rs_index];
739 rs_collect_tx_data(search_win,
740 rs_index, tpt, status);
741 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
742 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
743 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
744 if (curr_tbl->expected_tpt)
745 tpt = curr_tbl->expected_tpt[rs_index];
748 rs_collect_tx_data(window, rs_index, tpt, status);
751 if (lq->stay_in_tbl) {
758 rs_rate_scale_perform(priv, dev, hdr, sta);
763 static u8 rs_is_ant_connected(u8 valid_antenna,
764 enum iwl_antenna_type antenna_type)
766 if (antenna_type == ANT_AUX)
767 return ((valid_antenna & 0x2) ? 1:0);
768 else if (antenna_type == ANT_MAIN)
769 return ((valid_antenna & 0x1) ? 1:0);
770 else if (antenna_type == ANT_BOTH) {
771 if ((valid_antenna & 0x3) == 0x3)
780 static u8 rs_is_other_ant_connected(u8 valid_antenna,
781 enum iwl_antenna_type antenna_type)
783 if (antenna_type == ANT_AUX)
784 return (rs_is_ant_connected(valid_antenna, ANT_MAIN));
786 return (rs_is_ant_connected(valid_antenna, ANT_AUX));
791 static void rs_set_stay_in_table(u8 is_legacy,
792 struct iwl_rate_scale_priv *lq_data)
794 IWL_DEBUG_HT("we are staying in the same table\n");
795 lq_data->stay_in_tbl = 1;
797 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
798 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
799 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
801 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
802 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
803 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
805 lq_data->table_count = 0;
806 lq_data->total_failed = 0;
807 lq_data->total_success = 0;
810 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
811 struct iwl_scale_tbl_info *tbl)
813 if (is_legacy(tbl->lq_type)) {
814 if (!is_a_band(tbl->lq_type))
815 tbl->expected_tpt = expected_tpt_G;
817 tbl->expected_tpt = expected_tpt_A;
818 } else if (is_siso(tbl->lq_type)) {
819 if (tbl->is_fat && !lq_data->is_dup)
821 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
823 tbl->expected_tpt = expected_tpt_siso40MHz;
824 else if (tbl->is_SGI)
825 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
827 tbl->expected_tpt = expected_tpt_siso20MHz;
829 } else if (is_mimo(tbl->lq_type)) {
830 if (tbl->is_fat && !lq_data->is_dup)
832 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
834 tbl->expected_tpt = expected_tpt_mimo40MHz;
835 else if (tbl->is_SGI)
836 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
838 tbl->expected_tpt = expected_tpt_mimo20MHz;
840 tbl->expected_tpt = expected_tpt_G;
843 #ifdef CONFIG_IWLWIFI_HT
844 static s32 rs_get_best_rate(struct iwl_priv *priv,
845 struct iwl_rate_scale_priv *lq_data,
846 struct iwl_scale_tbl_info *tbl,
847 u16 rate_mask, s8 index, s8 rate)
849 struct iwl_scale_tbl_info *active_tbl =
850 &(lq_data->lq_info[lq_data->active_tbl]);
851 s32 new_rate, high, low, start_hi;
852 s32 active_sr = active_tbl->win[index].success_ratio;
853 s32 *tpt_tbl = tbl->expected_tpt;
854 s32 active_tpt = active_tbl->expected_tpt[index];
857 new_rate = high = low = start_hi = IWL_RATE_INVALID;
860 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
862 low = high_low & 0xff;
863 high = (high_low >> 8) & 0xff;
865 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
866 ((active_sr > IWL_RATE_DECREASE_TH) &&
867 (active_sr <= IWL_RATE_HIGH_TH) &&
868 (tpt_tbl[rate] <= active_tpt))) ||
869 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
870 (tpt_tbl[rate] > active_tpt))) {
872 if (start_hi != IWL_RATE_INVALID) {
877 if (low != IWL_RATE_INVALID)
882 if (new_rate != IWL_RATE_INVALID)
884 else if (high != IWL_RATE_INVALID) {
896 #endif /* CONFIG_IWLWIFI_HT */
898 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
900 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
903 static int rs_switch_to_mimo(struct iwl_priv *priv,
904 struct iwl_rate_scale_priv *lq_data,
905 struct iwl_scale_tbl_info *tbl, int index)
908 #ifdef CONFIG_IWLWIFI_HT
911 s8 is_green = lq_data->is_green;
913 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
916 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
917 tbl->lq_type = LQ_MIMO;
918 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
921 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
924 if (!rs_is_both_ant_supp(lq_data->antenna))
928 tbl->is_dup = lq_data->is_dup;
930 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
936 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
940 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
945 rs_get_expected_tpt_table(lq_data, tbl);
947 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
949 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
950 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
952 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
954 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
955 tbl->current_rate.rate_n_flags, is_green);
957 #endif /*CONFIG_IWLWIFI_HT */
961 static int rs_switch_to_siso(struct iwl_priv *priv,
962 struct iwl_rate_scale_priv *lq_data,
963 struct iwl_scale_tbl_info *tbl, int index)
966 #ifdef CONFIG_IWLWIFI_HT
968 u8 is_green = lq_data->is_green;
971 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
972 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
976 tbl->is_dup = lq_data->is_dup;
977 tbl->lq_type = LQ_SISO;
979 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
982 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
988 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
992 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
1000 rs_get_expected_tpt_table(lq_data, tbl);
1001 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
1003 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1004 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1005 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1009 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1010 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1011 tbl->current_rate.rate_n_flags, is_green);
1013 #endif /*CONFIG_IWLWIFI_HT */
1017 static int rs_move_legacy_other(struct iwl_priv *priv,
1018 struct iwl_rate_scale_priv *lq_data,
1022 struct iwl_scale_tbl_info *tbl =
1023 &(lq_data->lq_info[lq_data->active_tbl]);
1024 struct iwl_scale_tbl_info *search_tbl =
1025 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1026 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1027 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1028 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1029 u8 start_action = tbl->action;
1032 switch (tbl->action) {
1033 case IWL_LEGACY_SWITCH_ANTENNA:
1034 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1036 search_tbl->lq_type = LQ_NONE;
1037 lq_data->action_counter++;
1038 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1040 if (!rs_is_other_ant_connected(lq_data->antenna,
1044 memcpy(search_tbl, tbl, sz);
1046 rs_toggle_antenna(&(search_tbl->current_rate),
1048 rs_get_expected_tpt_table(lq_data, search_tbl);
1049 lq_data->search_better_tbl = 1;
1052 case IWL_LEGACY_SWITCH_SISO:
1053 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1054 memcpy(search_tbl, tbl, sz);
1055 search_tbl->lq_type = LQ_SISO;
1056 search_tbl->is_SGI = 0;
1057 search_tbl->is_fat = 0;
1058 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1061 lq_data->search_better_tbl = 1;
1062 lq_data->action_counter = 0;
1068 case IWL_LEGACY_SWITCH_MIMO:
1069 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1070 memcpy(search_tbl, tbl, sz);
1071 search_tbl->lq_type = LQ_MIMO;
1072 search_tbl->is_SGI = 0;
1073 search_tbl->is_fat = 0;
1074 search_tbl->antenna_type = ANT_BOTH;
1075 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1078 lq_data->search_better_tbl = 1;
1079 lq_data->action_counter = 0;
1086 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1087 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1089 if (tbl->action == start_action)
1097 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1098 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1103 static int rs_move_siso_to_other(struct iwl_priv *priv,
1104 struct iwl_rate_scale_priv *lq_data,
1108 u8 is_green = lq_data->is_green;
1109 struct iwl_scale_tbl_info *tbl =
1110 &(lq_data->lq_info[lq_data->active_tbl]);
1111 struct iwl_scale_tbl_info *search_tbl =
1112 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1113 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1114 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1115 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1116 u8 start_action = tbl->action;
1119 lq_data->action_counter++;
1120 switch (tbl->action) {
1121 case IWL_SISO_SWITCH_ANTENNA:
1122 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1123 search_tbl->lq_type = LQ_NONE;
1124 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1126 if (!rs_is_other_ant_connected(lq_data->antenna,
1130 memcpy(search_tbl, tbl, sz);
1131 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1132 rs_toggle_antenna(&(search_tbl->current_rate),
1134 lq_data->search_better_tbl = 1;
1138 case IWL_SISO_SWITCH_MIMO:
1139 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1140 memcpy(search_tbl, tbl, sz);
1141 search_tbl->lq_type = LQ_MIMO;
1142 search_tbl->is_SGI = 0;
1143 search_tbl->is_fat = 0;
1144 search_tbl->antenna_type = ANT_BOTH;
1145 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1148 lq_data->search_better_tbl = 1;
1153 case IWL_SISO_SWITCH_GI:
1154 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1155 memcpy(search_tbl, tbl, sz);
1156 search_tbl->action = 0;
1157 if (search_tbl->is_SGI)
1158 search_tbl->is_SGI = 0;
1160 search_tbl->is_SGI = 1;
1163 lq_data->search_better_tbl = 1;
1164 if ((tbl->lq_type == LQ_SISO) &&
1166 s32 tpt = lq_data->last_tpt / 100;
1167 if (((!tbl->is_fat) &&
1168 (tpt >= expected_tpt_siso20MHz[index])) ||
1170 (tpt >= expected_tpt_siso40MHz[index])))
1171 lq_data->search_better_tbl = 0;
1173 rs_get_expected_tpt_table(lq_data, search_tbl);
1174 rs_mcs_from_tbl(&search_tbl->current_rate,
1175 search_tbl, index, is_green);
1179 if (tbl->action > IWL_SISO_SWITCH_GI)
1180 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1182 if (tbl->action == start_action)
1189 if (tbl->action > IWL_SISO_SWITCH_GI)
1190 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1194 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1195 struct iwl_rate_scale_priv *lq_data,
1199 s8 is_green = lq_data->is_green;
1200 struct iwl_scale_tbl_info *tbl =
1201 &(lq_data->lq_info[lq_data->active_tbl]);
1202 struct iwl_scale_tbl_info *search_tbl =
1203 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1204 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1205 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1206 u8 start_action = tbl->action;
1209 lq_data->action_counter++;
1210 switch (tbl->action) {
1211 case IWL_MIMO_SWITCH_ANTENNA_A:
1212 case IWL_MIMO_SWITCH_ANTENNA_B:
1213 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1214 memcpy(search_tbl, tbl, sz);
1215 search_tbl->lq_type = LQ_SISO;
1216 search_tbl->is_SGI = 0;
1217 search_tbl->is_fat = 0;
1218 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1219 search_tbl->antenna_type = ANT_MAIN;
1221 search_tbl->antenna_type = ANT_AUX;
1223 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1226 lq_data->search_better_tbl = 1;
1231 case IWL_MIMO_SWITCH_GI:
1232 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1233 memcpy(search_tbl, tbl, sz);
1234 search_tbl->lq_type = LQ_MIMO;
1235 search_tbl->antenna_type = ANT_BOTH;
1236 search_tbl->action = 0;
1237 if (search_tbl->is_SGI)
1238 search_tbl->is_SGI = 0;
1240 search_tbl->is_SGI = 1;
1241 lq_data->search_better_tbl = 1;
1242 if ((tbl->lq_type == LQ_MIMO) &&
1244 s32 tpt = lq_data->last_tpt / 100;
1245 if (((!tbl->is_fat) &&
1246 (tpt >= expected_tpt_mimo20MHz[index])) ||
1248 (tpt >= expected_tpt_mimo40MHz[index])))
1249 lq_data->search_better_tbl = 0;
1251 rs_get_expected_tpt_table(lq_data, search_tbl);
1252 rs_mcs_from_tbl(&search_tbl->current_rate,
1253 search_tbl, index, is_green);
1258 if (tbl->action > IWL_MIMO_SWITCH_GI)
1259 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1261 if (tbl->action == start_action)
1268 if (tbl->action > IWL_MIMO_SWITCH_GI)
1269 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1274 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1276 struct iwl_scale_tbl_info *tbl;
1279 int flush_interval_passed = 0;
1281 active_tbl = lq_data->active_tbl;
1283 tbl = &(lq_data->lq_info[active_tbl]);
1285 if (lq_data->stay_in_tbl) {
1287 if (lq_data->flush_timer)
1288 flush_interval_passed =
1290 (unsigned long)(lq_data->flush_timer +
1291 IWL_RATE_SCALE_FLUSH_INTVL));
1293 flush_interval_passed = 0;
1294 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1295 (lq_data->total_success > lq_data->max_success_limit) ||
1296 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1297 && (flush_interval_passed))) {
1298 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1299 lq_data->total_failed,
1300 lq_data->total_success,
1301 flush_interval_passed);
1302 lq_data->stay_in_tbl = 0;
1303 lq_data->total_failed = 0;
1304 lq_data->total_success = 0;
1305 lq_data->flush_timer = 0;
1306 } else if (lq_data->table_count > 0) {
1307 lq_data->table_count++;
1308 if (lq_data->table_count >=
1309 lq_data->table_count_limit) {
1310 lq_data->table_count = 0;
1312 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1313 for (i = 0; i < IWL_RATE_COUNT; i++)
1314 rs_rate_scale_clear_window(
1319 if (!lq_data->stay_in_tbl) {
1320 for (i = 0; i < IWL_RATE_COUNT; i++)
1321 rs_rate_scale_clear_window(&(tbl->win[i]));
1326 static void rs_rate_scale_perform(struct iwl_priv *priv,
1327 struct net_device *dev,
1328 struct ieee80211_hdr *hdr,
1329 struct sta_info *sta)
1331 int low = IWL_RATE_INVALID;
1332 int high = IWL_RATE_INVALID;
1335 struct iwl_rate_scale_data *window = NULL;
1336 int current_tpt = IWL_INVALID_VALUE;
1337 int low_tpt = IWL_INVALID_VALUE;
1338 int high_tpt = IWL_INVALID_VALUE;
1340 s8 scale_action = 0;
1343 struct iwl_rate_scale_priv *lq_data;
1344 struct iwl_scale_tbl_info *tbl, *tbl1;
1345 u16 rate_scale_index_msk = 0;
1346 struct iwl_rate mcs_rate;
1352 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1354 fc = le16_to_cpu(hdr->frame_control);
1355 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1356 /* Send management frames and broadcast/multicast data using
1358 /* TODO: this could probably be improved.. */
1362 if (!sta || !sta->rate_ctrl_priv)
1365 if (!priv->lq_mngr.lq_ready) {
1366 IWL_DEBUG_RATE("still rate scaling not ready\n");
1369 lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1371 if (!lq_data->search_better_tbl)
1372 active_tbl = lq_data->active_tbl;
1374 active_tbl = 1 - lq_data->active_tbl;
1376 tbl = &(lq_data->lq_info[active_tbl]);
1377 is_green = lq_data->is_green;
1379 index = sta->last_txrate;
1381 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1384 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1387 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1389 /* mask with station rate restriction */
1390 if (is_legacy(tbl->lq_type)) {
1391 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1392 rate_scale_index_msk = (u16) (rate_mask &
1393 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
1395 rate_scale_index_msk = (u16) (rate_mask &
1396 lq_data->supp_rates);
1399 rate_scale_index_msk = rate_mask;
1401 if (!rate_scale_index_msk)
1402 rate_scale_index_msk = rate_mask;
1404 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1405 index = IWL_INVALID_VALUE;
1408 /* get the lowest availabe rate */
1409 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1410 if ((1 << i) & rate_scale_index_msk)
1414 if (index == IWL_INVALID_VALUE) {
1415 IWL_WARNING("Can not find a suitable rate\n");
1420 if (!tbl->expected_tpt)
1421 rs_get_expected_tpt_table(lq_data, tbl);
1423 window = &(tbl->win[index]);
1425 fail_count = window->counter - window->success_counter;
1426 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1427 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1428 || (tbl->expected_tpt == NULL)) {
1429 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1431 window->success_counter, window->counter, index);
1432 window->average_tpt = IWL_INVALID_VALUE;
1433 rs_stay_in_table(lq_data);
1435 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1436 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1437 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1442 window->average_tpt = ((window->success_ratio *
1443 tbl->expected_tpt[index] + 64) / 128);
1445 if (lq_data->search_better_tbl) {
1446 int success_limit = IWL_RATE_SCALE_SWITCH;
1448 if ((window->success_ratio > success_limit) ||
1449 (window->average_tpt > lq_data->last_tpt)) {
1450 if (!is_legacy(tbl->lq_type)) {
1451 IWL_DEBUG_HT("LQ: we are switching to HT"
1452 " rate suc %d current tpt %d"
1454 window->success_ratio,
1455 window->average_tpt,
1457 lq_data->enable_counter = 1;
1459 lq_data->active_tbl = active_tbl;
1460 current_tpt = window->average_tpt;
1462 tbl->lq_type = LQ_NONE;
1463 active_tbl = lq_data->active_tbl;
1464 tbl = &(lq_data->lq_info[active_tbl]);
1466 index = iwl_rate_index_from_plcp(
1467 tbl->current_rate.rate_n_flags);
1470 current_tpt = lq_data->last_tpt;
1471 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1473 lq_data->search_better_tbl = 0;
1478 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1480 low = high_low & 0xff;
1481 high = (high_low >> 8) & 0xff;
1483 current_tpt = window->average_tpt;
1485 if (low != IWL_RATE_INVALID)
1486 low_tpt = tbl->win[low].average_tpt;
1488 if (high != IWL_RATE_INVALID)
1489 high_tpt = tbl->win[high].average_tpt;
1494 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1495 (current_tpt == 0)) {
1496 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1498 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1499 (high_tpt == IWL_INVALID_VALUE))
1501 else if ((low_tpt != IWL_INVALID_VALUE) &&
1502 (high_tpt != IWL_INVALID_VALUE) &&
1503 (low_tpt < current_tpt) &&
1504 (high_tpt < current_tpt))
1507 if (high_tpt != IWL_INVALID_VALUE) {
1508 if (high_tpt > current_tpt)
1512 ("decrease rate because of high tpt\n");
1515 } else if (low_tpt != IWL_INVALID_VALUE) {
1516 if (low_tpt > current_tpt) {
1518 ("decrease rate because of low tpt\n");
1525 if (scale_action == -1) {
1526 if ((low != IWL_RATE_INVALID) &&
1527 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1528 (current_tpt > (100 * tbl->expected_tpt[low]))))
1530 } else if ((scale_action == 1) &&
1531 (window->success_ratio < IWL_RATE_INCREASE_TH))
1534 switch (scale_action) {
1536 if (low != IWL_RATE_INVALID) {
1542 if (high != IWL_RATE_INVALID) {
1553 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1554 "high %d type %d\n",
1555 index, scale_action, low, high, tbl->lq_type);
1559 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1560 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1561 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1563 rs_stay_in_table(lq_data);
1565 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1566 lq_data->last_tpt = current_tpt;
1568 if (is_legacy(tbl->lq_type))
1569 rs_move_legacy_other(priv, lq_data, index);
1570 else if (is_siso(tbl->lq_type))
1571 rs_move_siso_to_other(priv, lq_data, index);
1573 rs_move_mimo_to_other(priv, lq_data, index);
1575 if (lq_data->search_better_tbl) {
1576 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1577 for (i = 0; i < IWL_RATE_COUNT; i++)
1578 rs_rate_scale_clear_window(&(tbl->win[i]));
1580 index = iwl_rate_index_from_plcp(
1581 tbl->current_rate.rate_n_flags);
1583 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1584 tbl->current_rate.rate_n_flags, index);
1585 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1587 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1589 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1591 if (is_legacy(tbl1->lq_type) &&
1592 #ifdef CONFIG_IWLWIFI_HT
1593 !priv->current_assoc_ht.is_ht &&
1595 (lq_data->action_counter >= 1)) {
1596 lq_data->action_counter = 0;
1597 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1598 rs_set_stay_in_table(1, lq_data);
1601 if (lq_data->enable_counter &&
1602 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1603 #ifdef CONFIG_IWLWIFI_HT_AGG
1604 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1605 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1606 priv->lq_mngr.agg_ctrl.tid_retry =
1608 schedule_work(&priv->agg_work);
1610 #endif /*CONFIG_IWLWIFI_HT_AGG */
1611 lq_data->action_counter = 0;
1612 rs_set_stay_in_table(0, lq_data);
1615 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1616 lq_data->flush_timer = jiffies;
1620 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1622 sta->last_txrate = i;
1624 /* sta->txrate is an index to A mode rates which start
1625 * at IWL_FIRST_OFDM_RATE
1627 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1628 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1636 static void rs_initialize_lq(struct iwl_priv *priv,
1637 struct sta_info *sta)
1640 struct iwl_rate_scale_priv *lq;
1641 struct iwl_scale_tbl_info *tbl;
1644 u8 use_green = rs_use_green(priv);
1645 struct iwl_rate mcs_rate;
1647 if (!sta || !sta->rate_ctrl_priv)
1650 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1651 i = sta->last_txrate;
1653 if ((lq->lq.sta_id == 0xff) &&
1654 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1657 if (!lq->search_better_tbl)
1658 active_tbl = lq->active_tbl;
1660 active_tbl = 1 - lq->active_tbl;
1662 tbl = &(lq->lq_info[active_tbl]);
1664 if ((i < 0) || (i >= IWL_RATE_COUNT))
1667 mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1668 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1669 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1671 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1672 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1674 tbl->antenna_type = ANT_AUX;
1675 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1676 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1677 rs_toggle_antenna(&mcs_rate, tbl);
1679 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1680 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1681 rs_get_expected_tpt_table(lq, tbl);
1682 rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
1683 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1688 static struct ieee80211_rate *rs_get_lowest_rate(struct ieee80211_local
1691 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1694 for (i = 0; i < mode->num_rates; i++) {
1695 struct ieee80211_rate *rate = &mode->rates[i];
1697 if (rate->flags & IEEE80211_RATE_SUPPORTED)
1701 return &mode->rates[0];
1704 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
1705 struct net_device *dev,
1706 struct sk_buff *skb,
1707 struct rate_control_extra
1712 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1713 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1714 struct sta_info *sta;
1716 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1717 struct iwl_rate_scale_priv *lq;
1719 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
1721 memset(extra, 0, sizeof(*extra));
1723 fc = le16_to_cpu(hdr->frame_control);
1724 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1725 /* Send management frames and broadcast/multicast data using
1727 /* TODO: this could probably be improved.. */
1728 return rs_get_lowest_rate(local);
1731 sta = sta_info_get(local, hdr->addr1);
1733 if (!sta || !sta->rate_ctrl_priv) {
1736 return rs_get_lowest_rate(local);
1739 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1740 i = sta->last_txrate;
1742 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1743 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1744 DECLARE_MAC_BUF(mac);
1746 if (sta_id == IWL_INVALID_STATION) {
1747 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1748 print_mac(mac, hdr->addr1));
1749 sta_id = iwl_add_station(priv,
1750 hdr->addr1, 0, CMD_ASYNC);
1752 if ((sta_id != IWL_INVALID_STATION)) {
1753 lq->lq.sta_id = sta_id;
1754 lq->lq.rs_table[0].rate_n_flags = 0;
1755 lq->ibss_sta_added = 1;
1756 rs_initialize_lq(priv, sta);
1758 if (!lq->ibss_sta_added)
1764 if ((i < 0) || (i > IWL_RATE_COUNT))
1765 return rs_get_lowest_rate(local);
1767 return &priv->ieee_rates[i];
1770 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1772 struct iwl_rate_scale_priv *crl;
1775 IWL_DEBUG_RATE("create station rate scale window\n");
1777 crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1781 crl->lq.sta_id = 0xff;
1784 for (j = 0; j < LQ_SIZE; j++)
1785 for (i = 0; i < IWL_RATE_COUNT; i++)
1786 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1791 static void rs_rate_init(void *priv_rate, void *priv_sta,
1792 struct ieee80211_local *local,
1793 struct sta_info *sta)
1796 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1797 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1798 struct iwl_rate_scale_priv *crl = priv_sta;
1800 crl->flush_timer = 0;
1801 crl->supp_rates = sta->supp_rates;
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 void 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)
1882 int repeat_rate = 0;
1883 u8 ant_toggle_count = 0;
1884 u8 use_ht_possible = 1;
1885 struct iwl_rate new_rate;
1886 struct iwl_scale_tbl_info tbl_type = { 0 };
1888 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1889 &tbl_type, &rate_idx);
1891 if (is_legacy(tbl_type.lq_type)) {
1892 ant_toggle_count = 1;
1893 repeat_rate = IWL_NUMBER_TRY;
1895 repeat_rate = IWL_HT_NUMBER_TRY;
1897 lq_cmd->general_params.mimo_delimiter =
1898 is_mimo(tbl_type.lq_type) ? 1 : 0;
1899 lq_cmd->rs_table[index].rate_n_flags =
1900 cpu_to_le32(tx_mcs->rate_n_flags);
1901 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1903 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1904 lq_cmd->general_params.single_stream_ant_msk = 1;
1906 lq_cmd->general_params.single_stream_ant_msk = 2;
1911 while (index < LINK_QUAL_MAX_RETRY_NUM) {
1912 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1913 if (is_legacy(tbl_type.lq_type)) {
1914 if (ant_toggle_count <
1915 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1918 rs_toggle_antenna(&new_rate, &tbl_type);
1919 ant_toggle_count = 1;
1922 lq_cmd->rs_table[index].rate_n_flags =
1923 cpu_to_le32(new_rate.rate_n_flags);
1928 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1931 if (is_mimo(tbl_type.lq_type))
1932 lq_cmd->general_params.mimo_delimiter = index;
1934 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1935 use_ht_possible, &new_rate);
1937 if (is_legacy(tbl_type.lq_type)) {
1938 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1941 rs_toggle_antenna(&new_rate, &tbl_type);
1942 ant_toggle_count = 1;
1944 repeat_rate = IWL_NUMBER_TRY;
1946 repeat_rate = IWL_HT_NUMBER_TRY;
1948 use_ht_possible = 0;
1950 lq_cmd->rs_table[index].rate_n_flags =
1951 cpu_to_le32(new_rate.rate_n_flags);
1957 lq_cmd->general_params.dual_stream_ant_msk = 3;
1958 lq_cmd->agg_params.agg_dis_start_th = 3;
1959 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1962 static void *rs_alloc(struct ieee80211_local *local)
1964 return local->hw.priv;
1966 /* rate scale requires free function to be implemented */
1967 static void rs_free(void *priv_rate)
1972 static void rs_clear(void *priv_rate)
1974 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1976 IWL_DEBUG_RATE("enter\n");
1978 priv->lq_mngr.lq_ready = 0;
1979 #ifdef CONFIG_IWLWIFI_HT
1980 #ifdef CONFIG_IWLWIFI_HT_AGG
1981 if (priv->lq_mngr.agg_ctrl.granted_ba)
1982 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
1983 #endif /*CONFIG_IWLWIFI_HT_AGG */
1984 #endif /* CONFIG_IWLWIFI_HT */
1986 IWL_DEBUG_RATE("leave\n");
1989 static void rs_free_sta(void *priv, void *priv_sta)
1991 struct iwl_rate_scale_priv *rs_priv = priv_sta;
1993 IWL_DEBUG_RATE("enter\n");
1995 IWL_DEBUG_RATE("leave\n");
1999 #ifdef CONFIG_MAC80211_DEBUGFS
2000 static int open_file_generic(struct inode *inode, struct file *file)
2002 file->private_data = inode->i_private;
2006 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2007 char __user *user_buf, size_t count, loff_t *ppos)
2013 struct iwl_rate_scale_priv *rs_priv = file->private_data;
2015 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
2016 desc += sprintf(buff+desc, "failed=%d success=%d rate=%X\n",
2017 rs_priv->total_failed, rs_priv->total_success,
2018 rs_priv->active_rate);
2019 desc += sprintf(buff+desc, "general:"
2020 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2021 rs_priv->lq.general_params.flags,
2022 rs_priv->lq.general_params.mimo_delimiter,
2023 rs_priv->lq.general_params.single_stream_ant_msk,
2024 rs_priv->lq.general_params.dual_stream_ant_msk);
2026 desc += sprintf(buff+desc, "agg:"
2027 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2028 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2029 rs_priv->lq.agg_params.agg_dis_start_th,
2030 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2032 desc += sprintf(buff+desc,
2033 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2034 rs_priv->lq.general_params.start_rate_index[0],
2035 rs_priv->lq.general_params.start_rate_index[1],
2036 rs_priv->lq.general_params.start_rate_index[2],
2037 rs_priv->lq.general_params.start_rate_index[3]);
2040 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2041 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2042 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2044 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2047 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2048 .read = rs_sta_dbgfs_scale_table_read,
2049 .open = open_file_generic,
2052 static void rs_add_debugfs(void *priv, void *priv_sta,
2055 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2056 rs_priv->rs_sta_dbgfs_scale_table_file =
2057 debugfs_create_file("rate_scale_table", 0444, dir,
2058 rs_priv, &rs_sta_dbgfs_scale_table_ops);
2061 static void rs_remove_debugfs(void *priv, void *priv_sta)
2063 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2064 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
2068 static struct rate_control_ops rs_ops = {
2071 .tx_status = rs_tx_status,
2072 .get_rate = rs_get_rate,
2073 .rate_init = rs_rate_init,
2077 .alloc_sta = rs_alloc_sta,
2078 .free_sta = rs_free_sta,
2079 #ifdef CONFIG_MAC80211_DEBUGFS
2080 .add_sta_debugfs = rs_add_debugfs,
2081 .remove_sta_debugfs = rs_remove_debugfs,
2085 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2087 struct ieee80211_local *local = hw_to_local(hw);
2088 struct iwl_priv *priv = hw->priv;
2089 struct iwl_rate_scale_priv *rs_priv;
2090 struct sta_info *sta;
2092 u32 samples = 0, success = 0, good = 0;
2093 unsigned long now = jiffies;
2095 u8 lq_type, antenna;
2097 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2098 if (!sta || !sta->rate_ctrl_priv) {
2101 IWL_DEBUG_RATE("leave - no private rate data!\n");
2103 IWL_DEBUG_RATE("leave - no station!\n");
2104 return sprintf(buf, "station %d not found\n", sta_id);
2107 rs_priv = (void *)sta->rate_ctrl_priv;
2109 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2110 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2112 if (is_legacy(lq_type))
2113 i = IWL_RATE_54M_INDEX;
2115 i = IWL_RATE_60M_INDEX;
2119 int active = rs_priv->active_tbl;
2122 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2124 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2125 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2127 (rs_priv->lq_info[active].win[i].data & mask)
2130 samples += rs_priv->lq_info[active].win[i].counter;
2131 good += rs_priv->lq_info[active].win[i].success_counter;
2132 success += rs_priv->lq_info[active].win[i].success_counter *
2135 if (rs_priv->lq_info[active].win[i].stamp) {
2137 jiffies_to_msecs(now -
2138 rs_priv->lq_info[active].win[i].stamp);
2140 if (delta > max_time)
2143 count += sprintf(&buf[count], "%5dms\n", delta);
2145 buf[count++] = '\n';
2147 j = iwl_get_prev_ieee_rate(i);
2153 /* Display the average rate of all samples taken.
2155 * NOTE: We multiple # of samples by 2 since the IEEE measurement
2156 * added from iwl_rates is actually 2X the rate */
2158 count += sprintf(&buf[count],
2159 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2160 "%3d%% success (%d good packets over %d tries)\n",
2161 success / (2 * samples), (success * 5 / samples) % 10,
2162 max_time, good * 100 / samples, good, samples);
2164 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2165 count += sprintf(&buf[count], "\nrate scale type %d anntena %d "
2166 "active_search %d rate index %d\n", lq_type, antenna,
2167 rs_priv->search_better_tbl, sta->last_txrate);
2173 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2175 struct iwl_priv *priv = hw->priv;
2177 priv->lq_mngr.lq_ready = 1;
2180 void iwl_rate_control_register(struct ieee80211_hw *hw)
2182 ieee80211_rate_control_register(&rs_ops);
2185 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2187 ieee80211_rate_control_unregister(&rs_ops);