1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2012-2015, 2018-2022 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
7 #include <net/mac80211.h>
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.
18 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
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);
24 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
27 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
28 enum nl80211_iftype iftype)
33 BUILD_BUG_ON(IWL_MVM_STATION_COUNT_MAX > 32);
34 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
36 lockdep_assert_held(&mvm->mutex);
38 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
39 if (iftype != NL80211_IFTYPE_STATION)
40 reserved_ids = BIT(0);
42 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
43 for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) {
44 if (BIT(sta_id) & reserved_ids)
47 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
48 lockdep_is_held(&mvm->mutex)))
51 return IWL_MVM_INVALID_STA;
54 /* send station add/update command to firmware */
55 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
56 bool update, unsigned int flags)
58 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
59 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
60 .sta_id = mvm_sta->sta_id,
61 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
62 .add_modify = update ? 1 : 0,
63 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
65 STA_FLG_RTS_MIMO_PROT),
66 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
70 u32 agg_size = 0, mpdu_dens = 0;
72 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
73 add_sta_cmd.station_type = mvm_sta->sta_type;
75 if (!update || (flags & STA_MODIFY_QUEUES)) {
76 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
78 if (!iwl_mvm_has_new_tx_api(mvm)) {
79 add_sta_cmd.tfd_queue_msk =
80 cpu_to_le32(mvm_sta->tfd_queue_msk);
82 if (flags & STA_MODIFY_QUEUES)
83 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
85 WARN_ON(flags & STA_MODIFY_QUEUES);
89 switch (sta->deflink.bandwidth) {
90 case IEEE80211_STA_RX_BW_320:
91 case IEEE80211_STA_RX_BW_160:
92 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
94 case IEEE80211_STA_RX_BW_80:
95 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
97 case IEEE80211_STA_RX_BW_40:
98 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
100 case IEEE80211_STA_RX_BW_20:
101 if (sta->deflink.ht_cap.ht_supported)
102 add_sta_cmd.station_flags |=
103 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
107 switch (sta->deflink.rx_nss) {
109 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
112 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
115 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
119 switch (sta->deflink.smps_mode) {
120 case IEEE80211_SMPS_AUTOMATIC:
121 case IEEE80211_SMPS_NUM_MODES:
124 case IEEE80211_SMPS_STATIC:
126 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
127 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
129 case IEEE80211_SMPS_DYNAMIC:
130 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
132 case IEEE80211_SMPS_OFF:
137 if (sta->deflink.ht_cap.ht_supported) {
138 add_sta_cmd.station_flags_msk |=
139 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
140 STA_FLG_AGG_MPDU_DENS_MSK);
142 mpdu_dens = sta->deflink.ht_cap.ampdu_density;
145 if (mvm_sta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ) {
146 add_sta_cmd.station_flags_msk |=
147 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
148 STA_FLG_AGG_MPDU_DENS_MSK);
150 mpdu_dens = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
151 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
152 agg_size = le16_get_bits(sta->deflink.he_6ghz_capa.capa,
153 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
154 } else if (sta->deflink.vht_cap.vht_supported) {
155 agg_size = sta->deflink.vht_cap.cap &
156 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
158 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
159 } else if (sta->deflink.ht_cap.ht_supported) {
160 agg_size = sta->deflink.ht_cap.ampdu_factor;
163 /* D6.0 10.12.2 A-MPDU length limit rules
164 * A STA indicates the maximum length of the A-MPDU preEOF padding
165 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
166 * Exponent field in its HT Capabilities, VHT Capabilities,
167 * and HE 6 GHz Band Capabilities elements (if present) and the
168 * Maximum AMPDU Length Exponent Extension field in its HE
169 * Capabilities element
171 if (sta->deflink.he_cap.has_he)
172 agg_size += u8_get_bits(sta->deflink.he_cap.he_cap_elem.mac_cap_info[3],
173 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
175 /* Limit to max A-MPDU supported by FW */
176 if (agg_size > (STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT))
177 agg_size = (STA_FLG_MAX_AGG_SIZE_4M >>
178 STA_FLG_MAX_AGG_SIZE_SHIFT);
180 add_sta_cmd.station_flags |=
181 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
182 add_sta_cmd.station_flags |=
183 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
184 if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
185 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
188 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
190 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
191 add_sta_cmd.uapsd_acs |= BIT(AC_BK);
192 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
193 add_sta_cmd.uapsd_acs |= BIT(AC_BE);
194 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
195 add_sta_cmd.uapsd_acs |= BIT(AC_VI);
196 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
197 add_sta_cmd.uapsd_acs |= BIT(AC_VO);
198 add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
199 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
202 status = ADD_STA_SUCCESS;
203 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
204 iwl_mvm_add_sta_cmd_size(mvm),
205 &add_sta_cmd, &status);
209 switch (status & IWL_ADD_STA_STATUS_MASK) {
210 case ADD_STA_SUCCESS:
211 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
215 IWL_ERR(mvm, "ADD_STA failed\n");
222 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
224 struct iwl_mvm_baid_data *data =
225 from_timer(data, t, session_timer);
226 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
227 struct iwl_mvm_baid_data *ba_data;
228 struct ieee80211_sta *sta;
229 struct iwl_mvm_sta *mvm_sta;
230 unsigned long timeout;
234 ba_data = rcu_dereference(*rcu_ptr);
236 if (WARN_ON(!ba_data))
239 if (!ba_data->timeout)
242 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
243 if (time_is_after_jiffies(timeout)) {
244 mod_timer(&ba_data->session_timer, timeout);
249 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
252 * sta should be valid unless the following happens:
253 * The firmware asserts which triggers a reconfig flow, but
254 * the reconfig fails before we set the pointer to sta into
255 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
256 * A-MDPU and hence the timer continues to run. Then, the
257 * timer expires and sta is NULL.
262 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
263 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
264 sta->addr, ba_data->tid);
269 /* Disable aggregations for a bitmap of TIDs for a given station */
270 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
271 unsigned long disable_agg_tids,
274 struct iwl_mvm_add_sta_cmd cmd = {};
275 struct ieee80211_sta *sta;
276 struct iwl_mvm_sta *mvmsta;
280 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
283 sta_id = mvm->queue_info[queue].ra_sta_id;
287 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
289 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
294 mvmsta = iwl_mvm_sta_from_mac80211(sta);
296 mvmsta->tid_disable_agg |= disable_agg_tids;
298 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
299 cmd.sta_id = mvmsta->sta_id;
300 cmd.add_modify = STA_MODE_MODIFY;
301 cmd.modify_mask = STA_MODIFY_QUEUES;
302 if (disable_agg_tids)
303 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
305 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
306 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
307 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
311 /* Notify FW of queue removal from the STA queues */
312 status = ADD_STA_SUCCESS;
313 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
314 iwl_mvm_add_sta_cmd_size(mvm),
318 static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
319 u16 *queueptr, u8 tid)
321 int queue = *queueptr;
322 struct iwl_scd_txq_cfg_cmd cmd = {
324 .action = SCD_CFG_DISABLE_QUEUE,
328 lockdep_assert_held(&mvm->mutex);
330 if (iwl_mvm_has_new_tx_api(mvm)) {
331 if (mvm->sta_remove_requires_queue_remove) {
332 u32 cmd_id = WIDE_ID(DATA_PATH_GROUP,
333 SCD_QUEUE_CONFIG_CMD);
334 struct iwl_scd_queue_cfg_cmd remove_cmd = {
335 .operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE),
336 .u.remove.queue = cpu_to_le32(queue),
339 ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0,
346 iwl_trans_txq_free(mvm->trans, queue);
347 *queueptr = IWL_MVM_INVALID_QUEUE;
352 if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
355 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
357 cmd.action = mvm->queue_info[queue].tid_bitmap ?
358 SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
359 if (cmd.action == SCD_CFG_DISABLE_QUEUE)
360 mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
362 IWL_DEBUG_TX_QUEUES(mvm,
363 "Disabling TXQ #%d tids=0x%x\n",
365 mvm->queue_info[queue].tid_bitmap);
367 /* If the queue is still enabled - nothing left to do in this func */
368 if (cmd.action == SCD_CFG_ENABLE_QUEUE)
371 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
372 cmd.tid = mvm->queue_info[queue].txq_tid;
374 /* Make sure queue info is correct even though we overwrite it */
375 WARN(mvm->queue_info[queue].tid_bitmap,
376 "TXQ #%d info out-of-sync - tids=0x%x\n",
377 queue, mvm->queue_info[queue].tid_bitmap);
379 /* If we are here - the queue is freed and we can zero out these vals */
380 mvm->queue_info[queue].tid_bitmap = 0;
383 struct iwl_mvm_txq *mvmtxq =
384 iwl_mvm_txq_from_tid(sta, tid);
386 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
387 list_del_init(&mvmtxq->list);
390 /* Regardless if this is a reserved TXQ for a STA - mark it as false */
391 mvm->queue_info[queue].reserved = false;
393 iwl_trans_txq_disable(mvm->trans, queue, false);
394 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0,
395 sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
398 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
403 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
405 struct ieee80211_sta *sta;
406 struct iwl_mvm_sta *mvmsta;
407 unsigned long tid_bitmap;
408 unsigned long agg_tids = 0;
412 lockdep_assert_held(&mvm->mutex);
414 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
417 sta_id = mvm->queue_info[queue].ra_sta_id;
418 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
420 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
421 lockdep_is_held(&mvm->mutex));
423 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
426 mvmsta = iwl_mvm_sta_from_mac80211(sta);
428 spin_lock_bh(&mvmsta->lock);
429 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
430 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
431 agg_tids |= BIT(tid);
433 spin_unlock_bh(&mvmsta->lock);
439 * Remove a queue from a station's resources.
440 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
441 * doesn't disable the queue
443 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
445 struct ieee80211_sta *sta;
446 struct iwl_mvm_sta *mvmsta;
447 unsigned long tid_bitmap;
448 unsigned long disable_agg_tids = 0;
452 lockdep_assert_held(&mvm->mutex);
454 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
457 sta_id = mvm->queue_info[queue].ra_sta_id;
458 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
462 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
464 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
469 mvmsta = iwl_mvm_sta_from_mac80211(sta);
471 spin_lock_bh(&mvmsta->lock);
472 /* Unmap MAC queues and TIDs from this queue */
473 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
474 struct iwl_mvm_txq *mvmtxq =
475 iwl_mvm_txq_from_tid(sta, tid);
477 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
478 disable_agg_tids |= BIT(tid);
479 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
481 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
482 list_del_init(&mvmtxq->list);
485 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
486 spin_unlock_bh(&mvmsta->lock);
491 * The TX path may have been using this TXQ_ID from the tid_data,
492 * so make sure it's no longer running so that we can safely reuse
493 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
494 * above, but nothing guarantees we've stopped using them. Thus,
495 * without this, we could get to iwl_mvm_disable_txq() and remove
496 * the queue while still sending frames to it.
500 return disable_agg_tids;
503 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
504 struct ieee80211_sta *old_sta,
507 struct iwl_mvm_sta *mvmsta;
509 unsigned long disable_agg_tids = 0;
511 u16 queue_tmp = queue;
514 lockdep_assert_held(&mvm->mutex);
516 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
519 sta_id = mvm->queue_info[queue].ra_sta_id;
520 tid = mvm->queue_info[queue].txq_tid;
522 same_sta = sta_id == new_sta_id;
524 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
525 if (WARN_ON(!mvmsta))
528 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
529 /* Disable the queue */
530 if (disable_agg_tids)
531 iwl_mvm_invalidate_sta_queue(mvm, queue,
532 disable_agg_tids, false);
534 ret = iwl_mvm_disable_txq(mvm, old_sta, &queue_tmp, tid);
537 "Failed to free inactive queue %d (ret=%d)\n",
543 /* If TXQ is allocated to another STA, update removal in FW */
545 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
550 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
551 unsigned long tfd_queue_mask, u8 ac)
554 u8 ac_to_queue[IEEE80211_NUM_ACS];
558 * This protects us against grabbing a queue that's being reconfigured
559 * by the inactivity checker.
561 lockdep_assert_held(&mvm->mutex);
563 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
566 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
568 /* See what ACs the existing queues for this STA have */
569 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
570 /* Only DATA queues can be shared */
571 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
572 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
575 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
579 * The queue to share is chosen only from DATA queues as follows (in
580 * descending priority):
583 * 3. Highest AC queue that is lower than new AC
584 * 4. Any existing AC (there always is at least 1 DATA queue)
587 /* Priority 1: An AC_BE queue */
588 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
589 queue = ac_to_queue[IEEE80211_AC_BE];
590 /* Priority 2: Same AC queue */
591 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
592 queue = ac_to_queue[ac];
593 /* Priority 3a: If new AC is VO and VI exists - use VI */
594 else if (ac == IEEE80211_AC_VO &&
595 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
596 queue = ac_to_queue[IEEE80211_AC_VI];
597 /* Priority 3b: No BE so only AC less than the new one is BK */
598 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
599 queue = ac_to_queue[IEEE80211_AC_BK];
600 /* Priority 4a: No BE nor BK - use VI if exists */
601 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
602 queue = ac_to_queue[IEEE80211_AC_VI];
603 /* Priority 4b: No BE, BK nor VI - use VO if exists */
604 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
605 queue = ac_to_queue[IEEE80211_AC_VO];
607 /* Make sure queue found (or not) is legal */
608 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
609 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
610 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
611 IWL_ERR(mvm, "No DATA queues available to share\n");
618 /* Re-configure the SCD for a queue that has already been configured */
619 static int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo,
620 int sta_id, int tid, int frame_limit, u16 ssn)
622 struct iwl_scd_txq_cfg_cmd cmd = {
624 .action = SCD_CFG_ENABLE_QUEUE,
625 .window = frame_limit,
627 .ssn = cpu_to_le16(ssn),
629 .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
630 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
635 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
638 if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
639 "Trying to reconfig unallocated queue %d\n", queue))
642 IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
644 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
645 WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
652 * If a given queue has a higher AC than the TID stream that is being compared
653 * to, the queue needs to be redirected to the lower AC. This function does that
654 * in such a case, otherwise - if no redirection required - it does nothing,
655 * unless the %force param is true.
657 static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
658 int ac, int ssn, unsigned int wdg_timeout,
659 bool force, struct iwl_mvm_txq *txq)
661 struct iwl_scd_txq_cfg_cmd cmd = {
663 .action = SCD_CFG_DISABLE_QUEUE,
668 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
672 * If the AC is lower than current one - FIFO needs to be redirected to
673 * the lowest one of the streams in the queue. Check if this is needed
675 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
676 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
677 * we need to check if the numerical value of X is LARGER than of Y.
679 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
680 IWL_DEBUG_TX_QUEUES(mvm,
681 "No redirection needed on TXQ #%d\n",
686 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
687 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
688 cmd.tid = mvm->queue_info[queue].txq_tid;
689 shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
691 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
692 queue, iwl_mvm_ac_to_tx_fifo[ac]);
694 /* Stop the queue and wait for it to empty */
697 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
699 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
705 /* Before redirecting the queue we need to de-activate it */
706 iwl_trans_txq_disable(mvm->trans, queue, false);
707 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
709 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
712 /* Make sure the SCD wrptr is correctly set before reconfiguring */
713 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
715 /* Update the TID "owner" of the queue */
716 mvm->queue_info[queue].txq_tid = tid;
718 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
720 /* Redirect to lower AC */
721 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
722 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
724 /* Update AC marking of the queue */
725 mvm->queue_info[queue].mac80211_ac = ac;
728 * Mark queue as shared in transport if shared
729 * Note this has to be done after queue enablement because enablement
730 * can also set this value, and there is no indication there to shared
734 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
737 /* Continue using the queue */
738 txq->stopped = false;
743 static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
748 lockdep_assert_held(&mvm->mutex);
750 if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
751 "max queue %d >= num_of_queues (%d)", maxq,
752 mvm->trans->trans_cfg->base_params->num_of_queues))
753 maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
755 /* This should not be hit with new TX path */
756 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
759 /* Start by looking for a free queue */
760 for (i = minq; i <= maxq; i++)
761 if (mvm->queue_info[i].tid_bitmap == 0 &&
762 mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
768 static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
769 u8 sta_id, u8 tid, unsigned int timeout)
773 if (tid == IWL_MAX_TID_COUNT) {
775 size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
776 mvm->trans->cfg->min_txq_size);
778 struct ieee80211_sta *sta;
781 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
783 /* this queue isn't used for traffic (cab_queue) */
784 if (IS_ERR_OR_NULL(sta)) {
785 size = IWL_MGMT_QUEUE_SIZE;
786 } else if (sta->deflink.he_cap.has_he) {
787 /* support for 256 ba size */
788 size = IWL_DEFAULT_QUEUE_SIZE_HE;
790 size = IWL_DEFAULT_QUEUE_SIZE;
796 /* take the min with bc tbl entries allowed */
797 size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16));
799 /* size needs to be power of 2 values for calculating read/write pointers */
800 size = rounddown_pow_of_two(size);
803 queue = iwl_trans_txq_alloc(mvm->trans, 0, BIT(sta_id),
807 IWL_DEBUG_TX_QUEUES(mvm,
808 "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
809 size, sta_id, tid, queue);
811 } while (queue < 0 && size >= 16);
816 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
822 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
823 struct ieee80211_sta *sta, u8 ac,
826 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
827 struct iwl_mvm_txq *mvmtxq =
828 iwl_mvm_txq_from_tid(sta, tid);
829 unsigned int wdg_timeout =
830 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
833 lockdep_assert_held(&mvm->mutex);
835 IWL_DEBUG_TX_QUEUES(mvm,
836 "Allocating queue for sta %d on tid %d\n",
837 mvmsta->sta_id, tid);
838 queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
842 mvmtxq->txq_id = queue;
843 mvm->tvqm_info[queue].txq_tid = tid;
844 mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
846 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
848 spin_lock_bh(&mvmsta->lock);
849 mvmsta->tid_data[tid].txq_id = queue;
850 spin_unlock_bh(&mvmsta->lock);
855 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
856 struct ieee80211_sta *sta,
857 int queue, u8 sta_id, u8 tid)
859 bool enable_queue = true;
861 /* Make sure this TID isn't already enabled */
862 if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
863 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
868 /* Update mappings and refcounts */
869 if (mvm->queue_info[queue].tid_bitmap)
870 enable_queue = false;
872 mvm->queue_info[queue].tid_bitmap |= BIT(tid);
873 mvm->queue_info[queue].ra_sta_id = sta_id;
876 if (tid != IWL_MAX_TID_COUNT)
877 mvm->queue_info[queue].mac80211_ac =
878 tid_to_mac80211_ac[tid];
880 mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
882 mvm->queue_info[queue].txq_tid = tid;
886 struct iwl_mvm_txq *mvmtxq =
887 iwl_mvm_txq_from_tid(sta, tid);
889 mvmtxq->txq_id = queue;
892 IWL_DEBUG_TX_QUEUES(mvm,
893 "Enabling TXQ #%d tids=0x%x\n",
894 queue, mvm->queue_info[queue].tid_bitmap);
899 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
901 const struct iwl_trans_txq_scd_cfg *cfg,
902 unsigned int wdg_timeout)
904 struct iwl_scd_txq_cfg_cmd cmd = {
906 .action = SCD_CFG_ENABLE_QUEUE,
907 .window = cfg->frame_limit,
908 .sta_id = cfg->sta_id,
909 .ssn = cpu_to_le16(ssn),
910 .tx_fifo = cfg->fifo,
911 .aggregate = cfg->aggregate,
916 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
919 /* Send the enabling command if we need to */
920 if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
923 inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
926 le16_add_cpu(&cmd.ssn, 1);
928 WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
929 "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
934 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
936 struct iwl_scd_txq_cfg_cmd cmd = {
938 .action = SCD_CFG_UPDATE_QUEUE_TID,
941 unsigned long tid_bitmap;
944 lockdep_assert_held(&mvm->mutex);
946 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
949 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
951 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
954 /* Find any TID for queue */
955 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
957 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
959 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
961 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
966 mvm->queue_info[queue].txq_tid = tid;
967 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
971 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
973 struct ieee80211_sta *sta;
974 struct iwl_mvm_sta *mvmsta;
977 unsigned long tid_bitmap;
978 unsigned int wdg_timeout;
982 /* queue sharing is disabled on new TX path */
983 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
986 lockdep_assert_held(&mvm->mutex);
988 sta_id = mvm->queue_info[queue].ra_sta_id;
989 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
991 /* Find TID for queue, and make sure it is the only one on the queue */
992 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
993 if (tid_bitmap != BIT(tid)) {
994 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
999 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
1002 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1003 lockdep_is_held(&mvm->mutex));
1005 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1008 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1009 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1011 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1013 ret = iwl_mvm_redirect_queue(mvm, queue, tid,
1014 tid_to_mac80211_ac[tid], ssn,
1016 iwl_mvm_txq_from_tid(sta, tid));
1018 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1022 /* If aggs should be turned back on - do it */
1023 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
1024 struct iwl_mvm_add_sta_cmd cmd = {0};
1026 mvmsta->tid_disable_agg &= ~BIT(tid);
1028 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1029 cmd.sta_id = mvmsta->sta_id;
1030 cmd.add_modify = STA_MODE_MODIFY;
1031 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1032 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1033 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1035 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1036 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1038 IWL_DEBUG_TX_QUEUES(mvm,
1039 "TXQ #%d is now aggregated again\n",
1042 /* Mark queue intenally as aggregating again */
1043 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1047 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1051 * Remove inactive TIDs of a given queue.
1052 * If all queue TIDs are inactive - mark the queue as inactive
1053 * If only some the queue TIDs are inactive - unmap them from the queue
1055 * Returns %true if all TIDs were removed and the queue could be reused.
1057 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1058 struct iwl_mvm_sta *mvmsta, int queue,
1059 unsigned long tid_bitmap,
1060 unsigned long *unshare_queues,
1061 unsigned long *changetid_queues)
1065 lockdep_assert_held(&mvmsta->lock);
1066 lockdep_assert_held(&mvm->mutex);
1068 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1071 /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1072 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1073 /* If some TFDs are still queued - don't mark TID as inactive */
1074 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1075 tid_bitmap &= ~BIT(tid);
1077 /* Don't mark as inactive any TID that has an active BA */
1078 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1079 tid_bitmap &= ~BIT(tid);
1082 /* If all TIDs in the queue are inactive - return it can be reused */
1083 if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1084 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1089 * If we are here, this is a shared queue and not all TIDs timed-out.
1090 * Remove the ones that did.
1092 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1095 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1096 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1098 q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1101 * We need to take into account a situation in which a TXQ was
1102 * allocated to TID x, and then turned shared by adding TIDs y
1103 * and z. If TID x becomes inactive and is removed from the TXQ,
1104 * ownership must be given to one of the remaining TIDs.
1105 * This is mainly because if TID x continues - a new queue can't
1106 * be allocated for it as long as it is an owner of another TXQ.
1108 * Mark this queue in the right bitmap, we'll send the command
1109 * to the firmware later.
1111 if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1112 set_bit(queue, changetid_queues);
1114 IWL_DEBUG_TX_QUEUES(mvm,
1115 "Removing inactive TID %d from shared Q:%d\n",
1119 IWL_DEBUG_TX_QUEUES(mvm,
1120 "TXQ #%d left with tid bitmap 0x%x\n", queue,
1121 mvm->queue_info[queue].tid_bitmap);
1124 * There may be different TIDs with the same mac queues, so make
1125 * sure all TIDs have existing corresponding mac queues enabled
1127 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1129 /* If the queue is marked as shared - "unshare" it */
1130 if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1131 mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1132 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1134 set_bit(queue, unshare_queues);
1141 * Check for inactivity - this includes checking if any queue
1142 * can be unshared and finding one (and only one) that can be
1144 * This function is also invoked as a sort of clean-up task,
1145 * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1147 * Returns the queue number, or -ENOSPC.
1149 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1151 unsigned long now = jiffies;
1152 unsigned long unshare_queues = 0;
1153 unsigned long changetid_queues = 0;
1154 int i, ret, free_queue = -ENOSPC;
1155 struct ieee80211_sta *queue_owner = NULL;
1157 lockdep_assert_held(&mvm->mutex);
1159 if (iwl_mvm_has_new_tx_api(mvm))
1164 /* we skip the CMD queue below by starting at 1 */
1165 BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1167 for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1168 struct ieee80211_sta *sta;
1169 struct iwl_mvm_sta *mvmsta;
1172 unsigned long inactive_tid_bitmap = 0;
1173 unsigned long queue_tid_bitmap;
1175 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1176 if (!queue_tid_bitmap)
1179 /* If TXQ isn't in active use anyway - nothing to do here... */
1180 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1181 mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1184 /* Check to see if there are inactive TIDs on this queue */
1185 for_each_set_bit(tid, &queue_tid_bitmap,
1186 IWL_MAX_TID_COUNT + 1) {
1187 if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1188 IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1191 inactive_tid_bitmap |= BIT(tid);
1194 /* If all TIDs are active - finish check on this queue */
1195 if (!inactive_tid_bitmap)
1199 * If we are here - the queue hadn't been served recently and is
1203 sta_id = mvm->queue_info[i].ra_sta_id;
1204 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1207 * If the STA doesn't exist anymore, it isn't an error. It could
1208 * be that it was removed since getting the queues, and in this
1209 * case it should've inactivated its queues anyway.
1211 if (IS_ERR_OR_NULL(sta))
1214 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1216 spin_lock_bh(&mvmsta->lock);
1217 ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1218 inactive_tid_bitmap,
1221 if (ret && free_queue < 0) {
1225 /* only unlock sta lock - we still need the queue info lock */
1226 spin_unlock_bh(&mvmsta->lock);
1230 /* Reconfigure queues requiring reconfiguation */
1231 for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1232 iwl_mvm_unshare_queue(mvm, i);
1233 for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1234 iwl_mvm_change_queue_tid(mvm, i);
1238 if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1239 ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1248 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1249 struct ieee80211_sta *sta, u8 ac, int tid)
1251 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1252 struct iwl_trans_txq_scd_cfg cfg = {
1253 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1254 .sta_id = mvmsta->sta_id,
1256 .frame_limit = IWL_FRAME_LIMIT,
1258 unsigned int wdg_timeout =
1259 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1262 unsigned long disable_agg_tids = 0;
1263 enum iwl_mvm_agg_state queue_state;
1264 bool shared_queue = false, inc_ssn;
1266 unsigned long tfd_queue_mask;
1269 lockdep_assert_held(&mvm->mutex);
1271 if (iwl_mvm_has_new_tx_api(mvm))
1272 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1274 spin_lock_bh(&mvmsta->lock);
1275 tfd_queue_mask = mvmsta->tfd_queue_msk;
1276 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1277 spin_unlock_bh(&mvmsta->lock);
1279 if (tid == IWL_MAX_TID_COUNT) {
1280 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1281 IWL_MVM_DQA_MIN_MGMT_QUEUE,
1282 IWL_MVM_DQA_MAX_MGMT_QUEUE);
1283 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1284 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1287 /* If no such queue is found, we'll use a DATA queue instead */
1290 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1291 (mvm->queue_info[mvmsta->reserved_queue].status ==
1292 IWL_MVM_QUEUE_RESERVED)) {
1293 queue = mvmsta->reserved_queue;
1294 mvm->queue_info[queue].reserved = true;
1295 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1299 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1300 IWL_MVM_DQA_MIN_DATA_QUEUE,
1301 IWL_MVM_DQA_MAX_DATA_QUEUE);
1303 /* try harder - perhaps kill an inactive queue */
1304 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1307 /* No free queue - we'll have to share */
1309 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1311 shared_queue = true;
1312 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1317 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1318 * to make sure no one else takes it.
1319 * This will allow avoiding re-acquiring the lock at the end of the
1320 * configuration. On error we'll mark it back as free.
1322 if (queue > 0 && !shared_queue)
1323 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1325 /* This shouldn't happen - out of queues */
1326 if (WARN_ON(queue <= 0)) {
1327 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1333 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1334 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1336 * Mark all DATA queues as allowing to be aggregated at some point
1338 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1339 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1341 IWL_DEBUG_TX_QUEUES(mvm,
1342 "Allocating %squeue #%d to sta %d on tid %d\n",
1343 shared_queue ? "shared " : "", queue,
1344 mvmsta->sta_id, tid);
1347 /* Disable any open aggs on this queue */
1348 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1350 if (disable_agg_tids) {
1351 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1353 iwl_mvm_invalidate_sta_queue(mvm, queue,
1354 disable_agg_tids, false);
1358 inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1361 * Mark queue as shared in transport if shared
1362 * Note this has to be done after queue enablement because enablement
1363 * can also set this value, and there is no indication there to shared
1367 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1369 spin_lock_bh(&mvmsta->lock);
1371 * This looks racy, but it is not. We have only one packet for
1372 * this ra/tid in our Tx path since we stop the Qdisc when we
1373 * need to allocate a new TFD queue.
1376 mvmsta->tid_data[tid].seq_number += 0x10;
1377 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1379 mvmsta->tid_data[tid].txq_id = queue;
1380 mvmsta->tfd_queue_msk |= BIT(queue);
1381 queue_state = mvmsta->tid_data[tid].state;
1383 if (mvmsta->reserved_queue == queue)
1384 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1385 spin_unlock_bh(&mvmsta->lock);
1387 if (!shared_queue) {
1388 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1392 /* If we need to re-enable aggregations... */
1393 if (queue_state == IWL_AGG_ON) {
1394 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1399 /* Redirect queue, if needed */
1400 ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1402 iwl_mvm_txq_from_tid(sta, tid));
1411 iwl_mvm_disable_txq(mvm, sta, &queue_tmp, tid);
1416 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1418 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1421 mutex_lock(&mvm->mutex);
1423 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1425 while (!list_empty(&mvm->add_stream_txqs)) {
1426 struct iwl_mvm_txq *mvmtxq;
1427 struct ieee80211_txq *txq;
1430 mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1431 struct iwl_mvm_txq, list);
1433 txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1436 if (tid == IEEE80211_NUM_TIDS)
1437 tid = IWL_MAX_TID_COUNT;
1440 * We can't really do much here, but if this fails we can't
1441 * transmit anyway - so just don't transmit the frame etc.
1442 * and let them back up ... we've tried our best to allocate
1443 * a queue in the function itself.
1445 if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
1446 list_del_init(&mvmtxq->list);
1450 list_del_init(&mvmtxq->list);
1452 iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1456 mutex_unlock(&mvm->mutex);
1459 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1460 struct ieee80211_sta *sta,
1461 enum nl80211_iftype vif_type)
1463 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1466 /* queue reserving is disabled on new TX path */
1467 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1470 /* run the general cleanup/unsharing of queues */
1471 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1473 /* Make sure we have free resources for this STA */
1474 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1475 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1476 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1477 IWL_MVM_QUEUE_FREE))
1478 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1480 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1481 IWL_MVM_DQA_MIN_DATA_QUEUE,
1482 IWL_MVM_DQA_MAX_DATA_QUEUE);
1484 /* try again - this time kick out a queue if needed */
1485 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1487 IWL_ERR(mvm, "No available queues for new station\n");
1491 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1493 mvmsta->reserved_queue = queue;
1495 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1496 queue, mvmsta->sta_id);
1502 * In DQA mode, after a HW restart the queues should be allocated as before, in
1503 * order to avoid race conditions when there are shared queues. This function
1504 * does the re-mapping and queue allocation.
1506 * Note that re-enabling aggregations isn't done in this function.
1508 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1509 struct ieee80211_sta *sta)
1511 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1513 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1515 struct iwl_trans_txq_scd_cfg cfg = {
1516 .sta_id = mvm_sta->sta_id,
1517 .frame_limit = IWL_FRAME_LIMIT,
1520 /* Make sure reserved queue is still marked as such (if allocated) */
1521 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1522 mvm->queue_info[mvm_sta->reserved_queue].status =
1523 IWL_MVM_QUEUE_RESERVED;
1525 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1526 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1527 int txq_id = tid_data->txq_id;
1530 if (txq_id == IWL_MVM_INVALID_QUEUE)
1533 ac = tid_to_mac80211_ac[i];
1535 if (iwl_mvm_has_new_tx_api(mvm)) {
1536 IWL_DEBUG_TX_QUEUES(mvm,
1537 "Re-mapping sta %d tid %d\n",
1538 mvm_sta->sta_id, i);
1539 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1542 * on failures, just set it to IWL_MVM_INVALID_QUEUE
1543 * to try again later, we have no other good way of
1547 txq_id = IWL_MVM_INVALID_QUEUE;
1548 tid_data->txq_id = txq_id;
1551 * Since we don't set the seq number after reset, and HW
1552 * sets it now, FW reset will cause the seq num to start
1553 * at 0 again, so driver will need to update it
1554 * internally as well, so it keeps in sync with real val
1556 tid_data->seq_number = 0;
1558 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1561 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1562 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1564 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1566 IWL_DEBUG_TX_QUEUES(mvm,
1567 "Re-mapping sta %d tid %d to queue %d\n",
1568 mvm_sta->sta_id, i, txq_id);
1570 iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1571 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1576 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1577 struct iwl_mvm_int_sta *sta,
1579 u16 mac_id, u16 color)
1581 struct iwl_mvm_add_sta_cmd cmd;
1583 u32 status = ADD_STA_SUCCESS;
1585 lockdep_assert_held(&mvm->mutex);
1587 memset(&cmd, 0, sizeof(cmd));
1588 cmd.sta_id = sta->sta_id;
1590 if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12 &&
1591 sta->type == IWL_STA_AUX_ACTIVITY)
1592 cmd.mac_id_n_color = cpu_to_le32(mac_id);
1594 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1597 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1598 cmd.station_type = sta->type;
1600 if (!iwl_mvm_has_new_tx_api(mvm))
1601 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1602 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1605 memcpy(cmd.addr, addr, ETH_ALEN);
1607 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1608 iwl_mvm_add_sta_cmd_size(mvm),
1613 switch (status & IWL_ADD_STA_STATUS_MASK) {
1614 case ADD_STA_SUCCESS:
1615 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1619 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1626 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1627 struct ieee80211_vif *vif,
1628 struct ieee80211_sta *sta)
1630 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1631 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1632 struct iwl_mvm_rxq_dup_data *dup_data;
1634 bool sta_update = false;
1635 unsigned int sta_flags = 0;
1637 lockdep_assert_held(&mvm->mutex);
1639 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1640 sta_id = iwl_mvm_find_free_sta_id(mvm,
1641 ieee80211_vif_type_p2p(vif));
1643 sta_id = mvm_sta->sta_id;
1645 if (sta_id == IWL_MVM_INVALID_STA)
1648 spin_lock_init(&mvm_sta->lock);
1650 /* if this is a HW restart re-alloc existing queues */
1651 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1652 struct iwl_mvm_int_sta tmp_sta = {
1654 .type = mvm_sta->sta_type,
1658 * First add an empty station since allocating
1659 * a queue requires a valid station
1661 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1662 mvmvif->id, mvmvif->color);
1666 iwl_mvm_realloc_queues_after_restart(mvm, sta);
1668 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1672 mvm_sta->sta_id = sta_id;
1673 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1676 if (!mvm->trans->trans_cfg->gen2)
1677 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1679 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1680 mvm_sta->tx_protection = 0;
1681 mvm_sta->tt_tx_protection = false;
1682 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1684 /* HW restart, don't assume the memory has been zeroed */
1685 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1686 mvm_sta->tfd_queue_msk = 0;
1688 /* for HW restart - reset everything but the sequence number */
1689 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1690 u16 seq = mvm_sta->tid_data[i].seq_number;
1691 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1692 mvm_sta->tid_data[i].seq_number = seq;
1695 * Mark all queues for this STA as unallocated and defer TX
1696 * frames until the queue is allocated
1698 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1701 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1702 struct iwl_mvm_txq *mvmtxq =
1703 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1705 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1706 INIT_LIST_HEAD(&mvmtxq->list);
1707 atomic_set(&mvmtxq->tx_request, 0);
1710 mvm_sta->agg_tids = 0;
1712 if (iwl_mvm_has_new_rx_api(mvm) &&
1713 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1716 dup_data = kcalloc(mvm->trans->num_rx_queues,
1717 sizeof(*dup_data), GFP_KERNEL);
1721 * Initialize all the last_seq values to 0xffff which can never
1722 * compare equal to the frame's seq_ctrl in the check in
1723 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1724 * number and fragmented packets don't reach that function.
1726 * This thus allows receiving a packet with seqno 0 and the
1727 * retry bit set as the very first packet on a new TID.
1729 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1730 memset(dup_data[q].last_seq, 0xff,
1731 sizeof(dup_data[q].last_seq));
1732 mvm_sta->dup_data = dup_data;
1735 if (!iwl_mvm_has_new_tx_api(mvm)) {
1736 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1737 ieee80211_vif_type_p2p(vif));
1743 * if rs is registered with mac80211, then "add station" will be handled
1744 * via the corresponding ops, otherwise need to notify rate scaling here
1746 if (iwl_mvm_has_tlc_offload(mvm))
1747 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1749 spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
1751 iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1754 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1758 if (vif->type == NL80211_IFTYPE_STATION) {
1760 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1761 mvmvif->ap_sta_id = sta_id;
1763 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1767 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1775 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1778 struct iwl_mvm_add_sta_cmd cmd = {};
1782 lockdep_assert_held(&mvm->mutex);
1784 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1785 cmd.sta_id = mvmsta->sta_id;
1786 cmd.add_modify = STA_MODE_MODIFY;
1787 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1788 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1790 status = ADD_STA_SUCCESS;
1791 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1792 iwl_mvm_add_sta_cmd_size(mvm),
1797 switch (status & IWL_ADD_STA_STATUS_MASK) {
1798 case ADD_STA_SUCCESS:
1799 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1804 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1813 * Remove a station from the FW table. Before sending the command to remove
1814 * the station validate that the station is indeed known to the driver (sanity
1817 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1819 struct ieee80211_sta *sta;
1820 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1825 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1826 lockdep_is_held(&mvm->mutex));
1828 /* Note: internal stations are marked as error values */
1830 IWL_ERR(mvm, "Invalid station id\n");
1834 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1835 sizeof(rm_sta_cmd), &rm_sta_cmd);
1837 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1844 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1845 struct ieee80211_vif *vif,
1846 struct ieee80211_sta *sta)
1848 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1851 lockdep_assert_held(&mvm->mutex);
1853 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1854 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1857 iwl_mvm_disable_txq(mvm, sta, &mvm_sta->tid_data[i].txq_id, i);
1858 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1861 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1862 struct iwl_mvm_txq *mvmtxq =
1863 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1865 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1866 list_del_init(&mvmtxq->list);
1870 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1871 struct iwl_mvm_sta *mvm_sta)
1875 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1879 spin_lock_bh(&mvm_sta->lock);
1880 txq_id = mvm_sta->tid_data[i].txq_id;
1881 spin_unlock_bh(&mvm_sta->lock);
1883 if (txq_id == IWL_MVM_INVALID_QUEUE)
1886 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1894 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1895 struct ieee80211_vif *vif,
1896 struct ieee80211_sta *sta)
1898 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1899 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1900 u8 sta_id = mvm_sta->sta_id;
1903 lockdep_assert_held(&mvm->mutex);
1905 if (iwl_mvm_has_new_rx_api(mvm))
1906 kfree(mvm_sta->dup_data);
1908 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1912 /* flush its queues here since we are freeing mvm_sta */
1913 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false);
1916 if (iwl_mvm_has_new_tx_api(mvm)) {
1917 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1919 u32 q_mask = mvm_sta->tfd_queue_msk;
1921 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1927 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1929 iwl_mvm_disable_sta_queues(mvm, vif, sta);
1931 /* If there is a TXQ still marked as reserved - free it */
1932 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1933 u8 reserved_txq = mvm_sta->reserved_queue;
1934 enum iwl_mvm_queue_status *status;
1937 * If no traffic has gone through the reserved TXQ - it
1938 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1939 * should be manually marked as free again
1941 status = &mvm->queue_info[reserved_txq].status;
1942 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1943 (*status != IWL_MVM_QUEUE_FREE),
1944 "sta_id %d reserved txq %d status %d",
1945 sta_id, reserved_txq, *status))
1948 *status = IWL_MVM_QUEUE_FREE;
1951 if (vif->type == NL80211_IFTYPE_STATION &&
1952 mvmvif->ap_sta_id == sta_id) {
1953 /* if associated - we can't remove the AP STA now */
1957 /* unassoc - go ahead - remove the AP STA now */
1958 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1962 * This shouldn't happen - the TDLS channel switch should be canceled
1963 * before the STA is removed.
1965 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1966 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1967 cancel_delayed_work(&mvm->tdls_cs.dwork);
1971 * Make sure that the tx response code sees the station as -EBUSY and
1972 * calls the drain worker.
1974 spin_lock_bh(&mvm_sta->lock);
1975 spin_unlock_bh(&mvm_sta->lock);
1977 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1978 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1983 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1984 struct ieee80211_vif *vif,
1987 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1989 lockdep_assert_held(&mvm->mutex);
1991 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1995 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1996 struct iwl_mvm_int_sta *sta,
1997 u32 qmask, enum nl80211_iftype iftype,
1998 enum iwl_sta_type type)
2000 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
2001 sta->sta_id == IWL_MVM_INVALID_STA) {
2002 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
2003 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
2007 sta->tfd_queue_msk = qmask;
2010 /* put a non-NULL value so iterating over the stations won't stop */
2011 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
2015 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
2017 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
2018 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
2019 sta->sta_id = IWL_MVM_INVALID_STA;
2022 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
2025 unsigned int wdg_timeout =
2026 mvm->trans->trans_cfg->base_params->wd_timeout;
2027 struct iwl_trans_txq_scd_cfg cfg = {
2030 .tid = IWL_MAX_TID_COUNT,
2032 .frame_limit = IWL_FRAME_LIMIT,
2035 WARN_ON(iwl_mvm_has_new_tx_api(mvm));
2037 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2040 static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
2042 unsigned int wdg_timeout =
2043 mvm->trans->trans_cfg->base_params->wd_timeout;
2045 WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2047 return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
2051 static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2052 int maccolor, u8 *addr,
2053 struct iwl_mvm_int_sta *sta,
2054 u16 *queue, int fifo)
2058 /* Map queue to fifo - needs to happen before adding station */
2059 if (!iwl_mvm_has_new_tx_api(mvm))
2060 iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2062 ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
2064 if (!iwl_mvm_has_new_tx_api(mvm))
2065 iwl_mvm_disable_txq(mvm, NULL, queue,
2071 * For 22000 firmware and on we cannot add queue to a station unknown
2072 * to firmware so enable queue here - after the station was added
2074 if (iwl_mvm_has_new_tx_api(mvm)) {
2077 txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2079 iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2089 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2093 lockdep_assert_held(&mvm->mutex);
2095 /* Allocate aux station and assign to it the aux queue */
2096 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2097 NL80211_IFTYPE_UNSPECIFIED,
2098 IWL_STA_AUX_ACTIVITY);
2103 * In CDB NICs we need to specify which lmac to use for aux activity
2104 * using the mac_id argument place to send lmac_id to the function
2106 ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
2107 &mvm->aux_sta, &mvm->aux_queue,
2108 IWL_MVM_TX_FIFO_MCAST);
2110 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2117 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2119 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2121 lockdep_assert_held(&mvm->mutex);
2123 return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2124 NULL, &mvm->snif_sta,
2126 IWL_MVM_TX_FIFO_BE);
2129 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2133 lockdep_assert_held(&mvm->mutex);
2135 if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2138 iwl_mvm_disable_txq(mvm, NULL, &mvm->snif_queue, IWL_MAX_TID_COUNT);
2139 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2141 IWL_WARN(mvm, "Failed sending remove station\n");
2146 int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2150 lockdep_assert_held(&mvm->mutex);
2152 if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2155 iwl_mvm_disable_txq(mvm, NULL, &mvm->aux_queue, IWL_MAX_TID_COUNT);
2156 ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2158 IWL_WARN(mvm, "Failed sending remove station\n");
2159 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2164 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2166 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2170 * Send the add station command for the vif's broadcast station.
2171 * Assumes that the station was already allocated.
2173 * @mvm: the mvm component
2174 * @vif: the interface to which the broadcast station is added
2175 * @bsta: the broadcast station to add.
2177 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2179 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2180 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2181 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2182 const u8 *baddr = _baddr;
2185 unsigned int wdg_timeout =
2186 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2187 struct iwl_trans_txq_scd_cfg cfg = {
2188 .fifo = IWL_MVM_TX_FIFO_VO,
2189 .sta_id = mvmvif->bcast_sta.sta_id,
2190 .tid = IWL_MAX_TID_COUNT,
2192 .frame_limit = IWL_FRAME_LIMIT,
2195 lockdep_assert_held(&mvm->mutex);
2197 if (!iwl_mvm_has_new_tx_api(mvm)) {
2198 if (vif->type == NL80211_IFTYPE_AP ||
2199 vif->type == NL80211_IFTYPE_ADHOC) {
2200 queue = mvm->probe_queue;
2201 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2202 queue = mvm->p2p_dev_queue;
2204 WARN(1, "Missing required TXQ for adding bcast STA\n");
2208 bsta->tfd_queue_msk |= BIT(queue);
2210 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2213 if (vif->type == NL80211_IFTYPE_ADHOC)
2214 baddr = vif->bss_conf.bssid;
2216 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2219 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2220 mvmvif->id, mvmvif->color);
2225 * For 22000 firmware and on we cannot add queue to a station unknown
2226 * to firmware so enable queue here - after the station was added
2228 if (iwl_mvm_has_new_tx_api(mvm)) {
2229 queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2233 iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2237 if (vif->type == NL80211_IFTYPE_AP ||
2238 vif->type == NL80211_IFTYPE_ADHOC)
2239 mvm->probe_queue = queue;
2240 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2241 mvm->p2p_dev_queue = queue;
2247 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2248 struct ieee80211_vif *vif)
2250 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2251 u16 *queueptr, queue;
2253 lockdep_assert_held(&mvm->mutex);
2255 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true);
2257 switch (vif->type) {
2258 case NL80211_IFTYPE_AP:
2259 case NL80211_IFTYPE_ADHOC:
2260 queueptr = &mvm->probe_queue;
2262 case NL80211_IFTYPE_P2P_DEVICE:
2263 queueptr = &mvm->p2p_dev_queue;
2266 WARN(1, "Can't free bcast queue on vif type %d\n",
2272 iwl_mvm_disable_txq(mvm, NULL, queueptr, IWL_MAX_TID_COUNT);
2273 if (iwl_mvm_has_new_tx_api(mvm))
2276 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2277 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2280 /* Send the FW a request to remove the station from it's internal data
2281 * structures, but DO NOT remove the entry from the local data structures. */
2282 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2284 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2287 lockdep_assert_held(&mvm->mutex);
2289 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2291 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2293 IWL_WARN(mvm, "Failed sending remove station\n");
2297 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2299 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2301 lockdep_assert_held(&mvm->mutex);
2303 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2304 ieee80211_vif_type_p2p(vif),
2305 IWL_STA_GENERAL_PURPOSE);
2308 /* Allocate a new station entry for the broadcast station to the given vif,
2309 * and send it to the FW.
2310 * Note that each P2P mac should have its own broadcast station.
2312 * @mvm: the mvm component
2313 * @vif: the interface to which the broadcast station is added
2314 * @bsta: the broadcast station to add. */
2315 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2317 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2318 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2321 lockdep_assert_held(&mvm->mutex);
2323 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2327 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2330 iwl_mvm_dealloc_int_sta(mvm, bsta);
2335 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2337 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2339 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2343 * Send the FW a request to remove the station from it's internal data
2344 * structures, and in addition remove it from the local data structure.
2346 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2350 lockdep_assert_held(&mvm->mutex);
2352 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2354 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2360 * Allocate a new station entry for the multicast station to the given vif,
2361 * and send it to the FW.
2362 * Note that each AP/GO mac should have its own multicast station.
2364 * @mvm: the mvm component
2365 * @vif: the interface to which the multicast station is added
2367 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2369 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2370 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2371 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2372 const u8 *maddr = _maddr;
2373 struct iwl_trans_txq_scd_cfg cfg = {
2374 .fifo = vif->type == NL80211_IFTYPE_AP ?
2375 IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2376 .sta_id = msta->sta_id,
2379 .frame_limit = IWL_FRAME_LIMIT,
2381 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2384 lockdep_assert_held(&mvm->mutex);
2386 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2387 vif->type != NL80211_IFTYPE_ADHOC))
2391 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2392 * invalid, so make sure we use the queue we want.
2393 * Note that this is done here as we want to avoid making DQA
2394 * changes in mac80211 layer.
2396 if (vif->type == NL80211_IFTYPE_ADHOC)
2397 mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2400 * While in previous FWs we had to exclude cab queue from TFD queue
2401 * mask, now it is needed as any other queue.
2403 if (!iwl_mvm_has_new_tx_api(mvm) &&
2404 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2405 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2407 msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2409 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2410 mvmvif->id, mvmvif->color);
2415 * Enable cab queue after the ADD_STA command is sent.
2416 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2417 * command with unknown station id, and for FW that doesn't support
2418 * station API since the cab queue is not included in the
2421 if (iwl_mvm_has_new_tx_api(mvm)) {
2422 int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2429 mvmvif->cab_queue = queue;
2430 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2431 IWL_UCODE_TLV_API_STA_TYPE))
2432 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2437 iwl_mvm_dealloc_int_sta(mvm, msta);
2441 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2442 struct ieee80211_key_conf *keyconf,
2446 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2447 struct iwl_mvm_add_sta_key_cmd cmd;
2449 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2450 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2455 /* This is a valid situation for GTK removal */
2456 if (sta_id == IWL_MVM_INVALID_STA)
2459 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2460 STA_KEY_FLG_KEYID_MSK);
2461 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2462 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2465 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2468 * The fields assigned here are in the same location at the start
2469 * of the command, so we can do this union trick.
2471 u.cmd.common.key_flags = key_flags;
2472 u.cmd.common.key_offset = keyconf->hw_key_idx;
2473 u.cmd.common.sta_id = sta_id;
2475 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2477 status = ADD_STA_SUCCESS;
2478 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2482 case ADD_STA_SUCCESS:
2483 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2487 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2495 * Send the FW a request to remove the station from it's internal data
2496 * structures, and in addition remove it from the local data structure.
2498 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2500 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2503 lockdep_assert_held(&mvm->mutex);
2505 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true);
2507 iwl_mvm_disable_txq(mvm, NULL, &mvmvif->cab_queue, 0);
2509 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2511 IWL_WARN(mvm, "Failed sending remove station\n");
2516 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2518 struct iwl_mvm_delba_data notif = {
2522 iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
2523 ¬if, sizeof(notif));
2526 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2527 struct iwl_mvm_baid_data *data)
2531 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2533 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2535 struct iwl_mvm_reorder_buffer *reorder_buf =
2536 &data->reorder_buf[i];
2537 struct iwl_mvm_reorder_buf_entry *entries =
2538 &data->entries[i * data->entries_per_queue];
2540 spin_lock_bh(&reorder_buf->lock);
2541 if (likely(!reorder_buf->num_stored)) {
2542 spin_unlock_bh(&reorder_buf->lock);
2547 * This shouldn't happen in regular DELBA since the internal
2548 * delBA notification should trigger a release of all frames in
2549 * the reorder buffer.
2553 for (j = 0; j < reorder_buf->buf_size; j++)
2554 __skb_queue_purge(&entries[j].e.frames);
2556 * Prevent timer re-arm. This prevents a very far fetched case
2557 * where we timed out on the notification. There may be prior
2558 * RX frames pending in the RX queue before the notification
2559 * that might get processed between now and the actual deletion
2560 * and we would re-arm the timer although we are deleting the
2563 reorder_buf->removed = true;
2564 spin_unlock_bh(&reorder_buf->lock);
2565 del_timer_sync(&reorder_buf->reorder_timer);
2569 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2570 struct iwl_mvm_baid_data *data,
2571 u16 ssn, u16 buf_size)
2575 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2576 struct iwl_mvm_reorder_buffer *reorder_buf =
2577 &data->reorder_buf[i];
2578 struct iwl_mvm_reorder_buf_entry *entries =
2579 &data->entries[i * data->entries_per_queue];
2582 reorder_buf->num_stored = 0;
2583 reorder_buf->head_sn = ssn;
2584 reorder_buf->buf_size = buf_size;
2585 /* rx reorder timer */
2586 timer_setup(&reorder_buf->reorder_timer,
2587 iwl_mvm_reorder_timer_expired, 0);
2588 spin_lock_init(&reorder_buf->lock);
2589 reorder_buf->mvm = mvm;
2590 reorder_buf->queue = i;
2591 reorder_buf->valid = false;
2592 for (j = 0; j < reorder_buf->buf_size; j++)
2593 __skb_queue_head_init(&entries[j].e.frames);
2597 static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
2598 struct iwl_mvm_sta *mvm_sta,
2599 bool start, int tid, u16 ssn,
2602 struct iwl_mvm_add_sta_cmd cmd = {
2603 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
2604 .sta_id = mvm_sta->sta_id,
2605 .add_modify = STA_MODE_MODIFY,
2611 cmd.add_immediate_ba_tid = tid;
2612 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2613 cmd.rx_ba_window = cpu_to_le16(buf_size);
2614 cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
2616 cmd.remove_immediate_ba_tid = tid;
2617 cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
2620 status = ADD_STA_SUCCESS;
2621 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2622 iwl_mvm_add_sta_cmd_size(mvm),
2627 switch (status & IWL_ADD_STA_STATUS_MASK) {
2628 case ADD_STA_SUCCESS:
2629 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2630 start ? "start" : "stopp");
2631 if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
2632 !(status & IWL_ADD_STA_BAID_VALID_MASK)))
2634 return u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
2635 case ADD_STA_IMMEDIATE_BA_FAILURE:
2636 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2639 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2640 start ? "start" : "stopp", status);
2645 static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm,
2646 struct iwl_mvm_sta *mvm_sta,
2647 bool start, int tid, u16 ssn,
2648 u16 buf_size, int baid)
2650 struct iwl_rx_baid_cfg_cmd cmd = {
2651 .action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) :
2652 cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
2654 u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
2657 BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
2660 cmd.alloc.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
2661 cmd.alloc.tid = tid;
2662 cmd.alloc.ssn = cpu_to_le16(ssn);
2663 cmd.alloc.win_size = cpu_to_le16(buf_size);
2665 } else if (iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1) == 1) {
2666 cmd.remove_v1.baid = cpu_to_le32(baid);
2667 BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove));
2669 cmd.remove.sta_id_mask = cpu_to_le32(BIT(mvm_sta->sta_id));
2670 cmd.remove.tid = cpu_to_le32(tid);
2673 ret = iwl_mvm_send_cmd_pdu_status(mvm, cmd_id, sizeof(cmd),
2679 /* ignore firmware baid on remove */
2683 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2684 start ? "start" : "stopp");
2686 if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
2692 static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvm_sta,
2693 bool start, int tid, u16 ssn, u16 buf_size,
2696 if (fw_has_capa(&mvm->fw->ucode_capa,
2697 IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
2698 return iwl_mvm_fw_baid_op_cmd(mvm, mvm_sta, start,
2699 tid, ssn, buf_size, baid);
2701 return iwl_mvm_fw_baid_op_sta(mvm, mvm_sta, start,
2702 tid, ssn, buf_size);
2705 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2706 int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2708 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2709 struct iwl_mvm_baid_data *baid_data = NULL;
2711 u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2714 lockdep_assert_held(&mvm->mutex);
2716 if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2717 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2721 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2722 u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2724 /* sparse doesn't like the __align() so don't check */
2727 * The division below will be OK if either the cache line size
2728 * can be divided by the entry size (ALIGN will round up) or if
2729 * if the entry size can be divided by the cache line size, in
2730 * which case the ALIGN() will do nothing.
2732 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2733 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2737 * Upward align the reorder buffer size to fill an entire cache
2738 * line for each queue, to avoid sharing cache lines between
2741 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2744 * Allocate here so if allocation fails we can bail out early
2745 * before starting the BA session in the firmware
2747 baid_data = kzalloc(sizeof(*baid_data) +
2748 mvm->trans->num_rx_queues *
2755 * This division is why we need the above BUILD_BUG_ON(),
2756 * if that doesn't hold then this will not be right.
2758 baid_data->entries_per_queue =
2759 reorder_buf_size / sizeof(baid_data->entries[0]);
2762 if (iwl_mvm_has_new_rx_api(mvm) && !start) {
2763 baid = mvm_sta->tid_to_baid[tid];
2765 /* we don't really need it in this case */
2769 /* Don't send command to remove (start=0) BAID during restart */
2770 if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
2771 baid = iwl_mvm_fw_baid_op(mvm, mvm_sta, start, tid, ssn, buf_size,
2780 mvm->rx_ba_sessions++;
2782 if (!iwl_mvm_has_new_rx_api(mvm))
2785 baid_data->baid = baid;
2786 baid_data->timeout = timeout;
2787 baid_data->last_rx = jiffies;
2788 baid_data->rcu_ptr = &mvm->baid_map[baid];
2789 timer_setup(&baid_data->session_timer,
2790 iwl_mvm_rx_agg_session_expired, 0);
2791 baid_data->mvm = mvm;
2792 baid_data->tid = tid;
2793 baid_data->sta_id = mvm_sta->sta_id;
2795 mvm_sta->tid_to_baid[tid] = baid;
2797 mod_timer(&baid_data->session_timer,
2798 TU_TO_EXP_TIME(timeout * 2));
2800 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2802 * protect the BA data with RCU to cover a case where our
2803 * internal RX sync mechanism will timeout (not that it's
2804 * supposed to happen) and we will free the session data while
2805 * RX is being processed in parallel
2807 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2808 mvm_sta->sta_id, tid, baid);
2809 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2810 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2812 baid = mvm_sta->tid_to_baid[tid];
2814 if (mvm->rx_ba_sessions > 0)
2815 /* check that restart flow didn't zero the counter */
2816 mvm->rx_ba_sessions--;
2817 if (!iwl_mvm_has_new_rx_api(mvm))
2820 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2823 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2824 if (WARN_ON(!baid_data))
2827 /* synchronize all rx queues so we can safely delete */
2828 iwl_mvm_free_reorder(mvm, baid_data);
2829 del_timer_sync(&baid_data->session_timer);
2830 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2831 kfree_rcu(baid_data, rcu_head);
2832 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2835 * After we've deleted it, do another queue sync
2836 * so if an IWL_MVM_RXQ_NSSN_SYNC was concurrently
2837 * running it won't find a new session in the old
2838 * BAID. It can find the NULL pointer for the BAID,
2839 * but we must not have it find a different session.
2841 iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY,
2851 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2852 int tid, u8 queue, bool start)
2854 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2855 struct iwl_mvm_add_sta_cmd cmd = {};
2859 lockdep_assert_held(&mvm->mutex);
2862 mvm_sta->tfd_queue_msk |= BIT(queue);
2863 mvm_sta->tid_disable_agg &= ~BIT(tid);
2865 /* In DQA-mode the queue isn't removed on agg termination */
2866 mvm_sta->tid_disable_agg |= BIT(tid);
2869 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2870 cmd.sta_id = mvm_sta->sta_id;
2871 cmd.add_modify = STA_MODE_MODIFY;
2872 if (!iwl_mvm_has_new_tx_api(mvm))
2873 cmd.modify_mask = STA_MODIFY_QUEUES;
2874 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2875 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2876 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2878 status = ADD_STA_SUCCESS;
2879 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2880 iwl_mvm_add_sta_cmd_size(mvm),
2885 switch (status & IWL_ADD_STA_STATUS_MASK) {
2886 case ADD_STA_SUCCESS:
2890 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2891 start ? "start" : "stopp", status);
2898 const u8 tid_to_mac80211_ac[] = {
2907 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2910 static const u8 tid_to_ucode_ac[] = {
2921 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2922 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2924 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2925 struct iwl_mvm_tid_data *tid_data;
2930 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2933 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2934 mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2936 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2937 mvmsta->tid_data[tid].state);
2941 lockdep_assert_held(&mvm->mutex);
2943 if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2944 iwl_mvm_has_new_tx_api(mvm)) {
2945 u8 ac = tid_to_mac80211_ac[tid];
2947 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2952 spin_lock_bh(&mvmsta->lock);
2955 * Note the possible cases:
2956 * 1. An enabled TXQ - TXQ needs to become agg'ed
2957 * 2. The TXQ hasn't yet been enabled, so find a free one and mark
2960 txq_id = mvmsta->tid_data[tid].txq_id;
2961 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2962 ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2963 IWL_MVM_DQA_MIN_DATA_QUEUE,
2964 IWL_MVM_DQA_MAX_DATA_QUEUE);
2966 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2972 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2973 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2974 } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2976 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2977 tid, IWL_MAX_HW_QUEUES - 1);
2980 } else if (unlikely(mvm->queue_info[txq_id].status ==
2981 IWL_MVM_QUEUE_SHARED)) {
2983 IWL_DEBUG_TX_QUEUES(mvm,
2984 "Can't start tid %d agg on shared queue!\n",
2989 IWL_DEBUG_TX_QUEUES(mvm,
2990 "AGG for tid %d will be on queue #%d\n",
2993 tid_data = &mvmsta->tid_data[tid];
2994 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2995 tid_data->txq_id = txq_id;
2996 *ssn = tid_data->ssn;
2998 IWL_DEBUG_TX_QUEUES(mvm,
2999 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
3000 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
3001 tid_data->next_reclaimed);
3004 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3005 * to align the wrap around of ssn so we compare relevant values.
3007 normalized_ssn = tid_data->ssn;
3008 if (mvm->trans->trans_cfg->gen2)
3009 normalized_ssn &= 0xff;
3011 if (normalized_ssn == tid_data->next_reclaimed) {
3012 tid_data->state = IWL_AGG_STARTING;
3013 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
3015 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
3016 ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
3020 spin_unlock_bh(&mvmsta->lock);
3025 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3026 struct ieee80211_sta *sta, u16 tid, u16 buf_size,
3029 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3030 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3031 unsigned int wdg_timeout =
3032 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
3034 bool alloc_queue = true;
3035 enum iwl_mvm_queue_status queue_status;
3038 struct iwl_trans_txq_scd_cfg cfg = {
3039 .sta_id = mvmsta->sta_id,
3041 .frame_limit = buf_size,
3046 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
3047 * manager, so this function should never be called in this case.
3049 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
3052 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3053 != IWL_MAX_TID_COUNT);
3055 spin_lock_bh(&mvmsta->lock);
3056 ssn = tid_data->ssn;
3057 queue = tid_data->txq_id;
3058 tid_data->state = IWL_AGG_ON;
3059 mvmsta->agg_tids |= BIT(tid);
3060 tid_data->ssn = 0xffff;
3061 tid_data->amsdu_in_ampdu_allowed = amsdu;
3062 spin_unlock_bh(&mvmsta->lock);
3064 if (iwl_mvm_has_new_tx_api(mvm)) {
3066 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
3067 * would have failed, so if we are here there is no need to
3069 * However, if aggregation size is different than the default
3070 * size, the scheduler should be reconfigured.
3071 * We cannot do this with the new TX API, so return unsupported
3072 * for now, until it will be offloaded to firmware..
3073 * Note that if SCD default value changes - this condition
3074 * should be updated as well.
3076 if (buf_size < IWL_FRAME_LIMIT)
3079 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3085 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3087 queue_status = mvm->queue_info[queue].status;
3089 /* Maybe there is no need to even alloc a queue... */
3090 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3091 alloc_queue = false;
3094 * Only reconfig the SCD for the queue if the window size has
3095 * changed from current (become smaller)
3097 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3099 * If reconfiguring an existing queue, it first must be
3102 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3106 "Error draining queue before reconfig\n");
3110 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3111 mvmsta->sta_id, tid,
3115 "Error reconfiguring TXQ #%d\n", queue);
3121 iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3124 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
3125 if (queue_status != IWL_MVM_QUEUE_SHARED) {
3126 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3131 /* No need to mark as reserved */
3132 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3136 * Even though in theory the peer could have different
3137 * aggregation reorder buffer sizes for different sessions,
3138 * our ucode doesn't allow for that and has a global limit
3139 * for each station. Therefore, use the minimum of all the
3140 * aggregation sessions and our default value.
3142 mvmsta->max_agg_bufsize =
3143 min(mvmsta->max_agg_bufsize, buf_size);
3144 mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
3146 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3149 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3152 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3153 struct iwl_mvm_sta *mvmsta,
3154 struct iwl_mvm_tid_data *tid_data)
3156 u16 txq_id = tid_data->txq_id;
3158 lockdep_assert_held(&mvm->mutex);
3160 if (iwl_mvm_has_new_tx_api(mvm))
3164 * The TXQ is marked as reserved only if no traffic came through yet
3165 * This means no traffic has been sent on this TID (agg'd or not), so
3166 * we no longer have use for the queue. Since it hasn't even been
3167 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3170 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3171 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3172 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3176 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3177 struct ieee80211_sta *sta, u16 tid)
3179 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3180 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3185 * If mac80211 is cleaning its state, then say that we finished since
3186 * our state has been cleared anyway.
3188 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3189 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3193 spin_lock_bh(&mvmsta->lock);
3195 txq_id = tid_data->txq_id;
3197 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3198 mvmsta->sta_id, tid, txq_id, tid_data->state);
3200 mvmsta->agg_tids &= ~BIT(tid);
3202 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3204 switch (tid_data->state) {
3206 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3208 IWL_DEBUG_TX_QUEUES(mvm,
3209 "ssn = %d, next_recl = %d\n",
3210 tid_data->ssn, tid_data->next_reclaimed);
3212 tid_data->ssn = 0xffff;
3213 tid_data->state = IWL_AGG_OFF;
3214 spin_unlock_bh(&mvmsta->lock);
3216 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3218 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3220 case IWL_AGG_STARTING:
3221 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3223 * The agg session has been stopped before it was set up. This
3224 * can happen when the AddBA timer times out for example.
3227 /* No barriers since we are under mutex */
3228 lockdep_assert_held(&mvm->mutex);
3230 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3231 tid_data->state = IWL_AGG_OFF;
3236 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3237 mvmsta->sta_id, tid, tid_data->state);
3239 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
3243 spin_unlock_bh(&mvmsta->lock);
3248 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3249 struct ieee80211_sta *sta, u16 tid)
3251 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3252 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3254 enum iwl_mvm_agg_state old_state;
3257 * First set the agg state to OFF to avoid calling
3258 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3260 spin_lock_bh(&mvmsta->lock);
3261 txq_id = tid_data->txq_id;
3262 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3263 mvmsta->sta_id, tid, txq_id, tid_data->state);
3264 old_state = tid_data->state;
3265 tid_data->state = IWL_AGG_OFF;
3266 mvmsta->agg_tids &= ~BIT(tid);
3267 spin_unlock_bh(&mvmsta->lock);
3269 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3271 if (old_state >= IWL_AGG_ON) {
3272 iwl_mvm_drain_sta(mvm, mvmsta, true);
3274 if (iwl_mvm_has_new_tx_api(mvm)) {
3275 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3277 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3278 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3280 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3281 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3282 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3285 iwl_mvm_drain_sta(mvm, mvmsta, false);
3287 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3293 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3295 int i, max = -1, max_offs = -1;
3297 lockdep_assert_held(&mvm->mutex);
3299 /* Pick the unused key offset with the highest 'deleted'
3300 * counter. Every time a key is deleted, all the counters
3301 * are incremented and the one that was just deleted is
3302 * reset to zero. Thus, the highest counter is the one
3303 * that was deleted longest ago. Pick that one.
3305 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3306 if (test_bit(i, mvm->fw_key_table))
3308 if (mvm->fw_key_deleted[i] > max) {
3309 max = mvm->fw_key_deleted[i];
3315 return STA_KEY_IDX_INVALID;
3320 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3321 struct ieee80211_vif *vif,
3322 struct ieee80211_sta *sta)
3324 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3327 return iwl_mvm_sta_from_mac80211(sta);
3330 * The device expects GTKs for station interfaces to be
3331 * installed as GTKs for the AP station. If we have no
3332 * station ID, then use AP's station ID.
3334 if (vif->type == NL80211_IFTYPE_STATION &&
3335 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3336 u8 sta_id = mvmvif->ap_sta_id;
3338 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3339 lockdep_is_held(&mvm->mutex));
3342 * It is possible that the 'sta' parameter is NULL,
3343 * for example when a GTK is removed - the sta_id will then
3344 * be the AP ID, and no station was passed by mac80211.
3346 if (IS_ERR_OR_NULL(sta))
3349 return iwl_mvm_sta_from_mac80211(sta);
3355 static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3359 for (i = len - 1; i >= 0; i--) {
3360 if (pn1[i] > pn2[i])
3362 if (pn1[i] < pn2[i])
3369 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3371 struct ieee80211_key_conf *key, bool mcast,
3372 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3373 u8 key_offset, bool mfp)
3376 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3377 struct iwl_mvm_add_sta_key_cmd cmd;
3385 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3386 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3387 int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3390 if (sta_id == IWL_MVM_INVALID_STA)
3393 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3394 STA_KEY_FLG_KEYID_MSK;
3395 key_flags = cpu_to_le16(keyidx);
3396 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3398 switch (key->cipher) {
3399 case WLAN_CIPHER_SUITE_TKIP:
3400 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3402 memcpy((void *)&u.cmd.tx_mic_key,
3403 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3406 memcpy((void *)&u.cmd.rx_mic_key,
3407 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3409 pn = atomic64_read(&key->tx_pn);
3412 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3413 for (i = 0; i < 5; i++)
3414 u.cmd_v1.tkip_rx_ttak[i] =
3415 cpu_to_le16(tkip_p1k[i]);
3417 memcpy(u.cmd.common.key, key->key, key->keylen);
3419 case WLAN_CIPHER_SUITE_CCMP:
3420 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3421 memcpy(u.cmd.common.key, key->key, key->keylen);
3423 pn = atomic64_read(&key->tx_pn);
3425 case WLAN_CIPHER_SUITE_WEP104:
3426 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3428 case WLAN_CIPHER_SUITE_WEP40:
3429 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3430 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3432 case WLAN_CIPHER_SUITE_GCMP_256:
3433 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3435 case WLAN_CIPHER_SUITE_GCMP:
3436 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3437 memcpy(u.cmd.common.key, key->key, key->keylen);
3439 pn = atomic64_read(&key->tx_pn);
3442 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3443 memcpy(u.cmd.common.key, key->key, key->keylen);
3447 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3449 key_flags |= cpu_to_le16(STA_KEY_MFP);
3451 u.cmd.common.key_offset = key_offset;
3452 u.cmd.common.key_flags = key_flags;
3453 u.cmd.common.sta_id = sta_id;
3455 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3460 for (; i < IEEE80211_NUM_TIDS; i++) {
3461 struct ieee80211_key_seq seq = {};
3462 u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3464 /* there's a hole at 2/3 in FW format depending on version */
3465 int hole = api_ver >= 3 ? 0 : 2;
3467 ieee80211_get_key_rx_seq(key, i, &seq);
3469 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3470 rx_pn[0] = seq.tkip.iv16;
3471 rx_pn[1] = seq.tkip.iv16 >> 8;
3472 rx_pn[2 + hole] = seq.tkip.iv32;
3473 rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3474 rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3475 rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3476 } else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3478 rx_pn_len = seq.hw.seq_len;
3480 rx_pn[0] = seq.ccmp.pn[0];
3481 rx_pn[1] = seq.ccmp.pn[1];
3482 rx_pn[2 + hole] = seq.ccmp.pn[2];
3483 rx_pn[3 + hole] = seq.ccmp.pn[3];
3484 rx_pn[4 + hole] = seq.ccmp.pn[4];
3485 rx_pn[5 + hole] = seq.ccmp.pn[5];
3488 if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3490 memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3495 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3496 size = sizeof(u.cmd);
3498 size = sizeof(u.cmd_v1);
3501 status = ADD_STA_SUCCESS;
3502 if (cmd_flags & CMD_ASYNC)
3503 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3506 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3510 case ADD_STA_SUCCESS:
3511 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3515 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3522 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3523 struct ieee80211_key_conf *keyconf,
3524 u8 sta_id, bool remove_key)
3526 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3528 /* verify the key details match the required command's expectations */
3529 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3530 (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3531 keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
3532 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3533 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3534 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3537 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3538 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3541 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3542 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3545 /* This is a valid situation for IGTK */
3546 if (sta_id == IWL_MVM_INVALID_STA)
3549 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3551 struct ieee80211_key_seq seq;
3554 switch (keyconf->cipher) {
3555 case WLAN_CIPHER_SUITE_AES_CMAC:
3556 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3558 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3559 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3560 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3566 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3567 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3568 igtk_cmd.ctrl_flags |=
3569 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3570 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3571 pn = seq.aes_cmac.pn;
3572 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3573 ((u64) pn[4] << 8) |
3574 ((u64) pn[3] << 16) |
3575 ((u64) pn[2] << 24) |
3576 ((u64) pn[1] << 32) |
3577 ((u64) pn[0] << 40));
3580 IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3581 remove_key ? "removing" : "installing",
3582 keyconf->keyidx >= 6 ? "B" : "",
3583 keyconf->keyidx, igtk_cmd.sta_id);
3585 if (!iwl_mvm_has_new_rx_api(mvm)) {
3586 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3587 .ctrl_flags = igtk_cmd.ctrl_flags,
3588 .key_id = igtk_cmd.key_id,
3589 .sta_id = igtk_cmd.sta_id,
3590 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3593 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3594 ARRAY_SIZE(igtk_cmd_v1.igtk));
3595 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3596 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3598 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3599 sizeof(igtk_cmd), &igtk_cmd);
3603 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3604 struct ieee80211_vif *vif,
3605 struct ieee80211_sta *sta)
3607 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3612 if (vif->type == NL80211_IFTYPE_STATION &&
3613 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3614 u8 sta_id = mvmvif->ap_sta_id;
3615 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3616 lockdep_is_held(&mvm->mutex));
3624 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3625 struct ieee80211_vif *vif,
3626 struct ieee80211_sta *sta,
3627 struct ieee80211_key_conf *keyconf,
3632 struct ieee80211_key_seq seq;
3638 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3640 sta_id = mvm_sta->sta_id;
3642 } else if (vif->type == NL80211_IFTYPE_AP &&
3643 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3644 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3646 sta_id = mvmvif->mcast_sta.sta_id;
3648 IWL_ERR(mvm, "Failed to find station id\n");
3652 if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3653 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3654 /* get phase 1 key from mac80211 */
3655 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3656 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3658 return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3659 seq.tkip.iv32, p1k, 0, key_offset,
3663 return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3664 0, NULL, 0, key_offset, mfp);
3667 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3668 struct ieee80211_vif *vif,
3669 struct ieee80211_sta *sta,
3670 struct ieee80211_key_conf *keyconf,
3673 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3674 struct iwl_mvm_sta *mvm_sta;
3675 u8 sta_id = IWL_MVM_INVALID_STA;
3677 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3679 lockdep_assert_held(&mvm->mutex);
3681 if (vif->type != NL80211_IFTYPE_AP ||
3682 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3683 /* Get the station id from the mvm local station table */
3684 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3686 IWL_ERR(mvm, "Failed to find station\n");
3689 sta_id = mvm_sta->sta_id;
3692 * It is possible that the 'sta' parameter is NULL, and thus
3693 * there is a need to retrieve the sta from the local station
3697 sta = rcu_dereference_protected(
3698 mvm->fw_id_to_mac_id[sta_id],
3699 lockdep_is_held(&mvm->mutex));
3700 if (IS_ERR_OR_NULL(sta)) {
3701 IWL_ERR(mvm, "Invalid station id\n");
3706 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3709 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3711 sta_id = mvmvif->mcast_sta.sta_id;
3714 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3715 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3716 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3717 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3721 /* If the key_offset is not pre-assigned, we need to find a
3722 * new offset to use. In normal cases, the offset is not
3723 * pre-assigned, but during HW_RESTART we want to reuse the
3724 * same indices, so we pass them when this function is called.
3726 * In D3 entry, we need to hardcoded the indices (because the
3727 * firmware hardcodes the PTK offset to 0). In this case, we
3728 * need to make sure we don't overwrite the hw_key_idx in the
3729 * keyconf structure, because otherwise we cannot configure
3730 * the original ones back when resuming.
3732 if (key_offset == STA_KEY_IDX_INVALID) {
3733 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3734 if (key_offset == STA_KEY_IDX_INVALID)
3736 keyconf->hw_key_idx = key_offset;
3739 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3744 * For WEP, the same key is used for multicast and unicast. Upload it
3745 * again, using the same key offset, and now pointing the other one
3746 * to the same key slot (offset).
3747 * If this fails, remove the original as well.
3749 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3750 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3752 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3753 key_offset, !mcast);
3755 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3760 __set_bit(key_offset, mvm->fw_key_table);
3763 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3764 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3765 sta ? sta->addr : zero_addr, ret);
3769 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3770 struct ieee80211_vif *vif,
3771 struct ieee80211_sta *sta,
3772 struct ieee80211_key_conf *keyconf)
3774 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3775 struct iwl_mvm_sta *mvm_sta;
3776 u8 sta_id = IWL_MVM_INVALID_STA;
3779 lockdep_assert_held(&mvm->mutex);
3781 /* Get the station from the mvm local station table */
3782 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3784 sta_id = mvm_sta->sta_id;
3785 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3786 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3789 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3790 keyconf->keyidx, sta_id);
3792 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3793 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3794 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3795 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3797 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3798 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3799 keyconf->hw_key_idx);
3803 /* track which key was deleted last */
3804 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3805 if (mvm->fw_key_deleted[i] < U8_MAX)
3806 mvm->fw_key_deleted[i]++;
3808 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3810 if (sta && !mvm_sta) {
3811 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3815 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3819 /* delete WEP key twice to get rid of (now useless) offset */
3820 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3821 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3822 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3827 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3828 struct ieee80211_vif *vif,
3829 struct ieee80211_key_conf *keyconf,
3830 struct ieee80211_sta *sta, u32 iv32,
3833 struct iwl_mvm_sta *mvm_sta;
3834 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3835 bool mfp = sta ? sta->mfp : false;
3839 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3840 if (WARN_ON_ONCE(!mvm_sta))
3842 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3843 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3850 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3851 struct ieee80211_sta *sta)
3853 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3854 struct iwl_mvm_add_sta_cmd cmd = {
3855 .add_modify = STA_MODE_MODIFY,
3856 .sta_id = mvmsta->sta_id,
3857 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3858 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3862 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3863 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3865 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3868 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3869 struct ieee80211_sta *sta,
3870 enum ieee80211_frame_release_type reason,
3871 u16 cnt, u16 tids, bool more_data,
3872 bool single_sta_queue)
3874 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3875 struct iwl_mvm_add_sta_cmd cmd = {
3876 .add_modify = STA_MODE_MODIFY,
3877 .sta_id = mvmsta->sta_id,
3878 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3879 .sleep_tx_count = cpu_to_le16(cnt),
3880 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3883 unsigned long _tids = tids;
3885 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3886 * Note that this field is reserved and unused by firmware not
3887 * supporting GO uAPSD, so it's safe to always do this.
3889 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3890 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3892 /* If we're releasing frames from aggregation or dqa queues then check
3893 * if all the queues that we're releasing frames from, combined, have:
3894 * - more frames than the service period, in which case more_data
3896 * - fewer than 'cnt' frames, in which case we need to adjust the
3897 * firmware command (but do that unconditionally)
3899 if (single_sta_queue) {
3900 int remaining = cnt;
3903 spin_lock_bh(&mvmsta->lock);
3904 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3905 struct iwl_mvm_tid_data *tid_data;
3908 tid_data = &mvmsta->tid_data[tid];
3910 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3911 if (n_queued > remaining) {
3916 remaining -= n_queued;
3918 sleep_tx_count = cnt - remaining;
3919 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3920 mvmsta->sleep_tx_count = sleep_tx_count;
3921 spin_unlock_bh(&mvmsta->lock);
3923 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3924 if (WARN_ON(cnt - remaining == 0)) {
3925 ieee80211_sta_eosp(sta);
3930 /* Note: this is ignored by firmware not supporting GO uAPSD */
3932 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3934 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3935 mvmsta->next_status_eosp = true;
3936 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3938 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3941 /* block the Tx queues until the FW updated the sleep Tx count */
3942 iwl_trans_block_txq_ptrs(mvm->trans, true);
3944 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3945 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3946 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3948 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3951 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3952 struct iwl_rx_cmd_buffer *rxb)
3954 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3955 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3956 struct ieee80211_sta *sta;
3957 u32 sta_id = le32_to_cpu(notif->sta_id);
3959 if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
3963 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3964 if (!IS_ERR_OR_NULL(sta))
3965 ieee80211_sta_eosp(sta);
3969 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3970 struct iwl_mvm_sta *mvmsta, bool disable)
3972 struct iwl_mvm_add_sta_cmd cmd = {
3973 .add_modify = STA_MODE_MODIFY,
3974 .sta_id = mvmsta->sta_id,
3975 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3976 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3977 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3981 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3982 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3984 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3987 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3988 struct ieee80211_sta *sta,
3991 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3993 spin_lock_bh(&mvm_sta->lock);
3995 if (mvm_sta->disable_tx == disable) {
3996 spin_unlock_bh(&mvm_sta->lock);
4000 mvm_sta->disable_tx = disable;
4003 * If sta PS state is handled by mac80211, tell it to start/stop
4004 * queuing tx for this station.
4006 if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
4007 ieee80211_sta_block_awake(mvm->hw, sta, disable);
4009 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
4011 spin_unlock_bh(&mvm_sta->lock);
4014 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
4015 struct iwl_mvm_vif *mvmvif,
4016 struct iwl_mvm_int_sta *sta,
4019 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
4020 struct iwl_mvm_add_sta_cmd cmd = {
4021 .add_modify = STA_MODE_MODIFY,
4022 .sta_id = sta->sta_id,
4023 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4024 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4025 .mac_id_n_color = cpu_to_le32(id),
4029 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4030 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4032 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4035 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
4036 struct iwl_mvm_vif *mvmvif,
4039 struct ieee80211_sta *sta;
4040 struct iwl_mvm_sta *mvm_sta;
4045 /* Block/unblock all the stations of the given mvmvif */
4046 for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4047 sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
4048 if (IS_ERR_OR_NULL(sta))
4051 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4052 if (mvm_sta->mac_id_n_color !=
4053 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
4056 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
4061 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
4064 /* Need to block/unblock also multicast station */
4065 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
4066 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4067 &mvmvif->mcast_sta, disable);
4070 * Only unblock the broadcast station (FW blocks it for immediate
4071 * quiet, not the driver)
4073 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
4074 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4075 &mvmvif->bcast_sta, disable);
4078 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4080 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4081 struct iwl_mvm_sta *mvmsta;
4085 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
4088 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
4093 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
4095 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
4098 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
4099 * to align the wrap around of ssn so we compare relevant values.
4101 if (mvm->trans->trans_cfg->gen2)
4104 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4107 int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4108 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
4109 u8 *key, u32 key_len)
4113 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4114 struct ieee80211_key_conf *keyconf;
4116 ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
4117 NL80211_IFTYPE_UNSPECIFIED,
4122 ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
4124 IWL_MVM_TX_FIFO_BE);
4128 keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
4134 keyconf->cipher = cipher;
4135 memcpy(keyconf->key, key, key_len);
4136 keyconf->keylen = key_len;
4138 ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
4139 0, NULL, 0, 0, true);
4143 iwl_mvm_dealloc_int_sta(mvm, sta);
4147 void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4148 struct ieee80211_vif *vif,
4151 struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4152 .mac_id = cpu_to_le32(mac_id),
4156 ret = iwl_mvm_send_cmd_pdu(mvm,
4157 WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4159 sizeof(cancel_channel_switch_cmd),
4160 &cancel_channel_switch_cmd);
4162 IWL_ERR(mvm, "Failed to cancel the channel switch\n");