blob: 64b07b114d3c943dfa3423d2c9c82894e2b44cde [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 Kaufman24afba72015-07-28 18:56:08 +0300313static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
314 struct ieee80211_sta *sta, u8 ac, int tid,
315 struct ieee80211_hdr *hdr)
316{
317 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
318 struct iwl_trans_txq_scd_cfg cfg = {
319 .fifo = iwl_mvm_ac_to_tx_fifo[ac],
320 .sta_id = mvmsta->sta_id,
321 .tid = tid,
322 .frame_limit = IWL_FRAME_LIMIT,
323 };
324 unsigned int wdg_timeout =
325 iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
326 u8 mac_queue = mvmsta->vif->hw_queue[ac];
327 int queue = -1;
328 int ssn;
Liad Kaufmancf961e12015-08-13 19:16:08 +0300329 int ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300330
331 lockdep_assert_held(&mvm->mutex);
332
Liad Kaufmand2515a92016-03-23 16:31:08 +0200333 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300334
335 /*
336 * Non-QoS, QoS NDP and MGMT frames should go to a MGMT queue, if one
337 * exists
338 */
339 if (!ieee80211_is_data_qos(hdr->frame_control) ||
340 ieee80211_is_qos_nullfunc(hdr->frame_control)) {
341 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_MGMT_QUEUE,
342 IWL_MVM_DQA_MAX_MGMT_QUEUE);
343 if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
344 IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
345 queue);
346
347 /* If no such queue is found, we'll use a DATA queue instead */
348 }
349
350 if (queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
351 queue = mvmsta->reserved_queue;
352 IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
353 }
354
355 if (queue < 0)
356 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_DATA_QUEUE,
357 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufmancf961e12015-08-13 19:16:08 +0300358
359 /*
360 * Mark TXQ as ready, even though it hasn't been fully configured yet,
361 * to make sure no one else takes it.
362 * This will allow avoiding re-acquiring the lock at the end of the
363 * configuration. On error we'll mark it back as free.
364 */
Liad Kaufman24afba72015-07-28 18:56:08 +0300365 if (queue >= 0)
Liad Kaufmancf961e12015-08-13 19:16:08 +0300366 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman24afba72015-07-28 18:56:08 +0300367
Liad Kaufmand2515a92016-03-23 16:31:08 +0200368 spin_unlock_bh(&mvm->queue_info_lock);
Liad Kaufman24afba72015-07-28 18:56:08 +0300369
370 /* TODO: support shared queues for same RA */
371 if (queue < 0)
372 return -ENOSPC;
373
374 /*
375 * Actual en/disablement of aggregations is through the ADD_STA HCMD,
376 * but for configuring the SCD to send A-MPDUs we need to mark the queue
377 * as aggregatable.
378 * Mark all DATA queues as allowing to be aggregated at some point
379 */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300380 cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
381 queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300382
383 IWL_DEBUG_TX_QUEUES(mvm, "Allocating queue #%d to sta %d on tid %d\n",
384 queue, mvmsta->sta_id, tid);
385
386 ssn = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
387 iwl_mvm_enable_txq(mvm, queue, mac_queue, ssn, &cfg,
388 wdg_timeout);
389
390 spin_lock_bh(&mvmsta->lock);
391 mvmsta->tid_data[tid].txq_id = queue;
392 mvmsta->tfd_queue_msk |= BIT(queue);
393
394 if (mvmsta->reserved_queue == queue)
395 mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
396 spin_unlock_bh(&mvmsta->lock);
397
Liad Kaufmancf961e12015-08-13 19:16:08 +0300398 ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
399 if (ret)
400 goto out_err;
401
402 return 0;
403
404out_err:
405 iwl_mvm_disable_txq(mvm, queue, mac_queue, tid, 0);
406
407 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300408}
409
410static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
411{
412 if (tid == IWL_MAX_TID_COUNT)
413 return IEEE80211_AC_VO; /* MGMT */
414
415 return tid_to_mac80211_ac[tid];
416}
417
418static void iwl_mvm_tx_deferred_stream(struct iwl_mvm *mvm,
419 struct ieee80211_sta *sta, int tid)
420{
421 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
422 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
423 struct sk_buff *skb;
424 struct ieee80211_hdr *hdr;
425 struct sk_buff_head deferred_tx;
426 u8 mac_queue;
427 bool no_queue = false; /* Marks if there is a problem with the queue */
428 u8 ac;
429
430 lockdep_assert_held(&mvm->mutex);
431
432 skb = skb_peek(&tid_data->deferred_tx_frames);
433 if (!skb)
434 return;
435 hdr = (void *)skb->data;
436
437 ac = iwl_mvm_tid_to_ac_queue(tid);
438 mac_queue = IEEE80211_SKB_CB(skb)->hw_queue;
439
440 if (tid_data->txq_id == IEEE80211_INVAL_HW_QUEUE &&
441 iwl_mvm_sta_alloc_queue(mvm, sta, ac, tid, hdr)) {
442 IWL_ERR(mvm,
443 "Can't alloc TXQ for sta %d tid %d - dropping frame\n",
444 mvmsta->sta_id, tid);
445
446 /*
447 * Mark queue as problematic so later the deferred traffic is
448 * freed, as we can do nothing with it
449 */
450 no_queue = true;
451 }
452
453 __skb_queue_head_init(&deferred_tx);
454
Liad Kaufmand2515a92016-03-23 16:31:08 +0200455 /* Disable bottom-halves when entering TX path */
456 local_bh_disable();
Liad Kaufman24afba72015-07-28 18:56:08 +0300457 spin_lock(&mvmsta->lock);
458 skb_queue_splice_init(&tid_data->deferred_tx_frames, &deferred_tx);
459 spin_unlock(&mvmsta->lock);
460
Liad Kaufman24afba72015-07-28 18:56:08 +0300461 while ((skb = __skb_dequeue(&deferred_tx)))
462 if (no_queue || iwl_mvm_tx_skb(mvm, skb, sta))
463 ieee80211_free_txskb(mvm->hw, skb);
464 local_bh_enable();
465
466 /* Wake queue */
467 iwl_mvm_start_mac_queues(mvm, BIT(mac_queue));
468}
469
470void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
471{
472 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
473 add_stream_wk);
474 struct ieee80211_sta *sta;
475 struct iwl_mvm_sta *mvmsta;
476 unsigned long deferred_tid_traffic;
477 int sta_id, tid;
478
479 mutex_lock(&mvm->mutex);
480
481 /* Go over all stations with deferred traffic */
482 for_each_set_bit(sta_id, mvm->sta_deferred_frames,
483 IWL_MVM_STATION_COUNT) {
484 clear_bit(sta_id, mvm->sta_deferred_frames);
485 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
486 lockdep_is_held(&mvm->mutex));
487 if (IS_ERR_OR_NULL(sta))
488 continue;
489
490 mvmsta = iwl_mvm_sta_from_mac80211(sta);
491 deferred_tid_traffic = mvmsta->deferred_traffic_tid_map;
492
493 for_each_set_bit(tid, &deferred_tid_traffic,
494 IWL_MAX_TID_COUNT + 1)
495 iwl_mvm_tx_deferred_stream(mvm, sta, tid);
496 }
497
498 mutex_unlock(&mvm->mutex);
499}
500
501static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
Liad Kaufmand5216a22015-08-09 15:50:51 +0300502 struct ieee80211_sta *sta,
503 enum nl80211_iftype vif_type)
Liad Kaufman24afba72015-07-28 18:56:08 +0300504{
505 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
506 int queue;
507
508 spin_lock_bh(&mvm->queue_info_lock);
509
510 /* Make sure we have free resources for this STA */
Liad Kaufmand5216a22015-08-09 15:50:51 +0300511 if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
512 !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].hw_queue_refcount &&
Liad Kaufmancf961e12015-08-13 19:16:08 +0300513 (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
514 IWL_MVM_QUEUE_FREE))
Liad Kaufmand5216a22015-08-09 15:50:51 +0300515 queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
516 else
517 queue = iwl_mvm_find_free_queue(mvm, IWL_MVM_DQA_MIN_DATA_QUEUE,
518 IWL_MVM_DQA_MAX_DATA_QUEUE);
Liad Kaufman24afba72015-07-28 18:56:08 +0300519 if (queue < 0) {
520 spin_unlock_bh(&mvm->queue_info_lock);
521 IWL_ERR(mvm, "No available queues for new station\n");
522 return -ENOSPC;
523 }
Liad Kaufmancf961e12015-08-13 19:16:08 +0300524 mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman24afba72015-07-28 18:56:08 +0300525
526 spin_unlock_bh(&mvm->queue_info_lock);
527
528 mvmsta->reserved_queue = queue;
529
530 IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
531 queue, mvmsta->sta_id);
532
533 return 0;
534}
535
Johannes Berg8ca151b2013-01-24 14:25:36 +0100536int iwl_mvm_add_sta(struct iwl_mvm *mvm,
537 struct ieee80211_vif *vif,
538 struct ieee80211_sta *sta)
539{
540 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100541 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Sara Sharona571f5f2015-12-07 12:50:58 +0200542 struct iwl_mvm_rxq_dup_data *dup_data;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100543 int i, ret, sta_id;
544
545 lockdep_assert_held(&mvm->mutex);
546
547 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
Eliad Pellerb92e6612014-01-23 17:58:23 +0200548 sta_id = iwl_mvm_find_free_sta_id(mvm,
549 ieee80211_vif_type_p2p(vif));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100550 else
551 sta_id = mvm_sta->sta_id;
552
Johannes Berg36f46312015-03-10 20:32:08 +0100553 if (sta_id == IWL_MVM_STATION_COUNT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100554 return -ENOSPC;
555
556 spin_lock_init(&mvm_sta->lock);
557
558 mvm_sta->sta_id = sta_id;
559 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
560 mvmvif->color);
561 mvm_sta->vif = vif;
562 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300563 mvm_sta->tx_protection = 0;
564 mvm_sta->tt_tx_protection = false;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100565
566 /* HW restart, don't assume the memory has been zeroed */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300567 atomic_set(&mvm->pending_frames[sta_id], 0);
Liad Kaufman69191af2015-09-01 18:50:22 +0300568 mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100569 mvm_sta->tfd_queue_msk = 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300570
571 /* allocate new queues for a TDLS station */
572 if (sta->tdls) {
573 ret = iwl_mvm_tdls_sta_init(mvm, sta);
574 if (ret)
575 return ret;
Liad Kaufman24afba72015-07-28 18:56:08 +0300576 } else if (!iwl_mvm_is_dqa_supported(mvm)) {
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300577 for (i = 0; i < IEEE80211_NUM_ACS; i++)
578 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
579 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
580 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100581
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200582 /* for HW restart - reset everything but the sequence number */
Liad Kaufman24afba72015-07-28 18:56:08 +0300583 for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200584 u16 seq = mvm_sta->tid_data[i].seq_number;
585 memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
586 mvm_sta->tid_data[i].seq_number = seq;
Liad Kaufman24afba72015-07-28 18:56:08 +0300587
588 if (!iwl_mvm_is_dqa_supported(mvm))
589 continue;
590
591 /*
592 * Mark all queues for this STA as unallocated and defer TX
593 * frames until the queue is allocated
594 */
595 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
596 skb_queue_head_init(&mvm_sta->tid_data[i].deferred_tx_frames);
Johannes Berg6d9d32b2013-08-06 18:58:56 +0200597 }
Liad Kaufman24afba72015-07-28 18:56:08 +0300598 mvm_sta->deferred_traffic_tid_map = 0;
Eyal Shapiraefed6642014-09-14 15:58:53 +0300599 mvm_sta->agg_tids = 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100600
Sara Sharona571f5f2015-12-07 12:50:58 +0200601 if (iwl_mvm_has_new_rx_api(mvm) &&
602 !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
603 dup_data = kcalloc(mvm->trans->num_rx_queues,
604 sizeof(*dup_data),
605 GFP_KERNEL);
606 if (!dup_data)
607 return -ENOMEM;
608 mvm_sta->dup_data = dup_data;
609 }
610
Liad Kaufman24afba72015-07-28 18:56:08 +0300611 if (iwl_mvm_is_dqa_supported(mvm)) {
Liad Kaufmand5216a22015-08-09 15:50:51 +0300612 ret = iwl_mvm_reserve_sta_stream(mvm, sta,
613 ieee80211_vif_type_p2p(vif));
Liad Kaufman24afba72015-07-28 18:56:08 +0300614 if (ret)
615 goto err;
616 }
617
618 ret = iwl_mvm_sta_send_to_fw(mvm, sta, false, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100619 if (ret)
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300620 goto err;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100621
Johannes Berg9e848012014-08-04 14:33:42 +0200622 if (vif->type == NL80211_IFTYPE_STATION) {
623 if (!sta->tdls) {
624 WARN_ON(mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT);
625 mvmvif->ap_sta_id = sta_id;
626 } else {
627 WARN_ON(mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT);
628 }
629 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100630
631 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
632
633 return 0;
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300634
635err:
636 iwl_mvm_tdls_sta_deinit(mvm, sta);
637 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100638}
639
Johannes Berg7a453972013-02-12 13:10:44 +0100640int iwl_mvm_update_sta(struct iwl_mvm *mvm,
641 struct ieee80211_vif *vif,
642 struct ieee80211_sta *sta)
643{
Liad Kaufman24afba72015-07-28 18:56:08 +0300644 return iwl_mvm_sta_send_to_fw(mvm, sta, true, 0);
Johannes Berg7a453972013-02-12 13:10:44 +0100645}
646
Johannes Berg8ca151b2013-01-24 14:25:36 +0100647int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
648 bool drain)
649{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300650 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +0100651 int ret;
652 u32 status;
653
654 lockdep_assert_held(&mvm->mutex);
655
656 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
657 cmd.sta_id = mvmsta->sta_id;
658 cmd.add_modify = STA_MODE_MODIFY;
659 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
660 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
661
662 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +0200663 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
664 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300665 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100666 if (ret)
667 return ret;
668
Sara Sharon837c4da2016-01-07 16:50:45 +0200669 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100670 case ADD_STA_SUCCESS:
671 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
672 mvmsta->sta_id);
673 break;
674 default:
675 ret = -EIO;
676 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
677 mvmsta->sta_id);
678 break;
679 }
680
681 return ret;
682}
683
684/*
685 * Remove a station from the FW table. Before sending the command to remove
686 * the station validate that the station is indeed known to the driver (sanity
687 * only).
688 */
689static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
690{
691 struct ieee80211_sta *sta;
692 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
693 .sta_id = sta_id,
694 };
695 int ret;
696
697 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
698 lockdep_is_held(&mvm->mutex));
699
700 /* Note: internal stations are marked as error values */
701 if (!sta) {
702 IWL_ERR(mvm, "Invalid station id\n");
703 return -EINVAL;
704 }
705
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300706 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100707 sizeof(rm_sta_cmd), &rm_sta_cmd);
708 if (ret) {
709 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
710 return ret;
711 }
712
713 return 0;
714}
715
716void iwl_mvm_sta_drained_wk(struct work_struct *wk)
717{
718 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
719 u8 sta_id;
720
721 /*
722 * The mutex is needed because of the SYNC cmd, but not only: if the
723 * work would run concurrently with iwl_mvm_rm_sta, it would run before
724 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
725 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
726 * that later.
727 */
728 mutex_lock(&mvm->mutex);
729
730 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
731 int ret;
732 struct ieee80211_sta *sta =
733 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
734 lockdep_is_held(&mvm->mutex));
735
Johannes Berg1ddbbb02013-12-04 22:39:17 +0100736 /*
737 * This station is in use or RCU-removed; the latter happens in
738 * managed mode, where mac80211 removes the station before we
739 * can remove it from firmware (we can only do that after the
740 * MAC is marked unassociated), and possibly while the deauth
741 * frame to disconnect from the AP is still queued. Then, the
742 * station pointer is -ENOENT when the last skb is reclaimed.
743 */
744 if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100745 continue;
746
747 if (PTR_ERR(sta) == -EINVAL) {
748 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
749 sta_id);
750 continue;
751 }
752
753 if (!sta) {
754 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
755 sta_id);
756 continue;
757 }
758
759 WARN_ON(PTR_ERR(sta) != -EBUSY);
760 /* This station was removed and we waited until it got drained,
761 * we can now proceed and remove it.
762 */
763 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
764 if (ret) {
765 IWL_ERR(mvm,
766 "Couldn't remove sta %d after it was drained\n",
767 sta_id);
768 continue;
769 }
Monam Agarwalc531c772014-03-24 00:05:56 +0530770 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100771 clear_bit(sta_id, mvm->sta_drained);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300772
773 if (mvm->tfd_drained[sta_id]) {
774 unsigned long i, msk = mvm->tfd_drained[sta_id];
775
Emmanuel Grumbacha4ca3ed2015-01-20 17:07:10 +0200776 for_each_set_bit(i, &msk, sizeof(msk) * BITS_PER_BYTE)
Arik Nemtsov06ecdba2015-10-12 14:47:11 +0300777 iwl_mvm_disable_txq(mvm, i, i,
778 IWL_MAX_TID_COUNT, 0);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300779
780 mvm->tfd_drained[sta_id] = 0;
781 IWL_DEBUG_TDLS(mvm, "Drained sta %d, with queues %ld\n",
782 sta_id, msk);
783 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100784 }
785
786 mutex_unlock(&mvm->mutex);
787}
788
Liad Kaufman24afba72015-07-28 18:56:08 +0300789static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
790 struct ieee80211_vif *vif,
791 struct iwl_mvm_sta *mvm_sta)
792{
793 int ac;
794 int i;
795
796 lockdep_assert_held(&mvm->mutex);
797
798 for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
799 if (mvm_sta->tid_data[i].txq_id == IEEE80211_INVAL_HW_QUEUE)
800 continue;
801
802 ac = iwl_mvm_tid_to_ac_queue(i);
803 iwl_mvm_disable_txq(mvm, mvm_sta->tid_data[i].txq_id,
804 vif->hw_queue[ac], i, 0);
805 mvm_sta->tid_data[i].txq_id = IEEE80211_INVAL_HW_QUEUE;
806 }
807}
808
Johannes Berg8ca151b2013-01-24 14:25:36 +0100809int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
810 struct ieee80211_vif *vif,
811 struct ieee80211_sta *sta)
812{
813 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg9d8ce6a2014-12-23 16:02:40 +0100814 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100815 int ret;
816
817 lockdep_assert_held(&mvm->mutex);
818
Sara Sharona571f5f2015-12-07 12:50:58 +0200819 if (iwl_mvm_has_new_rx_api(mvm))
820 kfree(mvm_sta->dup_data);
821
Liad Kaufmana6f035a2015-08-24 15:23:14 +0300822 if ((vif->type == NL80211_IFTYPE_STATION &&
823 mvmvif->ap_sta_id == mvm_sta->sta_id) ||
824 iwl_mvm_is_dqa_supported(mvm)){
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200825 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
826 if (ret)
827 return ret;
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200828 /* flush its queues here since we are freeing mvm_sta */
Luca Coelho5888a402015-10-06 09:54:57 +0300829 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, 0);
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +0200830 if (ret)
831 return ret;
832 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
833 mvm_sta->tfd_queue_msk);
834 if (ret)
835 return ret;
836 ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
Emmanuel Grumbach80d85652013-02-19 15:32:42 +0200837
Liad Kaufman24afba72015-07-28 18:56:08 +0300838 /* If DQA is supported - the queues can be disabled now */
839 if (iwl_mvm_is_dqa_supported(mvm))
840 iwl_mvm_disable_sta_queues(mvm, vif, mvm_sta);
841
Johannes Berg8ca151b2013-01-24 14:25:36 +0100842 /* if we are associated - we can't remove the AP STA now */
843 if (vif->bss_conf.assoc)
844 return ret;
845
846 /* unassoc - go ahead - remove the AP STA now */
847 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
Eliad Peller37577fe2013-12-05 17:19:39 +0200848
849 /* clear d0i3_ap_sta_id if no longer relevant */
850 if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
851 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100852 }
853
854 /*
Arik Nemtsov1d3c3f62014-10-23 18:03:10 +0300855 * This shouldn't happen - the TDLS channel switch should be canceled
856 * before the STA is removed.
857 */
858 if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == mvm_sta->sta_id)) {
859 mvm->tdls_cs.peer.sta_id = IWL_MVM_STATION_COUNT;
860 cancel_delayed_work(&mvm->tdls_cs.dwork);
861 }
862
863 /*
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300864 * Make sure that the tx response code sees the station as -EBUSY and
865 * calls the drain worker.
866 */
867 spin_lock_bh(&mvm_sta->lock);
868 /*
Johannes Berg8ca151b2013-01-24 14:25:36 +0100869 * There are frames pending on the AC queues for this station.
870 * We need to wait until all the frames are drained...
871 */
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300872 if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100873 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
874 ERR_PTR(-EBUSY));
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300875 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300876
877 /* disable TDLS sta queues on drain complete */
878 if (sta->tdls) {
879 mvm->tfd_drained[mvm_sta->sta_id] =
880 mvm_sta->tfd_queue_msk;
881 IWL_DEBUG_TDLS(mvm, "Draining TDLS sta %d\n",
882 mvm_sta->sta_id);
883 }
884
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300885 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100886 } else {
Emmanuel Grumbache3d4bc82013-05-07 14:08:24 +0300887 spin_unlock_bh(&mvm_sta->lock);
Arik Nemtsova0f6bf22014-09-21 19:10:04 +0300888
889 if (sta->tdls)
890 iwl_mvm_tdls_sta_deinit(mvm, sta);
891
Johannes Berg8ca151b2013-01-24 14:25:36 +0100892 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
Monam Agarwalc531c772014-03-24 00:05:56 +0530893 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100894 }
895
896 return ret;
897}
898
899int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
900 struct ieee80211_vif *vif,
901 u8 sta_id)
902{
903 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
904
905 lockdep_assert_held(&mvm->mutex);
906
Monam Agarwalc531c772014-03-24 00:05:56 +0530907 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100908 return ret;
909}
910
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +0200911int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
912 struct iwl_mvm_int_sta *sta,
913 u32 qmask, enum nl80211_iftype iftype)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100914{
915 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
Eliad Pellerb92e6612014-01-23 17:58:23 +0200916 sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100917 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
918 return -ENOSPC;
919 }
920
921 sta->tfd_queue_msk = qmask;
922
923 /* put a non-NULL value so iterating over the stations won't stop */
924 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
925 return 0;
926}
927
Johannes Berg712b24a2014-08-04 14:14:14 +0200928static void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm,
929 struct iwl_mvm_int_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100930{
Monam Agarwalc531c772014-03-24 00:05:56 +0530931 RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100932 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
933 sta->sta_id = IWL_MVM_STATION_COUNT;
934}
935
936static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
937 struct iwl_mvm_int_sta *sta,
938 const u8 *addr,
939 u16 mac_id, u16 color)
940{
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300941 struct iwl_mvm_add_sta_cmd cmd;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100942 int ret;
943 u32 status;
944
945 lockdep_assert_held(&mvm->mutex);
946
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300947 memset(&cmd, 0, sizeof(cmd));
Johannes Berg8ca151b2013-01-24 14:25:36 +0100948 cmd.sta_id = sta->sta_id;
949 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
950 color));
951
952 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
Liad Kaufmancf0cda12015-09-24 10:44:12 +0200953 cmd.tid_disable_tx = cpu_to_le16(0xffff);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100954
955 if (addr)
956 memcpy(cmd.addr, addr, ETH_ALEN);
957
Sara Sharon854c5702016-01-26 13:17:47 +0200958 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
959 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +0300960 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100961 if (ret)
962 return ret;
963
Sara Sharon837c4da2016-01-07 16:50:45 +0200964 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100965 case ADD_STA_SUCCESS:
966 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
967 return 0;
968 default:
969 ret = -EIO;
970 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
971 status);
972 break;
973 }
974 return ret;
975}
976
977int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
978{
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200979 unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
980 mvm->cfg->base_params->wd_timeout :
981 IWL_WATCHDOG_DISABLED;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100982 int ret;
983
984 lockdep_assert_held(&mvm->mutex);
985
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300986 /* Map Aux queue to fifo - needs to happen before adding Aux station */
Liad Kaufman4ecafae2015-07-14 13:36:18 +0300987 iwl_mvm_enable_ac_txq(mvm, mvm->aux_queue, mvm->aux_queue,
Liad Kaufman5c1156e2015-07-22 17:59:53 +0300988 IWL_MVM_TX_FIFO_MCAST, 0, wdg_timeout);
Ariej Marjieh7da91b02014-07-07 12:09:40 +0300989
990 /* Allocate aux station and assign to it the aux queue */
991 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
Eliad Pellerb92e6612014-01-23 17:58:23 +0200992 NL80211_IFTYPE_UNSPECIFIED);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100993 if (ret)
994 return ret;
995
996 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
997 MAC_INDEX_AUX, 0);
998
999 if (ret)
1000 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1001 return ret;
1002}
1003
Chaya Rachel Ivgi0e39eb02015-12-03 15:51:46 +02001004int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1005{
1006 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1007
1008 lockdep_assert_held(&mvm->mutex);
1009 return iwl_mvm_add_int_sta_common(mvm, &mvm->snif_sta, vif->addr,
1010 mvmvif->id, 0);
1011}
1012
1013int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1014{
1015 int ret;
1016
1017 lockdep_assert_held(&mvm->mutex);
1018
1019 ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
1020 if (ret)
1021 IWL_WARN(mvm, "Failed sending remove station\n");
1022
1023 return ret;
1024}
1025
1026void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
1027{
1028 iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
1029}
1030
Johannes Berg712b24a2014-08-04 14:14:14 +02001031void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
1032{
1033 lockdep_assert_held(&mvm->mutex);
1034
1035 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
1036}
1037
Johannes Berg8ca151b2013-01-24 14:25:36 +01001038/*
1039 * Send the add station command for the vif's broadcast station.
1040 * Assumes that the station was already allocated.
1041 *
1042 * @mvm: the mvm component
1043 * @vif: the interface to which the broadcast station is added
1044 * @bsta: the broadcast station to add.
1045 */
Johannes Berg013290a2014-08-04 13:38:48 +02001046int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001047{
1048 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001049 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg5023d962013-07-31 14:07:43 +02001050 static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
Johannes Berga4243402014-01-20 23:46:38 +01001051 const u8 *baddr = _baddr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001052
1053 lockdep_assert_held(&mvm->mutex);
1054
Liad Kaufmande24f632015-08-04 15:19:18 +03001055 if (iwl_mvm_is_dqa_supported(mvm)) {
1056 struct iwl_trans_txq_scd_cfg cfg = {
1057 .fifo = IWL_MVM_TX_FIFO_VO,
1058 .sta_id = mvmvif->bcast_sta.sta_id,
1059 .tid = IWL_MAX_TID_COUNT,
1060 .aggregate = false,
1061 .frame_limit = IWL_FRAME_LIMIT,
1062 };
1063 unsigned int wdg_timeout =
1064 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
1065 int queue;
1066
1067 if ((vif->type == NL80211_IFTYPE_AP) &&
1068 (mvmvif->bcast_sta.tfd_queue_msk &
1069 BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE)))
1070 queue = IWL_MVM_DQA_AP_PROBE_RESP_QUEUE;
Liad Kaufman4c965132015-08-09 19:26:56 +03001071 else if ((vif->type == NL80211_IFTYPE_P2P_DEVICE) &&
1072 (mvmvif->bcast_sta.tfd_queue_msk &
1073 BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE)))
1074 queue = IWL_MVM_DQA_P2P_DEVICE_QUEUE;
Liad Kaufmande24f632015-08-04 15:19:18 +03001075 else if (WARN(1, "Missed required TXQ for adding bcast STA\n"))
1076 return -EINVAL;
1077
1078 iwl_mvm_enable_txq(mvm, queue, vif->hw_queue[0], 0, &cfg,
1079 wdg_timeout);
1080 }
1081
Johannes Berg5023d962013-07-31 14:07:43 +02001082 if (vif->type == NL80211_IFTYPE_ADHOC)
1083 baddr = vif->bss_conf.bssid;
1084
Johannes Berg8ca151b2013-01-24 14:25:36 +01001085 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
1086 return -ENOSPC;
1087
1088 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
1089 mvmvif->id, mvmvif->color);
1090}
1091
1092/* Send the FW a request to remove the station from it's internal data
1093 * structures, but DO NOT remove the entry from the local data structures. */
Johannes Berg013290a2014-08-04 13:38:48 +02001094int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001095{
Johannes Berg013290a2014-08-04 13:38:48 +02001096 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001097 int ret;
1098
1099 lockdep_assert_held(&mvm->mutex);
1100
Johannes Berg013290a2014-08-04 13:38:48 +02001101 ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001102 if (ret)
1103 IWL_WARN(mvm, "Failed sending remove station\n");
1104 return ret;
1105}
1106
Johannes Berg013290a2014-08-04 13:38:48 +02001107int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1108{
1109 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Liad Kaufmande24f632015-08-04 15:19:18 +03001110 u32 qmask = 0;
Johannes Berg013290a2014-08-04 13:38:48 +02001111
1112 lockdep_assert_held(&mvm->mutex);
1113
Liad Kaufmande24f632015-08-04 15:19:18 +03001114 if (!iwl_mvm_is_dqa_supported(mvm))
1115 qmask = iwl_mvm_mac_get_queues_mask(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001116
Liad Kaufmande24f632015-08-04 15:19:18 +03001117 if (vif->type == NL80211_IFTYPE_AP) {
1118 /*
1119 * The firmware defines the TFD queue mask to only be relevant
1120 * for *unicast* queues, so the multicast (CAB) queue shouldn't
1121 * be included.
1122 */
Johannes Berg013290a2014-08-04 13:38:48 +02001123 qmask &= ~BIT(vif->cab_queue);
1124
Liad Kaufmande24f632015-08-04 15:19:18 +03001125 if (iwl_mvm_is_dqa_supported(mvm))
1126 qmask |= BIT(IWL_MVM_DQA_AP_PROBE_RESP_QUEUE);
Liad Kaufman4c965132015-08-09 19:26:56 +03001127 } else if (iwl_mvm_is_dqa_supported(mvm) &&
1128 vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1129 qmask |= BIT(IWL_MVM_DQA_P2P_DEVICE_QUEUE);
Liad Kaufmande24f632015-08-04 15:19:18 +03001130 }
1131
Johannes Berg013290a2014-08-04 13:38:48 +02001132 return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, qmask,
1133 ieee80211_vif_type_p2p(vif));
1134}
1135
Johannes Berg8ca151b2013-01-24 14:25:36 +01001136/* Allocate a new station entry for the broadcast station to the given vif,
1137 * and send it to the FW.
1138 * Note that each P2P mac should have its own broadcast station.
1139 *
1140 * @mvm: the mvm component
1141 * @vif: the interface to which the broadcast station is added
1142 * @bsta: the broadcast station to add. */
Johannes Berg013290a2014-08-04 13:38:48 +02001143int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001144{
1145 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg013290a2014-08-04 13:38:48 +02001146 struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001147 int ret;
1148
1149 lockdep_assert_held(&mvm->mutex);
1150
Johannes Berg013290a2014-08-04 13:38:48 +02001151 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001152 if (ret)
1153 return ret;
1154
Johannes Berg013290a2014-08-04 13:38:48 +02001155 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001156
1157 if (ret)
1158 iwl_mvm_dealloc_int_sta(mvm, bsta);
Johannes Berg013290a2014-08-04 13:38:48 +02001159
Johannes Berg8ca151b2013-01-24 14:25:36 +01001160 return ret;
1161}
1162
Johannes Berg013290a2014-08-04 13:38:48 +02001163void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1164{
1165 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1166
1167 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1168}
1169
Johannes Berg8ca151b2013-01-24 14:25:36 +01001170/*
1171 * Send the FW a request to remove the station from it's internal data
1172 * structures, and in addition remove it from the local data structure.
1173 */
Johannes Berg013290a2014-08-04 13:38:48 +02001174int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001175{
1176 int ret;
1177
1178 lockdep_assert_held(&mvm->mutex);
1179
Johannes Berg013290a2014-08-04 13:38:48 +02001180 ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001181
Johannes Berg013290a2014-08-04 13:38:48 +02001182 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1183
Johannes Berg8ca151b2013-01-24 14:25:36 +01001184 return ret;
1185}
1186
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001187#define IWL_MAX_RX_BA_SESSIONS 16
1188
Sara Sharonb915c102016-03-23 16:32:02 +02001189static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
Sara Sharon10b2b202016-03-20 16:23:41 +02001190{
Sara Sharonb915c102016-03-23 16:32:02 +02001191 struct iwl_mvm_delba_notif notif = {
1192 .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
1193 .metadata.sync = 1,
1194 .delba.baid = baid,
Sara Sharon10b2b202016-03-20 16:23:41 +02001195 };
Sara Sharonb915c102016-03-23 16:32:02 +02001196 iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
1197};
Sara Sharon10b2b202016-03-20 16:23:41 +02001198
Sara Sharonb915c102016-03-23 16:32:02 +02001199static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
1200 struct iwl_mvm_baid_data *data)
1201{
1202 int i;
1203
1204 iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
1205
1206 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1207 int j;
1208 struct iwl_mvm_reorder_buffer *reorder_buf =
1209 &data->reorder_buf[i];
1210
Sara Sharon06904052016-02-28 20:28:17 +02001211 spin_lock_bh(&reorder_buf->lock);
1212 if (likely(!reorder_buf->num_stored)) {
1213 spin_unlock_bh(&reorder_buf->lock);
Sara Sharonb915c102016-03-23 16:32:02 +02001214 continue;
Sara Sharon06904052016-02-28 20:28:17 +02001215 }
Sara Sharonb915c102016-03-23 16:32:02 +02001216
1217 /*
1218 * This shouldn't happen in regular DELBA since the internal
1219 * delBA notification should trigger a release of all frames in
1220 * the reorder buffer.
1221 */
1222 WARN_ON(1);
1223
1224 for (j = 0; j < reorder_buf->buf_size; j++)
1225 __skb_queue_purge(&reorder_buf->entries[j]);
Sara Sharon06904052016-02-28 20:28:17 +02001226 /*
1227 * Prevent timer re-arm. This prevents a very far fetched case
1228 * where we timed out on the notification. There may be prior
1229 * RX frames pending in the RX queue before the notification
1230 * that might get processed between now and the actual deletion
1231 * and we would re-arm the timer although we are deleting the
1232 * reorder buffer.
1233 */
1234 reorder_buf->removed = true;
1235 spin_unlock_bh(&reorder_buf->lock);
1236 del_timer_sync(&reorder_buf->reorder_timer);
Sara Sharonb915c102016-03-23 16:32:02 +02001237 }
1238}
1239
1240static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
1241 u32 sta_id,
1242 struct iwl_mvm_baid_data *data,
1243 u16 ssn, u8 buf_size)
1244{
1245 int i;
1246
1247 for (i = 0; i < mvm->trans->num_rx_queues; i++) {
1248 struct iwl_mvm_reorder_buffer *reorder_buf =
1249 &data->reorder_buf[i];
1250 int j;
1251
1252 reorder_buf->num_stored = 0;
1253 reorder_buf->head_sn = ssn;
1254 reorder_buf->buf_size = buf_size;
Sara Sharon06904052016-02-28 20:28:17 +02001255 /* rx reorder timer */
1256 reorder_buf->reorder_timer.function =
1257 iwl_mvm_reorder_timer_expired;
1258 reorder_buf->reorder_timer.data = (unsigned long)reorder_buf;
1259 init_timer(&reorder_buf->reorder_timer);
1260 spin_lock_init(&reorder_buf->lock);
1261 reorder_buf->mvm = mvm;
Sara Sharonb915c102016-03-23 16:32:02 +02001262 reorder_buf->queue = i;
1263 reorder_buf->sta_id = sta_id;
1264 for (j = 0; j < reorder_buf->buf_size; j++)
1265 __skb_queue_head_init(&reorder_buf->entries[j]);
1266 }
Sara Sharon10b2b202016-03-20 16:23:41 +02001267}
1268
Johannes Berg8ca151b2013-01-24 14:25:36 +01001269int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
Sara Sharon10b2b202016-03-20 16:23:41 +02001270 int tid, u16 ssn, bool start, u8 buf_size, u16 timeout)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001271{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001272 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001273 struct iwl_mvm_add_sta_cmd cmd = {};
Sara Sharon10b2b202016-03-20 16:23:41 +02001274 struct iwl_mvm_baid_data *baid_data = NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001275 int ret;
1276 u32 status;
1277
1278 lockdep_assert_held(&mvm->mutex);
1279
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001280 if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
1281 IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
1282 return -ENOSPC;
1283 }
1284
Sara Sharon10b2b202016-03-20 16:23:41 +02001285 if (iwl_mvm_has_new_rx_api(mvm) && start) {
1286 /*
1287 * Allocate here so if allocation fails we can bail out early
1288 * before starting the BA session in the firmware
1289 */
Sara Sharonb915c102016-03-23 16:32:02 +02001290 baid_data = kzalloc(sizeof(*baid_data) +
1291 mvm->trans->num_rx_queues *
1292 sizeof(baid_data->reorder_buf[0]),
1293 GFP_KERNEL);
Sara Sharon10b2b202016-03-20 16:23:41 +02001294 if (!baid_data)
1295 return -ENOMEM;
1296 }
1297
Johannes Berg8ca151b2013-01-24 14:25:36 +01001298 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1299 cmd.sta_id = mvm_sta->sta_id;
1300 cmd.add_modify = STA_MODE_MODIFY;
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001301 if (start) {
1302 cmd.add_immediate_ba_tid = (u8) tid;
1303 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
Sara Sharon854c5702016-01-26 13:17:47 +02001304 cmd.rx_ba_window = cpu_to_le16((u16)buf_size);
Emmanuel Grumbach93a42662013-07-02 13:35:35 +03001305 } else {
1306 cmd.remove_immediate_ba_tid = (u8) tid;
1307 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001308 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
1309 STA_MODIFY_REMOVE_BA_TID;
1310
1311 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001312 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1313 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001314 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001315 if (ret)
Sara Sharon10b2b202016-03-20 16:23:41 +02001316 goto out_free;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001317
Sara Sharon837c4da2016-01-07 16:50:45 +02001318 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001319 case ADD_STA_SUCCESS:
1320 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
1321 start ? "start" : "stopp");
1322 break;
1323 case ADD_STA_IMMEDIATE_BA_FAILURE:
1324 IWL_WARN(mvm, "RX BA Session refused by fw\n");
1325 ret = -ENOSPC;
1326 break;
1327 default:
1328 ret = -EIO;
1329 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
1330 start ? "start" : "stopp", status);
1331 break;
1332 }
1333
Sara Sharon10b2b202016-03-20 16:23:41 +02001334 if (ret)
1335 goto out_free;
Emmanuel Grumbach113a0442013-07-02 14:16:38 +03001336
Sara Sharon10b2b202016-03-20 16:23:41 +02001337 if (start) {
1338 u8 baid;
1339
1340 mvm->rx_ba_sessions++;
1341
1342 if (!iwl_mvm_has_new_rx_api(mvm))
1343 return 0;
1344
1345 if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
1346 ret = -EINVAL;
1347 goto out_free;
1348 }
1349 baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
1350 IWL_ADD_STA_BAID_SHIFT);
1351 baid_data->baid = baid;
1352 baid_data->timeout = timeout;
1353 baid_data->last_rx = jiffies;
1354 init_timer(&baid_data->session_timer);
1355 baid_data->session_timer.function =
1356 iwl_mvm_rx_agg_session_expired;
1357 baid_data->session_timer.data =
1358 (unsigned long)&mvm->baid_map[baid];
1359 baid_data->mvm = mvm;
1360 baid_data->tid = tid;
1361 baid_data->sta_id = mvm_sta->sta_id;
1362
1363 mvm_sta->tid_to_baid[tid] = baid;
1364 if (timeout)
1365 mod_timer(&baid_data->session_timer,
1366 TU_TO_EXP_TIME(timeout * 2));
1367
Sara Sharonb915c102016-03-23 16:32:02 +02001368 iwl_mvm_init_reorder_buffer(mvm, mvm_sta->sta_id,
1369 baid_data, ssn, buf_size);
Sara Sharon10b2b202016-03-20 16:23:41 +02001370 /*
1371 * protect the BA data with RCU to cover a case where our
1372 * internal RX sync mechanism will timeout (not that it's
1373 * supposed to happen) and we will free the session data while
1374 * RX is being processed in parallel
1375 */
1376 WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
1377 rcu_assign_pointer(mvm->baid_map[baid], baid_data);
1378 } else if (mvm->rx_ba_sessions > 0) {
1379 u8 baid = mvm_sta->tid_to_baid[tid];
1380
1381 /* check that restart flow didn't zero the counter */
1382 mvm->rx_ba_sessions--;
1383 if (!iwl_mvm_has_new_rx_api(mvm))
1384 return 0;
1385
1386 if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
1387 return -EINVAL;
1388
1389 baid_data = rcu_access_pointer(mvm->baid_map[baid]);
1390 if (WARN_ON(!baid_data))
1391 return -EINVAL;
1392
1393 /* synchronize all rx queues so we can safely delete */
Sara Sharonb915c102016-03-23 16:32:02 +02001394 iwl_mvm_free_reorder(mvm, baid_data);
Sara Sharon10b2b202016-03-20 16:23:41 +02001395 del_timer_sync(&baid_data->session_timer);
Sara Sharon10b2b202016-03-20 16:23:41 +02001396 RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
1397 kfree_rcu(baid_data, rcu_head);
1398 }
1399 return 0;
1400
1401out_free:
1402 kfree(baid_data);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001403 return ret;
1404}
1405
1406static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
1407 int tid, u8 queue, bool start)
1408{
Johannes Berg9d8ce6a2014-12-23 16:02:40 +01001409 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001410 struct iwl_mvm_add_sta_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01001411 int ret;
1412 u32 status;
1413
1414 lockdep_assert_held(&mvm->mutex);
1415
1416 if (start) {
1417 mvm_sta->tfd_queue_msk |= BIT(queue);
1418 mvm_sta->tid_disable_agg &= ~BIT(tid);
1419 } else {
Liad Kaufmancf961e12015-08-13 19:16:08 +03001420 /* In DQA-mode the queue isn't removed on agg termination */
1421 if (!iwl_mvm_is_dqa_supported(mvm))
1422 mvm_sta->tfd_queue_msk &= ~BIT(queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001423 mvm_sta->tid_disable_agg |= BIT(tid);
1424 }
1425
1426 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1427 cmd.sta_id = mvm_sta->sta_id;
1428 cmd.add_modify = STA_MODE_MODIFY;
1429 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
1430 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
1431 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
1432
1433 status = ADD_STA_SUCCESS;
Sara Sharon854c5702016-01-26 13:17:47 +02001434 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1435 iwl_mvm_add_sta_cmd_size(mvm),
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001436 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001437 if (ret)
1438 return ret;
1439
Sara Sharon837c4da2016-01-07 16:50:45 +02001440 switch (status & IWL_ADD_STA_STATUS_MASK) {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001441 case ADD_STA_SUCCESS:
1442 break;
1443 default:
1444 ret = -EIO;
1445 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
1446 start ? "start" : "stopp", status);
1447 break;
1448 }
1449
1450 return ret;
1451}
1452
Emmanuel Grumbachb797e3f2014-03-06 14:49:36 +02001453const u8 tid_to_mac80211_ac[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01001454 IEEE80211_AC_BE,
1455 IEEE80211_AC_BK,
1456 IEEE80211_AC_BK,
1457 IEEE80211_AC_BE,
1458 IEEE80211_AC_VI,
1459 IEEE80211_AC_VI,
1460 IEEE80211_AC_VO,
1461 IEEE80211_AC_VO,
1462};
1463
Johannes Berg3e56ead2013-02-15 22:23:18 +01001464static const u8 tid_to_ucode_ac[] = {
1465 AC_BE,
1466 AC_BK,
1467 AC_BK,
1468 AC_BE,
1469 AC_VI,
1470 AC_VI,
1471 AC_VO,
1472 AC_VO,
1473};
1474
Johannes Berg8ca151b2013-01-24 14:25:36 +01001475int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1476 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1477{
Johannes Berg5b577a92013-11-14 18:20:04 +01001478 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001479 struct iwl_mvm_tid_data *tid_data;
1480 int txq_id;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001481 int ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001482
1483 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
1484 return -EINVAL;
1485
1486 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
1487 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
1488 mvmsta->tid_data[tid].state);
1489 return -ENXIO;
1490 }
1491
1492 lockdep_assert_held(&mvm->mutex);
1493
Arik Nemtsovb2492502014-03-13 12:21:50 +02001494 spin_lock_bh(&mvmsta->lock);
1495
1496 /* possible race condition - we entered D0i3 while starting agg */
1497 if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
1498 spin_unlock_bh(&mvmsta->lock);
1499 IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
1500 return -EIO;
1501 }
1502
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001503 spin_lock_bh(&mvm->queue_info_lock);
1504
Liad Kaufmancf961e12015-08-13 19:16:08 +03001505 /*
1506 * Note the possible cases:
1507 * 1. In DQA mode with an enabled TXQ - TXQ needs to become agg'ed
1508 * 2. Non-DQA mode: the TXQ hasn't yet been enabled, so find a free
1509 * one and mark it as reserved
1510 * 3. In DQA mode, but no traffic yet on this TID: same treatment as in
1511 * non-DQA mode, since the TXQ hasn't yet been allocated
1512 */
1513 txq_id = mvmsta->tid_data[tid].txq_id;
1514 if (!iwl_mvm_is_dqa_supported(mvm) ||
1515 mvm->queue_info[txq_id].status != IWL_MVM_QUEUE_READY) {
1516 txq_id = iwl_mvm_find_free_queue(mvm, mvm->first_agg_queue,
1517 mvm->last_agg_queue);
1518 if (txq_id < 0) {
1519 ret = txq_id;
1520 spin_unlock_bh(&mvm->queue_info_lock);
1521 IWL_ERR(mvm, "Failed to allocate agg queue\n");
1522 goto release_locks;
1523 }
1524
1525 /* TXQ hasn't yet been enabled, so mark it only as reserved */
1526 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001527 }
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001528 spin_unlock_bh(&mvm->queue_info_lock);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001529
Liad Kaufmancf961e12015-08-13 19:16:08 +03001530 IWL_DEBUG_TX_QUEUES(mvm,
1531 "AGG for tid %d will be on queue #%d\n",
1532 tid, txq_id);
1533
Johannes Berg8ca151b2013-01-24 14:25:36 +01001534 tid_data = &mvmsta->tid_data[tid];
Johannes Berg9a886582013-02-15 19:25:00 +01001535 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001536 tid_data->txq_id = txq_id;
1537 *ssn = tid_data->ssn;
1538
1539 IWL_DEBUG_TX_QUEUES(mvm,
1540 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
1541 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
1542 tid_data->next_reclaimed);
1543
1544 if (tid_data->ssn == tid_data->next_reclaimed) {
1545 tid_data->state = IWL_AGG_STARTING;
1546 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1547 } else {
1548 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
1549 }
1550
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001551 ret = 0;
1552
1553release_locks:
Johannes Berg8ca151b2013-01-24 14:25:36 +01001554 spin_unlock_bh(&mvmsta->lock);
1555
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001556 return ret;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001557}
1558
1559int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001560 struct ieee80211_sta *sta, u16 tid, u8 buf_size,
1561 bool amsdu)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001562{
Johannes Berg5b577a92013-11-14 18:20:04 +01001563 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001564 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +02001565 unsigned int wdg_timeout =
1566 iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001567 int queue, ret;
Liad Kaufmancf961e12015-08-13 19:16:08 +03001568 bool alloc_queue = true;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001569 u16 ssn;
1570
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001571 struct iwl_trans_txq_scd_cfg cfg = {
1572 .sta_id = mvmsta->sta_id,
1573 .tid = tid,
1574 .frame_limit = buf_size,
1575 .aggregate = true,
1576 };
1577
Eyal Shapiraefed6642014-09-14 15:58:53 +03001578 BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
1579 != IWL_MAX_TID_COUNT);
1580
Johannes Berg8ca151b2013-01-24 14:25:36 +01001581 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
1582
1583 spin_lock_bh(&mvmsta->lock);
1584 ssn = tid_data->ssn;
1585 queue = tid_data->txq_id;
1586 tid_data->state = IWL_AGG_ON;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001587 mvmsta->agg_tids |= BIT(tid);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001588 tid_data->ssn = 0xffff;
Emmanuel Grumbachbb81bb62015-10-26 16:00:29 +02001589 tid_data->amsdu_in_ampdu_allowed = amsdu;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001590 spin_unlock_bh(&mvmsta->lock);
1591
Emmanuel Grumbacheea76c32016-02-21 16:29:17 +02001592 cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
Johannes Berg8ca151b2013-01-24 14:25:36 +01001593
Liad Kaufmancf961e12015-08-13 19:16:08 +03001594 /* In DQA mode, the existing queue might need to be reconfigured */
1595 if (iwl_mvm_is_dqa_supported(mvm)) {
1596 spin_lock_bh(&mvm->queue_info_lock);
1597 /* Maybe there is no need to even alloc a queue... */
1598 if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
1599 alloc_queue = false;
1600 spin_unlock_bh(&mvm->queue_info_lock);
1601
1602 /*
1603 * Only reconfig the SCD for the queue if the window size has
1604 * changed from current (become smaller)
1605 */
1606 if (!alloc_queue && buf_size < mvmsta->max_agg_bufsize) {
1607 /*
1608 * If reconfiguring an existing queue, it first must be
1609 * drained
1610 */
1611 ret = iwl_trans_wait_tx_queue_empty(mvm->trans,
1612 BIT(queue));
1613 if (ret) {
1614 IWL_ERR(mvm,
1615 "Error draining queue before reconfig\n");
1616 return ret;
1617 }
1618
1619 ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
1620 mvmsta->sta_id, tid,
1621 buf_size, ssn);
1622 if (ret) {
1623 IWL_ERR(mvm,
1624 "Error reconfiguring TXQ #%d\n", queue);
1625 return ret;
1626 }
1627 }
1628 }
1629
1630 if (alloc_queue)
1631 iwl_mvm_enable_txq(mvm, queue,
1632 vif->hw_queue[tid_to_mac80211_ac[tid]], ssn,
1633 &cfg, wdg_timeout);
Andrei Otcheretianskifa7878e2015-05-05 09:28:16 +03001634
Johannes Berg8ca151b2013-01-24 14:25:36 +01001635 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1636 if (ret)
1637 return -EIO;
1638
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001639 /* No need to mark as reserved */
1640 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03001641 mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001642 spin_unlock_bh(&mvm->queue_info_lock);
1643
Johannes Berg8ca151b2013-01-24 14:25:36 +01001644 /*
1645 * Even though in theory the peer could have different
1646 * aggregation reorder buffer sizes for different sessions,
1647 * our ucode doesn't allow for that and has a global limit
1648 * for each station. Therefore, use the minimum of all the
1649 * aggregation sessions and our default value.
1650 */
1651 mvmsta->max_agg_bufsize =
1652 min(mvmsta->max_agg_bufsize, buf_size);
1653 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
1654
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +03001655 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
1656 sta->addr, tid);
1657
Eyal Shapira9e680942013-11-09 00:16:16 +02001658 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001659}
1660
1661int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1662 struct ieee80211_sta *sta, u16 tid)
1663{
Johannes Berg5b577a92013-11-14 18:20:04 +01001664 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001665 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1666 u16 txq_id;
1667 int err;
1668
Emmanuel Grumbachf9aa8dd2013-03-04 09:11:08 +02001669
1670 /*
1671 * If mac80211 is cleaning its state, then say that we finished since
1672 * our state has been cleared anyway.
1673 */
1674 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1675 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1676 return 0;
1677 }
1678
Johannes Berg8ca151b2013-01-24 14:25:36 +01001679 spin_lock_bh(&mvmsta->lock);
1680
1681 txq_id = tid_data->txq_id;
1682
1683 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1684 mvmsta->sta_id, tid, txq_id, tid_data->state);
1685
Eyal Shapiraefed6642014-09-14 15:58:53 +03001686 mvmsta->agg_tids &= ~BIT(tid);
1687
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001688 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03001689 /*
1690 * The TXQ is marked as reserved only if no traffic came through yet
1691 * This means no traffic has been sent on this TID (agg'd or not), so
1692 * we no longer have use for the queue. Since it hasn't even been
1693 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
1694 * free.
1695 */
1696 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
1697 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001698 spin_unlock_bh(&mvm->queue_info_lock);
1699
Johannes Berg8ca151b2013-01-24 14:25:36 +01001700 switch (tid_data->state) {
1701 case IWL_AGG_ON:
Johannes Berg9a886582013-02-15 19:25:00 +01001702 tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001703
1704 IWL_DEBUG_TX_QUEUES(mvm,
1705 "ssn = %d, next_recl = %d\n",
1706 tid_data->ssn, tid_data->next_reclaimed);
1707
1708 /* There are still packets for this RA / TID in the HW */
1709 if (tid_data->ssn != tid_data->next_reclaimed) {
1710 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1711 err = 0;
1712 break;
1713 }
1714
1715 tid_data->ssn = 0xffff;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001716 tid_data->state = IWL_AGG_OFF;
Johannes Bergf7f89e72014-08-05 15:24:44 +02001717 spin_unlock_bh(&mvmsta->lock);
1718
1719 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1720
1721 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1722
Liad Kaufmancf961e12015-08-13 19:16:08 +03001723 if (!iwl_mvm_is_dqa_supported(mvm)) {
1724 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
1725
1726 iwl_mvm_disable_txq(mvm, txq_id, mac_queue, tid, 0);
1727 }
Johannes Bergf7f89e72014-08-05 15:24:44 +02001728 return 0;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001729 case IWL_AGG_STARTING:
1730 case IWL_EMPTYING_HW_QUEUE_ADDBA:
1731 /*
1732 * The agg session has been stopped before it was set up. This
1733 * can happen when the AddBA timer times out for example.
1734 */
1735
1736 /* No barriers since we are under mutex */
1737 lockdep_assert_held(&mvm->mutex);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001738
1739 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1740 tid_data->state = IWL_AGG_OFF;
1741 err = 0;
1742 break;
1743 default:
1744 IWL_ERR(mvm,
1745 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1746 mvmsta->sta_id, tid, tid_data->state);
1747 IWL_ERR(mvm,
1748 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1749 err = -EINVAL;
1750 }
1751
1752 spin_unlock_bh(&mvmsta->lock);
1753
1754 return err;
1755}
1756
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001757int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1758 struct ieee80211_sta *sta, u16 tid)
1759{
Johannes Berg5b577a92013-11-14 18:20:04 +01001760 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001761 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1762 u16 txq_id;
Johannes Bergb6658ff2013-07-24 13:55:51 +02001763 enum iwl_mvm_agg_state old_state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001764
1765 /*
1766 * First set the agg state to OFF to avoid calling
1767 * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1768 */
1769 spin_lock_bh(&mvmsta->lock);
1770 txq_id = tid_data->txq_id;
1771 IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1772 mvmsta->sta_id, tid, txq_id, tid_data->state);
Johannes Bergb6658ff2013-07-24 13:55:51 +02001773 old_state = tid_data->state;
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001774 tid_data->state = IWL_AGG_OFF;
Eyal Shapiraefed6642014-09-14 15:58:53 +03001775 mvmsta->agg_tids &= ~BIT(tid);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001776 spin_unlock_bh(&mvmsta->lock);
1777
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001778 spin_lock_bh(&mvm->queue_info_lock);
Liad Kaufmancf961e12015-08-13 19:16:08 +03001779 /*
1780 * The TXQ is marked as reserved only if no traffic came through yet
1781 * This means no traffic has been sent on this TID (agg'd or not), so
1782 * we no longer have use for the queue. Since it hasn't even been
1783 * allocated through iwl_mvm_enable_txq, so we can just mark it back as
1784 * free.
1785 */
1786 if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED)
1787 mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
Liad Kaufman4ecafae2015-07-14 13:36:18 +03001788 spin_unlock_bh(&mvm->queue_info_lock);
1789
Johannes Bergb6658ff2013-07-24 13:55:51 +02001790 if (old_state >= IWL_AGG_ON) {
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001791 iwl_mvm_drain_sta(mvm, mvmsta, true);
Luca Coelho5888a402015-10-06 09:54:57 +03001792 if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
Johannes Bergb6658ff2013-07-24 13:55:51 +02001793 IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
Emmanuel Grumbachfe92e322015-03-11 09:34:31 +02001794 iwl_trans_wait_tx_queue_empty(mvm->trans,
1795 mvmsta->tfd_queue_msk);
1796 iwl_mvm_drain_sta(mvm, mvmsta, false);
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001797
Johannes Bergf7f89e72014-08-05 15:24:44 +02001798 iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
1799
Liad Kaufmancf961e12015-08-13 19:16:08 +03001800 if (!iwl_mvm_is_dqa_supported(mvm)) {
1801 int mac_queue = vif->hw_queue[tid_to_mac80211_ac[tid]];
1802
1803 iwl_mvm_disable_txq(mvm, tid_data->txq_id, mac_queue,
1804 tid, 0);
1805 }
Johannes Bergb6658ff2013-07-24 13:55:51 +02001806 }
1807
Emmanuel Grumbache3d9e7c2013-02-19 16:13:53 +02001808 return 0;
1809}
1810
Johannes Berg8ca151b2013-01-24 14:25:36 +01001811static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1812{
Johannes Berg2dc2a152015-06-16 17:09:18 +02001813 int i, max = -1, max_offs = -1;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001814
1815 lockdep_assert_held(&mvm->mutex);
1816
Johannes Berg2dc2a152015-06-16 17:09:18 +02001817 /* Pick the unused key offset with the highest 'deleted'
1818 * counter. Every time a key is deleted, all the counters
1819 * are incremented and the one that was just deleted is
1820 * reset to zero. Thus, the highest counter is the one
1821 * that was deleted longest ago. Pick that one.
1822 */
1823 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
1824 if (test_bit(i, mvm->fw_key_table))
1825 continue;
1826 if (mvm->fw_key_deleted[i] > max) {
1827 max = mvm->fw_key_deleted[i];
1828 max_offs = i;
1829 }
1830 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001831
Johannes Berg2dc2a152015-06-16 17:09:18 +02001832 if (max_offs < 0)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001833 return STA_KEY_IDX_INVALID;
1834
Johannes Berg2dc2a152015-06-16 17:09:18 +02001835 return max_offs;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001836}
1837
Johannes Berg5f7a1842015-12-11 09:36:10 +01001838static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
1839 struct ieee80211_vif *vif,
1840 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001841{
Johannes Berg5b530e92014-12-23 16:00:17 +01001842 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001843
Johannes Berg5f7a1842015-12-11 09:36:10 +01001844 if (sta)
1845 return iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001846
1847 /*
1848 * The device expects GTKs for station interfaces to be
1849 * installed as GTKs for the AP station. If we have no
1850 * station ID, then use AP's station ID.
1851 */
1852 if (vif->type == NL80211_IFTYPE_STATION &&
Avri Altman9513c5e2015-10-19 16:29:11 +02001853 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1854 u8 sta_id = mvmvif->ap_sta_id;
1855
Avri Altman9513c5e2015-10-19 16:29:11 +02001856 /*
1857 * It is possible that the 'sta' parameter is NULL,
1858 * for example when a GTK is removed - the sta_id will then
1859 * be the AP ID, and no station was passed by mac80211.
1860 */
Sara Sharon13303c02016-04-10 15:51:54 +03001861 return iwl_mvm_sta_from_staid_protected(mvm, sta_id);
Avri Altman9513c5e2015-10-19 16:29:11 +02001862 }
Johannes Berg8ca151b2013-01-24 14:25:36 +01001863
Johannes Berg5f7a1842015-12-11 09:36:10 +01001864 return NULL;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001865}
1866
1867static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1868 struct iwl_mvm_sta *mvm_sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01001869 struct ieee80211_key_conf *keyconf, bool mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001870 u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
1871 u8 key_offset)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001872{
Max Stepanov5a258aa2013-04-07 09:11:21 +03001873 struct iwl_mvm_add_sta_key_cmd cmd = {};
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001874 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01001875 int ret;
1876 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001877 u16 keyidx;
1878 int i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01001879 u8 sta_id = mvm_sta->sta_id;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001880
1881 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1882 STA_KEY_FLG_KEYID_MSK;
1883 key_flags = cpu_to_le16(keyidx);
1884 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1885
1886 switch (keyconf->cipher) {
1887 case WLAN_CIPHER_SUITE_TKIP:
1888 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001889 cmd.tkip_rx_tsc_byte2 = tkip_iv32;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001890 for (i = 0; i < 5; i++)
Max Stepanov5a258aa2013-04-07 09:11:21 +03001891 cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1892 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001893 break;
1894 case WLAN_CIPHER_SUITE_CCMP:
1895 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
Max Stepanov5a258aa2013-04-07 09:11:21 +03001896 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001897 break;
Johannes Bergba3943b2014-11-12 23:54:48 +01001898 case WLAN_CIPHER_SUITE_WEP104:
1899 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
John W. Linvilleaa0cb082015-01-12 16:18:11 -05001900 /* fall through */
Johannes Bergba3943b2014-11-12 23:54:48 +01001901 case WLAN_CIPHER_SUITE_WEP40:
1902 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
1903 memcpy(cmd.key + 3, keyconf->key, keyconf->keylen);
1904 break;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001905 default:
Max Stepanove36e5432013-08-27 19:56:13 +03001906 key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1907 memcpy(cmd.key, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001908 }
1909
Johannes Bergba3943b2014-11-12 23:54:48 +01001910 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001911 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1912
Luca Coelhod6ee54a2015-11-10 22:13:43 +02001913 cmd.key_offset = key_offset;
Max Stepanov5a258aa2013-04-07 09:11:21 +03001914 cmd.key_flags = key_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001915 cmd.sta_id = sta_id;
1916
1917 status = ADD_STA_SUCCESS;
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001918 if (cmd_flags & CMD_ASYNC)
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03001919 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC,
1920 sizeof(cmd), &cmd);
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001921 else
1922 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
1923 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001924
1925 switch (status) {
1926 case ADD_STA_SUCCESS:
1927 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1928 break;
1929 default:
1930 ret = -EIO;
1931 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1932 break;
1933 }
1934
1935 return ret;
1936}
1937
1938static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1939 struct ieee80211_key_conf *keyconf,
1940 u8 sta_id, bool remove_key)
1941{
1942 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1943
1944 /* verify the key details match the required command's expectations */
1945 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1946 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1947 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1948 return -EINVAL;
1949
1950 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1951 igtk_cmd.sta_id = cpu_to_le32(sta_id);
1952
1953 if (remove_key) {
1954 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1955 } else {
1956 struct ieee80211_key_seq seq;
1957 const u8 *pn;
1958
1959 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001960 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1961 pn = seq.aes_cmac.pn;
1962 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1963 ((u64) pn[4] << 8) |
1964 ((u64) pn[3] << 16) |
1965 ((u64) pn[2] << 24) |
1966 ((u64) pn[1] << 32) |
1967 ((u64) pn[0] << 40));
1968 }
1969
1970 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1971 remove_key ? "removing" : "installing",
1972 igtk_cmd.sta_id);
1973
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001974 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001975 sizeof(igtk_cmd), &igtk_cmd);
1976}
1977
1978
1979static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1980 struct ieee80211_vif *vif,
1981 struct ieee80211_sta *sta)
1982{
Johannes Berg5b530e92014-12-23 16:00:17 +01001983 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001984
1985 if (sta)
1986 return sta->addr;
1987
1988 if (vif->type == NL80211_IFTYPE_STATION &&
1989 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1990 u8 sta_id = mvmvif->ap_sta_id;
1991 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1992 lockdep_is_held(&mvm->mutex));
1993 return sta->addr;
1994 }
1995
1996
1997 return NULL;
1998}
1999
Johannes Berg2f6319d2014-11-12 23:39:56 +01002000static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2001 struct ieee80211_vif *vif,
2002 struct ieee80211_sta *sta,
Johannes Bergba3943b2014-11-12 23:54:48 +01002003 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002004 u8 key_offset,
Johannes Bergba3943b2014-11-12 23:54:48 +01002005 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002006{
Johannes Berg2f6319d2014-11-12 23:39:56 +01002007 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002008 int ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002009 const u8 *addr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002010 struct ieee80211_key_seq seq;
2011 u16 p1k[5];
2012
Johannes Berg8ca151b2013-01-24 14:25:36 +01002013 switch (keyconf->cipher) {
2014 case WLAN_CIPHER_SUITE_TKIP:
2015 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
2016 /* get phase 1 key from mac80211 */
2017 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
2018 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Bergba3943b2014-11-12 23:54:48 +01002019 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002020 seq.tkip.iv32, p1k, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002021 break;
2022 case WLAN_CIPHER_SUITE_CCMP:
Johannes Bergba3943b2014-11-12 23:54:48 +01002023 case WLAN_CIPHER_SUITE_WEP40:
2024 case WLAN_CIPHER_SUITE_WEP104:
2025 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002026 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002027 break;
2028 default:
Johannes Bergba3943b2014-11-12 23:54:48 +01002029 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002030 0, NULL, 0, key_offset);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002031 }
2032
Johannes Berg8ca151b2013-01-24 14:25:36 +01002033 return ret;
2034}
2035
Johannes Berg2f6319d2014-11-12 23:39:56 +01002036static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
Johannes Bergba3943b2014-11-12 23:54:48 +01002037 struct ieee80211_key_conf *keyconf,
2038 bool mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002039{
Max Stepanov5a258aa2013-04-07 09:11:21 +03002040 struct iwl_mvm_add_sta_key_cmd cmd = {};
Johannes Berg8ca151b2013-01-24 14:25:36 +01002041 __le16 key_flags;
Johannes Berg79920742014-11-03 15:43:04 +01002042 int ret;
2043 u32 status;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002044
Emmanuel Grumbach8115efb2013-02-05 10:08:35 +02002045 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2046 STA_KEY_FLG_KEYID_MSK);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002047 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2048 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2049
Johannes Bergba3943b2014-11-12 23:54:48 +01002050 if (mcast)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002051 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2052
Max Stepanov5a258aa2013-04-07 09:11:21 +03002053 cmd.key_flags = key_flags;
2054 cmd.key_offset = keyconf->hw_key_idx;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002055 cmd.sta_id = sta_id;
2056
Johannes Berg8ca151b2013-01-24 14:25:36 +01002057 status = ADD_STA_SUCCESS;
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002058 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, sizeof(cmd),
2059 &cmd, &status);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002060
2061 switch (status) {
2062 case ADD_STA_SUCCESS:
2063 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2064 break;
2065 default:
2066 ret = -EIO;
2067 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2068 break;
2069 }
2070
2071 return ret;
2072}
2073
Johannes Berg2f6319d2014-11-12 23:39:56 +01002074int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
2075 struct ieee80211_vif *vif,
2076 struct ieee80211_sta *sta,
2077 struct ieee80211_key_conf *keyconf,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002078 u8 key_offset)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002079{
Johannes Bergba3943b2014-11-12 23:54:48 +01002080 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002081 struct iwl_mvm_sta *mvm_sta;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002082 u8 sta_id;
2083 int ret;
Matti Gottlieb11828db2015-06-01 15:15:11 +03002084 static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
Johannes Berg2f6319d2014-11-12 23:39:56 +01002085
2086 lockdep_assert_held(&mvm->mutex);
2087
2088 /* Get the station id from the mvm local station table */
Johannes Berg5f7a1842015-12-11 09:36:10 +01002089 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2090 if (!mvm_sta) {
2091 IWL_ERR(mvm, "Failed to find station\n");
Johannes Berg2f6319d2014-11-12 23:39:56 +01002092 return -EINVAL;
2093 }
Johannes Berg5f7a1842015-12-11 09:36:10 +01002094 sta_id = mvm_sta->sta_id;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002095
2096 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
2097 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
2098 goto end;
2099 }
2100
2101 /*
2102 * It is possible that the 'sta' parameter is NULL, and thus
2103 * there is a need to retrieve the sta from the local station table.
2104 */
2105 if (!sta) {
2106 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2107 lockdep_is_held(&mvm->mutex));
2108 if (IS_ERR_OR_NULL(sta)) {
2109 IWL_ERR(mvm, "Invalid station id\n");
2110 return -EINVAL;
2111 }
2112 }
2113
2114 if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
2115 return -EINVAL;
2116
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002117 /* If the key_offset is not pre-assigned, we need to find a
2118 * new offset to use. In normal cases, the offset is not
2119 * pre-assigned, but during HW_RESTART we want to reuse the
2120 * same indices, so we pass them when this function is called.
2121 *
2122 * In D3 entry, we need to hardcoded the indices (because the
2123 * firmware hardcodes the PTK offset to 0). In this case, we
2124 * need to make sure we don't overwrite the hw_key_idx in the
2125 * keyconf structure, because otherwise we cannot configure
2126 * the original ones back when resuming.
2127 */
2128 if (key_offset == STA_KEY_IDX_INVALID) {
2129 key_offset = iwl_mvm_set_fw_key_idx(mvm);
2130 if (key_offset == STA_KEY_IDX_INVALID)
Johannes Berg2f6319d2014-11-12 23:39:56 +01002131 return -ENOSPC;
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002132 keyconf->hw_key_idx = key_offset;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002133 }
2134
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002135 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002136 if (ret)
Johannes Bergba3943b2014-11-12 23:54:48 +01002137 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002138
2139 /*
2140 * For WEP, the same key is used for multicast and unicast. Upload it
2141 * again, using the same key offset, and now pointing the other one
2142 * to the same key slot (offset).
2143 * If this fails, remove the original as well.
2144 */
2145 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2146 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) {
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002147 ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
2148 key_offset, !mcast);
Johannes Bergba3943b2014-11-12 23:54:48 +01002149 if (ret) {
Johannes Bergba3943b2014-11-12 23:54:48 +01002150 __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002151 goto end;
Johannes Bergba3943b2014-11-12 23:54:48 +01002152 }
2153 }
Johannes Berg2f6319d2014-11-12 23:39:56 +01002154
Luca Coelho9c3deeb2015-11-11 01:06:17 +02002155 __set_bit(key_offset, mvm->fw_key_table);
2156
Johannes Berg2f6319d2014-11-12 23:39:56 +01002157end:
2158 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
2159 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Matti Gottlieb11828db2015-06-01 15:15:11 +03002160 sta ? sta->addr : zero_addr, ret);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002161 return ret;
2162}
2163
2164int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
2165 struct ieee80211_vif *vif,
2166 struct ieee80211_sta *sta,
2167 struct ieee80211_key_conf *keyconf)
2168{
Johannes Bergba3943b2014-11-12 23:54:48 +01002169 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg5f7a1842015-12-11 09:36:10 +01002170 struct iwl_mvm_sta *mvm_sta;
2171 u8 sta_id = IWL_MVM_STATION_COUNT;
Johannes Berg2dc2a152015-06-16 17:09:18 +02002172 int ret, i;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002173
2174 lockdep_assert_held(&mvm->mutex);
2175
Johannes Berg5f7a1842015-12-11 09:36:10 +01002176 /* Get the station from the mvm local station table */
2177 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
Johannes Berg2f6319d2014-11-12 23:39:56 +01002178
2179 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
2180 keyconf->keyidx, sta_id);
2181
2182 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
2183 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
2184
2185 if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
2186 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
2187 keyconf->hw_key_idx);
2188 return -ENOENT;
2189 }
2190
Johannes Berg2dc2a152015-06-16 17:09:18 +02002191 /* track which key was deleted last */
2192 for (i = 0; i < STA_KEY_MAX_NUM; i++) {
2193 if (mvm->fw_key_deleted[i] < U8_MAX)
2194 mvm->fw_key_deleted[i]++;
2195 }
2196 mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
2197
Johannes Berg5f7a1842015-12-11 09:36:10 +01002198 if (!mvm_sta) {
Johannes Berg2f6319d2014-11-12 23:39:56 +01002199 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
2200 return 0;
2201 }
2202
Johannes Berg5f7a1842015-12-11 09:36:10 +01002203 sta_id = mvm_sta->sta_id;
2204
Johannes Bergba3943b2014-11-12 23:54:48 +01002205 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
2206 if (ret)
2207 return ret;
2208
2209 /* delete WEP key twice to get rid of (now useless) offset */
2210 if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2211 keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
2212 ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
2213
2214 return ret;
Johannes Berg2f6319d2014-11-12 23:39:56 +01002215}
2216
Johannes Berg8ca151b2013-01-24 14:25:36 +01002217void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
2218 struct ieee80211_vif *vif,
2219 struct ieee80211_key_conf *keyconf,
2220 struct ieee80211_sta *sta, u32 iv32,
2221 u16 *phase1key)
2222{
Beni Levc3eb5362013-02-06 17:22:18 +02002223 struct iwl_mvm_sta *mvm_sta;
Johannes Bergba3943b2014-11-12 23:54:48 +01002224 bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002225
Beni Levc3eb5362013-02-06 17:22:18 +02002226 rcu_read_lock();
2227
Johannes Berg5f7a1842015-12-11 09:36:10 +01002228 mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
2229 if (WARN_ON_ONCE(!mvm_sta))
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002230 goto unlock;
Johannes Bergba3943b2014-11-12 23:54:48 +01002231 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, mcast,
Luca Coelhod6ee54a2015-11-10 22:13:43 +02002232 iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx);
Emmanuel Grumbach12f17212015-12-20 14:48:08 +02002233
2234 unlock:
Beni Levc3eb5362013-02-06 17:22:18 +02002235 rcu_read_unlock();
Johannes Berg8ca151b2013-01-24 14:25:36 +01002236}
2237
Johannes Berg9cc40712013-02-15 22:47:48 +01002238void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
2239 struct ieee80211_sta *sta)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002240{
Johannes Berg5b577a92013-11-14 18:20:04 +01002241 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002242 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002243 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002244 .sta_id = mvmsta->sta_id,
Emmanuel Grumbach5af01772013-06-09 12:59:24 +03002245 .station_flags_msk = cpu_to_le32(STA_FLG_PS),
Johannes Berg9cc40712013-02-15 22:47:48 +01002246 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002247 };
2248 int ret;
2249
Sara Sharon854c5702016-01-26 13:17:47 +02002250 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2251 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002252 if (ret)
2253 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2254}
2255
Johannes Berg9cc40712013-02-15 22:47:48 +01002256void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
2257 struct ieee80211_sta *sta,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002258 enum ieee80211_frame_release_type reason,
Johannes Berg3e56ead2013-02-15 22:23:18 +01002259 u16 cnt, u16 tids, bool more_data,
2260 bool agg)
Johannes Berg8ca151b2013-01-24 14:25:36 +01002261{
Johannes Berg5b577a92013-11-14 18:20:04 +01002262 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
Emmanuel Grumbachf9dc0002014-03-30 09:53:27 +03002263 struct iwl_mvm_add_sta_cmd cmd = {
Johannes Berg8ca151b2013-01-24 14:25:36 +01002264 .add_modify = STA_MODE_MODIFY,
Johannes Berg9cc40712013-02-15 22:47:48 +01002265 .sta_id = mvmsta->sta_id,
Johannes Berg8ca151b2013-01-24 14:25:36 +01002266 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
2267 .sleep_tx_count = cpu_to_le16(cnt),
Johannes Berg9cc40712013-02-15 22:47:48 +01002268 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
Johannes Berg8ca151b2013-01-24 14:25:36 +01002269 };
Johannes Berg3e56ead2013-02-15 22:23:18 +01002270 int tid, ret;
2271 unsigned long _tids = tids;
Johannes Berg8ca151b2013-01-24 14:25:36 +01002272
Johannes Berg3e56ead2013-02-15 22:23:18 +01002273 /* convert TIDs to ACs - we don't support TSPEC so that's OK
2274 * Note that this field is reserved and unused by firmware not
2275 * supporting GO uAPSD, so it's safe to always do this.
2276 */
2277 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
2278 cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
2279
2280 /* If we're releasing frames from aggregation queues then check if the
2281 * all queues combined that we're releasing frames from have
2282 * - more frames than the service period, in which case more_data
2283 * needs to be set
2284 * - fewer than 'cnt' frames, in which case we need to adjust the
2285 * firmware command (but do that unconditionally)
2286 */
2287 if (agg) {
2288 int remaining = cnt;
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002289 int sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002290
2291 spin_lock_bh(&mvmsta->lock);
2292 for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
2293 struct iwl_mvm_tid_data *tid_data;
2294 u16 n_queued;
2295
2296 tid_data = &mvmsta->tid_data[tid];
2297 if (WARN(tid_data->state != IWL_AGG_ON &&
2298 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
2299 "TID %d state is %d\n",
2300 tid, tid_data->state)) {
2301 spin_unlock_bh(&mvmsta->lock);
2302 ieee80211_sta_eosp(sta);
2303 return;
2304 }
2305
2306 n_queued = iwl_mvm_tid_queued(tid_data);
2307 if (n_queued > remaining) {
2308 more_data = true;
2309 remaining = 0;
2310 break;
2311 }
2312 remaining -= n_queued;
2313 }
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002314 sleep_tx_count = cnt - remaining;
2315 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
2316 mvmsta->sleep_tx_count = sleep_tx_count;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002317 spin_unlock_bh(&mvmsta->lock);
2318
Emmanuel Grumbach36be0eb2015-11-05 10:32:31 +02002319 cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
Johannes Berg3e56ead2013-02-15 22:23:18 +01002320 if (WARN_ON(cnt - remaining == 0)) {
2321 ieee80211_sta_eosp(sta);
2322 return;
2323 }
2324 }
2325
2326 /* Note: this is ignored by firmware not supporting GO uAPSD */
2327 if (more_data)
2328 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
2329
2330 if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
2331 mvmsta->next_status_eosp = true;
2332 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
2333 } else {
2334 cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
2335 }
2336
Emmanuel Grumbach156f92f2015-11-24 14:55:18 +02002337 /* block the Tx queues until the FW updated the sleep Tx count */
2338 iwl_trans_block_txq_ptrs(mvm->trans, true);
2339
2340 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
2341 CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
Sara Sharon854c5702016-01-26 13:17:47 +02002342 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +01002343 if (ret)
2344 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2345}
Johannes Berg3e56ead2013-02-15 22:23:18 +01002346
Johannes Berg04168412015-06-23 21:22:09 +02002347void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
2348 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg3e56ead2013-02-15 22:23:18 +01002349{
2350 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2351 struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
2352 struct ieee80211_sta *sta;
2353 u32 sta_id = le32_to_cpu(notif->sta_id);
2354
2355 if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
Johannes Berg04168412015-06-23 21:22:09 +02002356 return;
Johannes Berg3e56ead2013-02-15 22:23:18 +01002357
2358 rcu_read_lock();
2359 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
2360 if (!IS_ERR_OR_NULL(sta))
2361 ieee80211_sta_eosp(sta);
2362 rcu_read_unlock();
Johannes Berg3e56ead2013-02-15 22:23:18 +01002363}
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002364
2365void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
2366 struct iwl_mvm_sta *mvmsta, bool disable)
2367{
2368 struct iwl_mvm_add_sta_cmd cmd = {
2369 .add_modify = STA_MODE_MODIFY,
2370 .sta_id = mvmsta->sta_id,
2371 .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
2372 .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
2373 .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
2374 };
2375 int ret;
2376
Sara Sharon854c5702016-01-26 13:17:47 +02002377 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
2378 iwl_mvm_add_sta_cmd_size(mvm), &cmd);
Andrei Otcheretianski09b0ce12014-05-25 17:07:38 +03002379 if (ret)
2380 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
2381}
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002382
2383void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
2384 struct ieee80211_sta *sta,
2385 bool disable)
2386{
2387 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2388
2389 spin_lock_bh(&mvm_sta->lock);
2390
2391 if (mvm_sta->disable_tx == disable) {
2392 spin_unlock_bh(&mvm_sta->lock);
2393 return;
2394 }
2395
2396 mvm_sta->disable_tx = disable;
2397
2398 /*
Sara Sharon0d365ae2015-03-31 12:24:05 +03002399 * Tell mac80211 to start/stop queuing tx for this station,
2400 * but don't stop queuing if there are still pending frames
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03002401 * for this station.
2402 */
2403 if (disable || !atomic_read(&mvm->pending_frames[mvm_sta->sta_id]))
2404 ieee80211_sta_block_awake(mvm->hw, sta, disable);
2405
2406 iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
2407
2408 spin_unlock_bh(&mvm_sta->lock);
2409}
2410
2411void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
2412 struct iwl_mvm_vif *mvmvif,
2413 bool disable)
2414{
2415 struct ieee80211_sta *sta;
2416 struct iwl_mvm_sta *mvm_sta;
2417 int i;
2418
2419 lockdep_assert_held(&mvm->mutex);
2420
2421 /* Block/unblock all the stations of the given mvmvif */
2422 for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
2423 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
2424 lockdep_is_held(&mvm->mutex));
2425 if (IS_ERR_OR_NULL(sta))
2426 continue;
2427
2428 mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2429 if (mvm_sta->mac_id_n_color !=
2430 FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
2431 continue;
2432
2433 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
2434 }
2435}
Luciano Coelhodc88b4b2014-11-10 11:10:14 +02002436
2437void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2438{
2439 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2440 struct iwl_mvm_sta *mvmsta;
2441
2442 rcu_read_lock();
2443
2444 mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
2445
2446 if (!WARN_ON(!mvmsta))
2447 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
2448
2449 rcu_read_unlock();
2450}