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 Intel Deutschland GmbH
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
29 * Contact Information:
30 * Intel Linux Wireless <linuxwifi@intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
35 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37 * Copyright(c) 2016 Intel Deutschland GmbH
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66 *****************************************************************************/
67 #include <net/mac80211.h>
74 * New version of ADD_STA_sta command added new fields at the end of the
75 * structure, so sending the size of the relevant API's structure is enough to
76 * support both API versions.
78 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
80 return iwl_mvm_has_new_rx_api(mvm) ?
81 sizeof(struct iwl_mvm_add_sta_cmd) :
82 sizeof(struct iwl_mvm_add_sta_cmd_v7);
85 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
86 enum nl80211_iftype iftype)
91 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
92 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
94 lockdep_assert_held(&mvm->mutex);
96 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
97 if (iftype != NL80211_IFTYPE_STATION)
98 reserved_ids = BIT(0);
100 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
101 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
102 if (BIT(sta_id) & reserved_ids)
105 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
106 lockdep_is_held(&mvm->mutex)))
109 return IWL_MVM_STATION_COUNT;
112 /* send station add/update command to firmware */
113 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
114 bool update, unsigned int flags)
116 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
117 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
118 .sta_id = mvm_sta->sta_id,
119 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
120 .add_modify = update ? 1 : 0,
121 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
122 STA_FLG_MIMO_EN_MSK),
123 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
127 u32 agg_size = 0, mpdu_dens = 0;
129 if (!update || (flags & STA_MODIFY_QUEUES)) {
130 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
131 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
133 if (flags & STA_MODIFY_QUEUES)
134 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
137 switch (sta->bandwidth) {
138 case IEEE80211_STA_RX_BW_160:
139 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
141 case IEEE80211_STA_RX_BW_80:
142 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
144 case IEEE80211_STA_RX_BW_40:
145 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
147 case IEEE80211_STA_RX_BW_20:
148 if (sta->ht_cap.ht_supported)
149 add_sta_cmd.station_flags |=
150 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
154 switch (sta->rx_nss) {
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
162 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
166 switch (sta->smps_mode) {
167 case IEEE80211_SMPS_AUTOMATIC:
168 case IEEE80211_SMPS_NUM_MODES:
171 case IEEE80211_SMPS_STATIC:
173 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
174 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
176 case IEEE80211_SMPS_DYNAMIC:
177 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
179 case IEEE80211_SMPS_OFF:
184 if (sta->ht_cap.ht_supported) {
185 add_sta_cmd.station_flags_msk |=
186 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
187 STA_FLG_AGG_MPDU_DENS_MSK);
189 mpdu_dens = sta->ht_cap.ampdu_density;
192 if (sta->vht_cap.vht_supported) {
193 agg_size = sta->vht_cap.cap &
194 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
196 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
197 } else if (sta->ht_cap.ht_supported) {
198 agg_size = sta->ht_cap.ampdu_factor;
201 add_sta_cmd.station_flags |=
202 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
203 add_sta_cmd.station_flags |=
204 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
206 status = ADD_STA_SUCCESS;
207 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
208 iwl_mvm_add_sta_cmd_size(mvm),
209 &add_sta_cmd, &status);
213 switch (status & IWL_ADD_STA_STATUS_MASK) {
214 case ADD_STA_SUCCESS:
215 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
219 IWL_ERR(mvm, "ADD_STA failed\n");
226 static void iwl_mvm_rx_agg_session_expired(unsigned long data)
228 struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
229 struct iwl_mvm_baid_data *ba_data;
230 struct ieee80211_sta *sta;
231 struct iwl_mvm_sta *mvm_sta;
232 unsigned long timeout;
236 ba_data = rcu_dereference(*rcu_ptr);
238 if (WARN_ON(!ba_data))
241 if (!ba_data->timeout)
244 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
245 if (time_is_after_jiffies(timeout)) {
246 mod_timer(&ba_data->session_timer, timeout);
251 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
252 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
253 ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
254 sta->addr, ba_data->tid);
259 static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
260 struct ieee80211_sta *sta)
262 unsigned long used_hw_queues;
263 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
264 unsigned int wdg_timeout =
265 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
268 lockdep_assert_held(&mvm->mutex);
270 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
272 /* Find available queues, and allocate them to the ACs */
273 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
274 u8 queue = find_first_zero_bit(&used_hw_queues,
275 mvm->first_agg_queue);
277 if (queue >= mvm->first_agg_queue) {
278 IWL_ERR(mvm, "Failed to allocate STA queue\n");
282 __set_bit(queue, &used_hw_queues);
283 mvmsta->hw_queue[ac] = queue;
286 /* Found a place for all queues - enable them */
287 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
288 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
289 mvmsta->hw_queue[ac],
290 iwl_mvm_ac_to_tx_fifo[ac], 0,
292 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
298 static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
299 struct ieee80211_sta *sta)
301 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
302 unsigned long sta_msk;
305 lockdep_assert_held(&mvm->mutex);
307 /* disable the TDLS STA-specific queues */
308 sta_msk = mvmsta->tfd_queue_msk;
309 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
310 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
313 /* Disable aggregations for a bitmap of TIDs for a given station */
314 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
315 unsigned long disable_agg_tids,
318 struct iwl_mvm_add_sta_cmd cmd = {};
319 struct ieee80211_sta *sta;
320 struct iwl_mvm_sta *mvmsta;
325 spin_lock_bh(&mvm->queue_info_lock);
326 sta_id = mvm->queue_info[queue].ra_sta_id;
327 spin_unlock_bh(&mvm->queue_info_lock);
331 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
333 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
338 mvmsta = iwl_mvm_sta_from_mac80211(sta);
340 mvmsta->tid_disable_agg |= disable_agg_tids;
342 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
343 cmd.sta_id = mvmsta->sta_id;
344 cmd.add_modify = STA_MODE_MODIFY;
345 cmd.modify_mask = STA_MODIFY_QUEUES;
346 if (disable_agg_tids)
347 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
349 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
350 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
351 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
355 /* Notify FW of queue removal from the STA queues */
356 status = ADD_STA_SUCCESS;
357 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
358 iwl_mvm_add_sta_cmd_size(mvm),
364 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
366 struct ieee80211_sta *sta;
367 struct iwl_mvm_sta *mvmsta;
368 unsigned long tid_bitmap;
369 unsigned long agg_tids = 0;
373 lockdep_assert_held(&mvm->mutex);
375 spin_lock_bh(&mvm->queue_info_lock);
376 sta_id = mvm->queue_info[queue].ra_sta_id;
377 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
378 spin_unlock_bh(&mvm->queue_info_lock);
380 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
381 lockdep_is_held(&mvm->mutex));
383 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
386 mvmsta = iwl_mvm_sta_from_mac80211(sta);
388 spin_lock_bh(&mvmsta->lock);
389 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
390 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
391 agg_tids |= BIT(tid);
393 spin_unlock_bh(&mvmsta->lock);
399 * Remove a queue from a station's resources.
400 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
401 * doesn't disable the queue
403 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
405 struct ieee80211_sta *sta;
406 struct iwl_mvm_sta *mvmsta;
407 unsigned long tid_bitmap;
408 unsigned long disable_agg_tids = 0;
412 lockdep_assert_held(&mvm->mutex);
414 spin_lock_bh(&mvm->queue_info_lock);
415 sta_id = mvm->queue_info[queue].ra_sta_id;
416 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
417 spin_unlock_bh(&mvm->queue_info_lock);
421 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
423 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
428 mvmsta = iwl_mvm_sta_from_mac80211(sta);
430 spin_lock_bh(&mvmsta->lock);
431 /* Unmap MAC queues and TIDs from this queue */
432 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
433 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
434 disable_agg_tids |= BIT(tid);
435 mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
438 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
439 spin_unlock_bh(&mvmsta->lock);
443 spin_lock_bh(&mvm->queue_info_lock);
444 /* Unmap MAC queues and TIDs from this queue */
445 mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
446 mvm->queue_info[queue].hw_queue_refcount = 0;
447 mvm->queue_info[queue].tid_bitmap = 0;
448 spin_unlock_bh(&mvm->queue_info_lock);
450 return disable_agg_tids;
453 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
454 unsigned long tfd_queue_mask, u8 ac)
457 u8 ac_to_queue[IEEE80211_NUM_ACS];
460 lockdep_assert_held(&mvm->queue_info_lock);
462 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
464 /* See what ACs the existing queues for this STA have */
465 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
466 /* Only DATA queues can be shared */
467 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
468 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
471 /* Don't try and take queues being reconfigured */
472 if (mvm->queue_info[queue].status ==
473 IWL_MVM_QUEUE_RECONFIGURING)
476 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
480 * The queue to share is chosen only from DATA queues as follows (in
481 * descending priority):
484 * 3. Highest AC queue that is lower than new AC
485 * 4. Any existing AC (there always is at least 1 DATA queue)
488 /* Priority 1: An AC_BE queue */
489 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
490 queue = ac_to_queue[IEEE80211_AC_BE];
491 /* Priority 2: Same AC queue */
492 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
493 queue = ac_to_queue[ac];
494 /* Priority 3a: If new AC is VO and VI exists - use VI */
495 else if (ac == IEEE80211_AC_VO &&
496 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
497 queue = ac_to_queue[IEEE80211_AC_VI];
498 /* Priority 3b: No BE so only AC less than the new one is BK */
499 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
500 queue = ac_to_queue[IEEE80211_AC_BK];
501 /* Priority 4a: No BE nor BK - use VI if exists */
502 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
503 queue = ac_to_queue[IEEE80211_AC_VI];
504 /* Priority 4b: No BE, BK nor VI - use VO if exists */
505 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
506 queue = ac_to_queue[IEEE80211_AC_VO];
508 /* Make sure queue found (or not) is legal */
509 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
510 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
511 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
512 IWL_ERR(mvm, "No DATA queues available to share\n");
516 /* Make sure the queue isn't in the middle of being reconfigured */
517 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
519 "TXQ %d is in the middle of re-config - try again\n",
528 * If a given queue has a higher AC than the TID stream that is being compared
529 * to, the queue needs to be redirected to the lower AC. This function does that
530 * in such a case, otherwise - if no redirection required - it does nothing,
531 * unless the %force param is true.
533 int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
534 int ac, int ssn, unsigned int wdg_timeout,
537 struct iwl_scd_txq_cfg_cmd cmd = {
539 .action = SCD_CFG_DISABLE_QUEUE,
546 * If the AC is lower than current one - FIFO needs to be redirected to
547 * the lowest one of the streams in the queue. Check if this is needed
549 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
550 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
551 * we need to check if the numerical value of X is LARGER than of Y.
553 spin_lock_bh(&mvm->queue_info_lock);
554 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
555 spin_unlock_bh(&mvm->queue_info_lock);
557 IWL_DEBUG_TX_QUEUES(mvm,
558 "No redirection needed on TXQ #%d\n",
563 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
564 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
565 cmd.tid = mvm->queue_info[queue].txq_tid;
566 mq = mvm->queue_info[queue].hw_queue_to_mac80211;
567 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
568 spin_unlock_bh(&mvm->queue_info_lock);
570 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
571 queue, iwl_mvm_ac_to_tx_fifo[ac]);
573 /* Stop MAC queues and wait for this queue to empty */
574 iwl_mvm_stop_mac_queues(mvm, mq);
575 ret = iwl_trans_wait_tx_queue_empty(mvm->trans, BIT(queue));
577 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
583 /* Before redirecting the queue we need to de-activate it */
584 iwl_trans_txq_disable(mvm->trans, queue, false);
585 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
587 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
590 /* Make sure the SCD wrptr is correctly set before reconfiguring */
591 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
593 /* Update the TID "owner" of the queue */
594 spin_lock_bh(&mvm->queue_info_lock);
595 mvm->queue_info[queue].txq_tid = tid;
596 spin_unlock_bh(&mvm->queue_info_lock);
598 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
600 /* Redirect to lower AC */
601 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
602 cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
605 /* Update AC marking of the queue */
606 spin_lock_bh(&mvm->queue_info_lock);
607 mvm->queue_info[queue].mac80211_ac = ac;
608 spin_unlock_bh(&mvm->queue_info_lock);
611 * Mark queue as shared in transport if shared
612 * Note this has to be done after queue enablement because enablement
613 * can also set this value, and there is no indication there to shared
617 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
620 /* Continue using the MAC queues */
621 iwl_mvm_start_mac_queues(mvm, mq);
626 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
627 struct ieee80211_sta *sta, u8 ac, int tid,
628 struct ieee80211_hdr *hdr)
630 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
631 struct iwl_trans_txq_scd_cfg cfg = {
632 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
633 .sta_id = mvmsta->sta_id,
635 .frame_limit = IWL_FRAME_LIMIT,
637 unsigned int wdg_timeout =
638 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
639 u8 mac_queue = mvmsta->vif->hw_queue[ac];
641 bool using_inactive_queue = false;
642 unsigned long disable_agg_tids = 0;
643 enum iwl_mvm_agg_state queue_state;
644 bool shared_queue = false;
646 unsigned long tfd_queue_mask;
649 lockdep_assert_held(&mvm->mutex);
651 spin_lock_bh(&mvmsta->lock);
652 tfd_queue_mask = mvmsta->tfd_queue_msk;
653 spin_unlock_bh(&mvmsta->lock);
655 spin_lock_bh(&mvm->queue_info_lock);
658 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
661 if (!ieee80211_is_data_qos(hdr->frame_control) ||
662 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
663 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
664 IWL_MVM_DQA_MIN_MGMT_QUEUE,
665 IWL_MVM_DQA_MAX_MGMT_QUEUE);
666 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
667 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
670 /* If no such queue is found, we'll use a DATA queue instead */
673 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
674 (mvm->queue_info[mvmsta->reserved_queue].status ==
675 IWL_MVM_QUEUE_RESERVED ||
676 mvm->queue_info[mvmsta->reserved_queue].status ==
677 IWL_MVM_QUEUE_INACTIVE)) {
678 queue = mvmsta->reserved_queue;
679 mvm->queue_info[queue].reserved = true;
680 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
684 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
685 IWL_MVM_DQA_MIN_DATA_QUEUE,
686 IWL_MVM_DQA_MAX_DATA_QUEUE);
689 * Check if this queue is already allocated but inactive.
690 * In such a case, we'll need to first free this queue before enabling
691 * it again, so we'll mark it as reserved to make sure no new traffic
695 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
696 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
697 using_inactive_queue = true;
698 IWL_DEBUG_TX_QUEUES(mvm,
699 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
700 queue, mvmsta->sta_id, tid);
703 /* No free queue - we'll have to share */
705 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
708 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
713 * Mark TXQ as ready, even though it hasn't been fully configured yet,
714 * to make sure no one else takes it.
715 * This will allow avoiding re-acquiring the lock at the end of the
716 * configuration. On error we'll mark it back as free.
718 if ((queue > 0) && !shared_queue)
719 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
721 spin_unlock_bh(&mvm->queue_info_lock);
723 /* This shouldn't happen - out of queues */
724 if (WARN_ON(queue <= 0)) {
725 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
731 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
732 * but for configuring the SCD to send A-MPDUs we need to mark the queue
734 * Mark all DATA queues as allowing to be aggregated at some point
736 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
737 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
740 * If this queue was previously inactive (idle) - we need to free it
743 if (using_inactive_queue) {
744 struct iwl_scd_txq_cfg_cmd cmd = {
746 .action = SCD_CFG_DISABLE_QUEUE,
750 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
752 spin_lock_bh(&mvm->queue_info_lock);
753 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
754 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
755 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[txq_curr_ac];
756 cmd.tid = mvm->queue_info[queue].txq_tid;
757 spin_unlock_bh(&mvm->queue_info_lock);
759 /* Disable the queue */
760 if (disable_agg_tids)
761 iwl_mvm_invalidate_sta_queue(mvm, queue,
762 disable_agg_tids, false);
763 iwl_trans_txq_disable(mvm->trans, queue, false);
764 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
768 "Failed to free inactive queue %d (ret=%d)\n",
771 /* Re-mark the inactive queue as inactive */
772 spin_lock_bh(&mvm->queue_info_lock);
773 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
774 spin_unlock_bh(&mvm->queue_info_lock);
779 /* If TXQ is allocated to another STA, update removal in FW */
780 if (cmd.sta_id != mvmsta->sta_id)
781 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
784 IWL_DEBUG_TX_QUEUES(mvm,
785 "Allocating %squeue #%d to sta %d on tid %d\n",
786 shared_queue ? "shared " : "", queue,
787 mvmsta->sta_id, tid);
790 /* Disable any open aggs on this queue */
791 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
793 if (disable_agg_tids) {
794 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
796 iwl_mvm_invalidate_sta_queue(mvm, queue,
797 disable_agg_tids, false);
801 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
802 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
806 * Mark queue as shared in transport if shared
807 * Note this has to be done after queue enablement because enablement
808 * can also set this value, and there is no indication there to shared
812 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
814 spin_lock_bh(&mvmsta->lock);
815 mvmsta->tid_data[tid].txq_id = queue;
816 mvmsta->tid_data[tid].is_tid_active = true;
817 mvmsta->tfd_queue_msk |= BIT(queue);
818 queue_state = mvmsta->tid_data[tid].state;
820 if (mvmsta->reserved_queue == queue)
821 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
822 spin_unlock_bh(&mvmsta->lock);
825 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
829 /* If we need to re-enable aggregations... */
830 if (queue_state == IWL_AGG_ON) {
831 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
836 /* Redirect queue, if needed */
837 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
846 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
851 static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
853 struct iwl_scd_txq_cfg_cmd cmd = {
855 .action = SCD_CFG_UPDATE_QUEUE_TID,
859 unsigned long tid_bitmap;
862 lockdep_assert_held(&mvm->mutex);
864 spin_lock_bh(&mvm->queue_info_lock);
865 sta_id = mvm->queue_info[queue].ra_sta_id;
866 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
867 spin_unlock_bh(&mvm->queue_info_lock);
869 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
872 /* Find any TID for queue */
873 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
875 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
877 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
879 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
882 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
886 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
888 struct ieee80211_sta *sta;
889 struct iwl_mvm_sta *mvmsta;
892 unsigned long tid_bitmap;
893 unsigned int wdg_timeout;
897 lockdep_assert_held(&mvm->mutex);
899 spin_lock_bh(&mvm->queue_info_lock);
900 sta_id = mvm->queue_info[queue].ra_sta_id;
901 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
902 spin_unlock_bh(&mvm->queue_info_lock);
904 /* Find TID for queue, and make sure it is the only one on the queue */
905 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
906 if (tid_bitmap != BIT(tid)) {
907 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
912 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
915 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
916 lockdep_is_held(&mvm->mutex));
918 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
921 mvmsta = iwl_mvm_sta_from_mac80211(sta);
922 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
924 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
926 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
927 tid_to_mac80211_ac[tid], ssn,
930 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
934 /* If aggs should be turned back on - do it */
935 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
936 struct iwl_mvm_add_sta_cmd cmd;
938 mvmsta->tid_disable_agg &= ~BIT(tid);
940 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
941 cmd.sta_id = mvmsta->sta_id;
942 cmd.add_modify = STA_MODE_MODIFY;
943 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
944 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
945 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
947 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
948 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
950 IWL_DEBUG_TX_QUEUES(mvm,
951 "TXQ #%d is now aggregated again\n",
954 /* Mark queue intenally as aggregating again */
955 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
959 spin_lock_bh(&mvm->queue_info_lock);
960 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
961 spin_unlock_bh(&mvm->queue_info_lock);
964 static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
966 if (tid == IWL_MAX_TID_COUNT)
967 return IEEE80211_AC_VO; /* MGMT */
969 return tid_to_mac80211_ac[tid];
972 static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
973 struct ieee80211_sta *sta, int tid)
975 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
976 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
978 struct ieee80211_hdr *hdr;
979 struct sk_buff_head deferred_tx;
981 bool no_queue = false; /* Marks if there is a problem with the queue */
984 lockdep_assert_held(&mvm->mutex);
986 skb = skb_peek(&tid_data->deferred_tx_frames);
989 hdr = (void *)skb->data;
991 ac = iwl_mvm_tid_to_ac_queue(tid);
992 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
994 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
995 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
997 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
998 mvmsta->sta_id, tid);
1001 * Mark queue as problematic so later the deferred traffic is
1002 * freed, as we can do nothing with it
1007 __skb_queue_head_init(&deferred_tx);
1009 /* Disable bottom-halves when entering TX path */
1011 spin_lock(&mvmsta->lock);
1012 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
1013 spin_unlock(&mvmsta->lock);
1015 while ((skb = __skb_dequeue(&deferred_tx)))
1016 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1017 ieee80211_free_txskb(mvm->hw, skb);
1021 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1024 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1026 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1028 struct ieee80211_sta *sta;
1029 struct iwl_mvm_sta *mvmsta;
1030 unsigned long deferred_tid_traffic;
1031 int queue, sta_id, tid;
1033 /* Check inactivity of queues */
1034 iwl_mvm_inactivity_check(mvm);
1036 mutex_lock(&mvm->mutex);
1038 /* Reconfigure queues requiring reconfiguation */
1039 for (queue = 0; queue < IWL_MAX_HW_QUEUES; queue++) {
1043 spin_lock_bh(&mvm->queue_info_lock);
1044 reconfig = (mvm->queue_info[queue].status ==
1045 IWL_MVM_QUEUE_RECONFIGURING);
1048 * We need to take into account a situation in which a TXQ was
1049 * allocated to TID x, and then turned shared by adding TIDs y
1050 * and z. If TID x becomes inactive and is removed from the TXQ,
1051 * ownership must be given to one of the remaining TIDs.
1052 * This is mainly because if TID x continues - a new queue can't
1053 * be allocated for it as long as it is an owner of another TXQ.
1055 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1056 BIT(mvm->queue_info[queue].txq_tid)) &&
1057 (mvm->queue_info[queue].status ==
1058 IWL_MVM_QUEUE_SHARED);
1059 spin_unlock_bh(&mvm->queue_info_lock);
1062 iwl_mvm_unshare_queue(mvm, queue);
1063 else if (change_owner)
1064 iwl_mvm_change_queue_owner(mvm, queue);
1067 /* Go over all stations with deferred traffic */
1068 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1069 IWL_MVM_STATION_COUNT) {
1070 clear_bit(sta_id, mvm->sta_deferred_frames);
1071 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1072 lockdep_is_held(&mvm->mutex));
1073 if (IS_ERR_OR_NULL(sta))
1076 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1077 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1079 for_each_set_bit(tid, &deferred_tid_traffic,
1080 IWL_MAX_TID_COUNT + 1)
1081 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1084 mutex_unlock(&mvm->mutex);
1087 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1088 struct ieee80211_sta *sta,
1089 enum nl80211_iftype vif_type)
1091 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1095 * Check for inactive queues, so we don't reach a situation where we
1096 * can't add a STA due to a shortage in queues that doesn't really exist
1098 iwl_mvm_inactivity_check(mvm);
1100 spin_lock_bh(&mvm->queue_info_lock);
1102 /* Make sure we have free resources for this STA */
1103 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1104 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
1105 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1106 IWL_MVM_QUEUE_FREE))
1107 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1109 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1110 IWL_MVM_DQA_MIN_DATA_QUEUE,
1111 IWL_MVM_DQA_MAX_DATA_QUEUE);
1113 spin_unlock_bh(&mvm->queue_info_lock);
1114 IWL_ERR(mvm, "No available queues for new station\n");
1117 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1119 spin_unlock_bh(&mvm->queue_info_lock);
1121 mvmsta->reserved_queue = queue;
1123 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1124 queue, mvmsta->sta_id);
1130 * In DQA mode, after a HW restart the queues should be allocated as before, in
1131 * order to avoid race conditions when there are shared queues. This function
1132 * does the re-mapping and queue allocation.
1134 * Note that re-enabling aggregations isn't done in this function.
1136 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1137 struct iwl_mvm_sta *mvm_sta)
1139 unsigned int wdg_timeout =
1140 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1142 struct iwl_trans_txq_scd_cfg cfg = {
1143 .sta_id = mvm_sta->sta_id,
1144 .frame_limit = IWL_FRAME_LIMIT,
1147 /* Make sure reserved queue is still marked as such (or allocated) */
1148 mvm->queue_info[mvm_sta->reserved_queue].status =
1149 IWL_MVM_QUEUE_RESERVED;
1151 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1152 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1153 int txq_id = tid_data->txq_id;
1157 if (txq_id == IEEE80211_INVAL_HW_QUEUE)
1160 skb_queue_head_init(&tid_data->deferred_tx_frames);
1162 ac = tid_to_mac80211_ac[i];
1163 mac_queue = mvm_sta->vif->hw_queue[ac];
1166 cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
1167 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1168 txq_id == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1170 IWL_DEBUG_TX_QUEUES(mvm,
1171 "Re-mapping sta %d tid %d to queue %d\n",
1172 mvm_sta->sta_id, i, txq_id);
1174 iwl_mvm_enable_txq(mvm, txq_id, mac_queue,
1175 IEEE80211_SEQ_TO_SN(tid_data->seq_number),
1178 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1181 atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
1184 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1185 struct ieee80211_vif *vif,
1186 struct ieee80211_sta *sta)
1188 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1189 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1190 struct iwl_mvm_rxq_dup_data *dup_data;
1193 lockdep_assert_held(&mvm->mutex);
1195 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1196 sta_id = iwl_mvm_find_free_sta_id(mvm,
1197 ieee80211_vif_type_p2p(vif));
1199 sta_id = mvm_sta->sta_id;
1201 if (sta_id == IWL_MVM_STATION_COUNT)
1204 spin_lock_init(&mvm_sta->lock);
1206 /* In DQA mode, if this is a HW restart, re-alloc existing queues */
1207 if (iwl_mvm_is_dqa_supported(mvm) &&
1208 test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1209 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1213 mvm_sta->sta_id = sta_id;
1214 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1217 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1218 mvm_sta->tx_protection = 0;
1219 mvm_sta->tt_tx_protection = false;
1221 /* HW restart, don't assume the memory has been zeroed */
1222 atomic_set(&mvm->pending_frames[sta_id], 0);
1223 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1224 mvm_sta->tfd_queue_msk = 0;
1227 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1228 * and then they'll be allocated dynamically
1230 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
1231 ret = iwl_mvm_tdls_sta_init(mvm, sta);
1234 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
1235 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1236 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1237 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1240 /* for HW restart - reset everything but the sequence number */
1241 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1242 u16 seq = mvm_sta->tid_data[i].seq_number;
1243 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1244 mvm_sta->tid_data[i].seq_number = seq;
1246 if (!iwl_mvm_is_dqa_supported(mvm))
1250 * Mark all queues for this STA as unallocated and defer TX
1251 * frames until the queue is allocated
1253 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1254 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
1256 mvm_sta->deferred_traffic_tid_map = 0;
1257 mvm_sta->agg_tids = 0;
1259 if (iwl_mvm_has_new_rx_api(mvm) &&
1260 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1261 dup_data = kcalloc(mvm->trans->num_rx_queues,
1266 mvm_sta->dup_data = dup_data;
1269 if (iwl_mvm_is_dqa_supported(mvm)) {
1270 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1271 ieee80211_vif_type_p2p(vif));
1277 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
1281 if (vif->type == NL80211_IFTYPE_STATION) {
1283 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
1284 mvmvif->ap_sta_id = sta_id;
1286 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
1290 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1295 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1296 iwl_mvm_tdls_sta_deinit(mvm, sta);
1300 int iwl_mvm_update_sta(struct iwl_mvm *mvm,
1301 struct ieee80211_vif *vif,
1302 struct ieee80211_sta *sta)
1304 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
1307 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1310 struct iwl_mvm_add_sta_cmd cmd = {};
1314 lockdep_assert_held(&mvm->mutex);
1316 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1317 cmd.sta_id = mvmsta->sta_id;
1318 cmd.add_modify = STA_MODE_MODIFY;
1319 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1320 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1322 status = ADD_STA_SUCCESS;
1323 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1324 iwl_mvm_add_sta_cmd_size(mvm),
1329 switch (status & IWL_ADD_STA_STATUS_MASK) {
1330 case ADD_STA_SUCCESS:
1331 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1336 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1345 * Remove a station from the FW table. Before sending the command to remove
1346 * the station validate that the station is indeed known to the driver (sanity
1349 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1351 struct ieee80211_sta *sta;
1352 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1357 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1358 lockdep_is_held(&mvm->mutex));
1360 /* Note: internal stations are marked as error values */
1362 IWL_ERR(mvm, "Invalid station id\n");
1366 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1367 sizeof(rm_sta_cmd), &rm_sta_cmd);
1369 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1376 void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1378 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1382 * The mutex is needed because of the SYNC cmd, but not only: if the
1383 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1384 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1385 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1388 mutex_lock(&mvm->mutex);
1390 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1392 struct ieee80211_sta *sta =
1393 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1394 lockdep_is_held(&mvm->mutex));
1397 * This station is in use or RCU-removed; the latter happens in
1398 * managed mode, where mac80211 removes the station before we
1399 * can remove it from firmware (we can only do that after the
1400 * MAC is marked unassociated), and possibly while the deauth
1401 * frame to disconnect from the AP is still queued. Then, the
1402 * station pointer is -ENOENT when the last skb is reclaimed.
1404 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
1407 if (PTR_ERR(sta) == -EINVAL) {
1408 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1414 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1419 WARN_ON(PTR_ERR(sta) != -EBUSY);
1420 /* This station was removed and we waited until it got drained,
1421 * we can now proceed and remove it.
1423 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1426 "Couldn't remove sta %d after it was drained\n",
1430 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1431 clear_bit(sta_id, mvm->sta_drained);
1433 if (mvm->tfd_drained[sta_id]) {
1434 unsigned long i, msk = mvm->tfd_drained[sta_id];
1436 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
1437 iwl_mvm_disable_txq(mvm, i, i,
1438 IWL_MAX_TID_COUNT, 0);
1440 mvm->tfd_drained[sta_id] = 0;
1441 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1446 mutex_unlock(&mvm->mutex);
1449 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1450 struct ieee80211_vif *vif,
1451 struct iwl_mvm_sta *mvm_sta)
1456 lockdep_assert_held(&mvm->mutex);
1458 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1459 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
1462 ac = iwl_mvm_tid_to_ac_queue(i);
1463 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1464 vif->hw_queue[ac], i, 0);
1465 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1469 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1470 struct ieee80211_vif *vif,
1471 struct ieee80211_sta *sta)
1473 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1474 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1477 lockdep_assert_held(&mvm->mutex);
1479 if (iwl_mvm_has_new_rx_api(mvm))
1480 kfree(mvm_sta->dup_data);
1482 if ((vif->type == NL80211_IFTYPE_STATION &&
1483 mvmvif->ap_sta_id == mvm_sta->sta_id) ||
1484 iwl_mvm_is_dqa_supported(mvm)){
1485 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1488 /* flush its queues here since we are freeing mvm_sta */
1489 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
1492 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1493 mvm_sta->tfd_queue_msk);
1496 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1498 /* If DQA is supported - the queues can be disabled now */
1499 if (iwl_mvm_is_dqa_supported(mvm)) {
1500 u8 reserved_txq = mvm_sta->reserved_queue;
1501 enum iwl_mvm_queue_status *status;
1503 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
1506 * If no traffic has gone through the reserved TXQ - it
1507 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1508 * should be manually marked as free again
1510 spin_lock_bh(&mvm->queue_info_lock);
1511 status = &mvm->queue_info[reserved_txq].status;
1512 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1513 (*status != IWL_MVM_QUEUE_FREE),
1514 "sta_id %d reserved txq %d status %d",
1515 mvm_sta->sta_id, reserved_txq, *status)) {
1516 spin_unlock_bh(&mvm->queue_info_lock);
1520 *status = IWL_MVM_QUEUE_FREE;
1521 spin_unlock_bh(&mvm->queue_info_lock);
1524 if (vif->type == NL80211_IFTYPE_STATION &&
1525 mvmvif->ap_sta_id == mvm_sta->sta_id) {
1526 /* if associated - we can't remove the AP STA now */
1527 if (vif->bss_conf.assoc)
1530 /* unassoc - go ahead - remove the AP STA now */
1531 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1533 /* clear d0i3_ap_sta_id if no longer relevant */
1534 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
1535 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1540 * This shouldn't happen - the TDLS channel switch should be canceled
1541 * before the STA is removed.
1543 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
1544 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1545 cancel_delayed_work(&mvm->tdls_cs.dwork);
1549 * Make sure that the tx response code sees the station as -EBUSY and
1550 * calls the drain worker.
1552 spin_lock_bh(&mvm_sta->lock);
1554 * There are frames pending on the AC queues for this station.
1555 * We need to wait until all the frames are drained...
1557 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
1558 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1560 spin_unlock_bh(&mvm_sta->lock);
1562 /* disable TDLS sta queues on drain complete */
1564 mvm->tfd_drained[mvm_sta->sta_id] =
1565 mvm_sta->tfd_queue_msk;
1566 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
1570 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1572 spin_unlock_bh(&mvm_sta->lock);
1574 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1575 iwl_mvm_tdls_sta_deinit(mvm, sta);
1577 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1578 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1584 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1585 struct ieee80211_vif *vif,
1588 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1590 lockdep_assert_held(&mvm->mutex);
1592 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1596 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1597 struct iwl_mvm_int_sta *sta,
1598 u32 qmask, enum nl80211_iftype iftype)
1600 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1601 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1602 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1606 sta->tfd_queue_msk = qmask;
1608 /* put a non-NULL value so iterating over the stations won't stop */
1609 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1613 static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1614 struct iwl_mvm_int_sta *sta)
1616 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1617 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1618 sta->sta_id = IWL_MVM_STATION_COUNT;
1621 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1622 struct iwl_mvm_int_sta *sta,
1624 u16 mac_id, u16 color)
1626 struct iwl_mvm_add_sta_cmd cmd;
1630 lockdep_assert_held(&mvm->mutex);
1632 memset(&cmd, 0, sizeof(cmd));
1633 cmd.sta_id = sta->sta_id;
1634 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1637 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1638 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1641 memcpy(cmd.addr, addr, ETH_ALEN);
1643 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1644 iwl_mvm_add_sta_cmd_size(mvm),
1649 switch (status & IWL_ADD_STA_STATUS_MASK) {
1650 case ADD_STA_SUCCESS:
1651 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1655 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1662 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1664 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1665 mvm->cfg->base_params->wd_timeout :
1666 IWL_WATCHDOG_DISABLED;
1669 lockdep_assert_held(&mvm->mutex);
1671 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1672 if (!iwl_mvm_is_dqa_supported(mvm))
1673 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1674 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
1676 /* Allocate aux station and assign to it the aux queue */
1677 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1678 NL80211_IFTYPE_UNSPECIFIED);
1682 if (iwl_mvm_is_dqa_supported(mvm)) {
1683 struct iwl_trans_txq_scd_cfg cfg = {
1684 .fifo = IWL_MVM_TX_FIFO_MCAST,
1685 .sta_id = mvm->aux_sta.sta_id,
1686 .tid = IWL_MAX_TID_COUNT,
1688 .frame_limit = IWL_FRAME_LIMIT,
1691 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1695 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1699 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1703 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1705 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1707 lockdep_assert_held(&mvm->mutex);
1708 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1712 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1716 lockdep_assert_held(&mvm->mutex);
1718 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1720 IWL_WARN(mvm, "Failed sending remove station\n");
1725 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1727 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1730 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1732 lockdep_assert_held(&mvm->mutex);
1734 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1738 * Send the add station command for the vif's broadcast station.
1739 * Assumes that the station was already allocated.
1741 * @mvm: the mvm component
1742 * @vif: the interface to which the broadcast station is added
1743 * @bsta: the broadcast station to add.
1745 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1747 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1748 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1749 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1750 const u8 *baddr = _baddr;
1752 lockdep_assert_held(&mvm->mutex);
1754 if (iwl_mvm_is_dqa_supported(mvm)) {
1755 struct iwl_trans_txq_scd_cfg cfg = {
1756 .fifo = IWL_MVM_TX_FIFO_VO,
1757 .sta_id = mvmvif->bcast_sta.sta_id,
1758 .tid = IWL_MAX_TID_COUNT,
1760 .frame_limit = IWL_FRAME_LIMIT,
1762 unsigned int wdg_timeout =
1763 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1766 if ((vif->type == NL80211_IFTYPE_AP) &&
1767 (mvmvif->bcast_sta.tfd_queue_msk &
1768 BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1769 queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
1770 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1771 (mvmvif->bcast_sta.tfd_queue_msk &
1772 BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1773 queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1774 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1777 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1781 if (vif->type == NL80211_IFTYPE_ADHOC)
1782 baddr = vif->bss_conf.bssid;
1784 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1787 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1788 mvmvif->id, mvmvif->color);
1791 /* Send the FW a request to remove the station from it's internal data
1792 * structures, but DO NOT remove the entry from the local data structures. */
1793 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1795 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1798 lockdep_assert_held(&mvm->mutex);
1800 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
1802 IWL_WARN(mvm, "Failed sending remove station\n");
1806 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1808 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1811 lockdep_assert_held(&mvm->mutex);
1813 if (!iwl_mvm_is_dqa_supported(mvm))
1814 qmask = iwl_mvm_mac_get_queues_mask(vif);
1816 if (vif->type == NL80211_IFTYPE_AP) {
1818 * The firmware defines the TFD queue mask to only be relevant
1819 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1822 qmask &= ~BIT(vif->cab_queue);
1824 if (iwl_mvm_is_dqa_supported(mvm))
1825 qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
1826 } else if (iwl_mvm_is_dqa_supported(mvm) &&
1827 vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1828 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
1831 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1832 ieee80211_vif_type_p2p(vif));
1835 /* Allocate a new station entry for the broadcast station to the given vif,
1836 * and send it to the FW.
1837 * Note that each P2P mac should have its own broadcast station.
1839 * @mvm: the mvm component
1840 * @vif: the interface to which the broadcast station is added
1841 * @bsta: the broadcast station to add. */
1842 int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1844 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1845 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1848 lockdep_assert_held(&mvm->mutex);
1850 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1854 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
1857 iwl_mvm_dealloc_int_sta(mvm, bsta);
1862 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1864 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1866 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1870 * Send the FW a request to remove the station from it's internal data
1871 * structures, and in addition remove it from the local data structure.
1873 int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1877 lockdep_assert_held(&mvm->mutex);
1879 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
1881 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1886 #define IWL_MAX_RX_BA_SESSIONS 16
1888 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
1890 struct iwl_mvm_delba_notif notif = {
1891 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1895 iwl_mvm_sync_rx_queues_internal(mvm, (void *)¬if, sizeof(notif));
1898 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1899 struct iwl_mvm_baid_data *data)
1903 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1905 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1907 struct iwl_mvm_reorder_buffer *reorder_buf =
1908 &data->reorder_buf[i];
1910 spin_lock_bh(&reorder_buf->lock);
1911 if (likely(!reorder_buf->num_stored)) {
1912 spin_unlock_bh(&reorder_buf->lock);
1917 * This shouldn't happen in regular DELBA since the internal
1918 * delBA notification should trigger a release of all frames in
1919 * the reorder buffer.
1923 for (j = 0; j < reorder_buf->buf_size; j++)
1924 __skb_queue_purge(&reorder_buf->entries[j]);
1926 * Prevent timer re-arm. This prevents a very far fetched case
1927 * where we timed out on the notification. There may be prior
1928 * RX frames pending in the RX queue before the notification
1929 * that might get processed between now and the actual deletion
1930 * and we would re-arm the timer although we are deleting the
1933 reorder_buf->removed = true;
1934 spin_unlock_bh(&reorder_buf->lock);
1935 del_timer_sync(&reorder_buf->reorder_timer);
1939 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1941 struct iwl_mvm_baid_data *data,
1942 u16 ssn, u8 buf_size)
1946 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1947 struct iwl_mvm_reorder_buffer *reorder_buf =
1948 &data->reorder_buf[i];
1951 reorder_buf->num_stored = 0;
1952 reorder_buf->head_sn = ssn;
1953 reorder_buf->buf_size = buf_size;
1954 /* rx reorder timer */
1955 reorder_buf->reorder_timer.function =
1956 iwl_mvm_reorder_timer_expired;
1957 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
1958 init_timer(&reorder_buf->reorder_timer);
1959 spin_lock_init(&reorder_buf->lock);
1960 reorder_buf->mvm = mvm;
1961 reorder_buf->queue = i;
1962 reorder_buf->sta_id = sta_id;
1963 for (j = 0; j < reorder_buf->buf_size; j++)
1964 __skb_queue_head_init(&reorder_buf->entries[j]);
1968 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1969 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
1971 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1972 struct iwl_mvm_add_sta_cmd cmd = {};
1973 struct iwl_mvm_baid_data *baid_data = NULL;
1977 lockdep_assert_held(&mvm->mutex);
1979 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1980 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1984 if (iwl_mvm_has_new_rx_api(mvm) && start) {
1986 * Allocate here so if allocation fails we can bail out early
1987 * before starting the BA session in the firmware
1989 baid_data = kzalloc(sizeof(*baid_data) +
1990 mvm->trans->num_rx_queues *
1991 sizeof(baid_data->reorder_buf[0]),
1997 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1998 cmd.sta_id = mvm_sta->sta_id;
1999 cmd.add_modify = STA_MODE_MODIFY;
2001 cmd.add_immediate_ba_tid = (u8) tid;
2002 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2003 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
2005 cmd.remove_immediate_ba_tid = (u8) tid;
2007 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2008 STA_MODIFY_REMOVE_BA_TID;
2010 status = ADD_STA_SUCCESS;
2011 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2012 iwl_mvm_add_sta_cmd_size(mvm),
2017 switch (status & IWL_ADD_STA_STATUS_MASK) {
2018 case ADD_STA_SUCCESS:
2019 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2020 start ? "start" : "stopp");
2022 case ADD_STA_IMMEDIATE_BA_FAILURE:
2023 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2028 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2029 start ? "start" : "stopp", status);
2039 mvm->rx_ba_sessions++;
2041 if (!iwl_mvm_has_new_rx_api(mvm))
2044 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2048 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2049 IWL_ADD_STA_BAID_SHIFT);
2050 baid_data->baid = baid;
2051 baid_data->timeout = timeout;
2052 baid_data->last_rx = jiffies;
2053 setup_timer(&baid_data->session_timer,
2054 iwl_mvm_rx_agg_session_expired,
2055 (unsigned long)&mvm->baid_map[baid]);
2056 baid_data->mvm = mvm;
2057 baid_data->tid = tid;
2058 baid_data->sta_id = mvm_sta->sta_id;
2060 mvm_sta->tid_to_baid[tid] = baid;
2062 mod_timer(&baid_data->session_timer,
2063 TU_TO_EXP_TIME(timeout * 2));
2065 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2066 baid_data, ssn, buf_size);
2068 * protect the BA data with RCU to cover a case where our
2069 * internal RX sync mechanism will timeout (not that it's
2070 * supposed to happen) and we will free the session data while
2071 * RX is being processed in parallel
2073 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2074 mvm_sta->sta_id, tid, baid);
2075 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2076 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2078 u8 baid = mvm_sta->tid_to_baid[tid];
2080 if (mvm->rx_ba_sessions > 0)
2081 /* check that restart flow didn't zero the counter */
2082 mvm->rx_ba_sessions--;
2083 if (!iwl_mvm_has_new_rx_api(mvm))
2086 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2089 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2090 if (WARN_ON(!baid_data))
2093 /* synchronize all rx queues so we can safely delete */
2094 iwl_mvm_free_reorder(mvm, baid_data);
2095 del_timer_sync(&baid_data->session_timer);
2096 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2097 kfree_rcu(baid_data, rcu_head);
2098 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2107 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2108 int tid, u8 queue, bool start)
2110 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2111 struct iwl_mvm_add_sta_cmd cmd = {};
2115 lockdep_assert_held(&mvm->mutex);
2118 mvm_sta->tfd_queue_msk |= BIT(queue);
2119 mvm_sta->tid_disable_agg &= ~BIT(tid);
2121 /* In DQA-mode the queue isn't removed on agg termination */
2122 if (!iwl_mvm_is_dqa_supported(mvm))
2123 mvm_sta->tfd_queue_msk &= ~BIT(queue);
2124 mvm_sta->tid_disable_agg |= BIT(tid);
2127 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2128 cmd.sta_id = mvm_sta->sta_id;
2129 cmd.add_modify = STA_MODE_MODIFY;
2130 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
2131 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2132 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2134 status = ADD_STA_SUCCESS;
2135 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2136 iwl_mvm_add_sta_cmd_size(mvm),
2141 switch (status & IWL_ADD_STA_STATUS_MASK) {
2142 case ADD_STA_SUCCESS:
2146 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2147 start ? "start" : "stopp", status);
2154 const u8 tid_to_mac80211_ac[] = {
2163 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2166 static const u8 tid_to_ucode_ac[] = {
2177 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2178 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2180 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2181 struct iwl_mvm_tid_data *tid_data;
2185 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2188 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2189 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2190 mvmsta->tid_data[tid].state);
2194 lockdep_assert_held(&mvm->mutex);
2196 spin_lock_bh(&mvmsta->lock);
2198 /* possible race condition - we entered D0i3 while starting agg */
2199 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2200 spin_unlock_bh(&mvmsta->lock);
2201 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2205 spin_lock(&mvm->queue_info_lock);
2208 * Note the possible cases:
2209 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2210 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2211 * one and mark it as reserved
2212 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2213 * non-DQA mode, since the TXQ hasn't yet been allocated
2215 txq_id = mvmsta->tid_data[tid].txq_id;
2216 if (iwl_mvm_is_dqa_supported(mvm) &&
2217 unlikely(mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_SHARED)) {
2219 IWL_DEBUG_TX_QUEUES(mvm,
2220 "Can't start tid %d agg on shared queue!\n",
2223 } else if (!iwl_mvm_is_dqa_supported(mvm) ||
2224 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
2225 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2226 mvm->first_agg_queue,
2227 mvm->last_agg_queue);
2230 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2234 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2235 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2238 spin_unlock(&mvm->queue_info_lock);
2240 IWL_DEBUG_TX_QUEUES(mvm,
2241 "AGG for tid %d will be on queue #%d\n",
2244 tid_data = &mvmsta->tid_data[tid];
2245 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2246 tid_data->txq_id = txq_id;
2247 *ssn = tid_data->ssn;
2249 IWL_DEBUG_TX_QUEUES(mvm,
2250 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2251 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2252 tid_data->next_reclaimed);
2254 if (tid_data->ssn == tid_data->next_reclaimed) {
2255 tid_data->state = IWL_AGG_STARTING;
2256 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2258 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2265 spin_unlock(&mvm->queue_info_lock);
2267 spin_unlock_bh(&mvmsta->lock);
2272 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2273 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2276 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2277 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2278 unsigned int wdg_timeout =
2279 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2281 bool alloc_queue = true;
2282 enum iwl_mvm_queue_status queue_status;
2285 struct iwl_trans_txq_scd_cfg cfg = {
2286 .sta_id = mvmsta->sta_id,
2288 .frame_limit = buf_size,
2292 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2293 != IWL_MAX_TID_COUNT);
2295 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2297 spin_lock_bh(&mvmsta->lock);
2298 ssn = tid_data->ssn;
2299 queue = tid_data->txq_id;
2300 tid_data->state = IWL_AGG_ON;
2301 mvmsta->agg_tids |= BIT(tid);
2302 tid_data->ssn = 0xffff;
2303 tid_data->amsdu_in_ampdu_allowed = amsdu;
2304 spin_unlock_bh(&mvmsta->lock);
2306 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2308 spin_lock_bh(&mvm->queue_info_lock);
2309 queue_status = mvm->queue_info[queue].status;
2310 spin_unlock_bh(&mvm->queue_info_lock);
2312 /* In DQA mode, the existing queue might need to be reconfigured */
2313 if (iwl_mvm_is_dqa_supported(mvm)) {
2314 /* Maybe there is no need to even alloc a queue... */
2315 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2316 alloc_queue = false;
2319 * Only reconfig the SCD for the queue if the window size has
2320 * changed from current (become smaller)
2322 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2324 * If reconfiguring an existing queue, it first must be
2327 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
2331 "Error draining queue before reconfig\n");
2335 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2336 mvmsta->sta_id, tid,
2340 "Error reconfiguring TXQ #%d\n", queue);
2347 iwl_mvm_enable_txq(mvm, queue,
2348 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2351 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2352 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2353 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2358 /* No need to mark as reserved */
2359 spin_lock_bh(&mvm->queue_info_lock);
2360 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2361 spin_unlock_bh(&mvm->queue_info_lock);
2364 * Even though in theory the peer could have different
2365 * aggregation reorder buffer sizes for different sessions,
2366 * our ucode doesn't allow for that and has a global limit
2367 * for each station. Therefore, use the minimum of all the
2368 * aggregation sessions and our default value.
2370 mvmsta->max_agg_bufsize =
2371 min(mvmsta->max_agg_bufsize, buf_size);
2372 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2374 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2377 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
2380 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2381 struct ieee80211_sta *sta, u16 tid)
2383 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2384 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2389 * If mac80211 is cleaning its state, then say that we finished since
2390 * our state has been cleared anyway.
2392 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2393 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2397 spin_lock_bh(&mvmsta->lock);
2399 txq_id = tid_data->txq_id;
2401 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2402 mvmsta->sta_id, tid, txq_id, tid_data->state);
2404 mvmsta->agg_tids &= ~BIT(tid);
2406 spin_lock_bh(&mvm->queue_info_lock);
2408 * The TXQ is marked as reserved only if no traffic came through yet
2409 * This means no traffic has been sent on this TID (agg'd or not), so
2410 * we no longer have use for the queue. Since it hasn't even been
2411 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2414 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2415 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2417 spin_unlock_bh(&mvm->queue_info_lock);
2419 switch (tid_data->state) {
2421 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2423 IWL_DEBUG_TX_QUEUES(mvm,
2424 "ssn = %d, next_recl = %d\n",
2425 tid_data->ssn, tid_data->next_reclaimed);
2427 /* There are still packets for this RA / TID in the HW */
2428 if (tid_data->ssn != tid_data->next_reclaimed) {
2429 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2434 tid_data->ssn = 0xffff;
2435 tid_data->state = IWL_AGG_OFF;
2436 spin_unlock_bh(&mvmsta->lock);
2438 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2440 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2442 if (!iwl_mvm_is_dqa_supported(mvm)) {
2443 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2445 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2448 case IWL_AGG_STARTING:
2449 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2451 * The agg session has been stopped before it was set up. This
2452 * can happen when the AddBA timer times out for example.
2455 /* No barriers since we are under mutex */
2456 lockdep_assert_held(&mvm->mutex);
2458 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2459 tid_data->state = IWL_AGG_OFF;
2464 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2465 mvmsta->sta_id, tid, tid_data->state);
2467 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2471 spin_unlock_bh(&mvmsta->lock);
2476 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2477 struct ieee80211_sta *sta, u16 tid)
2479 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2480 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2482 enum iwl_mvm_agg_state old_state;
2485 * First set the agg state to OFF to avoid calling
2486 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2488 spin_lock_bh(&mvmsta->lock);
2489 txq_id = tid_data->txq_id;
2490 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2491 mvmsta->sta_id, tid, txq_id, tid_data->state);
2492 old_state = tid_data->state;
2493 tid_data->state = IWL_AGG_OFF;
2494 mvmsta->agg_tids &= ~BIT(tid);
2495 spin_unlock_bh(&mvmsta->lock);
2497 spin_lock_bh(&mvm->queue_info_lock);
2499 * The TXQ is marked as reserved only if no traffic came through yet
2500 * This means no traffic has been sent on this TID (agg'd or not), so
2501 * we no longer have use for the queue. Since it hasn't even been
2502 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2505 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2506 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2507 spin_unlock_bh(&mvm->queue_info_lock);
2509 if (old_state >= IWL_AGG_ON) {
2510 iwl_mvm_drain_sta(mvm, mvmsta, true);
2511 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2512 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
2513 iwl_trans_wait_tx_queue_empty(mvm->trans,
2514 mvmsta->tfd_queue_msk);
2515 iwl_mvm_drain_sta(mvm, mvmsta, false);
2517 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2519 if (!iwl_mvm_is_dqa_supported(mvm)) {
2520 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2522 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2530 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2532 int i, max = -1, max_offs = -1;
2534 lockdep_assert_held(&mvm->mutex);
2536 /* Pick the unused key offset with the highest 'deleted'
2537 * counter. Every time a key is deleted, all the counters
2538 * are incremented and the one that was just deleted is
2539 * reset to zero. Thus, the highest counter is the one
2540 * that was deleted longest ago. Pick that one.
2542 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2543 if (test_bit(i, mvm->fw_key_table))
2545 if (mvm->fw_key_deleted[i] > max) {
2546 max = mvm->fw_key_deleted[i];
2552 return STA_KEY_IDX_INVALID;
2557 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2558 struct ieee80211_vif *vif,
2559 struct ieee80211_sta *sta)
2561 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2564 return iwl_mvm_sta_from_mac80211(sta);
2567 * The device expects GTKs for station interfaces to be
2568 * installed as GTKs for the AP station. If we have no
2569 * station ID, then use AP's station ID.
2571 if (vif->type == NL80211_IFTYPE_STATION &&
2572 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2573 u8 sta_id = mvmvif->ap_sta_id;
2575 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2576 lockdep_is_held(&mvm->mutex));
2579 * It is possible that the 'sta' parameter is NULL,
2580 * for example when a GTK is removed - the sta_id will then
2581 * be the AP ID, and no station was passed by mac80211.
2583 if (IS_ERR_OR_NULL(sta))
2586 return iwl_mvm_sta_from_mac80211(sta);
2592 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2593 struct iwl_mvm_sta *mvm_sta,
2594 struct ieee80211_key_conf *keyconf, bool mcast,
2595 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2598 struct iwl_mvm_add_sta_key_cmd cmd = {};
2604 u8 sta_id = mvm_sta->sta_id;
2606 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2607 STA_KEY_FLG_KEYID_MSK;
2608 key_flags = cpu_to_le16(keyidx);
2609 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2611 switch (keyconf->cipher) {
2612 case WLAN_CIPHER_SUITE_TKIP:
2613 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
2614 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
2615 for (i = 0; i < 5; i++)
2616 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2617 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2619 case WLAN_CIPHER_SUITE_CCMP:
2620 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
2621 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2623 case WLAN_CIPHER_SUITE_WEP104:
2624 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
2626 case WLAN_CIPHER_SUITE_WEP40:
2627 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2628 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2630 case WLAN_CIPHER_SUITE_GCMP_256:
2631 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2633 case WLAN_CIPHER_SUITE_GCMP:
2634 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
2635 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2638 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2639 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2643 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2645 cmd.key_offset = key_offset;
2646 cmd.key_flags = key_flags;
2647 cmd.sta_id = sta_id;
2649 status = ADD_STA_SUCCESS;
2650 if (cmd_flags & CMD_ASYNC)
2651 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2654 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2658 case ADD_STA_SUCCESS:
2659 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2663 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2670 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2671 struct ieee80211_key_conf *keyconf,
2672 u8 sta_id, bool remove_key)
2674 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2676 /* verify the key details match the required command's expectations */
2677 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2678 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
2679 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2680 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
2681 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
2684 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
2685 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
2688 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2689 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2692 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2694 struct ieee80211_key_seq seq;
2697 switch (keyconf->cipher) {
2698 case WLAN_CIPHER_SUITE_AES_CMAC:
2699 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
2701 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2702 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2703 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
2709 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
2710 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2711 igtk_cmd.ctrl_flags |=
2712 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
2713 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2714 pn = seq.aes_cmac.pn;
2715 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2716 ((u64) pn[4] << 8) |
2717 ((u64) pn[3] << 16) |
2718 ((u64) pn[2] << 24) |
2719 ((u64) pn[1] << 32) |
2720 ((u64) pn[0] << 40));
2723 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2724 remove_key ? "removing" : "installing",
2727 if (!iwl_mvm_has_new_rx_api(mvm)) {
2728 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
2729 .ctrl_flags = igtk_cmd.ctrl_flags,
2730 .key_id = igtk_cmd.key_id,
2731 .sta_id = igtk_cmd.sta_id,
2732 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
2735 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
2736 ARRAY_SIZE(igtk_cmd_v1.igtk));
2737 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2738 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
2740 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2741 sizeof(igtk_cmd), &igtk_cmd);
2745 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2746 struct ieee80211_vif *vif,
2747 struct ieee80211_sta *sta)
2749 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2754 if (vif->type == NL80211_IFTYPE_STATION &&
2755 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2756 u8 sta_id = mvmvif->ap_sta_id;
2757 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2758 lockdep_is_held(&mvm->mutex));
2766 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2767 struct ieee80211_vif *vif,
2768 struct ieee80211_sta *sta,
2769 struct ieee80211_key_conf *keyconf,
2773 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2776 struct ieee80211_key_seq seq;
2779 switch (keyconf->cipher) {
2780 case WLAN_CIPHER_SUITE_TKIP:
2781 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2782 /* get phase 1 key from mac80211 */
2783 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2784 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
2785 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
2786 seq.tkip.iv32, p1k, 0, key_offset);
2788 case WLAN_CIPHER_SUITE_CCMP:
2789 case WLAN_CIPHER_SUITE_WEP40:
2790 case WLAN_CIPHER_SUITE_WEP104:
2791 case WLAN_CIPHER_SUITE_GCMP:
2792 case WLAN_CIPHER_SUITE_GCMP_256:
2793 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
2794 0, NULL, 0, key_offset);
2797 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
2798 0, NULL, 0, key_offset);
2804 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2805 struct ieee80211_key_conf *keyconf,
2808 struct iwl_mvm_add_sta_key_cmd cmd = {};
2813 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2814 STA_KEY_FLG_KEYID_MSK);
2815 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2816 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2819 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2821 cmd.key_flags = key_flags;
2822 cmd.key_offset = keyconf->hw_key_idx;
2823 cmd.sta_id = sta_id;
2825 status = ADD_STA_SUCCESS;
2826 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2830 case ADD_STA_SUCCESS:
2831 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2835 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2842 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2843 struct ieee80211_vif *vif,
2844 struct ieee80211_sta *sta,
2845 struct ieee80211_key_conf *keyconf,
2848 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
2849 struct iwl_mvm_sta *mvm_sta;
2852 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
2854 lockdep_assert_held(&mvm->mutex);
2856 /* Get the station id from the mvm local station table */
2857 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2859 IWL_ERR(mvm, "Failed to find station\n");
2862 sta_id = mvm_sta->sta_id;
2864 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2865 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2866 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
2867 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2872 * It is possible that the 'sta' parameter is NULL, and thus
2873 * there is a need to retrieve the sta from the local station table.
2876 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2877 lockdep_is_held(&mvm->mutex));
2878 if (IS_ERR_OR_NULL(sta)) {
2879 IWL_ERR(mvm, "Invalid station id\n");
2884 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2887 /* If the key_offset is not pre-assigned, we need to find a
2888 * new offset to use. In normal cases, the offset is not
2889 * pre-assigned, but during HW_RESTART we want to reuse the
2890 * same indices, so we pass them when this function is called.
2892 * In D3 entry, we need to hardcoded the indices (because the
2893 * firmware hardcodes the PTK offset to 0). In this case, we
2894 * need to make sure we don't overwrite the hw_key_idx in the
2895 * keyconf structure, because otherwise we cannot configure
2896 * the original ones back when resuming.
2898 if (key_offset == STA_KEY_IDX_INVALID) {
2899 key_offset = iwl_mvm_set_fw_key_idx(mvm);
2900 if (key_offset == STA_KEY_IDX_INVALID)
2902 keyconf->hw_key_idx = key_offset;
2905 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
2910 * For WEP, the same key is used for multicast and unicast. Upload it
2911 * again, using the same key offset, and now pointing the other one
2912 * to the same key slot (offset).
2913 * If this fails, remove the original as well.
2915 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2916 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
2917 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
2918 key_offset, !mcast);
2920 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2925 __set_bit(key_offset, mvm->fw_key_table);
2928 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2929 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
2930 sta ? sta->addr : zero_addr, ret);
2934 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2935 struct ieee80211_vif *vif,
2936 struct ieee80211_sta *sta,
2937 struct ieee80211_key_conf *keyconf)
2939 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
2940 struct iwl_mvm_sta *mvm_sta;
2941 u8 sta_id = IWL_MVM_STATION_COUNT;
2944 lockdep_assert_held(&mvm->mutex);
2946 /* Get the station from the mvm local station table */
2947 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2949 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2950 keyconf->keyidx, sta_id);
2952 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2953 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2954 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2955 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2957 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2958 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2959 keyconf->hw_key_idx);
2963 /* track which key was deleted last */
2964 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2965 if (mvm->fw_key_deleted[i] < U8_MAX)
2966 mvm->fw_key_deleted[i]++;
2968 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2971 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2975 sta_id = mvm_sta->sta_id;
2977 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2981 /* delete WEP key twice to get rid of (now useless) offset */
2982 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2983 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2984 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2989 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2990 struct ieee80211_vif *vif,
2991 struct ieee80211_key_conf *keyconf,
2992 struct ieee80211_sta *sta, u32 iv32,
2995 struct iwl_mvm_sta *mvm_sta;
2996 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3000 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3001 if (WARN_ON_ONCE(!mvm_sta))
3003 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
3004 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
3010 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3011 struct ieee80211_sta *sta)
3013 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3014 struct iwl_mvm_add_sta_cmd cmd = {
3015 .add_modify = STA_MODE_MODIFY,
3016 .sta_id = mvmsta->sta_id,
3017 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3018 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3022 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3023 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3025 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3028 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3029 struct ieee80211_sta *sta,
3030 enum ieee80211_frame_release_type reason,
3031 u16 cnt, u16 tids, bool more_data,
3034 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3035 struct iwl_mvm_add_sta_cmd cmd = {
3036 .add_modify = STA_MODE_MODIFY,
3037 .sta_id = mvmsta->sta_id,
3038 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3039 .sleep_tx_count = cpu_to_le16(cnt),
3040 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3043 unsigned long _tids = tids;
3045 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3046 * Note that this field is reserved and unused by firmware not
3047 * supporting GO uAPSD, so it's safe to always do this.
3049 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3050 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3052 /* If we're releasing frames from aggregation queues then check if the
3053 * all queues combined that we're releasing frames from have
3054 * - more frames than the service period, in which case more_data
3056 * - fewer than 'cnt' frames, in which case we need to adjust the
3057 * firmware command (but do that unconditionally)
3060 int remaining = cnt;
3063 spin_lock_bh(&mvmsta->lock);
3064 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3065 struct iwl_mvm_tid_data *tid_data;
3068 tid_data = &mvmsta->tid_data[tid];
3069 if (WARN(tid_data->state != IWL_AGG_ON &&
3070 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3071 "TID %d state is %d\n",
3072 tid, tid_data->state)) {
3073 spin_unlock_bh(&mvmsta->lock);
3074 ieee80211_sta_eosp(sta);
3078 n_queued = iwl_mvm_tid_queued(tid_data);
3079 if (n_queued > remaining) {
3084 remaining -= n_queued;
3086 sleep_tx_count = cnt - remaining;
3087 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3088 mvmsta->sleep_tx_count = sleep_tx_count;
3089 spin_unlock_bh(&mvmsta->lock);
3091 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3092 if (WARN_ON(cnt - remaining == 0)) {
3093 ieee80211_sta_eosp(sta);
3098 /* Note: this is ignored by firmware not supporting GO uAPSD */
3100 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
3102 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3103 mvmsta->next_status_eosp = true;
3104 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
3106 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
3109 /* block the Tx queues until the FW updated the sleep Tx count */
3110 iwl_trans_block_txq_ptrs(mvm->trans, true);
3112 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3113 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3114 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3116 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3119 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3120 struct iwl_rx_cmd_buffer *rxb)
3122 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3123 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3124 struct ieee80211_sta *sta;
3125 u32 sta_id = le32_to_cpu(notif->sta_id);
3127 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3131 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3132 if (!IS_ERR_OR_NULL(sta))
3133 ieee80211_sta_eosp(sta);
3137 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3138 struct iwl_mvm_sta *mvmsta, bool disable)
3140 struct iwl_mvm_add_sta_cmd cmd = {
3141 .add_modify = STA_MODE_MODIFY,
3142 .sta_id = mvmsta->sta_id,
3143 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3144 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3145 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3149 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3150 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3152 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3155 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3156 struct ieee80211_sta *sta,
3159 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3161 spin_lock_bh(&mvm_sta->lock);
3163 if (mvm_sta->disable_tx == disable) {
3164 spin_unlock_bh(&mvm_sta->lock);
3168 mvm_sta->disable_tx = disable;
3171 * Tell mac80211 to start/stop queuing tx for this station,
3172 * but don't stop queuing if there are still pending frames
3175 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3176 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3178 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3180 spin_unlock_bh(&mvm_sta->lock);
3183 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3184 struct iwl_mvm_vif *mvmvif,
3187 struct ieee80211_sta *sta;
3188 struct iwl_mvm_sta *mvm_sta;
3191 lockdep_assert_held(&mvm->mutex);
3193 /* Block/unblock all the stations of the given mvmvif */
3194 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
3195 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3196 lockdep_is_held(&mvm->mutex));
3197 if (IS_ERR_OR_NULL(sta))
3200 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3201 if (mvm_sta->mac_id_n_color !=
3202 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3205 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3209 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3211 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3212 struct iwl_mvm_sta *mvmsta;
3216 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3218 if (!WARN_ON(!mvmsta))
3219 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);