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 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 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 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 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 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 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 * 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 385 il3945_rs_free(void *il) 386 { 387 } 388 389 static void * 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 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 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 /* Treat uninitialized rate scaling data same as non-existing. */ 452 if (!rs_sta->il) { 453 D_RATE("leave: STA il data uninitialized!\n"); 454 return; 455 } 456 457 rs_sta->tx_packets++; 458 459 scale_rate_idx = first_idx; 460 last_idx = first_idx; 461 462 /* 463 * Update the win for each rate. We determine which rates 464 * were Tx'd based on the total number of retries vs. the number 465 * of retries configured for each rate -- currently set to the 466 * il value 'retry_rate' vs. rate specific 467 * 468 * On exit from this while loop last_idx indicates the rate 469 * at which the frame was finally transmitted (or failed if no 470 * ACK) 471 */ 472 while (retries > 1) { 473 if ((retries - 1) < il->retry_rate) { 474 current_count = (retries - 1); 475 last_idx = scale_rate_idx; 476 } else { 477 current_count = il->retry_rate; 478 last_idx = il3945_rs_next_rate(il, scale_rate_idx); 479 } 480 481 /* Update this rate accounting for as many retries 482 * as was used for it (per current_count) */ 483 il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0, 484 current_count, scale_rate_idx); 485 D_RATE("Update rate %d for %d retries.\n", scale_rate_idx, 486 current_count); 487 488 retries -= current_count; 489 490 scale_rate_idx = last_idx; 491 } 492 493 /* Update the last idx win with success/failure based on ACK */ 494 D_RATE("Update rate %d with %s.\n", last_idx, 495 (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure"); 496 il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx], 497 info->flags & IEEE80211_TX_STAT_ACK, 1, 498 last_idx); 499 500 /* We updated the rate scale win -- if its been more than 501 * flush_time since the last run, schedule the flush 502 * again */ 503 spin_lock_irqsave(&rs_sta->lock, flags); 504 505 if (!rs_sta->flush_pending && 506 time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) { 507 508 rs_sta->last_partial_flush = jiffies; 509 rs_sta->flush_pending = 1; 510 mod_timer(&rs_sta->rate_scale_flush, 511 jiffies + rs_sta->flush_time); 512 } 513 514 spin_unlock_irqrestore(&rs_sta->lock, flags); 515 516 D_RATE("leave\n"); 517 } 518 519 static u16 520 il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask, 521 enum nl80211_band band) 522 { 523 u8 high = RATE_INVALID; 524 u8 low = RATE_INVALID; 525 struct il_priv *il __maybe_unused = rs_sta->il; 526 527 /* 802.11A walks to the next literal adjacent rate in 528 * the rate table */ 529 if (unlikely(band == NL80211_BAND_5GHZ)) { 530 int i; 531 u32 mask; 532 533 /* Find the previous rate that is in the rate mask */ 534 i = idx - 1; 535 for (mask = (1 << i); i >= 0; i--, mask >>= 1) { 536 if (rate_mask & mask) { 537 low = i; 538 break; 539 } 540 } 541 542 /* Find the next rate that is in the rate mask */ 543 i = idx + 1; 544 for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) { 545 if (rate_mask & mask) { 546 high = i; 547 break; 548 } 549 } 550 551 return (high << 8) | low; 552 } 553 554 low = idx; 555 while (low != RATE_INVALID) { 556 if (rs_sta->tgg) 557 low = il3945_rates[low].prev_rs_tgg; 558 else 559 low = il3945_rates[low].prev_rs; 560 if (low == RATE_INVALID) 561 break; 562 if (rate_mask & (1 << low)) 563 break; 564 D_RATE("Skipping masked lower rate: %d\n", low); 565 } 566 567 high = idx; 568 while (high != RATE_INVALID) { 569 if (rs_sta->tgg) 570 high = il3945_rates[high].next_rs_tgg; 571 else 572 high = il3945_rates[high].next_rs; 573 if (high == RATE_INVALID) 574 break; 575 if (rate_mask & (1 << high)) 576 break; 577 D_RATE("Skipping masked higher rate: %d\n", high); 578 } 579 580 return (high << 8) | low; 581 } 582 583 /* 584 * il3945_rs_get_rate - find the rate for the requested packet 585 * 586 * Returns the ieee80211_rate structure allocated by the driver. 587 * 588 * The rate control algorithm has no internal mapping between hw_mode's 589 * rate ordering and the rate ordering used by the rate control algorithm. 590 * 591 * The rate control algorithm uses a single table of rates that goes across 592 * the entire A/B/G spectrum vs. being limited to just one particular 593 * hw_mode. 594 * 595 * As such, we can't convert the idx obtained below into the hw_mode's 596 * rate table and must reference the driver allocated rate table 597 * 598 */ 599 static void 600 il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta, 601 struct ieee80211_tx_rate_control *txrc) 602 { 603 struct ieee80211_supported_band *sband = txrc->sband; 604 struct sk_buff *skb = txrc->skb; 605 u8 low = RATE_INVALID; 606 u8 high = RATE_INVALID; 607 u16 high_low; 608 int idx; 609 struct il3945_rs_sta *rs_sta = il_sta; 610 struct il3945_rate_scale_data *win = NULL; 611 int current_tpt = IL_INVALID_VALUE; 612 int low_tpt = IL_INVALID_VALUE; 613 int high_tpt = IL_INVALID_VALUE; 614 u32 fail_count; 615 s8 scale_action = 0; 616 unsigned long flags; 617 u16 rate_mask; 618 s8 max_rate_idx = -1; 619 struct il_priv *il __maybe_unused = (struct il_priv *)il_r; 620 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 621 622 D_RATE("enter\n"); 623 624 /* Treat uninitialized rate scaling data same as non-existing. */ 625 if (!rs_sta->il) { 626 D_RATE("Rate scaling information not initialized yet.\n"); 627 il_sta = NULL; 628 } 629 630 rate_mask = sta->deflink.supp_rates[sband->band]; 631 632 /* get user max rate if set */ 633 max_rate_idx = fls(txrc->rate_idx_mask) - 1; 634 if (sband->band == NL80211_BAND_5GHZ && max_rate_idx != -1) 635 max_rate_idx += IL_FIRST_OFDM_RATE; 636 if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT) 637 max_rate_idx = -1; 638 639 idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1); 640 641 if (sband->band == NL80211_BAND_5GHZ) 642 rate_mask = rate_mask << IL_FIRST_OFDM_RATE; 643 644 spin_lock_irqsave(&rs_sta->lock, flags); 645 646 /* for recent assoc, choose best rate regarding 647 * to rssi value 648 */ 649 if (rs_sta->start_rate != RATE_INVALID) { 650 if (rs_sta->start_rate < idx && 651 (rate_mask & (1 << rs_sta->start_rate))) 652 idx = rs_sta->start_rate; 653 rs_sta->start_rate = RATE_INVALID; 654 } 655 656 /* force user max rate if set by user */ 657 if (max_rate_idx != -1 && max_rate_idx < idx) { 658 if (rate_mask & (1 << max_rate_idx)) 659 idx = max_rate_idx; 660 } 661 662 win = &(rs_sta->win[idx]); 663 664 fail_count = win->counter - win->success_counter; 665 666 if (fail_count < RATE_MIN_FAILURE_TH && 667 win->success_counter < RATE_MIN_SUCCESS_TH) { 668 spin_unlock_irqrestore(&rs_sta->lock, flags); 669 670 D_RATE("Invalid average_tpt on rate %d: " 671 "counter: %d, success_counter: %d, " 672 "expected_tpt is %sNULL\n", idx, win->counter, 673 win->success_counter, 674 rs_sta->expected_tpt ? "not " : ""); 675 676 /* Can't calculate this yet; not enough history */ 677 win->average_tpt = IL_INVALID_VALUE; 678 goto out; 679 680 } 681 682 current_tpt = win->average_tpt; 683 684 high_low = 685 il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band); 686 low = high_low & 0xff; 687 high = (high_low >> 8) & 0xff; 688 689 /* If user set max rate, dont allow higher than user constrain */ 690 if (max_rate_idx != -1 && max_rate_idx < high) 691 high = RATE_INVALID; 692 693 /* Collect Measured throughputs of adjacent rates */ 694 if (low != RATE_INVALID) 695 low_tpt = rs_sta->win[low].average_tpt; 696 697 if (high != RATE_INVALID) 698 high_tpt = rs_sta->win[high].average_tpt; 699 700 spin_unlock_irqrestore(&rs_sta->lock, flags); 701 702 scale_action = 0; 703 704 /* Low success ratio , need to drop the rate */ 705 if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) { 706 D_RATE("decrease rate because of low success_ratio\n"); 707 scale_action = -1; 708 /* No throughput measured yet for adjacent rates, 709 * try increase */ 710 } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) { 711 712 if (high != RATE_INVALID && 713 win->success_ratio >= RATE_INCREASE_TH) 714 scale_action = 1; 715 else if (low != RATE_INVALID) 716 scale_action = 0; 717 718 /* Both adjacent throughputs are measured, but neither one has 719 * better throughput; we're using the best rate, don't change 720 * it! */ 721 } else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE 722 && low_tpt < current_tpt && high_tpt < current_tpt) { 723 724 D_RATE("No action -- low [%d] & high [%d] < " 725 "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt); 726 scale_action = 0; 727 728 /* At least one of the rates has better throughput */ 729 } else { 730 if (high_tpt != IL_INVALID_VALUE) { 731 732 /* High rate has better throughput, Increase 733 * rate */ 734 if (high_tpt > current_tpt && 735 win->success_ratio >= RATE_INCREASE_TH) 736 scale_action = 1; 737 else { 738 D_RATE("decrease rate because of high tpt\n"); 739 scale_action = 0; 740 } 741 } else if (low_tpt != IL_INVALID_VALUE) { 742 if (low_tpt > current_tpt) { 743 D_RATE("decrease rate because of low tpt\n"); 744 scale_action = -1; 745 } else if (win->success_ratio >= RATE_INCREASE_TH) { 746 /* Lower rate has better 747 * throughput,decrease rate */ 748 scale_action = 1; 749 } 750 } 751 } 752 753 /* Sanity check; asked for decrease, but success rate or throughput 754 * has been good at old rate. Don't change it. */ 755 if (scale_action == -1 && low != RATE_INVALID && 756 (win->success_ratio > RATE_HIGH_TH || 757 current_tpt > 100 * rs_sta->expected_tpt[low])) 758 scale_action = 0; 759 760 switch (scale_action) { 761 case -1: 762 /* Decrease rate */ 763 if (low != RATE_INVALID) 764 idx = low; 765 break; 766 case 1: 767 /* Increase rate */ 768 if (high != RATE_INVALID) 769 idx = high; 770 771 break; 772 case 0: 773 default: 774 /* No change */ 775 break; 776 } 777 778 D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action, 779 low, high); 780 781 out: 782 783 if (sband->band == NL80211_BAND_5GHZ) { 784 if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE)) 785 idx = IL_FIRST_OFDM_RATE; 786 rs_sta->last_txrate_idx = idx; 787 info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE; 788 } else { 789 rs_sta->last_txrate_idx = idx; 790 info->control.rates[0].idx = rs_sta->last_txrate_idx; 791 } 792 info->control.rates[0].count = 1; 793 794 D_RATE("leave: %d\n", idx); 795 } 796 797 #ifdef CONFIG_MAC80211_DEBUGFS 798 799 static ssize_t 800 il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf, 801 size_t count, loff_t *ppos) 802 { 803 char *buff; 804 int desc = 0; 805 int j; 806 ssize_t ret; 807 struct il3945_rs_sta *lq_sta = file->private_data; 808 809 buff = kmalloc(1024, GFP_KERNEL); 810 if (!buff) 811 return -ENOMEM; 812 813 desc += 814 sprintf(buff + desc, 815 "tx packets=%d last rate idx=%d\n" 816 "rate=0x%X flush time %d\n", lq_sta->tx_packets, 817 lq_sta->last_txrate_idx, lq_sta->start_rate, 818 jiffies_to_msecs(lq_sta->flush_time)); 819 for (j = 0; j < RATE_COUNT_3945; j++) { 820 desc += 821 sprintf(buff + desc, "counter=%d success=%d %%=%d\n", 822 lq_sta->win[j].counter, 823 lq_sta->win[j].success_counter, 824 lq_sta->win[j].success_ratio); 825 } 826 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc); 827 kfree(buff); 828 return ret; 829 } 830 831 static const struct file_operations rs_sta_dbgfs_stats_table_ops = { 832 .read = il3945_sta_dbgfs_stats_table_read, 833 .open = simple_open, 834 .llseek = default_llseek, 835 }; 836 837 static void 838 il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir) 839 { 840 struct il3945_rs_sta *lq_sta = il_sta; 841 842 debugfs_create_file("rate_stats_table", 0600, dir, lq_sta, 843 &rs_sta_dbgfs_stats_table_ops); 844 } 845 #endif 846 847 /* 848 * Initialization of rate scaling information is done by driver after 849 * the station is added. Since mac80211 calls this function before a 850 * station is added we ignore it. 851 */ 852 static void 853 il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband, 854 struct cfg80211_chan_def *chandef, 855 struct ieee80211_sta *sta, void *il_sta) 856 { 857 } 858 859 static const struct rate_control_ops rs_ops = { 860 .name = RS_NAME, 861 .tx_status = il3945_rs_tx_status, 862 .get_rate = il3945_rs_get_rate, 863 .rate_init = il3945_rs_rate_init_stub, 864 .alloc = il3945_rs_alloc, 865 .free = il3945_rs_free, 866 .alloc_sta = il3945_rs_alloc_sta, 867 .free_sta = il3945_rs_free_sta, 868 #ifdef CONFIG_MAC80211_DEBUGFS 869 .add_sta_debugfs = il3945_add_debugfs, 870 #endif 871 872 }; 873 874 void 875 il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id) 876 { 877 struct il_priv *il = hw->priv; 878 s32 rssi = 0; 879 unsigned long flags; 880 struct il3945_rs_sta *rs_sta; 881 struct ieee80211_sta *sta; 882 struct il3945_sta_priv *psta; 883 884 D_RATE("enter\n"); 885 886 rcu_read_lock(); 887 888 sta = ieee80211_find_sta(il->vif, il->stations[sta_id].sta.sta.addr); 889 if (!sta) { 890 D_RATE("Unable to find station to initialize rate scaling.\n"); 891 rcu_read_unlock(); 892 return; 893 } 894 895 psta = (void *)sta->drv_priv; 896 rs_sta = &psta->rs_sta; 897 898 spin_lock_irqsave(&rs_sta->lock, flags); 899 900 rs_sta->tgg = 0; 901 switch (il->band) { 902 case NL80211_BAND_2GHZ: 903 /* TODO: this always does G, not a regression */ 904 if (il->active.flags & RXON_FLG_TGG_PROTECT_MSK) { 905 rs_sta->tgg = 1; 906 rs_sta->expected_tpt = il3945_expected_tpt_g_prot; 907 } else 908 rs_sta->expected_tpt = il3945_expected_tpt_g; 909 break; 910 case NL80211_BAND_5GHZ: 911 rs_sta->expected_tpt = il3945_expected_tpt_a; 912 break; 913 default: 914 BUG(); 915 break; 916 } 917 918 spin_unlock_irqrestore(&rs_sta->lock, flags); 919 920 rssi = il->_3945.last_rx_rssi; 921 if (rssi == 0) 922 rssi = IL_MIN_RSSI_VAL; 923 924 D_RATE("Network RSSI: %d\n", rssi); 925 926 rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band); 927 928 D_RATE("leave: rssi %d assign rate idx: " "%d (plcp 0x%x)\n", rssi, 929 rs_sta->start_rate, il3945_rates[rs_sta->start_rate].plcp); 930 rcu_read_unlock(); 931 } 932 933 int 934 il3945_rate_control_register(void) 935 { 936 return ieee80211_rate_control_register(&rs_ops); 937 } 938 939 void 940 il3945_rate_control_unregister(void) 941 { 942 ieee80211_rate_control_unregister(&rs_ops); 943 } 944