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