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>
42 #include "../net/mac80211/ieee80211_rate.h"
45 #include "iwl-helpers.h"
47 #define RS_NAME "iwl-4965-rs"
49 #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
50 #define IWL_NUMBER_TRY 1
51 #define IWL_HT_NUMBER_TRY 3
53 #define IWL_RATE_MAX_WINDOW 62
54 #define IWL_RATE_HIGH_TH 10880
55 #define IWL_RATE_MIN_FAILURE_TH 6
56 #define IWL_RATE_MIN_SUCCESS_TH 8
57 #define IWL_RATE_DECREASE_TH 1920
58 #define IWL_RATE_INCREASE_TH 8960
59 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ) /*2 seconds */
61 static u8 rs_ht_to_legacy[] = {
62 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
63 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
65 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
66 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
67 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
68 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
73 } __attribute__ ((packed));
75 struct iwl_rate_scale_data {
84 struct iwl_scale_tbl_info {
85 enum iwl_table_type lq_type;
86 enum iwl_antenna_type antenna_type;
92 struct iwl_rate current_rate;
93 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
96 struct iwl_rate_scale_priv {
100 u8 search_better_tbl;
102 u32 table_count_limit;
103 u32 max_failure_limit;
104 u32 max_success_limit;
117 u16 active_siso_rate;
118 u16 active_mimo_rate;
119 u16 active_rate_basic;
120 struct iwl_link_quality_cmd lq;
121 struct iwl_scale_tbl_info lq_info[LQ_SIZE];
124 static void rs_rate_scale_perform(struct iwl_priv *priv,
125 struct net_device *dev,
126 struct ieee80211_hdr *hdr,
127 struct sta_info *sta);
128 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
129 struct iwl_rate *tx_mcs,
130 struct iwl_link_quality_cmd *tbl,
131 struct sta_info *sta);
134 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
135 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
138 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
139 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
142 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
143 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
146 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
147 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
150 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
151 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
154 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
155 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
158 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
159 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
162 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
163 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
166 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
167 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
170 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
171 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
174 static int iwl_lq_sync_callback(struct iwl_priv *priv,
175 struct iwl_cmd *cmd, struct sk_buff *skb)
177 /*We didn't cache the SKB; let the caller free it */
181 static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
183 return (u8)(rate_n_flags & 0xFF);
186 static int rs_send_lq_cmd(struct iwl_priv *priv,
187 struct iwl_link_quality_cmd *lq, u8 flags)
189 #ifdef CONFIG_IWLWIFI_DEBUG
194 struct iwl_host_cmd cmd = {
195 .id = REPLY_TX_LINK_QUALITY_CMD,
196 .len = sizeof(struct iwl_link_quality_cmd),
201 if ((lq->sta_id == 0xFF) &&
202 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
205 if (lq->sta_id == 0xFF)
206 lq->sta_id = IWL_AP_ID;
208 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
209 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
210 lq->general_params.single_stream_ant_msk,
211 lq->general_params.dual_stream_ant_msk);
212 #ifdef CONFIG_IWLWIFI_DEBUG
213 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
214 IWL_DEBUG_RATE("lq index %d 0x%X\n",
215 i, lq->rs_table[i].rate_n_flags);
218 if (flags & CMD_ASYNC)
219 cmd.meta.u.callback = iwl_lq_sync_callback;
221 if (iwl_is_associated(priv) && priv->assoc_station_added &&
222 priv->lq_mngr.lq_ready)
223 rc = iwl_send_cmd(priv, &cmd);
228 static int rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
231 window->success_counter = 0;
232 window->success_ratio = IWL_INVALID_VALUE;
234 window->average_tpt = IWL_INVALID_VALUE;
240 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
241 int scale_index, s32 tpt, u32 status)
244 struct iwl_rate_scale_data *window = NULL;
246 u8 win_size = IWL_RATE_MAX_WINDOW;
252 if (scale_index >= IWL_RATE_COUNT)
255 window = &(windows[scale_index]);
257 if (window->counter >= win_size) {
259 window->counter = win_size - 1;
261 mask = (mask << (win_size - 1));
262 if ((window->data & mask)) {
263 window->data &= ~mask;
264 window->success_counter = window->success_counter - 1;
268 window->counter = window->counter + 1;
270 window->data = (mask << 1);
272 window->success_counter = window->success_counter + 1;
276 if (window->counter > 0)
277 window->success_ratio = 128 * (100 * window->success_counter)
280 window->success_ratio = IWL_INVALID_VALUE;
282 fail_count = window->counter - window->success_counter;
284 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
285 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
286 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
288 window->average_tpt = IWL_INVALID_VALUE;
290 window->stamp = jiffies;
295 int static rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
296 struct iwl_scale_tbl_info *tbl,
297 int index, u8 use_green)
301 if (is_legacy(tbl->lq_type)) {
302 mcs_rate->rate_n_flags = iwl_rates[index].plcp;
303 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
304 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
306 } else if (is_siso(tbl->lq_type)) {
307 if (index > IWL_LAST_OFDM_RATE)
308 index = IWL_LAST_OFDM_RATE;
309 mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
312 if (index > IWL_LAST_OFDM_RATE)
313 index = IWL_LAST_OFDM_RATE;
314 mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
318 switch (tbl->antenna_type) {
320 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
323 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
326 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
332 if (is_legacy(tbl->lq_type))
337 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
339 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
342 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
345 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
346 if (is_siso(tbl->lq_type))
347 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
352 static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
353 int phymode, struct iwl_scale_tbl_info *tbl,
359 index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
361 if (index == IWL_RATE_INVALID) {
368 tbl->antenna_type = ANT_BOTH;
370 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
371 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
373 if (ant_msk == RATE_MCS_ANT_AB_MSK)
374 tbl->lq_type = LQ_NONE;
377 if (phymode == MODE_IEEE80211A)
382 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
383 tbl->antenna_type = ANT_MAIN;
385 tbl->antenna_type = ANT_AUX;
389 } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
390 <= IWL_RATE_SISO_60M_PLCP) {
391 tbl->lq_type = LQ_SISO;
393 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
394 if (ant_msk == RATE_MCS_ANT_AB_MSK)
395 tbl->lq_type = LQ_NONE;
397 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
398 tbl->antenna_type = ANT_MAIN;
400 tbl->antenna_type = ANT_AUX;
402 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
405 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
406 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
409 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
414 tbl->lq_type = LQ_MIMO;
415 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
418 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
419 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
422 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
429 static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
430 struct iwl_scale_tbl_info *tbl)
432 if (tbl->antenna_type == ANT_AUX) {
433 tbl->antenna_type = ANT_MAIN;
434 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
435 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
437 tbl->antenna_type = ANT_AUX;
438 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
439 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
443 static inline s8 rs_use_green(struct iwl_priv *priv)
446 #ifdef CONFIG_IWLWIFI_HT
447 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
450 if ((priv->current_assoc_ht.is_green_field) &&
451 !(priv->current_assoc_ht.operating_mode & 0x4))
453 #endif /*CONFIG_IWLWIFI_HT */
458 * rs_get_supported_rates - get the available rates
460 * if management frame or broadcast frame only return
461 * basic available rates.
464 static void rs_get_supported_rates(struct iwl_rate_scale_priv *lq_data,
465 struct ieee80211_hdr *hdr,
466 enum iwl_table_type rate_type,
469 if (is_legacy(rate_type))
470 *data_rate = lq_data->active_rate;
472 if (is_siso(rate_type))
473 *data_rate = lq_data->active_siso_rate;
475 *data_rate = lq_data->active_mimo_rate;
478 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
479 lq_data->active_rate_basic)
480 *data_rate = lq_data->active_rate_basic;
483 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
485 u8 high = IWL_RATE_INVALID;
486 u8 low = IWL_RATE_INVALID;
488 /* 802.11A or ht walks to the next literal adjascent rate in
490 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
494 /* Find the previous rate that is in the rate mask */
496 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
497 if (rate_mask & mask) {
503 /* Find the next rate that is in the rate mask */
505 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
506 if (rate_mask & mask) {
512 return (high << 8) | low;
516 while (low != IWL_RATE_INVALID) {
517 low = iwl_rates[low].prev_rs;
518 if (low == IWL_RATE_INVALID)
520 if (rate_mask & (1 << low))
522 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
526 while (high != IWL_RATE_INVALID) {
527 high = iwl_rates[high].next_rs;
528 if (high == IWL_RATE_INVALID)
530 if (rate_mask & (1 << high))
532 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
535 return (high << 8) | low;
538 static int rs_get_lower_rate(struct iwl_rate_scale_priv *lq_data,
539 struct iwl_scale_tbl_info *tbl, u8 scale_index,
540 u8 ht_possible, struct iwl_rate *mcs_rate,
541 struct sta_info *sta)
543 u8 is_green = lq_data->is_green;
547 u8 switch_to_legacy = 0;
549 /* check if we need to switch from HT to legacy rates.
550 * assumption is that mandatory rates (1Mbps or 6Mbps)
551 * are always supported (spec demand) */
552 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
553 switch_to_legacy = 1;
554 scale_index = rs_ht_to_legacy[scale_index];
555 if (lq_data->phymode == MODE_IEEE80211A)
560 if ((tbl->antenna_type == ANT_BOTH) ||
561 (tbl->antenna_type == ANT_NONE))
562 tbl->antenna_type = ANT_MAIN;
568 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
570 /* mask with station rate restriction */
571 if (is_legacy(tbl->lq_type)) {
572 if (lq_data->phymode == (u8) MODE_IEEE80211A)
573 rate_mask = (u16)(rate_mask &
574 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
576 rate_mask = (u16)(rate_mask & sta->supp_rates);
579 /* if we did switched from HT to legacy check current rate */
580 if ((switch_to_legacy) &&
581 (rate_mask & (1 << scale_index))) {
582 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
586 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
587 low = high_low & 0xff;
589 if (low != IWL_RATE_INVALID)
590 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
592 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
597 static void rs_tx_status(void *priv_rate,
598 struct net_device *dev,
600 struct ieee80211_tx_status *tx_resp)
604 int rs_index, index = 0;
605 struct iwl_rate_scale_priv *lq;
606 struct iwl_link_quality_cmd *table;
607 struct sta_info *sta;
608 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
609 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
610 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
611 struct iwl_rate_scale_data *window = NULL;
612 struct iwl_rate_scale_data *search_win = NULL;
613 struct iwl_rate tx_mcs;
614 struct iwl_scale_tbl_info tbl_type;
615 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
617 u16 fc = le16_to_cpu(hdr->frame_control);
620 IWL_DEBUG_RATE("get frame ack response, update rate scale window\n");
622 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
625 retries = tx_resp->retry_count;
631 sta = sta_info_get(local, hdr->addr1);
633 if (!sta || !sta->rate_ctrl_priv) {
639 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
641 if (!priv->lq_mngr.lq_ready)
644 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
648 active_index = lq->active_tbl;
650 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
652 lq->antenna = lq->valid_antenna;
654 lq->antenna = lq->valid_antenna;
655 curr_tbl = &(lq->lq_info[active_index]);
656 search_tbl = &(lq->lq_info[(1 - active_index)]);
657 window = (struct iwl_rate_scale_data *)
659 search_win = (struct iwl_rate_scale_data *)
660 &(search_tbl->win[0]);
662 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
664 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
665 &tbl_type, &rs_index);
666 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
667 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
668 rs_index, tx_mcs.rate_n_flags);
674 (tx_mcs.rate_n_flags !=
675 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
676 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
678 le32_to_cpu(table->rs_table[0].rate_n_flags));
684 tx_mcs.rate_n_flags =
685 le32_to_cpu(table->rs_table[index].rate_n_flags);
686 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
687 &tbl_type, &rs_index);
689 if ((tbl_type.lq_type == search_tbl->lq_type) &&
690 (tbl_type.antenna_type == search_tbl->antenna_type) &&
691 (tbl_type.is_SGI == search_tbl->is_SGI)) {
692 if (search_tbl->expected_tpt)
693 tpt = search_tbl->expected_tpt[rs_index];
696 rs_collect_tx_data(search_win,
698 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
699 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
700 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
701 if (curr_tbl->expected_tpt)
702 tpt = curr_tbl->expected_tpt[rs_index];
705 rs_collect_tx_data(window, rs_index, tpt, 0);
714 if (!tx_resp->retry_count)
715 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
717 tx_mcs.rate_n_flags =
718 le32_to_cpu(table->rs_table[index].rate_n_flags);
720 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
721 &tbl_type, &rs_index);
723 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
728 if ((tbl_type.lq_type == search_tbl->lq_type) &&
729 (tbl_type.antenna_type == search_tbl->antenna_type) &&
730 (tbl_type.is_SGI == search_tbl->is_SGI)) {
731 if (search_tbl->expected_tpt)
732 tpt = search_tbl->expected_tpt[rs_index];
735 rs_collect_tx_data(search_win,
736 rs_index, tpt, status);
737 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
738 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
739 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
740 if (curr_tbl->expected_tpt)
741 tpt = curr_tbl->expected_tpt[rs_index];
744 rs_collect_tx_data(window, rs_index, tpt, status);
747 if (lq->stay_in_tbl) {
754 rs_rate_scale_perform(priv, dev, hdr, sta);
759 static u8 rs_is_ant_connected(u8 valid_antenna,
760 enum iwl_antenna_type antenna_type)
762 if (antenna_type == ANT_AUX)
763 return ((valid_antenna & 0x2) ? 1:0);
764 else if (antenna_type == ANT_MAIN)
765 return ((valid_antenna & 0x1) ? 1:0);
766 else if (antenna_type == ANT_BOTH) {
767 if ((valid_antenna & 0x3) == 0x3)
776 static u8 rs_is_other_ant_connected(u8 valid_antenna,
777 enum iwl_antenna_type antenna_type)
779 if (antenna_type == ANT_AUX)
780 return (rs_is_ant_connected(valid_antenna, ANT_MAIN));
782 return (rs_is_ant_connected(valid_antenna, ANT_AUX));
787 static void rs_set_stay_in_table(u8 is_legacy,
788 struct iwl_rate_scale_priv *lq_data)
790 IWL_DEBUG_HT("we are staying in the same table\n");
791 lq_data->stay_in_tbl = 1;
793 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
794 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
795 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
797 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
798 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
799 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
801 lq_data->table_count = 0;
802 lq_data->total_failed = 0;
803 lq_data->total_success = 0;
806 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
807 struct iwl_scale_tbl_info *tbl)
809 if (is_legacy(tbl->lq_type)) {
810 if (!is_a_band(tbl->lq_type))
811 tbl->expected_tpt = expected_tpt_G;
813 tbl->expected_tpt = expected_tpt_A;
814 } else if (is_siso(tbl->lq_type)) {
815 if (tbl->is_fat && !lq_data->is_dup)
817 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
819 tbl->expected_tpt = expected_tpt_siso40MHz;
820 else if (tbl->is_SGI)
821 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
823 tbl->expected_tpt = expected_tpt_siso20MHz;
825 } else if (is_mimo(tbl->lq_type)) {
826 if (tbl->is_fat && !lq_data->is_dup)
828 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
830 tbl->expected_tpt = expected_tpt_mimo40MHz;
831 else if (tbl->is_SGI)
832 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
834 tbl->expected_tpt = expected_tpt_mimo20MHz;
836 tbl->expected_tpt = expected_tpt_G;
839 #ifdef CONFIG_IWLWIFI_HT
840 static s32 rs_get_best_rate(struct iwl_priv *priv,
841 struct iwl_rate_scale_priv *lq_data,
842 struct iwl_scale_tbl_info *tbl,
843 u16 rate_mask, s8 index, s8 rate)
845 struct iwl_scale_tbl_info *active_tbl =
846 &(lq_data->lq_info[lq_data->active_tbl]);
847 s32 new_rate, high, low, start_hi;
848 s32 active_sr = active_tbl->win[index].success_ratio;
849 s32 *tpt_tbl = tbl->expected_tpt;
850 s32 active_tpt = active_tbl->expected_tpt[index];
853 new_rate = high = low = start_hi = IWL_RATE_INVALID;
856 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
858 low = high_low & 0xff;
859 high = (high_low >> 8) & 0xff;
861 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
862 ((active_sr > IWL_RATE_DECREASE_TH) &&
863 (active_sr <= IWL_RATE_HIGH_TH) &&
864 (tpt_tbl[rate] <= active_tpt))) ||
865 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
866 (tpt_tbl[rate] > active_tpt))) {
868 if (start_hi != IWL_RATE_INVALID) {
873 if (low != IWL_RATE_INVALID)
878 if (new_rate != IWL_RATE_INVALID)
880 else if (high != IWL_RATE_INVALID) {
892 #endif /* CONFIG_IWLWIFI_HT */
894 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
896 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
899 static int rs_switch_to_mimo(struct iwl_priv *priv,
900 struct iwl_rate_scale_priv *lq_data,
901 struct iwl_scale_tbl_info *tbl, int index)
904 #ifdef CONFIG_IWLWIFI_HT
907 s8 is_green = lq_data->is_green;
909 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
912 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
913 tbl->lq_type = LQ_MIMO;
914 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
917 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
920 if (!rs_is_both_ant_supp(lq_data->antenna))
924 tbl->is_dup = lq_data->is_dup;
926 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
932 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
936 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
941 rs_get_expected_tpt_table(lq_data, tbl);
943 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
945 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
946 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
948 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
950 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
951 tbl->current_rate.rate_n_flags, is_green);
953 #endif /*CONFIG_IWLWIFI_HT */
957 static int rs_switch_to_siso(struct iwl_priv *priv,
958 struct iwl_rate_scale_priv *lq_data,
959 struct iwl_scale_tbl_info *tbl, int index)
962 #ifdef CONFIG_IWLWIFI_HT
964 u8 is_green = lq_data->is_green;
967 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
968 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
972 tbl->is_dup = lq_data->is_dup;
973 tbl->lq_type = LQ_SISO;
975 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
978 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
984 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
988 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
996 rs_get_expected_tpt_table(lq_data, tbl);
997 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
999 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1000 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1001 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1005 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
1006 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
1007 tbl->current_rate.rate_n_flags, is_green);
1009 #endif /*CONFIG_IWLWIFI_HT */
1013 static int rs_move_legacy_other(struct iwl_priv *priv,
1014 struct iwl_rate_scale_priv *lq_data,
1018 struct iwl_scale_tbl_info *tbl =
1019 &(lq_data->lq_info[lq_data->active_tbl]);
1020 struct iwl_scale_tbl_info *search_tbl =
1021 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1022 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1023 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1024 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1025 u8 start_action = tbl->action;
1028 switch (tbl->action) {
1029 case IWL_LEGACY_SWITCH_ANTENNA:
1030 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1032 search_tbl->lq_type = LQ_NONE;
1033 lq_data->action_counter++;
1034 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1036 if (!rs_is_other_ant_connected(lq_data->antenna,
1040 memcpy(search_tbl, tbl, sz);
1042 rs_toggle_antenna(&(search_tbl->current_rate),
1044 rs_get_expected_tpt_table(lq_data, search_tbl);
1045 lq_data->search_better_tbl = 1;
1048 case IWL_LEGACY_SWITCH_SISO:
1049 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1050 memcpy(search_tbl, tbl, sz);
1051 search_tbl->lq_type = LQ_SISO;
1052 search_tbl->is_SGI = 0;
1053 search_tbl->is_fat = 0;
1054 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1057 lq_data->search_better_tbl = 1;
1058 lq_data->action_counter = 0;
1064 case IWL_LEGACY_SWITCH_MIMO:
1065 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1066 memcpy(search_tbl, tbl, sz);
1067 search_tbl->lq_type = LQ_MIMO;
1068 search_tbl->is_SGI = 0;
1069 search_tbl->is_fat = 0;
1070 search_tbl->antenna_type = ANT_BOTH;
1071 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1074 lq_data->search_better_tbl = 1;
1075 lq_data->action_counter = 0;
1082 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1083 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1085 if (tbl->action == start_action)
1093 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1094 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1099 static int rs_move_siso_to_other(struct iwl_priv *priv,
1100 struct iwl_rate_scale_priv *lq_data,
1104 u8 is_green = lq_data->is_green;
1105 struct iwl_scale_tbl_info *tbl =
1106 &(lq_data->lq_info[lq_data->active_tbl]);
1107 struct iwl_scale_tbl_info *search_tbl =
1108 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1109 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1110 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1111 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1112 u8 start_action = tbl->action;
1115 lq_data->action_counter++;
1116 switch (tbl->action) {
1117 case IWL_SISO_SWITCH_ANTENNA:
1118 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1119 search_tbl->lq_type = LQ_NONE;
1120 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1122 if (!rs_is_other_ant_connected(lq_data->antenna,
1126 memcpy(search_tbl, tbl, sz);
1127 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1128 rs_toggle_antenna(&(search_tbl->current_rate),
1130 lq_data->search_better_tbl = 1;
1134 case IWL_SISO_SWITCH_MIMO:
1135 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1136 memcpy(search_tbl, tbl, sz);
1137 search_tbl->lq_type = LQ_MIMO;
1138 search_tbl->is_SGI = 0;
1139 search_tbl->is_fat = 0;
1140 search_tbl->antenna_type = ANT_BOTH;
1141 rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
1144 lq_data->search_better_tbl = 1;
1149 case IWL_SISO_SWITCH_GI:
1150 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1151 memcpy(search_tbl, tbl, sz);
1152 search_tbl->action = 0;
1153 if (search_tbl->is_SGI)
1154 search_tbl->is_SGI = 0;
1156 search_tbl->is_SGI = 1;
1159 lq_data->search_better_tbl = 1;
1160 if ((tbl->lq_type == LQ_SISO) &&
1162 s32 tpt = lq_data->last_tpt / 100;
1163 if (((!tbl->is_fat) &&
1164 (tpt >= expected_tpt_siso20MHz[index])) ||
1166 (tpt >= expected_tpt_siso40MHz[index])))
1167 lq_data->search_better_tbl = 0;
1169 rs_get_expected_tpt_table(lq_data, search_tbl);
1170 rs_mcs_from_tbl(&search_tbl->current_rate,
1171 search_tbl, index, is_green);
1175 if (tbl->action > IWL_SISO_SWITCH_GI)
1176 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1178 if (tbl->action == start_action)
1185 if (tbl->action > IWL_SISO_SWITCH_GI)
1186 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1190 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1191 struct iwl_rate_scale_priv *lq_data,
1195 s8 is_green = lq_data->is_green;
1196 struct iwl_scale_tbl_info *tbl =
1197 &(lq_data->lq_info[lq_data->active_tbl]);
1198 struct iwl_scale_tbl_info *search_tbl =
1199 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1200 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1201 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1202 u8 start_action = tbl->action;
1205 lq_data->action_counter++;
1206 switch (tbl->action) {
1207 case IWL_MIMO_SWITCH_ANTENNA_A:
1208 case IWL_MIMO_SWITCH_ANTENNA_B:
1209 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1210 memcpy(search_tbl, tbl, sz);
1211 search_tbl->lq_type = LQ_SISO;
1212 search_tbl->is_SGI = 0;
1213 search_tbl->is_fat = 0;
1214 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1215 search_tbl->antenna_type = ANT_MAIN;
1217 search_tbl->antenna_type = ANT_AUX;
1219 rc = rs_switch_to_siso(priv, lq_data, search_tbl,
1222 lq_data->search_better_tbl = 1;
1227 case IWL_MIMO_SWITCH_GI:
1228 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1229 memcpy(search_tbl, tbl, sz);
1230 search_tbl->lq_type = LQ_MIMO;
1231 search_tbl->antenna_type = ANT_BOTH;
1232 search_tbl->action = 0;
1233 if (search_tbl->is_SGI)
1234 search_tbl->is_SGI = 0;
1236 search_tbl->is_SGI = 1;
1237 lq_data->search_better_tbl = 1;
1238 if ((tbl->lq_type == LQ_MIMO) &&
1240 s32 tpt = lq_data->last_tpt / 100;
1241 if (((!tbl->is_fat) &&
1242 (tpt >= expected_tpt_mimo20MHz[index])) ||
1244 (tpt >= expected_tpt_mimo40MHz[index])))
1245 lq_data->search_better_tbl = 0;
1247 rs_get_expected_tpt_table(lq_data, search_tbl);
1248 rs_mcs_from_tbl(&search_tbl->current_rate,
1249 search_tbl, index, is_green);
1254 if (tbl->action > IWL_MIMO_SWITCH_GI)
1255 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1257 if (tbl->action == start_action)
1264 if (tbl->action > IWL_MIMO_SWITCH_GI)
1265 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1270 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1272 struct iwl_scale_tbl_info *tbl;
1275 int flush_interval_passed = 0;
1277 active_tbl = lq_data->active_tbl;
1279 tbl = &(lq_data->lq_info[active_tbl]);
1281 if (lq_data->stay_in_tbl) {
1283 if (lq_data->flush_timer)
1284 flush_interval_passed =
1286 (unsigned long)(lq_data->flush_timer +
1287 IWL_RATE_SCALE_FLUSH_INTVL));
1289 flush_interval_passed = 0;
1290 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1291 (lq_data->total_success > lq_data->max_success_limit) ||
1292 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1293 && (flush_interval_passed))) {
1294 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1295 lq_data->total_failed,
1296 lq_data->total_success,
1297 flush_interval_passed);
1298 lq_data->stay_in_tbl = 0;
1299 lq_data->total_failed = 0;
1300 lq_data->total_success = 0;
1301 lq_data->flush_timer = 0;
1302 } else if (lq_data->table_count > 0) {
1303 lq_data->table_count++;
1304 if (lq_data->table_count >=
1305 lq_data->table_count_limit) {
1306 lq_data->table_count = 0;
1308 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1309 for (i = 0; i < IWL_RATE_COUNT; i++)
1310 rs_rate_scale_clear_window(
1315 if (!lq_data->stay_in_tbl) {
1316 for (i = 0; i < IWL_RATE_COUNT; i++)
1317 rs_rate_scale_clear_window(&(tbl->win[i]));
1322 static void rs_rate_scale_perform(struct iwl_priv *priv,
1323 struct net_device *dev,
1324 struct ieee80211_hdr *hdr,
1325 struct sta_info *sta)
1327 int low = IWL_RATE_INVALID;
1328 int high = IWL_RATE_INVALID;
1331 struct iwl_rate_scale_data *window = NULL;
1332 int current_tpt = IWL_INVALID_VALUE;
1333 int low_tpt = IWL_INVALID_VALUE;
1334 int high_tpt = IWL_INVALID_VALUE;
1336 s8 scale_action = 0;
1339 struct iwl_rate_scale_priv *lq_data;
1340 struct iwl_scale_tbl_info *tbl, *tbl1;
1341 u16 rate_scale_index_msk = 0;
1342 struct iwl_rate mcs_rate;
1348 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1350 fc = le16_to_cpu(hdr->frame_control);
1351 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1352 /* Send management frames and broadcast/multicast data using
1354 /* TODO: this could probably be improved.. */
1358 if (!sta || !sta->rate_ctrl_priv)
1361 if (!priv->lq_mngr.lq_ready) {
1362 IWL_DEBUG_RATE("still rate scaling not ready\n");
1365 lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1367 if (!lq_data->search_better_tbl)
1368 active_tbl = lq_data->active_tbl;
1370 active_tbl = 1 - lq_data->active_tbl;
1372 tbl = &(lq_data->lq_info[active_tbl]);
1373 is_green = lq_data->is_green;
1375 index = sta->last_txrate;
1377 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1380 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1383 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1385 /* mask with station rate restriction */
1386 if (is_legacy(tbl->lq_type)) {
1387 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1388 rate_scale_index_msk = (u16) (rate_mask &
1389 (sta->supp_rates << IWL_FIRST_OFDM_RATE));
1391 rate_scale_index_msk = (u16) (rate_mask &
1395 rate_scale_index_msk = rate_mask;
1397 if (!rate_scale_index_msk)
1398 rate_scale_index_msk = rate_mask;
1400 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1401 index = IWL_INVALID_VALUE;
1404 /* get the lowest availabe rate */
1405 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1406 if ((1 << i) & rate_scale_index_msk)
1410 if (index == IWL_INVALID_VALUE) {
1411 IWL_WARNING("Can not find a suitable rate\n");
1416 if (!tbl->expected_tpt)
1417 rs_get_expected_tpt_table(lq_data, tbl);
1419 window = &(tbl->win[index]);
1421 fail_count = window->counter - window->success_counter;
1422 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1423 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1424 || (tbl->expected_tpt == NULL)) {
1425 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1427 window->success_counter, window->counter, index);
1428 window->average_tpt = IWL_INVALID_VALUE;
1429 rs_stay_in_table(lq_data);
1431 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1432 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1433 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1438 window->average_tpt = ((window->success_ratio *
1439 tbl->expected_tpt[index] + 64) / 128);
1441 if (lq_data->search_better_tbl) {
1442 int success_limit = IWL_RATE_SCALE_SWITCH;
1444 if ((window->success_ratio > success_limit) ||
1445 (window->average_tpt > lq_data->last_tpt)) {
1446 if (!is_legacy(tbl->lq_type)) {
1447 IWL_DEBUG_HT("LQ: we are switching to HT"
1448 " rate suc %d current tpt %d"
1450 window->success_ratio,
1451 window->average_tpt,
1453 lq_data->enable_counter = 1;
1455 lq_data->active_tbl = active_tbl;
1456 current_tpt = window->average_tpt;
1458 tbl->lq_type = LQ_NONE;
1459 active_tbl = lq_data->active_tbl;
1460 tbl = &(lq_data->lq_info[active_tbl]);
1462 index = iwl_rate_index_from_plcp(
1463 tbl->current_rate.rate_n_flags);
1466 current_tpt = lq_data->last_tpt;
1467 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1469 lq_data->search_better_tbl = 0;
1474 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1476 low = high_low & 0xff;
1477 high = (high_low >> 8) & 0xff;
1479 current_tpt = window->average_tpt;
1481 if (low != IWL_RATE_INVALID)
1482 low_tpt = tbl->win[low].average_tpt;
1484 if (high != IWL_RATE_INVALID)
1485 high_tpt = tbl->win[high].average_tpt;
1490 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1491 (current_tpt == 0)) {
1492 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1494 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1495 (high_tpt == IWL_INVALID_VALUE))
1497 else if ((low_tpt != IWL_INVALID_VALUE) &&
1498 (high_tpt != IWL_INVALID_VALUE) &&
1499 (low_tpt < current_tpt) &&
1500 (high_tpt < current_tpt))
1503 if (high_tpt != IWL_INVALID_VALUE) {
1504 if (high_tpt > current_tpt)
1508 ("decrease rate because of high tpt\n");
1511 } else if (low_tpt != IWL_INVALID_VALUE) {
1512 if (low_tpt > current_tpt) {
1514 ("decrease rate because of low tpt\n");
1521 if (scale_action == -1) {
1522 if ((low != IWL_RATE_INVALID) &&
1523 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1524 (current_tpt > (100 * tbl->expected_tpt[low]))))
1526 } else if ((scale_action == 1) &&
1527 (window->success_ratio < IWL_RATE_INCREASE_TH))
1530 switch (scale_action) {
1532 if (low != IWL_RATE_INVALID) {
1538 if (high != IWL_RATE_INVALID) {
1549 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1550 "high %d type %d\n",
1551 index, scale_action, low, high, tbl->lq_type);
1555 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1556 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq, sta);
1557 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1559 rs_stay_in_table(lq_data);
1561 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1562 lq_data->last_tpt = current_tpt;
1564 if (is_legacy(tbl->lq_type))
1565 rs_move_legacy_other(priv, lq_data, index);
1566 else if (is_siso(tbl->lq_type))
1567 rs_move_siso_to_other(priv, lq_data, index);
1569 rs_move_mimo_to_other(priv, lq_data, index);
1571 if (lq_data->search_better_tbl) {
1572 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1573 for (i = 0; i < IWL_RATE_COUNT; i++)
1574 rs_rate_scale_clear_window(&(tbl->win[i]));
1576 index = iwl_rate_index_from_plcp(
1577 tbl->current_rate.rate_n_flags);
1579 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1580 tbl->current_rate.rate_n_flags, index);
1581 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1582 &(lq_data->lq), sta);
1583 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1585 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1587 if (is_legacy(tbl1->lq_type) &&
1588 #ifdef CONFIG_IWLWIFI_HT
1589 !priv->current_assoc_ht.is_ht &&
1591 (lq_data->action_counter >= 1)) {
1592 lq_data->action_counter = 0;
1593 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1594 rs_set_stay_in_table(1, lq_data);
1597 if (lq_data->enable_counter &&
1598 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1599 #ifdef CONFIG_IWLWIFI_HT_AGG
1600 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1601 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1602 priv->lq_mngr.agg_ctrl.tid_retry =
1604 schedule_work(&priv->agg_work);
1606 #endif /*CONFIG_IWLWIFI_HT_AGG */
1607 lq_data->action_counter = 0;
1608 rs_set_stay_in_table(0, lq_data);
1611 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1612 lq_data->flush_timer = jiffies;
1616 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1618 sta->last_txrate = i;
1620 /* sta->txrate is an index to A mode rates which start
1621 * at IWL_FIRST_OFDM_RATE
1623 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1624 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1632 static void rs_initialize_lq(struct iwl_priv *priv,
1633 struct sta_info *sta)
1636 struct iwl_rate_scale_priv *lq;
1637 struct iwl_scale_tbl_info *tbl;
1640 u8 use_green = rs_use_green(priv);
1641 struct iwl_rate mcs_rate;
1643 if (!sta || !sta->rate_ctrl_priv)
1646 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1647 i = sta->last_txrate;
1649 if ((lq->lq.sta_id == 0xff) &&
1650 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1653 if (!lq->search_better_tbl)
1654 active_tbl = lq->active_tbl;
1656 active_tbl = 1 - lq->active_tbl;
1658 tbl = &(lq->lq_info[active_tbl]);
1660 if ((i < 0) || (i >= IWL_RATE_COUNT))
1663 mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1664 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1665 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1667 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1668 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1670 tbl->antenna_type = ANT_AUX;
1671 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1672 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1673 rs_toggle_antenna(&mcs_rate, tbl),
1675 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1676 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1677 rs_get_expected_tpt_table(lq, tbl);
1678 rs_fill_link_cmd(lq, &mcs_rate, &(lq->lq), sta);
1679 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1684 static struct ieee80211_rate *rs_get_lowest_rate(struct ieee80211_local
1687 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1690 for (i = 0; i < mode->num_rates; i++) {
1691 struct ieee80211_rate *rate = &mode->rates[i];
1693 if (rate->flags & IEEE80211_RATE_SUPPORTED)
1697 return &mode->rates[0];
1700 static struct ieee80211_rate *rs_get_rate(void *priv_rate,
1701 struct net_device *dev,
1702 struct sk_buff *skb,
1703 struct rate_control_extra
1708 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1709 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1710 struct sta_info *sta;
1712 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1713 struct iwl_rate_scale_priv *lq;
1715 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1717 memset(extra, 0, sizeof(*extra));
1719 fc = le16_to_cpu(hdr->frame_control);
1720 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1721 /* Send management frames and broadcast/multicast data using
1723 /* TODO: this could probably be improved.. */
1724 return rs_get_lowest_rate(local);
1727 sta = sta_info_get(local, hdr->addr1);
1729 if (!sta || !sta->rate_ctrl_priv) {
1732 return rs_get_lowest_rate(local);
1735 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1736 i = sta->last_txrate;
1738 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1739 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1740 DECLARE_MAC_BUF(mac);
1742 if (sta_id == IWL_INVALID_STATION) {
1743 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1744 print_mac(mac, hdr->addr1));
1745 sta_id = iwl_add_station(priv,
1746 hdr->addr1, 0, CMD_ASYNC);
1748 if ((sta_id != IWL_INVALID_STATION)) {
1749 lq->lq.sta_id = sta_id;
1750 lq->lq.rs_table[0].rate_n_flags = 0;
1751 lq->ibss_sta_added = 1;
1752 rs_initialize_lq(priv, sta);
1754 if (!lq->ibss_sta_added)
1760 if ((i < 0) || (i > IWL_RATE_COUNT))
1761 return rs_get_lowest_rate(local);
1763 return &priv->ieee_rates[i];
1766 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1768 struct iwl_rate_scale_priv *crl;
1771 IWL_DEBUG_RATE("create station rate scale window\n");
1773 crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1778 memset(crl, 0, sizeof(struct iwl_rate_scale_priv));
1779 crl->lq.sta_id = 0xff;
1781 for (j = 0; j < LQ_SIZE; j++)
1782 for (i = 0; i < IWL_RATE_COUNT; i++)
1783 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1788 static void rs_rate_init(void *priv_rate, void *priv_sta,
1789 struct ieee80211_local *local,
1790 struct sta_info *sta)
1793 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1794 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1795 struct iwl_rate_scale_priv *crl = priv_sta;
1797 memset(crl, 0, sizeof(struct iwl_rate_scale_priv));
1799 crl->lq.sta_id = 0xff;
1800 crl->flush_timer = 0;
1802 for (j = 0; j < LQ_SIZE; j++)
1803 for (i = 0; i < IWL_RATE_COUNT; i++)
1804 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1806 IWL_DEBUG_RATE("rate scale global init\n");
1807 /* TODO: what is a good starting rate for STA? About middle? Maybe not
1808 * the lowest or the highest rate.. Could consider using RSSI from
1809 * previous packets? Need to have IEEE 802.1X auth succeed immediately
1812 crl->ibss_sta_added = 0;
1813 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1814 u8 sta_id = iwl_hw_find_station(priv, sta->addr);
1815 DECLARE_MAC_BUF(mac);
1817 /* for IBSS the call are from tasklet */
1818 IWL_DEBUG_HT("LQ: ADD station %s\n",
1819 print_mac(mac, sta->addr));
1821 if (sta_id == IWL_INVALID_STATION) {
1822 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1823 print_mac(mac, sta->addr));
1824 sta_id = iwl_add_station(priv,
1825 sta->addr, 0, CMD_ASYNC);
1827 if ((sta_id != IWL_INVALID_STATION)) {
1828 crl->lq.sta_id = sta_id;
1829 crl->lq.rs_table[0].rate_n_flags = 0;
1831 /* FIXME: this is w/a remove it later */
1832 priv->assoc_station_added = 1;
1835 for (i = 0; i < mode->num_rates; i++) {
1836 if ((sta->supp_rates & BIT(i)) &&
1837 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1840 sta->last_txrate = sta->txrate;
1841 /* For MODE_IEEE80211A mode cck rate are at end
1844 if (local->hw.conf.phymode == MODE_IEEE80211A)
1845 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1847 crl->is_dup = priv->is_dup;
1848 crl->valid_antenna = priv->valid_antenna;
1849 crl->antenna = priv->antenna;
1850 crl->is_green = rs_use_green(priv);
1851 crl->active_rate = priv->active_rate;
1852 crl->active_rate &= ~(0x1000);
1853 crl->active_rate_basic = priv->active_rate_basic;
1854 crl->phymode = priv->phymode;
1855 #ifdef CONFIG_IWLWIFI_HT
1856 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1857 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1858 crl->active_siso_rate &= ~((u16)0x2);
1859 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1861 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1862 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1863 crl->active_mimo_rate &= ~((u16)0x2);
1864 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1865 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1866 crl->active_mimo_rate);
1867 #endif /*CONFIG_IWLWIFI_HT*/
1869 if (priv->assoc_station_added)
1870 priv->lq_mngr.lq_ready = 1;
1872 rs_initialize_lq(priv, sta);
1875 static int rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
1876 struct iwl_rate *tx_mcs,
1877 struct iwl_link_quality_cmd *lq_cmd,
1878 struct sta_info *sta)
1883 u8 ant_toggle_count = 0;
1884 u8 use_ht_possible = 1;
1885 u8 repeat_cur_rate = 0;
1886 struct iwl_rate new_rate;
1887 struct iwl_scale_tbl_info tbl_type = { 0 };
1889 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1890 &tbl_type, &rate_idx);
1892 if (is_legacy(tbl_type.lq_type)) {
1893 ant_toggle_count = 1;
1894 repeat_cur_rate = IWL_NUMBER_TRY;
1896 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1898 lq_cmd->general_params.mimo_delimiter =
1899 is_mimo(tbl_type.lq_type) ? 1 : 0;
1900 lq_cmd->rs_table[index].rate_n_flags =
1901 cpu_to_le32(tx_mcs->rate_n_flags);
1902 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1904 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1905 lq_cmd->general_params.single_stream_ant_msk = 1;
1907 lq_cmd->general_params.single_stream_ant_msk = 2;
1912 while (index < LINK_QUAL_MAX_RETRY_NUM) {
1913 while (repeat_cur_rate && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1914 if (is_legacy(tbl_type.lq_type)) {
1915 if (ant_toggle_count <
1916 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1919 rs_toggle_antenna(&new_rate, &tbl_type);
1920 ant_toggle_count = 1;
1923 lq_cmd->rs_table[index].rate_n_flags =
1924 cpu_to_le32(new_rate.rate_n_flags);
1929 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1932 if (is_mimo(tbl_type.lq_type))
1933 lq_cmd->general_params.mimo_delimiter = index;
1935 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1936 use_ht_possible, &new_rate, sta);
1938 if (is_legacy(tbl_type.lq_type)) {
1939 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1942 rs_toggle_antenna(&new_rate, &tbl_type);
1943 ant_toggle_count = 1;
1945 repeat_cur_rate = IWL_NUMBER_TRY;
1947 repeat_cur_rate = IWL_HT_NUMBER_TRY;
1949 use_ht_possible = 0;
1951 lq_cmd->rs_table[index].rate_n_flags =
1952 cpu_to_le32(new_rate.rate_n_flags);
1953 /* lq_cmd->rs_table[index].rate_n_flags = 0x800d; */
1959 /* lq_cmd->rs_table[0].rate_n_flags = 0x800d; */
1961 lq_cmd->general_params.dual_stream_ant_msk = 3;
1962 lq_cmd->agg_params.agg_dis_start_th = 3;
1963 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1967 static void *rs_alloc(struct ieee80211_local *local)
1969 return local->hw.priv;
1971 /* rate scale requires free function to be implemented */
1972 static void rs_free(void *priv_rate)
1977 static void rs_clear(void *priv_rate)
1979 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1981 IWL_DEBUG_RATE("enter\n");
1983 priv->lq_mngr.lq_ready = 0;
1984 #ifdef CONFIG_IWLWIFI_HT
1985 #ifdef CONFIG_IWLWIFI_HT_AGG
1986 if (priv->lq_mngr.agg_ctrl.granted_ba)
1987 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
1988 #endif /*CONFIG_IWLWIFI_HT_AGG */
1989 #endif /* CONFIG_IWLWIFI_HT */
1991 IWL_DEBUG_RATE("leave\n");
1994 static void rs_free_sta(void *priv, void *priv_sta)
1996 struct iwl_rate_scale_priv *rs_priv = priv_sta;
1998 IWL_DEBUG_RATE("enter\n");
2000 IWL_DEBUG_RATE("leave\n");
2004 static struct rate_control_ops rs_ops = {
2007 .tx_status = rs_tx_status,
2008 .get_rate = rs_get_rate,
2009 .rate_init = rs_rate_init,
2013 .alloc_sta = rs_alloc_sta,
2014 .free_sta = rs_free_sta,
2017 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2019 struct ieee80211_local *local = hw_to_local(hw);
2020 struct iwl_priv *priv = hw->priv;
2021 struct iwl_rate_scale_priv *rs_priv;
2022 struct sta_info *sta;
2024 u32 samples = 0, success = 0, good = 0;
2025 unsigned long now = jiffies;
2027 u8 lq_type, antenna;
2029 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2030 if (!sta || !sta->rate_ctrl_priv) {
2033 IWL_DEBUG_RATE("leave - no private rate data!\n");
2035 IWL_DEBUG_RATE("leave - no station!\n");
2036 return sprintf(buf, "station %d not found\n", sta_id);
2039 rs_priv = (void *)sta->rate_ctrl_priv;
2041 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2042 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2044 if (is_legacy(lq_type))
2045 i = IWL_RATE_54M_INDEX;
2047 i = IWL_RATE_60M_INDEX;
2051 int active = rs_priv->active_tbl;
2054 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2056 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2057 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2059 (rs_priv->lq_info[active].win[i].data & mask)
2062 samples += rs_priv->lq_info[active].win[i].counter;
2063 good += rs_priv->lq_info[active].win[i].success_counter;
2064 success += rs_priv->lq_info[active].win[i].success_counter *
2067 if (rs_priv->lq_info[active].win[i].stamp) {
2069 jiffies_to_msecs(now -
2070 rs_priv->lq_info[active].win[i].stamp);
2072 if (delta > max_time)
2075 count += sprintf(&buf[count], "%5dms\n", delta);
2077 buf[count++] = '\n';
2079 j = iwl_get_prev_ieee_rate(i);
2085 /* Display the average rate of all samples taken.
2087 * NOTE: We multiple # of samples by 2 since the IEEE measurement
2088 * added from iwl_rates is actually 2X the rate */
2090 count += sprintf(&buf[count],
2091 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2092 "%3d%% success (%d good packets over %d tries)\n",
2093 success / (2 * samples), (success * 5 / samples) % 10,
2094 max_time, good * 100 / samples, good, samples);
2096 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2097 count += sprintf(&buf[count], "\nrate scale type %d anntena %d "
2098 "active_search %d rate index %d\n", lq_type, antenna,
2099 rs_priv->search_better_tbl, sta->last_txrate);
2105 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2107 struct iwl_priv *priv = hw->priv;
2109 priv->lq_mngr.lq_ready = 1;
2112 void iwl_rate_control_register(struct ieee80211_hw *hw)
2114 ieee80211_rate_control_register(&rs_ops);
2117 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2119 ieee80211_rate_control_unregister(&rs_ops);