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:
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;
172 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,
184 #ifdef CONFIG_MAC80211_DEBUGFS
185 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
186 u32 *rate_n_flags, int index);
188 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
189 u32 *rate_n_flags, int index)
194 * Expected throughput metrics for following rates:
195 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196 * "G" is the only table that supports CCK (the first 4 rates).
198 /*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
199 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
203 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
207 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
211 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
215 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
219 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
223 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
227 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
231 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
235 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
236 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
239 static inline u8 rs_extract_rate(u32 rate_n_flags)
241 return (u8)(rate_n_flags & 0xFF);
244 static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
247 window->success_counter = 0;
248 window->success_ratio = IWL_INVALID_VALUE;
250 window->average_tpt = IWL_INVALID_VALUE;
254 static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
256 return ((ant_type & valid_antenna) == ant_type);
259 #ifdef CONFIG_IWL4965_HT
261 * removes the old data from the statistics. All data that is older than
262 * TID_MAX_TIME_DIFF, will be deleted.
264 static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
266 /* The oldest age we want to keep */
267 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
269 while (tl->queue_count &&
270 (tl->time_stamp < oldest_time)) {
271 tl->total -= tl->packet_count[tl->head];
272 tl->packet_count[tl->head] = 0;
273 tl->time_stamp += TID_QUEUE_CELL_SPACING;
276 if (tl->head >= TID_QUEUE_MAX_SIZE)
282 * increment traffic load value for tid and also remove
283 * any old values if passed the certain time period
285 static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data,
286 struct ieee80211_hdr *hdr)
288 u32 curr_time = jiffies_to_msecs(jiffies);
291 struct iwl4965_traffic_load *tl = NULL;
292 u16 fc = le16_to_cpu(hdr->frame_control);
295 if (ieee80211_is_qos_data(fc)) {
296 u8 *qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
301 tl = &lq_data->load[tid];
303 curr_time -= curr_time % TID_ROUND_VALUE;
305 /* Happens only for the first packet. Initialize the data */
306 if (!(tl->queue_count)) {
308 tl->time_stamp = curr_time;
311 tl->packet_count[0] = 1;
315 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
316 index = time_diff / TID_QUEUE_CELL_SPACING;
318 /* The history is too long: remove data that is older than */
319 /* TID_MAX_TIME_DIFF */
320 if (index >= TID_QUEUE_MAX_SIZE)
321 rs_tl_rm_old_stats(tl, curr_time);
323 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
324 tl->packet_count[index] = tl->packet_count[index] + 1;
325 tl->total = tl->total + 1;
327 if ((index + 1) > tl->queue_count)
328 tl->queue_count = index + 1;
332 get the traffic load value for tid
334 static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
336 u32 curr_time = jiffies_to_msecs(jiffies);
339 struct iwl4965_traffic_load *tl = NULL;
341 if (tid >= TID_MAX_LOAD_COUNT)
344 tl = &(lq_data->load[tid]);
346 curr_time -= curr_time % TID_ROUND_VALUE;
348 if (!(tl->queue_count))
351 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
352 index = time_diff / TID_QUEUE_CELL_SPACING;
354 /* The history is too long: remove data that is older than */
355 /* TID_MAX_TIME_DIFF */
356 if (index >= TID_QUEUE_MAX_SIZE)
357 rs_tl_rm_old_stats(tl, curr_time);
362 static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
363 struct iwl4965_lq_sta *lq_data, u8 tid,
364 struct sta_info *sta)
367 DECLARE_MAC_BUF(mac);
369 spin_lock_bh(&sta->lock);
370 state = sta->ampdu_mlme.tid_state_tx[tid];
371 spin_unlock_bh(&sta->lock);
373 if (state == HT_AGG_STATE_IDLE &&
374 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
375 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
376 print_mac(mac, sta->addr), tid);
377 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
381 static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
382 struct iwl4965_lq_sta *lq_data,
383 struct sta_info *sta)
385 if ((tid < TID_MAX_LOAD_COUNT))
386 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
387 else if (tid == IWL_AGG_ALL_TID)
388 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
389 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
392 #endif /* CONFIG_IWLWIFI_HT */
394 static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
396 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
397 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
398 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
402 * rs_collect_tx_data - Update the success/failure sliding window
404 * We keep a sliding window of the last 62 packets transmitted
405 * at this rate. window->data contains the bitmask of successful
408 static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
409 int scale_index, s32 tpt, int retries,
412 struct iwl4965_rate_scale_data *window = NULL;
413 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
416 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
419 /* Select data for current tx bit rate */
420 window = &(windows[scale_index]);
423 * Keep track of only the latest 62 tx frame attempts in this rate's
424 * history window; anything older isn't really relevant any more.
425 * If we have filled up the sliding window, drop the oldest attempt;
426 * if the oldest attempt (highest bit in bitmap) shows "success",
427 * subtract "1" from the success counter (this is the main reason
428 * we keep these bitmaps!).
430 while (retries > 0) {
431 if (window->counter >= IWL_RATE_MAX_WINDOW) {
433 /* remove earliest */
434 window->counter = IWL_RATE_MAX_WINDOW - 1;
436 if (window->data & mask) {
437 window->data &= ~mask;
438 window->success_counter--;
442 /* Increment frames-attempted counter */
445 /* Shift bitmap by one frame (throw away oldest history),
446 * OR in "1", and increment "success" if this
447 * frame was successful. */
450 window->success_counter++;
458 /* Calculate current success ratio, avoid divide-by-0! */
459 if (window->counter > 0)
460 window->success_ratio = 128 * (100 * window->success_counter)
463 window->success_ratio = IWL_INVALID_VALUE;
465 fail_count = window->counter - window->success_counter;
467 /* Calculate average throughput, if we have enough history. */
468 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
469 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
470 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
472 window->average_tpt = IWL_INVALID_VALUE;
474 /* Tag this window as having been updated */
475 window->stamp = jiffies;
481 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
483 /* FIXME:RS:remove this function and put the flags statically in the table */
484 static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
485 int index, u8 use_green)
487 u32 rate_n_flags = 0;
489 if (is_legacy(tbl->lq_type)) {
490 rate_n_flags = iwl_rates[index].plcp;
491 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
492 rate_n_flags |= RATE_MCS_CCK_MSK;
494 } else if (is_Ht(tbl->lq_type)) {
495 if (index > IWL_LAST_OFDM_RATE) {
496 IWL_ERROR("invalid HT rate index %d\n", index);
497 index = IWL_LAST_OFDM_RATE;
499 rate_n_flags = RATE_MCS_HT_MSK;
501 if (is_siso(tbl->lq_type))
502 rate_n_flags |= iwl_rates[index].plcp_siso;
503 else if (is_mimo2(tbl->lq_type))
504 rate_n_flags |= iwl_rates[index].plcp_mimo2;
506 rate_n_flags |= iwl_rates[index].plcp_mimo3;
508 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
511 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
512 RATE_MCS_ANT_ABC_MSK);
514 if (is_Ht(tbl->lq_type)) {
517 rate_n_flags |= RATE_MCS_DUP_MSK;
519 rate_n_flags |= RATE_MCS_FAT_MSK;
522 rate_n_flags |= RATE_MCS_SGI_MSK;
525 rate_n_flags |= RATE_MCS_GF_MSK;
526 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
527 rate_n_flags &= ~RATE_MCS_SGI_MSK;
528 IWL_ERROR("GF was set with SGI:SISO\n");
536 * Interpret uCode API's rate_n_flags format,
537 * fill "search" or "active" tx mode table.
539 static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
540 enum ieee80211_band band,
541 struct iwl4965_scale_tbl_info *tbl,
544 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
545 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
548 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
550 if (*rate_idx == IWL_RATE_INVALID) {
554 tbl->is_SGI = 0; /* default legacy setup */
557 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
558 tbl->lq_type = LQ_NONE;
560 /* legacy rate format */
561 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
562 if (num_of_ant == 1) {
563 if (band == IEEE80211_BAND_5GHZ)
570 if (rate_n_flags & RATE_MCS_SGI_MSK)
573 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
574 (rate_n_flags & RATE_MCS_DUP_MSK))
577 if (rate_n_flags & RATE_MCS_DUP_MSK)
580 mcs = rs_extract_rate(rate_n_flags);
583 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
585 tbl->lq_type = LQ_SISO; /*else NONE*/
587 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
589 tbl->lq_type = LQ_MIMO2;
593 tbl->lq_type = LQ_MIMO3;
599 /* switch to another antenna/antennas and return 1 */
600 /* if no other valid antenna found, return 0 */
601 static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
602 struct iwl4965_scale_tbl_info *tbl)
606 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
609 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
612 new_ant_type = ant_toggle_lookup[tbl->ant_type];
614 while ((new_ant_type != tbl->ant_type) &&
615 !rs_is_valid_ant(valid_ant, new_ant_type))
616 new_ant_type = ant_toggle_lookup[new_ant_type];
618 if (new_ant_type == tbl->ant_type)
621 tbl->ant_type = new_ant_type;
622 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
623 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
627 /* FIXME:RS: in 4965 we don't use greenfield at all */
628 /* FIXME:RS: don't use greenfield for now in TX */
629 /* #ifdef CONFIG_IWL4965_HT */
631 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
633 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
634 priv->current_ht_config.is_green_field &&
635 !priv->current_ht_config.non_GF_STA_present);
638 static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
642 #endif /* CONFIG_IWL4965_HT */
645 * rs_get_supported_rates - get the available rates
647 * if management frame or broadcast frame only return
648 * basic available rates.
651 static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
652 struct ieee80211_hdr *hdr,
653 enum iwl_table_type rate_type)
655 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
656 lq_sta->active_rate_basic)
657 return lq_sta->active_rate_basic;
659 if (is_legacy(rate_type)) {
660 return lq_sta->active_legacy_rate;
662 if (is_siso(rate_type))
663 return lq_sta->active_siso_rate;
664 else if (is_mimo2(rate_type))
665 return lq_sta->active_mimo2_rate;
667 return lq_sta->active_mimo3_rate;
671 static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
674 u8 high = IWL_RATE_INVALID;
675 u8 low = IWL_RATE_INVALID;
677 /* 802.11A or ht walks to the next literal adjacent rate in
679 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
683 /* Find the previous rate that is in the rate mask */
685 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
686 if (rate_mask & mask) {
692 /* Find the next rate that is in the rate mask */
694 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
695 if (rate_mask & mask) {
701 return (high << 8) | low;
705 while (low != IWL_RATE_INVALID) {
706 low = iwl_rates[low].prev_rs;
707 if (low == IWL_RATE_INVALID)
709 if (rate_mask & (1 << low))
711 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
715 while (high != IWL_RATE_INVALID) {
716 high = iwl_rates[high].next_rs;
717 if (high == IWL_RATE_INVALID)
719 if (rate_mask & (1 << high))
721 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
724 return (high << 8) | low;
727 static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
728 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
734 u8 switch_to_legacy = 0;
735 u8 is_green = lq_sta->is_green;
737 /* check if we need to switch from HT to legacy rates.
738 * assumption is that mandatory rates (1Mbps or 6Mbps)
739 * are always supported (spec demand) */
740 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
741 switch_to_legacy = 1;
742 scale_index = rs_ht_to_legacy[scale_index];
743 if (lq_sta->band == IEEE80211_BAND_5GHZ)
748 if (num_of_ant(tbl->ant_type) > 1)
749 tbl->ant_type = ANT_A;/*FIXME:RS*/
755 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
757 /* Mask with station rate restriction */
758 if (is_legacy(tbl->lq_type)) {
759 /* supp_rates has no CCK bits in A mode */
760 if (lq_sta->band == IEEE80211_BAND_5GHZ)
761 rate_mask = (u16)(rate_mask &
762 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
764 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
767 /* If we switched from HT to legacy, check current rate */
768 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
773 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
775 low = high_low & 0xff;
777 if (low == IWL_RATE_INVALID)
781 return rate_n_flags_from_tbl(tbl, low, is_green);
785 * mac80211 sends us Tx status
787 static void rs_tx_status(void *priv_rate, struct net_device *dev,
792 int rs_index, index = 0;
793 struct iwl4965_lq_sta *lq_sta;
794 struct iwl_link_quality_cmd *table;
795 struct sta_info *sta;
796 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
797 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
798 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
799 struct ieee80211_hw *hw = local_to_hw(local);
800 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
801 struct iwl4965_rate_scale_data *window = NULL;
802 struct iwl4965_rate_scale_data *search_win = NULL;
804 struct iwl4965_scale_tbl_info tbl_type;
805 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
807 u16 fc = le16_to_cpu(hdr->frame_control);
810 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
812 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
815 /* This packet was aggregated but doesn't carry rate scale info */
816 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
817 !(info->flags & IEEE80211_TX_STAT_AMPDU))
820 retries = info->status.retry_count;
827 sta = sta_info_get(local, hdr->addr1);
829 if (!sta || !sta->rate_ctrl_priv)
833 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
835 if (!priv->lq_mngr.lq_ready)
838 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
839 !lq_sta->ibss_sta_added)
843 active_index = lq_sta->active_tbl;
845 curr_tbl = &(lq_sta->lq_info[active_index]);
846 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
847 window = (struct iwl4965_rate_scale_data *)
849 search_win = (struct iwl4965_rate_scale_data *)
850 &(search_tbl->win[0]);
853 * Ignore this Tx frame response if its initial rate doesn't match
854 * that of latest Link Quality command. There may be stragglers
855 * from a previous Link Quality command, but we're no longer interested
856 * in those; they're either from the "active" mode while we're trying
857 * to check "search" mode, or a prior "search" mode after we've moved
858 * to a new "search" mode (which might become the new "active" mode).
860 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
861 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
862 if (priv->band == IEEE80211_BAND_5GHZ)
863 rs_index -= IWL_FIRST_OFDM_RATE;
865 if ((info->tx_rate_idx < 0) ||
867 !!(info->flags & IEEE80211_TX_CTL_SHORT_GI)) ||
869 !!(info->flags & IEEE80211_TX_CTL_40_MHZ_WIDTH)) ||
871 !!(info->flags & IEEE80211_TX_CTL_DUP_DATA)) ||
872 (tbl_type.ant_type ^ info->antenna_sel_tx) ||
873 (!!(tx_rate & RATE_MCS_HT_MSK) ^
874 !!(info->flags & IEEE80211_TX_CTL_OFDM_HT)) ||
875 (!!(tx_rate & RATE_MCS_GF_MSK) ^
876 !!(info->flags & IEEE80211_TX_CTL_GREEN_FIELD)) ||
877 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
878 hw->wiphy->bands[info->band]->bitrates[info->tx_rate_idx].bitrate)) {
879 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
883 /* Update frame history window with "failure" for each Tx retry. */
885 /* Look up the rate and other info used for each tx attempt.
886 * Each tx attempt steps one entry deeper in the rate table. */
887 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
888 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
889 &tbl_type, &rs_index);
891 /* If type matches "search" table,
892 * add failure to "search" history */
893 if ((tbl_type.lq_type == search_tbl->lq_type) &&
894 (tbl_type.ant_type == search_tbl->ant_type) &&
895 (tbl_type.is_SGI == search_tbl->is_SGI)) {
896 if (search_tbl->expected_tpt)
897 tpt = search_tbl->expected_tpt[rs_index];
900 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
902 /* Else if type matches "current/active" table,
903 * add failure to "current/active" history */
904 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
905 (tbl_type.ant_type == curr_tbl->ant_type) &&
906 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
907 if (curr_tbl->expected_tpt)
908 tpt = curr_tbl->expected_tpt[rs_index];
911 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
914 /* If not searching for a new mode, increment failed counter
915 * ... this helps determine when to start searching again */
916 if (lq_sta->stay_in_tbl)
917 lq_sta->total_failed++;
924 * Find (by rate) the history window to update with final Tx attempt;
925 * if Tx was successful first try, use original rate,
926 * else look up the rate that was, finally, successful.
928 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
929 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
931 /* Update frame history window with "success" if Tx got ACKed ... */
932 status = !!(info->flags & IEEE80211_TX_STAT_ACK);
934 /* If type matches "search" table,
935 * add final tx status to "search" history */
936 if ((tbl_type.lq_type == search_tbl->lq_type) &&
937 (tbl_type.ant_type == search_tbl->ant_type) &&
938 (tbl_type.is_SGI == search_tbl->is_SGI)) {
939 if (search_tbl->expected_tpt)
940 tpt = search_tbl->expected_tpt[rs_index];
943 if (info->flags & IEEE80211_TX_CTL_AMPDU)
944 rs_collect_tx_data(search_win, rs_index, tpt,
945 info->status.ampdu_ack_len,
946 info->status.ampdu_ack_map);
948 rs_collect_tx_data(search_win, rs_index, tpt,
950 /* Else if type matches "current/active" table,
951 * add final tx status to "current/active" history */
952 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
953 (tbl_type.ant_type == curr_tbl->ant_type) &&
954 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
955 if (curr_tbl->expected_tpt)
956 tpt = curr_tbl->expected_tpt[rs_index];
959 if (info->flags & IEEE80211_TX_CTL_AMPDU)
960 rs_collect_tx_data(window, rs_index, tpt,
961 info->status.ampdu_ack_len,
962 info->status.ampdu_ack_map);
964 rs_collect_tx_data(window, rs_index, tpt,
968 /* If not searching for new mode, increment success/failed counter
969 * ... these help determine when to start searching again */
970 if (lq_sta->stay_in_tbl) {
971 if (info->flags & IEEE80211_TX_CTL_AMPDU) {
972 lq_sta->total_success += info->status.ampdu_ack_map;
973 lq_sta->total_failed +=
974 (info->status.ampdu_ack_len - info->status.ampdu_ack_map);
977 lq_sta->total_success++;
979 lq_sta->total_failed++;
983 /* See if there's a better rate or modulation mode to try. */
984 rs_rate_scale_perform(priv, dev, hdr, sta);
991 * Begin a period of staying with a selected modulation mode.
992 * Set "stay_in_tbl" flag to prevent any mode switches.
993 * Set frame tx success limits according to legacy vs. high-throughput,
994 * and reset overall (spanning all rates) tx success history statistics.
995 * These control how long we stay using same modulation mode before
996 * searching for a new mode.
998 static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
999 struct iwl4965_lq_sta *lq_sta)
1001 IWL_DEBUG_RATE("we are staying in the same table\n");
1002 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1004 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1005 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1006 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1008 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1009 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1010 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1012 lq_sta->table_count = 0;
1013 lq_sta->total_failed = 0;
1014 lq_sta->total_success = 0;
1018 * Find correct throughput table for given mode of modulation
1020 static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
1021 struct iwl4965_scale_tbl_info *tbl)
1023 if (is_legacy(tbl->lq_type)) {
1024 if (!is_a_band(tbl->lq_type))
1025 tbl->expected_tpt = expected_tpt_G;
1027 tbl->expected_tpt = expected_tpt_A;
1028 } else if (is_siso(tbl->lq_type)) {
1029 if (tbl->is_fat && !lq_sta->is_dup)
1031 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1033 tbl->expected_tpt = expected_tpt_siso40MHz;
1034 else if (tbl->is_SGI)
1035 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1037 tbl->expected_tpt = expected_tpt_siso20MHz;
1039 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
1040 if (tbl->is_fat && !lq_sta->is_dup)
1042 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1044 tbl->expected_tpt = expected_tpt_mimo40MHz;
1045 else if (tbl->is_SGI)
1046 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1048 tbl->expected_tpt = expected_tpt_mimo20MHz;
1050 tbl->expected_tpt = expected_tpt_G;
1053 #ifdef CONFIG_IWL4965_HT
1055 * Find starting rate for new "search" high-throughput mode of modulation.
1056 * Goal is to find lowest expected rate (under perfect conditions) that is
1057 * above the current measured throughput of "active" mode, to give new mode
1058 * a fair chance to prove itself without too many challenges.
1060 * This gets called when transitioning to more aggressive modulation
1061 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1062 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1063 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1064 * bit rate will typically need to increase, but not if performance was bad.
1066 static s32 rs_get_best_rate(struct iwl_priv *priv,
1067 struct iwl4965_lq_sta *lq_sta,
1068 struct iwl4965_scale_tbl_info *tbl, /* "search" */
1069 u16 rate_mask, s8 index)
1071 /* "active" values */
1072 struct iwl4965_scale_tbl_info *active_tbl =
1073 &(lq_sta->lq_info[lq_sta->active_tbl]);
1074 s32 active_sr = active_tbl->win[index].success_ratio;
1075 s32 active_tpt = active_tbl->expected_tpt[index];
1077 /* expected "search" throughput */
1078 s32 *tpt_tbl = tbl->expected_tpt;
1080 s32 new_rate, high, low, start_hi;
1084 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1087 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1090 low = high_low & 0xff;
1091 high = (high_low >> 8) & 0xff;
1094 * Lower the "search" bit rate, to give new "search" mode
1095 * approximately the same throughput as "active" if:
1097 * 1) "Active" mode has been working modestly well (but not
1098 * great), and expected "search" throughput (under perfect
1099 * conditions) at candidate rate is above the actual
1100 * measured "active" throughput (but less than expected
1101 * "active" throughput under perfect conditions).
1103 * 2) "Active" mode has been working perfectly or very well
1104 * and expected "search" throughput (under perfect
1105 * conditions) at candidate rate is above expected
1106 * "active" throughput (under perfect conditions).
1108 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1109 ((active_sr > IWL_RATE_DECREASE_TH) &&
1110 (active_sr <= IWL_RATE_HIGH_TH) &&
1111 (tpt_tbl[rate] <= active_tpt))) ||
1112 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1113 (tpt_tbl[rate] > active_tpt))) {
1115 /* (2nd or later pass)
1116 * If we've already tried to raise the rate, and are
1117 * now trying to lower it, use the higher rate. */
1118 if (start_hi != IWL_RATE_INVALID) {
1119 new_rate = start_hi;
1125 /* Loop again with lower rate */
1126 if (low != IWL_RATE_INVALID)
1129 /* Lower rate not available, use the original */
1133 /* Else try to raise the "search" rate to match "active" */
1135 /* (2nd or later pass)
1136 * If we've already tried to lower the rate, and are
1137 * now trying to raise it, use the lower rate. */
1138 if (new_rate != IWL_RATE_INVALID)
1141 /* Loop again with higher rate */
1142 else if (high != IWL_RATE_INVALID) {
1146 /* Higher rate not available, use the original */
1156 #endif /* CONFIG_IWL4965_HT */
1159 * Set up search table for MIMO
1161 #ifdef CONFIG_IWL4965_HT
1162 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1163 struct iwl4965_lq_sta *lq_sta,
1164 struct ieee80211_conf *conf,
1165 struct sta_info *sta,
1166 struct iwl4965_scale_tbl_info *tbl, int index)
1170 s8 is_green = lq_sta->is_green;
1172 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1173 !sta->ht_info.ht_supported)
1176 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
1179 /* Need both Tx chains/antennas to support MIMO */
1180 if (priv->hw_params.tx_chains_num < 2)
1183 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
1185 tbl->lq_type = LQ_MIMO2;
1186 tbl->is_dup = lq_sta->is_dup;
1188 rate_mask = lq_sta->active_mimo2_rate;
1190 if (priv->current_ht_config.supported_chan_width
1191 == IWL_CHANNEL_WIDTH_40MHZ)
1196 /* FIXME: - don't toggle SGI here
1198 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1202 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1208 rs_set_expected_tpt_table(lq_sta, tbl);
1210 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1212 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1214 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1215 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
1219 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
1221 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
1222 tbl->current_rate, is_green);
1226 static int rs_switch_to_mimo2(struct iwl_priv *priv,
1227 struct iwl4965_lq_sta *lq_sta,
1228 struct ieee80211_conf *conf,
1229 struct sta_info *sta,
1230 struct iwl4965_scale_tbl_info *tbl, int index)
1234 #endif /*CONFIG_IWL4965_HT */
1237 * Set up search table for SISO
1239 static int rs_switch_to_siso(struct iwl_priv *priv,
1240 struct iwl4965_lq_sta *lq_sta,
1241 struct ieee80211_conf *conf,
1242 struct sta_info *sta,
1243 struct iwl4965_scale_tbl_info *tbl, int index)
1245 #ifdef CONFIG_IWL4965_HT
1247 u8 is_green = lq_sta->is_green;
1250 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1251 !sta->ht_info.ht_supported)
1254 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
1256 tbl->is_dup = lq_sta->is_dup;
1257 tbl->lq_type = LQ_SISO;
1259 rate_mask = lq_sta->active_siso_rate;
1261 if (priv->current_ht_config.supported_chan_width
1262 == IWL_CHANNEL_WIDTH_40MHZ)
1267 /* FIXME: - don't toggle SGI here
1269 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
1273 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
1280 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1282 rs_set_expected_tpt_table(lq_sta, tbl);
1283 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1285 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
1286 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1287 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
1291 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
1292 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
1293 tbl->current_rate, is_green);
1297 #endif /*CONFIG_IWL4965_HT */
1301 * Try to switch to new modulation mode from legacy
1303 static int rs_move_legacy_other(struct iwl_priv *priv,
1304 struct iwl4965_lq_sta *lq_sta,
1305 struct ieee80211_conf *conf,
1306 struct sta_info *sta,
1309 struct iwl4965_scale_tbl_info *tbl =
1310 &(lq_sta->lq_info[lq_sta->active_tbl]);
1311 struct iwl4965_scale_tbl_info *search_tbl =
1312 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1313 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1314 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1315 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1316 u8 start_action = tbl->action;
1317 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1321 switch (tbl->action) {
1322 case IWL_LEGACY_SWITCH_ANTENNA:
1323 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
1325 lq_sta->action_counter++;
1327 /* Don't change antenna if success has been great */
1328 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1331 /* Set up search table to try other antenna */
1332 memcpy(search_tbl, tbl, sz);
1334 if (rs_toggle_antenna(valid_tx_ant,
1335 &search_tbl->current_rate, search_tbl)) {
1336 lq_sta->search_better_tbl = 1;
1340 case IWL_LEGACY_SWITCH_SISO:
1341 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
1343 /* Set up search table to try SISO */
1344 memcpy(search_tbl, tbl, sz);
1345 search_tbl->is_SGI = 0;
1346 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1349 lq_sta->search_better_tbl = 1;
1350 lq_sta->action_counter = 0;
1355 case IWL_LEGACY_SWITCH_MIMO2:
1356 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
1358 /* Set up search table to try MIMO */
1359 memcpy(search_tbl, tbl, sz);
1360 search_tbl->is_SGI = 0;
1361 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1362 /*FIXME:RS:need to check ant validity*/
1363 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1366 lq_sta->search_better_tbl = 1;
1367 lq_sta->action_counter = 0;
1373 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1374 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1376 if (tbl->action == start_action)
1384 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
1385 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1391 * Try to switch to new modulation mode from SISO
1393 static int rs_move_siso_to_other(struct iwl_priv *priv,
1394 struct iwl4965_lq_sta *lq_sta,
1395 struct ieee80211_conf *conf,
1396 struct sta_info *sta,
1399 u8 is_green = lq_sta->is_green;
1400 struct iwl4965_scale_tbl_info *tbl =
1401 &(lq_sta->lq_info[lq_sta->active_tbl]);
1402 struct iwl4965_scale_tbl_info *search_tbl =
1403 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1404 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1405 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1406 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1407 u8 start_action = tbl->action;
1408 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1412 lq_sta->action_counter++;
1413 switch (tbl->action) {
1414 case IWL_SISO_SWITCH_ANTENNA:
1415 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
1416 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1419 memcpy(search_tbl, tbl, sz);
1420 if (rs_toggle_antenna(valid_tx_ant,
1421 &search_tbl->current_rate, search_tbl)) {
1422 lq_sta->search_better_tbl = 1;
1426 case IWL_SISO_SWITCH_MIMO2:
1427 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
1428 memcpy(search_tbl, tbl, sz);
1429 search_tbl->is_SGI = 0;
1430 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1431 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
1434 lq_sta->search_better_tbl = 1;
1438 case IWL_SISO_SWITCH_GI:
1440 !(priv->current_ht_config.sgf &
1444 !(priv->current_ht_config.sgf &
1448 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
1450 memcpy(search_tbl, tbl, sz);
1455 IWL_ERROR("SGI was set in GF+SISO\n");
1457 search_tbl->is_SGI = !tbl->is_SGI;
1458 rs_set_expected_tpt_table(lq_sta, search_tbl);
1460 s32 tpt = lq_sta->last_tpt / 100;
1461 if (tpt >= search_tbl->expected_tpt[index])
1464 search_tbl->current_rate = rate_n_flags_from_tbl(
1465 search_tbl, index, is_green);
1466 lq_sta->search_better_tbl = 1;
1470 if (tbl->action > IWL_SISO_SWITCH_GI)
1471 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1473 if (tbl->action == start_action)
1480 if (tbl->action > IWL_SISO_SWITCH_GI)
1481 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1486 * Try to switch to new modulation mode from MIMO
1488 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1489 struct iwl4965_lq_sta *lq_sta,
1490 struct ieee80211_conf *conf,
1491 struct sta_info *sta,
1494 s8 is_green = lq_sta->is_green;
1495 struct iwl4965_scale_tbl_info *tbl =
1496 &(lq_sta->lq_info[lq_sta->active_tbl]);
1497 struct iwl4965_scale_tbl_info *search_tbl =
1498 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1499 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1500 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
1501 u8 start_action = tbl->action;
1502 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1506 lq_sta->action_counter++;
1507 switch (tbl->action) {
1508 case IWL_MIMO_SWITCH_ANTENNA_A:
1509 case IWL_MIMO_SWITCH_ANTENNA_B:
1510 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
1512 /* Set up new search table for SISO */
1513 memcpy(search_tbl, tbl, sz);
1515 /*FIXME:RS:need to check ant validity + C*/
1516 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1517 search_tbl->ant_type = ANT_A;
1519 search_tbl->ant_type = ANT_B;
1521 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
1524 lq_sta->search_better_tbl = 1;
1529 case IWL_MIMO_SWITCH_GI:
1531 !(priv->current_ht_config.sgf &
1535 !(priv->current_ht_config.sgf &
1539 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
1541 /* Set up new search table for MIMO */
1542 memcpy(search_tbl, tbl, sz);
1543 search_tbl->is_SGI = !tbl->is_SGI;
1544 rs_set_expected_tpt_table(lq_sta, search_tbl);
1546 * If active table already uses the fastest possible
1547 * modulation (dual stream with short guard interval),
1548 * and it's working well, there's no need to look
1549 * for a better type of modulation!
1552 s32 tpt = lq_sta->last_tpt / 100;
1553 if (tpt >= search_tbl->expected_tpt[index])
1556 search_tbl->current_rate = rate_n_flags_from_tbl(
1557 search_tbl, index, is_green);
1558 lq_sta->search_better_tbl = 1;
1563 if (tbl->action > IWL_MIMO_SWITCH_GI)
1564 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1566 if (tbl->action == start_action)
1573 if (tbl->action > IWL_MIMO_SWITCH_GI)
1574 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1580 * Check whether we should continue using same modulation mode, or
1581 * begin search for a new mode, based on:
1582 * 1) # tx successes or failures while using this mode
1583 * 2) # times calling this function
1584 * 3) elapsed time in this mode (not used, for now)
1586 static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
1588 struct iwl4965_scale_tbl_info *tbl;
1591 int flush_interval_passed = 0;
1592 struct iwl_priv *priv;
1595 active_tbl = lq_sta->active_tbl;
1597 tbl = &(lq_sta->lq_info[active_tbl]);
1599 /* If we've been disallowing search, see if we should now allow it */
1600 if (lq_sta->stay_in_tbl) {
1602 /* Elapsed time using current modulation mode */
1603 if (lq_sta->flush_timer)
1604 flush_interval_passed =
1606 (unsigned long)(lq_sta->flush_timer +
1607 IWL_RATE_SCALE_FLUSH_INTVL));
1610 * Check if we should allow search for new modulation mode.
1611 * If many frames have failed or succeeded, or we've used
1612 * this same modulation for a long time, allow search, and
1613 * reset history stats that keep track of whether we should
1614 * allow a new search. Also (below) reset all bitmaps and
1615 * stats in active history.
1617 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1618 (lq_sta->total_success > lq_sta->max_success_limit) ||
1619 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1620 && (flush_interval_passed))) {
1621 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
1622 lq_sta->total_failed,
1623 lq_sta->total_success,
1624 flush_interval_passed);
1626 /* Allow search for new mode */
1627 lq_sta->stay_in_tbl = 0; /* only place reset */
1628 lq_sta->total_failed = 0;
1629 lq_sta->total_success = 0;
1630 lq_sta->flush_timer = 0;
1633 * Else if we've used this modulation mode enough repetitions
1634 * (regardless of elapsed time or success/failure), reset
1635 * history bitmaps and rate-specific stats for all rates in
1639 lq_sta->table_count++;
1640 if (lq_sta->table_count >=
1641 lq_sta->table_count_limit) {
1642 lq_sta->table_count = 0;
1644 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
1645 for (i = 0; i < IWL_RATE_COUNT; i++)
1646 rs_rate_scale_clear_window(
1651 /* If transitioning to allow "search", reset all history
1652 * bitmaps and stats in active table (this will become the new
1653 * "search" table). */
1654 if (!lq_sta->stay_in_tbl) {
1655 for (i = 0; i < IWL_RATE_COUNT; i++)
1656 rs_rate_scale_clear_window(&(tbl->win[i]));
1662 * Do rate scaling and search for new modulation mode.
1664 static void rs_rate_scale_perform(struct iwl_priv *priv,
1665 struct net_device *dev,
1666 struct ieee80211_hdr *hdr,
1667 struct sta_info *sta)
1669 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1670 struct ieee80211_hw *hw = local_to_hw(local);
1671 struct ieee80211_conf *conf = &hw->conf;
1672 int low = IWL_RATE_INVALID;
1673 int high = IWL_RATE_INVALID;
1676 struct iwl4965_rate_scale_data *window = NULL;
1677 int current_tpt = IWL_INVALID_VALUE;
1678 int low_tpt = IWL_INVALID_VALUE;
1679 int high_tpt = IWL_INVALID_VALUE;
1681 s8 scale_action = 0;
1684 struct iwl4965_lq_sta *lq_sta;
1685 struct iwl4965_scale_tbl_info *tbl, *tbl1;
1686 u16 rate_scale_index_msk = 0;
1692 #ifdef CONFIG_IWL4965_HT
1693 u8 tid = MAX_TID_COUNT;
1696 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1698 fc = le16_to_cpu(hdr->frame_control);
1699 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1700 /* Send management frames and broadcast/multicast data using
1702 /* TODO: this could probably be improved.. */
1706 if (!sta || !sta->rate_ctrl_priv)
1709 if (!priv->lq_mngr.lq_ready) {
1710 IWL_DEBUG_RATE("still rate scaling not ready\n");
1713 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
1715 #ifdef CONFIG_IWL4965_HT
1716 rs_tl_add_packet(lq_sta, hdr);
1719 * Select rate-scale / modulation-mode table to work with in
1720 * the rest of this function: "search" if searching for better
1721 * modulation mode, or "active" if doing rate scaling within a mode.
1723 if (!lq_sta->search_better_tbl)
1724 active_tbl = lq_sta->active_tbl;
1726 active_tbl = 1 - lq_sta->active_tbl;
1728 tbl = &(lq_sta->lq_info[active_tbl]);
1729 is_green = lq_sta->is_green;
1731 /* current tx rate */
1732 index = sta->last_txrate_idx;
1734 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1737 /* rates available for this association, and for modulation mode */
1738 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1740 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1742 /* mask with station rate restriction */
1743 if (is_legacy(tbl->lq_type)) {
1744 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1745 /* supp_rates has no CCK bits in A mode */
1746 rate_scale_index_msk = (u16) (rate_mask &
1747 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1749 rate_scale_index_msk = (u16) (rate_mask &
1750 lq_sta->supp_rates);
1753 rate_scale_index_msk = rate_mask;
1755 if (!rate_scale_index_msk)
1756 rate_scale_index_msk = rate_mask;
1758 if (!((1 << index) & rate_scale_index_msk)) {
1759 IWL_ERROR("Current Rate is not valid\n");
1763 /* Get expected throughput table and history window for current rate */
1764 if (!tbl->expected_tpt) {
1765 IWL_ERROR("tbl->expected_tpt is NULL\n");
1769 window = &(tbl->win[index]);
1772 * If there is not enough history to calculate actual average
1773 * throughput, keep analyzing results of more tx frames, without
1774 * changing rate or mode (bypass most of the rest of this function).
1775 * Set up new rate table in uCode only if old rate is not supported
1776 * in current association (use new rate found above).
1778 fail_count = window->counter - window->success_counter;
1779 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1780 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1781 IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
1783 window->success_counter, window->counter, index);
1785 /* Can't calculate this yet; not enough history */
1786 window->average_tpt = IWL_INVALID_VALUE;
1788 /* Should we stay with this modulation mode,
1789 * or search for a new one? */
1790 rs_stay_in_table(lq_sta);
1794 /* Else we have enough samples; calculate estimate of
1795 * actual average throughput */
1797 /*FIXME:RS remove this else if we don't get this error*/
1798 if (window->average_tpt != ((window->success_ratio *
1799 tbl->expected_tpt[index] + 64) / 128)) {
1800 IWL_ERROR("expected_tpt should have been calculated"
1802 window->average_tpt = ((window->success_ratio *
1803 tbl->expected_tpt[index] + 64) / 128);
1807 /* If we are searching for better modulation mode, check success. */
1808 if (lq_sta->search_better_tbl) {
1810 /* If good success, continue using the "search" mode;
1811 * no need to send new link quality command, since we're
1812 * continuing to use the setup that we've been trying. */
1813 if (window->average_tpt > lq_sta->last_tpt) {
1815 IWL_DEBUG_RATE("LQ: SWITCHING TO CURRENT TABLE "
1816 "suc=%d cur-tpt=%d old-tpt=%d\n",
1817 window->success_ratio,
1818 window->average_tpt,
1821 if (!is_legacy(tbl->lq_type))
1822 lq_sta->enable_counter = 1;
1824 /* Swap tables; "search" becomes "active" */
1825 lq_sta->active_tbl = active_tbl;
1826 current_tpt = window->average_tpt;
1828 /* Else poor success; go back to mode in "active" table */
1831 IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
1832 "suc=%d cur-tpt=%d old-tpt=%d\n",
1833 window->success_ratio,
1834 window->average_tpt,
1837 /* Nullify "search" table */
1838 tbl->lq_type = LQ_NONE;
1840 /* Revert to "active" table */
1841 active_tbl = lq_sta->active_tbl;
1842 tbl = &(lq_sta->lq_info[active_tbl]);
1844 /* Revert to "active" rate and throughput info */
1845 index = iwl4965_hwrate_to_plcp_idx(
1847 current_tpt = lq_sta->last_tpt;
1849 /* Need to set up a new rate table in uCode */
1853 /* Either way, we've made a decision; modulation mode
1854 * search is done, allow rate adjustment next time. */
1855 lq_sta->search_better_tbl = 0;
1856 done_search = 1; /* Don't switch modes below! */
1860 /* (Else) not in search of better modulation mode, try for better
1861 * starting rate, while staying in this mode. */
1862 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
1864 low = high_low & 0xff;
1865 high = (high_low >> 8) & 0xff;
1867 /* Collect measured throughputs for current and adjacent rates */
1868 current_tpt = window->average_tpt;
1869 if (low != IWL_RATE_INVALID)
1870 low_tpt = tbl->win[low].average_tpt;
1871 if (high != IWL_RATE_INVALID)
1872 high_tpt = tbl->win[high].average_tpt;
1874 /* Assume rate increase */
1877 /* Too many failures, decrease rate */
1878 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1879 (current_tpt == 0)) {
1880 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1883 /* No throughput measured yet for adjacent rates; try increase. */
1884 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1885 (high_tpt == IWL_INVALID_VALUE))
1888 /* Both adjacent throughputs are measured, but neither one has better
1889 * throughput; we're using the best rate, don't change it! */
1890 else if ((low_tpt != IWL_INVALID_VALUE) &&
1891 (high_tpt != IWL_INVALID_VALUE) &&
1892 (low_tpt < current_tpt) &&
1893 (high_tpt < current_tpt))
1896 /* At least one adjacent rate's throughput is measured,
1897 * and may have better performance. */
1899 /* Higher adjacent rate's throughput is measured */
1900 if (high_tpt != IWL_INVALID_VALUE) {
1901 /* Higher rate has better throughput */
1902 if (high_tpt > current_tpt)
1906 ("decrease rate because of high tpt\n");
1910 /* Lower adjacent rate's throughput is measured */
1911 } else if (low_tpt != IWL_INVALID_VALUE) {
1912 /* Lower rate has better throughput */
1913 if (low_tpt > current_tpt) {
1915 ("decrease rate because of low tpt\n");
1922 /* Sanity check; asked for decrease, but success rate or throughput
1923 * has been good at old rate. Don't change it. */
1924 if (scale_action == -1) {
1925 if ((low != IWL_RATE_INVALID) &&
1926 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1927 (current_tpt > (100 * tbl->expected_tpt[low]))))
1930 /* Sanity check; asked for increase, but success rate has not been great
1931 * even at old rate, higher rate will be worse. Don't change it. */
1932 } else if ((scale_action == 1) &&
1933 (window->success_ratio < IWL_RATE_INCREASE_TH))
1936 switch (scale_action) {
1938 /* Decrease starting rate, update uCode's rate table */
1939 if (low != IWL_RATE_INVALID) {
1945 /* Increase starting rate, update uCode's rate table */
1946 if (high != IWL_RATE_INVALID) {
1958 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
1959 "high %d type %d\n",
1960 index, scale_action, low, high, tbl->lq_type);
1963 /* Replace uCode's rate table for the destination station. */
1965 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1966 rs_fill_link_cmd(priv, lq_sta, rate);
1967 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
1970 /* Should we stay with this modulation mode, or search for a new one? */
1971 rs_stay_in_table(lq_sta);
1974 * Search for new modulation mode if we're:
1975 * 1) Not changing rates right now
1976 * 2) Not just finishing up a search
1977 * 3) Allowing a new search
1979 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
1980 /* Save current throughput to compare with "search" throughput*/
1981 lq_sta->last_tpt = current_tpt;
1983 /* Select a new "search" modulation mode to try.
1984 * If one is found, set up the new "search" table. */
1985 if (is_legacy(tbl->lq_type))
1986 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
1987 else if (is_siso(tbl->lq_type))
1988 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
1990 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
1992 /* If new "search" mode was selected, set up in uCode table */
1993 if (lq_sta->search_better_tbl) {
1994 /* Access the "search" table, clear its history. */
1995 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1996 for (i = 0; i < IWL_RATE_COUNT; i++)
1997 rs_rate_scale_clear_window(&(tbl->win[i]));
1999 /* Use new "search" start rate */
2000 index = iwl4965_hwrate_to_plcp_idx(
2003 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
2004 tbl->current_rate, index);
2005 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
2006 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2009 /* If the "active" (non-search) mode was legacy,
2010 * and we've tried switching antennas,
2011 * but we haven't been able to try HT modes (not available),
2012 * stay with best antenna legacy modulation for a while
2013 * before next round of mode comparisons. */
2014 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2015 if (is_legacy(tbl1->lq_type) &&
2016 #ifdef CONFIG_IWL4965_HT
2017 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
2019 (lq_sta->action_counter >= 1)) {
2020 lq_sta->action_counter = 0;
2021 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
2022 rs_set_stay_in_table(priv, 1, lq_sta);
2025 /* If we're in an HT mode, and all 3 mode switch actions
2026 * have been tried and compared, stay in this best modulation
2027 * mode for a while before next round of mode comparisons. */
2028 if (lq_sta->enable_counter &&
2029 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
2030 #ifdef CONFIG_IWL4965_HT
2031 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2032 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2033 (tid != MAX_TID_COUNT)) {
2034 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
2035 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
2037 #endif /*CONFIG_IWL4965_HT */
2038 lq_sta->action_counter = 0;
2039 rs_set_stay_in_table(priv, 0, lq_sta);
2043 * Else, don't search for a new modulation mode.
2044 * Put new timestamp in stay-in-modulation-mode flush timer if:
2045 * 1) Not changing rates right now
2046 * 2) Not just finishing up a search
2047 * 3) flush timer is empty
2050 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2051 lq_sta->flush_timer = jiffies;
2055 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
2057 sta->last_txrate_idx = i;
2059 /* sta->txrate_idx is an index to A mode rates which start
2060 * at IWL_FIRST_OFDM_RATE
2062 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2063 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
2065 sta->txrate_idx = i;
2071 static void rs_initialize_lq(struct iwl_priv *priv,
2072 struct ieee80211_conf *conf,
2073 struct sta_info *sta)
2075 struct iwl4965_lq_sta *lq_sta;
2076 struct iwl4965_scale_tbl_info *tbl;
2080 u8 use_green = rs_use_green(priv, conf);
2084 if (!sta || !sta->rate_ctrl_priv)
2087 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2088 i = sta->last_txrate_idx;
2090 if ((lq_sta->lq.sta_id == 0xff) &&
2091 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2094 valid_tx_ant = priv->hw_params.valid_tx_ant;
2096 if (!lq_sta->search_better_tbl)
2097 active_tbl = lq_sta->active_tbl;
2099 active_tbl = 1 - lq_sta->active_tbl;
2101 tbl = &(lq_sta->lq_info[active_tbl]);
2103 if ((i < 0) || (i >= IWL_RATE_COUNT))
2106 /* FIXME:RS: This is also wrong in 4965 */
2107 rate = iwl_rates[i].plcp;
2108 rate |= RATE_MCS_ANT_B_MSK;
2109 rate &= ~RATE_MCS_ANT_A_MSK;
2111 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2112 rate |= RATE_MCS_CCK_MSK;
2114 tbl->ant_type = ANT_B;
2115 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2116 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2117 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2119 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2120 tbl->current_rate = rate;
2121 rs_set_expected_tpt_table(lq_sta, tbl);
2122 rs_fill_link_cmd(NULL, lq_sta, rate);
2123 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
2128 static void rs_get_rate(void *priv_rate, struct net_device *dev,
2129 struct ieee80211_supported_band *sband,
2130 struct sk_buff *skb,
2131 struct rate_selection *sel)
2135 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2136 struct ieee80211_conf *conf = &local->hw.conf;
2137 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2138 struct sta_info *sta;
2140 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
2141 struct iwl4965_lq_sta *lq_sta;
2143 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
2147 sta = sta_info_get(local, hdr->addr1);
2149 /* Send management frames and broadcast/multicast data using lowest
2151 fc = le16_to_cpu(hdr->frame_control);
2152 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2153 !sta || !sta->rate_ctrl_priv) {
2154 sel->rate_idx = rate_lowest_index(local, sband, sta);
2158 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
2159 i = sta->last_txrate_idx;
2161 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2162 !lq_sta->ibss_sta_added) {
2163 u8 sta_id = iwl_find_station(priv, hdr->addr1);
2164 DECLARE_MAC_BUF(mac);
2166 if (sta_id == IWL_INVALID_STATION) {
2167 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2168 print_mac(mac, hdr->addr1));
2169 sta_id = iwl_add_station_flags(priv, hdr->addr1,
2170 0, CMD_ASYNC, NULL);
2172 if ((sta_id != IWL_INVALID_STATION)) {
2173 lq_sta->lq.sta_id = sta_id;
2174 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2175 lq_sta->ibss_sta_added = 1;
2176 rs_initialize_lq(priv, conf, sta);
2178 if (!lq_sta->ibss_sta_added)
2183 if ((i < 0) || (i > IWL_RATE_COUNT)) {
2184 sel->rate_idx = rate_lowest_index(local, sband, sta);
2188 if (sband->band == IEEE80211_BAND_5GHZ)
2189 i -= IWL_FIRST_OFDM_RATE;
2195 static void *rs_alloc_sta(void *priv_rate, gfp_t gfp)
2197 struct iwl4965_lq_sta *lq_sta;
2198 struct iwl_priv *priv;
2201 priv = (struct iwl_priv *)priv_rate;
2202 IWL_DEBUG_RATE("create station rate scale window\n");
2204 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
2208 lq_sta->lq.sta_id = 0xff;
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]));
2218 static void rs_rate_init(void *priv_rate, void *priv_sta,
2219 struct ieee80211_local *local,
2220 struct sta_info *sta)
2223 struct ieee80211_conf *conf = &local->hw.conf;
2224 struct ieee80211_supported_band *sband;
2225 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
2226 struct iwl4965_lq_sta *lq_sta = priv_sta;
2228 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2230 lq_sta->flush_timer = 0;
2231 lq_sta->supp_rates = sta->supp_rates[sband->band];
2232 sta->txrate_idx = 3;
2233 for (j = 0; j < LQ_SIZE; j++)
2234 for (i = 0; i < IWL_RATE_COUNT; i++)
2235 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
2237 IWL_DEBUG_RATE("LQ: *** rate scale global init ***\n");
2238 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2239 * the lowest or the highest rate.. Could consider using RSSI from
2240 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2243 lq_sta->ibss_sta_added = 0;
2244 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2245 u8 sta_id = iwl_find_station(priv, sta->addr);
2246 DECLARE_MAC_BUF(mac);
2248 /* for IBSS the call are from tasklet */
2249 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2250 print_mac(mac, sta->addr));
2252 if (sta_id == IWL_INVALID_STATION) {
2253 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2254 print_mac(mac, sta->addr));
2255 sta_id = iwl_add_station_flags(priv, sta->addr,
2256 0, CMD_ASYNC, NULL);
2258 if ((sta_id != IWL_INVALID_STATION)) {
2259 lq_sta->lq.sta_id = sta_id;
2260 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2262 /* FIXME: this is w/a remove it later */
2263 priv->assoc_station_added = 1;
2266 /* Find highest tx rate supported by hardware and destination station */
2267 for (i = 0; i < sband->n_bitrates; i++)
2268 if (sta->supp_rates[sband->band] & BIT(i))
2269 sta->txrate_idx = i;
2271 sta->last_txrate_idx = sta->txrate_idx;
2272 /* WTF is with this bogus comment? A doesn't have cck rates */
2273 /* For MODE_IEEE80211A, cck rates are at end of rate table */
2274 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2275 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2278 lq_sta->is_green = rs_use_green(priv, conf);
2279 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2280 lq_sta->active_rate_basic = priv->active_rate_basic;
2281 lq_sta->band = priv->band;
2282 #ifdef CONFIG_IWL4965_HT
2284 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2285 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2287 lq_sta->active_siso_rate =
2288 priv->current_ht_config.supp_mcs_set[0] << 1;
2289 lq_sta->active_siso_rate |=
2290 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2291 lq_sta->active_siso_rate &= ~((u16)0x2);
2292 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2295 lq_sta->active_mimo2_rate =
2296 priv->current_ht_config.supp_mcs_set[1] << 1;
2297 lq_sta->active_mimo2_rate |=
2298 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2299 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2300 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2302 lq_sta->active_mimo3_rate =
2303 priv->current_ht_config.supp_mcs_set[2] << 1;
2304 lq_sta->active_mimo3_rate |=
2305 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2306 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2307 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2309 IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2310 lq_sta->active_siso_rate,
2311 lq_sta->active_mimo2_rate,
2312 lq_sta->active_mimo3_rate);
2314 /* These values will be overriden later */
2315 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2316 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2318 /* as default allow aggregation for all tids */
2319 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2320 #endif /*CONFIG_IWL4965_HT*/
2321 #ifdef CONFIG_MAC80211_DEBUGFS
2325 if (priv->assoc_station_added)
2326 priv->lq_mngr.lq_ready = 1;
2328 rs_initialize_lq(priv, conf, sta);
2331 static void rs_fill_link_cmd(const struct iwl_priv *priv,
2332 struct iwl4965_lq_sta *lq_sta,
2335 struct iwl4965_scale_tbl_info tbl_type;
2338 int repeat_rate = 0;
2339 u8 ant_toggle_cnt = 0;
2340 u8 use_ht_possible = 1;
2341 u8 valid_tx_ant = 0;
2342 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2344 /* Override starting rate (index 0) if needed for debug purposes */
2345 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2347 /* Interpret new_rate (rate_n_flags) */
2348 memset(&tbl_type, 0, sizeof(tbl_type));
2349 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2350 &tbl_type, &rate_idx);
2352 /* How many times should we repeat the initial rate? */
2353 if (is_legacy(tbl_type.lq_type)) {
2355 repeat_rate = IWL_NUMBER_TRY;
2357 repeat_rate = IWL_HT_NUMBER_TRY;
2360 lq_cmd->general_params.mimo_delimiter =
2361 is_mimo(tbl_type.lq_type) ? 1 : 0;
2363 /* Fill 1st table entry (index 0) */
2364 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2366 if (num_of_ant(tbl_type.ant_type) == 1) {
2367 lq_cmd->general_params.single_stream_ant_msk =
2369 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2370 lq_cmd->general_params.dual_stream_ant_msk =
2372 } /* otherwise we don't modify the existing value */
2378 valid_tx_ant = priv->hw_params.valid_tx_ant;
2380 /* Fill rest of rate table */
2381 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2382 /* Repeat initial/next rate.
2383 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2384 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2385 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2386 if (is_legacy(tbl_type.lq_type)) {
2387 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2390 rs_toggle_antenna(valid_tx_ant,
2391 &new_rate, &tbl_type))
2395 /* Override next rate if needed for debug purposes */
2396 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2398 /* Fill next table entry */
2399 lq_cmd->rs_table[index].rate_n_flags =
2400 cpu_to_le32(new_rate);
2405 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2408 /* Indicate to uCode which entries might be MIMO.
2409 * If initial rate was MIMO, this will finally end up
2410 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2411 if (is_mimo(tbl_type.lq_type))
2412 lq_cmd->general_params.mimo_delimiter = index;
2415 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2418 /* How many times should we repeat the next rate? */
2419 if (is_legacy(tbl_type.lq_type)) {
2420 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2423 rs_toggle_antenna(valid_tx_ant,
2424 &new_rate, &tbl_type))
2427 repeat_rate = IWL_NUMBER_TRY;
2429 repeat_rate = IWL_HT_NUMBER_TRY;
2432 /* Don't allow HT rates after next pass.
2433 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2434 use_ht_possible = 0;
2436 /* Override next rate if needed for debug purposes */
2437 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2439 /* Fill next table entry */
2440 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2446 lq_cmd->agg_params.agg_dis_start_th = 3;
2447 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
2450 static void *rs_alloc(struct ieee80211_local *local)
2452 return local->hw.priv;
2454 /* rate scale requires free function to be implemented */
2455 static void rs_free(void *priv_rate)
2460 static void rs_clear(void *priv_rate)
2462 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
2464 IWL_DEBUG_RATE("enter\n");
2466 priv->lq_mngr.lq_ready = 0;
2468 IWL_DEBUG_RATE("leave\n");
2471 static void rs_free_sta(void *priv_rate, void *priv_sta)
2473 struct iwl4965_lq_sta *lq_sta = priv_sta;
2474 struct iwl_priv *priv;
2476 priv = (struct iwl_priv *)priv_rate;
2477 IWL_DEBUG_RATE("enter\n");
2479 IWL_DEBUG_RATE("leave\n");
2483 #ifdef CONFIG_MAC80211_DEBUGFS
2484 static int open_file_generic(struct inode *inode, struct file *file)
2486 file->private_data = inode->i_private;
2489 static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
2490 u32 *rate_n_flags, int index)
2492 struct iwl_priv *priv;
2495 if (lq_sta->dbg_fixed_rate) {
2497 *rate_n_flags = lq_sta->dbg_fixed_rate;
2499 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2500 *rate_n_flags = 0x800D;
2502 *rate_n_flags = 0x820A;
2504 IWL_DEBUG_RATE("Fixed rate ON\n");
2506 IWL_DEBUG_RATE("Fixed rate OFF\n");
2510 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2511 const char __user *user_buf, size_t count, loff_t *ppos)
2513 struct iwl4965_lq_sta *lq_sta = file->private_data;
2514 struct iwl_priv *priv;
2520 memset(buf, 0, sizeof(buf));
2521 buf_size = min(count, sizeof(buf) - 1);
2522 if (copy_from_user(buf, user_buf, buf_size))
2525 if (sscanf(buf, "%x", &parsed_rate) == 1)
2526 lq_sta->dbg_fixed_rate = parsed_rate;
2528 lq_sta->dbg_fixed_rate = 0;
2530 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2531 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2532 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2533 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2535 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2536 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2538 if (lq_sta->dbg_fixed_rate) {
2539 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2540 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
2546 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2547 char __user *user_buf, size_t count, loff_t *ppos)
2553 struct iwl4965_lq_sta *lq_sta = file->private_data;
2555 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2556 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2557 lq_sta->total_failed, lq_sta->total_success,
2558 lq_sta->active_legacy_rate);
2559 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2560 lq_sta->dbg_fixed_rate);
2561 desc += sprintf(buff+desc, "general:"
2562 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2563 lq_sta->lq.general_params.flags,
2564 lq_sta->lq.general_params.mimo_delimiter,
2565 lq_sta->lq.general_params.single_stream_ant_msk,
2566 lq_sta->lq.general_params.dual_stream_ant_msk);
2568 desc += sprintf(buff+desc, "agg:"
2569 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2570 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2571 lq_sta->lq.agg_params.agg_dis_start_th,
2572 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2574 desc += sprintf(buff+desc,
2575 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2576 lq_sta->lq.general_params.start_rate_index[0],
2577 lq_sta->lq.general_params.start_rate_index[1],
2578 lq_sta->lq.general_params.start_rate_index[2],
2579 lq_sta->lq.general_params.start_rate_index[3]);
2582 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2583 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2584 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2586 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2589 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2590 .write = rs_sta_dbgfs_scale_table_write,
2591 .read = rs_sta_dbgfs_scale_table_read,
2592 .open = open_file_generic,
2594 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2595 char __user *user_buf, size_t count, loff_t *ppos)
2601 struct iwl4965_lq_sta *lq_sta = file->private_data;
2602 for (i = 0; i < LQ_SIZE; i++) {
2603 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2605 lq_sta->active_tbl == i?"*":"x",
2606 lq_sta->lq_info[i].lq_type,
2607 lq_sta->lq_info[i].is_SGI,
2608 lq_sta->lq_info[i].is_fat,
2609 lq_sta->lq_info[i].is_dup,
2610 lq_sta->lq_info[i].current_rate);
2611 for (j = 0; j < IWL_RATE_COUNT; j++) {
2612 desc += sprintf(buff+desc,
2613 "counter=%d success=%d %%=%d\n",
2614 lq_sta->lq_info[i].win[j].counter,
2615 lq_sta->lq_info[i].win[j].success_counter,
2616 lq_sta->lq_info[i].win[j].success_ratio);
2619 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2622 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2623 .read = rs_sta_dbgfs_stats_table_read,
2624 .open = open_file_generic,
2627 static void rs_add_debugfs(void *priv, void *priv_sta,
2630 struct iwl4965_lq_sta *lq_sta = priv_sta;
2631 lq_sta->rs_sta_dbgfs_scale_table_file =
2632 debugfs_create_file("rate_scale_table", 0600, dir,
2633 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2634 lq_sta->rs_sta_dbgfs_stats_table_file =
2635 debugfs_create_file("rate_stats_table", 0600, dir,
2636 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2637 #ifdef CONFIG_IWL4965_HT
2638 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2639 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2640 &lq_sta->tx_agg_tid_en);
2645 static void rs_remove_debugfs(void *priv, void *priv_sta)
2647 struct iwl4965_lq_sta *lq_sta = priv_sta;
2648 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2649 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2650 #ifdef CONFIG_IWL4965_HT
2651 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2656 static struct rate_control_ops rs_ops = {
2659 .tx_status = rs_tx_status,
2660 .get_rate = rs_get_rate,
2661 .rate_init = rs_rate_init,
2665 .alloc_sta = rs_alloc_sta,
2666 .free_sta = rs_free_sta,
2667 #ifdef CONFIG_MAC80211_DEBUGFS
2668 .add_sta_debugfs = rs_add_debugfs,
2669 .remove_sta_debugfs = rs_remove_debugfs,
2673 int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2675 struct ieee80211_local *local = hw_to_local(hw);
2676 struct iwl_priv *priv = hw->priv;
2677 struct iwl4965_lq_sta *lq_sta;
2678 struct sta_info *sta;
2680 u32 samples = 0, success = 0, good = 0;
2681 unsigned long now = jiffies;
2683 u8 lq_type, antenna;
2687 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2688 if (!sta || !sta->rate_ctrl_priv) {
2690 IWL_DEBUG_RATE("leave - no private rate data!\n");
2692 IWL_DEBUG_RATE("leave - no station!\n");
2694 return sprintf(buf, "station %d not found\n", sta_id);
2697 lq_sta = (void *)sta->rate_ctrl_priv;
2699 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
2700 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
2702 if (is_legacy(lq_type))
2703 i = IWL_RATE_54M_INDEX;
2705 i = IWL_RATE_60M_INDEX;
2709 int active = lq_sta->active_tbl;
2712 sprintf(&buf[cnt], " %2dMbs: ", iwl_rates[i].ieee / 2);
2714 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2715 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2717 (lq_sta->lq_info[active].win[i].data & mask)
2720 samples += lq_sta->lq_info[active].win[i].counter;
2721 good += lq_sta->lq_info[active].win[i].success_counter;
2722 success += lq_sta->lq_info[active].win[i].success_counter *
2725 if (lq_sta->lq_info[active].win[i].stamp) {
2727 jiffies_to_msecs(now -
2728 lq_sta->lq_info[active].win[i].stamp);
2730 if (delta > max_time)
2733 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
2737 j = iwl4965_get_prev_ieee_rate(i);
2744 * Display the average rate of all samples taken.
2745 * NOTE: We multiply # of samples by 2 since the IEEE measurement
2746 * added from iwl_rates is actually 2X the rate.
2749 cnt += sprintf(&buf[cnt],
2750 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2751 "%3d%% success (%d good packets over %d tries)\n",
2752 success / (2 * samples), (success * 5 / samples) % 10,
2753 max_time, good * 100 / samples, good, samples);
2755 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2757 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
2758 "active_search %d rate index %d\n", lq_type, antenna,
2759 lq_sta->search_better_tbl, sta->last_txrate_idx);
2765 void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2767 struct iwl_priv *priv = hw->priv;
2769 priv->lq_mngr.lq_ready = 1;
2772 int iwl4965_rate_control_register(void)
2774 return ieee80211_rate_control_register(&rs_ops);
2777 void iwl4965_rate_control_unregister(void)
2779 ieee80211_rate_control_unregister(&rs_ops);