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