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