blob: fb031197266ee34c848a1c5818fe50a7c9f20ca0 [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{
80 return iwl_mvm_has_new_rx_api(mvm) ?
81 sizeof(struct iwl_mvm_add_sta_cmd) :
82 sizeof(struct iwl_mvm_add_sta_cmd_v7);
83}
84
Eliad Pellerb92e6612014-01-23 17:58:23 +020085static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
86 enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +010087{
88 int sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +020089 u32 reserved_ids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +010090
Eliad Pellerb92e6612014-01-23 17:58:23 +020091 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
Johannes Berg8ca151b2013-01-24 14:25:36 +010092 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
93
94 lockdep_assert_held(&mvm->mutex);
95
Eliad Pellerb92e6612014-01-23 17:58:23 +020096 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
97 if (iftype != NL80211_IFTYPE_STATION)
98 reserved_ids = BIT(0);
99
Johannes Berg8ca151b2013-01-24 14:25:36 +0100100 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
Sara Sharon0ae98812017-01-04 14:53:58 +0200101 for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200102 if (BIT(sta_id) & reserved_ids)
103 continue;
104
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
106 lockdep_is_held(&mvm->mutex)))
107 return sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +0200108 }
Sara Sharon0ae98812017-01-04 14:53:58 +0200109 return IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100110}
111
Johannes Berg7a453972013-02-12 13:10:44 +0100112/* send station add/update command to firmware */
113int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Liad Kaufman24afba72015-07-28 18:56:08 +0300114 bool update, unsigned int flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100115{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100116 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300117 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
118 .sta_id = mvm_sta->sta_id,
119 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
120 .add_modify = update ? 1 : 0,
121 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
122 STA_FLG_MIMO_EN_MSK),
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200123 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300124 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100125 int ret;
126 u32 status;
127 u32 agg_size = 0, mpdu_dens = 0;
128
Liad Kaufman24afba72015-07-28 18:56:08 +0300129 if (!update || (flags & STA_MODIFY_QUEUES)) {
Johannes Berg7a453972013-02-12 13:10:44 +0100130 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
131 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
Liad Kaufman24afba72015-07-28 18:56:08 +0300132
133 if (flags & STA_MODIFY_QUEUES)
134 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
Johannes Berg7a453972013-02-12 13:10:44 +0100135 }
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100136
137 switch (sta->bandwidth) {
138 case IEEE80211_STA_RX_BW_160:
139 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
140 /* fall through */
141 case IEEE80211_STA_RX_BW_80:
142 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
143 /* fall through */
144 case IEEE80211_STA_RX_BW_40:
145 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
146 /* fall through */
147 case IEEE80211_STA_RX_BW_20:
148 if (sta->ht_cap.ht_supported)
149 add_sta_cmd.station_flags |=
150 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
151 break;
152 }
153
154 switch (sta->rx_nss) {
155 case 1:
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
157 break;
158 case 2:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
160 break;
161 case 3 ... 8:
162 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
163 break;
164 }
165
166 switch (sta->smps_mode) {
167 case IEEE80211_SMPS_AUTOMATIC:
168 case IEEE80211_SMPS_NUM_MODES:
169 WARN_ON(1);
170 break;
171 case IEEE80211_SMPS_STATIC:
172 /* override NSS */
173 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
174 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
175 break;
176 case IEEE80211_SMPS_DYNAMIC:
177 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
178 break;
179 case IEEE80211_SMPS_OFF:
180 /* nothing */
181 break;
182 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100183
184 if (sta->ht_cap.ht_supported) {
185 add_sta_cmd.station_flags_msk |=
186 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
187 STA_FLG_AGG_MPDU_DENS_MSK);
188
189 mpdu_dens = sta->ht_cap.ampdu_density;
190 }
191
192 if (sta->vht_cap.vht_supported) {
193 agg_size = sta->vht_cap.cap &
194 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
195 agg_size >>=
196 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
197 } else if (sta->ht_cap.ht_supported) {
198 agg_size = sta->ht_cap.ampdu_factor;
199 }
200
201 add_sta_cmd.station_flags |=
202 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
203 add_sta_cmd.station_flags |=
204 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
Johannes Berg6ea29ce2016-12-01 16:25:30 +0100205 if (mvm_sta->associated)
206 add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100207
Johannes Berg65e25482016-04-13 14:24:22 +0200208 if (sta->wme) {
209 add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
210
211 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
212 add_sta_cmd.uapsd_trigger_acs |= BIT(AC_BK);
213 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
214 add_sta_cmd.uapsd_trigger_acs |= BIT(AC_BE);
215 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
216 add_sta_cmd.uapsd_trigger_acs |= BIT(AC_VI);
217 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
218 add_sta_cmd.uapsd_trigger_acs |= BIT(AC_VO);
219 }
220
Johannes Berg8ca151b2013-01-24 14:25:36 +0100221 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200222 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
223 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300224 &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100225 if (ret)
226 return ret;
227
Sara Sharon837c4da2016-01-07 16:50:45 +0200228 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100229 case ADD_STA_SUCCESS:
230 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
231 break;
232 default:
233 ret = -EIO;
234 IWL_ERR(mvm, "ADD_STA failed\n");
235 break;
236 }
237
238 return ret;
239}
240
Sara Sharon10b2b202016-03-20 16:23:41 +0200241static void iwl_mvm_rx_agg_session_expired(unsigned long data)
242{
243 struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
244 struct iwl_mvm_baid_data *ba_data;
245 struct ieee80211_sta *sta;
246 struct iwl_mvm_sta *mvm_sta;
247 unsigned long timeout;
248
249 rcu_read_lock();
250
251 ba_data = rcu_dereference(*rcu_ptr);
252
253 if (WARN_ON(!ba_data))
254 goto unlock;
255
256 if (!ba_data->timeout)
257 goto unlock;
258
259 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
260 if (time_is_after_jiffies(timeout)) {
261 mod_timer(&ba_data->session_timer, timeout);
262 goto unlock;
263 }
264
265 /* Timer expired */
266 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
267 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
268 ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
269 sta->addr, ba_data->tid);
270unlock:
271 rcu_read_unlock();
272}
273
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300274static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
275 struct ieee80211_sta *sta)
276{
277 unsigned long used_hw_queues;
278 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200279 unsigned int wdg_timeout =
280 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300281 u32 ac;
282
283 lockdep_assert_held(&mvm->mutex);
284
285 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
286
287 /* Find available queues, and allocate them to the ACs */
288 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
289 u8 queue = find_first_zero_bit(&used_hw_queues,
290 mvm->first_agg_queue);
291
292 if (queue >= mvm->first_agg_queue) {
293 IWL_ERR(mvm, "Failed to allocate STA queue\n");
294 return -EBUSY;
295 }
296
297 __set_bit(queue, &used_hw_queues);
298 mvmsta->hw_queue[ac] = queue;
299 }
300
301 /* Found a place for all queues - enable them */
302 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
303 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300304 mvmsta->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300305 iwl_mvm_ac_to_tx_fifo[ac], 0,
306 wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300307 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
308 }
309
310 return 0;
311}
312
313static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
314 struct ieee80211_sta *sta)
315{
316 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
317 unsigned long sta_msk;
318 int i;
319
320 lockdep_assert_held(&mvm->mutex);
321
322 /* disable the TDLS STA-specific queues */
323 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200324 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300325 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300326}
327
Liad Kaufman9794c642015-08-19 17:34:28 +0300328/* Disable aggregations for a bitmap of TIDs for a given station */
329static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
330 unsigned long disable_agg_tids,
331 bool remove_queue)
332{
333 struct iwl_mvm_add_sta_cmd cmd = {};
334 struct ieee80211_sta *sta;
335 struct iwl_mvm_sta *mvmsta;
336 u32 status;
337 u8 sta_id;
338 int ret;
339
340 spin_lock_bh(&mvm->queue_info_lock);
341 sta_id = mvm->queue_info[queue].ra_sta_id;
342 spin_unlock_bh(&mvm->queue_info_lock);
343
344 rcu_read_lock();
345
346 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
347
348 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
349 rcu_read_unlock();
350 return -EINVAL;
351 }
352
353 mvmsta = iwl_mvm_sta_from_mac80211(sta);
354
355 mvmsta->tid_disable_agg |= disable_agg_tids;
356
357 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
358 cmd.sta_id = mvmsta->sta_id;
359 cmd.add_modify = STA_MODE_MODIFY;
360 cmd.modify_mask = STA_MODIFY_QUEUES;
361 if (disable_agg_tids)
362 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
363 if (remove_queue)
364 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
365 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
366 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
367
368 rcu_read_unlock();
369
370 /* Notify FW of queue removal from the STA queues */
371 status = ADD_STA_SUCCESS;
372 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
373 iwl_mvm_add_sta_cmd_size(mvm),
374 &cmd, &status);
375
376 return ret;
377}
378
Liad Kaufman42db09c2016-05-02 14:01:14 +0300379static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
380{
381 struct ieee80211_sta *sta;
382 struct iwl_mvm_sta *mvmsta;
383 unsigned long tid_bitmap;
384 unsigned long agg_tids = 0;
385 s8 sta_id;
386 int tid;
387
388 lockdep_assert_held(&mvm->mutex);
389
390 spin_lock_bh(&mvm->queue_info_lock);
391 sta_id = mvm->queue_info[queue].ra_sta_id;
392 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
393 spin_unlock_bh(&mvm->queue_info_lock);
394
395 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
396 lockdep_is_held(&mvm->mutex));
397
398 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
399 return -EINVAL;
400
401 mvmsta = iwl_mvm_sta_from_mac80211(sta);
402
403 spin_lock_bh(&mvmsta->lock);
404 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
405 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
406 agg_tids |= BIT(tid);
407 }
408 spin_unlock_bh(&mvmsta->lock);
409
410 return agg_tids;
411}
412
Liad Kaufman9794c642015-08-19 17:34:28 +0300413/*
414 * Remove a queue from a station's resources.
415 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
416 * doesn't disable the queue
417 */
418static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
419{
420 struct ieee80211_sta *sta;
421 struct iwl_mvm_sta *mvmsta;
422 unsigned long tid_bitmap;
423 unsigned long disable_agg_tids = 0;
424 u8 sta_id;
425 int tid;
426
427 lockdep_assert_held(&mvm->mutex);
428
429 spin_lock_bh(&mvm->queue_info_lock);
430 sta_id = mvm->queue_info[queue].ra_sta_id;
431 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
432 spin_unlock_bh(&mvm->queue_info_lock);
433
434 rcu_read_lock();
435
436 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
437
438 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
439 rcu_read_unlock();
440 return 0;
441 }
442
443 mvmsta = iwl_mvm_sta_from_mac80211(sta);
444
445 spin_lock_bh(&mvmsta->lock);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300446 /* Unmap MAC queues and TIDs from this queue */
Liad Kaufman9794c642015-08-19 17:34:28 +0300447 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300448 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
449 disable_agg_tids |= BIT(tid);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300450 mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
Liad Kaufman9794c642015-08-19 17:34:28 +0300451 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300452
Liad Kaufman42db09c2016-05-02 14:01:14 +0300453 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
Liad Kaufman9794c642015-08-19 17:34:28 +0300454 spin_unlock_bh(&mvmsta->lock);
455
456 rcu_read_unlock();
457
Liad Kaufman9794c642015-08-19 17:34:28 +0300458 return disable_agg_tids;
459}
460
Sara Sharon01796ff2016-11-16 17:04:36 +0200461static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
462 bool same_sta)
463{
464 struct iwl_mvm_sta *mvmsta;
465 u8 txq_curr_ac, sta_id, tid;
466 unsigned long disable_agg_tids = 0;
467 int ret;
468
469 lockdep_assert_held(&mvm->mutex);
470
471 spin_lock_bh(&mvm->queue_info_lock);
472 txq_curr_ac = mvm->queue_info[queue].mac80211_ac;
473 sta_id = mvm->queue_info[queue].ra_sta_id;
474 tid = mvm->queue_info[queue].txq_tid;
475 spin_unlock_bh(&mvm->queue_info_lock);
476
477 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
478
479 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
480 /* Disable the queue */
481 if (disable_agg_tids)
482 iwl_mvm_invalidate_sta_queue(mvm, queue,
483 disable_agg_tids, false);
484
485 ret = iwl_mvm_disable_txq(mvm, queue,
486 mvmsta->vif->hw_queue[txq_curr_ac],
487 tid, 0);
488 if (ret) {
489 /* Re-mark the inactive queue as inactive */
490 spin_lock_bh(&mvm->queue_info_lock);
491 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
492 spin_unlock_bh(&mvm->queue_info_lock);
493 IWL_ERR(mvm,
494 "Failed to free inactive queue %d (ret=%d)\n",
495 queue, ret);
496
497 return ret;
498 }
499
500 /* If TXQ is allocated to another STA, update removal in FW */
501 if (!same_sta)
502 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
503
504 return 0;
505}
506
Liad Kaufman42db09c2016-05-02 14:01:14 +0300507static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
508 unsigned long tfd_queue_mask, u8 ac)
509{
510 int queue = 0;
511 u8 ac_to_queue[IEEE80211_NUM_ACS];
512 int i;
513
514 lockdep_assert_held(&mvm->queue_info_lock);
515
516 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
517
518 /* See what ACs the existing queues for this STA have */
519 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
520 /* Only DATA queues can be shared */
521 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
522 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
523 continue;
524
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200525 /* Don't try and take queues being reconfigured */
526 if (mvm->queue_info[queue].status ==
527 IWL_MVM_QUEUE_RECONFIGURING)
528 continue;
529
Liad Kaufman42db09c2016-05-02 14:01:14 +0300530 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
531 }
532
533 /*
534 * The queue to share is chosen only from DATA queues as follows (in
535 * descending priority):
536 * 1. An AC_BE queue
537 * 2. Same AC queue
538 * 3. Highest AC queue that is lower than new AC
539 * 4. Any existing AC (there always is at least 1 DATA queue)
540 */
541
542 /* Priority 1: An AC_BE queue */
543 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
544 queue = ac_to_queue[IEEE80211_AC_BE];
545 /* Priority 2: Same AC queue */
546 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
547 queue = ac_to_queue[ac];
548 /* Priority 3a: If new AC is VO and VI exists - use VI */
549 else if (ac == IEEE80211_AC_VO &&
550 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
551 queue = ac_to_queue[IEEE80211_AC_VI];
552 /* Priority 3b: No BE so only AC less than the new one is BK */
553 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
554 queue = ac_to_queue[IEEE80211_AC_BK];
555 /* Priority 4a: No BE nor BK - use VI if exists */
556 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
557 queue = ac_to_queue[IEEE80211_AC_VI];
558 /* Priority 4b: No BE, BK nor VI - use VO if exists */
559 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
560 queue = ac_to_queue[IEEE80211_AC_VO];
561
562 /* Make sure queue found (or not) is legal */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200563 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
564 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
565 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
Liad Kaufman42db09c2016-05-02 14:01:14 +0300566 IWL_ERR(mvm, "No DATA queues available to share\n");
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200567 return -ENOSPC;
568 }
569
570 /* Make sure the queue isn't in the middle of being reconfigured */
571 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
572 IWL_ERR(mvm,
573 "TXQ %d is in the middle of re-config - try again\n",
574 queue);
575 return -EBUSY;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300576 }
577
578 return queue;
579}
580
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200581/*
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200582 * If a given queue has a higher AC than the TID stream that is being compared
583 * to, the queue needs to be redirected to the lower AC. This function does that
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200584 * in such a case, otherwise - if no redirection required - it does nothing,
585 * unless the %force param is true.
586 */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200587int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
588 int ac, int ssn, unsigned int wdg_timeout,
589 bool force)
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200590{
591 struct iwl_scd_txq_cfg_cmd cmd = {
592 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200593 .action = SCD_CFG_DISABLE_QUEUE,
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200594 };
595 bool shared_queue;
596 unsigned long mq;
597 int ret;
598
599 /*
600 * If the AC is lower than current one - FIFO needs to be redirected to
601 * the lowest one of the streams in the queue. Check if this is needed
602 * here.
603 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
604 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
605 * we need to check if the numerical value of X is LARGER than of Y.
606 */
607 spin_lock_bh(&mvm->queue_info_lock);
608 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
609 spin_unlock_bh(&mvm->queue_info_lock);
610
611 IWL_DEBUG_TX_QUEUES(mvm,
612 "No redirection needed on TXQ #%d\n",
613 queue);
614 return 0;
615 }
616
617 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
618 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200619 cmd.tid = mvm->queue_info[queue].txq_tid;
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200620 mq = mvm->queue_info[queue].hw_queue_to_mac80211;
621 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
622 spin_unlock_bh(&mvm->queue_info_lock);
623
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200624 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200625 queue, iwl_mvm_ac_to_tx_fifo[ac]);
626
627 /* Stop MAC queues and wait for this queue to empty */
628 iwl_mvm_stop_mac_queues(mvm, mq);
629 ret = iwl_trans_wait_tx_queue_empty(mvm->trans, BIT(queue));
630 if (ret) {
631 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
632 queue);
633 ret = -EIO;
634 goto out;
635 }
636
637 /* Before redirecting the queue we need to de-activate it */
638 iwl_trans_txq_disable(mvm->trans, queue, false);
639 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
640 if (ret)
641 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
642 ret);
643
644 /* Make sure the SCD wrptr is correctly set before reconfiguring */
Sara Sharonca3b9c62016-06-30 16:14:02 +0300645 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200646
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200647 /* Update the TID "owner" of the queue */
648 spin_lock_bh(&mvm->queue_info_lock);
649 mvm->queue_info[queue].txq_tid = tid;
650 spin_unlock_bh(&mvm->queue_info_lock);
651
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200652 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
653
654 /* Redirect to lower AC */
655 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
656 cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
657 ssn);
658
659 /* Update AC marking of the queue */
660 spin_lock_bh(&mvm->queue_info_lock);
661 mvm->queue_info[queue].mac80211_ac = ac;
662 spin_unlock_bh(&mvm->queue_info_lock);
663
664 /*
665 * Mark queue as shared in transport if shared
666 * Note this has to be done after queue enablement because enablement
667 * can also set this value, and there is no indication there to shared
668 * queues
669 */
670 if (shared_queue)
671 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
672
673out:
674 /* Continue using the MAC queues */
675 iwl_mvm_start_mac_queues(mvm, mq);
676
677 return ret;
678}
679
Liad Kaufman24afba72015-07-28 18:56:08 +0300680static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
681 struct ieee80211_sta *sta, u8 ac, int tid,
682 struct ieee80211_hdr *hdr)
683{
684 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
685 struct iwl_trans_txq_scd_cfg cfg = {
686 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
687 .sta_id = mvmsta->sta_id,
688 .tid = tid,
689 .frame_limit = IWL_FRAME_LIMIT,
690 };
691 unsigned int wdg_timeout =
692 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
693 u8 mac_queue = mvmsta->vif->hw_queue[ac];
694 int queue = -1;
Sara Sharon01796ff2016-11-16 17:04:36 +0200695 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman9794c642015-08-19 17:34:28 +0300696 unsigned long disable_agg_tids = 0;
697 enum iwl_mvm_agg_state queue_state;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300698 bool shared_queue = false;
Liad Kaufman24afba72015-07-28 18:56:08 +0300699 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300700 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300701 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300702
703 lockdep_assert_held(&mvm->mutex);
704
Liad Kaufman42db09c2016-05-02 14:01:14 +0300705 spin_lock_bh(&mvmsta->lock);
706 tfd_queue_mask = mvmsta->tfd_queue_msk;
707 spin_unlock_bh(&mvmsta->lock);
708
Liad Kaufmand2515a92016-03-23 16:31:08 +0200709 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300710
711 /*
712 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
713 * exists
714 */
715 if (!ieee80211_is_data_qos(hdr->frame_control) ||
716 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300717 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
718 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300719 IWL_MVM_DQA_MAX_MGMT_QUEUE);
720 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
721 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
722 queue);
723
724 /* If no such queue is found, we'll use a DATA queue instead */
725 }
726
Liad Kaufman9794c642015-08-19 17:34:28 +0300727 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
728 (mvm->queue_info[mvmsta->reserved_queue].status ==
729 IWL_MVM_QUEUE_RESERVED ||
730 mvm->queue_info[mvmsta->reserved_queue].status ==
731 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300732 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300733 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300734 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
735 }
736
737 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300738 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
739 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300740 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300741
742 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300743 * Check if this queue is already allocated but inactive.
744 * In such a case, we'll need to first free this queue before enabling
745 * it again, so we'll mark it as reserved to make sure no new traffic
746 * arrives on it
747 */
748 if (queue > 0 &&
749 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
750 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
751 using_inactive_queue = true;
Sara Sharon01796ff2016-11-16 17:04:36 +0200752 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman9794c642015-08-19 17:34:28 +0300753 IWL_DEBUG_TX_QUEUES(mvm,
754 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
755 queue, mvmsta->sta_id, tid);
756 }
757
Liad Kaufman42db09c2016-05-02 14:01:14 +0300758 /* No free queue - we'll have to share */
759 if (queue <= 0) {
760 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
761 if (queue > 0) {
762 shared_queue = true;
763 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
764 }
765 }
766
Liad Kaufman9794c642015-08-19 17:34:28 +0300767 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300768 * Mark TXQ as ready, even though it hasn't been fully configured yet,
769 * to make sure no one else takes it.
770 * This will allow avoiding re-acquiring the lock at the end of the
771 * configuration. On error we'll mark it back as free.
772 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300773 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300774 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300775
Liad Kaufmand2515a92016-03-23 16:31:08 +0200776 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300777
Liad Kaufman42db09c2016-05-02 14:01:14 +0300778 /* This shouldn't happen - out of queues */
779 if (WARN_ON(queue <= 0)) {
780 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
781 tid, cfg.sta_id);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200782 return queue;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300783 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300784
785 /*
786 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
787 * but for configuring the SCD to send A-MPDUs we need to mark the queue
788 * as aggregatable.
789 * Mark all DATA queues as allowing to be aggregated at some point
790 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300791 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
792 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300793
Liad Kaufman9794c642015-08-19 17:34:28 +0300794 /*
795 * If this queue was previously inactive (idle) - we need to free it
796 * first
797 */
798 if (using_inactive_queue) {
Sara Sharon01796ff2016-11-16 17:04:36 +0200799 ret = iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
800 if (ret)
Liad Kaufman9794c642015-08-19 17:34:28 +0300801 return ret;
Liad Kaufman9794c642015-08-19 17:34:28 +0300802 }
803
Liad Kaufman42db09c2016-05-02 14:01:14 +0300804 IWL_DEBUG_TX_QUEUES(mvm,
805 "Allocating %squeue #%d to sta %d on tid %d\n",
806 shared_queue ? "shared " : "", queue,
807 mvmsta->sta_id, tid);
808
809 if (shared_queue) {
810 /* Disable any open aggs on this queue */
811 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
812
813 if (disable_agg_tids) {
814 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
815 queue);
816 iwl_mvm_invalidate_sta_queue(mvm, queue,
817 disable_agg_tids, false);
818 }
Liad Kaufman42db09c2016-05-02 14:01:14 +0300819 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300820
821 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
822 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
823 wdg_timeout);
824
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200825 /*
826 * Mark queue as shared in transport if shared
827 * Note this has to be done after queue enablement because enablement
828 * can also set this value, and there is no indication there to shared
829 * queues
830 */
831 if (shared_queue)
832 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
833
Liad Kaufman24afba72015-07-28 18:56:08 +0300834 spin_lock_bh(&mvmsta->lock);
835 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300836 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300837 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300838 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300839
840 if (mvmsta->reserved_queue == queue)
841 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
842 spin_unlock_bh(&mvmsta->lock);
843
Liad Kaufman42db09c2016-05-02 14:01:14 +0300844 if (!shared_queue) {
845 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
846 if (ret)
847 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300848
Liad Kaufman42db09c2016-05-02 14:01:14 +0300849 /* If we need to re-enable aggregations... */
850 if (queue_state == IWL_AGG_ON) {
851 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
852 if (ret)
853 goto out_err;
854 }
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200855 } else {
856 /* Redirect queue, if needed */
857 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
858 wdg_timeout, false);
859 if (ret)
860 goto out_err;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300861 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300862
Liad Kaufman42db09c2016-05-02 14:01:14 +0300863 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300864
865out_err:
866 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
867
868 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300869}
870
Liad Kaufman19aefa42016-03-08 14:29:51 +0200871static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
872{
873 struct iwl_scd_txq_cfg_cmd cmd = {
874 .scd_queue = queue,
875 .action = SCD_CFG_UPDATE_QUEUE_TID,
876 };
Liad Kaufman19aefa42016-03-08 14:29:51 +0200877 int tid;
878 unsigned long tid_bitmap;
879 int ret;
880
881 lockdep_assert_held(&mvm->mutex);
882
883 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200884 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
885 spin_unlock_bh(&mvm->queue_info_lock);
886
887 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
888 return;
889
890 /* Find any TID for queue */
891 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
892 cmd.tid = tid;
893 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
894
895 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
Liad Kaufman341ca402016-09-18 14:51:59 +0300896 if (ret) {
Liad Kaufman19aefa42016-03-08 14:29:51 +0200897 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
898 queue, ret);
Liad Kaufman341ca402016-09-18 14:51:59 +0300899 return;
900 }
901
902 spin_lock_bh(&mvm->queue_info_lock);
903 mvm->queue_info[queue].txq_tid = tid;
904 spin_unlock_bh(&mvm->queue_info_lock);
905 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
906 queue, tid);
Liad Kaufman19aefa42016-03-08 14:29:51 +0200907}
908
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200909static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
910{
911 struct ieee80211_sta *sta;
912 struct iwl_mvm_sta *mvmsta;
913 s8 sta_id;
914 int tid = -1;
915 unsigned long tid_bitmap;
916 unsigned int wdg_timeout;
917 int ssn;
918 int ret = true;
919
920 lockdep_assert_held(&mvm->mutex);
921
922 spin_lock_bh(&mvm->queue_info_lock);
923 sta_id = mvm->queue_info[queue].ra_sta_id;
924 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
925 spin_unlock_bh(&mvm->queue_info_lock);
926
927 /* Find TID for queue, and make sure it is the only one on the queue */
928 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
929 if (tid_bitmap != BIT(tid)) {
930 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
931 queue, tid_bitmap);
932 return;
933 }
934
935 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
936 tid);
937
938 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
939 lockdep_is_held(&mvm->mutex));
940
941 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
942 return;
943
944 mvmsta = iwl_mvm_sta_from_mac80211(sta);
945 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
946
947 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
948
949 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
950 tid_to_mac80211_ac[tid], ssn,
951 wdg_timeout, true);
952 if (ret) {
953 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
954 return;
955 }
956
957 /* If aggs should be turned back on - do it */
958 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
Emmanuel Grumbach9cd70e82016-09-20 13:40:33 +0300959 struct iwl_mvm_add_sta_cmd cmd = {0};
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200960
961 mvmsta->tid_disable_agg &= ~BIT(tid);
962
963 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
964 cmd.sta_id = mvmsta->sta_id;
965 cmd.add_modify = STA_MODE_MODIFY;
966 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
967 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
968 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
969
970 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
971 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
972 if (!ret) {
973 IWL_DEBUG_TX_QUEUES(mvm,
974 "TXQ #%d is now aggregated again\n",
975 queue);
976
977 /* Mark queue intenally as aggregating again */
978 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
979 }
980 }
981
982 spin_lock_bh(&mvm->queue_info_lock);
983 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
984 spin_unlock_bh(&mvm->queue_info_lock);
985}
986
Liad Kaufman24afba72015-07-28 18:56:08 +0300987static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
988{
989 if (tid == IWL_MAX_TID_COUNT)
990 return IEEE80211_AC_VO; /* MGMT */
991
992 return tid_to_mac80211_ac[tid];
993}
994
995static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
996 struct ieee80211_sta *sta, int tid)
997{
998 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
999 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1000 struct sk_buff *skb;
1001 struct ieee80211_hdr *hdr;
1002 struct sk_buff_head deferred_tx;
1003 u8 mac_queue;
1004 bool no_queue = false; /* Marks if there is a problem with the queue */
1005 u8 ac;
1006
1007 lockdep_assert_held(&mvm->mutex);
1008
1009 skb = skb_peek(&tid_data->deferred_tx_frames);
1010 if (!skb)
1011 return;
1012 hdr = (void *)skb->data;
1013
1014 ac = iwl_mvm_tid_to_ac_queue(tid);
1015 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
1016
1017 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
1018 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
1019 IWL_ERR(mvm,
1020 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
1021 mvmsta->sta_id, tid);
1022
1023 /*
1024 * Mark queue as problematic so later the deferred traffic is
1025 * freed, as we can do nothing with it
1026 */
1027 no_queue = true;
1028 }
1029
1030 __skb_queue_head_init(&deferred_tx);
1031
Liad Kaufmand2515a92016-03-23 16:31:08 +02001032 /* Disable bottom-halves when entering TX path */
1033 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +03001034 spin_lock(&mvmsta->lock);
1035 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
Liad Kaufmanad5de732016-09-27 16:01:10 +03001036 mvmsta->deferred_traffic_tid_map &= ~BIT(tid);
Liad Kaufman24afba72015-07-28 18:56:08 +03001037 spin_unlock(&mvmsta->lock);
1038
Liad Kaufman24afba72015-07-28 18:56:08 +03001039 while ((skb = __skb_dequeue(&deferred_tx)))
1040 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1041 ieee80211_free_txskb(mvm->hw, skb);
1042 local_bh_enable();
1043
1044 /* Wake queue */
1045 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1046}
1047
1048void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1049{
1050 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1051 add_stream_wk);
1052 struct ieee80211_sta *sta;
1053 struct iwl_mvm_sta *mvmsta;
1054 unsigned long deferred_tid_traffic;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001055 int queue, sta_id, tid;
Liad Kaufman24afba72015-07-28 18:56:08 +03001056
Liad Kaufman9794c642015-08-19 17:34:28 +03001057 /* Check inactivity of queues */
1058 iwl_mvm_inactivity_check(mvm);
1059
Liad Kaufman24afba72015-07-28 18:56:08 +03001060 mutex_lock(&mvm->mutex);
1061
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001062 /* Reconfigure queues requiring reconfiguation */
1063 for (queue = 0; queue < IWL_MAX_HW_QUEUES; queue++) {
1064 bool reconfig;
Liad Kaufman19aefa42016-03-08 14:29:51 +02001065 bool change_owner;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001066
1067 spin_lock_bh(&mvm->queue_info_lock);
1068 reconfig = (mvm->queue_info[queue].status ==
1069 IWL_MVM_QUEUE_RECONFIGURING);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001070
1071 /*
1072 * We need to take into account a situation in which a TXQ was
1073 * allocated to TID x, and then turned shared by adding TIDs y
1074 * and z. If TID x becomes inactive and is removed from the TXQ,
1075 * ownership must be given to one of the remaining TIDs.
1076 * This is mainly because if TID x continues - a new queue can't
1077 * be allocated for it as long as it is an owner of another TXQ.
1078 */
1079 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1080 BIT(mvm->queue_info[queue].txq_tid)) &&
1081 (mvm->queue_info[queue].status ==
1082 IWL_MVM_QUEUE_SHARED);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001083 spin_unlock_bh(&mvm->queue_info_lock);
1084
1085 if (reconfig)
1086 iwl_mvm_unshare_queue(mvm, queue);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001087 else if (change_owner)
1088 iwl_mvm_change_queue_owner(mvm, queue);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001089 }
1090
Liad Kaufman24afba72015-07-28 18:56:08 +03001091 /* Go over all stations with deferred traffic */
1092 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1093 IWL_MVM_STATION_COUNT) {
1094 clear_bit(sta_id, mvm->sta_deferred_frames);
1095 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1096 lockdep_is_held(&mvm->mutex));
1097 if (IS_ERR_OR_NULL(sta))
1098 continue;
1099
1100 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1101 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1102
1103 for_each_set_bit(tid, &deferred_tid_traffic,
1104 IWL_MAX_TID_COUNT + 1)
1105 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1106 }
1107
1108 mutex_unlock(&mvm->mutex);
1109}
1110
1111static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001112 struct ieee80211_sta *sta,
1113 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +03001114{
1115 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1116 int queue;
Sara Sharon01796ff2016-11-16 17:04:36 +02001117 bool using_inactive_queue = false, same_sta = false;
Liad Kaufman24afba72015-07-28 18:56:08 +03001118
Liad Kaufman9794c642015-08-19 17:34:28 +03001119 /*
1120 * Check for inactive queues, so we don't reach a situation where we
1121 * can't add a STA due to a shortage in queues that doesn't really exist
1122 */
1123 iwl_mvm_inactivity_check(mvm);
1124
Liad Kaufman24afba72015-07-28 18:56:08 +03001125 spin_lock_bh(&mvm->queue_info_lock);
1126
1127 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +03001128 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1129 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +03001130 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1131 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +03001132 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1133 else
Liad Kaufman9794c642015-08-19 17:34:28 +03001134 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1135 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001136 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +03001137 if (queue < 0) {
1138 spin_unlock_bh(&mvm->queue_info_lock);
1139 IWL_ERR(mvm, "No available queues for new station\n");
1140 return -ENOSPC;
Sara Sharon01796ff2016-11-16 17:04:36 +02001141 } else if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
1142 /*
1143 * If this queue is already allocated but inactive we'll need to
1144 * first free this queue before enabling it again, we'll mark
1145 * it as reserved to make sure no new traffic arrives on it
1146 */
1147 using_inactive_queue = true;
1148 same_sta = mvm->queue_info[queue].ra_sta_id == mvmsta->sta_id;
Liad Kaufman24afba72015-07-28 18:56:08 +03001149 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03001150 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +03001151
1152 spin_unlock_bh(&mvm->queue_info_lock);
1153
1154 mvmsta->reserved_queue = queue;
1155
Sara Sharon01796ff2016-11-16 17:04:36 +02001156 if (using_inactive_queue)
1157 iwl_mvm_free_inactive_queue(mvm, queue, same_sta);
1158
Liad Kaufman24afba72015-07-28 18:56:08 +03001159 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1160 queue, mvmsta->sta_id);
1161
1162 return 0;
1163}
1164
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001165/*
1166 * In DQA mode, after a HW restart the queues should be allocated as before, in
1167 * order to avoid race conditions when there are shared queues. This function
1168 * does the re-mapping and queue allocation.
1169 *
1170 * Note that re-enabling aggregations isn't done in this function.
1171 */
1172static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1173 struct iwl_mvm_sta *mvm_sta)
1174{
1175 unsigned int wdg_timeout =
1176 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1177 int i;
1178 struct iwl_trans_txq_scd_cfg cfg = {
1179 .sta_id = mvm_sta->sta_id,
1180 .frame_limit = IWL_FRAME_LIMIT,
1181 };
1182
Johannes Berg03c902b2016-12-02 12:03:36 +01001183 /* Make sure reserved queue is still marked as such (if allocated) */
1184 if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1185 mvm->queue_info[mvm_sta->reserved_queue].status =
1186 IWL_MVM_QUEUE_RESERVED;
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001187
1188 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1189 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1190 int txq_id = tid_data->txq_id;
1191 int ac;
1192 u8 mac_queue;
1193
1194 if (txq_id == IEEE80211_INVAL_HW_QUEUE)
1195 continue;
1196
1197 skb_queue_head_init(&tid_data->deferred_tx_frames);
1198
1199 ac = tid_to_mac80211_ac[i];
1200 mac_queue = mvm_sta->vif->hw_queue[ac];
1201
1202 cfg.tid = i;
1203 cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
1204 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1205 txq_id == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1206
1207 IWL_DEBUG_TX_QUEUES(mvm,
1208 "Re-mapping sta %d tid %d to queue %d\n",
1209 mvm_sta->sta_id, i, txq_id);
1210
1211 iwl_mvm_enable_txq(mvm, txq_id, mac_queue,
1212 IEEE80211_SEQ_TO_SN(tid_data->seq_number),
1213 &cfg, wdg_timeout);
1214
1215 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1216 }
1217
1218 atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
1219}
1220
Johannes Berg8ca151b2013-01-24 14:25:36 +01001221int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1222 struct ieee80211_vif *vif,
1223 struct ieee80211_sta *sta)
1224{
1225 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001226 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001227 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001228 int i, ret, sta_id;
1229
1230 lockdep_assert_held(&mvm->mutex);
1231
1232 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001233 sta_id = iwl_mvm_find_free_sta_id(mvm,
1234 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001235 else
1236 sta_id = mvm_sta->sta_id;
1237
Sara Sharon0ae98812017-01-04 14:53:58 +02001238 if (sta_id == IWL_MVM_INVALID_STA)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239 return -ENOSPC;
1240
1241 spin_lock_init(&mvm_sta->lock);
1242
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001243 /* In DQA mode, if this is a HW restart, re-alloc existing queues */
1244 if (iwl_mvm_is_dqa_supported(mvm) &&
1245 test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1246 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1247 goto update_fw;
1248 }
1249
Johannes Berg8ca151b2013-01-24 14:25:36 +01001250 mvm_sta->sta_id = sta_id;
1251 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1252 mvmvif->color);
1253 mvm_sta->vif = vif;
1254 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001255 mvm_sta->tx_protection = 0;
1256 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001257
1258 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001259 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +03001260 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001261 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001262
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001263 /*
1264 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1265 * and then they'll be allocated dynamically
1266 */
1267 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001268 ret = iwl_mvm_tdls_sta_init(mvm, sta);
1269 if (ret)
1270 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +03001271 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001272 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1273 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1274 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1275 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001276
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001277 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001278 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001279 u16 seq = mvm_sta->tid_data[i].seq_number;
1280 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1281 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001282
1283 if (!iwl_mvm_is_dqa_supported(mvm))
1284 continue;
1285
1286 /*
1287 * Mark all queues for this STA as unallocated and defer TX
1288 * frames until the queue is allocated
1289 */
1290 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1291 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001292 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001293 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001294 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001295
Sara Sharona571f5f2015-12-07 12:50:58 +02001296 if (iwl_mvm_has_new_rx_api(mvm) &&
1297 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1298 dup_data = kcalloc(mvm->trans->num_rx_queues,
1299 sizeof(*dup_data),
1300 GFP_KERNEL);
1301 if (!dup_data)
1302 return -ENOMEM;
1303 mvm_sta->dup_data = dup_data;
1304 }
1305
Liad Kaufman24afba72015-07-28 18:56:08 +03001306 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001307 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1308 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001309 if (ret)
1310 goto err;
1311 }
1312
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001313update_fw:
Liad Kaufman24afba72015-07-28 18:56:08 +03001314 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001315 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001316 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001317
Johannes Berg9e848012014-08-04 14:33:42 +02001318 if (vif->type == NL80211_IFTYPE_STATION) {
1319 if (!sta->tdls) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001320 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001321 mvmvif->ap_sta_id = sta_id;
1322 } else {
Sara Sharon0ae98812017-01-04 14:53:58 +02001323 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
Johannes Berg9e848012014-08-04 14:33:42 +02001324 }
1325 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001326
1327 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1328
1329 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001330
1331err:
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001332 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1333 iwl_mvm_tdls_sta_deinit(mvm, sta);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001334 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001335}
1336
1337int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1338 bool drain)
1339{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001340 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001341 int ret;
1342 u32 status;
1343
1344 lockdep_assert_held(&mvm->mutex);
1345
1346 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1347 cmd.sta_id = mvmsta->sta_id;
1348 cmd.add_modify = STA_MODE_MODIFY;
1349 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1350 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1351
1352 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001353 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1354 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001355 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001356 if (ret)
1357 return ret;
1358
Sara Sharon837c4da2016-01-07 16:50:45 +02001359 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001360 case ADD_STA_SUCCESS:
1361 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1362 mvmsta->sta_id);
1363 break;
1364 default:
1365 ret = -EIO;
1366 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1367 mvmsta->sta_id);
1368 break;
1369 }
1370
1371 return ret;
1372}
1373
1374/*
1375 * Remove a station from the FW table. Before sending the command to remove
1376 * the station validate that the station is indeed known to the driver (sanity
1377 * only).
1378 */
1379static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1380{
1381 struct ieee80211_sta *sta;
1382 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1383 .sta_id = sta_id,
1384 };
1385 int ret;
1386
1387 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1388 lockdep_is_held(&mvm->mutex));
1389
1390 /* Note: internal stations are marked as error values */
1391 if (!sta) {
1392 IWL_ERR(mvm, "Invalid station id\n");
1393 return -EINVAL;
1394 }
1395
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001396 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001397 sizeof(rm_sta_cmd), &rm_sta_cmd);
1398 if (ret) {
1399 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1400 return ret;
1401 }
1402
1403 return 0;
1404}
1405
1406void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1407{
1408 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1409 u8 sta_id;
1410
1411 /*
1412 * The mutex is needed because of the SYNC cmd, but not only: if the
1413 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1414 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1415 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1416 * that later.
1417 */
1418 mutex_lock(&mvm->mutex);
1419
1420 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1421 int ret;
1422 struct ieee80211_sta *sta =
1423 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1424 lockdep_is_held(&mvm->mutex));
1425
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001426 /*
1427 * This station is in use or RCU-removed; the latter happens in
1428 * managed mode, where mac80211 removes the station before we
1429 * can remove it from firmware (we can only do that after the
1430 * MAC is marked unassociated), and possibly while the deauth
1431 * frame to disconnect from the AP is still queued. Then, the
1432 * station pointer is -ENOENT when the last skb is reclaimed.
1433 */
1434 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001435 continue;
1436
1437 if (PTR_ERR(sta) == -EINVAL) {
1438 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1439 sta_id);
1440 continue;
1441 }
1442
1443 if (!sta) {
1444 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1445 sta_id);
1446 continue;
1447 }
1448
1449 WARN_ON(PTR_ERR(sta) != -EBUSY);
1450 /* This station was removed and we waited until it got drained,
1451 * we can now proceed and remove it.
1452 */
1453 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1454 if (ret) {
1455 IWL_ERR(mvm,
1456 "Couldn't remove sta %d after it was drained\n",
1457 sta_id);
1458 continue;
1459 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301460 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001461 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001462
1463 if (mvm->tfd_drained[sta_id]) {
1464 unsigned long i, msk = mvm->tfd_drained[sta_id];
1465
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001466 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001467 iwl_mvm_disable_txq(mvm, i, i,
1468 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001469
1470 mvm->tfd_drained[sta_id] = 0;
1471 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1472 sta_id, msk);
1473 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001474 }
1475
1476 mutex_unlock(&mvm->mutex);
1477}
1478
Liad Kaufman24afba72015-07-28 18:56:08 +03001479static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1480 struct ieee80211_vif *vif,
1481 struct iwl_mvm_sta *mvm_sta)
1482{
1483 int ac;
1484 int i;
1485
1486 lockdep_assert_held(&mvm->mutex);
1487
1488 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1489 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
1490 continue;
1491
1492 ac = iwl_mvm_tid_to_ac_queue(i);
1493 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1494 vif->hw_queue[ac], i, 0);
1495 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1496 }
1497}
1498
Johannes Berg8ca151b2013-01-24 14:25:36 +01001499int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1500 struct ieee80211_vif *vif,
1501 struct ieee80211_sta *sta)
1502{
1503 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001504 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001505 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001506 int ret;
1507
1508 lockdep_assert_held(&mvm->mutex);
1509
Sara Sharona571f5f2015-12-07 12:50:58 +02001510 if (iwl_mvm_has_new_rx_api(mvm))
1511 kfree(mvm_sta->dup_data);
1512
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001513 if ((vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001514 mvmvif->ap_sta_id == sta_id) ||
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001515 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001516 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1517 if (ret)
1518 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001519 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +03001520 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001521 if (ret)
1522 return ret;
1523 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1524 mvm_sta->tfd_queue_msk);
1525 if (ret)
1526 return ret;
1527 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001528
Liad Kaufman24afba72015-07-28 18:56:08 +03001529 /* If DQA is supported - the queues can be disabled now */
Sara Sharon94c3e612016-12-07 15:04:37 +02001530 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman56214742016-09-22 15:14:08 +03001531 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
Sara Sharon94c3e612016-12-07 15:04:37 +02001532 /*
1533 * If pending_frames is set at this point - it must be
1534 * driver internal logic error, since queues are empty
1535 * and removed successuly.
1536 * warn on it but set it to 0 anyway to avoid station
1537 * not being removed later in the function
1538 */
1539 WARN_ON(atomic_xchg(&mvm->pending_frames[sta_id], 0));
1540 }
Liad Kaufman56214742016-09-22 15:14:08 +03001541
1542 /* If there is a TXQ still marked as reserved - free it */
1543 if (iwl_mvm_is_dqa_supported(mvm) &&
1544 mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001545 u8 reserved_txq = mvm_sta->reserved_queue;
1546 enum iwl_mvm_queue_status *status;
1547
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001548 /*
1549 * If no traffic has gone through the reserved TXQ - it
1550 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1551 * should be manually marked as free again
1552 */
1553 spin_lock_bh(&mvm->queue_info_lock);
1554 status = &mvm->queue_info[reserved_txq].status;
1555 if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1556 (*status != IWL_MVM_QUEUE_FREE),
1557 "sta_id %d reserved txq %d status %d",
Sara Sharon94c3e612016-12-07 15:04:37 +02001558 sta_id, reserved_txq, *status)) {
Liad Kaufmana0315dea2016-07-07 13:25:59 +03001559 spin_unlock_bh(&mvm->queue_info_lock);
1560 return -EINVAL;
1561 }
1562
1563 *status = IWL_MVM_QUEUE_FREE;
1564 spin_unlock_bh(&mvm->queue_info_lock);
1565 }
1566
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001567 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon94c3e612016-12-07 15:04:37 +02001568 mvmvif->ap_sta_id == sta_id) {
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001569 /* if associated - we can't remove the AP STA now */
1570 if (vif->bss_conf.assoc)
1571 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001572
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001573 /* unassoc - go ahead - remove the AP STA now */
Sara Sharon0ae98812017-01-04 14:53:58 +02001574 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
Eliad Peller37577fe2013-12-05 17:19:39 +02001575
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001576 /* clear d0i3_ap_sta_id if no longer relevant */
Sara Sharon94c3e612016-12-07 15:04:37 +02001577 if (mvm->d0i3_ap_sta_id == sta_id)
Sara Sharon0ae98812017-01-04 14:53:58 +02001578 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001579 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001580 }
1581
1582 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001583 * This shouldn't happen - the TDLS channel switch should be canceled
1584 * before the STA is removed.
1585 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001586 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
Sara Sharon0ae98812017-01-04 14:53:58 +02001587 mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001588 cancel_delayed_work(&mvm->tdls_cs.dwork);
1589 }
1590
1591 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001592 * Make sure that the tx response code sees the station as -EBUSY and
1593 * calls the drain worker.
1594 */
1595 spin_lock_bh(&mvm_sta->lock);
Sara Sharon94c3e612016-12-07 15:04:37 +02001596
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001597 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001598 * There are frames pending on the AC queues for this station.
1599 * We need to wait until all the frames are drained...
1600 */
Sara Sharon94c3e612016-12-07 15:04:37 +02001601 if (atomic_read(&mvm->pending_frames[sta_id])) {
1602 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id],
Johannes Berg8ca151b2013-01-24 14:25:36 +01001603 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001604 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001605
1606 /* disable TDLS sta queues on drain complete */
1607 if (sta->tdls) {
Sara Sharon94c3e612016-12-07 15:04:37 +02001608 mvm->tfd_drained[sta_id] = mvm_sta->tfd_queue_msk;
1609 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n", sta_id);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001610 }
1611
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001612 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001613 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001614 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001615
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001616 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001617 iwl_mvm_tdls_sta_deinit(mvm, sta);
1618
Johannes Berg8ca151b2013-01-24 14:25:36 +01001619 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301620 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001621 }
1622
1623 return ret;
1624}
1625
1626int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1627 struct ieee80211_vif *vif,
1628 u8 sta_id)
1629{
1630 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1631
1632 lockdep_assert_held(&mvm->mutex);
1633
Monam Agarwalc531c772014-03-24 00:05:56 +05301634 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001635 return ret;
1636}
1637
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001638int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1639 struct iwl_mvm_int_sta *sta,
1640 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001641{
1642 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001643 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Sara Sharon0ae98812017-01-04 14:53:58 +02001644 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001645 return -ENOSPC;
1646 }
1647
1648 sta->tfd_queue_msk = qmask;
1649
1650 /* put a non-NULL value so iterating over the stations won't stop */
1651 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1652 return 0;
1653}
1654
Sara Sharon26d6c162017-01-03 12:00:19 +02001655void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001656{
Monam Agarwalc531c772014-03-24 00:05:56 +05301657 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001658 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
Sara Sharon0ae98812017-01-04 14:53:58 +02001659 sta->sta_id = IWL_MVM_INVALID_STA;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001660}
1661
1662static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1663 struct iwl_mvm_int_sta *sta,
1664 const u8 *addr,
1665 u16 mac_id, u16 color)
1666{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001667 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001668 int ret;
1669 u32 status;
1670
1671 lockdep_assert_held(&mvm->mutex);
1672
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001673 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001674 cmd.sta_id = sta->sta_id;
1675 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1676 color));
1677
1678 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001679 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001680
1681 if (addr)
1682 memcpy(cmd.addr, addr, ETH_ALEN);
1683
Sara Sharon854c5702016-01-26 13:17:47 +02001684 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1685 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001686 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001687 if (ret)
1688 return ret;
1689
Sara Sharon837c4da2016-01-07 16:50:45 +02001690 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001691 case ADD_STA_SUCCESS:
1692 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1693 return 0;
1694 default:
1695 ret = -EIO;
1696 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1697 status);
1698 break;
1699 }
1700 return ret;
1701}
1702
Sara Sharonc5a719e2016-11-15 10:20:48 +02001703static void iwl_mvm_enable_aux_queue(struct iwl_mvm *mvm)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001704{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001705 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1706 mvm->cfg->base_params->wd_timeout :
1707 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001708
Liad Kaufman28d07932015-09-01 16:36:25 +03001709 if (iwl_mvm_is_dqa_supported(mvm)) {
1710 struct iwl_trans_txq_scd_cfg cfg = {
1711 .fifo = IWL_MVM_TX_FIFO_MCAST,
1712 .sta_id = mvm->aux_sta.sta_id,
1713 .tid = IWL_MAX_TID_COUNT,
1714 .aggregate = false,
1715 .frame_limit = IWL_FRAME_LIMIT,
1716 };
1717
1718 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1719 wdg_timeout);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001720 } else {
1721 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1722 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Liad Kaufman28d07932015-09-01 16:36:25 +03001723 }
Sara Sharonc5a719e2016-11-15 10:20:48 +02001724}
1725
1726int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1727{
1728 int ret;
1729
1730 lockdep_assert_held(&mvm->mutex);
1731
1732 /* Allocate aux station and assign to it the aux queue */
1733 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
1734 NL80211_IFTYPE_UNSPECIFIED);
1735 if (ret)
1736 return ret;
1737
1738 /* Map Aux queue to fifo - needs to happen before adding Aux station */
1739 if (!iwl_mvm_has_new_tx_api(mvm))
1740 iwl_mvm_enable_aux_queue(mvm);
Liad Kaufman28d07932015-09-01 16:36:25 +03001741
Johannes Berg8ca151b2013-01-24 14:25:36 +01001742 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1743 MAC_INDEX_AUX, 0);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001744 if (ret) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001745 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001746 return ret;
1747 }
1748
1749 /*
1750 * For a000 firmware and on we cannot add queue to a station unknown
1751 * to firmware so enable queue here - after the station was added
1752 */
1753 if (iwl_mvm_has_new_tx_api(mvm))
1754 iwl_mvm_enable_aux_queue(mvm);
1755
1756 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001757}
1758
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001759int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1760{
1761 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1762
1763 lockdep_assert_held(&mvm->mutex);
1764 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1765 mvmvif->id, 0);
1766}
1767
1768int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1769{
1770 int ret;
1771
1772 lockdep_assert_held(&mvm->mutex);
1773
1774 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1775 if (ret)
1776 IWL_WARN(mvm, "Failed sending remove station\n");
1777
1778 return ret;
1779}
1780
1781void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1782{
1783 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1784}
1785
Johannes Berg712b24a2014-08-04 14:14:14 +02001786void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1787{
1788 lockdep_assert_held(&mvm->mutex);
1789
1790 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1791}
1792
Johannes Berg8ca151b2013-01-24 14:25:36 +01001793/*
1794 * Send the add station command for the vif's broadcast station.
1795 * Assumes that the station was already allocated.
1796 *
1797 * @mvm: the mvm component
1798 * @vif: the interface to which the broadcast station is added
1799 * @bsta: the broadcast station to add.
1800 */
Johannes Berg013290a2014-08-04 13:38:48 +02001801int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001802{
1803 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001804 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001805 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001806 const u8 *baddr = _baddr;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001807 int queue = 0;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001808 int ret;
Sara Sharonc5a719e2016-11-15 10:20:48 +02001809 unsigned int wdg_timeout =
1810 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1811 struct iwl_trans_txq_scd_cfg cfg = {
1812 .fifo = IWL_MVM_TX_FIFO_VO,
1813 .sta_id = mvmvif->bcast_sta.sta_id,
1814 .tid = IWL_MAX_TID_COUNT,
1815 .aggregate = false,
1816 .frame_limit = IWL_FRAME_LIMIT,
1817 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001818
1819 lockdep_assert_held(&mvm->mutex);
1820
Liad Kaufmande24f632015-08-04 15:19:18 +03001821 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufman4d339982017-03-21 17:13:16 +02001822 if (vif->type == NL80211_IFTYPE_AP ||
1823 vif->type == NL80211_IFTYPE_ADHOC)
Sara Sharon49f71712017-01-09 12:07:16 +02001824 queue = mvm->probe_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001825 else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
Sara Sharon49f71712017-01-09 12:07:16 +02001826 queue = mvm->p2p_dev_queue;
Liad Kaufmandf88c082016-11-24 15:31:00 +02001827 else if (WARN(1, "Missing required TXQ for adding bcast STA\n"))
Liad Kaufmande24f632015-08-04 15:19:18 +03001828 return -EINVAL;
1829
Liad Kaufmandf88c082016-11-24 15:31:00 +02001830 bsta->tfd_queue_msk |= BIT(queue);
Sara Sharonc5a719e2016-11-15 10:20:48 +02001831
1832 if (!iwl_mvm_has_new_tx_api(mvm))
1833 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0,
1834 &cfg, wdg_timeout);
Liad Kaufmande24f632015-08-04 15:19:18 +03001835 }
1836
Johannes Berg5023d962013-07-31 14:07:43 +02001837 if (vif->type == NL80211_IFTYPE_ADHOC)
1838 baddr = vif->bss_conf.bssid;
1839
Sara Sharon0ae98812017-01-04 14:53:58 +02001840 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
Johannes Berg8ca151b2013-01-24 14:25:36 +01001841 return -ENOSPC;
1842
Liad Kaufmandf88c082016-11-24 15:31:00 +02001843 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1844 mvmvif->id, mvmvif->color);
1845 if (ret)
1846 return ret;
1847
1848 /*
Sara Sharonc5a719e2016-11-15 10:20:48 +02001849 * For a000 firmware and on we cannot add queue to a station unknown
1850 * to firmware so enable queue here - after the station was added
Liad Kaufmandf88c082016-11-24 15:31:00 +02001851 */
Sara Sharonc5a719e2016-11-15 10:20:48 +02001852 if (iwl_mvm_has_new_tx_api(mvm))
1853 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1854 wdg_timeout);
Liad Kaufmandf88c082016-11-24 15:31:00 +02001855
1856 return 0;
1857}
1858
1859static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
1860 struct ieee80211_vif *vif)
1861{
1862 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1863
1864 lockdep_assert_held(&mvm->mutex);
1865
Liad Kaufman4d339982017-03-21 17:13:16 +02001866 if (vif->type == NL80211_IFTYPE_AP ||
1867 vif->type == NL80211_IFTYPE_ADHOC)
Liad Kaufmandf88c082016-11-24 15:31:00 +02001868 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
1869 IWL_MAX_TID_COUNT, 0);
1870
Sara Sharon49f71712017-01-09 12:07:16 +02001871 if (mvmvif->bcast_sta.tfd_queue_msk & BIT(mvm->probe_queue)) {
1872 iwl_mvm_disable_txq(mvm, mvm->probe_queue,
Liad Kaufmandf88c082016-11-24 15:31:00 +02001873 vif->hw_queue[0], IWL_MAX_TID_COUNT,
1874 0);
Sara Sharon49f71712017-01-09 12:07:16 +02001875 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(mvm->probe_queue);
Liad Kaufmandf88c082016-11-24 15:31:00 +02001876 }
1877
Sara Sharon49f71712017-01-09 12:07:16 +02001878 if (mvmvif->bcast_sta.tfd_queue_msk & BIT(mvm->p2p_dev_queue)) {
1879 iwl_mvm_disable_txq(mvm, mvm->p2p_dev_queue,
Liad Kaufmandf88c082016-11-24 15:31:00 +02001880 vif->hw_queue[0], IWL_MAX_TID_COUNT,
1881 0);
Sara Sharon49f71712017-01-09 12:07:16 +02001882 mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(mvm->p2p_dev_queue);
Liad Kaufmandf88c082016-11-24 15:31:00 +02001883 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001884}
1885
1886/* Send the FW a request to remove the station from it's internal data
1887 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001888int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001889{
Johannes Berg013290a2014-08-04 13:38:48 +02001890 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001891 int ret;
1892
1893 lockdep_assert_held(&mvm->mutex);
1894
Liad Kaufmandf88c082016-11-24 15:31:00 +02001895 if (iwl_mvm_is_dqa_supported(mvm))
1896 iwl_mvm_free_bcast_sta_queues(mvm, vif);
1897
Johannes Berg013290a2014-08-04 13:38:48 +02001898 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001899 if (ret)
1900 IWL_WARN(mvm, "Failed sending remove station\n");
1901 return ret;
1902}
1903
Johannes Berg013290a2014-08-04 13:38:48 +02001904int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1905{
1906 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03001907 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02001908
1909 lockdep_assert_held(&mvm->mutex);
1910
Liad Kaufmandf88c082016-11-24 15:31:00 +02001911 if (!iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmande24f632015-08-04 15:19:18 +03001912 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001913
Liad Kaufmande24f632015-08-04 15:19:18 +03001914 /*
1915 * The firmware defines the TFD queue mask to only be relevant
1916 * for *unicast* queues, so the multicast (CAB) queue shouldn't
Liad Kaufmandf88c082016-11-24 15:31:00 +02001917 * be included. This only happens in NL80211_IFTYPE_AP vif type,
1918 * so the next line will only have an effect there.
Liad Kaufmande24f632015-08-04 15:19:18 +03001919 */
Johannes Berg013290a2014-08-04 13:38:48 +02001920 qmask &= ~BIT(vif->cab_queue);
Liad Kaufmande24f632015-08-04 15:19:18 +03001921 }
1922
Johannes Berg013290a2014-08-04 13:38:48 +02001923 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1924 ieee80211_vif_type_p2p(vif));
1925}
1926
Johannes Berg8ca151b2013-01-24 14:25:36 +01001927/* Allocate a new station entry for the broadcast station to the given vif,
1928 * and send it to the FW.
1929 * Note that each P2P mac should have its own broadcast station.
1930 *
1931 * @mvm: the mvm component
1932 * @vif: the interface to which the broadcast station is added
1933 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02001934int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001935{
1936 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001937 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001938 int ret;
1939
1940 lockdep_assert_held(&mvm->mutex);
1941
Johannes Berg013290a2014-08-04 13:38:48 +02001942 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001943 if (ret)
1944 return ret;
1945
Johannes Berg013290a2014-08-04 13:38:48 +02001946 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001947
1948 if (ret)
1949 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001950
Johannes Berg8ca151b2013-01-24 14:25:36 +01001951 return ret;
1952}
1953
Johannes Berg013290a2014-08-04 13:38:48 +02001954void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1955{
1956 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1957
1958 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1959}
1960
Johannes Berg8ca151b2013-01-24 14:25:36 +01001961/*
1962 * Send the FW a request to remove the station from it's internal data
1963 * structures, and in addition remove it from the local data structure.
1964 */
Johannes Berg013290a2014-08-04 13:38:48 +02001965int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001966{
1967 int ret;
1968
1969 lockdep_assert_held(&mvm->mutex);
1970
Johannes Berg013290a2014-08-04 13:38:48 +02001971 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001972
Johannes Berg013290a2014-08-04 13:38:48 +02001973 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1974
Johannes Berg8ca151b2013-01-24 14:25:36 +01001975 return ret;
1976}
1977
Sara Sharon26d6c162017-01-03 12:00:19 +02001978/*
1979 * Allocate a new station entry for the multicast station to the given vif,
1980 * and send it to the FW.
1981 * Note that each AP/GO mac should have its own multicast station.
1982 *
1983 * @mvm: the mvm component
1984 * @vif: the interface to which the multicast station is added
1985 */
1986int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1987{
1988 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1989 struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
1990 static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
1991 const u8 *maddr = _maddr;
1992 struct iwl_trans_txq_scd_cfg cfg = {
1993 .fifo = IWL_MVM_TX_FIFO_MCAST,
1994 .sta_id = msta->sta_id,
1995 .tid = IWL_MAX_TID_COUNT,
1996 .aggregate = false,
1997 .frame_limit = IWL_FRAME_LIMIT,
1998 };
1999 unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2000 int ret;
2001
2002 lockdep_assert_held(&mvm->mutex);
2003
2004 if (!iwl_mvm_is_dqa_supported(mvm))
2005 return 0;
2006
2007 if (WARN_ON(vif->type != NL80211_IFTYPE_AP))
2008 return -ENOTSUPP;
2009
2010 ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2011 mvmvif->id, mvmvif->color);
2012 if (ret) {
2013 iwl_mvm_dealloc_int_sta(mvm, msta);
2014 return ret;
2015 }
2016
2017 /*
2018 * Enable cab queue after the ADD_STA command is sent.
2019 * This is needed for a000 firmware which won't accept SCD_QUEUE_CFG
2020 * command with unknown station id.
2021 */
2022 iwl_mvm_enable_txq(mvm, vif->cab_queue, vif->cab_queue, 0, &cfg,
2023 timeout);
2024
2025 return 0;
2026}
2027
2028/*
2029 * Send the FW a request to remove the station from it's internal data
2030 * structures, and in addition remove it from the local data structure.
2031 */
2032int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2033{
2034 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2035 int ret;
2036
2037 lockdep_assert_held(&mvm->mutex);
2038
2039 if (!iwl_mvm_is_dqa_supported(mvm))
2040 return 0;
2041
2042 iwl_mvm_disable_txq(mvm, vif->cab_queue, vif->cab_queue,
2043 IWL_MAX_TID_COUNT, 0);
2044
2045 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2046 if (ret)
2047 IWL_WARN(mvm, "Failed sending remove station\n");
2048
2049 return ret;
2050}
2051
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002052#define IWL_MAX_RX_BA_SESSIONS 16
2053
Sara Sharonb915c102016-03-23 16:32:02 +02002054static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02002055{
Sara Sharonb915c102016-03-23 16:32:02 +02002056 struct iwl_mvm_delba_notif notif = {
2057 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2058 .metadata.sync = 1,
2059 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02002060 };
Sara Sharonb915c102016-03-23 16:32:02 +02002061 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2062};
Sara Sharon10b2b202016-03-20 16:23:41 +02002063
Sara Sharonb915c102016-03-23 16:32:02 +02002064static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2065 struct iwl_mvm_baid_data *data)
2066{
2067 int i;
2068
2069 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2070
2071 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2072 int j;
2073 struct iwl_mvm_reorder_buffer *reorder_buf =
2074 &data->reorder_buf[i];
2075
Sara Sharon06904052016-02-28 20:28:17 +02002076 spin_lock_bh(&reorder_buf->lock);
2077 if (likely(!reorder_buf->num_stored)) {
2078 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02002079 continue;
Sara Sharon06904052016-02-28 20:28:17 +02002080 }
Sara Sharonb915c102016-03-23 16:32:02 +02002081
2082 /*
2083 * This shouldn't happen in regular DELBA since the internal
2084 * delBA notification should trigger a release of all frames in
2085 * the reorder buffer.
2086 */
2087 WARN_ON(1);
2088
2089 for (j = 0; j < reorder_buf->buf_size; j++)
2090 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02002091 /*
2092 * Prevent timer re-arm. This prevents a very far fetched case
2093 * where we timed out on the notification. There may be prior
2094 * RX frames pending in the RX queue before the notification
2095 * that might get processed between now and the actual deletion
2096 * and we would re-arm the timer although we are deleting the
2097 * reorder buffer.
2098 */
2099 reorder_buf->removed = true;
2100 spin_unlock_bh(&reorder_buf->lock);
2101 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02002102 }
2103}
2104
2105static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2106 u32 sta_id,
2107 struct iwl_mvm_baid_data *data,
2108 u16 ssn, u8 buf_size)
2109{
2110 int i;
2111
2112 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2113 struct iwl_mvm_reorder_buffer *reorder_buf =
2114 &data->reorder_buf[i];
2115 int j;
2116
2117 reorder_buf->num_stored = 0;
2118 reorder_buf->head_sn = ssn;
2119 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02002120 /* rx reorder timer */
2121 reorder_buf->reorder_timer.function =
2122 iwl_mvm_reorder_timer_expired;
2123 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
2124 init_timer(&reorder_buf->reorder_timer);
2125 spin_lock_init(&reorder_buf->lock);
2126 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02002127 reorder_buf->queue = i;
2128 reorder_buf->sta_id = sta_id;
2129 for (j = 0; j < reorder_buf->buf_size; j++)
2130 __skb_queue_head_init(&reorder_buf->entries[j]);
2131 }
Sara Sharon10b2b202016-03-20 16:23:41 +02002132}
2133
Johannes Berg8ca151b2013-01-24 14:25:36 +01002134int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02002135 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002136{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002137 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002138 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02002139 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002140 int ret;
2141 u32 status;
2142
2143 lockdep_assert_held(&mvm->mutex);
2144
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002145 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2146 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2147 return -ENOSPC;
2148 }
2149
Sara Sharon10b2b202016-03-20 16:23:41 +02002150 if (iwl_mvm_has_new_rx_api(mvm) && start) {
2151 /*
2152 * Allocate here so if allocation fails we can bail out early
2153 * before starting the BA session in the firmware
2154 */
Sara Sharonb915c102016-03-23 16:32:02 +02002155 baid_data = kzalloc(sizeof(*baid_data) +
2156 mvm->trans->num_rx_queues *
2157 sizeof(baid_data->reorder_buf[0]),
2158 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02002159 if (!baid_data)
2160 return -ENOMEM;
2161 }
2162
Johannes Berg8ca151b2013-01-24 14:25:36 +01002163 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2164 cmd.sta_id = mvm_sta->sta_id;
2165 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002166 if (start) {
2167 cmd.add_immediate_ba_tid = (u8) tid;
2168 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02002169 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03002170 } else {
2171 cmd.remove_immediate_ba_tid = (u8) tid;
2172 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002173 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2174 STA_MODIFY_REMOVE_BA_TID;
2175
2176 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002177 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2178 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002179 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002180 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02002181 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002182
Sara Sharon837c4da2016-01-07 16:50:45 +02002183 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002184 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03002185 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2186 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01002187 break;
2188 case ADD_STA_IMMEDIATE_BA_FAILURE:
2189 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2190 ret = -ENOSPC;
2191 break;
2192 default:
2193 ret = -EIO;
2194 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2195 start ? "start" : "stopp", status);
2196 break;
2197 }
2198
Sara Sharon10b2b202016-03-20 16:23:41 +02002199 if (ret)
2200 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002201
Sara Sharon10b2b202016-03-20 16:23:41 +02002202 if (start) {
2203 u8 baid;
2204
2205 mvm->rx_ba_sessions++;
2206
2207 if (!iwl_mvm_has_new_rx_api(mvm))
2208 return 0;
2209
2210 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2211 ret = -EINVAL;
2212 goto out_free;
2213 }
2214 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2215 IWL_ADD_STA_BAID_SHIFT);
2216 baid_data->baid = baid;
2217 baid_data->timeout = timeout;
2218 baid_data->last_rx = jiffies;
Wei Yongjun72c240f2016-07-12 11:40:57 +00002219 setup_timer(&baid_data->session_timer,
2220 iwl_mvm_rx_agg_session_expired,
2221 (unsigned long)&mvm->baid_map[baid]);
Sara Sharon10b2b202016-03-20 16:23:41 +02002222 baid_data->mvm = mvm;
2223 baid_data->tid = tid;
2224 baid_data->sta_id = mvm_sta->sta_id;
2225
2226 mvm_sta->tid_to_baid[tid] = baid;
2227 if (timeout)
2228 mod_timer(&baid_data->session_timer,
2229 TU_TO_EXP_TIME(timeout * 2));
2230
Sara Sharonb915c102016-03-23 16:32:02 +02002231 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2232 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002233 /*
2234 * protect the BA data with RCU to cover a case where our
2235 * internal RX sync mechanism will timeout (not that it's
2236 * supposed to happen) and we will free the session data while
2237 * RX is being processed in parallel
2238 */
Sara Sharon35263a02016-06-21 12:12:10 +03002239 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2240 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002241 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2242 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002243 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002244 u8 baid = mvm_sta->tid_to_baid[tid];
2245
Sara Sharon60dec522016-06-21 14:14:08 +03002246 if (mvm->rx_ba_sessions > 0)
2247 /* check that restart flow didn't zero the counter */
2248 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002249 if (!iwl_mvm_has_new_rx_api(mvm))
2250 return 0;
2251
2252 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2253 return -EINVAL;
2254
2255 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2256 if (WARN_ON(!baid_data))
2257 return -EINVAL;
2258
2259 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002260 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002261 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002262 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2263 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002264 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002265 }
2266 return 0;
2267
2268out_free:
2269 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002270 return ret;
2271}
2272
Liad Kaufman9794c642015-08-19 17:34:28 +03002273int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2274 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002275{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002276 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002277 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002278 int ret;
2279 u32 status;
2280
2281 lockdep_assert_held(&mvm->mutex);
2282
2283 if (start) {
2284 mvm_sta->tfd_queue_msk |= BIT(queue);
2285 mvm_sta->tid_disable_agg &= ~BIT(tid);
2286 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002287 /* In DQA-mode the queue isn't removed on agg termination */
2288 if (!iwl_mvm_is_dqa_supported(mvm))
2289 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002290 mvm_sta->tid_disable_agg |= BIT(tid);
2291 }
2292
2293 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2294 cmd.sta_id = mvm_sta->sta_id;
2295 cmd.add_modify = STA_MODE_MODIFY;
2296 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
2297 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2298 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2299
2300 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002301 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2302 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002303 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002304 if (ret)
2305 return ret;
2306
Sara Sharon837c4da2016-01-07 16:50:45 +02002307 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002308 case ADD_STA_SUCCESS:
2309 break;
2310 default:
2311 ret = -EIO;
2312 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2313 start ? "start" : "stopp", status);
2314 break;
2315 }
2316
2317 return ret;
2318}
2319
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002320const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002321 IEEE80211_AC_BE,
2322 IEEE80211_AC_BK,
2323 IEEE80211_AC_BK,
2324 IEEE80211_AC_BE,
2325 IEEE80211_AC_VI,
2326 IEEE80211_AC_VI,
2327 IEEE80211_AC_VO,
2328 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002329 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002330};
2331
Johannes Berg3e56ead2013-02-15 22:23:18 +01002332static const u8 tid_to_ucode_ac[] = {
2333 AC_BE,
2334 AC_BK,
2335 AC_BK,
2336 AC_BE,
2337 AC_VI,
2338 AC_VI,
2339 AC_VO,
2340 AC_VO,
2341};
2342
Johannes Berg8ca151b2013-01-24 14:25:36 +01002343int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2344 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2345{
Johannes Berg5b577a92013-11-14 18:20:04 +01002346 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002347 struct iwl_mvm_tid_data *tid_data;
2348 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002349 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002350
2351 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2352 return -EINVAL;
2353
2354 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2355 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2356 mvmsta->tid_data[tid].state);
2357 return -ENXIO;
2358 }
2359
2360 lockdep_assert_held(&mvm->mutex);
2361
Arik Nemtsovb2492502014-03-13 12:21:50 +02002362 spin_lock_bh(&mvmsta->lock);
2363
2364 /* possible race condition - we entered D0i3 while starting agg */
2365 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2366 spin_unlock_bh(&mvmsta->lock);
2367 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2368 return -EIO;
2369 }
2370
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002371 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002372
Liad Kaufmancf961e12015-08-13 19:16:08 +03002373 /*
2374 * Note the possible cases:
2375 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2376 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2377 * one and mark it as reserved
2378 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2379 * non-DQA mode, since the TXQ hasn't yet been allocated
2380 */
2381 txq_id = mvmsta->tid_data[tid].txq_id;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002382 if (iwl_mvm_is_dqa_supported(mvm) &&
2383 unlikely(mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_SHARED)) {
2384 ret = -ENXIO;
2385 IWL_DEBUG_TX_QUEUES(mvm,
2386 "Can't start tid %d agg on shared queue!\n",
2387 tid);
2388 goto release_locks;
2389 } else if (!iwl_mvm_is_dqa_supported(mvm) ||
Liad Kaufmancf961e12015-08-13 19:16:08 +03002390 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002391 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2392 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03002393 mvm->last_agg_queue);
2394 if (txq_id < 0) {
2395 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002396 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2397 goto release_locks;
2398 }
Sara Sharon01796ff2016-11-16 17:04:36 +02002399 /*
2400 * TXQ shouldn't be in inactive mode for non-DQA, so getting
2401 * an inactive queue from iwl_mvm_find_free_queue() is
2402 * certainly a bug
2403 */
2404 WARN_ON(mvm->queue_info[txq_id].status ==
2405 IWL_MVM_QUEUE_INACTIVE);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002406
2407 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2408 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002409 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002410
2411 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002412
Liad Kaufmancf961e12015-08-13 19:16:08 +03002413 IWL_DEBUG_TX_QUEUES(mvm,
2414 "AGG for tid %d will be on queue #%d\n",
2415 tid, txq_id);
2416
Johannes Berg8ca151b2013-01-24 14:25:36 +01002417 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002418 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002419 tid_data->txq_id = txq_id;
2420 *ssn = tid_data->ssn;
2421
2422 IWL_DEBUG_TX_QUEUES(mvm,
2423 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2424 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2425 tid_data->next_reclaimed);
2426
2427 if (tid_data->ssn == tid_data->next_reclaimed) {
2428 tid_data->state = IWL_AGG_STARTING;
2429 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2430 } else {
2431 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2432 }
2433
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002434 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002435 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002436
2437release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002438 spin_unlock(&mvm->queue_info_lock);
2439out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002440 spin_unlock_bh(&mvmsta->lock);
2441
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002442 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002443}
2444
2445int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002446 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2447 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002448{
Johannes Berg5b577a92013-11-14 18:20:04 +01002449 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002450 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002451 unsigned int wdg_timeout =
2452 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002453 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002454 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002455 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002456 u16 ssn;
2457
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002458 struct iwl_trans_txq_scd_cfg cfg = {
2459 .sta_id = mvmsta->sta_id,
2460 .tid = tid,
2461 .frame_limit = buf_size,
2462 .aggregate = true,
2463 };
2464
Eyal Shapiraefed6642014-09-14 15:58:53 +03002465 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2466 != IWL_MAX_TID_COUNT);
2467
Johannes Berg8ca151b2013-01-24 14:25:36 +01002468 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2469
2470 spin_lock_bh(&mvmsta->lock);
2471 ssn = tid_data->ssn;
2472 queue = tid_data->txq_id;
2473 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002474 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002475 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002476 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002477 spin_unlock_bh(&mvmsta->lock);
2478
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002479 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002480
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002481 spin_lock_bh(&mvm->queue_info_lock);
2482 queue_status = mvm->queue_info[queue].status;
2483 spin_unlock_bh(&mvm->queue_info_lock);
2484
Liad Kaufmancf961e12015-08-13 19:16:08 +03002485 /* In DQA mode, the existing queue might need to be reconfigured */
2486 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002487 /* Maybe there is no need to even alloc a queue... */
2488 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2489 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002490
2491 /*
2492 * Only reconfig the SCD for the queue if the window size has
2493 * changed from current (become smaller)
2494 */
2495 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2496 /*
2497 * If reconfiguring an existing queue, it first must be
2498 * drained
2499 */
2500 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
2501 BIT(queue));
2502 if (ret) {
2503 IWL_ERR(mvm,
2504 "Error draining queue before reconfig\n");
2505 return ret;
2506 }
2507
2508 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2509 mvmsta->sta_id, tid,
2510 buf_size, ssn);
2511 if (ret) {
2512 IWL_ERR(mvm,
2513 "Error reconfiguring TXQ #%d\n", queue);
2514 return ret;
2515 }
2516 }
2517 }
2518
2519 if (alloc_queue)
2520 iwl_mvm_enable_txq(mvm, queue,
2521 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2522 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002523
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002524 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2525 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2526 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2527 if (ret)
2528 return -EIO;
2529 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002530
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002531 /* No need to mark as reserved */
2532 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002533 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002534 spin_unlock_bh(&mvm->queue_info_lock);
2535
Johannes Berg8ca151b2013-01-24 14:25:36 +01002536 /*
2537 * Even though in theory the peer could have different
2538 * aggregation reorder buffer sizes for different sessions,
2539 * our ucode doesn't allow for that and has a global limit
2540 * for each station. Therefore, use the minimum of all the
2541 * aggregation sessions and our default value.
2542 */
2543 mvmsta->max_agg_bufsize =
2544 min(mvmsta->max_agg_bufsize, buf_size);
2545 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2546
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002547 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2548 sta->addr, tid);
2549
Eyal Shapira9e680942013-11-09 00:16:16 +02002550 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002551}
2552
2553int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2554 struct ieee80211_sta *sta, u16 tid)
2555{
Johannes Berg5b577a92013-11-14 18:20:04 +01002556 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002557 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2558 u16 txq_id;
2559 int err;
2560
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002561 /*
2562 * If mac80211 is cleaning its state, then say that we finished since
2563 * our state has been cleared anyway.
2564 */
2565 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2566 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2567 return 0;
2568 }
2569
Johannes Berg8ca151b2013-01-24 14:25:36 +01002570 spin_lock_bh(&mvmsta->lock);
2571
2572 txq_id = tid_data->txq_id;
2573
2574 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2575 mvmsta->sta_id, tid, txq_id, tid_data->state);
2576
Eyal Shapiraefed6642014-09-14 15:58:53 +03002577 mvmsta->agg_tids &= ~BIT(tid);
2578
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002579 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002580 /*
2581 * The TXQ is marked as reserved only if no traffic came through yet
2582 * This means no traffic has been sent on this TID (agg'd or not), so
2583 * we no longer have use for the queue. Since it hasn't even been
2584 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2585 * free.
2586 */
2587 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2588 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002589
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002590 spin_unlock_bh(&mvm->queue_info_lock);
2591
Johannes Berg8ca151b2013-01-24 14:25:36 +01002592 switch (tid_data->state) {
2593 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002594 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002595
2596 IWL_DEBUG_TX_QUEUES(mvm,
2597 "ssn = %d, next_recl = %d\n",
2598 tid_data->ssn, tid_data->next_reclaimed);
2599
2600 /* There are still packets for this RA / TID in the HW */
2601 if (tid_data->ssn != tid_data->next_reclaimed) {
2602 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2603 err = 0;
2604 break;
2605 }
2606
2607 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002608 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002609 spin_unlock_bh(&mvmsta->lock);
2610
2611 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2612
2613 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2614
Liad Kaufmancf961e12015-08-13 19:16:08 +03002615 if (!iwl_mvm_is_dqa_supported(mvm)) {
2616 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2617
2618 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2619 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002620 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002621 case IWL_AGG_STARTING:
2622 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2623 /*
2624 * The agg session has been stopped before it was set up. This
2625 * can happen when the AddBA timer times out for example.
2626 */
2627
2628 /* No barriers since we are under mutex */
2629 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002630
2631 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2632 tid_data->state = IWL_AGG_OFF;
2633 err = 0;
2634 break;
2635 default:
2636 IWL_ERR(mvm,
2637 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2638 mvmsta->sta_id, tid, tid_data->state);
2639 IWL_ERR(mvm,
2640 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2641 err = -EINVAL;
2642 }
2643
2644 spin_unlock_bh(&mvmsta->lock);
2645
2646 return err;
2647}
2648
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002649int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2650 struct ieee80211_sta *sta, u16 tid)
2651{
Johannes Berg5b577a92013-11-14 18:20:04 +01002652 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002653 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2654 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002655 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002656
2657 /*
2658 * First set the agg state to OFF to avoid calling
2659 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2660 */
2661 spin_lock_bh(&mvmsta->lock);
2662 txq_id = tid_data->txq_id;
2663 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2664 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002665 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002666 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002667 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002668 spin_unlock_bh(&mvmsta->lock);
2669
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002670 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002671 /*
2672 * The TXQ is marked as reserved only if no traffic came through yet
2673 * This means no traffic has been sent on this TID (agg'd or not), so
2674 * we no longer have use for the queue. Since it hasn't even been
2675 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2676 * free.
2677 */
2678 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2679 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002680 spin_unlock_bh(&mvm->queue_info_lock);
2681
Johannes Bergb6658ff2013-07-24 13:55:51 +02002682 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002683 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03002684 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02002685 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002686 iwl_trans_wait_tx_queue_empty(mvm->trans,
2687 mvmsta->tfd_queue_msk);
2688 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002689
Johannes Bergf7f89e72014-08-05 15:24:44 +02002690 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2691
Liad Kaufmancf961e12015-08-13 19:16:08 +03002692 if (!iwl_mvm_is_dqa_supported(mvm)) {
2693 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2694
2695 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2696 tid, 0);
2697 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002698 }
2699
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002700 return 0;
2701}
2702
Johannes Berg8ca151b2013-01-24 14:25:36 +01002703static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2704{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002705 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002706
2707 lockdep_assert_held(&mvm->mutex);
2708
Johannes Berg2dc2a152015-06-16 17:09:18 +02002709 /* Pick the unused key offset with the highest 'deleted'
2710 * counter. Every time a key is deleted, all the counters
2711 * are incremented and the one that was just deleted is
2712 * reset to zero. Thus, the highest counter is the one
2713 * that was deleted longest ago. Pick that one.
2714 */
2715 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2716 if (test_bit(i, mvm->fw_key_table))
2717 continue;
2718 if (mvm->fw_key_deleted[i] > max) {
2719 max = mvm->fw_key_deleted[i];
2720 max_offs = i;
2721 }
2722 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002723
Johannes Berg2dc2a152015-06-16 17:09:18 +02002724 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002725 return STA_KEY_IDX_INVALID;
2726
Johannes Berg2dc2a152015-06-16 17:09:18 +02002727 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002728}
2729
Johannes Berg5f7a1842015-12-11 09:36:10 +01002730static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2731 struct ieee80211_vif *vif,
2732 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002733{
Johannes Berg5b530e92014-12-23 16:00:17 +01002734 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002735
Johannes Berg5f7a1842015-12-11 09:36:10 +01002736 if (sta)
2737 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002738
2739 /*
2740 * The device expects GTKs for station interfaces to be
2741 * installed as GTKs for the AP station. If we have no
2742 * station ID, then use AP's station ID.
2743 */
2744 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02002745 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Avri Altman9513c5e2015-10-19 16:29:11 +02002746 u8 sta_id = mvmvif->ap_sta_id;
2747
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002748 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2749 lockdep_is_held(&mvm->mutex));
2750
Avri Altman9513c5e2015-10-19 16:29:11 +02002751 /*
2752 * It is possible that the 'sta' parameter is NULL,
2753 * for example when a GTK is removed - the sta_id will then
2754 * be the AP ID, and no station was passed by mac80211.
2755 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002756 if (IS_ERR_OR_NULL(sta))
2757 return NULL;
2758
2759 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02002760 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002761
Johannes Berg5f7a1842015-12-11 09:36:10 +01002762 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002763}
2764
2765static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2766 struct iwl_mvm_sta *mvm_sta,
Sara Sharon45c458b2016-11-09 15:43:26 +02002767 struct ieee80211_key_conf *key, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002768 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2769 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002770{
Sara Sharon45c458b2016-11-09 15:43:26 +02002771 union {
2772 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2773 struct iwl_mvm_add_sta_key_cmd cmd;
2774 } u = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002775 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002776 int ret;
2777 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002778 u16 keyidx;
Sara Sharon45c458b2016-11-09 15:43:26 +02002779 u64 pn = 0;
2780 int i, size;
2781 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2782 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002783
Sara Sharon45c458b2016-11-09 15:43:26 +02002784 keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
Johannes Berg8ca151b2013-01-24 14:25:36 +01002785 STA_KEY_FLG_KEYID_MSK;
2786 key_flags = cpu_to_le16(keyidx);
2787 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2788
Sara Sharon45c458b2016-11-09 15:43:26 +02002789 switch (key->cipher) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002790 case WLAN_CIPHER_SUITE_TKIP:
2791 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002792 if (new_api) {
2793 memcpy((void *)&u.cmd.tx_mic_key,
2794 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
2795 IWL_MIC_KEY_SIZE);
2796
2797 memcpy((void *)&u.cmd.rx_mic_key,
2798 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
2799 IWL_MIC_KEY_SIZE);
2800 pn = atomic64_read(&key->tx_pn);
2801
2802 } else {
2803 u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
2804 for (i = 0; i < 5; i++)
2805 u.cmd_v1.tkip_rx_ttak[i] =
2806 cpu_to_le16(tkip_p1k[i]);
2807 }
2808 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002809 break;
2810 case WLAN_CIPHER_SUITE_CCMP:
2811 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Sara Sharon45c458b2016-11-09 15:43:26 +02002812 memcpy(u.cmd.common.key, key->key, key->keylen);
2813 if (new_api)
2814 pn = atomic64_read(&key->tx_pn);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002815 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002816 case WLAN_CIPHER_SUITE_WEP104:
2817 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002818 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002819 case WLAN_CIPHER_SUITE_WEP40:
2820 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002821 memcpy(u.cmd.common.key + 3, key->key, key->keylen);
Johannes Bergba3943b2014-11-12 23:54:48 +01002822 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03002823 case WLAN_CIPHER_SUITE_GCMP_256:
2824 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2825 /* fall through */
2826 case WLAN_CIPHER_SUITE_GCMP:
2827 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
Sara Sharon45c458b2016-11-09 15:43:26 +02002828 memcpy(u.cmd.common.key, key->key, key->keylen);
2829 if (new_api)
2830 pn = atomic64_read(&key->tx_pn);
Ayala Beker2a53d162016-04-07 16:21:57 +03002831 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002832 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002833 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
Sara Sharon45c458b2016-11-09 15:43:26 +02002834 memcpy(u.cmd.common.key, key->key, key->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002835 }
2836
Johannes Bergba3943b2014-11-12 23:54:48 +01002837 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002838 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2839
Sara Sharon45c458b2016-11-09 15:43:26 +02002840 u.cmd.common.key_offset = key_offset;
2841 u.cmd.common.key_flags = key_flags;
2842 u.cmd.common.sta_id = mvm_sta->sta_id;
2843
2844 if (new_api) {
2845 u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
2846 size = sizeof(u.cmd);
2847 } else {
2848 size = sizeof(u.cmd_v1);
2849 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002850
2851 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002852 if (cmd_flags & CMD_ASYNC)
Sara Sharon45c458b2016-11-09 15:43:26 +02002853 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
2854 &u.cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002855 else
Sara Sharon45c458b2016-11-09 15:43:26 +02002856 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
2857 &u.cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002858
2859 switch (status) {
2860 case ADD_STA_SUCCESS:
2861 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2862 break;
2863 default:
2864 ret = -EIO;
2865 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2866 break;
2867 }
2868
2869 return ret;
2870}
2871
2872static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2873 struct ieee80211_key_conf *keyconf,
2874 u8 sta_id, bool remove_key)
2875{
2876 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2877
2878 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03002879 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2880 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
2881 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2882 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
2883 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
2884 return -EINVAL;
2885
2886 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
2887 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01002888 return -EINVAL;
2889
2890 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2891 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2892
2893 if (remove_key) {
2894 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2895 } else {
2896 struct ieee80211_key_seq seq;
2897 const u8 *pn;
2898
Ayala Bekeraa950522016-06-01 00:28:09 +03002899 switch (keyconf->cipher) {
2900 case WLAN_CIPHER_SUITE_AES_CMAC:
2901 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
2902 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03002903 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2904 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2905 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
2906 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03002907 default:
2908 return -EINVAL;
2909 }
2910
Ayala Beker8e160ab2016-04-11 11:37:38 +03002911 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
2912 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2913 igtk_cmd.ctrl_flags |=
2914 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002915 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2916 pn = seq.aes_cmac.pn;
2917 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2918 ((u64) pn[4] << 8) |
2919 ((u64) pn[3] << 16) |
2920 ((u64) pn[2] << 24) |
2921 ((u64) pn[1] << 32) |
2922 ((u64) pn[0] << 40));
2923 }
2924
2925 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2926 remove_key ? "removing" : "installing",
2927 igtk_cmd.sta_id);
2928
Ayala Beker8e160ab2016-04-11 11:37:38 +03002929 if (!iwl_mvm_has_new_rx_api(mvm)) {
2930 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
2931 .ctrl_flags = igtk_cmd.ctrl_flags,
2932 .key_id = igtk_cmd.key_id,
2933 .sta_id = igtk_cmd.sta_id,
2934 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
2935 };
2936
2937 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
2938 ARRAY_SIZE(igtk_cmd_v1.igtk));
2939 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2940 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
2941 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002942 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002943 sizeof(igtk_cmd), &igtk_cmd);
2944}
2945
2946
2947static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2948 struct ieee80211_vif *vif,
2949 struct ieee80211_sta *sta)
2950{
Johannes Berg5b530e92014-12-23 16:00:17 +01002951 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002952
2953 if (sta)
2954 return sta->addr;
2955
2956 if (vif->type == NL80211_IFTYPE_STATION &&
Sara Sharon0ae98812017-01-04 14:53:58 +02002957 mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002958 u8 sta_id = mvmvif->ap_sta_id;
2959 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2960 lockdep_is_held(&mvm->mutex));
2961 return sta->addr;
2962 }
2963
2964
2965 return NULL;
2966}
2967
Johannes Berg2f6319d2014-11-12 23:39:56 +01002968static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2969 struct ieee80211_vif *vif,
2970 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002971 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002972 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01002973 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002974{
Johannes Berg2f6319d2014-11-12 23:39:56 +01002975 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002976 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002977 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002978 struct ieee80211_key_seq seq;
2979 u16 p1k[5];
2980
Johannes Berg8ca151b2013-01-24 14:25:36 +01002981 switch (keyconf->cipher) {
2982 case WLAN_CIPHER_SUITE_TKIP:
2983 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2984 /* get phase 1 key from mac80211 */
2985 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2986 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01002987 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002988 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002989 break;
2990 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01002991 case WLAN_CIPHER_SUITE_WEP40:
2992 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03002993 case WLAN_CIPHER_SUITE_GCMP:
2994 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergba3943b2014-11-12 23:54:48 +01002995 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002996 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002997 break;
2998 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01002999 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003000 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003001 }
3002
Johannes Berg8ca151b2013-01-24 14:25:36 +01003003 return ret;
3004}
3005
Johannes Berg2f6319d2014-11-12 23:39:56 +01003006static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01003007 struct ieee80211_key_conf *keyconf,
3008 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003009{
Sara Sharon45c458b2016-11-09 15:43:26 +02003010 union {
3011 struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3012 struct iwl_mvm_add_sta_key_cmd cmd;
3013 } u = {};
3014 bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3015 IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003016 __le16 key_flags;
Sara Sharon45c458b2016-11-09 15:43:26 +02003017 int ret, size;
Johannes Berg79920742014-11-03 15:43:04 +01003018 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003019
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02003020 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
3021 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003022 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
3023 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
3024
Johannes Bergba3943b2014-11-12 23:54:48 +01003025 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003026 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3027
Sara Sharon45c458b2016-11-09 15:43:26 +02003028 /*
3029 * The fields assigned here are in the same location at the start
3030 * of the command, so we can do this union trick.
3031 */
3032 u.cmd.common.key_flags = key_flags;
3033 u.cmd.common.key_offset = keyconf->hw_key_idx;
3034 u.cmd.common.sta_id = sta_id;
3035
3036 size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003037
Johannes Berg8ca151b2013-01-24 14:25:36 +01003038 status = ADD_STA_SUCCESS;
Sara Sharon45c458b2016-11-09 15:43:26 +02003039 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
3040 &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003041
3042 switch (status) {
3043 case ADD_STA_SUCCESS:
3044 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
3045 break;
3046 default:
3047 ret = -EIO;
3048 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
3049 break;
3050 }
3051
3052 return ret;
3053}
3054
Johannes Berg2f6319d2014-11-12 23:39:56 +01003055int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3056 struct ieee80211_vif *vif,
3057 struct ieee80211_sta *sta,
3058 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003059 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003060{
Johannes Bergba3943b2014-11-12 23:54:48 +01003061 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003062 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003063 u8 sta_id;
3064 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03003065 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01003066
3067 lockdep_assert_held(&mvm->mutex);
3068
3069 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01003070 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3071 if (!mvm_sta) {
3072 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01003073 return -EINVAL;
3074 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01003075 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003076
Ayala Beker8e160ab2016-04-11 11:37:38 +03003077 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3078 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3079 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003080 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3081 goto end;
3082 }
3083
3084 /*
3085 * It is possible that the 'sta' parameter is NULL, and thus
3086 * there is a need to retrieve the sta from the local station table.
3087 */
3088 if (!sta) {
3089 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3090 lockdep_is_held(&mvm->mutex));
3091 if (IS_ERR_OR_NULL(sta)) {
3092 IWL_ERR(mvm, "Invalid station id\n");
3093 return -EINVAL;
3094 }
3095 }
3096
3097 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3098 return -EINVAL;
3099
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003100 /* If the key_offset is not pre-assigned, we need to find a
3101 * new offset to use. In normal cases, the offset is not
3102 * pre-assigned, but during HW_RESTART we want to reuse the
3103 * same indices, so we pass them when this function is called.
3104 *
3105 * In D3 entry, we need to hardcoded the indices (because the
3106 * firmware hardcodes the PTK offset to 0). In this case, we
3107 * need to make sure we don't overwrite the hw_key_idx in the
3108 * keyconf structure, because otherwise we cannot configure
3109 * the original ones back when resuming.
3110 */
3111 if (key_offset == STA_KEY_IDX_INVALID) {
3112 key_offset = iwl_mvm_set_fw_key_idx(mvm);
3113 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003114 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003115 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003116 }
3117
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003118 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003119 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01003120 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003121
3122 /*
3123 * For WEP, the same key is used for multicast and unicast. Upload it
3124 * again, using the same key offset, and now pointing the other one
3125 * to the same key slot (offset).
3126 * If this fails, remove the original as well.
3127 */
3128 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3129 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003130 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3131 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01003132 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01003133 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003134 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01003135 }
3136 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01003137
Luca Coelho9c3deeb2015-11-11 01:06:17 +02003138 __set_bit(key_offset, mvm->fw_key_table);
3139
Johannes Berg2f6319d2014-11-12 23:39:56 +01003140end:
3141 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3142 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03003143 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01003144 return ret;
3145}
3146
3147int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3148 struct ieee80211_vif *vif,
3149 struct ieee80211_sta *sta,
3150 struct ieee80211_key_conf *keyconf)
3151{
Johannes Bergba3943b2014-11-12 23:54:48 +01003152 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01003153 struct iwl_mvm_sta *mvm_sta;
Sara Sharon0ae98812017-01-04 14:53:58 +02003154 u8 sta_id = IWL_MVM_INVALID_STA;
Johannes Berg2dc2a152015-06-16 17:09:18 +02003155 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003156
3157 lockdep_assert_held(&mvm->mutex);
3158
Johannes Berg5f7a1842015-12-11 09:36:10 +01003159 /* Get the station from the mvm local station table */
3160 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Ilan Peercd4d23c2017-01-16 15:07:03 +02003161 if (!mvm_sta) {
3162 IWL_ERR(mvm, "Failed to find station\n");
3163 return -EINVAL;
3164 }
3165 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003166
3167 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3168 keyconf->keyidx, sta_id);
3169
Ayala Beker8e160ab2016-04-11 11:37:38 +03003170 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3171 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3172 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
Johannes Berg2f6319d2014-11-12 23:39:56 +01003173 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3174
3175 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3176 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3177 keyconf->hw_key_idx);
3178 return -ENOENT;
3179 }
3180
Johannes Berg2dc2a152015-06-16 17:09:18 +02003181 /* track which key was deleted last */
3182 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3183 if (mvm->fw_key_deleted[i] < U8_MAX)
3184 mvm->fw_key_deleted[i]++;
3185 }
3186 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3187
Johannes Berg5f7a1842015-12-11 09:36:10 +01003188 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01003189 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3190 return 0;
3191 }
3192
Johannes Bergba3943b2014-11-12 23:54:48 +01003193 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3194 if (ret)
3195 return ret;
3196
3197 /* delete WEP key twice to get rid of (now useless) offset */
3198 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3199 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3200 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3201
3202 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01003203}
3204
Johannes Berg8ca151b2013-01-24 14:25:36 +01003205void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3206 struct ieee80211_vif *vif,
3207 struct ieee80211_key_conf *keyconf,
3208 struct ieee80211_sta *sta, u32 iv32,
3209 u16 *phase1key)
3210{
Beni Levc3eb5362013-02-06 17:22:18 +02003211 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01003212 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003213
Beni Levc3eb5362013-02-06 17:22:18 +02003214 rcu_read_lock();
3215
Johannes Berg5f7a1842015-12-11 09:36:10 +01003216 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3217 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003218 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01003219 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02003220 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02003221
3222 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02003223 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01003224}
3225
Johannes Berg9cc40712013-02-15 22:47:48 +01003226void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3227 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003228{
Johannes Berg5b577a92013-11-14 18:20:04 +01003229 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003230 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003231 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003232 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03003233 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01003234 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003235 };
3236 int ret;
3237
Sara Sharon854c5702016-01-26 13:17:47 +02003238 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3239 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003240 if (ret)
3241 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3242}
3243
Johannes Berg9cc40712013-02-15 22:47:48 +01003244void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3245 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003246 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003247 u16 cnt, u16 tids, bool more_data,
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003248 bool single_sta_queue)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003249{
Johannes Berg5b577a92013-11-14 18:20:04 +01003250 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003251 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003252 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003253 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003254 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3255 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003256 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003257 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003258 int tid, ret;
3259 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003260
Johannes Berg3e56ead2013-02-15 22:23:18 +01003261 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3262 * Note that this field is reserved and unused by firmware not
3263 * supporting GO uAPSD, so it's safe to always do this.
3264 */
3265 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3266 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3267
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003268 /* If we're releasing frames from aggregation or dqa queues then check
3269 * if all the queues that we're releasing frames from, combined, have:
Johannes Berg3e56ead2013-02-15 22:23:18 +01003270 * - more frames than the service period, in which case more_data
3271 * needs to be set
3272 * - fewer than 'cnt' frames, in which case we need to adjust the
3273 * firmware command (but do that unconditionally)
3274 */
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003275 if (single_sta_queue) {
Johannes Berg3e56ead2013-02-15 22:23:18 +01003276 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003277 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003278
3279 spin_lock_bh(&mvmsta->lock);
3280 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3281 struct iwl_mvm_tid_data *tid_data;
3282 u16 n_queued;
3283
3284 tid_data = &mvmsta->tid_data[tid];
Sara Sharon9a3fcf92017-03-14 09:50:35 +02003285 if (WARN(!iwl_mvm_is_dqa_supported(mvm) &&
3286 tid_data->state != IWL_AGG_ON &&
Johannes Berg3e56ead2013-02-15 22:23:18 +01003287 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3288 "TID %d state is %d\n",
3289 tid, tid_data->state)) {
3290 spin_unlock_bh(&mvmsta->lock);
3291 ieee80211_sta_eosp(sta);
3292 return;
3293 }
3294
3295 n_queued = iwl_mvm_tid_queued(tid_data);
3296 if (n_queued > remaining) {
3297 more_data = true;
3298 remaining = 0;
3299 break;
3300 }
3301 remaining -= n_queued;
3302 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003303 sleep_tx_count = cnt - remaining;
3304 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3305 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003306 spin_unlock_bh(&mvmsta->lock);
3307
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003308 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003309 if (WARN_ON(cnt - remaining == 0)) {
3310 ieee80211_sta_eosp(sta);
3311 return;
3312 }
3313 }
3314
3315 /* Note: this is ignored by firmware not supporting GO uAPSD */
3316 if (more_data)
3317 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
3318
3319 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3320 mvmsta->next_status_eosp = true;
3321 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
3322 } else {
3323 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
3324 }
3325
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003326 /* block the Tx queues until the FW updated the sleep Tx count */
3327 iwl_trans_block_txq_ptrs(mvm->trans, true);
3328
3329 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3330 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003331 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003332 if (ret)
3333 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3334}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003335
Johannes Berg04168412015-06-23 21:22:09 +02003336void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3337 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003338{
3339 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3340 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3341 struct ieee80211_sta *sta;
3342 u32 sta_id = le32_to_cpu(notif->sta_id);
3343
3344 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003345 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003346
3347 rcu_read_lock();
3348 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3349 if (!IS_ERR_OR_NULL(sta))
3350 ieee80211_sta_eosp(sta);
3351 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003352}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003353
3354void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3355 struct iwl_mvm_sta *mvmsta, bool disable)
3356{
3357 struct iwl_mvm_add_sta_cmd cmd = {
3358 .add_modify = STA_MODE_MODIFY,
3359 .sta_id = mvmsta->sta_id,
3360 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3361 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3362 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3363 };
3364 int ret;
3365
Sara Sharon854c5702016-01-26 13:17:47 +02003366 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3367 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003368 if (ret)
3369 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3370}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003371
3372void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3373 struct ieee80211_sta *sta,
3374 bool disable)
3375{
3376 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3377
3378 spin_lock_bh(&mvm_sta->lock);
3379
3380 if (mvm_sta->disable_tx == disable) {
3381 spin_unlock_bh(&mvm_sta->lock);
3382 return;
3383 }
3384
3385 mvm_sta->disable_tx = disable;
3386
3387 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03003388 * Tell mac80211 to start/stop queuing tx for this station,
3389 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003390 * for this station.
3391 */
3392 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3393 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3394
3395 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3396
3397 spin_unlock_bh(&mvm_sta->lock);
3398}
3399
3400void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3401 struct iwl_mvm_vif *mvmvif,
3402 bool disable)
3403{
3404 struct ieee80211_sta *sta;
3405 struct iwl_mvm_sta *mvm_sta;
3406 int i;
3407
3408 lockdep_assert_held(&mvm->mutex);
3409
3410 /* Block/unblock all the stations of the given mvmvif */
Sara Sharon0ae98812017-01-04 14:53:58 +02003411 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003412 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3413 lockdep_is_held(&mvm->mutex));
3414 if (IS_ERR_OR_NULL(sta))
3415 continue;
3416
3417 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3418 if (mvm_sta->mac_id_n_color !=
3419 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3420 continue;
3421
3422 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3423 }
3424}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003425
3426void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3427{
3428 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3429 struct iwl_mvm_sta *mvmsta;
3430
3431 rcu_read_lock();
3432
3433 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3434
3435 if (!WARN_ON(!mvmsta))
3436 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3437
3438 rcu_read_unlock();
3439}