1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *
4 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
5 *
6 * Contact Information:
7 * Intel Linux Wireless <ilw@linux.intel.com>
8 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
9 *
10 *****************************************************************************/
11
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/slab.h>
15 #include <net/mac80211.h>
16
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/delay.h>
20
21 #include <linux/workqueue.h>
22
23 #include "commands.h"
24 #include "3945.h"
25
26 #define RS_NAME "iwl-3945-rs"
27
28 static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
29 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
30 };
31
32 static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
33 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
34 };
35
36 static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
37 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
38 };
39
40 static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
41 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
42 };
43
44 struct il3945_tpt_entry {
45 s8 min_rssi;
46 u8 idx;
47 };
48
49 static struct il3945_tpt_entry il3945_tpt_table_a[] = {
50 {-60, RATE_54M_IDX},
51 {-64, RATE_48M_IDX},
52 {-72, RATE_36M_IDX},
53 {-80, RATE_24M_IDX},
54 {-84, RATE_18M_IDX},
55 {-85, RATE_12M_IDX},
56 {-87, RATE_9M_IDX},
57 {-89, RATE_6M_IDX}
58 };
59
60 static struct il3945_tpt_entry il3945_tpt_table_g[] = {
61 {-60, RATE_54M_IDX},
62 {-64, RATE_48M_IDX},
63 {-68, RATE_36M_IDX},
64 {-80, RATE_24M_IDX},
65 {-84, RATE_18M_IDX},
66 {-85, RATE_12M_IDX},
67 {-86, RATE_11M_IDX},
68 {-88, RATE_5M_IDX},
69 {-90, RATE_2M_IDX},
70 {-92, RATE_1M_IDX}
71 };
72
73 #define RATE_MAX_WINDOW 62
74 #define RATE_FLUSH (3*HZ)
75 #define RATE_WIN_FLUSH (HZ/2)
76 #define IL39_RATE_HIGH_TH 11520
77 #define IL_SUCCESS_UP_TH 8960
78 #define IL_SUCCESS_DOWN_TH 10880
79 #define RATE_MIN_FAILURE_TH 6
80 #define RATE_MIN_SUCCESS_TH 8
81 #define RATE_DECREASE_TH 1920
82 #define RATE_RETRY_TH 15
83
84 static u8
il3945_get_rate_idx_by_rssi(s32 rssi,enum nl80211_band band)85 il3945_get_rate_idx_by_rssi(s32 rssi, enum nl80211_band band)
86 {
87 u32 idx = 0;
88 u32 table_size = 0;
89 struct il3945_tpt_entry *tpt_table = NULL;
90
91 if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
92 rssi = IL_MIN_RSSI_VAL;
93
94 switch (band) {
95 case NL80211_BAND_2GHZ:
96 tpt_table = il3945_tpt_table_g;
97 table_size = ARRAY_SIZE(il3945_tpt_table_g);
98 break;
99 case NL80211_BAND_5GHZ:
100 tpt_table = il3945_tpt_table_a;
101 table_size = ARRAY_SIZE(il3945_tpt_table_a);
102 break;
103 default:
104 BUG();
105 break;
106 }
107
108 while (idx < table_size && rssi < tpt_table[idx].min_rssi)
109 idx++;
110
111 idx = min(idx, table_size - 1);
112
113 return tpt_table[idx].idx;
114 }
115
116 static void
il3945_clear_win(struct il3945_rate_scale_data * win)117 il3945_clear_win(struct il3945_rate_scale_data *win)
118 {
119 win->data = 0;
120 win->success_counter = 0;
121 win->success_ratio = -1;
122 win->counter = 0;
123 win->average_tpt = IL_INVALID_VALUE;
124 win->stamp = 0;
125 }
126
127 /*
128 * il3945_rate_scale_flush_wins - flush out the rate scale wins
129 *
130 * Returns the number of wins that have gathered data but were
131 * not flushed. If there were any that were not flushed, then
132 * reschedule the rate flushing routine.
133 */
134 static int
il3945_rate_scale_flush_wins(struct il3945_rs_sta * rs_sta)135 il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
136 {
137 int unflushed = 0;
138 int i;
139 unsigned long flags;
140 struct il_priv *il __maybe_unused = rs_sta->il;
141
142 /*
143 * For each rate, if we have collected data on that rate
144 * and it has been more than RATE_WIN_FLUSH
145 * since we flushed, clear out the gathered stats
146 */
147 for (i = 0; i < RATE_COUNT_3945; i++) {
148 if (!rs_sta->win[i].counter)
149 continue;
150
151 spin_lock_irqsave(&rs_sta->lock, flags);
152 if (time_after(jiffies, rs_sta->win[i].stamp + RATE_WIN_FLUSH)) {
153 D_RATE("flushing %d samples of rate " "idx %d\n",
154 rs_sta->win[i].counter, i);
155 il3945_clear_win(&rs_sta->win[i]);
156 } else
157 unflushed++;
158 spin_unlock_irqrestore(&rs_sta->lock, flags);
159 }
160
161 return unflushed;
162 }
163
164 #define RATE_FLUSH_MAX 5000 /* msec */
165 #define RATE_FLUSH_MIN 50 /* msec */
166 #define IL_AVERAGE_PACKETS 1500
167
168 static void
il3945_bg_rate_scale_flush(struct timer_list * t)169 il3945_bg_rate_scale_flush(struct timer_list *t)
170 {
171 struct il3945_rs_sta *rs_sta = timer_container_of(rs_sta, t,
172 rate_scale_flush);
173 struct il_priv *il __maybe_unused = rs_sta->il;
174 int unflushed = 0;
175 unsigned long flags;
176 u32 packet_count, duration, pps;
177
178 D_RATE("enter\n");
179
180 unflushed = il3945_rate_scale_flush_wins(rs_sta);
181
182 spin_lock_irqsave(&rs_sta->lock, flags);
183
184 /* Number of packets Rx'd since last time this timer ran */
185 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
186
187 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
188
189 if (unflushed) {
190 duration =
191 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
192
193 D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
194
195 /* Determine packets per second */
196 if (duration)
197 pps = (packet_count * 1000) / duration;
198 else
199 pps = 0;
200
201 if (pps) {
202 duration = (IL_AVERAGE_PACKETS * 1000) / pps;
203 if (duration < RATE_FLUSH_MIN)
204 duration = RATE_FLUSH_MIN;
205 else if (duration > RATE_FLUSH_MAX)
206 duration = RATE_FLUSH_MAX;
207 } else
208 duration = RATE_FLUSH_MAX;
209
210 rs_sta->flush_time = msecs_to_jiffies(duration);
211
212 D_RATE("new flush period: %d msec ave %d\n", duration,
213 packet_count);
214
215 mod_timer(&rs_sta->rate_scale_flush,
216 jiffies + rs_sta->flush_time);
217
218 rs_sta->last_partial_flush = jiffies;
219 } else {
220 rs_sta->flush_time = RATE_FLUSH;
221 rs_sta->flush_pending = 0;
222 }
223 /* If there weren't any unflushed entries, we don't schedule the timer
224 * to run again */
225
226 rs_sta->last_flush = jiffies;
227
228 spin_unlock_irqrestore(&rs_sta->lock, flags);
229
230 D_RATE("leave\n");
231 }
232
233 /*
234 * il3945_collect_tx_data - Update the success/failure sliding win
235 *
236 * We keep a sliding win of the last 64 packets transmitted
237 * at this rate. win->data contains the bitmask of successful
238 * packets.
239 */
240 static void
il3945_collect_tx_data(struct il3945_rs_sta * rs_sta,struct il3945_rate_scale_data * win,int success,int retries,int idx)241 il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
242 struct il3945_rate_scale_data *win, int success,
243 int retries, int idx)
244 {
245 unsigned long flags;
246 s32 fail_count;
247 struct il_priv *il __maybe_unused = rs_sta->il;
248
249 if (!retries) {
250 D_RATE("leave: retries == 0 -- should be at least 1\n");
251 return;
252 }
253
254 spin_lock_irqsave(&rs_sta->lock, flags);
255
256 /*
257 * Keep track of only the latest 62 tx frame attempts in this rate's
258 * history win; anything older isn't really relevant any more.
259 * If we have filled up the sliding win, drop the oldest attempt;
260 * if the oldest attempt (highest bit in bitmap) shows "success",
261 * subtract "1" from the success counter (this is the main reason
262 * we keep these bitmaps!).
263 * */
264 while (retries > 0) {
265 if (win->counter >= RATE_MAX_WINDOW) {
266
267 /* remove earliest */
268 win->counter = RATE_MAX_WINDOW - 1;
269
270 if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
271 win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
272 win->success_counter--;
273 }
274 }
275
276 /* Increment frames-attempted counter */
277 win->counter++;
278
279 /* Shift bitmap by one frame (throw away oldest history),
280 * OR in "1", and increment "success" if this
281 * frame was successful. */
282 win->data <<= 1;
283 if (success > 0) {
284 win->success_counter++;
285 win->data |= 0x1;
286 success--;
287 }
288
289 retries--;
290 }
291
292 /* Calculate current success ratio, avoid divide-by-0! */
293 if (win->counter > 0)
294 win->success_ratio =
295 128 * (100 * win->success_counter) / win->counter;
296 else
297 win->success_ratio = IL_INVALID_VALUE;
298
299 fail_count = win->counter - win->success_counter;
300
301 /* Calculate average throughput, if we have enough history. */
302 if (fail_count >= RATE_MIN_FAILURE_TH ||
303 win->success_counter >= RATE_MIN_SUCCESS_TH)
304 win->average_tpt =
305 ((win->success_ratio * rs_sta->expected_tpt[idx] +
306 64) / 128);
307 else
308 win->average_tpt = IL_INVALID_VALUE;
309
310 /* Tag this win as having been updated */
311 win->stamp = jiffies;
312
313 spin_unlock_irqrestore(&rs_sta->lock, flags);
314 }
315
316 /*
317 * Called after adding a new station to initialize rate scaling
318 */
319 void
il3945_rs_rate_init(struct il_priv * il,struct ieee80211_sta * sta,u8 sta_id)320 il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
321 {
322 struct ieee80211_hw *hw = il->hw;
323 struct ieee80211_conf *conf = &il->hw->conf;
324 struct il3945_sta_priv *psta;
325 struct il3945_rs_sta *rs_sta;
326 struct ieee80211_supported_band *sband;
327 int i;
328
329 D_INFO("enter\n");
330 if (sta_id == il->hw_params.bcast_id)
331 goto out;
332
333 psta = (struct il3945_sta_priv *)sta->drv_priv;
334 rs_sta = &psta->rs_sta;
335 sband = hw->wiphy->bands[conf->chandef.chan->band];
336
337 rs_sta->il = il;
338
339 rs_sta->start_rate = RATE_INVALID;
340
341 /* default to just 802.11b */
342 rs_sta->expected_tpt = il3945_expected_tpt_b;
343
344 rs_sta->last_partial_flush = jiffies;
345 rs_sta->last_flush = jiffies;
346 rs_sta->flush_time = RATE_FLUSH;
347 rs_sta->last_tx_packets = 0;
348
349 for (i = 0; i < RATE_COUNT_3945; i++)
350 il3945_clear_win(&rs_sta->win[i]);
351
352 /* TODO: what is a good starting rate for STA? About middle? Maybe not
353 * the lowest or the highest rate.. Could consider using RSSI from
354 * previous packets? Need to have IEEE 802.1X auth succeed immediately
355 * after assoc.. */
356
357 for (i = sband->n_bitrates - 1; i >= 0; i--) {
358 if (sta->deflink.supp_rates[sband->band] & (1 << i)) {
359 rs_sta->last_txrate_idx = i;
360 break;
361 }
362 }
363
364 il->_3945.sta_supp_rates = sta->deflink.supp_rates[sband->band];
365 /* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
366 if (sband->band == NL80211_BAND_5GHZ) {
367 rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
368 il->_3945.sta_supp_rates <<= IL_FIRST_OFDM_RATE;
369 }
370
371 out:
372 il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
373
374 D_INFO("leave\n");
375 }
376
377 static void *
il3945_rs_alloc(struct ieee80211_hw * hw)378 il3945_rs_alloc(struct ieee80211_hw *hw)
379 {
380 return hw->priv;
381 }
382
383 /* rate scale requires free function to be implemented */
384 static void
il3945_rs_free(void * il)385 il3945_rs_free(void *il)
386 {
387 }
388
389 static void *
il3945_rs_alloc_sta(void * il_priv,struct ieee80211_sta * sta,gfp_t gfp)390 il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
391 {
392 struct il3945_rs_sta *rs_sta;
393 struct il3945_sta_priv *psta = (void *)sta->drv_priv;
394 struct il_priv *il __maybe_unused = il_priv;
395
396 D_RATE("enter\n");
397
398 rs_sta = &psta->rs_sta;
399
400 spin_lock_init(&rs_sta->lock);
401 timer_setup(&rs_sta->rate_scale_flush, il3945_bg_rate_scale_flush, 0);
402 D_RATE("leave\n");
403
404 return rs_sta;
405 }
406
407 static void
il3945_rs_free_sta(void * il_priv,struct ieee80211_sta * sta,void * il_sta)408 il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
409 {
410 struct il3945_rs_sta *rs_sta = il_sta;
411
412 /*
413 * Be careful not to use any members of il3945_rs_sta (like trying
414 * to use il_priv to print out debugging) since it may not be fully
415 * initialized at this point.
416 */
417 timer_delete_sync(&rs_sta->rate_scale_flush);
418 }
419
420 /*
421 * il3945_rs_tx_status - Update rate control values based on Tx results
422 *
423 * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
424 * the hardware for each rate.
425 */
426 static void
il3945_rs_tx_status(void * il_rate,struct ieee80211_supported_band * sband,struct ieee80211_sta * sta,void * il_sta,struct sk_buff * skb)427 il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
428 struct ieee80211_sta *sta, void *il_sta,
429 struct sk_buff *skb)
430 {
431 s8 retries = 0, current_count;
432 int scale_rate_idx, first_idx, last_idx;
433 unsigned long flags;
434 struct il_priv *il = (struct il_priv *)il_rate;
435 struct il3945_rs_sta *rs_sta = il_sta;
436 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
437
438 D_RATE("enter\n");
439
440 retries = info->status.rates[0].count;
441 /* Sanity Check for retries */
442 if (retries > RATE_RETRY_TH)
443 retries = RATE_RETRY_TH;
444
445 first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
446 if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
447 D_RATE("leave: Rate out of bounds: %d\n", first_idx);
448 return;
449 }
450
451 if (!il_sta) {
452 D_RATE("leave: No STA il data to update!\n");
453 return;
454 }
455
456 /* Treat uninitialized rate scaling data same as non-existing. */
457 if (!rs_sta->il) {
458 D_RATE("leave: STA il data uninitialized!\n");
459 return;
460 }
461
462 rs_sta->tx_packets++;
463
464 scale_rate_idx = first_idx;
465 last_idx = first_idx;
466
467 /*
468 * Update the win for each rate. We determine which rates
469 * were Tx'd based on the total number of retries vs. the number
470 * of retries configured for each rate -- currently set to the
471 * il value 'retry_rate' vs. rate specific
472 *
473 * On exit from this while loop last_idx indicates the rate
474 * at which the frame was finally transmitted (or failed if no
475 * ACK)
476 */
477 while (retries > 1) {
478 if ((retries - 1) < il->retry_rate) {
479 current_count = (retries - 1);
480 last_idx = scale_rate_idx;
481 } else {
482 current_count = il->retry_rate;
483 last_idx = il3945_rs_next_rate(il, scale_rate_idx);
484 }
485
486 /* Update this rate accounting for as many retries
487 * as was used for it (per current_count) */
488 il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
489 current_count, scale_rate_idx);
490 D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
491 current_count);
492
493 retries -= current_count;
494
495 scale_rate_idx = last_idx;
496 }
497
498 /* Update the last idx win with success/failure based on ACK */
499 D_RATE("Update rate %d with %s.\n", last_idx,
500 (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
501 il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
502 info->flags & IEEE80211_TX_STAT_ACK, 1,
503 last_idx);
504
505 /* We updated the rate scale win -- if its been more than
506 * flush_time since the last run, schedule the flush
507 * again */
508 spin_lock_irqsave(&rs_sta->lock, flags);
509
510 if (!rs_sta->flush_pending &&
511 time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) {
512
513 rs_sta->last_partial_flush = jiffies;
514 rs_sta->flush_pending = 1;
515 mod_timer(&rs_sta->rate_scale_flush,
516 jiffies + rs_sta->flush_time);
517 }
518
519 spin_unlock_irqrestore(&rs_sta->lock, flags);
520
521 D_RATE("leave\n");
522 }
523
524 static u16
il3945_get_adjacent_rate(struct il3945_rs_sta * rs_sta,u8 idx,u16 rate_mask,enum nl80211_band band)525 il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
526 enum nl80211_band band)
527 {
528 u8 high = RATE_INVALID;
529 u8 low = RATE_INVALID;
530 struct il_priv *il __maybe_unused = rs_sta->il;
531
532 /* 802.11A walks to the next literal adjacent rate in
533 * the rate table */
534 if (unlikely(band == NL80211_BAND_5GHZ)) {
535 int i;
536 u32 mask;
537
538 /* Find the previous rate that is in the rate mask */
539 i = idx - 1;
540 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
541 if (rate_mask & mask) {
542 low = i;
543 break;
544 }
545 }
546
547 /* Find the next rate that is in the rate mask */
548 i = idx + 1;
549 for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
550 if (rate_mask & mask) {
551 high = i;
552 break;
553 }
554 }
555
556 return (high << 8) | low;
557 }
558
559 low = idx;
560 while (low != RATE_INVALID) {
561 if (rs_sta->tgg)
562 low = il3945_rates[low].prev_rs_tgg;
563 else
564 low = il3945_rates[low].prev_rs;
565 if (low == RATE_INVALID)
566 break;
567 if (rate_mask & (1 << low))
568 break;
569 D_RATE("Skipping masked lower rate: %d\n", low);
570 }
571
572 high = idx;
573 while (high != RATE_INVALID) {
574 if (rs_sta->tgg)
575 high = il3945_rates[high].next_rs_tgg;
576 else
577 high = il3945_rates[high].next_rs;
578 if (high == RATE_INVALID)
579 break;
580 if (rate_mask & (1 << high))
581 break;
582 D_RATE("Skipping masked higher rate: %d\n", high);
583 }
584
585 return (high << 8) | low;
586 }
587
588 /*
589 * il3945_rs_get_rate - find the rate for the requested packet
590 *
591 * Returns the ieee80211_rate structure allocated by the driver.
592 *
593 * The rate control algorithm has no internal mapping between hw_mode's
594 * rate ordering and the rate ordering used by the rate control algorithm.
595 *
596 * The rate control algorithm uses a single table of rates that goes across
597 * the entire A/B/G spectrum vs. being limited to just one particular
598 * hw_mode.
599 *
600 * As such, we can't convert the idx obtained below into the hw_mode's
601 * rate table and must reference the driver allocated rate table
602 *
603 */
604 static void
il3945_rs_get_rate(void * il_r,struct ieee80211_sta * sta,void * il_sta,struct ieee80211_tx_rate_control * txrc)605 il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
606 struct ieee80211_tx_rate_control *txrc)
607 {
608 struct ieee80211_supported_band *sband = txrc->sband;
609 struct sk_buff *skb = txrc->skb;
610 u8 low = RATE_INVALID;
611 u8 high = RATE_INVALID;
612 u16 high_low;
613 int idx;
614 struct il3945_rs_sta *rs_sta = il_sta;
615 struct il3945_rate_scale_data *win = NULL;
616 int current_tpt = IL_INVALID_VALUE;
617 int low_tpt = IL_INVALID_VALUE;
618 int high_tpt = IL_INVALID_VALUE;
619 u32 fail_count;
620 s8 scale_action = 0;
621 unsigned long flags;
622 u16 rate_mask;
623 s8 max_rate_idx = -1;
624 struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
625 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
626
627 D_RATE("enter\n");
628
629 /* Treat uninitialized rate scaling data same as non-existing. */
630 if (rs_sta && !rs_sta->il) {
631 D_RATE("Rate scaling information not initialized yet.\n");
632 il_sta = NULL;
633 }
634
635 rate_mask = sta->deflink.supp_rates[sband->band];
636
637 /* get user max rate if set */
638 max_rate_idx = fls(txrc->rate_idx_mask) - 1;
639 if (sband->band == NL80211_BAND_5GHZ && max_rate_idx != -1)
640 max_rate_idx += IL_FIRST_OFDM_RATE;
641 if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
642 max_rate_idx = -1;
643
644 idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
645
646 if (sband->band == NL80211_BAND_5GHZ)
647 rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
648
649 spin_lock_irqsave(&rs_sta->lock, flags);
650
651 /* for recent assoc, choose best rate regarding
652 * to rssi value
653 */
654 if (rs_sta->start_rate != RATE_INVALID) {
655 if (rs_sta->start_rate < idx &&
656 (rate_mask & (1 << rs_sta->start_rate)))
657 idx = rs_sta->start_rate;
658 rs_sta->start_rate = RATE_INVALID;
659 }
660
661 /* force user max rate if set by user */
662 if (max_rate_idx != -1 && max_rate_idx < idx) {
663 if (rate_mask & (1 << max_rate_idx))
664 idx = max_rate_idx;
665 }
666
667 win = &(rs_sta->win[idx]);
668
669 fail_count = win->counter - win->success_counter;
670
671 if (fail_count < RATE_MIN_FAILURE_TH &&
672 win->success_counter < RATE_MIN_SUCCESS_TH) {
673 spin_unlock_irqrestore(&rs_sta->lock, flags);
674
675 D_RATE("Invalid average_tpt on rate %d: "
676 "counter: %d, success_counter: %d, "
677 "expected_tpt is %sNULL\n", idx, win->counter,
678 win->success_counter,
679 rs_sta->expected_tpt ? "not " : "");
680
681 /* Can't calculate this yet; not enough history */
682 win->average_tpt = IL_INVALID_VALUE;
683 goto out;
684
685 }
686
687 current_tpt = win->average_tpt;
688
689 high_low =
690 il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
691 low = high_low & 0xff;
692 high = (high_low >> 8) & 0xff;
693
694 /* If user set max rate, dont allow higher than user constrain */
695 if (max_rate_idx != -1 && max_rate_idx < high)
696 high = RATE_INVALID;
697
698 /* Collect Measured throughputs of adjacent rates */
699 if (low != RATE_INVALID)
700 low_tpt = rs_sta->win[low].average_tpt;
701
702 if (high != RATE_INVALID)
703 high_tpt = rs_sta->win[high].average_tpt;
704
705 spin_unlock_irqrestore(&rs_sta->lock, flags);
706
707 scale_action = 0;
708
709 /* Low success ratio , need to drop the rate */
710 if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
711 D_RATE("decrease rate because of low success_ratio\n");
712 scale_action = -1;
713 /* No throughput measured yet for adjacent rates,
714 * try increase */
715 } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
716
717 if (high != RATE_INVALID &&
718 win->success_ratio >= RATE_INCREASE_TH)
719 scale_action = 1;
720 else if (low != RATE_INVALID)
721 scale_action = 0;
722
723 /* Both adjacent throughputs are measured, but neither one has
724 * better throughput; we're using the best rate, don't change
725 * it! */
726 } else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
727 && low_tpt < current_tpt && high_tpt < current_tpt) {
728
729 D_RATE("No action -- low [%d] & high [%d] < "
730 "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
731 scale_action = 0;
732
733 /* At least one of the rates has better throughput */
734 } else {
735 if (high_tpt != IL_INVALID_VALUE) {
736
737 /* High rate has better throughput, Increase
738 * rate */
739 if (high_tpt > current_tpt &&
740 win->success_ratio >= RATE_INCREASE_TH)
741 scale_action = 1;
742 else {
743 D_RATE("decrease rate because of high tpt\n");
744 scale_action = 0;
745 }
746 } else if (low_tpt != IL_INVALID_VALUE) {
747 if (low_tpt > current_tpt) {
748 D_RATE("decrease rate because of low tpt\n");
749 scale_action = -1;
750 } else if (win->success_ratio >= RATE_INCREASE_TH) {
751 /* Lower rate has better
752 * throughput,decrease rate */
753 scale_action = 1;
754 }
755 }
756 }
757
758 /* Sanity check; asked for decrease, but success rate or throughput
759 * has been good at old rate. Don't change it. */
760 if (scale_action == -1 && low != RATE_INVALID &&
761 (win->success_ratio > RATE_HIGH_TH ||
762 current_tpt > 100 * rs_sta->expected_tpt[low]))
763 scale_action = 0;
764
765 switch (scale_action) {
766 case -1:
767 /* Decrease rate */
768 if (low != RATE_INVALID)
769 idx = low;
770 break;
771 case 1:
772 /* Increase rate */
773 if (high != RATE_INVALID)
774 idx = high;
775
776 break;
777 case 0:
778 default:
779 /* No change */
780 break;
781 }
782
783 D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
784 low, high);
785
786 out:
787
788 if (sband->band == NL80211_BAND_5GHZ) {
789 if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
790 idx = IL_FIRST_OFDM_RATE;
791 rs_sta->last_txrate_idx = idx;
792 info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
793 } else {
794 rs_sta->last_txrate_idx = idx;
795 info->control.rates[0].idx = rs_sta->last_txrate_idx;
796 }
797 info->control.rates[0].count = 1;
798
799 D_RATE("leave: %d\n", idx);
800 }
801
802 #ifdef CONFIG_MAC80211_DEBUGFS
803
804 static ssize_t
il3945_sta_dbgfs_stats_table_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)805 il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
806 size_t count, loff_t *ppos)
807 {
808 char *buff;
809 int desc = 0;
810 int j;
811 ssize_t ret;
812 struct il3945_rs_sta *lq_sta = file->private_data;
813
814 buff = kmalloc(1024, GFP_KERNEL);
815 if (!buff)
816 return -ENOMEM;
817
818 desc +=
819 sprintf(buff + desc,
820 "tx packets=%d last rate idx=%d\n"
821 "rate=0x%X flush time %d\n", lq_sta->tx_packets,
822 lq_sta->last_txrate_idx, lq_sta->start_rate,
823 jiffies_to_msecs(lq_sta->flush_time));
824 for (j = 0; j < RATE_COUNT_3945; j++) {
825 desc +=
826 sprintf(buff + desc, "counter=%d success=%d %%=%d\n",
827 lq_sta->win[j].counter,
828 lq_sta->win[j].success_counter,
829 lq_sta->win[j].success_ratio);
830 }
831 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
832 kfree(buff);
833 return ret;
834 }
835
836 static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
837 .read = il3945_sta_dbgfs_stats_table_read,
838 .open = simple_open,
839 .llseek = default_llseek,
840 };
841
842 static void
il3945_add_debugfs(void * il,void * il_sta,struct dentry * dir)843 il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir)
844 {
845 struct il3945_rs_sta *lq_sta = il_sta;
846
847 debugfs_create_file("rate_stats_table", 0600, dir, lq_sta,
848 &rs_sta_dbgfs_stats_table_ops);
849 }
850 #endif
851
852 /*
853 * Initialization of rate scaling information is done by driver after
854 * the station is added. Since mac80211 calls this function before a
855 * station is added we ignore it.
856 */
857 static void
il3945_rs_rate_init_stub(void * il_r,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * il_sta)858 il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
859 struct cfg80211_chan_def *chandef,
860 struct ieee80211_sta *sta, void *il_sta)
861 {
862 }
863
864 static const struct rate_control_ops rs_ops = {
865 .name = RS_NAME,
866 .tx_status = il3945_rs_tx_status,
867 .get_rate = il3945_rs_get_rate,
868 .rate_init = il3945_rs_rate_init_stub,
869 .alloc = il3945_rs_alloc,
870 .free = il3945_rs_free,
871 .alloc_sta = il3945_rs_alloc_sta,
872 .free_sta = il3945_rs_free_sta,
873 #ifdef CONFIG_MAC80211_DEBUGFS
874 .add_sta_debugfs = il3945_add_debugfs,
875 #endif
876
877 };
878
879 void
il3945_rate_scale_init(struct ieee80211_hw * hw,s32 sta_id)880 il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
881 {
882 struct il_priv *il = hw->priv;
883 s32 rssi = 0;
884 unsigned long flags;
885 struct il3945_rs_sta *rs_sta;
886 struct ieee80211_sta *sta;
887 struct il3945_sta_priv *psta;
888
889 D_RATE("enter\n");
890
891 rcu_read_lock();
892
893 sta = ieee80211_find_sta(il->vif, il->stations[sta_id].sta.sta.addr);
894 if (!sta) {
895 D_RATE("Unable to find station to initialize rate scaling.\n");
896 rcu_read_unlock();
897 return;
898 }
899
900 psta = (void *)sta->drv_priv;
901 rs_sta = &psta->rs_sta;
902
903 spin_lock_irqsave(&rs_sta->lock, flags);
904
905 rs_sta->tgg = 0;
906 switch (il->band) {
907 case NL80211_BAND_2GHZ:
908 /* TODO: this always does G, not a regression */
909 if (il->active.flags & RXON_FLG_TGG_PROTECT_MSK) {
910 rs_sta->tgg = 1;
911 rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
912 } else
913 rs_sta->expected_tpt = il3945_expected_tpt_g;
914 break;
915 case NL80211_BAND_5GHZ:
916 rs_sta->expected_tpt = il3945_expected_tpt_a;
917 break;
918 default:
919 BUG();
920 break;
921 }
922
923 spin_unlock_irqrestore(&rs_sta->lock, flags);
924
925 rssi = il->_3945.last_rx_rssi;
926 if (rssi == 0)
927 rssi = IL_MIN_RSSI_VAL;
928
929 D_RATE("Network RSSI: %d\n", rssi);
930
931 rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
932
933 D_RATE("leave: rssi %d assign rate idx: " "%d (plcp 0x%x)\n", rssi,
934 rs_sta->start_rate, il3945_rates[rs_sta->start_rate].plcp);
935 rcu_read_unlock();
936 }
937
938 int
il3945_rate_control_register(void)939 il3945_rate_control_register(void)
940 {
941 return ieee80211_rate_control_register(&rs_ops);
942 }
943
944 void
il3945_rate_control_unregister(void)945 il3945_rate_control_unregister(void)
946 {
947 ieee80211_rate_control_unregister(&rs_ops);
948 }
949