1 /******************************************************************************
3 * Copyright(c) 2005 - 2008 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>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/delay.h>
36 #include <linux/workqueue.h>
38 #include "../net/mac80211/rate.h"
42 #include "iwl-helpers.h"
44 #define RS_NAME "iwl-4965-rs"
46 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
47 #define IWL_NUMBER_TRY 1
48 #define IWL_HT_NUMBER_TRY 3
50 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
54 /* max time to accum history 2 seconds */
55 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
57 static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
67 static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
71 /*ANT_AB -> */ ANT_BC,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
79 * struct iwl4965_rate_scale_data -- tx success history for one rate
81 struct iwl4965_rate_scale_data {
82 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
91 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
93 * There are two of these in struct iwl4965_lq_sta,
94 * one for "active", and one for "search".
96 struct iwl4965_scale_tbl_info {
97 enum iwl_table_type lq_type;
99 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
104 u32 current_rate; /* rate_n_flags, uCode API format */
105 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
108 #ifdef CONFIG_IWL4965_HT
110 struct iwl4965_traffic_load {
111 unsigned long time_stamp; /* age of the oldest statistics */
112 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
114 u32 total; /* total num of packets during the
115 * last TID_MAX_TIME_DIFF */
116 u8 queue_count; /* number of queues that has
117 * been used since the last cleanup */
118 u8 head; /* start of the circular buffer */
121 #endif /* CONFIG_IWL4965_HT */
124 * struct iwl4965_lq_sta -- driver's rate scaling private structure
126 * Pointer to this gets passed back and forth between driver and mac80211.
128 struct iwl4965_lq_sta {
129 u8 active_tbl; /* index of active table, range 0-1 */
130 u8 enable_counter; /* indicates HT mode */
131 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
132 u8 search_better_tbl; /* 1: currently trying alternate mode */
135 /* The following determine when to search for a new mode */
136 u32 table_count_limit;
137 u32 max_failure_limit; /* # failed frames before new search */
138 u32 max_success_limit; /* # successful frames before new search */
140 u32 total_failed; /* total failed frames, any/all rates */
141 u32 total_success; /* total successful frames, any/all rates */
142 u32 flush_timer; /* time staying in mode before new search */
144 u8 action_counter; /* # mode-switch actions tried */
147 enum ieee80211_band band;
150 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
152 u16 active_legacy_rate;
153 u16 active_siso_rate;
154 u16 active_mimo2_rate;
155 u16 active_mimo3_rate;
156 u16 active_rate_basic;
158 struct iwl_link_quality_cmd lq;
159 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
160 #ifdef CONFIG_IWL4965_HT
161 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
164 #ifdef CONFIG_MAC80211_DEBUGFS
165 struct dentry *rs_sta_dbgfs_scale_table_file;
166 struct dentry *rs_sta_dbgfs_stats_table_file;
167 #ifdef CONFIG_IWL4965_HT
168 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
171 struct iwl_priv *drv;
175 static void rs_rate_scale_perform(struct iwl_priv *priv,
176 struct net_device *dev,
177 struct ieee80211_hdr *hdr,
178 struct sta_info *sta);
179 static void rs_fill_link_cmd(const struct iwl_priv *priv,
180 struct iwl4965_lq_sta *lq_sta,
182 struct iwl_link_quality_cmd *tbl);
185 #ifdef CONFIG_MAC80211_DEBUGFS
186 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
187 u32 *rate_n_flags, int index);
189 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
190 u32 *rate_n_flags, int index)
195 * Expected throughput metrics for following rates:
196 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
197 * "G" is the only table that supports CCK (the first 4 rates).
199 /*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
200 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
201 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
204 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
205 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
208 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
209 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
212 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
213 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
216 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
217 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
220 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
221 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
224 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
225 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
228 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
229 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
232 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
233 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
236 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
237 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
240 static inline u8 rs_extract_rate(u32 rate_n_flags)
242 return (u8)(rate_n_flags & 0xFF);
245 static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
248 window->success_counter = 0;
249 window->success_ratio = IWL_INVALID_VALUE;
251 window->average_tpt = IWL_INVALID_VALUE;
255 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
257 return ((ant_type & valid_antenna) == ant_type);
260 #ifdef CONFIG_IWL4965_HT
262 * removes the old data from the statistics. All data that is older than
263 * TID_MAX_TIME_DIFF, will be deleted.
265 static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
267 /* The oldest age we want to keep */
268 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
270 while (tl->queue_count &&
271 (tl->time_stamp < oldest_time)) {
272 tl->total -= tl->packet_count[tl->head];
273 tl->packet_count[tl->head] = 0;
274 tl->time_stamp += TID_QUEUE_CELL_SPACING;
277 if (tl->head >= TID_QUEUE_MAX_SIZE)
283 * increment traffic load value for tid and also remove
284 * any old values if passed the certain time period
286 static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
288 u32 curr_time = jiffies_to_msecs(jiffies);
291 struct iwl4965_traffic_load *tl = NULL;
293 if (tid >= TID_MAX_LOAD_COUNT)
296 tl = &lq_data->load[tid];
298 curr_time -= curr_time % TID_ROUND_VALUE;
300 /* Happens only for the first packet. Initialize the data */
301 if (!(tl->queue_count)) {
303 tl->time_stamp = curr_time;
306 tl->packet_count[0] = 1;
310 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
311 index = time_diff / TID_QUEUE_CELL_SPACING;
313 /* The history is too long: remove data that is older than */
314 /* TID_MAX_TIME_DIFF */
315 if (index >= TID_QUEUE_MAX_SIZE)
316 rs_tl_rm_old_stats(tl, curr_time);
318 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
319 tl->packet_count[index] = tl->packet_count[index] + 1;
320 tl->total = tl->total + 1;
322 if ((index + 1) > tl->queue_count)
323 tl->queue_count = index + 1;
327 get the traffic load value for tid
329 static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
331 u32 curr_time = jiffies_to_msecs(jiffies);
334 struct iwl4965_traffic_load *tl = NULL;
336 if (tid >= TID_MAX_LOAD_COUNT)
339 tl = &(lq_data->load[tid]);
341 curr_time -= curr_time % TID_ROUND_VALUE;
343 if (!(tl->queue_count))
346 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
347 index = time_diff / TID_QUEUE_CELL_SPACING;
349 /* The history is too long: remove data that is older than */
350 /* TID_MAX_TIME_DIFF */
351 if (index >= TID_QUEUE_MAX_SIZE)
352 rs_tl_rm_old_stats(tl, curr_time);
357 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
358 struct iwl4965_lq_sta *lq_data, u8 tid,
359 struct sta_info *sta)
362 DECLARE_MAC_BUF(mac);
364 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
365 state = sta->ampdu_mlme.tid_state_tx[tid];
366 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
368 if (state == HT_AGG_STATE_IDLE &&
369 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
370 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
371 print_mac(mac, sta->addr), tid);
372 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
376 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
377 struct iwl4965_lq_sta *lq_data,
378 struct sta_info *sta)
380 if ((tid < TID_MAX_LOAD_COUNT))
381 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
382 else if (tid == IWL_AGG_ALL_TID)
383 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
384 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
387 #endif /* CONFIG_IWLWIFI_HT */
389 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
391 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
392 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
393 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
397 * rs_collect_tx_data - Update the success/failure sliding window
399 * We keep a sliding window of the last 62 packets transmitted
400 * at this rate. window->data contains the bitmask of successful
403 static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
404 int scale_index, s32 tpt, int retries,
407 struct iwl4965_rate_scale_data *window = NULL;
408 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
411 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
414 /* Select data for current tx bit rate */
415 window = &(windows[scale_index]);
418 * Keep track of only the latest 62 tx frame attempts in this rate's
419 * history window; anything older isn't really relevant any more.
420 * If we have filled up the sliding window, drop the oldest attempt;
421 * if the oldest attempt (highest bit in bitmap) shows "success",
422 * subtract "1" from the success counter (this is the main reason
423 * we keep these bitmaps!).
425 while (retries > 0) {
426 if (window->counter >= IWL_RATE_MAX_WINDOW) {
428 /* remove earliest */
429 window->counter = IWL_RATE_MAX_WINDOW - 1;
431 if (window->data & mask) {
432 window->data &= ~mask;
433 window->success_counter--;
437 /* Increment frames-attempted counter */
440 /* Shift bitmap by one frame (throw away oldest history),
441 * OR in "1", and increment "success" if this
442 * frame was successful. */
445 window->success_counter++;
453 /* Calculate current success ratio, avoid divide-by-0! */
454 if (window->counter > 0)
455 window->success_ratio = 128 * (100 * window->success_counter)
458 window->success_ratio = IWL_INVALID_VALUE;
460 fail_count = window->counter - window->success_counter;
462 /* Calculate average throughput, if we have enough history. */
463 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
464 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
465 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
467 window->average_tpt = IWL_INVALID_VALUE;
469 /* Tag this window as having been updated */
470 window->stamp = jiffies;
476 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
478 /* FIXME:RS:remove this function and put the flags statically in the table */
479 static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
480 int index, u8 use_green)
482 u32 rate_n_flags = 0;
484 if (is_legacy(tbl->lq_type)) {
485 rate_n_flags = iwl4965_rates[index].plcp;
486 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
487 rate_n_flags |= RATE_MCS_CCK_MSK;
489 } else if (is_Ht(tbl->lq_type)) {
490 if (index > IWL_LAST_OFDM_RATE) {
491 IWL_ERROR("invalid HT rate index %d\n", index);
492 index = IWL_LAST_OFDM_RATE;
494 rate_n_flags = RATE_MCS_HT_MSK;
496 if (is_siso(tbl->lq_type))
497 rate_n_flags |= iwl4965_rates[index].plcp_siso;
498 else if (is_mimo2(tbl->lq_type))
499 rate_n_flags |= iwl4965_rates[index].plcp_mimo2;
501 rate_n_flags |= iwl4965_rates[index].plcp_mimo3;
503 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
506 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
507 RATE_MCS_ANT_ABC_MSK);
509 if (is_Ht(tbl->lq_type)) {
512 rate_n_flags |= RATE_MCS_DUP_MSK;
514 rate_n_flags |= RATE_MCS_FAT_MSK;
517 rate_n_flags |= RATE_MCS_SGI_MSK;
520 rate_n_flags |= RATE_MCS_GF_MSK;
521 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
522 rate_n_flags &= ~RATE_MCS_SGI_MSK;
523 IWL_ERROR("GF was set with SGI:SISO\n");
531 * Interpret uCode API's rate_n_flags format,
532 * fill "search" or "active" tx mode table.
534 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
535 enum ieee80211_band band,
536 struct iwl4965_scale_tbl_info *tbl,
539 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
540 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
543 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
545 if (*rate_idx == IWL_RATE_INVALID) {
549 tbl->is_SGI = 0; /* default legacy setup */
552 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
553 tbl->lq_type = LQ_NONE;
555 /* legacy rate format */
556 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
557 if (num_of_ant == 1) {
558 if (band == IEEE80211_BAND_5GHZ)
565 if (rate_n_flags & RATE_MCS_SGI_MSK)
568 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
569 (rate_n_flags & RATE_MCS_DUP_MSK))
572 if (rate_n_flags & RATE_MCS_DUP_MSK)
575 mcs = rs_extract_rate(rate_n_flags);
578 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
580 tbl->lq_type = LQ_SISO; /*else NONE*/
582 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
584 tbl->lq_type = LQ_MIMO2;
588 tbl->lq_type = LQ_MIMO3;
594 /* switch to another antenna/antennas and return 1 */
595 /* if no other valid antenna found, return 0 */
596 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
597 struct iwl4965_scale_tbl_info *tbl)
601 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
604 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
607 new_ant_type = ant_toggle_lookup[tbl->ant_type];
609 while ((new_ant_type != tbl->ant_type) &&
610 !rs_is_valid_ant(valid_ant, new_ant_type))
611 new_ant_type = ant_toggle_lookup[new_ant_type];
613 if (new_ant_type == tbl->ant_type)
616 tbl->ant_type = new_ant_type;
617 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
618 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
622 /* FIXME:RS: in 4965 we don't use greenfield at all */
623 /* FIXME:RS: don't use greenfield for now in TX */
624 /* #ifdef CONFIG_IWL4965_HT */
626 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
628 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
629 priv->current_ht_config.is_green_field &&
630 !priv->current_ht_config.non_GF_STA_present);
633 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
637 #endif /* CONFIG_IWL4965_HT */
640 * rs_get_supported_rates - get the available rates
642 * if management frame or broadcast frame only return
643 * basic available rates.
646 static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
647 struct ieee80211_hdr *hdr,
648 enum iwl_table_type rate_type)
650 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
651 lq_sta->active_rate_basic)
652 return lq_sta->active_rate_basic;
654 if (is_legacy(rate_type)) {
655 return lq_sta->active_legacy_rate;
657 if (is_siso(rate_type))
658 return lq_sta->active_siso_rate;
659 else if (is_mimo2(rate_type))
660 return lq_sta->active_mimo2_rate;
662 return lq_sta->active_mimo3_rate;
666 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
668 u8 high = IWL_RATE_INVALID;
669 u8 low = IWL_RATE_INVALID;
671 /* 802.11A or ht walks to the next literal adjacent rate in
673 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
677 /* Find the previous rate that is in the rate mask */
679 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
680 if (rate_mask & mask) {
686 /* Find the next rate that is in the rate mask */
688 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
689 if (rate_mask & mask) {
695 return (high << 8) | low;
699 while (low != IWL_RATE_INVALID) {
700 low = iwl4965_rates[low].prev_rs;
701 if (low == IWL_RATE_INVALID)
703 if (rate_mask & (1 << low))
705 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
709 while (high != IWL_RATE_INVALID) {
710 high = iwl4965_rates[high].next_rs;
711 if (high == IWL_RATE_INVALID)
713 if (rate_mask & (1 << high))
715 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
718 return (high << 8) | low;
721 static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
722 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
728 u8 switch_to_legacy = 0;
729 u8 is_green = lq_sta->is_green;
731 /* check if we need to switch from HT to legacy rates.
732 * assumption is that mandatory rates (1Mbps or 6Mbps)
733 * are always supported (spec demand) */
734 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
735 switch_to_legacy = 1;
736 scale_index = rs_ht_to_legacy[scale_index];
737 if (lq_sta->band == IEEE80211_BAND_5GHZ)
742 if (num_of_ant(tbl->ant_type) > 1)
743 tbl->ant_type = ANT_A;/*FIXME:RS*/
749 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
751 /* Mask with station rate restriction */
752 if (is_legacy(tbl->lq_type)) {
753 /* supp_rates has no CCK bits in A mode */
754 if (lq_sta->band == IEEE80211_BAND_5GHZ)
755 rate_mask = (u16)(rate_mask &
756 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
758 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
761 /* If we switched from HT to legacy, check current rate */
762 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
767 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
768 low = high_low & 0xff;
770 if (low == IWL_RATE_INVALID)
774 return rate_n_flags_from_tbl(tbl, low, is_green);
778 * mac80211 sends us Tx status
780 static void rs_tx_status(void *priv_rate, struct net_device *dev,
782 struct ieee80211_tx_status *tx_resp)
786 int rs_index, index = 0;
787 struct iwl4965_lq_sta *lq_sta;
788 struct iwl_link_quality_cmd *table;
789 struct sta_info *sta;
790 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
791 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
792 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
793 struct ieee80211_hw *hw = local_to_hw(local);
794 struct iwl4965_rate_scale_data *window = NULL;
795 struct iwl4965_rate_scale_data *search_win = NULL;
797 struct iwl4965_scale_tbl_info tbl_type;
798 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
800 u16 fc = le16_to_cpu(hdr->frame_control);
803 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
805 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
808 /* This packet was aggregated but doesn't carry rate scale info */
809 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
810 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
813 retries = tx_resp->retry_count;
820 sta = sta_info_get(local, hdr->addr1);
822 if (!sta || !sta->rate_ctrl_priv)
826 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
828 if (!priv->lq_mngr.lq_ready)
831 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
832 !lq_sta->ibss_sta_added)
836 active_index = lq_sta->active_tbl;
838 curr_tbl = &(lq_sta->lq_info[active_index]);
839 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
840 window = (struct iwl4965_rate_scale_data *)
842 search_win = (struct iwl4965_rate_scale_data *)
843 &(search_tbl->win[0]);
846 * Ignore this Tx frame response if its initial rate doesn't match
847 * that of latest Link Quality command. There may be stragglers
848 * from a previous Link Quality command, but we're no longer interested
849 * in those; they're either from the "active" mode while we're trying
850 * to check "search" mode, or a prior "search" mode after we've moved
851 * to a new "search" mode (which might become the new "active" mode).
853 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
854 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
855 if (priv->band == IEEE80211_BAND_5GHZ)
856 rs_index -= IWL_FIRST_OFDM_RATE;
858 if ((tx_resp->control.tx_rate == NULL) ||
860 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
862 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
864 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
865 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
866 (!!(tx_rate & RATE_MCS_HT_MSK) ^
867 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
868 (!!(tx_rate & RATE_MCS_GF_MSK) ^
869 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
870 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
871 tx_resp->control.tx_rate->bitrate)) {
872 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
876 /* Update frame history window with "failure" for each Tx retry. */
878 /* Look up the rate and other info used for each tx attempt.
879 * Each tx attempt steps one entry deeper in the rate table. */
880 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
881 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
882 &tbl_type, &rs_index);
884 /* If type matches "search" table,
885 * add failure to "search" history */
886 if ((tbl_type.lq_type == search_tbl->lq_type) &&
887 (tbl_type.ant_type == search_tbl->ant_type) &&
888 (tbl_type.is_SGI == search_tbl->is_SGI)) {
889 if (search_tbl->expected_tpt)
890 tpt = search_tbl->expected_tpt[rs_index];
893 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
895 /* Else if type matches "current/active" table,
896 * add failure to "current/active" history */
897 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
898 (tbl_type.ant_type == curr_tbl->ant_type) &&
899 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
900 if (curr_tbl->expected_tpt)
901 tpt = curr_tbl->expected_tpt[rs_index];
904 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
907 /* If not searching for a new mode, increment failed counter
908 * ... this helps determine when to start searching again */
909 if (lq_sta->stay_in_tbl)
910 lq_sta->total_failed++;
917 * Find (by rate) the history window to update with final Tx attempt;
918 * if Tx was successful first try, use original rate,
919 * else look up the rate that was, finally, successful.
921 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
922 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
924 /* Update frame history window with "success" if Tx got ACKed ... */
925 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
930 /* If type matches "search" table,
931 * add final tx status to "search" history */
932 if ((tbl_type.lq_type == search_tbl->lq_type) &&
933 (tbl_type.ant_type == search_tbl->ant_type) &&
934 (tbl_type.is_SGI == search_tbl->is_SGI)) {
935 if (search_tbl->expected_tpt)
936 tpt = search_tbl->expected_tpt[rs_index];
939 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
940 rs_collect_tx_data(search_win, rs_index, tpt,
941 tx_resp->ampdu_ack_len,
942 tx_resp->ampdu_ack_map);
944 rs_collect_tx_data(search_win, rs_index, tpt,
946 /* Else if type matches "current/active" table,
947 * add final tx status to "current/active" history */
948 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
949 (tbl_type.ant_type == curr_tbl->ant_type) &&
950 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
951 if (curr_tbl->expected_tpt)
952 tpt = curr_tbl->expected_tpt[rs_index];
955 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
956 rs_collect_tx_data(window, rs_index, tpt,
957 tx_resp->ampdu_ack_len,
958 tx_resp->ampdu_ack_map);
960 rs_collect_tx_data(window, rs_index, tpt,
964 /* If not searching for new mode, increment success/failed counter
965 * ... these help determine when to start searching again */
966 if (lq_sta->stay_in_tbl) {
967 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
968 lq_sta->total_success += tx_resp->ampdu_ack_map;
969 lq_sta->total_failed +=
970 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
973 lq_sta->total_success++;
975 lq_sta->total_failed++;
979 /* See if there's a better rate or modulation mode to try. */
980 rs_rate_scale_perform(priv, dev, hdr, sta);
987 * Begin a period of staying with a selected modulation mode.
988 * Set "stay_in_tbl" flag to prevent any mode switches.
989 * Set frame tx success limits according to legacy vs. high-throughput,
990 * and reset overall (spanning all rates) tx success history statistics.
991 * These control how long we stay using same modulation mode before
992 * searching for a new mode.
994 static void rs_set_stay_in_table(u8 is_legacy,
995 struct iwl4965_lq_sta *lq_sta)
997 IWL_DEBUG_RATE("we are staying in the same table\n");
998 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1000 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1001 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1002 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1004 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1005 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1006 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1008 lq_sta->table_count = 0;
1009 lq_sta->total_failed = 0;
1010 lq_sta->total_success = 0;
1014 * Find correct throughput table for given mode of modulation
1016 static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
1017 struct iwl4965_scale_tbl_info *tbl)
1019 if (is_legacy(tbl->lq_type)) {
1020 if (!is_a_band(tbl->lq_type))
1021 tbl->expected_tpt = expected_tpt_G;
1023 tbl->expected_tpt = expected_tpt_A;
1024 } else if (is_siso(tbl->lq_type)) {
1025 if (tbl->is_fat && !lq_sta->is_dup)
1027 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1029 tbl->expected_tpt = expected_tpt_siso40MHz;
1030 else if (tbl->is_SGI)
1031 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1033 tbl->expected_tpt = expected_tpt_siso20MHz;
1035 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
1036 if (tbl->is_fat && !lq_sta->is_dup)
1038 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1040 tbl->expected_tpt = expected_tpt_mimo40MHz;
1041 else if (tbl->is_SGI)
1042 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1044 tbl->expected_tpt = expected_tpt_mimo20MHz;
1046 tbl->expected_tpt = expected_tpt_G;
1049 #ifdef CONFIG_IWL4965_HT
1051 * Find starting rate for new "search" high-throughput mode of modulation.
1052 * Goal is to find lowest expected rate (under perfect conditions) that is
1053 * above the current measured throughput of "active" mode, to give new mode
1054 * a fair chance to prove itself without too many challenges.
1056 * This gets called when transitioning to more aggressive modulation
1057 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1058 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1059 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1060 * bit rate will typically need to increase, but not if performance was bad.
1062 static s32 rs_get_best_rate(struct iwl_priv *priv,
1063 struct iwl4965_lq_sta *lq_sta,
1064 struct iwl4965_scale_tbl_info *tbl, /* "search" */
1065 u16 rate_mask, s8 index)
1067 /* "active" values */
1068 struct iwl4965_scale_tbl_info *active_tbl =
1069 &(lq_sta->lq_info[lq_sta->active_tbl]);
1070 s32 active_sr = active_tbl->win[index].success_ratio;
1071 s32 active_tpt = active_tbl->expected_tpt[index];
1073 /* expected "search" throughput */
1074 s32 *tpt_tbl = tbl->expected_tpt;
1076 s32 new_rate, high, low, start_hi;
1080 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1083 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1085 low = high_low & 0xff;
1086 high = (high_low >> 8) & 0xff;
1089 * Lower the "search" bit rate, to give new "search" mode
1090 * approximately the same throughput as "active" if:
1092 * 1) "Active" mode has been working modestly well (but not
1093 * great), and expected "search" throughput (under perfect
1094 * conditions) at candidate rate is above the actual
1095 * measured "active" throughput (but less than expected
1096 * "active" throughput under perfect conditions).
1098 * 2) "Active" mode has been working perfectly or very well
1099 * and expected "search" throughput (under perfect
1100 * conditions) at candidate rate is above expected
1101 * "active" throughput (under perfect conditions).
1103 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1104 ((active_sr > IWL_RATE_DECREASE_TH) &&
1105 (active_sr <= IWL_RATE_HIGH_TH) &&
1106 (tpt_tbl[rate] <= active_tpt))) ||
1107 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1108 (tpt_tbl[rate] > active_tpt))) {
1110 /* (2nd or later pass)
1111 * If we've already tried to raise the rate, and are
1112 * now trying to lower it, use the higher rate. */
1113 if (start_hi != IWL_RATE_INVALID) {
1114 new_rate = start_hi;
1120 /* Loop again with lower rate */
1121 if (low != IWL_RATE_INVALID)
1124 /* Lower rate not available, use the original */
1128 /* Else try to raise the "search" rate to match "active" */
1130 /* (2nd or later pass)
1131 * If we've already tried to lower the rate, and are
1132 * now trying to raise it, use the lower rate. */
1133 if (new_rate != IWL_RATE_INVALID)
1136 /* Loop again with higher rate */
1137 else if (high != IWL_RATE_INVALID) {
1141 /* Higher rate not available, use the original */
1151 #endif /* CONFIG_IWL4965_HT */
1154 * Set up search table for MIMO
1156 #ifdef CONFIG_IWL4965_HT
1157 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1158 struct iwl4965_lq_sta *lq_sta,
1159 struct ieee80211_conf *conf,
1160 struct sta_info *sta,
1161 struct iwl4965_scale_tbl_info *tbl, int index)
1165 s8 is_green = lq_sta->is_green;
1167 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1168 !sta->ht_info.ht_supported)
1171 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
1174 /* Need both Tx chains/antennas to support MIMO */
1175 if (priv->hw_params.tx_chains_num < 2)
1178 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
1180 tbl->lq_type = LQ_MIMO2;
1181 tbl->is_dup = lq_sta->is_dup;
1183 rate_mask = lq_sta->active_mimo2_rate;
1185 if (priv->current_ht_config.supported_chan_width
1186 == IWL_CHANNEL_WIDTH_40MHZ)
1191 /* FIXME: - don't toggle SGI here
1193 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1197 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1203 rs_set_expected_tpt_table(lq_sta, tbl);
1205 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1207 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1209 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1210 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
1214 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
1216 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
1217 tbl->current_rate, is_green);
1221 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1222 struct iwl4965_lq_sta *lq_sta,
1223 struct ieee80211_conf *conf,
1224 struct sta_info *sta,
1225 struct iwl4965_scale_tbl_info *tbl, int index)
1229 #endif /*CONFIG_IWL4965_HT */
1232 * Set up search table for SISO
1234 static int rs_switch_to_siso(struct iwl_priv *priv,
1235 struct iwl4965_lq_sta *lq_sta,
1236 struct ieee80211_conf *conf,
1237 struct sta_info *sta,
1238 struct iwl4965_scale_tbl_info *tbl, int index)
1240 #ifdef CONFIG_IWL4965_HT
1242 u8 is_green = lq_sta->is_green;
1245 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1246 !sta->ht_info.ht_supported)
1249 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
1251 tbl->is_dup = lq_sta->is_dup;
1252 tbl->lq_type = LQ_SISO;
1254 rate_mask = lq_sta->active_siso_rate;
1256 if (priv->current_ht_config.supported_chan_width
1257 == IWL_CHANNEL_WIDTH_40MHZ)
1262 /* FIXME: - don't toggle SGI here
1264 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1268 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1275 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1277 rs_set_expected_tpt_table(lq_sta, tbl);
1278 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1280 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
1281 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1282 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
1286 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
1287 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
1288 tbl->current_rate, is_green);
1292 #endif /*CONFIG_IWL4965_HT */
1296 * Try to switch to new modulation mode from legacy
1298 static int rs_move_legacy_other(struct iwl_priv *priv,
1299 struct iwl4965_lq_sta *lq_sta,
1300 struct ieee80211_conf *conf,
1301 struct sta_info *sta,
1304 struct iwl4965_scale_tbl_info *tbl =
1305 &(lq_sta->lq_info[lq_sta->active_tbl]);
1306 struct iwl4965_scale_tbl_info *search_tbl =
1307 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1308 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1309 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1310 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1311 u8 start_action = tbl->action;
1312 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1316 switch (tbl->action) {
1317 case IWL_LEGACY_SWITCH_ANTENNA:
1318 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
1320 lq_sta->action_counter++;
1322 /* Don't change antenna if success has been great */
1323 /*FIXME:RS:not sure this is really needed*/
1324 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1327 /* Set up search table to try other antenna */
1328 memcpy(search_tbl, tbl, sz);
1330 if (rs_toggle_antenna(valid_tx_ant,
1331 &search_tbl->current_rate, search_tbl)) {
1332 rs_set_expected_tpt_table(lq_sta, search_tbl);
1333 lq_sta->search_better_tbl = 1;
1337 case IWL_LEGACY_SWITCH_SISO:
1338 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
1340 /* Set up search table to try SISO */
1341 memcpy(search_tbl, tbl, sz);
1342 search_tbl->is_SGI = 0;
1343 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1346 lq_sta->search_better_tbl = 1;
1347 lq_sta->action_counter = 0;
1352 case IWL_LEGACY_SWITCH_MIMO2:
1353 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
1355 /* Set up search table to try MIMO */
1356 memcpy(search_tbl, tbl, sz);
1357 search_tbl->is_SGI = 0;
1358 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1359 /*FIXME:RS:need to check ant validity*/
1360 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1363 lq_sta->search_better_tbl = 1;
1364 lq_sta->action_counter = 0;
1370 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1371 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1373 if (tbl->action == start_action)
1381 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1382 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1388 * Try to switch to new modulation mode from SISO
1390 static int rs_move_siso_to_other(struct iwl_priv *priv,
1391 struct iwl4965_lq_sta *lq_sta,
1392 struct ieee80211_conf *conf,
1393 struct sta_info *sta,
1396 u8 is_green = lq_sta->is_green;
1397 struct iwl4965_scale_tbl_info *tbl =
1398 &(lq_sta->lq_info[lq_sta->active_tbl]);
1399 struct iwl4965_scale_tbl_info *search_tbl =
1400 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1401 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1402 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1403 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1404 u8 start_action = tbl->action;
1405 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1409 lq_sta->action_counter++;
1410 switch (tbl->action) {
1411 case IWL_SISO_SWITCH_ANTENNA:
1412 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
1413 /*FIXME:RS: is this really needed for SISO?*/
1414 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1417 memcpy(search_tbl, tbl, sz);
1418 if (rs_toggle_antenna(valid_tx_ant,
1419 &search_tbl->current_rate, search_tbl)) {
1420 lq_sta->search_better_tbl = 1;
1424 case IWL_SISO_SWITCH_MIMO2:
1425 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
1426 memcpy(search_tbl, tbl, sz);
1427 search_tbl->is_SGI = 0;
1428 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1429 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1432 lq_sta->search_better_tbl = 1;
1436 case IWL_SISO_SWITCH_GI:
1437 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
1439 memcpy(search_tbl, tbl, sz);
1444 IWL_ERROR("SGI was set in GF+SISO\n");
1446 search_tbl->is_SGI = !tbl->is_SGI;
1447 rs_set_expected_tpt_table(lq_sta, search_tbl);
1449 s32 tpt = lq_sta->last_tpt / 100;
1450 if (tpt >= search_tbl->expected_tpt[index])
1453 search_tbl->current_rate = rate_n_flags_from_tbl(
1454 search_tbl, index, is_green);
1455 lq_sta->search_better_tbl = 1;
1459 if (tbl->action > IWL_SISO_SWITCH_GI)
1460 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1462 if (tbl->action == start_action)
1469 if (tbl->action > IWL_SISO_SWITCH_GI)
1470 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1475 * Try to switch to new modulation mode from MIMO
1477 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1478 struct iwl4965_lq_sta *lq_sta,
1479 struct ieee80211_conf *conf,
1480 struct sta_info *sta,
1483 s8 is_green = lq_sta->is_green;
1484 struct iwl4965_scale_tbl_info *tbl =
1485 &(lq_sta->lq_info[lq_sta->active_tbl]);
1486 struct iwl4965_scale_tbl_info *search_tbl =
1487 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1488 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1489 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1490 u8 start_action = tbl->action;
1491 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1495 lq_sta->action_counter++;
1496 switch (tbl->action) {
1497 case IWL_MIMO_SWITCH_ANTENNA_A:
1498 case IWL_MIMO_SWITCH_ANTENNA_B:
1499 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
1501 /* Set up new search table for SISO */
1502 memcpy(search_tbl, tbl, sz);
1504 /*FIXME:RS:need to check ant validity + C*/
1505 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1506 search_tbl->ant_type = ANT_A;
1508 search_tbl->ant_type = ANT_B;
1510 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1513 lq_sta->search_better_tbl = 1;
1518 case IWL_MIMO_SWITCH_GI:
1519 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
1521 /* Set up new search table for MIMO */
1522 memcpy(search_tbl, tbl, sz);
1523 search_tbl->is_SGI = !tbl->is_SGI;
1524 rs_set_expected_tpt_table(lq_sta, search_tbl);
1526 * If active table already uses the fastest possible
1527 * modulation (dual stream with short guard interval),
1528 * and it's working well, there's no need to look
1529 * for a better type of modulation!
1532 s32 tpt = lq_sta->last_tpt / 100;
1533 if (tpt >= search_tbl->expected_tpt[index])
1536 search_tbl->current_rate = rate_n_flags_from_tbl(
1537 search_tbl, index, is_green);
1538 lq_sta->search_better_tbl = 1;
1543 if (tbl->action > IWL_MIMO_SWITCH_GI)
1544 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1546 if (tbl->action == start_action)
1553 if (tbl->action > IWL_MIMO_SWITCH_GI)
1554 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1560 * Check whether we should continue using same modulation mode, or
1561 * begin search for a new mode, based on:
1562 * 1) # tx successes or failures while using this mode
1563 * 2) # times calling this function
1564 * 3) elapsed time in this mode (not used, for now)
1566 static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
1568 struct iwl4965_scale_tbl_info *tbl;
1571 int flush_interval_passed = 0;
1573 active_tbl = lq_sta->active_tbl;
1575 tbl = &(lq_sta->lq_info[active_tbl]);
1577 /* If we've been disallowing search, see if we should now allow it */
1578 if (lq_sta->stay_in_tbl) {
1580 /* Elapsed time using current modulation mode */
1581 if (lq_sta->flush_timer)
1582 flush_interval_passed =
1584 (unsigned long)(lq_sta->flush_timer +
1585 IWL_RATE_SCALE_FLUSH_INTVL));
1588 * Check if we should allow search for new modulation mode.
1589 * If many frames have failed or succeeded, or we've used
1590 * this same modulation for a long time, allow search, and
1591 * reset history stats that keep track of whether we should
1592 * allow a new search. Also (below) reset all bitmaps and
1593 * stats in active history.
1595 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1596 (lq_sta->total_success > lq_sta->max_success_limit) ||
1597 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1598 && (flush_interval_passed))) {
1599 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
1600 lq_sta->total_failed,
1601 lq_sta->total_success,
1602 flush_interval_passed);
1604 /* Allow search for new mode */
1605 lq_sta->stay_in_tbl = 0; /* only place reset */
1606 lq_sta->total_failed = 0;
1607 lq_sta->total_success = 0;
1608 lq_sta->flush_timer = 0;
1611 * Else if we've used this modulation mode enough repetitions
1612 * (regardless of elapsed time or success/failure), reset
1613 * history bitmaps and rate-specific stats for all rates in
1617 lq_sta->table_count++;
1618 if (lq_sta->table_count >=
1619 lq_sta->table_count_limit) {
1620 lq_sta->table_count = 0;
1622 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
1623 for (i = 0; i < IWL_RATE_COUNT; i++)
1624 rs_rate_scale_clear_window(
1629 /* If transitioning to allow "search", reset all history
1630 * bitmaps and stats in active table (this will become the new
1631 * "search" table). */
1632 if (!lq_sta->stay_in_tbl) {
1633 for (i = 0; i < IWL_RATE_COUNT; i++)
1634 rs_rate_scale_clear_window(&(tbl->win[i]));
1640 * Do rate scaling and search for new modulation mode.
1642 static void rs_rate_scale_perform(struct iwl_priv *priv,
1643 struct net_device *dev,
1644 struct ieee80211_hdr *hdr,
1645 struct sta_info *sta)
1647 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1648 struct ieee80211_hw *hw = local_to_hw(local);
1649 struct ieee80211_conf *conf = &hw->conf;
1650 int low = IWL_RATE_INVALID;
1651 int high = IWL_RATE_INVALID;
1654 struct iwl4965_rate_scale_data *window = NULL;
1655 int current_tpt = IWL_INVALID_VALUE;
1656 int low_tpt = IWL_INVALID_VALUE;
1657 int high_tpt = IWL_INVALID_VALUE;
1659 s8 scale_action = 0;
1662 struct iwl4965_lq_sta *lq_sta;
1663 struct iwl4965_scale_tbl_info *tbl, *tbl1;
1664 u16 rate_scale_index_msk = 0;
1670 #ifdef CONFIG_IWL4965_HT
1671 u8 tid = MAX_TID_COUNT;
1675 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1677 fc = le16_to_cpu(hdr->frame_control);
1678 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1679 /* Send management frames and broadcast/multicast data using
1681 /* TODO: this could probably be improved.. */
1685 if (!sta || !sta->rate_ctrl_priv)
1688 if (!priv->lq_mngr.lq_ready) {
1689 IWL_DEBUG_RATE("still rate scaling not ready\n");
1692 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
1694 #ifdef CONFIG_IWL4965_HT
1695 qc = ieee80211_get_qos_ctrl(hdr);
1697 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1698 rs_tl_add_packet(lq_sta, tid);
1702 * Select rate-scale / modulation-mode table to work with in
1703 * the rest of this function: "search" if searching for better
1704 * modulation mode, or "active" if doing rate scaling within a mode.
1706 if (!lq_sta->search_better_tbl)
1707 active_tbl = lq_sta->active_tbl;
1709 active_tbl = 1 - lq_sta->active_tbl;
1711 tbl = &(lq_sta->lq_info[active_tbl]);
1712 is_green = lq_sta->is_green;
1714 /* current tx rate */
1715 index = sta->last_txrate_idx;
1717 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1720 /* rates available for this association, and for modulation mode */
1721 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1723 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1725 /* mask with station rate restriction */
1726 if (is_legacy(tbl->lq_type)) {
1727 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1728 /* supp_rates has no CCK bits in A mode */
1729 rate_scale_index_msk = (u16) (rate_mask &
1730 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1732 rate_scale_index_msk = (u16) (rate_mask &
1733 lq_sta->supp_rates);
1736 rate_scale_index_msk = rate_mask;
1738 if (!rate_scale_index_msk)
1739 rate_scale_index_msk = rate_mask;
1741 /* If current rate is no longer supported on current association,
1742 * or user changed preferences for rates, find a new supported rate. */
1743 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1744 index = IWL_INVALID_VALUE;
1747 /* get the highest available rate */
1748 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1749 if ((1 << i) & rate_scale_index_msk)
1753 if (index == IWL_INVALID_VALUE) {
1754 IWL_WARNING("Can not find a suitable rate\n");
1759 /* Get expected throughput table and history window for current rate */
1760 if (!tbl->expected_tpt)
1761 rs_set_expected_tpt_table(lq_sta, tbl);
1763 window = &(tbl->win[index]);
1766 * If there is not enough history to calculate actual average
1767 * throughput, keep analyzing results of more tx frames, without
1768 * changing rate or mode (bypass most of the rest of this function).
1769 * Set up new rate table in uCode only if old rate is not supported
1770 * in current association (use new rate found above).
1772 fail_count = window->counter - window->success_counter;
1773 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1774 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1775 || (tbl->expected_tpt == NULL)) {
1776 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1778 window->success_counter, window->counter, index);
1780 /* Can't calculate this yet; not enough history */
1781 window->average_tpt = IWL_INVALID_VALUE;
1783 /* Should we stay with this modulation mode,
1784 * or search for a new one? */
1785 rs_stay_in_table(lq_sta);
1787 /* Set up new rate table in uCode, if needed */
1789 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1790 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
1791 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1795 /* Else we have enough samples; calculate estimate of
1796 * actual average throughput */
1798 window->average_tpt = ((window->success_ratio *
1799 tbl->expected_tpt[index] + 64) / 128);
1801 /* If we are searching for better modulation mode, check success. */
1802 if (lq_sta->search_better_tbl) {
1803 int success_limit = IWL_RATE_SCALE_SWITCH;
1805 /* If good success, continue using the "search" mode;
1806 * no need to send new link quality command, since we're
1807 * continuing to use the setup that we've been trying. */
1808 if ((window->success_ratio > success_limit) ||
1809 (window->average_tpt > lq_sta->last_tpt)) {
1810 if (!is_legacy(tbl->lq_type)) {
1811 IWL_DEBUG_RATE("LQ: we are switching to HT"
1812 " rate suc %d current tpt %d"
1814 window->success_ratio,
1815 window->average_tpt,
1817 lq_sta->enable_counter = 1;
1819 /* Swap tables; "search" becomes "active" */
1820 lq_sta->active_tbl = active_tbl;
1821 current_tpt = window->average_tpt;
1823 /* Else poor success; go back to mode in "active" table */
1825 /* Nullify "search" table */
1826 tbl->lq_type = LQ_NONE;
1828 /* Revert to "active" table */
1829 active_tbl = lq_sta->active_tbl;
1830 tbl = &(lq_sta->lq_info[active_tbl]);
1832 /* Revert to "active" rate and throughput info */
1833 index = iwl4965_hwrate_to_plcp_idx(
1835 current_tpt = lq_sta->last_tpt;
1837 /* Need to set up a new rate table in uCode */
1839 IWL_DEBUG_RATE("XXY GO BACK TO OLD TABLE\n");
1842 /* Either way, we've made a decision; modulation mode
1843 * search is done, allow rate adjustment next time. */
1844 lq_sta->search_better_tbl = 0;
1845 done_search = 1; /* Don't switch modes below! */
1849 /* (Else) not in search of better modulation mode, try for better
1850 * starting rate, while staying in this mode. */
1851 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1853 low = high_low & 0xff;
1854 high = (high_low >> 8) & 0xff;
1856 /* Collect measured throughputs for current and adjacent rates */
1857 current_tpt = window->average_tpt;
1858 if (low != IWL_RATE_INVALID)
1859 low_tpt = tbl->win[low].average_tpt;
1860 if (high != IWL_RATE_INVALID)
1861 high_tpt = tbl->win[high].average_tpt;
1863 /* Assume rate increase */
1866 /* Too many failures, decrease rate */
1867 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1868 (current_tpt == 0)) {
1869 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1872 /* No throughput measured yet for adjacent rates; try increase. */
1873 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1874 (high_tpt == IWL_INVALID_VALUE))
1877 /* Both adjacent throughputs are measured, but neither one has better
1878 * throughput; we're using the best rate, don't change it! */
1879 else if ((low_tpt != IWL_INVALID_VALUE) &&
1880 (high_tpt != IWL_INVALID_VALUE) &&
1881 (low_tpt < current_tpt) &&
1882 (high_tpt < current_tpt))
1885 /* At least one adjacent rate's throughput is measured,
1886 * and may have better performance. */
1888 /* Higher adjacent rate's throughput is measured */
1889 if (high_tpt != IWL_INVALID_VALUE) {
1890 /* Higher rate has better throughput */
1891 if (high_tpt > current_tpt)
1895 ("decrease rate because of high tpt\n");
1899 /* Lower adjacent rate's throughput is measured */
1900 } else if (low_tpt != IWL_INVALID_VALUE) {
1901 /* Lower rate has better throughput */
1902 if (low_tpt > current_tpt) {
1904 ("decrease rate because of low tpt\n");
1911 /* Sanity check; asked for decrease, but success rate or throughput
1912 * has been good at old rate. Don't change it. */
1913 if (scale_action == -1) {
1914 if ((low != IWL_RATE_INVALID) &&
1915 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1916 (current_tpt > (100 * tbl->expected_tpt[low]))))
1919 /* Sanity check; asked for increase, but success rate has not been great
1920 * even at old rate, higher rate will be worse. Don't change it. */
1921 } else if ((scale_action == 1) &&
1922 (window->success_ratio < IWL_RATE_INCREASE_TH))
1925 switch (scale_action) {
1927 /* Decrease starting rate, update uCode's rate table */
1928 if (low != IWL_RATE_INVALID) {
1934 /* Increase starting rate, update uCode's rate table */
1935 if (high != IWL_RATE_INVALID) {
1947 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
1948 "high %d type %d\n",
1949 index, scale_action, low, high, tbl->lq_type);
1952 /* Replace uCode's rate table for the destination station. */
1954 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1955 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
1956 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1959 /* Should we stay with this modulation mode, or search for a new one? */
1960 rs_stay_in_table(lq_sta);
1963 * Search for new modulation mode if we're:
1964 * 1) Not changing rates right now
1965 * 2) Not just finishing up a search
1966 * 3) Allowing a new search
1968 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
1969 /* Save current throughput to compare with "search" throughput*/
1970 lq_sta->last_tpt = current_tpt;
1972 /* Select a new "search" modulation mode to try.
1973 * If one is found, set up the new "search" table. */
1974 if (is_legacy(tbl->lq_type))
1975 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
1976 else if (is_siso(tbl->lq_type))
1977 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
1979 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
1981 /* If new "search" mode was selected, set up in uCode table */
1982 if (lq_sta->search_better_tbl) {
1983 /* Access the "search" table, clear its history. */
1984 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1985 for (i = 0; i < IWL_RATE_COUNT; i++)
1986 rs_rate_scale_clear_window(&(tbl->win[i]));
1988 /* Use new "search" start rate */
1989 index = iwl4965_hwrate_to_plcp_idx(
1992 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
1993 tbl->current_rate, index);
1994 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate,
1996 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1999 /* If the "active" (non-search) mode was legacy,
2000 * and we've tried switching antennas,
2001 * but we haven't been able to try HT modes (not available),
2002 * stay with best antenna legacy modulation for a while
2003 * before next round of mode comparisons. */
2004 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2005 if (is_legacy(tbl1->lq_type) &&
2006 #ifdef CONFIG_IWL4965_HT
2007 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
2009 (lq_sta->action_counter >= 1)) {
2010 lq_sta->action_counter = 0;
2011 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
2012 rs_set_stay_in_table(1, lq_sta);
2015 /* If we're in an HT mode, and all 3 mode switch actions
2016 * have been tried and compared, stay in this best modulation
2017 * mode for a while before next round of mode comparisons. */
2018 if (lq_sta->enable_counter &&
2019 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
2020 #ifdef CONFIG_IWL4965_HT
2021 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2022 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2023 (tid != MAX_TID_COUNT)) {
2024 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
2025 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
2027 #endif /*CONFIG_IWL4965_HT */
2028 lq_sta->action_counter = 0;
2029 rs_set_stay_in_table(0, lq_sta);
2033 * Else, don't search for a new modulation mode.
2034 * Put new timestamp in stay-in-modulation-mode flush timer if:
2035 * 1) Not changing rates right now
2036 * 2) Not just finishing up a search
2037 * 3) flush timer is empty
2040 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2041 lq_sta->flush_timer = jiffies;
2045 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
2047 sta->last_txrate_idx = i;
2049 /* sta->txrate_idx is an index to A mode rates which start
2050 * at IWL_FIRST_OFDM_RATE
2052 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2053 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
2055 sta->txrate_idx = i;
2061 static void rs_initialize_lq(struct iwl_priv *priv,
2062 struct ieee80211_conf *conf,
2063 struct sta_info *sta)
2065 struct iwl4965_lq_sta *lq_sta;
2066 struct iwl4965_scale_tbl_info *tbl;
2070 u8 use_green = rs_use_green(priv, conf);
2074 if (!sta || !sta->rate_ctrl_priv)
2077 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2078 i = sta->last_txrate_idx;
2080 if ((lq_sta->lq.sta_id == 0xff) &&
2081 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2084 valid_tx_ant = priv->hw_params.valid_tx_ant;
2086 if (!lq_sta->search_better_tbl)
2087 active_tbl = lq_sta->active_tbl;
2089 active_tbl = 1 - lq_sta->active_tbl;
2091 tbl = &(lq_sta->lq_info[active_tbl]);
2093 if ((i < 0) || (i >= IWL_RATE_COUNT))
2096 /* FIXME:RS: This is also wrong in 4965 */
2097 rate = iwl4965_rates[i].plcp;
2098 rate |= RATE_MCS_ANT_B_MSK;
2099 rate &= ~RATE_MCS_ANT_A_MSK;
2101 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2102 rate |= RATE_MCS_CCK_MSK;
2104 tbl->ant_type = ANT_B;
2105 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2106 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2107 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2109 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2110 tbl->current_rate = rate;
2111 rs_set_expected_tpt_table(lq_sta, tbl);
2112 rs_fill_link_cmd(NULL, lq_sta, rate, &lq_sta->lq);
2113 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2118 static void rs_get_rate(void *priv_rate, struct net_device *dev,
2119 struct ieee80211_supported_band *sband,
2120 struct sk_buff *skb,
2121 struct rate_selection *sel)
2125 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2126 struct ieee80211_conf *conf = &local->hw.conf;
2127 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2128 struct sta_info *sta;
2130 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
2131 struct iwl4965_lq_sta *lq_sta;
2133 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
2137 sta = sta_info_get(local, hdr->addr1);
2139 /* Send management frames and broadcast/multicast data using lowest
2141 fc = le16_to_cpu(hdr->frame_control);
2142 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2143 !sta || !sta->rate_ctrl_priv) {
2144 sel->rate = rate_lowest(local, sband, sta);
2148 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2149 i = sta->last_txrate_idx;
2151 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2152 !lq_sta->ibss_sta_added) {
2153 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2154 DECLARE_MAC_BUF(mac);
2156 if (sta_id == IWL_INVALID_STATION) {
2157 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2158 print_mac(mac, hdr->addr1));
2159 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2160 0, CMD_ASYNC, NULL);
2162 if ((sta_id != IWL_INVALID_STATION)) {
2163 lq_sta->lq.sta_id = sta_id;
2164 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2165 lq_sta->ibss_sta_added = 1;
2166 rs_initialize_lq(priv, conf, sta);
2168 if (!lq_sta->ibss_sta_added)
2173 if ((i < 0) || (i > IWL_RATE_COUNT)) {
2174 sel->rate = rate_lowest(local, sband, sta);
2178 sel->rate = &priv->ieee_rates[i];
2183 static void *rs_alloc_sta(void *priv, gfp_t gfp)
2185 struct iwl4965_lq_sta *lq_sta;
2188 IWL_DEBUG_RATE("create station rate scale window\n");
2190 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
2194 lq_sta->lq.sta_id = 0xff;
2197 for (j = 0; j < LQ_SIZE; j++)
2198 for (i = 0; i < IWL_RATE_COUNT; i++)
2199 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
2204 static void rs_rate_init(void *priv_rate, void *priv_sta,
2205 struct ieee80211_local *local,
2206 struct sta_info *sta)
2209 struct ieee80211_conf *conf = &local->hw.conf;
2210 struct ieee80211_supported_band *sband;
2211 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
2212 struct iwl4965_lq_sta *lq_sta = priv_sta;
2214 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2216 lq_sta->flush_timer = 0;
2217 lq_sta->supp_rates = sta->supp_rates[sband->band];
2218 sta->txrate_idx = 3;
2219 for (j = 0; j < LQ_SIZE; j++)
2220 for (i = 0; i < IWL_RATE_COUNT; i++)
2221 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
2223 IWL_DEBUG_RATE("rate scale global init\n");
2224 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2225 * the lowest or the highest rate.. Could consider using RSSI from
2226 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2229 lq_sta->ibss_sta_added = 0;
2230 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2231 u8 sta_id = iwl_find_station(priv, sta->addr);
2232 DECLARE_MAC_BUF(mac);
2234 /* for IBSS the call are from tasklet */
2235 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2236 print_mac(mac, sta->addr));
2238 if (sta_id == IWL_INVALID_STATION) {
2239 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2240 print_mac(mac, sta->addr));
2241 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2242 0, CMD_ASYNC, NULL);
2244 if ((sta_id != IWL_INVALID_STATION)) {
2245 lq_sta->lq.sta_id = sta_id;
2246 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2248 /* FIXME: this is w/a remove it later */
2249 priv->assoc_station_added = 1;
2252 /* Find highest tx rate supported by hardware and destination station */
2253 for (i = 0; i < sband->n_bitrates; i++)
2254 if (sta->supp_rates[sband->band] & BIT(i))
2255 sta->txrate_idx = i;
2257 sta->last_txrate_idx = sta->txrate_idx;
2258 /* WTF is with this bogus comment? A doesn't have cck rates */
2259 /* For MODE_IEEE80211A, cck rates are at end of rate table */
2260 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2261 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2264 lq_sta->is_green = rs_use_green(priv, conf);
2265 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2266 lq_sta->active_rate_basic = priv->active_rate_basic;
2267 lq_sta->band = priv->band;
2268 #ifdef CONFIG_IWL4965_HT
2270 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2271 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2273 lq_sta->active_siso_rate =
2274 priv->current_ht_config.supp_mcs_set[0] << 1;
2275 lq_sta->active_siso_rate |=
2276 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2277 lq_sta->active_siso_rate &= ~((u16)0x2);
2278 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2281 lq_sta->active_mimo2_rate =
2282 priv->current_ht_config.supp_mcs_set[1] << 1;
2283 lq_sta->active_mimo2_rate |=
2284 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2285 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2286 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2288 lq_sta->active_mimo3_rate =
2289 priv->current_ht_config.supp_mcs_set[2] << 1;
2290 lq_sta->active_mimo3_rate |=
2291 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2292 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2293 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2295 IWL_DEBUG_RATE("SISO RATE %X MIMO2 RATE %X MIMO3 RATE %X\n",
2296 lq_sta->active_siso_rate,
2297 lq_sta->active_mimo2_rate,
2298 lq_sta->active_mimo3_rate);
2300 /* as default allow aggregation for all tids */
2301 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2302 #endif /*CONFIG_IWL4965_HT*/
2303 #ifdef CONFIG_MAC80211_DEBUGFS
2307 if (priv->assoc_station_added)
2308 priv->lq_mngr.lq_ready = 1;
2310 rs_initialize_lq(priv, conf, sta);
2313 static void rs_fill_link_cmd(const struct iwl_priv *priv,
2314 struct iwl4965_lq_sta *lq_sta,
2316 struct iwl_link_quality_cmd *lq_cmd)
2318 struct iwl4965_scale_tbl_info tbl_type;
2321 int repeat_rate = 0;
2322 u8 ant_toggle_cnt = 0;
2323 u8 use_ht_possible = 1;
2324 u8 valid_tx_ant = 0;
2326 /* Override starting rate (index 0) if needed for debug purposes */
2327 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2329 /* Interpret new_rate (rate_n_flags) */
2330 memset(&tbl_type, 0, sizeof(tbl_type));
2331 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2332 &tbl_type, &rate_idx);
2334 /* How many times should we repeat the initial rate? */
2335 if (is_legacy(tbl_type.lq_type)) {
2337 repeat_rate = IWL_NUMBER_TRY;
2339 repeat_rate = IWL_HT_NUMBER_TRY;
2342 lq_cmd->general_params.mimo_delimiter =
2343 is_mimo(tbl_type.lq_type) ? 1 : 0;
2345 /* Fill 1st table entry (index 0) */
2346 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2349 if (is_mimo(tbl_type.lq_type) || (tbl_type.ant_type == ANT_A))
2350 lq_cmd->general_params.single_stream_ant_msk
2351 = LINK_QUAL_ANT_A_MSK;
2353 lq_cmd->general_params.single_stream_ant_msk
2354 = LINK_QUAL_ANT_B_MSK;
2360 valid_tx_ant = priv->hw_params.valid_tx_ant;
2362 /* Fill rest of rate table */
2363 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2364 /* Repeat initial/next rate.
2365 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2366 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2367 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2368 if (is_legacy(tbl_type.lq_type)) {
2369 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2372 rs_toggle_antenna(valid_tx_ant,
2373 &new_rate, &tbl_type))
2377 /* Override next rate if needed for debug purposes */
2378 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2380 /* Fill next table entry */
2381 lq_cmd->rs_table[index].rate_n_flags =
2382 cpu_to_le32(new_rate);
2387 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2390 /* Indicate to uCode which entries might be MIMO.
2391 * If initial rate was MIMO, this will finally end up
2392 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2393 if (is_mimo(tbl_type.lq_type))
2394 lq_cmd->general_params.mimo_delimiter = index;
2397 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2400 /* How many times should we repeat the next rate? */
2401 if (is_legacy(tbl_type.lq_type)) {
2402 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2405 rs_toggle_antenna(valid_tx_ant,
2406 &new_rate, &tbl_type))
2409 repeat_rate = IWL_NUMBER_TRY;
2411 repeat_rate = IWL_HT_NUMBER_TRY;
2414 /* Don't allow HT rates after next pass.
2415 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2416 use_ht_possible = 0;
2418 /* Override next rate if needed for debug purposes */
2419 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2421 /* Fill next table entry */
2422 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2428 lq_cmd->general_params.dual_stream_ant_msk = 3;
2429 lq_cmd->agg_params.agg_dis_start_th = 3;
2430 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
2433 static void *rs_alloc(struct ieee80211_local *local)
2435 return local->hw.priv;
2437 /* rate scale requires free function to be implemented */
2438 static void rs_free(void *priv_rate)
2443 static void rs_clear(void *priv_rate)
2445 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
2447 IWL_DEBUG_RATE("enter\n");
2449 priv->lq_mngr.lq_ready = 0;
2451 IWL_DEBUG_RATE("leave\n");
2454 static void rs_free_sta(void *priv, void *priv_sta)
2456 struct iwl4965_lq_sta *lq_sta = priv_sta;
2458 IWL_DEBUG_RATE("enter\n");
2460 IWL_DEBUG_RATE("leave\n");
2464 #ifdef CONFIG_MAC80211_DEBUGFS
2465 static int open_file_generic(struct inode *inode, struct file *file)
2467 file->private_data = inode->i_private;
2470 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
2471 u32 *rate_n_flags, int index)
2473 if (lq_sta->dbg_fixed_rate) {
2475 *rate_n_flags = lq_sta->dbg_fixed_rate;
2477 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2478 *rate_n_flags = 0x800D;
2480 *rate_n_flags = 0x820A;
2482 IWL_DEBUG_RATE("Fixed rate ON\n");
2484 IWL_DEBUG_RATE("Fixed rate OFF\n");
2488 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2489 const char __user *user_buf, size_t count, loff_t *ppos)
2491 struct iwl4965_lq_sta *lq_sta = file->private_data;
2496 memset(buf, 0, sizeof(buf));
2497 buf_size = min(count, sizeof(buf) - 1);
2498 if (copy_from_user(buf, user_buf, buf_size))
2501 if (sscanf(buf, "%x", &parsed_rate) == 1)
2502 lq_sta->dbg_fixed_rate = parsed_rate;
2504 lq_sta->dbg_fixed_rate = 0;
2506 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2507 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2508 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2509 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2511 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2512 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2514 if (lq_sta->dbg_fixed_rate) {
2515 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate,
2517 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2523 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2524 char __user *user_buf, size_t count, loff_t *ppos)
2530 struct iwl4965_lq_sta *lq_sta = file->private_data;
2532 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2533 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2534 lq_sta->total_failed, lq_sta->total_success,
2535 lq_sta->active_legacy_rate);
2536 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2537 lq_sta->dbg_fixed_rate);
2538 desc += sprintf(buff+desc, "general:"
2539 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2540 lq_sta->lq.general_params.flags,
2541 lq_sta->lq.general_params.mimo_delimiter,
2542 lq_sta->lq.general_params.single_stream_ant_msk,
2543 lq_sta->lq.general_params.dual_stream_ant_msk);
2545 desc += sprintf(buff+desc, "agg:"
2546 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2547 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2548 lq_sta->lq.agg_params.agg_dis_start_th,
2549 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2551 desc += sprintf(buff+desc,
2552 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2553 lq_sta->lq.general_params.start_rate_index[0],
2554 lq_sta->lq.general_params.start_rate_index[1],
2555 lq_sta->lq.general_params.start_rate_index[2],
2556 lq_sta->lq.general_params.start_rate_index[3]);
2559 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2560 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2561 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2563 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2566 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2567 .write = rs_sta_dbgfs_scale_table_write,
2568 .read = rs_sta_dbgfs_scale_table_read,
2569 .open = open_file_generic,
2571 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2572 char __user *user_buf, size_t count, loff_t *ppos)
2578 struct iwl4965_lq_sta *lq_sta = file->private_data;
2579 for (i = 0; i < LQ_SIZE; i++) {
2580 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2582 lq_sta->active_tbl == i?"*":"x",
2583 lq_sta->lq_info[i].lq_type,
2584 lq_sta->lq_info[i].is_SGI,
2585 lq_sta->lq_info[i].is_fat,
2586 lq_sta->lq_info[i].is_dup,
2587 lq_sta->lq_info[i].current_rate);
2588 for (j = 0; j < IWL_RATE_COUNT; j++) {
2589 desc += sprintf(buff+desc,
2590 "counter=%d success=%d %%=%d\n",
2591 lq_sta->lq_info[i].win[j].counter,
2592 lq_sta->lq_info[i].win[j].success_counter,
2593 lq_sta->lq_info[i].win[j].success_ratio);
2596 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2599 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2600 .read = rs_sta_dbgfs_stats_table_read,
2601 .open = open_file_generic,
2604 static void rs_add_debugfs(void *priv, void *priv_sta,
2607 struct iwl4965_lq_sta *lq_sta = priv_sta;
2608 lq_sta->rs_sta_dbgfs_scale_table_file =
2609 debugfs_create_file("rate_scale_table", 0600, dir,
2610 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2611 lq_sta->rs_sta_dbgfs_stats_table_file =
2612 debugfs_create_file("rate_stats_table", 0600, dir,
2613 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2614 #ifdef CONFIG_IWL4965_HT
2615 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2616 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2617 &lq_sta->tx_agg_tid_en);
2622 static void rs_remove_debugfs(void *priv, void *priv_sta)
2624 struct iwl4965_lq_sta *lq_sta = priv_sta;
2625 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2626 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2627 #ifdef CONFIG_IWL4965_HT
2628 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2633 static struct rate_control_ops rs_ops = {
2636 .tx_status = rs_tx_status,
2637 .get_rate = rs_get_rate,
2638 .rate_init = rs_rate_init,
2642 .alloc_sta = rs_alloc_sta,
2643 .free_sta = rs_free_sta,
2644 #ifdef CONFIG_MAC80211_DEBUGFS
2645 .add_sta_debugfs = rs_add_debugfs,
2646 .remove_sta_debugfs = rs_remove_debugfs,
2650 int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2652 struct ieee80211_local *local = hw_to_local(hw);
2653 struct iwl_priv *priv = hw->priv;
2654 struct iwl4965_lq_sta *lq_sta;
2655 struct sta_info *sta;
2657 u32 samples = 0, success = 0, good = 0;
2658 unsigned long now = jiffies;
2660 u8 lq_type, antenna;
2664 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2665 if (!sta || !sta->rate_ctrl_priv) {
2667 IWL_DEBUG_RATE("leave - no private rate data!\n");
2669 IWL_DEBUG_RATE("leave - no station!\n");
2671 return sprintf(buf, "station %d not found\n", sta_id);
2674 lq_sta = (void *)sta->rate_ctrl_priv;
2676 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
2677 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
2679 if (is_legacy(lq_type))
2680 i = IWL_RATE_54M_INDEX;
2682 i = IWL_RATE_60M_INDEX;
2686 int active = lq_sta->active_tbl;
2689 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
2691 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2692 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2694 (lq_sta->lq_info[active].win[i].data & mask)
2697 samples += lq_sta->lq_info[active].win[i].counter;
2698 good += lq_sta->lq_info[active].win[i].success_counter;
2699 success += lq_sta->lq_info[active].win[i].success_counter *
2700 iwl4965_rates[i].ieee;
2702 if (lq_sta->lq_info[active].win[i].stamp) {
2704 jiffies_to_msecs(now -
2705 lq_sta->lq_info[active].win[i].stamp);
2707 if (delta > max_time)
2710 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
2714 j = iwl4965_get_prev_ieee_rate(i);
2720 /* Display the average rate of all samples taken.
2722 * NOTE: We multiply # of samples by 2 since the IEEE measurement
2723 * added from iwl4965_rates is actually 2X the rate */
2725 cnt += sprintf(&buf[cnt],
2726 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2727 "%3d%% success (%d good packets over %d tries)\n",
2728 success / (2 * samples), (success * 5 / samples) % 10,
2729 max_time, good * 100 / samples, good, samples);
2731 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2733 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
2734 "active_search %d rate index %d\n", lq_type, antenna,
2735 lq_sta->search_better_tbl, sta->last_txrate_idx);
2741 void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2743 struct iwl_priv *priv = hw->priv;
2745 priv->lq_mngr.lq_ready = 1;
2748 int iwl4965_rate_control_register(void)
2750 return ieee80211_rate_control_register(&rs_ops);
2753 void iwl4965_rate_control_unregister(void)
2755 ieee80211_rate_control_unregister(&rs_ops);