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