]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/net/wireless/intel/iwlwifi/mvm/sta.c
iwlwifi: edit the 9000 series PCI IDs
[mirror_ubuntu-eoan-kernel.git] / drivers / net / wireless / intel / iwlwifi / mvm / sta.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
fa7878e7
AO
8 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
854c5705 10 * Copyright(c) 2016 Intel Deutschland GmbH
8ca151b5
JB
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * USA
25 *
26 * The full GNU General Public License is included in this distribution
410dc5aa 27 * in the file called COPYING.
8ca151b5
JB
28 *
29 * Contact Information:
cb2f8277 30 * Intel Linux Wireless <linuxwifi@intel.com>
8ca151b5
JB
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
fa7878e7
AO
35 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
854c5705 37 * Copyright(c) 2016 Intel Deutschland GmbH
8ca151b5
JB
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
49 * distribution.
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *****************************************************************************/
67#include <net/mac80211.h>
68
69#include "mvm.h"
70#include "sta.h"
9ee718aa 71#include "rs.h"
8ca151b5 72
854c5705
SS
73/*
74 * New version of ADD_STA_sta command added new fields at the end of the
75 * structure, so sending the size of the relevant API's structure is enough to
76 * support both API versions.
77 */
78static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
79{
80 return iwl_mvm_has_new_rx_api(mvm) ?
81 sizeof(struct iwl_mvm_add_sta_cmd) :
82 sizeof(struct iwl_mvm_add_sta_cmd_v7);
83}
84
b92e661b
EP
85static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
86 enum nl80211_iftype iftype)
8ca151b5
JB
87{
88 int sta_id;
b92e661b 89 u32 reserved_ids = 0;
8ca151b5 90
b92e661b 91 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
8ca151b5
JB
92 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
93
94 lockdep_assert_held(&mvm->mutex);
95
b92e661b
EP
96 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
97 if (iftype != NL80211_IFTYPE_STATION)
98 reserved_ids = BIT(0);
99
8ca151b5 100 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
b92e661b
EP
101 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
102 if (BIT(sta_id) & reserved_ids)
103 continue;
104
8ca151b5
JB
105 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
106 lockdep_is_held(&mvm->mutex)))
107 return sta_id;
b92e661b 108 }
8ca151b5
JB
109 return IWL_MVM_STATION_COUNT;
110}
111
7a453973
JB
112/* send station add/update command to firmware */
113int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
24afba76 114 bool update, unsigned int flags)
8ca151b5 115{
9d8ce6af 116 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4b8265ab
EG
117 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
118 .sta_id = mvm_sta->sta_id,
119 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
120 .add_modify = update ? 1 : 0,
121 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
122 STA_FLG_MIMO_EN_MSK),
cf0cda19 123 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
4b8265ab 124 };
8ca151b5
JB
125 int ret;
126 u32 status;
127 u32 agg_size = 0, mpdu_dens = 0;
128
24afba76 129 if (!update || (flags & STA_MODIFY_QUEUES)) {
7a453973
JB
130 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
131 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
24afba76
LK
132
133 if (flags & STA_MODIFY_QUEUES)
134 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
7a453973 135 }
5bc5aaad
JB
136
137 switch (sta->bandwidth) {
138 case IEEE80211_STA_RX_BW_160:
139 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
140 /* fall through */
141 case IEEE80211_STA_RX_BW_80:
142 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
143 /* fall through */
144 case IEEE80211_STA_RX_BW_40:
145 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
146 /* fall through */
147 case IEEE80211_STA_RX_BW_20:
148 if (sta->ht_cap.ht_supported)
149 add_sta_cmd.station_flags |=
150 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
151 break;
152 }
153
154 switch (sta->rx_nss) {
155 case 1:
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
157 break;
158 case 2:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
160 break;
161 case 3 ... 8:
162 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
163 break;
164 }
165
166 switch (sta->smps_mode) {
167 case IEEE80211_SMPS_AUTOMATIC:
168 case IEEE80211_SMPS_NUM_MODES:
169 WARN_ON(1);
170 break;
171 case IEEE80211_SMPS_STATIC:
172 /* override NSS */
173 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
174 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
175 break;
176 case IEEE80211_SMPS_DYNAMIC:
177 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
178 break;
179 case IEEE80211_SMPS_OFF:
180 /* nothing */
181 break;
182 }
8ca151b5
JB
183
184 if (sta->ht_cap.ht_supported) {
185 add_sta_cmd.station_flags_msk |=
186 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
187 STA_FLG_AGG_MPDU_DENS_MSK);
188
189 mpdu_dens = sta->ht_cap.ampdu_density;
190 }
191
192 if (sta->vht_cap.vht_supported) {
193 agg_size = sta->vht_cap.cap &
194 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
195 agg_size >>=
196 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
197 } else if (sta->ht_cap.ht_supported) {
198 agg_size = sta->ht_cap.ampdu_factor;
199 }
200
201 add_sta_cmd.station_flags |=
202 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
203 add_sta_cmd.station_flags |=
204 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
205
206 status = ADD_STA_SUCCESS;
854c5705
SS
207 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
208 iwl_mvm_add_sta_cmd_size(mvm),
f9dc0004 209 &add_sta_cmd, &status);
8ca151b5
JB
210 if (ret)
211 return ret;
212
837c4da9 213 switch (status & IWL_ADD_STA_STATUS_MASK) {
8ca151b5
JB
214 case ADD_STA_SUCCESS:
215 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
216 break;
217 default:
218 ret = -EIO;
219 IWL_ERR(mvm, "ADD_STA failed\n");
220 break;
221 }
222
223 return ret;
224}
225
a0f6bf2a
AN
226static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
227 struct ieee80211_sta *sta)
228{
229 unsigned long used_hw_queues;
230 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5d42e7b2
EG
231 unsigned int wdg_timeout =
232 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
a0f6bf2a
AN
233 u32 ac;
234
235 lockdep_assert_held(&mvm->mutex);
236
237 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
238
239 /* Find available queues, and allocate them to the ACs */
240 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
241 u8 queue = find_first_zero_bit(&used_hw_queues,
242 mvm->first_agg_queue);
243
244 if (queue >= mvm->first_agg_queue) {
245 IWL_ERR(mvm, "Failed to allocate STA queue\n");
246 return -EBUSY;
247 }
248
249 __set_bit(queue, &used_hw_queues);
250 mvmsta->hw_queue[ac] = queue;
251 }
252
253 /* Found a place for all queues - enable them */
254 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
255 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
4ecafae9 256 mvmsta->hw_queue[ac],
5c1156ef
LK
257 iwl_mvm_ac_to_tx_fifo[ac], 0,
258 wdg_timeout);
a0f6bf2a
AN
259 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
260 }
261
262 return 0;
263}
264
265static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
266 struct ieee80211_sta *sta)
267{
268 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
269 unsigned long sta_msk;
270 int i;
271
272 lockdep_assert_held(&mvm->mutex);
273
274 /* disable the TDLS STA-specific queues */
275 sta_msk = mvmsta->tfd_queue_msk;
a4ca3ed4 276 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
06ecdba3 277 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
a0f6bf2a
AN
278}
279
24afba76
LK
280static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
281 struct ieee80211_sta *sta, u8 ac, int tid,
282 struct ieee80211_hdr *hdr)
283{
284 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
285 struct iwl_trans_txq_scd_cfg cfg = {
286 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
287 .sta_id = mvmsta->sta_id,
288 .tid = tid,
289 .frame_limit = IWL_FRAME_LIMIT,
290 };
291 unsigned int wdg_timeout =
292 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
293 u8 mac_queue = mvmsta->vif->hw_queue[ac];
294 int queue = -1;
295 int ssn;
296
297 lockdep_assert_held(&mvm->mutex);
298
299 spin_lock(&mvm->queue_info_lock);
300
301 /*
302 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
303 * exists
304 */
305 if (!ieee80211_is_data_qos(hdr->frame_control) ||
306 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
307 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_MGMT_QUEUE,
308 IWL_MVM_DQA_MAX_MGMT_QUEUE);
309 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
310 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
311 queue);
312
313 /* If no such queue is found, we'll use a DATA queue instead */
314 }
315
316 if (queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
317 queue = mvmsta->reserved_queue;
318 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
319 }
320
321 if (queue < 0)
322 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_DATA_QUEUE,
323 IWL_MVM_DQA_MAX_DATA_QUEUE);
324 if (queue >= 0)
325 mvm->queue_info[queue].setup_reserved = false;
326
327 spin_unlock(&mvm->queue_info_lock);
328
329 /* TODO: support shared queues for same RA */
330 if (queue < 0)
331 return -ENOSPC;
332
333 /*
334 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
335 * but for configuring the SCD to send A-MPDUs we need to mark the queue
336 * as aggregatable.
337 * Mark all DATA queues as allowing to be aggregated at some point
338 */
339 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE);
340
341 IWL_DEBUG_TX_QUEUES(mvm, "Allocating queue #%d to sta %d on tid %d\n",
342 queue, mvmsta->sta_id, tid);
343
344 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
345 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
346 wdg_timeout);
347
348 spin_lock_bh(&mvmsta->lock);
349 mvmsta->tid_data[tid].txq_id = queue;
350 mvmsta->tfd_queue_msk |= BIT(queue);
351
352 if (mvmsta->reserved_queue == queue)
353 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
354 spin_unlock_bh(&mvmsta->lock);
355
356 return iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
357}
358
359static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
360{
361 if (tid == IWL_MAX_TID_COUNT)
362 return IEEE80211_AC_VO; /* MGMT */
363
364 return tid_to_mac80211_ac[tid];
365}
366
367static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
368 struct ieee80211_sta *sta, int tid)
369{
370 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
371 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
372 struct sk_buff *skb;
373 struct ieee80211_hdr *hdr;
374 struct sk_buff_head deferred_tx;
375 u8 mac_queue;
376 bool no_queue = false; /* Marks if there is a problem with the queue */
377 u8 ac;
378
379 lockdep_assert_held(&mvm->mutex);
380
381 skb = skb_peek(&tid_data->deferred_tx_frames);
382 if (!skb)
383 return;
384 hdr = (void *)skb->data;
385
386 ac = iwl_mvm_tid_to_ac_queue(tid);
387 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
388
389 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
390 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
391 IWL_ERR(mvm,
392 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
393 mvmsta->sta_id, tid);
394
395 /*
396 * Mark queue as problematic so later the deferred traffic is
397 * freed, as we can do nothing with it
398 */
399 no_queue = true;
400 }
401
402 __skb_queue_head_init(&deferred_tx);
403
404 spin_lock(&mvmsta->lock);
405 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
406 spin_unlock(&mvmsta->lock);
407
408 /* Disable bottom-halves when entering TX path */
409 local_bh_disable();
410 while ((skb = __skb_dequeue(&deferred_tx)))
411 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
412 ieee80211_free_txskb(mvm->hw, skb);
413 local_bh_enable();
414
415 /* Wake queue */
416 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
417}
418
419void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
420{
421 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
422 add_stream_wk);
423 struct ieee80211_sta *sta;
424 struct iwl_mvm_sta *mvmsta;
425 unsigned long deferred_tid_traffic;
426 int sta_id, tid;
427
428 mutex_lock(&mvm->mutex);
429
430 /* Go over all stations with deferred traffic */
431 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
432 IWL_MVM_STATION_COUNT) {
433 clear_bit(sta_id, mvm->sta_deferred_frames);
434 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
435 lockdep_is_held(&mvm->mutex));
436 if (IS_ERR_OR_NULL(sta))
437 continue;
438
439 mvmsta = iwl_mvm_sta_from_mac80211(sta);
440 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
441
442 for_each_set_bit(tid, &deferred_tid_traffic,
443 IWL_MAX_TID_COUNT + 1)
444 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
445 }
446
447 mutex_unlock(&mvm->mutex);
448}
449
450static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
451 struct ieee80211_sta *sta)
452{
453 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
454 int queue;
455
456 spin_lock_bh(&mvm->queue_info_lock);
457
458 /* Make sure we have free resources for this STA */
459 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_DATA_QUEUE,
460 IWL_MVM_DQA_MAX_DATA_QUEUE);
461 if (queue < 0) {
462 spin_unlock_bh(&mvm->queue_info_lock);
463 IWL_ERR(mvm, "No available queues for new station\n");
464 return -ENOSPC;
465 }
466 mvm->queue_info[queue].setup_reserved = true;
467
468 spin_unlock_bh(&mvm->queue_info_lock);
469
470 mvmsta->reserved_queue = queue;
471
472 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
473 queue, mvmsta->sta_id);
474
475 return 0;
476}
477
8ca151b5
JB
478int iwl_mvm_add_sta(struct iwl_mvm *mvm,
479 struct ieee80211_vif *vif,
480 struct ieee80211_sta *sta)
481{
482 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9d8ce6af 483 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
a571f5f6 484 struct iwl_mvm_rxq_dup_data *dup_data;
8ca151b5
JB
485 int i, ret, sta_id;
486
487 lockdep_assert_held(&mvm->mutex);
488
489 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
b92e661b
EP
490 sta_id = iwl_mvm_find_free_sta_id(mvm,
491 ieee80211_vif_type_p2p(vif));
8ca151b5
JB
492 else
493 sta_id = mvm_sta->sta_id;
494
36f4631c 495 if (sta_id == IWL_MVM_STATION_COUNT)
8ca151b5
JB
496 return -ENOSPC;
497
498 spin_lock_init(&mvm_sta->lock);
499
500 mvm_sta->sta_id = sta_id;
501 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
502 mvmvif->color);
503 mvm_sta->vif = vif;
504 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
9ee718aa
EL
505 mvm_sta->tx_protection = 0;
506 mvm_sta->tt_tx_protection = false;
8ca151b5
JB
507
508 /* HW restart, don't assume the memory has been zeroed */
e3d4bc8c 509 atomic_set(&mvm->pending_frames[sta_id], 0);
69191afe 510 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
8ca151b5 511 mvm_sta->tfd_queue_msk = 0;
a0f6bf2a
AN
512
513 /* allocate new queues for a TDLS station */
514 if (sta->tdls) {
515 ret = iwl_mvm_tdls_sta_init(mvm, sta);
516 if (ret)
517 return ret;
24afba76 518 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
a0f6bf2a
AN
519 for (i = 0; i < IEEE80211_NUM_ACS; i++)
520 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
521 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
522 }
8ca151b5 523
6d9d32b8 524 /* for HW restart - reset everything but the sequence number */
24afba76 525 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
6d9d32b8
JB
526 u16 seq = mvm_sta->tid_data[i].seq_number;
527 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
528 mvm_sta->tid_data[i].seq_number = seq;
24afba76
LK
529
530 if (!iwl_mvm_is_dqa_supported(mvm))
531 continue;
532
533 /*
534 * Mark all queues for this STA as unallocated and defer TX
535 * frames until the queue is allocated
536 */
537 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
538 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
6d9d32b8 539 }
24afba76 540 mvm_sta->deferred_traffic_tid_map = 0;
efed6640 541 mvm_sta->agg_tids = 0;
8ca151b5 542
a571f5f6
SS
543 if (iwl_mvm_has_new_rx_api(mvm) &&
544 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
545 dup_data = kcalloc(mvm->trans->num_rx_queues,
546 sizeof(*dup_data),
547 GFP_KERNEL);
548 if (!dup_data)
549 return -ENOMEM;
550 mvm_sta->dup_data = dup_data;
551 }
552
24afba76
LK
553 if (iwl_mvm_is_dqa_supported(mvm)) {
554 ret = iwl_mvm_reserve_sta_stream(mvm, sta);
555 if (ret)
556 goto err;
557 }
558
559 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
8ca151b5 560 if (ret)
a0f6bf2a 561 goto err;
8ca151b5 562
9e848010
JB
563 if (vif->type == NL80211_IFTYPE_STATION) {
564 if (!sta->tdls) {
565 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
566 mvmvif->ap_sta_id = sta_id;
567 } else {
568 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
569 }
570 }
8ca151b5
JB
571
572 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
573
574 return 0;
a0f6bf2a
AN
575
576err:
577 iwl_mvm_tdls_sta_deinit(mvm, sta);
578 return ret;
8ca151b5
JB
579}
580
7a453973
JB
581int iwl_mvm_update_sta(struct iwl_mvm *mvm,
582 struct ieee80211_vif *vif,
583 struct ieee80211_sta *sta)
584{
24afba76 585 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
7a453973
JB
586}
587
8ca151b5
JB
588int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
589 bool drain)
590{
f9dc0004 591 struct iwl_mvm_add_sta_cmd cmd = {};
8ca151b5
JB
592 int ret;
593 u32 status;
594
595 lockdep_assert_held(&mvm->mutex);
596
597 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
598 cmd.sta_id = mvmsta->sta_id;
599 cmd.add_modify = STA_MODE_MODIFY;
600 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
601 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
602
603 status = ADD_STA_SUCCESS;
854c5705
SS
604 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
605 iwl_mvm_add_sta_cmd_size(mvm),
f9dc0004 606 &cmd, &status);
8ca151b5
JB
607 if (ret)
608 return ret;
609
837c4da9 610 switch (status & IWL_ADD_STA_STATUS_MASK) {
8ca151b5
JB
611 case ADD_STA_SUCCESS:
612 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
613 mvmsta->sta_id);
614 break;
615 default:
616 ret = -EIO;
617 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
618 mvmsta->sta_id);
619 break;
620 }
621
622 return ret;
623}
624
625/*
626 * Remove a station from the FW table. Before sending the command to remove
627 * the station validate that the station is indeed known to the driver (sanity
628 * only).
629 */
630static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
631{
632 struct ieee80211_sta *sta;
633 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
634 .sta_id = sta_id,
635 };
636 int ret;
637
638 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
639 lockdep_is_held(&mvm->mutex));
640
641 /* Note: internal stations are marked as error values */
642 if (!sta) {
643 IWL_ERR(mvm, "Invalid station id\n");
644 return -EINVAL;
645 }
646
a1022927 647 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
8ca151b5
JB
648 sizeof(rm_sta_cmd), &rm_sta_cmd);
649 if (ret) {
650 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
651 return ret;
652 }
653
654 return 0;
655}
656
657void iwl_mvm_sta_drained_wk(struct work_struct *wk)
658{
659 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
660 u8 sta_id;
661
662 /*
663 * The mutex is needed because of the SYNC cmd, but not only: if the
664 * work would run concurrently with iwl_mvm_rm_sta, it would run before
665 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
666 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
667 * that later.
668 */
669 mutex_lock(&mvm->mutex);
670
671 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
672 int ret;
673 struct ieee80211_sta *sta =
674 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
675 lockdep_is_held(&mvm->mutex));
676
1ddbbb0c
JB
677 /*
678 * This station is in use or RCU-removed; the latter happens in
679 * managed mode, where mac80211 removes the station before we
680 * can remove it from firmware (we can only do that after the
681 * MAC is marked unassociated), and possibly while the deauth
682 * frame to disconnect from the AP is still queued. Then, the
683 * station pointer is -ENOENT when the last skb is reclaimed.
684 */
685 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
8ca151b5
JB
686 continue;
687
688 if (PTR_ERR(sta) == -EINVAL) {
689 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
690 sta_id);
691 continue;
692 }
693
694 if (!sta) {
695 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
696 sta_id);
697 continue;
698 }
699
700 WARN_ON(PTR_ERR(sta) != -EBUSY);
701 /* This station was removed and we waited until it got drained,
702 * we can now proceed and remove it.
703 */
704 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
705 if (ret) {
706 IWL_ERR(mvm,
707 "Couldn't remove sta %d after it was drained\n",
708 sta_id);
709 continue;
710 }
c531c771 711 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
8ca151b5 712 clear_bit(sta_id, mvm->sta_drained);
a0f6bf2a
AN
713
714 if (mvm->tfd_drained[sta_id]) {
715 unsigned long i, msk = mvm->tfd_drained[sta_id];
716
a4ca3ed4 717 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
06ecdba3
AN
718 iwl_mvm_disable_txq(mvm, i, i,
719 IWL_MAX_TID_COUNT, 0);
a0f6bf2a
AN
720
721 mvm->tfd_drained[sta_id] = 0;
722 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
723 sta_id, msk);
724 }
8ca151b5
JB
725 }
726
727 mutex_unlock(&mvm->mutex);
728}
729
24afba76
LK
730static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
731 struct ieee80211_vif *vif,
732 struct iwl_mvm_sta *mvm_sta)
733{
734 int ac;
735 int i;
736
737 lockdep_assert_held(&mvm->mutex);
738
739 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
740 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
741 continue;
742
743 ac = iwl_mvm_tid_to_ac_queue(i);
744 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
745 vif->hw_queue[ac], i, 0);
746 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
747 }
748}
749
8ca151b5
JB
750int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
751 struct ieee80211_vif *vif,
752 struct ieee80211_sta *sta)
753{
754 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9d8ce6af 755 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
756 int ret;
757
758 lockdep_assert_held(&mvm->mutex);
759
a571f5f6
SS
760 if (iwl_mvm_has_new_rx_api(mvm))
761 kfree(mvm_sta->dup_data);
762
8ca151b5
JB
763 if (vif->type == NL80211_IFTYPE_STATION &&
764 mvmvif->ap_sta_id == mvm_sta->sta_id) {
fe92e32a
EG
765 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
766 if (ret)
767 return ret;
80d85655 768 /* flush its queues here since we are freeing mvm_sta */
5888a40c 769 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
fe92e32a
EG
770 if (ret)
771 return ret;
772 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
773 mvm_sta->tfd_queue_msk);
774 if (ret)
775 return ret;
776 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
80d85655 777
24afba76
LK
778 /* If DQA is supported - the queues can be disabled now */
779 if (iwl_mvm_is_dqa_supported(mvm))
780 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
781
8ca151b5
JB
782 /* if we are associated - we can't remove the AP STA now */
783 if (vif->bss_conf.assoc)
784 return ret;
785
786 /* unassoc - go ahead - remove the AP STA now */
787 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
37577fe2
EP
788
789 /* clear d0i3_ap_sta_id if no longer relevant */
790 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
791 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
8ca151b5
JB
792 }
793
1d3c3f63
AN
794 /*
795 * This shouldn't happen - the TDLS channel switch should be canceled
796 * before the STA is removed.
797 */
798 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
799 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
800 cancel_delayed_work(&mvm->tdls_cs.dwork);
801 }
802
e3d4bc8c
EG
803 /*
804 * Make sure that the tx response code sees the station as -EBUSY and
805 * calls the drain worker.
806 */
807 spin_lock_bh(&mvm_sta->lock);
8ca151b5
JB
808 /*
809 * There are frames pending on the AC queues for this station.
810 * We need to wait until all the frames are drained...
811 */
e3d4bc8c 812 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
8ca151b5
JB
813 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
814 ERR_PTR(-EBUSY));
e3d4bc8c 815 spin_unlock_bh(&mvm_sta->lock);
a0f6bf2a
AN
816
817 /* disable TDLS sta queues on drain complete */
818 if (sta->tdls) {
819 mvm->tfd_drained[mvm_sta->sta_id] =
820 mvm_sta->tfd_queue_msk;
821 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
822 mvm_sta->sta_id);
823 }
824
e3d4bc8c 825 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
8ca151b5 826 } else {
e3d4bc8c 827 spin_unlock_bh(&mvm_sta->lock);
a0f6bf2a
AN
828
829 if (sta->tdls)
830 iwl_mvm_tdls_sta_deinit(mvm, sta);
831
8ca151b5 832 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
c531c771 833 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
8ca151b5
JB
834 }
835
836 return ret;
837}
838
839int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
840 struct ieee80211_vif *vif,
841 u8 sta_id)
842{
843 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
844
845 lockdep_assert_held(&mvm->mutex);
846
c531c771 847 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
8ca151b5
JB
848 return ret;
849}
850
0e39eb03
CRI
851int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
852 struct iwl_mvm_int_sta *sta,
853 u32 qmask, enum nl80211_iftype iftype)
8ca151b5
JB
854{
855 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
b92e661b 856 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
8ca151b5
JB
857 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
858 return -ENOSPC;
859 }
860
861 sta->tfd_queue_msk = qmask;
862
863 /* put a non-NULL value so iterating over the stations won't stop */
864 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
865 return 0;
866}
867
712b24ad
JB
868static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
869 struct iwl_mvm_int_sta *sta)
8ca151b5 870{
c531c771 871 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
8ca151b5
JB
872 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
873 sta->sta_id = IWL_MVM_STATION_COUNT;
874}
875
876static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
877 struct iwl_mvm_int_sta *sta,
878 const u8 *addr,
879 u16 mac_id, u16 color)
880{
f9dc0004 881 struct iwl_mvm_add_sta_cmd cmd;
8ca151b5
JB
882 int ret;
883 u32 status;
884
885 lockdep_assert_held(&mvm->mutex);
886
f9dc0004 887 memset(&cmd, 0, sizeof(cmd));
8ca151b5
JB
888 cmd.sta_id = sta->sta_id;
889 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
890 color));
891
892 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
cf0cda19 893 cmd.tid_disable_tx = cpu_to_le16(0xffff);
8ca151b5
JB
894
895 if (addr)
896 memcpy(cmd.addr, addr, ETH_ALEN);
897
854c5705
SS
898 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
899 iwl_mvm_add_sta_cmd_size(mvm),
f9dc0004 900 &cmd, &status);
8ca151b5
JB
901 if (ret)
902 return ret;
903
837c4da9 904 switch (status & IWL_ADD_STA_STATUS_MASK) {
8ca151b5
JB
905 case ADD_STA_SUCCESS:
906 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
907 return 0;
908 default:
909 ret = -EIO;
910 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
911 status);
912 break;
913 }
914 return ret;
915}
916
917int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
918{
4cf677fd
EG
919 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
920 mvm->cfg->base_params->wd_timeout :
921 IWL_WATCHDOG_DISABLED;
8ca151b5
JB
922 int ret;
923
924 lockdep_assert_held(&mvm->mutex);
925
7da91b0e 926 /* Map Aux queue to fifo - needs to happen before adding Aux station */
4ecafae9 927 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
5c1156ef 928 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
7da91b0e
AM
929
930 /* Allocate aux station and assign to it the aux queue */
931 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
b92e661b 932 NL80211_IFTYPE_UNSPECIFIED);
8ca151b5
JB
933 if (ret)
934 return ret;
935
936 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
937 MAC_INDEX_AUX, 0);
938
939 if (ret)
940 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
941 return ret;
942}
943
0e39eb03
CRI
944int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
945{
946 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
947
948 lockdep_assert_held(&mvm->mutex);
949 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
950 mvmvif->id, 0);
951}
952
953int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
954{
955 int ret;
956
957 lockdep_assert_held(&mvm->mutex);
958
959 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
960 if (ret)
961 IWL_WARN(mvm, "Failed sending remove station\n");
962
963 return ret;
964}
965
966void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
967{
968 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
969}
970
712b24ad
JB
971void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
972{
973 lockdep_assert_held(&mvm->mutex);
974
975 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
976}
977
8ca151b5
JB
978/*
979 * Send the add station command for the vif's broadcast station.
980 * Assumes that the station was already allocated.
981 *
982 * @mvm: the mvm component
983 * @vif: the interface to which the broadcast station is added
984 * @bsta: the broadcast station to add.
985 */
013290aa 986int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
8ca151b5
JB
987{
988 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
013290aa 989 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
5023d966 990 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
a4243402 991 const u8 *baddr = _baddr;
8ca151b5
JB
992
993 lockdep_assert_held(&mvm->mutex);
994
5023d966
JB
995 if (vif->type == NL80211_IFTYPE_ADHOC)
996 baddr = vif->bss_conf.bssid;
997
8ca151b5
JB
998 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
999 return -ENOSPC;
1000
1001 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1002 mvmvif->id, mvmvif->color);
1003}
1004
1005/* Send the FW a request to remove the station from it's internal data
1006 * structures, but DO NOT remove the entry from the local data structures. */
013290aa 1007int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
8ca151b5 1008{
013290aa 1009 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5
JB
1010 int ret;
1011
1012 lockdep_assert_held(&mvm->mutex);
1013
013290aa 1014 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
8ca151b5
JB
1015 if (ret)
1016 IWL_WARN(mvm, "Failed sending remove station\n");
1017 return ret;
1018}
1019
013290aa
JB
1020int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1021{
1022 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1023 u32 qmask;
1024
1025 lockdep_assert_held(&mvm->mutex);
1026
d92b732e 1027 qmask = iwl_mvm_mac_get_queues_mask(vif);
013290aa
JB
1028
1029 /*
1030 * The firmware defines the TFD queue mask to only be relevant
1031 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1032 * be included.
1033 */
1034 if (vif->type == NL80211_IFTYPE_AP)
1035 qmask &= ~BIT(vif->cab_queue);
1036
1037 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1038 ieee80211_vif_type_p2p(vif));
1039}
1040
8ca151b5
JB
1041/* Allocate a new station entry for the broadcast station to the given vif,
1042 * and send it to the FW.
1043 * Note that each P2P mac should have its own broadcast station.
1044 *
1045 * @mvm: the mvm component
1046 * @vif: the interface to which the broadcast station is added
1047 * @bsta: the broadcast station to add. */
013290aa 1048int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
8ca151b5
JB
1049{
1050 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
013290aa 1051 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
8ca151b5
JB
1052 int ret;
1053
1054 lockdep_assert_held(&mvm->mutex);
1055
013290aa 1056 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
8ca151b5
JB
1057 if (ret)
1058 return ret;
1059
013290aa 1060 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
8ca151b5
JB
1061
1062 if (ret)
1063 iwl_mvm_dealloc_int_sta(mvm, bsta);
013290aa 1064
8ca151b5
JB
1065 return ret;
1066}
1067
013290aa
JB
1068void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1069{
1070 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1071
1072 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1073}
1074
8ca151b5
JB
1075/*
1076 * Send the FW a request to remove the station from it's internal data
1077 * structures, and in addition remove it from the local data structure.
1078 */
013290aa 1079int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
8ca151b5
JB
1080{
1081 int ret;
1082
1083 lockdep_assert_held(&mvm->mutex);
1084
013290aa
JB
1085 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
1086
1087 iwl_mvm_dealloc_bcast_sta(mvm, vif);
8ca151b5 1088
8ca151b5
JB
1089 return ret;
1090}
1091
113a0447
EG
1092#define IWL_MAX_RX_BA_SESSIONS 16
1093
8ca151b5 1094int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
854c5705 1095 int tid, u16 ssn, bool start, u8 buf_size)
8ca151b5 1096{
9d8ce6af 1097 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
f9dc0004 1098 struct iwl_mvm_add_sta_cmd cmd = {};
8ca151b5
JB
1099 int ret;
1100 u32 status;
1101
1102 lockdep_assert_held(&mvm->mutex);
1103
113a0447
EG
1104 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1105 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1106 return -ENOSPC;
1107 }
1108
8ca151b5
JB
1109 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1110 cmd.sta_id = mvm_sta->sta_id;
1111 cmd.add_modify = STA_MODE_MODIFY;
93a42667
EG
1112 if (start) {
1113 cmd.add_immediate_ba_tid = (u8) tid;
1114 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
854c5705 1115 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
93a42667
EG
1116 } else {
1117 cmd.remove_immediate_ba_tid = (u8) tid;
1118 }
8ca151b5
JB
1119 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1120 STA_MODIFY_REMOVE_BA_TID;
1121
1122 status = ADD_STA_SUCCESS;
854c5705
SS
1123 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1124 iwl_mvm_add_sta_cmd_size(mvm),
f9dc0004 1125 &cmd, &status);
8ca151b5
JB
1126 if (ret)
1127 return ret;
1128
837c4da9 1129 switch (status & IWL_ADD_STA_STATUS_MASK) {
8ca151b5
JB
1130 case ADD_STA_SUCCESS:
1131 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
1132 start ? "start" : "stopp");
1133 break;
1134 case ADD_STA_IMMEDIATE_BA_FAILURE:
1135 IWL_WARN(mvm, "RX BA Session refused by fw\n");
1136 ret = -ENOSPC;
1137 break;
1138 default:
1139 ret = -EIO;
1140 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
1141 start ? "start" : "stopp", status);
1142 break;
1143 }
1144
113a0447
EG
1145 if (!ret) {
1146 if (start)
1147 mvm->rx_ba_sessions++;
1148 else if (mvm->rx_ba_sessions > 0)
1149 /* check that restart flow didn't zero the counter */
1150 mvm->rx_ba_sessions--;
1151 }
1152
8ca151b5
JB
1153 return ret;
1154}
1155
1156static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1157 int tid, u8 queue, bool start)
1158{
9d8ce6af 1159 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
f9dc0004 1160 struct iwl_mvm_add_sta_cmd cmd = {};
8ca151b5
JB
1161 int ret;
1162 u32 status;
1163
1164 lockdep_assert_held(&mvm->mutex);
1165
1166 if (start) {
1167 mvm_sta->tfd_queue_msk |= BIT(queue);
1168 mvm_sta->tid_disable_agg &= ~BIT(tid);
1169 } else {
1170 mvm_sta->tfd_queue_msk &= ~BIT(queue);
1171 mvm_sta->tid_disable_agg |= BIT(tid);
1172 }
1173
1174 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1175 cmd.sta_id = mvm_sta->sta_id;
1176 cmd.add_modify = STA_MODE_MODIFY;
1177 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
1178 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
1179 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
1180
1181 status = ADD_STA_SUCCESS;
854c5705
SS
1182 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1183 iwl_mvm_add_sta_cmd_size(mvm),
f9dc0004 1184 &cmd, &status);
8ca151b5
JB
1185 if (ret)
1186 return ret;
1187
837c4da9 1188 switch (status & IWL_ADD_STA_STATUS_MASK) {
8ca151b5
JB
1189 case ADD_STA_SUCCESS:
1190 break;
1191 default:
1192 ret = -EIO;
1193 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
1194 start ? "start" : "stopp", status);
1195 break;
1196 }
1197
1198 return ret;
1199}
1200
b797e3fb 1201const u8 tid_to_mac80211_ac[] = {
8ca151b5
JB
1202 IEEE80211_AC_BE,
1203 IEEE80211_AC_BK,
1204 IEEE80211_AC_BK,
1205 IEEE80211_AC_BE,
1206 IEEE80211_AC_VI,
1207 IEEE80211_AC_VI,
1208 IEEE80211_AC_VO,
1209 IEEE80211_AC_VO,
1210};
1211
3e56eadf
JB
1212static const u8 tid_to_ucode_ac[] = {
1213 AC_BE,
1214 AC_BK,
1215 AC_BK,
1216 AC_BE,
1217 AC_VI,
1218 AC_VI,
1219 AC_VO,
1220 AC_VO,
1221};
1222
8ca151b5
JB
1223int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1224 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1225{
5b577a90 1226 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
1227 struct iwl_mvm_tid_data *tid_data;
1228 int txq_id;
4ecafae9 1229 int ret;
8ca151b5
JB
1230
1231 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1232 return -EINVAL;
1233
1234 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
1235 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
1236 mvmsta->tid_data[tid].state);
1237 return -ENXIO;
1238 }
1239
1240 lockdep_assert_held(&mvm->mutex);
1241
b2492501
AN
1242 spin_lock_bh(&mvmsta->lock);
1243
1244 /* possible race condition - we entered D0i3 while starting agg */
1245 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
1246 spin_unlock_bh(&mvmsta->lock);
1247 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
1248 return -EIO;
1249 }
1250
4ecafae9
LK
1251 spin_lock_bh(&mvm->queue_info_lock);
1252
1253 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
1254 mvm->last_agg_queue);
1255 if (txq_id < 0) {
1256 ret = txq_id;
1257 spin_unlock_bh(&mvm->queue_info_lock);
1258 IWL_ERR(mvm, "Failed to allocate agg queue\n");
1259 goto release_locks;
1260 }
1261 mvm->queue_info[txq_id].setup_reserved = true;
1262 spin_unlock_bh(&mvm->queue_info_lock);
8ca151b5 1263
8ca151b5 1264 tid_data = &mvmsta->tid_data[tid];
9a886586 1265 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
8ca151b5
JB
1266 tid_data->txq_id = txq_id;
1267 *ssn = tid_data->ssn;
1268
1269 IWL_DEBUG_TX_QUEUES(mvm,
1270 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1271 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1272 tid_data->next_reclaimed);
1273
1274 if (tid_data->ssn == tid_data->next_reclaimed) {
1275 tid_data->state = IWL_AGG_STARTING;
1276 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1277 } else {
1278 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1279 }
1280
4ecafae9
LK
1281 ret = 0;
1282
1283release_locks:
8ca151b5
JB
1284 spin_unlock_bh(&mvmsta->lock);
1285
4ecafae9 1286 return ret;
8ca151b5
JB
1287}
1288
1289int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
bb81bb68
EG
1290 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
1291 bool amsdu)
8ca151b5 1292{
5b577a90 1293 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5 1294 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
5d42e7b2
EG
1295 unsigned int wdg_timeout =
1296 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
eea76c36 1297 int queue, ret;
8ca151b5
JB
1298 u16 ssn;
1299
eea76c36
EG
1300 struct iwl_trans_txq_scd_cfg cfg = {
1301 .sta_id = mvmsta->sta_id,
1302 .tid = tid,
1303 .frame_limit = buf_size,
1304 .aggregate = true,
1305 };
1306
efed6640
ES
1307 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1308 != IWL_MAX_TID_COUNT);
1309
8ca151b5
JB
1310 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1311
1312 spin_lock_bh(&mvmsta->lock);
1313 ssn = tid_data->ssn;
1314 queue = tid_data->txq_id;
1315 tid_data->state = IWL_AGG_ON;
efed6640 1316 mvmsta->agg_tids |= BIT(tid);
8ca151b5 1317 tid_data->ssn = 0xffff;
bb81bb68 1318 tid_data->amsdu_in_ampdu_allowed = amsdu;
8ca151b5
JB
1319 spin_unlock_bh(&mvmsta->lock);
1320
eea76c36 1321 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
8ca151b5 1322
eea76c36
EG
1323 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[tid_to_mac80211_ac[tid]],
1324 ssn, &cfg, wdg_timeout);
fa7878e7 1325
8ca151b5
JB
1326 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1327 if (ret)
1328 return -EIO;
1329
4ecafae9
LK
1330 /* No need to mark as reserved */
1331 spin_lock_bh(&mvm->queue_info_lock);
1332 mvm->queue_info[queue].setup_reserved = false;
1333 spin_unlock_bh(&mvm->queue_info_lock);
1334
8ca151b5
JB
1335 /*
1336 * Even though in theory the peer could have different
1337 * aggregation reorder buffer sizes for different sessions,
1338 * our ucode doesn't allow for that and has a global limit
1339 * for each station. Therefore, use the minimum of all the
1340 * aggregation sessions and our default value.
1341 */
1342 mvmsta->max_agg_bufsize =
1343 min(mvmsta->max_agg_bufsize, buf_size);
1344 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1345
9ee718aa
EL
1346 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1347 sta->addr, tid);
1348
9e680946 1349 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
8ca151b5
JB
1350}
1351
1352int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1353 struct ieee80211_sta *sta, u16 tid)
1354{
5b577a90 1355 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
1356 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1357 u16 txq_id;
1358 int err;
1359
f9aa8dd3
EG
1360
1361 /*
1362 * If mac80211 is cleaning its state, then say that we finished since
1363 * our state has been cleared anyway.
1364 */
1365 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1366 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1367 return 0;
1368 }
1369
8ca151b5
JB
1370 spin_lock_bh(&mvmsta->lock);
1371
1372 txq_id = tid_data->txq_id;
1373
1374 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1375 mvmsta->sta_id, tid, txq_id, tid_data->state);
1376
efed6640
ES
1377 mvmsta->agg_tids &= ~BIT(tid);
1378
4ecafae9
LK
1379 /* No need to mark as reserved anymore */
1380 spin_lock_bh(&mvm->queue_info_lock);
1381 mvm->queue_info[txq_id].setup_reserved = false;
1382 spin_unlock_bh(&mvm->queue_info_lock);
1383
8ca151b5
JB
1384 switch (tid_data->state) {
1385 case IWL_AGG_ON:
9a886586 1386 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
8ca151b5
JB
1387
1388 IWL_DEBUG_TX_QUEUES(mvm,
1389 "ssn = %d, next_recl = %d\n",
1390 tid_data->ssn, tid_data->next_reclaimed);
1391
1392 /* There are still packets for this RA / TID in the HW */
1393 if (tid_data->ssn != tid_data->next_reclaimed) {
1394 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1395 err = 0;
1396 break;
1397 }
1398
1399 tid_data->ssn = 0xffff;
f7f89e7b 1400 tid_data->state = IWL_AGG_OFF;
f7f89e7b
JB
1401 spin_unlock_bh(&mvmsta->lock);
1402
1403 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1404
1405 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1406
4ecafae9
LK
1407 iwl_mvm_disable_txq(mvm, txq_id,
1408 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1409 0);
f7f89e7b 1410 return 0;
8ca151b5
JB
1411 case IWL_AGG_STARTING:
1412 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1413 /*
1414 * The agg session has been stopped before it was set up. This
1415 * can happen when the AddBA timer times out for example.
1416 */
1417
1418 /* No barriers since we are under mutex */
1419 lockdep_assert_held(&mvm->mutex);
8ca151b5
JB
1420
1421 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1422 tid_data->state = IWL_AGG_OFF;
1423 err = 0;
1424 break;
1425 default:
1426 IWL_ERR(mvm,
1427 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1428 mvmsta->sta_id, tid, tid_data->state);
1429 IWL_ERR(mvm,
1430 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1431 err = -EINVAL;
1432 }
1433
1434 spin_unlock_bh(&mvmsta->lock);
1435
1436 return err;
1437}
1438
e3d9e7ce
EG
1439int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1440 struct ieee80211_sta *sta, u16 tid)
1441{
5b577a90 1442 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
e3d9e7ce
EG
1443 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1444 u16 txq_id;
b6658ff8 1445 enum iwl_mvm_agg_state old_state;
e3d9e7ce
EG
1446
1447 /*
1448 * First set the agg state to OFF to avoid calling
1449 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1450 */
1451 spin_lock_bh(&mvmsta->lock);
1452 txq_id = tid_data->txq_id;
1453 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1454 mvmsta->sta_id, tid, txq_id, tid_data->state);
b6658ff8 1455 old_state = tid_data->state;
e3d9e7ce 1456 tid_data->state = IWL_AGG_OFF;
efed6640 1457 mvmsta->agg_tids &= ~BIT(tid);
e3d9e7ce
EG
1458 spin_unlock_bh(&mvmsta->lock);
1459
4ecafae9
LK
1460 /* No need to mark as reserved */
1461 spin_lock_bh(&mvm->queue_info_lock);
1462 mvm->queue_info[txq_id].setup_reserved = false;
1463 spin_unlock_bh(&mvm->queue_info_lock);
1464
b6658ff8 1465 if (old_state >= IWL_AGG_ON) {
fe92e32a 1466 iwl_mvm_drain_sta(mvm, mvmsta, true);
5888a40c 1467 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
b6658ff8 1468 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
fe92e32a
EG
1469 iwl_trans_wait_tx_queue_empty(mvm->trans,
1470 mvmsta->tfd_queue_msk);
1471 iwl_mvm_drain_sta(mvm, mvmsta, false);
b6658ff8 1472
f7f89e7b
JB
1473 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1474
4ecafae9
LK
1475 iwl_mvm_disable_txq(mvm, tid_data->txq_id,
1476 vif->hw_queue[tid_to_mac80211_ac[tid]], tid,
1477 0);
b6658ff8 1478 }
e3d9e7ce 1479
e3d9e7ce
EG
1480 return 0;
1481}
1482
8ca151b5
JB
1483static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1484{
2dc2a15e 1485 int i, max = -1, max_offs = -1;
8ca151b5
JB
1486
1487 lockdep_assert_held(&mvm->mutex);
1488
2dc2a15e
JB
1489 /* Pick the unused key offset with the highest 'deleted'
1490 * counter. Every time a key is deleted, all the counters
1491 * are incremented and the one that was just deleted is
1492 * reset to zero. Thus, the highest counter is the one
1493 * that was deleted longest ago. Pick that one.
1494 */
1495 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1496 if (test_bit(i, mvm->fw_key_table))
1497 continue;
1498 if (mvm->fw_key_deleted[i] > max) {
1499 max = mvm->fw_key_deleted[i];
1500 max_offs = i;
1501 }
1502 }
8ca151b5 1503
2dc2a15e 1504 if (max_offs < 0)
8ca151b5
JB
1505 return STA_KEY_IDX_INVALID;
1506
2dc2a15e 1507 return max_offs;
8ca151b5
JB
1508}
1509
5f7a1847
JB
1510static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
1511 struct ieee80211_vif *vif,
1512 struct ieee80211_sta *sta)
8ca151b5 1513{
5b530e95 1514 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5 1515
5f7a1847
JB
1516 if (sta)
1517 return iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
1518
1519 /*
1520 * The device expects GTKs for station interfaces to be
1521 * installed as GTKs for the AP station. If we have no
1522 * station ID, then use AP's station ID.
1523 */
1524 if (vif->type == NL80211_IFTYPE_STATION &&
9513c5e1
AA
1525 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1526 u8 sta_id = mvmvif->ap_sta_id;
1527
45854360
JB
1528 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
1529 lockdep_is_held(&mvm->mutex));
9513c5e1
AA
1530 /*
1531 * It is possible that the 'sta' parameter is NULL,
1532 * for example when a GTK is removed - the sta_id will then
1533 * be the AP ID, and no station was passed by mac80211.
1534 */
1535 if (IS_ERR_OR_NULL(sta))
5f7a1847 1536 return NULL;
9513c5e1 1537
5f7a1847 1538 return iwl_mvm_sta_from_mac80211(sta);
9513c5e1 1539 }
8ca151b5 1540
5f7a1847 1541 return NULL;
8ca151b5
JB
1542}
1543
1544static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1545 struct iwl_mvm_sta *mvm_sta,
ba3943b0 1546 struct ieee80211_key_conf *keyconf, bool mcast,
d6ee54a9
LC
1547 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
1548 u8 key_offset)
8ca151b5 1549{
5a258aae 1550 struct iwl_mvm_add_sta_key_cmd cmd = {};
f9dc0004 1551 __le16 key_flags;
79920749
JB
1552 int ret;
1553 u32 status;
8ca151b5
JB
1554 u16 keyidx;
1555 int i;
2f6319d1 1556 u8 sta_id = mvm_sta->sta_id;
8ca151b5
JB
1557
1558 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1559 STA_KEY_FLG_KEYID_MSK;
1560 key_flags = cpu_to_le16(keyidx);
1561 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1562
1563 switch (keyconf->cipher) {
1564 case WLAN_CIPHER_SUITE_TKIP:
1565 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
5a258aae 1566 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
8ca151b5 1567 for (i = 0; i < 5; i++)
5a258aae
MS
1568 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1569 memcpy(cmd.key, keyconf->key, keyconf->keylen);
8ca151b5
JB
1570 break;
1571 case WLAN_CIPHER_SUITE_CCMP:
1572 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
5a258aae 1573 memcpy(cmd.key, keyconf->key, keyconf->keylen);
8ca151b5 1574 break;
ba3943b0
JB
1575 case WLAN_CIPHER_SUITE_WEP104:
1576 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
aa0cb08b 1577 /* fall through */
ba3943b0
JB
1578 case WLAN_CIPHER_SUITE_WEP40:
1579 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1580 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1581 break;
8ca151b5 1582 default:
e36e5433
MS
1583 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1584 memcpy(cmd.key, keyconf->key, keyconf->keylen);
8ca151b5
JB
1585 }
1586
ba3943b0 1587 if (mcast)
8ca151b5
JB
1588 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1589
d6ee54a9 1590 cmd.key_offset = key_offset;
5a258aae 1591 cmd.key_flags = key_flags;
8ca151b5
JB
1592 cmd.sta_id = sta_id;
1593
1594 status = ADD_STA_SUCCESS;
a1022927 1595 if (cmd_flags & CMD_ASYNC)
f9dc0004
EG
1596 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1597 sizeof(cmd), &cmd);
a1022927
EG
1598 else
1599 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1600 &cmd, &status);
8ca151b5
JB
1601
1602 switch (status) {
1603 case ADD_STA_SUCCESS:
1604 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1605 break;
1606 default:
1607 ret = -EIO;
1608 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1609 break;
1610 }
1611
1612 return ret;
1613}
1614
1615static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1616 struct ieee80211_key_conf *keyconf,
1617 u8 sta_id, bool remove_key)
1618{
1619 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1620
1621 /* verify the key details match the required command's expectations */
1622 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1623 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1624 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1625 return -EINVAL;
1626
1627 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1628 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1629
1630 if (remove_key) {
1631 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1632 } else {
1633 struct ieee80211_key_seq seq;
1634 const u8 *pn;
1635
1636 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
8ca151b5
JB
1637 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1638 pn = seq.aes_cmac.pn;
1639 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1640 ((u64) pn[4] << 8) |
1641 ((u64) pn[3] << 16) |
1642 ((u64) pn[2] << 24) |
1643 ((u64) pn[1] << 32) |
1644 ((u64) pn[0] << 40));
1645 }
1646
1647 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1648 remove_key ? "removing" : "installing",
1649 igtk_cmd.sta_id);
1650
a1022927 1651 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
8ca151b5
JB
1652 sizeof(igtk_cmd), &igtk_cmd);
1653}
1654
1655
1656static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1657 struct ieee80211_vif *vif,
1658 struct ieee80211_sta *sta)
1659{
5b530e95 1660 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5
JB
1661
1662 if (sta)
1663 return sta->addr;
1664
1665 if (vif->type == NL80211_IFTYPE_STATION &&
1666 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1667 u8 sta_id = mvmvif->ap_sta_id;
1668 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1669 lockdep_is_held(&mvm->mutex));
1670 return sta->addr;
1671 }
1672
1673
1674 return NULL;
1675}
1676
2f6319d1
JB
1677static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1678 struct ieee80211_vif *vif,
1679 struct ieee80211_sta *sta,
ba3943b0 1680 struct ieee80211_key_conf *keyconf,
d6ee54a9 1681 u8 key_offset,
ba3943b0 1682 bool mcast)
2f6319d1
JB
1683{
1684 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1685 int ret;
1686 const u8 *addr;
1687 struct ieee80211_key_seq seq;
1688 u16 p1k[5];
1689
1690 switch (keyconf->cipher) {
1691 case WLAN_CIPHER_SUITE_TKIP:
1692 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1693 /* get phase 1 key from mac80211 */
1694 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1695 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
ba3943b0 1696 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
d6ee54a9 1697 seq.tkip.iv32, p1k, 0, key_offset);
2f6319d1
JB
1698 break;
1699 case WLAN_CIPHER_SUITE_CCMP:
ba3943b0
JB
1700 case WLAN_CIPHER_SUITE_WEP40:
1701 case WLAN_CIPHER_SUITE_WEP104:
1702 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
d6ee54a9 1703 0, NULL, 0, key_offset);
2f6319d1
JB
1704 break;
1705 default:
ba3943b0 1706 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
d6ee54a9 1707 0, NULL, 0, key_offset);
2f6319d1
JB
1708 }
1709
1710 return ret;
1711}
1712
1713static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
ba3943b0
JB
1714 struct ieee80211_key_conf *keyconf,
1715 bool mcast)
2f6319d1
JB
1716{
1717 struct iwl_mvm_add_sta_key_cmd cmd = {};
1718 __le16 key_flags;
1719 int ret;
1720 u32 status;
1721
1722 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1723 STA_KEY_FLG_KEYID_MSK);
1724 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1725 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1726
ba3943b0 1727 if (mcast)
2f6319d1
JB
1728 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1729
1730 cmd.key_flags = key_flags;
1731 cmd.key_offset = keyconf->hw_key_idx;
1732 cmd.sta_id = sta_id;
1733
1734 status = ADD_STA_SUCCESS;
1735 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1736 &cmd, &status);
1737
1738 switch (status) {
1739 case ADD_STA_SUCCESS:
1740 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1741 break;
1742 default:
1743 ret = -EIO;
1744 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1745 break;
1746 }
1747
1748 return ret;
1749}
1750
8ca151b5
JB
1751int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1752 struct ieee80211_vif *vif,
1753 struct ieee80211_sta *sta,
1754 struct ieee80211_key_conf *keyconf,
d6ee54a9 1755 u8 key_offset)
8ca151b5 1756{
ba3943b0 1757 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
5f7a1847 1758 struct iwl_mvm_sta *mvm_sta;
2f6319d1 1759 u8 sta_id;
8ca151b5 1760 int ret;
11828dbc 1761 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
8ca151b5
JB
1762
1763 lockdep_assert_held(&mvm->mutex);
1764
1765 /* Get the station id from the mvm local station table */
5f7a1847
JB
1766 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1767 if (!mvm_sta) {
1768 IWL_ERR(mvm, "Failed to find station\n");
8ca151b5
JB
1769 return -EINVAL;
1770 }
5f7a1847 1771 sta_id = mvm_sta->sta_id;
8ca151b5
JB
1772
1773 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1774 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1775 goto end;
1776 }
1777
1778 /*
1779 * It is possible that the 'sta' parameter is NULL, and thus
1780 * there is a need to retrieve the sta from the local station table.
1781 */
1782 if (!sta) {
1783 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1784 lockdep_is_held(&mvm->mutex));
1785 if (IS_ERR_OR_NULL(sta)) {
1786 IWL_ERR(mvm, "Invalid station id\n");
1787 return -EINVAL;
1788 }
1789 }
1790
2f6319d1 1791 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
8ca151b5
JB
1792 return -EINVAL;
1793
d6ee54a9
LC
1794 /* If the key_offset is not pre-assigned, we need to find a
1795 * new offset to use. In normal cases, the offset is not
1796 * pre-assigned, but during HW_RESTART we want to reuse the
1797 * same indices, so we pass them when this function is called.
1798 *
1799 * In D3 entry, we need to hardcoded the indices (because the
1800 * firmware hardcodes the PTK offset to 0). In this case, we
1801 * need to make sure we don't overwrite the hw_key_idx in the
1802 * keyconf structure, because otherwise we cannot configure
1803 * the original ones back when resuming.
1804 */
1805 if (key_offset == STA_KEY_IDX_INVALID) {
1806 key_offset = iwl_mvm_set_fw_key_idx(mvm);
1807 if (key_offset == STA_KEY_IDX_INVALID)
8ca151b5 1808 return -ENOSPC;
d6ee54a9 1809 keyconf->hw_key_idx = key_offset;
8ca151b5
JB
1810 }
1811
d6ee54a9 1812 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
9c3deeb5 1813 if (ret)
ba3943b0 1814 goto end;
ba3943b0
JB
1815
1816 /*
1817 * For WEP, the same key is used for multicast and unicast. Upload it
1818 * again, using the same key offset, and now pointing the other one
1819 * to the same key slot (offset).
1820 * If this fails, remove the original as well.
1821 */
1822 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1823 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
d6ee54a9
LC
1824 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
1825 key_offset, !mcast);
ba3943b0 1826 if (ret) {
ba3943b0 1827 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
9c3deeb5 1828 goto end;
ba3943b0
JB
1829 }
1830 }
8ca151b5 1831
9c3deeb5
LC
1832 __set_bit(key_offset, mvm->fw_key_table);
1833
8ca151b5
JB
1834end:
1835 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1836 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
11828dbc 1837 sta ? sta->addr : zero_addr, ret);
8ca151b5
JB
1838 return ret;
1839}
1840
1841int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1842 struct ieee80211_vif *vif,
1843 struct ieee80211_sta *sta,
1844 struct ieee80211_key_conf *keyconf)
1845{
ba3943b0 1846 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
5f7a1847
JB
1847 struct iwl_mvm_sta *mvm_sta;
1848 u8 sta_id = IWL_MVM_STATION_COUNT;
2dc2a15e 1849 int ret, i;
8ca151b5
JB
1850
1851 lockdep_assert_held(&mvm->mutex);
1852
5f7a1847
JB
1853 /* Get the station from the mvm local station table */
1854 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
8ca151b5
JB
1855
1856 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1857 keyconf->keyidx, sta_id);
1858
1859 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1860 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1861
2f6319d1 1862 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
8ca151b5
JB
1863 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1864 keyconf->hw_key_idx);
1865 return -ENOENT;
1866 }
1867
2dc2a15e
JB
1868 /* track which key was deleted last */
1869 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1870 if (mvm->fw_key_deleted[i] < U8_MAX)
1871 mvm->fw_key_deleted[i]++;
1872 }
1873 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
1874
5f7a1847 1875 if (!mvm_sta) {
8ca151b5
JB
1876 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1877 return 0;
1878 }
1879
5f7a1847
JB
1880 sta_id = mvm_sta->sta_id;
1881
ba3943b0
JB
1882 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
1883 if (ret)
1884 return ret;
1885
1886 /* delete WEP key twice to get rid of (now useless) offset */
1887 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1888 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
1889 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
1890
1891 return ret;
8ca151b5
JB
1892}
1893
1894void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1895 struct ieee80211_vif *vif,
1896 struct ieee80211_key_conf *keyconf,
1897 struct ieee80211_sta *sta, u32 iv32,
1898 u16 *phase1key)
1899{
c3eb536a 1900 struct iwl_mvm_sta *mvm_sta;
ba3943b0 1901 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
8ca151b5 1902
c3eb536a
BL
1903 rcu_read_lock();
1904
5f7a1847
JB
1905 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
1906 if (WARN_ON_ONCE(!mvm_sta))
45854360 1907 goto unlock;
ba3943b0 1908 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
d6ee54a9 1909 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
45854360
JB
1910
1911 unlock:
c3eb536a 1912 rcu_read_unlock();
8ca151b5
JB
1913}
1914
9cc40712
JB
1915void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1916 struct ieee80211_sta *sta)
8ca151b5 1917{
5b577a90 1918 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
f9dc0004 1919 struct iwl_mvm_add_sta_cmd cmd = {
8ca151b5 1920 .add_modify = STA_MODE_MODIFY,
9cc40712 1921 .sta_id = mvmsta->sta_id,
5af01772 1922 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
9cc40712 1923 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
8ca151b5
JB
1924 };
1925 int ret;
1926
854c5705
SS
1927 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1928 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
8ca151b5
JB
1929 if (ret)
1930 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1931}
1932
9cc40712
JB
1933void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1934 struct ieee80211_sta *sta,
8ca151b5 1935 enum ieee80211_frame_release_type reason,
3e56eadf
JB
1936 u16 cnt, u16 tids, bool more_data,
1937 bool agg)
8ca151b5 1938{
5b577a90 1939 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
f9dc0004 1940 struct iwl_mvm_add_sta_cmd cmd = {
8ca151b5 1941 .add_modify = STA_MODE_MODIFY,
9cc40712 1942 .sta_id = mvmsta->sta_id,
8ca151b5
JB
1943 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1944 .sleep_tx_count = cpu_to_le16(cnt),
9cc40712 1945 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
8ca151b5 1946 };
3e56eadf
JB
1947 int tid, ret;
1948 unsigned long _tids = tids;
1949
1950 /* convert TIDs to ACs - we don't support TSPEC so that's OK
1951 * Note that this field is reserved and unused by firmware not
1952 * supporting GO uAPSD, so it's safe to always do this.
1953 */
1954 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1955 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1956
1957 /* If we're releasing frames from aggregation queues then check if the
1958 * all queues combined that we're releasing frames from have
1959 * - more frames than the service period, in which case more_data
1960 * needs to be set
1961 * - fewer than 'cnt' frames, in which case we need to adjust the
1962 * firmware command (but do that unconditionally)
1963 */
1964 if (agg) {
1965 int remaining = cnt;
36be0eb6 1966 int sleep_tx_count;
3e56eadf
JB
1967
1968 spin_lock_bh(&mvmsta->lock);
1969 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1970 struct iwl_mvm_tid_data *tid_data;
1971 u16 n_queued;
1972
1973 tid_data = &mvmsta->tid_data[tid];
1974 if (WARN(tid_data->state != IWL_AGG_ON &&
1975 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1976 "TID %d state is %d\n",
1977 tid, tid_data->state)) {
1978 spin_unlock_bh(&mvmsta->lock);
1979 ieee80211_sta_eosp(sta);
1980 return;
1981 }
1982
1983 n_queued = iwl_mvm_tid_queued(tid_data);
1984 if (n_queued > remaining) {
1985 more_data = true;
1986 remaining = 0;
1987 break;
1988 }
1989 remaining -= n_queued;
1990 }
36be0eb6
EG
1991 sleep_tx_count = cnt - remaining;
1992 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1993 mvmsta->sleep_tx_count = sleep_tx_count;
3e56eadf
JB
1994 spin_unlock_bh(&mvmsta->lock);
1995
36be0eb6 1996 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3e56eadf
JB
1997 if (WARN_ON(cnt - remaining == 0)) {
1998 ieee80211_sta_eosp(sta);
1999 return;
2000 }
2001 }
2002
2003 /* Note: this is ignored by firmware not supporting GO uAPSD */
2004 if (more_data)
2005 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
2006
2007 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
2008 mvmsta->next_status_eosp = true;
2009 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
2010 } else {
2011 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
2012 }
8ca151b5 2013
156f92f2
EG
2014 /* block the Tx queues until the FW updated the sleep Tx count */
2015 iwl_trans_block_txq_ptrs(mvm->trans, true);
2016
2017 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
2018 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
854c5705 2019 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
8ca151b5
JB
2020 if (ret)
2021 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2022}
3e56eadf 2023
0416841d
JB
2024void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
2025 struct iwl_rx_cmd_buffer *rxb)
3e56eadf
JB
2026{
2027 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2028 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
2029 struct ieee80211_sta *sta;
2030 u32 sta_id = le32_to_cpu(notif->sta_id);
2031
2032 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
0416841d 2033 return;
3e56eadf
JB
2034
2035 rcu_read_lock();
2036 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2037 if (!IS_ERR_OR_NULL(sta))
2038 ieee80211_sta_eosp(sta);
2039 rcu_read_unlock();
3e56eadf 2040}
09b0ce1a
AO
2041
2042void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
2043 struct iwl_mvm_sta *mvmsta, bool disable)
2044{
2045 struct iwl_mvm_add_sta_cmd cmd = {
2046 .add_modify = STA_MODE_MODIFY,
2047 .sta_id = mvmsta->sta_id,
2048 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
2049 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
2050 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2051 };
2052 int ret;
2053
854c5705
SS
2054 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2055 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
09b0ce1a
AO
2056 if (ret)
2057 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2058}
003e5236
AO
2059
2060void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
2061 struct ieee80211_sta *sta,
2062 bool disable)
2063{
2064 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2065
2066 spin_lock_bh(&mvm_sta->lock);
2067
2068 if (mvm_sta->disable_tx == disable) {
2069 spin_unlock_bh(&mvm_sta->lock);
2070 return;
2071 }
2072
2073 mvm_sta->disable_tx = disable;
2074
2075 /*
0d365ae5
SS
2076 * Tell mac80211 to start/stop queuing tx for this station,
2077 * but don't stop queuing if there are still pending frames
003e5236
AO
2078 * for this station.
2079 */
2080 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
2081 ieee80211_sta_block_awake(mvm->hw, sta, disable);
2082
2083 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
2084
2085 spin_unlock_bh(&mvm_sta->lock);
2086}
2087
2088void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
2089 struct iwl_mvm_vif *mvmvif,
2090 bool disable)
2091{
2092 struct ieee80211_sta *sta;
2093 struct iwl_mvm_sta *mvm_sta;
2094 int i;
2095
2096 lockdep_assert_held(&mvm->mutex);
2097
2098 /* Block/unblock all the stations of the given mvmvif */
2099 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
2100 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
2101 lockdep_is_held(&mvm->mutex));
2102 if (IS_ERR_OR_NULL(sta))
2103 continue;
2104
2105 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2106 if (mvm_sta->mac_id_n_color !=
2107 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
2108 continue;
2109
2110 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
2111 }
2112}
dc88b4ba
LC
2113
2114void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2115{
2116 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2117 struct iwl_mvm_sta *mvmsta;
2118
2119 rcu_read_lock();
2120
2121 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
2122
2123 if (!WARN_ON(!mvmsta))
2124 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2125
2126 rcu_read_unlock();
2127}