1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2012-2014, 2018-2025 Intel Corporation 4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH 5 * Copyright (C) 2015-2017 Intel Deutschland GmbH 6 */ 7 #if defined(__FreeBSD__) 8 #include <linux/math64.h> 9 #endif 10 #include <net/mac80211.h> 11 12 #include "iwl-debug.h" 13 #include "iwl-io.h" 14 #include "iwl-prph.h" 15 #include "iwl-csr.h" 16 #include "mvm.h" 17 #include "fw/api/rs.h" 18 #include "fw/img.h" 19 20 /* 21 * Will return 0 even if the cmd failed when RFKILL is asserted unless 22 * CMD_WANT_SKB is set in cmd->flags. 23 */ 24 int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd) 25 { 26 int ret; 27 28 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP) 29 if (WARN_ON(mvm->d3_test_active)) 30 return -EIO; 31 #endif 32 33 /* 34 * Synchronous commands from this op-mode must hold 35 * the mutex, this ensures we don't try to send two 36 * (or more) synchronous commands at a time. 37 */ 38 if (!(cmd->flags & CMD_ASYNC)) 39 lockdep_assert_held(&mvm->mutex); 40 41 ret = iwl_trans_send_cmd(mvm->trans, cmd); 42 43 /* 44 * If the caller wants the SKB, then don't hide any problems, the 45 * caller might access the response buffer which will be NULL if 46 * the command failed. 47 */ 48 if (cmd->flags & CMD_WANT_SKB) 49 return ret; 50 51 /* 52 * Silently ignore failures if RFKILL is asserted or 53 * we are in suspend\resume process 54 */ 55 if (!ret || ret == -ERFKILL || ret == -EHOSTDOWN) 56 return 0; 57 return ret; 58 } 59 60 int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id, 61 u32 flags, u16 len, const void *data) 62 { 63 struct iwl_host_cmd cmd = { 64 .id = id, 65 .len = { len, }, 66 .data = { data, }, 67 .flags = flags, 68 }; 69 70 return iwl_mvm_send_cmd(mvm, &cmd); 71 } 72 73 /* 74 * We assume that the caller set the status to the success value 75 */ 76 int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd, 77 u32 *status) 78 { 79 struct iwl_rx_packet *pkt; 80 struct iwl_cmd_response *resp; 81 int ret, resp_len; 82 83 lockdep_assert_held(&mvm->mutex); 84 85 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP) 86 if (WARN_ON(mvm->d3_test_active)) 87 return -EIO; 88 #endif 89 90 /* 91 * Only synchronous commands can wait for status, 92 * we use WANT_SKB so the caller can't. 93 */ 94 if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB), 95 "cmd flags %x", cmd->flags)) 96 return -EINVAL; 97 98 cmd->flags |= CMD_WANT_SKB; 99 100 ret = iwl_trans_send_cmd(mvm->trans, cmd); 101 if (ret == -ERFKILL) { 102 /* 103 * The command failed because of RFKILL, don't update 104 * the status, leave it as success and return 0. 105 */ 106 return 0; 107 } else if (ret) { 108 return ret; 109 } 110 111 pkt = cmd->resp_pkt; 112 113 resp_len = iwl_rx_packet_payload_len(pkt); 114 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) { 115 ret = -EIO; 116 goto out_free_resp; 117 } 118 119 resp = (void *)pkt->data; 120 *status = le32_to_cpu(resp->status); 121 out_free_resp: 122 iwl_free_resp(cmd); 123 return ret; 124 } 125 126 /* 127 * We assume that the caller set the status to the sucess value 128 */ 129 int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len, 130 const void *data, u32 *status) 131 { 132 struct iwl_host_cmd cmd = { 133 .id = id, 134 .len = { len, }, 135 .data = { data, }, 136 }; 137 138 return iwl_mvm_send_cmd_status(mvm, &cmd, status); 139 } 140 141 int iwl_mvm_legacy_hw_idx_to_mac80211_idx(u32 rate_n_flags, 142 enum nl80211_band band) 143 { 144 int format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK; 145 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK; 146 bool is_LB = band == NL80211_BAND_2GHZ; 147 148 if (format == RATE_MCS_MOD_TYPE_LEGACY_OFDM) 149 return is_LB ? rate + IWL_FIRST_OFDM_RATE : 150 rate; 151 152 /* CCK is not allowed in HB */ 153 return is_LB ? rate : -1; 154 } 155 156 int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags, 157 enum nl80211_band band) 158 { 159 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK_V1; 160 int idx; 161 int band_offset = 0; 162 163 /* Legacy rate format, search for match in table */ 164 if (band != NL80211_BAND_2GHZ) 165 band_offset = IWL_FIRST_OFDM_RATE; 166 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++) 167 if (iwl_fw_rate_idx_to_plcp(idx) == rate) 168 return idx - band_offset; 169 170 return -1; 171 } 172 173 u8 iwl_mvm_mac80211_idx_to_hwrate(const struct iwl_fw *fw, int rate_idx) 174 { 175 return (rate_idx >= IWL_FIRST_OFDM_RATE ? 176 rate_idx - IWL_FIRST_OFDM_RATE : 177 rate_idx); 178 } 179 180 u8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac) 181 { 182 static const u8 mac80211_ac_to_ucode_ac[] = { 183 AC_VO, 184 AC_VI, 185 AC_BE, 186 AC_BK 187 }; 188 189 return mac80211_ac_to_ucode_ac[ac]; 190 } 191 192 void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 193 { 194 struct iwl_rx_packet *pkt = rxb_addr(rxb); 195 struct iwl_error_resp *err_resp = (void *)pkt->data; 196 197 IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n", 198 le32_to_cpu(err_resp->error_type), err_resp->cmd_id); 199 IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n", 200 le16_to_cpu(err_resp->bad_cmd_seq_num), 201 le32_to_cpu(err_resp->error_service)); 202 IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n", 203 le64_to_cpu(err_resp->timestamp)); 204 } 205 206 /* 207 * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h. 208 * The parameter should also be a combination of ANT_[ABC]. 209 */ 210 u8 first_antenna(u8 mask) 211 { 212 BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */ 213 if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */ 214 return BIT(0); 215 return BIT(ffs(mask) - 1); 216 } 217 218 #define MAX_ANT_NUM 2 219 /* 220 * Toggles between TX antennas to send the probe request on. 221 * Receives the bitmask of valid TX antennas and the *index* used 222 * for the last TX, and returns the next valid *index* to use. 223 * In order to set it in the tx_cmd, must do BIT(idx). 224 */ 225 u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx) 226 { 227 u8 ind = last_idx; 228 int i; 229 230 for (i = 0; i < MAX_ANT_NUM; i++) { 231 ind = (ind + 1) % MAX_ANT_NUM; 232 if (valid & BIT(ind)) 233 return ind; 234 } 235 236 WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid); 237 return last_idx; 238 } 239 240 /** 241 * iwl_mvm_send_lq_cmd() - Send link quality command 242 * @mvm: Driver data. 243 * @lq: Link quality command to send. 244 * 245 * The link quality command is sent as the last step of station creation. 246 * This is the special case in which init is set and we call a callback in 247 * this case to clear the state indicating that station creation is in 248 * progress. 249 * 250 * Returns: an error code indicating success or failure 251 */ 252 int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq) 253 { 254 struct iwl_host_cmd cmd = { 255 .id = LQ_CMD, 256 .len = { sizeof(struct iwl_lq_cmd), }, 257 .flags = CMD_ASYNC, 258 .data = { lq, }, 259 }; 260 261 if (WARN_ON(lq->sta_id == IWL_INVALID_STA || 262 iwl_mvm_has_tlc_offload(mvm))) 263 return -EINVAL; 264 265 return iwl_mvm_send_cmd(mvm, &cmd); 266 } 267 268 /** 269 * iwl_mvm_update_smps - Get a request to change the SMPS mode 270 * @mvm: Driver data. 271 * @vif: Pointer to the ieee80211_vif structure 272 * @req_type: The part of the driver who call for a change. 273 * @smps_request: The request to change the SMPS mode. 274 * @link_id: for MLO link_id, otherwise 0 (deflink) 275 * 276 * Get a requst to change the SMPS mode, 277 * and change it according to all other requests in the driver. 278 */ 279 void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 280 enum iwl_mvm_smps_type_request req_type, 281 enum ieee80211_smps_mode smps_request, 282 unsigned int link_id) 283 { 284 struct iwl_mvm_vif *mvmvif; 285 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC; 286 int i; 287 288 lockdep_assert_held(&mvm->mutex); 289 290 /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */ 291 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1) 292 return; 293 294 if (vif->type != NL80211_IFTYPE_STATION) 295 return; 296 297 /* SMPS is handled by firmware */ 298 if (iwl_mvm_has_rlc_offload(mvm)) 299 return; 300 301 mvmvif = iwl_mvm_vif_from_mac80211(vif); 302 303 if (WARN_ON_ONCE(!mvmvif->link[link_id])) 304 return; 305 306 mvmvif->link[link_id]->smps_requests[req_type] = smps_request; 307 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) { 308 if (mvmvif->link[link_id]->smps_requests[i] == 309 IEEE80211_SMPS_STATIC) { 310 smps_mode = IEEE80211_SMPS_STATIC; 311 break; 312 } 313 if (mvmvif->link[link_id]->smps_requests[i] == 314 IEEE80211_SMPS_DYNAMIC) 315 smps_mode = IEEE80211_SMPS_DYNAMIC; 316 } 317 318 /* SMPS is disabled in eSR */ 319 if (mvmvif->esr_active) 320 smps_mode = IEEE80211_SMPS_OFF; 321 322 ieee80211_request_smps(vif, link_id, smps_mode); 323 } 324 325 void iwl_mvm_update_smps_on_active_links(struct iwl_mvm *mvm, 326 struct ieee80211_vif *vif, 327 enum iwl_mvm_smps_type_request req_type, 328 enum ieee80211_smps_mode smps_request) 329 { 330 struct ieee80211_bss_conf *link_conf; 331 unsigned int link_id; 332 333 rcu_read_lock(); 334 for_each_vif_active_link(vif, link_conf, link_id) 335 iwl_mvm_update_smps(mvm, vif, req_type, smps_request, 336 link_id); 337 rcu_read_unlock(); 338 } 339 340 static bool iwl_wait_stats_complete(struct iwl_notif_wait_data *notif_wait, 341 struct iwl_rx_packet *pkt, void *data) 342 { 343 WARN_ON(pkt->hdr.cmd != STATISTICS_NOTIFICATION); 344 345 return true; 346 } 347 348 #define PERIODIC_STAT_RATE 5 349 350 int iwl_mvm_request_periodic_system_statistics(struct iwl_mvm *mvm, bool enable) 351 { 352 u32 flags = enable ? 0 : IWL_STATS_CFG_FLG_DISABLE_NTFY_MSK; 353 u32 type = enable ? (IWL_STATS_NTFY_TYPE_ID_OPER | 354 IWL_STATS_NTFY_TYPE_ID_OPER_PART1) : 0; 355 struct iwl_system_statistics_cmd system_cmd = { 356 .cfg_mask = cpu_to_le32(flags), 357 .config_time_sec = cpu_to_le32(enable ? 358 PERIODIC_STAT_RATE : 0), 359 .type_id_mask = cpu_to_le32(type), 360 }; 361 362 return iwl_mvm_send_cmd_pdu(mvm, 363 WIDE_ID(SYSTEM_GROUP, 364 SYSTEM_STATISTICS_CMD), 365 0, sizeof(system_cmd), &system_cmd); 366 } 367 368 static int iwl_mvm_request_system_statistics(struct iwl_mvm *mvm, bool clear, 369 u8 cmd_ver) 370 { 371 struct iwl_system_statistics_cmd system_cmd = { 372 .cfg_mask = clear ? 373 cpu_to_le32(IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK) : 374 cpu_to_le32(IWL_STATS_CFG_FLG_RESET_MSK | 375 IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK), 376 .type_id_mask = cpu_to_le32(IWL_STATS_NTFY_TYPE_ID_OPER | 377 IWL_STATS_NTFY_TYPE_ID_OPER_PART1), 378 }; 379 struct iwl_host_cmd cmd = { 380 .id = WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_CMD), 381 .len[0] = sizeof(system_cmd), 382 .data[0] = &system_cmd, 383 }; 384 struct iwl_notification_wait stats_wait; 385 static const u16 stats_complete[] = { 386 WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_END_NOTIF), 387 }; 388 int ret; 389 390 if (cmd_ver != 1) { 391 IWL_FW_CHECK_FAILED(mvm, 392 "Invalid system statistics command version:%d\n", 393 cmd_ver); 394 return -EOPNOTSUPP; 395 } 396 397 iwl_init_notification_wait(&mvm->notif_wait, &stats_wait, 398 stats_complete, ARRAY_SIZE(stats_complete), 399 NULL, NULL); 400 401 mvm->statistics_clear = clear; 402 ret = iwl_mvm_send_cmd(mvm, &cmd); 403 if (ret) { 404 iwl_remove_notification(&mvm->notif_wait, &stats_wait); 405 return ret; 406 } 407 408 /* 500ms for OPERATIONAL, PART1 and END notification should be enough 409 * for FW to collect data from all LMACs and send 410 * STATISTICS_NOTIFICATION to host 411 */ 412 ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 2); 413 if (ret) 414 return ret; 415 416 if (clear) 417 iwl_mvm_accu_radio_stats(mvm); 418 419 return ret; 420 } 421 422 int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear) 423 { 424 struct iwl_statistics_cmd scmd = { 425 .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0, 426 }; 427 428 struct iwl_host_cmd cmd = { 429 .id = STATISTICS_CMD, 430 .len[0] = sizeof(scmd), 431 .data[0] = &scmd, 432 }; 433 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, 434 WIDE_ID(SYSTEM_GROUP, 435 SYSTEM_STATISTICS_CMD), 436 IWL_FW_CMD_VER_UNKNOWN); 437 int ret; 438 439 /* 440 * Don't request statistics during restart, they'll not have any useful 441 * information right after restart, nor is clearing needed 442 */ 443 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) 444 return 0; 445 446 if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN) 447 return iwl_mvm_request_system_statistics(mvm, clear, cmd_ver); 448 449 /* From version 15 - STATISTICS_NOTIFICATION, the reply for 450 * STATISTICS_CMD is empty, and the response is with 451 * STATISTICS_NOTIFICATION notification 452 */ 453 if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, 454 STATISTICS_NOTIFICATION, 0) < 15) { 455 cmd.flags = CMD_WANT_SKB; 456 457 ret = iwl_mvm_send_cmd(mvm, &cmd); 458 if (ret) 459 return ret; 460 461 iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt); 462 iwl_free_resp(&cmd); 463 } else { 464 struct iwl_notification_wait stats_wait; 465 static const u16 stats_complete[] = { 466 STATISTICS_NOTIFICATION, 467 }; 468 469 iwl_init_notification_wait(&mvm->notif_wait, &stats_wait, 470 stats_complete, ARRAY_SIZE(stats_complete), 471 iwl_wait_stats_complete, NULL); 472 473 ret = iwl_mvm_send_cmd(mvm, &cmd); 474 if (ret) { 475 iwl_remove_notification(&mvm->notif_wait, &stats_wait); 476 return ret; 477 } 478 479 /* 200ms should be enough for FW to collect data from all 480 * LMACs and send STATISTICS_NOTIFICATION to host 481 */ 482 ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 5); 483 if (ret) 484 return ret; 485 } 486 487 if (clear) 488 iwl_mvm_accu_radio_stats(mvm); 489 490 return 0; 491 } 492 493 void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm) 494 { 495 mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time; 496 mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time; 497 mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf; 498 mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan; 499 } 500 501 struct iwl_mvm_diversity_iter_data { 502 struct iwl_mvm_phy_ctxt *ctxt; 503 bool result; 504 }; 505 506 static void iwl_mvm_diversity_iter(void *_data, u8 *mac, 507 struct ieee80211_vif *vif) 508 { 509 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 510 struct iwl_mvm_diversity_iter_data *data = _data; 511 int i, link_id; 512 513 for_each_mvm_vif_valid_link(mvmvif, link_id) { 514 struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id]; 515 516 if (link_info->phy_ctxt != data->ctxt) 517 continue; 518 519 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) { 520 if (link_info->smps_requests[i] == IEEE80211_SMPS_STATIC || 521 link_info->smps_requests[i] == IEEE80211_SMPS_DYNAMIC) { 522 data->result = false; 523 break; 524 } 525 } 526 } 527 } 528 529 bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm, 530 struct iwl_mvm_phy_ctxt *ctxt) 531 { 532 struct iwl_mvm_diversity_iter_data data = { 533 .ctxt = ctxt, 534 .result = true, 535 }; 536 537 lockdep_assert_held(&mvm->mutex); 538 539 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM) 540 return false; 541 542 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1) 543 return false; 544 545 if (mvm->cfg->rx_with_siso_diversity) 546 return false; 547 548 ieee80211_iterate_active_interfaces_atomic( 549 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 550 iwl_mvm_diversity_iter, &data); 551 552 return data.result; 553 } 554 555 void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm, 556 bool low_latency, u16 mac_id) 557 { 558 struct iwl_mac_low_latency_cmd cmd = { 559 .mac_id = cpu_to_le32(mac_id) 560 }; 561 562 if (!fw_has_capa(&mvm->fw->ucode_capa, 563 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA)) 564 return; 565 566 if (low_latency) { 567 /* currently we don't care about the direction */ 568 cmd.low_latency_rx = 1; 569 cmd.low_latency_tx = 1; 570 } 571 572 if (iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, LOW_LATENCY_CMD), 573 0, sizeof(cmd), &cmd)) 574 IWL_ERR(mvm, "Failed to send low latency command\n"); 575 } 576 577 int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 578 bool low_latency, 579 enum iwl_mvm_low_latency_cause cause) 580 { 581 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 582 int res; 583 bool prev; 584 585 lockdep_assert_held(&mvm->mutex); 586 587 prev = iwl_mvm_vif_low_latency(mvmvif); 588 iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause); 589 590 low_latency = iwl_mvm_vif_low_latency(mvmvif); 591 592 if (low_latency == prev) 593 return 0; 594 595 iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id); 596 597 res = iwl_mvm_update_quotas(mvm, false, NULL); 598 if (res) 599 return res; 600 601 iwl_mvm_bt_coex_vif_change(mvm); 602 603 return iwl_mvm_power_update_mac(mvm); 604 } 605 606 struct iwl_mvm_low_latency_iter { 607 bool result; 608 bool result_per_band[NUM_NL80211_BANDS]; 609 }; 610 611 static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif) 612 { 613 struct iwl_mvm_low_latency_iter *result = _data; 614 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 615 enum nl80211_band band; 616 617 if (iwl_mvm_vif_low_latency(mvmvif)) { 618 result->result = true; 619 620 if (!mvmvif->deflink.phy_ctxt) 621 return; 622 623 band = mvmvif->deflink.phy_ctxt->channel->band; 624 result->result_per_band[band] = true; 625 } 626 } 627 628 bool iwl_mvm_low_latency(struct iwl_mvm *mvm) 629 { 630 struct iwl_mvm_low_latency_iter data = {}; 631 632 ieee80211_iterate_active_interfaces_atomic( 633 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 634 iwl_mvm_ll_iter, &data); 635 636 return data.result; 637 } 638 639 bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band) 640 { 641 struct iwl_mvm_low_latency_iter data = {}; 642 643 ieee80211_iterate_active_interfaces_atomic( 644 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 645 iwl_mvm_ll_iter, &data); 646 647 return data.result_per_band[band]; 648 } 649 650 struct iwl_bss_iter_data { 651 struct ieee80211_vif *vif; 652 bool error; 653 }; 654 655 static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac, 656 struct ieee80211_vif *vif) 657 { 658 struct iwl_bss_iter_data *data = _data; 659 660 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p) 661 return; 662 663 if (data->vif) { 664 data->error = true; 665 return; 666 } 667 668 data->vif = vif; 669 } 670 671 struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm) 672 { 673 struct iwl_bss_iter_data bss_iter_data = {}; 674 675 ieee80211_iterate_active_interfaces_atomic( 676 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 677 iwl_mvm_bss_iface_iterator, &bss_iter_data); 678 679 if (bss_iter_data.error) 680 return ERR_PTR(-EINVAL); 681 682 return bss_iter_data.vif; 683 } 684 685 struct iwl_bss_find_iter_data { 686 struct ieee80211_vif *vif; 687 u32 macid; 688 }; 689 690 static void iwl_mvm_bss_find_iface_iterator(void *_data, u8 *mac, 691 struct ieee80211_vif *vif) 692 { 693 struct iwl_bss_find_iter_data *data = _data; 694 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 695 696 if (mvmvif->id == data->macid) 697 data->vif = vif; 698 } 699 700 struct ieee80211_vif *iwl_mvm_get_vif_by_macid(struct iwl_mvm *mvm, u32 macid) 701 { 702 struct iwl_bss_find_iter_data data = { 703 .macid = macid, 704 }; 705 706 lockdep_assert_held(&mvm->mutex); 707 708 ieee80211_iterate_active_interfaces_atomic( 709 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 710 iwl_mvm_bss_find_iface_iterator, &data); 711 712 return data.vif; 713 } 714 715 struct iwl_sta_iter_data { 716 bool assoc; 717 }; 718 719 static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac, 720 struct ieee80211_vif *vif) 721 { 722 struct iwl_sta_iter_data *data = _data; 723 724 if (vif->type != NL80211_IFTYPE_STATION) 725 return; 726 727 if (vif->cfg.assoc) 728 data->assoc = true; 729 } 730 731 bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm) 732 { 733 struct iwl_sta_iter_data data = { 734 .assoc = false, 735 }; 736 737 ieee80211_iterate_active_interfaces_atomic(mvm->hw, 738 IEEE80211_IFACE_ITER_NORMAL, 739 iwl_mvm_sta_iface_iterator, 740 &data); 741 return data.assoc; 742 } 743 744 unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm, 745 struct ieee80211_vif *vif) 746 { 747 unsigned int default_timeout = 748 mvm->trans->mac_cfg->base->wd_timeout; 749 750 /* 751 * We can't know when the station is asleep or awake, so we 752 * must disable the queue hang detection. 753 */ 754 if (fw_has_capa(&mvm->fw->ucode_capa, 755 IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) && 756 vif->type == NL80211_IFTYPE_AP) 757 return IWL_WATCHDOG_DISABLED; 758 return default_timeout; 759 } 760 761 void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 762 const char *errmsg) 763 { 764 struct iwl_fw_dbg_trigger_tlv *trig; 765 struct iwl_fw_dbg_trigger_mlme *trig_mlme; 766 767 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif), 768 FW_DBG_TRIGGER_MLME); 769 if (!trig) 770 goto out; 771 772 trig_mlme = (void *)trig->data; 773 774 if (trig_mlme->stop_connection_loss && 775 --trig_mlme->stop_connection_loss) 776 goto out; 777 778 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg); 779 780 out: 781 ieee80211_connection_loss(vif); 782 } 783 784 void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm, 785 struct ieee80211_vif *vif, 786 const struct ieee80211_sta *sta, 787 u16 tid) 788 { 789 struct iwl_fw_dbg_trigger_tlv *trig; 790 struct iwl_fw_dbg_trigger_ba *ba_trig; 791 792 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif), 793 FW_DBG_TRIGGER_BA); 794 if (!trig) 795 return; 796 797 ba_trig = (void *)trig->data; 798 799 if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid))) 800 return; 801 802 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, 803 "Frame from %pM timed out, tid %d", 804 sta->addr, tid); 805 } 806 807 u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed) 808 { 809 if (!elapsed) 810 return 0; 811 812 return (100 * airtime / elapsed) / USEC_PER_MSEC; 813 } 814 815 static enum iwl_mvm_traffic_load 816 iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed) 817 { 818 u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed); 819 820 if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH) 821 return IWL_MVM_TRAFFIC_HIGH; 822 if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH) 823 return IWL_MVM_TRAFFIC_MEDIUM; 824 825 return IWL_MVM_TRAFFIC_LOW; 826 } 827 828 static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif) 829 { 830 struct iwl_mvm *mvm = _data; 831 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 832 bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC; 833 834 if (mvmvif->id >= NUM_MAC_INDEX_DRIVER) 835 return; 836 837 low_latency = mvm->tcm.result.low_latency[mvmvif->id]; 838 839 if (!mvm->tcm.result.change[mvmvif->id] && 840 prev == low_latency) { 841 iwl_mvm_update_quotas(mvm, false, NULL); 842 return; 843 } 844 845 if (prev != low_latency) { 846 /* this sends traffic load and updates quota as well */ 847 iwl_mvm_update_low_latency(mvm, vif, low_latency, 848 LOW_LATENCY_TRAFFIC); 849 } else { 850 iwl_mvm_update_quotas(mvm, false, NULL); 851 } 852 } 853 854 static void iwl_mvm_tcm_results(struct iwl_mvm *mvm) 855 { 856 guard(mvm)(mvm); 857 858 ieee80211_iterate_active_interfaces( 859 mvm->hw, IEEE80211_IFACE_ITER_NORMAL, 860 iwl_mvm_tcm_iter, mvm); 861 862 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) 863 iwl_mvm_config_scan(mvm); 864 } 865 866 static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk) 867 { 868 struct iwl_mvm *mvm; 869 struct iwl_mvm_vif *mvmvif; 870 struct ieee80211_vif *vif; 871 872 mvmvif = container_of(wk, struct iwl_mvm_vif, 873 uapsd_nonagg_detected_wk.work); 874 vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv); 875 mvm = mvmvif->mvm; 876 877 if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions) 878 return; 879 880 /* remember that this AP is broken */ 881 memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr, 882 vif->bss_conf.bssid, ETH_ALEN); 883 mvm->uapsd_noagg_bssid_write_idx++; 884 if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN) 885 mvm->uapsd_noagg_bssid_write_idx = 0; 886 887 iwl_mvm_connection_loss(mvm, vif, 888 "AP isn't using AMPDU with uAPSD enabled"); 889 } 890 891 static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm, 892 struct ieee80211_vif *vif) 893 { 894 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 895 896 if (vif->type != NL80211_IFTYPE_STATION) 897 return; 898 899 if (!vif->cfg.assoc) 900 return; 901 902 if (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd && 903 !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd && 904 !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd && 905 !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd) 906 return; 907 908 if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected) 909 return; 910 911 mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true; 912 IWL_INFO(mvm, 913 "detected AP should do aggregation but isn't, likely due to U-APSD\n"); 914 schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 915 15 * HZ); 916 } 917 918 static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm, 919 unsigned int elapsed, 920 int mac) 921 { 922 u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes; 923 u64 tpt; 924 unsigned long rate; 925 struct ieee80211_vif *vif; 926 927 rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate); 928 929 if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions || 930 mvm->tcm.data[mac].uapsd_nonagg_detect.detected) 931 return; 932 933 if (iwl_mvm_has_new_rx_api(mvm)) { 934 tpt = 8 * bytes; /* kbps */ 935 do_div(tpt, elapsed); 936 rate *= 1000; /* kbps */ 937 if (tpt < 22 * rate / 100) 938 return; 939 } else { 940 /* 941 * the rate here is actually the threshold, in 100Kbps units, 942 * so do the needed conversion from bytes to 100Kbps: 943 * 100kb = bits / (100 * 1000), 944 * 100kbps = 100kb / (msecs / 1000) == 945 * (bits / (100 * 1000)) / (msecs / 1000) == 946 * bits / (100 * msecs) 947 */ 948 tpt = (8 * bytes); 949 do_div(tpt, elapsed * 100); 950 if (tpt < rate) 951 return; 952 } 953 954 rcu_read_lock(); 955 vif = rcu_dereference(mvm->vif_id_to_mac[mac]); 956 if (vif) 957 iwl_mvm_uapsd_agg_disconnect(mvm, vif); 958 rcu_read_unlock(); 959 } 960 961 static void iwl_mvm_tcm_iterator(void *_data, u8 *mac, 962 struct ieee80211_vif *vif) 963 { 964 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 965 u32 *band = _data; 966 967 if (!mvmvif->deflink.phy_ctxt) 968 return; 969 970 band[mvmvif->id] = mvmvif->deflink.phy_ctxt->channel->band; 971 } 972 973 static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm, 974 unsigned long ts, 975 bool handle_uapsd) 976 { 977 unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts); 978 unsigned int uapsd_elapsed = 979 jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts); 980 u32 total_airtime = 0; 981 u32 band_airtime[NUM_NL80211_BANDS] = {0}; 982 u32 band[NUM_MAC_INDEX_DRIVER] = {0}; 983 int ac, mac, i; 984 bool low_latency = false; 985 enum iwl_mvm_traffic_load load, band_load; 986 bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD); 987 988 if (handle_ll) 989 mvm->tcm.ll_ts = ts; 990 if (handle_uapsd) 991 mvm->tcm.uapsd_nonagg_ts = ts; 992 993 mvm->tcm.result.elapsed = elapsed; 994 995 ieee80211_iterate_active_interfaces_atomic(mvm->hw, 996 IEEE80211_IFACE_ITER_NORMAL, 997 iwl_mvm_tcm_iterator, 998 &band); 999 1000 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) { 1001 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac]; 1002 u32 vo_vi_pkts = 0; 1003 u32 airtime = mdata->rx.airtime + mdata->tx.airtime; 1004 1005 total_airtime += airtime; 1006 band_airtime[band[mac]] += airtime; 1007 1008 load = iwl_mvm_tcm_load(mvm, airtime, elapsed); 1009 mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac]; 1010 mvm->tcm.result.load[mac] = load; 1011 mvm->tcm.result.airtime[mac] = airtime; 1012 1013 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++) 1014 vo_vi_pkts += mdata->rx.pkts[ac] + 1015 mdata->tx.pkts[ac]; 1016 1017 /* enable immediately with enough packets but defer disabling */ 1018 if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH) 1019 mvm->tcm.result.low_latency[mac] = true; 1020 else if (handle_ll) 1021 mvm->tcm.result.low_latency[mac] = false; 1022 1023 if (handle_ll) { 1024 /* clear old data */ 1025 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts)); 1026 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts)); 1027 } 1028 low_latency |= mvm->tcm.result.low_latency[mac]; 1029 1030 if (!mvm->tcm.result.low_latency[mac] && handle_uapsd) 1031 iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed, 1032 mac); 1033 /* clear old data */ 1034 if (handle_uapsd) 1035 mdata->uapsd_nonagg_detect.rx_bytes = 0; 1036 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime)); 1037 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime)); 1038 } 1039 1040 load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed); 1041 mvm->tcm.result.global_load = load; 1042 1043 for (i = 0; i < NUM_NL80211_BANDS; i++) { 1044 band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed); 1045 mvm->tcm.result.band_load[i] = band_load; 1046 } 1047 1048 /* 1049 * If the current load isn't low we need to force re-evaluation 1050 * in the TCM period, so that we can return to low load if there 1051 * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get 1052 * triggered by traffic). 1053 */ 1054 if (load != IWL_MVM_TRAFFIC_LOW) 1055 return MVM_TCM_PERIOD; 1056 /* 1057 * If low-latency is active we need to force re-evaluation after 1058 * (the longer) MVM_LL_PERIOD, so that we can disable low-latency 1059 * when there's no traffic at all. 1060 */ 1061 if (low_latency) 1062 return MVM_LL_PERIOD; 1063 /* 1064 * Otherwise, we don't need to run the work struct because we're 1065 * in the default "idle" state - traffic indication is low (which 1066 * also covers the "no traffic" case) and low-latency is disabled 1067 * so there's no state that may need to be disabled when there's 1068 * no traffic at all. 1069 * 1070 * Note that this has no impact on the regular scheduling of the 1071 * updates triggered by traffic - those happen whenever one of the 1072 * two timeouts expire (if there's traffic at all.) 1073 */ 1074 return 0; 1075 } 1076 1077 void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm) 1078 { 1079 unsigned long ts = jiffies; 1080 bool handle_uapsd = 1081 time_after(ts, mvm->tcm.uapsd_nonagg_ts + 1082 msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD)); 1083 1084 spin_lock(&mvm->tcm.lock); 1085 if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) { 1086 spin_unlock(&mvm->tcm.lock); 1087 return; 1088 } 1089 spin_unlock(&mvm->tcm.lock); 1090 1091 if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) { 1092 guard(mvm)(mvm); 1093 if (iwl_mvm_request_statistics(mvm, true)) 1094 handle_uapsd = false; 1095 } 1096 1097 spin_lock(&mvm->tcm.lock); 1098 /* re-check if somebody else won the recheck race */ 1099 if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) { 1100 /* calculate statistics */ 1101 unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts, 1102 handle_uapsd); 1103 1104 /* the memset needs to be visible before the timestamp */ 1105 smp_mb(); 1106 mvm->tcm.ts = ts; 1107 if (work_delay) 1108 schedule_delayed_work(&mvm->tcm.work, work_delay); 1109 } 1110 spin_unlock(&mvm->tcm.lock); 1111 1112 iwl_mvm_tcm_results(mvm); 1113 } 1114 1115 void iwl_mvm_tcm_work(struct work_struct *work) 1116 { 1117 struct delayed_work *delayed_work = to_delayed_work(work); 1118 struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm, 1119 tcm.work); 1120 1121 iwl_mvm_recalc_tcm(mvm); 1122 } 1123 1124 void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel) 1125 { 1126 spin_lock_bh(&mvm->tcm.lock); 1127 mvm->tcm.paused = true; 1128 spin_unlock_bh(&mvm->tcm.lock); 1129 if (with_cancel) 1130 cancel_delayed_work_sync(&mvm->tcm.work); 1131 } 1132 1133 void iwl_mvm_resume_tcm(struct iwl_mvm *mvm) 1134 { 1135 int mac; 1136 bool low_latency = false; 1137 1138 spin_lock_bh(&mvm->tcm.lock); 1139 mvm->tcm.ts = jiffies; 1140 mvm->tcm.ll_ts = jiffies; 1141 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) { 1142 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac]; 1143 1144 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts)); 1145 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts)); 1146 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime)); 1147 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime)); 1148 1149 if (mvm->tcm.result.low_latency[mac]) 1150 low_latency = true; 1151 } 1152 /* The TCM data needs to be reset before "paused" flag changes */ 1153 smp_mb(); 1154 mvm->tcm.paused = false; 1155 1156 /* 1157 * if the current load is not low or low latency is active, force 1158 * re-evaluation to cover the case of no traffic. 1159 */ 1160 if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW) 1161 schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD); 1162 else if (low_latency) 1163 schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD); 1164 1165 spin_unlock_bh(&mvm->tcm.lock); 1166 } 1167 1168 void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 1169 { 1170 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 1171 1172 INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk, 1173 iwl_mvm_tcm_uapsd_nonagg_detected_wk); 1174 } 1175 1176 void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 1177 { 1178 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 1179 1180 cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk); 1181 } 1182 1183 u32 iwl_mvm_get_systime(struct iwl_mvm *mvm) 1184 { 1185 u32 reg_addr = DEVICE_SYSTEM_TIME_REG; 1186 1187 if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_22000 && 1188 mvm->trans->mac_cfg->base->gp2_reg_addr) 1189 reg_addr = mvm->trans->mac_cfg->base->gp2_reg_addr; 1190 1191 return iwl_read_prph(mvm->trans, reg_addr); 1192 } 1193 1194 void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, int clock_type, 1195 u32 *gp2, u64 *boottime, ktime_t *realtime) 1196 { 1197 bool ps_disabled; 1198 1199 lockdep_assert_held(&mvm->mutex); 1200 1201 /* Disable power save when reading GP2 */ 1202 ps_disabled = mvm->ps_disabled; 1203 if (!ps_disabled) { 1204 mvm->ps_disabled = true; 1205 iwl_mvm_power_update_device(mvm); 1206 } 1207 1208 *gp2 = iwl_mvm_get_systime(mvm); 1209 1210 if (clock_type == CLOCK_BOOTTIME && boottime) 1211 *boottime = ktime_get_boottime_ns(); 1212 else if (clock_type == CLOCK_REALTIME && realtime) 1213 *realtime = ktime_get_real(); 1214 1215 if (!ps_disabled) { 1216 mvm->ps_disabled = ps_disabled; 1217 iwl_mvm_power_update_device(mvm); 1218 } 1219 } 1220 1221 /* Find if at least two links from different vifs use same channel 1222 * FIXME: consider having a refcount array in struct iwl_mvm_vif for 1223 * used phy_ctxt ids. 1224 */ 1225 bool iwl_mvm_have_links_same_channel(struct iwl_mvm_vif *vif1, 1226 struct iwl_mvm_vif *vif2) 1227 { 1228 unsigned int i, j; 1229 1230 for_each_mvm_vif_valid_link(vif1, i) { 1231 for_each_mvm_vif_valid_link(vif2, j) { 1232 if (vif1->link[i]->phy_ctxt == vif2->link[j]->phy_ctxt) 1233 return true; 1234 } 1235 } 1236 1237 return false; 1238 } 1239 1240 static u32 iwl_legacy_rate_to_fw_idx(u32 rate_n_flags) 1241 { 1242 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK_V1; 1243 int idx; 1244 bool ofdm = !(rate_n_flags & RATE_MCS_CCK_MSK_V1); 1245 int offset = ofdm ? IWL_FIRST_OFDM_RATE : 0; 1246 int last = ofdm ? IWL_RATE_COUNT_LEGACY : IWL_FIRST_OFDM_RATE; 1247 1248 for (idx = offset; idx < last; idx++) 1249 if (iwl_fw_rate_idx_to_plcp(idx) == rate) 1250 return idx - offset; 1251 return IWL_RATE_INVALID; 1252 } 1253 1254 u32 iwl_mvm_v3_rate_from_fw(__le32 rate, u8 rate_ver) 1255 { 1256 u32 rate_v3 = 0, rate_v1; 1257 u32 dup = 0; 1258 1259 if (rate_ver > 1) 1260 return iwl_v3_rate_from_v2_v3(rate, rate_ver >= 3); 1261 1262 rate_v1 = le32_to_cpu(rate); 1263 if (rate_v1 == 0) 1264 return rate_v1; 1265 /* convert rate */ 1266 if (rate_v1 & RATE_MCS_HT_MSK_V1) { 1267 u32 nss; 1268 1269 rate_v3 |= RATE_MCS_MOD_TYPE_HT; 1270 rate_v3 |= 1271 rate_v1 & RATE_HT_MCS_RATE_CODE_MSK_V1; 1272 nss = u32_get_bits(rate_v1, RATE_HT_MCS_MIMO2_MSK); 1273 rate_v3 |= u32_encode_bits(nss, RATE_MCS_NSS_MSK); 1274 } else if (rate_v1 & RATE_MCS_VHT_MSK_V1 || 1275 rate_v1 & RATE_MCS_HE_MSK_V1) { 1276 u32 nss = u32_get_bits(rate_v1, RATE_VHT_MCS_NSS_MSK); 1277 1278 rate_v3 |= rate_v1 & RATE_VHT_MCS_RATE_CODE_MSK; 1279 1280 rate_v3 |= u32_encode_bits(nss, RATE_MCS_NSS_MSK); 1281 1282 if (rate_v1 & RATE_MCS_HE_MSK_V1) { 1283 u32 he_type_bits = rate_v1 & RATE_MCS_HE_TYPE_MSK_V1; 1284 u32 he_type = he_type_bits >> RATE_MCS_HE_TYPE_POS_V1; 1285 u32 he_106t = (rate_v1 & RATE_MCS_HE_106T_MSK_V1) >> 1286 RATE_MCS_HE_106T_POS_V1; 1287 u32 he_gi_ltf = (rate_v1 & RATE_MCS_HE_GI_LTF_MSK_V1) >> 1288 RATE_MCS_HE_GI_LTF_POS; 1289 1290 if ((he_type_bits == RATE_MCS_HE_TYPE_SU || 1291 he_type_bits == RATE_MCS_HE_TYPE_EXT_SU) && 1292 he_gi_ltf == RATE_MCS_HE_SU_4_LTF) 1293 /* the new rate have an additional bit to 1294 * represent the value 4 rather then using SGI 1295 * bit for this purpose - as it was done in the 1296 * old rate 1297 */ 1298 he_gi_ltf += (rate_v1 & RATE_MCS_SGI_MSK_V1) >> 1299 RATE_MCS_SGI_POS_V1; 1300 1301 rate_v3 |= he_gi_ltf << RATE_MCS_HE_GI_LTF_POS; 1302 rate_v3 |= he_type << RATE_MCS_HE_TYPE_POS; 1303 rate_v3 |= he_106t << RATE_MCS_HE_106T_POS; 1304 rate_v3 |= rate_v1 & RATE_HE_DUAL_CARRIER_MODE_MSK; 1305 rate_v3 |= RATE_MCS_MOD_TYPE_HE; 1306 } else { 1307 rate_v3 |= RATE_MCS_MOD_TYPE_VHT; 1308 } 1309 /* if legacy format */ 1310 } else { 1311 u32 legacy_rate = iwl_legacy_rate_to_fw_idx(rate_v1); 1312 1313 if (WARN_ON_ONCE(legacy_rate == IWL_RATE_INVALID)) 1314 legacy_rate = (rate_v1 & RATE_MCS_CCK_MSK_V1) ? 1315 IWL_FIRST_CCK_RATE : IWL_FIRST_OFDM_RATE; 1316 1317 rate_v3 |= legacy_rate; 1318 if (!(rate_v1 & RATE_MCS_CCK_MSK_V1)) 1319 rate_v3 |= RATE_MCS_MOD_TYPE_LEGACY_OFDM; 1320 } 1321 1322 /* convert flags */ 1323 if (rate_v1 & RATE_MCS_LDPC_MSK_V1) 1324 rate_v3 |= RATE_MCS_LDPC_MSK; 1325 rate_v3 |= (rate_v1 & RATE_MCS_CHAN_WIDTH_MSK_V1) | 1326 (rate_v1 & RATE_MCS_ANT_AB_MSK) | 1327 (rate_v1 & RATE_MCS_STBC_MSK) | 1328 (rate_v1 & RATE_MCS_BF_MSK); 1329 1330 dup = (rate_v1 & RATE_MCS_DUP_MSK_V1) >> RATE_MCS_DUP_POS_V1; 1331 if (dup) { 1332 rate_v3 |= RATE_MCS_DUP_MSK; 1333 rate_v3 |= dup << RATE_MCS_CHAN_WIDTH_POS; 1334 } 1335 1336 if ((!(rate_v1 & RATE_MCS_HE_MSK_V1)) && 1337 (rate_v1 & RATE_MCS_SGI_MSK_V1)) 1338 rate_v3 |= RATE_MCS_SGI_MSK; 1339 1340 return rate_v3; 1341 } 1342 1343 __le32 iwl_mvm_v3_rate_to_fw(u32 rate, u8 rate_ver) 1344 { 1345 u32 result = 0; 1346 int rate_idx; 1347 1348 if (rate_ver > 1) 1349 return iwl_v3_rate_to_v2_v3(rate, rate_ver > 2); 1350 1351 switch (rate & RATE_MCS_MOD_TYPE_MSK) { 1352 case RATE_MCS_MOD_TYPE_CCK: 1353 result = RATE_MCS_CCK_MSK_V1; 1354 fallthrough; 1355 case RATE_MCS_MOD_TYPE_LEGACY_OFDM: 1356 rate_idx = u32_get_bits(rate, RATE_LEGACY_RATE_MSK); 1357 if (!(result & RATE_MCS_CCK_MSK_V1)) 1358 rate_idx += IWL_FIRST_OFDM_RATE; 1359 result |= u32_encode_bits(iwl_fw_rate_idx_to_plcp(rate_idx), 1360 RATE_LEGACY_RATE_MSK_V1); 1361 break; 1362 case RATE_MCS_MOD_TYPE_HT: 1363 result = RATE_MCS_HT_MSK_V1; 1364 result |= u32_encode_bits(u32_get_bits(rate, 1365 RATE_HT_MCS_CODE_MSK), 1366 RATE_HT_MCS_RATE_CODE_MSK_V1); 1367 result |= u32_encode_bits(u32_get_bits(rate, 1368 RATE_MCS_NSS_MSK), 1369 RATE_HT_MCS_MIMO2_MSK); 1370 break; 1371 case RATE_MCS_MOD_TYPE_VHT: 1372 result = RATE_MCS_VHT_MSK_V1; 1373 result |= u32_encode_bits(u32_get_bits(rate, 1374 RATE_VHT_MCS_NSS_MSK), 1375 RATE_MCS_CODE_MSK); 1376 result |= u32_encode_bits(u32_get_bits(rate, RATE_MCS_NSS_MSK), 1377 RATE_VHT_MCS_NSS_MSK); 1378 break; 1379 case RATE_MCS_MOD_TYPE_HE: /* not generated */ 1380 default: 1381 WARN_ONCE(1, "bad modulation type %d\n", 1382 u32_get_bits(rate, RATE_MCS_MOD_TYPE_MSK)); 1383 return 0; 1384 } 1385 1386 if (rate & RATE_MCS_LDPC_MSK) 1387 result |= RATE_MCS_LDPC_MSK_V1; 1388 WARN_ON_ONCE(u32_get_bits(rate, RATE_MCS_CHAN_WIDTH_MSK) > 1389 RATE_MCS_CHAN_WIDTH_160_VAL); 1390 result |= (rate & RATE_MCS_CHAN_WIDTH_MSK_V1) | 1391 (rate & RATE_MCS_ANT_AB_MSK) | 1392 (rate & RATE_MCS_STBC_MSK) | 1393 (rate & RATE_MCS_BF_MSK); 1394 1395 /* not handling DUP since we don't use it */ 1396 WARN_ON_ONCE(rate & RATE_MCS_DUP_MSK); 1397 1398 if (rate & RATE_MCS_SGI_MSK) 1399 result |= RATE_MCS_SGI_MSK_V1; 1400 1401 return cpu_to_le32(result); 1402 } 1403 1404 bool iwl_mvm_vif_is_active(struct iwl_mvm_vif *mvmvif) 1405 { 1406 unsigned int i; 1407 1408 /* FIXME: can it fail when phy_ctxt is assigned? */ 1409 for_each_mvm_vif_valid_link(mvmvif, i) { 1410 if (mvmvif->link[i]->phy_ctxt && 1411 mvmvif->link[i]->phy_ctxt->id < NUM_PHY_CTX) 1412 return true; 1413 } 1414 1415 return false; 1416 } 1417