nfs: move nfs_file_operations declaration to bottom of file.c (try #2)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlegacy / iwl-4965-rs.c
blob57ebe214e68ca621f218eaa69db0ed42cbed2b74
1 /******************************************************************************
3 * Copyright(c) 2005 - 2011 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 * Intel Linux Wireless <ilw@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/slab.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 "iwl-dev.h"
39 #include "iwl-sta.h"
40 #include "iwl-core.h"
41 #include "iwl-4965.h"
43 #define IWL4965_RS_NAME "iwl-4965-rs"
45 #define NUM_TRY_BEFORE_ANT_TOGGLE 1
46 #define IWL_NUMBER_TRY 1
47 #define IWL_HT_NUMBER_TRY 3
49 #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
50 #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
51 #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53 /* max allowed rate miss before sync LQ cmd */
54 #define IWL_MISSED_RATE_MAX 15
55 /* max time to accum history 2 seconds */
56 #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
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 static const u8 ant_toggle_lookup[] = {
69 /*ANT_NONE -> */ ANT_NONE,
70 /*ANT_A -> */ ANT_B,
71 /*ANT_B -> */ ANT_C,
72 /*ANT_AB -> */ ANT_BC,
73 /*ANT_C -> */ ANT_A,
74 /*ANT_AC -> */ ANT_AB,
75 /*ANT_BC -> */ ANT_AC,
76 /*ANT_ABC -> */ ANT_ABC,
79 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
80 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
81 IWL_RATE_SISO_##s##M_PLCP, \
82 IWL_RATE_MIMO2_##s##M_PLCP,\
83 IWL_RATE_##r##M_IEEE, \
84 IWL_RATE_##ip##M_INDEX, \
85 IWL_RATE_##in##M_INDEX, \
86 IWL_RATE_##rp##M_INDEX, \
87 IWL_RATE_##rn##M_INDEX, \
88 IWL_RATE_##pp##M_INDEX, \
89 IWL_RATE_##np##M_INDEX }
92 * Parameter order:
93 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
95 * If there isn't a valid next or previous rate then INV is used which
96 * maps to IWL_RATE_INVALID
99 const struct iwl_rate_info iwlegacy_rates[IWL_RATE_COUNT] = {
100 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
101 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
102 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
103 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
104 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
105 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
106 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
107 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
108 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
109 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
110 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
111 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
112 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
115 static int iwl4965_hwrate_to_plcp_idx(u32 rate_n_flags)
117 int idx = 0;
119 /* HT rate format */
120 if (rate_n_flags & RATE_MCS_HT_MSK) {
121 idx = (rate_n_flags & 0xff);
123 if (idx >= IWL_RATE_MIMO2_6M_PLCP)
124 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
126 idx += IWL_FIRST_OFDM_RATE;
127 /* skip 9M not supported in ht*/
128 if (idx >= IWL_RATE_9M_INDEX)
129 idx += 1;
130 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
131 return idx;
133 /* legacy rate format, search for match in table */
134 } else {
135 for (idx = 0; idx < ARRAY_SIZE(iwlegacy_rates); idx++)
136 if (iwlegacy_rates[idx].plcp == (rate_n_flags & 0xFF))
137 return idx;
140 return -1;
143 static void iwl4965_rs_rate_scale_perform(struct iwl_priv *priv,
144 struct sk_buff *skb,
145 struct ieee80211_sta *sta,
146 struct iwl_lq_sta *lq_sta);
147 static void iwl4965_rs_fill_link_cmd(struct iwl_priv *priv,
148 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
149 static void iwl4965_rs_stay_in_table(struct iwl_lq_sta *lq_sta,
150 bool force_search);
152 #ifdef CONFIG_MAC80211_DEBUGFS
153 static void iwl4965_rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
154 u32 *rate_n_flags, int index);
155 #else
156 static void iwl4965_rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
157 u32 *rate_n_flags, int index)
159 #endif
162 * The following tables contain the expected throughput metrics for all rates
164 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
166 * where invalid entries are zeros.
168 * CCK rates are only valid in legacy table and will only be used in G
169 * (2.4 GHz) band.
172 static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
173 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
176 static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
177 {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
178 {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
179 {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
180 {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
183 static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
184 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
185 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
186 {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
187 {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
190 static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
191 {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
192 {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
193 {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
194 {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
197 static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
198 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
199 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
200 {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
201 {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
204 /* mbps, mcs */
205 static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
206 { "1", "BPSK DSSS"},
207 { "2", "QPSK DSSS"},
208 {"5.5", "BPSK CCK"},
209 { "11", "QPSK CCK"},
210 { "6", "BPSK 1/2"},
211 { "9", "BPSK 1/2"},
212 { "12", "QPSK 1/2"},
213 { "18", "QPSK 3/4"},
214 { "24", "16QAM 1/2"},
215 { "36", "16QAM 3/4"},
216 { "48", "64QAM 2/3"},
217 { "54", "64QAM 3/4"},
218 { "60", "64QAM 5/6"},
221 #define MCS_INDEX_PER_STREAM (8)
223 static inline u8 iwl4965_rs_extract_rate(u32 rate_n_flags)
225 return (u8)(rate_n_flags & 0xFF);
228 static void
229 iwl4965_rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
231 window->data = 0;
232 window->success_counter = 0;
233 window->success_ratio = IWL_INVALID_VALUE;
234 window->counter = 0;
235 window->average_tpt = IWL_INVALID_VALUE;
236 window->stamp = 0;
239 static inline u8 iwl4965_rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
241 return (ant_type & valid_antenna) == ant_type;
245 * removes the old data from the statistics. All data that is older than
246 * TID_MAX_TIME_DIFF, will be deleted.
248 static void
249 iwl4965_rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
251 /* The oldest age we want to keep */
252 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
254 while (tl->queue_count &&
255 (tl->time_stamp < oldest_time)) {
256 tl->total -= tl->packet_count[tl->head];
257 tl->packet_count[tl->head] = 0;
258 tl->time_stamp += TID_QUEUE_CELL_SPACING;
259 tl->queue_count--;
260 tl->head++;
261 if (tl->head >= TID_QUEUE_MAX_SIZE)
262 tl->head = 0;
267 * increment traffic load value for tid and also remove
268 * any old values if passed the certain time period
270 static u8 iwl4965_rs_tl_add_packet(struct iwl_lq_sta *lq_data,
271 struct ieee80211_hdr *hdr)
273 u32 curr_time = jiffies_to_msecs(jiffies);
274 u32 time_diff;
275 s32 index;
276 struct iwl_traffic_load *tl = NULL;
277 u8 tid;
279 if (ieee80211_is_data_qos(hdr->frame_control)) {
280 u8 *qc = ieee80211_get_qos_ctl(hdr);
281 tid = qc[0] & 0xf;
282 } else
283 return MAX_TID_COUNT;
285 if (unlikely(tid >= TID_MAX_LOAD_COUNT))
286 return MAX_TID_COUNT;
288 tl = &lq_data->load[tid];
290 curr_time -= curr_time % TID_ROUND_VALUE;
292 /* Happens only for the first packet. Initialize the data */
293 if (!(tl->queue_count)) {
294 tl->total = 1;
295 tl->time_stamp = curr_time;
296 tl->queue_count = 1;
297 tl->head = 0;
298 tl->packet_count[0] = 1;
299 return MAX_TID_COUNT;
302 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
303 index = time_diff / TID_QUEUE_CELL_SPACING;
305 /* The history is too long: remove data that is older than */
306 /* TID_MAX_TIME_DIFF */
307 if (index >= TID_QUEUE_MAX_SIZE)
308 iwl4965_rs_tl_rm_old_stats(tl, curr_time);
310 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
311 tl->packet_count[index] = tl->packet_count[index] + 1;
312 tl->total = tl->total + 1;
314 if ((index + 1) > tl->queue_count)
315 tl->queue_count = index + 1;
317 return tid;
321 get the traffic load value for tid
323 static u32 iwl4965_rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
325 u32 curr_time = jiffies_to_msecs(jiffies);
326 u32 time_diff;
327 s32 index;
328 struct iwl_traffic_load *tl = NULL;
330 if (tid >= TID_MAX_LOAD_COUNT)
331 return 0;
333 tl = &(lq_data->load[tid]);
335 curr_time -= curr_time % TID_ROUND_VALUE;
337 if (!(tl->queue_count))
338 return 0;
340 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
341 index = time_diff / TID_QUEUE_CELL_SPACING;
343 /* The history is too long: remove data that is older than */
344 /* TID_MAX_TIME_DIFF */
345 if (index >= TID_QUEUE_MAX_SIZE)
346 iwl4965_rs_tl_rm_old_stats(tl, curr_time);
348 return tl->total;
351 static int iwl4965_rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
352 struct iwl_lq_sta *lq_data, u8 tid,
353 struct ieee80211_sta *sta)
355 int ret = -EAGAIN;
356 u32 load;
358 load = iwl4965_rs_tl_get_load(lq_data, tid);
360 if (load > IWL_AGG_LOAD_THRESHOLD) {
361 IWL_DEBUG_HT(priv, "Starting Tx agg: STA: %pM tid: %d\n",
362 sta->addr, tid);
363 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
364 if (ret == -EAGAIN) {
366 * driver and mac80211 is out of sync
367 * this might be cause by reloading firmware
368 * stop the tx ba session here
370 IWL_ERR(priv, "Fail start Tx agg on tid: %d\n",
371 tid);
372 ieee80211_stop_tx_ba_session(sta, tid);
374 } else {
375 IWL_ERR(priv, "Aggregation not enabled for tid %d "
376 "because load = %u\n", tid, load);
378 return ret;
381 static void iwl4965_rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
382 struct iwl_lq_sta *lq_data,
383 struct ieee80211_sta *sta)
385 if (tid < TID_MAX_LOAD_COUNT)
386 iwl4965_rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
387 else
388 IWL_ERR(priv, "tid exceeds max load count: %d/%d\n",
389 tid, TID_MAX_LOAD_COUNT);
392 static inline int iwl4965_get_iwl4965_num_of_ant_from_rate(u32 rate_n_flags)
394 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
395 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
396 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
400 * Static function to get the expected throughput from an iwl_scale_tbl_info
401 * that wraps a NULL pointer check
403 static s32
404 iwl4965_get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
406 if (tbl->expected_tpt)
407 return tbl->expected_tpt[rs_index];
408 return 0;
412 * iwl4965_rs_collect_tx_data - Update the success/failure sliding window
414 * We keep a sliding window of the last 62 packets transmitted
415 * at this rate. window->data contains the bitmask of successful
416 * packets.
418 static int iwl4965_rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
419 int scale_index, int attempts, int successes)
421 struct iwl_rate_scale_data *window = NULL;
422 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
423 s32 fail_count, tpt;
425 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
426 return -EINVAL;
428 /* Select window for current tx bit rate */
429 window = &(tbl->win[scale_index]);
431 /* Get expected throughput */
432 tpt = iwl4965_get_expected_tpt(tbl, scale_index);
435 * Keep track of only the latest 62 tx frame attempts in this rate's
436 * history window; anything older isn't really relevant any more.
437 * If we have filled up the sliding window, drop the oldest attempt;
438 * if the oldest attempt (highest bit in bitmap) shows "success",
439 * subtract "1" from the success counter (this is the main reason
440 * we keep these bitmaps!).
442 while (attempts > 0) {
443 if (window->counter >= IWL_RATE_MAX_WINDOW) {
445 /* remove earliest */
446 window->counter = IWL_RATE_MAX_WINDOW - 1;
448 if (window->data & mask) {
449 window->data &= ~mask;
450 window->success_counter--;
454 /* Increment frames-attempted counter */
455 window->counter++;
457 /* Shift bitmap by one frame to throw away oldest history */
458 window->data <<= 1;
460 /* Mark the most recent #successes attempts as successful */
461 if (successes > 0) {
462 window->success_counter++;
463 window->data |= 0x1;
464 successes--;
467 attempts--;
470 /* Calculate current success ratio, avoid divide-by-0! */
471 if (window->counter > 0)
472 window->success_ratio = 128 * (100 * window->success_counter)
473 / window->counter;
474 else
475 window->success_ratio = IWL_INVALID_VALUE;
477 fail_count = window->counter - window->success_counter;
479 /* Calculate average throughput, if we have enough history. */
480 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
481 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
482 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
483 else
484 window->average_tpt = IWL_INVALID_VALUE;
486 /* Tag this window as having been updated */
487 window->stamp = jiffies;
489 return 0;
493 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
495 static u32 iwl4965_rate_n_flags_from_tbl(struct iwl_priv *priv,
496 struct iwl_scale_tbl_info *tbl,
497 int index, u8 use_green)
499 u32 rate_n_flags = 0;
501 if (is_legacy(tbl->lq_type)) {
502 rate_n_flags = iwlegacy_rates[index].plcp;
503 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
504 rate_n_flags |= RATE_MCS_CCK_MSK;
506 } else if (is_Ht(tbl->lq_type)) {
507 if (index > IWL_LAST_OFDM_RATE) {
508 IWL_ERR(priv, "Invalid HT rate index %d\n", index);
509 index = IWL_LAST_OFDM_RATE;
511 rate_n_flags = RATE_MCS_HT_MSK;
513 if (is_siso(tbl->lq_type))
514 rate_n_flags |= iwlegacy_rates[index].plcp_siso;
515 else
516 rate_n_flags |= iwlegacy_rates[index].plcp_mimo2;
517 } else {
518 IWL_ERR(priv, "Invalid tbl->lq_type %d\n", tbl->lq_type);
521 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
522 RATE_MCS_ANT_ABC_MSK);
524 if (is_Ht(tbl->lq_type)) {
525 if (tbl->is_ht40) {
526 if (tbl->is_dup)
527 rate_n_flags |= RATE_MCS_DUP_MSK;
528 else
529 rate_n_flags |= RATE_MCS_HT40_MSK;
531 if (tbl->is_SGI)
532 rate_n_flags |= RATE_MCS_SGI_MSK;
534 if (use_green) {
535 rate_n_flags |= RATE_MCS_GF_MSK;
536 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
537 rate_n_flags &= ~RATE_MCS_SGI_MSK;
538 IWL_ERR(priv, "GF was set with SGI:SISO\n");
542 return rate_n_flags;
546 * Interpret uCode API's rate_n_flags format,
547 * fill "search" or "active" tx mode table.
549 static int iwl4965_rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
550 enum ieee80211_band band,
551 struct iwl_scale_tbl_info *tbl,
552 int *rate_idx)
554 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
555 u8 iwl4965_num_of_ant = iwl4965_get_iwl4965_num_of_ant_from_rate(rate_n_flags);
556 u8 mcs;
558 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
559 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
561 if (*rate_idx == IWL_RATE_INVALID) {
562 *rate_idx = -1;
563 return -EINVAL;
565 tbl->is_SGI = 0; /* default legacy setup */
566 tbl->is_ht40 = 0;
567 tbl->is_dup = 0;
568 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
569 tbl->lq_type = LQ_NONE;
570 tbl->max_search = IWL_MAX_SEARCH;
572 /* legacy rate format */
573 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
574 if (iwl4965_num_of_ant == 1) {
575 if (band == IEEE80211_BAND_5GHZ)
576 tbl->lq_type = LQ_A;
577 else
578 tbl->lq_type = LQ_G;
580 /* HT rate format */
581 } else {
582 if (rate_n_flags & RATE_MCS_SGI_MSK)
583 tbl->is_SGI = 1;
585 if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
586 (rate_n_flags & RATE_MCS_DUP_MSK))
587 tbl->is_ht40 = 1;
589 if (rate_n_flags & RATE_MCS_DUP_MSK)
590 tbl->is_dup = 1;
592 mcs = iwl4965_rs_extract_rate(rate_n_flags);
594 /* SISO */
595 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
596 if (iwl4965_num_of_ant == 1)
597 tbl->lq_type = LQ_SISO; /*else NONE*/
598 /* MIMO2 */
599 } else {
600 if (iwl4965_num_of_ant == 2)
601 tbl->lq_type = LQ_MIMO2;
604 return 0;
607 /* switch to another antenna/antennas and return 1 */
608 /* if no other valid antenna found, return 0 */
609 static int iwl4965_rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
610 struct iwl_scale_tbl_info *tbl)
612 u8 new_ant_type;
614 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
615 return 0;
617 if (!iwl4965_rs_is_valid_ant(valid_ant, tbl->ant_type))
618 return 0;
620 new_ant_type = ant_toggle_lookup[tbl->ant_type];
622 while ((new_ant_type != tbl->ant_type) &&
623 !iwl4965_rs_is_valid_ant(valid_ant, new_ant_type))
624 new_ant_type = ant_toggle_lookup[new_ant_type];
626 if (new_ant_type == tbl->ant_type)
627 return 0;
629 tbl->ant_type = new_ant_type;
630 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
631 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
632 return 1;
636 * Green-field mode is valid if the station supports it and
637 * there are no non-GF stations present in the BSS.
639 static bool iwl4965_rs_use_green(struct ieee80211_sta *sta)
641 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
642 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
644 return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
645 !(ctx->ht.non_gf_sta_present);
649 * iwl4965_rs_get_supported_rates - get the available rates
651 * if management frame or broadcast frame only return
652 * basic available rates.
655 static u16 iwl4965_rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
656 struct ieee80211_hdr *hdr,
657 enum iwl_table_type rate_type)
659 if (is_legacy(rate_type)) {
660 return lq_sta->active_legacy_rate;
661 } else {
662 if (is_siso(rate_type))
663 return lq_sta->active_siso_rate;
664 else
665 return lq_sta->active_mimo2_rate;
669 static u16
670 iwl4965_rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
671 int rate_type)
673 u8 high = IWL_RATE_INVALID;
674 u8 low = IWL_RATE_INVALID;
676 /* 802.11A or ht walks to the next literal adjacent rate in
677 * the rate table */
678 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
679 int i;
680 u32 mask;
682 /* Find the previous rate that is in the rate mask */
683 i = index - 1;
684 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
685 if (rate_mask & mask) {
686 low = i;
687 break;
691 /* Find the next rate that is in the rate mask */
692 i = index + 1;
693 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
694 if (rate_mask & mask) {
695 high = i;
696 break;
700 return (high << 8) | low;
703 low = index;
704 while (low != IWL_RATE_INVALID) {
705 low = iwlegacy_rates[low].prev_rs;
706 if (low == IWL_RATE_INVALID)
707 break;
708 if (rate_mask & (1 << low))
709 break;
710 IWL_DEBUG_RATE(priv, "Skipping masked lower rate: %d\n", low);
713 high = index;
714 while (high != IWL_RATE_INVALID) {
715 high = iwlegacy_rates[high].next_rs;
716 if (high == IWL_RATE_INVALID)
717 break;
718 if (rate_mask & (1 << high))
719 break;
720 IWL_DEBUG_RATE(priv, "Skipping masked higher rate: %d\n", high);
723 return (high << 8) | low;
726 static u32 iwl4965_rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
727 struct iwl_scale_tbl_info *tbl,
728 u8 scale_index, u8 ht_possible)
730 s32 low;
731 u16 rate_mask;
732 u16 high_low;
733 u8 switch_to_legacy = 0;
734 u8 is_green = lq_sta->is_green;
735 struct iwl_priv *priv = lq_sta->drv;
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)
744 tbl->lq_type = LQ_A;
745 else
746 tbl->lq_type = LQ_G;
748 if (iwl4965_num_of_ant(tbl->ant_type) > 1)
749 tbl->ant_type =
750 iwl4965_first_antenna(priv->hw_params.valid_tx_ant);
752 tbl->is_ht40 = 0;
753 tbl->is_SGI = 0;
754 tbl->max_search = IWL_MAX_SEARCH;
757 rate_mask = iwl4965_rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
759 /* Mask with station rate restriction */
760 if (is_legacy(tbl->lq_type)) {
761 /* supp_rates has no CCK bits in A mode */
762 if (lq_sta->band == IEEE80211_BAND_5GHZ)
763 rate_mask = (u16)(rate_mask &
764 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
765 else
766 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
769 /* If we switched from HT to legacy, check current rate */
770 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
771 low = scale_index;
772 goto out;
775 high_low = iwl4965_rs_get_adjacent_rate(lq_sta->drv,
776 scale_index, rate_mask,
777 tbl->lq_type);
778 low = high_low & 0xff;
780 if (low == IWL_RATE_INVALID)
781 low = scale_index;
783 out:
784 return iwl4965_rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
788 * Simple function to compare two rate scale table types
790 static bool iwl4965_table_type_matches(struct iwl_scale_tbl_info *a,
791 struct iwl_scale_tbl_info *b)
793 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
794 (a->is_SGI == b->is_SGI);
798 * mac80211 sends us Tx status
800 static void
801 iwl4965_rs_tx_status(void *priv_r, struct ieee80211_supported_band *sband,
802 struct ieee80211_sta *sta, void *priv_sta,
803 struct sk_buff *skb)
805 int legacy_success;
806 int retries;
807 int rs_index, mac_index, i;
808 struct iwl_lq_sta *lq_sta = priv_sta;
809 struct iwl_link_quality_cmd *table;
810 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
811 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
812 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
813 enum mac80211_rate_control_flags mac_flags;
814 u32 tx_rate;
815 struct iwl_scale_tbl_info tbl_type;
816 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
817 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
818 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
820 IWL_DEBUG_RATE_LIMIT(priv,
821 "get frame ack response, update rate scale window\n");
823 /* Treat uninitialized rate scaling data same as non-existing. */
824 if (!lq_sta) {
825 IWL_DEBUG_RATE(priv, "Station rate scaling not created yet.\n");
826 return;
827 } else if (!lq_sta->drv) {
828 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
829 return;
832 if (!ieee80211_is_data(hdr->frame_control) ||
833 info->flags & IEEE80211_TX_CTL_NO_ACK)
834 return;
836 /* This packet was aggregated but doesn't carry status info */
837 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
838 !(info->flags & IEEE80211_TX_STAT_AMPDU))
839 return;
842 * Ignore this Tx frame response if its initial rate doesn't match
843 * that of latest Link Quality command. There may be stragglers
844 * from a previous Link Quality command, but we're no longer interested
845 * in those; they're either from the "active" mode while we're trying
846 * to check "search" mode, or a prior "search" mode after we've moved
847 * to a new "search" mode (which might become the new "active" mode).
849 table = &lq_sta->lq;
850 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
851 iwl4965_rs_get_tbl_info_from_mcs(tx_rate,
852 priv->band, &tbl_type, &rs_index);
853 if (priv->band == IEEE80211_BAND_5GHZ)
854 rs_index -= IWL_FIRST_OFDM_RATE;
855 mac_flags = info->status.rates[0].flags;
856 mac_index = info->status.rates[0].idx;
857 /* For HT packets, map MCS to PLCP */
858 if (mac_flags & IEEE80211_TX_RC_MCS) {
859 mac_index &= RATE_MCS_CODE_MSK; /* Remove # of streams */
860 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
861 mac_index++;
863 * mac80211 HT index is always zero-indexed; we need to move
864 * HT OFDM rates after CCK rates in 2.4 GHz band
866 if (priv->band == IEEE80211_BAND_2GHZ)
867 mac_index += IWL_FIRST_OFDM_RATE;
869 /* Here we actually compare this rate to the latest LQ command */
870 if ((mac_index < 0) ||
871 (tbl_type.is_SGI !=
872 !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
873 (tbl_type.is_ht40 !=
874 !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
875 (tbl_type.is_dup !=
876 !!(mac_flags & IEEE80211_TX_RC_DUP_DATA)) ||
877 (tbl_type.ant_type != info->antenna_sel_tx) ||
878 (!!(tx_rate & RATE_MCS_HT_MSK) !=
879 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
880 (!!(tx_rate & RATE_MCS_GF_MSK) !=
881 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
882 (rs_index != mac_index)) {
883 IWL_DEBUG_RATE(priv,
884 "initial rate %d does not match %d (0x%x)\n",
885 mac_index, rs_index, tx_rate);
887 * Since rates mis-match, the last LQ command may have failed.
888 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
889 * ... driver.
891 lq_sta->missed_rate_counter++;
892 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
893 lq_sta->missed_rate_counter = 0;
894 iwl_legacy_send_lq_cmd(priv, ctx, &lq_sta->lq,
895 CMD_ASYNC, false);
897 /* Regardless, ignore this status info for outdated rate */
898 return;
899 } else
900 /* Rate did match, so reset the missed_rate_counter */
901 lq_sta->missed_rate_counter = 0;
903 /* Figure out if rate scale algorithm is in active or search table */
904 if (iwl4965_table_type_matches(&tbl_type,
905 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
906 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
907 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
908 } else if (iwl4965_table_type_matches(&tbl_type,
909 &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
910 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
911 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
912 } else {
913 IWL_DEBUG_RATE(priv,
914 "Neither active nor search matches tx rate\n");
915 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
916 IWL_DEBUG_RATE(priv, "active- lq:%x, ant:%x, SGI:%d\n",
917 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
918 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
919 IWL_DEBUG_RATE(priv, "search- lq:%x, ant:%x, SGI:%d\n",
920 tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
921 IWL_DEBUG_RATE(priv, "actual- lq:%x, ant:%x, SGI:%d\n",
922 tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
924 * no matching table found, let's by-pass the data collection
925 * and continue to perform rate scale to find the rate table
927 iwl4965_rs_stay_in_table(lq_sta, true);
928 goto done;
932 * Updating the frame history depends on whether packets were
933 * aggregated.
935 * For aggregation, all packets were transmitted at the same rate, the
936 * first index into rate scale table.
938 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
939 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
940 iwl4965_rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type,
941 &rs_index);
942 iwl4965_rs_collect_tx_data(curr_tbl, rs_index,
943 info->status.ampdu_len,
944 info->status.ampdu_ack_len);
946 /* Update success/fail counts if not searching for new mode */
947 if (lq_sta->stay_in_tbl) {
948 lq_sta->total_success += info->status.ampdu_ack_len;
949 lq_sta->total_failed += (info->status.ampdu_len -
950 info->status.ampdu_ack_len);
952 } else {
954 * For legacy, update frame history with for each Tx retry.
956 retries = info->status.rates[0].count - 1;
957 /* HW doesn't send more than 15 retries */
958 retries = min(retries, 15);
960 /* The last transmission may have been successful */
961 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
962 /* Collect data for each rate used during failed TX attempts */
963 for (i = 0; i <= retries; ++i) {
964 tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
965 iwl4965_rs_get_tbl_info_from_mcs(tx_rate, priv->band,
966 &tbl_type, &rs_index);
968 * Only collect stats if retried rate is in the same RS
969 * table as active/search.
971 if (iwl4965_table_type_matches(&tbl_type, curr_tbl))
972 tmp_tbl = curr_tbl;
973 else if (iwl4965_table_type_matches(&tbl_type,
974 other_tbl))
975 tmp_tbl = other_tbl;
976 else
977 continue;
978 iwl4965_rs_collect_tx_data(tmp_tbl, rs_index, 1,
979 i < retries ? 0 : legacy_success);
982 /* Update success/fail counts if not searching for new mode */
983 if (lq_sta->stay_in_tbl) {
984 lq_sta->total_success += legacy_success;
985 lq_sta->total_failed += retries + (1 - legacy_success);
988 /* The last TX rate is cached in lq_sta; it's set in if/else above */
989 lq_sta->last_rate_n_flags = tx_rate;
990 done:
991 /* See if there's a better rate or modulation mode to try. */
992 if (sta && sta->supp_rates[sband->band])
993 iwl4965_rs_rate_scale_perform(priv, skb, sta, lq_sta);
997 * Begin a period of staying with a selected modulation mode.
998 * Set "stay_in_tbl" flag to prevent any mode switches.
999 * Set frame tx success limits according to legacy vs. high-throughput,
1000 * and reset overall (spanning all rates) tx success history statistics.
1001 * These control how long we stay using same modulation mode before
1002 * searching for a new mode.
1004 static void iwl4965_rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
1005 struct iwl_lq_sta *lq_sta)
1007 IWL_DEBUG_RATE(priv, "we are staying in the same table\n");
1008 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1009 if (is_legacy) {
1010 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1011 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1012 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1013 } else {
1014 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1015 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1016 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1018 lq_sta->table_count = 0;
1019 lq_sta->total_failed = 0;
1020 lq_sta->total_success = 0;
1021 lq_sta->flush_timer = jiffies;
1022 lq_sta->action_counter = 0;
1026 * Find correct throughput table for given mode of modulation
1028 static void iwl4965_rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1029 struct iwl_scale_tbl_info *tbl)
1031 /* Used to choose among HT tables */
1032 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1034 /* Check for invalid LQ type */
1035 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1036 tbl->expected_tpt = expected_tpt_legacy;
1037 return;
1040 /* Legacy rates have only one table */
1041 if (is_legacy(tbl->lq_type)) {
1042 tbl->expected_tpt = expected_tpt_legacy;
1043 return;
1046 /* Choose among many HT tables depending on number of streams
1047 * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
1048 * status */
1049 if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1050 ht_tbl_pointer = expected_tpt_siso20MHz;
1051 else if (is_siso(tbl->lq_type))
1052 ht_tbl_pointer = expected_tpt_siso40MHz;
1053 else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
1054 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1055 else /* if (is_mimo2(tbl->lq_type)) <-- must be true */
1056 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1058 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1059 tbl->expected_tpt = ht_tbl_pointer[0];
1060 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1061 tbl->expected_tpt = ht_tbl_pointer[1];
1062 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1063 tbl->expected_tpt = ht_tbl_pointer[2];
1064 else /* AGG+SGI */
1065 tbl->expected_tpt = ht_tbl_pointer[3];
1069 * Find starting rate for new "search" high-throughput mode of modulation.
1070 * Goal is to find lowest expected rate (under perfect conditions) that is
1071 * above the current measured throughput of "active" mode, to give new mode
1072 * a fair chance to prove itself without too many challenges.
1074 * This gets called when transitioning to more aggressive modulation
1075 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1076 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1077 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1078 * bit rate will typically need to increase, but not if performance was bad.
1080 static s32 iwl4965_rs_get_best_rate(struct iwl_priv *priv,
1081 struct iwl_lq_sta *lq_sta,
1082 struct iwl_scale_tbl_info *tbl, /* "search" */
1083 u16 rate_mask, s8 index)
1085 /* "active" values */
1086 struct iwl_scale_tbl_info *active_tbl =
1087 &(lq_sta->lq_info[lq_sta->active_tbl]);
1088 s32 active_sr = active_tbl->win[index].success_ratio;
1089 s32 active_tpt = active_tbl->expected_tpt[index];
1091 /* expected "search" throughput */
1092 s32 *tpt_tbl = tbl->expected_tpt;
1094 s32 new_rate, high, low, start_hi;
1095 u16 high_low;
1096 s8 rate = index;
1098 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1100 for (; ;) {
1101 high_low = iwl4965_rs_get_adjacent_rate(priv, rate, rate_mask,
1102 tbl->lq_type);
1104 low = high_low & 0xff;
1105 high = (high_low >> 8) & 0xff;
1108 * Lower the "search" bit rate, to give new "search" mode
1109 * approximately the same throughput as "active" if:
1111 * 1) "Active" mode has been working modestly well (but not
1112 * great), and expected "search" throughput (under perfect
1113 * conditions) at candidate rate is above the actual
1114 * measured "active" throughput (but less than expected
1115 * "active" throughput under perfect conditions).
1116 * OR
1117 * 2) "Active" mode has been working perfectly or very well
1118 * and expected "search" throughput (under perfect
1119 * conditions) at candidate rate is above expected
1120 * "active" throughput (under perfect conditions).
1122 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1123 ((active_sr > IWL_RATE_DECREASE_TH) &&
1124 (active_sr <= IWL_RATE_HIGH_TH) &&
1125 (tpt_tbl[rate] <= active_tpt))) ||
1126 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1127 (tpt_tbl[rate] > active_tpt))) {
1129 /* (2nd or later pass)
1130 * If we've already tried to raise the rate, and are
1131 * now trying to lower it, use the higher rate. */
1132 if (start_hi != IWL_RATE_INVALID) {
1133 new_rate = start_hi;
1134 break;
1137 new_rate = rate;
1139 /* Loop again with lower rate */
1140 if (low != IWL_RATE_INVALID)
1141 rate = low;
1143 /* Lower rate not available, use the original */
1144 else
1145 break;
1147 /* Else try to raise the "search" rate to match "active" */
1148 } else {
1149 /* (2nd or later pass)
1150 * If we've already tried to lower the rate, and are
1151 * now trying to raise it, use the lower rate. */
1152 if (new_rate != IWL_RATE_INVALID)
1153 break;
1155 /* Loop again with higher rate */
1156 else if (high != IWL_RATE_INVALID) {
1157 start_hi = high;
1158 rate = high;
1160 /* Higher rate not available, use the original */
1161 } else {
1162 new_rate = rate;
1163 break;
1168 return new_rate;
1172 * Set up search table for MIMO2
1174 static int iwl4965_rs_switch_to_mimo2(struct iwl_priv *priv,
1175 struct iwl_lq_sta *lq_sta,
1176 struct ieee80211_conf *conf,
1177 struct ieee80211_sta *sta,
1178 struct iwl_scale_tbl_info *tbl, int index)
1180 u16 rate_mask;
1181 s32 rate;
1182 s8 is_green = lq_sta->is_green;
1183 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1184 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
1186 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1187 return -1;
1189 if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
1190 == WLAN_HT_CAP_SM_PS_STATIC)
1191 return -1;
1193 /* Need both Tx chains/antennas to support MIMO */
1194 if (priv->hw_params.tx_chains_num < 2)
1195 return -1;
1197 IWL_DEBUG_RATE(priv, "LQ: try to switch to MIMO2\n");
1199 tbl->lq_type = LQ_MIMO2;
1200 tbl->is_dup = lq_sta->is_dup;
1201 tbl->action = 0;
1202 tbl->max_search = IWL_MAX_SEARCH;
1203 rate_mask = lq_sta->active_mimo2_rate;
1205 if (iwl_legacy_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1206 tbl->is_ht40 = 1;
1207 else
1208 tbl->is_ht40 = 0;
1210 iwl4965_rs_set_expected_tpt_table(lq_sta, tbl);
1212 rate = iwl4965_rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1214 IWL_DEBUG_RATE(priv, "LQ: MIMO2 best rate %d mask %X\n",
1215 rate, rate_mask);
1216 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1217 IWL_DEBUG_RATE(priv,
1218 "Can't switch with index %d rate mask %x\n",
1219 rate, rate_mask);
1220 return -1;
1222 tbl->current_rate = iwl4965_rate_n_flags_from_tbl(priv,
1223 tbl, rate, is_green);
1225 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1226 tbl->current_rate, is_green);
1227 return 0;
1231 * Set up search table for SISO
1233 static int iwl4965_rs_switch_to_siso(struct iwl_priv *priv,
1234 struct iwl_lq_sta *lq_sta,
1235 struct ieee80211_conf *conf,
1236 struct ieee80211_sta *sta,
1237 struct iwl_scale_tbl_info *tbl, int index)
1239 u16 rate_mask;
1240 u8 is_green = lq_sta->is_green;
1241 s32 rate;
1242 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1243 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
1245 if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
1246 return -1;
1248 IWL_DEBUG_RATE(priv, "LQ: try to switch to SISO\n");
1250 tbl->is_dup = lq_sta->is_dup;
1251 tbl->lq_type = LQ_SISO;
1252 tbl->action = 0;
1253 tbl->max_search = IWL_MAX_SEARCH;
1254 rate_mask = lq_sta->active_siso_rate;
1256 if (iwl_legacy_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
1257 tbl->is_ht40 = 1;
1258 else
1259 tbl->is_ht40 = 0;
1261 if (is_green)
1262 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1264 iwl4965_rs_set_expected_tpt_table(lq_sta, tbl);
1265 rate = iwl4965_rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
1267 IWL_DEBUG_RATE(priv, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1268 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1269 IWL_DEBUG_RATE(priv,
1270 "can not switch with index %d rate mask %x\n",
1271 rate, rate_mask);
1272 return -1;
1274 tbl->current_rate = iwl4965_rate_n_flags_from_tbl(priv,
1275 tbl, rate, is_green);
1276 IWL_DEBUG_RATE(priv, "LQ: Switch to new mcs %X index is green %X\n",
1277 tbl->current_rate, is_green);
1278 return 0;
1282 * Try to switch to new modulation mode from legacy
1284 static int iwl4965_rs_move_legacy_other(struct iwl_priv *priv,
1285 struct iwl_lq_sta *lq_sta,
1286 struct ieee80211_conf *conf,
1287 struct ieee80211_sta *sta,
1288 int index)
1290 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1291 struct iwl_scale_tbl_info *search_tbl =
1292 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1293 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1294 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1295 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1296 u8 start_action;
1297 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1298 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1299 int ret = 0;
1300 u8 update_search_tbl_counter = 0;
1302 tbl->action = IWL_LEGACY_SWITCH_SISO;
1304 start_action = tbl->action;
1305 for (; ;) {
1306 lq_sta->action_counter++;
1307 switch (tbl->action) {
1308 case IWL_LEGACY_SWITCH_ANTENNA1:
1309 case IWL_LEGACY_SWITCH_ANTENNA2:
1310 IWL_DEBUG_RATE(priv, "LQ: Legacy toggle Antenna\n");
1312 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1313 tx_chains_num <= 1) ||
1314 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1315 tx_chains_num <= 2))
1316 break;
1318 /* Don't change antenna if success has been great */
1319 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1320 break;
1322 /* Set up search table to try other antenna */
1323 memcpy(search_tbl, tbl, sz);
1325 if (iwl4965_rs_toggle_antenna(valid_tx_ant,
1326 &search_tbl->current_rate, search_tbl)) {
1327 update_search_tbl_counter = 1;
1328 iwl4965_rs_set_expected_tpt_table(lq_sta,
1329 search_tbl);
1330 goto out;
1332 break;
1333 case IWL_LEGACY_SWITCH_SISO:
1334 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to SISO\n");
1336 /* Set up search table to try SISO */
1337 memcpy(search_tbl, tbl, sz);
1338 search_tbl->is_SGI = 0;
1339 ret = iwl4965_rs_switch_to_siso(priv, lq_sta, conf, sta,
1340 search_tbl, index);
1341 if (!ret) {
1342 lq_sta->action_counter = 0;
1343 goto out;
1346 break;
1347 case IWL_LEGACY_SWITCH_MIMO2_AB:
1348 case IWL_LEGACY_SWITCH_MIMO2_AC:
1349 case IWL_LEGACY_SWITCH_MIMO2_BC:
1350 IWL_DEBUG_RATE(priv, "LQ: Legacy switch to MIMO2\n");
1352 /* Set up search table to try MIMO */
1353 memcpy(search_tbl, tbl, sz);
1354 search_tbl->is_SGI = 0;
1356 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1357 search_tbl->ant_type = ANT_AB;
1358 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1359 search_tbl->ant_type = ANT_AC;
1360 else
1361 search_tbl->ant_type = ANT_BC;
1363 if (!iwl4965_rs_is_valid_ant(valid_tx_ant,
1364 search_tbl->ant_type))
1365 break;
1367 ret = iwl4965_rs_switch_to_mimo2(priv, lq_sta,
1368 conf, sta,
1369 search_tbl, index);
1370 if (!ret) {
1371 lq_sta->action_counter = 0;
1372 goto out;
1374 break;
1376 tbl->action++;
1377 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1378 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1380 if (tbl->action == start_action)
1381 break;
1384 search_tbl->lq_type = LQ_NONE;
1385 return 0;
1387 out:
1388 lq_sta->search_better_tbl = 1;
1389 tbl->action++;
1390 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2_BC)
1391 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1392 if (update_search_tbl_counter)
1393 search_tbl->action = tbl->action;
1394 return 0;
1399 * Try to switch to new modulation mode from SISO
1401 static int iwl4965_rs_move_siso_to_other(struct iwl_priv *priv,
1402 struct iwl_lq_sta *lq_sta,
1403 struct ieee80211_conf *conf,
1404 struct ieee80211_sta *sta, int index)
1406 u8 is_green = lq_sta->is_green;
1407 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1408 struct iwl_scale_tbl_info *search_tbl =
1409 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1410 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1411 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1412 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1413 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1414 u8 start_action;
1415 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1416 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1417 u8 update_search_tbl_counter = 0;
1418 int ret;
1420 start_action = tbl->action;
1422 for (;;) {
1423 lq_sta->action_counter++;
1424 switch (tbl->action) {
1425 case IWL_SISO_SWITCH_ANTENNA1:
1426 case IWL_SISO_SWITCH_ANTENNA2:
1427 IWL_DEBUG_RATE(priv, "LQ: SISO toggle Antenna\n");
1428 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1429 tx_chains_num <= 1) ||
1430 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1431 tx_chains_num <= 2))
1432 break;
1434 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1435 break;
1437 memcpy(search_tbl, tbl, sz);
1438 if (iwl4965_rs_toggle_antenna(valid_tx_ant,
1439 &search_tbl->current_rate, search_tbl)) {
1440 update_search_tbl_counter = 1;
1441 goto out;
1443 break;
1444 case IWL_SISO_SWITCH_MIMO2_AB:
1445 case IWL_SISO_SWITCH_MIMO2_AC:
1446 case IWL_SISO_SWITCH_MIMO2_BC:
1447 IWL_DEBUG_RATE(priv, "LQ: SISO switch to MIMO2\n");
1448 memcpy(search_tbl, tbl, sz);
1449 search_tbl->is_SGI = 0;
1451 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1452 search_tbl->ant_type = ANT_AB;
1453 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1454 search_tbl->ant_type = ANT_AC;
1455 else
1456 search_tbl->ant_type = ANT_BC;
1458 if (!iwl4965_rs_is_valid_ant(valid_tx_ant,
1459 search_tbl->ant_type))
1460 break;
1462 ret = iwl4965_rs_switch_to_mimo2(priv, lq_sta,
1463 conf, sta,
1464 search_tbl, index);
1465 if (!ret)
1466 goto out;
1467 break;
1468 case IWL_SISO_SWITCH_GI:
1469 if (!tbl->is_ht40 && !(ht_cap->cap &
1470 IEEE80211_HT_CAP_SGI_20))
1471 break;
1472 if (tbl->is_ht40 && !(ht_cap->cap &
1473 IEEE80211_HT_CAP_SGI_40))
1474 break;
1476 IWL_DEBUG_RATE(priv, "LQ: SISO toggle SGI/NGI\n");
1478 memcpy(search_tbl, tbl, sz);
1479 if (is_green) {
1480 if (!tbl->is_SGI)
1481 break;
1482 else
1483 IWL_ERR(priv,
1484 "SGI was set in GF+SISO\n");
1486 search_tbl->is_SGI = !tbl->is_SGI;
1487 iwl4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
1488 if (tbl->is_SGI) {
1489 s32 tpt = lq_sta->last_tpt / 100;
1490 if (tpt >= search_tbl->expected_tpt[index])
1491 break;
1493 search_tbl->current_rate =
1494 iwl4965_rate_n_flags_from_tbl(priv, search_tbl,
1495 index, is_green);
1496 update_search_tbl_counter = 1;
1497 goto out;
1499 tbl->action++;
1500 if (tbl->action > IWL_SISO_SWITCH_GI)
1501 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1503 if (tbl->action == start_action)
1504 break;
1506 search_tbl->lq_type = LQ_NONE;
1507 return 0;
1509 out:
1510 lq_sta->search_better_tbl = 1;
1511 tbl->action++;
1512 if (tbl->action > IWL_SISO_SWITCH_GI)
1513 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1514 if (update_search_tbl_counter)
1515 search_tbl->action = tbl->action;
1517 return 0;
1521 * Try to switch to new modulation mode from MIMO2
1523 static int iwl4965_rs_move_mimo2_to_other(struct iwl_priv *priv,
1524 struct iwl_lq_sta *lq_sta,
1525 struct ieee80211_conf *conf,
1526 struct ieee80211_sta *sta, int index)
1528 s8 is_green = lq_sta->is_green;
1529 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1530 struct iwl_scale_tbl_info *search_tbl =
1531 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1532 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1533 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1534 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1535 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1536 u8 start_action;
1537 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1538 u8 tx_chains_num = priv->hw_params.tx_chains_num;
1539 u8 update_search_tbl_counter = 0;
1540 int ret;
1542 start_action = tbl->action;
1543 for (;;) {
1544 lq_sta->action_counter++;
1545 switch (tbl->action) {
1546 case IWL_MIMO2_SWITCH_ANTENNA1:
1547 case IWL_MIMO2_SWITCH_ANTENNA2:
1548 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle Antennas\n");
1550 if (tx_chains_num <= 2)
1551 break;
1553 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1554 break;
1556 memcpy(search_tbl, tbl, sz);
1557 if (iwl4965_rs_toggle_antenna(valid_tx_ant,
1558 &search_tbl->current_rate, search_tbl)) {
1559 update_search_tbl_counter = 1;
1560 goto out;
1562 break;
1563 case IWL_MIMO2_SWITCH_SISO_A:
1564 case IWL_MIMO2_SWITCH_SISO_B:
1565 case IWL_MIMO2_SWITCH_SISO_C:
1566 IWL_DEBUG_RATE(priv, "LQ: MIMO2 switch to SISO\n");
1568 /* Set up new search table for SISO */
1569 memcpy(search_tbl, tbl, sz);
1571 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1572 search_tbl->ant_type = ANT_A;
1573 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1574 search_tbl->ant_type = ANT_B;
1575 else
1576 search_tbl->ant_type = ANT_C;
1578 if (!iwl4965_rs_is_valid_ant(valid_tx_ant,
1579 search_tbl->ant_type))
1580 break;
1582 ret = iwl4965_rs_switch_to_siso(priv, lq_sta,
1583 conf, sta,
1584 search_tbl, index);
1585 if (!ret)
1586 goto out;
1588 break;
1590 case IWL_MIMO2_SWITCH_GI:
1591 if (!tbl->is_ht40 && !(ht_cap->cap &
1592 IEEE80211_HT_CAP_SGI_20))
1593 break;
1594 if (tbl->is_ht40 && !(ht_cap->cap &
1595 IEEE80211_HT_CAP_SGI_40))
1596 break;
1598 IWL_DEBUG_RATE(priv, "LQ: MIMO2 toggle SGI/NGI\n");
1600 /* Set up new search table for MIMO2 */
1601 memcpy(search_tbl, tbl, sz);
1602 search_tbl->is_SGI = !tbl->is_SGI;
1603 iwl4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
1605 * If active table already uses the fastest possible
1606 * modulation (dual stream with short guard interval),
1607 * and it's working well, there's no need to look
1608 * for a better type of modulation!
1610 if (tbl->is_SGI) {
1611 s32 tpt = lq_sta->last_tpt / 100;
1612 if (tpt >= search_tbl->expected_tpt[index])
1613 break;
1615 search_tbl->current_rate =
1616 iwl4965_rate_n_flags_from_tbl(priv, search_tbl,
1617 index, is_green);
1618 update_search_tbl_counter = 1;
1619 goto out;
1622 tbl->action++;
1623 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1624 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1626 if (tbl->action == start_action)
1627 break;
1629 search_tbl->lq_type = LQ_NONE;
1630 return 0;
1631 out:
1632 lq_sta->search_better_tbl = 1;
1633 tbl->action++;
1634 if (tbl->action > IWL_MIMO2_SWITCH_GI)
1635 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1636 if (update_search_tbl_counter)
1637 search_tbl->action = tbl->action;
1639 return 0;
1644 * Check whether we should continue using same modulation mode, or
1645 * begin search for a new mode, based on:
1646 * 1) # tx successes or failures while using this mode
1647 * 2) # times calling this function
1648 * 3) elapsed time in this mode (not used, for now)
1650 static void
1651 iwl4965_rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1653 struct iwl_scale_tbl_info *tbl;
1654 int i;
1655 int active_tbl;
1656 int flush_interval_passed = 0;
1657 struct iwl_priv *priv;
1659 priv = lq_sta->drv;
1660 active_tbl = lq_sta->active_tbl;
1662 tbl = &(lq_sta->lq_info[active_tbl]);
1664 /* If we've been disallowing search, see if we should now allow it */
1665 if (lq_sta->stay_in_tbl) {
1667 /* Elapsed time using current modulation mode */
1668 if (lq_sta->flush_timer)
1669 flush_interval_passed =
1670 time_after(jiffies,
1671 (unsigned long)(lq_sta->flush_timer +
1672 IWL_RATE_SCALE_FLUSH_INTVL));
1675 * Check if we should allow search for new modulation mode.
1676 * If many frames have failed or succeeded, or we've used
1677 * this same modulation for a long time, allow search, and
1678 * reset history stats that keep track of whether we should
1679 * allow a new search. Also (below) reset all bitmaps and
1680 * stats in active history.
1682 if (force_search ||
1683 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1684 (lq_sta->total_success > lq_sta->max_success_limit) ||
1685 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
1686 && (flush_interval_passed))) {
1687 IWL_DEBUG_RATE(priv, "LQ: stay is expired %d %d %d\n:",
1688 lq_sta->total_failed,
1689 lq_sta->total_success,
1690 flush_interval_passed);
1692 /* Allow search for new mode */
1693 lq_sta->stay_in_tbl = 0; /* only place reset */
1694 lq_sta->total_failed = 0;
1695 lq_sta->total_success = 0;
1696 lq_sta->flush_timer = 0;
1699 * Else if we've used this modulation mode enough repetitions
1700 * (regardless of elapsed time or success/failure), reset
1701 * history bitmaps and rate-specific stats for all rates in
1702 * active table.
1704 } else {
1705 lq_sta->table_count++;
1706 if (lq_sta->table_count >=
1707 lq_sta->table_count_limit) {
1708 lq_sta->table_count = 0;
1710 IWL_DEBUG_RATE(priv,
1711 "LQ: stay in table clear win\n");
1712 for (i = 0; i < IWL_RATE_COUNT; i++)
1713 iwl4965_rs_rate_scale_clear_window(
1714 &(tbl->win[i]));
1718 /* If transitioning to allow "search", reset all history
1719 * bitmaps and stats in active table (this will become the new
1720 * "search" table). */
1721 if (!lq_sta->stay_in_tbl) {
1722 for (i = 0; i < IWL_RATE_COUNT; i++)
1723 iwl4965_rs_rate_scale_clear_window(
1724 &(tbl->win[i]));
1730 * setup rate table in uCode
1731 * return rate_n_flags as used in the table
1733 static u32 iwl4965_rs_update_rate_tbl(struct iwl_priv *priv,
1734 struct iwl_rxon_context *ctx,
1735 struct iwl_lq_sta *lq_sta,
1736 struct iwl_scale_tbl_info *tbl,
1737 int index, u8 is_green)
1739 u32 rate;
1741 /* Update uCode's rate table. */
1742 rate = iwl4965_rate_n_flags_from_tbl(priv, tbl, index, is_green);
1743 iwl4965_rs_fill_link_cmd(priv, lq_sta, rate);
1744 iwl_legacy_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_ASYNC, false);
1746 return rate;
1750 * Do rate scaling and search for new modulation mode.
1752 static void iwl4965_rs_rate_scale_perform(struct iwl_priv *priv,
1753 struct sk_buff *skb,
1754 struct ieee80211_sta *sta,
1755 struct iwl_lq_sta *lq_sta)
1757 struct ieee80211_hw *hw = priv->hw;
1758 struct ieee80211_conf *conf = &hw->conf;
1759 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1760 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1761 int low = IWL_RATE_INVALID;
1762 int high = IWL_RATE_INVALID;
1763 int index;
1764 int i;
1765 struct iwl_rate_scale_data *window = NULL;
1766 int current_tpt = IWL_INVALID_VALUE;
1767 int low_tpt = IWL_INVALID_VALUE;
1768 int high_tpt = IWL_INVALID_VALUE;
1769 u32 fail_count;
1770 s8 scale_action = 0;
1771 u16 rate_mask;
1772 u8 update_lq = 0;
1773 struct iwl_scale_tbl_info *tbl, *tbl1;
1774 u16 rate_scale_index_msk = 0;
1775 u32 rate;
1776 u8 is_green = 0;
1777 u8 active_tbl = 0;
1778 u8 done_search = 0;
1779 u16 high_low;
1780 s32 sr;
1781 u8 tid = MAX_TID_COUNT;
1782 struct iwl_tid_data *tid_data;
1783 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1784 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
1786 IWL_DEBUG_RATE(priv, "rate scale calculate new rate for skb\n");
1788 /* Send management frames and NO_ACK data using lowest rate. */
1789 /* TODO: this could probably be improved.. */
1790 if (!ieee80211_is_data(hdr->frame_control) ||
1791 info->flags & IEEE80211_TX_CTL_NO_ACK)
1792 return;
1794 if (!sta || !lq_sta)
1795 return;
1797 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
1799 tid = iwl4965_rs_tl_add_packet(lq_sta, hdr);
1800 if ((tid != MAX_TID_COUNT) && (lq_sta->tx_agg_tid_en & (1 << tid))) {
1801 tid_data = &priv->stations[lq_sta->lq.sta_id].tid[tid];
1802 if (tid_data->agg.state == IWL_AGG_OFF)
1803 lq_sta->is_agg = 0;
1804 else
1805 lq_sta->is_agg = 1;
1806 } else
1807 lq_sta->is_agg = 0;
1810 * Select rate-scale / modulation-mode table to work with in
1811 * the rest of this function: "search" if searching for better
1812 * modulation mode, or "active" if doing rate scaling within a mode.
1814 if (!lq_sta->search_better_tbl)
1815 active_tbl = lq_sta->active_tbl;
1816 else
1817 active_tbl = 1 - lq_sta->active_tbl;
1819 tbl = &(lq_sta->lq_info[active_tbl]);
1820 if (is_legacy(tbl->lq_type))
1821 lq_sta->is_green = 0;
1822 else
1823 lq_sta->is_green = iwl4965_rs_use_green(sta);
1824 is_green = lq_sta->is_green;
1826 /* current tx rate */
1827 index = lq_sta->last_txrate_idx;
1829 IWL_DEBUG_RATE(priv, "Rate scale index %d for type %d\n", index,
1830 tbl->lq_type);
1832 /* rates available for this association, and for modulation mode */
1833 rate_mask = iwl4965_rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
1835 IWL_DEBUG_RATE(priv, "mask 0x%04X\n", rate_mask);
1837 /* mask with station rate restriction */
1838 if (is_legacy(tbl->lq_type)) {
1839 if (lq_sta->band == IEEE80211_BAND_5GHZ)
1840 /* supp_rates has no CCK bits in A mode */
1841 rate_scale_index_msk = (u16) (rate_mask &
1842 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
1843 else
1844 rate_scale_index_msk = (u16) (rate_mask &
1845 lq_sta->supp_rates);
1847 } else
1848 rate_scale_index_msk = rate_mask;
1850 if (!rate_scale_index_msk)
1851 rate_scale_index_msk = rate_mask;
1853 if (!((1 << index) & rate_scale_index_msk)) {
1854 IWL_ERR(priv, "Current Rate is not valid\n");
1855 if (lq_sta->search_better_tbl) {
1856 /* revert to active table if search table is not valid*/
1857 tbl->lq_type = LQ_NONE;
1858 lq_sta->search_better_tbl = 0;
1859 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1860 /* get "active" rate info */
1861 index = iwl4965_hwrate_to_plcp_idx(tbl->current_rate);
1862 rate = iwl4965_rs_update_rate_tbl(priv, ctx, lq_sta,
1863 tbl, index, is_green);
1865 return;
1868 /* Get expected throughput table and history window for current rate */
1869 if (!tbl->expected_tpt) {
1870 IWL_ERR(priv, "tbl->expected_tpt is NULL\n");
1871 return;
1874 /* force user max rate if set by user */
1875 if ((lq_sta->max_rate_idx != -1) &&
1876 (lq_sta->max_rate_idx < index)) {
1877 index = lq_sta->max_rate_idx;
1878 update_lq = 1;
1879 window = &(tbl->win[index]);
1880 goto lq_update;
1883 window = &(tbl->win[index]);
1886 * If there is not enough history to calculate actual average
1887 * throughput, keep analyzing results of more tx frames, without
1888 * changing rate or mode (bypass most of the rest of this function).
1889 * Set up new rate table in uCode only if old rate is not supported
1890 * in current association (use new rate found above).
1892 fail_count = window->counter - window->success_counter;
1893 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1894 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1895 IWL_DEBUG_RATE(priv, "LQ: still below TH. succ=%d total=%d "
1896 "for index %d\n",
1897 window->success_counter, window->counter, index);
1899 /* Can't calculate this yet; not enough history */
1900 window->average_tpt = IWL_INVALID_VALUE;
1902 /* Should we stay with this modulation mode,
1903 * or search for a new one? */
1904 iwl4965_rs_stay_in_table(lq_sta, false);
1906 goto out;
1908 /* Else we have enough samples; calculate estimate of
1909 * actual average throughput */
1910 if (window->average_tpt != ((window->success_ratio *
1911 tbl->expected_tpt[index] + 64) / 128)) {
1912 IWL_ERR(priv,
1913 "expected_tpt should have been calculated by now\n");
1914 window->average_tpt = ((window->success_ratio *
1915 tbl->expected_tpt[index] + 64) / 128);
1918 /* If we are searching for better modulation mode, check success. */
1919 if (lq_sta->search_better_tbl) {
1920 /* If good success, continue using the "search" mode;
1921 * no need to send new link quality command, since we're
1922 * continuing to use the setup that we've been trying. */
1923 if (window->average_tpt > lq_sta->last_tpt) {
1925 IWL_DEBUG_RATE(priv, "LQ: SWITCHING TO NEW TABLE "
1926 "suc=%d cur-tpt=%d old-tpt=%d\n",
1927 window->success_ratio,
1928 window->average_tpt,
1929 lq_sta->last_tpt);
1931 if (!is_legacy(tbl->lq_type))
1932 lq_sta->enable_counter = 1;
1934 /* Swap tables; "search" becomes "active" */
1935 lq_sta->active_tbl = active_tbl;
1936 current_tpt = window->average_tpt;
1938 /* Else poor success; go back to mode in "active" table */
1939 } else {
1941 IWL_DEBUG_RATE(priv, "LQ: GOING BACK TO THE OLD TABLE "
1942 "suc=%d cur-tpt=%d old-tpt=%d\n",
1943 window->success_ratio,
1944 window->average_tpt,
1945 lq_sta->last_tpt);
1947 /* Nullify "search" table */
1948 tbl->lq_type = LQ_NONE;
1950 /* Revert to "active" table */
1951 active_tbl = lq_sta->active_tbl;
1952 tbl = &(lq_sta->lq_info[active_tbl]);
1954 /* Revert to "active" rate and throughput info */
1955 index = iwl4965_hwrate_to_plcp_idx(tbl->current_rate);
1956 current_tpt = lq_sta->last_tpt;
1958 /* Need to set up a new rate table in uCode */
1959 update_lq = 1;
1962 /* Either way, we've made a decision; modulation mode
1963 * search is done, allow rate adjustment next time. */
1964 lq_sta->search_better_tbl = 0;
1965 done_search = 1; /* Don't switch modes below! */
1966 goto lq_update;
1969 /* (Else) not in search of better modulation mode, try for better
1970 * starting rate, while staying in this mode. */
1971 high_low = iwl4965_rs_get_adjacent_rate(priv, index,
1972 rate_scale_index_msk,
1973 tbl->lq_type);
1974 low = high_low & 0xff;
1975 high = (high_low >> 8) & 0xff;
1977 /* If user set max rate, dont allow higher than user constrain */
1978 if ((lq_sta->max_rate_idx != -1) &&
1979 (lq_sta->max_rate_idx < high))
1980 high = IWL_RATE_INVALID;
1982 sr = window->success_ratio;
1984 /* Collect measured throughputs for current and adjacent rates */
1985 current_tpt = window->average_tpt;
1986 if (low != IWL_RATE_INVALID)
1987 low_tpt = tbl->win[low].average_tpt;
1988 if (high != IWL_RATE_INVALID)
1989 high_tpt = tbl->win[high].average_tpt;
1991 scale_action = 0;
1993 /* Too many failures, decrease rate */
1994 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
1995 IWL_DEBUG_RATE(priv,
1996 "decrease rate because of low success_ratio\n");
1997 scale_action = -1;
1999 /* No throughput measured yet for adjacent rates; try increase. */
2000 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2001 (high_tpt == IWL_INVALID_VALUE)) {
2003 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2004 scale_action = 1;
2005 else if (low != IWL_RATE_INVALID)
2006 scale_action = 0;
2009 /* Both adjacent throughputs are measured, but neither one has better
2010 * throughput; we're using the best rate, don't change it! */
2011 else if ((low_tpt != IWL_INVALID_VALUE) &&
2012 (high_tpt != IWL_INVALID_VALUE) &&
2013 (low_tpt < current_tpt) &&
2014 (high_tpt < current_tpt))
2015 scale_action = 0;
2017 /* At least one adjacent rate's throughput is measured,
2018 * and may have better performance. */
2019 else {
2020 /* Higher adjacent rate's throughput is measured */
2021 if (high_tpt != IWL_INVALID_VALUE) {
2022 /* Higher rate has better throughput */
2023 if (high_tpt > current_tpt &&
2024 sr >= IWL_RATE_INCREASE_TH) {
2025 scale_action = 1;
2026 } else {
2027 scale_action = 0;
2030 /* Lower adjacent rate's throughput is measured */
2031 } else if (low_tpt != IWL_INVALID_VALUE) {
2032 /* Lower rate has better throughput */
2033 if (low_tpt > current_tpt) {
2034 IWL_DEBUG_RATE(priv,
2035 "decrease rate because of low tpt\n");
2036 scale_action = -1;
2037 } else if (sr >= IWL_RATE_INCREASE_TH) {
2038 scale_action = 1;
2043 /* Sanity check; asked for decrease, but success rate or throughput
2044 * has been good at old rate. Don't change it. */
2045 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2046 ((sr > IWL_RATE_HIGH_TH) ||
2047 (current_tpt > (100 * tbl->expected_tpt[low]))))
2048 scale_action = 0;
2050 switch (scale_action) {
2051 case -1:
2052 /* Decrease starting rate, update uCode's rate table */
2053 if (low != IWL_RATE_INVALID) {
2054 update_lq = 1;
2055 index = low;
2058 break;
2059 case 1:
2060 /* Increase starting rate, update uCode's rate table */
2061 if (high != IWL_RATE_INVALID) {
2062 update_lq = 1;
2063 index = high;
2066 break;
2067 case 0:
2068 /* No change */
2069 default:
2070 break;
2073 IWL_DEBUG_RATE(priv, "choose rate scale index %d action %d low %d "
2074 "high %d type %d\n",
2075 index, scale_action, low, high, tbl->lq_type);
2077 lq_update:
2078 /* Replace uCode's rate table for the destination station. */
2079 if (update_lq)
2080 rate = iwl4965_rs_update_rate_tbl(priv, ctx, lq_sta,
2081 tbl, index, is_green);
2083 /* Should we stay with this modulation mode,
2084 * or search for a new one? */
2085 iwl4965_rs_stay_in_table(lq_sta, false);
2088 * Search for new modulation mode if we're:
2089 * 1) Not changing rates right now
2090 * 2) Not just finishing up a search
2091 * 3) Allowing a new search
2093 if (!update_lq && !done_search &&
2094 !lq_sta->stay_in_tbl && window->counter) {
2095 /* Save current throughput to compare with "search" throughput*/
2096 lq_sta->last_tpt = current_tpt;
2098 /* Select a new "search" modulation mode to try.
2099 * If one is found, set up the new "search" table. */
2100 if (is_legacy(tbl->lq_type))
2101 iwl4965_rs_move_legacy_other(priv, lq_sta,
2102 conf, sta, index);
2103 else if (is_siso(tbl->lq_type))
2104 iwl4965_rs_move_siso_to_other(priv, lq_sta,
2105 conf, sta, index);
2106 else /* (is_mimo2(tbl->lq_type)) */
2107 iwl4965_rs_move_mimo2_to_other(priv, lq_sta,
2108 conf, sta, index);
2110 /* If new "search" mode was selected, set up in uCode table */
2111 if (lq_sta->search_better_tbl) {
2112 /* Access the "search" table, clear its history. */
2113 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2114 for (i = 0; i < IWL_RATE_COUNT; i++)
2115 iwl4965_rs_rate_scale_clear_window(
2116 &(tbl->win[i]));
2118 /* Use new "search" start rate */
2119 index = iwl4965_hwrate_to_plcp_idx(tbl->current_rate);
2121 IWL_DEBUG_RATE(priv,
2122 "Switch current mcs: %X index: %d\n",
2123 tbl->current_rate, index);
2124 iwl4965_rs_fill_link_cmd(priv, lq_sta,
2125 tbl->current_rate);
2126 iwl_legacy_send_lq_cmd(priv, ctx,
2127 &lq_sta->lq, CMD_ASYNC, false);
2128 } else
2129 done_search = 1;
2132 if (done_search && !lq_sta->stay_in_tbl) {
2133 /* If the "active" (non-search) mode was legacy,
2134 * and we've tried switching antennas,
2135 * but we haven't been able to try HT modes (not available),
2136 * stay with best antenna legacy modulation for a while
2137 * before next round of mode comparisons. */
2138 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2139 if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
2140 lq_sta->action_counter > tbl1->max_search) {
2141 IWL_DEBUG_RATE(priv, "LQ: STAY in legacy table\n");
2142 iwl4965_rs_set_stay_in_table(priv, 1, lq_sta);
2145 /* If we're in an HT mode, and all 3 mode switch actions
2146 * have been tried and compared, stay in this best modulation
2147 * mode for a while before next round of mode comparisons. */
2148 if (lq_sta->enable_counter &&
2149 (lq_sta->action_counter >= tbl1->max_search)) {
2150 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2151 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2152 (tid != MAX_TID_COUNT)) {
2153 tid_data =
2154 &priv->stations[lq_sta->lq.sta_id].tid[tid];
2155 if (tid_data->agg.state == IWL_AGG_OFF) {
2156 IWL_DEBUG_RATE(priv,
2157 "try to aggregate tid %d\n",
2158 tid);
2159 iwl4965_rs_tl_turn_on_agg(priv, tid,
2160 lq_sta, sta);
2163 iwl4965_rs_set_stay_in_table(priv, 0, lq_sta);
2167 out:
2168 tbl->current_rate = iwl4965_rate_n_flags_from_tbl(priv, tbl,
2169 index, is_green);
2170 i = index;
2171 lq_sta->last_txrate_idx = i;
2175 * iwl4965_rs_initialize_lq - Initialize a station's hardware rate table
2177 * The uCode's station table contains a table of fallback rates
2178 * for automatic fallback during transmission.
2180 * NOTE: This sets up a default set of values. These will be replaced later
2181 * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
2182 * rc80211_simple.
2184 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2185 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2186 * which requires station table entry to exist).
2188 static void iwl4965_rs_initialize_lq(struct iwl_priv *priv,
2189 struct ieee80211_conf *conf,
2190 struct ieee80211_sta *sta,
2191 struct iwl_lq_sta *lq_sta)
2193 struct iwl_scale_tbl_info *tbl;
2194 int rate_idx;
2195 int i;
2196 u32 rate;
2197 u8 use_green = iwl4965_rs_use_green(sta);
2198 u8 active_tbl = 0;
2199 u8 valid_tx_ant;
2200 struct iwl_station_priv *sta_priv;
2201 struct iwl_rxon_context *ctx;
2203 if (!sta || !lq_sta)
2204 return;
2206 sta_priv = (void *)sta->drv_priv;
2207 ctx = sta_priv->common.ctx;
2209 i = lq_sta->last_txrate_idx;
2211 valid_tx_ant = priv->hw_params.valid_tx_ant;
2213 if (!lq_sta->search_better_tbl)
2214 active_tbl = lq_sta->active_tbl;
2215 else
2216 active_tbl = 1 - lq_sta->active_tbl;
2218 tbl = &(lq_sta->lq_info[active_tbl]);
2220 if ((i < 0) || (i >= IWL_RATE_COUNT))
2221 i = 0;
2223 rate = iwlegacy_rates[i].plcp;
2224 tbl->ant_type = iwl4965_first_antenna(valid_tx_ant);
2225 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2227 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2228 rate |= RATE_MCS_CCK_MSK;
2230 iwl4965_rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
2231 if (!iwl4965_rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2232 iwl4965_rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2234 rate = iwl4965_rate_n_flags_from_tbl(priv, tbl, rate_idx, use_green);
2235 tbl->current_rate = rate;
2236 iwl4965_rs_set_expected_tpt_table(lq_sta, tbl);
2237 iwl4965_rs_fill_link_cmd(NULL, lq_sta, rate);
2238 priv->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
2239 iwl_legacy_send_lq_cmd(priv, ctx, &lq_sta->lq, CMD_SYNC, true);
2242 static void
2243 iwl4965_rs_get_rate(void *priv_r, struct ieee80211_sta *sta, void *priv_sta,
2244 struct ieee80211_tx_rate_control *txrc)
2247 struct sk_buff *skb = txrc->skb;
2248 struct ieee80211_supported_band *sband = txrc->sband;
2249 struct iwl_priv *priv __maybe_unused = (struct iwl_priv *)priv_r;
2250 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2251 struct iwl_lq_sta *lq_sta = priv_sta;
2252 int rate_idx;
2254 IWL_DEBUG_RATE_LIMIT(priv, "rate scale calculate new rate for skb\n");
2256 /* Get max rate if user set max rate */
2257 if (lq_sta) {
2258 lq_sta->max_rate_idx = txrc->max_rate_idx;
2259 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2260 (lq_sta->max_rate_idx != -1))
2261 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2262 if ((lq_sta->max_rate_idx < 0) ||
2263 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2264 lq_sta->max_rate_idx = -1;
2267 /* Treat uninitialized rate scaling data same as non-existing. */
2268 if (lq_sta && !lq_sta->drv) {
2269 IWL_DEBUG_RATE(priv, "Rate scaling not initialized yet.\n");
2270 priv_sta = NULL;
2273 /* Send management frames and NO_ACK data using lowest rate. */
2274 if (rate_control_send_low(sta, priv_sta, txrc))
2275 return;
2277 if (!lq_sta)
2278 return;
2280 rate_idx = lq_sta->last_txrate_idx;
2282 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2283 rate_idx -= IWL_FIRST_OFDM_RATE;
2284 /* 6M and 9M shared same MCS index */
2285 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2286 if (iwl4965_rs_extract_rate(lq_sta->last_rate_n_flags) >=
2287 IWL_RATE_MIMO2_6M_PLCP)
2288 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2289 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2290 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2291 info->control.rates[0].flags |=
2292 IEEE80211_TX_RC_SHORT_GI;
2293 if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
2294 info->control.rates[0].flags |=
2295 IEEE80211_TX_RC_DUP_DATA;
2296 if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
2297 info->control.rates[0].flags |=
2298 IEEE80211_TX_RC_40_MHZ_WIDTH;
2299 if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
2300 info->control.rates[0].flags |=
2301 IEEE80211_TX_RC_GREEN_FIELD;
2302 } else {
2303 /* Check for invalid rates */
2304 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2305 ((sband->band == IEEE80211_BAND_5GHZ) &&
2306 (rate_idx < IWL_FIRST_OFDM_RATE)))
2307 rate_idx = rate_lowest_index(sband, sta);
2308 /* On valid 5 GHz rate, adjust index */
2309 else if (sband->band == IEEE80211_BAND_5GHZ)
2310 rate_idx -= IWL_FIRST_OFDM_RATE;
2311 info->control.rates[0].flags = 0;
2313 info->control.rates[0].idx = rate_idx;
2317 static void *iwl4965_rs_alloc_sta(void *priv_rate, struct ieee80211_sta *sta,
2318 gfp_t gfp)
2320 struct iwl_lq_sta *lq_sta;
2321 struct iwl_station_priv *sta_priv =
2322 (struct iwl_station_priv *) sta->drv_priv;
2323 struct iwl_priv *priv;
2325 priv = (struct iwl_priv *)priv_rate;
2326 IWL_DEBUG_RATE(priv, "create station rate scale window\n");
2328 lq_sta = &sta_priv->lq_sta;
2330 return lq_sta;
2334 * Called after adding a new station to initialize rate scaling
2336 void
2337 iwl4965_rs_rate_init(struct iwl_priv *priv,
2338 struct ieee80211_sta *sta,
2339 u8 sta_id)
2341 int i, j;
2342 struct ieee80211_hw *hw = priv->hw;
2343 struct ieee80211_conf *conf = &priv->hw->conf;
2344 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2345 struct iwl_station_priv *sta_priv;
2346 struct iwl_lq_sta *lq_sta;
2347 struct ieee80211_supported_band *sband;
2349 sta_priv = (struct iwl_station_priv *) sta->drv_priv;
2350 lq_sta = &sta_priv->lq_sta;
2351 sband = hw->wiphy->bands[conf->channel->band];
2354 lq_sta->lq.sta_id = sta_id;
2356 for (j = 0; j < LQ_SIZE; j++)
2357 for (i = 0; i < IWL_RATE_COUNT; i++)
2358 iwl4965_rs_rate_scale_clear_window(
2359 &lq_sta->lq_info[j].win[i]);
2361 lq_sta->flush_timer = 0;
2362 lq_sta->supp_rates = sta->supp_rates[sband->band];
2363 for (j = 0; j < LQ_SIZE; j++)
2364 for (i = 0; i < IWL_RATE_COUNT; i++)
2365 iwl4965_rs_rate_scale_clear_window(
2366 &lq_sta->lq_info[j].win[i]);
2368 IWL_DEBUG_RATE(priv, "LQ:"
2369 "*** rate scale station global init for station %d ***\n",
2370 sta_id);
2371 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2372 * the lowest or the highest rate.. Could consider using RSSI from
2373 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2374 * after assoc.. */
2376 lq_sta->is_dup = 0;
2377 lq_sta->max_rate_idx = -1;
2378 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2379 lq_sta->is_green = iwl4965_rs_use_green(sta);
2380 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
2381 lq_sta->band = priv->band;
2383 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2384 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2386 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2387 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2388 lq_sta->active_siso_rate &= ~((u16)0x2);
2389 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2391 /* Same here */
2392 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2393 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2394 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2395 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2397 /* These values will be overridden later */
2398 lq_sta->lq.general_params.single_stream_ant_msk =
2399 iwl4965_first_antenna(priv->hw_params.valid_tx_ant);
2400 lq_sta->lq.general_params.dual_stream_ant_msk =
2401 priv->hw_params.valid_tx_ant &
2402 ~iwl4965_first_antenna(priv->hw_params.valid_tx_ant);
2403 if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
2404 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2405 } else if (iwl4965_num_of_ant(priv->hw_params.valid_tx_ant) == 2) {
2406 lq_sta->lq.general_params.dual_stream_ant_msk =
2407 priv->hw_params.valid_tx_ant;
2410 /* as default allow aggregation for all tids */
2411 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2412 lq_sta->drv = priv;
2414 /* Set last_txrate_idx to lowest rate */
2415 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2416 if (sband->band == IEEE80211_BAND_5GHZ)
2417 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2418 lq_sta->is_agg = 0;
2420 #ifdef CONFIG_MAC80211_DEBUGFS
2421 lq_sta->dbg_fixed_rate = 0;
2422 #endif
2424 iwl4965_rs_initialize_lq(priv, conf, sta, lq_sta);
2427 static void iwl4965_rs_fill_link_cmd(struct iwl_priv *priv,
2428 struct iwl_lq_sta *lq_sta, u32 new_rate)
2430 struct iwl_scale_tbl_info tbl_type;
2431 int index = 0;
2432 int rate_idx;
2433 int repeat_rate = 0;
2434 u8 ant_toggle_cnt = 0;
2435 u8 use_ht_possible = 1;
2436 u8 valid_tx_ant = 0;
2437 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
2439 /* Override starting rate (index 0) if needed for debug purposes */
2440 iwl4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2442 /* Interpret new_rate (rate_n_flags) */
2443 iwl4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2444 &tbl_type, &rate_idx);
2446 /* How many times should we repeat the initial rate? */
2447 if (is_legacy(tbl_type.lq_type)) {
2448 ant_toggle_cnt = 1;
2449 repeat_rate = IWL_NUMBER_TRY;
2450 } else {
2451 repeat_rate = IWL_HT_NUMBER_TRY;
2454 lq_cmd->general_params.mimo_delimiter =
2455 is_mimo(tbl_type.lq_type) ? 1 : 0;
2457 /* Fill 1st table entry (index 0) */
2458 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2460 if (iwl4965_num_of_ant(tbl_type.ant_type) == 1) {
2461 lq_cmd->general_params.single_stream_ant_msk =
2462 tbl_type.ant_type;
2463 } else if (iwl4965_num_of_ant(tbl_type.ant_type) == 2) {
2464 lq_cmd->general_params.dual_stream_ant_msk =
2465 tbl_type.ant_type;
2466 } /* otherwise we don't modify the existing value */
2468 index++;
2469 repeat_rate--;
2470 if (priv)
2471 valid_tx_ant = priv->hw_params.valid_tx_ant;
2473 /* Fill rest of rate table */
2474 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2475 /* Repeat initial/next rate.
2476 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2477 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2478 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2479 if (is_legacy(tbl_type.lq_type)) {
2480 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2481 ant_toggle_cnt++;
2482 else if (priv &&
2483 iwl4965_rs_toggle_antenna(valid_tx_ant,
2484 &new_rate, &tbl_type))
2485 ant_toggle_cnt = 1;
2488 /* Override next rate if needed for debug purposes */
2489 iwl4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2491 /* Fill next table entry */
2492 lq_cmd->rs_table[index].rate_n_flags =
2493 cpu_to_le32(new_rate);
2494 repeat_rate--;
2495 index++;
2498 iwl4965_rs_get_tbl_info_from_mcs(new_rate,
2499 lq_sta->band, &tbl_type,
2500 &rate_idx);
2502 /* Indicate to uCode which entries might be MIMO.
2503 * If initial rate was MIMO, this will finally end up
2504 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2505 if (is_mimo(tbl_type.lq_type))
2506 lq_cmd->general_params.mimo_delimiter = index;
2508 /* Get next rate */
2509 new_rate = iwl4965_rs_get_lower_rate(lq_sta,
2510 &tbl_type, rate_idx,
2511 use_ht_possible);
2513 /* How many times should we repeat the next rate? */
2514 if (is_legacy(tbl_type.lq_type)) {
2515 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2516 ant_toggle_cnt++;
2517 else if (priv &&
2518 iwl4965_rs_toggle_antenna(valid_tx_ant,
2519 &new_rate, &tbl_type))
2520 ant_toggle_cnt = 1;
2522 repeat_rate = IWL_NUMBER_TRY;
2523 } else {
2524 repeat_rate = IWL_HT_NUMBER_TRY;
2527 /* Don't allow HT rates after next pass.
2528 * iwl4965_rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2529 use_ht_possible = 0;
2531 /* Override next rate if needed for debug purposes */
2532 iwl4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2534 /* Fill next table entry */
2535 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
2537 index++;
2538 repeat_rate--;
2541 lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2542 lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2544 lq_cmd->agg_params.agg_time_limit =
2545 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2548 static void
2549 *iwl4965_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2551 return hw->priv;
2553 /* rate scale requires free function to be implemented */
2554 static void iwl4965_rs_free(void *priv_rate)
2556 return;
2559 static void iwl4965_rs_free_sta(void *priv_r, struct ieee80211_sta *sta,
2560 void *priv_sta)
2562 struct iwl_priv *priv __maybe_unused = priv_r;
2564 IWL_DEBUG_RATE(priv, "enter\n");
2565 IWL_DEBUG_RATE(priv, "leave\n");
2569 #ifdef CONFIG_MAC80211_DEBUGFS
2570 static int iwl4965_open_file_generic(struct inode *inode, struct file *file)
2572 file->private_data = inode->i_private;
2573 return 0;
2575 static void iwl4965_rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2576 u32 *rate_n_flags, int index)
2578 struct iwl_priv *priv;
2579 u8 valid_tx_ant;
2580 u8 ant_sel_tx;
2582 priv = lq_sta->drv;
2583 valid_tx_ant = priv->hw_params.valid_tx_ant;
2584 if (lq_sta->dbg_fixed_rate) {
2585 ant_sel_tx =
2586 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2587 >> RATE_MCS_ANT_POS);
2588 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2589 *rate_n_flags = lq_sta->dbg_fixed_rate;
2590 IWL_DEBUG_RATE(priv, "Fixed rate ON\n");
2591 } else {
2592 lq_sta->dbg_fixed_rate = 0;
2593 IWL_ERR(priv,
2594 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2595 ant_sel_tx, valid_tx_ant);
2596 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2598 } else {
2599 IWL_DEBUG_RATE(priv, "Fixed rate OFF\n");
2603 static ssize_t iwl4965_rs_sta_dbgfs_scale_table_write(struct file *file,
2604 const char __user *user_buf, size_t count, loff_t *ppos)
2606 struct iwl_lq_sta *lq_sta = file->private_data;
2607 struct iwl_priv *priv;
2608 char buf[64];
2609 size_t buf_size;
2610 u32 parsed_rate;
2611 struct iwl_station_priv *sta_priv =
2612 container_of(lq_sta, struct iwl_station_priv, lq_sta);
2613 struct iwl_rxon_context *ctx = sta_priv->common.ctx;
2615 priv = lq_sta->drv;
2616 memset(buf, 0, sizeof(buf));
2617 buf_size = min(count, sizeof(buf) - 1);
2618 if (copy_from_user(buf, user_buf, buf_size))
2619 return -EFAULT;
2621 if (sscanf(buf, "%x", &parsed_rate) == 1)
2622 lq_sta->dbg_fixed_rate = parsed_rate;
2623 else
2624 lq_sta->dbg_fixed_rate = 0;
2626 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2627 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2628 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2630 IWL_DEBUG_RATE(priv, "sta_id %d rate 0x%X\n",
2631 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
2633 if (lq_sta->dbg_fixed_rate) {
2634 iwl4965_rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
2635 iwl_legacy_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
2636 false);
2639 return count;
2642 static ssize_t iwl4965_rs_sta_dbgfs_scale_table_read(struct file *file,
2643 char __user *user_buf, size_t count, loff_t *ppos)
2645 char *buff;
2646 int desc = 0;
2647 int i = 0;
2648 int index = 0;
2649 ssize_t ret;
2651 struct iwl_lq_sta *lq_sta = file->private_data;
2652 struct iwl_priv *priv;
2653 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2655 priv = lq_sta->drv;
2656 buff = kmalloc(1024, GFP_KERNEL);
2657 if (!buff)
2658 return -ENOMEM;
2660 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2661 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2662 lq_sta->total_failed, lq_sta->total_success,
2663 lq_sta->active_legacy_rate);
2664 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2665 lq_sta->dbg_fixed_rate);
2666 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
2667 (priv->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
2668 (priv->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
2669 (priv->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
2670 desc += sprintf(buff+desc, "lq type %s\n",
2671 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2672 if (is_Ht(tbl->lq_type)) {
2673 desc += sprintf(buff+desc, " %s",
2674 (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
2675 desc += sprintf(buff+desc, " %s",
2676 (tbl->is_ht40) ? "40MHz" : "20MHz");
2677 desc += sprintf(buff+desc, " %s %s %s\n",
2678 (tbl->is_SGI) ? "SGI" : "",
2679 (lq_sta->is_green) ? "GF enabled" : "",
2680 (lq_sta->is_agg) ? "AGG on" : "");
2682 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2683 lq_sta->last_rate_n_flags);
2684 desc += sprintf(buff+desc, "general:"
2685 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2686 lq_sta->lq.general_params.flags,
2687 lq_sta->lq.general_params.mimo_delimiter,
2688 lq_sta->lq.general_params.single_stream_ant_msk,
2689 lq_sta->lq.general_params.dual_stream_ant_msk);
2691 desc += sprintf(buff+desc, "agg:"
2692 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2693 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2694 lq_sta->lq.agg_params.agg_dis_start_th,
2695 lq_sta->lq.agg_params.agg_frame_cnt_limit);
2697 desc += sprintf(buff+desc,
2698 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2699 lq_sta->lq.general_params.start_rate_index[0],
2700 lq_sta->lq.general_params.start_rate_index[1],
2701 lq_sta->lq.general_params.start_rate_index[2],
2702 lq_sta->lq.general_params.start_rate_index[3]);
2704 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2705 index = iwl4965_hwrate_to_plcp_idx(
2706 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
2707 if (is_legacy(tbl->lq_type)) {
2708 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2710 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2711 iwl_rate_mcs[index].mbps);
2712 } else {
2713 desc += sprintf(buff+desc,
2714 " rate[%d] 0x%X %smbps (%s)\n",
2716 le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
2717 iwl_rate_mcs[index].mbps, iwl_rate_mcs[index].mcs);
2721 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2722 kfree(buff);
2723 return ret;
2726 static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2727 .write = iwl4965_rs_sta_dbgfs_scale_table_write,
2728 .read = iwl4965_rs_sta_dbgfs_scale_table_read,
2729 .open = iwl4965_open_file_generic,
2730 .llseek = default_llseek,
2732 static ssize_t iwl4965_rs_sta_dbgfs_stats_table_read(struct file *file,
2733 char __user *user_buf, size_t count, loff_t *ppos)
2735 char *buff;
2736 int desc = 0;
2737 int i, j;
2738 ssize_t ret;
2740 struct iwl_lq_sta *lq_sta = file->private_data;
2742 buff = kmalloc(1024, GFP_KERNEL);
2743 if (!buff)
2744 return -ENOMEM;
2746 for (i = 0; i < LQ_SIZE; i++) {
2747 desc += sprintf(buff+desc,
2748 "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
2749 "rate=0x%X\n",
2750 lq_sta->active_tbl == i ? "*" : "x",
2751 lq_sta->lq_info[i].lq_type,
2752 lq_sta->lq_info[i].is_SGI,
2753 lq_sta->lq_info[i].is_ht40,
2754 lq_sta->lq_info[i].is_dup,
2755 lq_sta->is_green,
2756 lq_sta->lq_info[i].current_rate);
2757 for (j = 0; j < IWL_RATE_COUNT; j++) {
2758 desc += sprintf(buff+desc,
2759 "counter=%d success=%d %%=%d\n",
2760 lq_sta->lq_info[i].win[j].counter,
2761 lq_sta->lq_info[i].win[j].success_counter,
2762 lq_sta->lq_info[i].win[j].success_ratio);
2765 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2766 kfree(buff);
2767 return ret;
2770 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2771 .read = iwl4965_rs_sta_dbgfs_stats_table_read,
2772 .open = iwl4965_open_file_generic,
2773 .llseek = default_llseek,
2776 static ssize_t iwl4965_rs_sta_dbgfs_rate_scale_data_read(struct file *file,
2777 char __user *user_buf, size_t count, loff_t *ppos)
2779 char buff[120];
2780 int desc = 0;
2781 ssize_t ret;
2783 struct iwl_lq_sta *lq_sta = file->private_data;
2784 struct iwl_priv *priv;
2785 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
2787 priv = lq_sta->drv;
2789 if (is_Ht(tbl->lq_type))
2790 desc += sprintf(buff+desc,
2791 "Bit Rate= %d Mb/s\n",
2792 tbl->expected_tpt[lq_sta->last_txrate_idx]);
2793 else
2794 desc += sprintf(buff+desc,
2795 "Bit Rate= %d Mb/s\n",
2796 iwlegacy_rates[lq_sta->last_txrate_idx].ieee >> 1);
2798 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2799 return ret;
2802 static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
2803 .read = iwl4965_rs_sta_dbgfs_rate_scale_data_read,
2804 .open = iwl4965_open_file_generic,
2805 .llseek = default_llseek,
2808 static void iwl4965_rs_add_debugfs(void *priv, void *priv_sta,
2809 struct dentry *dir)
2811 struct iwl_lq_sta *lq_sta = priv_sta;
2812 lq_sta->rs_sta_dbgfs_scale_table_file =
2813 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
2814 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2815 lq_sta->rs_sta_dbgfs_stats_table_file =
2816 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
2817 lq_sta, &rs_sta_dbgfs_stats_table_ops);
2818 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
2819 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
2820 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
2821 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2822 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
2823 &lq_sta->tx_agg_tid_en);
2827 static void iwl4965_rs_remove_debugfs(void *priv, void *priv_sta)
2829 struct iwl_lq_sta *lq_sta = priv_sta;
2830 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2831 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
2832 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
2833 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2835 #endif
2838 * Initialization of rate scaling information is done by driver after
2839 * the station is added. Since mac80211 calls this function before a
2840 * station is added we ignore it.
2842 static void
2843 iwl4965_rs_rate_init_stub(void *priv_r, struct ieee80211_supported_band *sband,
2844 struct ieee80211_sta *sta, void *priv_sta)
2847 static struct rate_control_ops rs_4965_ops = {
2848 .module = NULL,
2849 .name = IWL4965_RS_NAME,
2850 .tx_status = iwl4965_rs_tx_status,
2851 .get_rate = iwl4965_rs_get_rate,
2852 .rate_init = iwl4965_rs_rate_init_stub,
2853 .alloc = iwl4965_rs_alloc,
2854 .free = iwl4965_rs_free,
2855 .alloc_sta = iwl4965_rs_alloc_sta,
2856 .free_sta = iwl4965_rs_free_sta,
2857 #ifdef CONFIG_MAC80211_DEBUGFS
2858 .add_sta_debugfs = iwl4965_rs_add_debugfs,
2859 .remove_sta_debugfs = iwl4965_rs_remove_debugfs,
2860 #endif
2863 int iwl4965_rate_control_register(void)
2865 return ieee80211_rate_control_register(&rs_4965_ops);
2868 void iwl4965_rate_control_unregister(void)
2870 ieee80211_rate_control_unregister(&rs_4965_ops);