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_ANTENNA_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
68 * struct iwl4965_rate_scale_data -- tx success history for one rate
70 struct iwl4965_rate_scale_data {
71 u64 data; /* bitmap of successful frames */
72 s32 success_counter; /* number of frames successful */
73 s32 success_ratio; /* per-cent * 128 */
74 s32 counter; /* number of frames attempted */
75 s32 average_tpt; /* success ratio * expected throughput */
80 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
82 * There are two of these in struct iwl4965_lq_sta,
83 * one for "active", and one for "search".
85 struct iwl4965_scale_tbl_info {
86 enum iwl_table_type lq_type;
88 u8 is_SGI; /* 1 = short guard interval */
89 u8 is_fat; /* 1 = 40 MHz channel width */
90 u8 is_dup; /* 1 = duplicated data streams */
91 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
92 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
93 u32 current_rate; /* rate_n_flags, uCode API format */
94 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
97 #ifdef CONFIG_IWL4965_HT
99 struct iwl4965_traffic_load {
100 unsigned long time_stamp; /* age of the oldest statistics */
101 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
103 u32 total; /* total num of packets during the
104 * last TID_MAX_TIME_DIFF */
105 u8 queue_count; /* number of queues that has
106 * been used since the last cleanup */
107 u8 head; /* start of the circular buffer */
110 #endif /* CONFIG_IWL4965_HT */
113 * struct iwl4965_lq_sta -- driver's rate scaling private structure
115 * Pointer to this gets passed back and forth between driver and mac80211.
117 struct iwl4965_lq_sta {
118 u8 active_tbl; /* index of active table, range 0-1 */
119 u8 enable_counter; /* indicates HT mode */
120 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
121 u8 search_better_tbl; /* 1: currently trying alternate mode */
124 /* The following determine when to search for a new mode */
125 u32 table_count_limit;
126 u32 max_failure_limit; /* # failed frames before new search */
127 u32 max_success_limit; /* # successful frames before new search */
129 u32 total_failed; /* total failed frames, any/all rates */
130 u32 total_success; /* total successful frames, any/all rates */
131 u32 flush_timer; /* time staying in mode before new search */
133 u8 action_counter; /* # mode-switch actions tried */
138 enum ieee80211_band band;
141 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
143 u16 active_legacy_rate;
144 u16 active_siso_rate;
145 u16 active_mimo2_rate;
146 u16 active_mimo3_rate;
147 u16 active_rate_basic;
149 struct iwl_link_quality_cmd lq;
150 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
151 #ifdef CONFIG_IWL4965_HT
152 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
155 #ifdef CONFIG_MAC80211_DEBUGFS
156 struct dentry *rs_sta_dbgfs_scale_table_file;
157 struct dentry *rs_sta_dbgfs_stats_table_file;
158 #ifdef CONFIG_IWL4965_HT
159 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
162 struct iwl_priv *drv;
166 static void rs_rate_scale_perform(struct iwl_priv *priv,
167 struct net_device *dev,
168 struct ieee80211_hdr *hdr,
169 struct sta_info *sta);
170 static void rs_fill_link_cmd(const struct iwl_priv *priv,
171 struct iwl4965_lq_sta *lq_sta,
173 struct iwl_link_quality_cmd *tbl);
176 #ifdef CONFIG_MAC80211_DEBUGFS
177 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
178 u32 *rate_n_flags, int index);
180 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
181 u32 *rate_n_flags, int index)
186 * Expected throughput metrics for following rates:
187 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
188 * "G" is the only table that supports CCK (the first 4 rates).
190 /*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
191 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
192 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
195 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
196 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
199 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
203 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
204 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
207 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
211 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
215 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
219 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
223 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
227 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
231 static inline u8 rs_extract_rate(u32 rate_n_flags)
233 return (u8)(rate_n_flags & 0xFF);
236 static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
239 window->success_counter = 0;
240 window->success_ratio = IWL_INVALID_VALUE;
242 window->average_tpt = IWL_INVALID_VALUE;
246 #ifdef CONFIG_IWL4965_HT
248 * removes the old data from the statistics. All data that is older than
249 * TID_MAX_TIME_DIFF, will be deleted.
251 static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
253 /* The oldest age we want to keep */
254 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
256 while (tl->queue_count &&
257 (tl->time_stamp < oldest_time)) {
258 tl->total -= tl->packet_count[tl->head];
259 tl->packet_count[tl->head] = 0;
260 tl->time_stamp += TID_QUEUE_CELL_SPACING;
263 if (tl->head >= TID_QUEUE_MAX_SIZE)
269 * increment traffic load value for tid and also remove
270 * any old values if passed the certain time period
272 static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
274 u32 curr_time = jiffies_to_msecs(jiffies);
277 struct iwl4965_traffic_load *tl = NULL;
279 if (tid >= TID_MAX_LOAD_COUNT)
282 tl = &lq_data->load[tid];
284 curr_time -= curr_time % TID_ROUND_VALUE;
286 /* Happens only for the first packet. Initialize the data */
287 if (!(tl->queue_count)) {
289 tl->time_stamp = curr_time;
292 tl->packet_count[0] = 1;
296 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
297 index = time_diff / TID_QUEUE_CELL_SPACING;
299 /* The history is too long: remove data that is older than */
300 /* TID_MAX_TIME_DIFF */
301 if (index >= TID_QUEUE_MAX_SIZE)
302 rs_tl_rm_old_stats(tl, curr_time);
304 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
305 tl->packet_count[index] = tl->packet_count[index] + 1;
306 tl->total = tl->total + 1;
308 if ((index + 1) > tl->queue_count)
309 tl->queue_count = index + 1;
313 get the traffic load value for tid
315 static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
317 u32 curr_time = jiffies_to_msecs(jiffies);
320 struct iwl4965_traffic_load *tl = NULL;
322 if (tid >= TID_MAX_LOAD_COUNT)
325 tl = &(lq_data->load[tid]);
327 curr_time -= curr_time % TID_ROUND_VALUE;
329 if (!(tl->queue_count))
332 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
333 index = time_diff / TID_QUEUE_CELL_SPACING;
335 /* The history is too long: remove data that is older than */
336 /* TID_MAX_TIME_DIFF */
337 if (index >= TID_QUEUE_MAX_SIZE)
338 rs_tl_rm_old_stats(tl, curr_time);
343 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
344 struct iwl4965_lq_sta *lq_data, u8 tid,
345 struct sta_info *sta)
348 DECLARE_MAC_BUF(mac);
350 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
351 state = sta->ampdu_mlme.tid_state_tx[tid];
352 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
354 if (state == HT_AGG_STATE_IDLE &&
355 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
356 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
357 print_mac(mac, sta->addr), tid);
358 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
362 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
363 struct iwl4965_lq_sta *lq_data,
364 struct sta_info *sta)
366 if ((tid < TID_MAX_LOAD_COUNT))
367 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
368 else if (tid == IWL_AGG_ALL_TID)
369 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
370 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
373 #endif /* CONFIG_IWLWIFI_HT */
375 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
377 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
378 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
379 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
383 * rs_collect_tx_data - Update the success/failure sliding window
385 * We keep a sliding window of the last 62 packets transmitted
386 * at this rate. window->data contains the bitmask of successful
389 static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
390 int scale_index, s32 tpt, int retries,
393 struct iwl4965_rate_scale_data *window = NULL;
394 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
397 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
400 /* Select data for current tx bit rate */
401 window = &(windows[scale_index]);
404 * Keep track of only the latest 62 tx frame attempts in this rate's
405 * history window; anything older isn't really relevant any more.
406 * If we have filled up the sliding window, drop the oldest attempt;
407 * if the oldest attempt (highest bit in bitmap) shows "success",
408 * subtract "1" from the success counter (this is the main reason
409 * we keep these bitmaps!).
411 while (retries > 0) {
412 if (window->counter >= IWL_RATE_MAX_WINDOW) {
414 /* remove earliest */
415 window->counter = IWL_RATE_MAX_WINDOW - 1;
417 if (window->data & mask) {
418 window->data &= ~mask;
419 window->success_counter--;
423 /* Increment frames-attempted counter */
426 /* Shift bitmap by one frame (throw away oldest history),
427 * OR in "1", and increment "success" if this
428 * frame was successful. */
431 window->success_counter++;
439 /* Calculate current success ratio, avoid divide-by-0! */
440 if (window->counter > 0)
441 window->success_ratio = 128 * (100 * window->success_counter)
444 window->success_ratio = IWL_INVALID_VALUE;
446 fail_count = window->counter - window->success_counter;
448 /* Calculate average throughput, if we have enough history. */
449 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
450 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
451 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
453 window->average_tpt = IWL_INVALID_VALUE;
455 /* Tag this window as having been updated */
456 window->stamp = jiffies;
462 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
464 /* FIXME:RS:remove this function and put the flags statically in the table */
465 static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
466 int index, u8 use_green)
468 u32 rate_n_flags = 0;
470 if (is_legacy(tbl->lq_type)) {
471 rate_n_flags = iwl4965_rates[index].plcp;
472 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
473 rate_n_flags |= RATE_MCS_CCK_MSK;
475 } else if (is_Ht(tbl->lq_type)) {
476 if (index > IWL_LAST_OFDM_RATE) {
477 IWL_ERROR("invalid HT rate index %d\n", index);
478 index = IWL_LAST_OFDM_RATE;
480 rate_n_flags = RATE_MCS_HT_MSK;
482 if (is_siso(tbl->lq_type))
483 rate_n_flags |= iwl4965_rates[index].plcp_siso;
484 else if (is_mimo2(tbl->lq_type))
485 rate_n_flags |= iwl4965_rates[index].plcp_mimo2;
487 rate_n_flags |= iwl4965_rates[index].plcp_mimo3;
489 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
492 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
493 RATE_MCS_ANT_ABC_MSK);
495 if (is_Ht(tbl->lq_type)) {
498 rate_n_flags |= RATE_MCS_DUP_MSK;
500 rate_n_flags |= RATE_MCS_FAT_MSK;
503 rate_n_flags |= RATE_MCS_SGI_MSK;
506 rate_n_flags |= RATE_MCS_GF_MSK;
507 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
508 rate_n_flags &= ~RATE_MCS_SGI_MSK;
509 IWL_ERROR("GF was set with SGI:SISO\n");
517 * Interpret uCode API's rate_n_flags format,
518 * fill "search" or "active" tx mode table.
520 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
521 enum ieee80211_band band,
522 struct iwl4965_scale_tbl_info *tbl,
525 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
526 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
529 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
531 if (*rate_idx == IWL_RATE_INVALID) {
535 tbl->is_SGI = 0; /* default legacy setup */
538 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
539 tbl->lq_type = LQ_NONE;
541 /* legacy rate format */
542 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
543 if (num_of_ant == 1) {
544 if (band == IEEE80211_BAND_5GHZ)
551 if (rate_n_flags & RATE_MCS_SGI_MSK)
554 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
555 (rate_n_flags & RATE_MCS_DUP_MSK))
558 if (rate_n_flags & RATE_MCS_DUP_MSK)
561 mcs = rs_extract_rate(rate_n_flags);
564 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
566 tbl->lq_type = LQ_SISO; /*else NONE*/
568 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
570 tbl->lq_type = LQ_MIMO2;
574 tbl->lq_type = LQ_MIMO3;
579 /* FIXME:RS: need to toggle also ANT_C, and also AB,AC,BC */
580 static inline void rs_toggle_antenna(u32 *rate_n_flags,
581 struct iwl4965_scale_tbl_info *tbl)
583 tbl->ant_type ^= ANT_AB;
584 *rate_n_flags ^= (RATE_MCS_ANT_A_MSK|RATE_MCS_ANT_B_MSK);
587 /* FIXME:RS: in 4965 we don't use greenfield at all */
588 static inline u8 rs_use_green(struct iwl_priv *priv,
589 struct ieee80211_conf *conf)
591 #ifdef CONFIG_IWL4965_HT
592 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
593 priv->current_ht_config.is_green_field &&
594 !priv->current_ht_config.non_GF_STA_present);
597 #endif /* CONFIG_IWL4965_HT */
601 * rs_get_supported_rates - get the available rates
603 * if management frame or broadcast frame only return
604 * basic available rates.
607 static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
608 struct ieee80211_hdr *hdr,
609 enum iwl_table_type rate_type)
611 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
612 lq_sta->active_rate_basic)
613 return lq_sta->active_rate_basic;
615 if (is_legacy(rate_type)) {
616 return lq_sta->active_legacy_rate;
618 if (is_siso(rate_type))
619 return lq_sta->active_siso_rate;
620 else if (is_mimo2(rate_type))
621 return lq_sta->active_mimo2_rate;
623 return lq_sta->active_mimo3_rate;
627 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
629 u8 high = IWL_RATE_INVALID;
630 u8 low = IWL_RATE_INVALID;
632 /* 802.11A or ht walks to the next literal adjacent rate in
634 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
638 /* Find the previous rate that is in the rate mask */
640 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
641 if (rate_mask & mask) {
647 /* Find the next rate that is in the rate mask */
649 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
650 if (rate_mask & mask) {
656 return (high << 8) | low;
660 while (low != IWL_RATE_INVALID) {
661 low = iwl4965_rates[low].prev_rs;
662 if (low == IWL_RATE_INVALID)
664 if (rate_mask & (1 << low))
666 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
670 while (high != IWL_RATE_INVALID) {
671 high = iwl4965_rates[high].next_rs;
672 if (high == IWL_RATE_INVALID)
674 if (rate_mask & (1 << high))
676 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
679 return (high << 8) | low;
682 static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
683 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
689 u8 switch_to_legacy = 0;
690 u8 is_green = lq_sta->is_green;
692 /* check if we need to switch from HT to legacy rates.
693 * assumption is that mandatory rates (1Mbps or 6Mbps)
694 * are always supported (spec demand) */
695 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
696 switch_to_legacy = 1;
697 scale_index = rs_ht_to_legacy[scale_index];
698 if (lq_sta->band == IEEE80211_BAND_5GHZ)
703 if (num_of_ant(tbl->ant_type > 1))
704 tbl->ant_type = ANT_A;/*FIXME:RS*/
710 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
712 /* Mask with station rate restriction */
713 if (is_legacy(tbl->lq_type)) {
714 /* supp_rates has no CCK bits in A mode */
715 if (lq_sta->band == IEEE80211_BAND_5GHZ)
716 rate_mask = (u16)(rate_mask &
717 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
719 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
722 /* If we switched from HT to legacy, check current rate */
723 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
728 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
729 low = high_low & 0xff;
731 if (low == IWL_RATE_INVALID)
735 return rate_n_flags_from_tbl(tbl, low, is_green);
739 * mac80211 sends us Tx status
741 static void rs_tx_status(void *priv_rate, struct net_device *dev,
743 struct ieee80211_tx_status *tx_resp)
747 int rs_index, index = 0;
748 struct iwl4965_lq_sta *lq_sta;
749 struct iwl_link_quality_cmd *table;
750 struct sta_info *sta;
751 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
752 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
753 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
754 struct ieee80211_hw *hw = local_to_hw(local);
755 struct iwl4965_rate_scale_data *window = NULL;
756 struct iwl4965_rate_scale_data *search_win = NULL;
758 struct iwl4965_scale_tbl_info tbl_type;
759 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
761 u16 fc = le16_to_cpu(hdr->frame_control);
764 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
766 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
769 /* This packet was aggregated but doesn't carry rate scale info */
770 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
771 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
774 retries = tx_resp->retry_count;
781 sta = sta_info_get(local, hdr->addr1);
783 if (!sta || !sta->rate_ctrl_priv)
787 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
789 if (!priv->lq_mngr.lq_ready)
792 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
793 !lq_sta->ibss_sta_added)
797 active_index = lq_sta->active_tbl;
799 lq_sta->antenna = lq_sta->valid_antenna;
801 curr_tbl = &(lq_sta->lq_info[active_index]);
802 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
803 window = (struct iwl4965_rate_scale_data *)
805 search_win = (struct iwl4965_rate_scale_data *)
806 &(search_tbl->win[0]);
809 * Ignore this Tx frame response if its initial rate doesn't match
810 * that of latest Link Quality command. There may be stragglers
811 * from a previous Link Quality command, but we're no longer interested
812 * in those; they're either from the "active" mode while we're trying
813 * to check "search" mode, or a prior "search" mode after we've moved
814 * to a new "search" mode (which might become the new "active" mode).
816 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
817 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
818 if (priv->band == IEEE80211_BAND_5GHZ)
819 rs_index -= IWL_FIRST_OFDM_RATE;
821 if ((tx_resp->control.tx_rate == NULL) ||
823 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
825 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
827 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
828 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
829 (!!(tx_rate & RATE_MCS_HT_MSK) ^
830 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
831 (!!(tx_rate & RATE_MCS_GF_MSK) ^
832 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
833 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
834 tx_resp->control.tx_rate->bitrate)) {
835 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
839 /* Update frame history window with "failure" for each Tx retry. */
841 /* Look up the rate and other info used for each tx attempt.
842 * Each tx attempt steps one entry deeper in the rate table. */
843 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
844 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
845 &tbl_type, &rs_index);
847 /* If type matches "search" table,
848 * add failure to "search" history */
849 if ((tbl_type.lq_type == search_tbl->lq_type) &&
850 (tbl_type.ant_type == search_tbl->ant_type) &&
851 (tbl_type.is_SGI == search_tbl->is_SGI)) {
852 if (search_tbl->expected_tpt)
853 tpt = search_tbl->expected_tpt[rs_index];
856 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
858 /* Else if type matches "current/active" table,
859 * add failure to "current/active" history */
860 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
861 (tbl_type.ant_type == curr_tbl->ant_type) &&
862 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
863 if (curr_tbl->expected_tpt)
864 tpt = curr_tbl->expected_tpt[rs_index];
867 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
870 /* If not searching for a new mode, increment failed counter
871 * ... this helps determine when to start searching again */
872 if (lq_sta->stay_in_tbl)
873 lq_sta->total_failed++;
880 * Find (by rate) the history window to update with final Tx attempt;
881 * if Tx was successful first try, use original rate,
882 * else look up the rate that was, finally, successful.
884 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
885 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
887 /* Update frame history window with "success" if Tx got ACKed ... */
888 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
893 /* If type matches "search" table,
894 * add final tx status to "search" history */
895 if ((tbl_type.lq_type == search_tbl->lq_type) &&
896 (tbl_type.ant_type == search_tbl->ant_type) &&
897 (tbl_type.is_SGI == search_tbl->is_SGI)) {
898 if (search_tbl->expected_tpt)
899 tpt = search_tbl->expected_tpt[rs_index];
902 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
903 rs_collect_tx_data(search_win, rs_index, tpt,
904 tx_resp->ampdu_ack_len,
905 tx_resp->ampdu_ack_map);
907 rs_collect_tx_data(search_win, rs_index, tpt,
909 /* Else if type matches "current/active" table,
910 * add final tx status to "current/active" history */
911 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
912 (tbl_type.ant_type == curr_tbl->ant_type) &&
913 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
914 if (curr_tbl->expected_tpt)
915 tpt = curr_tbl->expected_tpt[rs_index];
918 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
919 rs_collect_tx_data(window, rs_index, tpt,
920 tx_resp->ampdu_ack_len,
921 tx_resp->ampdu_ack_map);
923 rs_collect_tx_data(window, rs_index, tpt,
927 /* If not searching for new mode, increment success/failed counter
928 * ... these help determine when to start searching again */
929 if (lq_sta->stay_in_tbl) {
930 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
931 lq_sta->total_success += tx_resp->ampdu_ack_map;
932 lq_sta->total_failed +=
933 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
936 lq_sta->total_success++;
938 lq_sta->total_failed++;
942 /* See if there's a better rate or modulation mode to try. */
943 rs_rate_scale_perform(priv, dev, hdr, sta);
949 static inline u8 rs_is_ant_connected(u8 valid_antenna, u8 ant_type)
951 return ((ant_type & valid_antenna) == ant_type);
954 /*FIXME:RS: this function should be replaced*/
955 static u8 rs_is_other_ant_connected(u8 valid_antenna, u8 ant_type)
957 if (ant_type == ANT_B)
958 return rs_is_ant_connected(valid_antenna, ANT_A);
960 return rs_is_ant_connected(valid_antenna, ANT_B);
966 * Begin a period of staying with a selected modulation mode.
967 * Set "stay_in_tbl" flag to prevent any mode switches.
968 * Set frame tx success limits according to legacy vs. high-throughput,
969 * and reset overall (spanning all rates) tx success history statistics.
970 * These control how long we stay using same modulation mode before
971 * searching for a new mode.
973 static void rs_set_stay_in_table(u8 is_legacy,
974 struct iwl4965_lq_sta *lq_sta)
976 IWL_DEBUG_RATE("we are staying in the same table\n");
977 lq_sta->stay_in_tbl = 1; /* only place this gets set */
979 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
980 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
981 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
983 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
984 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
985 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
987 lq_sta->table_count = 0;
988 lq_sta->total_failed = 0;
989 lq_sta->total_success = 0;
993 * Find correct throughput table for given mode of modulation
995 static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
996 struct iwl4965_scale_tbl_info *tbl)
998 if (is_legacy(tbl->lq_type)) {
999 if (!is_a_band(tbl->lq_type))
1000 tbl->expected_tpt = expected_tpt_G;
1002 tbl->expected_tpt = expected_tpt_A;
1003 } else if (is_siso(tbl->lq_type)) {
1004 if (tbl->is_fat && !lq_sta->is_dup)
1006 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1008 tbl->expected_tpt = expected_tpt_siso40MHz;
1009 else if (tbl->is_SGI)
1010 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1012 tbl->expected_tpt = expected_tpt_siso20MHz;
1014 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
1015 if (tbl->is_fat && !lq_sta->is_dup)
1017 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1019 tbl->expected_tpt = expected_tpt_mimo40MHz;
1020 else if (tbl->is_SGI)
1021 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1023 tbl->expected_tpt = expected_tpt_mimo20MHz;
1025 tbl->expected_tpt = expected_tpt_G;
1028 #ifdef CONFIG_IWL4965_HT
1030 * Find starting rate for new "search" high-throughput mode of modulation.
1031 * Goal is to find lowest expected rate (under perfect conditions) that is
1032 * above the current measured throughput of "active" mode, to give new mode
1033 * a fair chance to prove itself without too many challenges.
1035 * This gets called when transitioning to more aggressive modulation
1036 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1037 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1038 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1039 * bit rate will typically need to increase, but not if performance was bad.
1041 static s32 rs_get_best_rate(struct iwl_priv *priv,
1042 struct iwl4965_lq_sta *lq_sta,
1043 struct iwl4965_scale_tbl_info *tbl, /* "search" */
1044 u16 rate_mask, s8 index, s8 rate)
1046 /* "active" values */
1047 struct iwl4965_scale_tbl_info *active_tbl =
1048 &(lq_sta->lq_info[lq_sta->active_tbl]);
1049 s32 active_sr = active_tbl->win[index].success_ratio;
1050 s32 active_tpt = active_tbl->expected_tpt[index];
1052 /* expected "search" throughput */
1053 s32 *tpt_tbl = tbl->expected_tpt;
1055 s32 new_rate, high, low, start_hi;
1058 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1061 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1063 low = high_low & 0xff;
1064 high = (high_low >> 8) & 0xff;
1067 * Lower the "search" bit rate, to give new "search" mode
1068 * approximately the same throughput as "active" if:
1070 * 1) "Active" mode has been working modestly well (but not
1071 * great), and expected "search" throughput (under perfect
1072 * conditions) at candidate rate is above the actual
1073 * measured "active" throughput (but less than expected
1074 * "active" throughput under perfect conditions).
1076 * 2) "Active" mode has been working perfectly or very well
1077 * and expected "search" throughput (under perfect
1078 * conditions) at candidate rate is above expected
1079 * "active" throughput (under perfect conditions).
1081 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1082 ((active_sr > IWL_RATE_DECREASE_TH) &&
1083 (active_sr <= IWL_RATE_HIGH_TH) &&
1084 (tpt_tbl[rate] <= active_tpt))) ||
1085 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1086 (tpt_tbl[rate] > active_tpt))) {
1088 /* (2nd or later pass)
1089 * If we've already tried to raise the rate, and are
1090 * now trying to lower it, use the higher rate. */
1091 if (start_hi != IWL_RATE_INVALID) {
1092 new_rate = start_hi;
1098 /* Loop again with lower rate */
1099 if (low != IWL_RATE_INVALID)
1102 /* Lower rate not available, use the original */
1106 /* Else try to raise the "search" rate to match "active" */
1108 /* (2nd or later pass)
1109 * If we've already tried to lower the rate, and are
1110 * now trying to raise it, use the lower rate. */
1111 if (new_rate != IWL_RATE_INVALID)
1114 /* Loop again with higher rate */
1115 else if (high != IWL_RATE_INVALID) {
1119 /* Higher rate not available, use the original */
1129 #endif /* CONFIG_IWL4965_HT */
1131 /*FIXME:RS:this function should be replaced*/
1132 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1134 return (rs_is_ant_connected(valid_antenna, ANT_AB));
1138 * Set up search table for MIMO
1140 #ifdef CONFIG_IWL4965_HT
1141 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1142 struct iwl4965_lq_sta *lq_sta,
1143 struct ieee80211_conf *conf,
1144 struct sta_info *sta,
1145 struct iwl4965_scale_tbl_info *tbl, int index)
1149 s8 is_green = lq_sta->is_green;
1151 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1152 !sta->ht_info.ht_supported)
1155 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
1158 /* Need both Tx chains/antennas to support MIMO */
1159 if (!rs_is_both_ant_supp(priv->hw_params.valid_tx_ant))
1162 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
1164 tbl->lq_type = LQ_MIMO2;
1165 tbl->is_dup = lq_sta->is_dup;
1167 rate_mask = lq_sta->active_mimo2_rate;
1169 if (priv->current_ht_config.supported_chan_width
1170 == IWL_CHANNEL_WIDTH_40MHZ)
1175 /* FIXME: - don't toggle SGI here
1177 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1181 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1187 rs_set_expected_tpt_table(lq_sta, tbl);
1189 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
1191 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1193 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1194 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
1198 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
1200 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
1201 tbl->current_rate, is_green);
1205 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1206 struct iwl4965_lq_sta *lq_sta,
1207 struct ieee80211_conf *conf,
1208 struct sta_info *sta,
1209 struct iwl4965_scale_tbl_info *tbl, int index)
1213 #endif /*CONFIG_IWL4965_HT */
1216 * Set up search table for SISO
1218 static int rs_switch_to_siso(struct iwl_priv *priv,
1219 struct iwl4965_lq_sta *lq_sta,
1220 struct ieee80211_conf *conf,
1221 struct sta_info *sta,
1222 struct iwl4965_scale_tbl_info *tbl, int index)
1224 #ifdef CONFIG_IWL4965_HT
1226 u8 is_green = lq_sta->is_green;
1229 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1230 !sta->ht_info.ht_supported)
1233 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
1235 tbl->is_dup = lq_sta->is_dup;
1236 tbl->lq_type = LQ_SISO;
1238 rate_mask = lq_sta->active_siso_rate;
1240 if (priv->current_ht_config.supported_chan_width
1241 == IWL_CHANNEL_WIDTH_40MHZ)
1246 /* FIXME: - don't toggle SGI here
1248 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1252 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1259 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1261 rs_set_expected_tpt_table(lq_sta, tbl);
1262 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
1264 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
1265 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1266 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
1270 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
1271 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
1272 tbl->current_rate, is_green);
1277 #endif /*CONFIG_IWL4965_HT */
1281 * Try to switch to new modulation mode from legacy
1283 static int rs_move_legacy_other(struct iwl_priv *priv,
1284 struct iwl4965_lq_sta *lq_sta,
1285 struct ieee80211_conf *conf,
1286 struct sta_info *sta,
1289 struct iwl4965_scale_tbl_info *tbl =
1290 &(lq_sta->lq_info[lq_sta->active_tbl]);
1291 struct iwl4965_scale_tbl_info *search_tbl =
1292 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1293 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1294 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1295 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1296 u8 start_action = tbl->action;
1297 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1301 switch (tbl->action) {
1302 case IWL_LEGACY_SWITCH_ANTENNA:
1303 IWL_DEBUG_RATE("LQ Legacy toggle Antenna\n");
1305 search_tbl->lq_type = LQ_NONE;
1306 lq_sta->action_counter++;
1308 /* Don't change antenna if success has been great */
1309 /*FIXME:RS:not sure this is really needed*/
1310 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1313 /* Don't change antenna if other one is not connected */
1314 if (!rs_is_other_ant_connected(valid_tx_ant,
1318 /* Set up search table to try other antenna */
1319 memcpy(search_tbl, tbl, sz);
1321 rs_toggle_antenna(&search_tbl->current_rate,
1323 rs_set_expected_tpt_table(lq_sta, search_tbl);
1324 lq_sta->search_better_tbl = 1;
1327 case IWL_LEGACY_SWITCH_SISO:
1328 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
1330 /* Set up search table to try SISO */
1331 memcpy(search_tbl, tbl, sz);
1332 search_tbl->is_SGI = 0;
1333 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1336 lq_sta->search_better_tbl = 1;
1337 lq_sta->action_counter = 0;
1342 case IWL_LEGACY_SWITCH_MIMO2:
1343 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
1345 /* Set up search table to try MIMO */
1346 memcpy(search_tbl, tbl, sz);
1347 search_tbl->is_SGI = 0;
1348 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1349 /*FIXME:RS:need to check ant validity*/
1350 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1353 lq_sta->search_better_tbl = 1;
1354 lq_sta->action_counter = 0;
1360 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1361 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1363 if (tbl->action == start_action)
1371 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1372 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1378 * Try to switch to new modulation mode from SISO
1380 static int rs_move_siso_to_other(struct iwl_priv *priv,
1381 struct iwl4965_lq_sta *lq_sta,
1382 struct ieee80211_conf *conf,
1383 struct sta_info *sta,
1386 u8 is_green = lq_sta->is_green;
1387 struct iwl4965_scale_tbl_info *tbl =
1388 &(lq_sta->lq_info[lq_sta->active_tbl]);
1389 struct iwl4965_scale_tbl_info *search_tbl =
1390 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1391 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1392 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1393 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1394 u8 start_action = tbl->action;
1395 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1399 lq_sta->action_counter++;
1400 switch (tbl->action) {
1401 case IWL_SISO_SWITCH_ANTENNA:
1402 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
1403 /*FIXME:RS: is this really needed for SISO?*/
1404 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1406 if (!rs_is_other_ant_connected(valid_tx_ant,
1410 memcpy(search_tbl, tbl, sz);
1411 rs_toggle_antenna(&search_tbl->current_rate,
1413 lq_sta->search_better_tbl = 1;
1417 case IWL_SISO_SWITCH_MIMO2:
1418 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
1419 memcpy(search_tbl, tbl, sz);
1420 search_tbl->is_SGI = 0;
1421 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1422 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1425 lq_sta->search_better_tbl = 1;
1429 case IWL_SISO_SWITCH_GI:
1430 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
1432 memcpy(search_tbl, tbl, sz);
1437 IWL_ERROR("SGI was set in GF+SISO\n");
1439 search_tbl->is_SGI = !tbl->is_SGI;
1440 rs_set_expected_tpt_table(lq_sta, search_tbl);
1442 s32 tpt = lq_sta->last_tpt / 100;
1443 if (tpt >= search_tbl->expected_tpt[index])
1446 search_tbl->current_rate = rate_n_flags_from_tbl(
1447 search_tbl, index, is_green);
1448 lq_sta->search_better_tbl = 1;
1452 if (tbl->action > IWL_SISO_SWITCH_GI)
1453 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1455 if (tbl->action == start_action)
1462 if (tbl->action > IWL_SISO_SWITCH_GI)
1463 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1468 * Try to switch to new modulation mode from MIMO
1470 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1471 struct iwl4965_lq_sta *lq_sta,
1472 struct ieee80211_conf *conf,
1473 struct sta_info *sta,
1477 s8 is_green = lq_sta->is_green;
1478 struct iwl4965_scale_tbl_info *tbl =
1479 &(lq_sta->lq_info[lq_sta->active_tbl]);
1480 struct iwl4965_scale_tbl_info *search_tbl =
1481 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1482 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1483 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1484 u8 start_action = tbl->action;
1487 lq_sta->action_counter++;
1488 switch (tbl->action) {
1489 case IWL_MIMO_SWITCH_ANTENNA_A:
1490 case IWL_MIMO_SWITCH_ANTENNA_B:
1491 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
1493 /* Set up new search table for SISO */
1494 memcpy(search_tbl, tbl, sz);
1496 /*FIXME:RS:need to check ant validity + C*/
1497 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1498 search_tbl->ant_type = ANT_A;
1500 search_tbl->ant_type = ANT_B;
1502 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1505 lq_sta->search_better_tbl = 1;
1510 case IWL_MIMO_SWITCH_GI:
1511 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
1513 /* Set up new search table for MIMO */
1514 memcpy(search_tbl, tbl, sz);
1515 search_tbl->is_SGI = !tbl->is_SGI;
1516 rs_set_expected_tpt_table(lq_sta, search_tbl);
1518 * If active table already uses the fastest possible
1519 * modulation (dual stream with short guard interval),
1520 * and it's working well, there's no need to look
1521 * for a better type of modulation!
1524 s32 tpt = lq_sta->last_tpt / 100;
1525 if (tpt >= search_tbl->expected_tpt[index])
1528 search_tbl->current_rate = rate_n_flags_from_tbl(
1529 search_tbl, index, is_green);
1530 lq_sta->search_better_tbl = 1;
1535 if (tbl->action > IWL_MIMO_SWITCH_GI)
1536 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1538 if (tbl->action == start_action)
1545 if (tbl->action > IWL_MIMO_SWITCH_GI)
1546 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1552 * Check whether we should continue using same modulation mode, or
1553 * begin search for a new mode, based on:
1554 * 1) # tx successes or failures while using this mode
1555 * 2) # times calling this function
1556 * 3) elapsed time in this mode (not used, for now)
1558 static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
1560 struct iwl4965_scale_tbl_info *tbl;
1563 int flush_interval_passed = 0;
1565 active_tbl = lq_sta->active_tbl;
1567 tbl = &(lq_sta->lq_info[active_tbl]);
1569 /* If we've been disallowing search, see if we should now allow it */
1570 if (lq_sta->stay_in_tbl) {
1572 /* Elapsed time using current modulation mode */
1573 if (lq_sta->flush_timer)
1574 flush_interval_passed =
1576 (unsigned long)(lq_sta->flush_timer +
1577 IWL_RATE_SCALE_FLUSH_INTVL));
1579 /* For now, disable the elapsed time criterion */
1580 flush_interval_passed = 0;
1583 * Check if we should allow search for new modulation mode.
1584 * If many frames have failed or succeeded, or we've used
1585 * this same modulation for a long time, allow search, and
1586 * reset history stats that keep track of whether we should
1587 * allow a new search. Also (below) reset all bitmaps and
1588 * stats in active history.
1590 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1591 (lq_sta->total_success > lq_sta->max_success_limit) ||
1592 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1593 && (flush_interval_passed))) {
1594 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
1595 lq_sta->total_failed,
1596 lq_sta->total_success,
1597 flush_interval_passed);
1599 /* Allow search for new mode */
1600 lq_sta->stay_in_tbl = 0; /* only place reset */
1601 lq_sta->total_failed = 0;
1602 lq_sta->total_success = 0;
1603 lq_sta->flush_timer = 0;
1606 * Else if we've used this modulation mode enough repetitions
1607 * (regardless of elapsed time or success/failure), reset
1608 * history bitmaps and rate-specific stats for all rates in
1612 lq_sta->table_count++;
1613 if (lq_sta->table_count >=
1614 lq_sta->table_count_limit) {
1615 lq_sta->table_count = 0;
1617 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
1618 for (i = 0; i < IWL_RATE_COUNT; i++)
1619 rs_rate_scale_clear_window(
1624 /* If transitioning to allow "search", reset all history
1625 * bitmaps and stats in active table (this will become the new
1626 * "search" table). */
1627 if (!lq_sta->stay_in_tbl) {
1628 for (i = 0; i < IWL_RATE_COUNT; i++)
1629 rs_rate_scale_clear_window(&(tbl->win[i]));
1635 * Do rate scaling and search for new modulation mode.
1637 static void rs_rate_scale_perform(struct iwl_priv *priv,
1638 struct net_device *dev,
1639 struct ieee80211_hdr *hdr,
1640 struct sta_info *sta)
1642 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1643 struct ieee80211_hw *hw = local_to_hw(local);
1644 struct ieee80211_conf *conf = &hw->conf;
1645 int low = IWL_RATE_INVALID;
1646 int high = IWL_RATE_INVALID;
1649 struct iwl4965_rate_scale_data *window = NULL;
1650 int current_tpt = IWL_INVALID_VALUE;
1651 int low_tpt = IWL_INVALID_VALUE;
1652 int high_tpt = IWL_INVALID_VALUE;
1654 s8 scale_action = 0;
1657 struct iwl4965_lq_sta *lq_sta;
1658 struct iwl4965_scale_tbl_info *tbl, *tbl1;
1659 u16 rate_scale_index_msk = 0;
1665 #ifdef CONFIG_IWL4965_HT
1666 u8 tid = MAX_TID_COUNT;
1670 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1672 fc = le16_to_cpu(hdr->frame_control);
1673 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1674 /* Send management frames and broadcast/multicast data using
1676 /* TODO: this could probably be improved.. */
1680 if (!sta || !sta->rate_ctrl_priv)
1683 if (!priv->lq_mngr.lq_ready) {
1684 IWL_DEBUG_RATE("still rate scaling not ready\n");
1687 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
1689 #ifdef CONFIG_IWL4965_HT
1690 qc = ieee80211_get_qos_ctrl(hdr);
1692 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1693 rs_tl_add_packet(lq_sta, tid);
1697 * Select rate-scale / modulation-mode table to work with in
1698 * the rest of this function: "search" if searching for better
1699 * modulation mode, or "active" if doing rate scaling within a mode.
1701 if (!lq_sta->search_better_tbl)
1702 active_tbl = lq_sta->active_tbl;
1704 active_tbl = 1 - lq_sta->active_tbl;
1706 tbl = &(lq_sta->lq_info[active_tbl]);
1707 is_green = lq_sta->is_green;
1709 /* current tx rate */
1710 index = sta->last_txrate_idx;
1712 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1715 /* rates available for this association, and for modulation mode */
1716 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1718 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1720 /* mask with station rate restriction */
1721 if (is_legacy(tbl->lq_type)) {
1722 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1723 /* supp_rates has no CCK bits in A mode */
1724 rate_scale_index_msk = (u16) (rate_mask &
1725 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1727 rate_scale_index_msk = (u16) (rate_mask &
1728 lq_sta->supp_rates);
1731 rate_scale_index_msk = rate_mask;
1733 if (!rate_scale_index_msk)
1734 rate_scale_index_msk = rate_mask;
1736 /* If current rate is no longer supported on current association,
1737 * or user changed preferences for rates, find a new supported rate. */
1738 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1739 index = IWL_INVALID_VALUE;
1742 /* get the highest available rate */
1743 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1744 if ((1 << i) & rate_scale_index_msk)
1748 if (index == IWL_INVALID_VALUE) {
1749 IWL_WARNING("Can not find a suitable rate\n");
1754 /* Get expected throughput table and history window for current rate */
1755 if (!tbl->expected_tpt)
1756 rs_set_expected_tpt_table(lq_sta, tbl);
1758 window = &(tbl->win[index]);
1761 * If there is not enough history to calculate actual average
1762 * throughput, keep analyzing results of more tx frames, without
1763 * changing rate or mode (bypass most of the rest of this function).
1764 * Set up new rate table in uCode only if old rate is not supported
1765 * in current association (use new rate found above).
1767 fail_count = window->counter - window->success_counter;
1768 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1769 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1770 || (tbl->expected_tpt == NULL)) {
1771 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1773 window->success_counter, window->counter, index);
1775 /* Can't calculate this yet; not enough history */
1776 window->average_tpt = IWL_INVALID_VALUE;
1778 /* Should we stay with this modulation mode,
1779 * or search for a new one? */
1780 rs_stay_in_table(lq_sta);
1782 /* Set up new rate table in uCode, if needed */
1784 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1785 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
1786 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1790 /* Else we have enough samples; calculate estimate of
1791 * actual average throughput */
1793 window->average_tpt = ((window->success_ratio *
1794 tbl->expected_tpt[index] + 64) / 128);
1796 /* If we are searching for better modulation mode, check success. */
1797 if (lq_sta->search_better_tbl) {
1798 int success_limit = IWL_RATE_SCALE_SWITCH;
1800 /* If good success, continue using the "search" mode;
1801 * no need to send new link quality command, since we're
1802 * continuing to use the setup that we've been trying. */
1803 if ((window->success_ratio > success_limit) ||
1804 (window->average_tpt > lq_sta->last_tpt)) {
1805 if (!is_legacy(tbl->lq_type)) {
1806 IWL_DEBUG_RATE("LQ: we are switching to HT"
1807 " rate suc %d current tpt %d"
1809 window->success_ratio,
1810 window->average_tpt,
1812 lq_sta->enable_counter = 1;
1814 /* Swap tables; "search" becomes "active" */
1815 lq_sta->active_tbl = active_tbl;
1816 current_tpt = window->average_tpt;
1818 /* Else poor success; go back to mode in "active" table */
1820 /* Nullify "search" table */
1821 tbl->lq_type = LQ_NONE;
1823 /* Revert to "active" table */
1824 active_tbl = lq_sta->active_tbl;
1825 tbl = &(lq_sta->lq_info[active_tbl]);
1827 /* Revert to "active" rate and throughput info */
1828 index = iwl4965_hwrate_to_plcp_idx(
1830 current_tpt = lq_sta->last_tpt;
1832 /* Need to set up a new rate table in uCode */
1834 IWL_DEBUG_RATE("XXY GO BACK TO OLD TABLE\n");
1837 /* Either way, we've made a decision; modulation mode
1838 * search is done, allow rate adjustment next time. */
1839 lq_sta->search_better_tbl = 0;
1840 done_search = 1; /* Don't switch modes below! */
1844 /* (Else) not in search of better modulation mode, try for better
1845 * starting rate, while staying in this mode. */
1846 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1848 low = high_low & 0xff;
1849 high = (high_low >> 8) & 0xff;
1851 /* Collect measured throughputs for current and adjacent rates */
1852 current_tpt = window->average_tpt;
1853 if (low != IWL_RATE_INVALID)
1854 low_tpt = tbl->win[low].average_tpt;
1855 if (high != IWL_RATE_INVALID)
1856 high_tpt = tbl->win[high].average_tpt;
1858 /* Assume rate increase */
1861 /* Too many failures, decrease rate */
1862 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1863 (current_tpt == 0)) {
1864 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1867 /* No throughput measured yet for adjacent rates; try increase. */
1868 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1869 (high_tpt == IWL_INVALID_VALUE))
1872 /* Both adjacent throughputs are measured, but neither one has better
1873 * throughput; we're using the best rate, don't change it! */
1874 else if ((low_tpt != IWL_INVALID_VALUE) &&
1875 (high_tpt != IWL_INVALID_VALUE) &&
1876 (low_tpt < current_tpt) &&
1877 (high_tpt < current_tpt))
1880 /* At least one adjacent rate's throughput is measured,
1881 * and may have better performance. */
1883 /* Higher adjacent rate's throughput is measured */
1884 if (high_tpt != IWL_INVALID_VALUE) {
1885 /* Higher rate has better throughput */
1886 if (high_tpt > current_tpt)
1890 ("decrease rate because of high tpt\n");
1894 /* Lower adjacent rate's throughput is measured */
1895 } else if (low_tpt != IWL_INVALID_VALUE) {
1896 /* Lower rate has better throughput */
1897 if (low_tpt > current_tpt) {
1899 ("decrease rate because of low tpt\n");
1906 /* Sanity check; asked for decrease, but success rate or throughput
1907 * has been good at old rate. Don't change it. */
1908 if (scale_action == -1) {
1909 if ((low != IWL_RATE_INVALID) &&
1910 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1911 (current_tpt > (100 * tbl->expected_tpt[low]))))
1914 /* Sanity check; asked for increase, but success rate has not been great
1915 * even at old rate, higher rate will be worse. Don't change it. */
1916 } else if ((scale_action == 1) &&
1917 (window->success_ratio < IWL_RATE_INCREASE_TH))
1920 switch (scale_action) {
1922 /* Decrease starting rate, update uCode's rate table */
1923 if (low != IWL_RATE_INVALID) {
1929 /* Increase starting rate, update uCode's rate table */
1930 if (high != IWL_RATE_INVALID) {
1942 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
1943 "high %d type %d\n",
1944 index, scale_action, low, high, tbl->lq_type);
1947 /* Replace uCode's rate table for the destination station. */
1949 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1950 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
1951 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1954 /* Should we stay with this modulation mode, or search for a new one? */
1955 rs_stay_in_table(lq_sta);
1958 * Search for new modulation mode if we're:
1959 * 1) Not changing rates right now
1960 * 2) Not just finishing up a search
1961 * 3) Allowing a new search
1963 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
1964 /* Save current throughput to compare with "search" throughput*/
1965 lq_sta->last_tpt = current_tpt;
1967 /* Select a new "search" modulation mode to try.
1968 * If one is found, set up the new "search" table. */
1969 if (is_legacy(tbl->lq_type))
1970 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
1971 else if (is_siso(tbl->lq_type))
1972 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
1974 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
1976 /* If new "search" mode was selected, set up in uCode table */
1977 if (lq_sta->search_better_tbl) {
1978 /* Access the "search" table, clear its history. */
1979 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1980 for (i = 0; i < IWL_RATE_COUNT; i++)
1981 rs_rate_scale_clear_window(&(tbl->win[i]));
1983 /* Use new "search" start rate */
1984 index = iwl4965_hwrate_to_plcp_idx(
1987 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
1988 tbl->current_rate, index);
1989 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate,
1991 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1994 /* If the "active" (non-search) mode was legacy,
1995 * and we've tried switching antennas,
1996 * but we haven't been able to try HT modes (not available),
1997 * stay with best antenna legacy modulation for a while
1998 * before next round of mode comparisons. */
1999 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2000 if (is_legacy(tbl1->lq_type) &&
2001 #ifdef CONFIG_IWL4965_HT
2002 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
2004 (lq_sta->action_counter >= 1)) {
2005 lq_sta->action_counter = 0;
2006 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
2007 rs_set_stay_in_table(1, lq_sta);
2010 /* If we're in an HT mode, and all 3 mode switch actions
2011 * have been tried and compared, stay in this best modulation
2012 * mode for a while before next round of mode comparisons. */
2013 if (lq_sta->enable_counter &&
2014 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
2015 #ifdef CONFIG_IWL4965_HT
2016 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2017 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2018 (tid != MAX_TID_COUNT)) {
2019 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
2020 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
2022 #endif /*CONFIG_IWL4965_HT */
2023 lq_sta->action_counter = 0;
2024 rs_set_stay_in_table(0, lq_sta);
2028 * Else, don't search for a new modulation mode.
2029 * Put new timestamp in stay-in-modulation-mode flush timer if:
2030 * 1) Not changing rates right now
2031 * 2) Not just finishing up a search
2032 * 3) flush timer is empty
2035 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2036 lq_sta->flush_timer = jiffies;
2040 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
2042 sta->last_txrate_idx = i;
2044 /* sta->txrate_idx is an index to A mode rates which start
2045 * at IWL_FIRST_OFDM_RATE
2047 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2048 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
2050 sta->txrate_idx = i;
2056 static void rs_initialize_lq(struct iwl_priv *priv,
2057 struct ieee80211_conf *conf,
2058 struct sta_info *sta)
2061 struct iwl4965_lq_sta *lq_sta;
2062 struct iwl4965_scale_tbl_info *tbl;
2065 u8 use_green = rs_use_green(priv, conf);
2068 if (!sta || !sta->rate_ctrl_priv)
2071 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2072 i = sta->last_txrate_idx;
2074 if ((lq_sta->lq.sta_id == 0xff) &&
2075 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2078 if (!lq_sta->search_better_tbl)
2079 active_tbl = lq_sta->active_tbl;
2081 active_tbl = 1 - lq_sta->active_tbl;
2083 tbl = &(lq_sta->lq_info[active_tbl]);
2085 if ((i < 0) || (i >= IWL_RATE_COUNT))
2088 /* FIXME:RS: This is also wrong in 4965 */
2089 rate = iwl4965_rates[i].plcp;
2090 rate |= RATE_MCS_ANT_B_MSK;
2091 rate &= ~RATE_MCS_ANT_A_MSK;
2093 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2094 rate |= RATE_MCS_CCK_MSK;
2096 tbl->ant_type = ANT_B;
2097 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2098 if (!rs_is_ant_connected(priv->hw_params.valid_tx_ant, tbl->ant_type))
2099 rs_toggle_antenna(&rate, tbl);
2101 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2102 tbl->current_rate = rate;
2103 rs_set_expected_tpt_table(lq_sta, tbl);
2104 rs_fill_link_cmd(NULL, lq_sta, rate, &lq_sta->lq);
2105 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2110 static void rs_get_rate(void *priv_rate, struct net_device *dev,
2111 struct ieee80211_supported_band *sband,
2112 struct sk_buff *skb,
2113 struct rate_selection *sel)
2117 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2118 struct ieee80211_conf *conf = &local->hw.conf;
2119 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2120 struct sta_info *sta;
2122 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
2123 struct iwl4965_lq_sta *lq_sta;
2125 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
2129 sta = sta_info_get(local, hdr->addr1);
2131 /* Send management frames and broadcast/multicast data using lowest
2133 fc = le16_to_cpu(hdr->frame_control);
2134 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2135 !sta || !sta->rate_ctrl_priv) {
2136 sel->rate = rate_lowest(local, sband, sta);
2140 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2141 i = sta->last_txrate_idx;
2143 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2144 !lq_sta->ibss_sta_added) {
2145 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2146 DECLARE_MAC_BUF(mac);
2148 if (sta_id == IWL_INVALID_STATION) {
2149 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2150 print_mac(mac, hdr->addr1));
2151 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2152 0, CMD_ASYNC, NULL);
2154 if ((sta_id != IWL_INVALID_STATION)) {
2155 lq_sta->lq.sta_id = sta_id;
2156 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2157 lq_sta->ibss_sta_added = 1;
2158 rs_initialize_lq(priv, conf, sta);
2160 if (!lq_sta->ibss_sta_added)
2165 if ((i < 0) || (i > IWL_RATE_COUNT)) {
2166 sel->rate = rate_lowest(local, sband, sta);
2170 sel->rate = &priv->ieee_rates[i];
2175 static void *rs_alloc_sta(void *priv, gfp_t gfp)
2177 struct iwl4965_lq_sta *lq_sta;
2180 IWL_DEBUG_RATE("create station rate scale window\n");
2182 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
2186 lq_sta->lq.sta_id = 0xff;
2189 for (j = 0; j < LQ_SIZE; j++)
2190 for (i = 0; i < IWL_RATE_COUNT; i++)
2191 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
2196 static void rs_rate_init(void *priv_rate, void *priv_sta,
2197 struct ieee80211_local *local,
2198 struct sta_info *sta)
2201 struct ieee80211_conf *conf = &local->hw.conf;
2202 struct ieee80211_supported_band *sband;
2203 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
2204 struct iwl4965_lq_sta *lq_sta = priv_sta;
2206 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2208 lq_sta->flush_timer = 0;
2209 lq_sta->supp_rates = sta->supp_rates[sband->band];
2210 sta->txrate_idx = 3;
2211 for (j = 0; j < LQ_SIZE; j++)
2212 for (i = 0; i < IWL_RATE_COUNT; i++)
2213 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
2215 IWL_DEBUG_RATE("rate scale global init\n");
2216 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2217 * the lowest or the highest rate.. Could consider using RSSI from
2218 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2221 lq_sta->ibss_sta_added = 0;
2222 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2223 u8 sta_id = iwl_find_station(priv, sta->addr);
2224 DECLARE_MAC_BUF(mac);
2226 /* for IBSS the call are from tasklet */
2227 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2228 print_mac(mac, sta->addr));
2230 if (sta_id == IWL_INVALID_STATION) {
2231 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2232 print_mac(mac, sta->addr));
2233 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2234 0, CMD_ASYNC, NULL);
2236 if ((sta_id != IWL_INVALID_STATION)) {
2237 lq_sta->lq.sta_id = sta_id;
2238 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2240 /* FIXME: this is w/a remove it later */
2241 priv->assoc_station_added = 1;
2244 /* Find highest tx rate supported by hardware and destination station */
2245 for (i = 0; i < sband->n_bitrates; i++)
2246 if (sta->supp_rates[sband->band] & BIT(i))
2247 sta->txrate_idx = i;
2249 sta->last_txrate_idx = sta->txrate_idx;
2250 /* WTF is with this bogus comment? A doesn't have cck rates */
2251 /* For MODE_IEEE80211A, cck rates are at end of rate table */
2252 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2253 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2256 lq_sta->is_green = rs_use_green(priv, conf);
2257 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2258 lq_sta->active_rate_basic = priv->active_rate_basic;
2259 lq_sta->band = priv->band;
2260 #ifdef CONFIG_IWL4965_HT
2262 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2263 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2265 lq_sta->active_siso_rate =
2266 priv->current_ht_config.supp_mcs_set[0] << 1;
2267 lq_sta->active_siso_rate |=
2268 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2269 lq_sta->active_siso_rate &= ~((u16)0x2);
2270 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2273 lq_sta->active_mimo2_rate =
2274 priv->current_ht_config.supp_mcs_set[1] << 1;
2275 lq_sta->active_mimo2_rate |=
2276 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2277 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2278 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2280 lq_sta->active_mimo3_rate =
2281 priv->current_ht_config.supp_mcs_set[2] << 1;
2282 lq_sta->active_mimo3_rate |=
2283 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2284 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2285 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2287 IWL_DEBUG_RATE("SISO RATE %X MIMO2 RATE %X MIMO3 RATE %X\n",
2288 lq_sta->active_siso_rate,
2289 lq_sta->active_mimo2_rate,
2290 lq_sta->active_mimo3_rate);
2292 /* as default allow aggregation for all tids */
2293 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2294 #endif /*CONFIG_IWL4965_HT*/
2295 #ifdef CONFIG_MAC80211_DEBUGFS
2299 if (priv->assoc_station_added)
2300 priv->lq_mngr.lq_ready = 1;
2302 rs_initialize_lq(priv, conf, sta);
2305 static void rs_fill_link_cmd(const struct iwl_priv *priv,
2306 struct iwl4965_lq_sta *lq_sta,
2308 struct iwl_link_quality_cmd *lq_cmd)
2312 int repeat_rate = 0;
2313 u8 ant_toggle_count = 0;
2314 u8 use_ht_possible = 1;
2315 struct iwl4965_scale_tbl_info tbl_type = { 0 };
2317 /* Override starting rate (index 0) if needed for debug purposes */
2318 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2320 /* Interpret new_rate (rate_n_flags) */
2321 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2322 &tbl_type, &rate_idx);
2324 /* How many times should we repeat the initial rate? */
2325 if (is_legacy(tbl_type.lq_type)) {
2326 ant_toggle_count = 1;
2327 repeat_rate = IWL_NUMBER_TRY;
2329 repeat_rate = IWL_HT_NUMBER_TRY;
2331 lq_cmd->general_params.mimo_delimiter =
2332 is_mimo(tbl_type.lq_type) ? 1 : 0;
2334 /* Fill 1st table entry (index 0) */
2335 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2338 if (is_mimo(tbl_type.lq_type) || (tbl_type.ant_type == ANT_A))
2339 lq_cmd->general_params.single_stream_ant_msk
2340 = LINK_QUAL_ANT_A_MSK;
2342 lq_cmd->general_params.single_stream_ant_msk
2343 = LINK_QUAL_ANT_B_MSK;
2348 /* Fill rest of rate table */
2349 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2350 /* Repeat initial/next rate.
2351 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2352 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2353 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2354 if (is_legacy(tbl_type.lq_type)) {
2355 if (ant_toggle_count <
2356 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2358 else if (priv && rs_is_other_ant_connected(
2359 priv->hw_params.valid_tx_ant,
2360 tbl_type.ant_type)) {
2361 rs_toggle_antenna(&new_rate, &tbl_type);
2362 ant_toggle_count = 1;
2366 /* Override next rate if needed for debug purposes */
2367 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2369 /* Fill next table entry */
2370 lq_cmd->rs_table[index].rate_n_flags =
2371 cpu_to_le32(new_rate);
2376 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2379 /* Indicate to uCode which entries might be MIMO.
2380 * If initial rate was MIMO, this will finally end up
2381 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2382 if (is_mimo(tbl_type.lq_type))
2383 lq_cmd->general_params.mimo_delimiter = index;
2386 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2389 /* How many times should we repeat the next rate? */
2390 if (is_legacy(tbl_type.lq_type)) {
2391 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2393 else if (priv && rs_is_other_ant_connected(
2394 priv->hw_params.valid_tx_ant,
2395 tbl_type.ant_type)) {
2396 rs_toggle_antenna(&new_rate, &tbl_type);
2397 ant_toggle_count = 1;
2399 repeat_rate = IWL_NUMBER_TRY;
2401 repeat_rate = IWL_HT_NUMBER_TRY;
2403 /* Don't allow HT rates after next pass.
2404 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2405 use_ht_possible = 0;
2407 /* Override next rate if needed for debug purposes */
2408 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2410 /* Fill next table entry */
2411 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2417 lq_cmd->general_params.dual_stream_ant_msk = 3;
2418 lq_cmd->agg_params.agg_dis_start_th = 3;
2419 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
2422 static void *rs_alloc(struct ieee80211_local *local)
2424 return local->hw.priv;
2426 /* rate scale requires free function to be implemented */
2427 static void rs_free(void *priv_rate)
2432 static void rs_clear(void *priv_rate)
2434 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
2436 IWL_DEBUG_RATE("enter\n");
2438 priv->lq_mngr.lq_ready = 0;
2440 IWL_DEBUG_RATE("leave\n");
2443 static void rs_free_sta(void *priv, void *priv_sta)
2445 struct iwl4965_lq_sta *lq_sta = priv_sta;
2447 IWL_DEBUG_RATE("enter\n");
2449 IWL_DEBUG_RATE("leave\n");
2453 #ifdef CONFIG_MAC80211_DEBUGFS
2454 static int open_file_generic(struct inode *inode, struct file *file)
2456 file->private_data = inode->i_private;
2459 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
2460 u32 *rate_n_flags, int index)
2462 if (lq_sta->dbg_fixed_rate) {
2464 *rate_n_flags = lq_sta->dbg_fixed_rate;
2466 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2467 *rate_n_flags = 0x800D;
2469 *rate_n_flags = 0x820A;
2471 IWL_DEBUG_RATE("Fixed rate ON\n");
2473 IWL_DEBUG_RATE("Fixed rate OFF\n");
2477 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2478 const char __user *user_buf, size_t count, loff_t *ppos)
2480 struct iwl4965_lq_sta *lq_sta = file->private_data;
2485 memset(buf, 0, sizeof(buf));
2486 buf_size = min(count, sizeof(buf) - 1);
2487 if (copy_from_user(buf, user_buf, buf_size))
2490 if (sscanf(buf, "%x", &parsed_rate) == 1)
2491 lq_sta->dbg_fixed_rate = parsed_rate;
2493 lq_sta->dbg_fixed_rate = 0;
2495 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2496 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2497 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2498 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2500 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2501 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2503 if (lq_sta->dbg_fixed_rate) {
2504 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate,
2506 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2512 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2513 char __user *user_buf, size_t count, loff_t *ppos)
2519 struct iwl4965_lq_sta *lq_sta = file->private_data;
2521 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2522 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2523 lq_sta->total_failed, lq_sta->total_success,
2524 lq_sta->active_legacy_rate);
2525 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2526 lq_sta->dbg_fixed_rate);
2527 desc += sprintf(buff+desc, "general:"
2528 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2529 lq_sta->lq.general_params.flags,
2530 lq_sta->lq.general_params.mimo_delimiter,
2531 lq_sta->lq.general_params.single_stream_ant_msk,
2532 lq_sta->lq.general_params.dual_stream_ant_msk);
2534 desc += sprintf(buff+desc, "agg:"
2535 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2536 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2537 lq_sta->lq.agg_params.agg_dis_start_th,
2538 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2540 desc += sprintf(buff+desc,
2541 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2542 lq_sta->lq.general_params.start_rate_index[0],
2543 lq_sta->lq.general_params.start_rate_index[1],
2544 lq_sta->lq.general_params.start_rate_index[2],
2545 lq_sta->lq.general_params.start_rate_index[3]);
2548 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2549 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2550 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2552 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2555 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2556 .write = rs_sta_dbgfs_scale_table_write,
2557 .read = rs_sta_dbgfs_scale_table_read,
2558 .open = open_file_generic,
2560 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2561 char __user *user_buf, size_t count, loff_t *ppos)
2567 struct iwl4965_lq_sta *lq_sta = file->private_data;
2568 for (i = 0; i < LQ_SIZE; i++) {
2569 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2571 lq_sta->active_tbl == i?"*":"x",
2572 lq_sta->lq_info[i].lq_type,
2573 lq_sta->lq_info[i].is_SGI,
2574 lq_sta->lq_info[i].is_fat,
2575 lq_sta->lq_info[i].is_dup,
2576 lq_sta->lq_info[i].current_rate);
2577 for (j = 0; j < IWL_RATE_COUNT; j++) {
2578 desc += sprintf(buff+desc,
2579 "counter=%d success=%d %%=%d\n",
2580 lq_sta->lq_info[i].win[j].counter,
2581 lq_sta->lq_info[i].win[j].success_counter,
2582 lq_sta->lq_info[i].win[j].success_ratio);
2585 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2588 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2589 .read = rs_sta_dbgfs_stats_table_read,
2590 .open = open_file_generic,
2593 static void rs_add_debugfs(void *priv, void *priv_sta,
2596 struct iwl4965_lq_sta *lq_sta = priv_sta;
2597 lq_sta->rs_sta_dbgfs_scale_table_file =
2598 debugfs_create_file("rate_scale_table", 0600, dir,
2599 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2600 lq_sta->rs_sta_dbgfs_stats_table_file =
2601 debugfs_create_file("rate_stats_table", 0600, dir,
2602 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2603 #ifdef CONFIG_IWL4965_HT
2604 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2605 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2606 &lq_sta->tx_agg_tid_en);
2611 static void rs_remove_debugfs(void *priv, void *priv_sta)
2613 struct iwl4965_lq_sta *lq_sta = priv_sta;
2614 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2615 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2616 #ifdef CONFIG_IWL4965_HT
2617 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2622 static struct rate_control_ops rs_ops = {
2625 .tx_status = rs_tx_status,
2626 .get_rate = rs_get_rate,
2627 .rate_init = rs_rate_init,
2631 .alloc_sta = rs_alloc_sta,
2632 .free_sta = rs_free_sta,
2633 #ifdef CONFIG_MAC80211_DEBUGFS
2634 .add_sta_debugfs = rs_add_debugfs,
2635 .remove_sta_debugfs = rs_remove_debugfs,
2639 int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2641 struct ieee80211_local *local = hw_to_local(hw);
2642 struct iwl_priv *priv = hw->priv;
2643 struct iwl4965_lq_sta *lq_sta;
2644 struct sta_info *sta;
2646 u32 samples = 0, success = 0, good = 0;
2647 unsigned long now = jiffies;
2649 u8 lq_type, antenna;
2653 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2654 if (!sta || !sta->rate_ctrl_priv) {
2656 IWL_DEBUG_RATE("leave - no private rate data!\n");
2658 IWL_DEBUG_RATE("leave - no station!\n");
2660 return sprintf(buf, "station %d not found\n", sta_id);
2663 lq_sta = (void *)sta->rate_ctrl_priv;
2665 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
2666 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
2668 if (is_legacy(lq_type))
2669 i = IWL_RATE_54M_INDEX;
2671 i = IWL_RATE_60M_INDEX;
2675 int active = lq_sta->active_tbl;
2678 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
2680 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2681 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2683 (lq_sta->lq_info[active].win[i].data & mask)
2686 samples += lq_sta->lq_info[active].win[i].counter;
2687 good += lq_sta->lq_info[active].win[i].success_counter;
2688 success += lq_sta->lq_info[active].win[i].success_counter *
2689 iwl4965_rates[i].ieee;
2691 if (lq_sta->lq_info[active].win[i].stamp) {
2693 jiffies_to_msecs(now -
2694 lq_sta->lq_info[active].win[i].stamp);
2696 if (delta > max_time)
2699 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
2703 j = iwl4965_get_prev_ieee_rate(i);
2709 /* Display the average rate of all samples taken.
2711 * NOTE: We multiply # of samples by 2 since the IEEE measurement
2712 * added from iwl4965_rates is actually 2X the rate */
2714 cnt += sprintf(&buf[cnt],
2715 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2716 "%3d%% success (%d good packets over %d tries)\n",
2717 success / (2 * samples), (success * 5 / samples) % 10,
2718 max_time, good * 100 / samples, good, samples);
2720 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2722 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
2723 "active_search %d rate index %d\n", lq_type, antenna,
2724 lq_sta->search_better_tbl, sta->last_txrate_idx);
2730 void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2732 struct iwl_priv *priv = hw->priv;
2734 priv->lq_mngr.lq_ready = 1;
2737 int iwl4965_rate_control_register(void)
2739 return ieee80211_rate_control_register(&rs_ops);
2742 void iwl4965_rate_control_unregister(void)
2744 ieee80211_rate_control_unregister(&rs_ops);