1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2012-2015, 2018-2024 Intel Corporation 4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH 5 * Copyright (C) 2016-2017 Intel Deutschland GmbH 6 */ 7 #include <net/mac80211.h> 8 9 #include "mvm.h" 10 #include "sta.h" 11 #include "rs.h" 12 13 /* 14 * New version of ADD_STA_sta command added new fields at the end of the 15 * structure, so sending the size of the relevant API's structure is enough to 16 * support both API versions. 17 */ 18 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm) 19 { 20 if (iwl_mvm_has_new_rx_api(mvm) || 21 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) 22 return sizeof(struct iwl_mvm_add_sta_cmd); 23 else 24 return sizeof(struct iwl_mvm_add_sta_cmd_v7); 25 } 26 27 int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm, enum nl80211_iftype iftype) 28 { 29 int sta_id; 30 u32 reserved_ids = 0; 31 32 BUILD_BUG_ON(IWL_MVM_STATION_COUNT_MAX > 32); 33 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)); 34 35 lockdep_assert_held(&mvm->mutex); 36 37 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */ 38 if (iftype != NL80211_IFTYPE_STATION) 39 reserved_ids = BIT(0); 40 41 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */ 42 for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) { 43 if (BIT(sta_id) & reserved_ids) 44 continue; 45 46 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], 47 lockdep_is_held(&mvm->mutex))) 48 return sta_id; 49 } 50 return IWL_MVM_INVALID_STA; 51 } 52 53 /* Calculate the ampdu density and max size */ 54 u32 iwl_mvm_get_sta_ampdu_dens(struct ieee80211_link_sta *link_sta, 55 struct ieee80211_bss_conf *link_conf, 56 u32 *_agg_size) 57 { 58 u32 agg_size = 0, mpdu_dens = 0; 59 60 if (WARN_ON(!link_sta)) 61 return 0; 62 63 /* Note that we always use only legacy & highest supported PPDUs, so 64 * of Draft P802.11be D.30 Table 10-12a--Fields used for calculating 65 * the maximum A-MPDU size of various PPDU types in different bands, 66 * we only need to worry about the highest supported PPDU type here. 67 */ 68 69 if (link_sta->ht_cap.ht_supported) { 70 agg_size = link_sta->ht_cap.ampdu_factor; 71 mpdu_dens = link_sta->ht_cap.ampdu_density; 72 } 73 74 if (link_conf->chanreq.oper.chan->band == NL80211_BAND_6GHZ) { 75 /* overwrite HT values on 6 GHz */ 76 mpdu_dens = le16_get_bits(link_sta->he_6ghz_capa.capa, 77 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START); 78 agg_size = le16_get_bits(link_sta->he_6ghz_capa.capa, 79 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP); 80 } else if (link_sta->vht_cap.vht_supported) { 81 /* if VHT supported overwrite HT value */ 82 agg_size = u32_get_bits(link_sta->vht_cap.cap, 83 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK); 84 } 85 86 /* D6.0 10.12.2 A-MPDU length limit rules 87 * A STA indicates the maximum length of the A-MPDU preEOF padding 88 * that it can receive in an HE PPDU in the Maximum A-MPDU Length 89 * Exponent field in its HT Capabilities, VHT Capabilities, 90 * and HE 6 GHz Band Capabilities elements (if present) and the 91 * Maximum AMPDU Length Exponent Extension field in its HE 92 * Capabilities element 93 */ 94 if (link_sta->he_cap.has_he) 95 agg_size += 96 u8_get_bits(link_sta->he_cap.he_cap_elem.mac_cap_info[3], 97 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK); 98 99 if (link_sta->eht_cap.has_eht) 100 agg_size += u8_get_bits(link_sta->eht_cap.eht_cap_elem.mac_cap_info[1], 101 IEEE80211_EHT_MAC_CAP1_MAX_AMPDU_LEN_MASK); 102 103 /* Limit to max A-MPDU supported by FW */ 104 agg_size = min_t(u32, agg_size, 105 STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT); 106 107 *_agg_size = agg_size; 108 return mpdu_dens; 109 } 110 111 u8 iwl_mvm_get_sta_uapsd_acs(struct ieee80211_sta *sta) 112 { 113 u8 uapsd_acs = 0; 114 115 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) 116 uapsd_acs |= BIT(AC_BK); 117 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) 118 uapsd_acs |= BIT(AC_BE); 119 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) 120 uapsd_acs |= BIT(AC_VI); 121 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) 122 uapsd_acs |= BIT(AC_VO); 123 124 return uapsd_acs | uapsd_acs << 4; 125 } 126 127 /* send station add/update command to firmware */ 128 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 129 bool update, unsigned int flags) 130 { 131 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 132 struct iwl_mvm_add_sta_cmd add_sta_cmd = { 133 .sta_id = mvm_sta->deflink.sta_id, 134 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color), 135 .add_modify = update ? 1 : 0, 136 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK | 137 STA_FLG_MIMO_EN_MSK | 138 STA_FLG_RTS_MIMO_PROT), 139 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg), 140 }; 141 int ret; 142 u32 status; 143 u32 agg_size = 0, mpdu_dens = 0; 144 145 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) 146 add_sta_cmd.station_type = mvm_sta->sta_type; 147 148 if (!update || (flags & STA_MODIFY_QUEUES)) { 149 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN); 150 151 if (!iwl_mvm_has_new_tx_api(mvm)) { 152 add_sta_cmd.tfd_queue_msk = 153 cpu_to_le32(mvm_sta->tfd_queue_msk); 154 155 if (flags & STA_MODIFY_QUEUES) 156 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES; 157 } else { 158 WARN_ON(flags & STA_MODIFY_QUEUES); 159 } 160 } 161 162 switch (sta->deflink.bandwidth) { 163 case IEEE80211_STA_RX_BW_320: 164 case IEEE80211_STA_RX_BW_160: 165 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ); 166 fallthrough; 167 case IEEE80211_STA_RX_BW_80: 168 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ); 169 fallthrough; 170 case IEEE80211_STA_RX_BW_40: 171 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ); 172 fallthrough; 173 case IEEE80211_STA_RX_BW_20: 174 if (sta->deflink.ht_cap.ht_supported) 175 add_sta_cmd.station_flags |= 176 cpu_to_le32(STA_FLG_FAT_EN_20MHZ); 177 break; 178 } 179 180 switch (sta->deflink.rx_nss) { 181 case 1: 182 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO); 183 break; 184 case 2: 185 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2); 186 break; 187 case 3 ... 8: 188 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3); 189 break; 190 } 191 192 switch (sta->deflink.smps_mode) { 193 case IEEE80211_SMPS_AUTOMATIC: 194 case IEEE80211_SMPS_NUM_MODES: 195 WARN_ON(1); 196 break; 197 case IEEE80211_SMPS_STATIC: 198 /* override NSS */ 199 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK); 200 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO); 201 break; 202 case IEEE80211_SMPS_DYNAMIC: 203 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT); 204 break; 205 case IEEE80211_SMPS_OFF: 206 /* nothing */ 207 break; 208 } 209 210 if (sta->deflink.ht_cap.ht_supported || 211 mvm_sta->vif->bss_conf.chanreq.oper.chan->band == NL80211_BAND_6GHZ) 212 add_sta_cmd.station_flags_msk |= 213 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK | 214 STA_FLG_AGG_MPDU_DENS_MSK); 215 216 mpdu_dens = iwl_mvm_get_sta_ampdu_dens(&sta->deflink, 217 &mvm_sta->vif->bss_conf, 218 &agg_size); 219 add_sta_cmd.station_flags |= 220 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT); 221 add_sta_cmd.station_flags |= 222 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT); 223 224 if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC) 225 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid); 226 227 if (sta->wme) { 228 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS; 229 add_sta_cmd.uapsd_acs = iwl_mvm_get_sta_uapsd_acs(sta); 230 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128; 231 } 232 233 status = ADD_STA_SUCCESS; 234 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, 235 iwl_mvm_add_sta_cmd_size(mvm), 236 &add_sta_cmd, &status); 237 if (ret) 238 return ret; 239 240 switch (status & IWL_ADD_STA_STATUS_MASK) { 241 case ADD_STA_SUCCESS: 242 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n"); 243 break; 244 default: 245 ret = -EIO; 246 IWL_ERR(mvm, "ADD_STA failed\n"); 247 break; 248 } 249 250 return ret; 251 } 252 253 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t) 254 { 255 struct iwl_mvm_baid_data *data = 256 from_timer(data, t, session_timer); 257 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr; 258 struct iwl_mvm_baid_data *ba_data; 259 struct ieee80211_sta *sta; 260 struct iwl_mvm_sta *mvm_sta; 261 unsigned long timeout; 262 unsigned int sta_id; 263 264 rcu_read_lock(); 265 266 ba_data = rcu_dereference(*rcu_ptr); 267 268 if (WARN_ON(!ba_data)) 269 goto unlock; 270 271 if (!ba_data->timeout) 272 goto unlock; 273 274 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2); 275 if (time_is_after_jiffies(timeout)) { 276 mod_timer(&ba_data->session_timer, timeout); 277 goto unlock; 278 } 279 280 /* Timer expired */ 281 sta_id = ffs(ba_data->sta_mask) - 1; /* don't care which one */ 282 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[sta_id]); 283 284 /* 285 * sta should be valid unless the following happens: 286 * The firmware asserts which triggers a reconfig flow, but 287 * the reconfig fails before we set the pointer to sta into 288 * the fw_id_to_mac_id pointer table. Mac80211 can't stop 289 * A-MDPU and hence the timer continues to run. Then, the 290 * timer expires and sta is NULL. 291 */ 292 if (IS_ERR_OR_NULL(sta)) 293 goto unlock; 294 295 mvm_sta = iwl_mvm_sta_from_mac80211(sta); 296 ieee80211_rx_ba_timer_expired(mvm_sta->vif, 297 sta->addr, ba_data->tid); 298 unlock: 299 rcu_read_unlock(); 300 } 301 302 /* Disable aggregations for a bitmap of TIDs for a given station */ 303 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue, 304 unsigned long disable_agg_tids, 305 bool remove_queue) 306 { 307 struct iwl_mvm_add_sta_cmd cmd = {}; 308 struct ieee80211_sta *sta; 309 struct iwl_mvm_sta *mvmsta; 310 u32 status; 311 u8 sta_id; 312 313 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 314 return -EINVAL; 315 316 sta_id = mvm->queue_info[queue].ra_sta_id; 317 318 rcu_read_lock(); 319 320 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]); 321 322 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) { 323 rcu_read_unlock(); 324 return -EINVAL; 325 } 326 327 mvmsta = iwl_mvm_sta_from_mac80211(sta); 328 329 mvmsta->tid_disable_agg |= disable_agg_tids; 330 331 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color); 332 cmd.sta_id = mvmsta->deflink.sta_id; 333 cmd.add_modify = STA_MODE_MODIFY; 334 cmd.modify_mask = STA_MODIFY_QUEUES; 335 if (disable_agg_tids) 336 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX; 337 if (remove_queue) 338 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL; 339 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk); 340 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg); 341 342 rcu_read_unlock(); 343 344 /* Notify FW of queue removal from the STA queues */ 345 status = ADD_STA_SUCCESS; 346 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, 347 iwl_mvm_add_sta_cmd_size(mvm), 348 &cmd, &status); 349 } 350 351 static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 352 int sta_id, u16 *queueptr, u8 tid) 353 { 354 int queue = *queueptr; 355 struct iwl_scd_txq_cfg_cmd cmd = { 356 .scd_queue = queue, 357 .action = SCD_CFG_DISABLE_QUEUE, 358 }; 359 int ret; 360 361 lockdep_assert_held(&mvm->mutex); 362 363 if (iwl_mvm_has_new_tx_api(mvm)) { 364 if (mvm->sta_remove_requires_queue_remove) { 365 u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, 366 SCD_QUEUE_CONFIG_CMD); 367 struct iwl_scd_queue_cfg_cmd remove_cmd = { 368 .operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE), 369 .u.remove.sta_mask = cpu_to_le32(BIT(sta_id)), 370 }; 371 372 if (tid == IWL_MAX_TID_COUNT) 373 tid = IWL_MGMT_TID; 374 375 remove_cmd.u.remove.tid = cpu_to_le32(tid); 376 377 ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, 378 sizeof(remove_cmd), 379 &remove_cmd); 380 } else { 381 ret = 0; 382 } 383 384 iwl_trans_txq_free(mvm->trans, queue); 385 *queueptr = IWL_MVM_INVALID_QUEUE; 386 387 return ret; 388 } 389 390 if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0)) 391 return 0; 392 393 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid); 394 395 cmd.action = mvm->queue_info[queue].tid_bitmap ? 396 SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE; 397 if (cmd.action == SCD_CFG_DISABLE_QUEUE) 398 mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE; 399 400 IWL_DEBUG_TX_QUEUES(mvm, 401 "Disabling TXQ #%d tids=0x%x\n", 402 queue, 403 mvm->queue_info[queue].tid_bitmap); 404 405 /* If the queue is still enabled - nothing left to do in this func */ 406 if (cmd.action == SCD_CFG_ENABLE_QUEUE) 407 return 0; 408 409 cmd.sta_id = mvm->queue_info[queue].ra_sta_id; 410 cmd.tid = mvm->queue_info[queue].txq_tid; 411 412 /* Make sure queue info is correct even though we overwrite it */ 413 WARN(mvm->queue_info[queue].tid_bitmap, 414 "TXQ #%d info out-of-sync - tids=0x%x\n", 415 queue, mvm->queue_info[queue].tid_bitmap); 416 417 /* If we are here - the queue is freed and we can zero out these vals */ 418 mvm->queue_info[queue].tid_bitmap = 0; 419 420 if (sta) { 421 struct iwl_mvm_txq *mvmtxq = 422 iwl_mvm_txq_from_tid(sta, tid); 423 424 spin_lock_bh(&mvm->add_stream_lock); 425 list_del_init(&mvmtxq->list); 426 clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state); 427 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE; 428 spin_unlock_bh(&mvm->add_stream_lock); 429 } 430 431 /* Regardless if this is a reserved TXQ for a STA - mark it as false */ 432 mvm->queue_info[queue].reserved = false; 433 434 iwl_trans_txq_disable(mvm->trans, queue, false); 435 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, 436 sizeof(struct iwl_scd_txq_cfg_cmd), &cmd); 437 438 if (ret) 439 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n", 440 queue, ret); 441 return ret; 442 } 443 444 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue) 445 { 446 struct ieee80211_sta *sta; 447 struct iwl_mvm_sta *mvmsta; 448 unsigned long tid_bitmap; 449 unsigned long agg_tids = 0; 450 u8 sta_id; 451 int tid; 452 453 lockdep_assert_held(&mvm->mutex); 454 455 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 456 return -EINVAL; 457 458 sta_id = mvm->queue_info[queue].ra_sta_id; 459 tid_bitmap = mvm->queue_info[queue].tid_bitmap; 460 461 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], 462 lockdep_is_held(&mvm->mutex)); 463 464 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) 465 return -EINVAL; 466 467 mvmsta = iwl_mvm_sta_from_mac80211(sta); 468 469 spin_lock_bh(&mvmsta->lock); 470 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) { 471 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) 472 agg_tids |= BIT(tid); 473 } 474 spin_unlock_bh(&mvmsta->lock); 475 476 return agg_tids; 477 } 478 479 /* 480 * Remove a queue from a station's resources. 481 * Note that this only marks as free. It DOESN'T delete a BA agreement, and 482 * doesn't disable the queue 483 */ 484 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue) 485 { 486 struct ieee80211_sta *sta; 487 struct iwl_mvm_sta *mvmsta; 488 unsigned long tid_bitmap; 489 unsigned long disable_agg_tids = 0; 490 u8 sta_id; 491 int tid; 492 493 lockdep_assert_held(&mvm->mutex); 494 495 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 496 return -EINVAL; 497 498 sta_id = mvm->queue_info[queue].ra_sta_id; 499 tid_bitmap = mvm->queue_info[queue].tid_bitmap; 500 501 rcu_read_lock(); 502 503 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]); 504 505 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) { 506 rcu_read_unlock(); 507 return 0; 508 } 509 510 mvmsta = iwl_mvm_sta_from_mac80211(sta); 511 512 spin_lock_bh(&mvmsta->lock); 513 /* Unmap MAC queues and TIDs from this queue */ 514 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) { 515 struct iwl_mvm_txq *mvmtxq = 516 iwl_mvm_txq_from_tid(sta, tid); 517 518 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) 519 disable_agg_tids |= BIT(tid); 520 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE; 521 522 spin_lock_bh(&mvm->add_stream_lock); 523 list_del_init(&mvmtxq->list); 524 clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state); 525 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE; 526 spin_unlock_bh(&mvm->add_stream_lock); 527 } 528 529 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */ 530 spin_unlock_bh(&mvmsta->lock); 531 532 rcu_read_unlock(); 533 534 /* 535 * The TX path may have been using this TXQ_ID from the tid_data, 536 * so make sure it's no longer running so that we can safely reuse 537 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE 538 * above, but nothing guarantees we've stopped using them. Thus, 539 * without this, we could get to iwl_mvm_disable_txq() and remove 540 * the queue while still sending frames to it. 541 */ 542 synchronize_net(); 543 544 return disable_agg_tids; 545 } 546 547 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue, 548 struct ieee80211_sta *old_sta, 549 u8 new_sta_id) 550 { 551 struct iwl_mvm_sta *mvmsta; 552 u8 sta_id, tid; 553 unsigned long disable_agg_tids = 0; 554 bool same_sta; 555 u16 queue_tmp = queue; 556 int ret; 557 558 lockdep_assert_held(&mvm->mutex); 559 560 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 561 return -EINVAL; 562 563 sta_id = mvm->queue_info[queue].ra_sta_id; 564 tid = mvm->queue_info[queue].txq_tid; 565 566 same_sta = sta_id == new_sta_id; 567 568 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id); 569 if (WARN_ON(!mvmsta)) 570 return -EINVAL; 571 572 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue); 573 /* Disable the queue */ 574 if (disable_agg_tids) 575 iwl_mvm_invalidate_sta_queue(mvm, queue, 576 disable_agg_tids, false); 577 578 ret = iwl_mvm_disable_txq(mvm, old_sta, sta_id, &queue_tmp, tid); 579 if (ret) { 580 IWL_ERR(mvm, 581 "Failed to free inactive queue %d (ret=%d)\n", 582 queue, ret); 583 584 return ret; 585 } 586 587 /* If TXQ is allocated to another STA, update removal in FW */ 588 if (!same_sta) 589 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true); 590 591 return 0; 592 } 593 594 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm, 595 unsigned long tfd_queue_mask, u8 ac) 596 { 597 int queue = 0; 598 u8 ac_to_queue[IEEE80211_NUM_ACS]; 599 int i; 600 601 /* 602 * This protects us against grabbing a queue that's being reconfigured 603 * by the inactivity checker. 604 */ 605 lockdep_assert_held(&mvm->mutex); 606 607 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 608 return -EINVAL; 609 610 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue)); 611 612 /* See what ACs the existing queues for this STA have */ 613 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) { 614 /* Only DATA queues can be shared */ 615 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE && 616 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE) 617 continue; 618 619 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i; 620 } 621 622 /* 623 * The queue to share is chosen only from DATA queues as follows (in 624 * descending priority): 625 * 1. An AC_BE queue 626 * 2. Same AC queue 627 * 3. Highest AC queue that is lower than new AC 628 * 4. Any existing AC (there always is at least 1 DATA queue) 629 */ 630 631 /* Priority 1: An AC_BE queue */ 632 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE) 633 queue = ac_to_queue[IEEE80211_AC_BE]; 634 /* Priority 2: Same AC queue */ 635 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE) 636 queue = ac_to_queue[ac]; 637 /* Priority 3a: If new AC is VO and VI exists - use VI */ 638 else if (ac == IEEE80211_AC_VO && 639 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE) 640 queue = ac_to_queue[IEEE80211_AC_VI]; 641 /* Priority 3b: No BE so only AC less than the new one is BK */ 642 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE) 643 queue = ac_to_queue[IEEE80211_AC_BK]; 644 /* Priority 4a: No BE nor BK - use VI if exists */ 645 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE) 646 queue = ac_to_queue[IEEE80211_AC_VI]; 647 /* Priority 4b: No BE, BK nor VI - use VO if exists */ 648 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE) 649 queue = ac_to_queue[IEEE80211_AC_VO]; 650 651 /* Make sure queue found (or not) is legal */ 652 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) && 653 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) && 654 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) { 655 IWL_ERR(mvm, "No DATA queues available to share\n"); 656 return -ENOSPC; 657 } 658 659 return queue; 660 } 661 662 /* Re-configure the SCD for a queue that has already been configured */ 663 static int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, 664 int sta_id, int tid, int frame_limit, u16 ssn) 665 { 666 struct iwl_scd_txq_cfg_cmd cmd = { 667 .scd_queue = queue, 668 .action = SCD_CFG_ENABLE_QUEUE, 669 .window = frame_limit, 670 .sta_id = sta_id, 671 .ssn = cpu_to_le16(ssn), 672 .tx_fifo = fifo, 673 .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE || 674 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE), 675 .tid = tid, 676 }; 677 int ret; 678 679 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 680 return -EINVAL; 681 682 if (WARN(mvm->queue_info[queue].tid_bitmap == 0, 683 "Trying to reconfig unallocated queue %d\n", queue)) 684 return -ENXIO; 685 686 IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue); 687 688 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd); 689 WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n", 690 queue, fifo, ret); 691 692 return ret; 693 } 694 695 /* 696 * If a given queue has a higher AC than the TID stream that is being compared 697 * to, the queue needs to be redirected to the lower AC. This function does that 698 * in such a case, otherwise - if no redirection required - it does nothing, 699 * unless the %force param is true. 700 */ 701 static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid, 702 int ac, int ssn, unsigned int wdg_timeout, 703 bool force, struct iwl_mvm_txq *txq) 704 { 705 struct iwl_scd_txq_cfg_cmd cmd = { 706 .scd_queue = queue, 707 .action = SCD_CFG_DISABLE_QUEUE, 708 }; 709 bool shared_queue; 710 int ret; 711 712 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 713 return -EINVAL; 714 715 /* 716 * If the AC is lower than current one - FIFO needs to be redirected to 717 * the lowest one of the streams in the queue. Check if this is needed 718 * here. 719 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with 720 * value 3 and VO with value 0, so to check if ac X is lower than ac Y 721 * we need to check if the numerical value of X is LARGER than of Y. 722 */ 723 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) { 724 IWL_DEBUG_TX_QUEUES(mvm, 725 "No redirection needed on TXQ #%d\n", 726 queue); 727 return 0; 728 } 729 730 cmd.sta_id = mvm->queue_info[queue].ra_sta_id; 731 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac]; 732 cmd.tid = mvm->queue_info[queue].txq_tid; 733 shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1; 734 735 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n", 736 queue, iwl_mvm_ac_to_tx_fifo[ac]); 737 738 /* Stop the queue and wait for it to empty */ 739 set_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state); 740 741 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue)); 742 if (ret) { 743 IWL_ERR(mvm, "Error draining queue %d before reconfig\n", 744 queue); 745 ret = -EIO; 746 goto out; 747 } 748 749 /* Before redirecting the queue we need to de-activate it */ 750 iwl_trans_txq_disable(mvm->trans, queue, false); 751 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd); 752 if (ret) 753 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue, 754 ret); 755 756 /* Make sure the SCD wrptr is correctly set before reconfiguring */ 757 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout); 758 759 /* Update the TID "owner" of the queue */ 760 mvm->queue_info[queue].txq_tid = tid; 761 762 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */ 763 764 /* Redirect to lower AC */ 765 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac], 766 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn); 767 768 /* Update AC marking of the queue */ 769 mvm->queue_info[queue].mac80211_ac = ac; 770 771 /* 772 * Mark queue as shared in transport if shared 773 * Note this has to be done after queue enablement because enablement 774 * can also set this value, and there is no indication there to shared 775 * queues 776 */ 777 if (shared_queue) 778 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true); 779 780 out: 781 /* Continue using the queue */ 782 clear_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state); 783 784 return ret; 785 } 786 787 static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id, 788 u8 minq, u8 maxq) 789 { 790 int i; 791 792 lockdep_assert_held(&mvm->mutex); 793 794 if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues, 795 "max queue %d >= num_of_queues (%d)", maxq, 796 mvm->trans->trans_cfg->base_params->num_of_queues)) 797 maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1; 798 799 /* This should not be hit with new TX path */ 800 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 801 return -ENOSPC; 802 803 /* Start by looking for a free queue */ 804 for (i = minq; i <= maxq; i++) 805 if (mvm->queue_info[i].tid_bitmap == 0 && 806 mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE) 807 return i; 808 809 return -ENOSPC; 810 } 811 812 static int iwl_mvm_get_queue_size(struct ieee80211_sta *sta) 813 { 814 int max_size = IWL_DEFAULT_QUEUE_SIZE; 815 unsigned int link_id; 816 817 /* this queue isn't used for traffic (cab_queue) */ 818 if (!sta) 819 return IWL_MGMT_QUEUE_SIZE; 820 821 rcu_read_lock(); 822 823 for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) { 824 struct ieee80211_link_sta *link = 825 rcu_dereference(sta->link[link_id]); 826 827 if (!link) 828 continue; 829 830 /* support for 512 ba size */ 831 if (link->eht_cap.has_eht && 832 max_size < IWL_DEFAULT_QUEUE_SIZE_EHT) 833 max_size = IWL_DEFAULT_QUEUE_SIZE_EHT; 834 835 /* support for 256 ba size */ 836 if (link->he_cap.has_he && 837 max_size < IWL_DEFAULT_QUEUE_SIZE_HE) 838 max_size = IWL_DEFAULT_QUEUE_SIZE_HE; 839 } 840 841 rcu_read_unlock(); 842 return max_size; 843 } 844 845 int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm, 846 struct ieee80211_sta *sta, 847 u8 sta_id, u8 tid, unsigned int timeout) 848 { 849 int queue, size; 850 u32 sta_mask = 0; 851 852 if (tid == IWL_MAX_TID_COUNT) { 853 tid = IWL_MGMT_TID; 854 size = max_t(u32, IWL_MGMT_QUEUE_SIZE, 855 mvm->trans->cfg->min_txq_size); 856 } else { 857 size = iwl_mvm_get_queue_size(sta); 858 } 859 860 /* take the min with bc tbl entries allowed */ 861 size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16)); 862 863 /* size needs to be power of 2 values for calculating read/write pointers */ 864 size = rounddown_pow_of_two(size); 865 866 if (sta) { 867 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 868 struct ieee80211_link_sta *link_sta; 869 unsigned int link_id; 870 871 rcu_read_lock(); 872 for_each_sta_active_link(mvmsta->vif, sta, link_sta, link_id) { 873 struct iwl_mvm_link_sta *link = 874 rcu_dereference_protected(mvmsta->link[link_id], 875 lockdep_is_held(&mvm->mutex)); 876 877 if (!link) 878 continue; 879 880 sta_mask |= BIT(link->sta_id); 881 } 882 rcu_read_unlock(); 883 } else { 884 sta_mask |= BIT(sta_id); 885 } 886 887 if (!sta_mask) 888 return -EINVAL; 889 890 do { 891 queue = iwl_trans_txq_alloc(mvm->trans, 0, sta_mask, 892 tid, size, timeout); 893 894 if (queue < 0) 895 IWL_DEBUG_TX_QUEUES(mvm, 896 "Failed allocating TXQ of size %d for sta mask %x tid %d, ret: %d\n", 897 size, sta_mask, tid, queue); 898 size /= 2; 899 } while (queue < 0 && size >= 16); 900 901 if (queue < 0) 902 return queue; 903 904 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta mask 0x%x tid %d\n", 905 queue, sta_mask, tid); 906 907 return queue; 908 } 909 910 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm, 911 struct ieee80211_sta *sta, u8 ac, 912 int tid) 913 { 914 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 915 struct iwl_mvm_txq *mvmtxq = 916 iwl_mvm_txq_from_tid(sta, tid); 917 unsigned int wdg_timeout = 918 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false); 919 int queue = -1; 920 921 lockdep_assert_held(&mvm->mutex); 922 923 IWL_DEBUG_TX_QUEUES(mvm, 924 "Allocating queue for sta %d on tid %d\n", 925 mvmsta->deflink.sta_id, tid); 926 queue = iwl_mvm_tvqm_enable_txq(mvm, sta, mvmsta->deflink.sta_id, 927 tid, wdg_timeout); 928 if (queue < 0) 929 return queue; 930 931 mvmtxq->txq_id = queue; 932 mvm->tvqm_info[queue].txq_tid = tid; 933 mvm->tvqm_info[queue].sta_id = mvmsta->deflink.sta_id; 934 935 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue); 936 937 spin_lock_bh(&mvmsta->lock); 938 mvmsta->tid_data[tid].txq_id = queue; 939 spin_unlock_bh(&mvmsta->lock); 940 941 return 0; 942 } 943 944 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm, 945 struct ieee80211_sta *sta, 946 int queue, u8 sta_id, u8 tid) 947 { 948 bool enable_queue = true; 949 950 /* Make sure this TID isn't already enabled */ 951 if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) { 952 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n", 953 queue, tid); 954 return false; 955 } 956 957 /* Update mappings and refcounts */ 958 if (mvm->queue_info[queue].tid_bitmap) 959 enable_queue = false; 960 961 mvm->queue_info[queue].tid_bitmap |= BIT(tid); 962 mvm->queue_info[queue].ra_sta_id = sta_id; 963 964 if (enable_queue) { 965 if (tid != IWL_MAX_TID_COUNT) 966 mvm->queue_info[queue].mac80211_ac = 967 tid_to_mac80211_ac[tid]; 968 else 969 mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO; 970 971 mvm->queue_info[queue].txq_tid = tid; 972 } 973 974 if (sta) { 975 struct iwl_mvm_txq *mvmtxq = 976 iwl_mvm_txq_from_tid(sta, tid); 977 978 mvmtxq->txq_id = queue; 979 } 980 981 IWL_DEBUG_TX_QUEUES(mvm, 982 "Enabling TXQ #%d tids=0x%x\n", 983 queue, mvm->queue_info[queue].tid_bitmap); 984 985 return enable_queue; 986 } 987 988 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 989 int queue, u16 ssn, 990 const struct iwl_trans_txq_scd_cfg *cfg, 991 unsigned int wdg_timeout) 992 { 993 struct iwl_scd_txq_cfg_cmd cmd = { 994 .scd_queue = queue, 995 .action = SCD_CFG_ENABLE_QUEUE, 996 .window = cfg->frame_limit, 997 .sta_id = cfg->sta_id, 998 .ssn = cpu_to_le16(ssn), 999 .tx_fifo = cfg->fifo, 1000 .aggregate = cfg->aggregate, 1001 .tid = cfg->tid, 1002 }; 1003 bool inc_ssn; 1004 1005 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 1006 return false; 1007 1008 /* Send the enabling command if we need to */ 1009 if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid)) 1010 return false; 1011 1012 inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, 1013 NULL, wdg_timeout); 1014 if (inc_ssn) 1015 le16_add_cpu(&cmd.ssn, 1); 1016 1017 WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd), 1018 "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo); 1019 1020 return inc_ssn; 1021 } 1022 1023 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue) 1024 { 1025 struct iwl_scd_txq_cfg_cmd cmd = { 1026 .scd_queue = queue, 1027 .action = SCD_CFG_UPDATE_QUEUE_TID, 1028 }; 1029 int tid; 1030 unsigned long tid_bitmap; 1031 int ret; 1032 1033 lockdep_assert_held(&mvm->mutex); 1034 1035 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 1036 return; 1037 1038 tid_bitmap = mvm->queue_info[queue].tid_bitmap; 1039 1040 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue)) 1041 return; 1042 1043 /* Find any TID for queue */ 1044 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1); 1045 cmd.tid = tid; 1046 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]]; 1047 1048 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd); 1049 if (ret) { 1050 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n", 1051 queue, ret); 1052 return; 1053 } 1054 1055 mvm->queue_info[queue].txq_tid = tid; 1056 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n", 1057 queue, tid); 1058 } 1059 1060 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue) 1061 { 1062 struct ieee80211_sta *sta; 1063 struct iwl_mvm_sta *mvmsta; 1064 u8 sta_id; 1065 int tid = -1; 1066 unsigned long tid_bitmap; 1067 unsigned int wdg_timeout; 1068 int ssn; 1069 int ret = true; 1070 1071 /* queue sharing is disabled on new TX path */ 1072 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 1073 return; 1074 1075 lockdep_assert_held(&mvm->mutex); 1076 1077 sta_id = mvm->queue_info[queue].ra_sta_id; 1078 tid_bitmap = mvm->queue_info[queue].tid_bitmap; 1079 1080 /* Find TID for queue, and make sure it is the only one on the queue */ 1081 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1); 1082 if (tid_bitmap != BIT(tid)) { 1083 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n", 1084 queue, tid_bitmap); 1085 return; 1086 } 1087 1088 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue, 1089 tid); 1090 1091 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], 1092 lockdep_is_held(&mvm->mutex)); 1093 1094 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) 1095 return; 1096 1097 mvmsta = iwl_mvm_sta_from_mac80211(sta); 1098 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false); 1099 1100 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number); 1101 1102 ret = iwl_mvm_redirect_queue(mvm, queue, tid, 1103 tid_to_mac80211_ac[tid], ssn, 1104 wdg_timeout, true, 1105 iwl_mvm_txq_from_tid(sta, tid)); 1106 if (ret) { 1107 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue); 1108 return; 1109 } 1110 1111 /* If aggs should be turned back on - do it */ 1112 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) { 1113 struct iwl_mvm_add_sta_cmd cmd = {0}; 1114 1115 mvmsta->tid_disable_agg &= ~BIT(tid); 1116 1117 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color); 1118 cmd.sta_id = mvmsta->deflink.sta_id; 1119 cmd.add_modify = STA_MODE_MODIFY; 1120 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX; 1121 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk); 1122 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg); 1123 1124 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, 1125 iwl_mvm_add_sta_cmd_size(mvm), &cmd); 1126 if (!ret) { 1127 IWL_DEBUG_TX_QUEUES(mvm, 1128 "TXQ #%d is now aggregated again\n", 1129 queue); 1130 1131 /* Mark queue intenally as aggregating again */ 1132 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false); 1133 } 1134 } 1135 1136 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY; 1137 } 1138 1139 /* 1140 * Remove inactive TIDs of a given queue. 1141 * If all queue TIDs are inactive - mark the queue as inactive 1142 * If only some the queue TIDs are inactive - unmap them from the queue 1143 * 1144 * Returns %true if all TIDs were removed and the queue could be reused. 1145 */ 1146 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm, 1147 struct iwl_mvm_sta *mvmsta, int queue, 1148 unsigned long tid_bitmap, 1149 unsigned long *unshare_queues, 1150 unsigned long *changetid_queues) 1151 { 1152 unsigned int tid; 1153 1154 lockdep_assert_held(&mvmsta->lock); 1155 lockdep_assert_held(&mvm->mutex); 1156 1157 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 1158 return false; 1159 1160 /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */ 1161 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) { 1162 /* If some TFDs are still queued - don't mark TID as inactive */ 1163 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid])) 1164 tid_bitmap &= ~BIT(tid); 1165 1166 /* Don't mark as inactive any TID that has an active BA */ 1167 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) 1168 tid_bitmap &= ~BIT(tid); 1169 } 1170 1171 /* If all TIDs in the queue are inactive - return it can be reused */ 1172 if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) { 1173 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue); 1174 return true; 1175 } 1176 1177 /* 1178 * If we are here, this is a shared queue and not all TIDs timed-out. 1179 * Remove the ones that did. 1180 */ 1181 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) { 1182 u16 q_tid_bitmap; 1183 1184 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE; 1185 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid); 1186 1187 q_tid_bitmap = mvm->queue_info[queue].tid_bitmap; 1188 1189 /* 1190 * We need to take into account a situation in which a TXQ was 1191 * allocated to TID x, and then turned shared by adding TIDs y 1192 * and z. If TID x becomes inactive and is removed from the TXQ, 1193 * ownership must be given to one of the remaining TIDs. 1194 * This is mainly because if TID x continues - a new queue can't 1195 * be allocated for it as long as it is an owner of another TXQ. 1196 * 1197 * Mark this queue in the right bitmap, we'll send the command 1198 * to the firmware later. 1199 */ 1200 if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid))) 1201 set_bit(queue, changetid_queues); 1202 1203 IWL_DEBUG_TX_QUEUES(mvm, 1204 "Removing inactive TID %d from shared Q:%d\n", 1205 tid, queue); 1206 } 1207 1208 IWL_DEBUG_TX_QUEUES(mvm, 1209 "TXQ #%d left with tid bitmap 0x%x\n", queue, 1210 mvm->queue_info[queue].tid_bitmap); 1211 1212 /* 1213 * There may be different TIDs with the same mac queues, so make 1214 * sure all TIDs have existing corresponding mac queues enabled 1215 */ 1216 tid_bitmap = mvm->queue_info[queue].tid_bitmap; 1217 1218 /* If the queue is marked as shared - "unshare" it */ 1219 if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 && 1220 mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) { 1221 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n", 1222 queue); 1223 set_bit(queue, unshare_queues); 1224 } 1225 1226 return false; 1227 } 1228 1229 /* 1230 * Check for inactivity - this includes checking if any queue 1231 * can be unshared and finding one (and only one) that can be 1232 * reused. 1233 * This function is also invoked as a sort of clean-up task, 1234 * in which case @alloc_for_sta is IWL_MVM_INVALID_STA. 1235 * 1236 * Returns the queue number, or -ENOSPC. 1237 */ 1238 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta) 1239 { 1240 unsigned long now = jiffies; 1241 unsigned long unshare_queues = 0; 1242 unsigned long changetid_queues = 0; 1243 int i, ret, free_queue = -ENOSPC; 1244 struct ieee80211_sta *queue_owner = NULL; 1245 1246 lockdep_assert_held(&mvm->mutex); 1247 1248 if (iwl_mvm_has_new_tx_api(mvm)) 1249 return -ENOSPC; 1250 1251 rcu_read_lock(); 1252 1253 /* we skip the CMD queue below by starting at 1 */ 1254 BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0); 1255 1256 for (i = 1; i < IWL_MAX_HW_QUEUES; i++) { 1257 struct ieee80211_sta *sta; 1258 struct iwl_mvm_sta *mvmsta; 1259 u8 sta_id; 1260 int tid; 1261 unsigned long inactive_tid_bitmap = 0; 1262 unsigned long queue_tid_bitmap; 1263 1264 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap; 1265 if (!queue_tid_bitmap) 1266 continue; 1267 1268 /* If TXQ isn't in active use anyway - nothing to do here... */ 1269 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY && 1270 mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED) 1271 continue; 1272 1273 /* Check to see if there are inactive TIDs on this queue */ 1274 for_each_set_bit(tid, &queue_tid_bitmap, 1275 IWL_MAX_TID_COUNT + 1) { 1276 if (time_after(mvm->queue_info[i].last_frame_time[tid] + 1277 IWL_MVM_DQA_QUEUE_TIMEOUT, now)) 1278 continue; 1279 1280 inactive_tid_bitmap |= BIT(tid); 1281 } 1282 1283 /* If all TIDs are active - finish check on this queue */ 1284 if (!inactive_tid_bitmap) 1285 continue; 1286 1287 /* 1288 * If we are here - the queue hadn't been served recently and is 1289 * in use 1290 */ 1291 1292 sta_id = mvm->queue_info[i].ra_sta_id; 1293 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]); 1294 1295 /* 1296 * If the STA doesn't exist anymore, it isn't an error. It could 1297 * be that it was removed since getting the queues, and in this 1298 * case it should've inactivated its queues anyway. 1299 */ 1300 if (IS_ERR_OR_NULL(sta)) 1301 continue; 1302 1303 mvmsta = iwl_mvm_sta_from_mac80211(sta); 1304 1305 spin_lock_bh(&mvmsta->lock); 1306 ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i, 1307 inactive_tid_bitmap, 1308 &unshare_queues, 1309 &changetid_queues); 1310 if (ret && free_queue < 0) { 1311 queue_owner = sta; 1312 free_queue = i; 1313 } 1314 /* only unlock sta lock - we still need the queue info lock */ 1315 spin_unlock_bh(&mvmsta->lock); 1316 } 1317 1318 1319 /* Reconfigure queues requiring reconfiguation */ 1320 for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES) 1321 iwl_mvm_unshare_queue(mvm, i); 1322 for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES) 1323 iwl_mvm_change_queue_tid(mvm, i); 1324 1325 rcu_read_unlock(); 1326 1327 if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) { 1328 ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner, 1329 alloc_for_sta); 1330 if (ret) 1331 return ret; 1332 } 1333 1334 return free_queue; 1335 } 1336 1337 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm, 1338 struct ieee80211_sta *sta, u8 ac, int tid) 1339 { 1340 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 1341 struct iwl_trans_txq_scd_cfg cfg = { 1342 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac), 1343 .sta_id = mvmsta->deflink.sta_id, 1344 .tid = tid, 1345 .frame_limit = IWL_FRAME_LIMIT, 1346 }; 1347 unsigned int wdg_timeout = 1348 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false); 1349 int queue = -1; 1350 u16 queue_tmp; 1351 unsigned long disable_agg_tids = 0; 1352 enum iwl_mvm_agg_state queue_state; 1353 bool shared_queue = false, inc_ssn; 1354 int ssn; 1355 unsigned long tfd_queue_mask; 1356 int ret; 1357 1358 lockdep_assert_held(&mvm->mutex); 1359 1360 if (iwl_mvm_has_new_tx_api(mvm)) 1361 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid); 1362 1363 spin_lock_bh(&mvmsta->lock); 1364 tfd_queue_mask = mvmsta->tfd_queue_msk; 1365 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number); 1366 spin_unlock_bh(&mvmsta->lock); 1367 1368 if (tid == IWL_MAX_TID_COUNT) { 1369 queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id, 1370 IWL_MVM_DQA_MIN_MGMT_QUEUE, 1371 IWL_MVM_DQA_MAX_MGMT_QUEUE); 1372 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE) 1373 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n", 1374 queue); 1375 1376 /* If no such queue is found, we'll use a DATA queue instead */ 1377 } 1378 1379 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) && 1380 (mvm->queue_info[mvmsta->reserved_queue].status == 1381 IWL_MVM_QUEUE_RESERVED)) { 1382 queue = mvmsta->reserved_queue; 1383 mvm->queue_info[queue].reserved = true; 1384 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue); 1385 } 1386 1387 if (queue < 0) 1388 queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id, 1389 IWL_MVM_DQA_MIN_DATA_QUEUE, 1390 IWL_MVM_DQA_MAX_DATA_QUEUE); 1391 if (queue < 0) { 1392 /* try harder - perhaps kill an inactive queue */ 1393 queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id); 1394 } 1395 1396 /* No free queue - we'll have to share */ 1397 if (queue <= 0) { 1398 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac); 1399 if (queue > 0) { 1400 shared_queue = true; 1401 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED; 1402 } 1403 } 1404 1405 /* 1406 * Mark TXQ as ready, even though it hasn't been fully configured yet, 1407 * to make sure no one else takes it. 1408 * This will allow avoiding re-acquiring the lock at the end of the 1409 * configuration. On error we'll mark it back as free. 1410 */ 1411 if (queue > 0 && !shared_queue) 1412 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY; 1413 1414 /* This shouldn't happen - out of queues */ 1415 if (WARN_ON(queue <= 0)) { 1416 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n", 1417 tid, cfg.sta_id); 1418 return queue; 1419 } 1420 1421 /* 1422 * Actual en/disablement of aggregations is through the ADD_STA HCMD, 1423 * but for configuring the SCD to send A-MPDUs we need to mark the queue 1424 * as aggregatable. 1425 * Mark all DATA queues as allowing to be aggregated at some point 1426 */ 1427 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE || 1428 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE); 1429 1430 IWL_DEBUG_TX_QUEUES(mvm, 1431 "Allocating %squeue #%d to sta %d on tid %d\n", 1432 shared_queue ? "shared " : "", queue, 1433 mvmsta->deflink.sta_id, tid); 1434 1435 if (shared_queue) { 1436 /* Disable any open aggs on this queue */ 1437 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue); 1438 1439 if (disable_agg_tids) { 1440 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n", 1441 queue); 1442 iwl_mvm_invalidate_sta_queue(mvm, queue, 1443 disable_agg_tids, false); 1444 } 1445 } 1446 1447 inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout); 1448 1449 /* 1450 * Mark queue as shared in transport if shared 1451 * Note this has to be done after queue enablement because enablement 1452 * can also set this value, and there is no indication there to shared 1453 * queues 1454 */ 1455 if (shared_queue) 1456 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true); 1457 1458 spin_lock_bh(&mvmsta->lock); 1459 /* 1460 * This looks racy, but it is not. We have only one packet for 1461 * this ra/tid in our Tx path since we stop the Qdisc when we 1462 * need to allocate a new TFD queue. 1463 */ 1464 if (inc_ssn) { 1465 mvmsta->tid_data[tid].seq_number += 0x10; 1466 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ; 1467 } 1468 mvmsta->tid_data[tid].txq_id = queue; 1469 mvmsta->tfd_queue_msk |= BIT(queue); 1470 queue_state = mvmsta->tid_data[tid].state; 1471 1472 if (mvmsta->reserved_queue == queue) 1473 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE; 1474 spin_unlock_bh(&mvmsta->lock); 1475 1476 if (!shared_queue) { 1477 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES); 1478 if (ret) 1479 goto out_err; 1480 1481 /* If we need to re-enable aggregations... */ 1482 if (queue_state == IWL_AGG_ON) { 1483 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true); 1484 if (ret) 1485 goto out_err; 1486 } 1487 } else { 1488 /* Redirect queue, if needed */ 1489 ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn, 1490 wdg_timeout, false, 1491 iwl_mvm_txq_from_tid(sta, tid)); 1492 if (ret) 1493 goto out_err; 1494 } 1495 1496 return 0; 1497 1498 out_err: 1499 queue_tmp = queue; 1500 iwl_mvm_disable_txq(mvm, sta, mvmsta->deflink.sta_id, &queue_tmp, tid); 1501 1502 return ret; 1503 } 1504 1505 int iwl_mvm_sta_ensure_queue(struct iwl_mvm *mvm, 1506 struct ieee80211_txq *txq) 1507 { 1508 struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq); 1509 int ret = -EINVAL; 1510 1511 lockdep_assert_held(&mvm->mutex); 1512 1513 if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) || 1514 !txq->sta) { 1515 return 0; 1516 } 1517 1518 if (!iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, txq->tid)) { 1519 set_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state); 1520 ret = 0; 1521 } 1522 1523 local_bh_disable(); 1524 spin_lock(&mvm->add_stream_lock); 1525 if (!list_empty(&mvmtxq->list)) 1526 list_del_init(&mvmtxq->list); 1527 spin_unlock(&mvm->add_stream_lock); 1528 local_bh_enable(); 1529 1530 return ret; 1531 } 1532 1533 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk) 1534 { 1535 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, 1536 add_stream_wk); 1537 1538 mutex_lock(&mvm->mutex); 1539 1540 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA); 1541 1542 while (!list_empty(&mvm->add_stream_txqs)) { 1543 struct iwl_mvm_txq *mvmtxq; 1544 struct ieee80211_txq *txq; 1545 u8 tid; 1546 1547 mvmtxq = list_first_entry(&mvm->add_stream_txqs, 1548 struct iwl_mvm_txq, list); 1549 1550 txq = container_of((void *)mvmtxq, struct ieee80211_txq, 1551 drv_priv); 1552 tid = txq->tid; 1553 if (tid == IEEE80211_NUM_TIDS) 1554 tid = IWL_MAX_TID_COUNT; 1555 1556 /* 1557 * We can't really do much here, but if this fails we can't 1558 * transmit anyway - so just don't transmit the frame etc. 1559 * and let them back up ... we've tried our best to allocate 1560 * a queue in the function itself. 1561 */ 1562 if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) { 1563 spin_lock_bh(&mvm->add_stream_lock); 1564 list_del_init(&mvmtxq->list); 1565 spin_unlock_bh(&mvm->add_stream_lock); 1566 continue; 1567 } 1568 1569 /* now we're ready, any remaining races/concurrency will be 1570 * handled in iwl_mvm_mac_itxq_xmit() 1571 */ 1572 set_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state); 1573 1574 local_bh_disable(); 1575 spin_lock(&mvm->add_stream_lock); 1576 list_del_init(&mvmtxq->list); 1577 spin_unlock(&mvm->add_stream_lock); 1578 1579 iwl_mvm_mac_itxq_xmit(mvm->hw, txq); 1580 local_bh_enable(); 1581 } 1582 1583 mutex_unlock(&mvm->mutex); 1584 } 1585 1586 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm, 1587 struct ieee80211_sta *sta, 1588 enum nl80211_iftype vif_type) 1589 { 1590 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 1591 int queue; 1592 1593 /* queue reserving is disabled on new TX path */ 1594 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) 1595 return 0; 1596 1597 /* run the general cleanup/unsharing of queues */ 1598 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA); 1599 1600 /* Make sure we have free resources for this STA */ 1601 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls && 1602 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap && 1603 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status == 1604 IWL_MVM_QUEUE_FREE)) 1605 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE; 1606 else 1607 queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id, 1608 IWL_MVM_DQA_MIN_DATA_QUEUE, 1609 IWL_MVM_DQA_MAX_DATA_QUEUE); 1610 if (queue < 0) { 1611 /* try again - this time kick out a queue if needed */ 1612 queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id); 1613 if (queue < 0) { 1614 IWL_ERR(mvm, "No available queues for new station\n"); 1615 return -ENOSPC; 1616 } 1617 } 1618 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED; 1619 1620 mvmsta->reserved_queue = queue; 1621 1622 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n", 1623 queue, mvmsta->deflink.sta_id); 1624 1625 return 0; 1626 } 1627 1628 /* 1629 * In DQA mode, after a HW restart the queues should be allocated as before, in 1630 * order to avoid race conditions when there are shared queues. This function 1631 * does the re-mapping and queue allocation. 1632 * 1633 * Note that re-enabling aggregations isn't done in this function. 1634 */ 1635 void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm, 1636 struct ieee80211_sta *sta) 1637 { 1638 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 1639 unsigned int wdg = 1640 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false); 1641 int i; 1642 struct iwl_trans_txq_scd_cfg cfg = { 1643 .sta_id = mvm_sta->deflink.sta_id, 1644 .frame_limit = IWL_FRAME_LIMIT, 1645 }; 1646 1647 /* Make sure reserved queue is still marked as such (if allocated) */ 1648 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) 1649 mvm->queue_info[mvm_sta->reserved_queue].status = 1650 IWL_MVM_QUEUE_RESERVED; 1651 1652 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) { 1653 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i]; 1654 int txq_id = tid_data->txq_id; 1655 int ac; 1656 1657 if (txq_id == IWL_MVM_INVALID_QUEUE) 1658 continue; 1659 1660 ac = tid_to_mac80211_ac[i]; 1661 1662 if (iwl_mvm_has_new_tx_api(mvm)) { 1663 IWL_DEBUG_TX_QUEUES(mvm, 1664 "Re-mapping sta %d tid %d\n", 1665 mvm_sta->deflink.sta_id, i); 1666 txq_id = iwl_mvm_tvqm_enable_txq(mvm, sta, 1667 mvm_sta->deflink.sta_id, 1668 i, wdg); 1669 /* 1670 * on failures, just set it to IWL_MVM_INVALID_QUEUE 1671 * to try again later, we have no other good way of 1672 * failing here 1673 */ 1674 if (txq_id < 0) 1675 txq_id = IWL_MVM_INVALID_QUEUE; 1676 tid_data->txq_id = txq_id; 1677 1678 /* 1679 * Since we don't set the seq number after reset, and HW 1680 * sets it now, FW reset will cause the seq num to start 1681 * at 0 again, so driver will need to update it 1682 * internally as well, so it keeps in sync with real val 1683 */ 1684 tid_data->seq_number = 0; 1685 } else { 1686 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number); 1687 1688 cfg.tid = i; 1689 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac); 1690 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE || 1691 txq_id == 1692 IWL_MVM_DQA_BSS_CLIENT_QUEUE); 1693 1694 IWL_DEBUG_TX_QUEUES(mvm, 1695 "Re-mapping sta %d tid %d to queue %d\n", 1696 mvm_sta->deflink.sta_id, i, 1697 txq_id); 1698 1699 iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg); 1700 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY; 1701 } 1702 } 1703 } 1704 1705 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm, 1706 struct iwl_mvm_int_sta *sta, 1707 const u8 *addr, 1708 u16 mac_id, u16 color) 1709 { 1710 struct iwl_mvm_add_sta_cmd cmd; 1711 int ret; 1712 u32 status = ADD_STA_SUCCESS; 1713 1714 lockdep_assert_held(&mvm->mutex); 1715 1716 memset(&cmd, 0, sizeof(cmd)); 1717 cmd.sta_id = sta->sta_id; 1718 1719 if (iwl_mvm_has_new_station_api(mvm->fw) && 1720 sta->type == IWL_STA_AUX_ACTIVITY) 1721 cmd.mac_id_n_color = cpu_to_le32(mac_id); 1722 else 1723 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id, 1724 color)); 1725 1726 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) 1727 cmd.station_type = sta->type; 1728 1729 if (!iwl_mvm_has_new_tx_api(mvm)) 1730 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk); 1731 cmd.tid_disable_tx = cpu_to_le16(0xffff); 1732 1733 if (addr) 1734 memcpy(cmd.addr, addr, ETH_ALEN); 1735 1736 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, 1737 iwl_mvm_add_sta_cmd_size(mvm), 1738 &cmd, &status); 1739 if (ret) 1740 return ret; 1741 1742 switch (status & IWL_ADD_STA_STATUS_MASK) { 1743 case ADD_STA_SUCCESS: 1744 IWL_DEBUG_INFO(mvm, "Internal station added.\n"); 1745 return 0; 1746 default: 1747 ret = -EIO; 1748 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n", 1749 status); 1750 break; 1751 } 1752 return ret; 1753 } 1754 1755 /* Initialize driver data of a new sta */ 1756 int iwl_mvm_sta_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 1757 struct ieee80211_sta *sta, int sta_id, u8 sta_type) 1758 { 1759 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 1760 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 1761 struct iwl_mvm_rxq_dup_data *dup_data; 1762 int i, ret = 0; 1763 1764 lockdep_assert_held(&mvm->mutex); 1765 1766 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id, 1767 mvmvif->color); 1768 mvm_sta->vif = vif; 1769 1770 /* for MLD sta_id(s) should be allocated for each link before calling 1771 * this function 1772 */ 1773 if (!mvm->mld_api_is_used) { 1774 if (WARN_ON(sta_id == IWL_MVM_INVALID_STA)) 1775 return -EINVAL; 1776 1777 mvm_sta->deflink.sta_id = sta_id; 1778 rcu_assign_pointer(mvm_sta->link[0], &mvm_sta->deflink); 1779 1780 if (!mvm->trans->trans_cfg->gen2) 1781 mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize = 1782 LINK_QUAL_AGG_FRAME_LIMIT_DEF; 1783 else 1784 mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize = 1785 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF; 1786 } 1787 1788 mvm_sta->tt_tx_protection = false; 1789 mvm_sta->sta_type = sta_type; 1790 1791 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */ 1792 1793 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) { 1794 /* 1795 * Mark all queues for this STA as unallocated and defer TX 1796 * frames until the queue is allocated 1797 */ 1798 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE; 1799 } 1800 1801 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) { 1802 struct iwl_mvm_txq *mvmtxq = 1803 iwl_mvm_txq_from_mac80211(sta->txq[i]); 1804 1805 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE; 1806 INIT_LIST_HEAD(&mvmtxq->list); 1807 atomic_set(&mvmtxq->tx_request, 0); 1808 } 1809 1810 if (iwl_mvm_has_new_rx_api(mvm)) { 1811 int q; 1812 1813 dup_data = kcalloc(mvm->trans->num_rx_queues, 1814 sizeof(*dup_data), GFP_KERNEL); 1815 if (!dup_data) 1816 return -ENOMEM; 1817 /* 1818 * Initialize all the last_seq values to 0xffff which can never 1819 * compare equal to the frame's seq_ctrl in the check in 1820 * iwl_mvm_is_dup() since the lower 4 bits are the fragment 1821 * number and fragmented packets don't reach that function. 1822 * 1823 * This thus allows receiving a packet with seqno 0 and the 1824 * retry bit set as the very first packet on a new TID. 1825 */ 1826 for (q = 0; q < mvm->trans->num_rx_queues; q++) 1827 memset(dup_data[q].last_seq, 0xff, 1828 sizeof(dup_data[q].last_seq)); 1829 mvm_sta->dup_data = dup_data; 1830 } 1831 1832 if (!iwl_mvm_has_new_tx_api(mvm)) { 1833 ret = iwl_mvm_reserve_sta_stream(mvm, sta, 1834 ieee80211_vif_type_p2p(vif)); 1835 if (ret) 1836 return ret; 1837 } 1838 1839 /* 1840 * if rs is registered with mac80211, then "add station" will be handled 1841 * via the corresponding ops, otherwise need to notify rate scaling here 1842 */ 1843 if (iwl_mvm_has_tlc_offload(mvm)) 1844 iwl_mvm_rs_add_sta(mvm, mvm_sta); 1845 else 1846 spin_lock_init(&mvm_sta->deflink.lq_sta.rs_drv.pers.lock); 1847 1848 iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant); 1849 1850 /* MPDUs are counted only when EMLSR is possible */ 1851 if (vif->type == NL80211_IFTYPE_STATION && !vif->p2p && 1852 !sta->tdls && ieee80211_vif_is_mld(vif)) { 1853 mvm_sta->mpdu_counters = 1854 kcalloc(mvm->trans->num_rx_queues, 1855 sizeof(*mvm_sta->mpdu_counters), 1856 GFP_KERNEL); 1857 if (mvm_sta->mpdu_counters) 1858 for (int q = 0; q < mvm->trans->num_rx_queues; q++) 1859 spin_lock_init(&mvm_sta->mpdu_counters[q].lock); 1860 } 1861 1862 return 0; 1863 } 1864 1865 int iwl_mvm_add_sta(struct iwl_mvm *mvm, 1866 struct ieee80211_vif *vif, 1867 struct ieee80211_sta *sta) 1868 { 1869 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 1870 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 1871 int ret, sta_id; 1872 bool sta_update = false; 1873 unsigned int sta_flags = 0; 1874 1875 lockdep_assert_held(&mvm->mutex); 1876 1877 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) 1878 sta_id = iwl_mvm_find_free_sta_id(mvm, 1879 ieee80211_vif_type_p2p(vif)); 1880 else 1881 sta_id = mvm_sta->deflink.sta_id; 1882 1883 if (sta_id == IWL_MVM_INVALID_STA) 1884 return -ENOSPC; 1885 1886 spin_lock_init(&mvm_sta->lock); 1887 1888 /* if this is a HW restart re-alloc existing queues */ 1889 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) { 1890 struct iwl_mvm_int_sta tmp_sta = { 1891 .sta_id = sta_id, 1892 .type = mvm_sta->sta_type, 1893 }; 1894 1895 /* First add an empty station since allocating 1896 * a queue requires a valid station 1897 */ 1898 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr, 1899 mvmvif->id, mvmvif->color); 1900 if (ret) 1901 goto err; 1902 1903 iwl_mvm_realloc_queues_after_restart(mvm, sta); 1904 sta_update = true; 1905 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES; 1906 goto update_fw; 1907 } 1908 1909 ret = iwl_mvm_sta_init(mvm, vif, sta, sta_id, 1910 sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK); 1911 if (ret) 1912 goto err; 1913 1914 update_fw: 1915 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags); 1916 if (ret) 1917 goto err; 1918 1919 if (vif->type == NL80211_IFTYPE_STATION) { 1920 if (!sta->tdls) { 1921 WARN_ON(mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA); 1922 mvmvif->deflink.ap_sta_id = sta_id; 1923 } else { 1924 WARN_ON(mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA); 1925 } 1926 } 1927 1928 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta); 1929 1930 return 0; 1931 1932 err: 1933 return ret; 1934 } 1935 1936 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta, 1937 bool drain) 1938 { 1939 struct iwl_mvm_add_sta_cmd cmd = {}; 1940 int ret; 1941 u32 status; 1942 1943 lockdep_assert_held(&mvm->mutex); 1944 1945 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color); 1946 cmd.sta_id = mvmsta->deflink.sta_id; 1947 cmd.add_modify = STA_MODE_MODIFY; 1948 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0; 1949 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW); 1950 1951 status = ADD_STA_SUCCESS; 1952 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, 1953 iwl_mvm_add_sta_cmd_size(mvm), 1954 &cmd, &status); 1955 if (ret) 1956 return ret; 1957 1958 switch (status & IWL_ADD_STA_STATUS_MASK) { 1959 case ADD_STA_SUCCESS: 1960 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n", 1961 mvmsta->deflink.sta_id); 1962 break; 1963 default: 1964 ret = -EIO; 1965 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n", 1966 mvmsta->deflink.sta_id); 1967 break; 1968 } 1969 1970 return ret; 1971 } 1972 1973 /* 1974 * Remove a station from the FW table. Before sending the command to remove 1975 * the station validate that the station is indeed known to the driver (sanity 1976 * only). 1977 */ 1978 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id) 1979 { 1980 struct ieee80211_sta *sta; 1981 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = { 1982 .sta_id = sta_id, 1983 }; 1984 int ret; 1985 1986 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], 1987 lockdep_is_held(&mvm->mutex)); 1988 1989 /* Note: internal stations are marked as error values */ 1990 if (!sta) { 1991 IWL_ERR(mvm, "Invalid station id\n"); 1992 return -EINVAL; 1993 } 1994 1995 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0, 1996 sizeof(rm_sta_cmd), &rm_sta_cmd); 1997 if (ret) { 1998 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id); 1999 return ret; 2000 } 2001 2002 return 0; 2003 } 2004 2005 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm, 2006 struct ieee80211_vif *vif, 2007 struct ieee80211_sta *sta) 2008 { 2009 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 2010 int i; 2011 2012 lockdep_assert_held(&mvm->mutex); 2013 2014 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) { 2015 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE) 2016 continue; 2017 2018 iwl_mvm_disable_txq(mvm, sta, mvm_sta->deflink.sta_id, 2019 &mvm_sta->tid_data[i].txq_id, i); 2020 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE; 2021 } 2022 2023 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) { 2024 struct iwl_mvm_txq *mvmtxq = 2025 iwl_mvm_txq_from_mac80211(sta->txq[i]); 2026 2027 spin_lock_bh(&mvm->add_stream_lock); 2028 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE; 2029 list_del_init(&mvmtxq->list); 2030 clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state); 2031 spin_unlock_bh(&mvm->add_stream_lock); 2032 } 2033 } 2034 2035 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm, 2036 struct iwl_mvm_sta *mvm_sta) 2037 { 2038 int i; 2039 2040 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) { 2041 u16 txq_id; 2042 int ret; 2043 2044 spin_lock_bh(&mvm_sta->lock); 2045 txq_id = mvm_sta->tid_data[i].txq_id; 2046 spin_unlock_bh(&mvm_sta->lock); 2047 2048 if (txq_id == IWL_MVM_INVALID_QUEUE) 2049 continue; 2050 2051 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id); 2052 if (ret) 2053 return ret; 2054 } 2055 2056 return 0; 2057 } 2058 2059 /* Execute the common part for both MLD and non-MLD modes. 2060 * Returns if we're done with removing the station, either 2061 * with error or success 2062 */ 2063 bool iwl_mvm_sta_del(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 2064 struct ieee80211_sta *sta, 2065 struct ieee80211_link_sta *link_sta, int *ret) 2066 { 2067 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2068 struct iwl_mvm_vif_link_info *mvm_link = 2069 mvmvif->link[link_sta->link_id]; 2070 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 2071 struct iwl_mvm_link_sta *mvm_link_sta; 2072 u8 sta_id; 2073 2074 lockdep_assert_held(&mvm->mutex); 2075 2076 mvm_link_sta = 2077 rcu_dereference_protected(mvm_sta->link[link_sta->link_id], 2078 lockdep_is_held(&mvm->mutex)); 2079 sta_id = mvm_link_sta->sta_id; 2080 2081 /* If there is a TXQ still marked as reserved - free it */ 2082 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) { 2083 u8 reserved_txq = mvm_sta->reserved_queue; 2084 enum iwl_mvm_queue_status *status; 2085 2086 /* 2087 * If no traffic has gone through the reserved TXQ - it 2088 * is still marked as IWL_MVM_QUEUE_RESERVED, and 2089 * should be manually marked as free again 2090 */ 2091 status = &mvm->queue_info[reserved_txq].status; 2092 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) && 2093 (*status != IWL_MVM_QUEUE_FREE), 2094 "sta_id %d reserved txq %d status %d", 2095 sta_id, reserved_txq, *status)) { 2096 *ret = -EINVAL; 2097 return true; 2098 } 2099 2100 *status = IWL_MVM_QUEUE_FREE; 2101 } 2102 2103 if (vif->type == NL80211_IFTYPE_STATION && 2104 mvm_link->ap_sta_id == sta_id) { 2105 /* if associated - we can't remove the AP STA now */ 2106 if (vif->cfg.assoc) 2107 return true; 2108 2109 /* first remove remaining keys */ 2110 iwl_mvm_sec_key_remove_ap(mvm, vif, mvm_link, 0); 2111 2112 /* unassoc - go ahead - remove the AP STA now */ 2113 mvm_link->ap_sta_id = IWL_MVM_INVALID_STA; 2114 } 2115 2116 /* 2117 * This shouldn't happen - the TDLS channel switch should be canceled 2118 * before the STA is removed. 2119 */ 2120 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) { 2121 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA; 2122 cancel_delayed_work(&mvm->tdls_cs.dwork); 2123 } 2124 2125 return false; 2126 } 2127 2128 int iwl_mvm_rm_sta(struct iwl_mvm *mvm, 2129 struct ieee80211_vif *vif, 2130 struct ieee80211_sta *sta) 2131 { 2132 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 2133 int ret; 2134 2135 lockdep_assert_held(&mvm->mutex); 2136 2137 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true); 2138 if (ret) 2139 return ret; 2140 2141 /* flush its queues here since we are freeing mvm_sta */ 2142 ret = iwl_mvm_flush_sta(mvm, mvm_sta->deflink.sta_id, 2143 mvm_sta->tfd_queue_msk); 2144 if (ret) 2145 return ret; 2146 if (iwl_mvm_has_new_tx_api(mvm)) { 2147 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta); 2148 } else { 2149 u32 q_mask = mvm_sta->tfd_queue_msk; 2150 2151 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, 2152 q_mask); 2153 } 2154 if (ret) 2155 return ret; 2156 2157 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false); 2158 2159 iwl_mvm_disable_sta_queues(mvm, vif, sta); 2160 2161 if (iwl_mvm_sta_del(mvm, vif, sta, &sta->deflink, &ret)) 2162 return ret; 2163 2164 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->deflink.sta_id); 2165 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->deflink.sta_id], NULL); 2166 2167 return ret; 2168 } 2169 2170 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm, 2171 struct ieee80211_vif *vif, 2172 u8 sta_id) 2173 { 2174 int ret = iwl_mvm_rm_sta_common(mvm, sta_id); 2175 2176 lockdep_assert_held(&mvm->mutex); 2177 2178 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL); 2179 return ret; 2180 } 2181 2182 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, 2183 struct iwl_mvm_int_sta *sta, 2184 u32 qmask, enum nl80211_iftype iftype, 2185 u8 type) 2186 { 2187 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) || 2188 sta->sta_id == IWL_MVM_INVALID_STA) { 2189 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype); 2190 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA)) 2191 return -ENOSPC; 2192 } 2193 2194 sta->tfd_queue_msk = qmask; 2195 sta->type = type; 2196 2197 /* put a non-NULL value so iterating over the stations won't stop */ 2198 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL)); 2199 return 0; 2200 } 2201 2202 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta) 2203 { 2204 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL); 2205 memset(sta, 0, sizeof(struct iwl_mvm_int_sta)); 2206 sta->sta_id = IWL_MVM_INVALID_STA; 2207 } 2208 2209 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue, 2210 u8 sta_id, u8 fifo) 2211 { 2212 unsigned int wdg_timeout = 2213 mvm->trans->trans_cfg->base_params->wd_timeout; 2214 struct iwl_trans_txq_scd_cfg cfg = { 2215 .fifo = fifo, 2216 .sta_id = sta_id, 2217 .tid = IWL_MAX_TID_COUNT, 2218 .aggregate = false, 2219 .frame_limit = IWL_FRAME_LIMIT, 2220 }; 2221 2222 WARN_ON(iwl_mvm_has_new_tx_api(mvm)); 2223 2224 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout); 2225 } 2226 2227 static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id) 2228 { 2229 unsigned int wdg_timeout = 2230 mvm->trans->trans_cfg->base_params->wd_timeout; 2231 2232 WARN_ON(!iwl_mvm_has_new_tx_api(mvm)); 2233 2234 return iwl_mvm_tvqm_enable_txq(mvm, NULL, sta_id, IWL_MAX_TID_COUNT, 2235 wdg_timeout); 2236 } 2237 2238 static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx, 2239 int maccolor, u8 *addr, 2240 struct iwl_mvm_int_sta *sta, 2241 u16 *queue, int fifo) 2242 { 2243 int ret; 2244 2245 /* Map queue to fifo - needs to happen before adding station */ 2246 if (!iwl_mvm_has_new_tx_api(mvm)) 2247 iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo); 2248 2249 ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor); 2250 if (ret) { 2251 if (!iwl_mvm_has_new_tx_api(mvm)) 2252 iwl_mvm_disable_txq(mvm, NULL, sta->sta_id, queue, 2253 IWL_MAX_TID_COUNT); 2254 return ret; 2255 } 2256 2257 /* 2258 * For 22000 firmware and on we cannot add queue to a station unknown 2259 * to firmware so enable queue here - after the station was added 2260 */ 2261 if (iwl_mvm_has_new_tx_api(mvm)) { 2262 int txq; 2263 2264 txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id); 2265 if (txq < 0) { 2266 iwl_mvm_rm_sta_common(mvm, sta->sta_id); 2267 return txq; 2268 } 2269 2270 *queue = txq; 2271 } 2272 2273 return 0; 2274 } 2275 2276 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id) 2277 { 2278 int ret; 2279 u32 qmask = mvm->aux_queue == IWL_MVM_INVALID_QUEUE ? 0 : 2280 BIT(mvm->aux_queue); 2281 2282 lockdep_assert_held(&mvm->mutex); 2283 2284 /* Allocate aux station and assign to it the aux queue */ 2285 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, qmask, 2286 NL80211_IFTYPE_UNSPECIFIED, 2287 IWL_STA_AUX_ACTIVITY); 2288 if (ret) 2289 return ret; 2290 2291 /* 2292 * In CDB NICs we need to specify which lmac to use for aux activity 2293 * using the mac_id argument place to send lmac_id to the function 2294 */ 2295 ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL, 2296 &mvm->aux_sta, &mvm->aux_queue, 2297 IWL_MVM_TX_FIFO_MCAST); 2298 if (ret) { 2299 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta); 2300 return ret; 2301 } 2302 2303 return 0; 2304 } 2305 2306 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2307 { 2308 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2309 2310 lockdep_assert_held(&mvm->mutex); 2311 2312 return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color, 2313 NULL, &mvm->snif_sta, 2314 &mvm->snif_queue, 2315 IWL_MVM_TX_FIFO_BE); 2316 } 2317 2318 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2319 { 2320 int ret; 2321 2322 lockdep_assert_held(&mvm->mutex); 2323 2324 if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA)) 2325 return -EINVAL; 2326 2327 iwl_mvm_disable_txq(mvm, NULL, mvm->snif_sta.sta_id, 2328 &mvm->snif_queue, IWL_MAX_TID_COUNT); 2329 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id); 2330 if (ret) 2331 IWL_WARN(mvm, "Failed sending remove station\n"); 2332 2333 return ret; 2334 } 2335 2336 int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm) 2337 { 2338 int ret; 2339 2340 lockdep_assert_held(&mvm->mutex); 2341 2342 if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA)) 2343 return -EINVAL; 2344 2345 iwl_mvm_disable_txq(mvm, NULL, mvm->aux_sta.sta_id, 2346 &mvm->aux_queue, IWL_MAX_TID_COUNT); 2347 ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id); 2348 if (ret) 2349 IWL_WARN(mvm, "Failed sending remove station\n"); 2350 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta); 2351 2352 return ret; 2353 } 2354 2355 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm) 2356 { 2357 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta); 2358 } 2359 2360 /* 2361 * Send the add station command for the vif's broadcast station. 2362 * Assumes that the station was already allocated. 2363 * 2364 * @mvm: the mvm component 2365 * @vif: the interface to which the broadcast station is added 2366 * @bsta: the broadcast station to add. 2367 */ 2368 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2369 { 2370 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2371 struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta; 2372 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; 2373 const u8 *baddr = _baddr; 2374 int queue; 2375 int ret; 2376 unsigned int wdg_timeout = 2377 iwl_mvm_get_wd_timeout(mvm, vif, false, false); 2378 struct iwl_trans_txq_scd_cfg cfg = { 2379 .fifo = IWL_MVM_TX_FIFO_VO, 2380 .sta_id = mvmvif->deflink.bcast_sta.sta_id, 2381 .tid = IWL_MAX_TID_COUNT, 2382 .aggregate = false, 2383 .frame_limit = IWL_FRAME_LIMIT, 2384 }; 2385 2386 lockdep_assert_held(&mvm->mutex); 2387 2388 if (!iwl_mvm_has_new_tx_api(mvm)) { 2389 if (vif->type == NL80211_IFTYPE_AP || 2390 vif->type == NL80211_IFTYPE_ADHOC) { 2391 queue = mvm->probe_queue; 2392 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) { 2393 queue = mvm->p2p_dev_queue; 2394 } else { 2395 WARN(1, "Missing required TXQ for adding bcast STA\n"); 2396 return -EINVAL; 2397 } 2398 2399 bsta->tfd_queue_msk |= BIT(queue); 2400 2401 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout); 2402 } 2403 2404 if (vif->type == NL80211_IFTYPE_ADHOC) 2405 baddr = vif->bss_conf.bssid; 2406 2407 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA)) 2408 return -ENOSPC; 2409 2410 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr, 2411 mvmvif->id, mvmvif->color); 2412 if (ret) 2413 return ret; 2414 2415 /* 2416 * For 22000 firmware and on we cannot add queue to a station unknown 2417 * to firmware so enable queue here - after the station was added 2418 */ 2419 if (iwl_mvm_has_new_tx_api(mvm)) { 2420 queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, bsta->sta_id, 2421 IWL_MAX_TID_COUNT, 2422 wdg_timeout); 2423 if (queue < 0) { 2424 iwl_mvm_rm_sta_common(mvm, bsta->sta_id); 2425 return queue; 2426 } 2427 2428 if (vif->type == NL80211_IFTYPE_AP || 2429 vif->type == NL80211_IFTYPE_ADHOC) { 2430 /* for queue management */ 2431 mvm->probe_queue = queue; 2432 /* for use in TX */ 2433 mvmvif->deflink.mgmt_queue = queue; 2434 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) { 2435 mvm->p2p_dev_queue = queue; 2436 } 2437 } else if (vif->type == NL80211_IFTYPE_AP || 2438 vif->type == NL80211_IFTYPE_ADHOC) { 2439 /* set it for use in TX */ 2440 mvmvif->deflink.mgmt_queue = mvm->probe_queue; 2441 } 2442 2443 return 0; 2444 } 2445 2446 void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm, 2447 struct ieee80211_vif *vif) 2448 { 2449 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2450 u16 *queueptr, queue; 2451 2452 lockdep_assert_held(&mvm->mutex); 2453 2454 iwl_mvm_flush_sta(mvm, mvmvif->deflink.bcast_sta.sta_id, 2455 mvmvif->deflink.bcast_sta.tfd_queue_msk); 2456 2457 switch (vif->type) { 2458 case NL80211_IFTYPE_AP: 2459 case NL80211_IFTYPE_ADHOC: 2460 queueptr = &mvm->probe_queue; 2461 break; 2462 case NL80211_IFTYPE_P2P_DEVICE: 2463 queueptr = &mvm->p2p_dev_queue; 2464 break; 2465 default: 2466 WARN(1, "Can't free bcast queue on vif type %d\n", 2467 vif->type); 2468 return; 2469 } 2470 2471 queue = *queueptr; 2472 iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.bcast_sta.sta_id, 2473 queueptr, IWL_MAX_TID_COUNT); 2474 2475 if (vif->type == NL80211_IFTYPE_AP || vif->type == NL80211_IFTYPE_ADHOC) 2476 mvmvif->deflink.mgmt_queue = mvm->probe_queue; 2477 2478 if (iwl_mvm_has_new_tx_api(mvm)) 2479 return; 2480 2481 WARN_ON(!(mvmvif->deflink.bcast_sta.tfd_queue_msk & BIT(queue))); 2482 mvmvif->deflink.bcast_sta.tfd_queue_msk &= ~BIT(queue); 2483 } 2484 2485 /* Send the FW a request to remove the station from it's internal data 2486 * structures, but DO NOT remove the entry from the local data structures. */ 2487 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2488 { 2489 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2490 int ret; 2491 2492 lockdep_assert_held(&mvm->mutex); 2493 2494 iwl_mvm_free_bcast_sta_queues(mvm, vif); 2495 2496 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.bcast_sta.sta_id); 2497 if (ret) 2498 IWL_WARN(mvm, "Failed sending remove station\n"); 2499 return ret; 2500 } 2501 2502 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2503 { 2504 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2505 2506 lockdep_assert_held(&mvm->mutex); 2507 2508 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.bcast_sta, 0, 2509 ieee80211_vif_type_p2p(vif), 2510 IWL_STA_GENERAL_PURPOSE); 2511 } 2512 2513 /* Allocate a new station entry for the broadcast station to the given vif, 2514 * and send it to the FW. 2515 * Note that each P2P mac should have its own broadcast station. 2516 * 2517 * @mvm: the mvm component 2518 * @vif: the interface to which the broadcast station is added 2519 * @bsta: the broadcast station to add. */ 2520 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2521 { 2522 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2523 struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta; 2524 int ret; 2525 2526 lockdep_assert_held(&mvm->mutex); 2527 2528 ret = iwl_mvm_alloc_bcast_sta(mvm, vif); 2529 if (ret) 2530 return ret; 2531 2532 ret = iwl_mvm_send_add_bcast_sta(mvm, vif); 2533 2534 if (ret) 2535 iwl_mvm_dealloc_int_sta(mvm, bsta); 2536 2537 return ret; 2538 } 2539 2540 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2541 { 2542 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2543 2544 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.bcast_sta); 2545 } 2546 2547 /* 2548 * Send the FW a request to remove the station from it's internal data 2549 * structures, and in addition remove it from the local data structure. 2550 */ 2551 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2552 { 2553 int ret; 2554 2555 lockdep_assert_held(&mvm->mutex); 2556 2557 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif); 2558 2559 iwl_mvm_dealloc_bcast_sta(mvm, vif); 2560 2561 return ret; 2562 } 2563 2564 /* 2565 * Allocate a new station entry for the multicast station to the given vif, 2566 * and send it to the FW. 2567 * Note that each AP/GO mac should have its own multicast station. 2568 * 2569 * @mvm: the mvm component 2570 * @vif: the interface to which the multicast station is added 2571 */ 2572 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2573 { 2574 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2575 struct iwl_mvm_int_sta *msta = &mvmvif->deflink.mcast_sta; 2576 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00}; 2577 const u8 *maddr = _maddr; 2578 struct iwl_trans_txq_scd_cfg cfg = { 2579 .fifo = vif->type == NL80211_IFTYPE_AP ? 2580 IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE, 2581 .sta_id = msta->sta_id, 2582 .tid = 0, 2583 .aggregate = false, 2584 .frame_limit = IWL_FRAME_LIMIT, 2585 }; 2586 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false); 2587 int ret; 2588 2589 lockdep_assert_held(&mvm->mutex); 2590 2591 if (WARN_ON(vif->type != NL80211_IFTYPE_AP && 2592 vif->type != NL80211_IFTYPE_ADHOC)) 2593 return -EOPNOTSUPP; 2594 2595 /* 2596 * In IBSS, ieee80211_check_queues() sets the cab_queue to be 2597 * invalid, so make sure we use the queue we want. 2598 * Note that this is done here as we want to avoid making DQA 2599 * changes in mac80211 layer. 2600 */ 2601 if (vif->type == NL80211_IFTYPE_ADHOC) 2602 mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE; 2603 2604 /* 2605 * While in previous FWs we had to exclude cab queue from TFD queue 2606 * mask, now it is needed as any other queue. 2607 */ 2608 if (!iwl_mvm_has_new_tx_api(mvm) && 2609 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) { 2610 iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0, 2611 &cfg, 2612 timeout); 2613 msta->tfd_queue_msk |= BIT(mvmvif->deflink.cab_queue); 2614 } 2615 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr, 2616 mvmvif->id, mvmvif->color); 2617 if (ret) 2618 goto err; 2619 2620 /* 2621 * Enable cab queue after the ADD_STA command is sent. 2622 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG 2623 * command with unknown station id, and for FW that doesn't support 2624 * station API since the cab queue is not included in the 2625 * tfd_queue_mask. 2626 */ 2627 if (iwl_mvm_has_new_tx_api(mvm)) { 2628 int queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, msta->sta_id, 2629 0, timeout); 2630 if (queue < 0) { 2631 ret = queue; 2632 goto err; 2633 } 2634 mvmvif->deflink.cab_queue = queue; 2635 } else if (!fw_has_api(&mvm->fw->ucode_capa, 2636 IWL_UCODE_TLV_API_STA_TYPE)) 2637 iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0, 2638 &cfg, 2639 timeout); 2640 2641 return 0; 2642 err: 2643 iwl_mvm_dealloc_int_sta(mvm, msta); 2644 return ret; 2645 } 2646 2647 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id, 2648 struct ieee80211_key_conf *keyconf, 2649 bool mcast) 2650 { 2651 union { 2652 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1; 2653 struct iwl_mvm_add_sta_key_cmd cmd; 2654 } u = {}; 2655 bool new_api = fw_has_api(&mvm->fw->ucode_capa, 2656 IWL_UCODE_TLV_API_TKIP_MIC_KEYS); 2657 __le16 key_flags; 2658 int ret, size; 2659 u32 status; 2660 2661 /* This is a valid situation for GTK removal */ 2662 if (sta_id == IWL_MVM_INVALID_STA) 2663 return 0; 2664 2665 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) & 2666 STA_KEY_FLG_KEYID_MSK); 2667 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP); 2668 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID); 2669 2670 if (mcast) 2671 key_flags |= cpu_to_le16(STA_KEY_MULTICAST); 2672 2673 /* 2674 * The fields assigned here are in the same location at the start 2675 * of the command, so we can do this union trick. 2676 */ 2677 u.cmd.common.key_flags = key_flags; 2678 u.cmd.common.key_offset = keyconf->hw_key_idx; 2679 u.cmd.common.sta_id = sta_id; 2680 2681 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1); 2682 2683 status = ADD_STA_SUCCESS; 2684 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd, 2685 &status); 2686 2687 switch (status) { 2688 case ADD_STA_SUCCESS: 2689 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n"); 2690 break; 2691 default: 2692 ret = -EIO; 2693 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n"); 2694 break; 2695 } 2696 2697 return ret; 2698 } 2699 2700 /* 2701 * Send the FW a request to remove the station from it's internal data 2702 * structures, and in addition remove it from the local data structure. 2703 */ 2704 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 2705 { 2706 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 2707 int ret; 2708 2709 lockdep_assert_held(&mvm->mutex); 2710 2711 iwl_mvm_flush_sta(mvm, mvmvif->deflink.mcast_sta.sta_id, 2712 mvmvif->deflink.mcast_sta.tfd_queue_msk); 2713 2714 iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.mcast_sta.sta_id, 2715 &mvmvif->deflink.cab_queue, 0); 2716 2717 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.mcast_sta.sta_id); 2718 if (ret) 2719 IWL_WARN(mvm, "Failed sending remove station\n"); 2720 2721 return ret; 2722 } 2723 2724 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid) 2725 { 2726 struct iwl_mvm_delba_data notif = { 2727 .baid = baid, 2728 }; 2729 2730 iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true, 2731 ¬if, sizeof(notif)); 2732 }; 2733 2734 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm, 2735 struct iwl_mvm_baid_data *data) 2736 { 2737 int i; 2738 2739 iwl_mvm_sync_rxq_del_ba(mvm, data->baid); 2740 2741 for (i = 0; i < mvm->trans->num_rx_queues; i++) { 2742 int j; 2743 struct iwl_mvm_reorder_buffer *reorder_buf = 2744 &data->reorder_buf[i]; 2745 struct iwl_mvm_reorder_buf_entry *entries = 2746 &data->entries[i * data->entries_per_queue]; 2747 2748 spin_lock_bh(&reorder_buf->lock); 2749 if (likely(!reorder_buf->num_stored)) { 2750 spin_unlock_bh(&reorder_buf->lock); 2751 continue; 2752 } 2753 2754 /* 2755 * This shouldn't happen in regular DELBA since the internal 2756 * delBA notification should trigger a release of all frames in 2757 * the reorder buffer. 2758 */ 2759 WARN_ON(1); 2760 2761 for (j = 0; j < reorder_buf->buf_size; j++) 2762 __skb_queue_purge(&entries[j].frames); 2763 2764 spin_unlock_bh(&reorder_buf->lock); 2765 } 2766 } 2767 2768 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm, 2769 struct iwl_mvm_baid_data *data, 2770 u16 ssn, u16 buf_size) 2771 { 2772 int i; 2773 2774 for (i = 0; i < mvm->trans->num_rx_queues; i++) { 2775 struct iwl_mvm_reorder_buffer *reorder_buf = 2776 &data->reorder_buf[i]; 2777 struct iwl_mvm_reorder_buf_entry *entries = 2778 &data->entries[i * data->entries_per_queue]; 2779 int j; 2780 2781 reorder_buf->num_stored = 0; 2782 reorder_buf->head_sn = ssn; 2783 reorder_buf->buf_size = buf_size; 2784 spin_lock_init(&reorder_buf->lock); 2785 reorder_buf->mvm = mvm; 2786 reorder_buf->queue = i; 2787 reorder_buf->valid = false; 2788 for (j = 0; j < reorder_buf->buf_size; j++) 2789 __skb_queue_head_init(&entries[j].frames); 2790 } 2791 } 2792 2793 static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm, 2794 struct ieee80211_sta *sta, 2795 bool start, int tid, u16 ssn, 2796 u16 buf_size) 2797 { 2798 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 2799 struct iwl_mvm_add_sta_cmd cmd = { 2800 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color), 2801 .sta_id = mvm_sta->deflink.sta_id, 2802 .add_modify = STA_MODE_MODIFY, 2803 }; 2804 u32 status; 2805 int ret; 2806 2807 if (start) { 2808 cmd.add_immediate_ba_tid = tid; 2809 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn); 2810 cmd.rx_ba_window = cpu_to_le16(buf_size); 2811 cmd.modify_mask = STA_MODIFY_ADD_BA_TID; 2812 } else { 2813 cmd.remove_immediate_ba_tid = tid; 2814 cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID; 2815 } 2816 2817 status = ADD_STA_SUCCESS; 2818 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, 2819 iwl_mvm_add_sta_cmd_size(mvm), 2820 &cmd, &status); 2821 if (ret) 2822 return ret; 2823 2824 switch (status & IWL_ADD_STA_STATUS_MASK) { 2825 case ADD_STA_SUCCESS: 2826 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n", 2827 start ? "start" : "stopp"); 2828 if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) && 2829 !(status & IWL_ADD_STA_BAID_VALID_MASK))) 2830 return -EINVAL; 2831 return u32_get_bits(status, IWL_ADD_STA_BAID_MASK); 2832 case ADD_STA_IMMEDIATE_BA_FAILURE: 2833 IWL_WARN(mvm, "RX BA Session refused by fw\n"); 2834 return -ENOSPC; 2835 default: 2836 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n", 2837 start ? "start" : "stopp", status); 2838 return -EIO; 2839 } 2840 } 2841 2842 static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm, 2843 struct ieee80211_sta *sta, 2844 bool start, int tid, u16 ssn, 2845 u16 buf_size, int baid) 2846 { 2847 struct iwl_rx_baid_cfg_cmd cmd = { 2848 .action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) : 2849 cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE), 2850 }; 2851 struct iwl_host_cmd hcmd = { 2852 .id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD), 2853 .flags = CMD_SEND_IN_RFKILL, 2854 .len[0] = sizeof(cmd), 2855 .data[0] = &cmd, 2856 }; 2857 int ret; 2858 2859 BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid)); 2860 2861 if (start) { 2862 cmd.alloc.sta_id_mask = 2863 cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1)); 2864 cmd.alloc.tid = tid; 2865 cmd.alloc.ssn = cpu_to_le16(ssn); 2866 cmd.alloc.win_size = cpu_to_le16(buf_size); 2867 baid = -EIO; 2868 } else if (iwl_fw_lookup_cmd_ver(mvm->fw, hcmd.id, 1) == 1) { 2869 cmd.remove_v1.baid = cpu_to_le32(baid); 2870 BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove)); 2871 } else { 2872 cmd.remove.sta_id_mask = 2873 cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1)); 2874 cmd.remove.tid = cpu_to_le32(tid); 2875 } 2876 2877 ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &baid); 2878 if (ret) 2879 return ret; 2880 2881 if (!start) { 2882 /* ignore firmware baid on remove */ 2883 baid = 0; 2884 } 2885 2886 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n", 2887 start ? "start" : "stopp"); 2888 2889 if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map)) 2890 return -EINVAL; 2891 2892 return baid; 2893 } 2894 2895 static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 2896 bool start, int tid, u16 ssn, u16 buf_size, 2897 int baid) 2898 { 2899 if (fw_has_capa(&mvm->fw->ucode_capa, 2900 IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT)) 2901 return iwl_mvm_fw_baid_op_cmd(mvm, sta, start, 2902 tid, ssn, buf_size, baid); 2903 2904 return iwl_mvm_fw_baid_op_sta(mvm, sta, start, 2905 tid, ssn, buf_size); 2906 } 2907 2908 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 2909 int tid, u16 ssn, bool start, u16 buf_size, u16 timeout) 2910 { 2911 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 2912 struct iwl_mvm_baid_data *baid_data = NULL; 2913 int ret, baid; 2914 u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID : 2915 IWL_MAX_BAID_OLD; 2916 2917 lockdep_assert_held(&mvm->mutex); 2918 2919 if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) { 2920 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n"); 2921 return -ENOSPC; 2922 } 2923 2924 if (iwl_mvm_has_new_rx_api(mvm) && start) { 2925 u32 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]); 2926 2927 /* sparse doesn't like the __align() so don't check */ 2928 #ifndef __CHECKER__ 2929 /* 2930 * The division below will be OK if either the cache line size 2931 * can be divided by the entry size (ALIGN will round up) or if 2932 * if the entry size can be divided by the cache line size, in 2933 * which case the ALIGN() will do nothing. 2934 */ 2935 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) && 2936 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES); 2937 #endif 2938 2939 /* 2940 * Upward align the reorder buffer size to fill an entire cache 2941 * line for each queue, to avoid sharing cache lines between 2942 * different queues. 2943 */ 2944 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES); 2945 2946 /* 2947 * Allocate here so if allocation fails we can bail out early 2948 * before starting the BA session in the firmware 2949 */ 2950 baid_data = kzalloc(sizeof(*baid_data) + 2951 mvm->trans->num_rx_queues * 2952 reorder_buf_size, 2953 GFP_KERNEL); 2954 if (!baid_data) 2955 return -ENOMEM; 2956 2957 /* 2958 * This division is why we need the above BUILD_BUG_ON(), 2959 * if that doesn't hold then this will not be right. 2960 */ 2961 baid_data->entries_per_queue = 2962 reorder_buf_size / sizeof(baid_data->entries[0]); 2963 } 2964 2965 if (iwl_mvm_has_new_rx_api(mvm) && !start) { 2966 baid = mvm_sta->tid_to_baid[tid]; 2967 } else { 2968 /* we don't really need it in this case */ 2969 baid = -1; 2970 } 2971 2972 /* Don't send command to remove (start=0) BAID during restart */ 2973 if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) 2974 baid = iwl_mvm_fw_baid_op(mvm, sta, start, tid, ssn, buf_size, 2975 baid); 2976 2977 if (baid < 0) { 2978 ret = baid; 2979 goto out_free; 2980 } 2981 2982 if (start) { 2983 mvm->rx_ba_sessions++; 2984 2985 if (!iwl_mvm_has_new_rx_api(mvm)) 2986 return 0; 2987 2988 baid_data->baid = baid; 2989 baid_data->timeout = timeout; 2990 baid_data->last_rx = jiffies; 2991 baid_data->rcu_ptr = &mvm->baid_map[baid]; 2992 timer_setup(&baid_data->session_timer, 2993 iwl_mvm_rx_agg_session_expired, 0); 2994 baid_data->mvm = mvm; 2995 baid_data->tid = tid; 2996 baid_data->sta_mask = iwl_mvm_sta_fw_id_mask(mvm, sta, -1); 2997 2998 mvm_sta->tid_to_baid[tid] = baid; 2999 if (timeout) 3000 mod_timer(&baid_data->session_timer, 3001 TU_TO_EXP_TIME(timeout * 2)); 3002 3003 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size); 3004 /* 3005 * protect the BA data with RCU to cover a case where our 3006 * internal RX sync mechanism will timeout (not that it's 3007 * supposed to happen) and we will free the session data while 3008 * RX is being processed in parallel 3009 */ 3010 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n", 3011 mvm_sta->deflink.sta_id, tid, baid); 3012 WARN_ON(rcu_access_pointer(mvm->baid_map[baid])); 3013 rcu_assign_pointer(mvm->baid_map[baid], baid_data); 3014 } else { 3015 baid = mvm_sta->tid_to_baid[tid]; 3016 3017 if (mvm->rx_ba_sessions > 0) 3018 /* check that restart flow didn't zero the counter */ 3019 mvm->rx_ba_sessions--; 3020 if (!iwl_mvm_has_new_rx_api(mvm)) 3021 return 0; 3022 3023 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID)) 3024 return -EINVAL; 3025 3026 baid_data = rcu_access_pointer(mvm->baid_map[baid]); 3027 if (WARN_ON(!baid_data)) 3028 return -EINVAL; 3029 3030 /* synchronize all rx queues so we can safely delete */ 3031 iwl_mvm_free_reorder(mvm, baid_data); 3032 timer_shutdown_sync(&baid_data->session_timer); 3033 RCU_INIT_POINTER(mvm->baid_map[baid], NULL); 3034 kfree_rcu(baid_data, rcu_head); 3035 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid); 3036 } 3037 return 0; 3038 3039 out_free: 3040 kfree(baid_data); 3041 return ret; 3042 } 3043 3044 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta, 3045 int tid, u8 queue, bool start) 3046 { 3047 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 3048 struct iwl_mvm_add_sta_cmd cmd = {}; 3049 int ret; 3050 u32 status; 3051 3052 lockdep_assert_held(&mvm->mutex); 3053 3054 if (start) { 3055 mvm_sta->tfd_queue_msk |= BIT(queue); 3056 mvm_sta->tid_disable_agg &= ~BIT(tid); 3057 } else { 3058 /* In DQA-mode the queue isn't removed on agg termination */ 3059 mvm_sta->tid_disable_agg |= BIT(tid); 3060 } 3061 3062 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color); 3063 cmd.sta_id = mvm_sta->deflink.sta_id; 3064 cmd.add_modify = STA_MODE_MODIFY; 3065 if (!iwl_mvm_has_new_tx_api(mvm)) 3066 cmd.modify_mask = STA_MODIFY_QUEUES; 3067 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX; 3068 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk); 3069 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg); 3070 3071 status = ADD_STA_SUCCESS; 3072 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, 3073 iwl_mvm_add_sta_cmd_size(mvm), 3074 &cmd, &status); 3075 if (ret) 3076 return ret; 3077 3078 switch (status & IWL_ADD_STA_STATUS_MASK) { 3079 case ADD_STA_SUCCESS: 3080 break; 3081 default: 3082 ret = -EIO; 3083 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n", 3084 start ? "start" : "stopp", status); 3085 break; 3086 } 3087 3088 return ret; 3089 } 3090 3091 const u8 tid_to_mac80211_ac[] = { 3092 IEEE80211_AC_BE, 3093 IEEE80211_AC_BK, 3094 IEEE80211_AC_BK, 3095 IEEE80211_AC_BE, 3096 IEEE80211_AC_VI, 3097 IEEE80211_AC_VI, 3098 IEEE80211_AC_VO, 3099 IEEE80211_AC_VO, 3100 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */ 3101 }; 3102 3103 static const u8 tid_to_ucode_ac[] = { 3104 AC_BE, 3105 AC_BK, 3106 AC_BK, 3107 AC_BE, 3108 AC_VI, 3109 AC_VI, 3110 AC_VO, 3111 AC_VO, 3112 }; 3113 3114 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 3115 struct ieee80211_sta *sta, u16 tid, u16 *ssn) 3116 { 3117 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3118 struct iwl_mvm_tid_data *tid_data; 3119 u16 normalized_ssn; 3120 u16 txq_id; 3121 int ret; 3122 3123 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT)) 3124 return -EINVAL; 3125 3126 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED && 3127 mvmsta->tid_data[tid].state != IWL_AGG_OFF) { 3128 IWL_ERR(mvm, 3129 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n", 3130 mvmsta->tid_data[tid].state); 3131 return -ENXIO; 3132 } 3133 3134 lockdep_assert_held(&mvm->mutex); 3135 3136 if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE && 3137 iwl_mvm_has_new_tx_api(mvm)) { 3138 u8 ac = tid_to_mac80211_ac[tid]; 3139 3140 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid); 3141 if (ret) 3142 return ret; 3143 } 3144 3145 spin_lock_bh(&mvmsta->lock); 3146 3147 /* 3148 * Note the possible cases: 3149 * 1. An enabled TXQ - TXQ needs to become agg'ed 3150 * 2. The TXQ hasn't yet been enabled, so find a free one and mark 3151 * it as reserved 3152 */ 3153 txq_id = mvmsta->tid_data[tid].txq_id; 3154 if (txq_id == IWL_MVM_INVALID_QUEUE) { 3155 ret = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id, 3156 IWL_MVM_DQA_MIN_DATA_QUEUE, 3157 IWL_MVM_DQA_MAX_DATA_QUEUE); 3158 if (ret < 0) { 3159 IWL_ERR(mvm, "Failed to allocate agg queue\n"); 3160 goto out; 3161 } 3162 3163 txq_id = ret; 3164 3165 /* TXQ hasn't yet been enabled, so mark it only as reserved */ 3166 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED; 3167 } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) { 3168 ret = -ENXIO; 3169 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n", 3170 tid, IWL_MAX_HW_QUEUES - 1); 3171 goto out; 3172 3173 } else if (unlikely(mvm->queue_info[txq_id].status == 3174 IWL_MVM_QUEUE_SHARED)) { 3175 ret = -ENXIO; 3176 IWL_DEBUG_TX_QUEUES(mvm, 3177 "Can't start tid %d agg on shared queue!\n", 3178 tid); 3179 goto out; 3180 } 3181 3182 IWL_DEBUG_TX_QUEUES(mvm, 3183 "AGG for tid %d will be on queue #%d\n", 3184 tid, txq_id); 3185 3186 tid_data = &mvmsta->tid_data[tid]; 3187 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number); 3188 tid_data->txq_id = txq_id; 3189 *ssn = tid_data->ssn; 3190 3191 IWL_DEBUG_TX_QUEUES(mvm, 3192 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n", 3193 mvmsta->deflink.sta_id, tid, txq_id, 3194 tid_data->ssn, 3195 tid_data->next_reclaimed); 3196 3197 /* 3198 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need 3199 * to align the wrap around of ssn so we compare relevant values. 3200 */ 3201 normalized_ssn = tid_data->ssn; 3202 if (mvm->trans->trans_cfg->gen2) 3203 normalized_ssn &= 0xff; 3204 3205 if (normalized_ssn == tid_data->next_reclaimed) { 3206 tid_data->state = IWL_AGG_STARTING; 3207 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE; 3208 } else { 3209 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA; 3210 ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA; 3211 } 3212 3213 out: 3214 spin_unlock_bh(&mvmsta->lock); 3215 3216 return ret; 3217 } 3218 3219 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 3220 struct ieee80211_sta *sta, u16 tid, u16 buf_size, 3221 bool amsdu) 3222 { 3223 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3224 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid]; 3225 unsigned int wdg_timeout = 3226 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false); 3227 int queue, ret; 3228 bool alloc_queue = true; 3229 enum iwl_mvm_queue_status queue_status; 3230 u16 ssn; 3231 3232 struct iwl_trans_txq_scd_cfg cfg = { 3233 .sta_id = mvmsta->deflink.sta_id, 3234 .tid = tid, 3235 .frame_limit = buf_size, 3236 .aggregate = true, 3237 }; 3238 3239 /* 3240 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation 3241 * manager, so this function should never be called in this case. 3242 */ 3243 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm))) 3244 return -EINVAL; 3245 3246 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE) 3247 != IWL_MAX_TID_COUNT); 3248 3249 spin_lock_bh(&mvmsta->lock); 3250 ssn = tid_data->ssn; 3251 queue = tid_data->txq_id; 3252 tid_data->state = IWL_AGG_ON; 3253 mvmsta->agg_tids |= BIT(tid); 3254 tid_data->ssn = 0xffff; 3255 tid_data->amsdu_in_ampdu_allowed = amsdu; 3256 spin_unlock_bh(&mvmsta->lock); 3257 3258 if (iwl_mvm_has_new_tx_api(mvm)) { 3259 /* 3260 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start() 3261 * would have failed, so if we are here there is no need to 3262 * allocate a queue. 3263 * However, if aggregation size is different than the default 3264 * size, the scheduler should be reconfigured. 3265 * We cannot do this with the new TX API, so return unsupported 3266 * for now, until it will be offloaded to firmware.. 3267 * Note that if SCD default value changes - this condition 3268 * should be updated as well. 3269 */ 3270 if (buf_size < IWL_FRAME_LIMIT) 3271 return -EOPNOTSUPP; 3272 3273 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true); 3274 if (ret) 3275 return -EIO; 3276 goto out; 3277 } 3278 3279 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]]; 3280 3281 queue_status = mvm->queue_info[queue].status; 3282 3283 /* Maybe there is no need to even alloc a queue... */ 3284 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY) 3285 alloc_queue = false; 3286 3287 /* 3288 * Only reconfig the SCD for the queue if the window size has 3289 * changed from current (become smaller) 3290 */ 3291 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) { 3292 /* 3293 * If reconfiguring an existing queue, it first must be 3294 * drained 3295 */ 3296 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, 3297 BIT(queue)); 3298 if (ret) { 3299 IWL_ERR(mvm, 3300 "Error draining queue before reconfig\n"); 3301 return ret; 3302 } 3303 3304 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo, 3305 mvmsta->deflink.sta_id, tid, 3306 buf_size, ssn); 3307 if (ret) { 3308 IWL_ERR(mvm, 3309 "Error reconfiguring TXQ #%d\n", queue); 3310 return ret; 3311 } 3312 } 3313 3314 if (alloc_queue) 3315 iwl_mvm_enable_txq(mvm, sta, queue, ssn, 3316 &cfg, wdg_timeout); 3317 3318 /* Send ADD_STA command to enable aggs only if the queue isn't shared */ 3319 if (queue_status != IWL_MVM_QUEUE_SHARED) { 3320 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true); 3321 if (ret) 3322 return -EIO; 3323 } 3324 3325 /* No need to mark as reserved */ 3326 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY; 3327 3328 out: 3329 /* 3330 * Even though in theory the peer could have different 3331 * aggregation reorder buffer sizes for different sessions, 3332 * our ucode doesn't allow for that and has a global limit 3333 * for each station. Therefore, use the minimum of all the 3334 * aggregation sessions and our default value. 3335 */ 3336 mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize = 3337 min(mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize, 3338 buf_size); 3339 mvmsta->deflink.lq_sta.rs_drv.lq.agg_frame_cnt_limit = 3340 mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize; 3341 3342 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n", 3343 sta->addr, tid); 3344 3345 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->deflink.lq_sta.rs_drv.lq); 3346 } 3347 3348 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm, 3349 struct iwl_mvm_sta *mvmsta, 3350 struct iwl_mvm_tid_data *tid_data) 3351 { 3352 u16 txq_id = tid_data->txq_id; 3353 3354 lockdep_assert_held(&mvm->mutex); 3355 3356 if (iwl_mvm_has_new_tx_api(mvm)) 3357 return; 3358 3359 /* 3360 * The TXQ is marked as reserved only if no traffic came through yet 3361 * This means no traffic has been sent on this TID (agg'd or not), so 3362 * we no longer have use for the queue. Since it hasn't even been 3363 * allocated through iwl_mvm_enable_txq, so we can just mark it back as 3364 * free. 3365 */ 3366 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) { 3367 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE; 3368 tid_data->txq_id = IWL_MVM_INVALID_QUEUE; 3369 } 3370 } 3371 3372 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 3373 struct ieee80211_sta *sta, u16 tid) 3374 { 3375 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3376 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid]; 3377 u16 txq_id; 3378 int err; 3379 3380 /* 3381 * If mac80211 is cleaning its state, then say that we finished since 3382 * our state has been cleared anyway. 3383 */ 3384 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) { 3385 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 3386 return 0; 3387 } 3388 3389 spin_lock_bh(&mvmsta->lock); 3390 3391 txq_id = tid_data->txq_id; 3392 3393 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n", 3394 mvmsta->deflink.sta_id, tid, txq_id, 3395 tid_data->state); 3396 3397 mvmsta->agg_tids &= ~BIT(tid); 3398 3399 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data); 3400 3401 switch (tid_data->state) { 3402 case IWL_AGG_ON: 3403 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number); 3404 3405 IWL_DEBUG_TX_QUEUES(mvm, 3406 "ssn = %d, next_recl = %d\n", 3407 tid_data->ssn, tid_data->next_reclaimed); 3408 3409 tid_data->ssn = 0xffff; 3410 tid_data->state = IWL_AGG_OFF; 3411 spin_unlock_bh(&mvmsta->lock); 3412 3413 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 3414 3415 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false); 3416 return 0; 3417 case IWL_AGG_STARTING: 3418 case IWL_EMPTYING_HW_QUEUE_ADDBA: 3419 /* 3420 * The agg session has been stopped before it was set up. This 3421 * can happen when the AddBA timer times out for example. 3422 */ 3423 3424 /* No barriers since we are under mutex */ 3425 lockdep_assert_held(&mvm->mutex); 3426 3427 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid); 3428 tid_data->state = IWL_AGG_OFF; 3429 err = 0; 3430 break; 3431 default: 3432 IWL_ERR(mvm, 3433 "Stopping AGG while state not ON or starting for %d on %d (%d)\n", 3434 mvmsta->deflink.sta_id, tid, tid_data->state); 3435 IWL_ERR(mvm, 3436 "\ttid_data->txq_id = %d\n", tid_data->txq_id); 3437 err = -EINVAL; 3438 } 3439 3440 spin_unlock_bh(&mvmsta->lock); 3441 3442 return err; 3443 } 3444 3445 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 3446 struct ieee80211_sta *sta, u16 tid) 3447 { 3448 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 3449 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid]; 3450 u16 txq_id; 3451 enum iwl_mvm_agg_state old_state; 3452 3453 /* 3454 * First set the agg state to OFF to avoid calling 3455 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty. 3456 */ 3457 spin_lock_bh(&mvmsta->lock); 3458 txq_id = tid_data->txq_id; 3459 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n", 3460 mvmsta->deflink.sta_id, tid, txq_id, 3461 tid_data->state); 3462 old_state = tid_data->state; 3463 tid_data->state = IWL_AGG_OFF; 3464 mvmsta->agg_tids &= ~BIT(tid); 3465 spin_unlock_bh(&mvmsta->lock); 3466 3467 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data); 3468 3469 if (old_state >= IWL_AGG_ON) { 3470 iwl_mvm_drain_sta(mvm, mvmsta, true); 3471 3472 if (iwl_mvm_has_new_tx_api(mvm)) { 3473 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->deflink.sta_id, 3474 BIT(tid))) 3475 IWL_ERR(mvm, "Couldn't flush the AGG queue\n"); 3476 iwl_trans_wait_txq_empty(mvm->trans, txq_id); 3477 } else { 3478 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id))) 3479 IWL_ERR(mvm, "Couldn't flush the AGG queue\n"); 3480 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id)); 3481 } 3482 3483 iwl_mvm_drain_sta(mvm, mvmsta, false); 3484 3485 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false); 3486 } 3487 3488 return 0; 3489 } 3490 3491 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm) 3492 { 3493 int i, max = -1, max_offs = -1; 3494 3495 lockdep_assert_held(&mvm->mutex); 3496 3497 /* Pick the unused key offset with the highest 'deleted' 3498 * counter. Every time a key is deleted, all the counters 3499 * are incremented and the one that was just deleted is 3500 * reset to zero. Thus, the highest counter is the one 3501 * that was deleted longest ago. Pick that one. 3502 */ 3503 for (i = 0; i < STA_KEY_MAX_NUM; i++) { 3504 if (test_bit(i, mvm->fw_key_table)) 3505 continue; 3506 if (mvm->fw_key_deleted[i] > max) { 3507 max = mvm->fw_key_deleted[i]; 3508 max_offs = i; 3509 } 3510 } 3511 3512 if (max_offs < 0) 3513 return STA_KEY_IDX_INVALID; 3514 3515 return max_offs; 3516 } 3517 3518 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm, 3519 struct ieee80211_vif *vif, 3520 struct ieee80211_sta *sta) 3521 { 3522 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 3523 3524 if (sta) 3525 return iwl_mvm_sta_from_mac80211(sta); 3526 3527 /* 3528 * The device expects GTKs for station interfaces to be 3529 * installed as GTKs for the AP station. If we have no 3530 * station ID, then use AP's station ID. 3531 */ 3532 if (vif->type == NL80211_IFTYPE_STATION && 3533 mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) { 3534 u8 sta_id = mvmvif->deflink.ap_sta_id; 3535 3536 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id], 3537 lockdep_is_held(&mvm->mutex)); 3538 3539 /* 3540 * It is possible that the 'sta' parameter is NULL, 3541 * for example when a GTK is removed - the sta_id will then 3542 * be the AP ID, and no station was passed by mac80211. 3543 */ 3544 if (IS_ERR_OR_NULL(sta)) 3545 return NULL; 3546 3547 return iwl_mvm_sta_from_mac80211(sta); 3548 } 3549 3550 return NULL; 3551 } 3552 3553 static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len) 3554 { 3555 int i; 3556 3557 for (i = len - 1; i >= 0; i--) { 3558 if (pn1[i] > pn2[i]) 3559 return 1; 3560 if (pn1[i] < pn2[i]) 3561 return -1; 3562 } 3563 3564 return 0; 3565 } 3566 3567 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm, 3568 u32 sta_id, 3569 struct ieee80211_key_conf *key, bool mcast, 3570 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags, 3571 u8 key_offset, bool mfp) 3572 { 3573 union { 3574 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1; 3575 struct iwl_mvm_add_sta_key_cmd cmd; 3576 } u = {}; 3577 __le16 key_flags; 3578 int ret; 3579 u32 status; 3580 u16 keyidx; 3581 u64 pn = 0; 3582 int i, size; 3583 bool new_api = fw_has_api(&mvm->fw->ucode_capa, 3584 IWL_UCODE_TLV_API_TKIP_MIC_KEYS); 3585 int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY, 3586 new_api ? 2 : 1); 3587 3588 if (sta_id == IWL_MVM_INVALID_STA) 3589 return -EINVAL; 3590 3591 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) & 3592 STA_KEY_FLG_KEYID_MSK; 3593 key_flags = cpu_to_le16(keyidx); 3594 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP); 3595 3596 if (key->flags & IEEE80211_KEY_FLAG_SPP_AMSDU) 3597 key_flags |= cpu_to_le16(STA_KEY_FLG_AMSDU_SPP); 3598 3599 switch (key->cipher) { 3600 case WLAN_CIPHER_SUITE_TKIP: 3601 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP); 3602 if (api_ver >= 2) { 3603 memcpy((void *)&u.cmd.tx_mic_key, 3604 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY], 3605 IWL_MIC_KEY_SIZE); 3606 3607 memcpy((void *)&u.cmd.rx_mic_key, 3608 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY], 3609 IWL_MIC_KEY_SIZE); 3610 pn = atomic64_read(&key->tx_pn); 3611 3612 } else { 3613 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32; 3614 for (i = 0; i < 5; i++) 3615 u.cmd_v1.tkip_rx_ttak[i] = 3616 cpu_to_le16(tkip_p1k[i]); 3617 } 3618 memcpy(u.cmd.common.key, key->key, key->keylen); 3619 break; 3620 case WLAN_CIPHER_SUITE_CCMP: 3621 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM); 3622 memcpy(u.cmd.common.key, key->key, key->keylen); 3623 if (api_ver >= 2) 3624 pn = atomic64_read(&key->tx_pn); 3625 break; 3626 case WLAN_CIPHER_SUITE_WEP104: 3627 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES); 3628 fallthrough; 3629 case WLAN_CIPHER_SUITE_WEP40: 3630 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP); 3631 memcpy(u.cmd.common.key + 3, key->key, key->keylen); 3632 break; 3633 case WLAN_CIPHER_SUITE_GCMP_256: 3634 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES); 3635 fallthrough; 3636 case WLAN_CIPHER_SUITE_GCMP: 3637 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP); 3638 memcpy(u.cmd.common.key, key->key, key->keylen); 3639 if (api_ver >= 2) 3640 pn = atomic64_read(&key->tx_pn); 3641 break; 3642 default: 3643 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT); 3644 memcpy(u.cmd.common.key, key->key, key->keylen); 3645 } 3646 3647 if (mcast) 3648 key_flags |= cpu_to_le16(STA_KEY_MULTICAST); 3649 if (mfp) 3650 key_flags |= cpu_to_le16(STA_KEY_MFP); 3651 3652 u.cmd.common.key_offset = key_offset; 3653 u.cmd.common.key_flags = key_flags; 3654 u.cmd.common.sta_id = sta_id; 3655 3656 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) 3657 i = 0; 3658 else 3659 i = -1; 3660 3661 for (; i < IEEE80211_NUM_TIDS; i++) { 3662 struct ieee80211_key_seq seq = {}; 3663 u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn; 3664 int rx_pn_len = 8; 3665 /* there's a hole at 2/3 in FW format depending on version */ 3666 int hole = api_ver >= 3 ? 0 : 2; 3667 3668 ieee80211_get_key_rx_seq(key, i, &seq); 3669 3670 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) { 3671 rx_pn[0] = seq.tkip.iv16; 3672 rx_pn[1] = seq.tkip.iv16 >> 8; 3673 rx_pn[2 + hole] = seq.tkip.iv32; 3674 rx_pn[3 + hole] = seq.tkip.iv32 >> 8; 3675 rx_pn[4 + hole] = seq.tkip.iv32 >> 16; 3676 rx_pn[5 + hole] = seq.tkip.iv32 >> 24; 3677 } else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) { 3678 rx_pn = seq.hw.seq; 3679 rx_pn_len = seq.hw.seq_len; 3680 } else { 3681 rx_pn[0] = seq.ccmp.pn[0]; 3682 rx_pn[1] = seq.ccmp.pn[1]; 3683 rx_pn[2 + hole] = seq.ccmp.pn[2]; 3684 rx_pn[3 + hole] = seq.ccmp.pn[3]; 3685 rx_pn[4 + hole] = seq.ccmp.pn[4]; 3686 rx_pn[5 + hole] = seq.ccmp.pn[5]; 3687 } 3688 3689 if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt, 3690 rx_pn_len) > 0) 3691 memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn, 3692 rx_pn_len); 3693 } 3694 3695 if (api_ver >= 2) { 3696 u.cmd.transmit_seq_cnt = cpu_to_le64(pn); 3697 size = sizeof(u.cmd); 3698 } else { 3699 size = sizeof(u.cmd_v1); 3700 } 3701 3702 status = ADD_STA_SUCCESS; 3703 if (cmd_flags & CMD_ASYNC) 3704 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size, 3705 &u.cmd); 3706 else 3707 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, 3708 &u.cmd, &status); 3709 3710 switch (status) { 3711 case ADD_STA_SUCCESS: 3712 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n"); 3713 break; 3714 default: 3715 ret = -EIO; 3716 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n"); 3717 break; 3718 } 3719 3720 return ret; 3721 } 3722 3723 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm, 3724 struct ieee80211_key_conf *keyconf, 3725 u8 sta_id, bool remove_key) 3726 { 3727 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {}; 3728 3729 /* verify the key details match the required command's expectations */ 3730 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) || 3731 (keyconf->keyidx != 4 && keyconf->keyidx != 5 && 3732 keyconf->keyidx != 6 && keyconf->keyidx != 7) || 3733 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC && 3734 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 && 3735 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256))) 3736 return -EINVAL; 3737 3738 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) && 3739 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC)) 3740 return -EINVAL; 3741 3742 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx); 3743 igtk_cmd.sta_id = cpu_to_le32(sta_id); 3744 3745 if (remove_key) { 3746 /* This is a valid situation for IGTK */ 3747 if (sta_id == IWL_MVM_INVALID_STA) 3748 return 0; 3749 3750 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID); 3751 } else { 3752 struct ieee80211_key_seq seq; 3753 const u8 *pn; 3754 3755 switch (keyconf->cipher) { 3756 case WLAN_CIPHER_SUITE_AES_CMAC: 3757 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM); 3758 break; 3759 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 3760 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 3761 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP); 3762 break; 3763 default: 3764 return -EINVAL; 3765 } 3766 3767 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen); 3768 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) 3769 igtk_cmd.ctrl_flags |= 3770 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES); 3771 ieee80211_get_key_rx_seq(keyconf, 0, &seq); 3772 pn = seq.aes_cmac.pn; 3773 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) | 3774 ((u64) pn[4] << 8) | 3775 ((u64) pn[3] << 16) | 3776 ((u64) pn[2] << 24) | 3777 ((u64) pn[1] << 32) | 3778 ((u64) pn[0] << 40)); 3779 } 3780 3781 IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n", 3782 remove_key ? "removing" : "installing", 3783 keyconf->keyidx >= 6 ? "B" : "", 3784 keyconf->keyidx, igtk_cmd.sta_id); 3785 3786 if (!iwl_mvm_has_new_rx_api(mvm)) { 3787 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = { 3788 .ctrl_flags = igtk_cmd.ctrl_flags, 3789 .key_id = igtk_cmd.key_id, 3790 .sta_id = igtk_cmd.sta_id, 3791 .receive_seq_cnt = igtk_cmd.receive_seq_cnt 3792 }; 3793 3794 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk, 3795 ARRAY_SIZE(igtk_cmd_v1.igtk)); 3796 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0, 3797 sizeof(igtk_cmd_v1), &igtk_cmd_v1); 3798 } 3799 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0, 3800 sizeof(igtk_cmd), &igtk_cmd); 3801 } 3802 3803 3804 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm, 3805 struct ieee80211_vif *vif, 3806 struct ieee80211_sta *sta) 3807 { 3808 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 3809 3810 if (sta) 3811 return sta->addr; 3812 3813 if (vif->type == NL80211_IFTYPE_STATION && 3814 mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) { 3815 u8 sta_id = mvmvif->deflink.ap_sta_id; 3816 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id], 3817 lockdep_is_held(&mvm->mutex)); 3818 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) 3819 return NULL; 3820 3821 return sta->addr; 3822 } 3823 3824 3825 return NULL; 3826 } 3827 3828 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm, 3829 struct ieee80211_vif *vif, 3830 struct ieee80211_sta *sta, 3831 struct ieee80211_key_conf *keyconf, 3832 u8 key_offset, 3833 bool mcast) 3834 { 3835 const u8 *addr; 3836 struct ieee80211_key_seq seq; 3837 u16 p1k[5]; 3838 u32 sta_id; 3839 bool mfp = false; 3840 3841 if (sta) { 3842 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 3843 3844 sta_id = mvm_sta->deflink.sta_id; 3845 mfp = sta->mfp; 3846 } else if (vif->type == NL80211_IFTYPE_AP && 3847 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) { 3848 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 3849 3850 sta_id = mvmvif->deflink.mcast_sta.sta_id; 3851 } else { 3852 IWL_ERR(mvm, "Failed to find station id\n"); 3853 return -EINVAL; 3854 } 3855 3856 if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) { 3857 addr = iwl_mvm_get_mac_addr(mvm, vif, sta); 3858 if (!addr) { 3859 IWL_ERR(mvm, "Failed to find mac address\n"); 3860 return -EINVAL; 3861 } 3862 3863 /* get phase 1 key from mac80211 */ 3864 ieee80211_get_key_rx_seq(keyconf, 0, &seq); 3865 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k); 3866 3867 return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast, 3868 seq.tkip.iv32, p1k, 0, key_offset, 3869 mfp); 3870 } 3871 3872 return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast, 3873 0, NULL, 0, key_offset, mfp); 3874 } 3875 3876 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm, 3877 struct ieee80211_vif *vif, 3878 struct ieee80211_sta *sta, 3879 struct ieee80211_key_conf *keyconf, 3880 u8 key_offset) 3881 { 3882 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE); 3883 struct iwl_mvm_sta *mvm_sta; 3884 u8 sta_id = IWL_MVM_INVALID_STA; 3885 int ret; 3886 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0}; 3887 3888 lockdep_assert_held(&mvm->mutex); 3889 3890 if (vif->type != NL80211_IFTYPE_AP || 3891 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) { 3892 /* Get the station id from the mvm local station table */ 3893 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta); 3894 if (!mvm_sta) { 3895 IWL_ERR(mvm, "Failed to find station\n"); 3896 return -EINVAL; 3897 } 3898 sta_id = mvm_sta->deflink.sta_id; 3899 3900 /* 3901 * It is possible that the 'sta' parameter is NULL, and thus 3902 * there is a need to retrieve the sta from the local station 3903 * table. 3904 */ 3905 if (!sta) { 3906 sta = rcu_dereference_protected( 3907 mvm->fw_id_to_mac_id[sta_id], 3908 lockdep_is_held(&mvm->mutex)); 3909 if (IS_ERR_OR_NULL(sta)) { 3910 IWL_ERR(mvm, "Invalid station id\n"); 3911 return -EINVAL; 3912 } 3913 } 3914 3915 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif)) 3916 return -EINVAL; 3917 } else { 3918 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 3919 3920 sta_id = mvmvif->deflink.mcast_sta.sta_id; 3921 } 3922 3923 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC || 3924 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 || 3925 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) { 3926 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false); 3927 goto end; 3928 } 3929 3930 /* If the key_offset is not pre-assigned, we need to find a 3931 * new offset to use. In normal cases, the offset is not 3932 * pre-assigned, but during HW_RESTART we want to reuse the 3933 * same indices, so we pass them when this function is called. 3934 * 3935 * In D3 entry, we need to hardcoded the indices (because the 3936 * firmware hardcodes the PTK offset to 0). In this case, we 3937 * need to make sure we don't overwrite the hw_key_idx in the 3938 * keyconf structure, because otherwise we cannot configure 3939 * the original ones back when resuming. 3940 */ 3941 if (key_offset == STA_KEY_IDX_INVALID) { 3942 key_offset = iwl_mvm_set_fw_key_idx(mvm); 3943 if (key_offset == STA_KEY_IDX_INVALID) 3944 return -ENOSPC; 3945 keyconf->hw_key_idx = key_offset; 3946 } 3947 3948 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast); 3949 if (ret) 3950 goto end; 3951 3952 /* 3953 * For WEP, the same key is used for multicast and unicast. Upload it 3954 * again, using the same key offset, and now pointing the other one 3955 * to the same key slot (offset). 3956 * If this fails, remove the original as well. 3957 */ 3958 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 || 3959 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) && 3960 sta) { 3961 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, 3962 key_offset, !mcast); 3963 if (ret) { 3964 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast); 3965 goto end; 3966 } 3967 } 3968 3969 __set_bit(key_offset, mvm->fw_key_table); 3970 3971 end: 3972 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n", 3973 keyconf->cipher, keyconf->keylen, keyconf->keyidx, 3974 sta ? sta->addr : zero_addr, ret); 3975 return ret; 3976 } 3977 3978 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, 3979 struct ieee80211_vif *vif, 3980 struct ieee80211_sta *sta, 3981 struct ieee80211_key_conf *keyconf) 3982 { 3983 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE); 3984 struct iwl_mvm_sta *mvm_sta; 3985 u8 sta_id = IWL_MVM_INVALID_STA; 3986 int ret, i; 3987 3988 lockdep_assert_held(&mvm->mutex); 3989 3990 /* Get the station from the mvm local station table */ 3991 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta); 3992 if (mvm_sta) 3993 sta_id = mvm_sta->deflink.sta_id; 3994 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast) 3995 sta_id = iwl_mvm_vif_from_mac80211(vif)->deflink.mcast_sta.sta_id; 3996 3997 3998 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n", 3999 keyconf->keyidx, sta_id); 4000 4001 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC || 4002 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 || 4003 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) 4004 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true); 4005 4006 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) { 4007 IWL_ERR(mvm, "offset %d not used in fw key table.\n", 4008 keyconf->hw_key_idx); 4009 return -ENOENT; 4010 } 4011 4012 /* track which key was deleted last */ 4013 for (i = 0; i < STA_KEY_MAX_NUM; i++) { 4014 if (mvm->fw_key_deleted[i] < U8_MAX) 4015 mvm->fw_key_deleted[i]++; 4016 } 4017 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0; 4018 4019 if (sta && !mvm_sta) { 4020 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n"); 4021 return 0; 4022 } 4023 4024 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast); 4025 if (ret) 4026 return ret; 4027 4028 /* delete WEP key twice to get rid of (now useless) offset */ 4029 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 || 4030 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) 4031 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast); 4032 4033 return ret; 4034 } 4035 4036 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm, 4037 struct ieee80211_vif *vif, 4038 struct ieee80211_key_conf *keyconf, 4039 struct ieee80211_sta *sta, u32 iv32, 4040 u16 *phase1key) 4041 { 4042 struct iwl_mvm_sta *mvm_sta; 4043 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE); 4044 bool mfp = sta ? sta->mfp : false; 4045 4046 rcu_read_lock(); 4047 4048 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta); 4049 if (WARN_ON_ONCE(!mvm_sta)) 4050 goto unlock; 4051 iwl_mvm_send_sta_key(mvm, mvm_sta->deflink.sta_id, keyconf, mcast, 4052 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx, 4053 mfp); 4054 4055 unlock: 4056 rcu_read_unlock(); 4057 } 4058 4059 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm, 4060 struct ieee80211_sta *sta) 4061 { 4062 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 4063 struct iwl_mvm_add_sta_cmd cmd = { 4064 .add_modify = STA_MODE_MODIFY, 4065 .sta_id = mvmsta->deflink.sta_id, 4066 .station_flags_msk = cpu_to_le32(STA_FLG_PS), 4067 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color), 4068 }; 4069 int ret; 4070 4071 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, 4072 iwl_mvm_add_sta_cmd_size(mvm), &cmd); 4073 if (ret) 4074 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret); 4075 } 4076 4077 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm, 4078 struct ieee80211_sta *sta, 4079 enum ieee80211_frame_release_type reason, 4080 u16 cnt, u16 tids, bool more_data, 4081 bool single_sta_queue) 4082 { 4083 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 4084 struct iwl_mvm_add_sta_cmd cmd = { 4085 .add_modify = STA_MODE_MODIFY, 4086 .sta_id = mvmsta->deflink.sta_id, 4087 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT, 4088 .sleep_tx_count = cpu_to_le16(cnt), 4089 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color), 4090 }; 4091 int tid, ret; 4092 unsigned long _tids = tids; 4093 4094 /* convert TIDs to ACs - we don't support TSPEC so that's OK 4095 * Note that this field is reserved and unused by firmware not 4096 * supporting GO uAPSD, so it's safe to always do this. 4097 */ 4098 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) 4099 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]); 4100 4101 /* If we're releasing frames from aggregation or dqa queues then check 4102 * if all the queues that we're releasing frames from, combined, have: 4103 * - more frames than the service period, in which case more_data 4104 * needs to be set 4105 * - fewer than 'cnt' frames, in which case we need to adjust the 4106 * firmware command (but do that unconditionally) 4107 */ 4108 if (single_sta_queue) { 4109 int remaining = cnt; 4110 int sleep_tx_count; 4111 4112 spin_lock_bh(&mvmsta->lock); 4113 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) { 4114 struct iwl_mvm_tid_data *tid_data; 4115 u16 n_queued; 4116 4117 tid_data = &mvmsta->tid_data[tid]; 4118 4119 n_queued = iwl_mvm_tid_queued(mvm, tid_data); 4120 if (n_queued > remaining) { 4121 more_data = true; 4122 remaining = 0; 4123 break; 4124 } 4125 remaining -= n_queued; 4126 } 4127 sleep_tx_count = cnt - remaining; 4128 if (reason == IEEE80211_FRAME_RELEASE_UAPSD) 4129 mvmsta->sleep_tx_count = sleep_tx_count; 4130 spin_unlock_bh(&mvmsta->lock); 4131 4132 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count); 4133 if (WARN_ON(cnt - remaining == 0)) { 4134 ieee80211_sta_eosp(sta); 4135 return; 4136 } 4137 } 4138 4139 /* Note: this is ignored by firmware not supporting GO uAPSD */ 4140 if (more_data) 4141 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA; 4142 4143 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) { 4144 mvmsta->next_status_eosp = true; 4145 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL; 4146 } else { 4147 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD; 4148 } 4149 4150 /* block the Tx queues until the FW updated the sleep Tx count */ 4151 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 4152 CMD_ASYNC | CMD_BLOCK_TXQS, 4153 iwl_mvm_add_sta_cmd_size(mvm), &cmd); 4154 if (ret) 4155 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret); 4156 } 4157 4158 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm, 4159 struct iwl_rx_cmd_buffer *rxb) 4160 { 4161 struct iwl_rx_packet *pkt = rxb_addr(rxb); 4162 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data; 4163 struct ieee80211_sta *sta; 4164 u32 sta_id = le32_to_cpu(notif->sta_id); 4165 4166 if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations)) 4167 return; 4168 4169 rcu_read_lock(); 4170 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]); 4171 if (!IS_ERR_OR_NULL(sta)) 4172 ieee80211_sta_eosp(sta); 4173 rcu_read_unlock(); 4174 } 4175 4176 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm, 4177 struct iwl_mvm_sta *mvmsta, 4178 bool disable) 4179 { 4180 struct iwl_mvm_add_sta_cmd cmd = { 4181 .add_modify = STA_MODE_MODIFY, 4182 .sta_id = mvmsta->deflink.sta_id, 4183 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0, 4184 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX), 4185 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color), 4186 }; 4187 int ret; 4188 4189 if (mvm->mld_api_is_used) { 4190 if (!iwl_mvm_has_no_host_disable_tx(mvm)) 4191 iwl_mvm_mld_sta_modify_disable_tx(mvm, mvmsta, disable); 4192 return; 4193 } 4194 4195 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, 4196 iwl_mvm_add_sta_cmd_size(mvm), &cmd); 4197 if (ret) 4198 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret); 4199 } 4200 4201 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm, 4202 struct ieee80211_sta *sta, 4203 bool disable) 4204 { 4205 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta); 4206 4207 if (mvm->mld_api_is_used) { 4208 if (!iwl_mvm_has_no_host_disable_tx(mvm)) 4209 iwl_mvm_mld_sta_modify_disable_tx_ap(mvm, sta, disable); 4210 return; 4211 } 4212 4213 spin_lock_bh(&mvm_sta->lock); 4214 4215 if (mvm_sta->disable_tx == disable) { 4216 spin_unlock_bh(&mvm_sta->lock); 4217 return; 4218 } 4219 4220 mvm_sta->disable_tx = disable; 4221 4222 /* 4223 * If sta PS state is handled by mac80211, tell it to start/stop 4224 * queuing tx for this station. 4225 */ 4226 if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS)) 4227 ieee80211_sta_block_awake(mvm->hw, sta, disable); 4228 4229 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable); 4230 4231 spin_unlock_bh(&mvm_sta->lock); 4232 } 4233 4234 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm, 4235 struct iwl_mvm_vif *mvmvif, 4236 struct iwl_mvm_int_sta *sta, 4237 bool disable) 4238 { 4239 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color); 4240 struct iwl_mvm_add_sta_cmd cmd = { 4241 .add_modify = STA_MODE_MODIFY, 4242 .sta_id = sta->sta_id, 4243 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0, 4244 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX), 4245 .mac_id_n_color = cpu_to_le32(id), 4246 }; 4247 int ret; 4248 4249 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, 4250 iwl_mvm_add_sta_cmd_size(mvm), &cmd); 4251 if (ret) 4252 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret); 4253 } 4254 4255 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm, 4256 struct iwl_mvm_vif *mvmvif, 4257 bool disable) 4258 { 4259 struct ieee80211_sta *sta; 4260 struct iwl_mvm_sta *mvm_sta; 4261 int i; 4262 4263 if (mvm->mld_api_is_used) { 4264 if (!iwl_mvm_has_no_host_disable_tx(mvm)) 4265 iwl_mvm_mld_modify_all_sta_disable_tx(mvm, mvmvif, 4266 disable); 4267 return; 4268 } 4269 4270 rcu_read_lock(); 4271 4272 /* Block/unblock all the stations of the given mvmvif */ 4273 for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) { 4274 sta = rcu_dereference(mvm->fw_id_to_mac_id[i]); 4275 if (IS_ERR_OR_NULL(sta)) 4276 continue; 4277 4278 mvm_sta = iwl_mvm_sta_from_mac80211(sta); 4279 if (mvm_sta->mac_id_n_color != 4280 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color)) 4281 continue; 4282 4283 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable); 4284 } 4285 4286 rcu_read_unlock(); 4287 4288 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) 4289 return; 4290 4291 /* Need to block/unblock also multicast station */ 4292 if (mvmvif->deflink.mcast_sta.sta_id != IWL_MVM_INVALID_STA) 4293 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif, 4294 &mvmvif->deflink.mcast_sta, 4295 disable); 4296 4297 /* 4298 * Only unblock the broadcast station (FW blocks it for immediate 4299 * quiet, not the driver) 4300 */ 4301 if (!disable && mvmvif->deflink.bcast_sta.sta_id != IWL_MVM_INVALID_STA) 4302 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif, 4303 &mvmvif->deflink.bcast_sta, 4304 disable); 4305 } 4306 4307 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif) 4308 { 4309 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 4310 struct iwl_mvm_sta *mvmsta; 4311 4312 rcu_read_lock(); 4313 4314 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->deflink.ap_sta_id); 4315 4316 if (mvmsta) 4317 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true); 4318 4319 rcu_read_unlock(); 4320 } 4321 4322 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data) 4323 { 4324 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number); 4325 4326 /* 4327 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need 4328 * to align the wrap around of ssn so we compare relevant values. 4329 */ 4330 if (mvm->trans->trans_cfg->gen2) 4331 sn &= 0xff; 4332 4333 return ieee80211_sn_sub(sn, tid_data->next_reclaimed); 4334 } 4335 4336 int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 4337 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher, 4338 u8 *key, u32 key_len, 4339 struct ieee80211_key_conf *keyconf) 4340 { 4341 int ret; 4342 u16 queue; 4343 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 4344 unsigned int wdg_timeout = 4345 iwl_mvm_get_wd_timeout(mvm, vif, false, false); 4346 bool mld = iwl_mvm_has_mld_api(mvm->fw); 4347 u32 type = mld ? STATION_TYPE_PEER : IWL_STA_LINK; 4348 4349 ret = iwl_mvm_allocate_int_sta(mvm, sta, 0, 4350 NL80211_IFTYPE_UNSPECIFIED, type); 4351 if (ret) 4352 return ret; 4353 4354 if (mld) 4355 ret = iwl_mvm_mld_add_int_sta_with_queue(mvm, sta, addr, 4356 mvmvif->deflink.fw_link_id, 4357 &queue, 4358 IWL_MAX_TID_COUNT, 4359 &wdg_timeout); 4360 else 4361 ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, 4362 mvmvif->color, addr, sta, 4363 &queue, 4364 IWL_MVM_TX_FIFO_BE); 4365 if (ret) 4366 goto out; 4367 4368 keyconf->cipher = cipher; 4369 memcpy(keyconf->key, key, key_len); 4370 keyconf->keylen = key_len; 4371 keyconf->flags = IEEE80211_KEY_FLAG_PAIRWISE; 4372 4373 if (mld) { 4374 /* The MFP flag is set according to the station mfp field. Since 4375 * we don't have a station, set it manually. 4376 */ 4377 u32 key_flags = 4378 iwl_mvm_get_sec_flags(mvm, vif, NULL, keyconf) | 4379 IWL_SEC_KEY_FLAG_MFP; 4380 u32 sta_mask = BIT(sta->sta_id); 4381 4382 ret = iwl_mvm_mld_send_key(mvm, sta_mask, key_flags, keyconf); 4383 } else { 4384 ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false, 4385 0, NULL, 0, 0, true); 4386 } 4387 4388 out: 4389 if (ret) 4390 iwl_mvm_dealloc_int_sta(mvm, sta); 4391 return ret; 4392 } 4393 4394 void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm, 4395 struct ieee80211_vif *vif, 4396 u32 id) 4397 { 4398 struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = { 4399 .id = cpu_to_le32(id), 4400 }; 4401 int ret; 4402 4403 ret = iwl_mvm_send_cmd_pdu(mvm, 4404 WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD), 4405 CMD_ASYNC, 4406 sizeof(cancel_channel_switch_cmd), 4407 &cancel_channel_switch_cmd); 4408 if (ret) 4409 IWL_ERR(mvm, "Failed to cancel the channel switch\n"); 4410 } 4411 4412 static int iwl_mvm_fw_sta_id_to_fw_link_id(struct iwl_mvm_vif *mvmvif, 4413 u8 fw_sta_id) 4414 { 4415 struct ieee80211_link_sta *link_sta = 4416 rcu_dereference(mvmvif->mvm->fw_id_to_link_sta[fw_sta_id]); 4417 struct iwl_mvm_vif_link_info *link; 4418 4419 if (WARN_ON_ONCE(!link_sta)) 4420 return -EINVAL; 4421 4422 link = mvmvif->link[link_sta->link_id]; 4423 4424 if (WARN_ON_ONCE(!link)) 4425 return -EINVAL; 4426 4427 return link->fw_link_id; 4428 } 4429 4430 #define IWL_MVM_TPT_COUNT_WINDOW (IWL_MVM_TPT_COUNT_WINDOW_SEC * HZ) 4431 4432 void iwl_mvm_count_mpdu(struct iwl_mvm_sta *mvm_sta, u8 fw_sta_id, u32 count, 4433 bool tx, int queue) 4434 { 4435 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvm_sta->vif); 4436 struct iwl_mvm_tpt_counter *queue_counter; 4437 struct iwl_mvm_mpdu_counter *link_counter; 4438 u32 total_mpdus = 0; 4439 int fw_link_id; 4440 4441 /* Count only for a BSS sta, and only when EMLSR is possible */ 4442 if (!mvm_sta->mpdu_counters) 4443 return; 4444 4445 /* Map sta id to link id */ 4446 fw_link_id = iwl_mvm_fw_sta_id_to_fw_link_id(mvmvif, fw_sta_id); 4447 if (fw_link_id < 0) 4448 return; 4449 4450 queue_counter = &mvm_sta->mpdu_counters[queue]; 4451 link_counter = &queue_counter->per_link[fw_link_id]; 4452 4453 spin_lock_bh(&queue_counter->lock); 4454 4455 if (tx) 4456 link_counter->tx += count; 4457 else 4458 link_counter->rx += count; 4459 4460 /* 4461 * When not in EMLSR, the window and the decision to enter EMLSR are 4462 * handled during counting, when in EMLSR - in the statistics flow 4463 */ 4464 if (mvmvif->esr_active) 4465 goto out; 4466 4467 if (time_is_before_jiffies(queue_counter->window_start + 4468 IWL_MVM_TPT_COUNT_WINDOW)) { 4469 memset(queue_counter->per_link, 0, 4470 sizeof(queue_counter->per_link)); 4471 queue_counter->window_start = jiffies; 4472 } 4473 4474 for (int i = 0; i < IWL_MVM_FW_MAX_LINK_ID; i++) 4475 total_mpdus += tx ? queue_counter->per_link[i].tx : 4476 queue_counter->per_link[i].rx; 4477 4478 if (total_mpdus > IWL_MVM_ENTER_ESR_TPT_THRESH) 4479 wiphy_work_queue(mvmvif->mvm->hw->wiphy, 4480 &mvmvif->unblock_esr_tpt_wk); 4481 4482 out: 4483 spin_unlock_bh(&queue_counter->lock); 4484 } 4485