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 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 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
27 * The full GNU General Public License is included in this distribution
28 * in the file called COPYING.
30 * Contact Information:
31 * Intel Linux Wireless <linuxwifi@intel.com>
32 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
36 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
37 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
38 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
39 * Copyright(c) 2018 Intel Corporation
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
46 * * Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * * Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in
50 * the documentation and/or other materials provided with the
52 * * Neither the name Intel Corporation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
57 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
58 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
59 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
60 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
61 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
62 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
63 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
64 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
66 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 *****************************************************************************/
69 #include <net/mac80211.h>
76 * New version of ADD_STA_sta command added new fields at the end of the
77 * structure, so sending the size of the relevant API's structure is enough to
78 * support both API versions.
80 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
82 if (iwl_mvm_has_new_rx_api(mvm) ||
83 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
84 return sizeof(struct iwl_mvm_add_sta_cmd);
86 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
89 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
90 enum nl80211_iftype iftype)
95 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
96 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
98 lockdep_assert_held(&mvm->mutex);
100 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
101 if (iftype != NL80211_IFTYPE_STATION)
102 reserved_ids = BIT(0);
104 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
105 for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
106 if (BIT(sta_id) & reserved_ids)
109 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
110 lockdep_is_held(&mvm->mutex)))
113 return IWL_MVM_INVALID_STA;
116 /* send station add/update command to firmware */
117 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
118 bool update, unsigned int flags)
120 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
121 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
122 .sta_id = mvm_sta->sta_id,
123 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
124 .add_modify = update ? 1 : 0,
125 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
126 STA_FLG_MIMO_EN_MSK |
127 STA_FLG_RTS_MIMO_PROT),
128 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
132 u32 agg_size = 0, mpdu_dens = 0;
134 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
135 add_sta_cmd.station_type = mvm_sta->sta_type;
137 if (!update || (flags & STA_MODIFY_QUEUES)) {
138 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
140 if (!iwl_mvm_has_new_tx_api(mvm)) {
141 add_sta_cmd.tfd_queue_msk =
142 cpu_to_le32(mvm_sta->tfd_queue_msk);
144 if (flags & STA_MODIFY_QUEUES)
145 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
147 WARN_ON(flags & STA_MODIFY_QUEUES);
151 switch (sta->bandwidth) {
152 case IEEE80211_STA_RX_BW_160:
153 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
155 case IEEE80211_STA_RX_BW_80:
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
158 case IEEE80211_STA_RX_BW_40:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
161 case IEEE80211_STA_RX_BW_20:
162 if (sta->ht_cap.ht_supported)
163 add_sta_cmd.station_flags |=
164 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
168 switch (sta->rx_nss) {
170 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
173 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
176 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
180 switch (sta->smps_mode) {
181 case IEEE80211_SMPS_AUTOMATIC:
182 case IEEE80211_SMPS_NUM_MODES:
185 case IEEE80211_SMPS_STATIC:
187 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
188 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
190 case IEEE80211_SMPS_DYNAMIC:
191 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
193 case IEEE80211_SMPS_OFF:
198 if (sta->ht_cap.ht_supported) {
199 add_sta_cmd.station_flags_msk |=
200 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
201 STA_FLG_AGG_MPDU_DENS_MSK);
203 mpdu_dens = sta->ht_cap.ampdu_density;
206 if (sta->vht_cap.vht_supported) {
207 agg_size = sta->vht_cap.cap &
208 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
210 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
211 } else if (sta->ht_cap.ht_supported) {
212 agg_size = sta->ht_cap.ampdu_factor;
215 add_sta_cmd.station_flags |=
216 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
217 add_sta_cmd.station_flags |=
218 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
219 if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
220 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
223 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
225 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
226 add_sta_cmd.uapsd_acs |= BIT(AC_BK);
227 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
228 add_sta_cmd.uapsd_acs |= BIT(AC_BE);
229 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
230 add_sta_cmd.uapsd_acs |= BIT(AC_VI);
231 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
232 add_sta_cmd.uapsd_acs |= BIT(AC_VO);
233 add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
234 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
237 status = ADD_STA_SUCCESS;
238 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
239 iwl_mvm_add_sta_cmd_size(mvm),
240 &add_sta_cmd, &status);
244 switch (status & IWL_ADD_STA_STATUS_MASK) {
245 case ADD_STA_SUCCESS:
246 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
250 IWL_ERR(mvm, "ADD_STA failed\n");
257 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
259 struct iwl_mvm_baid_data *data =
260 from_timer(data, t, session_timer);
261 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
262 struct iwl_mvm_baid_data *ba_data;
263 struct ieee80211_sta *sta;
264 struct iwl_mvm_sta *mvm_sta;
265 unsigned long timeout;
269 ba_data = rcu_dereference(*rcu_ptr);
271 if (WARN_ON(!ba_data))
274 if (!ba_data->timeout)
277 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
278 if (time_is_after_jiffies(timeout)) {
279 mod_timer(&ba_data->session_timer, timeout);
284 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
287 * sta should be valid unless the following happens:
288 * The firmware asserts which triggers a reconfig flow, but
289 * the reconfig fails before we set the pointer to sta into
290 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
291 * A-MDPU and hence the timer continues to run. Then, the
292 * timer expires and sta is NULL.
297 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
298 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
299 sta->addr, ba_data->tid);
304 /* Disable aggregations for a bitmap of TIDs for a given station */
305 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
306 unsigned long disable_agg_tids,
309 struct iwl_mvm_add_sta_cmd cmd = {};
310 struct ieee80211_sta *sta;
311 struct iwl_mvm_sta *mvmsta;
316 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
319 spin_lock_bh(&mvm->queue_info_lock);
320 sta_id = mvm->queue_info[queue].ra_sta_id;
321 spin_unlock_bh(&mvm->queue_info_lock);
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 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
352 iwl_mvm_add_sta_cmd_size(mvm),
358 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
360 struct ieee80211_sta *sta;
361 struct iwl_mvm_sta *mvmsta;
362 unsigned long tid_bitmap;
363 unsigned long agg_tids = 0;
367 lockdep_assert_held(&mvm->mutex);
369 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
372 spin_lock_bh(&mvm->queue_info_lock);
373 sta_id = mvm->queue_info[queue].ra_sta_id;
374 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
375 spin_unlock_bh(&mvm->queue_info_lock);
377 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
378 lockdep_is_held(&mvm->mutex));
380 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
383 mvmsta = iwl_mvm_sta_from_mac80211(sta);
385 spin_lock_bh(&mvmsta->lock);
386 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
387 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
388 agg_tids |= BIT(tid);
390 spin_unlock_bh(&mvmsta->lock);
396 * Remove a queue from a station's resources.
397 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
398 * doesn't disable the queue
400 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
402 struct ieee80211_sta *sta;
403 struct iwl_mvm_sta *mvmsta;
404 unsigned long tid_bitmap;
405 unsigned long disable_agg_tids = 0;
409 lockdep_assert_held(&mvm->mutex);
411 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
414 spin_lock_bh(&mvm->queue_info_lock);
415 sta_id = mvm->queue_info[queue].ra_sta_id;
416 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
417 spin_unlock_bh(&mvm->queue_info_lock);
421 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
423 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
428 mvmsta = iwl_mvm_sta_from_mac80211(sta);
430 spin_lock_bh(&mvmsta->lock);
431 /* Unmap MAC queues and TIDs from this queue */
432 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
433 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
434 disable_agg_tids |= BIT(tid);
435 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
438 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
439 spin_unlock_bh(&mvmsta->lock);
444 * The TX path may have been using this TXQ_ID from the tid_data,
445 * so make sure it's no longer running so that we can safely reuse
446 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
447 * above, but nothing guarantees we've stopped using them. Thus,
448 * without this, we could get to iwl_mvm_disable_txq() and remove
449 * the queue while still sending frames to it.
453 return disable_agg_tids;
456 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
459 struct iwl_mvm_sta *mvmsta;
460 u8 txq_curr_ac, sta_id, tid;
461 unsigned long disable_agg_tids = 0;
464 lockdep_assert_held(&mvm->mutex);
466 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
469 spin_lock_bh(&mvm->queue_info_lock);
470 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
471 sta_id = mvm->queue_info[queue].ra_sta_id;
472 tid = mvm->queue_info[queue].txq_tid;
473 spin_unlock_bh(&mvm->queue_info_lock);
475 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
476 if (WARN_ON(!mvmsta))
479 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
480 /* Disable the queue */
481 if (disable_agg_tids)
482 iwl_mvm_invalidate_sta_queue(mvm, queue,
483 disable_agg_tids, false);
485 ret = iwl_mvm_disable_txq(mvm, queue,
486 mvmsta->vif->hw_queue[txq_curr_ac],
489 /* Re-mark the inactive queue as inactive */
490 spin_lock_bh(&mvm->queue_info_lock);
491 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
492 spin_unlock_bh(&mvm->queue_info_lock);
494 "Failed to free inactive queue %d (ret=%d)\n",
500 /* If TXQ is allocated to another STA, update removal in FW */
502 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
507 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
508 unsigned long tfd_queue_mask, u8 ac)
511 u8 ac_to_queue[IEEE80211_NUM_ACS];
514 lockdep_assert_held(&mvm->queue_info_lock);
515 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
518 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
520 /* See what ACs the existing queues for this STA have */
521 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
522 /* Only DATA queues can be shared */
523 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
524 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
527 /* Don't try and take queues being reconfigured */
528 if (mvm->queue_info[queue].status ==
529 IWL_MVM_QUEUE_RECONFIGURING)
532 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
536 * The queue to share is chosen only from DATA queues as follows (in
537 * descending priority):
540 * 3. Highest AC queue that is lower than new AC
541 * 4. Any existing AC (there always is at least 1 DATA queue)
544 /* Priority 1: An AC_BE queue */
545 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
546 queue = ac_to_queue[IEEE80211_AC_BE];
547 /* Priority 2: Same AC queue */
548 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
549 queue = ac_to_queue[ac];
550 /* Priority 3a: If new AC is VO and VI exists - use VI */
551 else if (ac == IEEE80211_AC_VO &&
552 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
553 queue = ac_to_queue[IEEE80211_AC_VI];
554 /* Priority 3b: No BE so only AC less than the new one is BK */
555 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
556 queue = ac_to_queue[IEEE80211_AC_BK];
557 /* Priority 4a: No BE nor BK - use VI if exists */
558 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
559 queue = ac_to_queue[IEEE80211_AC_VI];
560 /* Priority 4b: No BE, BK nor VI - use VO if exists */
561 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
562 queue = ac_to_queue[IEEE80211_AC_VO];
564 /* Make sure queue found (or not) is legal */
565 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
566 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
567 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
568 IWL_ERR(mvm, "No DATA queues available to share\n");
572 /* Make sure the queue isn't in the middle of being reconfigured */
573 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
575 "TXQ %d is in the middle of re-config - try again\n",
584 * If a given queue has a higher AC than the TID stream that is being compared
585 * to, the queue needs to be redirected to the lower AC. This function does that
586 * in such a case, otherwise - if no redirection required - it does nothing,
587 * unless the %force param is true.
589 int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
590 int ac, int ssn, unsigned int wdg_timeout,
593 struct iwl_scd_txq_cfg_cmd cmd = {
595 .action = SCD_CFG_DISABLE_QUEUE,
601 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
605 * If the AC is lower than current one - FIFO needs to be redirected to
606 * the lowest one of the streams in the queue. Check if this is needed
608 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
609 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
610 * we need to check if the numerical value of X is LARGER than of Y.
612 spin_lock_bh(&mvm->queue_info_lock);
613 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
614 spin_unlock_bh(&mvm->queue_info_lock);
616 IWL_DEBUG_TX_QUEUES(mvm,
617 "No redirection needed on TXQ #%d\n",
622 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
623 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
624 cmd.tid = mvm->queue_info[queue].txq_tid;
625 mq = mvm->hw_queue_to_mac80211[queue];
626 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
627 spin_unlock_bh(&mvm->queue_info_lock);
629 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
630 queue, iwl_mvm_ac_to_tx_fifo[ac]);
632 /* Stop MAC queues and wait for this queue to empty */
633 iwl_mvm_stop_mac_queues(mvm, mq);
634 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
636 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
642 /* Before redirecting the queue we need to de-activate it */
643 iwl_trans_txq_disable(mvm->trans, queue, false);
644 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
646 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
649 /* Make sure the SCD wrptr is correctly set before reconfiguring */
650 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
652 /* Update the TID "owner" of the queue */
653 spin_lock_bh(&mvm->queue_info_lock);
654 mvm->queue_info[queue].txq_tid = tid;
655 spin_unlock_bh(&mvm->queue_info_lock);
657 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
659 /* Redirect to lower AC */
660 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
661 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
663 /* Update AC marking of the queue */
664 spin_lock_bh(&mvm->queue_info_lock);
665 mvm->queue_info[queue].mac80211_ac = ac;
666 spin_unlock_bh(&mvm->queue_info_lock);
669 * Mark queue as shared in transport if shared
670 * Note this has to be done after queue enablement because enablement
671 * can also set this value, and there is no indication there to shared
675 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
678 /* Continue using the MAC queues */
679 iwl_mvm_start_mac_queues(mvm, mq);
684 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
685 struct ieee80211_sta *sta, u8 ac,
688 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
689 unsigned int wdg_timeout =
690 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
691 u8 mac_queue = mvmsta->vif->hw_queue[ac];
694 lockdep_assert_held(&mvm->mutex);
696 IWL_DEBUG_TX_QUEUES(mvm,
697 "Allocating queue for sta %d on tid %d\n",
698 mvmsta->sta_id, tid);
699 queue = iwl_mvm_tvqm_enable_txq(mvm, mac_queue, mvmsta->sta_id, tid,
704 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
706 spin_lock_bh(&mvmsta->lock);
707 mvmsta->tid_data[tid].txq_id = queue;
708 mvmsta->tid_data[tid].is_tid_active = true;
709 spin_unlock_bh(&mvmsta->lock);
714 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
715 struct ieee80211_sta *sta, u8 ac, int tid,
716 struct ieee80211_hdr *hdr)
718 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
719 struct iwl_trans_txq_scd_cfg cfg = {
720 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
721 .sta_id = mvmsta->sta_id,
723 .frame_limit = IWL_FRAME_LIMIT,
725 unsigned int wdg_timeout =
726 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
727 u8 mac_queue = mvmsta->vif->hw_queue[ac];
729 bool using_inactive_queue = false, same_sta = false;
730 unsigned long disable_agg_tids = 0;
731 enum iwl_mvm_agg_state queue_state;
732 bool shared_queue = false, inc_ssn;
734 unsigned long tfd_queue_mask;
737 lockdep_assert_held(&mvm->mutex);
739 if (iwl_mvm_has_new_tx_api(mvm))
740 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
742 spin_lock_bh(&mvmsta->lock);
743 tfd_queue_mask = mvmsta->tfd_queue_msk;
744 spin_unlock_bh(&mvmsta->lock);
746 spin_lock_bh(&mvm->queue_info_lock);
749 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
752 if (!ieee80211_is_data_qos(hdr->frame_control) ||
753 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
754 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
755 IWL_MVM_DQA_MIN_MGMT_QUEUE,
756 IWL_MVM_DQA_MAX_MGMT_QUEUE);
757 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
758 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
761 /* If no such queue is found, we'll use a DATA queue instead */
764 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
765 (mvm->queue_info[mvmsta->reserved_queue].status ==
766 IWL_MVM_QUEUE_RESERVED ||
767 mvm->queue_info[mvmsta->reserved_queue].status ==
768 IWL_MVM_QUEUE_INACTIVE)) {
769 queue = mvmsta->reserved_queue;
770 mvm->queue_info[queue].reserved = true;
771 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
775 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
776 IWL_MVM_DQA_MIN_DATA_QUEUE,
777 IWL_MVM_DQA_MAX_DATA_QUEUE);
780 * Check if this queue is already allocated but inactive.
781 * In such a case, we'll need to first free this queue before enabling
782 * it again, so we'll mark it as reserved to make sure no new traffic
786 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
787 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
788 using_inactive_queue = true;
789 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
790 IWL_DEBUG_TX_QUEUES(mvm,
791 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
792 queue, mvmsta->sta_id, tid);
795 /* No free queue - we'll have to share */
797 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
800 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
805 * Mark TXQ as ready, even though it hasn't been fully configured yet,
806 * to make sure no one else takes it.
807 * This will allow avoiding re-acquiring the lock at the end of the
808 * configuration. On error we'll mark it back as free.
810 if ((queue > 0) && !shared_queue)
811 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
813 spin_unlock_bh(&mvm->queue_info_lock);
815 /* This shouldn't happen - out of queues */
816 if (WARN_ON(queue <= 0)) {
817 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
823 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
824 * but for configuring the SCD to send A-MPDUs we need to mark the queue
826 * Mark all DATA queues as allowing to be aggregated at some point
828 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
829 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
832 * If this queue was previously inactive (idle) - we need to free it
835 if (using_inactive_queue) {
836 ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
841 IWL_DEBUG_TX_QUEUES(mvm,
842 "Allocating %squeue #%d to sta %d on tid %d\n",
843 shared_queue ? "shared " : "", queue,
844 mvmsta->sta_id, tid);
847 /* Disable any open aggs on this queue */
848 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
850 if (disable_agg_tids) {
851 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
853 iwl_mvm_invalidate_sta_queue(mvm, queue,
854 disable_agg_tids, false);
858 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
859 inc_ssn = iwl_mvm_enable_txq(mvm, queue, mac_queue,
860 ssn, &cfg, wdg_timeout);
862 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
863 le16_add_cpu(&hdr->seq_ctrl, 0x10);
867 * Mark queue as shared in transport if shared
868 * Note this has to be done after queue enablement because enablement
869 * can also set this value, and there is no indication there to shared
873 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
875 spin_lock_bh(&mvmsta->lock);
877 * This looks racy, but it is not. We have only one packet for
878 * this ra/tid in our Tx path since we stop the Qdisc when we
879 * need to allocate a new TFD queue.
882 mvmsta->tid_data[tid].seq_number += 0x10;
883 mvmsta->tid_data[tid].txq_id = queue;
884 mvmsta->tid_data[tid].is_tid_active = true;
885 mvmsta->tfd_queue_msk |= BIT(queue);
886 queue_state = mvmsta->tid_data[tid].state;
888 if (mvmsta->reserved_queue == queue)
889 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
890 spin_unlock_bh(&mvmsta->lock);
893 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
897 /* If we need to re-enable aggregations... */
898 if (queue_state == IWL_AGG_ON) {
899 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
904 /* Redirect queue, if needed */
905 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
914 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
919 static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
921 struct iwl_scd_txq_cfg_cmd cmd = {
923 .action = SCD_CFG_UPDATE_QUEUE_TID,
926 unsigned long tid_bitmap;
929 lockdep_assert_held(&mvm->mutex);
931 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
934 spin_lock_bh(&mvm->queue_info_lock);
935 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
936 spin_unlock_bh(&mvm->queue_info_lock);
938 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
941 /* Find any TID for queue */
942 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
944 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
946 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
948 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
953 spin_lock_bh(&mvm->queue_info_lock);
954 mvm->queue_info[queue].txq_tid = tid;
955 spin_unlock_bh(&mvm->queue_info_lock);
956 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
960 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
962 struct ieee80211_sta *sta;
963 struct iwl_mvm_sta *mvmsta;
966 unsigned long tid_bitmap;
967 unsigned int wdg_timeout;
971 /* queue sharing is disabled on new TX path */
972 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
975 lockdep_assert_held(&mvm->mutex);
977 spin_lock_bh(&mvm->queue_info_lock);
978 sta_id = mvm->queue_info[queue].ra_sta_id;
979 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
980 spin_unlock_bh(&mvm->queue_info_lock);
982 /* Find TID for queue, and make sure it is the only one on the queue */
983 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
984 if (tid_bitmap != BIT(tid)) {
985 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
990 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
993 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
994 lockdep_is_held(&mvm->mutex));
996 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
999 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1000 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1002 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1004 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
1005 tid_to_mac80211_ac[tid], ssn,
1008 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1012 /* If aggs should be turned back on - do it */
1013 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
1014 struct iwl_mvm_add_sta_cmd cmd = {0};
1016 mvmsta->tid_disable_agg &= ~BIT(tid);
1018 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1019 cmd.sta_id = mvmsta->sta_id;
1020 cmd.add_modify = STA_MODE_MODIFY;
1021 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1022 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1023 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1025 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1026 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1028 IWL_DEBUG_TX_QUEUES(mvm,
1029 "TXQ #%d is now aggregated again\n",
1032 /* Mark queue intenally as aggregating again */
1033 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1037 spin_lock_bh(&mvm->queue_info_lock);
1038 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1039 spin_unlock_bh(&mvm->queue_info_lock);
1042 static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1044 if (tid == IWL_MAX_TID_COUNT)
1045 return IEEE80211_AC_VO; /* MGMT */
1047 return tid_to_mac80211_ac[tid];
1050 static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
1051 struct ieee80211_sta *sta, int tid)
1053 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1054 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1055 struct sk_buff *skb;
1056 struct ieee80211_hdr *hdr;
1057 struct sk_buff_head deferred_tx;
1059 bool no_queue = false; /* Marks if there is a problem with the queue */
1062 lockdep_assert_held(&mvm->mutex);
1064 skb = skb_peek(&tid_data->deferred_tx_frames);
1067 hdr = (void *)skb->data;
1069 ac = iwl_mvm_tid_to_ac_queue(tid);
1070 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
1072 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE &&
1073 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
1075 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
1076 mvmsta->sta_id, tid);
1079 * Mark queue as problematic so later the deferred traffic is
1080 * freed, as we can do nothing with it
1085 __skb_queue_head_init(&deferred_tx);
1087 /* Disable bottom-halves when entering TX path */
1089 spin_lock(&mvmsta->lock);
1090 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
1091 mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
1092 spin_unlock(&mvmsta->lock);
1094 while ((skb = __skb_dequeue(&deferred_tx)))
1095 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1096 ieee80211_free_txskb(mvm->hw, skb);
1100 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1103 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1105 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1107 struct ieee80211_sta *sta;
1108 struct iwl_mvm_sta *mvmsta;
1109 unsigned long deferred_tid_traffic;
1110 int queue, sta_id, tid;
1112 /* Check inactivity of queues */
1113 iwl_mvm_inactivity_check(mvm);
1115 mutex_lock(&mvm->mutex);
1117 /* No queue reconfiguration in TVQM mode */
1118 if (iwl_mvm_has_new_tx_api(mvm))
1121 /* Reconfigure queues requiring reconfiguation */
1122 for (queue = 0; queue < ARRAY_SIZE(mvm->queue_info); queue++) {
1126 spin_lock_bh(&mvm->queue_info_lock);
1127 reconfig = (mvm->queue_info[queue].status ==
1128 IWL_MVM_QUEUE_RECONFIGURING);
1131 * We need to take into account a situation in which a TXQ was
1132 * allocated to TID x, and then turned shared by adding TIDs y
1133 * and z. If TID x becomes inactive and is removed from the TXQ,
1134 * ownership must be given to one of the remaining TIDs.
1135 * This is mainly because if TID x continues - a new queue can't
1136 * be allocated for it as long as it is an owner of another TXQ.
1138 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1139 BIT(mvm->queue_info[queue].txq_tid)) &&
1140 (mvm->queue_info[queue].status ==
1141 IWL_MVM_QUEUE_SHARED);
1142 spin_unlock_bh(&mvm->queue_info_lock);
1145 iwl_mvm_unshare_queue(mvm, queue);
1146 else if (change_owner)
1147 iwl_mvm_change_queue_owner(mvm, queue);
1151 /* Go over all stations with deferred traffic */
1152 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1153 IWL_MVM_STATION_COUNT) {
1154 clear_bit(sta_id, mvm->sta_deferred_frames);
1155 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1156 lockdep_is_held(&mvm->mutex));
1157 if (IS_ERR_OR_NULL(sta))
1160 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1161 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1163 for_each_set_bit(tid, &deferred_tid_traffic,
1164 IWL_MAX_TID_COUNT + 1)
1165 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1168 mutex_unlock(&mvm->mutex);
1171 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1172 struct ieee80211_sta *sta,
1173 enum nl80211_iftype vif_type)
1175 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1177 bool using_inactive_queue = false, same_sta = false;
1179 /* queue reserving is disabled on new TX path */
1180 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1184 * Check for inactive queues, so we don't reach a situation where we
1185 * can't add a STA due to a shortage in queues that doesn't really exist
1187 iwl_mvm_inactivity_check(mvm);
1189 spin_lock_bh(&mvm->queue_info_lock);
1191 /* Make sure we have free resources for this STA */
1192 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1193 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
1194 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1195 IWL_MVM_QUEUE_FREE))
1196 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1198 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1199 IWL_MVM_DQA_MIN_DATA_QUEUE,
1200 IWL_MVM_DQA_MAX_DATA_QUEUE);
1202 spin_unlock_bh(&mvm->queue_info_lock);
1203 IWL_ERR(mvm, "No available queues for new station\n");
1205 } else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
1207 * If this queue is already allocated but inactive we'll need to
1208 * first free this queue before enabling it again, we'll mark
1209 * it as reserved to make sure no new traffic arrives on it
1211 using_inactive_queue = true;
1212 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
1214 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1216 spin_unlock_bh(&mvm->queue_info_lock);
1218 mvmsta->reserved_queue = queue;
1220 if (using_inactive_queue)
1221 iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
1223 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1224 queue, mvmsta->sta_id);
1230 * In DQA mode, after a HW restart the queues should be allocated as before, in
1231 * order to avoid race conditions when there are shared queues. This function
1232 * does the re-mapping and queue allocation.
1234 * Note that re-enabling aggregations isn't done in this function.
1236 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1237 struct iwl_mvm_sta *mvm_sta)
1239 unsigned int wdg_timeout =
1240 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1242 struct iwl_trans_txq_scd_cfg cfg = {
1243 .sta_id = mvm_sta->sta_id,
1244 .frame_limit = IWL_FRAME_LIMIT,
1247 /* Make sure reserved queue is still marked as such (if allocated) */
1248 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1249 mvm->queue_info[mvm_sta->reserved_queue].status =
1250 IWL_MVM_QUEUE_RESERVED;
1252 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1253 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1254 int txq_id = tid_data->txq_id;
1258 if (txq_id == IWL_MVM_INVALID_QUEUE)
1261 skb_queue_head_init(&tid_data->deferred_tx_frames);
1263 ac = tid_to_mac80211_ac[i];
1264 mac_queue = mvm_sta->vif->hw_queue[ac];
1266 if (iwl_mvm_has_new_tx_api(mvm)) {
1267 IWL_DEBUG_TX_QUEUES(mvm,
1268 "Re-mapping sta %d tid %d\n",
1269 mvm_sta->sta_id, i);
1270 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mac_queue,
1273 tid_data->txq_id = txq_id;
1276 * Since we don't set the seq number after reset, and HW
1277 * sets it now, FW reset will cause the seq num to start
1278 * at 0 again, so driver will need to update it
1279 * internally as well, so it keeps in sync with real val
1281 tid_data->seq_number = 0;
1283 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1286 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1287 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1289 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1291 IWL_DEBUG_TX_QUEUES(mvm,
1292 "Re-mapping sta %d tid %d to queue %d\n",
1293 mvm_sta->sta_id, i, txq_id);
1295 iwl_mvm_enable_txq(mvm, txq_id, mac_queue, seq, &cfg,
1297 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1302 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1303 struct iwl_mvm_int_sta *sta,
1305 u16 mac_id, u16 color)
1307 struct iwl_mvm_add_sta_cmd cmd;
1309 u32 status = ADD_STA_SUCCESS;
1311 lockdep_assert_held(&mvm->mutex);
1313 memset(&cmd, 0, sizeof(cmd));
1314 cmd.sta_id = sta->sta_id;
1315 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1317 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1318 cmd.station_type = sta->type;
1320 if (!iwl_mvm_has_new_tx_api(mvm))
1321 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1322 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1325 memcpy(cmd.addr, addr, ETH_ALEN);
1327 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1328 iwl_mvm_add_sta_cmd_size(mvm),
1333 switch (status & IWL_ADD_STA_STATUS_MASK) {
1334 case ADD_STA_SUCCESS:
1335 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1339 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1346 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1347 struct ieee80211_vif *vif,
1348 struct ieee80211_sta *sta)
1350 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1351 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1352 struct iwl_mvm_rxq_dup_data *dup_data;
1354 bool sta_update = false;
1355 unsigned int sta_flags = 0;
1357 lockdep_assert_held(&mvm->mutex);
1359 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1360 sta_id = iwl_mvm_find_free_sta_id(mvm,
1361 ieee80211_vif_type_p2p(vif));
1363 sta_id = mvm_sta->sta_id;
1365 if (sta_id == IWL_MVM_INVALID_STA)
1368 spin_lock_init(&mvm_sta->lock);
1370 /* if this is a HW restart re-alloc existing queues */
1371 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1372 struct iwl_mvm_int_sta tmp_sta = {
1374 .type = mvm_sta->sta_type,
1378 * First add an empty station since allocating
1379 * a queue requires a valid station
1381 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1382 mvmvif->id, mvmvif->color);
1386 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1388 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1392 mvm_sta->sta_id = sta_id;
1393 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1396 if (!mvm->trans->cfg->gen2)
1397 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1399 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1400 mvm_sta->tx_protection = 0;
1401 mvm_sta->tt_tx_protection = false;
1402 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1404 /* HW restart, don't assume the memory has been zeroed */
1405 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1406 mvm_sta->tfd_queue_msk = 0;
1408 /* for HW restart - reset everything but the sequence number */
1409 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1410 u16 seq = mvm_sta->tid_data[i].seq_number;
1411 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1412 mvm_sta->tid_data[i].seq_number = seq;
1415 * Mark all queues for this STA as unallocated and defer TX
1416 * frames until the queue is allocated
1418 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1419 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
1421 mvm_sta->deferred_traffic_tid_map = 0;
1422 mvm_sta->agg_tids = 0;
1424 if (iwl_mvm_has_new_rx_api(mvm) &&
1425 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1428 dup_data = kcalloc(mvm->trans->num_rx_queues,
1429 sizeof(*dup_data), GFP_KERNEL);
1433 * Initialize all the last_seq values to 0xffff which can never
1434 * compare equal to the frame's seq_ctrl in the check in
1435 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1436 * number and fragmented packets don't reach that function.
1438 * This thus allows receiving a packet with seqno 0 and the
1439 * retry bit set as the very first packet on a new TID.
1441 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1442 memset(dup_data[q].last_seq, 0xff,
1443 sizeof(dup_data[q].last_seq));
1444 mvm_sta->dup_data = dup_data;
1447 if (!iwl_mvm_has_new_tx_api(mvm)) {
1448 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1449 ieee80211_vif_type_p2p(vif));
1455 * if rs is registered with mac80211, then "add station" will be handled
1456 * via the corresponding ops, otherwise need to notify rate scaling here
1458 if (iwl_mvm_has_tlc_offload(mvm))
1459 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1462 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1466 if (vif->type == NL80211_IFTYPE_STATION) {
1468 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1469 mvmvif->ap_sta_id = sta_id;
1471 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1475 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1483 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1486 struct iwl_mvm_add_sta_cmd cmd = {};
1490 lockdep_assert_held(&mvm->mutex);
1492 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1493 cmd.sta_id = mvmsta->sta_id;
1494 cmd.add_modify = STA_MODE_MODIFY;
1495 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1496 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1498 status = ADD_STA_SUCCESS;
1499 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1500 iwl_mvm_add_sta_cmd_size(mvm),
1505 switch (status & IWL_ADD_STA_STATUS_MASK) {
1506 case ADD_STA_SUCCESS:
1507 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1512 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1521 * Remove a station from the FW table. Before sending the command to remove
1522 * the station validate that the station is indeed known to the driver (sanity
1525 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1527 struct ieee80211_sta *sta;
1528 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1533 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1534 lockdep_is_held(&mvm->mutex));
1536 /* Note: internal stations are marked as error values */
1538 IWL_ERR(mvm, "Invalid station id\n");
1542 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1543 sizeof(rm_sta_cmd), &rm_sta_cmd);
1545 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1552 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1553 struct ieee80211_vif *vif,
1554 struct iwl_mvm_sta *mvm_sta)
1559 lockdep_assert_held(&mvm->mutex);
1561 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1562 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1565 ac = iwl_mvm_tid_to_ac_queue(i);
1566 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1567 vif->hw_queue[ac], i, 0);
1568 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1572 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1573 struct iwl_mvm_sta *mvm_sta)
1577 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1581 spin_lock_bh(&mvm_sta->lock);
1582 txq_id = mvm_sta->tid_data[i].txq_id;
1583 spin_unlock_bh(&mvm_sta->lock);
1585 if (txq_id == IWL_MVM_INVALID_QUEUE)
1588 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1596 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1597 struct ieee80211_vif *vif,
1598 struct ieee80211_sta *sta)
1600 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1601 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1602 u8 sta_id = mvm_sta->sta_id;
1605 lockdep_assert_held(&mvm->mutex);
1607 if (iwl_mvm_has_new_rx_api(mvm))
1608 kfree(mvm_sta->dup_data);
1610 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1614 /* flush its queues here since we are freeing mvm_sta */
1615 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1618 if (iwl_mvm_has_new_tx_api(mvm)) {
1619 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1621 u32 q_mask = mvm_sta->tfd_queue_msk;
1623 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1629 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1631 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
1633 /* If there is a TXQ still marked as reserved - free it */
1634 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1635 u8 reserved_txq = mvm_sta->reserved_queue;
1636 enum iwl_mvm_queue_status *status;
1639 * If no traffic has gone through the reserved TXQ - it
1640 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1641 * should be manually marked as free again
1643 spin_lock_bh(&mvm->queue_info_lock);
1644 status = &mvm->queue_info[reserved_txq].status;
1645 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1646 (*status != IWL_MVM_QUEUE_FREE),
1647 "sta_id %d reserved txq %d status %d",
1648 sta_id, reserved_txq, *status)) {
1649 spin_unlock_bh(&mvm->queue_info_lock);
1653 *status = IWL_MVM_QUEUE_FREE;
1654 spin_unlock_bh(&mvm->queue_info_lock);
1657 if (vif->type == NL80211_IFTYPE_STATION &&
1658 mvmvif->ap_sta_id == sta_id) {
1659 /* if associated - we can't remove the AP STA now */
1660 if (vif->bss_conf.assoc)
1663 /* unassoc - go ahead - remove the AP STA now */
1664 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1666 /* clear d0i3_ap_sta_id if no longer relevant */
1667 if (mvm->d0i3_ap_sta_id == sta_id)
1668 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1672 * This shouldn't happen - the TDLS channel switch should be canceled
1673 * before the STA is removed.
1675 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1676 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1677 cancel_delayed_work(&mvm->tdls_cs.dwork);
1681 * Make sure that the tx response code sees the station as -EBUSY and
1682 * calls the drain worker.
1684 spin_lock_bh(&mvm_sta->lock);
1685 spin_unlock_bh(&mvm_sta->lock);
1687 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1688 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1693 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1694 struct ieee80211_vif *vif,
1697 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1699 lockdep_assert_held(&mvm->mutex);
1701 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1705 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1706 struct iwl_mvm_int_sta *sta,
1707 u32 qmask, enum nl80211_iftype iftype,
1708 enum iwl_sta_type type)
1710 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1711 sta->sta_id == IWL_MVM_INVALID_STA) {
1712 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1713 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1717 sta->tfd_queue_msk = qmask;
1720 /* put a non-NULL value so iterating over the stations won't stop */
1721 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1725 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1727 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1728 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1729 sta->sta_id = IWL_MVM_INVALID_STA;
1732 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 *queue,
1735 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1736 mvm->cfg->base_params->wd_timeout :
1737 IWL_WATCHDOG_DISABLED;
1739 if (iwl_mvm_has_new_tx_api(mvm)) {
1741 iwl_mvm_tvqm_enable_txq(mvm, *queue, sta_id,
1744 *queue = tvqm_queue;
1746 struct iwl_trans_txq_scd_cfg cfg = {
1749 .tid = IWL_MAX_TID_COUNT,
1751 .frame_limit = IWL_FRAME_LIMIT,
1754 iwl_mvm_enable_txq(mvm, *queue, *queue, 0, &cfg, wdg_timeout);
1758 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1762 lockdep_assert_held(&mvm->mutex);
1764 /* Allocate aux station and assign to it the aux queue */
1765 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1766 NL80211_IFTYPE_UNSPECIFIED,
1767 IWL_STA_AUX_ACTIVITY);
1771 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1772 if (!iwl_mvm_has_new_tx_api(mvm))
1773 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
1774 mvm->aux_sta.sta_id,
1775 IWL_MVM_TX_FIFO_MCAST);
1777 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1780 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1785 * For 22000 firmware and on we cannot add queue to a station unknown
1786 * to firmware so enable queue here - after the station was added
1788 if (iwl_mvm_has_new_tx_api(mvm))
1789 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
1790 mvm->aux_sta.sta_id,
1791 IWL_MVM_TX_FIFO_MCAST);
1796 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1798 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1801 lockdep_assert_held(&mvm->mutex);
1803 /* Map snif queue to fifo - must happen before adding snif station */
1804 if (!iwl_mvm_has_new_tx_api(mvm))
1805 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
1806 mvm->snif_sta.sta_id,
1807 IWL_MVM_TX_FIFO_BE);
1809 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1815 * For 22000 firmware and on we cannot add queue to a station unknown
1816 * to firmware so enable queue here - after the station was added
1818 if (iwl_mvm_has_new_tx_api(mvm))
1819 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
1820 mvm->snif_sta.sta_id,
1821 IWL_MVM_TX_FIFO_BE);
1826 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1830 lockdep_assert_held(&mvm->mutex);
1832 iwl_mvm_disable_txq(mvm, mvm->snif_queue, mvm->snif_queue,
1833 IWL_MAX_TID_COUNT, 0);
1834 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1836 IWL_WARN(mvm, "Failed sending remove station\n");
1841 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1843 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1846 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1848 lockdep_assert_held(&mvm->mutex);
1850 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1854 * Send the add station command for the vif's broadcast station.
1855 * Assumes that the station was already allocated.
1857 * @mvm: the mvm component
1858 * @vif: the interface to which the broadcast station is added
1859 * @bsta: the broadcast station to add.
1861 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1863 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1864 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1865 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1866 const u8 *baddr = _baddr;
1869 unsigned int wdg_timeout =
1870 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1871 struct iwl_trans_txq_scd_cfg cfg = {
1872 .fifo = IWL_MVM_TX_FIFO_VO,
1873 .sta_id = mvmvif->bcast_sta.sta_id,
1874 .tid = IWL_MAX_TID_COUNT,
1876 .frame_limit = IWL_FRAME_LIMIT,
1879 lockdep_assert_held(&mvm->mutex);
1881 if (!iwl_mvm_has_new_tx_api(mvm)) {
1882 if (vif->type == NL80211_IFTYPE_AP ||
1883 vif->type == NL80211_IFTYPE_ADHOC)
1884 queue = mvm->probe_queue;
1885 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1886 queue = mvm->p2p_dev_queue;
1887 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
1890 bsta->tfd_queue_msk |= BIT(queue);
1892 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1896 if (vif->type == NL80211_IFTYPE_ADHOC)
1897 baddr = vif->bss_conf.bssid;
1899 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
1902 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1903 mvmvif->id, mvmvif->color);
1908 * For 22000 firmware and on we cannot add queue to a station unknown
1909 * to firmware so enable queue here - after the station was added
1911 if (iwl_mvm_has_new_tx_api(mvm)) {
1912 queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
1917 if (vif->type == NL80211_IFTYPE_AP ||
1918 vif->type == NL80211_IFTYPE_ADHOC)
1919 mvm->probe_queue = queue;
1920 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1921 mvm->p2p_dev_queue = queue;
1927 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
1928 struct ieee80211_vif *vif)
1930 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1933 lockdep_assert_held(&mvm->mutex);
1935 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
1937 switch (vif->type) {
1938 case NL80211_IFTYPE_AP:
1939 case NL80211_IFTYPE_ADHOC:
1940 queue = mvm->probe_queue;
1942 case NL80211_IFTYPE_P2P_DEVICE:
1943 queue = mvm->p2p_dev_queue;
1946 WARN(1, "Can't free bcast queue on vif type %d\n",
1951 iwl_mvm_disable_txq(mvm, queue, vif->hw_queue[0], IWL_MAX_TID_COUNT, 0);
1952 if (iwl_mvm_has_new_tx_api(mvm))
1955 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
1956 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
1959 /* Send the FW a request to remove the station from it's internal data
1960 * structures, but DO NOT remove the entry from the local data structures. */
1961 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1963 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1966 lockdep_assert_held(&mvm->mutex);
1968 iwl_mvm_free_bcast_sta_queues(mvm, vif);
1970 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
1972 IWL_WARN(mvm, "Failed sending remove station\n");
1976 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1978 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1980 lockdep_assert_held(&mvm->mutex);
1982 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
1983 ieee80211_vif_type_p2p(vif),
1984 IWL_STA_GENERAL_PURPOSE);
1987 /* Allocate a new station entry for the broadcast station to the given vif,
1988 * and send it to the FW.
1989 * Note that each P2P mac should have its own broadcast station.
1991 * @mvm: the mvm component
1992 * @vif: the interface to which the broadcast station is added
1993 * @bsta: the broadcast station to add. */
1994 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1996 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1997 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2000 lockdep_assert_held(&mvm->mutex);
2002 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2006 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2009 iwl_mvm_dealloc_int_sta(mvm, bsta);
2014 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2016 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2018 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2022 * Send the FW a request to remove the station from it's internal data
2023 * structures, and in addition remove it from the local data structure.
2025 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2029 lockdep_assert_held(&mvm->mutex);
2031 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2033 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2039 * Allocate a new station entry for the multicast station to the given vif,
2040 * and send it to the FW.
2041 * Note that each AP/GO mac should have its own multicast station.
2043 * @mvm: the mvm component
2044 * @vif: the interface to which the multicast station is added
2046 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2048 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2049 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2050 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2051 const u8 *maddr = _maddr;
2052 struct iwl_trans_txq_scd_cfg cfg = {
2053 .fifo = IWL_MVM_TX_FIFO_MCAST,
2054 .sta_id = msta->sta_id,
2057 .frame_limit = IWL_FRAME_LIMIT,
2059 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2062 lockdep_assert_held(&mvm->mutex);
2064 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2065 vif->type != NL80211_IFTYPE_ADHOC))
2069 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2070 * invalid, so make sure we use the queue we want.
2071 * Note that this is done here as we want to avoid making DQA
2072 * changes in mac80211 layer.
2074 if (vif->type == NL80211_IFTYPE_ADHOC) {
2075 vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2076 mvmvif->cab_queue = vif->cab_queue;
2080 * While in previous FWs we had to exclude cab queue from TFD queue
2081 * mask, now it is needed as any other queue.
2083 if (!iwl_mvm_has_new_tx_api(mvm) &&
2084 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2085 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2087 msta->tfd_queue_msk |= BIT(vif->cab_queue);
2089 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2090 mvmvif->id, mvmvif->color);
2092 iwl_mvm_dealloc_int_sta(mvm, msta);
2097 * Enable cab queue after the ADD_STA command is sent.
2098 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2099 * command with unknown station id, and for FW that doesn't support
2100 * station API since the cab queue is not included in the
2103 if (iwl_mvm_has_new_tx_api(mvm)) {
2104 int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2108 mvmvif->cab_queue = queue;
2109 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2110 IWL_UCODE_TLV_API_STA_TYPE))
2111 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2118 * Send the FW a request to remove the station from it's internal data
2119 * structures, and in addition remove it from the local data structure.
2121 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2123 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2126 lockdep_assert_held(&mvm->mutex);
2128 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2130 iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
2133 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2135 IWL_WARN(mvm, "Failed sending remove station\n");
2140 #define IWL_MAX_RX_BA_SESSIONS 16
2142 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2144 struct iwl_mvm_delba_notif notif = {
2145 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2149 iwl_mvm_sync_rx_queues_internal(mvm, (void *)¬if, sizeof(notif));
2152 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2153 struct iwl_mvm_baid_data *data)
2157 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2159 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2161 struct iwl_mvm_reorder_buffer *reorder_buf =
2162 &data->reorder_buf[i];
2163 struct iwl_mvm_reorder_buf_entry *entries =
2164 &data->entries[i * data->entries_per_queue];
2166 spin_lock_bh(&reorder_buf->lock);
2167 if (likely(!reorder_buf->num_stored)) {
2168 spin_unlock_bh(&reorder_buf->lock);
2173 * This shouldn't happen in regular DELBA since the internal
2174 * delBA notification should trigger a release of all frames in
2175 * the reorder buffer.
2179 for (j = 0; j < reorder_buf->buf_size; j++)
2180 __skb_queue_purge(&entries[j].e.frames);
2182 * Prevent timer re-arm. This prevents a very far fetched case
2183 * where we timed out on the notification. There may be prior
2184 * RX frames pending in the RX queue before the notification
2185 * that might get processed between now and the actual deletion
2186 * and we would re-arm the timer although we are deleting the
2189 reorder_buf->removed = true;
2190 spin_unlock_bh(&reorder_buf->lock);
2191 del_timer_sync(&reorder_buf->reorder_timer);
2195 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2196 struct iwl_mvm_baid_data *data,
2197 u16 ssn, u16 buf_size)
2201 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2202 struct iwl_mvm_reorder_buffer *reorder_buf =
2203 &data->reorder_buf[i];
2204 struct iwl_mvm_reorder_buf_entry *entries =
2205 &data->entries[i * data->entries_per_queue];
2208 reorder_buf->num_stored = 0;
2209 reorder_buf->head_sn = ssn;
2210 reorder_buf->buf_size = buf_size;
2211 /* rx reorder timer */
2212 timer_setup(&reorder_buf->reorder_timer,
2213 iwl_mvm_reorder_timer_expired, 0);
2214 spin_lock_init(&reorder_buf->lock);
2215 reorder_buf->mvm = mvm;
2216 reorder_buf->queue = i;
2217 reorder_buf->valid = false;
2218 for (j = 0; j < reorder_buf->buf_size; j++)
2219 __skb_queue_head_init(&entries[j].e.frames);
2223 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2224 int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2226 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2227 struct iwl_mvm_add_sta_cmd cmd = {};
2228 struct iwl_mvm_baid_data *baid_data = NULL;
2232 lockdep_assert_held(&mvm->mutex);
2234 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2235 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2239 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2240 u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2242 /* sparse doesn't like the __align() so don't check */
2245 * The division below will be OK if either the cache line size
2246 * can be divided by the entry size (ALIGN will round up) or if
2247 * if the entry size can be divided by the cache line size, in
2248 * which case the ALIGN() will do nothing.
2250 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2251 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2255 * Upward align the reorder buffer size to fill an entire cache
2256 * line for each queue, to avoid sharing cache lines between
2259 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2262 * Allocate here so if allocation fails we can bail out early
2263 * before starting the BA session in the firmware
2265 baid_data = kzalloc(sizeof(*baid_data) +
2266 mvm->trans->num_rx_queues *
2273 * This division is why we need the above BUILD_BUG_ON(),
2274 * if that doesn't hold then this will not be right.
2276 baid_data->entries_per_queue =
2277 reorder_buf_size / sizeof(baid_data->entries[0]);
2280 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2281 cmd.sta_id = mvm_sta->sta_id;
2282 cmd.add_modify = STA_MODE_MODIFY;
2284 cmd.add_immediate_ba_tid = (u8) tid;
2285 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2286 cmd.rx_ba_window = cpu_to_le16(buf_size);
2288 cmd.remove_immediate_ba_tid = (u8) tid;
2290 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2291 STA_MODIFY_REMOVE_BA_TID;
2293 status = ADD_STA_SUCCESS;
2294 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2295 iwl_mvm_add_sta_cmd_size(mvm),
2300 switch (status & IWL_ADD_STA_STATUS_MASK) {
2301 case ADD_STA_SUCCESS:
2302 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2303 start ? "start" : "stopp");
2305 case ADD_STA_IMMEDIATE_BA_FAILURE:
2306 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2311 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2312 start ? "start" : "stopp", status);
2322 mvm->rx_ba_sessions++;
2324 if (!iwl_mvm_has_new_rx_api(mvm))
2327 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2331 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2332 IWL_ADD_STA_BAID_SHIFT);
2333 baid_data->baid = baid;
2334 baid_data->timeout = timeout;
2335 baid_data->last_rx = jiffies;
2336 baid_data->rcu_ptr = &mvm->baid_map[baid];
2337 timer_setup(&baid_data->session_timer,
2338 iwl_mvm_rx_agg_session_expired, 0);
2339 baid_data->mvm = mvm;
2340 baid_data->tid = tid;
2341 baid_data->sta_id = mvm_sta->sta_id;
2343 mvm_sta->tid_to_baid[tid] = baid;
2345 mod_timer(&baid_data->session_timer,
2346 TU_TO_EXP_TIME(timeout * 2));
2348 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2350 * protect the BA data with RCU to cover a case where our
2351 * internal RX sync mechanism will timeout (not that it's
2352 * supposed to happen) and we will free the session data while
2353 * RX is being processed in parallel
2355 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2356 mvm_sta->sta_id, tid, baid);
2357 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2358 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2360 u8 baid = mvm_sta->tid_to_baid[tid];
2362 if (mvm->rx_ba_sessions > 0)
2363 /* check that restart flow didn't zero the counter */
2364 mvm->rx_ba_sessions--;
2365 if (!iwl_mvm_has_new_rx_api(mvm))
2368 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2371 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2372 if (WARN_ON(!baid_data))
2375 /* synchronize all rx queues so we can safely delete */
2376 iwl_mvm_free_reorder(mvm, baid_data);
2377 del_timer_sync(&baid_data->session_timer);
2378 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2379 kfree_rcu(baid_data, rcu_head);
2380 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2389 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2390 int tid, u8 queue, bool start)
2392 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2393 struct iwl_mvm_add_sta_cmd cmd = {};
2397 lockdep_assert_held(&mvm->mutex);
2400 mvm_sta->tfd_queue_msk |= BIT(queue);
2401 mvm_sta->tid_disable_agg &= ~BIT(tid);
2403 /* In DQA-mode the queue isn't removed on agg termination */
2404 mvm_sta->tid_disable_agg |= BIT(tid);
2407 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2408 cmd.sta_id = mvm_sta->sta_id;
2409 cmd.add_modify = STA_MODE_MODIFY;
2410 if (!iwl_mvm_has_new_tx_api(mvm))
2411 cmd.modify_mask = STA_MODIFY_QUEUES;
2412 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2413 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2414 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2416 status = ADD_STA_SUCCESS;
2417 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2418 iwl_mvm_add_sta_cmd_size(mvm),
2423 switch (status & IWL_ADD_STA_STATUS_MASK) {
2424 case ADD_STA_SUCCESS:
2428 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2429 start ? "start" : "stopp", status);
2436 const u8 tid_to_mac80211_ac[] = {
2445 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2448 static const u8 tid_to_ucode_ac[] = {
2459 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2460 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2462 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2463 struct iwl_mvm_tid_data *tid_data;
2468 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2471 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2472 mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2474 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2475 mvmsta->tid_data[tid].state);
2479 lockdep_assert_held(&mvm->mutex);
2481 if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2482 iwl_mvm_has_new_tx_api(mvm)) {
2483 u8 ac = tid_to_mac80211_ac[tid];
2485 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2490 spin_lock_bh(&mvmsta->lock);
2492 /* possible race condition - we entered D0i3 while starting agg */
2493 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2494 spin_unlock_bh(&mvmsta->lock);
2495 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2499 spin_lock(&mvm->queue_info_lock);
2502 * Note the possible cases:
2503 * 1. An enabled TXQ - TXQ needs to become agg'ed
2504 * 2. The TXQ hasn't yet been enabled, so find a free one and mark
2507 txq_id = mvmsta->tid_data[tid].txq_id;
2508 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2509 ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2510 IWL_MVM_DQA_MIN_DATA_QUEUE,
2511 IWL_MVM_DQA_MAX_DATA_QUEUE);
2513 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2519 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2520 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2521 } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2523 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2524 tid, IWL_MAX_HW_QUEUES - 1);
2527 } else if (unlikely(mvm->queue_info[txq_id].status ==
2528 IWL_MVM_QUEUE_SHARED)) {
2530 IWL_DEBUG_TX_QUEUES(mvm,
2531 "Can't start tid %d agg on shared queue!\n",
2536 spin_unlock(&mvm->queue_info_lock);
2538 IWL_DEBUG_TX_QUEUES(mvm,
2539 "AGG for tid %d will be on queue #%d\n",
2542 tid_data = &mvmsta->tid_data[tid];
2543 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2544 tid_data->txq_id = txq_id;
2545 *ssn = tid_data->ssn;
2547 IWL_DEBUG_TX_QUEUES(mvm,
2548 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2549 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2550 tid_data->next_reclaimed);
2553 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2554 * to align the wrap around of ssn so we compare relevant values.
2556 normalized_ssn = tid_data->ssn;
2557 if (mvm->trans->cfg->gen2)
2558 normalized_ssn &= 0xff;
2560 if (normalized_ssn == tid_data->next_reclaimed) {
2561 tid_data->state = IWL_AGG_STARTING;
2562 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2564 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2571 spin_unlock(&mvm->queue_info_lock);
2573 spin_unlock_bh(&mvmsta->lock);
2578 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2579 struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2582 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2583 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2584 unsigned int wdg_timeout =
2585 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2587 bool alloc_queue = true;
2588 enum iwl_mvm_queue_status queue_status;
2591 struct iwl_trans_txq_scd_cfg cfg = {
2592 .sta_id = mvmsta->sta_id,
2594 .frame_limit = buf_size,
2599 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2600 * manager, so this function should never be called in this case.
2602 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2605 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2606 != IWL_MAX_TID_COUNT);
2608 spin_lock_bh(&mvmsta->lock);
2609 ssn = tid_data->ssn;
2610 queue = tid_data->txq_id;
2611 tid_data->state = IWL_AGG_ON;
2612 mvmsta->agg_tids |= BIT(tid);
2613 tid_data->ssn = 0xffff;
2614 tid_data->amsdu_in_ampdu_allowed = amsdu;
2615 spin_unlock_bh(&mvmsta->lock);
2617 if (iwl_mvm_has_new_tx_api(mvm)) {
2619 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2620 * would have failed, so if we are here there is no need to
2622 * However, if aggregation size is different than the default
2623 * size, the scheduler should be reconfigured.
2624 * We cannot do this with the new TX API, so return unsupported
2625 * for now, until it will be offloaded to firmware..
2626 * Note that if SCD default value changes - this condition
2627 * should be updated as well.
2629 if (buf_size < IWL_FRAME_LIMIT)
2632 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2638 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2640 spin_lock_bh(&mvm->queue_info_lock);
2641 queue_status = mvm->queue_info[queue].status;
2642 spin_unlock_bh(&mvm->queue_info_lock);
2644 /* Maybe there is no need to even alloc a queue... */
2645 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2646 alloc_queue = false;
2649 * Only reconfig the SCD for the queue if the window size has
2650 * changed from current (become smaller)
2652 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2654 * If reconfiguring an existing queue, it first must be
2657 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2661 "Error draining queue before reconfig\n");
2665 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2666 mvmsta->sta_id, tid,
2670 "Error reconfiguring TXQ #%d\n", queue);
2676 iwl_mvm_enable_txq(mvm, queue,
2677 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2680 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2681 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2682 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2687 /* No need to mark as reserved */
2688 spin_lock_bh(&mvm->queue_info_lock);
2689 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2690 spin_unlock_bh(&mvm->queue_info_lock);
2694 * Even though in theory the peer could have different
2695 * aggregation reorder buffer sizes for different sessions,
2696 * our ucode doesn't allow for that and has a global limit
2697 * for each station. Therefore, use the minimum of all the
2698 * aggregation sessions and our default value.
2700 mvmsta->max_agg_bufsize =
2701 min(mvmsta->max_agg_bufsize, buf_size);
2702 mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2704 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2707 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq, false);
2710 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2711 struct iwl_mvm_sta *mvmsta,
2712 struct iwl_mvm_tid_data *tid_data)
2714 u16 txq_id = tid_data->txq_id;
2716 if (iwl_mvm_has_new_tx_api(mvm))
2719 spin_lock_bh(&mvm->queue_info_lock);
2721 * The TXQ is marked as reserved only if no traffic came through yet
2722 * This means no traffic has been sent on this TID (agg'd or not), so
2723 * we no longer have use for the queue. Since it hasn't even been
2724 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2727 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
2728 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2729 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
2732 spin_unlock_bh(&mvm->queue_info_lock);
2735 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2736 struct ieee80211_sta *sta, u16 tid)
2738 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2739 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2744 * If mac80211 is cleaning its state, then say that we finished since
2745 * our state has been cleared anyway.
2747 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2748 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2752 spin_lock_bh(&mvmsta->lock);
2754 txq_id = tid_data->txq_id;
2756 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2757 mvmsta->sta_id, tid, txq_id, tid_data->state);
2759 mvmsta->agg_tids &= ~BIT(tid);
2761 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
2763 switch (tid_data->state) {
2765 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2767 IWL_DEBUG_TX_QUEUES(mvm,
2768 "ssn = %d, next_recl = %d\n",
2769 tid_data->ssn, tid_data->next_reclaimed);
2771 tid_data->ssn = 0xffff;
2772 tid_data->state = IWL_AGG_OFF;
2773 spin_unlock_bh(&mvmsta->lock);
2775 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2777 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2779 case IWL_AGG_STARTING:
2780 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2782 * The agg session has been stopped before it was set up. This
2783 * can happen when the AddBA timer times out for example.
2786 /* No barriers since we are under mutex */
2787 lockdep_assert_held(&mvm->mutex);
2789 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2790 tid_data->state = IWL_AGG_OFF;
2795 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2796 mvmsta->sta_id, tid, tid_data->state);
2798 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2802 spin_unlock_bh(&mvmsta->lock);
2807 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2808 struct ieee80211_sta *sta, u16 tid)
2810 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2811 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2813 enum iwl_mvm_agg_state old_state;
2816 * First set the agg state to OFF to avoid calling
2817 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2819 spin_lock_bh(&mvmsta->lock);
2820 txq_id = tid_data->txq_id;
2821 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2822 mvmsta->sta_id, tid, txq_id, tid_data->state);
2823 old_state = tid_data->state;
2824 tid_data->state = IWL_AGG_OFF;
2825 mvmsta->agg_tids &= ~BIT(tid);
2826 spin_unlock_bh(&mvmsta->lock);
2828 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
2830 if (old_state >= IWL_AGG_ON) {
2831 iwl_mvm_drain_sta(mvm, mvmsta, true);
2833 if (iwl_mvm_has_new_tx_api(mvm)) {
2834 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
2836 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
2837 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
2839 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2840 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
2841 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
2844 iwl_mvm_drain_sta(mvm, mvmsta, false);
2846 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2852 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2854 int i, max = -1, max_offs = -1;
2856 lockdep_assert_held(&mvm->mutex);
2858 /* Pick the unused key offset with the highest 'deleted'
2859 * counter. Every time a key is deleted, all the counters
2860 * are incremented and the one that was just deleted is
2861 * reset to zero. Thus, the highest counter is the one
2862 * that was deleted longest ago. Pick that one.
2864 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2865 if (test_bit(i, mvm->fw_key_table))
2867 if (mvm->fw_key_deleted[i] > max) {
2868 max = mvm->fw_key_deleted[i];
2874 return STA_KEY_IDX_INVALID;
2879 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2880 struct ieee80211_vif *vif,
2881 struct ieee80211_sta *sta)
2883 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2886 return iwl_mvm_sta_from_mac80211(sta);
2889 * The device expects GTKs for station interfaces to be
2890 * installed as GTKs for the AP station. If we have no
2891 * station ID, then use AP's station ID.
2893 if (vif->type == NL80211_IFTYPE_STATION &&
2894 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2895 u8 sta_id = mvmvif->ap_sta_id;
2897 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2898 lockdep_is_held(&mvm->mutex));
2901 * It is possible that the 'sta' parameter is NULL,
2902 * for example when a GTK is removed - the sta_id will then
2903 * be the AP ID, and no station was passed by mac80211.
2905 if (IS_ERR_OR_NULL(sta))
2908 return iwl_mvm_sta_from_mac80211(sta);
2914 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2916 struct ieee80211_key_conf *key, bool mcast,
2917 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2918 u8 key_offset, bool mfp)
2921 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2922 struct iwl_mvm_add_sta_key_cmd cmd;
2930 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2931 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2933 if (sta_id == IWL_MVM_INVALID_STA)
2936 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
2937 STA_KEY_FLG_KEYID_MSK;
2938 key_flags = cpu_to_le16(keyidx);
2939 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2941 switch (key->cipher) {
2942 case WLAN_CIPHER_SUITE_TKIP:
2943 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
2945 memcpy((void *)&u.cmd.tx_mic_key,
2946 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
2949 memcpy((void *)&u.cmd.rx_mic_key,
2950 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
2952 pn = atomic64_read(&key->tx_pn);
2955 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
2956 for (i = 0; i < 5; i++)
2957 u.cmd_v1.tkip_rx_ttak[i] =
2958 cpu_to_le16(tkip_p1k[i]);
2960 memcpy(u.cmd.common.key, key->key, key->keylen);
2962 case WLAN_CIPHER_SUITE_CCMP:
2963 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
2964 memcpy(u.cmd.common.key, key->key, key->keylen);
2966 pn = atomic64_read(&key->tx_pn);
2968 case WLAN_CIPHER_SUITE_WEP104:
2969 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
2971 case WLAN_CIPHER_SUITE_WEP40:
2972 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2973 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
2975 case WLAN_CIPHER_SUITE_GCMP_256:
2976 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2978 case WLAN_CIPHER_SUITE_GCMP:
2979 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
2980 memcpy(u.cmd.common.key, key->key, key->keylen);
2982 pn = atomic64_read(&key->tx_pn);
2985 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2986 memcpy(u.cmd.common.key, key->key, key->keylen);
2990 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2992 key_flags |= cpu_to_le16(STA_KEY_MFP);
2994 u.cmd.common.key_offset = key_offset;
2995 u.cmd.common.key_flags = key_flags;
2996 u.cmd.common.sta_id = sta_id;
2999 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3000 size = sizeof(u.cmd);
3002 size = sizeof(u.cmd_v1);
3005 status = ADD_STA_SUCCESS;
3006 if (cmd_flags & CMD_ASYNC)
3007 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3010 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3014 case ADD_STA_SUCCESS:
3015 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3019 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3026 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3027 struct ieee80211_key_conf *keyconf,
3028 u8 sta_id, bool remove_key)
3030 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3032 /* verify the key details match the required command's expectations */
3033 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3034 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3035 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3036 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3037 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3040 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3041 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3044 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3045 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3048 /* This is a valid situation for IGTK */
3049 if (sta_id == IWL_MVM_INVALID_STA)
3052 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3054 struct ieee80211_key_seq seq;
3057 switch (keyconf->cipher) {
3058 case WLAN_CIPHER_SUITE_AES_CMAC:
3059 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3061 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3062 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3063 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3069 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3070 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3071 igtk_cmd.ctrl_flags |=
3072 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3073 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3074 pn = seq.aes_cmac.pn;
3075 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3076 ((u64) pn[4] << 8) |
3077 ((u64) pn[3] << 16) |
3078 ((u64) pn[2] << 24) |
3079 ((u64) pn[1] << 32) |
3080 ((u64) pn[0] << 40));
3083 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3084 remove_key ? "removing" : "installing",
3087 if (!iwl_mvm_has_new_rx_api(mvm)) {
3088 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3089 .ctrl_flags = igtk_cmd.ctrl_flags,
3090 .key_id = igtk_cmd.key_id,
3091 .sta_id = igtk_cmd.sta_id,
3092 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3095 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3096 ARRAY_SIZE(igtk_cmd_v1.igtk));
3097 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3098 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3100 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3101 sizeof(igtk_cmd), &igtk_cmd);
3105 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3106 struct ieee80211_vif *vif,
3107 struct ieee80211_sta *sta)
3109 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3114 if (vif->type == NL80211_IFTYPE_STATION &&
3115 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3116 u8 sta_id = mvmvif->ap_sta_id;
3117 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3118 lockdep_is_held(&mvm->mutex));
3126 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3127 struct ieee80211_vif *vif,
3128 struct ieee80211_sta *sta,
3129 struct ieee80211_key_conf *keyconf,
3135 struct ieee80211_key_seq seq;
3141 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3143 sta_id = mvm_sta->sta_id;
3145 } else if (vif->type == NL80211_IFTYPE_AP &&
3146 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3147 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3149 sta_id = mvmvif->mcast_sta.sta_id;
3151 IWL_ERR(mvm, "Failed to find station id\n");
3155 switch (keyconf->cipher) {
3156 case WLAN_CIPHER_SUITE_TKIP:
3157 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3158 /* get phase 1 key from mac80211 */
3159 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3160 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3161 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3162 seq.tkip.iv32, p1k, 0, key_offset,
3165 case WLAN_CIPHER_SUITE_CCMP:
3166 case WLAN_CIPHER_SUITE_WEP40:
3167 case WLAN_CIPHER_SUITE_WEP104:
3168 case WLAN_CIPHER_SUITE_GCMP:
3169 case WLAN_CIPHER_SUITE_GCMP_256:
3170 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3171 0, NULL, 0, key_offset, mfp);
3174 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3175 0, NULL, 0, key_offset, mfp);
3181 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
3182 struct ieee80211_key_conf *keyconf,
3186 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3187 struct iwl_mvm_add_sta_key_cmd cmd;
3189 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3190 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3195 /* This is a valid situation for GTK removal */
3196 if (sta_id == IWL_MVM_INVALID_STA)
3199 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3200 STA_KEY_FLG_KEYID_MSK);
3201 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3202 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3205 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3208 * The fields assigned here are in the same location at the start
3209 * of the command, so we can do this union trick.
3211 u.cmd.common.key_flags = key_flags;
3212 u.cmd.common.key_offset = keyconf->hw_key_idx;
3213 u.cmd.common.sta_id = sta_id;
3215 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
3217 status = ADD_STA_SUCCESS;
3218 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3222 case ADD_STA_SUCCESS:
3223 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3227 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3234 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3235 struct ieee80211_vif *vif,
3236 struct ieee80211_sta *sta,
3237 struct ieee80211_key_conf *keyconf,
3240 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3241 struct iwl_mvm_sta *mvm_sta;
3242 u8 sta_id = IWL_MVM_INVALID_STA;
3244 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3246 lockdep_assert_held(&mvm->mutex);
3248 if (vif->type != NL80211_IFTYPE_AP ||
3249 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3250 /* Get the station id from the mvm local station table */
3251 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3253 IWL_ERR(mvm, "Failed to find station\n");
3256 sta_id = mvm_sta->sta_id;
3259 * It is possible that the 'sta' parameter is NULL, and thus
3260 * there is a need to retrieve the sta from the local station
3264 sta = rcu_dereference_protected(
3265 mvm->fw_id_to_mac_id[sta_id],
3266 lockdep_is_held(&mvm->mutex));
3267 if (IS_ERR_OR_NULL(sta)) {
3268 IWL_ERR(mvm, "Invalid station id\n");
3273 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3276 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3278 sta_id = mvmvif->mcast_sta.sta_id;
3281 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3282 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3283 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3284 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3288 /* If the key_offset is not pre-assigned, we need to find a
3289 * new offset to use. In normal cases, the offset is not
3290 * pre-assigned, but during HW_RESTART we want to reuse the
3291 * same indices, so we pass them when this function is called.
3293 * In D3 entry, we need to hardcoded the indices (because the
3294 * firmware hardcodes the PTK offset to 0). In this case, we
3295 * need to make sure we don't overwrite the hw_key_idx in the
3296 * keyconf structure, because otherwise we cannot configure
3297 * the original ones back when resuming.
3299 if (key_offset == STA_KEY_IDX_INVALID) {
3300 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3301 if (key_offset == STA_KEY_IDX_INVALID)
3303 keyconf->hw_key_idx = key_offset;
3306 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3311 * For WEP, the same key is used for multicast and unicast. Upload it
3312 * again, using the same key offset, and now pointing the other one
3313 * to the same key slot (offset).
3314 * If this fails, remove the original as well.
3316 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3317 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3319 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3320 key_offset, !mcast);
3322 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3327 __set_bit(key_offset, mvm->fw_key_table);
3330 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3331 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3332 sta ? sta->addr : zero_addr, ret);
3336 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3337 struct ieee80211_vif *vif,
3338 struct ieee80211_sta *sta,
3339 struct ieee80211_key_conf *keyconf)
3341 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3342 struct iwl_mvm_sta *mvm_sta;
3343 u8 sta_id = IWL_MVM_INVALID_STA;
3346 lockdep_assert_held(&mvm->mutex);
3348 /* Get the station from the mvm local station table */
3349 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3351 sta_id = mvm_sta->sta_id;
3352 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3353 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3356 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3357 keyconf->keyidx, sta_id);
3359 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3360 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3361 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3362 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3364 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3365 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3366 keyconf->hw_key_idx);
3370 /* track which key was deleted last */
3371 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3372 if (mvm->fw_key_deleted[i] < U8_MAX)
3373 mvm->fw_key_deleted[i]++;
3375 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3377 if (sta && !mvm_sta) {
3378 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3382 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3386 /* delete WEP key twice to get rid of (now useless) offset */
3387 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3388 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3389 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3394 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3395 struct ieee80211_vif *vif,
3396 struct ieee80211_key_conf *keyconf,
3397 struct ieee80211_sta *sta, u32 iv32,
3400 struct iwl_mvm_sta *mvm_sta;
3401 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3402 bool mfp = sta ? sta->mfp : false;
3406 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3407 if (WARN_ON_ONCE(!mvm_sta))
3409 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3410 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3417 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3418 struct ieee80211_sta *sta)
3420 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3421 struct iwl_mvm_add_sta_cmd cmd = {
3422 .add_modify = STA_MODE_MODIFY,
3423 .sta_id = mvmsta->sta_id,
3424 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3425 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3429 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3430 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3432 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3435 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3436 struct ieee80211_sta *sta,
3437 enum ieee80211_frame_release_type reason,
3438 u16 cnt, u16 tids, bool more_data,
3439 bool single_sta_queue)
3441 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3442 struct iwl_mvm_add_sta_cmd cmd = {
3443 .add_modify = STA_MODE_MODIFY,
3444 .sta_id = mvmsta->sta_id,
3445 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3446 .sleep_tx_count = cpu_to_le16(cnt),
3447 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3450 unsigned long _tids = tids;
3452 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3453 * Note that this field is reserved and unused by firmware not
3454 * supporting GO uAPSD, so it's safe to always do this.
3456 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3457 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3459 /* If we're releasing frames from aggregation or dqa queues then check
3460 * if all the queues that we're releasing frames from, combined, have:
3461 * - more frames than the service period, in which case more_data
3463 * - fewer than 'cnt' frames, in which case we need to adjust the
3464 * firmware command (but do that unconditionally)
3466 if (single_sta_queue) {
3467 int remaining = cnt;
3470 spin_lock_bh(&mvmsta->lock);
3471 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3472 struct iwl_mvm_tid_data *tid_data;
3475 tid_data = &mvmsta->tid_data[tid];
3477 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3478 if (n_queued > remaining) {
3483 remaining -= n_queued;
3485 sleep_tx_count = cnt - remaining;
3486 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3487 mvmsta->sleep_tx_count = sleep_tx_count;
3488 spin_unlock_bh(&mvmsta->lock);
3490 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3491 if (WARN_ON(cnt - remaining == 0)) {
3492 ieee80211_sta_eosp(sta);
3497 /* Note: this is ignored by firmware not supporting GO uAPSD */
3499 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3501 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3502 mvmsta->next_status_eosp = true;
3503 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3505 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3508 /* block the Tx queues until the FW updated the sleep Tx count */
3509 iwl_trans_block_txq_ptrs(mvm->trans, true);
3511 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3512 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3513 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3515 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3518 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3519 struct iwl_rx_cmd_buffer *rxb)
3521 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3522 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3523 struct ieee80211_sta *sta;
3524 u32 sta_id = le32_to_cpu(notif->sta_id);
3526 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3530 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3531 if (!IS_ERR_OR_NULL(sta))
3532 ieee80211_sta_eosp(sta);
3536 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3537 struct iwl_mvm_sta *mvmsta, bool disable)
3539 struct iwl_mvm_add_sta_cmd cmd = {
3540 .add_modify = STA_MODE_MODIFY,
3541 .sta_id = mvmsta->sta_id,
3542 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3543 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3544 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3548 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3549 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3551 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3554 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3555 struct ieee80211_sta *sta,
3558 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3560 spin_lock_bh(&mvm_sta->lock);
3562 if (mvm_sta->disable_tx == disable) {
3563 spin_unlock_bh(&mvm_sta->lock);
3567 mvm_sta->disable_tx = disable;
3569 /* Tell mac80211 to start/stop queuing tx for this station */
3570 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3572 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3574 spin_unlock_bh(&mvm_sta->lock);
3577 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3578 struct iwl_mvm_vif *mvmvif,
3579 struct iwl_mvm_int_sta *sta,
3582 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3583 struct iwl_mvm_add_sta_cmd cmd = {
3584 .add_modify = STA_MODE_MODIFY,
3585 .sta_id = sta->sta_id,
3586 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3587 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3588 .mac_id_n_color = cpu_to_le32(id),
3592 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3593 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3595 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3598 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3599 struct iwl_mvm_vif *mvmvif,
3602 struct ieee80211_sta *sta;
3603 struct iwl_mvm_sta *mvm_sta;
3606 lockdep_assert_held(&mvm->mutex);
3608 /* Block/unblock all the stations of the given mvmvif */
3609 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3610 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3611 lockdep_is_held(&mvm->mutex));
3612 if (IS_ERR_OR_NULL(sta))
3615 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3616 if (mvm_sta->mac_id_n_color !=
3617 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3620 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3623 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3626 /* Need to block/unblock also multicast station */
3627 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3628 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3629 &mvmvif->mcast_sta, disable);
3632 * Only unblock the broadcast station (FW blocks it for immediate
3633 * quiet, not the driver)
3635 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3636 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3637 &mvmvif->bcast_sta, disable);
3640 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3642 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3643 struct iwl_mvm_sta *mvmsta;
3647 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3649 if (!WARN_ON(!mvmsta))
3650 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3655 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3657 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3660 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3661 * to align the wrap around of ssn so we compare relevant values.
3663 if (mvm->trans->cfg->gen2)
3666 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);