blob: f88202c38d4c77d14ad55273f642d4f8b557ebe2 [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
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001280static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1281 struct iwl_mvm_int_sta *sta,
1282 const u8 *addr,
1283 u16 mac_id, u16 color)
1284{
1285 struct iwl_mvm_add_sta_cmd cmd;
1286 int ret;
1287 u32 status;
1288
1289 lockdep_assert_held(&mvm->mutex);
1290
1291 memset(&cmd, 0, sizeof(cmd));
1292 cmd.sta_id = sta->sta_id;
1293 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1294 color));
1295 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1296 cmd.station_type = sta->type;
1297
1298 if (!iwl_mvm_has_new_tx_api(mvm))
1299 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1300 cmd.tid_disable_tx = cpu_to_le16(0xffff);
1301
1302 if (addr)
1303 memcpy(cmd.addr, addr, ETH_ALEN);
1304
1305 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1306 iwl_mvm_add_sta_cmd_size(mvm),
1307 &cmd, &status);
1308 if (ret)
1309 return ret;
1310
1311 switch (status & IWL_ADD_STA_STATUS_MASK) {
1312 case ADD_STA_SUCCESS:
1313 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1314 return 0;
1315 default:
1316 ret = -EIO;
1317 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1318 status);
1319 break;
1320 }
1321 return ret;
1322}
1323
Johannes Berg8ca151b2013-01-24 14:25:36 +01001324int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1325 struct ieee80211_vif *vif,
1326 struct ieee80211_sta *sta)
1327{
1328 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001329 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001330 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001331 int i, ret, sta_id;
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001332 bool sta_update = false;
1333 unsigned int sta_flags = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001334
1335 lockdep_assert_held(&mvm->mutex);
1336
1337 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001338 sta_id = iwl_mvm_find_free_sta_id(mvm,
1339 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001340 else
1341 sta_id = mvm_sta->sta_id;
1342
Sara Sharon0ae98812017-01-04 14:53:58 +02001343 if (sta_id == IWL_MVM_INVALID_STA)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001344 return -ENOSPC;
1345
1346 spin_lock_init(&mvm_sta->lock);
1347
Johannes Bergc8f54702017-06-19 23:50:31 +02001348 /* if this is a HW restart re-alloc existing queues */
1349 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001350 struct iwl_mvm_int_sta tmp_sta = {
1351 .sta_id = sta_id,
1352 .type = mvm_sta->sta_type,
1353 };
1354
1355 /*
1356 * First add an empty station since allocating
1357 * a queue requires a valid station
1358 */
1359 ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1360 mvmvif->id, mvmvif->color);
1361 if (ret)
1362 goto err;
1363
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001364 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001365 sta_update = true;
1366 sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001367 goto update_fw;
1368 }
1369
Johannes Berg8ca151b2013-01-24 14:25:36 +01001370 mvm_sta->sta_id = sta_id;
1371 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1372 mvmvif->color);
1373 mvm_sta->vif = vif;
Liad Kaufmana58bb462017-05-28 14:20:04 +03001374 if (!mvm->trans->cfg->gen2)
1375 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1376 else
1377 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001378 mvm_sta->tx_protection = 0;
1379 mvm_sta->tt_tx_protection = false;
Sara Sharonced19f22017-02-06 19:09:32 +02001380 mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001381
1382 /* HW restart, don't assume the memory has been zeroed */
Liad Kaufman69191af2015-09-01 18:50:22 +03001383 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001385
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001386 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001387 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001388 u16 seq = mvm_sta->tid_data[i].seq_number;
1389 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1390 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001391
Liad Kaufman24afba72015-07-28 18:56:08 +03001392 /*
1393 * Mark all queues for this STA as unallocated and defer TX
1394 * frames until the queue is allocated
1395 */
Sara Sharon6862fce2017-02-22 19:34:17 +02001396 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001397 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001398 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001399 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001400 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001401
Sara Sharona571f5f2015-12-07 12:50:58 +02001402 if (iwl_mvm_has_new_rx_api(mvm) &&
1403 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Johannes Berg92c4dca2017-06-07 10:35:54 +02001404 int q;
1405
Sara Sharona571f5f2015-12-07 12:50:58 +02001406 dup_data = kcalloc(mvm->trans->num_rx_queues,
Johannes Berg92c4dca2017-06-07 10:35:54 +02001407 sizeof(*dup_data), GFP_KERNEL);
Sara Sharona571f5f2015-12-07 12:50:58 +02001408 if (!dup_data)
1409 return -ENOMEM;
Johannes Berg92c4dca2017-06-07 10:35:54 +02001410 /*
1411 * Initialize all the last_seq values to 0xffff which can never
1412 * compare equal to the frame's seq_ctrl in the check in
1413 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1414 * number and fragmented packets don't reach that function.
1415 *
1416 * This thus allows receiving a packet with seqno 0 and the
1417 * retry bit set as the very first packet on a new TID.
1418 */
1419 for (q = 0; q < mvm->trans->num_rx_queues; q++)
1420 memset(dup_data[q].last_seq, 0xff,
1421 sizeof(dup_data[q].last_seq));
Sara Sharona571f5f2015-12-07 12:50:58 +02001422 mvm_sta->dup_data = dup_data;
1423 }
1424
Johannes Bergc8f54702017-06-19 23:50:31 +02001425 if (!iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001426 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1427 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001428 if (ret)
1429 goto err;
1430 }
1431
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001432update_fw:
Shaul Triebitz732d06e2017-07-10 19:58:10 +03001433 ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001434 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001435 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001436
Johannes Berg9e848012014-08-04 14:33:42 +02001437 if (vif->type == NL80211_IFTYPE_STATION) {
1438 if (!sta->tdls) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001439 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001440 mvmvif->ap_sta_id = sta_id;
1441 } else {
Sara Sharon0ae98812017-01-04 14:53:58 +02001442 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001443 }
1444 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001445
1446 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1447
1448 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001449
1450err:
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001451 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001452}
1453
1454int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1455 bool drain)
1456{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001457 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001458 int ret;
1459 u32 status;
1460
1461 lockdep_assert_held(&mvm->mutex);
1462
1463 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1464 cmd.sta_id = mvmsta->sta_id;
1465 cmd.add_modify = STA_MODE_MODIFY;
1466 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1467 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1468
1469 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001470 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1471 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001472 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001473 if (ret)
1474 return ret;
1475
Sara Sharon837c4da2016-01-07 16:50:45 +02001476 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001477 case ADD_STA_SUCCESS:
1478 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1479 mvmsta->sta_id);
1480 break;
1481 default:
1482 ret = -EIO;
1483 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1484 mvmsta->sta_id);
1485 break;
1486 }
1487
1488 return ret;
1489}
1490
1491/*
1492 * Remove a station from the FW table. Before sending the command to remove
1493 * the station validate that the station is indeed known to the driver (sanity
1494 * only).
1495 */
1496static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1497{
1498 struct ieee80211_sta *sta;
1499 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1500 .sta_id = sta_id,
1501 };
1502 int ret;
1503
1504 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1505 lockdep_is_held(&mvm->mutex));
1506
1507 /* Note: internal stations are marked as error values */
1508 if (!sta) {
1509 IWL_ERR(mvm, "Invalid station id\n");
1510 return -EINVAL;
1511 }
1512
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001513 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001514 sizeof(rm_sta_cmd), &rm_sta_cmd);
1515 if (ret) {
1516 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1517 return ret;
1518 }
1519
1520 return 0;
1521}
1522
Liad Kaufman24afba72015-07-28 18:56:08 +03001523static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1524 struct ieee80211_vif *vif,
1525 struct iwl_mvm_sta *mvm_sta)
1526{
1527 int ac;
1528 int i;
1529
1530 lockdep_assert_held(&mvm->mutex);
1531
1532 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
Sara Sharon6862fce2017-02-22 19:34:17 +02001533 if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
Liad Kaufman24afba72015-07-28 18:56:08 +03001534 continue;
1535
1536 ac = iwl_mvm_tid_to_ac_queue(i);
1537 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1538 vif->hw_queue[ac], i, 0);
Sara Sharon6862fce2017-02-22 19:34:17 +02001539 mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
Liad Kaufman24afba72015-07-28 18:56:08 +03001540 }
1541}
1542
Sara Sharond6d517b2017-03-06 10:16:11 +02001543int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1544 struct iwl_mvm_sta *mvm_sta)
1545{
Sharon Dvirbec95222017-06-12 11:40:33 +03001546 int i;
Sara Sharond6d517b2017-03-06 10:16:11 +02001547
1548 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1549 u16 txq_id;
Sharon Dvirbec95222017-06-12 11:40:33 +03001550 int ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001551
1552 spin_lock_bh(&mvm_sta->lock);
1553 txq_id = mvm_sta->tid_data[i].txq_id;
1554 spin_unlock_bh(&mvm_sta->lock);
1555
1556 if (txq_id == IWL_MVM_INVALID_QUEUE)
1557 continue;
1558
1559 ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1560 if (ret)
Sharon Dvirbec95222017-06-12 11:40:33 +03001561 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001562 }
1563
Sharon Dvirbec95222017-06-12 11:40:33 +03001564 return 0;
Sara Sharond6d517b2017-03-06 10:16:11 +02001565}
1566
Johannes Berg8ca151b2013-01-24 14:25:36 +01001567int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1568 struct ieee80211_vif *vif,
1569 struct ieee80211_sta *sta)
1570{
1571 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001572 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001573 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001574 int ret;
1575
1576 lockdep_assert_held(&mvm->mutex);
1577
Sara Sharona571f5f2015-12-07 12:50:58 +02001578 if (iwl_mvm_has_new_rx_api(mvm))
1579 kfree(mvm_sta->dup_data);
1580
Johannes Bergc8f54702017-06-19 23:50:31 +02001581 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1582 if (ret)
1583 return ret;
Sara Sharond6d517b2017-03-06 10:16:11 +02001584
Johannes Bergc8f54702017-06-19 23:50:31 +02001585 /* flush its queues here since we are freeing mvm_sta */
1586 ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1587 if (ret)
1588 return ret;
1589 if (iwl_mvm_has_new_tx_api(mvm)) {
1590 ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1591 } else {
1592 u32 q_mask = mvm_sta->tfd_queue_msk;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001593
Johannes Bergc8f54702017-06-19 23:50:31 +02001594 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1595 q_mask);
1596 }
1597 if (ret)
1598 return ret;
Liad Kaufman56214742016-09-22 15:14:08 +03001599
Johannes Bergc8f54702017-06-19 23:50:31 +02001600 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001601
Johannes Bergc8f54702017-06-19 23:50:31 +02001602 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001603
Johannes Bergc8f54702017-06-19 23:50:31 +02001604 /* If there is a TXQ still marked as reserved - free it */
1605 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1606 u8 reserved_txq = mvm_sta->reserved_queue;
1607 enum iwl_mvm_queue_status *status;
1608
1609 /*
1610 * If no traffic has gone through the reserved TXQ - it
1611 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1612 * should be manually marked as free again
1613 */
1614 spin_lock_bh(&mvm->queue_info_lock);
1615 status = &mvm->queue_info[reserved_txq].status;
1616 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1617 (*status != IWL_MVM_QUEUE_FREE),
1618 "sta_id %d reserved txq %d status %d",
1619 sta_id, reserved_txq, *status)) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001620 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Bergc8f54702017-06-19 23:50:31 +02001621 return -EINVAL;
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001622 }
1623
Johannes Bergc8f54702017-06-19 23:50:31 +02001624 *status = IWL_MVM_QUEUE_FREE;
1625 spin_unlock_bh(&mvm->queue_info_lock);
1626 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001627
Johannes Bergc8f54702017-06-19 23:50:31 +02001628 if (vif->type == NL80211_IFTYPE_STATION &&
1629 mvmvif->ap_sta_id == sta_id) {
1630 /* if associated - we can't remove the AP STA now */
1631 if (vif->bss_conf.assoc)
1632 return ret;
Eliad Peller37577fe2013-12-05 17:19:39 +02001633
Johannes Bergc8f54702017-06-19 23:50:31 +02001634 /* unassoc - go ahead - remove the AP STA now */
1635 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1636
1637 /* clear d0i3_ap_sta_id if no longer relevant */
1638 if (mvm->d0i3_ap_sta_id == sta_id)
1639 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001640 }
1641
1642 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001643 * This shouldn't happen - the TDLS channel switch should be canceled
1644 * before the STA is removed.
1645 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001646 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001647 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001648 cancel_delayed_work(&mvm->tdls_cs.dwork);
1649 }
1650
1651 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001652 * Make sure that the tx response code sees the station as -EBUSY and
1653 * calls the drain worker.
1654 */
1655 spin_lock_bh(&mvm_sta->lock);
Johannes Bergc8f54702017-06-19 23:50:31 +02001656 spin_unlock_bh(&mvm_sta->lock);
Sara Sharon94c3e612016-12-07 15:04:37 +02001657
Johannes Bergc8f54702017-06-19 23:50:31 +02001658 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1659 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001660
1661 return ret;
1662}
1663
1664int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1665 struct ieee80211_vif *vif,
1666 u8 sta_id)
1667{
1668 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1669
1670 lockdep_assert_held(&mvm->mutex);
1671
Monam Agarwalc531c772014-03-24 00:05:56 +05301672 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001673 return ret;
1674}
1675
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001676int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1677 struct iwl_mvm_int_sta *sta,
Sara Sharonced19f22017-02-06 19:09:32 +02001678 u32 qmask, enum nl80211_iftype iftype,
1679 enum iwl_sta_type type)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001680{
1681 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001682 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Sara Sharon0ae98812017-01-04 14:53:58 +02001683 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001684 return -ENOSPC;
1685 }
1686
1687 sta->tfd_queue_msk = qmask;
Sara Sharonced19f22017-02-06 19:09:32 +02001688 sta->type = type;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001689
1690 /* put a non-NULL value so iterating over the stations won't stop */
1691 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1692 return 0;
1693}
1694
Sara Sharon26d6c162017-01-03 12:00:19 +02001695void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001696{
Monam Agarwalc531c772014-03-24 00:05:56 +05301697 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001698 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
Sara Sharon0ae98812017-01-04 14:53:58 +02001699 sta->sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001700}
1701
Sara Sharonc5a719e2016-11-15 10:20:48 +02001702static void iwl_mvm_enable_aux_queue(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001703{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001704 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1705 mvm->cfg->base_params->wd_timeout :
1706 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001707
Sara Sharon310181e2017-01-17 14:27:48 +02001708 if (iwl_mvm_has_new_tx_api(mvm)) {
1709 int queue = iwl_mvm_tvqm_enable_txq(mvm, mvm->aux_queue,
1710 mvm->aux_sta.sta_id,
1711 IWL_MAX_TID_COUNT,
1712 wdg_timeout);
1713 mvm->aux_queue = queue;
Johannes Bergc8f54702017-06-19 23:50:31 +02001714 } else {
Liad Kaufman28d07932015-09-01 16:36:25 +03001715 struct iwl_trans_txq_scd_cfg cfg = {
1716 .fifo = IWL_MVM_TX_FIFO_MCAST,
1717 .sta_id = mvm->aux_sta.sta_id,
1718 .tid = IWL_MAX_TID_COUNT,
1719 .aggregate = false,
1720 .frame_limit = IWL_FRAME_LIMIT,
1721 };
1722
1723 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1724 wdg_timeout);
1725 }
Sara Sharonc5a719e2016-11-15 10:20:48 +02001726}
1727
1728int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1729{
1730 int ret;
1731
1732 lockdep_assert_held(&mvm->mutex);
1733
1734 /* Allocate aux station and assign to it the aux queue */
1735 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Sara Sharonced19f22017-02-06 19:09:32 +02001736 NL80211_IFTYPE_UNSPECIFIED,
1737 IWL_STA_AUX_ACTIVITY);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001738 if (ret)
1739 return ret;
1740
1741 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1742 if (!iwl_mvm_has_new_tx_api(mvm))
1743 iwl_mvm_enable_aux_queue(mvm);
Liad Kaufman28d07932015-09-01 16:36:25 +03001744
Johannes Berg8ca151b2013-01-24 14:25:36 +01001745 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1746 MAC_INDEX_AUX, 0);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001747 if (ret) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001748 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001749 return ret;
1750 }
1751
1752 /*
1753 * For a000 firmware and on we cannot add queue to a station unknown
1754 * to firmware so enable queue here - after the station was added
1755 */
1756 if (iwl_mvm_has_new_tx_api(mvm))
1757 iwl_mvm_enable_aux_queue(mvm);
1758
1759 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001760}
1761
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001762int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1763{
1764 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1765
1766 lockdep_assert_held(&mvm->mutex);
1767 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1768 mvmvif->id, 0);
1769}
1770
1771int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1772{
1773 int ret;
1774
1775 lockdep_assert_held(&mvm->mutex);
1776
1777 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1778 if (ret)
1779 IWL_WARN(mvm, "Failed sending remove station\n");
1780
1781 return ret;
1782}
1783
1784void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1785{
1786 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1787}
1788
Johannes Berg712b24a2014-08-04 14:14:14 +02001789void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1790{
1791 lockdep_assert_held(&mvm->mutex);
1792
1793 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1794}
1795
Johannes Berg8ca151b2013-01-24 14:25:36 +01001796/*
1797 * Send the add station command for the vif's broadcast station.
1798 * Assumes that the station was already allocated.
1799 *
1800 * @mvm: the mvm component
1801 * @vif: the interface to which the broadcast station is added
1802 * @bsta: the broadcast station to add.
1803 */
Johannes Berg013290a2014-08-04 13:38:48 +02001804int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001805{
1806 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001807 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001808 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001809 const u8 *baddr = _baddr;
Johannes Berg7daa7622017-02-24 12:02:22 +01001810 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001811 int ret;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001812 unsigned int wdg_timeout =
1813 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1814 struct iwl_trans_txq_scd_cfg cfg = {
1815 .fifo = IWL_MVM_TX_FIFO_VO,
1816 .sta_id = mvmvif->bcast_sta.sta_id,
1817 .tid = IWL_MAX_TID_COUNT,
1818 .aggregate = false,
1819 .frame_limit = IWL_FRAME_LIMIT,
1820 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001821
1822 lockdep_assert_held(&mvm->mutex);
1823
Johannes Bergc8f54702017-06-19 23:50:31 +02001824 if (!iwl_mvm_has_new_tx_api(mvm)) {
Liad Kaufman4d339982017-03-21 17:13:16 +02001825 if (vif->type == NL80211_IFTYPE_AP ||
1826 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon49f71712017-01-09 12:07:16 +02001827 queue = mvm->probe_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001828 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
Sara Sharon49f71712017-01-09 12:07:16 +02001829 queue = mvm->p2p_dev_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001830 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
Liad Kaufmande24f632015-08-04 15:19:18 +03001831 return -EINVAL;
1832
Liad Kaufmandf88c082016-11-24 15:31:00 +02001833 bsta->tfd_queue_msk |= BIT(queue);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001834
Sara Sharon310181e2017-01-17 14:27:48 +02001835 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1836 &cfg, wdg_timeout);
Liad Kaufmande24f632015-08-04 15:19:18 +03001837 }
1838
Johannes Berg5023d962013-07-31 14:07:43 +02001839 if (vif->type == NL80211_IFTYPE_ADHOC)
1840 baddr = vif->bss_conf.bssid;
1841
Sara Sharon0ae98812017-01-04 14:53:58 +02001842 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001843 return -ENOSPC;
1844
Liad Kaufmandf88c082016-11-24 15:31:00 +02001845 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1846 mvmvif->id, mvmvif->color);
1847 if (ret)
1848 return ret;
1849
1850 /*
Sara Sharonc5a719e2016-11-15 10:20:48 +02001851 * For a000 firmware and on we cannot add queue to a station unknown
1852 * to firmware so enable queue here - after the station was added
Liad Kaufmandf88c082016-11-24 15:31:00 +02001853 */
Sara Sharon310181e2017-01-17 14:27:48 +02001854 if (iwl_mvm_has_new_tx_api(mvm)) {
Johannes Berg7daa7622017-02-24 12:02:22 +01001855 queue = iwl_mvm_tvqm_enable_txq(mvm, vif->hw_queue[0],
1856 bsta->sta_id,
1857 IWL_MAX_TID_COUNT,
1858 wdg_timeout);
1859
Luca Coelho7b758a12017-06-20 13:40:03 +03001860 if (vif->type == NL80211_IFTYPE_AP ||
1861 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon310181e2017-01-17 14:27:48 +02001862 mvm->probe_queue = queue;
1863 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1864 mvm->p2p_dev_queue = queue;
Sara Sharon310181e2017-01-17 14:27:48 +02001865 }
Liad Kaufmandf88c082016-11-24 15:31:00 +02001866
1867 return 0;
1868}
1869
1870static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
1871 struct ieee80211_vif *vif)
1872{
1873 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001874 int queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001875
1876 lockdep_assert_held(&mvm->mutex);
1877
Sara Sharond49394a2017-03-05 13:01:08 +02001878 iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
1879
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001880 switch (vif->type) {
1881 case NL80211_IFTYPE_AP:
1882 case NL80211_IFTYPE_ADHOC:
1883 queue = mvm->probe_queue;
1884 break;
1885 case NL80211_IFTYPE_P2P_DEVICE:
1886 queue = mvm->p2p_dev_queue;
1887 break;
1888 default:
1889 WARN(1, "Can't free bcast queue on vif type %d\n",
1890 vif->type);
1891 return;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001892 }
1893
Mordechai Goodsteind167e812017-05-10 16:42:53 +03001894 iwl_mvm_disable_txq(mvm, queue, vif->hw_queue[0], IWL_MAX_TID_COUNT, 0);
1895 if (iwl_mvm_has_new_tx_api(mvm))
1896 return;
1897
1898 WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
1899 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001900}
1901
1902/* Send the FW a request to remove the station from it's internal data
1903 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001904int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001905{
Johannes Berg013290a2014-08-04 13:38:48 +02001906 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001907 int ret;
1908
1909 lockdep_assert_held(&mvm->mutex);
1910
Johannes Bergc8f54702017-06-19 23:50:31 +02001911 iwl_mvm_free_bcast_sta_queues(mvm, vif);
Liad Kaufmandf88c082016-11-24 15:31:00 +02001912
Johannes Berg013290a2014-08-04 13:38:48 +02001913 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001914 if (ret)
1915 IWL_WARN(mvm, "Failed sending remove station\n");
1916 return ret;
1917}
1918
Johannes Berg013290a2014-08-04 13:38:48 +02001919int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1920{
1921 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001922
1923 lockdep_assert_held(&mvm->mutex);
1924
Johannes Bergc8f54702017-06-19 23:50:31 +02001925 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
Sara Sharonced19f22017-02-06 19:09:32 +02001926 ieee80211_vif_type_p2p(vif),
1927 IWL_STA_GENERAL_PURPOSE);
Johannes Berg013290a2014-08-04 13:38:48 +02001928}
1929
Johannes Berg8ca151b2013-01-24 14:25:36 +01001930/* Allocate a new station entry for the broadcast station to the given vif,
1931 * and send it to the FW.
1932 * Note that each P2P mac should have its own broadcast station.
1933 *
1934 * @mvm: the mvm component
1935 * @vif: the interface to which the broadcast station is added
1936 * @bsta: the broadcast station to add. */
Luca Coelhod1973582017-06-22 16:00:25 +03001937int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001938{
1939 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001940 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001941 int ret;
1942
1943 lockdep_assert_held(&mvm->mutex);
1944
Johannes Berg013290a2014-08-04 13:38:48 +02001945 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001946 if (ret)
1947 return ret;
1948
Johannes Berg013290a2014-08-04 13:38:48 +02001949 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001950
1951 if (ret)
1952 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001953
Johannes Berg8ca151b2013-01-24 14:25:36 +01001954 return ret;
1955}
1956
Johannes Berg013290a2014-08-04 13:38:48 +02001957void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1958{
1959 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1960
1961 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1962}
1963
Johannes Berg8ca151b2013-01-24 14:25:36 +01001964/*
1965 * Send the FW a request to remove the station from it's internal data
1966 * structures, and in addition remove it from the local data structure.
1967 */
Luca Coelhod1973582017-06-22 16:00:25 +03001968int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001969{
1970 int ret;
1971
1972 lockdep_assert_held(&mvm->mutex);
1973
Johannes Berg013290a2014-08-04 13:38:48 +02001974 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001975
Johannes Berg013290a2014-08-04 13:38:48 +02001976 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1977
Johannes Berg8ca151b2013-01-24 14:25:36 +01001978 return ret;
1979}
1980
Sara Sharon26d6c162017-01-03 12:00:19 +02001981/*
1982 * Allocate a new station entry for the multicast station to the given vif,
1983 * and send it to the FW.
1984 * Note that each AP/GO mac should have its own multicast station.
1985 *
1986 * @mvm: the mvm component
1987 * @vif: the interface to which the multicast station is added
1988 */
1989int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1990{
1991 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1992 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
1993 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
1994 const u8 *maddr = _maddr;
1995 struct iwl_trans_txq_scd_cfg cfg = {
1996 .fifo = IWL_MVM_TX_FIFO_MCAST,
1997 .sta_id = msta->sta_id,
1998 .tid = IWL_MAX_TID_COUNT,
1999 .aggregate = false,
2000 .frame_limit = IWL_FRAME_LIMIT,
2001 };
2002 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2003 int ret;
2004
2005 lockdep_assert_held(&mvm->mutex);
2006
Liad Kaufmanee48b722017-03-21 17:13:16 +02002007 if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2008 vif->type != NL80211_IFTYPE_ADHOC))
Sara Sharon26d6c162017-01-03 12:00:19 +02002009 return -ENOTSUPP;
2010
Sara Sharonced19f22017-02-06 19:09:32 +02002011 /*
2012 * While in previous FWs we had to exclude cab queue from TFD queue
2013 * mask, now it is needed as any other queue.
2014 */
2015 if (!iwl_mvm_has_new_tx_api(mvm) &&
2016 fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2017 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2018 &cfg, timeout);
2019 msta->tfd_queue_msk |= BIT(vif->cab_queue);
2020 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002021 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2022 mvmvif->id, mvmvif->color);
2023 if (ret) {
2024 iwl_mvm_dealloc_int_sta(mvm, msta);
2025 return ret;
2026 }
2027
2028 /*
2029 * Enable cab queue after the ADD_STA command is sent.
2030 * This is needed for a000 firmware which won't accept SCD_QUEUE_CFG
Sara Sharonced19f22017-02-06 19:09:32 +02002031 * command with unknown station id, and for FW that doesn't support
2032 * station API since the cab queue is not included in the
2033 * tfd_queue_mask.
Sara Sharon26d6c162017-01-03 12:00:19 +02002034 */
Sara Sharon310181e2017-01-17 14:27:48 +02002035 if (iwl_mvm_has_new_tx_api(mvm)) {
2036 int queue = iwl_mvm_tvqm_enable_txq(mvm, vif->cab_queue,
2037 msta->sta_id,
2038 IWL_MAX_TID_COUNT,
2039 timeout);
Sara Sharone2af3fa2017-02-22 19:35:10 +02002040 mvmvif->cab_queue = queue;
Sara Sharonced19f22017-02-06 19:09:32 +02002041 } else if (!fw_has_api(&mvm->fw->ucode_capa,
2042 IWL_UCODE_TLV_API_STA_TYPE)) {
Liad Kaufmanee48b722017-03-21 17:13:16 +02002043 /*
2044 * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2045 * invalid, so make sure we use the queue we want.
2046 * Note that this is done here as we want to avoid making DQA
2047 * changes in mac80211 layer.
2048 */
2049 if (vif->type == NL80211_IFTYPE_ADHOC) {
2050 vif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2051 mvmvif->cab_queue = vif->cab_queue;
2052 }
Sara Sharon310181e2017-01-17 14:27:48 +02002053 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0,
2054 &cfg, timeout);
2055 }
Sara Sharon26d6c162017-01-03 12:00:19 +02002056
2057 return 0;
2058}
2059
2060/*
2061 * Send the FW a request to remove the station from it's internal data
2062 * structures, and in addition remove it from the local data structure.
2063 */
2064int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2065{
2066 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2067 int ret;
2068
2069 lockdep_assert_held(&mvm->mutex);
2070
Sara Sharond49394a2017-03-05 13:01:08 +02002071 iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2072
Sara Sharone2af3fa2017-02-22 19:35:10 +02002073 iwl_mvm_disable_txq(mvm, mvmvif->cab_queue, vif->cab_queue,
Sara Sharon26d6c162017-01-03 12:00:19 +02002074 IWL_MAX_TID_COUNT, 0);
2075
2076 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2077 if (ret)
2078 IWL_WARN(mvm, "Failed sending remove station\n");
2079
2080 return ret;
2081}
2082
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002083#define IWL_MAX_RX_BA_SESSIONS 16
2084
Sara Sharonb915c102016-03-23 16:32:02 +02002085static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02002086{
Sara Sharonb915c102016-03-23 16:32:02 +02002087 struct iwl_mvm_delba_notif notif = {
2088 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2089 .metadata.sync = 1,
2090 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02002091 };
Sara Sharonb915c102016-03-23 16:32:02 +02002092 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2093};
Sara Sharon10b2b202016-03-20 16:23:41 +02002094
Sara Sharonb915c102016-03-23 16:32:02 +02002095static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2096 struct iwl_mvm_baid_data *data)
2097{
2098 int i;
2099
2100 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2101
2102 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2103 int j;
2104 struct iwl_mvm_reorder_buffer *reorder_buf =
2105 &data->reorder_buf[i];
2106
Sara Sharon06904052016-02-28 20:28:17 +02002107 spin_lock_bh(&reorder_buf->lock);
2108 if (likely(!reorder_buf->num_stored)) {
2109 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02002110 continue;
Sara Sharon06904052016-02-28 20:28:17 +02002111 }
Sara Sharonb915c102016-03-23 16:32:02 +02002112
2113 /*
2114 * This shouldn't happen in regular DELBA since the internal
2115 * delBA notification should trigger a release of all frames in
2116 * the reorder buffer.
2117 */
2118 WARN_ON(1);
2119
2120 for (j = 0; j < reorder_buf->buf_size; j++)
2121 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02002122 /*
2123 * Prevent timer re-arm. This prevents a very far fetched case
2124 * where we timed out on the notification. There may be prior
2125 * RX frames pending in the RX queue before the notification
2126 * that might get processed between now and the actual deletion
2127 * and we would re-arm the timer although we are deleting the
2128 * reorder buffer.
2129 */
2130 reorder_buf->removed = true;
2131 spin_unlock_bh(&reorder_buf->lock);
2132 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02002133 }
2134}
2135
2136static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2137 u32 sta_id,
2138 struct iwl_mvm_baid_data *data,
2139 u16 ssn, u8 buf_size)
2140{
2141 int i;
2142
2143 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2144 struct iwl_mvm_reorder_buffer *reorder_buf =
2145 &data->reorder_buf[i];
2146 int j;
2147
2148 reorder_buf->num_stored = 0;
2149 reorder_buf->head_sn = ssn;
2150 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02002151 /* rx reorder timer */
2152 reorder_buf->reorder_timer.function =
2153 iwl_mvm_reorder_timer_expired;
2154 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
2155 init_timer(&reorder_buf->reorder_timer);
2156 spin_lock_init(&reorder_buf->lock);
2157 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02002158 reorder_buf->queue = i;
2159 reorder_buf->sta_id = sta_id;
Sara Sharon5d43eab2017-02-02 12:51:39 +02002160 reorder_buf->valid = false;
Sara Sharonb915c102016-03-23 16:32:02 +02002161 for (j = 0; j < reorder_buf->buf_size; j++)
2162 __skb_queue_head_init(&reorder_buf->entries[j]);
2163 }
Sara Sharon10b2b202016-03-20 16:23:41 +02002164}
2165
Johannes Berg8ca151b2013-01-24 14:25:36 +01002166int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02002167 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002168{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002169 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002170 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02002171 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002172 int ret;
2173 u32 status;
2174
2175 lockdep_assert_held(&mvm->mutex);
2176
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002177 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2178 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2179 return -ENOSPC;
2180 }
2181
Sara Sharon10b2b202016-03-20 16:23:41 +02002182 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2183 /*
2184 * Allocate here so if allocation fails we can bail out early
2185 * before starting the BA session in the firmware
2186 */
Sara Sharonb915c102016-03-23 16:32:02 +02002187 baid_data = kzalloc(sizeof(*baid_data) +
2188 mvm->trans->num_rx_queues *
2189 sizeof(baid_data->reorder_buf[0]),
2190 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02002191 if (!baid_data)
2192 return -ENOMEM;
2193 }
2194
Johannes Berg8ca151b2013-01-24 14:25:36 +01002195 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2196 cmd.sta_id = mvm_sta->sta_id;
2197 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002198 if (start) {
2199 cmd.add_immediate_ba_tid = (u8) tid;
2200 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02002201 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002202 } else {
2203 cmd.remove_immediate_ba_tid = (u8) tid;
2204 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002205 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2206 STA_MODIFY_REMOVE_BA_TID;
2207
2208 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002209 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2210 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002211 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002212 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02002213 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002214
Sara Sharon837c4da2016-01-07 16:50:45 +02002215 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002216 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03002217 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2218 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002219 break;
2220 case ADD_STA_IMMEDIATE_BA_FAILURE:
2221 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2222 ret = -ENOSPC;
2223 break;
2224 default:
2225 ret = -EIO;
2226 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2227 start ? "start" : "stopp", status);
2228 break;
2229 }
2230
Sara Sharon10b2b202016-03-20 16:23:41 +02002231 if (ret)
2232 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002233
Sara Sharon10b2b202016-03-20 16:23:41 +02002234 if (start) {
2235 u8 baid;
2236
2237 mvm->rx_ba_sessions++;
2238
2239 if (!iwl_mvm_has_new_rx_api(mvm))
2240 return 0;
2241
2242 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2243 ret = -EINVAL;
2244 goto out_free;
2245 }
2246 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2247 IWL_ADD_STA_BAID_SHIFT);
2248 baid_data->baid = baid;
2249 baid_data->timeout = timeout;
2250 baid_data->last_rx = jiffies;
Wei Yongjun72c240f2016-07-12 11:40:57 +00002251 setup_timer(&baid_data->session_timer,
2252 iwl_mvm_rx_agg_session_expired,
2253 (unsigned long)&mvm->baid_map[baid]);
Sara Sharon10b2b202016-03-20 16:23:41 +02002254 baid_data->mvm = mvm;
2255 baid_data->tid = tid;
2256 baid_data->sta_id = mvm_sta->sta_id;
2257
2258 mvm_sta->tid_to_baid[tid] = baid;
2259 if (timeout)
2260 mod_timer(&baid_data->session_timer,
2261 TU_TO_EXP_TIME(timeout * 2));
2262
Sara Sharonb915c102016-03-23 16:32:02 +02002263 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2264 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002265 /*
2266 * protect the BA data with RCU to cover a case where our
2267 * internal RX sync mechanism will timeout (not that it's
2268 * supposed to happen) and we will free the session data while
2269 * RX is being processed in parallel
2270 */
Sara Sharon35263a02016-06-21 12:12:10 +03002271 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2272 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002273 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2274 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002275 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002276 u8 baid = mvm_sta->tid_to_baid[tid];
2277
Sara Sharon60dec522016-06-21 14:14:08 +03002278 if (mvm->rx_ba_sessions > 0)
2279 /* check that restart flow didn't zero the counter */
2280 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002281 if (!iwl_mvm_has_new_rx_api(mvm))
2282 return 0;
2283
2284 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2285 return -EINVAL;
2286
2287 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2288 if (WARN_ON(!baid_data))
2289 return -EINVAL;
2290
2291 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002292 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002293 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002294 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2295 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002296 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002297 }
2298 return 0;
2299
2300out_free:
2301 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002302 return ret;
2303}
2304
Liad Kaufman9794c642015-08-19 17:34:28 +03002305int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2306 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002307{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002308 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002309 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002310 int ret;
2311 u32 status;
2312
2313 lockdep_assert_held(&mvm->mutex);
2314
2315 if (start) {
2316 mvm_sta->tfd_queue_msk |= BIT(queue);
2317 mvm_sta->tid_disable_agg &= ~BIT(tid);
2318 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002319 /* In DQA-mode the queue isn't removed on agg termination */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002320 mvm_sta->tid_disable_agg |= BIT(tid);
2321 }
2322
2323 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2324 cmd.sta_id = mvm_sta->sta_id;
2325 cmd.add_modify = STA_MODE_MODIFY;
Sara Sharonbb497012016-09-29 14:52:40 +03002326 if (!iwl_mvm_has_new_tx_api(mvm))
2327 cmd.modify_mask = STA_MODIFY_QUEUES;
2328 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002329 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2330 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2331
2332 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002333 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2334 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002335 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002336 if (ret)
2337 return ret;
2338
Sara Sharon837c4da2016-01-07 16:50:45 +02002339 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002340 case ADD_STA_SUCCESS:
2341 break;
2342 default:
2343 ret = -EIO;
2344 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2345 start ? "start" : "stopp", status);
2346 break;
2347 }
2348
2349 return ret;
2350}
2351
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002352const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002353 IEEE80211_AC_BE,
2354 IEEE80211_AC_BK,
2355 IEEE80211_AC_BK,
2356 IEEE80211_AC_BE,
2357 IEEE80211_AC_VI,
2358 IEEE80211_AC_VI,
2359 IEEE80211_AC_VO,
2360 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002361 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002362};
2363
Johannes Berg3e56ead2013-02-15 22:23:18 +01002364static const u8 tid_to_ucode_ac[] = {
2365 AC_BE,
2366 AC_BK,
2367 AC_BK,
2368 AC_BE,
2369 AC_VI,
2370 AC_VI,
2371 AC_VO,
2372 AC_VO,
2373};
2374
Johannes Berg8ca151b2013-01-24 14:25:36 +01002375int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2376 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2377{
Johannes Berg5b577a92013-11-14 18:20:04 +01002378 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002379 struct iwl_mvm_tid_data *tid_data;
Liad Kaufmandd321622017-04-05 16:25:11 +03002380 u16 normalized_ssn;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002381 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002382 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002383
2384 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2385 return -EINVAL;
2386
2387 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2388 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2389 mvmsta->tid_data[tid].state);
2390 return -ENXIO;
2391 }
2392
2393 lockdep_assert_held(&mvm->mutex);
2394
Arik Nemtsovb2492502014-03-13 12:21:50 +02002395 spin_lock_bh(&mvmsta->lock);
2396
2397 /* possible race condition - we entered D0i3 while starting agg */
2398 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2399 spin_unlock_bh(&mvmsta->lock);
2400 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2401 return -EIO;
2402 }
2403
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002404 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002405
Liad Kaufmancf961e12015-08-13 19:16:08 +03002406 /*
2407 * Note the possible cases:
2408 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2409 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2410 * one and mark it as reserved
2411 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2412 * non-DQA mode, since the TXQ hasn't yet been allocated
Sara Sharon34e10862017-02-23 13:15:07 +02002413 * Don't support case 3 for new TX path as it is not expected to happen
2414 * and aggregation will be offloaded soon anyway
Liad Kaufmancf961e12015-08-13 19:16:08 +03002415 */
2416 txq_id = mvmsta->tid_data[tid].txq_id;
Sara Sharon34e10862017-02-23 13:15:07 +02002417 if (iwl_mvm_has_new_tx_api(mvm)) {
2418 if (txq_id == IWL_MVM_INVALID_QUEUE) {
2419 ret = -ENXIO;
2420 goto release_locks;
2421 }
Johannes Bergc8f54702017-06-19 23:50:31 +02002422 } else if (unlikely(mvm->queue_info[txq_id].status ==
Sara Sharon34e10862017-02-23 13:15:07 +02002423 IWL_MVM_QUEUE_SHARED)) {
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002424 ret = -ENXIO;
2425 IWL_DEBUG_TX_QUEUES(mvm,
2426 "Can't start tid %d agg on shared queue!\n",
2427 tid);
2428 goto release_locks;
Johannes Bergc8f54702017-06-19 23:50:31 +02002429 } else if (mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002430 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
Johannes Bergc8f54702017-06-19 23:50:31 +02002431 IWL_MVM_DQA_MIN_DATA_QUEUE,
2432 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002433 if (txq_id < 0) {
2434 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002435 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2436 goto release_locks;
2437 }
Sara Sharon01796ff2016-11-16 17:04:36 +02002438 /*
2439 * TXQ shouldn't be in inactive mode for non-DQA, so getting
2440 * an inactive queue from iwl_mvm_find_free_queue() is
2441 * certainly a bug
2442 */
2443 WARN_ON(mvm->queue_info[txq_id].status ==
2444 IWL_MVM_QUEUE_INACTIVE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002445
2446 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2447 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002448 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002449
2450 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002451
Liad Kaufmancf961e12015-08-13 19:16:08 +03002452 IWL_DEBUG_TX_QUEUES(mvm,
2453 "AGG for tid %d will be on queue #%d\n",
2454 tid, txq_id);
2455
Johannes Berg8ca151b2013-01-24 14:25:36 +01002456 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002457 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002458 tid_data->txq_id = txq_id;
2459 *ssn = tid_data->ssn;
2460
2461 IWL_DEBUG_TX_QUEUES(mvm,
2462 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2463 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2464 tid_data->next_reclaimed);
2465
Liad Kaufmandd321622017-04-05 16:25:11 +03002466 /*
2467 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
2468 * to align the wrap around of ssn so we compare relevant values.
2469 */
2470 normalized_ssn = tid_data->ssn;
2471 if (mvm->trans->cfg->gen2)
2472 normalized_ssn &= 0xff;
2473
2474 if (normalized_ssn == tid_data->next_reclaimed) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002475 tid_data->state = IWL_AGG_STARTING;
2476 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2477 } else {
2478 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2479 }
2480
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002481 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002482 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002483
2484release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002485 spin_unlock(&mvm->queue_info_lock);
2486out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002487 spin_unlock_bh(&mvmsta->lock);
2488
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002489 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002490}
2491
2492int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002493 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2494 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002495{
Johannes Berg5b577a92013-11-14 18:20:04 +01002496 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002497 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002498 unsigned int wdg_timeout =
2499 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002500 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002501 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002502 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002503 u16 ssn;
2504
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002505 struct iwl_trans_txq_scd_cfg cfg = {
2506 .sta_id = mvmsta->sta_id,
2507 .tid = tid,
2508 .frame_limit = buf_size,
2509 .aggregate = true,
2510 };
2511
Eyal Shapiraefed6642014-09-14 15:58:53 +03002512 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2513 != IWL_MAX_TID_COUNT);
2514
Liad Kaufmana58bb462017-05-28 14:20:04 +03002515 if (!mvm->trans->cfg->gen2)
2516 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2517 else
2518 buf_size = min_t(int, buf_size,
2519 LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002520
2521 spin_lock_bh(&mvmsta->lock);
2522 ssn = tid_data->ssn;
2523 queue = tid_data->txq_id;
2524 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002525 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002526 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002527 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002528 spin_unlock_bh(&mvmsta->lock);
2529
Sara Sharon34e10862017-02-23 13:15:07 +02002530 if (iwl_mvm_has_new_tx_api(mvm)) {
2531 /*
2532 * If no queue iwl_mvm_sta_tx_agg_start() would have failed so
2533 * no need to check queue's status
2534 */
2535 if (buf_size < mvmsta->max_agg_bufsize)
2536 return -ENOTSUPP;
2537
2538 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2539 if (ret)
2540 return -EIO;
2541 goto out;
2542 }
2543
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002544 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002545
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002546 spin_lock_bh(&mvm->queue_info_lock);
2547 queue_status = mvm->queue_info[queue].status;
2548 spin_unlock_bh(&mvm->queue_info_lock);
2549
Johannes Bergc8f54702017-06-19 23:50:31 +02002550 /* Maybe there is no need to even alloc a queue... */
2551 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2552 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002553
Johannes Bergc8f54702017-06-19 23:50:31 +02002554 /*
2555 * Only reconfig the SCD for the queue if the window size has
2556 * changed from current (become smaller)
2557 */
2558 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002559 /*
Johannes Bergc8f54702017-06-19 23:50:31 +02002560 * If reconfiguring an existing queue, it first must be
2561 * drained
Liad Kaufmancf961e12015-08-13 19:16:08 +03002562 */
Johannes Bergc8f54702017-06-19 23:50:31 +02002563 ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2564 BIT(queue));
2565 if (ret) {
2566 IWL_ERR(mvm,
2567 "Error draining queue before reconfig\n");
2568 return ret;
2569 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03002570
Johannes Bergc8f54702017-06-19 23:50:31 +02002571 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2572 mvmsta->sta_id, tid,
2573 buf_size, ssn);
2574 if (ret) {
2575 IWL_ERR(mvm,
2576 "Error reconfiguring TXQ #%d\n", queue);
2577 return ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002578 }
2579 }
2580
2581 if (alloc_queue)
2582 iwl_mvm_enable_txq(mvm, queue,
2583 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2584 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002585
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002586 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2587 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2588 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2589 if (ret)
2590 return -EIO;
2591 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002592
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002593 /* No need to mark as reserved */
2594 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002595 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002596 spin_unlock_bh(&mvm->queue_info_lock);
2597
Sara Sharon34e10862017-02-23 13:15:07 +02002598out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002599 /*
2600 * Even though in theory the peer could have different
2601 * aggregation reorder buffer sizes for different sessions,
2602 * our ucode doesn't allow for that and has a global limit
2603 * for each station. Therefore, use the minimum of all the
2604 * aggregation sessions and our default value.
2605 */
2606 mvmsta->max_agg_bufsize =
2607 min(mvmsta->max_agg_bufsize, buf_size);
2608 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2609
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002610 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2611 sta->addr, tid);
2612
Eyal Shapira9e680942013-11-09 00:16:16 +02002613 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002614}
2615
Sara Sharon34e10862017-02-23 13:15:07 +02002616static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2617 struct iwl_mvm_sta *mvmsta,
2618 u16 txq_id)
2619{
2620 if (iwl_mvm_has_new_tx_api(mvm))
2621 return;
2622
2623 spin_lock_bh(&mvm->queue_info_lock);
2624 /*
2625 * The TXQ is marked as reserved only if no traffic came through yet
2626 * This means no traffic has been sent on this TID (agg'd or not), so
2627 * we no longer have use for the queue. Since it hasn't even been
2628 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2629 * free.
2630 */
2631 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2632 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
2633
2634 spin_unlock_bh(&mvm->queue_info_lock);
2635}
2636
Johannes Berg8ca151b2013-01-24 14:25:36 +01002637int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2638 struct ieee80211_sta *sta, u16 tid)
2639{
Johannes Berg5b577a92013-11-14 18:20:04 +01002640 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002641 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2642 u16 txq_id;
2643 int err;
2644
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002645 /*
2646 * If mac80211 is cleaning its state, then say that we finished since
2647 * our state has been cleared anyway.
2648 */
2649 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2650 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2651 return 0;
2652 }
2653
Johannes Berg8ca151b2013-01-24 14:25:36 +01002654 spin_lock_bh(&mvmsta->lock);
2655
2656 txq_id = tid_data->txq_id;
2657
2658 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2659 mvmsta->sta_id, tid, txq_id, tid_data->state);
2660
Eyal Shapiraefed6642014-09-14 15:58:53 +03002661 mvmsta->agg_tids &= ~BIT(tid);
2662
Sara Sharon34e10862017-02-23 13:15:07 +02002663 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002664
Johannes Berg8ca151b2013-01-24 14:25:36 +01002665 switch (tid_data->state) {
2666 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002667 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002668
2669 IWL_DEBUG_TX_QUEUES(mvm,
2670 "ssn = %d, next_recl = %d\n",
2671 tid_data->ssn, tid_data->next_reclaimed);
2672
Johannes Berg8ca151b2013-01-24 14:25:36 +01002673 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002674 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002675 spin_unlock_bh(&mvmsta->lock);
2676
2677 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2678
2679 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
Johannes Bergf7f89e72014-08-05 15:24:44 +02002680 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002681 case IWL_AGG_STARTING:
2682 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2683 /*
2684 * The agg session has been stopped before it was set up. This
2685 * can happen when the AddBA timer times out for example.
2686 */
2687
2688 /* No barriers since we are under mutex */
2689 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002690
2691 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2692 tid_data->state = IWL_AGG_OFF;
2693 err = 0;
2694 break;
2695 default:
2696 IWL_ERR(mvm,
2697 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2698 mvmsta->sta_id, tid, tid_data->state);
2699 IWL_ERR(mvm,
2700 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2701 err = -EINVAL;
2702 }
2703
2704 spin_unlock_bh(&mvmsta->lock);
2705
2706 return err;
2707}
2708
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002709int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2710 struct ieee80211_sta *sta, u16 tid)
2711{
Johannes Berg5b577a92013-11-14 18:20:04 +01002712 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002713 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2714 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002715 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002716
2717 /*
2718 * First set the agg state to OFF to avoid calling
2719 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2720 */
2721 spin_lock_bh(&mvmsta->lock);
2722 txq_id = tid_data->txq_id;
2723 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2724 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002725 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002726 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002727 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002728 spin_unlock_bh(&mvmsta->lock);
2729
Sara Sharon34e10862017-02-23 13:15:07 +02002730 iwl_mvm_unreserve_agg_queue(mvm, mvmsta, txq_id);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002731
Johannes Bergb6658ff2013-07-24 13:55:51 +02002732 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002733 iwl_mvm_drain_sta(mvm, mvmsta, true);
Sara Sharond6d517b2017-03-06 10:16:11 +02002734
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002735 if (iwl_mvm_has_new_tx_api(mvm)) {
2736 if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
2737 BIT(tid), 0))
2738 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002739 iwl_trans_wait_txq_empty(mvm->trans, txq_id);
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002740 } else {
2741 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
2742 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Sara Sharond6d517b2017-03-06 10:16:11 +02002743 iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
Mordechai Goodsteind167e812017-05-10 16:42:53 +03002744 }
Sara Sharond6d517b2017-03-06 10:16:11 +02002745
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002746 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002747
Johannes Bergf7f89e72014-08-05 15:24:44 +02002748 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002749 }
2750
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002751 return 0;
2752}
2753
Johannes Berg8ca151b2013-01-24 14:25:36 +01002754static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2755{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002756 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002757
2758 lockdep_assert_held(&mvm->mutex);
2759
Johannes Berg2dc2a152015-06-16 17:09:18 +02002760 /* Pick the unused key offset with the highest 'deleted'
2761 * counter. Every time a key is deleted, all the counters
2762 * are incremented and the one that was just deleted is
2763 * reset to zero. Thus, the highest counter is the one
2764 * that was deleted longest ago. Pick that one.
2765 */
2766 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2767 if (test_bit(i, mvm->fw_key_table))
2768 continue;
2769 if (mvm->fw_key_deleted[i] > max) {
2770 max = mvm->fw_key_deleted[i];
2771 max_offs = i;
2772 }
2773 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002774
Johannes Berg2dc2a152015-06-16 17:09:18 +02002775 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002776 return STA_KEY_IDX_INVALID;
2777
Johannes Berg2dc2a152015-06-16 17:09:18 +02002778 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002779}
2780
Johannes Berg5f7a1842015-12-11 09:36:10 +01002781static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2782 struct ieee80211_vif *vif,
2783 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002784{
Johannes Berg5b530e92014-12-23 16:00:17 +01002785 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002786
Johannes Berg5f7a1842015-12-11 09:36:10 +01002787 if (sta)
2788 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002789
2790 /*
2791 * The device expects GTKs for station interfaces to be
2792 * installed as GTKs for the AP station. If we have no
2793 * station ID, then use AP's station ID.
2794 */
2795 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02002796 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Avri Altman9513c5e2015-10-19 16:29:11 +02002797 u8 sta_id = mvmvif->ap_sta_id;
2798
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002799 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2800 lockdep_is_held(&mvm->mutex));
2801
Avri Altman9513c5e2015-10-19 16:29:11 +02002802 /*
2803 * It is possible that the 'sta' parameter is NULL,
2804 * for example when a GTK is removed - the sta_id will then
2805 * be the AP ID, and no station was passed by mac80211.
2806 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002807 if (IS_ERR_OR_NULL(sta))
2808 return NULL;
2809
2810 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02002811 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002812
Johannes Berg5f7a1842015-12-11 09:36:10 +01002813 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002814}
2815
2816static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
David Spinadel85aeb582017-03-30 19:43:53 +03002817 u32 sta_id,
Sara Sharon45c458b2016-11-09 15:43:26 +02002818 struct ieee80211_key_conf *key, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002819 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2820 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002821{
Sara Sharon45c458b2016-11-09 15:43:26 +02002822 union {
2823 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2824 struct iwl_mvm_add_sta_key_cmd cmd;
2825 } u = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002826 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002827 int ret;
2828 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002829 u16 keyidx;
Sara Sharon45c458b2016-11-09 15:43:26 +02002830 u64 pn = 0;
2831 int i, size;
2832 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2833 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002834
David Spinadel85aeb582017-03-30 19:43:53 +03002835 if (sta_id == IWL_MVM_INVALID_STA)
2836 return -EINVAL;
2837
Sara Sharon45c458b2016-11-09 15:43:26 +02002838 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
Johannes Berg8ca151b2013-01-24 14:25:36 +01002839 STA_KEY_FLG_KEYID_MSK;
2840 key_flags = cpu_to_le16(keyidx);
2841 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2842
Sara Sharon45c458b2016-11-09 15:43:26 +02002843 switch (key->cipher) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002844 case WLAN_CIPHER_SUITE_TKIP:
2845 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002846 if (new_api) {
2847 memcpy((void *)&u.cmd.tx_mic_key,
2848 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
2849 IWL_MIC_KEY_SIZE);
2850
2851 memcpy((void *)&u.cmd.rx_mic_key,
2852 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
2853 IWL_MIC_KEY_SIZE);
2854 pn = atomic64_read(&key->tx_pn);
2855
2856 } else {
2857 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
2858 for (i = 0; i < 5; i++)
2859 u.cmd_v1.tkip_rx_ttak[i] =
2860 cpu_to_le16(tkip_p1k[i]);
2861 }
2862 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002863 break;
2864 case WLAN_CIPHER_SUITE_CCMP:
2865 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Sara Sharon45c458b2016-11-09 15:43:26 +02002866 memcpy(u.cmd.common.key, key->key, key->keylen);
2867 if (new_api)
2868 pn = atomic64_read(&key->tx_pn);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002869 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002870 case WLAN_CIPHER_SUITE_WEP104:
2871 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002872 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002873 case WLAN_CIPHER_SUITE_WEP40:
2874 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002875 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
Johannes Bergba3943b2014-11-12 23:54:48 +01002876 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03002877 case WLAN_CIPHER_SUITE_GCMP_256:
2878 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2879 /* fall through */
2880 case WLAN_CIPHER_SUITE_GCMP:
2881 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002882 memcpy(u.cmd.common.key, key->key, key->keylen);
2883 if (new_api)
2884 pn = atomic64_read(&key->tx_pn);
Ayala Beker2a53d162016-04-07 16:21:57 +03002885 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002886 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002887 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
Sara Sharon45c458b2016-11-09 15:43:26 +02002888 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002889 }
2890
Johannes Bergba3943b2014-11-12 23:54:48 +01002891 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002892 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2893
Sara Sharon45c458b2016-11-09 15:43:26 +02002894 u.cmd.common.key_offset = key_offset;
2895 u.cmd.common.key_flags = key_flags;
David Spinadel85aeb582017-03-30 19:43:53 +03002896 u.cmd.common.sta_id = sta_id;
Sara Sharon45c458b2016-11-09 15:43:26 +02002897
2898 if (new_api) {
2899 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
2900 size = sizeof(u.cmd);
2901 } else {
2902 size = sizeof(u.cmd_v1);
2903 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002904
2905 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002906 if (cmd_flags & CMD_ASYNC)
Sara Sharon45c458b2016-11-09 15:43:26 +02002907 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
2908 &u.cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002909 else
Sara Sharon45c458b2016-11-09 15:43:26 +02002910 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
2911 &u.cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002912
2913 switch (status) {
2914 case ADD_STA_SUCCESS:
2915 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2916 break;
2917 default:
2918 ret = -EIO;
2919 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2920 break;
2921 }
2922
2923 return ret;
2924}
2925
2926static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2927 struct ieee80211_key_conf *keyconf,
2928 u8 sta_id, bool remove_key)
2929{
2930 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2931
2932 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03002933 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2934 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
2935 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2936 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
2937 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
2938 return -EINVAL;
2939
2940 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
2941 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01002942 return -EINVAL;
2943
2944 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2945 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2946
2947 if (remove_key) {
2948 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2949 } else {
2950 struct ieee80211_key_seq seq;
2951 const u8 *pn;
2952
Ayala Bekeraa950522016-06-01 00:28:09 +03002953 switch (keyconf->cipher) {
2954 case WLAN_CIPHER_SUITE_AES_CMAC:
2955 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
2956 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03002957 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2958 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2959 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
2960 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03002961 default:
2962 return -EINVAL;
2963 }
2964
Ayala Beker8e160ab2016-04-11 11:37:38 +03002965 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
2966 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2967 igtk_cmd.ctrl_flags |=
2968 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002969 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2970 pn = seq.aes_cmac.pn;
2971 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2972 ((u64) pn[4] << 8) |
2973 ((u64) pn[3] << 16) |
2974 ((u64) pn[2] << 24) |
2975 ((u64) pn[1] << 32) |
2976 ((u64) pn[0] << 40));
2977 }
2978
2979 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2980 remove_key ? "removing" : "installing",
2981 igtk_cmd.sta_id);
2982
Ayala Beker8e160ab2016-04-11 11:37:38 +03002983 if (!iwl_mvm_has_new_rx_api(mvm)) {
2984 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
2985 .ctrl_flags = igtk_cmd.ctrl_flags,
2986 .key_id = igtk_cmd.key_id,
2987 .sta_id = igtk_cmd.sta_id,
2988 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
2989 };
2990
2991 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
2992 ARRAY_SIZE(igtk_cmd_v1.igtk));
2993 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2994 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
2995 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002996 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002997 sizeof(igtk_cmd), &igtk_cmd);
2998}
2999
3000
3001static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3002 struct ieee80211_vif *vif,
3003 struct ieee80211_sta *sta)
3004{
Johannes Berg5b530e92014-12-23 16:00:17 +01003005 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003006
3007 if (sta)
3008 return sta->addr;
3009
3010 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02003011 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003012 u8 sta_id = mvmvif->ap_sta_id;
3013 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3014 lockdep_is_held(&mvm->mutex));
3015 return sta->addr;
3016 }
3017
3018
3019 return NULL;
3020}
3021
Johannes Berg2f6319d2014-11-12 23:39:56 +01003022static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3023 struct ieee80211_vif *vif,
3024 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01003025 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003026 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01003027 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003028{
Johannes Berg8ca151b2013-01-24 14:25:36 +01003029 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003030 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003031 struct ieee80211_key_seq seq;
3032 u16 p1k[5];
David Spinadel85aeb582017-03-30 19:43:53 +03003033 u32 sta_id;
3034
3035 if (sta) {
3036 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3037
3038 sta_id = mvm_sta->sta_id;
3039 } else if (vif->type == NL80211_IFTYPE_AP &&
3040 !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3041 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3042
3043 sta_id = mvmvif->mcast_sta.sta_id;
3044 } else {
3045 IWL_ERR(mvm, "Failed to find station id\n");
3046 return -EINVAL;
3047 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003048
Johannes Berg8ca151b2013-01-24 14:25:36 +01003049 switch (keyconf->cipher) {
3050 case WLAN_CIPHER_SUITE_TKIP:
David Spinadel85aeb582017-03-30 19:43:53 +03003051 if (vif->type == NL80211_IFTYPE_AP) {
3052 ret = -EINVAL;
3053 break;
3054 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01003055 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3056 /* get phase 1 key from mac80211 */
3057 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3058 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
David Spinadel85aeb582017-03-30 19:43:53 +03003059 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003060 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003061 break;
3062 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01003063 case WLAN_CIPHER_SUITE_WEP40:
3064 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03003065 case WLAN_CIPHER_SUITE_GCMP:
3066 case WLAN_CIPHER_SUITE_GCMP_256:
David Spinadel85aeb582017-03-30 19:43:53 +03003067 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003068 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003069 break;
3070 default:
David Spinadel85aeb582017-03-30 19:43:53 +03003071 ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003072 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003073 }
3074
Johannes Berg8ca151b2013-01-24 14:25:36 +01003075 return ret;
3076}
3077
Johannes Berg2f6319d2014-11-12 23:39:56 +01003078static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01003079 struct ieee80211_key_conf *keyconf,
3080 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003081{
Sara Sharon45c458b2016-11-09 15:43:26 +02003082 union {
3083 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3084 struct iwl_mvm_add_sta_key_cmd cmd;
3085 } u = {};
3086 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3087 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003088 __le16 key_flags;
Sara Sharon45c458b2016-11-09 15:43:26 +02003089 int ret, size;
Johannes Berg79920742014-11-03 15:43:04 +01003090 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003091
David Spinadel85aeb582017-03-30 19:43:53 +03003092 if (sta_id == IWL_MVM_INVALID_STA)
3093 return -EINVAL;
3094
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02003095 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3096 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003097 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3098 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3099
Johannes Bergba3943b2014-11-12 23:54:48 +01003100 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003101 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3102
Sara Sharon45c458b2016-11-09 15:43:26 +02003103 /*
3104 * The fields assigned here are in the same location at the start
3105 * of the command, so we can do this union trick.
3106 */
3107 u.cmd.common.key_flags = key_flags;
3108 u.cmd.common.key_offset = keyconf->hw_key_idx;
3109 u.cmd.common.sta_id = sta_id;
3110
3111 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003112
Johannes Berg8ca151b2013-01-24 14:25:36 +01003113 status = ADD_STA_SUCCESS;
Sara Sharon45c458b2016-11-09 15:43:26 +02003114 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3115 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003116
3117 switch (status) {
3118 case ADD_STA_SUCCESS:
3119 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3120 break;
3121 default:
3122 ret = -EIO;
3123 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3124 break;
3125 }
3126
3127 return ret;
3128}
3129
Johannes Berg2f6319d2014-11-12 23:39:56 +01003130int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3131 struct ieee80211_vif *vif,
3132 struct ieee80211_sta *sta,
3133 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003134 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003135{
Johannes Bergba3943b2014-11-12 23:54:48 +01003136 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003137 struct iwl_mvm_sta *mvm_sta;
David Spinadel85aeb582017-03-30 19:43:53 +03003138 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003139 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03003140 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01003141
3142 lockdep_assert_held(&mvm->mutex);
3143
David Spinadel85aeb582017-03-30 19:43:53 +03003144 if (vif->type != NL80211_IFTYPE_AP ||
3145 keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3146 /* Get the station id from the mvm local station table */
3147 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3148 if (!mvm_sta) {
3149 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01003150 return -EINVAL;
3151 }
David Spinadel85aeb582017-03-30 19:43:53 +03003152 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003153
David Spinadel85aeb582017-03-30 19:43:53 +03003154 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3155 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3156 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3157 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id,
3158 false);
3159 goto end;
3160 }
3161
3162 /*
3163 * It is possible that the 'sta' parameter is NULL, and thus
3164 * there is a need to retrieve the sta from the local station
3165 * table.
3166 */
3167 if (!sta) {
3168 sta = rcu_dereference_protected(
3169 mvm->fw_id_to_mac_id[sta_id],
3170 lockdep_is_held(&mvm->mutex));
3171 if (IS_ERR_OR_NULL(sta)) {
3172 IWL_ERR(mvm, "Invalid station id\n");
3173 return -EINVAL;
3174 }
3175 }
3176
3177 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3178 return -EINVAL;
3179 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003180
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003181 /* If the key_offset is not pre-assigned, we need to find a
3182 * new offset to use. In normal cases, the offset is not
3183 * pre-assigned, but during HW_RESTART we want to reuse the
3184 * same indices, so we pass them when this function is called.
3185 *
3186 * In D3 entry, we need to hardcoded the indices (because the
3187 * firmware hardcodes the PTK offset to 0). In this case, we
3188 * need to make sure we don't overwrite the hw_key_idx in the
3189 * keyconf structure, because otherwise we cannot configure
3190 * the original ones back when resuming.
3191 */
3192 if (key_offset == STA_KEY_IDX_INVALID) {
3193 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3194 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003195 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003196 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003197 }
3198
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003199 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003200 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01003201 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003202
3203 /*
3204 * For WEP, the same key is used for multicast and unicast. Upload it
3205 * again, using the same key offset, and now pointing the other one
3206 * to the same key slot (offset).
3207 * If this fails, remove the original as well.
3208 */
David Spinadel85aeb582017-03-30 19:43:53 +03003209 if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3210 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3211 sta) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003212 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3213 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01003214 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01003215 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003216 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003217 }
3218 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003219
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003220 __set_bit(key_offset, mvm->fw_key_table);
3221
Johannes Berg2f6319d2014-11-12 23:39:56 +01003222end:
3223 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3224 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03003225 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01003226 return ret;
3227}
3228
3229int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3230 struct ieee80211_vif *vif,
3231 struct ieee80211_sta *sta,
3232 struct ieee80211_key_conf *keyconf)
3233{
Johannes Bergba3943b2014-11-12 23:54:48 +01003234 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003235 struct iwl_mvm_sta *mvm_sta;
Sara Sharon0ae98812017-01-04 14:53:58 +02003236 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2dc2a152015-06-16 17:09:18 +02003237 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003238
3239 lockdep_assert_held(&mvm->mutex);
3240
Johannes Berg5f7a1842015-12-11 09:36:10 +01003241 /* Get the station from the mvm local station table */
3242 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Luca Coelho71793b7d2017-03-30 12:04:47 +03003243 if (mvm_sta)
3244 sta_id = mvm_sta->sta_id;
David Spinadel85aeb582017-03-30 19:43:53 +03003245 else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3246 sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3247
Johannes Berg2f6319d2014-11-12 23:39:56 +01003248
3249 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3250 keyconf->keyidx, sta_id);
3251
Luca Coelho71793b7d2017-03-30 12:04:47 +03003252 if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3253 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3254 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
Johannes Berg2f6319d2014-11-12 23:39:56 +01003255 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3256
3257 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3258 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3259 keyconf->hw_key_idx);
3260 return -ENOENT;
3261 }
3262
Johannes Berg2dc2a152015-06-16 17:09:18 +02003263 /* track which key was deleted last */
3264 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3265 if (mvm->fw_key_deleted[i] < U8_MAX)
3266 mvm->fw_key_deleted[i]++;
3267 }
3268 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3269
David Spinadel85aeb582017-03-30 19:43:53 +03003270 if (sta && !mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003271 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3272 return 0;
3273 }
3274
Johannes Bergba3943b2014-11-12 23:54:48 +01003275 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3276 if (ret)
3277 return ret;
3278
3279 /* delete WEP key twice to get rid of (now useless) offset */
3280 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3281 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3282 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3283
3284 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003285}
3286
Johannes Berg8ca151b2013-01-24 14:25:36 +01003287void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3288 struct ieee80211_vif *vif,
3289 struct ieee80211_key_conf *keyconf,
3290 struct ieee80211_sta *sta, u32 iv32,
3291 u16 *phase1key)
3292{
Beni Levc3eb5362013-02-06 17:22:18 +02003293 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01003294 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003295
Beni Levc3eb5362013-02-06 17:22:18 +02003296 rcu_read_lock();
3297
Johannes Berg5f7a1842015-12-11 09:36:10 +01003298 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3299 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003300 goto unlock;
David Spinadel85aeb582017-03-30 19:43:53 +03003301 iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003302 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003303
3304 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02003305 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01003306}
3307
Johannes Berg9cc40712013-02-15 22:47:48 +01003308void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3309 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003310{
Johannes Berg5b577a92013-11-14 18:20:04 +01003311 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003312 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003313 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003314 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03003315 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01003316 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003317 };
3318 int ret;
3319
Sara Sharon854c5702016-01-26 13:17:47 +02003320 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3321 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003322 if (ret)
3323 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3324}
3325
Johannes Berg9cc40712013-02-15 22:47:48 +01003326void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3327 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003328 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003329 u16 cnt, u16 tids, bool more_data,
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003330 bool single_sta_queue)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003331{
Johannes Berg5b577a92013-11-14 18:20:04 +01003332 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003333 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003334 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003335 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003336 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3337 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003338 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003339 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003340 int tid, ret;
3341 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003342
Johannes Berg3e56ead2013-02-15 22:23:18 +01003343 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3344 * Note that this field is reserved and unused by firmware not
3345 * supporting GO uAPSD, so it's safe to always do this.
3346 */
3347 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3348 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3349
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003350 /* If we're releasing frames from aggregation or dqa queues then check
3351 * if all the queues that we're releasing frames from, combined, have:
Johannes Berg3e56ead2013-02-15 22:23:18 +01003352 * - more frames than the service period, in which case more_data
3353 * needs to be set
3354 * - fewer than 'cnt' frames, in which case we need to adjust the
3355 * firmware command (but do that unconditionally)
3356 */
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003357 if (single_sta_queue) {
Johannes Berg3e56ead2013-02-15 22:23:18 +01003358 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003359 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003360
3361 spin_lock_bh(&mvmsta->lock);
3362 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3363 struct iwl_mvm_tid_data *tid_data;
3364 u16 n_queued;
3365
3366 tid_data = &mvmsta->tid_data[tid];
Johannes Berg3e56ead2013-02-15 22:23:18 +01003367
Liad Kaufmandd321622017-04-05 16:25:11 +03003368 n_queued = iwl_mvm_tid_queued(mvm, tid_data);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003369 if (n_queued > remaining) {
3370 more_data = true;
3371 remaining = 0;
3372 break;
3373 }
3374 remaining -= n_queued;
3375 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003376 sleep_tx_count = cnt - remaining;
3377 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3378 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003379 spin_unlock_bh(&mvmsta->lock);
3380
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003381 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003382 if (WARN_ON(cnt - remaining == 0)) {
3383 ieee80211_sta_eosp(sta);
3384 return;
3385 }
3386 }
3387
3388 /* Note: this is ignored by firmware not supporting GO uAPSD */
3389 if (more_data)
Sara Sharonced19f22017-02-06 19:09:32 +02003390 cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003391
3392 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3393 mvmsta->next_status_eosp = true;
Sara Sharonced19f22017-02-06 19:09:32 +02003394 cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003395 } else {
Sara Sharonced19f22017-02-06 19:09:32 +02003396 cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003397 }
3398
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003399 /* block the Tx queues until the FW updated the sleep Tx count */
3400 iwl_trans_block_txq_ptrs(mvm->trans, true);
3401
3402 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3403 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003404 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003405 if (ret)
3406 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3407}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003408
Johannes Berg04168412015-06-23 21:22:09 +02003409void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3410 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003411{
3412 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3413 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3414 struct ieee80211_sta *sta;
3415 u32 sta_id = le32_to_cpu(notif->sta_id);
3416
3417 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003418 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003419
3420 rcu_read_lock();
3421 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3422 if (!IS_ERR_OR_NULL(sta))
3423 ieee80211_sta_eosp(sta);
3424 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003425}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003426
3427void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3428 struct iwl_mvm_sta *mvmsta, bool disable)
3429{
3430 struct iwl_mvm_add_sta_cmd cmd = {
3431 .add_modify = STA_MODE_MODIFY,
3432 .sta_id = mvmsta->sta_id,
3433 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3434 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3435 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3436 };
3437 int ret;
3438
Sara Sharon854c5702016-01-26 13:17:47 +02003439 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3440 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003441 if (ret)
3442 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3443}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003444
3445void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3446 struct ieee80211_sta *sta,
3447 bool disable)
3448{
3449 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3450
3451 spin_lock_bh(&mvm_sta->lock);
3452
3453 if (mvm_sta->disable_tx == disable) {
3454 spin_unlock_bh(&mvm_sta->lock);
3455 return;
3456 }
3457
3458 mvm_sta->disable_tx = disable;
3459
Johannes Bergc8f54702017-06-19 23:50:31 +02003460 /* Tell mac80211 to start/stop queuing tx for this station */
3461 ieee80211_sta_block_awake(mvm->hw, sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003462
3463 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3464
3465 spin_unlock_bh(&mvm_sta->lock);
3466}
3467
Sara Sharonced19f22017-02-06 19:09:32 +02003468static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3469 struct iwl_mvm_vif *mvmvif,
3470 struct iwl_mvm_int_sta *sta,
3471 bool disable)
3472{
3473 u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3474 struct iwl_mvm_add_sta_cmd cmd = {
3475 .add_modify = STA_MODE_MODIFY,
3476 .sta_id = sta->sta_id,
3477 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3478 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3479 .mac_id_n_color = cpu_to_le32(id),
3480 };
3481 int ret;
3482
3483 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3484 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3485 if (ret)
3486 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3487}
3488
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003489void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3490 struct iwl_mvm_vif *mvmvif,
3491 bool disable)
3492{
3493 struct ieee80211_sta *sta;
3494 struct iwl_mvm_sta *mvm_sta;
3495 int i;
3496
3497 lockdep_assert_held(&mvm->mutex);
3498
3499 /* Block/unblock all the stations of the given mvmvif */
Sara Sharon0ae98812017-01-04 14:53:58 +02003500 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003501 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3502 lockdep_is_held(&mvm->mutex));
3503 if (IS_ERR_OR_NULL(sta))
3504 continue;
3505
3506 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3507 if (mvm_sta->mac_id_n_color !=
3508 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3509 continue;
3510
3511 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3512 }
Sara Sharonced19f22017-02-06 19:09:32 +02003513
3514 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3515 return;
3516
3517 /* Need to block/unblock also multicast station */
3518 if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3519 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3520 &mvmvif->mcast_sta, disable);
3521
3522 /*
3523 * Only unblock the broadcast station (FW blocks it for immediate
3524 * quiet, not the driver)
3525 */
3526 if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3527 iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3528 &mvmvif->bcast_sta, disable);
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003529}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003530
3531void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3532{
3533 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3534 struct iwl_mvm_sta *mvmsta;
3535
3536 rcu_read_lock();
3537
3538 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3539
3540 if (!WARN_ON(!mvmsta))
3541 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3542
3543 rcu_read_unlock();
3544}
Liad Kaufmandd321622017-04-05 16:25:11 +03003545
3546u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3547{
3548 u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3549
3550 /*
3551 * In A000 HW, the next_reclaimed index is only 8 bit, so we'll need
3552 * to align the wrap around of ssn so we compare relevant values.
3553 */
3554 if (mvm->trans->cfg->gen2)
3555 sn &= 0xff;
3556
3557 return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3558}