blob: 5c707c62f1dc1c8b8534500437d57c65c4317a0c [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 |
124 STA_FLG_MIMO_EN_MSK),
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200125 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300126 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100127 int ret;
128 u32 status;
129 u32 agg_size = 0, mpdu_dens = 0;
130
Sara Sharonced19f22017-02-06 19:09:32 +0200131 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
132 add_sta_cmd.station_type = mvm_sta->sta_type;
133
Liad Kaufman24afba72015-07-28 18:56:08 +0300134 if (!update || (flags & STA_MODIFY_QUEUES)) {
Johannes Berg7a453972013-02-12 13:10:44 +0100135 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
Liad Kaufman24afba72015-07-28 18:56:08 +0300136
Sara Sharonbb497012016-09-29 14:52:40 +0300137 if (!iwl_mvm_has_new_tx_api(mvm)) {
138 add_sta_cmd.tfd_queue_msk =
139 cpu_to_le32(mvm_sta->tfd_queue_msk);
140
141 if (flags & STA_MODIFY_QUEUES)
142 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
143 } else {
144 WARN_ON(flags & STA_MODIFY_QUEUES);
145 }
Johannes Berg7a453972013-02-12 13:10:44 +0100146 }
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100147
148 switch (sta->bandwidth) {
149 case IEEE80211_STA_RX_BW_160:
150 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
151 /* fall through */
152 case IEEE80211_STA_RX_BW_80:
153 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
154 /* fall through */
155 case IEEE80211_STA_RX_BW_40:
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
157 /* fall through */
158 case IEEE80211_STA_RX_BW_20:
159 if (sta->ht_cap.ht_supported)
160 add_sta_cmd.station_flags |=
161 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
162 break;
163 }
164
165 switch (sta->rx_nss) {
166 case 1:
167 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
168 break;
169 case 2:
170 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
171 break;
172 case 3 ... 8:
173 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
174 break;
175 }
176
177 switch (sta->smps_mode) {
178 case IEEE80211_SMPS_AUTOMATIC:
179 case IEEE80211_SMPS_NUM_MODES:
180 WARN_ON(1);
181 break;
182 case IEEE80211_SMPS_STATIC:
183 /* override NSS */
184 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
185 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
186 break;
187 case IEEE80211_SMPS_DYNAMIC:
188 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
189 break;
190 case IEEE80211_SMPS_OFF:
191 /* nothing */
192 break;
193 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100194
195 if (sta->ht_cap.ht_supported) {
196 add_sta_cmd.station_flags_msk |=
197 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
198 STA_FLG_AGG_MPDU_DENS_MSK);
199
200 mpdu_dens = sta->ht_cap.ampdu_density;
201 }
202
203 if (sta->vht_cap.vht_supported) {
204 agg_size = sta->vht_cap.cap &
205 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
206 agg_size >>=
207 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
208 } else if (sta->ht_cap.ht_supported) {
209 agg_size = sta->ht_cap.ampdu_factor;
210 }
211
212 add_sta_cmd.station_flags |=
213 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
214 add_sta_cmd.station_flags |=
215 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
Johannes Berg6ea29ce2016-12-01 16:25:30 +0100216 if (mvm_sta->associated)
217 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100218
Johannes Berg65e25482016-04-13 14:24:22 +0200219 if (sta->wme) {
220 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
221
222 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200223 add_sta_cmd.uapsd_acs |= BIT(AC_BK);
Johannes Berg65e25482016-04-13 14:24:22 +0200224 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200225 add_sta_cmd.uapsd_acs |= BIT(AC_BE);
Johannes Berg65e25482016-04-13 14:24:22 +0200226 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200227 add_sta_cmd.uapsd_acs |= BIT(AC_VI);
Johannes Berg65e25482016-04-13 14:24:22 +0200228 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
Emmanuel Grumbachc80eb572017-01-12 15:43:57 +0200229 add_sta_cmd.uapsd_acs |= BIT(AC_VO);
230 add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
Emmanuel Grumbache71ca5e2017-02-08 14:53:32 +0200231 add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
Johannes Berg65e25482016-04-13 14:24:22 +0200232 }
233
Johannes Berg8ca151b2013-01-24 14:25:36 +0100234 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200235 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
236 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300237 &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100238 if (ret)
239 return ret;
240
Sara Sharon837c4da2016-01-07 16:50:45 +0200241 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100242 case ADD_STA_SUCCESS:
243 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
244 break;
245 default:
246 ret = -EIO;
247 IWL_ERR(mvm, "ADD_STA failed\n");
248 break;
249 }
250
251 return ret;
252}
253
Sara Sharon10b2b202016-03-20 16:23:41 +0200254static void iwl_mvm_rx_agg_session_expired(unsigned long data)
255{
256 struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
257 struct iwl_mvm_baid_data *ba_data;
258 struct ieee80211_sta *sta;
259 struct iwl_mvm_sta *mvm_sta;
260 unsigned long timeout;
261
262 rcu_read_lock();
263
264 ba_data = rcu_dereference(*rcu_ptr);
265
266 if (WARN_ON(!ba_data))
267 goto unlock;
268
269 if (!ba_data->timeout)
270 goto unlock;
271
272 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
273 if (time_is_after_jiffies(timeout)) {
274 mod_timer(&ba_data->session_timer, timeout);
275 goto unlock;
276 }
277
278 /* Timer expired */
279 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
280 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
281 ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
282 sta->addr, ba_data->tid);
283unlock:
284 rcu_read_unlock();
285}
286
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300287static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
288 struct ieee80211_sta *sta)
289{
290 unsigned long used_hw_queues;
291 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200292 unsigned int wdg_timeout =
293 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300294 u32 ac;
295
296 lockdep_assert_held(&mvm->mutex);
297
298 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
299
300 /* Find available queues, and allocate them to the ACs */
301 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
302 u8 queue = find_first_zero_bit(&used_hw_queues,
303 mvm->first_agg_queue);
304
305 if (queue >= mvm->first_agg_queue) {
306 IWL_ERR(mvm, "Failed to allocate STA queue\n");
307 return -EBUSY;
308 }
309
310 __set_bit(queue, &used_hw_queues);
311 mvmsta->hw_queue[ac] = queue;
312 }
313
314 /* Found a place for all queues - enable them */
315 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
316 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300317 mvmsta->hw_queue[ac],
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +0300318 iwl_mvm_mac_ac_to_tx_fifo(mvm, ac), 0,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300319 wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300320 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
321 }
322
323 return 0;
324}
325
326static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
327 struct ieee80211_sta *sta)
328{
329 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
330 unsigned long sta_msk;
331 int i;
332
333 lockdep_assert_held(&mvm->mutex);
334
335 /* disable the TDLS STA-specific queues */
336 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200337 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300338 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300339}
340
Liad Kaufman9794c642015-08-19 17:34:28 +0300341/* Disable aggregations for a bitmap of TIDs for a given station */
342static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
343 unsigned long disable_agg_tids,
344 bool remove_queue)
345{
346 struct iwl_mvm_add_sta_cmd cmd = {};
347 struct ieee80211_sta *sta;
348 struct iwl_mvm_sta *mvmsta;
349 u32 status;
350 u8 sta_id;
351 int ret;
352
Sara Sharonbb497012016-09-29 14:52:40 +0300353 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
354 return -EINVAL;
355
Liad Kaufman9794c642015-08-19 17:34:28 +0300356 spin_lock_bh(&mvm->queue_info_lock);
357 sta_id = mvm->queue_info[queue].ra_sta_id;
358 spin_unlock_bh(&mvm->queue_info_lock);
359
360 rcu_read_lock();
361
362 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
363
364 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
365 rcu_read_unlock();
366 return -EINVAL;
367 }
368
369 mvmsta = iwl_mvm_sta_from_mac80211(sta);
370
371 mvmsta->tid_disable_agg |= disable_agg_tids;
372
373 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
374 cmd.sta_id = mvmsta->sta_id;
375 cmd.add_modify = STA_MODE_MODIFY;
376 cmd.modify_mask = STA_MODIFY_QUEUES;
377 if (disable_agg_tids)
378 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
379 if (remove_queue)
380 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
381 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
382 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
383
384 rcu_read_unlock();
385
386 /* Notify FW of queue removal from the STA queues */
387 status = ADD_STA_SUCCESS;
388 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
389 iwl_mvm_add_sta_cmd_size(mvm),
390 &cmd, &status);
391
392 return ret;
393}
394
Liad Kaufman42db09c2016-05-02 14:01:14 +0300395static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
396{
397 struct ieee80211_sta *sta;
398 struct iwl_mvm_sta *mvmsta;
399 unsigned long tid_bitmap;
400 unsigned long agg_tids = 0;
Sharon Dvir806911d2017-05-21 12:09:49 +0300401 u8 sta_id;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300402 int tid;
403
404 lockdep_assert_held(&mvm->mutex);
405
Sara Sharonbb497012016-09-29 14:52:40 +0300406 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
407 return -EINVAL;
408
Liad Kaufman42db09c2016-05-02 14:01:14 +0300409 spin_lock_bh(&mvm->queue_info_lock);
410 sta_id = mvm->queue_info[queue].ra_sta_id;
411 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
412 spin_unlock_bh(&mvm->queue_info_lock);
413
414 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
415 lockdep_is_held(&mvm->mutex));
416
417 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
418 return -EINVAL;
419
420 mvmsta = iwl_mvm_sta_from_mac80211(sta);
421
422 spin_lock_bh(&mvmsta->lock);
423 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
424 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
425 agg_tids |= BIT(tid);
426 }
427 spin_unlock_bh(&mvmsta->lock);
428
429 return agg_tids;
430}
431
Liad Kaufman9794c642015-08-19 17:34:28 +0300432/*
433 * Remove a queue from a station's resources.
434 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
435 * doesn't disable the queue
436 */
437static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
438{
439 struct ieee80211_sta *sta;
440 struct iwl_mvm_sta *mvmsta;
441 unsigned long tid_bitmap;
442 unsigned long disable_agg_tids = 0;
443 u8 sta_id;
444 int tid;
445
446 lockdep_assert_held(&mvm->mutex);
447
Sara Sharonbb497012016-09-29 14:52:40 +0300448 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
449 return -EINVAL;
450
Liad Kaufman9794c642015-08-19 17:34:28 +0300451 spin_lock_bh(&mvm->queue_info_lock);
452 sta_id = mvm->queue_info[queue].ra_sta_id;
453 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
454 spin_unlock_bh(&mvm->queue_info_lock);
455
456 rcu_read_lock();
457
458 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
459
460 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
461 rcu_read_unlock();
462 return 0;
463 }
464
465 mvmsta = iwl_mvm_sta_from_mac80211(sta);
466
467 spin_lock_bh(&mvmsta->lock);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300468 /* Unmap MAC queues and TIDs from this queue */
Liad Kaufman9794c642015-08-19 17:34:28 +0300469 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300470 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
471 disable_agg_tids |= BIT(tid);
Sara Sharon6862fce2017-02-22 19:34:17 +0200472 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman9794c642015-08-19 17:34:28 +0300473 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300474
Liad Kaufman42db09c2016-05-02 14:01:14 +0300475 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
Liad Kaufman9794c642015-08-19 17:34:28 +0300476 spin_unlock_bh(&mvmsta->lock);
477
478 rcu_read_unlock();
479
Liad Kaufman9794c642015-08-19 17:34:28 +0300480 return disable_agg_tids;
481}
482
Sara Sharon01796ff2016-11-16 17:04:36 +0200483static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
484 bool same_sta)
485{
486 struct iwl_mvm_sta *mvmsta;
487 u8 txq_curr_ac, sta_id, tid;
488 unsigned long disable_agg_tids = 0;
489 int ret;
490
491 lockdep_assert_held(&mvm->mutex);
492
Sara Sharonbb497012016-09-29 14:52:40 +0300493 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
494 return -EINVAL;
495
Sara Sharon01796ff2016-11-16 17:04:36 +0200496 spin_lock_bh(&mvm->queue_info_lock);
497 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
498 sta_id = mvm->queue_info[queue].ra_sta_id;
499 tid = mvm->queue_info[queue].txq_tid;
500 spin_unlock_bh(&mvm->queue_info_lock);
501
502 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Sharon Dvire3df1e42017-02-21 10:41:31 +0200503 if (WARN_ON(!mvmsta))
504 return -EINVAL;
Sara Sharon01796ff2016-11-16 17:04:36 +0200505
506 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
507 /* Disable the queue */
508 if (disable_agg_tids)
509 iwl_mvm_invalidate_sta_queue(mvm, queue,
510 disable_agg_tids, false);
511
512 ret = iwl_mvm_disable_txq(mvm, queue,
513 mvmsta->vif->hw_queue[txq_curr_ac],
514 tid, 0);
515 if (ret) {
516 /* Re-mark the inactive queue as inactive */
517 spin_lock_bh(&mvm->queue_info_lock);
518 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
519 spin_unlock_bh(&mvm->queue_info_lock);
520 IWL_ERR(mvm,
521 "Failed to free inactive queue %d (ret=%d)\n",
522 queue, ret);
523
524 return ret;
525 }
526
527 /* If TXQ is allocated to another STA, update removal in FW */
528 if (!same_sta)
529 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
530
531 return 0;
532}
533
Liad Kaufman42db09c2016-05-02 14:01:14 +0300534static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
535 unsigned long tfd_queue_mask, u8 ac)
536{
537 int queue = 0;
538 u8 ac_to_queue[IEEE80211_NUM_ACS];
539 int i;
540
541 lockdep_assert_held(&mvm->queue_info_lock);
Sara Sharonbb497012016-09-29 14:52:40 +0300542 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
543 return -EINVAL;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300544
545 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
546
547 /* See what ACs the existing queues for this STA have */
548 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
549 /* Only DATA queues can be shared */
550 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
551 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
552 continue;
553
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200554 /* Don't try and take queues being reconfigured */
555 if (mvm->queue_info[queue].status ==
556 IWL_MVM_QUEUE_RECONFIGURING)
557 continue;
558
Liad Kaufman42db09c2016-05-02 14:01:14 +0300559 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
560 }
561
562 /*
563 * The queue to share is chosen only from DATA queues as follows (in
564 * descending priority):
565 * 1. An AC_BE queue
566 * 2. Same AC queue
567 * 3. Highest AC queue that is lower than new AC
568 * 4. Any existing AC (there always is at least 1 DATA queue)
569 */
570
571 /* Priority 1: An AC_BE queue */
572 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
573 queue = ac_to_queue[IEEE80211_AC_BE];
574 /* Priority 2: Same AC queue */
575 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
576 queue = ac_to_queue[ac];
577 /* Priority 3a: If new AC is VO and VI exists - use VI */
578 else if (ac == IEEE80211_AC_VO &&
579 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
580 queue = ac_to_queue[IEEE80211_AC_VI];
581 /* Priority 3b: No BE so only AC less than the new one is BK */
582 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
583 queue = ac_to_queue[IEEE80211_AC_BK];
584 /* Priority 4a: No BE nor BK - use VI if exists */
585 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
586 queue = ac_to_queue[IEEE80211_AC_VI];
587 /* Priority 4b: No BE, BK nor VI - use VO if exists */
588 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
589 queue = ac_to_queue[IEEE80211_AC_VO];
590
591 /* Make sure queue found (or not) is legal */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200592 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
593 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
594 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
Liad Kaufman42db09c2016-05-02 14:01:14 +0300595 IWL_ERR(mvm, "No DATA queues available to share\n");
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200596 return -ENOSPC;
597 }
598
599 /* Make sure the queue isn't in the middle of being reconfigured */
600 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
601 IWL_ERR(mvm,
602 "TXQ %d is in the middle of re-config - try again\n",
603 queue);
604 return -EBUSY;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300605 }
606
607 return queue;
608}
609
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200610/*
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200611 * If a given queue has a higher AC than the TID stream that is being compared
612 * to, the queue needs to be redirected to the lower AC. This function does that
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200613 * in such a case, otherwise - if no redirection required - it does nothing,
614 * unless the %force param is true.
615 */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200616int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
617 int ac, int ssn, unsigned int wdg_timeout,
618 bool force)
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200619{
620 struct iwl_scd_txq_cfg_cmd cmd = {
621 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200622 .action = SCD_CFG_DISABLE_QUEUE,
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200623 };
624 bool shared_queue;
625 unsigned long mq;
626 int ret;
627
Sara Sharonbb497012016-09-29 14:52:40 +0300628 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
629 return -EINVAL;
630
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200631 /*
632 * If the AC is lower than current one - FIFO needs to be redirected to
633 * the lowest one of the streams in the queue. Check if this is needed
634 * here.
635 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
636 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
637 * we need to check if the numerical value of X is LARGER than of Y.
638 */
639 spin_lock_bh(&mvm->queue_info_lock);
640 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
641 spin_unlock_bh(&mvm->queue_info_lock);
642
643 IWL_DEBUG_TX_QUEUES(mvm,
644 "No redirection needed on TXQ #%d\n",
645 queue);
646 return 0;
647 }
648
649 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
650 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200651 cmd.tid = mvm->queue_info[queue].txq_tid;
Sara Sharon34e10862017-02-23 13:15:07 +0200652 mq = mvm->hw_queue_to_mac80211[queue];
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200653 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
654 spin_unlock_bh(&mvm->queue_info_lock);
655
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200656 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200657 queue, iwl_mvm_ac_to_tx_fifo[ac]);
658
659 /* Stop MAC queues and wait for this queue to empty */
660 iwl_mvm_stop_mac_queues(mvm, mq);
Sara Sharona1a57872017-03-05 11:38:58 +0200661 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200662 if (ret) {
663 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
664 queue);
665 ret = -EIO;
666 goto out;
667 }
668
669 /* Before redirecting the queue we need to de-activate it */
670 iwl_trans_txq_disable(mvm->trans, queue, false);
671 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
672 if (ret)
673 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
674 ret);
675
676 /* Make sure the SCD wrptr is correctly set before reconfiguring */
Sara Sharonca3b9c62016-06-30 16:14:02 +0300677 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200678
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200679 /* Update the TID "owner" of the queue */
680 spin_lock_bh(&mvm->queue_info_lock);
681 mvm->queue_info[queue].txq_tid = tid;
682 spin_unlock_bh(&mvm->queue_info_lock);
683
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200684 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
685
686 /* Redirect to lower AC */
687 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
688 cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
689 ssn);
690
691 /* Update AC marking of the queue */
692 spin_lock_bh(&mvm->queue_info_lock);
693 mvm->queue_info[queue].mac80211_ac = ac;
694 spin_unlock_bh(&mvm->queue_info_lock);
695
696 /*
697 * Mark queue as shared in transport if shared
698 * Note this has to be done after queue enablement because enablement
699 * can also set this value, and there is no indication there to shared
700 * queues
701 */
702 if (shared_queue)
703 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
704
705out:
706 /* Continue using the MAC queues */
707 iwl_mvm_start_mac_queues(mvm, mq);
708
709 return ret;
710}
711
Sara Sharon310181e2017-01-17 14:27:48 +0200712static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
713 struct ieee80211_sta *sta, u8 ac,
714 int tid)
715{
716 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
717 unsigned int wdg_timeout =
718 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
719 u8 mac_queue = mvmsta->vif->hw_queue[ac];
720 int queue = -1;
721
722 lockdep_assert_held(&mvm->mutex);
723
724 IWL_DEBUG_TX_QUEUES(mvm,
725 "Allocating queue for sta %d on tid %d\n",
726 mvmsta->sta_id, tid);
727 queue = iwl_mvm_tvqm_enable_txq(mvm, mac_queue, mvmsta->sta_id, tid,
728 wdg_timeout);
729 if (queue < 0)
730 return queue;
731
732 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
733
734 spin_lock_bh(&mvmsta->lock);
735 mvmsta->tid_data[tid].txq_id = queue;
736 mvmsta->tid_data[tid].is_tid_active = true;
Sara Sharon310181e2017-01-17 14:27:48 +0200737 spin_unlock_bh(&mvmsta->lock);
738
Sara Sharon310181e2017-01-17 14:27:48 +0200739 return 0;
740}
741
Liad Kaufman24afba72015-07-28 18:56:08 +0300742static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
743 struct ieee80211_sta *sta, u8 ac, int tid,
744 struct ieee80211_hdr *hdr)
745{
746 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
747 struct iwl_trans_txq_scd_cfg cfg = {
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +0300748 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
Liad Kaufman24afba72015-07-28 18:56:08 +0300749 .sta_id = mvmsta->sta_id,
750 .tid = tid,
751 .frame_limit = IWL_FRAME_LIMIT,
752 };
753 unsigned int wdg_timeout =
754 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
755 u8 mac_queue = mvmsta->vif->hw_queue[ac];
756 int queue = -1;
Sara Sharon01796ff2016-11-16 17:04:36 +0200757 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman9794c642015-08-19 17:34:28 +0300758 unsigned long disable_agg_tids = 0;
759 enum iwl_mvm_agg_state queue_state;
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300760 bool shared_queue = false, inc_ssn;
Liad Kaufman24afba72015-07-28 18:56:08 +0300761 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300762 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300763 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300764
765 lockdep_assert_held(&mvm->mutex);
766
Sara Sharon310181e2017-01-17 14:27:48 +0200767 if (iwl_mvm_has_new_tx_api(mvm))
768 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
769
Liad Kaufman42db09c2016-05-02 14:01:14 +0300770 spin_lock_bh(&mvmsta->lock);
771 tfd_queue_mask = mvmsta->tfd_queue_msk;
772 spin_unlock_bh(&mvmsta->lock);
773
Liad Kaufmand2515a92016-03-23 16:31:08 +0200774 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300775
776 /*
777 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
778 * exists
779 */
780 if (!ieee80211_is_data_qos(hdr->frame_control) ||
781 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300782 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
783 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300784 IWL_MVM_DQA_MAX_MGMT_QUEUE);
785 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
786 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
787 queue);
788
789 /* If no such queue is found, we'll use a DATA queue instead */
790 }
791
Liad Kaufman9794c642015-08-19 17:34:28 +0300792 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
793 (mvm->queue_info[mvmsta->reserved_queue].status ==
794 IWL_MVM_QUEUE_RESERVED ||
795 mvm->queue_info[mvmsta->reserved_queue].status ==
796 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300797 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300798 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300799 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
800 }
801
802 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300803 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
804 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300805 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300806
807 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300808 * Check if this queue is already allocated but inactive.
809 * In such a case, we'll need to first free this queue before enabling
810 * it again, so we'll mark it as reserved to make sure no new traffic
811 * arrives on it
812 */
813 if (queue > 0 &&
814 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
815 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
816 using_inactive_queue = true;
Sara Sharon01796ff2016-11-16 17:04:36 +0200817 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman9794c642015-08-19 17:34:28 +0300818 IWL_DEBUG_TX_QUEUES(mvm,
819 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
820 queue, mvmsta->sta_id, tid);
821 }
822
Liad Kaufman42db09c2016-05-02 14:01:14 +0300823 /* No free queue - we'll have to share */
824 if (queue <= 0) {
825 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
826 if (queue > 0) {
827 shared_queue = true;
828 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
829 }
830 }
831
Liad Kaufman9794c642015-08-19 17:34:28 +0300832 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300833 * Mark TXQ as ready, even though it hasn't been fully configured yet,
834 * to make sure no one else takes it.
835 * This will allow avoiding re-acquiring the lock at the end of the
836 * configuration. On error we'll mark it back as free.
837 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300838 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300839 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300840
Liad Kaufmand2515a92016-03-23 16:31:08 +0200841 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300842
Liad Kaufman42db09c2016-05-02 14:01:14 +0300843 /* This shouldn't happen - out of queues */
844 if (WARN_ON(queue <= 0)) {
845 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
846 tid, cfg.sta_id);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200847 return queue;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300848 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300849
850 /*
851 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
852 * but for configuring the SCD to send A-MPDUs we need to mark the queue
853 * as aggregatable.
854 * Mark all DATA queues as allowing to be aggregated at some point
855 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300856 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
857 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300858
Liad Kaufman9794c642015-08-19 17:34:28 +0300859 /*
860 * If this queue was previously inactive (idle) - we need to free it
861 * first
862 */
863 if (using_inactive_queue) {
Sara Sharon01796ff2016-11-16 17:04:36 +0200864 ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
865 if (ret)
Liad Kaufman9794c642015-08-19 17:34:28 +0300866 return ret;
Liad Kaufman9794c642015-08-19 17:34:28 +0300867 }
868
Liad Kaufman42db09c2016-05-02 14:01:14 +0300869 IWL_DEBUG_TX_QUEUES(mvm,
870 "Allocating %squeue #%d to sta %d on tid %d\n",
871 shared_queue ? "shared " : "", queue,
872 mvmsta->sta_id, tid);
873
874 if (shared_queue) {
875 /* Disable any open aggs on this queue */
876 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
877
878 if (disable_agg_tids) {
879 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
880 queue);
881 iwl_mvm_invalidate_sta_queue(mvm, queue,
882 disable_agg_tids, false);
883 }
Liad Kaufman42db09c2016-05-02 14:01:14 +0300884 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300885
886 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300887 inc_ssn = iwl_mvm_enable_txq(mvm, queue, mac_queue,
888 ssn, &cfg, wdg_timeout);
889 if (inc_ssn) {
890 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
891 le16_add_cpu(&hdr->seq_ctrl, 0x10);
892 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300893
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200894 /*
895 * Mark queue as shared in transport if shared
896 * Note this has to be done after queue enablement because enablement
897 * can also set this value, and there is no indication there to shared
898 * queues
899 */
900 if (shared_queue)
901 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
902
Liad Kaufman24afba72015-07-28 18:56:08 +0300903 spin_lock_bh(&mvmsta->lock);
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300904 /*
905 * This looks racy, but it is not. We have only one packet for
906 * this ra/tid in our Tx path since we stop the Qdisc when we
907 * need to allocate a new TFD queue.
908 */
909 if (inc_ssn)
910 mvmsta->tid_data[tid].seq_number += 0x10;
Liad Kaufman24afba72015-07-28 18:56:08 +0300911 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300912 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300913 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300914 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300915
916 if (mvmsta->reserved_queue == queue)
917 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
918 spin_unlock_bh(&mvmsta->lock);
919
Liad Kaufman42db09c2016-05-02 14:01:14 +0300920 if (!shared_queue) {
921 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
922 if (ret)
923 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300924
Liad Kaufman42db09c2016-05-02 14:01:14 +0300925 /* If we need to re-enable aggregations... */
926 if (queue_state == IWL_AGG_ON) {
927 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
928 if (ret)
929 goto out_err;
930 }
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200931 } else {
932 /* Redirect queue, if needed */
933 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
934 wdg_timeout, false);
935 if (ret)
936 goto out_err;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300937 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300938
Liad Kaufman42db09c2016-05-02 14:01:14 +0300939 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300940
941out_err:
942 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
943
944 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300945}
946
Liad Kaufman19aefa42016-03-08 14:29:51 +0200947static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
948{
949 struct iwl_scd_txq_cfg_cmd cmd = {
950 .scd_queue = queue,
951 .action = SCD_CFG_UPDATE_QUEUE_TID,
952 };
Liad Kaufman19aefa42016-03-08 14:29:51 +0200953 int tid;
954 unsigned long tid_bitmap;
955 int ret;
956
957 lockdep_assert_held(&mvm->mutex);
958
Sara Sharonbb497012016-09-29 14:52:40 +0300959 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
960 return;
961
Liad Kaufman19aefa42016-03-08 14:29:51 +0200962 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200963 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
964 spin_unlock_bh(&mvm->queue_info_lock);
965
966 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
967 return;
968
969 /* Find any TID for queue */
970 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
971 cmd.tid = tid;
972 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
973
974 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
Liad Kaufman341ca402016-09-18 14:51:59 +0300975 if (ret) {
Liad Kaufman19aefa42016-03-08 14:29:51 +0200976 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
977 queue, ret);
Liad Kaufman341ca402016-09-18 14:51:59 +0300978 return;
979 }
980
981 spin_lock_bh(&mvm->queue_info_lock);
982 mvm->queue_info[queue].txq_tid = tid;
983 spin_unlock_bh(&mvm->queue_info_lock);
984 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
985 queue, tid);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200986}
987
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200988static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
989{
990 struct ieee80211_sta *sta;
991 struct iwl_mvm_sta *mvmsta;
Sharon Dvir806911d2017-05-21 12:09:49 +0300992 u8 sta_id;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200993 int tid = -1;
994 unsigned long tid_bitmap;
995 unsigned int wdg_timeout;
996 int ssn;
997 int ret = true;
998
Sara Sharonbb497012016-09-29 14:52:40 +0300999 /* queue sharing is disabled on new TX path */
1000 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1001 return;
1002
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001003 lockdep_assert_held(&mvm->mutex);
1004
1005 spin_lock_bh(&mvm->queue_info_lock);
1006 sta_id = mvm->queue_info[queue].ra_sta_id;
1007 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1008 spin_unlock_bh(&mvm->queue_info_lock);
1009
1010 /* Find TID for queue, and make sure it is the only one on the queue */
1011 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1012 if (tid_bitmap != BIT(tid)) {
1013 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
1014 queue, tid_bitmap);
1015 return;
1016 }
1017
1018 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
1019 tid);
1020
1021 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1022 lockdep_is_held(&mvm->mutex));
1023
1024 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1025 return;
1026
1027 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1028 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1029
1030 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1031
1032 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
1033 tid_to_mac80211_ac[tid], ssn,
1034 wdg_timeout, true);
1035 if (ret) {
1036 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1037 return;
1038 }
1039
1040 /* If aggs should be turned back on - do it */
1041 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
Emmanuel Grumbach9cd70e82016-09-20 13:40:33 +03001042 struct iwl_mvm_add_sta_cmd cmd = {0};
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001043
1044 mvmsta->tid_disable_agg &= ~BIT(tid);
1045
1046 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1047 cmd.sta_id = mvmsta->sta_id;
1048 cmd.add_modify = STA_MODE_MODIFY;
1049 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1050 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1051 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1052
1053 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1054 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1055 if (!ret) {
1056 IWL_DEBUG_TX_QUEUES(mvm,
1057 "TXQ #%d is now aggregated again\n",
1058 queue);
1059
1060 /* Mark queue intenally as aggregating again */
1061 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1062 }
1063 }
1064
1065 spin_lock_bh(&mvm->queue_info_lock);
1066 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1067 spin_unlock_bh(&mvm->queue_info_lock);
1068}
1069
Liad Kaufman24afba72015-07-28 18:56:08 +03001070static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1071{
1072 if (tid == IWL_MAX_TID_COUNT)
1073 return IEEE80211_AC_VO; /* MGMT */
1074
1075 return tid_to_mac80211_ac[tid];
1076}
1077
1078static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
1079 struct ieee80211_sta *sta, int tid)
1080{
1081 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1082 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1083 struct sk_buff *skb;
1084 struct ieee80211_hdr *hdr;
1085 struct sk_buff_head deferred_tx;
1086 u8 mac_queue;
1087 bool no_queue = false; /* Marks if there is a problem with the queue */
1088 u8 ac;
1089
1090 lockdep_assert_held(&mvm->mutex);
1091
1092 skb = skb_peek(&tid_data->deferred_tx_frames);
1093 if (!skb)
1094 return;
1095 hdr = (void *)skb->data;
1096
1097 ac = iwl_mvm_tid_to_ac_queue(tid);
1098 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
1099
Sara Sharon6862fce2017-02-22 19:34:17 +02001100 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE &&
Liad Kaufman24afba72015-07-28 18:56:08 +03001101 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
1102 IWL_ERR(mvm,
1103 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
1104 mvmsta->sta_id, tid);
1105
1106 /*
1107 * Mark queue as problematic so later the deferred traffic is
1108 * freed, as we can do nothing with it
1109 */
1110 no_queue = true;
1111 }
1112
1113 __skb_queue_head_init(&deferred_tx);
1114
Liad Kaufmand2515a92016-03-23 16:31:08 +02001115 /* Disable bottom-halves when entering TX path */
1116 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +03001117 spin_lock(&mvmsta->lock);
1118 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
Liad Kaufmanad5de732016-09-27 16:01:10 +03001119 mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
Liad Kaufman24afba72015-07-28 18:56:08 +03001120 spin_unlock(&mvmsta->lock);
1121
Liad Kaufman24afba72015-07-28 18:56:08 +03001122 while ((skb = __skb_dequeue(&deferred_tx)))
1123 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1124 ieee80211_free_txskb(mvm->hw, skb);
1125 local_bh_enable();
1126
1127 /* Wake queue */
1128 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1129}
1130
1131void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1132{
1133 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1134 add_stream_wk);
1135 struct ieee80211_sta *sta;
1136 struct iwl_mvm_sta *mvmsta;
1137 unsigned long deferred_tid_traffic;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001138 int queue, sta_id, tid;
Liad Kaufman24afba72015-07-28 18:56:08 +03001139
Liad Kaufman9794c642015-08-19 17:34:28 +03001140 /* Check inactivity of queues */
1141 iwl_mvm_inactivity_check(mvm);
1142
Liad Kaufman24afba72015-07-28 18:56:08 +03001143 mutex_lock(&mvm->mutex);
1144
Sara Sharon34e10862017-02-23 13:15:07 +02001145 /* No queue reconfiguration in TVQM mode */
1146 if (iwl_mvm_has_new_tx_api(mvm))
1147 goto alloc_queues;
1148
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001149 /* Reconfigure queues requiring reconfiguation */
Sara Sharon34e10862017-02-23 13:15:07 +02001150 for (queue = 0; queue < ARRAY_SIZE(mvm->queue_info); queue++) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001151 bool reconfig;
Liad Kaufman19aefa42016-03-08 14:29:51 +02001152 bool change_owner;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001153
1154 spin_lock_bh(&mvm->queue_info_lock);
1155 reconfig = (mvm->queue_info[queue].status ==
1156 IWL_MVM_QUEUE_RECONFIGURING);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001157
1158 /*
1159 * We need to take into account a situation in which a TXQ was
1160 * allocated to TID x, and then turned shared by adding TIDs y
1161 * and z. If TID x becomes inactive and is removed from the TXQ,
1162 * ownership must be given to one of the remaining TIDs.
1163 * This is mainly because if TID x continues - a new queue can't
1164 * be allocated for it as long as it is an owner of another TXQ.
1165 */
1166 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1167 BIT(mvm->queue_info[queue].txq_tid)) &&
1168 (mvm->queue_info[queue].status ==
1169 IWL_MVM_QUEUE_SHARED);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001170 spin_unlock_bh(&mvm->queue_info_lock);
1171
1172 if (reconfig)
1173 iwl_mvm_unshare_queue(mvm, queue);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001174 else if (change_owner)
1175 iwl_mvm_change_queue_owner(mvm, queue);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001176 }
1177
Sara Sharon34e10862017-02-23 13:15:07 +02001178alloc_queues:
Liad Kaufman24afba72015-07-28 18:56:08 +03001179 /* Go over all stations with deferred traffic */
1180 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1181 IWL_MVM_STATION_COUNT) {
1182 clear_bit(sta_id, mvm->sta_deferred_frames);
1183 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1184 lockdep_is_held(&mvm->mutex));
1185 if (IS_ERR_OR_NULL(sta))
1186 continue;
1187
1188 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1189 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1190
1191 for_each_set_bit(tid, &deferred_tid_traffic,
1192 IWL_MAX_TID_COUNT + 1)
1193 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1194 }
1195
1196 mutex_unlock(&mvm->mutex);
1197}
1198
1199static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001200 struct ieee80211_sta *sta,
1201 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +03001202{
1203 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1204 int queue;
Sara Sharon01796ff2016-11-16 17:04:36 +02001205 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman24afba72015-07-28 18:56:08 +03001206
Sara Sharon396952e2017-02-22 19:40:55 +02001207 /* queue reserving is disabled on new TX path */
1208 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1209 return 0;
1210
Liad Kaufman9794c642015-08-19 17:34:28 +03001211 /*
1212 * Check for inactive queues, so we don't reach a situation where we
1213 * can't add a STA due to a shortage in queues that doesn't really exist
1214 */
1215 iwl_mvm_inactivity_check(mvm);
1216
Liad Kaufman24afba72015-07-28 18:56:08 +03001217 spin_lock_bh(&mvm->queue_info_lock);
1218
1219 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +03001220 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1221 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +03001222 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1223 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +03001224 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1225 else
Liad Kaufman9794c642015-08-19 17:34:28 +03001226 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1227 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001228 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +03001229 if (queue < 0) {
1230 spin_unlock_bh(&mvm->queue_info_lock);
1231 IWL_ERR(mvm, "No available queues for new station\n");
1232 return -ENOSPC;
Sara Sharon01796ff2016-11-16 17:04:36 +02001233 } else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
1234 /*
1235 * If this queue is already allocated but inactive we'll need to
1236 * first free this queue before enabling it again, we'll mark
1237 * it as reserved to make sure no new traffic arrives on it
1238 */
1239 using_inactive_queue = true;
1240 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman24afba72015-07-28 18:56:08 +03001241 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03001242 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +03001243
1244 spin_unlock_bh(&mvm->queue_info_lock);
1245
1246 mvmsta->reserved_queue = queue;
1247
Sara Sharon01796ff2016-11-16 17:04:36 +02001248 if (using_inactive_queue)
1249 iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
1250
Liad Kaufman24afba72015-07-28 18:56:08 +03001251 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1252 queue, mvmsta->sta_id);
1253
1254 return 0;
1255}
1256
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001257/*
1258 * In DQA mode, after a HW restart the queues should be allocated as before, in
1259 * order to avoid race conditions when there are shared queues. This function
1260 * does the re-mapping and queue allocation.
1261 *
1262 * Note that re-enabling aggregations isn't done in this function.
1263 */
1264static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1265 struct iwl_mvm_sta *mvm_sta)
1266{
1267 unsigned int wdg_timeout =
1268 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1269 int i;
1270 struct iwl_trans_txq_scd_cfg cfg = {
1271 .sta_id = mvm_sta->sta_id,
1272 .frame_limit = IWL_FRAME_LIMIT,
1273 };
1274
Johannes Berg03c902b2016-12-02 12:03:36 +01001275 /* Make sure reserved queue is still marked as such (if allocated) */
1276 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1277 mvm->queue_info[mvm_sta->reserved_queue].status =
1278 IWL_MVM_QUEUE_RESERVED;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001279
1280 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1281 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1282 int txq_id = tid_data->txq_id;
1283 int ac;
1284 u8 mac_queue;
1285
Sara Sharon6862fce2017-02-22 19:34:17 +02001286 if (txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001287 continue;
1288
1289 skb_queue_head_init(&tid_data->deferred_tx_frames);
1290
1291 ac = tid_to_mac80211_ac[i];
1292 mac_queue = mvm_sta->vif->hw_queue[ac];
1293
Sara Sharon310181e2017-01-17 14:27:48 +02001294 if (iwl_mvm_has_new_tx_api(mvm)) {
1295 IWL_DEBUG_TX_QUEUES(mvm,
1296 "Re-mapping sta %d tid %d\n",
1297 mvm_sta->sta_id, i);
1298 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mac_queue,
1299 mvm_sta->sta_id,
1300 i, wdg_timeout);
1301 tid_data->txq_id = txq_id;
1302 } else {
1303 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001304
Sara Sharon310181e2017-01-17 14:27:48 +02001305 cfg.tid = i;
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +03001306 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
Sara Sharon310181e2017-01-17 14:27:48 +02001307 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1308 txq_id ==
1309 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001310
Sara Sharon310181e2017-01-17 14:27:48 +02001311 IWL_DEBUG_TX_QUEUES(mvm,
1312 "Re-mapping sta %d tid %d to queue %d\n",
1313 mvm_sta->sta_id, i, txq_id);
1314
1315 iwl_mvm_enable_txq(mvm, txq_id, mac_queue, seq, &cfg,
1316 wdg_timeout);
Sara Sharon34e10862017-02-23 13:15:07 +02001317 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
Sara Sharon310181e2017-01-17 14:27:48 +02001318 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001319 }
1320
1321 atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
1322}
1323
Johannes Berg8ca151b2013-01-24 14:25:36 +01001324int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1325 struct ieee80211_vif *vif,
1326 struct ieee80211_sta *sta)
1327{
1328 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001329 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001330 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001331 int i, ret, sta_id;
1332
1333 lockdep_assert_held(&mvm->mutex);
1334
1335 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001336 sta_id = iwl_mvm_find_free_sta_id(mvm,
1337 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001338 else
1339 sta_id = mvm_sta->sta_id;
1340
Sara Sharon0ae98812017-01-04 14:53:58 +02001341 if (sta_id == IWL_MVM_INVALID_STA)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001342 return -ENOSPC;
1343
1344 spin_lock_init(&mvm_sta->lock);
1345
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001346 /* In DQA mode, if this is a HW restart, re-alloc existing queues */
1347 if (iwl_mvm_is_dqa_supported(mvm) &&
1348 test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1349 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1350 goto update_fw;
1351 }
1352
Johannes Berg8ca151b2013-01-24 14:25:36 +01001353 mvm_sta->sta_id = sta_id;
1354 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1355 mvmvif->color);
1356 mvm_sta->vif = vif;
Liad Kaufmana58bb462017-05-28 14:20:04 +03001357 if (!mvm->trans->cfg->gen2)
1358 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1359 else
1360 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001361 mvm_sta->tx_protection = 0;
1362 mvm_sta->tt_tx_protection = false;
Sara Sharonced19f22017-02-06 19:09:32 +02001363 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001364
1365 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001366 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +03001367 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001368 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001369
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001370 /*
1371 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1372 * and then they'll be allocated dynamically
1373 */
1374 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001375 ret = iwl_mvm_tdls_sta_init(mvm, sta);
1376 if (ret)
1377 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +03001378 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001379 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1380 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1381 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1382 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001383
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001384 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001385 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001386 u16 seq = mvm_sta->tid_data[i].seq_number;
1387 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1388 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001389
1390 if (!iwl_mvm_is_dqa_supported(mvm))
1391 continue;
1392
1393 /*
1394 * Mark all queues for this STA as unallocated and defer TX
1395 * frames until the queue is allocated
1396 */
Sara Sharon6862fce2017-02-22 19:34:17 +02001397 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001398 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001399 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001400 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001401 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001402
Sara Sharona571f5f2015-12-07 12:50:58 +02001403 if (iwl_mvm_has_new_rx_api(mvm) &&
1404 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Johannes Berg92c4dca2017-06-07 10:35:54 +02001405 int q;
1406
Sara Sharona571f5f2015-12-07 12:50:58 +02001407 dup_data = kcalloc(mvm->trans->num_rx_queues,
Johannes Berg92c4dca2017-06-07 10:35:54 +02001408 sizeof(*dup_data), GFP_KERNEL);
Sara Sharona571f5f2015-12-07 12:50:58 +02001409 if (!dup_data)
1410 return -ENOMEM;
Johannes Berg92c4dca2017-06-07 10:35:54 +02001411 /*
1412 * Initialize all the last_seq values to 0xffff which can never
1413 * compare equal to the frame's seq_ctrl in the check in
1414 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1415 * number and fragmented packets don't reach that function.
1416 *
1417 * This thus allows receiving a packet with seqno 0 and the
1418 * retry bit set as the very first packet on a new TID.
1419 */
1420 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1421 memset(dup_data[q].last_seq, 0xff,
1422 sizeof(dup_data[q].last_seq));
Sara Sharona571f5f2015-12-07 12:50:58 +02001423 mvm_sta->dup_data = dup_data;
1424 }
1425
Sara Sharon396952e2017-02-22 19:40:55 +02001426 if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001427 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1428 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001429 if (ret)
1430 goto err;
1431 }
1432
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001433update_fw:
Liad Kaufman24afba72015-07-28 18:56:08 +03001434 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001435 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001436 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001437
Johannes Berg9e848012014-08-04 14:33:42 +02001438 if (vif->type == NL80211_IFTYPE_STATION) {
1439 if (!sta->tdls) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001440 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001441 mvmvif->ap_sta_id = sta_id;
1442 } else {
Sara Sharon0ae98812017-01-04 14:53:58 +02001443 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001444 }
1445 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001446
1447 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1448
1449 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001450
1451err:
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001452 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1453 iwl_mvm_tdls_sta_deinit(mvm, sta);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001454 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001455}
1456
1457int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1458 bool drain)
1459{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001460 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001461 int ret;
1462 u32 status;
1463
1464 lockdep_assert_held(&mvm->mutex);
1465
1466 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1467 cmd.sta_id = mvmsta->sta_id;
1468 cmd.add_modify = STA_MODE_MODIFY;
1469 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1470 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1471
1472 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001473 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1474 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001475 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001476 if (ret)
1477 return ret;
1478
Sara Sharon837c4da2016-01-07 16:50:45 +02001479 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001480 case ADD_STA_SUCCESS:
1481 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1482 mvmsta->sta_id);
1483 break;
1484 default:
1485 ret = -EIO;
1486 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1487 mvmsta->sta_id);
1488 break;
1489 }
1490
1491 return ret;
1492}
1493
1494/*
1495 * Remove a station from the FW table. Before sending the command to remove
1496 * the station validate that the station is indeed known to the driver (sanity
1497 * only).
1498 */
1499static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1500{
1501 struct ieee80211_sta *sta;
1502 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1503 .sta_id = sta_id,
1504 };
1505 int ret;
1506
1507 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1508 lockdep_is_held(&mvm->mutex));
1509
1510 /* Note: internal stations are marked as error values */
1511 if (!sta) {
1512 IWL_ERR(mvm, "Invalid station id\n");
1513 return -EINVAL;
1514 }
1515
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001516 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001517 sizeof(rm_sta_cmd), &rm_sta_cmd);
1518 if (ret) {
1519 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1520 return ret;
1521 }
1522
1523 return 0;
1524}
1525
1526void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1527{
1528 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1529 u8 sta_id;
1530
1531 /*
1532 * The mutex is needed because of the SYNC cmd, but not only: if the
1533 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1534 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1535 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1536 * that later.
1537 */
1538 mutex_lock(&mvm->mutex);
1539
1540 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1541 int ret;
1542 struct ieee80211_sta *sta =
1543 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1544 lockdep_is_held(&mvm->mutex));
1545
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001546 /*
1547 * This station is in use or RCU-removed; the latter happens in
1548 * managed mode, where mac80211 removes the station before we
1549 * can remove it from firmware (we can only do that after the
1550 * MAC is marked unassociated), and possibly while the deauth
1551 * frame to disconnect from the AP is still queued. Then, the
1552 * station pointer is -ENOENT when the last skb is reclaimed.
1553 */
1554 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001555 continue;
1556
1557 if (PTR_ERR(sta) == -EINVAL) {
1558 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1559 sta_id);
1560 continue;
1561 }
1562
1563 if (!sta) {
1564 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1565 sta_id);
1566 continue;
1567 }
1568
1569 WARN_ON(PTR_ERR(sta) != -EBUSY);
1570 /* This station was removed and we waited until it got drained,
1571 * we can now proceed and remove it.
1572 */
1573 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1574 if (ret) {
1575 IWL_ERR(mvm,
1576 "Couldn't remove sta %d after it was drained\n",
1577 sta_id);
1578 continue;
1579 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301580 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001581 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001582
1583 if (mvm->tfd_drained[sta_id]) {
1584 unsigned long i, msk = mvm->tfd_drained[sta_id];
1585
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001586 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001587 iwl_mvm_disable_txq(mvm, i, i,
1588 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001589
1590 mvm->tfd_drained[sta_id] = 0;
1591 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1592 sta_id, msk);
1593 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001594 }
1595
1596 mutex_unlock(&mvm->mutex);
1597}
1598
Liad Kaufman24afba72015-07-28 18:56:08 +03001599static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1600 struct ieee80211_vif *vif,
1601 struct iwl_mvm_sta *mvm_sta)
1602{
1603 int ac;
1604 int i;
1605
1606 lockdep_assert_held(&mvm->mutex);
1607
1608 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
Sara Sharon6862fce2017-02-22 19:34:17 +02001609 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman24afba72015-07-28 18:56:08 +03001610 continue;
1611
1612 ac = iwl_mvm_tid_to_ac_queue(i);
1613 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1614 vif->hw_queue[ac], i, 0);
Sara Sharon6862fce2017-02-22 19:34:17 +02001615 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001616 }
1617}
1618
Sara Sharond6d517b2017-03-06 10:16:11 +02001619int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1620 struct iwl_mvm_sta *mvm_sta)
1621{
Sharon Dvirbec95222017-06-12 11:40:33 +03001622 int i;
Sara Sharond6d517b2017-03-06 10:16:11 +02001623
1624 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1625 u16 txq_id;
Sharon Dvirbec95222017-06-12 11:40:33 +03001626 int ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001627
1628 spin_lock_bh(&mvm_sta->lock);
1629 txq_id = mvm_sta->tid_data[i].txq_id;
1630 spin_unlock_bh(&mvm_sta->lock);
1631
1632 if (txq_id == IWL_MVM_INVALID_QUEUE)
1633 continue;
1634
1635 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1636 if (ret)
Sharon Dvirbec95222017-06-12 11:40:33 +03001637 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001638 }
1639
Sharon Dvirbec95222017-06-12 11:40:33 +03001640 return 0;
Sara Sharond6d517b2017-03-06 10:16:11 +02001641}
1642
Johannes Berg8ca151b2013-01-24 14:25:36 +01001643int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1644 struct ieee80211_vif *vif,
1645 struct ieee80211_sta *sta)
1646{
1647 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001648 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001649 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001650 int ret;
1651
1652 lockdep_assert_held(&mvm->mutex);
1653
Sara Sharona571f5f2015-12-07 12:50:58 +02001654 if (iwl_mvm_has_new_rx_api(mvm))
1655 kfree(mvm_sta->dup_data);
1656
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001657 if ((vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001658 mvmvif->ap_sta_id == sta_id) ||
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001659 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001660 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1661 if (ret)
1662 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001663 /* flush its queues here since we are freeing mvm_sta */
Sara Sharond49394a2017-03-05 13:01:08 +02001664 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001665 if (ret)
1666 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001667 if (iwl_mvm_has_new_tx_api(mvm)) {
1668 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1669 } else {
1670 u32 q_mask = mvm_sta->tfd_queue_msk;
1671
1672 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1673 q_mask);
1674 }
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001675 if (ret)
1676 return ret;
1677 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001678
Liad Kaufman24afba72015-07-28 18:56:08 +03001679 /* If DQA is supported - the queues can be disabled now */
Sara Sharon94c3e612016-12-07 15:04:37 +02001680 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman56214742016-09-22 15:14:08 +03001681 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001682 /*
1683 * If pending_frames is set at this point - it must be
1684 * driver internal logic error, since queues are empty
1685 * and removed successuly.
1686 * warn on it but set it to 0 anyway to avoid station
1687 * not being removed later in the function
1688 */
1689 WARN_ON(atomic_xchg(&mvm->pending_frames[sta_id], 0));
1690 }
Liad Kaufman56214742016-09-22 15:14:08 +03001691
1692 /* If there is a TXQ still marked as reserved - free it */
1693 if (iwl_mvm_is_dqa_supported(mvm) &&
1694 mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001695 u8 reserved_txq = mvm_sta->reserved_queue;
1696 enum iwl_mvm_queue_status *status;
1697
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001698 /*
1699 * If no traffic has gone through the reserved TXQ - it
1700 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1701 * should be manually marked as free again
1702 */
1703 spin_lock_bh(&mvm->queue_info_lock);
1704 status = &mvm->queue_info[reserved_txq].status;
1705 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1706 (*status != IWL_MVM_QUEUE_FREE),
1707 "sta_id %d reserved txq %d status %d",
Sara Sharon94c3e612016-12-07 15:04:37 +02001708 sta_id, reserved_txq, *status)) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001709 spin_unlock_bh(&mvm->queue_info_lock);
1710 return -EINVAL;
1711 }
1712
1713 *status = IWL_MVM_QUEUE_FREE;
1714 spin_unlock_bh(&mvm->queue_info_lock);
1715 }
1716
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001717 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001718 mvmvif->ap_sta_id == sta_id) {
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001719 /* if associated - we can't remove the AP STA now */
1720 if (vif->bss_conf.assoc)
1721 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001722
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001723 /* unassoc - go ahead - remove the AP STA now */
Sara Sharon0ae98812017-01-04 14:53:58 +02001724 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Eliad Peller37577fe2013-12-05 17:19:39 +02001725
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001726 /* clear d0i3_ap_sta_id if no longer relevant */
Sara Sharon94c3e612016-12-07 15:04:37 +02001727 if (mvm->d0i3_ap_sta_id == sta_id)
Sara Sharon0ae98812017-01-04 14:53:58 +02001728 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001729 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001730 }
1731
1732 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001733 * This shouldn't happen - the TDLS channel switch should be canceled
1734 * before the STA is removed.
1735 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001736 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001737 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001738 cancel_delayed_work(&mvm->tdls_cs.dwork);
1739 }
1740
1741 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001742 * Make sure that the tx response code sees the station as -EBUSY and
1743 * calls the drain worker.
1744 */
1745 spin_lock_bh(&mvm_sta->lock);
Sara Sharon94c3e612016-12-07 15:04:37 +02001746
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001747 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001748 * There are frames pending on the AC queues for this station.
1749 * We need to wait until all the frames are drained...
1750 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001751 if (atomic_read(&mvm->pending_frames[sta_id])) {
1752 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id],
Johannes Berg8ca151b2013-01-24 14:25:36 +01001753 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001754 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001755
1756 /* disable TDLS sta queues on drain complete */
1757 if (sta->tdls) {
Sara Sharon94c3e612016-12-07 15:04:37 +02001758 mvm->tfd_drained[sta_id] = mvm_sta->tfd_queue_msk;
1759 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n", sta_id);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001760 }
1761
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001762 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001763 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001764 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001765
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001766 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001767 iwl_mvm_tdls_sta_deinit(mvm, sta);
1768
Johannes Berg8ca151b2013-01-24 14:25:36 +01001769 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301770 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001771 }
1772
1773 return ret;
1774}
1775
1776int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1777 struct ieee80211_vif *vif,
1778 u8 sta_id)
1779{
1780 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1781
1782 lockdep_assert_held(&mvm->mutex);
1783
Monam Agarwalc531c772014-03-24 00:05:56 +05301784 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001785 return ret;
1786}
1787
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001788int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1789 struct iwl_mvm_int_sta *sta,
Sara Sharonced19f22017-02-06 19:09:32 +02001790 u32 qmask, enum nl80211_iftype iftype,
1791 enum iwl_sta_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001792{
1793 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001794 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Sara Sharon0ae98812017-01-04 14:53:58 +02001795 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001796 return -ENOSPC;
1797 }
1798
1799 sta->tfd_queue_msk = qmask;
Sara Sharonced19f22017-02-06 19:09:32 +02001800 sta->type = type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001801
1802 /* put a non-NULL value so iterating over the stations won't stop */
1803 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1804 return 0;
1805}
1806
Sara Sharon26d6c162017-01-03 12:00:19 +02001807void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001808{
Monam Agarwalc531c772014-03-24 00:05:56 +05301809 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001810 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
Sara Sharon0ae98812017-01-04 14:53:58 +02001811 sta->sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001812}
1813
1814static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1815 struct iwl_mvm_int_sta *sta,
1816 const u8 *addr,
1817 u16 mac_id, u16 color)
1818{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001819 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001820 int ret;
1821 u32 status;
1822
1823 lockdep_assert_held(&mvm->mutex);
1824
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001825 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001826 cmd.sta_id = sta->sta_id;
1827 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1828 color));
Sara Sharonced19f22017-02-06 19:09:32 +02001829 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1830 cmd.station_type = sta->type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001831
Sara Sharonbb497012016-09-29 14:52:40 +03001832 if (!iwl_mvm_has_new_tx_api(mvm))
1833 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001834 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001835
1836 if (addr)
1837 memcpy(cmd.addr, addr, ETH_ALEN);
1838
Sara Sharon854c5702016-01-26 13:17:47 +02001839 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1840 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001841 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001842 if (ret)
1843 return ret;
1844
Sara Sharon837c4da2016-01-07 16:50:45 +02001845 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001846 case ADD_STA_SUCCESS:
1847 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1848 return 0;
1849 default:
1850 ret = -EIO;
1851 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1852 status);
1853 break;
1854 }
1855 return ret;
1856}
1857
Sara Sharonc5a719e2016-11-15 10:20:48 +02001858static void iwl_mvm_enable_aux_queue(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001859{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001860 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1861 mvm->cfg->base_params->wd_timeout :
1862 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001863
Sara Sharon310181e2017-01-17 14:27:48 +02001864 if (iwl_mvm_has_new_tx_api(mvm)) {
1865 int queue = iwl_mvm_tvqm_enable_txq(mvm, mvm->aux_queue,
1866 mvm->aux_sta.sta_id,
1867 IWL_MAX_TID_COUNT,
1868 wdg_timeout);
1869 mvm->aux_queue = queue;
1870 } else if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman28d07932015-09-01 16:36:25 +03001871 struct iwl_trans_txq_scd_cfg cfg = {
1872 .fifo = IWL_MVM_TX_FIFO_MCAST,
1873 .sta_id = mvm->aux_sta.sta_id,
1874 .tid = IWL_MAX_TID_COUNT,
1875 .aggregate = false,
1876 .frame_limit = IWL_FRAME_LIMIT,
1877 };
1878
1879 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1880 wdg_timeout);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001881 } else {
1882 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1883 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Liad Kaufman28d07932015-09-01 16:36:25 +03001884 }
Sara Sharonc5a719e2016-11-15 10:20:48 +02001885}
1886
1887int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1888{
1889 int ret;
1890
1891 lockdep_assert_held(&mvm->mutex);
1892
1893 /* Allocate aux station and assign to it the aux queue */
1894 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Sara Sharonced19f22017-02-06 19:09:32 +02001895 NL80211_IFTYPE_UNSPECIFIED,
1896 IWL_STA_AUX_ACTIVITY);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001897 if (ret)
1898 return ret;
1899
1900 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1901 if (!iwl_mvm_has_new_tx_api(mvm))
1902 iwl_mvm_enable_aux_queue(mvm);
Liad Kaufman28d07932015-09-01 16:36:25 +03001903
Johannes Berg8ca151b2013-01-24 14:25:36 +01001904 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1905 MAC_INDEX_AUX, 0);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001906 if (ret) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001907 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001908 return ret;
1909 }
1910
1911 /*
1912 * For a000 firmware and on we cannot add queue to a station unknown
1913 * to firmware so enable queue here - after the station was added
1914 */
1915 if (iwl_mvm_has_new_tx_api(mvm))
1916 iwl_mvm_enable_aux_queue(mvm);
1917
1918 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001919}
1920
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001921int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1922{
1923 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1924
1925 lockdep_assert_held(&mvm->mutex);
1926 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1927 mvmvif->id, 0);
1928}
1929
1930int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1931{
1932 int ret;
1933
1934 lockdep_assert_held(&mvm->mutex);
1935
1936 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1937 if (ret)
1938 IWL_WARN(mvm, "Failed sending remove station\n");
1939
1940 return ret;
1941}
1942
1943void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1944{
1945 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1946}
1947
Johannes Berg712b24a2014-08-04 14:14:14 +02001948void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1949{
1950 lockdep_assert_held(&mvm->mutex);
1951
1952 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1953}
1954
Johannes Berg8ca151b2013-01-24 14:25:36 +01001955/*
1956 * Send the add station command for the vif's broadcast station.
1957 * Assumes that the station was already allocated.
1958 *
1959 * @mvm: the mvm component
1960 * @vif: the interface to which the broadcast station is added
1961 * @bsta: the broadcast station to add.
1962 */
Johannes Berg013290a2014-08-04 13:38:48 +02001963int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001964{
1965 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001966 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001967 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001968 const u8 *baddr = _baddr;
Johannes Berg7daa7622017-02-24 12:02:22 +01001969 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001970 int ret;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001971 unsigned int wdg_timeout =
1972 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1973 struct iwl_trans_txq_scd_cfg cfg = {
1974 .fifo = IWL_MVM_TX_FIFO_VO,
1975 .sta_id = mvmvif->bcast_sta.sta_id,
1976 .tid = IWL_MAX_TID_COUNT,
1977 .aggregate = false,
1978 .frame_limit = IWL_FRAME_LIMIT,
1979 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001980
1981 lockdep_assert_held(&mvm->mutex);
1982
Sara Sharon310181e2017-01-17 14:27:48 +02001983 if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufman4d339982017-03-21 17:13:16 +02001984 if (vif->type == NL80211_IFTYPE_AP ||
1985 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon49f71712017-01-09 12:07:16 +02001986 queue = mvm->probe_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001987 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
Sara Sharon49f71712017-01-09 12:07:16 +02001988 queue = mvm->p2p_dev_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001989 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
Liad Kaufmande24f632015-08-04 15:19:18 +03001990 return -EINVAL;
1991
Liad Kaufmandf88c082016-11-24 15:31:00 +02001992 bsta->tfd_queue_msk |= BIT(queue);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001993
Sara Sharon310181e2017-01-17 14:27:48 +02001994 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1995 &cfg, wdg_timeout);
Liad Kaufmande24f632015-08-04 15:19:18 +03001996 }
1997
Johannes Berg5023d962013-07-31 14:07:43 +02001998 if (vif->type == NL80211_IFTYPE_ADHOC)
1999 baddr = vif->bss_conf.bssid;
2000
Sara Sharon0ae98812017-01-04 14:53:58 +02002001 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01002002 return -ENOSPC;
2003
Liad Kaufmandf88c082016-11-24 15:31:00 +02002004 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2005 mvmvif->id, mvmvif->color);
2006 if (ret)
2007 return ret;
2008
2009 /*
Sara Sharonc5a719e2016-11-15 10:20:48 +02002010 * For a000 firmware and on we cannot add queue to a station unknown
2011 * to firmware so enable queue here - after the station was added
Liad Kaufmandf88c082016-11-24 15:31:00 +02002012 */
Sara Sharon310181e2017-01-17 14:27:48 +02002013 if (iwl_mvm_has_new_tx_api(mvm)) {
Johannes Berg7daa7622017-02-24 12:02:22 +01002014 queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
2015 bsta->sta_id,
2016 IWL_MAX_TID_COUNT,
2017 wdg_timeout);
2018
Sara Sharon310181e2017-01-17 14:27:48 +02002019 if (vif->type == NL80211_IFTYPE_AP)
2020 mvm->probe_queue = queue;
2021 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2022 mvm->p2p_dev_queue = queue;
Sara Sharon310181e2017-01-17 14:27:48 +02002023 }
Liad Kaufmandf88c082016-11-24 15:31:00 +02002024
2025 return 0;
2026}
2027
2028static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2029 struct ieee80211_vif *vif)
2030{
2031 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002032 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02002033
2034 lockdep_assert_held(&mvm->mutex);
2035
Sara Sharond49394a2017-03-05 13:01:08 +02002036 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2037
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002038 switch (vif->type) {
2039 case NL80211_IFTYPE_AP:
2040 case NL80211_IFTYPE_ADHOC:
2041 queue = mvm->probe_queue;
2042 break;
2043 case NL80211_IFTYPE_P2P_DEVICE:
2044 queue = mvm->p2p_dev_queue;
2045 break;
2046 default:
2047 WARN(1, "Can't free bcast queue on vif type %d\n",
2048 vif->type);
2049 return;
Liad Kaufmandf88c082016-11-24 15:31:00 +02002050 }
2051
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002052 iwl_mvm_disable_txq(mvm, queue, vif->hw_queue[0], IWL_MAX_TID_COUNT, 0);
2053 if (iwl_mvm_has_new_tx_api(mvm))
2054 return;
2055
2056 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2057 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002058}
2059
2060/* Send the FW a request to remove the station from it's internal data
2061 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02002062int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002063{
Johannes Berg013290a2014-08-04 13:38:48 +02002064 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002065 int ret;
2066
2067 lockdep_assert_held(&mvm->mutex);
2068
Liad Kaufmandf88c082016-11-24 15:31:00 +02002069 if (iwl_mvm_is_dqa_supported(mvm))
2070 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2071
Johannes Berg013290a2014-08-04 13:38:48 +02002072 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002073 if (ret)
2074 IWL_WARN(mvm, "Failed sending remove station\n");
2075 return ret;
2076}
2077
Johannes Berg013290a2014-08-04 13:38:48 +02002078int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2079{
2080 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03002081 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02002082
2083 lockdep_assert_held(&mvm->mutex);
2084
Liad Kaufmandf88c082016-11-24 15:31:00 +02002085 if (!iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmande24f632015-08-04 15:19:18 +03002086 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02002087
Liad Kaufmande24f632015-08-04 15:19:18 +03002088 /*
2089 * The firmware defines the TFD queue mask to only be relevant
2090 * for *unicast* queues, so the multicast (CAB) queue shouldn't
Liad Kaufmandf88c082016-11-24 15:31:00 +02002091 * be included. This only happens in NL80211_IFTYPE_AP vif type,
2092 * so the next line will only have an effect there.
Liad Kaufmande24f632015-08-04 15:19:18 +03002093 */
Johannes Berg013290a2014-08-04 13:38:48 +02002094 qmask &= ~BIT(vif->cab_queue);
Liad Kaufmande24f632015-08-04 15:19:18 +03002095 }
2096
Johannes Berg013290a2014-08-04 13:38:48 +02002097 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
Sara Sharonced19f22017-02-06 19:09:32 +02002098 ieee80211_vif_type_p2p(vif),
2099 IWL_STA_GENERAL_PURPOSE);
Johannes Berg013290a2014-08-04 13:38:48 +02002100}
2101
Johannes Berg8ca151b2013-01-24 14:25:36 +01002102/* Allocate a new station entry for the broadcast station to the given vif,
2103 * and send it to the FW.
2104 * Note that each P2P mac should have its own broadcast station.
2105 *
2106 * @mvm: the mvm component
2107 * @vif: the interface to which the broadcast station is added
2108 * @bsta: the broadcast station to add. */
Luca Coelhod1973582017-06-22 16:00:25 +03002109int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002110{
2111 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02002112 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002113 int ret;
2114
2115 lockdep_assert_held(&mvm->mutex);
2116
Johannes Berg013290a2014-08-04 13:38:48 +02002117 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002118 if (ret)
2119 return ret;
2120
Johannes Berg013290a2014-08-04 13:38:48 +02002121 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002122
2123 if (ret)
2124 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02002125
Johannes Berg8ca151b2013-01-24 14:25:36 +01002126 return ret;
2127}
2128
Johannes Berg013290a2014-08-04 13:38:48 +02002129void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2130{
2131 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2132
2133 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2134}
2135
Johannes Berg8ca151b2013-01-24 14:25:36 +01002136/*
2137 * Send the FW a request to remove the station from it's internal data
2138 * structures, and in addition remove it from the local data structure.
2139 */
Luca Coelhod1973582017-06-22 16:00:25 +03002140int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002141{
2142 int ret;
2143
2144 lockdep_assert_held(&mvm->mutex);
2145
Johannes Berg013290a2014-08-04 13:38:48 +02002146 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002147
Johannes Berg013290a2014-08-04 13:38:48 +02002148 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2149
Johannes Berg8ca151b2013-01-24 14:25:36 +01002150 return ret;
2151}
2152
Sara Sharon26d6c162017-01-03 12:00:19 +02002153/*
2154 * Allocate a new station entry for the multicast station to the given vif,
2155 * and send it to the FW.
2156 * Note that each AP/GO mac should have its own multicast station.
2157 *
2158 * @mvm: the mvm component
2159 * @vif: the interface to which the multicast station is added
2160 */
2161int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2162{
2163 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2164 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2165 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2166 const u8 *maddr = _maddr;
2167 struct iwl_trans_txq_scd_cfg cfg = {
2168 .fifo = IWL_MVM_TX_FIFO_MCAST,
2169 .sta_id = msta->sta_id,
2170 .tid = IWL_MAX_TID_COUNT,
2171 .aggregate = false,
2172 .frame_limit = IWL_FRAME_LIMIT,
2173 };
2174 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2175 int ret;
2176
2177 lockdep_assert_held(&mvm->mutex);
2178
2179 if (!iwl_mvm_is_dqa_supported(mvm))
2180 return 0;
2181
Liad Kaufmanee48b722017-03-21 17:13:16 +02002182 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2183 vif->type != NL80211_IFTYPE_ADHOC))
Sara Sharon26d6c162017-01-03 12:00:19 +02002184 return -ENOTSUPP;
2185
Sara Sharonced19f22017-02-06 19:09:32 +02002186 /*
2187 * While in previous FWs we had to exclude cab queue from TFD queue
2188 * mask, now it is needed as any other queue.
2189 */
2190 if (!iwl_mvm_has_new_tx_api(mvm) &&
2191 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2192 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2193 &cfg, timeout);
2194 msta->tfd_queue_msk |= BIT(vif->cab_queue);
2195 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002196 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2197 mvmvif->id, mvmvif->color);
2198 if (ret) {
2199 iwl_mvm_dealloc_int_sta(mvm, msta);
2200 return ret;
2201 }
2202
2203 /*
2204 * Enable cab queue after the ADD_STA command is sent.
2205 * This is needed for a000 firmware which won't accept SCD_QUEUE_CFG
Sara Sharonced19f22017-02-06 19:09:32 +02002206 * command with unknown station id, and for FW that doesn't support
2207 * station API since the cab queue is not included in the
2208 * tfd_queue_mask.
Sara Sharon26d6c162017-01-03 12:00:19 +02002209 */
Sara Sharon310181e2017-01-17 14:27:48 +02002210 if (iwl_mvm_has_new_tx_api(mvm)) {
2211 int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2212 msta->sta_id,
2213 IWL_MAX_TID_COUNT,
2214 timeout);
Sara Sharone2af3fa2017-02-22 19:35:10 +02002215 mvmvif->cab_queue = queue;
Sara Sharonced19f22017-02-06 19:09:32 +02002216 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2217 IWL_UCODE_TLV_API_STA_TYPE)) {
Liad Kaufmanee48b722017-03-21 17:13:16 +02002218 /*
2219 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2220 * invalid, so make sure we use the queue we want.
2221 * Note that this is done here as we want to avoid making DQA
2222 * changes in mac80211 layer.
2223 */
2224 if (vif->type == NL80211_IFTYPE_ADHOC) {
2225 vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2226 mvmvif->cab_queue = vif->cab_queue;
2227 }
Sara Sharon310181e2017-01-17 14:27:48 +02002228 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2229 &cfg, timeout);
2230 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002231
2232 return 0;
2233}
2234
2235/*
2236 * Send the FW a request to remove the station from it's internal data
2237 * structures, and in addition remove it from the local data structure.
2238 */
2239int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2240{
2241 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2242 int ret;
2243
2244 lockdep_assert_held(&mvm->mutex);
2245
2246 if (!iwl_mvm_is_dqa_supported(mvm))
2247 return 0;
2248
Sara Sharond49394a2017-03-05 13:01:08 +02002249 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2250
Sara Sharone2af3fa2017-02-22 19:35:10 +02002251 iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
Sara Sharon26d6c162017-01-03 12:00:19 +02002252 IWL_MAX_TID_COUNT, 0);
2253
2254 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2255 if (ret)
2256 IWL_WARN(mvm, "Failed sending remove station\n");
2257
2258 return ret;
2259}
2260
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002261#define IWL_MAX_RX_BA_SESSIONS 16
2262
Sara Sharonb915c102016-03-23 16:32:02 +02002263static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02002264{
Sara Sharonb915c102016-03-23 16:32:02 +02002265 struct iwl_mvm_delba_notif notif = {
2266 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2267 .metadata.sync = 1,
2268 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02002269 };
Sara Sharonb915c102016-03-23 16:32:02 +02002270 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2271};
Sara Sharon10b2b202016-03-20 16:23:41 +02002272
Sara Sharonb915c102016-03-23 16:32:02 +02002273static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2274 struct iwl_mvm_baid_data *data)
2275{
2276 int i;
2277
2278 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2279
2280 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2281 int j;
2282 struct iwl_mvm_reorder_buffer *reorder_buf =
2283 &data->reorder_buf[i];
2284
Sara Sharon06904052016-02-28 20:28:17 +02002285 spin_lock_bh(&reorder_buf->lock);
2286 if (likely(!reorder_buf->num_stored)) {
2287 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02002288 continue;
Sara Sharon06904052016-02-28 20:28:17 +02002289 }
Sara Sharonb915c102016-03-23 16:32:02 +02002290
2291 /*
2292 * This shouldn't happen in regular DELBA since the internal
2293 * delBA notification should trigger a release of all frames in
2294 * the reorder buffer.
2295 */
2296 WARN_ON(1);
2297
2298 for (j = 0; j < reorder_buf->buf_size; j++)
2299 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02002300 /*
2301 * Prevent timer re-arm. This prevents a very far fetched case
2302 * where we timed out on the notification. There may be prior
2303 * RX frames pending in the RX queue before the notification
2304 * that might get processed between now and the actual deletion
2305 * and we would re-arm the timer although we are deleting the
2306 * reorder buffer.
2307 */
2308 reorder_buf->removed = true;
2309 spin_unlock_bh(&reorder_buf->lock);
2310 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02002311 }
2312}
2313
2314static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2315 u32 sta_id,
2316 struct iwl_mvm_baid_data *data,
2317 u16 ssn, u8 buf_size)
2318{
2319 int i;
2320
2321 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2322 struct iwl_mvm_reorder_buffer *reorder_buf =
2323 &data->reorder_buf[i];
2324 int j;
2325
2326 reorder_buf->num_stored = 0;
2327 reorder_buf->head_sn = ssn;
2328 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02002329 /* rx reorder timer */
2330 reorder_buf->reorder_timer.function =
2331 iwl_mvm_reorder_timer_expired;
2332 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
2333 init_timer(&reorder_buf->reorder_timer);
2334 spin_lock_init(&reorder_buf->lock);
2335 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02002336 reorder_buf->queue = i;
2337 reorder_buf->sta_id = sta_id;
Sara Sharon5d43eab2017-02-02 12:51:39 +02002338 reorder_buf->valid = false;
Sara Sharonb915c102016-03-23 16:32:02 +02002339 for (j = 0; j < reorder_buf->buf_size; j++)
2340 __skb_queue_head_init(&reorder_buf->entries[j]);
2341 }
Sara Sharon10b2b202016-03-20 16:23:41 +02002342}
2343
Johannes Berg8ca151b2013-01-24 14:25:36 +01002344int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02002345 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002346{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002347 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002348 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02002349 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002350 int ret;
2351 u32 status;
2352
2353 lockdep_assert_held(&mvm->mutex);
2354
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002355 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2356 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2357 return -ENOSPC;
2358 }
2359
Sara Sharon10b2b202016-03-20 16:23:41 +02002360 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2361 /*
2362 * Allocate here so if allocation fails we can bail out early
2363 * before starting the BA session in the firmware
2364 */
Sara Sharonb915c102016-03-23 16:32:02 +02002365 baid_data = kzalloc(sizeof(*baid_data) +
2366 mvm->trans->num_rx_queues *
2367 sizeof(baid_data->reorder_buf[0]),
2368 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02002369 if (!baid_data)
2370 return -ENOMEM;
2371 }
2372
Johannes Berg8ca151b2013-01-24 14:25:36 +01002373 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2374 cmd.sta_id = mvm_sta->sta_id;
2375 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002376 if (start) {
2377 cmd.add_immediate_ba_tid = (u8) tid;
2378 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02002379 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002380 } else {
2381 cmd.remove_immediate_ba_tid = (u8) tid;
2382 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002383 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2384 STA_MODIFY_REMOVE_BA_TID;
2385
2386 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002387 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2388 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002389 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002390 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02002391 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002392
Sara Sharon837c4da2016-01-07 16:50:45 +02002393 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002394 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03002395 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2396 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002397 break;
2398 case ADD_STA_IMMEDIATE_BA_FAILURE:
2399 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2400 ret = -ENOSPC;
2401 break;
2402 default:
2403 ret = -EIO;
2404 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2405 start ? "start" : "stopp", status);
2406 break;
2407 }
2408
Sara Sharon10b2b202016-03-20 16:23:41 +02002409 if (ret)
2410 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002411
Sara Sharon10b2b202016-03-20 16:23:41 +02002412 if (start) {
2413 u8 baid;
2414
2415 mvm->rx_ba_sessions++;
2416
2417 if (!iwl_mvm_has_new_rx_api(mvm))
2418 return 0;
2419
2420 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2421 ret = -EINVAL;
2422 goto out_free;
2423 }
2424 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2425 IWL_ADD_STA_BAID_SHIFT);
2426 baid_data->baid = baid;
2427 baid_data->timeout = timeout;
2428 baid_data->last_rx = jiffies;
Wei Yongjun72c240f2016-07-12 11:40:57 +00002429 setup_timer(&baid_data->session_timer,
2430 iwl_mvm_rx_agg_session_expired,
2431 (unsigned long)&mvm->baid_map[baid]);
Sara Sharon10b2b202016-03-20 16:23:41 +02002432 baid_data->mvm = mvm;
2433 baid_data->tid = tid;
2434 baid_data->sta_id = mvm_sta->sta_id;
2435
2436 mvm_sta->tid_to_baid[tid] = baid;
2437 if (timeout)
2438 mod_timer(&baid_data->session_timer,
2439 TU_TO_EXP_TIME(timeout * 2));
2440
Sara Sharonb915c102016-03-23 16:32:02 +02002441 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2442 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002443 /*
2444 * protect the BA data with RCU to cover a case where our
2445 * internal RX sync mechanism will timeout (not that it's
2446 * supposed to happen) and we will free the session data while
2447 * RX is being processed in parallel
2448 */
Sara Sharon35263a02016-06-21 12:12:10 +03002449 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2450 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002451 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2452 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002453 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002454 u8 baid = mvm_sta->tid_to_baid[tid];
2455
Sara Sharon60dec522016-06-21 14:14:08 +03002456 if (mvm->rx_ba_sessions > 0)
2457 /* check that restart flow didn't zero the counter */
2458 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002459 if (!iwl_mvm_has_new_rx_api(mvm))
2460 return 0;
2461
2462 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2463 return -EINVAL;
2464
2465 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2466 if (WARN_ON(!baid_data))
2467 return -EINVAL;
2468
2469 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002470 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002471 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002472 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2473 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002474 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002475 }
2476 return 0;
2477
2478out_free:
2479 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002480 return ret;
2481}
2482
Liad Kaufman9794c642015-08-19 17:34:28 +03002483int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2484 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002485{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002486 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002487 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002488 int ret;
2489 u32 status;
2490
2491 lockdep_assert_held(&mvm->mutex);
2492
2493 if (start) {
2494 mvm_sta->tfd_queue_msk |= BIT(queue);
2495 mvm_sta->tid_disable_agg &= ~BIT(tid);
2496 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002497 /* In DQA-mode the queue isn't removed on agg termination */
2498 if (!iwl_mvm_is_dqa_supported(mvm))
2499 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002500 mvm_sta->tid_disable_agg |= BIT(tid);
2501 }
2502
2503 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2504 cmd.sta_id = mvm_sta->sta_id;
2505 cmd.add_modify = STA_MODE_MODIFY;
Sara Sharonbb497012016-09-29 14:52:40 +03002506 if (!iwl_mvm_has_new_tx_api(mvm))
2507 cmd.modify_mask = STA_MODIFY_QUEUES;
2508 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002509 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2510 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2511
2512 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002513 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2514 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002515 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002516 if (ret)
2517 return ret;
2518
Sara Sharon837c4da2016-01-07 16:50:45 +02002519 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002520 case ADD_STA_SUCCESS:
2521 break;
2522 default:
2523 ret = -EIO;
2524 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2525 start ? "start" : "stopp", status);
2526 break;
2527 }
2528
2529 return ret;
2530}
2531
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002532const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002533 IEEE80211_AC_BE,
2534 IEEE80211_AC_BK,
2535 IEEE80211_AC_BK,
2536 IEEE80211_AC_BE,
2537 IEEE80211_AC_VI,
2538 IEEE80211_AC_VI,
2539 IEEE80211_AC_VO,
2540 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002541 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002542};
2543
Johannes Berg3e56ead2013-02-15 22:23:18 +01002544static const u8 tid_to_ucode_ac[] = {
2545 AC_BE,
2546 AC_BK,
2547 AC_BK,
2548 AC_BE,
2549 AC_VI,
2550 AC_VI,
2551 AC_VO,
2552 AC_VO,
2553};
2554
Johannes Berg8ca151b2013-01-24 14:25:36 +01002555int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2556 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2557{
Johannes Berg5b577a92013-11-14 18:20:04 +01002558 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002559 struct iwl_mvm_tid_data *tid_data;
Liad Kaufmandd321622017-04-05 16:25:11 +03002560 u16 normalized_ssn;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002561 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002562 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002563
2564 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2565 return -EINVAL;
2566
2567 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2568 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2569 mvmsta->tid_data[tid].state);
2570 return -ENXIO;
2571 }
2572
2573 lockdep_assert_held(&mvm->mutex);
2574
Arik Nemtsovb2492502014-03-13 12:21:50 +02002575 spin_lock_bh(&mvmsta->lock);
2576
2577 /* possible race condition - we entered D0i3 while starting agg */
2578 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2579 spin_unlock_bh(&mvmsta->lock);
2580 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2581 return -EIO;
2582 }
2583
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002584 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002585
Liad Kaufmancf961e12015-08-13 19:16:08 +03002586 /*
2587 * Note the possible cases:
2588 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2589 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2590 * one and mark it as reserved
2591 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2592 * non-DQA mode, since the TXQ hasn't yet been allocated
Sara Sharon34e10862017-02-23 13:15:07 +02002593 * Don't support case 3 for new TX path as it is not expected to happen
2594 * and aggregation will be offloaded soon anyway
Liad Kaufmancf961e12015-08-13 19:16:08 +03002595 */
2596 txq_id = mvmsta->tid_data[tid].txq_id;
Sara Sharon34e10862017-02-23 13:15:07 +02002597 if (iwl_mvm_has_new_tx_api(mvm)) {
2598 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2599 ret = -ENXIO;
2600 goto release_locks;
2601 }
2602 } else if (iwl_mvm_is_dqa_supported(mvm) &&
2603 unlikely(mvm->queue_info[txq_id].status ==
2604 IWL_MVM_QUEUE_SHARED)) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002605 ret = -ENXIO;
2606 IWL_DEBUG_TX_QUEUES(mvm,
2607 "Can't start tid %d agg on shared queue!\n",
2608 tid);
2609 goto release_locks;
2610 } else if (!iwl_mvm_is_dqa_supported(mvm) ||
Liad Kaufmancf961e12015-08-13 19:16:08 +03002611 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002612 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2613 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03002614 mvm->last_agg_queue);
2615 if (txq_id < 0) {
2616 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002617 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2618 goto release_locks;
2619 }
Sara Sharon01796ff2016-11-16 17:04:36 +02002620 /*
2621 * TXQ shouldn't be in inactive mode for non-DQA, so getting
2622 * an inactive queue from iwl_mvm_find_free_queue() is
2623 * certainly a bug
2624 */
2625 WARN_ON(mvm->queue_info[txq_id].status ==
2626 IWL_MVM_QUEUE_INACTIVE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002627
2628 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2629 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002630 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002631
2632 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002633
Liad Kaufmancf961e12015-08-13 19:16:08 +03002634 IWL_DEBUG_TX_QUEUES(mvm,
2635 "AGG for tid %d will be on queue #%d\n",
2636 tid, txq_id);
2637
Johannes Berg8ca151b2013-01-24 14:25:36 +01002638 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002639 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002640 tid_data->txq_id = txq_id;
2641 *ssn = tid_data->ssn;
2642
2643 IWL_DEBUG_TX_QUEUES(mvm,
2644 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2645 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2646 tid_data->next_reclaimed);
2647
Liad Kaufmandd321622017-04-05 16:25:11 +03002648 /*
2649 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
2650 * to align the wrap around of ssn so we compare relevant values.
2651 */
2652 normalized_ssn = tid_data->ssn;
2653 if (mvm->trans->cfg->gen2)
2654 normalized_ssn &= 0xff;
2655
2656 if (normalized_ssn == tid_data->next_reclaimed) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002657 tid_data->state = IWL_AGG_STARTING;
2658 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2659 } else {
2660 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2661 }
2662
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002663 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002664 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002665
2666release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002667 spin_unlock(&mvm->queue_info_lock);
2668out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002669 spin_unlock_bh(&mvmsta->lock);
2670
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002671 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002672}
2673
2674int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002675 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2676 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002677{
Johannes Berg5b577a92013-11-14 18:20:04 +01002678 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002679 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002680 unsigned int wdg_timeout =
2681 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002682 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002683 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002684 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002685 u16 ssn;
2686
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002687 struct iwl_trans_txq_scd_cfg cfg = {
2688 .sta_id = mvmsta->sta_id,
2689 .tid = tid,
2690 .frame_limit = buf_size,
2691 .aggregate = true,
2692 };
2693
Eyal Shapiraefed6642014-09-14 15:58:53 +03002694 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2695 != IWL_MAX_TID_COUNT);
2696
Liad Kaufmana58bb462017-05-28 14:20:04 +03002697 if (!mvm->trans->cfg->gen2)
2698 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2699 else
2700 buf_size = min_t(int, buf_size,
2701 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002702
2703 spin_lock_bh(&mvmsta->lock);
2704 ssn = tid_data->ssn;
2705 queue = tid_data->txq_id;
2706 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002707 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002708 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002709 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002710 spin_unlock_bh(&mvmsta->lock);
2711
Sara Sharon34e10862017-02-23 13:15:07 +02002712 if (iwl_mvm_has_new_tx_api(mvm)) {
2713 /*
2714 * If no queue iwl_mvm_sta_tx_agg_start() would have failed so
2715 * no need to check queue's status
2716 */
2717 if (buf_size < mvmsta->max_agg_bufsize)
2718 return -ENOTSUPP;
2719
2720 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2721 if (ret)
2722 return -EIO;
2723 goto out;
2724 }
2725
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002726 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002727
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002728 spin_lock_bh(&mvm->queue_info_lock);
2729 queue_status = mvm->queue_info[queue].status;
2730 spin_unlock_bh(&mvm->queue_info_lock);
2731
Liad Kaufmancf961e12015-08-13 19:16:08 +03002732 /* In DQA mode, the existing queue might need to be reconfigured */
2733 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002734 /* Maybe there is no need to even alloc a queue... */
2735 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2736 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002737
2738 /*
2739 * Only reconfig the SCD for the queue if the window size has
2740 * changed from current (become smaller)
2741 */
2742 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2743 /*
2744 * If reconfiguring an existing queue, it first must be
2745 * drained
2746 */
Sara Sharona1a57872017-03-05 11:38:58 +02002747 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2748 BIT(queue));
Liad Kaufmancf961e12015-08-13 19:16:08 +03002749 if (ret) {
2750 IWL_ERR(mvm,
2751 "Error draining queue before reconfig\n");
2752 return ret;
2753 }
2754
2755 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2756 mvmsta->sta_id, tid,
2757 buf_size, ssn);
2758 if (ret) {
2759 IWL_ERR(mvm,
2760 "Error reconfiguring TXQ #%d\n", queue);
2761 return ret;
2762 }
2763 }
2764 }
2765
2766 if (alloc_queue)
2767 iwl_mvm_enable_txq(mvm, queue,
2768 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2769 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002770
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002771 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2772 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2773 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2774 if (ret)
2775 return -EIO;
2776 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002777
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002778 /* No need to mark as reserved */
2779 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002780 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002781 spin_unlock_bh(&mvm->queue_info_lock);
2782
Sara Sharon34e10862017-02-23 13:15:07 +02002783out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002784 /*
2785 * Even though in theory the peer could have different
2786 * aggregation reorder buffer sizes for different sessions,
2787 * our ucode doesn't allow for that and has a global limit
2788 * for each station. Therefore, use the minimum of all the
2789 * aggregation sessions and our default value.
2790 */
2791 mvmsta->max_agg_bufsize =
2792 min(mvmsta->max_agg_bufsize, buf_size);
2793 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2794
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002795 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2796 sta->addr, tid);
2797
Eyal Shapira9e680942013-11-09 00:16:16 +02002798 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002799}
2800
Sara Sharon34e10862017-02-23 13:15:07 +02002801static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2802 struct iwl_mvm_sta *mvmsta,
2803 u16 txq_id)
2804{
2805 if (iwl_mvm_has_new_tx_api(mvm))
2806 return;
2807
2808 spin_lock_bh(&mvm->queue_info_lock);
2809 /*
2810 * The TXQ is marked as reserved only if no traffic came through yet
2811 * This means no traffic has been sent on this TID (agg'd or not), so
2812 * we no longer have use for the queue. Since it hasn't even been
2813 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2814 * free.
2815 */
2816 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2817 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2818
2819 spin_unlock_bh(&mvm->queue_info_lock);
2820}
2821
Johannes Berg8ca151b2013-01-24 14:25:36 +01002822int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2823 struct ieee80211_sta *sta, u16 tid)
2824{
Johannes Berg5b577a92013-11-14 18:20:04 +01002825 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002826 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2827 u16 txq_id;
2828 int err;
2829
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002830 /*
2831 * If mac80211 is cleaning its state, then say that we finished since
2832 * our state has been cleared anyway.
2833 */
2834 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2835 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2836 return 0;
2837 }
2838
Johannes Berg8ca151b2013-01-24 14:25:36 +01002839 spin_lock_bh(&mvmsta->lock);
2840
2841 txq_id = tid_data->txq_id;
2842
2843 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2844 mvmsta->sta_id, tid, txq_id, tid_data->state);
2845
Eyal Shapiraefed6642014-09-14 15:58:53 +03002846 mvmsta->agg_tids &= ~BIT(tid);
2847
Sara Sharon34e10862017-02-23 13:15:07 +02002848 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002849
Johannes Berg8ca151b2013-01-24 14:25:36 +01002850 switch (tid_data->state) {
2851 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002852 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002853
2854 IWL_DEBUG_TX_QUEUES(mvm,
2855 "ssn = %d, next_recl = %d\n",
2856 tid_data->ssn, tid_data->next_reclaimed);
2857
Liad Kaufman664e9682017-04-05 10:35:18 +03002858 /*
2859 * There are still packets for this RA / TID in the HW.
2860 * Not relevant for DQA mode, since there is no need to disable
2861 * the queue.
2862 */
2863 if (!iwl_mvm_is_dqa_supported(mvm) &&
2864 tid_data->ssn != tid_data->next_reclaimed) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002865 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2866 err = 0;
2867 break;
2868 }
2869
2870 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002871 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002872 spin_unlock_bh(&mvmsta->lock);
2873
2874 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2875
2876 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2877
Liad Kaufmancf961e12015-08-13 19:16:08 +03002878 if (!iwl_mvm_is_dqa_supported(mvm)) {
2879 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2880
2881 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2882 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002883 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002884 case IWL_AGG_STARTING:
2885 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2886 /*
2887 * The agg session has been stopped before it was set up. This
2888 * can happen when the AddBA timer times out for example.
2889 */
2890
2891 /* No barriers since we are under mutex */
2892 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002893
2894 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2895 tid_data->state = IWL_AGG_OFF;
2896 err = 0;
2897 break;
2898 default:
2899 IWL_ERR(mvm,
2900 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2901 mvmsta->sta_id, tid, tid_data->state);
2902 IWL_ERR(mvm,
2903 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2904 err = -EINVAL;
2905 }
2906
2907 spin_unlock_bh(&mvmsta->lock);
2908
2909 return err;
2910}
2911
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002912int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2913 struct ieee80211_sta *sta, u16 tid)
2914{
Johannes Berg5b577a92013-11-14 18:20:04 +01002915 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002916 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2917 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002918 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002919
2920 /*
2921 * First set the agg state to OFF to avoid calling
2922 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2923 */
2924 spin_lock_bh(&mvmsta->lock);
2925 txq_id = tid_data->txq_id;
2926 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2927 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002928 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002929 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002930 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002931 spin_unlock_bh(&mvmsta->lock);
2932
Sara Sharon34e10862017-02-23 13:15:07 +02002933 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002934
Johannes Bergb6658ff2013-07-24 13:55:51 +02002935 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002936 iwl_mvm_drain_sta(mvm, mvmsta, true);
Sara Sharond6d517b2017-03-06 10:16:11 +02002937
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002938 if (iwl_mvm_has_new_tx_api(mvm)) {
2939 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
2940 BIT(tid), 0))
2941 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002942 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002943 } else {
2944 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2945 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002946 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002947 }
Sara Sharond6d517b2017-03-06 10:16:11 +02002948
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002949 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002950
Johannes Bergf7f89e72014-08-05 15:24:44 +02002951 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2952
Liad Kaufmancf961e12015-08-13 19:16:08 +03002953 if (!iwl_mvm_is_dqa_supported(mvm)) {
2954 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2955
2956 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2957 tid, 0);
2958 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002959 }
2960
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002961 return 0;
2962}
2963
Johannes Berg8ca151b2013-01-24 14:25:36 +01002964static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2965{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002966 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002967
2968 lockdep_assert_held(&mvm->mutex);
2969
Johannes Berg2dc2a152015-06-16 17:09:18 +02002970 /* Pick the unused key offset with the highest 'deleted'
2971 * counter. Every time a key is deleted, all the counters
2972 * are incremented and the one that was just deleted is
2973 * reset to zero. Thus, the highest counter is the one
2974 * that was deleted longest ago. Pick that one.
2975 */
2976 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2977 if (test_bit(i, mvm->fw_key_table))
2978 continue;
2979 if (mvm->fw_key_deleted[i] > max) {
2980 max = mvm->fw_key_deleted[i];
2981 max_offs = i;
2982 }
2983 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002984
Johannes Berg2dc2a152015-06-16 17:09:18 +02002985 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002986 return STA_KEY_IDX_INVALID;
2987
Johannes Berg2dc2a152015-06-16 17:09:18 +02002988 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002989}
2990
Johannes Berg5f7a1842015-12-11 09:36:10 +01002991static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2992 struct ieee80211_vif *vif,
2993 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002994{
Johannes Berg5b530e92014-12-23 16:00:17 +01002995 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002996
Johannes Berg5f7a1842015-12-11 09:36:10 +01002997 if (sta)
2998 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002999
3000 /*
3001 * The device expects GTKs for station interfaces to be
3002 * installed as GTKs for the AP station. If we have no
3003 * station ID, then use AP's station ID.
3004 */
3005 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02003006 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Avri Altman9513c5e2015-10-19 16:29:11 +02003007 u8 sta_id = mvmvif->ap_sta_id;
3008
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03003009 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3010 lockdep_is_held(&mvm->mutex));
3011
Avri Altman9513c5e2015-10-19 16:29:11 +02003012 /*
3013 * It is possible that the 'sta' parameter is NULL,
3014 * for example when a GTK is removed - the sta_id will then
3015 * be the AP ID, and no station was passed by mac80211.
3016 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03003017 if (IS_ERR_OR_NULL(sta))
3018 return NULL;
3019
3020 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02003021 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003022
Johannes Berg5f7a1842015-12-11 09:36:10 +01003023 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003024}
3025
3026static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
David Spinadel85aeb582017-03-30 19:43:53 +03003027 u32 sta_id,
Sara Sharon45c458b2016-11-09 15:43:26 +02003028 struct ieee80211_key_conf *key, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003029 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3030 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003031{
Sara Sharon45c458b2016-11-09 15:43:26 +02003032 union {
3033 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3034 struct iwl_mvm_add_sta_key_cmd cmd;
3035 } u = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003036 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01003037 int ret;
3038 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003039 u16 keyidx;
Sara Sharon45c458b2016-11-09 15:43:26 +02003040 u64 pn = 0;
3041 int i, size;
3042 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3043 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003044
David Spinadel85aeb582017-03-30 19:43:53 +03003045 if (sta_id == IWL_MVM_INVALID_STA)
3046 return -EINVAL;
3047
Sara Sharon45c458b2016-11-09 15:43:26 +02003048 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
Johannes Berg8ca151b2013-01-24 14:25:36 +01003049 STA_KEY_FLG_KEYID_MSK;
3050 key_flags = cpu_to_le16(keyidx);
3051 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3052
Sara Sharon45c458b2016-11-09 15:43:26 +02003053 switch (key->cipher) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003054 case WLAN_CIPHER_SUITE_TKIP:
3055 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003056 if (new_api) {
3057 memcpy((void *)&u.cmd.tx_mic_key,
3058 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3059 IWL_MIC_KEY_SIZE);
3060
3061 memcpy((void *)&u.cmd.rx_mic_key,
3062 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3063 IWL_MIC_KEY_SIZE);
3064 pn = atomic64_read(&key->tx_pn);
3065
3066 } else {
3067 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3068 for (i = 0; i < 5; i++)
3069 u.cmd_v1.tkip_rx_ttak[i] =
3070 cpu_to_le16(tkip_p1k[i]);
3071 }
3072 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003073 break;
3074 case WLAN_CIPHER_SUITE_CCMP:
3075 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Sara Sharon45c458b2016-11-09 15:43:26 +02003076 memcpy(u.cmd.common.key, key->key, key->keylen);
3077 if (new_api)
3078 pn = atomic64_read(&key->tx_pn);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003079 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01003080 case WLAN_CIPHER_SUITE_WEP104:
3081 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05003082 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01003083 case WLAN_CIPHER_SUITE_WEP40:
3084 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003085 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
Johannes Bergba3943b2014-11-12 23:54:48 +01003086 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03003087 case WLAN_CIPHER_SUITE_GCMP_256:
3088 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3089 /* fall through */
3090 case WLAN_CIPHER_SUITE_GCMP:
3091 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003092 memcpy(u.cmd.common.key, key->key, key->keylen);
3093 if (new_api)
3094 pn = atomic64_read(&key->tx_pn);
Ayala Beker2a53d162016-04-07 16:21:57 +03003095 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003096 default:
Max Stepanove36e5432013-08-27 19:56:13 +03003097 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
Sara Sharon45c458b2016-11-09 15:43:26 +02003098 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003099 }
3100
Johannes Bergba3943b2014-11-12 23:54:48 +01003101 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003102 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3103
Sara Sharon45c458b2016-11-09 15:43:26 +02003104 u.cmd.common.key_offset = key_offset;
3105 u.cmd.common.key_flags = key_flags;
David Spinadel85aeb582017-03-30 19:43:53 +03003106 u.cmd.common.sta_id = sta_id;
Sara Sharon45c458b2016-11-09 15:43:26 +02003107
3108 if (new_api) {
3109 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3110 size = sizeof(u.cmd);
3111 } else {
3112 size = sizeof(u.cmd_v1);
3113 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003114
3115 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003116 if (cmd_flags & CMD_ASYNC)
Sara Sharon45c458b2016-11-09 15:43:26 +02003117 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3118 &u.cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003119 else
Sara Sharon45c458b2016-11-09 15:43:26 +02003120 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3121 &u.cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003122
3123 switch (status) {
3124 case ADD_STA_SUCCESS:
3125 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3126 break;
3127 default:
3128 ret = -EIO;
3129 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3130 break;
3131 }
3132
3133 return ret;
3134}
3135
3136static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3137 struct ieee80211_key_conf *keyconf,
3138 u8 sta_id, bool remove_key)
3139{
3140 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3141
3142 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03003143 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3144 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3145 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3146 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3147 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3148 return -EINVAL;
3149
3150 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3151 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01003152 return -EINVAL;
3153
3154 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3155 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3156
3157 if (remove_key) {
3158 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3159 } else {
3160 struct ieee80211_key_seq seq;
3161 const u8 *pn;
3162
Ayala Bekeraa950522016-06-01 00:28:09 +03003163 switch (keyconf->cipher) {
3164 case WLAN_CIPHER_SUITE_AES_CMAC:
3165 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3166 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03003167 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3168 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3169 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3170 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03003171 default:
3172 return -EINVAL;
3173 }
3174
Ayala Beker8e160ab2016-04-11 11:37:38 +03003175 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3176 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3177 igtk_cmd.ctrl_flags |=
3178 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003179 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3180 pn = seq.aes_cmac.pn;
3181 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3182 ((u64) pn[4] << 8) |
3183 ((u64) pn[3] << 16) |
3184 ((u64) pn[2] << 24) |
3185 ((u64) pn[1] << 32) |
3186 ((u64) pn[0] << 40));
3187 }
3188
3189 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3190 remove_key ? "removing" : "installing",
3191 igtk_cmd.sta_id);
3192
Ayala Beker8e160ab2016-04-11 11:37:38 +03003193 if (!iwl_mvm_has_new_rx_api(mvm)) {
3194 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3195 .ctrl_flags = igtk_cmd.ctrl_flags,
3196 .key_id = igtk_cmd.key_id,
3197 .sta_id = igtk_cmd.sta_id,
3198 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3199 };
3200
3201 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3202 ARRAY_SIZE(igtk_cmd_v1.igtk));
3203 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3204 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3205 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003206 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003207 sizeof(igtk_cmd), &igtk_cmd);
3208}
3209
3210
3211static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3212 struct ieee80211_vif *vif,
3213 struct ieee80211_sta *sta)
3214{
Johannes Berg5b530e92014-12-23 16:00:17 +01003215 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003216
3217 if (sta)
3218 return sta->addr;
3219
3220 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02003221 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003222 u8 sta_id = mvmvif->ap_sta_id;
3223 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3224 lockdep_is_held(&mvm->mutex));
3225 return sta->addr;
3226 }
3227
3228
3229 return NULL;
3230}
3231
Johannes Berg2f6319d2014-11-12 23:39:56 +01003232static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3233 struct ieee80211_vif *vif,
3234 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01003235 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003236 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01003237 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003238{
Johannes Berg8ca151b2013-01-24 14:25:36 +01003239 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003240 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003241 struct ieee80211_key_seq seq;
3242 u16 p1k[5];
David Spinadel85aeb582017-03-30 19:43:53 +03003243 u32 sta_id;
3244
3245 if (sta) {
3246 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3247
3248 sta_id = mvm_sta->sta_id;
3249 } else if (vif->type == NL80211_IFTYPE_AP &&
3250 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3251 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3252
3253 sta_id = mvmvif->mcast_sta.sta_id;
3254 } else {
3255 IWL_ERR(mvm, "Failed to find station id\n");
3256 return -EINVAL;
3257 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003258
Johannes Berg8ca151b2013-01-24 14:25:36 +01003259 switch (keyconf->cipher) {
3260 case WLAN_CIPHER_SUITE_TKIP:
David Spinadel85aeb582017-03-30 19:43:53 +03003261 if (vif->type == NL80211_IFTYPE_AP) {
3262 ret = -EINVAL;
3263 break;
3264 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003265 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3266 /* get phase 1 key from mac80211 */
3267 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3268 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
David Spinadel85aeb582017-03-30 19:43:53 +03003269 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003270 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003271 break;
3272 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01003273 case WLAN_CIPHER_SUITE_WEP40:
3274 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03003275 case WLAN_CIPHER_SUITE_GCMP:
3276 case WLAN_CIPHER_SUITE_GCMP_256:
David Spinadel85aeb582017-03-30 19:43:53 +03003277 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003278 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003279 break;
3280 default:
David Spinadel85aeb582017-03-30 19:43:53 +03003281 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003282 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003283 }
3284
Johannes Berg8ca151b2013-01-24 14:25:36 +01003285 return ret;
3286}
3287
Johannes Berg2f6319d2014-11-12 23:39:56 +01003288static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01003289 struct ieee80211_key_conf *keyconf,
3290 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003291{
Sara Sharon45c458b2016-11-09 15:43:26 +02003292 union {
3293 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3294 struct iwl_mvm_add_sta_key_cmd cmd;
3295 } u = {};
3296 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3297 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003298 __le16 key_flags;
Sara Sharon45c458b2016-11-09 15:43:26 +02003299 int ret, size;
Johannes Berg79920742014-11-03 15:43:04 +01003300 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003301
David Spinadel85aeb582017-03-30 19:43:53 +03003302 if (sta_id == IWL_MVM_INVALID_STA)
3303 return -EINVAL;
3304
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02003305 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3306 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003307 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3308 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3309
Johannes Bergba3943b2014-11-12 23:54:48 +01003310 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003311 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3312
Sara Sharon45c458b2016-11-09 15:43:26 +02003313 /*
3314 * The fields assigned here are in the same location at the start
3315 * of the command, so we can do this union trick.
3316 */
3317 u.cmd.common.key_flags = key_flags;
3318 u.cmd.common.key_offset = keyconf->hw_key_idx;
3319 u.cmd.common.sta_id = sta_id;
3320
3321 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003322
Johannes Berg8ca151b2013-01-24 14:25:36 +01003323 status = ADD_STA_SUCCESS;
Sara Sharon45c458b2016-11-09 15:43:26 +02003324 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3325 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003326
3327 switch (status) {
3328 case ADD_STA_SUCCESS:
3329 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3330 break;
3331 default:
3332 ret = -EIO;
3333 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3334 break;
3335 }
3336
3337 return ret;
3338}
3339
Johannes Berg2f6319d2014-11-12 23:39:56 +01003340int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3341 struct ieee80211_vif *vif,
3342 struct ieee80211_sta *sta,
3343 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003344 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003345{
Johannes Bergba3943b2014-11-12 23:54:48 +01003346 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003347 struct iwl_mvm_sta *mvm_sta;
David Spinadel85aeb582017-03-30 19:43:53 +03003348 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003349 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03003350 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01003351
3352 lockdep_assert_held(&mvm->mutex);
3353
David Spinadel85aeb582017-03-30 19:43:53 +03003354 if (vif->type != NL80211_IFTYPE_AP ||
3355 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3356 /* Get the station id from the mvm local station table */
3357 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3358 if (!mvm_sta) {
3359 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01003360 return -EINVAL;
3361 }
David Spinadel85aeb582017-03-30 19:43:53 +03003362 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003363
David Spinadel85aeb582017-03-30 19:43:53 +03003364 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3365 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3366 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3367 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id,
3368 false);
3369 goto end;
3370 }
3371
3372 /*
3373 * It is possible that the 'sta' parameter is NULL, and thus
3374 * there is a need to retrieve the sta from the local station
3375 * table.
3376 */
3377 if (!sta) {
3378 sta = rcu_dereference_protected(
3379 mvm->fw_id_to_mac_id[sta_id],
3380 lockdep_is_held(&mvm->mutex));
3381 if (IS_ERR_OR_NULL(sta)) {
3382 IWL_ERR(mvm, "Invalid station id\n");
3383 return -EINVAL;
3384 }
3385 }
3386
3387 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3388 return -EINVAL;
3389 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003390
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003391 /* If the key_offset is not pre-assigned, we need to find a
3392 * new offset to use. In normal cases, the offset is not
3393 * pre-assigned, but during HW_RESTART we want to reuse the
3394 * same indices, so we pass them when this function is called.
3395 *
3396 * In D3 entry, we need to hardcoded the indices (because the
3397 * firmware hardcodes the PTK offset to 0). In this case, we
3398 * need to make sure we don't overwrite the hw_key_idx in the
3399 * keyconf structure, because otherwise we cannot configure
3400 * the original ones back when resuming.
3401 */
3402 if (key_offset == STA_KEY_IDX_INVALID) {
3403 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3404 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003405 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003406 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003407 }
3408
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003409 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003410 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01003411 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003412
3413 /*
3414 * For WEP, the same key is used for multicast and unicast. Upload it
3415 * again, using the same key offset, and now pointing the other one
3416 * to the same key slot (offset).
3417 * If this fails, remove the original as well.
3418 */
David Spinadel85aeb582017-03-30 19:43:53 +03003419 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3420 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3421 sta) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003422 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3423 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01003424 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01003425 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003426 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003427 }
3428 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003429
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003430 __set_bit(key_offset, mvm->fw_key_table);
3431
Johannes Berg2f6319d2014-11-12 23:39:56 +01003432end:
3433 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3434 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03003435 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01003436 return ret;
3437}
3438
3439int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3440 struct ieee80211_vif *vif,
3441 struct ieee80211_sta *sta,
3442 struct ieee80211_key_conf *keyconf)
3443{
Johannes Bergba3943b2014-11-12 23:54:48 +01003444 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003445 struct iwl_mvm_sta *mvm_sta;
Sara Sharon0ae98812017-01-04 14:53:58 +02003446 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2dc2a152015-06-16 17:09:18 +02003447 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003448
3449 lockdep_assert_held(&mvm->mutex);
3450
Johannes Berg5f7a1842015-12-11 09:36:10 +01003451 /* Get the station from the mvm local station table */
3452 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Luca Coelho71793b7d2017-03-30 12:04:47 +03003453 if (mvm_sta)
3454 sta_id = mvm_sta->sta_id;
David Spinadel85aeb582017-03-30 19:43:53 +03003455 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3456 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3457
Johannes Berg2f6319d2014-11-12 23:39:56 +01003458
3459 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3460 keyconf->keyidx, sta_id);
3461
Luca Coelho71793b7d2017-03-30 12:04:47 +03003462 if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3463 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3464 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
Johannes Berg2f6319d2014-11-12 23:39:56 +01003465 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3466
3467 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3468 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3469 keyconf->hw_key_idx);
3470 return -ENOENT;
3471 }
3472
Johannes Berg2dc2a152015-06-16 17:09:18 +02003473 /* track which key was deleted last */
3474 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3475 if (mvm->fw_key_deleted[i] < U8_MAX)
3476 mvm->fw_key_deleted[i]++;
3477 }
3478 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3479
David Spinadel85aeb582017-03-30 19:43:53 +03003480 if (sta && !mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003481 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3482 return 0;
3483 }
3484
Johannes Bergba3943b2014-11-12 23:54:48 +01003485 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3486 if (ret)
3487 return ret;
3488
3489 /* delete WEP key twice to get rid of (now useless) offset */
3490 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3491 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3492 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3493
3494 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003495}
3496
Johannes Berg8ca151b2013-01-24 14:25:36 +01003497void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3498 struct ieee80211_vif *vif,
3499 struct ieee80211_key_conf *keyconf,
3500 struct ieee80211_sta *sta, u32 iv32,
3501 u16 *phase1key)
3502{
Beni Levc3eb5362013-02-06 17:22:18 +02003503 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01003504 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003505
Beni Levc3eb5362013-02-06 17:22:18 +02003506 rcu_read_lock();
3507
Johannes Berg5f7a1842015-12-11 09:36:10 +01003508 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3509 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003510 goto unlock;
David Spinadel85aeb582017-03-30 19:43:53 +03003511 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003512 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003513
3514 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02003515 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01003516}
3517
Johannes Berg9cc40712013-02-15 22:47:48 +01003518void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3519 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003520{
Johannes Berg5b577a92013-11-14 18:20:04 +01003521 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003522 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003523 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003524 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03003525 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01003526 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003527 };
3528 int ret;
3529
Sara Sharon854c5702016-01-26 13:17:47 +02003530 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3531 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003532 if (ret)
3533 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3534}
3535
Johannes Berg9cc40712013-02-15 22:47:48 +01003536void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3537 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003538 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003539 u16 cnt, u16 tids, bool more_data,
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003540 bool single_sta_queue)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003541{
Johannes Berg5b577a92013-11-14 18:20:04 +01003542 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003543 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003544 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003545 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003546 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3547 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003548 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003549 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003550 int tid, ret;
3551 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003552
Johannes Berg3e56ead2013-02-15 22:23:18 +01003553 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3554 * Note that this field is reserved and unused by firmware not
3555 * supporting GO uAPSD, so it's safe to always do this.
3556 */
3557 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3558 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3559
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003560 /* If we're releasing frames from aggregation or dqa queues then check
3561 * if all the queues that we're releasing frames from, combined, have:
Johannes Berg3e56ead2013-02-15 22:23:18 +01003562 * - more frames than the service period, in which case more_data
3563 * needs to be set
3564 * - fewer than 'cnt' frames, in which case we need to adjust the
3565 * firmware command (but do that unconditionally)
3566 */
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003567 if (single_sta_queue) {
Johannes Berg3e56ead2013-02-15 22:23:18 +01003568 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003569 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003570
3571 spin_lock_bh(&mvmsta->lock);
3572 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3573 struct iwl_mvm_tid_data *tid_data;
3574 u16 n_queued;
3575
3576 tid_data = &mvmsta->tid_data[tid];
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003577 if (WARN(!iwl_mvm_is_dqa_supported(mvm) &&
3578 tid_data->state != IWL_AGG_ON &&
Johannes Berg3e56ead2013-02-15 22:23:18 +01003579 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3580 "TID %d state is %d\n",
3581 tid, tid_data->state)) {
3582 spin_unlock_bh(&mvmsta->lock);
3583 ieee80211_sta_eosp(sta);
3584 return;
3585 }
3586
Liad Kaufmandd321622017-04-05 16:25:11 +03003587 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003588 if (n_queued > remaining) {
3589 more_data = true;
3590 remaining = 0;
3591 break;
3592 }
3593 remaining -= n_queued;
3594 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003595 sleep_tx_count = cnt - remaining;
3596 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3597 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003598 spin_unlock_bh(&mvmsta->lock);
3599
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003600 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003601 if (WARN_ON(cnt - remaining == 0)) {
3602 ieee80211_sta_eosp(sta);
3603 return;
3604 }
3605 }
3606
3607 /* Note: this is ignored by firmware not supporting GO uAPSD */
3608 if (more_data)
Sara Sharonced19f22017-02-06 19:09:32 +02003609 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003610
3611 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3612 mvmsta->next_status_eosp = true;
Sara Sharonced19f22017-02-06 19:09:32 +02003613 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003614 } else {
Sara Sharonced19f22017-02-06 19:09:32 +02003615 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003616 }
3617
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003618 /* block the Tx queues until the FW updated the sleep Tx count */
3619 iwl_trans_block_txq_ptrs(mvm->trans, true);
3620
3621 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3622 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003623 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003624 if (ret)
3625 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3626}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003627
Johannes Berg04168412015-06-23 21:22:09 +02003628void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3629 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003630{
3631 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3632 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3633 struct ieee80211_sta *sta;
3634 u32 sta_id = le32_to_cpu(notif->sta_id);
3635
3636 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003637 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003638
3639 rcu_read_lock();
3640 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3641 if (!IS_ERR_OR_NULL(sta))
3642 ieee80211_sta_eosp(sta);
3643 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003644}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003645
3646void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3647 struct iwl_mvm_sta *mvmsta, bool disable)
3648{
3649 struct iwl_mvm_add_sta_cmd cmd = {
3650 .add_modify = STA_MODE_MODIFY,
3651 .sta_id = mvmsta->sta_id,
3652 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3653 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3654 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3655 };
3656 int ret;
3657
Sara Sharon854c5702016-01-26 13:17:47 +02003658 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3659 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003660 if (ret)
3661 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3662}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003663
3664void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3665 struct ieee80211_sta *sta,
3666 bool disable)
3667{
3668 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3669
3670 spin_lock_bh(&mvm_sta->lock);
3671
3672 if (mvm_sta->disable_tx == disable) {
3673 spin_unlock_bh(&mvm_sta->lock);
3674 return;
3675 }
3676
3677 mvm_sta->disable_tx = disable;
3678
3679 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03003680 * Tell mac80211 to start/stop queuing tx for this station,
3681 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003682 * for this station.
3683 */
3684 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3685 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3686
3687 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3688
3689 spin_unlock_bh(&mvm_sta->lock);
3690}
3691
Sara Sharonced19f22017-02-06 19:09:32 +02003692static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3693 struct iwl_mvm_vif *mvmvif,
3694 struct iwl_mvm_int_sta *sta,
3695 bool disable)
3696{
3697 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3698 struct iwl_mvm_add_sta_cmd cmd = {
3699 .add_modify = STA_MODE_MODIFY,
3700 .sta_id = sta->sta_id,
3701 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3702 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3703 .mac_id_n_color = cpu_to_le32(id),
3704 };
3705 int ret;
3706
3707 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3708 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3709 if (ret)
3710 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3711}
3712
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003713void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3714 struct iwl_mvm_vif *mvmvif,
3715 bool disable)
3716{
3717 struct ieee80211_sta *sta;
3718 struct iwl_mvm_sta *mvm_sta;
3719 int i;
3720
3721 lockdep_assert_held(&mvm->mutex);
3722
3723 /* Block/unblock all the stations of the given mvmvif */
Sara Sharon0ae98812017-01-04 14:53:58 +02003724 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003725 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3726 lockdep_is_held(&mvm->mutex));
3727 if (IS_ERR_OR_NULL(sta))
3728 continue;
3729
3730 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3731 if (mvm_sta->mac_id_n_color !=
3732 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3733 continue;
3734
3735 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3736 }
Sara Sharonced19f22017-02-06 19:09:32 +02003737
3738 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3739 return;
3740
3741 /* Need to block/unblock also multicast station */
3742 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3743 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3744 &mvmvif->mcast_sta, disable);
3745
3746 /*
3747 * Only unblock the broadcast station (FW blocks it for immediate
3748 * quiet, not the driver)
3749 */
3750 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3751 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3752 &mvmvif->bcast_sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003753}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003754
3755void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3756{
3757 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3758 struct iwl_mvm_sta *mvmsta;
3759
3760 rcu_read_lock();
3761
3762 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3763
3764 if (!WARN_ON(!mvmsta))
3765 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3766
3767 rcu_read_unlock();
3768}
Liad Kaufmandd321622017-04-05 16:25:11 +03003769
3770u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3771{
3772 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3773
3774 /*
3775 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
3776 * to align the wrap around of ssn so we compare relevant values.
3777 */
3778 if (mvm->trans->cfg->gen2)
3779 sn &= 0xff;
3780
3781 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3782}