GNU Linux-libre 4.9.309-gnu1
[releases.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 Intel Deutschland GmbH
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of version 2 of the GNU General Public License as
14  * published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24  * USA
25  *
26  * The full GNU General Public License is included in this distribution
27  * in the file called COPYING.
28  *
29  * Contact Information:
30  *  Intel Linux Wireless <linuxwifi@intel.com>
31  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32  *
33  * BSD LICENSE
34  *
35  * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
36  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37  * Copyright(c) 2016 Intel Deutschland GmbH
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  *
44  *  * Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  *  * Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in
48  *    the documentation and/or other materials provided with the
49  *    distribution.
50  *  * Neither the name Intel Corporation nor the names of its
51  *    contributors may be used to endorse or promote products derived
52  *    from this software without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  *****************************************************************************/
67 #include <net/mac80211.h>
68
69 #include "mvm.h"
70 #include "sta.h"
71 #include "rs.h"
72
73 /*
74  * New version of ADD_STA_sta command added new fields at the end of the
75  * structure, so sending the size of the relevant API's structure is enough to
76  * support both API versions.
77  */
78 static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
79 {
80         return iwl_mvm_has_new_rx_api(mvm) ?
81                 sizeof(struct iwl_mvm_add_sta_cmd) :
82                 sizeof(struct iwl_mvm_add_sta_cmd_v7);
83 }
84
85 static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
86                                     enum nl80211_iftype iftype)
87 {
88         int sta_id;
89         u32 reserved_ids = 0;
90
91         BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
92         WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
93
94         lockdep_assert_held(&mvm->mutex);
95
96         /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
97         if (iftype != NL80211_IFTYPE_STATION)
98                 reserved_ids = BIT(0);
99
100         /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
101         for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
102                 if (BIT(sta_id) & reserved_ids)
103                         continue;
104
105                 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
106                                                lockdep_is_held(&mvm->mutex)))
107                         return sta_id;
108         }
109         return IWL_MVM_STATION_COUNT;
110 }
111
112 /* send station add/update command to firmware */
113 int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
114                            bool update, unsigned int flags)
115 {
116         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
117         struct iwl_mvm_add_sta_cmd add_sta_cmd = {
118                 .sta_id = mvm_sta->sta_id,
119                 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
120                 .add_modify = update ? 1 : 0,
121                 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
122                                                  STA_FLG_MIMO_EN_MSK),
123                 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
124         };
125         int ret;
126         u32 status;
127         u32 agg_size = 0, mpdu_dens = 0;
128
129         if (!update || (flags & STA_MODIFY_QUEUES)) {
130                 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
131                 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
132
133                 if (flags & STA_MODIFY_QUEUES)
134                         add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
135         }
136
137         switch (sta->bandwidth) {
138         case IEEE80211_STA_RX_BW_160:
139                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
140                 /* fall through */
141         case IEEE80211_STA_RX_BW_80:
142                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
143                 /* fall through */
144         case IEEE80211_STA_RX_BW_40:
145                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
146                 /* fall through */
147         case IEEE80211_STA_RX_BW_20:
148                 if (sta->ht_cap.ht_supported)
149                         add_sta_cmd.station_flags |=
150                                 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
151                 break;
152         }
153
154         switch (sta->rx_nss) {
155         case 1:
156                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
157                 break;
158         case 2:
159                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
160                 break;
161         case 3 ... 8:
162                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
163                 break;
164         }
165
166         switch (sta->smps_mode) {
167         case IEEE80211_SMPS_AUTOMATIC:
168         case IEEE80211_SMPS_NUM_MODES:
169                 WARN_ON(1);
170                 break;
171         case IEEE80211_SMPS_STATIC:
172                 /* override NSS */
173                 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
174                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
175                 break;
176         case IEEE80211_SMPS_DYNAMIC:
177                 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
178                 break;
179         case IEEE80211_SMPS_OFF:
180                 /* nothing */
181                 break;
182         }
183
184         if (sta->ht_cap.ht_supported) {
185                 add_sta_cmd.station_flags_msk |=
186                         cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
187                                     STA_FLG_AGG_MPDU_DENS_MSK);
188
189                 mpdu_dens = sta->ht_cap.ampdu_density;
190         }
191
192         if (sta->vht_cap.vht_supported) {
193                 agg_size = sta->vht_cap.cap &
194                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
195                 agg_size >>=
196                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
197         } else if (sta->ht_cap.ht_supported) {
198                 agg_size = sta->ht_cap.ampdu_factor;
199         }
200
201         add_sta_cmd.station_flags |=
202                 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
203         add_sta_cmd.station_flags |=
204                 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
205
206         status = ADD_STA_SUCCESS;
207         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
208                                           iwl_mvm_add_sta_cmd_size(mvm),
209                                           &add_sta_cmd, &status);
210         if (ret)
211                 return ret;
212
213         switch (status & IWL_ADD_STA_STATUS_MASK) {
214         case ADD_STA_SUCCESS:
215                 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
216                 break;
217         default:
218                 ret = -EIO;
219                 IWL_ERR(mvm, "ADD_STA failed\n");
220                 break;
221         }
222
223         return ret;
224 }
225
226 static void iwl_mvm_rx_agg_session_expired(unsigned long data)
227 {
228         struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
229         struct iwl_mvm_baid_data *ba_data;
230         struct ieee80211_sta *sta;
231         struct iwl_mvm_sta *mvm_sta;
232         unsigned long timeout;
233
234         rcu_read_lock();
235
236         ba_data = rcu_dereference(*rcu_ptr);
237
238         if (WARN_ON(!ba_data))
239                 goto unlock;
240
241         if (!ba_data->timeout)
242                 goto unlock;
243
244         timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
245         if (time_is_after_jiffies(timeout)) {
246                 mod_timer(&ba_data->session_timer, timeout);
247                 goto unlock;
248         }
249
250         /* Timer expired */
251         sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
252         mvm_sta = iwl_mvm_sta_from_mac80211(sta);
253         ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
254                                           sta->addr, ba_data->tid);
255 unlock:
256         rcu_read_unlock();
257 }
258
259 static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
260                                  struct ieee80211_sta *sta)
261 {
262         unsigned long used_hw_queues;
263         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
264         unsigned int wdg_timeout =
265                 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
266         u32 ac;
267
268         lockdep_assert_held(&mvm->mutex);
269
270         used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
271
272         /* Find available queues, and allocate them to the ACs */
273         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
274                 u8 queue = find_first_zero_bit(&used_hw_queues,
275                                                mvm->first_agg_queue);
276
277                 if (queue >= mvm->first_agg_queue) {
278                         IWL_ERR(mvm, "Failed to allocate STA queue\n");
279                         return -EBUSY;
280                 }
281
282                 __set_bit(queue, &used_hw_queues);
283                 mvmsta->hw_queue[ac] = queue;
284         }
285
286         /* Found a place for all queues - enable them */
287         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
288                 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
289                                       mvmsta->hw_queue[ac],
290                                       iwl_mvm_ac_to_tx_fifo[ac], 0,
291                                       wdg_timeout);
292                 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
293         }
294
295         return 0;
296 }
297
298 static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
299                                     struct ieee80211_sta *sta)
300 {
301         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
302         unsigned long sta_msk;
303         int i;
304
305         lockdep_assert_held(&mvm->mutex);
306
307         /* disable the TDLS STA-specific queues */
308         sta_msk = mvmsta->tfd_queue_msk;
309         for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
310                 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
311 }
312
313 /* Disable aggregations for a bitmap of TIDs for a given station */
314 static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
315                                         unsigned long disable_agg_tids,
316                                         bool remove_queue)
317 {
318         struct iwl_mvm_add_sta_cmd cmd = {};
319         struct ieee80211_sta *sta;
320         struct iwl_mvm_sta *mvmsta;
321         u32 status;
322         u8 sta_id;
323         int ret;
324
325         spin_lock_bh(&mvm->queue_info_lock);
326         sta_id = mvm->queue_info[queue].ra_sta_id;
327         spin_unlock_bh(&mvm->queue_info_lock);
328
329         rcu_read_lock();
330
331         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
332
333         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
334                 rcu_read_unlock();
335                 return -EINVAL;
336         }
337
338         mvmsta = iwl_mvm_sta_from_mac80211(sta);
339
340         mvmsta->tid_disable_agg |= disable_agg_tids;
341
342         cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
343         cmd.sta_id = mvmsta->sta_id;
344         cmd.add_modify = STA_MODE_MODIFY;
345         cmd.modify_mask = STA_MODIFY_QUEUES;
346         if (disable_agg_tids)
347                 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
348         if (remove_queue)
349                 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
350         cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
351         cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
352
353         rcu_read_unlock();
354
355         /* Notify FW of queue removal from the STA queues */
356         status = ADD_STA_SUCCESS;
357         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
358                                           iwl_mvm_add_sta_cmd_size(mvm),
359                                           &cmd, &status);
360
361         return ret;
362 }
363
364 static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
365 {
366         struct ieee80211_sta *sta;
367         struct iwl_mvm_sta *mvmsta;
368         unsigned long tid_bitmap;
369         unsigned long agg_tids = 0;
370         s8 sta_id;
371         int tid;
372
373         lockdep_assert_held(&mvm->mutex);
374
375         spin_lock_bh(&mvm->queue_info_lock);
376         sta_id = mvm->queue_info[queue].ra_sta_id;
377         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
378         spin_unlock_bh(&mvm->queue_info_lock);
379
380         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
381                                         lockdep_is_held(&mvm->mutex));
382
383         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
384                 return -EINVAL;
385
386         mvmsta = iwl_mvm_sta_from_mac80211(sta);
387
388         spin_lock_bh(&mvmsta->lock);
389         for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
390                 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
391                         agg_tids |= BIT(tid);
392         }
393         spin_unlock_bh(&mvmsta->lock);
394
395         return agg_tids;
396 }
397
398 /*
399  * Remove a queue from a station's resources.
400  * Note that this only marks as free. It DOESN'T delete a BA agreement, and
401  * doesn't disable the queue
402  */
403 static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
404 {
405         struct ieee80211_sta *sta;
406         struct iwl_mvm_sta *mvmsta;
407         unsigned long tid_bitmap;
408         unsigned long disable_agg_tids = 0;
409         u8 sta_id;
410         int tid;
411
412         lockdep_assert_held(&mvm->mutex);
413
414         spin_lock_bh(&mvm->queue_info_lock);
415         sta_id = mvm->queue_info[queue].ra_sta_id;
416         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
417         spin_unlock_bh(&mvm->queue_info_lock);
418
419         rcu_read_lock();
420
421         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
422
423         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
424                 rcu_read_unlock();
425                 return 0;
426         }
427
428         mvmsta = iwl_mvm_sta_from_mac80211(sta);
429
430         spin_lock_bh(&mvmsta->lock);
431         /* Unmap MAC queues and TIDs from this queue */
432         for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
433                 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
434                         disable_agg_tids |= BIT(tid);
435                 mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
436         }
437
438         mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
439         spin_unlock_bh(&mvmsta->lock);
440
441         rcu_read_unlock();
442
443         spin_lock_bh(&mvm->queue_info_lock);
444         /* Unmap MAC queues and TIDs from this queue */
445         mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
446         mvm->queue_info[queue].hw_queue_refcount = 0;
447         mvm->queue_info[queue].tid_bitmap = 0;
448         spin_unlock_bh(&mvm->queue_info_lock);
449
450         return disable_agg_tids;
451 }
452
453 static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
454                                     unsigned long tfd_queue_mask, u8 ac)
455 {
456         int queue = 0;
457         u8 ac_to_queue[IEEE80211_NUM_ACS];
458         int i;
459
460         lockdep_assert_held(&mvm->queue_info_lock);
461
462         memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
463
464         /* See what ACs the existing queues for this STA have */
465         for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
466                 /* Only DATA queues can be shared */
467                 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
468                     i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
469                         continue;
470
471                 /* Don't try and take queues being reconfigured */
472                 if (mvm->queue_info[queue].status ==
473                     IWL_MVM_QUEUE_RECONFIGURING)
474                         continue;
475
476                 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
477         }
478
479         /*
480          * The queue to share is chosen only from DATA queues as follows (in
481          * descending priority):
482          * 1. An AC_BE queue
483          * 2. Same AC queue
484          * 3. Highest AC queue that is lower than new AC
485          * 4. Any existing AC (there always is at least 1 DATA queue)
486          */
487
488         /* Priority 1: An AC_BE queue */
489         if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
490                 queue = ac_to_queue[IEEE80211_AC_BE];
491         /* Priority 2: Same AC queue */
492         else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
493                 queue = ac_to_queue[ac];
494         /* Priority 3a: If new AC is VO and VI exists - use VI */
495         else if (ac == IEEE80211_AC_VO &&
496                  ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
497                 queue = ac_to_queue[IEEE80211_AC_VI];
498         /* Priority 3b: No BE so only AC less than the new one is BK */
499         else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
500                 queue = ac_to_queue[IEEE80211_AC_BK];
501         /* Priority 4a: No BE nor BK - use VI if exists */
502         else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
503                 queue = ac_to_queue[IEEE80211_AC_VI];
504         /* Priority 4b: No BE, BK nor VI - use VO if exists */
505         else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
506                 queue = ac_to_queue[IEEE80211_AC_VO];
507
508         /* Make sure queue found (or not) is legal */
509         if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
510             !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
511             (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
512                 IWL_ERR(mvm, "No DATA queues available to share\n");
513                 return -ENOSPC;
514         }
515
516         /* Make sure the queue isn't in the middle of being reconfigured */
517         if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
518                 IWL_ERR(mvm,
519                         "TXQ %d is in the middle of re-config - try again\n",
520                         queue);
521                 return -EBUSY;
522         }
523
524         return queue;
525 }
526
527 /*
528  * If a given queue has a higher AC than the TID stream that is being compared
529  * to, the queue needs to be redirected to the lower AC. This function does that
530  * in such a case, otherwise - if no redirection required - it does nothing,
531  * unless the %force param is true.
532  */
533 int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
534                                int ac, int ssn, unsigned int wdg_timeout,
535                                bool force)
536 {
537         struct iwl_scd_txq_cfg_cmd cmd = {
538                 .scd_queue = queue,
539                 .action = SCD_CFG_DISABLE_QUEUE,
540         };
541         bool shared_queue;
542         unsigned long mq;
543         int ret;
544
545         /*
546          * If the AC is lower than current one - FIFO needs to be redirected to
547          * the lowest one of the streams in the queue. Check if this is needed
548          * here.
549          * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
550          * value 3 and VO with value 0, so to check if ac X is lower than ac Y
551          * we need to check if the numerical value of X is LARGER than of Y.
552          */
553         spin_lock_bh(&mvm->queue_info_lock);
554         if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
555                 spin_unlock_bh(&mvm->queue_info_lock);
556
557                 IWL_DEBUG_TX_QUEUES(mvm,
558                                     "No redirection needed on TXQ #%d\n",
559                                     queue);
560                 return 0;
561         }
562
563         cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
564         cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
565         cmd.tid = mvm->queue_info[queue].txq_tid;
566         mq = mvm->queue_info[queue].hw_queue_to_mac80211;
567         shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
568         spin_unlock_bh(&mvm->queue_info_lock);
569
570         IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
571                             queue, iwl_mvm_ac_to_tx_fifo[ac]);
572
573         /* Stop MAC queues and wait for this queue to empty */
574         iwl_mvm_stop_mac_queues(mvm, mq);
575         ret = iwl_trans_wait_tx_queue_empty(mvm->trans, BIT(queue));
576         if (ret) {
577                 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
578                         queue);
579                 ret = -EIO;
580                 goto out;
581         }
582
583         /* Before redirecting the queue we need to de-activate it */
584         iwl_trans_txq_disable(mvm->trans, queue, false);
585         ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
586         if (ret)
587                 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
588                         ret);
589
590         /* Make sure the SCD wrptr is correctly set before reconfiguring */
591         iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
592
593         /* Update the TID "owner" of the queue */
594         spin_lock_bh(&mvm->queue_info_lock);
595         mvm->queue_info[queue].txq_tid = tid;
596         spin_unlock_bh(&mvm->queue_info_lock);
597
598         /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
599
600         /* Redirect to lower AC */
601         iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
602                              cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
603                              ssn);
604
605         /* Update AC marking of the queue */
606         spin_lock_bh(&mvm->queue_info_lock);
607         mvm->queue_info[queue].mac80211_ac = ac;
608         spin_unlock_bh(&mvm->queue_info_lock);
609
610         /*
611          * Mark queue as shared in transport if shared
612          * Note this has to be done after queue enablement because enablement
613          * can also set this value, and there is no indication there to shared
614          * queues
615          */
616         if (shared_queue)
617                 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
618
619 out:
620         /* Continue using the MAC queues */
621         iwl_mvm_start_mac_queues(mvm, mq);
622
623         return ret;
624 }
625
626 static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
627                                    struct ieee80211_sta *sta, u8 ac, int tid,
628                                    struct ieee80211_hdr *hdr)
629 {
630         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
631         struct iwl_trans_txq_scd_cfg cfg = {
632                 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
633                 .sta_id = mvmsta->sta_id,
634                 .tid = tid,
635                 .frame_limit = IWL_FRAME_LIMIT,
636         };
637         unsigned int wdg_timeout =
638                 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
639         u8 mac_queue = mvmsta->vif->hw_queue[ac];
640         int queue = -1;
641         bool using_inactive_queue = false;
642         unsigned long disable_agg_tids = 0;
643         enum iwl_mvm_agg_state queue_state;
644         bool shared_queue = false;
645         int ssn;
646         unsigned long tfd_queue_mask;
647         int ret;
648
649         lockdep_assert_held(&mvm->mutex);
650
651         spin_lock_bh(&mvmsta->lock);
652         tfd_queue_mask = mvmsta->tfd_queue_msk;
653         spin_unlock_bh(&mvmsta->lock);
654
655         spin_lock_bh(&mvm->queue_info_lock);
656
657         /*
658          * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
659          * exists
660          */
661         if (!ieee80211_is_data_qos(hdr->frame_control) ||
662             ieee80211_is_qos_nullfunc(hdr->frame_control)) {
663                 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
664                                                 IWL_MVM_DQA_MIN_MGMT_QUEUE,
665                                                 IWL_MVM_DQA_MAX_MGMT_QUEUE);
666                 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
667                         IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
668                                             queue);
669
670                 /* If no such queue is found, we'll use a DATA queue instead */
671         }
672
673         if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
674             (mvm->queue_info[mvmsta->reserved_queue].status ==
675              IWL_MVM_QUEUE_RESERVED ||
676              mvm->queue_info[mvmsta->reserved_queue].status ==
677              IWL_MVM_QUEUE_INACTIVE)) {
678                 queue = mvmsta->reserved_queue;
679                 mvm->queue_info[queue].reserved = true;
680                 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
681         }
682
683         if (queue < 0)
684                 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
685                                                 IWL_MVM_DQA_MIN_DATA_QUEUE,
686                                                 IWL_MVM_DQA_MAX_DATA_QUEUE);
687
688         /*
689          * Check if this queue is already allocated but inactive.
690          * In such a case, we'll need to first free this queue before enabling
691          * it again, so we'll mark it as reserved to make sure no new traffic
692          * arrives on it
693          */
694         if (queue > 0 &&
695             mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
696                 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
697                 using_inactive_queue = true;
698                 IWL_DEBUG_TX_QUEUES(mvm,
699                                     "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
700                                     queue, mvmsta->sta_id, tid);
701         }
702
703         /* No free queue - we'll have to share */
704         if (queue <= 0) {
705                 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
706                 if (queue > 0) {
707                         shared_queue = true;
708                         mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
709                 }
710         }
711
712         /*
713          * Mark TXQ as ready, even though it hasn't been fully configured yet,
714          * to make sure no one else takes it.
715          * This will allow avoiding re-acquiring the lock at the end of the
716          * configuration. On error we'll mark it back as free.
717          */
718         if ((queue > 0) && !shared_queue)
719                 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
720
721         spin_unlock_bh(&mvm->queue_info_lock);
722
723         /* This shouldn't happen - out of queues */
724         if (WARN_ON(queue <= 0)) {
725                 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
726                         tid, cfg.sta_id);
727                 return queue;
728         }
729
730         /*
731          * Actual en/disablement of aggregations is through the ADD_STA HCMD,
732          * but for configuring the SCD to send A-MPDUs we need to mark the queue
733          * as aggregatable.
734          * Mark all DATA queues as allowing to be aggregated at some point
735          */
736         cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
737                          queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
738
739         /*
740          * If this queue was previously inactive (idle) - we need to free it
741          * first
742          */
743         if (using_inactive_queue) {
744                 struct iwl_scd_txq_cfg_cmd cmd = {
745                         .scd_queue = queue,
746                         .action = SCD_CFG_DISABLE_QUEUE,
747                 };
748                 u8 txq_curr_ac;
749
750                 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
751
752                 spin_lock_bh(&mvm->queue_info_lock);
753                 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
754                 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
755                 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[txq_curr_ac];
756                 cmd.tid = mvm->queue_info[queue].txq_tid;
757                 spin_unlock_bh(&mvm->queue_info_lock);
758
759                 /* Disable the queue */
760                 if (disable_agg_tids)
761                         iwl_mvm_invalidate_sta_queue(mvm, queue,
762                                                      disable_agg_tids, false);
763                 iwl_trans_txq_disable(mvm->trans, queue, false);
764                 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
765                                            &cmd);
766                 if (ret) {
767                         IWL_ERR(mvm,
768                                 "Failed to free inactive queue %d (ret=%d)\n",
769                                 queue, ret);
770
771                         /* Re-mark the inactive queue as inactive */
772                         spin_lock_bh(&mvm->queue_info_lock);
773                         mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
774                         spin_unlock_bh(&mvm->queue_info_lock);
775
776                         return ret;
777                 }
778
779                 /* If TXQ is allocated to another STA, update removal in FW */
780                 if (cmd.sta_id != mvmsta->sta_id)
781                         iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
782         }
783
784         IWL_DEBUG_TX_QUEUES(mvm,
785                             "Allocating %squeue #%d to sta %d on tid %d\n",
786                             shared_queue ? "shared " : "", queue,
787                             mvmsta->sta_id, tid);
788
789         if (shared_queue) {
790                 /* Disable any open aggs on this queue */
791                 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
792
793                 if (disable_agg_tids) {
794                         IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
795                                             queue);
796                         iwl_mvm_invalidate_sta_queue(mvm, queue,
797                                                      disable_agg_tids, false);
798                 }
799         }
800
801         ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
802         iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
803                            wdg_timeout);
804
805         /*
806          * Mark queue as shared in transport if shared
807          * Note this has to be done after queue enablement because enablement
808          * can also set this value, and there is no indication there to shared
809          * queues
810          */
811         if (shared_queue)
812                 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
813
814         spin_lock_bh(&mvmsta->lock);
815         mvmsta->tid_data[tid].txq_id = queue;
816         mvmsta->tid_data[tid].is_tid_active = true;
817         mvmsta->tfd_queue_msk |= BIT(queue);
818         queue_state = mvmsta->tid_data[tid].state;
819
820         if (mvmsta->reserved_queue == queue)
821                 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
822         spin_unlock_bh(&mvmsta->lock);
823
824         if (!shared_queue) {
825                 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
826                 if (ret)
827                         goto out_err;
828
829                 /* If we need to re-enable aggregations... */
830                 if (queue_state == IWL_AGG_ON) {
831                         ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
832                         if (ret)
833                                 goto out_err;
834                 }
835         } else {
836                 /* Redirect queue, if needed */
837                 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
838                                                  wdg_timeout, false);
839                 if (ret)
840                         goto out_err;
841         }
842
843         return 0;
844
845 out_err:
846         iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
847
848         return ret;
849 }
850
851 static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
852 {
853         struct iwl_scd_txq_cfg_cmd cmd = {
854                 .scd_queue = queue,
855                 .action = SCD_CFG_UPDATE_QUEUE_TID,
856         };
857         s8 sta_id;
858         int tid;
859         unsigned long tid_bitmap;
860         int ret;
861
862         lockdep_assert_held(&mvm->mutex);
863
864         spin_lock_bh(&mvm->queue_info_lock);
865         sta_id = mvm->queue_info[queue].ra_sta_id;
866         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
867         spin_unlock_bh(&mvm->queue_info_lock);
868
869         if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
870                 return;
871
872         /* Find any TID for queue */
873         tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
874         cmd.tid = tid;
875         cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
876
877         ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
878         if (ret)
879                 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
880                         queue, ret);
881         else
882                 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
883                                     queue, tid);
884 }
885
886 static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
887 {
888         struct ieee80211_sta *sta;
889         struct iwl_mvm_sta *mvmsta;
890         s8 sta_id;
891         int tid = -1;
892         unsigned long tid_bitmap;
893         unsigned int wdg_timeout;
894         int ssn;
895         int ret = true;
896
897         lockdep_assert_held(&mvm->mutex);
898
899         spin_lock_bh(&mvm->queue_info_lock);
900         sta_id = mvm->queue_info[queue].ra_sta_id;
901         tid_bitmap = mvm->queue_info[queue].tid_bitmap;
902         spin_unlock_bh(&mvm->queue_info_lock);
903
904         /* Find TID for queue, and make sure it is the only one on the queue */
905         tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
906         if (tid_bitmap != BIT(tid)) {
907                 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
908                         queue, tid_bitmap);
909                 return;
910         }
911
912         IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
913                             tid);
914
915         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
916                                         lockdep_is_held(&mvm->mutex));
917
918         if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
919                 return;
920
921         mvmsta = iwl_mvm_sta_from_mac80211(sta);
922         wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
923
924         ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
925
926         ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
927                                          tid_to_mac80211_ac[tid], ssn,
928                                          wdg_timeout, true);
929         if (ret) {
930                 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
931                 return;
932         }
933
934         /* If aggs should be turned back on - do it */
935         if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
936                 struct iwl_mvm_add_sta_cmd cmd = {0};
937
938                 mvmsta->tid_disable_agg &= ~BIT(tid);
939
940                 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
941                 cmd.sta_id = mvmsta->sta_id;
942                 cmd.add_modify = STA_MODE_MODIFY;
943                 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
944                 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
945                 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
946
947                 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
948                                            iwl_mvm_add_sta_cmd_size(mvm), &cmd);
949                 if (!ret) {
950                         IWL_DEBUG_TX_QUEUES(mvm,
951                                             "TXQ #%d is now aggregated again\n",
952                                             queue);
953
954                         /* Mark queue intenally as aggregating again */
955                         iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
956                 }
957         }
958
959         spin_lock_bh(&mvm->queue_info_lock);
960         mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
961         spin_unlock_bh(&mvm->queue_info_lock);
962 }
963
964 static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
965 {
966         if (tid == IWL_MAX_TID_COUNT)
967                 return IEEE80211_AC_VO; /* MGMT */
968
969         return tid_to_mac80211_ac[tid];
970 }
971
972 static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
973                                        struct ieee80211_sta *sta, int tid)
974 {
975         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
976         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
977         struct sk_buff *skb;
978         struct ieee80211_hdr *hdr;
979         struct sk_buff_head deferred_tx;
980         u8 mac_queue;
981         bool no_queue = false; /* Marks if there is a problem with the queue */
982         u8 ac;
983
984         lockdep_assert_held(&mvm->mutex);
985
986         skb = skb_peek(&tid_data->deferred_tx_frames);
987         if (!skb)
988                 return;
989         hdr = (void *)skb->data;
990
991         ac = iwl_mvm_tid_to_ac_queue(tid);
992         mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
993
994         if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
995             iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
996                 IWL_ERR(mvm,
997                         "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
998                         mvmsta->sta_id, tid);
999
1000                 /*
1001                  * Mark queue as problematic so later the deferred traffic is
1002                  * freed, as we can do nothing with it
1003                  */
1004                 no_queue = true;
1005         }
1006
1007         __skb_queue_head_init(&deferred_tx);
1008
1009         /* Disable bottom-halves when entering TX path */
1010         local_bh_disable();
1011         spin_lock(&mvmsta->lock);
1012         skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
1013         spin_unlock(&mvmsta->lock);
1014
1015         while ((skb = __skb_dequeue(&deferred_tx)))
1016                 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1017                         ieee80211_free_txskb(mvm->hw, skb);
1018         local_bh_enable();
1019
1020         /* Wake queue */
1021         iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1022 }
1023
1024 void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1025 {
1026         struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1027                                            add_stream_wk);
1028         struct ieee80211_sta *sta;
1029         struct iwl_mvm_sta *mvmsta;
1030         unsigned long deferred_tid_traffic;
1031         int queue, sta_id, tid;
1032
1033         /* Check inactivity of queues */
1034         iwl_mvm_inactivity_check(mvm);
1035
1036         mutex_lock(&mvm->mutex);
1037
1038         /* Reconfigure queues requiring reconfiguation */
1039         for (queue = 0; queue < IWL_MAX_HW_QUEUES; queue++) {
1040                 bool reconfig;
1041                 bool change_owner;
1042
1043                 spin_lock_bh(&mvm->queue_info_lock);
1044                 reconfig = (mvm->queue_info[queue].status ==
1045                             IWL_MVM_QUEUE_RECONFIGURING);
1046
1047                 /*
1048                  * We need to take into account a situation in which a TXQ was
1049                  * allocated to TID x, and then turned shared by adding TIDs y
1050                  * and z. If TID x becomes inactive and is removed from the TXQ,
1051                  * ownership must be given to one of the remaining TIDs.
1052                  * This is mainly because if TID x continues - a new queue can't
1053                  * be allocated for it as long as it is an owner of another TXQ.
1054                  */
1055                 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1056                                  BIT(mvm->queue_info[queue].txq_tid)) &&
1057                                (mvm->queue_info[queue].status ==
1058                                 IWL_MVM_QUEUE_SHARED);
1059                 spin_unlock_bh(&mvm->queue_info_lock);
1060
1061                 if (reconfig)
1062                         iwl_mvm_unshare_queue(mvm, queue);
1063                 else if (change_owner)
1064                         iwl_mvm_change_queue_owner(mvm, queue);
1065         }
1066
1067         /* Go over all stations with deferred traffic */
1068         for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1069                          IWL_MVM_STATION_COUNT) {
1070                 clear_bit(sta_id, mvm->sta_deferred_frames);
1071                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1072                                                 lockdep_is_held(&mvm->mutex));
1073                 if (IS_ERR_OR_NULL(sta))
1074                         continue;
1075
1076                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1077                 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1078
1079                 for_each_set_bit(tid, &deferred_tid_traffic,
1080                                  IWL_MAX_TID_COUNT + 1)
1081                         iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1082         }
1083
1084         mutex_unlock(&mvm->mutex);
1085 }
1086
1087 static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1088                                       struct ieee80211_sta *sta,
1089                                       enum nl80211_iftype vif_type)
1090 {
1091         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1092         int queue;
1093
1094         /*
1095          * Check for inactive queues, so we don't reach a situation where we
1096          * can't add a STA due to a shortage in queues that doesn't really exist
1097          */
1098         iwl_mvm_inactivity_check(mvm);
1099
1100         spin_lock_bh(&mvm->queue_info_lock);
1101
1102         /* Make sure we have free resources for this STA */
1103         if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1104             !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
1105             (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1106              IWL_MVM_QUEUE_FREE))
1107                 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1108         else
1109                 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1110                                                 IWL_MVM_DQA_MIN_DATA_QUEUE,
1111                                                 IWL_MVM_DQA_MAX_DATA_QUEUE);
1112         if (queue < 0) {
1113                 spin_unlock_bh(&mvm->queue_info_lock);
1114                 IWL_ERR(mvm, "No available queues for new station\n");
1115                 return -ENOSPC;
1116         }
1117         mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1118
1119         spin_unlock_bh(&mvm->queue_info_lock);
1120
1121         mvmsta->reserved_queue = queue;
1122
1123         IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1124                             queue, mvmsta->sta_id);
1125
1126         return 0;
1127 }
1128
1129 /*
1130  * In DQA mode, after a HW restart the queues should be allocated as before, in
1131  * order to avoid race conditions when there are shared queues. This function
1132  * does the re-mapping and queue allocation.
1133  *
1134  * Note that re-enabling aggregations isn't done in this function.
1135  */
1136 static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1137                                                  struct iwl_mvm_sta *mvm_sta)
1138 {
1139         unsigned int wdg_timeout =
1140                         iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1141         int i;
1142         struct iwl_trans_txq_scd_cfg cfg = {
1143                 .sta_id = mvm_sta->sta_id,
1144                 .frame_limit = IWL_FRAME_LIMIT,
1145         };
1146
1147         /* Make sure reserved queue is still marked as such (if allocated) */
1148         if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1149                 mvm->queue_info[mvm_sta->reserved_queue].status =
1150                         IWL_MVM_QUEUE_RESERVED;
1151
1152         for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1153                 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1154                 int txq_id = tid_data->txq_id;
1155                 int ac;
1156                 u8 mac_queue;
1157
1158                 if (txq_id == IEEE80211_INVAL_HW_QUEUE)
1159                         continue;
1160
1161                 skb_queue_head_init(&tid_data->deferred_tx_frames);
1162
1163                 ac = tid_to_mac80211_ac[i];
1164                 mac_queue = mvm_sta->vif->hw_queue[ac];
1165
1166                 cfg.tid = i;
1167                 cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
1168                 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1169                                  txq_id == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1170
1171                 IWL_DEBUG_TX_QUEUES(mvm,
1172                                     "Re-mapping sta %d tid %d to queue %d\n",
1173                                     mvm_sta->sta_id, i, txq_id);
1174
1175                 iwl_mvm_enable_txq(mvm, txq_id, mac_queue,
1176                                    IEEE80211_SEQ_TO_SN(tid_data->seq_number),
1177                                    &cfg, wdg_timeout);
1178
1179                 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1180         }
1181
1182         atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
1183 }
1184
1185 int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1186                     struct ieee80211_vif *vif,
1187                     struct ieee80211_sta *sta)
1188 {
1189         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1190         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1191         struct iwl_mvm_rxq_dup_data *dup_data;
1192         int i, ret, sta_id;
1193
1194         lockdep_assert_held(&mvm->mutex);
1195
1196         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1197                 sta_id = iwl_mvm_find_free_sta_id(mvm,
1198                                                   ieee80211_vif_type_p2p(vif));
1199         else
1200                 sta_id = mvm_sta->sta_id;
1201
1202         if (sta_id == IWL_MVM_STATION_COUNT)
1203                 return -ENOSPC;
1204
1205         spin_lock_init(&mvm_sta->lock);
1206
1207         /* In DQA mode, if this is a HW restart, re-alloc existing queues */
1208         if (iwl_mvm_is_dqa_supported(mvm) &&
1209             test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1210                 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1211                 goto update_fw;
1212         }
1213
1214         mvm_sta->sta_id = sta_id;
1215         mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1216                                                       mvmvif->color);
1217         mvm_sta->vif = vif;
1218         mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1219         mvm_sta->tx_protection = 0;
1220         mvm_sta->tt_tx_protection = false;
1221
1222         /* HW restart, don't assume the memory has been zeroed */
1223         atomic_set(&mvm->pending_frames[sta_id], 0);
1224         mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1225         mvm_sta->tfd_queue_msk = 0;
1226
1227         /*
1228          * Allocate new queues for a TDLS station, unless we're in DQA mode,
1229          * and then they'll be allocated dynamically
1230          */
1231         if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
1232                 ret = iwl_mvm_tdls_sta_init(mvm, sta);
1233                 if (ret)
1234                         return ret;
1235         } else if (!iwl_mvm_is_dqa_supported(mvm)) {
1236                 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1237                         if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1238                                 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1239         }
1240
1241         /* for HW restart - reset everything but the sequence number */
1242         for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1243                 u16 seq = mvm_sta->tid_data[i].seq_number;
1244                 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1245                 mvm_sta->tid_data[i].seq_number = seq;
1246
1247                 if (!iwl_mvm_is_dqa_supported(mvm))
1248                         continue;
1249
1250                 /*
1251                  * Mark all queues for this STA as unallocated and defer TX
1252                  * frames until the queue is allocated
1253                  */
1254                 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1255                 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
1256         }
1257         mvm_sta->deferred_traffic_tid_map = 0;
1258         mvm_sta->agg_tids = 0;
1259
1260         if (iwl_mvm_has_new_rx_api(mvm) &&
1261             !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1262                 dup_data = kcalloc(mvm->trans->num_rx_queues,
1263                                    sizeof(*dup_data),
1264                                    GFP_KERNEL);
1265                 if (!dup_data)
1266                         return -ENOMEM;
1267                 mvm_sta->dup_data = dup_data;
1268         }
1269
1270         if (iwl_mvm_is_dqa_supported(mvm)) {
1271                 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1272                                                  ieee80211_vif_type_p2p(vif));
1273                 if (ret)
1274                         goto err;
1275         }
1276
1277 update_fw:
1278         ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
1279         if (ret)
1280                 goto err;
1281
1282         if (vif->type == NL80211_IFTYPE_STATION) {
1283                 if (!sta->tdls) {
1284                         WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
1285                         mvmvif->ap_sta_id = sta_id;
1286                 } else {
1287                         WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
1288                 }
1289         }
1290
1291         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1292
1293         return 0;
1294
1295 err:
1296         if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1297                 iwl_mvm_tdls_sta_deinit(mvm, sta);
1298         return ret;
1299 }
1300
1301 int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1302                       bool drain)
1303 {
1304         struct iwl_mvm_add_sta_cmd cmd = {};
1305         int ret;
1306         u32 status;
1307
1308         lockdep_assert_held(&mvm->mutex);
1309
1310         cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1311         cmd.sta_id = mvmsta->sta_id;
1312         cmd.add_modify = STA_MODE_MODIFY;
1313         cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1314         cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1315
1316         status = ADD_STA_SUCCESS;
1317         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1318                                           iwl_mvm_add_sta_cmd_size(mvm),
1319                                           &cmd, &status);
1320         if (ret)
1321                 return ret;
1322
1323         switch (status & IWL_ADD_STA_STATUS_MASK) {
1324         case ADD_STA_SUCCESS:
1325                 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1326                                mvmsta->sta_id);
1327                 break;
1328         default:
1329                 ret = -EIO;
1330                 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1331                         mvmsta->sta_id);
1332                 break;
1333         }
1334
1335         return ret;
1336 }
1337
1338 /*
1339  * Remove a station from the FW table. Before sending the command to remove
1340  * the station validate that the station is indeed known to the driver (sanity
1341  * only).
1342  */
1343 static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1344 {
1345         struct ieee80211_sta *sta;
1346         struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1347                 .sta_id = sta_id,
1348         };
1349         int ret;
1350
1351         sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1352                                         lockdep_is_held(&mvm->mutex));
1353
1354         /* Note: internal stations are marked as error values */
1355         if (!sta) {
1356                 IWL_ERR(mvm, "Invalid station id\n");
1357                 return -EINVAL;
1358         }
1359
1360         ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1361                                    sizeof(rm_sta_cmd), &rm_sta_cmd);
1362         if (ret) {
1363                 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1364                 return ret;
1365         }
1366
1367         return 0;
1368 }
1369
1370 void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1371 {
1372         struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1373         u8 sta_id;
1374
1375         /*
1376          * The mutex is needed because of the SYNC cmd, but not only: if the
1377          * work would run concurrently with iwl_mvm_rm_sta, it would run before
1378          * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1379          * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1380          * that later.
1381          */
1382         mutex_lock(&mvm->mutex);
1383
1384         for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1385                 int ret;
1386                 struct ieee80211_sta *sta =
1387                         rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1388                                                   lockdep_is_held(&mvm->mutex));
1389
1390                 /*
1391                  * This station is in use or RCU-removed; the latter happens in
1392                  * managed mode, where mac80211 removes the station before we
1393                  * can remove it from firmware (we can only do that after the
1394                  * MAC is marked unassociated), and possibly while the deauth
1395                  * frame to disconnect from the AP is still queued. Then, the
1396                  * station pointer is -ENOENT when the last skb is reclaimed.
1397                  */
1398                 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
1399                         continue;
1400
1401                 if (PTR_ERR(sta) == -EINVAL) {
1402                         IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1403                                 sta_id);
1404                         continue;
1405                 }
1406
1407                 if (!sta) {
1408                         IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1409                                 sta_id);
1410                         continue;
1411                 }
1412
1413                 WARN_ON(PTR_ERR(sta) != -EBUSY);
1414                 /* This station was removed and we waited until it got drained,
1415                  * we can now proceed and remove it.
1416                  */
1417                 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1418                 if (ret) {
1419                         IWL_ERR(mvm,
1420                                 "Couldn't remove sta %d after it was drained\n",
1421                                 sta_id);
1422                         continue;
1423                 }
1424                 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1425                 clear_bit(sta_id, mvm->sta_drained);
1426
1427                 if (mvm->tfd_drained[sta_id]) {
1428                         unsigned long i, msk = mvm->tfd_drained[sta_id];
1429
1430                         for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
1431                                 iwl_mvm_disable_txq(mvm, i, i,
1432                                                     IWL_MAX_TID_COUNT, 0);
1433
1434                         mvm->tfd_drained[sta_id] = 0;
1435                         IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1436                                        sta_id, msk);
1437                 }
1438         }
1439
1440         mutex_unlock(&mvm->mutex);
1441 }
1442
1443 static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1444                                        struct ieee80211_vif *vif,
1445                                        struct iwl_mvm_sta *mvm_sta)
1446 {
1447         int ac;
1448         int i;
1449
1450         lockdep_assert_held(&mvm->mutex);
1451
1452         for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1453                 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
1454                         continue;
1455
1456                 ac = iwl_mvm_tid_to_ac_queue(i);
1457                 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1458                                     vif->hw_queue[ac], i, 0);
1459                 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1460         }
1461 }
1462
1463 int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1464                    struct ieee80211_vif *vif,
1465                    struct ieee80211_sta *sta)
1466 {
1467         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1468         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1469         u8 sta_id = mvm_sta->sta_id;
1470         int ret;
1471
1472         lockdep_assert_held(&mvm->mutex);
1473
1474         if (iwl_mvm_has_new_rx_api(mvm))
1475                 kfree(mvm_sta->dup_data);
1476
1477         if ((vif->type == NL80211_IFTYPE_STATION &&
1478              mvmvif->ap_sta_id == sta_id) ||
1479             iwl_mvm_is_dqa_supported(mvm)){
1480                 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1481                 if (ret)
1482                         return ret;
1483                 /* flush its queues here since we are freeing mvm_sta */
1484                 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
1485                 if (ret)
1486                         return ret;
1487                 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1488                                                     mvm_sta->tfd_queue_msk);
1489                 if (ret)
1490                         return ret;
1491                 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1492
1493                 /* If DQA is supported - the queues can be disabled now */
1494                 if (iwl_mvm_is_dqa_supported(mvm)) {
1495                         u8 reserved_txq = mvm_sta->reserved_queue;
1496                         enum iwl_mvm_queue_status *status;
1497
1498                         iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
1499
1500                         /*
1501                          * If pending_frames is set at this point - it must be
1502                          * driver internal logic error, since queues are empty
1503                          * and removed successuly.
1504                          * warn on it but set it to 0 anyway to avoid station
1505                          * not being removed later in the function
1506                          */
1507                         WARN_ON(atomic_xchg(&mvm->pending_frames[sta_id], 0));
1508
1509                         /*
1510                          * If no traffic has gone through the reserved TXQ - it
1511                          * is still marked as IWL_MVM_QUEUE_RESERVED, and
1512                          * should be manually marked as free again
1513                          */
1514                         spin_lock_bh(&mvm->queue_info_lock);
1515                         status = &mvm->queue_info[reserved_txq].status;
1516                         if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1517                                  (*status != IWL_MVM_QUEUE_FREE),
1518                                  "sta_id %d reserved txq %d status %d",
1519                                  sta_id, reserved_txq, *status)) {
1520                                 spin_unlock_bh(&mvm->queue_info_lock);
1521                                 return -EINVAL;
1522                         }
1523
1524                         *status = IWL_MVM_QUEUE_FREE;
1525                         spin_unlock_bh(&mvm->queue_info_lock);
1526                 }
1527
1528                 if (vif->type == NL80211_IFTYPE_STATION &&
1529                     mvmvif->ap_sta_id == sta_id) {
1530                         /* if associated - we can't remove the AP STA now */
1531                         if (vif->bss_conf.assoc)
1532                                 return ret;
1533
1534                         /* unassoc - go ahead - remove the AP STA now */
1535                         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1536
1537                         /* clear d0i3_ap_sta_id if no longer relevant */
1538                         if (mvm->d0i3_ap_sta_id == sta_id)
1539                                 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1540                 }
1541         }
1542
1543         /*
1544          * This shouldn't happen - the TDLS channel switch should be canceled
1545          * before the STA is removed.
1546          */
1547         if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1548                 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1549                 cancel_delayed_work(&mvm->tdls_cs.dwork);
1550         }
1551
1552         /*
1553          * Make sure that the tx response code sees the station as -EBUSY and
1554          * calls the drain worker.
1555          */
1556         spin_lock_bh(&mvm_sta->lock);
1557
1558         /*
1559          * There are frames pending on the AC queues for this station.
1560          * We need to wait until all the frames are drained...
1561          */
1562         if (atomic_read(&mvm->pending_frames[sta_id])) {
1563                 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id],
1564                                    ERR_PTR(-EBUSY));
1565                 spin_unlock_bh(&mvm_sta->lock);
1566
1567                 /* disable TDLS sta queues on drain complete */
1568                 if (sta->tdls) {
1569                         mvm->tfd_drained[sta_id] = mvm_sta->tfd_queue_msk;
1570                         IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n", sta_id);
1571                 }
1572
1573                 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1574         } else {
1575                 spin_unlock_bh(&mvm_sta->lock);
1576
1577                 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1578                         iwl_mvm_tdls_sta_deinit(mvm, sta);
1579
1580                 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1581                 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1582         }
1583
1584         return ret;
1585 }
1586
1587 int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1588                       struct ieee80211_vif *vif,
1589                       u8 sta_id)
1590 {
1591         int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1592
1593         lockdep_assert_held(&mvm->mutex);
1594
1595         RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1596         return ret;
1597 }
1598
1599 int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1600                              struct iwl_mvm_int_sta *sta,
1601                              u32 qmask, enum nl80211_iftype iftype)
1602 {
1603         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1604                 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1605                 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1606                         return -ENOSPC;
1607         }
1608
1609         sta->tfd_queue_msk = qmask;
1610
1611         /* put a non-NULL value so iterating over the stations won't stop */
1612         rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1613         return 0;
1614 }
1615
1616 static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1617                                     struct iwl_mvm_int_sta *sta)
1618 {
1619         RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1620         memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1621         sta->sta_id = IWL_MVM_STATION_COUNT;
1622 }
1623
1624 static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1625                                       struct iwl_mvm_int_sta *sta,
1626                                       const u8 *addr,
1627                                       u16 mac_id, u16 color)
1628 {
1629         struct iwl_mvm_add_sta_cmd cmd;
1630         int ret;
1631         u32 status;
1632
1633         lockdep_assert_held(&mvm->mutex);
1634
1635         memset(&cmd, 0, sizeof(cmd));
1636         cmd.sta_id = sta->sta_id;
1637         cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1638                                                              color));
1639
1640         cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1641         cmd.tid_disable_tx = cpu_to_le16(0xffff);
1642
1643         if (addr)
1644                 memcpy(cmd.addr, addr, ETH_ALEN);
1645
1646         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1647                                           iwl_mvm_add_sta_cmd_size(mvm),
1648                                           &cmd, &status);
1649         if (ret)
1650                 return ret;
1651
1652         switch (status & IWL_ADD_STA_STATUS_MASK) {
1653         case ADD_STA_SUCCESS:
1654                 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1655                 return 0;
1656         default:
1657                 ret = -EIO;
1658                 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1659                         status);
1660                 break;
1661         }
1662         return ret;
1663 }
1664
1665 int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1666 {
1667         unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1668                                         mvm->cfg->base_params->wd_timeout :
1669                                         IWL_WATCHDOG_DISABLED;
1670         int ret;
1671
1672         lockdep_assert_held(&mvm->mutex);
1673
1674         /* Map Aux queue to fifo - needs to happen before adding Aux station */
1675         if (!iwl_mvm_is_dqa_supported(mvm))
1676                 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1677                                       IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
1678
1679         /* Allocate aux station and assign to it the aux queue */
1680         ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1681                                        NL80211_IFTYPE_UNSPECIFIED);
1682         if (ret)
1683                 return ret;
1684
1685         if (iwl_mvm_is_dqa_supported(mvm)) {
1686                 struct iwl_trans_txq_scd_cfg cfg = {
1687                         .fifo = IWL_MVM_TX_FIFO_MCAST,
1688                         .sta_id = mvm->aux_sta.sta_id,
1689                         .tid = IWL_MAX_TID_COUNT,
1690                         .aggregate = false,
1691                         .frame_limit = IWL_FRAME_LIMIT,
1692                 };
1693
1694                 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1695                                    wdg_timeout);
1696         }
1697
1698         ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1699                                          MAC_INDEX_AUX, 0);
1700
1701         if (ret)
1702                 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1703         return ret;
1704 }
1705
1706 int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1707 {
1708         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1709
1710         lockdep_assert_held(&mvm->mutex);
1711         return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1712                                          mvmvif->id, 0);
1713 }
1714
1715 int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1716 {
1717         int ret;
1718
1719         lockdep_assert_held(&mvm->mutex);
1720
1721         ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1722         if (ret)
1723                 IWL_WARN(mvm, "Failed sending remove station\n");
1724
1725         return ret;
1726 }
1727
1728 void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1729 {
1730         iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1731 }
1732
1733 void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1734 {
1735         lockdep_assert_held(&mvm->mutex);
1736
1737         iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1738 }
1739
1740 /*
1741  * Send the add station command for the vif's broadcast station.
1742  * Assumes that the station was already allocated.
1743  *
1744  * @mvm: the mvm component
1745  * @vif: the interface to which the broadcast station is added
1746  * @bsta: the broadcast station to add.
1747  */
1748 int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1749 {
1750         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1751         struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1752         static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1753         const u8 *baddr = _baddr;
1754
1755         lockdep_assert_held(&mvm->mutex);
1756
1757         if (iwl_mvm_is_dqa_supported(mvm)) {
1758                 struct iwl_trans_txq_scd_cfg cfg = {
1759                         .fifo = IWL_MVM_TX_FIFO_VO,
1760                         .sta_id = mvmvif->bcast_sta.sta_id,
1761                         .tid = IWL_MAX_TID_COUNT,
1762                         .aggregate = false,
1763                         .frame_limit = IWL_FRAME_LIMIT,
1764                 };
1765                 unsigned int wdg_timeout =
1766                         iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1767                 int queue;
1768
1769                 if ((vif->type == NL80211_IFTYPE_AP) &&
1770                     (mvmvif->bcast_sta.tfd_queue_msk &
1771                      BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1772                         queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
1773                 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1774                          (mvmvif->bcast_sta.tfd_queue_msk &
1775                           BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1776                         queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
1777                 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1778                         return -EINVAL;
1779
1780                 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1781                                    wdg_timeout);
1782         }
1783
1784         if (vif->type == NL80211_IFTYPE_ADHOC)
1785                 baddr = vif->bss_conf.bssid;
1786
1787         if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1788                 return -ENOSPC;
1789
1790         return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1791                                           mvmvif->id, mvmvif->color);
1792 }
1793
1794 /* Send the FW a request to remove the station from it's internal data
1795  * structures, but DO NOT remove the entry from the local data structures. */
1796 int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1797 {
1798         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1799         int ret;
1800
1801         lockdep_assert_held(&mvm->mutex);
1802
1803         ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
1804         if (ret)
1805                 IWL_WARN(mvm, "Failed sending remove station\n");
1806         return ret;
1807 }
1808
1809 int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1810 {
1811         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1812         u32 qmask = 0;
1813
1814         lockdep_assert_held(&mvm->mutex);
1815
1816         if (!iwl_mvm_is_dqa_supported(mvm))
1817                 qmask = iwl_mvm_mac_get_queues_mask(vif);
1818
1819         if (vif->type == NL80211_IFTYPE_AP) {
1820                 /*
1821                  * The firmware defines the TFD queue mask to only be relevant
1822                  * for *unicast* queues, so the multicast (CAB) queue shouldn't
1823                  * be included.
1824                  */
1825                 qmask &= ~BIT(vif->cab_queue);
1826
1827                 if (iwl_mvm_is_dqa_supported(mvm))
1828                         qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
1829         } else if (iwl_mvm_is_dqa_supported(mvm) &&
1830                    vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1831                 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
1832         }
1833
1834         return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1835                                         ieee80211_vif_type_p2p(vif));
1836 }
1837
1838 /* Allocate a new station entry for the broadcast station to the given vif,
1839  * and send it to the FW.
1840  * Note that each P2P mac should have its own broadcast station.
1841  *
1842  * @mvm: the mvm component
1843  * @vif: the interface to which the broadcast station is added
1844  * @bsta: the broadcast station to add. */
1845 int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1846 {
1847         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1848         struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
1849         int ret;
1850
1851         lockdep_assert_held(&mvm->mutex);
1852
1853         ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1854         if (ret)
1855                 return ret;
1856
1857         ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
1858
1859         if (ret)
1860                 iwl_mvm_dealloc_int_sta(mvm, bsta);
1861
1862         return ret;
1863 }
1864
1865 void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1866 {
1867         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1868
1869         iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1870 }
1871
1872 /*
1873  * Send the FW a request to remove the station from it's internal data
1874  * structures, and in addition remove it from the local data structure.
1875  */
1876 int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1877 {
1878         int ret;
1879
1880         lockdep_assert_held(&mvm->mutex);
1881
1882         ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
1883
1884         iwl_mvm_dealloc_bcast_sta(mvm, vif);
1885
1886         return ret;
1887 }
1888
1889 #define IWL_MAX_RX_BA_SESSIONS 16
1890
1891 static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
1892 {
1893         struct iwl_mvm_delba_notif notif = {
1894                 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1895                 .metadata.sync = 1,
1896                 .delba.baid = baid,
1897         };
1898         iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
1899 };
1900
1901 static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1902                                  struct iwl_mvm_baid_data *data)
1903 {
1904         int i;
1905
1906         iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1907
1908         for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1909                 int j;
1910                 struct iwl_mvm_reorder_buffer *reorder_buf =
1911                         &data->reorder_buf[i];
1912
1913                 spin_lock_bh(&reorder_buf->lock);
1914                 if (likely(!reorder_buf->num_stored)) {
1915                         spin_unlock_bh(&reorder_buf->lock);
1916                         continue;
1917                 }
1918
1919                 /*
1920                  * This shouldn't happen in regular DELBA since the internal
1921                  * delBA notification should trigger a release of all frames in
1922                  * the reorder buffer.
1923                  */
1924                 WARN_ON(1);
1925
1926                 for (j = 0; j < reorder_buf->buf_size; j++)
1927                         __skb_queue_purge(&reorder_buf->entries[j]);
1928                 /*
1929                  * Prevent timer re-arm. This prevents a very far fetched case
1930                  * where we timed out on the notification. There may be prior
1931                  * RX frames pending in the RX queue before the notification
1932                  * that might get processed between now and the actual deletion
1933                  * and we would re-arm the timer although we are deleting the
1934                  * reorder buffer.
1935                  */
1936                 reorder_buf->removed = true;
1937                 spin_unlock_bh(&reorder_buf->lock);
1938                 del_timer_sync(&reorder_buf->reorder_timer);
1939         }
1940 }
1941
1942 static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1943                                         u32 sta_id,
1944                                         struct iwl_mvm_baid_data *data,
1945                                         u16 ssn, u8 buf_size)
1946 {
1947         int i;
1948
1949         for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1950                 struct iwl_mvm_reorder_buffer *reorder_buf =
1951                         &data->reorder_buf[i];
1952                 int j;
1953
1954                 reorder_buf->num_stored = 0;
1955                 reorder_buf->head_sn = ssn;
1956                 reorder_buf->buf_size = buf_size;
1957                 /* rx reorder timer */
1958                 reorder_buf->reorder_timer.function =
1959                         iwl_mvm_reorder_timer_expired;
1960                 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
1961                 init_timer(&reorder_buf->reorder_timer);
1962                 spin_lock_init(&reorder_buf->lock);
1963                 reorder_buf->mvm = mvm;
1964                 reorder_buf->queue = i;
1965                 reorder_buf->sta_id = sta_id;
1966                 for (j = 0; j < reorder_buf->buf_size; j++)
1967                         __skb_queue_head_init(&reorder_buf->entries[j]);
1968         }
1969 }
1970
1971 int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1972                        int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
1973 {
1974         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1975         struct iwl_mvm_add_sta_cmd cmd = {};
1976         struct iwl_mvm_baid_data *baid_data = NULL;
1977         int ret;
1978         u32 status;
1979
1980         lockdep_assert_held(&mvm->mutex);
1981
1982         if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1983                 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1984                 return -ENOSPC;
1985         }
1986
1987         if (iwl_mvm_has_new_rx_api(mvm) && start) {
1988                 /*
1989                  * Allocate here so if allocation fails we can bail out early
1990                  * before starting the BA session in the firmware
1991                  */
1992                 baid_data = kzalloc(sizeof(*baid_data) +
1993                                     mvm->trans->num_rx_queues *
1994                                     sizeof(baid_data->reorder_buf[0]),
1995                                     GFP_KERNEL);
1996                 if (!baid_data)
1997                         return -ENOMEM;
1998         }
1999
2000         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2001         cmd.sta_id = mvm_sta->sta_id;
2002         cmd.add_modify = STA_MODE_MODIFY;
2003         if (start) {
2004                 cmd.add_immediate_ba_tid = (u8) tid;
2005                 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2006                 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
2007         } else {
2008                 cmd.remove_immediate_ba_tid = (u8) tid;
2009         }
2010         cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2011                                   STA_MODIFY_REMOVE_BA_TID;
2012
2013         status = ADD_STA_SUCCESS;
2014         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2015                                           iwl_mvm_add_sta_cmd_size(mvm),
2016                                           &cmd, &status);
2017         if (ret)
2018                 goto out_free;
2019
2020         switch (status & IWL_ADD_STA_STATUS_MASK) {
2021         case ADD_STA_SUCCESS:
2022                 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2023                              start ? "start" : "stopp");
2024                 break;
2025         case ADD_STA_IMMEDIATE_BA_FAILURE:
2026                 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2027                 ret = -ENOSPC;
2028                 break;
2029         default:
2030                 ret = -EIO;
2031                 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2032                         start ? "start" : "stopp", status);
2033                 break;
2034         }
2035
2036         if (ret)
2037                 goto out_free;
2038
2039         if (start) {
2040                 u8 baid;
2041
2042                 mvm->rx_ba_sessions++;
2043
2044                 if (!iwl_mvm_has_new_rx_api(mvm))
2045                         return 0;
2046
2047                 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2048                         ret = -EINVAL;
2049                         goto out_free;
2050                 }
2051                 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2052                             IWL_ADD_STA_BAID_SHIFT);
2053                 baid_data->baid = baid;
2054                 baid_data->timeout = timeout;
2055                 baid_data->last_rx = jiffies;
2056                 setup_timer(&baid_data->session_timer,
2057                             iwl_mvm_rx_agg_session_expired,
2058                             (unsigned long)&mvm->baid_map[baid]);
2059                 baid_data->mvm = mvm;
2060                 baid_data->tid = tid;
2061                 baid_data->sta_id = mvm_sta->sta_id;
2062
2063                 mvm_sta->tid_to_baid[tid] = baid;
2064                 if (timeout)
2065                         mod_timer(&baid_data->session_timer,
2066                                   TU_TO_EXP_TIME(timeout * 2));
2067
2068                 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2069                                             baid_data, ssn, buf_size);
2070                 /*
2071                  * protect the BA data with RCU to cover a case where our
2072                  * internal RX sync mechanism will timeout (not that it's
2073                  * supposed to happen) and we will free the session data while
2074                  * RX is being processed in parallel
2075                  */
2076                 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2077                              mvm_sta->sta_id, tid, baid);
2078                 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2079                 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2080         } else  {
2081                 u8 baid = mvm_sta->tid_to_baid[tid];
2082
2083                 if (mvm->rx_ba_sessions > 0)
2084                         /* check that restart flow didn't zero the counter */
2085                         mvm->rx_ba_sessions--;
2086                 if (!iwl_mvm_has_new_rx_api(mvm))
2087                         return 0;
2088
2089                 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2090                         return -EINVAL;
2091
2092                 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2093                 if (WARN_ON(!baid_data))
2094                         return -EINVAL;
2095
2096                 /* synchronize all rx queues so we can safely delete */
2097                 iwl_mvm_free_reorder(mvm, baid_data);
2098                 del_timer_sync(&baid_data->session_timer);
2099                 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2100                 kfree_rcu(baid_data, rcu_head);
2101                 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2102         }
2103         return 0;
2104
2105 out_free:
2106         kfree(baid_data);
2107         return ret;
2108 }
2109
2110 int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2111                        int tid, u8 queue, bool start)
2112 {
2113         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2114         struct iwl_mvm_add_sta_cmd cmd = {};
2115         int ret;
2116         u32 status;
2117
2118         lockdep_assert_held(&mvm->mutex);
2119
2120         if (start) {
2121                 mvm_sta->tfd_queue_msk |= BIT(queue);
2122                 mvm_sta->tid_disable_agg &= ~BIT(tid);
2123         } else {
2124                 /* In DQA-mode the queue isn't removed on agg termination */
2125                 if (!iwl_mvm_is_dqa_supported(mvm))
2126                         mvm_sta->tfd_queue_msk &= ~BIT(queue);
2127                 mvm_sta->tid_disable_agg |= BIT(tid);
2128         }
2129
2130         cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2131         cmd.sta_id = mvm_sta->sta_id;
2132         cmd.add_modify = STA_MODE_MODIFY;
2133         cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
2134         cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2135         cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2136
2137         status = ADD_STA_SUCCESS;
2138         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2139                                           iwl_mvm_add_sta_cmd_size(mvm),
2140                                           &cmd, &status);
2141         if (ret)
2142                 return ret;
2143
2144         switch (status & IWL_ADD_STA_STATUS_MASK) {
2145         case ADD_STA_SUCCESS:
2146                 break;
2147         default:
2148                 ret = -EIO;
2149                 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2150                         start ? "start" : "stopp", status);
2151                 break;
2152         }
2153
2154         return ret;
2155 }
2156
2157 const u8 tid_to_mac80211_ac[] = {
2158         IEEE80211_AC_BE,
2159         IEEE80211_AC_BK,
2160         IEEE80211_AC_BK,
2161         IEEE80211_AC_BE,
2162         IEEE80211_AC_VI,
2163         IEEE80211_AC_VI,
2164         IEEE80211_AC_VO,
2165         IEEE80211_AC_VO,
2166         IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2167 };
2168
2169 static const u8 tid_to_ucode_ac[] = {
2170         AC_BE,
2171         AC_BK,
2172         AC_BK,
2173         AC_BE,
2174         AC_VI,
2175         AC_VI,
2176         AC_VO,
2177         AC_VO,
2178 };
2179
2180 int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2181                              struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2182 {
2183         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2184         struct iwl_mvm_tid_data *tid_data;
2185         int txq_id;
2186         int ret;
2187
2188         if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2189                 return -EINVAL;
2190
2191         if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2192                 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2193                         mvmsta->tid_data[tid].state);
2194                 return -ENXIO;
2195         }
2196
2197         lockdep_assert_held(&mvm->mutex);
2198
2199         spin_lock_bh(&mvmsta->lock);
2200
2201         /* possible race condition - we entered D0i3 while starting agg */
2202         if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2203                 spin_unlock_bh(&mvmsta->lock);
2204                 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2205                 return -EIO;
2206         }
2207
2208         spin_lock(&mvm->queue_info_lock);
2209
2210         /*
2211          * Note the possible cases:
2212          *  1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2213          *  2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2214          *      one and mark it as reserved
2215          *  3. In DQA mode, but no traffic yet on this TID: same treatment as in
2216          *      non-DQA mode, since the TXQ hasn't yet been allocated
2217          */
2218         txq_id = mvmsta->tid_data[tid].txq_id;
2219         if (iwl_mvm_is_dqa_supported(mvm) &&
2220             unlikely(mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_SHARED)) {
2221                 ret = -ENXIO;
2222                 IWL_DEBUG_TX_QUEUES(mvm,
2223                                     "Can't start tid %d agg on shared queue!\n",
2224                                     tid);
2225                 goto release_locks;
2226         } else if (!iwl_mvm_is_dqa_supported(mvm) ||
2227             mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
2228                 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2229                                                  mvm->first_agg_queue,
2230                                                  mvm->last_agg_queue);
2231                 if (txq_id < 0) {
2232                         ret = txq_id;
2233                         IWL_ERR(mvm, "Failed to allocate agg queue\n");
2234                         goto release_locks;
2235                 }
2236
2237                 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2238                 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2239         }
2240
2241         spin_unlock(&mvm->queue_info_lock);
2242
2243         IWL_DEBUG_TX_QUEUES(mvm,
2244                             "AGG for tid %d will be on queue #%d\n",
2245                             tid, txq_id);
2246
2247         tid_data = &mvmsta->tid_data[tid];
2248         tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2249         tid_data->txq_id = txq_id;
2250         *ssn = tid_data->ssn;
2251
2252         IWL_DEBUG_TX_QUEUES(mvm,
2253                             "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2254                             mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2255                             tid_data->next_reclaimed);
2256
2257         if (tid_data->ssn == tid_data->next_reclaimed) {
2258                 tid_data->state = IWL_AGG_STARTING;
2259                 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2260         } else {
2261                 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2262         }
2263
2264         ret = 0;
2265         goto out;
2266
2267 release_locks:
2268         spin_unlock(&mvm->queue_info_lock);
2269 out:
2270         spin_unlock_bh(&mvmsta->lock);
2271
2272         return ret;
2273 }
2274
2275 int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2276                             struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2277                             bool amsdu)
2278 {
2279         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2280         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2281         unsigned int wdg_timeout =
2282                 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2283         int queue, ret;
2284         bool alloc_queue = true;
2285         enum iwl_mvm_queue_status queue_status;
2286         u16 ssn;
2287
2288         struct iwl_trans_txq_scd_cfg cfg = {
2289                 .sta_id = mvmsta->sta_id,
2290                 .tid = tid,
2291                 .frame_limit = buf_size,
2292                 .aggregate = true,
2293         };
2294
2295         BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2296                      != IWL_MAX_TID_COUNT);
2297
2298         buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2299
2300         spin_lock_bh(&mvmsta->lock);
2301         ssn = tid_data->ssn;
2302         queue = tid_data->txq_id;
2303         tid_data->state = IWL_AGG_ON;
2304         mvmsta->agg_tids |= BIT(tid);
2305         tid_data->ssn = 0xffff;
2306         tid_data->amsdu_in_ampdu_allowed = amsdu;
2307         spin_unlock_bh(&mvmsta->lock);
2308
2309         cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2310
2311         spin_lock_bh(&mvm->queue_info_lock);
2312         queue_status = mvm->queue_info[queue].status;
2313         spin_unlock_bh(&mvm->queue_info_lock);
2314
2315         /* In DQA mode, the existing queue might need to be reconfigured */
2316         if (iwl_mvm_is_dqa_supported(mvm)) {
2317                 /* Maybe there is no need to even alloc a queue... */
2318                 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2319                         alloc_queue = false;
2320
2321                 /*
2322                  * Only reconfig the SCD for the queue if the window size has
2323                  * changed from current (become smaller)
2324                  */
2325                 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2326                         /*
2327                          * If reconfiguring an existing queue, it first must be
2328                          * drained
2329                          */
2330                         ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
2331                                                             BIT(queue));
2332                         if (ret) {
2333                                 IWL_ERR(mvm,
2334                                         "Error draining queue before reconfig\n");
2335                                 return ret;
2336                         }
2337
2338                         ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2339                                                    mvmsta->sta_id, tid,
2340                                                    buf_size, ssn);
2341                         if (ret) {
2342                                 IWL_ERR(mvm,
2343                                         "Error reconfiguring TXQ #%d\n", queue);
2344                                 return ret;
2345                         }
2346                 }
2347         }
2348
2349         if (alloc_queue)
2350                 iwl_mvm_enable_txq(mvm, queue,
2351                                    vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2352                                    &cfg, wdg_timeout);
2353
2354         /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2355         if (queue_status != IWL_MVM_QUEUE_SHARED) {
2356                 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2357                 if (ret)
2358                         return -EIO;
2359         }
2360
2361         /* No need to mark as reserved */
2362         spin_lock_bh(&mvm->queue_info_lock);
2363         mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2364         spin_unlock_bh(&mvm->queue_info_lock);
2365
2366         /*
2367          * Even though in theory the peer could have different
2368          * aggregation reorder buffer sizes for different sessions,
2369          * our ucode doesn't allow for that and has a global limit
2370          * for each station. Therefore, use the minimum of all the
2371          * aggregation sessions and our default value.
2372          */
2373         mvmsta->max_agg_bufsize =
2374                 min(mvmsta->max_agg_bufsize, buf_size);
2375         mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2376
2377         IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2378                      sta->addr, tid);
2379
2380         return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
2381 }
2382
2383 int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2384                             struct ieee80211_sta *sta, u16 tid)
2385 {
2386         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2387         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2388         u16 txq_id;
2389         int err;
2390
2391         /*
2392          * If mac80211 is cleaning its state, then say that we finished since
2393          * our state has been cleared anyway.
2394          */
2395         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2396                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2397                 return 0;
2398         }
2399
2400         spin_lock_bh(&mvmsta->lock);
2401
2402         txq_id = tid_data->txq_id;
2403
2404         IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2405                             mvmsta->sta_id, tid, txq_id, tid_data->state);
2406
2407         mvmsta->agg_tids &= ~BIT(tid);
2408
2409         spin_lock_bh(&mvm->queue_info_lock);
2410         /*
2411          * The TXQ is marked as reserved only if no traffic came through yet
2412          * This means no traffic has been sent on this TID (agg'd or not), so
2413          * we no longer have use for the queue. Since it hasn't even been
2414          * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2415          * free.
2416          */
2417         if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2418                 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2419
2420         spin_unlock_bh(&mvm->queue_info_lock);
2421
2422         switch (tid_data->state) {
2423         case IWL_AGG_ON:
2424                 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2425
2426                 IWL_DEBUG_TX_QUEUES(mvm,
2427                                     "ssn = %d, next_recl = %d\n",
2428                                     tid_data->ssn, tid_data->next_reclaimed);
2429
2430                 /* There are still packets for this RA / TID in the HW */
2431                 if (tid_data->ssn != tid_data->next_reclaimed) {
2432                         tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2433                         err = 0;
2434                         break;
2435                 }
2436
2437                 tid_data->ssn = 0xffff;
2438                 tid_data->state = IWL_AGG_OFF;
2439                 spin_unlock_bh(&mvmsta->lock);
2440
2441                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2442
2443                 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2444
2445                 if (!iwl_mvm_is_dqa_supported(mvm)) {
2446                         int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2447
2448                         iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2449                 }
2450                 return 0;
2451         case IWL_AGG_STARTING:
2452         case IWL_EMPTYING_HW_QUEUE_ADDBA:
2453                 /*
2454                  * The agg session has been stopped before it was set up. This
2455                  * can happen when the AddBA timer times out for example.
2456                  */
2457
2458                 /* No barriers since we are under mutex */
2459                 lockdep_assert_held(&mvm->mutex);
2460
2461                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2462                 tid_data->state = IWL_AGG_OFF;
2463                 err = 0;
2464                 break;
2465         default:
2466                 IWL_ERR(mvm,
2467                         "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2468                         mvmsta->sta_id, tid, tid_data->state);
2469                 IWL_ERR(mvm,
2470                         "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2471                 err = -EINVAL;
2472         }
2473
2474         spin_unlock_bh(&mvmsta->lock);
2475
2476         return err;
2477 }
2478
2479 int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2480                             struct ieee80211_sta *sta, u16 tid)
2481 {
2482         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2483         struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2484         u16 txq_id;
2485         enum iwl_mvm_agg_state old_state;
2486
2487         /*
2488          * First set the agg state to OFF to avoid calling
2489          * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2490          */
2491         spin_lock_bh(&mvmsta->lock);
2492         txq_id = tid_data->txq_id;
2493         IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2494                             mvmsta->sta_id, tid, txq_id, tid_data->state);
2495         old_state = tid_data->state;
2496         tid_data->state = IWL_AGG_OFF;
2497         mvmsta->agg_tids &= ~BIT(tid);
2498         spin_unlock_bh(&mvmsta->lock);
2499
2500         spin_lock_bh(&mvm->queue_info_lock);
2501         /*
2502          * The TXQ is marked as reserved only if no traffic came through yet
2503          * This means no traffic has been sent on this TID (agg'd or not), so
2504          * we no longer have use for the queue. Since it hasn't even been
2505          * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2506          * free.
2507          */
2508         if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2509                 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2510         spin_unlock_bh(&mvm->queue_info_lock);
2511
2512         if (old_state >= IWL_AGG_ON) {
2513                 iwl_mvm_drain_sta(mvm, mvmsta, true);
2514                 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2515                         IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
2516                 iwl_trans_wait_tx_queue_empty(mvm->trans,
2517                                               mvmsta->tfd_queue_msk);
2518                 iwl_mvm_drain_sta(mvm, mvmsta, false);
2519
2520                 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2521
2522                 if (!iwl_mvm_is_dqa_supported(mvm)) {
2523                         int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2524
2525                         iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2526                                             tid, 0);
2527                 }
2528         }
2529
2530         return 0;
2531 }
2532
2533 static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2534 {
2535         int i, max = -1, max_offs = -1;
2536
2537         lockdep_assert_held(&mvm->mutex);
2538
2539         /* Pick the unused key offset with the highest 'deleted'
2540          * counter. Every time a key is deleted, all the counters
2541          * are incremented and the one that was just deleted is
2542          * reset to zero. Thus, the highest counter is the one
2543          * that was deleted longest ago. Pick that one.
2544          */
2545         for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2546                 if (test_bit(i, mvm->fw_key_table))
2547                         continue;
2548                 if (mvm->fw_key_deleted[i] > max) {
2549                         max = mvm->fw_key_deleted[i];
2550                         max_offs = i;
2551                 }
2552         }
2553
2554         if (max_offs < 0)
2555                 return STA_KEY_IDX_INVALID;
2556
2557         return max_offs;
2558 }
2559
2560 static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2561                                                struct ieee80211_vif *vif,
2562                                                struct ieee80211_sta *sta)
2563 {
2564         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2565
2566         if (sta)
2567                 return iwl_mvm_sta_from_mac80211(sta);
2568
2569         /*
2570          * The device expects GTKs for station interfaces to be
2571          * installed as GTKs for the AP station. If we have no
2572          * station ID, then use AP's station ID.
2573          */
2574         if (vif->type == NL80211_IFTYPE_STATION &&
2575             mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2576                 u8 sta_id = mvmvif->ap_sta_id;
2577
2578                 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2579                                             lockdep_is_held(&mvm->mutex));
2580
2581                 /*
2582                  * It is possible that the 'sta' parameter is NULL,
2583                  * for example when a GTK is removed - the sta_id will then
2584                  * be the AP ID, and no station was passed by mac80211.
2585                  */
2586                 if (IS_ERR_OR_NULL(sta))
2587                         return NULL;
2588
2589                 return iwl_mvm_sta_from_mac80211(sta);
2590         }
2591
2592         return NULL;
2593 }
2594
2595 static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2596                                 struct iwl_mvm_sta *mvm_sta,
2597                                 struct ieee80211_key_conf *keyconf, bool mcast,
2598                                 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2599                                 u8 key_offset)
2600 {
2601         struct iwl_mvm_add_sta_key_cmd cmd = {};
2602         __le16 key_flags;
2603         int ret;
2604         u32 status;
2605         u16 keyidx;
2606         int i;
2607         u8 sta_id = mvm_sta->sta_id;
2608
2609         keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2610                  STA_KEY_FLG_KEYID_MSK;
2611         key_flags = cpu_to_le16(keyidx);
2612         key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2613
2614         switch (keyconf->cipher) {
2615         case WLAN_CIPHER_SUITE_TKIP:
2616                 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
2617                 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
2618                 for (i = 0; i < 5; i++)
2619                         cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2620                 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2621                 break;
2622         case WLAN_CIPHER_SUITE_CCMP:
2623                 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
2624                 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2625                 break;
2626         case WLAN_CIPHER_SUITE_WEP104:
2627                 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
2628                 /* fall through */
2629         case WLAN_CIPHER_SUITE_WEP40:
2630                 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2631                 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2632                 break;
2633         case WLAN_CIPHER_SUITE_GCMP_256:
2634                 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2635                 /* fall through */
2636         case WLAN_CIPHER_SUITE_GCMP:
2637                 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
2638                 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2639                 break;
2640         default:
2641                 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2642                 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2643         }
2644
2645         if (mcast)
2646                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2647
2648         cmd.key_offset = key_offset;
2649         cmd.key_flags = key_flags;
2650         cmd.sta_id = sta_id;
2651
2652         status = ADD_STA_SUCCESS;
2653         if (cmd_flags & CMD_ASYNC)
2654                 ret =  iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2655                                             sizeof(cmd), &cmd);
2656         else
2657                 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2658                                                   &cmd, &status);
2659
2660         switch (status) {
2661         case ADD_STA_SUCCESS:
2662                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2663                 break;
2664         default:
2665                 ret = -EIO;
2666                 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2667                 break;
2668         }
2669
2670         return ret;
2671 }
2672
2673 static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2674                                  struct ieee80211_key_conf *keyconf,
2675                                  u8 sta_id, bool remove_key)
2676 {
2677         struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2678
2679         /* verify the key details match the required command's expectations */
2680         if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2681                     (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
2682                     (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2683                      keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
2684                      keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
2685                 return -EINVAL;
2686
2687         if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
2688                     keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
2689                 return -EINVAL;
2690
2691         igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2692         igtk_cmd.sta_id = cpu_to_le32(sta_id);
2693
2694         if (remove_key) {
2695                 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2696         } else {
2697                 struct ieee80211_key_seq seq;
2698                 const u8 *pn;
2699
2700                 switch (keyconf->cipher) {
2701                 case WLAN_CIPHER_SUITE_AES_CMAC:
2702                         igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
2703                         break;
2704                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2705                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2706                         igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
2707                         break;
2708                 default:
2709                         return -EINVAL;
2710                 }
2711
2712                 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
2713                 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2714                         igtk_cmd.ctrl_flags |=
2715                                 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
2716                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2717                 pn = seq.aes_cmac.pn;
2718                 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2719                                                        ((u64) pn[4] << 8) |
2720                                                        ((u64) pn[3] << 16) |
2721                                                        ((u64) pn[2] << 24) |
2722                                                        ((u64) pn[1] << 32) |
2723                                                        ((u64) pn[0] << 40));
2724         }
2725
2726         IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2727                        remove_key ? "removing" : "installing",
2728                        igtk_cmd.sta_id);
2729
2730         if (!iwl_mvm_has_new_rx_api(mvm)) {
2731                 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
2732                         .ctrl_flags = igtk_cmd.ctrl_flags,
2733                         .key_id = igtk_cmd.key_id,
2734                         .sta_id = igtk_cmd.sta_id,
2735                         .receive_seq_cnt = igtk_cmd.receive_seq_cnt
2736                 };
2737
2738                 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
2739                        ARRAY_SIZE(igtk_cmd_v1.igtk));
2740                 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2741                                             sizeof(igtk_cmd_v1), &igtk_cmd_v1);
2742         }
2743         return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2744                                     sizeof(igtk_cmd), &igtk_cmd);
2745 }
2746
2747
2748 static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2749                                        struct ieee80211_vif *vif,
2750                                        struct ieee80211_sta *sta)
2751 {
2752         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2753
2754         if (sta)
2755                 return sta->addr;
2756
2757         if (vif->type == NL80211_IFTYPE_STATION &&
2758             mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2759                 u8 sta_id = mvmvif->ap_sta_id;
2760                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2761                                                 lockdep_is_held(&mvm->mutex));
2762                 return sta->addr;
2763         }
2764
2765
2766         return NULL;
2767 }
2768
2769 static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2770                                  struct ieee80211_vif *vif,
2771                                  struct ieee80211_sta *sta,
2772                                  struct ieee80211_key_conf *keyconf,
2773                                  u8 key_offset,
2774                                  bool mcast)
2775 {
2776         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2777         int ret;
2778         const u8 *addr;
2779         struct ieee80211_key_seq seq;
2780         u16 p1k[5];
2781
2782         switch (keyconf->cipher) {
2783         case WLAN_CIPHER_SUITE_TKIP:
2784                 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2785                 /* get phase 1 key from mac80211 */
2786                 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2787                 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
2788                 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
2789                                            seq.tkip.iv32, p1k, 0, key_offset);
2790                 break;
2791         case WLAN_CIPHER_SUITE_CCMP:
2792         case WLAN_CIPHER_SUITE_WEP40:
2793         case WLAN_CIPHER_SUITE_WEP104:
2794         case WLAN_CIPHER_SUITE_GCMP:
2795         case WLAN_CIPHER_SUITE_GCMP_256:
2796                 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
2797                                            0, NULL, 0, key_offset);
2798                 break;
2799         default:
2800                 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
2801                                            0, NULL, 0, key_offset);
2802         }
2803
2804         return ret;
2805 }
2806
2807 static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2808                                     struct ieee80211_key_conf *keyconf,
2809                                     bool mcast)
2810 {
2811         struct iwl_mvm_add_sta_key_cmd cmd = {};
2812         __le16 key_flags;
2813         int ret;
2814         u32 status;
2815
2816         key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2817                                  STA_KEY_FLG_KEYID_MSK);
2818         key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2819         key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2820
2821         if (mcast)
2822                 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2823
2824         cmd.key_flags = key_flags;
2825         cmd.key_offset = keyconf->hw_key_idx;
2826         cmd.sta_id = sta_id;
2827
2828         status = ADD_STA_SUCCESS;
2829         ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2830                                           &cmd, &status);
2831
2832         switch (status) {
2833         case ADD_STA_SUCCESS:
2834                 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2835                 break;
2836         default:
2837                 ret = -EIO;
2838                 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2839                 break;
2840         }
2841
2842         return ret;
2843 }
2844
2845 int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2846                         struct ieee80211_vif *vif,
2847                         struct ieee80211_sta *sta,
2848                         struct ieee80211_key_conf *keyconf,
2849                         u8 key_offset)
2850 {
2851         bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
2852         struct iwl_mvm_sta *mvm_sta;
2853         u8 sta_id;
2854         int ret;
2855         static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
2856
2857         lockdep_assert_held(&mvm->mutex);
2858
2859         /* Get the station id from the mvm local station table */
2860         mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2861         if (!mvm_sta) {
2862                 IWL_ERR(mvm, "Failed to find station\n");
2863                 return -EINVAL;
2864         }
2865         sta_id = mvm_sta->sta_id;
2866
2867         if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2868             keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2869             keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
2870                 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2871                 goto end;
2872         }
2873
2874         /*
2875          * It is possible that the 'sta' parameter is NULL, and thus
2876          * there is a need to retrieve  the sta from the local station table.
2877          */
2878         if (!sta) {
2879                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2880                                                 lockdep_is_held(&mvm->mutex));
2881                 if (IS_ERR_OR_NULL(sta)) {
2882                         IWL_ERR(mvm, "Invalid station id\n");
2883                         return -EINVAL;
2884                 }
2885         }
2886
2887         if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2888                 return -EINVAL;
2889
2890         /* If the key_offset is not pre-assigned, we need to find a
2891          * new offset to use.  In normal cases, the offset is not
2892          * pre-assigned, but during HW_RESTART we want to reuse the
2893          * same indices, so we pass them when this function is called.
2894          *
2895          * In D3 entry, we need to hardcoded the indices (because the
2896          * firmware hardcodes the PTK offset to 0).  In this case, we
2897          * need to make sure we don't overwrite the hw_key_idx in the
2898          * keyconf structure, because otherwise we cannot configure
2899          * the original ones back when resuming.
2900          */
2901         if (key_offset == STA_KEY_IDX_INVALID) {
2902                 key_offset  = iwl_mvm_set_fw_key_idx(mvm);
2903                 if (key_offset == STA_KEY_IDX_INVALID)
2904                         return -ENOSPC;
2905                 keyconf->hw_key_idx = key_offset;
2906         }
2907
2908         ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
2909         if (ret)
2910                 goto end;
2911
2912         /*
2913          * For WEP, the same key is used for multicast and unicast. Upload it
2914          * again, using the same key offset, and now pointing the other one
2915          * to the same key slot (offset).
2916          * If this fails, remove the original as well.
2917          */
2918         if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2919             keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
2920                 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
2921                                             key_offset, !mcast);
2922                 if (ret) {
2923                         __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2924                         goto end;
2925                 }
2926         }
2927
2928         __set_bit(key_offset, mvm->fw_key_table);
2929
2930 end:
2931         IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2932                       keyconf->cipher, keyconf->keylen, keyconf->keyidx,
2933                       sta ? sta->addr : zero_addr, ret);
2934         return ret;
2935 }
2936
2937 int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2938                            struct ieee80211_vif *vif,
2939                            struct ieee80211_sta *sta,
2940                            struct ieee80211_key_conf *keyconf)
2941 {
2942         bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
2943         struct iwl_mvm_sta *mvm_sta;
2944         u8 sta_id = IWL_MVM_STATION_COUNT;
2945         int ret, i;
2946
2947         lockdep_assert_held(&mvm->mutex);
2948
2949         /* Get the station from the mvm local station table */
2950         mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2951
2952         IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2953                       keyconf->keyidx, sta_id);
2954
2955         if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2956             keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2957             keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2958                 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2959
2960         if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2961                 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2962                         keyconf->hw_key_idx);
2963                 return -ENOENT;
2964         }
2965
2966         /* track which key was deleted last */
2967         for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2968                 if (mvm->fw_key_deleted[i] < U8_MAX)
2969                         mvm->fw_key_deleted[i]++;
2970         }
2971         mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2972
2973         if (!mvm_sta) {
2974                 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2975                 return 0;
2976         }
2977
2978         sta_id = mvm_sta->sta_id;
2979
2980         ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2981         if (ret)
2982                 return ret;
2983
2984         /* delete WEP key twice to get rid of (now useless) offset */
2985         if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2986             keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2987                 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2988
2989         return ret;
2990 }
2991
2992 void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2993                              struct ieee80211_vif *vif,
2994                              struct ieee80211_key_conf *keyconf,
2995                              struct ieee80211_sta *sta, u32 iv32,
2996                              u16 *phase1key)
2997 {
2998         struct iwl_mvm_sta *mvm_sta;
2999         bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3000
3001         rcu_read_lock();
3002
3003         mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3004         if (WARN_ON_ONCE(!mvm_sta))
3005                 goto unlock;
3006         iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
3007                              iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
3008
3009  unlock:
3010         rcu_read_unlock();
3011 }
3012
3013 void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3014                                 struct ieee80211_sta *sta)
3015 {
3016         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3017         struct iwl_mvm_add_sta_cmd cmd = {
3018                 .add_modify = STA_MODE_MODIFY,
3019                 .sta_id = mvmsta->sta_id,
3020                 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3021                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3022         };
3023         int ret;
3024
3025         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3026                                    iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3027         if (ret)
3028                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3029 }
3030
3031 void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3032                                        struct ieee80211_sta *sta,
3033                                        enum ieee80211_frame_release_type reason,
3034                                        u16 cnt, u16 tids, bool more_data,
3035                                        bool single_sta_queue)
3036 {
3037         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3038         struct iwl_mvm_add_sta_cmd cmd = {
3039                 .add_modify = STA_MODE_MODIFY,
3040                 .sta_id = mvmsta->sta_id,
3041                 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3042                 .sleep_tx_count = cpu_to_le16(cnt),
3043                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3044         };
3045         int tid, ret;
3046         unsigned long _tids = tids;
3047
3048         /* convert TIDs to ACs - we don't support TSPEC so that's OK
3049          * Note that this field is reserved and unused by firmware not
3050          * supporting GO uAPSD, so it's safe to always do this.
3051          */
3052         for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3053                 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3054
3055         /* If we're releasing frames from aggregation or dqa queues then check
3056          * if all the queues that we're releasing frames from, combined, have:
3057          *  - more frames than the service period, in which case more_data
3058          *    needs to be set
3059          *  - fewer than 'cnt' frames, in which case we need to adjust the
3060          *    firmware command (but do that unconditionally)
3061          */
3062         if (single_sta_queue) {
3063                 int remaining = cnt;
3064                 int sleep_tx_count;
3065
3066                 spin_lock_bh(&mvmsta->lock);
3067                 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3068                         struct iwl_mvm_tid_data *tid_data;
3069                         u16 n_queued;
3070
3071                         tid_data = &mvmsta->tid_data[tid];
3072                         if (WARN(!iwl_mvm_is_dqa_supported(mvm) &&
3073                                  tid_data->state != IWL_AGG_ON &&
3074                                  tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3075                                  "TID %d state is %d\n",
3076                                  tid, tid_data->state)) {
3077                                 spin_unlock_bh(&mvmsta->lock);
3078                                 ieee80211_sta_eosp(sta);
3079                                 return;
3080                         }
3081
3082                         n_queued = iwl_mvm_tid_queued(tid_data);
3083                         if (n_queued > remaining) {
3084                                 more_data = true;
3085                                 remaining = 0;
3086                                 break;
3087                         }
3088                         remaining -= n_queued;
3089                 }
3090                 sleep_tx_count = cnt - remaining;
3091                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3092                         mvmsta->sleep_tx_count = sleep_tx_count;
3093                 spin_unlock_bh(&mvmsta->lock);
3094
3095                 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3096                 if (WARN_ON(cnt - remaining == 0)) {
3097                         ieee80211_sta_eosp(sta);
3098                         return;
3099                 }
3100         }
3101
3102         /* Note: this is ignored by firmware not supporting GO uAPSD */
3103         if (more_data)
3104                 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
3105
3106         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3107                 mvmsta->next_status_eosp = true;
3108                 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
3109         } else {
3110                 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
3111         }
3112
3113         /* block the Tx queues until the FW updated the sleep Tx count */
3114         iwl_trans_block_txq_ptrs(mvm->trans, true);
3115
3116         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3117                                    CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3118                                    iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3119         if (ret)
3120                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3121 }
3122
3123 void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3124                            struct iwl_rx_cmd_buffer *rxb)
3125 {
3126         struct iwl_rx_packet *pkt = rxb_addr(rxb);
3127         struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3128         struct ieee80211_sta *sta;
3129         u32 sta_id = le32_to_cpu(notif->sta_id);
3130
3131         if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3132                 return;
3133
3134         rcu_read_lock();
3135         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3136         if (!IS_ERR_OR_NULL(sta))
3137                 ieee80211_sta_eosp(sta);
3138         rcu_read_unlock();
3139 }
3140
3141 void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3142                                    struct iwl_mvm_sta *mvmsta, bool disable)
3143 {
3144         struct iwl_mvm_add_sta_cmd cmd = {
3145                 .add_modify = STA_MODE_MODIFY,
3146                 .sta_id = mvmsta->sta_id,
3147                 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3148                 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3149                 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3150         };
3151         int ret;
3152
3153         ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3154                                    iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3155         if (ret)
3156                 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3157 }
3158
3159 void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3160                                       struct ieee80211_sta *sta,
3161                                       bool disable)
3162 {
3163         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3164
3165         spin_lock_bh(&mvm_sta->lock);
3166
3167         if (mvm_sta->disable_tx == disable) {
3168                 spin_unlock_bh(&mvm_sta->lock);
3169                 return;
3170         }
3171
3172         mvm_sta->disable_tx = disable;
3173
3174         /*
3175          * Tell mac80211 to start/stop queuing tx for this station,
3176          * but don't stop queuing if there are still pending frames
3177          * for this station.
3178          */
3179         if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3180                 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3181
3182         iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3183
3184         spin_unlock_bh(&mvm_sta->lock);
3185 }
3186
3187 void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3188                                        struct iwl_mvm_vif *mvmvif,
3189                                        bool disable)
3190 {
3191         struct ieee80211_sta *sta;
3192         struct iwl_mvm_sta *mvm_sta;
3193         int i;
3194
3195         lockdep_assert_held(&mvm->mutex);
3196
3197         /* Block/unblock all the stations of the given mvmvif */
3198         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
3199                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3200                                                 lockdep_is_held(&mvm->mutex));
3201                 if (IS_ERR_OR_NULL(sta))
3202                         continue;
3203
3204                 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3205                 if (mvm_sta->mac_id_n_color !=
3206                     FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3207                         continue;
3208
3209                 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3210         }
3211 }
3212
3213 void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3214 {
3215         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3216         struct iwl_mvm_sta *mvmsta;
3217
3218         rcu_read_lock();
3219
3220         mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3221
3222         if (!WARN_ON(!mvmsta))
3223                 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3224
3225         rcu_read_unlock();
3226 }