Merge tag 'for-airlie-tda998x' of git://git.armlinux.org.uk/~rmk/linux-arm into drm...
[sfrench/cifs-2.6.git] / drivers / net / wireless / intel / iwlwifi / mvm / sta.c
1 /******************************************************************************
2  *
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.
5  *
6  * GPL LICENSE SUMMARY
7  *
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
12  *
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.
16  *
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.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
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.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
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
46  *    distribution.
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.
50  *
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.
62  *
63  *****************************************************************************/
64 #include <net/mac80211.h>
65
66 #include "mvm.h"
67 #include "sta.h"
68 #include "rs.h"
69
70 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm);
71
72 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
73                                 u32 sta_id,
74                                 struct ieee80211_key_conf *key, bool mcast,
75                                 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
76                                 u8 key_offset, bool mfp);
77
78 /*
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.
82  */
83 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
84 {
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);
88         else
89                 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
90 }
91
92 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
93                                     enum nl80211_iftype iftype)
94 {
95         int sta_id;
96         u32 reserved_ids = 0;
97
98         BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
99         WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
100
101         lockdep_assert_held(&mvm->mutex);
102
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);
106
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)
110                         continue;
111
112                 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
113                                                lockdep_is_held(&mvm->mutex)))
114                         return sta_id;
115         }
116         return IWL_MVM_INVALID_STA;
117 }
118
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)
122 {
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),
132         };
133         int ret;
134         u32 status;
135         u32 agg_size = 0, mpdu_dens = 0;
136
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;
139
140         if (!update || (flags & STA_MODIFY_QUEUES)) {
141                 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
142
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);
146
147                         if (flags & STA_MODIFY_QUEUES)
148                                 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
149                 } else {
150                         WARN_ON(flags & STA_MODIFY_QUEUES);
151                 }
152         }
153
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);
157                 /* fall through */
158         case IEEE80211_STA_RX_BW_80:
159                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
160                 /* fall through */
161         case IEEE80211_STA_RX_BW_40:
162                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
163                 /* fall through */
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);
168                 break;
169         }
170
171         switch (sta->rx_nss) {
172         case 1:
173                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
174                 break;
175         case 2:
176                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
177                 break;
178         case 3 ... 8:
179                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
180                 break;
181         }
182
183         switch (sta->smps_mode) {
184         case IEEE80211_SMPS_AUTOMATIC:
185         case IEEE80211_SMPS_NUM_MODES:
186                 WARN_ON(1);
187                 break;
188         case IEEE80211_SMPS_STATIC:
189                 /* override NSS */
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);
192                 break;
193         case IEEE80211_SMPS_DYNAMIC:
194                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
195                 break;
196         case IEEE80211_SMPS_OFF:
197                 /* nothing */
198                 break;
199         }
200
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);
205
206                 mpdu_dens = sta->ht_cap.ampdu_density;
207         }
208
209         if (sta->vht_cap.vht_supported) {
210                 agg_size = sta->vht_cap.cap &
211                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
212                 agg_size >>=
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;
216         }
217
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);
224
225         if (sta->wme) {
226                 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
227
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;
238         }
239
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);
244         if (ret)
245                 return ret;
246
247         switch (status & IWL_ADD_STA_STATUS_MASK) {
248         case ADD_STA_SUCCESS:
249                 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
250                 break;
251         default:
252                 ret = -EIO;
253                 IWL_ERR(mvm, "ADD_STA failed\n");
254                 break;
255         }
256
257         return ret;
258 }
259
260 static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
261 {
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;
269
270         rcu_read_lock();
271
272         ba_data = rcu_dereference(*rcu_ptr);
273
274         if (WARN_ON(!ba_data))
275                 goto unlock;
276
277         if (!ba_data->timeout)
278                 goto unlock;
279
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);
283                 goto unlock;
284         }
285
286         /* Timer expired */
287         sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
288
289         /*
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.
296          */
297         if (!sta)
298                 goto unlock;
299
300         mvm_sta = iwl_mvm_sta_from_mac80211(sta);
301         ieee80211_rx_ba_timer_expired(mvm_sta->vif,
302                                       sta->addr, ba_data->tid);
303 unlock:
304         rcu_read_unlock();
305 }
306
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,
310                                         bool remove_queue)
311 {
312         struct iwl_mvm_add_sta_cmd cmd = {};
313         struct ieee80211_sta *sta;
314         struct iwl_mvm_sta *mvmsta;
315         u32 status;
316         u8 sta_id;
317
318         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
319                 return -EINVAL;
320
321         sta_id = mvm->queue_info[queue].ra_sta_id;
322
323         rcu_read_lock();
324
325         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
326
327         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
328                 rcu_read_unlock();
329                 return -EINVAL;
330         }
331
332         mvmsta = iwl_mvm_sta_from_mac80211(sta);
333
334         mvmsta->tid_disable_agg |= disable_agg_tids;
335
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;
342         if (remove_queue)
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);
346
347         rcu_read_unlock();
348
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),
353                                            &cmd, &status);
354 }
355
356 static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
357                                int queue, u8 tid, u8 flags)
358 {
359         struct iwl_scd_txq_cfg_cmd cmd = {
360                 .scd_queue = queue,
361                 .action = SCD_CFG_DISABLE_QUEUE,
362         };
363         int ret;
364
365         if (iwl_mvm_has_new_tx_api(mvm)) {
366                 iwl_trans_txq_free(mvm->trans, queue);
367
368                 return 0;
369         }
370
371         if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
372                 return 0;
373
374         mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
375
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;
380
381         IWL_DEBUG_TX_QUEUES(mvm,
382                             "Disabling TXQ #%d tids=0x%x\n",
383                             queue,
384                             mvm->queue_info[queue].tid_bitmap);
385
386         /* If the queue is still enabled - nothing left to do in this func */
387         if (cmd.action == SCD_CFG_ENABLE_QUEUE)
388                 return 0;
389
390         cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
391         cmd.tid = mvm->queue_info[queue].txq_tid;
392
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);
397
398         /* If we are here - the queue is freed and we can zero out these vals */
399         mvm->queue_info[queue].tid_bitmap = 0;
400
401         if (sta) {
402                 struct iwl_mvm_txq *mvmtxq =
403                         iwl_mvm_txq_from_tid(sta, tid);
404
405                 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
406         }
407
408         /* Regardless if this is a reserved TXQ for a STA - mark it as false */
409         mvm->queue_info[queue].reserved = false;
410
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);
414
415         if (ret)
416                 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
417                         queue, ret);
418         return ret;
419 }
420
421 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
422 {
423         struct ieee80211_sta *sta;
424         struct iwl_mvm_sta *mvmsta;
425         unsigned long tid_bitmap;
426         unsigned long agg_tids = 0;
427         u8 sta_id;
428         int tid;
429
430         lockdep_assert_held(&mvm->mutex);
431
432         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
433                 return -EINVAL;
434
435         sta_id = mvm->queue_info[queue].ra_sta_id;
436         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
437
438         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
439                                         lockdep_is_held(&mvm->mutex));
440
441         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
442                 return -EINVAL;
443
444         mvmsta = iwl_mvm_sta_from_mac80211(sta);
445
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);
450         }
451         spin_unlock_bh(&mvmsta->lock);
452
453         return agg_tids;
454 }
455
456 /*
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
460  */
461 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
462 {
463         struct ieee80211_sta *sta;
464         struct iwl_mvm_sta *mvmsta;
465         unsigned long tid_bitmap;
466         unsigned long disable_agg_tids = 0;
467         u8 sta_id;
468         int tid;
469
470         lockdep_assert_held(&mvm->mutex);
471
472         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
473                 return -EINVAL;
474
475         sta_id = mvm->queue_info[queue].ra_sta_id;
476         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
477
478         rcu_read_lock();
479
480         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
481
482         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
483                 rcu_read_unlock();
484                 return 0;
485         }
486
487         mvmsta = iwl_mvm_sta_from_mac80211(sta);
488
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);
494
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;
498
499                 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
500         }
501
502         mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
503         spin_unlock_bh(&mvmsta->lock);
504
505         rcu_read_unlock();
506
507         /*
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.
514          */
515         synchronize_net();
516
517         return disable_agg_tids;
518 }
519
520 static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
521                                        struct ieee80211_sta *old_sta,
522                                        u8 new_sta_id)
523 {
524         struct iwl_mvm_sta *mvmsta;
525         u8 sta_id, tid;
526         unsigned long disable_agg_tids = 0;
527         bool same_sta;
528         int ret;
529
530         lockdep_assert_held(&mvm->mutex);
531
532         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
533                 return -EINVAL;
534
535         sta_id = mvm->queue_info[queue].ra_sta_id;
536         tid = mvm->queue_info[queue].txq_tid;
537
538         same_sta = sta_id == new_sta_id;
539
540         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
541         if (WARN_ON(!mvmsta))
542                 return -EINVAL;
543
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);
549
550         ret = iwl_mvm_disable_txq(mvm, old_sta, queue, tid, 0);
551         if (ret) {
552                 IWL_ERR(mvm,
553                         "Failed to free inactive queue %d (ret=%d)\n",
554                         queue, ret);
555
556                 return ret;
557         }
558
559         /* If TXQ is allocated to another STA, update removal in FW */
560         if (!same_sta)
561                 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
562
563         return 0;
564 }
565
566 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
567                                     unsigned long tfd_queue_mask, u8 ac)
568 {
569         int queue = 0;
570         u8 ac_to_queue[IEEE80211_NUM_ACS];
571         int i;
572
573         /*
574          * This protects us against grabbing a queue that's being reconfigured
575          * by the inactivity checker.
576          */
577         lockdep_assert_held(&mvm->mutex);
578
579         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
580                 return -EINVAL;
581
582         memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
583
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)
589                         continue;
590
591                 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
592         }
593
594         /*
595          * The queue to share is chosen only from DATA queues as follows (in
596          * descending priority):
597          * 1. An AC_BE queue
598          * 2. Same AC queue
599          * 3. Highest AC queue that is lower than new AC
600          * 4. Any existing AC (there always is at least 1 DATA queue)
601          */
602
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];
622
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");
628                 return -ENOSPC;
629         }
630
631         return queue;
632 }
633
634 /*
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.
639  */
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)
643 {
644         struct iwl_scd_txq_cfg_cmd cmd = {
645                 .scd_queue = queue,
646                 .action = SCD_CFG_DISABLE_QUEUE,
647         };
648         bool shared_queue;
649         int ret;
650
651         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
652                 return -EINVAL;
653
654         /*
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
657          * here.
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.
661          */
662         if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
663                 IWL_DEBUG_TX_QUEUES(mvm,
664                                     "No redirection needed on TXQ #%d\n",
665                                     queue);
666                 return 0;
667         }
668
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;
673
674         IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
675                             queue, iwl_mvm_ac_to_tx_fifo[ac]);
676
677         /* Stop the queue and wait for it to empty */
678         txq->stopped = true;
679
680         ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
681         if (ret) {
682                 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
683                         queue);
684                 ret = -EIO;
685                 goto out;
686         }
687
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);
691         if (ret)
692                 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
693                         ret);
694
695         /* Make sure the SCD wrptr is correctly set before reconfiguring */
696         iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
697
698         /* Update the TID "owner" of the queue */
699         mvm->queue_info[queue].txq_tid = tid;
700
701         /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
702
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);
706
707         /* Update AC marking of the queue */
708         mvm->queue_info[queue].mac80211_ac = ac;
709
710         /*
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
714          * queues
715          */
716         if (shared_queue)
717                 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
718
719 out:
720         /* Continue using the queue */
721         txq->stopped = false;
722
723         return ret;
724 }
725
726 static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
727                                    u8 minq, u8 maxq)
728 {
729         int i;
730
731         lockdep_assert_held(&mvm->mutex);
732
733         /* This should not be hit with new TX path */
734         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
735                 return -ENOSPC;
736
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)
741                         return i;
742
743         return -ENOSPC;
744 }
745
746 static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
747                                    u8 sta_id, u8 tid, unsigned int timeout)
748 {
749         int queue, size = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
750                                 mvm->trans->cfg->min_256_ba_txq_size);
751
752         if (tid == IWL_MAX_TID_COUNT) {
753                 tid = IWL_MGMT_TID;
754                 size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
755                              mvm->trans->cfg->min_txq_size);
756         }
757         queue = iwl_trans_txq_alloc(mvm->trans,
758                                     cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
759                                     sta_id, tid, SCD_QUEUE_CFG, size, timeout);
760
761         if (queue < 0) {
762                 IWL_DEBUG_TX_QUEUES(mvm,
763                                     "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
764                                     sta_id, tid, queue);
765                 return queue;
766         }
767
768         IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
769                             queue, sta_id, tid);
770
771         IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d\n", queue);
772
773         return queue;
774 }
775
776 static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
777                                         struct ieee80211_sta *sta, u8 ac,
778                                         int tid)
779 {
780         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
781         struct iwl_mvm_txq *mvmtxq =
782                 iwl_mvm_txq_from_tid(sta, tid);
783         unsigned int wdg_timeout =
784                 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
785         int queue = -1;
786
787         lockdep_assert_held(&mvm->mutex);
788
789         IWL_DEBUG_TX_QUEUES(mvm,
790                             "Allocating queue for sta %d on tid %d\n",
791                             mvmsta->sta_id, tid);
792         queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
793         if (queue < 0)
794                 return queue;
795
796         mvmtxq->txq_id = queue;
797         mvm->tvqm_info[queue].txq_tid = tid;
798         mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
799
800         IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
801
802         spin_lock_bh(&mvmsta->lock);
803         mvmsta->tid_data[tid].txq_id = queue;
804         spin_unlock_bh(&mvmsta->lock);
805
806         return 0;
807 }
808
809 static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
810                                        struct ieee80211_sta *sta,
811                                        int queue, u8 sta_id, u8 tid)
812 {
813         bool enable_queue = true;
814
815         /* Make sure this TID isn't already enabled */
816         if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
817                 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
818                         queue, tid);
819                 return false;
820         }
821
822         /* Update mappings and refcounts */
823         if (mvm->queue_info[queue].tid_bitmap)
824                 enable_queue = false;
825
826         mvm->queue_info[queue].tid_bitmap |= BIT(tid);
827         mvm->queue_info[queue].ra_sta_id = sta_id;
828
829         if (enable_queue) {
830                 if (tid != IWL_MAX_TID_COUNT)
831                         mvm->queue_info[queue].mac80211_ac =
832                                 tid_to_mac80211_ac[tid];
833                 else
834                         mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
835
836                 mvm->queue_info[queue].txq_tid = tid;
837         }
838
839         if (sta) {
840                 struct iwl_mvm_txq *mvmtxq =
841                         iwl_mvm_txq_from_tid(sta, tid);
842
843                 mvmtxq->txq_id = queue;
844         }
845
846         IWL_DEBUG_TX_QUEUES(mvm,
847                             "Enabling TXQ #%d tids=0x%x\n",
848                             queue, mvm->queue_info[queue].tid_bitmap);
849
850         return enable_queue;
851 }
852
853 static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
854                                int queue, u16 ssn,
855                                const struct iwl_trans_txq_scd_cfg *cfg,
856                                unsigned int wdg_timeout)
857 {
858         struct iwl_scd_txq_cfg_cmd cmd = {
859                 .scd_queue = queue,
860                 .action = SCD_CFG_ENABLE_QUEUE,
861                 .window = cfg->frame_limit,
862                 .sta_id = cfg->sta_id,
863                 .ssn = cpu_to_le16(ssn),
864                 .tx_fifo = cfg->fifo,
865                 .aggregate = cfg->aggregate,
866                 .tid = cfg->tid,
867         };
868         bool inc_ssn;
869
870         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
871                 return false;
872
873         /* Send the enabling command if we need to */
874         if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
875                 return false;
876
877         inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
878                                            NULL, wdg_timeout);
879         if (inc_ssn)
880                 le16_add_cpu(&cmd.ssn, 1);
881
882         WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
883              "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
884
885         return inc_ssn;
886 }
887
888 static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
889 {
890         struct iwl_scd_txq_cfg_cmd cmd = {
891                 .scd_queue = queue,
892                 .action = SCD_CFG_UPDATE_QUEUE_TID,
893         };
894         int tid;
895         unsigned long tid_bitmap;
896         int ret;
897
898         lockdep_assert_held(&mvm->mutex);
899
900         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
901                 return;
902
903         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
904
905         if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
906                 return;
907
908         /* Find any TID for queue */
909         tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
910         cmd.tid = tid;
911         cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
912
913         ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
914         if (ret) {
915                 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
916                         queue, ret);
917                 return;
918         }
919
920         mvm->queue_info[queue].txq_tid = tid;
921         IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
922                             queue, tid);
923 }
924
925 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
926 {
927         struct ieee80211_sta *sta;
928         struct iwl_mvm_sta *mvmsta;
929         u8 sta_id;
930         int tid = -1;
931         unsigned long tid_bitmap;
932         unsigned int wdg_timeout;
933         int ssn;
934         int ret = true;
935
936         /* queue sharing is disabled on new TX path */
937         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
938                 return;
939
940         lockdep_assert_held(&mvm->mutex);
941
942         sta_id = mvm->queue_info[queue].ra_sta_id;
943         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
944
945         /* Find TID for queue, and make sure it is the only one on the queue */
946         tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
947         if (tid_bitmap != BIT(tid)) {
948                 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
949                         queue, tid_bitmap);
950                 return;
951         }
952
953         IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
954                             tid);
955
956         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
957                                         lockdep_is_held(&mvm->mutex));
958
959         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
960                 return;
961
962         mvmsta = iwl_mvm_sta_from_mac80211(sta);
963         wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
964
965         ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
966
967         ret = iwl_mvm_redirect_queue(mvm, queue, tid,
968                                      tid_to_mac80211_ac[tid], ssn,
969                                      wdg_timeout, true,
970                                      iwl_mvm_txq_from_tid(sta, tid));
971         if (ret) {
972                 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
973                 return;
974         }
975
976         /* If aggs should be turned back on - do it */
977         if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
978                 struct iwl_mvm_add_sta_cmd cmd = {0};
979
980                 mvmsta->tid_disable_agg &= ~BIT(tid);
981
982                 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
983                 cmd.sta_id = mvmsta->sta_id;
984                 cmd.add_modify = STA_MODE_MODIFY;
985                 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
986                 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
987                 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
988
989                 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
990                                            iwl_mvm_add_sta_cmd_size(mvm), &cmd);
991                 if (!ret) {
992                         IWL_DEBUG_TX_QUEUES(mvm,
993                                             "TXQ #%d is now aggregated again\n",
994                                             queue);
995
996                         /* Mark queue intenally as aggregating again */
997                         iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
998                 }
999         }
1000
1001         mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1002 }
1003
1004 /*
1005  * Remove inactive TIDs of a given queue.
1006  * If all queue TIDs are inactive - mark the queue as inactive
1007  * If only some the queue TIDs are inactive - unmap them from the queue
1008  *
1009  * Returns %true if all TIDs were removed and the queue could be reused.
1010  */
1011 static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1012                                          struct iwl_mvm_sta *mvmsta, int queue,
1013                                          unsigned long tid_bitmap,
1014                                          unsigned long *unshare_queues,
1015                                          unsigned long *changetid_queues)
1016 {
1017         int tid;
1018
1019         lockdep_assert_held(&mvmsta->lock);
1020         lockdep_assert_held(&mvm->mutex);
1021
1022         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1023                 return false;
1024
1025         /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1026         for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1027                 /* If some TFDs are still queued - don't mark TID as inactive */
1028                 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1029                         tid_bitmap &= ~BIT(tid);
1030
1031                 /* Don't mark as inactive any TID that has an active BA */
1032                 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1033                         tid_bitmap &= ~BIT(tid);
1034         }
1035
1036         /* If all TIDs in the queue are inactive - return it can be reused */
1037         if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1038                 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1039                 return true;
1040         }
1041
1042         /*
1043          * If we are here, this is a shared queue and not all TIDs timed-out.
1044          * Remove the ones that did.
1045          */
1046         for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1047                 u16 tid_bitmap;
1048
1049                 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1050                 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1051
1052                 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1053
1054                 /*
1055                  * We need to take into account a situation in which a TXQ was
1056                  * allocated to TID x, and then turned shared by adding TIDs y
1057                  * and z. If TID x becomes inactive and is removed from the TXQ,
1058                  * ownership must be given to one of the remaining TIDs.
1059                  * This is mainly because if TID x continues - a new queue can't
1060                  * be allocated for it as long as it is an owner of another TXQ.
1061                  *
1062                  * Mark this queue in the right bitmap, we'll send the command
1063                  * to the firmware later.
1064                  */
1065                 if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1066                         set_bit(queue, changetid_queues);
1067
1068                 IWL_DEBUG_TX_QUEUES(mvm,
1069                                     "Removing inactive TID %d from shared Q:%d\n",
1070                                     tid, queue);
1071         }
1072
1073         IWL_DEBUG_TX_QUEUES(mvm,
1074                             "TXQ #%d left with tid bitmap 0x%x\n", queue,
1075                             mvm->queue_info[queue].tid_bitmap);
1076
1077         /*
1078          * There may be different TIDs with the same mac queues, so make
1079          * sure all TIDs have existing corresponding mac queues enabled
1080          */
1081         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1082
1083         /* If the queue is marked as shared - "unshare" it */
1084         if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1085             mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1086                 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1087                                     queue);
1088                 set_bit(queue, unshare_queues);
1089         }
1090
1091         return false;
1092 }
1093
1094 /*
1095  * Check for inactivity - this includes checking if any queue
1096  * can be unshared and finding one (and only one) that can be
1097  * reused.
1098  * This function is also invoked as a sort of clean-up task,
1099  * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1100  *
1101  * Returns the queue number, or -ENOSPC.
1102  */
1103 static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1104 {
1105         unsigned long now = jiffies;
1106         unsigned long unshare_queues = 0;
1107         unsigned long changetid_queues = 0;
1108         int i, ret, free_queue = -ENOSPC;
1109         struct ieee80211_sta *queue_owner  = NULL;
1110
1111         lockdep_assert_held(&mvm->mutex);
1112
1113         if (iwl_mvm_has_new_tx_api(mvm))
1114                 return -ENOSPC;
1115
1116         rcu_read_lock();
1117
1118         /* we skip the CMD queue below by starting at 1 */
1119         BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1120
1121         for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1122                 struct ieee80211_sta *sta;
1123                 struct iwl_mvm_sta *mvmsta;
1124                 u8 sta_id;
1125                 int tid;
1126                 unsigned long inactive_tid_bitmap = 0;
1127                 unsigned long queue_tid_bitmap;
1128
1129                 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1130                 if (!queue_tid_bitmap)
1131                         continue;
1132
1133                 /* If TXQ isn't in active use anyway - nothing to do here... */
1134                 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1135                     mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1136                         continue;
1137
1138                 /* Check to see if there are inactive TIDs on this queue */
1139                 for_each_set_bit(tid, &queue_tid_bitmap,
1140                                  IWL_MAX_TID_COUNT + 1) {
1141                         if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1142                                        IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1143                                 continue;
1144
1145                         inactive_tid_bitmap |= BIT(tid);
1146                 }
1147
1148                 /* If all TIDs are active - finish check on this queue */
1149                 if (!inactive_tid_bitmap)
1150                         continue;
1151
1152                 /*
1153                  * If we are here - the queue hadn't been served recently and is
1154                  * in use
1155                  */
1156
1157                 sta_id = mvm->queue_info[i].ra_sta_id;
1158                 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1159
1160                 /*
1161                  * If the STA doesn't exist anymore, it isn't an error. It could
1162                  * be that it was removed since getting the queues, and in this
1163                  * case it should've inactivated its queues anyway.
1164                  */
1165                 if (IS_ERR_OR_NULL(sta))
1166                         continue;
1167
1168                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1169
1170                 spin_lock_bh(&mvmsta->lock);
1171                 ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1172                                                    inactive_tid_bitmap,
1173                                                    &unshare_queues,
1174                                                    &changetid_queues);
1175                 if (ret >= 0 && free_queue < 0) {
1176                         queue_owner = sta;
1177                         free_queue = ret;
1178                 }
1179                 /* only unlock sta lock - we still need the queue info lock */
1180                 spin_unlock_bh(&mvmsta->lock);
1181         }
1182
1183
1184         /* Reconfigure queues requiring reconfiguation */
1185         for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1186                 iwl_mvm_unshare_queue(mvm, i);
1187         for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1188                 iwl_mvm_change_queue_tid(mvm, i);
1189
1190         if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1191                 ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1192                                                   alloc_for_sta);
1193                 if (ret) {
1194                         rcu_read_unlock();
1195                         return ret;
1196                 }
1197         }
1198
1199         rcu_read_unlock();
1200
1201         return free_queue;
1202 }
1203
1204 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1205                                    struct ieee80211_sta *sta, u8 ac, int tid)
1206 {
1207         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1208         struct iwl_trans_txq_scd_cfg cfg = {
1209                 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1210                 .sta_id = mvmsta->sta_id,
1211                 .tid = tid,
1212                 .frame_limit = IWL_FRAME_LIMIT,
1213         };
1214         unsigned int wdg_timeout =
1215                 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1216         int queue = -1;
1217         unsigned long disable_agg_tids = 0;
1218         enum iwl_mvm_agg_state queue_state;
1219         bool shared_queue = false, inc_ssn;
1220         int ssn;
1221         unsigned long tfd_queue_mask;
1222         int ret;
1223
1224         lockdep_assert_held(&mvm->mutex);
1225
1226         if (iwl_mvm_has_new_tx_api(mvm))
1227                 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1228
1229         spin_lock_bh(&mvmsta->lock);
1230         tfd_queue_mask = mvmsta->tfd_queue_msk;
1231         ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1232         spin_unlock_bh(&mvmsta->lock);
1233
1234         if (tid == IWL_MAX_TID_COUNT) {
1235                 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1236                                                 IWL_MVM_DQA_MIN_MGMT_QUEUE,
1237                                                 IWL_MVM_DQA_MAX_MGMT_QUEUE);
1238                 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1239                         IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1240                                             queue);
1241
1242                 /* If no such queue is found, we'll use a DATA queue instead */
1243         }
1244
1245         if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1246             (mvm->queue_info[mvmsta->reserved_queue].status ==
1247                         IWL_MVM_QUEUE_RESERVED)) {
1248                 queue = mvmsta->reserved_queue;
1249                 mvm->queue_info[queue].reserved = true;
1250                 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1251         }
1252
1253         if (queue < 0)
1254                 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1255                                                 IWL_MVM_DQA_MIN_DATA_QUEUE,
1256                                                 IWL_MVM_DQA_MAX_DATA_QUEUE);
1257         if (queue < 0) {
1258                 /* try harder - perhaps kill an inactive queue */
1259                 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1260         }
1261
1262         /* No free queue - we'll have to share */
1263         if (queue <= 0) {
1264                 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1265                 if (queue > 0) {
1266                         shared_queue = true;
1267                         mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1268                 }
1269         }
1270
1271         /*
1272          * Mark TXQ as ready, even though it hasn't been fully configured yet,
1273          * to make sure no one else takes it.
1274          * This will allow avoiding re-acquiring the lock at the end of the
1275          * configuration. On error we'll mark it back as free.
1276          */
1277         if (queue > 0 && !shared_queue)
1278                 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1279
1280         /* This shouldn't happen - out of queues */
1281         if (WARN_ON(queue <= 0)) {
1282                 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1283                         tid, cfg.sta_id);
1284                 return queue;
1285         }
1286
1287         /*
1288          * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1289          * but for configuring the SCD to send A-MPDUs we need to mark the queue
1290          * as aggregatable.
1291          * Mark all DATA queues as allowing to be aggregated at some point
1292          */
1293         cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1294                          queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1295
1296         IWL_DEBUG_TX_QUEUES(mvm,
1297                             "Allocating %squeue #%d to sta %d on tid %d\n",
1298                             shared_queue ? "shared " : "", queue,
1299                             mvmsta->sta_id, tid);
1300
1301         if (shared_queue) {
1302                 /* Disable any open aggs on this queue */
1303                 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1304
1305                 if (disable_agg_tids) {
1306                         IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1307                                             queue);
1308                         iwl_mvm_invalidate_sta_queue(mvm, queue,
1309                                                      disable_agg_tids, false);
1310                 }
1311         }
1312
1313         inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1314
1315         /*
1316          * Mark queue as shared in transport if shared
1317          * Note this has to be done after queue enablement because enablement
1318          * can also set this value, and there is no indication there to shared
1319          * queues
1320          */
1321         if (shared_queue)
1322                 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1323
1324         spin_lock_bh(&mvmsta->lock);
1325         /*
1326          * This looks racy, but it is not. We have only one packet for
1327          * this ra/tid in our Tx path since we stop the Qdisc when we
1328          * need to allocate a new TFD queue.
1329          */
1330         if (inc_ssn) {
1331                 mvmsta->tid_data[tid].seq_number += 0x10;
1332                 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1333         }
1334         mvmsta->tid_data[tid].txq_id = queue;
1335         mvmsta->tfd_queue_msk |= BIT(queue);
1336         queue_state = mvmsta->tid_data[tid].state;
1337
1338         if (mvmsta->reserved_queue == queue)
1339                 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1340         spin_unlock_bh(&mvmsta->lock);
1341
1342         if (!shared_queue) {
1343                 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1344                 if (ret)
1345                         goto out_err;
1346
1347                 /* If we need to re-enable aggregations... */
1348                 if (queue_state == IWL_AGG_ON) {
1349                         ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1350                         if (ret)
1351                                 goto out_err;
1352                 }
1353         } else {
1354                 /* Redirect queue, if needed */
1355                 ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1356                                              wdg_timeout, false,
1357                                              iwl_mvm_txq_from_tid(sta, tid));
1358                 if (ret)
1359                         goto out_err;
1360         }
1361
1362         return 0;
1363
1364 out_err:
1365         iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1366
1367         return ret;
1368 }
1369
1370 static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1371 {
1372         if (tid == IWL_MAX_TID_COUNT)
1373                 return IEEE80211_AC_VO; /* MGMT */
1374
1375         return tid_to_mac80211_ac[tid];
1376 }
1377
1378 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1379 {
1380         struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1381                                            add_stream_wk);
1382
1383         mutex_lock(&mvm->mutex);
1384
1385         iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1386
1387         while (!list_empty(&mvm->add_stream_txqs)) {
1388                 struct iwl_mvm_txq *mvmtxq;
1389                 struct ieee80211_txq *txq;
1390                 u8 tid;
1391
1392                 mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1393                                           struct iwl_mvm_txq, list);
1394
1395                 txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1396                                    drv_priv);
1397                 tid = txq->tid;
1398                 if (tid == IEEE80211_NUM_TIDS)
1399                         tid = IWL_MAX_TID_COUNT;
1400
1401                 iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid);
1402                 list_del_init(&mvmtxq->list);
1403                 local_bh_disable();
1404                 iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1405                 local_bh_enable();
1406         }
1407
1408         mutex_unlock(&mvm->mutex);
1409 }
1410
1411 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1412                                       struct ieee80211_sta *sta,
1413                                       enum nl80211_iftype vif_type)
1414 {
1415         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1416         int queue;
1417
1418         /* queue reserving is disabled on new TX path */
1419         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1420                 return 0;
1421
1422         /* run the general cleanup/unsharing of queues */
1423         iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1424
1425         /* Make sure we have free resources for this STA */
1426         if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1427             !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1428             (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1429              IWL_MVM_QUEUE_FREE))
1430                 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1431         else
1432                 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1433                                                 IWL_MVM_DQA_MIN_DATA_QUEUE,
1434                                                 IWL_MVM_DQA_MAX_DATA_QUEUE);
1435         if (queue < 0) {
1436                 /* try again - this time kick out a queue if needed */
1437                 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1438                 if (queue < 0) {
1439                         IWL_ERR(mvm, "No available queues for new station\n");
1440                         return -ENOSPC;
1441                 }
1442         }
1443         mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1444
1445         mvmsta->reserved_queue = queue;
1446
1447         IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1448                             queue, mvmsta->sta_id);
1449
1450         return 0;
1451 }
1452
1453 /*
1454  * In DQA mode, after a HW restart the queues should be allocated as before, in
1455  * order to avoid race conditions when there are shared queues. This function
1456  * does the re-mapping and queue allocation.
1457  *
1458  * Note that re-enabling aggregations isn't done in this function.
1459  */
1460 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1461                                                  struct ieee80211_sta *sta)
1462 {
1463         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1464         unsigned int wdg =
1465                 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1466         int i;
1467         struct iwl_trans_txq_scd_cfg cfg = {
1468                 .sta_id = mvm_sta->sta_id,
1469                 .frame_limit = IWL_FRAME_LIMIT,
1470         };
1471
1472         /* Make sure reserved queue is still marked as such (if allocated) */
1473         if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1474                 mvm->queue_info[mvm_sta->reserved_queue].status =
1475                         IWL_MVM_QUEUE_RESERVED;
1476
1477         for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1478                 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1479                 int txq_id = tid_data->txq_id;
1480                 int ac;
1481
1482                 if (txq_id == IWL_MVM_INVALID_QUEUE)
1483                         continue;
1484
1485                 ac = tid_to_mac80211_ac[i];
1486
1487                 if (iwl_mvm_has_new_tx_api(mvm)) {
1488                         IWL_DEBUG_TX_QUEUES(mvm,
1489                                             "Re-mapping sta %d tid %d\n",
1490                                             mvm_sta->sta_id, i);
1491                         txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1492                                                          i, wdg);
1493                         tid_data->txq_id = txq_id;
1494
1495                         /*
1496                          * Since we don't set the seq number after reset, and HW
1497                          * sets it now, FW reset will cause the seq num to start
1498                          * at 0 again, so driver will need to update it
1499                          * internally as well, so it keeps in sync with real val
1500                          */
1501                         tid_data->seq_number = 0;
1502                 } else {
1503                         u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1504
1505                         cfg.tid = i;
1506                         cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1507                         cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1508                                          txq_id ==
1509                                          IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1510
1511                         IWL_DEBUG_TX_QUEUES(mvm,
1512                                             "Re-mapping sta %d tid %d to queue %d\n",
1513                                             mvm_sta->sta_id, i, txq_id);
1514
1515                         iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1516                         mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1517                 }
1518         }
1519 }
1520
1521 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1522                                       struct iwl_mvm_int_sta *sta,
1523                                       const u8 *addr,
1524                                       u16 mac_id, u16 color)
1525 {
1526         struct iwl_mvm_add_sta_cmd cmd;
1527         int ret;
1528         u32 status = ADD_STA_SUCCESS;
1529
1530         lockdep_assert_held(&mvm->mutex);
1531
1532         memset(&cmd, 0, sizeof(cmd));
1533         cmd.sta_id = sta->sta_id;
1534         cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1535                                                              color));
1536         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1537                 cmd.station_type = sta->type;
1538
1539         if (!iwl_mvm_has_new_tx_api(mvm))
1540                 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1541         cmd.tid_disable_tx = cpu_to_le16(0xffff);
1542
1543         if (addr)
1544                 memcpy(cmd.addr, addr, ETH_ALEN);
1545
1546         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1547                                           iwl_mvm_add_sta_cmd_size(mvm),
1548                                           &cmd, &status);
1549         if (ret)
1550                 return ret;
1551
1552         switch (status & IWL_ADD_STA_STATUS_MASK) {
1553         case ADD_STA_SUCCESS:
1554                 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1555                 return 0;
1556         default:
1557                 ret = -EIO;
1558                 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1559                         status);
1560                 break;
1561         }
1562         return ret;
1563 }
1564
1565 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1566                     struct ieee80211_vif *vif,
1567                     struct ieee80211_sta *sta)
1568 {
1569         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1570         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1571         struct iwl_mvm_rxq_dup_data *dup_data;
1572         int i, ret, sta_id;
1573         bool sta_update = false;
1574         unsigned int sta_flags = 0;
1575
1576         lockdep_assert_held(&mvm->mutex);
1577
1578         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1579                 sta_id = iwl_mvm_find_free_sta_id(mvm,
1580                                                   ieee80211_vif_type_p2p(vif));
1581         else
1582                 sta_id = mvm_sta->sta_id;
1583
1584         if (sta_id == IWL_MVM_INVALID_STA)
1585                 return -ENOSPC;
1586
1587         spin_lock_init(&mvm_sta->lock);
1588
1589         /* if this is a HW restart re-alloc existing queues */
1590         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1591                 struct iwl_mvm_int_sta tmp_sta = {
1592                         .sta_id = sta_id,
1593                         .type = mvm_sta->sta_type,
1594                 };
1595
1596                 /*
1597                  * First add an empty station since allocating
1598                  * a queue requires a valid station
1599                  */
1600                 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1601                                                  mvmvif->id, mvmvif->color);
1602                 if (ret)
1603                         goto err;
1604
1605                 iwl_mvm_realloc_queues_after_restart(mvm, sta);
1606                 sta_update = true;
1607                 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1608                 goto update_fw;
1609         }
1610
1611         mvm_sta->sta_id = sta_id;
1612         mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1613                                                       mvmvif->color);
1614         mvm_sta->vif = vif;
1615         if (!mvm->trans->cfg->gen2)
1616                 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1617         else
1618                 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1619         mvm_sta->tx_protection = 0;
1620         mvm_sta->tt_tx_protection = false;
1621         mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1622
1623         /* HW restart, don't assume the memory has been zeroed */
1624         mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1625         mvm_sta->tfd_queue_msk = 0;
1626
1627         /* for HW restart - reset everything but the sequence number */
1628         for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1629                 u16 seq = mvm_sta->tid_data[i].seq_number;
1630                 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1631                 mvm_sta->tid_data[i].seq_number = seq;
1632
1633                 /*
1634                  * Mark all queues for this STA as unallocated and defer TX
1635                  * frames until the queue is allocated
1636                  */
1637                 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1638         }
1639
1640         for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1641                 struct iwl_mvm_txq *mvmtxq =
1642                         iwl_mvm_txq_from_mac80211(sta->txq[i]);
1643
1644                 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1645                 INIT_LIST_HEAD(&mvmtxq->list);
1646                 atomic_set(&mvmtxq->tx_request, 0);
1647         }
1648
1649         mvm_sta->agg_tids = 0;
1650
1651         if (iwl_mvm_has_new_rx_api(mvm) &&
1652             !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1653                 int q;
1654
1655                 dup_data = kcalloc(mvm->trans->num_rx_queues,
1656                                    sizeof(*dup_data), GFP_KERNEL);
1657                 if (!dup_data)
1658                         return -ENOMEM;
1659                 /*
1660                  * Initialize all the last_seq values to 0xffff which can never
1661                  * compare equal to the frame's seq_ctrl in the check in
1662                  * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1663                  * number and fragmented packets don't reach that function.
1664                  *
1665                  * This thus allows receiving a packet with seqno 0 and the
1666                  * retry bit set as the very first packet on a new TID.
1667                  */
1668                 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1669                         memset(dup_data[q].last_seq, 0xff,
1670                                sizeof(dup_data[q].last_seq));
1671                 mvm_sta->dup_data = dup_data;
1672         }
1673
1674         if (!iwl_mvm_has_new_tx_api(mvm)) {
1675                 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1676                                                  ieee80211_vif_type_p2p(vif));
1677                 if (ret)
1678                         goto err;
1679         }
1680
1681         /*
1682          * if rs is registered with mac80211, then "add station" will be handled
1683          * via the corresponding ops, otherwise need to notify rate scaling here
1684          */
1685         if (iwl_mvm_has_tlc_offload(mvm))
1686                 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1687
1688         iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1689
1690 update_fw:
1691         ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1692         if (ret)
1693                 goto err;
1694
1695         if (vif->type == NL80211_IFTYPE_STATION) {
1696                 if (!sta->tdls) {
1697                         WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1698                         mvmvif->ap_sta_id = sta_id;
1699                 } else {
1700                         WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1701                 }
1702         }
1703
1704         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1705
1706         return 0;
1707
1708 err:
1709         return ret;
1710 }
1711
1712 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1713                       bool drain)
1714 {
1715         struct iwl_mvm_add_sta_cmd cmd = {};
1716         int ret;
1717         u32 status;
1718
1719         lockdep_assert_held(&mvm->mutex);
1720
1721         cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1722         cmd.sta_id = mvmsta->sta_id;
1723         cmd.add_modify = STA_MODE_MODIFY;
1724         cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1725         cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1726
1727         status = ADD_STA_SUCCESS;
1728         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1729                                           iwl_mvm_add_sta_cmd_size(mvm),
1730                                           &cmd, &status);
1731         if (ret)
1732                 return ret;
1733
1734         switch (status & IWL_ADD_STA_STATUS_MASK) {
1735         case ADD_STA_SUCCESS:
1736                 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1737                                mvmsta->sta_id);
1738                 break;
1739         default:
1740                 ret = -EIO;
1741                 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1742                         mvmsta->sta_id);
1743                 break;
1744         }
1745
1746         return ret;
1747 }
1748
1749 /*
1750  * Remove a station from the FW table. Before sending the command to remove
1751  * the station validate that the station is indeed known to the driver (sanity
1752  * only).
1753  */
1754 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1755 {
1756         struct ieee80211_sta *sta;
1757         struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1758                 .sta_id = sta_id,
1759         };
1760         int ret;
1761
1762         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1763                                         lockdep_is_held(&mvm->mutex));
1764
1765         /* Note: internal stations are marked as error values */
1766         if (!sta) {
1767                 IWL_ERR(mvm, "Invalid station id\n");
1768                 return -EINVAL;
1769         }
1770
1771         ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1772                                    sizeof(rm_sta_cmd), &rm_sta_cmd);
1773         if (ret) {
1774                 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1775                 return ret;
1776         }
1777
1778         return 0;
1779 }
1780
1781 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1782                                        struct ieee80211_vif *vif,
1783                                        struct ieee80211_sta *sta)
1784 {
1785         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1786         int i;
1787
1788         lockdep_assert_held(&mvm->mutex);
1789
1790         for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1791                 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1792                         continue;
1793
1794                 iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1795                                     0);
1796                 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1797         }
1798
1799         for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1800                 struct iwl_mvm_txq *mvmtxq =
1801                         iwl_mvm_txq_from_mac80211(sta->txq[i]);
1802
1803                 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1804         }
1805 }
1806
1807 int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1808                                   struct iwl_mvm_sta *mvm_sta)
1809 {
1810         int i;
1811
1812         for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1813                 u16 txq_id;
1814                 int ret;
1815
1816                 spin_lock_bh(&mvm_sta->lock);
1817                 txq_id = mvm_sta->tid_data[i].txq_id;
1818                 spin_unlock_bh(&mvm_sta->lock);
1819
1820                 if (txq_id == IWL_MVM_INVALID_QUEUE)
1821                         continue;
1822
1823                 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1824                 if (ret)
1825                         return ret;
1826         }
1827
1828         return 0;
1829 }
1830
1831 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1832                    struct ieee80211_vif *vif,
1833                    struct ieee80211_sta *sta)
1834 {
1835         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1836         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1837         u8 sta_id = mvm_sta->sta_id;
1838         int ret;
1839
1840         lockdep_assert_held(&mvm->mutex);
1841
1842         if (iwl_mvm_has_new_rx_api(mvm))
1843                 kfree(mvm_sta->dup_data);
1844
1845         ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1846         if (ret)
1847                 return ret;
1848
1849         /* flush its queues here since we are freeing mvm_sta */
1850         ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1851         if (ret)
1852                 return ret;
1853         if (iwl_mvm_has_new_tx_api(mvm)) {
1854                 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1855         } else {
1856                 u32 q_mask = mvm_sta->tfd_queue_msk;
1857
1858                 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1859                                                      q_mask);
1860         }
1861         if (ret)
1862                 return ret;
1863
1864         ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1865
1866         iwl_mvm_disable_sta_queues(mvm, vif, sta);
1867
1868         /* If there is a TXQ still marked as reserved - free it */
1869         if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1870                 u8 reserved_txq = mvm_sta->reserved_queue;
1871                 enum iwl_mvm_queue_status *status;
1872
1873                 /*
1874                  * If no traffic has gone through the reserved TXQ - it
1875                  * is still marked as IWL_MVM_QUEUE_RESERVED, and
1876                  * should be manually marked as free again
1877                  */
1878                 status = &mvm->queue_info[reserved_txq].status;
1879                 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1880                          (*status != IWL_MVM_QUEUE_FREE),
1881                          "sta_id %d reserved txq %d status %d",
1882                          sta_id, reserved_txq, *status))
1883                         return -EINVAL;
1884
1885                 *status = IWL_MVM_QUEUE_FREE;
1886         }
1887
1888         if (vif->type == NL80211_IFTYPE_STATION &&
1889             mvmvif->ap_sta_id == sta_id) {
1890                 /* if associated - we can't remove the AP STA now */
1891                 if (vif->bss_conf.assoc)
1892                         return ret;
1893
1894                 /* unassoc - go ahead - remove the AP STA now */
1895                 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1896
1897                 /* clear d0i3_ap_sta_id if no longer relevant */
1898                 if (mvm->d0i3_ap_sta_id == sta_id)
1899                         mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
1900         }
1901
1902         /*
1903          * This shouldn't happen - the TDLS channel switch should be canceled
1904          * before the STA is removed.
1905          */
1906         if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1907                 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1908                 cancel_delayed_work(&mvm->tdls_cs.dwork);
1909         }
1910
1911         /*
1912          * Make sure that the tx response code sees the station as -EBUSY and
1913          * calls the drain worker.
1914          */
1915         spin_lock_bh(&mvm_sta->lock);
1916         spin_unlock_bh(&mvm_sta->lock);
1917
1918         ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1919         RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1920
1921         return ret;
1922 }
1923
1924 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1925                       struct ieee80211_vif *vif,
1926                       u8 sta_id)
1927 {
1928         int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1929
1930         lockdep_assert_held(&mvm->mutex);
1931
1932         RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1933         return ret;
1934 }
1935
1936 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1937                              struct iwl_mvm_int_sta *sta,
1938                              u32 qmask, enum nl80211_iftype iftype,
1939                              enum iwl_sta_type type)
1940 {
1941         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1942             sta->sta_id == IWL_MVM_INVALID_STA) {
1943                 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1944                 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1945                         return -ENOSPC;
1946         }
1947
1948         sta->tfd_queue_msk = qmask;
1949         sta->type = type;
1950
1951         /* put a non-NULL value so iterating over the stations won't stop */
1952         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1953         return 0;
1954 }
1955
1956 void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1957 {
1958         RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1959         memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1960         sta->sta_id = IWL_MVM_INVALID_STA;
1961 }
1962
1963 static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 *queue,
1964                                           u8 sta_id, u8 fifo)
1965 {
1966         unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1967                                         mvm->cfg->base_params->wd_timeout :
1968                                         IWL_WATCHDOG_DISABLED;
1969
1970         if (iwl_mvm_has_new_tx_api(mvm)) {
1971                 int tvqm_queue =
1972                         iwl_mvm_tvqm_enable_txq(mvm, sta_id,
1973                                                 IWL_MAX_TID_COUNT,
1974                                                 wdg_timeout);
1975                 *queue = tvqm_queue;
1976         } else {
1977                 struct iwl_trans_txq_scd_cfg cfg = {
1978                         .fifo = fifo,
1979                         .sta_id = sta_id,
1980                         .tid = IWL_MAX_TID_COUNT,
1981                         .aggregate = false,
1982                         .frame_limit = IWL_FRAME_LIMIT,
1983                 };
1984
1985                 iwl_mvm_enable_txq(mvm, NULL, *queue, 0, &cfg, wdg_timeout);
1986         }
1987 }
1988
1989 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1990 {
1991         int ret;
1992
1993         lockdep_assert_held(&mvm->mutex);
1994
1995         /* Allocate aux station and assign to it the aux queue */
1996         ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1997                                        NL80211_IFTYPE_UNSPECIFIED,
1998                                        IWL_STA_AUX_ACTIVITY);
1999         if (ret)
2000                 return ret;
2001
2002         /* Map Aux queue to fifo - needs to happen before adding Aux station */
2003         if (!iwl_mvm_has_new_tx_api(mvm))
2004                 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2005                                               mvm->aux_sta.sta_id,
2006                                               IWL_MVM_TX_FIFO_MCAST);
2007
2008         ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
2009                                          MAC_INDEX_AUX, 0);
2010         if (ret) {
2011                 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2012                 return ret;
2013         }
2014
2015         /*
2016          * For 22000 firmware and on we cannot add queue to a station unknown
2017          * to firmware so enable queue here - after the station was added
2018          */
2019         if (iwl_mvm_has_new_tx_api(mvm))
2020                 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
2021                                               mvm->aux_sta.sta_id,
2022                                               IWL_MVM_TX_FIFO_MCAST);
2023
2024         return 0;
2025 }
2026
2027 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2028 {
2029         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2030         int ret;
2031
2032         lockdep_assert_held(&mvm->mutex);
2033
2034         /* Map snif queue to fifo - must happen before adding snif station */
2035         if (!iwl_mvm_has_new_tx_api(mvm))
2036                 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2037                                               mvm->snif_sta.sta_id,
2038                                               IWL_MVM_TX_FIFO_BE);
2039
2040         ret = iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
2041                                          mvmvif->id, 0);
2042         if (ret)
2043                 return ret;
2044
2045         /*
2046          * For 22000 firmware and on we cannot add queue to a station unknown
2047          * to firmware so enable queue here - after the station was added
2048          */
2049         if (iwl_mvm_has_new_tx_api(mvm))
2050                 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
2051                                               mvm->snif_sta.sta_id,
2052                                               IWL_MVM_TX_FIFO_BE);
2053
2054         return 0;
2055 }
2056
2057 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2058 {
2059         int ret;
2060
2061         lockdep_assert_held(&mvm->mutex);
2062
2063         iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2064         ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2065         if (ret)
2066                 IWL_WARN(mvm, "Failed sending remove station\n");
2067
2068         return ret;
2069 }
2070
2071 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2072 {
2073         iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2074 }
2075
2076 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
2077 {
2078         lockdep_assert_held(&mvm->mutex);
2079
2080         iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2081 }
2082
2083 /*
2084  * Send the add station command for the vif's broadcast station.
2085  * Assumes that the station was already allocated.
2086  *
2087  * @mvm: the mvm component
2088  * @vif: the interface to which the broadcast station is added
2089  * @bsta: the broadcast station to add.
2090  */
2091 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2092 {
2093         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2094         struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2095         static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2096         const u8 *baddr = _baddr;
2097         int queue;
2098         int ret;
2099         unsigned int wdg_timeout =
2100                 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2101         struct iwl_trans_txq_scd_cfg cfg = {
2102                 .fifo = IWL_MVM_TX_FIFO_VO,
2103                 .sta_id = mvmvif->bcast_sta.sta_id,
2104                 .tid = IWL_MAX_TID_COUNT,
2105                 .aggregate = false,
2106                 .frame_limit = IWL_FRAME_LIMIT,
2107         };
2108
2109         lockdep_assert_held(&mvm->mutex);
2110
2111         if (!iwl_mvm_has_new_tx_api(mvm)) {
2112                 if (vif->type == NL80211_IFTYPE_AP ||
2113                     vif->type == NL80211_IFTYPE_ADHOC) {
2114                         queue = mvm->probe_queue;
2115                 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2116                         queue = mvm->p2p_dev_queue;
2117                 } else {
2118                         WARN(1, "Missing required TXQ for adding bcast STA\n");
2119                         return -EINVAL;
2120                 }
2121
2122                 bsta->tfd_queue_msk |= BIT(queue);
2123
2124                 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2125         }
2126
2127         if (vif->type == NL80211_IFTYPE_ADHOC)
2128                 baddr = vif->bss_conf.bssid;
2129
2130         if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2131                 return -ENOSPC;
2132
2133         ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2134                                          mvmvif->id, mvmvif->color);
2135         if (ret)
2136                 return ret;
2137
2138         /*
2139          * For 22000 firmware and on we cannot add queue to a station unknown
2140          * to firmware so enable queue here - after the station was added
2141          */
2142         if (iwl_mvm_has_new_tx_api(mvm)) {
2143                 queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2144                                                 IWL_MAX_TID_COUNT,
2145                                                 wdg_timeout);
2146
2147                 if (vif->type == NL80211_IFTYPE_AP ||
2148                     vif->type == NL80211_IFTYPE_ADHOC)
2149                         mvm->probe_queue = queue;
2150                 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2151                         mvm->p2p_dev_queue = queue;
2152         }
2153
2154         return 0;
2155 }
2156
2157 static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2158                                           struct ieee80211_vif *vif)
2159 {
2160         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2161         int queue;
2162
2163         lockdep_assert_held(&mvm->mutex);
2164
2165         iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2166
2167         switch (vif->type) {
2168         case NL80211_IFTYPE_AP:
2169         case NL80211_IFTYPE_ADHOC:
2170                 queue = mvm->probe_queue;
2171                 break;
2172         case NL80211_IFTYPE_P2P_DEVICE:
2173                 queue = mvm->p2p_dev_queue;
2174                 break;
2175         default:
2176                 WARN(1, "Can't free bcast queue on vif type %d\n",
2177                      vif->type);
2178                 return;
2179         }
2180
2181         iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2182         if (iwl_mvm_has_new_tx_api(mvm))
2183                 return;
2184
2185         WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2186         mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2187 }
2188
2189 /* Send the FW a request to remove the station from it's internal data
2190  * structures, but DO NOT remove the entry from the local data structures. */
2191 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2192 {
2193         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2194         int ret;
2195
2196         lockdep_assert_held(&mvm->mutex);
2197
2198         iwl_mvm_free_bcast_sta_queues(mvm, vif);
2199
2200         ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2201         if (ret)
2202                 IWL_WARN(mvm, "Failed sending remove station\n");
2203         return ret;
2204 }
2205
2206 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2207 {
2208         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2209
2210         lockdep_assert_held(&mvm->mutex);
2211
2212         return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2213                                         ieee80211_vif_type_p2p(vif),
2214                                         IWL_STA_GENERAL_PURPOSE);
2215 }
2216
2217 /* Allocate a new station entry for the broadcast station to the given vif,
2218  * and send it to the FW.
2219  * Note that each P2P mac should have its own broadcast station.
2220  *
2221  * @mvm: the mvm component
2222  * @vif: the interface to which the broadcast station is added
2223  * @bsta: the broadcast station to add. */
2224 int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2225 {
2226         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2227         struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2228         int ret;
2229
2230         lockdep_assert_held(&mvm->mutex);
2231
2232         ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2233         if (ret)
2234                 return ret;
2235
2236         ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2237
2238         if (ret)
2239                 iwl_mvm_dealloc_int_sta(mvm, bsta);
2240
2241         return ret;
2242 }
2243
2244 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2245 {
2246         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2247
2248         iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2249 }
2250
2251 /*
2252  * Send the FW a request to remove the station from it's internal data
2253  * structures, and in addition remove it from the local data structure.
2254  */
2255 int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2256 {
2257         int ret;
2258
2259         lockdep_assert_held(&mvm->mutex);
2260
2261         ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2262
2263         iwl_mvm_dealloc_bcast_sta(mvm, vif);
2264
2265         return ret;
2266 }
2267
2268 /*
2269  * Allocate a new station entry for the multicast station to the given vif,
2270  * and send it to the FW.
2271  * Note that each AP/GO mac should have its own multicast station.
2272  *
2273  * @mvm: the mvm component
2274  * @vif: the interface to which the multicast station is added
2275  */
2276 int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2277 {
2278         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2279         struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2280         static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2281         const u8 *maddr = _maddr;
2282         struct iwl_trans_txq_scd_cfg cfg = {
2283                 .fifo = vif->type == NL80211_IFTYPE_AP ?
2284                         IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2285                 .sta_id = msta->sta_id,
2286                 .tid = 0,
2287                 .aggregate = false,
2288                 .frame_limit = IWL_FRAME_LIMIT,
2289         };
2290         unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2291         int ret;
2292
2293         lockdep_assert_held(&mvm->mutex);
2294
2295         if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2296                     vif->type != NL80211_IFTYPE_ADHOC))
2297                 return -ENOTSUPP;
2298
2299         /*
2300          * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2301          * invalid, so make sure we use the queue we want.
2302          * Note that this is done here as we want to avoid making DQA
2303          * changes in mac80211 layer.
2304          */
2305         if (vif->type == NL80211_IFTYPE_ADHOC)
2306                 mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2307
2308         /*
2309          * While in previous FWs we had to exclude cab queue from TFD queue
2310          * mask, now it is needed as any other queue.
2311          */
2312         if (!iwl_mvm_has_new_tx_api(mvm) &&
2313             fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2314                 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2315                                    timeout);
2316                 msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2317         }
2318         ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2319                                          mvmvif->id, mvmvif->color);
2320         if (ret) {
2321                 iwl_mvm_dealloc_int_sta(mvm, msta);
2322                 return ret;
2323         }
2324
2325         /*
2326          * Enable cab queue after the ADD_STA command is sent.
2327          * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2328          * command with unknown station id, and for FW that doesn't support
2329          * station API since the cab queue is not included in the
2330          * tfd_queue_mask.
2331          */
2332         if (iwl_mvm_has_new_tx_api(mvm)) {
2333                 int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2334                                                     0,
2335                                                     timeout);
2336                 mvmvif->cab_queue = queue;
2337         } else if (!fw_has_api(&mvm->fw->ucode_capa,
2338                                IWL_UCODE_TLV_API_STA_TYPE))
2339                 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2340                                    timeout);
2341
2342         return 0;
2343 }
2344
2345 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2346                                     struct ieee80211_key_conf *keyconf,
2347                                     bool mcast)
2348 {
2349         union {
2350                 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2351                 struct iwl_mvm_add_sta_key_cmd cmd;
2352         } u = {};
2353         bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2354                                   IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2355         __le16 key_flags;
2356         int ret, size;
2357         u32 status;
2358
2359         /* This is a valid situation for GTK removal */
2360         if (sta_id == IWL_MVM_INVALID_STA)
2361                 return 0;
2362
2363         key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2364                                  STA_KEY_FLG_KEYID_MSK);
2365         key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2366         key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2367
2368         if (mcast)
2369                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2370
2371         /*
2372          * The fields assigned here are in the same location at the start
2373          * of the command, so we can do this union trick.
2374          */
2375         u.cmd.common.key_flags = key_flags;
2376         u.cmd.common.key_offset = keyconf->hw_key_idx;
2377         u.cmd.common.sta_id = sta_id;
2378
2379         size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2380
2381         status = ADD_STA_SUCCESS;
2382         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2383                                           &status);
2384
2385         switch (status) {
2386         case ADD_STA_SUCCESS:
2387                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2388                 break;
2389         default:
2390                 ret = -EIO;
2391                 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2392                 break;
2393         }
2394
2395         return ret;
2396 }
2397
2398 /*
2399  * Send the FW a request to remove the station from it's internal data
2400  * structures, and in addition remove it from the local data structure.
2401  */
2402 int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2403 {
2404         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2405         int ret;
2406
2407         lockdep_assert_held(&mvm->mutex);
2408
2409         iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2410
2411         iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
2412
2413         ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2414         if (ret)
2415                 IWL_WARN(mvm, "Failed sending remove station\n");
2416
2417         return ret;
2418 }
2419
2420 #define IWL_MAX_RX_BA_SESSIONS 16
2421
2422 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2423 {
2424         struct iwl_mvm_delba_notif notif = {
2425                 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2426                 .metadata.sync = 1,
2427                 .delba.baid = baid,
2428         };
2429         iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2430 };
2431
2432 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2433                                  struct iwl_mvm_baid_data *data)
2434 {
2435         int i;
2436
2437         iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2438
2439         for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2440                 int j;
2441                 struct iwl_mvm_reorder_buffer *reorder_buf =
2442                         &data->reorder_buf[i];
2443                 struct iwl_mvm_reorder_buf_entry *entries =
2444                         &data->entries[i * data->entries_per_queue];
2445
2446                 spin_lock_bh(&reorder_buf->lock);
2447                 if (likely(!reorder_buf->num_stored)) {
2448                         spin_unlock_bh(&reorder_buf->lock);
2449                         continue;
2450                 }
2451
2452                 /*
2453                  * This shouldn't happen in regular DELBA since the internal
2454                  * delBA notification should trigger a release of all frames in
2455                  * the reorder buffer.
2456                  */
2457                 WARN_ON(1);
2458
2459                 for (j = 0; j < reorder_buf->buf_size; j++)
2460                         __skb_queue_purge(&entries[j].e.frames);
2461                 /*
2462                  * Prevent timer re-arm. This prevents a very far fetched case
2463                  * where we timed out on the notification. There may be prior
2464                  * RX frames pending in the RX queue before the notification
2465                  * that might get processed between now and the actual deletion
2466                  * and we would re-arm the timer although we are deleting the
2467                  * reorder buffer.
2468                  */
2469                 reorder_buf->removed = true;
2470                 spin_unlock_bh(&reorder_buf->lock);
2471                 del_timer_sync(&reorder_buf->reorder_timer);
2472         }
2473 }
2474
2475 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2476                                         struct iwl_mvm_baid_data *data,
2477                                         u16 ssn, u16 buf_size)
2478 {
2479         int i;
2480
2481         for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2482                 struct iwl_mvm_reorder_buffer *reorder_buf =
2483                         &data->reorder_buf[i];
2484                 struct iwl_mvm_reorder_buf_entry *entries =
2485                         &data->entries[i * data->entries_per_queue];
2486                 int j;
2487
2488                 reorder_buf->num_stored = 0;
2489                 reorder_buf->head_sn = ssn;
2490                 reorder_buf->buf_size = buf_size;
2491                 /* rx reorder timer */
2492                 timer_setup(&reorder_buf->reorder_timer,
2493                             iwl_mvm_reorder_timer_expired, 0);
2494                 spin_lock_init(&reorder_buf->lock);
2495                 reorder_buf->mvm = mvm;
2496                 reorder_buf->queue = i;
2497                 reorder_buf->valid = false;
2498                 for (j = 0; j < reorder_buf->buf_size; j++)
2499                         __skb_queue_head_init(&entries[j].e.frames);
2500         }
2501 }
2502
2503 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2504                        int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2505 {
2506         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2507         struct iwl_mvm_add_sta_cmd cmd = {};
2508         struct iwl_mvm_baid_data *baid_data = NULL;
2509         int ret;
2510         u32 status;
2511
2512         lockdep_assert_held(&mvm->mutex);
2513
2514         if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2515                 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2516                 return -ENOSPC;
2517         }
2518
2519         if (iwl_mvm_has_new_rx_api(mvm) && start) {
2520                 u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2521
2522                 /* sparse doesn't like the __align() so don't check */
2523 #ifndef __CHECKER__
2524                 /*
2525                  * The division below will be OK if either the cache line size
2526                  * can be divided by the entry size (ALIGN will round up) or if
2527                  * if the entry size can be divided by the cache line size, in
2528                  * which case the ALIGN() will do nothing.
2529                  */
2530                 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2531                              sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2532 #endif
2533
2534                 /*
2535                  * Upward align the reorder buffer size to fill an entire cache
2536                  * line for each queue, to avoid sharing cache lines between
2537                  * different queues.
2538                  */
2539                 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2540
2541                 /*
2542                  * Allocate here so if allocation fails we can bail out early
2543                  * before starting the BA session in the firmware
2544                  */
2545                 baid_data = kzalloc(sizeof(*baid_data) +
2546                                     mvm->trans->num_rx_queues *
2547                                     reorder_buf_size,
2548                                     GFP_KERNEL);
2549                 if (!baid_data)
2550                         return -ENOMEM;
2551
2552                 /*
2553                  * This division is why we need the above BUILD_BUG_ON(),
2554                  * if that doesn't hold then this will not be right.
2555                  */
2556                 baid_data->entries_per_queue =
2557                         reorder_buf_size / sizeof(baid_data->entries[0]);
2558         }
2559
2560         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2561         cmd.sta_id = mvm_sta->sta_id;
2562         cmd.add_modify = STA_MODE_MODIFY;
2563         if (start) {
2564                 cmd.add_immediate_ba_tid = (u8) tid;
2565                 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2566                 cmd.rx_ba_window = cpu_to_le16(buf_size);
2567         } else {
2568                 cmd.remove_immediate_ba_tid = (u8) tid;
2569         }
2570         cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2571                                   STA_MODIFY_REMOVE_BA_TID;
2572
2573         status = ADD_STA_SUCCESS;
2574         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2575                                           iwl_mvm_add_sta_cmd_size(mvm),
2576                                           &cmd, &status);
2577         if (ret)
2578                 goto out_free;
2579
2580         switch (status & IWL_ADD_STA_STATUS_MASK) {
2581         case ADD_STA_SUCCESS:
2582                 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2583                              start ? "start" : "stopp");
2584                 break;
2585         case ADD_STA_IMMEDIATE_BA_FAILURE:
2586                 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2587                 ret = -ENOSPC;
2588                 break;
2589         default:
2590                 ret = -EIO;
2591                 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2592                         start ? "start" : "stopp", status);
2593                 break;
2594         }
2595
2596         if (ret)
2597                 goto out_free;
2598
2599         if (start) {
2600                 u8 baid;
2601
2602                 mvm->rx_ba_sessions++;
2603
2604                 if (!iwl_mvm_has_new_rx_api(mvm))
2605                         return 0;
2606
2607                 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2608                         ret = -EINVAL;
2609                         goto out_free;
2610                 }
2611                 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2612                             IWL_ADD_STA_BAID_SHIFT);
2613                 baid_data->baid = baid;
2614                 baid_data->timeout = timeout;
2615                 baid_data->last_rx = jiffies;
2616                 baid_data->rcu_ptr = &mvm->baid_map[baid];
2617                 timer_setup(&baid_data->session_timer,
2618                             iwl_mvm_rx_agg_session_expired, 0);
2619                 baid_data->mvm = mvm;
2620                 baid_data->tid = tid;
2621                 baid_data->sta_id = mvm_sta->sta_id;
2622
2623                 mvm_sta->tid_to_baid[tid] = baid;
2624                 if (timeout)
2625                         mod_timer(&baid_data->session_timer,
2626                                   TU_TO_EXP_TIME(timeout * 2));
2627
2628                 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2629                 /*
2630                  * protect the BA data with RCU to cover a case where our
2631                  * internal RX sync mechanism will timeout (not that it's
2632                  * supposed to happen) and we will free the session data while
2633                  * RX is being processed in parallel
2634                  */
2635                 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2636                              mvm_sta->sta_id, tid, baid);
2637                 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2638                 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2639         } else  {
2640                 u8 baid = mvm_sta->tid_to_baid[tid];
2641
2642                 if (mvm->rx_ba_sessions > 0)
2643                         /* check that restart flow didn't zero the counter */
2644                         mvm->rx_ba_sessions--;
2645                 if (!iwl_mvm_has_new_rx_api(mvm))
2646                         return 0;
2647
2648                 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2649                         return -EINVAL;
2650
2651                 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2652                 if (WARN_ON(!baid_data))
2653                         return -EINVAL;
2654
2655                 /* synchronize all rx queues so we can safely delete */
2656                 iwl_mvm_free_reorder(mvm, baid_data);
2657                 del_timer_sync(&baid_data->session_timer);
2658                 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2659                 kfree_rcu(baid_data, rcu_head);
2660                 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2661         }
2662         return 0;
2663
2664 out_free:
2665         kfree(baid_data);
2666         return ret;
2667 }
2668
2669 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2670                        int tid, u8 queue, bool start)
2671 {
2672         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2673         struct iwl_mvm_add_sta_cmd cmd = {};
2674         int ret;
2675         u32 status;
2676
2677         lockdep_assert_held(&mvm->mutex);
2678
2679         if (start) {
2680                 mvm_sta->tfd_queue_msk |= BIT(queue);
2681                 mvm_sta->tid_disable_agg &= ~BIT(tid);
2682         } else {
2683                 /* In DQA-mode the queue isn't removed on agg termination */
2684                 mvm_sta->tid_disable_agg |= BIT(tid);
2685         }
2686
2687         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2688         cmd.sta_id = mvm_sta->sta_id;
2689         cmd.add_modify = STA_MODE_MODIFY;
2690         if (!iwl_mvm_has_new_tx_api(mvm))
2691                 cmd.modify_mask = STA_MODIFY_QUEUES;
2692         cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2693         cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2694         cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2695
2696         status = ADD_STA_SUCCESS;
2697         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2698                                           iwl_mvm_add_sta_cmd_size(mvm),
2699                                           &cmd, &status);
2700         if (ret)
2701                 return ret;
2702
2703         switch (status & IWL_ADD_STA_STATUS_MASK) {
2704         case ADD_STA_SUCCESS:
2705                 break;
2706         default:
2707                 ret = -EIO;
2708                 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2709                         start ? "start" : "stopp", status);
2710                 break;
2711         }
2712
2713         return ret;
2714 }
2715
2716 const u8 tid_to_mac80211_ac[] = {
2717         IEEE80211_AC_BE,
2718         IEEE80211_AC_BK,
2719         IEEE80211_AC_BK,
2720         IEEE80211_AC_BE,
2721         IEEE80211_AC_VI,
2722         IEEE80211_AC_VI,
2723         IEEE80211_AC_VO,
2724         IEEE80211_AC_VO,
2725         IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2726 };
2727
2728 static const u8 tid_to_ucode_ac[] = {
2729         AC_BE,
2730         AC_BK,
2731         AC_BK,
2732         AC_BE,
2733         AC_VI,
2734         AC_VI,
2735         AC_VO,
2736         AC_VO,
2737 };
2738
2739 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2740                              struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2741 {
2742         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2743         struct iwl_mvm_tid_data *tid_data;
2744         u16 normalized_ssn;
2745         u16 txq_id;
2746         int ret;
2747
2748         if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2749                 return -EINVAL;
2750
2751         if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2752             mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2753                 IWL_ERR(mvm,
2754                         "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2755                         mvmsta->tid_data[tid].state);
2756                 return -ENXIO;
2757         }
2758
2759         lockdep_assert_held(&mvm->mutex);
2760
2761         if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2762             iwl_mvm_has_new_tx_api(mvm)) {
2763                 u8 ac = tid_to_mac80211_ac[tid];
2764
2765                 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2766                 if (ret)
2767                         return ret;
2768         }
2769
2770         spin_lock_bh(&mvmsta->lock);
2771
2772         /* possible race condition - we entered D0i3 while starting agg */
2773         if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2774                 spin_unlock_bh(&mvmsta->lock);
2775                 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2776                 return -EIO;
2777         }
2778
2779         /*
2780          * Note the possible cases:
2781          *  1. An enabled TXQ - TXQ needs to become agg'ed
2782          *  2. The TXQ hasn't yet been enabled, so find a free one and mark
2783          *      it as reserved
2784          */
2785         txq_id = mvmsta->tid_data[tid].txq_id;
2786         if (txq_id == IWL_MVM_INVALID_QUEUE) {
2787                 ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2788                                               IWL_MVM_DQA_MIN_DATA_QUEUE,
2789                                               IWL_MVM_DQA_MAX_DATA_QUEUE);
2790                 if (ret < 0) {
2791                         IWL_ERR(mvm, "Failed to allocate agg queue\n");
2792                         goto out;
2793                 }
2794
2795                 txq_id = ret;
2796
2797                 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2798                 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2799         } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2800                 ret = -ENXIO;
2801                 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2802                         tid, IWL_MAX_HW_QUEUES - 1);
2803                 goto out;
2804
2805         } else if (unlikely(mvm->queue_info[txq_id].status ==
2806                             IWL_MVM_QUEUE_SHARED)) {
2807                 ret = -ENXIO;
2808                 IWL_DEBUG_TX_QUEUES(mvm,
2809                                     "Can't start tid %d agg on shared queue!\n",
2810                                     tid);
2811                 goto out;
2812         }
2813
2814         IWL_DEBUG_TX_QUEUES(mvm,
2815                             "AGG for tid %d will be on queue #%d\n",
2816                             tid, txq_id);
2817
2818         tid_data = &mvmsta->tid_data[tid];
2819         tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2820         tid_data->txq_id = txq_id;
2821         *ssn = tid_data->ssn;
2822
2823         IWL_DEBUG_TX_QUEUES(mvm,
2824                             "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2825                             mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2826                             tid_data->next_reclaimed);
2827
2828         /*
2829          * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2830          * to align the wrap around of ssn so we compare relevant values.
2831          */
2832         normalized_ssn = tid_data->ssn;
2833         if (mvm->trans->cfg->gen2)
2834                 normalized_ssn &= 0xff;
2835
2836         if (normalized_ssn == tid_data->next_reclaimed) {
2837                 tid_data->state = IWL_AGG_STARTING;
2838                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2839         } else {
2840                 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2841         }
2842
2843         ret = 0;
2844
2845 out:
2846         spin_unlock_bh(&mvmsta->lock);
2847
2848         return ret;
2849 }
2850
2851 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2852                             struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2853                             bool amsdu)
2854 {
2855         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2856         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2857         unsigned int wdg_timeout =
2858                 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2859         int queue, ret;
2860         bool alloc_queue = true;
2861         enum iwl_mvm_queue_status queue_status;
2862         u16 ssn;
2863
2864         struct iwl_trans_txq_scd_cfg cfg = {
2865                 .sta_id = mvmsta->sta_id,
2866                 .tid = tid,
2867                 .frame_limit = buf_size,
2868                 .aggregate = true,
2869         };
2870
2871         /*
2872          * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2873          * manager, so this function should never be called in this case.
2874          */
2875         if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2876                 return -EINVAL;
2877
2878         BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2879                      != IWL_MAX_TID_COUNT);
2880
2881         spin_lock_bh(&mvmsta->lock);
2882         ssn = tid_data->ssn;
2883         queue = tid_data->txq_id;
2884         tid_data->state = IWL_AGG_ON;
2885         mvmsta->agg_tids |= BIT(tid);
2886         tid_data->ssn = 0xffff;
2887         tid_data->amsdu_in_ampdu_allowed = amsdu;
2888         spin_unlock_bh(&mvmsta->lock);
2889
2890         if (iwl_mvm_has_new_tx_api(mvm)) {
2891                 /*
2892                  * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2893                  * would have failed, so if we are here there is no need to
2894                  * allocate a queue.
2895                  * However, if aggregation size is different than the default
2896                  * size, the scheduler should be reconfigured.
2897                  * We cannot do this with the new TX API, so return unsupported
2898                  * for now, until it will be offloaded to firmware..
2899                  * Note that if SCD default value changes - this condition
2900                  * should be updated as well.
2901                  */
2902                 if (buf_size < IWL_FRAME_LIMIT)
2903                         return -ENOTSUPP;
2904
2905                 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2906                 if (ret)
2907                         return -EIO;
2908                 goto out;
2909         }
2910
2911         cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2912
2913         queue_status = mvm->queue_info[queue].status;
2914
2915         /* Maybe there is no need to even alloc a queue... */
2916         if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2917                 alloc_queue = false;
2918
2919         /*
2920          * Only reconfig the SCD for the queue if the window size has
2921          * changed from current (become smaller)
2922          */
2923         if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2924                 /*
2925                  * If reconfiguring an existing queue, it first must be
2926                  * drained
2927                  */
2928                 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2929                                                      BIT(queue));
2930                 if (ret) {
2931                         IWL_ERR(mvm,
2932                                 "Error draining queue before reconfig\n");
2933                         return ret;
2934                 }
2935
2936                 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2937                                            mvmsta->sta_id, tid,
2938                                            buf_size, ssn);
2939                 if (ret) {
2940                         IWL_ERR(mvm,
2941                                 "Error reconfiguring TXQ #%d\n", queue);
2942                         return ret;
2943                 }
2944         }
2945
2946         if (alloc_queue)
2947                 iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2948                                    &cfg, wdg_timeout);
2949
2950         /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2951         if (queue_status != IWL_MVM_QUEUE_SHARED) {
2952                 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2953                 if (ret)
2954                         return -EIO;
2955         }
2956
2957         /* No need to mark as reserved */
2958         mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2959
2960 out:
2961         /*
2962          * Even though in theory the peer could have different
2963          * aggregation reorder buffer sizes for different sessions,
2964          * our ucode doesn't allow for that and has a global limit
2965          * for each station. Therefore, use the minimum of all the
2966          * aggregation sessions and our default value.
2967          */
2968         mvmsta->max_agg_bufsize =
2969                 min(mvmsta->max_agg_bufsize, buf_size);
2970         mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2971
2972         IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2973                      sta->addr, tid);
2974
2975         return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq, false);
2976 }
2977
2978 static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2979                                         struct iwl_mvm_sta *mvmsta,
2980                                         struct iwl_mvm_tid_data *tid_data)
2981 {
2982         u16 txq_id = tid_data->txq_id;
2983
2984         lockdep_assert_held(&mvm->mutex);
2985
2986         if (iwl_mvm_has_new_tx_api(mvm))
2987                 return;
2988
2989         /*
2990          * The TXQ is marked as reserved only if no traffic came through yet
2991          * This means no traffic has been sent on this TID (agg'd or not), so
2992          * we no longer have use for the queue. Since it hasn't even been
2993          * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2994          * free.
2995          */
2996         if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
2997                 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2998                 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
2999         }
3000 }
3001
3002 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3003                             struct ieee80211_sta *sta, u16 tid)
3004 {
3005         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3006         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3007         u16 txq_id;
3008         int err;
3009
3010         /*
3011          * If mac80211 is cleaning its state, then say that we finished since
3012          * our state has been cleared anyway.
3013          */
3014         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3015                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3016                 return 0;
3017         }
3018
3019         spin_lock_bh(&mvmsta->lock);
3020
3021         txq_id = tid_data->txq_id;
3022
3023         IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3024                             mvmsta->sta_id, tid, txq_id, tid_data->state);
3025
3026         mvmsta->agg_tids &= ~BIT(tid);
3027
3028         iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3029
3030         switch (tid_data->state) {
3031         case IWL_AGG_ON:
3032                 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3033
3034                 IWL_DEBUG_TX_QUEUES(mvm,
3035                                     "ssn = %d, next_recl = %d\n",
3036                                     tid_data->ssn, tid_data->next_reclaimed);
3037
3038                 tid_data->ssn = 0xffff;
3039                 tid_data->state = IWL_AGG_OFF;
3040                 spin_unlock_bh(&mvmsta->lock);
3041
3042                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3043
3044                 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3045                 return 0;
3046         case IWL_AGG_STARTING:
3047         case IWL_EMPTYING_HW_QUEUE_ADDBA:
3048                 /*
3049                  * The agg session has been stopped before it was set up. This
3050                  * can happen when the AddBA timer times out for example.
3051                  */
3052
3053                 /* No barriers since we are under mutex */
3054                 lockdep_assert_held(&mvm->mutex);
3055
3056                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3057                 tid_data->state = IWL_AGG_OFF;
3058                 err = 0;
3059                 break;
3060         default:
3061                 IWL_ERR(mvm,
3062                         "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3063                         mvmsta->sta_id, tid, tid_data->state);
3064                 IWL_ERR(mvm,
3065                         "\ttid_data->txq_id = %d\n", tid_data->txq_id);
3066                 err = -EINVAL;
3067         }
3068
3069         spin_unlock_bh(&mvmsta->lock);
3070
3071         return err;
3072 }
3073
3074 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3075                             struct ieee80211_sta *sta, u16 tid)
3076 {
3077         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3078         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3079         u16 txq_id;
3080         enum iwl_mvm_agg_state old_state;
3081
3082         /*
3083          * First set the agg state to OFF to avoid calling
3084          * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3085          */
3086         spin_lock_bh(&mvmsta->lock);
3087         txq_id = tid_data->txq_id;
3088         IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3089                             mvmsta->sta_id, tid, txq_id, tid_data->state);
3090         old_state = tid_data->state;
3091         tid_data->state = IWL_AGG_OFF;
3092         mvmsta->agg_tids &= ~BIT(tid);
3093         spin_unlock_bh(&mvmsta->lock);
3094
3095         iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3096
3097         if (old_state >= IWL_AGG_ON) {
3098                 iwl_mvm_drain_sta(mvm, mvmsta, true);
3099
3100                 if (iwl_mvm_has_new_tx_api(mvm)) {
3101                         if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3102                                                    BIT(tid), 0))
3103                                 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3104                         iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3105                 } else {
3106                         if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3107                                 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3108                         iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3109                 }
3110
3111                 iwl_mvm_drain_sta(mvm, mvmsta, false);
3112
3113                 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3114         }
3115
3116         return 0;
3117 }
3118
3119 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3120 {
3121         int i, max = -1, max_offs = -1;
3122
3123         lockdep_assert_held(&mvm->mutex);
3124
3125         /* Pick the unused key offset with the highest 'deleted'
3126          * counter. Every time a key is deleted, all the counters
3127          * are incremented and the one that was just deleted is
3128          * reset to zero. Thus, the highest counter is the one
3129          * that was deleted longest ago. Pick that one.
3130          */
3131         for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3132                 if (test_bit(i, mvm->fw_key_table))
3133                         continue;
3134                 if (mvm->fw_key_deleted[i] > max) {
3135                         max = mvm->fw_key_deleted[i];
3136                         max_offs = i;
3137                 }
3138         }
3139
3140         if (max_offs < 0)
3141                 return STA_KEY_IDX_INVALID;
3142
3143         return max_offs;
3144 }
3145
3146 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3147                                                struct ieee80211_vif *vif,
3148                                                struct ieee80211_sta *sta)
3149 {
3150         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3151
3152         if (sta)
3153                 return iwl_mvm_sta_from_mac80211(sta);
3154
3155         /*
3156          * The device expects GTKs for station interfaces to be
3157          * installed as GTKs for the AP station. If we have no
3158          * station ID, then use AP's station ID.
3159          */
3160         if (vif->type == NL80211_IFTYPE_STATION &&
3161             mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3162                 u8 sta_id = mvmvif->ap_sta_id;
3163
3164                 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3165                                             lockdep_is_held(&mvm->mutex));
3166
3167                 /*
3168                  * It is possible that the 'sta' parameter is NULL,
3169                  * for example when a GTK is removed - the sta_id will then
3170                  * be the AP ID, and no station was passed by mac80211.
3171                  */
3172                 if (IS_ERR_OR_NULL(sta))
3173                         return NULL;
3174
3175                 return iwl_mvm_sta_from_mac80211(sta);
3176         }
3177
3178         return NULL;
3179 }
3180
3181 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3182                                 u32 sta_id,
3183                                 struct ieee80211_key_conf *key, bool mcast,
3184                                 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3185                                 u8 key_offset, bool mfp)
3186 {
3187         union {
3188                 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3189                 struct iwl_mvm_add_sta_key_cmd cmd;
3190         } u = {};
3191         __le16 key_flags;
3192         int ret;
3193         u32 status;
3194         u16 keyidx;
3195         u64 pn = 0;
3196         int i, size;
3197         bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3198                                   IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3199
3200         if (sta_id == IWL_MVM_INVALID_STA)
3201                 return -EINVAL;
3202
3203         keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3204                  STA_KEY_FLG_KEYID_MSK;
3205         key_flags = cpu_to_le16(keyidx);
3206         key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3207
3208         switch (key->cipher) {
3209         case WLAN_CIPHER_SUITE_TKIP:
3210                 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3211                 if (new_api) {
3212                         memcpy((void *)&u.cmd.tx_mic_key,
3213                                &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3214                                IWL_MIC_KEY_SIZE);
3215
3216                         memcpy((void *)&u.cmd.rx_mic_key,
3217                                &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3218                                IWL_MIC_KEY_SIZE);
3219                         pn = atomic64_read(&key->tx_pn);
3220
3221                 } else {
3222                         u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3223                         for (i = 0; i < 5; i++)
3224                                 u.cmd_v1.tkip_rx_ttak[i] =
3225                                         cpu_to_le16(tkip_p1k[i]);
3226                 }
3227                 memcpy(u.cmd.common.key, key->key, key->keylen);
3228                 break;
3229         case WLAN_CIPHER_SUITE_CCMP:
3230                 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3231                 memcpy(u.cmd.common.key, key->key, key->keylen);
3232                 if (new_api)
3233                         pn = atomic64_read(&key->tx_pn);
3234                 break;
3235         case WLAN_CIPHER_SUITE_WEP104:
3236                 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3237                 /* fall through */
3238         case WLAN_CIPHER_SUITE_WEP40:
3239                 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3240                 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3241                 break;
3242         case WLAN_CIPHER_SUITE_GCMP_256:
3243                 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3244                 /* fall through */
3245         case WLAN_CIPHER_SUITE_GCMP:
3246                 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3247                 memcpy(u.cmd.common.key, key->key, key->keylen);
3248                 if (new_api)
3249                         pn = atomic64_read(&key->tx_pn);
3250                 break;
3251         default:
3252                 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3253                 memcpy(u.cmd.common.key, key->key, key->keylen);
3254         }
3255
3256         if (mcast)
3257                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3258         if (mfp)
3259                 key_flags |= cpu_to_le16(STA_KEY_MFP);
3260
3261         u.cmd.common.key_offset = key_offset;
3262         u.cmd.common.key_flags = key_flags;
3263         u.cmd.common.sta_id = sta_id;
3264
3265         if (new_api) {
3266                 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3267                 size = sizeof(u.cmd);
3268         } else {
3269                 size = sizeof(u.cmd_v1);
3270         }
3271
3272         status = ADD_STA_SUCCESS;
3273         if (cmd_flags & CMD_ASYNC)
3274                 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3275                                            &u.cmd);
3276         else
3277                 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3278                                                   &u.cmd, &status);
3279
3280         switch (status) {
3281         case ADD_STA_SUCCESS:
3282                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3283                 break;
3284         default:
3285                 ret = -EIO;
3286                 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3287                 break;
3288         }
3289
3290         return ret;
3291 }
3292
3293 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3294                                  struct ieee80211_key_conf *keyconf,
3295                                  u8 sta_id, bool remove_key)
3296 {
3297         struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3298
3299         /* verify the key details match the required command's expectations */
3300         if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3301                     (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3302                     (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3303                      keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3304                      keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3305                 return -EINVAL;
3306
3307         if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3308                     keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3309                 return -EINVAL;
3310
3311         igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3312         igtk_cmd.sta_id = cpu_to_le32(sta_id);
3313
3314         if (remove_key) {
3315                 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3316         } else {
3317                 struct ieee80211_key_seq seq;
3318                 const u8 *pn;
3319
3320                 switch (keyconf->cipher) {
3321                 case WLAN_CIPHER_SUITE_AES_CMAC:
3322                         igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3323                         break;
3324                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3325                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3326                         igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3327                         break;
3328                 default:
3329                         return -EINVAL;
3330                 }
3331
3332                 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3333                 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3334                         igtk_cmd.ctrl_flags |=
3335                                 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3336                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3337                 pn = seq.aes_cmac.pn;
3338                 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3339                                                        ((u64) pn[4] << 8) |
3340                                                        ((u64) pn[3] << 16) |
3341                                                        ((u64) pn[2] << 24) |
3342                                                        ((u64) pn[1] << 32) |
3343                                                        ((u64) pn[0] << 40));
3344         }
3345
3346         IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3347                        remove_key ? "removing" : "installing",
3348                        igtk_cmd.sta_id);
3349
3350         if (!iwl_mvm_has_new_rx_api(mvm)) {
3351                 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3352                         .ctrl_flags = igtk_cmd.ctrl_flags,
3353                         .key_id = igtk_cmd.key_id,
3354                         .sta_id = igtk_cmd.sta_id,
3355                         .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3356                 };
3357
3358                 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3359                        ARRAY_SIZE(igtk_cmd_v1.igtk));
3360                 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3361                                             sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3362         }
3363         return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3364                                     sizeof(igtk_cmd), &igtk_cmd);
3365 }
3366
3367
3368 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3369                                        struct ieee80211_vif *vif,
3370                                        struct ieee80211_sta *sta)
3371 {
3372         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3373
3374         if (sta)
3375                 return sta->addr;
3376
3377         if (vif->type == NL80211_IFTYPE_STATION &&
3378             mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3379                 u8 sta_id = mvmvif->ap_sta_id;
3380                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3381                                                 lockdep_is_held(&mvm->mutex));
3382                 return sta->addr;
3383         }
3384
3385
3386         return NULL;
3387 }
3388
3389 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3390                                  struct ieee80211_vif *vif,
3391                                  struct ieee80211_sta *sta,
3392                                  struct ieee80211_key_conf *keyconf,
3393                                  u8 key_offset,
3394                                  bool mcast)
3395 {
3396         int ret;
3397         const u8 *addr;
3398         struct ieee80211_key_seq seq;
3399         u16 p1k[5];
3400         u32 sta_id;
3401         bool mfp = false;
3402
3403         if (sta) {
3404                 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3405
3406                 sta_id = mvm_sta->sta_id;
3407                 mfp = sta->mfp;
3408         } else if (vif->type == NL80211_IFTYPE_AP &&
3409                    !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3410                 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3411
3412                 sta_id = mvmvif->mcast_sta.sta_id;
3413         } else {
3414                 IWL_ERR(mvm, "Failed to find station id\n");
3415                 return -EINVAL;
3416         }
3417
3418         switch (keyconf->cipher) {
3419         case WLAN_CIPHER_SUITE_TKIP:
3420                 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3421                 /* get phase 1 key from mac80211 */
3422                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3423                 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3424                 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3425                                            seq.tkip.iv32, p1k, 0, key_offset,
3426                                            mfp);
3427                 break;
3428         case WLAN_CIPHER_SUITE_CCMP:
3429         case WLAN_CIPHER_SUITE_WEP40:
3430         case WLAN_CIPHER_SUITE_WEP104:
3431         case WLAN_CIPHER_SUITE_GCMP:
3432         case WLAN_CIPHER_SUITE_GCMP_256:
3433                 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3434                                            0, NULL, 0, key_offset, mfp);
3435                 break;
3436         default:
3437                 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3438                                            0, NULL, 0, key_offset, mfp);
3439         }
3440
3441         return ret;
3442 }
3443
3444 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3445                         struct ieee80211_vif *vif,
3446                         struct ieee80211_sta *sta,
3447                         struct ieee80211_key_conf *keyconf,
3448                         u8 key_offset)
3449 {
3450         bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3451         struct iwl_mvm_sta *mvm_sta;
3452         u8 sta_id = IWL_MVM_INVALID_STA;
3453         int ret;
3454         static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3455
3456         lockdep_assert_held(&mvm->mutex);
3457
3458         if (vif->type != NL80211_IFTYPE_AP ||
3459             keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3460                 /* Get the station id from the mvm local station table */
3461                 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3462                 if (!mvm_sta) {
3463                         IWL_ERR(mvm, "Failed to find station\n");
3464                         return -EINVAL;
3465                 }
3466                 sta_id = mvm_sta->sta_id;
3467
3468                 /*
3469                  * It is possible that the 'sta' parameter is NULL, and thus
3470                  * there is a need to retrieve the sta from the local station
3471                  * table.
3472                  */
3473                 if (!sta) {
3474                         sta = rcu_dereference_protected(
3475                                 mvm->fw_id_to_mac_id[sta_id],
3476                                 lockdep_is_held(&mvm->mutex));
3477                         if (IS_ERR_OR_NULL(sta)) {
3478                                 IWL_ERR(mvm, "Invalid station id\n");
3479                                 return -EINVAL;
3480                         }
3481                 }
3482
3483                 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3484                         return -EINVAL;
3485         } else {
3486                 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3487
3488                 sta_id = mvmvif->mcast_sta.sta_id;
3489         }
3490
3491         if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3492             keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3493             keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3494                 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3495                 goto end;
3496         }
3497
3498         /* If the key_offset is not pre-assigned, we need to find a
3499          * new offset to use.  In normal cases, the offset is not
3500          * pre-assigned, but during HW_RESTART we want to reuse the
3501          * same indices, so we pass them when this function is called.
3502          *
3503          * In D3 entry, we need to hardcoded the indices (because the
3504          * firmware hardcodes the PTK offset to 0).  In this case, we
3505          * need to make sure we don't overwrite the hw_key_idx in the
3506          * keyconf structure, because otherwise we cannot configure
3507          * the original ones back when resuming.
3508          */
3509         if (key_offset == STA_KEY_IDX_INVALID) {
3510                 key_offset  = iwl_mvm_set_fw_key_idx(mvm);
3511                 if (key_offset == STA_KEY_IDX_INVALID)
3512                         return -ENOSPC;
3513                 keyconf->hw_key_idx = key_offset;
3514         }
3515
3516         ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3517         if (ret)
3518                 goto end;
3519
3520         /*
3521          * For WEP, the same key is used for multicast and unicast. Upload it
3522          * again, using the same key offset, and now pointing the other one
3523          * to the same key slot (offset).
3524          * If this fails, remove the original as well.
3525          */
3526         if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3527              keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3528             sta) {
3529                 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3530                                             key_offset, !mcast);
3531                 if (ret) {
3532                         __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3533                         goto end;
3534                 }
3535         }
3536
3537         __set_bit(key_offset, mvm->fw_key_table);
3538
3539 end:
3540         IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3541                       keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3542                       sta ? sta->addr : zero_addr, ret);
3543         return ret;
3544 }
3545
3546 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3547                            struct ieee80211_vif *vif,
3548                            struct ieee80211_sta *sta,
3549                            struct ieee80211_key_conf *keyconf)
3550 {
3551         bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3552         struct iwl_mvm_sta *mvm_sta;
3553         u8 sta_id = IWL_MVM_INVALID_STA;
3554         int ret, i;
3555
3556         lockdep_assert_held(&mvm->mutex);
3557
3558         /* Get the station from the mvm local station table */
3559         mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3560         if (mvm_sta)
3561                 sta_id = mvm_sta->sta_id;
3562         else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3563                 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3564
3565
3566         IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3567                       keyconf->keyidx, sta_id);
3568
3569         if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3570                         keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3571                         keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
3572                 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3573
3574         if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3575                 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3576                         keyconf->hw_key_idx);
3577                 return -ENOENT;
3578         }
3579
3580         /* track which key was deleted last */
3581         for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3582                 if (mvm->fw_key_deleted[i] < U8_MAX)
3583                         mvm->fw_key_deleted[i]++;
3584         }
3585         mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3586
3587         if (sta && !mvm_sta) {
3588                 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3589                 return 0;
3590         }
3591
3592         ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3593         if (ret)
3594                 return ret;
3595
3596         /* delete WEP key twice to get rid of (now useless) offset */
3597         if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3598             keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3599                 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3600
3601         return ret;
3602 }
3603
3604 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3605                              struct ieee80211_vif *vif,
3606                              struct ieee80211_key_conf *keyconf,
3607                              struct ieee80211_sta *sta, u32 iv32,
3608                              u16 *phase1key)
3609 {
3610         struct iwl_mvm_sta *mvm_sta;
3611         bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3612         bool mfp = sta ? sta->mfp : false;
3613
3614         rcu_read_lock();
3615
3616         mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3617         if (WARN_ON_ONCE(!mvm_sta))
3618                 goto unlock;
3619         iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3620                              iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3621                              mfp);
3622
3623  unlock:
3624         rcu_read_unlock();
3625 }
3626
3627 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3628                                 struct ieee80211_sta *sta)
3629 {
3630         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3631         struct iwl_mvm_add_sta_cmd cmd = {
3632                 .add_modify = STA_MODE_MODIFY,
3633                 .sta_id = mvmsta->sta_id,
3634                 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3635                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3636         };
3637         int ret;
3638
3639         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3640                                    iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3641         if (ret)
3642                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3643 }
3644
3645 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3646                                        struct ieee80211_sta *sta,
3647                                        enum ieee80211_frame_release_type reason,
3648                                        u16 cnt, u16 tids, bool more_data,
3649                                        bool single_sta_queue)
3650 {
3651         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3652         struct iwl_mvm_add_sta_cmd cmd = {
3653                 .add_modify = STA_MODE_MODIFY,
3654                 .sta_id = mvmsta->sta_id,
3655                 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3656                 .sleep_tx_count = cpu_to_le16(cnt),
3657                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3658         };
3659         int tid, ret;
3660         unsigned long _tids = tids;
3661
3662         /* convert TIDs to ACs - we don't support TSPEC so that's OK
3663          * Note that this field is reserved and unused by firmware not
3664          * supporting GO uAPSD, so it's safe to always do this.
3665          */
3666         for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3667                 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3668
3669         /* If we're releasing frames from aggregation or dqa queues then check
3670          * if all the queues that we're releasing frames from, combined, have:
3671          *  - more frames than the service period, in which case more_data
3672          *    needs to be set
3673          *  - fewer than 'cnt' frames, in which case we need to adjust the
3674          *    firmware command (but do that unconditionally)
3675          */
3676         if (single_sta_queue) {
3677                 int remaining = cnt;
3678                 int sleep_tx_count;
3679
3680                 spin_lock_bh(&mvmsta->lock);
3681                 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3682                         struct iwl_mvm_tid_data *tid_data;
3683                         u16 n_queued;
3684
3685                         tid_data = &mvmsta->tid_data[tid];
3686
3687                         n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3688                         if (n_queued > remaining) {
3689                                 more_data = true;
3690                                 remaining = 0;
3691                                 break;
3692                         }
3693                         remaining -= n_queued;
3694                 }
3695                 sleep_tx_count = cnt - remaining;
3696                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3697                         mvmsta->sleep_tx_count = sleep_tx_count;
3698                 spin_unlock_bh(&mvmsta->lock);
3699
3700                 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3701                 if (WARN_ON(cnt - remaining == 0)) {
3702                         ieee80211_sta_eosp(sta);
3703                         return;
3704                 }
3705         }
3706
3707         /* Note: this is ignored by firmware not supporting GO uAPSD */
3708         if (more_data)
3709                 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3710
3711         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3712                 mvmsta->next_status_eosp = true;
3713                 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3714         } else {
3715                 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3716         }
3717
3718         /* block the Tx queues until the FW updated the sleep Tx count */
3719         iwl_trans_block_txq_ptrs(mvm->trans, true);
3720
3721         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3722                                    CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3723                                    iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3724         if (ret)
3725                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3726 }
3727
3728 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3729                            struct iwl_rx_cmd_buffer *rxb)
3730 {
3731         struct iwl_rx_packet *pkt = rxb_addr(rxb);
3732         struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3733         struct ieee80211_sta *sta;
3734         u32 sta_id = le32_to_cpu(notif->sta_id);
3735
3736         if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3737                 return;
3738
3739         rcu_read_lock();
3740         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3741         if (!IS_ERR_OR_NULL(sta))
3742                 ieee80211_sta_eosp(sta);
3743         rcu_read_unlock();
3744 }
3745
3746 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3747                                    struct iwl_mvm_sta *mvmsta, bool disable)
3748 {
3749         struct iwl_mvm_add_sta_cmd cmd = {
3750                 .add_modify = STA_MODE_MODIFY,
3751                 .sta_id = mvmsta->sta_id,
3752                 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3753                 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3754                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3755         };
3756         int ret;
3757
3758         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3759                                    iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3760         if (ret)
3761                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3762 }
3763
3764 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3765                                       struct ieee80211_sta *sta,
3766                                       bool disable)
3767 {
3768         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3769
3770         spin_lock_bh(&mvm_sta->lock);
3771
3772         if (mvm_sta->disable_tx == disable) {
3773                 spin_unlock_bh(&mvm_sta->lock);
3774                 return;
3775         }
3776
3777         mvm_sta->disable_tx = disable;
3778
3779         /* Tell mac80211 to start/stop queuing tx for this station */
3780         ieee80211_sta_block_awake(mvm->hw, sta, disable);
3781
3782         iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3783
3784         spin_unlock_bh(&mvm_sta->lock);
3785 }
3786
3787 static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3788                                               struct iwl_mvm_vif *mvmvif,
3789                                               struct iwl_mvm_int_sta *sta,
3790                                               bool disable)
3791 {
3792         u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3793         struct iwl_mvm_add_sta_cmd cmd = {
3794                 .add_modify = STA_MODE_MODIFY,
3795                 .sta_id = sta->sta_id,
3796                 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3797                 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3798                 .mac_id_n_color = cpu_to_le32(id),
3799         };
3800         int ret;
3801
3802         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3803                                    iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3804         if (ret)
3805                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3806 }
3807
3808 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3809                                        struct iwl_mvm_vif *mvmvif,
3810                                        bool disable)
3811 {
3812         struct ieee80211_sta *sta;
3813         struct iwl_mvm_sta *mvm_sta;
3814         int i;
3815
3816         lockdep_assert_held(&mvm->mutex);
3817
3818         /* Block/unblock all the stations of the given mvmvif */
3819         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3820                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3821                                                 lockdep_is_held(&mvm->mutex));
3822                 if (IS_ERR_OR_NULL(sta))
3823                         continue;
3824
3825                 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3826                 if (mvm_sta->mac_id_n_color !=
3827                     FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3828                         continue;
3829
3830                 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3831         }
3832
3833         if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3834                 return;
3835
3836         /* Need to block/unblock also multicast station */
3837         if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3838                 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3839                                                   &mvmvif->mcast_sta, disable);
3840
3841         /*
3842          * Only unblock the broadcast station (FW blocks it for immediate
3843          * quiet, not the driver)
3844          */
3845         if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3846                 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3847                                                   &mvmvif->bcast_sta, disable);
3848 }
3849
3850 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3851 {
3852         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3853         struct iwl_mvm_sta *mvmsta;
3854
3855         rcu_read_lock();
3856
3857         mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3858
3859         if (!WARN_ON(!mvmsta))
3860                 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3861
3862         rcu_read_unlock();
3863 }
3864
3865 u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3866 {
3867         u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3868
3869         /*
3870          * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3871          * to align the wrap around of ssn so we compare relevant values.
3872          */
3873         if (mvm->trans->cfg->gen2)
3874                 sn &= 0xff;
3875
3876         return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3877 }