1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11 * Copyright(c) 2018 - 2019 Intel Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of version 2 of the GNU General Public License as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
25 * Contact Information:
26 * Intel Linux Wireless <linuxwifi@intel.com>
27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
32 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34 * Copyright(c) 2018 - 2019 Intel Corporation
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
47 * * Neither the name Intel Corporation nor the names of its
48 * contributors may be used to endorse or promote products derived
49 * from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *****************************************************************************/
64 #include <net/mac80211.h>
70 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm);
72 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
74 struct ieee80211_key_conf *key, bool mcast,
75 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
76 u8 key_offset, bool mfp);
79 * New version of ADD_STA_sta command added new fields at the end of the
80 * structure, so sending the size of the relevant API's structure is enough to
81 * support both API versions.
83 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
85 if (iwl_mvm_has_new_rx_api(mvm) ||
86 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
87 return sizeof(struct iwl_mvm_add_sta_cmd);
89 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
92 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
93 enum nl80211_iftype iftype)
98 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
99 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
101 lockdep_assert_held(&mvm->mutex);
103 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
104 if (iftype != NL80211_IFTYPE_STATION)
105 reserved_ids = BIT(0);
107 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
108 for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
109 if (BIT(sta_id) & reserved_ids)
112 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
113 lockdep_is_held(&mvm->mutex)))
116 return IWL_MVM_INVALID_STA;
119 /* send station add/update command to firmware */
120 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
121 bool update, unsigned int flags)
123 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
124 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
125 .sta_id = mvm_sta->sta_id,
126 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
127 .add_modify = update ? 1 : 0,
128 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
129 STA_FLG_MIMO_EN_MSK |
130 STA_FLG_RTS_MIMO_PROT),
131 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
135 u32 agg_size = 0, mpdu_dens = 0;
137 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
138 add_sta_cmd.station_type = mvm_sta->sta_type;
140 if (!update || (flags & STA_MODIFY_QUEUES)) {
141 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
143 if (!iwl_mvm_has_new_tx_api(mvm)) {
144 add_sta_cmd.tfd_queue_msk =
145 cpu_to_le32(mvm_sta->tfd_queue_msk);
147 if (flags & STA_MODIFY_QUEUES)
148 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
150 WARN_ON(flags & STA_MODIFY_QUEUES);
154 switch (sta->bandwidth) {
155 case IEEE80211_STA_RX_BW_160:
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
158 case IEEE80211_STA_RX_BW_80:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
161 case IEEE80211_STA_RX_BW_40:
162 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
164 case IEEE80211_STA_RX_BW_20:
165 if (sta->ht_cap.ht_supported)
166 add_sta_cmd.station_flags |=
167 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
171 switch (sta->rx_nss) {
173 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
176 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
179 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
183 switch (sta->smps_mode) {
184 case IEEE80211_SMPS_AUTOMATIC:
185 case IEEE80211_SMPS_NUM_MODES:
188 case IEEE80211_SMPS_STATIC:
190 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
191 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
193 case IEEE80211_SMPS_DYNAMIC:
194 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
196 case IEEE80211_SMPS_OFF:
201 if (sta->ht_cap.ht_supported) {
202 add_sta_cmd.station_flags_msk |=
203 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
204 STA_FLG_AGG_MPDU_DENS_MSK);
206 mpdu_dens = sta->ht_cap.ampdu_density;
209 if (sta->vht_cap.vht_supported) {
210 agg_size = sta->vht_cap.cap &
211 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
213 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
214 } else if (sta->ht_cap.ht_supported) {
215 agg_size = sta->ht_cap.ampdu_factor;
218 add_sta_cmd.station_flags |=
219 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
220 add_sta_cmd.station_flags |=
221 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
222 if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
223 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
226 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
228 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
229 add_sta_cmd.uapsd_acs |= BIT(AC_BK);
230 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
231 add_sta_cmd.uapsd_acs |= BIT(AC_BE);
232 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
233 add_sta_cmd.uapsd_acs |= BIT(AC_VI);
234 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
235 add_sta_cmd.uapsd_acs |= BIT(AC_VO);
236 add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
237 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
240 status = ADD_STA_SUCCESS;
241 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
242 iwl_mvm_add_sta_cmd_size(mvm),
243 &add_sta_cmd, &status);
247 switch (status & IWL_ADD_STA_STATUS_MASK) {
248 case ADD_STA_SUCCESS:
249 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
253 IWL_ERR(mvm, "ADD_STA failed\n");
260 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
262 struct iwl_mvm_baid_data *data =
263 from_timer(data, t, session_timer);
264 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
265 struct iwl_mvm_baid_data *ba_data;
266 struct ieee80211_sta *sta;
267 struct iwl_mvm_sta *mvm_sta;
268 unsigned long timeout;
272 ba_data = rcu_dereference(*rcu_ptr);
274 if (WARN_ON(!ba_data))
277 if (!ba_data->timeout)
280 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
281 if (time_is_after_jiffies(timeout)) {
282 mod_timer(&ba_data->session_timer, timeout);
287 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
290 * sta should be valid unless the following happens:
291 * The firmware asserts which triggers a reconfig flow, but
292 * the reconfig fails before we set the pointer to sta into
293 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
294 * A-MDPU and hence the timer continues to run. Then, the
295 * timer expires and sta is NULL.
300 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
301 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
302 sta->addr, ba_data->tid);
307 /* Disable aggregations for a bitmap of TIDs for a given station */
308 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
309 unsigned long disable_agg_tids,
312 struct iwl_mvm_add_sta_cmd cmd = {};
313 struct ieee80211_sta *sta;
314 struct iwl_mvm_sta *mvmsta;
318 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
321 sta_id = mvm->queue_info[queue].ra_sta_id;
325 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
327 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
332 mvmsta = iwl_mvm_sta_from_mac80211(sta);
334 mvmsta->tid_disable_agg |= disable_agg_tids;
336 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
337 cmd.sta_id = mvmsta->sta_id;
338 cmd.add_modify = STA_MODE_MODIFY;
339 cmd.modify_mask = STA_MODIFY_QUEUES;
340 if (disable_agg_tids)
341 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
343 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
344 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
345 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
349 /* Notify FW of queue removal from the STA queues */
350 status = ADD_STA_SUCCESS;
351 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
352 iwl_mvm_add_sta_cmd_size(mvm),
356 static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
357 int queue, u8 tid, u8 flags)
359 struct iwl_scd_txq_cfg_cmd cmd = {
361 .action = SCD_CFG_DISABLE_QUEUE,
365 if (iwl_mvm_has_new_tx_api(mvm)) {
366 iwl_trans_txq_free(mvm->trans, queue);
371 if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
374 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
376 cmd.action = mvm->queue_info[queue].tid_bitmap ?
377 SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
378 if (cmd.action == SCD_CFG_DISABLE_QUEUE)
379 mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
381 IWL_DEBUG_TX_QUEUES(mvm,
382 "Disabling TXQ #%d tids=0x%x\n",
384 mvm->queue_info[queue].tid_bitmap);
386 /* If the queue is still enabled - nothing left to do in this func */
387 if (cmd.action == SCD_CFG_ENABLE_QUEUE)
390 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
391 cmd.tid = mvm->queue_info[queue].txq_tid;
393 /* Make sure queue info is correct even though we overwrite it */
394 WARN(mvm->queue_info[queue].tid_bitmap,
395 "TXQ #%d info out-of-sync - tids=0x%x\n",
396 queue, mvm->queue_info[queue].tid_bitmap);
398 /* If we are here - the queue is freed and we can zero out these vals */
399 mvm->queue_info[queue].tid_bitmap = 0;
402 struct iwl_mvm_txq *mvmtxq =
403 iwl_mvm_txq_from_tid(sta, tid);
405 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
408 /* Regardless if this is a reserved TXQ for a STA - mark it as false */
409 mvm->queue_info[queue].reserved = false;
411 iwl_trans_txq_disable(mvm->trans, queue, false);
412 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
413 sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
416 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
421 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
423 struct ieee80211_sta *sta;
424 struct iwl_mvm_sta *mvmsta;
425 unsigned long tid_bitmap;
426 unsigned long agg_tids = 0;
430 lockdep_assert_held(&mvm->mutex);
432 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
435 sta_id = mvm->queue_info[queue].ra_sta_id;
436 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
438 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
439 lockdep_is_held(&mvm->mutex));
441 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
444 mvmsta = iwl_mvm_sta_from_mac80211(sta);
446 spin_lock_bh(&mvmsta->lock);
447 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
448 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
449 agg_tids |= BIT(tid);
451 spin_unlock_bh(&mvmsta->lock);
457 * Remove a queue from a station's resources.
458 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
459 * doesn't disable the queue
461 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
463 struct ieee80211_sta *sta;
464 struct iwl_mvm_sta *mvmsta;
465 unsigned long tid_bitmap;
466 unsigned long disable_agg_tids = 0;
470 lockdep_assert_held(&mvm->mutex);
472 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
475 sta_id = mvm->queue_info[queue].ra_sta_id;
476 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
480 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
482 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
487 mvmsta = iwl_mvm_sta_from_mac80211(sta);
489 spin_lock_bh(&mvmsta->lock);
490 /* Unmap MAC queues and TIDs from this queue */
491 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
492 struct iwl_mvm_txq *mvmtxq =
493 iwl_mvm_txq_from_tid(sta, tid);
495 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
496 disable_agg_tids |= BIT(tid);
497 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
499 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
502 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
503 spin_unlock_bh(&mvmsta->lock);
508 * The TX path may have been using this TXQ_ID from the tid_data,
509 * so make sure it's no longer running so that we can safely reuse
510 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
511 * above, but nothing guarantees we've stopped using them. Thus,
512 * without this, we could get to iwl_mvm_disable_txq() and remove
513 * the queue while still sending frames to it.
517 return disable_agg_tids;
520 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
521 struct ieee80211_sta *old_sta,
524 struct iwl_mvm_sta *mvmsta;
526 unsigned long disable_agg_tids = 0;
530 lockdep_assert_held(&mvm->mutex);
532 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
535 sta_id = mvm->queue_info[queue].ra_sta_id;
536 tid = mvm->queue_info[queue].txq_tid;
538 same_sta = sta_id == new_sta_id;
540 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
541 if (WARN_ON(!mvmsta))
544 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
545 /* Disable the queue */
546 if (disable_agg_tids)
547 iwl_mvm_invalidate_sta_queue(mvm, queue,
548 disable_agg_tids, false);
550 ret = iwl_mvm_disable_txq(mvm, old_sta, queue, tid, 0);
553 "Failed to free inactive queue %d (ret=%d)\n",
559 /* If TXQ is allocated to another STA, update removal in FW */
561 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
566 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
567 unsigned long tfd_queue_mask, u8 ac)
570 u8 ac_to_queue[IEEE80211_NUM_ACS];
574 * This protects us against grabbing a queue that's being reconfigured
575 * by the inactivity checker.
577 lockdep_assert_held(&mvm->mutex);
579 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
582 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
584 /* See what ACs the existing queues for this STA have */
585 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
586 /* Only DATA queues can be shared */
587 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
588 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
591 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
595 * The queue to share is chosen only from DATA queues as follows (in
596 * descending priority):
599 * 3. Highest AC queue that is lower than new AC
600 * 4. Any existing AC (there always is at least 1 DATA queue)
603 /* Priority 1: An AC_BE queue */
604 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
605 queue = ac_to_queue[IEEE80211_AC_BE];
606 /* Priority 2: Same AC queue */
607 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
608 queue = ac_to_queue[ac];
609 /* Priority 3a: If new AC is VO and VI exists - use VI */
610 else if (ac == IEEE80211_AC_VO &&
611 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
612 queue = ac_to_queue[IEEE80211_AC_VI];
613 /* Priority 3b: No BE so only AC less than the new one is BK */
614 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
615 queue = ac_to_queue[IEEE80211_AC_BK];
616 /* Priority 4a: No BE nor BK - use VI if exists */
617 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
618 queue = ac_to_queue[IEEE80211_AC_VI];
619 /* Priority 4b: No BE, BK nor VI - use VO if exists */
620 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
621 queue = ac_to_queue[IEEE80211_AC_VO];
623 /* Make sure queue found (or not) is legal */
624 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
625 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
626 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
627 IWL_ERR(mvm, "No DATA queues available to share\n");
635 * If a given queue has a higher AC than the TID stream that is being compared
636 * to, the queue needs to be redirected to the lower AC. This function does that
637 * in such a case, otherwise - if no redirection required - it does nothing,
638 * unless the %force param is true.
640 static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
641 int ac, int ssn, unsigned int wdg_timeout,
642 bool force, struct iwl_mvm_txq *txq)
644 struct iwl_scd_txq_cfg_cmd cmd = {
646 .action = SCD_CFG_DISABLE_QUEUE,
651 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
655 * If the AC is lower than current one - FIFO needs to be redirected to
656 * the lowest one of the streams in the queue. Check if this is needed
658 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
659 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
660 * we need to check if the numerical value of X is LARGER than of Y.
662 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
663 IWL_DEBUG_TX_QUEUES(mvm,
664 "No redirection needed on TXQ #%d\n",
669 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
670 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
671 cmd.tid = mvm->queue_info[queue].txq_tid;
672 shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
674 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
675 queue, iwl_mvm_ac_to_tx_fifo[ac]);
677 /* Stop the queue and wait for it to empty */
680 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
682 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
688 /* Before redirecting the queue we need to de-activate it */
689 iwl_trans_txq_disable(mvm->trans, queue, false);
690 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
692 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
695 /* Make sure the SCD wrptr is correctly set before reconfiguring */
696 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
698 /* Update the TID "owner" of the queue */
699 mvm->queue_info[queue].txq_tid = tid;
701 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
703 /* Redirect to lower AC */
704 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
705 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
707 /* Update AC marking of the queue */
708 mvm->queue_info[queue].mac80211_ac = ac;
711 * Mark queue as shared in transport if shared
712 * Note this has to be done after queue enablement because enablement
713 * can also set this value, and there is no indication there to shared
717 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
720 /* Continue using the queue */
721 txq->stopped = false;
726 static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
731 lockdep_assert_held(&mvm->mutex);
733 /* This should not be hit with new TX path */
734 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
737 /* Start by looking for a free queue */
738 for (i = minq; i <= maxq; i++)
739 if (mvm->queue_info[i].tid_bitmap == 0 &&
740 mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
746 static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
747 u8 sta_id, u8 tid, unsigned int timeout)
749 int queue, size = IWL_DEFAULT_QUEUE_SIZE;
751 if (tid == IWL_MAX_TID_COUNT) {
753 size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
754 mvm->trans->cfg->min_txq_size);
756 queue = iwl_trans_txq_alloc(mvm->trans,
757 cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
758 sta_id, tid, SCD_QUEUE_CFG, size, timeout);
761 IWL_DEBUG_TX_QUEUES(mvm,
762 "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
767 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
770 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d\n", queue);
775 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
776 struct ieee80211_sta *sta, u8 ac,
779 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
780 struct iwl_mvm_txq *mvmtxq =
781 iwl_mvm_txq_from_tid(sta, tid);
782 unsigned int wdg_timeout =
783 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
786 lockdep_assert_held(&mvm->mutex);
788 IWL_DEBUG_TX_QUEUES(mvm,
789 "Allocating queue for sta %d on tid %d\n",
790 mvmsta->sta_id, tid);
791 queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
795 mvmtxq->txq_id = queue;
796 mvm->tvqm_info[queue].txq_tid = tid;
797 mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
799 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
801 spin_lock_bh(&mvmsta->lock);
802 mvmsta->tid_data[tid].txq_id = queue;
803 spin_unlock_bh(&mvmsta->lock);
808 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
809 struct ieee80211_sta *sta,
810 int queue, u8 sta_id, u8 tid)
812 bool enable_queue = true;
814 /* Make sure this TID isn't already enabled */
815 if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
816 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
821 /* Update mappings and refcounts */
822 if (mvm->queue_info[queue].tid_bitmap)
823 enable_queue = false;
825 mvm->queue_info[queue].tid_bitmap |= BIT(tid);
826 mvm->queue_info[queue].ra_sta_id = sta_id;
829 if (tid != IWL_MAX_TID_COUNT)
830 mvm->queue_info[queue].mac80211_ac =
831 tid_to_mac80211_ac[tid];
833 mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
835 mvm->queue_info[queue].txq_tid = tid;
839 struct iwl_mvm_txq *mvmtxq =
840 iwl_mvm_txq_from_tid(sta, tid);
842 mvmtxq->txq_id = queue;
845 IWL_DEBUG_TX_QUEUES(mvm,
846 "Enabling TXQ #%d tids=0x%x\n",
847 queue, mvm->queue_info[queue].tid_bitmap);
852 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
854 const struct iwl_trans_txq_scd_cfg *cfg,
855 unsigned int wdg_timeout)
857 struct iwl_scd_txq_cfg_cmd cmd = {
859 .action = SCD_CFG_ENABLE_QUEUE,
860 .window = cfg->frame_limit,
861 .sta_id = cfg->sta_id,
862 .ssn = cpu_to_le16(ssn),
863 .tx_fifo = cfg->fifo,
864 .aggregate = cfg->aggregate,
869 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
872 /* Send the enabling command if we need to */
873 if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
876 inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
879 le16_add_cpu(&cmd.ssn, 1);
881 WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
882 "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
887 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
889 struct iwl_scd_txq_cfg_cmd cmd = {
891 .action = SCD_CFG_UPDATE_QUEUE_TID,
894 unsigned long tid_bitmap;
897 lockdep_assert_held(&mvm->mutex);
899 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
902 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
904 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
907 /* Find any TID for queue */
908 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
910 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
912 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
914 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
919 mvm->queue_info[queue].txq_tid = tid;
920 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
924 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
926 struct ieee80211_sta *sta;
927 struct iwl_mvm_sta *mvmsta;
930 unsigned long tid_bitmap;
931 unsigned int wdg_timeout;
935 /* queue sharing is disabled on new TX path */
936 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
939 lockdep_assert_held(&mvm->mutex);
941 sta_id = mvm->queue_info[queue].ra_sta_id;
942 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
944 /* Find TID for queue, and make sure it is the only one on the queue */
945 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
946 if (tid_bitmap != BIT(tid)) {
947 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
952 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
955 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
956 lockdep_is_held(&mvm->mutex));
958 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
961 mvmsta = iwl_mvm_sta_from_mac80211(sta);
962 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
964 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
966 ret = iwl_mvm_redirect_queue(mvm, queue, tid,
967 tid_to_mac80211_ac[tid], ssn,
969 iwl_mvm_txq_from_tid(sta, tid));
971 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
975 /* If aggs should be turned back on - do it */
976 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
977 struct iwl_mvm_add_sta_cmd cmd = {0};
979 mvmsta->tid_disable_agg &= ~BIT(tid);
981 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
982 cmd.sta_id = mvmsta->sta_id;
983 cmd.add_modify = STA_MODE_MODIFY;
984 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
985 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
986 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
988 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
989 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
991 IWL_DEBUG_TX_QUEUES(mvm,
992 "TXQ #%d is now aggregated again\n",
995 /* Mark queue intenally as aggregating again */
996 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1000 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1004 * Remove inactive TIDs of a given queue.
1005 * If all queue TIDs are inactive - mark the queue as inactive
1006 * If only some the queue TIDs are inactive - unmap them from the queue
1008 * Returns %true if all TIDs were removed and the queue could be reused.
1010 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1011 struct iwl_mvm_sta *mvmsta, int queue,
1012 unsigned long tid_bitmap,
1013 unsigned long *unshare_queues,
1014 unsigned long *changetid_queues)
1018 lockdep_assert_held(&mvmsta->lock);
1019 lockdep_assert_held(&mvm->mutex);
1021 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1024 /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1025 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1026 /* If some TFDs are still queued - don't mark TID as inactive */
1027 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1028 tid_bitmap &= ~BIT(tid);
1030 /* Don't mark as inactive any TID that has an active BA */
1031 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1032 tid_bitmap &= ~BIT(tid);
1035 /* If all TIDs in the queue are inactive - return it can be reused */
1036 if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1037 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1042 * If we are here, this is a shared queue and not all TIDs timed-out.
1043 * Remove the ones that did.
1045 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1048 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1049 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1051 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1054 * We need to take into account a situation in which a TXQ was
1055 * allocated to TID x, and then turned shared by adding TIDs y
1056 * and z. If TID x becomes inactive and is removed from the TXQ,
1057 * ownership must be given to one of the remaining TIDs.
1058 * This is mainly because if TID x continues - a new queue can't
1059 * be allocated for it as long as it is an owner of another TXQ.
1061 * Mark this queue in the right bitmap, we'll send the command
1062 * to the firmware later.
1064 if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1065 set_bit(queue, changetid_queues);
1067 IWL_DEBUG_TX_QUEUES(mvm,
1068 "Removing inactive TID %d from shared Q:%d\n",
1072 IWL_DEBUG_TX_QUEUES(mvm,
1073 "TXQ #%d left with tid bitmap 0x%x\n", queue,
1074 mvm->queue_info[queue].tid_bitmap);
1077 * There may be different TIDs with the same mac queues, so make
1078 * sure all TIDs have existing corresponding mac queues enabled
1080 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1082 /* If the queue is marked as shared - "unshare" it */
1083 if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1084 mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1085 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1087 set_bit(queue, unshare_queues);
1094 * Check for inactivity - this includes checking if any queue
1095 * can be unshared and finding one (and only one) that can be
1097 * This function is also invoked as a sort of clean-up task,
1098 * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1100 * Returns the queue number, or -ENOSPC.
1102 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1104 unsigned long now = jiffies;
1105 unsigned long unshare_queues = 0;
1106 unsigned long changetid_queues = 0;
1107 int i, ret, free_queue = -ENOSPC;
1108 struct ieee80211_sta *queue_owner = NULL;
1110 lockdep_assert_held(&mvm->mutex);
1112 if (iwl_mvm_has_new_tx_api(mvm))
1117 /* we skip the CMD queue below by starting at 1 */
1118 BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1120 for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1121 struct ieee80211_sta *sta;
1122 struct iwl_mvm_sta *mvmsta;
1125 unsigned long inactive_tid_bitmap = 0;
1126 unsigned long queue_tid_bitmap;
1128 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1129 if (!queue_tid_bitmap)
1132 /* If TXQ isn't in active use anyway - nothing to do here... */
1133 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1134 mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1137 /* Check to see if there are inactive TIDs on this queue */
1138 for_each_set_bit(tid, &queue_tid_bitmap,
1139 IWL_MAX_TID_COUNT + 1) {
1140 if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1141 IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1144 inactive_tid_bitmap |= BIT(tid);
1147 /* If all TIDs are active - finish check on this queue */
1148 if (!inactive_tid_bitmap)
1152 * If we are here - the queue hadn't been served recently and is
1156 sta_id = mvm->queue_info[i].ra_sta_id;
1157 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1160 * If the STA doesn't exist anymore, it isn't an error. It could
1161 * be that it was removed since getting the queues, and in this
1162 * case it should've inactivated its queues anyway.
1164 if (IS_ERR_OR_NULL(sta))
1167 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1169 spin_lock_bh(&mvmsta->lock);
1170 ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1171 inactive_tid_bitmap,
1174 if (ret >= 0 && free_queue < 0) {
1178 /* only unlock sta lock - we still need the queue info lock */
1179 spin_unlock_bh(&mvmsta->lock);
1183 /* Reconfigure queues requiring reconfiguation */
1184 for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1185 iwl_mvm_unshare_queue(mvm, i);
1186 for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1187 iwl_mvm_change_queue_tid(mvm, i);
1189 if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1190 ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1203 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1204 struct ieee80211_sta *sta, u8 ac, int tid)
1206 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1207 struct iwl_trans_txq_scd_cfg cfg = {
1208 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1209 .sta_id = mvmsta->sta_id,
1211 .frame_limit = IWL_FRAME_LIMIT,
1213 unsigned int wdg_timeout =
1214 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1216 unsigned long disable_agg_tids = 0;
1217 enum iwl_mvm_agg_state queue_state;
1218 bool shared_queue = false, inc_ssn;
1220 unsigned long tfd_queue_mask;
1223 lockdep_assert_held(&mvm->mutex);
1225 if (iwl_mvm_has_new_tx_api(mvm))
1226 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1228 spin_lock_bh(&mvmsta->lock);
1229 tfd_queue_mask = mvmsta->tfd_queue_msk;
1230 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1231 spin_unlock_bh(&mvmsta->lock);
1233 if (tid == IWL_MAX_TID_COUNT) {
1234 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1235 IWL_MVM_DQA_MIN_MGMT_QUEUE,
1236 IWL_MVM_DQA_MAX_MGMT_QUEUE);
1237 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1238 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1241 /* If no such queue is found, we'll use a DATA queue instead */
1244 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1245 (mvm->queue_info[mvmsta->reserved_queue].status ==
1246 IWL_MVM_QUEUE_RESERVED)) {
1247 queue = mvmsta->reserved_queue;
1248 mvm->queue_info[queue].reserved = true;
1249 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1253 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1254 IWL_MVM_DQA_MIN_DATA_QUEUE,
1255 IWL_MVM_DQA_MAX_DATA_QUEUE);
1257 /* try harder - perhaps kill an inactive queue */
1258 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1261 /* No free queue - we'll have to share */
1263 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1265 shared_queue = true;
1266 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1271 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1272 * to make sure no one else takes it.
1273 * This will allow avoiding re-acquiring the lock at the end of the
1274 * configuration. On error we'll mark it back as free.
1276 if (queue > 0 && !shared_queue)
1277 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1279 /* This shouldn't happen - out of queues */
1280 if (WARN_ON(queue <= 0)) {
1281 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1287 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1288 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1290 * Mark all DATA queues as allowing to be aggregated at some point
1292 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1293 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1295 IWL_DEBUG_TX_QUEUES(mvm,
1296 "Allocating %squeue #%d to sta %d on tid %d\n",
1297 shared_queue ? "shared " : "", queue,
1298 mvmsta->sta_id, tid);
1301 /* Disable any open aggs on this queue */
1302 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1304 if (disable_agg_tids) {
1305 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1307 iwl_mvm_invalidate_sta_queue(mvm, queue,
1308 disable_agg_tids, false);
1312 inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1315 * Mark queue as shared in transport if shared
1316 * Note this has to be done after queue enablement because enablement
1317 * can also set this value, and there is no indication there to shared
1321 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1323 spin_lock_bh(&mvmsta->lock);
1325 * This looks racy, but it is not. We have only one packet for
1326 * this ra/tid in our Tx path since we stop the Qdisc when we
1327 * need to allocate a new TFD queue.
1330 mvmsta->tid_data[tid].seq_number += 0x10;
1331 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1333 mvmsta->tid_data[tid].txq_id = queue;
1334 mvmsta->tfd_queue_msk |= BIT(queue);
1335 queue_state = mvmsta->tid_data[tid].state;
1337 if (mvmsta->reserved_queue == queue)
1338 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1339 spin_unlock_bh(&mvmsta->lock);
1341 if (!shared_queue) {
1342 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1346 /* If we need to re-enable aggregations... */
1347 if (queue_state == IWL_AGG_ON) {
1348 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1353 /* Redirect queue, if needed */
1354 ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1356 iwl_mvm_txq_from_tid(sta, tid));
1364 iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1369 static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1371 if (tid == IWL_MAX_TID_COUNT)
1372 return IEEE80211_AC_VO; /* MGMT */
1374 return tid_to_mac80211_ac[tid];
1377 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1379 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1382 mutex_lock(&mvm->mutex);
1384 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1386 while (!list_empty(&mvm->add_stream_txqs)) {
1387 struct iwl_mvm_txq *mvmtxq;
1388 struct ieee80211_txq *txq;
1391 mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1392 struct iwl_mvm_txq, list);
1394 txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1397 if (tid == IEEE80211_NUM_TIDS)
1398 tid = IWL_MAX_TID_COUNT;
1400 iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid);
1401 list_del_init(&mvmtxq->list);
1402 iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1405 mutex_unlock(&mvm->mutex);
1408 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1409 struct ieee80211_sta *sta,
1410 enum nl80211_iftype vif_type)
1412 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1415 /* queue reserving is disabled on new TX path */
1416 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1419 /* run the general cleanup/unsharing of queues */
1420 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1422 /* Make sure we have free resources for this STA */
1423 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1424 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1425 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1426 IWL_MVM_QUEUE_FREE))
1427 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1429 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1430 IWL_MVM_DQA_MIN_DATA_QUEUE,
1431 IWL_MVM_DQA_MAX_DATA_QUEUE);
1433 /* try again - this time kick out a queue if needed */
1434 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1436 IWL_ERR(mvm, "No available queues for new station\n");
1440 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1442 mvmsta->reserved_queue = queue;
1444 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1445 queue, mvmsta->sta_id);
1451 * In DQA mode, after a HW restart the queues should be allocated as before, in
1452 * order to avoid race conditions when there are shared queues. This function
1453 * does the re-mapping and queue allocation.
1455 * Note that re-enabling aggregations isn't done in this function.
1457 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1458 struct ieee80211_sta *sta)
1460 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1462 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1464 struct iwl_trans_txq_scd_cfg cfg = {
1465 .sta_id = mvm_sta->sta_id,
1466 .frame_limit = IWL_FRAME_LIMIT,
1469 /* Make sure reserved queue is still marked as such (if allocated) */
1470 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1471 mvm->queue_info[mvm_sta->reserved_queue].status =
1472 IWL_MVM_QUEUE_RESERVED;
1474 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1475 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1476 int txq_id = tid_data->txq_id;
1479 if (txq_id == IWL_MVM_INVALID_QUEUE)
1482 ac = tid_to_mac80211_ac[i];
1484 if (iwl_mvm_has_new_tx_api(mvm)) {
1485 IWL_DEBUG_TX_QUEUES(mvm,
1486 "Re-mapping sta %d tid %d\n",
1487 mvm_sta->sta_id, i);
1488 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1490 tid_data->txq_id = txq_id;
1493 * Since we don't set the seq number after reset, and HW
1494 * sets it now, FW reset will cause the seq num to start
1495 * at 0 again, so driver will need to update it
1496 * internally as well, so it keeps in sync with real val
1498 tid_data->seq_number = 0;
1500 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1503 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1504 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1506 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1508 IWL_DEBUG_TX_QUEUES(mvm,
1509 "Re-mapping sta %d tid %d to queue %d\n",
1510 mvm_sta->sta_id, i, txq_id);
1512 iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1513 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1518 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1519 struct iwl_mvm_int_sta *sta,
1521 u16 mac_id, u16 color)
1523 struct iwl_mvm_add_sta_cmd cmd;
1525 u32 status = ADD_STA_SUCCESS;
1527 lockdep_assert_held(&mvm->mutex);
1529 memset(&cmd, 0, sizeof(cmd));
1530 cmd.sta_id = sta->sta_id;
1531 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1533 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1534 cmd.station_type = sta->type;
1536 if (!iwl_mvm_has_new_tx_api(mvm))
1537 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1538 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1541 memcpy(cmd.addr, addr, ETH_ALEN);
1543 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1544 iwl_mvm_add_sta_cmd_size(mvm),
1549 switch (status & IWL_ADD_STA_STATUS_MASK) {
1550 case ADD_STA_SUCCESS:
1551 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1555 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1562 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1563 struct ieee80211_vif *vif,
1564 struct ieee80211_sta *sta)
1566 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1567 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1568 struct iwl_mvm_rxq_dup_data *dup_data;
1570 bool sta_update = false;
1571 unsigned int sta_flags = 0;
1573 lockdep_assert_held(&mvm->mutex);
1575 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1576 sta_id = iwl_mvm_find_free_sta_id(mvm,
1577 ieee80211_vif_type_p2p(vif));
1579 sta_id = mvm_sta->sta_id;
1581 if (sta_id == IWL_MVM_INVALID_STA)
1584 spin_lock_init(&mvm_sta->lock);
1586 /* if this is a HW restart re-alloc existing queues */
1587 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1588 struct iwl_mvm_int_sta tmp_sta = {
1590 .type = mvm_sta->sta_type,
1594 * First add an empty station since allocating
1595 * a queue requires a valid station
1597 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1598 mvmvif->id, mvmvif->color);
1602 iwl_mvm_realloc_queues_after_restart(mvm, sta);
1604 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1608 mvm_sta->sta_id = sta_id;
1609 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1612 if (!mvm->trans->cfg->gen2)
1613 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1615 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1616 mvm_sta->tx_protection = 0;
1617 mvm_sta->tt_tx_protection = false;
1618 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1620 /* HW restart, don't assume the memory has been zeroed */
1621 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1622 mvm_sta->tfd_queue_msk = 0;
1624 /* for HW restart - reset everything but the sequence number */
1625 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1626 u16 seq = mvm_sta->tid_data[i].seq_number;
1627 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1628 mvm_sta->tid_data[i].seq_number = seq;
1631 * Mark all queues for this STA as unallocated and defer TX
1632 * frames until the queue is allocated
1634 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1637 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1638 struct iwl_mvm_txq *mvmtxq =
1639 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1641 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1642 INIT_LIST_HEAD(&mvmtxq->list);
1643 atomic_set(&mvmtxq->tx_request, 0);
1646 mvm_sta->agg_tids = 0;
1648 if (iwl_mvm_has_new_rx_api(mvm) &&
1649 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1652 dup_data = kcalloc(mvm->trans->num_rx_queues,
1653 sizeof(*dup_data), GFP_KERNEL);
1657 * Initialize all the last_seq values to 0xffff which can never
1658 * compare equal to the frame's seq_ctrl in the check in
1659 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1660 * number and fragmented packets don't reach that function.
1662 * This thus allows receiving a packet with seqno 0 and the
1663 * retry bit set as the very first packet on a new TID.
1665 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1666 memset(dup_data[q].last_seq, 0xff,
1667 sizeof(dup_data[q].last_seq));
1668 mvm_sta->dup_data = dup_data;
1671 if (!iwl_mvm_has_new_tx_api(mvm)) {
1672 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1673 ieee80211_vif_type_p2p(vif));
1679 * if rs is registered with mac80211, then "add station" will be handled
1680 * via the corresponding ops, otherwise need to notify rate scaling here
1682 if (iwl_mvm_has_tlc_offload(mvm))
1683 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1685 iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1688 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1692 if (vif->type == NL80211_IFTYPE_STATION) {
1694 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1695 mvmvif->ap_sta_id = sta_id;
1697 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1701 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1709 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1712 struct iwl_mvm_add_sta_cmd cmd = {};
1716 lockdep_assert_held(&mvm->mutex);
1718 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1719 cmd.sta_id = mvmsta->sta_id;
1720 cmd.add_modify = STA_MODE_MODIFY;
1721 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1722 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1724 status = ADD_STA_SUCCESS;
1725 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1726 iwl_mvm_add_sta_cmd_size(mvm),
1731 switch (status & IWL_ADD_STA_STATUS_MASK) {
1732 case ADD_STA_SUCCESS:
1733 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1738 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1747 * Remove a station from the FW table. Before sending the command to remove
1748 * the station validate that the station is indeed known to the driver (sanity
1751 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1753 struct ieee80211_sta *sta;
1754 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1759 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1760 lockdep_is_held(&mvm->mutex));
1762 /* Note: internal stations are marked as error values */
1764 IWL_ERR(mvm, "Invalid station id\n");
1768 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1769 sizeof(rm_sta_cmd), &rm_sta_cmd);
1771 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1778 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1779 struct ieee80211_vif *vif,
1780 struct ieee80211_sta *sta)
1782 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1785 lockdep_assert_held(&mvm->mutex);
1787 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1788 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1791 iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1793 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1796 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1797 struct iwl_mvm_txq *mvmtxq =
1798 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1800 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1804 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1805 struct iwl_mvm_sta *mvm_sta)
1809 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1813 spin_lock_bh(&mvm_sta->lock);
1814 txq_id = mvm_sta->tid_data[i].txq_id;
1815 spin_unlock_bh(&mvm_sta->lock);
1817 if (txq_id == IWL_MVM_INVALID_QUEUE)
1820 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1828 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1829 struct ieee80211_vif *vif,
1830 struct ieee80211_sta *sta)
1832 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1833 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1834 u8 sta_id = mvm_sta->sta_id;
1837 lockdep_assert_held(&mvm->mutex);
1839 if (iwl_mvm_has_new_rx_api(mvm))
1840 kfree(mvm_sta->dup_data);
1842 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1846 /* flush its queues here since we are freeing mvm_sta */
1847 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1850 if (iwl_mvm_has_new_tx_api(mvm)) {
1851 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1853 u32 q_mask = mvm_sta->tfd_queue_msk;
1855 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1861 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1863 iwl_mvm_disable_sta_queues(mvm, vif, sta);
1865 /* If there is a TXQ still marked as reserved - free it */
1866 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1867 u8 reserved_txq = mvm_sta->reserved_queue;
1868 enum iwl_mvm_queue_status *status;
1871 * If no traffic has gone through the reserved TXQ - it
1872 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1873 * should be manually marked as free again
1875 status = &mvm->queue_info[reserved_txq].status;
1876 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1877 (*status != IWL_MVM_QUEUE_FREE),
1878 "sta_id %d reserved txq %d status %d",
1879 sta_id, reserved_txq, *status))
1882 *status = IWL_MVM_QUEUE_FREE;
1885 if (vif->type == NL80211_IFTYPE_STATION &&
1886 mvmvif->ap_sta_id == sta_id) {
1887 /* if associated - we can't remove the AP STA now */
1888 if (vif->bss_conf.assoc)
1891 /* unassoc - go ahead - remove the AP STA now */
1892 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1894 /* clear d0i3_ap_sta_id if no longer relevant */
1895 if (mvm->d0i3_ap_sta_id == sta_id)
1896 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1900 * This shouldn't happen - the TDLS channel switch should be canceled
1901 * before the STA is removed.
1903 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1904 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1905 cancel_delayed_work(&mvm->tdls_cs.dwork);
1909 * Make sure that the tx response code sees the station as -EBUSY and
1910 * calls the drain worker.
1912 spin_lock_bh(&mvm_sta->lock);
1913 spin_unlock_bh(&mvm_sta->lock);
1915 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1916 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1921 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1922 struct ieee80211_vif *vif,
1925 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1927 lockdep_assert_held(&mvm->mutex);
1929 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1933 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1934 struct iwl_mvm_int_sta *sta,
1935 u32 qmask, enum nl80211_iftype iftype,
1936 enum iwl_sta_type type)
1938 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1939 sta->sta_id == IWL_MVM_INVALID_STA) {
1940 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1941 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1945 sta->tfd_queue_msk = qmask;
1948 /* put a non-NULL value so iterating over the stations won't stop */
1949 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1953 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1955 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1956 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1957 sta->sta_id = IWL_MVM_INVALID_STA;
1960 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 *queue,
1963 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1964 mvm->cfg->base_params->wd_timeout :
1965 IWL_WATCHDOG_DISABLED;
1967 if (iwl_mvm_has_new_tx_api(mvm)) {
1969 iwl_mvm_tvqm_enable_txq(mvm, sta_id,
1972 *queue = tvqm_queue;
1974 struct iwl_trans_txq_scd_cfg cfg = {
1977 .tid = IWL_MAX_TID_COUNT,
1979 .frame_limit = IWL_FRAME_LIMIT,
1982 iwl_mvm_enable_txq(mvm, NULL, *queue, 0, &cfg, wdg_timeout);
1986 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1990 lockdep_assert_held(&mvm->mutex);
1992 /* Allocate aux station and assign to it the aux queue */
1993 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1994 NL80211_IFTYPE_UNSPECIFIED,
1995 IWL_STA_AUX_ACTIVITY);
1999 /* Map Aux queue to fifo - needs to happen before adding Aux station */
2000 if (!iwl_mvm_has_new_tx_api(mvm))
2001 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2002 mvm->aux_sta.sta_id,
2003 IWL_MVM_TX_FIFO_MCAST);
2005 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
2008 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2013 * For 22000 firmware and on we cannot add queue to a station unknown
2014 * to firmware so enable queue here - after the station was added
2016 if (iwl_mvm_has_new_tx_api(mvm))
2017 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2018 mvm->aux_sta.sta_id,
2019 IWL_MVM_TX_FIFO_MCAST);
2024 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2026 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2029 lockdep_assert_held(&mvm->mutex);
2031 /* Map snif queue to fifo - must happen before adding snif station */
2032 if (!iwl_mvm_has_new_tx_api(mvm))
2033 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2034 mvm->snif_sta.sta_id,
2035 IWL_MVM_TX_FIFO_BE);
2037 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
2043 * For 22000 firmware and on we cannot add queue to a station unknown
2044 * to firmware so enable queue here - after the station was added
2046 if (iwl_mvm_has_new_tx_api(mvm))
2047 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2048 mvm->snif_sta.sta_id,
2049 IWL_MVM_TX_FIFO_BE);
2054 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2058 lockdep_assert_held(&mvm->mutex);
2060 iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2061 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2063 IWL_WARN(mvm, "Failed sending remove station\n");
2068 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2070 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2073 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
2075 lockdep_assert_held(&mvm->mutex);
2077 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2081 * Send the add station command for the vif's broadcast station.
2082 * Assumes that the station was already allocated.
2084 * @mvm: the mvm component
2085 * @vif: the interface to which the broadcast station is added
2086 * @bsta: the broadcast station to add.
2088 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2090 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2091 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2092 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2093 const u8 *baddr = _baddr;
2096 unsigned int wdg_timeout =
2097 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2098 struct iwl_trans_txq_scd_cfg cfg = {
2099 .fifo = IWL_MVM_TX_FIFO_VO,
2100 .sta_id = mvmvif->bcast_sta.sta_id,
2101 .tid = IWL_MAX_TID_COUNT,
2103 .frame_limit = IWL_FRAME_LIMIT,
2106 lockdep_assert_held(&mvm->mutex);
2108 if (!iwl_mvm_has_new_tx_api(mvm)) {
2109 if (vif->type == NL80211_IFTYPE_AP ||
2110 vif->type == NL80211_IFTYPE_ADHOC)
2111 queue = mvm->probe_queue;
2112 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2113 queue = mvm->p2p_dev_queue;
2114 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
2117 bsta->tfd_queue_msk |= BIT(queue);
2119 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2122 if (vif->type == NL80211_IFTYPE_ADHOC)
2123 baddr = vif->bss_conf.bssid;
2125 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2128 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2129 mvmvif->id, mvmvif->color);
2134 * For 22000 firmware and on we cannot add queue to a station unknown
2135 * to firmware so enable queue here - after the station was added
2137 if (iwl_mvm_has_new_tx_api(mvm)) {
2138 queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2142 if (vif->type == NL80211_IFTYPE_AP ||
2143 vif->type == NL80211_IFTYPE_ADHOC)
2144 mvm->probe_queue = queue;
2145 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2146 mvm->p2p_dev_queue = queue;
2152 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2153 struct ieee80211_vif *vif)
2155 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2158 lockdep_assert_held(&mvm->mutex);
2160 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2162 switch (vif->type) {
2163 case NL80211_IFTYPE_AP:
2164 case NL80211_IFTYPE_ADHOC:
2165 queue = mvm->probe_queue;
2167 case NL80211_IFTYPE_P2P_DEVICE:
2168 queue = mvm->p2p_dev_queue;
2171 WARN(1, "Can't free bcast queue on vif type %d\n",
2176 iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2177 if (iwl_mvm_has_new_tx_api(mvm))
2180 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2181 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2184 /* Send the FW a request to remove the station from it's internal data
2185 * structures, but DO NOT remove the entry from the local data structures. */
2186 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2188 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2191 lockdep_assert_held(&mvm->mutex);
2193 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2195 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2197 IWL_WARN(mvm, "Failed sending remove station\n");
2201 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2203 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2205 lockdep_assert_held(&mvm->mutex);
2207 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2208 ieee80211_vif_type_p2p(vif),
2209 IWL_STA_GENERAL_PURPOSE);
2212 /* Allocate a new station entry for the broadcast station to the given vif,
2213 * and send it to the FW.
2214 * Note that each P2P mac should have its own broadcast station.
2216 * @mvm: the mvm component
2217 * @vif: the interface to which the broadcast station is added
2218 * @bsta: the broadcast station to add. */
2219 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2221 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2222 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2225 lockdep_assert_held(&mvm->mutex);
2227 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2231 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2234 iwl_mvm_dealloc_int_sta(mvm, bsta);
2239 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2241 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2243 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2247 * Send the FW a request to remove the station from it's internal data
2248 * structures, and in addition remove it from the local data structure.
2250 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2254 lockdep_assert_held(&mvm->mutex);
2256 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2258 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2264 * Allocate a new station entry for the multicast station to the given vif,
2265 * and send it to the FW.
2266 * Note that each AP/GO mac should have its own multicast station.
2268 * @mvm: the mvm component
2269 * @vif: the interface to which the multicast station is added
2271 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2273 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2274 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2275 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2276 const u8 *maddr = _maddr;
2277 struct iwl_trans_txq_scd_cfg cfg = {
2278 .fifo = IWL_MVM_TX_FIFO_MCAST,
2279 .sta_id = msta->sta_id,
2282 .frame_limit = IWL_FRAME_LIMIT,
2284 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2287 lockdep_assert_held(&mvm->mutex);
2289 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2290 vif->type != NL80211_IFTYPE_ADHOC))
2294 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2295 * invalid, so make sure we use the queue we want.
2296 * Note that this is done here as we want to avoid making DQA
2297 * changes in mac80211 layer.
2299 if (vif->type == NL80211_IFTYPE_ADHOC)
2300 mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2303 * While in previous FWs we had to exclude cab queue from TFD queue
2304 * mask, now it is needed as any other queue.
2306 if (!iwl_mvm_has_new_tx_api(mvm) &&
2307 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2308 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2310 msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2312 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2313 mvmvif->id, mvmvif->color);
2315 iwl_mvm_dealloc_int_sta(mvm, msta);
2320 * Enable cab queue after the ADD_STA command is sent.
2321 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2322 * command with unknown station id, and for FW that doesn't support
2323 * station API since the cab queue is not included in the
2326 if (iwl_mvm_has_new_tx_api(mvm)) {
2327 int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2330 mvmvif->cab_queue = queue;
2331 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2332 IWL_UCODE_TLV_API_STA_TYPE))
2333 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2339 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2340 struct ieee80211_key_conf *keyconf,
2344 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2345 struct iwl_mvm_add_sta_key_cmd cmd;
2347 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2348 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2353 /* This is a valid situation for GTK removal */
2354 if (sta_id == IWL_MVM_INVALID_STA)
2357 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2358 STA_KEY_FLG_KEYID_MSK);
2359 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2360 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2363 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2366 * The fields assigned here are in the same location at the start
2367 * of the command, so we can do this union trick.
2369 u.cmd.common.key_flags = key_flags;
2370 u.cmd.common.key_offset = keyconf->hw_key_idx;
2371 u.cmd.common.sta_id = sta_id;
2373 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2375 status = ADD_STA_SUCCESS;
2376 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2380 case ADD_STA_SUCCESS:
2381 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2385 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2393 * Send the FW a request to remove the station from it's internal data
2394 * structures, and in addition remove it from the local data structure.
2396 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2398 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2401 lockdep_assert_held(&mvm->mutex);
2403 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2405 iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
2407 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2409 IWL_WARN(mvm, "Failed sending remove station\n");
2414 #define IWL_MAX_RX_BA_SESSIONS 16
2416 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2418 struct iwl_mvm_delba_notif notif = {
2419 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2423 iwl_mvm_sync_rx_queues_internal(mvm, (void *)¬if, sizeof(notif));
2426 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2427 struct iwl_mvm_baid_data *data)
2431 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2433 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2435 struct iwl_mvm_reorder_buffer *reorder_buf =
2436 &data->reorder_buf[i];
2437 struct iwl_mvm_reorder_buf_entry *entries =
2438 &data->entries[i * data->entries_per_queue];
2440 spin_lock_bh(&reorder_buf->lock);
2441 if (likely(!reorder_buf->num_stored)) {
2442 spin_unlock_bh(&reorder_buf->lock);
2447 * This shouldn't happen in regular DELBA since the internal
2448 * delBA notification should trigger a release of all frames in
2449 * the reorder buffer.
2453 for (j = 0; j < reorder_buf->buf_size; j++)
2454 __skb_queue_purge(&entries[j].e.frames);
2456 * Prevent timer re-arm. This prevents a very far fetched case
2457 * where we timed out on the notification. There may be prior
2458 * RX frames pending in the RX queue before the notification
2459 * that might get processed between now and the actual deletion
2460 * and we would re-arm the timer although we are deleting the
2463 reorder_buf->removed = true;
2464 spin_unlock_bh(&reorder_buf->lock);
2465 del_timer_sync(&reorder_buf->reorder_timer);
2469 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2470 struct iwl_mvm_baid_data *data,
2471 u16 ssn, u16 buf_size)
2475 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2476 struct iwl_mvm_reorder_buffer *reorder_buf =
2477 &data->reorder_buf[i];
2478 struct iwl_mvm_reorder_buf_entry *entries =
2479 &data->entries[i * data->entries_per_queue];
2482 reorder_buf->num_stored = 0;
2483 reorder_buf->head_sn = ssn;
2484 reorder_buf->buf_size = buf_size;
2485 /* rx reorder timer */
2486 timer_setup(&reorder_buf->reorder_timer,
2487 iwl_mvm_reorder_timer_expired, 0);
2488 spin_lock_init(&reorder_buf->lock);
2489 reorder_buf->mvm = mvm;
2490 reorder_buf->queue = i;
2491 reorder_buf->valid = false;
2492 for (j = 0; j < reorder_buf->buf_size; j++)
2493 __skb_queue_head_init(&entries[j].e.frames);
2497 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2498 int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2500 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2501 struct iwl_mvm_add_sta_cmd cmd = {};
2502 struct iwl_mvm_baid_data *baid_data = NULL;
2506 lockdep_assert_held(&mvm->mutex);
2508 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2509 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2513 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2514 u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2516 /* sparse doesn't like the __align() so don't check */
2519 * The division below will be OK if either the cache line size
2520 * can be divided by the entry size (ALIGN will round up) or if
2521 * if the entry size can be divided by the cache line size, in
2522 * which case the ALIGN() will do nothing.
2524 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2525 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2529 * Upward align the reorder buffer size to fill an entire cache
2530 * line for each queue, to avoid sharing cache lines between
2533 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2536 * Allocate here so if allocation fails we can bail out early
2537 * before starting the BA session in the firmware
2539 baid_data = kzalloc(sizeof(*baid_data) +
2540 mvm->trans->num_rx_queues *
2547 * This division is why we need the above BUILD_BUG_ON(),
2548 * if that doesn't hold then this will not be right.
2550 baid_data->entries_per_queue =
2551 reorder_buf_size / sizeof(baid_data->entries[0]);
2554 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2555 cmd.sta_id = mvm_sta->sta_id;
2556 cmd.add_modify = STA_MODE_MODIFY;
2558 cmd.add_immediate_ba_tid = (u8) tid;
2559 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2560 cmd.rx_ba_window = cpu_to_le16(buf_size);
2562 cmd.remove_immediate_ba_tid = (u8) tid;
2564 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2565 STA_MODIFY_REMOVE_BA_TID;
2567 status = ADD_STA_SUCCESS;
2568 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2569 iwl_mvm_add_sta_cmd_size(mvm),
2574 switch (status & IWL_ADD_STA_STATUS_MASK) {
2575 case ADD_STA_SUCCESS:
2576 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2577 start ? "start" : "stopp");
2579 case ADD_STA_IMMEDIATE_BA_FAILURE:
2580 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2585 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2586 start ? "start" : "stopp", status);
2596 mvm->rx_ba_sessions++;
2598 if (!iwl_mvm_has_new_rx_api(mvm))
2601 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2605 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2606 IWL_ADD_STA_BAID_SHIFT);
2607 baid_data->baid = baid;
2608 baid_data->timeout = timeout;
2609 baid_data->last_rx = jiffies;
2610 baid_data->rcu_ptr = &mvm->baid_map[baid];
2611 timer_setup(&baid_data->session_timer,
2612 iwl_mvm_rx_agg_session_expired, 0);
2613 baid_data->mvm = mvm;
2614 baid_data->tid = tid;
2615 baid_data->sta_id = mvm_sta->sta_id;
2617 mvm_sta->tid_to_baid[tid] = baid;
2619 mod_timer(&baid_data->session_timer,
2620 TU_TO_EXP_TIME(timeout * 2));
2622 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2624 * protect the BA data with RCU to cover a case where our
2625 * internal RX sync mechanism will timeout (not that it's
2626 * supposed to happen) and we will free the session data while
2627 * RX is being processed in parallel
2629 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2630 mvm_sta->sta_id, tid, baid);
2631 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2632 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2634 u8 baid = mvm_sta->tid_to_baid[tid];
2636 if (mvm->rx_ba_sessions > 0)
2637 /* check that restart flow didn't zero the counter */
2638 mvm->rx_ba_sessions--;
2639 if (!iwl_mvm_has_new_rx_api(mvm))
2642 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2645 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2646 if (WARN_ON(!baid_data))
2649 /* synchronize all rx queues so we can safely delete */
2650 iwl_mvm_free_reorder(mvm, baid_data);
2651 del_timer_sync(&baid_data->session_timer);
2652 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2653 kfree_rcu(baid_data, rcu_head);
2654 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2663 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2664 int tid, u8 queue, bool start)
2666 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2667 struct iwl_mvm_add_sta_cmd cmd = {};
2671 lockdep_assert_held(&mvm->mutex);
2674 mvm_sta->tfd_queue_msk |= BIT(queue);
2675 mvm_sta->tid_disable_agg &= ~BIT(tid);
2677 /* In DQA-mode the queue isn't removed on agg termination */
2678 mvm_sta->tid_disable_agg |= BIT(tid);
2681 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2682 cmd.sta_id = mvm_sta->sta_id;
2683 cmd.add_modify = STA_MODE_MODIFY;
2684 if (!iwl_mvm_has_new_tx_api(mvm))
2685 cmd.modify_mask = STA_MODIFY_QUEUES;
2686 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2687 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2688 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2690 status = ADD_STA_SUCCESS;
2691 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2692 iwl_mvm_add_sta_cmd_size(mvm),
2697 switch (status & IWL_ADD_STA_STATUS_MASK) {
2698 case ADD_STA_SUCCESS:
2702 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2703 start ? "start" : "stopp", status);
2710 const u8 tid_to_mac80211_ac[] = {
2719 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2722 static const u8 tid_to_ucode_ac[] = {
2733 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2734 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2736 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2737 struct iwl_mvm_tid_data *tid_data;
2742 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2745 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2746 mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2748 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2749 mvmsta->tid_data[tid].state);
2753 lockdep_assert_held(&mvm->mutex);
2755 if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2756 iwl_mvm_has_new_tx_api(mvm)) {
2757 u8 ac = tid_to_mac80211_ac[tid];
2759 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2764 spin_lock_bh(&mvmsta->lock);
2766 /* possible race condition - we entered D0i3 while starting agg */
2767 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2768 spin_unlock_bh(&mvmsta->lock);
2769 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2774 * Note the possible cases:
2775 * 1. An enabled TXQ - TXQ needs to become agg'ed
2776 * 2. The TXQ hasn't yet been enabled, so find a free one and mark
2779 txq_id = mvmsta->tid_data[tid].txq_id;
2780 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2781 ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2782 IWL_MVM_DQA_MIN_DATA_QUEUE,
2783 IWL_MVM_DQA_MAX_DATA_QUEUE);
2785 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2791 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2792 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2793 } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2795 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2796 tid, IWL_MAX_HW_QUEUES - 1);
2799 } else if (unlikely(mvm->queue_info[txq_id].status ==
2800 IWL_MVM_QUEUE_SHARED)) {
2802 IWL_DEBUG_TX_QUEUES(mvm,
2803 "Can't start tid %d agg on shared queue!\n",
2808 IWL_DEBUG_TX_QUEUES(mvm,
2809 "AGG for tid %d will be on queue #%d\n",
2812 tid_data = &mvmsta->tid_data[tid];
2813 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2814 tid_data->txq_id = txq_id;
2815 *ssn = tid_data->ssn;
2817 IWL_DEBUG_TX_QUEUES(mvm,
2818 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2819 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2820 tid_data->next_reclaimed);
2823 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2824 * to align the wrap around of ssn so we compare relevant values.
2826 normalized_ssn = tid_data->ssn;
2827 if (mvm->trans->cfg->gen2)
2828 normalized_ssn &= 0xff;
2830 if (normalized_ssn == tid_data->next_reclaimed) {
2831 tid_data->state = IWL_AGG_STARTING;
2832 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2834 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2840 spin_unlock_bh(&mvmsta->lock);
2845 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2846 struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2849 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2850 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2851 unsigned int wdg_timeout =
2852 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2854 bool alloc_queue = true;
2855 enum iwl_mvm_queue_status queue_status;
2858 struct iwl_trans_txq_scd_cfg cfg = {
2859 .sta_id = mvmsta->sta_id,
2861 .frame_limit = buf_size,
2866 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2867 * manager, so this function should never be called in this case.
2869 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2872 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2873 != IWL_MAX_TID_COUNT);
2875 spin_lock_bh(&mvmsta->lock);
2876 ssn = tid_data->ssn;
2877 queue = tid_data->txq_id;
2878 tid_data->state = IWL_AGG_ON;
2879 mvmsta->agg_tids |= BIT(tid);
2880 tid_data->ssn = 0xffff;
2881 tid_data->amsdu_in_ampdu_allowed = amsdu;
2882 spin_unlock_bh(&mvmsta->lock);
2884 if (iwl_mvm_has_new_tx_api(mvm)) {
2886 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2887 * would have failed, so if we are here there is no need to
2889 * However, if aggregation size is different than the default
2890 * size, the scheduler should be reconfigured.
2891 * We cannot do this with the new TX API, so return unsupported
2892 * for now, until it will be offloaded to firmware..
2893 * Note that if SCD default value changes - this condition
2894 * should be updated as well.
2896 if (buf_size < IWL_FRAME_LIMIT)
2899 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2905 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2907 queue_status = mvm->queue_info[queue].status;
2909 /* Maybe there is no need to even alloc a queue... */
2910 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2911 alloc_queue = false;
2914 * Only reconfig the SCD for the queue if the window size has
2915 * changed from current (become smaller)
2917 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2919 * If reconfiguring an existing queue, it first must be
2922 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2926 "Error draining queue before reconfig\n");
2930 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2931 mvmsta->sta_id, tid,
2935 "Error reconfiguring TXQ #%d\n", queue);
2941 iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2944 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2945 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2946 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2951 /* No need to mark as reserved */
2952 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2956 * Even though in theory the peer could have different
2957 * aggregation reorder buffer sizes for different sessions,
2958 * our ucode doesn't allow for that and has a global limit
2959 * for each station. Therefore, use the minimum of all the
2960 * aggregation sessions and our default value.
2962 mvmsta->max_agg_bufsize =
2963 min(mvmsta->max_agg_bufsize, buf_size);
2964 mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2966 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2969 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq, false);
2972 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2973 struct iwl_mvm_sta *mvmsta,
2974 struct iwl_mvm_tid_data *tid_data)
2976 u16 txq_id = tid_data->txq_id;
2978 lockdep_assert_held(&mvm->mutex);
2980 if (iwl_mvm_has_new_tx_api(mvm))
2984 * The TXQ is marked as reserved only if no traffic came through yet
2985 * This means no traffic has been sent on this TID (agg'd or not), so
2986 * we no longer have use for the queue. Since it hasn't even been
2987 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2990 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
2991 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2992 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
2996 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2997 struct ieee80211_sta *sta, u16 tid)
2999 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3000 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3005 * If mac80211 is cleaning its state, then say that we finished since
3006 * our state has been cleared anyway.
3008 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3009 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3013 spin_lock_bh(&mvmsta->lock);
3015 txq_id = tid_data->txq_id;
3017 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3018 mvmsta->sta_id, tid, txq_id, tid_data->state);
3020 mvmsta->agg_tids &= ~BIT(tid);
3022 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3024 switch (tid_data->state) {
3026 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3028 IWL_DEBUG_TX_QUEUES(mvm,
3029 "ssn = %d, next_recl = %d\n",
3030 tid_data->ssn, tid_data->next_reclaimed);
3032 tid_data->ssn = 0xffff;
3033 tid_data->state = IWL_AGG_OFF;
3034 spin_unlock_bh(&mvmsta->lock);
3036 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3038 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3040 case IWL_AGG_STARTING:
3041 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3043 * The agg session has been stopped before it was set up. This
3044 * can happen when the AddBA timer times out for example.
3047 /* No barriers since we are under mutex */
3048 lockdep_assert_held(&mvm->mutex);
3050 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3051 tid_data->state = IWL_AGG_OFF;
3056 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3057 mvmsta->sta_id, tid, tid_data->state);
3059 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
3063 spin_unlock_bh(&mvmsta->lock);
3068 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3069 struct ieee80211_sta *sta, u16 tid)
3071 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3072 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3074 enum iwl_mvm_agg_state old_state;
3077 * First set the agg state to OFF to avoid calling
3078 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3080 spin_lock_bh(&mvmsta->lock);
3081 txq_id = tid_data->txq_id;
3082 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3083 mvmsta->sta_id, tid, txq_id, tid_data->state);
3084 old_state = tid_data->state;
3085 tid_data->state = IWL_AGG_OFF;
3086 mvmsta->agg_tids &= ~BIT(tid);
3087 spin_unlock_bh(&mvmsta->lock);
3089 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3091 if (old_state >= IWL_AGG_ON) {
3092 iwl_mvm_drain_sta(mvm, mvmsta, true);
3094 if (iwl_mvm_has_new_tx_api(mvm)) {
3095 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3097 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3098 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3100 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3101 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3102 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3105 iwl_mvm_drain_sta(mvm, mvmsta, false);
3107 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3113 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3115 int i, max = -1, max_offs = -1;
3117 lockdep_assert_held(&mvm->mutex);
3119 /* Pick the unused key offset with the highest 'deleted'
3120 * counter. Every time a key is deleted, all the counters
3121 * are incremented and the one that was just deleted is
3122 * reset to zero. Thus, the highest counter is the one
3123 * that was deleted longest ago. Pick that one.
3125 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3126 if (test_bit(i, mvm->fw_key_table))
3128 if (mvm->fw_key_deleted[i] > max) {
3129 max = mvm->fw_key_deleted[i];
3135 return STA_KEY_IDX_INVALID;
3140 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3141 struct ieee80211_vif *vif,
3142 struct ieee80211_sta *sta)
3144 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3147 return iwl_mvm_sta_from_mac80211(sta);
3150 * The device expects GTKs for station interfaces to be
3151 * installed as GTKs for the AP station. If we have no
3152 * station ID, then use AP's station ID.
3154 if (vif->type == NL80211_IFTYPE_STATION &&
3155 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3156 u8 sta_id = mvmvif->ap_sta_id;
3158 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3159 lockdep_is_held(&mvm->mutex));
3162 * It is possible that the 'sta' parameter is NULL,
3163 * for example when a GTK is removed - the sta_id will then
3164 * be the AP ID, and no station was passed by mac80211.
3166 if (IS_ERR_OR_NULL(sta))
3169 return iwl_mvm_sta_from_mac80211(sta);
3175 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3177 struct ieee80211_key_conf *key, bool mcast,
3178 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3179 u8 key_offset, bool mfp)
3182 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3183 struct iwl_mvm_add_sta_key_cmd cmd;
3191 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3192 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3194 if (sta_id == IWL_MVM_INVALID_STA)
3197 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3198 STA_KEY_FLG_KEYID_MSK;
3199 key_flags = cpu_to_le16(keyidx);
3200 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3202 switch (key->cipher) {
3203 case WLAN_CIPHER_SUITE_TKIP:
3204 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3206 memcpy((void *)&u.cmd.tx_mic_key,
3207 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3210 memcpy((void *)&u.cmd.rx_mic_key,
3211 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3213 pn = atomic64_read(&key->tx_pn);
3216 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3217 for (i = 0; i < 5; i++)
3218 u.cmd_v1.tkip_rx_ttak[i] =
3219 cpu_to_le16(tkip_p1k[i]);
3221 memcpy(u.cmd.common.key, key->key, key->keylen);
3223 case WLAN_CIPHER_SUITE_CCMP:
3224 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3225 memcpy(u.cmd.common.key, key->key, key->keylen);
3227 pn = atomic64_read(&key->tx_pn);
3229 case WLAN_CIPHER_SUITE_WEP104:
3230 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3232 case WLAN_CIPHER_SUITE_WEP40:
3233 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3234 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3236 case WLAN_CIPHER_SUITE_GCMP_256:
3237 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3239 case WLAN_CIPHER_SUITE_GCMP:
3240 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3241 memcpy(u.cmd.common.key, key->key, key->keylen);
3243 pn = atomic64_read(&key->tx_pn);
3246 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3247 memcpy(u.cmd.common.key, key->key, key->keylen);
3251 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3253 key_flags |= cpu_to_le16(STA_KEY_MFP);
3255 u.cmd.common.key_offset = key_offset;
3256 u.cmd.common.key_flags = key_flags;
3257 u.cmd.common.sta_id = sta_id;
3260 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3261 size = sizeof(u.cmd);
3263 size = sizeof(u.cmd_v1);
3266 status = ADD_STA_SUCCESS;
3267 if (cmd_flags & CMD_ASYNC)
3268 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3271 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3275 case ADD_STA_SUCCESS:
3276 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3280 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3287 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3288 struct ieee80211_key_conf *keyconf,
3289 u8 sta_id, bool remove_key)
3291 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3293 /* verify the key details match the required command's expectations */
3294 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3295 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3296 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3297 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3298 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3301 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3302 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3305 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3306 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3309 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3311 struct ieee80211_key_seq seq;
3314 switch (keyconf->cipher) {
3315 case WLAN_CIPHER_SUITE_AES_CMAC:
3316 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3318 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3319 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3320 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3326 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3327 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3328 igtk_cmd.ctrl_flags |=
3329 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3330 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3331 pn = seq.aes_cmac.pn;
3332 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3333 ((u64) pn[4] << 8) |
3334 ((u64) pn[3] << 16) |
3335 ((u64) pn[2] << 24) |
3336 ((u64) pn[1] << 32) |
3337 ((u64) pn[0] << 40));
3340 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3341 remove_key ? "removing" : "installing",
3344 if (!iwl_mvm_has_new_rx_api(mvm)) {
3345 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3346 .ctrl_flags = igtk_cmd.ctrl_flags,
3347 .key_id = igtk_cmd.key_id,
3348 .sta_id = igtk_cmd.sta_id,
3349 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3352 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3353 ARRAY_SIZE(igtk_cmd_v1.igtk));
3354 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3355 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3357 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3358 sizeof(igtk_cmd), &igtk_cmd);
3362 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3363 struct ieee80211_vif *vif,
3364 struct ieee80211_sta *sta)
3366 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3371 if (vif->type == NL80211_IFTYPE_STATION &&
3372 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3373 u8 sta_id = mvmvif->ap_sta_id;
3374 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3375 lockdep_is_held(&mvm->mutex));
3383 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3384 struct ieee80211_vif *vif,
3385 struct ieee80211_sta *sta,
3386 struct ieee80211_key_conf *keyconf,
3392 struct ieee80211_key_seq seq;
3398 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3400 sta_id = mvm_sta->sta_id;
3402 } else if (vif->type == NL80211_IFTYPE_AP &&
3403 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3404 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3406 sta_id = mvmvif->mcast_sta.sta_id;
3408 IWL_ERR(mvm, "Failed to find station id\n");
3412 switch (keyconf->cipher) {
3413 case WLAN_CIPHER_SUITE_TKIP:
3414 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3415 /* get phase 1 key from mac80211 */
3416 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3417 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3418 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3419 seq.tkip.iv32, p1k, 0, key_offset,
3422 case WLAN_CIPHER_SUITE_CCMP:
3423 case WLAN_CIPHER_SUITE_WEP40:
3424 case WLAN_CIPHER_SUITE_WEP104:
3425 case WLAN_CIPHER_SUITE_GCMP:
3426 case WLAN_CIPHER_SUITE_GCMP_256:
3427 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3428 0, NULL, 0, key_offset, mfp);
3431 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3432 0, NULL, 0, key_offset, mfp);
3438 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3439 struct ieee80211_vif *vif,
3440 struct ieee80211_sta *sta,
3441 struct ieee80211_key_conf *keyconf,
3444 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3445 struct iwl_mvm_sta *mvm_sta;
3446 u8 sta_id = IWL_MVM_INVALID_STA;
3448 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3450 lockdep_assert_held(&mvm->mutex);
3452 if (vif->type != NL80211_IFTYPE_AP ||
3453 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3454 /* Get the station id from the mvm local station table */
3455 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3457 IWL_ERR(mvm, "Failed to find station\n");
3460 sta_id = mvm_sta->sta_id;
3463 * It is possible that the 'sta' parameter is NULL, and thus
3464 * there is a need to retrieve the sta from the local station
3468 sta = rcu_dereference_protected(
3469 mvm->fw_id_to_mac_id[sta_id],
3470 lockdep_is_held(&mvm->mutex));
3471 if (IS_ERR_OR_NULL(sta)) {
3472 IWL_ERR(mvm, "Invalid station id\n");
3477 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3480 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3482 sta_id = mvmvif->mcast_sta.sta_id;
3485 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3486 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3487 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3488 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3492 /* If the key_offset is not pre-assigned, we need to find a
3493 * new offset to use. In normal cases, the offset is not
3494 * pre-assigned, but during HW_RESTART we want to reuse the
3495 * same indices, so we pass them when this function is called.
3497 * In D3 entry, we need to hardcoded the indices (because the
3498 * firmware hardcodes the PTK offset to 0). In this case, we
3499 * need to make sure we don't overwrite the hw_key_idx in the
3500 * keyconf structure, because otherwise we cannot configure
3501 * the original ones back when resuming.
3503 if (key_offset == STA_KEY_IDX_INVALID) {
3504 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3505 if (key_offset == STA_KEY_IDX_INVALID)
3507 keyconf->hw_key_idx = key_offset;
3510 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3515 * For WEP, the same key is used for multicast and unicast. Upload it
3516 * again, using the same key offset, and now pointing the other one
3517 * to the same key slot (offset).
3518 * If this fails, remove the original as well.
3520 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3521 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3523 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3524 key_offset, !mcast);
3526 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3531 __set_bit(key_offset, mvm->fw_key_table);
3534 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3535 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3536 sta ? sta->addr : zero_addr, ret);
3540 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3541 struct ieee80211_vif *vif,
3542 struct ieee80211_sta *sta,
3543 struct ieee80211_key_conf *keyconf)
3545 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3546 struct iwl_mvm_sta *mvm_sta;
3547 u8 sta_id = IWL_MVM_INVALID_STA;
3550 lockdep_assert_held(&mvm->mutex);
3552 /* Get the station from the mvm local station table */
3553 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3555 sta_id = mvm_sta->sta_id;
3556 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3557 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3560 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3561 keyconf->keyidx, sta_id);
3563 if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3564 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3565 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
3566 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3568 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3569 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3570 keyconf->hw_key_idx);
3574 /* track which key was deleted last */
3575 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3576 if (mvm->fw_key_deleted[i] < U8_MAX)
3577 mvm->fw_key_deleted[i]++;
3579 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3581 if (sta && !mvm_sta) {
3582 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3586 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3590 /* delete WEP key twice to get rid of (now useless) offset */
3591 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3592 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3593 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3598 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3599 struct ieee80211_vif *vif,
3600 struct ieee80211_key_conf *keyconf,
3601 struct ieee80211_sta *sta, u32 iv32,
3604 struct iwl_mvm_sta *mvm_sta;
3605 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3606 bool mfp = sta ? sta->mfp : false;
3610 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3611 if (WARN_ON_ONCE(!mvm_sta))
3613 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3614 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3621 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3622 struct ieee80211_sta *sta)
3624 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3625 struct iwl_mvm_add_sta_cmd cmd = {
3626 .add_modify = STA_MODE_MODIFY,
3627 .sta_id = mvmsta->sta_id,
3628 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3629 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3633 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3634 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3636 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3639 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3640 struct ieee80211_sta *sta,
3641 enum ieee80211_frame_release_type reason,
3642 u16 cnt, u16 tids, bool more_data,
3643 bool single_sta_queue)
3645 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3646 struct iwl_mvm_add_sta_cmd cmd = {
3647 .add_modify = STA_MODE_MODIFY,
3648 .sta_id = mvmsta->sta_id,
3649 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3650 .sleep_tx_count = cpu_to_le16(cnt),
3651 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3654 unsigned long _tids = tids;
3656 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3657 * Note that this field is reserved and unused by firmware not
3658 * supporting GO uAPSD, so it's safe to always do this.
3660 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3661 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3663 /* If we're releasing frames from aggregation or dqa queues then check
3664 * if all the queues that we're releasing frames from, combined, have:
3665 * - more frames than the service period, in which case more_data
3667 * - fewer than 'cnt' frames, in which case we need to adjust the
3668 * firmware command (but do that unconditionally)
3670 if (single_sta_queue) {
3671 int remaining = cnt;
3674 spin_lock_bh(&mvmsta->lock);
3675 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3676 struct iwl_mvm_tid_data *tid_data;
3679 tid_data = &mvmsta->tid_data[tid];
3681 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3682 if (n_queued > remaining) {
3687 remaining -= n_queued;
3689 sleep_tx_count = cnt - remaining;
3690 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3691 mvmsta->sleep_tx_count = sleep_tx_count;
3692 spin_unlock_bh(&mvmsta->lock);
3694 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3695 if (WARN_ON(cnt - remaining == 0)) {
3696 ieee80211_sta_eosp(sta);
3701 /* Note: this is ignored by firmware not supporting GO uAPSD */
3703 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3705 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3706 mvmsta->next_status_eosp = true;
3707 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3709 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3712 /* block the Tx queues until the FW updated the sleep Tx count */
3713 iwl_trans_block_txq_ptrs(mvm->trans, true);
3715 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3716 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3717 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3719 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3722 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3723 struct iwl_rx_cmd_buffer *rxb)
3725 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3726 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3727 struct ieee80211_sta *sta;
3728 u32 sta_id = le32_to_cpu(notif->sta_id);
3730 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3734 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3735 if (!IS_ERR_OR_NULL(sta))
3736 ieee80211_sta_eosp(sta);
3740 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3741 struct iwl_mvm_sta *mvmsta, bool disable)
3743 struct iwl_mvm_add_sta_cmd cmd = {
3744 .add_modify = STA_MODE_MODIFY,
3745 .sta_id = mvmsta->sta_id,
3746 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3747 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3748 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3752 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3753 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3755 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3758 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3759 struct ieee80211_sta *sta,
3762 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3764 spin_lock_bh(&mvm_sta->lock);
3766 if (mvm_sta->disable_tx == disable) {
3767 spin_unlock_bh(&mvm_sta->lock);
3771 mvm_sta->disable_tx = disable;
3773 /* Tell mac80211 to start/stop queuing tx for this station */
3774 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3776 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3778 spin_unlock_bh(&mvm_sta->lock);
3781 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3782 struct iwl_mvm_vif *mvmvif,
3783 struct iwl_mvm_int_sta *sta,
3786 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3787 struct iwl_mvm_add_sta_cmd cmd = {
3788 .add_modify = STA_MODE_MODIFY,
3789 .sta_id = sta->sta_id,
3790 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3791 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3792 .mac_id_n_color = cpu_to_le32(id),
3796 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3797 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3799 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3802 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3803 struct iwl_mvm_vif *mvmvif,
3806 struct ieee80211_sta *sta;
3807 struct iwl_mvm_sta *mvm_sta;
3810 lockdep_assert_held(&mvm->mutex);
3812 /* Block/unblock all the stations of the given mvmvif */
3813 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3814 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3815 lockdep_is_held(&mvm->mutex));
3816 if (IS_ERR_OR_NULL(sta))
3819 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3820 if (mvm_sta->mac_id_n_color !=
3821 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3824 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3827 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3830 /* Need to block/unblock also multicast station */
3831 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3832 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3833 &mvmvif->mcast_sta, disable);
3836 * Only unblock the broadcast station (FW blocks it for immediate
3837 * quiet, not the driver)
3839 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3840 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3841 &mvmvif->bcast_sta, disable);
3844 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3846 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3847 struct iwl_mvm_sta *mvmsta;
3851 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3853 if (!WARN_ON(!mvmsta))
3854 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3859 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3861 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3864 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3865 * to align the wrap around of ssn so we compare relevant values.
3867 if (mvm->trans->cfg->gen2)
3870 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);