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