blob: 3dad87b79cf0ae565554f73cd5f802a392e55e1b [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],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300318 iwl_mvm_ac_to_tx_fifo[ac], 0,
319 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;
401 s8 sta_id;
402 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;
737 mvmsta->tfd_queue_msk |= BIT(queue);
738 spin_unlock_bh(&mvmsta->lock);
739
Sara Sharon310181e2017-01-17 14:27:48 +0200740 return 0;
741}
742
Liad Kaufman24afba72015-07-28 18:56:08 +0300743static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
744 struct ieee80211_sta *sta, u8 ac, int tid,
745 struct ieee80211_hdr *hdr)
746{
747 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
748 struct iwl_trans_txq_scd_cfg cfg = {
749 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
750 .sta_id = mvmsta->sta_id,
751 .tid = tid,
752 .frame_limit = IWL_FRAME_LIMIT,
753 };
754 unsigned int wdg_timeout =
755 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
756 u8 mac_queue = mvmsta->vif->hw_queue[ac];
757 int queue = -1;
Sara Sharon01796ff2016-11-16 17:04:36 +0200758 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman9794c642015-08-19 17:34:28 +0300759 unsigned long disable_agg_tids = 0;
760 enum iwl_mvm_agg_state queue_state;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300761 bool shared_queue = false;
Liad Kaufman24afba72015-07-28 18:56:08 +0300762 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300763 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300764 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300765
766 lockdep_assert_held(&mvm->mutex);
767
Sara Sharon310181e2017-01-17 14:27:48 +0200768 if (iwl_mvm_has_new_tx_api(mvm))
769 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
770
Liad Kaufman42db09c2016-05-02 14:01:14 +0300771 spin_lock_bh(&mvmsta->lock);
772 tfd_queue_mask = mvmsta->tfd_queue_msk;
773 spin_unlock_bh(&mvmsta->lock);
774
Liad Kaufmand2515a92016-03-23 16:31:08 +0200775 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300776
777 /*
778 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
779 * exists
780 */
781 if (!ieee80211_is_data_qos(hdr->frame_control) ||
782 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300783 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
784 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300785 IWL_MVM_DQA_MAX_MGMT_QUEUE);
786 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
787 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
788 queue);
789
790 /* If no such queue is found, we'll use a DATA queue instead */
791 }
792
Liad Kaufman9794c642015-08-19 17:34:28 +0300793 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
794 (mvm->queue_info[mvmsta->reserved_queue].status ==
795 IWL_MVM_QUEUE_RESERVED ||
796 mvm->queue_info[mvmsta->reserved_queue].status ==
797 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300798 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300799 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300800 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
801 }
802
803 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300804 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
805 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300806 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300807
808 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300809 * Check if this queue is already allocated but inactive.
810 * In such a case, we'll need to first free this queue before enabling
811 * it again, so we'll mark it as reserved to make sure no new traffic
812 * arrives on it
813 */
814 if (queue > 0 &&
815 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
816 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
817 using_inactive_queue = true;
Sara Sharon01796ff2016-11-16 17:04:36 +0200818 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman9794c642015-08-19 17:34:28 +0300819 IWL_DEBUG_TX_QUEUES(mvm,
820 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
821 queue, mvmsta->sta_id, tid);
822 }
823
Liad Kaufman42db09c2016-05-02 14:01:14 +0300824 /* No free queue - we'll have to share */
825 if (queue <= 0) {
826 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
827 if (queue > 0) {
828 shared_queue = true;
829 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
830 }
831 }
832
Liad Kaufman9794c642015-08-19 17:34:28 +0300833 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300834 * Mark TXQ as ready, even though it hasn't been fully configured yet,
835 * to make sure no one else takes it.
836 * This will allow avoiding re-acquiring the lock at the end of the
837 * configuration. On error we'll mark it back as free.
838 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300839 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300840 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300841
Liad Kaufmand2515a92016-03-23 16:31:08 +0200842 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300843
Liad Kaufman42db09c2016-05-02 14:01:14 +0300844 /* This shouldn't happen - out of queues */
845 if (WARN_ON(queue <= 0)) {
846 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
847 tid, cfg.sta_id);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200848 return queue;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300849 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300850
851 /*
852 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
853 * but for configuring the SCD to send A-MPDUs we need to mark the queue
854 * as aggregatable.
855 * Mark all DATA queues as allowing to be aggregated at some point
856 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300857 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
858 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300859
Liad Kaufman9794c642015-08-19 17:34:28 +0300860 /*
861 * If this queue was previously inactive (idle) - we need to free it
862 * first
863 */
864 if (using_inactive_queue) {
Sara Sharon01796ff2016-11-16 17:04:36 +0200865 ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
866 if (ret)
Liad Kaufman9794c642015-08-19 17:34:28 +0300867 return ret;
Liad Kaufman9794c642015-08-19 17:34:28 +0300868 }
869
Liad Kaufman42db09c2016-05-02 14:01:14 +0300870 IWL_DEBUG_TX_QUEUES(mvm,
871 "Allocating %squeue #%d to sta %d on tid %d\n",
872 shared_queue ? "shared " : "", queue,
873 mvmsta->sta_id, tid);
874
875 if (shared_queue) {
876 /* Disable any open aggs on this queue */
877 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
878
879 if (disable_agg_tids) {
880 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
881 queue);
882 iwl_mvm_invalidate_sta_queue(mvm, queue,
883 disable_agg_tids, false);
884 }
Liad Kaufman42db09c2016-05-02 14:01:14 +0300885 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300886
887 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
888 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
889 wdg_timeout);
890
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200891 /*
892 * Mark queue as shared in transport if shared
893 * Note this has to be done after queue enablement because enablement
894 * can also set this value, and there is no indication there to shared
895 * queues
896 */
897 if (shared_queue)
898 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
899
Liad Kaufman24afba72015-07-28 18:56:08 +0300900 spin_lock_bh(&mvmsta->lock);
901 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300902 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300903 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300904 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300905
906 if (mvmsta->reserved_queue == queue)
907 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
908 spin_unlock_bh(&mvmsta->lock);
909
Liad Kaufman42db09c2016-05-02 14:01:14 +0300910 if (!shared_queue) {
911 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
912 if (ret)
913 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300914
Liad Kaufman42db09c2016-05-02 14:01:14 +0300915 /* If we need to re-enable aggregations... */
916 if (queue_state == IWL_AGG_ON) {
917 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
918 if (ret)
919 goto out_err;
920 }
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200921 } else {
922 /* Redirect queue, if needed */
923 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
924 wdg_timeout, false);
925 if (ret)
926 goto out_err;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300927 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300928
Liad Kaufman42db09c2016-05-02 14:01:14 +0300929 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300930
931out_err:
932 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
933
934 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300935}
936
Liad Kaufman19aefa42016-03-08 14:29:51 +0200937static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
938{
939 struct iwl_scd_txq_cfg_cmd cmd = {
940 .scd_queue = queue,
941 .action = SCD_CFG_UPDATE_QUEUE_TID,
942 };
Liad Kaufman19aefa42016-03-08 14:29:51 +0200943 int tid;
944 unsigned long tid_bitmap;
945 int ret;
946
947 lockdep_assert_held(&mvm->mutex);
948
Sara Sharonbb497012016-09-29 14:52:40 +0300949 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
950 return;
951
Liad Kaufman19aefa42016-03-08 14:29:51 +0200952 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200953 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
954 spin_unlock_bh(&mvm->queue_info_lock);
955
956 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
957 return;
958
959 /* Find any TID for queue */
960 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
961 cmd.tid = tid;
962 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
963
964 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
Liad Kaufman341ca402016-09-18 14:51:59 +0300965 if (ret) {
Liad Kaufman19aefa42016-03-08 14:29:51 +0200966 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
967 queue, ret);
Liad Kaufman341ca402016-09-18 14:51:59 +0300968 return;
969 }
970
971 spin_lock_bh(&mvm->queue_info_lock);
972 mvm->queue_info[queue].txq_tid = tid;
973 spin_unlock_bh(&mvm->queue_info_lock);
974 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
975 queue, tid);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200976}
977
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200978static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
979{
980 struct ieee80211_sta *sta;
981 struct iwl_mvm_sta *mvmsta;
982 s8 sta_id;
983 int tid = -1;
984 unsigned long tid_bitmap;
985 unsigned int wdg_timeout;
986 int ssn;
987 int ret = true;
988
Sara Sharonbb497012016-09-29 14:52:40 +0300989 /* queue sharing is disabled on new TX path */
990 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
991 return;
992
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200993 lockdep_assert_held(&mvm->mutex);
994
995 spin_lock_bh(&mvm->queue_info_lock);
996 sta_id = mvm->queue_info[queue].ra_sta_id;
997 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
998 spin_unlock_bh(&mvm->queue_info_lock);
999
1000 /* Find TID for queue, and make sure it is the only one on the queue */
1001 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
1002 if (tid_bitmap != BIT(tid)) {
1003 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
1004 queue, tid_bitmap);
1005 return;
1006 }
1007
1008 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
1009 tid);
1010
1011 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1012 lockdep_is_held(&mvm->mutex));
1013
1014 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
1015 return;
1016
1017 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1018 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1019
1020 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1021
1022 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
1023 tid_to_mac80211_ac[tid], ssn,
1024 wdg_timeout, true);
1025 if (ret) {
1026 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
1027 return;
1028 }
1029
1030 /* If aggs should be turned back on - do it */
1031 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
Emmanuel Grumbach9cd70e82016-09-20 13:40:33 +03001032 struct iwl_mvm_add_sta_cmd cmd = {0};
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001033
1034 mvmsta->tid_disable_agg &= ~BIT(tid);
1035
1036 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1037 cmd.sta_id = mvmsta->sta_id;
1038 cmd.add_modify = STA_MODE_MODIFY;
1039 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1040 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1041 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1042
1043 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1044 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1045 if (!ret) {
1046 IWL_DEBUG_TX_QUEUES(mvm,
1047 "TXQ #%d is now aggregated again\n",
1048 queue);
1049
1050 /* Mark queue intenally as aggregating again */
1051 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1052 }
1053 }
1054
1055 spin_lock_bh(&mvm->queue_info_lock);
1056 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1057 spin_unlock_bh(&mvm->queue_info_lock);
1058}
1059
Liad Kaufman24afba72015-07-28 18:56:08 +03001060static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1061{
1062 if (tid == IWL_MAX_TID_COUNT)
1063 return IEEE80211_AC_VO; /* MGMT */
1064
1065 return tid_to_mac80211_ac[tid];
1066}
1067
1068static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
1069 struct ieee80211_sta *sta, int tid)
1070{
1071 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1072 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1073 struct sk_buff *skb;
1074 struct ieee80211_hdr *hdr;
1075 struct sk_buff_head deferred_tx;
1076 u8 mac_queue;
1077 bool no_queue = false; /* Marks if there is a problem with the queue */
1078 u8 ac;
1079
1080 lockdep_assert_held(&mvm->mutex);
1081
1082 skb = skb_peek(&tid_data->deferred_tx_frames);
1083 if (!skb)
1084 return;
1085 hdr = (void *)skb->data;
1086
1087 ac = iwl_mvm_tid_to_ac_queue(tid);
1088 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
1089
Sara Sharon6862fce2017-02-22 19:34:17 +02001090 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE &&
Liad Kaufman24afba72015-07-28 18:56:08 +03001091 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
1092 IWL_ERR(mvm,
1093 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
1094 mvmsta->sta_id, tid);
1095
1096 /*
1097 * Mark queue as problematic so later the deferred traffic is
1098 * freed, as we can do nothing with it
1099 */
1100 no_queue = true;
1101 }
1102
1103 __skb_queue_head_init(&deferred_tx);
1104
Liad Kaufmand2515a92016-03-23 16:31:08 +02001105 /* Disable bottom-halves when entering TX path */
1106 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +03001107 spin_lock(&mvmsta->lock);
1108 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
Liad Kaufmanad5de732016-09-27 16:01:10 +03001109 mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
Liad Kaufman24afba72015-07-28 18:56:08 +03001110 spin_unlock(&mvmsta->lock);
1111
Liad Kaufman24afba72015-07-28 18:56:08 +03001112 while ((skb = __skb_dequeue(&deferred_tx)))
1113 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1114 ieee80211_free_txskb(mvm->hw, skb);
1115 local_bh_enable();
1116
1117 /* Wake queue */
1118 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1119}
1120
1121void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1122{
1123 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1124 add_stream_wk);
1125 struct ieee80211_sta *sta;
1126 struct iwl_mvm_sta *mvmsta;
1127 unsigned long deferred_tid_traffic;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001128 int queue, sta_id, tid;
Liad Kaufman24afba72015-07-28 18:56:08 +03001129
Liad Kaufman9794c642015-08-19 17:34:28 +03001130 /* Check inactivity of queues */
1131 iwl_mvm_inactivity_check(mvm);
1132
Liad Kaufman24afba72015-07-28 18:56:08 +03001133 mutex_lock(&mvm->mutex);
1134
Sara Sharon34e10862017-02-23 13:15:07 +02001135 /* No queue reconfiguration in TVQM mode */
1136 if (iwl_mvm_has_new_tx_api(mvm))
1137 goto alloc_queues;
1138
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001139 /* Reconfigure queues requiring reconfiguation */
Sara Sharon34e10862017-02-23 13:15:07 +02001140 for (queue = 0; queue < ARRAY_SIZE(mvm->queue_info); queue++) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001141 bool reconfig;
Liad Kaufman19aefa42016-03-08 14:29:51 +02001142 bool change_owner;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001143
1144 spin_lock_bh(&mvm->queue_info_lock);
1145 reconfig = (mvm->queue_info[queue].status ==
1146 IWL_MVM_QUEUE_RECONFIGURING);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001147
1148 /*
1149 * We need to take into account a situation in which a TXQ was
1150 * allocated to TID x, and then turned shared by adding TIDs y
1151 * and z. If TID x becomes inactive and is removed from the TXQ,
1152 * ownership must be given to one of the remaining TIDs.
1153 * This is mainly because if TID x continues - a new queue can't
1154 * be allocated for it as long as it is an owner of another TXQ.
1155 */
1156 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1157 BIT(mvm->queue_info[queue].txq_tid)) &&
1158 (mvm->queue_info[queue].status ==
1159 IWL_MVM_QUEUE_SHARED);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001160 spin_unlock_bh(&mvm->queue_info_lock);
1161
1162 if (reconfig)
1163 iwl_mvm_unshare_queue(mvm, queue);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001164 else if (change_owner)
1165 iwl_mvm_change_queue_owner(mvm, queue);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001166 }
1167
Sara Sharon34e10862017-02-23 13:15:07 +02001168alloc_queues:
Liad Kaufman24afba72015-07-28 18:56:08 +03001169 /* Go over all stations with deferred traffic */
1170 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1171 IWL_MVM_STATION_COUNT) {
1172 clear_bit(sta_id, mvm->sta_deferred_frames);
1173 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1174 lockdep_is_held(&mvm->mutex));
1175 if (IS_ERR_OR_NULL(sta))
1176 continue;
1177
1178 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1179 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1180
1181 for_each_set_bit(tid, &deferred_tid_traffic,
1182 IWL_MAX_TID_COUNT + 1)
1183 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1184 }
1185
1186 mutex_unlock(&mvm->mutex);
1187}
1188
1189static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001190 struct ieee80211_sta *sta,
1191 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +03001192{
1193 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1194 int queue;
Sara Sharon01796ff2016-11-16 17:04:36 +02001195 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman24afba72015-07-28 18:56:08 +03001196
Sara Sharon396952e2017-02-22 19:40:55 +02001197 /* queue reserving is disabled on new TX path */
1198 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1199 return 0;
1200
Liad Kaufman9794c642015-08-19 17:34:28 +03001201 /*
1202 * Check for inactive queues, so we don't reach a situation where we
1203 * can't add a STA due to a shortage in queues that doesn't really exist
1204 */
1205 iwl_mvm_inactivity_check(mvm);
1206
Liad Kaufman24afba72015-07-28 18:56:08 +03001207 spin_lock_bh(&mvm->queue_info_lock);
1208
1209 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +03001210 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1211 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +03001212 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1213 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +03001214 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1215 else
Liad Kaufman9794c642015-08-19 17:34:28 +03001216 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1217 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001218 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +03001219 if (queue < 0) {
1220 spin_unlock_bh(&mvm->queue_info_lock);
1221 IWL_ERR(mvm, "No available queues for new station\n");
1222 return -ENOSPC;
Sara Sharon01796ff2016-11-16 17:04:36 +02001223 } else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
1224 /*
1225 * If this queue is already allocated but inactive we'll need to
1226 * first free this queue before enabling it again, we'll mark
1227 * it as reserved to make sure no new traffic arrives on it
1228 */
1229 using_inactive_queue = true;
1230 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman24afba72015-07-28 18:56:08 +03001231 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03001232 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +03001233
1234 spin_unlock_bh(&mvm->queue_info_lock);
1235
1236 mvmsta->reserved_queue = queue;
1237
Sara Sharon01796ff2016-11-16 17:04:36 +02001238 if (using_inactive_queue)
1239 iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
1240
Liad Kaufman24afba72015-07-28 18:56:08 +03001241 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1242 queue, mvmsta->sta_id);
1243
1244 return 0;
1245}
1246
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001247/*
1248 * In DQA mode, after a HW restart the queues should be allocated as before, in
1249 * order to avoid race conditions when there are shared queues. This function
1250 * does the re-mapping and queue allocation.
1251 *
1252 * Note that re-enabling aggregations isn't done in this function.
1253 */
1254static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1255 struct iwl_mvm_sta *mvm_sta)
1256{
1257 unsigned int wdg_timeout =
1258 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1259 int i;
1260 struct iwl_trans_txq_scd_cfg cfg = {
1261 .sta_id = mvm_sta->sta_id,
1262 .frame_limit = IWL_FRAME_LIMIT,
1263 };
1264
Johannes Berg03c902b2016-12-02 12:03:36 +01001265 /* Make sure reserved queue is still marked as such (if allocated) */
1266 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1267 mvm->queue_info[mvm_sta->reserved_queue].status =
1268 IWL_MVM_QUEUE_RESERVED;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001269
1270 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1271 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1272 int txq_id = tid_data->txq_id;
1273 int ac;
1274 u8 mac_queue;
1275
Sara Sharon6862fce2017-02-22 19:34:17 +02001276 if (txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001277 continue;
1278
1279 skb_queue_head_init(&tid_data->deferred_tx_frames);
1280
1281 ac = tid_to_mac80211_ac[i];
1282 mac_queue = mvm_sta->vif->hw_queue[ac];
1283
Sara Sharon310181e2017-01-17 14:27:48 +02001284 if (iwl_mvm_has_new_tx_api(mvm)) {
1285 IWL_DEBUG_TX_QUEUES(mvm,
1286 "Re-mapping sta %d tid %d\n",
1287 mvm_sta->sta_id, i);
1288 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mac_queue,
1289 mvm_sta->sta_id,
1290 i, wdg_timeout);
1291 tid_data->txq_id = txq_id;
1292 } else {
1293 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001294
Sara Sharon310181e2017-01-17 14:27:48 +02001295 cfg.tid = i;
1296 cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
1297 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1298 txq_id ==
1299 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001300
Sara Sharon310181e2017-01-17 14:27:48 +02001301 IWL_DEBUG_TX_QUEUES(mvm,
1302 "Re-mapping sta %d tid %d to queue %d\n",
1303 mvm_sta->sta_id, i, txq_id);
1304
1305 iwl_mvm_enable_txq(mvm, txq_id, mac_queue, seq, &cfg,
1306 wdg_timeout);
Sara Sharon34e10862017-02-23 13:15:07 +02001307 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
Sara Sharon310181e2017-01-17 14:27:48 +02001308 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001309 }
1310
1311 atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
1312}
1313
Johannes Berg8ca151b2013-01-24 14:25:36 +01001314int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1315 struct ieee80211_vif *vif,
1316 struct ieee80211_sta *sta)
1317{
1318 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001319 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001320 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001321 int i, ret, sta_id;
1322
1323 lockdep_assert_held(&mvm->mutex);
1324
1325 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001326 sta_id = iwl_mvm_find_free_sta_id(mvm,
1327 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001328 else
1329 sta_id = mvm_sta->sta_id;
1330
Sara Sharon0ae98812017-01-04 14:53:58 +02001331 if (sta_id == IWL_MVM_INVALID_STA)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001332 return -ENOSPC;
1333
1334 spin_lock_init(&mvm_sta->lock);
1335
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001336 /* In DQA mode, if this is a HW restart, re-alloc existing queues */
1337 if (iwl_mvm_is_dqa_supported(mvm) &&
1338 test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1339 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1340 goto update_fw;
1341 }
1342
Johannes Berg8ca151b2013-01-24 14:25:36 +01001343 mvm_sta->sta_id = sta_id;
1344 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1345 mvmvif->color);
1346 mvm_sta->vif = vif;
1347 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001348 mvm_sta->tx_protection = 0;
1349 mvm_sta->tt_tx_protection = false;
Sara Sharonced19f22017-02-06 19:09:32 +02001350 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001351
1352 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001353 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +03001354 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001355 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001356
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001357 /*
1358 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1359 * and then they'll be allocated dynamically
1360 */
1361 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001362 ret = iwl_mvm_tdls_sta_init(mvm, sta);
1363 if (ret)
1364 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +03001365 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001366 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1367 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1368 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1369 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001370
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001371 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001372 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001373 u16 seq = mvm_sta->tid_data[i].seq_number;
1374 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1375 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001376
1377 if (!iwl_mvm_is_dqa_supported(mvm))
1378 continue;
1379
1380 /*
1381 * Mark all queues for this STA as unallocated and defer TX
1382 * frames until the queue is allocated
1383 */
Sara Sharon6862fce2017-02-22 19:34:17 +02001384 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001385 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001386 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001387 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001388 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001389
Sara Sharona571f5f2015-12-07 12:50:58 +02001390 if (iwl_mvm_has_new_rx_api(mvm) &&
1391 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1392 dup_data = kcalloc(mvm->trans->num_rx_queues,
1393 sizeof(*dup_data),
1394 GFP_KERNEL);
1395 if (!dup_data)
1396 return -ENOMEM;
1397 mvm_sta->dup_data = dup_data;
1398 }
1399
Sara Sharon396952e2017-02-22 19:40:55 +02001400 if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001401 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1402 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001403 if (ret)
1404 goto err;
1405 }
1406
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001407update_fw:
Liad Kaufman24afba72015-07-28 18:56:08 +03001408 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001409 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001410 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001411
Johannes Berg9e848012014-08-04 14:33:42 +02001412 if (vif->type == NL80211_IFTYPE_STATION) {
1413 if (!sta->tdls) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001414 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001415 mvmvif->ap_sta_id = sta_id;
1416 } else {
Sara Sharon0ae98812017-01-04 14:53:58 +02001417 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001418 }
1419 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001420
1421 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1422
1423 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001424
1425err:
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001426 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1427 iwl_mvm_tdls_sta_deinit(mvm, sta);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001428 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001429}
1430
1431int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1432 bool drain)
1433{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001434 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001435 int ret;
1436 u32 status;
1437
1438 lockdep_assert_held(&mvm->mutex);
1439
1440 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1441 cmd.sta_id = mvmsta->sta_id;
1442 cmd.add_modify = STA_MODE_MODIFY;
1443 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1444 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1445
1446 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001447 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1448 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001449 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001450 if (ret)
1451 return ret;
1452
Sara Sharon837c4da2016-01-07 16:50:45 +02001453 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001454 case ADD_STA_SUCCESS:
1455 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1456 mvmsta->sta_id);
1457 break;
1458 default:
1459 ret = -EIO;
1460 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1461 mvmsta->sta_id);
1462 break;
1463 }
1464
1465 return ret;
1466}
1467
1468/*
1469 * Remove a station from the FW table. Before sending the command to remove
1470 * the station validate that the station is indeed known to the driver (sanity
1471 * only).
1472 */
1473static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1474{
1475 struct ieee80211_sta *sta;
1476 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1477 .sta_id = sta_id,
1478 };
1479 int ret;
1480
1481 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1482 lockdep_is_held(&mvm->mutex));
1483
1484 /* Note: internal stations are marked as error values */
1485 if (!sta) {
1486 IWL_ERR(mvm, "Invalid station id\n");
1487 return -EINVAL;
1488 }
1489
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001490 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001491 sizeof(rm_sta_cmd), &rm_sta_cmd);
1492 if (ret) {
1493 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1494 return ret;
1495 }
1496
1497 return 0;
1498}
1499
1500void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1501{
1502 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1503 u8 sta_id;
1504
1505 /*
1506 * The mutex is needed because of the SYNC cmd, but not only: if the
1507 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1508 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1509 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1510 * that later.
1511 */
1512 mutex_lock(&mvm->mutex);
1513
1514 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1515 int ret;
1516 struct ieee80211_sta *sta =
1517 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1518 lockdep_is_held(&mvm->mutex));
1519
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001520 /*
1521 * This station is in use or RCU-removed; the latter happens in
1522 * managed mode, where mac80211 removes the station before we
1523 * can remove it from firmware (we can only do that after the
1524 * MAC is marked unassociated), and possibly while the deauth
1525 * frame to disconnect from the AP is still queued. Then, the
1526 * station pointer is -ENOENT when the last skb is reclaimed.
1527 */
1528 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001529 continue;
1530
1531 if (PTR_ERR(sta) == -EINVAL) {
1532 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1533 sta_id);
1534 continue;
1535 }
1536
1537 if (!sta) {
1538 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1539 sta_id);
1540 continue;
1541 }
1542
1543 WARN_ON(PTR_ERR(sta) != -EBUSY);
1544 /* This station was removed and we waited until it got drained,
1545 * we can now proceed and remove it.
1546 */
1547 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1548 if (ret) {
1549 IWL_ERR(mvm,
1550 "Couldn't remove sta %d after it was drained\n",
1551 sta_id);
1552 continue;
1553 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301554 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001555 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001556
1557 if (mvm->tfd_drained[sta_id]) {
1558 unsigned long i, msk = mvm->tfd_drained[sta_id];
1559
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001560 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001561 iwl_mvm_disable_txq(mvm, i, i,
1562 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001563
1564 mvm->tfd_drained[sta_id] = 0;
1565 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1566 sta_id, msk);
1567 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001568 }
1569
1570 mutex_unlock(&mvm->mutex);
1571}
1572
Liad Kaufman24afba72015-07-28 18:56:08 +03001573static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1574 struct ieee80211_vif *vif,
1575 struct iwl_mvm_sta *mvm_sta)
1576{
1577 int ac;
1578 int i;
1579
1580 lockdep_assert_held(&mvm->mutex);
1581
1582 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
Sara Sharon6862fce2017-02-22 19:34:17 +02001583 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman24afba72015-07-28 18:56:08 +03001584 continue;
1585
1586 ac = iwl_mvm_tid_to_ac_queue(i);
1587 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1588 vif->hw_queue[ac], i, 0);
Sara Sharon6862fce2017-02-22 19:34:17 +02001589 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001590 }
1591}
1592
Sara Sharond6d517b2017-03-06 10:16:11 +02001593int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1594 struct iwl_mvm_sta *mvm_sta)
1595{
1596 int i, ret;
1597
1598 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1599 u16 txq_id;
1600
1601 spin_lock_bh(&mvm_sta->lock);
1602 txq_id = mvm_sta->tid_data[i].txq_id;
1603 spin_unlock_bh(&mvm_sta->lock);
1604
1605 if (txq_id == IWL_MVM_INVALID_QUEUE)
1606 continue;
1607
1608 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1609 if (ret)
1610 break;
1611 }
1612
1613 return ret;
1614}
1615
Johannes Berg8ca151b2013-01-24 14:25:36 +01001616int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1617 struct ieee80211_vif *vif,
1618 struct ieee80211_sta *sta)
1619{
1620 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001621 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001622 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001623 int ret;
1624
1625 lockdep_assert_held(&mvm->mutex);
1626
Sara Sharona571f5f2015-12-07 12:50:58 +02001627 if (iwl_mvm_has_new_rx_api(mvm))
1628 kfree(mvm_sta->dup_data);
1629
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001630 if ((vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001631 mvmvif->ap_sta_id == sta_id) ||
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001632 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001633 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1634 if (ret)
1635 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001636 /* flush its queues here since we are freeing mvm_sta */
Sara Sharond49394a2017-03-05 13:01:08 +02001637 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001638 if (ret)
1639 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001640 if (iwl_mvm_has_new_tx_api(mvm)) {
1641 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1642 } else {
1643 u32 q_mask = mvm_sta->tfd_queue_msk;
1644
1645 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1646 q_mask);
1647 }
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001648 if (ret)
1649 return ret;
1650 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001651
Liad Kaufman24afba72015-07-28 18:56:08 +03001652 /* If DQA is supported - the queues can be disabled now */
Sara Sharon94c3e612016-12-07 15:04:37 +02001653 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman56214742016-09-22 15:14:08 +03001654 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001655 /*
1656 * If pending_frames is set at this point - it must be
1657 * driver internal logic error, since queues are empty
1658 * and removed successuly.
1659 * warn on it but set it to 0 anyway to avoid station
1660 * not being removed later in the function
1661 */
1662 WARN_ON(atomic_xchg(&mvm->pending_frames[sta_id], 0));
1663 }
Liad Kaufman56214742016-09-22 15:14:08 +03001664
1665 /* If there is a TXQ still marked as reserved - free it */
1666 if (iwl_mvm_is_dqa_supported(mvm) &&
1667 mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001668 u8 reserved_txq = mvm_sta->reserved_queue;
1669 enum iwl_mvm_queue_status *status;
1670
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001671 /*
1672 * If no traffic has gone through the reserved TXQ - it
1673 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1674 * should be manually marked as free again
1675 */
1676 spin_lock_bh(&mvm->queue_info_lock);
1677 status = &mvm->queue_info[reserved_txq].status;
1678 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1679 (*status != IWL_MVM_QUEUE_FREE),
1680 "sta_id %d reserved txq %d status %d",
Sara Sharon94c3e612016-12-07 15:04:37 +02001681 sta_id, reserved_txq, *status)) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001682 spin_unlock_bh(&mvm->queue_info_lock);
1683 return -EINVAL;
1684 }
1685
1686 *status = IWL_MVM_QUEUE_FREE;
1687 spin_unlock_bh(&mvm->queue_info_lock);
1688 }
1689
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001690 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001691 mvmvif->ap_sta_id == sta_id) {
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001692 /* if associated - we can't remove the AP STA now */
1693 if (vif->bss_conf.assoc)
1694 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001695
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001696 /* unassoc - go ahead - remove the AP STA now */
Sara Sharon0ae98812017-01-04 14:53:58 +02001697 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Eliad Peller37577fe2013-12-05 17:19:39 +02001698
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001699 /* clear d0i3_ap_sta_id if no longer relevant */
Sara Sharon94c3e612016-12-07 15:04:37 +02001700 if (mvm->d0i3_ap_sta_id == sta_id)
Sara Sharon0ae98812017-01-04 14:53:58 +02001701 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001702 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001703 }
1704
1705 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001706 * This shouldn't happen - the TDLS channel switch should be canceled
1707 * before the STA is removed.
1708 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001709 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001710 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001711 cancel_delayed_work(&mvm->tdls_cs.dwork);
1712 }
1713
1714 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001715 * Make sure that the tx response code sees the station as -EBUSY and
1716 * calls the drain worker.
1717 */
1718 spin_lock_bh(&mvm_sta->lock);
Sara Sharon94c3e612016-12-07 15:04:37 +02001719
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001720 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001721 * There are frames pending on the AC queues for this station.
1722 * We need to wait until all the frames are drained...
1723 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001724 if (atomic_read(&mvm->pending_frames[sta_id])) {
1725 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id],
Johannes Berg8ca151b2013-01-24 14:25:36 +01001726 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001727 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001728
1729 /* disable TDLS sta queues on drain complete */
1730 if (sta->tdls) {
Sara Sharon94c3e612016-12-07 15:04:37 +02001731 mvm->tfd_drained[sta_id] = mvm_sta->tfd_queue_msk;
1732 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n", sta_id);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001733 }
1734
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001735 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001736 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001737 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001738
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001739 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001740 iwl_mvm_tdls_sta_deinit(mvm, sta);
1741
Johannes Berg8ca151b2013-01-24 14:25:36 +01001742 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301743 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001744 }
1745
1746 return ret;
1747}
1748
1749int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1750 struct ieee80211_vif *vif,
1751 u8 sta_id)
1752{
1753 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1754
1755 lockdep_assert_held(&mvm->mutex);
1756
Monam Agarwalc531c772014-03-24 00:05:56 +05301757 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001758 return ret;
1759}
1760
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001761int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1762 struct iwl_mvm_int_sta *sta,
Sara Sharonced19f22017-02-06 19:09:32 +02001763 u32 qmask, enum nl80211_iftype iftype,
1764 enum iwl_sta_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001765{
1766 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001767 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Sara Sharon0ae98812017-01-04 14:53:58 +02001768 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001769 return -ENOSPC;
1770 }
1771
1772 sta->tfd_queue_msk = qmask;
Sara Sharonced19f22017-02-06 19:09:32 +02001773 sta->type = type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001774
1775 /* put a non-NULL value so iterating over the stations won't stop */
1776 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1777 return 0;
1778}
1779
Sara Sharon26d6c162017-01-03 12:00:19 +02001780void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001781{
Monam Agarwalc531c772014-03-24 00:05:56 +05301782 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001783 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
Sara Sharon0ae98812017-01-04 14:53:58 +02001784 sta->sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001785}
1786
1787static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1788 struct iwl_mvm_int_sta *sta,
1789 const u8 *addr,
1790 u16 mac_id, u16 color)
1791{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001792 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001793 int ret;
1794 u32 status;
1795
1796 lockdep_assert_held(&mvm->mutex);
1797
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001798 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001799 cmd.sta_id = sta->sta_id;
1800 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1801 color));
Sara Sharonced19f22017-02-06 19:09:32 +02001802 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1803 cmd.station_type = sta->type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001804
Sara Sharonbb497012016-09-29 14:52:40 +03001805 if (!iwl_mvm_has_new_tx_api(mvm))
1806 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001807 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001808
1809 if (addr)
1810 memcpy(cmd.addr, addr, ETH_ALEN);
1811
Sara Sharon854c5702016-01-26 13:17:47 +02001812 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1813 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001814 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001815 if (ret)
1816 return ret;
1817
Sara Sharon837c4da2016-01-07 16:50:45 +02001818 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001819 case ADD_STA_SUCCESS:
1820 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1821 return 0;
1822 default:
1823 ret = -EIO;
1824 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1825 status);
1826 break;
1827 }
1828 return ret;
1829}
1830
Sara Sharonc5a719e2016-11-15 10:20:48 +02001831static void iwl_mvm_enable_aux_queue(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001832{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001833 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1834 mvm->cfg->base_params->wd_timeout :
1835 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001836
Sara Sharon310181e2017-01-17 14:27:48 +02001837 if (iwl_mvm_has_new_tx_api(mvm)) {
1838 int queue = iwl_mvm_tvqm_enable_txq(mvm, mvm->aux_queue,
1839 mvm->aux_sta.sta_id,
1840 IWL_MAX_TID_COUNT,
1841 wdg_timeout);
1842 mvm->aux_queue = queue;
1843 } else if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman28d07932015-09-01 16:36:25 +03001844 struct iwl_trans_txq_scd_cfg cfg = {
1845 .fifo = IWL_MVM_TX_FIFO_MCAST,
1846 .sta_id = mvm->aux_sta.sta_id,
1847 .tid = IWL_MAX_TID_COUNT,
1848 .aggregate = false,
1849 .frame_limit = IWL_FRAME_LIMIT,
1850 };
1851
1852 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1853 wdg_timeout);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001854 } else {
1855 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1856 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Liad Kaufman28d07932015-09-01 16:36:25 +03001857 }
Sara Sharonc5a719e2016-11-15 10:20:48 +02001858}
1859
1860int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1861{
1862 int ret;
1863
1864 lockdep_assert_held(&mvm->mutex);
1865
1866 /* Allocate aux station and assign to it the aux queue */
1867 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Sara Sharonced19f22017-02-06 19:09:32 +02001868 NL80211_IFTYPE_UNSPECIFIED,
1869 IWL_STA_AUX_ACTIVITY);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001870 if (ret)
1871 return ret;
1872
1873 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1874 if (!iwl_mvm_has_new_tx_api(mvm))
1875 iwl_mvm_enable_aux_queue(mvm);
Liad Kaufman28d07932015-09-01 16:36:25 +03001876
Johannes Berg8ca151b2013-01-24 14:25:36 +01001877 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1878 MAC_INDEX_AUX, 0);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001879 if (ret) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001880 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001881 return ret;
1882 }
1883
1884 /*
1885 * For a000 firmware and on we cannot add queue to a station unknown
1886 * to firmware so enable queue here - after the station was added
1887 */
1888 if (iwl_mvm_has_new_tx_api(mvm))
1889 iwl_mvm_enable_aux_queue(mvm);
1890
1891 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001892}
1893
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001894int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1895{
1896 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1897
1898 lockdep_assert_held(&mvm->mutex);
1899 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1900 mvmvif->id, 0);
1901}
1902
1903int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1904{
1905 int ret;
1906
1907 lockdep_assert_held(&mvm->mutex);
1908
1909 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1910 if (ret)
1911 IWL_WARN(mvm, "Failed sending remove station\n");
1912
1913 return ret;
1914}
1915
1916void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1917{
1918 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1919}
1920
Johannes Berg712b24a2014-08-04 14:14:14 +02001921void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1922{
1923 lockdep_assert_held(&mvm->mutex);
1924
1925 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1926}
1927
Johannes Berg8ca151b2013-01-24 14:25:36 +01001928/*
1929 * Send the add station command for the vif's broadcast station.
1930 * Assumes that the station was already allocated.
1931 *
1932 * @mvm: the mvm component
1933 * @vif: the interface to which the broadcast station is added
1934 * @bsta: the broadcast station to add.
1935 */
Johannes Berg013290a2014-08-04 13:38:48 +02001936int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001937{
1938 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001939 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001940 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001941 const u8 *baddr = _baddr;
Johannes Berg7daa7622017-02-24 12:02:22 +01001942 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001943 int ret;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001944 unsigned int wdg_timeout =
1945 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1946 struct iwl_trans_txq_scd_cfg cfg = {
1947 .fifo = IWL_MVM_TX_FIFO_VO,
1948 .sta_id = mvmvif->bcast_sta.sta_id,
1949 .tid = IWL_MAX_TID_COUNT,
1950 .aggregate = false,
1951 .frame_limit = IWL_FRAME_LIMIT,
1952 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001953
1954 lockdep_assert_held(&mvm->mutex);
1955
Sara Sharon310181e2017-01-17 14:27:48 +02001956 if (iwl_mvm_is_dqa_supported(mvm) && !iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufman4d339982017-03-21 17:13:16 +02001957 if (vif->type == NL80211_IFTYPE_AP ||
1958 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon49f71712017-01-09 12:07:16 +02001959 queue = mvm->probe_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001960 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
Sara Sharon49f71712017-01-09 12:07:16 +02001961 queue = mvm->p2p_dev_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001962 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
Liad Kaufmande24f632015-08-04 15:19:18 +03001963 return -EINVAL;
1964
Liad Kaufmandf88c082016-11-24 15:31:00 +02001965 bsta->tfd_queue_msk |= BIT(queue);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001966
Sara Sharon310181e2017-01-17 14:27:48 +02001967 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1968 &cfg, wdg_timeout);
Liad Kaufmande24f632015-08-04 15:19:18 +03001969 }
1970
Johannes Berg5023d962013-07-31 14:07:43 +02001971 if (vif->type == NL80211_IFTYPE_ADHOC)
1972 baddr = vif->bss_conf.bssid;
1973
Sara Sharon0ae98812017-01-04 14:53:58 +02001974 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001975 return -ENOSPC;
1976
Liad Kaufmandf88c082016-11-24 15:31:00 +02001977 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1978 mvmvif->id, mvmvif->color);
1979 if (ret)
1980 return ret;
1981
1982 /*
Sara Sharonc5a719e2016-11-15 10:20:48 +02001983 * For a000 firmware and on we cannot add queue to a station unknown
1984 * to firmware so enable queue here - after the station was added
Liad Kaufmandf88c082016-11-24 15:31:00 +02001985 */
Sara Sharon310181e2017-01-17 14:27:48 +02001986 if (iwl_mvm_has_new_tx_api(mvm)) {
Johannes Berg7daa7622017-02-24 12:02:22 +01001987 queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
1988 bsta->sta_id,
1989 IWL_MAX_TID_COUNT,
1990 wdg_timeout);
1991
Sara Sharon310181e2017-01-17 14:27:48 +02001992 if (vif->type == NL80211_IFTYPE_AP)
1993 mvm->probe_queue = queue;
1994 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1995 mvm->p2p_dev_queue = queue;
1996
1997 bsta->tfd_queue_msk |= BIT(queue);
1998 }
Liad Kaufmandf88c082016-11-24 15:31:00 +02001999
2000 return 0;
2001}
2002
2003static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2004 struct ieee80211_vif *vif)
2005{
2006 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2007
2008 lockdep_assert_held(&mvm->mutex);
2009
Sara Sharond49394a2017-03-05 13:01:08 +02002010 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2011
Liad Kaufman4d339982017-03-21 17:13:16 +02002012 if (vif->type == NL80211_IFTYPE_AP ||
2013 vif->type == NL80211_IFTYPE_ADHOC)
Liad Kaufmandf88c082016-11-24 15:31:00 +02002014 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
2015 IWL_MAX_TID_COUNT, 0);
2016
Sara Sharon49f71712017-01-09 12:07:16 +02002017 if (mvmvif->bcast_sta.tfd_queue_msk & BIT(mvm->probe_queue)) {
2018 iwl_mvm_disable_txq(mvm, mvm->probe_queue,
Liad Kaufmandf88c082016-11-24 15:31:00 +02002019 vif->hw_queue[0], IWL_MAX_TID_COUNT,
2020 0);
Sara Sharon49f71712017-01-09 12:07:16 +02002021 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(mvm->probe_queue);
Liad Kaufmandf88c082016-11-24 15:31:00 +02002022 }
2023
Sara Sharon49f71712017-01-09 12:07:16 +02002024 if (mvmvif->bcast_sta.tfd_queue_msk & BIT(mvm->p2p_dev_queue)) {
2025 iwl_mvm_disable_txq(mvm, mvm->p2p_dev_queue,
Liad Kaufmandf88c082016-11-24 15:31:00 +02002026 vif->hw_queue[0], IWL_MAX_TID_COUNT,
2027 0);
Sara Sharon49f71712017-01-09 12:07:16 +02002028 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(mvm->p2p_dev_queue);
Liad Kaufmandf88c082016-11-24 15:31:00 +02002029 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002030}
2031
2032/* Send the FW a request to remove the station from it's internal data
2033 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02002034int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002035{
Johannes Berg013290a2014-08-04 13:38:48 +02002036 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002037 int ret;
2038
2039 lockdep_assert_held(&mvm->mutex);
2040
Liad Kaufmandf88c082016-11-24 15:31:00 +02002041 if (iwl_mvm_is_dqa_supported(mvm))
2042 iwl_mvm_free_bcast_sta_queues(mvm, vif);
2043
Johannes Berg013290a2014-08-04 13:38:48 +02002044 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002045 if (ret)
2046 IWL_WARN(mvm, "Failed sending remove station\n");
2047 return ret;
2048}
2049
Johannes Berg013290a2014-08-04 13:38:48 +02002050int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2051{
2052 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03002053 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02002054
2055 lockdep_assert_held(&mvm->mutex);
2056
Liad Kaufmandf88c082016-11-24 15:31:00 +02002057 if (!iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmande24f632015-08-04 15:19:18 +03002058 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02002059
Liad Kaufmande24f632015-08-04 15:19:18 +03002060 /*
2061 * The firmware defines the TFD queue mask to only be relevant
2062 * for *unicast* queues, so the multicast (CAB) queue shouldn't
Liad Kaufmandf88c082016-11-24 15:31:00 +02002063 * be included. This only happens in NL80211_IFTYPE_AP vif type,
2064 * so the next line will only have an effect there.
Liad Kaufmande24f632015-08-04 15:19:18 +03002065 */
Johannes Berg013290a2014-08-04 13:38:48 +02002066 qmask &= ~BIT(vif->cab_queue);
Liad Kaufmande24f632015-08-04 15:19:18 +03002067 }
2068
Johannes Berg013290a2014-08-04 13:38:48 +02002069 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
Sara Sharonced19f22017-02-06 19:09:32 +02002070 ieee80211_vif_type_p2p(vif),
2071 IWL_STA_GENERAL_PURPOSE);
Johannes Berg013290a2014-08-04 13:38:48 +02002072}
2073
Johannes Berg8ca151b2013-01-24 14:25:36 +01002074/* Allocate a new station entry for the broadcast station to the given vif,
2075 * and send it to the FW.
2076 * Note that each P2P mac should have its own broadcast station.
2077 *
2078 * @mvm: the mvm component
2079 * @vif: the interface to which the broadcast station is added
2080 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02002081int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002082{
2083 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02002084 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002085 int ret;
2086
2087 lockdep_assert_held(&mvm->mutex);
2088
Johannes Berg013290a2014-08-04 13:38:48 +02002089 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002090 if (ret)
2091 return ret;
2092
Johannes Berg013290a2014-08-04 13:38:48 +02002093 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002094
2095 if (ret)
2096 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02002097
Johannes Berg8ca151b2013-01-24 14:25:36 +01002098 return ret;
2099}
2100
Johannes Berg013290a2014-08-04 13:38:48 +02002101void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2102{
2103 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2104
2105 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2106}
2107
Johannes Berg8ca151b2013-01-24 14:25:36 +01002108/*
2109 * Send the FW a request to remove the station from it's internal data
2110 * structures, and in addition remove it from the local data structure.
2111 */
Johannes Berg013290a2014-08-04 13:38:48 +02002112int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002113{
2114 int ret;
2115
2116 lockdep_assert_held(&mvm->mutex);
2117
Johannes Berg013290a2014-08-04 13:38:48 +02002118 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002119
Johannes Berg013290a2014-08-04 13:38:48 +02002120 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2121
Johannes Berg8ca151b2013-01-24 14:25:36 +01002122 return ret;
2123}
2124
Sara Sharon26d6c162017-01-03 12:00:19 +02002125/*
2126 * Allocate a new station entry for the multicast station to the given vif,
2127 * and send it to the FW.
2128 * Note that each AP/GO mac should have its own multicast station.
2129 *
2130 * @mvm: the mvm component
2131 * @vif: the interface to which the multicast station is added
2132 */
2133int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2134{
2135 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2136 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2137 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2138 const u8 *maddr = _maddr;
2139 struct iwl_trans_txq_scd_cfg cfg = {
2140 .fifo = IWL_MVM_TX_FIFO_MCAST,
2141 .sta_id = msta->sta_id,
2142 .tid = IWL_MAX_TID_COUNT,
2143 .aggregate = false,
2144 .frame_limit = IWL_FRAME_LIMIT,
2145 };
2146 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2147 int ret;
2148
2149 lockdep_assert_held(&mvm->mutex);
2150
2151 if (!iwl_mvm_is_dqa_supported(mvm))
2152 return 0;
2153
2154 if (WARN_ON(vif->type != NL80211_IFTYPE_AP))
2155 return -ENOTSUPP;
2156
Sara Sharonced19f22017-02-06 19:09:32 +02002157 /*
2158 * While in previous FWs we had to exclude cab queue from TFD queue
2159 * mask, now it is needed as any other queue.
2160 */
2161 if (!iwl_mvm_has_new_tx_api(mvm) &&
2162 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2163 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2164 &cfg, timeout);
2165 msta->tfd_queue_msk |= BIT(vif->cab_queue);
2166 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002167 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2168 mvmvif->id, mvmvif->color);
2169 if (ret) {
2170 iwl_mvm_dealloc_int_sta(mvm, msta);
2171 return ret;
2172 }
2173
2174 /*
2175 * Enable cab queue after the ADD_STA command is sent.
2176 * This is needed for a000 firmware which won't accept SCD_QUEUE_CFG
Sara Sharonced19f22017-02-06 19:09:32 +02002177 * command with unknown station id, and for FW that doesn't support
2178 * station API since the cab queue is not included in the
2179 * tfd_queue_mask.
Sara Sharon26d6c162017-01-03 12:00:19 +02002180 */
Sara Sharon310181e2017-01-17 14:27:48 +02002181 if (iwl_mvm_has_new_tx_api(mvm)) {
2182 int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2183 msta->sta_id,
2184 IWL_MAX_TID_COUNT,
2185 timeout);
Sara Sharone2af3fa2017-02-22 19:35:10 +02002186 mvmvif->cab_queue = queue;
Sara Sharonced19f22017-02-06 19:09:32 +02002187 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2188 IWL_UCODE_TLV_API_STA_TYPE)) {
Sara Sharon310181e2017-01-17 14:27:48 +02002189 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2190 &cfg, timeout);
2191 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002192
2193 return 0;
2194}
2195
2196/*
2197 * Send the FW a request to remove the station from it's internal data
2198 * structures, and in addition remove it from the local data structure.
2199 */
2200int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2201{
2202 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2203 int ret;
2204
2205 lockdep_assert_held(&mvm->mutex);
2206
2207 if (!iwl_mvm_is_dqa_supported(mvm))
2208 return 0;
2209
Sara Sharond49394a2017-03-05 13:01:08 +02002210 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2211
Sara Sharone2af3fa2017-02-22 19:35:10 +02002212 iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
Sara Sharon26d6c162017-01-03 12:00:19 +02002213 IWL_MAX_TID_COUNT, 0);
2214
2215 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2216 if (ret)
2217 IWL_WARN(mvm, "Failed sending remove station\n");
2218
2219 return ret;
2220}
2221
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002222#define IWL_MAX_RX_BA_SESSIONS 16
2223
Sara Sharonb915c102016-03-23 16:32:02 +02002224static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02002225{
Sara Sharonb915c102016-03-23 16:32:02 +02002226 struct iwl_mvm_delba_notif notif = {
2227 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2228 .metadata.sync = 1,
2229 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02002230 };
Sara Sharonb915c102016-03-23 16:32:02 +02002231 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2232};
Sara Sharon10b2b202016-03-20 16:23:41 +02002233
Sara Sharonb915c102016-03-23 16:32:02 +02002234static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2235 struct iwl_mvm_baid_data *data)
2236{
2237 int i;
2238
2239 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2240
2241 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2242 int j;
2243 struct iwl_mvm_reorder_buffer *reorder_buf =
2244 &data->reorder_buf[i];
2245
Sara Sharon06904052016-02-28 20:28:17 +02002246 spin_lock_bh(&reorder_buf->lock);
2247 if (likely(!reorder_buf->num_stored)) {
2248 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02002249 continue;
Sara Sharon06904052016-02-28 20:28:17 +02002250 }
Sara Sharonb915c102016-03-23 16:32:02 +02002251
2252 /*
2253 * This shouldn't happen in regular DELBA since the internal
2254 * delBA notification should trigger a release of all frames in
2255 * the reorder buffer.
2256 */
2257 WARN_ON(1);
2258
2259 for (j = 0; j < reorder_buf->buf_size; j++)
2260 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02002261 /*
2262 * Prevent timer re-arm. This prevents a very far fetched case
2263 * where we timed out on the notification. There may be prior
2264 * RX frames pending in the RX queue before the notification
2265 * that might get processed between now and the actual deletion
2266 * and we would re-arm the timer although we are deleting the
2267 * reorder buffer.
2268 */
2269 reorder_buf->removed = true;
2270 spin_unlock_bh(&reorder_buf->lock);
2271 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02002272 }
2273}
2274
2275static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2276 u32 sta_id,
2277 struct iwl_mvm_baid_data *data,
2278 u16 ssn, u8 buf_size)
2279{
2280 int i;
2281
2282 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2283 struct iwl_mvm_reorder_buffer *reorder_buf =
2284 &data->reorder_buf[i];
2285 int j;
2286
2287 reorder_buf->num_stored = 0;
2288 reorder_buf->head_sn = ssn;
2289 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02002290 /* rx reorder timer */
2291 reorder_buf->reorder_timer.function =
2292 iwl_mvm_reorder_timer_expired;
2293 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
2294 init_timer(&reorder_buf->reorder_timer);
2295 spin_lock_init(&reorder_buf->lock);
2296 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02002297 reorder_buf->queue = i;
2298 reorder_buf->sta_id = sta_id;
Sara Sharon5d43eab2017-02-02 12:51:39 +02002299 reorder_buf->valid = false;
Sara Sharonb915c102016-03-23 16:32:02 +02002300 for (j = 0; j < reorder_buf->buf_size; j++)
2301 __skb_queue_head_init(&reorder_buf->entries[j]);
2302 }
Sara Sharon10b2b202016-03-20 16:23:41 +02002303}
2304
Johannes Berg8ca151b2013-01-24 14:25:36 +01002305int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02002306 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002307{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002308 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002309 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02002310 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002311 int ret;
2312 u32 status;
2313
2314 lockdep_assert_held(&mvm->mutex);
2315
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002316 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2317 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2318 return -ENOSPC;
2319 }
2320
Sara Sharon10b2b202016-03-20 16:23:41 +02002321 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2322 /*
2323 * Allocate here so if allocation fails we can bail out early
2324 * before starting the BA session in the firmware
2325 */
Sara Sharonb915c102016-03-23 16:32:02 +02002326 baid_data = kzalloc(sizeof(*baid_data) +
2327 mvm->trans->num_rx_queues *
2328 sizeof(baid_data->reorder_buf[0]),
2329 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02002330 if (!baid_data)
2331 return -ENOMEM;
2332 }
2333
Johannes Berg8ca151b2013-01-24 14:25:36 +01002334 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2335 cmd.sta_id = mvm_sta->sta_id;
2336 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002337 if (start) {
2338 cmd.add_immediate_ba_tid = (u8) tid;
2339 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02002340 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002341 } else {
2342 cmd.remove_immediate_ba_tid = (u8) tid;
2343 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002344 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2345 STA_MODIFY_REMOVE_BA_TID;
2346
2347 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002348 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2349 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002350 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002351 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02002352 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002353
Sara Sharon837c4da2016-01-07 16:50:45 +02002354 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002355 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03002356 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2357 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002358 break;
2359 case ADD_STA_IMMEDIATE_BA_FAILURE:
2360 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2361 ret = -ENOSPC;
2362 break;
2363 default:
2364 ret = -EIO;
2365 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2366 start ? "start" : "stopp", status);
2367 break;
2368 }
2369
Sara Sharon10b2b202016-03-20 16:23:41 +02002370 if (ret)
2371 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002372
Sara Sharon10b2b202016-03-20 16:23:41 +02002373 if (start) {
2374 u8 baid;
2375
2376 mvm->rx_ba_sessions++;
2377
2378 if (!iwl_mvm_has_new_rx_api(mvm))
2379 return 0;
2380
2381 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2382 ret = -EINVAL;
2383 goto out_free;
2384 }
2385 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2386 IWL_ADD_STA_BAID_SHIFT);
2387 baid_data->baid = baid;
2388 baid_data->timeout = timeout;
2389 baid_data->last_rx = jiffies;
Wei Yongjun72c240f2016-07-12 11:40:57 +00002390 setup_timer(&baid_data->session_timer,
2391 iwl_mvm_rx_agg_session_expired,
2392 (unsigned long)&mvm->baid_map[baid]);
Sara Sharon10b2b202016-03-20 16:23:41 +02002393 baid_data->mvm = mvm;
2394 baid_data->tid = tid;
2395 baid_data->sta_id = mvm_sta->sta_id;
2396
2397 mvm_sta->tid_to_baid[tid] = baid;
2398 if (timeout)
2399 mod_timer(&baid_data->session_timer,
2400 TU_TO_EXP_TIME(timeout * 2));
2401
Sara Sharonb915c102016-03-23 16:32:02 +02002402 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2403 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002404 /*
2405 * protect the BA data with RCU to cover a case where our
2406 * internal RX sync mechanism will timeout (not that it's
2407 * supposed to happen) and we will free the session data while
2408 * RX is being processed in parallel
2409 */
Sara Sharon35263a02016-06-21 12:12:10 +03002410 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2411 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002412 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2413 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002414 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002415 u8 baid = mvm_sta->tid_to_baid[tid];
2416
Sara Sharon60dec522016-06-21 14:14:08 +03002417 if (mvm->rx_ba_sessions > 0)
2418 /* check that restart flow didn't zero the counter */
2419 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002420 if (!iwl_mvm_has_new_rx_api(mvm))
2421 return 0;
2422
2423 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2424 return -EINVAL;
2425
2426 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2427 if (WARN_ON(!baid_data))
2428 return -EINVAL;
2429
2430 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002431 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002432 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002433 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2434 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002435 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002436 }
2437 return 0;
2438
2439out_free:
2440 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002441 return ret;
2442}
2443
Liad Kaufman9794c642015-08-19 17:34:28 +03002444int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2445 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002446{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002447 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002448 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002449 int ret;
2450 u32 status;
2451
2452 lockdep_assert_held(&mvm->mutex);
2453
2454 if (start) {
2455 mvm_sta->tfd_queue_msk |= BIT(queue);
2456 mvm_sta->tid_disable_agg &= ~BIT(tid);
2457 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002458 /* In DQA-mode the queue isn't removed on agg termination */
2459 if (!iwl_mvm_is_dqa_supported(mvm))
2460 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002461 mvm_sta->tid_disable_agg |= BIT(tid);
2462 }
2463
2464 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2465 cmd.sta_id = mvm_sta->sta_id;
2466 cmd.add_modify = STA_MODE_MODIFY;
Sara Sharonbb497012016-09-29 14:52:40 +03002467 if (!iwl_mvm_has_new_tx_api(mvm))
2468 cmd.modify_mask = STA_MODIFY_QUEUES;
2469 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002470 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2471 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2472
2473 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002474 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2475 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002476 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002477 if (ret)
2478 return ret;
2479
Sara Sharon837c4da2016-01-07 16:50:45 +02002480 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002481 case ADD_STA_SUCCESS:
2482 break;
2483 default:
2484 ret = -EIO;
2485 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2486 start ? "start" : "stopp", status);
2487 break;
2488 }
2489
2490 return ret;
2491}
2492
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002493const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002494 IEEE80211_AC_BE,
2495 IEEE80211_AC_BK,
2496 IEEE80211_AC_BK,
2497 IEEE80211_AC_BE,
2498 IEEE80211_AC_VI,
2499 IEEE80211_AC_VI,
2500 IEEE80211_AC_VO,
2501 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002502 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002503};
2504
Johannes Berg3e56ead2013-02-15 22:23:18 +01002505static const u8 tid_to_ucode_ac[] = {
2506 AC_BE,
2507 AC_BK,
2508 AC_BK,
2509 AC_BE,
2510 AC_VI,
2511 AC_VI,
2512 AC_VO,
2513 AC_VO,
2514};
2515
Johannes Berg8ca151b2013-01-24 14:25:36 +01002516int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2517 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2518{
Johannes Berg5b577a92013-11-14 18:20:04 +01002519 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002520 struct iwl_mvm_tid_data *tid_data;
2521 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002522 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002523
2524 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2525 return -EINVAL;
2526
2527 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2528 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2529 mvmsta->tid_data[tid].state);
2530 return -ENXIO;
2531 }
2532
2533 lockdep_assert_held(&mvm->mutex);
2534
Arik Nemtsovb2492502014-03-13 12:21:50 +02002535 spin_lock_bh(&mvmsta->lock);
2536
2537 /* possible race condition - we entered D0i3 while starting agg */
2538 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2539 spin_unlock_bh(&mvmsta->lock);
2540 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2541 return -EIO;
2542 }
2543
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002544 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002545
Liad Kaufmancf961e12015-08-13 19:16:08 +03002546 /*
2547 * Note the possible cases:
2548 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2549 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2550 * one and mark it as reserved
2551 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2552 * non-DQA mode, since the TXQ hasn't yet been allocated
Sara Sharon34e10862017-02-23 13:15:07 +02002553 * Don't support case 3 for new TX path as it is not expected to happen
2554 * and aggregation will be offloaded soon anyway
Liad Kaufmancf961e12015-08-13 19:16:08 +03002555 */
2556 txq_id = mvmsta->tid_data[tid].txq_id;
Sara Sharon34e10862017-02-23 13:15:07 +02002557 if (iwl_mvm_has_new_tx_api(mvm)) {
2558 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2559 ret = -ENXIO;
2560 goto release_locks;
2561 }
2562 } else if (iwl_mvm_is_dqa_supported(mvm) &&
2563 unlikely(mvm->queue_info[txq_id].status ==
2564 IWL_MVM_QUEUE_SHARED)) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002565 ret = -ENXIO;
2566 IWL_DEBUG_TX_QUEUES(mvm,
2567 "Can't start tid %d agg on shared queue!\n",
2568 tid);
2569 goto release_locks;
2570 } else if (!iwl_mvm_is_dqa_supported(mvm) ||
Liad Kaufmancf961e12015-08-13 19:16:08 +03002571 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002572 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2573 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03002574 mvm->last_agg_queue);
2575 if (txq_id < 0) {
2576 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002577 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2578 goto release_locks;
2579 }
Sara Sharon01796ff2016-11-16 17:04:36 +02002580 /*
2581 * TXQ shouldn't be in inactive mode for non-DQA, so getting
2582 * an inactive queue from iwl_mvm_find_free_queue() is
2583 * certainly a bug
2584 */
2585 WARN_ON(mvm->queue_info[txq_id].status ==
2586 IWL_MVM_QUEUE_INACTIVE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002587
2588 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2589 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002590 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002591
2592 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002593
Liad Kaufmancf961e12015-08-13 19:16:08 +03002594 IWL_DEBUG_TX_QUEUES(mvm,
2595 "AGG for tid %d will be on queue #%d\n",
2596 tid, txq_id);
2597
Johannes Berg8ca151b2013-01-24 14:25:36 +01002598 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002599 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002600 tid_data->txq_id = txq_id;
2601 *ssn = tid_data->ssn;
2602
2603 IWL_DEBUG_TX_QUEUES(mvm,
2604 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2605 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2606 tid_data->next_reclaimed);
2607
2608 if (tid_data->ssn == tid_data->next_reclaimed) {
2609 tid_data->state = IWL_AGG_STARTING;
2610 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2611 } else {
2612 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2613 }
2614
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002615 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002616 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002617
2618release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002619 spin_unlock(&mvm->queue_info_lock);
2620out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002621 spin_unlock_bh(&mvmsta->lock);
2622
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002623 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002624}
2625
2626int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002627 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2628 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002629{
Johannes Berg5b577a92013-11-14 18:20:04 +01002630 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002631 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002632 unsigned int wdg_timeout =
2633 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002634 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002635 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002636 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002637 u16 ssn;
2638
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002639 struct iwl_trans_txq_scd_cfg cfg = {
2640 .sta_id = mvmsta->sta_id,
2641 .tid = tid,
2642 .frame_limit = buf_size,
2643 .aggregate = true,
2644 };
2645
Eyal Shapiraefed6642014-09-14 15:58:53 +03002646 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2647 != IWL_MAX_TID_COUNT);
2648
Johannes Berg8ca151b2013-01-24 14:25:36 +01002649 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2650
2651 spin_lock_bh(&mvmsta->lock);
2652 ssn = tid_data->ssn;
2653 queue = tid_data->txq_id;
2654 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002655 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002656 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002657 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002658 spin_unlock_bh(&mvmsta->lock);
2659
Sara Sharon34e10862017-02-23 13:15:07 +02002660 if (iwl_mvm_has_new_tx_api(mvm)) {
2661 /*
2662 * If no queue iwl_mvm_sta_tx_agg_start() would have failed so
2663 * no need to check queue's status
2664 */
2665 if (buf_size < mvmsta->max_agg_bufsize)
2666 return -ENOTSUPP;
2667
2668 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2669 if (ret)
2670 return -EIO;
2671 goto out;
2672 }
2673
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002674 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002675
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002676 spin_lock_bh(&mvm->queue_info_lock);
2677 queue_status = mvm->queue_info[queue].status;
2678 spin_unlock_bh(&mvm->queue_info_lock);
2679
Liad Kaufmancf961e12015-08-13 19:16:08 +03002680 /* In DQA mode, the existing queue might need to be reconfigured */
2681 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002682 /* Maybe there is no need to even alloc a queue... */
2683 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2684 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002685
2686 /*
2687 * Only reconfig the SCD for the queue if the window size has
2688 * changed from current (become smaller)
2689 */
2690 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2691 /*
2692 * If reconfiguring an existing queue, it first must be
2693 * drained
2694 */
Sara Sharona1a57872017-03-05 11:38:58 +02002695 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2696 BIT(queue));
Liad Kaufmancf961e12015-08-13 19:16:08 +03002697 if (ret) {
2698 IWL_ERR(mvm,
2699 "Error draining queue before reconfig\n");
2700 return ret;
2701 }
2702
2703 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2704 mvmsta->sta_id, tid,
2705 buf_size, ssn);
2706 if (ret) {
2707 IWL_ERR(mvm,
2708 "Error reconfiguring TXQ #%d\n", queue);
2709 return ret;
2710 }
2711 }
2712 }
2713
2714 if (alloc_queue)
2715 iwl_mvm_enable_txq(mvm, queue,
2716 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2717 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002718
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002719 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2720 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2721 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2722 if (ret)
2723 return -EIO;
2724 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002725
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002726 /* No need to mark as reserved */
2727 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002728 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002729 spin_unlock_bh(&mvm->queue_info_lock);
2730
Sara Sharon34e10862017-02-23 13:15:07 +02002731out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002732 /*
2733 * Even though in theory the peer could have different
2734 * aggregation reorder buffer sizes for different sessions,
2735 * our ucode doesn't allow for that and has a global limit
2736 * for each station. Therefore, use the minimum of all the
2737 * aggregation sessions and our default value.
2738 */
2739 mvmsta->max_agg_bufsize =
2740 min(mvmsta->max_agg_bufsize, buf_size);
2741 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2742
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002743 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2744 sta->addr, tid);
2745
Eyal Shapira9e680942013-11-09 00:16:16 +02002746 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002747}
2748
Sara Sharon34e10862017-02-23 13:15:07 +02002749static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2750 struct iwl_mvm_sta *mvmsta,
2751 u16 txq_id)
2752{
2753 if (iwl_mvm_has_new_tx_api(mvm))
2754 return;
2755
2756 spin_lock_bh(&mvm->queue_info_lock);
2757 /*
2758 * The TXQ is marked as reserved only if no traffic came through yet
2759 * This means no traffic has been sent on this TID (agg'd or not), so
2760 * we no longer have use for the queue. Since it hasn't even been
2761 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2762 * free.
2763 */
2764 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2765 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2766
2767 spin_unlock_bh(&mvm->queue_info_lock);
2768}
2769
Johannes Berg8ca151b2013-01-24 14:25:36 +01002770int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2771 struct ieee80211_sta *sta, u16 tid)
2772{
Johannes Berg5b577a92013-11-14 18:20:04 +01002773 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002774 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2775 u16 txq_id;
2776 int err;
2777
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002778 /*
2779 * If mac80211 is cleaning its state, then say that we finished since
2780 * our state has been cleared anyway.
2781 */
2782 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2783 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2784 return 0;
2785 }
2786
Johannes Berg8ca151b2013-01-24 14:25:36 +01002787 spin_lock_bh(&mvmsta->lock);
2788
2789 txq_id = tid_data->txq_id;
2790
2791 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2792 mvmsta->sta_id, tid, txq_id, tid_data->state);
2793
Eyal Shapiraefed6642014-09-14 15:58:53 +03002794 mvmsta->agg_tids &= ~BIT(tid);
2795
Sara Sharon34e10862017-02-23 13:15:07 +02002796 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002797
Johannes Berg8ca151b2013-01-24 14:25:36 +01002798 switch (tid_data->state) {
2799 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002800 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002801
2802 IWL_DEBUG_TX_QUEUES(mvm,
2803 "ssn = %d, next_recl = %d\n",
2804 tid_data->ssn, tid_data->next_reclaimed);
2805
2806 /* There are still packets for this RA / TID in the HW */
2807 if (tid_data->ssn != tid_data->next_reclaimed) {
2808 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2809 err = 0;
2810 break;
2811 }
2812
2813 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002814 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002815 spin_unlock_bh(&mvmsta->lock);
2816
2817 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2818
2819 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2820
Liad Kaufmancf961e12015-08-13 19:16:08 +03002821 if (!iwl_mvm_is_dqa_supported(mvm)) {
2822 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2823
2824 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2825 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002826 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002827 case IWL_AGG_STARTING:
2828 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2829 /*
2830 * The agg session has been stopped before it was set up. This
2831 * can happen when the AddBA timer times out for example.
2832 */
2833
2834 /* No barriers since we are under mutex */
2835 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002836
2837 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2838 tid_data->state = IWL_AGG_OFF;
2839 err = 0;
2840 break;
2841 default:
2842 IWL_ERR(mvm,
2843 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2844 mvmsta->sta_id, tid, tid_data->state);
2845 IWL_ERR(mvm,
2846 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2847 err = -EINVAL;
2848 }
2849
2850 spin_unlock_bh(&mvmsta->lock);
2851
2852 return err;
2853}
2854
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002855int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2856 struct ieee80211_sta *sta, u16 tid)
2857{
Johannes Berg5b577a92013-11-14 18:20:04 +01002858 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002859 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2860 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002861 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002862
2863 /*
2864 * First set the agg state to OFF to avoid calling
2865 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2866 */
2867 spin_lock_bh(&mvmsta->lock);
2868 txq_id = tid_data->txq_id;
2869 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2870 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002871 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002872 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002873 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002874 spin_unlock_bh(&mvmsta->lock);
2875
Sara Sharon34e10862017-02-23 13:15:07 +02002876 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002877
Johannes Bergb6658ff2013-07-24 13:55:51 +02002878 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002879 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03002880 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02002881 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002882
2883 if (iwl_mvm_has_new_tx_api(mvm))
2884 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
2885
2886 else
2887 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
2888
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002889 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002890
Johannes Bergf7f89e72014-08-05 15:24:44 +02002891 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2892
Liad Kaufmancf961e12015-08-13 19:16:08 +03002893 if (!iwl_mvm_is_dqa_supported(mvm)) {
2894 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2895
2896 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2897 tid, 0);
2898 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002899 }
2900
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002901 return 0;
2902}
2903
Johannes Berg8ca151b2013-01-24 14:25:36 +01002904static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2905{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002906 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002907
2908 lockdep_assert_held(&mvm->mutex);
2909
Johannes Berg2dc2a152015-06-16 17:09:18 +02002910 /* Pick the unused key offset with the highest 'deleted'
2911 * counter. Every time a key is deleted, all the counters
2912 * are incremented and the one that was just deleted is
2913 * reset to zero. Thus, the highest counter is the one
2914 * that was deleted longest ago. Pick that one.
2915 */
2916 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2917 if (test_bit(i, mvm->fw_key_table))
2918 continue;
2919 if (mvm->fw_key_deleted[i] > max) {
2920 max = mvm->fw_key_deleted[i];
2921 max_offs = i;
2922 }
2923 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002924
Johannes Berg2dc2a152015-06-16 17:09:18 +02002925 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002926 return STA_KEY_IDX_INVALID;
2927
Johannes Berg2dc2a152015-06-16 17:09:18 +02002928 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002929}
2930
Johannes Berg5f7a1842015-12-11 09:36:10 +01002931static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2932 struct ieee80211_vif *vif,
2933 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002934{
Johannes Berg5b530e92014-12-23 16:00:17 +01002935 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002936
Johannes Berg5f7a1842015-12-11 09:36:10 +01002937 if (sta)
2938 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002939
2940 /*
2941 * The device expects GTKs for station interfaces to be
2942 * installed as GTKs for the AP station. If we have no
2943 * station ID, then use AP's station ID.
2944 */
2945 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02002946 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Avri Altman9513c5e2015-10-19 16:29:11 +02002947 u8 sta_id = mvmvif->ap_sta_id;
2948
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002949 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2950 lockdep_is_held(&mvm->mutex));
2951
Avri Altman9513c5e2015-10-19 16:29:11 +02002952 /*
2953 * It is possible that the 'sta' parameter is NULL,
2954 * for example when a GTK is removed - the sta_id will then
2955 * be the AP ID, and no station was passed by mac80211.
2956 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002957 if (IS_ERR_OR_NULL(sta))
2958 return NULL;
2959
2960 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02002961 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002962
Johannes Berg5f7a1842015-12-11 09:36:10 +01002963 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002964}
2965
2966static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2967 struct iwl_mvm_sta *mvm_sta,
Sara Sharon45c458b2016-11-09 15:43:26 +02002968 struct ieee80211_key_conf *key, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002969 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2970 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002971{
Sara Sharon45c458b2016-11-09 15:43:26 +02002972 union {
2973 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2974 struct iwl_mvm_add_sta_key_cmd cmd;
2975 } u = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002976 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002977 int ret;
2978 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002979 u16 keyidx;
Sara Sharon45c458b2016-11-09 15:43:26 +02002980 u64 pn = 0;
2981 int i, size;
2982 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2983 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002984
Sara Sharon45c458b2016-11-09 15:43:26 +02002985 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
Johannes Berg8ca151b2013-01-24 14:25:36 +01002986 STA_KEY_FLG_KEYID_MSK;
2987 key_flags = cpu_to_le16(keyidx);
2988 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2989
Sara Sharon45c458b2016-11-09 15:43:26 +02002990 switch (key->cipher) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002991 case WLAN_CIPHER_SUITE_TKIP:
2992 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002993 if (new_api) {
2994 memcpy((void *)&u.cmd.tx_mic_key,
2995 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
2996 IWL_MIC_KEY_SIZE);
2997
2998 memcpy((void *)&u.cmd.rx_mic_key,
2999 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3000 IWL_MIC_KEY_SIZE);
3001 pn = atomic64_read(&key->tx_pn);
3002
3003 } else {
3004 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3005 for (i = 0; i < 5; i++)
3006 u.cmd_v1.tkip_rx_ttak[i] =
3007 cpu_to_le16(tkip_p1k[i]);
3008 }
3009 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003010 break;
3011 case WLAN_CIPHER_SUITE_CCMP:
3012 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Sara Sharon45c458b2016-11-09 15:43:26 +02003013 memcpy(u.cmd.common.key, key->key, key->keylen);
3014 if (new_api)
3015 pn = atomic64_read(&key->tx_pn);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003016 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01003017 case WLAN_CIPHER_SUITE_WEP104:
3018 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05003019 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01003020 case WLAN_CIPHER_SUITE_WEP40:
3021 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003022 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
Johannes Bergba3943b2014-11-12 23:54:48 +01003023 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03003024 case WLAN_CIPHER_SUITE_GCMP_256:
3025 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3026 /* fall through */
3027 case WLAN_CIPHER_SUITE_GCMP:
3028 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
Sara Sharon45c458b2016-11-09 15:43:26 +02003029 memcpy(u.cmd.common.key, key->key, key->keylen);
3030 if (new_api)
3031 pn = atomic64_read(&key->tx_pn);
Ayala Beker2a53d162016-04-07 16:21:57 +03003032 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003033 default:
Max Stepanove36e5432013-08-27 19:56:13 +03003034 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
Sara Sharon45c458b2016-11-09 15:43:26 +02003035 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003036 }
3037
Johannes Bergba3943b2014-11-12 23:54:48 +01003038 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003039 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3040
Sara Sharon45c458b2016-11-09 15:43:26 +02003041 u.cmd.common.key_offset = key_offset;
3042 u.cmd.common.key_flags = key_flags;
3043 u.cmd.common.sta_id = mvm_sta->sta_id;
3044
3045 if (new_api) {
3046 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3047 size = sizeof(u.cmd);
3048 } else {
3049 size = sizeof(u.cmd_v1);
3050 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003051
3052 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003053 if (cmd_flags & CMD_ASYNC)
Sara Sharon45c458b2016-11-09 15:43:26 +02003054 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3055 &u.cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003056 else
Sara Sharon45c458b2016-11-09 15:43:26 +02003057 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3058 &u.cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003059
3060 switch (status) {
3061 case ADD_STA_SUCCESS:
3062 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3063 break;
3064 default:
3065 ret = -EIO;
3066 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3067 break;
3068 }
3069
3070 return ret;
3071}
3072
3073static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3074 struct ieee80211_key_conf *keyconf,
3075 u8 sta_id, bool remove_key)
3076{
3077 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3078
3079 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03003080 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3081 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3082 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3083 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3084 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3085 return -EINVAL;
3086
3087 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3088 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01003089 return -EINVAL;
3090
3091 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3092 igtk_cmd.sta_id = cpu_to_le32(sta_id);
3093
3094 if (remove_key) {
3095 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3096 } else {
3097 struct ieee80211_key_seq seq;
3098 const u8 *pn;
3099
Ayala Bekeraa950522016-06-01 00:28:09 +03003100 switch (keyconf->cipher) {
3101 case WLAN_CIPHER_SUITE_AES_CMAC:
3102 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3103 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03003104 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3105 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3106 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3107 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03003108 default:
3109 return -EINVAL;
3110 }
3111
Ayala Beker8e160ab2016-04-11 11:37:38 +03003112 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3113 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3114 igtk_cmd.ctrl_flags |=
3115 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003116 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3117 pn = seq.aes_cmac.pn;
3118 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3119 ((u64) pn[4] << 8) |
3120 ((u64) pn[3] << 16) |
3121 ((u64) pn[2] << 24) |
3122 ((u64) pn[1] << 32) |
3123 ((u64) pn[0] << 40));
3124 }
3125
3126 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3127 remove_key ? "removing" : "installing",
3128 igtk_cmd.sta_id);
3129
Ayala Beker8e160ab2016-04-11 11:37:38 +03003130 if (!iwl_mvm_has_new_rx_api(mvm)) {
3131 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3132 .ctrl_flags = igtk_cmd.ctrl_flags,
3133 .key_id = igtk_cmd.key_id,
3134 .sta_id = igtk_cmd.sta_id,
3135 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3136 };
3137
3138 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3139 ARRAY_SIZE(igtk_cmd_v1.igtk));
3140 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3141 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3142 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03003143 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003144 sizeof(igtk_cmd), &igtk_cmd);
3145}
3146
3147
3148static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3149 struct ieee80211_vif *vif,
3150 struct ieee80211_sta *sta)
3151{
Johannes Berg5b530e92014-12-23 16:00:17 +01003152 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003153
3154 if (sta)
3155 return sta->addr;
3156
3157 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02003158 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003159 u8 sta_id = mvmvif->ap_sta_id;
3160 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3161 lockdep_is_held(&mvm->mutex));
3162 return sta->addr;
3163 }
3164
3165
3166 return NULL;
3167}
3168
Johannes Berg2f6319d2014-11-12 23:39:56 +01003169static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3170 struct ieee80211_vif *vif,
3171 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01003172 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003173 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01003174 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003175{
Johannes Berg2f6319d2014-11-12 23:39:56 +01003176 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003177 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003178 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003179 struct ieee80211_key_seq seq;
3180 u16 p1k[5];
3181
Johannes Berg8ca151b2013-01-24 14:25:36 +01003182 switch (keyconf->cipher) {
3183 case WLAN_CIPHER_SUITE_TKIP:
3184 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3185 /* get phase 1 key from mac80211 */
3186 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3187 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01003188 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003189 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003190 break;
3191 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01003192 case WLAN_CIPHER_SUITE_WEP40:
3193 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03003194 case WLAN_CIPHER_SUITE_GCMP:
3195 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergba3943b2014-11-12 23:54:48 +01003196 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003197 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003198 break;
3199 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01003200 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003201 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003202 }
3203
Johannes Berg8ca151b2013-01-24 14:25:36 +01003204 return ret;
3205}
3206
Johannes Berg2f6319d2014-11-12 23:39:56 +01003207static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01003208 struct ieee80211_key_conf *keyconf,
3209 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003210{
Sara Sharon45c458b2016-11-09 15:43:26 +02003211 union {
3212 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3213 struct iwl_mvm_add_sta_key_cmd cmd;
3214 } u = {};
3215 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3216 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003217 __le16 key_flags;
Sara Sharon45c458b2016-11-09 15:43:26 +02003218 int ret, size;
Johannes Berg79920742014-11-03 15:43:04 +01003219 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003220
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02003221 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3222 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003223 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3224 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3225
Johannes Bergba3943b2014-11-12 23:54:48 +01003226 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003227 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3228
Sara Sharon45c458b2016-11-09 15:43:26 +02003229 /*
3230 * The fields assigned here are in the same location at the start
3231 * of the command, so we can do this union trick.
3232 */
3233 u.cmd.common.key_flags = key_flags;
3234 u.cmd.common.key_offset = keyconf->hw_key_idx;
3235 u.cmd.common.sta_id = sta_id;
3236
3237 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003238
Johannes Berg8ca151b2013-01-24 14:25:36 +01003239 status = ADD_STA_SUCCESS;
Sara Sharon45c458b2016-11-09 15:43:26 +02003240 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3241 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003242
3243 switch (status) {
3244 case ADD_STA_SUCCESS:
3245 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3246 break;
3247 default:
3248 ret = -EIO;
3249 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3250 break;
3251 }
3252
3253 return ret;
3254}
3255
Johannes Berg2f6319d2014-11-12 23:39:56 +01003256int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3257 struct ieee80211_vif *vif,
3258 struct ieee80211_sta *sta,
3259 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003260 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003261{
Johannes Bergba3943b2014-11-12 23:54:48 +01003262 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003263 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003264 u8 sta_id;
3265 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03003266 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01003267
3268 lockdep_assert_held(&mvm->mutex);
3269
3270 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01003271 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3272 if (!mvm_sta) {
3273 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01003274 return -EINVAL;
3275 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01003276 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003277
Ayala Beker8e160ab2016-04-11 11:37:38 +03003278 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3279 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3280 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003281 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3282 goto end;
3283 }
3284
3285 /*
3286 * It is possible that the 'sta' parameter is NULL, and thus
3287 * there is a need to retrieve the sta from the local station table.
3288 */
3289 if (!sta) {
3290 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3291 lockdep_is_held(&mvm->mutex));
3292 if (IS_ERR_OR_NULL(sta)) {
3293 IWL_ERR(mvm, "Invalid station id\n");
3294 return -EINVAL;
3295 }
3296 }
3297
3298 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3299 return -EINVAL;
3300
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003301 /* If the key_offset is not pre-assigned, we need to find a
3302 * new offset to use. In normal cases, the offset is not
3303 * pre-assigned, but during HW_RESTART we want to reuse the
3304 * same indices, so we pass them when this function is called.
3305 *
3306 * In D3 entry, we need to hardcoded the indices (because the
3307 * firmware hardcodes the PTK offset to 0). In this case, we
3308 * need to make sure we don't overwrite the hw_key_idx in the
3309 * keyconf structure, because otherwise we cannot configure
3310 * the original ones back when resuming.
3311 */
3312 if (key_offset == STA_KEY_IDX_INVALID) {
3313 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3314 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003315 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003316 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003317 }
3318
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003319 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003320 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01003321 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003322
3323 /*
3324 * For WEP, the same key is used for multicast and unicast. Upload it
3325 * again, using the same key offset, and now pointing the other one
3326 * to the same key slot (offset).
3327 * If this fails, remove the original as well.
3328 */
3329 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3330 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003331 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3332 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01003333 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01003334 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003335 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003336 }
3337 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003338
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003339 __set_bit(key_offset, mvm->fw_key_table);
3340
Johannes Berg2f6319d2014-11-12 23:39:56 +01003341end:
3342 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3343 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03003344 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01003345 return ret;
3346}
3347
3348int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3349 struct ieee80211_vif *vif,
3350 struct ieee80211_sta *sta,
3351 struct ieee80211_key_conf *keyconf)
3352{
Johannes Bergba3943b2014-11-12 23:54:48 +01003353 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003354 struct iwl_mvm_sta *mvm_sta;
Sara Sharon0ae98812017-01-04 14:53:58 +02003355 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2dc2a152015-06-16 17:09:18 +02003356 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003357
3358 lockdep_assert_held(&mvm->mutex);
3359
Johannes Berg5f7a1842015-12-11 09:36:10 +01003360 /* Get the station from the mvm local station table */
3361 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Ilan Peercd4d23c2017-01-16 15:07:03 +02003362 if (!mvm_sta) {
3363 IWL_ERR(mvm, "Failed to find station\n");
3364 return -EINVAL;
3365 }
3366 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003367
3368 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3369 keyconf->keyidx, sta_id);
3370
Ayala Beker8e160ab2016-04-11 11:37:38 +03003371 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3372 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3373 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003374 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3375
3376 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3377 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3378 keyconf->hw_key_idx);
3379 return -ENOENT;
3380 }
3381
Johannes Berg2dc2a152015-06-16 17:09:18 +02003382 /* track which key was deleted last */
3383 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3384 if (mvm->fw_key_deleted[i] < U8_MAX)
3385 mvm->fw_key_deleted[i]++;
3386 }
3387 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3388
Johannes Berg5f7a1842015-12-11 09:36:10 +01003389 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003390 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3391 return 0;
3392 }
3393
Johannes Bergba3943b2014-11-12 23:54:48 +01003394 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3395 if (ret)
3396 return ret;
3397
3398 /* delete WEP key twice to get rid of (now useless) offset */
3399 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3400 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3401 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3402
3403 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003404}
3405
Johannes Berg8ca151b2013-01-24 14:25:36 +01003406void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3407 struct ieee80211_vif *vif,
3408 struct ieee80211_key_conf *keyconf,
3409 struct ieee80211_sta *sta, u32 iv32,
3410 u16 *phase1key)
3411{
Beni Levc3eb5362013-02-06 17:22:18 +02003412 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01003413 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003414
Beni Levc3eb5362013-02-06 17:22:18 +02003415 rcu_read_lock();
3416
Johannes Berg5f7a1842015-12-11 09:36:10 +01003417 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3418 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003419 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01003420 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003421 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003422
3423 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02003424 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01003425}
3426
Johannes Berg9cc40712013-02-15 22:47:48 +01003427void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3428 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003429{
Johannes Berg5b577a92013-11-14 18:20:04 +01003430 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003431 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003432 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003433 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03003434 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01003435 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003436 };
3437 int ret;
3438
Sara Sharon854c5702016-01-26 13:17:47 +02003439 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3440 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003441 if (ret)
3442 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3443}
3444
Johannes Berg9cc40712013-02-15 22:47:48 +01003445void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3446 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003447 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003448 u16 cnt, u16 tids, bool more_data,
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003449 bool single_sta_queue)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003450{
Johannes Berg5b577a92013-11-14 18:20:04 +01003451 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003452 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003453 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003454 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003455 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3456 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003457 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003458 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003459 int tid, ret;
3460 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003461
Johannes Berg3e56ead2013-02-15 22:23:18 +01003462 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3463 * Note that this field is reserved and unused by firmware not
3464 * supporting GO uAPSD, so it's safe to always do this.
3465 */
3466 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3467 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3468
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003469 /* If we're releasing frames from aggregation or dqa queues then check
3470 * if all the queues that we're releasing frames from, combined, have:
Johannes Berg3e56ead2013-02-15 22:23:18 +01003471 * - more frames than the service period, in which case more_data
3472 * needs to be set
3473 * - fewer than 'cnt' frames, in which case we need to adjust the
3474 * firmware command (but do that unconditionally)
3475 */
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003476 if (single_sta_queue) {
Johannes Berg3e56ead2013-02-15 22:23:18 +01003477 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003478 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003479
3480 spin_lock_bh(&mvmsta->lock);
3481 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3482 struct iwl_mvm_tid_data *tid_data;
3483 u16 n_queued;
3484
3485 tid_data = &mvmsta->tid_data[tid];
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003486 if (WARN(!iwl_mvm_is_dqa_supported(mvm) &&
3487 tid_data->state != IWL_AGG_ON &&
Johannes Berg3e56ead2013-02-15 22:23:18 +01003488 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3489 "TID %d state is %d\n",
3490 tid, tid_data->state)) {
3491 spin_unlock_bh(&mvmsta->lock);
3492 ieee80211_sta_eosp(sta);
3493 return;
3494 }
3495
3496 n_queued = iwl_mvm_tid_queued(tid_data);
3497 if (n_queued > remaining) {
3498 more_data = true;
3499 remaining = 0;
3500 break;
3501 }
3502 remaining -= n_queued;
3503 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003504 sleep_tx_count = cnt - remaining;
3505 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3506 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003507 spin_unlock_bh(&mvmsta->lock);
3508
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003509 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003510 if (WARN_ON(cnt - remaining == 0)) {
3511 ieee80211_sta_eosp(sta);
3512 return;
3513 }
3514 }
3515
3516 /* Note: this is ignored by firmware not supporting GO uAPSD */
3517 if (more_data)
Sara Sharonced19f22017-02-06 19:09:32 +02003518 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003519
3520 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3521 mvmsta->next_status_eosp = true;
Sara Sharonced19f22017-02-06 19:09:32 +02003522 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003523 } else {
Sara Sharonced19f22017-02-06 19:09:32 +02003524 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003525 }
3526
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003527 /* block the Tx queues until the FW updated the sleep Tx count */
3528 iwl_trans_block_txq_ptrs(mvm->trans, true);
3529
3530 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3531 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003532 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003533 if (ret)
3534 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3535}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003536
Johannes Berg04168412015-06-23 21:22:09 +02003537void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3538 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003539{
3540 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3541 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3542 struct ieee80211_sta *sta;
3543 u32 sta_id = le32_to_cpu(notif->sta_id);
3544
3545 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003546 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003547
3548 rcu_read_lock();
3549 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3550 if (!IS_ERR_OR_NULL(sta))
3551 ieee80211_sta_eosp(sta);
3552 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003553}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003554
3555void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3556 struct iwl_mvm_sta *mvmsta, bool disable)
3557{
3558 struct iwl_mvm_add_sta_cmd cmd = {
3559 .add_modify = STA_MODE_MODIFY,
3560 .sta_id = mvmsta->sta_id,
3561 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3562 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3563 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3564 };
3565 int ret;
3566
Sara Sharon854c5702016-01-26 13:17:47 +02003567 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3568 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003569 if (ret)
3570 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3571}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003572
3573void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3574 struct ieee80211_sta *sta,
3575 bool disable)
3576{
3577 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3578
3579 spin_lock_bh(&mvm_sta->lock);
3580
3581 if (mvm_sta->disable_tx == disable) {
3582 spin_unlock_bh(&mvm_sta->lock);
3583 return;
3584 }
3585
3586 mvm_sta->disable_tx = disable;
3587
3588 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03003589 * Tell mac80211 to start/stop queuing tx for this station,
3590 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003591 * for this station.
3592 */
3593 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3594 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3595
3596 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3597
3598 spin_unlock_bh(&mvm_sta->lock);
3599}
3600
Sara Sharonced19f22017-02-06 19:09:32 +02003601static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3602 struct iwl_mvm_vif *mvmvif,
3603 struct iwl_mvm_int_sta *sta,
3604 bool disable)
3605{
3606 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3607 struct iwl_mvm_add_sta_cmd cmd = {
3608 .add_modify = STA_MODE_MODIFY,
3609 .sta_id = sta->sta_id,
3610 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3611 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3612 .mac_id_n_color = cpu_to_le32(id),
3613 };
3614 int ret;
3615
3616 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3617 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3618 if (ret)
3619 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3620}
3621
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003622void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3623 struct iwl_mvm_vif *mvmvif,
3624 bool disable)
3625{
3626 struct ieee80211_sta *sta;
3627 struct iwl_mvm_sta *mvm_sta;
3628 int i;
3629
3630 lockdep_assert_held(&mvm->mutex);
3631
3632 /* Block/unblock all the stations of the given mvmvif */
Sara Sharon0ae98812017-01-04 14:53:58 +02003633 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003634 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3635 lockdep_is_held(&mvm->mutex));
3636 if (IS_ERR_OR_NULL(sta))
3637 continue;
3638
3639 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3640 if (mvm_sta->mac_id_n_color !=
3641 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3642 continue;
3643
3644 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3645 }
Sara Sharonced19f22017-02-06 19:09:32 +02003646
3647 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3648 return;
3649
3650 /* Need to block/unblock also multicast station */
3651 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3652 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3653 &mvmvif->mcast_sta, disable);
3654
3655 /*
3656 * Only unblock the broadcast station (FW blocks it for immediate
3657 * quiet, not the driver)
3658 */
3659 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3660 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3661 &mvmvif->bcast_sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003662}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003663
3664void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3665{
3666 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3667 struct iwl_mvm_sta *mvmsta;
3668
3669 rcu_read_lock();
3670
3671 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3672
3673 if (!WARN_ON(!mvmsta))
3674 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3675
3676 rcu_read_unlock();
3677}