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