blob: b99fe3163866169c1bfc0c2091a0cae062205354 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
3 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
4 *
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.
8 *
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.
13 *
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
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * Intel Linux Wireless <ilw@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
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>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37#include "rs.h"
38#include "fw-api.h"
39#include "sta.h"
40#include "iwl-op-mode.h"
41#include "mvm.h"
42
43#define RS_NAME "iwl-mvm-rs"
44
45#define NUM_TRY_BEFORE_ANT_TOGGLE 1
46#define IWL_NUMBER_TRY 1
47#define IWL_HT_NUMBER_TRY 3
48
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 */
52
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)
57
58static 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
66};
67
68static 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,
77};
78
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_MIMO3_##s##M_PLCP,\
84 IWL_RATE_##r##M_IEEE, \
85 IWL_RATE_##ip##M_INDEX, \
86 IWL_RATE_##in##M_INDEX, \
87 IWL_RATE_##rp##M_INDEX, \
88 IWL_RATE_##rn##M_INDEX, \
89 IWL_RATE_##pp##M_INDEX, \
90 IWL_RATE_##np##M_INDEX }
91
92/*
93 * Parameter order:
94 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
95 *
96 * If there isn't a valid next or previous rate then INV is used which
97 * maps to IWL_RATE_INVALID
98 *
99 */
100static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
101 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
102 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
103 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
104 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
105 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
106 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
107 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
108 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
109 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
110 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
111 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
112 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
113 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
114 /* FIXME:RS: ^^ should be INV (legacy) */
115};
116
117static inline u8 rs_extract_rate(u32 rate_n_flags)
118{
119 /* also works for HT because bits 7:6 are zero there */
120 return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
121}
122
123static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
124{
125 int idx = 0;
126
127 /* HT rate format */
128 if (rate_n_flags & RATE_MCS_HT_MSK) {
129 idx = rs_extract_rate(rate_n_flags);
130
131 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
132 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
133 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
134 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
135
136 idx += IWL_FIRST_OFDM_RATE;
137 /* skip 9M not supported in ht*/
138 if (idx >= IWL_RATE_9M_INDEX)
139 idx += 1;
140 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
141 return idx;
142
143 /* legacy rate format, search for match in table */
144 } else {
145 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
146 if (iwl_rates[idx].plcp ==
147 rs_extract_rate(rate_n_flags))
148 return idx;
149 }
150
151 return -1;
152}
153
154static void rs_rate_scale_perform(struct iwl_mvm *mvm,
155 struct sk_buff *skb,
156 struct ieee80211_sta *sta,
157 struct iwl_lq_sta *lq_sta);
158static void rs_fill_link_cmd(struct iwl_mvm *mvm,
159 struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
160static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
161
162
163#ifdef CONFIG_MAC80211_DEBUGFS
164static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
165 u32 *rate_n_flags, int index);
166#else
167static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
168 u32 *rate_n_flags, int index)
169{}
170#endif
171
172/**
173 * The following tables contain the expected throughput metrics for all rates
174 *
175 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
176 *
177 * where invalid entries are zeros.
178 *
179 * CCK rates are only valid in legacy table and will only be used in G
180 * (2.4 GHz) band.
181 */
182
183static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
184 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
185};
186
187static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
188 {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
189 {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
190 {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
191 {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
192};
193
194static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
195 {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
196 {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
197 {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
198 {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
199};
200
201static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
202 {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
203 {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
204 {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
205 {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
206};
207
208static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
209 {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
210 {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
211 {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
212 {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
213};
214
215static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
216 {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
217 {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
218 {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
219 {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
220};
221
222static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
223 {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
224 {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
225 {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
226 {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
227};
228
229/* mbps, mcs */
230static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
231 { "1", "BPSK DSSS"},
232 { "2", "QPSK DSSS"},
233 {"5.5", "BPSK CCK"},
234 { "11", "QPSK CCK"},
235 { "6", "BPSK 1/2"},
236 { "9", "BPSK 1/2"},
237 { "12", "QPSK 1/2"},
238 { "18", "QPSK 3/4"},
239 { "24", "16QAM 1/2"},
240 { "36", "16QAM 3/4"},
241 { "48", "64QAM 2/3"},
242 { "54", "64QAM 3/4"},
243 { "60", "64QAM 5/6"},
244};
245
246#define MCS_INDEX_PER_STREAM (8)
247
248static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
249{
250 window->data = 0;
251 window->success_counter = 0;
252 window->success_ratio = IWL_INVALID_VALUE;
253 window->counter = 0;
254 window->average_tpt = IWL_INVALID_VALUE;
255 window->stamp = 0;
256}
257
258static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
259{
260 return (ant_type & valid_antenna) == ant_type;
261}
262
263/*
264 * removes the old data from the statistics. All data that is older than
265 * TID_MAX_TIME_DIFF, will be deleted.
266 */
267static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
268{
269 /* The oldest age we want to keep */
270 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
271
272 while (tl->queue_count &&
273 (tl->time_stamp < oldest_time)) {
274 tl->total -= tl->packet_count[tl->head];
275 tl->packet_count[tl->head] = 0;
276 tl->time_stamp += TID_QUEUE_CELL_SPACING;
277 tl->queue_count--;
278 tl->head++;
279 if (tl->head >= TID_QUEUE_MAX_SIZE)
280 tl->head = 0;
281 }
282}
283
284/*
285 * increment traffic load value for tid and also remove
286 * any old values if passed the certain time period
287 */
288static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
289 struct ieee80211_hdr *hdr)
290{
291 u32 curr_time = jiffies_to_msecs(jiffies);
292 u32 time_diff;
293 s32 index;
294 struct iwl_traffic_load *tl = NULL;
295 u8 tid;
296
297 if (ieee80211_is_data_qos(hdr->frame_control)) {
298 u8 *qc = ieee80211_get_qos_ctl(hdr);
299 tid = qc[0] & 0xf;
300 } else {
301 return IWL_MAX_TID_COUNT;
302 }
303
304 if (unlikely(tid >= IWL_MAX_TID_COUNT))
305 return IWL_MAX_TID_COUNT;
306
307 tl = &lq_data->load[tid];
308
309 curr_time -= curr_time % TID_ROUND_VALUE;
310
311 /* Happens only for the first packet. Initialize the data */
312 if (!(tl->queue_count)) {
313 tl->total = 1;
314 tl->time_stamp = curr_time;
315 tl->queue_count = 1;
316 tl->head = 0;
317 tl->packet_count[0] = 1;
318 return IWL_MAX_TID_COUNT;
319 }
320
321 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
322 index = time_diff / TID_QUEUE_CELL_SPACING;
323
324 /* The history is too long: remove data that is older than */
325 /* TID_MAX_TIME_DIFF */
326 if (index >= TID_QUEUE_MAX_SIZE)
327 rs_tl_rm_old_stats(tl, curr_time);
328
329 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
330 tl->packet_count[index] = tl->packet_count[index] + 1;
331 tl->total = tl->total + 1;
332
333 if ((index + 1) > tl->queue_count)
334 tl->queue_count = index + 1;
335
336 return tid;
337}
338
339#ifdef CONFIG_MAC80211_DEBUGFS
340/**
341 * Program the device to use fixed rate for frame transmit
342 * This is for debugging/testing only
343 * once the device start use fixed rate, we need to reload the module
344 * to being back the normal operation.
345 */
346static void rs_program_fix_rate(struct iwl_mvm *mvm,
347 struct iwl_lq_sta *lq_sta)
348{
349 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
350 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
351 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
352 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
353
354 IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
355 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
356
357 if (lq_sta->dbg_fixed_rate) {
358 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
359 iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
360 }
361}
362#endif
363
364/*
365 get the traffic load value for tid
366*/
367static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
368{
369 u32 curr_time = jiffies_to_msecs(jiffies);
370 u32 time_diff;
371 s32 index;
372 struct iwl_traffic_load *tl = NULL;
373
374 if (tid >= IWL_MAX_TID_COUNT)
375 return 0;
376
377 tl = &(lq_data->load[tid]);
378
379 curr_time -= curr_time % TID_ROUND_VALUE;
380
381 if (!(tl->queue_count))
382 return 0;
383
384 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
385 index = time_diff / TID_QUEUE_CELL_SPACING;
386
387 /* The history is too long: remove data that is older than */
388 /* TID_MAX_TIME_DIFF */
389 if (index >= TID_QUEUE_MAX_SIZE)
390 rs_tl_rm_old_stats(tl, curr_time);
391
392 return tl->total;
393}
394
395static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
396 struct iwl_lq_sta *lq_data, u8 tid,
397 struct ieee80211_sta *sta)
398{
399 int ret = -EAGAIN;
400 u32 load;
401
402 load = rs_tl_get_load(lq_data, tid);
403
404 if ((iwlwifi_mod_params.auto_agg) || (load > IWL_AGG_LOAD_THRESHOLD)) {
405 IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
406 sta->addr, tid);
407 ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
408 if (ret == -EAGAIN) {
409 /*
410 * driver and mac80211 is out of sync
411 * this might be cause by reloading firmware
412 * stop the tx ba session here
413 */
414 IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
415 tid);
416 ieee80211_stop_tx_ba_session(sta, tid);
417 }
418 } else {
419 IWL_DEBUG_HT(mvm,
420 "Aggregation not enabled for tid %d because load = %u\n",
421 tid, load);
422 }
423 return ret;
424}
425
426static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
427 struct iwl_lq_sta *lq_data,
428 struct ieee80211_sta *sta)
429{
430 if (tid < IWL_MAX_TID_COUNT)
431 rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
432 else
433 IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
434 tid, IWL_MAX_TID_COUNT);
435}
436
437static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
438{
439 return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
440 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
441 !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
442}
443
444/*
445 * Static function to get the expected throughput from an iwl_scale_tbl_info
446 * that wraps a NULL pointer check
447 */
448static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
449{
450 if (tbl->expected_tpt)
451 return tbl->expected_tpt[rs_index];
452 return 0;
453}
454
455/**
456 * rs_collect_tx_data - Update the success/failure sliding window
457 *
458 * We keep a sliding window of the last 62 packets transmitted
459 * at this rate. window->data contains the bitmask of successful
460 * packets.
461 */
462static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
463 int scale_index, int attempts, int successes)
464{
465 struct iwl_rate_scale_data *window = NULL;
466 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
467 s32 fail_count, tpt;
468
469 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
470 return -EINVAL;
471
472 /* Select window for current tx bit rate */
473 window = &(tbl->win[scale_index]);
474
475 /* Get expected throughput */
476 tpt = get_expected_tpt(tbl, scale_index);
477
478 /*
479 * Keep track of only the latest 62 tx frame attempts in this rate's
480 * history window; anything older isn't really relevant any more.
481 * If we have filled up the sliding window, drop the oldest attempt;
482 * if the oldest attempt (highest bit in bitmap) shows "success",
483 * subtract "1" from the success counter (this is the main reason
484 * we keep these bitmaps!).
485 */
486 while (attempts > 0) {
487 if (window->counter >= IWL_RATE_MAX_WINDOW) {
488 /* remove earliest */
489 window->counter = IWL_RATE_MAX_WINDOW - 1;
490
491 if (window->data & mask) {
492 window->data &= ~mask;
493 window->success_counter--;
494 }
495 }
496
497 /* Increment frames-attempted counter */
498 window->counter++;
499
500 /* Shift bitmap by one frame to throw away oldest history */
501 window->data <<= 1;
502
503 /* Mark the most recent #successes attempts as successful */
504 if (successes > 0) {
505 window->success_counter++;
506 window->data |= 0x1;
507 successes--;
508 }
509
510 attempts--;
511 }
512
513 /* Calculate current success ratio, avoid divide-by-0! */
514 if (window->counter > 0)
515 window->success_ratio = 128 * (100 * window->success_counter)
516 / window->counter;
517 else
518 window->success_ratio = IWL_INVALID_VALUE;
519
520 fail_count = window->counter - window->success_counter;
521
522 /* Calculate average throughput, if we have enough history. */
523 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
524 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
525 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
526 else
527 window->average_tpt = IWL_INVALID_VALUE;
528
529 /* Tag this window as having been updated */
530 window->stamp = jiffies;
531
532 return 0;
533}
534
535/*
536 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
537 */
538/* FIXME:RS:remove this function and put the flags statically in the table */
539static u32 rate_n_flags_from_tbl(struct iwl_mvm *mvm,
540 struct iwl_scale_tbl_info *tbl,
541 int index, u8 use_green)
542{
543 u32 rate_n_flags = 0;
544
545 if (is_legacy(tbl->lq_type)) {
546 rate_n_flags = iwl_rates[index].plcp;
547 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
548 rate_n_flags |= RATE_MCS_CCK_MSK;
549 } else if (is_Ht(tbl->lq_type)) {
550 if (index > IWL_LAST_OFDM_RATE) {
551 IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
552 index = IWL_LAST_OFDM_RATE;
553 }
554 rate_n_flags = RATE_MCS_HT_MSK;
555
556 if (is_siso(tbl->lq_type))
557 rate_n_flags |= iwl_rates[index].plcp_siso;
558 else if (is_mimo2(tbl->lq_type))
559 rate_n_flags |= iwl_rates[index].plcp_mimo2;
560 else
561 rate_n_flags |= iwl_rates[index].plcp_mimo3;
562 } else {
563 IWL_ERR(mvm, "Invalid tbl->lq_type %d\n", tbl->lq_type);
564 }
565
566 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
567 RATE_MCS_ANT_ABC_MSK);
568
569 if (is_Ht(tbl->lq_type)) {
570 if (tbl->is_ht40)
571 rate_n_flags |= RATE_MCS_CHAN_WIDTH_40;
572 if (tbl->is_SGI)
573 rate_n_flags |= RATE_MCS_SGI_MSK;
574
575 if (use_green) {
576 rate_n_flags |= RATE_HT_MCS_GF_MSK;
577 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
578 rate_n_flags &= ~RATE_MCS_SGI_MSK;
579 IWL_ERR(mvm, "GF was set with SGI:SISO\n");
580 }
581 }
582 }
583 return rate_n_flags;
584}
585
586/*
587 * Interpret uCode API's rate_n_flags format,
588 * fill "search" or "active" tx mode table.
589 */
590static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
591 enum ieee80211_band band,
592 struct iwl_scale_tbl_info *tbl,
593 int *rate_idx)
594{
595 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
596 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
597 u8 mcs;
598
599 memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
600 *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
601
602 if (*rate_idx == IWL_RATE_INVALID) {
603 *rate_idx = -1;
604 return -EINVAL;
605 }
606 tbl->is_SGI = 0; /* default legacy setup */
607 tbl->is_ht40 = 0;
608 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
609 tbl->lq_type = LQ_NONE;
610 tbl->max_search = IWL_MAX_SEARCH;
611
612 /* legacy rate format */
613 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
614 if (num_of_ant == 1) {
615 if (band == IEEE80211_BAND_5GHZ)
616 tbl->lq_type = LQ_A;
617 else
618 tbl->lq_type = LQ_G;
619 }
620 /* HT rate format */
621 } else {
622 if (rate_n_flags & RATE_MCS_SGI_MSK)
623 tbl->is_SGI = 1;
624
625 if (rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
626 tbl->is_ht40 = 1;
627
628 mcs = rs_extract_rate(rate_n_flags);
629
630 /* SISO */
631 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
632 if (num_of_ant == 1)
633 tbl->lq_type = LQ_SISO; /*else NONE*/
634 /* MIMO2 */
635 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
636 if (num_of_ant == 2)
637 tbl->lq_type = LQ_MIMO2;
638 /* MIMO3 */
639 } else {
640 if (num_of_ant == 3) {
641 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
642 tbl->lq_type = LQ_MIMO3;
643 }
644 }
645 }
646 return 0;
647}
648
649/* switch to another antenna/antennas and return 1 */
650/* if no other valid antenna found, return 0 */
651static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
652 struct iwl_scale_tbl_info *tbl)
653{
654 u8 new_ant_type;
655
656 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
657 return 0;
658
659 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
660 return 0;
661
662 new_ant_type = ant_toggle_lookup[tbl->ant_type];
663
664 while ((new_ant_type != tbl->ant_type) &&
665 !rs_is_valid_ant(valid_ant, new_ant_type))
666 new_ant_type = ant_toggle_lookup[new_ant_type];
667
668 if (new_ant_type == tbl->ant_type)
669 return 0;
670
671 tbl->ant_type = new_ant_type;
672 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
673 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
674 return 1;
675}
676
677/**
678 * Green-field mode is valid if the station supports it and
679 * there are no non-GF stations present in the BSS.
680 */
681static bool rs_use_green(struct ieee80211_sta *sta)
682{
Beni Lev6e6cc9f2013-02-19 14:40:11 +0200683 /*
684 * There's a bug somewhere in this code that causes the
685 * scaling to get stuck because GF+SGI can't be combined
686 * in SISO rates. Until we find that bug, disable GF, it
687 * has only limited benefit and we still interoperate with
688 * GF APs since we can always receive GF transmissions.
689 */
690 return false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100691}
692
693/**
694 * rs_get_supported_rates - get the available rates
695 *
696 * if management frame or broadcast frame only return
697 * basic available rates.
698 *
699 */
700static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
701 struct ieee80211_hdr *hdr,
702 enum iwl_table_type rate_type)
703{
704 if (is_legacy(rate_type)) {
705 return lq_sta->active_legacy_rate;
706 } else {
707 if (is_siso(rate_type))
708 return lq_sta->active_siso_rate;
709 else if (is_mimo2(rate_type))
710 return lq_sta->active_mimo2_rate;
711 else
712 return lq_sta->active_mimo3_rate;
713 }
714}
715
716static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
717 int rate_type)
718{
719 u8 high = IWL_RATE_INVALID;
720 u8 low = IWL_RATE_INVALID;
721
722 /* 802.11A or ht walks to the next literal adjacent rate in
723 * the rate table */
724 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
725 int i;
726 u32 mask;
727
728 /* Find the previous rate that is in the rate mask */
729 i = index - 1;
730 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
731 if (rate_mask & mask) {
732 low = i;
733 break;
734 }
735 }
736
737 /* Find the next rate that is in the rate mask */
738 i = index + 1;
739 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
740 if (rate_mask & mask) {
741 high = i;
742 break;
743 }
744 }
745
746 return (high << 8) | low;
747 }
748
749 low = index;
750 while (low != IWL_RATE_INVALID) {
751 low = iwl_rates[low].prev_rs;
752 if (low == IWL_RATE_INVALID)
753 break;
754 if (rate_mask & (1 << low))
755 break;
756 IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
757 }
758
759 high = index;
760 while (high != IWL_RATE_INVALID) {
761 high = iwl_rates[high].next_rs;
762 if (high == IWL_RATE_INVALID)
763 break;
764 if (rate_mask & (1 << high))
765 break;
766 IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
767 }
768
769 return (high << 8) | low;
770}
771
772static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
773 struct iwl_scale_tbl_info *tbl,
774 u8 scale_index, u8 ht_possible)
775{
776 s32 low;
777 u16 rate_mask;
778 u16 high_low;
779 u8 switch_to_legacy = 0;
780 u8 is_green = lq_sta->is_green;
781 struct iwl_mvm *mvm = lq_sta->drv;
782
783 /* check if we need to switch from HT to legacy rates.
784 * assumption is that mandatory rates (1Mbps or 6Mbps)
785 * are always supported (spec demand) */
786 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
787 switch_to_legacy = 1;
788 scale_index = rs_ht_to_legacy[scale_index];
789 if (lq_sta->band == IEEE80211_BAND_5GHZ)
790 tbl->lq_type = LQ_A;
791 else
792 tbl->lq_type = LQ_G;
793
794 if (num_of_ant(tbl->ant_type) > 1)
795 tbl->ant_type =
Emmanuel Grumbachff402312013-04-03 20:10:06 +0300796 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100797
798 tbl->is_ht40 = 0;
799 tbl->is_SGI = 0;
800 tbl->max_search = IWL_MAX_SEARCH;
801 }
802
803 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
804
805 /* Mask with station rate restriction */
806 if (is_legacy(tbl->lq_type)) {
807 /* supp_rates has no CCK bits in A mode */
808 if (lq_sta->band == IEEE80211_BAND_5GHZ)
809 rate_mask = (u16)(rate_mask &
810 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
811 else
812 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
813 }
814
815 /* If we switched from HT to legacy, check current rate */
816 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
817 low = scale_index;
818 goto out;
819 }
820
821 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
822 tbl->lq_type);
823 low = high_low & 0xff;
824
825 if (low == IWL_RATE_INVALID)
826 low = scale_index;
827
828out:
829 return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
830}
831
832/*
833 * Simple function to compare two rate scale table types
834 */
835static bool table_type_matches(struct iwl_scale_tbl_info *a,
836 struct iwl_scale_tbl_info *b)
837{
838 return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
839 (a->is_SGI == b->is_SGI);
840}
841
842/*
843 * mac80211 sends us Tx status
844 */
845static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
846 struct ieee80211_sta *sta, void *priv_sta,
847 struct sk_buff *skb)
848{
849 int legacy_success;
850 int retries;
851 int rs_index, mac_index, i;
852 struct iwl_lq_sta *lq_sta = priv_sta;
853 struct iwl_lq_cmd *table;
854 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
855 struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
856 struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
857 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
858 enum mac80211_rate_control_flags mac_flags;
859 u32 tx_rate;
860 struct iwl_scale_tbl_info tbl_type;
861 struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
862
863 IWL_DEBUG_RATE_LIMIT(mvm,
864 "get frame ack response, update rate scale window\n");
865
866 /* Treat uninitialized rate scaling data same as non-existing. */
867 if (!lq_sta) {
868 IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
869 return;
870 } else if (!lq_sta->drv) {
871 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
872 return;
873 }
874
875 if (!ieee80211_is_data(hdr->frame_control) ||
876 info->flags & IEEE80211_TX_CTL_NO_ACK)
877 return;
878
879 /* This packet was aggregated but doesn't carry status info */
880 if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
881 !(info->flags & IEEE80211_TX_STAT_AMPDU))
882 return;
883
884 /*
885 * Ignore this Tx frame response if its initial rate doesn't match
886 * that of latest Link Quality command. There may be stragglers
887 * from a previous Link Quality command, but we're no longer interested
888 * in those; they're either from the "active" mode while we're trying
889 * to check "search" mode, or a prior "search" mode after we've moved
890 * to a new "search" mode (which might become the new "active" mode).
891 */
892 table = &lq_sta->lq;
893 tx_rate = le32_to_cpu(table->rs_table[0]);
894 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type, &rs_index);
895 if (info->band == IEEE80211_BAND_5GHZ)
896 rs_index -= IWL_FIRST_OFDM_RATE;
897 mac_flags = info->status.rates[0].flags;
898 mac_index = info->status.rates[0].idx;
899 /* For HT packets, map MCS to PLCP */
900 if (mac_flags & IEEE80211_TX_RC_MCS) {
901 /* Remove # of streams */
902 mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
903 if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
904 mac_index++;
905 /*
906 * mac80211 HT index is always zero-indexed; we need to move
907 * HT OFDM rates after CCK rates in 2.4 GHz band
908 */
909 if (info->band == IEEE80211_BAND_2GHZ)
910 mac_index += IWL_FIRST_OFDM_RATE;
911 }
912 /* Here we actually compare this rate to the latest LQ command */
913 if ((mac_index < 0) ||
914 (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
915 (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
916 (tbl_type.ant_type != info->status.antenna) ||
917 (!!(tx_rate & RATE_MCS_HT_MSK) !=
918 !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
919 (!!(tx_rate & RATE_HT_MCS_GF_MSK) !=
920 !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
921 (rs_index != mac_index)) {
922 IWL_DEBUG_RATE(mvm,
923 "initial rate %d does not match %d (0x%x)\n",
924 mac_index, rs_index, tx_rate);
925 /*
926 * Since rates mis-match, the last LQ command may have failed.
927 * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
928 * ... driver.
929 */
930 lq_sta->missed_rate_counter++;
931 if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
932 lq_sta->missed_rate_counter = 0;
933 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
934 }
935 /* Regardless, ignore this status info for outdated rate */
936 return;
937 } else
938 /* Rate did match, so reset the missed_rate_counter */
939 lq_sta->missed_rate_counter = 0;
940
941 /* Figure out if rate scale algorithm is in active or search table */
942 if (table_type_matches(&tbl_type,
943 &(lq_sta->lq_info[lq_sta->active_tbl]))) {
944 curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
945 other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
946 } else if (table_type_matches(
947 &tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
948 curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
949 other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
950 } else {
951 IWL_DEBUG_RATE(mvm,
952 "Neither active nor search matches tx rate\n");
953 tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
954 IWL_DEBUG_RATE(mvm, "active- lq:%x, ant:%x, SGI:%d\n",
955 tmp_tbl->lq_type, tmp_tbl->ant_type,
956 tmp_tbl->is_SGI);
957 tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
958 IWL_DEBUG_RATE(mvm, "search- lq:%x, ant:%x, SGI:%d\n",
959 tmp_tbl->lq_type, tmp_tbl->ant_type,
960 tmp_tbl->is_SGI);
961 IWL_DEBUG_RATE(mvm, "actual- lq:%x, ant:%x, SGI:%d\n",
962 tbl_type.lq_type, tbl_type.ant_type,
963 tbl_type.is_SGI);
964 /*
965 * no matching table found, let's by-pass the data collection
966 * and continue to perform rate scale to find the rate table
967 */
968 rs_stay_in_table(lq_sta, true);
969 goto done;
970 }
971
972 /*
973 * Updating the frame history depends on whether packets were
974 * aggregated.
975 *
976 * For aggregation, all packets were transmitted at the same rate, the
977 * first index into rate scale table.
978 */
979 if (info->flags & IEEE80211_TX_STAT_AMPDU) {
980 tx_rate = le32_to_cpu(table->rs_table[0]);
981 rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type,
982 &rs_index);
983 rs_collect_tx_data(curr_tbl, rs_index,
984 info->status.ampdu_len,
985 info->status.ampdu_ack_len);
986
987 /* Update success/fail counts if not searching for new mode */
988 if (lq_sta->stay_in_tbl) {
989 lq_sta->total_success += info->status.ampdu_ack_len;
990 lq_sta->total_failed += (info->status.ampdu_len -
991 info->status.ampdu_ack_len);
992 }
993 } else {
994 /*
995 * For legacy, update frame history with for each Tx retry.
996 */
997 retries = info->status.rates[0].count - 1;
998 /* HW doesn't send more than 15 retries */
999 retries = min(retries, 15);
1000
1001 /* The last transmission may have been successful */
1002 legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
1003 /* Collect data for each rate used during failed TX attempts */
1004 for (i = 0; i <= retries; ++i) {
1005 tx_rate = le32_to_cpu(table->rs_table[i]);
1006 rs_get_tbl_info_from_mcs(tx_rate, info->band,
1007 &tbl_type, &rs_index);
1008 /*
1009 * Only collect stats if retried rate is in the same RS
1010 * table as active/search.
1011 */
1012 if (table_type_matches(&tbl_type, curr_tbl))
1013 tmp_tbl = curr_tbl;
1014 else if (table_type_matches(&tbl_type, other_tbl))
1015 tmp_tbl = other_tbl;
1016 else
1017 continue;
1018 rs_collect_tx_data(tmp_tbl, rs_index, 1,
1019 i < retries ? 0 : legacy_success);
1020 }
1021
1022 /* Update success/fail counts if not searching for new mode */
1023 if (lq_sta->stay_in_tbl) {
1024 lq_sta->total_success += legacy_success;
1025 lq_sta->total_failed += retries + (1 - legacy_success);
1026 }
1027 }
1028 /* The last TX rate is cached in lq_sta; it's set in if/else above */
1029 lq_sta->last_rate_n_flags = tx_rate;
1030done:
1031 /* See if there's a better rate or modulation mode to try. */
1032 if (sta && sta->supp_rates[sband->band])
1033 rs_rate_scale_perform(mvm, skb, sta, lq_sta);
1034}
1035
1036/*
1037 * Begin a period of staying with a selected modulation mode.
1038 * Set "stay_in_tbl" flag to prevent any mode switches.
1039 * Set frame tx success limits according to legacy vs. high-throughput,
1040 * and reset overall (spanning all rates) tx success history statistics.
1041 * These control how long we stay using same modulation mode before
1042 * searching for a new mode.
1043 */
1044static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
1045 struct iwl_lq_sta *lq_sta)
1046{
1047 IWL_DEBUG_RATE(mvm, "we are staying in the same table\n");
1048 lq_sta->stay_in_tbl = 1; /* only place this gets set */
1049 if (is_legacy) {
1050 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1051 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1052 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
1053 } else {
1054 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1055 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1056 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
1057 }
1058 lq_sta->table_count = 0;
1059 lq_sta->total_failed = 0;
1060 lq_sta->total_success = 0;
1061 lq_sta->flush_timer = jiffies;
1062 lq_sta->action_counter = 0;
1063}
1064
1065/*
1066 * Find correct throughput table for given mode of modulation
1067 */
1068static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
1069 struct iwl_scale_tbl_info *tbl)
1070{
1071 /* Used to choose among HT tables */
1072 s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
1073
1074 /* Check for invalid LQ type */
1075 if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
1076 tbl->expected_tpt = expected_tpt_legacy;
1077 return;
1078 }
1079
1080 /* Legacy rates have only one table */
1081 if (is_legacy(tbl->lq_type)) {
1082 tbl->expected_tpt = expected_tpt_legacy;
1083 return;
1084 }
1085
1086 /* Choose among many HT tables depending on number of streams
1087 * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
1088 * status */
1089 if (is_siso(tbl->lq_type) && !tbl->is_ht40)
1090 ht_tbl_pointer = expected_tpt_siso20MHz;
1091 else if (is_siso(tbl->lq_type))
1092 ht_tbl_pointer = expected_tpt_siso40MHz;
1093 else if (is_mimo2(tbl->lq_type) && !tbl->is_ht40)
1094 ht_tbl_pointer = expected_tpt_mimo2_20MHz;
1095 else if (is_mimo2(tbl->lq_type))
1096 ht_tbl_pointer = expected_tpt_mimo2_40MHz;
1097 else if (is_mimo3(tbl->lq_type) && !tbl->is_ht40)
1098 ht_tbl_pointer = expected_tpt_mimo3_20MHz;
1099 else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
1100 ht_tbl_pointer = expected_tpt_mimo3_40MHz;
1101
1102 if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
1103 tbl->expected_tpt = ht_tbl_pointer[0];
1104 else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
1105 tbl->expected_tpt = ht_tbl_pointer[1];
1106 else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
1107 tbl->expected_tpt = ht_tbl_pointer[2];
1108 else /* AGG+SGI */
1109 tbl->expected_tpt = ht_tbl_pointer[3];
1110}
1111
1112/*
1113 * Find starting rate for new "search" high-throughput mode of modulation.
1114 * Goal is to find lowest expected rate (under perfect conditions) that is
1115 * above the current measured throughput of "active" mode, to give new mode
1116 * a fair chance to prove itself without too many challenges.
1117 *
1118 * This gets called when transitioning to more aggressive modulation
1119 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1120 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1121 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1122 * bit rate will typically need to increase, but not if performance was bad.
1123 */
1124static s32 rs_get_best_rate(struct iwl_mvm *mvm,
1125 struct iwl_lq_sta *lq_sta,
1126 struct iwl_scale_tbl_info *tbl, /* "search" */
1127 u16 rate_mask, s8 index)
1128{
1129 /* "active" values */
1130 struct iwl_scale_tbl_info *active_tbl =
1131 &(lq_sta->lq_info[lq_sta->active_tbl]);
1132 s32 active_sr = active_tbl->win[index].success_ratio;
1133 s32 active_tpt = active_tbl->expected_tpt[index];
1134
1135 /* expected "search" throughput */
1136 s32 *tpt_tbl = tbl->expected_tpt;
1137
1138 s32 new_rate, high, low, start_hi;
1139 u16 high_low;
1140 s8 rate = index;
1141
1142 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1143
1144 while (1) {
1145 high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
1146 tbl->lq_type);
1147
1148 low = high_low & 0xff;
1149 high = (high_low >> 8) & 0xff;
1150
1151 /*
1152 * Lower the "search" bit rate, to give new "search" mode
1153 * approximately the same throughput as "active" if:
1154 *
1155 * 1) "Active" mode has been working modestly well (but not
1156 * great), and expected "search" throughput (under perfect
1157 * conditions) at candidate rate is above the actual
1158 * measured "active" throughput (but less than expected
1159 * "active" throughput under perfect conditions).
1160 * OR
1161 * 2) "Active" mode has been working perfectly or very well
1162 * and expected "search" throughput (under perfect
1163 * conditions) at candidate rate is above expected
1164 * "active" throughput (under perfect conditions).
1165 */
1166 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
1167 ((active_sr > IWL_RATE_DECREASE_TH) &&
1168 (active_sr <= IWL_RATE_HIGH_TH) &&
1169 (tpt_tbl[rate] <= active_tpt))) ||
1170 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1171 (tpt_tbl[rate] > active_tpt))) {
1172 /* (2nd or later pass)
1173 * If we've already tried to raise the rate, and are
1174 * now trying to lower it, use the higher rate. */
1175 if (start_hi != IWL_RATE_INVALID) {
1176 new_rate = start_hi;
1177 break;
1178 }
1179
1180 new_rate = rate;
1181
1182 /* Loop again with lower rate */
1183 if (low != IWL_RATE_INVALID)
1184 rate = low;
1185
1186 /* Lower rate not available, use the original */
1187 else
1188 break;
1189
1190 /* Else try to raise the "search" rate to match "active" */
1191 } else {
1192 /* (2nd or later pass)
1193 * If we've already tried to lower the rate, and are
1194 * now trying to raise it, use the lower rate. */
1195 if (new_rate != IWL_RATE_INVALID)
1196 break;
1197
1198 /* Loop again with higher rate */
1199 else if (high != IWL_RATE_INVALID) {
1200 start_hi = high;
1201 rate = high;
1202
1203 /* Higher rate not available, use the original */
1204 } else {
1205 new_rate = rate;
1206 break;
1207 }
1208 }
1209 }
1210
1211 return new_rate;
1212}
1213
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001214static bool iwl_is_ht40_tx_allowed(struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001215{
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001216 return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001217}
1218
1219/*
1220 * Set up search table for MIMO2
1221 */
1222static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
1223 struct iwl_lq_sta *lq_sta,
1224 struct ieee80211_sta *sta,
1225 struct iwl_scale_tbl_info *tbl, int index)
1226{
1227 u16 rate_mask;
1228 s32 rate;
1229 s8 is_green = lq_sta->is_green;
1230
1231 if (!sta->ht_cap.ht_supported)
1232 return -1;
1233
Johannes Bergaf0ed692013-02-12 14:21:00 +01001234 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001235 return -1;
1236
1237 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001238 if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239 return -1;
1240
1241 IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
1242
1243 tbl->lq_type = LQ_MIMO2;
1244 tbl->action = 0;
1245 tbl->max_search = IWL_MAX_SEARCH;
1246 rate_mask = lq_sta->active_mimo2_rate;
1247
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001248 if (iwl_is_ht40_tx_allowed(sta))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001249 tbl->is_ht40 = 1;
1250 else
1251 tbl->is_ht40 = 0;
1252
1253 rs_set_expected_tpt_table(lq_sta, tbl);
1254
1255 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1256
1257 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
1258 rate, rate_mask);
1259 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1260 IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
1261 rate, rate_mask);
1262 return -1;
1263 }
1264 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
1265
1266 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
1267 tbl->current_rate, is_green);
1268 return 0;
1269}
1270
1271/*
1272 * Set up search table for MIMO3
1273 */
1274static int rs_switch_to_mimo3(struct iwl_mvm *mvm,
1275 struct iwl_lq_sta *lq_sta,
1276 struct ieee80211_sta *sta,
1277 struct iwl_scale_tbl_info *tbl, int index)
1278{
1279 u16 rate_mask;
1280 s32 rate;
1281 s8 is_green = lq_sta->is_green;
1282
1283 if (!sta->ht_cap.ht_supported)
1284 return -1;
1285
Johannes Bergaf0ed692013-02-12 14:21:00 +01001286 if (sta->smps_mode == IEEE80211_SMPS_STATIC)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001287 return -1;
1288
1289 /* Need both Tx chains/antennas to support MIMO */
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001290 if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 3)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001291 return -1;
1292
1293 IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO3\n");
1294
1295 tbl->lq_type = LQ_MIMO3;
1296 tbl->action = 0;
1297 tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
1298 rate_mask = lq_sta->active_mimo3_rate;
1299
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001300 if (iwl_is_ht40_tx_allowed(sta))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001301 tbl->is_ht40 = 1;
1302 else
1303 tbl->is_ht40 = 0;
1304
1305 rs_set_expected_tpt_table(lq_sta, tbl);
1306
1307 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1308
1309 IWL_DEBUG_RATE(mvm, "LQ: MIMO3 best rate %d mask %X\n",
1310 rate, rate_mask);
1311 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1312 IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
1313 rate, rate_mask);
1314 return -1;
1315 }
1316 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
1317
1318 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
1319 tbl->current_rate, is_green);
1320 return 0;
1321}
1322
1323/*
1324 * Set up search table for SISO
1325 */
1326static int rs_switch_to_siso(struct iwl_mvm *mvm,
1327 struct iwl_lq_sta *lq_sta,
1328 struct ieee80211_sta *sta,
1329 struct iwl_scale_tbl_info *tbl, int index)
1330{
1331 u16 rate_mask;
1332 u8 is_green = lq_sta->is_green;
1333 s32 rate;
1334
1335 if (!sta->ht_cap.ht_supported)
1336 return -1;
1337
1338 IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
1339
1340 tbl->lq_type = LQ_SISO;
1341 tbl->action = 0;
1342 tbl->max_search = IWL_MAX_SEARCH;
1343 rate_mask = lq_sta->active_siso_rate;
1344
Johannes Berge1a0c6b2013-02-07 11:47:44 +01001345 if (iwl_is_ht40_tx_allowed(sta))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001346 tbl->is_ht40 = 1;
1347 else
1348 tbl->is_ht40 = 0;
1349
1350 if (is_green)
1351 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
1352
1353 rs_set_expected_tpt_table(lq_sta, tbl);
1354 rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
1355
1356 IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n", rate, rate_mask);
1357 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1358 IWL_DEBUG_RATE(mvm,
1359 "can not switch with index %d rate mask %x\n",
1360 rate, rate_mask);
1361 return -1;
1362 }
1363 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
1364 IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
1365 tbl->current_rate, is_green);
1366 return 0;
1367}
1368
1369/*
1370 * Try to switch to new modulation mode from legacy
1371 */
1372static int rs_move_legacy_other(struct iwl_mvm *mvm,
1373 struct iwl_lq_sta *lq_sta,
1374 struct ieee80211_sta *sta,
1375 int index)
1376{
1377 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1378 struct iwl_scale_tbl_info *search_tbl =
1379 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1380 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1381 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1382 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1383 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001384 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001385 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1386 int ret;
1387 u8 update_search_tbl_counter = 0;
1388
1389 start_action = tbl->action;
1390 while (1) {
1391 lq_sta->action_counter++;
1392 switch (tbl->action) {
1393 case IWL_LEGACY_SWITCH_ANTENNA1:
1394 case IWL_LEGACY_SWITCH_ANTENNA2:
1395 IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
1396
1397 if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
1398 tx_chains_num <= 1) ||
1399 (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
1400 tx_chains_num <= 2))
1401 break;
1402
1403 /* Don't change antenna if success has been great */
1404 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1405 break;
1406
1407 /* Set up search table to try other antenna */
1408 memcpy(search_tbl, tbl, sz);
1409
1410 if (rs_toggle_antenna(valid_tx_ant,
1411 &search_tbl->current_rate,
1412 search_tbl)) {
1413 update_search_tbl_counter = 1;
1414 rs_set_expected_tpt_table(lq_sta, search_tbl);
1415 goto out;
1416 }
1417 break;
1418 case IWL_LEGACY_SWITCH_SISO:
1419 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
1420
1421 /* Set up search table to try SISO */
1422 memcpy(search_tbl, tbl, sz);
1423 search_tbl->is_SGI = 0;
1424 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1425 search_tbl, index);
1426 if (!ret) {
1427 lq_sta->action_counter = 0;
1428 goto out;
1429 }
1430
1431 break;
1432 case IWL_LEGACY_SWITCH_MIMO2_AB:
1433 case IWL_LEGACY_SWITCH_MIMO2_AC:
1434 case IWL_LEGACY_SWITCH_MIMO2_BC:
1435 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
1436
1437 /* Set up search table to try MIMO */
1438 memcpy(search_tbl, tbl, sz);
1439 search_tbl->is_SGI = 0;
1440
1441 if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
1442 search_tbl->ant_type = ANT_AB;
1443 else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
1444 search_tbl->ant_type = ANT_AC;
1445 else
1446 search_tbl->ant_type = ANT_BC;
1447
1448 if (!rs_is_valid_ant(valid_tx_ant,
1449 search_tbl->ant_type))
1450 break;
1451
1452 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1453 search_tbl, index);
1454 if (!ret) {
1455 lq_sta->action_counter = 0;
1456 goto out;
1457 }
1458 break;
1459
1460 case IWL_LEGACY_SWITCH_MIMO3_ABC:
1461 IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO3\n");
1462
1463 /* Set up search table to try MIMO3 */
1464 memcpy(search_tbl, tbl, sz);
1465 search_tbl->is_SGI = 0;
1466
1467 search_tbl->ant_type = ANT_ABC;
1468
1469 if (!rs_is_valid_ant(valid_tx_ant,
1470 search_tbl->ant_type))
1471 break;
1472
1473 ret = rs_switch_to_mimo3(mvm, lq_sta, sta,
1474 search_tbl, index);
1475 if (!ret) {
1476 lq_sta->action_counter = 0;
1477 goto out;
1478 }
1479 break;
1480 }
1481 tbl->action++;
1482 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1483 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1484
1485 if (tbl->action == start_action)
1486 break;
1487 }
1488 search_tbl->lq_type = LQ_NONE;
1489 return 0;
1490
1491out:
1492 lq_sta->search_better_tbl = 1;
1493 tbl->action++;
1494 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1495 tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
1496 if (update_search_tbl_counter)
1497 search_tbl->action = tbl->action;
1498 return 0;
1499}
1500
1501/*
1502 * Try to switch to new modulation mode from SISO
1503 */
1504static int rs_move_siso_to_other(struct iwl_mvm *mvm,
1505 struct iwl_lq_sta *lq_sta,
1506 struct ieee80211_sta *sta, int index)
1507{
1508 u8 is_green = lq_sta->is_green;
1509 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1510 struct iwl_scale_tbl_info *search_tbl =
1511 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1512 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1513 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1514 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1515 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1516 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001517 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001518 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1519 u8 update_search_tbl_counter = 0;
1520 int ret;
1521
1522 start_action = tbl->action;
1523 while (1) {
1524 lq_sta->action_counter++;
1525 switch (tbl->action) {
1526 case IWL_SISO_SWITCH_ANTENNA1:
1527 case IWL_SISO_SWITCH_ANTENNA2:
1528 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
1529 if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
1530 tx_chains_num <= 1) ||
1531 (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
1532 tx_chains_num <= 2))
1533 break;
1534
1535 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1536 break;
1537
1538 memcpy(search_tbl, tbl, sz);
1539 if (rs_toggle_antenna(valid_tx_ant,
1540 &search_tbl->current_rate,
1541 search_tbl)) {
1542 update_search_tbl_counter = 1;
1543 goto out;
1544 }
1545 break;
1546 case IWL_SISO_SWITCH_MIMO2_AB:
1547 case IWL_SISO_SWITCH_MIMO2_AC:
1548 case IWL_SISO_SWITCH_MIMO2_BC:
1549 IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO2\n");
1550 memcpy(search_tbl, tbl, sz);
1551 search_tbl->is_SGI = 0;
1552
1553 if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
1554 search_tbl->ant_type = ANT_AB;
1555 else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
1556 search_tbl->ant_type = ANT_AC;
1557 else
1558 search_tbl->ant_type = ANT_BC;
1559
1560 if (!rs_is_valid_ant(valid_tx_ant,
1561 search_tbl->ant_type))
1562 break;
1563
1564 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1565 search_tbl, index);
1566 if (!ret)
1567 goto out;
1568 break;
1569 case IWL_SISO_SWITCH_GI:
1570 if (!tbl->is_ht40 && !(ht_cap->cap &
1571 IEEE80211_HT_CAP_SGI_20))
1572 break;
1573 if (tbl->is_ht40 && !(ht_cap->cap &
1574 IEEE80211_HT_CAP_SGI_40))
1575 break;
1576
1577 IWL_DEBUG_RATE(mvm, "LQ: SISO toggle SGI/NGI\n");
1578
1579 memcpy(search_tbl, tbl, sz);
1580 if (is_green) {
1581 if (!tbl->is_SGI)
1582 break;
1583 else
1584 IWL_ERR(mvm,
1585 "SGI was set in GF+SISO\n");
1586 }
1587 search_tbl->is_SGI = !tbl->is_SGI;
1588 rs_set_expected_tpt_table(lq_sta, search_tbl);
1589 if (tbl->is_SGI) {
1590 s32 tpt = lq_sta->last_tpt / 100;
1591 if (tpt >= search_tbl->expected_tpt[index])
1592 break;
1593 }
1594 search_tbl->current_rate =
1595 rate_n_flags_from_tbl(mvm, search_tbl,
1596 index, is_green);
1597 update_search_tbl_counter = 1;
1598 goto out;
1599 case IWL_SISO_SWITCH_MIMO3_ABC:
1600 IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO3\n");
1601 memcpy(search_tbl, tbl, sz);
1602 search_tbl->is_SGI = 0;
1603 search_tbl->ant_type = ANT_ABC;
1604
1605 if (!rs_is_valid_ant(valid_tx_ant,
1606 search_tbl->ant_type))
1607 break;
1608
1609 ret = rs_switch_to_mimo3(mvm, lq_sta, sta,
1610 search_tbl, index);
1611 if (!ret)
1612 goto out;
1613 break;
1614 }
1615 tbl->action++;
1616 if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
1617 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1618
1619 if (tbl->action == start_action)
1620 break;
1621 }
1622 search_tbl->lq_type = LQ_NONE;
1623 return 0;
1624
1625 out:
1626 lq_sta->search_better_tbl = 1;
1627 tbl->action++;
1628 if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
1629 tbl->action = IWL_SISO_SWITCH_ANTENNA1;
1630 if (update_search_tbl_counter)
1631 search_tbl->action = tbl->action;
1632
1633 return 0;
1634}
1635
1636/*
1637 * Try to switch to new modulation mode from MIMO2
1638 */
1639static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
1640 struct iwl_lq_sta *lq_sta,
1641 struct ieee80211_sta *sta, int index)
1642{
1643 s8 is_green = lq_sta->is_green;
1644 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1645 struct iwl_scale_tbl_info *search_tbl =
1646 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1647 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1648 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1649 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1650 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1651 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001652 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001653 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1654 u8 update_search_tbl_counter = 0;
1655 int ret;
1656
1657 start_action = tbl->action;
1658 while (1) {
1659 lq_sta->action_counter++;
1660 switch (tbl->action) {
1661 case IWL_MIMO2_SWITCH_ANTENNA1:
1662 case IWL_MIMO2_SWITCH_ANTENNA2:
1663 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle Antennas\n");
1664
1665 if (tx_chains_num <= 2)
1666 break;
1667
1668 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1669 break;
1670
1671 memcpy(search_tbl, tbl, sz);
1672 if (rs_toggle_antenna(valid_tx_ant,
1673 &search_tbl->current_rate,
1674 search_tbl)) {
1675 update_search_tbl_counter = 1;
1676 goto out;
1677 }
1678 break;
1679 case IWL_MIMO2_SWITCH_SISO_A:
1680 case IWL_MIMO2_SWITCH_SISO_B:
1681 case IWL_MIMO2_SWITCH_SISO_C:
1682 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
1683
1684 /* Set up new search table for SISO */
1685 memcpy(search_tbl, tbl, sz);
1686
1687 if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
1688 search_tbl->ant_type = ANT_A;
1689 else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
1690 search_tbl->ant_type = ANT_B;
1691 else
1692 search_tbl->ant_type = ANT_C;
1693
1694 if (!rs_is_valid_ant(valid_tx_ant,
1695 search_tbl->ant_type))
1696 break;
1697
1698 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1699 search_tbl, index);
1700 if (!ret)
1701 goto out;
1702
1703 break;
1704
1705 case IWL_MIMO2_SWITCH_GI:
1706 if (!tbl->is_ht40 && !(ht_cap->cap &
1707 IEEE80211_HT_CAP_SGI_20))
1708 break;
1709 if (tbl->is_ht40 && !(ht_cap->cap &
1710 IEEE80211_HT_CAP_SGI_40))
1711 break;
1712
1713 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
1714
1715 /* Set up new search table for MIMO2 */
1716 memcpy(search_tbl, tbl, sz);
1717 search_tbl->is_SGI = !tbl->is_SGI;
1718 rs_set_expected_tpt_table(lq_sta, search_tbl);
1719 /*
1720 * If active table already uses the fastest possible
1721 * modulation (dual stream with short guard interval),
1722 * and it's working well, there's no need to look
1723 * for a better type of modulation!
1724 */
1725 if (tbl->is_SGI) {
1726 s32 tpt = lq_sta->last_tpt / 100;
1727 if (tpt >= search_tbl->expected_tpt[index])
1728 break;
1729 }
1730 search_tbl->current_rate =
1731 rate_n_flags_from_tbl(mvm, search_tbl,
1732 index, is_green);
1733 update_search_tbl_counter = 1;
1734 goto out;
1735
1736 case IWL_MIMO2_SWITCH_MIMO3_ABC:
1737 IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to MIMO3\n");
1738 memcpy(search_tbl, tbl, sz);
1739 search_tbl->is_SGI = 0;
1740 search_tbl->ant_type = ANT_ABC;
1741
1742 if (!rs_is_valid_ant(valid_tx_ant,
1743 search_tbl->ant_type))
1744 break;
1745
1746 ret = rs_switch_to_mimo3(mvm, lq_sta, sta,
1747 search_tbl, index);
1748 if (!ret)
1749 goto out;
1750
1751 break;
1752 }
1753 tbl->action++;
1754 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1755 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1756
1757 if (tbl->action == start_action)
1758 break;
1759 }
1760 search_tbl->lq_type = LQ_NONE;
1761 return 0;
1762 out:
1763 lq_sta->search_better_tbl = 1;
1764 tbl->action++;
1765 if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
1766 tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
1767 if (update_search_tbl_counter)
1768 search_tbl->action = tbl->action;
1769
1770 return 0;
1771}
1772
1773/*
1774 * Try to switch to new modulation mode from MIMO3
1775 */
1776static int rs_move_mimo3_to_other(struct iwl_mvm *mvm,
1777 struct iwl_lq_sta *lq_sta,
1778 struct ieee80211_sta *sta, int index)
1779{
1780 s8 is_green = lq_sta->is_green;
1781 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
1782 struct iwl_scale_tbl_info *search_tbl =
1783 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
1784 struct iwl_rate_scale_data *window = &(tbl->win[index]);
1785 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1786 u32 sz = (sizeof(struct iwl_scale_tbl_info) -
1787 (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
1788 u8 start_action;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03001789 u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001790 u8 tx_chains_num = num_of_ant(valid_tx_ant);
1791 int ret;
1792 u8 update_search_tbl_counter = 0;
1793
1794 start_action = tbl->action;
1795 while (1) {
1796 lq_sta->action_counter++;
1797 switch (tbl->action) {
1798 case IWL_MIMO3_SWITCH_ANTENNA1:
1799 case IWL_MIMO3_SWITCH_ANTENNA2:
1800 IWL_DEBUG_RATE(mvm, "LQ: MIMO3 toggle Antennas\n");
1801
1802 if (tx_chains_num <= 3)
1803 break;
1804
1805 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1806 break;
1807
1808 memcpy(search_tbl, tbl, sz);
1809 if (rs_toggle_antenna(valid_tx_ant,
1810 &search_tbl->current_rate,
1811 search_tbl))
1812 goto out;
1813 break;
1814 case IWL_MIMO3_SWITCH_SISO_A:
1815 case IWL_MIMO3_SWITCH_SISO_B:
1816 case IWL_MIMO3_SWITCH_SISO_C:
1817 IWL_DEBUG_RATE(mvm, "LQ: MIMO3 switch to SISO\n");
1818
1819 /* Set up new search table for SISO */
1820 memcpy(search_tbl, tbl, sz);
1821
1822 if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
1823 search_tbl->ant_type = ANT_A;
1824 else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
1825 search_tbl->ant_type = ANT_B;
1826 else
1827 search_tbl->ant_type = ANT_C;
1828
1829 if (!rs_is_valid_ant(valid_tx_ant,
1830 search_tbl->ant_type))
1831 break;
1832
1833 ret = rs_switch_to_siso(mvm, lq_sta, sta,
1834 search_tbl, index);
1835 if (!ret)
1836 goto out;
1837
1838 break;
1839
1840 case IWL_MIMO3_SWITCH_MIMO2_AB:
1841 case IWL_MIMO3_SWITCH_MIMO2_AC:
1842 case IWL_MIMO3_SWITCH_MIMO2_BC:
1843 IWL_DEBUG_RATE(mvm, "LQ: MIMO3 switch to MIMO2\n");
1844
1845 memcpy(search_tbl, tbl, sz);
1846 search_tbl->is_SGI = 0;
1847 if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
1848 search_tbl->ant_type = ANT_AB;
1849 else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
1850 search_tbl->ant_type = ANT_AC;
1851 else
1852 search_tbl->ant_type = ANT_BC;
1853
1854 if (!rs_is_valid_ant(valid_tx_ant,
1855 search_tbl->ant_type))
1856 break;
1857
1858 ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
1859 search_tbl, index);
1860 if (!ret)
1861 goto out;
1862
1863 break;
1864
1865 case IWL_MIMO3_SWITCH_GI:
1866 if (!tbl->is_ht40 && !(ht_cap->cap &
1867 IEEE80211_HT_CAP_SGI_20))
1868 break;
1869 if (tbl->is_ht40 && !(ht_cap->cap &
1870 IEEE80211_HT_CAP_SGI_40))
1871 break;
1872
1873 IWL_DEBUG_RATE(mvm, "LQ: MIMO3 toggle SGI/NGI\n");
1874
1875 /* Set up new search table for MIMO */
1876 memcpy(search_tbl, tbl, sz);
1877 search_tbl->is_SGI = !tbl->is_SGI;
1878 rs_set_expected_tpt_table(lq_sta, search_tbl);
1879 /*
1880 * If active table already uses the fastest possible
1881 * modulation (dual stream with short guard interval),
1882 * and it's working well, there's no need to look
1883 * for a better type of modulation!
1884 */
1885 if (tbl->is_SGI) {
1886 s32 tpt = lq_sta->last_tpt / 100;
1887 if (tpt >= search_tbl->expected_tpt[index])
1888 break;
1889 }
1890 search_tbl->current_rate =
1891 rate_n_flags_from_tbl(mvm, search_tbl,
1892 index, is_green);
1893 update_search_tbl_counter = 1;
1894 goto out;
1895 }
1896 tbl->action++;
1897 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1898 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1899
1900 if (tbl->action == start_action)
1901 break;
1902 }
1903 search_tbl->lq_type = LQ_NONE;
1904 return 0;
1905 out:
1906 lq_sta->search_better_tbl = 1;
1907 tbl->action++;
1908 if (tbl->action > IWL_MIMO3_SWITCH_GI)
1909 tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
1910 if (update_search_tbl_counter)
1911 search_tbl->action = tbl->action;
1912
1913 return 0;
1914}
1915
1916/*
1917 * Check whether we should continue using same modulation mode, or
1918 * begin search for a new mode, based on:
1919 * 1) # tx successes or failures while using this mode
1920 * 2) # times calling this function
1921 * 3) elapsed time in this mode (not used, for now)
1922 */
1923static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
1924{
1925 struct iwl_scale_tbl_info *tbl;
1926 int i;
1927 int active_tbl;
1928 int flush_interval_passed = 0;
1929 struct iwl_mvm *mvm;
1930
1931 mvm = lq_sta->drv;
1932 active_tbl = lq_sta->active_tbl;
1933
1934 tbl = &(lq_sta->lq_info[active_tbl]);
1935
1936 /* If we've been disallowing search, see if we should now allow it */
1937 if (lq_sta->stay_in_tbl) {
1938 /* Elapsed time using current modulation mode */
1939 if (lq_sta->flush_timer)
1940 flush_interval_passed =
1941 time_after(jiffies,
1942 (unsigned long)(lq_sta->flush_timer +
1943 IWL_RATE_SCALE_FLUSH_INTVL));
1944
1945 /*
1946 * Check if we should allow search for new modulation mode.
1947 * If many frames have failed or succeeded, or we've used
1948 * this same modulation for a long time, allow search, and
1949 * reset history stats that keep track of whether we should
1950 * allow a new search. Also (below) reset all bitmaps and
1951 * stats in active history.
1952 */
1953 if (force_search ||
1954 (lq_sta->total_failed > lq_sta->max_failure_limit) ||
1955 (lq_sta->total_success > lq_sta->max_success_limit) ||
1956 ((!lq_sta->search_better_tbl) &&
1957 (lq_sta->flush_timer) && (flush_interval_passed))) {
1958 IWL_DEBUG_RATE(mvm,
1959 "LQ: stay is expired %d %d %d\n",
1960 lq_sta->total_failed,
1961 lq_sta->total_success,
1962 flush_interval_passed);
1963
1964 /* Allow search for new mode */
1965 lq_sta->stay_in_tbl = 0; /* only place reset */
1966 lq_sta->total_failed = 0;
1967 lq_sta->total_success = 0;
1968 lq_sta->flush_timer = 0;
1969 /*
1970 * Else if we've used this modulation mode enough repetitions
1971 * (regardless of elapsed time or success/failure), reset
1972 * history bitmaps and rate-specific stats for all rates in
1973 * active table.
1974 */
1975 } else {
1976 lq_sta->table_count++;
1977 if (lq_sta->table_count >=
1978 lq_sta->table_count_limit) {
1979 lq_sta->table_count = 0;
1980
1981 IWL_DEBUG_RATE(mvm,
1982 "LQ: stay in table clear win\n");
1983 for (i = 0; i < IWL_RATE_COUNT; i++)
1984 rs_rate_scale_clear_window(
1985 &(tbl->win[i]));
1986 }
1987 }
1988
1989 /* If transitioning to allow "search", reset all history
1990 * bitmaps and stats in active table (this will become the new
1991 * "search" table). */
1992 if (!lq_sta->stay_in_tbl) {
1993 for (i = 0; i < IWL_RATE_COUNT; i++)
1994 rs_rate_scale_clear_window(&(tbl->win[i]));
1995 }
1996 }
1997}
1998
1999/*
2000 * setup rate table in uCode
2001 */
2002static void rs_update_rate_tbl(struct iwl_mvm *mvm,
2003 struct iwl_lq_sta *lq_sta,
2004 struct iwl_scale_tbl_info *tbl,
2005 int index, u8 is_green)
2006{
2007 u32 rate;
2008
2009 /* Update uCode's rate table. */
2010 rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
2011 rs_fill_link_cmd(mvm, lq_sta, rate);
2012 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
2013}
2014
2015/*
2016 * Do rate scaling and search for new modulation mode.
2017 */
2018static void rs_rate_scale_perform(struct iwl_mvm *mvm,
2019 struct sk_buff *skb,
2020 struct ieee80211_sta *sta,
2021 struct iwl_lq_sta *lq_sta)
2022{
2023 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2024 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2025 int low = IWL_RATE_INVALID;
2026 int high = IWL_RATE_INVALID;
2027 int index;
2028 int i;
2029 struct iwl_rate_scale_data *window = NULL;
2030 int current_tpt = IWL_INVALID_VALUE;
2031 int low_tpt = IWL_INVALID_VALUE;
2032 int high_tpt = IWL_INVALID_VALUE;
2033 u32 fail_count;
2034 s8 scale_action = 0;
2035 u16 rate_mask;
2036 u8 update_lq = 0;
2037 struct iwl_scale_tbl_info *tbl, *tbl1;
2038 u16 rate_scale_index_msk = 0;
2039 u8 is_green = 0;
2040 u8 active_tbl = 0;
2041 u8 done_search = 0;
2042 u16 high_low;
2043 s32 sr;
2044 u8 tid = IWL_MAX_TID_COUNT;
2045 struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
2046 struct iwl_mvm_tid_data *tid_data;
2047
2048 IWL_DEBUG_RATE(mvm, "rate scale calculate new rate for skb\n");
2049
2050 /* Send management frames and NO_ACK data using lowest rate. */
2051 /* TODO: this could probably be improved.. */
2052 if (!ieee80211_is_data(hdr->frame_control) ||
2053 info->flags & IEEE80211_TX_CTL_NO_ACK)
2054 return;
2055
2056 lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
2057
2058 tid = rs_tl_add_packet(lq_sta, hdr);
2059 if ((tid != IWL_MAX_TID_COUNT) &&
2060 (lq_sta->tx_agg_tid_en & (1 << tid))) {
2061 tid_data = &sta_priv->tid_data[tid];
2062 if (tid_data->state == IWL_AGG_OFF)
2063 lq_sta->is_agg = 0;
2064 else
2065 lq_sta->is_agg = 1;
2066 } else {
2067 lq_sta->is_agg = 0;
2068 }
2069
2070 /*
2071 * Select rate-scale / modulation-mode table to work with in
2072 * the rest of this function: "search" if searching for better
2073 * modulation mode, or "active" if doing rate scaling within a mode.
2074 */
2075 if (!lq_sta->search_better_tbl)
2076 active_tbl = lq_sta->active_tbl;
2077 else
2078 active_tbl = 1 - lq_sta->active_tbl;
2079
2080 tbl = &(lq_sta->lq_info[active_tbl]);
2081 if (is_legacy(tbl->lq_type))
2082 lq_sta->is_green = 0;
2083 else
2084 lq_sta->is_green = rs_use_green(sta);
2085 is_green = lq_sta->is_green;
2086
2087 /* current tx rate */
2088 index = lq_sta->last_txrate_idx;
2089
2090 IWL_DEBUG_RATE(mvm, "Rate scale index %d for type %d\n", index,
2091 tbl->lq_type);
2092
2093 /* rates available for this association, and for modulation mode */
2094 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
2095
2096 IWL_DEBUG_RATE(mvm, "mask 0x%04X\n", rate_mask);
2097
2098 /* mask with station rate restriction */
2099 if (is_legacy(tbl->lq_type)) {
2100 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2101 /* supp_rates has no CCK bits in A mode */
2102 rate_scale_index_msk = (u16) (rate_mask &
2103 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
2104 else
2105 rate_scale_index_msk = (u16) (rate_mask &
2106 lq_sta->supp_rates);
2107
2108 } else {
2109 rate_scale_index_msk = rate_mask;
2110 }
2111
2112 if (!rate_scale_index_msk)
2113 rate_scale_index_msk = rate_mask;
2114
2115 if (!((1 << index) & rate_scale_index_msk)) {
2116 IWL_ERR(mvm, "Current Rate is not valid\n");
2117 if (lq_sta->search_better_tbl) {
2118 /* revert to active table if search table is not valid*/
2119 tbl->lq_type = LQ_NONE;
2120 lq_sta->search_better_tbl = 0;
2121 tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2122 /* get "active" rate info */
2123 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2124 rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
2125 }
2126 return;
2127 }
2128
2129 /* Get expected throughput table and history window for current rate */
2130 if (!tbl->expected_tpt) {
2131 IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
2132 return;
2133 }
2134
2135 /* force user max rate if set by user */
2136 if ((lq_sta->max_rate_idx != -1) &&
2137 (lq_sta->max_rate_idx < index)) {
2138 index = lq_sta->max_rate_idx;
2139 update_lq = 1;
2140 window = &(tbl->win[index]);
2141 goto lq_update;
2142 }
2143
2144 window = &(tbl->win[index]);
2145
2146 /*
2147 * If there is not enough history to calculate actual average
2148 * throughput, keep analyzing results of more tx frames, without
2149 * changing rate or mode (bypass most of the rest of this function).
2150 * Set up new rate table in uCode only if old rate is not supported
2151 * in current association (use new rate found above).
2152 */
2153 fail_count = window->counter - window->success_counter;
2154 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
2155 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
2156 IWL_DEBUG_RATE(mvm,
2157 "LQ: still below TH. succ=%d total=%d for index %d\n",
2158 window->success_counter, window->counter, index);
2159
2160 /* Can't calculate this yet; not enough history */
2161 window->average_tpt = IWL_INVALID_VALUE;
2162
2163 /* Should we stay with this modulation mode,
2164 * or search for a new one? */
2165 rs_stay_in_table(lq_sta, false);
2166
2167 goto out;
2168 }
2169 /* Else we have enough samples; calculate estimate of
2170 * actual average throughput */
2171 if (window->average_tpt != ((window->success_ratio *
2172 tbl->expected_tpt[index] + 64) / 128)) {
2173 IWL_ERR(mvm,
2174 "expected_tpt should have been calculated by now\n");
2175 window->average_tpt = ((window->success_ratio *
2176 tbl->expected_tpt[index] + 64) / 128);
2177 }
2178
2179 /* If we are searching for better modulation mode, check success. */
2180 if (lq_sta->search_better_tbl) {
2181 /* If good success, continue using the "search" mode;
2182 * no need to send new link quality command, since we're
2183 * continuing to use the setup that we've been trying. */
2184 if (window->average_tpt > lq_sta->last_tpt) {
2185 IWL_DEBUG_RATE(mvm,
2186 "LQ: SWITCHING TO NEW TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
2187 window->success_ratio,
2188 window->average_tpt,
2189 lq_sta->last_tpt);
2190
2191 if (!is_legacy(tbl->lq_type))
2192 lq_sta->enable_counter = 1;
2193
2194 /* Swap tables; "search" becomes "active" */
2195 lq_sta->active_tbl = active_tbl;
2196 current_tpt = window->average_tpt;
2197 /* Else poor success; go back to mode in "active" table */
2198 } else {
2199 IWL_DEBUG_RATE(mvm,
2200 "LQ: GOING BACK TO THE OLD TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
2201 window->success_ratio,
2202 window->average_tpt,
2203 lq_sta->last_tpt);
2204
2205 /* Nullify "search" table */
2206 tbl->lq_type = LQ_NONE;
2207
2208 /* Revert to "active" table */
2209 active_tbl = lq_sta->active_tbl;
2210 tbl = &(lq_sta->lq_info[active_tbl]);
2211
2212 /* Revert to "active" rate and throughput info */
2213 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2214 current_tpt = lq_sta->last_tpt;
2215
2216 /* Need to set up a new rate table in uCode */
2217 update_lq = 1;
2218 }
2219
2220 /* Either way, we've made a decision; modulation mode
2221 * search is done, allow rate adjustment next time. */
2222 lq_sta->search_better_tbl = 0;
2223 done_search = 1; /* Don't switch modes below! */
2224 goto lq_update;
2225 }
2226
2227 /* (Else) not in search of better modulation mode, try for better
2228 * starting rate, while staying in this mode. */
2229 high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
2230 tbl->lq_type);
2231 low = high_low & 0xff;
2232 high = (high_low >> 8) & 0xff;
2233
2234 /* If user set max rate, dont allow higher than user constrain */
2235 if ((lq_sta->max_rate_idx != -1) &&
2236 (lq_sta->max_rate_idx < high))
2237 high = IWL_RATE_INVALID;
2238
2239 sr = window->success_ratio;
2240
2241 /* Collect measured throughputs for current and adjacent rates */
2242 current_tpt = window->average_tpt;
2243 if (low != IWL_RATE_INVALID)
2244 low_tpt = tbl->win[low].average_tpt;
2245 if (high != IWL_RATE_INVALID)
2246 high_tpt = tbl->win[high].average_tpt;
2247
2248 scale_action = 0;
2249
2250 /* Too many failures, decrease rate */
2251 if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
2252 IWL_DEBUG_RATE(mvm,
2253 "decrease rate because of low success_ratio\n");
2254 scale_action = -1;
2255 /* No throughput measured yet for adjacent rates; try increase. */
2256 } else if ((low_tpt == IWL_INVALID_VALUE) &&
2257 (high_tpt == IWL_INVALID_VALUE)) {
2258 if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
2259 scale_action = 1;
2260 else if (low != IWL_RATE_INVALID)
2261 scale_action = 0;
2262 }
2263
2264 /* Both adjacent throughputs are measured, but neither one has better
2265 * throughput; we're using the best rate, don't change it! */
2266 else if ((low_tpt != IWL_INVALID_VALUE) &&
2267 (high_tpt != IWL_INVALID_VALUE) &&
2268 (low_tpt < current_tpt) &&
2269 (high_tpt < current_tpt))
2270 scale_action = 0;
2271
2272 /* At least one adjacent rate's throughput is measured,
2273 * and may have better performance. */
2274 else {
2275 /* Higher adjacent rate's throughput is measured */
2276 if (high_tpt != IWL_INVALID_VALUE) {
2277 /* Higher rate has better throughput */
2278 if (high_tpt > current_tpt &&
2279 sr >= IWL_RATE_INCREASE_TH) {
2280 scale_action = 1;
2281 } else {
2282 scale_action = 0;
2283 }
2284
2285 /* Lower adjacent rate's throughput is measured */
2286 } else if (low_tpt != IWL_INVALID_VALUE) {
2287 /* Lower rate has better throughput */
2288 if (low_tpt > current_tpt) {
2289 IWL_DEBUG_RATE(mvm,
2290 "decrease rate because of low tpt\n");
2291 scale_action = -1;
2292 } else if (sr >= IWL_RATE_INCREASE_TH) {
2293 scale_action = 1;
2294 }
2295 }
2296 }
2297
2298 /* Sanity check; asked for decrease, but success rate or throughput
2299 * has been good at old rate. Don't change it. */
2300 if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
2301 ((sr > IWL_RATE_HIGH_TH) ||
2302 (current_tpt > (100 * tbl->expected_tpt[low]))))
2303 scale_action = 0;
2304
2305 switch (scale_action) {
2306 case -1:
2307 /* Decrease starting rate, update uCode's rate table */
2308 if (low != IWL_RATE_INVALID) {
2309 update_lq = 1;
2310 index = low;
2311 }
2312
2313 break;
2314 case 1:
2315 /* Increase starting rate, update uCode's rate table */
2316 if (high != IWL_RATE_INVALID) {
2317 update_lq = 1;
2318 index = high;
2319 }
2320
2321 break;
2322 case 0:
2323 /* No change */
2324 default:
2325 break;
2326 }
2327
2328 IWL_DEBUG_RATE(mvm,
2329 "choose rate scale index %d action %d low %d high %d type %d\n",
2330 index, scale_action, low, high, tbl->lq_type);
2331
2332lq_update:
2333 /* Replace uCode's rate table for the destination station. */
2334 if (update_lq)
2335 rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
2336
2337 rs_stay_in_table(lq_sta, false);
2338
2339 /*
2340 * Search for new modulation mode if we're:
2341 * 1) Not changing rates right now
2342 * 2) Not just finishing up a search
2343 * 3) Allowing a new search
2344 */
2345 if (!update_lq && !done_search &&
2346 !lq_sta->stay_in_tbl && window->counter) {
2347 /* Save current throughput to compare with "search" throughput*/
2348 lq_sta->last_tpt = current_tpt;
2349
2350 /* Select a new "search" modulation mode to try.
2351 * If one is found, set up the new "search" table. */
2352 if (is_legacy(tbl->lq_type))
2353 rs_move_legacy_other(mvm, lq_sta, sta, index);
2354 else if (is_siso(tbl->lq_type))
2355 rs_move_siso_to_other(mvm, lq_sta, sta, index);
2356 else if (is_mimo2(tbl->lq_type))
2357 rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
2358 else
2359 rs_move_mimo3_to_other(mvm, lq_sta, sta, index);
2360
2361 /* If new "search" mode was selected, set up in uCode table */
2362 if (lq_sta->search_better_tbl) {
2363 /* Access the "search" table, clear its history. */
2364 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
2365 for (i = 0; i < IWL_RATE_COUNT; i++)
2366 rs_rate_scale_clear_window(&(tbl->win[i]));
2367
2368 /* Use new "search" start rate */
2369 index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
2370
2371 IWL_DEBUG_RATE(mvm,
2372 "Switch current mcs: %X index: %d\n",
2373 tbl->current_rate, index);
2374 rs_fill_link_cmd(mvm, lq_sta, tbl->current_rate);
2375 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
2376 } else {
2377 done_search = 1;
2378 }
2379 }
2380
2381 if (done_search && !lq_sta->stay_in_tbl) {
2382 /* If the "active" (non-search) mode was legacy,
2383 * and we've tried switching antennas,
2384 * but we haven't been able to try HT modes (not available),
2385 * stay with best antenna legacy modulation for a while
2386 * before next round of mode comparisons. */
2387 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
2388 if (is_legacy(tbl1->lq_type) && !sta->ht_cap.ht_supported &&
2389 lq_sta->action_counter > tbl1->max_search) {
2390 IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
2391 rs_set_stay_in_table(mvm, 1, lq_sta);
2392 }
2393
2394 /* If we're in an HT mode, and all 3 mode switch actions
2395 * have been tried and compared, stay in this best modulation
2396 * mode for a while before next round of mode comparisons. */
2397 if (lq_sta->enable_counter &&
2398 (lq_sta->action_counter >= tbl1->max_search)) {
2399 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2400 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2401 (tid != IWL_MAX_TID_COUNT)) {
2402 tid_data = &sta_priv->tid_data[tid];
2403 if (tid_data->state == IWL_AGG_OFF) {
2404 IWL_DEBUG_RATE(mvm,
2405 "try to aggregate tid %d\n",
2406 tid);
2407 rs_tl_turn_on_agg(mvm, tid,
2408 lq_sta, sta);
2409 }
2410 }
2411 rs_set_stay_in_table(mvm, 0, lq_sta);
2412 }
2413 }
2414
2415out:
2416 tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
2417 lq_sta->last_txrate_idx = index;
2418}
2419
2420/**
2421 * rs_initialize_lq - Initialize a station's hardware rate table
2422 *
2423 * The uCode's station table contains a table of fallback rates
2424 * for automatic fallback during transmission.
2425 *
2426 * NOTE: This sets up a default set of values. These will be replaced later
2427 * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
2428 * rc80211_simple.
2429 *
2430 * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
2431 * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
2432 * which requires station table entry to exist).
2433 */
2434static void rs_initialize_lq(struct iwl_mvm *mvm,
2435 struct ieee80211_sta *sta,
2436 struct iwl_lq_sta *lq_sta,
2437 enum ieee80211_band band)
2438{
2439 struct iwl_scale_tbl_info *tbl;
2440 int rate_idx;
2441 int i;
2442 u32 rate;
2443 u8 use_green = rs_use_green(sta);
2444 u8 active_tbl = 0;
2445 u8 valid_tx_ant;
2446
2447 if (!sta || !lq_sta)
2448 return;
2449
2450 i = lq_sta->last_txrate_idx;
2451
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002452 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002453
2454 if (!lq_sta->search_better_tbl)
2455 active_tbl = lq_sta->active_tbl;
2456 else
2457 active_tbl = 1 - lq_sta->active_tbl;
2458
2459 tbl = &(lq_sta->lq_info[active_tbl]);
2460
2461 if ((i < 0) || (i >= IWL_RATE_COUNT))
2462 i = 0;
2463
2464 rate = iwl_rates[i].plcp;
2465 tbl->ant_type = first_antenna(valid_tx_ant);
2466 rate |= tbl->ant_type << RATE_MCS_ANT_POS;
2467
2468 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
2469 rate |= RATE_MCS_CCK_MSK;
2470
2471 rs_get_tbl_info_from_mcs(rate, band, tbl, &rate_idx);
2472 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2473 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
2474
2475 rate = rate_n_flags_from_tbl(mvm, tbl, rate_idx, use_green);
2476 tbl->current_rate = rate;
2477 rs_set_expected_tpt_table(lq_sta, tbl);
2478 rs_fill_link_cmd(NULL, lq_sta, rate);
2479 /* TODO restore station should remember the lq cmd */
2480 iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_SYNC, true);
2481}
2482
2483static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
2484 struct ieee80211_tx_rate_control *txrc)
2485{
2486 struct sk_buff *skb = txrc->skb;
2487 struct ieee80211_supported_band *sband = txrc->sband;
2488 struct iwl_op_mode *op_mode __maybe_unused =
2489 (struct iwl_op_mode *)mvm_r;
2490 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2491 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2492 struct iwl_lq_sta *lq_sta = mvm_sta;
2493 int rate_idx;
2494
2495 IWL_DEBUG_RATE_LIMIT(mvm, "rate scale calculate new rate for skb\n");
2496
2497 /* Get max rate if user set max rate */
2498 if (lq_sta) {
2499 lq_sta->max_rate_idx = txrc->max_rate_idx;
2500 if ((sband->band == IEEE80211_BAND_5GHZ) &&
2501 (lq_sta->max_rate_idx != -1))
2502 lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
2503 if ((lq_sta->max_rate_idx < 0) ||
2504 (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
2505 lq_sta->max_rate_idx = -1;
2506 }
2507
2508 /* Treat uninitialized rate scaling data same as non-existing. */
2509 if (lq_sta && !lq_sta->drv) {
2510 IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
2511 mvm_sta = NULL;
2512 }
2513
2514 /* Send management frames and NO_ACK data using lowest rate. */
2515 if (rate_control_send_low(sta, mvm_sta, txrc))
2516 return;
2517
2518 rate_idx = lq_sta->last_txrate_idx;
2519
2520 if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
2521 rate_idx -= IWL_FIRST_OFDM_RATE;
2522 /* 6M and 9M shared same MCS index */
2523 rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
2524 if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2525 IWL_RATE_MIMO3_6M_PLCP)
2526 rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
2527 else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
2528 IWL_RATE_MIMO2_6M_PLCP)
2529 rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
2530 info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
2531 if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
2532 info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
2533 if (lq_sta->last_rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
2534 info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2535 if (lq_sta->last_rate_n_flags & RATE_HT_MCS_GF_MSK)
2536 info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
2537 } else {
2538 /* Check for invalid rates */
2539 if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
2540 ((sband->band == IEEE80211_BAND_5GHZ) &&
2541 (rate_idx < IWL_FIRST_OFDM_RATE)))
2542 rate_idx = rate_lowest_index(sband, sta);
2543 /* On valid 5 GHz rate, adjust index */
2544 else if (sband->band == IEEE80211_BAND_5GHZ)
2545 rate_idx -= IWL_FIRST_OFDM_RATE;
2546 info->control.rates[0].flags = 0;
2547 }
2548 info->control.rates[0].idx = rate_idx;
Moshe Benji622ebe92013-06-03 19:27:16 +03002549 info->control.rates[0].count = 1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002550}
2551
2552static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
2553 gfp_t gfp)
2554{
2555 struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2556 struct iwl_op_mode *op_mode __maybe_unused =
2557 (struct iwl_op_mode *)mvm_rate;
2558 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2559
2560 IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
2561
2562 return &sta_priv->lq_sta;
2563}
2564
2565/*
2566 * Called after adding a new station to initialize rate scaling
2567 */
2568void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2569 enum ieee80211_band band)
2570{
2571 int i, j;
2572 struct ieee80211_hw *hw = mvm->hw;
2573 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2574 struct iwl_mvm_sta *sta_priv;
2575 struct iwl_lq_sta *lq_sta;
2576 struct ieee80211_supported_band *sband;
2577 unsigned long supp; /* must be unsigned long for for_each_set_bit */
2578
2579 sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
2580 lq_sta = &sta_priv->lq_sta;
2581 sband = hw->wiphy->bands[band];
2582
2583 lq_sta->lq.sta_id = sta_priv->sta_id;
2584
2585 for (j = 0; j < LQ_SIZE; j++)
2586 for (i = 0; i < IWL_RATE_COUNT; i++)
2587 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2588
2589 lq_sta->flush_timer = 0;
2590 lq_sta->supp_rates = sta->supp_rates[sband->band];
2591 for (j = 0; j < LQ_SIZE; j++)
2592 for (i = 0; i < IWL_RATE_COUNT; i++)
2593 rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
2594
2595 IWL_DEBUG_RATE(mvm,
2596 "LQ: *** rate scale station global init for station %d ***\n",
2597 sta_priv->sta_id);
2598 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2599 * the lowest or the highest rate.. Could consider using RSSI from
2600 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2601 * after assoc.. */
2602
2603 lq_sta->max_rate_idx = -1;
2604 lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
2605 lq_sta->is_green = rs_use_green(sta);
2606 lq_sta->band = sband->band;
2607 /*
2608 * active legacy rates as per supported rates bitmap
2609 */
2610 supp = sta->supp_rates[sband->band];
2611 lq_sta->active_legacy_rate = 0;
2612 for_each_set_bit(i, &supp, BITS_PER_LONG)
2613 lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
2614
2615 /*
2616 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2617 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2618 */
2619 lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
2620 lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
2621 lq_sta->active_siso_rate &= ~((u16)0x2);
2622 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
2623
2624 /* Same here */
2625 lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
2626 lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
2627 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2628 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2629
2630 lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
2631 lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
2632 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2633 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2634
2635 IWL_DEBUG_RATE(mvm,
2636 "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
2637 lq_sta->active_siso_rate,
2638 lq_sta->active_mimo2_rate,
2639 lq_sta->active_mimo3_rate);
2640
2641 /* These values will be overridden later */
2642 lq_sta->lq.single_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002643 first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002644 lq_sta->lq.dual_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002645 iwl_fw_valid_tx_ant(mvm->fw) &
2646 ~first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
Johannes Berg8ca151b2013-01-24 14:25:36 +01002647 if (!lq_sta->lq.dual_stream_ant_msk) {
2648 lq_sta->lq.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002649 } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) == 2) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002650 lq_sta->lq.dual_stream_ant_msk =
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002651 iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002652 }
2653
2654 /* as default allow aggregation for all tids */
2655 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
2656 lq_sta->drv = mvm;
2657
2658 /* Set last_txrate_idx to lowest rate */
2659 lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
2660 if (sband->band == IEEE80211_BAND_5GHZ)
2661 lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
2662 lq_sta->is_agg = 0;
2663#ifdef CONFIG_MAC80211_DEBUGFS
2664 lq_sta->dbg_fixed_rate = 0;
2665#endif
2666
2667 rs_initialize_lq(mvm, sta, lq_sta, band);
2668}
2669
2670static void rs_fill_link_cmd(struct iwl_mvm *mvm,
2671 struct iwl_lq_sta *lq_sta, u32 new_rate)
2672{
2673 struct iwl_scale_tbl_info tbl_type;
2674 int index = 0;
2675 int rate_idx;
2676 int repeat_rate = 0;
2677 u8 ant_toggle_cnt = 0;
2678 u8 use_ht_possible = 1;
2679 u8 valid_tx_ant = 0;
2680 struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
2681
2682 /* Override starting rate (index 0) if needed for debug purposes */
2683 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2684
2685 /* Interpret new_rate (rate_n_flags) */
2686 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
2687 &tbl_type, &rate_idx);
2688
2689 /* How many times should we repeat the initial rate? */
2690 if (is_legacy(tbl_type.lq_type)) {
2691 ant_toggle_cnt = 1;
2692 repeat_rate = IWL_NUMBER_TRY;
2693 } else {
2694 repeat_rate = min(IWL_HT_NUMBER_TRY,
2695 LINK_QUAL_AGG_DISABLE_START_DEF - 1);
2696 }
2697
2698 lq_cmd->mimo_delim = is_mimo(tbl_type.lq_type) ? 1 : 0;
2699
2700 /* Fill 1st table entry (index 0) */
2701 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2702
2703 if (num_of_ant(tbl_type.ant_type) == 1)
2704 lq_cmd->single_stream_ant_msk = tbl_type.ant_type;
2705 else if (num_of_ant(tbl_type.ant_type) == 2)
2706 lq_cmd->dual_stream_ant_msk = tbl_type.ant_type;
2707 /* otherwise we don't modify the existing value */
2708
2709 index++;
2710 repeat_rate--;
2711 if (mvm)
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002712 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002713
2714 /* Fill rest of rate table */
2715 while (index < LINK_QUAL_MAX_RETRY_NUM) {
2716 /* Repeat initial/next rate.
2717 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2718 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
2719 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
2720 if (is_legacy(tbl_type.lq_type)) {
2721 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2722 ant_toggle_cnt++;
2723 else if (mvm &&
2724 rs_toggle_antenna(valid_tx_ant,
2725 &new_rate, &tbl_type))
2726 ant_toggle_cnt = 1;
2727 }
2728
2729 /* Override next rate if needed for debug purposes */
2730 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2731
2732 /* Fill next table entry */
2733 lq_cmd->rs_table[index] =
2734 cpu_to_le32(new_rate);
2735 repeat_rate--;
2736 index++;
2737 }
2738
2739 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
2740 &rate_idx);
2741
2742
2743 /* Indicate to uCode which entries might be MIMO.
2744 * If initial rate was MIMO, this will finally end up
2745 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
2746 if (is_mimo(tbl_type.lq_type))
2747 lq_cmd->mimo_delim = index;
2748
2749 /* Get next rate */
2750 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2751 use_ht_possible);
2752
2753 /* How many times should we repeat the next rate? */
2754 if (is_legacy(tbl_type.lq_type)) {
2755 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2756 ant_toggle_cnt++;
2757 else if (mvm &&
2758 rs_toggle_antenna(valid_tx_ant,
2759 &new_rate, &tbl_type))
2760 ant_toggle_cnt = 1;
2761
2762 repeat_rate = IWL_NUMBER_TRY;
2763 } else {
2764 repeat_rate = IWL_HT_NUMBER_TRY;
2765 }
2766
2767 /* Don't allow HT rates after next pass.
2768 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
2769 use_ht_possible = 0;
2770
2771 /* Override next rate if needed for debug purposes */
2772 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
2773
2774 /* Fill next table entry */
2775 lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
2776
2777 index++;
2778 repeat_rate--;
2779 }
2780
2781 lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
2782 lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
2783
2784 lq_cmd->agg_time_limit =
2785 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
2786}
2787
2788static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
2789{
2790 return hw->priv;
2791}
2792/* rate scale requires free function to be implemented */
2793static void rs_free(void *mvm_rate)
2794{
2795 return;
2796}
2797
2798static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
2799 void *mvm_sta)
2800{
2801 struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
2802 struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
2803
2804 IWL_DEBUG_RATE(mvm, "enter\n");
2805 IWL_DEBUG_RATE(mvm, "leave\n");
2806}
2807
2808#ifdef CONFIG_MAC80211_DEBUGFS
2809static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
2810 u32 *rate_n_flags, int index)
2811{
2812 struct iwl_mvm *mvm;
2813 u8 valid_tx_ant;
2814 u8 ant_sel_tx;
2815
2816 mvm = lq_sta->drv;
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002817 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002818 if (lq_sta->dbg_fixed_rate) {
2819 ant_sel_tx =
2820 ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
2821 >> RATE_MCS_ANT_POS);
2822 if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
2823 *rate_n_flags = lq_sta->dbg_fixed_rate;
2824 IWL_DEBUG_RATE(mvm, "Fixed rate ON\n");
2825 } else {
2826 lq_sta->dbg_fixed_rate = 0;
2827 IWL_ERR(mvm,
2828 "Invalid antenna selection 0x%X, Valid is 0x%X\n",
2829 ant_sel_tx, valid_tx_ant);
2830 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2831 }
2832 } else {
2833 IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
2834 }
2835}
2836
2837static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2838 const char __user *user_buf, size_t count, loff_t *ppos)
2839{
2840 struct iwl_lq_sta *lq_sta = file->private_data;
2841 struct iwl_mvm *mvm;
2842 char buf[64];
2843 size_t buf_size;
2844 u32 parsed_rate;
2845
2846
2847 mvm = lq_sta->drv;
2848 memset(buf, 0, sizeof(buf));
2849 buf_size = min(count, sizeof(buf) - 1);
2850 if (copy_from_user(buf, user_buf, buf_size))
2851 return -EFAULT;
2852
2853 if (sscanf(buf, "%x", &parsed_rate) == 1)
2854 lq_sta->dbg_fixed_rate = parsed_rate;
2855 else
2856 lq_sta->dbg_fixed_rate = 0;
2857
2858 rs_program_fix_rate(mvm, lq_sta);
2859
2860 return count;
2861}
2862
2863static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2864 char __user *user_buf, size_t count, loff_t *ppos)
2865{
2866 char *buff;
2867 int desc = 0;
2868 int i = 0;
2869 int index = 0;
2870 ssize_t ret;
2871
2872 struct iwl_lq_sta *lq_sta = file->private_data;
2873 struct iwl_mvm *mvm;
2874 struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
2875
2876 mvm = lq_sta->drv;
2877 buff = kmalloc(1024, GFP_KERNEL);
2878 if (!buff)
2879 return -ENOMEM;
2880
2881 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
2882 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
2883 lq_sta->total_failed, lq_sta->total_success,
2884 lq_sta->active_legacy_rate);
2885 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
2886 lq_sta->dbg_fixed_rate);
2887 desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
Emmanuel Grumbachff402312013-04-03 20:10:06 +03002888 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
2889 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
2890 (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002891 desc += sprintf(buff+desc, "lq type %s\n",
2892 (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
2893 if (is_Ht(tbl->lq_type)) {
2894 desc += sprintf(buff+desc, " %s",
2895 (is_siso(tbl->lq_type)) ? "SISO" :
2896 ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
2897 desc += sprintf(buff+desc, " %s",
2898 (tbl->is_ht40) ? "40MHz" : "20MHz");
2899 desc += sprintf(buff+desc, " %s %s %s\n",
2900 (tbl->is_SGI) ? "SGI" : "",
2901 (lq_sta->is_green) ? "GF enabled" : "",
2902 (lq_sta->is_agg) ? "AGG on" : "");
2903 }
2904 desc += sprintf(buff+desc, "last tx rate=0x%X\n",
2905 lq_sta->last_rate_n_flags);
2906 desc += sprintf(buff+desc,
2907 "general: flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
2908 lq_sta->lq.flags,
2909 lq_sta->lq.mimo_delim,
2910 lq_sta->lq.single_stream_ant_msk,
2911 lq_sta->lq.dual_stream_ant_msk);
2912
2913 desc += sprintf(buff+desc,
2914 "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
2915 le16_to_cpu(lq_sta->lq.agg_time_limit),
2916 lq_sta->lq.agg_disable_start_th,
2917 lq_sta->lq.agg_frame_cnt_limit);
2918
2919 desc += sprintf(buff+desc,
2920 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
2921 lq_sta->lq.initial_rate_index[0],
2922 lq_sta->lq.initial_rate_index[1],
2923 lq_sta->lq.initial_rate_index[2],
2924 lq_sta->lq.initial_rate_index[3]);
2925
2926 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
2927 index = iwl_hwrate_to_plcp_idx(
2928 le32_to_cpu(lq_sta->lq.rs_table[i]));
2929 if (is_legacy(tbl->lq_type)) {
2930 desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
2931 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2932 iwl_rate_mcs[index].mbps);
2933 } else {
2934 desc += sprintf(buff+desc,
2935 " rate[%d] 0x%X %smbps (%s)\n",
2936 i, le32_to_cpu(lq_sta->lq.rs_table[i]),
2937 iwl_rate_mcs[index].mbps,
2938 iwl_rate_mcs[index].mcs);
2939 }
2940 }
2941
2942 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2943 kfree(buff);
2944 return ret;
2945}
2946
2947static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
2948 .write = rs_sta_dbgfs_scale_table_write,
2949 .read = rs_sta_dbgfs_scale_table_read,
2950 .open = simple_open,
2951 .llseek = default_llseek,
2952};
2953static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2954 char __user *user_buf, size_t count, loff_t *ppos)
2955{
2956 char *buff;
2957 int desc = 0;
2958 int i, j;
2959 ssize_t ret;
2960
2961 struct iwl_lq_sta *lq_sta = file->private_data;
2962
2963 buff = kmalloc(1024, GFP_KERNEL);
2964 if (!buff)
2965 return -ENOMEM;
2966
2967 for (i = 0; i < LQ_SIZE; i++) {
2968 desc += sprintf(buff+desc,
2969 "%s type=%d SGI=%d HT40=%d DUP=0 GF=%d\n"
2970 "rate=0x%X\n",
2971 lq_sta->active_tbl == i ? "*" : "x",
2972 lq_sta->lq_info[i].lq_type,
2973 lq_sta->lq_info[i].is_SGI,
2974 lq_sta->lq_info[i].is_ht40,
2975 lq_sta->is_green,
2976 lq_sta->lq_info[i].current_rate);
2977 for (j = 0; j < IWL_RATE_COUNT; j++) {
2978 desc += sprintf(buff+desc,
2979 "counter=%d success=%d %%=%d\n",
2980 lq_sta->lq_info[i].win[j].counter,
2981 lq_sta->lq_info[i].win[j].success_counter,
2982 lq_sta->lq_info[i].win[j].success_ratio);
2983 }
2984 }
2985 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2986 kfree(buff);
2987 return ret;
2988}
2989
2990static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2991 .read = rs_sta_dbgfs_stats_table_read,
2992 .open = simple_open,
2993 .llseek = default_llseek,
2994};
2995
2996static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
2997 char __user *user_buf, size_t count, loff_t *ppos)
2998{
2999 struct iwl_lq_sta *lq_sta = file->private_data;
3000 struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
3001 char buff[120];
3002 int desc = 0;
3003
3004 if (is_Ht(tbl->lq_type))
3005 desc += sprintf(buff+desc,
3006 "Bit Rate= %d Mb/s\n",
3007 tbl->expected_tpt[lq_sta->last_txrate_idx]);
3008 else
3009 desc += sprintf(buff+desc,
3010 "Bit Rate= %d Mb/s\n",
3011 iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
3012
3013 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
3014}
3015
3016static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
3017 .read = rs_sta_dbgfs_rate_scale_data_read,
3018 .open = simple_open,
3019 .llseek = default_llseek,
3020};
3021
3022static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
3023{
3024 struct iwl_lq_sta *lq_sta = mvm_sta;
3025 lq_sta->rs_sta_dbgfs_scale_table_file =
3026 debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
3027 lq_sta, &rs_sta_dbgfs_scale_table_ops);
3028 lq_sta->rs_sta_dbgfs_stats_table_file =
3029 debugfs_create_file("rate_stats_table", S_IRUSR, dir,
3030 lq_sta, &rs_sta_dbgfs_stats_table_ops);
3031 lq_sta->rs_sta_dbgfs_rate_scale_data_file =
3032 debugfs_create_file("rate_scale_data", S_IRUSR, dir,
3033 lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
3034 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
3035 debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
3036 &lq_sta->tx_agg_tid_en);
3037}
3038
3039static void rs_remove_debugfs(void *mvm, void *mvm_sta)
3040{
3041 struct iwl_lq_sta *lq_sta = mvm_sta;
3042 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
3043 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
3044 debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
3045 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
3046}
3047#endif
3048
3049/*
3050 * Initialization of rate scaling information is done by driver after
3051 * the station is added. Since mac80211 calls this function before a
3052 * station is added we ignore it.
3053 */
3054static void rs_rate_init_stub(void *mvm_r,
3055 struct ieee80211_supported_band *sband,
3056 struct ieee80211_sta *sta, void *mvm_sta)
3057{
3058}
3059static struct rate_control_ops rs_mvm_ops = {
3060 .module = NULL,
3061 .name = RS_NAME,
3062 .tx_status = rs_tx_status,
3063 .get_rate = rs_get_rate,
3064 .rate_init = rs_rate_init_stub,
3065 .alloc = rs_alloc,
3066 .free = rs_free,
3067 .alloc_sta = rs_alloc_sta,
3068 .free_sta = rs_free_sta,
3069#ifdef CONFIG_MAC80211_DEBUGFS
3070 .add_sta_debugfs = rs_add_debugfs,
3071 .remove_sta_debugfs = rs_remove_debugfs,
3072#endif
3073};
3074
3075int iwl_mvm_rate_control_register(void)
3076{
3077 return ieee80211_rate_control_register(&rs_mvm_ops);
3078}
3079
3080void iwl_mvm_rate_control_unregister(void)
3081{
3082 ieee80211_rate_control_unregister(&rs_mvm_ops);
3083}