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