blob: a92e12edeb3ab94ca80401f592dfd14247e5dc23 [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
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200471 /* Don't try and take queues being reconfigured */
472 if (mvm->queue_info[queue].status ==
473 IWL_MVM_QUEUE_RECONFIGURING)
474 continue;
475
Liad Kaufman42db09c2016-05-02 14:01:14 +0300476 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
477 }
478
479 /*
480 * The queue to share is chosen only from DATA queues as follows (in
481 * descending priority):
482 * 1. An AC_BE queue
483 * 2. Same AC queue
484 * 3. Highest AC queue that is lower than new AC
485 * 4. Any existing AC (there always is at least 1 DATA queue)
486 */
487
488 /* Priority 1: An AC_BE queue */
489 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
490 queue = ac_to_queue[IEEE80211_AC_BE];
491 /* Priority 2: Same AC queue */
492 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
493 queue = ac_to_queue[ac];
494 /* Priority 3a: If new AC is VO and VI exists - use VI */
495 else if (ac == IEEE80211_AC_VO &&
496 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
497 queue = ac_to_queue[IEEE80211_AC_VI];
498 /* Priority 3b: No BE so only AC less than the new one is BK */
499 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
500 queue = ac_to_queue[IEEE80211_AC_BK];
501 /* Priority 4a: No BE nor BK - use VI if exists */
502 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
503 queue = ac_to_queue[IEEE80211_AC_VI];
504 /* Priority 4b: No BE, BK nor VI - use VO if exists */
505 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
506 queue = ac_to_queue[IEEE80211_AC_VO];
507
508 /* Make sure queue found (or not) is legal */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200509 if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
510 !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
511 (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
Liad Kaufman42db09c2016-05-02 14:01:14 +0300512 IWL_ERR(mvm, "No DATA queues available to share\n");
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200513 return -ENOSPC;
514 }
515
516 /* Make sure the queue isn't in the middle of being reconfigured */
517 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_RECONFIGURING) {
518 IWL_ERR(mvm,
519 "TXQ %d is in the middle of re-config - try again\n",
520 queue);
521 return -EBUSY;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300522 }
523
524 return queue;
525}
526
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200527/*
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200528 * If a given queue has a higher AC than the TID stream that is being compared
529 * to, the queue needs to be redirected to the lower AC. This function does that
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200530 * in such a case, otherwise - if no redirection required - it does nothing,
531 * unless the %force param is true.
532 */
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200533int iwl_mvm_scd_queue_redirect(struct iwl_mvm *mvm, int queue, int tid,
534 int ac, int ssn, unsigned int wdg_timeout,
535 bool force)
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200536{
537 struct iwl_scd_txq_cfg_cmd cmd = {
538 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200539 .action = SCD_CFG_DISABLE_QUEUE,
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200540 };
541 bool shared_queue;
542 unsigned long mq;
543 int ret;
544
545 /*
546 * If the AC is lower than current one - FIFO needs to be redirected to
547 * the lowest one of the streams in the queue. Check if this is needed
548 * here.
549 * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
550 * value 3 and VO with value 0, so to check if ac X is lower than ac Y
551 * we need to check if the numerical value of X is LARGER than of Y.
552 */
553 spin_lock_bh(&mvm->queue_info_lock);
554 if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
555 spin_unlock_bh(&mvm->queue_info_lock);
556
557 IWL_DEBUG_TX_QUEUES(mvm,
558 "No redirection needed on TXQ #%d\n",
559 queue);
560 return 0;
561 }
562
563 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
564 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200565 cmd.tid = mvm->queue_info[queue].txq_tid;
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200566 mq = mvm->queue_info[queue].hw_queue_to_mac80211;
567 shared_queue = (mvm->queue_info[queue].hw_queue_refcount > 1);
568 spin_unlock_bh(&mvm->queue_info_lock);
569
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200570 IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200571 queue, iwl_mvm_ac_to_tx_fifo[ac]);
572
573 /* Stop MAC queues and wait for this queue to empty */
574 iwl_mvm_stop_mac_queues(mvm, mq);
575 ret = iwl_trans_wait_tx_queue_empty(mvm->trans, BIT(queue));
576 if (ret) {
577 IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
578 queue);
579 ret = -EIO;
580 goto out;
581 }
582
583 /* Before redirecting the queue we need to de-activate it */
584 iwl_trans_txq_disable(mvm->trans, queue, false);
585 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
586 if (ret)
587 IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
588 ret);
589
590 /* Make sure the SCD wrptr is correctly set before reconfiguring */
Sara Sharonca3b9c62016-06-30 16:14:02 +0300591 iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200592
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200593 /* Update the TID "owner" of the queue */
594 spin_lock_bh(&mvm->queue_info_lock);
595 mvm->queue_info[queue].txq_tid = tid;
596 spin_unlock_bh(&mvm->queue_info_lock);
597
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200598 /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
599
600 /* Redirect to lower AC */
601 iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
602 cmd.sta_id, tid, LINK_QUAL_AGG_FRAME_LIMIT_DEF,
603 ssn);
604
605 /* Update AC marking of the queue */
606 spin_lock_bh(&mvm->queue_info_lock);
607 mvm->queue_info[queue].mac80211_ac = ac;
608 spin_unlock_bh(&mvm->queue_info_lock);
609
610 /*
611 * Mark queue as shared in transport if shared
612 * Note this has to be done after queue enablement because enablement
613 * can also set this value, and there is no indication there to shared
614 * queues
615 */
616 if (shared_queue)
617 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
618
619out:
620 /* Continue using the MAC queues */
621 iwl_mvm_start_mac_queues(mvm, mq);
622
623 return ret;
624}
625
Liad Kaufman24afba72015-07-28 18:56:08 +0300626static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
627 struct ieee80211_sta *sta, u8 ac, int tid,
628 struct ieee80211_hdr *hdr)
629{
630 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
631 struct iwl_trans_txq_scd_cfg cfg = {
632 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
633 .sta_id = mvmsta->sta_id,
634 .tid = tid,
635 .frame_limit = IWL_FRAME_LIMIT,
636 };
637 unsigned int wdg_timeout =
638 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
639 u8 mac_queue = mvmsta->vif->hw_queue[ac];
640 int queue = -1;
Liad Kaufman9794c642015-08-19 17:34:28 +0300641 bool using_inactive_queue = false;
642 unsigned long disable_agg_tids = 0;
643 enum iwl_mvm_agg_state queue_state;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300644 bool shared_queue = false;
Liad Kaufman24afba72015-07-28 18:56:08 +0300645 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300646 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300647 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300648
649 lockdep_assert_held(&mvm->mutex);
650
Liad Kaufman42db09c2016-05-02 14:01:14 +0300651 spin_lock_bh(&mvmsta->lock);
652 tfd_queue_mask = mvmsta->tfd_queue_msk;
653 spin_unlock_bh(&mvmsta->lock);
654
Liad Kaufmand2515a92016-03-23 16:31:08 +0200655 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300656
657 /*
658 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
659 * exists
660 */
661 if (!ieee80211_is_data_qos(hdr->frame_control) ||
662 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300663 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
664 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300665 IWL_MVM_DQA_MAX_MGMT_QUEUE);
666 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
667 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
668 queue);
669
670 /* If no such queue is found, we'll use a DATA queue instead */
671 }
672
Liad Kaufman9794c642015-08-19 17:34:28 +0300673 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
674 (mvm->queue_info[mvmsta->reserved_queue].status ==
675 IWL_MVM_QUEUE_RESERVED ||
676 mvm->queue_info[mvmsta->reserved_queue].status ==
677 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300678 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300679 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300680 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
681 }
682
683 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300684 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
685 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300686 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300687
688 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300689 * Check if this queue is already allocated but inactive.
690 * In such a case, we'll need to first free this queue before enabling
691 * it again, so we'll mark it as reserved to make sure no new traffic
692 * arrives on it
693 */
694 if (queue > 0 &&
695 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
696 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
697 using_inactive_queue = true;
698 IWL_DEBUG_TX_QUEUES(mvm,
699 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
700 queue, mvmsta->sta_id, tid);
701 }
702
Liad Kaufman42db09c2016-05-02 14:01:14 +0300703 /* No free queue - we'll have to share */
704 if (queue <= 0) {
705 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
706 if (queue > 0) {
707 shared_queue = true;
708 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
709 }
710 }
711
Liad Kaufman9794c642015-08-19 17:34:28 +0300712 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300713 * Mark TXQ as ready, even though it hasn't been fully configured yet,
714 * to make sure no one else takes it.
715 * This will allow avoiding re-acquiring the lock at the end of the
716 * configuration. On error we'll mark it back as free.
717 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300718 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300719 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300720
Liad Kaufmand2515a92016-03-23 16:31:08 +0200721 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300722
Liad Kaufman42db09c2016-05-02 14:01:14 +0300723 /* This shouldn't happen - out of queues */
724 if (WARN_ON(queue <= 0)) {
725 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
726 tid, cfg.sta_id);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200727 return queue;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300728 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300729
730 /*
731 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
732 * but for configuring the SCD to send A-MPDUs we need to mark the queue
733 * as aggregatable.
734 * Mark all DATA queues as allowing to be aggregated at some point
735 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300736 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
737 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300738
Liad Kaufman9794c642015-08-19 17:34:28 +0300739 /*
740 * If this queue was previously inactive (idle) - we need to free it
741 * first
742 */
743 if (using_inactive_queue) {
744 struct iwl_scd_txq_cfg_cmd cmd = {
745 .scd_queue = queue,
Liad Kaufmanf7c692d2016-03-08 10:41:32 +0200746 .action = SCD_CFG_DISABLE_QUEUE,
Liad Kaufman9794c642015-08-19 17:34:28 +0300747 };
Liad Kaufman93f436e2015-08-31 13:41:26 +0300748 u8 ac;
Liad Kaufman9794c642015-08-19 17:34:28 +0300749
750 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
751
Liad Kaufman93f436e2015-08-31 13:41:26 +0300752 spin_lock_bh(&mvm->queue_info_lock);
753 ac = mvm->queue_info[queue].mac80211_ac;
754 cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
755 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[ac];
Liad Kaufmanedbe9612016-02-02 15:43:32 +0200756 cmd.tid = mvm->queue_info[queue].txq_tid;
Liad Kaufman93f436e2015-08-31 13:41:26 +0300757 spin_unlock_bh(&mvm->queue_info_lock);
758
Liad Kaufman9794c642015-08-19 17:34:28 +0300759 /* Disable the queue */
Liad Kaufman8d98ae62016-02-02 16:02:46 +0200760 if (disable_agg_tids)
761 iwl_mvm_invalidate_sta_queue(mvm, queue,
762 disable_agg_tids, false);
Liad Kaufman9794c642015-08-19 17:34:28 +0300763 iwl_trans_txq_disable(mvm->trans, queue, false);
764 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
765 &cmd);
766 if (ret) {
767 IWL_ERR(mvm,
768 "Failed to free inactive queue %d (ret=%d)\n",
769 queue, ret);
770
771 /* Re-mark the inactive queue as inactive */
772 spin_lock_bh(&mvm->queue_info_lock);
773 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
774 spin_unlock_bh(&mvm->queue_info_lock);
775
776 return ret;
777 }
Liad Kaufman8d98ae62016-02-02 16:02:46 +0200778
779 /* If TXQ is allocated to another STA, update removal in FW */
780 if (cmd.sta_id != mvmsta->sta_id)
781 iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
Liad Kaufman9794c642015-08-19 17:34:28 +0300782 }
783
Liad Kaufman42db09c2016-05-02 14:01:14 +0300784 IWL_DEBUG_TX_QUEUES(mvm,
785 "Allocating %squeue #%d to sta %d on tid %d\n",
786 shared_queue ? "shared " : "", queue,
787 mvmsta->sta_id, tid);
788
789 if (shared_queue) {
790 /* Disable any open aggs on this queue */
791 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
792
793 if (disable_agg_tids) {
794 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
795 queue);
796 iwl_mvm_invalidate_sta_queue(mvm, queue,
797 disable_agg_tids, false);
798 }
Liad Kaufman42db09c2016-05-02 14:01:14 +0300799 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300800
801 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
802 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
803 wdg_timeout);
804
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200805 /*
806 * Mark queue as shared in transport if shared
807 * Note this has to be done after queue enablement because enablement
808 * can also set this value, and there is no indication there to shared
809 * queues
810 */
811 if (shared_queue)
812 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
813
Liad Kaufman24afba72015-07-28 18:56:08 +0300814 spin_lock_bh(&mvmsta->lock);
815 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300816 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300817 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300818 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300819
820 if (mvmsta->reserved_queue == queue)
821 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
822 spin_unlock_bh(&mvmsta->lock);
823
Liad Kaufman42db09c2016-05-02 14:01:14 +0300824 if (!shared_queue) {
825 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
826 if (ret)
827 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300828
Liad Kaufman42db09c2016-05-02 14:01:14 +0300829 /* If we need to re-enable aggregations... */
830 if (queue_state == IWL_AGG_ON) {
831 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
832 if (ret)
833 goto out_err;
834 }
Liad Kaufman58f2cc52015-09-30 16:44:28 +0200835 } else {
836 /* Redirect queue, if needed */
837 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid, ac, ssn,
838 wdg_timeout, false);
839 if (ret)
840 goto out_err;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300841 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300842
Liad Kaufman42db09c2016-05-02 14:01:14 +0300843 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300844
845out_err:
846 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
847
848 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300849}
850
Liad Kaufman19aefa42016-03-08 14:29:51 +0200851static void iwl_mvm_change_queue_owner(struct iwl_mvm *mvm, int queue)
852{
853 struct iwl_scd_txq_cfg_cmd cmd = {
854 .scd_queue = queue,
855 .action = SCD_CFG_UPDATE_QUEUE_TID,
856 };
857 s8 sta_id;
858 int tid;
859 unsigned long tid_bitmap;
860 int ret;
861
862 lockdep_assert_held(&mvm->mutex);
863
864 spin_lock_bh(&mvm->queue_info_lock);
865 sta_id = mvm->queue_info[queue].ra_sta_id;
866 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
867 spin_unlock_bh(&mvm->queue_info_lock);
868
869 if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
870 return;
871
872 /* Find any TID for queue */
873 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
874 cmd.tid = tid;
875 cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
876
877 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
878 if (ret)
879 IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
880 queue, ret);
881 else
882 IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
883 queue, tid);
884}
885
Liad Kaufman9f9af3d2015-12-23 16:03:46 +0200886static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
887{
888 struct ieee80211_sta *sta;
889 struct iwl_mvm_sta *mvmsta;
890 s8 sta_id;
891 int tid = -1;
892 unsigned long tid_bitmap;
893 unsigned int wdg_timeout;
894 int ssn;
895 int ret = true;
896
897 lockdep_assert_held(&mvm->mutex);
898
899 spin_lock_bh(&mvm->queue_info_lock);
900 sta_id = mvm->queue_info[queue].ra_sta_id;
901 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
902 spin_unlock_bh(&mvm->queue_info_lock);
903
904 /* Find TID for queue, and make sure it is the only one on the queue */
905 tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
906 if (tid_bitmap != BIT(tid)) {
907 IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
908 queue, tid_bitmap);
909 return;
910 }
911
912 IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
913 tid);
914
915 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
916 lockdep_is_held(&mvm->mutex));
917
918 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
919 return;
920
921 mvmsta = iwl_mvm_sta_from_mac80211(sta);
922 wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
923
924 ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
925
926 ret = iwl_mvm_scd_queue_redirect(mvm, queue, tid,
927 tid_to_mac80211_ac[tid], ssn,
928 wdg_timeout, true);
929 if (ret) {
930 IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
931 return;
932 }
933
934 /* If aggs should be turned back on - do it */
935 if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
936 struct iwl_mvm_add_sta_cmd cmd;
937
938 mvmsta->tid_disable_agg &= ~BIT(tid);
939
940 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
941 cmd.sta_id = mvmsta->sta_id;
942 cmd.add_modify = STA_MODE_MODIFY;
943 cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
944 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
945 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
946
947 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
948 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
949 if (!ret) {
950 IWL_DEBUG_TX_QUEUES(mvm,
951 "TXQ #%d is now aggregated again\n",
952 queue);
953
954 /* Mark queue intenally as aggregating again */
955 iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
956 }
957 }
958
959 spin_lock_bh(&mvm->queue_info_lock);
960 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
961 spin_unlock_bh(&mvm->queue_info_lock);
962}
963
Liad Kaufman24afba72015-07-28 18:56:08 +0300964static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
965{
966 if (tid == IWL_MAX_TID_COUNT)
967 return IEEE80211_AC_VO; /* MGMT */
968
969 return tid_to_mac80211_ac[tid];
970}
971
972static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
973 struct ieee80211_sta *sta, int tid)
974{
975 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
976 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
977 struct sk_buff *skb;
978 struct ieee80211_hdr *hdr;
979 struct sk_buff_head deferred_tx;
980 u8 mac_queue;
981 bool no_queue = false; /* Marks if there is a problem with the queue */
982 u8 ac;
983
984 lockdep_assert_held(&mvm->mutex);
985
986 skb = skb_peek(&tid_data->deferred_tx_frames);
987 if (!skb)
988 return;
989 hdr = (void *)skb->data;
990
991 ac = iwl_mvm_tid_to_ac_queue(tid);
992 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
993
994 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
995 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
996 IWL_ERR(mvm,
997 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
998 mvmsta->sta_id, tid);
999
1000 /*
1001 * Mark queue as problematic so later the deferred traffic is
1002 * freed, as we can do nothing with it
1003 */
1004 no_queue = true;
1005 }
1006
1007 __skb_queue_head_init(&deferred_tx);
1008
Liad Kaufmand2515a92016-03-23 16:31:08 +02001009 /* Disable bottom-halves when entering TX path */
1010 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +03001011 spin_lock(&mvmsta->lock);
1012 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
1013 spin_unlock(&mvmsta->lock);
1014
Liad Kaufman24afba72015-07-28 18:56:08 +03001015 while ((skb = __skb_dequeue(&deferred_tx)))
1016 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
1017 ieee80211_free_txskb(mvm->hw, skb);
1018 local_bh_enable();
1019
1020 /* Wake queue */
1021 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
1022}
1023
1024void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1025{
1026 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1027 add_stream_wk);
1028 struct ieee80211_sta *sta;
1029 struct iwl_mvm_sta *mvmsta;
1030 unsigned long deferred_tid_traffic;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001031 int queue, sta_id, tid;
Liad Kaufman24afba72015-07-28 18:56:08 +03001032
Liad Kaufman9794c642015-08-19 17:34:28 +03001033 /* Check inactivity of queues */
1034 iwl_mvm_inactivity_check(mvm);
1035
Liad Kaufman24afba72015-07-28 18:56:08 +03001036 mutex_lock(&mvm->mutex);
1037
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001038 /* Reconfigure queues requiring reconfiguation */
1039 for (queue = 0; queue < IWL_MAX_HW_QUEUES; queue++) {
1040 bool reconfig;
Liad Kaufman19aefa42016-03-08 14:29:51 +02001041 bool change_owner;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001042
1043 spin_lock_bh(&mvm->queue_info_lock);
1044 reconfig = (mvm->queue_info[queue].status ==
1045 IWL_MVM_QUEUE_RECONFIGURING);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001046
1047 /*
1048 * We need to take into account a situation in which a TXQ was
1049 * allocated to TID x, and then turned shared by adding TIDs y
1050 * and z. If TID x becomes inactive and is removed from the TXQ,
1051 * ownership must be given to one of the remaining TIDs.
1052 * This is mainly because if TID x continues - a new queue can't
1053 * be allocated for it as long as it is an owner of another TXQ.
1054 */
1055 change_owner = !(mvm->queue_info[queue].tid_bitmap &
1056 BIT(mvm->queue_info[queue].txq_tid)) &&
1057 (mvm->queue_info[queue].status ==
1058 IWL_MVM_QUEUE_SHARED);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001059 spin_unlock_bh(&mvm->queue_info_lock);
1060
1061 if (reconfig)
1062 iwl_mvm_unshare_queue(mvm, queue);
Liad Kaufman19aefa42016-03-08 14:29:51 +02001063 else if (change_owner)
1064 iwl_mvm_change_queue_owner(mvm, queue);
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02001065 }
1066
Liad Kaufman24afba72015-07-28 18:56:08 +03001067 /* Go over all stations with deferred traffic */
1068 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
1069 IWL_MVM_STATION_COUNT) {
1070 clear_bit(sta_id, mvm->sta_deferred_frames);
1071 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1072 lockdep_is_held(&mvm->mutex));
1073 if (IS_ERR_OR_NULL(sta))
1074 continue;
1075
1076 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1077 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
1078
1079 for_each_set_bit(tid, &deferred_tid_traffic,
1080 IWL_MAX_TID_COUNT + 1)
1081 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
1082 }
1083
1084 mutex_unlock(&mvm->mutex);
1085}
1086
1087static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001088 struct ieee80211_sta *sta,
1089 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +03001090{
1091 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1092 int queue;
1093
Liad Kaufman9794c642015-08-19 17:34:28 +03001094 /*
1095 * Check for inactive queues, so we don't reach a situation where we
1096 * can't add a STA due to a shortage in queues that doesn't really exist
1097 */
1098 iwl_mvm_inactivity_check(mvm);
1099
Liad Kaufman24afba72015-07-28 18:56:08 +03001100 spin_lock_bh(&mvm->queue_info_lock);
1101
1102 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +03001103 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1104 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +03001105 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1106 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +03001107 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1108 else
Liad Kaufman9794c642015-08-19 17:34:28 +03001109 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1110 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +03001111 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +03001112 if (queue < 0) {
1113 spin_unlock_bh(&mvm->queue_info_lock);
1114 IWL_ERR(mvm, "No available queues for new station\n");
1115 return -ENOSPC;
1116 }
Liad Kaufmancf961e12015-08-13 19:16:08 +03001117 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +03001118
1119 spin_unlock_bh(&mvm->queue_info_lock);
1120
1121 mvmsta->reserved_queue = queue;
1122
1123 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1124 queue, mvmsta->sta_id);
1125
1126 return 0;
1127}
1128
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001129/*
1130 * In DQA mode, after a HW restart the queues should be allocated as before, in
1131 * order to avoid race conditions when there are shared queues. This function
1132 * does the re-mapping and queue allocation.
1133 *
1134 * Note that re-enabling aggregations isn't done in this function.
1135 */
1136static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1137 struct iwl_mvm_sta *mvm_sta)
1138{
1139 unsigned int wdg_timeout =
1140 iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1141 int i;
1142 struct iwl_trans_txq_scd_cfg cfg = {
1143 .sta_id = mvm_sta->sta_id,
1144 .frame_limit = IWL_FRAME_LIMIT,
1145 };
1146
1147 /* Make sure reserved queue is still marked as such (or allocated) */
1148 mvm->queue_info[mvm_sta->reserved_queue].status =
1149 IWL_MVM_QUEUE_RESERVED;
1150
1151 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1152 struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1153 int txq_id = tid_data->txq_id;
1154 int ac;
1155 u8 mac_queue;
1156
1157 if (txq_id == IEEE80211_INVAL_HW_QUEUE)
1158 continue;
1159
1160 skb_queue_head_init(&tid_data->deferred_tx_frames);
1161
1162 ac = tid_to_mac80211_ac[i];
1163 mac_queue = mvm_sta->vif->hw_queue[ac];
1164
1165 cfg.tid = i;
1166 cfg.fifo = iwl_mvm_ac_to_tx_fifo[ac];
1167 cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1168 txq_id == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1169
1170 IWL_DEBUG_TX_QUEUES(mvm,
1171 "Re-mapping sta %d tid %d to queue %d\n",
1172 mvm_sta->sta_id, i, txq_id);
1173
1174 iwl_mvm_enable_txq(mvm, txq_id, mac_queue,
1175 IEEE80211_SEQ_TO_SN(tid_data->seq_number),
1176 &cfg, wdg_timeout);
1177
1178 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1179 }
1180
1181 atomic_set(&mvm->pending_frames[mvm_sta->sta_id], 0);
1182}
1183
Johannes Berg8ca151b2013-01-24 14:25:36 +01001184int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1185 struct ieee80211_vif *vif,
1186 struct ieee80211_sta *sta)
1187{
1188 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001189 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +02001190 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001191 int i, ret, sta_id;
1192
1193 lockdep_assert_held(&mvm->mutex);
1194
1195 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +02001196 sta_id = iwl_mvm_find_free_sta_id(mvm,
1197 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001198 else
1199 sta_id = mvm_sta->sta_id;
1200
Johannes Berg36f46312015-03-10 20:32:08 +01001201 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001202 return -ENOSPC;
1203
1204 spin_lock_init(&mvm_sta->lock);
1205
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001206 /* In DQA mode, if this is a HW restart, re-alloc existing queues */
1207 if (iwl_mvm_is_dqa_supported(mvm) &&
1208 test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1209 iwl_mvm_realloc_queues_after_restart(mvm, mvm_sta);
1210 goto update_fw;
1211 }
1212
Johannes Berg8ca151b2013-01-24 14:25:36 +01001213 mvm_sta->sta_id = sta_id;
1214 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1215 mvmvif->color);
1216 mvm_sta->vif = vif;
1217 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001218 mvm_sta->tx_protection = 0;
1219 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001220
1221 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001222 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +03001223 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001224 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001225
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001226 /*
1227 * Allocate new queues for a TDLS station, unless we're in DQA mode,
1228 * and then they'll be allocated dynamically
1229 */
1230 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001231 ret = iwl_mvm_tdls_sta_init(mvm, sta);
1232 if (ret)
1233 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +03001234 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001235 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1236 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
1237 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
1238 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001240 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +03001241 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001242 u16 seq = mvm_sta->tid_data[i].seq_number;
1243 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1244 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +03001245
1246 if (!iwl_mvm_is_dqa_supported(mvm))
1247 continue;
1248
1249 /*
1250 * Mark all queues for this STA as unallocated and defer TX
1251 * frames until the queue is allocated
1252 */
1253 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1254 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001255 }
Liad Kaufman24afba72015-07-28 18:56:08 +03001256 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001257 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001258
Sara Sharona571f5f2015-12-07 12:50:58 +02001259 if (iwl_mvm_has_new_rx_api(mvm) &&
1260 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1261 dup_data = kcalloc(mvm->trans->num_rx_queues,
1262 sizeof(*dup_data),
1263 GFP_KERNEL);
1264 if (!dup_data)
1265 return -ENOMEM;
1266 mvm_sta->dup_data = dup_data;
1267 }
1268
Liad Kaufman24afba72015-07-28 18:56:08 +03001269 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +03001270 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1271 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +03001272 if (ret)
1273 goto err;
1274 }
1275
Liad Kaufman8d98ae62016-02-02 16:02:46 +02001276update_fw:
Liad Kaufman24afba72015-07-28 18:56:08 +03001277 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001278 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001279 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001280
Johannes Berg9e848012014-08-04 14:33:42 +02001281 if (vif->type == NL80211_IFTYPE_STATION) {
1282 if (!sta->tdls) {
1283 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
1284 mvmvif->ap_sta_id = sta_id;
1285 } else {
1286 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
1287 }
1288 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001289
1290 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1291
1292 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001293
1294err:
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001295 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
1296 iwl_mvm_tdls_sta_deinit(mvm, sta);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001297 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001298}
1299
Johannes Berg7a453972013-02-12 13:10:44 +01001300int iwl_mvm_update_sta(struct iwl_mvm *mvm,
1301 struct ieee80211_vif *vif,
1302 struct ieee80211_sta *sta)
1303{
Liad Kaufman24afba72015-07-28 18:56:08 +03001304 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
Johannes Berg7a453972013-02-12 13:10:44 +01001305}
1306
Johannes Berg8ca151b2013-01-24 14:25:36 +01001307int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1308 bool drain)
1309{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001310 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001311 int ret;
1312 u32 status;
1313
1314 lockdep_assert_held(&mvm->mutex);
1315
1316 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1317 cmd.sta_id = mvmsta->sta_id;
1318 cmd.add_modify = STA_MODE_MODIFY;
1319 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1320 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1321
1322 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001323 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1324 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001325 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001326 if (ret)
1327 return ret;
1328
Sara Sharon837c4da2016-01-07 16:50:45 +02001329 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001330 case ADD_STA_SUCCESS:
1331 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1332 mvmsta->sta_id);
1333 break;
1334 default:
1335 ret = -EIO;
1336 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1337 mvmsta->sta_id);
1338 break;
1339 }
1340
1341 return ret;
1342}
1343
1344/*
1345 * Remove a station from the FW table. Before sending the command to remove
1346 * the station validate that the station is indeed known to the driver (sanity
1347 * only).
1348 */
1349static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1350{
1351 struct ieee80211_sta *sta;
1352 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1353 .sta_id = sta_id,
1354 };
1355 int ret;
1356
1357 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1358 lockdep_is_held(&mvm->mutex));
1359
1360 /* Note: internal stations are marked as error values */
1361 if (!sta) {
1362 IWL_ERR(mvm, "Invalid station id\n");
1363 return -EINVAL;
1364 }
1365
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001366 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001367 sizeof(rm_sta_cmd), &rm_sta_cmd);
1368 if (ret) {
1369 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1370 return ret;
1371 }
1372
1373 return 0;
1374}
1375
1376void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1377{
1378 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1379 u8 sta_id;
1380
1381 /*
1382 * The mutex is needed because of the SYNC cmd, but not only: if the
1383 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1384 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1385 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1386 * that later.
1387 */
1388 mutex_lock(&mvm->mutex);
1389
1390 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1391 int ret;
1392 struct ieee80211_sta *sta =
1393 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1394 lockdep_is_held(&mvm->mutex));
1395
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001396 /*
1397 * This station is in use or RCU-removed; the latter happens in
1398 * managed mode, where mac80211 removes the station before we
1399 * can remove it from firmware (we can only do that after the
1400 * MAC is marked unassociated), and possibly while the deauth
1401 * frame to disconnect from the AP is still queued. Then, the
1402 * station pointer is -ENOENT when the last skb is reclaimed.
1403 */
1404 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001405 continue;
1406
1407 if (PTR_ERR(sta) == -EINVAL) {
1408 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1409 sta_id);
1410 continue;
1411 }
1412
1413 if (!sta) {
1414 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1415 sta_id);
1416 continue;
1417 }
1418
1419 WARN_ON(PTR_ERR(sta) != -EBUSY);
1420 /* This station was removed and we waited until it got drained,
1421 * we can now proceed and remove it.
1422 */
1423 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1424 if (ret) {
1425 IWL_ERR(mvm,
1426 "Couldn't remove sta %d after it was drained\n",
1427 sta_id);
1428 continue;
1429 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301430 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001431 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001432
1433 if (mvm->tfd_drained[sta_id]) {
1434 unsigned long i, msk = mvm->tfd_drained[sta_id];
1435
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001436 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001437 iwl_mvm_disable_txq(mvm, i, i,
1438 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001439
1440 mvm->tfd_drained[sta_id] = 0;
1441 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1442 sta_id, msk);
1443 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001444 }
1445
1446 mutex_unlock(&mvm->mutex);
1447}
1448
Liad Kaufman24afba72015-07-28 18:56:08 +03001449static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1450 struct ieee80211_vif *vif,
1451 struct iwl_mvm_sta *mvm_sta)
1452{
1453 int ac;
1454 int i;
1455
1456 lockdep_assert_held(&mvm->mutex);
1457
1458 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1459 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
1460 continue;
1461
1462 ac = iwl_mvm_tid_to_ac_queue(i);
1463 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1464 vif->hw_queue[ac], i, 0);
1465 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1466 }
1467}
1468
Johannes Berg8ca151b2013-01-24 14:25:36 +01001469int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1470 struct ieee80211_vif *vif,
1471 struct ieee80211_sta *sta)
1472{
1473 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001474 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001475 int ret;
1476
1477 lockdep_assert_held(&mvm->mutex);
1478
Sara Sharona571f5f2015-12-07 12:50:58 +02001479 if (iwl_mvm_has_new_rx_api(mvm))
1480 kfree(mvm_sta->dup_data);
1481
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001482 if ((vif->type == NL80211_IFTYPE_STATION &&
1483 mvmvif->ap_sta_id == mvm_sta->sta_id) ||
1484 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001485 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1486 if (ret)
1487 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001488 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +03001489 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001490 if (ret)
1491 return ret;
1492 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1493 mvm_sta->tfd_queue_msk);
1494 if (ret)
1495 return ret;
1496 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001497
Liad Kaufman24afba72015-07-28 18:56:08 +03001498 /* If DQA is supported - the queues can be disabled now */
1499 if (iwl_mvm_is_dqa_supported(mvm))
1500 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
1501
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001502 if (vif->type == NL80211_IFTYPE_STATION &&
1503 mvmvif->ap_sta_id == mvm_sta->sta_id) {
1504 /* if associated - we can't remove the AP STA now */
1505 if (vif->bss_conf.assoc)
1506 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001507
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001508 /* unassoc - go ahead - remove the AP STA now */
1509 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +02001510
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001511 /* clear d0i3_ap_sta_id if no longer relevant */
1512 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
1513 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1514 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001515 }
1516
1517 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001518 * This shouldn't happen - the TDLS channel switch should be canceled
1519 * before the STA is removed.
1520 */
1521 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
1522 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1523 cancel_delayed_work(&mvm->tdls_cs.dwork);
1524 }
1525
1526 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001527 * Make sure that the tx response code sees the station as -EBUSY and
1528 * calls the drain worker.
1529 */
1530 spin_lock_bh(&mvm_sta->lock);
1531 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001532 * There are frames pending on the AC queues for this station.
1533 * We need to wait until all the frames are drained...
1534 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001535 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001536 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1537 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001538 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001539
1540 /* disable TDLS sta queues on drain complete */
1541 if (sta->tdls) {
1542 mvm->tfd_drained[mvm_sta->sta_id] =
1543 mvm_sta->tfd_queue_msk;
1544 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
1545 mvm_sta->sta_id);
1546 }
1547
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001548 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001549 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001550 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001551
Liad Kaufmane3118ad2016-06-05 10:49:02 +03001552 if (!iwl_mvm_is_dqa_supported(mvm) && sta->tdls)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001553 iwl_mvm_tdls_sta_deinit(mvm, sta);
1554
Johannes Berg8ca151b2013-01-24 14:25:36 +01001555 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301556 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001557 }
1558
1559 return ret;
1560}
1561
1562int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1563 struct ieee80211_vif *vif,
1564 u8 sta_id)
1565{
1566 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1567
1568 lockdep_assert_held(&mvm->mutex);
1569
Monam Agarwalc531c772014-03-24 00:05:56 +05301570 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001571 return ret;
1572}
1573
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001574int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1575 struct iwl_mvm_int_sta *sta,
1576 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001577{
1578 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001579 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001580 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1581 return -ENOSPC;
1582 }
1583
1584 sta->tfd_queue_msk = qmask;
1585
1586 /* put a non-NULL value so iterating over the stations won't stop */
1587 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1588 return 0;
1589}
1590
Johannes Berg712b24a2014-08-04 14:14:14 +02001591static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1592 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001593{
Monam Agarwalc531c772014-03-24 00:05:56 +05301594 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001595 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1596 sta->sta_id = IWL_MVM_STATION_COUNT;
1597}
1598
1599static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1600 struct iwl_mvm_int_sta *sta,
1601 const u8 *addr,
1602 u16 mac_id, u16 color)
1603{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001604 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001605 int ret;
1606 u32 status;
1607
1608 lockdep_assert_held(&mvm->mutex);
1609
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001610 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001611 cmd.sta_id = sta->sta_id;
1612 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1613 color));
1614
1615 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001616 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001617
1618 if (addr)
1619 memcpy(cmd.addr, addr, ETH_ALEN);
1620
Sara Sharon854c5702016-01-26 13:17:47 +02001621 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1622 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001623 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001624 if (ret)
1625 return ret;
1626
Sara Sharon837c4da2016-01-07 16:50:45 +02001627 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001628 case ADD_STA_SUCCESS:
1629 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1630 return 0;
1631 default:
1632 ret = -EIO;
1633 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1634 status);
1635 break;
1636 }
1637 return ret;
1638}
1639
1640int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1641{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001642 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1643 mvm->cfg->base_params->wd_timeout :
1644 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001645 int ret;
1646
1647 lockdep_assert_held(&mvm->mutex);
1648
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001649 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman28d07932015-09-01 16:36:25 +03001650 if (!iwl_mvm_is_dqa_supported(mvm))
1651 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
1652 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001653
1654 /* Allocate aux station and assign to it the aux queue */
1655 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +02001656 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001657 if (ret)
1658 return ret;
1659
Liad Kaufman28d07932015-09-01 16:36:25 +03001660 if (iwl_mvm_is_dqa_supported(mvm)) {
1661 struct iwl_trans_txq_scd_cfg cfg = {
1662 .fifo = IWL_MVM_TX_FIFO_MCAST,
1663 .sta_id = mvm->aux_sta.sta_id,
1664 .tid = IWL_MAX_TID_COUNT,
1665 .aggregate = false,
1666 .frame_limit = IWL_FRAME_LIMIT,
1667 };
1668
1669 iwl_mvm_enable_txq(mvm, mvm->aux_queue, mvm->aux_queue, 0, &cfg,
1670 wdg_timeout);
1671 }
1672
Johannes Berg8ca151b2013-01-24 14:25:36 +01001673 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1674 MAC_INDEX_AUX, 0);
1675
1676 if (ret)
1677 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1678 return ret;
1679}
1680
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001681int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1682{
1683 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1684
1685 lockdep_assert_held(&mvm->mutex);
1686 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1687 mvmvif->id, 0);
1688}
1689
1690int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1691{
1692 int ret;
1693
1694 lockdep_assert_held(&mvm->mutex);
1695
1696 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1697 if (ret)
1698 IWL_WARN(mvm, "Failed sending remove station\n");
1699
1700 return ret;
1701}
1702
1703void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1704{
1705 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1706}
1707
Johannes Berg712b24a2014-08-04 14:14:14 +02001708void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1709{
1710 lockdep_assert_held(&mvm->mutex);
1711
1712 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1713}
1714
Johannes Berg8ca151b2013-01-24 14:25:36 +01001715/*
1716 * Send the add station command for the vif's broadcast station.
1717 * Assumes that the station was already allocated.
1718 *
1719 * @mvm: the mvm component
1720 * @vif: the interface to which the broadcast station is added
1721 * @bsta: the broadcast station to add.
1722 */
Johannes Berg013290a2014-08-04 13:38:48 +02001723int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001724{
1725 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001726 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001727 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001728 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001729
1730 lockdep_assert_held(&mvm->mutex);
1731
Liad Kaufmande24f632015-08-04 15:19:18 +03001732 if (iwl_mvm_is_dqa_supported(mvm)) {
1733 struct iwl_trans_txq_scd_cfg cfg = {
1734 .fifo = IWL_MVM_TX_FIFO_VO,
1735 .sta_id = mvmvif->bcast_sta.sta_id,
1736 .tid = IWL_MAX_TID_COUNT,
1737 .aggregate = false,
1738 .frame_limit = IWL_FRAME_LIMIT,
1739 };
1740 unsigned int wdg_timeout =
1741 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1742 int queue;
1743
1744 if ((vif->type == NL80211_IFTYPE_AP) &&
1745 (mvmvif->bcast_sta.tfd_queue_msk &
1746 BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1747 queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
Liad Kaufman4c965132015-08-09 19:26:56 +03001748 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1749 (mvmvif->bcast_sta.tfd_queue_msk &
1750 BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1751 queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
Liad Kaufmande24f632015-08-04 15:19:18 +03001752 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1753 return -EINVAL;
1754
1755 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1756 wdg_timeout);
1757 }
1758
Johannes Berg5023d962013-07-31 14:07:43 +02001759 if (vif->type == NL80211_IFTYPE_ADHOC)
1760 baddr = vif->bss_conf.bssid;
1761
Johannes Berg8ca151b2013-01-24 14:25:36 +01001762 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1763 return -ENOSPC;
1764
1765 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1766 mvmvif->id, mvmvif->color);
1767}
1768
1769/* Send the FW a request to remove the station from it's internal data
1770 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001771int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001772{
Johannes Berg013290a2014-08-04 13:38:48 +02001773 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001774 int ret;
1775
1776 lockdep_assert_held(&mvm->mutex);
1777
Johannes Berg013290a2014-08-04 13:38:48 +02001778 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001779 if (ret)
1780 IWL_WARN(mvm, "Failed sending remove station\n");
1781 return ret;
1782}
1783
Johannes Berg013290a2014-08-04 13:38:48 +02001784int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1785{
1786 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03001787 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02001788
1789 lockdep_assert_held(&mvm->mutex);
1790
Liad Kaufmande24f632015-08-04 15:19:18 +03001791 if (!iwl_mvm_is_dqa_supported(mvm))
1792 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001793
Liad Kaufmande24f632015-08-04 15:19:18 +03001794 if (vif->type == NL80211_IFTYPE_AP) {
1795 /*
1796 * The firmware defines the TFD queue mask to only be relevant
1797 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1798 * be included.
1799 */
Johannes Berg013290a2014-08-04 13:38:48 +02001800 qmask &= ~BIT(vif->cab_queue);
1801
Liad Kaufmande24f632015-08-04 15:19:18 +03001802 if (iwl_mvm_is_dqa_supported(mvm))
1803 qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
Liad Kaufman4c965132015-08-09 19:26:56 +03001804 } else if (iwl_mvm_is_dqa_supported(mvm) &&
1805 vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1806 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
Liad Kaufmande24f632015-08-04 15:19:18 +03001807 }
1808
Johannes Berg013290a2014-08-04 13:38:48 +02001809 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1810 ieee80211_vif_type_p2p(vif));
1811}
1812
Johannes Berg8ca151b2013-01-24 14:25:36 +01001813/* Allocate a new station entry for the broadcast station to the given vif,
1814 * and send it to the FW.
1815 * Note that each P2P mac should have its own broadcast station.
1816 *
1817 * @mvm: the mvm component
1818 * @vif: the interface to which the broadcast station is added
1819 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02001820int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001821{
1822 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001823 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001824 int ret;
1825
1826 lockdep_assert_held(&mvm->mutex);
1827
Johannes Berg013290a2014-08-04 13:38:48 +02001828 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001829 if (ret)
1830 return ret;
1831
Johannes Berg013290a2014-08-04 13:38:48 +02001832 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001833
1834 if (ret)
1835 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001836
Johannes Berg8ca151b2013-01-24 14:25:36 +01001837 return ret;
1838}
1839
Johannes Berg013290a2014-08-04 13:38:48 +02001840void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1841{
1842 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1843
1844 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1845}
1846
Johannes Berg8ca151b2013-01-24 14:25:36 +01001847/*
1848 * Send the FW a request to remove the station from it's internal data
1849 * structures, and in addition remove it from the local data structure.
1850 */
Johannes Berg013290a2014-08-04 13:38:48 +02001851int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001852{
1853 int ret;
1854
1855 lockdep_assert_held(&mvm->mutex);
1856
Johannes Berg013290a2014-08-04 13:38:48 +02001857 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001858
Johannes Berg013290a2014-08-04 13:38:48 +02001859 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1860
Johannes Berg8ca151b2013-01-24 14:25:36 +01001861 return ret;
1862}
1863
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001864#define IWL_MAX_RX_BA_SESSIONS 16
1865
Sara Sharonb915c102016-03-23 16:32:02 +02001866static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02001867{
Sara Sharonb915c102016-03-23 16:32:02 +02001868 struct iwl_mvm_delba_notif notif = {
1869 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1870 .metadata.sync = 1,
1871 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02001872 };
Sara Sharonb915c102016-03-23 16:32:02 +02001873 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
1874};
Sara Sharon10b2b202016-03-20 16:23:41 +02001875
Sara Sharonb915c102016-03-23 16:32:02 +02001876static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1877 struct iwl_mvm_baid_data *data)
1878{
1879 int i;
1880
1881 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1882
1883 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1884 int j;
1885 struct iwl_mvm_reorder_buffer *reorder_buf =
1886 &data->reorder_buf[i];
1887
Sara Sharon06904052016-02-28 20:28:17 +02001888 spin_lock_bh(&reorder_buf->lock);
1889 if (likely(!reorder_buf->num_stored)) {
1890 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02001891 continue;
Sara Sharon06904052016-02-28 20:28:17 +02001892 }
Sara Sharonb915c102016-03-23 16:32:02 +02001893
1894 /*
1895 * This shouldn't happen in regular DELBA since the internal
1896 * delBA notification should trigger a release of all frames in
1897 * the reorder buffer.
1898 */
1899 WARN_ON(1);
1900
1901 for (j = 0; j < reorder_buf->buf_size; j++)
1902 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02001903 /*
1904 * Prevent timer re-arm. This prevents a very far fetched case
1905 * where we timed out on the notification. There may be prior
1906 * RX frames pending in the RX queue before the notification
1907 * that might get processed between now and the actual deletion
1908 * and we would re-arm the timer although we are deleting the
1909 * reorder buffer.
1910 */
1911 reorder_buf->removed = true;
1912 spin_unlock_bh(&reorder_buf->lock);
1913 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02001914 }
1915}
1916
1917static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1918 u32 sta_id,
1919 struct iwl_mvm_baid_data *data,
1920 u16 ssn, u8 buf_size)
1921{
1922 int i;
1923
1924 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1925 struct iwl_mvm_reorder_buffer *reorder_buf =
1926 &data->reorder_buf[i];
1927 int j;
1928
1929 reorder_buf->num_stored = 0;
1930 reorder_buf->head_sn = ssn;
1931 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02001932 /* rx reorder timer */
1933 reorder_buf->reorder_timer.function =
1934 iwl_mvm_reorder_timer_expired;
1935 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
1936 init_timer(&reorder_buf->reorder_timer);
1937 spin_lock_init(&reorder_buf->lock);
1938 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02001939 reorder_buf->queue = i;
1940 reorder_buf->sta_id = sta_id;
1941 for (j = 0; j < reorder_buf->buf_size; j++)
1942 __skb_queue_head_init(&reorder_buf->entries[j]);
1943 }
Sara Sharon10b2b202016-03-20 16:23:41 +02001944}
1945
Johannes Berg8ca151b2013-01-24 14:25:36 +01001946int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02001947 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001948{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001949 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001950 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02001951 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001952 int ret;
1953 u32 status;
1954
1955 lockdep_assert_held(&mvm->mutex);
1956
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001957 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1958 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1959 return -ENOSPC;
1960 }
1961
Sara Sharon10b2b202016-03-20 16:23:41 +02001962 if (iwl_mvm_has_new_rx_api(mvm) && start) {
1963 /*
1964 * Allocate here so if allocation fails we can bail out early
1965 * before starting the BA session in the firmware
1966 */
Sara Sharonb915c102016-03-23 16:32:02 +02001967 baid_data = kzalloc(sizeof(*baid_data) +
1968 mvm->trans->num_rx_queues *
1969 sizeof(baid_data->reorder_buf[0]),
1970 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02001971 if (!baid_data)
1972 return -ENOMEM;
1973 }
1974
Johannes Berg8ca151b2013-01-24 14:25:36 +01001975 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1976 cmd.sta_id = mvm_sta->sta_id;
1977 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001978 if (start) {
1979 cmd.add_immediate_ba_tid = (u8) tid;
1980 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02001981 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001982 } else {
1983 cmd.remove_immediate_ba_tid = (u8) tid;
1984 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001985 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1986 STA_MODIFY_REMOVE_BA_TID;
1987
1988 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001989 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1990 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001991 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001992 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02001993 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001994
Sara Sharon837c4da2016-01-07 16:50:45 +02001995 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001996 case ADD_STA_SUCCESS:
Sara Sharon35263a02016-06-21 12:12:10 +03001997 IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
1998 start ? "start" : "stopp");
Johannes Berg8ca151b2013-01-24 14:25:36 +01001999 break;
2000 case ADD_STA_IMMEDIATE_BA_FAILURE:
2001 IWL_WARN(mvm, "RX BA Session refused by fw\n");
2002 ret = -ENOSPC;
2003 break;
2004 default:
2005 ret = -EIO;
2006 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2007 start ? "start" : "stopp", status);
2008 break;
2009 }
2010
Sara Sharon10b2b202016-03-20 16:23:41 +02002011 if (ret)
2012 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03002013
Sara Sharon10b2b202016-03-20 16:23:41 +02002014 if (start) {
2015 u8 baid;
2016
2017 mvm->rx_ba_sessions++;
2018
2019 if (!iwl_mvm_has_new_rx_api(mvm))
2020 return 0;
2021
2022 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2023 ret = -EINVAL;
2024 goto out_free;
2025 }
2026 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2027 IWL_ADD_STA_BAID_SHIFT);
2028 baid_data->baid = baid;
2029 baid_data->timeout = timeout;
2030 baid_data->last_rx = jiffies;
2031 init_timer(&baid_data->session_timer);
2032 baid_data->session_timer.function =
2033 iwl_mvm_rx_agg_session_expired;
2034 baid_data->session_timer.data =
2035 (unsigned long)&mvm->baid_map[baid];
2036 baid_data->mvm = mvm;
2037 baid_data->tid = tid;
2038 baid_data->sta_id = mvm_sta->sta_id;
2039
2040 mvm_sta->tid_to_baid[tid] = baid;
2041 if (timeout)
2042 mod_timer(&baid_data->session_timer,
2043 TU_TO_EXP_TIME(timeout * 2));
2044
Sara Sharonb915c102016-03-23 16:32:02 +02002045 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
2046 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02002047 /*
2048 * protect the BA data with RCU to cover a case where our
2049 * internal RX sync mechanism will timeout (not that it's
2050 * supposed to happen) and we will free the session data while
2051 * RX is being processed in parallel
2052 */
Sara Sharon35263a02016-06-21 12:12:10 +03002053 IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2054 mvm_sta->sta_id, tid, baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002055 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2056 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
Sara Sharon60dec522016-06-21 14:14:08 +03002057 } else {
Sara Sharon10b2b202016-03-20 16:23:41 +02002058 u8 baid = mvm_sta->tid_to_baid[tid];
2059
Sara Sharon60dec522016-06-21 14:14:08 +03002060 if (mvm->rx_ba_sessions > 0)
2061 /* check that restart flow didn't zero the counter */
2062 mvm->rx_ba_sessions--;
Sara Sharon10b2b202016-03-20 16:23:41 +02002063 if (!iwl_mvm_has_new_rx_api(mvm))
2064 return 0;
2065
2066 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2067 return -EINVAL;
2068
2069 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2070 if (WARN_ON(!baid_data))
2071 return -EINVAL;
2072
2073 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02002074 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02002075 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02002076 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2077 kfree_rcu(baid_data, rcu_head);
Sara Sharon35263a02016-06-21 12:12:10 +03002078 IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
Sara Sharon10b2b202016-03-20 16:23:41 +02002079 }
2080 return 0;
2081
2082out_free:
2083 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002084 return ret;
2085}
2086
Liad Kaufman9794c642015-08-19 17:34:28 +03002087int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2088 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002089{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01002090 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002091 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002092 int ret;
2093 u32 status;
2094
2095 lockdep_assert_held(&mvm->mutex);
2096
2097 if (start) {
2098 mvm_sta->tfd_queue_msk |= BIT(queue);
2099 mvm_sta->tid_disable_agg &= ~BIT(tid);
2100 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002101 /* In DQA-mode the queue isn't removed on agg termination */
2102 if (!iwl_mvm_is_dqa_supported(mvm))
2103 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002104 mvm_sta->tid_disable_agg |= BIT(tid);
2105 }
2106
2107 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2108 cmd.sta_id = mvm_sta->sta_id;
2109 cmd.add_modify = STA_MODE_MODIFY;
2110 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
2111 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2112 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2113
2114 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02002115 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2116 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002117 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002118 if (ret)
2119 return ret;
2120
Sara Sharon837c4da2016-01-07 16:50:45 +02002121 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002122 case ADD_STA_SUCCESS:
2123 break;
2124 default:
2125 ret = -EIO;
2126 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2127 start ? "start" : "stopp", status);
2128 break;
2129 }
2130
2131 return ret;
2132}
2133
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02002134const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002135 IEEE80211_AC_BE,
2136 IEEE80211_AC_BK,
2137 IEEE80211_AC_BK,
2138 IEEE80211_AC_BE,
2139 IEEE80211_AC_VI,
2140 IEEE80211_AC_VI,
2141 IEEE80211_AC_VO,
2142 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03002143 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01002144};
2145
Johannes Berg3e56ead2013-02-15 22:23:18 +01002146static const u8 tid_to_ucode_ac[] = {
2147 AC_BE,
2148 AC_BK,
2149 AC_BK,
2150 AC_BE,
2151 AC_VI,
2152 AC_VI,
2153 AC_VO,
2154 AC_VO,
2155};
2156
Johannes Berg8ca151b2013-01-24 14:25:36 +01002157int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2158 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2159{
Johannes Berg5b577a92013-11-14 18:20:04 +01002160 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002161 struct iwl_mvm_tid_data *tid_data;
2162 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002163 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002164
2165 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2166 return -EINVAL;
2167
2168 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2169 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
2170 mvmsta->tid_data[tid].state);
2171 return -ENXIO;
2172 }
2173
2174 lockdep_assert_held(&mvm->mutex);
2175
Arik Nemtsovb2492502014-03-13 12:21:50 +02002176 spin_lock_bh(&mvmsta->lock);
2177
2178 /* possible race condition - we entered D0i3 while starting agg */
2179 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
2180 spin_unlock_bh(&mvmsta->lock);
2181 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
2182 return -EIO;
2183 }
2184
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002185 spin_lock(&mvm->queue_info_lock);
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002186
Liad Kaufmancf961e12015-08-13 19:16:08 +03002187 /*
2188 * Note the possible cases:
2189 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
2190 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
2191 * one and mark it as reserved
2192 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
2193 * non-DQA mode, since the TXQ hasn't yet been allocated
2194 */
2195 txq_id = mvmsta->tid_data[tid].txq_id;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002196 if (iwl_mvm_is_dqa_supported(mvm) &&
2197 unlikely(mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_SHARED)) {
2198 ret = -ENXIO;
2199 IWL_DEBUG_TX_QUEUES(mvm,
2200 "Can't start tid %d agg on shared queue!\n",
2201 tid);
2202 goto release_locks;
2203 } else if (!iwl_mvm_is_dqa_supported(mvm) ||
Liad Kaufmancf961e12015-08-13 19:16:08 +03002204 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03002205 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2206 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03002207 mvm->last_agg_queue);
2208 if (txq_id < 0) {
2209 ret = txq_id;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002210 IWL_ERR(mvm, "Failed to allocate agg queue\n");
2211 goto release_locks;
2212 }
2213
2214 /* TXQ hasn't yet been enabled, so mark it only as reserved */
2215 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002216 }
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002217
2218 spin_unlock(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002219
Liad Kaufmancf961e12015-08-13 19:16:08 +03002220 IWL_DEBUG_TX_QUEUES(mvm,
2221 "AGG for tid %d will be on queue #%d\n",
2222 tid, txq_id);
2223
Johannes Berg8ca151b2013-01-24 14:25:36 +01002224 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01002225 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002226 tid_data->txq_id = txq_id;
2227 *ssn = tid_data->ssn;
2228
2229 IWL_DEBUG_TX_QUEUES(mvm,
2230 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2231 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2232 tid_data->next_reclaimed);
2233
2234 if (tid_data->ssn == tid_data->next_reclaimed) {
2235 tid_data->state = IWL_AGG_STARTING;
2236 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2237 } else {
2238 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2239 }
2240
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002241 ret = 0;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002242 goto out;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002243
2244release_locks:
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002245 spin_unlock(&mvm->queue_info_lock);
2246out:
Johannes Berg8ca151b2013-01-24 14:25:36 +01002247 spin_unlock_bh(&mvmsta->lock);
2248
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002249 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002250}
2251
2252int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002253 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
2254 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002255{
Johannes Berg5b577a92013-11-14 18:20:04 +01002256 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002257 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02002258 unsigned int wdg_timeout =
2259 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002260 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002261 bool alloc_queue = true;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002262 enum iwl_mvm_queue_status queue_status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002263 u16 ssn;
2264
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002265 struct iwl_trans_txq_scd_cfg cfg = {
2266 .sta_id = mvmsta->sta_id,
2267 .tid = tid,
2268 .frame_limit = buf_size,
2269 .aggregate = true,
2270 };
2271
Eyal Shapiraefed6642014-09-14 15:58:53 +03002272 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2273 != IWL_MAX_TID_COUNT);
2274
Johannes Berg8ca151b2013-01-24 14:25:36 +01002275 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
2276
2277 spin_lock_bh(&mvmsta->lock);
2278 ssn = tid_data->ssn;
2279 queue = tid_data->txq_id;
2280 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002281 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002282 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02002283 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002284 spin_unlock_bh(&mvmsta->lock);
2285
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02002286 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01002287
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002288 spin_lock_bh(&mvm->queue_info_lock);
2289 queue_status = mvm->queue_info[queue].status;
2290 spin_unlock_bh(&mvm->queue_info_lock);
2291
Liad Kaufmancf961e12015-08-13 19:16:08 +03002292 /* In DQA mode, the existing queue might need to be reconfigured */
2293 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmancf961e12015-08-13 19:16:08 +03002294 /* Maybe there is no need to even alloc a queue... */
2295 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2296 alloc_queue = false;
Liad Kaufmancf961e12015-08-13 19:16:08 +03002297
2298 /*
2299 * Only reconfig the SCD for the queue if the window size has
2300 * changed from current (become smaller)
2301 */
2302 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
2303 /*
2304 * If reconfiguring an existing queue, it first must be
2305 * drained
2306 */
2307 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
2308 BIT(queue));
2309 if (ret) {
2310 IWL_ERR(mvm,
2311 "Error draining queue before reconfig\n");
2312 return ret;
2313 }
2314
2315 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2316 mvmsta->sta_id, tid,
2317 buf_size, ssn);
2318 if (ret) {
2319 IWL_ERR(mvm,
2320 "Error reconfiguring TXQ #%d\n", queue);
2321 return ret;
2322 }
2323 }
2324 }
2325
2326 if (alloc_queue)
2327 iwl_mvm_enable_txq(mvm, queue,
2328 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
2329 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03002330
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002331 /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2332 if (queue_status != IWL_MVM_QUEUE_SHARED) {
2333 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2334 if (ret)
2335 return -EIO;
2336 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002337
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002338 /* No need to mark as reserved */
2339 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002340 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002341 spin_unlock_bh(&mvm->queue_info_lock);
2342
Johannes Berg8ca151b2013-01-24 14:25:36 +01002343 /*
2344 * Even though in theory the peer could have different
2345 * aggregation reorder buffer sizes for different sessions,
2346 * our ucode doesn't allow for that and has a global limit
2347 * for each station. Therefore, use the minimum of all the
2348 * aggregation sessions and our default value.
2349 */
2350 mvmsta->max_agg_bufsize =
2351 min(mvmsta->max_agg_bufsize, buf_size);
2352 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2353
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03002354 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2355 sta->addr, tid);
2356
Eyal Shapira9e680942013-11-09 00:16:16 +02002357 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002358}
2359
2360int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2361 struct ieee80211_sta *sta, u16 tid)
2362{
Johannes Berg5b577a92013-11-14 18:20:04 +01002363 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002364 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2365 u16 txq_id;
2366 int err;
2367
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02002368 /*
2369 * If mac80211 is cleaning its state, then say that we finished since
2370 * our state has been cleared anyway.
2371 */
2372 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2373 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2374 return 0;
2375 }
2376
Johannes Berg8ca151b2013-01-24 14:25:36 +01002377 spin_lock_bh(&mvmsta->lock);
2378
2379 txq_id = tid_data->txq_id;
2380
2381 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2382 mvmsta->sta_id, tid, txq_id, tid_data->state);
2383
Eyal Shapiraefed6642014-09-14 15:58:53 +03002384 mvmsta->agg_tids &= ~BIT(tid);
2385
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002386 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002387 /*
2388 * The TXQ is marked as reserved only if no traffic came through yet
2389 * This means no traffic has been sent on this TID (agg'd or not), so
2390 * we no longer have use for the queue. Since it hasn't even been
2391 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2392 * free.
2393 */
2394 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2395 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman9f9af3d2015-12-23 16:03:46 +02002396
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002397 spin_unlock_bh(&mvm->queue_info_lock);
2398
Johannes Berg8ca151b2013-01-24 14:25:36 +01002399 switch (tid_data->state) {
2400 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002401 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002402
2403 IWL_DEBUG_TX_QUEUES(mvm,
2404 "ssn = %d, next_recl = %d\n",
2405 tid_data->ssn, tid_data->next_reclaimed);
2406
2407 /* There are still packets for this RA / TID in the HW */
2408 if (tid_data->ssn != tid_data->next_reclaimed) {
2409 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2410 err = 0;
2411 break;
2412 }
2413
2414 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002415 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002416 spin_unlock_bh(&mvmsta->lock);
2417
2418 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2419
2420 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2421
Liad Kaufmancf961e12015-08-13 19:16:08 +03002422 if (!iwl_mvm_is_dqa_supported(mvm)) {
2423 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2424
2425 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2426 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002427 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002428 case IWL_AGG_STARTING:
2429 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2430 /*
2431 * The agg session has been stopped before it was set up. This
2432 * can happen when the AddBA timer times out for example.
2433 */
2434
2435 /* No barriers since we are under mutex */
2436 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002437
2438 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2439 tid_data->state = IWL_AGG_OFF;
2440 err = 0;
2441 break;
2442 default:
2443 IWL_ERR(mvm,
2444 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2445 mvmsta->sta_id, tid, tid_data->state);
2446 IWL_ERR(mvm,
2447 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2448 err = -EINVAL;
2449 }
2450
2451 spin_unlock_bh(&mvmsta->lock);
2452
2453 return err;
2454}
2455
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002456int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2457 struct ieee80211_sta *sta, u16 tid)
2458{
Johannes Berg5b577a92013-11-14 18:20:04 +01002459 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002460 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2461 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002462 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002463
2464 /*
2465 * First set the agg state to OFF to avoid calling
2466 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2467 */
2468 spin_lock_bh(&mvmsta->lock);
2469 txq_id = tid_data->txq_id;
2470 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2471 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002472 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002473 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002474 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002475 spin_unlock_bh(&mvmsta->lock);
2476
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002477 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002478 /*
2479 * The TXQ is marked as reserved only if no traffic came through yet
2480 * This means no traffic has been sent on this TID (agg'd or not), so
2481 * we no longer have use for the queue. Since it hasn't even been
2482 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2483 * free.
2484 */
2485 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2486 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002487 spin_unlock_bh(&mvm->queue_info_lock);
2488
Johannes Bergb6658ff2013-07-24 13:55:51 +02002489 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002490 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03002491 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02002492 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002493 iwl_trans_wait_tx_queue_empty(mvm->trans,
2494 mvmsta->tfd_queue_msk);
2495 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002496
Johannes Bergf7f89e72014-08-05 15:24:44 +02002497 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2498
Liad Kaufmancf961e12015-08-13 19:16:08 +03002499 if (!iwl_mvm_is_dqa_supported(mvm)) {
2500 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2501
2502 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2503 tid, 0);
2504 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002505 }
2506
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002507 return 0;
2508}
2509
Johannes Berg8ca151b2013-01-24 14:25:36 +01002510static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2511{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002512 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002513
2514 lockdep_assert_held(&mvm->mutex);
2515
Johannes Berg2dc2a152015-06-16 17:09:18 +02002516 /* Pick the unused key offset with the highest 'deleted'
2517 * counter. Every time a key is deleted, all the counters
2518 * are incremented and the one that was just deleted is
2519 * reset to zero. Thus, the highest counter is the one
2520 * that was deleted longest ago. Pick that one.
2521 */
2522 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2523 if (test_bit(i, mvm->fw_key_table))
2524 continue;
2525 if (mvm->fw_key_deleted[i] > max) {
2526 max = mvm->fw_key_deleted[i];
2527 max_offs = i;
2528 }
2529 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002530
Johannes Berg2dc2a152015-06-16 17:09:18 +02002531 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002532 return STA_KEY_IDX_INVALID;
2533
Johannes Berg2dc2a152015-06-16 17:09:18 +02002534 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002535}
2536
Johannes Berg5f7a1842015-12-11 09:36:10 +01002537static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2538 struct ieee80211_vif *vif,
2539 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002540{
Johannes Berg5b530e92014-12-23 16:00:17 +01002541 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002542
Johannes Berg5f7a1842015-12-11 09:36:10 +01002543 if (sta)
2544 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002545
2546 /*
2547 * The device expects GTKs for station interfaces to be
2548 * installed as GTKs for the AP station. If we have no
2549 * station ID, then use AP's station ID.
2550 */
2551 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02002552 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2553 u8 sta_id = mvmvif->ap_sta_id;
2554
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002555 sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
2556 lockdep_is_held(&mvm->mutex));
2557
Avri Altman9513c5e2015-10-19 16:29:11 +02002558 /*
2559 * It is possible that the 'sta' parameter is NULL,
2560 * for example when a GTK is removed - the sta_id will then
2561 * be the AP ID, and no station was passed by mac80211.
2562 */
Emmanuel Grumbach7d6a1ab2016-05-15 10:20:29 +03002563 if (IS_ERR_OR_NULL(sta))
2564 return NULL;
2565
2566 return iwl_mvm_sta_from_mac80211(sta);
Avri Altman9513c5e2015-10-19 16:29:11 +02002567 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002568
Johannes Berg5f7a1842015-12-11 09:36:10 +01002569 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002570}
2571
2572static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2573 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002574 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002575 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2576 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002577{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002578 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002579 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002580 int ret;
2581 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002582 u16 keyidx;
2583 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002584 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002585
2586 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2587 STA_KEY_FLG_KEYID_MSK;
2588 key_flags = cpu_to_le16(keyidx);
2589 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2590
2591 switch (keyconf->cipher) {
2592 case WLAN_CIPHER_SUITE_TKIP:
2593 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002594 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002595 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03002596 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2597 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002598 break;
2599 case WLAN_CIPHER_SUITE_CCMP:
2600 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002601 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002602 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002603 case WLAN_CIPHER_SUITE_WEP104:
2604 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002605 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002606 case WLAN_CIPHER_SUITE_WEP40:
2607 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2608 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2609 break;
Ayala Beker2a53d162016-04-07 16:21:57 +03002610 case WLAN_CIPHER_SUITE_GCMP_256:
2611 key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
2612 /* fall through */
2613 case WLAN_CIPHER_SUITE_GCMP:
2614 key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
2615 memcpy(cmd.key, keyconf->key, keyconf->keylen);
2616 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002617 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002618 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2619 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002620 }
2621
Johannes Bergba3943b2014-11-12 23:54:48 +01002622 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002623 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2624
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002625 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03002626 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002627 cmd.sta_id = sta_id;
2628
2629 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002630 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002631 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2632 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002633 else
2634 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2635 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002636
2637 switch (status) {
2638 case ADD_STA_SUCCESS:
2639 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2640 break;
2641 default:
2642 ret = -EIO;
2643 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2644 break;
2645 }
2646
2647 return ret;
2648}
2649
2650static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2651 struct ieee80211_key_conf *keyconf,
2652 u8 sta_id, bool remove_key)
2653{
2654 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2655
2656 /* verify the key details match the required command's expectations */
Ayala Beker8e160ab2016-04-11 11:37:38 +03002657 if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2658 (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
2659 (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
2660 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
2661 keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
2662 return -EINVAL;
2663
2664 if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
2665 keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
Johannes Berg8ca151b2013-01-24 14:25:36 +01002666 return -EINVAL;
2667
2668 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2669 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2670
2671 if (remove_key) {
2672 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2673 } else {
2674 struct ieee80211_key_seq seq;
2675 const u8 *pn;
2676
Ayala Bekeraa950522016-06-01 00:28:09 +03002677 switch (keyconf->cipher) {
2678 case WLAN_CIPHER_SUITE_AES_CMAC:
2679 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
2680 break;
Ayala Beker8e160ab2016-04-11 11:37:38 +03002681 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2682 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2683 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
2684 break;
Ayala Bekeraa950522016-06-01 00:28:09 +03002685 default:
2686 return -EINVAL;
2687 }
2688
Ayala Beker8e160ab2016-04-11 11:37:38 +03002689 memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
2690 if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
2691 igtk_cmd.ctrl_flags |=
2692 cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002693 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2694 pn = seq.aes_cmac.pn;
2695 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2696 ((u64) pn[4] << 8) |
2697 ((u64) pn[3] << 16) |
2698 ((u64) pn[2] << 24) |
2699 ((u64) pn[1] << 32) |
2700 ((u64) pn[0] << 40));
2701 }
2702
2703 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2704 remove_key ? "removing" : "installing",
2705 igtk_cmd.sta_id);
2706
Ayala Beker8e160ab2016-04-11 11:37:38 +03002707 if (!iwl_mvm_has_new_rx_api(mvm)) {
2708 struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
2709 .ctrl_flags = igtk_cmd.ctrl_flags,
2710 .key_id = igtk_cmd.key_id,
2711 .sta_id = igtk_cmd.sta_id,
2712 .receive_seq_cnt = igtk_cmd.receive_seq_cnt
2713 };
2714
2715 memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
2716 ARRAY_SIZE(igtk_cmd_v1.igtk));
2717 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
2718 sizeof(igtk_cmd_v1), &igtk_cmd_v1);
2719 }
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002720 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002721 sizeof(igtk_cmd), &igtk_cmd);
2722}
2723
2724
2725static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2726 struct ieee80211_vif *vif,
2727 struct ieee80211_sta *sta)
2728{
Johannes Berg5b530e92014-12-23 16:00:17 +01002729 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002730
2731 if (sta)
2732 return sta->addr;
2733
2734 if (vif->type == NL80211_IFTYPE_STATION &&
2735 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2736 u8 sta_id = mvmvif->ap_sta_id;
2737 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2738 lockdep_is_held(&mvm->mutex));
2739 return sta->addr;
2740 }
2741
2742
2743 return NULL;
2744}
2745
Johannes Berg2f6319d2014-11-12 23:39:56 +01002746static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2747 struct ieee80211_vif *vif,
2748 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002749 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002750 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01002751 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002752{
Johannes Berg2f6319d2014-11-12 23:39:56 +01002753 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002754 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002755 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002756 struct ieee80211_key_seq seq;
2757 u16 p1k[5];
2758
Johannes Berg8ca151b2013-01-24 14:25:36 +01002759 switch (keyconf->cipher) {
2760 case WLAN_CIPHER_SUITE_TKIP:
2761 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2762 /* get phase 1 key from mac80211 */
2763 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2764 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01002765 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002766 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002767 break;
2768 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01002769 case WLAN_CIPHER_SUITE_WEP40:
2770 case WLAN_CIPHER_SUITE_WEP104:
Ayala Beker2a53d162016-04-07 16:21:57 +03002771 case WLAN_CIPHER_SUITE_GCMP:
2772 case WLAN_CIPHER_SUITE_GCMP_256:
Johannes Bergba3943b2014-11-12 23:54:48 +01002773 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002774 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002775 break;
2776 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01002777 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002778 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002779 }
2780
Johannes Berg8ca151b2013-01-24 14:25:36 +01002781 return ret;
2782}
2783
Johannes Berg2f6319d2014-11-12 23:39:56 +01002784static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01002785 struct ieee80211_key_conf *keyconf,
2786 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002787{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002788 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002789 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002790 int ret;
2791 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002792
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02002793 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2794 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002795 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2796 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2797
Johannes Bergba3943b2014-11-12 23:54:48 +01002798 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002799 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2800
Max Stepanov5a258aa2013-04-07 09:11:21 +03002801 cmd.key_flags = key_flags;
2802 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002803 cmd.sta_id = sta_id;
2804
Johannes Berg8ca151b2013-01-24 14:25:36 +01002805 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002806 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2807 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002808
2809 switch (status) {
2810 case ADD_STA_SUCCESS:
2811 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2812 break;
2813 default:
2814 ret = -EIO;
2815 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2816 break;
2817 }
2818
2819 return ret;
2820}
2821
Johannes Berg2f6319d2014-11-12 23:39:56 +01002822int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2823 struct ieee80211_vif *vif,
2824 struct ieee80211_sta *sta,
2825 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002826 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002827{
Johannes Bergba3943b2014-11-12 23:54:48 +01002828 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002829 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002830 u8 sta_id;
2831 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03002832 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01002833
2834 lockdep_assert_held(&mvm->mutex);
2835
2836 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01002837 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2838 if (!mvm_sta) {
2839 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01002840 return -EINVAL;
2841 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01002842 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002843
Ayala Beker8e160ab2016-04-11 11:37:38 +03002844 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2845 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2846 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01002847 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2848 goto end;
2849 }
2850
2851 /*
2852 * It is possible that the 'sta' parameter is NULL, and thus
2853 * there is a need to retrieve the sta from the local station table.
2854 */
2855 if (!sta) {
2856 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2857 lockdep_is_held(&mvm->mutex));
2858 if (IS_ERR_OR_NULL(sta)) {
2859 IWL_ERR(mvm, "Invalid station id\n");
2860 return -EINVAL;
2861 }
2862 }
2863
2864 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2865 return -EINVAL;
2866
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002867 /* If the key_offset is not pre-assigned, we need to find a
2868 * new offset to use. In normal cases, the offset is not
2869 * pre-assigned, but during HW_RESTART we want to reuse the
2870 * same indices, so we pass them when this function is called.
2871 *
2872 * In D3 entry, we need to hardcoded the indices (because the
2873 * firmware hardcodes the PTK offset to 0). In this case, we
2874 * need to make sure we don't overwrite the hw_key_idx in the
2875 * keyconf structure, because otherwise we cannot configure
2876 * the original ones back when resuming.
2877 */
2878 if (key_offset == STA_KEY_IDX_INVALID) {
2879 key_offset = iwl_mvm_set_fw_key_idx(mvm);
2880 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002881 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002882 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002883 }
2884
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002885 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002886 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01002887 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002888
2889 /*
2890 * For WEP, the same key is used for multicast and unicast. Upload it
2891 * again, using the same key offset, and now pointing the other one
2892 * to the same key slot (offset).
2893 * If this fails, remove the original as well.
2894 */
2895 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2896 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002897 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
2898 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01002899 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01002900 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002901 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002902 }
2903 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01002904
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002905 __set_bit(key_offset, mvm->fw_key_table);
2906
Johannes Berg2f6319d2014-11-12 23:39:56 +01002907end:
2908 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2909 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03002910 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002911 return ret;
2912}
2913
2914int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2915 struct ieee80211_vif *vif,
2916 struct ieee80211_sta *sta,
2917 struct ieee80211_key_conf *keyconf)
2918{
Johannes Bergba3943b2014-11-12 23:54:48 +01002919 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002920 struct iwl_mvm_sta *mvm_sta;
2921 u8 sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg2dc2a152015-06-16 17:09:18 +02002922 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002923
2924 lockdep_assert_held(&mvm->mutex);
2925
Johannes Berg5f7a1842015-12-11 09:36:10 +01002926 /* Get the station from the mvm local station table */
2927 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002928
2929 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2930 keyconf->keyidx, sta_id);
2931
Ayala Beker8e160ab2016-04-11 11:37:38 +03002932 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2933 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2934 keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002935 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2936
2937 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2938 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2939 keyconf->hw_key_idx);
2940 return -ENOENT;
2941 }
2942
Johannes Berg2dc2a152015-06-16 17:09:18 +02002943 /* track which key was deleted last */
2944 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2945 if (mvm->fw_key_deleted[i] < U8_MAX)
2946 mvm->fw_key_deleted[i]++;
2947 }
2948 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2949
Johannes Berg5f7a1842015-12-11 09:36:10 +01002950 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01002951 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2952 return 0;
2953 }
2954
Johannes Berg5f7a1842015-12-11 09:36:10 +01002955 sta_id = mvm_sta->sta_id;
2956
Johannes Bergba3943b2014-11-12 23:54:48 +01002957 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2958 if (ret)
2959 return ret;
2960
2961 /* delete WEP key twice to get rid of (now useless) offset */
2962 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2963 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2964 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2965
2966 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002967}
2968
Johannes Berg8ca151b2013-01-24 14:25:36 +01002969void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2970 struct ieee80211_vif *vif,
2971 struct ieee80211_key_conf *keyconf,
2972 struct ieee80211_sta *sta, u32 iv32,
2973 u16 *phase1key)
2974{
Beni Levc3eb5362013-02-06 17:22:18 +02002975 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01002976 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002977
Beni Levc3eb5362013-02-06 17:22:18 +02002978 rcu_read_lock();
2979
Johannes Berg5f7a1842015-12-11 09:36:10 +01002980 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2981 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002982 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01002983 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002984 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002985
2986 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02002987 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01002988}
2989
Johannes Berg9cc40712013-02-15 22:47:48 +01002990void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
2991 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002992{
Johannes Berg5b577a92013-11-14 18:20:04 +01002993 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002994 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002995 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002996 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03002997 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01002998 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002999 };
3000 int ret;
3001
Sara Sharon854c5702016-01-26 13:17:47 +02003002 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3003 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003004 if (ret)
3005 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3006}
3007
Johannes Berg9cc40712013-02-15 22:47:48 +01003008void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3009 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003010 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01003011 u16 cnt, u16 tids, bool more_data,
3012 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01003013{
Johannes Berg5b577a92013-11-14 18:20:04 +01003014 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03003015 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01003016 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01003017 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01003018 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3019 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01003020 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01003021 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01003022 int tid, ret;
3023 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01003024
Johannes Berg3e56ead2013-02-15 22:23:18 +01003025 /* convert TIDs to ACs - we don't support TSPEC so that's OK
3026 * Note that this field is reserved and unused by firmware not
3027 * supporting GO uAPSD, so it's safe to always do this.
3028 */
3029 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3030 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3031
3032 /* If we're releasing frames from aggregation queues then check if the
3033 * all queues combined that we're releasing frames from have
3034 * - more frames than the service period, in which case more_data
3035 * needs to be set
3036 * - fewer than 'cnt' frames, in which case we need to adjust the
3037 * firmware command (but do that unconditionally)
3038 */
3039 if (agg) {
3040 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003041 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003042
3043 spin_lock_bh(&mvmsta->lock);
3044 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3045 struct iwl_mvm_tid_data *tid_data;
3046 u16 n_queued;
3047
3048 tid_data = &mvmsta->tid_data[tid];
3049 if (WARN(tid_data->state != IWL_AGG_ON &&
3050 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
3051 "TID %d state is %d\n",
3052 tid, tid_data->state)) {
3053 spin_unlock_bh(&mvmsta->lock);
3054 ieee80211_sta_eosp(sta);
3055 return;
3056 }
3057
3058 n_queued = iwl_mvm_tid_queued(tid_data);
3059 if (n_queued > remaining) {
3060 more_data = true;
3061 remaining = 0;
3062 break;
3063 }
3064 remaining -= n_queued;
3065 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003066 sleep_tx_count = cnt - remaining;
3067 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3068 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003069 spin_unlock_bh(&mvmsta->lock);
3070
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02003071 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01003072 if (WARN_ON(cnt - remaining == 0)) {
3073 ieee80211_sta_eosp(sta);
3074 return;
3075 }
3076 }
3077
3078 /* Note: this is ignored by firmware not supporting GO uAPSD */
3079 if (more_data)
3080 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
3081
3082 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3083 mvmsta->next_status_eosp = true;
3084 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
3085 } else {
3086 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
3087 }
3088
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02003089 /* block the Tx queues until the FW updated the sleep Tx count */
3090 iwl_trans_block_txq_ptrs(mvm->trans, true);
3091
3092 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3093 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02003094 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01003095 if (ret)
3096 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3097}
Johannes Berg3e56ead2013-02-15 22:23:18 +01003098
Johannes Berg04168412015-06-23 21:22:09 +02003099void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3100 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01003101{
3102 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3103 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3104 struct ieee80211_sta *sta;
3105 u32 sta_id = le32_to_cpu(notif->sta_id);
3106
3107 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02003108 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01003109
3110 rcu_read_lock();
3111 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3112 if (!IS_ERR_OR_NULL(sta))
3113 ieee80211_sta_eosp(sta);
3114 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01003115}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003116
3117void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3118 struct iwl_mvm_sta *mvmsta, bool disable)
3119{
3120 struct iwl_mvm_add_sta_cmd cmd = {
3121 .add_modify = STA_MODE_MODIFY,
3122 .sta_id = mvmsta->sta_id,
3123 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3124 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3125 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3126 };
3127 int ret;
3128
Sara Sharon854c5702016-01-26 13:17:47 +02003129 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3130 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03003131 if (ret)
3132 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3133}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003134
3135void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3136 struct ieee80211_sta *sta,
3137 bool disable)
3138{
3139 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3140
3141 spin_lock_bh(&mvm_sta->lock);
3142
3143 if (mvm_sta->disable_tx == disable) {
3144 spin_unlock_bh(&mvm_sta->lock);
3145 return;
3146 }
3147
3148 mvm_sta->disable_tx = disable;
3149
3150 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03003151 * Tell mac80211 to start/stop queuing tx for this station,
3152 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03003153 * for this station.
3154 */
3155 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
3156 ieee80211_sta_block_awake(mvm->hw, sta, disable);
3157
3158 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3159
3160 spin_unlock_bh(&mvm_sta->lock);
3161}
3162
3163void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3164 struct iwl_mvm_vif *mvmvif,
3165 bool disable)
3166{
3167 struct ieee80211_sta *sta;
3168 struct iwl_mvm_sta *mvm_sta;
3169 int i;
3170
3171 lockdep_assert_held(&mvm->mutex);
3172
3173 /* Block/unblock all the stations of the given mvmvif */
3174 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
3175 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3176 lockdep_is_held(&mvm->mutex));
3177 if (IS_ERR_OR_NULL(sta))
3178 continue;
3179
3180 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3181 if (mvm_sta->mac_id_n_color !=
3182 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3183 continue;
3184
3185 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3186 }
3187}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02003188
3189void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3190{
3191 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3192 struct iwl_mvm_sta *mvmsta;
3193
3194 rcu_read_lock();
3195
3196 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3197
3198 if (!WARN_ON(!mvmsta))
3199 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3200
3201 rcu_read_unlock();
3202}