blob: cf0681765ec4e4dfe5c041ba3cb0a9913c8b976d [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
896 /* allocate new queues for a TDLS station */
897 if (sta->tdls) {
898 ret = iwl_mvm_tdls_sta_init(mvm, sta);
899 if (ret)
900 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300901 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300902 for (i = 0; i < IEEE80211_NUM_ACS; i++)
903 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
904 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
905 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100906
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200907 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +0300908 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200909 u16 seq = mvm_sta->tid_data[i].seq_number;
910 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
911 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +0300912
913 if (!iwl_mvm_is_dqa_supported(mvm))
914 continue;
915
916 /*
917 * Mark all queues for this STA as unallocated and defer TX
918 * frames until the queue is allocated
919 */
920 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
921 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200922 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300923 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +0300924 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100925
Sara Sharona571f5f2015-12-07 12:50:58 +0200926 if (iwl_mvm_has_new_rx_api(mvm) &&
927 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
928 dup_data = kcalloc(mvm->trans->num_rx_queues,
929 sizeof(*dup_data),
930 GFP_KERNEL);
931 if (!dup_data)
932 return -ENOMEM;
933 mvm_sta->dup_data = dup_data;
934 }
935
Liad Kaufman24afba72015-07-28 18:56:08 +0300936 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +0300937 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
938 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +0300939 if (ret)
940 goto err;
941 }
942
943 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100944 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300945 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100946
Johannes Berg9e848012014-08-04 14:33:42 +0200947 if (vif->type == NL80211_IFTYPE_STATION) {
948 if (!sta->tdls) {
949 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
950 mvmvif->ap_sta_id = sta_id;
951 } else {
952 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
953 }
954 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100955
956 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
957
958 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300959
960err:
961 iwl_mvm_tdls_sta_deinit(mvm, sta);
962 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100963}
964
Johannes Berg7a453972013-02-12 13:10:44 +0100965int iwl_mvm_update_sta(struct iwl_mvm *mvm,
966 struct ieee80211_vif *vif,
967 struct ieee80211_sta *sta)
968{
Liad Kaufman24afba72015-07-28 18:56:08 +0300969 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
Johannes Berg7a453972013-02-12 13:10:44 +0100970}
971
Johannes Berg8ca151b2013-01-24 14:25:36 +0100972int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
973 bool drain)
974{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300975 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100976 int ret;
977 u32 status;
978
979 lockdep_assert_held(&mvm->mutex);
980
981 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
982 cmd.sta_id = mvmsta->sta_id;
983 cmd.add_modify = STA_MODE_MODIFY;
984 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
985 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
986
987 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200988 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
989 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300990 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100991 if (ret)
992 return ret;
993
Sara Sharon837c4da2016-01-07 16:50:45 +0200994 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100995 case ADD_STA_SUCCESS:
996 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
997 mvmsta->sta_id);
998 break;
999 default:
1000 ret = -EIO;
1001 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1002 mvmsta->sta_id);
1003 break;
1004 }
1005
1006 return ret;
1007}
1008
1009/*
1010 * Remove a station from the FW table. Before sending the command to remove
1011 * the station validate that the station is indeed known to the driver (sanity
1012 * only).
1013 */
1014static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1015{
1016 struct ieee80211_sta *sta;
1017 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1018 .sta_id = sta_id,
1019 };
1020 int ret;
1021
1022 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1023 lockdep_is_held(&mvm->mutex));
1024
1025 /* Note: internal stations are marked as error values */
1026 if (!sta) {
1027 IWL_ERR(mvm, "Invalid station id\n");
1028 return -EINVAL;
1029 }
1030
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001031 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001032 sizeof(rm_sta_cmd), &rm_sta_cmd);
1033 if (ret) {
1034 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1035 return ret;
1036 }
1037
1038 return 0;
1039}
1040
1041void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1042{
1043 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1044 u8 sta_id;
1045
1046 /*
1047 * The mutex is needed because of the SYNC cmd, but not only: if the
1048 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1049 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1050 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1051 * that later.
1052 */
1053 mutex_lock(&mvm->mutex);
1054
1055 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1056 int ret;
1057 struct ieee80211_sta *sta =
1058 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1059 lockdep_is_held(&mvm->mutex));
1060
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001061 /*
1062 * This station is in use or RCU-removed; the latter happens in
1063 * managed mode, where mac80211 removes the station before we
1064 * can remove it from firmware (we can only do that after the
1065 * MAC is marked unassociated), and possibly while the deauth
1066 * frame to disconnect from the AP is still queued. Then, the
1067 * station pointer is -ENOENT when the last skb is reclaimed.
1068 */
1069 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001070 continue;
1071
1072 if (PTR_ERR(sta) == -EINVAL) {
1073 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1074 sta_id);
1075 continue;
1076 }
1077
1078 if (!sta) {
1079 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1080 sta_id);
1081 continue;
1082 }
1083
1084 WARN_ON(PTR_ERR(sta) != -EBUSY);
1085 /* This station was removed and we waited until it got drained,
1086 * we can now proceed and remove it.
1087 */
1088 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1089 if (ret) {
1090 IWL_ERR(mvm,
1091 "Couldn't remove sta %d after it was drained\n",
1092 sta_id);
1093 continue;
1094 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301095 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001096 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001097
1098 if (mvm->tfd_drained[sta_id]) {
1099 unsigned long i, msk = mvm->tfd_drained[sta_id];
1100
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001101 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001102 iwl_mvm_disable_txq(mvm, i, i,
1103 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001104
1105 mvm->tfd_drained[sta_id] = 0;
1106 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1107 sta_id, msk);
1108 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001109 }
1110
1111 mutex_unlock(&mvm->mutex);
1112}
1113
Liad Kaufman24afba72015-07-28 18:56:08 +03001114static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1115 struct ieee80211_vif *vif,
1116 struct iwl_mvm_sta *mvm_sta)
1117{
1118 int ac;
1119 int i;
1120
1121 lockdep_assert_held(&mvm->mutex);
1122
1123 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1124 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
1125 continue;
1126
1127 ac = iwl_mvm_tid_to_ac_queue(i);
1128 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1129 vif->hw_queue[ac], i, 0);
1130 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1131 }
1132}
1133
Johannes Berg8ca151b2013-01-24 14:25:36 +01001134int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1135 struct ieee80211_vif *vif,
1136 struct ieee80211_sta *sta)
1137{
1138 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001139 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001140 int ret;
1141
1142 lockdep_assert_held(&mvm->mutex);
1143
Sara Sharona571f5f2015-12-07 12:50:58 +02001144 if (iwl_mvm_has_new_rx_api(mvm))
1145 kfree(mvm_sta->dup_data);
1146
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001147 if ((vif->type == NL80211_IFTYPE_STATION &&
1148 mvmvif->ap_sta_id == mvm_sta->sta_id) ||
1149 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001150 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1151 if (ret)
1152 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001153 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +03001154 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001155 if (ret)
1156 return ret;
1157 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1158 mvm_sta->tfd_queue_msk);
1159 if (ret)
1160 return ret;
1161 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001162
Liad Kaufman24afba72015-07-28 18:56:08 +03001163 /* If DQA is supported - the queues can be disabled now */
1164 if (iwl_mvm_is_dqa_supported(mvm))
1165 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
1166
Johannes Berg8ca151b2013-01-24 14:25:36 +01001167 /* if we are associated - we can't remove the AP STA now */
1168 if (vif->bss_conf.assoc)
1169 return ret;
1170
1171 /* unassoc - go ahead - remove the AP STA now */
1172 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +02001173
1174 /* clear d0i3_ap_sta_id if no longer relevant */
1175 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
1176 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001177 }
1178
1179 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001180 * This shouldn't happen - the TDLS channel switch should be canceled
1181 * before the STA is removed.
1182 */
1183 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
1184 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1185 cancel_delayed_work(&mvm->tdls_cs.dwork);
1186 }
1187
1188 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001189 * Make sure that the tx response code sees the station as -EBUSY and
1190 * calls the drain worker.
1191 */
1192 spin_lock_bh(&mvm_sta->lock);
1193 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001194 * There are frames pending on the AC queues for this station.
1195 * We need to wait until all the frames are drained...
1196 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001197 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001198 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1199 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001200 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001201
1202 /* disable TDLS sta queues on drain complete */
1203 if (sta->tdls) {
1204 mvm->tfd_drained[mvm_sta->sta_id] =
1205 mvm_sta->tfd_queue_msk;
1206 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
1207 mvm_sta->sta_id);
1208 }
1209
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001210 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001211 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001212 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001213
1214 if (sta->tdls)
1215 iwl_mvm_tdls_sta_deinit(mvm, sta);
1216
Johannes Berg8ca151b2013-01-24 14:25:36 +01001217 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301218 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001219 }
1220
1221 return ret;
1222}
1223
1224int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1225 struct ieee80211_vif *vif,
1226 u8 sta_id)
1227{
1228 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1229
1230 lockdep_assert_held(&mvm->mutex);
1231
Monam Agarwalc531c772014-03-24 00:05:56 +05301232 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001233 return ret;
1234}
1235
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001236int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1237 struct iwl_mvm_int_sta *sta,
1238 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239{
1240 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001241 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001242 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1243 return -ENOSPC;
1244 }
1245
1246 sta->tfd_queue_msk = qmask;
1247
1248 /* put a non-NULL value so iterating over the stations won't stop */
1249 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1250 return 0;
1251}
1252
Johannes Berg712b24a2014-08-04 14:14:14 +02001253static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1254 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001255{
Monam Agarwalc531c772014-03-24 00:05:56 +05301256 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001257 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1258 sta->sta_id = IWL_MVM_STATION_COUNT;
1259}
1260
1261static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1262 struct iwl_mvm_int_sta *sta,
1263 const u8 *addr,
1264 u16 mac_id, u16 color)
1265{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001266 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001267 int ret;
1268 u32 status;
1269
1270 lockdep_assert_held(&mvm->mutex);
1271
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001272 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001273 cmd.sta_id = sta->sta_id;
1274 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1275 color));
1276
1277 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001278 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001279
1280 if (addr)
1281 memcpy(cmd.addr, addr, ETH_ALEN);
1282
Sara Sharon854c5702016-01-26 13:17:47 +02001283 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1284 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001285 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001286 if (ret)
1287 return ret;
1288
Sara Sharon837c4da2016-01-07 16:50:45 +02001289 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001290 case ADD_STA_SUCCESS:
1291 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1292 return 0;
1293 default:
1294 ret = -EIO;
1295 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1296 status);
1297 break;
1298 }
1299 return ret;
1300}
1301
1302int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1303{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001304 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1305 mvm->cfg->base_params->wd_timeout :
1306 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001307 int ret;
1308
1309 lockdep_assert_held(&mvm->mutex);
1310
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001311 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman28d07932015-09-01 16:36:25 +03001312 if (!iwl_mvm_is_dqa_supported(mvm))
1313 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1314 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001315
1316 /* Allocate aux station and assign to it the aux queue */
1317 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +02001318 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001319 if (ret)
1320 return ret;
1321
Liad Kaufman28d07932015-09-01 16:36:25 +03001322 if (iwl_mvm_is_dqa_supported(mvm)) {
1323 struct iwl_trans_txq_scd_cfg cfg = {
1324 .fifo = IWL_MVM_TX_FIFO_MCAST,
1325 .sta_id = mvm->aux_sta.sta_id,
1326 .tid = IWL_MAX_TID_COUNT,
1327 .aggregate = false,
1328 .frame_limit = IWL_FRAME_LIMIT,
1329 };
1330
1331 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1332 wdg_timeout);
1333 }
1334
Johannes Berg8ca151b2013-01-24 14:25:36 +01001335 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1336 MAC_INDEX_AUX, 0);
1337
1338 if (ret)
1339 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1340 return ret;
1341}
1342
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001343int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1344{
1345 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1346
1347 lockdep_assert_held(&mvm->mutex);
1348 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1349 mvmvif->id, 0);
1350}
1351
1352int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1353{
1354 int ret;
1355
1356 lockdep_assert_held(&mvm->mutex);
1357
1358 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1359 if (ret)
1360 IWL_WARN(mvm, "Failed sending remove station\n");
1361
1362 return ret;
1363}
1364
1365void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1366{
1367 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1368}
1369
Johannes Berg712b24a2014-08-04 14:14:14 +02001370void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1371{
1372 lockdep_assert_held(&mvm->mutex);
1373
1374 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1375}
1376
Johannes Berg8ca151b2013-01-24 14:25:36 +01001377/*
1378 * Send the add station command for the vif's broadcast station.
1379 * Assumes that the station was already allocated.
1380 *
1381 * @mvm: the mvm component
1382 * @vif: the interface to which the broadcast station is added
1383 * @bsta: the broadcast station to add.
1384 */
Johannes Berg013290a2014-08-04 13:38:48 +02001385int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001386{
1387 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001388 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001389 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001390 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001391
1392 lockdep_assert_held(&mvm->mutex);
1393
Liad Kaufmande24f632015-08-04 15:19:18 +03001394 if (iwl_mvm_is_dqa_supported(mvm)) {
1395 struct iwl_trans_txq_scd_cfg cfg = {
1396 .fifo = IWL_MVM_TX_FIFO_VO,
1397 .sta_id = mvmvif->bcast_sta.sta_id,
1398 .tid = IWL_MAX_TID_COUNT,
1399 .aggregate = false,
1400 .frame_limit = IWL_FRAME_LIMIT,
1401 };
1402 unsigned int wdg_timeout =
1403 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1404 int queue;
1405
1406 if ((vif->type == NL80211_IFTYPE_AP) &&
1407 (mvmvif->bcast_sta.tfd_queue_msk &
1408 BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1409 queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
Liad Kaufman4c965132015-08-09 19:26:56 +03001410 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1411 (mvmvif->bcast_sta.tfd_queue_msk &
1412 BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1413 queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
Liad Kaufmande24f632015-08-04 15:19:18 +03001414 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1415 return -EINVAL;
1416
1417 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1418 wdg_timeout);
1419 }
1420
Johannes Berg5023d962013-07-31 14:07:43 +02001421 if (vif->type == NL80211_IFTYPE_ADHOC)
1422 baddr = vif->bss_conf.bssid;
1423
Johannes Berg8ca151b2013-01-24 14:25:36 +01001424 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1425 return -ENOSPC;
1426
1427 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1428 mvmvif->id, mvmvif->color);
1429}
1430
1431/* Send the FW a request to remove the station from it's internal data
1432 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001433int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001434{
Johannes Berg013290a2014-08-04 13:38:48 +02001435 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001436 int ret;
1437
1438 lockdep_assert_held(&mvm->mutex);
1439
Johannes Berg013290a2014-08-04 13:38:48 +02001440 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001441 if (ret)
1442 IWL_WARN(mvm, "Failed sending remove station\n");
1443 return ret;
1444}
1445
Johannes Berg013290a2014-08-04 13:38:48 +02001446int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1447{
1448 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03001449 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02001450
1451 lockdep_assert_held(&mvm->mutex);
1452
Liad Kaufmande24f632015-08-04 15:19:18 +03001453 if (!iwl_mvm_is_dqa_supported(mvm))
1454 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001455
Liad Kaufmande24f632015-08-04 15:19:18 +03001456 if (vif->type == NL80211_IFTYPE_AP) {
1457 /*
1458 * The firmware defines the TFD queue mask to only be relevant
1459 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1460 * be included.
1461 */
Johannes Berg013290a2014-08-04 13:38:48 +02001462 qmask &= ~BIT(vif->cab_queue);
1463
Liad Kaufmande24f632015-08-04 15:19:18 +03001464 if (iwl_mvm_is_dqa_supported(mvm))
1465 qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
Liad Kaufman4c965132015-08-09 19:26:56 +03001466 } else if (iwl_mvm_is_dqa_supported(mvm) &&
1467 vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1468 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
Liad Kaufmande24f632015-08-04 15:19:18 +03001469 }
1470
Johannes Berg013290a2014-08-04 13:38:48 +02001471 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1472 ieee80211_vif_type_p2p(vif));
1473}
1474
Johannes Berg8ca151b2013-01-24 14:25:36 +01001475/* Allocate a new station entry for the broadcast station to the given vif,
1476 * and send it to the FW.
1477 * Note that each P2P mac should have its own broadcast station.
1478 *
1479 * @mvm: the mvm component
1480 * @vif: the interface to which the broadcast station is added
1481 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02001482int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001483{
1484 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001485 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001486 int ret;
1487
1488 lockdep_assert_held(&mvm->mutex);
1489
Johannes Berg013290a2014-08-04 13:38:48 +02001490 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001491 if (ret)
1492 return ret;
1493
Johannes Berg013290a2014-08-04 13:38:48 +02001494 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001495
1496 if (ret)
1497 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001498
Johannes Berg8ca151b2013-01-24 14:25:36 +01001499 return ret;
1500}
1501
Johannes Berg013290a2014-08-04 13:38:48 +02001502void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1503{
1504 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1505
1506 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1507}
1508
Johannes Berg8ca151b2013-01-24 14:25:36 +01001509/*
1510 * Send the FW a request to remove the station from it's internal data
1511 * structures, and in addition remove it from the local data structure.
1512 */
Johannes Berg013290a2014-08-04 13:38:48 +02001513int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001514{
1515 int ret;
1516
1517 lockdep_assert_held(&mvm->mutex);
1518
Johannes Berg013290a2014-08-04 13:38:48 +02001519 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001520
Johannes Berg013290a2014-08-04 13:38:48 +02001521 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1522
Johannes Berg8ca151b2013-01-24 14:25:36 +01001523 return ret;
1524}
1525
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001526#define IWL_MAX_RX_BA_SESSIONS 16
1527
Sara Sharonb915c102016-03-23 16:32:02 +02001528static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02001529{
Sara Sharonb915c102016-03-23 16:32:02 +02001530 struct iwl_mvm_delba_notif notif = {
1531 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1532 .metadata.sync = 1,
1533 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02001534 };
Sara Sharonb915c102016-03-23 16:32:02 +02001535 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
1536};
Sara Sharon10b2b202016-03-20 16:23:41 +02001537
Sara Sharonb915c102016-03-23 16:32:02 +02001538static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1539 struct iwl_mvm_baid_data *data)
1540{
1541 int i;
1542
1543 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1544
1545 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1546 int j;
1547 struct iwl_mvm_reorder_buffer *reorder_buf =
1548 &data->reorder_buf[i];
1549
Sara Sharon06904052016-02-28 20:28:17 +02001550 spin_lock_bh(&reorder_buf->lock);
1551 if (likely(!reorder_buf->num_stored)) {
1552 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02001553 continue;
Sara Sharon06904052016-02-28 20:28:17 +02001554 }
Sara Sharonb915c102016-03-23 16:32:02 +02001555
1556 /*
1557 * This shouldn't happen in regular DELBA since the internal
1558 * delBA notification should trigger a release of all frames in
1559 * the reorder buffer.
1560 */
1561 WARN_ON(1);
1562
1563 for (j = 0; j < reorder_buf->buf_size; j++)
1564 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02001565 /*
1566 * Prevent timer re-arm. This prevents a very far fetched case
1567 * where we timed out on the notification. There may be prior
1568 * RX frames pending in the RX queue before the notification
1569 * that might get processed between now and the actual deletion
1570 * and we would re-arm the timer although we are deleting the
1571 * reorder buffer.
1572 */
1573 reorder_buf->removed = true;
1574 spin_unlock_bh(&reorder_buf->lock);
1575 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02001576 }
1577}
1578
1579static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1580 u32 sta_id,
1581 struct iwl_mvm_baid_data *data,
1582 u16 ssn, u8 buf_size)
1583{
1584 int i;
1585
1586 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1587 struct iwl_mvm_reorder_buffer *reorder_buf =
1588 &data->reorder_buf[i];
1589 int j;
1590
1591 reorder_buf->num_stored = 0;
1592 reorder_buf->head_sn = ssn;
1593 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02001594 /* rx reorder timer */
1595 reorder_buf->reorder_timer.function =
1596 iwl_mvm_reorder_timer_expired;
1597 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
1598 init_timer(&reorder_buf->reorder_timer);
1599 spin_lock_init(&reorder_buf->lock);
1600 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02001601 reorder_buf->queue = i;
1602 reorder_buf->sta_id = sta_id;
1603 for (j = 0; j < reorder_buf->buf_size; j++)
1604 __skb_queue_head_init(&reorder_buf->entries[j]);
1605 }
Sara Sharon10b2b202016-03-20 16:23:41 +02001606}
1607
Johannes Berg8ca151b2013-01-24 14:25:36 +01001608int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02001609 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001610{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001611 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001612 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02001613 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001614 int ret;
1615 u32 status;
1616
1617 lockdep_assert_held(&mvm->mutex);
1618
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001619 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1620 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1621 return -ENOSPC;
1622 }
1623
Sara Sharon10b2b202016-03-20 16:23:41 +02001624 if (iwl_mvm_has_new_rx_api(mvm) && start) {
1625 /*
1626 * Allocate here so if allocation fails we can bail out early
1627 * before starting the BA session in the firmware
1628 */
Sara Sharonb915c102016-03-23 16:32:02 +02001629 baid_data = kzalloc(sizeof(*baid_data) +
1630 mvm->trans->num_rx_queues *
1631 sizeof(baid_data->reorder_buf[0]),
1632 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02001633 if (!baid_data)
1634 return -ENOMEM;
1635 }
1636
Johannes Berg8ca151b2013-01-24 14:25:36 +01001637 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1638 cmd.sta_id = mvm_sta->sta_id;
1639 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001640 if (start) {
1641 cmd.add_immediate_ba_tid = (u8) tid;
1642 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02001643 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001644 } else {
1645 cmd.remove_immediate_ba_tid = (u8) tid;
1646 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001647 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1648 STA_MODIFY_REMOVE_BA_TID;
1649
1650 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001651 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1652 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001653 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001654 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02001655 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001656
Sara Sharon837c4da2016-01-07 16:50:45 +02001657 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001658 case ADD_STA_SUCCESS:
1659 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
1660 start ? "start" : "stopp");
1661 break;
1662 case ADD_STA_IMMEDIATE_BA_FAILURE:
1663 IWL_WARN(mvm, "RX BA Session refused by fw\n");
1664 ret = -ENOSPC;
1665 break;
1666 default:
1667 ret = -EIO;
1668 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
1669 start ? "start" : "stopp", status);
1670 break;
1671 }
1672
Sara Sharon10b2b202016-03-20 16:23:41 +02001673 if (ret)
1674 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001675
Sara Sharon10b2b202016-03-20 16:23:41 +02001676 if (start) {
1677 u8 baid;
1678
1679 mvm->rx_ba_sessions++;
1680
1681 if (!iwl_mvm_has_new_rx_api(mvm))
1682 return 0;
1683
1684 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
1685 ret = -EINVAL;
1686 goto out_free;
1687 }
1688 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
1689 IWL_ADD_STA_BAID_SHIFT);
1690 baid_data->baid = baid;
1691 baid_data->timeout = timeout;
1692 baid_data->last_rx = jiffies;
1693 init_timer(&baid_data->session_timer);
1694 baid_data->session_timer.function =
1695 iwl_mvm_rx_agg_session_expired;
1696 baid_data->session_timer.data =
1697 (unsigned long)&mvm->baid_map[baid];
1698 baid_data->mvm = mvm;
1699 baid_data->tid = tid;
1700 baid_data->sta_id = mvm_sta->sta_id;
1701
1702 mvm_sta->tid_to_baid[tid] = baid;
1703 if (timeout)
1704 mod_timer(&baid_data->session_timer,
1705 TU_TO_EXP_TIME(timeout * 2));
1706
Sara Sharonb915c102016-03-23 16:32:02 +02001707 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
1708 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02001709 /*
1710 * protect the BA data with RCU to cover a case where our
1711 * internal RX sync mechanism will timeout (not that it's
1712 * supposed to happen) and we will free the session data while
1713 * RX is being processed in parallel
1714 */
1715 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
1716 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
1717 } else if (mvm->rx_ba_sessions > 0) {
1718 u8 baid = mvm_sta->tid_to_baid[tid];
1719
1720 /* check that restart flow didn't zero the counter */
1721 mvm->rx_ba_sessions--;
1722 if (!iwl_mvm_has_new_rx_api(mvm))
1723 return 0;
1724
1725 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
1726 return -EINVAL;
1727
1728 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
1729 if (WARN_ON(!baid_data))
1730 return -EINVAL;
1731
1732 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02001733 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02001734 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02001735 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
1736 kfree_rcu(baid_data, rcu_head);
1737 }
1738 return 0;
1739
1740out_free:
1741 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001742 return ret;
1743}
1744
Liad Kaufman9794c642015-08-19 17:34:28 +03001745int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1746 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001747{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001748 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001749 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001750 int ret;
1751 u32 status;
1752
1753 lockdep_assert_held(&mvm->mutex);
1754
1755 if (start) {
1756 mvm_sta->tfd_queue_msk |= BIT(queue);
1757 mvm_sta->tid_disable_agg &= ~BIT(tid);
1758 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03001759 /* In DQA-mode the queue isn't removed on agg termination */
1760 if (!iwl_mvm_is_dqa_supported(mvm))
1761 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001762 mvm_sta->tid_disable_agg |= BIT(tid);
1763 }
1764
1765 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1766 cmd.sta_id = mvm_sta->sta_id;
1767 cmd.add_modify = STA_MODE_MODIFY;
1768 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
1769 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
1770 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
1771
1772 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001773 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1774 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001775 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001776 if (ret)
1777 return ret;
1778
Sara Sharon837c4da2016-01-07 16:50:45 +02001779 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001780 case ADD_STA_SUCCESS:
1781 break;
1782 default:
1783 ret = -EIO;
1784 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
1785 start ? "start" : "stopp", status);
1786 break;
1787 }
1788
1789 return ret;
1790}
1791
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02001792const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001793 IEEE80211_AC_BE,
1794 IEEE80211_AC_BK,
1795 IEEE80211_AC_BK,
1796 IEEE80211_AC_BE,
1797 IEEE80211_AC_VI,
1798 IEEE80211_AC_VI,
1799 IEEE80211_AC_VO,
1800 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03001801 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001802};
1803
Johannes Berg3e56ead2013-02-15 22:23:18 +01001804static const u8 tid_to_ucode_ac[] = {
1805 AC_BE,
1806 AC_BK,
1807 AC_BK,
1808 AC_BE,
1809 AC_VI,
1810 AC_VI,
1811 AC_VO,
1812 AC_VO,
1813};
1814
Johannes Berg8ca151b2013-01-24 14:25:36 +01001815int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1816 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1817{
Johannes Berg5b577a92013-11-14 18:20:04 +01001818 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001819 struct iwl_mvm_tid_data *tid_data;
1820 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001821 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001822
1823 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1824 return -EINVAL;
1825
1826 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
1827 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
1828 mvmsta->tid_data[tid].state);
1829 return -ENXIO;
1830 }
1831
1832 lockdep_assert_held(&mvm->mutex);
1833
Arik Nemtsovb2492502014-03-13 12:21:50 +02001834 spin_lock_bh(&mvmsta->lock);
1835
1836 /* possible race condition - we entered D0i3 while starting agg */
1837 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
1838 spin_unlock_bh(&mvmsta->lock);
1839 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
1840 return -EIO;
1841 }
1842
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001843 spin_lock_bh(&mvm->queue_info_lock);
1844
Liad Kaufmancf961e12015-08-13 19:16:08 +03001845 /*
1846 * Note the possible cases:
1847 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
1848 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
1849 * one and mark it as reserved
1850 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
1851 * non-DQA mode, since the TXQ hasn't yet been allocated
1852 */
1853 txq_id = mvmsta->tid_data[tid].txq_id;
1854 if (!iwl_mvm_is_dqa_supported(mvm) ||
1855 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03001856 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1857 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03001858 mvm->last_agg_queue);
1859 if (txq_id < 0) {
1860 ret = txq_id;
1861 spin_unlock_bh(&mvm->queue_info_lock);
1862 IWL_ERR(mvm, "Failed to allocate agg queue\n");
1863 goto release_locks;
1864 }
1865
1866 /* TXQ hasn't yet been enabled, so mark it only as reserved */
1867 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001868 }
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001869 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001870
Liad Kaufmancf961e12015-08-13 19:16:08 +03001871 IWL_DEBUG_TX_QUEUES(mvm,
1872 "AGG for tid %d will be on queue #%d\n",
1873 tid, txq_id);
1874
Johannes Berg8ca151b2013-01-24 14:25:36 +01001875 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01001876 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001877 tid_data->txq_id = txq_id;
1878 *ssn = tid_data->ssn;
1879
1880 IWL_DEBUG_TX_QUEUES(mvm,
1881 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1882 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1883 tid_data->next_reclaimed);
1884
1885 if (tid_data->ssn == tid_data->next_reclaimed) {
1886 tid_data->state = IWL_AGG_STARTING;
1887 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1888 } else {
1889 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1890 }
1891
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001892 ret = 0;
1893
1894release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001895 spin_unlock_bh(&mvmsta->lock);
1896
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001897 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001898}
1899
1900int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001901 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
1902 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001903{
Johannes Berg5b577a92013-11-14 18:20:04 +01001904 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001905 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001906 unsigned int wdg_timeout =
1907 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001908 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03001909 bool alloc_queue = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001910 u16 ssn;
1911
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001912 struct iwl_trans_txq_scd_cfg cfg = {
1913 .sta_id = mvmsta->sta_id,
1914 .tid = tid,
1915 .frame_limit = buf_size,
1916 .aggregate = true,
1917 };
1918
Eyal Shapiraefed6642014-09-14 15:58:53 +03001919 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1920 != IWL_MAX_TID_COUNT);
1921
Johannes Berg8ca151b2013-01-24 14:25:36 +01001922 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1923
1924 spin_lock_bh(&mvmsta->lock);
1925 ssn = tid_data->ssn;
1926 queue = tid_data->txq_id;
1927 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001928 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001929 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001930 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001931 spin_unlock_bh(&mvmsta->lock);
1932
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001933 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001934
Liad Kaufmancf961e12015-08-13 19:16:08 +03001935 /* In DQA mode, the existing queue might need to be reconfigured */
1936 if (iwl_mvm_is_dqa_supported(mvm)) {
1937 spin_lock_bh(&mvm->queue_info_lock);
1938 /* Maybe there is no need to even alloc a queue... */
1939 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
1940 alloc_queue = false;
1941 spin_unlock_bh(&mvm->queue_info_lock);
1942
1943 /*
1944 * Only reconfig the SCD for the queue if the window size has
1945 * changed from current (become smaller)
1946 */
1947 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
1948 /*
1949 * If reconfiguring an existing queue, it first must be
1950 * drained
1951 */
1952 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1953 BIT(queue));
1954 if (ret) {
1955 IWL_ERR(mvm,
1956 "Error draining queue before reconfig\n");
1957 return ret;
1958 }
1959
1960 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
1961 mvmsta->sta_id, tid,
1962 buf_size, ssn);
1963 if (ret) {
1964 IWL_ERR(mvm,
1965 "Error reconfiguring TXQ #%d\n", queue);
1966 return ret;
1967 }
1968 }
1969 }
1970
1971 if (alloc_queue)
1972 iwl_mvm_enable_txq(mvm, queue,
1973 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
1974 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001975
Johannes Berg8ca151b2013-01-24 14:25:36 +01001976 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1977 if (ret)
1978 return -EIO;
1979
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001980 /* No need to mark as reserved */
1981 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03001982 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001983 spin_unlock_bh(&mvm->queue_info_lock);
1984
Johannes Berg8ca151b2013-01-24 14:25:36 +01001985 /*
1986 * Even though in theory the peer could have different
1987 * aggregation reorder buffer sizes for different sessions,
1988 * our ucode doesn't allow for that and has a global limit
1989 * for each station. Therefore, use the minimum of all the
1990 * aggregation sessions and our default value.
1991 */
1992 mvmsta->max_agg_bufsize =
1993 min(mvmsta->max_agg_bufsize, buf_size);
1994 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1995
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001996 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1997 sta->addr, tid);
1998
Eyal Shapira9e680942013-11-09 00:16:16 +02001999 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002000}
2001
2002int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2003 struct ieee80211_sta *sta, u16 tid)
2004{
Johannes Berg5b577a92013-11-14 18:20:04 +01002005 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002006 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2007 u16 txq_id;
2008 int err;
2009
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002010
2011 /*
2012 * If mac80211 is cleaning its state, then say that we finished since
2013 * our state has been cleared anyway.
2014 */
2015 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2016 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2017 return 0;
2018 }
2019
Johannes Berg8ca151b2013-01-24 14:25:36 +01002020 spin_lock_bh(&mvmsta->lock);
2021
2022 txq_id = tid_data->txq_id;
2023
2024 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2025 mvmsta->sta_id, tid, txq_id, tid_data->state);
2026
Eyal Shapiraefed6642014-09-14 15:58:53 +03002027 mvmsta->agg_tids &= ~BIT(tid);
2028
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002029 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002030 /*
2031 * The TXQ is marked as reserved only if no traffic came through yet
2032 * This means no traffic has been sent on this TID (agg'd or not), so
2033 * we no longer have use for the queue. Since it hasn't even been
2034 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2035 * free.
2036 */
2037 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2038 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002039 spin_unlock_bh(&mvm->queue_info_lock);
2040
Johannes Berg8ca151b2013-01-24 14:25:36 +01002041 switch (tid_data->state) {
2042 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002043 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002044
2045 IWL_DEBUG_TX_QUEUES(mvm,
2046 "ssn = %d, next_recl = %d\n",
2047 tid_data->ssn, tid_data->next_reclaimed);
2048
2049 /* There are still packets for this RA / TID in the HW */
2050 if (tid_data->ssn != tid_data->next_reclaimed) {
2051 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2052 err = 0;
2053 break;
2054 }
2055
2056 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002057 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002058 spin_unlock_bh(&mvmsta->lock);
2059
2060 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2061
2062 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2063
Liad Kaufmancf961e12015-08-13 19:16:08 +03002064 if (!iwl_mvm_is_dqa_supported(mvm)) {
2065 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2066
2067 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2068 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002069 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002070 case IWL_AGG_STARTING:
2071 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2072 /*
2073 * The agg session has been stopped before it was set up. This
2074 * can happen when the AddBA timer times out for example.
2075 */
2076
2077 /* No barriers since we are under mutex */
2078 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002079
2080 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2081 tid_data->state = IWL_AGG_OFF;
2082 err = 0;
2083 break;
2084 default:
2085 IWL_ERR(mvm,
2086 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2087 mvmsta->sta_id, tid, tid_data->state);
2088 IWL_ERR(mvm,
2089 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2090 err = -EINVAL;
2091 }
2092
2093 spin_unlock_bh(&mvmsta->lock);
2094
2095 return err;
2096}
2097
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002098int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2099 struct ieee80211_sta *sta, u16 tid)
2100{
Johannes Berg5b577a92013-11-14 18:20:04 +01002101 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002102 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2103 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002104 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002105
2106 /*
2107 * First set the agg state to OFF to avoid calling
2108 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2109 */
2110 spin_lock_bh(&mvmsta->lock);
2111 txq_id = tid_data->txq_id;
2112 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2113 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002114 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002115 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002116 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002117 spin_unlock_bh(&mvmsta->lock);
2118
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002119 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002120 /*
2121 * The TXQ is marked as reserved only if no traffic came through yet
2122 * This means no traffic has been sent on this TID (agg'd or not), so
2123 * we no longer have use for the queue. Since it hasn't even been
2124 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2125 * free.
2126 */
2127 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2128 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002129 spin_unlock_bh(&mvm->queue_info_lock);
2130
Johannes Bergb6658ff2013-07-24 13:55:51 +02002131 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002132 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03002133 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02002134 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002135 iwl_trans_wait_tx_queue_empty(mvm->trans,
2136 mvmsta->tfd_queue_msk);
2137 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002138
Johannes Bergf7f89e72014-08-05 15:24:44 +02002139 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2140
Liad Kaufmancf961e12015-08-13 19:16:08 +03002141 if (!iwl_mvm_is_dqa_supported(mvm)) {
2142 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2143
2144 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2145 tid, 0);
2146 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002147 }
2148
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002149 return 0;
2150}
2151
Johannes Berg8ca151b2013-01-24 14:25:36 +01002152static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2153{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002154 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002155
2156 lockdep_assert_held(&mvm->mutex);
2157
Johannes Berg2dc2a152015-06-16 17:09:18 +02002158 /* Pick the unused key offset with the highest 'deleted'
2159 * counter. Every time a key is deleted, all the counters
2160 * are incremented and the one that was just deleted is
2161 * reset to zero. Thus, the highest counter is the one
2162 * that was deleted longest ago. Pick that one.
2163 */
2164 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2165 if (test_bit(i, mvm->fw_key_table))
2166 continue;
2167 if (mvm->fw_key_deleted[i] > max) {
2168 max = mvm->fw_key_deleted[i];
2169 max_offs = i;
2170 }
2171 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002172
Johannes Berg2dc2a152015-06-16 17:09:18 +02002173 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002174 return STA_KEY_IDX_INVALID;
2175
Johannes Berg2dc2a152015-06-16 17:09:18 +02002176 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002177}
2178
Johannes Berg5f7a1842015-12-11 09:36:10 +01002179static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2180 struct ieee80211_vif *vif,
2181 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002182{
Johannes Berg5b530e92014-12-23 16:00:17 +01002183 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002184
Johannes Berg5f7a1842015-12-11 09:36:10 +01002185 if (sta)
2186 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002187
2188 /*
2189 * The device expects GTKs for station interfaces to be
2190 * installed as GTKs for the AP station. If we have no
2191 * station ID, then use AP's station ID.
2192 */
2193 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02002194 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2195 u8 sta_id = mvmvif->ap_sta_id;
2196
Avri Altman9513c5e2015-10-19 16:29:11 +02002197 /*
2198 * It is possible that the 'sta' parameter is NULL,
2199 * for example when a GTK is removed - the sta_id will then
2200 * be the AP ID, and no station was passed by mac80211.
2201 */
Sara Sharon13303c02016-04-10 15:51:54 +03002202 return iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Avri Altman9513c5e2015-10-19 16:29:11 +02002203 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002204
Johannes Berg5f7a1842015-12-11 09:36:10 +01002205 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002206}
2207
2208static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2209 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002210 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002211 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2212 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002213{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002214 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002215 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002216 int ret;
2217 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002218 u16 keyidx;
2219 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002220 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002221
2222 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2223 STA_KEY_FLG_KEYID_MSK;
2224 key_flags = cpu_to_le16(keyidx);
2225 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2226
2227 switch (keyconf->cipher) {
2228 case WLAN_CIPHER_SUITE_TKIP:
2229 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002230 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002231 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03002232 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2233 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002234 break;
2235 case WLAN_CIPHER_SUITE_CCMP:
2236 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002237 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002238 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002239 case WLAN_CIPHER_SUITE_WEP104:
2240 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002241 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002242 case WLAN_CIPHER_SUITE_WEP40:
2243 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2244 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2245 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002246 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002247 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2248 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002249 }
2250
Johannes Bergba3943b2014-11-12 23:54:48 +01002251 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002252 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2253
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002254 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03002255 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002256 cmd.sta_id = sta_id;
2257
2258 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002259 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002260 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2261 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002262 else
2263 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2264 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002265
2266 switch (status) {
2267 case ADD_STA_SUCCESS:
2268 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2269 break;
2270 default:
2271 ret = -EIO;
2272 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2273 break;
2274 }
2275
2276 return ret;
2277}
2278
2279static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2280 struct ieee80211_key_conf *keyconf,
2281 u8 sta_id, bool remove_key)
2282{
2283 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2284
2285 /* verify the key details match the required command's expectations */
2286 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
2287 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2288 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
2289 return -EINVAL;
2290
2291 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2292 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2293
2294 if (remove_key) {
2295 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2296 } else {
2297 struct ieee80211_key_seq seq;
2298 const u8 *pn;
2299
2300 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002301 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2302 pn = seq.aes_cmac.pn;
2303 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2304 ((u64) pn[4] << 8) |
2305 ((u64) pn[3] << 16) |
2306 ((u64) pn[2] << 24) |
2307 ((u64) pn[1] << 32) |
2308 ((u64) pn[0] << 40));
2309 }
2310
2311 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2312 remove_key ? "removing" : "installing",
2313 igtk_cmd.sta_id);
2314
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002315 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002316 sizeof(igtk_cmd), &igtk_cmd);
2317}
2318
2319
2320static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2321 struct ieee80211_vif *vif,
2322 struct ieee80211_sta *sta)
2323{
Johannes Berg5b530e92014-12-23 16:00:17 +01002324 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002325
2326 if (sta)
2327 return sta->addr;
2328
2329 if (vif->type == NL80211_IFTYPE_STATION &&
2330 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2331 u8 sta_id = mvmvif->ap_sta_id;
2332 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2333 lockdep_is_held(&mvm->mutex));
2334 return sta->addr;
2335 }
2336
2337
2338 return NULL;
2339}
2340
Johannes Berg2f6319d2014-11-12 23:39:56 +01002341static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2342 struct ieee80211_vif *vif,
2343 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002344 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002345 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01002346 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002347{
Johannes Berg2f6319d2014-11-12 23:39:56 +01002348 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002349 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002350 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002351 struct ieee80211_key_seq seq;
2352 u16 p1k[5];
2353
Johannes Berg8ca151b2013-01-24 14:25:36 +01002354 switch (keyconf->cipher) {
2355 case WLAN_CIPHER_SUITE_TKIP:
2356 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2357 /* get phase 1 key from mac80211 */
2358 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2359 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01002360 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002361 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002362 break;
2363 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01002364 case WLAN_CIPHER_SUITE_WEP40:
2365 case WLAN_CIPHER_SUITE_WEP104:
2366 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002367 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002368 break;
2369 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01002370 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002371 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002372 }
2373
Johannes Berg8ca151b2013-01-24 14:25:36 +01002374 return ret;
2375}
2376
Johannes Berg2f6319d2014-11-12 23:39:56 +01002377static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01002378 struct ieee80211_key_conf *keyconf,
2379 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002380{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002381 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002382 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002383 int ret;
2384 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002385
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02002386 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2387 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002388 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2389 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2390
Johannes Bergba3943b2014-11-12 23:54:48 +01002391 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002392 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2393
Max Stepanov5a258aa2013-04-07 09:11:21 +03002394 cmd.key_flags = key_flags;
2395 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002396 cmd.sta_id = sta_id;
2397
Johannes Berg8ca151b2013-01-24 14:25:36 +01002398 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002399 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2400 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002401
2402 switch (status) {
2403 case ADD_STA_SUCCESS:
2404 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2405 break;
2406 default:
2407 ret = -EIO;
2408 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2409 break;
2410 }
2411
2412 return ret;
2413}
2414
Johannes Berg2f6319d2014-11-12 23:39:56 +01002415int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2416 struct ieee80211_vif *vif,
2417 struct ieee80211_sta *sta,
2418 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002419 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002420{
Johannes Bergba3943b2014-11-12 23:54:48 +01002421 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002422 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002423 u8 sta_id;
2424 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03002425 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01002426
2427 lockdep_assert_held(&mvm->mutex);
2428
2429 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01002430 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2431 if (!mvm_sta) {
2432 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01002433 return -EINVAL;
2434 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01002435 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002436
2437 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
2438 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2439 goto end;
2440 }
2441
2442 /*
2443 * It is possible that the 'sta' parameter is NULL, and thus
2444 * there is a need to retrieve the sta from the local station table.
2445 */
2446 if (!sta) {
2447 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2448 lockdep_is_held(&mvm->mutex));
2449 if (IS_ERR_OR_NULL(sta)) {
2450 IWL_ERR(mvm, "Invalid station id\n");
2451 return -EINVAL;
2452 }
2453 }
2454
2455 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2456 return -EINVAL;
2457
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002458 /* If the key_offset is not pre-assigned, we need to find a
2459 * new offset to use. In normal cases, the offset is not
2460 * pre-assigned, but during HW_RESTART we want to reuse the
2461 * same indices, so we pass them when this function is called.
2462 *
2463 * In D3 entry, we need to hardcoded the indices (because the
2464 * firmware hardcodes the PTK offset to 0). In this case, we
2465 * need to make sure we don't overwrite the hw_key_idx in the
2466 * keyconf structure, because otherwise we cannot configure
2467 * the original ones back when resuming.
2468 */
2469 if (key_offset == STA_KEY_IDX_INVALID) {
2470 key_offset = iwl_mvm_set_fw_key_idx(mvm);
2471 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002472 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002473 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002474 }
2475
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002476 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002477 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01002478 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002479
2480 /*
2481 * For WEP, the same key is used for multicast and unicast. Upload it
2482 * again, using the same key offset, and now pointing the other one
2483 * to the same key slot (offset).
2484 * If this fails, remove the original as well.
2485 */
2486 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2487 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002488 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
2489 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01002490 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01002491 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002492 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002493 }
2494 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01002495
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002496 __set_bit(key_offset, mvm->fw_key_table);
2497
Johannes Berg2f6319d2014-11-12 23:39:56 +01002498end:
2499 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2500 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03002501 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002502 return ret;
2503}
2504
2505int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2506 struct ieee80211_vif *vif,
2507 struct ieee80211_sta *sta,
2508 struct ieee80211_key_conf *keyconf)
2509{
Johannes Bergba3943b2014-11-12 23:54:48 +01002510 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002511 struct iwl_mvm_sta *mvm_sta;
2512 u8 sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg2dc2a152015-06-16 17:09:18 +02002513 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002514
2515 lockdep_assert_held(&mvm->mutex);
2516
Johannes Berg5f7a1842015-12-11 09:36:10 +01002517 /* Get the station from the mvm local station table */
2518 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002519
2520 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2521 keyconf->keyidx, sta_id);
2522
2523 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
2524 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2525
2526 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2527 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2528 keyconf->hw_key_idx);
2529 return -ENOENT;
2530 }
2531
Johannes Berg2dc2a152015-06-16 17:09:18 +02002532 /* track which key was deleted last */
2533 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2534 if (mvm->fw_key_deleted[i] < U8_MAX)
2535 mvm->fw_key_deleted[i]++;
2536 }
2537 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2538
Johannes Berg5f7a1842015-12-11 09:36:10 +01002539 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01002540 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2541 return 0;
2542 }
2543
Johannes Berg5f7a1842015-12-11 09:36:10 +01002544 sta_id = mvm_sta->sta_id;
2545
Johannes Bergba3943b2014-11-12 23:54:48 +01002546 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2547 if (ret)
2548 return ret;
2549
2550 /* delete WEP key twice to get rid of (now useless) offset */
2551 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2552 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2553 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2554
2555 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002556}
2557
Johannes Berg8ca151b2013-01-24 14:25:36 +01002558void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2559 struct ieee80211_vif *vif,
2560 struct ieee80211_key_conf *keyconf,
2561 struct ieee80211_sta *sta, u32 iv32,
2562 u16 *phase1key)
2563{
Beni Levc3eb5362013-02-06 17:22:18 +02002564 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01002565 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002566
Beni Levc3eb5362013-02-06 17:22:18 +02002567 rcu_read_lock();
2568
Johannes Berg5f7a1842015-12-11 09:36:10 +01002569 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2570 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002571 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01002572 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002573 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002574
2575 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02002576 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01002577}
2578
Johannes Berg9cc40712013-02-15 22:47:48 +01002579void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
2580 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002581{
Johannes Berg5b577a92013-11-14 18:20:04 +01002582 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002583 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002584 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002585 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03002586 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01002587 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002588 };
2589 int ret;
2590
Sara Sharon854c5702016-01-26 13:17:47 +02002591 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2592 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002593 if (ret)
2594 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2595}
2596
Johannes Berg9cc40712013-02-15 22:47:48 +01002597void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
2598 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002599 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01002600 u16 cnt, u16 tids, bool more_data,
2601 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002602{
Johannes Berg5b577a92013-11-14 18:20:04 +01002603 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002604 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002605 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002606 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002607 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
2608 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01002609 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002610 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01002611 int tid, ret;
2612 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002613
Johannes Berg3e56ead2013-02-15 22:23:18 +01002614 /* convert TIDs to ACs - we don't support TSPEC so that's OK
2615 * Note that this field is reserved and unused by firmware not
2616 * supporting GO uAPSD, so it's safe to always do this.
2617 */
2618 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
2619 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
2620
2621 /* If we're releasing frames from aggregation queues then check if the
2622 * all queues combined that we're releasing frames from have
2623 * - more frames than the service period, in which case more_data
2624 * needs to be set
2625 * - fewer than 'cnt' frames, in which case we need to adjust the
2626 * firmware command (but do that unconditionally)
2627 */
2628 if (agg) {
2629 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002630 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002631
2632 spin_lock_bh(&mvmsta->lock);
2633 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
2634 struct iwl_mvm_tid_data *tid_data;
2635 u16 n_queued;
2636
2637 tid_data = &mvmsta->tid_data[tid];
2638 if (WARN(tid_data->state != IWL_AGG_ON &&
2639 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
2640 "TID %d state is %d\n",
2641 tid, tid_data->state)) {
2642 spin_unlock_bh(&mvmsta->lock);
2643 ieee80211_sta_eosp(sta);
2644 return;
2645 }
2646
2647 n_queued = iwl_mvm_tid_queued(tid_data);
2648 if (n_queued > remaining) {
2649 more_data = true;
2650 remaining = 0;
2651 break;
2652 }
2653 remaining -= n_queued;
2654 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002655 sleep_tx_count = cnt - remaining;
2656 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
2657 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002658 spin_unlock_bh(&mvmsta->lock);
2659
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002660 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01002661 if (WARN_ON(cnt - remaining == 0)) {
2662 ieee80211_sta_eosp(sta);
2663 return;
2664 }
2665 }
2666
2667 /* Note: this is ignored by firmware not supporting GO uAPSD */
2668 if (more_data)
2669 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
2670
2671 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
2672 mvmsta->next_status_eosp = true;
2673 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
2674 } else {
2675 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
2676 }
2677
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02002678 /* block the Tx queues until the FW updated the sleep Tx count */
2679 iwl_trans_block_txq_ptrs(mvm->trans, true);
2680
2681 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
2682 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02002683 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002684 if (ret)
2685 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2686}
Johannes Berg3e56ead2013-02-15 22:23:18 +01002687
Johannes Berg04168412015-06-23 21:22:09 +02002688void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
2689 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01002690{
2691 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2692 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
2693 struct ieee80211_sta *sta;
2694 u32 sta_id = le32_to_cpu(notif->sta_id);
2695
2696 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02002697 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002698
2699 rcu_read_lock();
2700 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2701 if (!IS_ERR_OR_NULL(sta))
2702 ieee80211_sta_eosp(sta);
2703 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01002704}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002705
2706void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
2707 struct iwl_mvm_sta *mvmsta, bool disable)
2708{
2709 struct iwl_mvm_add_sta_cmd cmd = {
2710 .add_modify = STA_MODE_MODIFY,
2711 .sta_id = mvmsta->sta_id,
2712 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
2713 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
2714 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2715 };
2716 int ret;
2717
Sara Sharon854c5702016-01-26 13:17:47 +02002718 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2719 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002720 if (ret)
2721 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2722}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002723
2724void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
2725 struct ieee80211_sta *sta,
2726 bool disable)
2727{
2728 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2729
2730 spin_lock_bh(&mvm_sta->lock);
2731
2732 if (mvm_sta->disable_tx == disable) {
2733 spin_unlock_bh(&mvm_sta->lock);
2734 return;
2735 }
2736
2737 mvm_sta->disable_tx = disable;
2738
2739 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03002740 * Tell mac80211 to start/stop queuing tx for this station,
2741 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002742 * for this station.
2743 */
2744 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
2745 ieee80211_sta_block_awake(mvm->hw, sta, disable);
2746
2747 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
2748
2749 spin_unlock_bh(&mvm_sta->lock);
2750}
2751
2752void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
2753 struct iwl_mvm_vif *mvmvif,
2754 bool disable)
2755{
2756 struct ieee80211_sta *sta;
2757 struct iwl_mvm_sta *mvm_sta;
2758 int i;
2759
2760 lockdep_assert_held(&mvm->mutex);
2761
2762 /* Block/unblock all the stations of the given mvmvif */
2763 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
2764 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
2765 lockdep_is_held(&mvm->mutex));
2766 if (IS_ERR_OR_NULL(sta))
2767 continue;
2768
2769 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2770 if (mvm_sta->mac_id_n_color !=
2771 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
2772 continue;
2773
2774 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
2775 }
2776}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02002777
2778void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2779{
2780 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2781 struct iwl_mvm_sta *mvmsta;
2782
2783 rcu_read_lock();
2784
2785 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
2786
2787 if (!WARN_ON(!mvmsta))
2788 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2789
2790 rcu_read_unlock();
2791}