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