1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11 * Copyright(c) 2018 - 2019 Intel Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of version 2 of the GNU General Public License as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
25 * Contact Information:
26 * Intel Linux Wireless <linuxwifi@intel.com>
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
32 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34 * Copyright(c) 2018 - 2019 Intel Corporation
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
47 * * Neither the name Intel Corporation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *****************************************************************************/
64 #include <net/mac80211.h>
70 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm);
72 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
74 struct ieee80211_key_conf *key, bool mcast,
75 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
76 u8 key_offset, bool mfp);
79 * New version of ADD_STA_sta command added new fields at the end of the
80 * structure, so sending the size of the relevant API's structure is enough to
81 * support both API versions.
83 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
85 if (iwl_mvm_has_new_rx_api(mvm) ||
86 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
87 return sizeof(struct iwl_mvm_add_sta_cmd);
89 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
92 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
93 enum nl80211_iftype iftype)
98 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
99 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
101 lockdep_assert_held(&mvm->mutex);
103 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
104 if (iftype != NL80211_IFTYPE_STATION)
105 reserved_ids = BIT(0);
107 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
108 for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
109 if (BIT(sta_id) & reserved_ids)
112 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
113 lockdep_is_held(&mvm->mutex)))
116 return IWL_MVM_INVALID_STA;
119 /* send station add/update command to firmware */
120 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
121 bool update, unsigned int flags)
123 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
124 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
125 .sta_id = mvm_sta->sta_id,
126 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
127 .add_modify = update ? 1 : 0,
128 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
129 STA_FLG_MIMO_EN_MSK |
130 STA_FLG_RTS_MIMO_PROT),
131 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
135 u32 agg_size = 0, mpdu_dens = 0;
137 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
138 add_sta_cmd.station_type = mvm_sta->sta_type;
140 if (!update || (flags & STA_MODIFY_QUEUES)) {
141 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
143 if (!iwl_mvm_has_new_tx_api(mvm)) {
144 add_sta_cmd.tfd_queue_msk =
145 cpu_to_le32(mvm_sta->tfd_queue_msk);
147 if (flags & STA_MODIFY_QUEUES)
148 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
150 WARN_ON(flags & STA_MODIFY_QUEUES);
154 switch (sta->bandwidth) {
155 case IEEE80211_STA_RX_BW_160:
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
158 case IEEE80211_STA_RX_BW_80:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
161 case IEEE80211_STA_RX_BW_40:
162 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
164 case IEEE80211_STA_RX_BW_20:
165 if (sta->ht_cap.ht_supported)
166 add_sta_cmd.station_flags |=
167 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
171 switch (sta->rx_nss) {
173 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
176 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
179 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
183 switch (sta->smps_mode) {
184 case IEEE80211_SMPS_AUTOMATIC:
185 case IEEE80211_SMPS_NUM_MODES:
188 case IEEE80211_SMPS_STATIC:
190 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
191 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
193 case IEEE80211_SMPS_DYNAMIC:
194 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
196 case IEEE80211_SMPS_OFF:
201 if (sta->ht_cap.ht_supported) {
202 add_sta_cmd.station_flags_msk |=
203 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
204 STA_FLG_AGG_MPDU_DENS_MSK);
206 mpdu_dens = sta->ht_cap.ampdu_density;
209 if (sta->vht_cap.vht_supported) {
210 agg_size = sta->vht_cap.cap &
211 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
213 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
214 } else if (sta->ht_cap.ht_supported) {
215 agg_size = sta->ht_cap.ampdu_factor;
218 add_sta_cmd.station_flags |=
219 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
220 add_sta_cmd.station_flags |=
221 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
222 if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
223 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
226 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
228 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
229 add_sta_cmd.uapsd_acs |= BIT(AC_BK);
230 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
231 add_sta_cmd.uapsd_acs |= BIT(AC_BE);
232 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
233 add_sta_cmd.uapsd_acs |= BIT(AC_VI);
234 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
235 add_sta_cmd.uapsd_acs |= BIT(AC_VO);
236 add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
237 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
240 status = ADD_STA_SUCCESS;
241 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
242 iwl_mvm_add_sta_cmd_size(mvm),
243 &add_sta_cmd, &status);
247 switch (status & IWL_ADD_STA_STATUS_MASK) {
248 case ADD_STA_SUCCESS:
249 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
253 IWL_ERR(mvm, "ADD_STA failed\n");
260 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
262 struct iwl_mvm_baid_data *data =
263 from_timer(data, t, session_timer);
264 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
265 struct iwl_mvm_baid_data *ba_data;
266 struct ieee80211_sta *sta;
267 struct iwl_mvm_sta *mvm_sta;
268 unsigned long timeout;
272 ba_data = rcu_dereference(*rcu_ptr);
274 if (WARN_ON(!ba_data))
277 if (!ba_data->timeout)
280 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
281 if (time_is_after_jiffies(timeout)) {
282 mod_timer(&ba_data->session_timer, timeout);
287 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
290 * sta should be valid unless the following happens:
291 * The firmware asserts which triggers a reconfig flow, but
292 * the reconfig fails before we set the pointer to sta into
293 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
294 * A-MDPU and hence the timer continues to run. Then, the
295 * timer expires and sta is NULL.
300 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
301 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
302 sta->addr, ba_data->tid);
307 /* Disable aggregations for a bitmap of TIDs for a given station */
308 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
309 unsigned long disable_agg_tids,
312 struct iwl_mvm_add_sta_cmd cmd = {};
313 struct ieee80211_sta *sta;
314 struct iwl_mvm_sta *mvmsta;
318 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
321 sta_id = mvm->queue_info[queue].ra_sta_id;
325 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
327 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
332 mvmsta = iwl_mvm_sta_from_mac80211(sta);
334 mvmsta->tid_disable_agg |= disable_agg_tids;
336 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
337 cmd.sta_id = mvmsta->sta_id;
338 cmd.add_modify = STA_MODE_MODIFY;
339 cmd.modify_mask = STA_MODIFY_QUEUES;
340 if (disable_agg_tids)
341 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
343 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
344 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
345 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
349 /* Notify FW of queue removal from the STA queues */
350 status = ADD_STA_SUCCESS;
351 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
352 iwl_mvm_add_sta_cmd_size(mvm),
356 static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
357 int queue, u8 tid, u8 flags)
359 struct iwl_scd_txq_cfg_cmd cmd = {
361 .action = SCD_CFG_DISABLE_QUEUE,
365 if (iwl_mvm_has_new_tx_api(mvm)) {
366 iwl_trans_txq_free(mvm->trans, queue);
371 if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
374 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
376 cmd.action = mvm->queue_info[queue].tid_bitmap ?
377 SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
378 if (cmd.action == SCD_CFG_DISABLE_QUEUE)
379 mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
381 IWL_DEBUG_TX_QUEUES(mvm,
382 "Disabling TXQ #%d tids=0x%x\n",
384 mvm->queue_info[queue].tid_bitmap);
386 /* If the queue is still enabled - nothing left to do in this func */
387 if (cmd.action == SCD_CFG_ENABLE_QUEUE)
390 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
391 cmd.tid = mvm->queue_info[queue].txq_tid;
393 /* Make sure queue info is correct even though we overwrite it */
394 WARN(mvm->queue_info[queue].tid_bitmap,
395 "TXQ #%d info out-of-sync - tids=0x%x\n",
396 queue, mvm->queue_info[queue].tid_bitmap);
398 /* If we are here - the queue is freed and we can zero out these vals */
399 mvm->queue_info[queue].tid_bitmap = 0;
402 struct iwl_mvm_txq *mvmtxq =
403 iwl_mvm_txq_from_tid(sta, tid);
405 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
408 /* Regardless if this is a reserved TXQ for a STA - mark it as false */
409 mvm->queue_info[queue].reserved = false;
411 iwl_trans_txq_disable(mvm->trans, queue, false);
412 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
413 sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
416 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
421 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
423 struct ieee80211_sta *sta;
424 struct iwl_mvm_sta *mvmsta;
425 unsigned long tid_bitmap;
426 unsigned long agg_tids = 0;
430 lockdep_assert_held(&mvm->mutex);
432 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
435 sta_id = mvm->queue_info[queue].ra_sta_id;
436 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
438 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
439 lockdep_is_held(&mvm->mutex));
441 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
444 mvmsta = iwl_mvm_sta_from_mac80211(sta);
446 spin_lock_bh(&mvmsta->lock);
447 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
448 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
449 agg_tids |= BIT(tid);
451 spin_unlock_bh(&mvmsta->lock);
457 * Remove a queue from a station's resources.
458 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
459 * doesn't disable the queue
461 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
463 struct ieee80211_sta *sta;
464 struct iwl_mvm_sta *mvmsta;
465 unsigned long tid_bitmap;
466 unsigned long disable_agg_tids = 0;
470 lockdep_assert_held(&mvm->mutex);
472 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
475 sta_id = mvm->queue_info[queue].ra_sta_id;
476 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
480 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
482 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
487 mvmsta = iwl_mvm_sta_from_mac80211(sta);
489 spin_lock_bh(&mvmsta->lock);
490 /* Unmap MAC queues and TIDs from this queue */
491 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
492 struct iwl_mvm_txq *mvmtxq =
493 iwl_mvm_txq_from_tid(sta, tid);
495 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
496 disable_agg_tids |= BIT(tid);
497 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
499 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
502 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
503 spin_unlock_bh(&mvmsta->lock);
508 * The TX path may have been using this TXQ_ID from the tid_data,
509 * so make sure it's no longer running so that we can safely reuse
510 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
511 * above, but nothing guarantees we've stopped using them. Thus,
512 * without this, we could get to iwl_mvm_disable_txq() and remove
513 * the queue while still sending frames to it.
517 return disable_agg_tids;
520 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
521 struct ieee80211_sta *old_sta,
524 struct iwl_mvm_sta *mvmsta;
526 unsigned long disable_agg_tids = 0;
530 lockdep_assert_held(&mvm->mutex);
532 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
535 sta_id = mvm->queue_info[queue].ra_sta_id;
536 tid = mvm->queue_info[queue].txq_tid;
538 same_sta = sta_id == new_sta_id;
540 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
541 if (WARN_ON(!mvmsta))
544 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
545 /* Disable the queue */
546 if (disable_agg_tids)
547 iwl_mvm_invalidate_sta_queue(mvm, queue,
548 disable_agg_tids, false);
550 ret = iwl_mvm_disable_txq(mvm, old_sta, queue, tid, 0);
553 "Failed to free inactive queue %d (ret=%d)\n",
559 /* If TXQ is allocated to another STA, update removal in FW */
561 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
566 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
567 unsigned long tfd_queue_mask, u8 ac)
570 u8 ac_to_queue[IEEE80211_NUM_ACS];
574 * This protects us against grabbing a queue that's being reconfigured
575 * by the inactivity checker.
577 lockdep_assert_held(&mvm->mutex);
579 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
582 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
584 /* See what ACs the existing queues for this STA have */
585 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
586 /* Only DATA queues can be shared */
587 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
588 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
591 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
595 * The queue to share is chosen only from DATA queues as follows (in
596 * descending priority):
599 * 3. Highest AC queue that is lower than new AC
600 * 4. Any existing AC (there always is at least 1 DATA queue)
603 /* Priority 1: An AC_BE queue */
604 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
605 queue = ac_to_queue[IEEE80211_AC_BE];
606 /* Priority 2: Same AC queue */
607 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
608 queue = ac_to_queue[ac];
609 /* Priority 3a: If new AC is VO and VI exists - use VI */
610 else if (ac == IEEE80211_AC_VO &&
611 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
612 queue = ac_to_queue[IEEE80211_AC_VI];
613 /* Priority 3b: No BE so only AC less than the new one is BK */
614 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
615 queue = ac_to_queue[IEEE80211_AC_BK];
616 /* Priority 4a: No BE nor BK - use VI if exists */
617 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
618 queue = ac_to_queue[IEEE80211_AC_VI];
619 /* Priority 4b: No BE, BK nor VI - use VO if exists */
620 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
621 queue = ac_to_queue[IEEE80211_AC_VO];
623 /* Make sure queue found (or not) is legal */
624 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
625 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
626 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
627 IWL_ERR(mvm, "No DATA queues available to share\n");
635 * If a given queue has a higher AC than the TID stream that is being compared
636 * to, the queue needs to be redirected to the lower AC. This function does that
637 * in such a case, otherwise - if no redirection required - it does nothing,
638 * unless the %force param is true.
640 static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
641 int ac, int ssn, unsigned int wdg_timeout,
642 bool force, struct iwl_mvm_txq *txq)
644 struct iwl_scd_txq_cfg_cmd cmd = {
646 .action = SCD_CFG_DISABLE_QUEUE,
651 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
655 * If the AC is lower than current one - FIFO needs to be redirected to
656 * the lowest one of the streams in the queue. Check if this is needed
658 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
659 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
660 * we need to check if the numerical value of X is LARGER than of Y.
662 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
663 IWL_DEBUG_TX_QUEUES(mvm,
664 "No redirection needed on TXQ #%d\n",
669 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
670 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
671 cmd.tid = mvm->queue_info[queue].txq_tid;
672 shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
674 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
675 queue, iwl_mvm_ac_to_tx_fifo[ac]);
677 /* Stop the queue and wait for it to empty */
680 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
682 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
688 /* Before redirecting the queue we need to de-activate it */
689 iwl_trans_txq_disable(mvm->trans, queue, false);
690 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
692 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
695 /* Make sure the SCD wrptr is correctly set before reconfiguring */
696 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
698 /* Update the TID "owner" of the queue */
699 mvm->queue_info[queue].txq_tid = tid;
701 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
703 /* Redirect to lower AC */
704 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
705 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
707 /* Update AC marking of the queue */
708 mvm->queue_info[queue].mac80211_ac = ac;
711 * Mark queue as shared in transport if shared
712 * Note this has to be done after queue enablement because enablement
713 * can also set this value, and there is no indication there to shared
717 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
720 /* Continue using the queue */
721 txq->stopped = false;
726 static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
731 lockdep_assert_held(&mvm->mutex);
733 /* This should not be hit with new TX path */
734 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
737 /* Start by looking for a free queue */
738 for (i = minq; i <= maxq; i++)
739 if (mvm->queue_info[i].tid_bitmap == 0 &&
740 mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
746 static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
747 u8 sta_id, u8 tid, unsigned int timeout)
749 int queue, size = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
750 mvm->trans->cfg->min_256_ba_txq_size);
752 if (tid == IWL_MAX_TID_COUNT) {
754 size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
755 mvm->trans->cfg->min_txq_size);
757 queue = iwl_trans_txq_alloc(mvm->trans,
758 cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
759 sta_id, tid, SCD_QUEUE_CFG, size, timeout);
762 IWL_DEBUG_TX_QUEUES(mvm,
763 "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
768 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
771 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d\n", queue);
776 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
777 struct ieee80211_sta *sta, u8 ac,
780 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
781 struct iwl_mvm_txq *mvmtxq =
782 iwl_mvm_txq_from_tid(sta, tid);
783 unsigned int wdg_timeout =
784 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
787 lockdep_assert_held(&mvm->mutex);
789 IWL_DEBUG_TX_QUEUES(mvm,
790 "Allocating queue for sta %d on tid %d\n",
791 mvmsta->sta_id, tid);
792 queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
796 mvmtxq->txq_id = queue;
797 mvm->tvqm_info[queue].txq_tid = tid;
798 mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
800 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
802 spin_lock_bh(&mvmsta->lock);
803 mvmsta->tid_data[tid].txq_id = queue;
804 spin_unlock_bh(&mvmsta->lock);
809 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
810 struct ieee80211_sta *sta,
811 int queue, u8 sta_id, u8 tid)
813 bool enable_queue = true;
815 /* Make sure this TID isn't already enabled */
816 if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
817 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
822 /* Update mappings and refcounts */
823 if (mvm->queue_info[queue].tid_bitmap)
824 enable_queue = false;
826 mvm->queue_info[queue].tid_bitmap |= BIT(tid);
827 mvm->queue_info[queue].ra_sta_id = sta_id;
830 if (tid != IWL_MAX_TID_COUNT)
831 mvm->queue_info[queue].mac80211_ac =
832 tid_to_mac80211_ac[tid];
834 mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
836 mvm->queue_info[queue].txq_tid = tid;
840 struct iwl_mvm_txq *mvmtxq =
841 iwl_mvm_txq_from_tid(sta, tid);
843 mvmtxq->txq_id = queue;
846 IWL_DEBUG_TX_QUEUES(mvm,
847 "Enabling TXQ #%d tids=0x%x\n",
848 queue, mvm->queue_info[queue].tid_bitmap);
853 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
855 const struct iwl_trans_txq_scd_cfg *cfg,
856 unsigned int wdg_timeout)
858 struct iwl_scd_txq_cfg_cmd cmd = {
860 .action = SCD_CFG_ENABLE_QUEUE,
861 .window = cfg->frame_limit,
862 .sta_id = cfg->sta_id,
863 .ssn = cpu_to_le16(ssn),
864 .tx_fifo = cfg->fifo,
865 .aggregate = cfg->aggregate,
870 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
873 /* Send the enabling command if we need to */
874 if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
877 inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
880 le16_add_cpu(&cmd.ssn, 1);
882 WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
883 "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
888 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
890 struct iwl_scd_txq_cfg_cmd cmd = {
892 .action = SCD_CFG_UPDATE_QUEUE_TID,
895 unsigned long tid_bitmap;
898 lockdep_assert_held(&mvm->mutex);
900 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
903 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
905 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
908 /* Find any TID for queue */
909 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
911 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
913 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
915 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
920 mvm->queue_info[queue].txq_tid = tid;
921 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
925 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
927 struct ieee80211_sta *sta;
928 struct iwl_mvm_sta *mvmsta;
931 unsigned long tid_bitmap;
932 unsigned int wdg_timeout;
936 /* queue sharing is disabled on new TX path */
937 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
940 lockdep_assert_held(&mvm->mutex);
942 sta_id = mvm->queue_info[queue].ra_sta_id;
943 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
945 /* Find TID for queue, and make sure it is the only one on the queue */
946 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
947 if (tid_bitmap != BIT(tid)) {
948 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
953 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
956 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
957 lockdep_is_held(&mvm->mutex));
959 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
962 mvmsta = iwl_mvm_sta_from_mac80211(sta);
963 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
965 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
967 ret = iwl_mvm_redirect_queue(mvm, queue, tid,
968 tid_to_mac80211_ac[tid], ssn,
970 iwl_mvm_txq_from_tid(sta, tid));
972 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
976 /* If aggs should be turned back on - do it */
977 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
978 struct iwl_mvm_add_sta_cmd cmd = {0};
980 mvmsta->tid_disable_agg &= ~BIT(tid);
982 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
983 cmd.sta_id = mvmsta->sta_id;
984 cmd.add_modify = STA_MODE_MODIFY;
985 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
986 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
987 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
989 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
990 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
992 IWL_DEBUG_TX_QUEUES(mvm,
993 "TXQ #%d is now aggregated again\n",
996 /* Mark queue intenally as aggregating again */
997 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1001 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1005 * Remove inactive TIDs of a given queue.
1006 * If all queue TIDs are inactive - mark the queue as inactive
1007 * If only some the queue TIDs are inactive - unmap them from the queue
1009 * Returns %true if all TIDs were removed and the queue could be reused.
1011 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1012 struct iwl_mvm_sta *mvmsta, int queue,
1013 unsigned long tid_bitmap,
1014 unsigned long *unshare_queues,
1015 unsigned long *changetid_queues)
1019 lockdep_assert_held(&mvmsta->lock);
1020 lockdep_assert_held(&mvm->mutex);
1022 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1025 /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1026 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1027 /* If some TFDs are still queued - don't mark TID as inactive */
1028 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1029 tid_bitmap &= ~BIT(tid);
1031 /* Don't mark as inactive any TID that has an active BA */
1032 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1033 tid_bitmap &= ~BIT(tid);
1036 /* If all TIDs in the queue are inactive - return it can be reused */
1037 if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1038 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1043 * If we are here, this is a shared queue and not all TIDs timed-out.
1044 * Remove the ones that did.
1046 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1049 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1050 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1052 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1055 * We need to take into account a situation in which a TXQ was
1056 * allocated to TID x, and then turned shared by adding TIDs y
1057 * and z. If TID x becomes inactive and is removed from the TXQ,
1058 * ownership must be given to one of the remaining TIDs.
1059 * This is mainly because if TID x continues - a new queue can't
1060 * be allocated for it as long as it is an owner of another TXQ.
1062 * Mark this queue in the right bitmap, we'll send the command
1063 * to the firmware later.
1065 if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1066 set_bit(queue, changetid_queues);
1068 IWL_DEBUG_TX_QUEUES(mvm,
1069 "Removing inactive TID %d from shared Q:%d\n",
1073 IWL_DEBUG_TX_QUEUES(mvm,
1074 "TXQ #%d left with tid bitmap 0x%x\n", queue,
1075 mvm->queue_info[queue].tid_bitmap);
1078 * There may be different TIDs with the same mac queues, so make
1079 * sure all TIDs have existing corresponding mac queues enabled
1081 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1083 /* If the queue is marked as shared - "unshare" it */
1084 if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1085 mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1086 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1088 set_bit(queue, unshare_queues);
1095 * Check for inactivity - this includes checking if any queue
1096 * can be unshared and finding one (and only one) that can be
1098 * This function is also invoked as a sort of clean-up task,
1099 * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1101 * Returns the queue number, or -ENOSPC.
1103 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1105 unsigned long now = jiffies;
1106 unsigned long unshare_queues = 0;
1107 unsigned long changetid_queues = 0;
1108 int i, ret, free_queue = -ENOSPC;
1109 struct ieee80211_sta *queue_owner = NULL;
1111 lockdep_assert_held(&mvm->mutex);
1113 if (iwl_mvm_has_new_tx_api(mvm))
1118 /* we skip the CMD queue below by starting at 1 */
1119 BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1121 for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1122 struct ieee80211_sta *sta;
1123 struct iwl_mvm_sta *mvmsta;
1126 unsigned long inactive_tid_bitmap = 0;
1127 unsigned long queue_tid_bitmap;
1129 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1130 if (!queue_tid_bitmap)
1133 /* If TXQ isn't in active use anyway - nothing to do here... */
1134 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1135 mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1138 /* Check to see if there are inactive TIDs on this queue */
1139 for_each_set_bit(tid, &queue_tid_bitmap,
1140 IWL_MAX_TID_COUNT + 1) {
1141 if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1142 IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1145 inactive_tid_bitmap |= BIT(tid);
1148 /* If all TIDs are active - finish check on this queue */
1149 if (!inactive_tid_bitmap)
1153 * If we are here - the queue hadn't been served recently and is
1157 sta_id = mvm->queue_info[i].ra_sta_id;
1158 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1161 * If the STA doesn't exist anymore, it isn't an error. It could
1162 * be that it was removed since getting the queues, and in this
1163 * case it should've inactivated its queues anyway.
1165 if (IS_ERR_OR_NULL(sta))
1168 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1170 spin_lock_bh(&mvmsta->lock);
1171 ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1172 inactive_tid_bitmap,
1175 if (ret >= 0 && free_queue < 0) {
1179 /* only unlock sta lock - we still need the queue info lock */
1180 spin_unlock_bh(&mvmsta->lock);
1184 /* Reconfigure queues requiring reconfiguation */
1185 for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1186 iwl_mvm_unshare_queue(mvm, i);
1187 for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1188 iwl_mvm_change_queue_tid(mvm, i);
1190 if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1191 ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1204 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1205 struct ieee80211_sta *sta, u8 ac, int tid)
1207 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1208 struct iwl_trans_txq_scd_cfg cfg = {
1209 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1210 .sta_id = mvmsta->sta_id,
1212 .frame_limit = IWL_FRAME_LIMIT,
1214 unsigned int wdg_timeout =
1215 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1217 unsigned long disable_agg_tids = 0;
1218 enum iwl_mvm_agg_state queue_state;
1219 bool shared_queue = false, inc_ssn;
1221 unsigned long tfd_queue_mask;
1224 lockdep_assert_held(&mvm->mutex);
1226 if (iwl_mvm_has_new_tx_api(mvm))
1227 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1229 spin_lock_bh(&mvmsta->lock);
1230 tfd_queue_mask = mvmsta->tfd_queue_msk;
1231 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1232 spin_unlock_bh(&mvmsta->lock);
1234 if (tid == IWL_MAX_TID_COUNT) {
1235 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1236 IWL_MVM_DQA_MIN_MGMT_QUEUE,
1237 IWL_MVM_DQA_MAX_MGMT_QUEUE);
1238 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1239 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1242 /* If no such queue is found, we'll use a DATA queue instead */
1245 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1246 (mvm->queue_info[mvmsta->reserved_queue].status ==
1247 IWL_MVM_QUEUE_RESERVED)) {
1248 queue = mvmsta->reserved_queue;
1249 mvm->queue_info[queue].reserved = true;
1250 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1254 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1255 IWL_MVM_DQA_MIN_DATA_QUEUE,
1256 IWL_MVM_DQA_MAX_DATA_QUEUE);
1258 /* try harder - perhaps kill an inactive queue */
1259 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1262 /* No free queue - we'll have to share */
1264 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1266 shared_queue = true;
1267 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1272 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1273 * to make sure no one else takes it.
1274 * This will allow avoiding re-acquiring the lock at the end of the
1275 * configuration. On error we'll mark it back as free.
1277 if (queue > 0 && !shared_queue)
1278 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1280 /* This shouldn't happen - out of queues */
1281 if (WARN_ON(queue <= 0)) {
1282 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1288 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1289 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1291 * Mark all DATA queues as allowing to be aggregated at some point
1293 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1294 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1296 IWL_DEBUG_TX_QUEUES(mvm,
1297 "Allocating %squeue #%d to sta %d on tid %d\n",
1298 shared_queue ? "shared " : "", queue,
1299 mvmsta->sta_id, tid);
1302 /* Disable any open aggs on this queue */
1303 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1305 if (disable_agg_tids) {
1306 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1308 iwl_mvm_invalidate_sta_queue(mvm, queue,
1309 disable_agg_tids, false);
1313 inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1316 * Mark queue as shared in transport if shared
1317 * Note this has to be done after queue enablement because enablement
1318 * can also set this value, and there is no indication there to shared
1322 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1324 spin_lock_bh(&mvmsta->lock);
1326 * This looks racy, but it is not. We have only one packet for
1327 * this ra/tid in our Tx path since we stop the Qdisc when we
1328 * need to allocate a new TFD queue.
1331 mvmsta->tid_data[tid].seq_number += 0x10;
1332 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1334 mvmsta->tid_data[tid].txq_id = queue;
1335 mvmsta->tfd_queue_msk |= BIT(queue);
1336 queue_state = mvmsta->tid_data[tid].state;
1338 if (mvmsta->reserved_queue == queue)
1339 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1340 spin_unlock_bh(&mvmsta->lock);
1342 if (!shared_queue) {
1343 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1347 /* If we need to re-enable aggregations... */
1348 if (queue_state == IWL_AGG_ON) {
1349 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1354 /* Redirect queue, if needed */
1355 ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1357 iwl_mvm_txq_from_tid(sta, tid));
1365 iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1370 static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1372 if (tid == IWL_MAX_TID_COUNT)
1373 return IEEE80211_AC_VO; /* MGMT */
1375 return tid_to_mac80211_ac[tid];
1378 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1380 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1383 mutex_lock(&mvm->mutex);
1385 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1387 while (!list_empty(&mvm->add_stream_txqs)) {
1388 struct iwl_mvm_txq *mvmtxq;
1389 struct ieee80211_txq *txq;
1392 mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1393 struct iwl_mvm_txq, list);
1395 txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1398 if (tid == IEEE80211_NUM_TIDS)
1399 tid = IWL_MAX_TID_COUNT;
1401 iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid);
1402 list_del_init(&mvmtxq->list);
1404 iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1408 mutex_unlock(&mvm->mutex);
1411 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1412 struct ieee80211_sta *sta,
1413 enum nl80211_iftype vif_type)
1415 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1418 /* queue reserving is disabled on new TX path */
1419 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1422 /* run the general cleanup/unsharing of queues */
1423 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1425 /* Make sure we have free resources for this STA */
1426 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1427 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1428 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1429 IWL_MVM_QUEUE_FREE))
1430 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1432 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1433 IWL_MVM_DQA_MIN_DATA_QUEUE,
1434 IWL_MVM_DQA_MAX_DATA_QUEUE);
1436 /* try again - this time kick out a queue if needed */
1437 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1439 IWL_ERR(mvm, "No available queues for new station\n");
1443 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1445 mvmsta->reserved_queue = queue;
1447 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1448 queue, mvmsta->sta_id);
1454 * In DQA mode, after a HW restart the queues should be allocated as before, in
1455 * order to avoid race conditions when there are shared queues. This function
1456 * does the re-mapping and queue allocation.
1458 * Note that re-enabling aggregations isn't done in this function.
1460 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1461 struct ieee80211_sta *sta)
1463 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1465 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1467 struct iwl_trans_txq_scd_cfg cfg = {
1468 .sta_id = mvm_sta->sta_id,
1469 .frame_limit = IWL_FRAME_LIMIT,
1472 /* Make sure reserved queue is still marked as such (if allocated) */
1473 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1474 mvm->queue_info[mvm_sta->reserved_queue].status =
1475 IWL_MVM_QUEUE_RESERVED;
1477 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1478 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1479 int txq_id = tid_data->txq_id;
1482 if (txq_id == IWL_MVM_INVALID_QUEUE)
1485 ac = tid_to_mac80211_ac[i];
1487 if (iwl_mvm_has_new_tx_api(mvm)) {
1488 IWL_DEBUG_TX_QUEUES(mvm,
1489 "Re-mapping sta %d tid %d\n",
1490 mvm_sta->sta_id, i);
1491 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1493 tid_data->txq_id = txq_id;
1496 * Since we don't set the seq number after reset, and HW
1497 * sets it now, FW reset will cause the seq num to start
1498 * at 0 again, so driver will need to update it
1499 * internally as well, so it keeps in sync with real val
1501 tid_data->seq_number = 0;
1503 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1506 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1507 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1509 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1511 IWL_DEBUG_TX_QUEUES(mvm,
1512 "Re-mapping sta %d tid %d to queue %d\n",
1513 mvm_sta->sta_id, i, txq_id);
1515 iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1516 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1521 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1522 struct iwl_mvm_int_sta *sta,
1524 u16 mac_id, u16 color)
1526 struct iwl_mvm_add_sta_cmd cmd;
1528 u32 status = ADD_STA_SUCCESS;
1530 lockdep_assert_held(&mvm->mutex);
1532 memset(&cmd, 0, sizeof(cmd));
1533 cmd.sta_id = sta->sta_id;
1534 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1536 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1537 cmd.station_type = sta->type;
1539 if (!iwl_mvm_has_new_tx_api(mvm))
1540 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1541 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1544 memcpy(cmd.addr, addr, ETH_ALEN);
1546 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1547 iwl_mvm_add_sta_cmd_size(mvm),
1552 switch (status & IWL_ADD_STA_STATUS_MASK) {
1553 case ADD_STA_SUCCESS:
1554 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1558 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1565 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1566 struct ieee80211_vif *vif,
1567 struct ieee80211_sta *sta)
1569 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1570 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1571 struct iwl_mvm_rxq_dup_data *dup_data;
1573 bool sta_update = false;
1574 unsigned int sta_flags = 0;
1576 lockdep_assert_held(&mvm->mutex);
1578 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1579 sta_id = iwl_mvm_find_free_sta_id(mvm,
1580 ieee80211_vif_type_p2p(vif));
1582 sta_id = mvm_sta->sta_id;
1584 if (sta_id == IWL_MVM_INVALID_STA)
1587 spin_lock_init(&mvm_sta->lock);
1589 /* if this is a HW restart re-alloc existing queues */
1590 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1591 struct iwl_mvm_int_sta tmp_sta = {
1593 .type = mvm_sta->sta_type,
1597 * First add an empty station since allocating
1598 * a queue requires a valid station
1600 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1601 mvmvif->id, mvmvif->color);
1605 iwl_mvm_realloc_queues_after_restart(mvm, sta);
1607 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1611 mvm_sta->sta_id = sta_id;
1612 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1615 if (!mvm->trans->cfg->gen2)
1616 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1618 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1619 mvm_sta->tx_protection = 0;
1620 mvm_sta->tt_tx_protection = false;
1621 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1623 /* HW restart, don't assume the memory has been zeroed */
1624 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1625 mvm_sta->tfd_queue_msk = 0;
1627 /* for HW restart - reset everything but the sequence number */
1628 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1629 u16 seq = mvm_sta->tid_data[i].seq_number;
1630 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1631 mvm_sta->tid_data[i].seq_number = seq;
1634 * Mark all queues for this STA as unallocated and defer TX
1635 * frames until the queue is allocated
1637 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1640 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1641 struct iwl_mvm_txq *mvmtxq =
1642 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1644 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1645 INIT_LIST_HEAD(&mvmtxq->list);
1646 atomic_set(&mvmtxq->tx_request, 0);
1649 mvm_sta->agg_tids = 0;
1651 if (iwl_mvm_has_new_rx_api(mvm) &&
1652 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1655 dup_data = kcalloc(mvm->trans->num_rx_queues,
1656 sizeof(*dup_data), GFP_KERNEL);
1660 * Initialize all the last_seq values to 0xffff which can never
1661 * compare equal to the frame's seq_ctrl in the check in
1662 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1663 * number and fragmented packets don't reach that function.
1665 * This thus allows receiving a packet with seqno 0 and the
1666 * retry bit set as the very first packet on a new TID.
1668 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1669 memset(dup_data[q].last_seq, 0xff,
1670 sizeof(dup_data[q].last_seq));
1671 mvm_sta->dup_data = dup_data;
1674 if (!iwl_mvm_has_new_tx_api(mvm)) {
1675 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1676 ieee80211_vif_type_p2p(vif));
1682 * if rs is registered with mac80211, then "add station" will be handled
1683 * via the corresponding ops, otherwise need to notify rate scaling here
1685 if (iwl_mvm_has_tlc_offload(mvm))
1686 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1688 iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1691 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1695 if (vif->type == NL80211_IFTYPE_STATION) {
1697 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1698 mvmvif->ap_sta_id = sta_id;
1700 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1704 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1712 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1715 struct iwl_mvm_add_sta_cmd cmd = {};
1719 lockdep_assert_held(&mvm->mutex);
1721 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1722 cmd.sta_id = mvmsta->sta_id;
1723 cmd.add_modify = STA_MODE_MODIFY;
1724 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1725 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1727 status = ADD_STA_SUCCESS;
1728 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1729 iwl_mvm_add_sta_cmd_size(mvm),
1734 switch (status & IWL_ADD_STA_STATUS_MASK) {
1735 case ADD_STA_SUCCESS:
1736 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1741 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1750 * Remove a station from the FW table. Before sending the command to remove
1751 * the station validate that the station is indeed known to the driver (sanity
1754 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1756 struct ieee80211_sta *sta;
1757 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1762 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1763 lockdep_is_held(&mvm->mutex));
1765 /* Note: internal stations are marked as error values */
1767 IWL_ERR(mvm, "Invalid station id\n");
1771 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1772 sizeof(rm_sta_cmd), &rm_sta_cmd);
1774 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1781 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1782 struct ieee80211_vif *vif,
1783 struct ieee80211_sta *sta)
1785 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1788 lockdep_assert_held(&mvm->mutex);
1790 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1791 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1794 iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1796 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1799 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1800 struct iwl_mvm_txq *mvmtxq =
1801 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1803 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1807 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1808 struct iwl_mvm_sta *mvm_sta)
1812 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1816 spin_lock_bh(&mvm_sta->lock);
1817 txq_id = mvm_sta->tid_data[i].txq_id;
1818 spin_unlock_bh(&mvm_sta->lock);
1820 if (txq_id == IWL_MVM_INVALID_QUEUE)
1823 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1831 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1832 struct ieee80211_vif *vif,
1833 struct ieee80211_sta *sta)
1835 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1836 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1837 u8 sta_id = mvm_sta->sta_id;
1840 lockdep_assert_held(&mvm->mutex);
1842 if (iwl_mvm_has_new_rx_api(mvm))
1843 kfree(mvm_sta->dup_data);
1845 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1849 /* flush its queues here since we are freeing mvm_sta */
1850 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1853 if (iwl_mvm_has_new_tx_api(mvm)) {
1854 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1856 u32 q_mask = mvm_sta->tfd_queue_msk;
1858 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1864 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1866 iwl_mvm_disable_sta_queues(mvm, vif, sta);
1868 /* If there is a TXQ still marked as reserved - free it */
1869 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1870 u8 reserved_txq = mvm_sta->reserved_queue;
1871 enum iwl_mvm_queue_status *status;
1874 * If no traffic has gone through the reserved TXQ - it
1875 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1876 * should be manually marked as free again
1878 status = &mvm->queue_info[reserved_txq].status;
1879 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1880 (*status != IWL_MVM_QUEUE_FREE),
1881 "sta_id %d reserved txq %d status %d",
1882 sta_id, reserved_txq, *status))
1885 *status = IWL_MVM_QUEUE_FREE;
1888 if (vif->type == NL80211_IFTYPE_STATION &&
1889 mvmvif->ap_sta_id == sta_id) {
1890 /* if associated - we can't remove the AP STA now */
1891 if (vif->bss_conf.assoc)
1894 /* unassoc - go ahead - remove the AP STA now */
1895 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1897 /* clear d0i3_ap_sta_id if no longer relevant */
1898 if (mvm->d0i3_ap_sta_id == sta_id)
1899 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1903 * This shouldn't happen - the TDLS channel switch should be canceled
1904 * before the STA is removed.
1906 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1907 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1908 cancel_delayed_work(&mvm->tdls_cs.dwork);
1912 * Make sure that the tx response code sees the station as -EBUSY and
1913 * calls the drain worker.
1915 spin_lock_bh(&mvm_sta->lock);
1916 spin_unlock_bh(&mvm_sta->lock);
1918 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1919 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1924 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1925 struct ieee80211_vif *vif,
1928 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1930 lockdep_assert_held(&mvm->mutex);
1932 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1936 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1937 struct iwl_mvm_int_sta *sta,
1938 u32 qmask, enum nl80211_iftype iftype,
1939 enum iwl_sta_type type)
1941 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1942 sta->sta_id == IWL_MVM_INVALID_STA) {
1943 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1944 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1948 sta->tfd_queue_msk = qmask;
1951 /* put a non-NULL value so iterating over the stations won't stop */
1952 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1956 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1958 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1959 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1960 sta->sta_id = IWL_MVM_INVALID_STA;
1963 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 *queue,
1966 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1967 mvm->cfg->base_params->wd_timeout :
1968 IWL_WATCHDOG_DISABLED;
1970 if (iwl_mvm_has_new_tx_api(mvm)) {
1972 iwl_mvm_tvqm_enable_txq(mvm, sta_id,
1975 *queue = tvqm_queue;
1977 struct iwl_trans_txq_scd_cfg cfg = {
1980 .tid = IWL_MAX_TID_COUNT,
1982 .frame_limit = IWL_FRAME_LIMIT,
1985 iwl_mvm_enable_txq(mvm, NULL, *queue, 0, &cfg, wdg_timeout);
1989 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1993 lockdep_assert_held(&mvm->mutex);
1995 /* Allocate aux station and assign to it the aux queue */
1996 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1997 NL80211_IFTYPE_UNSPECIFIED,
1998 IWL_STA_AUX_ACTIVITY);
2002 /* Map Aux queue to fifo - needs to happen before adding Aux station */
2003 if (!iwl_mvm_has_new_tx_api(mvm))
2004 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2005 mvm->aux_sta.sta_id,
2006 IWL_MVM_TX_FIFO_MCAST);
2008 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
2011 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2016 * For 22000 firmware and on we cannot add queue to a station unknown
2017 * to firmware so enable queue here - after the station was added
2019 if (iwl_mvm_has_new_tx_api(mvm))
2020 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2021 mvm->aux_sta.sta_id,
2022 IWL_MVM_TX_FIFO_MCAST);
2027 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2029 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2032 lockdep_assert_held(&mvm->mutex);
2034 /* Map snif queue to fifo - must happen before adding snif station */
2035 if (!iwl_mvm_has_new_tx_api(mvm))
2036 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2037 mvm->snif_sta.sta_id,
2038 IWL_MVM_TX_FIFO_BE);
2040 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
2046 * For 22000 firmware and on we cannot add queue to a station unknown
2047 * to firmware so enable queue here - after the station was added
2049 if (iwl_mvm_has_new_tx_api(mvm))
2050 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2051 mvm->snif_sta.sta_id,
2052 IWL_MVM_TX_FIFO_BE);
2057 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2061 lockdep_assert_held(&mvm->mutex);
2063 iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2064 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2066 IWL_WARN(mvm, "Failed sending remove station\n");
2071 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2073 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2076 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
2078 lockdep_assert_held(&mvm->mutex);
2080 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2084 * Send the add station command for the vif's broadcast station.
2085 * Assumes that the station was already allocated.
2087 * @mvm: the mvm component
2088 * @vif: the interface to which the broadcast station is added
2089 * @bsta: the broadcast station to add.
2091 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2093 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2094 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2095 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2096 const u8 *baddr = _baddr;
2099 unsigned int wdg_timeout =
2100 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2101 struct iwl_trans_txq_scd_cfg cfg = {
2102 .fifo = IWL_MVM_TX_FIFO_VO,
2103 .sta_id = mvmvif->bcast_sta.sta_id,
2104 .tid = IWL_MAX_TID_COUNT,
2106 .frame_limit = IWL_FRAME_LIMIT,
2109 lockdep_assert_held(&mvm->mutex);
2111 if (!iwl_mvm_has_new_tx_api(mvm)) {
2112 if (vif->type == NL80211_IFTYPE_AP ||
2113 vif->type == NL80211_IFTYPE_ADHOC) {
2114 queue = mvm->probe_queue;
2115 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2116 queue = mvm->p2p_dev_queue;
2118 WARN(1, "Missing required TXQ for adding bcast STA\n");
2122 bsta->tfd_queue_msk |= BIT(queue);
2124 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2127 if (vif->type == NL80211_IFTYPE_ADHOC)
2128 baddr = vif->bss_conf.bssid;
2130 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2133 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2134 mvmvif->id, mvmvif->color);
2139 * For 22000 firmware and on we cannot add queue to a station unknown
2140 * to firmware so enable queue here - after the station was added
2142 if (iwl_mvm_has_new_tx_api(mvm)) {
2143 queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2147 if (vif->type == NL80211_IFTYPE_AP ||
2148 vif->type == NL80211_IFTYPE_ADHOC)
2149 mvm->probe_queue = queue;
2150 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2151 mvm->p2p_dev_queue = queue;
2157 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2158 struct ieee80211_vif *vif)
2160 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2163 lockdep_assert_held(&mvm->mutex);
2165 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2167 switch (vif->type) {
2168 case NL80211_IFTYPE_AP:
2169 case NL80211_IFTYPE_ADHOC:
2170 queue = mvm->probe_queue;
2172 case NL80211_IFTYPE_P2P_DEVICE:
2173 queue = mvm->p2p_dev_queue;
2176 WARN(1, "Can't free bcast queue on vif type %d\n",
2181 iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2182 if (iwl_mvm_has_new_tx_api(mvm))
2185 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2186 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2189 /* Send the FW a request to remove the station from it's internal data
2190 * structures, but DO NOT remove the entry from the local data structures. */
2191 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2193 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2196 lockdep_assert_held(&mvm->mutex);
2198 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2200 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2202 IWL_WARN(mvm, "Failed sending remove station\n");
2206 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2208 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2210 lockdep_assert_held(&mvm->mutex);
2212 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2213 ieee80211_vif_type_p2p(vif),
2214 IWL_STA_GENERAL_PURPOSE);
2217 /* Allocate a new station entry for the broadcast station to the given vif,
2218 * and send it to the FW.
2219 * Note that each P2P mac should have its own broadcast station.
2221 * @mvm: the mvm component
2222 * @vif: the interface to which the broadcast station is added
2223 * @bsta: the broadcast station to add. */
2224 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2226 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2227 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2230 lockdep_assert_held(&mvm->mutex);
2232 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2236 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2239 iwl_mvm_dealloc_int_sta(mvm, bsta);
2244 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2246 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2248 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2252 * Send the FW a request to remove the station from it's internal data
2253 * structures, and in addition remove it from the local data structure.
2255 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2259 lockdep_assert_held(&mvm->mutex);
2261 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2263 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2269 * Allocate a new station entry for the multicast station to the given vif,
2270 * and send it to the FW.
2271 * Note that each AP/GO mac should have its own multicast station.
2273 * @mvm: the mvm component
2274 * @vif: the interface to which the multicast station is added
2276 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2278 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2279 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2280 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2281 const u8 *maddr = _maddr;
2282 struct iwl_trans_txq_scd_cfg cfg = {
2283 .fifo = vif->type == NL80211_IFTYPE_AP ?
2284 IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2285 .sta_id = msta->sta_id,
2288 .frame_limit = IWL_FRAME_LIMIT,
2290 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2293 lockdep_assert_held(&mvm->mutex);
2295 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2296 vif->type != NL80211_IFTYPE_ADHOC))
2300 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2301 * invalid, so make sure we use the queue we want.
2302 * Note that this is done here as we want to avoid making DQA
2303 * changes in mac80211 layer.
2305 if (vif->type == NL80211_IFTYPE_ADHOC)
2306 mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2309 * While in previous FWs we had to exclude cab queue from TFD queue
2310 * mask, now it is needed as any other queue.
2312 if (!iwl_mvm_has_new_tx_api(mvm) &&
2313 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2314 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2316 msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2318 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2319 mvmvif->id, mvmvif->color);
2321 iwl_mvm_dealloc_int_sta(mvm, msta);
2326 * Enable cab queue after the ADD_STA command is sent.
2327 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2328 * command with unknown station id, and for FW that doesn't support
2329 * station API since the cab queue is not included in the
2332 if (iwl_mvm_has_new_tx_api(mvm)) {
2333 int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2336 mvmvif->cab_queue = queue;
2337 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2338 IWL_UCODE_TLV_API_STA_TYPE))
2339 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2345 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2346 struct ieee80211_key_conf *keyconf,
2350 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2351 struct iwl_mvm_add_sta_key_cmd cmd;
2353 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2354 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2359 /* This is a valid situation for GTK removal */
2360 if (sta_id == IWL_MVM_INVALID_STA)
2363 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2364 STA_KEY_FLG_KEYID_MSK);
2365 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2366 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2369 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2372 * The fields assigned here are in the same location at the start
2373 * of the command, so we can do this union trick.
2375 u.cmd.common.key_flags = key_flags;
2376 u.cmd.common.key_offset = keyconf->hw_key_idx;
2377 u.cmd.common.sta_id = sta_id;
2379 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2381 status = ADD_STA_SUCCESS;
2382 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2386 case ADD_STA_SUCCESS:
2387 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2391 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2399 * Send the FW a request to remove the station from it's internal data
2400 * structures, and in addition remove it from the local data structure.
2402 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2404 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2407 lockdep_assert_held(&mvm->mutex);
2409 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2411 iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
2413 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2415 IWL_WARN(mvm, "Failed sending remove station\n");
2420 #define IWL_MAX_RX_BA_SESSIONS 16
2422 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2424 struct iwl_mvm_delba_notif notif = {
2425 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2429 iwl_mvm_sync_rx_queues_internal(mvm, (void *)¬if, sizeof(notif));
2432 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2433 struct iwl_mvm_baid_data *data)
2437 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2439 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2441 struct iwl_mvm_reorder_buffer *reorder_buf =
2442 &data->reorder_buf[i];
2443 struct iwl_mvm_reorder_buf_entry *entries =
2444 &data->entries[i * data->entries_per_queue];
2446 spin_lock_bh(&reorder_buf->lock);
2447 if (likely(!reorder_buf->num_stored)) {
2448 spin_unlock_bh(&reorder_buf->lock);
2453 * This shouldn't happen in regular DELBA since the internal
2454 * delBA notification should trigger a release of all frames in
2455 * the reorder buffer.
2459 for (j = 0; j < reorder_buf->buf_size; j++)
2460 __skb_queue_purge(&entries[j].e.frames);
2462 * Prevent timer re-arm. This prevents a very far fetched case
2463 * where we timed out on the notification. There may be prior
2464 * RX frames pending in the RX queue before the notification
2465 * that might get processed between now and the actual deletion
2466 * and we would re-arm the timer although we are deleting the
2469 reorder_buf->removed = true;
2470 spin_unlock_bh(&reorder_buf->lock);
2471 del_timer_sync(&reorder_buf->reorder_timer);
2475 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2476 struct iwl_mvm_baid_data *data,
2477 u16 ssn, u16 buf_size)
2481 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2482 struct iwl_mvm_reorder_buffer *reorder_buf =
2483 &data->reorder_buf[i];
2484 struct iwl_mvm_reorder_buf_entry *entries =
2485 &data->entries[i * data->entries_per_queue];
2488 reorder_buf->num_stored = 0;
2489 reorder_buf->head_sn = ssn;
2490 reorder_buf->buf_size = buf_size;
2491 /* rx reorder timer */
2492 timer_setup(&reorder_buf->reorder_timer,
2493 iwl_mvm_reorder_timer_expired, 0);
2494 spin_lock_init(&reorder_buf->lock);
2495 reorder_buf->mvm = mvm;
2496 reorder_buf->queue = i;
2497 reorder_buf->valid = false;
2498 for (j = 0; j < reorder_buf->buf_size; j++)
2499 __skb_queue_head_init(&entries[j].e.frames);
2503 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2504 int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2506 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2507 struct iwl_mvm_add_sta_cmd cmd = {};
2508 struct iwl_mvm_baid_data *baid_data = NULL;
2512 lockdep_assert_held(&mvm->mutex);
2514 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2515 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2519 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2520 u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2522 /* sparse doesn't like the __align() so don't check */
2525 * The division below will be OK if either the cache line size
2526 * can be divided by the entry size (ALIGN will round up) or if
2527 * if the entry size can be divided by the cache line size, in
2528 * which case the ALIGN() will do nothing.
2530 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2531 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2535 * Upward align the reorder buffer size to fill an entire cache
2536 * line for each queue, to avoid sharing cache lines between
2539 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2542 * Allocate here so if allocation fails we can bail out early
2543 * before starting the BA session in the firmware
2545 baid_data = kzalloc(sizeof(*baid_data) +
2546 mvm->trans->num_rx_queues *
2553 * This division is why we need the above BUILD_BUG_ON(),
2554 * if that doesn't hold then this will not be right.
2556 baid_data->entries_per_queue =
2557 reorder_buf_size / sizeof(baid_data->entries[0]);
2560 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2561 cmd.sta_id = mvm_sta->sta_id;
2562 cmd.add_modify = STA_MODE_MODIFY;
2564 cmd.add_immediate_ba_tid = (u8) tid;
2565 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2566 cmd.rx_ba_window = cpu_to_le16(buf_size);
2568 cmd.remove_immediate_ba_tid = (u8) tid;
2570 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2571 STA_MODIFY_REMOVE_BA_TID;
2573 status = ADD_STA_SUCCESS;
2574 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2575 iwl_mvm_add_sta_cmd_size(mvm),
2580 switch (status & IWL_ADD_STA_STATUS_MASK) {
2581 case ADD_STA_SUCCESS:
2582 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2583 start ? "start" : "stopp");
2585 case ADD_STA_IMMEDIATE_BA_FAILURE:
2586 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2591 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2592 start ? "start" : "stopp", status);
2602 mvm->rx_ba_sessions++;
2604 if (!iwl_mvm_has_new_rx_api(mvm))
2607 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2611 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2612 IWL_ADD_STA_BAID_SHIFT);
2613 baid_data->baid = baid;
2614 baid_data->timeout = timeout;
2615 baid_data->last_rx = jiffies;
2616 baid_data->rcu_ptr = &mvm->baid_map[baid];
2617 timer_setup(&baid_data->session_timer,
2618 iwl_mvm_rx_agg_session_expired, 0);
2619 baid_data->mvm = mvm;
2620 baid_data->tid = tid;
2621 baid_data->sta_id = mvm_sta->sta_id;
2623 mvm_sta->tid_to_baid[tid] = baid;
2625 mod_timer(&baid_data->session_timer,
2626 TU_TO_EXP_TIME(timeout * 2));
2628 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2630 * protect the BA data with RCU to cover a case where our
2631 * internal RX sync mechanism will timeout (not that it's
2632 * supposed to happen) and we will free the session data while
2633 * RX is being processed in parallel
2635 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2636 mvm_sta->sta_id, tid, baid);
2637 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2638 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2640 u8 baid = mvm_sta->tid_to_baid[tid];
2642 if (mvm->rx_ba_sessions > 0)
2643 /* check that restart flow didn't zero the counter */
2644 mvm->rx_ba_sessions--;
2645 if (!iwl_mvm_has_new_rx_api(mvm))
2648 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2651 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2652 if (WARN_ON(!baid_data))
2655 /* synchronize all rx queues so we can safely delete */
2656 iwl_mvm_free_reorder(mvm, baid_data);
2657 del_timer_sync(&baid_data->session_timer);
2658 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2659 kfree_rcu(baid_data, rcu_head);
2660 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2669 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2670 int tid, u8 queue, bool start)
2672 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2673 struct iwl_mvm_add_sta_cmd cmd = {};
2677 lockdep_assert_held(&mvm->mutex);
2680 mvm_sta->tfd_queue_msk |= BIT(queue);
2681 mvm_sta->tid_disable_agg &= ~BIT(tid);
2683 /* In DQA-mode the queue isn't removed on agg termination */
2684 mvm_sta->tid_disable_agg |= BIT(tid);
2687 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2688 cmd.sta_id = mvm_sta->sta_id;
2689 cmd.add_modify = STA_MODE_MODIFY;
2690 if (!iwl_mvm_has_new_tx_api(mvm))
2691 cmd.modify_mask = STA_MODIFY_QUEUES;
2692 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2693 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2694 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2696 status = ADD_STA_SUCCESS;
2697 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2698 iwl_mvm_add_sta_cmd_size(mvm),
2703 switch (status & IWL_ADD_STA_STATUS_MASK) {
2704 case ADD_STA_SUCCESS:
2708 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2709 start ? "start" : "stopp", status);
2716 const u8 tid_to_mac80211_ac[] = {
2725 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2728 static const u8 tid_to_ucode_ac[] = {
2739 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2740 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2742 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2743 struct iwl_mvm_tid_data *tid_data;
2748 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2751 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2752 mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2754 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2755 mvmsta->tid_data[tid].state);
2759 lockdep_assert_held(&mvm->mutex);
2761 if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2762 iwl_mvm_has_new_tx_api(mvm)) {
2763 u8 ac = tid_to_mac80211_ac[tid];
2765 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2770 spin_lock_bh(&mvmsta->lock);
2772 /* possible race condition - we entered D0i3 while starting agg */
2773 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2774 spin_unlock_bh(&mvmsta->lock);
2775 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2780 * Note the possible cases:
2781 * 1. An enabled TXQ - TXQ needs to become agg'ed
2782 * 2. The TXQ hasn't yet been enabled, so find a free one and mark
2785 txq_id = mvmsta->tid_data[tid].txq_id;
2786 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2787 ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2788 IWL_MVM_DQA_MIN_DATA_QUEUE,
2789 IWL_MVM_DQA_MAX_DATA_QUEUE);
2791 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2797 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2798 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2799 } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2801 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2802 tid, IWL_MAX_HW_QUEUES - 1);
2805 } else if (unlikely(mvm->queue_info[txq_id].status ==
2806 IWL_MVM_QUEUE_SHARED)) {
2808 IWL_DEBUG_TX_QUEUES(mvm,
2809 "Can't start tid %d agg on shared queue!\n",
2814 IWL_DEBUG_TX_QUEUES(mvm,
2815 "AGG for tid %d will be on queue #%d\n",
2818 tid_data = &mvmsta->tid_data[tid];
2819 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2820 tid_data->txq_id = txq_id;
2821 *ssn = tid_data->ssn;
2823 IWL_DEBUG_TX_QUEUES(mvm,
2824 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2825 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2826 tid_data->next_reclaimed);
2829 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2830 * to align the wrap around of ssn so we compare relevant values.
2832 normalized_ssn = tid_data->ssn;
2833 if (mvm->trans->cfg->gen2)
2834 normalized_ssn &= 0xff;
2836 if (normalized_ssn == tid_data->next_reclaimed) {
2837 tid_data->state = IWL_AGG_STARTING;
2838 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2840 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2846 spin_unlock_bh(&mvmsta->lock);
2851 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2852 struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2855 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2856 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2857 unsigned int wdg_timeout =
2858 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2860 bool alloc_queue = true;
2861 enum iwl_mvm_queue_status queue_status;
2864 struct iwl_trans_txq_scd_cfg cfg = {
2865 .sta_id = mvmsta->sta_id,
2867 .frame_limit = buf_size,
2872 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2873 * manager, so this function should never be called in this case.
2875 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2878 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2879 != IWL_MAX_TID_COUNT);
2881 spin_lock_bh(&mvmsta->lock);
2882 ssn = tid_data->ssn;
2883 queue = tid_data->txq_id;
2884 tid_data->state = IWL_AGG_ON;
2885 mvmsta->agg_tids |= BIT(tid);
2886 tid_data->ssn = 0xffff;
2887 tid_data->amsdu_in_ampdu_allowed = amsdu;
2888 spin_unlock_bh(&mvmsta->lock);
2890 if (iwl_mvm_has_new_tx_api(mvm)) {
2892 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2893 * would have failed, so if we are here there is no need to
2895 * However, if aggregation size is different than the default
2896 * size, the scheduler should be reconfigured.
2897 * We cannot do this with the new TX API, so return unsupported
2898 * for now, until it will be offloaded to firmware..
2899 * Note that if SCD default value changes - this condition
2900 * should be updated as well.
2902 if (buf_size < IWL_FRAME_LIMIT)
2905 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2911 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2913 queue_status = mvm->queue_info[queue].status;
2915 /* Maybe there is no need to even alloc a queue... */
2916 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2917 alloc_queue = false;
2920 * Only reconfig the SCD for the queue if the window size has
2921 * changed from current (become smaller)
2923 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2925 * If reconfiguring an existing queue, it first must be
2928 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2932 "Error draining queue before reconfig\n");
2936 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2937 mvmsta->sta_id, tid,
2941 "Error reconfiguring TXQ #%d\n", queue);
2947 iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2950 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2951 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2952 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2957 /* No need to mark as reserved */
2958 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2962 * Even though in theory the peer could have different
2963 * aggregation reorder buffer sizes for different sessions,
2964 * our ucode doesn't allow for that and has a global limit
2965 * for each station. Therefore, use the minimum of all the
2966 * aggregation sessions and our default value.
2968 mvmsta->max_agg_bufsize =
2969 min(mvmsta->max_agg_bufsize, buf_size);
2970 mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2972 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2975 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq, false);
2978 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2979 struct iwl_mvm_sta *mvmsta,
2980 struct iwl_mvm_tid_data *tid_data)
2982 u16 txq_id = tid_data->txq_id;
2984 lockdep_assert_held(&mvm->mutex);
2986 if (iwl_mvm_has_new_tx_api(mvm))
2990 * The TXQ is marked as reserved only if no traffic came through yet
2991 * This means no traffic has been sent on this TID (agg'd or not), so
2992 * we no longer have use for the queue. Since it hasn't even been
2993 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2996 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
2997 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2998 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3002 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3003 struct ieee80211_sta *sta, u16 tid)
3005 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3006 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3011 * If mac80211 is cleaning its state, then say that we finished since
3012 * our state has been cleared anyway.
3014 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3015 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3019 spin_lock_bh(&mvmsta->lock);
3021 txq_id = tid_data->txq_id;
3023 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3024 mvmsta->sta_id, tid, txq_id, tid_data->state);
3026 mvmsta->agg_tids &= ~BIT(tid);
3028 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3030 switch (tid_data->state) {
3032 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3034 IWL_DEBUG_TX_QUEUES(mvm,
3035 "ssn = %d, next_recl = %d\n",
3036 tid_data->ssn, tid_data->next_reclaimed);
3038 tid_data->ssn = 0xffff;
3039 tid_data->state = IWL_AGG_OFF;
3040 spin_unlock_bh(&mvmsta->lock);
3042 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3044 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3046 case IWL_AGG_STARTING:
3047 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3049 * The agg session has been stopped before it was set up. This
3050 * can happen when the AddBA timer times out for example.
3053 /* No barriers since we are under mutex */
3054 lockdep_assert_held(&mvm->mutex);
3056 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3057 tid_data->state = IWL_AGG_OFF;
3062 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3063 mvmsta->sta_id, tid, tid_data->state);
3065 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
3069 spin_unlock_bh(&mvmsta->lock);
3074 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3075 struct ieee80211_sta *sta, u16 tid)
3077 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3078 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3080 enum iwl_mvm_agg_state old_state;
3083 * First set the agg state to OFF to avoid calling
3084 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3086 spin_lock_bh(&mvmsta->lock);
3087 txq_id = tid_data->txq_id;
3088 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3089 mvmsta->sta_id, tid, txq_id, tid_data->state);
3090 old_state = tid_data->state;
3091 tid_data->state = IWL_AGG_OFF;
3092 mvmsta->agg_tids &= ~BIT(tid);
3093 spin_unlock_bh(&mvmsta->lock);
3095 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3097 if (old_state >= IWL_AGG_ON) {
3098 iwl_mvm_drain_sta(mvm, mvmsta, true);
3100 if (iwl_mvm_has_new_tx_api(mvm)) {
3101 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3103 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3104 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3106 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3107 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3108 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3111 iwl_mvm_drain_sta(mvm, mvmsta, false);
3113 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3119 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3121 int i, max = -1, max_offs = -1;
3123 lockdep_assert_held(&mvm->mutex);
3125 /* Pick the unused key offset with the highest 'deleted'
3126 * counter. Every time a key is deleted, all the counters
3127 * are incremented and the one that was just deleted is
3128 * reset to zero. Thus, the highest counter is the one
3129 * that was deleted longest ago. Pick that one.
3131 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3132 if (test_bit(i, mvm->fw_key_table))
3134 if (mvm->fw_key_deleted[i] > max) {
3135 max = mvm->fw_key_deleted[i];
3141 return STA_KEY_IDX_INVALID;
3146 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3147 struct ieee80211_vif *vif,
3148 struct ieee80211_sta *sta)
3150 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3153 return iwl_mvm_sta_from_mac80211(sta);
3156 * The device expects GTKs for station interfaces to be
3157 * installed as GTKs for the AP station. If we have no
3158 * station ID, then use AP's station ID.
3160 if (vif->type == NL80211_IFTYPE_STATION &&
3161 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3162 u8 sta_id = mvmvif->ap_sta_id;
3164 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3165 lockdep_is_held(&mvm->mutex));
3168 * It is possible that the 'sta' parameter is NULL,
3169 * for example when a GTK is removed - the sta_id will then
3170 * be the AP ID, and no station was passed by mac80211.
3172 if (IS_ERR_OR_NULL(sta))
3175 return iwl_mvm_sta_from_mac80211(sta);
3181 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3183 struct ieee80211_key_conf *key, bool mcast,
3184 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3185 u8 key_offset, bool mfp)
3188 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3189 struct iwl_mvm_add_sta_key_cmd cmd;
3197 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3198 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3200 if (sta_id == IWL_MVM_INVALID_STA)
3203 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3204 STA_KEY_FLG_KEYID_MSK;
3205 key_flags = cpu_to_le16(keyidx);
3206 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3208 switch (key->cipher) {
3209 case WLAN_CIPHER_SUITE_TKIP:
3210 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3212 memcpy((void *)&u.cmd.tx_mic_key,
3213 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3216 memcpy((void *)&u.cmd.rx_mic_key,
3217 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3219 pn = atomic64_read(&key->tx_pn);
3222 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3223 for (i = 0; i < 5; i++)
3224 u.cmd_v1.tkip_rx_ttak[i] =
3225 cpu_to_le16(tkip_p1k[i]);
3227 memcpy(u.cmd.common.key, key->key, key->keylen);
3229 case WLAN_CIPHER_SUITE_CCMP:
3230 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3231 memcpy(u.cmd.common.key, key->key, key->keylen);
3233 pn = atomic64_read(&key->tx_pn);
3235 case WLAN_CIPHER_SUITE_WEP104:
3236 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3238 case WLAN_CIPHER_SUITE_WEP40:
3239 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3240 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3242 case WLAN_CIPHER_SUITE_GCMP_256:
3243 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3245 case WLAN_CIPHER_SUITE_GCMP:
3246 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3247 memcpy(u.cmd.common.key, key->key, key->keylen);
3249 pn = atomic64_read(&key->tx_pn);
3252 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3253 memcpy(u.cmd.common.key, key->key, key->keylen);
3257 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3259 key_flags |= cpu_to_le16(STA_KEY_MFP);
3261 u.cmd.common.key_offset = key_offset;
3262 u.cmd.common.key_flags = key_flags;
3263 u.cmd.common.sta_id = sta_id;
3266 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3267 size = sizeof(u.cmd);
3269 size = sizeof(u.cmd_v1);
3272 status = ADD_STA_SUCCESS;
3273 if (cmd_flags & CMD_ASYNC)
3274 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3277 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3281 case ADD_STA_SUCCESS:
3282 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3286 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3293 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3294 struct ieee80211_key_conf *keyconf,
3295 u8 sta_id, bool remove_key)
3297 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3299 /* verify the key details match the required command's expectations */
3300 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3301 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3302 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3303 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3304 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3307 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3308 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3311 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3312 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3315 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3317 struct ieee80211_key_seq seq;
3320 switch (keyconf->cipher) {
3321 case WLAN_CIPHER_SUITE_AES_CMAC:
3322 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3324 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3325 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3326 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3332 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3333 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3334 igtk_cmd.ctrl_flags |=
3335 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3336 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3337 pn = seq.aes_cmac.pn;
3338 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3339 ((u64) pn[4] << 8) |
3340 ((u64) pn[3] << 16) |
3341 ((u64) pn[2] << 24) |
3342 ((u64) pn[1] << 32) |
3343 ((u64) pn[0] << 40));
3346 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3347 remove_key ? "removing" : "installing",
3350 if (!iwl_mvm_has_new_rx_api(mvm)) {
3351 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3352 .ctrl_flags = igtk_cmd.ctrl_flags,
3353 .key_id = igtk_cmd.key_id,
3354 .sta_id = igtk_cmd.sta_id,
3355 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3358 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3359 ARRAY_SIZE(igtk_cmd_v1.igtk));
3360 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3361 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3363 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3364 sizeof(igtk_cmd), &igtk_cmd);
3368 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3369 struct ieee80211_vif *vif,
3370 struct ieee80211_sta *sta)
3372 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3377 if (vif->type == NL80211_IFTYPE_STATION &&
3378 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3379 u8 sta_id = mvmvif->ap_sta_id;
3380 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3381 lockdep_is_held(&mvm->mutex));
3389 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3390 struct ieee80211_vif *vif,
3391 struct ieee80211_sta *sta,
3392 struct ieee80211_key_conf *keyconf,
3398 struct ieee80211_key_seq seq;
3404 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3406 sta_id = mvm_sta->sta_id;
3408 } else if (vif->type == NL80211_IFTYPE_AP &&
3409 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3410 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3412 sta_id = mvmvif->mcast_sta.sta_id;
3414 IWL_ERR(mvm, "Failed to find station id\n");
3418 switch (keyconf->cipher) {
3419 case WLAN_CIPHER_SUITE_TKIP:
3420 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3421 /* get phase 1 key from mac80211 */
3422 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3423 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3424 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3425 seq.tkip.iv32, p1k, 0, key_offset,
3428 case WLAN_CIPHER_SUITE_CCMP:
3429 case WLAN_CIPHER_SUITE_WEP40:
3430 case WLAN_CIPHER_SUITE_WEP104:
3431 case WLAN_CIPHER_SUITE_GCMP:
3432 case WLAN_CIPHER_SUITE_GCMP_256:
3433 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3434 0, NULL, 0, key_offset, mfp);
3437 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3438 0, NULL, 0, key_offset, mfp);
3444 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3445 struct ieee80211_vif *vif,
3446 struct ieee80211_sta *sta,
3447 struct ieee80211_key_conf *keyconf,
3450 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3451 struct iwl_mvm_sta *mvm_sta;
3452 u8 sta_id = IWL_MVM_INVALID_STA;
3454 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3456 lockdep_assert_held(&mvm->mutex);
3458 if (vif->type != NL80211_IFTYPE_AP ||
3459 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3460 /* Get the station id from the mvm local station table */
3461 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3463 IWL_ERR(mvm, "Failed to find station\n");
3466 sta_id = mvm_sta->sta_id;
3469 * It is possible that the 'sta' parameter is NULL, and thus
3470 * there is a need to retrieve the sta from the local station
3474 sta = rcu_dereference_protected(
3475 mvm->fw_id_to_mac_id[sta_id],
3476 lockdep_is_held(&mvm->mutex));
3477 if (IS_ERR_OR_NULL(sta)) {
3478 IWL_ERR(mvm, "Invalid station id\n");
3483 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3486 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3488 sta_id = mvmvif->mcast_sta.sta_id;
3491 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3492 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3493 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3494 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3498 /* If the key_offset is not pre-assigned, we need to find a
3499 * new offset to use. In normal cases, the offset is not
3500 * pre-assigned, but during HW_RESTART we want to reuse the
3501 * same indices, so we pass them when this function is called.
3503 * In D3 entry, we need to hardcoded the indices (because the
3504 * firmware hardcodes the PTK offset to 0). In this case, we
3505 * need to make sure we don't overwrite the hw_key_idx in the
3506 * keyconf structure, because otherwise we cannot configure
3507 * the original ones back when resuming.
3509 if (key_offset == STA_KEY_IDX_INVALID) {
3510 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3511 if (key_offset == STA_KEY_IDX_INVALID)
3513 keyconf->hw_key_idx = key_offset;
3516 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3521 * For WEP, the same key is used for multicast and unicast. Upload it
3522 * again, using the same key offset, and now pointing the other one
3523 * to the same key slot (offset).
3524 * If this fails, remove the original as well.
3526 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3527 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3529 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3530 key_offset, !mcast);
3532 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3537 __set_bit(key_offset, mvm->fw_key_table);
3540 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3541 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3542 sta ? sta->addr : zero_addr, ret);
3546 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3547 struct ieee80211_vif *vif,
3548 struct ieee80211_sta *sta,
3549 struct ieee80211_key_conf *keyconf)
3551 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3552 struct iwl_mvm_sta *mvm_sta;
3553 u8 sta_id = IWL_MVM_INVALID_STA;
3556 lockdep_assert_held(&mvm->mutex);
3558 /* Get the station from the mvm local station table */
3559 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3561 sta_id = mvm_sta->sta_id;
3562 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3563 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3566 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3567 keyconf->keyidx, sta_id);
3569 if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3570 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3571 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
3572 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3574 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3575 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3576 keyconf->hw_key_idx);
3580 /* track which key was deleted last */
3581 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3582 if (mvm->fw_key_deleted[i] < U8_MAX)
3583 mvm->fw_key_deleted[i]++;
3585 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3587 if (sta && !mvm_sta) {
3588 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3592 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3596 /* delete WEP key twice to get rid of (now useless) offset */
3597 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3598 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3599 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3604 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3605 struct ieee80211_vif *vif,
3606 struct ieee80211_key_conf *keyconf,
3607 struct ieee80211_sta *sta, u32 iv32,
3610 struct iwl_mvm_sta *mvm_sta;
3611 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3612 bool mfp = sta ? sta->mfp : false;
3616 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3617 if (WARN_ON_ONCE(!mvm_sta))
3619 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3620 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3627 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3628 struct ieee80211_sta *sta)
3630 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3631 struct iwl_mvm_add_sta_cmd cmd = {
3632 .add_modify = STA_MODE_MODIFY,
3633 .sta_id = mvmsta->sta_id,
3634 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3635 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3639 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3640 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3642 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3645 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3646 struct ieee80211_sta *sta,
3647 enum ieee80211_frame_release_type reason,
3648 u16 cnt, u16 tids, bool more_data,
3649 bool single_sta_queue)
3651 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3652 struct iwl_mvm_add_sta_cmd cmd = {
3653 .add_modify = STA_MODE_MODIFY,
3654 .sta_id = mvmsta->sta_id,
3655 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3656 .sleep_tx_count = cpu_to_le16(cnt),
3657 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3660 unsigned long _tids = tids;
3662 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3663 * Note that this field is reserved and unused by firmware not
3664 * supporting GO uAPSD, so it's safe to always do this.
3666 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3667 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3669 /* If we're releasing frames from aggregation or dqa queues then check
3670 * if all the queues that we're releasing frames from, combined, have:
3671 * - more frames than the service period, in which case more_data
3673 * - fewer than 'cnt' frames, in which case we need to adjust the
3674 * firmware command (but do that unconditionally)
3676 if (single_sta_queue) {
3677 int remaining = cnt;
3680 spin_lock_bh(&mvmsta->lock);
3681 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3682 struct iwl_mvm_tid_data *tid_data;
3685 tid_data = &mvmsta->tid_data[tid];
3687 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3688 if (n_queued > remaining) {
3693 remaining -= n_queued;
3695 sleep_tx_count = cnt - remaining;
3696 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3697 mvmsta->sleep_tx_count = sleep_tx_count;
3698 spin_unlock_bh(&mvmsta->lock);
3700 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3701 if (WARN_ON(cnt - remaining == 0)) {
3702 ieee80211_sta_eosp(sta);
3707 /* Note: this is ignored by firmware not supporting GO uAPSD */
3709 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3711 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3712 mvmsta->next_status_eosp = true;
3713 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3715 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3718 /* block the Tx queues until the FW updated the sleep Tx count */
3719 iwl_trans_block_txq_ptrs(mvm->trans, true);
3721 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3722 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3723 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3725 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3728 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3729 struct iwl_rx_cmd_buffer *rxb)
3731 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3732 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3733 struct ieee80211_sta *sta;
3734 u32 sta_id = le32_to_cpu(notif->sta_id);
3736 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3740 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3741 if (!IS_ERR_OR_NULL(sta))
3742 ieee80211_sta_eosp(sta);
3746 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3747 struct iwl_mvm_sta *mvmsta, bool disable)
3749 struct iwl_mvm_add_sta_cmd cmd = {
3750 .add_modify = STA_MODE_MODIFY,
3751 .sta_id = mvmsta->sta_id,
3752 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3753 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3754 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3758 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3759 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3761 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3764 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3765 struct ieee80211_sta *sta,
3768 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3770 spin_lock_bh(&mvm_sta->lock);
3772 if (mvm_sta->disable_tx == disable) {
3773 spin_unlock_bh(&mvm_sta->lock);
3777 mvm_sta->disable_tx = disable;
3779 /* Tell mac80211 to start/stop queuing tx for this station */
3780 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3782 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3784 spin_unlock_bh(&mvm_sta->lock);
3787 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3788 struct iwl_mvm_vif *mvmvif,
3789 struct iwl_mvm_int_sta *sta,
3792 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3793 struct iwl_mvm_add_sta_cmd cmd = {
3794 .add_modify = STA_MODE_MODIFY,
3795 .sta_id = sta->sta_id,
3796 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3797 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3798 .mac_id_n_color = cpu_to_le32(id),
3802 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3803 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3805 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3808 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3809 struct iwl_mvm_vif *mvmvif,
3812 struct ieee80211_sta *sta;
3813 struct iwl_mvm_sta *mvm_sta;
3816 lockdep_assert_held(&mvm->mutex);
3818 /* Block/unblock all the stations of the given mvmvif */
3819 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3820 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3821 lockdep_is_held(&mvm->mutex));
3822 if (IS_ERR_OR_NULL(sta))
3825 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3826 if (mvm_sta->mac_id_n_color !=
3827 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3830 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3833 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3836 /* Need to block/unblock also multicast station */
3837 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3838 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3839 &mvmvif->mcast_sta, disable);
3842 * Only unblock the broadcast station (FW blocks it for immediate
3843 * quiet, not the driver)
3845 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3846 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3847 &mvmvif->bcast_sta, disable);
3850 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3852 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3853 struct iwl_mvm_sta *mvmsta;
3857 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3859 if (!WARN_ON(!mvmsta))
3860 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3865 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3867 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3870 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3871 * to align the wrap around of ssn so we compare relevant values.
3873 if (mvm->trans->cfg->gen2)
3876 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);