blob: 80067eb9ea0510ad17136a496562d581ea8edce6 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
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 *
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03008 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Sara Sharon26d6c162017-01-03 12:00:19 +020010 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * USA
25 *
26 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020027 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010028 *
29 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020030 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010031 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +030035 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Sara Sharon26d6c162017-01-03 12:00:19 +020037 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010038 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
49 * distribution.
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *****************************************************************************/
67#include <net/mac80211.h>
68
69#include "mvm.h"
70#include "sta.h"
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030071#include "rs.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010072
Sara Sharon854c5702016-01-26 13:17:47 +020073/*
74 * New version of ADD_STA_sta command added new fields at the end of the
75 * structure, so sending the size of the relevant API's structure is enough to
76 * support both API versions.
77 */
78static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
79{
Sara Sharonced19f22017-02-06 19:09:32 +020080 if (iwl_mvm_has_new_rx_api(mvm) ||
81 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
82 return sizeof(struct iwl_mvm_add_sta_cmd);
83 else
84 return sizeof(struct iwl_mvm_add_sta_cmd_v7);
Sara Sharon854c5702016-01-26 13:17:47 +020085}
86
Eliad Pellerb92e6612014-01-23 17:58:23 +020087static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
88 enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +010089{
90 int sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +020091 u32 reserved_ids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +010092
Eliad Pellerb92e6612014-01-23 17:58:23 +020093 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
Johannes Berg8ca151b2013-01-24 14:25:36 +010094 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
95
96 lockdep_assert_held(&mvm->mutex);
97
Eliad Pellerb92e6612014-01-23 17:58:23 +020098 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
99 if (iftype != NL80211_IFTYPE_STATION)
100 reserved_ids = BIT(0);
101
Johannes Berg8ca151b2013-01-24 14:25:36 +0100102 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
Sara Sharon0ae98812017-01-04 14:53:58 +0200103 for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200104 if (BIT(sta_id) & reserved_ids)
105 continue;
106
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
108 lockdep_is_held(&mvm->mutex)))
109 return sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +0200110 }
Sara Sharon0ae98812017-01-04 14:53:58 +0200111 return IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100112}
113
Johannes Berg7a453972013-02-12 13:10:44 +0100114/* send station add/update command to firmware */
115int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Liad Kaufman24afba72015-07-28 18:56:08 +0300116 bool update, unsigned int flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100117{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100118 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300119 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
120 .sta_id = mvm_sta->sta_id,
121 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
122 .add_modify = update ? 1 : 0,
123 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
Naftali Goldstein8addabf2017-07-27 04:53:55 +0300124 STA_FLG_MIMO_EN_MSK |
125 STA_FLG_RTS_MIMO_PROT),
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200126 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300127 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100128 int ret;
129 u32 status;
130 u32 agg_size = 0, mpdu_dens = 0;
131
Sara Sharonced19f22017-02-06 19:09:32 +0200132 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
133 add_sta_cmd.station_type = mvm_sta->sta_type;
134
Liad Kaufman24afba72015-07-28 18:56:08 +0300135 if (!update || (flags & STA_MODIFY_QUEUES)) {
Johannes Berg7a453972013-02-12 13:10:44 +0100136 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
Liad Kaufman24afba72015-07-28 18:56:08 +0300137
Sara Sharonbb497012016-09-29 14:52:40 +0300138 if (!iwl_mvm_has_new_tx_api(mvm)) {
139 add_sta_cmd.tfd_queue_msk =
140 cpu_to_le32(mvm_sta->tfd_queue_msk);
141
142 if (flags & STA_MODIFY_QUEUES)
143 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
144 } else {
145 WARN_ON(flags & STA_MODIFY_QUEUES);
146 }
Johannes Berg7a453972013-02-12 13:10:44 +0100147 }
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100148
149 switch (sta->bandwidth) {
150 case IEEE80211_STA_RX_BW_160:
151 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
152 /* fall through */
153 case IEEE80211_STA_RX_BW_80:
154 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
155 /* fall through */
156 case IEEE80211_STA_RX_BW_40:
157 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
158 /* fall through */
159 case IEEE80211_STA_RX_BW_20:
160 if (sta->ht_cap.ht_supported)
161 add_sta_cmd.station_flags |=
162 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
163 break;
164 }
165
166 switch (sta->rx_nss) {
167 case 1:
168 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
169 break;
170 case 2:
171 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
172 break;
173 case 3 ... 8:
174 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
175 break;
176 }
177
178 switch (sta->smps_mode) {
179 case IEEE80211_SMPS_AUTOMATIC:
180 case IEEE80211_SMPS_NUM_MODES:
181 WARN_ON(1);
182 break;
183 case IEEE80211_SMPS_STATIC:
184 /* override NSS */
185 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
186 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
187 break;
188 case IEEE80211_SMPS_DYNAMIC:
189 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
190 break;
191 case IEEE80211_SMPS_OFF:
192 /* nothing */
193 break;
194 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100195
196 if (sta->ht_cap.ht_supported) {
197 add_sta_cmd.station_flags_msk |=
198 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
199 STA_FLG_AGG_MPDU_DENS_MSK);
200
201 mpdu_dens = sta->ht_cap.ampdu_density;
202 }
203
204 if (sta->vht_cap.vht_supported) {
205 agg_size = sta->vht_cap.cap &
206 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
207 agg_size >>=
208 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
209 } else if (sta->ht_cap.ht_supported) {
210 agg_size = sta->ht_cap.ampdu_factor;
211 }
212
213 add_sta_cmd.station_flags |=
214 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
215 add_sta_cmd.station_flags |=
216 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
Johannes Berg6ea29ce2016-12-01 16:25:30 +0100217 if (mvm_sta->associated)
218 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100219
Johannes Berg65e25482016-04-13 14:24:22 +0200220 if (sta->wme) {
221 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
222
223 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200224 add_sta_cmd.uapsd_acs |= BIT(AC_BK);
Johannes Berg65e25482016-04-13 14:24:22 +0200225 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200226 add_sta_cmd.uapsd_acs |= BIT(AC_BE);
Johannes Berg65e25482016-04-13 14:24:22 +0200227 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200228 add_sta_cmd.uapsd_acs |= BIT(AC_VI);
Johannes Berg65e25482016-04-13 14:24:22 +0200229 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200230 add_sta_cmd.uapsd_acs |= BIT(AC_VO);
231 add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
Emmanuel Grumbache71ca5e2017-02-08 14:53:32 +0200232 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
Johannes Berg65e25482016-04-13 14:24:22 +0200233 }
234
Johannes Berg8ca151b2013-01-24 14:25:36 +0100235 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200236 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
237 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300238 &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100239 if (ret)
240 return ret;
241
Sara Sharon837c4da2016-01-07 16:50:45 +0200242 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100243 case ADD_STA_SUCCESS:
244 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
245 break;
246 default:
247 ret = -EIO;
248 IWL_ERR(mvm, "ADD_STA failed\n");
249 break;
250 }
251
252 return ret;
253}
254
Kees Cook8cef5342017-10-24 02:29:37 -0700255static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
Sara Sharon10b2b202016-03-20 16:23:41 +0200256{
Kees Cook8cef5342017-10-24 02:29:37 -0700257 struct iwl_mvm_baid_data *data =
258 from_timer(data, t, session_timer);
259 struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
Sara Sharon10b2b202016-03-20 16:23:41 +0200260 struct iwl_mvm_baid_data *ba_data;
261 struct ieee80211_sta *sta;
262 struct iwl_mvm_sta *mvm_sta;
263 unsigned long timeout;
264
265 rcu_read_lock();
266
267 ba_data = rcu_dereference(*rcu_ptr);
268
269 if (WARN_ON(!ba_data))
270 goto unlock;
271
272 if (!ba_data->timeout)
273 goto unlock;
274
275 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
276 if (time_is_after_jiffies(timeout)) {
277 mod_timer(&ba_data->session_timer, timeout);
278 goto unlock;
279 }
280
281 /* Timer expired */
282 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
Emmanuel Grumbach61dd8a82017-06-12 15:10:09 +0300283
284 /*
285 * sta should be valid unless the following happens:
286 * The firmware asserts which triggers a reconfig flow, but
287 * the reconfig fails before we set the pointer to sta into
288 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
289 * A-MDPU and hence the timer continues to run. Then, the
290 * timer expires and sta is NULL.
291 */
292 if (!sta)
293 goto unlock;
294
Sara Sharon10b2b202016-03-20 16:23:41 +0200295 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Naftali Goldstein20fc6902017-07-11 10:07:32 +0300296 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
297 sta->addr, ba_data->tid);
Sara Sharon10b2b202016-03-20 16:23:41 +0200298unlock:
299 rcu_read_unlock();
300}
301
Liad Kaufman9794c642015-08-19 17:34:28 +0300302/* Disable aggregations for a bitmap of TIDs for a given station */
303static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
304 unsigned long disable_agg_tids,
305 bool remove_queue)
306{
307 struct iwl_mvm_add_sta_cmd cmd = {};
308 struct ieee80211_sta *sta;
309 struct iwl_mvm_sta *mvmsta;
310 u32 status;
311 u8 sta_id;
312 int ret;
313
Sara Sharonbb497012016-09-29 14:52:40 +0300314 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
315 return -EINVAL;
316
Liad Kaufman9794c642015-08-19 17:34:28 +0300317 spin_lock_bh(&mvm->queue_info_lock);
318 sta_id = mvm->queue_info[queue].ra_sta_id;
319 spin_unlock_bh(&mvm->queue_info_lock);
320
321 rcu_read_lock();
322
323 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
324
325 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
326 rcu_read_unlock();
327 return -EINVAL;
328 }
329
330 mvmsta = iwl_mvm_sta_from_mac80211(sta);
331
332 mvmsta->tid_disable_agg |= disable_agg_tids;
333
334 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
335 cmd.sta_id = mvmsta->sta_id;
336 cmd.add_modify = STA_MODE_MODIFY;
337 cmd.modify_mask = STA_MODIFY_QUEUES;
338 if (disable_agg_tids)
339 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
340 if (remove_queue)
341 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
342 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
343 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
344
345 rcu_read_unlock();
346
347 /* Notify FW of queue removal from the STA queues */
348 status = ADD_STA_SUCCESS;
349 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
350 iwl_mvm_add_sta_cmd_size(mvm),
351 &cmd, &status);
352
353 return ret;
354}
355
Liad Kaufman42db09c2016-05-02 14:01:14 +0300356static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
357{
358 struct ieee80211_sta *sta;
359 struct iwl_mvm_sta *mvmsta;
360 unsigned long tid_bitmap;
361 unsigned long agg_tids = 0;
Sharon Dvir806911d2017-05-21 12:09:49 +0300362 u8 sta_id;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300363 int tid;
364
365 lockdep_assert_held(&mvm->mutex);
366
Sara Sharonbb497012016-09-29 14:52:40 +0300367 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
368 return -EINVAL;
369
Liad Kaufman42db09c2016-05-02 14:01:14 +0300370 spin_lock_bh(&mvm->queue_info_lock);
371 sta_id = mvm->queue_info[queue].ra_sta_id;
372 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
373 spin_unlock_bh(&mvm->queue_info_lock);
374
375 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
376 lockdep_is_held(&mvm->mutex));
377
378 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
379 return -EINVAL;
380
381 mvmsta = iwl_mvm_sta_from_mac80211(sta);
382
383 spin_lock_bh(&mvmsta->lock);
384 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
385 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
386 agg_tids |= BIT(tid);
387 }
388 spin_unlock_bh(&mvmsta->lock);
389
390 return agg_tids;
391}
392
Liad Kaufman9794c642015-08-19 17:34:28 +0300393/*
394 * Remove a queue from a station's resources.
395 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
396 * doesn't disable the queue
397 */
398static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
399{
400 struct ieee80211_sta *sta;
401 struct iwl_mvm_sta *mvmsta;
402 unsigned long tid_bitmap;
403 unsigned long disable_agg_tids = 0;
404 u8 sta_id;
405 int tid;
406
407 lockdep_assert_held(&mvm->mutex);
408
Sara Sharonbb497012016-09-29 14:52:40 +0300409 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
410 return -EINVAL;
411
Liad Kaufman9794c642015-08-19 17:34:28 +0300412 spin_lock_bh(&mvm->queue_info_lock);
413 sta_id = mvm->queue_info[queue].ra_sta_id;
414 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
415 spin_unlock_bh(&mvm->queue_info_lock);
416
417 rcu_read_lock();
418
419 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
420
421 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
422 rcu_read_unlock();
423 return 0;
424 }
425
426 mvmsta = iwl_mvm_sta_from_mac80211(sta);
427
428 spin_lock_bh(&mvmsta->lock);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300429 /* Unmap MAC queues and TIDs from this queue */
Liad Kaufman9794c642015-08-19 17:34:28 +0300430 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300431 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
432 disable_agg_tids |= BIT(tid);
Sara Sharon6862fce2017-02-22 19:34:17 +0200433 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman9794c642015-08-19 17:34:28 +0300434 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300435
Liad Kaufman42db09c2016-05-02 14:01:14 +0300436 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
Liad Kaufman9794c642015-08-19 17:34:28 +0300437 spin_unlock_bh(&mvmsta->lock);
438
439 rcu_read_unlock();
440
Liad Kaufman9794c642015-08-19 17:34:28 +0300441 return disable_agg_tids;
442}
443
Sara Sharon01796ff2016-11-16 17:04:36 +0200444static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
445 bool same_sta)
446{
447 struct iwl_mvm_sta *mvmsta;
448 u8 txq_curr_ac, sta_id, tid;
449 unsigned long disable_agg_tids = 0;
450 int ret;
451
452 lockdep_assert_held(&mvm->mutex);
453
Sara Sharonbb497012016-09-29 14:52:40 +0300454 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
455 return -EINVAL;
456
Sara Sharon01796ff2016-11-16 17:04:36 +0200457 spin_lock_bh(&mvm->queue_info_lock);
458 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
459 sta_id = mvm->queue_info[queue].ra_sta_id;
460 tid = mvm->queue_info[queue].txq_tid;
461 spin_unlock_bh(&mvm->queue_info_lock);
462
463 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Sharon Dvire3df1e42017-02-21 10:41:31 +0200464 if (WARN_ON(!mvmsta))
465 return -EINVAL;
Sara Sharon01796ff2016-11-16 17:04:36 +0200466
467 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
468 /* Disable the queue */
469 if (disable_agg_tids)
470 iwl_mvm_invalidate_sta_queue(mvm, queue,
471 disable_agg_tids, false);
472
473 ret = iwl_mvm_disable_txq(mvm, queue,
474 mvmsta->vif->hw_queue[txq_curr_ac],
475 tid, 0);
476 if (ret) {
477 /* Re-mark the inactive queue as inactive */
478 spin_lock_bh(&mvm->queue_info_lock);
479 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
480 spin_unlock_bh(&mvm->queue_info_lock);
481 IWL_ERR(mvm,
482 "Failed to free inactive queue %d (ret=%d)\n",
483 queue, ret);
484
485 return ret;
486 }
487
488 /* If TXQ is allocated to another STA, update removal in FW */
489 if (!same_sta)
490 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
491
492 return 0;
493}
494
Liad Kaufman42db09c2016-05-02 14:01:14 +0300495static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
496 unsigned long tfd_queue_mask, u8 ac)
497{
498 int queue = 0;
499 u8 ac_to_queue[IEEE80211_NUM_ACS];
500 int i;
501
502 lockdep_assert_held(&mvm->queue_info_lock);
Sara Sharonbb497012016-09-29 14:52:40 +0300503 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
504 return -EINVAL;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300505
506 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
507
508 /* See what ACs the existing queues for this STA have */
509 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
510 /* Only DATA queues can be shared */
511 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
512 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
513 continue;
514
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200515 /* Don't try and take queues being reconfigured */
516 if (mvm->queue_info[queue].status ==
517 IWL_MVM_QUEUE_RECONFIGURING)
518 continue;
519
Liad Kaufman42db09c2016-05-02 14:01:14 +0300520 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
521 }
522
523 /*
524 * The queue to share is chosen only from DATA queues as follows (in
525 * descending priority):
526 * 1. An AC_BE queue
527 * 2. Same AC queue
528 * 3. Highest AC queue that is lower than new AC
529 * 4. Any existing AC (there always is at least 1 DATA queue)
530 */
531
532 /* Priority 1: An AC_BE queue */
533 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
534 queue = ac_to_queue[IEEE80211_AC_BE];
535 /* Priority 2: Same AC queue */
536 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
537 queue = ac_to_queue[ac];
538 /* Priority 3a: If new AC is VO and VI exists - use VI */
539 else if (ac == IEEE80211_AC_VO &&
540 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
541 queue = ac_to_queue[IEEE80211_AC_VI];
542 /* Priority 3b: No BE so only AC less than the new one is BK */
543 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
544 queue = ac_to_queue[IEEE80211_AC_BK];
545 /* Priority 4a: No BE nor BK - use VI if exists */
546 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
547 queue = ac_to_queue[IEEE80211_AC_VI];
548 /* Priority 4b: No BE, BK nor VI - use VO if exists */
549 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
550 queue = ac_to_queue[IEEE80211_AC_VO];
551
552 /* Make sure queue found (or not) is legal */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200553 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
554 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
555 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
Liad Kaufman42db09c2016-05-02 14:01:14 +0300556 IWL_ERR(mvm, "No DATA queues available to share\n");
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200557 return -ENOSPC;
558 }
559
560 /* Make sure the queue isn't in the middle of being reconfigured */
561 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
562 IWL_ERR(mvm,
563 "TXQ %d is in the middle of re-config - try again\n",
564 queue);
565 return -EBUSY;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300566 }
567
568 return queue;
569}
570
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200571/*
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200572 * If a given queue has a higher AC than the TID stream that is being compared
573 * to, the queue needs to be redirected to the lower AC. This function does that
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200574 * in such a case, otherwise - if no redirection required - it does nothing,
575 * unless the %force param is true.
576 */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200577int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
578 int ac, int ssn, unsigned int wdg_timeout,
579 bool force)
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200580{
581 struct iwl_scd_txq_cfg_cmd cmd = {
582 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200583 .action = SCD_CFG_DISABLE_QUEUE,
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200584 };
585 bool shared_queue;
586 unsigned long mq;
587 int ret;
588
Sara Sharonbb497012016-09-29 14:52:40 +0300589 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
590 return -EINVAL;
591
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200592 /*
593 * If the AC is lower than current one - FIFO needs to be redirected to
594 * the lowest one of the streams in the queue. Check if this is needed
595 * here.
596 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
597 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
598 * we need to check if the numerical value of X is LARGER than of Y.
599 */
600 spin_lock_bh(&mvm->queue_info_lock);
601 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
602 spin_unlock_bh(&mvm->queue_info_lock);
603
604 IWL_DEBUG_TX_QUEUES(mvm,
605 "No redirection needed on TXQ #%d\n",
606 queue);
607 return 0;
608 }
609
610 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
611 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200612 cmd.tid = mvm->queue_info[queue].txq_tid;
Sara Sharon34e10862017-02-23 13:15:07 +0200613 mq = mvm->hw_queue_to_mac80211[queue];
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200614 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
615 spin_unlock_bh(&mvm->queue_info_lock);
616
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200617 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200618 queue, iwl_mvm_ac_to_tx_fifo[ac]);
619
620 /* Stop MAC queues and wait for this queue to empty */
621 iwl_mvm_stop_mac_queues(mvm, mq);
Sara Sharona1a57872017-03-05 11:38:58 +0200622 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200623 if (ret) {
624 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
625 queue);
626 ret = -EIO;
627 goto out;
628 }
629
630 /* Before redirecting the queue we need to de-activate it */
631 iwl_trans_txq_disable(mvm->trans, queue, false);
632 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
633 if (ret)
634 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
635 ret);
636
637 /* Make sure the SCD wrptr is correctly set before reconfiguring */
Sara Sharonca3b9c62016-06-30 16:14:02 +0300638 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200639
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200640 /* Update the TID "owner" of the queue */
641 spin_lock_bh(&mvm->queue_info_lock);
642 mvm->queue_info[queue].txq_tid = tid;
643 spin_unlock_bh(&mvm->queue_info_lock);
644
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200645 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
646
647 /* Redirect to lower AC */
648 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
Sara Sharon0ec9257b2017-10-16 09:45:10 +0300649 cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200650
651 /* Update AC marking of the queue */
652 spin_lock_bh(&mvm->queue_info_lock);
653 mvm->queue_info[queue].mac80211_ac = ac;
654 spin_unlock_bh(&mvm->queue_info_lock);
655
656 /*
657 * Mark queue as shared in transport if shared
658 * Note this has to be done after queue enablement because enablement
659 * can also set this value, and there is no indication there to shared
660 * queues
661 */
662 if (shared_queue)
663 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
664
665out:
666 /* Continue using the MAC queues */
667 iwl_mvm_start_mac_queues(mvm, mq);
668
669 return ret;
670}
671
Sara Sharon310181e2017-01-17 14:27:48 +0200672static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
673 struct ieee80211_sta *sta, u8 ac,
674 int tid)
675{
676 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
677 unsigned int wdg_timeout =
678 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
679 u8 mac_queue = mvmsta->vif->hw_queue[ac];
680 int queue = -1;
681
682 lockdep_assert_held(&mvm->mutex);
683
684 IWL_DEBUG_TX_QUEUES(mvm,
685 "Allocating queue for sta %d on tid %d\n",
686 mvmsta->sta_id, tid);
687 queue = iwl_mvm_tvqm_enable_txq(mvm, mac_queue, mvmsta->sta_id, tid,
688 wdg_timeout);
689 if (queue < 0)
690 return queue;
691
692 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
693
694 spin_lock_bh(&mvmsta->lock);
695 mvmsta->tid_data[tid].txq_id = queue;
696 mvmsta->tid_data[tid].is_tid_active = true;
Sara Sharon310181e2017-01-17 14:27:48 +0200697 spin_unlock_bh(&mvmsta->lock);
698
Sara Sharon310181e2017-01-17 14:27:48 +0200699 return 0;
700}
701
Liad Kaufman24afba72015-07-28 18:56:08 +0300702static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
703 struct ieee80211_sta *sta, u8 ac, int tid,
704 struct ieee80211_hdr *hdr)
705{
706 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
707 struct iwl_trans_txq_scd_cfg cfg = {
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +0300708 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
Liad Kaufman24afba72015-07-28 18:56:08 +0300709 .sta_id = mvmsta->sta_id,
710 .tid = tid,
711 .frame_limit = IWL_FRAME_LIMIT,
712 };
713 unsigned int wdg_timeout =
714 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
715 u8 mac_queue = mvmsta->vif->hw_queue[ac];
716 int queue = -1;
Sara Sharon01796ff2016-11-16 17:04:36 +0200717 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman9794c642015-08-19 17:34:28 +0300718 unsigned long disable_agg_tids = 0;
719 enum iwl_mvm_agg_state queue_state;
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300720 bool shared_queue = false, inc_ssn;
Liad Kaufman24afba72015-07-28 18:56:08 +0300721 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300722 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300723 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300724
725 lockdep_assert_held(&mvm->mutex);
726
Sara Sharon310181e2017-01-17 14:27:48 +0200727 if (iwl_mvm_has_new_tx_api(mvm))
728 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
729
Liad Kaufman42db09c2016-05-02 14:01:14 +0300730 spin_lock_bh(&mvmsta->lock);
731 tfd_queue_mask = mvmsta->tfd_queue_msk;
732 spin_unlock_bh(&mvmsta->lock);
733
Liad Kaufmand2515a92016-03-23 16:31:08 +0200734 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300735
736 /*
737 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
738 * exists
739 */
740 if (!ieee80211_is_data_qos(hdr->frame_control) ||
741 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300742 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
743 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300744 IWL_MVM_DQA_MAX_MGMT_QUEUE);
745 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
746 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
747 queue);
748
749 /* If no such queue is found, we'll use a DATA queue instead */
750 }
751
Liad Kaufman9794c642015-08-19 17:34:28 +0300752 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
753 (mvm->queue_info[mvmsta->reserved_queue].status ==
754 IWL_MVM_QUEUE_RESERVED ||
755 mvm->queue_info[mvmsta->reserved_queue].status ==
756 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300757 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300758 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300759 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
760 }
761
762 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300763 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
764 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300765 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300766
767 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300768 * Check if this queue is already allocated but inactive.
769 * In such a case, we'll need to first free this queue before enabling
770 * it again, so we'll mark it as reserved to make sure no new traffic
771 * arrives on it
772 */
773 if (queue > 0 &&
774 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
775 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
776 using_inactive_queue = true;
Sara Sharon01796ff2016-11-16 17:04:36 +0200777 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman9794c642015-08-19 17:34:28 +0300778 IWL_DEBUG_TX_QUEUES(mvm,
779 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
780 queue, mvmsta->sta_id, tid);
781 }
782
Liad Kaufman42db09c2016-05-02 14:01:14 +0300783 /* No free queue - we'll have to share */
784 if (queue <= 0) {
785 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
786 if (queue > 0) {
787 shared_queue = true;
788 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
789 }
790 }
791
Liad Kaufman9794c642015-08-19 17:34:28 +0300792 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300793 * Mark TXQ as ready, even though it hasn't been fully configured yet,
794 * to make sure no one else takes it.
795 * This will allow avoiding re-acquiring the lock at the end of the
796 * configuration. On error we'll mark it back as free.
797 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300798 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300799 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300800
Liad Kaufmand2515a92016-03-23 16:31:08 +0200801 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300802
Liad Kaufman42db09c2016-05-02 14:01:14 +0300803 /* This shouldn't happen - out of queues */
804 if (WARN_ON(queue <= 0)) {
805 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
806 tid, cfg.sta_id);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200807 return queue;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300808 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300809
810 /*
811 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
812 * but for configuring the SCD to send A-MPDUs we need to mark the queue
813 * as aggregatable.
814 * Mark all DATA queues as allowing to be aggregated at some point
815 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300816 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
817 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300818
Liad Kaufman9794c642015-08-19 17:34:28 +0300819 /*
820 * If this queue was previously inactive (idle) - we need to free it
821 * first
822 */
823 if (using_inactive_queue) {
Sara Sharon01796ff2016-11-16 17:04:36 +0200824 ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
825 if (ret)
Liad Kaufman9794c642015-08-19 17:34:28 +0300826 return ret;
Liad Kaufman9794c642015-08-19 17:34:28 +0300827 }
828
Liad Kaufman42db09c2016-05-02 14:01:14 +0300829 IWL_DEBUG_TX_QUEUES(mvm,
830 "Allocating %squeue #%d to sta %d on tid %d\n",
831 shared_queue ? "shared " : "", queue,
832 mvmsta->sta_id, tid);
833
834 if (shared_queue) {
835 /* Disable any open aggs on this queue */
836 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
837
838 if (disable_agg_tids) {
839 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
840 queue);
841 iwl_mvm_invalidate_sta_queue(mvm, queue,
842 disable_agg_tids, false);
843 }
Liad Kaufman42db09c2016-05-02 14:01:14 +0300844 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300845
846 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300847 inc_ssn = iwl_mvm_enable_txq(mvm, queue, mac_queue,
848 ssn, &cfg, wdg_timeout);
849 if (inc_ssn) {
850 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
851 le16_add_cpu(&hdr->seq_ctrl, 0x10);
852 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300853
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200854 /*
855 * Mark queue as shared in transport if shared
856 * Note this has to be done after queue enablement because enablement
857 * can also set this value, and there is no indication there to shared
858 * queues
859 */
860 if (shared_queue)
861 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
862
Liad Kaufman24afba72015-07-28 18:56:08 +0300863 spin_lock_bh(&mvmsta->lock);
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300864 /*
865 * This looks racy, but it is not. We have only one packet for
866 * this ra/tid in our Tx path since we stop the Qdisc when we
867 * need to allocate a new TFD queue.
868 */
869 if (inc_ssn)
870 mvmsta->tid_data[tid].seq_number += 0x10;
Liad Kaufman24afba72015-07-28 18:56:08 +0300871 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300872 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300873 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300874 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300875
876 if (mvmsta->reserved_queue == queue)
877 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
878 spin_unlock_bh(&mvmsta->lock);
879
Liad Kaufman42db09c2016-05-02 14:01:14 +0300880 if (!shared_queue) {
881 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
882 if (ret)
883 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300884
Liad Kaufman42db09c2016-05-02 14:01:14 +0300885 /* If we need to re-enable aggregations... */
886 if (queue_state == IWL_AGG_ON) {
887 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
888 if (ret)
889 goto out_err;
890 }
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200891 } else {
892 /* Redirect queue, if needed */
893 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
894 wdg_timeout, false);
895 if (ret)
896 goto out_err;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300897 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300898
Liad Kaufman42db09c2016-05-02 14:01:14 +0300899 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300900
901out_err:
902 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
903
904 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300905}
906
Liad Kaufman19aefa42016-03-08 14:29:51 +0200907static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
908{
909 struct iwl_scd_txq_cfg_cmd cmd = {
910 .scd_queue = queue,
911 .action = SCD_CFG_UPDATE_QUEUE_TID,
912 };
Liad Kaufman19aefa42016-03-08 14:29:51 +0200913 int tid;
914 unsigned long tid_bitmap;
915 int ret;
916
917 lockdep_assert_held(&mvm->mutex);
918
Sara Sharonbb497012016-09-29 14:52:40 +0300919 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
920 return;
921
Liad Kaufman19aefa42016-03-08 14:29:51 +0200922 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200923 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
924 spin_unlock_bh(&mvm->queue_info_lock);
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);
Liad Kaufman341ca402016-09-18 14:51:59 +0300935 if (ret) {
Liad Kaufman19aefa42016-03-08 14:29:51 +0200936 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
937 queue, ret);
Liad Kaufman341ca402016-09-18 14:51:59 +0300938 return;
939 }
940
941 spin_lock_bh(&mvm->queue_info_lock);
942 mvm->queue_info[queue].txq_tid = tid;
943 spin_unlock_bh(&mvm->queue_info_lock);
944 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
945 queue, tid);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200946}
947
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200948static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
949{
950 struct ieee80211_sta *sta;
951 struct iwl_mvm_sta *mvmsta;
Sharon Dvir806911d2017-05-21 12:09:49 +0300952 u8 sta_id;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200953 int tid = -1;
954 unsigned long tid_bitmap;
955 unsigned int wdg_timeout;
956 int ssn;
957 int ret = true;
958
Sara Sharonbb497012016-09-29 14:52:40 +0300959 /* queue sharing is disabled on new TX path */
960 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
961 return;
962
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200963 lockdep_assert_held(&mvm->mutex);
964
965 spin_lock_bh(&mvm->queue_info_lock);
966 sta_id = mvm->queue_info[queue].ra_sta_id;
967 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
968 spin_unlock_bh(&mvm->queue_info_lock);
969
970 /* Find TID for queue, and make sure it is the only one on the queue */
971 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
972 if (tid_bitmap != BIT(tid)) {
973 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
974 queue, tid_bitmap);
975 return;
976 }
977
978 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
979 tid);
980
981 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
982 lockdep_is_held(&mvm->mutex));
983
984 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
985 return;
986
987 mvmsta = iwl_mvm_sta_from_mac80211(sta);
988 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
989
990 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
991
992 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
993 tid_to_mac80211_ac[tid], ssn,
994 wdg_timeout, true);
995 if (ret) {
996 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
997 return;
998 }
999
1000 /* If aggs should be turned back on - do it */
1001 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
Emmanuel Grumbach9cd70e82016-09-20 13:40:33 +03001002 struct iwl_mvm_add_sta_cmd cmd = {0};
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001003
1004 mvmsta->tid_disable_agg &= ~BIT(tid);
1005
1006 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1007 cmd.sta_id = mvmsta->sta_id;
1008 cmd.add_modify = STA_MODE_MODIFY;
1009 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1010 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1011 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1012
1013 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1014 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1015 if (!ret) {
1016 IWL_DEBUG_TX_QUEUES(mvm,
1017 "TXQ #%d is now aggregated again\n",
1018 queue);
1019
1020 /* Mark queue intenally as aggregating again */
1021 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1022 }
1023 }
1024
1025 spin_lock_bh(&mvm->queue_info_lock);
1026 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1027 spin_unlock_bh(&mvm->queue_info_lock);
1028}
1029
Liad Kaufman24afba72015-07-28 18:56:08 +03001030static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1031{
1032 if (tid == IWL_MAX_TID_COUNT)
1033 return IEEE80211_AC_VO; /* MGMT */
1034
1035 return tid_to_mac80211_ac[tid];
1036}
1037
1038static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
1039 struct ieee80211_sta *sta, int tid)
1040{
1041 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1042 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1043 struct sk_buff *skb;
1044 struct ieee80211_hdr *hdr;
1045 struct sk_buff_head deferred_tx;
1046 u8 mac_queue;
1047 bool no_queue = false; /* Marks if there is a problem with the queue */
1048 u8 ac;
1049
1050 lockdep_assert_held(&mvm->mutex);
1051
1052 skb = skb_peek(&tid_data->deferred_tx_frames);
1053 if (!skb)
1054 return;
1055 hdr = (void *)skb->data;
1056
1057 ac = iwl_mvm_tid_to_ac_queue(tid);
1058 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
1059
Sara Sharon6862fce2017-02-22 19:34:17 +02001060 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE &&
Liad Kaufman24afba72015-07-28 18:56:08 +03001061 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
1062 IWL_ERR(mvm,
1063 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
1064 mvmsta->sta_id, tid);
1065
1066 /*
1067 * Mark queue as problematic so later the deferred traffic is
1068 * freed, as we can do nothing with it
1069 */
1070 no_queue = true;
1071 }
1072
1073 __skb_queue_head_init(&deferred_tx);
1074
Liad Kaufmand2515a92016-03-23 16:31:08 +02001075 /* Disable bottom-halves when entering TX path */
1076 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +03001077 spin_lock(&mvmsta->lock);
1078 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
Liad Kaufmanad5de732016-09-27 16:01:10 +03001079 mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
Liad Kaufman24afba72015-07-28 18:56:08 +03001080 spin_unlock(&mvmsta->lock);
1081
Liad Kaufman24afba72015-07-28 18:56:08 +03001082 while ((skb = __skb_dequeue(&deferred_tx)))
1083 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1084 ieee80211_free_txskb(mvm->hw, skb);
1085 local_bh_enable();
1086
1087 /* Wake queue */
1088 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1089}
1090
1091void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1092{
1093 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1094 add_stream_wk);
1095 struct ieee80211_sta *sta;
1096 struct iwl_mvm_sta *mvmsta;
1097 unsigned long deferred_tid_traffic;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001098 int queue, sta_id, tid;
Liad Kaufman24afba72015-07-28 18:56:08 +03001099
Liad Kaufman9794c642015-08-19 17:34:28 +03001100 /* Check inactivity of queues */
1101 iwl_mvm_inactivity_check(mvm);
1102
Liad Kaufman24afba72015-07-28 18:56:08 +03001103 mutex_lock(&mvm->mutex);
1104
Sara Sharon34e10862017-02-23 13:15:07 +02001105 /* No queue reconfiguration in TVQM mode */
1106 if (iwl_mvm_has_new_tx_api(mvm))
1107 goto alloc_queues;
1108
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001109 /* Reconfigure queues requiring reconfiguation */
Sara Sharon34e10862017-02-23 13:15:07 +02001110 for (queue = 0; queue < ARRAY_SIZE(mvm->queue_info); queue++) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001111 bool reconfig;
Liad Kaufman19aefa42016-03-08 14:29:51 +02001112 bool change_owner;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001113
1114 spin_lock_bh(&mvm->queue_info_lock);
1115 reconfig = (mvm->queue_info[queue].status ==
1116 IWL_MVM_QUEUE_RECONFIGURING);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001117
1118 /*
1119 * We need to take into account a situation in which a TXQ was
1120 * allocated to TID x, and then turned shared by adding TIDs y
1121 * and z. If TID x becomes inactive and is removed from the TXQ,
1122 * ownership must be given to one of the remaining TIDs.
1123 * This is mainly because if TID x continues - a new queue can't
1124 * be allocated for it as long as it is an owner of another TXQ.
1125 */
1126 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1127 BIT(mvm->queue_info[queue].txq_tid)) &&
1128 (mvm->queue_info[queue].status ==
1129 IWL_MVM_QUEUE_SHARED);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001130 spin_unlock_bh(&mvm->queue_info_lock);
1131
1132 if (reconfig)
1133 iwl_mvm_unshare_queue(mvm, queue);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001134 else if (change_owner)
1135 iwl_mvm_change_queue_owner(mvm, queue);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001136 }
1137
Sara Sharon34e10862017-02-23 13:15:07 +02001138alloc_queues:
Liad Kaufman24afba72015-07-28 18:56:08 +03001139 /* Go over all stations with deferred traffic */
1140 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1141 IWL_MVM_STATION_COUNT) {
1142 clear_bit(sta_id, mvm->sta_deferred_frames);
1143 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1144 lockdep_is_held(&mvm->mutex));
1145 if (IS_ERR_OR_NULL(sta))
1146 continue;
1147
1148 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1149 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1150
1151 for_each_set_bit(tid, &deferred_tid_traffic,
1152 IWL_MAX_TID_COUNT + 1)
1153 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1154 }
1155
1156 mutex_unlock(&mvm->mutex);
1157}
1158
1159static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001160 struct ieee80211_sta *sta,
1161 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +03001162{
1163 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1164 int queue;
Sara Sharon01796ff2016-11-16 17:04:36 +02001165 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman24afba72015-07-28 18:56:08 +03001166
Sara Sharon396952e2017-02-22 19:40:55 +02001167 /* queue reserving is disabled on new TX path */
1168 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1169 return 0;
1170
Liad Kaufman9794c642015-08-19 17:34:28 +03001171 /*
1172 * Check for inactive queues, so we don't reach a situation where we
1173 * can't add a STA due to a shortage in queues that doesn't really exist
1174 */
1175 iwl_mvm_inactivity_check(mvm);
1176
Liad Kaufman24afba72015-07-28 18:56:08 +03001177 spin_lock_bh(&mvm->queue_info_lock);
1178
1179 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +03001180 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1181 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +03001182 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1183 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +03001184 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1185 else
Liad Kaufman9794c642015-08-19 17:34:28 +03001186 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1187 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001188 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +03001189 if (queue < 0) {
1190 spin_unlock_bh(&mvm->queue_info_lock);
1191 IWL_ERR(mvm, "No available queues for new station\n");
1192 return -ENOSPC;
Sara Sharon01796ff2016-11-16 17:04:36 +02001193 } else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
1194 /*
1195 * If this queue is already allocated but inactive we'll need to
1196 * first free this queue before enabling it again, we'll mark
1197 * it as reserved to make sure no new traffic arrives on it
1198 */
1199 using_inactive_queue = true;
1200 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman24afba72015-07-28 18:56:08 +03001201 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03001202 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +03001203
1204 spin_unlock_bh(&mvm->queue_info_lock);
1205
1206 mvmsta->reserved_queue = queue;
1207
Sara Sharon01796ff2016-11-16 17:04:36 +02001208 if (using_inactive_queue)
1209 iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
1210
Liad Kaufman24afba72015-07-28 18:56:08 +03001211 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1212 queue, mvmsta->sta_id);
1213
1214 return 0;
1215}
1216
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001217/*
1218 * In DQA mode, after a HW restart the queues should be allocated as before, in
1219 * order to avoid race conditions when there are shared queues. This function
1220 * does the re-mapping and queue allocation.
1221 *
1222 * Note that re-enabling aggregations isn't done in this function.
1223 */
1224static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1225 struct iwl_mvm_sta *mvm_sta)
1226{
1227 unsigned int wdg_timeout =
1228 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1229 int i;
1230 struct iwl_trans_txq_scd_cfg cfg = {
1231 .sta_id = mvm_sta->sta_id,
1232 .frame_limit = IWL_FRAME_LIMIT,
1233 };
1234
Johannes Berg03c902b2016-12-02 12:03:36 +01001235 /* Make sure reserved queue is still marked as such (if allocated) */
1236 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1237 mvm->queue_info[mvm_sta->reserved_queue].status =
1238 IWL_MVM_QUEUE_RESERVED;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001239
1240 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1241 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1242 int txq_id = tid_data->txq_id;
1243 int ac;
1244 u8 mac_queue;
1245
Sara Sharon6862fce2017-02-22 19:34:17 +02001246 if (txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001247 continue;
1248
1249 skb_queue_head_init(&tid_data->deferred_tx_frames);
1250
1251 ac = tid_to_mac80211_ac[i];
1252 mac_queue = mvm_sta->vif->hw_queue[ac];
1253
Sara Sharon310181e2017-01-17 14:27:48 +02001254 if (iwl_mvm_has_new_tx_api(mvm)) {
1255 IWL_DEBUG_TX_QUEUES(mvm,
1256 "Re-mapping sta %d tid %d\n",
1257 mvm_sta->sta_id, i);
1258 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mac_queue,
1259 mvm_sta->sta_id,
1260 i, wdg_timeout);
1261 tid_data->txq_id = txq_id;
Liad Kaufman5d390512017-10-17 16:26:00 +03001262
1263 /*
1264 * Since we don't set the seq number after reset, and HW
1265 * sets it now, FW reset will cause the seq num to start
1266 * at 0 again, so driver will need to update it
1267 * internally as well, so it keeps in sync with real val
1268 */
1269 tid_data->seq_number = 0;
Sara Sharon310181e2017-01-17 14:27:48 +02001270 } else {
1271 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001272
Sara Sharon310181e2017-01-17 14:27:48 +02001273 cfg.tid = i;
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +03001274 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
Sara Sharon310181e2017-01-17 14:27:48 +02001275 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1276 txq_id ==
1277 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001278
Sara Sharon310181e2017-01-17 14:27:48 +02001279 IWL_DEBUG_TX_QUEUES(mvm,
1280 "Re-mapping sta %d tid %d to queue %d\n",
1281 mvm_sta->sta_id, i, txq_id);
1282
1283 iwl_mvm_enable_txq(mvm, txq_id, mac_queue, seq, &cfg,
1284 wdg_timeout);
Sara Sharon34e10862017-02-23 13:15:07 +02001285 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
Sara Sharon310181e2017-01-17 14:27:48 +02001286 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001287 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001288}
1289
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001290static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1291 struct iwl_mvm_int_sta *sta,
1292 const u8 *addr,
1293 u16 mac_id, u16 color)
1294{
1295 struct iwl_mvm_add_sta_cmd cmd;
1296 int ret;
Luca Coelho3f497de2017-09-02 11:05:22 +03001297 u32 status = ADD_STA_SUCCESS;
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001298
1299 lockdep_assert_held(&mvm->mutex);
1300
1301 memset(&cmd, 0, sizeof(cmd));
1302 cmd.sta_id = sta->sta_id;
1303 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1304 color));
1305 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1306 cmd.station_type = sta->type;
1307
1308 if (!iwl_mvm_has_new_tx_api(mvm))
1309 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1310 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1311
1312 if (addr)
1313 memcpy(cmd.addr, addr, ETH_ALEN);
1314
1315 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1316 iwl_mvm_add_sta_cmd_size(mvm),
1317 &cmd, &status);
1318 if (ret)
1319 return ret;
1320
1321 switch (status & IWL_ADD_STA_STATUS_MASK) {
1322 case ADD_STA_SUCCESS:
1323 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1324 return 0;
1325 default:
1326 ret = -EIO;
1327 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1328 status);
1329 break;
1330 }
1331 return ret;
1332}
1333
Johannes Berg8ca151b2013-01-24 14:25:36 +01001334int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1335 struct ieee80211_vif *vif,
1336 struct ieee80211_sta *sta)
1337{
1338 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001339 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001340 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001341 int i, ret, sta_id;
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001342 bool sta_update = false;
1343 unsigned int sta_flags = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001344
1345 lockdep_assert_held(&mvm->mutex);
1346
1347 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001348 sta_id = iwl_mvm_find_free_sta_id(mvm,
1349 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001350 else
1351 sta_id = mvm_sta->sta_id;
1352
Sara Sharon0ae98812017-01-04 14:53:58 +02001353 if (sta_id == IWL_MVM_INVALID_STA)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001354 return -ENOSPC;
1355
1356 spin_lock_init(&mvm_sta->lock);
1357
Johannes Bergc8f54702017-06-19 23:50:31 +02001358 /* if this is a HW restart re-alloc existing queues */
1359 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001360 struct iwl_mvm_int_sta tmp_sta = {
1361 .sta_id = sta_id,
1362 .type = mvm_sta->sta_type,
1363 };
1364
1365 /*
1366 * First add an empty station since allocating
1367 * a queue requires a valid station
1368 */
1369 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1370 mvmvif->id, mvmvif->color);
1371 if (ret)
1372 goto err;
1373
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001374 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001375 sta_update = true;
1376 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001377 goto update_fw;
1378 }
1379
Johannes Berg8ca151b2013-01-24 14:25:36 +01001380 mvm_sta->sta_id = sta_id;
1381 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1382 mvmvif->color);
1383 mvm_sta->vif = vif;
Liad Kaufmana58bb462017-05-28 14:20:04 +03001384 if (!mvm->trans->cfg->gen2)
1385 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1386 else
1387 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001388 mvm_sta->tx_protection = 0;
1389 mvm_sta->tt_tx_protection = false;
Sara Sharonced19f22017-02-06 19:09:32 +02001390 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001391
1392 /* HW restart, don't assume the memory has been zeroed */
Liad Kaufman69191af2015-09-01 18:50:22 +03001393 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001394 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001395
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001396 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001397 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001398 u16 seq = mvm_sta->tid_data[i].seq_number;
1399 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1400 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001401
Liad Kaufman24afba72015-07-28 18:56:08 +03001402 /*
1403 * Mark all queues for this STA as unallocated and defer TX
1404 * frames until the queue is allocated
1405 */
Sara Sharon6862fce2017-02-22 19:34:17 +02001406 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001407 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001408 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001409 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001410 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001411
Sara Sharona571f5f2015-12-07 12:50:58 +02001412 if (iwl_mvm_has_new_rx_api(mvm) &&
1413 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Johannes Berg92c4dca2017-06-07 10:35:54 +02001414 int q;
1415
Sara Sharona571f5f2015-12-07 12:50:58 +02001416 dup_data = kcalloc(mvm->trans->num_rx_queues,
Johannes Berg92c4dca2017-06-07 10:35:54 +02001417 sizeof(*dup_data), GFP_KERNEL);
Sara Sharona571f5f2015-12-07 12:50:58 +02001418 if (!dup_data)
1419 return -ENOMEM;
Johannes Berg92c4dca2017-06-07 10:35:54 +02001420 /*
1421 * Initialize all the last_seq values to 0xffff which can never
1422 * compare equal to the frame's seq_ctrl in the check in
1423 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1424 * number and fragmented packets don't reach that function.
1425 *
1426 * This thus allows receiving a packet with seqno 0 and the
1427 * retry bit set as the very first packet on a new TID.
1428 */
1429 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1430 memset(dup_data[q].last_seq, 0xff,
1431 sizeof(dup_data[q].last_seq));
Sara Sharona571f5f2015-12-07 12:50:58 +02001432 mvm_sta->dup_data = dup_data;
1433 }
1434
Johannes Bergc8f54702017-06-19 23:50:31 +02001435 if (!iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001436 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1437 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001438 if (ret)
1439 goto err;
1440 }
1441
Gregory Greenman9f66a392017-11-05 18:49:48 +02001442 /*
1443 * if rs is registered with mac80211, then "add station" will be handled
1444 * via the corresponding ops, otherwise need to notify rate scaling here
1445 */
Emmanuel Grumbach4243edb2017-12-13 11:38:48 +02001446 if (iwl_mvm_has_tlc_offload(mvm))
Gregory Greenman9f66a392017-11-05 18:49:48 +02001447 iwl_mvm_rs_add_sta(mvm, mvm_sta);
1448
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001449update_fw:
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001450 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001451 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001452 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001453
Johannes Berg9e848012014-08-04 14:33:42 +02001454 if (vif->type == NL80211_IFTYPE_STATION) {
1455 if (!sta->tdls) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001456 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001457 mvmvif->ap_sta_id = sta_id;
1458 } else {
Sara Sharon0ae98812017-01-04 14:53:58 +02001459 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001460 }
1461 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001462
1463 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1464
1465 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001466
1467err:
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001468 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001469}
1470
1471int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1472 bool drain)
1473{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001474 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001475 int ret;
1476 u32 status;
1477
1478 lockdep_assert_held(&mvm->mutex);
1479
1480 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1481 cmd.sta_id = mvmsta->sta_id;
1482 cmd.add_modify = STA_MODE_MODIFY;
1483 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1484 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1485
1486 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001487 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1488 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001489 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001490 if (ret)
1491 return ret;
1492
Sara Sharon837c4da2016-01-07 16:50:45 +02001493 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001494 case ADD_STA_SUCCESS:
1495 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1496 mvmsta->sta_id);
1497 break;
1498 default:
1499 ret = -EIO;
1500 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1501 mvmsta->sta_id);
1502 break;
1503 }
1504
1505 return ret;
1506}
1507
1508/*
1509 * Remove a station from the FW table. Before sending the command to remove
1510 * the station validate that the station is indeed known to the driver (sanity
1511 * only).
1512 */
1513static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1514{
1515 struct ieee80211_sta *sta;
1516 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1517 .sta_id = sta_id,
1518 };
1519 int ret;
1520
1521 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1522 lockdep_is_held(&mvm->mutex));
1523
1524 /* Note: internal stations are marked as error values */
1525 if (!sta) {
1526 IWL_ERR(mvm, "Invalid station id\n");
1527 return -EINVAL;
1528 }
1529
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001530 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001531 sizeof(rm_sta_cmd), &rm_sta_cmd);
1532 if (ret) {
1533 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1534 return ret;
1535 }
1536
1537 return 0;
1538}
1539
Liad Kaufman24afba72015-07-28 18:56:08 +03001540static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1541 struct ieee80211_vif *vif,
1542 struct iwl_mvm_sta *mvm_sta)
1543{
1544 int ac;
1545 int i;
1546
1547 lockdep_assert_held(&mvm->mutex);
1548
1549 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
Sara Sharon6862fce2017-02-22 19:34:17 +02001550 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman24afba72015-07-28 18:56:08 +03001551 continue;
1552
1553 ac = iwl_mvm_tid_to_ac_queue(i);
1554 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1555 vif->hw_queue[ac], i, 0);
Sara Sharon6862fce2017-02-22 19:34:17 +02001556 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001557 }
1558}
1559
Sara Sharond6d517b2017-03-06 10:16:11 +02001560int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1561 struct iwl_mvm_sta *mvm_sta)
1562{
Sharon Dvirbec95222017-06-12 11:40:33 +03001563 int i;
Sara Sharond6d517b2017-03-06 10:16:11 +02001564
1565 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1566 u16 txq_id;
Sharon Dvirbec95222017-06-12 11:40:33 +03001567 int ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001568
1569 spin_lock_bh(&mvm_sta->lock);
1570 txq_id = mvm_sta->tid_data[i].txq_id;
1571 spin_unlock_bh(&mvm_sta->lock);
1572
1573 if (txq_id == IWL_MVM_INVALID_QUEUE)
1574 continue;
1575
1576 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1577 if (ret)
Sharon Dvirbec95222017-06-12 11:40:33 +03001578 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001579 }
1580
Sharon Dvirbec95222017-06-12 11:40:33 +03001581 return 0;
Sara Sharond6d517b2017-03-06 10:16:11 +02001582}
1583
Johannes Berg8ca151b2013-01-24 14:25:36 +01001584int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1585 struct ieee80211_vif *vif,
1586 struct ieee80211_sta *sta)
1587{
1588 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001589 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001590 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001591 int ret;
1592
1593 lockdep_assert_held(&mvm->mutex);
1594
Sara Sharona571f5f2015-12-07 12:50:58 +02001595 if (iwl_mvm_has_new_rx_api(mvm))
1596 kfree(mvm_sta->dup_data);
1597
Johannes Bergc8f54702017-06-19 23:50:31 +02001598 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1599 if (ret)
1600 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001601
Johannes Bergc8f54702017-06-19 23:50:31 +02001602 /* flush its queues here since we are freeing mvm_sta */
1603 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1604 if (ret)
1605 return ret;
1606 if (iwl_mvm_has_new_tx_api(mvm)) {
1607 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1608 } else {
1609 u32 q_mask = mvm_sta->tfd_queue_msk;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001610
Johannes Bergc8f54702017-06-19 23:50:31 +02001611 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1612 q_mask);
1613 }
1614 if (ret)
1615 return ret;
Liad Kaufman56214742016-09-22 15:14:08 +03001616
Johannes Bergc8f54702017-06-19 23:50:31 +02001617 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001618
Johannes Bergc8f54702017-06-19 23:50:31 +02001619 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001620
Johannes Bergc8f54702017-06-19 23:50:31 +02001621 /* If there is a TXQ still marked as reserved - free it */
1622 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1623 u8 reserved_txq = mvm_sta->reserved_queue;
1624 enum iwl_mvm_queue_status *status;
1625
1626 /*
1627 * If no traffic has gone through the reserved TXQ - it
1628 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1629 * should be manually marked as free again
1630 */
1631 spin_lock_bh(&mvm->queue_info_lock);
1632 status = &mvm->queue_info[reserved_txq].status;
1633 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1634 (*status != IWL_MVM_QUEUE_FREE),
1635 "sta_id %d reserved txq %d status %d",
1636 sta_id, reserved_txq, *status)) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001637 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Bergc8f54702017-06-19 23:50:31 +02001638 return -EINVAL;
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001639 }
1640
Johannes Bergc8f54702017-06-19 23:50:31 +02001641 *status = IWL_MVM_QUEUE_FREE;
1642 spin_unlock_bh(&mvm->queue_info_lock);
1643 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001644
Johannes Bergc8f54702017-06-19 23:50:31 +02001645 if (vif->type == NL80211_IFTYPE_STATION &&
1646 mvmvif->ap_sta_id == sta_id) {
1647 /* if associated - we can't remove the AP STA now */
1648 if (vif->bss_conf.assoc)
1649 return ret;
Eliad Peller37577fe2013-12-05 17:19:39 +02001650
Johannes Bergc8f54702017-06-19 23:50:31 +02001651 /* unassoc - go ahead - remove the AP STA now */
1652 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1653
1654 /* clear d0i3_ap_sta_id if no longer relevant */
1655 if (mvm->d0i3_ap_sta_id == sta_id)
1656 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001657 }
1658
1659 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001660 * This shouldn't happen - the TDLS channel switch should be canceled
1661 * before the STA is removed.
1662 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001663 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001664 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001665 cancel_delayed_work(&mvm->tdls_cs.dwork);
1666 }
1667
1668 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001669 * Make sure that the tx response code sees the station as -EBUSY and
1670 * calls the drain worker.
1671 */
1672 spin_lock_bh(&mvm_sta->lock);
Johannes Bergc8f54702017-06-19 23:50:31 +02001673 spin_unlock_bh(&mvm_sta->lock);
Sara Sharon94c3e612016-12-07 15:04:37 +02001674
Johannes Bergc8f54702017-06-19 23:50:31 +02001675 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1676 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001677
1678 return ret;
1679}
1680
1681int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1682 struct ieee80211_vif *vif,
1683 u8 sta_id)
1684{
1685 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1686
1687 lockdep_assert_held(&mvm->mutex);
1688
Monam Agarwalc531c772014-03-24 00:05:56 +05301689 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001690 return ret;
1691}
1692
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001693int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1694 struct iwl_mvm_int_sta *sta,
Sara Sharonced19f22017-02-06 19:09:32 +02001695 u32 qmask, enum nl80211_iftype iftype,
1696 enum iwl_sta_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001697{
Avraham Sterndf65c8d2018-03-06 14:10:49 +02001698 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1699 sta->sta_id == IWL_MVM_INVALID_STA) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001700 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Sara Sharon0ae98812017-01-04 14:53:58 +02001701 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001702 return -ENOSPC;
1703 }
1704
1705 sta->tfd_queue_msk = qmask;
Sara Sharonced19f22017-02-06 19:09:32 +02001706 sta->type = type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001707
1708 /* put a non-NULL value so iterating over the stations won't stop */
1709 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1710 return 0;
1711}
1712
Sara Sharon26d6c162017-01-03 12:00:19 +02001713void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001714{
Monam Agarwalc531c772014-03-24 00:05:56 +05301715 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001716 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
Sara Sharon0ae98812017-01-04 14:53:58 +02001717 sta->sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001718}
1719
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001720static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 *queue,
1721 u8 sta_id, u8 fifo)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001722{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001723 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1724 mvm->cfg->base_params->wd_timeout :
1725 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001726
Sara Sharon310181e2017-01-17 14:27:48 +02001727 if (iwl_mvm_has_new_tx_api(mvm)) {
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001728 int tvqm_queue =
1729 iwl_mvm_tvqm_enable_txq(mvm, *queue, sta_id,
1730 IWL_MAX_TID_COUNT,
1731 wdg_timeout);
1732 *queue = tvqm_queue;
Johannes Bergc8f54702017-06-19 23:50:31 +02001733 } else {
Liad Kaufman28d07932015-09-01 16:36:25 +03001734 struct iwl_trans_txq_scd_cfg cfg = {
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001735 .fifo = fifo,
1736 .sta_id = sta_id,
Liad Kaufman28d07932015-09-01 16:36:25 +03001737 .tid = IWL_MAX_TID_COUNT,
1738 .aggregate = false,
1739 .frame_limit = IWL_FRAME_LIMIT,
1740 };
1741
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001742 iwl_mvm_enable_txq(mvm, *queue, *queue, 0, &cfg, wdg_timeout);
Liad Kaufman28d07932015-09-01 16:36:25 +03001743 }
Sara Sharonc5a719e2016-11-15 10:20:48 +02001744}
1745
1746int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1747{
1748 int ret;
1749
1750 lockdep_assert_held(&mvm->mutex);
1751
1752 /* Allocate aux station and assign to it the aux queue */
1753 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Sara Sharonced19f22017-02-06 19:09:32 +02001754 NL80211_IFTYPE_UNSPECIFIED,
1755 IWL_STA_AUX_ACTIVITY);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001756 if (ret)
1757 return ret;
1758
1759 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1760 if (!iwl_mvm_has_new_tx_api(mvm))
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001761 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
1762 mvm->aux_sta.sta_id,
1763 IWL_MVM_TX_FIFO_MCAST);
Liad Kaufman28d07932015-09-01 16:36:25 +03001764
Johannes Berg8ca151b2013-01-24 14:25:36 +01001765 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1766 MAC_INDEX_AUX, 0);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001767 if (ret) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001768 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001769 return ret;
1770 }
1771
1772 /*
Luca Coelho2f7a3862017-11-15 15:07:34 +02001773 * For 22000 firmware and on we cannot add queue to a station unknown
Sara Sharonc5a719e2016-11-15 10:20:48 +02001774 * to firmware so enable queue here - after the station was added
1775 */
1776 if (iwl_mvm_has_new_tx_api(mvm))
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001777 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->aux_queue,
1778 mvm->aux_sta.sta_id,
1779 IWL_MVM_TX_FIFO_MCAST);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001780
1781 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001782}
1783
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001784int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1785{
1786 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001787 int ret;
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001788
1789 lockdep_assert_held(&mvm->mutex);
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001790
1791 /* Map snif queue to fifo - must happen before adding snif station */
1792 if (!iwl_mvm_has_new_tx_api(mvm))
1793 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
1794 mvm->snif_sta.sta_id,
1795 IWL_MVM_TX_FIFO_BE);
1796
1797 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001798 mvmvif->id, 0);
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001799 if (ret)
1800 return ret;
1801
1802 /*
1803 * For 22000 firmware and on we cannot add queue to a station unknown
1804 * to firmware so enable queue here - after the station was added
1805 */
1806 if (iwl_mvm_has_new_tx_api(mvm))
1807 iwl_mvm_enable_aux_snif_queue(mvm, &mvm->snif_queue,
1808 mvm->snif_sta.sta_id,
1809 IWL_MVM_TX_FIFO_BE);
1810
1811 return 0;
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001812}
1813
1814int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1815{
1816 int ret;
1817
1818 lockdep_assert_held(&mvm->mutex);
1819
Emmanuel Grumbachb13f43a2017-11-19 10:35:14 +02001820 iwl_mvm_disable_txq(mvm, mvm->snif_queue, mvm->snif_queue,
1821 IWL_MAX_TID_COUNT, 0);
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001822 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1823 if (ret)
1824 IWL_WARN(mvm, "Failed sending remove station\n");
1825
1826 return ret;
1827}
1828
1829void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1830{
1831 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1832}
1833
Johannes Berg712b24a2014-08-04 14:14:14 +02001834void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1835{
1836 lockdep_assert_held(&mvm->mutex);
1837
1838 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1839}
1840
Johannes Berg8ca151b2013-01-24 14:25:36 +01001841/*
1842 * Send the add station command for the vif's broadcast station.
1843 * Assumes that the station was already allocated.
1844 *
1845 * @mvm: the mvm component
1846 * @vif: the interface to which the broadcast station is added
1847 * @bsta: the broadcast station to add.
1848 */
Johannes Berg013290a2014-08-04 13:38:48 +02001849int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001850{
1851 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001852 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001853 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001854 const u8 *baddr = _baddr;
Johannes Berg7daa7622017-02-24 12:02:22 +01001855 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001856 int ret;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001857 unsigned int wdg_timeout =
1858 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1859 struct iwl_trans_txq_scd_cfg cfg = {
1860 .fifo = IWL_MVM_TX_FIFO_VO,
1861 .sta_id = mvmvif->bcast_sta.sta_id,
1862 .tid = IWL_MAX_TID_COUNT,
1863 .aggregate = false,
1864 .frame_limit = IWL_FRAME_LIMIT,
1865 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001866
1867 lockdep_assert_held(&mvm->mutex);
1868
Johannes Bergc8f54702017-06-19 23:50:31 +02001869 if (!iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufman4d339982017-03-21 17:13:16 +02001870 if (vif->type == NL80211_IFTYPE_AP ||
1871 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon49f71712017-01-09 12:07:16 +02001872 queue = mvm->probe_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001873 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
Sara Sharon49f71712017-01-09 12:07:16 +02001874 queue = mvm->p2p_dev_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001875 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
Liad Kaufmande24f632015-08-04 15:19:18 +03001876 return -EINVAL;
1877
Liad Kaufmandf88c082016-11-24 15:31:00 +02001878 bsta->tfd_queue_msk |= BIT(queue);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001879
Sara Sharon310181e2017-01-17 14:27:48 +02001880 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1881 &cfg, wdg_timeout);
Liad Kaufmande24f632015-08-04 15:19:18 +03001882 }
1883
Johannes Berg5023d962013-07-31 14:07:43 +02001884 if (vif->type == NL80211_IFTYPE_ADHOC)
1885 baddr = vif->bss_conf.bssid;
1886
Sara Sharon0ae98812017-01-04 14:53:58 +02001887 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001888 return -ENOSPC;
1889
Liad Kaufmandf88c082016-11-24 15:31:00 +02001890 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1891 mvmvif->id, mvmvif->color);
1892 if (ret)
1893 return ret;
1894
1895 /*
Luca Coelho2f7a3862017-11-15 15:07:34 +02001896 * For 22000 firmware and on we cannot add queue to a station unknown
Sara Sharonc5a719e2016-11-15 10:20:48 +02001897 * to firmware so enable queue here - after the station was added
Liad Kaufmandf88c082016-11-24 15:31:00 +02001898 */
Sara Sharon310181e2017-01-17 14:27:48 +02001899 if (iwl_mvm_has_new_tx_api(mvm)) {
Johannes Berg7daa7622017-02-24 12:02:22 +01001900 queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
1901 bsta->sta_id,
1902 IWL_MAX_TID_COUNT,
1903 wdg_timeout);
1904
Luca Coelho7b758a12017-06-20 13:40:03 +03001905 if (vif->type == NL80211_IFTYPE_AP ||
1906 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon310181e2017-01-17 14:27:48 +02001907 mvm->probe_queue = queue;
1908 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1909 mvm->p2p_dev_queue = queue;
Sara Sharon310181e2017-01-17 14:27:48 +02001910 }
Liad Kaufmandf88c082016-11-24 15:31:00 +02001911
1912 return 0;
1913}
1914
1915static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
1916 struct ieee80211_vif *vif)
1917{
1918 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001919 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001920
1921 lockdep_assert_held(&mvm->mutex);
1922
Sara Sharond49394a2017-03-05 13:01:08 +02001923 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
1924
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001925 switch (vif->type) {
1926 case NL80211_IFTYPE_AP:
1927 case NL80211_IFTYPE_ADHOC:
1928 queue = mvm->probe_queue;
1929 break;
1930 case NL80211_IFTYPE_P2P_DEVICE:
1931 queue = mvm->p2p_dev_queue;
1932 break;
1933 default:
1934 WARN(1, "Can't free bcast queue on vif type %d\n",
1935 vif->type);
1936 return;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001937 }
1938
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001939 iwl_mvm_disable_txq(mvm, queue, vif->hw_queue[0], IWL_MAX_TID_COUNT, 0);
1940 if (iwl_mvm_has_new_tx_api(mvm))
1941 return;
1942
1943 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
1944 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001945}
1946
1947/* Send the FW a request to remove the station from it's internal data
1948 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001949int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001950{
Johannes Berg013290a2014-08-04 13:38:48 +02001951 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001952 int ret;
1953
1954 lockdep_assert_held(&mvm->mutex);
1955
Johannes Bergc8f54702017-06-19 23:50:31 +02001956 iwl_mvm_free_bcast_sta_queues(mvm, vif);
Liad Kaufmandf88c082016-11-24 15:31:00 +02001957
Johannes Berg013290a2014-08-04 13:38:48 +02001958 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001959 if (ret)
1960 IWL_WARN(mvm, "Failed sending remove station\n");
1961 return ret;
1962}
1963
Johannes Berg013290a2014-08-04 13:38:48 +02001964int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1965{
1966 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001967
1968 lockdep_assert_held(&mvm->mutex);
1969
Johannes Bergc8f54702017-06-19 23:50:31 +02001970 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
Sara Sharonced19f22017-02-06 19:09:32 +02001971 ieee80211_vif_type_p2p(vif),
1972 IWL_STA_GENERAL_PURPOSE);
Johannes Berg013290a2014-08-04 13:38:48 +02001973}
1974
Johannes Berg8ca151b2013-01-24 14:25:36 +01001975/* Allocate a new station entry for the broadcast station to the given vif,
1976 * and send it to the FW.
1977 * Note that each P2P mac should have its own broadcast station.
1978 *
1979 * @mvm: the mvm component
1980 * @vif: the interface to which the broadcast station is added
1981 * @bsta: the broadcast station to add. */
Luca Coelhod1973582017-06-22 16:00:25 +03001982int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001983{
1984 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001985 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001986 int ret;
1987
1988 lockdep_assert_held(&mvm->mutex);
1989
Johannes Berg013290a2014-08-04 13:38:48 +02001990 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001991 if (ret)
1992 return ret;
1993
Johannes Berg013290a2014-08-04 13:38:48 +02001994 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001995
1996 if (ret)
1997 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001998
Johannes Berg8ca151b2013-01-24 14:25:36 +01001999 return ret;
2000}
2001
Johannes Berg013290a2014-08-04 13:38:48 +02002002void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2003{
2004 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2005
2006 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2007}
2008
Johannes Berg8ca151b2013-01-24 14:25:36 +01002009/*
2010 * Send the FW a request to remove the station from it's internal data
2011 * structures, and in addition remove it from the local data structure.
2012 */
Luca Coelhod1973582017-06-22 16:00:25 +03002013int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002014{
2015 int ret;
2016
2017 lockdep_assert_held(&mvm->mutex);
2018
Johannes Berg013290a2014-08-04 13:38:48 +02002019 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002020
Johannes Berg013290a2014-08-04 13:38:48 +02002021 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2022
Johannes Berg8ca151b2013-01-24 14:25:36 +01002023 return ret;
2024}
2025
Sara Sharon26d6c162017-01-03 12:00:19 +02002026/*
2027 * Allocate a new station entry for the multicast station to the given vif,
2028 * and send it to the FW.
2029 * Note that each AP/GO mac should have its own multicast station.
2030 *
2031 * @mvm: the mvm component
2032 * @vif: the interface to which the multicast station is added
2033 */
2034int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2035{
2036 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2037 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2038 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2039 const u8 *maddr = _maddr;
2040 struct iwl_trans_txq_scd_cfg cfg = {
2041 .fifo = IWL_MVM_TX_FIFO_MCAST,
2042 .sta_id = msta->sta_id,
Ilan Peer6508de02018-01-25 15:22:41 +02002043 .tid = 0,
Sara Sharon26d6c162017-01-03 12:00:19 +02002044 .aggregate = false,
2045 .frame_limit = IWL_FRAME_LIMIT,
2046 };
2047 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2048 int ret;
2049
2050 lockdep_assert_held(&mvm->mutex);
2051
Liad Kaufmanee48b722017-03-21 17:13:16 +02002052 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2053 vif->type != NL80211_IFTYPE_ADHOC))
Sara Sharon26d6c162017-01-03 12:00:19 +02002054 return -ENOTSUPP;
2055
Sara Sharonced19f22017-02-06 19:09:32 +02002056 /*
Sara Sharonfc07bd82017-12-21 15:05:28 +02002057 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2058 * invalid, so make sure we use the queue we want.
2059 * Note that this is done here as we want to avoid making DQA
2060 * changes in mac80211 layer.
2061 */
2062 if (vif->type == NL80211_IFTYPE_ADHOC) {
2063 vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2064 mvmvif->cab_queue = vif->cab_queue;
2065 }
2066
2067 /*
Sara Sharonced19f22017-02-06 19:09:32 +02002068 * While in previous FWs we had to exclude cab queue from TFD queue
2069 * mask, now it is needed as any other queue.
2070 */
2071 if (!iwl_mvm_has_new_tx_api(mvm) &&
2072 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2073 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2074 &cfg, timeout);
2075 msta->tfd_queue_msk |= BIT(vif->cab_queue);
2076 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002077 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2078 mvmvif->id, mvmvif->color);
2079 if (ret) {
2080 iwl_mvm_dealloc_int_sta(mvm, msta);
2081 return ret;
2082 }
2083
2084 /*
2085 * Enable cab queue after the ADD_STA command is sent.
Luca Coelho2f7a3862017-11-15 15:07:34 +02002086 * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
Sara Sharonced19f22017-02-06 19:09:32 +02002087 * command with unknown station id, and for FW that doesn't support
2088 * station API since the cab queue is not included in the
2089 * tfd_queue_mask.
Sara Sharon26d6c162017-01-03 12:00:19 +02002090 */
Sara Sharon310181e2017-01-17 14:27:48 +02002091 if (iwl_mvm_has_new_tx_api(mvm)) {
2092 int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2093 msta->sta_id,
Ilan Peer6508de02018-01-25 15:22:41 +02002094 0,
Sara Sharon310181e2017-01-17 14:27:48 +02002095 timeout);
Sara Sharone2af3fa2017-02-22 19:35:10 +02002096 mvmvif->cab_queue = queue;
Sara Sharonced19f22017-02-06 19:09:32 +02002097 } else if (!fw_has_api(&mvm->fw->ucode_capa,
Sara Sharonfc07bd82017-12-21 15:05:28 +02002098 IWL_UCODE_TLV_API_STA_TYPE))
Sara Sharon310181e2017-01-17 14:27:48 +02002099 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2100 &cfg, timeout);
Sara Sharon26d6c162017-01-03 12:00:19 +02002101
2102 return 0;
2103}
2104
2105/*
2106 * Send the FW a request to remove the station from it's internal data
2107 * structures, and in addition remove it from the local data structure.
2108 */
2109int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2110{
2111 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2112 int ret;
2113
2114 lockdep_assert_held(&mvm->mutex);
2115
Sara Sharond49394a2017-03-05 13:01:08 +02002116 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2117
Sara Sharone2af3fa2017-02-22 19:35:10 +02002118 iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
Ilan Peer6508de02018-01-25 15:22:41 +02002119 0, 0);
Sara Sharon26d6c162017-01-03 12:00:19 +02002120
2121 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2122 if (ret)
2123 IWL_WARN(mvm, "Failed sending remove station\n");
2124
2125 return ret;
2126}
2127
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002128#define IWL_MAX_RX_BA_SESSIONS 16
2129
Sara Sharonb915c102016-03-23 16:32:02 +02002130static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02002131{
Sara Sharonb915c102016-03-23 16:32:02 +02002132 struct iwl_mvm_delba_notif notif = {
2133 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2134 .metadata.sync = 1,
2135 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02002136 };
Sara Sharonb915c102016-03-23 16:32:02 +02002137 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2138};
Sara Sharon10b2b202016-03-20 16:23:41 +02002139
Sara Sharonb915c102016-03-23 16:32:02 +02002140static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2141 struct iwl_mvm_baid_data *data)
2142{
2143 int i;
2144
2145 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2146
2147 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2148 int j;
2149 struct iwl_mvm_reorder_buffer *reorder_buf =
2150 &data->reorder_buf[i];
Johannes Bergdfdddd92017-09-26 12:24:51 +02002151 struct iwl_mvm_reorder_buf_entry *entries =
2152 &data->entries[i * data->entries_per_queue];
Sara Sharonb915c102016-03-23 16:32:02 +02002153
Sara Sharon06904052016-02-28 20:28:17 +02002154 spin_lock_bh(&reorder_buf->lock);
2155 if (likely(!reorder_buf->num_stored)) {
2156 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02002157 continue;
Sara Sharon06904052016-02-28 20:28:17 +02002158 }
Sara Sharonb915c102016-03-23 16:32:02 +02002159
2160 /*
2161 * This shouldn't happen in regular DELBA since the internal
2162 * delBA notification should trigger a release of all frames in
2163 * the reorder buffer.
2164 */
2165 WARN_ON(1);
2166
2167 for (j = 0; j < reorder_buf->buf_size; j++)
Johannes Bergdfdddd92017-09-26 12:24:51 +02002168 __skb_queue_purge(&entries[j].e.frames);
Sara Sharon06904052016-02-28 20:28:17 +02002169 /*
2170 * Prevent timer re-arm. This prevents a very far fetched case
2171 * where we timed out on the notification. There may be prior
2172 * RX frames pending in the RX queue before the notification
2173 * that might get processed between now and the actual deletion
2174 * and we would re-arm the timer although we are deleting the
2175 * reorder buffer.
2176 */
2177 reorder_buf->removed = true;
2178 spin_unlock_bh(&reorder_buf->lock);
2179 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02002180 }
2181}
2182
2183static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
Sara Sharonb915c102016-03-23 16:32:02 +02002184 struct iwl_mvm_baid_data *data,
2185 u16 ssn, u8 buf_size)
2186{
2187 int i;
2188
2189 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2190 struct iwl_mvm_reorder_buffer *reorder_buf =
2191 &data->reorder_buf[i];
Johannes Bergdfdddd92017-09-26 12:24:51 +02002192 struct iwl_mvm_reorder_buf_entry *entries =
2193 &data->entries[i * data->entries_per_queue];
Sara Sharonb915c102016-03-23 16:32:02 +02002194 int j;
2195
2196 reorder_buf->num_stored = 0;
2197 reorder_buf->head_sn = ssn;
2198 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02002199 /* rx reorder timer */
Kees Cook8cef5342017-10-24 02:29:37 -07002200 timer_setup(&reorder_buf->reorder_timer,
2201 iwl_mvm_reorder_timer_expired, 0);
Sara Sharon06904052016-02-28 20:28:17 +02002202 spin_lock_init(&reorder_buf->lock);
2203 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02002204 reorder_buf->queue = i;
Sara Sharon5d43eab2017-02-02 12:51:39 +02002205 reorder_buf->valid = false;
Sara Sharonb915c102016-03-23 16:32:02 +02002206 for (j = 0; j < reorder_buf->buf_size; j++)
Johannes Bergdfdddd92017-09-26 12:24:51 +02002207 __skb_queue_head_init(&entries[j].e.frames);
Sara Sharonb915c102016-03-23 16:32:02 +02002208 }
Sara Sharon10b2b202016-03-20 16:23:41 +02002209}
2210
Johannes Berg8ca151b2013-01-24 14:25:36 +01002211int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02002212 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002213{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002214 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002215 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02002216 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002217 int ret;
2218 u32 status;
2219
2220 lockdep_assert_held(&mvm->mutex);
2221
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002222 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2223 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2224 return -ENOSPC;
2225 }
2226
Sara Sharon10b2b202016-03-20 16:23:41 +02002227 if (iwl_mvm_has_new_rx_api(mvm) && start) {
Johannes Bergdfdddd92017-09-26 12:24:51 +02002228 u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2229
2230 /* sparse doesn't like the __align() so don't check */
2231#ifndef __CHECKER__
2232 /*
2233 * The division below will be OK if either the cache line size
2234 * can be divided by the entry size (ALIGN will round up) or if
2235 * if the entry size can be divided by the cache line size, in
2236 * which case the ALIGN() will do nothing.
2237 */
2238 BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2239 sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2240#endif
2241
2242 /*
2243 * Upward align the reorder buffer size to fill an entire cache
2244 * line for each queue, to avoid sharing cache lines between
2245 * different queues.
2246 */
2247 reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2248
Sara Sharon10b2b202016-03-20 16:23:41 +02002249 /*
2250 * Allocate here so if allocation fails we can bail out early
2251 * before starting the BA session in the firmware
2252 */
Sara Sharonb915c102016-03-23 16:32:02 +02002253 baid_data = kzalloc(sizeof(*baid_data) +
2254 mvm->trans->num_rx_queues *
Johannes Bergdfdddd92017-09-26 12:24:51 +02002255 reorder_buf_size,
Sara Sharonb915c102016-03-23 16:32:02 +02002256 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02002257 if (!baid_data)
2258 return -ENOMEM;
Johannes Bergdfdddd92017-09-26 12:24:51 +02002259
2260 /*
2261 * This division is why we need the above BUILD_BUG_ON(),
2262 * if that doesn't hold then this will not be right.
2263 */
2264 baid_data->entries_per_queue =
2265 reorder_buf_size / sizeof(baid_data->entries[0]);
Sara Sharon10b2b202016-03-20 16:23:41 +02002266 }
2267
Johannes Berg8ca151b2013-01-24 14:25:36 +01002268 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2269 cmd.sta_id = mvm_sta->sta_id;
2270 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002271 if (start) {
2272 cmd.add_immediate_ba_tid = (u8) tid;
2273 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02002274 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002275 } else {
2276 cmd.remove_immediate_ba_tid = (u8) tid;
2277 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002278 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2279 STA_MODIFY_REMOVE_BA_TID;
2280
2281 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002282 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2283 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002284 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002285 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02002286 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002287
Sara Sharon837c4da2016-01-07 16:50:45 +02002288 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002289 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03002290 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2291 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002292 break;
2293 case ADD_STA_IMMEDIATE_BA_FAILURE:
2294 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2295 ret = -ENOSPC;
2296 break;
2297 default:
2298 ret = -EIO;
2299 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2300 start ? "start" : "stopp", status);
2301 break;
2302 }
2303
Sara Sharon10b2b202016-03-20 16:23:41 +02002304 if (ret)
2305 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002306
Sara Sharon10b2b202016-03-20 16:23:41 +02002307 if (start) {
2308 u8 baid;
2309
2310 mvm->rx_ba_sessions++;
2311
2312 if (!iwl_mvm_has_new_rx_api(mvm))
2313 return 0;
2314
2315 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2316 ret = -EINVAL;
2317 goto out_free;
2318 }
2319 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2320 IWL_ADD_STA_BAID_SHIFT);
2321 baid_data->baid = baid;
2322 baid_data->timeout = timeout;
2323 baid_data->last_rx = jiffies;
Kees Cook8cef5342017-10-24 02:29:37 -07002324 baid_data->rcu_ptr = &mvm->baid_map[baid];
2325 timer_setup(&baid_data->session_timer,
2326 iwl_mvm_rx_agg_session_expired, 0);
Sara Sharon10b2b202016-03-20 16:23:41 +02002327 baid_data->mvm = mvm;
2328 baid_data->tid = tid;
2329 baid_data->sta_id = mvm_sta->sta_id;
2330
2331 mvm_sta->tid_to_baid[tid] = baid;
2332 if (timeout)
2333 mod_timer(&baid_data->session_timer,
2334 TU_TO_EXP_TIME(timeout * 2));
2335
Sara Sharon3f1c4c52017-10-02 12:07:59 +03002336 iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002337 /*
2338 * protect the BA data with RCU to cover a case where our
2339 * internal RX sync mechanism will timeout (not that it's
2340 * supposed to happen) and we will free the session data while
2341 * RX is being processed in parallel
2342 */
Sara Sharon35263a02016-06-21 12:12:10 +03002343 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2344 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002345 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2346 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002347 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002348 u8 baid = mvm_sta->tid_to_baid[tid];
2349
Sara Sharon60dec522016-06-21 14:14:08 +03002350 if (mvm->rx_ba_sessions > 0)
2351 /* check that restart flow didn't zero the counter */
2352 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002353 if (!iwl_mvm_has_new_rx_api(mvm))
2354 return 0;
2355
2356 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2357 return -EINVAL;
2358
2359 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2360 if (WARN_ON(!baid_data))
2361 return -EINVAL;
2362
2363 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002364 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002365 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002366 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2367 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002368 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002369 }
2370 return 0;
2371
2372out_free:
2373 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002374 return ret;
2375}
2376
Liad Kaufman9794c642015-08-19 17:34:28 +03002377int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2378 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002379{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002380 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002381 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002382 int ret;
2383 u32 status;
2384
2385 lockdep_assert_held(&mvm->mutex);
2386
2387 if (start) {
2388 mvm_sta->tfd_queue_msk |= BIT(queue);
2389 mvm_sta->tid_disable_agg &= ~BIT(tid);
2390 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002391 /* In DQA-mode the queue isn't removed on agg termination */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002392 mvm_sta->tid_disable_agg |= BIT(tid);
2393 }
2394
2395 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2396 cmd.sta_id = mvm_sta->sta_id;
2397 cmd.add_modify = STA_MODE_MODIFY;
Sara Sharonbb497012016-09-29 14:52:40 +03002398 if (!iwl_mvm_has_new_tx_api(mvm))
2399 cmd.modify_mask = STA_MODIFY_QUEUES;
2400 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002401 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2402 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2403
2404 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002405 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2406 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002407 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002408 if (ret)
2409 return ret;
2410
Sara Sharon837c4da2016-01-07 16:50:45 +02002411 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002412 case ADD_STA_SUCCESS:
2413 break;
2414 default:
2415 ret = -EIO;
2416 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2417 start ? "start" : "stopp", status);
2418 break;
2419 }
2420
2421 return ret;
2422}
2423
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002424const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002425 IEEE80211_AC_BE,
2426 IEEE80211_AC_BK,
2427 IEEE80211_AC_BK,
2428 IEEE80211_AC_BE,
2429 IEEE80211_AC_VI,
2430 IEEE80211_AC_VI,
2431 IEEE80211_AC_VO,
2432 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002433 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002434};
2435
Johannes Berg3e56ead2013-02-15 22:23:18 +01002436static const u8 tid_to_ucode_ac[] = {
2437 AC_BE,
2438 AC_BK,
2439 AC_BK,
2440 AC_BE,
2441 AC_VI,
2442 AC_VI,
2443 AC_VO,
2444 AC_VO,
2445};
2446
Johannes Berg8ca151b2013-01-24 14:25:36 +01002447int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2448 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2449{
Johannes Berg5b577a92013-11-14 18:20:04 +01002450 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002451 struct iwl_mvm_tid_data *tid_data;
Liad Kaufmandd321622017-04-05 16:25:11 +03002452 u16 normalized_ssn;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002453 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002454 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002455
2456 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2457 return -EINVAL;
2458
Naftali Goldsteinbd800e42017-08-28 11:51:05 +03002459 if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2460 mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2461 IWL_ERR(mvm,
2462 "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
Johannes Berg8ca151b2013-01-24 14:25:36 +01002463 mvmsta->tid_data[tid].state);
2464 return -ENXIO;
2465 }
2466
2467 lockdep_assert_held(&mvm->mutex);
2468
Arik Nemtsovb2492502014-03-13 12:21:50 +02002469 spin_lock_bh(&mvmsta->lock);
2470
2471 /* possible race condition - we entered D0i3 while starting agg */
2472 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2473 spin_unlock_bh(&mvmsta->lock);
2474 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2475 return -EIO;
2476 }
2477
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002478 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002479
Liad Kaufmancf961e12015-08-13 19:16:08 +03002480 /*
2481 * Note the possible cases:
Avraham Stern4a6d2e52018-03-05 11:26:53 +02002482 * 1. An enabled TXQ - TXQ needs to become agg'ed
2483 * 2. The TXQ hasn't yet been enabled, so find a free one and mark
2484 * it as reserved
Liad Kaufmancf961e12015-08-13 19:16:08 +03002485 */
2486 txq_id = mvmsta->tid_data[tid].txq_id;
Avraham Stern4a6d2e52018-03-05 11:26:53 +02002487 if (txq_id == IWL_MVM_INVALID_QUEUE) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002488 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
Johannes Bergc8f54702017-06-19 23:50:31 +02002489 IWL_MVM_DQA_MIN_DATA_QUEUE,
2490 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002491 if (txq_id < 0) {
2492 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002493 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2494 goto release_locks;
2495 }
2496
2497 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2498 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Avraham Stern4a6d2e52018-03-05 11:26:53 +02002499 } else if (unlikely(mvm->queue_info[txq_id].status ==
2500 IWL_MVM_QUEUE_SHARED)) {
2501 ret = -ENXIO;
2502 IWL_DEBUG_TX_QUEUES(mvm,
2503 "Can't start tid %d agg on shared queue!\n",
2504 tid);
2505 goto release_locks;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002506 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002507
2508 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002509
Liad Kaufmancf961e12015-08-13 19:16:08 +03002510 IWL_DEBUG_TX_QUEUES(mvm,
2511 "AGG for tid %d will be on queue #%d\n",
2512 tid, txq_id);
2513
Johannes Berg8ca151b2013-01-24 14:25:36 +01002514 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002515 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002516 tid_data->txq_id = txq_id;
2517 *ssn = tid_data->ssn;
2518
2519 IWL_DEBUG_TX_QUEUES(mvm,
2520 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2521 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2522 tid_data->next_reclaimed);
2523
Liad Kaufmandd321622017-04-05 16:25:11 +03002524 /*
Luca Coelho2f7a3862017-11-15 15:07:34 +02002525 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
Liad Kaufmandd321622017-04-05 16:25:11 +03002526 * to align the wrap around of ssn so we compare relevant values.
2527 */
2528 normalized_ssn = tid_data->ssn;
2529 if (mvm->trans->cfg->gen2)
2530 normalized_ssn &= 0xff;
2531
2532 if (normalized_ssn == tid_data->next_reclaimed) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002533 tid_data->state = IWL_AGG_STARTING;
2534 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2535 } else {
2536 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2537 }
2538
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002539 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002540 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002541
2542release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002543 spin_unlock(&mvm->queue_info_lock);
2544out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002545 spin_unlock_bh(&mvmsta->lock);
2546
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002547 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002548}
2549
2550int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002551 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2552 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002553{
Johannes Berg5b577a92013-11-14 18:20:04 +01002554 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002555 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002556 unsigned int wdg_timeout =
2557 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002558 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002559 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002560 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002561 u16 ssn;
2562
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002563 struct iwl_trans_txq_scd_cfg cfg = {
2564 .sta_id = mvmsta->sta_id,
2565 .tid = tid,
2566 .frame_limit = buf_size,
2567 .aggregate = true,
2568 };
2569
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002570 /*
2571 * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2572 * manager, so this function should never be called in this case.
2573 */
Emmanuel Grumbach4243edb2017-12-13 11:38:48 +02002574 if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002575 return -EINVAL;
2576
Eyal Shapiraefed6642014-09-14 15:58:53 +03002577 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2578 != IWL_MAX_TID_COUNT);
2579
Johannes Berg8ca151b2013-01-24 14:25:36 +01002580 spin_lock_bh(&mvmsta->lock);
2581 ssn = tid_data->ssn;
2582 queue = tid_data->txq_id;
2583 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002584 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002585 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002586 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002587 spin_unlock_bh(&mvmsta->lock);
2588
Sara Sharon34e10862017-02-23 13:15:07 +02002589 if (iwl_mvm_has_new_tx_api(mvm)) {
2590 /*
Sara Sharon0ec9257b2017-10-16 09:45:10 +03002591 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2592 * would have failed, so if we are here there is no need to
2593 * allocate a queue.
2594 * However, if aggregation size is different than the default
2595 * size, the scheduler should be reconfigured.
2596 * We cannot do this with the new TX API, so return unsupported
2597 * for now, until it will be offloaded to firmware..
2598 * Note that if SCD default value changes - this condition
2599 * should be updated as well.
Sara Sharon34e10862017-02-23 13:15:07 +02002600 */
Sara Sharon0ec9257b2017-10-16 09:45:10 +03002601 if (buf_size < IWL_FRAME_LIMIT)
Sara Sharon34e10862017-02-23 13:15:07 +02002602 return -ENOTSUPP;
2603
2604 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2605 if (ret)
2606 return -EIO;
2607 goto out;
2608 }
2609
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002610 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002611
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002612 spin_lock_bh(&mvm->queue_info_lock);
2613 queue_status = mvm->queue_info[queue].status;
2614 spin_unlock_bh(&mvm->queue_info_lock);
2615
Johannes Bergc8f54702017-06-19 23:50:31 +02002616 /* Maybe there is no need to even alloc a queue... */
2617 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2618 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002619
Johannes Bergc8f54702017-06-19 23:50:31 +02002620 /*
2621 * Only reconfig the SCD for the queue if the window size has
2622 * changed from current (become smaller)
2623 */
Sara Sharon0ec9257b2017-10-16 09:45:10 +03002624 if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002625 /*
Johannes Bergc8f54702017-06-19 23:50:31 +02002626 * If reconfiguring an existing queue, it first must be
2627 * drained
Liad Kaufmancf961e12015-08-13 19:16:08 +03002628 */
Johannes Bergc8f54702017-06-19 23:50:31 +02002629 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2630 BIT(queue));
2631 if (ret) {
2632 IWL_ERR(mvm,
2633 "Error draining queue before reconfig\n");
2634 return ret;
2635 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03002636
Johannes Bergc8f54702017-06-19 23:50:31 +02002637 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2638 mvmsta->sta_id, tid,
2639 buf_size, ssn);
2640 if (ret) {
2641 IWL_ERR(mvm,
2642 "Error reconfiguring TXQ #%d\n", queue);
2643 return ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002644 }
2645 }
2646
2647 if (alloc_queue)
2648 iwl_mvm_enable_txq(mvm, queue,
2649 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2650 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002651
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002652 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2653 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2654 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2655 if (ret)
2656 return -EIO;
2657 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002658
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002659 /* No need to mark as reserved */
2660 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002661 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002662 spin_unlock_bh(&mvm->queue_info_lock);
2663
Sara Sharon34e10862017-02-23 13:15:07 +02002664out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002665 /*
2666 * Even though in theory the peer could have different
2667 * aggregation reorder buffer sizes for different sessions,
2668 * our ucode doesn't allow for that and has a global limit
2669 * for each station. Therefore, use the minimum of all the
2670 * aggregation sessions and our default value.
2671 */
2672 mvmsta->max_agg_bufsize =
2673 min(mvmsta->max_agg_bufsize, buf_size);
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002674 mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002675
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002676 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2677 sta->addr, tid);
2678
Gregory Greenmanecaf71d2017-11-01 07:16:29 +02002679 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002680}
2681
Sara Sharon34e10862017-02-23 13:15:07 +02002682static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2683 struct iwl_mvm_sta *mvmsta,
Avraham Stern4b387902018-03-07 10:41:18 +02002684 struct iwl_mvm_tid_data *tid_data)
Sara Sharon34e10862017-02-23 13:15:07 +02002685{
Avraham Stern4b387902018-03-07 10:41:18 +02002686 u16 txq_id = tid_data->txq_id;
2687
Sara Sharon34e10862017-02-23 13:15:07 +02002688 if (iwl_mvm_has_new_tx_api(mvm))
2689 return;
2690
2691 spin_lock_bh(&mvm->queue_info_lock);
2692 /*
2693 * The TXQ is marked as reserved only if no traffic came through yet
2694 * This means no traffic has been sent on this TID (agg'd or not), so
2695 * we no longer have use for the queue. Since it hasn't even been
2696 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2697 * free.
2698 */
Avraham Stern4b387902018-03-07 10:41:18 +02002699 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
Sara Sharon34e10862017-02-23 13:15:07 +02002700 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Avraham Stern4b387902018-03-07 10:41:18 +02002701 tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
2702 }
Sara Sharon34e10862017-02-23 13:15:07 +02002703
2704 spin_unlock_bh(&mvm->queue_info_lock);
2705}
2706
Johannes Berg8ca151b2013-01-24 14:25:36 +01002707int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2708 struct ieee80211_sta *sta, u16 tid)
2709{
Johannes Berg5b577a92013-11-14 18:20:04 +01002710 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002711 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2712 u16 txq_id;
2713 int err;
2714
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002715 /*
2716 * If mac80211 is cleaning its state, then say that we finished since
2717 * our state has been cleared anyway.
2718 */
2719 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2720 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2721 return 0;
2722 }
2723
Johannes Berg8ca151b2013-01-24 14:25:36 +01002724 spin_lock_bh(&mvmsta->lock);
2725
2726 txq_id = tid_data->txq_id;
2727
2728 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2729 mvmsta->sta_id, tid, txq_id, tid_data->state);
2730
Eyal Shapiraefed6642014-09-14 15:58:53 +03002731 mvmsta->agg_tids &= ~BIT(tid);
2732
Avraham Stern4b387902018-03-07 10:41:18 +02002733 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002734
Johannes Berg8ca151b2013-01-24 14:25:36 +01002735 switch (tid_data->state) {
2736 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002737 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002738
2739 IWL_DEBUG_TX_QUEUES(mvm,
2740 "ssn = %d, next_recl = %d\n",
2741 tid_data->ssn, tid_data->next_reclaimed);
2742
Johannes Berg8ca151b2013-01-24 14:25:36 +01002743 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002744 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002745 spin_unlock_bh(&mvmsta->lock);
2746
2747 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2748
2749 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
Johannes Bergf7f89e72014-08-05 15:24:44 +02002750 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002751 case IWL_AGG_STARTING:
2752 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2753 /*
2754 * The agg session has been stopped before it was set up. This
2755 * can happen when the AddBA timer times out for example.
2756 */
2757
2758 /* No barriers since we are under mutex */
2759 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002760
2761 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2762 tid_data->state = IWL_AGG_OFF;
2763 err = 0;
2764 break;
2765 default:
2766 IWL_ERR(mvm,
2767 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2768 mvmsta->sta_id, tid, tid_data->state);
2769 IWL_ERR(mvm,
2770 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2771 err = -EINVAL;
2772 }
2773
2774 spin_unlock_bh(&mvmsta->lock);
2775
2776 return err;
2777}
2778
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002779int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2780 struct ieee80211_sta *sta, u16 tid)
2781{
Johannes Berg5b577a92013-11-14 18:20:04 +01002782 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002783 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2784 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002785 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002786
2787 /*
2788 * First set the agg state to OFF to avoid calling
2789 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2790 */
2791 spin_lock_bh(&mvmsta->lock);
2792 txq_id = tid_data->txq_id;
2793 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2794 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002795 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002796 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002797 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002798 spin_unlock_bh(&mvmsta->lock);
2799
Avraham Stern4b387902018-03-07 10:41:18 +02002800 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002801
Johannes Bergb6658ff2013-07-24 13:55:51 +02002802 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002803 iwl_mvm_drain_sta(mvm, mvmsta, true);
Sara Sharond6d517b2017-03-06 10:16:11 +02002804
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002805 if (iwl_mvm_has_new_tx_api(mvm)) {
2806 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
2807 BIT(tid), 0))
2808 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002809 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002810 } else {
2811 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2812 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002813 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002814 }
Sara Sharond6d517b2017-03-06 10:16:11 +02002815
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002816 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002817
Johannes Bergf7f89e72014-08-05 15:24:44 +02002818 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002819 }
2820
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002821 return 0;
2822}
2823
Johannes Berg8ca151b2013-01-24 14:25:36 +01002824static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2825{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002826 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002827
2828 lockdep_assert_held(&mvm->mutex);
2829
Johannes Berg2dc2a152015-06-16 17:09:18 +02002830 /* Pick the unused key offset with the highest 'deleted'
2831 * counter. Every time a key is deleted, all the counters
2832 * are incremented and the one that was just deleted is
2833 * reset to zero. Thus, the highest counter is the one
2834 * that was deleted longest ago. Pick that one.
2835 */
2836 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2837 if (test_bit(i, mvm->fw_key_table))
2838 continue;
2839 if (mvm->fw_key_deleted[i] > max) {
2840 max = mvm->fw_key_deleted[i];
2841 max_offs = i;
2842 }
2843 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002844
Johannes Berg2dc2a152015-06-16 17:09:18 +02002845 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002846 return STA_KEY_IDX_INVALID;
2847
Johannes Berg2dc2a152015-06-16 17:09:18 +02002848 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002849}
2850
Johannes Berg5f7a1842015-12-11 09:36:10 +01002851static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2852 struct ieee80211_vif *vif,
2853 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002854{
Johannes Berg5b530e92014-12-23 16:00:17 +01002855 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002856
Johannes Berg5f7a1842015-12-11 09:36:10 +01002857 if (sta)
2858 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002859
2860 /*
2861 * The device expects GTKs for station interfaces to be
2862 * installed as GTKs for the AP station. If we have no
2863 * station ID, then use AP's station ID.
2864 */
2865 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02002866 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Avri Altman9513c5e2015-10-19 16:29:11 +02002867 u8 sta_id = mvmvif->ap_sta_id;
2868
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002869 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2870 lockdep_is_held(&mvm->mutex));
2871
Avri Altman9513c5e2015-10-19 16:29:11 +02002872 /*
2873 * It is possible that the 'sta' parameter is NULL,
2874 * for example when a GTK is removed - the sta_id will then
2875 * be the AP ID, and no station was passed by mac80211.
2876 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002877 if (IS_ERR_OR_NULL(sta))
2878 return NULL;
2879
2880 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02002881 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002882
Johannes Berg5f7a1842015-12-11 09:36:10 +01002883 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002884}
2885
2886static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
David Spinadel85aeb582017-03-30 19:43:53 +03002887 u32 sta_id,
Sara Sharon45c458b2016-11-09 15:43:26 +02002888 struct ieee80211_key_conf *key, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002889 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2890 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002891{
Sara Sharon45c458b2016-11-09 15:43:26 +02002892 union {
2893 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2894 struct iwl_mvm_add_sta_key_cmd cmd;
2895 } u = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002896 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002897 int ret;
2898 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002899 u16 keyidx;
Sara Sharon45c458b2016-11-09 15:43:26 +02002900 u64 pn = 0;
2901 int i, size;
2902 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2903 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002904
David Spinadel85aeb582017-03-30 19:43:53 +03002905 if (sta_id == IWL_MVM_INVALID_STA)
2906 return -EINVAL;
2907
Sara Sharon45c458b2016-11-09 15:43:26 +02002908 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
Johannes Berg8ca151b2013-01-24 14:25:36 +01002909 STA_KEY_FLG_KEYID_MSK;
2910 key_flags = cpu_to_le16(keyidx);
2911 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2912
Sara Sharon45c458b2016-11-09 15:43:26 +02002913 switch (key->cipher) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002914 case WLAN_CIPHER_SUITE_TKIP:
2915 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002916 if (new_api) {
2917 memcpy((void *)&u.cmd.tx_mic_key,
2918 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
2919 IWL_MIC_KEY_SIZE);
2920
2921 memcpy((void *)&u.cmd.rx_mic_key,
2922 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
2923 IWL_MIC_KEY_SIZE);
2924 pn = atomic64_read(&key->tx_pn);
2925
2926 } else {
2927 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
2928 for (i = 0; i < 5; i++)
2929 u.cmd_v1.tkip_rx_ttak[i] =
2930 cpu_to_le16(tkip_p1k[i]);
2931 }
2932 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002933 break;
2934 case WLAN_CIPHER_SUITE_CCMP:
2935 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Sara Sharon45c458b2016-11-09 15:43:26 +02002936 memcpy(u.cmd.common.key, key->key, key->keylen);
2937 if (new_api)
2938 pn = atomic64_read(&key->tx_pn);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002939 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002940 case WLAN_CIPHER_SUITE_WEP104:
2941 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002942 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002943 case WLAN_CIPHER_SUITE_WEP40:
2944 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002945 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
Johannes Bergba3943b2014-11-12 23:54:48 +01002946 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03002947 case WLAN_CIPHER_SUITE_GCMP_256:
2948 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2949 /* fall through */
2950 case WLAN_CIPHER_SUITE_GCMP:
2951 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002952 memcpy(u.cmd.common.key, key->key, key->keylen);
2953 if (new_api)
2954 pn = atomic64_read(&key->tx_pn);
Ayala Beker2a53d162016-04-07 16:21:57 +03002955 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002956 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002957 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
Sara Sharon45c458b2016-11-09 15:43:26 +02002958 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002959 }
2960
Johannes Bergba3943b2014-11-12 23:54:48 +01002961 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002962 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2963
Sara Sharon45c458b2016-11-09 15:43:26 +02002964 u.cmd.common.key_offset = key_offset;
2965 u.cmd.common.key_flags = key_flags;
David Spinadel85aeb582017-03-30 19:43:53 +03002966 u.cmd.common.sta_id = sta_id;
Sara Sharon45c458b2016-11-09 15:43:26 +02002967
2968 if (new_api) {
2969 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
2970 size = sizeof(u.cmd);
2971 } else {
2972 size = sizeof(u.cmd_v1);
2973 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002974
2975 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002976 if (cmd_flags & CMD_ASYNC)
Sara Sharon45c458b2016-11-09 15:43:26 +02002977 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
2978 &u.cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002979 else
Sara Sharon45c458b2016-11-09 15:43:26 +02002980 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
2981 &u.cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002982
2983 switch (status) {
2984 case ADD_STA_SUCCESS:
2985 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2986 break;
2987 default:
2988 ret = -EIO;
2989 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2990 break;
2991 }
2992
2993 return ret;
2994}
2995
2996static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2997 struct ieee80211_key_conf *keyconf,
2998 u8 sta_id, bool remove_key)
2999{
3000 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3001
3002 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03003003 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3004 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3005 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3006 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3007 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3008 return -EINVAL;
3009
3010 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3011 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01003012 return -EINVAL;
3013
3014 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3015 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3016
3017 if (remove_key) {
3018 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3019 } else {
3020 struct ieee80211_key_seq seq;
3021 const u8 *pn;
3022
Ayala Bekeraa950522016-06-01 00:28:09 +03003023 switch (keyconf->cipher) {
3024 case WLAN_CIPHER_SUITE_AES_CMAC:
3025 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3026 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03003027 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3028 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3029 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3030 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03003031 default:
3032 return -EINVAL;
3033 }
3034
Ayala Beker8e160ab2016-04-11 11:37:38 +03003035 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3036 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3037 igtk_cmd.ctrl_flags |=
3038 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003039 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3040 pn = seq.aes_cmac.pn;
3041 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3042 ((u64) pn[4] << 8) |
3043 ((u64) pn[3] << 16) |
3044 ((u64) pn[2] << 24) |
3045 ((u64) pn[1] << 32) |
3046 ((u64) pn[0] << 40));
3047 }
3048
3049 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3050 remove_key ? "removing" : "installing",
3051 igtk_cmd.sta_id);
3052
Ayala Beker8e160ab2016-04-11 11:37:38 +03003053 if (!iwl_mvm_has_new_rx_api(mvm)) {
3054 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3055 .ctrl_flags = igtk_cmd.ctrl_flags,
3056 .key_id = igtk_cmd.key_id,
3057 .sta_id = igtk_cmd.sta_id,
3058 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3059 };
3060
3061 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3062 ARRAY_SIZE(igtk_cmd_v1.igtk));
3063 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3064 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3065 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003066 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003067 sizeof(igtk_cmd), &igtk_cmd);
3068}
3069
3070
3071static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3072 struct ieee80211_vif *vif,
3073 struct ieee80211_sta *sta)
3074{
Johannes Berg5b530e92014-12-23 16:00:17 +01003075 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003076
3077 if (sta)
3078 return sta->addr;
3079
3080 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02003081 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003082 u8 sta_id = mvmvif->ap_sta_id;
3083 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3084 lockdep_is_held(&mvm->mutex));
3085 return sta->addr;
3086 }
3087
3088
3089 return NULL;
3090}
3091
Johannes Berg2f6319d2014-11-12 23:39:56 +01003092static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3093 struct ieee80211_vif *vif,
3094 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01003095 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003096 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01003097 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003098{
Johannes Berg8ca151b2013-01-24 14:25:36 +01003099 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003100 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003101 struct ieee80211_key_seq seq;
3102 u16 p1k[5];
David Spinadel85aeb582017-03-30 19:43:53 +03003103 u32 sta_id;
3104
3105 if (sta) {
3106 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3107
3108 sta_id = mvm_sta->sta_id;
3109 } else if (vif->type == NL80211_IFTYPE_AP &&
3110 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3111 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3112
3113 sta_id = mvmvif->mcast_sta.sta_id;
3114 } else {
3115 IWL_ERR(mvm, "Failed to find station id\n");
3116 return -EINVAL;
3117 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003118
Johannes Berg8ca151b2013-01-24 14:25:36 +01003119 switch (keyconf->cipher) {
3120 case WLAN_CIPHER_SUITE_TKIP:
David Spinadel85aeb582017-03-30 19:43:53 +03003121 if (vif->type == NL80211_IFTYPE_AP) {
3122 ret = -EINVAL;
3123 break;
3124 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003125 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3126 /* get phase 1 key from mac80211 */
3127 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3128 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
David Spinadel85aeb582017-03-30 19:43:53 +03003129 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003130 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003131 break;
3132 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01003133 case WLAN_CIPHER_SUITE_WEP40:
3134 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03003135 case WLAN_CIPHER_SUITE_GCMP:
3136 case WLAN_CIPHER_SUITE_GCMP_256:
David Spinadel85aeb582017-03-30 19:43:53 +03003137 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003138 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003139 break;
3140 default:
David Spinadel85aeb582017-03-30 19:43:53 +03003141 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003142 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003143 }
3144
Johannes Berg8ca151b2013-01-24 14:25:36 +01003145 return ret;
3146}
3147
Johannes Berg2f6319d2014-11-12 23:39:56 +01003148static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01003149 struct ieee80211_key_conf *keyconf,
3150 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003151{
Sara Sharon45c458b2016-11-09 15:43:26 +02003152 union {
3153 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3154 struct iwl_mvm_add_sta_key_cmd cmd;
3155 } u = {};
3156 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3157 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003158 __le16 key_flags;
Sara Sharon45c458b2016-11-09 15:43:26 +02003159 int ret, size;
Johannes Berg79920742014-11-03 15:43:04 +01003160 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003161
Sara Sharone4f13ad2018-01-15 13:50:59 +02003162 /* This is a valid situation for GTK removal */
David Spinadel85aeb582017-03-30 19:43:53 +03003163 if (sta_id == IWL_MVM_INVALID_STA)
Sara Sharone4f13ad2018-01-15 13:50:59 +02003164 return 0;
David Spinadel85aeb582017-03-30 19:43:53 +03003165
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02003166 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3167 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003168 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3169 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3170
Johannes Bergba3943b2014-11-12 23:54:48 +01003171 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003172 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3173
Sara Sharon45c458b2016-11-09 15:43:26 +02003174 /*
3175 * The fields assigned here are in the same location at the start
3176 * of the command, so we can do this union trick.
3177 */
3178 u.cmd.common.key_flags = key_flags;
3179 u.cmd.common.key_offset = keyconf->hw_key_idx;
3180 u.cmd.common.sta_id = sta_id;
3181
3182 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003183
Johannes Berg8ca151b2013-01-24 14:25:36 +01003184 status = ADD_STA_SUCCESS;
Sara Sharon45c458b2016-11-09 15:43:26 +02003185 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3186 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003187
3188 switch (status) {
3189 case ADD_STA_SUCCESS:
3190 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3191 break;
3192 default:
3193 ret = -EIO;
3194 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3195 break;
3196 }
3197
3198 return ret;
3199}
3200
Johannes Berg2f6319d2014-11-12 23:39:56 +01003201int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3202 struct ieee80211_vif *vif,
3203 struct ieee80211_sta *sta,
3204 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003205 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003206{
Johannes Bergba3943b2014-11-12 23:54:48 +01003207 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003208 struct iwl_mvm_sta *mvm_sta;
David Spinadel85aeb582017-03-30 19:43:53 +03003209 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003210 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03003211 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01003212
3213 lockdep_assert_held(&mvm->mutex);
3214
David Spinadel85aeb582017-03-30 19:43:53 +03003215 if (vif->type != NL80211_IFTYPE_AP ||
3216 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3217 /* Get the station id from the mvm local station table */
3218 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3219 if (!mvm_sta) {
3220 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01003221 return -EINVAL;
3222 }
David Spinadel85aeb582017-03-30 19:43:53 +03003223 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003224
David Spinadel85aeb582017-03-30 19:43:53 +03003225 /*
3226 * It is possible that the 'sta' parameter is NULL, and thus
Beni Leve829b172018-02-20 13:41:54 +02003227 * there is a need to retrieve the sta from the local station
David Spinadel85aeb582017-03-30 19:43:53 +03003228 * table.
3229 */
3230 if (!sta) {
3231 sta = rcu_dereference_protected(
3232 mvm->fw_id_to_mac_id[sta_id],
3233 lockdep_is_held(&mvm->mutex));
3234 if (IS_ERR_OR_NULL(sta)) {
3235 IWL_ERR(mvm, "Invalid station id\n");
3236 return -EINVAL;
3237 }
3238 }
3239
3240 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3241 return -EINVAL;
Beni Leve829b172018-02-20 13:41:54 +02003242 } else {
3243 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3244
3245 sta_id = mvmvif->mcast_sta.sta_id;
3246 }
3247
3248 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3249 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3250 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3251 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3252 goto end;
David Spinadel85aeb582017-03-30 19:43:53 +03003253 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003254
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003255 /* If the key_offset is not pre-assigned, we need to find a
3256 * new offset to use. In normal cases, the offset is not
3257 * pre-assigned, but during HW_RESTART we want to reuse the
3258 * same indices, so we pass them when this function is called.
3259 *
3260 * In D3 entry, we need to hardcoded the indices (because the
3261 * firmware hardcodes the PTK offset to 0). In this case, we
3262 * need to make sure we don't overwrite the hw_key_idx in the
3263 * keyconf structure, because otherwise we cannot configure
3264 * the original ones back when resuming.
3265 */
3266 if (key_offset == STA_KEY_IDX_INVALID) {
3267 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3268 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003269 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003270 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003271 }
3272
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003273 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003274 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01003275 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003276
3277 /*
3278 * For WEP, the same key is used for multicast and unicast. Upload it
3279 * again, using the same key offset, and now pointing the other one
3280 * to the same key slot (offset).
3281 * If this fails, remove the original as well.
3282 */
David Spinadel85aeb582017-03-30 19:43:53 +03003283 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3284 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3285 sta) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003286 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3287 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01003288 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01003289 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003290 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003291 }
3292 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003293
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003294 __set_bit(key_offset, mvm->fw_key_table);
3295
Johannes Berg2f6319d2014-11-12 23:39:56 +01003296end:
3297 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3298 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03003299 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01003300 return ret;
3301}
3302
3303int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3304 struct ieee80211_vif *vif,
3305 struct ieee80211_sta *sta,
3306 struct ieee80211_key_conf *keyconf)
3307{
Johannes Bergba3943b2014-11-12 23:54:48 +01003308 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003309 struct iwl_mvm_sta *mvm_sta;
Sara Sharon0ae98812017-01-04 14:53:58 +02003310 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2dc2a152015-06-16 17:09:18 +02003311 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003312
3313 lockdep_assert_held(&mvm->mutex);
3314
Johannes Berg5f7a1842015-12-11 09:36:10 +01003315 /* Get the station from the mvm local station table */
3316 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Luca Coelho71793b7d2017-03-30 12:04:47 +03003317 if (mvm_sta)
3318 sta_id = mvm_sta->sta_id;
David Spinadel85aeb582017-03-30 19:43:53 +03003319 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3320 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3321
Johannes Berg2f6319d2014-11-12 23:39:56 +01003322
3323 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3324 keyconf->keyidx, sta_id);
3325
Luca Coelho71793b7d2017-03-30 12:04:47 +03003326 if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3327 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3328 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
Johannes Berg2f6319d2014-11-12 23:39:56 +01003329 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3330
3331 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3332 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3333 keyconf->hw_key_idx);
3334 return -ENOENT;
3335 }
3336
Johannes Berg2dc2a152015-06-16 17:09:18 +02003337 /* track which key was deleted last */
3338 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3339 if (mvm->fw_key_deleted[i] < U8_MAX)
3340 mvm->fw_key_deleted[i]++;
3341 }
3342 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3343
David Spinadel85aeb582017-03-30 19:43:53 +03003344 if (sta && !mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003345 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3346 return 0;
3347 }
3348
Johannes Bergba3943b2014-11-12 23:54:48 +01003349 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3350 if (ret)
3351 return ret;
3352
3353 /* delete WEP key twice to get rid of (now useless) offset */
3354 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3355 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3356 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3357
3358 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003359}
3360
Johannes Berg8ca151b2013-01-24 14:25:36 +01003361void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3362 struct ieee80211_vif *vif,
3363 struct ieee80211_key_conf *keyconf,
3364 struct ieee80211_sta *sta, u32 iv32,
3365 u16 *phase1key)
3366{
Beni Levc3eb5362013-02-06 17:22:18 +02003367 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01003368 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003369
Beni Levc3eb5362013-02-06 17:22:18 +02003370 rcu_read_lock();
3371
Johannes Berg5f7a1842015-12-11 09:36:10 +01003372 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3373 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003374 goto unlock;
David Spinadel85aeb582017-03-30 19:43:53 +03003375 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003376 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003377
3378 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02003379 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01003380}
3381
Johannes Berg9cc40712013-02-15 22:47:48 +01003382void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3383 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003384{
Johannes Berg5b577a92013-11-14 18:20:04 +01003385 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003386 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003387 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003388 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03003389 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01003390 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003391 };
3392 int ret;
3393
Sara Sharon854c5702016-01-26 13:17:47 +02003394 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3395 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003396 if (ret)
3397 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3398}
3399
Johannes Berg9cc40712013-02-15 22:47:48 +01003400void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3401 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003402 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003403 u16 cnt, u16 tids, bool more_data,
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003404 bool single_sta_queue)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003405{
Johannes Berg5b577a92013-11-14 18:20:04 +01003406 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003407 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003408 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003409 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003410 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3411 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003412 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003413 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003414 int tid, ret;
3415 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003416
Johannes Berg3e56ead2013-02-15 22:23:18 +01003417 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3418 * Note that this field is reserved and unused by firmware not
3419 * supporting GO uAPSD, so it's safe to always do this.
3420 */
3421 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3422 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3423
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003424 /* If we're releasing frames from aggregation or dqa queues then check
3425 * if all the queues that we're releasing frames from, combined, have:
Johannes Berg3e56ead2013-02-15 22:23:18 +01003426 * - more frames than the service period, in which case more_data
3427 * needs to be set
3428 * - fewer than 'cnt' frames, in which case we need to adjust the
3429 * firmware command (but do that unconditionally)
3430 */
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003431 if (single_sta_queue) {
Johannes Berg3e56ead2013-02-15 22:23:18 +01003432 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003433 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003434
3435 spin_lock_bh(&mvmsta->lock);
3436 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3437 struct iwl_mvm_tid_data *tid_data;
3438 u16 n_queued;
3439
3440 tid_data = &mvmsta->tid_data[tid];
Johannes Berg3e56ead2013-02-15 22:23:18 +01003441
Liad Kaufmandd321622017-04-05 16:25:11 +03003442 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003443 if (n_queued > remaining) {
3444 more_data = true;
3445 remaining = 0;
3446 break;
3447 }
3448 remaining -= n_queued;
3449 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003450 sleep_tx_count = cnt - remaining;
3451 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3452 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003453 spin_unlock_bh(&mvmsta->lock);
3454
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003455 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003456 if (WARN_ON(cnt - remaining == 0)) {
3457 ieee80211_sta_eosp(sta);
3458 return;
3459 }
3460 }
3461
3462 /* Note: this is ignored by firmware not supporting GO uAPSD */
3463 if (more_data)
Sara Sharonced19f22017-02-06 19:09:32 +02003464 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003465
3466 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3467 mvmsta->next_status_eosp = true;
Sara Sharonced19f22017-02-06 19:09:32 +02003468 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003469 } else {
Sara Sharonced19f22017-02-06 19:09:32 +02003470 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003471 }
3472
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003473 /* block the Tx queues until the FW updated the sleep Tx count */
3474 iwl_trans_block_txq_ptrs(mvm->trans, true);
3475
3476 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3477 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003478 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003479 if (ret)
3480 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3481}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003482
Johannes Berg04168412015-06-23 21:22:09 +02003483void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3484 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003485{
3486 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3487 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3488 struct ieee80211_sta *sta;
3489 u32 sta_id = le32_to_cpu(notif->sta_id);
3490
3491 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003492 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003493
3494 rcu_read_lock();
3495 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3496 if (!IS_ERR_OR_NULL(sta))
3497 ieee80211_sta_eosp(sta);
3498 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003499}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003500
3501void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3502 struct iwl_mvm_sta *mvmsta, bool disable)
3503{
3504 struct iwl_mvm_add_sta_cmd cmd = {
3505 .add_modify = STA_MODE_MODIFY,
3506 .sta_id = mvmsta->sta_id,
3507 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3508 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3509 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3510 };
3511 int ret;
3512
Sara Sharon854c5702016-01-26 13:17:47 +02003513 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3514 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003515 if (ret)
3516 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3517}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003518
3519void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3520 struct ieee80211_sta *sta,
3521 bool disable)
3522{
3523 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3524
3525 spin_lock_bh(&mvm_sta->lock);
3526
3527 if (mvm_sta->disable_tx == disable) {
3528 spin_unlock_bh(&mvm_sta->lock);
3529 return;
3530 }
3531
3532 mvm_sta->disable_tx = disable;
3533
Johannes Bergc8f54702017-06-19 23:50:31 +02003534 /* Tell mac80211 to start/stop queuing tx for this station */
3535 ieee80211_sta_block_awake(mvm->hw, sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003536
3537 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3538
3539 spin_unlock_bh(&mvm_sta->lock);
3540}
3541
Sara Sharonced19f22017-02-06 19:09:32 +02003542static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3543 struct iwl_mvm_vif *mvmvif,
3544 struct iwl_mvm_int_sta *sta,
3545 bool disable)
3546{
3547 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3548 struct iwl_mvm_add_sta_cmd cmd = {
3549 .add_modify = STA_MODE_MODIFY,
3550 .sta_id = sta->sta_id,
3551 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3552 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3553 .mac_id_n_color = cpu_to_le32(id),
3554 };
3555 int ret;
3556
3557 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3558 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3559 if (ret)
3560 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3561}
3562
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003563void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3564 struct iwl_mvm_vif *mvmvif,
3565 bool disable)
3566{
3567 struct ieee80211_sta *sta;
3568 struct iwl_mvm_sta *mvm_sta;
3569 int i;
3570
3571 lockdep_assert_held(&mvm->mutex);
3572
3573 /* Block/unblock all the stations of the given mvmvif */
Sara Sharon0ae98812017-01-04 14:53:58 +02003574 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003575 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3576 lockdep_is_held(&mvm->mutex));
3577 if (IS_ERR_OR_NULL(sta))
3578 continue;
3579
3580 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3581 if (mvm_sta->mac_id_n_color !=
3582 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3583 continue;
3584
3585 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3586 }
Sara Sharonced19f22017-02-06 19:09:32 +02003587
3588 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3589 return;
3590
3591 /* Need to block/unblock also multicast station */
3592 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3593 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3594 &mvmvif->mcast_sta, disable);
3595
3596 /*
3597 * Only unblock the broadcast station (FW blocks it for immediate
3598 * quiet, not the driver)
3599 */
3600 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3601 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3602 &mvmvif->bcast_sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003603}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003604
3605void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3606{
3607 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3608 struct iwl_mvm_sta *mvmsta;
3609
3610 rcu_read_lock();
3611
3612 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3613
3614 if (!WARN_ON(!mvmsta))
3615 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3616
3617 rcu_read_unlock();
3618}
Liad Kaufmandd321622017-04-05 16:25:11 +03003619
3620u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3621{
3622 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3623
3624 /*
Luca Coelho2f7a3862017-11-15 15:07:34 +02003625 * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
Liad Kaufmandd321622017-04-05 16:25:11 +03003626 * to align the wrap around of ssn so we compare relevant values.
3627 */
3628 if (mvm->trans->cfg->gen2)
3629 sn &= 0xff;
3630
3631 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3632}