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