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