blob: 922cd5379841f0ca7486e9f0f765c0f28437b7e7 [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]);
Emmanuel Grumbach61dd8a82017-06-12 15:10:09 +0300280
281 /*
282 * sta should be valid unless the following happens:
283 * The firmware asserts which triggers a reconfig flow, but
284 * the reconfig fails before we set the pointer to sta into
285 * the fw_id_to_mac_id pointer table. Mac80211 can't stop
286 * A-MDPU and hence the timer continues to run. Then, the
287 * timer expires and sta is NULL.
288 */
289 if (!sta)
290 goto unlock;
291
Sara Sharon10b2b202016-03-20 16:23:41 +0200292 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
293 ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
294 sta->addr, ba_data->tid);
295unlock:
296 rcu_read_unlock();
297}
298
Liad Kaufman9794c642015-08-19 17:34:28 +0300299/* Disable aggregations for a bitmap of TIDs for a given station */
300static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
301 unsigned long disable_agg_tids,
302 bool remove_queue)
303{
304 struct iwl_mvm_add_sta_cmd cmd = {};
305 struct ieee80211_sta *sta;
306 struct iwl_mvm_sta *mvmsta;
307 u32 status;
308 u8 sta_id;
309 int ret;
310
Sara Sharonbb497012016-09-29 14:52:40 +0300311 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
312 return -EINVAL;
313
Liad Kaufman9794c642015-08-19 17:34:28 +0300314 spin_lock_bh(&mvm->queue_info_lock);
315 sta_id = mvm->queue_info[queue].ra_sta_id;
316 spin_unlock_bh(&mvm->queue_info_lock);
317
318 rcu_read_lock();
319
320 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
321
322 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
323 rcu_read_unlock();
324 return -EINVAL;
325 }
326
327 mvmsta = iwl_mvm_sta_from_mac80211(sta);
328
329 mvmsta->tid_disable_agg |= disable_agg_tids;
330
331 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
332 cmd.sta_id = mvmsta->sta_id;
333 cmd.add_modify = STA_MODE_MODIFY;
334 cmd.modify_mask = STA_MODIFY_QUEUES;
335 if (disable_agg_tids)
336 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
337 if (remove_queue)
338 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
339 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
340 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
341
342 rcu_read_unlock();
343
344 /* Notify FW of queue removal from the STA queues */
345 status = ADD_STA_SUCCESS;
346 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
347 iwl_mvm_add_sta_cmd_size(mvm),
348 &cmd, &status);
349
350 return ret;
351}
352
Liad Kaufman42db09c2016-05-02 14:01:14 +0300353static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
354{
355 struct ieee80211_sta *sta;
356 struct iwl_mvm_sta *mvmsta;
357 unsigned long tid_bitmap;
358 unsigned long agg_tids = 0;
Sharon Dvir806911d2017-05-21 12:09:49 +0300359 u8 sta_id;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300360 int tid;
361
362 lockdep_assert_held(&mvm->mutex);
363
Sara Sharonbb497012016-09-29 14:52:40 +0300364 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
365 return -EINVAL;
366
Liad Kaufman42db09c2016-05-02 14:01:14 +0300367 spin_lock_bh(&mvm->queue_info_lock);
368 sta_id = mvm->queue_info[queue].ra_sta_id;
369 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
370 spin_unlock_bh(&mvm->queue_info_lock);
371
372 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
373 lockdep_is_held(&mvm->mutex));
374
375 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
376 return -EINVAL;
377
378 mvmsta = iwl_mvm_sta_from_mac80211(sta);
379
380 spin_lock_bh(&mvmsta->lock);
381 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
382 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
383 agg_tids |= BIT(tid);
384 }
385 spin_unlock_bh(&mvmsta->lock);
386
387 return agg_tids;
388}
389
Liad Kaufman9794c642015-08-19 17:34:28 +0300390/*
391 * Remove a queue from a station's resources.
392 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
393 * doesn't disable the queue
394 */
395static int iwl_mvm_remove_sta_queue_marking(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 disable_agg_tids = 0;
401 u8 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 Kaufman9794c642015-08-19 17:34:28 +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 rcu_read_lock();
415
416 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
417
418 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
419 rcu_read_unlock();
420 return 0;
421 }
422
423 mvmsta = iwl_mvm_sta_from_mac80211(sta);
424
425 spin_lock_bh(&mvmsta->lock);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300426 /* Unmap MAC queues and TIDs from this queue */
Liad Kaufman9794c642015-08-19 17:34:28 +0300427 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300428 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
429 disable_agg_tids |= BIT(tid);
Sara Sharon6862fce2017-02-22 19:34:17 +0200430 mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman9794c642015-08-19 17:34:28 +0300431 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300432
Liad Kaufman42db09c2016-05-02 14:01:14 +0300433 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
Liad Kaufman9794c642015-08-19 17:34:28 +0300434 spin_unlock_bh(&mvmsta->lock);
435
436 rcu_read_unlock();
437
Liad Kaufman9794c642015-08-19 17:34:28 +0300438 return disable_agg_tids;
439}
440
Sara Sharon01796ff2016-11-16 17:04:36 +0200441static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
442 bool same_sta)
443{
444 struct iwl_mvm_sta *mvmsta;
445 u8 txq_curr_ac, sta_id, tid;
446 unsigned long disable_agg_tids = 0;
447 int ret;
448
449 lockdep_assert_held(&mvm->mutex);
450
Sara Sharonbb497012016-09-29 14:52:40 +0300451 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
452 return -EINVAL;
453
Sara Sharon01796ff2016-11-16 17:04:36 +0200454 spin_lock_bh(&mvm->queue_info_lock);
455 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
456 sta_id = mvm->queue_info[queue].ra_sta_id;
457 tid = mvm->queue_info[queue].txq_tid;
458 spin_unlock_bh(&mvm->queue_info_lock);
459
460 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Sharon Dvire3df1e42017-02-21 10:41:31 +0200461 if (WARN_ON(!mvmsta))
462 return -EINVAL;
Sara Sharon01796ff2016-11-16 17:04:36 +0200463
464 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
465 /* Disable the queue */
466 if (disable_agg_tids)
467 iwl_mvm_invalidate_sta_queue(mvm, queue,
468 disable_agg_tids, false);
469
470 ret = iwl_mvm_disable_txq(mvm, queue,
471 mvmsta->vif->hw_queue[txq_curr_ac],
472 tid, 0);
473 if (ret) {
474 /* Re-mark the inactive queue as inactive */
475 spin_lock_bh(&mvm->queue_info_lock);
476 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
477 spin_unlock_bh(&mvm->queue_info_lock);
478 IWL_ERR(mvm,
479 "Failed to free inactive queue %d (ret=%d)\n",
480 queue, ret);
481
482 return ret;
483 }
484
485 /* If TXQ is allocated to another STA, update removal in FW */
486 if (!same_sta)
487 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
488
489 return 0;
490}
491
Liad Kaufman42db09c2016-05-02 14:01:14 +0300492static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
493 unsigned long tfd_queue_mask, u8 ac)
494{
495 int queue = 0;
496 u8 ac_to_queue[IEEE80211_NUM_ACS];
497 int i;
498
499 lockdep_assert_held(&mvm->queue_info_lock);
Sara Sharonbb497012016-09-29 14:52:40 +0300500 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
501 return -EINVAL;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300502
503 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
504
505 /* See what ACs the existing queues for this STA have */
506 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
507 /* Only DATA queues can be shared */
508 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
509 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
510 continue;
511
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200512 /* Don't try and take queues being reconfigured */
513 if (mvm->queue_info[queue].status ==
514 IWL_MVM_QUEUE_RECONFIGURING)
515 continue;
516
Liad Kaufman42db09c2016-05-02 14:01:14 +0300517 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
518 }
519
520 /*
521 * The queue to share is chosen only from DATA queues as follows (in
522 * descending priority):
523 * 1. An AC_BE queue
524 * 2. Same AC queue
525 * 3. Highest AC queue that is lower than new AC
526 * 4. Any existing AC (there always is at least 1 DATA queue)
527 */
528
529 /* Priority 1: An AC_BE queue */
530 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
531 queue = ac_to_queue[IEEE80211_AC_BE];
532 /* Priority 2: Same AC queue */
533 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
534 queue = ac_to_queue[ac];
535 /* Priority 3a: If new AC is VO and VI exists - use VI */
536 else if (ac == IEEE80211_AC_VO &&
537 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
538 queue = ac_to_queue[IEEE80211_AC_VI];
539 /* Priority 3b: No BE so only AC less than the new one is BK */
540 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
541 queue = ac_to_queue[IEEE80211_AC_BK];
542 /* Priority 4a: No BE nor BK - use VI if exists */
543 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
544 queue = ac_to_queue[IEEE80211_AC_VI];
545 /* Priority 4b: No BE, BK nor VI - use VO if exists */
546 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
547 queue = ac_to_queue[IEEE80211_AC_VO];
548
549 /* Make sure queue found (or not) is legal */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200550 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
551 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
552 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
Liad Kaufman42db09c2016-05-02 14:01:14 +0300553 IWL_ERR(mvm, "No DATA queues available to share\n");
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200554 return -ENOSPC;
555 }
556
557 /* Make sure the queue isn't in the middle of being reconfigured */
558 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
559 IWL_ERR(mvm,
560 "TXQ %d is in the middle of re-config - try again\n",
561 queue);
562 return -EBUSY;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300563 }
564
565 return queue;
566}
567
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200568/*
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200569 * If a given queue has a higher AC than the TID stream that is being compared
570 * to, the queue needs to be redirected to the lower AC. This function does that
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200571 * in such a case, otherwise - if no redirection required - it does nothing,
572 * unless the %force param is true.
573 */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200574int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
575 int ac, int ssn, unsigned int wdg_timeout,
576 bool force)
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200577{
578 struct iwl_scd_txq_cfg_cmd cmd = {
579 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200580 .action = SCD_CFG_DISABLE_QUEUE,
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200581 };
582 bool shared_queue;
583 unsigned long mq;
584 int ret;
585
Sara Sharonbb497012016-09-29 14:52:40 +0300586 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
587 return -EINVAL;
588
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200589 /*
590 * If the AC is lower than current one - FIFO needs to be redirected to
591 * the lowest one of the streams in the queue. Check if this is needed
592 * here.
593 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
594 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
595 * we need to check if the numerical value of X is LARGER than of Y.
596 */
597 spin_lock_bh(&mvm->queue_info_lock);
598 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
599 spin_unlock_bh(&mvm->queue_info_lock);
600
601 IWL_DEBUG_TX_QUEUES(mvm,
602 "No redirection needed on TXQ #%d\n",
603 queue);
604 return 0;
605 }
606
607 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
608 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200609 cmd.tid = mvm->queue_info[queue].txq_tid;
Sara Sharon34e10862017-02-23 13:15:07 +0200610 mq = mvm->hw_queue_to_mac80211[queue];
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200611 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
612 spin_unlock_bh(&mvm->queue_info_lock);
613
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200614 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200615 queue, iwl_mvm_ac_to_tx_fifo[ac]);
616
617 /* Stop MAC queues and wait for this queue to empty */
618 iwl_mvm_stop_mac_queues(mvm, mq);
Sara Sharona1a57872017-03-05 11:38:58 +0200619 ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200620 if (ret) {
621 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
622 queue);
623 ret = -EIO;
624 goto out;
625 }
626
627 /* Before redirecting the queue we need to de-activate it */
628 iwl_trans_txq_disable(mvm->trans, queue, false);
629 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
630 if (ret)
631 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
632 ret);
633
634 /* Make sure the SCD wrptr is correctly set before reconfiguring */
Sara Sharonca3b9c62016-06-30 16:14:02 +0300635 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200636
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200637 /* Update the TID "owner" of the queue */
638 spin_lock_bh(&mvm->queue_info_lock);
639 mvm->queue_info[queue].txq_tid = tid;
640 spin_unlock_bh(&mvm->queue_info_lock);
641
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200642 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
643
644 /* Redirect to lower AC */
645 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
646 cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
647 ssn);
648
649 /* Update AC marking of the queue */
650 spin_lock_bh(&mvm->queue_info_lock);
651 mvm->queue_info[queue].mac80211_ac = ac;
652 spin_unlock_bh(&mvm->queue_info_lock);
653
654 /*
655 * Mark queue as shared in transport if shared
656 * Note this has to be done after queue enablement because enablement
657 * can also set this value, and there is no indication there to shared
658 * queues
659 */
660 if (shared_queue)
661 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
662
663out:
664 /* Continue using the MAC queues */
665 iwl_mvm_start_mac_queues(mvm, mq);
666
667 return ret;
668}
669
Sara Sharon310181e2017-01-17 14:27:48 +0200670static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
671 struct ieee80211_sta *sta, u8 ac,
672 int tid)
673{
674 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
675 unsigned int wdg_timeout =
676 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
677 u8 mac_queue = mvmsta->vif->hw_queue[ac];
678 int queue = -1;
679
680 lockdep_assert_held(&mvm->mutex);
681
682 IWL_DEBUG_TX_QUEUES(mvm,
683 "Allocating queue for sta %d on tid %d\n",
684 mvmsta->sta_id, tid);
685 queue = iwl_mvm_tvqm_enable_txq(mvm, mac_queue, mvmsta->sta_id, tid,
686 wdg_timeout);
687 if (queue < 0)
688 return queue;
689
690 IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
691
692 spin_lock_bh(&mvmsta->lock);
693 mvmsta->tid_data[tid].txq_id = queue;
694 mvmsta->tid_data[tid].is_tid_active = true;
Sara Sharon310181e2017-01-17 14:27:48 +0200695 spin_unlock_bh(&mvmsta->lock);
696
Sara Sharon310181e2017-01-17 14:27:48 +0200697 return 0;
698}
699
Liad Kaufman24afba72015-07-28 18:56:08 +0300700static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
701 struct ieee80211_sta *sta, u8 ac, int tid,
702 struct ieee80211_hdr *hdr)
703{
704 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
705 struct iwl_trans_txq_scd_cfg cfg = {
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +0300706 .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
Liad Kaufman24afba72015-07-28 18:56:08 +0300707 .sta_id = mvmsta->sta_id,
708 .tid = tid,
709 .frame_limit = IWL_FRAME_LIMIT,
710 };
711 unsigned int wdg_timeout =
712 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
713 u8 mac_queue = mvmsta->vif->hw_queue[ac];
714 int queue = -1;
Sara Sharon01796ff2016-11-16 17:04:36 +0200715 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman9794c642015-08-19 17:34:28 +0300716 unsigned long disable_agg_tids = 0;
717 enum iwl_mvm_agg_state queue_state;
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300718 bool shared_queue = false, inc_ssn;
Liad Kaufman24afba72015-07-28 18:56:08 +0300719 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300720 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300721 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300722
723 lockdep_assert_held(&mvm->mutex);
724
Sara Sharon310181e2017-01-17 14:27:48 +0200725 if (iwl_mvm_has_new_tx_api(mvm))
726 return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
727
Liad Kaufman42db09c2016-05-02 14:01:14 +0300728 spin_lock_bh(&mvmsta->lock);
729 tfd_queue_mask = mvmsta->tfd_queue_msk;
730 spin_unlock_bh(&mvmsta->lock);
731
Liad Kaufmand2515a92016-03-23 16:31:08 +0200732 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300733
734 /*
735 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
736 * exists
737 */
738 if (!ieee80211_is_data_qos(hdr->frame_control) ||
739 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300740 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
741 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300742 IWL_MVM_DQA_MAX_MGMT_QUEUE);
743 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
744 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
745 queue);
746
747 /* If no such queue is found, we'll use a DATA queue instead */
748 }
749
Liad Kaufman9794c642015-08-19 17:34:28 +0300750 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
751 (mvm->queue_info[mvmsta->reserved_queue].status ==
752 IWL_MVM_QUEUE_RESERVED ||
753 mvm->queue_info[mvmsta->reserved_queue].status ==
754 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300755 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300756 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300757 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
758 }
759
760 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300761 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
762 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300763 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300764
765 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300766 * Check if this queue is already allocated but inactive.
767 * In such a case, we'll need to first free this queue before enabling
768 * it again, so we'll mark it as reserved to make sure no new traffic
769 * arrives on it
770 */
771 if (queue > 0 &&
772 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
773 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
774 using_inactive_queue = true;
Sara Sharon01796ff2016-11-16 17:04:36 +0200775 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman9794c642015-08-19 17:34:28 +0300776 IWL_DEBUG_TX_QUEUES(mvm,
777 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
778 queue, mvmsta->sta_id, tid);
779 }
780
Liad Kaufman42db09c2016-05-02 14:01:14 +0300781 /* No free queue - we'll have to share */
782 if (queue <= 0) {
783 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
784 if (queue > 0) {
785 shared_queue = true;
786 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
787 }
788 }
789
Liad Kaufman9794c642015-08-19 17:34:28 +0300790 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300791 * Mark TXQ as ready, even though it hasn't been fully configured yet,
792 * to make sure no one else takes it.
793 * This will allow avoiding re-acquiring the lock at the end of the
794 * configuration. On error we'll mark it back as free.
795 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300796 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300797 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300798
Liad Kaufmand2515a92016-03-23 16:31:08 +0200799 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300800
Liad Kaufman42db09c2016-05-02 14:01:14 +0300801 /* This shouldn't happen - out of queues */
802 if (WARN_ON(queue <= 0)) {
803 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
804 tid, cfg.sta_id);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200805 return queue;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300806 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300807
808 /*
809 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
810 * but for configuring the SCD to send A-MPDUs we need to mark the queue
811 * as aggregatable.
812 * Mark all DATA queues as allowing to be aggregated at some point
813 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300814 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
815 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300816
Liad Kaufman9794c642015-08-19 17:34:28 +0300817 /*
818 * If this queue was previously inactive (idle) - we need to free it
819 * first
820 */
821 if (using_inactive_queue) {
Sara Sharon01796ff2016-11-16 17:04:36 +0200822 ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
823 if (ret)
Liad Kaufman9794c642015-08-19 17:34:28 +0300824 return ret;
Liad Kaufman9794c642015-08-19 17:34:28 +0300825 }
826
Liad Kaufman42db09c2016-05-02 14:01:14 +0300827 IWL_DEBUG_TX_QUEUES(mvm,
828 "Allocating %squeue #%d to sta %d on tid %d\n",
829 shared_queue ? "shared " : "", queue,
830 mvmsta->sta_id, tid);
831
832 if (shared_queue) {
833 /* Disable any open aggs on this queue */
834 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
835
836 if (disable_agg_tids) {
837 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
838 queue);
839 iwl_mvm_invalidate_sta_queue(mvm, queue,
840 disable_agg_tids, false);
841 }
Liad Kaufman42db09c2016-05-02 14:01:14 +0300842 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300843
844 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300845 inc_ssn = iwl_mvm_enable_txq(mvm, queue, mac_queue,
846 ssn, &cfg, wdg_timeout);
847 if (inc_ssn) {
848 ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
849 le16_add_cpu(&hdr->seq_ctrl, 0x10);
850 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300851
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200852 /*
853 * Mark queue as shared in transport if shared
854 * Note this has to be done after queue enablement because enablement
855 * can also set this value, and there is no indication there to shared
856 * queues
857 */
858 if (shared_queue)
859 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
860
Liad Kaufman24afba72015-07-28 18:56:08 +0300861 spin_lock_bh(&mvmsta->lock);
Emmanuel Grumbachdcfbd672017-05-07 15:00:31 +0300862 /*
863 * This looks racy, but it is not. We have only one packet for
864 * this ra/tid in our Tx path since we stop the Qdisc when we
865 * need to allocate a new TFD queue.
866 */
867 if (inc_ssn)
868 mvmsta->tid_data[tid].seq_number += 0x10;
Liad Kaufman24afba72015-07-28 18:56:08 +0300869 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300870 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300871 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300872 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300873
874 if (mvmsta->reserved_queue == queue)
875 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
876 spin_unlock_bh(&mvmsta->lock);
877
Liad Kaufman42db09c2016-05-02 14:01:14 +0300878 if (!shared_queue) {
879 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
880 if (ret)
881 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300882
Liad Kaufman42db09c2016-05-02 14:01:14 +0300883 /* If we need to re-enable aggregations... */
884 if (queue_state == IWL_AGG_ON) {
885 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
886 if (ret)
887 goto out_err;
888 }
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200889 } else {
890 /* Redirect queue, if needed */
891 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
892 wdg_timeout, false);
893 if (ret)
894 goto out_err;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300895 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300896
Liad Kaufman42db09c2016-05-02 14:01:14 +0300897 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300898
899out_err:
900 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
901
902 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300903}
904
Liad Kaufman19aefa42016-03-08 14:29:51 +0200905static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
906{
907 struct iwl_scd_txq_cfg_cmd cmd = {
908 .scd_queue = queue,
909 .action = SCD_CFG_UPDATE_QUEUE_TID,
910 };
Liad Kaufman19aefa42016-03-08 14:29:51 +0200911 int tid;
912 unsigned long tid_bitmap;
913 int ret;
914
915 lockdep_assert_held(&mvm->mutex);
916
Sara Sharonbb497012016-09-29 14:52:40 +0300917 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
918 return;
919
Liad Kaufman19aefa42016-03-08 14:29:51 +0200920 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200921 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
922 spin_unlock_bh(&mvm->queue_info_lock);
923
924 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
925 return;
926
927 /* Find any TID for queue */
928 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
929 cmd.tid = tid;
930 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
931
932 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
Liad Kaufman341ca402016-09-18 14:51:59 +0300933 if (ret) {
Liad Kaufman19aefa42016-03-08 14:29:51 +0200934 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
935 queue, ret);
Liad Kaufman341ca402016-09-18 14:51:59 +0300936 return;
937 }
938
939 spin_lock_bh(&mvm->queue_info_lock);
940 mvm->queue_info[queue].txq_tid = tid;
941 spin_unlock_bh(&mvm->queue_info_lock);
942 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
943 queue, tid);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200944}
945
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200946static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
947{
948 struct ieee80211_sta *sta;
949 struct iwl_mvm_sta *mvmsta;
Sharon Dvir806911d2017-05-21 12:09:49 +0300950 u8 sta_id;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200951 int tid = -1;
952 unsigned long tid_bitmap;
953 unsigned int wdg_timeout;
954 int ssn;
955 int ret = true;
956
Sara Sharonbb497012016-09-29 14:52:40 +0300957 /* queue sharing is disabled on new TX path */
958 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
959 return;
960
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200961 lockdep_assert_held(&mvm->mutex);
962
963 spin_lock_bh(&mvm->queue_info_lock);
964 sta_id = mvm->queue_info[queue].ra_sta_id;
965 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
966 spin_unlock_bh(&mvm->queue_info_lock);
967
968 /* Find TID for queue, and make sure it is the only one on the queue */
969 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
970 if (tid_bitmap != BIT(tid)) {
971 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
972 queue, tid_bitmap);
973 return;
974 }
975
976 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
977 tid);
978
979 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
980 lockdep_is_held(&mvm->mutex));
981
982 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
983 return;
984
985 mvmsta = iwl_mvm_sta_from_mac80211(sta);
986 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
987
988 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
989
990 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
991 tid_to_mac80211_ac[tid], ssn,
992 wdg_timeout, true);
993 if (ret) {
994 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
995 return;
996 }
997
998 /* If aggs should be turned back on - do it */
999 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
Emmanuel Grumbach9cd70e82016-09-20 13:40:33 +03001000 struct iwl_mvm_add_sta_cmd cmd = {0};
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001001
1002 mvmsta->tid_disable_agg &= ~BIT(tid);
1003
1004 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1005 cmd.sta_id = mvmsta->sta_id;
1006 cmd.add_modify = STA_MODE_MODIFY;
1007 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1008 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
1009 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
1010
1011 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
1012 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
1013 if (!ret) {
1014 IWL_DEBUG_TX_QUEUES(mvm,
1015 "TXQ #%d is now aggregated again\n",
1016 queue);
1017
1018 /* Mark queue intenally as aggregating again */
1019 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
1020 }
1021 }
1022
1023 spin_lock_bh(&mvm->queue_info_lock);
1024 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1025 spin_unlock_bh(&mvm->queue_info_lock);
1026}
1027
Liad Kaufman24afba72015-07-28 18:56:08 +03001028static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1029{
1030 if (tid == IWL_MAX_TID_COUNT)
1031 return IEEE80211_AC_VO; /* MGMT */
1032
1033 return tid_to_mac80211_ac[tid];
1034}
1035
1036static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
1037 struct ieee80211_sta *sta, int tid)
1038{
1039 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1040 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1041 struct sk_buff *skb;
1042 struct ieee80211_hdr *hdr;
1043 struct sk_buff_head deferred_tx;
1044 u8 mac_queue;
1045 bool no_queue = false; /* Marks if there is a problem with the queue */
1046 u8 ac;
1047
1048 lockdep_assert_held(&mvm->mutex);
1049
1050 skb = skb_peek(&tid_data->deferred_tx_frames);
1051 if (!skb)
1052 return;
1053 hdr = (void *)skb->data;
1054
1055 ac = iwl_mvm_tid_to_ac_queue(tid);
1056 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
1057
Sara Sharon6862fce2017-02-22 19:34:17 +02001058 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE &&
Liad Kaufman24afba72015-07-28 18:56:08 +03001059 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
1060 IWL_ERR(mvm,
1061 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
1062 mvmsta->sta_id, tid);
1063
1064 /*
1065 * Mark queue as problematic so later the deferred traffic is
1066 * freed, as we can do nothing with it
1067 */
1068 no_queue = true;
1069 }
1070
1071 __skb_queue_head_init(&deferred_tx);
1072
Liad Kaufmand2515a92016-03-23 16:31:08 +02001073 /* Disable bottom-halves when entering TX path */
1074 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +03001075 spin_lock(&mvmsta->lock);
1076 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
Liad Kaufmanad5de732016-09-27 16:01:10 +03001077 mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
Liad Kaufman24afba72015-07-28 18:56:08 +03001078 spin_unlock(&mvmsta->lock);
1079
Liad Kaufman24afba72015-07-28 18:56:08 +03001080 while ((skb = __skb_dequeue(&deferred_tx)))
1081 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1082 ieee80211_free_txskb(mvm->hw, skb);
1083 local_bh_enable();
1084
1085 /* Wake queue */
1086 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1087}
1088
1089void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1090{
1091 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1092 add_stream_wk);
1093 struct ieee80211_sta *sta;
1094 struct iwl_mvm_sta *mvmsta;
1095 unsigned long deferred_tid_traffic;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001096 int queue, sta_id, tid;
Liad Kaufman24afba72015-07-28 18:56:08 +03001097
Liad Kaufman9794c642015-08-19 17:34:28 +03001098 /* Check inactivity of queues */
1099 iwl_mvm_inactivity_check(mvm);
1100
Liad Kaufman24afba72015-07-28 18:56:08 +03001101 mutex_lock(&mvm->mutex);
1102
Sara Sharon34e10862017-02-23 13:15:07 +02001103 /* No queue reconfiguration in TVQM mode */
1104 if (iwl_mvm_has_new_tx_api(mvm))
1105 goto alloc_queues;
1106
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001107 /* Reconfigure queues requiring reconfiguation */
Sara Sharon34e10862017-02-23 13:15:07 +02001108 for (queue = 0; queue < ARRAY_SIZE(mvm->queue_info); queue++) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001109 bool reconfig;
Liad Kaufman19aefa42016-03-08 14:29:51 +02001110 bool change_owner;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001111
1112 spin_lock_bh(&mvm->queue_info_lock);
1113 reconfig = (mvm->queue_info[queue].status ==
1114 IWL_MVM_QUEUE_RECONFIGURING);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001115
1116 /*
1117 * We need to take into account a situation in which a TXQ was
1118 * allocated to TID x, and then turned shared by adding TIDs y
1119 * and z. If TID x becomes inactive and is removed from the TXQ,
1120 * ownership must be given to one of the remaining TIDs.
1121 * This is mainly because if TID x continues - a new queue can't
1122 * be allocated for it as long as it is an owner of another TXQ.
1123 */
1124 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1125 BIT(mvm->queue_info[queue].txq_tid)) &&
1126 (mvm->queue_info[queue].status ==
1127 IWL_MVM_QUEUE_SHARED);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001128 spin_unlock_bh(&mvm->queue_info_lock);
1129
1130 if (reconfig)
1131 iwl_mvm_unshare_queue(mvm, queue);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001132 else if (change_owner)
1133 iwl_mvm_change_queue_owner(mvm, queue);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001134 }
1135
Sara Sharon34e10862017-02-23 13:15:07 +02001136alloc_queues:
Liad Kaufman24afba72015-07-28 18:56:08 +03001137 /* Go over all stations with deferred traffic */
1138 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1139 IWL_MVM_STATION_COUNT) {
1140 clear_bit(sta_id, mvm->sta_deferred_frames);
1141 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1142 lockdep_is_held(&mvm->mutex));
1143 if (IS_ERR_OR_NULL(sta))
1144 continue;
1145
1146 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1147 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1148
1149 for_each_set_bit(tid, &deferred_tid_traffic,
1150 IWL_MAX_TID_COUNT + 1)
1151 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1152 }
1153
1154 mutex_unlock(&mvm->mutex);
1155}
1156
1157static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001158 struct ieee80211_sta *sta,
1159 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +03001160{
1161 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1162 int queue;
Sara Sharon01796ff2016-11-16 17:04:36 +02001163 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman24afba72015-07-28 18:56:08 +03001164
Sara Sharon396952e2017-02-22 19:40:55 +02001165 /* queue reserving is disabled on new TX path */
1166 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1167 return 0;
1168
Liad Kaufman9794c642015-08-19 17:34:28 +03001169 /*
1170 * Check for inactive queues, so we don't reach a situation where we
1171 * can't add a STA due to a shortage in queues that doesn't really exist
1172 */
1173 iwl_mvm_inactivity_check(mvm);
1174
Liad Kaufman24afba72015-07-28 18:56:08 +03001175 spin_lock_bh(&mvm->queue_info_lock);
1176
1177 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +03001178 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1179 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +03001180 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1181 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +03001182 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1183 else
Liad Kaufman9794c642015-08-19 17:34:28 +03001184 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1185 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001186 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +03001187 if (queue < 0) {
1188 spin_unlock_bh(&mvm->queue_info_lock);
1189 IWL_ERR(mvm, "No available queues for new station\n");
1190 return -ENOSPC;
Sara Sharon01796ff2016-11-16 17:04:36 +02001191 } else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
1192 /*
1193 * If this queue is already allocated but inactive we'll need to
1194 * first free this queue before enabling it again, we'll mark
1195 * it as reserved to make sure no new traffic arrives on it
1196 */
1197 using_inactive_queue = true;
1198 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman24afba72015-07-28 18:56:08 +03001199 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03001200 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +03001201
1202 spin_unlock_bh(&mvm->queue_info_lock);
1203
1204 mvmsta->reserved_queue = queue;
1205
Sara Sharon01796ff2016-11-16 17:04:36 +02001206 if (using_inactive_queue)
1207 iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
1208
Liad Kaufman24afba72015-07-28 18:56:08 +03001209 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1210 queue, mvmsta->sta_id);
1211
1212 return 0;
1213}
1214
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001215/*
1216 * In DQA mode, after a HW restart the queues should be allocated as before, in
1217 * order to avoid race conditions when there are shared queues. This function
1218 * does the re-mapping and queue allocation.
1219 *
1220 * Note that re-enabling aggregations isn't done in this function.
1221 */
1222static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1223 struct iwl_mvm_sta *mvm_sta)
1224{
1225 unsigned int wdg_timeout =
1226 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1227 int i;
1228 struct iwl_trans_txq_scd_cfg cfg = {
1229 .sta_id = mvm_sta->sta_id,
1230 .frame_limit = IWL_FRAME_LIMIT,
1231 };
1232
Johannes Berg03c902b2016-12-02 12:03:36 +01001233 /* Make sure reserved queue is still marked as such (if allocated) */
1234 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1235 mvm->queue_info[mvm_sta->reserved_queue].status =
1236 IWL_MVM_QUEUE_RESERVED;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001237
1238 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1239 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1240 int txq_id = tid_data->txq_id;
1241 int ac;
1242 u8 mac_queue;
1243
Sara Sharon6862fce2017-02-22 19:34:17 +02001244 if (txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001245 continue;
1246
1247 skb_queue_head_init(&tid_data->deferred_tx_frames);
1248
1249 ac = tid_to_mac80211_ac[i];
1250 mac_queue = mvm_sta->vif->hw_queue[ac];
1251
Sara Sharon310181e2017-01-17 14:27:48 +02001252 if (iwl_mvm_has_new_tx_api(mvm)) {
1253 IWL_DEBUG_TX_QUEUES(mvm,
1254 "Re-mapping sta %d tid %d\n",
1255 mvm_sta->sta_id, i);
1256 txq_id = iwl_mvm_tvqm_enable_txq(mvm, mac_queue,
1257 mvm_sta->sta_id,
1258 i, wdg_timeout);
1259 tid_data->txq_id = txq_id;
1260 } else {
1261 u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001262
Sara Sharon310181e2017-01-17 14:27:48 +02001263 cfg.tid = i;
Emmanuel Grumbachcf6c6ea2017-06-13 13:18:48 +03001264 cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
Sara Sharon310181e2017-01-17 14:27:48 +02001265 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1266 txq_id ==
1267 IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001268
Sara Sharon310181e2017-01-17 14:27:48 +02001269 IWL_DEBUG_TX_QUEUES(mvm,
1270 "Re-mapping sta %d tid %d to queue %d\n",
1271 mvm_sta->sta_id, i, txq_id);
1272
1273 iwl_mvm_enable_txq(mvm, txq_id, mac_queue, seq, &cfg,
1274 wdg_timeout);
Sara Sharon34e10862017-02-23 13:15:07 +02001275 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
Sara Sharon310181e2017-01-17 14:27:48 +02001276 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001277 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001278}
1279
Johannes Berg8ca151b2013-01-24 14:25:36 +01001280int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1281 struct ieee80211_vif *vif,
1282 struct ieee80211_sta *sta)
1283{
1284 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001285 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001286 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001287 int i, ret, sta_id;
1288
1289 lockdep_assert_held(&mvm->mutex);
1290
1291 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001292 sta_id = iwl_mvm_find_free_sta_id(mvm,
1293 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001294 else
1295 sta_id = mvm_sta->sta_id;
1296
Sara Sharon0ae98812017-01-04 14:53:58 +02001297 if (sta_id == IWL_MVM_INVALID_STA)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001298 return -ENOSPC;
1299
1300 spin_lock_init(&mvm_sta->lock);
1301
Johannes Bergc8f54702017-06-19 23:50:31 +02001302 /* if this is a HW restart re-alloc existing queues */
1303 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001304 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1305 goto update_fw;
1306 }
1307
Johannes Berg8ca151b2013-01-24 14:25:36 +01001308 mvm_sta->sta_id = sta_id;
1309 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1310 mvmvif->color);
1311 mvm_sta->vif = vif;
Liad Kaufmana58bb462017-05-28 14:20:04 +03001312 if (!mvm->trans->cfg->gen2)
1313 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1314 else
1315 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001316 mvm_sta->tx_protection = 0;
1317 mvm_sta->tt_tx_protection = false;
Sara Sharonced19f22017-02-06 19:09:32 +02001318 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001319
1320 /* HW restart, don't assume the memory has been zeroed */
Liad Kaufman69191af2015-09-01 18:50:22 +03001321 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001322 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001323
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001324 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001325 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001326 u16 seq = mvm_sta->tid_data[i].seq_number;
1327 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1328 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001329
Liad Kaufman24afba72015-07-28 18:56:08 +03001330 /*
1331 * Mark all queues for this STA as unallocated and defer TX
1332 * frames until the queue is allocated
1333 */
Sara Sharon6862fce2017-02-22 19:34:17 +02001334 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001335 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001336 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001337 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001338 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001339
Sara Sharona571f5f2015-12-07 12:50:58 +02001340 if (iwl_mvm_has_new_rx_api(mvm) &&
1341 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Johannes Berg92c4dca2017-06-07 10:35:54 +02001342 int q;
1343
Sara Sharona571f5f2015-12-07 12:50:58 +02001344 dup_data = kcalloc(mvm->trans->num_rx_queues,
Johannes Berg92c4dca2017-06-07 10:35:54 +02001345 sizeof(*dup_data), GFP_KERNEL);
Sara Sharona571f5f2015-12-07 12:50:58 +02001346 if (!dup_data)
1347 return -ENOMEM;
Johannes Berg92c4dca2017-06-07 10:35:54 +02001348 /*
1349 * Initialize all the last_seq values to 0xffff which can never
1350 * compare equal to the frame's seq_ctrl in the check in
1351 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1352 * number and fragmented packets don't reach that function.
1353 *
1354 * This thus allows receiving a packet with seqno 0 and the
1355 * retry bit set as the very first packet on a new TID.
1356 */
1357 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1358 memset(dup_data[q].last_seq, 0xff,
1359 sizeof(dup_data[q].last_seq));
Sara Sharona571f5f2015-12-07 12:50:58 +02001360 mvm_sta->dup_data = dup_data;
1361 }
1362
Johannes Bergc8f54702017-06-19 23:50:31 +02001363 if (!iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001364 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1365 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001366 if (ret)
1367 goto err;
1368 }
1369
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001370update_fw:
Liad Kaufman24afba72015-07-28 18:56:08 +03001371 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001372 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001373 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001374
Johannes Berg9e848012014-08-04 14:33:42 +02001375 if (vif->type == NL80211_IFTYPE_STATION) {
1376 if (!sta->tdls) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001377 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001378 mvmvif->ap_sta_id = sta_id;
1379 } else {
Sara Sharon0ae98812017-01-04 14:53:58 +02001380 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001381 }
1382 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001383
1384 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1385
1386 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001387
1388err:
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001389 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001390}
1391
1392int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1393 bool drain)
1394{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001395 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001396 int ret;
1397 u32 status;
1398
1399 lockdep_assert_held(&mvm->mutex);
1400
1401 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1402 cmd.sta_id = mvmsta->sta_id;
1403 cmd.add_modify = STA_MODE_MODIFY;
1404 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1405 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1406
1407 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001408 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1409 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001410 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001411 if (ret)
1412 return ret;
1413
Sara Sharon837c4da2016-01-07 16:50:45 +02001414 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001415 case ADD_STA_SUCCESS:
1416 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1417 mvmsta->sta_id);
1418 break;
1419 default:
1420 ret = -EIO;
1421 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1422 mvmsta->sta_id);
1423 break;
1424 }
1425
1426 return ret;
1427}
1428
1429/*
1430 * Remove a station from the FW table. Before sending the command to remove
1431 * the station validate that the station is indeed known to the driver (sanity
1432 * only).
1433 */
1434static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1435{
1436 struct ieee80211_sta *sta;
1437 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1438 .sta_id = sta_id,
1439 };
1440 int ret;
1441
1442 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1443 lockdep_is_held(&mvm->mutex));
1444
1445 /* Note: internal stations are marked as error values */
1446 if (!sta) {
1447 IWL_ERR(mvm, "Invalid station id\n");
1448 return -EINVAL;
1449 }
1450
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001451 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001452 sizeof(rm_sta_cmd), &rm_sta_cmd);
1453 if (ret) {
1454 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1455 return ret;
1456 }
1457
1458 return 0;
1459}
1460
Liad Kaufman24afba72015-07-28 18:56:08 +03001461static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1462 struct ieee80211_vif *vif,
1463 struct iwl_mvm_sta *mvm_sta)
1464{
1465 int ac;
1466 int i;
1467
1468 lockdep_assert_held(&mvm->mutex);
1469
1470 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
Sara Sharon6862fce2017-02-22 19:34:17 +02001471 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman24afba72015-07-28 18:56:08 +03001472 continue;
1473
1474 ac = iwl_mvm_tid_to_ac_queue(i);
1475 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1476 vif->hw_queue[ac], i, 0);
Sara Sharon6862fce2017-02-22 19:34:17 +02001477 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001478 }
1479}
1480
Sara Sharond6d517b2017-03-06 10:16:11 +02001481int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1482 struct iwl_mvm_sta *mvm_sta)
1483{
Sharon Dvirbec95222017-06-12 11:40:33 +03001484 int i;
Sara Sharond6d517b2017-03-06 10:16:11 +02001485
1486 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1487 u16 txq_id;
Sharon Dvirbec95222017-06-12 11:40:33 +03001488 int ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001489
1490 spin_lock_bh(&mvm_sta->lock);
1491 txq_id = mvm_sta->tid_data[i].txq_id;
1492 spin_unlock_bh(&mvm_sta->lock);
1493
1494 if (txq_id == IWL_MVM_INVALID_QUEUE)
1495 continue;
1496
1497 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1498 if (ret)
Sharon Dvirbec95222017-06-12 11:40:33 +03001499 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001500 }
1501
Sharon Dvirbec95222017-06-12 11:40:33 +03001502 return 0;
Sara Sharond6d517b2017-03-06 10:16:11 +02001503}
1504
Johannes Berg8ca151b2013-01-24 14:25:36 +01001505int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1506 struct ieee80211_vif *vif,
1507 struct ieee80211_sta *sta)
1508{
1509 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001510 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001511 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001512 int ret;
1513
1514 lockdep_assert_held(&mvm->mutex);
1515
Sara Sharona571f5f2015-12-07 12:50:58 +02001516 if (iwl_mvm_has_new_rx_api(mvm))
1517 kfree(mvm_sta->dup_data);
1518
Johannes Bergc8f54702017-06-19 23:50:31 +02001519 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1520 if (ret)
1521 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001522
Johannes Bergc8f54702017-06-19 23:50:31 +02001523 /* flush its queues here since we are freeing mvm_sta */
1524 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1525 if (ret)
1526 return ret;
1527 if (iwl_mvm_has_new_tx_api(mvm)) {
1528 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1529 } else {
1530 u32 q_mask = mvm_sta->tfd_queue_msk;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001531
Johannes Bergc8f54702017-06-19 23:50:31 +02001532 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1533 q_mask);
1534 }
1535 if (ret)
1536 return ret;
Liad Kaufman56214742016-09-22 15:14:08 +03001537
Johannes Bergc8f54702017-06-19 23:50:31 +02001538 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001539
Johannes Bergc8f54702017-06-19 23:50:31 +02001540 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001541
Johannes Bergc8f54702017-06-19 23:50:31 +02001542 /* If there is a TXQ still marked as reserved - free it */
1543 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1544 u8 reserved_txq = mvm_sta->reserved_queue;
1545 enum iwl_mvm_queue_status *status;
1546
1547 /*
1548 * If no traffic has gone through the reserved TXQ - it
1549 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1550 * should be manually marked as free again
1551 */
1552 spin_lock_bh(&mvm->queue_info_lock);
1553 status = &mvm->queue_info[reserved_txq].status;
1554 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1555 (*status != IWL_MVM_QUEUE_FREE),
1556 "sta_id %d reserved txq %d status %d",
1557 sta_id, reserved_txq, *status)) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001558 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Bergc8f54702017-06-19 23:50:31 +02001559 return -EINVAL;
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001560 }
1561
Johannes Bergc8f54702017-06-19 23:50:31 +02001562 *status = IWL_MVM_QUEUE_FREE;
1563 spin_unlock_bh(&mvm->queue_info_lock);
1564 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001565
Johannes Bergc8f54702017-06-19 23:50:31 +02001566 if (vif->type == NL80211_IFTYPE_STATION &&
1567 mvmvif->ap_sta_id == sta_id) {
1568 /* if associated - we can't remove the AP STA now */
1569 if (vif->bss_conf.assoc)
1570 return ret;
Eliad Peller37577fe2013-12-05 17:19:39 +02001571
Johannes Bergc8f54702017-06-19 23:50:31 +02001572 /* unassoc - go ahead - remove the AP STA now */
1573 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1574
1575 /* clear d0i3_ap_sta_id if no longer relevant */
1576 if (mvm->d0i3_ap_sta_id == sta_id)
1577 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001578 }
1579
1580 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001581 * This shouldn't happen - the TDLS channel switch should be canceled
1582 * before the STA is removed.
1583 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001584 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001585 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001586 cancel_delayed_work(&mvm->tdls_cs.dwork);
1587 }
1588
1589 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001590 * Make sure that the tx response code sees the station as -EBUSY and
1591 * calls the drain worker.
1592 */
1593 spin_lock_bh(&mvm_sta->lock);
Johannes Bergc8f54702017-06-19 23:50:31 +02001594 spin_unlock_bh(&mvm_sta->lock);
Sara Sharon94c3e612016-12-07 15:04:37 +02001595
Johannes Bergc8f54702017-06-19 23:50:31 +02001596 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1597 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001598
1599 return ret;
1600}
1601
1602int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1603 struct ieee80211_vif *vif,
1604 u8 sta_id)
1605{
1606 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1607
1608 lockdep_assert_held(&mvm->mutex);
1609
Monam Agarwalc531c772014-03-24 00:05:56 +05301610 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001611 return ret;
1612}
1613
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001614int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1615 struct iwl_mvm_int_sta *sta,
Sara Sharonced19f22017-02-06 19:09:32 +02001616 u32 qmask, enum nl80211_iftype iftype,
1617 enum iwl_sta_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001618{
1619 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001620 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Sara Sharon0ae98812017-01-04 14:53:58 +02001621 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001622 return -ENOSPC;
1623 }
1624
1625 sta->tfd_queue_msk = qmask;
Sara Sharonced19f22017-02-06 19:09:32 +02001626 sta->type = type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001627
1628 /* put a non-NULL value so iterating over the stations won't stop */
1629 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1630 return 0;
1631}
1632
Sara Sharon26d6c162017-01-03 12:00:19 +02001633void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001634{
Monam Agarwalc531c772014-03-24 00:05:56 +05301635 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001636 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
Sara Sharon0ae98812017-01-04 14:53:58 +02001637 sta->sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001638}
1639
1640static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1641 struct iwl_mvm_int_sta *sta,
1642 const u8 *addr,
1643 u16 mac_id, u16 color)
1644{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001645 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001646 int ret;
1647 u32 status;
1648
1649 lockdep_assert_held(&mvm->mutex);
1650
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001651 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001652 cmd.sta_id = sta->sta_id;
1653 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1654 color));
Sara Sharonced19f22017-02-06 19:09:32 +02001655 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1656 cmd.station_type = sta->type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001657
Sara Sharonbb497012016-09-29 14:52:40 +03001658 if (!iwl_mvm_has_new_tx_api(mvm))
1659 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001660 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001661
1662 if (addr)
1663 memcpy(cmd.addr, addr, ETH_ALEN);
1664
Sara Sharon854c5702016-01-26 13:17:47 +02001665 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1666 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001667 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001668 if (ret)
1669 return ret;
1670
Sara Sharon837c4da2016-01-07 16:50:45 +02001671 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001672 case ADD_STA_SUCCESS:
1673 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1674 return 0;
1675 default:
1676 ret = -EIO;
1677 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1678 status);
1679 break;
1680 }
1681 return ret;
1682}
1683
Sara Sharonc5a719e2016-11-15 10:20:48 +02001684static void iwl_mvm_enable_aux_queue(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001685{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001686 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1687 mvm->cfg->base_params->wd_timeout :
1688 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001689
Sara Sharon310181e2017-01-17 14:27:48 +02001690 if (iwl_mvm_has_new_tx_api(mvm)) {
1691 int queue = iwl_mvm_tvqm_enable_txq(mvm, mvm->aux_queue,
1692 mvm->aux_sta.sta_id,
1693 IWL_MAX_TID_COUNT,
1694 wdg_timeout);
1695 mvm->aux_queue = queue;
Johannes Bergc8f54702017-06-19 23:50:31 +02001696 } else {
Liad Kaufman28d07932015-09-01 16:36:25 +03001697 struct iwl_trans_txq_scd_cfg cfg = {
1698 .fifo = IWL_MVM_TX_FIFO_MCAST,
1699 .sta_id = mvm->aux_sta.sta_id,
1700 .tid = IWL_MAX_TID_COUNT,
1701 .aggregate = false,
1702 .frame_limit = IWL_FRAME_LIMIT,
1703 };
1704
1705 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1706 wdg_timeout);
1707 }
Sara Sharonc5a719e2016-11-15 10:20:48 +02001708}
1709
1710int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1711{
1712 int ret;
1713
1714 lockdep_assert_held(&mvm->mutex);
1715
1716 /* Allocate aux station and assign to it the aux queue */
1717 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Sara Sharonced19f22017-02-06 19:09:32 +02001718 NL80211_IFTYPE_UNSPECIFIED,
1719 IWL_STA_AUX_ACTIVITY);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001720 if (ret)
1721 return ret;
1722
1723 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1724 if (!iwl_mvm_has_new_tx_api(mvm))
1725 iwl_mvm_enable_aux_queue(mvm);
Liad Kaufman28d07932015-09-01 16:36:25 +03001726
Johannes Berg8ca151b2013-01-24 14:25:36 +01001727 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1728 MAC_INDEX_AUX, 0);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001729 if (ret) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001730 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001731 return ret;
1732 }
1733
1734 /*
1735 * For a000 firmware and on we cannot add queue to a station unknown
1736 * to firmware so enable queue here - after the station was added
1737 */
1738 if (iwl_mvm_has_new_tx_api(mvm))
1739 iwl_mvm_enable_aux_queue(mvm);
1740
1741 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001742}
1743
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001744int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1745{
1746 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1747
1748 lockdep_assert_held(&mvm->mutex);
1749 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1750 mvmvif->id, 0);
1751}
1752
1753int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1754{
1755 int ret;
1756
1757 lockdep_assert_held(&mvm->mutex);
1758
1759 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1760 if (ret)
1761 IWL_WARN(mvm, "Failed sending remove station\n");
1762
1763 return ret;
1764}
1765
1766void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1767{
1768 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1769}
1770
Johannes Berg712b24a2014-08-04 14:14:14 +02001771void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1772{
1773 lockdep_assert_held(&mvm->mutex);
1774
1775 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1776}
1777
Johannes Berg8ca151b2013-01-24 14:25:36 +01001778/*
1779 * Send the add station command for the vif's broadcast station.
1780 * Assumes that the station was already allocated.
1781 *
1782 * @mvm: the mvm component
1783 * @vif: the interface to which the broadcast station is added
1784 * @bsta: the broadcast station to add.
1785 */
Johannes Berg013290a2014-08-04 13:38:48 +02001786int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001787{
1788 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001789 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001790 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001791 const u8 *baddr = _baddr;
Johannes Berg7daa7622017-02-24 12:02:22 +01001792 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001793 int ret;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001794 unsigned int wdg_timeout =
1795 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1796 struct iwl_trans_txq_scd_cfg cfg = {
1797 .fifo = IWL_MVM_TX_FIFO_VO,
1798 .sta_id = mvmvif->bcast_sta.sta_id,
1799 .tid = IWL_MAX_TID_COUNT,
1800 .aggregate = false,
1801 .frame_limit = IWL_FRAME_LIMIT,
1802 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001803
1804 lockdep_assert_held(&mvm->mutex);
1805
Johannes Bergc8f54702017-06-19 23:50:31 +02001806 if (!iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufman4d339982017-03-21 17:13:16 +02001807 if (vif->type == NL80211_IFTYPE_AP ||
1808 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon49f71712017-01-09 12:07:16 +02001809 queue = mvm->probe_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001810 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
Sara Sharon49f71712017-01-09 12:07:16 +02001811 queue = mvm->p2p_dev_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001812 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
Liad Kaufmande24f632015-08-04 15:19:18 +03001813 return -EINVAL;
1814
Liad Kaufmandf88c082016-11-24 15:31:00 +02001815 bsta->tfd_queue_msk |= BIT(queue);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001816
Sara Sharon310181e2017-01-17 14:27:48 +02001817 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1818 &cfg, wdg_timeout);
Liad Kaufmande24f632015-08-04 15:19:18 +03001819 }
1820
Johannes Berg5023d962013-07-31 14:07:43 +02001821 if (vif->type == NL80211_IFTYPE_ADHOC)
1822 baddr = vif->bss_conf.bssid;
1823
Sara Sharon0ae98812017-01-04 14:53:58 +02001824 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001825 return -ENOSPC;
1826
Liad Kaufmandf88c082016-11-24 15:31:00 +02001827 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1828 mvmvif->id, mvmvif->color);
1829 if (ret)
1830 return ret;
1831
1832 /*
Sara Sharonc5a719e2016-11-15 10:20:48 +02001833 * For a000 firmware and on we cannot add queue to a station unknown
1834 * to firmware so enable queue here - after the station was added
Liad Kaufmandf88c082016-11-24 15:31:00 +02001835 */
Sara Sharon310181e2017-01-17 14:27:48 +02001836 if (iwl_mvm_has_new_tx_api(mvm)) {
Johannes Berg7daa7622017-02-24 12:02:22 +01001837 queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
1838 bsta->sta_id,
1839 IWL_MAX_TID_COUNT,
1840 wdg_timeout);
1841
Luca Coelho7b758a12017-06-20 13:40:03 +03001842 if (vif->type == NL80211_IFTYPE_AP ||
1843 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon310181e2017-01-17 14:27:48 +02001844 mvm->probe_queue = queue;
1845 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1846 mvm->p2p_dev_queue = queue;
Sara Sharon310181e2017-01-17 14:27:48 +02001847 }
Liad Kaufmandf88c082016-11-24 15:31:00 +02001848
1849 return 0;
1850}
1851
1852static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
1853 struct ieee80211_vif *vif)
1854{
1855 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001856 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001857
1858 lockdep_assert_held(&mvm->mutex);
1859
Sara Sharond49394a2017-03-05 13:01:08 +02001860 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
1861
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001862 switch (vif->type) {
1863 case NL80211_IFTYPE_AP:
1864 case NL80211_IFTYPE_ADHOC:
1865 queue = mvm->probe_queue;
1866 break;
1867 case NL80211_IFTYPE_P2P_DEVICE:
1868 queue = mvm->p2p_dev_queue;
1869 break;
1870 default:
1871 WARN(1, "Can't free bcast queue on vif type %d\n",
1872 vif->type);
1873 return;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001874 }
1875
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001876 iwl_mvm_disable_txq(mvm, queue, vif->hw_queue[0], IWL_MAX_TID_COUNT, 0);
1877 if (iwl_mvm_has_new_tx_api(mvm))
1878 return;
1879
1880 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
1881 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001882}
1883
1884/* Send the FW a request to remove the station from it's internal data
1885 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001886int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001887{
Johannes Berg013290a2014-08-04 13:38:48 +02001888 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001889 int ret;
1890
1891 lockdep_assert_held(&mvm->mutex);
1892
Johannes Bergc8f54702017-06-19 23:50:31 +02001893 iwl_mvm_free_bcast_sta_queues(mvm, vif);
Liad Kaufmandf88c082016-11-24 15:31:00 +02001894
Johannes Berg013290a2014-08-04 13:38:48 +02001895 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001896 if (ret)
1897 IWL_WARN(mvm, "Failed sending remove station\n");
1898 return ret;
1899}
1900
Johannes Berg013290a2014-08-04 13:38:48 +02001901int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1902{
1903 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001904
1905 lockdep_assert_held(&mvm->mutex);
1906
Johannes Bergc8f54702017-06-19 23:50:31 +02001907 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
Sara Sharonced19f22017-02-06 19:09:32 +02001908 ieee80211_vif_type_p2p(vif),
1909 IWL_STA_GENERAL_PURPOSE);
Johannes Berg013290a2014-08-04 13:38:48 +02001910}
1911
Johannes Berg8ca151b2013-01-24 14:25:36 +01001912/* Allocate a new station entry for the broadcast station to the given vif,
1913 * and send it to the FW.
1914 * Note that each P2P mac should have its own broadcast station.
1915 *
1916 * @mvm: the mvm component
1917 * @vif: the interface to which the broadcast station is added
1918 * @bsta: the broadcast station to add. */
Luca Coelhod1973582017-06-22 16:00:25 +03001919int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001920{
1921 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001922 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001923 int ret;
1924
1925 lockdep_assert_held(&mvm->mutex);
1926
Johannes Berg013290a2014-08-04 13:38:48 +02001927 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001928 if (ret)
1929 return ret;
1930
Johannes Berg013290a2014-08-04 13:38:48 +02001931 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001932
1933 if (ret)
1934 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001935
Johannes Berg8ca151b2013-01-24 14:25:36 +01001936 return ret;
1937}
1938
Johannes Berg013290a2014-08-04 13:38:48 +02001939void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1940{
1941 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1942
1943 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1944}
1945
Johannes Berg8ca151b2013-01-24 14:25:36 +01001946/*
1947 * Send the FW a request to remove the station from it's internal data
1948 * structures, and in addition remove it from the local data structure.
1949 */
Luca Coelhod1973582017-06-22 16:00:25 +03001950int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001951{
1952 int ret;
1953
1954 lockdep_assert_held(&mvm->mutex);
1955
Johannes Berg013290a2014-08-04 13:38:48 +02001956 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001957
Johannes Berg013290a2014-08-04 13:38:48 +02001958 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1959
Johannes Berg8ca151b2013-01-24 14:25:36 +01001960 return ret;
1961}
1962
Sara Sharon26d6c162017-01-03 12:00:19 +02001963/*
1964 * Allocate a new station entry for the multicast station to the given vif,
1965 * and send it to the FW.
1966 * Note that each AP/GO mac should have its own multicast station.
1967 *
1968 * @mvm: the mvm component
1969 * @vif: the interface to which the multicast station is added
1970 */
1971int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1972{
1973 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1974 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
1975 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
1976 const u8 *maddr = _maddr;
1977 struct iwl_trans_txq_scd_cfg cfg = {
1978 .fifo = IWL_MVM_TX_FIFO_MCAST,
1979 .sta_id = msta->sta_id,
1980 .tid = IWL_MAX_TID_COUNT,
1981 .aggregate = false,
1982 .frame_limit = IWL_FRAME_LIMIT,
1983 };
1984 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1985 int ret;
1986
1987 lockdep_assert_held(&mvm->mutex);
1988
Liad Kaufmanee48b722017-03-21 17:13:16 +02001989 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1990 vif->type != NL80211_IFTYPE_ADHOC))
Sara Sharon26d6c162017-01-03 12:00:19 +02001991 return -ENOTSUPP;
1992
Sara Sharonced19f22017-02-06 19:09:32 +02001993 /*
1994 * While in previous FWs we had to exclude cab queue from TFD queue
1995 * mask, now it is needed as any other queue.
1996 */
1997 if (!iwl_mvm_has_new_tx_api(mvm) &&
1998 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
1999 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2000 &cfg, timeout);
2001 msta->tfd_queue_msk |= BIT(vif->cab_queue);
2002 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002003 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2004 mvmvif->id, mvmvif->color);
2005 if (ret) {
2006 iwl_mvm_dealloc_int_sta(mvm, msta);
2007 return ret;
2008 }
2009
2010 /*
2011 * Enable cab queue after the ADD_STA command is sent.
2012 * This is needed for a000 firmware which won't accept SCD_QUEUE_CFG
Sara Sharonced19f22017-02-06 19:09:32 +02002013 * command with unknown station id, and for FW that doesn't support
2014 * station API since the cab queue is not included in the
2015 * tfd_queue_mask.
Sara Sharon26d6c162017-01-03 12:00:19 +02002016 */
Sara Sharon310181e2017-01-17 14:27:48 +02002017 if (iwl_mvm_has_new_tx_api(mvm)) {
2018 int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2019 msta->sta_id,
2020 IWL_MAX_TID_COUNT,
2021 timeout);
Sara Sharone2af3fa2017-02-22 19:35:10 +02002022 mvmvif->cab_queue = queue;
Sara Sharonced19f22017-02-06 19:09:32 +02002023 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2024 IWL_UCODE_TLV_API_STA_TYPE)) {
Liad Kaufmanee48b722017-03-21 17:13:16 +02002025 /*
2026 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2027 * invalid, so make sure we use the queue we want.
2028 * Note that this is done here as we want to avoid making DQA
2029 * changes in mac80211 layer.
2030 */
2031 if (vif->type == NL80211_IFTYPE_ADHOC) {
2032 vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2033 mvmvif->cab_queue = vif->cab_queue;
2034 }
Sara Sharon310181e2017-01-17 14:27:48 +02002035 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2036 &cfg, timeout);
2037 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002038
2039 return 0;
2040}
2041
2042/*
2043 * Send the FW a request to remove the station from it's internal data
2044 * structures, and in addition remove it from the local data structure.
2045 */
2046int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2047{
2048 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2049 int ret;
2050
2051 lockdep_assert_held(&mvm->mutex);
2052
Sara Sharond49394a2017-03-05 13:01:08 +02002053 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2054
Sara Sharone2af3fa2017-02-22 19:35:10 +02002055 iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
Sara Sharon26d6c162017-01-03 12:00:19 +02002056 IWL_MAX_TID_COUNT, 0);
2057
2058 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2059 if (ret)
2060 IWL_WARN(mvm, "Failed sending remove station\n");
2061
2062 return ret;
2063}
2064
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002065#define IWL_MAX_RX_BA_SESSIONS 16
2066
Sara Sharonb915c102016-03-23 16:32:02 +02002067static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02002068{
Sara Sharonb915c102016-03-23 16:32:02 +02002069 struct iwl_mvm_delba_notif notif = {
2070 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2071 .metadata.sync = 1,
2072 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02002073 };
Sara Sharonb915c102016-03-23 16:32:02 +02002074 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2075};
Sara Sharon10b2b202016-03-20 16:23:41 +02002076
Sara Sharonb915c102016-03-23 16:32:02 +02002077static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2078 struct iwl_mvm_baid_data *data)
2079{
2080 int i;
2081
2082 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2083
2084 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2085 int j;
2086 struct iwl_mvm_reorder_buffer *reorder_buf =
2087 &data->reorder_buf[i];
2088
Sara Sharon06904052016-02-28 20:28:17 +02002089 spin_lock_bh(&reorder_buf->lock);
2090 if (likely(!reorder_buf->num_stored)) {
2091 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02002092 continue;
Sara Sharon06904052016-02-28 20:28:17 +02002093 }
Sara Sharonb915c102016-03-23 16:32:02 +02002094
2095 /*
2096 * This shouldn't happen in regular DELBA since the internal
2097 * delBA notification should trigger a release of all frames in
2098 * the reorder buffer.
2099 */
2100 WARN_ON(1);
2101
2102 for (j = 0; j < reorder_buf->buf_size; j++)
2103 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02002104 /*
2105 * Prevent timer re-arm. This prevents a very far fetched case
2106 * where we timed out on the notification. There may be prior
2107 * RX frames pending in the RX queue before the notification
2108 * that might get processed between now and the actual deletion
2109 * and we would re-arm the timer although we are deleting the
2110 * reorder buffer.
2111 */
2112 reorder_buf->removed = true;
2113 spin_unlock_bh(&reorder_buf->lock);
2114 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02002115 }
2116}
2117
2118static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2119 u32 sta_id,
2120 struct iwl_mvm_baid_data *data,
2121 u16 ssn, u8 buf_size)
2122{
2123 int i;
2124
2125 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2126 struct iwl_mvm_reorder_buffer *reorder_buf =
2127 &data->reorder_buf[i];
2128 int j;
2129
2130 reorder_buf->num_stored = 0;
2131 reorder_buf->head_sn = ssn;
2132 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02002133 /* rx reorder timer */
2134 reorder_buf->reorder_timer.function =
2135 iwl_mvm_reorder_timer_expired;
2136 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
2137 init_timer(&reorder_buf->reorder_timer);
2138 spin_lock_init(&reorder_buf->lock);
2139 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02002140 reorder_buf->queue = i;
2141 reorder_buf->sta_id = sta_id;
Sara Sharon5d43eab2017-02-02 12:51:39 +02002142 reorder_buf->valid = false;
Sara Sharonb915c102016-03-23 16:32:02 +02002143 for (j = 0; j < reorder_buf->buf_size; j++)
2144 __skb_queue_head_init(&reorder_buf->entries[j]);
2145 }
Sara Sharon10b2b202016-03-20 16:23:41 +02002146}
2147
Johannes Berg8ca151b2013-01-24 14:25:36 +01002148int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02002149 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002150{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002151 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002152 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02002153 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002154 int ret;
2155 u32 status;
2156
2157 lockdep_assert_held(&mvm->mutex);
2158
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002159 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2160 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2161 return -ENOSPC;
2162 }
2163
Sara Sharon10b2b202016-03-20 16:23:41 +02002164 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2165 /*
2166 * Allocate here so if allocation fails we can bail out early
2167 * before starting the BA session in the firmware
2168 */
Sara Sharonb915c102016-03-23 16:32:02 +02002169 baid_data = kzalloc(sizeof(*baid_data) +
2170 mvm->trans->num_rx_queues *
2171 sizeof(baid_data->reorder_buf[0]),
2172 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02002173 if (!baid_data)
2174 return -ENOMEM;
2175 }
2176
Johannes Berg8ca151b2013-01-24 14:25:36 +01002177 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2178 cmd.sta_id = mvm_sta->sta_id;
2179 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002180 if (start) {
2181 cmd.add_immediate_ba_tid = (u8) tid;
2182 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02002183 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002184 } else {
2185 cmd.remove_immediate_ba_tid = (u8) tid;
2186 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002187 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2188 STA_MODIFY_REMOVE_BA_TID;
2189
2190 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002191 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2192 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002193 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002194 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02002195 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002196
Sara Sharon837c4da2016-01-07 16:50:45 +02002197 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002198 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03002199 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2200 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002201 break;
2202 case ADD_STA_IMMEDIATE_BA_FAILURE:
2203 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2204 ret = -ENOSPC;
2205 break;
2206 default:
2207 ret = -EIO;
2208 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2209 start ? "start" : "stopp", status);
2210 break;
2211 }
2212
Sara Sharon10b2b202016-03-20 16:23:41 +02002213 if (ret)
2214 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002215
Sara Sharon10b2b202016-03-20 16:23:41 +02002216 if (start) {
2217 u8 baid;
2218
2219 mvm->rx_ba_sessions++;
2220
2221 if (!iwl_mvm_has_new_rx_api(mvm))
2222 return 0;
2223
2224 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2225 ret = -EINVAL;
2226 goto out_free;
2227 }
2228 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2229 IWL_ADD_STA_BAID_SHIFT);
2230 baid_data->baid = baid;
2231 baid_data->timeout = timeout;
2232 baid_data->last_rx = jiffies;
Wei Yongjun72c240f2016-07-12 11:40:57 +00002233 setup_timer(&baid_data->session_timer,
2234 iwl_mvm_rx_agg_session_expired,
2235 (unsigned long)&mvm->baid_map[baid]);
Sara Sharon10b2b202016-03-20 16:23:41 +02002236 baid_data->mvm = mvm;
2237 baid_data->tid = tid;
2238 baid_data->sta_id = mvm_sta->sta_id;
2239
2240 mvm_sta->tid_to_baid[tid] = baid;
2241 if (timeout)
2242 mod_timer(&baid_data->session_timer,
2243 TU_TO_EXP_TIME(timeout * 2));
2244
Sara Sharonb915c102016-03-23 16:32:02 +02002245 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2246 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002247 /*
2248 * protect the BA data with RCU to cover a case where our
2249 * internal RX sync mechanism will timeout (not that it's
2250 * supposed to happen) and we will free the session data while
2251 * RX is being processed in parallel
2252 */
Sara Sharon35263a02016-06-21 12:12:10 +03002253 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2254 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002255 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2256 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002257 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002258 u8 baid = mvm_sta->tid_to_baid[tid];
2259
Sara Sharon60dec522016-06-21 14:14:08 +03002260 if (mvm->rx_ba_sessions > 0)
2261 /* check that restart flow didn't zero the counter */
2262 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002263 if (!iwl_mvm_has_new_rx_api(mvm))
2264 return 0;
2265
2266 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2267 return -EINVAL;
2268
2269 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2270 if (WARN_ON(!baid_data))
2271 return -EINVAL;
2272
2273 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002274 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002275 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002276 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2277 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002278 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002279 }
2280 return 0;
2281
2282out_free:
2283 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002284 return ret;
2285}
2286
Liad Kaufman9794c642015-08-19 17:34:28 +03002287int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2288 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002289{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002290 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002291 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002292 int ret;
2293 u32 status;
2294
2295 lockdep_assert_held(&mvm->mutex);
2296
2297 if (start) {
2298 mvm_sta->tfd_queue_msk |= BIT(queue);
2299 mvm_sta->tid_disable_agg &= ~BIT(tid);
2300 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002301 /* In DQA-mode the queue isn't removed on agg termination */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002302 mvm_sta->tid_disable_agg |= BIT(tid);
2303 }
2304
2305 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2306 cmd.sta_id = mvm_sta->sta_id;
2307 cmd.add_modify = STA_MODE_MODIFY;
Sara Sharonbb497012016-09-29 14:52:40 +03002308 if (!iwl_mvm_has_new_tx_api(mvm))
2309 cmd.modify_mask = STA_MODIFY_QUEUES;
2310 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002311 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2312 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2313
2314 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002315 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2316 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002317 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002318 if (ret)
2319 return ret;
2320
Sara Sharon837c4da2016-01-07 16:50:45 +02002321 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002322 case ADD_STA_SUCCESS:
2323 break;
2324 default:
2325 ret = -EIO;
2326 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2327 start ? "start" : "stopp", status);
2328 break;
2329 }
2330
2331 return ret;
2332}
2333
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002334const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002335 IEEE80211_AC_BE,
2336 IEEE80211_AC_BK,
2337 IEEE80211_AC_BK,
2338 IEEE80211_AC_BE,
2339 IEEE80211_AC_VI,
2340 IEEE80211_AC_VI,
2341 IEEE80211_AC_VO,
2342 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002343 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002344};
2345
Johannes Berg3e56ead2013-02-15 22:23:18 +01002346static const u8 tid_to_ucode_ac[] = {
2347 AC_BE,
2348 AC_BK,
2349 AC_BK,
2350 AC_BE,
2351 AC_VI,
2352 AC_VI,
2353 AC_VO,
2354 AC_VO,
2355};
2356
Johannes Berg8ca151b2013-01-24 14:25:36 +01002357int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2358 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2359{
Johannes Berg5b577a92013-11-14 18:20:04 +01002360 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002361 struct iwl_mvm_tid_data *tid_data;
Liad Kaufmandd321622017-04-05 16:25:11 +03002362 u16 normalized_ssn;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002363 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002364 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002365
2366 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2367 return -EINVAL;
2368
2369 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2370 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2371 mvmsta->tid_data[tid].state);
2372 return -ENXIO;
2373 }
2374
2375 lockdep_assert_held(&mvm->mutex);
2376
Arik Nemtsovb2492502014-03-13 12:21:50 +02002377 spin_lock_bh(&mvmsta->lock);
2378
2379 /* possible race condition - we entered D0i3 while starting agg */
2380 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2381 spin_unlock_bh(&mvmsta->lock);
2382 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2383 return -EIO;
2384 }
2385
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002386 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002387
Liad Kaufmancf961e12015-08-13 19:16:08 +03002388 /*
2389 * Note the possible cases:
2390 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2391 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2392 * one and mark it as reserved
2393 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2394 * non-DQA mode, since the TXQ hasn't yet been allocated
Sara Sharon34e10862017-02-23 13:15:07 +02002395 * Don't support case 3 for new TX path as it is not expected to happen
2396 * and aggregation will be offloaded soon anyway
Liad Kaufmancf961e12015-08-13 19:16:08 +03002397 */
2398 txq_id = mvmsta->tid_data[tid].txq_id;
Sara Sharon34e10862017-02-23 13:15:07 +02002399 if (iwl_mvm_has_new_tx_api(mvm)) {
2400 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2401 ret = -ENXIO;
2402 goto release_locks;
2403 }
Johannes Bergc8f54702017-06-19 23:50:31 +02002404 } else if (unlikely(mvm->queue_info[txq_id].status ==
Sara Sharon34e10862017-02-23 13:15:07 +02002405 IWL_MVM_QUEUE_SHARED)) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002406 ret = -ENXIO;
2407 IWL_DEBUG_TX_QUEUES(mvm,
2408 "Can't start tid %d agg on shared queue!\n",
2409 tid);
2410 goto release_locks;
Johannes Bergc8f54702017-06-19 23:50:31 +02002411 } else if (mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002412 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
Johannes Bergc8f54702017-06-19 23:50:31 +02002413 IWL_MVM_DQA_MIN_DATA_QUEUE,
2414 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002415 if (txq_id < 0) {
2416 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002417 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2418 goto release_locks;
2419 }
Sara Sharon01796ff2016-11-16 17:04:36 +02002420 /*
2421 * TXQ shouldn't be in inactive mode for non-DQA, so getting
2422 * an inactive queue from iwl_mvm_find_free_queue() is
2423 * certainly a bug
2424 */
2425 WARN_ON(mvm->queue_info[txq_id].status ==
2426 IWL_MVM_QUEUE_INACTIVE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002427
2428 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2429 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002430 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002431
2432 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002433
Liad Kaufmancf961e12015-08-13 19:16:08 +03002434 IWL_DEBUG_TX_QUEUES(mvm,
2435 "AGG for tid %d will be on queue #%d\n",
2436 tid, txq_id);
2437
Johannes Berg8ca151b2013-01-24 14:25:36 +01002438 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002439 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002440 tid_data->txq_id = txq_id;
2441 *ssn = tid_data->ssn;
2442
2443 IWL_DEBUG_TX_QUEUES(mvm,
2444 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2445 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2446 tid_data->next_reclaimed);
2447
Liad Kaufmandd321622017-04-05 16:25:11 +03002448 /*
2449 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
2450 * to align the wrap around of ssn so we compare relevant values.
2451 */
2452 normalized_ssn = tid_data->ssn;
2453 if (mvm->trans->cfg->gen2)
2454 normalized_ssn &= 0xff;
2455
2456 if (normalized_ssn == tid_data->next_reclaimed) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002457 tid_data->state = IWL_AGG_STARTING;
2458 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2459 } else {
2460 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2461 }
2462
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002463 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002464 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002465
2466release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002467 spin_unlock(&mvm->queue_info_lock);
2468out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002469 spin_unlock_bh(&mvmsta->lock);
2470
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002471 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002472}
2473
2474int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002475 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2476 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002477{
Johannes Berg5b577a92013-11-14 18:20:04 +01002478 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002479 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002480 unsigned int wdg_timeout =
2481 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002482 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002483 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002484 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002485 u16 ssn;
2486
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002487 struct iwl_trans_txq_scd_cfg cfg = {
2488 .sta_id = mvmsta->sta_id,
2489 .tid = tid,
2490 .frame_limit = buf_size,
2491 .aggregate = true,
2492 };
2493
Eyal Shapiraefed6642014-09-14 15:58:53 +03002494 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2495 != IWL_MAX_TID_COUNT);
2496
Liad Kaufmana58bb462017-05-28 14:20:04 +03002497 if (!mvm->trans->cfg->gen2)
2498 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2499 else
2500 buf_size = min_t(int, buf_size,
2501 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002502
2503 spin_lock_bh(&mvmsta->lock);
2504 ssn = tid_data->ssn;
2505 queue = tid_data->txq_id;
2506 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002507 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002508 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002509 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002510 spin_unlock_bh(&mvmsta->lock);
2511
Sara Sharon34e10862017-02-23 13:15:07 +02002512 if (iwl_mvm_has_new_tx_api(mvm)) {
2513 /*
2514 * If no queue iwl_mvm_sta_tx_agg_start() would have failed so
2515 * no need to check queue's status
2516 */
2517 if (buf_size < mvmsta->max_agg_bufsize)
2518 return -ENOTSUPP;
2519
2520 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2521 if (ret)
2522 return -EIO;
2523 goto out;
2524 }
2525
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002526 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002527
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002528 spin_lock_bh(&mvm->queue_info_lock);
2529 queue_status = mvm->queue_info[queue].status;
2530 spin_unlock_bh(&mvm->queue_info_lock);
2531
Johannes Bergc8f54702017-06-19 23:50:31 +02002532 /* Maybe there is no need to even alloc a queue... */
2533 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2534 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002535
Johannes Bergc8f54702017-06-19 23:50:31 +02002536 /*
2537 * Only reconfig the SCD for the queue if the window size has
2538 * changed from current (become smaller)
2539 */
2540 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002541 /*
Johannes Bergc8f54702017-06-19 23:50:31 +02002542 * If reconfiguring an existing queue, it first must be
2543 * drained
Liad Kaufmancf961e12015-08-13 19:16:08 +03002544 */
Johannes Bergc8f54702017-06-19 23:50:31 +02002545 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2546 BIT(queue));
2547 if (ret) {
2548 IWL_ERR(mvm,
2549 "Error draining queue before reconfig\n");
2550 return ret;
2551 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03002552
Johannes Bergc8f54702017-06-19 23:50:31 +02002553 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2554 mvmsta->sta_id, tid,
2555 buf_size, ssn);
2556 if (ret) {
2557 IWL_ERR(mvm,
2558 "Error reconfiguring TXQ #%d\n", queue);
2559 return ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002560 }
2561 }
2562
2563 if (alloc_queue)
2564 iwl_mvm_enable_txq(mvm, queue,
2565 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2566 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002567
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002568 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2569 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2570 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2571 if (ret)
2572 return -EIO;
2573 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002574
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002575 /* No need to mark as reserved */
2576 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002577 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002578 spin_unlock_bh(&mvm->queue_info_lock);
2579
Sara Sharon34e10862017-02-23 13:15:07 +02002580out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002581 /*
2582 * Even though in theory the peer could have different
2583 * aggregation reorder buffer sizes for different sessions,
2584 * our ucode doesn't allow for that and has a global limit
2585 * for each station. Therefore, use the minimum of all the
2586 * aggregation sessions and our default value.
2587 */
2588 mvmsta->max_agg_bufsize =
2589 min(mvmsta->max_agg_bufsize, buf_size);
2590 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2591
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002592 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2593 sta->addr, tid);
2594
Eyal Shapira9e680942013-11-09 00:16:16 +02002595 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002596}
2597
Sara Sharon34e10862017-02-23 13:15:07 +02002598static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2599 struct iwl_mvm_sta *mvmsta,
2600 u16 txq_id)
2601{
2602 if (iwl_mvm_has_new_tx_api(mvm))
2603 return;
2604
2605 spin_lock_bh(&mvm->queue_info_lock);
2606 /*
2607 * The TXQ is marked as reserved only if no traffic came through yet
2608 * This means no traffic has been sent on this TID (agg'd or not), so
2609 * we no longer have use for the queue. Since it hasn't even been
2610 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2611 * free.
2612 */
2613 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2614 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2615
2616 spin_unlock_bh(&mvm->queue_info_lock);
2617}
2618
Johannes Berg8ca151b2013-01-24 14:25:36 +01002619int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2620 struct ieee80211_sta *sta, u16 tid)
2621{
Johannes Berg5b577a92013-11-14 18:20:04 +01002622 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002623 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2624 u16 txq_id;
2625 int err;
2626
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002627 /*
2628 * If mac80211 is cleaning its state, then say that we finished since
2629 * our state has been cleared anyway.
2630 */
2631 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2632 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2633 return 0;
2634 }
2635
Johannes Berg8ca151b2013-01-24 14:25:36 +01002636 spin_lock_bh(&mvmsta->lock);
2637
2638 txq_id = tid_data->txq_id;
2639
2640 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2641 mvmsta->sta_id, tid, txq_id, tid_data->state);
2642
Eyal Shapiraefed6642014-09-14 15:58:53 +03002643 mvmsta->agg_tids &= ~BIT(tid);
2644
Sara Sharon34e10862017-02-23 13:15:07 +02002645 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002646
Johannes Berg8ca151b2013-01-24 14:25:36 +01002647 switch (tid_data->state) {
2648 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002649 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002650
2651 IWL_DEBUG_TX_QUEUES(mvm,
2652 "ssn = %d, next_recl = %d\n",
2653 tid_data->ssn, tid_data->next_reclaimed);
2654
Johannes Berg8ca151b2013-01-24 14:25:36 +01002655 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002656 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002657 spin_unlock_bh(&mvmsta->lock);
2658
2659 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2660
2661 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
Johannes Bergf7f89e72014-08-05 15:24:44 +02002662 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002663 case IWL_AGG_STARTING:
2664 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2665 /*
2666 * The agg session has been stopped before it was set up. This
2667 * can happen when the AddBA timer times out for example.
2668 */
2669
2670 /* No barriers since we are under mutex */
2671 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002672
2673 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2674 tid_data->state = IWL_AGG_OFF;
2675 err = 0;
2676 break;
2677 default:
2678 IWL_ERR(mvm,
2679 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2680 mvmsta->sta_id, tid, tid_data->state);
2681 IWL_ERR(mvm,
2682 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2683 err = -EINVAL;
2684 }
2685
2686 spin_unlock_bh(&mvmsta->lock);
2687
2688 return err;
2689}
2690
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002691int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2692 struct ieee80211_sta *sta, u16 tid)
2693{
Johannes Berg5b577a92013-11-14 18:20:04 +01002694 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002695 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2696 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002697 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002698
2699 /*
2700 * First set the agg state to OFF to avoid calling
2701 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2702 */
2703 spin_lock_bh(&mvmsta->lock);
2704 txq_id = tid_data->txq_id;
2705 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2706 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002707 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002708 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002709 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002710 spin_unlock_bh(&mvmsta->lock);
2711
Sara Sharon34e10862017-02-23 13:15:07 +02002712 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002713
Johannes Bergb6658ff2013-07-24 13:55:51 +02002714 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002715 iwl_mvm_drain_sta(mvm, mvmsta, true);
Sara Sharond6d517b2017-03-06 10:16:11 +02002716
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002717 if (iwl_mvm_has_new_tx_api(mvm)) {
2718 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
2719 BIT(tid), 0))
2720 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002721 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002722 } else {
2723 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2724 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002725 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002726 }
Sara Sharond6d517b2017-03-06 10:16:11 +02002727
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002728 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002729
Johannes Bergf7f89e72014-08-05 15:24:44 +02002730 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002731 }
2732
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002733 return 0;
2734}
2735
Johannes Berg8ca151b2013-01-24 14:25:36 +01002736static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2737{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002738 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002739
2740 lockdep_assert_held(&mvm->mutex);
2741
Johannes Berg2dc2a152015-06-16 17:09:18 +02002742 /* Pick the unused key offset with the highest 'deleted'
2743 * counter. Every time a key is deleted, all the counters
2744 * are incremented and the one that was just deleted is
2745 * reset to zero. Thus, the highest counter is the one
2746 * that was deleted longest ago. Pick that one.
2747 */
2748 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2749 if (test_bit(i, mvm->fw_key_table))
2750 continue;
2751 if (mvm->fw_key_deleted[i] > max) {
2752 max = mvm->fw_key_deleted[i];
2753 max_offs = i;
2754 }
2755 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002756
Johannes Berg2dc2a152015-06-16 17:09:18 +02002757 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002758 return STA_KEY_IDX_INVALID;
2759
Johannes Berg2dc2a152015-06-16 17:09:18 +02002760 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002761}
2762
Johannes Berg5f7a1842015-12-11 09:36:10 +01002763static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2764 struct ieee80211_vif *vif,
2765 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002766{
Johannes Berg5b530e92014-12-23 16:00:17 +01002767 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002768
Johannes Berg5f7a1842015-12-11 09:36:10 +01002769 if (sta)
2770 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002771
2772 /*
2773 * The device expects GTKs for station interfaces to be
2774 * installed as GTKs for the AP station. If we have no
2775 * station ID, then use AP's station ID.
2776 */
2777 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02002778 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Avri Altman9513c5e2015-10-19 16:29:11 +02002779 u8 sta_id = mvmvif->ap_sta_id;
2780
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002781 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2782 lockdep_is_held(&mvm->mutex));
2783
Avri Altman9513c5e2015-10-19 16:29:11 +02002784 /*
2785 * It is possible that the 'sta' parameter is NULL,
2786 * for example when a GTK is removed - the sta_id will then
2787 * be the AP ID, and no station was passed by mac80211.
2788 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002789 if (IS_ERR_OR_NULL(sta))
2790 return NULL;
2791
2792 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02002793 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002794
Johannes Berg5f7a1842015-12-11 09:36:10 +01002795 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002796}
2797
2798static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
David Spinadel85aeb582017-03-30 19:43:53 +03002799 u32 sta_id,
Sara Sharon45c458b2016-11-09 15:43:26 +02002800 struct ieee80211_key_conf *key, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002801 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2802 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002803{
Sara Sharon45c458b2016-11-09 15:43:26 +02002804 union {
2805 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2806 struct iwl_mvm_add_sta_key_cmd cmd;
2807 } u = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002808 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002809 int ret;
2810 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002811 u16 keyidx;
Sara Sharon45c458b2016-11-09 15:43:26 +02002812 u64 pn = 0;
2813 int i, size;
2814 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2815 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002816
David Spinadel85aeb582017-03-30 19:43:53 +03002817 if (sta_id == IWL_MVM_INVALID_STA)
2818 return -EINVAL;
2819
Sara Sharon45c458b2016-11-09 15:43:26 +02002820 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
Johannes Berg8ca151b2013-01-24 14:25:36 +01002821 STA_KEY_FLG_KEYID_MSK;
2822 key_flags = cpu_to_le16(keyidx);
2823 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2824
Sara Sharon45c458b2016-11-09 15:43:26 +02002825 switch (key->cipher) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002826 case WLAN_CIPHER_SUITE_TKIP:
2827 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002828 if (new_api) {
2829 memcpy((void *)&u.cmd.tx_mic_key,
2830 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
2831 IWL_MIC_KEY_SIZE);
2832
2833 memcpy((void *)&u.cmd.rx_mic_key,
2834 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
2835 IWL_MIC_KEY_SIZE);
2836 pn = atomic64_read(&key->tx_pn);
2837
2838 } else {
2839 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
2840 for (i = 0; i < 5; i++)
2841 u.cmd_v1.tkip_rx_ttak[i] =
2842 cpu_to_le16(tkip_p1k[i]);
2843 }
2844 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002845 break;
2846 case WLAN_CIPHER_SUITE_CCMP:
2847 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Sara Sharon45c458b2016-11-09 15:43:26 +02002848 memcpy(u.cmd.common.key, key->key, key->keylen);
2849 if (new_api)
2850 pn = atomic64_read(&key->tx_pn);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002851 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002852 case WLAN_CIPHER_SUITE_WEP104:
2853 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002854 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002855 case WLAN_CIPHER_SUITE_WEP40:
2856 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002857 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
Johannes Bergba3943b2014-11-12 23:54:48 +01002858 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03002859 case WLAN_CIPHER_SUITE_GCMP_256:
2860 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2861 /* fall through */
2862 case WLAN_CIPHER_SUITE_GCMP:
2863 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002864 memcpy(u.cmd.common.key, key->key, key->keylen);
2865 if (new_api)
2866 pn = atomic64_read(&key->tx_pn);
Ayala Beker2a53d162016-04-07 16:21:57 +03002867 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002868 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002869 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
Sara Sharon45c458b2016-11-09 15:43:26 +02002870 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002871 }
2872
Johannes Bergba3943b2014-11-12 23:54:48 +01002873 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002874 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2875
Sara Sharon45c458b2016-11-09 15:43:26 +02002876 u.cmd.common.key_offset = key_offset;
2877 u.cmd.common.key_flags = key_flags;
David Spinadel85aeb582017-03-30 19:43:53 +03002878 u.cmd.common.sta_id = sta_id;
Sara Sharon45c458b2016-11-09 15:43:26 +02002879
2880 if (new_api) {
2881 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
2882 size = sizeof(u.cmd);
2883 } else {
2884 size = sizeof(u.cmd_v1);
2885 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002886
2887 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002888 if (cmd_flags & CMD_ASYNC)
Sara Sharon45c458b2016-11-09 15:43:26 +02002889 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
2890 &u.cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002891 else
Sara Sharon45c458b2016-11-09 15:43:26 +02002892 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
2893 &u.cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002894
2895 switch (status) {
2896 case ADD_STA_SUCCESS:
2897 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2898 break;
2899 default:
2900 ret = -EIO;
2901 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2902 break;
2903 }
2904
2905 return ret;
2906}
2907
2908static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2909 struct ieee80211_key_conf *keyconf,
2910 u8 sta_id, bool remove_key)
2911{
2912 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2913
2914 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03002915 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2916 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
2917 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2918 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
2919 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
2920 return -EINVAL;
2921
2922 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
2923 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01002924 return -EINVAL;
2925
2926 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2927 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2928
2929 if (remove_key) {
2930 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2931 } else {
2932 struct ieee80211_key_seq seq;
2933 const u8 *pn;
2934
Ayala Bekeraa950522016-06-01 00:28:09 +03002935 switch (keyconf->cipher) {
2936 case WLAN_CIPHER_SUITE_AES_CMAC:
2937 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
2938 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03002939 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2940 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2941 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
2942 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03002943 default:
2944 return -EINVAL;
2945 }
2946
Ayala Beker8e160ab2016-04-11 11:37:38 +03002947 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
2948 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2949 igtk_cmd.ctrl_flags |=
2950 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002951 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2952 pn = seq.aes_cmac.pn;
2953 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2954 ((u64) pn[4] << 8) |
2955 ((u64) pn[3] << 16) |
2956 ((u64) pn[2] << 24) |
2957 ((u64) pn[1] << 32) |
2958 ((u64) pn[0] << 40));
2959 }
2960
2961 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2962 remove_key ? "removing" : "installing",
2963 igtk_cmd.sta_id);
2964
Ayala Beker8e160ab2016-04-11 11:37:38 +03002965 if (!iwl_mvm_has_new_rx_api(mvm)) {
2966 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
2967 .ctrl_flags = igtk_cmd.ctrl_flags,
2968 .key_id = igtk_cmd.key_id,
2969 .sta_id = igtk_cmd.sta_id,
2970 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
2971 };
2972
2973 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
2974 ARRAY_SIZE(igtk_cmd_v1.igtk));
2975 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2976 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
2977 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002978 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002979 sizeof(igtk_cmd), &igtk_cmd);
2980}
2981
2982
2983static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2984 struct ieee80211_vif *vif,
2985 struct ieee80211_sta *sta)
2986{
Johannes Berg5b530e92014-12-23 16:00:17 +01002987 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002988
2989 if (sta)
2990 return sta->addr;
2991
2992 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02002993 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002994 u8 sta_id = mvmvif->ap_sta_id;
2995 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2996 lockdep_is_held(&mvm->mutex));
2997 return sta->addr;
2998 }
2999
3000
3001 return NULL;
3002}
3003
Johannes Berg2f6319d2014-11-12 23:39:56 +01003004static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3005 struct ieee80211_vif *vif,
3006 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01003007 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003008 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01003009 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003010{
Johannes Berg8ca151b2013-01-24 14:25:36 +01003011 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003012 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003013 struct ieee80211_key_seq seq;
3014 u16 p1k[5];
David Spinadel85aeb582017-03-30 19:43:53 +03003015 u32 sta_id;
3016
3017 if (sta) {
3018 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3019
3020 sta_id = mvm_sta->sta_id;
3021 } else if (vif->type == NL80211_IFTYPE_AP &&
3022 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3023 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3024
3025 sta_id = mvmvif->mcast_sta.sta_id;
3026 } else {
3027 IWL_ERR(mvm, "Failed to find station id\n");
3028 return -EINVAL;
3029 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003030
Johannes Berg8ca151b2013-01-24 14:25:36 +01003031 switch (keyconf->cipher) {
3032 case WLAN_CIPHER_SUITE_TKIP:
David Spinadel85aeb582017-03-30 19:43:53 +03003033 if (vif->type == NL80211_IFTYPE_AP) {
3034 ret = -EINVAL;
3035 break;
3036 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003037 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3038 /* get phase 1 key from mac80211 */
3039 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3040 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
David Spinadel85aeb582017-03-30 19:43:53 +03003041 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003042 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003043 break;
3044 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01003045 case WLAN_CIPHER_SUITE_WEP40:
3046 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03003047 case WLAN_CIPHER_SUITE_GCMP:
3048 case WLAN_CIPHER_SUITE_GCMP_256:
David Spinadel85aeb582017-03-30 19:43:53 +03003049 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003050 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003051 break;
3052 default:
David Spinadel85aeb582017-03-30 19:43:53 +03003053 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003054 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003055 }
3056
Johannes Berg8ca151b2013-01-24 14:25:36 +01003057 return ret;
3058}
3059
Johannes Berg2f6319d2014-11-12 23:39:56 +01003060static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01003061 struct ieee80211_key_conf *keyconf,
3062 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003063{
Sara Sharon45c458b2016-11-09 15:43:26 +02003064 union {
3065 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3066 struct iwl_mvm_add_sta_key_cmd cmd;
3067 } u = {};
3068 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3069 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003070 __le16 key_flags;
Sara Sharon45c458b2016-11-09 15:43:26 +02003071 int ret, size;
Johannes Berg79920742014-11-03 15:43:04 +01003072 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003073
David Spinadel85aeb582017-03-30 19:43:53 +03003074 if (sta_id == IWL_MVM_INVALID_STA)
3075 return -EINVAL;
3076
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02003077 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3078 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003079 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3080 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3081
Johannes Bergba3943b2014-11-12 23:54:48 +01003082 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003083 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3084
Sara Sharon45c458b2016-11-09 15:43:26 +02003085 /*
3086 * The fields assigned here are in the same location at the start
3087 * of the command, so we can do this union trick.
3088 */
3089 u.cmd.common.key_flags = key_flags;
3090 u.cmd.common.key_offset = keyconf->hw_key_idx;
3091 u.cmd.common.sta_id = sta_id;
3092
3093 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003094
Johannes Berg8ca151b2013-01-24 14:25:36 +01003095 status = ADD_STA_SUCCESS;
Sara Sharon45c458b2016-11-09 15:43:26 +02003096 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3097 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003098
3099 switch (status) {
3100 case ADD_STA_SUCCESS:
3101 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3102 break;
3103 default:
3104 ret = -EIO;
3105 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3106 break;
3107 }
3108
3109 return ret;
3110}
3111
Johannes Berg2f6319d2014-11-12 23:39:56 +01003112int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3113 struct ieee80211_vif *vif,
3114 struct ieee80211_sta *sta,
3115 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003116 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003117{
Johannes Bergba3943b2014-11-12 23:54:48 +01003118 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003119 struct iwl_mvm_sta *mvm_sta;
David Spinadel85aeb582017-03-30 19:43:53 +03003120 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003121 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03003122 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01003123
3124 lockdep_assert_held(&mvm->mutex);
3125
David Spinadel85aeb582017-03-30 19:43:53 +03003126 if (vif->type != NL80211_IFTYPE_AP ||
3127 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3128 /* Get the station id from the mvm local station table */
3129 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3130 if (!mvm_sta) {
3131 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01003132 return -EINVAL;
3133 }
David Spinadel85aeb582017-03-30 19:43:53 +03003134 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003135
David Spinadel85aeb582017-03-30 19:43:53 +03003136 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3137 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3138 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3139 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id,
3140 false);
3141 goto end;
3142 }
3143
3144 /*
3145 * It is possible that the 'sta' parameter is NULL, and thus
3146 * there is a need to retrieve the sta from the local station
3147 * table.
3148 */
3149 if (!sta) {
3150 sta = rcu_dereference_protected(
3151 mvm->fw_id_to_mac_id[sta_id],
3152 lockdep_is_held(&mvm->mutex));
3153 if (IS_ERR_OR_NULL(sta)) {
3154 IWL_ERR(mvm, "Invalid station id\n");
3155 return -EINVAL;
3156 }
3157 }
3158
3159 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3160 return -EINVAL;
3161 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003162
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003163 /* If the key_offset is not pre-assigned, we need to find a
3164 * new offset to use. In normal cases, the offset is not
3165 * pre-assigned, but during HW_RESTART we want to reuse the
3166 * same indices, so we pass them when this function is called.
3167 *
3168 * In D3 entry, we need to hardcoded the indices (because the
3169 * firmware hardcodes the PTK offset to 0). In this case, we
3170 * need to make sure we don't overwrite the hw_key_idx in the
3171 * keyconf structure, because otherwise we cannot configure
3172 * the original ones back when resuming.
3173 */
3174 if (key_offset == STA_KEY_IDX_INVALID) {
3175 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3176 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003177 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003178 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003179 }
3180
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003181 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003182 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01003183 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003184
3185 /*
3186 * For WEP, the same key is used for multicast and unicast. Upload it
3187 * again, using the same key offset, and now pointing the other one
3188 * to the same key slot (offset).
3189 * If this fails, remove the original as well.
3190 */
David Spinadel85aeb582017-03-30 19:43:53 +03003191 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3192 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3193 sta) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003194 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3195 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01003196 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01003197 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003198 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003199 }
3200 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003201
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003202 __set_bit(key_offset, mvm->fw_key_table);
3203
Johannes Berg2f6319d2014-11-12 23:39:56 +01003204end:
3205 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3206 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03003207 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01003208 return ret;
3209}
3210
3211int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3212 struct ieee80211_vif *vif,
3213 struct ieee80211_sta *sta,
3214 struct ieee80211_key_conf *keyconf)
3215{
Johannes Bergba3943b2014-11-12 23:54:48 +01003216 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003217 struct iwl_mvm_sta *mvm_sta;
Sara Sharon0ae98812017-01-04 14:53:58 +02003218 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2dc2a152015-06-16 17:09:18 +02003219 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003220
3221 lockdep_assert_held(&mvm->mutex);
3222
Johannes Berg5f7a1842015-12-11 09:36:10 +01003223 /* Get the station from the mvm local station table */
3224 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Luca Coelho71793b7d2017-03-30 12:04:47 +03003225 if (mvm_sta)
3226 sta_id = mvm_sta->sta_id;
David Spinadel85aeb582017-03-30 19:43:53 +03003227 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3228 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3229
Johannes Berg2f6319d2014-11-12 23:39:56 +01003230
3231 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3232 keyconf->keyidx, sta_id);
3233
Luca Coelho71793b7d2017-03-30 12:04:47 +03003234 if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3235 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3236 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
Johannes Berg2f6319d2014-11-12 23:39:56 +01003237 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3238
3239 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3240 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3241 keyconf->hw_key_idx);
3242 return -ENOENT;
3243 }
3244
Johannes Berg2dc2a152015-06-16 17:09:18 +02003245 /* track which key was deleted last */
3246 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3247 if (mvm->fw_key_deleted[i] < U8_MAX)
3248 mvm->fw_key_deleted[i]++;
3249 }
3250 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3251
David Spinadel85aeb582017-03-30 19:43:53 +03003252 if (sta && !mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003253 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3254 return 0;
3255 }
3256
Johannes Bergba3943b2014-11-12 23:54:48 +01003257 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3258 if (ret)
3259 return ret;
3260
3261 /* delete WEP key twice to get rid of (now useless) offset */
3262 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3263 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3264 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3265
3266 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003267}
3268
Johannes Berg8ca151b2013-01-24 14:25:36 +01003269void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3270 struct ieee80211_vif *vif,
3271 struct ieee80211_key_conf *keyconf,
3272 struct ieee80211_sta *sta, u32 iv32,
3273 u16 *phase1key)
3274{
Beni Levc3eb5362013-02-06 17:22:18 +02003275 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01003276 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003277
Beni Levc3eb5362013-02-06 17:22:18 +02003278 rcu_read_lock();
3279
Johannes Berg5f7a1842015-12-11 09:36:10 +01003280 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3281 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003282 goto unlock;
David Spinadel85aeb582017-03-30 19:43:53 +03003283 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003284 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003285
3286 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02003287 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01003288}
3289
Johannes Berg9cc40712013-02-15 22:47:48 +01003290void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3291 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003292{
Johannes Berg5b577a92013-11-14 18:20:04 +01003293 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003294 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003295 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003296 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03003297 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01003298 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003299 };
3300 int ret;
3301
Sara Sharon854c5702016-01-26 13:17:47 +02003302 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3303 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003304 if (ret)
3305 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3306}
3307
Johannes Berg9cc40712013-02-15 22:47:48 +01003308void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3309 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003310 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003311 u16 cnt, u16 tids, bool more_data,
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003312 bool single_sta_queue)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003313{
Johannes Berg5b577a92013-11-14 18:20:04 +01003314 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003315 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003316 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003317 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003318 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3319 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003320 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003321 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003322 int tid, ret;
3323 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003324
Johannes Berg3e56ead2013-02-15 22:23:18 +01003325 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3326 * Note that this field is reserved and unused by firmware not
3327 * supporting GO uAPSD, so it's safe to always do this.
3328 */
3329 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3330 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3331
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003332 /* If we're releasing frames from aggregation or dqa queues then check
3333 * if all the queues that we're releasing frames from, combined, have:
Johannes Berg3e56ead2013-02-15 22:23:18 +01003334 * - more frames than the service period, in which case more_data
3335 * needs to be set
3336 * - fewer than 'cnt' frames, in which case we need to adjust the
3337 * firmware command (but do that unconditionally)
3338 */
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003339 if (single_sta_queue) {
Johannes Berg3e56ead2013-02-15 22:23:18 +01003340 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003341 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003342
3343 spin_lock_bh(&mvmsta->lock);
3344 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3345 struct iwl_mvm_tid_data *tid_data;
3346 u16 n_queued;
3347
3348 tid_data = &mvmsta->tid_data[tid];
Johannes Berg3e56ead2013-02-15 22:23:18 +01003349
Liad Kaufmandd321622017-04-05 16:25:11 +03003350 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003351 if (n_queued > remaining) {
3352 more_data = true;
3353 remaining = 0;
3354 break;
3355 }
3356 remaining -= n_queued;
3357 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003358 sleep_tx_count = cnt - remaining;
3359 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3360 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003361 spin_unlock_bh(&mvmsta->lock);
3362
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003363 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003364 if (WARN_ON(cnt - remaining == 0)) {
3365 ieee80211_sta_eosp(sta);
3366 return;
3367 }
3368 }
3369
3370 /* Note: this is ignored by firmware not supporting GO uAPSD */
3371 if (more_data)
Sara Sharonced19f22017-02-06 19:09:32 +02003372 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003373
3374 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3375 mvmsta->next_status_eosp = true;
Sara Sharonced19f22017-02-06 19:09:32 +02003376 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003377 } else {
Sara Sharonced19f22017-02-06 19:09:32 +02003378 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003379 }
3380
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003381 /* block the Tx queues until the FW updated the sleep Tx count */
3382 iwl_trans_block_txq_ptrs(mvm->trans, true);
3383
3384 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3385 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003386 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003387 if (ret)
3388 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3389}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003390
Johannes Berg04168412015-06-23 21:22:09 +02003391void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3392 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003393{
3394 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3395 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3396 struct ieee80211_sta *sta;
3397 u32 sta_id = le32_to_cpu(notif->sta_id);
3398
3399 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003400 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003401
3402 rcu_read_lock();
3403 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3404 if (!IS_ERR_OR_NULL(sta))
3405 ieee80211_sta_eosp(sta);
3406 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003407}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003408
3409void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3410 struct iwl_mvm_sta *mvmsta, bool disable)
3411{
3412 struct iwl_mvm_add_sta_cmd cmd = {
3413 .add_modify = STA_MODE_MODIFY,
3414 .sta_id = mvmsta->sta_id,
3415 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3416 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3417 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3418 };
3419 int ret;
3420
Sara Sharon854c5702016-01-26 13:17:47 +02003421 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3422 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003423 if (ret)
3424 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3425}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003426
3427void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3428 struct ieee80211_sta *sta,
3429 bool disable)
3430{
3431 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3432
3433 spin_lock_bh(&mvm_sta->lock);
3434
3435 if (mvm_sta->disable_tx == disable) {
3436 spin_unlock_bh(&mvm_sta->lock);
3437 return;
3438 }
3439
3440 mvm_sta->disable_tx = disable;
3441
Johannes Bergc8f54702017-06-19 23:50:31 +02003442 /* Tell mac80211 to start/stop queuing tx for this station */
3443 ieee80211_sta_block_awake(mvm->hw, sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003444
3445 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3446
3447 spin_unlock_bh(&mvm_sta->lock);
3448}
3449
Sara Sharonced19f22017-02-06 19:09:32 +02003450static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3451 struct iwl_mvm_vif *mvmvif,
3452 struct iwl_mvm_int_sta *sta,
3453 bool disable)
3454{
3455 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3456 struct iwl_mvm_add_sta_cmd cmd = {
3457 .add_modify = STA_MODE_MODIFY,
3458 .sta_id = sta->sta_id,
3459 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3460 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3461 .mac_id_n_color = cpu_to_le32(id),
3462 };
3463 int ret;
3464
3465 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3466 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3467 if (ret)
3468 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3469}
3470
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003471void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3472 struct iwl_mvm_vif *mvmvif,
3473 bool disable)
3474{
3475 struct ieee80211_sta *sta;
3476 struct iwl_mvm_sta *mvm_sta;
3477 int i;
3478
3479 lockdep_assert_held(&mvm->mutex);
3480
3481 /* Block/unblock all the stations of the given mvmvif */
Sara Sharon0ae98812017-01-04 14:53:58 +02003482 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003483 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3484 lockdep_is_held(&mvm->mutex));
3485 if (IS_ERR_OR_NULL(sta))
3486 continue;
3487
3488 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3489 if (mvm_sta->mac_id_n_color !=
3490 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3491 continue;
3492
3493 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3494 }
Sara Sharonced19f22017-02-06 19:09:32 +02003495
3496 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3497 return;
3498
3499 /* Need to block/unblock also multicast station */
3500 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3501 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3502 &mvmvif->mcast_sta, disable);
3503
3504 /*
3505 * Only unblock the broadcast station (FW blocks it for immediate
3506 * quiet, not the driver)
3507 */
3508 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3509 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3510 &mvmvif->bcast_sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003511}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003512
3513void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3514{
3515 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3516 struct iwl_mvm_sta *mvmsta;
3517
3518 rcu_read_lock();
3519
3520 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3521
3522 if (!WARN_ON(!mvmsta))
3523 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3524
3525 rcu_read_unlock();
3526}
Liad Kaufmandd321622017-04-05 16:25:11 +03003527
3528u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3529{
3530 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3531
3532 /*
3533 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
3534 * to align the wrap around of ssn so we compare relevant values.
3535 */
3536 if (mvm->trans->cfg->gen2)
3537 sn &= 0xff;
3538
3539 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3540}