iwlwifi: keep 3945 and 4965 headers separate
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-4965-rs.c
blob0e384e60648e943cb3a928760fc62c9a75f9fbae
1 /******************************************************************************
3 * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
21 * Contact Information:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 *****************************************************************************/
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/skbuff.h>
29 #include <linux/wireless.h>
30 #include <net/mac80211.h>
31 #include <net/ieee80211.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/delay.h>
37 #include <linux/workqueue.h>
39 #include "../net/mac80211/ieee80211_rate.h"
41 #include "iwl-4965.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
51 #define IWL_RATE_HIGH_TH 10880
52 #define IWL_RATE_MIN_FAILURE_TH 6
53 #define IWL_RATE_MIN_SUCCESS_TH 8
54 #define IWL_RATE_DECREASE_TH 1920
55 #define IWL_RATE_INCREASE_TH 8960
56 #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ) /*2 seconds */
58 static u8 rs_ht_to_legacy[] = {
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX,
62 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
63 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
64 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
65 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
68 struct iwl_rate {
69 u32 rate_n_flags;
70 } __attribute__ ((packed));
72 struct iwl_rate_scale_data {
73 u64 data;
74 s32 success_counter;
75 s32 success_ratio;
76 s32 counter;
77 s32 average_tpt;
78 unsigned long stamp;
81 struct iwl_scale_tbl_info {
82 enum iwl_table_type lq_type;
83 enum iwl_antenna_type antenna_type;
84 u8 is_SGI;
85 u8 is_fat;
86 u8 is_dup;
87 u8 action;
88 s32 *expected_tpt;
89 struct iwl_rate current_rate;
90 struct iwl_rate_scale_data win[IWL_RATE_COUNT];
93 struct iwl_rate_scale_priv {
94 u8 active_tbl;
95 u8 enable_counter;
96 u8 stay_in_tbl;
97 u8 search_better_tbl;
98 s32 last_tpt;
99 u32 table_count_limit;
100 u32 max_failure_limit;
101 u32 max_success_limit;
102 u32 table_count;
103 u32 total_failed;
104 u32 total_success;
105 u32 flush_timer;
106 u8 action_counter;
107 u8 antenna;
108 u8 valid_antenna;
109 u8 is_green;
110 u8 is_dup;
111 u8 phymode;
112 u8 ibss_sta_added;
113 u32 supp_rates;
114 u16 active_rate;
115 u16 active_siso_rate;
116 u16 active_mimo_rate;
117 u16 active_rate_basic;
118 struct iwl_link_quality_cmd lq;
119 struct iwl_scale_tbl_info lq_info[LQ_SIZE];
120 #ifdef CONFIG_MAC80211_DEBUGFS
121 struct dentry *rs_sta_dbgfs_scale_table_file;
122 struct dentry *rs_sta_dbgfs_stats_table_file;
123 struct iwl_rate dbg_fixed;
124 struct iwl_priv *drv;
125 #endif
128 static void rs_rate_scale_perform(struct iwl_priv *priv,
129 struct net_device *dev,
130 struct ieee80211_hdr *hdr,
131 struct sta_info *sta);
132 static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
133 struct iwl_rate *tx_mcs,
134 struct iwl_link_quality_cmd *tbl);
137 #ifdef CONFIG_MAC80211_DEBUGFS
138 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
139 struct iwl_rate *mcs, int index);
140 #else
141 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
142 struct iwl_rate *mcs, int index)
144 #endif
145 static s32 expected_tpt_A[IWL_RATE_COUNT] = {
146 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
149 static s32 expected_tpt_G[IWL_RATE_COUNT] = {
150 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
153 static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
154 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
157 static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
158 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
161 static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
162 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
165 static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
166 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
169 static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
170 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
173 static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
174 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
177 static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
178 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
181 static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
182 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
185 static int iwl_lq_sync_callback(struct iwl_priv *priv,
186 struct iwl_cmd *cmd, struct sk_buff *skb)
188 /*We didn't cache the SKB; let the caller free it */
189 return 1;
192 static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
194 return (u8)(rate_n_flags & 0xFF);
197 static int rs_send_lq_cmd(struct iwl_priv *priv,
198 struct iwl_link_quality_cmd *lq, u8 flags)
200 #ifdef CONFIG_IWLWIFI_DEBUG
201 int i;
202 #endif
203 struct iwl_host_cmd cmd = {
204 .id = REPLY_TX_LINK_QUALITY_CMD,
205 .len = sizeof(struct iwl_link_quality_cmd),
206 .meta.flags = flags,
207 .data = lq,
210 if ((lq->sta_id == 0xFF) &&
211 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
212 return -EINVAL;
214 if (lq->sta_id == 0xFF)
215 lq->sta_id = IWL_AP_ID;
217 IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
218 IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
219 lq->general_params.single_stream_ant_msk,
220 lq->general_params.dual_stream_ant_msk);
221 #ifdef CONFIG_IWLWIFI_DEBUG
222 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
223 IWL_DEBUG_RATE("lq index %d 0x%X\n",
224 i, lq->rs_table[i].rate_n_flags);
225 #endif
227 if (flags & CMD_ASYNC)
228 cmd.meta.u.callback = iwl_lq_sync_callback;
230 if (iwl_is_associated(priv) && priv->assoc_station_added &&
231 priv->lq_mngr.lq_ready)
232 return iwl_send_cmd(priv, &cmd);
234 return 0;
237 static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
239 window->data = 0;
240 window->success_counter = 0;
241 window->success_ratio = IWL_INVALID_VALUE;
242 window->counter = 0;
243 window->average_tpt = IWL_INVALID_VALUE;
244 window->stamp = 0;
247 static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
248 int scale_index, s32 tpt, u32 status)
250 struct iwl_rate_scale_data *window = NULL;
251 u64 mask;
252 u8 win_size = IWL_RATE_MAX_WINDOW;
253 s32 fail_count;
255 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
256 return -EINVAL;
258 window = &(windows[scale_index]);
260 if (window->counter >= win_size) {
262 window->counter = win_size - 1;
263 mask = 1;
264 mask = (mask << (win_size - 1));
265 if ((window->data & mask)) {
266 window->data &= ~mask;
267 window->success_counter = window->success_counter - 1;
271 window->counter = window->counter + 1;
272 mask = window->data;
273 window->data = (mask << 1);
274 if (status != 0) {
275 window->success_counter = window->success_counter + 1;
276 window->data |= 0x1;
279 if (window->counter > 0)
280 window->success_ratio = 128 * (100 * window->success_counter)
281 / window->counter;
282 else
283 window->success_ratio = IWL_INVALID_VALUE;
285 fail_count = window->counter - window->success_counter;
287 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
288 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
289 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
290 else
291 window->average_tpt = IWL_INVALID_VALUE;
293 window->stamp = jiffies;
295 return 0;
298 static void rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
299 struct iwl_scale_tbl_info *tbl,
300 int index, u8 use_green)
302 if (is_legacy(tbl->lq_type)) {
303 mcs_rate->rate_n_flags = iwl_rates[index].plcp;
304 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
305 mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
307 } else if (is_siso(tbl->lq_type)) {
308 if (index > IWL_LAST_OFDM_RATE)
309 index = IWL_LAST_OFDM_RATE;
310 mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
311 RATE_MCS_HT_MSK;
312 } else {
313 if (index > IWL_LAST_OFDM_RATE)
314 index = IWL_LAST_OFDM_RATE;
315 mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
316 RATE_MCS_HT_MSK;
319 switch (tbl->antenna_type) {
320 case ANT_BOTH:
321 mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
322 break;
323 case ANT_MAIN:
324 mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
325 break;
326 case ANT_AUX:
327 mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
328 break;
329 case ANT_NONE:
330 break;
333 if (is_legacy(tbl->lq_type))
334 return;
336 if (tbl->is_fat) {
337 if (tbl->is_dup)
338 mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
339 else
340 mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
342 if (tbl->is_SGI)
343 mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
345 if (use_green) {
346 mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
347 if (is_siso(tbl->lq_type))
348 mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
352 static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
353 int phymode, struct iwl_scale_tbl_info *tbl,
354 int *rate_idx)
356 int index;
357 u32 ant_msk;
359 index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
361 if (index == IWL_RATE_INVALID) {
362 *rate_idx = -1;
363 return -EINVAL;
365 tbl->is_SGI = 0;
366 tbl->is_fat = 0;
367 tbl->is_dup = 0;
368 tbl->antenna_type = ANT_BOTH;
370 if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
371 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
373 if (ant_msk == RATE_MCS_ANT_AB_MSK)
374 tbl->lq_type = LQ_NONE;
375 else {
377 if (phymode == MODE_IEEE80211A)
378 tbl->lq_type = LQ_A;
379 else
380 tbl->lq_type = LQ_G;
382 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
383 tbl->antenna_type = ANT_MAIN;
384 else
385 tbl->antenna_type = ANT_AUX;
387 *rate_idx = index;
389 } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
390 <= IWL_RATE_SISO_60M_PLCP) {
391 tbl->lq_type = LQ_SISO;
393 ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
394 if (ant_msk == RATE_MCS_ANT_AB_MSK)
395 tbl->lq_type = LQ_NONE;
396 else {
397 if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
398 tbl->antenna_type = ANT_MAIN;
399 else
400 tbl->antenna_type = ANT_AUX;
402 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
403 tbl->is_SGI = 1;
405 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
406 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
407 tbl->is_fat = 1;
409 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
410 tbl->is_dup = 1;
412 *rate_idx = index;
413 } else {
414 tbl->lq_type = LQ_MIMO;
415 if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
416 tbl->is_SGI = 1;
418 if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
419 (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
420 tbl->is_fat = 1;
422 if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
423 tbl->is_dup = 1;
424 *rate_idx = index;
426 return 0;
429 static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
430 struct iwl_scale_tbl_info *tbl)
432 if (tbl->antenna_type == ANT_AUX) {
433 tbl->antenna_type = ANT_MAIN;
434 new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
435 new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
436 } else {
437 tbl->antenna_type = ANT_AUX;
438 new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
439 new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
443 static inline u8 rs_use_green(struct iwl_priv *priv)
445 #ifdef CONFIG_IWLWIFI_HT
446 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
447 return 0;
449 return ((priv->current_assoc_ht.is_green_field) &&
450 !(priv->current_assoc_ht.operating_mode & 0x4));
451 #endif /*CONFIG_IWLWIFI_HT */
452 return 0;
456 * rs_get_supported_rates - get the available rates
458 * if management frame or broadcast frame only return
459 * basic available rates.
462 static void rs_get_supported_rates(struct iwl_rate_scale_priv *lq_data,
463 struct ieee80211_hdr *hdr,
464 enum iwl_table_type rate_type,
465 u16 *data_rate)
467 if (is_legacy(rate_type))
468 *data_rate = lq_data->active_rate;
469 else {
470 if (is_siso(rate_type))
471 *data_rate = lq_data->active_siso_rate;
472 else
473 *data_rate = lq_data->active_mimo_rate;
476 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
477 lq_data->active_rate_basic)
478 *data_rate = lq_data->active_rate_basic;
481 static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
483 u8 high = IWL_RATE_INVALID;
484 u8 low = IWL_RATE_INVALID;
486 /* 802.11A or ht walks to the next literal adjacent rate in
487 * the rate table */
488 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
489 int i;
490 u32 mask;
492 /* Find the previous rate that is in the rate mask */
493 i = index - 1;
494 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
495 if (rate_mask & mask) {
496 low = i;
497 break;
501 /* Find the next rate that is in the rate mask */
502 i = index + 1;
503 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
504 if (rate_mask & mask) {
505 high = i;
506 break;
510 return (high << 8) | low;
513 low = index;
514 while (low != IWL_RATE_INVALID) {
515 low = iwl_rates[low].prev_rs;
516 if (low == IWL_RATE_INVALID)
517 break;
518 if (rate_mask & (1 << low))
519 break;
520 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
523 high = index;
524 while (high != IWL_RATE_INVALID) {
525 high = iwl_rates[high].next_rs;
526 if (high == IWL_RATE_INVALID)
527 break;
528 if (rate_mask & (1 << high))
529 break;
530 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
533 return (high << 8) | low;
536 static void rs_get_lower_rate(struct iwl_rate_scale_priv *lq_data,
537 struct iwl_scale_tbl_info *tbl, u8 scale_index,
538 u8 ht_possible, struct iwl_rate *mcs_rate)
540 s32 low;
541 u16 rate_mask;
542 u16 high_low;
543 u8 switch_to_legacy = 0;
544 u8 is_green = lq_data->is_green;
546 /* check if we need to switch from HT to legacy rates.
547 * assumption is that mandatory rates (1Mbps or 6Mbps)
548 * are always supported (spec demand) */
549 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
550 switch_to_legacy = 1;
551 scale_index = rs_ht_to_legacy[scale_index];
552 if (lq_data->phymode == MODE_IEEE80211A)
553 tbl->lq_type = LQ_A;
554 else
555 tbl->lq_type = LQ_G;
557 if ((tbl->antenna_type == ANT_BOTH) ||
558 (tbl->antenna_type == ANT_NONE))
559 tbl->antenna_type = ANT_MAIN;
561 tbl->is_fat = 0;
562 tbl->is_SGI = 0;
565 rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
567 /* mask with station rate restriction */
568 if (is_legacy(tbl->lq_type)) {
569 if (lq_data->phymode == (u8) MODE_IEEE80211A)
570 rate_mask = (u16)(rate_mask &
571 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
572 else
573 rate_mask = (u16)(rate_mask & lq_data->supp_rates);
576 /* if we did switched from HT to legacy check current rate */
577 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
578 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
579 return;
582 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
583 low = high_low & 0xff;
585 if (low != IWL_RATE_INVALID)
586 rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
587 else
588 rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
591 static void rs_tx_status(void *priv_rate,
592 struct net_device *dev,
593 struct sk_buff *skb,
594 struct ieee80211_tx_status *tx_resp)
596 int status;
597 u8 retries;
598 int rs_index, index = 0;
599 struct iwl_rate_scale_priv *lq;
600 struct iwl_link_quality_cmd *table;
601 struct sta_info *sta;
602 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
603 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
604 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
605 struct iwl_rate_scale_data *window = NULL;
606 struct iwl_rate_scale_data *search_win = NULL;
607 struct iwl_rate tx_mcs;
608 struct iwl_scale_tbl_info tbl_type;
609 struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
610 u8 active_index = 0;
611 u16 fc = le16_to_cpu(hdr->frame_control);
612 s32 tpt = 0;
614 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
616 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
617 return;
619 retries = tx_resp->retry_count;
621 if (retries > 15)
622 retries = 15;
625 sta = sta_info_get(local, hdr->addr1);
627 if (!sta || !sta->rate_ctrl_priv) {
628 if (sta)
629 sta_info_put(sta);
630 return;
633 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
635 if (!priv->lq_mngr.lq_ready)
636 return;
638 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
639 return;
641 table = &lq->lq;
642 active_index = lq->active_tbl;
644 lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
645 if (!lq->antenna)
646 lq->antenna = lq->valid_antenna;
648 lq->antenna = lq->valid_antenna;
649 curr_tbl = &(lq->lq_info[active_index]);
650 search_tbl = &(lq->lq_info[(1 - active_index)]);
651 window = (struct iwl_rate_scale_data *)
652 &(curr_tbl->win[0]);
653 search_win = (struct iwl_rate_scale_data *)
654 &(search_tbl->win[0]);
656 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
658 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
659 &tbl_type, &rs_index);
660 if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
661 IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
662 rs_index, tx_mcs.rate_n_flags);
663 sta_info_put(sta);
664 return;
667 if (retries &&
668 (tx_mcs.rate_n_flags !=
669 le32_to_cpu(table->rs_table[0].rate_n_flags))) {
670 IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
671 tx_mcs.rate_n_flags,
672 le32_to_cpu(table->rs_table[0].rate_n_flags));
673 sta_info_put(sta);
674 return;
677 while (retries) {
678 tx_mcs.rate_n_flags =
679 le32_to_cpu(table->rs_table[index].rate_n_flags);
680 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
681 &tbl_type, &rs_index);
683 if ((tbl_type.lq_type == search_tbl->lq_type) &&
684 (tbl_type.antenna_type == search_tbl->antenna_type) &&
685 (tbl_type.is_SGI == search_tbl->is_SGI)) {
686 if (search_tbl->expected_tpt)
687 tpt = search_tbl->expected_tpt[rs_index];
688 else
689 tpt = 0;
690 rs_collect_tx_data(search_win,
691 rs_index, tpt, 0);
692 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
693 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
694 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
695 if (curr_tbl->expected_tpt)
696 tpt = curr_tbl->expected_tpt[rs_index];
697 else
698 tpt = 0;
699 rs_collect_tx_data(window, rs_index, tpt, 0);
701 if (lq->stay_in_tbl)
702 lq->total_failed++;
703 --retries;
704 index++;
708 if (!tx_resp->retry_count)
709 tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
710 else
711 tx_mcs.rate_n_flags =
712 le32_to_cpu(table->rs_table[index].rate_n_flags);
714 rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
715 &tbl_type, &rs_index);
717 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
718 status = 1;
719 else
720 status = 0;
722 if ((tbl_type.lq_type == search_tbl->lq_type) &&
723 (tbl_type.antenna_type == search_tbl->antenna_type) &&
724 (tbl_type.is_SGI == search_tbl->is_SGI)) {
725 if (search_tbl->expected_tpt)
726 tpt = search_tbl->expected_tpt[rs_index];
727 else
728 tpt = 0;
729 rs_collect_tx_data(search_win,
730 rs_index, tpt, status);
731 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
732 (tbl_type.antenna_type == curr_tbl->antenna_type) &&
733 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
734 if (curr_tbl->expected_tpt)
735 tpt = curr_tbl->expected_tpt[rs_index];
736 else
737 tpt = 0;
738 rs_collect_tx_data(window, rs_index, tpt, status);
741 if (lq->stay_in_tbl) {
742 if (status)
743 lq->total_success++;
744 else
745 lq->total_failed++;
748 rs_rate_scale_perform(priv, dev, hdr, sta);
749 sta_info_put(sta);
750 return;
753 static u8 rs_is_ant_connected(u8 valid_antenna,
754 enum iwl_antenna_type antenna_type)
756 if (antenna_type == ANT_AUX)
757 return ((valid_antenna & 0x2) ? 1:0);
758 else if (antenna_type == ANT_MAIN)
759 return ((valid_antenna & 0x1) ? 1:0);
760 else if (antenna_type == ANT_BOTH)
761 return ((valid_antenna & 0x3) == 0x3);
763 return 1;
766 static u8 rs_is_other_ant_connected(u8 valid_antenna,
767 enum iwl_antenna_type antenna_type)
769 if (antenna_type == ANT_AUX)
770 return rs_is_ant_connected(valid_antenna, ANT_MAIN);
771 else
772 return rs_is_ant_connected(valid_antenna, ANT_AUX);
774 return 0;
777 static void rs_set_stay_in_table(u8 is_legacy,
778 struct iwl_rate_scale_priv *lq_data)
780 IWL_DEBUG_HT("we are staying in the same table\n");
781 lq_data->stay_in_tbl = 1;
782 if (is_legacy) {
783 lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
784 lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
785 lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
786 } else {
787 lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
788 lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
789 lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
791 lq_data->table_count = 0;
792 lq_data->total_failed = 0;
793 lq_data->total_success = 0;
796 static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
797 struct iwl_scale_tbl_info *tbl)
799 if (is_legacy(tbl->lq_type)) {
800 if (!is_a_band(tbl->lq_type))
801 tbl->expected_tpt = expected_tpt_G;
802 else
803 tbl->expected_tpt = expected_tpt_A;
804 } else if (is_siso(tbl->lq_type)) {
805 if (tbl->is_fat && !lq_data->is_dup)
806 if (tbl->is_SGI)
807 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
808 else
809 tbl->expected_tpt = expected_tpt_siso40MHz;
810 else if (tbl->is_SGI)
811 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
812 else
813 tbl->expected_tpt = expected_tpt_siso20MHz;
815 } else if (is_mimo(tbl->lq_type)) {
816 if (tbl->is_fat && !lq_data->is_dup)
817 if (tbl->is_SGI)
818 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
819 else
820 tbl->expected_tpt = expected_tpt_mimo40MHz;
821 else if (tbl->is_SGI)
822 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
823 else
824 tbl->expected_tpt = expected_tpt_mimo20MHz;
825 } else
826 tbl->expected_tpt = expected_tpt_G;
829 #ifdef CONFIG_IWLWIFI_HT
830 static s32 rs_get_best_rate(struct iwl_priv *priv,
831 struct iwl_rate_scale_priv *lq_data,
832 struct iwl_scale_tbl_info *tbl,
833 u16 rate_mask, s8 index, s8 rate)
835 struct iwl_scale_tbl_info *active_tbl =
836 &(lq_data->lq_info[lq_data->active_tbl]);
837 s32 new_rate, high, low, start_hi;
838 s32 active_sr = active_tbl->win[index].success_ratio;
839 s32 *tpt_tbl = tbl->expected_tpt;
840 s32 active_tpt = active_tbl->expected_tpt[index];
841 u16 high_low;
843 new_rate = high = low = start_hi = IWL_RATE_INVALID;
845 for (; ;) {
846 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
848 low = high_low & 0xff;
849 high = (high_low >> 8) & 0xff;
851 if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
852 ((active_sr > IWL_RATE_DECREASE_TH) &&
853 (active_sr <= IWL_RATE_HIGH_TH) &&
854 (tpt_tbl[rate] <= active_tpt))) ||
855 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
856 (tpt_tbl[rate] > active_tpt))) {
858 if (start_hi != IWL_RATE_INVALID) {
859 new_rate = start_hi;
860 break;
862 new_rate = rate;
863 if (low != IWL_RATE_INVALID)
864 rate = low;
865 else
866 break;
867 } else {
868 if (new_rate != IWL_RATE_INVALID)
869 break;
870 else if (high != IWL_RATE_INVALID) {
871 start_hi = high;
872 rate = high;
873 } else {
874 new_rate = rate;
875 break;
880 return new_rate;
882 #endif /* CONFIG_IWLWIFI_HT */
884 static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
886 return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
889 static int rs_switch_to_mimo(struct iwl_priv *priv,
890 struct iwl_rate_scale_priv *lq_data,
891 struct iwl_scale_tbl_info *tbl, int index)
893 #ifdef CONFIG_IWLWIFI_HT
894 u16 rate_mask;
895 s32 rate;
896 s8 is_green = lq_data->is_green;
898 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
899 return -1;
901 IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
902 tbl->lq_type = LQ_MIMO;
903 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
904 &rate_mask);
906 if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
907 return -1;
909 if (!rs_is_both_ant_supp(lq_data->antenna))
910 return -1;
912 tbl->is_dup = lq_data->is_dup;
913 tbl->action = 0;
914 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
915 tbl->is_fat = 1;
916 else
917 tbl->is_fat = 0;
919 if (tbl->is_fat) {
920 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
921 tbl->is_SGI = 1;
922 else
923 tbl->is_SGI = 0;
924 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
925 tbl->is_SGI = 1;
926 else
927 tbl->is_SGI = 0;
929 rs_get_expected_tpt_table(lq_data, tbl);
931 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
933 IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
934 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
935 return -1;
936 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
938 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
939 tbl->current_rate.rate_n_flags, is_green);
941 #endif /*CONFIG_IWLWIFI_HT */
942 return 0;
945 static int rs_switch_to_siso(struct iwl_priv *priv,
946 struct iwl_rate_scale_priv *lq_data,
947 struct iwl_scale_tbl_info *tbl, int index)
949 #ifdef CONFIG_IWLWIFI_HT
950 u16 rate_mask;
951 u8 is_green = lq_data->is_green;
952 s32 rate;
954 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
955 if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
956 return -1;
958 tbl->is_dup = lq_data->is_dup;
959 tbl->lq_type = LQ_SISO;
960 tbl->action = 0;
961 rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
962 &rate_mask);
964 if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
965 tbl->is_fat = 1;
966 else
967 tbl->is_fat = 0;
969 if (tbl->is_fat) {
970 if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
971 tbl->is_SGI = 1;
972 else
973 tbl->is_SGI = 0;
974 } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
975 tbl->is_SGI = 1;
976 else
977 tbl->is_SGI = 0;
979 if (is_green)
980 tbl->is_SGI = 0;
982 rs_get_expected_tpt_table(lq_data, tbl);
983 rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
985 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
986 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
987 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
988 rate, rate_mask);
989 return -1;
991 rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
992 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
993 tbl->current_rate.rate_n_flags, is_green);
995 #endif /*CONFIG_IWLWIFI_HT */
996 return 0;
999 static int rs_move_legacy_other(struct iwl_priv *priv,
1000 struct iwl_rate_scale_priv *lq_data,
1001 int index)
1003 int ret = 0;
1004 struct iwl_scale_tbl_info *tbl =
1005 &(lq_data->lq_info[lq_data->active_tbl]);
1006 struct iwl_scale_tbl_info *search_tbl =
1007 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1008 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1009 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1010 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1011 u8 start_action = tbl->action;
1013 for (; ;) {
1014 switch (tbl->action) {
1015 case IWL_LEGACY_SWITCH_ANTENNA:
1016 IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
1018 search_tbl->lq_type = LQ_NONE;
1019 lq_data->action_counter++;
1020 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1021 break;
1022 if (!rs_is_other_ant_connected(lq_data->antenna,
1023 tbl->antenna_type))
1024 break;
1026 memcpy(search_tbl, tbl, sz);
1028 rs_toggle_antenna(&(search_tbl->current_rate),
1029 search_tbl);
1030 rs_get_expected_tpt_table(lq_data, search_tbl);
1031 lq_data->search_better_tbl = 1;
1032 goto out;
1034 case IWL_LEGACY_SWITCH_SISO:
1035 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
1036 memcpy(search_tbl, tbl, sz);
1037 search_tbl->lq_type = LQ_SISO;
1038 search_tbl->is_SGI = 0;
1039 search_tbl->is_fat = 0;
1040 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
1041 index);
1042 if (!ret) {
1043 lq_data->search_better_tbl = 1;
1044 lq_data->action_counter = 0;
1045 goto out;
1048 break;
1049 case IWL_LEGACY_SWITCH_MIMO:
1050 IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
1051 memcpy(search_tbl, tbl, sz);
1052 search_tbl->lq_type = LQ_MIMO;
1053 search_tbl->is_SGI = 0;
1054 search_tbl->is_fat = 0;
1055 search_tbl->antenna_type = ANT_BOTH;
1056 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
1057 index);
1058 if (!ret) {
1059 lq_data->search_better_tbl = 1;
1060 lq_data->action_counter = 0;
1061 goto out;
1063 break;
1065 tbl->action++;
1066 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1067 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1069 if (tbl->action == start_action)
1070 break;
1073 return 0;
1075 out:
1076 tbl->action++;
1077 if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
1078 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1079 return 0;
1083 static int rs_move_siso_to_other(struct iwl_priv *priv,
1084 struct iwl_rate_scale_priv *lq_data,
1085 int index)
1087 int ret;
1088 u8 is_green = lq_data->is_green;
1089 struct iwl_scale_tbl_info *tbl =
1090 &(lq_data->lq_info[lq_data->active_tbl]);
1091 struct iwl_scale_tbl_info *search_tbl =
1092 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1093 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1094 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1095 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1096 u8 start_action = tbl->action;
1098 for (;;) {
1099 lq_data->action_counter++;
1100 switch (tbl->action) {
1101 case IWL_SISO_SWITCH_ANTENNA:
1102 IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
1103 search_tbl->lq_type = LQ_NONE;
1104 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1105 break;
1106 if (!rs_is_other_ant_connected(lq_data->antenna,
1107 tbl->antenna_type))
1108 break;
1110 memcpy(search_tbl, tbl, sz);
1111 search_tbl->action = IWL_SISO_SWITCH_MIMO;
1112 rs_toggle_antenna(&(search_tbl->current_rate),
1113 search_tbl);
1114 lq_data->search_better_tbl = 1;
1116 goto out;
1118 case IWL_SISO_SWITCH_MIMO:
1119 IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
1120 memcpy(search_tbl, tbl, sz);
1121 search_tbl->lq_type = LQ_MIMO;
1122 search_tbl->is_SGI = 0;
1123 search_tbl->is_fat = 0;
1124 search_tbl->antenna_type = ANT_BOTH;
1125 ret = rs_switch_to_mimo(priv, lq_data, search_tbl,
1126 index);
1127 if (!ret) {
1128 lq_data->search_better_tbl = 1;
1129 goto out;
1131 break;
1132 case IWL_SISO_SWITCH_GI:
1133 IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
1134 memcpy(search_tbl, tbl, sz);
1135 search_tbl->action = 0;
1136 if (search_tbl->is_SGI)
1137 search_tbl->is_SGI = 0;
1138 else if (!is_green)
1139 search_tbl->is_SGI = 1;
1140 else
1141 break;
1142 lq_data->search_better_tbl = 1;
1143 if ((tbl->lq_type == LQ_SISO) &&
1144 (tbl->is_SGI)) {
1145 s32 tpt = lq_data->last_tpt / 100;
1146 if (((!tbl->is_fat) &&
1147 (tpt >= expected_tpt_siso20MHz[index])) ||
1148 ((tbl->is_fat) &&
1149 (tpt >= expected_tpt_siso40MHz[index])))
1150 lq_data->search_better_tbl = 0;
1152 rs_get_expected_tpt_table(lq_data, search_tbl);
1153 rs_mcs_from_tbl(&search_tbl->current_rate,
1154 search_tbl, index, is_green);
1155 goto out;
1157 tbl->action++;
1158 if (tbl->action > IWL_SISO_SWITCH_GI)
1159 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1161 if (tbl->action == start_action)
1162 break;
1164 return 0;
1166 out:
1167 tbl->action++;
1168 if (tbl->action > IWL_SISO_SWITCH_GI)
1169 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1170 return 0;
1173 static int rs_move_mimo_to_other(struct iwl_priv *priv,
1174 struct iwl_rate_scale_priv *lq_data,
1175 int index)
1177 int ret;
1178 s8 is_green = lq_data->is_green;
1179 struct iwl_scale_tbl_info *tbl =
1180 &(lq_data->lq_info[lq_data->active_tbl]);
1181 struct iwl_scale_tbl_info *search_tbl =
1182 &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1183 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1184 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1185 u8 start_action = tbl->action;
1187 for (;;) {
1188 lq_data->action_counter++;
1189 switch (tbl->action) {
1190 case IWL_MIMO_SWITCH_ANTENNA_A:
1191 case IWL_MIMO_SWITCH_ANTENNA_B:
1192 IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
1193 memcpy(search_tbl, tbl, sz);
1194 search_tbl->lq_type = LQ_SISO;
1195 search_tbl->is_SGI = 0;
1196 search_tbl->is_fat = 0;
1197 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
1198 search_tbl->antenna_type = ANT_MAIN;
1199 else
1200 search_tbl->antenna_type = ANT_AUX;
1202 ret = rs_switch_to_siso(priv, lq_data, search_tbl,
1203 index);
1204 if (!ret) {
1205 lq_data->search_better_tbl = 1;
1206 goto out;
1208 break;
1210 case IWL_MIMO_SWITCH_GI:
1211 IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
1212 memcpy(search_tbl, tbl, sz);
1213 search_tbl->lq_type = LQ_MIMO;
1214 search_tbl->antenna_type = ANT_BOTH;
1215 search_tbl->action = 0;
1216 if (search_tbl->is_SGI)
1217 search_tbl->is_SGI = 0;
1218 else
1219 search_tbl->is_SGI = 1;
1220 lq_data->search_better_tbl = 1;
1221 if ((tbl->lq_type == LQ_MIMO) &&
1222 (tbl->is_SGI)) {
1223 s32 tpt = lq_data->last_tpt / 100;
1224 if (((!tbl->is_fat) &&
1225 (tpt >= expected_tpt_mimo20MHz[index])) ||
1226 ((tbl->is_fat) &&
1227 (tpt >= expected_tpt_mimo40MHz[index])))
1228 lq_data->search_better_tbl = 0;
1230 rs_get_expected_tpt_table(lq_data, search_tbl);
1231 rs_mcs_from_tbl(&search_tbl->current_rate,
1232 search_tbl, index, is_green);
1233 goto out;
1236 tbl->action++;
1237 if (tbl->action > IWL_MIMO_SWITCH_GI)
1238 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1240 if (tbl->action == start_action)
1241 break;
1244 return 0;
1245 out:
1246 tbl->action++;
1247 if (tbl->action > IWL_MIMO_SWITCH_GI)
1248 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1249 return 0;
1253 static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
1255 struct iwl_scale_tbl_info *tbl;
1256 int i;
1257 int active_tbl;
1258 int flush_interval_passed = 0;
1260 active_tbl = lq_data->active_tbl;
1262 tbl = &(lq_data->lq_info[active_tbl]);
1264 if (lq_data->stay_in_tbl) {
1266 if (lq_data->flush_timer)
1267 flush_interval_passed =
1268 time_after(jiffies,
1269 (unsigned long)(lq_data->flush_timer +
1270 IWL_RATE_SCALE_FLUSH_INTVL));
1272 flush_interval_passed = 0;
1273 if ((lq_data->total_failed > lq_data->max_failure_limit) ||
1274 (lq_data->total_success > lq_data->max_success_limit) ||
1275 ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
1276 && (flush_interval_passed))) {
1277 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
1278 lq_data->total_failed,
1279 lq_data->total_success,
1280 flush_interval_passed);
1281 lq_data->stay_in_tbl = 0;
1282 lq_data->total_failed = 0;
1283 lq_data->total_success = 0;
1284 lq_data->flush_timer = 0;
1285 } else if (lq_data->table_count > 0) {
1286 lq_data->table_count++;
1287 if (lq_data->table_count >=
1288 lq_data->table_count_limit) {
1289 lq_data->table_count = 0;
1291 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1292 for (i = 0; i < IWL_RATE_COUNT; i++)
1293 rs_rate_scale_clear_window(
1294 &(tbl->win[i]));
1298 if (!lq_data->stay_in_tbl) {
1299 for (i = 0; i < IWL_RATE_COUNT; i++)
1300 rs_rate_scale_clear_window(&(tbl->win[i]));
1305 static void rs_rate_scale_perform(struct iwl_priv *priv,
1306 struct net_device *dev,
1307 struct ieee80211_hdr *hdr,
1308 struct sta_info *sta)
1310 int low = IWL_RATE_INVALID;
1311 int high = IWL_RATE_INVALID;
1312 int index;
1313 int i;
1314 struct iwl_rate_scale_data *window = NULL;
1315 int current_tpt = IWL_INVALID_VALUE;
1316 int low_tpt = IWL_INVALID_VALUE;
1317 int high_tpt = IWL_INVALID_VALUE;
1318 u32 fail_count;
1319 s8 scale_action = 0;
1320 u16 fc, rate_mask;
1321 u8 update_lq = 0;
1322 struct iwl_rate_scale_priv *lq_data;
1323 struct iwl_scale_tbl_info *tbl, *tbl1;
1324 u16 rate_scale_index_msk = 0;
1325 struct iwl_rate mcs_rate;
1326 u8 is_green = 0;
1327 u8 active_tbl = 0;
1328 u8 done_search = 0;
1329 u16 high_low;
1331 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1333 fc = le16_to_cpu(hdr->frame_control);
1334 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1335 /* Send management frames and broadcast/multicast data using
1336 * lowest rate. */
1337 /* TODO: this could probably be improved.. */
1338 return;
1341 if (!sta || !sta->rate_ctrl_priv)
1342 return;
1344 if (!priv->lq_mngr.lq_ready) {
1345 IWL_DEBUG_RATE("still rate scaling not ready\n");
1346 return;
1348 lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1350 if (!lq_data->search_better_tbl)
1351 active_tbl = lq_data->active_tbl;
1352 else
1353 active_tbl = 1 - lq_data->active_tbl;
1355 tbl = &(lq_data->lq_info[active_tbl]);
1356 is_green = lq_data->is_green;
1358 index = sta->last_txrate;
1360 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1361 tbl->lq_type);
1363 rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
1364 &rate_mask);
1366 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1368 /* mask with station rate restriction */
1369 if (is_legacy(tbl->lq_type)) {
1370 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1371 rate_scale_index_msk = (u16) (rate_mask &
1372 (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
1373 else
1374 rate_scale_index_msk = (u16) (rate_mask &
1375 lq_data->supp_rates);
1377 } else
1378 rate_scale_index_msk = rate_mask;
1380 if (!rate_scale_index_msk)
1381 rate_scale_index_msk = rate_mask;
1383 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1384 index = IWL_INVALID_VALUE;
1385 update_lq = 1;
1387 /* get the lowest available rate */
1388 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1389 if ((1 << i) & rate_scale_index_msk)
1390 index = i;
1393 if (index == IWL_INVALID_VALUE) {
1394 IWL_WARNING("Can not find a suitable rate\n");
1395 return;
1399 if (!tbl->expected_tpt)
1400 rs_get_expected_tpt_table(lq_data, tbl);
1402 window = &(tbl->win[index]);
1404 fail_count = window->counter - window->success_counter;
1405 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1406 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1407 || (tbl->expected_tpt == NULL)) {
1408 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1409 "for index %d\n",
1410 window->success_counter, window->counter, index);
1411 window->average_tpt = IWL_INVALID_VALUE;
1412 rs_stay_in_table(lq_data);
1413 if (update_lq) {
1414 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1415 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1416 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1418 goto out;
1420 } else
1421 window->average_tpt = ((window->success_ratio *
1422 tbl->expected_tpt[index] + 64) / 128);
1424 if (lq_data->search_better_tbl) {
1425 int success_limit = IWL_RATE_SCALE_SWITCH;
1427 if ((window->success_ratio > success_limit) ||
1428 (window->average_tpt > lq_data->last_tpt)) {
1429 if (!is_legacy(tbl->lq_type)) {
1430 IWL_DEBUG_HT("LQ: we are switching to HT"
1431 " rate suc %d current tpt %d"
1432 " old tpt %d\n",
1433 window->success_ratio,
1434 window->average_tpt,
1435 lq_data->last_tpt);
1436 lq_data->enable_counter = 1;
1438 lq_data->active_tbl = active_tbl;
1439 current_tpt = window->average_tpt;
1440 } else {
1441 tbl->lq_type = LQ_NONE;
1442 active_tbl = lq_data->active_tbl;
1443 tbl = &(lq_data->lq_info[active_tbl]);
1445 index = iwl_rate_index_from_plcp(
1446 tbl->current_rate.rate_n_flags);
1448 update_lq = 1;
1449 current_tpt = lq_data->last_tpt;
1450 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1452 lq_data->search_better_tbl = 0;
1453 done_search = 1;
1454 goto lq_update;
1457 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1458 tbl->lq_type);
1459 low = high_low & 0xff;
1460 high = (high_low >> 8) & 0xff;
1462 current_tpt = window->average_tpt;
1464 if (low != IWL_RATE_INVALID)
1465 low_tpt = tbl->win[low].average_tpt;
1467 if (high != IWL_RATE_INVALID)
1468 high_tpt = tbl->win[high].average_tpt;
1471 scale_action = 1;
1473 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1474 (current_tpt == 0)) {
1475 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1476 scale_action = -1;
1477 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1478 (high_tpt == IWL_INVALID_VALUE))
1479 scale_action = 1;
1480 else if ((low_tpt != IWL_INVALID_VALUE) &&
1481 (high_tpt != IWL_INVALID_VALUE) &&
1482 (low_tpt < current_tpt) &&
1483 (high_tpt < current_tpt))
1484 scale_action = 0;
1485 else {
1486 if (high_tpt != IWL_INVALID_VALUE) {
1487 if (high_tpt > current_tpt)
1488 scale_action = 1;
1489 else {
1490 IWL_DEBUG_RATE
1491 ("decrease rate because of high tpt\n");
1492 scale_action = -1;
1494 } else if (low_tpt != IWL_INVALID_VALUE) {
1495 if (low_tpt > current_tpt) {
1496 IWL_DEBUG_RATE
1497 ("decrease rate because of low tpt\n");
1498 scale_action = -1;
1499 } else
1500 scale_action = 1;
1504 if (scale_action == -1) {
1505 if ((low != IWL_RATE_INVALID) &&
1506 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1507 (current_tpt > (100 * tbl->expected_tpt[low]))))
1508 scale_action = 0;
1509 } else if ((scale_action == 1) &&
1510 (window->success_ratio < IWL_RATE_INCREASE_TH))
1511 scale_action = 0;
1513 switch (scale_action) {
1514 case -1:
1515 if (low != IWL_RATE_INVALID) {
1516 update_lq = 1;
1517 index = low;
1519 break;
1520 case 1:
1521 if (high != IWL_RATE_INVALID) {
1522 update_lq = 1;
1523 index = high;
1526 break;
1527 case 0:
1528 default:
1529 break;
1532 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1533 "high %d type %d\n",
1534 index, scale_action, low, high, tbl->lq_type);
1536 lq_update:
1537 if (update_lq) {
1538 rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
1539 rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
1540 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1542 rs_stay_in_table(lq_data);
1544 if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
1545 lq_data->last_tpt = current_tpt;
1547 if (is_legacy(tbl->lq_type))
1548 rs_move_legacy_other(priv, lq_data, index);
1549 else if (is_siso(tbl->lq_type))
1550 rs_move_siso_to_other(priv, lq_data, index);
1551 else
1552 rs_move_mimo_to_other(priv, lq_data, index);
1554 if (lq_data->search_better_tbl) {
1555 tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
1556 for (i = 0; i < IWL_RATE_COUNT; i++)
1557 rs_rate_scale_clear_window(&(tbl->win[i]));
1559 index = iwl_rate_index_from_plcp(
1560 tbl->current_rate.rate_n_flags);
1562 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
1563 tbl->current_rate.rate_n_flags, index);
1564 rs_fill_link_cmd(lq_data, &tbl->current_rate,
1565 &lq_data->lq);
1566 rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
1568 tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
1570 if (is_legacy(tbl1->lq_type) &&
1571 #ifdef CONFIG_IWLWIFI_HT
1572 !priv->current_assoc_ht.is_ht &&
1573 #endif
1574 (lq_data->action_counter >= 1)) {
1575 lq_data->action_counter = 0;
1576 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
1577 rs_set_stay_in_table(1, lq_data);
1580 if (lq_data->enable_counter &&
1581 (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
1582 #ifdef CONFIG_IWLWIFI_HT_AGG
1583 if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
1584 (priv->lq_mngr.agg_ctrl.auto_agg)) {
1585 priv->lq_mngr.agg_ctrl.tid_retry =
1586 TID_ALL_SPECIFIED;
1587 schedule_work(&priv->agg_work);
1589 #endif /*CONFIG_IWLWIFI_HT_AGG */
1590 lq_data->action_counter = 0;
1591 rs_set_stay_in_table(0, lq_data);
1593 } else {
1594 if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
1595 lq_data->flush_timer = jiffies;
1598 out:
1599 rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
1600 i = index;
1601 sta->last_txrate = i;
1603 /* sta->txrate is an index to A mode rates which start
1604 * at IWL_FIRST_OFDM_RATE
1606 if (lq_data->phymode == (u8) MODE_IEEE80211A)
1607 sta->txrate = i - IWL_FIRST_OFDM_RATE;
1608 else
1609 sta->txrate = i;
1611 return;
1615 static void rs_initialize_lq(struct iwl_priv *priv,
1616 struct sta_info *sta)
1618 int i;
1619 struct iwl_rate_scale_priv *lq;
1620 struct iwl_scale_tbl_info *tbl;
1621 u8 active_tbl = 0;
1622 int rate_idx;
1623 u8 use_green = rs_use_green(priv);
1624 struct iwl_rate mcs_rate;
1626 if (!sta || !sta->rate_ctrl_priv)
1627 goto out;
1629 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1630 i = sta->last_txrate;
1632 if ((lq->lq.sta_id == 0xff) &&
1633 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
1634 goto out;
1636 if (!lq->search_better_tbl)
1637 active_tbl = lq->active_tbl;
1638 else
1639 active_tbl = 1 - lq->active_tbl;
1641 tbl = &(lq->lq_info[active_tbl]);
1643 if ((i < 0) || (i >= IWL_RATE_COUNT))
1644 i = 0;
1646 mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
1647 mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
1648 mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
1650 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
1651 mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
1653 tbl->antenna_type = ANT_AUX;
1654 rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
1655 if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
1656 rs_toggle_antenna(&mcs_rate, tbl);
1658 rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
1659 tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
1660 rs_get_expected_tpt_table(lq, tbl);
1661 rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
1662 rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
1663 out:
1664 return;
1667 static void rs_get_rate(void *priv_rate, struct net_device *dev,
1668 struct ieee80211_hw_mode *mode, struct sk_buff *skb,
1669 struct rate_selection *sel)
1672 int i;
1673 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1674 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1675 struct sta_info *sta;
1676 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1677 struct iwl_rate_scale_priv *lq;
1679 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
1681 sta = sta_info_get(local, hdr->addr1);
1683 if (!sta || !sta->rate_ctrl_priv) {
1684 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
1685 if (sta)
1686 sta_info_put(sta);
1687 return;
1690 lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
1691 i = sta->last_txrate;
1693 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
1694 u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
1695 DECLARE_MAC_BUF(mac);
1697 if (sta_id == IWL_INVALID_STATION) {
1698 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1699 print_mac(mac, hdr->addr1));
1700 sta_id = iwl_add_station(priv,
1701 hdr->addr1, 0, CMD_ASYNC);
1703 if ((sta_id != IWL_INVALID_STATION)) {
1704 lq->lq.sta_id = sta_id;
1705 lq->lq.rs_table[0].rate_n_flags = 0;
1706 lq->ibss_sta_added = 1;
1707 rs_initialize_lq(priv, sta);
1709 if (!lq->ibss_sta_added)
1710 goto done;
1713 done:
1714 if ((i < 0) || (i > IWL_RATE_COUNT)) {
1715 sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
1716 return;
1718 sta_info_put(sta);
1720 sel->rate = &priv->ieee_rates[i];
1723 static void *rs_alloc_sta(void *priv, gfp_t gfp)
1725 struct iwl_rate_scale_priv *crl;
1726 int i, j;
1728 IWL_DEBUG_RATE("create station rate scale window\n");
1730 crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
1732 if (crl == NULL)
1733 return NULL;
1734 crl->lq.sta_id = 0xff;
1737 for (j = 0; j < LQ_SIZE; j++)
1738 for (i = 0; i < IWL_RATE_COUNT; i++)
1739 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1741 return crl;
1744 static void rs_rate_init(void *priv_rate, void *priv_sta,
1745 struct ieee80211_local *local,
1746 struct sta_info *sta)
1748 int i, j;
1749 struct ieee80211_hw_mode *mode = local->oper_hw_mode;
1750 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
1751 struct iwl_rate_scale_priv *crl = priv_sta;
1753 crl->flush_timer = 0;
1754 crl->supp_rates = sta->supp_rates;
1755 sta->txrate = 3;
1756 for (j = 0; j < LQ_SIZE; j++)
1757 for (i = 0; i < IWL_RATE_COUNT; i++)
1758 rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
1760 IWL_DEBUG_RATE("rate scale global init\n");
1761 /* TODO: what is a good starting rate for STA? About middle? Maybe not
1762 * the lowest or the highest rate.. Could consider using RSSI from
1763 * previous packets? Need to have IEEE 802.1X auth succeed immediately
1764 * after assoc.. */
1766 crl->ibss_sta_added = 0;
1767 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1768 u8 sta_id = iwl_hw_find_station(priv, sta->addr);
1769 DECLARE_MAC_BUF(mac);
1771 /* for IBSS the call are from tasklet */
1772 IWL_DEBUG_HT("LQ: ADD station %s\n",
1773 print_mac(mac, sta->addr));
1775 if (sta_id == IWL_INVALID_STATION) {
1776 IWL_DEBUG_RATE("LQ: ADD station %s\n",
1777 print_mac(mac, sta->addr));
1778 sta_id = iwl_add_station(priv,
1779 sta->addr, 0, CMD_ASYNC);
1781 if ((sta_id != IWL_INVALID_STATION)) {
1782 crl->lq.sta_id = sta_id;
1783 crl->lq.rs_table[0].rate_n_flags = 0;
1785 /* FIXME: this is w/a remove it later */
1786 priv->assoc_station_added = 1;
1789 for (i = 0; i < mode->num_rates; i++) {
1790 if ((sta->supp_rates & BIT(i)) &&
1791 (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
1792 sta->txrate = i;
1794 sta->last_txrate = sta->txrate;
1795 /* For MODE_IEEE80211A mode cck rate are at end
1796 * rate table
1798 if (local->hw.conf.phymode == MODE_IEEE80211A)
1799 sta->last_txrate += IWL_FIRST_OFDM_RATE;
1801 crl->is_dup = priv->is_dup;
1802 crl->valid_antenna = priv->valid_antenna;
1803 crl->antenna = priv->antenna;
1804 crl->is_green = rs_use_green(priv);
1805 crl->active_rate = priv->active_rate;
1806 crl->active_rate &= ~(0x1000);
1807 crl->active_rate_basic = priv->active_rate_basic;
1808 crl->phymode = priv->phymode;
1809 #ifdef CONFIG_IWLWIFI_HT
1810 crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
1811 crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
1812 crl->active_siso_rate &= ~((u16)0x2);
1813 crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
1815 crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
1816 crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
1817 crl->active_mimo_rate &= ~((u16)0x2);
1818 crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
1819 IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
1820 crl->active_mimo_rate);
1821 #endif /*CONFIG_IWLWIFI_HT*/
1822 #ifdef CONFIG_MAC80211_DEBUGFS
1823 crl->drv = priv;
1824 #endif
1826 if (priv->assoc_station_added)
1827 priv->lq_mngr.lq_ready = 1;
1829 rs_initialize_lq(priv, sta);
1832 static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
1833 struct iwl_rate *tx_mcs,
1834 struct iwl_link_quality_cmd *lq_cmd)
1836 int index = 0;
1837 int rate_idx;
1838 int repeat_rate = 0;
1839 u8 ant_toggle_count = 0;
1840 u8 use_ht_possible = 1;
1841 struct iwl_rate new_rate;
1842 struct iwl_scale_tbl_info tbl_type = { 0 };
1844 rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
1846 rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
1847 &tbl_type, &rate_idx);
1849 if (is_legacy(tbl_type.lq_type)) {
1850 ant_toggle_count = 1;
1851 repeat_rate = IWL_NUMBER_TRY;
1852 } else
1853 repeat_rate = IWL_HT_NUMBER_TRY;
1855 lq_cmd->general_params.mimo_delimiter =
1856 is_mimo(tbl_type.lq_type) ? 1 : 0;
1857 lq_cmd->rs_table[index].rate_n_flags =
1858 cpu_to_le32(tx_mcs->rate_n_flags);
1859 new_rate.rate_n_flags = tx_mcs->rate_n_flags;
1861 if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
1862 lq_cmd->general_params.single_stream_ant_msk = 1;
1863 else
1864 lq_cmd->general_params.single_stream_ant_msk = 2;
1866 index++;
1867 repeat_rate--;
1869 while (index < LINK_QUAL_MAX_RETRY_NUM) {
1870 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
1871 if (is_legacy(tbl_type.lq_type)) {
1872 if (ant_toggle_count <
1873 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1874 ant_toggle_count++;
1875 else {
1876 rs_toggle_antenna(&new_rate, &tbl_type);
1877 ant_toggle_count = 1;
1881 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
1882 lq_cmd->rs_table[index].rate_n_flags =
1883 cpu_to_le32(new_rate.rate_n_flags);
1884 repeat_rate--;
1885 index++;
1888 rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
1889 &rate_idx);
1891 if (is_mimo(tbl_type.lq_type))
1892 lq_cmd->general_params.mimo_delimiter = index;
1894 rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
1895 use_ht_possible, &new_rate);
1897 if (is_legacy(tbl_type.lq_type)) {
1898 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
1899 ant_toggle_count++;
1900 else {
1901 rs_toggle_antenna(&new_rate, &tbl_type);
1902 ant_toggle_count = 1;
1904 repeat_rate = IWL_NUMBER_TRY;
1905 } else
1906 repeat_rate = IWL_HT_NUMBER_TRY;
1908 use_ht_possible = 0;
1910 rs_dbgfs_set_mcs(lq_data, &new_rate, index);
1911 lq_cmd->rs_table[index].rate_n_flags =
1912 cpu_to_le32(new_rate.rate_n_flags);
1914 index++;
1915 repeat_rate--;
1918 lq_cmd->general_params.dual_stream_ant_msk = 3;
1919 lq_cmd->agg_params.agg_dis_start_th = 3;
1920 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
1923 static void *rs_alloc(struct ieee80211_local *local)
1925 return local->hw.priv;
1927 /* rate scale requires free function to be implemented */
1928 static void rs_free(void *priv_rate)
1930 return;
1933 static void rs_clear(void *priv_rate)
1935 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
1937 IWL_DEBUG_RATE("enter\n");
1939 priv->lq_mngr.lq_ready = 0;
1940 #ifdef CONFIG_IWLWIFI_HT
1941 #ifdef CONFIG_IWLWIFI_HT_AGG
1942 if (priv->lq_mngr.agg_ctrl.granted_ba)
1943 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
1944 #endif /*CONFIG_IWLWIFI_HT_AGG */
1945 #endif /* CONFIG_IWLWIFI_HT */
1947 IWL_DEBUG_RATE("leave\n");
1950 static void rs_free_sta(void *priv, void *priv_sta)
1952 struct iwl_rate_scale_priv *rs_priv = priv_sta;
1954 IWL_DEBUG_RATE("enter\n");
1955 kfree(rs_priv);
1956 IWL_DEBUG_RATE("leave\n");
1960 #ifdef CONFIG_MAC80211_DEBUGFS
1961 static int open_file_generic(struct inode *inode, struct file *file)
1963 file->private_data = inode->i_private;
1964 return 0;
1966 static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
1967 struct iwl_rate *mcs, int index)
1969 u32 base_rate;
1971 if (rs_priv->phymode == (u8) MODE_IEEE80211A)
1972 base_rate = 0x800D;
1973 else
1974 base_rate = 0x820A;
1976 if (rs_priv->dbg_fixed.rate_n_flags) {
1977 if (index < 12)
1978 mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
1979 else
1980 mcs->rate_n_flags = base_rate;
1981 IWL_DEBUG_RATE("Fixed rate ON\n");
1982 return;
1985 IWL_DEBUG_RATE("Fixed rate OFF\n");
1988 static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
1989 const char __user *user_buf, size_t count, loff_t *ppos)
1991 struct iwl_rate_scale_priv *rs_priv = file->private_data;
1992 char buf[64];
1993 int buf_size;
1994 u32 parsed_rate;
1996 memset(buf, 0, sizeof(buf));
1997 buf_size = min(count, sizeof(buf) - 1);
1998 if (copy_from_user(buf, user_buf, buf_size))
1999 return -EFAULT;
2001 if (sscanf(buf, "%x", &parsed_rate) == 1)
2002 rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
2003 else
2004 rs_priv->dbg_fixed.rate_n_flags = 0;
2006 rs_priv->active_rate = 0x0FFF;
2007 rs_priv->active_siso_rate = 0x1FD0;
2008 rs_priv->active_mimo_rate = 0x1FD0;
2010 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
2011 rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
2013 if (rs_priv->dbg_fixed.rate_n_flags) {
2014 rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
2015 rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
2018 return count;
2021 static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2022 char __user *user_buf, size_t count, loff_t *ppos)
2024 char buff[1024];
2025 int desc = 0;
2026 int i = 0;
2028 struct iwl_rate_scale_priv *rs_priv = file->private_data;
2030 desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
2031 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2032 rs_priv->total_failed, rs_priv->total_success,
2033 rs_priv->active_rate);
2034 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2035 rs_priv->dbg_fixed.rate_n_flags);
2036 desc += sprintf(buff+desc, "general:"
2037 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2038 rs_priv->lq.general_params.flags,
2039 rs_priv->lq.general_params.mimo_delimiter,
2040 rs_priv->lq.general_params.single_stream_ant_msk,
2041 rs_priv->lq.general_params.dual_stream_ant_msk);
2043 desc += sprintf(buff+desc, "agg:"
2044 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2045 le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
2046 rs_priv->lq.agg_params.agg_dis_start_th,
2047 rs_priv->lq.agg_params.agg_frame_cnt_limit);
2049 desc += sprintf(buff+desc,
2050 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2051 rs_priv->lq.general_params.start_rate_index[0],
2052 rs_priv->lq.general_params.start_rate_index[1],
2053 rs_priv->lq.general_params.start_rate_index[2],
2054 rs_priv->lq.general_params.start_rate_index[3]);
2057 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2058 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
2059 i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
2061 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2064 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2065 .write = rs_sta_dbgfs_scale_table_write,
2066 .read = rs_sta_dbgfs_scale_table_read,
2067 .open = open_file_generic,
2069 static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2070 char __user *user_buf, size_t count, loff_t *ppos)
2072 char buff[1024];
2073 int desc = 0;
2074 int i, j;
2076 struct iwl_rate_scale_priv *rs_priv = file->private_data;
2077 for (i = 0; i < LQ_SIZE; i++) {
2078 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2079 "rate=0x%X\n",
2080 rs_priv->active_tbl == i?"*":"x",
2081 rs_priv->lq_info[i].lq_type,
2082 rs_priv->lq_info[i].is_SGI,
2083 rs_priv->lq_info[i].is_fat,
2084 rs_priv->lq_info[i].is_dup,
2085 rs_priv->lq_info[i].current_rate.rate_n_flags);
2086 for (j = 0; j < IWL_RATE_COUNT; j++) {
2087 desc += sprintf(buff+desc,
2088 "counter=%d success=%d %%=%d\n",
2089 rs_priv->lq_info[i].win[j].counter,
2090 rs_priv->lq_info[i].win[j].success_counter,
2091 rs_priv->lq_info[i].win[j].success_ratio);
2094 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2097 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2098 .read = rs_sta_dbgfs_stats_table_read,
2099 .open = open_file_generic,
2102 static void rs_add_debugfs(void *priv, void *priv_sta,
2103 struct dentry *dir)
2105 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2106 rs_priv->rs_sta_dbgfs_scale_table_file =
2107 debugfs_create_file("rate_scale_table", 0600, dir,
2108 rs_priv, &rs_sta_dbgfs_scale_table_ops);
2109 rs_priv->rs_sta_dbgfs_stats_table_file =
2110 debugfs_create_file("rate_stats_table", 0600, dir,
2111 rs_priv, &rs_sta_dbgfs_stats_table_ops);
2114 static void rs_remove_debugfs(void *priv, void *priv_sta)
2116 struct iwl_rate_scale_priv *rs_priv = priv_sta;
2117 debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
2118 debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
2120 #endif
2122 static struct rate_control_ops rs_ops = {
2123 .module = NULL,
2124 .name = RS_NAME,
2125 .tx_status = rs_tx_status,
2126 .get_rate = rs_get_rate,
2127 .rate_init = rs_rate_init,
2128 .clear = rs_clear,
2129 .alloc = rs_alloc,
2130 .free = rs_free,
2131 .alloc_sta = rs_alloc_sta,
2132 .free_sta = rs_free_sta,
2133 #ifdef CONFIG_MAC80211_DEBUGFS
2134 .add_sta_debugfs = rs_add_debugfs,
2135 .remove_sta_debugfs = rs_remove_debugfs,
2136 #endif
2139 int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
2141 struct ieee80211_local *local = hw_to_local(hw);
2142 struct iwl_priv *priv = hw->priv;
2143 struct iwl_rate_scale_priv *rs_priv;
2144 struct sta_info *sta;
2145 int count = 0, i;
2146 u32 samples = 0, success = 0, good = 0;
2147 unsigned long now = jiffies;
2148 u32 max_time = 0;
2149 u8 lq_type, antenna;
2151 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2152 if (!sta || !sta->rate_ctrl_priv) {
2153 if (sta) {
2154 sta_info_put(sta);
2155 IWL_DEBUG_RATE("leave - no private rate data!\n");
2156 } else
2157 IWL_DEBUG_RATE("leave - no station!\n");
2158 return sprintf(buf, "station %d not found\n", sta_id);
2161 rs_priv = (void *)sta->rate_ctrl_priv;
2163 lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
2164 antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
2166 if (is_legacy(lq_type))
2167 i = IWL_RATE_54M_INDEX;
2168 else
2169 i = IWL_RATE_60M_INDEX;
2170 while (1) {
2171 u64 mask;
2172 int j;
2173 int active = rs_priv->active_tbl;
2175 count +=
2176 sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
2178 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2179 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
2180 buf[count++] =
2181 (rs_priv->lq_info[active].win[i].data & mask)
2182 ? '1' : '0';
2184 samples += rs_priv->lq_info[active].win[i].counter;
2185 good += rs_priv->lq_info[active].win[i].success_counter;
2186 success += rs_priv->lq_info[active].win[i].success_counter *
2187 iwl_rates[i].ieee;
2189 if (rs_priv->lq_info[active].win[i].stamp) {
2190 int delta =
2191 jiffies_to_msecs(now -
2192 rs_priv->lq_info[active].win[i].stamp);
2194 if (delta > max_time)
2195 max_time = delta;
2197 count += sprintf(&buf[count], "%5dms\n", delta);
2198 } else
2199 buf[count++] = '\n';
2201 j = iwl_get_prev_ieee_rate(i);
2202 if (j == i)
2203 break;
2204 i = j;
2207 /* Display the average rate of all samples taken.
2209 * NOTE: We multiple # of samples by 2 since the IEEE measurement
2210 * added from iwl_rates is actually 2X the rate */
2211 if (samples)
2212 count += sprintf(&buf[count],
2213 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2214 "%3d%% success (%d good packets over %d tries)\n",
2215 success / (2 * samples), (success * 5 / samples) % 10,
2216 max_time, good * 100 / samples, good, samples);
2217 else
2218 count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
2219 count += sprintf(&buf[count], "\nrate scale type %d antenna %d "
2220 "active_search %d rate index %d\n", lq_type, antenna,
2221 rs_priv->search_better_tbl, sta->last_txrate);
2223 sta_info_put(sta);
2224 return count;
2227 void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
2229 struct iwl_priv *priv = hw->priv;
2231 priv->lq_mngr.lq_ready = 1;
2234 void iwl_rate_control_register(struct ieee80211_hw *hw)
2236 ieee80211_rate_control_register(&rs_ops);
2239 void iwl_rate_control_unregister(struct ieee80211_hw *hw)
2241 ieee80211_rate_control_unregister(&rs_ops);