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