blob: 14c06cbe861f882807e6e0ffc257a06c111b2d6b [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03008 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Sara Sharon854c5702016-01-26 13:17:47 +020010 * Copyright(c) 2016 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * USA
25 *
26 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020027 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010028 *
29 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020030 * Intel Linux Wireless <linuxwifi@intel.com>
Johannes Berg8ca151b2013-01-24 14:25:36 +010031 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +030035 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
Sara Sharon854c5702016-01-26 13:17:47 +020037 * Copyright(c) 2016 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010038 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
49 * distribution.
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *****************************************************************************/
67#include <net/mac80211.h>
68
69#include "mvm.h"
70#include "sta.h"
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +030071#include "rs.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010072
Sara Sharon854c5702016-01-26 13:17:47 +020073/*
74 * New version of ADD_STA_sta command added new fields at the end of the
75 * structure, so sending the size of the relevant API's structure is enough to
76 * support both API versions.
77 */
78static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
79{
80 return iwl_mvm_has_new_rx_api(mvm) ?
81 sizeof(struct iwl_mvm_add_sta_cmd) :
82 sizeof(struct iwl_mvm_add_sta_cmd_v7);
83}
84
Eliad Pellerb92e6612014-01-23 17:58:23 +020085static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
86 enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +010087{
88 int sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +020089 u32 reserved_ids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +010090
Eliad Pellerb92e6612014-01-23 17:58:23 +020091 BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
Johannes Berg8ca151b2013-01-24 14:25:36 +010092 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
93
94 lockdep_assert_held(&mvm->mutex);
95
Eliad Pellerb92e6612014-01-23 17:58:23 +020096 /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
97 if (iftype != NL80211_IFTYPE_STATION)
98 reserved_ids = BIT(0);
99
Johannes Berg8ca151b2013-01-24 14:25:36 +0100100 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
Eliad Pellerb92e6612014-01-23 17:58:23 +0200101 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
102 if (BIT(sta_id) & reserved_ids)
103 continue;
104
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
106 lockdep_is_held(&mvm->mutex)))
107 return sta_id;
Eliad Pellerb92e6612014-01-23 17:58:23 +0200108 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100109 return IWL_MVM_STATION_COUNT;
110}
111
Johannes Berg7a453972013-02-12 13:10:44 +0100112/* send station add/update command to firmware */
113int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Liad Kaufman24afba72015-07-28 18:56:08 +0300114 bool update, unsigned int flags)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100115{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100116 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300117 struct iwl_mvm_add_sta_cmd add_sta_cmd = {
118 .sta_id = mvm_sta->sta_id,
119 .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
120 .add_modify = update ? 1 : 0,
121 .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
122 STA_FLG_MIMO_EN_MSK),
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200123 .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
Emmanuel Grumbach4b8265a2014-07-13 08:58:04 +0300124 };
Johannes Berg8ca151b2013-01-24 14:25:36 +0100125 int ret;
126 u32 status;
127 u32 agg_size = 0, mpdu_dens = 0;
128
Liad Kaufman24afba72015-07-28 18:56:08 +0300129 if (!update || (flags & STA_MODIFY_QUEUES)) {
Johannes Berg7a453972013-02-12 13:10:44 +0100130 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
131 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
Liad Kaufman24afba72015-07-28 18:56:08 +0300132
133 if (flags & STA_MODIFY_QUEUES)
134 add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
Johannes Berg7a453972013-02-12 13:10:44 +0100135 }
Johannes Berg5bc5aaa2013-02-12 14:35:36 +0100136
137 switch (sta->bandwidth) {
138 case IEEE80211_STA_RX_BW_160:
139 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
140 /* fall through */
141 case IEEE80211_STA_RX_BW_80:
142 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
143 /* fall through */
144 case IEEE80211_STA_RX_BW_40:
145 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
146 /* fall through */
147 case IEEE80211_STA_RX_BW_20:
148 if (sta->ht_cap.ht_supported)
149 add_sta_cmd.station_flags |=
150 cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
151 break;
152 }
153
154 switch (sta->rx_nss) {
155 case 1:
156 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
157 break;
158 case 2:
159 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
160 break;
161 case 3 ... 8:
162 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
163 break;
164 }
165
166 switch (sta->smps_mode) {
167 case IEEE80211_SMPS_AUTOMATIC:
168 case IEEE80211_SMPS_NUM_MODES:
169 WARN_ON(1);
170 break;
171 case IEEE80211_SMPS_STATIC:
172 /* override NSS */
173 add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
174 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
175 break;
176 case IEEE80211_SMPS_DYNAMIC:
177 add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
178 break;
179 case IEEE80211_SMPS_OFF:
180 /* nothing */
181 break;
182 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100183
184 if (sta->ht_cap.ht_supported) {
185 add_sta_cmd.station_flags_msk |=
186 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
187 STA_FLG_AGG_MPDU_DENS_MSK);
188
189 mpdu_dens = sta->ht_cap.ampdu_density;
190 }
191
192 if (sta->vht_cap.vht_supported) {
193 agg_size = sta->vht_cap.cap &
194 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
195 agg_size >>=
196 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
197 } else if (sta->ht_cap.ht_supported) {
198 agg_size = sta->ht_cap.ampdu_factor;
199 }
200
201 add_sta_cmd.station_flags |=
202 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
203 add_sta_cmd.station_flags |=
204 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
205
206 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200207 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
208 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300209 &add_sta_cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100210 if (ret)
211 return ret;
212
Sara Sharon837c4da2016-01-07 16:50:45 +0200213 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100214 case ADD_STA_SUCCESS:
215 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
216 break;
217 default:
218 ret = -EIO;
219 IWL_ERR(mvm, "ADD_STA failed\n");
220 break;
221 }
222
223 return ret;
224}
225
Sara Sharon10b2b202016-03-20 16:23:41 +0200226static void iwl_mvm_rx_agg_session_expired(unsigned long data)
227{
228 struct iwl_mvm_baid_data __rcu **rcu_ptr = (void *)data;
229 struct iwl_mvm_baid_data *ba_data;
230 struct ieee80211_sta *sta;
231 struct iwl_mvm_sta *mvm_sta;
232 unsigned long timeout;
233
234 rcu_read_lock();
235
236 ba_data = rcu_dereference(*rcu_ptr);
237
238 if (WARN_ON(!ba_data))
239 goto unlock;
240
241 if (!ba_data->timeout)
242 goto unlock;
243
244 timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
245 if (time_is_after_jiffies(timeout)) {
246 mod_timer(&ba_data->session_timer, timeout);
247 goto unlock;
248 }
249
250 /* Timer expired */
251 sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
252 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
253 ieee80211_stop_rx_ba_session_offl(mvm_sta->vif,
254 sta->addr, ba_data->tid);
255unlock:
256 rcu_read_unlock();
257}
258
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300259static int iwl_mvm_tdls_sta_init(struct iwl_mvm *mvm,
260 struct ieee80211_sta *sta)
261{
262 unsigned long used_hw_queues;
263 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200264 unsigned int wdg_timeout =
265 iwl_mvm_get_wd_timeout(mvm, NULL, true, false);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300266 u32 ac;
267
268 lockdep_assert_held(&mvm->mutex);
269
270 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, NULL);
271
272 /* Find available queues, and allocate them to the ACs */
273 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
274 u8 queue = find_first_zero_bit(&used_hw_queues,
275 mvm->first_agg_queue);
276
277 if (queue >= mvm->first_agg_queue) {
278 IWL_ERR(mvm, "Failed to allocate STA queue\n");
279 return -EBUSY;
280 }
281
282 __set_bit(queue, &used_hw_queues);
283 mvmsta->hw_queue[ac] = queue;
284 }
285
286 /* Found a place for all queues - enable them */
287 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
288 iwl_mvm_enable_ac_txq(mvm, mvmsta->hw_queue[ac],
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300289 mvmsta->hw_queue[ac],
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300290 iwl_mvm_ac_to_tx_fifo[ac], 0,
291 wdg_timeout);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300292 mvmsta->tfd_queue_msk |= BIT(mvmsta->hw_queue[ac]);
293 }
294
295 return 0;
296}
297
298static void iwl_mvm_tdls_sta_deinit(struct iwl_mvm *mvm,
299 struct ieee80211_sta *sta)
300{
301 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
302 unsigned long sta_msk;
303 int i;
304
305 lockdep_assert_held(&mvm->mutex);
306
307 /* disable the TDLS STA-specific queues */
308 sta_msk = mvmsta->tfd_queue_msk;
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200309 for_each_set_bit(i, &sta_msk, sizeof(sta_msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300310 iwl_mvm_disable_txq(mvm, i, i, IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300311}
312
Liad Kaufman9794c642015-08-19 17:34:28 +0300313/* Disable aggregations for a bitmap of TIDs for a given station */
314static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
315 unsigned long disable_agg_tids,
316 bool remove_queue)
317{
318 struct iwl_mvm_add_sta_cmd cmd = {};
319 struct ieee80211_sta *sta;
320 struct iwl_mvm_sta *mvmsta;
321 u32 status;
322 u8 sta_id;
323 int ret;
324
325 spin_lock_bh(&mvm->queue_info_lock);
326 sta_id = mvm->queue_info[queue].ra_sta_id;
327 spin_unlock_bh(&mvm->queue_info_lock);
328
329 rcu_read_lock();
330
331 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
332
333 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
334 rcu_read_unlock();
335 return -EINVAL;
336 }
337
338 mvmsta = iwl_mvm_sta_from_mac80211(sta);
339
340 mvmsta->tid_disable_agg |= disable_agg_tids;
341
342 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
343 cmd.sta_id = mvmsta->sta_id;
344 cmd.add_modify = STA_MODE_MODIFY;
345 cmd.modify_mask = STA_MODIFY_QUEUES;
346 if (disable_agg_tids)
347 cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
348 if (remove_queue)
349 cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
350 cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
351 cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
352
353 rcu_read_unlock();
354
355 /* Notify FW of queue removal from the STA queues */
356 status = ADD_STA_SUCCESS;
357 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
358 iwl_mvm_add_sta_cmd_size(mvm),
359 &cmd, &status);
360
361 return ret;
362}
363
Liad Kaufman42db09c2016-05-02 14:01:14 +0300364static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
365{
366 struct ieee80211_sta *sta;
367 struct iwl_mvm_sta *mvmsta;
368 unsigned long tid_bitmap;
369 unsigned long agg_tids = 0;
370 s8 sta_id;
371 int tid;
372
373 lockdep_assert_held(&mvm->mutex);
374
375 spin_lock_bh(&mvm->queue_info_lock);
376 sta_id = mvm->queue_info[queue].ra_sta_id;
377 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
378 spin_unlock_bh(&mvm->queue_info_lock);
379
380 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
381 lockdep_is_held(&mvm->mutex));
382
383 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
384 return -EINVAL;
385
386 mvmsta = iwl_mvm_sta_from_mac80211(sta);
387
388 spin_lock_bh(&mvmsta->lock);
389 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
390 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
391 agg_tids |= BIT(tid);
392 }
393 spin_unlock_bh(&mvmsta->lock);
394
395 return agg_tids;
396}
397
Liad Kaufman9794c642015-08-19 17:34:28 +0300398/*
399 * Remove a queue from a station's resources.
400 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
401 * doesn't disable the queue
402 */
403static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
404{
405 struct ieee80211_sta *sta;
406 struct iwl_mvm_sta *mvmsta;
407 unsigned long tid_bitmap;
408 unsigned long disable_agg_tids = 0;
409 u8 sta_id;
410 int tid;
411
412 lockdep_assert_held(&mvm->mutex);
413
414 spin_lock_bh(&mvm->queue_info_lock);
415 sta_id = mvm->queue_info[queue].ra_sta_id;
416 tid_bitmap = mvm->queue_info[queue].tid_bitmap;
417 spin_unlock_bh(&mvm->queue_info_lock);
418
419 rcu_read_lock();
420
421 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
422
423 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
424 rcu_read_unlock();
425 return 0;
426 }
427
428 mvmsta = iwl_mvm_sta_from_mac80211(sta);
429
430 spin_lock_bh(&mvmsta->lock);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300431 /* Unmap MAC queues and TIDs from this queue */
Liad Kaufman9794c642015-08-19 17:34:28 +0300432 for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300433 if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
434 disable_agg_tids |= BIT(tid);
Liad Kaufman42db09c2016-05-02 14:01:14 +0300435 mvmsta->tid_data[tid].txq_id = IEEE80211_INVAL_HW_QUEUE;
Liad Kaufman9794c642015-08-19 17:34:28 +0300436 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300437
Liad Kaufman42db09c2016-05-02 14:01:14 +0300438 mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
Liad Kaufman9794c642015-08-19 17:34:28 +0300439 spin_unlock_bh(&mvmsta->lock);
440
441 rcu_read_unlock();
442
Liad Kaufman42db09c2016-05-02 14:01:14 +0300443 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman9794c642015-08-19 17:34:28 +0300444 /* Unmap MAC queues and TIDs from this queue */
445 mvm->queue_info[queue].hw_queue_to_mac80211 = 0;
446 mvm->queue_info[queue].hw_queue_refcount = 0;
447 mvm->queue_info[queue].tid_bitmap = 0;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300448 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman9794c642015-08-19 17:34:28 +0300449
450 return disable_agg_tids;
451}
452
Liad Kaufman42db09c2016-05-02 14:01:14 +0300453static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
454 unsigned long tfd_queue_mask, u8 ac)
455{
456 int queue = 0;
457 u8 ac_to_queue[IEEE80211_NUM_ACS];
458 int i;
459
460 lockdep_assert_held(&mvm->queue_info_lock);
461
462 memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
463
464 /* See what ACs the existing queues for this STA have */
465 for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
466 /* Only DATA queues can be shared */
467 if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
468 i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
469 continue;
470
471 ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
472 }
473
474 /*
475 * The queue to share is chosen only from DATA queues as follows (in
476 * descending priority):
477 * 1. An AC_BE queue
478 * 2. Same AC queue
479 * 3. Highest AC queue that is lower than new AC
480 * 4. Any existing AC (there always is at least 1 DATA queue)
481 */
482
483 /* Priority 1: An AC_BE queue */
484 if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
485 queue = ac_to_queue[IEEE80211_AC_BE];
486 /* Priority 2: Same AC queue */
487 else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
488 queue = ac_to_queue[ac];
489 /* Priority 3a: If new AC is VO and VI exists - use VI */
490 else if (ac == IEEE80211_AC_VO &&
491 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
492 queue = ac_to_queue[IEEE80211_AC_VI];
493 /* Priority 3b: No BE so only AC less than the new one is BK */
494 else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
495 queue = ac_to_queue[IEEE80211_AC_BK];
496 /* Priority 4a: No BE nor BK - use VI if exists */
497 else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
498 queue = ac_to_queue[IEEE80211_AC_VI];
499 /* Priority 4b: No BE, BK nor VI - use VO if exists */
500 else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
501 queue = ac_to_queue[IEEE80211_AC_VO];
502
503 /* Make sure queue found (or not) is legal */
504 if (!((queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE &&
505 queue <= IWL_MVM_DQA_MAX_MGMT_QUEUE) ||
506 (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE &&
507 queue <= IWL_MVM_DQA_MAX_DATA_QUEUE) ||
508 (queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE))) {
509 IWL_ERR(mvm, "No DATA queues available to share\n");
510 queue = -ENOSPC;
511 }
512
513 return queue;
514}
515
Liad Kaufman24afba72015-07-28 18:56:08 +0300516static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
517 struct ieee80211_sta *sta, u8 ac, int tid,
518 struct ieee80211_hdr *hdr)
519{
520 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
521 struct iwl_trans_txq_scd_cfg cfg = {
522 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
523 .sta_id = mvmsta->sta_id,
524 .tid = tid,
525 .frame_limit = IWL_FRAME_LIMIT,
526 };
527 unsigned int wdg_timeout =
528 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
529 u8 mac_queue = mvmsta->vif->hw_queue[ac];
530 int queue = -1;
Liad Kaufman9794c642015-08-19 17:34:28 +0300531 bool using_inactive_queue = false;
532 unsigned long disable_agg_tids = 0;
533 enum iwl_mvm_agg_state queue_state;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300534 bool shared_queue = false;
Liad Kaufman24afba72015-07-28 18:56:08 +0300535 int ssn;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300536 unsigned long tfd_queue_mask;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300537 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300538
539 lockdep_assert_held(&mvm->mutex);
540
Liad Kaufman42db09c2016-05-02 14:01:14 +0300541 spin_lock_bh(&mvmsta->lock);
542 tfd_queue_mask = mvmsta->tfd_queue_msk;
543 spin_unlock_bh(&mvmsta->lock);
544
Liad Kaufmand2515a92016-03-23 16:31:08 +0200545 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300546
547 /*
548 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
549 * exists
550 */
551 if (!ieee80211_is_data_qos(hdr->frame_control) ||
552 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
Liad Kaufman9794c642015-08-19 17:34:28 +0300553 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
554 IWL_MVM_DQA_MIN_MGMT_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300555 IWL_MVM_DQA_MAX_MGMT_QUEUE);
556 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
557 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
558 queue);
559
560 /* If no such queue is found, we'll use a DATA queue instead */
561 }
562
Liad Kaufman9794c642015-08-19 17:34:28 +0300563 if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
564 (mvm->queue_info[mvmsta->reserved_queue].status ==
565 IWL_MVM_QUEUE_RESERVED ||
566 mvm->queue_info[mvmsta->reserved_queue].status ==
567 IWL_MVM_QUEUE_INACTIVE)) {
Liad Kaufman24afba72015-07-28 18:56:08 +0300568 queue = mvmsta->reserved_queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300569 mvm->queue_info[queue].reserved = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300570 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
571 }
572
573 if (queue < 0)
Liad Kaufman9794c642015-08-19 17:34:28 +0300574 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
575 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufman24afba72015-07-28 18:56:08 +0300576 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300577
578 /*
Liad Kaufman9794c642015-08-19 17:34:28 +0300579 * Check if this queue is already allocated but inactive.
580 * In such a case, we'll need to first free this queue before enabling
581 * it again, so we'll mark it as reserved to make sure no new traffic
582 * arrives on it
583 */
584 if (queue > 0 &&
585 mvm->queue_info[queue].status == IWL_MVM_QUEUE_INACTIVE) {
586 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
587 using_inactive_queue = true;
588 IWL_DEBUG_TX_QUEUES(mvm,
589 "Re-assigning TXQ %d: sta_id=%d, tid=%d\n",
590 queue, mvmsta->sta_id, tid);
591 }
592
Liad Kaufman42db09c2016-05-02 14:01:14 +0300593 /* No free queue - we'll have to share */
594 if (queue <= 0) {
595 queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
596 if (queue > 0) {
597 shared_queue = true;
598 mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
599 }
600 }
601
Liad Kaufman9794c642015-08-19 17:34:28 +0300602 /*
Liad Kaufmancf961e12015-08-13 19:16:08 +0300603 * Mark TXQ as ready, even though it hasn't been fully configured yet,
604 * to make sure no one else takes it.
605 * This will allow avoiding re-acquiring the lock at the end of the
606 * configuration. On error we'll mark it back as free.
607 */
Liad Kaufman42db09c2016-05-02 14:01:14 +0300608 if ((queue > 0) && !shared_queue)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300609 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300610
Liad Kaufmand2515a92016-03-23 16:31:08 +0200611 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300612
Liad Kaufman42db09c2016-05-02 14:01:14 +0300613 /* This shouldn't happen - out of queues */
614 if (WARN_ON(queue <= 0)) {
615 IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
616 tid, cfg.sta_id);
Liad Kaufman24afba72015-07-28 18:56:08 +0300617 return -ENOSPC;
Liad Kaufman42db09c2016-05-02 14:01:14 +0300618 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300619
620 /*
621 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
622 * but for configuring the SCD to send A-MPDUs we need to mark the queue
623 * as aggregatable.
624 * Mark all DATA queues as allowing to be aggregated at some point
625 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300626 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
627 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300628
Liad Kaufman9794c642015-08-19 17:34:28 +0300629 /*
630 * If this queue was previously inactive (idle) - we need to free it
631 * first
632 */
633 if (using_inactive_queue) {
634 struct iwl_scd_txq_cfg_cmd cmd = {
635 .scd_queue = queue,
636 .enable = 0,
637 };
638
639 disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
640
641 /* Disable the queue */
642 iwl_mvm_invalidate_sta_queue(mvm, queue, disable_agg_tids,
643 true);
644 iwl_trans_txq_disable(mvm->trans, queue, false);
645 ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd),
646 &cmd);
647 if (ret) {
648 IWL_ERR(mvm,
649 "Failed to free inactive queue %d (ret=%d)\n",
650 queue, ret);
651
652 /* Re-mark the inactive queue as inactive */
653 spin_lock_bh(&mvm->queue_info_lock);
654 mvm->queue_info[queue].status = IWL_MVM_QUEUE_INACTIVE;
655 spin_unlock_bh(&mvm->queue_info_lock);
656
657 return ret;
658 }
659 }
660
Liad Kaufman42db09c2016-05-02 14:01:14 +0300661 IWL_DEBUG_TX_QUEUES(mvm,
662 "Allocating %squeue #%d to sta %d on tid %d\n",
663 shared_queue ? "shared " : "", queue,
664 mvmsta->sta_id, tid);
665
666 if (shared_queue) {
667 /* Disable any open aggs on this queue */
668 disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
669
670 if (disable_agg_tids) {
671 IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
672 queue);
673 iwl_mvm_invalidate_sta_queue(mvm, queue,
674 disable_agg_tids, false);
675 }
676
677 /* Mark queue as shared in transport */
678 iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
679
680 /* TODO: a redirection may be required - DQA phase 2 */
681 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300682
683 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
684 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
685 wdg_timeout);
686
687 spin_lock_bh(&mvmsta->lock);
688 mvmsta->tid_data[tid].txq_id = queue;
Liad Kaufman9794c642015-08-19 17:34:28 +0300689 mvmsta->tid_data[tid].is_tid_active = true;
Liad Kaufman24afba72015-07-28 18:56:08 +0300690 mvmsta->tfd_queue_msk |= BIT(queue);
Liad Kaufman9794c642015-08-19 17:34:28 +0300691 queue_state = mvmsta->tid_data[tid].state;
Liad Kaufman24afba72015-07-28 18:56:08 +0300692
693 if (mvmsta->reserved_queue == queue)
694 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
695 spin_unlock_bh(&mvmsta->lock);
696
Liad Kaufman42db09c2016-05-02 14:01:14 +0300697 if (!shared_queue) {
698 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
699 if (ret)
700 goto out_err;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300701
Liad Kaufman42db09c2016-05-02 14:01:14 +0300702 /* If we need to re-enable aggregations... */
703 if (queue_state == IWL_AGG_ON) {
704 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
705 if (ret)
706 goto out_err;
707 }
708 }
Liad Kaufman9794c642015-08-19 17:34:28 +0300709
Liad Kaufman42db09c2016-05-02 14:01:14 +0300710 return 0;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300711
712out_err:
713 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
714
715 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300716}
717
718static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
719{
720 if (tid == IWL_MAX_TID_COUNT)
721 return IEEE80211_AC_VO; /* MGMT */
722
723 return tid_to_mac80211_ac[tid];
724}
725
726static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
727 struct ieee80211_sta *sta, int tid)
728{
729 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
730 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
731 struct sk_buff *skb;
732 struct ieee80211_hdr *hdr;
733 struct sk_buff_head deferred_tx;
734 u8 mac_queue;
735 bool no_queue = false; /* Marks if there is a problem with the queue */
736 u8 ac;
737
738 lockdep_assert_held(&mvm->mutex);
739
740 skb = skb_peek(&tid_data->deferred_tx_frames);
741 if (!skb)
742 return;
743 hdr = (void *)skb->data;
744
745 ac = iwl_mvm_tid_to_ac_queue(tid);
746 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
747
748 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
749 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
750 IWL_ERR(mvm,
751 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
752 mvmsta->sta_id, tid);
753
754 /*
755 * Mark queue as problematic so later the deferred traffic is
756 * freed, as we can do nothing with it
757 */
758 no_queue = true;
759 }
760
761 __skb_queue_head_init(&deferred_tx);
762
Liad Kaufmand2515a92016-03-23 16:31:08 +0200763 /* Disable bottom-halves when entering TX path */
764 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +0300765 spin_lock(&mvmsta->lock);
766 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
767 spin_unlock(&mvmsta->lock);
768
Liad Kaufman24afba72015-07-28 18:56:08 +0300769 while ((skb = __skb_dequeue(&deferred_tx)))
770 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
771 ieee80211_free_txskb(mvm->hw, skb);
772 local_bh_enable();
773
774 /* Wake queue */
775 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
776}
777
778void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
779{
780 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
781 add_stream_wk);
782 struct ieee80211_sta *sta;
783 struct iwl_mvm_sta *mvmsta;
784 unsigned long deferred_tid_traffic;
785 int sta_id, tid;
786
Liad Kaufman9794c642015-08-19 17:34:28 +0300787 /* Check inactivity of queues */
788 iwl_mvm_inactivity_check(mvm);
789
Liad Kaufman24afba72015-07-28 18:56:08 +0300790 mutex_lock(&mvm->mutex);
791
792 /* Go over all stations with deferred traffic */
793 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
794 IWL_MVM_STATION_COUNT) {
795 clear_bit(sta_id, mvm->sta_deferred_frames);
796 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
797 lockdep_is_held(&mvm->mutex));
798 if (IS_ERR_OR_NULL(sta))
799 continue;
800
801 mvmsta = iwl_mvm_sta_from_mac80211(sta);
802 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
803
804 for_each_set_bit(tid, &deferred_tid_traffic,
805 IWL_MAX_TID_COUNT + 1)
806 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
807 }
808
809 mutex_unlock(&mvm->mutex);
810}
811
812static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +0300813 struct ieee80211_sta *sta,
814 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +0300815{
816 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
817 int queue;
818
Liad Kaufman9794c642015-08-19 17:34:28 +0300819 /*
820 * Check for inactive queues, so we don't reach a situation where we
821 * can't add a STA due to a shortage in queues that doesn't really exist
822 */
823 iwl_mvm_inactivity_check(mvm);
824
Liad Kaufman24afba72015-07-28 18:56:08 +0300825 spin_lock_bh(&mvm->queue_info_lock);
826
827 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300828 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
829 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +0300830 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
831 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +0300832 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
833 else
Liad Kaufman9794c642015-08-19 17:34:28 +0300834 queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
835 IWL_MVM_DQA_MIN_DATA_QUEUE,
Liad Kaufmand5216a22015-08-09 15:50:51 +0300836 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300837 if (queue < 0) {
838 spin_unlock_bh(&mvm->queue_info_lock);
839 IWL_ERR(mvm, "No available queues for new station\n");
840 return -ENOSPC;
841 }
Liad Kaufmancf961e12015-08-13 19:16:08 +0300842 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +0300843
844 spin_unlock_bh(&mvm->queue_info_lock);
845
846 mvmsta->reserved_queue = queue;
847
848 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
849 queue, mvmsta->sta_id);
850
851 return 0;
852}
853
Johannes Berg8ca151b2013-01-24 14:25:36 +0100854int iwl_mvm_add_sta(struct iwl_mvm *mvm,
855 struct ieee80211_vif *vif,
856 struct ieee80211_sta *sta)
857{
858 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100859 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +0200860 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100861 int i, ret, sta_id;
862
863 lockdep_assert_held(&mvm->mutex);
864
865 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200866 sta_id = iwl_mvm_find_free_sta_id(mvm,
867 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100868 else
869 sta_id = mvm_sta->sta_id;
870
Johannes Berg36f46312015-03-10 20:32:08 +0100871 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100872 return -ENOSPC;
873
874 spin_lock_init(&mvm_sta->lock);
875
876 mvm_sta->sta_id = sta_id;
877 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
878 mvmvif->color);
879 mvm_sta->vif = vif;
880 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300881 mvm_sta->tx_protection = 0;
882 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100883
884 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300885 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +0300886 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100887 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300888
889 /* allocate new queues for a TDLS station */
890 if (sta->tdls) {
891 ret = iwl_mvm_tdls_sta_init(mvm, sta);
892 if (ret)
893 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300894 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300895 for (i = 0; i < IEEE80211_NUM_ACS; i++)
896 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
897 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
898 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100899
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200900 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +0300901 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200902 u16 seq = mvm_sta->tid_data[i].seq_number;
903 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
904 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +0300905
906 if (!iwl_mvm_is_dqa_supported(mvm))
907 continue;
908
909 /*
910 * Mark all queues for this STA as unallocated and defer TX
911 * frames until the queue is allocated
912 */
913 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
914 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200915 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300916 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +0300917 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100918
Sara Sharona571f5f2015-12-07 12:50:58 +0200919 if (iwl_mvm_has_new_rx_api(mvm) &&
920 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
921 dup_data = kcalloc(mvm->trans->num_rx_queues,
922 sizeof(*dup_data),
923 GFP_KERNEL);
924 if (!dup_data)
925 return -ENOMEM;
926 mvm_sta->dup_data = dup_data;
927 }
928
Liad Kaufman24afba72015-07-28 18:56:08 +0300929 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +0300930 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
931 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +0300932 if (ret)
933 goto err;
934 }
935
936 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100937 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300938 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100939
Johannes Berg9e848012014-08-04 14:33:42 +0200940 if (vif->type == NL80211_IFTYPE_STATION) {
941 if (!sta->tdls) {
942 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
943 mvmvif->ap_sta_id = sta_id;
944 } else {
945 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
946 }
947 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100948
949 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
950
951 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300952
953err:
954 iwl_mvm_tdls_sta_deinit(mvm, sta);
955 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100956}
957
Johannes Berg7a453972013-02-12 13:10:44 +0100958int iwl_mvm_update_sta(struct iwl_mvm *mvm,
959 struct ieee80211_vif *vif,
960 struct ieee80211_sta *sta)
961{
Liad Kaufman24afba72015-07-28 18:56:08 +0300962 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
Johannes Berg7a453972013-02-12 13:10:44 +0100963}
964
Johannes Berg8ca151b2013-01-24 14:25:36 +0100965int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
966 bool drain)
967{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300968 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100969 int ret;
970 u32 status;
971
972 lockdep_assert_held(&mvm->mutex);
973
974 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
975 cmd.sta_id = mvmsta->sta_id;
976 cmd.add_modify = STA_MODE_MODIFY;
977 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
978 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
979
980 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200981 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
982 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300983 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100984 if (ret)
985 return ret;
986
Sara Sharon837c4da2016-01-07 16:50:45 +0200987 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100988 case ADD_STA_SUCCESS:
989 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
990 mvmsta->sta_id);
991 break;
992 default:
993 ret = -EIO;
994 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
995 mvmsta->sta_id);
996 break;
997 }
998
999 return ret;
1000}
1001
1002/*
1003 * Remove a station from the FW table. Before sending the command to remove
1004 * the station validate that the station is indeed known to the driver (sanity
1005 * only).
1006 */
1007static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1008{
1009 struct ieee80211_sta *sta;
1010 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1011 .sta_id = sta_id,
1012 };
1013 int ret;
1014
1015 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1016 lockdep_is_held(&mvm->mutex));
1017
1018 /* Note: internal stations are marked as error values */
1019 if (!sta) {
1020 IWL_ERR(mvm, "Invalid station id\n");
1021 return -EINVAL;
1022 }
1023
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001024 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001025 sizeof(rm_sta_cmd), &rm_sta_cmd);
1026 if (ret) {
1027 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1028 return ret;
1029 }
1030
1031 return 0;
1032}
1033
1034void iwl_mvm_sta_drained_wk(struct work_struct *wk)
1035{
1036 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
1037 u8 sta_id;
1038
1039 /*
1040 * The mutex is needed because of the SYNC cmd, but not only: if the
1041 * work would run concurrently with iwl_mvm_rm_sta, it would run before
1042 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
1043 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
1044 * that later.
1045 */
1046 mutex_lock(&mvm->mutex);
1047
1048 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
1049 int ret;
1050 struct ieee80211_sta *sta =
1051 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1052 lockdep_is_held(&mvm->mutex));
1053
Johannes Berg1ddbbb02013-12-04 22:39:17 +01001054 /*
1055 * This station is in use or RCU-removed; the latter happens in
1056 * managed mode, where mac80211 removes the station before we
1057 * can remove it from firmware (we can only do that after the
1058 * MAC is marked unassociated), and possibly while the deauth
1059 * frame to disconnect from the AP is still queued. Then, the
1060 * station pointer is -ENOENT when the last skb is reclaimed.
1061 */
1062 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001063 continue;
1064
1065 if (PTR_ERR(sta) == -EINVAL) {
1066 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
1067 sta_id);
1068 continue;
1069 }
1070
1071 if (!sta) {
1072 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
1073 sta_id);
1074 continue;
1075 }
1076
1077 WARN_ON(PTR_ERR(sta) != -EBUSY);
1078 /* This station was removed and we waited until it got drained,
1079 * we can now proceed and remove it.
1080 */
1081 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1082 if (ret) {
1083 IWL_ERR(mvm,
1084 "Couldn't remove sta %d after it was drained\n",
1085 sta_id);
1086 continue;
1087 }
Monam Agarwalc531c772014-03-24 00:05:56 +05301088 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001089 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001090
1091 if (mvm->tfd_drained[sta_id]) {
1092 unsigned long i, msk = mvm->tfd_drained[sta_id];
1093
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +02001094 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +03001095 iwl_mvm_disable_txq(mvm, i, i,
1096 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001097
1098 mvm->tfd_drained[sta_id] = 0;
1099 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
1100 sta_id, msk);
1101 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001102 }
1103
1104 mutex_unlock(&mvm->mutex);
1105}
1106
Liad Kaufman24afba72015-07-28 18:56:08 +03001107static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1108 struct ieee80211_vif *vif,
1109 struct iwl_mvm_sta *mvm_sta)
1110{
1111 int ac;
1112 int i;
1113
1114 lockdep_assert_held(&mvm->mutex);
1115
1116 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1117 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
1118 continue;
1119
1120 ac = iwl_mvm_tid_to_ac_queue(i);
1121 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
1122 vif->hw_queue[ac], i, 0);
1123 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
1124 }
1125}
1126
Johannes Berg8ca151b2013-01-24 14:25:36 +01001127int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1128 struct ieee80211_vif *vif,
1129 struct ieee80211_sta *sta)
1130{
1131 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001132 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001133 int ret;
1134
1135 lockdep_assert_held(&mvm->mutex);
1136
Sara Sharona571f5f2015-12-07 12:50:58 +02001137 if (iwl_mvm_has_new_rx_api(mvm))
1138 kfree(mvm_sta->dup_data);
1139
Liad Kaufmana6f035a2015-08-24 15:23:14 +03001140 if ((vif->type == NL80211_IFTYPE_STATION &&
1141 mvmvif->ap_sta_id == mvm_sta->sta_id) ||
1142 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001143 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1144 if (ret)
1145 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001146 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +03001147 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001148 if (ret)
1149 return ret;
1150 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1151 mvm_sta->tfd_queue_msk);
1152 if (ret)
1153 return ret;
1154 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +02001155
Liad Kaufman24afba72015-07-28 18:56:08 +03001156 /* If DQA is supported - the queues can be disabled now */
1157 if (iwl_mvm_is_dqa_supported(mvm))
1158 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
1159
Johannes Berg8ca151b2013-01-24 14:25:36 +01001160 /* if we are associated - we can't remove the AP STA now */
1161 if (vif->bss_conf.assoc)
1162 return ret;
1163
1164 /* unassoc - go ahead - remove the AP STA now */
1165 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +02001166
1167 /* clear d0i3_ap_sta_id if no longer relevant */
1168 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
1169 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001170 }
1171
1172 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +03001173 * This shouldn't happen - the TDLS channel switch should be canceled
1174 * before the STA is removed.
1175 */
1176 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
1177 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
1178 cancel_delayed_work(&mvm->tdls_cs.dwork);
1179 }
1180
1181 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001182 * Make sure that the tx response code sees the station as -EBUSY and
1183 * calls the drain worker.
1184 */
1185 spin_lock_bh(&mvm_sta->lock);
1186 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +01001187 * There are frames pending on the AC queues for this station.
1188 * We need to wait until all the frames are drained...
1189 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001190 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001191 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1192 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001193 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001194
1195 /* disable TDLS sta queues on drain complete */
1196 if (sta->tdls) {
1197 mvm->tfd_drained[mvm_sta->sta_id] =
1198 mvm_sta->tfd_queue_msk;
1199 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
1200 mvm_sta->sta_id);
1201 }
1202
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001203 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001204 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +03001205 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +03001206
1207 if (sta->tdls)
1208 iwl_mvm_tdls_sta_deinit(mvm, sta);
1209
Johannes Berg8ca151b2013-01-24 14:25:36 +01001210 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +05301211 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001212 }
1213
1214 return ret;
1215}
1216
1217int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1218 struct ieee80211_vif *vif,
1219 u8 sta_id)
1220{
1221 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1222
1223 lockdep_assert_held(&mvm->mutex);
1224
Monam Agarwalc531c772014-03-24 00:05:56 +05301225 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001226 return ret;
1227}
1228
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001229int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1230 struct iwl_mvm_int_sta *sta,
1231 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001232{
1233 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +02001234 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001235 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
1236 return -ENOSPC;
1237 }
1238
1239 sta->tfd_queue_msk = qmask;
1240
1241 /* put a non-NULL value so iterating over the stations won't stop */
1242 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1243 return 0;
1244}
1245
Johannes Berg712b24a2014-08-04 14:14:14 +02001246static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
1247 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001248{
Monam Agarwalc531c772014-03-24 00:05:56 +05301249 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001250 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1251 sta->sta_id = IWL_MVM_STATION_COUNT;
1252}
1253
1254static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1255 struct iwl_mvm_int_sta *sta,
1256 const u8 *addr,
1257 u16 mac_id, u16 color)
1258{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001259 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001260 int ret;
1261 u32 status;
1262
1263 lockdep_assert_held(&mvm->mutex);
1264
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001265 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001266 cmd.sta_id = sta->sta_id;
1267 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1268 color));
1269
1270 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +02001271 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001272
1273 if (addr)
1274 memcpy(cmd.addr, addr, ETH_ALEN);
1275
Sara Sharon854c5702016-01-26 13:17:47 +02001276 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1277 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001278 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001279 if (ret)
1280 return ret;
1281
Sara Sharon837c4da2016-01-07 16:50:45 +02001282 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001283 case ADD_STA_SUCCESS:
1284 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1285 return 0;
1286 default:
1287 ret = -EIO;
1288 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1289 status);
1290 break;
1291 }
1292 return ret;
1293}
1294
1295int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
1296{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +02001297 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1298 mvm->cfg->base_params->wd_timeout :
1299 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001300 int ret;
1301
1302 lockdep_assert_held(&mvm->mutex);
1303
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001304 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001305 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +03001306 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +03001307
1308 /* Allocate aux station and assign to it the aux queue */
1309 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +02001310 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001311 if (ret)
1312 return ret;
1313
1314 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
1315 MAC_INDEX_AUX, 0);
1316
1317 if (ret)
1318 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1319 return ret;
1320}
1321
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001322int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1323{
1324 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1325
1326 lockdep_assert_held(&mvm->mutex);
1327 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1328 mvmvif->id, 0);
1329}
1330
1331int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1332{
1333 int ret;
1334
1335 lockdep_assert_held(&mvm->mutex);
1336
1337 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1338 if (ret)
1339 IWL_WARN(mvm, "Failed sending remove station\n");
1340
1341 return ret;
1342}
1343
1344void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1345{
1346 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1347}
1348
Johannes Berg712b24a2014-08-04 14:14:14 +02001349void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1350{
1351 lockdep_assert_held(&mvm->mutex);
1352
1353 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1354}
1355
Johannes Berg8ca151b2013-01-24 14:25:36 +01001356/*
1357 * Send the add station command for the vif's broadcast station.
1358 * Assumes that the station was already allocated.
1359 *
1360 * @mvm: the mvm component
1361 * @vif: the interface to which the broadcast station is added
1362 * @bsta: the broadcast station to add.
1363 */
Johannes Berg013290a2014-08-04 13:38:48 +02001364int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001365{
1366 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001367 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001368 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001369 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001370
1371 lockdep_assert_held(&mvm->mutex);
1372
Liad Kaufmande24f632015-08-04 15:19:18 +03001373 if (iwl_mvm_is_dqa_supported(mvm)) {
1374 struct iwl_trans_txq_scd_cfg cfg = {
1375 .fifo = IWL_MVM_TX_FIFO_VO,
1376 .sta_id = mvmvif->bcast_sta.sta_id,
1377 .tid = IWL_MAX_TID_COUNT,
1378 .aggregate = false,
1379 .frame_limit = IWL_FRAME_LIMIT,
1380 };
1381 unsigned int wdg_timeout =
1382 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1383 int queue;
1384
1385 if ((vif->type == NL80211_IFTYPE_AP) &&
1386 (mvmvif->bcast_sta.tfd_queue_msk &
1387 BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1388 queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
Liad Kaufman4c965132015-08-09 19:26:56 +03001389 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1390 (mvmvif->bcast_sta.tfd_queue_msk &
1391 BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1392 queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
Liad Kaufmande24f632015-08-04 15:19:18 +03001393 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1394 return -EINVAL;
1395
1396 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1397 wdg_timeout);
1398 }
1399
Johannes Berg5023d962013-07-31 14:07:43 +02001400 if (vif->type == NL80211_IFTYPE_ADHOC)
1401 baddr = vif->bss_conf.bssid;
1402
Johannes Berg8ca151b2013-01-24 14:25:36 +01001403 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1404 return -ENOSPC;
1405
1406 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1407 mvmvif->id, mvmvif->color);
1408}
1409
1410/* Send the FW a request to remove the station from it's internal data
1411 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001412int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001413{
Johannes Berg013290a2014-08-04 13:38:48 +02001414 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001415 int ret;
1416
1417 lockdep_assert_held(&mvm->mutex);
1418
Johannes Berg013290a2014-08-04 13:38:48 +02001419 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001420 if (ret)
1421 IWL_WARN(mvm, "Failed sending remove station\n");
1422 return ret;
1423}
1424
Johannes Berg013290a2014-08-04 13:38:48 +02001425int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1426{
1427 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03001428 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02001429
1430 lockdep_assert_held(&mvm->mutex);
1431
Liad Kaufmande24f632015-08-04 15:19:18 +03001432 if (!iwl_mvm_is_dqa_supported(mvm))
1433 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001434
Liad Kaufmande24f632015-08-04 15:19:18 +03001435 if (vif->type == NL80211_IFTYPE_AP) {
1436 /*
1437 * The firmware defines the TFD queue mask to only be relevant
1438 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1439 * be included.
1440 */
Johannes Berg013290a2014-08-04 13:38:48 +02001441 qmask &= ~BIT(vif->cab_queue);
1442
Liad Kaufmande24f632015-08-04 15:19:18 +03001443 if (iwl_mvm_is_dqa_supported(mvm))
1444 qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
Liad Kaufman4c965132015-08-09 19:26:56 +03001445 } else if (iwl_mvm_is_dqa_supported(mvm) &&
1446 vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1447 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
Liad Kaufmande24f632015-08-04 15:19:18 +03001448 }
1449
Johannes Berg013290a2014-08-04 13:38:48 +02001450 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1451 ieee80211_vif_type_p2p(vif));
1452}
1453
Johannes Berg8ca151b2013-01-24 14:25:36 +01001454/* Allocate a new station entry for the broadcast station to the given vif,
1455 * and send it to the FW.
1456 * Note that each P2P mac should have its own broadcast station.
1457 *
1458 * @mvm: the mvm component
1459 * @vif: the interface to which the broadcast station is added
1460 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02001461int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001462{
1463 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001464 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001465 int ret;
1466
1467 lockdep_assert_held(&mvm->mutex);
1468
Johannes Berg013290a2014-08-04 13:38:48 +02001469 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001470 if (ret)
1471 return ret;
1472
Johannes Berg013290a2014-08-04 13:38:48 +02001473 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001474
1475 if (ret)
1476 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001477
Johannes Berg8ca151b2013-01-24 14:25:36 +01001478 return ret;
1479}
1480
Johannes Berg013290a2014-08-04 13:38:48 +02001481void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1482{
1483 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1484
1485 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1486}
1487
Johannes Berg8ca151b2013-01-24 14:25:36 +01001488/*
1489 * Send the FW a request to remove the station from it's internal data
1490 * structures, and in addition remove it from the local data structure.
1491 */
Johannes Berg013290a2014-08-04 13:38:48 +02001492int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001493{
1494 int ret;
1495
1496 lockdep_assert_held(&mvm->mutex);
1497
Johannes Berg013290a2014-08-04 13:38:48 +02001498 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001499
Johannes Berg013290a2014-08-04 13:38:48 +02001500 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1501
Johannes Berg8ca151b2013-01-24 14:25:36 +01001502 return ret;
1503}
1504
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001505#define IWL_MAX_RX_BA_SESSIONS 16
1506
Sara Sharonb915c102016-03-23 16:32:02 +02001507static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02001508{
Sara Sharonb915c102016-03-23 16:32:02 +02001509 struct iwl_mvm_delba_notif notif = {
1510 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1511 .metadata.sync = 1,
1512 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02001513 };
Sara Sharonb915c102016-03-23 16:32:02 +02001514 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
1515};
Sara Sharon10b2b202016-03-20 16:23:41 +02001516
Sara Sharonb915c102016-03-23 16:32:02 +02001517static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1518 struct iwl_mvm_baid_data *data)
1519{
1520 int i;
1521
1522 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1523
1524 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1525 int j;
1526 struct iwl_mvm_reorder_buffer *reorder_buf =
1527 &data->reorder_buf[i];
1528
Sara Sharon06904052016-02-28 20:28:17 +02001529 spin_lock_bh(&reorder_buf->lock);
1530 if (likely(!reorder_buf->num_stored)) {
1531 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02001532 continue;
Sara Sharon06904052016-02-28 20:28:17 +02001533 }
Sara Sharonb915c102016-03-23 16:32:02 +02001534
1535 /*
1536 * This shouldn't happen in regular DELBA since the internal
1537 * delBA notification should trigger a release of all frames in
1538 * the reorder buffer.
1539 */
1540 WARN_ON(1);
1541
1542 for (j = 0; j < reorder_buf->buf_size; j++)
1543 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02001544 /*
1545 * Prevent timer re-arm. This prevents a very far fetched case
1546 * where we timed out on the notification. There may be prior
1547 * RX frames pending in the RX queue before the notification
1548 * that might get processed between now and the actual deletion
1549 * and we would re-arm the timer although we are deleting the
1550 * reorder buffer.
1551 */
1552 reorder_buf->removed = true;
1553 spin_unlock_bh(&reorder_buf->lock);
1554 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02001555 }
1556}
1557
1558static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1559 u32 sta_id,
1560 struct iwl_mvm_baid_data *data,
1561 u16 ssn, u8 buf_size)
1562{
1563 int i;
1564
1565 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1566 struct iwl_mvm_reorder_buffer *reorder_buf =
1567 &data->reorder_buf[i];
1568 int j;
1569
1570 reorder_buf->num_stored = 0;
1571 reorder_buf->head_sn = ssn;
1572 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02001573 /* rx reorder timer */
1574 reorder_buf->reorder_timer.function =
1575 iwl_mvm_reorder_timer_expired;
1576 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
1577 init_timer(&reorder_buf->reorder_timer);
1578 spin_lock_init(&reorder_buf->lock);
1579 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02001580 reorder_buf->queue = i;
1581 reorder_buf->sta_id = sta_id;
1582 for (j = 0; j < reorder_buf->buf_size; j++)
1583 __skb_queue_head_init(&reorder_buf->entries[j]);
1584 }
Sara Sharon10b2b202016-03-20 16:23:41 +02001585}
1586
Johannes Berg8ca151b2013-01-24 14:25:36 +01001587int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02001588 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001589{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001590 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001591 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02001592 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001593 int ret;
1594 u32 status;
1595
1596 lockdep_assert_held(&mvm->mutex);
1597
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001598 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1599 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1600 return -ENOSPC;
1601 }
1602
Sara Sharon10b2b202016-03-20 16:23:41 +02001603 if (iwl_mvm_has_new_rx_api(mvm) && start) {
1604 /*
1605 * Allocate here so if allocation fails we can bail out early
1606 * before starting the BA session in the firmware
1607 */
Sara Sharonb915c102016-03-23 16:32:02 +02001608 baid_data = kzalloc(sizeof(*baid_data) +
1609 mvm->trans->num_rx_queues *
1610 sizeof(baid_data->reorder_buf[0]),
1611 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02001612 if (!baid_data)
1613 return -ENOMEM;
1614 }
1615
Johannes Berg8ca151b2013-01-24 14:25:36 +01001616 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1617 cmd.sta_id = mvm_sta->sta_id;
1618 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001619 if (start) {
1620 cmd.add_immediate_ba_tid = (u8) tid;
1621 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02001622 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001623 } else {
1624 cmd.remove_immediate_ba_tid = (u8) tid;
1625 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001626 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1627 STA_MODIFY_REMOVE_BA_TID;
1628
1629 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001630 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1631 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001632 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001633 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02001634 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001635
Sara Sharon837c4da2016-01-07 16:50:45 +02001636 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001637 case ADD_STA_SUCCESS:
1638 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
1639 start ? "start" : "stopp");
1640 break;
1641 case ADD_STA_IMMEDIATE_BA_FAILURE:
1642 IWL_WARN(mvm, "RX BA Session refused by fw\n");
1643 ret = -ENOSPC;
1644 break;
1645 default:
1646 ret = -EIO;
1647 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
1648 start ? "start" : "stopp", status);
1649 break;
1650 }
1651
Sara Sharon10b2b202016-03-20 16:23:41 +02001652 if (ret)
1653 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001654
Sara Sharon10b2b202016-03-20 16:23:41 +02001655 if (start) {
1656 u8 baid;
1657
1658 mvm->rx_ba_sessions++;
1659
1660 if (!iwl_mvm_has_new_rx_api(mvm))
1661 return 0;
1662
1663 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
1664 ret = -EINVAL;
1665 goto out_free;
1666 }
1667 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
1668 IWL_ADD_STA_BAID_SHIFT);
1669 baid_data->baid = baid;
1670 baid_data->timeout = timeout;
1671 baid_data->last_rx = jiffies;
1672 init_timer(&baid_data->session_timer);
1673 baid_data->session_timer.function =
1674 iwl_mvm_rx_agg_session_expired;
1675 baid_data->session_timer.data =
1676 (unsigned long)&mvm->baid_map[baid];
1677 baid_data->mvm = mvm;
1678 baid_data->tid = tid;
1679 baid_data->sta_id = mvm_sta->sta_id;
1680
1681 mvm_sta->tid_to_baid[tid] = baid;
1682 if (timeout)
1683 mod_timer(&baid_data->session_timer,
1684 TU_TO_EXP_TIME(timeout * 2));
1685
Sara Sharonb915c102016-03-23 16:32:02 +02001686 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
1687 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02001688 /*
1689 * protect the BA data with RCU to cover a case where our
1690 * internal RX sync mechanism will timeout (not that it's
1691 * supposed to happen) and we will free the session data while
1692 * RX is being processed in parallel
1693 */
1694 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
1695 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
1696 } else if (mvm->rx_ba_sessions > 0) {
1697 u8 baid = mvm_sta->tid_to_baid[tid];
1698
1699 /* check that restart flow didn't zero the counter */
1700 mvm->rx_ba_sessions--;
1701 if (!iwl_mvm_has_new_rx_api(mvm))
1702 return 0;
1703
1704 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
1705 return -EINVAL;
1706
1707 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
1708 if (WARN_ON(!baid_data))
1709 return -EINVAL;
1710
1711 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02001712 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02001713 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02001714 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
1715 kfree_rcu(baid_data, rcu_head);
1716 }
1717 return 0;
1718
1719out_free:
1720 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001721 return ret;
1722}
1723
Liad Kaufman9794c642015-08-19 17:34:28 +03001724int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1725 int tid, u8 queue, bool start)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001726{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001727 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001728 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001729 int ret;
1730 u32 status;
1731
1732 lockdep_assert_held(&mvm->mutex);
1733
1734 if (start) {
1735 mvm_sta->tfd_queue_msk |= BIT(queue);
1736 mvm_sta->tid_disable_agg &= ~BIT(tid);
1737 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03001738 /* In DQA-mode the queue isn't removed on agg termination */
1739 if (!iwl_mvm_is_dqa_supported(mvm))
1740 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001741 mvm_sta->tid_disable_agg |= BIT(tid);
1742 }
1743
1744 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1745 cmd.sta_id = mvm_sta->sta_id;
1746 cmd.add_modify = STA_MODE_MODIFY;
1747 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
1748 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
1749 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
1750
1751 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001752 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1753 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001754 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001755 if (ret)
1756 return ret;
1757
Sara Sharon837c4da2016-01-07 16:50:45 +02001758 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001759 case ADD_STA_SUCCESS:
1760 break;
1761 default:
1762 ret = -EIO;
1763 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
1764 start ? "start" : "stopp", status);
1765 break;
1766 }
1767
1768 return ret;
1769}
1770
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02001771const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001772 IEEE80211_AC_BE,
1773 IEEE80211_AC_BK,
1774 IEEE80211_AC_BK,
1775 IEEE80211_AC_BE,
1776 IEEE80211_AC_VI,
1777 IEEE80211_AC_VI,
1778 IEEE80211_AC_VO,
1779 IEEE80211_AC_VO,
Liad Kaufman9794c642015-08-19 17:34:28 +03001780 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001781};
1782
Johannes Berg3e56ead2013-02-15 22:23:18 +01001783static const u8 tid_to_ucode_ac[] = {
1784 AC_BE,
1785 AC_BK,
1786 AC_BK,
1787 AC_BE,
1788 AC_VI,
1789 AC_VI,
1790 AC_VO,
1791 AC_VO,
1792};
1793
Johannes Berg8ca151b2013-01-24 14:25:36 +01001794int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1795 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1796{
Johannes Berg5b577a92013-11-14 18:20:04 +01001797 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001798 struct iwl_mvm_tid_data *tid_data;
1799 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001800 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001801
1802 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1803 return -EINVAL;
1804
1805 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
1806 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
1807 mvmsta->tid_data[tid].state);
1808 return -ENXIO;
1809 }
1810
1811 lockdep_assert_held(&mvm->mutex);
1812
Arik Nemtsovb2492502014-03-13 12:21:50 +02001813 spin_lock_bh(&mvmsta->lock);
1814
1815 /* possible race condition - we entered D0i3 while starting agg */
1816 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
1817 spin_unlock_bh(&mvmsta->lock);
1818 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
1819 return -EIO;
1820 }
1821
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001822 spin_lock_bh(&mvm->queue_info_lock);
1823
Liad Kaufmancf961e12015-08-13 19:16:08 +03001824 /*
1825 * Note the possible cases:
1826 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
1827 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
1828 * one and mark it as reserved
1829 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
1830 * non-DQA mode, since the TXQ hasn't yet been allocated
1831 */
1832 txq_id = mvmsta->tid_data[tid].txq_id;
1833 if (!iwl_mvm_is_dqa_supported(mvm) ||
1834 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
Liad Kaufman9794c642015-08-19 17:34:28 +03001835 txq_id = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1836 mvm->first_agg_queue,
Liad Kaufmancf961e12015-08-13 19:16:08 +03001837 mvm->last_agg_queue);
1838 if (txq_id < 0) {
1839 ret = txq_id;
1840 spin_unlock_bh(&mvm->queue_info_lock);
1841 IWL_ERR(mvm, "Failed to allocate agg queue\n");
1842 goto release_locks;
1843 }
1844
1845 /* TXQ hasn't yet been enabled, so mark it only as reserved */
1846 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001847 }
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001848 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001849
Liad Kaufmancf961e12015-08-13 19:16:08 +03001850 IWL_DEBUG_TX_QUEUES(mvm,
1851 "AGG for tid %d will be on queue #%d\n",
1852 tid, txq_id);
1853
Johannes Berg8ca151b2013-01-24 14:25:36 +01001854 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01001855 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001856 tid_data->txq_id = txq_id;
1857 *ssn = tid_data->ssn;
1858
1859 IWL_DEBUG_TX_QUEUES(mvm,
1860 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1861 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1862 tid_data->next_reclaimed);
1863
1864 if (tid_data->ssn == tid_data->next_reclaimed) {
1865 tid_data->state = IWL_AGG_STARTING;
1866 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1867 } else {
1868 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1869 }
1870
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001871 ret = 0;
1872
1873release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001874 spin_unlock_bh(&mvmsta->lock);
1875
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001876 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001877}
1878
1879int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001880 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
1881 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001882{
Johannes Berg5b577a92013-11-14 18:20:04 +01001883 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001884 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001885 unsigned int wdg_timeout =
1886 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001887 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03001888 bool alloc_queue = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001889 u16 ssn;
1890
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001891 struct iwl_trans_txq_scd_cfg cfg = {
1892 .sta_id = mvmsta->sta_id,
1893 .tid = tid,
1894 .frame_limit = buf_size,
1895 .aggregate = true,
1896 };
1897
Eyal Shapiraefed6642014-09-14 15:58:53 +03001898 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1899 != IWL_MAX_TID_COUNT);
1900
Johannes Berg8ca151b2013-01-24 14:25:36 +01001901 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1902
1903 spin_lock_bh(&mvmsta->lock);
1904 ssn = tid_data->ssn;
1905 queue = tid_data->txq_id;
1906 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001907 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001908 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001909 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001910 spin_unlock_bh(&mvmsta->lock);
1911
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001912 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001913
Liad Kaufmancf961e12015-08-13 19:16:08 +03001914 /* In DQA mode, the existing queue might need to be reconfigured */
1915 if (iwl_mvm_is_dqa_supported(mvm)) {
1916 spin_lock_bh(&mvm->queue_info_lock);
1917 /* Maybe there is no need to even alloc a queue... */
1918 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
1919 alloc_queue = false;
1920 spin_unlock_bh(&mvm->queue_info_lock);
1921
1922 /*
1923 * Only reconfig the SCD for the queue if the window size has
1924 * changed from current (become smaller)
1925 */
1926 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
1927 /*
1928 * If reconfiguring an existing queue, it first must be
1929 * drained
1930 */
1931 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1932 BIT(queue));
1933 if (ret) {
1934 IWL_ERR(mvm,
1935 "Error draining queue before reconfig\n");
1936 return ret;
1937 }
1938
1939 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
1940 mvmsta->sta_id, tid,
1941 buf_size, ssn);
1942 if (ret) {
1943 IWL_ERR(mvm,
1944 "Error reconfiguring TXQ #%d\n", queue);
1945 return ret;
1946 }
1947 }
1948 }
1949
1950 if (alloc_queue)
1951 iwl_mvm_enable_txq(mvm, queue,
1952 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
1953 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001954
Johannes Berg8ca151b2013-01-24 14:25:36 +01001955 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1956 if (ret)
1957 return -EIO;
1958
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001959 /* No need to mark as reserved */
1960 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03001961 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001962 spin_unlock_bh(&mvm->queue_info_lock);
1963
Johannes Berg8ca151b2013-01-24 14:25:36 +01001964 /*
1965 * Even though in theory the peer could have different
1966 * aggregation reorder buffer sizes for different sessions,
1967 * our ucode doesn't allow for that and has a global limit
1968 * for each station. Therefore, use the minimum of all the
1969 * aggregation sessions and our default value.
1970 */
1971 mvmsta->max_agg_bufsize =
1972 min(mvmsta->max_agg_bufsize, buf_size);
1973 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1974
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001975 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1976 sta->addr, tid);
1977
Eyal Shapira9e680942013-11-09 00:16:16 +02001978 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001979}
1980
1981int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1982 struct ieee80211_sta *sta, u16 tid)
1983{
Johannes Berg5b577a92013-11-14 18:20:04 +01001984 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001985 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1986 u16 txq_id;
1987 int err;
1988
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001989
1990 /*
1991 * If mac80211 is cleaning its state, then say that we finished since
1992 * our state has been cleared anyway.
1993 */
1994 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1995 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1996 return 0;
1997 }
1998
Johannes Berg8ca151b2013-01-24 14:25:36 +01001999 spin_lock_bh(&mvmsta->lock);
2000
2001 txq_id = tid_data->txq_id;
2002
2003 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
2004 mvmsta->sta_id, tid, txq_id, tid_data->state);
2005
Eyal Shapiraefed6642014-09-14 15:58:53 +03002006 mvmsta->agg_tids &= ~BIT(tid);
2007
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002008 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002009 /*
2010 * The TXQ is marked as reserved only if no traffic came through yet
2011 * This means no traffic has been sent on this TID (agg'd or not), so
2012 * we no longer have use for the queue. Since it hasn't even been
2013 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2014 * free.
2015 */
2016 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2017 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002018 spin_unlock_bh(&mvm->queue_info_lock);
2019
Johannes Berg8ca151b2013-01-24 14:25:36 +01002020 switch (tid_data->state) {
2021 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01002022 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002023
2024 IWL_DEBUG_TX_QUEUES(mvm,
2025 "ssn = %d, next_recl = %d\n",
2026 tid_data->ssn, tid_data->next_reclaimed);
2027
2028 /* There are still packets for this RA / TID in the HW */
2029 if (tid_data->ssn != tid_data->next_reclaimed) {
2030 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
2031 err = 0;
2032 break;
2033 }
2034
2035 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002036 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02002037 spin_unlock_bh(&mvmsta->lock);
2038
2039 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2040
2041 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2042
Liad Kaufmancf961e12015-08-13 19:16:08 +03002043 if (!iwl_mvm_is_dqa_supported(mvm)) {
2044 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2045
2046 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
2047 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02002048 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002049 case IWL_AGG_STARTING:
2050 case IWL_EMPTYING_HW_QUEUE_ADDBA:
2051 /*
2052 * The agg session has been stopped before it was set up. This
2053 * can happen when the AddBA timer times out for example.
2054 */
2055
2056 /* No barriers since we are under mutex */
2057 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002058
2059 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2060 tid_data->state = IWL_AGG_OFF;
2061 err = 0;
2062 break;
2063 default:
2064 IWL_ERR(mvm,
2065 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
2066 mvmsta->sta_id, tid, tid_data->state);
2067 IWL_ERR(mvm,
2068 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
2069 err = -EINVAL;
2070 }
2071
2072 spin_unlock_bh(&mvmsta->lock);
2073
2074 return err;
2075}
2076
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002077int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2078 struct ieee80211_sta *sta, u16 tid)
2079{
Johannes Berg5b577a92013-11-14 18:20:04 +01002080 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002081 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2082 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02002083 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002084
2085 /*
2086 * First set the agg state to OFF to avoid calling
2087 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
2088 */
2089 spin_lock_bh(&mvmsta->lock);
2090 txq_id = tid_data->txq_id;
2091 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
2092 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02002093 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002094 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03002095 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002096 spin_unlock_bh(&mvmsta->lock);
2097
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002098 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03002099 /*
2100 * The TXQ is marked as reserved only if no traffic came through yet
2101 * This means no traffic has been sent on this TID (agg'd or not), so
2102 * we no longer have use for the queue. Since it hasn't even been
2103 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
2104 * free.
2105 */
2106 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
2107 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03002108 spin_unlock_bh(&mvm->queue_info_lock);
2109
Johannes Bergb6658ff2013-07-24 13:55:51 +02002110 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002111 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03002112 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02002113 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02002114 iwl_trans_wait_tx_queue_empty(mvm->trans,
2115 mvmsta->tfd_queue_msk);
2116 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002117
Johannes Bergf7f89e72014-08-05 15:24:44 +02002118 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
2119
Liad Kaufmancf961e12015-08-13 19:16:08 +03002120 if (!iwl_mvm_is_dqa_supported(mvm)) {
2121 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
2122
2123 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
2124 tid, 0);
2125 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02002126 }
2127
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02002128 return 0;
2129}
2130
Johannes Berg8ca151b2013-01-24 14:25:36 +01002131static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
2132{
Johannes Berg2dc2a152015-06-16 17:09:18 +02002133 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002134
2135 lockdep_assert_held(&mvm->mutex);
2136
Johannes Berg2dc2a152015-06-16 17:09:18 +02002137 /* Pick the unused key offset with the highest 'deleted'
2138 * counter. Every time a key is deleted, all the counters
2139 * are incremented and the one that was just deleted is
2140 * reset to zero. Thus, the highest counter is the one
2141 * that was deleted longest ago. Pick that one.
2142 */
2143 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2144 if (test_bit(i, mvm->fw_key_table))
2145 continue;
2146 if (mvm->fw_key_deleted[i] > max) {
2147 max = mvm->fw_key_deleted[i];
2148 max_offs = i;
2149 }
2150 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002151
Johannes Berg2dc2a152015-06-16 17:09:18 +02002152 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002153 return STA_KEY_IDX_INVALID;
2154
Johannes Berg2dc2a152015-06-16 17:09:18 +02002155 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002156}
2157
Johannes Berg5f7a1842015-12-11 09:36:10 +01002158static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
2159 struct ieee80211_vif *vif,
2160 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002161{
Johannes Berg5b530e92014-12-23 16:00:17 +01002162 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002163
Johannes Berg5f7a1842015-12-11 09:36:10 +01002164 if (sta)
2165 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002166
2167 /*
2168 * The device expects GTKs for station interfaces to be
2169 * installed as GTKs for the AP station. If we have no
2170 * station ID, then use AP's station ID.
2171 */
2172 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02002173 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2174 u8 sta_id = mvmvif->ap_sta_id;
2175
Avri Altman9513c5e2015-10-19 16:29:11 +02002176 /*
2177 * It is possible that the 'sta' parameter is NULL,
2178 * for example when a GTK is removed - the sta_id will then
2179 * be the AP ID, and no station was passed by mac80211.
2180 */
Sara Sharon13303c02016-04-10 15:51:54 +03002181 return iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Avri Altman9513c5e2015-10-19 16:29:11 +02002182 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01002183
Johannes Berg5f7a1842015-12-11 09:36:10 +01002184 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002185}
2186
2187static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
2188 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002189 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002190 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
2191 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002192{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002193 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002194 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002195 int ret;
2196 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002197 u16 keyidx;
2198 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002199 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002200
2201 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2202 STA_KEY_FLG_KEYID_MSK;
2203 key_flags = cpu_to_le16(keyidx);
2204 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
2205
2206 switch (keyconf->cipher) {
2207 case WLAN_CIPHER_SUITE_TKIP:
2208 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002209 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002210 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03002211 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
2212 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002213 break;
2214 case WLAN_CIPHER_SUITE_CCMP:
2215 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03002216 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002217 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01002218 case WLAN_CIPHER_SUITE_WEP104:
2219 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05002220 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01002221 case WLAN_CIPHER_SUITE_WEP40:
2222 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
2223 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
2224 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002225 default:
Max Stepanove36e5432013-08-27 19:56:13 +03002226 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
2227 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002228 }
2229
Johannes Bergba3943b2014-11-12 23:54:48 +01002230 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002231 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2232
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002233 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03002234 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002235 cmd.sta_id = sta_id;
2236
2237 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002238 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002239 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
2240 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002241 else
2242 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2243 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002244
2245 switch (status) {
2246 case ADD_STA_SUCCESS:
2247 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
2248 break;
2249 default:
2250 ret = -EIO;
2251 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
2252 break;
2253 }
2254
2255 return ret;
2256}
2257
2258static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
2259 struct ieee80211_key_conf *keyconf,
2260 u8 sta_id, bool remove_key)
2261{
2262 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
2263
2264 /* verify the key details match the required command's expectations */
2265 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
2266 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
2267 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
2268 return -EINVAL;
2269
2270 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
2271 igtk_cmd.sta_id = cpu_to_le32(sta_id);
2272
2273 if (remove_key) {
2274 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
2275 } else {
2276 struct ieee80211_key_seq seq;
2277 const u8 *pn;
2278
2279 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002280 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2281 pn = seq.aes_cmac.pn;
2282 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
2283 ((u64) pn[4] << 8) |
2284 ((u64) pn[3] << 16) |
2285 ((u64) pn[2] << 24) |
2286 ((u64) pn[1] << 32) |
2287 ((u64) pn[0] << 40));
2288 }
2289
2290 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
2291 remove_key ? "removing" : "installing",
2292 igtk_cmd.sta_id);
2293
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03002294 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002295 sizeof(igtk_cmd), &igtk_cmd);
2296}
2297
2298
2299static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
2300 struct ieee80211_vif *vif,
2301 struct ieee80211_sta *sta)
2302{
Johannes Berg5b530e92014-12-23 16:00:17 +01002303 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002304
2305 if (sta)
2306 return sta->addr;
2307
2308 if (vif->type == NL80211_IFTYPE_STATION &&
2309 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
2310 u8 sta_id = mvmvif->ap_sta_id;
2311 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2312 lockdep_is_held(&mvm->mutex));
2313 return sta->addr;
2314 }
2315
2316
2317 return NULL;
2318}
2319
Johannes Berg2f6319d2014-11-12 23:39:56 +01002320static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2321 struct ieee80211_vif *vif,
2322 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002323 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002324 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01002325 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002326{
Johannes Berg2f6319d2014-11-12 23:39:56 +01002327 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002328 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002329 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002330 struct ieee80211_key_seq seq;
2331 u16 p1k[5];
2332
Johannes Berg8ca151b2013-01-24 14:25:36 +01002333 switch (keyconf->cipher) {
2334 case WLAN_CIPHER_SUITE_TKIP:
2335 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2336 /* get phase 1 key from mac80211 */
2337 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2338 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01002339 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002340 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002341 break;
2342 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01002343 case WLAN_CIPHER_SUITE_WEP40:
2344 case WLAN_CIPHER_SUITE_WEP104:
2345 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002346 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002347 break;
2348 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01002349 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002350 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002351 }
2352
Johannes Berg8ca151b2013-01-24 14:25:36 +01002353 return ret;
2354}
2355
Johannes Berg2f6319d2014-11-12 23:39:56 +01002356static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01002357 struct ieee80211_key_conf *keyconf,
2358 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002359{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002360 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002361 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002362 int ret;
2363 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002364
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02002365 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2366 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002367 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2368 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2369
Johannes Bergba3943b2014-11-12 23:54:48 +01002370 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002371 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2372
Max Stepanov5a258aa2013-04-07 09:11:21 +03002373 cmd.key_flags = key_flags;
2374 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002375 cmd.sta_id = sta_id;
2376
Johannes Berg8ca151b2013-01-24 14:25:36 +01002377 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002378 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2379 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002380
2381 switch (status) {
2382 case ADD_STA_SUCCESS:
2383 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2384 break;
2385 default:
2386 ret = -EIO;
2387 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2388 break;
2389 }
2390
2391 return ret;
2392}
2393
Johannes Berg2f6319d2014-11-12 23:39:56 +01002394int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2395 struct ieee80211_vif *vif,
2396 struct ieee80211_sta *sta,
2397 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002398 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002399{
Johannes Bergba3943b2014-11-12 23:54:48 +01002400 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002401 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002402 u8 sta_id;
2403 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03002404 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01002405
2406 lockdep_assert_held(&mvm->mutex);
2407
2408 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01002409 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2410 if (!mvm_sta) {
2411 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01002412 return -EINVAL;
2413 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01002414 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002415
2416 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
2417 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2418 goto end;
2419 }
2420
2421 /*
2422 * It is possible that the 'sta' parameter is NULL, and thus
2423 * there is a need to retrieve the sta from the local station table.
2424 */
2425 if (!sta) {
2426 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2427 lockdep_is_held(&mvm->mutex));
2428 if (IS_ERR_OR_NULL(sta)) {
2429 IWL_ERR(mvm, "Invalid station id\n");
2430 return -EINVAL;
2431 }
2432 }
2433
2434 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2435 return -EINVAL;
2436
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002437 /* If the key_offset is not pre-assigned, we need to find a
2438 * new offset to use. In normal cases, the offset is not
2439 * pre-assigned, but during HW_RESTART we want to reuse the
2440 * same indices, so we pass them when this function is called.
2441 *
2442 * In D3 entry, we need to hardcoded the indices (because the
2443 * firmware hardcodes the PTK offset to 0). In this case, we
2444 * need to make sure we don't overwrite the hw_key_idx in the
2445 * keyconf structure, because otherwise we cannot configure
2446 * the original ones back when resuming.
2447 */
2448 if (key_offset == STA_KEY_IDX_INVALID) {
2449 key_offset = iwl_mvm_set_fw_key_idx(mvm);
2450 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002451 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002452 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002453 }
2454
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002455 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002456 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01002457 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002458
2459 /*
2460 * For WEP, the same key is used for multicast and unicast. Upload it
2461 * again, using the same key offset, and now pointing the other one
2462 * to the same key slot (offset).
2463 * If this fails, remove the original as well.
2464 */
2465 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2466 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002467 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
2468 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01002469 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01002470 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002471 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002472 }
2473 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01002474
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002475 __set_bit(key_offset, mvm->fw_key_table);
2476
Johannes Berg2f6319d2014-11-12 23:39:56 +01002477end:
2478 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2479 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03002480 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002481 return ret;
2482}
2483
2484int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2485 struct ieee80211_vif *vif,
2486 struct ieee80211_sta *sta,
2487 struct ieee80211_key_conf *keyconf)
2488{
Johannes Bergba3943b2014-11-12 23:54:48 +01002489 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002490 struct iwl_mvm_sta *mvm_sta;
2491 u8 sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg2dc2a152015-06-16 17:09:18 +02002492 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002493
2494 lockdep_assert_held(&mvm->mutex);
2495
Johannes Berg5f7a1842015-12-11 09:36:10 +01002496 /* Get the station from the mvm local station table */
2497 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002498
2499 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2500 keyconf->keyidx, sta_id);
2501
2502 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
2503 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2504
2505 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2506 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2507 keyconf->hw_key_idx);
2508 return -ENOENT;
2509 }
2510
Johannes Berg2dc2a152015-06-16 17:09:18 +02002511 /* track which key was deleted last */
2512 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2513 if (mvm->fw_key_deleted[i] < U8_MAX)
2514 mvm->fw_key_deleted[i]++;
2515 }
2516 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2517
Johannes Berg5f7a1842015-12-11 09:36:10 +01002518 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01002519 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2520 return 0;
2521 }
2522
Johannes Berg5f7a1842015-12-11 09:36:10 +01002523 sta_id = mvm_sta->sta_id;
2524
Johannes Bergba3943b2014-11-12 23:54:48 +01002525 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2526 if (ret)
2527 return ret;
2528
2529 /* delete WEP key twice to get rid of (now useless) offset */
2530 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2531 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2532 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2533
2534 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002535}
2536
Johannes Berg8ca151b2013-01-24 14:25:36 +01002537void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2538 struct ieee80211_vif *vif,
2539 struct ieee80211_key_conf *keyconf,
2540 struct ieee80211_sta *sta, u32 iv32,
2541 u16 *phase1key)
2542{
Beni Levc3eb5362013-02-06 17:22:18 +02002543 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01002544 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002545
Beni Levc3eb5362013-02-06 17:22:18 +02002546 rcu_read_lock();
2547
Johannes Berg5f7a1842015-12-11 09:36:10 +01002548 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2549 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002550 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01002551 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002552 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002553
2554 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02002555 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01002556}
2557
Johannes Berg9cc40712013-02-15 22:47:48 +01002558void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
2559 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002560{
Johannes Berg5b577a92013-11-14 18:20:04 +01002561 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002562 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002563 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002564 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03002565 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01002566 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002567 };
2568 int ret;
2569
Sara Sharon854c5702016-01-26 13:17:47 +02002570 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2571 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002572 if (ret)
2573 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2574}
2575
Johannes Berg9cc40712013-02-15 22:47:48 +01002576void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
2577 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002578 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01002579 u16 cnt, u16 tids, bool more_data,
2580 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002581{
Johannes Berg5b577a92013-11-14 18:20:04 +01002582 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002583 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002584 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002585 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002586 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
2587 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01002588 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002589 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01002590 int tid, ret;
2591 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002592
Johannes Berg3e56ead2013-02-15 22:23:18 +01002593 /* convert TIDs to ACs - we don't support TSPEC so that's OK
2594 * Note that this field is reserved and unused by firmware not
2595 * supporting GO uAPSD, so it's safe to always do this.
2596 */
2597 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
2598 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
2599
2600 /* If we're releasing frames from aggregation queues then check if the
2601 * all queues combined that we're releasing frames from have
2602 * - more frames than the service period, in which case more_data
2603 * needs to be set
2604 * - fewer than 'cnt' frames, in which case we need to adjust the
2605 * firmware command (but do that unconditionally)
2606 */
2607 if (agg) {
2608 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002609 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002610
2611 spin_lock_bh(&mvmsta->lock);
2612 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
2613 struct iwl_mvm_tid_data *tid_data;
2614 u16 n_queued;
2615
2616 tid_data = &mvmsta->tid_data[tid];
2617 if (WARN(tid_data->state != IWL_AGG_ON &&
2618 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
2619 "TID %d state is %d\n",
2620 tid, tid_data->state)) {
2621 spin_unlock_bh(&mvmsta->lock);
2622 ieee80211_sta_eosp(sta);
2623 return;
2624 }
2625
2626 n_queued = iwl_mvm_tid_queued(tid_data);
2627 if (n_queued > remaining) {
2628 more_data = true;
2629 remaining = 0;
2630 break;
2631 }
2632 remaining -= n_queued;
2633 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002634 sleep_tx_count = cnt - remaining;
2635 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
2636 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002637 spin_unlock_bh(&mvmsta->lock);
2638
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002639 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01002640 if (WARN_ON(cnt - remaining == 0)) {
2641 ieee80211_sta_eosp(sta);
2642 return;
2643 }
2644 }
2645
2646 /* Note: this is ignored by firmware not supporting GO uAPSD */
2647 if (more_data)
2648 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
2649
2650 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
2651 mvmsta->next_status_eosp = true;
2652 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
2653 } else {
2654 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
2655 }
2656
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02002657 /* block the Tx queues until the FW updated the sleep Tx count */
2658 iwl_trans_block_txq_ptrs(mvm->trans, true);
2659
2660 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
2661 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02002662 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002663 if (ret)
2664 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2665}
Johannes Berg3e56ead2013-02-15 22:23:18 +01002666
Johannes Berg04168412015-06-23 21:22:09 +02002667void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
2668 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01002669{
2670 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2671 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
2672 struct ieee80211_sta *sta;
2673 u32 sta_id = le32_to_cpu(notif->sta_id);
2674
2675 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02002676 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002677
2678 rcu_read_lock();
2679 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2680 if (!IS_ERR_OR_NULL(sta))
2681 ieee80211_sta_eosp(sta);
2682 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01002683}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002684
2685void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
2686 struct iwl_mvm_sta *mvmsta, bool disable)
2687{
2688 struct iwl_mvm_add_sta_cmd cmd = {
2689 .add_modify = STA_MODE_MODIFY,
2690 .sta_id = mvmsta->sta_id,
2691 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
2692 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
2693 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2694 };
2695 int ret;
2696
Sara Sharon854c5702016-01-26 13:17:47 +02002697 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2698 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002699 if (ret)
2700 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2701}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002702
2703void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
2704 struct ieee80211_sta *sta,
2705 bool disable)
2706{
2707 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2708
2709 spin_lock_bh(&mvm_sta->lock);
2710
2711 if (mvm_sta->disable_tx == disable) {
2712 spin_unlock_bh(&mvm_sta->lock);
2713 return;
2714 }
2715
2716 mvm_sta->disable_tx = disable;
2717
2718 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03002719 * Tell mac80211 to start/stop queuing tx for this station,
2720 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002721 * for this station.
2722 */
2723 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
2724 ieee80211_sta_block_awake(mvm->hw, sta, disable);
2725
2726 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
2727
2728 spin_unlock_bh(&mvm_sta->lock);
2729}
2730
2731void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
2732 struct iwl_mvm_vif *mvmvif,
2733 bool disable)
2734{
2735 struct ieee80211_sta *sta;
2736 struct iwl_mvm_sta *mvm_sta;
2737 int i;
2738
2739 lockdep_assert_held(&mvm->mutex);
2740
2741 /* Block/unblock all the stations of the given mvmvif */
2742 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
2743 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
2744 lockdep_is_held(&mvm->mutex));
2745 if (IS_ERR_OR_NULL(sta))
2746 continue;
2747
2748 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2749 if (mvm_sta->mac_id_n_color !=
2750 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
2751 continue;
2752
2753 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
2754 }
2755}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02002756
2757void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2758{
2759 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2760 struct iwl_mvm_sta *mvmsta;
2761
2762 rcu_read_lock();
2763
2764 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
2765
2766 if (!WARN_ON(!mvmsta))
2767 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2768
2769 rcu_read_unlock();
2770}