blob: 1617de7c2ddc5e473c357c598194e00219006f80 [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 Sharon854c5702016-01-26 13:17:47 +020010 * Copyright(c) 2016 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 Sharon854c5702016-01-26 13:17:47 +020037 * Copyright(c) 2016 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 */
Eliad Pellerb92e6612014-01-23 17:58:23 +0200101 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
102 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 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109 return IWL_MVM_STATION_COUNT;
110}
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);
205
206 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200207 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
208 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300209 &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100210 if (ret)
211 return ret;
212
Sara Sharon837c4da2016-01-07 16:50:45 +0200213 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100214 case ADD_STA_SUCCESS:
215 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
216 break;
217 default:
218 ret = -EIO;
219 IWL_ERR(mvm, "ADD_STA failed\n");
220 break;
221 }
222
223 return ret;
224}
225
Sara Sharon10b2b202016-03-20 16:23:41 +0200226static void iwl_mvm_rx_agg_session_expired(unsigned long data)
227{
228 struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
229 struct iwl_mvm_baid_data *ba_data;
230 struct ieee80211_sta *sta;
231 struct iwl_mvm_sta *mvm_sta;
232 unsigned long timeout;
233
234 rcu_read_lock();
235
236 ba_data = rcu_dereference(*rcu_ptr);
237
238 if (WARN_ON(!ba_data))
239 goto unlock;
240
241 if (!ba_data->timeout)
242 goto unlock;
243
244 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
245 if (time_is_after_jiffies(timeout)) {
246 mod_timer(&ba_data->session_timer, timeout);
247 goto unlock;
248 }
249
250 /* Timer expired */
251 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
252 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
253 ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
254 sta->addr, ba_data->tid);
255unlock:
256 rcu_read_unlock();
257}
258
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300259static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
260 struct ieee80211_sta *sta)
261{
262 unsigned long used_hw_queues;
263 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200264 unsigned int wdg_timeout =
265 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300266 u32 ac;
267
268 lockdep_assert_held(&mvm->mutex);
269
270 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
271
272 /* Find available queues, and allocate them to the ACs */
273 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
274 u8 queue = find_first_zero_bit(&used_hw_queues,
275 mvm->first_agg_queue);
276
277 if (queue >= mvm->first_agg_queue) {
278 IWL_ERR(mvm, "Failed to allocate STA queue\n");
279 return -EBUSY;
280 }
281
282 __set_bit(queue, &used_hw_queues);
283 mvmsta->hw_queue[ac] = queue;
284 }
285
286 /* Found a place for all queues - enable them */
287 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
288 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300289 mvmsta->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300290 iwl_mvm_ac_to_tx_fifo[ac], 0,
291 wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300292 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
293 }
294
295 return 0;
296}
297
298static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
299 struct ieee80211_sta *sta)
300{
301 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
302 unsigned long sta_msk;
303 int i;
304
305 lockdep_assert_held(&mvm->mutex);
306
307 /* disable the TDLS STA-specific queues */
308 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200309 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300310 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300311}
312
Liad Kaufman9794c642015-08-19 17:34:28 +0300313/* Disable aggregations for a bitmap of TIDs for a given station */
314static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
315 unsigned long disable_agg_tids,
316 bool remove_queue)
317{
318 struct iwl_mvm_add_sta_cmd cmd = {};
319 struct ieee80211_sta *sta;
320 struct iwl_mvm_sta *mvmsta;
321 u32 status;
322 u8 sta_id;
323 int ret;
324
325 spin_lock_bh(&mvm->queue_info_lock);
326 sta_id = mvm->queue_info[queue].ra_sta_id;
327 spin_unlock_bh(&mvm->queue_info_lock);
328
329 rcu_read_lock();
330
331 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
332
333 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
334 rcu_read_unlock();
335 return -EINVAL;
336 }
337
338 mvmsta = iwl_mvm_sta_from_mac80211(sta);
339
340 mvmsta->tid_disable_agg |= disable_agg_tids;
341
342 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
343 cmd.sta_id = mvmsta->sta_id;
344 cmd.add_modify = STA_MODE_MODIFY;
345 cmd.modify_mask = STA_MODIFY_QUEUES;
346 if (disable_agg_tids)
347 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
348 if (remove_queue)
349 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
350 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
351 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
352
353 rcu_read_unlock();
354
355 /* Notify FW of queue removal from the STA queues */
356 status = ADD_STA_SUCCESS;
357 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
358 iwl_mvm_add_sta_cmd_size(mvm),
359 &cmd, &status);
360
361 return ret;
362}
363
Liad Kaufman42db09c2016-05-02 14:01:14 +0300364static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
365{
366 struct ieee80211_sta *sta;
367 struct iwl_mvm_sta *mvmsta;
368 unsigned long tid_bitmap;
369 unsigned long agg_tids = 0;
370 s8 sta_id;
371 int tid;
372
373 lockdep_assert_held(&mvm->mutex);
374
375 spin_lock_bh(&mvm->queue_info_lock);
376 sta_id = mvm->queue_info[queue].ra_sta_id;
377 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
378 spin_unlock_bh(&mvm->queue_info_lock);
379
380 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
381 lockdep_is_held(&mvm->mutex));
382
383 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
384 return -EINVAL;
385
386 mvmsta = iwl_mvm_sta_from_mac80211(sta);
387
388 spin_lock_bh(&mvmsta->lock);
389 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
390 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
391 agg_tids |= BIT(tid);
392 }
393 spin_unlock_bh(&mvmsta->lock);
394
395 return agg_tids;
396}
397
Liad Kaufman9794c642015-08-19 17:34:28 +0300398/*
399 * Remove a queue from a station's resources.
400 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
401 * doesn't disable the queue
402 */
403static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
404{
405 struct ieee80211_sta *sta;
406 struct iwl_mvm_sta *mvmsta;
407 unsigned long tid_bitmap;
408 unsigned long disable_agg_tids = 0;
409 u8 sta_id;
410 int tid;
411
412 lockdep_assert_held(&mvm->mutex);
413
414 spin_lock_bh(&mvm->queue_info_lock);
415 sta_id = mvm->queue_info[queue].ra_sta_id;
416 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
417 spin_unlock_bh(&mvm->queue_info_lock);
418
419 rcu_read_lock();
420
421 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
422
423 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
424 rcu_read_unlock();
425 return 0;
426 }
427
428 mvmsta = iwl_mvm_sta_from_mac80211(sta);
429
430 spin_lock_bh(&mvmsta->lock);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300431 /* Unmap MAC queues and TIDs from this queue */
Liad Kaufman9794c642015-08-19 17:34:28 +0300432 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300433 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
434 disable_agg_tids |= BIT(tid);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300435 mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
Liad Kaufman9794c642015-08-19 17:34:28 +0300436 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300437
Liad Kaufman42db09c2016-05-02 14:01:14 +0300438 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
Liad Kaufman9794c642015-08-19 17:34:28 +0300439 spin_unlock_bh(&mvmsta->lock);
440
441 rcu_read_unlock();
442
Liad Kaufman42db09c2016-05-02 14:01:14 +0300443 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman9794c642015-08-19 17:34:28 +0300444 /* Unmap MAC queues and TIDs from this queue */
445 mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
446 mvm->queue_info[queue].hw_queue_refcount = 0;
447 mvm->queue_info[queue].tid_bitmap = 0;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300448 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman9794c642015-08-19 17:34:28 +0300449
450 return disable_agg_tids;
451}
452
Liad Kaufman42db09c2016-05-02 14:01:14 +0300453static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
454 unsigned long tfd_queue_mask, u8 ac)
455{
456 int queue = 0;
457 u8 ac_to_queue[IEEE80211_NUM_ACS];
458 int i;
459
460 lockdep_assert_held(&mvm->queue_info_lock);
461
462 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
463
464 /* See what ACs the existing queues for this STA have */
465 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
466 /* Only DATA queues can be shared */
467 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
468 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
469 continue;
470
471 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
472 }
473
474 /*
475 * The queue to share is chosen only from DATA queues as follows (in
476 * descending priority):
477 * 1. An AC_BE queue
478 * 2. Same AC queue
479 * 3. Highest AC queue that is lower than new AC
480 * 4. Any existing AC (there always is at least 1 DATA queue)
481 */
482
483 /* Priority 1: An AC_BE queue */
484 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
485 queue = ac_to_queue[IEEE80211_AC_BE];
486 /* Priority 2: Same AC queue */
487 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
488 queue = ac_to_queue[ac];
489 /* Priority 3a: If new AC is VO and VI exists - use VI */
490 else if (ac == IEEE80211_AC_VO &&
491 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
492 queue = ac_to_queue[IEEE80211_AC_VI];
493 /* Priority 3b: No BE so only AC less than the new one is BK */
494 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
495 queue = ac_to_queue[IEEE80211_AC_BK];
496 /* Priority 4a: No BE nor BK - use VI if exists */
497 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
498 queue = ac_to_queue[IEEE80211_AC_VI];
499 /* Priority 4b: No BE, BK nor VI - use VO if exists */
500 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
501 queue = ac_to_queue[IEEE80211_AC_VO];
502
503 /* Make sure queue found (or not) is legal */
504 if (!((queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE &&
505 queue <= IWL_MVM_DQA_MAX_MGMT_QUEUE) ||
506 (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE &&
507 queue <= IWL_MVM_DQA_MAX_DATA_QUEUE) ||
508 (queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE))) {
509 IWL_ERR(mvm, "No DATA queues available to share\n");
510 queue = -ENOSPC;
511 }
512
513 return queue;
514}
515
Liad Kaufman24afba72015-07-28 18:56:08 +0300516static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
517 struct ieee80211_sta *sta, u8 ac, int tid,
518 struct ieee80211_hdr *hdr)
519{
520 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
521 struct iwl_trans_txq_scd_cfg cfg = {
522 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
523 .sta_id = mvmsta->sta_id,
524 .tid = tid,
525 .frame_limit = IWL_FRAME_LIMIT,
526 };
527 unsigned int wdg_timeout =
528 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
529 u8 mac_queue = mvmsta->vif->hw_queue[ac];
530 int queue = -1;
Liad Kaufman9794c642015-08-19 17:34:28 +0300531 bool using_inactive_queue = false;
532 unsigned long disable_agg_tids = 0;
533 enum iwl_mvm_agg_state queue_state;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300534 bool shared_queue = false;
Liad Kaufman24afba72015-07-28 18:56:08 +0300535 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300536 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300537 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300538
539 lockdep_assert_held(&mvm->mutex);
540
Liad Kaufman42db09c2016-05-02 14:01:14 +0300541 spin_lock_bh(&mvmsta->lock);
542 tfd_queue_mask = mvmsta->tfd_queue_msk;
543 spin_unlock_bh(&mvmsta->lock);
544
Liad Kaufmand2515a92016-03-23 16:31:08 +0200545 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300546
547 /*
548 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
549 * exists
550 */
551 if (!ieee80211_is_data_qos(hdr->frame_control) ||
552 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300553 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
554 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300555 IWL_MVM_DQA_MAX_MGMT_QUEUE);
556 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
557 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
558 queue);
559
560 /* If no such queue is found, we'll use a DATA queue instead */
561 }
562
Liad Kaufman9794c642015-08-19 17:34:28 +0300563 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
564 (mvm->queue_info[mvmsta->reserved_queue].status ==
565 IWL_MVM_QUEUE_RESERVED ||
566 mvm->queue_info[mvmsta->reserved_queue].status ==
567 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300568 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300569 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300570 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
571 }
572
573 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300574 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
575 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300576 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300577
578 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300579 * Check if this queue is already allocated but inactive.
580 * In such a case, we'll need to first free this queue before enabling
581 * it again, so we'll mark it as reserved to make sure no new traffic
582 * arrives on it
583 */
584 if (queue > 0 &&
585 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
586 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
587 using_inactive_queue = true;
588 IWL_DEBUG_TX_QUEUES(mvm,
589 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
590 queue, mvmsta->sta_id, tid);
591 }
592
Liad Kaufman42db09c2016-05-02 14:01:14 +0300593 /* No free queue - we'll have to share */
594 if (queue <= 0) {
595 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
596 if (queue > 0) {
597 shared_queue = true;
598 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
599 }
600 }
601
Liad Kaufman9794c642015-08-19 17:34:28 +0300602 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300603 * Mark TXQ as ready, even though it hasn't been fully configured yet,
604 * to make sure no one else takes it.
605 * This will allow avoiding re-acquiring the lock at the end of the
606 * configuration. On error we'll mark it back as free.
607 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300608 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300609 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300610
Liad Kaufmand2515a92016-03-23 16:31:08 +0200611 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300612
Liad Kaufman42db09c2016-05-02 14:01:14 +0300613 /* This shouldn't happen - out of queues */
614 if (WARN_ON(queue <= 0)) {
615 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
616 tid, cfg.sta_id);
Liad Kaufman24afba72015-07-28 18:56:08 +0300617 return -ENOSPC;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300618 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300619
620 /*
621 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
622 * but for configuring the SCD to send A-MPDUs we need to mark the queue
623 * as aggregatable.
624 * Mark all DATA queues as allowing to be aggregated at some point
625 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300626 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
627 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300628
Liad Kaufman9794c642015-08-19 17:34:28 +0300629 /*
630 * If this queue was previously inactive (idle) - we need to free it
631 * first
632 */
633 if (using_inactive_queue) {
634 struct iwl_scd_txq_cfg_cmd cmd = {
635 .scd_queue = queue,
636 .enable = 0,
637 };
Liad Kaufman93f436e2015-08-31 13:41:26 +0300638 u8 ac;
Liad Kaufman9794c642015-08-19 17:34:28 +0300639
640 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
641
Liad Kaufman93f436e2015-08-31 13:41:26 +0300642 spin_lock_bh(&mvm->queue_info_lock);
643 ac = mvm->queue_info[queue].mac80211_ac;
644 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
645 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[ac];
646 spin_unlock_bh(&mvm->queue_info_lock);
647
Liad Kaufman9794c642015-08-19 17:34:28 +0300648 /* Disable the queue */
649 iwl_mvm_invalidate_sta_queue(mvm, queue, disable_agg_tids,
650 true);
651 iwl_trans_txq_disable(mvm->trans, queue, false);
652 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
653 &cmd);
654 if (ret) {
655 IWL_ERR(mvm,
656 "Failed to free inactive queue %d (ret=%d)\n",
657 queue, ret);
658
659 /* Re-mark the inactive queue as inactive */
660 spin_lock_bh(&mvm->queue_info_lock);
661 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
662 spin_unlock_bh(&mvm->queue_info_lock);
663
664 return ret;
665 }
666 }
667
Liad Kaufman42db09c2016-05-02 14:01:14 +0300668 IWL_DEBUG_TX_QUEUES(mvm,
669 "Allocating %squeue #%d to sta %d on tid %d\n",
670 shared_queue ? "shared " : "", queue,
671 mvmsta->sta_id, tid);
672
673 if (shared_queue) {
674 /* Disable any open aggs on this queue */
675 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
676
677 if (disable_agg_tids) {
678 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
679 queue);
680 iwl_mvm_invalidate_sta_queue(mvm, queue,
681 disable_agg_tids, false);
682 }
683
684 /* Mark queue as shared in transport */
685 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
686
687 /* TODO: a redirection may be required - DQA phase 2 */
688 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300689
690 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
691 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
692 wdg_timeout);
693
694 spin_lock_bh(&mvmsta->lock);
695 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300696 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300697 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300698 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300699
700 if (mvmsta->reserved_queue == queue)
701 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
702 spin_unlock_bh(&mvmsta->lock);
703
Liad Kaufman42db09c2016-05-02 14:01:14 +0300704 if (!shared_queue) {
705 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
706 if (ret)
707 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300708
Liad Kaufman42db09c2016-05-02 14:01:14 +0300709 /* If we need to re-enable aggregations... */
710 if (queue_state == IWL_AGG_ON) {
711 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
712 if (ret)
713 goto out_err;
714 }
715 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300716
Liad Kaufman42db09c2016-05-02 14:01:14 +0300717 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300718
719out_err:
720 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
721
722 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300723}
724
725static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
726{
727 if (tid == IWL_MAX_TID_COUNT)
728 return IEEE80211_AC_VO; /* MGMT */
729
730 return tid_to_mac80211_ac[tid];
731}
732
733static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
734 struct ieee80211_sta *sta, int tid)
735{
736 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
737 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
738 struct sk_buff *skb;
739 struct ieee80211_hdr *hdr;
740 struct sk_buff_head deferred_tx;
741 u8 mac_queue;
742 bool no_queue = false; /* Marks if there is a problem with the queue */
743 u8 ac;
744
745 lockdep_assert_held(&mvm->mutex);
746
747 skb = skb_peek(&tid_data->deferred_tx_frames);
748 if (!skb)
749 return;
750 hdr = (void *)skb->data;
751
752 ac = iwl_mvm_tid_to_ac_queue(tid);
753 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
754
755 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
756 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
757 IWL_ERR(mvm,
758 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
759 mvmsta->sta_id, tid);
760
761 /*
762 * Mark queue as problematic so later the deferred traffic is
763 * freed, as we can do nothing with it
764 */
765 no_queue = true;
766 }
767
768 __skb_queue_head_init(&deferred_tx);
769
Liad Kaufmand2515a92016-03-23 16:31:08 +0200770 /* Disable bottom-halves when entering TX path */
771 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +0300772 spin_lock(&mvmsta->lock);
773 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
774 spin_unlock(&mvmsta->lock);
775
Liad Kaufman24afba72015-07-28 18:56:08 +0300776 while ((skb = __skb_dequeue(&deferred_tx)))
777 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
778 ieee80211_free_txskb(mvm->hw, skb);
779 local_bh_enable();
780
781 /* Wake queue */
782 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
783}
784
785void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
786{
787 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
788 add_stream_wk);
789 struct ieee80211_sta *sta;
790 struct iwl_mvm_sta *mvmsta;
791 unsigned long deferred_tid_traffic;
792 int sta_id, tid;
793
Liad Kaufman9794c642015-08-19 17:34:28 +0300794 /* Check inactivity of queues */
795 iwl_mvm_inactivity_check(mvm);
796
Liad Kaufman24afba72015-07-28 18:56:08 +0300797 mutex_lock(&mvm->mutex);
798
799 /* Go over all stations with deferred traffic */
800 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
801 IWL_MVM_STATION_COUNT) {
802 clear_bit(sta_id, mvm->sta_deferred_frames);
803 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
804 lockdep_is_held(&mvm->mutex));
805 if (IS_ERR_OR_NULL(sta))
806 continue;
807
808 mvmsta = iwl_mvm_sta_from_mac80211(sta);
809 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
810
811 for_each_set_bit(tid, &deferred_tid_traffic,
812 IWL_MAX_TID_COUNT + 1)
813 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
814 }
815
816 mutex_unlock(&mvm->mutex);
817}
818
819static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +0300820 struct ieee80211_sta *sta,
821 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +0300822{
823 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
824 int queue;
825
Liad Kaufman9794c642015-08-19 17:34:28 +0300826 /*
827 * Check for inactive queues, so we don't reach a situation where we
828 * can't add a STA due to a shortage in queues that doesn't really exist
829 */
830 iwl_mvm_inactivity_check(mvm);
831
Liad Kaufman24afba72015-07-28 18:56:08 +0300832 spin_lock_bh(&mvm->queue_info_lock);
833
834 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300835 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
836 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +0300837 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
838 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +0300839 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
840 else
Liad Kaufman9794c642015-08-19 17:34:28 +0300841 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
842 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +0300843 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300844 if (queue < 0) {
845 spin_unlock_bh(&mvm->queue_info_lock);
846 IWL_ERR(mvm, "No available queues for new station\n");
847 return -ENOSPC;
848 }
Liad Kaufmancf961e12015-08-13 19:16:08 +0300849 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +0300850
851 spin_unlock_bh(&mvm->queue_info_lock);
852
853 mvmsta->reserved_queue = queue;
854
855 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
856 queue, mvmsta->sta_id);
857
858 return 0;
859}
860
Johannes Berg8ca151b2013-01-24 14:25:36 +0100861int iwl_mvm_add_sta(struct iwl_mvm *mvm,
862 struct ieee80211_vif *vif,
863 struct ieee80211_sta *sta)
864{
865 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100866 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +0200867 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100868 int i, ret, sta_id;
869
870 lockdep_assert_held(&mvm->mutex);
871
872 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200873 sta_id = iwl_mvm_find_free_sta_id(mvm,
874 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100875 else
876 sta_id = mvm_sta->sta_id;
877
Johannes Berg36f46312015-03-10 20:32:08 +0100878 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100879 return -ENOSPC;
880
881 spin_lock_init(&mvm_sta->lock);
882
883 mvm_sta->sta_id = sta_id;
884 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
885 mvmvif->color);
886 mvm_sta->vif = vif;
887 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300888 mvm_sta->tx_protection = 0;
889 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100890
891 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300892 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +0300893 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100894 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300895
Liad Kaufmane3118ad2016-06-05 10:49:02 +0300896 /*
897 * Allocate new queues for a TDLS station, unless we're in DQA mode,
898 * and then they'll be allocated dynamically
899 */
900 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300901 ret = iwl_mvm_tdls_sta_init(mvm, sta);
902 if (ret)
903 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300904 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300905 for (i = 0; i < IEEE80211_NUM_ACS; i++)
906 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
907 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
908 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100909
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200910 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +0300911 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200912 u16 seq = mvm_sta->tid_data[i].seq_number;
913 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
914 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +0300915
916 if (!iwl_mvm_is_dqa_supported(mvm))
917 continue;
918
919 /*
920 * Mark all queues for this STA as unallocated and defer TX
921 * frames until the queue is allocated
922 */
923 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
924 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200925 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300926 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +0300927 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100928
Sara Sharona571f5f2015-12-07 12:50:58 +0200929 if (iwl_mvm_has_new_rx_api(mvm) &&
930 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
931 dup_data = kcalloc(mvm->trans->num_rx_queues,
932 sizeof(*dup_data),
933 GFP_KERNEL);
934 if (!dup_data)
935 return -ENOMEM;
936 mvm_sta->dup_data = dup_data;
937 }
938
Liad Kaufman24afba72015-07-28 18:56:08 +0300939 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +0300940 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
941 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +0300942 if (ret)
943 goto err;
944 }
945
946 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100947 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300948 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100949
Johannes Berg9e848012014-08-04 14:33:42 +0200950 if (vif->type == NL80211_IFTYPE_STATION) {
951 if (!sta->tdls) {
952 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
953 mvmvif->ap_sta_id = sta_id;
954 } else {
955 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
956 }
957 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100958
959 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
960
961 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300962
963err:
Liad Kaufmane3118ad2016-06-05 10:49:02 +0300964 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
965 iwl_mvm_tdls_sta_deinit(mvm, sta);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300966 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100967}
968
Johannes Berg7a453972013-02-12 13:10:44 +0100969int iwl_mvm_update_sta(struct iwl_mvm *mvm,
970 struct ieee80211_vif *vif,
971 struct ieee80211_sta *sta)
972{
Liad Kaufman24afba72015-07-28 18:56:08 +0300973 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
Johannes Berg7a453972013-02-12 13:10:44 +0100974}
975
Johannes Berg8ca151b2013-01-24 14:25:36 +0100976int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
977 bool drain)
978{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300979 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100980 int ret;
981 u32 status;
982
983 lockdep_assert_held(&mvm->mutex);
984
985 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
986 cmd.sta_id = mvmsta->sta_id;
987 cmd.add_modify = STA_MODE_MODIFY;
988 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
989 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
990
991 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200992 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
993 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300994 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100995 if (ret)
996 return ret;
997
Sara Sharon837c4da2016-01-07 16:50:45 +0200998 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100999 case ADD_STA_SUCCESS:
1000 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1001 mvmsta->sta_id);
1002 break;
1003 default:
1004 ret = -EIO;
1005 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1006 mvmsta->sta_id);
1007 break;
1008 }
1009
1010 return ret;
1011}
1012
1013/*
1014 * Remove a station from the FW table. Before sending the command to remove
1015 * the station validate that the station is indeed known to the driver (sanity
1016 * only).
1017 */
1018static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1019{
1020 struct ieee80211_sta *sta;
1021 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1022 .sta_id = sta_id,
1023 };
1024 int ret;
1025
1026 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1027 lockdep_is_held(&mvm->mutex));
1028
1029 /* Note: internal stations are marked as error values */
1030 if (!sta) {
1031 IWL_ERR(mvm, "Invalid station id\n");
1032 return -EINVAL;
1033 }
1034
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001035 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001036 sizeof(rm_sta_cmd), &rm_sta_cmd);
1037 if (ret) {
1038 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1039 return ret;
1040 }
1041
1042 return 0;
1043}
1044
1045void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1046{
1047 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1048 u8 sta_id;
1049
1050 /*
1051 * The mutex is needed because of the SYNC cmd, but not only: if the
1052 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1053 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1054 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1055 * that later.
1056 */
1057 mutex_lock(&mvm->mutex);
1058
1059 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1060 int ret;
1061 struct ieee80211_sta *sta =
1062 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1063 lockdep_is_held(&mvm->mutex));
1064
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001065 /*
1066 * This station is in use or RCU-removed; the latter happens in
1067 * managed mode, where mac80211 removes the station before we
1068 * can remove it from firmware (we can only do that after the
1069 * MAC is marked unassociated), and possibly while the deauth
1070 * frame to disconnect from the AP is still queued. Then, the
1071 * station pointer is -ENOENT when the last skb is reclaimed.
1072 */
1073 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001074 continue;
1075
1076 if (PTR_ERR(sta) == -EINVAL) {
1077 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1078 sta_id);
1079 continue;
1080 }
1081
1082 if (!sta) {
1083 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1084 sta_id);
1085 continue;
1086 }
1087
1088 WARN_ON(PTR_ERR(sta) != -EBUSY);
1089 /* This station was removed and we waited until it got drained,
1090 * we can now proceed and remove it.
1091 */
1092 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1093 if (ret) {
1094 IWL_ERR(mvm,
1095 "Couldn't remove sta %d after it was drained\n",
1096 sta_id);
1097 continue;
1098 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301099 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001100 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001101
1102 if (mvm->tfd_drained[sta_id]) {
1103 unsigned long i, msk = mvm->tfd_drained[sta_id];
1104
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001105 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001106 iwl_mvm_disable_txq(mvm, i, i,
1107 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001108
1109 mvm->tfd_drained[sta_id] = 0;
1110 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1111 sta_id, msk);
1112 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001113 }
1114
1115 mutex_unlock(&mvm->mutex);
1116}
1117
Liad Kaufman24afba72015-07-28 18:56:08 +03001118static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1119 struct ieee80211_vif *vif,
1120 struct iwl_mvm_sta *mvm_sta)
1121{
1122 int ac;
1123 int i;
1124
1125 lockdep_assert_held(&mvm->mutex);
1126
1127 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1128 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
1129 continue;
1130
1131 ac = iwl_mvm_tid_to_ac_queue(i);
1132 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1133 vif->hw_queue[ac], i, 0);
1134 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1135 }
1136}
1137
Johannes Berg8ca151b2013-01-24 14:25:36 +01001138int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1139 struct ieee80211_vif *vif,
1140 struct ieee80211_sta *sta)
1141{
1142 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001143 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001144 int ret;
1145
1146 lockdep_assert_held(&mvm->mutex);
1147
Sara Sharona571f5f2015-12-07 12:50:58 +02001148 if (iwl_mvm_has_new_rx_api(mvm))
1149 kfree(mvm_sta->dup_data);
1150
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001151 if ((vif->type == NL80211_IFTYPE_STATION &&
1152 mvmvif->ap_sta_id == mvm_sta->sta_id) ||
1153 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001154 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1155 if (ret)
1156 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001157 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +03001158 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001159 if (ret)
1160 return ret;
1161 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1162 mvm_sta->tfd_queue_msk);
1163 if (ret)
1164 return ret;
1165 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001166
Liad Kaufman24afba72015-07-28 18:56:08 +03001167 /* If DQA is supported - the queues can be disabled now */
1168 if (iwl_mvm_is_dqa_supported(mvm))
1169 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
1170
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001171 if (vif->type == NL80211_IFTYPE_STATION &&
1172 mvmvif->ap_sta_id == mvm_sta->sta_id) {
1173 /* if associated - we can't remove the AP STA now */
1174 if (vif->bss_conf.assoc)
1175 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001176
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001177 /* unassoc - go ahead - remove the AP STA now */
1178 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +02001179
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001180 /* clear d0i3_ap_sta_id if no longer relevant */
1181 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
1182 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1183 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001184 }
1185
1186 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001187 * This shouldn't happen - the TDLS channel switch should be canceled
1188 * before the STA is removed.
1189 */
1190 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
1191 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1192 cancel_delayed_work(&mvm->tdls_cs.dwork);
1193 }
1194
1195 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001196 * Make sure that the tx response code sees the station as -EBUSY and
1197 * calls the drain worker.
1198 */
1199 spin_lock_bh(&mvm_sta->lock);
1200 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001201 * There are frames pending on the AC queues for this station.
1202 * We need to wait until all the frames are drained...
1203 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001204 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001205 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1206 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001207 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001208
1209 /* disable TDLS sta queues on drain complete */
1210 if (sta->tdls) {
1211 mvm->tfd_drained[mvm_sta->sta_id] =
1212 mvm_sta->tfd_queue_msk;
1213 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
1214 mvm_sta->sta_id);
1215 }
1216
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001217 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001218 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001219 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001220
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001221 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001222 iwl_mvm_tdls_sta_deinit(mvm, sta);
1223
Johannes Berg8ca151b2013-01-24 14:25:36 +01001224 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301225 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001226 }
1227
1228 return ret;
1229}
1230
1231int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1232 struct ieee80211_vif *vif,
1233 u8 sta_id)
1234{
1235 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1236
1237 lockdep_assert_held(&mvm->mutex);
1238
Monam Agarwalc531c772014-03-24 00:05:56 +05301239 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001240 return ret;
1241}
1242
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001243int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1244 struct iwl_mvm_int_sta *sta,
1245 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001246{
1247 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001248 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001249 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1250 return -ENOSPC;
1251 }
1252
1253 sta->tfd_queue_msk = qmask;
1254
1255 /* put a non-NULL value so iterating over the stations won't stop */
1256 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1257 return 0;
1258}
1259
Johannes Berg712b24a2014-08-04 14:14:14 +02001260static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1261 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001262{
Monam Agarwalc531c772014-03-24 00:05:56 +05301263 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001264 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1265 sta->sta_id = IWL_MVM_STATION_COUNT;
1266}
1267
1268static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1269 struct iwl_mvm_int_sta *sta,
1270 const u8 *addr,
1271 u16 mac_id, u16 color)
1272{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001273 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001274 int ret;
1275 u32 status;
1276
1277 lockdep_assert_held(&mvm->mutex);
1278
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001279 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001280 cmd.sta_id = sta->sta_id;
1281 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1282 color));
1283
1284 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001285 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001286
1287 if (addr)
1288 memcpy(cmd.addr, addr, ETH_ALEN);
1289
Sara Sharon854c5702016-01-26 13:17:47 +02001290 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1291 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001292 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001293 if (ret)
1294 return ret;
1295
Sara Sharon837c4da2016-01-07 16:50:45 +02001296 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001297 case ADD_STA_SUCCESS:
1298 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1299 return 0;
1300 default:
1301 ret = -EIO;
1302 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1303 status);
1304 break;
1305 }
1306 return ret;
1307}
1308
1309int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1310{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001311 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1312 mvm->cfg->base_params->wd_timeout :
1313 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001314 int ret;
1315
1316 lockdep_assert_held(&mvm->mutex);
1317
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001318 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman28d07932015-09-01 16:36:25 +03001319 if (!iwl_mvm_is_dqa_supported(mvm))
1320 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1321 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001322
1323 /* Allocate aux station and assign to it the aux queue */
1324 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +02001325 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001326 if (ret)
1327 return ret;
1328
Liad Kaufman28d07932015-09-01 16:36:25 +03001329 if (iwl_mvm_is_dqa_supported(mvm)) {
1330 struct iwl_trans_txq_scd_cfg cfg = {
1331 .fifo = IWL_MVM_TX_FIFO_MCAST,
1332 .sta_id = mvm->aux_sta.sta_id,
1333 .tid = IWL_MAX_TID_COUNT,
1334 .aggregate = false,
1335 .frame_limit = IWL_FRAME_LIMIT,
1336 };
1337
1338 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1339 wdg_timeout);
1340 }
1341
Johannes Berg8ca151b2013-01-24 14:25:36 +01001342 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1343 MAC_INDEX_AUX, 0);
1344
1345 if (ret)
1346 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1347 return ret;
1348}
1349
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001350int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1351{
1352 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1353
1354 lockdep_assert_held(&mvm->mutex);
1355 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1356 mvmvif->id, 0);
1357}
1358
1359int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1360{
1361 int ret;
1362
1363 lockdep_assert_held(&mvm->mutex);
1364
1365 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1366 if (ret)
1367 IWL_WARN(mvm, "Failed sending remove station\n");
1368
1369 return ret;
1370}
1371
1372void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1373{
1374 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1375}
1376
Johannes Berg712b24a2014-08-04 14:14:14 +02001377void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1378{
1379 lockdep_assert_held(&mvm->mutex);
1380
1381 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1382}
1383
Johannes Berg8ca151b2013-01-24 14:25:36 +01001384/*
1385 * Send the add station command for the vif's broadcast station.
1386 * Assumes that the station was already allocated.
1387 *
1388 * @mvm: the mvm component
1389 * @vif: the interface to which the broadcast station is added
1390 * @bsta: the broadcast station to add.
1391 */
Johannes Berg013290a2014-08-04 13:38:48 +02001392int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001393{
1394 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001395 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001396 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001397 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001398
1399 lockdep_assert_held(&mvm->mutex);
1400
Liad Kaufmande24f632015-08-04 15:19:18 +03001401 if (iwl_mvm_is_dqa_supported(mvm)) {
1402 struct iwl_trans_txq_scd_cfg cfg = {
1403 .fifo = IWL_MVM_TX_FIFO_VO,
1404 .sta_id = mvmvif->bcast_sta.sta_id,
1405 .tid = IWL_MAX_TID_COUNT,
1406 .aggregate = false,
1407 .frame_limit = IWL_FRAME_LIMIT,
1408 };
1409 unsigned int wdg_timeout =
1410 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1411 int queue;
1412
1413 if ((vif->type == NL80211_IFTYPE_AP) &&
1414 (mvmvif->bcast_sta.tfd_queue_msk &
1415 BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1416 queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
Liad Kaufman4c965132015-08-09 19:26:56 +03001417 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1418 (mvmvif->bcast_sta.tfd_queue_msk &
1419 BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1420 queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
Liad Kaufmande24f632015-08-04 15:19:18 +03001421 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1422 return -EINVAL;
1423
1424 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1425 wdg_timeout);
1426 }
1427
Johannes Berg5023d962013-07-31 14:07:43 +02001428 if (vif->type == NL80211_IFTYPE_ADHOC)
1429 baddr = vif->bss_conf.bssid;
1430
Johannes Berg8ca151b2013-01-24 14:25:36 +01001431 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1432 return -ENOSPC;
1433
1434 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1435 mvmvif->id, mvmvif->color);
1436}
1437
1438/* Send the FW a request to remove the station from it's internal data
1439 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001440int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001441{
Johannes Berg013290a2014-08-04 13:38:48 +02001442 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001443 int ret;
1444
1445 lockdep_assert_held(&mvm->mutex);
1446
Johannes Berg013290a2014-08-04 13:38:48 +02001447 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001448 if (ret)
1449 IWL_WARN(mvm, "Failed sending remove station\n");
1450 return ret;
1451}
1452
Johannes Berg013290a2014-08-04 13:38:48 +02001453int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1454{
1455 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03001456 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02001457
1458 lockdep_assert_held(&mvm->mutex);
1459
Liad Kaufmande24f632015-08-04 15:19:18 +03001460 if (!iwl_mvm_is_dqa_supported(mvm))
1461 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001462
Liad Kaufmande24f632015-08-04 15:19:18 +03001463 if (vif->type == NL80211_IFTYPE_AP) {
1464 /*
1465 * The firmware defines the TFD queue mask to only be relevant
1466 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1467 * be included.
1468 */
Johannes Berg013290a2014-08-04 13:38:48 +02001469 qmask &= ~BIT(vif->cab_queue);
1470
Liad Kaufmande24f632015-08-04 15:19:18 +03001471 if (iwl_mvm_is_dqa_supported(mvm))
1472 qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
Liad Kaufman4c965132015-08-09 19:26:56 +03001473 } else if (iwl_mvm_is_dqa_supported(mvm) &&
1474 vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1475 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
Liad Kaufmande24f632015-08-04 15:19:18 +03001476 }
1477
Johannes Berg013290a2014-08-04 13:38:48 +02001478 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1479 ieee80211_vif_type_p2p(vif));
1480}
1481
Johannes Berg8ca151b2013-01-24 14:25:36 +01001482/* Allocate a new station entry for the broadcast station to the given vif,
1483 * and send it to the FW.
1484 * Note that each P2P mac should have its own broadcast station.
1485 *
1486 * @mvm: the mvm component
1487 * @vif: the interface to which the broadcast station is added
1488 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02001489int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001490{
1491 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001492 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001493 int ret;
1494
1495 lockdep_assert_held(&mvm->mutex);
1496
Johannes Berg013290a2014-08-04 13:38:48 +02001497 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001498 if (ret)
1499 return ret;
1500
Johannes Berg013290a2014-08-04 13:38:48 +02001501 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001502
1503 if (ret)
1504 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001505
Johannes Berg8ca151b2013-01-24 14:25:36 +01001506 return ret;
1507}
1508
Johannes Berg013290a2014-08-04 13:38:48 +02001509void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1510{
1511 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1512
1513 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1514}
1515
Johannes Berg8ca151b2013-01-24 14:25:36 +01001516/*
1517 * Send the FW a request to remove the station from it's internal data
1518 * structures, and in addition remove it from the local data structure.
1519 */
Johannes Berg013290a2014-08-04 13:38:48 +02001520int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001521{
1522 int ret;
1523
1524 lockdep_assert_held(&mvm->mutex);
1525
Johannes Berg013290a2014-08-04 13:38:48 +02001526 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001527
Johannes Berg013290a2014-08-04 13:38:48 +02001528 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1529
Johannes Berg8ca151b2013-01-24 14:25:36 +01001530 return ret;
1531}
1532
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001533#define IWL_MAX_RX_BA_SESSIONS 16
1534
Sara Sharonb915c102016-03-23 16:32:02 +02001535static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02001536{
Sara Sharonb915c102016-03-23 16:32:02 +02001537 struct iwl_mvm_delba_notif notif = {
1538 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1539 .metadata.sync = 1,
1540 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02001541 };
Sara Sharonb915c102016-03-23 16:32:02 +02001542 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
1543};
Sara Sharon10b2b202016-03-20 16:23:41 +02001544
Sara Sharonb915c102016-03-23 16:32:02 +02001545static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1546 struct iwl_mvm_baid_data *data)
1547{
1548 int i;
1549
1550 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1551
1552 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1553 int j;
1554 struct iwl_mvm_reorder_buffer *reorder_buf =
1555 &data->reorder_buf[i];
1556
Sara Sharon06904052016-02-28 20:28:17 +02001557 spin_lock_bh(&reorder_buf->lock);
1558 if (likely(!reorder_buf->num_stored)) {
1559 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02001560 continue;
Sara Sharon06904052016-02-28 20:28:17 +02001561 }
Sara Sharonb915c102016-03-23 16:32:02 +02001562
1563 /*
1564 * This shouldn't happen in regular DELBA since the internal
1565 * delBA notification should trigger a release of all frames in
1566 * the reorder buffer.
1567 */
1568 WARN_ON(1);
1569
1570 for (j = 0; j < reorder_buf->buf_size; j++)
1571 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02001572 /*
1573 * Prevent timer re-arm. This prevents a very far fetched case
1574 * where we timed out on the notification. There may be prior
1575 * RX frames pending in the RX queue before the notification
1576 * that might get processed between now and the actual deletion
1577 * and we would re-arm the timer although we are deleting the
1578 * reorder buffer.
1579 */
1580 reorder_buf->removed = true;
1581 spin_unlock_bh(&reorder_buf->lock);
1582 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02001583 }
1584}
1585
1586static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1587 u32 sta_id,
1588 struct iwl_mvm_baid_data *data,
1589 u16 ssn, u8 buf_size)
1590{
1591 int i;
1592
1593 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1594 struct iwl_mvm_reorder_buffer *reorder_buf =
1595 &data->reorder_buf[i];
1596 int j;
1597
1598 reorder_buf->num_stored = 0;
1599 reorder_buf->head_sn = ssn;
1600 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02001601 /* rx reorder timer */
1602 reorder_buf->reorder_timer.function =
1603 iwl_mvm_reorder_timer_expired;
1604 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
1605 init_timer(&reorder_buf->reorder_timer);
1606 spin_lock_init(&reorder_buf->lock);
1607 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02001608 reorder_buf->queue = i;
1609 reorder_buf->sta_id = sta_id;
1610 for (j = 0; j < reorder_buf->buf_size; j++)
1611 __skb_queue_head_init(&reorder_buf->entries[j]);
1612 }
Sara Sharon10b2b202016-03-20 16:23:41 +02001613}
1614
Johannes Berg8ca151b2013-01-24 14:25:36 +01001615int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02001616 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001617{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001618 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001619 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02001620 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001621 int ret;
1622 u32 status;
1623
1624 lockdep_assert_held(&mvm->mutex);
1625
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001626 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1627 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1628 return -ENOSPC;
1629 }
1630
Sara Sharon10b2b202016-03-20 16:23:41 +02001631 if (iwl_mvm_has_new_rx_api(mvm) && start) {
1632 /*
1633 * Allocate here so if allocation fails we can bail out early
1634 * before starting the BA session in the firmware
1635 */
Sara Sharonb915c102016-03-23 16:32:02 +02001636 baid_data = kzalloc(sizeof(*baid_data) +
1637 mvm->trans->num_rx_queues *
1638 sizeof(baid_data->reorder_buf[0]),
1639 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02001640 if (!baid_data)
1641 return -ENOMEM;
1642 }
1643
Johannes Berg8ca151b2013-01-24 14:25:36 +01001644 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1645 cmd.sta_id = mvm_sta->sta_id;
1646 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001647 if (start) {
1648 cmd.add_immediate_ba_tid = (u8) tid;
1649 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02001650 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001651 } else {
1652 cmd.remove_immediate_ba_tid = (u8) tid;
1653 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001654 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1655 STA_MODIFY_REMOVE_BA_TID;
1656
1657 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001658 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1659 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001660 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001661 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02001662 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001663
Sara Sharon837c4da2016-01-07 16:50:45 +02001664 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001665 case ADD_STA_SUCCESS:
1666 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
1667 start ? "start" : "stopp");
1668 break;
1669 case ADD_STA_IMMEDIATE_BA_FAILURE:
1670 IWL_WARN(mvm, "RX BA Session refused by fw\n");
1671 ret = -ENOSPC;
1672 break;
1673 default:
1674 ret = -EIO;
1675 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
1676 start ? "start" : "stopp", status);
1677 break;
1678 }
1679
Sara Sharon10b2b202016-03-20 16:23:41 +02001680 if (ret)
1681 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001682
Sara Sharon10b2b202016-03-20 16:23:41 +02001683 if (start) {
1684 u8 baid;
1685
1686 mvm->rx_ba_sessions++;
1687
1688 if (!iwl_mvm_has_new_rx_api(mvm))
1689 return 0;
1690
1691 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
1692 ret = -EINVAL;
1693 goto out_free;
1694 }
1695 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
1696 IWL_ADD_STA_BAID_SHIFT);
1697 baid_data->baid = baid;
1698 baid_data->timeout = timeout;
1699 baid_data->last_rx = jiffies;
1700 init_timer(&baid_data->session_timer);
1701 baid_data->session_timer.function =
1702 iwl_mvm_rx_agg_session_expired;
1703 baid_data->session_timer.data =
1704 (unsigned long)&mvm->baid_map[baid];
1705 baid_data->mvm = mvm;
1706 baid_data->tid = tid;
1707 baid_data->sta_id = mvm_sta->sta_id;
1708
1709 mvm_sta->tid_to_baid[tid] = baid;
1710 if (timeout)
1711 mod_timer(&baid_data->session_timer,
1712 TU_TO_EXP_TIME(timeout * 2));
1713
Sara Sharonb915c102016-03-23 16:32:02 +02001714 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
1715 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02001716 /*
1717 * protect the BA data with RCU to cover a case where our
1718 * internal RX sync mechanism will timeout (not that it's
1719 * supposed to happen) and we will free the session data while
1720 * RX is being processed in parallel
1721 */
1722 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
1723 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
1724 } else if (mvm->rx_ba_sessions > 0) {
1725 u8 baid = mvm_sta->tid_to_baid[tid];
1726
1727 /* check that restart flow didn't zero the counter */
1728 mvm->rx_ba_sessions--;
1729 if (!iwl_mvm_has_new_rx_api(mvm))
1730 return 0;
1731
1732 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
1733 return -EINVAL;
1734
1735 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
1736 if (WARN_ON(!baid_data))
1737 return -EINVAL;
1738
1739 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02001740 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02001741 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02001742 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
1743 kfree_rcu(baid_data, rcu_head);
1744 }
1745 return 0;
1746
1747out_free:
1748 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001749 return ret;
1750}
1751
Liad Kaufman9794c642015-08-19 17:34:28 +03001752int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1753 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001754{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001755 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001756 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001757 int ret;
1758 u32 status;
1759
1760 lockdep_assert_held(&mvm->mutex);
1761
1762 if (start) {
1763 mvm_sta->tfd_queue_msk |= BIT(queue);
1764 mvm_sta->tid_disable_agg &= ~BIT(tid);
1765 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03001766 /* In DQA-mode the queue isn't removed on agg termination */
1767 if (!iwl_mvm_is_dqa_supported(mvm))
1768 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001769 mvm_sta->tid_disable_agg |= BIT(tid);
1770 }
1771
1772 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1773 cmd.sta_id = mvm_sta->sta_id;
1774 cmd.add_modify = STA_MODE_MODIFY;
1775 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
1776 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
1777 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
1778
1779 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001780 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1781 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001782 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001783 if (ret)
1784 return ret;
1785
Sara Sharon837c4da2016-01-07 16:50:45 +02001786 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001787 case ADD_STA_SUCCESS:
1788 break;
1789 default:
1790 ret = -EIO;
1791 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
1792 start ? "start" : "stopp", status);
1793 break;
1794 }
1795
1796 return ret;
1797}
1798
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02001799const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001800 IEEE80211_AC_BE,
1801 IEEE80211_AC_BK,
1802 IEEE80211_AC_BK,
1803 IEEE80211_AC_BE,
1804 IEEE80211_AC_VI,
1805 IEEE80211_AC_VI,
1806 IEEE80211_AC_VO,
1807 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03001808 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001809};
1810
Johannes Berg3e56ead2013-02-15 22:23:18 +01001811static const u8 tid_to_ucode_ac[] = {
1812 AC_BE,
1813 AC_BK,
1814 AC_BK,
1815 AC_BE,
1816 AC_VI,
1817 AC_VI,
1818 AC_VO,
1819 AC_VO,
1820};
1821
Johannes Berg8ca151b2013-01-24 14:25:36 +01001822int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1823 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1824{
Johannes Berg5b577a92013-11-14 18:20:04 +01001825 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001826 struct iwl_mvm_tid_data *tid_data;
1827 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001828 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001829
1830 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1831 return -EINVAL;
1832
1833 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
1834 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
1835 mvmsta->tid_data[tid].state);
1836 return -ENXIO;
1837 }
1838
1839 lockdep_assert_held(&mvm->mutex);
1840
Arik Nemtsovb2492502014-03-13 12:21:50 +02001841 spin_lock_bh(&mvmsta->lock);
1842
1843 /* possible race condition - we entered D0i3 while starting agg */
1844 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
1845 spin_unlock_bh(&mvmsta->lock);
1846 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
1847 return -EIO;
1848 }
1849
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001850 spin_lock_bh(&mvm->queue_info_lock);
1851
Liad Kaufmancf961e12015-08-13 19:16:08 +03001852 /*
1853 * Note the possible cases:
1854 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
1855 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
1856 * one and mark it as reserved
1857 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
1858 * non-DQA mode, since the TXQ hasn't yet been allocated
1859 */
1860 txq_id = mvmsta->tid_data[tid].txq_id;
1861 if (!iwl_mvm_is_dqa_supported(mvm) ||
1862 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03001863 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1864 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03001865 mvm->last_agg_queue);
1866 if (txq_id < 0) {
1867 ret = txq_id;
1868 spin_unlock_bh(&mvm->queue_info_lock);
1869 IWL_ERR(mvm, "Failed to allocate agg queue\n");
1870 goto release_locks;
1871 }
1872
1873 /* TXQ hasn't yet been enabled, so mark it only as reserved */
1874 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001875 }
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001876 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001877
Liad Kaufmancf961e12015-08-13 19:16:08 +03001878 IWL_DEBUG_TX_QUEUES(mvm,
1879 "AGG for tid %d will be on queue #%d\n",
1880 tid, txq_id);
1881
Johannes Berg8ca151b2013-01-24 14:25:36 +01001882 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01001883 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001884 tid_data->txq_id = txq_id;
1885 *ssn = tid_data->ssn;
1886
1887 IWL_DEBUG_TX_QUEUES(mvm,
1888 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1889 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1890 tid_data->next_reclaimed);
1891
1892 if (tid_data->ssn == tid_data->next_reclaimed) {
1893 tid_data->state = IWL_AGG_STARTING;
1894 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1895 } else {
1896 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1897 }
1898
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001899 ret = 0;
1900
1901release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001902 spin_unlock_bh(&mvmsta->lock);
1903
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001904 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001905}
1906
1907int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001908 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
1909 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001910{
Johannes Berg5b577a92013-11-14 18:20:04 +01001911 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001912 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001913 unsigned int wdg_timeout =
1914 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001915 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03001916 bool alloc_queue = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001917 u16 ssn;
1918
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001919 struct iwl_trans_txq_scd_cfg cfg = {
1920 .sta_id = mvmsta->sta_id,
1921 .tid = tid,
1922 .frame_limit = buf_size,
1923 .aggregate = true,
1924 };
1925
Eyal Shapiraefed6642014-09-14 15:58:53 +03001926 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1927 != IWL_MAX_TID_COUNT);
1928
Johannes Berg8ca151b2013-01-24 14:25:36 +01001929 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1930
1931 spin_lock_bh(&mvmsta->lock);
1932 ssn = tid_data->ssn;
1933 queue = tid_data->txq_id;
1934 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001935 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001936 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001937 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001938 spin_unlock_bh(&mvmsta->lock);
1939
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001940 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001941
Liad Kaufmancf961e12015-08-13 19:16:08 +03001942 /* In DQA mode, the existing queue might need to be reconfigured */
1943 if (iwl_mvm_is_dqa_supported(mvm)) {
1944 spin_lock_bh(&mvm->queue_info_lock);
1945 /* Maybe there is no need to even alloc a queue... */
1946 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
1947 alloc_queue = false;
1948 spin_unlock_bh(&mvm->queue_info_lock);
1949
1950 /*
1951 * Only reconfig the SCD for the queue if the window size has
1952 * changed from current (become smaller)
1953 */
1954 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
1955 /*
1956 * If reconfiguring an existing queue, it first must be
1957 * drained
1958 */
1959 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1960 BIT(queue));
1961 if (ret) {
1962 IWL_ERR(mvm,
1963 "Error draining queue before reconfig\n");
1964 return ret;
1965 }
1966
1967 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
1968 mvmsta->sta_id, tid,
1969 buf_size, ssn);
1970 if (ret) {
1971 IWL_ERR(mvm,
1972 "Error reconfiguring TXQ #%d\n", queue);
1973 return ret;
1974 }
1975 }
1976 }
1977
1978 if (alloc_queue)
1979 iwl_mvm_enable_txq(mvm, queue,
1980 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
1981 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001982
Johannes Berg8ca151b2013-01-24 14:25:36 +01001983 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1984 if (ret)
1985 return -EIO;
1986
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001987 /* No need to mark as reserved */
1988 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03001989 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001990 spin_unlock_bh(&mvm->queue_info_lock);
1991
Johannes Berg8ca151b2013-01-24 14:25:36 +01001992 /*
1993 * Even though in theory the peer could have different
1994 * aggregation reorder buffer sizes for different sessions,
1995 * our ucode doesn't allow for that and has a global limit
1996 * for each station. Therefore, use the minimum of all the
1997 * aggregation sessions and our default value.
1998 */
1999 mvmsta->max_agg_bufsize =
2000 min(mvmsta->max_agg_bufsize, buf_size);
2001 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2002
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002003 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2004 sta->addr, tid);
2005
Eyal Shapira9e680942013-11-09 00:16:16 +02002006 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002007}
2008
2009int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2010 struct ieee80211_sta *sta, u16 tid)
2011{
Johannes Berg5b577a92013-11-14 18:20:04 +01002012 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002013 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2014 u16 txq_id;
2015 int err;
2016
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002017
2018 /*
2019 * If mac80211 is cleaning its state, then say that we finished since
2020 * our state has been cleared anyway.
2021 */
2022 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2023 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2024 return 0;
2025 }
2026
Johannes Berg8ca151b2013-01-24 14:25:36 +01002027 spin_lock_bh(&mvmsta->lock);
2028
2029 txq_id = tid_data->txq_id;
2030
2031 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2032 mvmsta->sta_id, tid, txq_id, tid_data->state);
2033
Eyal Shapiraefed6642014-09-14 15:58:53 +03002034 mvmsta->agg_tids &= ~BIT(tid);
2035
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002036 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002037 /*
2038 * The TXQ is marked as reserved only if no traffic came through yet
2039 * This means no traffic has been sent on this TID (agg'd or not), so
2040 * we no longer have use for the queue. Since it hasn't even been
2041 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2042 * free.
2043 */
2044 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2045 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002046 spin_unlock_bh(&mvm->queue_info_lock);
2047
Johannes Berg8ca151b2013-01-24 14:25:36 +01002048 switch (tid_data->state) {
2049 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002050 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002051
2052 IWL_DEBUG_TX_QUEUES(mvm,
2053 "ssn = %d, next_recl = %d\n",
2054 tid_data->ssn, tid_data->next_reclaimed);
2055
2056 /* There are still packets for this RA / TID in the HW */
2057 if (tid_data->ssn != tid_data->next_reclaimed) {
2058 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2059 err = 0;
2060 break;
2061 }
2062
2063 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002064 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002065 spin_unlock_bh(&mvmsta->lock);
2066
2067 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2068
2069 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2070
Liad Kaufmancf961e12015-08-13 19:16:08 +03002071 if (!iwl_mvm_is_dqa_supported(mvm)) {
2072 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2073
2074 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2075 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002076 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002077 case IWL_AGG_STARTING:
2078 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2079 /*
2080 * The agg session has been stopped before it was set up. This
2081 * can happen when the AddBA timer times out for example.
2082 */
2083
2084 /* No barriers since we are under mutex */
2085 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002086
2087 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2088 tid_data->state = IWL_AGG_OFF;
2089 err = 0;
2090 break;
2091 default:
2092 IWL_ERR(mvm,
2093 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2094 mvmsta->sta_id, tid, tid_data->state);
2095 IWL_ERR(mvm,
2096 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2097 err = -EINVAL;
2098 }
2099
2100 spin_unlock_bh(&mvmsta->lock);
2101
2102 return err;
2103}
2104
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002105int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2106 struct ieee80211_sta *sta, u16 tid)
2107{
Johannes Berg5b577a92013-11-14 18:20:04 +01002108 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002109 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2110 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002111 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002112
2113 /*
2114 * First set the agg state to OFF to avoid calling
2115 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2116 */
2117 spin_lock_bh(&mvmsta->lock);
2118 txq_id = tid_data->txq_id;
2119 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2120 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002121 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002122 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002123 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002124 spin_unlock_bh(&mvmsta->lock);
2125
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002126 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002127 /*
2128 * The TXQ is marked as reserved only if no traffic came through yet
2129 * This means no traffic has been sent on this TID (agg'd or not), so
2130 * we no longer have use for the queue. Since it hasn't even been
2131 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2132 * free.
2133 */
2134 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2135 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002136 spin_unlock_bh(&mvm->queue_info_lock);
2137
Johannes Bergb6658ff2013-07-24 13:55:51 +02002138 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002139 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03002140 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02002141 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002142 iwl_trans_wait_tx_queue_empty(mvm->trans,
2143 mvmsta->tfd_queue_msk);
2144 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002145
Johannes Bergf7f89e72014-08-05 15:24:44 +02002146 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2147
Liad Kaufmancf961e12015-08-13 19:16:08 +03002148 if (!iwl_mvm_is_dqa_supported(mvm)) {
2149 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2150
2151 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2152 tid, 0);
2153 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002154 }
2155
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002156 return 0;
2157}
2158
Johannes Berg8ca151b2013-01-24 14:25:36 +01002159static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2160{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002161 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002162
2163 lockdep_assert_held(&mvm->mutex);
2164
Johannes Berg2dc2a152015-06-16 17:09:18 +02002165 /* Pick the unused key offset with the highest 'deleted'
2166 * counter. Every time a key is deleted, all the counters
2167 * are incremented and the one that was just deleted is
2168 * reset to zero. Thus, the highest counter is the one
2169 * that was deleted longest ago. Pick that one.
2170 */
2171 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2172 if (test_bit(i, mvm->fw_key_table))
2173 continue;
2174 if (mvm->fw_key_deleted[i] > max) {
2175 max = mvm->fw_key_deleted[i];
2176 max_offs = i;
2177 }
2178 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002179
Johannes Berg2dc2a152015-06-16 17:09:18 +02002180 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002181 return STA_KEY_IDX_INVALID;
2182
Johannes Berg2dc2a152015-06-16 17:09:18 +02002183 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002184}
2185
Johannes Berg5f7a1842015-12-11 09:36:10 +01002186static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2187 struct ieee80211_vif *vif,
2188 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002189{
Johannes Berg5b530e92014-12-23 16:00:17 +01002190 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002191
Johannes Berg5f7a1842015-12-11 09:36:10 +01002192 if (sta)
2193 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002194
2195 /*
2196 * The device expects GTKs for station interfaces to be
2197 * installed as GTKs for the AP station. If we have no
2198 * station ID, then use AP's station ID.
2199 */
2200 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02002201 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2202 u8 sta_id = mvmvif->ap_sta_id;
2203
Avri Altman9513c5e2015-10-19 16:29:11 +02002204 /*
2205 * It is possible that the 'sta' parameter is NULL,
2206 * for example when a GTK is removed - the sta_id will then
2207 * be the AP ID, and no station was passed by mac80211.
2208 */
Sara Sharon13303c02016-04-10 15:51:54 +03002209 return iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Avri Altman9513c5e2015-10-19 16:29:11 +02002210 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002211
Johannes Berg5f7a1842015-12-11 09:36:10 +01002212 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002213}
2214
2215static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2216 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002217 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002218 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2219 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002220{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002221 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002222 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002223 int ret;
2224 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002225 u16 keyidx;
2226 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002227 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002228
2229 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2230 STA_KEY_FLG_KEYID_MSK;
2231 key_flags = cpu_to_le16(keyidx);
2232 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2233
2234 switch (keyconf->cipher) {
2235 case WLAN_CIPHER_SUITE_TKIP:
2236 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002237 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002238 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03002239 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2240 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002241 break;
2242 case WLAN_CIPHER_SUITE_CCMP:
2243 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002244 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002245 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002246 case WLAN_CIPHER_SUITE_WEP104:
2247 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002248 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002249 case WLAN_CIPHER_SUITE_WEP40:
2250 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2251 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2252 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03002253 case WLAN_CIPHER_SUITE_GCMP_256:
2254 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2255 /* fall through */
2256 case WLAN_CIPHER_SUITE_GCMP:
2257 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
2258 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2259 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002260 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002261 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2262 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002263 }
2264
Johannes Bergba3943b2014-11-12 23:54:48 +01002265 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002266 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2267
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002268 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03002269 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002270 cmd.sta_id = sta_id;
2271
2272 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002273 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002274 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2275 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002276 else
2277 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2278 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002279
2280 switch (status) {
2281 case ADD_STA_SUCCESS:
2282 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2283 break;
2284 default:
2285 ret = -EIO;
2286 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2287 break;
2288 }
2289
2290 return ret;
2291}
2292
2293static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2294 struct ieee80211_key_conf *keyconf,
2295 u8 sta_id, bool remove_key)
2296{
2297 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2298
2299 /* verify the key details match the required command's expectations */
2300 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
2301 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2302 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
2303 return -EINVAL;
2304
2305 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2306 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2307
2308 if (remove_key) {
2309 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2310 } else {
2311 struct ieee80211_key_seq seq;
2312 const u8 *pn;
2313
2314 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002315 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2316 pn = seq.aes_cmac.pn;
2317 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2318 ((u64) pn[4] << 8) |
2319 ((u64) pn[3] << 16) |
2320 ((u64) pn[2] << 24) |
2321 ((u64) pn[1] << 32) |
2322 ((u64) pn[0] << 40));
2323 }
2324
2325 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2326 remove_key ? "removing" : "installing",
2327 igtk_cmd.sta_id);
2328
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002329 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002330 sizeof(igtk_cmd), &igtk_cmd);
2331}
2332
2333
2334static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2335 struct ieee80211_vif *vif,
2336 struct ieee80211_sta *sta)
2337{
Johannes Berg5b530e92014-12-23 16:00:17 +01002338 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002339
2340 if (sta)
2341 return sta->addr;
2342
2343 if (vif->type == NL80211_IFTYPE_STATION &&
2344 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2345 u8 sta_id = mvmvif->ap_sta_id;
2346 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2347 lockdep_is_held(&mvm->mutex));
2348 return sta->addr;
2349 }
2350
2351
2352 return NULL;
2353}
2354
Johannes Berg2f6319d2014-11-12 23:39:56 +01002355static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2356 struct ieee80211_vif *vif,
2357 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002358 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002359 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01002360 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002361{
Johannes Berg2f6319d2014-11-12 23:39:56 +01002362 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002363 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002364 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002365 struct ieee80211_key_seq seq;
2366 u16 p1k[5];
2367
Johannes Berg8ca151b2013-01-24 14:25:36 +01002368 switch (keyconf->cipher) {
2369 case WLAN_CIPHER_SUITE_TKIP:
2370 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2371 /* get phase 1 key from mac80211 */
2372 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2373 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01002374 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002375 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002376 break;
2377 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01002378 case WLAN_CIPHER_SUITE_WEP40:
2379 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03002380 case WLAN_CIPHER_SUITE_GCMP:
2381 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergba3943b2014-11-12 23:54:48 +01002382 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002383 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002384 break;
2385 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01002386 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002387 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002388 }
2389
Johannes Berg8ca151b2013-01-24 14:25:36 +01002390 return ret;
2391}
2392
Johannes Berg2f6319d2014-11-12 23:39:56 +01002393static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01002394 struct ieee80211_key_conf *keyconf,
2395 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002396{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002397 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002398 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002399 int ret;
2400 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002401
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02002402 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2403 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002404 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2405 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2406
Johannes Bergba3943b2014-11-12 23:54:48 +01002407 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002408 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2409
Max Stepanov5a258aa2013-04-07 09:11:21 +03002410 cmd.key_flags = key_flags;
2411 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002412 cmd.sta_id = sta_id;
2413
Johannes Berg8ca151b2013-01-24 14:25:36 +01002414 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002415 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2416 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002417
2418 switch (status) {
2419 case ADD_STA_SUCCESS:
2420 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2421 break;
2422 default:
2423 ret = -EIO;
2424 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2425 break;
2426 }
2427
2428 return ret;
2429}
2430
Johannes Berg2f6319d2014-11-12 23:39:56 +01002431int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2432 struct ieee80211_vif *vif,
2433 struct ieee80211_sta *sta,
2434 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002435 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002436{
Johannes Bergba3943b2014-11-12 23:54:48 +01002437 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002438 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002439 u8 sta_id;
2440 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03002441 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01002442
2443 lockdep_assert_held(&mvm->mutex);
2444
2445 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01002446 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2447 if (!mvm_sta) {
2448 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01002449 return -EINVAL;
2450 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01002451 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002452
2453 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
2454 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2455 goto end;
2456 }
2457
2458 /*
2459 * It is possible that the 'sta' parameter is NULL, and thus
2460 * there is a need to retrieve the sta from the local station table.
2461 */
2462 if (!sta) {
2463 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2464 lockdep_is_held(&mvm->mutex));
2465 if (IS_ERR_OR_NULL(sta)) {
2466 IWL_ERR(mvm, "Invalid station id\n");
2467 return -EINVAL;
2468 }
2469 }
2470
2471 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2472 return -EINVAL;
2473
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002474 /* If the key_offset is not pre-assigned, we need to find a
2475 * new offset to use. In normal cases, the offset is not
2476 * pre-assigned, but during HW_RESTART we want to reuse the
2477 * same indices, so we pass them when this function is called.
2478 *
2479 * In D3 entry, we need to hardcoded the indices (because the
2480 * firmware hardcodes the PTK offset to 0). In this case, we
2481 * need to make sure we don't overwrite the hw_key_idx in the
2482 * keyconf structure, because otherwise we cannot configure
2483 * the original ones back when resuming.
2484 */
2485 if (key_offset == STA_KEY_IDX_INVALID) {
2486 key_offset = iwl_mvm_set_fw_key_idx(mvm);
2487 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002488 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002489 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002490 }
2491
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002492 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002493 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01002494 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002495
2496 /*
2497 * For WEP, the same key is used for multicast and unicast. Upload it
2498 * again, using the same key offset, and now pointing the other one
2499 * to the same key slot (offset).
2500 * If this fails, remove the original as well.
2501 */
2502 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2503 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002504 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
2505 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01002506 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01002507 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002508 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002509 }
2510 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01002511
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002512 __set_bit(key_offset, mvm->fw_key_table);
2513
Johannes Berg2f6319d2014-11-12 23:39:56 +01002514end:
2515 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2516 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03002517 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002518 return ret;
2519}
2520
2521int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2522 struct ieee80211_vif *vif,
2523 struct ieee80211_sta *sta,
2524 struct ieee80211_key_conf *keyconf)
2525{
Johannes Bergba3943b2014-11-12 23:54:48 +01002526 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002527 struct iwl_mvm_sta *mvm_sta;
2528 u8 sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg2dc2a152015-06-16 17:09:18 +02002529 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002530
2531 lockdep_assert_held(&mvm->mutex);
2532
Johannes Berg5f7a1842015-12-11 09:36:10 +01002533 /* Get the station from the mvm local station table */
2534 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002535
2536 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2537 keyconf->keyidx, sta_id);
2538
2539 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
2540 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2541
2542 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2543 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2544 keyconf->hw_key_idx);
2545 return -ENOENT;
2546 }
2547
Johannes Berg2dc2a152015-06-16 17:09:18 +02002548 /* track which key was deleted last */
2549 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2550 if (mvm->fw_key_deleted[i] < U8_MAX)
2551 mvm->fw_key_deleted[i]++;
2552 }
2553 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2554
Johannes Berg5f7a1842015-12-11 09:36:10 +01002555 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01002556 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2557 return 0;
2558 }
2559
Johannes Berg5f7a1842015-12-11 09:36:10 +01002560 sta_id = mvm_sta->sta_id;
2561
Johannes Bergba3943b2014-11-12 23:54:48 +01002562 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2563 if (ret)
2564 return ret;
2565
2566 /* delete WEP key twice to get rid of (now useless) offset */
2567 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2568 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2569 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2570
2571 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002572}
2573
Johannes Berg8ca151b2013-01-24 14:25:36 +01002574void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2575 struct ieee80211_vif *vif,
2576 struct ieee80211_key_conf *keyconf,
2577 struct ieee80211_sta *sta, u32 iv32,
2578 u16 *phase1key)
2579{
Beni Levc3eb5362013-02-06 17:22:18 +02002580 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01002581 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002582
Beni Levc3eb5362013-02-06 17:22:18 +02002583 rcu_read_lock();
2584
Johannes Berg5f7a1842015-12-11 09:36:10 +01002585 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2586 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002587 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01002588 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002589 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002590
2591 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02002592 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01002593}
2594
Johannes Berg9cc40712013-02-15 22:47:48 +01002595void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
2596 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002597{
Johannes Berg5b577a92013-11-14 18:20:04 +01002598 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002599 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002600 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002601 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03002602 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01002603 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002604 };
2605 int ret;
2606
Sara Sharon854c5702016-01-26 13:17:47 +02002607 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2608 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002609 if (ret)
2610 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2611}
2612
Johannes Berg9cc40712013-02-15 22:47:48 +01002613void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
2614 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002615 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01002616 u16 cnt, u16 tids, bool more_data,
2617 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002618{
Johannes Berg5b577a92013-11-14 18:20:04 +01002619 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002620 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002621 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002622 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002623 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
2624 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01002625 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002626 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01002627 int tid, ret;
2628 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002629
Johannes Berg3e56ead2013-02-15 22:23:18 +01002630 /* convert TIDs to ACs - we don't support TSPEC so that's OK
2631 * Note that this field is reserved and unused by firmware not
2632 * supporting GO uAPSD, so it's safe to always do this.
2633 */
2634 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
2635 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
2636
2637 /* If we're releasing frames from aggregation queues then check if the
2638 * all queues combined that we're releasing frames from have
2639 * - more frames than the service period, in which case more_data
2640 * needs to be set
2641 * - fewer than 'cnt' frames, in which case we need to adjust the
2642 * firmware command (but do that unconditionally)
2643 */
2644 if (agg) {
2645 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002646 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002647
2648 spin_lock_bh(&mvmsta->lock);
2649 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
2650 struct iwl_mvm_tid_data *tid_data;
2651 u16 n_queued;
2652
2653 tid_data = &mvmsta->tid_data[tid];
2654 if (WARN(tid_data->state != IWL_AGG_ON &&
2655 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
2656 "TID %d state is %d\n",
2657 tid, tid_data->state)) {
2658 spin_unlock_bh(&mvmsta->lock);
2659 ieee80211_sta_eosp(sta);
2660 return;
2661 }
2662
2663 n_queued = iwl_mvm_tid_queued(tid_data);
2664 if (n_queued > remaining) {
2665 more_data = true;
2666 remaining = 0;
2667 break;
2668 }
2669 remaining -= n_queued;
2670 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002671 sleep_tx_count = cnt - remaining;
2672 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
2673 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002674 spin_unlock_bh(&mvmsta->lock);
2675
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002676 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01002677 if (WARN_ON(cnt - remaining == 0)) {
2678 ieee80211_sta_eosp(sta);
2679 return;
2680 }
2681 }
2682
2683 /* Note: this is ignored by firmware not supporting GO uAPSD */
2684 if (more_data)
2685 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
2686
2687 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
2688 mvmsta->next_status_eosp = true;
2689 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
2690 } else {
2691 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
2692 }
2693
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02002694 /* block the Tx queues until the FW updated the sleep Tx count */
2695 iwl_trans_block_txq_ptrs(mvm->trans, true);
2696
2697 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
2698 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02002699 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002700 if (ret)
2701 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2702}
Johannes Berg3e56ead2013-02-15 22:23:18 +01002703
Johannes Berg04168412015-06-23 21:22:09 +02002704void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
2705 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01002706{
2707 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2708 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
2709 struct ieee80211_sta *sta;
2710 u32 sta_id = le32_to_cpu(notif->sta_id);
2711
2712 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02002713 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002714
2715 rcu_read_lock();
2716 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2717 if (!IS_ERR_OR_NULL(sta))
2718 ieee80211_sta_eosp(sta);
2719 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01002720}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002721
2722void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
2723 struct iwl_mvm_sta *mvmsta, bool disable)
2724{
2725 struct iwl_mvm_add_sta_cmd cmd = {
2726 .add_modify = STA_MODE_MODIFY,
2727 .sta_id = mvmsta->sta_id,
2728 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
2729 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
2730 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2731 };
2732 int ret;
2733
Sara Sharon854c5702016-01-26 13:17:47 +02002734 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2735 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002736 if (ret)
2737 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2738}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002739
2740void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
2741 struct ieee80211_sta *sta,
2742 bool disable)
2743{
2744 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2745
2746 spin_lock_bh(&mvm_sta->lock);
2747
2748 if (mvm_sta->disable_tx == disable) {
2749 spin_unlock_bh(&mvm_sta->lock);
2750 return;
2751 }
2752
2753 mvm_sta->disable_tx = disable;
2754
2755 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03002756 * Tell mac80211 to start/stop queuing tx for this station,
2757 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002758 * for this station.
2759 */
2760 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
2761 ieee80211_sta_block_awake(mvm->hw, sta, disable);
2762
2763 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
2764
2765 spin_unlock_bh(&mvm_sta->lock);
2766}
2767
2768void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
2769 struct iwl_mvm_vif *mvmvif,
2770 bool disable)
2771{
2772 struct ieee80211_sta *sta;
2773 struct iwl_mvm_sta *mvm_sta;
2774 int i;
2775
2776 lockdep_assert_held(&mvm->mutex);
2777
2778 /* Block/unblock all the stations of the given mvmvif */
2779 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
2780 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
2781 lockdep_is_held(&mvm->mutex));
2782 if (IS_ERR_OR_NULL(sta))
2783 continue;
2784
2785 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2786 if (mvm_sta->mac_id_n_color !=
2787 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
2788 continue;
2789
2790 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
2791 }
2792}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02002793
2794void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2795{
2796 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2797 struct iwl_mvm_sta *mvmsta;
2798
2799 rcu_read_lock();
2800
2801 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
2802
2803 if (!WARN_ON(!mvmsta))
2804 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2805
2806 rcu_read_unlock();
2807}