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