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