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