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