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