blob: 027ee5e72172c85f9eaa98f1fd1a26489ab74820 [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
Sara Sharon10b2b202016-03-20 16:23:41 +0200255static void iwl_mvm_rx_agg_session_expired(unsigned long data)
256{
257 struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
258 struct iwl_mvm_baid_data *ba_data;
259 struct ieee80211_sta *sta;
260 struct iwl_mvm_sta *mvm_sta;
261 unsigned long timeout;
262
263 rcu_read_lock();
264
265 ba_data = rcu_dereference(*rcu_ptr);
266
267 if (WARN_ON(!ba_data))
268 goto unlock;
269
270 if (!ba_data->timeout)
271 goto unlock;
272
273 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
274 if (time_is_after_jiffies(timeout)) {
275 mod_timer(&ba_data->session_timer, timeout);
276 goto unlock;
277 }
278
279 /* Timer expired */
280 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
Emmanuel Grumbach61dd8a82017-06-12 15:10:09 +0300281
282 /*
283 * sta should be valid unless the following happens:
284 * The firmware asserts which triggers a reconfig flow, but
285 * the reconfig fails before we set the pointer to sta into
286 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
287 * A-MDPU and hence the timer continues to run. Then, the
288 * timer expires and sta is NULL.
289 */
290 if (!sta)
291 goto unlock;
292
Sara Sharon10b2b202016-03-20 16:23:41 +0200293 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Naftali Goldstein20fc6902017-07-11 10:07:32 +0300294 ieee80211_rx_ba_timer_expired(mvm_sta->vif,
295 sta->addr, ba_data->tid);
Sara Sharon10b2b202016-03-20 16:23:41 +0200296unlock:
297 rcu_read_unlock();
298}
299
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300300static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
301 struct ieee80211_sta *sta)
302{
303 unsigned long used_hw_queues;
304 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200305 unsigned int wdg_timeout =
306 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300307 u32 ac;
308
309 lockdep_assert_held(&mvm->mutex);
310
311 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
312
313 /* Find available queues, and allocate them to the ACs */
314 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
315 u8 queue = find_first_zero_bit(&used_hw_queues,
316 mvm->first_agg_queue);
317
318 if (queue >= mvm->first_agg_queue) {
319 IWL_ERR(mvm, "Failed to allocate STA queue\n");
320 return -EBUSY;
321 }
322
323 __set_bit(queue, &used_hw_queues);
324 mvmsta->hw_queue[ac] = queue;
325 }
326
327 /* Found a place for all queues - enable them */
328 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
329 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300330 mvmsta->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300331 iwl_mvm_ac_to_tx_fifo[ac], 0,
332 wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300333 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
334 }
335
336 return 0;
337}
338
339static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
340 struct ieee80211_sta *sta)
341{
342 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
343 unsigned long sta_msk;
344 int i;
345
346 lockdep_assert_held(&mvm->mutex);
347
348 /* disable the TDLS STA-specific queues */
349 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200350 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300351 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300352}
353
Liad Kaufman9794c642015-08-19 17:34:28 +0300354/* Disable aggregations for a bitmap of TIDs for a given station */
355static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
356 unsigned long disable_agg_tids,
357 bool remove_queue)
358{
359 struct iwl_mvm_add_sta_cmd cmd = {};
360 struct ieee80211_sta *sta;
361 struct iwl_mvm_sta *mvmsta;
362 u32 status;
363 u8 sta_id;
364 int ret;
365
Sara Sharonbb497012016-09-29 14:52:40 +0300366 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
367 return -EINVAL;
368
Liad Kaufman9794c642015-08-19 17:34:28 +0300369 spin_lock_bh(&mvm->queue_info_lock);
370 sta_id = mvm->queue_info[queue].ra_sta_id;
371 spin_unlock_bh(&mvm->queue_info_lock);
372
373 rcu_read_lock();
374
375 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
376
377 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
378 rcu_read_unlock();
379 return -EINVAL;
380 }
381
382 mvmsta = iwl_mvm_sta_from_mac80211(sta);
383
384 mvmsta->tid_disable_agg |= disable_agg_tids;
385
386 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
387 cmd.sta_id = mvmsta->sta_id;
388 cmd.add_modify = STA_MODE_MODIFY;
389 cmd.modify_mask = STA_MODIFY_QUEUES;
390 if (disable_agg_tids)
391 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
392 if (remove_queue)
393 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
394 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
395 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
396
397 rcu_read_unlock();
398
399 /* Notify FW of queue removal from the STA queues */
400 status = ADD_STA_SUCCESS;
401 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
402 iwl_mvm_add_sta_cmd_size(mvm),
403 &cmd, &status);
404
405 return ret;
406}
407
Liad Kaufman42db09c2016-05-02 14:01:14 +0300408static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
409{
410 struct ieee80211_sta *sta;
411 struct iwl_mvm_sta *mvmsta;
412 unsigned long tid_bitmap;
413 unsigned long agg_tids = 0;
Sharon Dvir806911d2017-05-21 12:09:49 +0300414 u8 sta_id;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300415 int tid;
416
417 lockdep_assert_held(&mvm->mutex);
418
Sara Sharonbb497012016-09-29 14:52:40 +0300419 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
420 return -EINVAL;
421
Liad Kaufman42db09c2016-05-02 14:01:14 +0300422 spin_lock_bh(&mvm->queue_info_lock);
423 sta_id = mvm->queue_info[queue].ra_sta_id;
424 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
425 spin_unlock_bh(&mvm->queue_info_lock);
426
427 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
428 lockdep_is_held(&mvm->mutex));
429
430 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
431 return -EINVAL;
432
433 mvmsta = iwl_mvm_sta_from_mac80211(sta);
434
435 spin_lock_bh(&mvmsta->lock);
436 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
437 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
438 agg_tids |= BIT(tid);
439 }
440 spin_unlock_bh(&mvmsta->lock);
441
442 return agg_tids;
443}
444
Liad Kaufman9794c642015-08-19 17:34:28 +0300445/*
446 * Remove a queue from a station's resources.
447 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
448 * doesn't disable the queue
449 */
450static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
451{
452 struct ieee80211_sta *sta;
453 struct iwl_mvm_sta *mvmsta;
454 unsigned long tid_bitmap;
455 unsigned long disable_agg_tids = 0;
456 u8 sta_id;
457 int tid;
458
459 lockdep_assert_held(&mvm->mutex);
460
Sara Sharonbb497012016-09-29 14:52:40 +0300461 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
462 return -EINVAL;
463
Liad Kaufman9794c642015-08-19 17:34:28 +0300464 spin_lock_bh(&mvm->queue_info_lock);
465 sta_id = mvm->queue_info[queue].ra_sta_id;
466 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
467 spin_unlock_bh(&mvm->queue_info_lock);
468
469 rcu_read_lock();
470
471 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
472
473 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
474 rcu_read_unlock();
475 return 0;
476 }
477
478 mvmsta = iwl_mvm_sta_from_mac80211(sta);
479
480 spin_lock_bh(&mvmsta->lock);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300481 /* Unmap MAC queues and TIDs from this queue */
Liad Kaufman9794c642015-08-19 17:34:28 +0300482 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300483 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
484 disable_agg_tids |= BIT(tid);
Sara Sharon6862fce2017-02-22 19:34:17 +0200485 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman9794c642015-08-19 17:34:28 +0300486 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300487
Liad Kaufman42db09c2016-05-02 14:01:14 +0300488 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
Liad Kaufman9794c642015-08-19 17:34:28 +0300489 spin_unlock_bh(&mvmsta->lock);
490
491 rcu_read_unlock();
492
Liad Kaufman9794c642015-08-19 17:34:28 +0300493 return disable_agg_tids;
494}
495
Sara Sharon01796ff2016-11-16 17:04:36 +0200496static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
497 bool same_sta)
498{
499 struct iwl_mvm_sta *mvmsta;
500 u8 txq_curr_ac, sta_id, tid;
501 unsigned long disable_agg_tids = 0;
502 int ret;
503
504 lockdep_assert_held(&mvm->mutex);
505
Sara Sharonbb497012016-09-29 14:52:40 +0300506 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
507 return -EINVAL;
508
Sara Sharon01796ff2016-11-16 17:04:36 +0200509 spin_lock_bh(&mvm->queue_info_lock);
510 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
511 sta_id = mvm->queue_info[queue].ra_sta_id;
512 tid = mvm->queue_info[queue].txq_tid;
513 spin_unlock_bh(&mvm->queue_info_lock);
514
515 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Sharon Dvire3df1e42017-02-21 10:41:31 +0200516 if (WARN_ON(!mvmsta))
517 return -EINVAL;
Sara Sharon01796ff2016-11-16 17:04:36 +0200518
519 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
520 /* Disable the queue */
521 if (disable_agg_tids)
522 iwl_mvm_invalidate_sta_queue(mvm, queue,
523 disable_agg_tids, false);
524
525 ret = iwl_mvm_disable_txq(mvm, queue,
526 mvmsta->vif->hw_queue[txq_curr_ac],
527 tid, 0);
528 if (ret) {
529 /* Re-mark the inactive queue as inactive */
530 spin_lock_bh(&mvm->queue_info_lock);
531 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
532 spin_unlock_bh(&mvm->queue_info_lock);
533 IWL_ERR(mvm,
534 "Failed to free inactive queue %d (ret=%d)\n",
535 queue, ret);
536
537 return ret;
538 }
539
540 /* If TXQ is allocated to another STA, update removal in FW */
541 if (!same_sta)
542 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
543
544 return 0;
545}
546
Liad Kaufman42db09c2016-05-02 14:01:14 +0300547static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
548 unsigned long tfd_queue_mask, u8 ac)
549{
550 int queue = 0;
551 u8 ac_to_queue[IEEE80211_NUM_ACS];
552 int i;
553
554 lockdep_assert_held(&mvm->queue_info_lock);
Sara Sharonbb497012016-09-29 14:52:40 +0300555 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
556 return -EINVAL;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300557
558 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
559
560 /* See what ACs the existing queues for this STA have */
561 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
562 /* Only DATA queues can be shared */
563 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
564 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
565 continue;
566
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200567 /* Don't try and take queues being reconfigured */
568 if (mvm->queue_info[queue].status ==
569 IWL_MVM_QUEUE_RECONFIGURING)
570 continue;
571
Liad Kaufman42db09c2016-05-02 14:01:14 +0300572 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
573 }
574
575 /*
576 * The queue to share is chosen only from DATA queues as follows (in
577 * descending priority):
578 * 1. An AC_BE queue
579 * 2. Same AC queue
580 * 3. Highest AC queue that is lower than new AC
581 * 4. Any existing AC (there always is at least 1 DATA queue)
582 */
583
584 /* Priority 1: An AC_BE queue */
585 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
586 queue = ac_to_queue[IEEE80211_AC_BE];
587 /* Priority 2: Same AC queue */
588 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
589 queue = ac_to_queue[ac];
590 /* Priority 3a: If new AC is VO and VI exists - use VI */
591 else if (ac == IEEE80211_AC_VO &&
592 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
593 queue = ac_to_queue[IEEE80211_AC_VI];
594 /* Priority 3b: No BE so only AC less than the new one is BK */
595 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
596 queue = ac_to_queue[IEEE80211_AC_BK];
597 /* Priority 4a: No BE nor BK - use VI if exists */
598 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
599 queue = ac_to_queue[IEEE80211_AC_VI];
600 /* Priority 4b: No BE, BK nor VI - use VO if exists */
601 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
602 queue = ac_to_queue[IEEE80211_AC_VO];
603
604 /* Make sure queue found (or not) is legal */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200605 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
606 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
607 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
Liad Kaufman42db09c2016-05-02 14:01:14 +0300608 IWL_ERR(mvm, "No DATA queues available to share\n");
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200609 return -ENOSPC;
610 }
611
612 /* Make sure the queue isn't in the middle of being reconfigured */
613 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
614 IWL_ERR(mvm,
615 "TXQ %d is in the middle of re-config - try again\n",
616 queue);
617 return -EBUSY;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300618 }
619
620 return queue;
621}
622
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200623/*
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200624 * If a given queue has a higher AC than the TID stream that is being compared
625 * to, the queue needs to be redirected to the lower AC. This function does that
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200626 * in such a case, otherwise - if no redirection required - it does nothing,
627 * unless the %force param is true.
628 */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200629int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
630 int ac, int ssn, unsigned int wdg_timeout,
631 bool force)
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200632{
633 struct iwl_scd_txq_cfg_cmd cmd = {
634 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200635 .action = SCD_CFG_DISABLE_QUEUE,
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200636 };
637 bool shared_queue;
638 unsigned long mq;
639 int ret;
640
Sara Sharonbb497012016-09-29 14:52:40 +0300641 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
642 return -EINVAL;
643
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200644 /*
645 * If the AC is lower than current one - FIFO needs to be redirected to
646 * the lowest one of the streams in the queue. Check if this is needed
647 * here.
648 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
649 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
650 * we need to check if the numerical value of X is LARGER than of Y.
651 */
652 spin_lock_bh(&mvm->queue_info_lock);
653 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
654 spin_unlock_bh(&mvm->queue_info_lock);
655
656 IWL_DEBUG_TX_QUEUES(mvm,
657 "No redirection needed on TXQ #%d\n",
658 queue);
659 return 0;
660 }
661
662 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
663 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200664 cmd.tid = mvm->queue_info[queue].txq_tid;
Sara Sharon34e10862017-02-23 13:15:07 +0200665 mq = mvm->hw_queue_to_mac80211[queue];
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200666 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
667 spin_unlock_bh(&mvm->queue_info_lock);
668
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200669 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200670 queue, iwl_mvm_ac_to_tx_fifo[ac]);
671
672 /* Stop MAC queues and wait for this queue to empty */
673 iwl_mvm_stop_mac_queues(mvm, mq);
Sara Sharona1a57872017-03-05 11:38:58 +0200674 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200675 if (ret) {
676 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
677 queue);
678 ret = -EIO;
679 goto out;
680 }
681
682 /* Before redirecting the queue we need to de-activate it */
683 iwl_trans_txq_disable(mvm->trans, queue, false);
684 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
685 if (ret)
686 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
687 ret);
688
689 /* Make sure the SCD wrptr is correctly set before reconfiguring */
Sara Sharonca3b9c62016-06-30 16:14:02 +0300690 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200691
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200692 /* Update the TID "owner" of the queue */
693 spin_lock_bh(&mvm->queue_info_lock);
694 mvm->queue_info[queue].txq_tid = tid;
695 spin_unlock_bh(&mvm->queue_info_lock);
696
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200697 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
698
699 /* Redirect to lower AC */
700 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
701 cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
702 ssn);
703
704 /* Update AC marking of the queue */
705 spin_lock_bh(&mvm->queue_info_lock);
706 mvm->queue_info[queue].mac80211_ac = ac;
707 spin_unlock_bh(&mvm->queue_info_lock);
708
709 /*
710 * Mark queue as shared in transport if shared
711 * Note this has to be done after queue enablement because enablement
712 * can also set this value, and there is no indication there to shared
713 * queues
714 */
715 if (shared_queue)
716 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
717
718out:
719 /* Continue using the MAC queues */
720 iwl_mvm_start_mac_queues(mvm, mq);
721
722 return ret;
723}
724
Sara Sharon310181e2017-01-17 14:27:48 +0200725static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
726 struct ieee80211_sta *sta, u8 ac,
727 int tid)
728{
729 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
730 unsigned int wdg_timeout =
731 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
732 u8 mac_queue = mvmsta->vif->hw_queue[ac];
733 int queue = -1;
734
735 lockdep_assert_held(&mvm->mutex);
736
737 IWL_DEBUG_TX_QUEUES(mvm,
738 "Allocating queue for sta %d on tid %d\n",
739 mvmsta->sta_id, tid);
740 queue = iwl_mvm_tvqm_enable_txq(mvm, mac_queue, mvmsta->sta_id, tid,
741 wdg_timeout);
742 if (queue < 0)
743 return queue;
744
745 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
746
747 spin_lock_bh(&mvmsta->lock);
748 mvmsta->tid_data[tid].txq_id = queue;
749 mvmsta->tid_data[tid].is_tid_active = true;
Sara Sharon310181e2017-01-17 14:27:48 +0200750 spin_unlock_bh(&mvmsta->lock);
751
Sara Sharon310181e2017-01-17 14:27:48 +0200752 return 0;
753}
754
Liad Kaufman24afba72015-07-28 18:56:08 +0300755static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
756 struct ieee80211_sta *sta, u8 ac, int tid,
757 struct ieee80211_hdr *hdr)
758{
759 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
760 struct iwl_trans_txq_scd_cfg cfg = {
761 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
762 .sta_id = mvmsta->sta_id,
763 .tid = tid,
764 .frame_limit = IWL_FRAME_LIMIT,
765 };
766 unsigned int wdg_timeout =
767 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
768 u8 mac_queue = mvmsta->vif->hw_queue[ac];
769 int queue = -1;
Sara Sharon01796ff2016-11-16 17:04:36 +0200770 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman9794c642015-08-19 17:34:28 +0300771 unsigned long disable_agg_tids = 0;
772 enum iwl_mvm_agg_state queue_state;
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300773 bool shared_queue = false, inc_ssn;
Liad Kaufman24afba72015-07-28 18:56:08 +0300774 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300775 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300776 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300777
778 lockdep_assert_held(&mvm->mutex);
779
Sara Sharon310181e2017-01-17 14:27:48 +0200780 if (iwl_mvm_has_new_tx_api(mvm))
781 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
782
Liad Kaufman42db09c2016-05-02 14:01:14 +0300783 spin_lock_bh(&mvmsta->lock);
784 tfd_queue_mask = mvmsta->tfd_queue_msk;
785 spin_unlock_bh(&mvmsta->lock);
786
Liad Kaufmand2515a92016-03-23 16:31:08 +0200787 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300788
789 /*
790 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
791 * exists
792 */
793 if (!ieee80211_is_data_qos(hdr->frame_control) ||
794 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300795 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
796 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300797 IWL_MVM_DQA_MAX_MGMT_QUEUE);
798 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
799 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
800 queue);
801
802 /* If no such queue is found, we'll use a DATA queue instead */
803 }
804
Liad Kaufman9794c642015-08-19 17:34:28 +0300805 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
806 (mvm->queue_info[mvmsta->reserved_queue].status ==
807 IWL_MVM_QUEUE_RESERVED ||
808 mvm->queue_info[mvmsta->reserved_queue].status ==
809 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300810 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300811 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300812 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
813 }
814
815 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300816 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
817 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300818 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300819
820 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300821 * Check if this queue is already allocated but inactive.
822 * In such a case, we'll need to first free this queue before enabling
823 * it again, so we'll mark it as reserved to make sure no new traffic
824 * arrives on it
825 */
826 if (queue > 0 &&
827 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
828 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
829 using_inactive_queue = true;
Sara Sharon01796ff2016-11-16 17:04:36 +0200830 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman9794c642015-08-19 17:34:28 +0300831 IWL_DEBUG_TX_QUEUES(mvm,
832 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
833 queue, mvmsta->sta_id, tid);
834 }
835
Liad Kaufman42db09c2016-05-02 14:01:14 +0300836 /* No free queue - we'll have to share */
837 if (queue <= 0) {
838 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
839 if (queue > 0) {
840 shared_queue = true;
841 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
842 }
843 }
844
Liad Kaufman9794c642015-08-19 17:34:28 +0300845 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300846 * Mark TXQ as ready, even though it hasn't been fully configured yet,
847 * to make sure no one else takes it.
848 * This will allow avoiding re-acquiring the lock at the end of the
849 * configuration. On error we'll mark it back as free.
850 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300851 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300852 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300853
Liad Kaufmand2515a92016-03-23 16:31:08 +0200854 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300855
Liad Kaufman42db09c2016-05-02 14:01:14 +0300856 /* This shouldn't happen - out of queues */
857 if (WARN_ON(queue <= 0)) {
858 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
859 tid, cfg.sta_id);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200860 return queue;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300861 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300862
863 /*
864 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
865 * but for configuring the SCD to send A-MPDUs we need to mark the queue
866 * as aggregatable.
867 * Mark all DATA queues as allowing to be aggregated at some point
868 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300869 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
870 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300871
Liad Kaufman9794c642015-08-19 17:34:28 +0300872 /*
873 * If this queue was previously inactive (idle) - we need to free it
874 * first
875 */
876 if (using_inactive_queue) {
Sara Sharon01796ff2016-11-16 17:04:36 +0200877 ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
878 if (ret)
Liad Kaufman9794c642015-08-19 17:34:28 +0300879 return ret;
Liad Kaufman9794c642015-08-19 17:34:28 +0300880 }
881
Liad Kaufman42db09c2016-05-02 14:01:14 +0300882 IWL_DEBUG_TX_QUEUES(mvm,
883 "Allocating %squeue #%d to sta %d on tid %d\n",
884 shared_queue ? "shared " : "", queue,
885 mvmsta->sta_id, tid);
886
887 if (shared_queue) {
888 /* Disable any open aggs on this queue */
889 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
890
891 if (disable_agg_tids) {
892 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
893 queue);
894 iwl_mvm_invalidate_sta_queue(mvm, queue,
895 disable_agg_tids, false);
896 }
Liad Kaufman42db09c2016-05-02 14:01:14 +0300897 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300898
899 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300900 inc_ssn = iwl_mvm_enable_txq(mvm, queue, mac_queue,
901 ssn, &cfg, wdg_timeout);
902 if (inc_ssn) {
903 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
904 le16_add_cpu(&hdr->seq_ctrl, 0x10);
905 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300906
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200907 /*
908 * Mark queue as shared in transport if shared
909 * Note this has to be done after queue enablement because enablement
910 * can also set this value, and there is no indication there to shared
911 * queues
912 */
913 if (shared_queue)
914 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
915
Liad Kaufman24afba72015-07-28 18:56:08 +0300916 spin_lock_bh(&mvmsta->lock);
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300917 /*
918 * This looks racy, but it is not. We have only one packet for
919 * this ra/tid in our Tx path since we stop the Qdisc when we
920 * need to allocate a new TFD queue.
921 */
922 if (inc_ssn)
923 mvmsta->tid_data[tid].seq_number += 0x10;
Liad Kaufman24afba72015-07-28 18:56:08 +0300924 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300925 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300926 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300927 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300928
929 if (mvmsta->reserved_queue == queue)
930 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
931 spin_unlock_bh(&mvmsta->lock);
932
Liad Kaufman42db09c2016-05-02 14:01:14 +0300933 if (!shared_queue) {
934 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
935 if (ret)
936 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300937
Liad Kaufman42db09c2016-05-02 14:01:14 +0300938 /* If we need to re-enable aggregations... */
939 if (queue_state == IWL_AGG_ON) {
940 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
941 if (ret)
942 goto out_err;
943 }
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200944 } else {
945 /* Redirect queue, if needed */
946 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
947 wdg_timeout, false);
948 if (ret)
949 goto out_err;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300950 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300951
Liad Kaufman42db09c2016-05-02 14:01:14 +0300952 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300953
954out_err:
955 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
956
957 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300958}
959
Liad Kaufman19aefa42016-03-08 14:29:51 +0200960static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
961{
962 struct iwl_scd_txq_cfg_cmd cmd = {
963 .scd_queue = queue,
964 .action = SCD_CFG_UPDATE_QUEUE_TID,
965 };
Liad Kaufman19aefa42016-03-08 14:29:51 +0200966 int tid;
967 unsigned long tid_bitmap;
968 int ret;
969
970 lockdep_assert_held(&mvm->mutex);
971
Sara Sharonbb497012016-09-29 14:52:40 +0300972 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
973 return;
974
Liad Kaufman19aefa42016-03-08 14:29:51 +0200975 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200976 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
977 spin_unlock_bh(&mvm->queue_info_lock);
978
979 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
980 return;
981
982 /* Find any TID for queue */
983 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
984 cmd.tid = tid;
985 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
986
987 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
Liad Kaufman341ca402016-09-18 14:51:59 +0300988 if (ret) {
Liad Kaufman19aefa42016-03-08 14:29:51 +0200989 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
990 queue, ret);
Liad Kaufman341ca402016-09-18 14:51:59 +0300991 return;
992 }
993
994 spin_lock_bh(&mvm->queue_info_lock);
995 mvm->queue_info[queue].txq_tid = tid;
996 spin_unlock_bh(&mvm->queue_info_lock);
997 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
998 queue, tid);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200999}
1000
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001001static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
1002{
1003 struct ieee80211_sta *sta;
1004 struct iwl_mvm_sta *mvmsta;
Sharon Dvir806911d2017-05-21 12:09:49 +03001005 u8 sta_id;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001006 int tid = -1;
1007 unsigned long tid_bitmap;
1008 unsigned int wdg_timeout;
1009 int ssn;
1010 int ret = true;
1011
Sara Sharonbb497012016-09-29 14:52:40 +03001012 /* queue sharing is disabled on new TX path */
1013 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1014 return;
1015
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001016 lockdep_assert_held(&mvm->mutex);
1017
1018 spin_lock_bh(&mvm->queue_info_lock);
1019 sta_id = mvm->queue_info[queue].ra_sta_id;
1020 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1021 spin_unlock_bh(&mvm->queue_info_lock);
1022
1023 /* Find TID for queue, and make sure it is the only one on the queue */
1024 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1025 if (tid_bitmap != BIT(tid)) {
1026 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
1027 queue, tid_bitmap);
1028 return;
1029 }
1030
1031 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
1032 tid);
1033
1034 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1035 lockdep_is_held(&mvm->mutex));
1036
1037 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1038 return;
1039
1040 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1041 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1042
1043 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1044
1045 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
1046 tid_to_mac80211_ac[tid], ssn,
1047 wdg_timeout, true);
1048 if (ret) {
1049 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1050 return;
1051 }
1052
1053 /* If aggs should be turned back on - do it */
1054 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
Emmanuel Grumbach9cd70e82016-09-20 13:40:33 +03001055 struct iwl_mvm_add_sta_cmd cmd = {0};
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001056
1057 mvmsta->tid_disable_agg &= ~BIT(tid);
1058
1059 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1060 cmd.sta_id = mvmsta->sta_id;
1061 cmd.add_modify = STA_MODE_MODIFY;
1062 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1063 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1064 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1065
1066 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1067 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1068 if (!ret) {
1069 IWL_DEBUG_TX_QUEUES(mvm,
1070 "TXQ #%d is now aggregated again\n",
1071 queue);
1072
1073 /* Mark queue intenally as aggregating again */
1074 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1075 }
1076 }
1077
1078 spin_lock_bh(&mvm->queue_info_lock);
1079 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1080 spin_unlock_bh(&mvm->queue_info_lock);
1081}
1082
Liad Kaufman24afba72015-07-28 18:56:08 +03001083static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1084{
1085 if (tid == IWL_MAX_TID_COUNT)
1086 return IEEE80211_AC_VO; /* MGMT */
1087
1088 return tid_to_mac80211_ac[tid];
1089}
1090
1091static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
1092 struct ieee80211_sta *sta, int tid)
1093{
1094 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1095 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1096 struct sk_buff *skb;
1097 struct ieee80211_hdr *hdr;
1098 struct sk_buff_head deferred_tx;
1099 u8 mac_queue;
1100 bool no_queue = false; /* Marks if there is a problem with the queue */
1101 u8 ac;
1102
1103 lockdep_assert_held(&mvm->mutex);
1104
1105 skb = skb_peek(&tid_data->deferred_tx_frames);
1106 if (!skb)
1107 return;
1108 hdr = (void *)skb->data;
1109
1110 ac = iwl_mvm_tid_to_ac_queue(tid);
1111 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
1112
Sara Sharon6862fce2017-02-22 19:34:17 +02001113 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE &&
Liad Kaufman24afba72015-07-28 18:56:08 +03001114 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
1115 IWL_ERR(mvm,
1116 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
1117 mvmsta->sta_id, tid);
1118
1119 /*
1120 * Mark queue as problematic so later the deferred traffic is
1121 * freed, as we can do nothing with it
1122 */
1123 no_queue = true;
1124 }
1125
1126 __skb_queue_head_init(&deferred_tx);
1127
Liad Kaufmand2515a92016-03-23 16:31:08 +02001128 /* Disable bottom-halves when entering TX path */
1129 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +03001130 spin_lock(&mvmsta->lock);
1131 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
Liad Kaufmanad5de732016-09-27 16:01:10 +03001132 mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
Liad Kaufman24afba72015-07-28 18:56:08 +03001133 spin_unlock(&mvmsta->lock);
1134
Liad Kaufman24afba72015-07-28 18:56:08 +03001135 while ((skb = __skb_dequeue(&deferred_tx)))
1136 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1137 ieee80211_free_txskb(mvm->hw, skb);
1138 local_bh_enable();
1139
1140 /* Wake queue */
1141 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1142}
1143
1144void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1145{
1146 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1147 add_stream_wk);
1148 struct ieee80211_sta *sta;
1149 struct iwl_mvm_sta *mvmsta;
1150 unsigned long deferred_tid_traffic;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001151 int queue, sta_id, tid;
Liad Kaufman24afba72015-07-28 18:56:08 +03001152
Liad Kaufman9794c642015-08-19 17:34:28 +03001153 /* Check inactivity of queues */
1154 iwl_mvm_inactivity_check(mvm);
1155
Liad Kaufman24afba72015-07-28 18:56:08 +03001156 mutex_lock(&mvm->mutex);
1157
Sara Sharon34e10862017-02-23 13:15:07 +02001158 /* No queue reconfiguration in TVQM mode */
1159 if (iwl_mvm_has_new_tx_api(mvm))
1160 goto alloc_queues;
1161
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001162 /* Reconfigure queues requiring reconfiguation */
Sara Sharon34e10862017-02-23 13:15:07 +02001163 for (queue = 0; queue < ARRAY_SIZE(mvm->queue_info); queue++) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001164 bool reconfig;
Liad Kaufman19aefa42016-03-08 14:29:51 +02001165 bool change_owner;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001166
1167 spin_lock_bh(&mvm->queue_info_lock);
1168 reconfig = (mvm->queue_info[queue].status ==
1169 IWL_MVM_QUEUE_RECONFIGURING);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001170
1171 /*
1172 * We need to take into account a situation in which a TXQ was
1173 * allocated to TID x, and then turned shared by adding TIDs y
1174 * and z. If TID x becomes inactive and is removed from the TXQ,
1175 * ownership must be given to one of the remaining TIDs.
1176 * This is mainly because if TID x continues - a new queue can't
1177 * be allocated for it as long as it is an owner of another TXQ.
1178 */
1179 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1180 BIT(mvm->queue_info[queue].txq_tid)) &&
1181 (mvm->queue_info[queue].status ==
1182 IWL_MVM_QUEUE_SHARED);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001183 spin_unlock_bh(&mvm->queue_info_lock);
1184
1185 if (reconfig)
1186 iwl_mvm_unshare_queue(mvm, queue);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001187 else if (change_owner)
1188 iwl_mvm_change_queue_owner(mvm, queue);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001189 }
1190
Sara Sharon34e10862017-02-23 13:15:07 +02001191alloc_queues:
Liad Kaufman24afba72015-07-28 18:56:08 +03001192 /* Go over all stations with deferred traffic */
1193 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1194 IWL_MVM_STATION_COUNT) {
1195 clear_bit(sta_id, mvm->sta_deferred_frames);
1196 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1197 lockdep_is_held(&mvm->mutex));
1198 if (IS_ERR_OR_NULL(sta))
1199 continue;
1200
1201 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1202 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1203
1204 for_each_set_bit(tid, &deferred_tid_traffic,
1205 IWL_MAX_TID_COUNT + 1)
1206 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1207 }
1208
1209 mutex_unlock(&mvm->mutex);
1210}
1211
1212static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001213 struct ieee80211_sta *sta,
1214 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +03001215{
1216 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1217 int queue;
Sara Sharon01796ff2016-11-16 17:04:36 +02001218 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman24afba72015-07-28 18:56:08 +03001219
Sara Sharon396952e2017-02-22 19:40:55 +02001220 /* queue reserving is disabled on new TX path */
1221 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1222 return 0;
1223
Liad Kaufman9794c642015-08-19 17:34:28 +03001224 /*
1225 * Check for inactive queues, so we don't reach a situation where we
1226 * can't add a STA due to a shortage in queues that doesn't really exist
1227 */
1228 iwl_mvm_inactivity_check(mvm);
1229
Liad Kaufman24afba72015-07-28 18:56:08 +03001230 spin_lock_bh(&mvm->queue_info_lock);
1231
1232 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +03001233 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1234 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +03001235 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1236 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +03001237 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1238 else
Liad Kaufman9794c642015-08-19 17:34:28 +03001239 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1240 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001241 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +03001242 if (queue < 0) {
1243 spin_unlock_bh(&mvm->queue_info_lock);
1244 IWL_ERR(mvm, "No available queues for new station\n");
1245 return -ENOSPC;
Sara Sharon01796ff2016-11-16 17:04:36 +02001246 } else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
1247 /*
1248 * If this queue is already allocated but inactive we'll need to
1249 * first free this queue before enabling it again, we'll mark
1250 * it as reserved to make sure no new traffic arrives on it
1251 */
1252 using_inactive_queue = true;
1253 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman24afba72015-07-28 18:56:08 +03001254 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03001255 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +03001256
1257 spin_unlock_bh(&mvm->queue_info_lock);
1258
1259 mvmsta->reserved_queue = queue;
1260
Sara Sharon01796ff2016-11-16 17:04:36 +02001261 if (using_inactive_queue)
1262 iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
1263
Liad Kaufman24afba72015-07-28 18:56:08 +03001264 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1265 queue, mvmsta->sta_id);
1266
1267 return 0;
1268}
1269
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001270/*
1271 * In DQA mode, after a HW restart the queues should be allocated as before, in
1272 * order to avoid race conditions when there are shared queues. This function
1273 * does the re-mapping and queue allocation.
1274 *
1275 * Note that re-enabling aggregations isn't done in this function.
1276 */
1277static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1278 struct iwl_mvm_sta *mvm_sta)
1279{
1280 unsigned int wdg_timeout =
1281 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1282 int i;
1283 struct iwl_trans_txq_scd_cfg cfg = {
1284 .sta_id = mvm_sta->sta_id,
1285 .frame_limit = IWL_FRAME_LIMIT,
1286 };
1287
Johannes Berg03c902b2016-12-02 12:03:36 +01001288 /* Make sure reserved queue is still marked as such (if allocated) */
1289 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1290 mvm->queue_info[mvm_sta->reserved_queue].status =
1291 IWL_MVM_QUEUE_RESERVED;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001292
1293 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1294 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1295 int txq_id = tid_data->txq_id;
1296 int ac;
1297 u8 mac_queue;
1298
Sara Sharon6862fce2017-02-22 19:34:17 +02001299 if (txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001300 continue;
1301
1302 skb_queue_head_init(&tid_data->deferred_tx_frames);
1303
1304 ac = tid_to_mac80211_ac[i];
1305 mac_queue = mvm_sta->vif->hw_queue[ac];
1306
Sara Sharon310181e2017-01-17 14:27:48 +02001307 if (iwl_mvm_has_new_tx_api(mvm)) {
1308 IWL_DEBUG_TX_QUEUES(mvm,
1309 "Re-mapping sta %d tid %d\n",
1310 mvm_sta->sta_id, i);
1311 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mac_queue,
1312 mvm_sta->sta_id,
1313 i, wdg_timeout);
1314 tid_data->txq_id = txq_id;
1315 } else {
1316 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001317
Sara Sharon310181e2017-01-17 14:27:48 +02001318 cfg.tid = i;
1319 cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
1320 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1321 txq_id ==
1322 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001323
Sara Sharon310181e2017-01-17 14:27:48 +02001324 IWL_DEBUG_TX_QUEUES(mvm,
1325 "Re-mapping sta %d tid %d to queue %d\n",
1326 mvm_sta->sta_id, i, txq_id);
1327
1328 iwl_mvm_enable_txq(mvm, txq_id, mac_queue, seq, &cfg,
1329 wdg_timeout);
Sara Sharon34e10862017-02-23 13:15:07 +02001330 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
Sara Sharon310181e2017-01-17 14:27:48 +02001331 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001332 }
1333
1334 atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
1335}
1336
Johannes Berg8ca151b2013-01-24 14:25:36 +01001337int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1338 struct ieee80211_vif *vif,
1339 struct ieee80211_sta *sta)
1340{
1341 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001342 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001343 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001344 int i, ret, sta_id;
1345
1346 lockdep_assert_held(&mvm->mutex);
1347
1348 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001349 sta_id = iwl_mvm_find_free_sta_id(mvm,
1350 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001351 else
1352 sta_id = mvm_sta->sta_id;
1353
Sara Sharon0ae98812017-01-04 14:53:58 +02001354 if (sta_id == IWL_MVM_INVALID_STA)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001355 return -ENOSPC;
1356
1357 spin_lock_init(&mvm_sta->lock);
1358
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001359 /* In DQA mode, if this is a HW restart, re-alloc existing queues */
1360 if (iwl_mvm_is_dqa_supported(mvm) &&
1361 test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1362 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1363 goto update_fw;
1364 }
1365
Johannes Berg8ca151b2013-01-24 14:25:36 +01001366 mvm_sta->sta_id = sta_id;
1367 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1368 mvmvif->color);
1369 mvm_sta->vif = vif;
Liad Kaufmana58bb462017-05-28 14:20:04 +03001370 if (!mvm->trans->cfg->gen2)
1371 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1372 else
1373 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001374 mvm_sta->tx_protection = 0;
1375 mvm_sta->tt_tx_protection = false;
Sara Sharonced19f22017-02-06 19:09:32 +02001376 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001377
1378 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001379 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +03001380 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001381 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001382
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001383 /*
1384 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1385 * and then they'll be allocated dynamically
1386 */
1387 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001388 ret = iwl_mvm_tdls_sta_init(mvm, sta);
1389 if (ret)
1390 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +03001391 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001392 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1393 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1394 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1395 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001396
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001397 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001398 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001399 u16 seq = mvm_sta->tid_data[i].seq_number;
1400 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1401 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001402
1403 if (!iwl_mvm_is_dqa_supported(mvm))
1404 continue;
1405
1406 /*
1407 * Mark all queues for this STA as unallocated and defer TX
1408 * frames until the queue is allocated
1409 */
Sara Sharon6862fce2017-02-22 19:34:17 +02001410 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001411 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001412 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001413 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001414 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001415
Sara Sharona571f5f2015-12-07 12:50:58 +02001416 if (iwl_mvm_has_new_rx_api(mvm) &&
1417 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Johannes Berg92c4dca2017-06-07 10:35:54 +02001418 int q;
1419
Sara Sharona571f5f2015-12-07 12:50:58 +02001420 dup_data = kcalloc(mvm->trans->num_rx_queues,
Johannes Berg92c4dca2017-06-07 10:35:54 +02001421 sizeof(*dup_data), GFP_KERNEL);
Sara Sharona571f5f2015-12-07 12:50:58 +02001422 if (!dup_data)
1423 return -ENOMEM;
Johannes Berg92c4dca2017-06-07 10:35:54 +02001424 /*
1425 * Initialize all the last_seq values to 0xffff which can never
1426 * compare equal to the frame's seq_ctrl in the check in
1427 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1428 * number and fragmented packets don't reach that function.
1429 *
1430 * This thus allows receiving a packet with seqno 0 and the
1431 * retry bit set as the very first packet on a new TID.
1432 */
1433 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1434 memset(dup_data[q].last_seq, 0xff,
1435 sizeof(dup_data[q].last_seq));
Sara Sharona571f5f2015-12-07 12:50:58 +02001436 mvm_sta->dup_data = dup_data;
1437 }
1438
Sara Sharon396952e2017-02-22 19:40:55 +02001439 if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001440 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1441 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001442 if (ret)
1443 goto err;
1444 }
1445
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001446update_fw:
Liad Kaufman24afba72015-07-28 18:56:08 +03001447 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001448 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001449 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001450
Johannes Berg9e848012014-08-04 14:33:42 +02001451 if (vif->type == NL80211_IFTYPE_STATION) {
1452 if (!sta->tdls) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001453 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001454 mvmvif->ap_sta_id = sta_id;
1455 } else {
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 }
1458 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001459
1460 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1461
1462 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001463
1464err:
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001465 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1466 iwl_mvm_tdls_sta_deinit(mvm, sta);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001467 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001468}
1469
1470int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1471 bool drain)
1472{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001473 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001474 int ret;
1475 u32 status;
1476
1477 lockdep_assert_held(&mvm->mutex);
1478
1479 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1480 cmd.sta_id = mvmsta->sta_id;
1481 cmd.add_modify = STA_MODE_MODIFY;
1482 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1483 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1484
1485 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001486 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1487 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001488 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001489 if (ret)
1490 return ret;
1491
Sara Sharon837c4da2016-01-07 16:50:45 +02001492 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001493 case ADD_STA_SUCCESS:
1494 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1495 mvmsta->sta_id);
1496 break;
1497 default:
1498 ret = -EIO;
1499 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1500 mvmsta->sta_id);
1501 break;
1502 }
1503
1504 return ret;
1505}
1506
1507/*
1508 * Remove a station from the FW table. Before sending the command to remove
1509 * the station validate that the station is indeed known to the driver (sanity
1510 * only).
1511 */
1512static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1513{
1514 struct ieee80211_sta *sta;
1515 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1516 .sta_id = sta_id,
1517 };
1518 int ret;
1519
1520 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1521 lockdep_is_held(&mvm->mutex));
1522
1523 /* Note: internal stations are marked as error values */
1524 if (!sta) {
1525 IWL_ERR(mvm, "Invalid station id\n");
1526 return -EINVAL;
1527 }
1528
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001529 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001530 sizeof(rm_sta_cmd), &rm_sta_cmd);
1531 if (ret) {
1532 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1533 return ret;
1534 }
1535
1536 return 0;
1537}
1538
1539void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1540{
1541 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1542 u8 sta_id;
1543
1544 /*
1545 * The mutex is needed because of the SYNC cmd, but not only: if the
1546 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1547 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1548 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1549 * that later.
1550 */
1551 mutex_lock(&mvm->mutex);
1552
1553 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1554 int ret;
1555 struct ieee80211_sta *sta =
1556 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1557 lockdep_is_held(&mvm->mutex));
1558
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001559 /*
1560 * This station is in use or RCU-removed; the latter happens in
1561 * managed mode, where mac80211 removes the station before we
1562 * can remove it from firmware (we can only do that after the
1563 * MAC is marked unassociated), and possibly while the deauth
1564 * frame to disconnect from the AP is still queued. Then, the
1565 * station pointer is -ENOENT when the last skb is reclaimed.
1566 */
1567 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001568 continue;
1569
1570 if (PTR_ERR(sta) == -EINVAL) {
1571 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1572 sta_id);
1573 continue;
1574 }
1575
1576 if (!sta) {
1577 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1578 sta_id);
1579 continue;
1580 }
1581
1582 WARN_ON(PTR_ERR(sta) != -EBUSY);
1583 /* This station was removed and we waited until it got drained,
1584 * we can now proceed and remove it.
1585 */
1586 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1587 if (ret) {
1588 IWL_ERR(mvm,
1589 "Couldn't remove sta %d after it was drained\n",
1590 sta_id);
1591 continue;
1592 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301593 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001594 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001595
1596 if (mvm->tfd_drained[sta_id]) {
1597 unsigned long i, msk = mvm->tfd_drained[sta_id];
1598
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001599 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001600 iwl_mvm_disable_txq(mvm, i, i,
1601 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001602
1603 mvm->tfd_drained[sta_id] = 0;
1604 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1605 sta_id, msk);
1606 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001607 }
1608
1609 mutex_unlock(&mvm->mutex);
1610}
1611
Liad Kaufman24afba72015-07-28 18:56:08 +03001612static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1613 struct ieee80211_vif *vif,
1614 struct iwl_mvm_sta *mvm_sta)
1615{
1616 int ac;
1617 int i;
1618
1619 lockdep_assert_held(&mvm->mutex);
1620
1621 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
Sara Sharon6862fce2017-02-22 19:34:17 +02001622 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman24afba72015-07-28 18:56:08 +03001623 continue;
1624
1625 ac = iwl_mvm_tid_to_ac_queue(i);
1626 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1627 vif->hw_queue[ac], i, 0);
Sara Sharon6862fce2017-02-22 19:34:17 +02001628 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001629 }
1630}
1631
Sara Sharond6d517b2017-03-06 10:16:11 +02001632int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1633 struct iwl_mvm_sta *mvm_sta)
1634{
1635 int i, ret;
1636
1637 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1638 u16 txq_id;
1639
1640 spin_lock_bh(&mvm_sta->lock);
1641 txq_id = mvm_sta->tid_data[i].txq_id;
1642 spin_unlock_bh(&mvm_sta->lock);
1643
1644 if (txq_id == IWL_MVM_INVALID_QUEUE)
1645 continue;
1646
1647 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1648 if (ret)
1649 break;
1650 }
1651
1652 return ret;
1653}
1654
Johannes Berg8ca151b2013-01-24 14:25:36 +01001655int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1656 struct ieee80211_vif *vif,
1657 struct ieee80211_sta *sta)
1658{
1659 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001660 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001661 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001662 int ret;
1663
1664 lockdep_assert_held(&mvm->mutex);
1665
Sara Sharona571f5f2015-12-07 12:50:58 +02001666 if (iwl_mvm_has_new_rx_api(mvm))
1667 kfree(mvm_sta->dup_data);
1668
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001669 if ((vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001670 mvmvif->ap_sta_id == sta_id) ||
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001671 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001672 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1673 if (ret)
1674 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001675 /* flush its queues here since we are freeing mvm_sta */
Sara Sharond49394a2017-03-05 13:01:08 +02001676 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001677 if (ret)
1678 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001679 if (iwl_mvm_has_new_tx_api(mvm)) {
1680 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1681 } else {
1682 u32 q_mask = mvm_sta->tfd_queue_msk;
1683
1684 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1685 q_mask);
1686 }
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001687 if (ret)
1688 return ret;
1689 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001690
Liad Kaufman24afba72015-07-28 18:56:08 +03001691 /* If DQA is supported - the queues can be disabled now */
Sara Sharon94c3e612016-12-07 15:04:37 +02001692 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman56214742016-09-22 15:14:08 +03001693 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001694 /*
1695 * If pending_frames is set at this point - it must be
1696 * driver internal logic error, since queues are empty
1697 * and removed successuly.
1698 * warn on it but set it to 0 anyway to avoid station
1699 * not being removed later in the function
1700 */
1701 WARN_ON(atomic_xchg(&mvm->pending_frames[sta_id], 0));
1702 }
Liad Kaufman56214742016-09-22 15:14:08 +03001703
1704 /* If there is a TXQ still marked as reserved - free it */
1705 if (iwl_mvm_is_dqa_supported(mvm) &&
1706 mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001707 u8 reserved_txq = mvm_sta->reserved_queue;
1708 enum iwl_mvm_queue_status *status;
1709
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001710 /*
1711 * If no traffic has gone through the reserved TXQ - it
1712 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1713 * should be manually marked as free again
1714 */
1715 spin_lock_bh(&mvm->queue_info_lock);
1716 status = &mvm->queue_info[reserved_txq].status;
1717 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1718 (*status != IWL_MVM_QUEUE_FREE),
1719 "sta_id %d reserved txq %d status %d",
Sara Sharon94c3e612016-12-07 15:04:37 +02001720 sta_id, reserved_txq, *status)) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001721 spin_unlock_bh(&mvm->queue_info_lock);
1722 return -EINVAL;
1723 }
1724
1725 *status = IWL_MVM_QUEUE_FREE;
1726 spin_unlock_bh(&mvm->queue_info_lock);
1727 }
1728
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001729 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001730 mvmvif->ap_sta_id == sta_id) {
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001731 /* if associated - we can't remove the AP STA now */
1732 if (vif->bss_conf.assoc)
1733 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001734
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001735 /* unassoc - go ahead - remove the AP STA now */
Sara Sharon0ae98812017-01-04 14:53:58 +02001736 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Eliad Peller37577fe2013-12-05 17:19:39 +02001737
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001738 /* clear d0i3_ap_sta_id if no longer relevant */
Sara Sharon94c3e612016-12-07 15:04:37 +02001739 if (mvm->d0i3_ap_sta_id == sta_id)
Sara Sharon0ae98812017-01-04 14:53:58 +02001740 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001741 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001742 }
1743
1744 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001745 * This shouldn't happen - the TDLS channel switch should be canceled
1746 * before the STA is removed.
1747 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001748 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001749 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001750 cancel_delayed_work(&mvm->tdls_cs.dwork);
1751 }
1752
1753 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001754 * Make sure that the tx response code sees the station as -EBUSY and
1755 * calls the drain worker.
1756 */
1757 spin_lock_bh(&mvm_sta->lock);
Sara Sharon94c3e612016-12-07 15:04:37 +02001758
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001759 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001760 * There are frames pending on the AC queues for this station.
1761 * We need to wait until all the frames are drained...
1762 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001763 if (atomic_read(&mvm->pending_frames[sta_id])) {
1764 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id],
Johannes Berg8ca151b2013-01-24 14:25:36 +01001765 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001766 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001767
1768 /* disable TDLS sta queues on drain complete */
1769 if (sta->tdls) {
Sara Sharon94c3e612016-12-07 15:04:37 +02001770 mvm->tfd_drained[sta_id] = mvm_sta->tfd_queue_msk;
1771 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n", sta_id);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001772 }
1773
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001774 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001775 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001776 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001777
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001778 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001779 iwl_mvm_tdls_sta_deinit(mvm, sta);
1780
Johannes Berg8ca151b2013-01-24 14:25:36 +01001781 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301782 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001783 }
1784
1785 return ret;
1786}
1787
1788int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1789 struct ieee80211_vif *vif,
1790 u8 sta_id)
1791{
1792 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1793
1794 lockdep_assert_held(&mvm->mutex);
1795
Monam Agarwalc531c772014-03-24 00:05:56 +05301796 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001797 return ret;
1798}
1799
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001800int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1801 struct iwl_mvm_int_sta *sta,
Sara Sharonced19f22017-02-06 19:09:32 +02001802 u32 qmask, enum nl80211_iftype iftype,
1803 enum iwl_sta_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001804{
1805 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001806 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Sara Sharon0ae98812017-01-04 14:53:58 +02001807 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001808 return -ENOSPC;
1809 }
1810
1811 sta->tfd_queue_msk = qmask;
Sara Sharonced19f22017-02-06 19:09:32 +02001812 sta->type = type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001813
1814 /* put a non-NULL value so iterating over the stations won't stop */
1815 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1816 return 0;
1817}
1818
Sara Sharon26d6c162017-01-03 12:00:19 +02001819void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001820{
Monam Agarwalc531c772014-03-24 00:05:56 +05301821 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001822 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
Sara Sharon0ae98812017-01-04 14:53:58 +02001823 sta->sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001824}
1825
1826static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1827 struct iwl_mvm_int_sta *sta,
1828 const u8 *addr,
1829 u16 mac_id, u16 color)
1830{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001831 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001832 int ret;
1833 u32 status;
1834
1835 lockdep_assert_held(&mvm->mutex);
1836
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001837 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001838 cmd.sta_id = sta->sta_id;
1839 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1840 color));
Sara Sharonced19f22017-02-06 19:09:32 +02001841 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1842 cmd.station_type = sta->type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001843
Sara Sharonbb497012016-09-29 14:52:40 +03001844 if (!iwl_mvm_has_new_tx_api(mvm))
1845 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001846 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001847
1848 if (addr)
1849 memcpy(cmd.addr, addr, ETH_ALEN);
1850
Sara Sharon854c5702016-01-26 13:17:47 +02001851 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1852 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001853 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001854 if (ret)
1855 return ret;
1856
Sara Sharon837c4da2016-01-07 16:50:45 +02001857 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001858 case ADD_STA_SUCCESS:
1859 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1860 return 0;
1861 default:
1862 ret = -EIO;
1863 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1864 status);
1865 break;
1866 }
1867 return ret;
1868}
1869
Sara Sharonc5a719e2016-11-15 10:20:48 +02001870static void iwl_mvm_enable_aux_queue(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001871{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001872 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1873 mvm->cfg->base_params->wd_timeout :
1874 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001875
Sara Sharon310181e2017-01-17 14:27:48 +02001876 if (iwl_mvm_has_new_tx_api(mvm)) {
1877 int queue = iwl_mvm_tvqm_enable_txq(mvm, mvm->aux_queue,
1878 mvm->aux_sta.sta_id,
1879 IWL_MAX_TID_COUNT,
1880 wdg_timeout);
1881 mvm->aux_queue = queue;
1882 } else if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman28d07932015-09-01 16:36:25 +03001883 struct iwl_trans_txq_scd_cfg cfg = {
1884 .fifo = IWL_MVM_TX_FIFO_MCAST,
1885 .sta_id = mvm->aux_sta.sta_id,
1886 .tid = IWL_MAX_TID_COUNT,
1887 .aggregate = false,
1888 .frame_limit = IWL_FRAME_LIMIT,
1889 };
1890
1891 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1892 wdg_timeout);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001893 } else {
1894 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1895 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Liad Kaufman28d07932015-09-01 16:36:25 +03001896 }
Sara Sharonc5a719e2016-11-15 10:20:48 +02001897}
1898
1899int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1900{
1901 int ret;
1902
1903 lockdep_assert_held(&mvm->mutex);
1904
1905 /* Allocate aux station and assign to it the aux queue */
1906 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Sara Sharonced19f22017-02-06 19:09:32 +02001907 NL80211_IFTYPE_UNSPECIFIED,
1908 IWL_STA_AUX_ACTIVITY);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001909 if (ret)
1910 return ret;
1911
1912 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1913 if (!iwl_mvm_has_new_tx_api(mvm))
1914 iwl_mvm_enable_aux_queue(mvm);
Liad Kaufman28d07932015-09-01 16:36:25 +03001915
Johannes Berg8ca151b2013-01-24 14:25:36 +01001916 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1917 MAC_INDEX_AUX, 0);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001918 if (ret) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001919 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001920 return ret;
1921 }
1922
1923 /*
1924 * For a000 firmware and on we cannot add queue to a station unknown
1925 * to firmware so enable queue here - after the station was added
1926 */
1927 if (iwl_mvm_has_new_tx_api(mvm))
1928 iwl_mvm_enable_aux_queue(mvm);
1929
1930 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001931}
1932
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001933int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1934{
1935 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1936
1937 lockdep_assert_held(&mvm->mutex);
1938 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1939 mvmvif->id, 0);
1940}
1941
1942int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1943{
1944 int ret;
1945
1946 lockdep_assert_held(&mvm->mutex);
1947
1948 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1949 if (ret)
1950 IWL_WARN(mvm, "Failed sending remove station\n");
1951
1952 return ret;
1953}
1954
1955void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1956{
1957 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1958}
1959
Johannes Berg712b24a2014-08-04 14:14:14 +02001960void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1961{
1962 lockdep_assert_held(&mvm->mutex);
1963
1964 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1965}
1966
Johannes Berg8ca151b2013-01-24 14:25:36 +01001967/*
1968 * Send the add station command for the vif's broadcast station.
1969 * Assumes that the station was already allocated.
1970 *
1971 * @mvm: the mvm component
1972 * @vif: the interface to which the broadcast station is added
1973 * @bsta: the broadcast station to add.
1974 */
Johannes Berg013290a2014-08-04 13:38:48 +02001975int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001976{
1977 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001978 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001979 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001980 const u8 *baddr = _baddr;
Johannes Berg7daa7622017-02-24 12:02:22 +01001981 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001982 int ret;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001983 unsigned int wdg_timeout =
1984 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1985 struct iwl_trans_txq_scd_cfg cfg = {
1986 .fifo = IWL_MVM_TX_FIFO_VO,
1987 .sta_id = mvmvif->bcast_sta.sta_id,
1988 .tid = IWL_MAX_TID_COUNT,
1989 .aggregate = false,
1990 .frame_limit = IWL_FRAME_LIMIT,
1991 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001992
1993 lockdep_assert_held(&mvm->mutex);
1994
Sara Sharon310181e2017-01-17 14:27:48 +02001995 if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufman4d339982017-03-21 17:13:16 +02001996 if (vif->type == NL80211_IFTYPE_AP ||
1997 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon49f71712017-01-09 12:07:16 +02001998 queue = mvm->probe_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001999 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
Sara Sharon49f71712017-01-09 12:07:16 +02002000 queue = mvm->p2p_dev_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02002001 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
Liad Kaufmande24f632015-08-04 15:19:18 +03002002 return -EINVAL;
2003
Liad Kaufmandf88c082016-11-24 15:31:00 +02002004 bsta->tfd_queue_msk |= BIT(queue);
Sara Sharonc5a719e2016-11-15 10:20:48 +02002005
Sara Sharon310181e2017-01-17 14:27:48 +02002006 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
2007 &cfg, wdg_timeout);
Liad Kaufmande24f632015-08-04 15:19:18 +03002008 }
2009
Johannes Berg5023d962013-07-31 14:07:43 +02002010 if (vif->type == NL80211_IFTYPE_ADHOC)
2011 baddr = vif->bss_conf.bssid;
2012
Sara Sharon0ae98812017-01-04 14:53:58 +02002013 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01002014 return -ENOSPC;
2015
Liad Kaufmandf88c082016-11-24 15:31:00 +02002016 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2017 mvmvif->id, mvmvif->color);
2018 if (ret)
2019 return ret;
2020
2021 /*
Sara Sharonc5a719e2016-11-15 10:20:48 +02002022 * For a000 firmware and on we cannot add queue to a station unknown
2023 * to firmware so enable queue here - after the station was added
Liad Kaufmandf88c082016-11-24 15:31:00 +02002024 */
Sara Sharon310181e2017-01-17 14:27:48 +02002025 if (iwl_mvm_has_new_tx_api(mvm)) {
Johannes Berg7daa7622017-02-24 12:02:22 +01002026 queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
2027 bsta->sta_id,
2028 IWL_MAX_TID_COUNT,
2029 wdg_timeout);
2030
Luca Coelho7b758a12017-06-20 13:40:03 +03002031 if (vif->type == NL80211_IFTYPE_AP ||
2032 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon310181e2017-01-17 14:27:48 +02002033 mvm->probe_queue = queue;
2034 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2035 mvm->p2p_dev_queue = queue;
Sara Sharon310181e2017-01-17 14:27:48 +02002036 }
Liad Kaufmandf88c082016-11-24 15:31:00 +02002037
2038 return 0;
2039}
2040
2041static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2042 struct ieee80211_vif *vif)
2043{
2044 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002045 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02002046
2047 lockdep_assert_held(&mvm->mutex);
2048
Sara Sharond49394a2017-03-05 13:01:08 +02002049 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2050
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002051 switch (vif->type) {
2052 case NL80211_IFTYPE_AP:
2053 case NL80211_IFTYPE_ADHOC:
2054 queue = mvm->probe_queue;
2055 break;
2056 case NL80211_IFTYPE_P2P_DEVICE:
2057 queue = mvm->p2p_dev_queue;
2058 break;
2059 default:
2060 WARN(1, "Can't free bcast queue on vif type %d\n",
2061 vif->type);
2062 return;
Liad Kaufmandf88c082016-11-24 15:31:00 +02002063 }
2064
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002065 iwl_mvm_disable_txq(mvm, queue, vif->hw_queue[0], IWL_MAX_TID_COUNT, 0);
2066 if (iwl_mvm_has_new_tx_api(mvm))
2067 return;
2068
2069 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2070 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002071}
2072
2073/* Send the FW a request to remove the station from it's internal data
2074 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02002075int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002076{
Johannes Berg013290a2014-08-04 13:38:48 +02002077 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002078 int ret;
2079
2080 lockdep_assert_held(&mvm->mutex);
2081
Liad Kaufmandf88c082016-11-24 15:31:00 +02002082 if (iwl_mvm_is_dqa_supported(mvm))
2083 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2084
Johannes Berg013290a2014-08-04 13:38:48 +02002085 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002086 if (ret)
2087 IWL_WARN(mvm, "Failed sending remove station\n");
2088 return ret;
2089}
2090
Johannes Berg013290a2014-08-04 13:38:48 +02002091int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2092{
2093 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03002094 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02002095
2096 lockdep_assert_held(&mvm->mutex);
2097
Liad Kaufmandf88c082016-11-24 15:31:00 +02002098 if (!iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmande24f632015-08-04 15:19:18 +03002099 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02002100
Liad Kaufmande24f632015-08-04 15:19:18 +03002101 /*
2102 * The firmware defines the TFD queue mask to only be relevant
2103 * for *unicast* queues, so the multicast (CAB) queue shouldn't
Liad Kaufmandf88c082016-11-24 15:31:00 +02002104 * be included. This only happens in NL80211_IFTYPE_AP vif type,
2105 * so the next line will only have an effect there.
Liad Kaufmande24f632015-08-04 15:19:18 +03002106 */
Johannes Berg013290a2014-08-04 13:38:48 +02002107 qmask &= ~BIT(vif->cab_queue);
Liad Kaufmande24f632015-08-04 15:19:18 +03002108 }
2109
Johannes Berg013290a2014-08-04 13:38:48 +02002110 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
Sara Sharonced19f22017-02-06 19:09:32 +02002111 ieee80211_vif_type_p2p(vif),
2112 IWL_STA_GENERAL_PURPOSE);
Johannes Berg013290a2014-08-04 13:38:48 +02002113}
2114
Johannes Berg8ca151b2013-01-24 14:25:36 +01002115/* Allocate a new station entry for the broadcast station to the given vif,
2116 * and send it to the FW.
2117 * Note that each P2P mac should have its own broadcast station.
2118 *
2119 * @mvm: the mvm component
2120 * @vif: the interface to which the broadcast station is added
2121 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02002122int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002123{
2124 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02002125 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002126 int ret;
2127
2128 lockdep_assert_held(&mvm->mutex);
2129
Johannes Berg013290a2014-08-04 13:38:48 +02002130 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002131 if (ret)
2132 return ret;
2133
Johannes Berg013290a2014-08-04 13:38:48 +02002134 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002135
2136 if (ret)
2137 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02002138
Johannes Berg8ca151b2013-01-24 14:25:36 +01002139 return ret;
2140}
2141
Johannes Berg013290a2014-08-04 13:38:48 +02002142void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2143{
2144 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2145
2146 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2147}
2148
Johannes Berg8ca151b2013-01-24 14:25:36 +01002149/*
2150 * Send the FW a request to remove the station from it's internal data
2151 * structures, and in addition remove it from the local data structure.
2152 */
Johannes Berg013290a2014-08-04 13:38:48 +02002153int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002154{
2155 int ret;
2156
2157 lockdep_assert_held(&mvm->mutex);
2158
Johannes Berg013290a2014-08-04 13:38:48 +02002159 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002160
Johannes Berg013290a2014-08-04 13:38:48 +02002161 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2162
Johannes Berg8ca151b2013-01-24 14:25:36 +01002163 return ret;
2164}
2165
Sara Sharon26d6c162017-01-03 12:00:19 +02002166/*
2167 * Allocate a new station entry for the multicast station to the given vif,
2168 * and send it to the FW.
2169 * Note that each AP/GO mac should have its own multicast station.
2170 *
2171 * @mvm: the mvm component
2172 * @vif: the interface to which the multicast station is added
2173 */
2174int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2175{
2176 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2177 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2178 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2179 const u8 *maddr = _maddr;
2180 struct iwl_trans_txq_scd_cfg cfg = {
2181 .fifo = IWL_MVM_TX_FIFO_MCAST,
2182 .sta_id = msta->sta_id,
2183 .tid = IWL_MAX_TID_COUNT,
2184 .aggregate = false,
2185 .frame_limit = IWL_FRAME_LIMIT,
2186 };
2187 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2188 int ret;
2189
2190 lockdep_assert_held(&mvm->mutex);
2191
2192 if (!iwl_mvm_is_dqa_supported(mvm))
2193 return 0;
2194
Liad Kaufmanee48b722017-03-21 17:13:16 +02002195 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2196 vif->type != NL80211_IFTYPE_ADHOC))
Sara Sharon26d6c162017-01-03 12:00:19 +02002197 return -ENOTSUPP;
2198
Sara Sharonced19f22017-02-06 19:09:32 +02002199 /*
2200 * While in previous FWs we had to exclude cab queue from TFD queue
2201 * mask, now it is needed as any other queue.
2202 */
2203 if (!iwl_mvm_has_new_tx_api(mvm) &&
2204 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2205 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2206 &cfg, timeout);
2207 msta->tfd_queue_msk |= BIT(vif->cab_queue);
2208 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002209 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2210 mvmvif->id, mvmvif->color);
2211 if (ret) {
2212 iwl_mvm_dealloc_int_sta(mvm, msta);
2213 return ret;
2214 }
2215
2216 /*
2217 * Enable cab queue after the ADD_STA command is sent.
2218 * This is needed for a000 firmware which won't accept SCD_QUEUE_CFG
Sara Sharonced19f22017-02-06 19:09:32 +02002219 * command with unknown station id, and for FW that doesn't support
2220 * station API since the cab queue is not included in the
2221 * tfd_queue_mask.
Sara Sharon26d6c162017-01-03 12:00:19 +02002222 */
Sara Sharon310181e2017-01-17 14:27:48 +02002223 if (iwl_mvm_has_new_tx_api(mvm)) {
2224 int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2225 msta->sta_id,
2226 IWL_MAX_TID_COUNT,
2227 timeout);
Sara Sharone2af3fa2017-02-22 19:35:10 +02002228 mvmvif->cab_queue = queue;
Sara Sharonced19f22017-02-06 19:09:32 +02002229 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2230 IWL_UCODE_TLV_API_STA_TYPE)) {
Liad Kaufmanee48b722017-03-21 17:13:16 +02002231 /*
2232 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2233 * invalid, so make sure we use the queue we want.
2234 * Note that this is done here as we want to avoid making DQA
2235 * changes in mac80211 layer.
2236 */
2237 if (vif->type == NL80211_IFTYPE_ADHOC) {
2238 vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2239 mvmvif->cab_queue = vif->cab_queue;
2240 }
Sara Sharon310181e2017-01-17 14:27:48 +02002241 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2242 &cfg, timeout);
2243 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002244
2245 return 0;
2246}
2247
2248/*
2249 * Send the FW a request to remove the station from it's internal data
2250 * structures, and in addition remove it from the local data structure.
2251 */
2252int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2253{
2254 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2255 int ret;
2256
2257 lockdep_assert_held(&mvm->mutex);
2258
2259 if (!iwl_mvm_is_dqa_supported(mvm))
2260 return 0;
2261
Sara Sharond49394a2017-03-05 13:01:08 +02002262 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2263
Sara Sharone2af3fa2017-02-22 19:35:10 +02002264 iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
Sara Sharon26d6c162017-01-03 12:00:19 +02002265 IWL_MAX_TID_COUNT, 0);
2266
2267 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2268 if (ret)
2269 IWL_WARN(mvm, "Failed sending remove station\n");
2270
2271 return ret;
2272}
2273
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002274#define IWL_MAX_RX_BA_SESSIONS 16
2275
Sara Sharonb915c102016-03-23 16:32:02 +02002276static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02002277{
Sara Sharonb915c102016-03-23 16:32:02 +02002278 struct iwl_mvm_delba_notif notif = {
2279 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2280 .metadata.sync = 1,
2281 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02002282 };
Sara Sharonb915c102016-03-23 16:32:02 +02002283 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2284};
Sara Sharon10b2b202016-03-20 16:23:41 +02002285
Sara Sharonb915c102016-03-23 16:32:02 +02002286static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2287 struct iwl_mvm_baid_data *data)
2288{
2289 int i;
2290
2291 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2292
2293 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2294 int j;
2295 struct iwl_mvm_reorder_buffer *reorder_buf =
2296 &data->reorder_buf[i];
2297
Sara Sharon06904052016-02-28 20:28:17 +02002298 spin_lock_bh(&reorder_buf->lock);
2299 if (likely(!reorder_buf->num_stored)) {
2300 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02002301 continue;
Sara Sharon06904052016-02-28 20:28:17 +02002302 }
Sara Sharonb915c102016-03-23 16:32:02 +02002303
2304 /*
2305 * This shouldn't happen in regular DELBA since the internal
2306 * delBA notification should trigger a release of all frames in
2307 * the reorder buffer.
2308 */
2309 WARN_ON(1);
2310
2311 for (j = 0; j < reorder_buf->buf_size; j++)
2312 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02002313 /*
2314 * Prevent timer re-arm. This prevents a very far fetched case
2315 * where we timed out on the notification. There may be prior
2316 * RX frames pending in the RX queue before the notification
2317 * that might get processed between now and the actual deletion
2318 * and we would re-arm the timer although we are deleting the
2319 * reorder buffer.
2320 */
2321 reorder_buf->removed = true;
2322 spin_unlock_bh(&reorder_buf->lock);
2323 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02002324 }
2325}
2326
2327static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2328 u32 sta_id,
2329 struct iwl_mvm_baid_data *data,
2330 u16 ssn, u8 buf_size)
2331{
2332 int i;
2333
2334 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2335 struct iwl_mvm_reorder_buffer *reorder_buf =
2336 &data->reorder_buf[i];
2337 int j;
2338
2339 reorder_buf->num_stored = 0;
2340 reorder_buf->head_sn = ssn;
2341 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02002342 /* rx reorder timer */
2343 reorder_buf->reorder_timer.function =
2344 iwl_mvm_reorder_timer_expired;
2345 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
2346 init_timer(&reorder_buf->reorder_timer);
2347 spin_lock_init(&reorder_buf->lock);
2348 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02002349 reorder_buf->queue = i;
2350 reorder_buf->sta_id = sta_id;
Sara Sharon5d43eab2017-02-02 12:51:39 +02002351 reorder_buf->valid = false;
Sara Sharonb915c102016-03-23 16:32:02 +02002352 for (j = 0; j < reorder_buf->buf_size; j++)
2353 __skb_queue_head_init(&reorder_buf->entries[j]);
2354 }
Sara Sharon10b2b202016-03-20 16:23:41 +02002355}
2356
Johannes Berg8ca151b2013-01-24 14:25:36 +01002357int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02002358 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002359{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002360 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002361 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02002362 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002363 int ret;
2364 u32 status;
2365
2366 lockdep_assert_held(&mvm->mutex);
2367
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002368 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2369 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2370 return -ENOSPC;
2371 }
2372
Sara Sharon10b2b202016-03-20 16:23:41 +02002373 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2374 /*
2375 * Allocate here so if allocation fails we can bail out early
2376 * before starting the BA session in the firmware
2377 */
Sara Sharonb915c102016-03-23 16:32:02 +02002378 baid_data = kzalloc(sizeof(*baid_data) +
2379 mvm->trans->num_rx_queues *
2380 sizeof(baid_data->reorder_buf[0]),
2381 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02002382 if (!baid_data)
2383 return -ENOMEM;
2384 }
2385
Johannes Berg8ca151b2013-01-24 14:25:36 +01002386 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2387 cmd.sta_id = mvm_sta->sta_id;
2388 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002389 if (start) {
2390 cmd.add_immediate_ba_tid = (u8) tid;
2391 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02002392 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002393 } else {
2394 cmd.remove_immediate_ba_tid = (u8) tid;
2395 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002396 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2397 STA_MODIFY_REMOVE_BA_TID;
2398
2399 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002400 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2401 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002402 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002403 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02002404 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002405
Sara Sharon837c4da2016-01-07 16:50:45 +02002406 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002407 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03002408 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2409 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002410 break;
2411 case ADD_STA_IMMEDIATE_BA_FAILURE:
2412 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2413 ret = -ENOSPC;
2414 break;
2415 default:
2416 ret = -EIO;
2417 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2418 start ? "start" : "stopp", status);
2419 break;
2420 }
2421
Sara Sharon10b2b202016-03-20 16:23:41 +02002422 if (ret)
2423 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002424
Sara Sharon10b2b202016-03-20 16:23:41 +02002425 if (start) {
2426 u8 baid;
2427
2428 mvm->rx_ba_sessions++;
2429
2430 if (!iwl_mvm_has_new_rx_api(mvm))
2431 return 0;
2432
2433 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2434 ret = -EINVAL;
2435 goto out_free;
2436 }
2437 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2438 IWL_ADD_STA_BAID_SHIFT);
2439 baid_data->baid = baid;
2440 baid_data->timeout = timeout;
2441 baid_data->last_rx = jiffies;
Wei Yongjun72c240f2016-07-12 11:40:57 +00002442 setup_timer(&baid_data->session_timer,
2443 iwl_mvm_rx_agg_session_expired,
2444 (unsigned long)&mvm->baid_map[baid]);
Sara Sharon10b2b202016-03-20 16:23:41 +02002445 baid_data->mvm = mvm;
2446 baid_data->tid = tid;
2447 baid_data->sta_id = mvm_sta->sta_id;
2448
2449 mvm_sta->tid_to_baid[tid] = baid;
2450 if (timeout)
2451 mod_timer(&baid_data->session_timer,
2452 TU_TO_EXP_TIME(timeout * 2));
2453
Sara Sharonb915c102016-03-23 16:32:02 +02002454 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2455 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002456 /*
2457 * protect the BA data with RCU to cover a case where our
2458 * internal RX sync mechanism will timeout (not that it's
2459 * supposed to happen) and we will free the session data while
2460 * RX is being processed in parallel
2461 */
Sara Sharon35263a02016-06-21 12:12:10 +03002462 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2463 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002464 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2465 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002466 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002467 u8 baid = mvm_sta->tid_to_baid[tid];
2468
Sara Sharon60dec522016-06-21 14:14:08 +03002469 if (mvm->rx_ba_sessions > 0)
2470 /* check that restart flow didn't zero the counter */
2471 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002472 if (!iwl_mvm_has_new_rx_api(mvm))
2473 return 0;
2474
2475 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2476 return -EINVAL;
2477
2478 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2479 if (WARN_ON(!baid_data))
2480 return -EINVAL;
2481
2482 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002483 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002484 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002485 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2486 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002487 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002488 }
2489 return 0;
2490
2491out_free:
2492 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002493 return ret;
2494}
2495
Liad Kaufman9794c642015-08-19 17:34:28 +03002496int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2497 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002498{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002499 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002500 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002501 int ret;
2502 u32 status;
2503
2504 lockdep_assert_held(&mvm->mutex);
2505
2506 if (start) {
2507 mvm_sta->tfd_queue_msk |= BIT(queue);
2508 mvm_sta->tid_disable_agg &= ~BIT(tid);
2509 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002510 /* In DQA-mode the queue isn't removed on agg termination */
2511 if (!iwl_mvm_is_dqa_supported(mvm))
2512 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002513 mvm_sta->tid_disable_agg |= BIT(tid);
2514 }
2515
2516 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2517 cmd.sta_id = mvm_sta->sta_id;
2518 cmd.add_modify = STA_MODE_MODIFY;
Sara Sharonbb497012016-09-29 14:52:40 +03002519 if (!iwl_mvm_has_new_tx_api(mvm))
2520 cmd.modify_mask = STA_MODIFY_QUEUES;
2521 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002522 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2523 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2524
2525 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002526 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2527 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002528 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002529 if (ret)
2530 return ret;
2531
Sara Sharon837c4da2016-01-07 16:50:45 +02002532 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002533 case ADD_STA_SUCCESS:
2534 break;
2535 default:
2536 ret = -EIO;
2537 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2538 start ? "start" : "stopp", status);
2539 break;
2540 }
2541
2542 return ret;
2543}
2544
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002545const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002546 IEEE80211_AC_BE,
2547 IEEE80211_AC_BK,
2548 IEEE80211_AC_BK,
2549 IEEE80211_AC_BE,
2550 IEEE80211_AC_VI,
2551 IEEE80211_AC_VI,
2552 IEEE80211_AC_VO,
2553 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002554 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002555};
2556
Johannes Berg3e56ead2013-02-15 22:23:18 +01002557static const u8 tid_to_ucode_ac[] = {
2558 AC_BE,
2559 AC_BK,
2560 AC_BK,
2561 AC_BE,
2562 AC_VI,
2563 AC_VI,
2564 AC_VO,
2565 AC_VO,
2566};
2567
Johannes Berg8ca151b2013-01-24 14:25:36 +01002568int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2569 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2570{
Johannes Berg5b577a92013-11-14 18:20:04 +01002571 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002572 struct iwl_mvm_tid_data *tid_data;
Liad Kaufmandd321622017-04-05 16:25:11 +03002573 u16 normalized_ssn;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002574 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002575 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002576
2577 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2578 return -EINVAL;
2579
2580 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2581 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2582 mvmsta->tid_data[tid].state);
2583 return -ENXIO;
2584 }
2585
2586 lockdep_assert_held(&mvm->mutex);
2587
Arik Nemtsovb2492502014-03-13 12:21:50 +02002588 spin_lock_bh(&mvmsta->lock);
2589
2590 /* possible race condition - we entered D0i3 while starting agg */
2591 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2592 spin_unlock_bh(&mvmsta->lock);
2593 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2594 return -EIO;
2595 }
2596
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002597 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002598
Liad Kaufmancf961e12015-08-13 19:16:08 +03002599 /*
2600 * Note the possible cases:
2601 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2602 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2603 * one and mark it as reserved
2604 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2605 * non-DQA mode, since the TXQ hasn't yet been allocated
Sara Sharon34e10862017-02-23 13:15:07 +02002606 * Don't support case 3 for new TX path as it is not expected to happen
2607 * and aggregation will be offloaded soon anyway
Liad Kaufmancf961e12015-08-13 19:16:08 +03002608 */
2609 txq_id = mvmsta->tid_data[tid].txq_id;
Sara Sharon34e10862017-02-23 13:15:07 +02002610 if (iwl_mvm_has_new_tx_api(mvm)) {
2611 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2612 ret = -ENXIO;
2613 goto release_locks;
2614 }
2615 } else if (iwl_mvm_is_dqa_supported(mvm) &&
2616 unlikely(mvm->queue_info[txq_id].status ==
2617 IWL_MVM_QUEUE_SHARED)) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002618 ret = -ENXIO;
2619 IWL_DEBUG_TX_QUEUES(mvm,
2620 "Can't start tid %d agg on shared queue!\n",
2621 tid);
2622 goto release_locks;
2623 } else if (!iwl_mvm_is_dqa_supported(mvm) ||
Liad Kaufmancf961e12015-08-13 19:16:08 +03002624 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002625 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2626 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03002627 mvm->last_agg_queue);
2628 if (txq_id < 0) {
2629 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002630 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2631 goto release_locks;
2632 }
Sara Sharon01796ff2016-11-16 17:04:36 +02002633 /*
2634 * TXQ shouldn't be in inactive mode for non-DQA, so getting
2635 * an inactive queue from iwl_mvm_find_free_queue() is
2636 * certainly a bug
2637 */
2638 WARN_ON(mvm->queue_info[txq_id].status ==
2639 IWL_MVM_QUEUE_INACTIVE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002640
2641 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2642 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002643 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002644
2645 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002646
Liad Kaufmancf961e12015-08-13 19:16:08 +03002647 IWL_DEBUG_TX_QUEUES(mvm,
2648 "AGG for tid %d will be on queue #%d\n",
2649 tid, txq_id);
2650
Johannes Berg8ca151b2013-01-24 14:25:36 +01002651 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002652 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002653 tid_data->txq_id = txq_id;
2654 *ssn = tid_data->ssn;
2655
2656 IWL_DEBUG_TX_QUEUES(mvm,
2657 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2658 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2659 tid_data->next_reclaimed);
2660
Liad Kaufmandd321622017-04-05 16:25:11 +03002661 /*
2662 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
2663 * to align the wrap around of ssn so we compare relevant values.
2664 */
2665 normalized_ssn = tid_data->ssn;
2666 if (mvm->trans->cfg->gen2)
2667 normalized_ssn &= 0xff;
2668
2669 if (normalized_ssn == tid_data->next_reclaimed) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002670 tid_data->state = IWL_AGG_STARTING;
2671 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2672 } else {
2673 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2674 }
2675
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002676 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002677 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002678
2679release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002680 spin_unlock(&mvm->queue_info_lock);
2681out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002682 spin_unlock_bh(&mvmsta->lock);
2683
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002684 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002685}
2686
2687int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002688 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2689 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002690{
Johannes Berg5b577a92013-11-14 18:20:04 +01002691 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002692 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002693 unsigned int wdg_timeout =
2694 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002695 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002696 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002697 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002698 u16 ssn;
2699
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002700 struct iwl_trans_txq_scd_cfg cfg = {
2701 .sta_id = mvmsta->sta_id,
2702 .tid = tid,
2703 .frame_limit = buf_size,
2704 .aggregate = true,
2705 };
2706
Eyal Shapiraefed6642014-09-14 15:58:53 +03002707 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2708 != IWL_MAX_TID_COUNT);
2709
Liad Kaufmana58bb462017-05-28 14:20:04 +03002710 if (!mvm->trans->cfg->gen2)
2711 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2712 else
2713 buf_size = min_t(int, buf_size,
2714 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002715
2716 spin_lock_bh(&mvmsta->lock);
2717 ssn = tid_data->ssn;
2718 queue = tid_data->txq_id;
2719 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002720 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002721 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002722 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002723 spin_unlock_bh(&mvmsta->lock);
2724
Sara Sharon34e10862017-02-23 13:15:07 +02002725 if (iwl_mvm_has_new_tx_api(mvm)) {
2726 /*
2727 * If no queue iwl_mvm_sta_tx_agg_start() would have failed so
2728 * no need to check queue's status
2729 */
2730 if (buf_size < mvmsta->max_agg_bufsize)
2731 return -ENOTSUPP;
2732
2733 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2734 if (ret)
2735 return -EIO;
2736 goto out;
2737 }
2738
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002739 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002740
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002741 spin_lock_bh(&mvm->queue_info_lock);
2742 queue_status = mvm->queue_info[queue].status;
2743 spin_unlock_bh(&mvm->queue_info_lock);
2744
Liad Kaufmancf961e12015-08-13 19:16:08 +03002745 /* In DQA mode, the existing queue might need to be reconfigured */
2746 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002747 /* Maybe there is no need to even alloc a queue... */
2748 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2749 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002750
2751 /*
2752 * Only reconfig the SCD for the queue if the window size has
2753 * changed from current (become smaller)
2754 */
2755 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2756 /*
2757 * If reconfiguring an existing queue, it first must be
2758 * drained
2759 */
Sara Sharona1a57872017-03-05 11:38:58 +02002760 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2761 BIT(queue));
Liad Kaufmancf961e12015-08-13 19:16:08 +03002762 if (ret) {
2763 IWL_ERR(mvm,
2764 "Error draining queue before reconfig\n");
2765 return ret;
2766 }
2767
2768 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2769 mvmsta->sta_id, tid,
2770 buf_size, ssn);
2771 if (ret) {
2772 IWL_ERR(mvm,
2773 "Error reconfiguring TXQ #%d\n", queue);
2774 return ret;
2775 }
2776 }
2777 }
2778
2779 if (alloc_queue)
2780 iwl_mvm_enable_txq(mvm, queue,
2781 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2782 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002783
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002784 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2785 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2786 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2787 if (ret)
2788 return -EIO;
2789 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002790
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002791 /* No need to mark as reserved */
2792 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002793 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002794 spin_unlock_bh(&mvm->queue_info_lock);
2795
Sara Sharon34e10862017-02-23 13:15:07 +02002796out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002797 /*
2798 * Even though in theory the peer could have different
2799 * aggregation reorder buffer sizes for different sessions,
2800 * our ucode doesn't allow for that and has a global limit
2801 * for each station. Therefore, use the minimum of all the
2802 * aggregation sessions and our default value.
2803 */
2804 mvmsta->max_agg_bufsize =
2805 min(mvmsta->max_agg_bufsize, buf_size);
2806 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2807
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002808 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2809 sta->addr, tid);
2810
Eyal Shapira9e680942013-11-09 00:16:16 +02002811 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002812}
2813
Sara Sharon34e10862017-02-23 13:15:07 +02002814static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2815 struct iwl_mvm_sta *mvmsta,
2816 u16 txq_id)
2817{
2818 if (iwl_mvm_has_new_tx_api(mvm))
2819 return;
2820
2821 spin_lock_bh(&mvm->queue_info_lock);
2822 /*
2823 * The TXQ is marked as reserved only if no traffic came through yet
2824 * This means no traffic has been sent on this TID (agg'd or not), so
2825 * we no longer have use for the queue. Since it hasn't even been
2826 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2827 * free.
2828 */
2829 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2830 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2831
2832 spin_unlock_bh(&mvm->queue_info_lock);
2833}
2834
Johannes Berg8ca151b2013-01-24 14:25:36 +01002835int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2836 struct ieee80211_sta *sta, u16 tid)
2837{
Johannes Berg5b577a92013-11-14 18:20:04 +01002838 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002839 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2840 u16 txq_id;
2841 int err;
2842
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002843 /*
2844 * If mac80211 is cleaning its state, then say that we finished since
2845 * our state has been cleared anyway.
2846 */
2847 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2848 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2849 return 0;
2850 }
2851
Johannes Berg8ca151b2013-01-24 14:25:36 +01002852 spin_lock_bh(&mvmsta->lock);
2853
2854 txq_id = tid_data->txq_id;
2855
2856 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2857 mvmsta->sta_id, tid, txq_id, tid_data->state);
2858
Eyal Shapiraefed6642014-09-14 15:58:53 +03002859 mvmsta->agg_tids &= ~BIT(tid);
2860
Sara Sharon34e10862017-02-23 13:15:07 +02002861 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002862
Johannes Berg8ca151b2013-01-24 14:25:36 +01002863 switch (tid_data->state) {
2864 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002865 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002866
2867 IWL_DEBUG_TX_QUEUES(mvm,
2868 "ssn = %d, next_recl = %d\n",
2869 tid_data->ssn, tid_data->next_reclaimed);
2870
Liad Kaufman664e9682017-04-05 10:35:18 +03002871 /*
2872 * There are still packets for this RA / TID in the HW.
2873 * Not relevant for DQA mode, since there is no need to disable
2874 * the queue.
2875 */
2876 if (!iwl_mvm_is_dqa_supported(mvm) &&
2877 tid_data->ssn != tid_data->next_reclaimed) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002878 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2879 err = 0;
2880 break;
2881 }
2882
2883 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002884 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002885 spin_unlock_bh(&mvmsta->lock);
2886
2887 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2888
2889 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2890
Liad Kaufmancf961e12015-08-13 19:16:08 +03002891 if (!iwl_mvm_is_dqa_supported(mvm)) {
2892 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2893
2894 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2895 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002896 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002897 case IWL_AGG_STARTING:
2898 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2899 /*
2900 * The agg session has been stopped before it was set up. This
2901 * can happen when the AddBA timer times out for example.
2902 */
2903
2904 /* No barriers since we are under mutex */
2905 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002906
2907 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2908 tid_data->state = IWL_AGG_OFF;
2909 err = 0;
2910 break;
2911 default:
2912 IWL_ERR(mvm,
2913 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2914 mvmsta->sta_id, tid, tid_data->state);
2915 IWL_ERR(mvm,
2916 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2917 err = -EINVAL;
2918 }
2919
2920 spin_unlock_bh(&mvmsta->lock);
2921
2922 return err;
2923}
2924
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002925int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2926 struct ieee80211_sta *sta, u16 tid)
2927{
Johannes Berg5b577a92013-11-14 18:20:04 +01002928 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002929 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2930 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002931 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002932
2933 /*
2934 * First set the agg state to OFF to avoid calling
2935 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2936 */
2937 spin_lock_bh(&mvmsta->lock);
2938 txq_id = tid_data->txq_id;
2939 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2940 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002941 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002942 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002943 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002944 spin_unlock_bh(&mvmsta->lock);
2945
Sara Sharon34e10862017-02-23 13:15:07 +02002946 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002947
Johannes Bergb6658ff2013-07-24 13:55:51 +02002948 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002949 iwl_mvm_drain_sta(mvm, mvmsta, true);
Sara Sharond6d517b2017-03-06 10:16:11 +02002950
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002951 if (iwl_mvm_has_new_tx_api(mvm)) {
2952 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
2953 BIT(tid), 0))
2954 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002955 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002956 } else {
2957 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2958 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002959 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002960 }
Sara Sharond6d517b2017-03-06 10:16:11 +02002961
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002962 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002963
Johannes Bergf7f89e72014-08-05 15:24:44 +02002964 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2965
Liad Kaufmancf961e12015-08-13 19:16:08 +03002966 if (!iwl_mvm_is_dqa_supported(mvm)) {
2967 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2968
2969 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2970 tid, 0);
2971 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002972 }
2973
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002974 return 0;
2975}
2976
Johannes Berg8ca151b2013-01-24 14:25:36 +01002977static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2978{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002979 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002980
2981 lockdep_assert_held(&mvm->mutex);
2982
Johannes Berg2dc2a152015-06-16 17:09:18 +02002983 /* Pick the unused key offset with the highest 'deleted'
2984 * counter. Every time a key is deleted, all the counters
2985 * are incremented and the one that was just deleted is
2986 * reset to zero. Thus, the highest counter is the one
2987 * that was deleted longest ago. Pick that one.
2988 */
2989 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2990 if (test_bit(i, mvm->fw_key_table))
2991 continue;
2992 if (mvm->fw_key_deleted[i] > max) {
2993 max = mvm->fw_key_deleted[i];
2994 max_offs = i;
2995 }
2996 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002997
Johannes Berg2dc2a152015-06-16 17:09:18 +02002998 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002999 return STA_KEY_IDX_INVALID;
3000
Johannes Berg2dc2a152015-06-16 17:09:18 +02003001 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003002}
3003
Johannes Berg5f7a1842015-12-11 09:36:10 +01003004static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3005 struct ieee80211_vif *vif,
3006 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003007{
Johannes Berg5b530e92014-12-23 16:00:17 +01003008 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003009
Johannes Berg5f7a1842015-12-11 09:36:10 +01003010 if (sta)
3011 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003012
3013 /*
3014 * The device expects GTKs for station interfaces to be
3015 * installed as GTKs for the AP station. If we have no
3016 * station ID, then use AP's station ID.
3017 */
3018 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02003019 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Avri Altman9513c5e2015-10-19 16:29:11 +02003020 u8 sta_id = mvmvif->ap_sta_id;
3021
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03003022 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3023 lockdep_is_held(&mvm->mutex));
3024
Avri Altman9513c5e2015-10-19 16:29:11 +02003025 /*
3026 * It is possible that the 'sta' parameter is NULL,
3027 * for example when a GTK is removed - the sta_id will then
3028 * be the AP ID, and no station was passed by mac80211.
3029 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03003030 if (IS_ERR_OR_NULL(sta))
3031 return NULL;
3032
3033 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02003034 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003035
Johannes Berg5f7a1842015-12-11 09:36:10 +01003036 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003037}
3038
3039static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
David Spinadel85aeb582017-03-30 19:43:53 +03003040 u32 sta_id,
Sara Sharon45c458b2016-11-09 15:43:26 +02003041 struct ieee80211_key_conf *key, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003042 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3043 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003044{
Sara Sharon45c458b2016-11-09 15:43:26 +02003045 union {
3046 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3047 struct iwl_mvm_add_sta_key_cmd cmd;
3048 } u = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003049 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01003050 int ret;
3051 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003052 u16 keyidx;
Sara Sharon45c458b2016-11-09 15:43:26 +02003053 u64 pn = 0;
3054 int i, size;
3055 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3056 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003057
David Spinadel85aeb582017-03-30 19:43:53 +03003058 if (sta_id == IWL_MVM_INVALID_STA)
3059 return -EINVAL;
3060
Sara Sharon45c458b2016-11-09 15:43:26 +02003061 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
Johannes Berg8ca151b2013-01-24 14:25:36 +01003062 STA_KEY_FLG_KEYID_MSK;
3063 key_flags = cpu_to_le16(keyidx);
3064 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3065
Sara Sharon45c458b2016-11-09 15:43:26 +02003066 switch (key->cipher) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003067 case WLAN_CIPHER_SUITE_TKIP:
3068 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003069 if (new_api) {
3070 memcpy((void *)&u.cmd.tx_mic_key,
3071 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3072 IWL_MIC_KEY_SIZE);
3073
3074 memcpy((void *)&u.cmd.rx_mic_key,
3075 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3076 IWL_MIC_KEY_SIZE);
3077 pn = atomic64_read(&key->tx_pn);
3078
3079 } else {
3080 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3081 for (i = 0; i < 5; i++)
3082 u.cmd_v1.tkip_rx_ttak[i] =
3083 cpu_to_le16(tkip_p1k[i]);
3084 }
3085 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003086 break;
3087 case WLAN_CIPHER_SUITE_CCMP:
3088 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Sara Sharon45c458b2016-11-09 15:43:26 +02003089 memcpy(u.cmd.common.key, key->key, key->keylen);
3090 if (new_api)
3091 pn = atomic64_read(&key->tx_pn);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003092 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01003093 case WLAN_CIPHER_SUITE_WEP104:
3094 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05003095 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01003096 case WLAN_CIPHER_SUITE_WEP40:
3097 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003098 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
Johannes Bergba3943b2014-11-12 23:54:48 +01003099 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03003100 case WLAN_CIPHER_SUITE_GCMP_256:
3101 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3102 /* fall through */
3103 case WLAN_CIPHER_SUITE_GCMP:
3104 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003105 memcpy(u.cmd.common.key, key->key, key->keylen);
3106 if (new_api)
3107 pn = atomic64_read(&key->tx_pn);
Ayala Beker2a53d162016-04-07 16:21:57 +03003108 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003109 default:
Max Stepanove36e5432013-08-27 19:56:13 +03003110 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
Sara Sharon45c458b2016-11-09 15:43:26 +02003111 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003112 }
3113
Johannes Bergba3943b2014-11-12 23:54:48 +01003114 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003115 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3116
Sara Sharon45c458b2016-11-09 15:43:26 +02003117 u.cmd.common.key_offset = key_offset;
3118 u.cmd.common.key_flags = key_flags;
David Spinadel85aeb582017-03-30 19:43:53 +03003119 u.cmd.common.sta_id = sta_id;
Sara Sharon45c458b2016-11-09 15:43:26 +02003120
3121 if (new_api) {
3122 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3123 size = sizeof(u.cmd);
3124 } else {
3125 size = sizeof(u.cmd_v1);
3126 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003127
3128 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003129 if (cmd_flags & CMD_ASYNC)
Sara Sharon45c458b2016-11-09 15:43:26 +02003130 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3131 &u.cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003132 else
Sara Sharon45c458b2016-11-09 15:43:26 +02003133 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3134 &u.cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003135
3136 switch (status) {
3137 case ADD_STA_SUCCESS:
3138 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3139 break;
3140 default:
3141 ret = -EIO;
3142 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3143 break;
3144 }
3145
3146 return ret;
3147}
3148
3149static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3150 struct ieee80211_key_conf *keyconf,
3151 u8 sta_id, bool remove_key)
3152{
3153 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3154
3155 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03003156 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3157 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3158 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3159 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3160 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3161 return -EINVAL;
3162
3163 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3164 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01003165 return -EINVAL;
3166
3167 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3168 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3169
3170 if (remove_key) {
3171 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3172 } else {
3173 struct ieee80211_key_seq seq;
3174 const u8 *pn;
3175
Ayala Bekeraa950522016-06-01 00:28:09 +03003176 switch (keyconf->cipher) {
3177 case WLAN_CIPHER_SUITE_AES_CMAC:
3178 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3179 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03003180 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3181 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3182 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3183 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03003184 default:
3185 return -EINVAL;
3186 }
3187
Ayala Beker8e160ab2016-04-11 11:37:38 +03003188 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3189 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3190 igtk_cmd.ctrl_flags |=
3191 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003192 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3193 pn = seq.aes_cmac.pn;
3194 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3195 ((u64) pn[4] << 8) |
3196 ((u64) pn[3] << 16) |
3197 ((u64) pn[2] << 24) |
3198 ((u64) pn[1] << 32) |
3199 ((u64) pn[0] << 40));
3200 }
3201
3202 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3203 remove_key ? "removing" : "installing",
3204 igtk_cmd.sta_id);
3205
Ayala Beker8e160ab2016-04-11 11:37:38 +03003206 if (!iwl_mvm_has_new_rx_api(mvm)) {
3207 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3208 .ctrl_flags = igtk_cmd.ctrl_flags,
3209 .key_id = igtk_cmd.key_id,
3210 .sta_id = igtk_cmd.sta_id,
3211 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3212 };
3213
3214 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3215 ARRAY_SIZE(igtk_cmd_v1.igtk));
3216 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3217 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3218 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003219 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003220 sizeof(igtk_cmd), &igtk_cmd);
3221}
3222
3223
3224static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3225 struct ieee80211_vif *vif,
3226 struct ieee80211_sta *sta)
3227{
Johannes Berg5b530e92014-12-23 16:00:17 +01003228 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003229
3230 if (sta)
3231 return sta->addr;
3232
3233 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02003234 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003235 u8 sta_id = mvmvif->ap_sta_id;
3236 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3237 lockdep_is_held(&mvm->mutex));
3238 return sta->addr;
3239 }
3240
3241
3242 return NULL;
3243}
3244
Johannes Berg2f6319d2014-11-12 23:39:56 +01003245static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3246 struct ieee80211_vif *vif,
3247 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01003248 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003249 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01003250 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003251{
Johannes Berg8ca151b2013-01-24 14:25:36 +01003252 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003253 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003254 struct ieee80211_key_seq seq;
3255 u16 p1k[5];
David Spinadel85aeb582017-03-30 19:43:53 +03003256 u32 sta_id;
3257
3258 if (sta) {
3259 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3260
3261 sta_id = mvm_sta->sta_id;
3262 } else if (vif->type == NL80211_IFTYPE_AP &&
3263 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3264 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3265
3266 sta_id = mvmvif->mcast_sta.sta_id;
3267 } else {
3268 IWL_ERR(mvm, "Failed to find station id\n");
3269 return -EINVAL;
3270 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003271
Johannes Berg8ca151b2013-01-24 14:25:36 +01003272 switch (keyconf->cipher) {
3273 case WLAN_CIPHER_SUITE_TKIP:
David Spinadel85aeb582017-03-30 19:43:53 +03003274 if (vif->type == NL80211_IFTYPE_AP) {
3275 ret = -EINVAL;
3276 break;
3277 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003278 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3279 /* get phase 1 key from mac80211 */
3280 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3281 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
David Spinadel85aeb582017-03-30 19:43:53 +03003282 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003283 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003284 break;
3285 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01003286 case WLAN_CIPHER_SUITE_WEP40:
3287 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03003288 case WLAN_CIPHER_SUITE_GCMP:
3289 case WLAN_CIPHER_SUITE_GCMP_256:
David Spinadel85aeb582017-03-30 19:43:53 +03003290 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003291 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003292 break;
3293 default:
David Spinadel85aeb582017-03-30 19:43:53 +03003294 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003295 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003296 }
3297
Johannes Berg8ca151b2013-01-24 14:25:36 +01003298 return ret;
3299}
3300
Johannes Berg2f6319d2014-11-12 23:39:56 +01003301static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01003302 struct ieee80211_key_conf *keyconf,
3303 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003304{
Sara Sharon45c458b2016-11-09 15:43:26 +02003305 union {
3306 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3307 struct iwl_mvm_add_sta_key_cmd cmd;
3308 } u = {};
3309 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3310 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003311 __le16 key_flags;
Sara Sharon45c458b2016-11-09 15:43:26 +02003312 int ret, size;
Johannes Berg79920742014-11-03 15:43:04 +01003313 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003314
David Spinadel85aeb582017-03-30 19:43:53 +03003315 if (sta_id == IWL_MVM_INVALID_STA)
3316 return -EINVAL;
3317
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02003318 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3319 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003320 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3321 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3322
Johannes Bergba3943b2014-11-12 23:54:48 +01003323 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003324 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3325
Sara Sharon45c458b2016-11-09 15:43:26 +02003326 /*
3327 * The fields assigned here are in the same location at the start
3328 * of the command, so we can do this union trick.
3329 */
3330 u.cmd.common.key_flags = key_flags;
3331 u.cmd.common.key_offset = keyconf->hw_key_idx;
3332 u.cmd.common.sta_id = sta_id;
3333
3334 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003335
Johannes Berg8ca151b2013-01-24 14:25:36 +01003336 status = ADD_STA_SUCCESS;
Sara Sharon45c458b2016-11-09 15:43:26 +02003337 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3338 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003339
3340 switch (status) {
3341 case ADD_STA_SUCCESS:
3342 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3343 break;
3344 default:
3345 ret = -EIO;
3346 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3347 break;
3348 }
3349
3350 return ret;
3351}
3352
Johannes Berg2f6319d2014-11-12 23:39:56 +01003353int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3354 struct ieee80211_vif *vif,
3355 struct ieee80211_sta *sta,
3356 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003357 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003358{
Johannes Bergba3943b2014-11-12 23:54:48 +01003359 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003360 struct iwl_mvm_sta *mvm_sta;
David Spinadel85aeb582017-03-30 19:43:53 +03003361 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003362 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03003363 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01003364
3365 lockdep_assert_held(&mvm->mutex);
3366
David Spinadel85aeb582017-03-30 19:43:53 +03003367 if (vif->type != NL80211_IFTYPE_AP ||
3368 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3369 /* Get the station id from the mvm local station table */
3370 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3371 if (!mvm_sta) {
3372 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01003373 return -EINVAL;
3374 }
David Spinadel85aeb582017-03-30 19:43:53 +03003375 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003376
David Spinadel85aeb582017-03-30 19:43:53 +03003377 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3378 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3379 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3380 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id,
3381 false);
3382 goto end;
3383 }
3384
3385 /*
3386 * It is possible that the 'sta' parameter is NULL, and thus
3387 * there is a need to retrieve the sta from the local station
3388 * table.
3389 */
3390 if (!sta) {
3391 sta = rcu_dereference_protected(
3392 mvm->fw_id_to_mac_id[sta_id],
3393 lockdep_is_held(&mvm->mutex));
3394 if (IS_ERR_OR_NULL(sta)) {
3395 IWL_ERR(mvm, "Invalid station id\n");
3396 return -EINVAL;
3397 }
3398 }
3399
3400 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3401 return -EINVAL;
3402 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003403
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003404 /* If the key_offset is not pre-assigned, we need to find a
3405 * new offset to use. In normal cases, the offset is not
3406 * pre-assigned, but during HW_RESTART we want to reuse the
3407 * same indices, so we pass them when this function is called.
3408 *
3409 * In D3 entry, we need to hardcoded the indices (because the
3410 * firmware hardcodes the PTK offset to 0). In this case, we
3411 * need to make sure we don't overwrite the hw_key_idx in the
3412 * keyconf structure, because otherwise we cannot configure
3413 * the original ones back when resuming.
3414 */
3415 if (key_offset == STA_KEY_IDX_INVALID) {
3416 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3417 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003418 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003419 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003420 }
3421
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003422 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003423 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01003424 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003425
3426 /*
3427 * For WEP, the same key is used for multicast and unicast. Upload it
3428 * again, using the same key offset, and now pointing the other one
3429 * to the same key slot (offset).
3430 * If this fails, remove the original as well.
3431 */
David Spinadel85aeb582017-03-30 19:43:53 +03003432 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3433 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3434 sta) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003435 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3436 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01003437 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01003438 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003439 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003440 }
3441 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003442
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003443 __set_bit(key_offset, mvm->fw_key_table);
3444
Johannes Berg2f6319d2014-11-12 23:39:56 +01003445end:
3446 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3447 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03003448 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01003449 return ret;
3450}
3451
3452int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3453 struct ieee80211_vif *vif,
3454 struct ieee80211_sta *sta,
3455 struct ieee80211_key_conf *keyconf)
3456{
Johannes Bergba3943b2014-11-12 23:54:48 +01003457 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003458 struct iwl_mvm_sta *mvm_sta;
Sara Sharon0ae98812017-01-04 14:53:58 +02003459 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2dc2a152015-06-16 17:09:18 +02003460 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003461
3462 lockdep_assert_held(&mvm->mutex);
3463
Johannes Berg5f7a1842015-12-11 09:36:10 +01003464 /* Get the station from the mvm local station table */
3465 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Luca Coelho71793b7d2017-03-30 12:04:47 +03003466 if (mvm_sta)
3467 sta_id = mvm_sta->sta_id;
David Spinadel85aeb582017-03-30 19:43:53 +03003468 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3469 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3470
Johannes Berg2f6319d2014-11-12 23:39:56 +01003471
3472 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3473 keyconf->keyidx, sta_id);
3474
Luca Coelho71793b7d2017-03-30 12:04:47 +03003475 if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3476 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3477 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
Johannes Berg2f6319d2014-11-12 23:39:56 +01003478 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3479
3480 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3481 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3482 keyconf->hw_key_idx);
3483 return -ENOENT;
3484 }
3485
Johannes Berg2dc2a152015-06-16 17:09:18 +02003486 /* track which key was deleted last */
3487 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3488 if (mvm->fw_key_deleted[i] < U8_MAX)
3489 mvm->fw_key_deleted[i]++;
3490 }
3491 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3492
David Spinadel85aeb582017-03-30 19:43:53 +03003493 if (sta && !mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003494 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3495 return 0;
3496 }
3497
Johannes Bergba3943b2014-11-12 23:54:48 +01003498 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3499 if (ret)
3500 return ret;
3501
3502 /* delete WEP key twice to get rid of (now useless) offset */
3503 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3504 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3505 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3506
3507 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003508}
3509
Johannes Berg8ca151b2013-01-24 14:25:36 +01003510void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3511 struct ieee80211_vif *vif,
3512 struct ieee80211_key_conf *keyconf,
3513 struct ieee80211_sta *sta, u32 iv32,
3514 u16 *phase1key)
3515{
Beni Levc3eb5362013-02-06 17:22:18 +02003516 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01003517 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003518
Beni Levc3eb5362013-02-06 17:22:18 +02003519 rcu_read_lock();
3520
Johannes Berg5f7a1842015-12-11 09:36:10 +01003521 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3522 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003523 goto unlock;
David Spinadel85aeb582017-03-30 19:43:53 +03003524 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003525 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003526
3527 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02003528 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01003529}
3530
Johannes Berg9cc40712013-02-15 22:47:48 +01003531void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3532 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003533{
Johannes Berg5b577a92013-11-14 18:20:04 +01003534 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003535 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003536 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003537 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03003538 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01003539 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003540 };
3541 int ret;
3542
Sara Sharon854c5702016-01-26 13:17:47 +02003543 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3544 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003545 if (ret)
3546 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3547}
3548
Johannes Berg9cc40712013-02-15 22:47:48 +01003549void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3550 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003551 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003552 u16 cnt, u16 tids, bool more_data,
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003553 bool single_sta_queue)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003554{
Johannes Berg5b577a92013-11-14 18:20:04 +01003555 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003556 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003557 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003558 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003559 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3560 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003561 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003562 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003563 int tid, ret;
3564 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003565
Johannes Berg3e56ead2013-02-15 22:23:18 +01003566 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3567 * Note that this field is reserved and unused by firmware not
3568 * supporting GO uAPSD, so it's safe to always do this.
3569 */
3570 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3571 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3572
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003573 /* If we're releasing frames from aggregation or dqa queues then check
3574 * if all the queues that we're releasing frames from, combined, have:
Johannes Berg3e56ead2013-02-15 22:23:18 +01003575 * - more frames than the service period, in which case more_data
3576 * needs to be set
3577 * - fewer than 'cnt' frames, in which case we need to adjust the
3578 * firmware command (but do that unconditionally)
3579 */
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003580 if (single_sta_queue) {
Johannes Berg3e56ead2013-02-15 22:23:18 +01003581 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003582 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003583
3584 spin_lock_bh(&mvmsta->lock);
3585 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3586 struct iwl_mvm_tid_data *tid_data;
3587 u16 n_queued;
3588
3589 tid_data = &mvmsta->tid_data[tid];
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003590 if (WARN(!iwl_mvm_is_dqa_supported(mvm) &&
3591 tid_data->state != IWL_AGG_ON &&
Johannes Berg3e56ead2013-02-15 22:23:18 +01003592 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3593 "TID %d state is %d\n",
3594 tid, tid_data->state)) {
3595 spin_unlock_bh(&mvmsta->lock);
3596 ieee80211_sta_eosp(sta);
3597 return;
3598 }
3599
Liad Kaufmandd321622017-04-05 16:25:11 +03003600 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003601 if (n_queued > remaining) {
3602 more_data = true;
3603 remaining = 0;
3604 break;
3605 }
3606 remaining -= n_queued;
3607 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003608 sleep_tx_count = cnt - remaining;
3609 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3610 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003611 spin_unlock_bh(&mvmsta->lock);
3612
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003613 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003614 if (WARN_ON(cnt - remaining == 0)) {
3615 ieee80211_sta_eosp(sta);
3616 return;
3617 }
3618 }
3619
3620 /* Note: this is ignored by firmware not supporting GO uAPSD */
3621 if (more_data)
Sara Sharonced19f22017-02-06 19:09:32 +02003622 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003623
3624 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3625 mvmsta->next_status_eosp = true;
Sara Sharonced19f22017-02-06 19:09:32 +02003626 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003627 } else {
Sara Sharonced19f22017-02-06 19:09:32 +02003628 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003629 }
3630
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003631 /* block the Tx queues until the FW updated the sleep Tx count */
3632 iwl_trans_block_txq_ptrs(mvm->trans, true);
3633
3634 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3635 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003636 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003637 if (ret)
3638 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3639}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003640
Johannes Berg04168412015-06-23 21:22:09 +02003641void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3642 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003643{
3644 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3645 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3646 struct ieee80211_sta *sta;
3647 u32 sta_id = le32_to_cpu(notif->sta_id);
3648
3649 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003650 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003651
3652 rcu_read_lock();
3653 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3654 if (!IS_ERR_OR_NULL(sta))
3655 ieee80211_sta_eosp(sta);
3656 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003657}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003658
3659void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3660 struct iwl_mvm_sta *mvmsta, bool disable)
3661{
3662 struct iwl_mvm_add_sta_cmd cmd = {
3663 .add_modify = STA_MODE_MODIFY,
3664 .sta_id = mvmsta->sta_id,
3665 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3666 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3667 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3668 };
3669 int ret;
3670
Sara Sharon854c5702016-01-26 13:17:47 +02003671 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3672 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003673 if (ret)
3674 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3675}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003676
3677void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3678 struct ieee80211_sta *sta,
3679 bool disable)
3680{
3681 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3682
3683 spin_lock_bh(&mvm_sta->lock);
3684
3685 if (mvm_sta->disable_tx == disable) {
3686 spin_unlock_bh(&mvm_sta->lock);
3687 return;
3688 }
3689
3690 mvm_sta->disable_tx = disable;
3691
3692 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03003693 * Tell mac80211 to start/stop queuing tx for this station,
3694 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003695 * for this station.
3696 */
3697 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3698 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3699
3700 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3701
3702 spin_unlock_bh(&mvm_sta->lock);
3703}
3704
Sara Sharonced19f22017-02-06 19:09:32 +02003705static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3706 struct iwl_mvm_vif *mvmvif,
3707 struct iwl_mvm_int_sta *sta,
3708 bool disable)
3709{
3710 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3711 struct iwl_mvm_add_sta_cmd cmd = {
3712 .add_modify = STA_MODE_MODIFY,
3713 .sta_id = sta->sta_id,
3714 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3715 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3716 .mac_id_n_color = cpu_to_le32(id),
3717 };
3718 int ret;
3719
3720 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3721 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3722 if (ret)
3723 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3724}
3725
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003726void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3727 struct iwl_mvm_vif *mvmvif,
3728 bool disable)
3729{
3730 struct ieee80211_sta *sta;
3731 struct iwl_mvm_sta *mvm_sta;
3732 int i;
3733
3734 lockdep_assert_held(&mvm->mutex);
3735
3736 /* Block/unblock all the stations of the given mvmvif */
Sara Sharon0ae98812017-01-04 14:53:58 +02003737 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003738 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3739 lockdep_is_held(&mvm->mutex));
3740 if (IS_ERR_OR_NULL(sta))
3741 continue;
3742
3743 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3744 if (mvm_sta->mac_id_n_color !=
3745 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3746 continue;
3747
3748 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3749 }
Sara Sharonced19f22017-02-06 19:09:32 +02003750
3751 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3752 return;
3753
3754 /* Need to block/unblock also multicast station */
3755 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3756 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3757 &mvmvif->mcast_sta, disable);
3758
3759 /*
3760 * Only unblock the broadcast station (FW blocks it for immediate
3761 * quiet, not the driver)
3762 */
3763 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3764 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3765 &mvmvif->bcast_sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003766}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003767
3768void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3769{
3770 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3771 struct iwl_mvm_sta *mvmsta;
3772
3773 rcu_read_lock();
3774
3775 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3776
3777 if (!WARN_ON(!mvmsta))
3778 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3779
3780 rcu_read_unlock();
3781}
Liad Kaufmandd321622017-04-05 16:25:11 +03003782
3783u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3784{
3785 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3786
3787 /*
3788 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
3789 * to align the wrap around of ssn so we compare relevant values.
3790 */
3791 if (mvm->trans->cfg->gen2)
3792 sn &= 0xff;
3793
3794 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3795}