Loading...
1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2/*
3 * Copyright (C) 2012-2015, 2018-2023 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
6 */
7#include <net/mac80211.h>
8
9#include "mvm.h"
10#include "sta.h"
11#include "rs.h"
12
13/*
14 * New version of ADD_STA_sta command added new fields at the end of the
15 * structure, so sending the size of the relevant API's structure is enough to
16 * support both API versions.
17 */
18static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
19{
20 if (iwl_mvm_has_new_rx_api(mvm) ||
21 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
22 return sizeof(struct iwl_mvm_add_sta_cmd);
23 else
24 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
25}
26
27int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm, enum nl80211_iftype iftype)
28{
29 int sta_id;
30 u32 reserved_ids = 0;
31
32 BUILD_BUG_ON(IWL_MVM_STATION_COUNT_MAX > 32);
33 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
34
35 lockdep_assert_held(&mvm->mutex);
36
37 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
38 if (iftype != NL80211_IFTYPE_STATION)
39 reserved_ids = BIT(0);
40
41 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
42 for (sta_id = 0; sta_id < mvm->fw->ucode_capa.num_stations; sta_id++) {
43 if (BIT(sta_id) & reserved_ids)
44 continue;
45
46 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
47 lockdep_is_held(&mvm->mutex)))
48 return sta_id;
49 }
50 return IWL_MVM_INVALID_STA;
51}
52
53/* Calculate the ampdu density and max size */
54u32 iwl_mvm_get_sta_ampdu_dens(struct ieee80211_link_sta *link_sta,
55 struct ieee80211_bss_conf *link_conf,
56 u32 *_agg_size)
57{
58 u32 agg_size = 0, mpdu_dens = 0;
59
60 if (WARN_ON(!link_sta))
61 return 0;
62
63 /* Note that we always use only legacy & highest supported PPDUs, so
64 * of Draft P802.11be D.30 Table 10-12a--Fields used for calculating
65 * the maximum A-MPDU size of various PPDU types in different bands,
66 * we only need to worry about the highest supported PPDU type here.
67 */
68
69 if (link_sta->ht_cap.ht_supported) {
70 agg_size = link_sta->ht_cap.ampdu_factor;
71 mpdu_dens = link_sta->ht_cap.ampdu_density;
72 }
73
74 if (link_conf->chandef.chan->band == NL80211_BAND_6GHZ) {
75 /* overwrite HT values on 6 GHz */
76 mpdu_dens = le16_get_bits(link_sta->he_6ghz_capa.capa,
77 IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
78 agg_size = le16_get_bits(link_sta->he_6ghz_capa.capa,
79 IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
80 } else if (link_sta->vht_cap.vht_supported) {
81 /* if VHT supported overwrite HT value */
82 agg_size = u32_get_bits(link_sta->vht_cap.cap,
83 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
84 }
85
86 /* D6.0 10.12.2 A-MPDU length limit rules
87 * A STA indicates the maximum length of the A-MPDU preEOF padding
88 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
89 * Exponent field in its HT Capabilities, VHT Capabilities,
90 * and HE 6 GHz Band Capabilities elements (if present) and the
91 * Maximum AMPDU Length Exponent Extension field in its HE
92 * Capabilities element
93 */
94 if (link_sta->he_cap.has_he)
95 agg_size +=
96 u8_get_bits(link_sta->he_cap.he_cap_elem.mac_cap_info[3],
97 IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
98
99 if (link_sta->eht_cap.has_eht)
100 agg_size += u8_get_bits(link_sta->eht_cap.eht_cap_elem.mac_cap_info[1],
101 IEEE80211_EHT_MAC_CAP1_MAX_AMPDU_LEN_MASK);
102
103 /* Limit to max A-MPDU supported by FW */
104 agg_size = min_t(u32, agg_size,
105 STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT);
106
107 *_agg_size = agg_size;
108 return mpdu_dens;
109}
110
111u8 iwl_mvm_get_sta_uapsd_acs(struct ieee80211_sta *sta)
112{
113 u8 uapsd_acs = 0;
114
115 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
116 uapsd_acs |= BIT(AC_BK);
117 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
118 uapsd_acs |= BIT(AC_BE);
119 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
120 uapsd_acs |= BIT(AC_VI);
121 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
122 uapsd_acs |= BIT(AC_VO);
123
124 return uapsd_acs | uapsd_acs << 4;
125}
126
127/* send station add/update command to firmware */
128int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
129 bool update, unsigned int flags)
130{
131 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
132 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
133 .sta_id = mvm_sta->deflink.sta_id,
134 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
135 .add_modify = update ? 1 : 0,
136 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
137 STA_FLG_MIMO_EN_MSK |
138 STA_FLG_RTS_MIMO_PROT),
139 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
140 };
141 int ret;
142 u32 status;
143 u32 agg_size = 0, mpdu_dens = 0;
144
145 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
146 add_sta_cmd.station_type = mvm_sta->sta_type;
147
148 if (!update || (flags & STA_MODIFY_QUEUES)) {
149 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
150
151 if (!iwl_mvm_has_new_tx_api(mvm)) {
152 add_sta_cmd.tfd_queue_msk =
153 cpu_to_le32(mvm_sta->tfd_queue_msk);
154
155 if (flags & STA_MODIFY_QUEUES)
156 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
157 } else {
158 WARN_ON(flags & STA_MODIFY_QUEUES);
159 }
160 }
161
162 switch (sta->deflink.bandwidth) {
163 case IEEE80211_STA_RX_BW_320:
164 case IEEE80211_STA_RX_BW_160:
165 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
166 fallthrough;
167 case IEEE80211_STA_RX_BW_80:
168 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
169 fallthrough;
170 case IEEE80211_STA_RX_BW_40:
171 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
172 fallthrough;
173 case IEEE80211_STA_RX_BW_20:
174 if (sta->deflink.ht_cap.ht_supported)
175 add_sta_cmd.station_flags |=
176 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
177 break;
178 }
179
180 switch (sta->deflink.rx_nss) {
181 case 1:
182 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
183 break;
184 case 2:
185 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
186 break;
187 case 3 ... 8:
188 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
189 break;
190 }
191
192 switch (sta->deflink.smps_mode) {
193 case IEEE80211_SMPS_AUTOMATIC:
194 case IEEE80211_SMPS_NUM_MODES:
195 WARN_ON(1);
196 break;
197 case IEEE80211_SMPS_STATIC:
198 /* override NSS */
199 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
200 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
201 break;
202 case IEEE80211_SMPS_DYNAMIC:
203 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
204 break;
205 case IEEE80211_SMPS_OFF:
206 /* nothing */
207 break;
208 }
209
210 if (sta->deflink.ht_cap.ht_supported ||
211 mvm_sta->vif->bss_conf.chandef.chan->band == NL80211_BAND_6GHZ)
212 add_sta_cmd.station_flags_msk |=
213 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
214 STA_FLG_AGG_MPDU_DENS_MSK);
215
216 mpdu_dens = iwl_mvm_get_sta_ampdu_dens(&sta->deflink,
217 &mvm_sta->vif->bss_conf,
218 &agg_size);
219 add_sta_cmd.station_flags |=
220 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
221 add_sta_cmd.station_flags |=
222 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
223
224 if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
225 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
226
227 if (sta->wme) {
228 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
229 add_sta_cmd.uapsd_acs = iwl_mvm_get_sta_uapsd_acs(sta);
230 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
231 }
232
233 status = ADD_STA_SUCCESS;
234 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
235 iwl_mvm_add_sta_cmd_size(mvm),
236 &add_sta_cmd, &status);
237 if (ret)
238 return ret;
239
240 switch (status & IWL_ADD_STA_STATUS_MASK) {
241 case ADD_STA_SUCCESS:
242 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
243 break;
244 default:
245 ret = -EIO;
246 IWL_ERR(mvm, "ADD_STA failed\n");
247 break;
248 }
249
250 return ret;
251}
252
253static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
254{
255 struct iwl_mvm_baid_data *data =
256 from_timer(data, t, session_timer);
257 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
258 struct iwl_mvm_baid_data *ba_data;
259 struct ieee80211_sta *sta;
260 struct iwl_mvm_sta *mvm_sta;
261 unsigned long timeout;
262 unsigned int sta_id;
263
264 rcu_read_lock();
265
266 ba_data = rcu_dereference(*rcu_ptr);
267
268 if (WARN_ON(!ba_data))
269 goto unlock;
270
271 if (!ba_data->timeout)
272 goto unlock;
273
274 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
275 if (time_is_after_jiffies(timeout)) {
276 mod_timer(&ba_data->session_timer, timeout);
277 goto unlock;
278 }
279
280 /* Timer expired */
281 sta_id = ffs(ba_data->sta_mask) - 1; /* don't care which one */
282 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[sta_id]);
283
284 /*
285 * sta should be valid unless the following happens:
286 * The firmware asserts which triggers a reconfig flow, but
287 * the reconfig fails before we set the pointer to sta into
288 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
289 * A-MDPU and hence the timer continues to run. Then, the
290 * timer expires and sta is NULL.
291 */
292 if (IS_ERR_OR_NULL(sta))
293 goto unlock;
294
295 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
296 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
297 sta->addr, ba_data->tid);
298unlock:
299 rcu_read_unlock();
300}
301
302/* Disable aggregations for a bitmap of TIDs for a given station */
303static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
304 unsigned long disable_agg_tids,
305 bool remove_queue)
306{
307 struct iwl_mvm_add_sta_cmd cmd = {};
308 struct ieee80211_sta *sta;
309 struct iwl_mvm_sta *mvmsta;
310 u32 status;
311 u8 sta_id;
312
313 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
314 return -EINVAL;
315
316 sta_id = mvm->queue_info[queue].ra_sta_id;
317
318 rcu_read_lock();
319
320 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
321
322 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
323 rcu_read_unlock();
324 return -EINVAL;
325 }
326
327 mvmsta = iwl_mvm_sta_from_mac80211(sta);
328
329 mvmsta->tid_disable_agg |= disable_agg_tids;
330
331 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
332 cmd.sta_id = mvmsta->deflink.sta_id;
333 cmd.add_modify = STA_MODE_MODIFY;
334 cmd.modify_mask = STA_MODIFY_QUEUES;
335 if (disable_agg_tids)
336 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
337 if (remove_queue)
338 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
339 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
340 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
341
342 rcu_read_unlock();
343
344 /* Notify FW of queue removal from the STA queues */
345 status = ADD_STA_SUCCESS;
346 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
347 iwl_mvm_add_sta_cmd_size(mvm),
348 &cmd, &status);
349}
350
351static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
352 int sta_id, u16 *queueptr, u8 tid)
353{
354 int queue = *queueptr;
355 struct iwl_scd_txq_cfg_cmd cmd = {
356 .scd_queue = queue,
357 .action = SCD_CFG_DISABLE_QUEUE,
358 };
359 int ret;
360
361 lockdep_assert_held(&mvm->mutex);
362
363 if (iwl_mvm_has_new_tx_api(mvm)) {
364 if (mvm->sta_remove_requires_queue_remove) {
365 u32 cmd_id = WIDE_ID(DATA_PATH_GROUP,
366 SCD_QUEUE_CONFIG_CMD);
367 struct iwl_scd_queue_cfg_cmd remove_cmd = {
368 .operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE),
369 .u.remove.sta_mask = cpu_to_le32(BIT(sta_id)),
370 };
371
372 if (tid == IWL_MAX_TID_COUNT)
373 tid = IWL_MGMT_TID;
374
375 remove_cmd.u.remove.tid = cpu_to_le32(tid);
376
377 ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0,
378 sizeof(remove_cmd),
379 &remove_cmd);
380 } else {
381 ret = 0;
382 }
383
384 iwl_trans_txq_free(mvm->trans, queue);
385 *queueptr = IWL_MVM_INVALID_QUEUE;
386
387 return ret;
388 }
389
390 if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
391 return 0;
392
393 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
394
395 cmd.action = mvm->queue_info[queue].tid_bitmap ?
396 SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
397 if (cmd.action == SCD_CFG_DISABLE_QUEUE)
398 mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
399
400 IWL_DEBUG_TX_QUEUES(mvm,
401 "Disabling TXQ #%d tids=0x%x\n",
402 queue,
403 mvm->queue_info[queue].tid_bitmap);
404
405 /* If the queue is still enabled - nothing left to do in this func */
406 if (cmd.action == SCD_CFG_ENABLE_QUEUE)
407 return 0;
408
409 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
410 cmd.tid = mvm->queue_info[queue].txq_tid;
411
412 /* Make sure queue info is correct even though we overwrite it */
413 WARN(mvm->queue_info[queue].tid_bitmap,
414 "TXQ #%d info out-of-sync - tids=0x%x\n",
415 queue, mvm->queue_info[queue].tid_bitmap);
416
417 /* If we are here - the queue is freed and we can zero out these vals */
418 mvm->queue_info[queue].tid_bitmap = 0;
419
420 if (sta) {
421 struct iwl_mvm_txq *mvmtxq =
422 iwl_mvm_txq_from_tid(sta, tid);
423
424 spin_lock_bh(&mvm->add_stream_lock);
425 list_del_init(&mvmtxq->list);
426 clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
427 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
428 spin_unlock_bh(&mvm->add_stream_lock);
429 }
430
431 /* Regardless if this is a reserved TXQ for a STA - mark it as false */
432 mvm->queue_info[queue].reserved = false;
433
434 iwl_trans_txq_disable(mvm->trans, queue, false);
435 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0,
436 sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
437
438 if (ret)
439 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
440 queue, ret);
441 return ret;
442}
443
444static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
445{
446 struct ieee80211_sta *sta;
447 struct iwl_mvm_sta *mvmsta;
448 unsigned long tid_bitmap;
449 unsigned long agg_tids = 0;
450 u8 sta_id;
451 int tid;
452
453 lockdep_assert_held(&mvm->mutex);
454
455 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
456 return -EINVAL;
457
458 sta_id = mvm->queue_info[queue].ra_sta_id;
459 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
460
461 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
462 lockdep_is_held(&mvm->mutex));
463
464 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
465 return -EINVAL;
466
467 mvmsta = iwl_mvm_sta_from_mac80211(sta);
468
469 spin_lock_bh(&mvmsta->lock);
470 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
471 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
472 agg_tids |= BIT(tid);
473 }
474 spin_unlock_bh(&mvmsta->lock);
475
476 return agg_tids;
477}
478
479/*
480 * Remove a queue from a station's resources.
481 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
482 * doesn't disable the queue
483 */
484static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
485{
486 struct ieee80211_sta *sta;
487 struct iwl_mvm_sta *mvmsta;
488 unsigned long tid_bitmap;
489 unsigned long disable_agg_tids = 0;
490 u8 sta_id;
491 int tid;
492
493 lockdep_assert_held(&mvm->mutex);
494
495 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
496 return -EINVAL;
497
498 sta_id = mvm->queue_info[queue].ra_sta_id;
499 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
500
501 rcu_read_lock();
502
503 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
504
505 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
506 rcu_read_unlock();
507 return 0;
508 }
509
510 mvmsta = iwl_mvm_sta_from_mac80211(sta);
511
512 spin_lock_bh(&mvmsta->lock);
513 /* Unmap MAC queues and TIDs from this queue */
514 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
515 struct iwl_mvm_txq *mvmtxq =
516 iwl_mvm_txq_from_tid(sta, tid);
517
518 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
519 disable_agg_tids |= BIT(tid);
520 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
521
522 spin_lock_bh(&mvm->add_stream_lock);
523 list_del_init(&mvmtxq->list);
524 clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
525 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
526 spin_unlock_bh(&mvm->add_stream_lock);
527 }
528
529 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
530 spin_unlock_bh(&mvmsta->lock);
531
532 rcu_read_unlock();
533
534 /*
535 * The TX path may have been using this TXQ_ID from the tid_data,
536 * so make sure it's no longer running so that we can safely reuse
537 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
538 * above, but nothing guarantees we've stopped using them. Thus,
539 * without this, we could get to iwl_mvm_disable_txq() and remove
540 * the queue while still sending frames to it.
541 */
542 synchronize_net();
543
544 return disable_agg_tids;
545}
546
547static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
548 struct ieee80211_sta *old_sta,
549 u8 new_sta_id)
550{
551 struct iwl_mvm_sta *mvmsta;
552 u8 sta_id, tid;
553 unsigned long disable_agg_tids = 0;
554 bool same_sta;
555 u16 queue_tmp = queue;
556 int ret;
557
558 lockdep_assert_held(&mvm->mutex);
559
560 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
561 return -EINVAL;
562
563 sta_id = mvm->queue_info[queue].ra_sta_id;
564 tid = mvm->queue_info[queue].txq_tid;
565
566 same_sta = sta_id == new_sta_id;
567
568 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
569 if (WARN_ON(!mvmsta))
570 return -EINVAL;
571
572 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
573 /* Disable the queue */
574 if (disable_agg_tids)
575 iwl_mvm_invalidate_sta_queue(mvm, queue,
576 disable_agg_tids, false);
577
578 ret = iwl_mvm_disable_txq(mvm, old_sta, sta_id, &queue_tmp, tid);
579 if (ret) {
580 IWL_ERR(mvm,
581 "Failed to free inactive queue %d (ret=%d)\n",
582 queue, ret);
583
584 return ret;
585 }
586
587 /* If TXQ is allocated to another STA, update removal in FW */
588 if (!same_sta)
589 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
590
591 return 0;
592}
593
594static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
595 unsigned long tfd_queue_mask, u8 ac)
596{
597 int queue = 0;
598 u8 ac_to_queue[IEEE80211_NUM_ACS];
599 int i;
600
601 /*
602 * This protects us against grabbing a queue that's being reconfigured
603 * by the inactivity checker.
604 */
605 lockdep_assert_held(&mvm->mutex);
606
607 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
608 return -EINVAL;
609
610 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
611
612 /* See what ACs the existing queues for this STA have */
613 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
614 /* Only DATA queues can be shared */
615 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
616 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
617 continue;
618
619 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
620 }
621
622 /*
623 * The queue to share is chosen only from DATA queues as follows (in
624 * descending priority):
625 * 1. An AC_BE queue
626 * 2. Same AC queue
627 * 3. Highest AC queue that is lower than new AC
628 * 4. Any existing AC (there always is at least 1 DATA queue)
629 */
630
631 /* Priority 1: An AC_BE queue */
632 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
633 queue = ac_to_queue[IEEE80211_AC_BE];
634 /* Priority 2: Same AC queue */
635 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
636 queue = ac_to_queue[ac];
637 /* Priority 3a: If new AC is VO and VI exists - use VI */
638 else if (ac == IEEE80211_AC_VO &&
639 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
640 queue = ac_to_queue[IEEE80211_AC_VI];
641 /* Priority 3b: No BE so only AC less than the new one is BK */
642 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
643 queue = ac_to_queue[IEEE80211_AC_BK];
644 /* Priority 4a: No BE nor BK - use VI if exists */
645 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
646 queue = ac_to_queue[IEEE80211_AC_VI];
647 /* Priority 4b: No BE, BK nor VI - use VO if exists */
648 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
649 queue = ac_to_queue[IEEE80211_AC_VO];
650
651 /* Make sure queue found (or not) is legal */
652 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
653 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
654 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
655 IWL_ERR(mvm, "No DATA queues available to share\n");
656 return -ENOSPC;
657 }
658
659 return queue;
660}
661
662/* Re-configure the SCD for a queue that has already been configured */
663static int iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo,
664 int sta_id, int tid, int frame_limit, u16 ssn)
665{
666 struct iwl_scd_txq_cfg_cmd cmd = {
667 .scd_queue = queue,
668 .action = SCD_CFG_ENABLE_QUEUE,
669 .window = frame_limit,
670 .sta_id = sta_id,
671 .ssn = cpu_to_le16(ssn),
672 .tx_fifo = fifo,
673 .aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
674 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
675 .tid = tid,
676 };
677 int ret;
678
679 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
680 return -EINVAL;
681
682 if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
683 "Trying to reconfig unallocated queue %d\n", queue))
684 return -ENXIO;
685
686 IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
687
688 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
689 WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
690 queue, fifo, ret);
691
692 return ret;
693}
694
695/*
696 * If a given queue has a higher AC than the TID stream that is being compared
697 * to, the queue needs to be redirected to the lower AC. This function does that
698 * in such a case, otherwise - if no redirection required - it does nothing,
699 * unless the %force param is true.
700 */
701static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
702 int ac, int ssn, unsigned int wdg_timeout,
703 bool force, struct iwl_mvm_txq *txq)
704{
705 struct iwl_scd_txq_cfg_cmd cmd = {
706 .scd_queue = queue,
707 .action = SCD_CFG_DISABLE_QUEUE,
708 };
709 bool shared_queue;
710 int ret;
711
712 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
713 return -EINVAL;
714
715 /*
716 * If the AC is lower than current one - FIFO needs to be redirected to
717 * the lowest one of the streams in the queue. Check if this is needed
718 * here.
719 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
720 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
721 * we need to check if the numerical value of X is LARGER than of Y.
722 */
723 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
724 IWL_DEBUG_TX_QUEUES(mvm,
725 "No redirection needed on TXQ #%d\n",
726 queue);
727 return 0;
728 }
729
730 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
731 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
732 cmd.tid = mvm->queue_info[queue].txq_tid;
733 shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
734
735 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
736 queue, iwl_mvm_ac_to_tx_fifo[ac]);
737
738 /* Stop the queue and wait for it to empty */
739 set_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state);
740
741 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
742 if (ret) {
743 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
744 queue);
745 ret = -EIO;
746 goto out;
747 }
748
749 /* Before redirecting the queue we need to de-activate it */
750 iwl_trans_txq_disable(mvm->trans, queue, false);
751 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
752 if (ret)
753 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
754 ret);
755
756 /* Make sure the SCD wrptr is correctly set before reconfiguring */
757 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
758
759 /* Update the TID "owner" of the queue */
760 mvm->queue_info[queue].txq_tid = tid;
761
762 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
763
764 /* Redirect to lower AC */
765 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
766 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
767
768 /* Update AC marking of the queue */
769 mvm->queue_info[queue].mac80211_ac = ac;
770
771 /*
772 * Mark queue as shared in transport if shared
773 * Note this has to be done after queue enablement because enablement
774 * can also set this value, and there is no indication there to shared
775 * queues
776 */
777 if (shared_queue)
778 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
779
780out:
781 /* Continue using the queue */
782 clear_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT, &txq->state);
783
784 return ret;
785}
786
787static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
788 u8 minq, u8 maxq)
789{
790 int i;
791
792 lockdep_assert_held(&mvm->mutex);
793
794 if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
795 "max queue %d >= num_of_queues (%d)", maxq,
796 mvm->trans->trans_cfg->base_params->num_of_queues))
797 maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
798
799 /* This should not be hit with new TX path */
800 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
801 return -ENOSPC;
802
803 /* Start by looking for a free queue */
804 for (i = minq; i <= maxq; i++)
805 if (mvm->queue_info[i].tid_bitmap == 0 &&
806 mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
807 return i;
808
809 return -ENOSPC;
810}
811
812static int iwl_mvm_get_queue_size(struct ieee80211_sta *sta)
813{
814 int max_size = IWL_DEFAULT_QUEUE_SIZE;
815 unsigned int link_id;
816
817 /* this queue isn't used for traffic (cab_queue) */
818 if (!sta)
819 return IWL_MGMT_QUEUE_SIZE;
820
821 rcu_read_lock();
822
823 for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) {
824 struct ieee80211_link_sta *link =
825 rcu_dereference(sta->link[link_id]);
826
827 if (!link)
828 continue;
829
830 /* support for 512 ba size */
831 if (link->eht_cap.has_eht &&
832 max_size < IWL_DEFAULT_QUEUE_SIZE_EHT)
833 max_size = IWL_DEFAULT_QUEUE_SIZE_EHT;
834
835 /* support for 256 ba size */
836 if (link->he_cap.has_he &&
837 max_size < IWL_DEFAULT_QUEUE_SIZE_HE)
838 max_size = IWL_DEFAULT_QUEUE_SIZE_HE;
839 }
840
841 rcu_read_unlock();
842 return max_size;
843}
844
845int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
846 struct ieee80211_sta *sta,
847 u8 sta_id, u8 tid, unsigned int timeout)
848{
849 int queue, size;
850 u32 sta_mask = 0;
851
852 if (tid == IWL_MAX_TID_COUNT) {
853 tid = IWL_MGMT_TID;
854 size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
855 mvm->trans->cfg->min_txq_size);
856 } else {
857 size = iwl_mvm_get_queue_size(sta);
858 }
859
860 /* take the min with bc tbl entries allowed */
861 size = min_t(u32, size, mvm->trans->txqs.bc_tbl_size / sizeof(u16));
862
863 /* size needs to be power of 2 values for calculating read/write pointers */
864 size = rounddown_pow_of_two(size);
865
866 if (sta) {
867 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
868 struct ieee80211_link_sta *link_sta;
869 unsigned int link_id;
870
871 rcu_read_lock();
872 for_each_sta_active_link(mvmsta->vif, sta, link_sta, link_id) {
873 struct iwl_mvm_link_sta *link =
874 rcu_dereference_protected(mvmsta->link[link_id],
875 lockdep_is_held(&mvm->mutex));
876
877 if (!link)
878 continue;
879
880 sta_mask |= BIT(link->sta_id);
881 }
882 rcu_read_unlock();
883 } else {
884 sta_mask |= BIT(sta_id);
885 }
886
887 if (!sta_mask)
888 return -EINVAL;
889
890 do {
891 queue = iwl_trans_txq_alloc(mvm->trans, 0, sta_mask,
892 tid, size, timeout);
893
894 if (queue < 0)
895 IWL_DEBUG_TX_QUEUES(mvm,
896 "Failed allocating TXQ of size %d for sta mask %x tid %d, ret: %d\n",
897 size, sta_mask, tid, queue);
898 size /= 2;
899 } while (queue < 0 && size >= 16);
900
901 if (queue < 0)
902 return queue;
903
904 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta mask 0x%x tid %d\n",
905 queue, sta_mask, tid);
906
907 return queue;
908}
909
910static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
911 struct ieee80211_sta *sta, u8 ac,
912 int tid)
913{
914 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
915 struct iwl_mvm_txq *mvmtxq =
916 iwl_mvm_txq_from_tid(sta, tid);
917 unsigned int wdg_timeout =
918 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
919 int queue = -1;
920
921 lockdep_assert_held(&mvm->mutex);
922
923 IWL_DEBUG_TX_QUEUES(mvm,
924 "Allocating queue for sta %d on tid %d\n",
925 mvmsta->deflink.sta_id, tid);
926 queue = iwl_mvm_tvqm_enable_txq(mvm, sta, mvmsta->deflink.sta_id,
927 tid, wdg_timeout);
928 if (queue < 0)
929 return queue;
930
931 mvmtxq->txq_id = queue;
932 mvm->tvqm_info[queue].txq_tid = tid;
933 mvm->tvqm_info[queue].sta_id = mvmsta->deflink.sta_id;
934
935 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
936
937 spin_lock_bh(&mvmsta->lock);
938 mvmsta->tid_data[tid].txq_id = queue;
939 spin_unlock_bh(&mvmsta->lock);
940
941 return 0;
942}
943
944static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
945 struct ieee80211_sta *sta,
946 int queue, u8 sta_id, u8 tid)
947{
948 bool enable_queue = true;
949
950 /* Make sure this TID isn't already enabled */
951 if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
952 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
953 queue, tid);
954 return false;
955 }
956
957 /* Update mappings and refcounts */
958 if (mvm->queue_info[queue].tid_bitmap)
959 enable_queue = false;
960
961 mvm->queue_info[queue].tid_bitmap |= BIT(tid);
962 mvm->queue_info[queue].ra_sta_id = sta_id;
963
964 if (enable_queue) {
965 if (tid != IWL_MAX_TID_COUNT)
966 mvm->queue_info[queue].mac80211_ac =
967 tid_to_mac80211_ac[tid];
968 else
969 mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
970
971 mvm->queue_info[queue].txq_tid = tid;
972 }
973
974 if (sta) {
975 struct iwl_mvm_txq *mvmtxq =
976 iwl_mvm_txq_from_tid(sta, tid);
977
978 mvmtxq->txq_id = queue;
979 }
980
981 IWL_DEBUG_TX_QUEUES(mvm,
982 "Enabling TXQ #%d tids=0x%x\n",
983 queue, mvm->queue_info[queue].tid_bitmap);
984
985 return enable_queue;
986}
987
988static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
989 int queue, u16 ssn,
990 const struct iwl_trans_txq_scd_cfg *cfg,
991 unsigned int wdg_timeout)
992{
993 struct iwl_scd_txq_cfg_cmd cmd = {
994 .scd_queue = queue,
995 .action = SCD_CFG_ENABLE_QUEUE,
996 .window = cfg->frame_limit,
997 .sta_id = cfg->sta_id,
998 .ssn = cpu_to_le16(ssn),
999 .tx_fifo = cfg->fifo,
1000 .aggregate = cfg->aggregate,
1001 .tid = cfg->tid,
1002 };
1003 bool inc_ssn;
1004
1005 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1006 return false;
1007
1008 /* Send the enabling command if we need to */
1009 if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
1010 return false;
1011
1012 inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
1013 NULL, wdg_timeout);
1014 if (inc_ssn)
1015 le16_add_cpu(&cmd.ssn, 1);
1016
1017 WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
1018 "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
1019
1020 return inc_ssn;
1021}
1022
1023static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
1024{
1025 struct iwl_scd_txq_cfg_cmd cmd = {
1026 .scd_queue = queue,
1027 .action = SCD_CFG_UPDATE_QUEUE_TID,
1028 };
1029 int tid;
1030 unsigned long tid_bitmap;
1031 int ret;
1032
1033 lockdep_assert_held(&mvm->mutex);
1034
1035 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1036 return;
1037
1038 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1039
1040 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
1041 return;
1042
1043 /* Find any TID for queue */
1044 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1045 cmd.tid = tid;
1046 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
1047
1048 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
1049 if (ret) {
1050 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
1051 queue, ret);
1052 return;
1053 }
1054
1055 mvm->queue_info[queue].txq_tid = tid;
1056 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
1057 queue, tid);
1058}
1059
1060static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
1061{
1062 struct ieee80211_sta *sta;
1063 struct iwl_mvm_sta *mvmsta;
1064 u8 sta_id;
1065 int tid = -1;
1066 unsigned long tid_bitmap;
1067 unsigned int wdg_timeout;
1068 int ssn;
1069 int ret = true;
1070
1071 /* queue sharing is disabled on new TX path */
1072 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1073 return;
1074
1075 lockdep_assert_held(&mvm->mutex);
1076
1077 sta_id = mvm->queue_info[queue].ra_sta_id;
1078 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1079
1080 /* Find TID for queue, and make sure it is the only one on the queue */
1081 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1082 if (tid_bitmap != BIT(tid)) {
1083 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
1084 queue, tid_bitmap);
1085 return;
1086 }
1087
1088 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
1089 tid);
1090
1091 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1092 lockdep_is_held(&mvm->mutex));
1093
1094 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1095 return;
1096
1097 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1098 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1099
1100 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1101
1102 ret = iwl_mvm_redirect_queue(mvm, queue, tid,
1103 tid_to_mac80211_ac[tid], ssn,
1104 wdg_timeout, true,
1105 iwl_mvm_txq_from_tid(sta, tid));
1106 if (ret) {
1107 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1108 return;
1109 }
1110
1111 /* If aggs should be turned back on - do it */
1112 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
1113 struct iwl_mvm_add_sta_cmd cmd = {0};
1114
1115 mvmsta->tid_disable_agg &= ~BIT(tid);
1116
1117 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1118 cmd.sta_id = mvmsta->deflink.sta_id;
1119 cmd.add_modify = STA_MODE_MODIFY;
1120 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1121 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1122 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1123
1124 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1125 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1126 if (!ret) {
1127 IWL_DEBUG_TX_QUEUES(mvm,
1128 "TXQ #%d is now aggregated again\n",
1129 queue);
1130
1131 /* Mark queue intenally as aggregating again */
1132 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1133 }
1134 }
1135
1136 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1137}
1138
1139/*
1140 * Remove inactive TIDs of a given queue.
1141 * If all queue TIDs are inactive - mark the queue as inactive
1142 * If only some the queue TIDs are inactive - unmap them from the queue
1143 *
1144 * Returns %true if all TIDs were removed and the queue could be reused.
1145 */
1146static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1147 struct iwl_mvm_sta *mvmsta, int queue,
1148 unsigned long tid_bitmap,
1149 unsigned long *unshare_queues,
1150 unsigned long *changetid_queues)
1151{
1152 unsigned int tid;
1153
1154 lockdep_assert_held(&mvmsta->lock);
1155 lockdep_assert_held(&mvm->mutex);
1156
1157 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1158 return false;
1159
1160 /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1161 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1162 /* If some TFDs are still queued - don't mark TID as inactive */
1163 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1164 tid_bitmap &= ~BIT(tid);
1165
1166 /* Don't mark as inactive any TID that has an active BA */
1167 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1168 tid_bitmap &= ~BIT(tid);
1169 }
1170
1171 /* If all TIDs in the queue are inactive - return it can be reused */
1172 if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1173 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1174 return true;
1175 }
1176
1177 /*
1178 * If we are here, this is a shared queue and not all TIDs timed-out.
1179 * Remove the ones that did.
1180 */
1181 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1182 u16 q_tid_bitmap;
1183
1184 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1185 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1186
1187 q_tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1188
1189 /*
1190 * We need to take into account a situation in which a TXQ was
1191 * allocated to TID x, and then turned shared by adding TIDs y
1192 * and z. If TID x becomes inactive and is removed from the TXQ,
1193 * ownership must be given to one of the remaining TIDs.
1194 * This is mainly because if TID x continues - a new queue can't
1195 * be allocated for it as long as it is an owner of another TXQ.
1196 *
1197 * Mark this queue in the right bitmap, we'll send the command
1198 * to the firmware later.
1199 */
1200 if (!(q_tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1201 set_bit(queue, changetid_queues);
1202
1203 IWL_DEBUG_TX_QUEUES(mvm,
1204 "Removing inactive TID %d from shared Q:%d\n",
1205 tid, queue);
1206 }
1207
1208 IWL_DEBUG_TX_QUEUES(mvm,
1209 "TXQ #%d left with tid bitmap 0x%x\n", queue,
1210 mvm->queue_info[queue].tid_bitmap);
1211
1212 /*
1213 * There may be different TIDs with the same mac queues, so make
1214 * sure all TIDs have existing corresponding mac queues enabled
1215 */
1216 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1217
1218 /* If the queue is marked as shared - "unshare" it */
1219 if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1220 mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1221 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1222 queue);
1223 set_bit(queue, unshare_queues);
1224 }
1225
1226 return false;
1227}
1228
1229/*
1230 * Check for inactivity - this includes checking if any queue
1231 * can be unshared and finding one (and only one) that can be
1232 * reused.
1233 * This function is also invoked as a sort of clean-up task,
1234 * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1235 *
1236 * Returns the queue number, or -ENOSPC.
1237 */
1238static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1239{
1240 unsigned long now = jiffies;
1241 unsigned long unshare_queues = 0;
1242 unsigned long changetid_queues = 0;
1243 int i, ret, free_queue = -ENOSPC;
1244 struct ieee80211_sta *queue_owner = NULL;
1245
1246 lockdep_assert_held(&mvm->mutex);
1247
1248 if (iwl_mvm_has_new_tx_api(mvm))
1249 return -ENOSPC;
1250
1251 rcu_read_lock();
1252
1253 /* we skip the CMD queue below by starting at 1 */
1254 BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1255
1256 for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1257 struct ieee80211_sta *sta;
1258 struct iwl_mvm_sta *mvmsta;
1259 u8 sta_id;
1260 int tid;
1261 unsigned long inactive_tid_bitmap = 0;
1262 unsigned long queue_tid_bitmap;
1263
1264 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1265 if (!queue_tid_bitmap)
1266 continue;
1267
1268 /* If TXQ isn't in active use anyway - nothing to do here... */
1269 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1270 mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1271 continue;
1272
1273 /* Check to see if there are inactive TIDs on this queue */
1274 for_each_set_bit(tid, &queue_tid_bitmap,
1275 IWL_MAX_TID_COUNT + 1) {
1276 if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1277 IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1278 continue;
1279
1280 inactive_tid_bitmap |= BIT(tid);
1281 }
1282
1283 /* If all TIDs are active - finish check on this queue */
1284 if (!inactive_tid_bitmap)
1285 continue;
1286
1287 /*
1288 * If we are here - the queue hadn't been served recently and is
1289 * in use
1290 */
1291
1292 sta_id = mvm->queue_info[i].ra_sta_id;
1293 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1294
1295 /*
1296 * If the STA doesn't exist anymore, it isn't an error. It could
1297 * be that it was removed since getting the queues, and in this
1298 * case it should've inactivated its queues anyway.
1299 */
1300 if (IS_ERR_OR_NULL(sta))
1301 continue;
1302
1303 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1304
1305 spin_lock_bh(&mvmsta->lock);
1306 ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1307 inactive_tid_bitmap,
1308 &unshare_queues,
1309 &changetid_queues);
1310 if (ret && free_queue < 0) {
1311 queue_owner = sta;
1312 free_queue = i;
1313 }
1314 /* only unlock sta lock - we still need the queue info lock */
1315 spin_unlock_bh(&mvmsta->lock);
1316 }
1317
1318
1319 /* Reconfigure queues requiring reconfiguation */
1320 for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1321 iwl_mvm_unshare_queue(mvm, i);
1322 for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1323 iwl_mvm_change_queue_tid(mvm, i);
1324
1325 rcu_read_unlock();
1326
1327 if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1328 ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1329 alloc_for_sta);
1330 if (ret)
1331 return ret;
1332 }
1333
1334 return free_queue;
1335}
1336
1337static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1338 struct ieee80211_sta *sta, u8 ac, int tid)
1339{
1340 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1341 struct iwl_trans_txq_scd_cfg cfg = {
1342 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1343 .sta_id = mvmsta->deflink.sta_id,
1344 .tid = tid,
1345 .frame_limit = IWL_FRAME_LIMIT,
1346 };
1347 unsigned int wdg_timeout =
1348 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1349 int queue = -1;
1350 u16 queue_tmp;
1351 unsigned long disable_agg_tids = 0;
1352 enum iwl_mvm_agg_state queue_state;
1353 bool shared_queue = false, inc_ssn;
1354 int ssn;
1355 unsigned long tfd_queue_mask;
1356 int ret;
1357
1358 lockdep_assert_held(&mvm->mutex);
1359
1360 if (iwl_mvm_has_new_tx_api(mvm))
1361 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1362
1363 spin_lock_bh(&mvmsta->lock);
1364 tfd_queue_mask = mvmsta->tfd_queue_msk;
1365 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1366 spin_unlock_bh(&mvmsta->lock);
1367
1368 if (tid == IWL_MAX_TID_COUNT) {
1369 queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1370 IWL_MVM_DQA_MIN_MGMT_QUEUE,
1371 IWL_MVM_DQA_MAX_MGMT_QUEUE);
1372 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1373 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1374 queue);
1375
1376 /* If no such queue is found, we'll use a DATA queue instead */
1377 }
1378
1379 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1380 (mvm->queue_info[mvmsta->reserved_queue].status ==
1381 IWL_MVM_QUEUE_RESERVED)) {
1382 queue = mvmsta->reserved_queue;
1383 mvm->queue_info[queue].reserved = true;
1384 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1385 }
1386
1387 if (queue < 0)
1388 queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1389 IWL_MVM_DQA_MIN_DATA_QUEUE,
1390 IWL_MVM_DQA_MAX_DATA_QUEUE);
1391 if (queue < 0) {
1392 /* try harder - perhaps kill an inactive queue */
1393 queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id);
1394 }
1395
1396 /* No free queue - we'll have to share */
1397 if (queue <= 0) {
1398 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1399 if (queue > 0) {
1400 shared_queue = true;
1401 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1402 }
1403 }
1404
1405 /*
1406 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1407 * to make sure no one else takes it.
1408 * This will allow avoiding re-acquiring the lock at the end of the
1409 * configuration. On error we'll mark it back as free.
1410 */
1411 if (queue > 0 && !shared_queue)
1412 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1413
1414 /* This shouldn't happen - out of queues */
1415 if (WARN_ON(queue <= 0)) {
1416 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1417 tid, cfg.sta_id);
1418 return queue;
1419 }
1420
1421 /*
1422 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1423 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1424 * as aggregatable.
1425 * Mark all DATA queues as allowing to be aggregated at some point
1426 */
1427 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1428 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1429
1430 IWL_DEBUG_TX_QUEUES(mvm,
1431 "Allocating %squeue #%d to sta %d on tid %d\n",
1432 shared_queue ? "shared " : "", queue,
1433 mvmsta->deflink.sta_id, tid);
1434
1435 if (shared_queue) {
1436 /* Disable any open aggs on this queue */
1437 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1438
1439 if (disable_agg_tids) {
1440 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1441 queue);
1442 iwl_mvm_invalidate_sta_queue(mvm, queue,
1443 disable_agg_tids, false);
1444 }
1445 }
1446
1447 inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1448
1449 /*
1450 * Mark queue as shared in transport if shared
1451 * Note this has to be done after queue enablement because enablement
1452 * can also set this value, and there is no indication there to shared
1453 * queues
1454 */
1455 if (shared_queue)
1456 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1457
1458 spin_lock_bh(&mvmsta->lock);
1459 /*
1460 * This looks racy, but it is not. We have only one packet for
1461 * this ra/tid in our Tx path since we stop the Qdisc when we
1462 * need to allocate a new TFD queue.
1463 */
1464 if (inc_ssn) {
1465 mvmsta->tid_data[tid].seq_number += 0x10;
1466 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1467 }
1468 mvmsta->tid_data[tid].txq_id = queue;
1469 mvmsta->tfd_queue_msk |= BIT(queue);
1470 queue_state = mvmsta->tid_data[tid].state;
1471
1472 if (mvmsta->reserved_queue == queue)
1473 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1474 spin_unlock_bh(&mvmsta->lock);
1475
1476 if (!shared_queue) {
1477 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1478 if (ret)
1479 goto out_err;
1480
1481 /* If we need to re-enable aggregations... */
1482 if (queue_state == IWL_AGG_ON) {
1483 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1484 if (ret)
1485 goto out_err;
1486 }
1487 } else {
1488 /* Redirect queue, if needed */
1489 ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1490 wdg_timeout, false,
1491 iwl_mvm_txq_from_tid(sta, tid));
1492 if (ret)
1493 goto out_err;
1494 }
1495
1496 return 0;
1497
1498out_err:
1499 queue_tmp = queue;
1500 iwl_mvm_disable_txq(mvm, sta, mvmsta->deflink.sta_id, &queue_tmp, tid);
1501
1502 return ret;
1503}
1504
1505int iwl_mvm_sta_ensure_queue(struct iwl_mvm *mvm,
1506 struct ieee80211_txq *txq)
1507{
1508 struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
1509 int ret = -EINVAL;
1510
1511 lockdep_assert_held(&mvm->mutex);
1512
1513 if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
1514 !txq->sta) {
1515 return 0;
1516 }
1517
1518 if (!iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, txq->tid)) {
1519 set_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
1520 ret = 0;
1521 }
1522
1523 local_bh_disable();
1524 spin_lock(&mvm->add_stream_lock);
1525 if (!list_empty(&mvmtxq->list))
1526 list_del_init(&mvmtxq->list);
1527 spin_unlock(&mvm->add_stream_lock);
1528 local_bh_enable();
1529
1530 return ret;
1531}
1532
1533void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1534{
1535 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1536 add_stream_wk);
1537
1538 mutex_lock(&mvm->mutex);
1539
1540 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1541
1542 while (!list_empty(&mvm->add_stream_txqs)) {
1543 struct iwl_mvm_txq *mvmtxq;
1544 struct ieee80211_txq *txq;
1545 u8 tid;
1546
1547 mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1548 struct iwl_mvm_txq, list);
1549
1550 txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1551 drv_priv);
1552 tid = txq->tid;
1553 if (tid == IEEE80211_NUM_TIDS)
1554 tid = IWL_MAX_TID_COUNT;
1555
1556 /*
1557 * We can't really do much here, but if this fails we can't
1558 * transmit anyway - so just don't transmit the frame etc.
1559 * and let them back up ... we've tried our best to allocate
1560 * a queue in the function itself.
1561 */
1562 if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
1563 spin_lock_bh(&mvm->add_stream_lock);
1564 list_del_init(&mvmtxq->list);
1565 spin_unlock_bh(&mvm->add_stream_lock);
1566 continue;
1567 }
1568
1569 /* now we're ready, any remaining races/concurrency will be
1570 * handled in iwl_mvm_mac_itxq_xmit()
1571 */
1572 set_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
1573
1574 local_bh_disable();
1575 spin_lock(&mvm->add_stream_lock);
1576 list_del_init(&mvmtxq->list);
1577 spin_unlock(&mvm->add_stream_lock);
1578
1579 iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1580 local_bh_enable();
1581 }
1582
1583 mutex_unlock(&mvm->mutex);
1584}
1585
1586static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1587 struct ieee80211_sta *sta,
1588 enum nl80211_iftype vif_type)
1589{
1590 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1591 int queue;
1592
1593 /* queue reserving is disabled on new TX path */
1594 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1595 return 0;
1596
1597 /* run the general cleanup/unsharing of queues */
1598 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1599
1600 /* Make sure we have free resources for this STA */
1601 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1602 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1603 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1604 IWL_MVM_QUEUE_FREE))
1605 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1606 else
1607 queue = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
1608 IWL_MVM_DQA_MIN_DATA_QUEUE,
1609 IWL_MVM_DQA_MAX_DATA_QUEUE);
1610 if (queue < 0) {
1611 /* try again - this time kick out a queue if needed */
1612 queue = iwl_mvm_inactivity_check(mvm, mvmsta->deflink.sta_id);
1613 if (queue < 0) {
1614 IWL_ERR(mvm, "No available queues for new station\n");
1615 return -ENOSPC;
1616 }
1617 }
1618 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1619
1620 mvmsta->reserved_queue = queue;
1621
1622 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1623 queue, mvmsta->deflink.sta_id);
1624
1625 return 0;
1626}
1627
1628/*
1629 * In DQA mode, after a HW restart the queues should be allocated as before, in
1630 * order to avoid race conditions when there are shared queues. This function
1631 * does the re-mapping and queue allocation.
1632 *
1633 * Note that re-enabling aggregations isn't done in this function.
1634 */
1635void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1636 struct ieee80211_sta *sta)
1637{
1638 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1639 unsigned int wdg =
1640 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1641 int i;
1642 struct iwl_trans_txq_scd_cfg cfg = {
1643 .sta_id = mvm_sta->deflink.sta_id,
1644 .frame_limit = IWL_FRAME_LIMIT,
1645 };
1646
1647 /* Make sure reserved queue is still marked as such (if allocated) */
1648 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1649 mvm->queue_info[mvm_sta->reserved_queue].status =
1650 IWL_MVM_QUEUE_RESERVED;
1651
1652 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1653 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1654 int txq_id = tid_data->txq_id;
1655 int ac;
1656
1657 if (txq_id == IWL_MVM_INVALID_QUEUE)
1658 continue;
1659
1660 ac = tid_to_mac80211_ac[i];
1661
1662 if (iwl_mvm_has_new_tx_api(mvm)) {
1663 IWL_DEBUG_TX_QUEUES(mvm,
1664 "Re-mapping sta %d tid %d\n",
1665 mvm_sta->deflink.sta_id, i);
1666 txq_id = iwl_mvm_tvqm_enable_txq(mvm, sta,
1667 mvm_sta->deflink.sta_id,
1668 i, wdg);
1669 /*
1670 * on failures, just set it to IWL_MVM_INVALID_QUEUE
1671 * to try again later, we have no other good way of
1672 * failing here
1673 */
1674 if (txq_id < 0)
1675 txq_id = IWL_MVM_INVALID_QUEUE;
1676 tid_data->txq_id = txq_id;
1677
1678 /*
1679 * Since we don't set the seq number after reset, and HW
1680 * sets it now, FW reset will cause the seq num to start
1681 * at 0 again, so driver will need to update it
1682 * internally as well, so it keeps in sync with real val
1683 */
1684 tid_data->seq_number = 0;
1685 } else {
1686 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1687
1688 cfg.tid = i;
1689 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1690 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1691 txq_id ==
1692 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1693
1694 IWL_DEBUG_TX_QUEUES(mvm,
1695 "Re-mapping sta %d tid %d to queue %d\n",
1696 mvm_sta->deflink.sta_id, i,
1697 txq_id);
1698
1699 iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1700 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1701 }
1702 }
1703}
1704
1705static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1706 struct iwl_mvm_int_sta *sta,
1707 const u8 *addr,
1708 u16 mac_id, u16 color)
1709{
1710 struct iwl_mvm_add_sta_cmd cmd;
1711 int ret;
1712 u32 status = ADD_STA_SUCCESS;
1713
1714 lockdep_assert_held(&mvm->mutex);
1715
1716 memset(&cmd, 0, sizeof(cmd));
1717 cmd.sta_id = sta->sta_id;
1718
1719 if (iwl_mvm_has_new_station_api(mvm->fw) &&
1720 sta->type == IWL_STA_AUX_ACTIVITY)
1721 cmd.mac_id_n_color = cpu_to_le32(mac_id);
1722 else
1723 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1724 color));
1725
1726 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1727 cmd.station_type = sta->type;
1728
1729 if (!iwl_mvm_has_new_tx_api(mvm))
1730 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1731 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1732
1733 if (addr)
1734 memcpy(cmd.addr, addr, ETH_ALEN);
1735
1736 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1737 iwl_mvm_add_sta_cmd_size(mvm),
1738 &cmd, &status);
1739 if (ret)
1740 return ret;
1741
1742 switch (status & IWL_ADD_STA_STATUS_MASK) {
1743 case ADD_STA_SUCCESS:
1744 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1745 return 0;
1746 default:
1747 ret = -EIO;
1748 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1749 status);
1750 break;
1751 }
1752 return ret;
1753}
1754
1755/* Initialize driver data of a new sta */
1756int iwl_mvm_sta_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1757 struct ieee80211_sta *sta, int sta_id, u8 sta_type)
1758{
1759 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1760 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1761 struct iwl_mvm_rxq_dup_data *dup_data;
1762 int i, ret = 0;
1763
1764 lockdep_assert_held(&mvm->mutex);
1765
1766 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1767 mvmvif->color);
1768 mvm_sta->vif = vif;
1769
1770 /* for MLD sta_id(s) should be allocated for each link before calling
1771 * this function
1772 */
1773 if (!mvm->mld_api_is_used) {
1774 if (WARN_ON(sta_id == IWL_MVM_INVALID_STA))
1775 return -EINVAL;
1776
1777 mvm_sta->deflink.sta_id = sta_id;
1778 rcu_assign_pointer(mvm_sta->link[0], &mvm_sta->deflink);
1779
1780 if (!mvm->trans->trans_cfg->gen2)
1781 mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
1782 LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1783 else
1784 mvm_sta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
1785 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1786 }
1787
1788 mvm_sta->tt_tx_protection = false;
1789 mvm_sta->sta_type = sta_type;
1790
1791 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1792
1793 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1794 /*
1795 * Mark all queues for this STA as unallocated and defer TX
1796 * frames until the queue is allocated
1797 */
1798 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1799 }
1800
1801 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1802 struct iwl_mvm_txq *mvmtxq =
1803 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1804
1805 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1806 INIT_LIST_HEAD(&mvmtxq->list);
1807 atomic_set(&mvmtxq->tx_request, 0);
1808 }
1809
1810 if (iwl_mvm_has_new_rx_api(mvm)) {
1811 int q;
1812
1813 dup_data = kcalloc(mvm->trans->num_rx_queues,
1814 sizeof(*dup_data), GFP_KERNEL);
1815 if (!dup_data)
1816 return -ENOMEM;
1817 /*
1818 * Initialize all the last_seq values to 0xffff which can never
1819 * compare equal to the frame's seq_ctrl in the check in
1820 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1821 * number and fragmented packets don't reach that function.
1822 *
1823 * This thus allows receiving a packet with seqno 0 and the
1824 * retry bit set as the very first packet on a new TID.
1825 */
1826 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1827 memset(dup_data[q].last_seq, 0xff,
1828 sizeof(dup_data[q].last_seq));
1829 mvm_sta->dup_data = dup_data;
1830 }
1831
1832 if (!iwl_mvm_has_new_tx_api(mvm)) {
1833 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1834 ieee80211_vif_type_p2p(vif));
1835 if (ret)
1836 return ret;
1837 }
1838
1839 /*
1840 * if rs is registered with mac80211, then "add station" will be handled
1841 * via the corresponding ops, otherwise need to notify rate scaling here
1842 */
1843 if (iwl_mvm_has_tlc_offload(mvm))
1844 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1845 else
1846 spin_lock_init(&mvm_sta->deflink.lq_sta.rs_drv.pers.lock);
1847
1848 iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1849
1850 return 0;
1851}
1852
1853int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1854 struct ieee80211_vif *vif,
1855 struct ieee80211_sta *sta)
1856{
1857 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1858 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1859 int ret, sta_id;
1860 bool sta_update = false;
1861 unsigned int sta_flags = 0;
1862
1863 lockdep_assert_held(&mvm->mutex);
1864
1865 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1866 sta_id = iwl_mvm_find_free_sta_id(mvm,
1867 ieee80211_vif_type_p2p(vif));
1868 else
1869 sta_id = mvm_sta->deflink.sta_id;
1870
1871 if (sta_id == IWL_MVM_INVALID_STA)
1872 return -ENOSPC;
1873
1874 spin_lock_init(&mvm_sta->lock);
1875
1876 /* if this is a HW restart re-alloc existing queues */
1877 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1878 struct iwl_mvm_int_sta tmp_sta = {
1879 .sta_id = sta_id,
1880 .type = mvm_sta->sta_type,
1881 };
1882
1883 /* First add an empty station since allocating
1884 * a queue requires a valid station
1885 */
1886 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1887 mvmvif->id, mvmvif->color);
1888 if (ret)
1889 goto err;
1890
1891 iwl_mvm_realloc_queues_after_restart(mvm, sta);
1892 sta_update = true;
1893 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1894 goto update_fw;
1895 }
1896
1897 ret = iwl_mvm_sta_init(mvm, vif, sta, sta_id,
1898 sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK);
1899 if (ret)
1900 goto err;
1901
1902update_fw:
1903 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1904 if (ret)
1905 goto err;
1906
1907 if (vif->type == NL80211_IFTYPE_STATION) {
1908 if (!sta->tdls) {
1909 WARN_ON(mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA);
1910 mvmvif->deflink.ap_sta_id = sta_id;
1911 } else {
1912 WARN_ON(mvmvif->deflink.ap_sta_id == IWL_MVM_INVALID_STA);
1913 }
1914 }
1915
1916 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1917
1918 return 0;
1919
1920err:
1921 return ret;
1922}
1923
1924int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1925 bool drain)
1926{
1927 struct iwl_mvm_add_sta_cmd cmd = {};
1928 int ret;
1929 u32 status;
1930
1931 lockdep_assert_held(&mvm->mutex);
1932
1933 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1934 cmd.sta_id = mvmsta->deflink.sta_id;
1935 cmd.add_modify = STA_MODE_MODIFY;
1936 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1937 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1938
1939 status = ADD_STA_SUCCESS;
1940 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1941 iwl_mvm_add_sta_cmd_size(mvm),
1942 &cmd, &status);
1943 if (ret)
1944 return ret;
1945
1946 switch (status & IWL_ADD_STA_STATUS_MASK) {
1947 case ADD_STA_SUCCESS:
1948 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1949 mvmsta->deflink.sta_id);
1950 break;
1951 default:
1952 ret = -EIO;
1953 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1954 mvmsta->deflink.sta_id);
1955 break;
1956 }
1957
1958 return ret;
1959}
1960
1961/*
1962 * Remove a station from the FW table. Before sending the command to remove
1963 * the station validate that the station is indeed known to the driver (sanity
1964 * only).
1965 */
1966static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1967{
1968 struct ieee80211_sta *sta;
1969 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1970 .sta_id = sta_id,
1971 };
1972 int ret;
1973
1974 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1975 lockdep_is_held(&mvm->mutex));
1976
1977 /* Note: internal stations are marked as error values */
1978 if (!sta) {
1979 IWL_ERR(mvm, "Invalid station id\n");
1980 return -EINVAL;
1981 }
1982
1983 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1984 sizeof(rm_sta_cmd), &rm_sta_cmd);
1985 if (ret) {
1986 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1987 return ret;
1988 }
1989
1990 return 0;
1991}
1992
1993static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1994 struct ieee80211_vif *vif,
1995 struct ieee80211_sta *sta)
1996{
1997 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1998 int i;
1999
2000 lockdep_assert_held(&mvm->mutex);
2001
2002 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
2003 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
2004 continue;
2005
2006 iwl_mvm_disable_txq(mvm, sta, mvm_sta->deflink.sta_id,
2007 &mvm_sta->tid_data[i].txq_id, i);
2008 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
2009 }
2010
2011 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
2012 struct iwl_mvm_txq *mvmtxq =
2013 iwl_mvm_txq_from_mac80211(sta->txq[i]);
2014
2015 spin_lock_bh(&mvm->add_stream_lock);
2016 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
2017 list_del_init(&mvmtxq->list);
2018 clear_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state);
2019 spin_unlock_bh(&mvm->add_stream_lock);
2020 }
2021}
2022
2023int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
2024 struct iwl_mvm_sta *mvm_sta)
2025{
2026 int i;
2027
2028 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
2029 u16 txq_id;
2030 int ret;
2031
2032 spin_lock_bh(&mvm_sta->lock);
2033 txq_id = mvm_sta->tid_data[i].txq_id;
2034 spin_unlock_bh(&mvm_sta->lock);
2035
2036 if (txq_id == IWL_MVM_INVALID_QUEUE)
2037 continue;
2038
2039 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
2040 if (ret)
2041 return ret;
2042 }
2043
2044 return 0;
2045}
2046
2047/* Execute the common part for both MLD and non-MLD modes.
2048 * Returns if we're done with removing the station, either
2049 * with error or success
2050 */
2051bool iwl_mvm_sta_del(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2052 struct ieee80211_sta *sta,
2053 struct ieee80211_link_sta *link_sta, int *ret)
2054{
2055 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2056 struct iwl_mvm_vif_link_info *mvm_link =
2057 mvmvif->link[link_sta->link_id];
2058 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2059 struct iwl_mvm_link_sta *mvm_link_sta;
2060 u8 sta_id;
2061
2062 lockdep_assert_held(&mvm->mutex);
2063
2064 mvm_link_sta =
2065 rcu_dereference_protected(mvm_sta->link[link_sta->link_id],
2066 lockdep_is_held(&mvm->mutex));
2067 sta_id = mvm_link_sta->sta_id;
2068
2069 /* If there is a TXQ still marked as reserved - free it */
2070 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
2071 u8 reserved_txq = mvm_sta->reserved_queue;
2072 enum iwl_mvm_queue_status *status;
2073
2074 /*
2075 * If no traffic has gone through the reserved TXQ - it
2076 * is still marked as IWL_MVM_QUEUE_RESERVED, and
2077 * should be manually marked as free again
2078 */
2079 status = &mvm->queue_info[reserved_txq].status;
2080 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
2081 (*status != IWL_MVM_QUEUE_FREE),
2082 "sta_id %d reserved txq %d status %d",
2083 sta_id, reserved_txq, *status)) {
2084 *ret = -EINVAL;
2085 return true;
2086 }
2087
2088 *status = IWL_MVM_QUEUE_FREE;
2089 }
2090
2091 if (vif->type == NL80211_IFTYPE_STATION &&
2092 mvm_link->ap_sta_id == sta_id) {
2093 /* if associated - we can't remove the AP STA now */
2094 if (vif->cfg.assoc)
2095 return true;
2096
2097 /* first remove remaining keys */
2098 iwl_mvm_sec_key_remove_ap(mvm, vif, mvm_link, 0);
2099
2100 /* unassoc - go ahead - remove the AP STA now */
2101 mvm_link->ap_sta_id = IWL_MVM_INVALID_STA;
2102 }
2103
2104 /*
2105 * This shouldn't happen - the TDLS channel switch should be canceled
2106 * before the STA is removed.
2107 */
2108 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
2109 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
2110 cancel_delayed_work(&mvm->tdls_cs.dwork);
2111 }
2112
2113 return false;
2114}
2115
2116int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
2117 struct ieee80211_vif *vif,
2118 struct ieee80211_sta *sta)
2119{
2120 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2121 int ret;
2122
2123 lockdep_assert_held(&mvm->mutex);
2124
2125 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
2126 if (ret)
2127 return ret;
2128
2129 /* flush its queues here since we are freeing mvm_sta */
2130 ret = iwl_mvm_flush_sta(mvm, mvm_sta->deflink.sta_id,
2131 mvm_sta->tfd_queue_msk);
2132 if (ret)
2133 return ret;
2134 if (iwl_mvm_has_new_tx_api(mvm)) {
2135 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
2136 } else {
2137 u32 q_mask = mvm_sta->tfd_queue_msk;
2138
2139 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2140 q_mask);
2141 }
2142 if (ret)
2143 return ret;
2144
2145 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
2146
2147 iwl_mvm_disable_sta_queues(mvm, vif, sta);
2148
2149 if (iwl_mvm_sta_del(mvm, vif, sta, &sta->deflink, &ret))
2150 return ret;
2151
2152 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->deflink.sta_id);
2153 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->deflink.sta_id], NULL);
2154
2155 return ret;
2156}
2157
2158int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
2159 struct ieee80211_vif *vif,
2160 u8 sta_id)
2161{
2162 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
2163
2164 lockdep_assert_held(&mvm->mutex);
2165
2166 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
2167 return ret;
2168}
2169
2170int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
2171 struct iwl_mvm_int_sta *sta,
2172 u32 qmask, enum nl80211_iftype iftype,
2173 u8 type)
2174{
2175 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
2176 sta->sta_id == IWL_MVM_INVALID_STA) {
2177 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
2178 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
2179 return -ENOSPC;
2180 }
2181
2182 sta->tfd_queue_msk = qmask;
2183 sta->type = type;
2184
2185 /* put a non-NULL value so iterating over the stations won't stop */
2186 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
2187 return 0;
2188}
2189
2190void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
2191{
2192 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
2193 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
2194 sta->sta_id = IWL_MVM_INVALID_STA;
2195}
2196
2197static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
2198 u8 sta_id, u8 fifo)
2199{
2200 unsigned int wdg_timeout =
2201 mvm->trans->trans_cfg->base_params->wd_timeout;
2202 struct iwl_trans_txq_scd_cfg cfg = {
2203 .fifo = fifo,
2204 .sta_id = sta_id,
2205 .tid = IWL_MAX_TID_COUNT,
2206 .aggregate = false,
2207 .frame_limit = IWL_FRAME_LIMIT,
2208 };
2209
2210 WARN_ON(iwl_mvm_has_new_tx_api(mvm));
2211
2212 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2213}
2214
2215static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
2216{
2217 unsigned int wdg_timeout =
2218 mvm->trans->trans_cfg->base_params->wd_timeout;
2219
2220 WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
2221
2222 return iwl_mvm_tvqm_enable_txq(mvm, NULL, sta_id, IWL_MAX_TID_COUNT,
2223 wdg_timeout);
2224}
2225
2226static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2227 int maccolor, u8 *addr,
2228 struct iwl_mvm_int_sta *sta,
2229 u16 *queue, int fifo)
2230{
2231 int ret;
2232
2233 /* Map queue to fifo - needs to happen before adding station */
2234 if (!iwl_mvm_has_new_tx_api(mvm))
2235 iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2236
2237 ret = iwl_mvm_add_int_sta_common(mvm, sta, addr, macidx, maccolor);
2238 if (ret) {
2239 if (!iwl_mvm_has_new_tx_api(mvm))
2240 iwl_mvm_disable_txq(mvm, NULL, sta->sta_id, queue,
2241 IWL_MAX_TID_COUNT);
2242 return ret;
2243 }
2244
2245 /*
2246 * For 22000 firmware and on we cannot add queue to a station unknown
2247 * to firmware so enable queue here - after the station was added
2248 */
2249 if (iwl_mvm_has_new_tx_api(mvm)) {
2250 int txq;
2251
2252 txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2253 if (txq < 0) {
2254 iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2255 return txq;
2256 }
2257
2258 *queue = txq;
2259 }
2260
2261 return 0;
2262}
2263
2264int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
2265{
2266 int ret;
2267 u32 qmask = mvm->aux_queue == IWL_MVM_INVALID_QUEUE ? 0 :
2268 BIT(mvm->aux_queue);
2269
2270 lockdep_assert_held(&mvm->mutex);
2271
2272 /* Allocate aux station and assign to it the aux queue */
2273 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, qmask,
2274 NL80211_IFTYPE_UNSPECIFIED,
2275 IWL_STA_AUX_ACTIVITY);
2276 if (ret)
2277 return ret;
2278
2279 /*
2280 * In CDB NICs we need to specify which lmac to use for aux activity
2281 * using the mac_id argument place to send lmac_id to the function
2282 */
2283 ret = iwl_mvm_add_int_sta_with_queue(mvm, lmac_id, 0, NULL,
2284 &mvm->aux_sta, &mvm->aux_queue,
2285 IWL_MVM_TX_FIFO_MCAST);
2286 if (ret) {
2287 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2288 return ret;
2289 }
2290
2291 return 0;
2292}
2293
2294int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2295{
2296 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2297
2298 lockdep_assert_held(&mvm->mutex);
2299
2300 return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2301 NULL, &mvm->snif_sta,
2302 &mvm->snif_queue,
2303 IWL_MVM_TX_FIFO_BE);
2304}
2305
2306int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2307{
2308 int ret;
2309
2310 lockdep_assert_held(&mvm->mutex);
2311
2312 if (WARN_ON_ONCE(mvm->snif_sta.sta_id == IWL_MVM_INVALID_STA))
2313 return -EINVAL;
2314
2315 iwl_mvm_disable_txq(mvm, NULL, mvm->snif_sta.sta_id,
2316 &mvm->snif_queue, IWL_MAX_TID_COUNT);
2317 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2318 if (ret)
2319 IWL_WARN(mvm, "Failed sending remove station\n");
2320
2321 return ret;
2322}
2323
2324int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2325{
2326 int ret;
2327
2328 lockdep_assert_held(&mvm->mutex);
2329
2330 if (WARN_ON_ONCE(mvm->aux_sta.sta_id == IWL_MVM_INVALID_STA))
2331 return -EINVAL;
2332
2333 iwl_mvm_disable_txq(mvm, NULL, mvm->aux_sta.sta_id,
2334 &mvm->aux_queue, IWL_MAX_TID_COUNT);
2335 ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2336 if (ret)
2337 IWL_WARN(mvm, "Failed sending remove station\n");
2338 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2339
2340 return ret;
2341}
2342
2343void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2344{
2345 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2346}
2347
2348/*
2349 * Send the add station command for the vif's broadcast station.
2350 * Assumes that the station was already allocated.
2351 *
2352 * @mvm: the mvm component
2353 * @vif: the interface to which the broadcast station is added
2354 * @bsta: the broadcast station to add.
2355 */
2356int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2357{
2358 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2359 struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta;
2360 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2361 const u8 *baddr = _baddr;
2362 int queue;
2363 int ret;
2364 unsigned int wdg_timeout =
2365 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2366 struct iwl_trans_txq_scd_cfg cfg = {
2367 .fifo = IWL_MVM_TX_FIFO_VO,
2368 .sta_id = mvmvif->deflink.bcast_sta.sta_id,
2369 .tid = IWL_MAX_TID_COUNT,
2370 .aggregate = false,
2371 .frame_limit = IWL_FRAME_LIMIT,
2372 };
2373
2374 lockdep_assert_held(&mvm->mutex);
2375
2376 if (!iwl_mvm_has_new_tx_api(mvm)) {
2377 if (vif->type == NL80211_IFTYPE_AP ||
2378 vif->type == NL80211_IFTYPE_ADHOC) {
2379 queue = mvm->probe_queue;
2380 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2381 queue = mvm->p2p_dev_queue;
2382 } else {
2383 WARN(1, "Missing required TXQ for adding bcast STA\n");
2384 return -EINVAL;
2385 }
2386
2387 bsta->tfd_queue_msk |= BIT(queue);
2388
2389 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2390 }
2391
2392 if (vif->type == NL80211_IFTYPE_ADHOC)
2393 baddr = vif->bss_conf.bssid;
2394
2395 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2396 return -ENOSPC;
2397
2398 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2399 mvmvif->id, mvmvif->color);
2400 if (ret)
2401 return ret;
2402
2403 /*
2404 * For 22000 firmware and on we cannot add queue to a station unknown
2405 * to firmware so enable queue here - after the station was added
2406 */
2407 if (iwl_mvm_has_new_tx_api(mvm)) {
2408 queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, bsta->sta_id,
2409 IWL_MAX_TID_COUNT,
2410 wdg_timeout);
2411 if (queue < 0) {
2412 iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2413 return queue;
2414 }
2415
2416 if (vif->type == NL80211_IFTYPE_AP ||
2417 vif->type == NL80211_IFTYPE_ADHOC) {
2418 /* for queue management */
2419 mvm->probe_queue = queue;
2420 /* for use in TX */
2421 mvmvif->deflink.mgmt_queue = queue;
2422 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2423 mvm->p2p_dev_queue = queue;
2424 }
2425 } else if (vif->type == NL80211_IFTYPE_AP ||
2426 vif->type == NL80211_IFTYPE_ADHOC) {
2427 /* set it for use in TX */
2428 mvmvif->deflink.mgmt_queue = mvm->probe_queue;
2429 }
2430
2431 return 0;
2432}
2433
2434void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2435 struct ieee80211_vif *vif)
2436{
2437 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2438 u16 *queueptr, queue;
2439
2440 lockdep_assert_held(&mvm->mutex);
2441
2442 iwl_mvm_flush_sta(mvm, mvmvif->deflink.bcast_sta.sta_id,
2443 mvmvif->deflink.bcast_sta.tfd_queue_msk);
2444
2445 switch (vif->type) {
2446 case NL80211_IFTYPE_AP:
2447 case NL80211_IFTYPE_ADHOC:
2448 queueptr = &mvm->probe_queue;
2449 break;
2450 case NL80211_IFTYPE_P2P_DEVICE:
2451 queueptr = &mvm->p2p_dev_queue;
2452 break;
2453 default:
2454 WARN(1, "Can't free bcast queue on vif type %d\n",
2455 vif->type);
2456 return;
2457 }
2458
2459 queue = *queueptr;
2460 iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.bcast_sta.sta_id,
2461 queueptr, IWL_MAX_TID_COUNT);
2462
2463 if (vif->type == NL80211_IFTYPE_AP || vif->type == NL80211_IFTYPE_ADHOC)
2464 mvmvif->deflink.mgmt_queue = mvm->probe_queue;
2465
2466 if (iwl_mvm_has_new_tx_api(mvm))
2467 return;
2468
2469 WARN_ON(!(mvmvif->deflink.bcast_sta.tfd_queue_msk & BIT(queue)));
2470 mvmvif->deflink.bcast_sta.tfd_queue_msk &= ~BIT(queue);
2471}
2472
2473/* Send the FW a request to remove the station from it's internal data
2474 * structures, but DO NOT remove the entry from the local data structures. */
2475int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2476{
2477 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2478 int ret;
2479
2480 lockdep_assert_held(&mvm->mutex);
2481
2482 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2483
2484 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.bcast_sta.sta_id);
2485 if (ret)
2486 IWL_WARN(mvm, "Failed sending remove station\n");
2487 return ret;
2488}
2489
2490int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2491{
2492 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2493
2494 lockdep_assert_held(&mvm->mutex);
2495
2496 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.bcast_sta, 0,
2497 ieee80211_vif_type_p2p(vif),
2498 IWL_STA_GENERAL_PURPOSE);
2499}
2500
2501/* Allocate a new station entry for the broadcast station to the given vif,
2502 * and send it to the FW.
2503 * Note that each P2P mac should have its own broadcast station.
2504 *
2505 * @mvm: the mvm component
2506 * @vif: the interface to which the broadcast station is added
2507 * @bsta: the broadcast station to add. */
2508int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2509{
2510 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2511 struct iwl_mvm_int_sta *bsta = &mvmvif->deflink.bcast_sta;
2512 int ret;
2513
2514 lockdep_assert_held(&mvm->mutex);
2515
2516 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2517 if (ret)
2518 return ret;
2519
2520 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2521
2522 if (ret)
2523 iwl_mvm_dealloc_int_sta(mvm, bsta);
2524
2525 return ret;
2526}
2527
2528void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2529{
2530 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2531
2532 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.bcast_sta);
2533}
2534
2535/*
2536 * Send the FW a request to remove the station from it's internal data
2537 * structures, and in addition remove it from the local data structure.
2538 */
2539int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2540{
2541 int ret;
2542
2543 lockdep_assert_held(&mvm->mutex);
2544
2545 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2546
2547 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2548
2549 return ret;
2550}
2551
2552/*
2553 * Allocate a new station entry for the multicast station to the given vif,
2554 * and send it to the FW.
2555 * Note that each AP/GO mac should have its own multicast station.
2556 *
2557 * @mvm: the mvm component
2558 * @vif: the interface to which the multicast station is added
2559 */
2560int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2561{
2562 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2563 struct iwl_mvm_int_sta *msta = &mvmvif->deflink.mcast_sta;
2564 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2565 const u8 *maddr = _maddr;
2566 struct iwl_trans_txq_scd_cfg cfg = {
2567 .fifo = vif->type == NL80211_IFTYPE_AP ?
2568 IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2569 .sta_id = msta->sta_id,
2570 .tid = 0,
2571 .aggregate = false,
2572 .frame_limit = IWL_FRAME_LIMIT,
2573 };
2574 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2575 int ret;
2576
2577 lockdep_assert_held(&mvm->mutex);
2578
2579 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2580 vif->type != NL80211_IFTYPE_ADHOC))
2581 return -EOPNOTSUPP;
2582
2583 /*
2584 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2585 * invalid, so make sure we use the queue we want.
2586 * Note that this is done here as we want to avoid making DQA
2587 * changes in mac80211 layer.
2588 */
2589 if (vif->type == NL80211_IFTYPE_ADHOC)
2590 mvmvif->deflink.cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2591
2592 /*
2593 * While in previous FWs we had to exclude cab queue from TFD queue
2594 * mask, now it is needed as any other queue.
2595 */
2596 if (!iwl_mvm_has_new_tx_api(mvm) &&
2597 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2598 iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0,
2599 &cfg,
2600 timeout);
2601 msta->tfd_queue_msk |= BIT(mvmvif->deflink.cab_queue);
2602 }
2603 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2604 mvmvif->id, mvmvif->color);
2605 if (ret)
2606 goto err;
2607
2608 /*
2609 * Enable cab queue after the ADD_STA command is sent.
2610 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2611 * command with unknown station id, and for FW that doesn't support
2612 * station API since the cab queue is not included in the
2613 * tfd_queue_mask.
2614 */
2615 if (iwl_mvm_has_new_tx_api(mvm)) {
2616 int queue = iwl_mvm_tvqm_enable_txq(mvm, NULL, msta->sta_id,
2617 0, timeout);
2618 if (queue < 0) {
2619 ret = queue;
2620 goto err;
2621 }
2622 mvmvif->deflink.cab_queue = queue;
2623 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2624 IWL_UCODE_TLV_API_STA_TYPE))
2625 iwl_mvm_enable_txq(mvm, NULL, mvmvif->deflink.cab_queue, 0,
2626 &cfg,
2627 timeout);
2628
2629 return 0;
2630err:
2631 iwl_mvm_dealloc_int_sta(mvm, msta);
2632 return ret;
2633}
2634
2635static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2636 struct ieee80211_key_conf *keyconf,
2637 bool mcast)
2638{
2639 union {
2640 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2641 struct iwl_mvm_add_sta_key_cmd cmd;
2642 } u = {};
2643 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2644 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2645 __le16 key_flags;
2646 int ret, size;
2647 u32 status;
2648
2649 /* This is a valid situation for GTK removal */
2650 if (sta_id == IWL_MVM_INVALID_STA)
2651 return 0;
2652
2653 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2654 STA_KEY_FLG_KEYID_MSK);
2655 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2656 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2657
2658 if (mcast)
2659 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2660
2661 /*
2662 * The fields assigned here are in the same location at the start
2663 * of the command, so we can do this union trick.
2664 */
2665 u.cmd.common.key_flags = key_flags;
2666 u.cmd.common.key_offset = keyconf->hw_key_idx;
2667 u.cmd.common.sta_id = sta_id;
2668
2669 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2670
2671 status = ADD_STA_SUCCESS;
2672 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2673 &status);
2674
2675 switch (status) {
2676 case ADD_STA_SUCCESS:
2677 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2678 break;
2679 default:
2680 ret = -EIO;
2681 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2682 break;
2683 }
2684
2685 return ret;
2686}
2687
2688/*
2689 * Send the FW a request to remove the station from it's internal data
2690 * structures, and in addition remove it from the local data structure.
2691 */
2692int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2693{
2694 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2695 int ret;
2696
2697 lockdep_assert_held(&mvm->mutex);
2698
2699 iwl_mvm_flush_sta(mvm, mvmvif->deflink.mcast_sta.sta_id,
2700 mvmvif->deflink.mcast_sta.tfd_queue_msk);
2701
2702 iwl_mvm_disable_txq(mvm, NULL, mvmvif->deflink.mcast_sta.sta_id,
2703 &mvmvif->deflink.cab_queue, 0);
2704
2705 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->deflink.mcast_sta.sta_id);
2706 if (ret)
2707 IWL_WARN(mvm, "Failed sending remove station\n");
2708
2709 return ret;
2710}
2711
2712static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2713{
2714 struct iwl_mvm_delba_data notif = {
2715 .baid = baid,
2716 };
2717
2718 iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NOTIF_DEL_BA, true,
2719 ¬if, sizeof(notif));
2720};
2721
2722static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2723 struct iwl_mvm_baid_data *data)
2724{
2725 int i;
2726
2727 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2728
2729 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2730 int j;
2731 struct iwl_mvm_reorder_buffer *reorder_buf =
2732 &data->reorder_buf[i];
2733 struct iwl_mvm_reorder_buf_entry *entries =
2734 &data->entries[i * data->entries_per_queue];
2735
2736 spin_lock_bh(&reorder_buf->lock);
2737 if (likely(!reorder_buf->num_stored)) {
2738 spin_unlock_bh(&reorder_buf->lock);
2739 continue;
2740 }
2741
2742 /*
2743 * This shouldn't happen in regular DELBA since the internal
2744 * delBA notification should trigger a release of all frames in
2745 * the reorder buffer.
2746 */
2747 WARN_ON(1);
2748
2749 for (j = 0; j < reorder_buf->buf_size; j++)
2750 __skb_queue_purge(&entries[j].frames);
2751
2752 spin_unlock_bh(&reorder_buf->lock);
2753 }
2754}
2755
2756static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2757 struct iwl_mvm_baid_data *data,
2758 u16 ssn, u16 buf_size)
2759{
2760 int i;
2761
2762 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2763 struct iwl_mvm_reorder_buffer *reorder_buf =
2764 &data->reorder_buf[i];
2765 struct iwl_mvm_reorder_buf_entry *entries =
2766 &data->entries[i * data->entries_per_queue];
2767 int j;
2768
2769 reorder_buf->num_stored = 0;
2770 reorder_buf->head_sn = ssn;
2771 reorder_buf->buf_size = buf_size;
2772 spin_lock_init(&reorder_buf->lock);
2773 reorder_buf->mvm = mvm;
2774 reorder_buf->queue = i;
2775 reorder_buf->valid = false;
2776 for (j = 0; j < reorder_buf->buf_size; j++)
2777 __skb_queue_head_init(&entries[j].frames);
2778 }
2779}
2780
2781static int iwl_mvm_fw_baid_op_sta(struct iwl_mvm *mvm,
2782 struct ieee80211_sta *sta,
2783 bool start, int tid, u16 ssn,
2784 u16 buf_size)
2785{
2786 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2787 struct iwl_mvm_add_sta_cmd cmd = {
2788 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
2789 .sta_id = mvm_sta->deflink.sta_id,
2790 .add_modify = STA_MODE_MODIFY,
2791 };
2792 u32 status;
2793 int ret;
2794
2795 if (start) {
2796 cmd.add_immediate_ba_tid = tid;
2797 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2798 cmd.rx_ba_window = cpu_to_le16(buf_size);
2799 cmd.modify_mask = STA_MODIFY_ADD_BA_TID;
2800 } else {
2801 cmd.remove_immediate_ba_tid = tid;
2802 cmd.modify_mask = STA_MODIFY_REMOVE_BA_TID;
2803 }
2804
2805 status = ADD_STA_SUCCESS;
2806 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2807 iwl_mvm_add_sta_cmd_size(mvm),
2808 &cmd, &status);
2809 if (ret)
2810 return ret;
2811
2812 switch (status & IWL_ADD_STA_STATUS_MASK) {
2813 case ADD_STA_SUCCESS:
2814 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2815 start ? "start" : "stopp");
2816 if (WARN_ON(start && iwl_mvm_has_new_rx_api(mvm) &&
2817 !(status & IWL_ADD_STA_BAID_VALID_MASK)))
2818 return -EINVAL;
2819 return u32_get_bits(status, IWL_ADD_STA_BAID_MASK);
2820 case ADD_STA_IMMEDIATE_BA_FAILURE:
2821 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2822 return -ENOSPC;
2823 default:
2824 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2825 start ? "start" : "stopp", status);
2826 return -EIO;
2827 }
2828}
2829
2830static int iwl_mvm_fw_baid_op_cmd(struct iwl_mvm *mvm,
2831 struct ieee80211_sta *sta,
2832 bool start, int tid, u16 ssn,
2833 u16 buf_size, int baid)
2834{
2835 struct iwl_rx_baid_cfg_cmd cmd = {
2836 .action = start ? cpu_to_le32(IWL_RX_BAID_ACTION_ADD) :
2837 cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
2838 };
2839 u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
2840 int ret;
2841
2842 BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
2843
2844 if (start) {
2845 cmd.alloc.sta_id_mask =
2846 cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1));
2847 cmd.alloc.tid = tid;
2848 cmd.alloc.ssn = cpu_to_le16(ssn);
2849 cmd.alloc.win_size = cpu_to_le16(buf_size);
2850 baid = -EIO;
2851 } else if (iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 1) == 1) {
2852 cmd.remove_v1.baid = cpu_to_le32(baid);
2853 BUILD_BUG_ON(sizeof(cmd.remove_v1) > sizeof(cmd.remove));
2854 } else {
2855 cmd.remove.sta_id_mask =
2856 cpu_to_le32(iwl_mvm_sta_fw_id_mask(mvm, sta, -1));
2857 cmd.remove.tid = cpu_to_le32(tid);
2858 }
2859
2860 ret = iwl_mvm_send_cmd_pdu_status(mvm, cmd_id, sizeof(cmd),
2861 &cmd, &baid);
2862 if (ret)
2863 return ret;
2864
2865 if (!start) {
2866 /* ignore firmware baid on remove */
2867 baid = 0;
2868 }
2869
2870 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2871 start ? "start" : "stopp");
2872
2873 if (baid < 0 || baid >= ARRAY_SIZE(mvm->baid_map))
2874 return -EINVAL;
2875
2876 return baid;
2877}
2878
2879static int iwl_mvm_fw_baid_op(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2880 bool start, int tid, u16 ssn, u16 buf_size,
2881 int baid)
2882{
2883 if (fw_has_capa(&mvm->fw->ucode_capa,
2884 IWL_UCODE_TLV_CAPA_BAID_ML_SUPPORT))
2885 return iwl_mvm_fw_baid_op_cmd(mvm, sta, start,
2886 tid, ssn, buf_size, baid);
2887
2888 return iwl_mvm_fw_baid_op_sta(mvm, sta, start,
2889 tid, ssn, buf_size);
2890}
2891
2892int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2893 int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2894{
2895 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2896 struct iwl_mvm_baid_data *baid_data = NULL;
2897 int ret, baid;
2898 u32 max_ba_id_sessions = iwl_mvm_has_new_tx_api(mvm) ? IWL_MAX_BAID :
2899 IWL_MAX_BAID_OLD;
2900
2901 lockdep_assert_held(&mvm->mutex);
2902
2903 if (start && mvm->rx_ba_sessions >= max_ba_id_sessions) {
2904 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2905 return -ENOSPC;
2906 }
2907
2908 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2909 u32 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2910
2911 /* sparse doesn't like the __align() so don't check */
2912#ifndef __CHECKER__
2913 /*
2914 * The division below will be OK if either the cache line size
2915 * can be divided by the entry size (ALIGN will round up) or if
2916 * if the entry size can be divided by the cache line size, in
2917 * which case the ALIGN() will do nothing.
2918 */
2919 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2920 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2921#endif
2922
2923 /*
2924 * Upward align the reorder buffer size to fill an entire cache
2925 * line for each queue, to avoid sharing cache lines between
2926 * different queues.
2927 */
2928 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2929
2930 /*
2931 * Allocate here so if allocation fails we can bail out early
2932 * before starting the BA session in the firmware
2933 */
2934 baid_data = kzalloc(sizeof(*baid_data) +
2935 mvm->trans->num_rx_queues *
2936 reorder_buf_size,
2937 GFP_KERNEL);
2938 if (!baid_data)
2939 return -ENOMEM;
2940
2941 /*
2942 * This division is why we need the above BUILD_BUG_ON(),
2943 * if that doesn't hold then this will not be right.
2944 */
2945 baid_data->entries_per_queue =
2946 reorder_buf_size / sizeof(baid_data->entries[0]);
2947 }
2948
2949 if (iwl_mvm_has_new_rx_api(mvm) && !start) {
2950 baid = mvm_sta->tid_to_baid[tid];
2951 } else {
2952 /* we don't really need it in this case */
2953 baid = -1;
2954 }
2955
2956 /* Don't send command to remove (start=0) BAID during restart */
2957 if (start || !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
2958 baid = iwl_mvm_fw_baid_op(mvm, sta, start, tid, ssn, buf_size,
2959 baid);
2960
2961 if (baid < 0) {
2962 ret = baid;
2963 goto out_free;
2964 }
2965
2966 if (start) {
2967 mvm->rx_ba_sessions++;
2968
2969 if (!iwl_mvm_has_new_rx_api(mvm))
2970 return 0;
2971
2972 baid_data->baid = baid;
2973 baid_data->timeout = timeout;
2974 baid_data->last_rx = jiffies;
2975 baid_data->rcu_ptr = &mvm->baid_map[baid];
2976 timer_setup(&baid_data->session_timer,
2977 iwl_mvm_rx_agg_session_expired, 0);
2978 baid_data->mvm = mvm;
2979 baid_data->tid = tid;
2980 baid_data->sta_mask = iwl_mvm_sta_fw_id_mask(mvm, sta, -1);
2981
2982 mvm_sta->tid_to_baid[tid] = baid;
2983 if (timeout)
2984 mod_timer(&baid_data->session_timer,
2985 TU_TO_EXP_TIME(timeout * 2));
2986
2987 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2988 /*
2989 * protect the BA data with RCU to cover a case where our
2990 * internal RX sync mechanism will timeout (not that it's
2991 * supposed to happen) and we will free the session data while
2992 * RX is being processed in parallel
2993 */
2994 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2995 mvm_sta->deflink.sta_id, tid, baid);
2996 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2997 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2998 } else {
2999 baid = mvm_sta->tid_to_baid[tid];
3000
3001 if (mvm->rx_ba_sessions > 0)
3002 /* check that restart flow didn't zero the counter */
3003 mvm->rx_ba_sessions--;
3004 if (!iwl_mvm_has_new_rx_api(mvm))
3005 return 0;
3006
3007 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
3008 return -EINVAL;
3009
3010 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
3011 if (WARN_ON(!baid_data))
3012 return -EINVAL;
3013
3014 /* synchronize all rx queues so we can safely delete */
3015 iwl_mvm_free_reorder(mvm, baid_data);
3016 timer_shutdown_sync(&baid_data->session_timer);
3017 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
3018 kfree_rcu(baid_data, rcu_head);
3019 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
3020
3021 /*
3022 * After we've deleted it, do another queue sync
3023 * so if an IWL_MVM_RXQ_NSSN_SYNC was concurrently
3024 * running it won't find a new session in the old
3025 * BAID. It can find the NULL pointer for the BAID,
3026 * but we must not have it find a different session.
3027 */
3028 iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY,
3029 true, NULL, 0);
3030 }
3031 return 0;
3032
3033out_free:
3034 kfree(baid_data);
3035 return ret;
3036}
3037
3038int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
3039 int tid, u8 queue, bool start)
3040{
3041 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3042 struct iwl_mvm_add_sta_cmd cmd = {};
3043 int ret;
3044 u32 status;
3045
3046 lockdep_assert_held(&mvm->mutex);
3047
3048 if (start) {
3049 mvm_sta->tfd_queue_msk |= BIT(queue);
3050 mvm_sta->tid_disable_agg &= ~BIT(tid);
3051 } else {
3052 /* In DQA-mode the queue isn't removed on agg termination */
3053 mvm_sta->tid_disable_agg |= BIT(tid);
3054 }
3055
3056 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
3057 cmd.sta_id = mvm_sta->deflink.sta_id;
3058 cmd.add_modify = STA_MODE_MODIFY;
3059 if (!iwl_mvm_has_new_tx_api(mvm))
3060 cmd.modify_mask = STA_MODIFY_QUEUES;
3061 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
3062 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
3063 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
3064
3065 status = ADD_STA_SUCCESS;
3066 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
3067 iwl_mvm_add_sta_cmd_size(mvm),
3068 &cmd, &status);
3069 if (ret)
3070 return ret;
3071
3072 switch (status & IWL_ADD_STA_STATUS_MASK) {
3073 case ADD_STA_SUCCESS:
3074 break;
3075 default:
3076 ret = -EIO;
3077 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
3078 start ? "start" : "stopp", status);
3079 break;
3080 }
3081
3082 return ret;
3083}
3084
3085const u8 tid_to_mac80211_ac[] = {
3086 IEEE80211_AC_BE,
3087 IEEE80211_AC_BK,
3088 IEEE80211_AC_BK,
3089 IEEE80211_AC_BE,
3090 IEEE80211_AC_VI,
3091 IEEE80211_AC_VI,
3092 IEEE80211_AC_VO,
3093 IEEE80211_AC_VO,
3094 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
3095};
3096
3097static const u8 tid_to_ucode_ac[] = {
3098 AC_BE,
3099 AC_BK,
3100 AC_BK,
3101 AC_BE,
3102 AC_VI,
3103 AC_VI,
3104 AC_VO,
3105 AC_VO,
3106};
3107
3108int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3109 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3110{
3111 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3112 struct iwl_mvm_tid_data *tid_data;
3113 u16 normalized_ssn;
3114 u16 txq_id;
3115 int ret;
3116
3117 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
3118 return -EINVAL;
3119
3120 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
3121 mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
3122 IWL_ERR(mvm,
3123 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
3124 mvmsta->tid_data[tid].state);
3125 return -ENXIO;
3126 }
3127
3128 lockdep_assert_held(&mvm->mutex);
3129
3130 if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
3131 iwl_mvm_has_new_tx_api(mvm)) {
3132 u8 ac = tid_to_mac80211_ac[tid];
3133
3134 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
3135 if (ret)
3136 return ret;
3137 }
3138
3139 spin_lock_bh(&mvmsta->lock);
3140
3141 /*
3142 * Note the possible cases:
3143 * 1. An enabled TXQ - TXQ needs to become agg'ed
3144 * 2. The TXQ hasn't yet been enabled, so find a free one and mark
3145 * it as reserved
3146 */
3147 txq_id = mvmsta->tid_data[tid].txq_id;
3148 if (txq_id == IWL_MVM_INVALID_QUEUE) {
3149 ret = iwl_mvm_find_free_queue(mvm, mvmsta->deflink.sta_id,
3150 IWL_MVM_DQA_MIN_DATA_QUEUE,
3151 IWL_MVM_DQA_MAX_DATA_QUEUE);
3152 if (ret < 0) {
3153 IWL_ERR(mvm, "Failed to allocate agg queue\n");
3154 goto out;
3155 }
3156
3157 txq_id = ret;
3158
3159 /* TXQ hasn't yet been enabled, so mark it only as reserved */
3160 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
3161 } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
3162 ret = -ENXIO;
3163 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
3164 tid, IWL_MAX_HW_QUEUES - 1);
3165 goto out;
3166
3167 } else if (unlikely(mvm->queue_info[txq_id].status ==
3168 IWL_MVM_QUEUE_SHARED)) {
3169 ret = -ENXIO;
3170 IWL_DEBUG_TX_QUEUES(mvm,
3171 "Can't start tid %d agg on shared queue!\n",
3172 tid);
3173 goto out;
3174 }
3175
3176 IWL_DEBUG_TX_QUEUES(mvm,
3177 "AGG for tid %d will be on queue #%d\n",
3178 tid, txq_id);
3179
3180 tid_data = &mvmsta->tid_data[tid];
3181 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3182 tid_data->txq_id = txq_id;
3183 *ssn = tid_data->ssn;
3184
3185 IWL_DEBUG_TX_QUEUES(mvm,
3186 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
3187 mvmsta->deflink.sta_id, tid, txq_id,
3188 tid_data->ssn,
3189 tid_data->next_reclaimed);
3190
3191 /*
3192 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3193 * to align the wrap around of ssn so we compare relevant values.
3194 */
3195 normalized_ssn = tid_data->ssn;
3196 if (mvm->trans->trans_cfg->gen2)
3197 normalized_ssn &= 0xff;
3198
3199 if (normalized_ssn == tid_data->next_reclaimed) {
3200 tid_data->state = IWL_AGG_STARTING;
3201 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
3202 } else {
3203 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
3204 ret = IEEE80211_AMPDU_TX_START_DELAY_ADDBA;
3205 }
3206
3207out:
3208 spin_unlock_bh(&mvmsta->lock);
3209
3210 return ret;
3211}
3212
3213int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3214 struct ieee80211_sta *sta, u16 tid, u16 buf_size,
3215 bool amsdu)
3216{
3217 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3218 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3219 unsigned int wdg_timeout =
3220 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
3221 int queue, ret;
3222 bool alloc_queue = true;
3223 enum iwl_mvm_queue_status queue_status;
3224 u16 ssn;
3225
3226 struct iwl_trans_txq_scd_cfg cfg = {
3227 .sta_id = mvmsta->deflink.sta_id,
3228 .tid = tid,
3229 .frame_limit = buf_size,
3230 .aggregate = true,
3231 };
3232
3233 /*
3234 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
3235 * manager, so this function should never be called in this case.
3236 */
3237 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
3238 return -EINVAL;
3239
3240 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
3241 != IWL_MAX_TID_COUNT);
3242
3243 spin_lock_bh(&mvmsta->lock);
3244 ssn = tid_data->ssn;
3245 queue = tid_data->txq_id;
3246 tid_data->state = IWL_AGG_ON;
3247 mvmsta->agg_tids |= BIT(tid);
3248 tid_data->ssn = 0xffff;
3249 tid_data->amsdu_in_ampdu_allowed = amsdu;
3250 spin_unlock_bh(&mvmsta->lock);
3251
3252 if (iwl_mvm_has_new_tx_api(mvm)) {
3253 /*
3254 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
3255 * would have failed, so if we are here there is no need to
3256 * allocate a queue.
3257 * However, if aggregation size is different than the default
3258 * size, the scheduler should be reconfigured.
3259 * We cannot do this with the new TX API, so return unsupported
3260 * for now, until it will be offloaded to firmware..
3261 * Note that if SCD default value changes - this condition
3262 * should be updated as well.
3263 */
3264 if (buf_size < IWL_FRAME_LIMIT)
3265 return -EOPNOTSUPP;
3266
3267 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3268 if (ret)
3269 return -EIO;
3270 goto out;
3271 }
3272
3273 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
3274
3275 queue_status = mvm->queue_info[queue].status;
3276
3277 /* Maybe there is no need to even alloc a queue... */
3278 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
3279 alloc_queue = false;
3280
3281 /*
3282 * Only reconfig the SCD for the queue if the window size has
3283 * changed from current (become smaller)
3284 */
3285 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
3286 /*
3287 * If reconfiguring an existing queue, it first must be
3288 * drained
3289 */
3290 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
3291 BIT(queue));
3292 if (ret) {
3293 IWL_ERR(mvm,
3294 "Error draining queue before reconfig\n");
3295 return ret;
3296 }
3297
3298 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
3299 mvmsta->deflink.sta_id, tid,
3300 buf_size, ssn);
3301 if (ret) {
3302 IWL_ERR(mvm,
3303 "Error reconfiguring TXQ #%d\n", queue);
3304 return ret;
3305 }
3306 }
3307
3308 if (alloc_queue)
3309 iwl_mvm_enable_txq(mvm, sta, queue, ssn,
3310 &cfg, wdg_timeout);
3311
3312 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
3313 if (queue_status != IWL_MVM_QUEUE_SHARED) {
3314 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
3315 if (ret)
3316 return -EIO;
3317 }
3318
3319 /* No need to mark as reserved */
3320 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
3321
3322out:
3323 /*
3324 * Even though in theory the peer could have different
3325 * aggregation reorder buffer sizes for different sessions,
3326 * our ucode doesn't allow for that and has a global limit
3327 * for each station. Therefore, use the minimum of all the
3328 * aggregation sessions and our default value.
3329 */
3330 mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize =
3331 min(mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize,
3332 buf_size);
3333 mvmsta->deflink.lq_sta.rs_drv.lq.agg_frame_cnt_limit =
3334 mvmsta->deflink.lq_sta.rs_drv.pers.max_agg_bufsize;
3335
3336 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
3337 sta->addr, tid);
3338
3339 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->deflink.lq_sta.rs_drv.lq);
3340}
3341
3342static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3343 struct iwl_mvm_sta *mvmsta,
3344 struct iwl_mvm_tid_data *tid_data)
3345{
3346 u16 txq_id = tid_data->txq_id;
3347
3348 lockdep_assert_held(&mvm->mutex);
3349
3350 if (iwl_mvm_has_new_tx_api(mvm))
3351 return;
3352
3353 /*
3354 * The TXQ is marked as reserved only if no traffic came through yet
3355 * This means no traffic has been sent on this TID (agg'd or not), so
3356 * we no longer have use for the queue. Since it hasn't even been
3357 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3358 * free.
3359 */
3360 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3361 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3362 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3363 }
3364}
3365
3366int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3367 struct ieee80211_sta *sta, u16 tid)
3368{
3369 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3370 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3371 u16 txq_id;
3372 int err;
3373
3374 /*
3375 * If mac80211 is cleaning its state, then say that we finished since
3376 * our state has been cleared anyway.
3377 */
3378 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3379 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3380 return 0;
3381 }
3382
3383 spin_lock_bh(&mvmsta->lock);
3384
3385 txq_id = tid_data->txq_id;
3386
3387 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3388 mvmsta->deflink.sta_id, tid, txq_id,
3389 tid_data->state);
3390
3391 mvmsta->agg_tids &= ~BIT(tid);
3392
3393 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3394
3395 switch (tid_data->state) {
3396 case IWL_AGG_ON:
3397 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3398
3399 IWL_DEBUG_TX_QUEUES(mvm,
3400 "ssn = %d, next_recl = %d\n",
3401 tid_data->ssn, tid_data->next_reclaimed);
3402
3403 tid_data->ssn = 0xffff;
3404 tid_data->state = IWL_AGG_OFF;
3405 spin_unlock_bh(&mvmsta->lock);
3406
3407 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3408
3409 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3410 return 0;
3411 case IWL_AGG_STARTING:
3412 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3413 /*
3414 * The agg session has been stopped before it was set up. This
3415 * can happen when the AddBA timer times out for example.
3416 */
3417
3418 /* No barriers since we are under mutex */
3419 lockdep_assert_held(&mvm->mutex);
3420
3421 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3422 tid_data->state = IWL_AGG_OFF;
3423 err = 0;
3424 break;
3425 default:
3426 IWL_ERR(mvm,
3427 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3428 mvmsta->deflink.sta_id, tid, tid_data->state);
3429 IWL_ERR(mvm,
3430 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
3431 err = -EINVAL;
3432 }
3433
3434 spin_unlock_bh(&mvmsta->lock);
3435
3436 return err;
3437}
3438
3439int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3440 struct ieee80211_sta *sta, u16 tid)
3441{
3442 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3443 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3444 u16 txq_id;
3445 enum iwl_mvm_agg_state old_state;
3446
3447 /*
3448 * First set the agg state to OFF to avoid calling
3449 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3450 */
3451 spin_lock_bh(&mvmsta->lock);
3452 txq_id = tid_data->txq_id;
3453 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3454 mvmsta->deflink.sta_id, tid, txq_id,
3455 tid_data->state);
3456 old_state = tid_data->state;
3457 tid_data->state = IWL_AGG_OFF;
3458 mvmsta->agg_tids &= ~BIT(tid);
3459 spin_unlock_bh(&mvmsta->lock);
3460
3461 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3462
3463 if (old_state >= IWL_AGG_ON) {
3464 iwl_mvm_drain_sta(mvm, mvmsta, true);
3465
3466 if (iwl_mvm_has_new_tx_api(mvm)) {
3467 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->deflink.sta_id,
3468 BIT(tid)))
3469 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3470 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3471 } else {
3472 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id)))
3473 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3474 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3475 }
3476
3477 iwl_mvm_drain_sta(mvm, mvmsta, false);
3478
3479 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3480 }
3481
3482 return 0;
3483}
3484
3485static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3486{
3487 int i, max = -1, max_offs = -1;
3488
3489 lockdep_assert_held(&mvm->mutex);
3490
3491 /* Pick the unused key offset with the highest 'deleted'
3492 * counter. Every time a key is deleted, all the counters
3493 * are incremented and the one that was just deleted is
3494 * reset to zero. Thus, the highest counter is the one
3495 * that was deleted longest ago. Pick that one.
3496 */
3497 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3498 if (test_bit(i, mvm->fw_key_table))
3499 continue;
3500 if (mvm->fw_key_deleted[i] > max) {
3501 max = mvm->fw_key_deleted[i];
3502 max_offs = i;
3503 }
3504 }
3505
3506 if (max_offs < 0)
3507 return STA_KEY_IDX_INVALID;
3508
3509 return max_offs;
3510}
3511
3512static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3513 struct ieee80211_vif *vif,
3514 struct ieee80211_sta *sta)
3515{
3516 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3517
3518 if (sta)
3519 return iwl_mvm_sta_from_mac80211(sta);
3520
3521 /*
3522 * The device expects GTKs for station interfaces to be
3523 * installed as GTKs for the AP station. If we have no
3524 * station ID, then use AP's station ID.
3525 */
3526 if (vif->type == NL80211_IFTYPE_STATION &&
3527 mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
3528 u8 sta_id = mvmvif->deflink.ap_sta_id;
3529
3530 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3531 lockdep_is_held(&mvm->mutex));
3532
3533 /*
3534 * It is possible that the 'sta' parameter is NULL,
3535 * for example when a GTK is removed - the sta_id will then
3536 * be the AP ID, and no station was passed by mac80211.
3537 */
3538 if (IS_ERR_OR_NULL(sta))
3539 return NULL;
3540
3541 return iwl_mvm_sta_from_mac80211(sta);
3542 }
3543
3544 return NULL;
3545}
3546
3547static int iwl_mvm_pn_cmp(const u8 *pn1, const u8 *pn2, int len)
3548{
3549 int i;
3550
3551 for (i = len - 1; i >= 0; i--) {
3552 if (pn1[i] > pn2[i])
3553 return 1;
3554 if (pn1[i] < pn2[i])
3555 return -1;
3556 }
3557
3558 return 0;
3559}
3560
3561static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3562 u32 sta_id,
3563 struct ieee80211_key_conf *key, bool mcast,
3564 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3565 u8 key_offset, bool mfp)
3566{
3567 union {
3568 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3569 struct iwl_mvm_add_sta_key_cmd cmd;
3570 } u = {};
3571 __le16 key_flags;
3572 int ret;
3573 u32 status;
3574 u16 keyidx;
3575 u64 pn = 0;
3576 int i, size;
3577 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3578 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3579 int api_ver = iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA_KEY,
3580 new_api ? 2 : 1);
3581
3582 if (sta_id == IWL_MVM_INVALID_STA)
3583 return -EINVAL;
3584
3585 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3586 STA_KEY_FLG_KEYID_MSK;
3587 key_flags = cpu_to_le16(keyidx);
3588 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3589
3590 switch (key->cipher) {
3591 case WLAN_CIPHER_SUITE_TKIP:
3592 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3593 if (api_ver >= 2) {
3594 memcpy((void *)&u.cmd.tx_mic_key,
3595 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3596 IWL_MIC_KEY_SIZE);
3597
3598 memcpy((void *)&u.cmd.rx_mic_key,
3599 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3600 IWL_MIC_KEY_SIZE);
3601 pn = atomic64_read(&key->tx_pn);
3602
3603 } else {
3604 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3605 for (i = 0; i < 5; i++)
3606 u.cmd_v1.tkip_rx_ttak[i] =
3607 cpu_to_le16(tkip_p1k[i]);
3608 }
3609 memcpy(u.cmd.common.key, key->key, key->keylen);
3610 break;
3611 case WLAN_CIPHER_SUITE_CCMP:
3612 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3613 memcpy(u.cmd.common.key, key->key, key->keylen);
3614 if (api_ver >= 2)
3615 pn = atomic64_read(&key->tx_pn);
3616 break;
3617 case WLAN_CIPHER_SUITE_WEP104:
3618 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3619 fallthrough;
3620 case WLAN_CIPHER_SUITE_WEP40:
3621 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3622 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3623 break;
3624 case WLAN_CIPHER_SUITE_GCMP_256:
3625 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3626 fallthrough;
3627 case WLAN_CIPHER_SUITE_GCMP:
3628 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3629 memcpy(u.cmd.common.key, key->key, key->keylen);
3630 if (api_ver >= 2)
3631 pn = atomic64_read(&key->tx_pn);
3632 break;
3633 default:
3634 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3635 memcpy(u.cmd.common.key, key->key, key->keylen);
3636 }
3637
3638 if (mcast)
3639 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3640 if (mfp)
3641 key_flags |= cpu_to_le16(STA_KEY_MFP);
3642
3643 u.cmd.common.key_offset = key_offset;
3644 u.cmd.common.key_flags = key_flags;
3645 u.cmd.common.sta_id = sta_id;
3646
3647 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3648 i = 0;
3649 else
3650 i = -1;
3651
3652 for (; i < IEEE80211_NUM_TIDS; i++) {
3653 struct ieee80211_key_seq seq = {};
3654 u8 _rx_pn[IEEE80211_MAX_PN_LEN] = {}, *rx_pn = _rx_pn;
3655 int rx_pn_len = 8;
3656 /* there's a hole at 2/3 in FW format depending on version */
3657 int hole = api_ver >= 3 ? 0 : 2;
3658
3659 ieee80211_get_key_rx_seq(key, i, &seq);
3660
3661 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
3662 rx_pn[0] = seq.tkip.iv16;
3663 rx_pn[1] = seq.tkip.iv16 >> 8;
3664 rx_pn[2 + hole] = seq.tkip.iv32;
3665 rx_pn[3 + hole] = seq.tkip.iv32 >> 8;
3666 rx_pn[4 + hole] = seq.tkip.iv32 >> 16;
3667 rx_pn[5 + hole] = seq.tkip.iv32 >> 24;
3668 } else if (key_flags & cpu_to_le16(STA_KEY_FLG_EXT)) {
3669 rx_pn = seq.hw.seq;
3670 rx_pn_len = seq.hw.seq_len;
3671 } else {
3672 rx_pn[0] = seq.ccmp.pn[0];
3673 rx_pn[1] = seq.ccmp.pn[1];
3674 rx_pn[2 + hole] = seq.ccmp.pn[2];
3675 rx_pn[3 + hole] = seq.ccmp.pn[3];
3676 rx_pn[4 + hole] = seq.ccmp.pn[4];
3677 rx_pn[5 + hole] = seq.ccmp.pn[5];
3678 }
3679
3680 if (iwl_mvm_pn_cmp(rx_pn, (u8 *)&u.cmd.common.rx_secur_seq_cnt,
3681 rx_pn_len) > 0)
3682 memcpy(&u.cmd.common.rx_secur_seq_cnt, rx_pn,
3683 rx_pn_len);
3684 }
3685
3686 if (api_ver >= 2) {
3687 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3688 size = sizeof(u.cmd);
3689 } else {
3690 size = sizeof(u.cmd_v1);
3691 }
3692
3693 status = ADD_STA_SUCCESS;
3694 if (cmd_flags & CMD_ASYNC)
3695 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3696 &u.cmd);
3697 else
3698 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3699 &u.cmd, &status);
3700
3701 switch (status) {
3702 case ADD_STA_SUCCESS:
3703 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3704 break;
3705 default:
3706 ret = -EIO;
3707 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3708 break;
3709 }
3710
3711 return ret;
3712}
3713
3714static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3715 struct ieee80211_key_conf *keyconf,
3716 u8 sta_id, bool remove_key)
3717{
3718 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3719
3720 /* verify the key details match the required command's expectations */
3721 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3722 (keyconf->keyidx != 4 && keyconf->keyidx != 5 &&
3723 keyconf->keyidx != 6 && keyconf->keyidx != 7) ||
3724 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3725 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3726 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3727 return -EINVAL;
3728
3729 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3730 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3731 return -EINVAL;
3732
3733 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3734 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3735
3736 if (remove_key) {
3737 /* This is a valid situation for IGTK */
3738 if (sta_id == IWL_MVM_INVALID_STA)
3739 return 0;
3740
3741 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3742 } else {
3743 struct ieee80211_key_seq seq;
3744 const u8 *pn;
3745
3746 switch (keyconf->cipher) {
3747 case WLAN_CIPHER_SUITE_AES_CMAC:
3748 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3749 break;
3750 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3751 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3752 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3753 break;
3754 default:
3755 return -EINVAL;
3756 }
3757
3758 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3759 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3760 igtk_cmd.ctrl_flags |=
3761 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3762 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3763 pn = seq.aes_cmac.pn;
3764 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3765 ((u64) pn[4] << 8) |
3766 ((u64) pn[3] << 16) |
3767 ((u64) pn[2] << 24) |
3768 ((u64) pn[1] << 32) |
3769 ((u64) pn[0] << 40));
3770 }
3771
3772 IWL_DEBUG_INFO(mvm, "%s %sIGTK (%d) for sta %u\n",
3773 remove_key ? "removing" : "installing",
3774 keyconf->keyidx >= 6 ? "B" : "",
3775 keyconf->keyidx, igtk_cmd.sta_id);
3776
3777 if (!iwl_mvm_has_new_rx_api(mvm)) {
3778 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3779 .ctrl_flags = igtk_cmd.ctrl_flags,
3780 .key_id = igtk_cmd.key_id,
3781 .sta_id = igtk_cmd.sta_id,
3782 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3783 };
3784
3785 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3786 ARRAY_SIZE(igtk_cmd_v1.igtk));
3787 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3788 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3789 }
3790 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3791 sizeof(igtk_cmd), &igtk_cmd);
3792}
3793
3794
3795static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3796 struct ieee80211_vif *vif,
3797 struct ieee80211_sta *sta)
3798{
3799 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3800
3801 if (sta)
3802 return sta->addr;
3803
3804 if (vif->type == NL80211_IFTYPE_STATION &&
3805 mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
3806 u8 sta_id = mvmvif->deflink.ap_sta_id;
3807 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3808 lockdep_is_held(&mvm->mutex));
3809 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
3810 return NULL;
3811
3812 return sta->addr;
3813 }
3814
3815
3816 return NULL;
3817}
3818
3819static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3820 struct ieee80211_vif *vif,
3821 struct ieee80211_sta *sta,
3822 struct ieee80211_key_conf *keyconf,
3823 u8 key_offset,
3824 bool mcast)
3825{
3826 const u8 *addr;
3827 struct ieee80211_key_seq seq;
3828 u16 p1k[5];
3829 u32 sta_id;
3830 bool mfp = false;
3831
3832 if (sta) {
3833 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3834
3835 sta_id = mvm_sta->deflink.sta_id;
3836 mfp = sta->mfp;
3837 } else if (vif->type == NL80211_IFTYPE_AP &&
3838 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3839 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3840
3841 sta_id = mvmvif->deflink.mcast_sta.sta_id;
3842 } else {
3843 IWL_ERR(mvm, "Failed to find station id\n");
3844 return -EINVAL;
3845 }
3846
3847 if (keyconf->cipher == WLAN_CIPHER_SUITE_TKIP) {
3848 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3849 if (!addr) {
3850 IWL_ERR(mvm, "Failed to find mac address\n");
3851 return -EINVAL;
3852 }
3853
3854 /* get phase 1 key from mac80211 */
3855 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3856 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3857
3858 return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3859 seq.tkip.iv32, p1k, 0, key_offset,
3860 mfp);
3861 }
3862
3863 return iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3864 0, NULL, 0, key_offset, mfp);
3865}
3866
3867int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3868 struct ieee80211_vif *vif,
3869 struct ieee80211_sta *sta,
3870 struct ieee80211_key_conf *keyconf,
3871 u8 key_offset)
3872{
3873 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3874 struct iwl_mvm_sta *mvm_sta;
3875 u8 sta_id = IWL_MVM_INVALID_STA;
3876 int ret;
3877 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3878
3879 lockdep_assert_held(&mvm->mutex);
3880
3881 if (vif->type != NL80211_IFTYPE_AP ||
3882 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3883 /* Get the station id from the mvm local station table */
3884 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3885 if (!mvm_sta) {
3886 IWL_ERR(mvm, "Failed to find station\n");
3887 return -EINVAL;
3888 }
3889 sta_id = mvm_sta->deflink.sta_id;
3890
3891 /*
3892 * It is possible that the 'sta' parameter is NULL, and thus
3893 * there is a need to retrieve the sta from the local station
3894 * table.
3895 */
3896 if (!sta) {
3897 sta = rcu_dereference_protected(
3898 mvm->fw_id_to_mac_id[sta_id],
3899 lockdep_is_held(&mvm->mutex));
3900 if (IS_ERR_OR_NULL(sta)) {
3901 IWL_ERR(mvm, "Invalid station id\n");
3902 return -EINVAL;
3903 }
3904 }
3905
3906 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3907 return -EINVAL;
3908 } else {
3909 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3910
3911 sta_id = mvmvif->deflink.mcast_sta.sta_id;
3912 }
3913
3914 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3915 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3916 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3917 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3918 goto end;
3919 }
3920
3921 /* If the key_offset is not pre-assigned, we need to find a
3922 * new offset to use. In normal cases, the offset is not
3923 * pre-assigned, but during HW_RESTART we want to reuse the
3924 * same indices, so we pass them when this function is called.
3925 *
3926 * In D3 entry, we need to hardcoded the indices (because the
3927 * firmware hardcodes the PTK offset to 0). In this case, we
3928 * need to make sure we don't overwrite the hw_key_idx in the
3929 * keyconf structure, because otherwise we cannot configure
3930 * the original ones back when resuming.
3931 */
3932 if (key_offset == STA_KEY_IDX_INVALID) {
3933 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3934 if (key_offset == STA_KEY_IDX_INVALID)
3935 return -ENOSPC;
3936 keyconf->hw_key_idx = key_offset;
3937 }
3938
3939 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3940 if (ret)
3941 goto end;
3942
3943 /*
3944 * For WEP, the same key is used for multicast and unicast. Upload it
3945 * again, using the same key offset, and now pointing the other one
3946 * to the same key slot (offset).
3947 * If this fails, remove the original as well.
3948 */
3949 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3950 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3951 sta) {
3952 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3953 key_offset, !mcast);
3954 if (ret) {
3955 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3956 goto end;
3957 }
3958 }
3959
3960 __set_bit(key_offset, mvm->fw_key_table);
3961
3962end:
3963 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3964 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3965 sta ? sta->addr : zero_addr, ret);
3966 return ret;
3967}
3968
3969int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3970 struct ieee80211_vif *vif,
3971 struct ieee80211_sta *sta,
3972 struct ieee80211_key_conf *keyconf)
3973{
3974 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3975 struct iwl_mvm_sta *mvm_sta;
3976 u8 sta_id = IWL_MVM_INVALID_STA;
3977 int ret, i;
3978
3979 lockdep_assert_held(&mvm->mutex);
3980
3981 /* Get the station from the mvm local station table */
3982 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3983 if (mvm_sta)
3984 sta_id = mvm_sta->deflink.sta_id;
3985 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3986 sta_id = iwl_mvm_vif_from_mac80211(vif)->deflink.mcast_sta.sta_id;
3987
3988
3989 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3990 keyconf->keyidx, sta_id);
3991
3992 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3993 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3994 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3995 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3996
3997 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3998 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3999 keyconf->hw_key_idx);
4000 return -ENOENT;
4001 }
4002
4003 /* track which key was deleted last */
4004 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
4005 if (mvm->fw_key_deleted[i] < U8_MAX)
4006 mvm->fw_key_deleted[i]++;
4007 }
4008 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
4009
4010 if (sta && !mvm_sta) {
4011 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
4012 return 0;
4013 }
4014
4015 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
4016 if (ret)
4017 return ret;
4018
4019 /* delete WEP key twice to get rid of (now useless) offset */
4020 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4021 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
4022 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
4023
4024 return ret;
4025}
4026
4027void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
4028 struct ieee80211_vif *vif,
4029 struct ieee80211_key_conf *keyconf,
4030 struct ieee80211_sta *sta, u32 iv32,
4031 u16 *phase1key)
4032{
4033 struct iwl_mvm_sta *mvm_sta;
4034 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
4035 bool mfp = sta ? sta->mfp : false;
4036
4037 rcu_read_lock();
4038
4039 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
4040 if (WARN_ON_ONCE(!mvm_sta))
4041 goto unlock;
4042 iwl_mvm_send_sta_key(mvm, mvm_sta->deflink.sta_id, keyconf, mcast,
4043 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
4044 mfp);
4045
4046 unlock:
4047 rcu_read_unlock();
4048}
4049
4050void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
4051 struct ieee80211_sta *sta)
4052{
4053 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4054 struct iwl_mvm_add_sta_cmd cmd = {
4055 .add_modify = STA_MODE_MODIFY,
4056 .sta_id = mvmsta->deflink.sta_id,
4057 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
4058 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4059 };
4060 int ret;
4061
4062 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4063 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4064 if (ret)
4065 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4066}
4067
4068void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
4069 struct ieee80211_sta *sta,
4070 enum ieee80211_frame_release_type reason,
4071 u16 cnt, u16 tids, bool more_data,
4072 bool single_sta_queue)
4073{
4074 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4075 struct iwl_mvm_add_sta_cmd cmd = {
4076 .add_modify = STA_MODE_MODIFY,
4077 .sta_id = mvmsta->deflink.sta_id,
4078 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
4079 .sleep_tx_count = cpu_to_le16(cnt),
4080 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4081 };
4082 int tid, ret;
4083 unsigned long _tids = tids;
4084
4085 /* convert TIDs to ACs - we don't support TSPEC so that's OK
4086 * Note that this field is reserved and unused by firmware not
4087 * supporting GO uAPSD, so it's safe to always do this.
4088 */
4089 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
4090 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
4091
4092 /* If we're releasing frames from aggregation or dqa queues then check
4093 * if all the queues that we're releasing frames from, combined, have:
4094 * - more frames than the service period, in which case more_data
4095 * needs to be set
4096 * - fewer than 'cnt' frames, in which case we need to adjust the
4097 * firmware command (but do that unconditionally)
4098 */
4099 if (single_sta_queue) {
4100 int remaining = cnt;
4101 int sleep_tx_count;
4102
4103 spin_lock_bh(&mvmsta->lock);
4104 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
4105 struct iwl_mvm_tid_data *tid_data;
4106 u16 n_queued;
4107
4108 tid_data = &mvmsta->tid_data[tid];
4109
4110 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
4111 if (n_queued > remaining) {
4112 more_data = true;
4113 remaining = 0;
4114 break;
4115 }
4116 remaining -= n_queued;
4117 }
4118 sleep_tx_count = cnt - remaining;
4119 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
4120 mvmsta->sleep_tx_count = sleep_tx_count;
4121 spin_unlock_bh(&mvmsta->lock);
4122
4123 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
4124 if (WARN_ON(cnt - remaining == 0)) {
4125 ieee80211_sta_eosp(sta);
4126 return;
4127 }
4128 }
4129
4130 /* Note: this is ignored by firmware not supporting GO uAPSD */
4131 if (more_data)
4132 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
4133
4134 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
4135 mvmsta->next_status_eosp = true;
4136 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
4137 } else {
4138 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
4139 }
4140
4141 /* block the Tx queues until the FW updated the sleep Tx count */
4142 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
4143 CMD_ASYNC | CMD_BLOCK_TXQS,
4144 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4145 if (ret)
4146 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4147}
4148
4149void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
4150 struct iwl_rx_cmd_buffer *rxb)
4151{
4152 struct iwl_rx_packet *pkt = rxb_addr(rxb);
4153 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
4154 struct ieee80211_sta *sta;
4155 u32 sta_id = le32_to_cpu(notif->sta_id);
4156
4157 if (WARN_ON_ONCE(sta_id >= mvm->fw->ucode_capa.num_stations))
4158 return;
4159
4160 rcu_read_lock();
4161 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
4162 if (!IS_ERR_OR_NULL(sta))
4163 ieee80211_sta_eosp(sta);
4164 rcu_read_unlock();
4165}
4166
4167void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
4168 struct iwl_mvm_sta *mvmsta,
4169 bool disable)
4170{
4171 struct iwl_mvm_add_sta_cmd cmd = {
4172 .add_modify = STA_MODE_MODIFY,
4173 .sta_id = mvmsta->deflink.sta_id,
4174 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4175 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4176 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
4177 };
4178 int ret;
4179
4180 if (mvm->mld_api_is_used) {
4181 if (!iwl_mvm_has_no_host_disable_tx(mvm))
4182 iwl_mvm_mld_sta_modify_disable_tx(mvm, mvmsta, disable);
4183 return;
4184 }
4185
4186 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4187 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4188 if (ret)
4189 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4190}
4191
4192void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
4193 struct ieee80211_sta *sta,
4194 bool disable)
4195{
4196 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4197
4198 if (mvm->mld_api_is_used) {
4199 if (!iwl_mvm_has_no_host_disable_tx(mvm))
4200 iwl_mvm_mld_sta_modify_disable_tx_ap(mvm, sta, disable);
4201 return;
4202 }
4203
4204 spin_lock_bh(&mvm_sta->lock);
4205
4206 if (mvm_sta->disable_tx == disable) {
4207 spin_unlock_bh(&mvm_sta->lock);
4208 return;
4209 }
4210
4211 mvm_sta->disable_tx = disable;
4212
4213 /*
4214 * If sta PS state is handled by mac80211, tell it to start/stop
4215 * queuing tx for this station.
4216 */
4217 if (!ieee80211_hw_check(mvm->hw, AP_LINK_PS))
4218 ieee80211_sta_block_awake(mvm->hw, sta, disable);
4219
4220 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
4221
4222 spin_unlock_bh(&mvm_sta->lock);
4223}
4224
4225static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
4226 struct iwl_mvm_vif *mvmvif,
4227 struct iwl_mvm_int_sta *sta,
4228 bool disable)
4229{
4230 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
4231 struct iwl_mvm_add_sta_cmd cmd = {
4232 .add_modify = STA_MODE_MODIFY,
4233 .sta_id = sta->sta_id,
4234 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
4235 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
4236 .mac_id_n_color = cpu_to_le32(id),
4237 };
4238 int ret;
4239
4240 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
4241 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
4242 if (ret)
4243 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
4244}
4245
4246void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
4247 struct iwl_mvm_vif *mvmvif,
4248 bool disable)
4249{
4250 struct ieee80211_sta *sta;
4251 struct iwl_mvm_sta *mvm_sta;
4252 int i;
4253
4254 if (mvm->mld_api_is_used) {
4255 if (!iwl_mvm_has_no_host_disable_tx(mvm))
4256 iwl_mvm_mld_modify_all_sta_disable_tx(mvm, mvmvif,
4257 disable);
4258 return;
4259 }
4260
4261 rcu_read_lock();
4262
4263 /* Block/unblock all the stations of the given mvmvif */
4264 for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4265 sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
4266 if (IS_ERR_OR_NULL(sta))
4267 continue;
4268
4269 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4270 if (mvm_sta->mac_id_n_color !=
4271 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
4272 continue;
4273
4274 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
4275 }
4276
4277 rcu_read_unlock();
4278
4279 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
4280 return;
4281
4282 /* Need to block/unblock also multicast station */
4283 if (mvmvif->deflink.mcast_sta.sta_id != IWL_MVM_INVALID_STA)
4284 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4285 &mvmvif->deflink.mcast_sta,
4286 disable);
4287
4288 /*
4289 * Only unblock the broadcast station (FW blocks it for immediate
4290 * quiet, not the driver)
4291 */
4292 if (!disable && mvmvif->deflink.bcast_sta.sta_id != IWL_MVM_INVALID_STA)
4293 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
4294 &mvmvif->deflink.bcast_sta,
4295 disable);
4296}
4297
4298void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4299{
4300 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4301 struct iwl_mvm_sta *mvmsta;
4302
4303 rcu_read_lock();
4304
4305 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->deflink.ap_sta_id);
4306
4307 if (mvmsta)
4308 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
4309
4310 rcu_read_unlock();
4311}
4312
4313u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
4314{
4315 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
4316
4317 /*
4318 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
4319 * to align the wrap around of ssn so we compare relevant values.
4320 */
4321 if (mvm->trans->trans_cfg->gen2)
4322 sn &= 0xff;
4323
4324 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
4325}
4326
4327int iwl_mvm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4328 struct iwl_mvm_int_sta *sta, u8 *addr, u32 cipher,
4329 u8 *key, u32 key_len)
4330{
4331 int ret;
4332 u16 queue;
4333 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4334 struct ieee80211_key_conf *keyconf;
4335 unsigned int wdg_timeout =
4336 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
4337 bool mld = iwl_mvm_has_mld_api(mvm->fw);
4338 u32 type = mld ? STATION_TYPE_PEER : IWL_STA_LINK;
4339
4340 ret = iwl_mvm_allocate_int_sta(mvm, sta, 0,
4341 NL80211_IFTYPE_UNSPECIFIED, type);
4342 if (ret)
4343 return ret;
4344
4345 if (mld)
4346 ret = iwl_mvm_mld_add_int_sta_with_queue(mvm, sta, addr,
4347 mvmvif->deflink.fw_link_id,
4348 &queue,
4349 IWL_MAX_TID_COUNT,
4350 &wdg_timeout);
4351 else
4352 ret = iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id,
4353 mvmvif->color, addr, sta,
4354 &queue,
4355 IWL_MVM_TX_FIFO_BE);
4356 if (ret)
4357 goto out;
4358
4359 keyconf = kzalloc(sizeof(*keyconf) + key_len, GFP_KERNEL);
4360 if (!keyconf) {
4361 ret = -ENOBUFS;
4362 goto out;
4363 }
4364
4365 keyconf->cipher = cipher;
4366 memcpy(keyconf->key, key, key_len);
4367 keyconf->keylen = key_len;
4368 keyconf->flags = IEEE80211_KEY_FLAG_PAIRWISE;
4369
4370 if (mld) {
4371 /* The MFP flag is set according to the station mfp field. Since
4372 * we don't have a station, set it manually.
4373 */
4374 u32 key_flags =
4375 iwl_mvm_get_sec_flags(mvm, vif, NULL, keyconf) |
4376 IWL_SEC_KEY_FLAG_MFP;
4377 u32 sta_mask = BIT(sta->sta_id);
4378
4379 ret = iwl_mvm_mld_send_key(mvm, sta_mask, key_flags, keyconf);
4380 } else {
4381 ret = iwl_mvm_send_sta_key(mvm, sta->sta_id, keyconf, false,
4382 0, NULL, 0, 0, true);
4383 }
4384
4385 kfree(keyconf);
4386 return 0;
4387out:
4388 iwl_mvm_dealloc_int_sta(mvm, sta);
4389 return ret;
4390}
4391
4392void iwl_mvm_cancel_channel_switch(struct iwl_mvm *mvm,
4393 struct ieee80211_vif *vif,
4394 u32 id)
4395{
4396 struct iwl_cancel_channel_switch_cmd cancel_channel_switch_cmd = {
4397 .id = cpu_to_le32(id),
4398 };
4399 int ret;
4400
4401 ret = iwl_mvm_send_cmd_pdu(mvm,
4402 WIDE_ID(MAC_CONF_GROUP, CANCEL_CHANNEL_SWITCH_CMD),
4403 CMD_ASYNC,
4404 sizeof(cancel_channel_switch_cmd),
4405 &cancel_channel_switch_cmd);
4406 if (ret)
4407 IWL_ERR(mvm, "Failed to cancel the channel switch\n");
4408}
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 *
8 * Copyright(c) 2012 - 2015, 2018 - 2020 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
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 * The full GNU General Public License is included in this distribution
22 * in the file called COPYING.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <linuxwifi@intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 * BSD LICENSE
29 *
30 * Copyright(c) 2012 - 2015, 2018 - 2020 Intel Corporation. All rights reserved.
31 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
32 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 *
39 * * Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * * Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in
43 * the documentation and/or other materials provided with the
44 * distribution.
45 * * Neither the name Intel Corporation nor the names of its
46 * contributors may be used to endorse or promote products derived
47 * from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
50 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
51 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
52 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
53 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 *
61 *****************************************************************************/
62#include <net/mac80211.h>
63
64#include "mvm.h"
65#include "sta.h"
66#include "rs.h"
67
68/*
69 * New version of ADD_STA_sta command added new fields at the end of the
70 * structure, so sending the size of the relevant API's structure is enough to
71 * support both API versions.
72 */
73static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
74{
75 if (iwl_mvm_has_new_rx_api(mvm) ||
76 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
77 return sizeof(struct iwl_mvm_add_sta_cmd);
78 else
79 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
80}
81
82static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
83 enum nl80211_iftype iftype)
84{
85 int sta_id;
86 u32 reserved_ids = 0;
87
88 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
89 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
90
91 lockdep_assert_held(&mvm->mutex);
92
93 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
94 if (iftype != NL80211_IFTYPE_STATION)
95 reserved_ids = BIT(0);
96
97 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
98 for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
99 if (BIT(sta_id) & reserved_ids)
100 continue;
101
102 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
103 lockdep_is_held(&mvm->mutex)))
104 return sta_id;
105 }
106 return IWL_MVM_INVALID_STA;
107}
108
109/* send station add/update command to firmware */
110int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
111 bool update, unsigned int flags)
112{
113 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
114 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
115 .sta_id = mvm_sta->sta_id,
116 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
117 .add_modify = update ? 1 : 0,
118 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
119 STA_FLG_MIMO_EN_MSK |
120 STA_FLG_RTS_MIMO_PROT),
121 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
122 };
123 int ret;
124 u32 status;
125 u32 agg_size = 0, mpdu_dens = 0;
126
127 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
128 add_sta_cmd.station_type = mvm_sta->sta_type;
129
130 if (!update || (flags & STA_MODIFY_QUEUES)) {
131 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
132
133 if (!iwl_mvm_has_new_tx_api(mvm)) {
134 add_sta_cmd.tfd_queue_msk =
135 cpu_to_le32(mvm_sta->tfd_queue_msk);
136
137 if (flags & STA_MODIFY_QUEUES)
138 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
139 } else {
140 WARN_ON(flags & STA_MODIFY_QUEUES);
141 }
142 }
143
144 switch (sta->bandwidth) {
145 case IEEE80211_STA_RX_BW_160:
146 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
147 /* fall through */
148 case IEEE80211_STA_RX_BW_80:
149 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
150 /* fall through */
151 case IEEE80211_STA_RX_BW_40:
152 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
153 /* fall through */
154 case IEEE80211_STA_RX_BW_20:
155 if (sta->ht_cap.ht_supported)
156 add_sta_cmd.station_flags |=
157 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
158 break;
159 }
160
161 switch (sta->rx_nss) {
162 case 1:
163 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
164 break;
165 case 2:
166 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
167 break;
168 case 3 ... 8:
169 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
170 break;
171 }
172
173 switch (sta->smps_mode) {
174 case IEEE80211_SMPS_AUTOMATIC:
175 case IEEE80211_SMPS_NUM_MODES:
176 WARN_ON(1);
177 break;
178 case IEEE80211_SMPS_STATIC:
179 /* override NSS */
180 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
181 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
182 break;
183 case IEEE80211_SMPS_DYNAMIC:
184 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
185 break;
186 case IEEE80211_SMPS_OFF:
187 /* nothing */
188 break;
189 }
190
191 if (sta->ht_cap.ht_supported) {
192 add_sta_cmd.station_flags_msk |=
193 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
194 STA_FLG_AGG_MPDU_DENS_MSK);
195
196 mpdu_dens = sta->ht_cap.ampdu_density;
197 }
198
199 if (sta->vht_cap.vht_supported) {
200 agg_size = sta->vht_cap.cap &
201 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
202 agg_size >>=
203 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
204 } else if (sta->ht_cap.ht_supported) {
205 agg_size = sta->ht_cap.ampdu_factor;
206 }
207
208 add_sta_cmd.station_flags |=
209 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
210 add_sta_cmd.station_flags |=
211 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
212 if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
213 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
214
215 if (sta->wme) {
216 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
217
218 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
219 add_sta_cmd.uapsd_acs |= BIT(AC_BK);
220 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
221 add_sta_cmd.uapsd_acs |= BIT(AC_BE);
222 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
223 add_sta_cmd.uapsd_acs |= BIT(AC_VI);
224 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
225 add_sta_cmd.uapsd_acs |= BIT(AC_VO);
226 add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
227 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
228 }
229
230 status = ADD_STA_SUCCESS;
231 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
232 iwl_mvm_add_sta_cmd_size(mvm),
233 &add_sta_cmd, &status);
234 if (ret)
235 return ret;
236
237 switch (status & IWL_ADD_STA_STATUS_MASK) {
238 case ADD_STA_SUCCESS:
239 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
240 break;
241 default:
242 ret = -EIO;
243 IWL_ERR(mvm, "ADD_STA failed\n");
244 break;
245 }
246
247 return ret;
248}
249
250static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
251{
252 struct iwl_mvm_baid_data *data =
253 from_timer(data, t, session_timer);
254 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
255 struct iwl_mvm_baid_data *ba_data;
256 struct ieee80211_sta *sta;
257 struct iwl_mvm_sta *mvm_sta;
258 unsigned long timeout;
259
260 rcu_read_lock();
261
262 ba_data = rcu_dereference(*rcu_ptr);
263
264 if (WARN_ON(!ba_data))
265 goto unlock;
266
267 if (!ba_data->timeout)
268 goto unlock;
269
270 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
271 if (time_is_after_jiffies(timeout)) {
272 mod_timer(&ba_data->session_timer, timeout);
273 goto unlock;
274 }
275
276 /* Timer expired */
277 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
278
279 /*
280 * sta should be valid unless the following happens:
281 * The firmware asserts which triggers a reconfig flow, but
282 * the reconfig fails before we set the pointer to sta into
283 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
284 * A-MDPU and hence the timer continues to run. Then, the
285 * timer expires and sta is NULL.
286 */
287 if (!sta)
288 goto unlock;
289
290 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
291 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
292 sta->addr, ba_data->tid);
293unlock:
294 rcu_read_unlock();
295}
296
297/* Disable aggregations for a bitmap of TIDs for a given station */
298static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
299 unsigned long disable_agg_tids,
300 bool remove_queue)
301{
302 struct iwl_mvm_add_sta_cmd cmd = {};
303 struct ieee80211_sta *sta;
304 struct iwl_mvm_sta *mvmsta;
305 u32 status;
306 u8 sta_id;
307
308 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
309 return -EINVAL;
310
311 sta_id = mvm->queue_info[queue].ra_sta_id;
312
313 rcu_read_lock();
314
315 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
316
317 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
318 rcu_read_unlock();
319 return -EINVAL;
320 }
321
322 mvmsta = iwl_mvm_sta_from_mac80211(sta);
323
324 mvmsta->tid_disable_agg |= disable_agg_tids;
325
326 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
327 cmd.sta_id = mvmsta->sta_id;
328 cmd.add_modify = STA_MODE_MODIFY;
329 cmd.modify_mask = STA_MODIFY_QUEUES;
330 if (disable_agg_tids)
331 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
332 if (remove_queue)
333 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
334 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
335 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
336
337 rcu_read_unlock();
338
339 /* Notify FW of queue removal from the STA queues */
340 status = ADD_STA_SUCCESS;
341 return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
342 iwl_mvm_add_sta_cmd_size(mvm),
343 &cmd, &status);
344}
345
346static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
347 int queue, u8 tid, u8 flags)
348{
349 struct iwl_scd_txq_cfg_cmd cmd = {
350 .scd_queue = queue,
351 .action = SCD_CFG_DISABLE_QUEUE,
352 };
353 int ret;
354
355 if (iwl_mvm_has_new_tx_api(mvm)) {
356 iwl_trans_txq_free(mvm->trans, queue);
357
358 return 0;
359 }
360
361 if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
362 return 0;
363
364 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
365
366 cmd.action = mvm->queue_info[queue].tid_bitmap ?
367 SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
368 if (cmd.action == SCD_CFG_DISABLE_QUEUE)
369 mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
370
371 IWL_DEBUG_TX_QUEUES(mvm,
372 "Disabling TXQ #%d tids=0x%x\n",
373 queue,
374 mvm->queue_info[queue].tid_bitmap);
375
376 /* If the queue is still enabled - nothing left to do in this func */
377 if (cmd.action == SCD_CFG_ENABLE_QUEUE)
378 return 0;
379
380 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
381 cmd.tid = mvm->queue_info[queue].txq_tid;
382
383 /* Make sure queue info is correct even though we overwrite it */
384 WARN(mvm->queue_info[queue].tid_bitmap,
385 "TXQ #%d info out-of-sync - tids=0x%x\n",
386 queue, mvm->queue_info[queue].tid_bitmap);
387
388 /* If we are here - the queue is freed and we can zero out these vals */
389 mvm->queue_info[queue].tid_bitmap = 0;
390
391 if (sta) {
392 struct iwl_mvm_txq *mvmtxq =
393 iwl_mvm_txq_from_tid(sta, tid);
394
395 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
396 }
397
398 /* Regardless if this is a reserved TXQ for a STA - mark it as false */
399 mvm->queue_info[queue].reserved = false;
400
401 iwl_trans_txq_disable(mvm->trans, queue, false);
402 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
403 sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
404
405 if (ret)
406 IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
407 queue, ret);
408 return ret;
409}
410
411static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
412{
413 struct ieee80211_sta *sta;
414 struct iwl_mvm_sta *mvmsta;
415 unsigned long tid_bitmap;
416 unsigned long agg_tids = 0;
417 u8 sta_id;
418 int tid;
419
420 lockdep_assert_held(&mvm->mutex);
421
422 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
423 return -EINVAL;
424
425 sta_id = mvm->queue_info[queue].ra_sta_id;
426 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
427
428 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
429 lockdep_is_held(&mvm->mutex));
430
431 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
432 return -EINVAL;
433
434 mvmsta = iwl_mvm_sta_from_mac80211(sta);
435
436 spin_lock_bh(&mvmsta->lock);
437 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
438 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
439 agg_tids |= BIT(tid);
440 }
441 spin_unlock_bh(&mvmsta->lock);
442
443 return agg_tids;
444}
445
446/*
447 * Remove a queue from a station's resources.
448 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
449 * doesn't disable the queue
450 */
451static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
452{
453 struct ieee80211_sta *sta;
454 struct iwl_mvm_sta *mvmsta;
455 unsigned long tid_bitmap;
456 unsigned long disable_agg_tids = 0;
457 u8 sta_id;
458 int tid;
459
460 lockdep_assert_held(&mvm->mutex);
461
462 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
463 return -EINVAL;
464
465 sta_id = mvm->queue_info[queue].ra_sta_id;
466 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
467
468 rcu_read_lock();
469
470 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
471
472 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
473 rcu_read_unlock();
474 return 0;
475 }
476
477 mvmsta = iwl_mvm_sta_from_mac80211(sta);
478
479 spin_lock_bh(&mvmsta->lock);
480 /* Unmap MAC queues and TIDs from this queue */
481 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
482 struct iwl_mvm_txq *mvmtxq =
483 iwl_mvm_txq_from_tid(sta, tid);
484
485 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
486 disable_agg_tids |= BIT(tid);
487 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
488
489 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
490 }
491
492 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
493 spin_unlock_bh(&mvmsta->lock);
494
495 rcu_read_unlock();
496
497 /*
498 * The TX path may have been using this TXQ_ID from the tid_data,
499 * so make sure it's no longer running so that we can safely reuse
500 * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
501 * above, but nothing guarantees we've stopped using them. Thus,
502 * without this, we could get to iwl_mvm_disable_txq() and remove
503 * the queue while still sending frames to it.
504 */
505 synchronize_net();
506
507 return disable_agg_tids;
508}
509
510static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
511 struct ieee80211_sta *old_sta,
512 u8 new_sta_id)
513{
514 struct iwl_mvm_sta *mvmsta;
515 u8 sta_id, tid;
516 unsigned long disable_agg_tids = 0;
517 bool same_sta;
518 int ret;
519
520 lockdep_assert_held(&mvm->mutex);
521
522 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
523 return -EINVAL;
524
525 sta_id = mvm->queue_info[queue].ra_sta_id;
526 tid = mvm->queue_info[queue].txq_tid;
527
528 same_sta = sta_id == new_sta_id;
529
530 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
531 if (WARN_ON(!mvmsta))
532 return -EINVAL;
533
534 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
535 /* Disable the queue */
536 if (disable_agg_tids)
537 iwl_mvm_invalidate_sta_queue(mvm, queue,
538 disable_agg_tids, false);
539
540 ret = iwl_mvm_disable_txq(mvm, old_sta, queue, tid, 0);
541 if (ret) {
542 IWL_ERR(mvm,
543 "Failed to free inactive queue %d (ret=%d)\n",
544 queue, ret);
545
546 return ret;
547 }
548
549 /* If TXQ is allocated to another STA, update removal in FW */
550 if (!same_sta)
551 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
552
553 return 0;
554}
555
556static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
557 unsigned long tfd_queue_mask, u8 ac)
558{
559 int queue = 0;
560 u8 ac_to_queue[IEEE80211_NUM_ACS];
561 int i;
562
563 /*
564 * This protects us against grabbing a queue that's being reconfigured
565 * by the inactivity checker.
566 */
567 lockdep_assert_held(&mvm->mutex);
568
569 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
570 return -EINVAL;
571
572 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
573
574 /* See what ACs the existing queues for this STA have */
575 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
576 /* Only DATA queues can be shared */
577 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
578 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
579 continue;
580
581 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
582 }
583
584 /*
585 * The queue to share is chosen only from DATA queues as follows (in
586 * descending priority):
587 * 1. An AC_BE queue
588 * 2. Same AC queue
589 * 3. Highest AC queue that is lower than new AC
590 * 4. Any existing AC (there always is at least 1 DATA queue)
591 */
592
593 /* Priority 1: An AC_BE queue */
594 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
595 queue = ac_to_queue[IEEE80211_AC_BE];
596 /* Priority 2: Same AC queue */
597 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
598 queue = ac_to_queue[ac];
599 /* Priority 3a: If new AC is VO and VI exists - use VI */
600 else if (ac == IEEE80211_AC_VO &&
601 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
602 queue = ac_to_queue[IEEE80211_AC_VI];
603 /* Priority 3b: No BE so only AC less than the new one is BK */
604 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
605 queue = ac_to_queue[IEEE80211_AC_BK];
606 /* Priority 4a: No BE nor BK - use VI if exists */
607 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
608 queue = ac_to_queue[IEEE80211_AC_VI];
609 /* Priority 4b: No BE, BK nor VI - use VO if exists */
610 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
611 queue = ac_to_queue[IEEE80211_AC_VO];
612
613 /* Make sure queue found (or not) is legal */
614 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
615 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
616 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
617 IWL_ERR(mvm, "No DATA queues available to share\n");
618 return -ENOSPC;
619 }
620
621 return queue;
622}
623
624/*
625 * If a given queue has a higher AC than the TID stream that is being compared
626 * to, the queue needs to be redirected to the lower AC. This function does that
627 * in such a case, otherwise - if no redirection required - it does nothing,
628 * unless the %force param is true.
629 */
630static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
631 int ac, int ssn, unsigned int wdg_timeout,
632 bool force, struct iwl_mvm_txq *txq)
633{
634 struct iwl_scd_txq_cfg_cmd cmd = {
635 .scd_queue = queue,
636 .action = SCD_CFG_DISABLE_QUEUE,
637 };
638 bool shared_queue;
639 int ret;
640
641 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
642 return -EINVAL;
643
644 /*
645 * If the AC is lower than current one - FIFO needs to be redirected to
646 * the lowest one of the streams in the queue. Check if this is needed
647 * here.
648 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
649 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
650 * we need to check if the numerical value of X is LARGER than of Y.
651 */
652 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
653 IWL_DEBUG_TX_QUEUES(mvm,
654 "No redirection needed on TXQ #%d\n",
655 queue);
656 return 0;
657 }
658
659 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
660 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
661 cmd.tid = mvm->queue_info[queue].txq_tid;
662 shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
663
664 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
665 queue, iwl_mvm_ac_to_tx_fifo[ac]);
666
667 /* Stop the queue and wait for it to empty */
668 txq->stopped = true;
669
670 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
671 if (ret) {
672 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
673 queue);
674 ret = -EIO;
675 goto out;
676 }
677
678 /* Before redirecting the queue we need to de-activate it */
679 iwl_trans_txq_disable(mvm->trans, queue, false);
680 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
681 if (ret)
682 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
683 ret);
684
685 /* Make sure the SCD wrptr is correctly set before reconfiguring */
686 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
687
688 /* Update the TID "owner" of the queue */
689 mvm->queue_info[queue].txq_tid = tid;
690
691 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
692
693 /* Redirect to lower AC */
694 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
695 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
696
697 /* Update AC marking of the queue */
698 mvm->queue_info[queue].mac80211_ac = ac;
699
700 /*
701 * Mark queue as shared in transport if shared
702 * Note this has to be done after queue enablement because enablement
703 * can also set this value, and there is no indication there to shared
704 * queues
705 */
706 if (shared_queue)
707 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
708
709out:
710 /* Continue using the queue */
711 txq->stopped = false;
712
713 return ret;
714}
715
716static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
717 u8 minq, u8 maxq)
718{
719 int i;
720
721 lockdep_assert_held(&mvm->mutex);
722
723 if (WARN(maxq >= mvm->trans->trans_cfg->base_params->num_of_queues,
724 "max queue %d >= num_of_queues (%d)", maxq,
725 mvm->trans->trans_cfg->base_params->num_of_queues))
726 maxq = mvm->trans->trans_cfg->base_params->num_of_queues - 1;
727
728 /* This should not be hit with new TX path */
729 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
730 return -ENOSPC;
731
732 /* Start by looking for a free queue */
733 for (i = minq; i <= maxq; i++)
734 if (mvm->queue_info[i].tid_bitmap == 0 &&
735 mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
736 return i;
737
738 return -ENOSPC;
739}
740
741static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
742 u8 sta_id, u8 tid, unsigned int timeout)
743{
744 int queue, size = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
745 mvm->trans->cfg->min_256_ba_txq_size);
746
747 if (tid == IWL_MAX_TID_COUNT) {
748 tid = IWL_MGMT_TID;
749 size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
750 mvm->trans->cfg->min_txq_size);
751 }
752
753 do {
754 __le16 enable = cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE);
755
756 queue = iwl_trans_txq_alloc(mvm->trans, enable,
757 sta_id, tid, SCD_QUEUE_CFG,
758 size, timeout);
759
760 if (queue < 0)
761 IWL_DEBUG_TX_QUEUES(mvm,
762 "Failed allocating TXQ of size %d for sta %d tid %d, ret: %d\n",
763 size, sta_id, tid, queue);
764 size /= 2;
765 } while (queue < 0 && size >= 16);
766
767 if (queue < 0)
768 return queue;
769
770 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
771 queue, sta_id, tid);
772
773 IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d\n", queue);
774
775 return queue;
776}
777
778static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
779 struct ieee80211_sta *sta, u8 ac,
780 int tid)
781{
782 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
783 struct iwl_mvm_txq *mvmtxq =
784 iwl_mvm_txq_from_tid(sta, tid);
785 unsigned int wdg_timeout =
786 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
787 int queue = -1;
788
789 lockdep_assert_held(&mvm->mutex);
790
791 IWL_DEBUG_TX_QUEUES(mvm,
792 "Allocating queue for sta %d on tid %d\n",
793 mvmsta->sta_id, tid);
794 queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
795 if (queue < 0)
796 return queue;
797
798 mvmtxq->txq_id = queue;
799 mvm->tvqm_info[queue].txq_tid = tid;
800 mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
801
802 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
803
804 spin_lock_bh(&mvmsta->lock);
805 mvmsta->tid_data[tid].txq_id = queue;
806 spin_unlock_bh(&mvmsta->lock);
807
808 return 0;
809}
810
811static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
812 struct ieee80211_sta *sta,
813 int queue, u8 sta_id, u8 tid)
814{
815 bool enable_queue = true;
816
817 /* Make sure this TID isn't already enabled */
818 if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
819 IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
820 queue, tid);
821 return false;
822 }
823
824 /* Update mappings and refcounts */
825 if (mvm->queue_info[queue].tid_bitmap)
826 enable_queue = false;
827
828 mvm->queue_info[queue].tid_bitmap |= BIT(tid);
829 mvm->queue_info[queue].ra_sta_id = sta_id;
830
831 if (enable_queue) {
832 if (tid != IWL_MAX_TID_COUNT)
833 mvm->queue_info[queue].mac80211_ac =
834 tid_to_mac80211_ac[tid];
835 else
836 mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
837
838 mvm->queue_info[queue].txq_tid = tid;
839 }
840
841 if (sta) {
842 struct iwl_mvm_txq *mvmtxq =
843 iwl_mvm_txq_from_tid(sta, tid);
844
845 mvmtxq->txq_id = queue;
846 }
847
848 IWL_DEBUG_TX_QUEUES(mvm,
849 "Enabling TXQ #%d tids=0x%x\n",
850 queue, mvm->queue_info[queue].tid_bitmap);
851
852 return enable_queue;
853}
854
855static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
856 int queue, u16 ssn,
857 const struct iwl_trans_txq_scd_cfg *cfg,
858 unsigned int wdg_timeout)
859{
860 struct iwl_scd_txq_cfg_cmd cmd = {
861 .scd_queue = queue,
862 .action = SCD_CFG_ENABLE_QUEUE,
863 .window = cfg->frame_limit,
864 .sta_id = cfg->sta_id,
865 .ssn = cpu_to_le16(ssn),
866 .tx_fifo = cfg->fifo,
867 .aggregate = cfg->aggregate,
868 .tid = cfg->tid,
869 };
870 bool inc_ssn;
871
872 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
873 return false;
874
875 /* Send the enabling command if we need to */
876 if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
877 return false;
878
879 inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
880 NULL, wdg_timeout);
881 if (inc_ssn)
882 le16_add_cpu(&cmd.ssn, 1);
883
884 WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
885 "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
886
887 return inc_ssn;
888}
889
890static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
891{
892 struct iwl_scd_txq_cfg_cmd cmd = {
893 .scd_queue = queue,
894 .action = SCD_CFG_UPDATE_QUEUE_TID,
895 };
896 int tid;
897 unsigned long tid_bitmap;
898 int ret;
899
900 lockdep_assert_held(&mvm->mutex);
901
902 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
903 return;
904
905 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
906
907 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
908 return;
909
910 /* Find any TID for queue */
911 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
912 cmd.tid = tid;
913 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
914
915 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
916 if (ret) {
917 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
918 queue, ret);
919 return;
920 }
921
922 mvm->queue_info[queue].txq_tid = tid;
923 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
924 queue, tid);
925}
926
927static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
928{
929 struct ieee80211_sta *sta;
930 struct iwl_mvm_sta *mvmsta;
931 u8 sta_id;
932 int tid = -1;
933 unsigned long tid_bitmap;
934 unsigned int wdg_timeout;
935 int ssn;
936 int ret = true;
937
938 /* queue sharing is disabled on new TX path */
939 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
940 return;
941
942 lockdep_assert_held(&mvm->mutex);
943
944 sta_id = mvm->queue_info[queue].ra_sta_id;
945 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
946
947 /* Find TID for queue, and make sure it is the only one on the queue */
948 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
949 if (tid_bitmap != BIT(tid)) {
950 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
951 queue, tid_bitmap);
952 return;
953 }
954
955 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
956 tid);
957
958 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
959 lockdep_is_held(&mvm->mutex));
960
961 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
962 return;
963
964 mvmsta = iwl_mvm_sta_from_mac80211(sta);
965 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
966
967 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
968
969 ret = iwl_mvm_redirect_queue(mvm, queue, tid,
970 tid_to_mac80211_ac[tid], ssn,
971 wdg_timeout, true,
972 iwl_mvm_txq_from_tid(sta, tid));
973 if (ret) {
974 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
975 return;
976 }
977
978 /* If aggs should be turned back on - do it */
979 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
980 struct iwl_mvm_add_sta_cmd cmd = {0};
981
982 mvmsta->tid_disable_agg &= ~BIT(tid);
983
984 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
985 cmd.sta_id = mvmsta->sta_id;
986 cmd.add_modify = STA_MODE_MODIFY;
987 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
988 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
989 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
990
991 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
992 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
993 if (!ret) {
994 IWL_DEBUG_TX_QUEUES(mvm,
995 "TXQ #%d is now aggregated again\n",
996 queue);
997
998 /* Mark queue intenally as aggregating again */
999 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1000 }
1001 }
1002
1003 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1004}
1005
1006/*
1007 * Remove inactive TIDs of a given queue.
1008 * If all queue TIDs are inactive - mark the queue as inactive
1009 * If only some the queue TIDs are inactive - unmap them from the queue
1010 *
1011 * Returns %true if all TIDs were removed and the queue could be reused.
1012 */
1013static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1014 struct iwl_mvm_sta *mvmsta, int queue,
1015 unsigned long tid_bitmap,
1016 unsigned long *unshare_queues,
1017 unsigned long *changetid_queues)
1018{
1019 int tid;
1020
1021 lockdep_assert_held(&mvmsta->lock);
1022 lockdep_assert_held(&mvm->mutex);
1023
1024 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1025 return false;
1026
1027 /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1028 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1029 /* If some TFDs are still queued - don't mark TID as inactive */
1030 if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1031 tid_bitmap &= ~BIT(tid);
1032
1033 /* Don't mark as inactive any TID that has an active BA */
1034 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1035 tid_bitmap &= ~BIT(tid);
1036 }
1037
1038 /* If all TIDs in the queue are inactive - return it can be reused */
1039 if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1040 IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1041 return true;
1042 }
1043
1044 /*
1045 * If we are here, this is a shared queue and not all TIDs timed-out.
1046 * Remove the ones that did.
1047 */
1048 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1049 u16 tid_bitmap;
1050
1051 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1052 mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1053
1054 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1055
1056 /*
1057 * We need to take into account a situation in which a TXQ was
1058 * allocated to TID x, and then turned shared by adding TIDs y
1059 * and z. If TID x becomes inactive and is removed from the TXQ,
1060 * ownership must be given to one of the remaining TIDs.
1061 * This is mainly because if TID x continues - a new queue can't
1062 * be allocated for it as long as it is an owner of another TXQ.
1063 *
1064 * Mark this queue in the right bitmap, we'll send the command
1065 * to the firmware later.
1066 */
1067 if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1068 set_bit(queue, changetid_queues);
1069
1070 IWL_DEBUG_TX_QUEUES(mvm,
1071 "Removing inactive TID %d from shared Q:%d\n",
1072 tid, queue);
1073 }
1074
1075 IWL_DEBUG_TX_QUEUES(mvm,
1076 "TXQ #%d left with tid bitmap 0x%x\n", queue,
1077 mvm->queue_info[queue].tid_bitmap);
1078
1079 /*
1080 * There may be different TIDs with the same mac queues, so make
1081 * sure all TIDs have existing corresponding mac queues enabled
1082 */
1083 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1084
1085 /* If the queue is marked as shared - "unshare" it */
1086 if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1087 mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1088 IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1089 queue);
1090 set_bit(queue, unshare_queues);
1091 }
1092
1093 return false;
1094}
1095
1096/*
1097 * Check for inactivity - this includes checking if any queue
1098 * can be unshared and finding one (and only one) that can be
1099 * reused.
1100 * This function is also invoked as a sort of clean-up task,
1101 * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1102 *
1103 * Returns the queue number, or -ENOSPC.
1104 */
1105static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1106{
1107 unsigned long now = jiffies;
1108 unsigned long unshare_queues = 0;
1109 unsigned long changetid_queues = 0;
1110 int i, ret, free_queue = -ENOSPC;
1111 struct ieee80211_sta *queue_owner = NULL;
1112
1113 lockdep_assert_held(&mvm->mutex);
1114
1115 if (iwl_mvm_has_new_tx_api(mvm))
1116 return -ENOSPC;
1117
1118 rcu_read_lock();
1119
1120 /* we skip the CMD queue below by starting at 1 */
1121 BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1122
1123 for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1124 struct ieee80211_sta *sta;
1125 struct iwl_mvm_sta *mvmsta;
1126 u8 sta_id;
1127 int tid;
1128 unsigned long inactive_tid_bitmap = 0;
1129 unsigned long queue_tid_bitmap;
1130
1131 queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1132 if (!queue_tid_bitmap)
1133 continue;
1134
1135 /* If TXQ isn't in active use anyway - nothing to do here... */
1136 if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1137 mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1138 continue;
1139
1140 /* Check to see if there are inactive TIDs on this queue */
1141 for_each_set_bit(tid, &queue_tid_bitmap,
1142 IWL_MAX_TID_COUNT + 1) {
1143 if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1144 IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1145 continue;
1146
1147 inactive_tid_bitmap |= BIT(tid);
1148 }
1149
1150 /* If all TIDs are active - finish check on this queue */
1151 if (!inactive_tid_bitmap)
1152 continue;
1153
1154 /*
1155 * If we are here - the queue hadn't been served recently and is
1156 * in use
1157 */
1158
1159 sta_id = mvm->queue_info[i].ra_sta_id;
1160 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1161
1162 /*
1163 * If the STA doesn't exist anymore, it isn't an error. It could
1164 * be that it was removed since getting the queues, and in this
1165 * case it should've inactivated its queues anyway.
1166 */
1167 if (IS_ERR_OR_NULL(sta))
1168 continue;
1169
1170 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1171
1172 spin_lock_bh(&mvmsta->lock);
1173 ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1174 inactive_tid_bitmap,
1175 &unshare_queues,
1176 &changetid_queues);
1177 if (ret && free_queue < 0) {
1178 queue_owner = sta;
1179 free_queue = i;
1180 }
1181 /* only unlock sta lock - we still need the queue info lock */
1182 spin_unlock_bh(&mvmsta->lock);
1183 }
1184
1185
1186 /* Reconfigure queues requiring reconfiguation */
1187 for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1188 iwl_mvm_unshare_queue(mvm, i);
1189 for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1190 iwl_mvm_change_queue_tid(mvm, i);
1191
1192 rcu_read_unlock();
1193
1194 if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1195 ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1196 alloc_for_sta);
1197 if (ret)
1198 return ret;
1199 }
1200
1201 return free_queue;
1202}
1203
1204static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1205 struct ieee80211_sta *sta, u8 ac, int tid)
1206{
1207 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1208 struct iwl_trans_txq_scd_cfg cfg = {
1209 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1210 .sta_id = mvmsta->sta_id,
1211 .tid = tid,
1212 .frame_limit = IWL_FRAME_LIMIT,
1213 };
1214 unsigned int wdg_timeout =
1215 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1216 int queue = -1;
1217 unsigned long disable_agg_tids = 0;
1218 enum iwl_mvm_agg_state queue_state;
1219 bool shared_queue = false, inc_ssn;
1220 int ssn;
1221 unsigned long tfd_queue_mask;
1222 int ret;
1223
1224 lockdep_assert_held(&mvm->mutex);
1225
1226 if (iwl_mvm_has_new_tx_api(mvm))
1227 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1228
1229 spin_lock_bh(&mvmsta->lock);
1230 tfd_queue_mask = mvmsta->tfd_queue_msk;
1231 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1232 spin_unlock_bh(&mvmsta->lock);
1233
1234 if (tid == IWL_MAX_TID_COUNT) {
1235 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1236 IWL_MVM_DQA_MIN_MGMT_QUEUE,
1237 IWL_MVM_DQA_MAX_MGMT_QUEUE);
1238 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1239 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1240 queue);
1241
1242 /* If no such queue is found, we'll use a DATA queue instead */
1243 }
1244
1245 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1246 (mvm->queue_info[mvmsta->reserved_queue].status ==
1247 IWL_MVM_QUEUE_RESERVED)) {
1248 queue = mvmsta->reserved_queue;
1249 mvm->queue_info[queue].reserved = true;
1250 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1251 }
1252
1253 if (queue < 0)
1254 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1255 IWL_MVM_DQA_MIN_DATA_QUEUE,
1256 IWL_MVM_DQA_MAX_DATA_QUEUE);
1257 if (queue < 0) {
1258 /* try harder - perhaps kill an inactive queue */
1259 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1260 }
1261
1262 /* No free queue - we'll have to share */
1263 if (queue <= 0) {
1264 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1265 if (queue > 0) {
1266 shared_queue = true;
1267 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1268 }
1269 }
1270
1271 /*
1272 * Mark TXQ as ready, even though it hasn't been fully configured yet,
1273 * to make sure no one else takes it.
1274 * This will allow avoiding re-acquiring the lock at the end of the
1275 * configuration. On error we'll mark it back as free.
1276 */
1277 if (queue > 0 && !shared_queue)
1278 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1279
1280 /* This shouldn't happen - out of queues */
1281 if (WARN_ON(queue <= 0)) {
1282 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1283 tid, cfg.sta_id);
1284 return queue;
1285 }
1286
1287 /*
1288 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1289 * but for configuring the SCD to send A-MPDUs we need to mark the queue
1290 * as aggregatable.
1291 * Mark all DATA queues as allowing to be aggregated at some point
1292 */
1293 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1294 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1295
1296 IWL_DEBUG_TX_QUEUES(mvm,
1297 "Allocating %squeue #%d to sta %d on tid %d\n",
1298 shared_queue ? "shared " : "", queue,
1299 mvmsta->sta_id, tid);
1300
1301 if (shared_queue) {
1302 /* Disable any open aggs on this queue */
1303 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1304
1305 if (disable_agg_tids) {
1306 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1307 queue);
1308 iwl_mvm_invalidate_sta_queue(mvm, queue,
1309 disable_agg_tids, false);
1310 }
1311 }
1312
1313 inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1314
1315 /*
1316 * Mark queue as shared in transport if shared
1317 * Note this has to be done after queue enablement because enablement
1318 * can also set this value, and there is no indication there to shared
1319 * queues
1320 */
1321 if (shared_queue)
1322 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1323
1324 spin_lock_bh(&mvmsta->lock);
1325 /*
1326 * This looks racy, but it is not. We have only one packet for
1327 * this ra/tid in our Tx path since we stop the Qdisc when we
1328 * need to allocate a new TFD queue.
1329 */
1330 if (inc_ssn) {
1331 mvmsta->tid_data[tid].seq_number += 0x10;
1332 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1333 }
1334 mvmsta->tid_data[tid].txq_id = queue;
1335 mvmsta->tfd_queue_msk |= BIT(queue);
1336 queue_state = mvmsta->tid_data[tid].state;
1337
1338 if (mvmsta->reserved_queue == queue)
1339 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1340 spin_unlock_bh(&mvmsta->lock);
1341
1342 if (!shared_queue) {
1343 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1344 if (ret)
1345 goto out_err;
1346
1347 /* If we need to re-enable aggregations... */
1348 if (queue_state == IWL_AGG_ON) {
1349 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1350 if (ret)
1351 goto out_err;
1352 }
1353 } else {
1354 /* Redirect queue, if needed */
1355 ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1356 wdg_timeout, false,
1357 iwl_mvm_txq_from_tid(sta, tid));
1358 if (ret)
1359 goto out_err;
1360 }
1361
1362 return 0;
1363
1364out_err:
1365 iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1366
1367 return ret;
1368}
1369
1370void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1371{
1372 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1373 add_stream_wk);
1374
1375 mutex_lock(&mvm->mutex);
1376
1377 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1378
1379 while (!list_empty(&mvm->add_stream_txqs)) {
1380 struct iwl_mvm_txq *mvmtxq;
1381 struct ieee80211_txq *txq;
1382 u8 tid;
1383
1384 mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1385 struct iwl_mvm_txq, list);
1386
1387 txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1388 drv_priv);
1389 tid = txq->tid;
1390 if (tid == IEEE80211_NUM_TIDS)
1391 tid = IWL_MAX_TID_COUNT;
1392
1393 /*
1394 * We can't really do much here, but if this fails we can't
1395 * transmit anyway - so just don't transmit the frame etc.
1396 * and let them back up ... we've tried our best to allocate
1397 * a queue in the function itself.
1398 */
1399 if (iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid)) {
1400 list_del_init(&mvmtxq->list);
1401 continue;
1402 }
1403
1404 list_del_init(&mvmtxq->list);
1405 local_bh_disable();
1406 iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1407 local_bh_enable();
1408 }
1409
1410 mutex_unlock(&mvm->mutex);
1411}
1412
1413static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1414 struct ieee80211_sta *sta,
1415 enum nl80211_iftype vif_type)
1416{
1417 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1418 int queue;
1419
1420 /* queue reserving is disabled on new TX path */
1421 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1422 return 0;
1423
1424 /* run the general cleanup/unsharing of queues */
1425 iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1426
1427 /* Make sure we have free resources for this STA */
1428 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1429 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1430 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1431 IWL_MVM_QUEUE_FREE))
1432 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1433 else
1434 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1435 IWL_MVM_DQA_MIN_DATA_QUEUE,
1436 IWL_MVM_DQA_MAX_DATA_QUEUE);
1437 if (queue < 0) {
1438 /* try again - this time kick out a queue if needed */
1439 queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1440 if (queue < 0) {
1441 IWL_ERR(mvm, "No available queues for new station\n");
1442 return -ENOSPC;
1443 }
1444 }
1445 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1446
1447 mvmsta->reserved_queue = queue;
1448
1449 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1450 queue, mvmsta->sta_id);
1451
1452 return 0;
1453}
1454
1455/*
1456 * In DQA mode, after a HW restart the queues should be allocated as before, in
1457 * order to avoid race conditions when there are shared queues. This function
1458 * does the re-mapping and queue allocation.
1459 *
1460 * Note that re-enabling aggregations isn't done in this function.
1461 */
1462static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1463 struct ieee80211_sta *sta)
1464{
1465 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1466 unsigned int wdg =
1467 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1468 int i;
1469 struct iwl_trans_txq_scd_cfg cfg = {
1470 .sta_id = mvm_sta->sta_id,
1471 .frame_limit = IWL_FRAME_LIMIT,
1472 };
1473
1474 /* Make sure reserved queue is still marked as such (if allocated) */
1475 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1476 mvm->queue_info[mvm_sta->reserved_queue].status =
1477 IWL_MVM_QUEUE_RESERVED;
1478
1479 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1480 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1481 int txq_id = tid_data->txq_id;
1482 int ac;
1483
1484 if (txq_id == IWL_MVM_INVALID_QUEUE)
1485 continue;
1486
1487 ac = tid_to_mac80211_ac[i];
1488
1489 if (iwl_mvm_has_new_tx_api(mvm)) {
1490 IWL_DEBUG_TX_QUEUES(mvm,
1491 "Re-mapping sta %d tid %d\n",
1492 mvm_sta->sta_id, i);
1493 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1494 i, wdg);
1495 /*
1496 * on failures, just set it to IWL_MVM_INVALID_QUEUE
1497 * to try again later, we have no other good way of
1498 * failing here
1499 */
1500 if (txq_id < 0)
1501 txq_id = IWL_MVM_INVALID_QUEUE;
1502 tid_data->txq_id = txq_id;
1503
1504 /*
1505 * Since we don't set the seq number after reset, and HW
1506 * sets it now, FW reset will cause the seq num to start
1507 * at 0 again, so driver will need to update it
1508 * internally as well, so it keeps in sync with real val
1509 */
1510 tid_data->seq_number = 0;
1511 } else {
1512 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1513
1514 cfg.tid = i;
1515 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1516 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1517 txq_id ==
1518 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1519
1520 IWL_DEBUG_TX_QUEUES(mvm,
1521 "Re-mapping sta %d tid %d to queue %d\n",
1522 mvm_sta->sta_id, i, txq_id);
1523
1524 iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1525 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1526 }
1527 }
1528}
1529
1530static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1531 struct iwl_mvm_int_sta *sta,
1532 const u8 *addr,
1533 u16 mac_id, u16 color)
1534{
1535 struct iwl_mvm_add_sta_cmd cmd;
1536 int ret;
1537 u32 status = ADD_STA_SUCCESS;
1538
1539 lockdep_assert_held(&mvm->mutex);
1540
1541 memset(&cmd, 0, sizeof(cmd));
1542 cmd.sta_id = sta->sta_id;
1543 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1544 color));
1545 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1546 cmd.station_type = sta->type;
1547
1548 if (!iwl_mvm_has_new_tx_api(mvm))
1549 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1550 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1551
1552 if (addr)
1553 memcpy(cmd.addr, addr, ETH_ALEN);
1554
1555 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1556 iwl_mvm_add_sta_cmd_size(mvm),
1557 &cmd, &status);
1558 if (ret)
1559 return ret;
1560
1561 switch (status & IWL_ADD_STA_STATUS_MASK) {
1562 case ADD_STA_SUCCESS:
1563 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1564 return 0;
1565 default:
1566 ret = -EIO;
1567 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1568 status);
1569 break;
1570 }
1571 return ret;
1572}
1573
1574int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1575 struct ieee80211_vif *vif,
1576 struct ieee80211_sta *sta)
1577{
1578 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1579 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1580 struct iwl_mvm_rxq_dup_data *dup_data;
1581 int i, ret, sta_id;
1582 bool sta_update = false;
1583 unsigned int sta_flags = 0;
1584
1585 lockdep_assert_held(&mvm->mutex);
1586
1587 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1588 sta_id = iwl_mvm_find_free_sta_id(mvm,
1589 ieee80211_vif_type_p2p(vif));
1590 else
1591 sta_id = mvm_sta->sta_id;
1592
1593 if (sta_id == IWL_MVM_INVALID_STA)
1594 return -ENOSPC;
1595
1596 spin_lock_init(&mvm_sta->lock);
1597
1598 /* if this is a HW restart re-alloc existing queues */
1599 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1600 struct iwl_mvm_int_sta tmp_sta = {
1601 .sta_id = sta_id,
1602 .type = mvm_sta->sta_type,
1603 };
1604
1605 /*
1606 * First add an empty station since allocating
1607 * a queue requires a valid station
1608 */
1609 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1610 mvmvif->id, mvmvif->color);
1611 if (ret)
1612 goto err;
1613
1614 iwl_mvm_realloc_queues_after_restart(mvm, sta);
1615 sta_update = true;
1616 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1617 goto update_fw;
1618 }
1619
1620 mvm_sta->sta_id = sta_id;
1621 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1622 mvmvif->color);
1623 mvm_sta->vif = vif;
1624 if (!mvm->trans->trans_cfg->gen2)
1625 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1626 else
1627 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1628 mvm_sta->tx_protection = 0;
1629 mvm_sta->tt_tx_protection = false;
1630 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1631
1632 /* HW restart, don't assume the memory has been zeroed */
1633 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1634 mvm_sta->tfd_queue_msk = 0;
1635
1636 /* for HW restart - reset everything but the sequence number */
1637 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1638 u16 seq = mvm_sta->tid_data[i].seq_number;
1639 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1640 mvm_sta->tid_data[i].seq_number = seq;
1641
1642 /*
1643 * Mark all queues for this STA as unallocated and defer TX
1644 * frames until the queue is allocated
1645 */
1646 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1647 }
1648
1649 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1650 struct iwl_mvm_txq *mvmtxq =
1651 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1652
1653 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1654 INIT_LIST_HEAD(&mvmtxq->list);
1655 atomic_set(&mvmtxq->tx_request, 0);
1656 }
1657
1658 mvm_sta->agg_tids = 0;
1659
1660 if (iwl_mvm_has_new_rx_api(mvm) &&
1661 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1662 int q;
1663
1664 dup_data = kcalloc(mvm->trans->num_rx_queues,
1665 sizeof(*dup_data), GFP_KERNEL);
1666 if (!dup_data)
1667 return -ENOMEM;
1668 /*
1669 * Initialize all the last_seq values to 0xffff which can never
1670 * compare equal to the frame's seq_ctrl in the check in
1671 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1672 * number and fragmented packets don't reach that function.
1673 *
1674 * This thus allows receiving a packet with seqno 0 and the
1675 * retry bit set as the very first packet on a new TID.
1676 */
1677 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1678 memset(dup_data[q].last_seq, 0xff,
1679 sizeof(dup_data[q].last_seq));
1680 mvm_sta->dup_data = dup_data;
1681 }
1682
1683 if (!iwl_mvm_has_new_tx_api(mvm)) {
1684 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1685 ieee80211_vif_type_p2p(vif));
1686 if (ret)
1687 goto err;
1688 }
1689
1690 /*
1691 * if rs is registered with mac80211, then "add station" will be handled
1692 * via the corresponding ops, otherwise need to notify rate scaling here
1693 */
1694 if (iwl_mvm_has_tlc_offload(mvm))
1695 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1696 else
1697 spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
1698
1699 iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1700
1701update_fw:
1702 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1703 if (ret)
1704 goto err;
1705
1706 if (vif->type == NL80211_IFTYPE_STATION) {
1707 if (!sta->tdls) {
1708 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1709 mvmvif->ap_sta_id = sta_id;
1710 } else {
1711 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1712 }
1713 }
1714
1715 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1716
1717 return 0;
1718
1719err:
1720 return ret;
1721}
1722
1723int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1724 bool drain)
1725{
1726 struct iwl_mvm_add_sta_cmd cmd = {};
1727 int ret;
1728 u32 status;
1729
1730 lockdep_assert_held(&mvm->mutex);
1731
1732 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1733 cmd.sta_id = mvmsta->sta_id;
1734 cmd.add_modify = STA_MODE_MODIFY;
1735 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1736 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1737
1738 status = ADD_STA_SUCCESS;
1739 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1740 iwl_mvm_add_sta_cmd_size(mvm),
1741 &cmd, &status);
1742 if (ret)
1743 return ret;
1744
1745 switch (status & IWL_ADD_STA_STATUS_MASK) {
1746 case ADD_STA_SUCCESS:
1747 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1748 mvmsta->sta_id);
1749 break;
1750 default:
1751 ret = -EIO;
1752 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1753 mvmsta->sta_id);
1754 break;
1755 }
1756
1757 return ret;
1758}
1759
1760/*
1761 * Remove a station from the FW table. Before sending the command to remove
1762 * the station validate that the station is indeed known to the driver (sanity
1763 * only).
1764 */
1765static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1766{
1767 struct ieee80211_sta *sta;
1768 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1769 .sta_id = sta_id,
1770 };
1771 int ret;
1772
1773 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1774 lockdep_is_held(&mvm->mutex));
1775
1776 /* Note: internal stations are marked as error values */
1777 if (!sta) {
1778 IWL_ERR(mvm, "Invalid station id\n");
1779 return -EINVAL;
1780 }
1781
1782 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1783 sizeof(rm_sta_cmd), &rm_sta_cmd);
1784 if (ret) {
1785 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1786 return ret;
1787 }
1788
1789 return 0;
1790}
1791
1792static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1793 struct ieee80211_vif *vif,
1794 struct ieee80211_sta *sta)
1795{
1796 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1797 int i;
1798
1799 lockdep_assert_held(&mvm->mutex);
1800
1801 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1802 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1803 continue;
1804
1805 iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1806 0);
1807 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1808 }
1809
1810 for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1811 struct iwl_mvm_txq *mvmtxq =
1812 iwl_mvm_txq_from_mac80211(sta->txq[i]);
1813
1814 mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1815 }
1816}
1817
1818int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1819 struct iwl_mvm_sta *mvm_sta)
1820{
1821 int i;
1822
1823 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1824 u16 txq_id;
1825 int ret;
1826
1827 spin_lock_bh(&mvm_sta->lock);
1828 txq_id = mvm_sta->tid_data[i].txq_id;
1829 spin_unlock_bh(&mvm_sta->lock);
1830
1831 if (txq_id == IWL_MVM_INVALID_QUEUE)
1832 continue;
1833
1834 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1835 if (ret)
1836 return ret;
1837 }
1838
1839 return 0;
1840}
1841
1842int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1843 struct ieee80211_vif *vif,
1844 struct ieee80211_sta *sta)
1845{
1846 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1847 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1848 u8 sta_id = mvm_sta->sta_id;
1849 int ret;
1850
1851 lockdep_assert_held(&mvm->mutex);
1852
1853 if (iwl_mvm_has_new_rx_api(mvm))
1854 kfree(mvm_sta->dup_data);
1855
1856 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1857 if (ret)
1858 return ret;
1859
1860 /* flush its queues here since we are freeing mvm_sta */
1861 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1862 if (ret)
1863 return ret;
1864 if (iwl_mvm_has_new_tx_api(mvm)) {
1865 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1866 } else {
1867 u32 q_mask = mvm_sta->tfd_queue_msk;
1868
1869 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1870 q_mask);
1871 }
1872 if (ret)
1873 return ret;
1874
1875 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1876
1877 iwl_mvm_disable_sta_queues(mvm, vif, sta);
1878
1879 /* If there is a TXQ still marked as reserved - free it */
1880 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1881 u8 reserved_txq = mvm_sta->reserved_queue;
1882 enum iwl_mvm_queue_status *status;
1883
1884 /*
1885 * If no traffic has gone through the reserved TXQ - it
1886 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1887 * should be manually marked as free again
1888 */
1889 status = &mvm->queue_info[reserved_txq].status;
1890 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1891 (*status != IWL_MVM_QUEUE_FREE),
1892 "sta_id %d reserved txq %d status %d",
1893 sta_id, reserved_txq, *status))
1894 return -EINVAL;
1895
1896 *status = IWL_MVM_QUEUE_FREE;
1897 }
1898
1899 if (vif->type == NL80211_IFTYPE_STATION &&
1900 mvmvif->ap_sta_id == sta_id) {
1901 /* if associated - we can't remove the AP STA now */
1902 if (vif->bss_conf.assoc)
1903 return ret;
1904
1905 /* unassoc - go ahead - remove the AP STA now */
1906 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1907 }
1908
1909 /*
1910 * This shouldn't happen - the TDLS channel switch should be canceled
1911 * before the STA is removed.
1912 */
1913 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1914 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1915 cancel_delayed_work(&mvm->tdls_cs.dwork);
1916 }
1917
1918 /*
1919 * Make sure that the tx response code sees the station as -EBUSY and
1920 * calls the drain worker.
1921 */
1922 spin_lock_bh(&mvm_sta->lock);
1923 spin_unlock_bh(&mvm_sta->lock);
1924
1925 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1926 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1927
1928 return ret;
1929}
1930
1931int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1932 struct ieee80211_vif *vif,
1933 u8 sta_id)
1934{
1935 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1936
1937 lockdep_assert_held(&mvm->mutex);
1938
1939 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1940 return ret;
1941}
1942
1943int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1944 struct iwl_mvm_int_sta *sta,
1945 u32 qmask, enum nl80211_iftype iftype,
1946 enum iwl_sta_type type)
1947{
1948 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1949 sta->sta_id == IWL_MVM_INVALID_STA) {
1950 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1951 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1952 return -ENOSPC;
1953 }
1954
1955 sta->tfd_queue_msk = qmask;
1956 sta->type = type;
1957
1958 /* put a non-NULL value so iterating over the stations won't stop */
1959 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1960 return 0;
1961}
1962
1963void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1964{
1965 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1966 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1967 sta->sta_id = IWL_MVM_INVALID_STA;
1968}
1969
1970static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
1971 u8 sta_id, u8 fifo)
1972{
1973 unsigned int wdg_timeout =
1974 mvm->trans->trans_cfg->base_params->wd_timeout;
1975 struct iwl_trans_txq_scd_cfg cfg = {
1976 .fifo = fifo,
1977 .sta_id = sta_id,
1978 .tid = IWL_MAX_TID_COUNT,
1979 .aggregate = false,
1980 .frame_limit = IWL_FRAME_LIMIT,
1981 };
1982
1983 WARN_ON(iwl_mvm_has_new_tx_api(mvm));
1984
1985 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
1986}
1987
1988static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
1989{
1990 unsigned int wdg_timeout =
1991 mvm->trans->trans_cfg->base_params->wd_timeout;
1992
1993 WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
1994
1995 return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
1996 wdg_timeout);
1997}
1998
1999static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
2000 int maccolor,
2001 struct iwl_mvm_int_sta *sta,
2002 u16 *queue, int fifo)
2003{
2004 int ret;
2005
2006 /* Map queue to fifo - needs to happen before adding station */
2007 if (!iwl_mvm_has_new_tx_api(mvm))
2008 iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2009
2010 ret = iwl_mvm_add_int_sta_common(mvm, sta, NULL, macidx, maccolor);
2011 if (ret) {
2012 if (!iwl_mvm_has_new_tx_api(mvm))
2013 iwl_mvm_disable_txq(mvm, NULL, *queue,
2014 IWL_MAX_TID_COUNT, 0);
2015 return ret;
2016 }
2017
2018 /*
2019 * For 22000 firmware and on we cannot add queue to a station unknown
2020 * to firmware so enable queue here - after the station was added
2021 */
2022 if (iwl_mvm_has_new_tx_api(mvm)) {
2023 int txq;
2024
2025 txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2026 if (txq < 0) {
2027 iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2028 return txq;
2029 }
2030
2031 *queue = txq;
2032 }
2033
2034 return 0;
2035}
2036
2037int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
2038{
2039 int ret;
2040
2041 lockdep_assert_held(&mvm->mutex);
2042
2043 /* Allocate aux station and assign to it the aux queue */
2044 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2045 NL80211_IFTYPE_UNSPECIFIED,
2046 IWL_STA_AUX_ACTIVITY);
2047 if (ret)
2048 return ret;
2049
2050 ret = iwl_mvm_add_int_sta_with_queue(mvm, MAC_INDEX_AUX, 0,
2051 &mvm->aux_sta, &mvm->aux_queue,
2052 IWL_MVM_TX_FIFO_MCAST);
2053 if (ret) {
2054 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2055 return ret;
2056 }
2057
2058 return 0;
2059}
2060
2061int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2062{
2063 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2064
2065 lockdep_assert_held(&mvm->mutex);
2066
2067 return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2068 &mvm->snif_sta, &mvm->snif_queue,
2069 IWL_MVM_TX_FIFO_BE);
2070}
2071
2072int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2073{
2074 int ret;
2075
2076 lockdep_assert_held(&mvm->mutex);
2077
2078 iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2079 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2080 if (ret)
2081 IWL_WARN(mvm, "Failed sending remove station\n");
2082
2083 return ret;
2084}
2085
2086int iwl_mvm_rm_aux_sta(struct iwl_mvm *mvm)
2087{
2088 int ret;
2089
2090 lockdep_assert_held(&mvm->mutex);
2091
2092 iwl_mvm_disable_txq(mvm, NULL, mvm->aux_queue, IWL_MAX_TID_COUNT, 0);
2093 ret = iwl_mvm_rm_sta_common(mvm, mvm->aux_sta.sta_id);
2094 if (ret)
2095 IWL_WARN(mvm, "Failed sending remove station\n");
2096 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2097
2098 return ret;
2099}
2100
2101void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2102{
2103 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2104}
2105
2106/*
2107 * Send the add station command for the vif's broadcast station.
2108 * Assumes that the station was already allocated.
2109 *
2110 * @mvm: the mvm component
2111 * @vif: the interface to which the broadcast station is added
2112 * @bsta: the broadcast station to add.
2113 */
2114int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2115{
2116 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2117 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2118 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2119 const u8 *baddr = _baddr;
2120 int queue;
2121 int ret;
2122 unsigned int wdg_timeout =
2123 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2124 struct iwl_trans_txq_scd_cfg cfg = {
2125 .fifo = IWL_MVM_TX_FIFO_VO,
2126 .sta_id = mvmvif->bcast_sta.sta_id,
2127 .tid = IWL_MAX_TID_COUNT,
2128 .aggregate = false,
2129 .frame_limit = IWL_FRAME_LIMIT,
2130 };
2131
2132 lockdep_assert_held(&mvm->mutex);
2133
2134 if (!iwl_mvm_has_new_tx_api(mvm)) {
2135 if (vif->type == NL80211_IFTYPE_AP ||
2136 vif->type == NL80211_IFTYPE_ADHOC) {
2137 queue = mvm->probe_queue;
2138 } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2139 queue = mvm->p2p_dev_queue;
2140 } else {
2141 WARN(1, "Missing required TXQ for adding bcast STA\n");
2142 return -EINVAL;
2143 }
2144
2145 bsta->tfd_queue_msk |= BIT(queue);
2146
2147 iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2148 }
2149
2150 if (vif->type == NL80211_IFTYPE_ADHOC)
2151 baddr = vif->bss_conf.bssid;
2152
2153 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2154 return -ENOSPC;
2155
2156 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2157 mvmvif->id, mvmvif->color);
2158 if (ret)
2159 return ret;
2160
2161 /*
2162 * For 22000 firmware and on we cannot add queue to a station unknown
2163 * to firmware so enable queue here - after the station was added
2164 */
2165 if (iwl_mvm_has_new_tx_api(mvm)) {
2166 queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2167 IWL_MAX_TID_COUNT,
2168 wdg_timeout);
2169 if (queue < 0) {
2170 iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2171 return queue;
2172 }
2173
2174 if (vif->type == NL80211_IFTYPE_AP ||
2175 vif->type == NL80211_IFTYPE_ADHOC)
2176 mvm->probe_queue = queue;
2177 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2178 mvm->p2p_dev_queue = queue;
2179 }
2180
2181 return 0;
2182}
2183
2184static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2185 struct ieee80211_vif *vif)
2186{
2187 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2188 int queue;
2189
2190 lockdep_assert_held(&mvm->mutex);
2191
2192 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2193
2194 switch (vif->type) {
2195 case NL80211_IFTYPE_AP:
2196 case NL80211_IFTYPE_ADHOC:
2197 queue = mvm->probe_queue;
2198 break;
2199 case NL80211_IFTYPE_P2P_DEVICE:
2200 queue = mvm->p2p_dev_queue;
2201 break;
2202 default:
2203 WARN(1, "Can't free bcast queue on vif type %d\n",
2204 vif->type);
2205 return;
2206 }
2207
2208 iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2209 if (iwl_mvm_has_new_tx_api(mvm))
2210 return;
2211
2212 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2213 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2214}
2215
2216/* Send the FW a request to remove the station from it's internal data
2217 * structures, but DO NOT remove the entry from the local data structures. */
2218int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2219{
2220 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2221 int ret;
2222
2223 lockdep_assert_held(&mvm->mutex);
2224
2225 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2226
2227 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2228 if (ret)
2229 IWL_WARN(mvm, "Failed sending remove station\n");
2230 return ret;
2231}
2232
2233int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2234{
2235 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2236
2237 lockdep_assert_held(&mvm->mutex);
2238
2239 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2240 ieee80211_vif_type_p2p(vif),
2241 IWL_STA_GENERAL_PURPOSE);
2242}
2243
2244/* Allocate a new station entry for the broadcast station to the given vif,
2245 * and send it to the FW.
2246 * Note that each P2P mac should have its own broadcast station.
2247 *
2248 * @mvm: the mvm component
2249 * @vif: the interface to which the broadcast station is added
2250 * @bsta: the broadcast station to add. */
2251int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2252{
2253 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2254 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2255 int ret;
2256
2257 lockdep_assert_held(&mvm->mutex);
2258
2259 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2260 if (ret)
2261 return ret;
2262
2263 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2264
2265 if (ret)
2266 iwl_mvm_dealloc_int_sta(mvm, bsta);
2267
2268 return ret;
2269}
2270
2271void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2272{
2273 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2274
2275 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2276}
2277
2278/*
2279 * Send the FW a request to remove the station from it's internal data
2280 * structures, and in addition remove it from the local data structure.
2281 */
2282int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2283{
2284 int ret;
2285
2286 lockdep_assert_held(&mvm->mutex);
2287
2288 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2289
2290 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2291
2292 return ret;
2293}
2294
2295/*
2296 * Allocate a new station entry for the multicast station to the given vif,
2297 * and send it to the FW.
2298 * Note that each AP/GO mac should have its own multicast station.
2299 *
2300 * @mvm: the mvm component
2301 * @vif: the interface to which the multicast station is added
2302 */
2303int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2304{
2305 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2306 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2307 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2308 const u8 *maddr = _maddr;
2309 struct iwl_trans_txq_scd_cfg cfg = {
2310 .fifo = vif->type == NL80211_IFTYPE_AP ?
2311 IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2312 .sta_id = msta->sta_id,
2313 .tid = 0,
2314 .aggregate = false,
2315 .frame_limit = IWL_FRAME_LIMIT,
2316 };
2317 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2318 int ret;
2319
2320 lockdep_assert_held(&mvm->mutex);
2321
2322 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2323 vif->type != NL80211_IFTYPE_ADHOC))
2324 return -ENOTSUPP;
2325
2326 /*
2327 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2328 * invalid, so make sure we use the queue we want.
2329 * Note that this is done here as we want to avoid making DQA
2330 * changes in mac80211 layer.
2331 */
2332 if (vif->type == NL80211_IFTYPE_ADHOC)
2333 mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2334
2335 /*
2336 * While in previous FWs we had to exclude cab queue from TFD queue
2337 * mask, now it is needed as any other queue.
2338 */
2339 if (!iwl_mvm_has_new_tx_api(mvm) &&
2340 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2341 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2342 timeout);
2343 msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2344 }
2345 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2346 mvmvif->id, mvmvif->color);
2347 if (ret)
2348 goto err;
2349
2350 /*
2351 * Enable cab queue after the ADD_STA command is sent.
2352 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2353 * command with unknown station id, and for FW that doesn't support
2354 * station API since the cab queue is not included in the
2355 * tfd_queue_mask.
2356 */
2357 if (iwl_mvm_has_new_tx_api(mvm)) {
2358 int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2359 0,
2360 timeout);
2361 if (queue < 0) {
2362 ret = queue;
2363 goto err;
2364 }
2365 mvmvif->cab_queue = queue;
2366 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2367 IWL_UCODE_TLV_API_STA_TYPE))
2368 iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2369 timeout);
2370
2371 return 0;
2372err:
2373 iwl_mvm_dealloc_int_sta(mvm, msta);
2374 return ret;
2375}
2376
2377static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2378 struct ieee80211_key_conf *keyconf,
2379 bool mcast)
2380{
2381 union {
2382 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2383 struct iwl_mvm_add_sta_key_cmd cmd;
2384 } u = {};
2385 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2386 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2387 __le16 key_flags;
2388 int ret, size;
2389 u32 status;
2390
2391 /* This is a valid situation for GTK removal */
2392 if (sta_id == IWL_MVM_INVALID_STA)
2393 return 0;
2394
2395 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2396 STA_KEY_FLG_KEYID_MSK);
2397 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2398 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2399
2400 if (mcast)
2401 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2402
2403 /*
2404 * The fields assigned here are in the same location at the start
2405 * of the command, so we can do this union trick.
2406 */
2407 u.cmd.common.key_flags = key_flags;
2408 u.cmd.common.key_offset = keyconf->hw_key_idx;
2409 u.cmd.common.sta_id = sta_id;
2410
2411 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2412
2413 status = ADD_STA_SUCCESS;
2414 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2415 &status);
2416
2417 switch (status) {
2418 case ADD_STA_SUCCESS:
2419 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2420 break;
2421 default:
2422 ret = -EIO;
2423 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2424 break;
2425 }
2426
2427 return ret;
2428}
2429
2430/*
2431 * Send the FW a request to remove the station from it's internal data
2432 * structures, and in addition remove it from the local data structure.
2433 */
2434int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2435{
2436 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2437 int ret;
2438
2439 lockdep_assert_held(&mvm->mutex);
2440
2441 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2442
2443 iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
2444
2445 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2446 if (ret)
2447 IWL_WARN(mvm, "Failed sending remove station\n");
2448
2449 return ret;
2450}
2451
2452#define IWL_MAX_RX_BA_SESSIONS 16
2453
2454static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2455{
2456 struct iwl_mvm_rss_sync_notif notif = {
2457 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2458 .metadata.sync = 1,
2459 .delba.baid = baid,
2460 };
2461 iwl_mvm_sync_rx_queues_internal(mvm, (void *)¬if, sizeof(notif));
2462};
2463
2464static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2465 struct iwl_mvm_baid_data *data)
2466{
2467 int i;
2468
2469 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2470
2471 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2472 int j;
2473 struct iwl_mvm_reorder_buffer *reorder_buf =
2474 &data->reorder_buf[i];
2475 struct iwl_mvm_reorder_buf_entry *entries =
2476 &data->entries[i * data->entries_per_queue];
2477
2478 spin_lock_bh(&reorder_buf->lock);
2479 if (likely(!reorder_buf->num_stored)) {
2480 spin_unlock_bh(&reorder_buf->lock);
2481 continue;
2482 }
2483
2484 /*
2485 * This shouldn't happen in regular DELBA since the internal
2486 * delBA notification should trigger a release of all frames in
2487 * the reorder buffer.
2488 */
2489 WARN_ON(1);
2490
2491 for (j = 0; j < reorder_buf->buf_size; j++)
2492 __skb_queue_purge(&entries[j].e.frames);
2493 /*
2494 * Prevent timer re-arm. This prevents a very far fetched case
2495 * where we timed out on the notification. There may be prior
2496 * RX frames pending in the RX queue before the notification
2497 * that might get processed between now and the actual deletion
2498 * and we would re-arm the timer although we are deleting the
2499 * reorder buffer.
2500 */
2501 reorder_buf->removed = true;
2502 spin_unlock_bh(&reorder_buf->lock);
2503 del_timer_sync(&reorder_buf->reorder_timer);
2504 }
2505}
2506
2507static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2508 struct iwl_mvm_baid_data *data,
2509 u16 ssn, u16 buf_size)
2510{
2511 int i;
2512
2513 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2514 struct iwl_mvm_reorder_buffer *reorder_buf =
2515 &data->reorder_buf[i];
2516 struct iwl_mvm_reorder_buf_entry *entries =
2517 &data->entries[i * data->entries_per_queue];
2518 int j;
2519
2520 reorder_buf->num_stored = 0;
2521 reorder_buf->head_sn = ssn;
2522 reorder_buf->buf_size = buf_size;
2523 /* rx reorder timer */
2524 timer_setup(&reorder_buf->reorder_timer,
2525 iwl_mvm_reorder_timer_expired, 0);
2526 spin_lock_init(&reorder_buf->lock);
2527 reorder_buf->mvm = mvm;
2528 reorder_buf->queue = i;
2529 reorder_buf->valid = false;
2530 for (j = 0; j < reorder_buf->buf_size; j++)
2531 __skb_queue_head_init(&entries[j].e.frames);
2532 }
2533}
2534
2535int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2536 int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2537{
2538 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2539 struct iwl_mvm_add_sta_cmd cmd = {};
2540 struct iwl_mvm_baid_data *baid_data = NULL;
2541 int ret;
2542 u32 status;
2543
2544 lockdep_assert_held(&mvm->mutex);
2545
2546 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2547 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2548 return -ENOSPC;
2549 }
2550
2551 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2552 u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2553
2554 /* sparse doesn't like the __align() so don't check */
2555#ifndef __CHECKER__
2556 /*
2557 * The division below will be OK if either the cache line size
2558 * can be divided by the entry size (ALIGN will round up) or if
2559 * if the entry size can be divided by the cache line size, in
2560 * which case the ALIGN() will do nothing.
2561 */
2562 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2563 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2564#endif
2565
2566 /*
2567 * Upward align the reorder buffer size to fill an entire cache
2568 * line for each queue, to avoid sharing cache lines between
2569 * different queues.
2570 */
2571 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2572
2573 /*
2574 * Allocate here so if allocation fails we can bail out early
2575 * before starting the BA session in the firmware
2576 */
2577 baid_data = kzalloc(sizeof(*baid_data) +
2578 mvm->trans->num_rx_queues *
2579 reorder_buf_size,
2580 GFP_KERNEL);
2581 if (!baid_data)
2582 return -ENOMEM;
2583
2584 /*
2585 * This division is why we need the above BUILD_BUG_ON(),
2586 * if that doesn't hold then this will not be right.
2587 */
2588 baid_data->entries_per_queue =
2589 reorder_buf_size / sizeof(baid_data->entries[0]);
2590 }
2591
2592 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2593 cmd.sta_id = mvm_sta->sta_id;
2594 cmd.add_modify = STA_MODE_MODIFY;
2595 if (start) {
2596 cmd.add_immediate_ba_tid = (u8) tid;
2597 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2598 cmd.rx_ba_window = cpu_to_le16(buf_size);
2599 } else {
2600 cmd.remove_immediate_ba_tid = (u8) tid;
2601 }
2602 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2603 STA_MODIFY_REMOVE_BA_TID;
2604
2605 status = ADD_STA_SUCCESS;
2606 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2607 iwl_mvm_add_sta_cmd_size(mvm),
2608 &cmd, &status);
2609 if (ret)
2610 goto out_free;
2611
2612 switch (status & IWL_ADD_STA_STATUS_MASK) {
2613 case ADD_STA_SUCCESS:
2614 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2615 start ? "start" : "stopp");
2616 break;
2617 case ADD_STA_IMMEDIATE_BA_FAILURE:
2618 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2619 ret = -ENOSPC;
2620 break;
2621 default:
2622 ret = -EIO;
2623 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2624 start ? "start" : "stopp", status);
2625 break;
2626 }
2627
2628 if (ret)
2629 goto out_free;
2630
2631 if (start) {
2632 u8 baid;
2633
2634 mvm->rx_ba_sessions++;
2635
2636 if (!iwl_mvm_has_new_rx_api(mvm))
2637 return 0;
2638
2639 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2640 ret = -EINVAL;
2641 goto out_free;
2642 }
2643 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2644 IWL_ADD_STA_BAID_SHIFT);
2645 baid_data->baid = baid;
2646 baid_data->timeout = timeout;
2647 baid_data->last_rx = jiffies;
2648 baid_data->rcu_ptr = &mvm->baid_map[baid];
2649 timer_setup(&baid_data->session_timer,
2650 iwl_mvm_rx_agg_session_expired, 0);
2651 baid_data->mvm = mvm;
2652 baid_data->tid = tid;
2653 baid_data->sta_id = mvm_sta->sta_id;
2654
2655 mvm_sta->tid_to_baid[tid] = baid;
2656 if (timeout)
2657 mod_timer(&baid_data->session_timer,
2658 TU_TO_EXP_TIME(timeout * 2));
2659
2660 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2661 /*
2662 * protect the BA data with RCU to cover a case where our
2663 * internal RX sync mechanism will timeout (not that it's
2664 * supposed to happen) and we will free the session data while
2665 * RX is being processed in parallel
2666 */
2667 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2668 mvm_sta->sta_id, tid, baid);
2669 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2670 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2671 } else {
2672 u8 baid = mvm_sta->tid_to_baid[tid];
2673
2674 if (mvm->rx_ba_sessions > 0)
2675 /* check that restart flow didn't zero the counter */
2676 mvm->rx_ba_sessions--;
2677 if (!iwl_mvm_has_new_rx_api(mvm))
2678 return 0;
2679
2680 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2681 return -EINVAL;
2682
2683 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2684 if (WARN_ON(!baid_data))
2685 return -EINVAL;
2686
2687 /* synchronize all rx queues so we can safely delete */
2688 iwl_mvm_free_reorder(mvm, baid_data);
2689 del_timer_sync(&baid_data->session_timer);
2690 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2691 kfree_rcu(baid_data, rcu_head);
2692 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2693 }
2694 return 0;
2695
2696out_free:
2697 kfree(baid_data);
2698 return ret;
2699}
2700
2701int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2702 int tid, u8 queue, bool start)
2703{
2704 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2705 struct iwl_mvm_add_sta_cmd cmd = {};
2706 int ret;
2707 u32 status;
2708
2709 lockdep_assert_held(&mvm->mutex);
2710
2711 if (start) {
2712 mvm_sta->tfd_queue_msk |= BIT(queue);
2713 mvm_sta->tid_disable_agg &= ~BIT(tid);
2714 } else {
2715 /* In DQA-mode the queue isn't removed on agg termination */
2716 mvm_sta->tid_disable_agg |= BIT(tid);
2717 }
2718
2719 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2720 cmd.sta_id = mvm_sta->sta_id;
2721 cmd.add_modify = STA_MODE_MODIFY;
2722 if (!iwl_mvm_has_new_tx_api(mvm))
2723 cmd.modify_mask = STA_MODIFY_QUEUES;
2724 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2725 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2726 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2727
2728 status = ADD_STA_SUCCESS;
2729 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2730 iwl_mvm_add_sta_cmd_size(mvm),
2731 &cmd, &status);
2732 if (ret)
2733 return ret;
2734
2735 switch (status & IWL_ADD_STA_STATUS_MASK) {
2736 case ADD_STA_SUCCESS:
2737 break;
2738 default:
2739 ret = -EIO;
2740 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2741 start ? "start" : "stopp", status);
2742 break;
2743 }
2744
2745 return ret;
2746}
2747
2748const u8 tid_to_mac80211_ac[] = {
2749 IEEE80211_AC_BE,
2750 IEEE80211_AC_BK,
2751 IEEE80211_AC_BK,
2752 IEEE80211_AC_BE,
2753 IEEE80211_AC_VI,
2754 IEEE80211_AC_VI,
2755 IEEE80211_AC_VO,
2756 IEEE80211_AC_VO,
2757 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2758};
2759
2760static const u8 tid_to_ucode_ac[] = {
2761 AC_BE,
2762 AC_BK,
2763 AC_BK,
2764 AC_BE,
2765 AC_VI,
2766 AC_VI,
2767 AC_VO,
2768 AC_VO,
2769};
2770
2771int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2772 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2773{
2774 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2775 struct iwl_mvm_tid_data *tid_data;
2776 u16 normalized_ssn;
2777 u16 txq_id;
2778 int ret;
2779
2780 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2781 return -EINVAL;
2782
2783 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2784 mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2785 IWL_ERR(mvm,
2786 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2787 mvmsta->tid_data[tid].state);
2788 return -ENXIO;
2789 }
2790
2791 lockdep_assert_held(&mvm->mutex);
2792
2793 if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2794 iwl_mvm_has_new_tx_api(mvm)) {
2795 u8 ac = tid_to_mac80211_ac[tid];
2796
2797 ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2798 if (ret)
2799 return ret;
2800 }
2801
2802 spin_lock_bh(&mvmsta->lock);
2803
2804 /*
2805 * Note the possible cases:
2806 * 1. An enabled TXQ - TXQ needs to become agg'ed
2807 * 2. The TXQ hasn't yet been enabled, so find a free one and mark
2808 * it as reserved
2809 */
2810 txq_id = mvmsta->tid_data[tid].txq_id;
2811 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2812 ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2813 IWL_MVM_DQA_MIN_DATA_QUEUE,
2814 IWL_MVM_DQA_MAX_DATA_QUEUE);
2815 if (ret < 0) {
2816 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2817 goto out;
2818 }
2819
2820 txq_id = ret;
2821
2822 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2823 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2824 } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2825 ret = -ENXIO;
2826 IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2827 tid, IWL_MAX_HW_QUEUES - 1);
2828 goto out;
2829
2830 } else if (unlikely(mvm->queue_info[txq_id].status ==
2831 IWL_MVM_QUEUE_SHARED)) {
2832 ret = -ENXIO;
2833 IWL_DEBUG_TX_QUEUES(mvm,
2834 "Can't start tid %d agg on shared queue!\n",
2835 tid);
2836 goto out;
2837 }
2838
2839 IWL_DEBUG_TX_QUEUES(mvm,
2840 "AGG for tid %d will be on queue #%d\n",
2841 tid, txq_id);
2842
2843 tid_data = &mvmsta->tid_data[tid];
2844 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2845 tid_data->txq_id = txq_id;
2846 *ssn = tid_data->ssn;
2847
2848 IWL_DEBUG_TX_QUEUES(mvm,
2849 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2850 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2851 tid_data->next_reclaimed);
2852
2853 /*
2854 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2855 * to align the wrap around of ssn so we compare relevant values.
2856 */
2857 normalized_ssn = tid_data->ssn;
2858 if (mvm->trans->trans_cfg->gen2)
2859 normalized_ssn &= 0xff;
2860
2861 if (normalized_ssn == tid_data->next_reclaimed) {
2862 tid_data->state = IWL_AGG_STARTING;
2863 ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
2864 } else {
2865 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2866 ret = 0;
2867 }
2868
2869out:
2870 spin_unlock_bh(&mvmsta->lock);
2871
2872 return ret;
2873}
2874
2875int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2876 struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2877 bool amsdu)
2878{
2879 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2880 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2881 unsigned int wdg_timeout =
2882 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2883 int queue, ret;
2884 bool alloc_queue = true;
2885 enum iwl_mvm_queue_status queue_status;
2886 u16 ssn;
2887
2888 struct iwl_trans_txq_scd_cfg cfg = {
2889 .sta_id = mvmsta->sta_id,
2890 .tid = tid,
2891 .frame_limit = buf_size,
2892 .aggregate = true,
2893 };
2894
2895 /*
2896 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2897 * manager, so this function should never be called in this case.
2898 */
2899 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2900 return -EINVAL;
2901
2902 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2903 != IWL_MAX_TID_COUNT);
2904
2905 spin_lock_bh(&mvmsta->lock);
2906 ssn = tid_data->ssn;
2907 queue = tid_data->txq_id;
2908 tid_data->state = IWL_AGG_ON;
2909 mvmsta->agg_tids |= BIT(tid);
2910 tid_data->ssn = 0xffff;
2911 tid_data->amsdu_in_ampdu_allowed = amsdu;
2912 spin_unlock_bh(&mvmsta->lock);
2913
2914 if (iwl_mvm_has_new_tx_api(mvm)) {
2915 /*
2916 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2917 * would have failed, so if we are here there is no need to
2918 * allocate a queue.
2919 * However, if aggregation size is different than the default
2920 * size, the scheduler should be reconfigured.
2921 * We cannot do this with the new TX API, so return unsupported
2922 * for now, until it will be offloaded to firmware..
2923 * Note that if SCD default value changes - this condition
2924 * should be updated as well.
2925 */
2926 if (buf_size < IWL_FRAME_LIMIT)
2927 return -ENOTSUPP;
2928
2929 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2930 if (ret)
2931 return -EIO;
2932 goto out;
2933 }
2934
2935 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2936
2937 queue_status = mvm->queue_info[queue].status;
2938
2939 /* Maybe there is no need to even alloc a queue... */
2940 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2941 alloc_queue = false;
2942
2943 /*
2944 * Only reconfig the SCD for the queue if the window size has
2945 * changed from current (become smaller)
2946 */
2947 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2948 /*
2949 * If reconfiguring an existing queue, it first must be
2950 * drained
2951 */
2952 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2953 BIT(queue));
2954 if (ret) {
2955 IWL_ERR(mvm,
2956 "Error draining queue before reconfig\n");
2957 return ret;
2958 }
2959
2960 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2961 mvmsta->sta_id, tid,
2962 buf_size, ssn);
2963 if (ret) {
2964 IWL_ERR(mvm,
2965 "Error reconfiguring TXQ #%d\n", queue);
2966 return ret;
2967 }
2968 }
2969
2970 if (alloc_queue)
2971 iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2972 &cfg, wdg_timeout);
2973
2974 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2975 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2976 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2977 if (ret)
2978 return -EIO;
2979 }
2980
2981 /* No need to mark as reserved */
2982 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2983
2984out:
2985 /*
2986 * Even though in theory the peer could have different
2987 * aggregation reorder buffer sizes for different sessions,
2988 * our ucode doesn't allow for that and has a global limit
2989 * for each station. Therefore, use the minimum of all the
2990 * aggregation sessions and our default value.
2991 */
2992 mvmsta->max_agg_bufsize =
2993 min(mvmsta->max_agg_bufsize, buf_size);
2994 mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2995
2996 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2997 sta->addr, tid);
2998
2999 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
3000}
3001
3002static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
3003 struct iwl_mvm_sta *mvmsta,
3004 struct iwl_mvm_tid_data *tid_data)
3005{
3006 u16 txq_id = tid_data->txq_id;
3007
3008 lockdep_assert_held(&mvm->mutex);
3009
3010 if (iwl_mvm_has_new_tx_api(mvm))
3011 return;
3012
3013 /*
3014 * The TXQ is marked as reserved only if no traffic came through yet
3015 * This means no traffic has been sent on this TID (agg'd or not), so
3016 * we no longer have use for the queue. Since it hasn't even been
3017 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3018 * free.
3019 */
3020 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3021 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3022 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3023 }
3024}
3025
3026int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3027 struct ieee80211_sta *sta, u16 tid)
3028{
3029 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3030 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3031 u16 txq_id;
3032 int err;
3033
3034 /*
3035 * If mac80211 is cleaning its state, then say that we finished since
3036 * our state has been cleared anyway.
3037 */
3038 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3039 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3040 return 0;
3041 }
3042
3043 spin_lock_bh(&mvmsta->lock);
3044
3045 txq_id = tid_data->txq_id;
3046
3047 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3048 mvmsta->sta_id, tid, txq_id, tid_data->state);
3049
3050 mvmsta->agg_tids &= ~BIT(tid);
3051
3052 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3053
3054 switch (tid_data->state) {
3055 case IWL_AGG_ON:
3056 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3057
3058 IWL_DEBUG_TX_QUEUES(mvm,
3059 "ssn = %d, next_recl = %d\n",
3060 tid_data->ssn, tid_data->next_reclaimed);
3061
3062 tid_data->ssn = 0xffff;
3063 tid_data->state = IWL_AGG_OFF;
3064 spin_unlock_bh(&mvmsta->lock);
3065
3066 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3067
3068 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3069 return 0;
3070 case IWL_AGG_STARTING:
3071 case IWL_EMPTYING_HW_QUEUE_ADDBA:
3072 /*
3073 * The agg session has been stopped before it was set up. This
3074 * can happen when the AddBA timer times out for example.
3075 */
3076
3077 /* No barriers since we are under mutex */
3078 lockdep_assert_held(&mvm->mutex);
3079
3080 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3081 tid_data->state = IWL_AGG_OFF;
3082 err = 0;
3083 break;
3084 default:
3085 IWL_ERR(mvm,
3086 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3087 mvmsta->sta_id, tid, tid_data->state);
3088 IWL_ERR(mvm,
3089 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
3090 err = -EINVAL;
3091 }
3092
3093 spin_unlock_bh(&mvmsta->lock);
3094
3095 return err;
3096}
3097
3098int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3099 struct ieee80211_sta *sta, u16 tid)
3100{
3101 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3102 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3103 u16 txq_id;
3104 enum iwl_mvm_agg_state old_state;
3105
3106 /*
3107 * First set the agg state to OFF to avoid calling
3108 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3109 */
3110 spin_lock_bh(&mvmsta->lock);
3111 txq_id = tid_data->txq_id;
3112 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3113 mvmsta->sta_id, tid, txq_id, tid_data->state);
3114 old_state = tid_data->state;
3115 tid_data->state = IWL_AGG_OFF;
3116 mvmsta->agg_tids &= ~BIT(tid);
3117 spin_unlock_bh(&mvmsta->lock);
3118
3119 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3120
3121 if (old_state >= IWL_AGG_ON) {
3122 iwl_mvm_drain_sta(mvm, mvmsta, true);
3123
3124 if (iwl_mvm_has_new_tx_api(mvm)) {
3125 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3126 BIT(tid), 0))
3127 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3128 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3129 } else {
3130 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3131 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3132 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3133 }
3134
3135 iwl_mvm_drain_sta(mvm, mvmsta, false);
3136
3137 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3138 }
3139
3140 return 0;
3141}
3142
3143static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3144{
3145 int i, max = -1, max_offs = -1;
3146
3147 lockdep_assert_held(&mvm->mutex);
3148
3149 /* Pick the unused key offset with the highest 'deleted'
3150 * counter. Every time a key is deleted, all the counters
3151 * are incremented and the one that was just deleted is
3152 * reset to zero. Thus, the highest counter is the one
3153 * that was deleted longest ago. Pick that one.
3154 */
3155 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3156 if (test_bit(i, mvm->fw_key_table))
3157 continue;
3158 if (mvm->fw_key_deleted[i] > max) {
3159 max = mvm->fw_key_deleted[i];
3160 max_offs = i;
3161 }
3162 }
3163
3164 if (max_offs < 0)
3165 return STA_KEY_IDX_INVALID;
3166
3167 return max_offs;
3168}
3169
3170static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3171 struct ieee80211_vif *vif,
3172 struct ieee80211_sta *sta)
3173{
3174 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3175
3176 if (sta)
3177 return iwl_mvm_sta_from_mac80211(sta);
3178
3179 /*
3180 * The device expects GTKs for station interfaces to be
3181 * installed as GTKs for the AP station. If we have no
3182 * station ID, then use AP's station ID.
3183 */
3184 if (vif->type == NL80211_IFTYPE_STATION &&
3185 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3186 u8 sta_id = mvmvif->ap_sta_id;
3187
3188 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3189 lockdep_is_held(&mvm->mutex));
3190
3191 /*
3192 * It is possible that the 'sta' parameter is NULL,
3193 * for example when a GTK is removed - the sta_id will then
3194 * be the AP ID, and no station was passed by mac80211.
3195 */
3196 if (IS_ERR_OR_NULL(sta))
3197 return NULL;
3198
3199 return iwl_mvm_sta_from_mac80211(sta);
3200 }
3201
3202 return NULL;
3203}
3204
3205static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3206 u32 sta_id,
3207 struct ieee80211_key_conf *key, bool mcast,
3208 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3209 u8 key_offset, bool mfp)
3210{
3211 union {
3212 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3213 struct iwl_mvm_add_sta_key_cmd cmd;
3214 } u = {};
3215 __le16 key_flags;
3216 int ret;
3217 u32 status;
3218 u16 keyidx;
3219 u64 pn = 0;
3220 int i, size;
3221 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3222 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3223
3224 if (sta_id == IWL_MVM_INVALID_STA)
3225 return -EINVAL;
3226
3227 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3228 STA_KEY_FLG_KEYID_MSK;
3229 key_flags = cpu_to_le16(keyidx);
3230 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3231
3232 switch (key->cipher) {
3233 case WLAN_CIPHER_SUITE_TKIP:
3234 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3235 if (new_api) {
3236 memcpy((void *)&u.cmd.tx_mic_key,
3237 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3238 IWL_MIC_KEY_SIZE);
3239
3240 memcpy((void *)&u.cmd.rx_mic_key,
3241 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3242 IWL_MIC_KEY_SIZE);
3243 pn = atomic64_read(&key->tx_pn);
3244
3245 } else {
3246 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3247 for (i = 0; i < 5; i++)
3248 u.cmd_v1.tkip_rx_ttak[i] =
3249 cpu_to_le16(tkip_p1k[i]);
3250 }
3251 memcpy(u.cmd.common.key, key->key, key->keylen);
3252 break;
3253 case WLAN_CIPHER_SUITE_CCMP:
3254 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3255 memcpy(u.cmd.common.key, key->key, key->keylen);
3256 if (new_api)
3257 pn = atomic64_read(&key->tx_pn);
3258 break;
3259 case WLAN_CIPHER_SUITE_WEP104:
3260 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3261 /* fall through */
3262 case WLAN_CIPHER_SUITE_WEP40:
3263 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3264 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3265 break;
3266 case WLAN_CIPHER_SUITE_GCMP_256:
3267 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3268 /* fall through */
3269 case WLAN_CIPHER_SUITE_GCMP:
3270 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3271 memcpy(u.cmd.common.key, key->key, key->keylen);
3272 if (new_api)
3273 pn = atomic64_read(&key->tx_pn);
3274 break;
3275 default:
3276 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3277 memcpy(u.cmd.common.key, key->key, key->keylen);
3278 }
3279
3280 if (mcast)
3281 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3282 if (mfp)
3283 key_flags |= cpu_to_le16(STA_KEY_MFP);
3284
3285 u.cmd.common.key_offset = key_offset;
3286 u.cmd.common.key_flags = key_flags;
3287 u.cmd.common.sta_id = sta_id;
3288
3289 if (new_api) {
3290 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3291 size = sizeof(u.cmd);
3292 } else {
3293 size = sizeof(u.cmd_v1);
3294 }
3295
3296 status = ADD_STA_SUCCESS;
3297 if (cmd_flags & CMD_ASYNC)
3298 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3299 &u.cmd);
3300 else
3301 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3302 &u.cmd, &status);
3303
3304 switch (status) {
3305 case ADD_STA_SUCCESS:
3306 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3307 break;
3308 default:
3309 ret = -EIO;
3310 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3311 break;
3312 }
3313
3314 return ret;
3315}
3316
3317static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3318 struct ieee80211_key_conf *keyconf,
3319 u8 sta_id, bool remove_key)
3320{
3321 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3322
3323 /* verify the key details match the required command's expectations */
3324 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3325 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3326 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3327 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3328 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3329 return -EINVAL;
3330
3331 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3332 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3333 return -EINVAL;
3334
3335 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3336 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3337
3338 if (remove_key) {
3339 /* This is a valid situation for IGTK */
3340 if (sta_id == IWL_MVM_INVALID_STA)
3341 return 0;
3342
3343 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3344 } else {
3345 struct ieee80211_key_seq seq;
3346 const u8 *pn;
3347
3348 switch (keyconf->cipher) {
3349 case WLAN_CIPHER_SUITE_AES_CMAC:
3350 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3351 break;
3352 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3353 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3354 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3355 break;
3356 default:
3357 return -EINVAL;
3358 }
3359
3360 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3361 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3362 igtk_cmd.ctrl_flags |=
3363 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3364 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3365 pn = seq.aes_cmac.pn;
3366 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3367 ((u64) pn[4] << 8) |
3368 ((u64) pn[3] << 16) |
3369 ((u64) pn[2] << 24) |
3370 ((u64) pn[1] << 32) |
3371 ((u64) pn[0] << 40));
3372 }
3373
3374 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3375 remove_key ? "removing" : "installing",
3376 igtk_cmd.sta_id);
3377
3378 if (!iwl_mvm_has_new_rx_api(mvm)) {
3379 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3380 .ctrl_flags = igtk_cmd.ctrl_flags,
3381 .key_id = igtk_cmd.key_id,
3382 .sta_id = igtk_cmd.sta_id,
3383 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3384 };
3385
3386 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3387 ARRAY_SIZE(igtk_cmd_v1.igtk));
3388 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3389 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3390 }
3391 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3392 sizeof(igtk_cmd), &igtk_cmd);
3393}
3394
3395
3396static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3397 struct ieee80211_vif *vif,
3398 struct ieee80211_sta *sta)
3399{
3400 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3401
3402 if (sta)
3403 return sta->addr;
3404
3405 if (vif->type == NL80211_IFTYPE_STATION &&
3406 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3407 u8 sta_id = mvmvif->ap_sta_id;
3408 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3409 lockdep_is_held(&mvm->mutex));
3410 return sta->addr;
3411 }
3412
3413
3414 return NULL;
3415}
3416
3417static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3418 struct ieee80211_vif *vif,
3419 struct ieee80211_sta *sta,
3420 struct ieee80211_key_conf *keyconf,
3421 u8 key_offset,
3422 bool mcast)
3423{
3424 int ret;
3425 const u8 *addr;
3426 struct ieee80211_key_seq seq;
3427 u16 p1k[5];
3428 u32 sta_id;
3429 bool mfp = false;
3430
3431 if (sta) {
3432 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3433
3434 sta_id = mvm_sta->sta_id;
3435 mfp = sta->mfp;
3436 } else if (vif->type == NL80211_IFTYPE_AP &&
3437 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3438 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3439
3440 sta_id = mvmvif->mcast_sta.sta_id;
3441 } else {
3442 IWL_ERR(mvm, "Failed to find station id\n");
3443 return -EINVAL;
3444 }
3445
3446 switch (keyconf->cipher) {
3447 case WLAN_CIPHER_SUITE_TKIP:
3448 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3449 /* get phase 1 key from mac80211 */
3450 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3451 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3452 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3453 seq.tkip.iv32, p1k, 0, key_offset,
3454 mfp);
3455 break;
3456 case WLAN_CIPHER_SUITE_CCMP:
3457 case WLAN_CIPHER_SUITE_WEP40:
3458 case WLAN_CIPHER_SUITE_WEP104:
3459 case WLAN_CIPHER_SUITE_GCMP:
3460 case WLAN_CIPHER_SUITE_GCMP_256:
3461 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3462 0, NULL, 0, key_offset, mfp);
3463 break;
3464 default:
3465 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3466 0, NULL, 0, key_offset, mfp);
3467 }
3468
3469 return ret;
3470}
3471
3472int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3473 struct ieee80211_vif *vif,
3474 struct ieee80211_sta *sta,
3475 struct ieee80211_key_conf *keyconf,
3476 u8 key_offset)
3477{
3478 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3479 struct iwl_mvm_sta *mvm_sta;
3480 u8 sta_id = IWL_MVM_INVALID_STA;
3481 int ret;
3482 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3483
3484 lockdep_assert_held(&mvm->mutex);
3485
3486 if (vif->type != NL80211_IFTYPE_AP ||
3487 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3488 /* Get the station id from the mvm local station table */
3489 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3490 if (!mvm_sta) {
3491 IWL_ERR(mvm, "Failed to find station\n");
3492 return -EINVAL;
3493 }
3494 sta_id = mvm_sta->sta_id;
3495
3496 /*
3497 * It is possible that the 'sta' parameter is NULL, and thus
3498 * there is a need to retrieve the sta from the local station
3499 * table.
3500 */
3501 if (!sta) {
3502 sta = rcu_dereference_protected(
3503 mvm->fw_id_to_mac_id[sta_id],
3504 lockdep_is_held(&mvm->mutex));
3505 if (IS_ERR_OR_NULL(sta)) {
3506 IWL_ERR(mvm, "Invalid station id\n");
3507 return -EINVAL;
3508 }
3509 }
3510
3511 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3512 return -EINVAL;
3513 } else {
3514 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3515
3516 sta_id = mvmvif->mcast_sta.sta_id;
3517 }
3518
3519 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3520 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3521 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3522 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3523 goto end;
3524 }
3525
3526 /* If the key_offset is not pre-assigned, we need to find a
3527 * new offset to use. In normal cases, the offset is not
3528 * pre-assigned, but during HW_RESTART we want to reuse the
3529 * same indices, so we pass them when this function is called.
3530 *
3531 * In D3 entry, we need to hardcoded the indices (because the
3532 * firmware hardcodes the PTK offset to 0). In this case, we
3533 * need to make sure we don't overwrite the hw_key_idx in the
3534 * keyconf structure, because otherwise we cannot configure
3535 * the original ones back when resuming.
3536 */
3537 if (key_offset == STA_KEY_IDX_INVALID) {
3538 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3539 if (key_offset == STA_KEY_IDX_INVALID)
3540 return -ENOSPC;
3541 keyconf->hw_key_idx = key_offset;
3542 }
3543
3544 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3545 if (ret)
3546 goto end;
3547
3548 /*
3549 * For WEP, the same key is used for multicast and unicast. Upload it
3550 * again, using the same key offset, and now pointing the other one
3551 * to the same key slot (offset).
3552 * If this fails, remove the original as well.
3553 */
3554 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3555 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3556 sta) {
3557 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3558 key_offset, !mcast);
3559 if (ret) {
3560 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3561 goto end;
3562 }
3563 }
3564
3565 __set_bit(key_offset, mvm->fw_key_table);
3566
3567end:
3568 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3569 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3570 sta ? sta->addr : zero_addr, ret);
3571 return ret;
3572}
3573
3574int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3575 struct ieee80211_vif *vif,
3576 struct ieee80211_sta *sta,
3577 struct ieee80211_key_conf *keyconf)
3578{
3579 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3580 struct iwl_mvm_sta *mvm_sta;
3581 u8 sta_id = IWL_MVM_INVALID_STA;
3582 int ret, i;
3583
3584 lockdep_assert_held(&mvm->mutex);
3585
3586 /* Get the station from the mvm local station table */
3587 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3588 if (mvm_sta)
3589 sta_id = mvm_sta->sta_id;
3590 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3591 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3592
3593
3594 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3595 keyconf->keyidx, sta_id);
3596
3597 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3598 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3599 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3600 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3601
3602 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3603 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3604 keyconf->hw_key_idx);
3605 return -ENOENT;
3606 }
3607
3608 /* track which key was deleted last */
3609 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3610 if (mvm->fw_key_deleted[i] < U8_MAX)
3611 mvm->fw_key_deleted[i]++;
3612 }
3613 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3614
3615 if (sta && !mvm_sta) {
3616 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3617 return 0;
3618 }
3619
3620 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3621 if (ret)
3622 return ret;
3623
3624 /* delete WEP key twice to get rid of (now useless) offset */
3625 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3626 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3627 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3628
3629 return ret;
3630}
3631
3632void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3633 struct ieee80211_vif *vif,
3634 struct ieee80211_key_conf *keyconf,
3635 struct ieee80211_sta *sta, u32 iv32,
3636 u16 *phase1key)
3637{
3638 struct iwl_mvm_sta *mvm_sta;
3639 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3640 bool mfp = sta ? sta->mfp : false;
3641
3642 rcu_read_lock();
3643
3644 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3645 if (WARN_ON_ONCE(!mvm_sta))
3646 goto unlock;
3647 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3648 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3649 mfp);
3650
3651 unlock:
3652 rcu_read_unlock();
3653}
3654
3655void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3656 struct ieee80211_sta *sta)
3657{
3658 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3659 struct iwl_mvm_add_sta_cmd cmd = {
3660 .add_modify = STA_MODE_MODIFY,
3661 .sta_id = mvmsta->sta_id,
3662 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3663 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3664 };
3665 int ret;
3666
3667 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3668 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3669 if (ret)
3670 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3671}
3672
3673void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3674 struct ieee80211_sta *sta,
3675 enum ieee80211_frame_release_type reason,
3676 u16 cnt, u16 tids, bool more_data,
3677 bool single_sta_queue)
3678{
3679 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3680 struct iwl_mvm_add_sta_cmd cmd = {
3681 .add_modify = STA_MODE_MODIFY,
3682 .sta_id = mvmsta->sta_id,
3683 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3684 .sleep_tx_count = cpu_to_le16(cnt),
3685 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3686 };
3687 int tid, ret;
3688 unsigned long _tids = tids;
3689
3690 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3691 * Note that this field is reserved and unused by firmware not
3692 * supporting GO uAPSD, so it's safe to always do this.
3693 */
3694 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3695 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3696
3697 /* If we're releasing frames from aggregation or dqa queues then check
3698 * if all the queues that we're releasing frames from, combined, have:
3699 * - more frames than the service period, in which case more_data
3700 * needs to be set
3701 * - fewer than 'cnt' frames, in which case we need to adjust the
3702 * firmware command (but do that unconditionally)
3703 */
3704 if (single_sta_queue) {
3705 int remaining = cnt;
3706 int sleep_tx_count;
3707
3708 spin_lock_bh(&mvmsta->lock);
3709 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3710 struct iwl_mvm_tid_data *tid_data;
3711 u16 n_queued;
3712
3713 tid_data = &mvmsta->tid_data[tid];
3714
3715 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3716 if (n_queued > remaining) {
3717 more_data = true;
3718 remaining = 0;
3719 break;
3720 }
3721 remaining -= n_queued;
3722 }
3723 sleep_tx_count = cnt - remaining;
3724 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3725 mvmsta->sleep_tx_count = sleep_tx_count;
3726 spin_unlock_bh(&mvmsta->lock);
3727
3728 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3729 if (WARN_ON(cnt - remaining == 0)) {
3730 ieee80211_sta_eosp(sta);
3731 return;
3732 }
3733 }
3734
3735 /* Note: this is ignored by firmware not supporting GO uAPSD */
3736 if (more_data)
3737 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3738
3739 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3740 mvmsta->next_status_eosp = true;
3741 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3742 } else {
3743 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3744 }
3745
3746 /* block the Tx queues until the FW updated the sleep Tx count */
3747 iwl_trans_block_txq_ptrs(mvm->trans, true);
3748
3749 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3750 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3751 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3752 if (ret)
3753 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3754}
3755
3756void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3757 struct iwl_rx_cmd_buffer *rxb)
3758{
3759 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3760 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3761 struct ieee80211_sta *sta;
3762 u32 sta_id = le32_to_cpu(notif->sta_id);
3763
3764 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3765 return;
3766
3767 rcu_read_lock();
3768 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3769 if (!IS_ERR_OR_NULL(sta))
3770 ieee80211_sta_eosp(sta);
3771 rcu_read_unlock();
3772}
3773
3774void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3775 struct iwl_mvm_sta *mvmsta, bool disable)
3776{
3777 struct iwl_mvm_add_sta_cmd cmd = {
3778 .add_modify = STA_MODE_MODIFY,
3779 .sta_id = mvmsta->sta_id,
3780 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3781 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3782 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3783 };
3784 int ret;
3785
3786 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3787 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3788 if (ret)
3789 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3790}
3791
3792void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3793 struct ieee80211_sta *sta,
3794 bool disable)
3795{
3796 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3797
3798 spin_lock_bh(&mvm_sta->lock);
3799
3800 if (mvm_sta->disable_tx == disable) {
3801 spin_unlock_bh(&mvm_sta->lock);
3802 return;
3803 }
3804
3805 mvm_sta->disable_tx = disable;
3806
3807 /* Tell mac80211 to start/stop queuing tx for this station */
3808 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3809
3810 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3811
3812 spin_unlock_bh(&mvm_sta->lock);
3813}
3814
3815static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3816 struct iwl_mvm_vif *mvmvif,
3817 struct iwl_mvm_int_sta *sta,
3818 bool disable)
3819{
3820 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3821 struct iwl_mvm_add_sta_cmd cmd = {
3822 .add_modify = STA_MODE_MODIFY,
3823 .sta_id = sta->sta_id,
3824 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3825 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3826 .mac_id_n_color = cpu_to_le32(id),
3827 };
3828 int ret;
3829
3830 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3831 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3832 if (ret)
3833 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3834}
3835
3836void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3837 struct iwl_mvm_vif *mvmvif,
3838 bool disable)
3839{
3840 struct ieee80211_sta *sta;
3841 struct iwl_mvm_sta *mvm_sta;
3842 int i;
3843
3844 lockdep_assert_held(&mvm->mutex);
3845
3846 /* Block/unblock all the stations of the given mvmvif */
3847 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3848 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3849 lockdep_is_held(&mvm->mutex));
3850 if (IS_ERR_OR_NULL(sta))
3851 continue;
3852
3853 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3854 if (mvm_sta->mac_id_n_color !=
3855 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3856 continue;
3857
3858 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3859 }
3860
3861 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3862 return;
3863
3864 /* Need to block/unblock also multicast station */
3865 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3866 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3867 &mvmvif->mcast_sta, disable);
3868
3869 /*
3870 * Only unblock the broadcast station (FW blocks it for immediate
3871 * quiet, not the driver)
3872 */
3873 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3874 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3875 &mvmvif->bcast_sta, disable);
3876}
3877
3878void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3879{
3880 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3881 struct iwl_mvm_sta *mvmsta;
3882
3883 rcu_read_lock();
3884
3885 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3886
3887 if (!WARN_ON(!mvmsta))
3888 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3889
3890 rcu_read_unlock();
3891}
3892
3893u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3894{
3895 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3896
3897 /*
3898 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3899 * to align the wrap around of ssn so we compare relevant values.
3900 */
3901 if (mvm->trans->trans_cfg->gen2)
3902 sn &= 0xff;
3903
3904 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3905}