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