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